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