blob: d90e94d8109d02065b88afc3c3fd0fca0824f5c7 [file] [log] [blame]
Michael Trimarchi241f7512008-11-28 13:20:46 +01001/*-
2 * Copyright (c) 2007-2008, Juniper Networks, Inc.
michael0a326102008-12-10 17:55:19 +01003 * Copyright (c) 2008, Excito Elektronik i Skåne AB
Remy Böhmer33e87482008-12-13 22:51:58 +01004 * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
5 *
Michael Trimarchi241f7512008-11-28 13:20:46 +01006 * 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 Trimarchi241f7512008-11-28 13:20:46 +010023#include <common.h>
michael0a326102008-12-10 17:55:19 +010024#include <asm/byteorder.h>
Lucas Stach835e11e2012-09-06 08:00:13 +020025#include <asm/unaligned.h>
Michael Trimarchi241f7512008-11-28 13:20:46 +010026#include <usb.h>
27#include <asm/io.h>
michael0a326102008-12-10 17:55:19 +010028#include <malloc.h>
Stefan Roese86b34cf2010-11-26 15:43:28 +010029#include <watchdog.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020030
31#include "ehci.h"
Michael Trimarchi241f7512008-11-28 13:20:46 +010032
Lucas Stach3494a4c2012-09-26 00:14:35 +020033#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
34#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
35#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +010036
Lucas Stach3494a4c2012-09-26 00:14:35 +020037static struct ehci_ctrl {
38 struct ehci_hccr *hccr; /* R/O registers, not need for volatile */
39 struct ehci_hcor *hcor;
40 int rootdev;
41 uint16_t portreset;
42 struct QH qh_list __attribute__((aligned(USB_DMA_MINALIGN)));
43} ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
Tom Rini2cabcf72012-07-15 22:14:24 +000044
45#define ALIGN_END_ADDR(type, ptr, size) \
46 ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchi241f7512008-11-28 13:20:46 +010047
michael0a326102008-12-10 17:55:19 +010048static struct descriptor {
49 struct usb_hub_descriptor hub;
50 struct usb_device_descriptor device;
51 struct usb_linux_config_descriptor config;
52 struct usb_linux_interface_descriptor interface;
53 struct usb_endpoint_descriptor endpoint;
54} __attribute__ ((packed)) descriptor = {
55 {
56 0x8, /* bDescLength */
57 0x29, /* bDescriptorType: hub descriptor */
58 2, /* bNrPorts -- runtime modified */
59 0, /* wHubCharacteristics */
Vincent Palatin8277b502011-12-05 14:52:22 -080060 10, /* bPwrOn2PwrGood */
michael0a326102008-12-10 17:55:19 +010061 0, /* bHubCntrCurrent */
62 {}, /* Device removable */
63 {} /* at most 7 ports! XXX */
64 },
65 {
66 0x12, /* bLength */
67 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyovfa30a272010-02-27 21:29:42 +030068 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michael0a326102008-12-10 17:55:19 +010069 9, /* bDeviceClass: UDCLASS_HUB */
70 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
71 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
72 64, /* bMaxPacketSize: 64 bytes */
73 0x0000, /* idVendor */
74 0x0000, /* idProduct */
Sergei Shtylyovfa30a272010-02-27 21:29:42 +030075 cpu_to_le16(0x0100), /* bcdDevice */
michael0a326102008-12-10 17:55:19 +010076 1, /* iManufacturer */
77 2, /* iProduct */
78 0, /* iSerialNumber */
79 1 /* bNumConfigurations: 1 */
80 },
81 {
82 0x9,
83 2, /* bDescriptorType: UDESC_CONFIG */
84 cpu_to_le16(0x19),
85 1, /* bNumInterface */
86 1, /* bConfigurationValue */
87 0, /* iConfiguration */
88 0x40, /* bmAttributes: UC_SELF_POWER */
89 0 /* bMaxPower */
90 },
91 {
92 0x9, /* bLength */
93 4, /* bDescriptorType: UDESC_INTERFACE */
94 0, /* bInterfaceNumber */
95 0, /* bAlternateSetting */
96 1, /* bNumEndpoints */
97 9, /* bInterfaceClass: UICLASS_HUB */
98 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
99 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
100 0 /* iInterface */
101 },
102 {
103 0x7, /* bLength */
104 5, /* bDescriptorType: UDESC_ENDPOINT */
105 0x81, /* bEndpointAddress:
106 * UE_DIR_IN | EHCI_INTR_ENDPT
107 */
108 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix83b9e1d2009-10-31 12:37:38 -0500109 8, /* wMaxPacketSize */
michael0a326102008-12-10 17:55:19 +0100110 255 /* bInterval */
111 },
Michael Trimarchi241f7512008-11-28 13:20:46 +0100112};
113
Remy Böhmer33e87482008-12-13 22:51:58 +0100114#if defined(CONFIG_EHCI_IS_TDI)
115#define ehci_is_TDI() (1)
116#else
117#define ehci_is_TDI() (0)
118#endif
119
Marek Vasut09734772011-07-11 02:37:01 +0200120void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
121{
122 mdelay(50);
123}
124
125void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
126 __attribute__((weak, alias("__ehci_powerup_fixup")));
127
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100128static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michael0a326102008-12-10 17:55:19 +0100129{
michael0bf2a032008-12-11 13:43:55 +0100130 uint32_t result;
131 do {
132 result = ehci_readl(ptr);
Wolfgang Denkcdc5a7a2010-10-22 14:23:00 +0200133 udelay(5);
michael0bf2a032008-12-11 13:43:55 +0100134 if (result == ~(uint32_t)0)
135 return -1;
136 result &= mask;
137 if (result == done)
138 return 0;
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100139 usec--;
140 } while (usec > 0);
michael0bf2a032008-12-11 13:43:55 +0100141 return -1;
142}
143
Lucas Stach3494a4c2012-09-26 00:14:35 +0200144static int ehci_reset(int index)
michael0bf2a032008-12-11 13:43:55 +0100145{
146 uint32_t cmd;
147 uint32_t tmp;
148 uint32_t *reg_ptr;
149 int ret = 0;
150
Lucas Stach3494a4c2012-09-26 00:14:35 +0200151 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Stefan Roese745af442010-11-26 15:44:00 +0100152 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200153 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
154 ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
155 CMD_RESET, 0, 250 * 1000);
michael0bf2a032008-12-11 13:43:55 +0100156 if (ret < 0) {
157 printf("EHCI fail to reset\n");
158 goto out;
159 }
160
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100161 if (ehci_is_TDI()) {
Lucas Stach3494a4c2012-09-26 00:14:35 +0200162 reg_ptr = (uint32_t *)((u8 *)ehcic[index].hcor + USBMODE);
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100163 tmp = ehci_readl(reg_ptr);
164 tmp |= USBMODE_CM_HC;
Remy Böhmer33e87482008-12-13 22:51:58 +0100165#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100166 tmp |= USBMODE_BE;
michael0bf2a032008-12-11 13:43:55 +0100167#endif
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100168 ehci_writel(reg_ptr, tmp);
169 }
Simon Glass5978cdb2012-02-27 10:52:47 +0000170
171#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Lucas Stach3494a4c2012-09-26 00:14:35 +0200172 cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200173 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass5978cdb2012-02-27 10:52:47 +0000174 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Lucas Stach3494a4c2012-09-26 00:14:35 +0200175 ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
Simon Glass5978cdb2012-02-27 10:52:47 +0000176#endif
michael0bf2a032008-12-11 13:43:55 +0100177out:
178 return ret;
michael0a326102008-12-10 17:55:19 +0100179}
Michael Trimarchi241f7512008-11-28 13:20:46 +0100180
Michael Trimarchi241f7512008-11-28 13:20:46 +0100181static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
182{
Marek Vasutff24dc32012-04-09 04:07:46 +0200183 uint32_t delta, next;
184 uint32_t addr = (uint32_t)buf;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100185 int idx;
186
Ilya Yanokfb113712012-07-15 04:43:49 +0000187 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutff24dc32012-04-09 04:07:46 +0200188 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
189
Ilya Yanokfb113712012-07-15 04:43:49 +0000190 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
191
Michael Trimarchi241f7512008-11-28 13:20:46 +0100192 idx = 0;
Benoît Thébaudeaue68f48a2012-07-19 22:16:38 +0200193 while (idx < QT_BUFFER_CNT) {
michael0a326102008-12-10 17:55:19 +0100194 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denkebb829f2010-10-19 16:13:15 +0200195 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200196 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100197 delta = next - addr;
198 if (delta >= sz)
199 break;
200 sz -= delta;
201 addr = next;
202 idx++;
203 }
204
Benoît Thébaudeaue68f48a2012-07-19 22:16:38 +0200205 if (idx == QT_BUFFER_CNT) {
Ilya Yanok84570d62012-07-15 04:43:52 +0000206 printf("out of buffer pointers (%u bytes left)\n", sz);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100207 return -1;
208 }
209
210 return 0;
211}
212
213static int
214ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
215 int length, struct devrequest *req)
216{
Tom Rini2cabcf72012-07-15 22:14:24 +0000217 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200218 struct qTD *qtd;
219 int qtd_count = 0;
Marek Vasut4f668312012-04-08 23:32:05 +0200220 int qtd_counter = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100221 volatile struct qTD *vtd;
222 unsigned long ts;
223 uint32_t *tdp;
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200224 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100225 uint32_t c, toggle;
michael0a326102008-12-10 17:55:19 +0100226 uint32_t cmd;
Simon Glassfd7f5132011-02-07 14:42:16 -0800227 int timeout;
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100228 int ret = 0;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200229 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100230
michael0a326102008-12-10 17:55:19 +0100231 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchi241f7512008-11-28 13:20:46 +0100232 buffer, length, req);
233 if (req != NULL)
michael0a326102008-12-10 17:55:19 +0100234 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100235 req->request, req->request,
236 req->requesttype, req->requesttype,
237 le16_to_cpu(req->value), le16_to_cpu(req->value),
michael0a326102008-12-10 17:55:19 +0100238 le16_to_cpu(req->index));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100239
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200240#define PKT_ALIGN 512
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200241 /*
242 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
243 * described by a transfer descriptor (the qTD). The qTDs form a linked
244 * list with a queue head (QH).
245 *
246 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
247 * have its beginning in a qTD transfer and its end in the following
248 * one, so the qTD transfer lengths have to be chosen accordingly.
249 *
250 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
251 * single pages. The first data buffer can start at any offset within a
252 * page (not considering the cache-line alignment issues), while the
253 * following buffers must be page-aligned. There is no alignment
254 * constraint on the size of a qTD transfer.
255 */
256 if (req != NULL)
257 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
258 qtd_count += 1 + 1;
259 if (length > 0 || req == NULL) {
260 /*
261 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200262 * data payload (not considering the first qTD transfer, which
263 * may be longer or shorter, and the final one, which may be
264 * shorter).
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200265 *
266 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200267 * transfer size is aligned to PKT_ALIGN, which is a multiple of
268 * wMaxPacketSize (except in some cases for interrupt transfers,
269 * see comment in submit_int_msg()).
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200270 *
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200271 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200272 * QT_BUFFER_CNT full pages will be used.
273 */
274 int xfr_sz = QT_BUFFER_CNT;
275 /*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200276 * However, if the input buffer is not aligned to PKT_ALIGN, the
277 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200278 * data buffer of each transfer will be page-unaligned.
279 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200280 if ((uint32_t)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200281 xfr_sz--;
282 /* Convert the qTD transfer size to bytes. */
283 xfr_sz *= EHCI_PAGE_SIZE;
284 /*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200285 * Approximate by excess the number of qTDs that will be
286 * required for the data payload. The exact formula is way more
287 * complicated and saves at most 2 qTDs, i.e. a total of 128
288 * bytes.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200289 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200290 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200291 }
292/*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200293 * Threshold value based on the worst-case total size of the allocated qTDs for
294 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200295 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200296#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200297#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
298#endif
299 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
300 if (qtd == NULL) {
301 printf("unable to allocate TDs\n");
302 return -1;
303 }
304
Tom Rini2cabcf72012-07-15 22:14:24 +0000305 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200306 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasut4f668312012-04-08 23:32:05 +0200307
Marek Vasutff24dc32012-04-09 04:07:46 +0200308 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
309
Marek Vasut285c8b32012-04-09 04:13:00 +0200310 /*
311 * Setup QH (3.6 in ehci-r10.pdf)
312 *
313 * qh_link ................. 03-00 H
314 * qh_endpt1 ............... 07-04 H
315 * qh_endpt2 ............... 0B-08 H
316 * - qh_curtd
317 * qh_overlay.qt_next ...... 13-10 H
318 * - qh_overlay.qt_altnext
319 */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200320 qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200321 c = usb_pipespeed(pipe) != USB_SPEED_HIGH && !usb_pipeendpoint(pipe);
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200322 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200323 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200324 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200325 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
326 QH_ENDPT1_EPS(usb_pipespeed(pipe)) |
327 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
328 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini2cabcf72012-07-15 22:14:24 +0000329 qh->qh_endpt1 = cpu_to_hc32(endpt);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200330 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
331 QH_ENDPT2_HUBADDR(dev->parent->devnum) |
332 QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini2cabcf72012-07-15 22:14:24 +0000333 qh->qh_endpt2 = cpu_to_hc32(endpt);
334 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100335
Tom Rini2cabcf72012-07-15 22:14:24 +0000336 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100337
Michael Trimarchi241f7512008-11-28 13:20:46 +0100338 if (req != NULL) {
Marek Vasut285c8b32012-04-09 04:13:00 +0200339 /*
340 * Setup request qTD (3.5 in ehci-r10.pdf)
341 *
342 * qt_next ................ 03-00 H
343 * qt_altnext ............. 07-04 H
344 * qt_token ............... 0B-08 H
345 *
346 * [ buffer, buffer_hi ] loaded with "req".
347 */
Marek Vasut4f668312012-04-08 23:32:05 +0200348 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
349 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200350 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
351 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
352 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
353 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasut4f668312012-04-08 23:32:05 +0200354 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200355 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
356 printf("unable to construct SETUP TD\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100357 goto fail;
358 }
Marek Vasut285c8b32012-04-09 04:13:00 +0200359 /* Update previous qTD! */
Marek Vasut4f668312012-04-08 23:32:05 +0200360 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
361 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100362 toggle = 1;
363 }
364
365 if (length > 0 || req == NULL) {
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200366 uint8_t *buf_ptr = buffer;
367 int left_length = length;
368
369 do {
370 /*
371 * Determine the size of this qTD transfer. By default,
372 * QT_BUFFER_CNT full pages can be used.
373 */
374 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
375 /*
376 * However, if the input buffer is not page-aligned, the
377 * portion of the first page before the buffer start
378 * offset within that page is unusable.
379 */
380 xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
381 /*
382 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200383 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200384 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200385 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200386 /*
387 * This transfer may be shorter than the available qTD
388 * transfer size that has just been computed.
389 */
390 xfr_bytes = min(xfr_bytes, left_length);
391
392 /*
393 * Setup request qTD (3.5 in ehci-r10.pdf)
394 *
395 * qt_next ................ 03-00 H
396 * qt_altnext ............. 07-04 H
397 * qt_token ............... 0B-08 H
398 *
399 * [ buffer, buffer_hi ] loaded with "buffer".
400 */
401 qtd[qtd_counter].qt_next =
402 cpu_to_hc32(QT_NEXT_TERMINATE);
403 qtd[qtd_counter].qt_altnext =
404 cpu_to_hc32(QT_NEXT_TERMINATE);
405 token = QT_TOKEN_DT(toggle) |
406 QT_TOKEN_TOTALBYTES(xfr_bytes) |
407 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
408 QT_TOKEN_CERR(3) |
409 QT_TOKEN_PID(usb_pipein(pipe) ?
410 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
411 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
412 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
413 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
414 xfr_bytes)) {
415 printf("unable to construct DATA TD\n");
416 goto fail;
417 }
418 /* Update previous qTD! */
419 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
420 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200421 /*
422 * Data toggle has to be adjusted since the qTD transfer
423 * size is not always an even multiple of
424 * wMaxPacketSize.
425 */
426 if ((xfr_bytes / maxpacket) & 1)
427 toggle ^= 1;
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200428 buf_ptr += xfr_bytes;
429 left_length -= xfr_bytes;
430 } while (left_length > 0);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100431 }
432
433 if (req != NULL) {
Marek Vasut285c8b32012-04-09 04:13:00 +0200434 /*
435 * Setup request qTD (3.5 in ehci-r10.pdf)
436 *
437 * qt_next ................ 03-00 H
438 * qt_altnext ............. 07-04 H
439 * qt_token ............... 0B-08 H
440 */
Marek Vasut4f668312012-04-08 23:32:05 +0200441 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
442 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200443 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200444 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
445 QT_TOKEN_PID(usb_pipein(pipe) ?
446 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
447 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasut4f668312012-04-08 23:32:05 +0200448 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut285c8b32012-04-09 04:13:00 +0200449 /* Update previous qTD! */
Marek Vasut4f668312012-04-08 23:32:05 +0200450 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
451 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100452 }
453
Lucas Stach3494a4c2012-09-26 00:14:35 +0200454 ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100455
Stefan Roese25983c12009-01-21 17:12:19 +0100456 /* Flush dcache */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200457 flush_dcache_range((uint32_t)&ctrl->qh_list,
458 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Tom Rini2cabcf72012-07-15 22:14:24 +0000459 flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200460 flush_dcache_range((uint32_t)qtd,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200461 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roese25983c12009-01-21 17:12:19 +0100462
Ilya Yanok84309bb2012-07-15 22:12:08 +0000463 /* Set async. queue head pointer. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200464 ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
Ilya Yanok84309bb2012-07-15 22:12:08 +0000465
Lucas Stach3494a4c2012-09-26 00:14:35 +0200466 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
467 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100468
469 /* Enable async. schedule. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200470 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael0bf2a032008-12-11 13:43:55 +0100471 cmd |= CMD_ASE;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200472 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0a326102008-12-10 17:55:19 +0100473
Lucas Stach3494a4c2012-09-26 00:14:35 +0200474 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100475 100 * 1000);
476 if (ret < 0) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200477 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100478 goto fail;
michael0bf2a032008-12-11 13:43:55 +0100479 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100480
481 /* Wait for TDs to be processed. */
482 ts = get_timer(0);
Marek Vasut4f668312012-04-08 23:32:05 +0200483 vtd = &qtd[qtd_counter - 1];
Simon Glassfd7f5132011-02-07 14:42:16 -0800484 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100485 do {
Stefan Roese25983c12009-01-21 17:12:19 +0100486 /* Invalidate dcache */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200487 invalidate_dcache_range((uint32_t)&ctrl->qh_list,
488 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Tom Rini2cabcf72012-07-15 22:14:24 +0000489 invalidate_dcache_range((uint32_t)qh,
490 ALIGN_END_ADDR(struct QH, qh, 1));
Marek Vasutff24dc32012-04-09 04:07:46 +0200491 invalidate_dcache_range((uint32_t)qtd,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200492 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutff24dc32012-04-09 04:07:46 +0200493
michael0a326102008-12-10 17:55:19 +0100494 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200495 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100496 break;
Stefan Roese86b34cf2010-11-26 15:43:28 +0100497 WATCHDOG_RESET();
Simon Glassfd7f5132011-02-07 14:42:16 -0800498 } while (get_timer(ts) < timeout);
499
Ilya Yanokfb113712012-07-15 04:43:49 +0000500 /*
501 * Invalidate the memory area occupied by buffer
502 * Don't try to fix the buffer alignment, if it isn't properly
503 * aligned it's upper layer's fault so let invalidate_dcache_range()
504 * vow about it. But we have to fix the length as it's actual
505 * transfer length and can be unaligned. This is potentially
506 * dangerous operation, it's responsibility of the calling
507 * code to make sure enough space is reserved.
508 */
509 invalidate_dcache_range((uint32_t)buffer,
510 ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutff24dc32012-04-09 04:07:46 +0200511
Simon Glassfd7f5132011-02-07 14:42:16 -0800512 /* Check that the TD processing happened */
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200513 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glassfd7f5132011-02-07 14:42:16 -0800514 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100515
516 /* Disable async schedule. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200517 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael0a326102008-12-10 17:55:19 +0100518 cmd &= ~CMD_ASE;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200519 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0bf2a032008-12-11 13:43:55 +0100520
Lucas Stach3494a4c2012-09-26 00:14:35 +0200521 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100522 100 * 1000);
523 if (ret < 0) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200524 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100525 goto fail;
michael0bf2a032008-12-11 13:43:55 +0100526 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100527
Tom Rini2cabcf72012-07-15 22:14:24 +0000528 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200529 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michael0a326102008-12-10 17:55:19 +0100530 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200531 switch (QT_TOKEN_GET_STATUS(token) &
532 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100533 case 0:
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200534 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100535 usb_settoggle(dev, usb_pipeendpoint(pipe),
536 usb_pipeout(pipe), toggle);
537 dev->status = 0;
538 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200539 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100540 dev->status = USB_ST_STALLED;
541 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200542 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
543 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100544 dev->status = USB_ST_BUF_ERR;
545 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200546 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
547 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100548 dev->status = USB_ST_BABBLE_DET;
549 break;
550 default:
551 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200552 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschine1e09312010-11-02 11:47:29 +0100553 dev->status |= USB_ST_STALLED;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100554 break;
555 }
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200556 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100557 } else {
558 dev->act_len = 0;
michael0a326102008-12-10 17:55:19 +0100559 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach3494a4c2012-09-26 00:14:35 +0200560 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
561 ehci_readl(&ctrl->hcor->or_portsc[0]),
562 ehci_readl(&ctrl->hcor->or_portsc[1]));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100563 }
564
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200565 free(qtd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100566 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
567
568fail:
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200569 free(qtd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100570 return -1;
571}
572
573static inline int min3(int a, int b, int c)
574{
575
576 if (b < a)
577 a = b;
578 if (c < a)
579 a = c;
580 return a;
581}
582
michael0a326102008-12-10 17:55:19 +0100583int
Michael Trimarchi241f7512008-11-28 13:20:46 +0100584ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
585 int length, struct devrequest *req)
586{
587 uint8_t tmpbuf[4];
588 u16 typeReq;
michael0a326102008-12-10 17:55:19 +0100589 void *srcptr = NULL;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100590 int len, srclen;
591 uint32_t reg;
Remy Böhmer33e87482008-12-13 22:51:58 +0100592 uint32_t *status_reg;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200593 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100594
Sergei Shtylyov9de4ac42010-02-27 21:32:17 +0300595 if (le16_to_cpu(req->index) > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
Remy Böhmer33e87482008-12-13 22:51:58 +0100596 printf("The request port(%d) is not configured\n",
597 le16_to_cpu(req->index) - 1);
598 return -1;
599 }
Lucas Stach3494a4c2012-09-26 00:14:35 +0200600 status_reg = (uint32_t *)&ctrl->hcor->or_portsc[
Remy Böhmer33e87482008-12-13 22:51:58 +0100601 le16_to_cpu(req->index) - 1];
Michael Trimarchi241f7512008-11-28 13:20:46 +0100602 srclen = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100603
michael0a326102008-12-10 17:55:19 +0100604 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100605 req->request, req->request,
606 req->requesttype, req->requesttype,
607 le16_to_cpu(req->value), le16_to_cpu(req->index));
608
Prafulla Wadaskar22810292009-07-17 19:56:30 +0530609 typeReq = req->request | req->requesttype << 8;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100610
Prafulla Wadaskar22810292009-07-17 19:56:30 +0530611 switch (typeReq) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100612 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
613 switch (le16_to_cpu(req->value) >> 8) {
614 case USB_DT_DEVICE:
michael0a326102008-12-10 17:55:19 +0100615 debug("USB_DT_DEVICE request\n");
616 srcptr = &descriptor.device;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200617 srclen = descriptor.device.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100618 break;
619 case USB_DT_CONFIG:
michael0a326102008-12-10 17:55:19 +0100620 debug("USB_DT_CONFIG config\n");
621 srcptr = &descriptor.config;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200622 srclen = descriptor.config.bLength +
623 descriptor.interface.bLength +
624 descriptor.endpoint.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100625 break;
626 case USB_DT_STRING:
michael0a326102008-12-10 17:55:19 +0100627 debug("USB_DT_STRING config\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100628 switch (le16_to_cpu(req->value) & 0xff) {
629 case 0: /* Language */
630 srcptr = "\4\3\1\0";
631 srclen = 4;
632 break;
633 case 1: /* Vendor */
634 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
635 srclen = 14;
636 break;
637 case 2: /* Product */
638 srcptr = "\52\3E\0H\0C\0I\0 "
639 "\0H\0o\0s\0t\0 "
640 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
641 srclen = 42;
642 break;
643 default:
michael0a326102008-12-10 17:55:19 +0100644 debug("unknown value DT_STRING %x\n",
645 le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100646 goto unknown;
647 }
648 break;
649 default:
michael0a326102008-12-10 17:55:19 +0100650 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100651 goto unknown;
652 }
653 break;
654 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
655 switch (le16_to_cpu(req->value) >> 8) {
656 case USB_DT_HUB:
michael0a326102008-12-10 17:55:19 +0100657 debug("USB_DT_HUB config\n");
658 srcptr = &descriptor.hub;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200659 srclen = descriptor.hub.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100660 break;
661 default:
michael0a326102008-12-10 17:55:19 +0100662 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100663 goto unknown;
664 }
665 break;
666 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michael0a326102008-12-10 17:55:19 +0100667 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach3494a4c2012-09-26 00:14:35 +0200668 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100669 break;
670 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michael0a326102008-12-10 17:55:19 +0100671 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100672 /* Nothing to do */
673 break;
674 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
675 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
676 tmpbuf[1] = 0;
677 srcptr = tmpbuf;
678 srclen = 2;
679 break;
michael0a326102008-12-10 17:55:19 +0100680 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchi241f7512008-11-28 13:20:46 +0100681 memset(tmpbuf, 0, 4);
Remy Böhmer33e87482008-12-13 22:51:58 +0100682 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100683 if (reg & EHCI_PS_CS)
684 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
685 if (reg & EHCI_PS_PE)
686 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
687 if (reg & EHCI_PS_SUSP)
688 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
689 if (reg & EHCI_PS_OCA)
690 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300691 if (reg & EHCI_PS_PR)
692 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100693 if (reg & EHCI_PS_PP)
694 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese497f1842009-01-21 17:12:01 +0100695
696 if (ehci_is_TDI()) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200697 switch (PORTSC_PSPD(reg)) {
698 case PORTSC_PSPD_FS:
Stefan Roese497f1842009-01-21 17:12:01 +0100699 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200700 case PORTSC_PSPD_LS:
Stefan Roese497f1842009-01-21 17:12:01 +0100701 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
702 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200703 case PORTSC_PSPD_HS:
Stefan Roese497f1842009-01-21 17:12:01 +0100704 default:
705 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
706 break;
707 }
708 } else {
709 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
710 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100711
712 if (reg & EHCI_PS_CSC)
713 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
714 if (reg & EHCI_PS_PEC)
715 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
716 if (reg & EHCI_PS_OCC)
717 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200718 if (ctrl->portreset & (1 << le16_to_cpu(req->index)))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100719 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmer33e87482008-12-13 22:51:58 +0100720
Michael Trimarchi241f7512008-11-28 13:20:46 +0100721 srcptr = tmpbuf;
722 srclen = 4;
723 break;
michael0a326102008-12-10 17:55:19 +0100724 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmer33e87482008-12-13 22:51:58 +0100725 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100726 reg &= ~EHCI_PS_CLEAR;
727 switch (le16_to_cpu(req->value)) {
michael0bf2a032008-12-11 13:43:55 +0100728 case USB_PORT_FEAT_ENABLE:
729 reg |= EHCI_PS_PE;
Remy Böhmer33e87482008-12-13 22:51:58 +0100730 ehci_writel(status_reg, reg);
michael0bf2a032008-12-11 13:43:55 +0100731 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100732 case USB_PORT_FEAT_POWER:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200733 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmer33e87482008-12-13 22:51:58 +0100734 reg |= EHCI_PS_PP;
735 ehci_writel(status_reg, reg);
736 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100737 break;
738 case USB_PORT_FEAT_RESET:
Remy Böhmer33e87482008-12-13 22:51:58 +0100739 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
740 !ehci_is_TDI() &&
741 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100742 /* Low speed device, give up ownership. */
Remy Böhmer33e87482008-12-13 22:51:58 +0100743 debug("port %d low speed --> companion\n",
744 req->index - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100745 reg |= EHCI_PS_PO;
Remy Böhmer33e87482008-12-13 22:51:58 +0100746 ehci_writel(status_reg, reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100747 break;
Remy Böhmer33e87482008-12-13 22:51:58 +0100748 } else {
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300749 int ret;
750
Remy Böhmer33e87482008-12-13 22:51:58 +0100751 reg |= EHCI_PS_PR;
752 reg &= ~EHCI_PS_PE;
753 ehci_writel(status_reg, reg);
754 /*
755 * caller must wait, then call GetPortStatus
756 * usb 2.0 specification say 50 ms resets on
757 * root
758 */
Marek Vasut09734772011-07-11 02:37:01 +0200759 ehci_powerup_fixup(status_reg, &reg);
760
Chris Zhangfddf6d62010-01-06 13:34:04 -0800761 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300762 /*
763 * A host controller must terminate the reset
764 * and stabilize the state of the port within
765 * 2 milliseconds
766 */
767 ret = handshake(status_reg, EHCI_PS_PR, 0,
768 2 * 1000);
769 if (!ret)
Lucas Stach3494a4c2012-09-26 00:14:35 +0200770 ctrl->portreset |=
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300771 1 << le16_to_cpu(req->index);
772 else
773 printf("port(%d) reset error\n",
774 le16_to_cpu(req->index) - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100775 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100776 break;
777 default:
michael0a326102008-12-10 17:55:19 +0100778 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100779 goto unknown;
780 }
Remy Böhmer33e87482008-12-13 22:51:58 +0100781 /* unblock posted writes */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200782 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100783 break;
michael0a326102008-12-10 17:55:19 +0100784 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmer33e87482008-12-13 22:51:58 +0100785 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100786 switch (le16_to_cpu(req->value)) {
787 case USB_PORT_FEAT_ENABLE:
788 reg &= ~EHCI_PS_PE;
789 break;
Remy Böhmer33e87482008-12-13 22:51:58 +0100790 case USB_PORT_FEAT_C_ENABLE:
791 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_PE;
792 break;
793 case USB_PORT_FEAT_POWER:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200794 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Remy Böhmer33e87482008-12-13 22:51:58 +0100795 reg = reg & ~(EHCI_PS_CLEAR | EHCI_PS_PP);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100796 case USB_PORT_FEAT_C_CONNECTION:
Remy Böhmer33e87482008-12-13 22:51:58 +0100797 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_CSC;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100798 break;
michael0bf2a032008-12-11 13:43:55 +0100799 case USB_PORT_FEAT_OVER_CURRENT:
Remy Böhmer33e87482008-12-13 22:51:58 +0100800 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_OCC;
michael0bf2a032008-12-11 13:43:55 +0100801 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100802 case USB_PORT_FEAT_C_RESET:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200803 ctrl->portreset &= ~(1 << le16_to_cpu(req->index));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100804 break;
805 default:
michael0a326102008-12-10 17:55:19 +0100806 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100807 goto unknown;
808 }
Remy Böhmer33e87482008-12-13 22:51:58 +0100809 ehci_writel(status_reg, reg);
810 /* unblock posted write */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200811 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100812 break;
813 default:
michael0a326102008-12-10 17:55:19 +0100814 debug("Unknown request\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100815 goto unknown;
816 }
817
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000818 mdelay(1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100819 len = min3(srclen, le16_to_cpu(req->length), length);
820 if (srcptr != NULL && len > 0)
821 memcpy(buffer, srcptr, len);
michael0a326102008-12-10 17:55:19 +0100822 else
823 debug("Len is 0\n");
824
Michael Trimarchi241f7512008-11-28 13:20:46 +0100825 dev->act_len = len;
826 dev->status = 0;
827 return 0;
828
829unknown:
michael0a326102008-12-10 17:55:19 +0100830 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100831 req->requesttype, req->request, le16_to_cpu(req->value),
832 le16_to_cpu(req->index), le16_to_cpu(req->length));
833
834 dev->act_len = 0;
835 dev->status = USB_ST_STALLED;
836 return -1;
837}
838
Lucas Stacha3231282012-09-26 00:14:34 +0200839int usb_lowlevel_stop(int index)
Michael Trimarchi241f7512008-11-28 13:20:46 +0100840{
Lucas Stach3494a4c2012-09-26 00:14:35 +0200841 return ehci_hcd_stop(index);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100842}
843
Lucas Stacha3231282012-09-26 00:14:34 +0200844int usb_lowlevel_init(int index, void **controller)
Michael Trimarchi241f7512008-11-28 13:20:46 +0100845{
846 uint32_t reg;
michael0a326102008-12-10 17:55:19 +0100847 uint32_t cmd;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200848 struct QH *qh_list;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100849
Lucas Stach3494a4c2012-09-26 00:14:35 +0200850 if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100851 return -1;
852
michael0bf2a032008-12-11 13:43:55 +0100853 /* EHCI spec section 4.1 */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200854 if (ehci_reset(index))
michael0bf2a032008-12-11 13:43:55 +0100855 return -1;
856
Stefan Roese2e98fc72009-01-21 17:12:10 +0100857#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
Lucas Stach3494a4c2012-09-26 00:14:35 +0200858 if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
Stefan Roese2e98fc72009-01-21 17:12:10 +0100859 return -1;
860#endif
861
Lucas Stach3494a4c2012-09-26 00:14:35 +0200862 qh_list = &ehcic[index].qh_list;
863
Michael Trimarchi241f7512008-11-28 13:20:46 +0100864 /* Set head of reclaim list */
Tom Rini2cabcf72012-07-15 22:14:24 +0000865 memset(qh_list, 0, sizeof(*qh_list));
866 qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200867 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
868 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini2cabcf72012-07-15 22:14:24 +0000869 qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
870 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
871 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200872 qh_list->qh_overlay.qt_token =
873 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100874
Lucas Stach3494a4c2012-09-26 00:14:35 +0200875 reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
michael0bf2a032008-12-11 13:43:55 +0100876 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stachf5b34082012-09-28 00:26:19 +0200877 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmer33e87482008-12-13 22:51:58 +0100878 /* Port Indicators */
879 if (HCS_INDICATOR(reg))
Lucas Stach835e11e2012-09-06 08:00:13 +0200880 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
881 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmer33e87482008-12-13 22:51:58 +0100882 /* Port Power Control */
883 if (HCS_PPC(reg))
Lucas Stach835e11e2012-09-06 08:00:13 +0200884 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
885 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100886
Michael Trimarchi241f7512008-11-28 13:20:46 +0100887 /* Start the host controller. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200888 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Wolfgang Denkfb718e12009-02-12 00:08:39 +0100889 /*
890 * Philips, Intel, and maybe others need CMD_RUN before the
891 * root hub will detect new devices (why?); NEC doesn't
892 */
michael0bf2a032008-12-11 13:43:55 +0100893 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
894 cmd |= CMD_RUN;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200895 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
michael0bf2a032008-12-11 13:43:55 +0100896
897 /* take control over the ports */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200898 cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
michael0bf2a032008-12-11 13:43:55 +0100899 cmd |= FLAG_CF;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200900 ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
Remy Böhmer33e87482008-12-13 22:51:58 +0100901 /* unblock posted write */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200902 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000903 mdelay(5);
Lucas Stach3494a4c2012-09-26 00:14:35 +0200904 reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
Remy Böhmer33e87482008-12-13 22:51:58 +0100905 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100906
Lucas Stach3494a4c2012-09-26 00:14:35 +0200907 ehcic[index].rootdev = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100908
Lucas Stach3494a4c2012-09-26 00:14:35 +0200909 *controller = &ehcic[index];
Michael Trimarchi241f7512008-11-28 13:20:46 +0100910 return 0;
911}
912
913int
914submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
915 int length)
916{
917
918 if (usb_pipetype(pipe) != PIPE_BULK) {
919 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
920 return -1;
921 }
922 return ehci_submit_async(dev, pipe, buffer, length, NULL);
923}
924
925int
926submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
927 int length, struct devrequest *setup)
928{
Lucas Stach3494a4c2012-09-26 00:14:35 +0200929 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100930
931 if (usb_pipetype(pipe) != PIPE_CONTROL) {
932 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
933 return -1;
934 }
935
Lucas Stach3494a4c2012-09-26 00:14:35 +0200936 if (usb_pipedevice(pipe) == ctrl->rootdev) {
937 if (!ctrl->rootdev)
Michael Trimarchi241f7512008-11-28 13:20:46 +0100938 dev->speed = USB_SPEED_HIGH;
939 return ehci_submit_root(dev, pipe, buffer, length, setup);
940 }
941 return ehci_submit_async(dev, pipe, buffer, length, setup);
942}
943
944int
945submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
946 int length, int interval)
947{
Michael Trimarchi241f7512008-11-28 13:20:46 +0100948 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
949 dev, pipe, buffer, length, interval);
Benoît Thébaudeau58c4dfb2012-08-09 23:50:44 +0200950
951 /*
952 * Interrupt transfers requiring several transactions are not supported
953 * because bInterval is ignored.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200954 *
955 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200956 * <= PKT_ALIGN if several qTDs are required, while the USB
957 * specification does not constrain this for interrupt transfers. That
958 * means that ehci_submit_async() would support interrupt transfers
959 * requiring several transactions only as long as the transfer size does
960 * not require more than a single qTD.
Benoît Thébaudeau58c4dfb2012-08-09 23:50:44 +0200961 */
962 if (length > usb_maxpacket(dev, pipe)) {
963 printf("%s: Interrupt transfers requiring several transactions "
964 "are not supported.\n", __func__);
965 return -1;
966 }
Marek Vasut9b315fe2011-09-25 21:07:56 +0200967 return ehci_submit_async(dev, pipe, buffer, length, NULL);
968}