2 * Copyright (c) 2007-2008, Juniper Networks, Inc.
3 * Copyright (c) 2008, Excito Elektronik i Skåne AB
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2 of
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 #include <asm/byteorder.h>
30 struct ehci_hccr *hccr; /* R/O registers, not need for volatile */
31 volatile struct ehci_hcor *hcor;
33 static uint16_t portreset;
34 static struct QH qh_list __attribute__((aligned(32)));
36 static struct descriptor {
37 struct usb_hub_descriptor hub;
38 struct usb_device_descriptor device;
39 struct usb_linux_config_descriptor config;
40 struct usb_linux_interface_descriptor interface;
41 struct usb_endpoint_descriptor endpoint;
42 } __attribute__ ((packed)) descriptor = {
44 0x8, /* bDescLength */
45 0x29, /* bDescriptorType: hub descriptor */
46 2, /* bNrPorts -- runtime modified */
47 0, /* wHubCharacteristics */
48 0xff, /* bPwrOn2PwrGood */
49 0, /* bHubCntrCurrent */
50 {}, /* Device removable */
51 {} /* at most 7 ports! XXX */
55 1, /* bDescriptorType: UDESC_DEVICE */
56 0x0002, /* bcdUSB: v2.0 */
57 9, /* bDeviceClass: UDCLASS_HUB */
58 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
59 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
60 64, /* bMaxPacketSize: 64 bytes */
61 0x0000, /* idVendor */
62 0x0000, /* idProduct */
63 0x0001, /* bcdDevice */
64 1, /* iManufacturer */
66 0, /* iSerialNumber */
67 1 /* bNumConfigurations: 1 */
71 2, /* bDescriptorType: UDESC_CONFIG */
73 1, /* bNumInterface */
74 1, /* bConfigurationValue */
75 0, /* iConfiguration */
76 0x40, /* bmAttributes: UC_SELF_POWER */
81 4, /* bDescriptorType: UDESC_INTERFACE */
82 0, /* bInterfaceNumber */
83 0, /* bAlternateSetting */
84 1, /* bNumEndpoints */
85 9, /* bInterfaceClass: UICLASS_HUB */
86 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
87 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
92 5, /* bDescriptorType: UDESC_ENDPOINT */
93 0x81, /* bEndpointAddress:
94 * UE_DIR_IN | EHCI_INTR_ENDPT
96 3, /* bmAttributes: UE_INTERRUPT */
97 8, 0, /* wMaxPacketSize */
102 static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int msec)
106 result = ehci_readl(ptr);
107 debug("handshake read reg(%x)=%x\n", (uint32_t)ptr, result);
108 if (result == ~(uint32_t)0)
119 static void ehci_free(void *p, size_t sz)
124 static int ehci_reset(void)
131 cmd = ehci_readl(&hcor->or_usbcmd);
133 ehci_writel(&hcor->or_usbcmd, cmd);
134 ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, 250);
136 printf("EHCI fail to reset\n");
140 #if defined CONFIG_EHCI_IS_TDI
141 reg_ptr = (uint32_t *)((u8 *)hcor + USBMODE);
142 tmp = ehci_readl(reg_ptr);
143 tmp |= USBMODE_CM_HC;
144 #if defined CONFIG_EHCI_MMIO_BIG_ENDIAN
147 ehci_writel(reg_ptr, tmp);
153 static void *ehci_alloc(size_t sz, size_t align)
155 static struct QH qh __attribute__((aligned(32)));
156 static struct qTD td[3] __attribute__((aligned (32)));
161 case sizeof(struct QH):
165 case sizeof(struct qTD):
167 debug("out of TDs\n");
174 debug("unknown allocation size\n");
182 static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
184 uint32_t addr, delta, next;
187 addr = (uint32_t) buf;
190 td->qt_buffer[idx] = cpu_to_hc32(addr);
191 next = (addr + 4096) & ~4095;
201 debug("out of buffer pointers (%u bytes left)\n", sz);
209 ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
210 int length, struct devrequest *req)
214 volatile struct qTD *vtd;
217 uint32_t endpt, token, usbsts;
222 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
223 buffer, length, req);
225 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
226 req->request, req->request,
227 req->requesttype, req->requesttype,
228 le16_to_cpu(req->value), le16_to_cpu(req->value),
229 le16_to_cpu(req->index));
231 qh = ehci_alloc(sizeof(struct QH), 32);
233 debug("unable to allocate QH\n");
236 qh->qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
237 c = (usb_pipespeed(pipe) != USB_SPEED_HIGH &&
238 usb_pipeendpoint(pipe) == 0) ? 1 : 0;
241 (usb_maxpacket(dev, pipe) << 16) |
244 (usb_pipespeed(pipe) << 12) |
245 (usb_pipeendpoint(pipe) << 8) |
246 (0 << 7) | (usb_pipedevice(pipe) << 0);
247 qh->qh_endpt1 = cpu_to_hc32(endpt);
249 (dev->portnr << 23) |
250 (dev->parent->devnum << 16) | (0 << 8) | (0 << 0);
251 qh->qh_endpt2 = cpu_to_hc32(endpt);
252 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
253 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
256 tdp = &qh->qh_overlay.qt_next;
259 usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
262 td = ehci_alloc(sizeof(struct qTD), 32);
264 debug("unable to allocate SETUP td\n");
267 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
268 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
270 (sizeof(*req) << 16) |
271 (0 << 15) | (0 << 12) | (3 << 10) | (2 << 8) | (0x80 << 0);
272 td->qt_token = cpu_to_hc32(token);
273 if (ehci_td_buffer(td, req, sizeof(*req)) != 0) {
274 debug("unable construct SETUP td\n");
275 ehci_free(td, sizeof(*td));
278 *tdp = cpu_to_hc32((uint32_t) td);
283 if (length > 0 || req == NULL) {
284 td = ehci_alloc(sizeof(struct qTD), 32);
286 debug("unable to allocate DATA td\n");
289 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
290 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
291 token = (toggle << 31) |
293 ((req == NULL ? 1 : 0) << 15) |
296 ((usb_pipein(pipe) ? 1 : 0) << 8) | (0x80 << 0);
297 td->qt_token = cpu_to_hc32(token);
298 if (ehci_td_buffer(td, buffer, length) != 0) {
299 debug("unable construct DATA td\n");
300 ehci_free(td, sizeof(*td));
303 *tdp = cpu_to_hc32((uint32_t) td);
308 td = ehci_alloc(sizeof(struct qTD), 32);
310 debug("unable to allocate ACK td\n");
313 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
314 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
315 token = (toggle << 31) |
320 ((usb_pipein(pipe) ? 0 : 1) << 8) | (0x80 << 0);
321 td->qt_token = cpu_to_hc32(token);
322 *tdp = cpu_to_hc32((uint32_t) td);
326 qh_list.qh_link = cpu_to_hc32((uint32_t) qh | QH_LINK_TYPE_QH);
328 usbsts = ehci_readl(&hcor->or_usbsts);
329 ehci_writel(&hcor->or_usbsts, (usbsts & 0x3f));
331 /* Enable async. schedule. */
332 cmd = ehci_readl(&hcor->or_usbcmd);
334 ehci_writel(&hcor->or_usbcmd, cmd);
336 sts = ehci_readl(&hcor->or_usbsts);
337 while ((sts & STD_ASS) == 0) {
338 sts = ehci_readl(&hcor->or_usbsts);
342 /* Wait for TDs to be processed. */
346 token = hc32_to_cpu(vtd->qt_token);
349 } while (get_timer(ts) < CONFIG_SYS_HZ);
351 /* Disable async schedule. */
352 cmd = ehci_readl(&hcor->or_usbcmd);
354 ehci_writel(&hcor->or_usbcmd, cmd);
356 sts = ehci_readl(&hcor->or_usbsts);
357 while ((sts & STD_ASS) != 0) {
358 sts = ehci_readl(&hcor->or_usbsts);
362 qh_list.qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
364 token = hc32_to_cpu(qh->qh_overlay.qt_token);
365 if (!(token & 0x80)) {
366 debug("TOKEN=%#x\n", token);
367 switch (token & 0xfc) {
369 toggle = token >> 31;
370 usb_settoggle(dev, usb_pipeendpoint(pipe),
371 usb_pipeout(pipe), toggle);
375 dev->status = USB_ST_STALLED;
379 dev->status = USB_ST_BUF_ERR;
383 dev->status = USB_ST_BABBLE_DET;
386 dev->status = USB_ST_CRC_ERR;
389 dev->act_len = length - ((token >> 16) & 0x7fff);
392 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
393 dev->devnum, ehci_readl(&hcor->or_usbsts),
394 ehci_readl(&hcor->or_portsc[0]),
395 ehci_readl(&hcor->or_portsc[1]));
398 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
401 td = (void *)hc32_to_cpu(qh->qh_overlay.qt_next);
402 while (td != (void *)QT_NEXT_TERMINATE) {
403 qh->qh_overlay.qt_next = td->qt_next;
404 ehci_free(td, sizeof(*td));
405 td = (void *)hc32_to_cpu(qh->qh_overlay.qt_next);
407 ehci_free(qh, sizeof(*qh));
411 static inline int min3(int a, int b, int c)
422 ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
423 int length, struct devrequest *req)
433 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
434 req->request, req->request,
435 req->requesttype, req->requesttype,
436 le16_to_cpu(req->value), le16_to_cpu(req->index));
438 typeReq = req->request << 8 | req->requesttype;
440 switch (le16_to_cpu(typeReq)) {
441 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
442 switch (le16_to_cpu(req->value) >> 8) {
444 debug("USB_DT_DEVICE request\n");
445 srcptr = &descriptor.device;
449 debug("USB_DT_CONFIG config\n");
450 srcptr = &descriptor.config;
454 debug("USB_DT_STRING config\n");
455 switch (le16_to_cpu(req->value) & 0xff) {
456 case 0: /* Language */
461 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
464 case 2: /* Product */
465 srcptr = "\52\3E\0H\0C\0I\0 "
467 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
471 debug("unknown value DT_STRING %x\n",
472 le16_to_cpu(req->value));
477 debug("unknown value %x\n", le16_to_cpu(req->value));
481 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
482 switch (le16_to_cpu(req->value) >> 8) {
484 debug("USB_DT_HUB config\n");
485 srcptr = &descriptor.hub;
489 debug("unknown value %x\n", le16_to_cpu(req->value));
493 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
494 debug("USB_REQ_SET_ADDRESS\n");
495 rootdev = le16_to_cpu(req->value);
497 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
498 debug("USB_REQ_SET_CONFIGURATION\n");
501 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
502 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
507 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
508 memset(tmpbuf, 0, 4);
509 reg = ehci_readl(&hcor->or_portsc[le16_to_cpu(req->index)
511 if (reg & EHCI_PS_CS)
512 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
513 if (reg & EHCI_PS_PE)
514 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
515 if (reg & EHCI_PS_SUSP)
516 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
517 if (reg & EHCI_PS_OCA)
518 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
519 if (reg & EHCI_PS_PR)
520 tmpbuf[0] |= USB_PORT_STAT_RESET;
521 if (reg & EHCI_PS_PP)
522 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
523 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
525 if (reg & EHCI_PS_CSC)
526 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
527 if (reg & EHCI_PS_PEC)
528 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
529 if (reg & EHCI_PS_OCC)
530 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
531 if (portreset & (1 << le16_to_cpu(req->index)))
532 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
536 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
537 reg = ehci_readl(&hcor->or_portsc[le16_to_cpu(req->index) - 1]);
538 reg &= ~EHCI_PS_CLEAR;
539 switch (le16_to_cpu(req->value)) {
540 case USB_PORT_FEAT_ENABLE:
543 case USB_PORT_FEAT_POWER:
546 case USB_PORT_FEAT_RESET:
547 debug("USB FEAT RESET\n");
548 if (EHCI_PS_IS_LOWSPEED(reg)) {
549 /* Low speed device, give up ownership. */
553 /* Start reset sequence. */
556 ehci_writel(&hcor->or_portsc[
557 le16_to_cpu(req->index) - 1], reg);
558 /* Wait for reset to complete. */
560 /* Terminate reset sequence. */
562 /* TODO: is it only fsl chip that requires this
563 * manual setting of port enable?
566 ehci_writel(&hcor->or_portsc[
567 le16_to_cpu(req->index) - 1], reg);
568 /* Wait for HC to complete reset. */
571 ehci_readl(&hcor->or_portsc[le16_to_cpu(req->index)
573 reg &= ~EHCI_PS_CLEAR;
574 if ((reg & EHCI_PS_PE) == 0) {
575 /* Not a high speed device, give up
580 portreset |= 1 << le16_to_cpu(req->index);
583 debug("unknown feature %x\n", le16_to_cpu(req->value));
586 ehci_writel(&hcor->or_portsc[le16_to_cpu(req->index) - 1], reg);
588 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
589 reg = ehci_readl(&hcor->or_portsc[le16_to_cpu(req->index) - 1]);
590 reg &= ~EHCI_PS_CLEAR;
591 switch (le16_to_cpu(req->value)) {
592 case USB_PORT_FEAT_ENABLE:
595 case USB_PORT_FEAT_C_CONNECTION:
598 case USB_PORT_FEAT_OVER_CURRENT:
601 case USB_PORT_FEAT_C_RESET:
602 portreset &= ~(1 << le16_to_cpu(req->index));
605 debug("unknown feature %x\n", le16_to_cpu(req->value));
608 ehci_writel(&hcor->or_portsc[le16_to_cpu(req->index) - 1], reg);
611 debug("Unknown request\n");
616 len = min3(srclen, le16_to_cpu(req->length), length);
617 if (srcptr != NULL && len > 0)
618 memcpy(buffer, srcptr, len);
627 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
628 req->requesttype, req->request, le16_to_cpu(req->value),
629 le16_to_cpu(req->index), le16_to_cpu(req->length));
632 dev->status = USB_ST_STALLED;
636 int usb_lowlevel_stop(void)
638 return ehci_hcd_stop();
641 int usb_lowlevel_init(void)
646 if (ehci_hcd_init() != 0)
649 /* EHCI spec section 4.1 */
650 if (ehci_reset() != 0)
653 /* Set head of reclaim list */
654 memset(&qh_list, 0, sizeof(qh_list));
655 qh_list.qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
656 qh_list.qh_endpt1 = cpu_to_hc32((1 << 15) | (USB_SPEED_HIGH << 12));
657 qh_list.qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
658 qh_list.qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
659 qh_list.qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
660 qh_list.qh_overlay.qt_token = cpu_to_hc32(0x40);
662 /* Set async. queue head pointer. */
663 ehci_writel(&hcor->or_asynclistaddr, (uint32_t)&qh_list);
665 reg = ehci_readl(&hccr->cr_hcsparams);
666 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
667 printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
668 if (reg & 0x10000) /* Port Indicators */
669 descriptor.hub.wHubCharacteristics |= 0x80;
670 if (reg & 0x10) /* Port Power Control */
671 descriptor.hub.wHubCharacteristics |= 0x01;
673 /* Start the host controller. */
674 cmd = ehci_readl(&hcor->or_usbcmd);
675 /* Philips, Intel, and maybe others need CMD_RUN before the
676 * root hub will detect new devices (why?); NEC doesn't */
677 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
679 ehci_writel(&hcor->or_usbcmd, cmd);
681 /* take control over the ports */
682 cmd = ehci_readl(&hcor->or_configflag);
684 ehci_writel(&hcor->or_configflag, cmd);
685 /* unblock posted writes */
686 cmd = ehci_readl(&hcor->or_usbcmd);
695 submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
699 if (usb_pipetype(pipe) != PIPE_BULK) {
700 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
703 return ehci_submit_async(dev, pipe, buffer, length, NULL);
707 submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
708 int length, struct devrequest *setup)
711 if (usb_pipetype(pipe) != PIPE_CONTROL) {
712 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
716 if (usb_pipedevice(pipe) == rootdev) {
718 dev->speed = USB_SPEED_HIGH;
719 return ehci_submit_root(dev, pipe, buffer, length, setup);
721 return ehci_submit_async(dev, pipe, buffer, length, setup);
725 submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
726 int length, int interval)
729 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
730 dev, pipe, buffer, length, interval);