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