Extend 'usb_pipe' to track the controller and ports of each device.
[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(u16 bdf, int busid)
183 {
184     if (! CONFIG_USB_UHCI)
185         return;
186     struct usb_uhci_s *cntl = malloc_tmphigh(sizeof(*cntl));
187     memset(cntl, 0, sizeof(*cntl));
188     cntl->usb.busid = busid;
189     cntl->usb.bdf = bdf;
190     cntl->usb.type = USB_TYPE_UHCI;
191     cntl->iobase = (pci_config_readl(bdf, PCI_BASE_ADDRESS_4)
192                     & PCI_BASE_ADDRESS_IO_MASK);
193
194     dprintf(1, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
195             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
196             , pci_bdf_to_fn(bdf), cntl->iobase);
197
198     pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
199
200     reset_uhci(cntl, bdf);
201
202     run_thread(configure_uhci, cntl);
203 }
204
205
206 /****************************************************************
207  * End point communication
208  ****************************************************************/
209
210 static int
211 wait_qh(struct usb_uhci_s *cntl, struct uhci_qh *qh)
212 {
213     // XXX - 500ms just a guess
214     u64 end = calc_future_tsc(500);
215     for (;;) {
216         if (qh->element & UHCI_PTR_TERM)
217             return 0;
218         if (check_tsc(end)) {
219             warn_timeout();
220             struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
221             dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
222                     , qh, td, td->status
223                     , inw(cntl->iobase + USBCMD)
224                     , inw(cntl->iobase + USBSTS));
225             return -1;
226         }
227         yield();
228     }
229 }
230
231 // Wait for next USB frame to start - for ensuring safe memory release.
232 static void
233 uhci_waittick(u16 iobase)
234 {
235     barrier();
236     u16 startframe = inw(iobase + USBFRNUM);
237     u64 end = calc_future_tsc(1000 * 5);
238     for (;;) {
239         if (inw(iobase + USBFRNUM) != startframe)
240             break;
241         if (check_tsc(end)) {
242             warn_timeout();
243             return;
244         }
245         yield();
246     }
247 }
248
249 struct uhci_pipe {
250     struct uhci_qh qh;
251     struct uhci_td *next_td;
252     struct usb_pipe pipe;
253     u16 iobase;
254     u8 toggle;
255 };
256
257 void
258 uhci_free_pipe(struct usb_pipe *p)
259 {
260     if (! CONFIG_USB_UHCI)
261         return;
262     dprintf(7, "uhci_free_pipe %p\n", p);
263     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
264     struct usb_uhci_s *cntl = container_of(
265         pipe->pipe.cntl, struct usb_uhci_s, usb);
266
267     struct uhci_qh *pos = (void*)(cntl->framelist->links[0] & ~UHCI_PTR_BITS);
268     for (;;) {
269         u32 link = pos->link;
270         if (link == UHCI_PTR_TERM) {
271             // Not found?!  Exit without freeing.
272             warn_internalerror();
273             return;
274         }
275         struct uhci_qh *next = (void*)(link & ~UHCI_PTR_BITS);
276         if (next == &pipe->qh) {
277             pos->link = next->link;
278             if (cntl->control_qh == next)
279                 cntl->control_qh = pos;
280             if (cntl->bulk_qh == next)
281                 cntl->bulk_qh = pos;
282             uhci_waittick(cntl->iobase);
283             free(pipe);
284             return;
285         }
286         pos = next;
287     }
288 }
289
290 struct usb_pipe *
291 uhci_alloc_control_pipe(struct usb_pipe *dummy)
292 {
293     if (! CONFIG_USB_UHCI)
294         return NULL;
295     struct usb_uhci_s *cntl = container_of(
296         dummy->cntl, struct usb_uhci_s, usb);
297     dprintf(7, "uhci_alloc_control_pipe %p\n", &cntl->usb);
298
299     // Allocate a queue head.
300     struct uhci_pipe *pipe = malloc_tmphigh(sizeof(*pipe));
301     if (!pipe) {
302         warn_noalloc();
303         return NULL;
304     }
305     memset(pipe, 0, sizeof(*pipe));
306     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
307     pipe->qh.element = UHCI_PTR_TERM;
308     pipe->iobase = cntl->iobase;
309
310     // Add queue head to controller list.
311     struct uhci_qh *control_qh = cntl->control_qh;
312     pipe->qh.link = control_qh->link;
313     barrier();
314     control_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
315     if (cntl->bulk_qh == control_qh)
316         cntl->bulk_qh = &pipe->qh;
317     return &pipe->pipe;
318 }
319
320 int
321 uhci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
322              , void *data, int datasize)
323 {
324     ASSERT32FLAT();
325     if (! CONFIG_USB_UHCI)
326         return -1;
327     dprintf(5, "uhci_control %p\n", p);
328     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
329     struct usb_uhci_s *cntl = container_of(
330         pipe->pipe.cntl, struct usb_uhci_s, usb);
331
332     int maxpacket = pipe->pipe.maxpacket;
333     int lowspeed = pipe->pipe.speed;
334     int devaddr = pipe->pipe.devaddr | (pipe->pipe.ep << 7);
335
336     // Setup transfer descriptors
337     int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
338     struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
339     if (!tds) {
340         warn_noalloc();
341         return -1;
342     }
343
344     tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
345     tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
346                      | TD_CTRL_ACTIVE);
347     tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
348                     | USB_PID_SETUP);
349     tds[0].buffer = (void*)cmd;
350     int toggle = TD_TOKEN_TOGGLE;
351     int i;
352     for (i=1; i<count-1; i++) {
353         tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
354         tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
355                          | TD_CTRL_ACTIVE);
356         int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
357         tds[i].token = (uhci_explen(len) | toggle
358                         | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
359                         | (dir ? USB_PID_IN : USB_PID_OUT));
360         tds[i].buffer = data + (i-1) * maxpacket;
361         toggle ^= TD_TOKEN_TOGGLE;
362     }
363     tds[i].link = UHCI_PTR_TERM;
364     tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
365                      | TD_CTRL_ACTIVE);
366     tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
367                     | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
368                     | (dir ? USB_PID_OUT : USB_PID_IN));
369     tds[i].buffer = 0;
370
371     // Transfer data
372     barrier();
373     pipe->qh.element = (u32)&tds[0];
374     int ret = wait_qh(cntl, &pipe->qh);
375     if (ret) {
376         pipe->qh.element = UHCI_PTR_TERM;
377         uhci_waittick(pipe->iobase);
378     }
379     free(tds);
380     return ret;
381 }
382
383 struct usb_pipe *
384 uhci_alloc_bulk_pipe(struct usb_pipe *dummy)
385 {
386     if (! CONFIG_USB_UHCI)
387         return NULL;
388     struct usb_uhci_s *cntl = container_of(
389         dummy->cntl, struct usb_uhci_s, usb);
390     dprintf(7, "uhci_alloc_bulk_pipe %p\n", &cntl->usb);
391
392     // Allocate a queue head.
393     struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
394     if (!pipe) {
395         warn_noalloc();
396         return NULL;
397     }
398     memset(pipe, 0, sizeof(*pipe));
399     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
400     pipe->qh.element = UHCI_PTR_TERM;
401     pipe->iobase = cntl->iobase;
402
403     // Add queue head to controller list.
404     struct uhci_qh *bulk_qh = cntl->bulk_qh;
405     pipe->qh.link = bulk_qh->link;
406     barrier();
407     bulk_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
408
409     return &pipe->pipe;
410 }
411
412 static int
413 wait_td(struct uhci_td *td)
414 {
415     u64 end = calc_future_tsc(5000); // XXX - lookup real time.
416     u32 status;
417     for (;;) {
418         status = td->status;
419         if (!(status & TD_CTRL_ACTIVE))
420             break;
421         if (check_tsc(end)) {
422             warn_timeout();
423             return -1;
424         }
425         yield();
426     }
427     if (status & TD_CTRL_ANY_ERROR) {
428         dprintf(1, "wait_td error - status=%x\n", status);
429         return -2;
430     }
431     return 0;
432 }
433
434 #define STACKTDS 4
435 #define TDALIGN 16
436
437 int
438 uhci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
439 {
440     if (! CONFIG_USB_UHCI)
441         return -1;
442     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
443     dprintf(7, "uhci_send_bulk qh=%p dir=%d data=%p size=%d\n"
444             , &pipe->qh, dir, data, datasize);
445     int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
446     int lowspeed = GET_FLATPTR(pipe->pipe.speed);
447     int devaddr = (GET_FLATPTR(pipe->pipe.devaddr)
448                    | (GET_FLATPTR(pipe->pipe.ep) << 7));
449     int toggle = GET_FLATPTR(pipe->toggle) ? TD_TOKEN_TOGGLE : 0;
450
451     // Allocate 4 tds on stack (16byte aligned)
452     u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
453     struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
454     memset(tds, 0, sizeof(*tds) * STACKTDS);
455
456     // Enable tds
457     barrier();
458     SET_FLATPTR(pipe->qh.element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
459
460     int tdpos = 0;
461     while (datasize) {
462         struct uhci_td *td = &tds[tdpos++ % STACKTDS];
463         int ret = wait_td(td);
464         if (ret)
465             goto fail;
466
467         int transfer = datasize;
468         if (transfer > maxpacket)
469             transfer = maxpacket;
470         struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
471                                                  , &tds[tdpos % STACKTDS]);
472         td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
473         td->token = (uhci_explen(transfer) | toggle
474                      | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
475                      | (dir ? USB_PID_IN : USB_PID_OUT));
476         td->buffer = data;
477         barrier();
478         td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
479                       | TD_CTRL_ACTIVE);
480         toggle ^= TD_TOKEN_TOGGLE;
481
482         data += transfer;
483         datasize -= transfer;
484     }
485     int i;
486     for (i=0; i<STACKTDS; i++) {
487         struct uhci_td *td = &tds[tdpos++ % STACKTDS];
488         int ret = wait_td(td);
489         if (ret)
490             goto fail;
491     }
492
493     SET_FLATPTR(pipe->toggle, !!toggle);
494     return 0;
495 fail:
496     dprintf(1, "uhci_send_bulk failed\n");
497     SET_FLATPTR(pipe->qh.element, UHCI_PTR_TERM);
498     uhci_waittick(GET_FLATPTR(pipe->iobase));
499     return -1;
500 }
501
502 struct usb_pipe *
503 uhci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
504 {
505     if (! CONFIG_USB_UHCI)
506         return NULL;
507     struct usb_uhci_s *cntl = container_of(
508         dummy->cntl, struct usb_uhci_s, usb);
509     dprintf(7, "uhci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
510
511     if (frameexp > 10)
512         frameexp = 10;
513     int maxpacket = dummy->maxpacket;
514     int lowspeed = dummy->speed;
515     int devaddr = dummy->devaddr | (dummy->ep << 7);
516     // Determine number of entries needed for 2 timer ticks.
517     int ms = 1<<frameexp;
518     int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
519     count = ALIGN(count, 2);
520     struct uhci_pipe *pipe = malloc_low(sizeof(*pipe));
521     struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
522     void *data = malloc_low(maxpacket * count);
523     if (!pipe || !tds || !data) {
524         warn_noalloc();
525         goto fail;
526     }
527     memset(pipe, 0, sizeof(*pipe));
528     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
529     pipe->qh.element = (u32)tds;
530     pipe->next_td = &tds[0];
531     pipe->iobase = cntl->iobase;
532
533     int toggle = 0;
534     int i;
535     for (i=0; i<count; i++) {
536         tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
537         tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
538                          | TD_CTRL_ACTIVE);
539         tds[i].token = (uhci_explen(maxpacket) | toggle
540                         | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
541                         | USB_PID_IN);
542         tds[i].buffer = data + maxpacket * i;
543         toggle ^= TD_TOKEN_TOGGLE;
544     }
545
546     // Add to interrupt schedule.
547     struct uhci_framelist *fl = cntl->framelist;
548     if (frameexp == 0) {
549         // Add to existing interrupt entry.
550         struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
551         pipe->qh.link = intr_qh->link;
552         barrier();
553         intr_qh->link = (u32)&pipe->qh | UHCI_PTR_QH;
554         if (cntl->control_qh == intr_qh)
555             cntl->control_qh = &pipe->qh;
556         if (cntl->bulk_qh == intr_qh)
557             cntl->bulk_qh = &pipe->qh;
558     } else {
559         int startpos = 1<<(frameexp-1);
560         pipe->qh.link = fl->links[startpos];
561         barrier();
562         for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
563             fl->links[i] = (u32)&pipe->qh | UHCI_PTR_QH;
564     }
565
566     return &pipe->pipe;
567 fail:
568     free(pipe);
569     free(tds);
570     free(data);
571     return NULL;
572 }
573
574 int
575 uhci_poll_intr(struct usb_pipe *p, void *data)
576 {
577     ASSERT16();
578     if (! CONFIG_USB_UHCI)
579         return -1;
580
581     struct uhci_pipe *pipe = container_of(p, struct uhci_pipe, pipe);
582     struct uhci_td *td = GET_FLATPTR(pipe->next_td);
583     u32 status = GET_FLATPTR(td->status);
584     u32 token = GET_FLATPTR(td->token);
585     if (status & TD_CTRL_ACTIVE)
586         // No intrs found.
587         return -1;
588     // XXX - check for errors.
589
590     // Copy data.
591     void *tddata = GET_FLATPTR(td->buffer);
592     memcpy_far(GET_SEG(SS), data
593                , FLATPTR_TO_SEG(tddata), (void*)FLATPTR_TO_OFFSET(tddata)
594                , uhci_expected_length(token));
595
596     // Reenable this td.
597     struct uhci_td *next = (void*)(GET_FLATPTR(td->link) & ~UHCI_PTR_BITS);
598     SET_FLATPTR(pipe->next_td, next);
599     barrier();
600     SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
601                              | TD_CTRL_ACTIVE));
602
603     return 0;
604 }