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