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 * SPDX-License-Identifier: GPL-2.0
13 #include <asm/byteorder.h>
14 #include <asm/unaligned.h>
20 #include <linux/compiler.h>
24 #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
25 #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
29 * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
30 * Let's time out after 8 to have a little safety margin on top of that.
32 #define HCHALT_TIMEOUT (8 * 1000)
35 static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
38 #define ALIGN_END_ADDR(type, ptr, size) \
39 ((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
41 static struct descriptor {
42 struct usb_hub_descriptor hub;
43 struct usb_device_descriptor device;
44 struct usb_linux_config_descriptor config;
45 struct usb_linux_interface_descriptor interface;
46 struct usb_endpoint_descriptor endpoint;
47 } __attribute__ ((packed)) descriptor = {
49 0x8, /* bDescLength */
50 0x29, /* bDescriptorType: hub descriptor */
51 2, /* bNrPorts -- runtime modified */
52 0, /* wHubCharacteristics */
53 10, /* bPwrOn2PwrGood */
54 0, /* bHubCntrCurrent */
55 {}, /* Device removable */
56 {} /* at most 7 ports! XXX */
60 1, /* bDescriptorType: UDESC_DEVICE */
61 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
62 9, /* bDeviceClass: UDCLASS_HUB */
63 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
64 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
65 64, /* bMaxPacketSize: 64 bytes */
66 0x0000, /* idVendor */
67 0x0000, /* idProduct */
68 cpu_to_le16(0x0100), /* bcdDevice */
69 1, /* iManufacturer */
71 0, /* iSerialNumber */
72 1 /* bNumConfigurations: 1 */
76 2, /* bDescriptorType: UDESC_CONFIG */
78 1, /* bNumInterface */
79 1, /* bConfigurationValue */
80 0, /* iConfiguration */
81 0x40, /* bmAttributes: UC_SELF_POWER */
86 4, /* bDescriptorType: UDESC_INTERFACE */
87 0, /* bInterfaceNumber */
88 0, /* bAlternateSetting */
89 1, /* bNumEndpoints */
90 9, /* bInterfaceClass: UICLASS_HUB */
91 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
92 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
97 5, /* bDescriptorType: UDESC_ENDPOINT */
98 0x81, /* bEndpointAddress:
99 * UE_DIR_IN | EHCI_INTR_ENDPT
101 3, /* bmAttributes: UE_INTERRUPT */
102 8, /* wMaxPacketSize */
107 #if defined(CONFIG_EHCI_IS_TDI)
108 #define ehci_is_TDI() (1)
110 #define ehci_is_TDI() (0)
113 static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev)
116 return dev_get_priv(usb_get_bus(udev->dev));
118 return udev->controller;
122 static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
124 return PORTSC_PSPD(reg);
127 static void ehci_set_usbmode(struct ehci_ctrl *ctrl)
132 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE);
133 tmp = ehci_readl(reg_ptr);
134 tmp |= USBMODE_CM_HC;
135 #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
138 ehci_writel(reg_ptr, tmp);
141 static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
147 static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
149 if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
150 /* Printing the message would cause a scan failure! */
151 debug("The request port(%u) is not configured\n", port);
155 return (uint32_t *)&ctrl->hcor->or_portsc[port];
158 static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
162 result = ehci_readl(ptr);
164 if (result == ~(uint32_t)0)
174 static int ehci_reset(struct ehci_ctrl *ctrl)
179 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
180 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
181 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
182 ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
183 CMD_RESET, 0, 250 * 1000);
185 printf("EHCI fail to reset\n");
190 ctrl->ops.set_usb_mode(ctrl);
192 #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
193 cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
194 cmd &= ~TXFIFO_THRESH_MASK;
195 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
196 ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
202 static int ehci_shutdown(struct ehci_ctrl *ctrl)
207 if (!ctrl || !ctrl->hcor)
210 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
211 cmd &= ~(CMD_PSE | CMD_ASE);
212 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
213 ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
217 for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
218 reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
220 ehci_writel(&ctrl->hcor->or_portsc[i], reg);
224 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
225 ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
230 puts("EHCI failed to shut down host controller.\n");
235 static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
237 uint32_t delta, next;
238 uint32_t addr = (unsigned long)buf;
241 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
242 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
244 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
247 while (idx < QT_BUFFER_CNT) {
248 td->qt_buffer[idx] = cpu_to_hc32(virt_to_phys((void *)addr));
249 td->qt_buffer_hi[idx] = 0;
250 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
259 if (idx == QT_BUFFER_CNT) {
260 printf("out of buffer pointers (%zu bytes left)\n", sz);
267 static inline u8 ehci_encode_speed(enum usb_device_speed speed)
269 #define QH_HIGH_SPEED 2
270 #define QH_FULL_SPEED 0
271 #define QH_LOW_SPEED 1
272 if (speed == USB_SPEED_HIGH)
273 return QH_HIGH_SPEED;
274 if (speed == USB_SPEED_LOW)
276 return QH_FULL_SPEED;
279 static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
285 if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
288 usb_find_usb2_hub_address_port(udev, &hubaddr, &portnr);
290 qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(portnr) |
291 QH_ENDPT2_HUBADDR(hubaddr));
295 ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
296 int length, struct devrequest *req)
298 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
302 volatile struct qTD *vtd;
305 uint32_t endpt, maxpacket, token, usbsts;
310 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
312 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
313 buffer, length, req);
315 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
316 req->request, req->request,
317 req->requesttype, req->requesttype,
318 le16_to_cpu(req->value), le16_to_cpu(req->value),
319 le16_to_cpu(req->index));
321 #define PKT_ALIGN 512
323 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
324 * described by a transfer descriptor (the qTD). The qTDs form a linked
325 * list with a queue head (QH).
327 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
328 * have its beginning in a qTD transfer and its end in the following
329 * one, so the qTD transfer lengths have to be chosen accordingly.
331 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
332 * single pages. The first data buffer can start at any offset within a
333 * page (not considering the cache-line alignment issues), while the
334 * following buffers must be page-aligned. There is no alignment
335 * constraint on the size of a qTD transfer.
338 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
340 if (length > 0 || req == NULL) {
342 * Determine the qTD transfer size that will be used for the
343 * data payload (not considering the first qTD transfer, which
344 * may be longer or shorter, and the final one, which may be
347 * In order to keep each packet within a qTD transfer, the qTD
348 * transfer size is aligned to PKT_ALIGN, which is a multiple of
349 * wMaxPacketSize (except in some cases for interrupt transfers,
350 * see comment in submit_int_msg()).
352 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
353 * QT_BUFFER_CNT full pages will be used.
355 int xfr_sz = QT_BUFFER_CNT;
357 * However, if the input buffer is not aligned to PKT_ALIGN, the
358 * qTD transfer size will be one page shorter, and the first qTD
359 * data buffer of each transfer will be page-unaligned.
361 if ((unsigned long)buffer & (PKT_ALIGN - 1))
363 /* Convert the qTD transfer size to bytes. */
364 xfr_sz *= EHCI_PAGE_SIZE;
366 * Approximate by excess the number of qTDs that will be
367 * required for the data payload. The exact formula is way more
368 * complicated and saves at most 2 qTDs, i.e. a total of 128
371 qtd_count += 2 + length / xfr_sz;
374 * Threshold value based on the worst-case total size of the allocated qTDs for
375 * a mass-storage transfer of 65535 blocks of 512 bytes.
377 #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
378 #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
380 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
382 printf("unable to allocate TDs\n");
386 memset(qh, 0, sizeof(struct QH));
387 memset(qtd, 0, qtd_count * sizeof(*qtd));
389 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
392 * Setup QH (3.6 in ehci-r10.pdf)
394 * qh_link ................. 03-00 H
395 * qh_endpt1 ............... 07-04 H
396 * qh_endpt2 ............... 0B-08 H
398 * qh_overlay.qt_next ...... 13-10 H
399 * - qh_overlay.qt_altnext
401 qh->qh_link = cpu_to_hc32(virt_to_phys(&ctrl->qh_list) | QH_LINK_TYPE_QH);
402 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
403 maxpacket = usb_maxpacket(dev, pipe);
404 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
405 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
406 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
407 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
408 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
409 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
410 qh->qh_endpt1 = cpu_to_hc32(endpt);
411 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
412 qh->qh_endpt2 = cpu_to_hc32(endpt);
413 ehci_update_endpt2_dev_n_port(dev, qh);
414 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
415 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
417 tdp = &qh->qh_overlay.qt_next;
420 * Setup request qTD (3.5 in ehci-r10.pdf)
422 * qt_next ................ 03-00 H
423 * qt_altnext ............. 07-04 H
424 * qt_token ............... 0B-08 H
426 * [ buffer, buffer_hi ] loaded with "req".
428 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
429 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
430 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
431 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
432 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
433 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
434 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
435 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
436 printf("unable to construct SETUP TD\n");
439 /* Update previous qTD! */
440 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
441 tdp = &qtd[qtd_counter++].qt_next;
445 if (length > 0 || req == NULL) {
446 uint8_t *buf_ptr = buffer;
447 int left_length = length;
451 * Determine the size of this qTD transfer. By default,
452 * QT_BUFFER_CNT full pages can be used.
454 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
456 * However, if the input buffer is not page-aligned, the
457 * portion of the first page before the buffer start
458 * offset within that page is unusable.
460 xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
462 * In order to keep each packet within a qTD transfer,
463 * align the qTD transfer size to PKT_ALIGN.
465 xfr_bytes &= ~(PKT_ALIGN - 1);
467 * This transfer may be shorter than the available qTD
468 * transfer size that has just been computed.
470 xfr_bytes = min(xfr_bytes, left_length);
473 * Setup request qTD (3.5 in ehci-r10.pdf)
475 * qt_next ................ 03-00 H
476 * qt_altnext ............. 07-04 H
477 * qt_token ............... 0B-08 H
479 * [ buffer, buffer_hi ] loaded with "buffer".
481 qtd[qtd_counter].qt_next =
482 cpu_to_hc32(QT_NEXT_TERMINATE);
483 qtd[qtd_counter].qt_altnext =
484 cpu_to_hc32(QT_NEXT_TERMINATE);
485 token = QT_TOKEN_DT(toggle) |
486 QT_TOKEN_TOTALBYTES(xfr_bytes) |
487 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
489 QT_TOKEN_PID(usb_pipein(pipe) ?
490 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
491 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
492 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
493 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
495 printf("unable to construct DATA TD\n");
498 /* Update previous qTD! */
499 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
500 tdp = &qtd[qtd_counter++].qt_next;
502 * Data toggle has to be adjusted since the qTD transfer
503 * size is not always an even multiple of
506 if ((xfr_bytes / maxpacket) & 1)
508 buf_ptr += xfr_bytes;
509 left_length -= xfr_bytes;
510 } while (left_length > 0);
515 * Setup request qTD (3.5 in ehci-r10.pdf)
517 * qt_next ................ 03-00 H
518 * qt_altnext ............. 07-04 H
519 * qt_token ............... 0B-08 H
521 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
522 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
523 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
524 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
525 QT_TOKEN_PID(usb_pipein(pipe) ?
526 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
527 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
528 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
529 /* Update previous qTD! */
530 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
531 tdp = &qtd[qtd_counter++].qt_next;
534 ctrl->qh_list.qh_link = cpu_to_hc32(virt_to_phys(qh) | QH_LINK_TYPE_QH);
537 flush_dcache_range((unsigned long)&ctrl->qh_list,
538 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
539 flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
540 flush_dcache_range((unsigned long)qtd,
541 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
543 /* Set async. queue head pointer. */
544 ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(&ctrl->qh_list));
546 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
547 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
549 /* Enable async. schedule. */
550 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
552 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
554 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
557 printf("EHCI fail timeout STS_ASS set\n");
561 /* Wait for TDs to be processed. */
563 vtd = &qtd[qtd_counter - 1];
564 timeout = USB_TIMEOUT_MS(pipe);
566 /* Invalidate dcache */
567 invalidate_dcache_range((unsigned long)&ctrl->qh_list,
568 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
569 invalidate_dcache_range((unsigned long)qh,
570 ALIGN_END_ADDR(struct QH, qh, 1));
571 invalidate_dcache_range((unsigned long)qtd,
572 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
574 token = hc32_to_cpu(vtd->qt_token);
575 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
578 } while (get_timer(ts) < timeout);
581 * Invalidate the memory area occupied by buffer
582 * Don't try to fix the buffer alignment, if it isn't properly
583 * aligned it's upper layer's fault so let invalidate_dcache_range()
584 * vow about it. But we have to fix the length as it's actual
585 * transfer length and can be unaligned. This is potentially
586 * dangerous operation, it's responsibility of the calling
587 * code to make sure enough space is reserved.
589 invalidate_dcache_range((unsigned long)buffer,
590 ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
592 /* Check that the TD processing happened */
593 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
594 printf("EHCI timed out on TD - token=%#x\n", token);
596 /* Disable async schedule. */
597 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
599 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
601 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
604 printf("EHCI fail timeout STS_ASS reset\n");
608 token = hc32_to_cpu(qh->qh_overlay.qt_token);
609 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
610 debug("TOKEN=%#x\n", token);
611 switch (QT_TOKEN_GET_STATUS(token) &
612 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
614 toggle = QT_TOKEN_GET_DT(token);
615 usb_settoggle(dev, usb_pipeendpoint(pipe),
616 usb_pipeout(pipe), toggle);
619 case QT_TOKEN_STATUS_HALTED:
620 dev->status = USB_ST_STALLED;
622 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
623 case QT_TOKEN_STATUS_DATBUFERR:
624 dev->status = USB_ST_BUF_ERR;
626 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
627 case QT_TOKEN_STATUS_BABBLEDET:
628 dev->status = USB_ST_BABBLE_DET;
631 dev->status = USB_ST_CRC_ERR;
632 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
633 dev->status |= USB_ST_STALLED;
636 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
639 #ifndef CONFIG_USB_EHCI_FARADAY
640 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
641 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
642 ehci_readl(&ctrl->hcor->or_portsc[0]),
643 ehci_readl(&ctrl->hcor->or_portsc[1]));
648 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
655 static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
656 void *buffer, int length, struct devrequest *req)
663 uint32_t *status_reg;
664 int port = le16_to_cpu(req->index) & 0xff;
665 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
669 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
670 req->request, req->request,
671 req->requesttype, req->requesttype,
672 le16_to_cpu(req->value), le16_to_cpu(req->index));
674 typeReq = req->request | req->requesttype << 8;
677 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
678 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
679 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
680 status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
690 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
691 switch (le16_to_cpu(req->value) >> 8) {
693 debug("USB_DT_DEVICE request\n");
694 srcptr = &descriptor.device;
695 srclen = descriptor.device.bLength;
698 debug("USB_DT_CONFIG config\n");
699 srcptr = &descriptor.config;
700 srclen = descriptor.config.bLength +
701 descriptor.interface.bLength +
702 descriptor.endpoint.bLength;
705 debug("USB_DT_STRING config\n");
706 switch (le16_to_cpu(req->value) & 0xff) {
707 case 0: /* Language */
712 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
715 case 2: /* Product */
716 srcptr = "\52\3E\0H\0C\0I\0 "
718 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
722 debug("unknown value DT_STRING %x\n",
723 le16_to_cpu(req->value));
728 debug("unknown value %x\n", le16_to_cpu(req->value));
732 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
733 switch (le16_to_cpu(req->value) >> 8) {
735 debug("USB_DT_HUB config\n");
736 srcptr = &descriptor.hub;
737 srclen = descriptor.hub.bLength;
740 debug("unknown value %x\n", le16_to_cpu(req->value));
744 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
745 debug("USB_REQ_SET_ADDRESS\n");
746 ctrl->rootdev = le16_to_cpu(req->value);
748 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
749 debug("USB_REQ_SET_CONFIGURATION\n");
752 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
753 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
758 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
759 memset(tmpbuf, 0, 4);
760 reg = ehci_readl(status_reg);
761 if (reg & EHCI_PS_CS)
762 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
763 if (reg & EHCI_PS_PE)
764 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
765 if (reg & EHCI_PS_SUSP)
766 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
767 if (reg & EHCI_PS_OCA)
768 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
769 if (reg & EHCI_PS_PR)
770 tmpbuf[0] |= USB_PORT_STAT_RESET;
771 if (reg & EHCI_PS_PP)
772 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
775 switch (ctrl->ops.get_port_speed(ctrl, reg)) {
779 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
783 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
787 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
790 if (reg & EHCI_PS_CSC)
791 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
792 if (reg & EHCI_PS_PEC)
793 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
794 if (reg & EHCI_PS_OCC)
795 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
796 if (ctrl->portreset & (1 << port))
797 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
802 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
803 reg = ehci_readl(status_reg);
804 reg &= ~EHCI_PS_CLEAR;
805 switch (le16_to_cpu(req->value)) {
806 case USB_PORT_FEAT_ENABLE:
808 ehci_writel(status_reg, reg);
810 case USB_PORT_FEAT_POWER:
811 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
813 ehci_writel(status_reg, reg);
816 case USB_PORT_FEAT_RESET:
817 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
819 EHCI_PS_IS_LOWSPEED(reg)) {
820 /* Low speed device, give up ownership. */
821 debug("port %d low speed --> companion\n",
824 ehci_writel(status_reg, reg);
831 ehci_writel(status_reg, reg);
833 * caller must wait, then call GetPortStatus
834 * usb 2.0 specification say 50 ms resets on
837 ctrl->ops.powerup_fixup(ctrl, status_reg, ®);
839 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
841 * A host controller must terminate the reset
842 * and stabilize the state of the port within
845 ret = handshake(status_reg, EHCI_PS_PR, 0,
848 reg = ehci_readl(status_reg);
849 if ((reg & (EHCI_PS_PE | EHCI_PS_CS))
850 == EHCI_PS_CS && !ehci_is_TDI()) {
851 debug("port %d full speed --> companion\n", port - 1);
852 reg &= ~EHCI_PS_CLEAR;
854 ehci_writel(status_reg, reg);
857 ctrl->portreset |= 1 << port;
860 printf("port(%d) reset error\n",
865 case USB_PORT_FEAT_TEST:
868 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
869 ehci_writel(status_reg, reg);
872 debug("unknown feature %x\n", le16_to_cpu(req->value));
875 /* unblock posted writes */
876 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
878 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
879 reg = ehci_readl(status_reg);
880 reg &= ~EHCI_PS_CLEAR;
881 switch (le16_to_cpu(req->value)) {
882 case USB_PORT_FEAT_ENABLE:
885 case USB_PORT_FEAT_C_ENABLE:
888 case USB_PORT_FEAT_POWER:
889 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
892 case USB_PORT_FEAT_C_CONNECTION:
895 case USB_PORT_FEAT_OVER_CURRENT:
898 case USB_PORT_FEAT_C_RESET:
899 ctrl->portreset &= ~(1 << port);
902 debug("unknown feature %x\n", le16_to_cpu(req->value));
905 ehci_writel(status_reg, reg);
906 /* unblock posted write */
907 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
910 debug("Unknown request\n");
915 len = min3(srclen, (int)le16_to_cpu(req->length), length);
916 if (srcptr != NULL && len > 0)
917 memcpy(buffer, srcptr, len);
926 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
927 req->requesttype, req->request, le16_to_cpu(req->value),
928 le16_to_cpu(req->index), le16_to_cpu(req->length));
931 dev->status = USB_ST_STALLED;
935 const struct ehci_ops default_ehci_ops = {
936 .set_usb_mode = ehci_set_usbmode,
937 .get_port_speed = ehci_get_port_speed,
938 .powerup_fixup = ehci_powerup_fixup,
939 .get_portsc_register = ehci_get_portsc_register,
942 static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
945 ctrl->ops = default_ehci_ops;
948 if (!ctrl->ops.set_usb_mode)
949 ctrl->ops.set_usb_mode = ehci_set_usbmode;
950 if (!ctrl->ops.get_port_speed)
951 ctrl->ops.get_port_speed = ehci_get_port_speed;
952 if (!ctrl->ops.powerup_fixup)
953 ctrl->ops.powerup_fixup = ehci_powerup_fixup;
954 if (!ctrl->ops.get_portsc_register)
955 ctrl->ops.get_portsc_register =
956 ehci_get_portsc_register;
960 #ifndef CONFIG_DM_USB
961 void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
963 struct ehci_ctrl *ctrl = &ehcic[index];
966 ehci_setup_ops(ctrl, ops);
969 void *ehci_get_controller_priv(int index)
971 return ehcic[index].priv;
975 static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
983 /* Set the high address word (aka segment) for 64-bit controller */
984 if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
985 ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
987 qh_list = &ctrl->qh_list;
989 /* Set head of reclaim list */
990 memset(qh_list, 0, sizeof(*qh_list));
991 qh_list->qh_link = cpu_to_hc32(virt_to_phys(qh_list) | QH_LINK_TYPE_QH);
992 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
993 QH_ENDPT1_EPS(USB_SPEED_HIGH));
994 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
995 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
996 qh_list->qh_overlay.qt_token =
997 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
999 flush_dcache_range((unsigned long)qh_list,
1000 ALIGN_END_ADDR(struct QH, qh_list, 1));
1002 /* Set async. queue head pointer. */
1003 ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(qh_list));
1006 * Set up periodic list
1007 * Step 1: Parent QH for all periodic transfers.
1009 ctrl->periodic_schedules = 0;
1010 periodic = &ctrl->periodic_queue;
1011 memset(periodic, 0, sizeof(*periodic));
1012 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
1013 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1014 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1016 flush_dcache_range((unsigned long)periodic,
1017 ALIGN_END_ADDR(struct QH, periodic, 1));
1020 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
1021 * In particular, device specifications on polling frequency
1022 * are disregarded. Keyboards seem to send NAK/NYet reliably
1023 * when polled with an empty buffer.
1025 * Split Transactions will be spread across microframes using
1026 * S-mask and C-mask.
1028 if (ctrl->periodic_list == NULL)
1029 ctrl->periodic_list = memalign(4096, 1024 * 4);
1031 if (!ctrl->periodic_list)
1033 for (i = 0; i < 1024; i++) {
1034 ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
1038 flush_dcache_range((unsigned long)ctrl->periodic_list,
1039 ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
1042 /* Set periodic list base address */
1043 ehci_writel(&ctrl->hcor->or_periodiclistbase,
1044 (unsigned long)ctrl->periodic_list);
1046 reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
1047 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
1048 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
1049 /* Port Indicators */
1050 if (HCS_INDICATOR(reg))
1051 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1052 | 0x80, &descriptor.hub.wHubCharacteristics);
1053 /* Port Power Control */
1055 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1056 | 0x01, &descriptor.hub.wHubCharacteristics);
1058 /* Start the host controller. */
1059 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
1061 * Philips, Intel, and maybe others need CMD_RUN before the
1062 * root hub will detect new devices (why?); NEC doesn't
1064 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
1066 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
1068 if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
1069 /* take control over the ports */
1070 cmd = ehci_readl(&ctrl->hcor->or_configflag);
1072 ehci_writel(&ctrl->hcor->or_configflag, cmd);
1075 /* unblock posted write */
1076 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
1078 reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
1079 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
1084 #ifndef CONFIG_DM_USB
1085 int usb_lowlevel_stop(int index)
1087 ehci_shutdown(&ehcic[index]);
1088 return ehci_hcd_stop(index);
1091 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1093 struct ehci_ctrl *ctrl = &ehcic[index];
1098 * Set ops to default_ehci_ops, ehci_hcd_init should call
1099 * ehci_set_controller_priv to change any of these function pointers.
1101 ctrl->ops = default_ehci_ops;
1103 rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
1106 if (init == USB_INIT_DEVICE)
1109 /* EHCI spec section 4.1 */
1110 if (ehci_reset(ctrl))
1113 #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
1114 rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
1118 #ifdef CONFIG_USB_EHCI_FARADAY
1119 tweaks |= EHCI_TWEAK_NO_INIT_CF;
1121 rc = ehci_common_init(ctrl, tweaks);
1127 *controller = &ehcic[index];
1132 static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
1133 void *buffer, int length)
1136 if (usb_pipetype(pipe) != PIPE_BULK) {
1137 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1140 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1143 static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
1144 void *buffer, int length,
1145 struct devrequest *setup)
1147 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
1149 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1150 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1154 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1156 dev->speed = USB_SPEED_HIGH;
1157 return ehci_submit_root(dev, pipe, buffer, length, setup);
1159 return ehci_submit_async(dev, pipe, buffer, length, setup);
1171 #define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
1174 enable_periodic(struct ehci_ctrl *ctrl)
1177 struct ehci_hcor *hcor = ctrl->hcor;
1180 cmd = ehci_readl(&hcor->or_usbcmd);
1182 ehci_writel(&hcor->or_usbcmd, cmd);
1184 ret = handshake((uint32_t *)&hcor->or_usbsts,
1185 STS_PSS, STS_PSS, 100 * 1000);
1187 printf("EHCI failed: timeout when enabling periodic list\n");
1195 disable_periodic(struct ehci_ctrl *ctrl)
1198 struct ehci_hcor *hcor = ctrl->hcor;
1201 cmd = ehci_readl(&hcor->or_usbcmd);
1203 ehci_writel(&hcor->or_usbcmd, cmd);
1205 ret = handshake((uint32_t *)&hcor->or_usbsts,
1206 STS_PSS, 0, 100 * 1000);
1208 printf("EHCI failed: timeout when disabling periodic list\n");
1214 static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
1215 unsigned long pipe, int queuesize, int elementsize,
1216 void *buffer, int interval)
1218 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
1219 struct int_queue *result = NULL;
1223 * Interrupt transfers requiring several transactions are not supported
1224 * because bInterval is ignored.
1226 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1227 * <= PKT_ALIGN if several qTDs are required, while the USB
1228 * specification does not constrain this for interrupt transfers. That
1229 * means that ehci_submit_async() would support interrupt transfers
1230 * requiring several transactions only as long as the transfer size does
1231 * not require more than a single qTD.
1233 if (elementsize > usb_maxpacket(dev, pipe)) {
1234 printf("%s: xfers requiring several transactions are not supported.\n",
1239 debug("Enter create_int_queue\n");
1240 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1241 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1245 /* limit to 4 full pages worth of data -
1246 * we can safely fit them in a single TD,
1247 * no matter the alignment
1249 if (elementsize >= 16384) {
1250 debug("too large elements for interrupt transfers\n");
1254 result = malloc(sizeof(*result));
1256 debug("ehci intr queue: out of memory\n");
1259 result->elementsize = elementsize;
1260 result->pipe = pipe;
1261 result->first = memalign(USB_DMA_MINALIGN,
1262 sizeof(struct QH) * queuesize);
1263 if (!result->first) {
1264 debug("ehci intr queue: out of memory\n");
1267 result->current = result->first;
1268 result->last = result->first + queuesize - 1;
1269 result->tds = memalign(USB_DMA_MINALIGN,
1270 sizeof(struct qTD) * queuesize);
1272 debug("ehci intr queue: out of memory\n");
1275 memset(result->first, 0, sizeof(struct QH) * queuesize);
1276 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1278 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
1280 for (i = 0; i < queuesize; i++) {
1281 struct QH *qh = result->first + i;
1282 struct qTD *td = result->tds + i;
1283 void **buf = &qh->buffer;
1285 qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
1286 if (i == queuesize - 1)
1287 qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
1289 qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
1290 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1292 cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
1293 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1295 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1296 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
1297 (usb_pipedevice(pipe) << 0));
1298 qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1299 (1 << 0)); /* S-mask: microframe 0 */
1300 if (dev->speed == USB_SPEED_LOW ||
1301 dev->speed == USB_SPEED_FULL) {
1302 /* C-mask: microframes 2-4 */
1303 qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
1305 ehci_update_endpt2_dev_n_port(dev, qh);
1307 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1308 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1309 debug("communication direction is '%s'\n",
1310 usb_pipein(pipe) ? "in" : "out");
1311 td->qt_token = cpu_to_hc32(
1312 QT_TOKEN_DT(toggle) |
1313 (elementsize << 16) |
1314 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
1317 cpu_to_hc32((unsigned long)buffer + i * elementsize);
1319 cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1321 cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1323 cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1325 cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
1327 *buf = buffer + i * elementsize;
1331 flush_dcache_range((unsigned long)buffer,
1332 ALIGN_END_ADDR(char, buffer,
1333 queuesize * elementsize));
1334 flush_dcache_range((unsigned long)result->first,
1335 ALIGN_END_ADDR(struct QH, result->first,
1337 flush_dcache_range((unsigned long)result->tds,
1338 ALIGN_END_ADDR(struct qTD, result->tds,
1341 if (ctrl->periodic_schedules > 0) {
1342 if (disable_periodic(ctrl) < 0) {
1343 debug("FATAL: periodic should never fail, but did");
1348 /* hook up to periodic list */
1349 struct QH *list = &ctrl->periodic_queue;
1350 result->last->qh_link = list->qh_link;
1351 list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
1353 flush_dcache_range((unsigned long)result->last,
1354 ALIGN_END_ADDR(struct QH, result->last, 1));
1355 flush_dcache_range((unsigned long)list,
1356 ALIGN_END_ADDR(struct QH, list, 1));
1358 if (enable_periodic(ctrl) < 0) {
1359 debug("FATAL: periodic should never fail, but did");
1362 ctrl->periodic_schedules++;
1364 debug("Exit create_int_queue\n");
1371 free(result->first);
1378 static void *_ehci_poll_int_queue(struct usb_device *dev,
1379 struct int_queue *queue)
1381 struct QH *cur = queue->current;
1383 uint32_t token, toggle;
1384 unsigned long pipe = queue->pipe;
1386 /* depleted queue */
1388 debug("Exit poll_int_queue with completed queue\n");
1392 cur_td = &queue->tds[queue->current - queue->first];
1393 invalidate_dcache_range((unsigned long)cur_td,
1394 ALIGN_END_ADDR(struct qTD, cur_td, 1));
1395 token = hc32_to_cpu(cur_td->qt_token);
1396 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) {
1397 debug("Exit poll_int_queue with no completed intr transfer. token is %x\n", token);
1401 toggle = QT_TOKEN_GET_DT(token);
1402 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle);
1404 if (!(cur->qh_link & QH_LINK_TERMINATE))
1407 queue->current = NULL;
1409 invalidate_dcache_range((unsigned long)cur->buffer,
1410 ALIGN_END_ADDR(char, cur->buffer,
1411 queue->elementsize));
1413 debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
1414 token, cur, queue->first);
1418 /* Do not free buffers associated with QHs, they're owned by someone else */
1419 static int _ehci_destroy_int_queue(struct usb_device *dev,
1420 struct int_queue *queue)
1422 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
1424 unsigned long timeout;
1426 if (disable_periodic(ctrl) < 0) {
1427 debug("FATAL: periodic should never fail, but did");
1430 ctrl->periodic_schedules--;
1432 struct QH *cur = &ctrl->periodic_queue;
1433 timeout = get_timer(0) + 500; /* abort after 500ms */
1434 while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
1435 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1436 if (NEXT_QH(cur) == queue->first) {
1437 debug("found candidate. removing from chain\n");
1438 cur->qh_link = queue->last->qh_link;
1439 flush_dcache_range((unsigned long)cur,
1440 ALIGN_END_ADDR(struct QH, cur, 1));
1445 if (get_timer(0) > timeout) {
1446 printf("Timeout destroying interrupt endpoint queue\n");
1452 if (ctrl->periodic_schedules > 0) {
1453 result = enable_periodic(ctrl);
1455 debug("FATAL: periodic should never fail, but did");
1466 static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe,
1467 void *buffer, int length, int interval)
1470 struct int_queue *queue;
1471 unsigned long timeout;
1472 int result = 0, ret;
1474 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1475 dev, pipe, buffer, length, interval);
1477 queue = _ehci_create_int_queue(dev, pipe, 1, length, buffer, interval);
1481 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1482 while ((backbuffer = _ehci_poll_int_queue(dev, queue)) == NULL)
1483 if (get_timer(0) > timeout) {
1484 printf("Timeout poll on interrupt endpoint\n");
1485 result = -ETIMEDOUT;
1489 if (backbuffer != buffer) {
1490 debug("got wrong buffer back (%p instead of %p)\n",
1491 backbuffer, buffer);
1495 ret = _ehci_destroy_int_queue(dev, queue);
1499 /* everything worked out fine */
1503 #ifndef CONFIG_DM_USB
1504 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
1505 void *buffer, int length)
1507 return _ehci_submit_bulk_msg(dev, pipe, buffer, length);
1510 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1511 int length, struct devrequest *setup)
1513 return _ehci_submit_control_msg(dev, pipe, buffer, length, setup);
1516 int submit_int_msg(struct usb_device *dev, unsigned long pipe,
1517 void *buffer, int length, int interval)
1519 return _ehci_submit_int_msg(dev, pipe, buffer, length, interval);
1522 struct int_queue *create_int_queue(struct usb_device *dev,
1523 unsigned long pipe, int queuesize, int elementsize,
1524 void *buffer, int interval)
1526 return _ehci_create_int_queue(dev, pipe, queuesize, elementsize,
1530 void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1532 return _ehci_poll_int_queue(dev, queue);
1535 int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1537 return _ehci_destroy_int_queue(dev, queue);
1541 #ifdef CONFIG_DM_USB
1542 static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1543 unsigned long pipe, void *buffer, int length,
1544 struct devrequest *setup)
1546 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1547 dev->name, udev, udev->dev->name, udev->portnr);
1549 return _ehci_submit_control_msg(udev, pipe, buffer, length, setup);
1552 static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1553 unsigned long pipe, void *buffer, int length)
1555 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1556 return _ehci_submit_bulk_msg(udev, pipe, buffer, length);
1559 static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1560 unsigned long pipe, void *buffer, int length,
1563 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1564 return _ehci_submit_int_msg(udev, pipe, buffer, length, interval);
1567 static struct int_queue *ehci_create_int_queue(struct udevice *dev,
1568 struct usb_device *udev, unsigned long pipe, int queuesize,
1569 int elementsize, void *buffer, int interval)
1571 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1572 return _ehci_create_int_queue(udev, pipe, queuesize, elementsize,
1576 static void *ehci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
1577 struct int_queue *queue)
1579 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1580 return _ehci_poll_int_queue(udev, queue);
1583 static int ehci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
1584 struct int_queue *queue)
1586 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1587 return _ehci_destroy_int_queue(udev, queue);
1590 int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
1591 struct ehci_hcor *hcor, const struct ehci_ops *ops,
1592 uint tweaks, enum usb_init_type init)
1594 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
1595 struct ehci_ctrl *ctrl = dev_get_priv(dev);
1598 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
1599 dev->name, ctrl, hccr, hcor, init);
1601 priv->desc_before_addr = true;
1603 ehci_setup_ops(ctrl, ops);
1609 if (ctrl->init == USB_INIT_DEVICE)
1612 ret = ehci_reset(ctrl);
1616 ret = ehci_common_init(ctrl, tweaks);
1623 debug("%s: failed, ret=%d\n", __func__, ret);
1627 int ehci_deregister(struct udevice *dev)
1629 struct ehci_ctrl *ctrl = dev_get_priv(dev);
1631 if (ctrl->init == USB_INIT_DEVICE)
1634 ehci_shutdown(ctrl);
1639 struct dm_usb_ops ehci_usb_ops = {
1640 .control = ehci_submit_control_msg,
1641 .bulk = ehci_submit_bulk_msg,
1642 .interrupt = ehci_submit_int_msg,
1643 .create_int_queue = ehci_create_int_queue,
1644 .poll_int_queue = ehci_poll_int_queue,
1645 .destroy_int_queue = ehci_destroy_int_queue,