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