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