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