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