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> |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 24 | #include <asm/byteorder.h> |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame^] | 25 | #include <asm/unaligned.h> |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 26 | #include <usb.h> |
| 27 | #include <asm/io.h> |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 28 | #include <malloc.h> |
Stefan Roese | 67333f7 | 2010-11-26 15:43:28 +0100 | [diff] [blame] | 29 | #include <watchdog.h> |
Jean-Christophe PLAGNIOL-VILLARD | 2731b9a | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 30 | |
| 31 | #include "ehci.h" |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 32 | |
| 33 | int rootdev; |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 34 | struct ehci_hccr *hccr; /* R/O registers, not need for volatile */ |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 35 | volatile struct ehci_hcor *hcor; |
| 36 | |
| 37 | static uint16_t portreset; |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 38 | DEFINE_ALIGN_BUFFER(struct QH, qh_list, 1, USB_DMA_MINALIGN); |
| 39 | |
| 40 | #define ALIGN_END_ADDR(type, ptr, size) \ |
| 41 | ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN)) |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 42 | |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 43 | static struct descriptor { |
| 44 | struct usb_hub_descriptor hub; |
| 45 | struct usb_device_descriptor device; |
| 46 | struct usb_linux_config_descriptor config; |
| 47 | struct usb_linux_interface_descriptor interface; |
| 48 | struct usb_endpoint_descriptor endpoint; |
| 49 | } __attribute__ ((packed)) descriptor = { |
| 50 | { |
| 51 | 0x8, /* bDescLength */ |
| 52 | 0x29, /* bDescriptorType: hub descriptor */ |
| 53 | 2, /* bNrPorts -- runtime modified */ |
| 54 | 0, /* wHubCharacteristics */ |
Vincent Palatin | 5f4b4f2 | 2011-12-05 14:52:22 -0800 | [diff] [blame] | 55 | 10, /* bPwrOn2PwrGood */ |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 56 | 0, /* bHubCntrCurrent */ |
| 57 | {}, /* Device removable */ |
| 58 | {} /* at most 7 ports! XXX */ |
| 59 | }, |
| 60 | { |
| 61 | 0x12, /* bLength */ |
| 62 | 1, /* bDescriptorType: UDESC_DEVICE */ |
Sergei Shtylyov | 6d313c8 | 2010-02-27 21:29:42 +0300 | [diff] [blame] | 63 | cpu_to_le16(0x0200), /* bcdUSB: v2.0 */ |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 64 | 9, /* bDeviceClass: UDCLASS_HUB */ |
| 65 | 0, /* bDeviceSubClass: UDSUBCLASS_HUB */ |
| 66 | 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */ |
| 67 | 64, /* bMaxPacketSize: 64 bytes */ |
| 68 | 0x0000, /* idVendor */ |
| 69 | 0x0000, /* idProduct */ |
Sergei Shtylyov | 6d313c8 | 2010-02-27 21:29:42 +0300 | [diff] [blame] | 70 | cpu_to_le16(0x0100), /* bcdDevice */ |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 71 | 1, /* iManufacturer */ |
| 72 | 2, /* iProduct */ |
| 73 | 0, /* iSerialNumber */ |
| 74 | 1 /* bNumConfigurations: 1 */ |
| 75 | }, |
| 76 | { |
| 77 | 0x9, |
| 78 | 2, /* bDescriptorType: UDESC_CONFIG */ |
| 79 | cpu_to_le16(0x19), |
| 80 | 1, /* bNumInterface */ |
| 81 | 1, /* bConfigurationValue */ |
| 82 | 0, /* iConfiguration */ |
| 83 | 0x40, /* bmAttributes: UC_SELF_POWER */ |
| 84 | 0 /* bMaxPower */ |
| 85 | }, |
| 86 | { |
| 87 | 0x9, /* bLength */ |
| 88 | 4, /* bDescriptorType: UDESC_INTERFACE */ |
| 89 | 0, /* bInterfaceNumber */ |
| 90 | 0, /* bAlternateSetting */ |
| 91 | 1, /* bNumEndpoints */ |
| 92 | 9, /* bInterfaceClass: UICLASS_HUB */ |
| 93 | 0, /* bInterfaceSubClass: UISUBCLASS_HUB */ |
| 94 | 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */ |
| 95 | 0 /* iInterface */ |
| 96 | }, |
| 97 | { |
| 98 | 0x7, /* bLength */ |
| 99 | 5, /* bDescriptorType: UDESC_ENDPOINT */ |
| 100 | 0x81, /* bEndpointAddress: |
| 101 | * UE_DIR_IN | EHCI_INTR_ENDPT |
| 102 | */ |
| 103 | 3, /* bmAttributes: UE_INTERRUPT */ |
Tom Rix | 8f8bd56 | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 104 | 8, /* wMaxPacketSize */ |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 105 | 255 /* bInterval */ |
| 106 | }, |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 109 | #if defined(CONFIG_EHCI_IS_TDI) |
| 110 | #define ehci_is_TDI() (1) |
| 111 | #else |
| 112 | #define ehci_is_TDI() (0) |
| 113 | #endif |
| 114 | |
Marek Vasut | 3874b6d | 2011-07-11 02:37:01 +0200 | [diff] [blame] | 115 | void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg) |
| 116 | { |
| 117 | mdelay(50); |
| 118 | } |
| 119 | |
| 120 | void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg) |
| 121 | __attribute__((weak, alias("__ehci_powerup_fixup"))); |
| 122 | |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 123 | 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] | 124 | { |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 125 | uint32_t result; |
| 126 | do { |
| 127 | result = ehci_readl(ptr); |
Wolfgang Denk | 09c83a4 | 2010-10-22 14:23:00 +0200 | [diff] [blame] | 128 | udelay(5); |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 129 | if (result == ~(uint32_t)0) |
| 130 | return -1; |
| 131 | result &= mask; |
| 132 | if (result == done) |
| 133 | return 0; |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 134 | usec--; |
| 135 | } while (usec > 0); |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 136 | return -1; |
| 137 | } |
| 138 | |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 139 | static int ehci_reset(void) |
| 140 | { |
| 141 | uint32_t cmd; |
| 142 | uint32_t tmp; |
| 143 | uint32_t *reg_ptr; |
| 144 | int ret = 0; |
| 145 | |
| 146 | cmd = ehci_readl(&hcor->or_usbcmd); |
Stefan Roese | 273d720 | 2010-11-26 15:44:00 +0100 | [diff] [blame] | 147 | cmd = (cmd & ~CMD_RUN) | CMD_RESET; |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 148 | ehci_writel(&hcor->or_usbcmd, cmd); |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 149 | ret = handshake((uint32_t *)&hcor->or_usbcmd, CMD_RESET, 0, 250 * 1000); |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 150 | if (ret < 0) { |
| 151 | printf("EHCI fail to reset\n"); |
| 152 | goto out; |
| 153 | } |
| 154 | |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 155 | if (ehci_is_TDI()) { |
| 156 | reg_ptr = (uint32_t *)((u8 *)hcor + USBMODE); |
| 157 | tmp = ehci_readl(reg_ptr); |
| 158 | tmp |= USBMODE_CM_HC; |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 159 | #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN) |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 160 | tmp |= USBMODE_BE; |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 161 | #endif |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 162 | ehci_writel(reg_ptr, tmp); |
| 163 | } |
Simon Glass | 9ab4ce2 | 2012-02-27 10:52:47 +0000 | [diff] [blame] | 164 | |
| 165 | #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH |
| 166 | cmd = ehci_readl(&hcor->or_txfilltuning); |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 167 | cmd &= ~TXFIFO_THRESH_MASK; |
Simon Glass | 9ab4ce2 | 2012-02-27 10:52:47 +0000 | [diff] [blame] | 168 | cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH); |
| 169 | ehci_writel(&hcor->or_txfilltuning, cmd); |
| 170 | #endif |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 171 | out: |
| 172 | return ret; |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 173 | } |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 174 | |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 175 | static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz) |
| 176 | { |
Marek Vasut | b8adb12 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 177 | uint32_t delta, next; |
| 178 | uint32_t addr = (uint32_t)buf; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 179 | int idx; |
| 180 | |
Ilya Yanok | 189a695 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 181 | if (addr != ALIGN(addr, ARCH_DMA_MINALIGN)) |
Marek Vasut | b8adb12 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 182 | debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf); |
| 183 | |
Ilya Yanok | 189a695 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 184 | flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN)); |
| 185 | |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 186 | idx = 0; |
Benoît Thébaudeau | cdeb916 | 2012-07-19 22:16:38 +0200 | [diff] [blame] | 187 | while (idx < QT_BUFFER_CNT) { |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 188 | td->qt_buffer[idx] = cpu_to_hc32(addr); |
Wolfgang Denk | 3ed1607 | 2010-10-19 16:13:15 +0200 | [diff] [blame] | 189 | td->qt_buffer_hi[idx] = 0; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 190 | next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 191 | delta = next - addr; |
| 192 | if (delta >= sz) |
| 193 | break; |
| 194 | sz -= delta; |
| 195 | addr = next; |
| 196 | idx++; |
| 197 | } |
| 198 | |
Benoît Thébaudeau | cdeb916 | 2012-07-19 22:16:38 +0200 | [diff] [blame] | 199 | if (idx == QT_BUFFER_CNT) { |
Ilya Yanok | 2af16f8 | 2012-07-15 04:43:52 +0000 | [diff] [blame] | 200 | printf("out of buffer pointers (%u bytes left)\n", sz); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int |
| 208 | ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 209 | int length, struct devrequest *req) |
| 210 | { |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 211 | ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN); |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 212 | struct qTD *qtd; |
| 213 | int qtd_count = 0; |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 214 | int qtd_counter = 0; |
| 215 | |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 216 | volatile struct qTD *vtd; |
| 217 | unsigned long ts; |
| 218 | uint32_t *tdp; |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 219 | uint32_t endpt, maxpacket, token, usbsts; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 220 | uint32_t c, toggle; |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 221 | uint32_t cmd; |
Simon Glass | 96820a3 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 222 | int timeout; |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 223 | int ret = 0; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 224 | |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 225 | 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] | 226 | buffer, length, req); |
| 227 | if (req != NULL) |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 228 | 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] | 229 | req->request, req->request, |
| 230 | req->requesttype, req->requesttype, |
| 231 | le16_to_cpu(req->value), le16_to_cpu(req->value), |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 232 | le16_to_cpu(req->index)); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 233 | |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 234 | #define PKT_ALIGN 512 |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 235 | /* |
| 236 | * The USB transfer is split into qTD transfers. Eeach qTD transfer is |
| 237 | * described by a transfer descriptor (the qTD). The qTDs form a linked |
| 238 | * list with a queue head (QH). |
| 239 | * |
| 240 | * Each qTD transfer starts with a new USB packet, i.e. a packet cannot |
| 241 | * have its beginning in a qTD transfer and its end in the following |
| 242 | * one, so the qTD transfer lengths have to be chosen accordingly. |
| 243 | * |
| 244 | * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to |
| 245 | * single pages. The first data buffer can start at any offset within a |
| 246 | * page (not considering the cache-line alignment issues), while the |
| 247 | * following buffers must be page-aligned. There is no alignment |
| 248 | * constraint on the size of a qTD transfer. |
| 249 | */ |
| 250 | if (req != NULL) |
| 251 | /* 1 qTD will be needed for SETUP, and 1 for ACK. */ |
| 252 | qtd_count += 1 + 1; |
| 253 | if (length > 0 || req == NULL) { |
| 254 | /* |
| 255 | * 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] | 256 | * data payload (not considering the first qTD transfer, which |
| 257 | * may be longer or shorter, and the final one, which may be |
| 258 | * shorter). |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 259 | * |
| 260 | * 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] | 261 | * transfer size is aligned to PKT_ALIGN, which is a multiple of |
| 262 | * wMaxPacketSize (except in some cases for interrupt transfers, |
| 263 | * see comment in submit_int_msg()). |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 264 | * |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 265 | * 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] | 266 | * QT_BUFFER_CNT full pages will be used. |
| 267 | */ |
| 268 | int xfr_sz = QT_BUFFER_CNT; |
| 269 | /* |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 270 | * However, if the input buffer is not aligned to PKT_ALIGN, the |
| 271 | * 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] | 272 | * data buffer of each transfer will be page-unaligned. |
| 273 | */ |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 274 | if ((uint32_t)buffer & (PKT_ALIGN - 1)) |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 275 | xfr_sz--; |
| 276 | /* Convert the qTD transfer size to bytes. */ |
| 277 | xfr_sz *= EHCI_PAGE_SIZE; |
| 278 | /* |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 279 | * Approximate by excess the number of qTDs that will be |
| 280 | * required for the data payload. The exact formula is way more |
| 281 | * complicated and saves at most 2 qTDs, i.e. a total of 128 |
| 282 | * bytes. |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 283 | */ |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 284 | qtd_count += 2 + length / xfr_sz; |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 285 | } |
| 286 | /* |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 287 | * Threshold value based on the worst-case total size of the allocated qTDs for |
| 288 | * a mass-storage transfer of 65535 blocks of 512 bytes. |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 289 | */ |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 290 | #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024 |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 291 | #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI |
| 292 | #endif |
| 293 | qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD)); |
| 294 | if (qtd == NULL) { |
| 295 | printf("unable to allocate TDs\n"); |
| 296 | return -1; |
| 297 | } |
| 298 | |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 299 | memset(qh, 0, sizeof(struct QH)); |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 300 | memset(qtd, 0, qtd_count * sizeof(*qtd)); |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 301 | |
Marek Vasut | b8adb12 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 302 | toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); |
| 303 | |
Marek Vasut | 41b1f0a | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 304 | /* |
| 305 | * Setup QH (3.6 in ehci-r10.pdf) |
| 306 | * |
| 307 | * qh_link ................. 03-00 H |
| 308 | * qh_endpt1 ............... 07-04 H |
| 309 | * qh_endpt2 ............... 0B-08 H |
| 310 | * - qh_curtd |
| 311 | * qh_overlay.qt_next ...... 13-10 H |
| 312 | * - qh_overlay.qt_altnext |
| 313 | */ |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 314 | qh->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] | 315 | c = usb_pipespeed(pipe) != USB_SPEED_HIGH && !usb_pipeendpoint(pipe); |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 316 | maxpacket = usb_maxpacket(dev, pipe); |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 317 | endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) | |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 318 | QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) | |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 319 | QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) | |
| 320 | QH_ENDPT1_EPS(usb_pipespeed(pipe)) | |
| 321 | QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) | |
| 322 | QH_ENDPT1_DEVADDR(usb_pipedevice(pipe)); |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 323 | qh->qh_endpt1 = cpu_to_hc32(endpt); |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 324 | endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) | |
| 325 | QH_ENDPT2_HUBADDR(dev->parent->devnum) | |
| 326 | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0); |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 327 | qh->qh_endpt2 = cpu_to_hc32(endpt); |
| 328 | qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 329 | |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 330 | tdp = &qh->qh_overlay.qt_next; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 331 | |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 332 | if (req != NULL) { |
Marek Vasut | 41b1f0a | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 333 | /* |
| 334 | * Setup request qTD (3.5 in ehci-r10.pdf) |
| 335 | * |
| 336 | * qt_next ................ 03-00 H |
| 337 | * qt_altnext ............. 07-04 H |
| 338 | * qt_token ............... 0B-08 H |
| 339 | * |
| 340 | * [ buffer, buffer_hi ] loaded with "req". |
| 341 | */ |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 342 | qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 343 | 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] | 344 | token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) | |
| 345 | QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) | |
| 346 | QT_TOKEN_PID(QT_TOKEN_PID_SETUP) | |
| 347 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 348 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 349 | if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) { |
| 350 | printf("unable to construct SETUP TD\n"); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 351 | goto fail; |
| 352 | } |
Marek Vasut | 41b1f0a | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 353 | /* Update previous qTD! */ |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 354 | *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]); |
| 355 | tdp = &qtd[qtd_counter++].qt_next; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 356 | toggle = 1; |
| 357 | } |
| 358 | |
| 359 | if (length > 0 || req == NULL) { |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 360 | uint8_t *buf_ptr = buffer; |
| 361 | int left_length = length; |
| 362 | |
| 363 | do { |
| 364 | /* |
| 365 | * Determine the size of this qTD transfer. By default, |
| 366 | * QT_BUFFER_CNT full pages can be used. |
| 367 | */ |
| 368 | int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE; |
| 369 | /* |
| 370 | * However, if the input buffer is not page-aligned, the |
| 371 | * portion of the first page before the buffer start |
| 372 | * offset within that page is unusable. |
| 373 | */ |
| 374 | xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1); |
| 375 | /* |
| 376 | * In order to keep each packet within a qTD transfer, |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 377 | * align the qTD transfer size to PKT_ALIGN. |
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 | xfr_bytes &= ~(PKT_ALIGN - 1); |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 380 | /* |
| 381 | * This transfer may be shorter than the available qTD |
| 382 | * transfer size that has just been computed. |
| 383 | */ |
| 384 | xfr_bytes = min(xfr_bytes, left_length); |
| 385 | |
| 386 | /* |
| 387 | * Setup request qTD (3.5 in ehci-r10.pdf) |
| 388 | * |
| 389 | * qt_next ................ 03-00 H |
| 390 | * qt_altnext ............. 07-04 H |
| 391 | * qt_token ............... 0B-08 H |
| 392 | * |
| 393 | * [ buffer, buffer_hi ] loaded with "buffer". |
| 394 | */ |
| 395 | qtd[qtd_counter].qt_next = |
| 396 | cpu_to_hc32(QT_NEXT_TERMINATE); |
| 397 | qtd[qtd_counter].qt_altnext = |
| 398 | cpu_to_hc32(QT_NEXT_TERMINATE); |
| 399 | token = QT_TOKEN_DT(toggle) | |
| 400 | QT_TOKEN_TOTALBYTES(xfr_bytes) | |
| 401 | QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) | |
| 402 | QT_TOKEN_CERR(3) | |
| 403 | QT_TOKEN_PID(usb_pipein(pipe) ? |
| 404 | QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) | |
| 405 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
| 406 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
| 407 | if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr, |
| 408 | xfr_bytes)) { |
| 409 | printf("unable to construct DATA TD\n"); |
| 410 | goto fail; |
| 411 | } |
| 412 | /* Update previous qTD! */ |
| 413 | *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]); |
| 414 | tdp = &qtd[qtd_counter++].qt_next; |
Benoît Thébaudeau | db19134 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 415 | /* |
| 416 | * Data toggle has to be adjusted since the qTD transfer |
| 417 | * size is not always an even multiple of |
| 418 | * wMaxPacketSize. |
| 419 | */ |
| 420 | if ((xfr_bytes / maxpacket) & 1) |
| 421 | toggle ^= 1; |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 422 | buf_ptr += xfr_bytes; |
| 423 | left_length -= xfr_bytes; |
| 424 | } while (left_length > 0); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 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 | */ |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 435 | qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 436 | 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] | 437 | token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) | |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 438 | QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) | |
| 439 | QT_TOKEN_PID(usb_pipein(pipe) ? |
| 440 | QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) | |
| 441 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 442 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
Marek Vasut | 41b1f0a | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 443 | /* Update previous qTD! */ |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 444 | *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]); |
| 445 | tdp = &qtd[qtd_counter++].qt_next; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 446 | } |
| 447 | |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 448 | 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] | 449 | |
Stefan Roese | daa2daf | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 450 | /* Flush dcache */ |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 451 | flush_dcache_range((uint32_t)qh_list, |
| 452 | ALIGN_END_ADDR(struct QH, qh_list, 1)); |
| 453 | 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] | 454 | flush_dcache_range((uint32_t)qtd, |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 455 | ALIGN_END_ADDR(struct qTD, qtd, qtd_count)); |
Stefan Roese | daa2daf | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 456 | |
Ilya Yanok | c7701af | 2012-07-15 22:12:08 +0000 | [diff] [blame] | 457 | /* Set async. queue head pointer. */ |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 458 | ehci_writel(&hcor->or_asynclistaddr, (uint32_t)qh_list); |
Ilya Yanok | c7701af | 2012-07-15 22:12:08 +0000 | [diff] [blame] | 459 | |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 460 | usbsts = ehci_readl(&hcor->or_usbsts); |
| 461 | ehci_writel(&hcor->or_usbsts, (usbsts & 0x3f)); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 462 | |
| 463 | /* Enable async. schedule. */ |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 464 | cmd = ehci_readl(&hcor->or_usbcmd); |
| 465 | cmd |= CMD_ASE; |
| 466 | ehci_writel(&hcor->or_usbcmd, cmd); |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 467 | |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 468 | ret = handshake((uint32_t *)&hcor->or_usbsts, STS_ASS, STS_ASS, |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 469 | 100 * 1000); |
| 470 | if (ret < 0) { |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 471 | printf("EHCI fail timeout STS_ASS set\n"); |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 472 | goto fail; |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 473 | } |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 474 | |
| 475 | /* Wait for TDs to be processed. */ |
| 476 | ts = get_timer(0); |
Marek Vasut | de98e8b | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 477 | vtd = &qtd[qtd_counter - 1]; |
Simon Glass | 96820a3 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 478 | timeout = USB_TIMEOUT_MS(pipe); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 479 | do { |
Stefan Roese | daa2daf | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 480 | /* Invalidate dcache */ |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 481 | invalidate_dcache_range((uint32_t)qh_list, |
| 482 | ALIGN_END_ADDR(struct QH, qh_list, 1)); |
| 483 | invalidate_dcache_range((uint32_t)qh, |
| 484 | ALIGN_END_ADDR(struct QH, qh, 1)); |
Marek Vasut | b8adb12 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 485 | invalidate_dcache_range((uint32_t)qtd, |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 486 | ALIGN_END_ADDR(struct qTD, qtd, qtd_count)); |
Marek Vasut | b8adb12 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 487 | |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 488 | token = hc32_to_cpu(vtd->qt_token); |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 489 | if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 490 | break; |
Stefan Roese | 67333f7 | 2010-11-26 15:43:28 +0100 | [diff] [blame] | 491 | WATCHDOG_RESET(); |
Simon Glass | 96820a3 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 492 | } while (get_timer(ts) < timeout); |
| 493 | |
Ilya Yanok | 189a695 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 494 | /* |
| 495 | * Invalidate the memory area occupied by buffer |
| 496 | * Don't try to fix the buffer alignment, if it isn't properly |
| 497 | * aligned it's upper layer's fault so let invalidate_dcache_range() |
| 498 | * vow about it. But we have to fix the length as it's actual |
| 499 | * transfer length and can be unaligned. This is potentially |
| 500 | * dangerous operation, it's responsibility of the calling |
| 501 | * code to make sure enough space is reserved. |
| 502 | */ |
| 503 | invalidate_dcache_range((uint32_t)buffer, |
| 504 | ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN)); |
Marek Vasut | b8adb12 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 505 | |
Simon Glass | 96820a3 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 506 | /* Check that the TD processing happened */ |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 507 | if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) |
Simon Glass | 96820a3 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 508 | printf("EHCI timed out on TD - token=%#x\n", token); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 509 | |
| 510 | /* Disable async schedule. */ |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 511 | cmd = ehci_readl(&hcor->or_usbcmd); |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 512 | cmd &= ~CMD_ASE; |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 513 | ehci_writel(&hcor->or_usbcmd, cmd); |
| 514 | |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 515 | ret = handshake((uint32_t *)&hcor->or_usbsts, STS_ASS, 0, |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 516 | 100 * 1000); |
| 517 | if (ret < 0) { |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 518 | printf("EHCI fail timeout STS_ASS reset\n"); |
Michael Trimarchi | 1ed9f9a | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 519 | goto fail; |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 520 | } |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 521 | |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 522 | token = hc32_to_cpu(qh->qh_overlay.qt_token); |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 523 | if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) { |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 524 | debug("TOKEN=%#x\n", token); |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 525 | switch (QT_TOKEN_GET_STATUS(token) & |
| 526 | ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) { |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 527 | case 0: |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 528 | toggle = QT_TOKEN_GET_DT(token); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 529 | usb_settoggle(dev, usb_pipeendpoint(pipe), |
| 530 | usb_pipeout(pipe), toggle); |
| 531 | dev->status = 0; |
| 532 | break; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 533 | case QT_TOKEN_STATUS_HALTED: |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 534 | dev->status = USB_ST_STALLED; |
| 535 | break; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 536 | case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR: |
| 537 | case QT_TOKEN_STATUS_DATBUFERR: |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 538 | dev->status = USB_ST_BUF_ERR; |
| 539 | break; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 540 | case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET: |
| 541 | case QT_TOKEN_STATUS_BABBLEDET: |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 542 | dev->status = USB_ST_BABBLE_DET; |
| 543 | break; |
| 544 | default: |
| 545 | dev->status = USB_ST_CRC_ERR; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 546 | if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED) |
Anatolij Gustschin | 222d6df | 2010-11-02 11:47:29 +0100 | [diff] [blame] | 547 | dev->status |= USB_ST_STALLED; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 548 | break; |
| 549 | } |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 550 | dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 551 | } else { |
| 552 | dev->act_len = 0; |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 553 | debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n", |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 554 | dev->devnum, ehci_readl(&hcor->or_usbsts), |
| 555 | ehci_readl(&hcor->or_portsc[0]), |
| 556 | ehci_readl(&hcor->or_portsc[1])); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 557 | } |
| 558 | |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 559 | free(qtd); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 560 | return (dev->status != USB_ST_NOT_PROC) ? 0 : -1; |
| 561 | |
| 562 | fail: |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 563 | free(qtd); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 564 | return -1; |
| 565 | } |
| 566 | |
| 567 | static inline int min3(int a, int b, int c) |
| 568 | { |
| 569 | |
| 570 | if (b < a) |
| 571 | a = b; |
| 572 | if (c < a) |
| 573 | a = c; |
| 574 | return a; |
| 575 | } |
| 576 | |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 577 | int |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 578 | ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 579 | int length, struct devrequest *req) |
| 580 | { |
| 581 | uint8_t tmpbuf[4]; |
| 582 | u16 typeReq; |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 583 | void *srcptr = NULL; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 584 | int len, srclen; |
| 585 | uint32_t reg; |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 586 | uint32_t *status_reg; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 587 | |
Sergei Shtylyov | e06a055 | 2010-02-27 21:32:17 +0300 | [diff] [blame] | 588 | 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] | 589 | printf("The request port(%d) is not configured\n", |
| 590 | le16_to_cpu(req->index) - 1); |
| 591 | return -1; |
| 592 | } |
| 593 | status_reg = (uint32_t *)&hcor->or_portsc[ |
| 594 | le16_to_cpu(req->index) - 1]; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 595 | srclen = 0; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 596 | |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 597 | debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n", |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 598 | req->request, req->request, |
| 599 | req->requesttype, req->requesttype, |
| 600 | le16_to_cpu(req->value), le16_to_cpu(req->index)); |
| 601 | |
Prafulla Wadaskar | 44259bb | 2009-07-17 19:56:30 +0530 | [diff] [blame] | 602 | typeReq = req->request | req->requesttype << 8; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 603 | |
Prafulla Wadaskar | 44259bb | 2009-07-17 19:56:30 +0530 | [diff] [blame] | 604 | switch (typeReq) { |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 605 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 606 | switch (le16_to_cpu(req->value) >> 8) { |
| 607 | case USB_DT_DEVICE: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 608 | debug("USB_DT_DEVICE request\n"); |
| 609 | srcptr = &descriptor.device; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 610 | srclen = descriptor.device.bLength; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 611 | break; |
| 612 | case USB_DT_CONFIG: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 613 | debug("USB_DT_CONFIG config\n"); |
| 614 | srcptr = &descriptor.config; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 615 | srclen = descriptor.config.bLength + |
| 616 | descriptor.interface.bLength + |
| 617 | descriptor.endpoint.bLength; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 618 | break; |
| 619 | case USB_DT_STRING: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 620 | debug("USB_DT_STRING config\n"); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 621 | switch (le16_to_cpu(req->value) & 0xff) { |
| 622 | case 0: /* Language */ |
| 623 | srcptr = "\4\3\1\0"; |
| 624 | srclen = 4; |
| 625 | break; |
| 626 | case 1: /* Vendor */ |
| 627 | srcptr = "\16\3u\0-\0b\0o\0o\0t\0"; |
| 628 | srclen = 14; |
| 629 | break; |
| 630 | case 2: /* Product */ |
| 631 | srcptr = "\52\3E\0H\0C\0I\0 " |
| 632 | "\0H\0o\0s\0t\0 " |
| 633 | "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0"; |
| 634 | srclen = 42; |
| 635 | break; |
| 636 | default: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 637 | debug("unknown value DT_STRING %x\n", |
| 638 | le16_to_cpu(req->value)); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 639 | goto unknown; |
| 640 | } |
| 641 | break; |
| 642 | default: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 643 | debug("unknown value %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 644 | goto unknown; |
| 645 | } |
| 646 | break; |
| 647 | case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 648 | switch (le16_to_cpu(req->value) >> 8) { |
| 649 | case USB_DT_HUB: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 650 | debug("USB_DT_HUB config\n"); |
| 651 | srcptr = &descriptor.hub; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 652 | srclen = descriptor.hub.bLength; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 653 | break; |
| 654 | default: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 655 | debug("unknown value %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 656 | goto unknown; |
| 657 | } |
| 658 | break; |
| 659 | case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8): |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 660 | debug("USB_REQ_SET_ADDRESS\n"); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 661 | rootdev = le16_to_cpu(req->value); |
| 662 | break; |
| 663 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 664 | debug("USB_REQ_SET_CONFIGURATION\n"); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 665 | /* Nothing to do */ |
| 666 | break; |
| 667 | case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 668 | tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */ |
| 669 | tmpbuf[1] = 0; |
| 670 | srcptr = tmpbuf; |
| 671 | srclen = 2; |
| 672 | break; |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 673 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 674 | memset(tmpbuf, 0, 4); |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 675 | reg = ehci_readl(status_reg); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 676 | if (reg & EHCI_PS_CS) |
| 677 | tmpbuf[0] |= USB_PORT_STAT_CONNECTION; |
| 678 | if (reg & EHCI_PS_PE) |
| 679 | tmpbuf[0] |= USB_PORT_STAT_ENABLE; |
| 680 | if (reg & EHCI_PS_SUSP) |
| 681 | tmpbuf[0] |= USB_PORT_STAT_SUSPEND; |
| 682 | if (reg & EHCI_PS_OCA) |
| 683 | tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT; |
Sergei Shtylyov | c8b2d1d | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 684 | if (reg & EHCI_PS_PR) |
| 685 | tmpbuf[0] |= USB_PORT_STAT_RESET; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 686 | if (reg & EHCI_PS_PP) |
| 687 | tmpbuf[1] |= USB_PORT_STAT_POWER >> 8; |
Stefan Roese | 597eb28 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 688 | |
| 689 | if (ehci_is_TDI()) { |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 690 | switch (PORTSC_PSPD(reg)) { |
| 691 | case PORTSC_PSPD_FS: |
Stefan Roese | 597eb28 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 692 | break; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 693 | case PORTSC_PSPD_LS: |
Stefan Roese | 597eb28 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 694 | tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8; |
| 695 | break; |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 696 | case PORTSC_PSPD_HS: |
Stefan Roese | 597eb28 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 697 | default: |
| 698 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 699 | break; |
| 700 | } |
| 701 | } else { |
| 702 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 703 | } |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 704 | |
| 705 | if (reg & EHCI_PS_CSC) |
| 706 | tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION; |
| 707 | if (reg & EHCI_PS_PEC) |
| 708 | tmpbuf[2] |= USB_PORT_STAT_C_ENABLE; |
| 709 | if (reg & EHCI_PS_OCC) |
| 710 | tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT; |
| 711 | if (portreset & (1 << le16_to_cpu(req->index))) |
| 712 | tmpbuf[2] |= USB_PORT_STAT_C_RESET; |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 713 | |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 714 | srcptr = tmpbuf; |
| 715 | srclen = 4; |
| 716 | break; |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 717 | 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] | 718 | reg = ehci_readl(status_reg); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 719 | reg &= ~EHCI_PS_CLEAR; |
| 720 | switch (le16_to_cpu(req->value)) { |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 721 | case USB_PORT_FEAT_ENABLE: |
| 722 | reg |= EHCI_PS_PE; |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 723 | ehci_writel(status_reg, reg); |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 724 | break; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 725 | case USB_PORT_FEAT_POWER: |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 726 | if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams))) { |
| 727 | reg |= EHCI_PS_PP; |
| 728 | ehci_writel(status_reg, reg); |
| 729 | } |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 730 | break; |
| 731 | case USB_PORT_FEAT_RESET: |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 732 | if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS && |
| 733 | !ehci_is_TDI() && |
| 734 | EHCI_PS_IS_LOWSPEED(reg)) { |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 735 | /* Low speed device, give up ownership. */ |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 736 | debug("port %d low speed --> companion\n", |
| 737 | req->index - 1); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 738 | reg |= EHCI_PS_PO; |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 739 | ehci_writel(status_reg, reg); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 740 | break; |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 741 | } else { |
Sergei Shtylyov | c8b2d1d | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 742 | int ret; |
| 743 | |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 744 | reg |= EHCI_PS_PR; |
| 745 | reg &= ~EHCI_PS_PE; |
| 746 | ehci_writel(status_reg, reg); |
| 747 | /* |
| 748 | * caller must wait, then call GetPortStatus |
| 749 | * usb 2.0 specification say 50 ms resets on |
| 750 | * root |
| 751 | */ |
Marek Vasut | 3874b6d | 2011-07-11 02:37:01 +0200 | [diff] [blame] | 752 | ehci_powerup_fixup(status_reg, ®); |
| 753 | |
Chris Zhang | b416191 | 2010-01-06 13:34:04 -0800 | [diff] [blame] | 754 | ehci_writel(status_reg, reg & ~EHCI_PS_PR); |
Sergei Shtylyov | c8b2d1d | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 755 | /* |
| 756 | * A host controller must terminate the reset |
| 757 | * and stabilize the state of the port within |
| 758 | * 2 milliseconds |
| 759 | */ |
| 760 | ret = handshake(status_reg, EHCI_PS_PR, 0, |
| 761 | 2 * 1000); |
| 762 | if (!ret) |
| 763 | portreset |= |
| 764 | 1 << le16_to_cpu(req->index); |
| 765 | else |
| 766 | printf("port(%d) reset error\n", |
| 767 | le16_to_cpu(req->index) - 1); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 768 | } |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 769 | break; |
| 770 | default: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 771 | debug("unknown feature %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 772 | goto unknown; |
| 773 | } |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 774 | /* unblock posted writes */ |
Kumar Gala | 50243e3 | 2009-07-07 15:48:58 -0500 | [diff] [blame] | 775 | (void) ehci_readl(&hcor->or_usbcmd); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 776 | break; |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 777 | 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] | 778 | reg = ehci_readl(status_reg); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 779 | switch (le16_to_cpu(req->value)) { |
| 780 | case USB_PORT_FEAT_ENABLE: |
| 781 | reg &= ~EHCI_PS_PE; |
| 782 | break; |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 783 | case USB_PORT_FEAT_C_ENABLE: |
| 784 | reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_PE; |
| 785 | break; |
| 786 | case USB_PORT_FEAT_POWER: |
| 787 | if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams))) |
| 788 | reg = reg & ~(EHCI_PS_CLEAR | EHCI_PS_PP); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 789 | case USB_PORT_FEAT_C_CONNECTION: |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 790 | reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_CSC; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 791 | break; |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 792 | case USB_PORT_FEAT_OVER_CURRENT: |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 793 | reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_OCC; |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 794 | break; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 795 | case USB_PORT_FEAT_C_RESET: |
| 796 | portreset &= ~(1 << le16_to_cpu(req->index)); |
| 797 | break; |
| 798 | default: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 799 | debug("unknown feature %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 800 | goto unknown; |
| 801 | } |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 802 | ehci_writel(status_reg, reg); |
| 803 | /* unblock posted write */ |
Kumar Gala | 50243e3 | 2009-07-07 15:48:58 -0500 | [diff] [blame] | 804 | (void) ehci_readl(&hcor->or_usbcmd); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 805 | break; |
| 806 | default: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 807 | debug("Unknown request\n"); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 808 | goto unknown; |
| 809 | } |
| 810 | |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 811 | mdelay(1); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 812 | len = min3(srclen, le16_to_cpu(req->length), length); |
| 813 | if (srcptr != NULL && len > 0) |
| 814 | memcpy(buffer, srcptr, len); |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 815 | else |
| 816 | debug("Len is 0\n"); |
| 817 | |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 818 | dev->act_len = len; |
| 819 | dev->status = 0; |
| 820 | return 0; |
| 821 | |
| 822 | unknown: |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 823 | debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n", |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 824 | req->requesttype, req->request, le16_to_cpu(req->value), |
| 825 | le16_to_cpu(req->index), le16_to_cpu(req->length)); |
| 826 | |
| 827 | dev->act_len = 0; |
| 828 | dev->status = USB_ST_STALLED; |
| 829 | return -1; |
| 830 | } |
| 831 | |
| 832 | int usb_lowlevel_stop(void) |
| 833 | { |
| 834 | return ehci_hcd_stop(); |
| 835 | } |
| 836 | |
| 837 | int usb_lowlevel_init(void) |
| 838 | { |
| 839 | uint32_t reg; |
michael | db63299 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 840 | uint32_t cmd; |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 841 | |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 842 | if (ehci_hcd_init()) |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 843 | return -1; |
| 844 | |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 845 | /* EHCI spec section 4.1 */ |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 846 | if (ehci_reset()) |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 847 | return -1; |
| 848 | |
Stefan Roese | 832e614 | 2009-01-21 17:12:10 +0100 | [diff] [blame] | 849 | #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET) |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 850 | if (ehci_hcd_init()) |
Stefan Roese | 832e614 | 2009-01-21 17:12:10 +0100 | [diff] [blame] | 851 | return -1; |
| 852 | #endif |
| 853 | |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 854 | /* Set head of reclaim list */ |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 855 | memset(qh_list, 0, sizeof(*qh_list)); |
| 856 | 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] | 857 | qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) | |
| 858 | QH_ENDPT1_EPS(USB_SPEED_HIGH)); |
Tom Rini | 71c5de4 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 859 | qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 860 | qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 861 | 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] | 862 | qh_list->qh_overlay.qt_token = |
| 863 | cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED)); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 864 | |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 865 | reg = ehci_readl(&hccr->cr_hcsparams); |
| 866 | descriptor.hub.bNbrPorts = HCS_N_PORTS(reg); |
| 867 | printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts); |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 868 | /* Port Indicators */ |
| 869 | if (HCS_INDICATOR(reg)) |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame^] | 870 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) |
| 871 | | 0x80, &descriptor.hub.wHubCharacteristics); |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 872 | /* Port Power Control */ |
| 873 | if (HCS_PPC(reg)) |
Lucas Stach | 93ad908 | 2012-09-06 08:00:13 +0200 | [diff] [blame^] | 874 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) |
| 875 | | 0x01, &descriptor.hub.wHubCharacteristics); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 876 | |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 877 | /* Start the host controller. */ |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 878 | cmd = ehci_readl(&hcor->or_usbcmd); |
Wolfgang Denk | f15c651 | 2009-02-12 00:08:39 +0100 | [diff] [blame] | 879 | /* |
| 880 | * Philips, Intel, and maybe others need CMD_RUN before the |
| 881 | * root hub will detect new devices (why?); NEC doesn't |
| 882 | */ |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 883 | cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET); |
| 884 | cmd |= CMD_RUN; |
| 885 | ehci_writel(&hcor->or_usbcmd, cmd); |
| 886 | |
| 887 | /* take control over the ports */ |
| 888 | cmd = ehci_readl(&hcor->or_configflag); |
| 889 | cmd |= FLAG_CF; |
| 890 | ehci_writel(&hcor->or_configflag, cmd); |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 891 | /* unblock posted write */ |
michael | 51ab142 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 892 | cmd = ehci_readl(&hcor->or_usbcmd); |
Mike Frysinger | 5b84dd6 | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 893 | mdelay(5); |
Remy Böhmer | c0d722f | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 894 | reg = HC_VERSION(ehci_readl(&hccr->cr_capbase)); |
| 895 | printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 896 | |
| 897 | rootdev = 0; |
| 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | int |
| 903 | submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 904 | int length) |
| 905 | { |
| 906 | |
| 907 | if (usb_pipetype(pipe) != PIPE_BULK) { |
| 908 | debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe)); |
| 909 | return -1; |
| 910 | } |
| 911 | return ehci_submit_async(dev, pipe, buffer, length, NULL); |
| 912 | } |
| 913 | |
| 914 | int |
| 915 | submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 916 | int length, struct devrequest *setup) |
| 917 | { |
| 918 | |
| 919 | if (usb_pipetype(pipe) != PIPE_CONTROL) { |
| 920 | debug("non-control pipe (type=%lu)", usb_pipetype(pipe)); |
| 921 | return -1; |
| 922 | } |
| 923 | |
| 924 | if (usb_pipedevice(pipe) == rootdev) { |
Benoît Thébaudeau | 14eb79b | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 925 | if (!rootdev) |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 926 | dev->speed = USB_SPEED_HIGH; |
| 927 | return ehci_submit_root(dev, pipe, buffer, length, setup); |
| 928 | } |
| 929 | return ehci_submit_async(dev, pipe, buffer, length, setup); |
| 930 | } |
| 931 | |
| 932 | int |
| 933 | submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 934 | int length, int interval) |
| 935 | { |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 936 | debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d", |
| 937 | dev, pipe, buffer, length, interval); |
Benoît Thébaudeau | 44ae0be | 2012-08-09 23:50:44 +0200 | [diff] [blame] | 938 | |
| 939 | /* |
| 940 | * Interrupt transfers requiring several transactions are not supported |
| 941 | * because bInterval is ignored. |
Benoît Thébaudeau | 5cec214 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 942 | * |
| 943 | * 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] | 944 | * <= PKT_ALIGN if several qTDs are required, while the USB |
| 945 | * specification does not constrain this for interrupt transfers. That |
| 946 | * means that ehci_submit_async() would support interrupt transfers |
| 947 | * requiring several transactions only as long as the transfer size does |
| 948 | * not require more than a single qTD. |
Benoît Thébaudeau | 44ae0be | 2012-08-09 23:50:44 +0200 | [diff] [blame] | 949 | */ |
| 950 | if (length > usb_maxpacket(dev, pipe)) { |
| 951 | printf("%s: Interrupt transfers requiring several transactions " |
| 952 | "are not supported.\n", __func__); |
| 953 | return -1; |
| 954 | } |
Marek Vasut | 7555d5e | 2011-09-25 21:07:56 +0200 | [diff] [blame] | 955 | return ehci_submit_async(dev, pipe, buffer, length, NULL); |
Michael Trimarchi | aaf098c | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 956 | } |