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