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