blob: 4716d985338a0cc500977e60708aa9400b25a452 [file] [log] [blame]
Kevin O'Connor114592f2009-09-28 21:32:08 -04001// Code for handling UHCI USB controllers.
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "util.h" // dprintf
8#include "pci.h" // pci_bdf_to_bus
9#include "config.h" // CONFIG_*
10#include "ioport.h" // outw
11#include "usb-uhci.h" // USBLEGSUP
12#include "pci_regs.h" // PCI_BASE_ADDRESS_4
13#include "usb.h" // struct usb_s
Kevin O'Connor4f6563e2012-05-19 22:42:51 -040014#include "biosvar.h" // GET_LOWFLAT
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040015#include "string.h" // memset
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050016
Kevin O'Connor406fad62010-02-28 02:23:19 -050017struct usb_uhci_s {
18 struct usb_s usb;
19 u16 iobase;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050020 struct uhci_qh *control_qh;
Kevin O'Connor406fad62010-02-28 02:23:19 -050021 struct uhci_framelist *framelist;
22};
23
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050024struct uhci_pipe {
25 struct uhci_qh qh;
26 struct uhci_td *next_td;
27 struct usb_pipe pipe;
28 u16 iobase;
29 u8 toggle;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -050030};
31
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050032
33/****************************************************************
34 * Root hub
35 ****************************************************************/
36
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040037// Check if device attached to a given port
38static int
39uhci_hub_detect(struct usbhub_s *hub, u32 port)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050040{
Kevin O'Connor406fad62010-02-28 02:23:19 -050041 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
42 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050043
44 u16 status = inw(ioport);
45 if (!(status & USBPORTSC_CCS))
46 // No device
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040047 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050048
49 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
50
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040051 // Begin reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050052 outw(USBPORTSC_PR, ioport);
53 msleep(USB_TIME_DRSTR);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040054 return 0;
55}
56
57// Reset device on port
58static int
59uhci_hub_reset(struct usbhub_s *hub, u32 port)
60{
61 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
62 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
63
64 // Finish reset on port
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050065 outw(0, ioport);
66 udelay(6); // 64 high-speed bit times
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040067 u16 status = inw(ioport);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050068 if (!(status & USBPORTSC_CCS))
69 // No longer connected
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040070 return -1;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050071 outw(USBPORTSC_PE, ioport);
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040072 return !!(status & USBPORTSC_LSDA);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050073}
74
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040075// Disable port
76static void
77uhci_hub_disconnect(struct usbhub_s *hub, u32 port)
78{
79 struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
80 u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
81 outw(0, ioport);
82}
83
84static struct usbhub_op_s uhci_HubOp = {
85 .detect = uhci_hub_detect,
86 .reset = uhci_hub_reset,
87 .disconnect = uhci_hub_disconnect,
88};
89
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050090// Find any devices connected to the root hub.
91static int
Kevin O'Connor406fad62010-02-28 02:23:19 -050092check_uhci_ports(struct usb_uhci_s *cntl)
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050093{
94 ASSERT32FLAT();
95 struct usbhub_s hub;
96 memset(&hub, 0, sizeof(hub));
Kevin O'Connor406fad62010-02-28 02:23:19 -050097 hub.cntl = &cntl->usb;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040098 hub.portcount = 2;
99 hub.op = &uhci_HubOp;
100 usb_enumerate(&hub);
Kevin O'Connor8ebcac02010-03-09 19:58:23 -0500101 return hub.devcount;
102}
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500103
104
105/****************************************************************
106 * Setup
107 ****************************************************************/
Kevin O'Connor114592f2009-09-28 21:32:08 -0400108
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500109// Wait for next USB frame to start - for ensuring safe memory release.
110static void
111uhci_waittick(u16 iobase)
112{
113 barrier();
114 u16 startframe = inw(iobase + USBFRNUM);
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400115 u32 end = timer_calc(1000 * 5);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500116 for (;;) {
117 if (inw(iobase + USBFRNUM) != startframe)
118 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400119 if (timer_check(end)) {
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500120 warn_timeout();
121 return;
122 }
123 yield();
124 }
125}
126
127static void
128uhci_free_pipes(struct usb_uhci_s *cntl)
129{
130 dprintf(7, "uhci_free_pipes %p\n", cntl);
131
132 struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
133 for (;;) {
134 u32 link = pos->link;
135 if (link == UHCI_PTR_TERM)
136 break;
137 struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
138 struct uhci_pipe *pipe = container_of(next, struct uhci_pipe, qh);
139 if (pipe->pipe.cntl != &cntl->usb)
140 pos->link = next->link;
141 else
142 pos = next;
143 }
144 uhci_waittick(cntl->iobase);
145 for (;;) {
146 struct usb_pipe *usbpipe = cntl->usb.freelist;
147 if (!usbpipe)
148 break;
149 cntl->usb.freelist = usbpipe->freenext;
150 struct uhci_pipe *pipe = container_of(usbpipe, struct uhci_pipe, pipe);
151 free(pipe);
152 }
153}
154
Kevin O'Connor114592f2009-09-28 21:32:08 -0400155static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500156reset_uhci(struct usb_uhci_s *cntl, u16 bdf)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400157{
158 // XXX - don't reset if not needed.
159
160 // Reset PIRQ and SMI
Kevin O'Connor406fad62010-02-28 02:23:19 -0500161 pci_config_writew(bdf, USBLEGSUP, USBLEGSUP_RWC);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400162
163 // Reset the HC
Kevin O'Connor406fad62010-02-28 02:23:19 -0500164 outw(USBCMD_HCRESET, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400165 udelay(5);
166
167 // Disable interrupts and commands (just to be safe).
Kevin O'Connor406fad62010-02-28 02:23:19 -0500168 outw(0, cntl->iobase + USBINTR);
169 outw(0, cntl->iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400170}
171
172static void
Kevin O'Connor406fad62010-02-28 02:23:19 -0500173configure_uhci(void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400174{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500175 struct usb_uhci_s *cntl = data;
176
Kevin O'Connor114592f2009-09-28 21:32:08 -0400177 // Allocate ram for schedule storage
178 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
179 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500180 struct uhci_pipe *intr_pipe = malloc_high(sizeof(*intr_pipe));
181 struct uhci_pipe *term_pipe = malloc_high(sizeof(*term_pipe));
182 if (!term_td || !fl || !intr_pipe || !term_pipe) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500183 warn_noalloc();
Kevin O'Connor406fad62010-02-28 02:23:19 -0500184 goto fail;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400185 }
186
187 // Work around for PIIX errata
188 memset(term_td, 0, sizeof(*term_td));
189 term_td->link = UHCI_PTR_TERM;
190 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
191 | USB_PID_IN);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500192 memset(term_pipe, 0, sizeof(*term_pipe));
193 term_pipe->qh.element = (u32)term_td;
194 term_pipe->qh.link = UHCI_PTR_TERM;
195 term_pipe->pipe.cntl = &cntl->usb;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400196
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500197 // Set schedule to point to primary intr queue head
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500198 memset(intr_pipe, 0, sizeof(*intr_pipe));
199 intr_pipe->qh.element = UHCI_PTR_TERM;
200 intr_pipe->qh.link = (u32)&term_pipe->qh | UHCI_PTR_QH;
201 intr_pipe->pipe.cntl = &cntl->usb;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400202 int i;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500203 for (i=0; i<ARRAY_SIZE(fl->links); i++)
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500204 fl->links[i] = (u32)&intr_pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500205 cntl->framelist = fl;
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500206 cntl->control_qh = &intr_pipe->qh;
Kevin O'Connor95714392010-02-17 22:59:53 -0500207 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400208
209 // Set the frame length to the default: 1 ms exactly
Kevin O'Connor406fad62010-02-28 02:23:19 -0500210 outb(USBSOF_DEFAULT, cntl->iobase + USBSOF);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400211
212 // Store the frame list base address
Kevin O'Connor406fad62010-02-28 02:23:19 -0500213 outl((u32)fl->links, cntl->iobase + USBFLBASEADD);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400214
215 // Set the current frame number
Kevin O'Connor406fad62010-02-28 02:23:19 -0500216 outw(0, cntl->iobase + USBFRNUM);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400217
Kevin O'Connor114592f2009-09-28 21:32:08 -0400218 // Mark as configured and running with a 64-byte max packet.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500219 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->iobase + USBCMD);
220
221 // Find devices
222 int count = check_uhci_ports(cntl);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500223 uhci_free_pipes(cntl);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500224 if (count)
225 // Success
226 return;
227
228 // No devices found - shutdown and free controller.
229 outw(0, cntl->iobase + USBCMD);
230fail:
231 free(term_td);
232 free(fl);
Kevin O'Connor1e4dc8a2012-02-20 12:54:49 -0500233 free(intr_pipe);
234 free(term_pipe);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500235 free(cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400236}
237
Kevin O'Connora5826b52009-10-24 17:57:29 -0400238void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500239uhci_setup(struct pci_device *pci, int busid)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400240{
241 if (! CONFIG_USB_UHCI)
Kevin O'Connora5826b52009-10-24 17:57:29 -0400242 return;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400243 u16 bdf = pci->bdf;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500244 struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
Kevin O'Connorffdcd3a2011-07-10 15:48:00 -0400245 if (!cntl) {
246 warn_noalloc();
247 return;
248 }
Kevin O'Connor406fad62010-02-28 02:23:19 -0500249 memset(cntl, 0, sizeof(*cntl));
250 cntl->usb.busid = busid;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -0400251 cntl->usb.pci = pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500252 cntl->usb.type = USB_TYPE_UHCI;
253 cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
254 & PCI_BASE_ADDRESS_IO_MASK);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400255
Kevin O'Connor9dc243e2010-03-20 17:53:03 -0400256 dprintf(1, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
Kevin O'Connor406fad62010-02-28 02:23:19 -0500257 , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
258 , pci_bdf_to_fn(bdf), cntl->iobase);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400259
Kevin O'Connor406fad62010-02-28 02:23:19 -0500260 pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400261
Kevin O'Connor406fad62010-02-28 02:23:19 -0500262 reset_uhci(cntl, bdf);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400263
Kevin O'Connor406fad62010-02-28 02:23:19 -0500264 run_thread(configure_uhci, cntl);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400265}
266
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500267
268/****************************************************************
269 * End point communication
270 ****************************************************************/
271
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500272static struct usb_pipe *
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500273uhci_alloc_intr_pipe(struct usbdevice_s *usbdev
274 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400275{
Kevin O'Connor406fad62010-02-28 02:23:19 -0500276 struct usb_uhci_s *cntl = container_of(
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500277 usbdev->hub->cntl, struct usb_uhci_s, usb);
278 int frameexp = usb_getFrameExp(usbdev, epdesc);
Kevin O'Connor406fad62010-02-28 02:23:19 -0500279 dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400280
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500281 if (frameexp > 10)
282 frameexp = 10;
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500283 int maxpacket = epdesc->wMaxPacketSize;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500284 // Determine number of entries needed for 2 timer ticks.
285 int ms = 1<<frameexp;
Kevin O'Connor69013372013-07-20 12:08:48 -0400286 int count = DIV_ROUND_UP(ticks_to_ms(2), ms);
Kevin O'Connor0770d672010-03-09 19:33:22 -0500287 count = ALIGN(count, 2);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500288 struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400289 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500290 void *data = malloc_low(maxpacket * count);
291 if (!pipe || !tds || !data) {
Kevin O'Connor95714392010-02-17 22:59:53 -0500292 warn_noalloc();
293 goto fail;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500294 }
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500295 memset(pipe, 0, sizeof(*pipe));
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500296 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
297 int lowspeed = pipe->pipe.speed;
298 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500299 pipe->qh.element = (u32)tds;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500300 pipe->next_td = &tds[0];
Kevin O'Connor406fad62010-02-28 02:23:19 -0500301 pipe->iobase = cntl->iobase;
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500302
Kevin O'Connor114592f2009-09-28 21:32:08 -0400303 int toggle = 0;
304 int i;
305 for (i=0; i<count; i++) {
306 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
307 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
308 | TD_CTRL_ACTIVE);
309 tds[i].token = (uhci_explen(maxpacket) | toggle
310 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
311 | USB_PID_IN);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500312 tds[i].buffer = data + maxpacket * i;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400313 toggle ^= TD_TOKEN_TOGGLE;
314 }
315
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500316 // Add to interrupt schedule.
Kevin O'Connor406fad62010-02-28 02:23:19 -0500317 struct uhci_framelist *fl = cntl->framelist;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500318 if (frameexp == 0) {
319 // Add to existing interrupt entry.
320 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500321 pipe->qh.link = intr_qh->link;
Kevin O'Connor95714392010-02-17 22:59:53 -0500322 barrier();
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500323 intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor406fad62010-02-28 02:23:19 -0500324 if (cntl->control_qh == intr_qh)
325 cntl->control_qh = &pipe->qh;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500326 } else {
327 int startpos = 1<<(frameexp-1);
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500328 pipe->qh.link = fl->links[startpos];
Kevin O'Connor95714392010-02-17 22:59:53 -0500329 barrier();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500330 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500331 fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500332 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400333
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500334 return &pipe->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500335fail:
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500336 free(pipe);
Kevin O'Connor95714392010-02-17 22:59:53 -0500337 free(tds);
Kevin O'Connor09e2f7c2010-02-27 23:50:48 -0500338 free(data);
Kevin O'Connor95714392010-02-17 22:59:53 -0500339 return NULL;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400340}
341
Kevin O'Connor6152c322012-03-10 08:53:58 -0500342struct usb_pipe *
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500343uhci_alloc_pipe(struct usbdevice_s *usbdev
344 , struct usb_endpoint_descriptor *epdesc)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500345{
346 if (! CONFIG_USB_UHCI)
347 return NULL;
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500348 u8 eptype = epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
349 if (eptype == USB_ENDPOINT_XFER_INT)
350 return uhci_alloc_intr_pipe(usbdev, epdesc);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500351 struct usb_uhci_s *cntl = container_of(
352 usbdev->hub->cntl, struct usb_uhci_s, usb);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500353 dprintf(7, "uhci_alloc_async_pipe %p %d\n", &cntl->usb, eptype);
354
355 struct usb_pipe *usbpipe = usb_getFreePipe(&cntl->usb, eptype);
356 if (usbpipe) {
357 // Use previously allocated pipe.
358 usb_desc2pipe(usbpipe, usbdev, epdesc);
359 return usbpipe;
360 }
361
362 // Allocate a new queue head.
363 struct uhci_pipe *pipe;
364 if (eptype == USB_ENDPOINT_XFER_CONTROL)
365 pipe = malloc_tmphigh(sizeof(*pipe));
366 else
367 pipe = malloc_low(sizeof(*pipe));
368 if (!pipe) {
369 warn_noalloc();
370 return NULL;
371 }
372 memset(pipe, 0, sizeof(*pipe));
373 usb_desc2pipe(&pipe->pipe, usbdev, epdesc);
374 pipe->qh.element = UHCI_PTR_TERM;
375 pipe->iobase = cntl->iobase;
376
377 // Add queue head to controller list.
378 struct uhci_qh *control_qh = cntl->control_qh;
379 pipe->qh.link = control_qh->link;
380 barrier();
381 control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
382 if (eptype == USB_ENDPOINT_XFER_CONTROL)
383 cntl->control_qh = &pipe->qh;
384 return &pipe->pipe;
385}
386
387static int
388wait_pipe(struct uhci_pipe *pipe, int timeout)
389{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400390 u32 end = timer_calc(timeout);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500391 for (;;) {
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400392 u32 el_link = GET_LOWFLAT(pipe->qh.element);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500393 if (el_link & UHCI_PTR_TERM)
394 return 0;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400395 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500396 warn_timeout();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400397 u16 iobase = GET_LOWFLAT(pipe->iobase);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500398 struct uhci_td *td = (void*)(el_link & ~UHCI_PTR_BITS);
399 dprintf(1, "Timeout on wait_pipe %p (td=%p s=%x c=%x/%x)\n"
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400400 , pipe, (void*)el_link, GET_LOWFLAT(td->status)
Kevin O'Connor6152c322012-03-10 08:53:58 -0500401 , inw(iobase + USBCMD)
402 , inw(iobase + USBSTS));
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400403 SET_LOWFLAT(pipe->qh.element, UHCI_PTR_TERM);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500404 uhci_waittick(iobase);
405 return -1;
406 }
407 yield();
408 }
409}
410
411static int
412wait_td(struct uhci_td *td)
413{
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400414 u32 end = timer_calc(5000); // XXX - lookup real time.
Kevin O'Connor6152c322012-03-10 08:53:58 -0500415 u32 status;
416 for (;;) {
417 status = td->status;
418 if (!(status & TD_CTRL_ACTIVE))
419 break;
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400420 if (timer_check(end)) {
Kevin O'Connor6152c322012-03-10 08:53:58 -0500421 warn_timeout();
422 return -1;
423 }
424 yield();
425 }
426 if (status & TD_CTRL_ANY_ERROR) {
427 dprintf(1, "wait_td error - status=%x\n", status);
428 return -2;
429 }
430 return 0;
431}
432
433int
434uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
435 , void *data, int datasize)
436{
437 ASSERT32FLAT();
438 if (! CONFIG_USB_UHCI)
439 return -1;
440 dprintf(5, "uhci_control %p\n", p);
441 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
442
443 int maxpacket = pipe->pipe.maxpacket;
444 int lowspeed = pipe->pipe.speed;
445 int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
446
447 // Setup transfer descriptors
448 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
449 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
450 if (!tds) {
451 warn_noalloc();
452 return -1;
453 }
454
455 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
456 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
457 | TD_CTRL_ACTIVE);
458 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
459 | USB_PID_SETUP);
460 tds[0].buffer = (void*)cmd;
461 int toggle = TD_TOKEN_TOGGLE;
462 int i;
463 for (i=1; i<count-1; i++) {
464 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
465 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
466 | TD_CTRL_ACTIVE);
467 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
468 tds[i].token = (uhci_explen(len) | toggle
469 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
470 | (dir ? USB_PID_IN : USB_PID_OUT));
471 tds[i].buffer = data + (i-1) * maxpacket;
472 toggle ^= TD_TOKEN_TOGGLE;
473 }
474 tds[i].link = UHCI_PTR_TERM;
475 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
476 | TD_CTRL_ACTIVE);
477 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
478 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
479 | (dir ? USB_PID_OUT : USB_PID_IN));
480 tds[i].buffer = 0;
481
482 // Transfer data
483 barrier();
484 pipe->qh.element = (u32)&tds[0];
485 int ret = wait_pipe(pipe, 500);
486 free(tds);
487 return ret;
488}
489
490#define STACKTDS 4
491#define TDALIGN 16
492
493int
494uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
495{
496 if (! CONFIG_USB_UHCI)
497 return -1;
498 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
499 dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
500 , &pipe->qh, dir, data, datasize);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400501 int maxpacket = GET_LOWFLAT(pipe->pipe.maxpacket);
502 int lowspeed = GET_LOWFLAT(pipe->pipe.speed);
503 int devaddr = (GET_LOWFLAT(pipe->pipe.devaddr)
504 | (GET_LOWFLAT(pipe->pipe.ep) << 7));
505 int toggle = GET_LOWFLAT(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
Kevin O'Connor6152c322012-03-10 08:53:58 -0500506
507 // Allocate 4 tds on stack (16byte aligned)
508 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
509 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
510 memset(tds, 0, sizeof(*tds) * STACKTDS);
511
512 // Enable tds
513 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400514 SET_LOWFLAT(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500515
516 int tdpos = 0;
517 while (datasize) {
518 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
519 int ret = wait_td(td);
520 if (ret)
521 goto fail;
522
523 int transfer = datasize;
524 if (transfer > maxpacket)
525 transfer = maxpacket;
526 struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
527 , &tds[tdpos % STACKTDS]);
528 td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
529 td->token = (uhci_explen(transfer) | toggle
530 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
531 | (dir ? USB_PID_IN : USB_PID_OUT));
532 td->buffer = data;
533 barrier();
534 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
535 | TD_CTRL_ACTIVE);
536 toggle ^= TD_TOKEN_TOGGLE;
537
538 data += transfer;
539 datasize -= transfer;
540 }
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400541 SET_LOWFLAT(pipe->toggle, !!toggle);
Kevin O'Connor6152c322012-03-10 08:53:58 -0500542 return wait_pipe(pipe, 5000);
543fail:
544 dprintf(1, "uhci_send_bulk failed\n");
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400545 SET_LOWFLAT(pipe->qh.element, UHCI_PTR_TERM);
546 uhci_waittick(GET_LOWFLAT(pipe->iobase));
Kevin O'Connor6152c322012-03-10 08:53:58 -0500547 return -1;
548}
549
Kevin O'Connor114592f2009-09-28 21:32:08 -0400550int
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500551uhci_poll_intr(struct usb_pipe *p, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400552{
553 ASSERT16();
554 if (! CONFIG_USB_UHCI)
555 return -1;
556
Kevin O'Connor357bdfa2010-02-26 08:57:13 -0500557 struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400558 struct uhci_td *td = GET_LOWFLAT(pipe->next_td);
559 u32 status = GET_LOWFLAT(td->status);
560 u32 token = GET_LOWFLAT(td->token);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400561 if (status & TD_CTRL_ACTIVE)
562 // No intrs found.
563 return -1;
564 // XXX - check for errors.
565
566 // Copy data.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400567 void *tddata = GET_LOWFLAT(td->buffer);
568 memcpy_far(GET_SEG(SS), data, SEG_LOW, LOWFLAT2LOW(tddata)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400569 , uhci_expected_length(token));
570
571 // Reenable this td.
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400572 struct uhci_td *next = (void*)(GET_LOWFLAT(td->link) & ~UHCI_PTR_BITS);
573 SET_LOWFLAT(pipe->next_td, next);
Kevin O'Connor95714392010-02-17 22:59:53 -0500574 barrier();
Kevin O'Connor4f6563e2012-05-19 22:42:51 -0400575 SET_LOWFLAT(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400576 | TD_CTRL_ACTIVE));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400577
578 return 0;
579}