242f3ba5aa8c24060e5b16f8c74dc9ab9f418704
[seabios.git] / src / usb-uhci.c
1 // 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
14 #include "farptr.h" // GET_FLATPTR
15
16 struct usb_uhci_s {
17     struct usb_s usb;
18     u16 iobase;
19     struct uhci_qh *control_qh, *bulk_qh;
20     struct uhci_framelist *framelist;
21 };
22
23
24 /****************************************************************
25  * Root hub
26  ****************************************************************/
27
28 // Check if device attached to a given port
29 static int
30 uhci_hub_detect(struct usbhub_s *hub, u32 port)
31 {
32     struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
33     u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
34
35     u16 status = inw(ioport);
36     if (!(status & USBPORTSC_CCS))
37         // No device
38         return -1;
39
40     // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
41
42     // Begin reset on port
43     outw(USBPORTSC_PR, ioport);
44     msleep(USB_TIME_DRSTR);
45     return 0;
46 }
47
48 // Reset device on port
49 static int
50 uhci_hub_reset(struct usbhub_s *hub, u32 port)
51 {
52     struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
53     u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
54
55     // Finish reset on port
56     outw(0, ioport);
57     udelay(6); // 64 high-speed bit times
58     u16 status = inw(ioport);
59     if (!(status & USBPORTSC_CCS))
60         // No longer connected
61         return -1;
62     outw(USBPORTSC_PE, ioport);
63     return !!(status & USBPORTSC_LSDA);
64 }
65
66 // Disable port
67 static void
68 uhci_hub_disconnect(struct usbhub_s *hub, u32 port)
69 {
70     struct usb_uhci_s *cntl = container_of(hub->cntl, struct usb_uhci_s, usb);
71     u16 ioport = cntl->iobase + USBPORTSC1 + port * 2;
72     outw(0, ioport);
73 }
74
75 static struct usbhub_op_s uhci_HubOp = {
76     .detect = uhci_hub_detect,
77     .reset = uhci_hub_reset,
78     .disconnect = uhci_hub_disconnect,
79 };
80
81 // Find any devices connected to the root hub.
82 static int
83 check_uhci_ports(struct usb_uhci_s *cntl)
84 {
85     ASSERT32FLAT();
86     struct usbhub_s hub;
87     memset(&hub, 0, sizeof(hub));
88     hub.cntl = &cntl->usb;
89     hub.portcount = 2;
90     hub.op = &uhci_HubOp;
91     usb_enumerate(&hub);
92     return hub.devcount;
93 }
94
95
96 /****************************************************************
97  * Setup
98  ****************************************************************/
99
100 static void
101 reset_uhci(struct usb_uhci_s *cntl, u16 bdf)
102 {
103     // XXX - don't reset if not needed.
104
105     // Reset PIRQ and SMI
106     pci_config_writew(bdf, USBLEGSUP, USBLEGSUP_RWC);
107
108     // Reset the HC
109     outw(USBCMD_HCRESET, cntl->iobase + USBCMD);
110     udelay(5);
111
112     // Disable interrupts and commands (just to be safe).
113     outw(0, cntl->iobase + USBINTR);
114     outw(0, cntl->iobase + USBCMD);
115 }
116
117 static void
118 configure_uhci(void *data)
119 {
120     struct usb_uhci_s *cntl = data;
121
122     // Allocate ram for schedule storage
123     struct uhci_td *term_td = malloc_high(sizeof(*term_td));
124     struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
125     struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
126     struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
127     if (!term_td || !fl || !intr_qh || !term_qh) {
128         warn_noalloc();
129         goto fail;
130     }
131
132     // Work around for PIIX errata
133     memset(term_td, 0, sizeof(*term_td));
134     term_td->link = UHCI_PTR_TERM;
135     term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
136                       | USB_PID_IN);
137     memset(term_qh, 0, sizeof(*term_qh));
138     term_qh->element = (u32)term_td;
139     term_qh->link = UHCI_PTR_TERM;
140
141     // Set schedule to point to primary intr queue head
142     memset(intr_qh, 0, sizeof(*intr_qh));
143     intr_qh->element = UHCI_PTR_TERM;
144     intr_qh->link = (u32)term_qh | UHCI_PTR_QH;
145     int i;
146     for (i=0; i<ARRAY_SIZE(fl->links); i++)
147         fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
148     cntl->framelist = fl;
149     cntl->control_qh = cntl->bulk_qh = intr_qh;
150     barrier();
151
152     // Set the frame length to the default: 1 ms exactly
153     outb(USBSOF_DEFAULT, cntl->iobase + USBSOF);
154
155     // Store the frame list base address
156     outl((u32)fl->links, cntl->iobase + USBFLBASEADD);
157
158     // Set the current frame number
159     outw(0, cntl->iobase + USBFRNUM);
160
161     // Mark as configured and running with a 64-byte max packet.
162     outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->iobase + USBCMD);
163
164     // Find devices
165     int count = check_uhci_ports(cntl);
166     free_pipe(cntl->usb.defaultpipe);
167     if (count)
168         // Success
169         return;
170
171     // No devices found - shutdown and free controller.
172     outw(0, cntl->iobase + USBCMD);
173 fail:
174     free(term_td);
175     free(fl);
176     free(intr_qh);
177     free(term_qh);
178     free(cntl);
179 }
180
181 void
182 uhci_init(struct pci_device *pci, int busid)
183 {
184     if (! CONFIG_USB_UHCI)
185         return;
186     u16 bdf = pci->bdf;
187     struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
188     if (!cntl) {
189         warn_noalloc();
190         return;
191     }
192     memset(cntl, 0, sizeof(*cntl));
193     cntl->usb.busid = busid;
194     cntl->usb.pci = pci;
195     cntl->usb.type = USB_TYPE_UHCI;
196     cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
197                     & PCI_BASE_ADDRESS_IO_MASK);
198
199     dprintf(1, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
200             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
201             , pci_bdf_to_fn(bdf), cntl->iobase);
202
203     pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
204
205     reset_uhci(cntl, bdf);
206
207     run_thread(configure_uhci, cntl);
208 }
209
210
211 /****************************************************************
212  * End point communication
213  ****************************************************************/
214
215 struct uhci_pipe {
216     struct uhci_qh qh;
217     struct uhci_td *next_td;
218     struct usb_pipe pipe;
219     u16 iobase;
220     u8 toggle;
221 };
222
223 // Wait for next USB frame to start - for ensuring safe memory release.
224 static void
225 uhci_waittick(u16 iobase)
226 {
227     barrier();
228     u16 startframe = inw(iobase + USBFRNUM);
229     u64 end = calc_future_tsc(1000 * 5);
230     for (;;) {
231         if (inw(iobase + USBFRNUM) != startframe)
232             break;
233         if (check_tsc(end)) {
234             warn_timeout();
235             return;
236         }
237         yield();
238     }
239 }
240
241 static int
242 wait_pipe(struct uhci_pipe *pipe, int timeout)
243 {
244     u64 end = calc_future_tsc(timeout);
245     for (;;) {
246         u32 el_link = GET_FLATPTR(pipe->qh.element);
247         if (el_link & UHCI_PTR_TERM)
248             return 0;
249         if (check_tsc(end)) {
250             warn_timeout();
251             struct uhci_td *td = (void*)(el_link & ~UHCI_PTR_BITS);
252             dprintf(1, "Timeout on wait_pipe %p (td=%p s=%x c=%x/%x)\n"
253                     , pipe, (void*)el_link, GET_FLATPTR(td->status)
254                     , inw(pipe->iobase + USBCMD)
255                     , inw(pipe->iobase + USBSTS));
256             SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
257             uhci_waittick(pipe->iobase);
258             return -1;
259         }
260         yield();
261     }
262 }
263
264 void
265 uhci_free_pipe(struct usb_pipe *p)
266 {
267     if (! CONFIG_USB_UHCI)
268         return;
269     dprintf(7, "uhci_free_pipe %p\n", p);
270     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
271     struct usb_uhci_s *cntl = container_of(
272         pipe->pipe.cntl, struct usb_uhci_s, usb);
273
274     struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
275     for (;;) {
276         u32 link = pos->link;
277         if (link == UHCI_PTR_TERM) {
278             // Not found?!  Exit without freeing.
279             warn_internalerror();
280             return;
281         }
282         struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
283         if (next == &pipe->qh) {
284             pos->link = next->link;
285             if (cntl->control_qh == next)
286                 cntl->control_qh = pos;
287             if (cntl->bulk_qh == next)
288                 cntl->bulk_qh = pos;
289             uhci_waittick(cntl->iobase);
290             free(pipe);
291             return;
292         }
293         pos = next;
294     }
295 }
296
297 struct usb_pipe *
298 uhci_alloc_control_pipe(struct usb_pipe *dummy)
299 {
300     if (! CONFIG_USB_UHCI)
301         return NULL;
302     struct usb_uhci_s *cntl = container_of(
303         dummy->cntl, struct usb_uhci_s, usb);
304     dprintf(7, "uhci_alloc_control_pipe %p\n", &cntl->usb);
305
306     // Allocate a queue head.
307     struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
308     if (!pipe) {
309         warn_noalloc();
310         return NULL;
311     }
312     memset(pipe, 0, sizeof(*pipe));
313     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
314     pipe->qh.element = UHCI_PTR_TERM;
315     pipe->iobase = cntl->iobase;
316
317     // Add queue head to controller list.
318     struct uhci_qh *control_qh = cntl->control_qh;
319     pipe->qh.link = control_qh->link;
320     barrier();
321     control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
322     if (cntl->bulk_qh == control_qh)
323         cntl->bulk_qh = &pipe->qh;
324     return &pipe->pipe;
325 }
326
327 int
328 uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
329              , void *data, int datasize)
330 {
331     ASSERT32FLAT();
332     if (! CONFIG_USB_UHCI)
333         return -1;
334     dprintf(5, "uhci_control %p\n", p);
335     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
336
337     int maxpacket = pipe->pipe.maxpacket;
338     int lowspeed = pipe->pipe.speed;
339     int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
340
341     // Setup transfer descriptors
342     int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
343     struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
344     if (!tds) {
345         warn_noalloc();
346         return -1;
347     }
348
349     tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
350     tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
351                      | TD_CTRL_ACTIVE);
352     tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
353                     | USB_PID_SETUP);
354     tds[0].buffer = (void*)cmd;
355     int toggle = TD_TOKEN_TOGGLE;
356     int i;
357     for (i=1; i<count-1; i++) {
358         tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
359         tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
360                          | TD_CTRL_ACTIVE);
361         int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
362         tds[i].token = (uhci_explen(len) | toggle
363                         | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
364                         | (dir ? USB_PID_IN : USB_PID_OUT));
365         tds[i].buffer = data + (i-1) * maxpacket;
366         toggle ^= TD_TOKEN_TOGGLE;
367     }
368     tds[i].link = UHCI_PTR_TERM;
369     tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
370                      | TD_CTRL_ACTIVE);
371     tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
372                     | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
373                     | (dir ? USB_PID_OUT : USB_PID_IN));
374     tds[i].buffer = 0;
375
376     // Transfer data
377     barrier();
378     pipe->qh.element = (u32)&tds[0];
379     int ret = wait_pipe(pipe, 500);
380     free(tds);
381     return ret;
382 }
383
384 struct usb_pipe *
385 uhci_alloc_bulk_pipe(struct usb_pipe *dummy)
386 {
387     if (! CONFIG_USB_UHCI)
388         return NULL;
389     struct usb_uhci_s *cntl = container_of(
390         dummy->cntl, struct usb_uhci_s, usb);
391     dprintf(7, "uhci_alloc_bulk_pipe %p\n", &cntl->usb);
392
393     // Allocate a queue head.
394     struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
395     if (!pipe) {
396         warn_noalloc();
397         return NULL;
398     }
399     memset(pipe, 0, sizeof(*pipe));
400     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
401     pipe->qh.element = UHCI_PTR_TERM;
402     pipe->iobase = cntl->iobase;
403
404     // Add queue head to controller list.
405     struct uhci_qh *bulk_qh = cntl->bulk_qh;
406     pipe->qh.link = bulk_qh->link;
407     barrier();
408     bulk_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
409
410     return &pipe->pipe;
411 }
412
413 static int
414 wait_td(struct uhci_td *td)
415 {
416     u64 end = calc_future_tsc(5000); // XXX - lookup real time.
417     u32 status;
418     for (;;) {
419         status = td->status;
420         if (!(status & TD_CTRL_ACTIVE))
421             break;
422         if (check_tsc(end)) {
423             warn_timeout();
424             return -1;
425         }
426         yield();
427     }
428     if (status & TD_CTRL_ANY_ERROR) {
429         dprintf(1, "wait_td error - status=%x\n", status);
430         return -2;
431     }
432     return 0;
433 }
434
435 #define STACKTDS 4
436 #define TDALIGN 16
437
438 int
439 uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
440 {
441     if (! CONFIG_USB_UHCI)
442         return -1;
443     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
444     dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
445             , &pipe->qh, dir, data, datasize);
446     int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
447     int lowspeed = GET_FLATPTR(pipe->pipe.speed);
448     int devaddr = (GET_FLATPTR(pipe->pipe.devaddr)
449                    | (GET_FLATPTR(pipe->pipe.ep) << 7));
450     int toggle = GET_FLATPTR(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
451
452     // Allocate 4 tds on stack (16byte aligned)
453     u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
454     struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
455     memset(tds, 0, sizeof(*tds) * STACKTDS);
456
457     // Enable tds
458     barrier();
459     SET_FLATPTR(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
460
461     int tdpos = 0;
462     while (datasize) {
463         struct uhci_td *td = &tds[tdpos++ % STACKTDS];
464         int ret = wait_td(td);
465         if (ret)
466             goto fail;
467
468         int transfer = datasize;
469         if (transfer > maxpacket)
470             transfer = maxpacket;
471         struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
472                                                  , &tds[tdpos % STACKTDS]);
473         td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
474         td->token = (uhci_explen(transfer) | toggle
475                      | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
476                      | (dir ? USB_PID_IN : USB_PID_OUT));
477         td->buffer = data;
478         barrier();
479         td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
480                       | TD_CTRL_ACTIVE);
481         toggle ^= TD_TOKEN_TOGGLE;
482
483         data += transfer;
484         datasize -= transfer;
485     }
486     SET_FLATPTR(pipe->toggle, !!toggle);
487     return wait_pipe(pipe, 5000);
488 fail:
489     dprintf(1, "uhci_send_bulk failed\n");
490     SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
491     uhci_waittick(GET_FLATPTR(pipe->iobase));
492     return -1;
493 }
494
495 struct usb_pipe *
496 uhci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
497 {
498     if (! CONFIG_USB_UHCI)
499         return NULL;
500     struct usb_uhci_s *cntl = container_of(
501         dummy->cntl, struct usb_uhci_s, usb);
502     dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
503
504     if (frameexp > 10)
505         frameexp = 10;
506     int maxpacket = dummy->maxpacket;
507     int lowspeed = dummy->speed;
508     int devaddr = dummy->devaddr | (dummy->ep << 7);
509     // Determine number of entries needed for 2 timer ticks.
510     int ms = 1<<frameexp;
511     int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
512     count = ALIGN(count, 2);
513     struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
514     struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
515     void *data = malloc_low(maxpacket * count);
516     if (!pipe || !tds || !data) {
517         warn_noalloc();
518         goto fail;
519     }
520     memset(pipe, 0, sizeof(*pipe));
521     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
522     pipe->qh.element = (u32)tds;
523     pipe->next_td = &tds[0];
524     pipe->iobase = cntl->iobase;
525
526     int toggle = 0;
527     int i;
528     for (i=0; i<count; i++) {
529         tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
530         tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
531                          | TD_CTRL_ACTIVE);
532         tds[i].token = (uhci_explen(maxpacket) | toggle
533                         | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
534                         | USB_PID_IN);
535         tds[i].buffer = data + maxpacket * i;
536         toggle ^= TD_TOKEN_TOGGLE;
537     }
538
539     // Add to interrupt schedule.
540     struct uhci_framelist *fl = cntl->framelist;
541     if (frameexp == 0) {
542         // Add to existing interrupt entry.
543         struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
544         pipe->qh.link = intr_qh->link;
545         barrier();
546         intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
547         if (cntl->control_qh == intr_qh)
548             cntl->control_qh = &pipe->qh;
549         if (cntl->bulk_qh == intr_qh)
550             cntl->bulk_qh = &pipe->qh;
551     } else {
552         int startpos = 1<<(frameexp-1);
553         pipe->qh.link = fl->links[startpos];
554         barrier();
555         for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
556             fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
557     }
558
559     return &pipe->pipe;
560 fail:
561     free(pipe);
562     free(tds);
563     free(data);
564     return NULL;
565 }
566
567 int
568 uhci_poll_intr(struct usb_pipe *p, void *data)
569 {
570     ASSERT16();
571     if (! CONFIG_USB_UHCI)
572         return -1;
573
574     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
575     struct uhci_td *td = GET_FLATPTR(pipe->next_td);
576     u32 status = GET_FLATPTR(td->status);
577     u32 token = GET_FLATPTR(td->token);
578     if (status & TD_CTRL_ACTIVE)
579         // No intrs found.
580         return -1;
581     // XXX - check for errors.
582
583     // Copy data.
584     void *tddata = GET_FLATPTR(td->buffer);
585     memcpy_far(GET_SEG(SS), data
586                , FLATPTR_TO_SEG(tddata), (void*)FLATPTR_TO_OFFSET(tddata)
587                , uhci_expected_length(token));
588
589     // Reenable this td.
590     struct uhci_td *next = (void*)(GET_FLATPTR(td->link) & ~UHCI_PTR_BITS);
591     SET_FLATPTR(pipe->next_td, next);
592     barrier();
593     SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
594                              | TD_CTRL_ACTIVE));
595
596     return 0;
597 }