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