2 * Copyright (c) 2007-2008, Juniper Networks, Inc.
3 * Copyright (c) 2008, Excito Elektronik i Skåne AB
4 * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2 of
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #include <asm/byteorder.h>
31 struct ehci_hccr
*hccr
; /* R/O registers, not need for volatile */
32 volatile struct ehci_hcor
*hcor
;
34 static uint16_t portreset
;
35 static struct QH qh_list
__attribute__((aligned(32)));
37 static struct descriptor
{
38 struct usb_hub_descriptor hub
;
39 struct usb_device_descriptor device
;
40 struct usb_linux_config_descriptor config
;
41 struct usb_linux_interface_descriptor interface
;
42 struct usb_endpoint_descriptor endpoint
;
43 } __attribute__ ((packed
)) descriptor
= {
45 0x8, /* bDescLength */
46 0x29, /* bDescriptorType: hub descriptor */
47 2, /* bNrPorts -- runtime modified */
48 0, /* wHubCharacteristics */
49 0xff, /* bPwrOn2PwrGood */
50 0, /* bHubCntrCurrent */
51 {}, /* Device removable */
52 {} /* at most 7 ports! XXX */
56 1, /* bDescriptorType: UDESC_DEVICE */
57 0x0002, /* bcdUSB: v2.0 */
58 9, /* bDeviceClass: UDCLASS_HUB */
59 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
60 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
61 64, /* bMaxPacketSize: 64 bytes */
62 0x0000, /* idVendor */
63 0x0000, /* idProduct */
64 0x0001, /* bcdDevice */
65 1, /* iManufacturer */
67 0, /* iSerialNumber */
68 1 /* bNumConfigurations: 1 */
72 2, /* bDescriptorType: UDESC_CONFIG */
74 1, /* bNumInterface */
75 1, /* bConfigurationValue */
76 0, /* iConfiguration */
77 0x40, /* bmAttributes: UC_SELF_POWER */
82 4, /* bDescriptorType: UDESC_INTERFACE */
83 0, /* bInterfaceNumber */
84 0, /* bAlternateSetting */
85 1, /* bNumEndpoints */
86 9, /* bInterfaceClass: UICLASS_HUB */
87 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
88 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
93 5, /* bDescriptorType: UDESC_ENDPOINT */
94 0x81, /* bEndpointAddress:
95 * UE_DIR_IN | EHCI_INTR_ENDPT
97 3, /* bmAttributes: UE_INTERRUPT */
98 8, 0, /* wMaxPacketSize */
103 #if defined(CONFIG_EHCI_IS_TDI)
104 #define ehci_is_TDI() (1)
106 #define ehci_is_TDI() (0)
109 static int handshake(uint32_t *ptr
, uint32_t mask
, uint32_t done
, int usec
)
113 result
= ehci_readl(ptr
);
114 if (result
== ~(uint32_t)0)
125 static void ehci_free(void *p
, size_t sz
)
130 static int ehci_reset(void)
137 cmd
= ehci_readl(&hcor
->or_usbcmd
);
139 ehci_writel(&hcor
->or_usbcmd
, cmd
);
140 ret
= handshake((uint32_t *)&hcor
->or_usbcmd
, CMD_RESET
, 0, 250 * 1000);
142 printf("EHCI fail to reset\n");
147 reg_ptr
= (uint32_t *)((u8
*)hcor
+ USBMODE
);
148 tmp
= ehci_readl(reg_ptr
);
149 tmp
|= USBMODE_CM_HC
;
150 #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
153 ehci_writel(reg_ptr
, tmp
);
159 static void *ehci_alloc(size_t sz
, size_t align
)
161 static struct QH qh
__attribute__((aligned(32)));
162 static struct qTD td
[3] __attribute__((aligned (32)));
167 case sizeof(struct QH
):
171 case sizeof(struct qTD
):
173 debug("out of TDs\n");
180 debug("unknown allocation size\n");
188 static int ehci_td_buffer(struct qTD
*td
, void *buf
, size_t sz
)
190 uint32_t addr
, delta
, next
;
193 addr
= (uint32_t) buf
;
196 td
->qt_buffer
[idx
] = cpu_to_hc32(addr
);
197 next
= (addr
+ 4096) & ~4095;
207 debug("out of buffer pointers (%u bytes left)\n", sz
);
215 ehci_submit_async(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
216 int length
, struct devrequest
*req
)
220 volatile struct qTD
*vtd
;
223 uint32_t endpt
, token
, usbsts
;
228 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev
, pipe
,
229 buffer
, length
, req
);
231 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
232 req
->request
, req
->request
,
233 req
->requesttype
, req
->requesttype
,
234 le16_to_cpu(req
->value
), le16_to_cpu(req
->value
),
235 le16_to_cpu(req
->index
));
237 qh
= ehci_alloc(sizeof(struct QH
), 32);
239 debug("unable to allocate QH\n");
242 qh
->qh_link
= cpu_to_hc32((uint32_t)&qh_list
| QH_LINK_TYPE_QH
);
243 c
= (usb_pipespeed(pipe
) != USB_SPEED_HIGH
&&
244 usb_pipeendpoint(pipe
) == 0) ? 1 : 0;
247 (usb_maxpacket(dev
, pipe
) << 16) |
250 (usb_pipespeed(pipe
) << 12) |
251 (usb_pipeendpoint(pipe
) << 8) |
252 (0 << 7) | (usb_pipedevice(pipe
) << 0);
253 qh
->qh_endpt1
= cpu_to_hc32(endpt
);
255 (dev
->portnr
<< 23) |
256 (dev
->parent
->devnum
<< 16) | (0 << 8) | (0 << 0);
257 qh
->qh_endpt2
= cpu_to_hc32(endpt
);
258 qh
->qh_overlay
.qt_next
= cpu_to_hc32(QT_NEXT_TERMINATE
);
259 qh
->qh_overlay
.qt_altnext
= cpu_to_hc32(QT_NEXT_TERMINATE
);
262 tdp
= &qh
->qh_overlay
.qt_next
;
265 usb_gettoggle(dev
, usb_pipeendpoint(pipe
), usb_pipeout(pipe
));
268 td
= ehci_alloc(sizeof(struct qTD
), 32);
270 debug("unable to allocate SETUP td\n");
273 td
->qt_next
= cpu_to_hc32(QT_NEXT_TERMINATE
);
274 td
->qt_altnext
= cpu_to_hc32(QT_NEXT_TERMINATE
);
276 (sizeof(*req
) << 16) |
277 (0 << 15) | (0 << 12) | (3 << 10) | (2 << 8) | (0x80 << 0);
278 td
->qt_token
= cpu_to_hc32(token
);
279 if (ehci_td_buffer(td
, req
, sizeof(*req
)) != 0) {
280 debug("unable construct SETUP td\n");
281 ehci_free(td
, sizeof(*td
));
284 *tdp
= cpu_to_hc32((uint32_t) td
);
289 if (length
> 0 || req
== NULL
) {
290 td
= ehci_alloc(sizeof(struct qTD
), 32);
292 debug("unable to allocate DATA td\n");
295 td
->qt_next
= cpu_to_hc32(QT_NEXT_TERMINATE
);
296 td
->qt_altnext
= cpu_to_hc32(QT_NEXT_TERMINATE
);
297 token
= (toggle
<< 31) |
299 ((req
== NULL
? 1 : 0) << 15) |
302 ((usb_pipein(pipe
) ? 1 : 0) << 8) | (0x80 << 0);
303 td
->qt_token
= cpu_to_hc32(token
);
304 if (ehci_td_buffer(td
, buffer
, length
) != 0) {
305 debug("unable construct DATA td\n");
306 ehci_free(td
, sizeof(*td
));
309 *tdp
= cpu_to_hc32((uint32_t) td
);
314 td
= ehci_alloc(sizeof(struct qTD
), 32);
316 debug("unable to allocate ACK td\n");
319 td
->qt_next
= cpu_to_hc32(QT_NEXT_TERMINATE
);
320 td
->qt_altnext
= cpu_to_hc32(QT_NEXT_TERMINATE
);
321 token
= (toggle
<< 31) |
326 ((usb_pipein(pipe
) ? 0 : 1) << 8) | (0x80 << 0);
327 td
->qt_token
= cpu_to_hc32(token
);
328 *tdp
= cpu_to_hc32((uint32_t) td
);
332 qh_list
.qh_link
= cpu_to_hc32((uint32_t) qh
| QH_LINK_TYPE_QH
);
334 usbsts
= ehci_readl(&hcor
->or_usbsts
);
335 ehci_writel(&hcor
->or_usbsts
, (usbsts
& 0x3f));
337 /* Enable async. schedule. */
338 cmd
= ehci_readl(&hcor
->or_usbcmd
);
340 ehci_writel(&hcor
->or_usbcmd
, cmd
);
342 ret
= handshake((uint32_t *)&hcor
->or_usbsts
, STD_ASS
, STD_ASS
,
345 printf("EHCI fail timeout STD_ASS set\n");
349 /* Wait for TDs to be processed. */
353 token
= hc32_to_cpu(vtd
->qt_token
);
356 } while (get_timer(ts
) < CONFIG_SYS_HZ
);
358 /* Disable async schedule. */
359 cmd
= ehci_readl(&hcor
->or_usbcmd
);
361 ehci_writel(&hcor
->or_usbcmd
, cmd
);
363 ret
= handshake((uint32_t *)&hcor
->or_usbsts
, STD_ASS
, 0,
366 printf("EHCI fail timeout STD_ASS reset\n");
370 qh_list
.qh_link
= cpu_to_hc32((uint32_t)&qh_list
| QH_LINK_TYPE_QH
);
372 token
= hc32_to_cpu(qh
->qh_overlay
.qt_token
);
373 if (!(token
& 0x80)) {
374 debug("TOKEN=%#x\n", token
);
375 switch (token
& 0xfc) {
377 toggle
= token
>> 31;
378 usb_settoggle(dev
, usb_pipeendpoint(pipe
),
379 usb_pipeout(pipe
), toggle
);
383 dev
->status
= USB_ST_STALLED
;
387 dev
->status
= USB_ST_BUF_ERR
;
391 dev
->status
= USB_ST_BABBLE_DET
;
394 dev
->status
= USB_ST_CRC_ERR
;
397 dev
->act_len
= length
- ((token
>> 16) & 0x7fff);
400 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
401 dev
->devnum
, ehci_readl(&hcor
->or_usbsts
),
402 ehci_readl(&hcor
->or_portsc
[0]),
403 ehci_readl(&hcor
->or_portsc
[1]));
406 return (dev
->status
!= USB_ST_NOT_PROC
) ? 0 : -1;
409 td
= (void *)hc32_to_cpu(qh
->qh_overlay
.qt_next
);
410 while (td
!= (void *)QT_NEXT_TERMINATE
) {
411 qh
->qh_overlay
.qt_next
= td
->qt_next
;
412 ehci_free(td
, sizeof(*td
));
413 td
= (void *)hc32_to_cpu(qh
->qh_overlay
.qt_next
);
415 ehci_free(qh
, sizeof(*qh
));
419 static inline int min3(int a
, int b
, int c
)
430 ehci_submit_root(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
431 int length
, struct devrequest
*req
)
438 uint32_t *status_reg
;
440 if (le16_to_cpu(req
->index
) >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS
) {
441 printf("The request port(%d) is not configured\n",
442 le16_to_cpu(req
->index
) - 1);
445 status_reg
= (uint32_t *)&hcor
->or_portsc
[
446 le16_to_cpu(req
->index
) - 1];
449 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
450 req
->request
, req
->request
,
451 req
->requesttype
, req
->requesttype
,
452 le16_to_cpu(req
->value
), le16_to_cpu(req
->index
));
454 typeReq
= req
->request
<< 8 | req
->requesttype
;
456 switch (le16_to_cpu(typeReq
)) {
457 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
458 switch (le16_to_cpu(req
->value
) >> 8) {
460 debug("USB_DT_DEVICE request\n");
461 srcptr
= &descriptor
.device
;
465 debug("USB_DT_CONFIG config\n");
466 srcptr
= &descriptor
.config
;
470 debug("USB_DT_STRING config\n");
471 switch (le16_to_cpu(req
->value
) & 0xff) {
472 case 0: /* Language */
477 srcptr
= "\16\3u\0-\0b\0o\0o\0t\0";
480 case 2: /* Product */
481 srcptr
= "\52\3E\0H\0C\0I\0 "
483 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
487 debug("unknown value DT_STRING %x\n",
488 le16_to_cpu(req
->value
));
493 debug("unknown value %x\n", le16_to_cpu(req
->value
));
497 case USB_REQ_GET_DESCRIPTOR
| ((USB_DIR_IN
| USB_RT_HUB
) << 8):
498 switch (le16_to_cpu(req
->value
) >> 8) {
500 debug("USB_DT_HUB config\n");
501 srcptr
= &descriptor
.hub
;
505 debug("unknown value %x\n", le16_to_cpu(req
->value
));
509 case USB_REQ_SET_ADDRESS
| (USB_RECIP_DEVICE
<< 8):
510 debug("USB_REQ_SET_ADDRESS\n");
511 rootdev
= le16_to_cpu(req
->value
);
513 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
514 debug("USB_REQ_SET_CONFIGURATION\n");
517 case USB_REQ_GET_STATUS
| ((USB_DIR_IN
| USB_RT_HUB
) << 8):
518 tmpbuf
[0] = 1; /* USB_STATUS_SELFPOWERED */
523 case USB_REQ_GET_STATUS
| ((USB_RT_PORT
| USB_DIR_IN
) << 8):
524 memset(tmpbuf
, 0, 4);
525 reg
= ehci_readl(status_reg
);
526 if (reg
& EHCI_PS_CS
)
527 tmpbuf
[0] |= USB_PORT_STAT_CONNECTION
;
528 if (reg
& EHCI_PS_PE
)
529 tmpbuf
[0] |= USB_PORT_STAT_ENABLE
;
530 if (reg
& EHCI_PS_SUSP
)
531 tmpbuf
[0] |= USB_PORT_STAT_SUSPEND
;
532 if (reg
& EHCI_PS_OCA
)
533 tmpbuf
[0] |= USB_PORT_STAT_OVERCURRENT
;
534 if (reg
& EHCI_PS_PR
&&
535 (portreset
& (1 << le16_to_cpu(req
->index
)))) {
537 /* force reset to complete */
538 reg
= reg
& ~(EHCI_PS_PR
| EHCI_PS_CLEAR
);
539 ehci_writel(status_reg
, reg
);
540 ret
= handshake(status_reg
, EHCI_PS_PR
, 0, 2 * 1000);
542 tmpbuf
[0] |= USB_PORT_STAT_RESET
;
544 printf("port(%d) reset error\n",
545 le16_to_cpu(req
->index
) - 1);
547 if (reg
& EHCI_PS_PP
)
548 tmpbuf
[1] |= USB_PORT_STAT_POWER
>> 8;
551 switch ((reg
>> 26) & 3) {
555 tmpbuf
[1] |= USB_PORT_STAT_LOW_SPEED
>> 8;
559 tmpbuf
[1] |= USB_PORT_STAT_HIGH_SPEED
>> 8;
563 tmpbuf
[1] |= USB_PORT_STAT_HIGH_SPEED
>> 8;
566 if (reg
& EHCI_PS_CSC
)
567 tmpbuf
[2] |= USB_PORT_STAT_C_CONNECTION
;
568 if (reg
& EHCI_PS_PEC
)
569 tmpbuf
[2] |= USB_PORT_STAT_C_ENABLE
;
570 if (reg
& EHCI_PS_OCC
)
571 tmpbuf
[2] |= USB_PORT_STAT_C_OVERCURRENT
;
572 if (portreset
& (1 << le16_to_cpu(req
->index
)))
573 tmpbuf
[2] |= USB_PORT_STAT_C_RESET
;
578 case USB_REQ_SET_FEATURE
| ((USB_DIR_OUT
| USB_RT_PORT
) << 8):
579 reg
= ehci_readl(status_reg
);
580 reg
&= ~EHCI_PS_CLEAR
;
581 switch (le16_to_cpu(req
->value
)) {
582 case USB_PORT_FEAT_ENABLE
:
584 ehci_writel(status_reg
, reg
);
586 case USB_PORT_FEAT_POWER
:
587 if (HCS_PPC(ehci_readl(&hccr
->cr_hcsparams
))) {
589 ehci_writel(status_reg
, reg
);
592 case USB_PORT_FEAT_RESET
:
593 if ((reg
& (EHCI_PS_PE
| EHCI_PS_CS
)) == EHCI_PS_CS
&&
595 EHCI_PS_IS_LOWSPEED(reg
)) {
596 /* Low speed device, give up ownership. */
597 debug("port %d low speed --> companion\n",
600 ehci_writel(status_reg
, reg
);
605 ehci_writel(status_reg
, reg
);
607 * caller must wait, then call GetPortStatus
608 * usb 2.0 specification say 50 ms resets on
612 portreset
|= 1 << le16_to_cpu(req
->index
);
616 debug("unknown feature %x\n", le16_to_cpu(req
->value
));
619 /* unblock posted writes */
620 ehci_readl(&hcor
->or_usbcmd
);
622 case USB_REQ_CLEAR_FEATURE
| ((USB_DIR_OUT
| USB_RT_PORT
) << 8):
623 reg
= ehci_readl(status_reg
);
624 switch (le16_to_cpu(req
->value
)) {
625 case USB_PORT_FEAT_ENABLE
:
628 case USB_PORT_FEAT_C_ENABLE
:
629 reg
= (reg
& ~EHCI_PS_CLEAR
) | EHCI_PS_PE
;
631 case USB_PORT_FEAT_POWER
:
632 if (HCS_PPC(ehci_readl(&hccr
->cr_hcsparams
)))
633 reg
= reg
& ~(EHCI_PS_CLEAR
| EHCI_PS_PP
);
634 case USB_PORT_FEAT_C_CONNECTION
:
635 reg
= (reg
& ~EHCI_PS_CLEAR
) | EHCI_PS_CSC
;
637 case USB_PORT_FEAT_OVER_CURRENT
:
638 reg
= (reg
& ~EHCI_PS_CLEAR
) | EHCI_PS_OCC
;
640 case USB_PORT_FEAT_C_RESET
:
641 portreset
&= ~(1 << le16_to_cpu(req
->index
));
644 debug("unknown feature %x\n", le16_to_cpu(req
->value
));
647 ehci_writel(status_reg
, reg
);
648 /* unblock posted write */
649 ehci_readl(&hcor
->or_usbcmd
);
652 debug("Unknown request\n");
657 len
= min3(srclen
, le16_to_cpu(req
->length
), length
);
658 if (srcptr
!= NULL
&& len
> 0)
659 memcpy(buffer
, srcptr
, len
);
668 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
669 req
->requesttype
, req
->request
, le16_to_cpu(req
->value
),
670 le16_to_cpu(req
->index
), le16_to_cpu(req
->length
));
673 dev
->status
= USB_ST_STALLED
;
677 int usb_lowlevel_stop(void)
679 return ehci_hcd_stop();
682 int usb_lowlevel_init(void)
687 if (ehci_hcd_init() != 0)
690 /* EHCI spec section 4.1 */
691 if (ehci_reset() != 0)
694 #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
695 if (ehci_hcd_init() != 0)
699 /* Set head of reclaim list */
700 memset(&qh_list
, 0, sizeof(qh_list
));
701 qh_list
.qh_link
= cpu_to_hc32((uint32_t)&qh_list
| QH_LINK_TYPE_QH
);
702 qh_list
.qh_endpt1
= cpu_to_hc32((1 << 15) | (USB_SPEED_HIGH
<< 12));
703 qh_list
.qh_curtd
= cpu_to_hc32(QT_NEXT_TERMINATE
);
704 qh_list
.qh_overlay
.qt_next
= cpu_to_hc32(QT_NEXT_TERMINATE
);
705 qh_list
.qh_overlay
.qt_altnext
= cpu_to_hc32(QT_NEXT_TERMINATE
);
706 qh_list
.qh_overlay
.qt_token
= cpu_to_hc32(0x40);
708 /* Set async. queue head pointer. */
709 ehci_writel(&hcor
->or_asynclistaddr
, (uint32_t)&qh_list
);
711 reg
= ehci_readl(&hccr
->cr_hcsparams
);
712 descriptor
.hub
.bNbrPorts
= HCS_N_PORTS(reg
);
713 printf("Register %x NbrPorts %d\n", reg
, descriptor
.hub
.bNbrPorts
);
714 /* Port Indicators */
715 if (HCS_INDICATOR(reg
))
716 descriptor
.hub
.wHubCharacteristics
|= 0x80;
717 /* Port Power Control */
719 descriptor
.hub
.wHubCharacteristics
|= 0x01;
721 /* Start the host controller. */
722 cmd
= ehci_readl(&hcor
->or_usbcmd
);
723 /* Philips, Intel, and maybe others need CMD_RUN before the
724 * root hub will detect new devices (why?); NEC doesn't */
725 cmd
&= ~(CMD_LRESET
|CMD_IAAD
|CMD_PSE
|CMD_ASE
|CMD_RESET
);
727 ehci_writel(&hcor
->or_usbcmd
, cmd
);
729 /* take control over the ports */
730 cmd
= ehci_readl(&hcor
->or_configflag
);
732 ehci_writel(&hcor
->or_configflag
, cmd
);
733 /* unblock posted write */
734 cmd
= ehci_readl(&hcor
->or_usbcmd
);
736 reg
= HC_VERSION(ehci_readl(&hccr
->cr_capbase
));
737 printf("USB EHCI %x.%02x\n", reg
>> 8, reg
& 0xff);
745 submit_bulk_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
749 if (usb_pipetype(pipe
) != PIPE_BULK
) {
750 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe
));
753 return ehci_submit_async(dev
, pipe
, buffer
, length
, NULL
);
757 submit_control_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
758 int length
, struct devrequest
*setup
)
761 if (usb_pipetype(pipe
) != PIPE_CONTROL
) {
762 debug("non-control pipe (type=%lu)", usb_pipetype(pipe
));
766 if (usb_pipedevice(pipe
) == rootdev
) {
768 dev
->speed
= USB_SPEED_HIGH
;
769 return ehci_submit_root(dev
, pipe
, buffer
, length
, setup
);
771 return ehci_submit_async(dev
, pipe
, buffer
, length
, setup
);
775 submit_int_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
776 int length
, int interval
)
779 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
780 dev
, pipe
, buffer
, length
, interval
);