Minor fix - make sure not to call ohci/uhci_init from irq handler.
[seabios.git] / src / usb-ehci.c
1 // Code for handling EHCI USB controllers.
2 //
3 // Copyright (C) 2010  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-ehci.h" // struct ehci_qh
12 #include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
13 #include "pci_regs.h" // PCI_BASE_ADDRESS_0
14 #include "usb.h" // struct usb_s
15 #include "farptr.h" // GET_FLATPTR
16 #include "usb-uhci.h" // init_uhci
17 #include "usb-ohci.h" // init_ohci
18
19 struct usb_ehci_s {
20     struct usb_s usb;
21     struct ehci_caps *caps;
22     struct ehci_regs *regs;
23     struct ehci_qh *async_qh;
24     struct pci_device *companion[8];
25     int checkports;
26     int legacycount;
27 };
28
29
30 /****************************************************************
31  * Root hub
32  ****************************************************************/
33
34 #define EHCI_TIME_POSTPOWER 20
35 #define EHCI_TIME_POSTRESET 2
36
37 // Check if need companion controllers for full/low speed devices
38 static void
39 ehci_note_port(struct usb_ehci_s *cntl)
40 {
41     if (--cntl->checkports)
42         // Ports still being detected.
43         return;
44     if (! cntl->legacycount)
45         // No full/low speed devices found.
46         return;
47     // Start companion controllers.
48     int i;
49     for (i=0; i<ARRAY_SIZE(cntl->companion); i++) {
50         struct pci_device *pci = cntl->companion[i];
51         if (!pci)
52             break;
53
54         // ohci/uhci_init call pci_config_XXX - don't run from irq handler.
55         wait_preempt();
56
57         if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
58             uhci_init(pci, cntl->usb.busid + i);
59         else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
60             ohci_init(pci, cntl->usb.busid + i);
61     }
62 }
63
64 // Check if device attached to port
65 static int
66 ehci_hub_detect(struct usbhub_s *hub, u32 port)
67 {
68     struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
69     u32 *portreg = &cntl->regs->portsc[port];
70     u32 portsc = readl(portreg);
71
72     // Power up port.
73     if (!(portsc & PORT_POWER)) {
74         portsc |= PORT_POWER;
75         writel(portreg, portsc);
76         msleep(EHCI_TIME_POSTPOWER);
77     } else {
78         msleep(1); // XXX - time for connect to be detected.
79     }
80     portsc = readl(portreg);
81
82     if (!(portsc & PORT_CONNECT))
83         // No device present
84         goto doneearly;
85
86     if ((portsc & PORT_LINESTATUS_MASK) == PORT_LINESTATUS_KSTATE) {
87         // low speed device
88         cntl->legacycount++;
89         writel(portreg, portsc | PORT_OWNER);
90         goto doneearly;
91     }
92
93     // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
94
95     // Begin reset on port
96     portsc = (portsc & ~PORT_PE) | PORT_RESET;
97     writel(portreg, portsc);
98     msleep(USB_TIME_DRSTR);
99     return 0;
100
101 doneearly:
102     ehci_note_port(cntl);
103     return -1;
104 }
105
106 // Reset device on port
107 static int
108 ehci_hub_reset(struct usbhub_s *hub, u32 port)
109 {
110     struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
111     u32 *portreg = &cntl->regs->portsc[port];
112     u32 portsc = readl(portreg);
113
114     // Finish reset on port
115     portsc &= ~PORT_RESET;
116     writel(portreg, portsc);
117     msleep(EHCI_TIME_POSTRESET);
118
119     int rv = -1;
120     portsc = readl(portreg);
121     if (!(portsc & PORT_CONNECT))
122         // No longer connected
123         goto resetfail;
124     if (!(portsc & PORT_PE)) {
125         // full speed device
126         cntl->legacycount++;
127         writel(portreg, portsc | PORT_OWNER);
128         goto resetfail;
129     }
130
131     rv = USB_HIGHSPEED;
132 resetfail:
133     ehci_note_port(cntl);
134     return rv;
135 }
136
137 // Disable port
138 static void
139 ehci_hub_disconnect(struct usbhub_s *hub, u32 port)
140 {
141     struct usb_ehci_s *cntl = container_of(hub->cntl, struct usb_ehci_s, usb);
142     u32 *portreg = &cntl->regs->portsc[port];
143     u32 portsc = readl(portreg);
144     writel(portreg, portsc & ~PORT_PE);
145 }
146
147 static struct usbhub_op_s ehci_HubOp = {
148     .detect = ehci_hub_detect,
149     .reset = ehci_hub_reset,
150     .disconnect = ehci_hub_disconnect,
151 };
152
153 // Find any devices connected to the root hub.
154 static int
155 check_ehci_ports(struct usb_ehci_s *cntl)
156 {
157     ASSERT32FLAT();
158     struct usbhub_s hub;
159     memset(&hub, 0, sizeof(hub));
160     hub.cntl = &cntl->usb;
161     hub.portcount = cntl->checkports;
162     hub.op = &ehci_HubOp;
163     usb_enumerate(&hub);
164     return hub.devcount;
165 }
166
167
168 /****************************************************************
169  * Setup
170  ****************************************************************/
171
172 static void
173 configure_ehci(void *data)
174 {
175     struct usb_ehci_s *cntl = data;
176
177     // Allocate ram for schedule storage
178     struct ehci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
179     struct ehci_qh *intr_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*intr_qh));
180     struct ehci_qh *async_qh = memalign_high(EHCI_QH_ALIGN, sizeof(*async_qh));
181     if (!fl || !intr_qh || !async_qh) {
182         warn_noalloc();
183         goto fail;
184     }
185
186     // XXX - check for halted?
187
188     // Reset the HC
189     u32 cmd = readl(&cntl->regs->usbcmd);
190     writel(&cntl->regs->usbcmd, (cmd & ~(CMD_ASE | CMD_PSE)) | CMD_HCRESET);
191     u64 end = calc_future_tsc(250);
192     for (;;) {
193         cmd = readl(&cntl->regs->usbcmd);
194         if (!(cmd & CMD_HCRESET))
195             break;
196         if (check_tsc(end)) {
197             warn_timeout();
198             goto fail;
199         }
200         yield();
201     }
202
203     // Disable interrupts (just to be safe).
204     writel(&cntl->regs->usbintr, 0);
205
206     // Set schedule to point to primary intr queue head
207     memset(intr_qh, 0, sizeof(*intr_qh));
208     intr_qh->next = EHCI_PTR_TERM;
209     intr_qh->info2 = (0x01 << QH_SMASK_SHIFT);
210     intr_qh->token = QTD_STS_HALT;
211     intr_qh->qtd_next = intr_qh->alt_next = EHCI_PTR_TERM;
212     int i;
213     for (i=0; i<ARRAY_SIZE(fl->links); i++)
214         fl->links[i] = (u32)intr_qh | EHCI_PTR_QH;
215     writel(&cntl->regs->periodiclistbase, (u32)fl);
216
217     // Set async list to point to primary async queue head
218     memset(async_qh, 0, sizeof(*async_qh));
219     async_qh->next = (u32)async_qh | EHCI_PTR_QH;
220     async_qh->info1 = QH_HEAD;
221     async_qh->token = QTD_STS_HALT;
222     async_qh->qtd_next = async_qh->alt_next = EHCI_PTR_TERM;
223     cntl->async_qh = async_qh;
224     writel(&cntl->regs->asynclistbase, (u32)async_qh);
225
226     // Enable queues
227     writel(&cntl->regs->usbcmd, cmd | CMD_ASE | CMD_PSE | CMD_RUN);
228
229     // Set default of high speed for root hub.
230     writel(&cntl->regs->configflag, 1);
231     cntl->checkports = readl(&cntl->caps->hcsparams) & HCS_N_PORTS_MASK;
232
233     // Find devices
234     int count = check_ehci_ports(cntl);
235     free_pipe(cntl->usb.defaultpipe);
236     if (count)
237         // Success
238         return;
239
240     // No devices found - shutdown and free controller.
241     writel(&cntl->regs->usbcmd, cmd & ~CMD_RUN);
242     msleep(4);  // 2ms to stop reading memory - XXX
243 fail:
244     free(fl);
245     free(intr_qh);
246     free(async_qh);
247     free(cntl);
248 }
249
250 int
251 ehci_init(struct pci_device *pci, int busid, struct pci_device *comppci)
252 {
253     if (! CONFIG_USB_EHCI)
254         return -1;
255
256     u16 bdf = pci->bdf;
257     u32 baseaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0);
258     struct ehci_caps *caps = (void*)(baseaddr & PCI_BASE_ADDRESS_MEM_MASK);
259     u32 hcc_params = readl(&caps->hccparams);
260     if (hcc_params & HCC_64BIT_ADDR) {
261         dprintf(1, "No support for 64bit EHCI\n");
262         return -1;
263     }
264
265     struct usb_ehci_s *cntl = malloc_tmphigh(sizeof(*cntl));
266     memset(cntl, 0, sizeof(*cntl));
267     cntl->usb.busid = busid;
268     cntl->usb.pci = pci;
269     cntl->usb.type = USB_TYPE_EHCI;
270     cntl->caps = caps;
271     cntl->regs = (void*)caps + readb(&caps->caplength);
272
273     dprintf(1, "EHCI init on dev %02x:%02x.%x (regs=%p)\n"
274             , pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)
275             , pci_bdf_to_fn(bdf), cntl->regs);
276
277     pci_config_maskw(bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
278
279     // XXX - check for and disable SMM control?
280
281     // Find companion controllers.
282     int count = 0;
283     for (;;) {
284         if (!comppci || comppci == pci)
285             break;
286         if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_UHCI)
287             cntl->companion[count++] = comppci;
288         else if (pci_classprog(comppci) == PCI_CLASS_SERIAL_USB_OHCI)
289             cntl->companion[count++] = comppci;
290         comppci = comppci->next;
291     }
292
293     run_thread(configure_ehci, cntl);
294     return 0;
295 }
296
297
298 /****************************************************************
299  * End point communication
300  ****************************************************************/
301
302 static int
303 ehci_wait_qh(struct usb_ehci_s *cntl, struct ehci_qh *qh)
304 {
305     // XXX - 500ms just a guess
306     u64 end = calc_future_tsc(500);
307     for (;;) {
308         if (qh->qtd_next & EHCI_PTR_TERM)
309             // XXX - confirm
310             return 0;
311         if (check_tsc(end)) {
312             warn_timeout();
313             return -1;
314         }
315         yield();
316     }
317 }
318
319 // Wait for next USB async frame to start - for ensuring safe memory release.
320 static void
321 ehci_waittick(struct usb_ehci_s *cntl)
322 {
323     if (MODE16) {
324         msleep(10);
325         return;
326     }
327     // Wait for access to "doorbell"
328     barrier();
329     u32 cmd, sts;
330     u64 end = calc_future_tsc(100);
331     for (;;) {
332         sts = readl(&cntl->regs->usbsts);
333         if (!(sts & STS_IAA)) {
334             cmd = readl(&cntl->regs->usbcmd);
335             if (!(cmd & CMD_IAAD))
336                 break;
337         }
338         if (check_tsc(end)) {
339             warn_timeout();
340             return;
341         }
342         yield();
343     }
344     // Ring "doorbell"
345     writel(&cntl->regs->usbcmd, cmd | CMD_IAAD);
346     // Wait for completion
347     for (;;) {
348         sts = readl(&cntl->regs->usbsts);
349         if (sts & STS_IAA)
350             break;
351         if (check_tsc(end)) {
352             warn_timeout();
353             return;
354         }
355         yield();
356     }
357     // Ack completion
358     writel(&cntl->regs->usbsts, STS_IAA);
359 }
360
361 struct ehci_pipe {
362     struct ehci_qh qh;
363     struct ehci_qtd *next_td, *tds;
364     void *data;
365     struct usb_pipe pipe;
366 };
367
368 void
369 ehci_free_pipe(struct usb_pipe *p)
370 {
371     if (! CONFIG_USB_EHCI)
372         return;
373     dprintf(7, "ehci_free_pipe %p\n", p);
374     struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
375     struct usb_ehci_s *cntl = container_of(
376         pipe->pipe.cntl, struct usb_ehci_s, usb);
377
378     struct ehci_qh *start = cntl->async_qh;
379     struct ehci_qh *pos = start;
380     for (;;) {
381         struct ehci_qh *next = (void*)(pos->next & ~EHCI_PTR_BITS);
382         if (next == start) {
383             // Not found?!  Exit without freeing.
384             warn_internalerror();
385             return;
386         }
387         if (next == &pipe->qh) {
388             pos->next = next->next;
389             ehci_waittick(cntl);
390             free(pipe);
391             return;
392         }
393         pos = next;
394     }
395 }
396
397 struct usb_pipe *
398 ehci_alloc_control_pipe(struct usb_pipe *dummy)
399 {
400     if (! CONFIG_USB_EHCI)
401         return NULL;
402     struct usb_ehci_s *cntl = container_of(
403         dummy->cntl, struct usb_ehci_s, usb);
404     dprintf(7, "ehci_alloc_control_pipe %p\n", &cntl->usb);
405
406     // Allocate a queue head.
407     struct ehci_pipe *pipe = memalign_tmphigh(EHCI_QH_ALIGN, sizeof(*pipe));
408     if (!pipe) {
409         warn_noalloc();
410         return NULL;
411     }
412     memset(pipe, 0, sizeof(*pipe));
413     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
414     pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
415     pipe->qh.token = QTD_STS_HALT;
416
417     // Add queue head to controller list.
418     struct ehci_qh *async_qh = cntl->async_qh;
419     pipe->qh.next = async_qh->next;
420     barrier();
421     async_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
422     return &pipe->pipe;
423 }
424
425 static int
426 fillTDbuffer(struct ehci_qtd *td, u16 maxpacket, const void *buf, int bytes)
427 {
428     u32 dest = (u32)buf;
429     u32 *pos = td->buf;
430     while (bytes) {
431         if (pos >= &td->buf[ARRAY_SIZE(td->buf)])
432             // More data than can transfer in a single qtd - only use
433             // full packets to prevent a babble error.
434             return ALIGN_DOWN(dest - (u32)buf, maxpacket);
435         u32 count = bytes;
436         u32 max = 0x1000 - (dest & 0xfff);
437         if (count > max)
438             count = max;
439         *pos = dest;
440         bytes -= count;
441         dest += count;
442         pos++;
443     }
444     return dest - (u32)buf;
445 }
446
447 int
448 ehci_control(struct usb_pipe *p, int dir, const void *cmd, int cmdsize
449              , void *data, int datasize)
450 {
451     ASSERT32FLAT();
452     if (! CONFIG_USB_EHCI)
453         return -1;
454     dprintf(5, "ehci_control %p\n", p);
455     if (datasize > 4*4096 || cmdsize > 4*4096) {
456         // XXX - should support larger sizes.
457         warn_noalloc();
458         return -1;
459     }
460     struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
461     struct usb_ehci_s *cntl = container_of(
462         pipe->pipe.cntl, struct usb_ehci_s, usb);
463
464     u16 maxpacket = pipe->pipe.maxpacket;
465     int speed = pipe->pipe.speed;
466
467     // Setup fields in qh
468     pipe->qh.info1 = (
469         (1 << QH_MULT_SHIFT) | (speed != USB_HIGHSPEED ? QH_CONTROL : 0)
470         | (maxpacket << QH_MAXPACKET_SHIFT)
471         | QH_TOGGLECONTROL
472         | (speed << QH_SPEED_SHIFT)
473         | (pipe->pipe.ep << QH_EP_SHIFT)
474         | (pipe->pipe.devaddr << QH_DEVADDR_SHIFT));
475     pipe->qh.info2 = ((1 << QH_MULT_SHIFT)
476                       | (pipe->pipe.tt_port << QH_HUBPORT_SHIFT)
477                       | (pipe->pipe.tt_devaddr << QH_HUBADDR_SHIFT));
478
479     // Setup transfer descriptors
480     struct ehci_qtd *tds = memalign_tmphigh(EHCI_QTD_ALIGN, sizeof(*tds) * 3);
481     if (!tds) {
482         warn_noalloc();
483         return -1;
484     }
485     memset(tds, 0, sizeof(*tds) * 3);
486     struct ehci_qtd *td = tds;
487
488     td->qtd_next = (u32)&td[1];
489     td->alt_next = EHCI_PTR_TERM;
490     td->token = (ehci_explen(cmdsize) | QTD_STS_ACTIVE
491                  | QTD_PID_SETUP | ehci_maxerr(3));
492     fillTDbuffer(td, maxpacket, cmd, cmdsize);
493     td++;
494
495     if (datasize) {
496         td->qtd_next = (u32)&td[1];
497         td->alt_next = EHCI_PTR_TERM;
498         td->token = (QTD_TOGGLE | ehci_explen(datasize) | QTD_STS_ACTIVE
499                      | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
500         fillTDbuffer(td, maxpacket, data, datasize);
501         td++;
502     }
503
504     td->qtd_next = EHCI_PTR_TERM;
505     td->alt_next = EHCI_PTR_TERM;
506     td->token = (QTD_TOGGLE | QTD_STS_ACTIVE
507                  | (dir ? QTD_PID_OUT : QTD_PID_IN) | ehci_maxerr(3));
508
509     // Transfer data
510     barrier();
511     pipe->qh.qtd_next = (u32)tds;
512     barrier();
513     pipe->qh.token = 0;
514     int ret = ehci_wait_qh(cntl, &pipe->qh);
515     pipe->qh.token = QTD_STS_HALT;
516     if (ret) {
517         pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
518         // XXX - halt qh?
519         ehci_waittick(cntl);
520     }
521     free(tds);
522     return ret;
523 }
524
525 struct usb_pipe *
526 ehci_alloc_bulk_pipe(struct usb_pipe *dummy)
527 {
528     // XXX - this func is same as alloc_control except for malloc_low
529     if (! CONFIG_USB_EHCI)
530         return NULL;
531     struct usb_ehci_s *cntl = container_of(
532         dummy->cntl, struct usb_ehci_s, usb);
533     dprintf(7, "ehci_alloc_bulk_pipe %p\n", &cntl->usb);
534
535     // Allocate a queue head.
536     struct ehci_pipe *pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
537     if (!pipe) {
538         warn_noalloc();
539         return NULL;
540     }
541     memset(pipe, 0, sizeof(*pipe));
542     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
543     pipe->qh.qtd_next = pipe->qh.alt_next = EHCI_PTR_TERM;
544     pipe->qh.token = QTD_STS_HALT;
545
546     // Add queue head to controller list.
547     struct ehci_qh *async_qh = cntl->async_qh;
548     pipe->qh.next = async_qh->next;
549     barrier();
550     async_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
551     return &pipe->pipe;
552 }
553
554 static int
555 ehci_wait_td(struct ehci_qtd *td)
556 {
557     u64 end = calc_future_tsc(5000); // XXX - lookup real time.
558     u32 status;
559     for (;;) {
560         status = td->token;
561         if (!(status & QTD_STS_ACTIVE))
562             break;
563         if (check_tsc(end)) {
564             warn_timeout();
565             return -1;
566         }
567         yield();
568     }
569     if (status & QTD_STS_HALT) {
570         dprintf(1, "ehci_wait_td error - status=%x\n", status);
571         return -2;
572     }
573     return 0;
574 }
575
576 #define STACKQTDS 4
577
578 int
579 ehci_send_bulk(struct usb_pipe *p, int dir, void *data, int datasize)
580 {
581     if (! CONFIG_USB_EHCI)
582         return -1;
583     struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
584     dprintf(7, "ehci_send_bulk qh=%p dir=%d data=%p size=%d\n"
585             , &pipe->qh, dir, data, datasize);
586
587     // Allocate 4 tds on stack (16byte aligned)
588     u8 tdsbuf[sizeof(struct ehci_qtd) * STACKQTDS + EHCI_QTD_ALIGN - 1];
589     struct ehci_qtd *tds = (void*)ALIGN((u32)tdsbuf, EHCI_QTD_ALIGN);
590     memset(tds, 0, sizeof(*tds) * STACKQTDS);
591
592     // Setup fields in qh
593     u16 maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
594     SET_FLATPTR(pipe->qh.info1
595                 , ((1 << QH_MULT_SHIFT)
596                    | (maxpacket << QH_MAXPACKET_SHIFT)
597                    | (GET_FLATPTR(pipe->pipe.speed) << QH_SPEED_SHIFT)
598                    | (GET_FLATPTR(pipe->pipe.ep) << QH_EP_SHIFT)
599                    | (GET_FLATPTR(pipe->pipe.devaddr) << QH_DEVADDR_SHIFT)));
600     SET_FLATPTR(pipe->qh.info2
601                 , ((1 << QH_MULT_SHIFT)
602                    | (GET_FLATPTR(pipe->pipe.tt_port) << QH_HUBPORT_SHIFT)
603                    | (GET_FLATPTR(pipe->pipe.tt_devaddr) << QH_HUBADDR_SHIFT)));
604     barrier();
605     SET_FLATPTR(pipe->qh.qtd_next, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
606     barrier();
607     SET_FLATPTR(pipe->qh.token, GET_FLATPTR(pipe->qh.token) & QTD_TOGGLE);
608
609     int tdpos = 0;
610     while (datasize) {
611         struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
612         int ret = ehci_wait_td(td);
613         if (ret)
614             goto fail;
615
616         struct ehci_qtd *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
617                                                  , &tds[tdpos % STACKQTDS]);
618
619         int transfer = fillTDbuffer(td, maxpacket, data, datasize);
620         td->qtd_next = (transfer==datasize ? EHCI_PTR_TERM : (u32)nexttd_fl);
621         td->alt_next = EHCI_PTR_TERM;
622         barrier();
623         td->token = (ehci_explen(transfer) | QTD_STS_ACTIVE
624                      | (dir ? QTD_PID_IN : QTD_PID_OUT) | ehci_maxerr(3));
625
626         data += transfer;
627         datasize -= transfer;
628     }
629     int i;
630     for (i=0; i<STACKQTDS; i++) {
631         struct ehci_qtd *td = &tds[tdpos++ % STACKQTDS];
632         int ret = ehci_wait_td(td);
633         if (ret)
634             goto fail;
635     }
636
637     return 0;
638 fail:
639     dprintf(1, "ehci_send_bulk failed\n");
640     SET_FLATPTR(pipe->qh.qtd_next, EHCI_PTR_TERM);
641     SET_FLATPTR(pipe->qh.alt_next, EHCI_PTR_TERM);
642     // XXX - halt qh?
643     struct usb_ehci_s *cntl = container_of(
644         GET_FLATPTR(pipe->pipe.cntl), struct usb_ehci_s, usb);
645     ehci_waittick(cntl);
646     return -1;
647 }
648
649 struct usb_pipe *
650 ehci_alloc_intr_pipe(struct usb_pipe *dummy, int frameexp)
651 {
652     if (! CONFIG_USB_EHCI)
653         return NULL;
654     struct usb_ehci_s *cntl = container_of(
655         dummy->cntl, struct usb_ehci_s, usb);
656     dprintf(7, "ehci_alloc_intr_pipe %p %d\n", &cntl->usb, frameexp);
657
658     if (frameexp > 10)
659         frameexp = 10;
660     int maxpacket = dummy->maxpacket;
661     // Determine number of entries needed for 2 timer ticks.
662     int ms = 1<<frameexp;
663     int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
664     struct ehci_pipe *pipe = memalign_low(EHCI_QH_ALIGN, sizeof(*pipe));
665     struct ehci_qtd *tds = memalign_low(EHCI_QTD_ALIGN, sizeof(*tds) * count);
666     void *data = malloc_low(maxpacket * count);
667     if (!pipe || !tds || !data) {
668         warn_noalloc();
669         goto fail;
670     }
671     memset(pipe, 0, sizeof(*pipe));
672     memcpy(&pipe->pipe, dummy, sizeof(pipe->pipe));
673     pipe->next_td = pipe->tds = tds;
674     pipe->data = data;
675
676     pipe->qh.info1 = (
677         (1 << QH_MULT_SHIFT)
678         | (maxpacket << QH_MAXPACKET_SHIFT)
679         | (pipe->pipe.speed << QH_SPEED_SHIFT)
680         | (pipe->pipe.ep << QH_EP_SHIFT)
681         | (pipe->pipe.devaddr << QH_DEVADDR_SHIFT));
682     pipe->qh.info2 = ((1 << QH_MULT_SHIFT)
683                       | (pipe->pipe.tt_port << QH_HUBPORT_SHIFT)
684                       | (pipe->pipe.tt_devaddr << QH_HUBADDR_SHIFT)
685                       | (0x01 << QH_SMASK_SHIFT)
686                       | (0x1c << QH_CMASK_SHIFT));
687     pipe->qh.qtd_next = (u32)tds;
688
689     int i;
690     for (i=0; i<count; i++) {
691         struct ehci_qtd *td = &tds[i];
692         td->qtd_next = (i==count-1 ? (u32)tds : (u32)&td[1]);
693         td->alt_next = EHCI_PTR_TERM;
694         td->token = (ehci_explen(maxpacket) | QTD_STS_ACTIVE
695                      | QTD_PID_IN | ehci_maxerr(3));
696         td->buf[0] = (u32)data + maxpacket * i;
697     }
698
699     // Add to interrupt schedule.
700     struct ehci_framelist *fl = (void*)readl(&cntl->regs->periodiclistbase);
701     if (frameexp == 0) {
702         // Add to existing interrupt entry.
703         struct ehci_qh *intr_qh = (void*)(fl->links[0] & ~EHCI_PTR_BITS);
704         pipe->qh.next = intr_qh->next;
705         barrier();
706         intr_qh->next = (u32)&pipe->qh | EHCI_PTR_QH;
707     } else {
708         int startpos = 1<<(frameexp-1);
709         pipe->qh.next = fl->links[startpos];
710         barrier();
711         for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
712             fl->links[i] = (u32)&pipe->qh | EHCI_PTR_QH;
713     }
714
715     return &pipe->pipe;
716 fail:
717     free(pipe);
718     free(tds);
719     free(data);
720     return NULL;
721 }
722
723 int
724 ehci_poll_intr(struct usb_pipe *p, void *data)
725 {
726     ASSERT16();
727     if (! CONFIG_USB_EHCI)
728         return -1;
729     struct ehci_pipe *pipe = container_of(p, struct ehci_pipe, pipe);
730     struct ehci_qtd *td = GET_FLATPTR(pipe->next_td);
731     u32 token = GET_FLATPTR(td->token);
732     if (token & QTD_STS_ACTIVE)
733         // No intrs found.
734         return -1;
735     // XXX - check for errors.
736
737     // Copy data.
738     int maxpacket = GET_FLATPTR(pipe->pipe.maxpacket);
739     int pos = td - GET_FLATPTR(pipe->tds);
740     void *tddata = GET_FLATPTR(pipe->data) + maxpacket * pos;
741     memcpy_far(GET_SEG(SS), data
742                , FLATPTR_TO_SEG(tddata), (void*)FLATPTR_TO_OFFSET(tddata)
743                , maxpacket);
744
745     // Reenable this td.
746     struct ehci_qtd *next = (void*)(GET_FLATPTR(td->qtd_next) & ~EHCI_PTR_BITS);
747     SET_FLATPTR(pipe->next_td, next);
748     SET_FLATPTR(td->buf[0], (u32)tddata);
749     barrier();
750     SET_FLATPTR(td->token, (ehci_explen(maxpacket) | QTD_STS_ACTIVE
751                             | QTD_PID_IN | ehci_maxerr(3)));
752
753     return 0;
754 }