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