1 /*****************************************************************************
2 * Copyright (c) 2013 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
23 #define dprintf(_x ...) do { printf(_x); } while(0)
25 #define dprintf(_x ...)
29 static void dump_ehci_regs(struct ehci_hcd *ehcd)
31 struct ehci_cap_regs *cap_regs;
32 struct ehci_op_regs *op_regs;
34 cap_regs = ehcd->cap_regs;
35 op_regs = ehcd->op_regs;
37 dprintf("\n - CAPLENGTH %02X", read_reg8(&cap_regs->caplength));
38 dprintf("\n - HCIVERSION %04X", read_reg16(&cap_regs->hciversion));
39 dprintf("\n - HCSPARAMS %08X", read_reg32(&cap_regs->hcsparams));
40 dprintf("\n - HCCPARAMS %08X", read_reg32(&cap_regs->hccparams));
41 dprintf("\n - HCSP_PORTROUTE %016llX", read_reg64(&cap_regs->portroute));
44 dprintf("\n - USBCMD %08X", read_reg32(&op_regs->usbcmd));
45 dprintf("\n - USBSTS %08X", read_reg32(&op_regs->usbsts));
46 dprintf("\n - USBINTR %08X", read_reg32(&op_regs->usbintr));
47 dprintf("\n - FRINDEX %08X", read_reg32(&op_regs->frindex));
48 dprintf("\n - CTRLDSSEGMENT %08X", read_reg32(&op_regs->ctrldssegment));
49 dprintf("\n - PERIODICLISTBASE %08X", read_reg32(&op_regs->periodiclistbase));
50 dprintf("\n - ASYNCLISTADDR %08X", read_reg32(&op_regs->asynclistaddr));
51 dprintf("\n - CONFIGFLAG %08X", read_reg32(&op_regs->configflag));
52 dprintf("\n - PORTSC %08X", read_reg32(&op_regs->portsc[0]));
57 static int ehci_hub_check_ports(struct ehci_hcd *ehcd)
59 uint32_t num_ports, portsc, i;
62 dprintf("%s: enter\n", __func__);
63 num_ports = read_reg32(&ehcd->cap_regs->hcsparams) & HCS_NPORTS_MASK;
64 for (i = 0; i < num_ports; i++) {
65 dprintf("%s: device %d\n", __func__, i);
66 portsc = read_reg32(&ehcd->op_regs->portsc[i]);
67 if (portsc & PORT_CONNECT) { /* Device present */
68 dprintf("usb-ehci: Device present on port %d\n", i);
70 portsc = read_reg32(&ehcd->op_regs->portsc[i]);
71 portsc = (portsc & ~PORT_PE) | PORT_RESET;
72 write_reg32(&ehcd->op_regs->portsc[i], portsc);
74 portsc = read_reg32(&ehcd->op_regs->portsc[i]);
75 portsc &= ~PORT_RESET;
76 write_reg32(&ehcd->op_regs->portsc[i], portsc);
78 dev = usb_devpool_get();
79 dprintf("usb-ehci: allocated device %p\n", dev);
80 dev->hcidev = ehcd->hcidev;
81 dev->speed = USB_HIGH_SPEED; /* TODO: Check for Low/Full speed device */
82 if (usb_setup_new_device(dev, i))
83 usb_slof_populate_new_device(dev);
85 printf("usb-ehci: unable to setup device on port %d\n", i);
88 dprintf("%s: exit\n", __func__);
92 static int ehci_hcd_init(struct ehci_hcd *ehcd)
96 struct ehci_framelist *fl;
97 struct ehci_qh *qh_intr, *qh_async;
99 long fl_phys = 0, qh_intr_phys = 0, qh_async_phys;
101 /* Reset the host controller */
102 time = SLOF_GetTimer() + 250;
103 usbcmd = read_reg32(&ehcd->op_regs->usbcmd);
104 write_reg32(&ehcd->op_regs->usbcmd, (usbcmd & ~(CMD_PSE | CMD_ASE)) | CMD_HCRESET);
105 while (time > SLOF_GetTimer())
107 usbcmd = read_reg32(&ehcd->op_regs->usbcmd);
108 if (usbcmd & CMD_HCRESET) {
109 printf("usb-ehci: reset failed\n");
113 /* Initialize periodic list */
114 fl = SLOF_dma_alloc(sizeof(*fl));
116 printf("usb-ehci: Unable to allocate frame list\n");
119 fl_phys = SLOF_dma_map_in(fl, sizeof(*fl), true);
120 dprintf("fl %p, fl_phys %lx\n", fl, fl_phys);
122 /* TODO: allocate qh pool */
123 qh_intr = SLOF_dma_alloc(sizeof(*qh_intr));
125 printf("usb-ehci: Unable to allocate interrupt queue head\n");
128 qh_intr_phys = SLOF_dma_map_in(qh_intr, sizeof(*qh_intr), true);
129 dprintf("qh_intr %p, qh_intr_phys %lx\n", qh_intr, qh_intr_phys);
131 memset(qh_intr, 0, sizeof(*qh_intr));
132 qh_intr->qh_ptr = QH_PTR_TERM;
133 qh_intr->ep_cap2 = cpu_to_le32(0x01 << QH_SMASK_SHIFT);
134 qh_intr->next_qtd = qh_intr->alt_next_qtd = QH_PTR_TERM;
135 qh_intr->token = cpu_to_le32(QH_STS_HALTED);
136 for (i = 0; i < FL_SIZE; i++)
137 fl->fl_ptr[i] = cpu_to_le32(qh_intr_phys | EHCI_TYP_QH);
138 write_reg32(&ehcd->op_regs->periodiclistbase, fl_phys);
140 /* Initialize async list */
141 qh_async = SLOF_dma_alloc(sizeof(*qh_async));
143 printf("usb-ehci: Unable to allocate async queue head\n");
146 qh_async_phys = SLOF_dma_map_in(qh_async, sizeof(*qh_async), true);
147 dprintf("qh_async %p, qh_async_phys %lx\n", qh_async, qh_async_phys);
149 memset(qh_async, 0, sizeof(*qh_async));
150 qh_async->qh_ptr = cpu_to_le32(qh_async_phys | EHCI_TYP_QH);
151 qh_async->ep_cap1 = cpu_to_le32(QH_CAP_H);
152 qh_async->next_qtd = qh_async->alt_next_qtd = QH_PTR_TERM;
153 qh_async->token = cpu_to_le32(QH_STS_HALTED);
154 write_reg32(&ehcd->op_regs->asynclistaddr, qh_async_phys);
155 ehcd->qh_async = qh_async;
156 ehcd->qh_async_phys = qh_async_phys;
157 ehcd->qh_intr = qh_intr;
158 ehcd->qh_intr_phys = qh_intr_phys;
160 ehcd->fl_phys = fl_phys;
162 write_reg32(&ehcd->op_regs->usbcmd, usbcmd | CMD_ASE | CMD_RUN);
163 write_reg32(&ehcd->op_regs->configflag, 1);
168 SLOF_dma_map_out(qh_intr_phys, qh_intr, sizeof(*qh_intr));
169 SLOF_dma_free(qh_intr, sizeof(*qh_intr));
171 SLOF_dma_map_out(fl_phys, fl, sizeof(*fl));
172 SLOF_dma_free(fl, sizeof(*fl));
177 static int ehci_hcd_exit(struct ehci_hcd *ehcd)
182 dprintf("NULL pointer\n");
186 usbcmd = read_reg32(&ehcd->op_regs->usbcmd);
187 write_reg32(&ehcd->op_regs->usbcmd, usbcmd | ~CMD_RUN);
188 write_reg32(&ehcd->op_regs->periodiclistbase, 0);
191 SLOF_dma_map_out(ehcd->pool_phys, ehcd->pool, EHCI_PIPE_POOL_SIZE);
192 SLOF_dma_free(ehcd->pool, EHCI_PIPE_POOL_SIZE);
195 SLOF_dma_map_out(ehcd->qh_intr_phys, ehcd->qh_intr, sizeof(struct ehci_qh));
196 SLOF_dma_free(ehcd->qh_intr, sizeof(struct ehci_qh));
198 if (ehcd->qh_async) {
199 SLOF_dma_map_out(ehcd->qh_async_phys, ehcd->qh_async, sizeof(struct ehci_qh));
200 SLOF_dma_free(ehcd->qh_async, sizeof(struct ehci_qh));
203 SLOF_dma_map_out(ehcd->fl_phys, ehcd->fl, sizeof(struct ehci_framelist));
204 SLOF_dma_free(ehcd->fl, sizeof(struct ehci_framelist));
209 static int ehci_alloc_pipe_pool(struct ehci_hcd *ehcd)
211 struct ehci_pipe *epipe, *curr, *prev;
212 unsigned int i, count;
215 count = EHCI_PIPE_POOL_SIZE/sizeof(*epipe);
216 ehcd->pool = epipe = SLOF_dma_alloc(EHCI_PIPE_POOL_SIZE);
219 ehcd->pool_phys = epipe_phys = SLOF_dma_map_in(epipe, EHCI_PIPE_POOL_SIZE, true);
220 dprintf("%s: epipe %p, epipe_phys %lx\n", __func__, epipe, epipe_phys);
222 /* Although an array, link them */
223 for (i = 0, curr = epipe, prev = NULL; i < count; i++, curr++) {
225 prev->pipe.next = &curr->pipe;
226 curr->pipe.next = NULL;
228 curr->qh_phys = epipe_phys + (curr - epipe) * sizeof(*curr) +
229 offset_of(struct ehci_pipe, qh);
230 dprintf("%s - %d: qh %p, qh_phys %lx\n", __func__,
231 i, &curr->qh, curr->qh_phys);
235 ehcd->freelist = &epipe->pipe;
237 ehcd->end->next = &epipe->pipe;
238 ehcd->end = &prev->pipe;
243 static void ehci_init(struct usb_hcd_dev *hcidev)
245 struct ehci_hcd *ehcd;
247 printf(" EHCI: Initializing\n");
248 dprintf("%s: device base address %p\n", __func__, hcidev->base);
250 ehcd = SLOF_alloc_mem(sizeof(*ehcd));
252 printf("usb-ehci: Unable to allocate memory\n");
255 memset(ehcd, 0, sizeof(*ehcd));
257 hcidev->nextaddr = 1;
259 ehcd->hcidev = hcidev;
260 ehcd->cap_regs = (struct ehci_cap_regs *)(hcidev->base);
261 ehcd->op_regs = (struct ehci_op_regs *)(hcidev->base +
262 read_reg8(&ehcd->cap_regs->caplength));
264 dump_ehci_regs(ehcd);
267 ehci_hub_check_ports(ehcd);
270 static void ehci_exit(struct usb_hcd_dev *hcidev)
272 struct ehci_hcd *ehcd;
273 static int count = 0;
275 dprintf("%s: enter \n", __func__);
277 if (!hcidev && !hcidev->priv) {
282 printf("%s: already called once \n", __func__);
287 SLOF_free_mem(ehcd, sizeof(*ehcd));
291 static void ehci_detect(void)
296 static void ehci_disconnect(void)
301 static int ehci_handshake(struct ehci_hcd *ehcd, uint32_t timeout)
303 uint32_t usbsts = 0, time;
306 usbcmd = read_reg32(&ehcd->op_regs->usbcmd);
307 /* Ring a doorbell */
308 write_reg32(&ehcd->op_regs->usbcmd, usbcmd | CMD_IAAD);
310 time = SLOF_GetTimer() + timeout;
311 while ((time > SLOF_GetTimer())) {
312 /* Wait for controller to confirm */
313 usbsts = read_reg32(&ehcd->op_regs->usbsts);
314 if (usbsts & STS_IAA) {
315 /* Acknowledge it, for next doorbell to work */
316 write_reg32(&ehcd->op_regs->usbsts, STS_IAA);
324 static int fill_qtd_buff(struct ehci_qtd *qtd, long data, uint32_t size)
327 long pos = (data + 0x1000) & ~0xfff;
329 qtd->buffer[0] = cpu_to_le32(PTR_U32(data));
330 for (i = 1; i < 5; i++) {
331 if ((data + size - 1) >= pos) {
332 //dprintf("data spans page boundary: %d, %p\n", i, pos);
333 qtd->buffer[i] = cpu_to_le32(pos);
338 if ((data + size) > pos)
339 rem = data + size - pos;
345 static int ehci_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data)
347 struct ehci_hcd *ehcd;
348 struct ehci_qtd *qtd, *qtds, *qtds_phys;
349 struct ehci_pipe *epipe;
350 uint32_t transfer_size = sizeof(*req);
351 uint32_t datalen, pid;
353 long req_phys = 0, data_phys = 0;
356 if (pipe->type != USB_EP_TYPE_CONTROL) {
357 printf("usb-ehci: Not a control pipe.\n");
361 ehcd = pipe->dev->hcidev->priv;
362 qtds = qtd = SLOF_dma_alloc(sizeof(*qtds) * 3);
364 printf("Error allocating qTDs.\n");
367 qtds_phys = (struct ehci_qtd *)SLOF_dma_map_in(qtds, sizeof(*qtds) * 3, true);
368 memset(qtds, 0, sizeof(*qtds) * 3);
369 req_phys = SLOF_dma_map_in(req, sizeof(struct usb_dev_req), true);
370 qtd->next_qtd = cpu_to_le32(PTR_U32(&qtds_phys[1]));
371 qtd->alt_next_qtd = QH_PTR_TERM;
372 qtd->token = cpu_to_le32((transfer_size << TOKEN_TBTT_SHIFT) |
373 (3 << TOKEN_CERR_SHIFT) |
374 (PID_SETUP << TOKEN_PID_SHIFT) |
375 (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT));
376 fill_qtd_buff(qtd, req_phys, sizeof(*req));
379 datalen = cpu_to_le16(req->wLength);
380 pid = (req->bmRequestType & REQT_DIR_IN) ? PID_IN : PID_OUT;
382 data_phys = SLOF_dma_map_in(data, datalen, true);
383 qtd->next_qtd = cpu_to_le32(PTR_U32(&qtds_phys[2]));
384 qtd->alt_next_qtd = QH_PTR_TERM;
385 qtd->token = cpu_to_le32((1 << TOKEN_DT_SHIFT) |
386 (datalen << TOKEN_TBTT_SHIFT) |
387 (3 << TOKEN_CERR_SHIFT) |
388 (pid << TOKEN_PID_SHIFT) |
389 (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT));
390 fill_qtd_buff(qtd, data_phys, datalen);
398 qtd->next_qtd = QH_PTR_TERM;
399 qtd->alt_next_qtd = QH_PTR_TERM;
400 qtd->token = cpu_to_le32((1 << TOKEN_DT_SHIFT) |
401 (3 << TOKEN_CERR_SHIFT) |
402 (pid << TOKEN_PID_SHIFT) |
403 (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT));
405 /* link qtd to qh and attach to ehcd */
407 epipe = container_of(pipe, struct ehci_pipe, pipe);
408 epipe->qh.next_qtd = cpu_to_le32(PTR_U32(qtds_phys));
409 epipe->qh.qh_ptr = cpu_to_le32(ehcd->qh_async_phys | EHCI_TYP_QH);
410 epipe->qh.ep_cap1 = cpu_to_le32((pipe->mps << QH_MPS_SHIFT) |
411 (pipe->speed << QH_EPS_SHIFT) |
412 (pipe->epno << QH_EP_SHIFT) |
413 (pipe->dev->addr << QH_DEV_ADDR_SHIFT));
416 ehcd->qh_async->qh_ptr = cpu_to_le32(epipe->qh_phys | EHCI_TYP_QH);
421 time = SLOF_GetTimer() + USB_TIMEOUT;
423 if (le32_to_cpu(qtd->token) & (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT))
428 if (time < SLOF_GetTimer()) { /* timed out */
429 printf("usb-ehci: control transfer timed out_\n");
433 } while (qtd->next_qtd != QH_PTR_TERM);
435 ehcd->qh_async->qh_ptr = cpu_to_le32(ehcd->qh_async_phys | EHCI_TYP_QH);
437 if (!ehci_handshake(ehcd, USB_TIMEOUT)) {
438 printf("%s: handshake failed\n", __func__);
442 SLOF_dma_map_out(req_phys, req, sizeof(struct usb_dev_req));
443 SLOF_dma_map_out(data_phys, data, datalen);
444 SLOF_dma_map_out(PTR_U32(qtds_phys), qtds, sizeof(*qtds) * 3);
445 SLOF_dma_free(qtds, sizeof(*qtds) * 3);
450 static int ehci_transfer_bulk(struct usb_pipe *pipe, void *td, void *td_phys,
451 void *data_phys, int size)
453 struct ehci_hcd *ehcd;
454 struct ehci_qtd *qtd, *qtd_phys;
455 struct ehci_pipe *epipe;
457 int i, rem, ret = true;
461 dprintf("usb-ehci: bulk transfer: data %p, size %d, td %p, td_phys %p\n",
462 data_phys, size, td, td_phys);
464 if (pipe->type != USB_EP_TYPE_BULK) {
465 printf("usb-ehci: Not a bulk pipe.\n");
469 if (size > QTD_MAX_TRANSFER_LEN) {
470 printf("usb-ehci: bulk transfer size too big\n");
474 ehcd = pipe->dev->hcidev->priv;
475 pid = (pipe->dir == USB_PIPE_OUT) ? PID_OUT : PID_IN;
476 qtd = (struct ehci_qtd *)td;
477 qtd_phys = (struct ehci_qtd *)td_phys;
478 ptr = (long)data_phys;
479 for (i = 0; i < NUM_BULK_QTDS; i++) {
480 memset(qtd, 0, sizeof(*qtd));
481 rem = fill_qtd_buff(qtd, ptr, size);
482 qtd->token = cpu_to_le32((1 << TOKEN_DT_SHIFT) |
483 ((size - rem) << TOKEN_TBTT_SHIFT) |
484 (3 << TOKEN_CERR_SHIFT) |
485 (pid << TOKEN_PID_SHIFT) |
486 (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT));
488 qtd->next_qtd = cpu_to_le32(PTR_U32(&qtd_phys[i+1]));
489 qtd->alt_next_qtd = QH_PTR_TERM;
494 qtd->next_qtd = qtd->alt_next_qtd = QH_PTR_TERM;
495 break; /* no more data */
499 /* link qtd to qh and attach to ehcd */
501 epipe = container_of(pipe, struct ehci_pipe, pipe);
502 epipe->qh.next_qtd = cpu_to_le32(PTR_U32(qtd_phys));
503 epipe->qh.qh_ptr = cpu_to_le32(ehcd->qh_async_phys | EHCI_TYP_QH);
504 epipe->qh.ep_cap1 = cpu_to_le32((pipe->mps << QH_MPS_SHIFT) |
505 (pipe->speed << QH_EPS_SHIFT) |
506 (pipe->epno << QH_EP_SHIFT) |
507 (pipe->dev->addr << QH_DEV_ADDR_SHIFT));
510 ehcd->qh_async->qh_ptr = cpu_to_le32(epipe->qh_phys | EHCI_TYP_QH);
514 qtd = (struct ehci_qtd *)td;
515 for (i = 0; i < NUM_BULK_QTDS; i++) {
516 time = SLOF_GetTimer() + USB_TIMEOUT;
517 while ((time > SLOF_GetTimer()) &&
518 (le32_to_cpu(qtd->token) & (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT)))
521 if (qtd->next_qtd == QH_PTR_TERM)
524 if (le32_to_cpu(qtd->token) & (QH_STS_ACTIVE << TOKEN_STATUS_SHIFT)) {
525 printf("usb-ehci: bulk transfer timed out_\n");
532 ehcd->qh_async->qh_ptr = cpu_to_le32(ehcd->qh_async_phys | EHCI_TYP_QH);
534 if (!ehci_handshake(ehcd, USB_TIMEOUT)) {
535 printf("%s: handshake failed\n", __func__);
541 static struct usb_pipe *ehci_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep,
542 char *buf, size_t len)
544 struct ehci_hcd *ehcd;
545 struct usb_pipe *new = NULL;
550 ehcd = (struct ehci_hcd *)dev->hcidev->priv;
551 if (!ehcd->freelist) {
552 dprintf("usb-ehci: %s allocating pool\n", __func__);
553 if (ehci_alloc_pipe_pool(ehcd))
557 new = ehcd->freelist;
558 ehcd->freelist = ehcd->freelist->next;
562 memset(new, 0, sizeof(*new));
565 new->type = ep->bmAttributes & USB_EP_TYPE_MASK;
566 new->speed = dev->speed;
567 new->mps = ep->wMaxPacketSize;
568 new->dir = (ep->bEndpointAddress & 0x80) >> 7;
569 new->epno = ep->bEndpointAddress & 0x0f;
574 static void ehci_put_pipe(struct usb_pipe *pipe)
576 struct ehci_hcd *ehcd;
578 dprintf("usb-ehci: %s enter - %p\n", __func__, pipe);
579 if (!pipe || !pipe->dev)
581 ehcd = pipe->dev->hcidev->priv;
583 ehcd->end->next = pipe;
585 ehcd->freelist = pipe;
590 memset(pipe, 0, sizeof(*pipe));
591 dprintf("usb-ehci: %s exit\n", __func__);
594 struct usb_hcd_ops ehci_ops = {
598 .detect = ehci_detect,
599 .disconnect = ehci_disconnect,
600 .get_pipe = ehci_get_pipe,
601 .put_pipe = ehci_put_pipe,
602 .send_ctrl = ehci_send_ctrl,
603 .transfer_bulk = ehci_transfer_bulk,
604 .usb_type = USB_EHCI,
608 void usb_ehci_register(void)
610 usb_hcd_register(&ehci_ops);