Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2007-2008, Juniper Networks, Inc. |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 3 | * Copyright (c) 2008, Excito Elektronik i Skåne AB |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 4 | * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it> |
| 5 | * |
Michael Trimarchi | 241f751 | 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 | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 23 | #include <common.h> |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 24 | #include <asm/byteorder.h> |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 25 | #include <usb.h> |
| 26 | #include <asm/io.h> |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 27 | #include <malloc.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8f6bcf4 | 2009-04-03 12:46:58 +0200 | [diff] [blame^] | 28 | |
| 29 | #include "ehci.h" |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 30 | |
| 31 | int rootdev; |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 32 | struct ehci_hccr *hccr; /* R/O registers, not need for volatile */ |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 33 | volatile struct ehci_hcor *hcor; |
| 34 | |
| 35 | static uint16_t portreset; |
| 36 | static struct QH qh_list __attribute__((aligned(32))); |
| 37 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 38 | static struct descriptor { |
| 39 | struct usb_hub_descriptor hub; |
| 40 | struct usb_device_descriptor device; |
| 41 | struct usb_linux_config_descriptor config; |
| 42 | struct usb_linux_interface_descriptor interface; |
| 43 | struct usb_endpoint_descriptor endpoint; |
| 44 | } __attribute__ ((packed)) descriptor = { |
| 45 | { |
| 46 | 0x8, /* bDescLength */ |
| 47 | 0x29, /* bDescriptorType: hub descriptor */ |
| 48 | 2, /* bNrPorts -- runtime modified */ |
| 49 | 0, /* wHubCharacteristics */ |
| 50 | 0xff, /* bPwrOn2PwrGood */ |
| 51 | 0, /* bHubCntrCurrent */ |
| 52 | {}, /* Device removable */ |
| 53 | {} /* at most 7 ports! XXX */ |
| 54 | }, |
| 55 | { |
| 56 | 0x12, /* bLength */ |
| 57 | 1, /* bDescriptorType: UDESC_DEVICE */ |
| 58 | 0x0002, /* bcdUSB: v2.0 */ |
| 59 | 9, /* bDeviceClass: UDCLASS_HUB */ |
| 60 | 0, /* bDeviceSubClass: UDSUBCLASS_HUB */ |
| 61 | 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */ |
| 62 | 64, /* bMaxPacketSize: 64 bytes */ |
| 63 | 0x0000, /* idVendor */ |
| 64 | 0x0000, /* idProduct */ |
| 65 | 0x0001, /* bcdDevice */ |
| 66 | 1, /* iManufacturer */ |
| 67 | 2, /* iProduct */ |
| 68 | 0, /* iSerialNumber */ |
| 69 | 1 /* bNumConfigurations: 1 */ |
| 70 | }, |
| 71 | { |
| 72 | 0x9, |
| 73 | 2, /* bDescriptorType: UDESC_CONFIG */ |
| 74 | cpu_to_le16(0x19), |
| 75 | 1, /* bNumInterface */ |
| 76 | 1, /* bConfigurationValue */ |
| 77 | 0, /* iConfiguration */ |
| 78 | 0x40, /* bmAttributes: UC_SELF_POWER */ |
| 79 | 0 /* bMaxPower */ |
| 80 | }, |
| 81 | { |
| 82 | 0x9, /* bLength */ |
| 83 | 4, /* bDescriptorType: UDESC_INTERFACE */ |
| 84 | 0, /* bInterfaceNumber */ |
| 85 | 0, /* bAlternateSetting */ |
| 86 | 1, /* bNumEndpoints */ |
| 87 | 9, /* bInterfaceClass: UICLASS_HUB */ |
| 88 | 0, /* bInterfaceSubClass: UISUBCLASS_HUB */ |
| 89 | 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */ |
| 90 | 0 /* iInterface */ |
| 91 | }, |
| 92 | { |
| 93 | 0x7, /* bLength */ |
| 94 | 5, /* bDescriptorType: UDESC_ENDPOINT */ |
| 95 | 0x81, /* bEndpointAddress: |
| 96 | * UE_DIR_IN | EHCI_INTR_ENDPT |
| 97 | */ |
| 98 | 3, /* bmAttributes: UE_INTERRUPT */ |
| 99 | 8, 0, /* wMaxPacketSize */ |
| 100 | 255 /* bInterval */ |
| 101 | }, |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 104 | #if defined(CONFIG_EHCI_IS_TDI) |
| 105 | #define ehci_is_TDI() (1) |
| 106 | #else |
| 107 | #define ehci_is_TDI() (0) |
| 108 | #endif |
| 109 | |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 110 | #if defined(CONFIG_EHCI_DCACHE) |
| 111 | /* |
| 112 | * Routines to handle (flush/invalidate) the dcache for the QH and qTD |
| 113 | * structures and data buffers. This is needed on platforms using this |
| 114 | * EHCI support with dcache enabled. |
| 115 | */ |
| 116 | static void flush_invalidate(u32 addr, int size, int flush) |
| 117 | { |
| 118 | if (flush) |
| 119 | flush_dcache_range(addr, addr + size); |
| 120 | else |
| 121 | invalidate_dcache_range(addr, addr + size); |
| 122 | } |
| 123 | |
| 124 | static void cache_qtd(struct qTD *qtd, int flush) |
| 125 | { |
| 126 | u32 *ptr = (u32 *)qtd->qt_buffer[0]; |
| 127 | int len = (qtd->qt_token & 0x7fff0000) >> 16; |
| 128 | |
| 129 | flush_invalidate((u32)qtd, sizeof(struct qTD), flush); |
| 130 | if (ptr && len) |
| 131 | flush_invalidate((u32)ptr, len, flush); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | static inline struct QH *qh_addr(struct QH *qh) |
| 136 | { |
| 137 | return (struct QH *)((u32)qh & 0xffffffe0); |
| 138 | } |
| 139 | |
| 140 | static void cache_qh(struct QH *qh, int flush) |
| 141 | { |
| 142 | struct qTD *qtd; |
| 143 | struct qTD *next; |
| 144 | static struct qTD *first_qtd; |
| 145 | |
| 146 | /* |
| 147 | * Walk the QH list and flush/invalidate all entries |
| 148 | */ |
| 149 | while (1) { |
| 150 | flush_invalidate((u32)qh_addr(qh), sizeof(struct QH), flush); |
| 151 | if ((u32)qh & QH_LINK_TYPE_QH) |
| 152 | break; |
| 153 | qh = qh_addr(qh); |
| 154 | qh = (struct QH *)qh->qh_link; |
| 155 | } |
| 156 | qh = qh_addr(qh); |
| 157 | |
| 158 | /* |
| 159 | * Save first qTD pointer, needed for invalidating pass on this QH |
| 160 | */ |
| 161 | if (flush) |
| 162 | first_qtd = qtd = (struct qTD *)(*(u32 *)&qh->qh_overlay & |
| 163 | 0xffffffe0); |
| 164 | else |
| 165 | qtd = first_qtd; |
| 166 | |
| 167 | /* |
| 168 | * Walk the qTD list and flush/invalidate all entries |
| 169 | */ |
| 170 | while (1) { |
| 171 | if (qtd == NULL) |
| 172 | break; |
| 173 | cache_qtd(qtd, flush); |
| 174 | next = (struct qTD *)((u32)qtd->qt_next & 0xffffffe0); |
| 175 | if (next == qtd) |
| 176 | break; |
| 177 | qtd = next; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static inline void ehci_flush_dcache(struct QH *qh) |
| 182 | { |
| 183 | cache_qh(qh, 1); |
| 184 | } |
| 185 | |
| 186 | static inline void ehci_invalidate_dcache(struct QH *qh) |
| 187 | { |
| 188 | cache_qh(qh, 0); |
| 189 | } |
| 190 | #else /* CONFIG_EHCI_DCACHE */ |
| 191 | /* |
| 192 | * |
| 193 | */ |
| 194 | static inline void ehci_flush_dcache(struct QH *qh) |
| 195 | { |
| 196 | } |
| 197 | |
| 198 | static inline void ehci_invalidate_dcache(struct QH *qh) |
| 199 | { |
| 200 | } |
| 201 | #endif /* CONFIG_EHCI_DCACHE */ |
| 202 | |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 203 | static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec) |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 204 | { |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 205 | uint32_t result; |
| 206 | do { |
| 207 | result = ehci_readl(ptr); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 208 | if (result == ~(uint32_t)0) |
| 209 | return -1; |
| 210 | result &= mask; |
| 211 | if (result == done) |
| 212 | return 0; |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 213 | udelay(1); |
| 214 | usec--; |
| 215 | } while (usec > 0); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | static void ehci_free(void *p, size_t sz) |
| 220 | { |
| 221 | |
| 222 | } |
| 223 | |
| 224 | static int ehci_reset(void) |
| 225 | { |
| 226 | uint32_t cmd; |
| 227 | uint32_t tmp; |
| 228 | uint32_t *reg_ptr; |
| 229 | int ret = 0; |
| 230 | |
| 231 | cmd = ehci_readl(&hcor->or_usbcmd); |
| 232 | cmd |= CMD_RESET; |
| 233 | ehci_writel(&hcor->or_usbcmd, cmd); |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 234 | ret = handshake((uint32_t *)&hcor->or_usbcmd, CMD_RESET, 0, 250 * 1000); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 235 | if (ret < 0) { |
| 236 | printf("EHCI fail to reset\n"); |
| 237 | goto out; |
| 238 | } |
| 239 | |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 240 | if (ehci_is_TDI()) { |
| 241 | reg_ptr = (uint32_t *)((u8 *)hcor + USBMODE); |
| 242 | tmp = ehci_readl(reg_ptr); |
| 243 | tmp |= USBMODE_CM_HC; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 244 | #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN) |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 245 | tmp |= USBMODE_BE; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 246 | #endif |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 247 | ehci_writel(reg_ptr, tmp); |
| 248 | } |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 249 | out: |
| 250 | return ret; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 251 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 252 | |
| 253 | static void *ehci_alloc(size_t sz, size_t align) |
| 254 | { |
| 255 | static struct QH qh __attribute__((aligned(32))); |
| 256 | static struct qTD td[3] __attribute__((aligned (32))); |
| 257 | static int ntds; |
| 258 | void *p; |
| 259 | |
| 260 | switch (sz) { |
| 261 | case sizeof(struct QH): |
| 262 | p = &qh; |
| 263 | ntds = 0; |
| 264 | break; |
| 265 | case sizeof(struct qTD): |
| 266 | if (ntds == 3) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 267 | debug("out of TDs\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 268 | return NULL; |
| 269 | } |
| 270 | p = &td[ntds]; |
| 271 | ntds++; |
| 272 | break; |
| 273 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 274 | debug("unknown allocation size\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 275 | return NULL; |
| 276 | } |
| 277 | |
| 278 | memset(p, sz, 0); |
| 279 | return p; |
| 280 | } |
| 281 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 282 | static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz) |
| 283 | { |
| 284 | uint32_t addr, delta, next; |
| 285 | int idx; |
| 286 | |
| 287 | addr = (uint32_t) buf; |
| 288 | idx = 0; |
| 289 | while (idx < 5) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 290 | td->qt_buffer[idx] = cpu_to_hc32(addr); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 291 | next = (addr + 4096) & ~4095; |
| 292 | delta = next - addr; |
| 293 | if (delta >= sz) |
| 294 | break; |
| 295 | sz -= delta; |
| 296 | addr = next; |
| 297 | idx++; |
| 298 | } |
| 299 | |
| 300 | if (idx == 5) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 301 | debug("out of buffer pointers (%u bytes left)\n", sz); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int |
| 309 | ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 310 | int length, struct devrequest *req) |
| 311 | { |
| 312 | struct QH *qh; |
| 313 | struct qTD *td; |
| 314 | volatile struct qTD *vtd; |
| 315 | unsigned long ts; |
| 316 | uint32_t *tdp; |
| 317 | uint32_t endpt, token, usbsts; |
| 318 | uint32_t c, toggle; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 319 | uint32_t cmd; |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 320 | int ret = 0; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 321 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 322 | debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe, |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 323 | buffer, length, req); |
| 324 | if (req != NULL) |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 325 | debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 326 | req->request, req->request, |
| 327 | req->requesttype, req->requesttype, |
| 328 | le16_to_cpu(req->value), le16_to_cpu(req->value), |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 329 | le16_to_cpu(req->index)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 330 | |
| 331 | qh = ehci_alloc(sizeof(struct QH), 32); |
| 332 | if (qh == NULL) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 333 | debug("unable to allocate QH\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 334 | return -1; |
| 335 | } |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 336 | qh->qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 337 | c = (usb_pipespeed(pipe) != USB_SPEED_HIGH && |
| 338 | usb_pipeendpoint(pipe) == 0) ? 1 : 0; |
| 339 | endpt = (8 << 28) | |
| 340 | (c << 27) | |
| 341 | (usb_maxpacket(dev, pipe) << 16) | |
| 342 | (0 << 15) | |
| 343 | (1 << 14) | |
| 344 | (usb_pipespeed(pipe) << 12) | |
| 345 | (usb_pipeendpoint(pipe) << 8) | |
| 346 | (0 << 7) | (usb_pipedevice(pipe) << 0); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 347 | qh->qh_endpt1 = cpu_to_hc32(endpt); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 348 | endpt = (1 << 30) | |
| 349 | (dev->portnr << 23) | |
| 350 | (dev->parent->devnum << 16) | (0 << 8) | (0 << 0); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 351 | qh->qh_endpt2 = cpu_to_hc32(endpt); |
| 352 | qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 353 | qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 354 | |
| 355 | td = NULL; |
| 356 | tdp = &qh->qh_overlay.qt_next; |
| 357 | |
| 358 | toggle = |
| 359 | usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); |
| 360 | |
| 361 | if (req != NULL) { |
| 362 | td = ehci_alloc(sizeof(struct qTD), 32); |
| 363 | if (td == NULL) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 364 | debug("unable to allocate SETUP td\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 365 | goto fail; |
| 366 | } |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 367 | td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 368 | td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 369 | token = (0 << 31) | |
| 370 | (sizeof(*req) << 16) | |
| 371 | (0 << 15) | (0 << 12) | (3 << 10) | (2 << 8) | (0x80 << 0); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 372 | td->qt_token = cpu_to_hc32(token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 373 | if (ehci_td_buffer(td, req, sizeof(*req)) != 0) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 374 | debug("unable construct SETUP td\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 375 | ehci_free(td, sizeof(*td)); |
| 376 | goto fail; |
| 377 | } |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 378 | *tdp = cpu_to_hc32((uint32_t) td); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 379 | tdp = &td->qt_next; |
| 380 | toggle = 1; |
| 381 | } |
| 382 | |
| 383 | if (length > 0 || req == NULL) { |
| 384 | td = ehci_alloc(sizeof(struct qTD), 32); |
| 385 | if (td == NULL) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 386 | debug("unable to allocate DATA td\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 387 | goto fail; |
| 388 | } |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 389 | td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 390 | td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 391 | token = (toggle << 31) | |
| 392 | (length << 16) | |
| 393 | ((req == NULL ? 1 : 0) << 15) | |
| 394 | (0 << 12) | |
| 395 | (3 << 10) | |
| 396 | ((usb_pipein(pipe) ? 1 : 0) << 8) | (0x80 << 0); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 397 | td->qt_token = cpu_to_hc32(token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 398 | if (ehci_td_buffer(td, buffer, length) != 0) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 399 | debug("unable construct DATA td\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 400 | ehci_free(td, sizeof(*td)); |
| 401 | goto fail; |
| 402 | } |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 403 | *tdp = cpu_to_hc32((uint32_t) td); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 404 | tdp = &td->qt_next; |
| 405 | } |
| 406 | |
| 407 | if (req != NULL) { |
| 408 | td = ehci_alloc(sizeof(struct qTD), 32); |
| 409 | if (td == NULL) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 410 | debug("unable to allocate ACK td\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 411 | goto fail; |
| 412 | } |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 413 | td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 414 | td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 415 | token = (toggle << 31) | |
| 416 | (0 << 16) | |
| 417 | (1 << 15) | |
| 418 | (0 << 12) | |
| 419 | (3 << 10) | |
| 420 | ((usb_pipein(pipe) ? 0 : 1) << 8) | (0x80 << 0); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 421 | td->qt_token = cpu_to_hc32(token); |
| 422 | *tdp = cpu_to_hc32((uint32_t) td); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 423 | tdp = &td->qt_next; |
| 424 | } |
| 425 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 426 | qh_list.qh_link = cpu_to_hc32((uint32_t) qh | QH_LINK_TYPE_QH); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 427 | |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 428 | /* Flush dcache */ |
| 429 | ehci_flush_dcache(&qh_list); |
| 430 | |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 431 | usbsts = ehci_readl(&hcor->or_usbsts); |
| 432 | ehci_writel(&hcor->or_usbsts, (usbsts & 0x3f)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 433 | |
| 434 | /* Enable async. schedule. */ |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 435 | cmd = ehci_readl(&hcor->or_usbcmd); |
| 436 | cmd |= CMD_ASE; |
| 437 | ehci_writel(&hcor->or_usbcmd, cmd); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 438 | |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 439 | ret = handshake((uint32_t *)&hcor->or_usbsts, STD_ASS, STD_ASS, |
| 440 | 100 * 1000); |
| 441 | if (ret < 0) { |
| 442 | printf("EHCI fail timeout STD_ASS set\n"); |
| 443 | goto fail; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 444 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 445 | |
| 446 | /* Wait for TDs to be processed. */ |
| 447 | ts = get_timer(0); |
| 448 | vtd = td; |
| 449 | do { |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 450 | /* Invalidate dcache */ |
| 451 | ehci_invalidate_dcache(&qh_list); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 452 | token = hc32_to_cpu(vtd->qt_token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 453 | if (!(token & 0x80)) |
| 454 | break; |
| 455 | } while (get_timer(ts) < CONFIG_SYS_HZ); |
| 456 | |
| 457 | /* Disable async schedule. */ |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 458 | cmd = ehci_readl(&hcor->or_usbcmd); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 459 | cmd &= ~CMD_ASE; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 460 | ehci_writel(&hcor->or_usbcmd, cmd); |
| 461 | |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 462 | ret = handshake((uint32_t *)&hcor->or_usbsts, STD_ASS, 0, |
| 463 | 100 * 1000); |
| 464 | if (ret < 0) { |
| 465 | printf("EHCI fail timeout STD_ASS reset\n"); |
| 466 | goto fail; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 467 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 468 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 469 | qh_list.qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 470 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 471 | token = hc32_to_cpu(qh->qh_overlay.qt_token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 472 | if (!(token & 0x80)) { |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 473 | debug("TOKEN=%#x\n", token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 474 | switch (token & 0xfc) { |
| 475 | case 0: |
| 476 | toggle = token >> 31; |
| 477 | usb_settoggle(dev, usb_pipeendpoint(pipe), |
| 478 | usb_pipeout(pipe), toggle); |
| 479 | dev->status = 0; |
| 480 | break; |
| 481 | case 0x40: |
| 482 | dev->status = USB_ST_STALLED; |
| 483 | break; |
| 484 | case 0xa0: |
| 485 | case 0x20: |
| 486 | dev->status = USB_ST_BUF_ERR; |
| 487 | break; |
| 488 | case 0x50: |
| 489 | case 0x10: |
| 490 | dev->status = USB_ST_BABBLE_DET; |
| 491 | break; |
| 492 | default: |
| 493 | dev->status = USB_ST_CRC_ERR; |
| 494 | break; |
| 495 | } |
| 496 | dev->act_len = length - ((token >> 16) & 0x7fff); |
| 497 | } else { |
| 498 | dev->act_len = 0; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 499 | debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n", |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 500 | dev->devnum, ehci_readl(&hcor->or_usbsts), |
| 501 | ehci_readl(&hcor->or_portsc[0]), |
| 502 | ehci_readl(&hcor->or_portsc[1])); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | return (dev->status != USB_ST_NOT_PROC) ? 0 : -1; |
| 506 | |
| 507 | fail: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 508 | td = (void *)hc32_to_cpu(qh->qh_overlay.qt_next); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 509 | while (td != (void *)QT_NEXT_TERMINATE) { |
| 510 | qh->qh_overlay.qt_next = td->qt_next; |
| 511 | ehci_free(td, sizeof(*td)); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 512 | td = (void *)hc32_to_cpu(qh->qh_overlay.qt_next); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 513 | } |
| 514 | ehci_free(qh, sizeof(*qh)); |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | static inline int min3(int a, int b, int c) |
| 519 | { |
| 520 | |
| 521 | if (b < a) |
| 522 | a = b; |
| 523 | if (c < a) |
| 524 | a = c; |
| 525 | return a; |
| 526 | } |
| 527 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 528 | int |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 529 | ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 530 | int length, struct devrequest *req) |
| 531 | { |
| 532 | uint8_t tmpbuf[4]; |
| 533 | u16 typeReq; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 534 | void *srcptr = NULL; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 535 | int len, srclen; |
| 536 | uint32_t reg; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 537 | uint32_t *status_reg; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 538 | |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 539 | if (le16_to_cpu(req->index) >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) { |
| 540 | printf("The request port(%d) is not configured\n", |
| 541 | le16_to_cpu(req->index) - 1); |
| 542 | return -1; |
| 543 | } |
| 544 | status_reg = (uint32_t *)&hcor->or_portsc[ |
| 545 | le16_to_cpu(req->index) - 1]; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 546 | srclen = 0; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 547 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 548 | debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 549 | req->request, req->request, |
| 550 | req->requesttype, req->requesttype, |
| 551 | le16_to_cpu(req->value), le16_to_cpu(req->index)); |
| 552 | |
| 553 | typeReq = req->request << 8 | req->requesttype; |
| 554 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 555 | switch (le16_to_cpu(typeReq)) { |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 556 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 557 | switch (le16_to_cpu(req->value) >> 8) { |
| 558 | case USB_DT_DEVICE: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 559 | debug("USB_DT_DEVICE request\n"); |
| 560 | srcptr = &descriptor.device; |
| 561 | srclen = 0x12; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 562 | break; |
| 563 | case USB_DT_CONFIG: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 564 | debug("USB_DT_CONFIG config\n"); |
| 565 | srcptr = &descriptor.config; |
| 566 | srclen = 0x19; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 567 | break; |
| 568 | case USB_DT_STRING: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 569 | debug("USB_DT_STRING config\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 570 | switch (le16_to_cpu(req->value) & 0xff) { |
| 571 | case 0: /* Language */ |
| 572 | srcptr = "\4\3\1\0"; |
| 573 | srclen = 4; |
| 574 | break; |
| 575 | case 1: /* Vendor */ |
| 576 | srcptr = "\16\3u\0-\0b\0o\0o\0t\0"; |
| 577 | srclen = 14; |
| 578 | break; |
| 579 | case 2: /* Product */ |
| 580 | srcptr = "\52\3E\0H\0C\0I\0 " |
| 581 | "\0H\0o\0s\0t\0 " |
| 582 | "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0"; |
| 583 | srclen = 42; |
| 584 | break; |
| 585 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 586 | debug("unknown value DT_STRING %x\n", |
| 587 | le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 588 | goto unknown; |
| 589 | } |
| 590 | break; |
| 591 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 592 | debug("unknown value %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 593 | goto unknown; |
| 594 | } |
| 595 | break; |
| 596 | case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 597 | switch (le16_to_cpu(req->value) >> 8) { |
| 598 | case USB_DT_HUB: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 599 | debug("USB_DT_HUB config\n"); |
| 600 | srcptr = &descriptor.hub; |
| 601 | srclen = 0x8; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 602 | break; |
| 603 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 604 | debug("unknown value %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 605 | goto unknown; |
| 606 | } |
| 607 | break; |
| 608 | case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8): |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 609 | debug("USB_REQ_SET_ADDRESS\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 610 | rootdev = le16_to_cpu(req->value); |
| 611 | break; |
| 612 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 613 | debug("USB_REQ_SET_CONFIGURATION\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 614 | /* Nothing to do */ |
| 615 | break; |
| 616 | case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 617 | tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */ |
| 618 | tmpbuf[1] = 0; |
| 619 | srcptr = tmpbuf; |
| 620 | srclen = 2; |
| 621 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 622 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 623 | memset(tmpbuf, 0, 4); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 624 | reg = ehci_readl(status_reg); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 625 | if (reg & EHCI_PS_CS) |
| 626 | tmpbuf[0] |= USB_PORT_STAT_CONNECTION; |
| 627 | if (reg & EHCI_PS_PE) |
| 628 | tmpbuf[0] |= USB_PORT_STAT_ENABLE; |
| 629 | if (reg & EHCI_PS_SUSP) |
| 630 | tmpbuf[0] |= USB_PORT_STAT_SUSPEND; |
| 631 | if (reg & EHCI_PS_OCA) |
| 632 | tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 633 | if (reg & EHCI_PS_PR && |
| 634 | (portreset & (1 << le16_to_cpu(req->index)))) { |
| 635 | int ret; |
| 636 | /* force reset to complete */ |
| 637 | reg = reg & ~(EHCI_PS_PR | EHCI_PS_CLEAR); |
| 638 | ehci_writel(status_reg, reg); |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 639 | ret = handshake(status_reg, EHCI_PS_PR, 0, 2 * 1000); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 640 | if (!ret) |
| 641 | tmpbuf[0] |= USB_PORT_STAT_RESET; |
| 642 | else |
| 643 | printf("port(%d) reset error\n", |
| 644 | le16_to_cpu(req->index) - 1); |
| 645 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 646 | if (reg & EHCI_PS_PP) |
| 647 | tmpbuf[1] |= USB_PORT_STAT_POWER >> 8; |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 648 | |
| 649 | if (ehci_is_TDI()) { |
| 650 | switch ((reg >> 26) & 3) { |
| 651 | case 0: |
| 652 | break; |
| 653 | case 1: |
| 654 | tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8; |
| 655 | break; |
| 656 | case 2: |
| 657 | default: |
| 658 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 659 | break; |
| 660 | } |
| 661 | } else { |
| 662 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 663 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 664 | |
| 665 | if (reg & EHCI_PS_CSC) |
| 666 | tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION; |
| 667 | if (reg & EHCI_PS_PEC) |
| 668 | tmpbuf[2] |= USB_PORT_STAT_C_ENABLE; |
| 669 | if (reg & EHCI_PS_OCC) |
| 670 | tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT; |
| 671 | if (portreset & (1 << le16_to_cpu(req->index))) |
| 672 | tmpbuf[2] |= USB_PORT_STAT_C_RESET; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 673 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 674 | srcptr = tmpbuf; |
| 675 | srclen = 4; |
| 676 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 677 | case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 678 | reg = ehci_readl(status_reg); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 679 | reg &= ~EHCI_PS_CLEAR; |
| 680 | switch (le16_to_cpu(req->value)) { |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 681 | case USB_PORT_FEAT_ENABLE: |
| 682 | reg |= EHCI_PS_PE; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 683 | ehci_writel(status_reg, reg); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 684 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 685 | case USB_PORT_FEAT_POWER: |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 686 | if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams))) { |
| 687 | reg |= EHCI_PS_PP; |
| 688 | ehci_writel(status_reg, reg); |
| 689 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 690 | break; |
| 691 | case USB_PORT_FEAT_RESET: |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 692 | if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS && |
| 693 | !ehci_is_TDI() && |
| 694 | EHCI_PS_IS_LOWSPEED(reg)) { |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 695 | /* Low speed device, give up ownership. */ |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 696 | debug("port %d low speed --> companion\n", |
| 697 | req->index - 1); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 698 | reg |= EHCI_PS_PO; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 699 | ehci_writel(status_reg, reg); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 700 | break; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 701 | } else { |
| 702 | reg |= EHCI_PS_PR; |
| 703 | reg &= ~EHCI_PS_PE; |
| 704 | ehci_writel(status_reg, reg); |
| 705 | /* |
| 706 | * caller must wait, then call GetPortStatus |
| 707 | * usb 2.0 specification say 50 ms resets on |
| 708 | * root |
| 709 | */ |
| 710 | wait_ms(50); |
| 711 | portreset |= 1 << le16_to_cpu(req->index); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 712 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 713 | break; |
| 714 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 715 | debug("unknown feature %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 716 | goto unknown; |
| 717 | } |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 718 | /* unblock posted writes */ |
| 719 | ehci_readl(&hcor->or_usbcmd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 720 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 721 | case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 722 | reg = ehci_readl(status_reg); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 723 | switch (le16_to_cpu(req->value)) { |
| 724 | case USB_PORT_FEAT_ENABLE: |
| 725 | reg &= ~EHCI_PS_PE; |
| 726 | break; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 727 | case USB_PORT_FEAT_C_ENABLE: |
| 728 | reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_PE; |
| 729 | break; |
| 730 | case USB_PORT_FEAT_POWER: |
| 731 | if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams))) |
| 732 | reg = reg & ~(EHCI_PS_CLEAR | EHCI_PS_PP); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 733 | case USB_PORT_FEAT_C_CONNECTION: |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 734 | reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_CSC; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 735 | break; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 736 | case USB_PORT_FEAT_OVER_CURRENT: |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 737 | reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_OCC; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 738 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 739 | case USB_PORT_FEAT_C_RESET: |
| 740 | portreset &= ~(1 << le16_to_cpu(req->index)); |
| 741 | break; |
| 742 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 743 | debug("unknown feature %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 744 | goto unknown; |
| 745 | } |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 746 | ehci_writel(status_reg, reg); |
| 747 | /* unblock posted write */ |
| 748 | ehci_readl(&hcor->or_usbcmd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 749 | break; |
| 750 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 751 | debug("Unknown request\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 752 | goto unknown; |
| 753 | } |
| 754 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 755 | wait_ms(1); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 756 | len = min3(srclen, le16_to_cpu(req->length), length); |
| 757 | if (srcptr != NULL && len > 0) |
| 758 | memcpy(buffer, srcptr, len); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 759 | else |
| 760 | debug("Len is 0\n"); |
| 761 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 762 | dev->act_len = len; |
| 763 | dev->status = 0; |
| 764 | return 0; |
| 765 | |
| 766 | unknown: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 767 | debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 768 | req->requesttype, req->request, le16_to_cpu(req->value), |
| 769 | le16_to_cpu(req->index), le16_to_cpu(req->length)); |
| 770 | |
| 771 | dev->act_len = 0; |
| 772 | dev->status = USB_ST_STALLED; |
| 773 | return -1; |
| 774 | } |
| 775 | |
| 776 | int usb_lowlevel_stop(void) |
| 777 | { |
| 778 | return ehci_hcd_stop(); |
| 779 | } |
| 780 | |
| 781 | int usb_lowlevel_init(void) |
| 782 | { |
| 783 | uint32_t reg; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 784 | uint32_t cmd; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 785 | |
| 786 | if (ehci_hcd_init() != 0) |
| 787 | return -1; |
| 788 | |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 789 | /* EHCI spec section 4.1 */ |
| 790 | if (ehci_reset() != 0) |
| 791 | return -1; |
| 792 | |
Stefan Roese | 2e98fc7 | 2009-01-21 17:12:10 +0100 | [diff] [blame] | 793 | #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET) |
| 794 | if (ehci_hcd_init() != 0) |
| 795 | return -1; |
| 796 | #endif |
| 797 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 798 | /* Set head of reclaim list */ |
| 799 | memset(&qh_list, 0, sizeof(qh_list)); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 800 | qh_list.qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH); |
| 801 | qh_list.qh_endpt1 = cpu_to_hc32((1 << 15) | (USB_SPEED_HIGH << 12)); |
| 802 | qh_list.qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 803 | qh_list.qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 804 | qh_list.qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 805 | qh_list.qh_overlay.qt_token = cpu_to_hc32(0x40); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 806 | |
| 807 | /* Set async. queue head pointer. */ |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 808 | ehci_writel(&hcor->or_asynclistaddr, (uint32_t)&qh_list); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 809 | |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 810 | reg = ehci_readl(&hccr->cr_hcsparams); |
| 811 | descriptor.hub.bNbrPorts = HCS_N_PORTS(reg); |
| 812 | printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 813 | /* Port Indicators */ |
| 814 | if (HCS_INDICATOR(reg)) |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 815 | descriptor.hub.wHubCharacteristics |= 0x80; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 816 | /* Port Power Control */ |
| 817 | if (HCS_PPC(reg)) |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 818 | descriptor.hub.wHubCharacteristics |= 0x01; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 819 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 820 | /* Start the host controller. */ |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 821 | cmd = ehci_readl(&hcor->or_usbcmd); |
Wolfgang Denk | fb718e1 | 2009-02-12 00:08:39 +0100 | [diff] [blame] | 822 | /* |
| 823 | * Philips, Intel, and maybe others need CMD_RUN before the |
| 824 | * root hub will detect new devices (why?); NEC doesn't |
| 825 | */ |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 826 | cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET); |
| 827 | cmd |= CMD_RUN; |
| 828 | ehci_writel(&hcor->or_usbcmd, cmd); |
| 829 | |
| 830 | /* take control over the ports */ |
| 831 | cmd = ehci_readl(&hcor->or_configflag); |
| 832 | cmd |= FLAG_CF; |
| 833 | ehci_writel(&hcor->or_configflag, cmd); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 834 | /* unblock posted write */ |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 835 | cmd = ehci_readl(&hcor->or_usbcmd); |
| 836 | wait_ms(5); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 837 | reg = HC_VERSION(ehci_readl(&hccr->cr_capbase)); |
| 838 | printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 839 | |
| 840 | rootdev = 0; |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | int |
| 846 | submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 847 | int length) |
| 848 | { |
| 849 | |
| 850 | if (usb_pipetype(pipe) != PIPE_BULK) { |
| 851 | debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe)); |
| 852 | return -1; |
| 853 | } |
| 854 | return ehci_submit_async(dev, pipe, buffer, length, NULL); |
| 855 | } |
| 856 | |
| 857 | int |
| 858 | submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 859 | int length, struct devrequest *setup) |
| 860 | { |
| 861 | |
| 862 | if (usb_pipetype(pipe) != PIPE_CONTROL) { |
| 863 | debug("non-control pipe (type=%lu)", usb_pipetype(pipe)); |
| 864 | return -1; |
| 865 | } |
| 866 | |
| 867 | if (usb_pipedevice(pipe) == rootdev) { |
| 868 | if (rootdev == 0) |
| 869 | dev->speed = USB_SPEED_HIGH; |
| 870 | return ehci_submit_root(dev, pipe, buffer, length, setup); |
| 871 | } |
| 872 | return ehci_submit_async(dev, pipe, buffer, length, setup); |
| 873 | } |
| 874 | |
| 875 | int |
| 876 | submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 877 | int length, int interval) |
| 878 | { |
| 879 | |
| 880 | debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d", |
| 881 | dev, pipe, buffer, length, interval); |
| 882 | return -1; |
| 883 | } |