Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2007-2008, Juniper Networks, Inc. |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 3 | * Copyright (c) 2008, Excito Elektronik i Skåne AB |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 4 | * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it> |
| 5 | * |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
Simon Glass | 1b2e365 | 2015-07-06 16:47:42 -0600 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0 |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 9 | */ |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 12 | #include <errno.h> |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 13 | #include <asm/byteorder.h> |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 14 | #include <asm/unaligned.h> |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 15 | #include <usb.h> |
| 16 | #include <asm/io.h> |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 17 | #include <malloc.h> |
Stefan Roese | 86b34cf | 2010-11-26 15:43:28 +0100 | [diff] [blame] | 18 | #include <watchdog.h> |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 19 | #include <linux/compiler.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8f6bcf4 | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 20 | |
| 21 | #include "ehci.h" |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 22 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 23 | #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT |
| 24 | #define CONFIG_USB_MAX_CONTROLLER_COUNT 1 |
| 25 | #endif |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 26 | |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 27 | /* |
| 28 | * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt. |
| 29 | * Let's time out after 8 to have a little safety margin on top of that. |
| 30 | */ |
| 31 | #define HCHALT_TIMEOUT (8 * 1000) |
| 32 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 33 | #ifndef CONFIG_DM_USB |
Marek Vasut | fd349a1 | 2013-07-10 03:16:31 +0200 | [diff] [blame] | 34 | static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT]; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 35 | #endif |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 36 | |
| 37 | #define ALIGN_END_ADDR(type, ptr, size) \ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 38 | ((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN)) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 39 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 40 | static struct descriptor { |
| 41 | struct usb_hub_descriptor hub; |
| 42 | struct usb_device_descriptor device; |
| 43 | struct usb_linux_config_descriptor config; |
| 44 | struct usb_linux_interface_descriptor interface; |
| 45 | struct usb_endpoint_descriptor endpoint; |
| 46 | } __attribute__ ((packed)) descriptor = { |
| 47 | { |
| 48 | 0x8, /* bDescLength */ |
| 49 | 0x29, /* bDescriptorType: hub descriptor */ |
| 50 | 2, /* bNrPorts -- runtime modified */ |
| 51 | 0, /* wHubCharacteristics */ |
Vincent Palatin | 8277b50 | 2011-12-05 14:52:22 -0800 | [diff] [blame] | 52 | 10, /* bPwrOn2PwrGood */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 53 | 0, /* bHubCntrCurrent */ |
| 54 | {}, /* Device removable */ |
| 55 | {} /* at most 7 ports! XXX */ |
| 56 | }, |
| 57 | { |
| 58 | 0x12, /* bLength */ |
| 59 | 1, /* bDescriptorType: UDESC_DEVICE */ |
Sergei Shtylyov | fa30a27 | 2010-02-27 21:29:42 +0300 | [diff] [blame] | 60 | cpu_to_le16(0x0200), /* bcdUSB: v2.0 */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 61 | 9, /* bDeviceClass: UDCLASS_HUB */ |
| 62 | 0, /* bDeviceSubClass: UDSUBCLASS_HUB */ |
| 63 | 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */ |
| 64 | 64, /* bMaxPacketSize: 64 bytes */ |
| 65 | 0x0000, /* idVendor */ |
| 66 | 0x0000, /* idProduct */ |
Sergei Shtylyov | fa30a27 | 2010-02-27 21:29:42 +0300 | [diff] [blame] | 67 | cpu_to_le16(0x0100), /* bcdDevice */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 68 | 1, /* iManufacturer */ |
| 69 | 2, /* iProduct */ |
| 70 | 0, /* iSerialNumber */ |
| 71 | 1 /* bNumConfigurations: 1 */ |
| 72 | }, |
| 73 | { |
| 74 | 0x9, |
| 75 | 2, /* bDescriptorType: UDESC_CONFIG */ |
| 76 | cpu_to_le16(0x19), |
| 77 | 1, /* bNumInterface */ |
| 78 | 1, /* bConfigurationValue */ |
| 79 | 0, /* iConfiguration */ |
| 80 | 0x40, /* bmAttributes: UC_SELF_POWER */ |
| 81 | 0 /* bMaxPower */ |
| 82 | }, |
| 83 | { |
| 84 | 0x9, /* bLength */ |
| 85 | 4, /* bDescriptorType: UDESC_INTERFACE */ |
| 86 | 0, /* bInterfaceNumber */ |
| 87 | 0, /* bAlternateSetting */ |
| 88 | 1, /* bNumEndpoints */ |
| 89 | 9, /* bInterfaceClass: UICLASS_HUB */ |
| 90 | 0, /* bInterfaceSubClass: UISUBCLASS_HUB */ |
| 91 | 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */ |
| 92 | 0 /* iInterface */ |
| 93 | }, |
| 94 | { |
| 95 | 0x7, /* bLength */ |
| 96 | 5, /* bDescriptorType: UDESC_ENDPOINT */ |
| 97 | 0x81, /* bEndpointAddress: |
| 98 | * UE_DIR_IN | EHCI_INTR_ENDPT |
| 99 | */ |
| 100 | 3, /* bmAttributes: UE_INTERRUPT */ |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 101 | 8, /* wMaxPacketSize */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 102 | 255 /* bInterval */ |
| 103 | }, |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 106 | #if defined(CONFIG_EHCI_IS_TDI) |
| 107 | #define ehci_is_TDI() (1) |
| 108 | #else |
| 109 | #define ehci_is_TDI() (0) |
| 110 | #endif |
| 111 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 112 | static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev) |
| 113 | { |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 114 | #ifdef CONFIG_DM_USB |
Hans de Goede | 6be39d1 | 2015-05-05 11:54:33 +0200 | [diff] [blame] | 115 | return dev_get_priv(usb_get_bus(udev->dev)); |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 116 | #else |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 117 | return udev->controller; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 118 | #endif |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 119 | } |
| 120 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 121 | static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg) |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 122 | { |
| 123 | return PORTSC_PSPD(reg); |
| 124 | } |
| 125 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 126 | static void ehci_set_usbmode(struct ehci_ctrl *ctrl) |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 127 | { |
| 128 | uint32_t tmp; |
| 129 | uint32_t *reg_ptr; |
| 130 | |
Simon Glass | 2d387ab | 2015-03-25 12:22:23 -0600 | [diff] [blame] | 131 | reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE); |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 132 | tmp = ehci_readl(reg_ptr); |
| 133 | tmp |= USBMODE_CM_HC; |
| 134 | #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN) |
| 135 | tmp |= USBMODE_BE; |
| 136 | #endif |
| 137 | ehci_writel(reg_ptr, tmp); |
| 138 | } |
| 139 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 140 | static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg, |
Simon Glass | cc0dc6b | 2015-03-25 12:22:21 -0600 | [diff] [blame] | 141 | uint32_t *reg) |
Marek Vasut | 0973477 | 2011-07-11 02:37:01 +0200 | [diff] [blame] | 142 | { |
| 143 | mdelay(50); |
| 144 | } |
| 145 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 146 | static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port) |
Simon Glass | 0bec128 | 2015-03-25 12:22:17 -0600 | [diff] [blame] | 147 | { |
| 148 | if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) { |
| 149 | /* Printing the message would cause a scan failure! */ |
| 150 | debug("The request port(%u) is not configured\n", port); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Simon Glass | dfbf186 | 2015-03-25 12:22:24 -0600 | [diff] [blame] | 154 | return (uint32_t *)&ctrl->hcor->or_portsc[port]; |
Simon Glass | 0bec128 | 2015-03-25 12:22:17 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 157 | static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec) |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 158 | { |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 159 | uint32_t result; |
| 160 | do { |
| 161 | result = ehci_readl(ptr); |
Wolfgang Denk | cdc5a7a | 2010-10-22 14:23:00 +0200 | [diff] [blame] | 162 | udelay(5); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 163 | if (result == ~(uint32_t)0) |
| 164 | return -1; |
| 165 | result &= mask; |
| 166 | if (result == done) |
| 167 | return 0; |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 168 | usec--; |
| 169 | } while (usec > 0); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 170 | return -1; |
| 171 | } |
| 172 | |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 173 | static int ehci_reset(struct ehci_ctrl *ctrl) |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 174 | { |
| 175 | uint32_t cmd; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 176 | int ret = 0; |
| 177 | |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 178 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
Stefan Roese | 745af44 | 2010-11-26 15:44:00 +0100 | [diff] [blame] | 179 | cmd = (cmd & ~CMD_RUN) | CMD_RESET; |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 180 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
| 181 | ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd, |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 182 | CMD_RESET, 0, 250 * 1000); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 183 | if (ret < 0) { |
| 184 | printf("EHCI fail to reset\n"); |
| 185 | goto out; |
| 186 | } |
| 187 | |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 188 | if (ehci_is_TDI()) |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 189 | ctrl->ops.set_usb_mode(ctrl); |
Simon Glass | 5978cdb | 2012-02-27 10:52:47 +0000 | [diff] [blame] | 190 | |
| 191 | #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 192 | cmd = ehci_readl(&ctrl->hcor->or_txfilltuning); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 193 | cmd &= ~TXFIFO_THRESH_MASK; |
Simon Glass | 5978cdb | 2012-02-27 10:52:47 +0000 | [diff] [blame] | 194 | cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH); |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 195 | ehci_writel(&ctrl->hcor->or_txfilltuning, cmd); |
Simon Glass | 5978cdb | 2012-02-27 10:52:47 +0000 | [diff] [blame] | 196 | #endif |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 197 | out: |
| 198 | return ret; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 199 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 200 | |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 201 | static int ehci_shutdown(struct ehci_ctrl *ctrl) |
| 202 | { |
| 203 | int i, ret = 0; |
| 204 | uint32_t cmd, reg; |
| 205 | |
Marek Vasut | 919d00a | 2013-12-14 02:03:11 +0100 | [diff] [blame] | 206 | if (!ctrl || !ctrl->hcor) |
| 207 | return -EINVAL; |
| 208 | |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 209 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
| 210 | cmd &= ~(CMD_PSE | CMD_ASE); |
| 211 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
| 212 | ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0, |
| 213 | 100 * 1000); |
| 214 | |
| 215 | if (!ret) { |
| 216 | for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) { |
| 217 | reg = ehci_readl(&ctrl->hcor->or_portsc[i]); |
| 218 | reg |= EHCI_PS_SUSP; |
| 219 | ehci_writel(&ctrl->hcor->or_portsc[i], reg); |
| 220 | } |
| 221 | |
| 222 | cmd &= ~CMD_RUN; |
| 223 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
| 224 | ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT, |
| 225 | HCHALT_TIMEOUT); |
| 226 | } |
| 227 | |
| 228 | if (ret) |
| 229 | puts("EHCI failed to shut down host controller.\n"); |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 234 | static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz) |
| 235 | { |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 236 | uint32_t delta, next; |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 237 | uint32_t addr = (unsigned long)buf; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 238 | int idx; |
| 239 | |
Ilya Yanok | fb11371 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 240 | if (addr != ALIGN(addr, ARCH_DMA_MINALIGN)) |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 241 | debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf); |
| 242 | |
Ilya Yanok | fb11371 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 243 | flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN)); |
| 244 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 245 | idx = 0; |
Benoît Thébaudeau | e68f48a | 2012-07-19 22:16:38 +0200 | [diff] [blame] | 246 | while (idx < QT_BUFFER_CNT) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 247 | td->qt_buffer[idx] = cpu_to_hc32(addr); |
Wolfgang Denk | ebb829f | 2010-10-19 16:13:15 +0200 | [diff] [blame] | 248 | td->qt_buffer_hi[idx] = 0; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 249 | next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 250 | delta = next - addr; |
| 251 | if (delta >= sz) |
| 252 | break; |
| 253 | sz -= delta; |
| 254 | addr = next; |
| 255 | idx++; |
| 256 | } |
| 257 | |
Benoît Thébaudeau | e68f48a | 2012-07-19 22:16:38 +0200 | [diff] [blame] | 258 | if (idx == QT_BUFFER_CNT) { |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 259 | printf("out of buffer pointers (%zu bytes left)\n", sz); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Ilya Yanok | a1cf10f | 2012-11-06 13:48:20 +0000 | [diff] [blame] | 266 | static inline u8 ehci_encode_speed(enum usb_device_speed speed) |
| 267 | { |
| 268 | #define QH_HIGH_SPEED 2 |
| 269 | #define QH_FULL_SPEED 0 |
| 270 | #define QH_LOW_SPEED 1 |
| 271 | if (speed == USB_SPEED_HIGH) |
| 272 | return QH_HIGH_SPEED; |
| 273 | if (speed == USB_SPEED_LOW) |
| 274 | return QH_LOW_SPEED; |
| 275 | return QH_FULL_SPEED; |
| 276 | } |
| 277 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 278 | static void ehci_update_endpt2_dev_n_port(struct usb_device *udev, |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 279 | struct QH *qh) |
| 280 | { |
| 281 | struct usb_device *ttdev; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 282 | int parent_devnum; |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 283 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 284 | if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL) |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 285 | return; |
| 286 | |
| 287 | /* |
| 288 | * For full / low speed devices we need to get the devnum and portnr of |
| 289 | * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs |
| 290 | * in the tree before that one! |
| 291 | */ |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 292 | #ifdef CONFIG_DM_USB |
Hans de Goede | d5ab160 | 2015-05-05 11:54:34 +0200 | [diff] [blame] | 293 | /* |
| 294 | * When called from usb-uclass.c: usb_scan_device() udev->dev points |
| 295 | * to the parent udevice, not the actual udevice belonging to the |
| 296 | * udev as the device is not instantiated yet. So when searching |
| 297 | * for the first usb-2 parent start with udev->dev not |
| 298 | * udev->dev->parent . |
| 299 | */ |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 300 | struct udevice *parent; |
Hans de Goede | d5ab160 | 2015-05-05 11:54:34 +0200 | [diff] [blame] | 301 | struct usb_device *uparent; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 302 | |
Hans de Goede | d5ab160 | 2015-05-05 11:54:34 +0200 | [diff] [blame] | 303 | ttdev = udev; |
| 304 | parent = udev->dev; |
| 305 | uparent = dev_get_parentdata(parent); |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 306 | |
Hans de Goede | d5ab160 | 2015-05-05 11:54:34 +0200 | [diff] [blame] | 307 | while (uparent->speed != USB_SPEED_HIGH) { |
| 308 | struct udevice *dev = parent; |
| 309 | |
| 310 | if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB) { |
Simon Glass | e855d26 | 2015-07-06 16:47:41 -0600 | [diff] [blame] | 311 | printf("ehci: Error cannot find high-speed parent of usb-1 device\n"); |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 312 | return; |
Hans de Goede | d5ab160 | 2015-05-05 11:54:34 +0200 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | ttdev = dev_get_parentdata(dev); |
| 316 | parent = dev->parent; |
| 317 | uparent = dev_get_parentdata(parent); |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 318 | } |
Hans de Goede | d5ab160 | 2015-05-05 11:54:34 +0200 | [diff] [blame] | 319 | parent_devnum = uparent->devnum; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 320 | #else |
| 321 | ttdev = udev; |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 322 | while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH) |
| 323 | ttdev = ttdev->parent; |
| 324 | if (!ttdev->parent) |
| 325 | return; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 326 | parent_devnum = ttdev->parent->devnum; |
| 327 | #endif |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 328 | |
| 329 | qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 330 | QH_ENDPT2_HUBADDR(parent_devnum)); |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 333 | static int |
| 334 | ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 335 | int length, struct devrequest *req) |
| 336 | { |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 337 | ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 338 | struct qTD *qtd; |
| 339 | int qtd_count = 0; |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 340 | int qtd_counter = 0; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 341 | volatile struct qTD *vtd; |
| 342 | unsigned long ts; |
| 343 | uint32_t *tdp; |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 344 | uint32_t endpt, maxpacket, token, usbsts; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 345 | uint32_t c, toggle; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 346 | uint32_t cmd; |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 347 | int timeout; |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 348 | int ret = 0; |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 349 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 350 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 351 | debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe, |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 352 | buffer, length, req); |
| 353 | if (req != NULL) |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 354 | debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 355 | req->request, req->request, |
| 356 | req->requesttype, req->requesttype, |
| 357 | le16_to_cpu(req->value), le16_to_cpu(req->value), |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 358 | le16_to_cpu(req->index)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 359 | |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 360 | #define PKT_ALIGN 512 |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 361 | /* |
| 362 | * The USB transfer is split into qTD transfers. Eeach qTD transfer is |
| 363 | * described by a transfer descriptor (the qTD). The qTDs form a linked |
| 364 | * list with a queue head (QH). |
| 365 | * |
| 366 | * Each qTD transfer starts with a new USB packet, i.e. a packet cannot |
| 367 | * have its beginning in a qTD transfer and its end in the following |
| 368 | * one, so the qTD transfer lengths have to be chosen accordingly. |
| 369 | * |
| 370 | * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to |
| 371 | * single pages. The first data buffer can start at any offset within a |
| 372 | * page (not considering the cache-line alignment issues), while the |
| 373 | * following buffers must be page-aligned. There is no alignment |
| 374 | * constraint on the size of a qTD transfer. |
| 375 | */ |
| 376 | if (req != NULL) |
| 377 | /* 1 qTD will be needed for SETUP, and 1 for ACK. */ |
| 378 | qtd_count += 1 + 1; |
| 379 | if (length > 0 || req == NULL) { |
| 380 | /* |
| 381 | * Determine the qTD transfer size that will be used for the |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 382 | * data payload (not considering the first qTD transfer, which |
| 383 | * may be longer or shorter, and the final one, which may be |
| 384 | * shorter). |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 385 | * |
| 386 | * In order to keep each packet within a qTD transfer, the qTD |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 387 | * transfer size is aligned to PKT_ALIGN, which is a multiple of |
| 388 | * wMaxPacketSize (except in some cases for interrupt transfers, |
| 389 | * see comment in submit_int_msg()). |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 390 | * |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 391 | * By default, i.e. if the input buffer is aligned to PKT_ALIGN, |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 392 | * QT_BUFFER_CNT full pages will be used. |
| 393 | */ |
| 394 | int xfr_sz = QT_BUFFER_CNT; |
| 395 | /* |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 396 | * However, if the input buffer is not aligned to PKT_ALIGN, the |
| 397 | * qTD transfer size will be one page shorter, and the first qTD |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 398 | * data buffer of each transfer will be page-unaligned. |
| 399 | */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 400 | if ((unsigned long)buffer & (PKT_ALIGN - 1)) |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 401 | xfr_sz--; |
| 402 | /* Convert the qTD transfer size to bytes. */ |
| 403 | xfr_sz *= EHCI_PAGE_SIZE; |
| 404 | /* |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 405 | * Approximate by excess the number of qTDs that will be |
| 406 | * required for the data payload. The exact formula is way more |
| 407 | * complicated and saves at most 2 qTDs, i.e. a total of 128 |
| 408 | * bytes. |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 409 | */ |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 410 | qtd_count += 2 + length / xfr_sz; |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 411 | } |
| 412 | /* |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 413 | * Threshold value based on the worst-case total size of the allocated qTDs for |
| 414 | * a mass-storage transfer of 65535 blocks of 512 bytes. |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 415 | */ |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 416 | #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024 |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 417 | #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI |
| 418 | #endif |
| 419 | qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD)); |
| 420 | if (qtd == NULL) { |
| 421 | printf("unable to allocate TDs\n"); |
| 422 | return -1; |
| 423 | } |
| 424 | |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 425 | memset(qh, 0, sizeof(struct QH)); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 426 | memset(qtd, 0, qtd_count * sizeof(*qtd)); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 427 | |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 428 | toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); |
| 429 | |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 430 | /* |
| 431 | * Setup QH (3.6 in ehci-r10.pdf) |
| 432 | * |
| 433 | * qh_link ................. 03-00 H |
| 434 | * qh_endpt1 ............... 07-04 H |
| 435 | * qh_endpt2 ............... 0B-08 H |
| 436 | * - qh_curtd |
| 437 | * qh_overlay.qt_next ...... 13-10 H |
| 438 | * - qh_overlay.qt_altnext |
| 439 | */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 440 | qh->qh_link = cpu_to_hc32((unsigned long)&ctrl->qh_list | QH_LINK_TYPE_QH); |
Ilya Yanok | a1cf10f | 2012-11-06 13:48:20 +0000 | [diff] [blame] | 441 | c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe); |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 442 | maxpacket = usb_maxpacket(dev, pipe); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 443 | endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) | |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 444 | QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) | |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 445 | QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) | |
Ilya Yanok | a1cf10f | 2012-11-06 13:48:20 +0000 | [diff] [blame] | 446 | QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) | |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 447 | QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) | |
| 448 | QH_ENDPT1_DEVADDR(usb_pipedevice(pipe)); |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 449 | qh->qh_endpt1 = cpu_to_hc32(endpt); |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 450 | endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0); |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 451 | qh->qh_endpt2 = cpu_to_hc32(endpt); |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 452 | ehci_update_endpt2_dev_n_port(dev, qh); |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 453 | qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
Stephen Warren | 1907e5a | 2014-02-07 09:53:50 -0700 | [diff] [blame] | 454 | qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 455 | |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 456 | tdp = &qh->qh_overlay.qt_next; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 457 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 458 | if (req != NULL) { |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 459 | /* |
| 460 | * Setup request qTD (3.5 in ehci-r10.pdf) |
| 461 | * |
| 462 | * qt_next ................ 03-00 H |
| 463 | * qt_altnext ............. 07-04 H |
| 464 | * qt_token ............... 0B-08 H |
| 465 | * |
| 466 | * [ buffer, buffer_hi ] loaded with "req". |
| 467 | */ |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 468 | qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 469 | qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 470 | token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) | |
| 471 | QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) | |
| 472 | QT_TOKEN_PID(QT_TOKEN_PID_SETUP) | |
| 473 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 474 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 475 | if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) { |
| 476 | printf("unable to construct SETUP TD\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 477 | goto fail; |
| 478 | } |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 479 | /* Update previous qTD! */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 480 | *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 481 | tdp = &qtd[qtd_counter++].qt_next; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 482 | toggle = 1; |
| 483 | } |
| 484 | |
| 485 | if (length > 0 || req == NULL) { |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 486 | uint8_t *buf_ptr = buffer; |
| 487 | int left_length = length; |
| 488 | |
| 489 | do { |
| 490 | /* |
| 491 | * Determine the size of this qTD transfer. By default, |
| 492 | * QT_BUFFER_CNT full pages can be used. |
| 493 | */ |
| 494 | int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE; |
| 495 | /* |
| 496 | * However, if the input buffer is not page-aligned, the |
| 497 | * portion of the first page before the buffer start |
| 498 | * offset within that page is unusable. |
| 499 | */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 500 | xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 501 | /* |
| 502 | * In order to keep each packet within a qTD transfer, |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 503 | * align the qTD transfer size to PKT_ALIGN. |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 504 | */ |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 505 | xfr_bytes &= ~(PKT_ALIGN - 1); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 506 | /* |
| 507 | * This transfer may be shorter than the available qTD |
| 508 | * transfer size that has just been computed. |
| 509 | */ |
| 510 | xfr_bytes = min(xfr_bytes, left_length); |
| 511 | |
| 512 | /* |
| 513 | * Setup request qTD (3.5 in ehci-r10.pdf) |
| 514 | * |
| 515 | * qt_next ................ 03-00 H |
| 516 | * qt_altnext ............. 07-04 H |
| 517 | * qt_token ............... 0B-08 H |
| 518 | * |
| 519 | * [ buffer, buffer_hi ] loaded with "buffer". |
| 520 | */ |
| 521 | qtd[qtd_counter].qt_next = |
| 522 | cpu_to_hc32(QT_NEXT_TERMINATE); |
| 523 | qtd[qtd_counter].qt_altnext = |
| 524 | cpu_to_hc32(QT_NEXT_TERMINATE); |
| 525 | token = QT_TOKEN_DT(toggle) | |
| 526 | QT_TOKEN_TOTALBYTES(xfr_bytes) | |
| 527 | QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) | |
| 528 | QT_TOKEN_CERR(3) | |
| 529 | QT_TOKEN_PID(usb_pipein(pipe) ? |
| 530 | QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) | |
| 531 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
| 532 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
| 533 | if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr, |
| 534 | xfr_bytes)) { |
| 535 | printf("unable to construct DATA TD\n"); |
| 536 | goto fail; |
| 537 | } |
| 538 | /* Update previous qTD! */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 539 | *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 540 | tdp = &qtd[qtd_counter++].qt_next; |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 541 | /* |
| 542 | * Data toggle has to be adjusted since the qTD transfer |
| 543 | * size is not always an even multiple of |
| 544 | * wMaxPacketSize. |
| 545 | */ |
| 546 | if ((xfr_bytes / maxpacket) & 1) |
| 547 | toggle ^= 1; |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 548 | buf_ptr += xfr_bytes; |
| 549 | left_length -= xfr_bytes; |
| 550 | } while (left_length > 0); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | if (req != NULL) { |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 554 | /* |
| 555 | * Setup request qTD (3.5 in ehci-r10.pdf) |
| 556 | * |
| 557 | * qt_next ................ 03-00 H |
| 558 | * qt_altnext ............. 07-04 H |
| 559 | * qt_token ............... 0B-08 H |
| 560 | */ |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 561 | qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 562 | qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 563 | token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) | |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 564 | QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) | |
| 565 | QT_TOKEN_PID(usb_pipein(pipe) ? |
| 566 | QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) | |
| 567 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 568 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 569 | /* Update previous qTD! */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 570 | *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 571 | tdp = &qtd[qtd_counter++].qt_next; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 574 | ctrl->qh_list.qh_link = cpu_to_hc32((unsigned long)qh | QH_LINK_TYPE_QH); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 575 | |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 576 | /* Flush dcache */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 577 | flush_dcache_range((unsigned long)&ctrl->qh_list, |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 578 | ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 579 | flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1)); |
| 580 | flush_dcache_range((unsigned long)qtd, |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 581 | ALIGN_END_ADDR(struct qTD, qtd, qtd_count)); |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 582 | |
Ilya Yanok | 84309bb | 2012-07-15 22:12:08 +0000 | [diff] [blame] | 583 | /* Set async. queue head pointer. */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 584 | ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)&ctrl->qh_list); |
Ilya Yanok | 84309bb | 2012-07-15 22:12:08 +0000 | [diff] [blame] | 585 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 586 | usbsts = ehci_readl(&ctrl->hcor->or_usbsts); |
| 587 | ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 588 | |
| 589 | /* Enable async. schedule. */ |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 590 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 591 | cmd |= CMD_ASE; |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 592 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 593 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 594 | ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS, |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 595 | 100 * 1000); |
| 596 | if (ret < 0) { |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 597 | printf("EHCI fail timeout STS_ASS set\n"); |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 598 | goto fail; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 599 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 600 | |
| 601 | /* Wait for TDs to be processed. */ |
| 602 | ts = get_timer(0); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 603 | vtd = &qtd[qtd_counter - 1]; |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 604 | timeout = USB_TIMEOUT_MS(pipe); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 605 | do { |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 606 | /* Invalidate dcache */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 607 | invalidate_dcache_range((unsigned long)&ctrl->qh_list, |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 608 | ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 609 | invalidate_dcache_range((unsigned long)qh, |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 610 | ALIGN_END_ADDR(struct QH, qh, 1)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 611 | invalidate_dcache_range((unsigned long)qtd, |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 612 | ALIGN_END_ADDR(struct qTD, qtd, qtd_count)); |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 613 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 614 | token = hc32_to_cpu(vtd->qt_token); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 615 | if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 616 | break; |
Stefan Roese | 86b34cf | 2010-11-26 15:43:28 +0100 | [diff] [blame] | 617 | WATCHDOG_RESET(); |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 618 | } while (get_timer(ts) < timeout); |
| 619 | |
Ilya Yanok | fb11371 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 620 | /* |
| 621 | * Invalidate the memory area occupied by buffer |
| 622 | * Don't try to fix the buffer alignment, if it isn't properly |
| 623 | * aligned it's upper layer's fault so let invalidate_dcache_range() |
| 624 | * vow about it. But we have to fix the length as it's actual |
| 625 | * transfer length and can be unaligned. This is potentially |
| 626 | * dangerous operation, it's responsibility of the calling |
| 627 | * code to make sure enough space is reserved. |
| 628 | */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 629 | invalidate_dcache_range((unsigned long)buffer, |
| 630 | ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN)); |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 631 | |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 632 | /* Check that the TD processing happened */ |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 633 | if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 634 | printf("EHCI timed out on TD - token=%#x\n", token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 635 | |
| 636 | /* Disable async schedule. */ |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 637 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 638 | cmd &= ~CMD_ASE; |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 639 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 640 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 641 | ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0, |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 642 | 100 * 1000); |
| 643 | if (ret < 0) { |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 644 | printf("EHCI fail timeout STS_ASS reset\n"); |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 645 | goto fail; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 646 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 647 | |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 648 | token = hc32_to_cpu(qh->qh_overlay.qt_token); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 649 | if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 650 | debug("TOKEN=%#x\n", token); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 651 | switch (QT_TOKEN_GET_STATUS(token) & |
| 652 | ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) { |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 653 | case 0: |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 654 | toggle = QT_TOKEN_GET_DT(token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 655 | usb_settoggle(dev, usb_pipeendpoint(pipe), |
| 656 | usb_pipeout(pipe), toggle); |
| 657 | dev->status = 0; |
| 658 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 659 | case QT_TOKEN_STATUS_HALTED: |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 660 | dev->status = USB_ST_STALLED; |
| 661 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 662 | case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR: |
| 663 | case QT_TOKEN_STATUS_DATBUFERR: |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 664 | dev->status = USB_ST_BUF_ERR; |
| 665 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 666 | case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET: |
| 667 | case QT_TOKEN_STATUS_BABBLEDET: |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 668 | dev->status = USB_ST_BABBLE_DET; |
| 669 | break; |
| 670 | default: |
| 671 | dev->status = USB_ST_CRC_ERR; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 672 | if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED) |
Anatolij Gustschin | e1e0931 | 2010-11-02 11:47:29 +0100 | [diff] [blame] | 673 | dev->status |= USB_ST_STALLED; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 674 | break; |
| 675 | } |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 676 | dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 677 | } else { |
| 678 | dev->act_len = 0; |
Kuo-Jung Su | b5d59de | 2013-05-15 15:29:23 +0800 | [diff] [blame] | 679 | #ifndef CONFIG_USB_EHCI_FARADAY |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 680 | debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n", |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 681 | dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts), |
| 682 | ehci_readl(&ctrl->hcor->or_portsc[0]), |
| 683 | ehci_readl(&ctrl->hcor->or_portsc[1])); |
Kuo-Jung Su | b5d59de | 2013-05-15 15:29:23 +0800 | [diff] [blame] | 684 | #endif |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 687 | free(qtd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 688 | return (dev->status != USB_ST_NOT_PROC) ? 0 : -1; |
| 689 | |
| 690 | fail: |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 691 | free(qtd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 692 | return -1; |
| 693 | } |
| 694 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 695 | static int ehci_submit_root(struct usb_device *dev, unsigned long pipe, |
| 696 | void *buffer, int length, struct devrequest *req) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 697 | { |
| 698 | uint8_t tmpbuf[4]; |
| 699 | u16 typeReq; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 700 | void *srcptr = NULL; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 701 | int len, srclen; |
| 702 | uint32_t reg; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 703 | uint32_t *status_reg; |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 704 | int port = le16_to_cpu(req->index) & 0xff; |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 705 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 706 | |
| 707 | srclen = 0; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 708 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 709 | debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 710 | req->request, req->request, |
| 711 | req->requesttype, req->requesttype, |
| 712 | le16_to_cpu(req->value), le16_to_cpu(req->index)); |
| 713 | |
Prafulla Wadaskar | 2281029 | 2009-07-17 19:56:30 +0530 | [diff] [blame] | 714 | typeReq = req->request | req->requesttype << 8; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 715 | |
Prafulla Wadaskar | 2281029 | 2009-07-17 19:56:30 +0530 | [diff] [blame] | 716 | switch (typeReq) { |
Kuo-Jung Su | 9930e9f | 2013-05-15 15:29:20 +0800 | [diff] [blame] | 717 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): |
| 718 | case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
| 719 | case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 720 | status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1); |
Kuo-Jung Su | 6a656df | 2013-05-15 15:29:21 +0800 | [diff] [blame] | 721 | if (!status_reg) |
Kuo-Jung Su | 9930e9f | 2013-05-15 15:29:20 +0800 | [diff] [blame] | 722 | return -1; |
Kuo-Jung Su | 9930e9f | 2013-05-15 15:29:20 +0800 | [diff] [blame] | 723 | break; |
| 724 | default: |
| 725 | status_reg = NULL; |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | switch (typeReq) { |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 730 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 731 | switch (le16_to_cpu(req->value) >> 8) { |
| 732 | case USB_DT_DEVICE: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 733 | debug("USB_DT_DEVICE request\n"); |
| 734 | srcptr = &descriptor.device; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 735 | srclen = descriptor.device.bLength; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 736 | break; |
| 737 | case USB_DT_CONFIG: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 738 | debug("USB_DT_CONFIG config\n"); |
| 739 | srcptr = &descriptor.config; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 740 | srclen = descriptor.config.bLength + |
| 741 | descriptor.interface.bLength + |
| 742 | descriptor.endpoint.bLength; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 743 | break; |
| 744 | case USB_DT_STRING: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 745 | debug("USB_DT_STRING config\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 746 | switch (le16_to_cpu(req->value) & 0xff) { |
| 747 | case 0: /* Language */ |
| 748 | srcptr = "\4\3\1\0"; |
| 749 | srclen = 4; |
| 750 | break; |
| 751 | case 1: /* Vendor */ |
| 752 | srcptr = "\16\3u\0-\0b\0o\0o\0t\0"; |
| 753 | srclen = 14; |
| 754 | break; |
| 755 | case 2: /* Product */ |
| 756 | srcptr = "\52\3E\0H\0C\0I\0 " |
| 757 | "\0H\0o\0s\0t\0 " |
| 758 | "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0"; |
| 759 | srclen = 42; |
| 760 | break; |
| 761 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 762 | debug("unknown value DT_STRING %x\n", |
| 763 | le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 764 | goto unknown; |
| 765 | } |
| 766 | break; |
| 767 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 768 | debug("unknown value %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 769 | goto unknown; |
| 770 | } |
| 771 | break; |
| 772 | case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 773 | switch (le16_to_cpu(req->value) >> 8) { |
| 774 | case USB_DT_HUB: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 775 | debug("USB_DT_HUB config\n"); |
| 776 | srcptr = &descriptor.hub; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 777 | srclen = descriptor.hub.bLength; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 778 | break; |
| 779 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 780 | debug("unknown value %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 781 | goto unknown; |
| 782 | } |
| 783 | break; |
| 784 | case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8): |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 785 | debug("USB_REQ_SET_ADDRESS\n"); |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 786 | ctrl->rootdev = le16_to_cpu(req->value); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 787 | break; |
| 788 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 789 | debug("USB_REQ_SET_CONFIGURATION\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 790 | /* Nothing to do */ |
| 791 | break; |
| 792 | case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 793 | tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */ |
| 794 | tmpbuf[1] = 0; |
| 795 | srcptr = tmpbuf; |
| 796 | srclen = 2; |
| 797 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 798 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 799 | memset(tmpbuf, 0, 4); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 800 | reg = ehci_readl(status_reg); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 801 | if (reg & EHCI_PS_CS) |
| 802 | tmpbuf[0] |= USB_PORT_STAT_CONNECTION; |
| 803 | if (reg & EHCI_PS_PE) |
| 804 | tmpbuf[0] |= USB_PORT_STAT_ENABLE; |
| 805 | if (reg & EHCI_PS_SUSP) |
| 806 | tmpbuf[0] |= USB_PORT_STAT_SUSPEND; |
| 807 | if (reg & EHCI_PS_OCA) |
| 808 | tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT; |
Sergei Shtylyov | 23dec68 | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 809 | if (reg & EHCI_PS_PR) |
| 810 | tmpbuf[0] |= USB_PORT_STAT_RESET; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 811 | if (reg & EHCI_PS_PP) |
| 812 | tmpbuf[1] |= USB_PORT_STAT_POWER >> 8; |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 813 | |
| 814 | if (ehci_is_TDI()) { |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 815 | switch (ctrl->ops.get_port_speed(ctrl, reg)) { |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 816 | case PORTSC_PSPD_FS: |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 817 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 818 | case PORTSC_PSPD_LS: |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 819 | tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8; |
| 820 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 821 | case PORTSC_PSPD_HS: |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 822 | default: |
| 823 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 824 | break; |
| 825 | } |
| 826 | } else { |
| 827 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 828 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 829 | |
| 830 | if (reg & EHCI_PS_CSC) |
| 831 | tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION; |
| 832 | if (reg & EHCI_PS_PEC) |
| 833 | tmpbuf[2] |= USB_PORT_STAT_C_ENABLE; |
| 834 | if (reg & EHCI_PS_OCC) |
| 835 | tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT; |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 836 | if (ctrl->portreset & (1 << port)) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 837 | tmpbuf[2] |= USB_PORT_STAT_C_RESET; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 838 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 839 | srcptr = tmpbuf; |
| 840 | srclen = 4; |
| 841 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 842 | case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 843 | reg = ehci_readl(status_reg); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 844 | reg &= ~EHCI_PS_CLEAR; |
| 845 | switch (le16_to_cpu(req->value)) { |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 846 | case USB_PORT_FEAT_ENABLE: |
| 847 | reg |= EHCI_PS_PE; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 848 | ehci_writel(status_reg, reg); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 849 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 850 | case USB_PORT_FEAT_POWER: |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 851 | if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) { |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 852 | reg |= EHCI_PS_PP; |
| 853 | ehci_writel(status_reg, reg); |
| 854 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 855 | break; |
| 856 | case USB_PORT_FEAT_RESET: |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 857 | if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS && |
| 858 | !ehci_is_TDI() && |
| 859 | EHCI_PS_IS_LOWSPEED(reg)) { |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 860 | /* Low speed device, give up ownership. */ |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 861 | debug("port %d low speed --> companion\n", |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 862 | port - 1); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 863 | reg |= EHCI_PS_PO; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 864 | ehci_writel(status_reg, reg); |
Hans de Goede | 63f34ca | 2015-05-10 14:10:16 +0200 | [diff] [blame] | 865 | return -ENXIO; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 866 | } else { |
Sergei Shtylyov | 23dec68 | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 867 | int ret; |
| 868 | |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 869 | reg |= EHCI_PS_PR; |
| 870 | reg &= ~EHCI_PS_PE; |
| 871 | ehci_writel(status_reg, reg); |
| 872 | /* |
| 873 | * caller must wait, then call GetPortStatus |
| 874 | * usb 2.0 specification say 50 ms resets on |
| 875 | * root |
| 876 | */ |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 877 | ctrl->ops.powerup_fixup(ctrl, status_reg, ®); |
Marek Vasut | 0973477 | 2011-07-11 02:37:01 +0200 | [diff] [blame] | 878 | |
Chris Zhang | fddf6d6 | 2010-01-06 13:34:04 -0800 | [diff] [blame] | 879 | ehci_writel(status_reg, reg & ~EHCI_PS_PR); |
Sergei Shtylyov | 23dec68 | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 880 | /* |
| 881 | * A host controller must terminate the reset |
| 882 | * and stabilize the state of the port within |
| 883 | * 2 milliseconds |
| 884 | */ |
| 885 | ret = handshake(status_reg, EHCI_PS_PR, 0, |
| 886 | 2 * 1000); |
Hans de Goede | b5b3ef2 | 2015-05-10 14:10:13 +0200 | [diff] [blame] | 887 | if (!ret) { |
| 888 | reg = ehci_readl(status_reg); |
| 889 | if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) |
| 890 | == EHCI_PS_CS && !ehci_is_TDI()) { |
| 891 | debug("port %d full speed --> companion\n", port - 1); |
| 892 | reg &= ~EHCI_PS_CLEAR; |
| 893 | reg |= EHCI_PS_PO; |
| 894 | ehci_writel(status_reg, reg); |
Hans de Goede | 63f34ca | 2015-05-10 14:10:16 +0200 | [diff] [blame] | 895 | return -ENXIO; |
Hans de Goede | b5b3ef2 | 2015-05-10 14:10:13 +0200 | [diff] [blame] | 896 | } else { |
| 897 | ctrl->portreset |= 1 << port; |
| 898 | } |
| 899 | } else { |
Sergei Shtylyov | 23dec68 | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 900 | printf("port(%d) reset error\n", |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 901 | port - 1); |
Hans de Goede | b5b3ef2 | 2015-05-10 14:10:13 +0200 | [diff] [blame] | 902 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 903 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 904 | break; |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 905 | case USB_PORT_FEAT_TEST: |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 906 | ehci_shutdown(ctrl); |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 907 | reg &= ~(0xf << 16); |
| 908 | reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16; |
| 909 | ehci_writel(status_reg, reg); |
| 910 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 911 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 912 | debug("unknown feature %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 913 | goto unknown; |
| 914 | } |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 915 | /* unblock posted writes */ |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 916 | (void) ehci_readl(&ctrl->hcor->or_usbcmd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 917 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 918 | case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 919 | reg = ehci_readl(status_reg); |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 920 | reg &= ~EHCI_PS_CLEAR; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 921 | switch (le16_to_cpu(req->value)) { |
| 922 | case USB_PORT_FEAT_ENABLE: |
| 923 | reg &= ~EHCI_PS_PE; |
| 924 | break; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 925 | case USB_PORT_FEAT_C_ENABLE: |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 926 | reg |= EHCI_PS_PE; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 927 | break; |
| 928 | case USB_PORT_FEAT_POWER: |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 929 | if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 930 | reg &= ~EHCI_PS_PP; |
| 931 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 932 | case USB_PORT_FEAT_C_CONNECTION: |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 933 | reg |= EHCI_PS_CSC; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 934 | break; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 935 | case USB_PORT_FEAT_OVER_CURRENT: |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 936 | reg |= EHCI_PS_OCC; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 937 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 938 | case USB_PORT_FEAT_C_RESET: |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 939 | ctrl->portreset &= ~(1 << port); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 940 | break; |
| 941 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 942 | debug("unknown feature %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 943 | goto unknown; |
| 944 | } |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 945 | ehci_writel(status_reg, reg); |
| 946 | /* unblock posted write */ |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 947 | (void) ehci_readl(&ctrl->hcor->or_usbcmd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 948 | break; |
| 949 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 950 | debug("Unknown request\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 951 | goto unknown; |
| 952 | } |
| 953 | |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 954 | mdelay(1); |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 955 | len = min3(srclen, (int)le16_to_cpu(req->length), length); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 956 | if (srcptr != NULL && len > 0) |
| 957 | memcpy(buffer, srcptr, len); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 958 | else |
| 959 | debug("Len is 0\n"); |
| 960 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 961 | dev->act_len = len; |
| 962 | dev->status = 0; |
| 963 | return 0; |
| 964 | |
| 965 | unknown: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 966 | debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 967 | req->requesttype, req->request, le16_to_cpu(req->value), |
| 968 | le16_to_cpu(req->index), le16_to_cpu(req->length)); |
| 969 | |
| 970 | dev->act_len = 0; |
| 971 | dev->status = USB_ST_STALLED; |
| 972 | return -1; |
| 973 | } |
| 974 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 975 | const struct ehci_ops default_ehci_ops = { |
| 976 | .set_usb_mode = ehci_set_usbmode, |
| 977 | .get_port_speed = ehci_get_port_speed, |
| 978 | .powerup_fixup = ehci_powerup_fixup, |
| 979 | .get_portsc_register = ehci_get_portsc_register, |
| 980 | }; |
| 981 | |
| 982 | static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops) |
Simon Glass | 0851caa | 2015-03-25 12:22:19 -0600 | [diff] [blame] | 983 | { |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 984 | if (!ops) { |
| 985 | ctrl->ops = default_ehci_ops; |
| 986 | } else { |
| 987 | ctrl->ops = *ops; |
| 988 | if (!ctrl->ops.set_usb_mode) |
| 989 | ctrl->ops.set_usb_mode = ehci_set_usbmode; |
| 990 | if (!ctrl->ops.get_port_speed) |
| 991 | ctrl->ops.get_port_speed = ehci_get_port_speed; |
| 992 | if (!ctrl->ops.powerup_fixup) |
| 993 | ctrl->ops.powerup_fixup = ehci_powerup_fixup; |
| 994 | if (!ctrl->ops.get_portsc_register) |
| 995 | ctrl->ops.get_portsc_register = |
| 996 | ehci_get_portsc_register; |
| 997 | } |
Simon Glass | 0851caa | 2015-03-25 12:22:19 -0600 | [diff] [blame] | 998 | } |
| 999 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1000 | #ifndef CONFIG_DM_USB |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 1001 | void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops) |
| 1002 | { |
| 1003 | struct ehci_ctrl *ctrl = &ehcic[index]; |
| 1004 | |
| 1005 | ctrl->priv = priv; |
| 1006 | ehci_setup_ops(ctrl, ops); |
| 1007 | } |
| 1008 | |
Simon Glass | 0851caa | 2015-03-25 12:22:19 -0600 | [diff] [blame] | 1009 | void *ehci_get_controller_priv(int index) |
| 1010 | { |
| 1011 | return ehcic[index].priv; |
| 1012 | } |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1013 | #endif |
Simon Glass | 0851caa | 2015-03-25 12:22:19 -0600 | [diff] [blame] | 1014 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1015 | static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1016 | { |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 1017 | struct QH *qh_list; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1018 | struct QH *periodic; |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1019 | uint32_t reg; |
| 1020 | uint32_t cmd; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1021 | int i; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 1022 | |
Vincent Palatin | 0d6f77c | 2012-12-12 17:55:22 -0800 | [diff] [blame] | 1023 | /* Set the high address word (aka segment) for 64-bit controller */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1024 | if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1) |
| 1025 | ehci_writel(&ctrl->hcor->or_ctrldssegment, 0); |
Stefan Roese | 2e98fc7 | 2009-01-21 17:12:10 +0100 | [diff] [blame] | 1026 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1027 | qh_list = &ctrl->qh_list; |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 1028 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1029 | /* Set head of reclaim list */ |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 1030 | memset(qh_list, 0, sizeof(*qh_list)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1031 | qh_list->qh_link = cpu_to_hc32((unsigned long)qh_list | QH_LINK_TYPE_QH); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 1032 | qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) | |
| 1033 | QH_ENDPT1_EPS(USB_SPEED_HIGH)); |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 1034 | qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1035 | qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 1036 | qh_list->qh_overlay.qt_token = |
| 1037 | cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1038 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1039 | flush_dcache_range((unsigned long)qh_list, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1040 | ALIGN_END_ADDR(struct QH, qh_list, 1)); |
| 1041 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1042 | /* Set async. queue head pointer. */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1043 | ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)qh_list); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1044 | |
| 1045 | /* |
| 1046 | * Set up periodic list |
| 1047 | * Step 1: Parent QH for all periodic transfers. |
| 1048 | */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1049 | ctrl->periodic_schedules = 0; |
| 1050 | periodic = &ctrl->periodic_queue; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1051 | memset(periodic, 0, sizeof(*periodic)); |
| 1052 | periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE); |
| 1053 | periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1054 | periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1055 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1056 | flush_dcache_range((unsigned long)periodic, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1057 | ALIGN_END_ADDR(struct QH, periodic, 1)); |
| 1058 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1059 | /* |
| 1060 | * Step 2: Setup frame-list: Every microframe, USB tries the same list. |
| 1061 | * In particular, device specifications on polling frequency |
| 1062 | * are disregarded. Keyboards seem to send NAK/NYet reliably |
| 1063 | * when polled with an empty buffer. |
| 1064 | * |
| 1065 | * Split Transactions will be spread across microframes using |
| 1066 | * S-mask and C-mask. |
| 1067 | */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1068 | if (ctrl->periodic_list == NULL) |
| 1069 | ctrl->periodic_list = memalign(4096, 1024 * 4); |
Nikita Kiryanov | 2f13e44 | 2013-07-29 13:27:40 +0300 | [diff] [blame] | 1070 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1071 | if (!ctrl->periodic_list) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1072 | return -ENOMEM; |
| 1073 | for (i = 0; i < 1024; i++) { |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1074 | ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1075 | | QH_LINK_TYPE_QH); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1078 | flush_dcache_range((unsigned long)ctrl->periodic_list, |
| 1079 | ALIGN_END_ADDR(uint32_t, ctrl->periodic_list, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1080 | 1024)); |
| 1081 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1082 | /* Set periodic list base address */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1083 | ehci_writel(&ctrl->hcor->or_periodiclistbase, |
| 1084 | (unsigned long)ctrl->periodic_list); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1085 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1086 | reg = ehci_readl(&ctrl->hccr->cr_hcsparams); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 1087 | descriptor.hub.bNbrPorts = HCS_N_PORTS(reg); |
Lucas Stach | f5b3408 | 2012-09-28 00:26:19 +0200 | [diff] [blame] | 1088 | debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 1089 | /* Port Indicators */ |
| 1090 | if (HCS_INDICATOR(reg)) |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 1091 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) |
| 1092 | | 0x80, &descriptor.hub.wHubCharacteristics); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 1093 | /* Port Power Control */ |
| 1094 | if (HCS_PPC(reg)) |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 1095 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) |
| 1096 | | 0x01, &descriptor.hub.wHubCharacteristics); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1097 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1098 | /* Start the host controller. */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1099 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
Wolfgang Denk | fb718e1 | 2009-02-12 00:08:39 +0100 | [diff] [blame] | 1100 | /* |
| 1101 | * Philips, Intel, and maybe others need CMD_RUN before the |
| 1102 | * root hub will detect new devices (why?); NEC doesn't |
| 1103 | */ |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 1104 | cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET); |
| 1105 | cmd |= CMD_RUN; |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1106 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 1107 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1108 | if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) { |
| 1109 | /* take control over the ports */ |
| 1110 | cmd = ehci_readl(&ctrl->hcor->or_configflag); |
| 1111 | cmd |= FLAG_CF; |
| 1112 | ehci_writel(&ctrl->hcor->or_configflag, cmd); |
| 1113 | } |
Kuo-Jung Su | b5d59de | 2013-05-15 15:29:23 +0800 | [diff] [blame] | 1114 | |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 1115 | /* unblock posted write */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1116 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 1117 | mdelay(5); |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1118 | reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase)); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 1119 | printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1120 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1124 | #ifndef CONFIG_DM_USB |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1125 | int usb_lowlevel_stop(int index) |
| 1126 | { |
| 1127 | ehci_shutdown(&ehcic[index]); |
| 1128 | return ehci_hcd_stop(index); |
| 1129 | } |
| 1130 | |
| 1131 | int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) |
| 1132 | { |
| 1133 | struct ehci_ctrl *ctrl = &ehcic[index]; |
| 1134 | uint tweaks = 0; |
| 1135 | int rc; |
| 1136 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 1137 | /** |
| 1138 | * Set ops to default_ehci_ops, ehci_hcd_init should call |
| 1139 | * ehci_set_controller_priv to change any of these function pointers. |
| 1140 | */ |
| 1141 | ctrl->ops = default_ehci_ops; |
| 1142 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1143 | rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor); |
| 1144 | if (rc) |
| 1145 | return rc; |
| 1146 | if (init == USB_INIT_DEVICE) |
| 1147 | goto done; |
| 1148 | |
| 1149 | /* EHCI spec section 4.1 */ |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 1150 | if (ehci_reset(ctrl)) |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1151 | return -1; |
| 1152 | |
| 1153 | #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET) |
| 1154 | rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor); |
| 1155 | if (rc) |
| 1156 | return rc; |
| 1157 | #endif |
| 1158 | #ifdef CONFIG_USB_EHCI_FARADAY |
| 1159 | tweaks |= EHCI_TWEAK_NO_INIT_CF; |
| 1160 | #endif |
| 1161 | rc = ehci_common_init(ctrl, tweaks); |
| 1162 | if (rc) |
| 1163 | return rc; |
| 1164 | |
| 1165 | ctrl->rootdev = 0; |
Troy Kisky | 7d6bbb9 | 2013-10-10 15:27:57 -0700 | [diff] [blame] | 1166 | done: |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 1167 | *controller = &ehcic[index]; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1168 | return 0; |
| 1169 | } |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1170 | #endif |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1171 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1172 | static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 1173 | void *buffer, int length) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1174 | { |
| 1175 | |
| 1176 | if (usb_pipetype(pipe) != PIPE_BULK) { |
| 1177 | debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe)); |
| 1178 | return -1; |
| 1179 | } |
| 1180 | return ehci_submit_async(dev, pipe, buffer, length, NULL); |
| 1181 | } |
| 1182 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1183 | static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe, |
| 1184 | void *buffer, int length, |
| 1185 | struct devrequest *setup) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1186 | { |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1187 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1188 | |
| 1189 | if (usb_pipetype(pipe) != PIPE_CONTROL) { |
| 1190 | debug("non-control pipe (type=%lu)", usb_pipetype(pipe)); |
| 1191 | return -1; |
| 1192 | } |
| 1193 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 1194 | if (usb_pipedevice(pipe) == ctrl->rootdev) { |
| 1195 | if (!ctrl->rootdev) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1196 | dev->speed = USB_SPEED_HIGH; |
| 1197 | return ehci_submit_root(dev, pipe, buffer, length, setup); |
| 1198 | } |
| 1199 | return ehci_submit_async(dev, pipe, buffer, length, setup); |
| 1200 | } |
| 1201 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1202 | struct int_queue { |
Hans de Goede | 8c5c5ca | 2014-09-24 14:06:05 +0200 | [diff] [blame] | 1203 | int elementsize; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1204 | unsigned long pipe; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1205 | struct QH *first; |
| 1206 | struct QH *current; |
| 1207 | struct QH *last; |
| 1208 | struct qTD *tds; |
| 1209 | }; |
| 1210 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1211 | #define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1212 | |
| 1213 | static int |
| 1214 | enable_periodic(struct ehci_ctrl *ctrl) |
| 1215 | { |
| 1216 | uint32_t cmd; |
| 1217 | struct ehci_hcor *hcor = ctrl->hcor; |
| 1218 | int ret; |
| 1219 | |
| 1220 | cmd = ehci_readl(&hcor->or_usbcmd); |
| 1221 | cmd |= CMD_PSE; |
| 1222 | ehci_writel(&hcor->or_usbcmd, cmd); |
| 1223 | |
| 1224 | ret = handshake((uint32_t *)&hcor->or_usbsts, |
| 1225 | STS_PSS, STS_PSS, 100 * 1000); |
| 1226 | if (ret < 0) { |
| 1227 | printf("EHCI failed: timeout when enabling periodic list\n"); |
| 1228 | return -ETIMEDOUT; |
| 1229 | } |
| 1230 | udelay(1000); |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static int |
| 1235 | disable_periodic(struct ehci_ctrl *ctrl) |
| 1236 | { |
| 1237 | uint32_t cmd; |
| 1238 | struct ehci_hcor *hcor = ctrl->hcor; |
| 1239 | int ret; |
| 1240 | |
| 1241 | cmd = ehci_readl(&hcor->or_usbcmd); |
| 1242 | cmd &= ~CMD_PSE; |
| 1243 | ehci_writel(&hcor->or_usbcmd, cmd); |
| 1244 | |
| 1245 | ret = handshake((uint32_t *)&hcor->or_usbsts, |
| 1246 | STS_PSS, 0, 100 * 1000); |
| 1247 | if (ret < 0) { |
| 1248 | printf("EHCI failed: timeout when disabling periodic list\n"); |
| 1249 | return -ETIMEDOUT; |
| 1250 | } |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1254 | static struct int_queue *_ehci_create_int_queue(struct usb_device *dev, |
| 1255 | unsigned long pipe, int queuesize, int elementsize, |
| 1256 | void *buffer, int interval) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1257 | { |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1258 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1259 | struct int_queue *result = NULL; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1260 | uint32_t i, toggle; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1261 | |
Hans de Goede | 7f7cb73 | 2014-09-24 14:06:04 +0200 | [diff] [blame] | 1262 | /* |
| 1263 | * Interrupt transfers requiring several transactions are not supported |
| 1264 | * because bInterval is ignored. |
| 1265 | * |
| 1266 | * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2 |
| 1267 | * <= PKT_ALIGN if several qTDs are required, while the USB |
| 1268 | * specification does not constrain this for interrupt transfers. That |
| 1269 | * means that ehci_submit_async() would support interrupt transfers |
| 1270 | * requiring several transactions only as long as the transfer size does |
| 1271 | * not require more than a single qTD. |
| 1272 | */ |
| 1273 | if (elementsize > usb_maxpacket(dev, pipe)) { |
| 1274 | printf("%s: xfers requiring several transactions are not supported.\n", |
| 1275 | __func__); |
| 1276 | return NULL; |
| 1277 | } |
| 1278 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1279 | debug("Enter create_int_queue\n"); |
| 1280 | if (usb_pipetype(pipe) != PIPE_INTERRUPT) { |
| 1281 | debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe)); |
| 1282 | return NULL; |
| 1283 | } |
| 1284 | |
| 1285 | /* limit to 4 full pages worth of data - |
| 1286 | * we can safely fit them in a single TD, |
| 1287 | * no matter the alignment |
| 1288 | */ |
| 1289 | if (elementsize >= 16384) { |
| 1290 | debug("too large elements for interrupt transfers\n"); |
| 1291 | return NULL; |
| 1292 | } |
| 1293 | |
| 1294 | result = malloc(sizeof(*result)); |
| 1295 | if (!result) { |
| 1296 | debug("ehci intr queue: out of memory\n"); |
| 1297 | goto fail1; |
| 1298 | } |
Hans de Goede | 8c5c5ca | 2014-09-24 14:06:05 +0200 | [diff] [blame] | 1299 | result->elementsize = elementsize; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1300 | result->pipe = pipe; |
Stephen Warren | d7fe61d | 2014-02-06 13:13:06 -0700 | [diff] [blame] | 1301 | result->first = memalign(USB_DMA_MINALIGN, |
| 1302 | sizeof(struct QH) * queuesize); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1303 | if (!result->first) { |
| 1304 | debug("ehci intr queue: out of memory\n"); |
| 1305 | goto fail2; |
| 1306 | } |
| 1307 | result->current = result->first; |
| 1308 | result->last = result->first + queuesize - 1; |
Stephen Warren | d7fe61d | 2014-02-06 13:13:06 -0700 | [diff] [blame] | 1309 | result->tds = memalign(USB_DMA_MINALIGN, |
| 1310 | sizeof(struct qTD) * queuesize); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1311 | if (!result->tds) { |
| 1312 | debug("ehci intr queue: out of memory\n"); |
| 1313 | goto fail3; |
| 1314 | } |
| 1315 | memset(result->first, 0, sizeof(struct QH) * queuesize); |
| 1316 | memset(result->tds, 0, sizeof(struct qTD) * queuesize); |
| 1317 | |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1318 | toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); |
| 1319 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1320 | for (i = 0; i < queuesize; i++) { |
| 1321 | struct QH *qh = result->first + i; |
| 1322 | struct qTD *td = result->tds + i; |
| 1323 | void **buf = &qh->buffer; |
| 1324 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1325 | qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1326 | if (i == queuesize - 1) |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1327 | qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1328 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1329 | qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td); |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1330 | qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1331 | qh->qh_endpt1 = |
| 1332 | cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */ |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1333 | (usb_maxpacket(dev, pipe) << 16) | /* MPS */ |
| 1334 | (1 << 14) | |
| 1335 | QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) | |
| 1336 | (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */ |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1337 | (usb_pipedevice(pipe) << 0)); |
| 1338 | qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */ |
| 1339 | (1 << 0)); /* S-mask: microframe 0 */ |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1340 | if (dev->speed == USB_SPEED_LOW || |
| 1341 | dev->speed == USB_SPEED_FULL) { |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 1342 | /* C-mask: microframes 2-4 */ |
| 1343 | qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8)); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1344 | } |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 1345 | ehci_update_endpt2_dev_n_port(dev, qh); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1346 | |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1347 | td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1348 | td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1349 | debug("communication direction is '%s'\n", |
| 1350 | usb_pipein(pipe) ? "in" : "out"); |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1351 | td->qt_token = cpu_to_hc32( |
| 1352 | QT_TOKEN_DT(toggle) | |
| 1353 | (elementsize << 16) | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1354 | ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */ |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1355 | 0x80); /* active */ |
| 1356 | td->qt_buffer[0] = |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1357 | cpu_to_hc32((unsigned long)buffer + i * elementsize); |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1358 | td->qt_buffer[1] = |
| 1359 | cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff); |
| 1360 | td->qt_buffer[2] = |
| 1361 | cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff); |
| 1362 | td->qt_buffer[3] = |
| 1363 | cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff); |
| 1364 | td->qt_buffer[4] = |
| 1365 | cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1366 | |
| 1367 | *buf = buffer + i * elementsize; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1368 | toggle ^= 1; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1371 | flush_dcache_range((unsigned long)buffer, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1372 | ALIGN_END_ADDR(char, buffer, |
| 1373 | queuesize * elementsize)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1374 | flush_dcache_range((unsigned long)result->first, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1375 | ALIGN_END_ADDR(struct QH, result->first, |
| 1376 | queuesize)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1377 | flush_dcache_range((unsigned long)result->tds, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1378 | ALIGN_END_ADDR(struct qTD, result->tds, |
| 1379 | queuesize)); |
| 1380 | |
Hans de Goede | 8ba55ed | 2014-09-24 14:06:03 +0200 | [diff] [blame] | 1381 | if (ctrl->periodic_schedules > 0) { |
| 1382 | if (disable_periodic(ctrl) < 0) { |
| 1383 | debug("FATAL: periodic should never fail, but did"); |
| 1384 | goto fail3; |
| 1385 | } |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | /* hook up to periodic list */ |
| 1389 | struct QH *list = &ctrl->periodic_queue; |
| 1390 | result->last->qh_link = list->qh_link; |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1391 | list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1392 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1393 | flush_dcache_range((unsigned long)result->last, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1394 | ALIGN_END_ADDR(struct QH, result->last, 1)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1395 | flush_dcache_range((unsigned long)list, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1396 | ALIGN_END_ADDR(struct QH, list, 1)); |
| 1397 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1398 | if (enable_periodic(ctrl) < 0) { |
| 1399 | debug("FATAL: periodic should never fail, but did"); |
| 1400 | goto fail3; |
| 1401 | } |
Hans de Goede | 8f5f4f7 | 2014-09-20 16:51:25 +0200 | [diff] [blame] | 1402 | ctrl->periodic_schedules++; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1403 | |
| 1404 | debug("Exit create_int_queue\n"); |
| 1405 | return result; |
| 1406 | fail3: |
| 1407 | if (result->tds) |
| 1408 | free(result->tds); |
| 1409 | fail2: |
| 1410 | if (result->first) |
| 1411 | free(result->first); |
| 1412 | if (result) |
| 1413 | free(result); |
| 1414 | fail1: |
| 1415 | return NULL; |
| 1416 | } |
| 1417 | |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1418 | static void *_ehci_poll_int_queue(struct usb_device *dev, |
| 1419 | struct int_queue *queue) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1420 | { |
| 1421 | struct QH *cur = queue->current; |
Hans de Goede | 9db174c | 2014-09-20 16:51:24 +0200 | [diff] [blame] | 1422 | struct qTD *cur_td; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1423 | uint32_t token, toggle; |
| 1424 | unsigned long pipe = queue->pipe; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1425 | |
| 1426 | /* depleted queue */ |
| 1427 | if (cur == NULL) { |
| 1428 | debug("Exit poll_int_queue with completed queue\n"); |
| 1429 | return NULL; |
| 1430 | } |
| 1431 | /* still active */ |
Hans de Goede | 9db174c | 2014-09-20 16:51:24 +0200 | [diff] [blame] | 1432 | cur_td = &queue->tds[queue->current - queue->first]; |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1433 | invalidate_dcache_range((unsigned long)cur_td, |
Hans de Goede | 9db174c | 2014-09-20 16:51:24 +0200 | [diff] [blame] | 1434 | ALIGN_END_ADDR(struct qTD, cur_td, 1)); |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1435 | token = hc32_to_cpu(cur_td->qt_token); |
| 1436 | if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) { |
| 1437 | debug("Exit poll_int_queue with no completed intr transfer. token is %x\n", token); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1438 | return NULL; |
| 1439 | } |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1440 | |
| 1441 | toggle = QT_TOKEN_GET_DT(token); |
| 1442 | usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle); |
| 1443 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1444 | if (!(cur->qh_link & QH_LINK_TERMINATE)) |
| 1445 | queue->current++; |
| 1446 | else |
| 1447 | queue->current = NULL; |
Hans de Goede | 8c5c5ca | 2014-09-24 14:06:05 +0200 | [diff] [blame] | 1448 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1449 | invalidate_dcache_range((unsigned long)cur->buffer, |
Hans de Goede | 8c5c5ca | 2014-09-24 14:06:05 +0200 | [diff] [blame] | 1450 | ALIGN_END_ADDR(char, cur->buffer, |
| 1451 | queue->elementsize)); |
| 1452 | |
Hans de Goede | 9db174c | 2014-09-20 16:51:24 +0200 | [diff] [blame] | 1453 | debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n", |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1454 | token, cur, queue->first); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1455 | return cur->buffer; |
| 1456 | } |
| 1457 | |
| 1458 | /* Do not free buffers associated with QHs, they're owned by someone else */ |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1459 | static int _ehci_destroy_int_queue(struct usb_device *dev, |
| 1460 | struct int_queue *queue) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1461 | { |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1462 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1463 | int result = -1; |
| 1464 | unsigned long timeout; |
| 1465 | |
| 1466 | if (disable_periodic(ctrl) < 0) { |
| 1467 | debug("FATAL: periodic should never fail, but did"); |
| 1468 | goto out; |
| 1469 | } |
Hans de Goede | 8f5f4f7 | 2014-09-20 16:51:25 +0200 | [diff] [blame] | 1470 | ctrl->periodic_schedules--; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1471 | |
| 1472 | struct QH *cur = &ctrl->periodic_queue; |
| 1473 | timeout = get_timer(0) + 500; /* abort after 500ms */ |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1474 | while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) { |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1475 | debug("considering %p, with qh_link %x\n", cur, cur->qh_link); |
| 1476 | if (NEXT_QH(cur) == queue->first) { |
| 1477 | debug("found candidate. removing from chain\n"); |
| 1478 | cur->qh_link = queue->last->qh_link; |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1479 | flush_dcache_range((unsigned long)cur, |
Hans de Goede | 8e00cf6 | 2014-09-20 16:51:23 +0200 | [diff] [blame] | 1480 | ALIGN_END_ADDR(struct QH, cur, 1)); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1481 | result = 0; |
| 1482 | break; |
| 1483 | } |
| 1484 | cur = NEXT_QH(cur); |
| 1485 | if (get_timer(0) > timeout) { |
| 1486 | printf("Timeout destroying interrupt endpoint queue\n"); |
| 1487 | result = -1; |
| 1488 | goto out; |
| 1489 | } |
| 1490 | } |
| 1491 | |
Hans de Goede | 8f5f4f7 | 2014-09-20 16:51:25 +0200 | [diff] [blame] | 1492 | if (ctrl->periodic_schedules > 0) { |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1493 | result = enable_periodic(ctrl); |
| 1494 | if (result < 0) |
| 1495 | debug("FATAL: periodic should never fail, but did"); |
| 1496 | } |
| 1497 | |
| 1498 | out: |
| 1499 | free(queue->tds); |
| 1500 | free(queue->first); |
| 1501 | free(queue); |
| 1502 | |
| 1503 | return result; |
| 1504 | } |
| 1505 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1506 | static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe, |
| 1507 | void *buffer, int length, int interval) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1508 | { |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1509 | void *backbuffer; |
| 1510 | struct int_queue *queue; |
| 1511 | unsigned long timeout; |
| 1512 | int result = 0, ret; |
| 1513 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1514 | debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d", |
| 1515 | dev, pipe, buffer, length, interval); |
Benoît Thébaudeau | 58c4dfb | 2012-08-09 23:50:44 +0200 | [diff] [blame] | 1516 | |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1517 | queue = _ehci_create_int_queue(dev, pipe, 1, length, buffer, interval); |
Hans de Goede | 7f7cb73 | 2014-09-24 14:06:04 +0200 | [diff] [blame] | 1518 | if (!queue) |
| 1519 | return -1; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1520 | |
| 1521 | timeout = get_timer(0) + USB_TIMEOUT_MS(pipe); |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1522 | while ((backbuffer = _ehci_poll_int_queue(dev, queue)) == NULL) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1523 | if (get_timer(0) > timeout) { |
| 1524 | printf("Timeout poll on interrupt endpoint\n"); |
| 1525 | result = -ETIMEDOUT; |
| 1526 | break; |
| 1527 | } |
| 1528 | |
| 1529 | if (backbuffer != buffer) { |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1530 | debug("got wrong buffer back (%p instead of %p)\n", |
| 1531 | backbuffer, buffer); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1532 | return -EINVAL; |
| 1533 | } |
| 1534 | |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1535 | ret = _ehci_destroy_int_queue(dev, queue); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1536 | if (ret < 0) |
| 1537 | return ret; |
| 1538 | |
| 1539 | /* everything worked out fine */ |
| 1540 | return result; |
Marek Vasut | 9b315fe | 2011-09-25 21:07:56 +0200 | [diff] [blame] | 1541 | } |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1542 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1543 | #ifndef CONFIG_DM_USB |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1544 | int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 1545 | void *buffer, int length) |
| 1546 | { |
| 1547 | return _ehci_submit_bulk_msg(dev, pipe, buffer, length); |
| 1548 | } |
| 1549 | |
| 1550 | int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 1551 | int length, struct devrequest *setup) |
| 1552 | { |
| 1553 | return _ehci_submit_control_msg(dev, pipe, buffer, length, setup); |
| 1554 | } |
| 1555 | |
| 1556 | int submit_int_msg(struct usb_device *dev, unsigned long pipe, |
| 1557 | void *buffer, int length, int interval) |
| 1558 | { |
| 1559 | return _ehci_submit_int_msg(dev, pipe, buffer, length, interval); |
| 1560 | } |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1561 | |
| 1562 | struct int_queue *create_int_queue(struct usb_device *dev, |
| 1563 | unsigned long pipe, int queuesize, int elementsize, |
| 1564 | void *buffer, int interval) |
| 1565 | { |
| 1566 | return _ehci_create_int_queue(dev, pipe, queuesize, elementsize, |
| 1567 | buffer, interval); |
| 1568 | } |
| 1569 | |
| 1570 | void *poll_int_queue(struct usb_device *dev, struct int_queue *queue) |
| 1571 | { |
| 1572 | return _ehci_poll_int_queue(dev, queue); |
| 1573 | } |
| 1574 | |
| 1575 | int destroy_int_queue(struct usb_device *dev, struct int_queue *queue) |
| 1576 | { |
| 1577 | return _ehci_destroy_int_queue(dev, queue); |
| 1578 | } |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1579 | #endif |
| 1580 | |
| 1581 | #ifdef CONFIG_DM_USB |
| 1582 | static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev, |
| 1583 | unsigned long pipe, void *buffer, int length, |
| 1584 | struct devrequest *setup) |
| 1585 | { |
| 1586 | debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__, |
| 1587 | dev->name, udev, udev->dev->name, udev->portnr); |
| 1588 | |
| 1589 | return _ehci_submit_control_msg(udev, pipe, buffer, length, setup); |
| 1590 | } |
| 1591 | |
| 1592 | static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev, |
| 1593 | unsigned long pipe, void *buffer, int length) |
| 1594 | { |
| 1595 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1596 | return _ehci_submit_bulk_msg(udev, pipe, buffer, length); |
| 1597 | } |
| 1598 | |
| 1599 | static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev, |
| 1600 | unsigned long pipe, void *buffer, int length, |
| 1601 | int interval) |
| 1602 | { |
| 1603 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1604 | return _ehci_submit_int_msg(udev, pipe, buffer, length, interval); |
| 1605 | } |
| 1606 | |
Hans de Goede | 0a7fa27 | 2015-05-10 14:10:18 +0200 | [diff] [blame] | 1607 | static struct int_queue *ehci_create_int_queue(struct udevice *dev, |
| 1608 | struct usb_device *udev, unsigned long pipe, int queuesize, |
| 1609 | int elementsize, void *buffer, int interval) |
| 1610 | { |
| 1611 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1612 | return _ehci_create_int_queue(udev, pipe, queuesize, elementsize, |
| 1613 | buffer, interval); |
| 1614 | } |
| 1615 | |
| 1616 | static void *ehci_poll_int_queue(struct udevice *dev, struct usb_device *udev, |
| 1617 | struct int_queue *queue) |
| 1618 | { |
| 1619 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1620 | return _ehci_poll_int_queue(udev, queue); |
| 1621 | } |
| 1622 | |
| 1623 | static int ehci_destroy_int_queue(struct udevice *dev, struct usb_device *udev, |
| 1624 | struct int_queue *queue) |
| 1625 | { |
| 1626 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1627 | return _ehci_destroy_int_queue(udev, queue); |
| 1628 | } |
| 1629 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1630 | int ehci_register(struct udevice *dev, struct ehci_hccr *hccr, |
| 1631 | struct ehci_hcor *hcor, const struct ehci_ops *ops, |
| 1632 | uint tweaks, enum usb_init_type init) |
| 1633 | { |
Hans de Goede | 76bc7f4 | 2015-05-05 11:54:35 +0200 | [diff] [blame] | 1634 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1635 | struct ehci_ctrl *ctrl = dev_get_priv(dev); |
| 1636 | int ret; |
| 1637 | |
| 1638 | debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__, |
| 1639 | dev->name, ctrl, hccr, hcor, init); |
| 1640 | |
Hans de Goede | 76bc7f4 | 2015-05-05 11:54:35 +0200 | [diff] [blame] | 1641 | priv->desc_before_addr = true; |
| 1642 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1643 | ehci_setup_ops(ctrl, ops); |
| 1644 | ctrl->hccr = hccr; |
| 1645 | ctrl->hcor = hcor; |
| 1646 | ctrl->priv = ctrl; |
| 1647 | |
| 1648 | if (init == USB_INIT_DEVICE) |
| 1649 | goto done; |
| 1650 | ret = ehci_reset(ctrl); |
| 1651 | if (ret) |
| 1652 | goto err; |
| 1653 | |
| 1654 | ret = ehci_common_init(ctrl, tweaks); |
| 1655 | if (ret) |
| 1656 | goto err; |
| 1657 | done: |
| 1658 | return 0; |
| 1659 | err: |
| 1660 | free(ctrl); |
| 1661 | debug("%s: failed, ret=%d\n", __func__, ret); |
| 1662 | return ret; |
| 1663 | } |
| 1664 | |
| 1665 | int ehci_deregister(struct udevice *dev) |
| 1666 | { |
| 1667 | struct ehci_ctrl *ctrl = dev_get_priv(dev); |
| 1668 | |
| 1669 | ehci_shutdown(ctrl); |
| 1670 | |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | struct dm_usb_ops ehci_usb_ops = { |
| 1675 | .control = ehci_submit_control_msg, |
| 1676 | .bulk = ehci_submit_bulk_msg, |
| 1677 | .interrupt = ehci_submit_int_msg, |
Hans de Goede | 0a7fa27 | 2015-05-10 14:10:18 +0200 | [diff] [blame] | 1678 | .create_int_queue = ehci_create_int_queue, |
| 1679 | .poll_int_queue = ehci_poll_int_queue, |
| 1680 | .destroy_int_queue = ehci_destroy_int_queue, |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1681 | }; |
| 1682 | |
| 1683 | #endif |