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