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