blob: fdad739724eacb545817ebaf8128bdba96a82c91 [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>
Patrick Georgie55fdac2013-03-06 14:08:31 +000024#include <errno.h>
michael0a326102008-12-10 17:55:19 +010025#include <asm/byteorder.h>
Lucas Stach835e11e2012-09-06 08:00:13 +020026#include <asm/unaligned.h>
Michael Trimarchi241f7512008-11-28 13:20:46 +010027#include <usb.h>
28#include <asm/io.h>
michael0a326102008-12-10 17:55:19 +010029#include <malloc.h>
Stefan Roese86b34cf2010-11-26 15:43:28 +010030#include <watchdog.h>
Patrick Georgie55fdac2013-03-06 14:08:31 +000031#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020032
33#include "ehci.h"
Michael Trimarchi241f7512008-11-28 13:20:46 +010034
Lucas Stach3494a4c2012-09-26 00:14:35 +020035#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
36#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
37#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +010038
Marek Vasutfd349a12013-07-10 03:16:31 +020039static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
Tom Rini2cabcf72012-07-15 22:14:24 +000040
41#define ALIGN_END_ADDR(type, ptr, size) \
42 ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchi241f7512008-11-28 13:20:46 +010043
michael0a326102008-12-10 17:55:19 +010044static struct descriptor {
45 struct usb_hub_descriptor hub;
46 struct usb_device_descriptor device;
47 struct usb_linux_config_descriptor config;
48 struct usb_linux_interface_descriptor interface;
49 struct usb_endpoint_descriptor endpoint;
50} __attribute__ ((packed)) descriptor = {
51 {
52 0x8, /* bDescLength */
53 0x29, /* bDescriptorType: hub descriptor */
54 2, /* bNrPorts -- runtime modified */
55 0, /* wHubCharacteristics */
Vincent Palatin8277b502011-12-05 14:52:22 -080056 10, /* bPwrOn2PwrGood */
michael0a326102008-12-10 17:55:19 +010057 0, /* bHubCntrCurrent */
58 {}, /* Device removable */
59 {} /* at most 7 ports! XXX */
60 },
61 {
62 0x12, /* bLength */
63 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyovfa30a272010-02-27 21:29:42 +030064 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michael0a326102008-12-10 17:55:19 +010065 9, /* bDeviceClass: UDCLASS_HUB */
66 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
67 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
68 64, /* bMaxPacketSize: 64 bytes */
69 0x0000, /* idVendor */
70 0x0000, /* idProduct */
Sergei Shtylyovfa30a272010-02-27 21:29:42 +030071 cpu_to_le16(0x0100), /* bcdDevice */
michael0a326102008-12-10 17:55:19 +010072 1, /* iManufacturer */
73 2, /* iProduct */
74 0, /* iSerialNumber */
75 1 /* bNumConfigurations: 1 */
76 },
77 {
78 0x9,
79 2, /* bDescriptorType: UDESC_CONFIG */
80 cpu_to_le16(0x19),
81 1, /* bNumInterface */
82 1, /* bConfigurationValue */
83 0, /* iConfiguration */
84 0x40, /* bmAttributes: UC_SELF_POWER */
85 0 /* bMaxPower */
86 },
87 {
88 0x9, /* bLength */
89 4, /* bDescriptorType: UDESC_INTERFACE */
90 0, /* bInterfaceNumber */
91 0, /* bAlternateSetting */
92 1, /* bNumEndpoints */
93 9, /* bInterfaceClass: UICLASS_HUB */
94 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
95 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
96 0 /* iInterface */
97 },
98 {
99 0x7, /* bLength */
100 5, /* bDescriptorType: UDESC_ENDPOINT */
101 0x81, /* bEndpointAddress:
102 * UE_DIR_IN | EHCI_INTR_ENDPT
103 */
104 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix83b9e1d2009-10-31 12:37:38 -0500105 8, /* wMaxPacketSize */
michael0a326102008-12-10 17:55:19 +0100106 255 /* bInterval */
107 },
Michael Trimarchi241f7512008-11-28 13:20:46 +0100108};
109
Remy Böhmer33e87482008-12-13 22:51:58 +0100110#if defined(CONFIG_EHCI_IS_TDI)
111#define ehci_is_TDI() (1)
112#else
113#define ehci_is_TDI() (0)
114#endif
115
Jim Lin54f3dfe2013-03-27 00:52:32 +0000116int __ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
117{
118 return PORTSC_PSPD(reg);
119}
120
121int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
122 __attribute__((weak, alias("__ehci_get_port_speed")));
123
124void __ehci_set_usbmode(int index)
125{
126 uint32_t tmp;
127 uint32_t *reg_ptr;
128
129 reg_ptr = (uint32_t *)((u8 *)&ehcic[index].hcor->or_usbcmd + USBMODE);
130 tmp = ehci_readl(reg_ptr);
131 tmp |= USBMODE_CM_HC;
132#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
133 tmp |= USBMODE_BE;
134#endif
135 ehci_writel(reg_ptr, tmp);
136}
137
138void ehci_set_usbmode(int index)
139 __attribute__((weak, alias("__ehci_set_usbmode")));
140
Marek Vasut09734772011-07-11 02:37:01 +0200141void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
142{
143 mdelay(50);
144}
145
146void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
147 __attribute__((weak, alias("__ehci_powerup_fixup")));
148
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100149static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michael0a326102008-12-10 17:55:19 +0100150{
michael0bf2a032008-12-11 13:43:55 +0100151 uint32_t result;
152 do {
153 result = ehci_readl(ptr);
Wolfgang Denkcdc5a7a2010-10-22 14:23:00 +0200154 udelay(5);
michael0bf2a032008-12-11 13:43:55 +0100155 if (result == ~(uint32_t)0)
156 return -1;
157 result &= mask;
158 if (result == done)
159 return 0;
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100160 usec--;
161 } while (usec > 0);
michael0bf2a032008-12-11 13:43:55 +0100162 return -1;
163}
164
Lucas Stach3494a4c2012-09-26 00:14:35 +0200165static int ehci_reset(int index)
michael0bf2a032008-12-11 13:43:55 +0100166{
167 uint32_t cmd;
michael0bf2a032008-12-11 13:43:55 +0100168 int ret = 0;
169
Lucas Stach3494a4c2012-09-26 00:14:35 +0200170 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Stefan Roese745af442010-11-26 15:44:00 +0100171 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200172 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
173 ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
174 CMD_RESET, 0, 250 * 1000);
michael0bf2a032008-12-11 13:43:55 +0100175 if (ret < 0) {
176 printf("EHCI fail to reset\n");
177 goto out;
178 }
179
Jim Lin54f3dfe2013-03-27 00:52:32 +0000180 if (ehci_is_TDI())
181 ehci_set_usbmode(index);
Simon Glass5978cdb2012-02-27 10:52:47 +0000182
183#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Lucas Stach3494a4c2012-09-26 00:14:35 +0200184 cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200185 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass5978cdb2012-02-27 10:52:47 +0000186 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Lucas Stach3494a4c2012-09-26 00:14:35 +0200187 ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
Simon Glass5978cdb2012-02-27 10:52:47 +0000188#endif
michael0bf2a032008-12-11 13:43:55 +0100189out:
190 return ret;
michael0a326102008-12-10 17:55:19 +0100191}
Michael Trimarchi241f7512008-11-28 13:20:46 +0100192
Michael Trimarchi241f7512008-11-28 13:20:46 +0100193static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
194{
Marek Vasutff24dc32012-04-09 04:07:46 +0200195 uint32_t delta, next;
196 uint32_t addr = (uint32_t)buf;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100197 int idx;
198
Ilya Yanokfb113712012-07-15 04:43:49 +0000199 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutff24dc32012-04-09 04:07:46 +0200200 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
201
Ilya Yanokfb113712012-07-15 04:43:49 +0000202 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
203
Michael Trimarchi241f7512008-11-28 13:20:46 +0100204 idx = 0;
Benoît Thébaudeaue68f48a2012-07-19 22:16:38 +0200205 while (idx < QT_BUFFER_CNT) {
michael0a326102008-12-10 17:55:19 +0100206 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denkebb829f2010-10-19 16:13:15 +0200207 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200208 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100209 delta = next - addr;
210 if (delta >= sz)
211 break;
212 sz -= delta;
213 addr = next;
214 idx++;
215 }
216
Benoît Thébaudeaue68f48a2012-07-19 22:16:38 +0200217 if (idx == QT_BUFFER_CNT) {
Ilya Yanok84570d62012-07-15 04:43:52 +0000218 printf("out of buffer pointers (%u bytes left)\n", sz);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100219 return -1;
220 }
221
222 return 0;
223}
224
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000225static inline u8 ehci_encode_speed(enum usb_device_speed speed)
226{
227 #define QH_HIGH_SPEED 2
228 #define QH_FULL_SPEED 0
229 #define QH_LOW_SPEED 1
230 if (speed == USB_SPEED_HIGH)
231 return QH_HIGH_SPEED;
232 if (speed == USB_SPEED_LOW)
233 return QH_LOW_SPEED;
234 return QH_FULL_SPEED;
235}
236
Michael Trimarchi241f7512008-11-28 13:20:46 +0100237static int
238ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
239 int length, struct devrequest *req)
240{
Tom Rini2cabcf72012-07-15 22:14:24 +0000241 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200242 struct qTD *qtd;
243 int qtd_count = 0;
Marek Vasut4f668312012-04-08 23:32:05 +0200244 int qtd_counter = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100245 volatile struct qTD *vtd;
246 unsigned long ts;
247 uint32_t *tdp;
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200248 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100249 uint32_t c, toggle;
michael0a326102008-12-10 17:55:19 +0100250 uint32_t cmd;
Simon Glassfd7f5132011-02-07 14:42:16 -0800251 int timeout;
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100252 int ret = 0;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200253 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100254
michael0a326102008-12-10 17:55:19 +0100255 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchi241f7512008-11-28 13:20:46 +0100256 buffer, length, req);
257 if (req != NULL)
michael0a326102008-12-10 17:55:19 +0100258 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100259 req->request, req->request,
260 req->requesttype, req->requesttype,
261 le16_to_cpu(req->value), le16_to_cpu(req->value),
michael0a326102008-12-10 17:55:19 +0100262 le16_to_cpu(req->index));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100263
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200264#define PKT_ALIGN 512
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200265 /*
266 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
267 * described by a transfer descriptor (the qTD). The qTDs form a linked
268 * list with a queue head (QH).
269 *
270 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
271 * have its beginning in a qTD transfer and its end in the following
272 * one, so the qTD transfer lengths have to be chosen accordingly.
273 *
274 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
275 * single pages. The first data buffer can start at any offset within a
276 * page (not considering the cache-line alignment issues), while the
277 * following buffers must be page-aligned. There is no alignment
278 * constraint on the size of a qTD transfer.
279 */
280 if (req != NULL)
281 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
282 qtd_count += 1 + 1;
283 if (length > 0 || req == NULL) {
284 /*
285 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200286 * data payload (not considering the first qTD transfer, which
287 * may be longer or shorter, and the final one, which may be
288 * shorter).
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200289 *
290 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200291 * transfer size is aligned to PKT_ALIGN, which is a multiple of
292 * wMaxPacketSize (except in some cases for interrupt transfers,
293 * see comment in submit_int_msg()).
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200294 *
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200295 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200296 * QT_BUFFER_CNT full pages will be used.
297 */
298 int xfr_sz = QT_BUFFER_CNT;
299 /*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200300 * However, if the input buffer is not aligned to PKT_ALIGN, the
301 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200302 * data buffer of each transfer will be page-unaligned.
303 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200304 if ((uint32_t)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200305 xfr_sz--;
306 /* Convert the qTD transfer size to bytes. */
307 xfr_sz *= EHCI_PAGE_SIZE;
308 /*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200309 * Approximate by excess the number of qTDs that will be
310 * required for the data payload. The exact formula is way more
311 * complicated and saves at most 2 qTDs, i.e. a total of 128
312 * bytes.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200313 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200314 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200315 }
316/*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200317 * Threshold value based on the worst-case total size of the allocated qTDs for
318 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200319 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200320#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200321#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
322#endif
323 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
324 if (qtd == NULL) {
325 printf("unable to allocate TDs\n");
326 return -1;
327 }
328
Tom Rini2cabcf72012-07-15 22:14:24 +0000329 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200330 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasut4f668312012-04-08 23:32:05 +0200331
Marek Vasutff24dc32012-04-09 04:07:46 +0200332 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
333
Marek Vasut285c8b32012-04-09 04:13:00 +0200334 /*
335 * Setup QH (3.6 in ehci-r10.pdf)
336 *
337 * qh_link ................. 03-00 H
338 * qh_endpt1 ............... 07-04 H
339 * qh_endpt2 ............... 0B-08 H
340 * - qh_curtd
341 * qh_overlay.qt_next ...... 13-10 H
342 * - qh_overlay.qt_altnext
343 */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200344 qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000345 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200346 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200347 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200348 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200349 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000350 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200351 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
352 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini2cabcf72012-07-15 22:14:24 +0000353 qh->qh_endpt1 = cpu_to_hc32(endpt);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200354 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
355 QH_ENDPT2_HUBADDR(dev->parent->devnum) |
356 QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini2cabcf72012-07-15 22:14:24 +0000357 qh->qh_endpt2 = cpu_to_hc32(endpt);
358 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100359
Tom Rini2cabcf72012-07-15 22:14:24 +0000360 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100361
Michael Trimarchi241f7512008-11-28 13:20:46 +0100362 if (req != NULL) {
Marek Vasut285c8b32012-04-09 04:13:00 +0200363 /*
364 * Setup request qTD (3.5 in ehci-r10.pdf)
365 *
366 * qt_next ................ 03-00 H
367 * qt_altnext ............. 07-04 H
368 * qt_token ............... 0B-08 H
369 *
370 * [ buffer, buffer_hi ] loaded with "req".
371 */
Marek Vasut4f668312012-04-08 23:32:05 +0200372 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
373 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200374 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
375 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
376 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
377 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasut4f668312012-04-08 23:32:05 +0200378 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200379 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
380 printf("unable to construct SETUP TD\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100381 goto fail;
382 }
Marek Vasut285c8b32012-04-09 04:13:00 +0200383 /* Update previous qTD! */
Marek Vasut4f668312012-04-08 23:32:05 +0200384 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
385 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100386 toggle = 1;
387 }
388
389 if (length > 0 || req == NULL) {
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200390 uint8_t *buf_ptr = buffer;
391 int left_length = length;
392
393 do {
394 /*
395 * Determine the size of this qTD transfer. By default,
396 * QT_BUFFER_CNT full pages can be used.
397 */
398 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
399 /*
400 * However, if the input buffer is not page-aligned, the
401 * portion of the first page before the buffer start
402 * offset within that page is unusable.
403 */
404 xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
405 /*
406 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200407 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200408 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200409 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200410 /*
411 * This transfer may be shorter than the available qTD
412 * transfer size that has just been computed.
413 */
414 xfr_bytes = min(xfr_bytes, left_length);
415
416 /*
417 * Setup request qTD (3.5 in ehci-r10.pdf)
418 *
419 * qt_next ................ 03-00 H
420 * qt_altnext ............. 07-04 H
421 * qt_token ............... 0B-08 H
422 *
423 * [ buffer, buffer_hi ] loaded with "buffer".
424 */
425 qtd[qtd_counter].qt_next =
426 cpu_to_hc32(QT_NEXT_TERMINATE);
427 qtd[qtd_counter].qt_altnext =
428 cpu_to_hc32(QT_NEXT_TERMINATE);
429 token = QT_TOKEN_DT(toggle) |
430 QT_TOKEN_TOTALBYTES(xfr_bytes) |
431 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
432 QT_TOKEN_CERR(3) |
433 QT_TOKEN_PID(usb_pipein(pipe) ?
434 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
435 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
436 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
437 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
438 xfr_bytes)) {
439 printf("unable to construct DATA TD\n");
440 goto fail;
441 }
442 /* Update previous qTD! */
443 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
444 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200445 /*
446 * Data toggle has to be adjusted since the qTD transfer
447 * size is not always an even multiple of
448 * wMaxPacketSize.
449 */
450 if ((xfr_bytes / maxpacket) & 1)
451 toggle ^= 1;
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200452 buf_ptr += xfr_bytes;
453 left_length -= xfr_bytes;
454 } while (left_length > 0);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100455 }
456
457 if (req != NULL) {
Marek Vasut285c8b32012-04-09 04:13:00 +0200458 /*
459 * Setup request qTD (3.5 in ehci-r10.pdf)
460 *
461 * qt_next ................ 03-00 H
462 * qt_altnext ............. 07-04 H
463 * qt_token ............... 0B-08 H
464 */
Marek Vasut4f668312012-04-08 23:32:05 +0200465 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
466 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200467 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200468 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
469 QT_TOKEN_PID(usb_pipein(pipe) ?
470 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
471 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasut4f668312012-04-08 23:32:05 +0200472 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut285c8b32012-04-09 04:13:00 +0200473 /* Update previous qTD! */
Marek Vasut4f668312012-04-08 23:32:05 +0200474 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
475 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100476 }
477
Lucas Stach3494a4c2012-09-26 00:14:35 +0200478 ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100479
Stefan Roese25983c12009-01-21 17:12:19 +0100480 /* Flush dcache */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200481 flush_dcache_range((uint32_t)&ctrl->qh_list,
482 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Tom Rini2cabcf72012-07-15 22:14:24 +0000483 flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200484 flush_dcache_range((uint32_t)qtd,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200485 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roese25983c12009-01-21 17:12:19 +0100486
Ilya Yanok84309bb2012-07-15 22:12:08 +0000487 /* Set async. queue head pointer. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200488 ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
Ilya Yanok84309bb2012-07-15 22:12:08 +0000489
Lucas Stach3494a4c2012-09-26 00:14:35 +0200490 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
491 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100492
493 /* Enable async. schedule. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200494 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael0bf2a032008-12-11 13:43:55 +0100495 cmd |= CMD_ASE;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200496 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0a326102008-12-10 17:55:19 +0100497
Lucas Stach3494a4c2012-09-26 00:14:35 +0200498 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100499 100 * 1000);
500 if (ret < 0) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200501 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100502 goto fail;
michael0bf2a032008-12-11 13:43:55 +0100503 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100504
505 /* Wait for TDs to be processed. */
506 ts = get_timer(0);
Marek Vasut4f668312012-04-08 23:32:05 +0200507 vtd = &qtd[qtd_counter - 1];
Simon Glassfd7f5132011-02-07 14:42:16 -0800508 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100509 do {
Stefan Roese25983c12009-01-21 17:12:19 +0100510 /* Invalidate dcache */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200511 invalidate_dcache_range((uint32_t)&ctrl->qh_list,
512 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Tom Rini2cabcf72012-07-15 22:14:24 +0000513 invalidate_dcache_range((uint32_t)qh,
514 ALIGN_END_ADDR(struct QH, qh, 1));
Marek Vasutff24dc32012-04-09 04:07:46 +0200515 invalidate_dcache_range((uint32_t)qtd,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200516 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutff24dc32012-04-09 04:07:46 +0200517
michael0a326102008-12-10 17:55:19 +0100518 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200519 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100520 break;
Stefan Roese86b34cf2010-11-26 15:43:28 +0100521 WATCHDOG_RESET();
Simon Glassfd7f5132011-02-07 14:42:16 -0800522 } while (get_timer(ts) < timeout);
523
Ilya Yanokfb113712012-07-15 04:43:49 +0000524 /*
525 * Invalidate the memory area occupied by buffer
526 * Don't try to fix the buffer alignment, if it isn't properly
527 * aligned it's upper layer's fault so let invalidate_dcache_range()
528 * vow about it. But we have to fix the length as it's actual
529 * transfer length and can be unaligned. This is potentially
530 * dangerous operation, it's responsibility of the calling
531 * code to make sure enough space is reserved.
532 */
533 invalidate_dcache_range((uint32_t)buffer,
534 ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutff24dc32012-04-09 04:07:46 +0200535
Simon Glassfd7f5132011-02-07 14:42:16 -0800536 /* Check that the TD processing happened */
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200537 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glassfd7f5132011-02-07 14:42:16 -0800538 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100539
540 /* Disable async schedule. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200541 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael0a326102008-12-10 17:55:19 +0100542 cmd &= ~CMD_ASE;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200543 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0bf2a032008-12-11 13:43:55 +0100544
Lucas Stach3494a4c2012-09-26 00:14:35 +0200545 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100546 100 * 1000);
547 if (ret < 0) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200548 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100549 goto fail;
michael0bf2a032008-12-11 13:43:55 +0100550 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100551
Tom Rini2cabcf72012-07-15 22:14:24 +0000552 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200553 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michael0a326102008-12-10 17:55:19 +0100554 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200555 switch (QT_TOKEN_GET_STATUS(token) &
556 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100557 case 0:
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200558 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100559 usb_settoggle(dev, usb_pipeendpoint(pipe),
560 usb_pipeout(pipe), toggle);
561 dev->status = 0;
562 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200563 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100564 dev->status = USB_ST_STALLED;
565 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200566 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
567 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100568 dev->status = USB_ST_BUF_ERR;
569 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200570 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
571 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100572 dev->status = USB_ST_BABBLE_DET;
573 break;
574 default:
575 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200576 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschine1e09312010-11-02 11:47:29 +0100577 dev->status |= USB_ST_STALLED;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100578 break;
579 }
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200580 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100581 } else {
582 dev->act_len = 0;
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800583#ifndef CONFIG_USB_EHCI_FARADAY
michael0a326102008-12-10 17:55:19 +0100584 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach3494a4c2012-09-26 00:14:35 +0200585 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
586 ehci_readl(&ctrl->hcor->or_portsc[0]),
587 ehci_readl(&ctrl->hcor->or_portsc[1]));
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800588#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +0100589 }
590
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200591 free(qtd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100592 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
593
594fail:
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200595 free(qtd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100596 return -1;
597}
598
Kuo-Jung Su6a656df2013-05-15 15:29:21 +0800599__weak uint32_t *ehci_get_portsc_register(struct ehci_hcor *hcor, int port)
600{
601 if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
602 /* Printing the message would cause a scan failure! */
603 debug("The request port(%u) is not configured\n", port);
604 return NULL;
605 }
606
607 return (uint32_t *)&hcor->or_portsc[port];
608}
609
michael0a326102008-12-10 17:55:19 +0100610int
Michael Trimarchi241f7512008-11-28 13:20:46 +0100611ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
612 int length, struct devrequest *req)
613{
614 uint8_t tmpbuf[4];
615 u16 typeReq;
michael0a326102008-12-10 17:55:19 +0100616 void *srcptr = NULL;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100617 int len, srclen;
618 uint32_t reg;
Remy Böhmer33e87482008-12-13 22:51:58 +0100619 uint32_t *status_reg;
Julius Wernerd4046702013-02-28 18:08:40 +0000620 int port = le16_to_cpu(req->index) & 0xff;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200621 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100622
623 srclen = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100624
michael0a326102008-12-10 17:55:19 +0100625 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100626 req->request, req->request,
627 req->requesttype, req->requesttype,
628 le16_to_cpu(req->value), le16_to_cpu(req->index));
629
Prafulla Wadaskar22810292009-07-17 19:56:30 +0530630 typeReq = req->request | req->requesttype << 8;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100631
Prafulla Wadaskar22810292009-07-17 19:56:30 +0530632 switch (typeReq) {
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800633 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
634 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
635 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Kuo-Jung Su6a656df2013-05-15 15:29:21 +0800636 status_reg = ehci_get_portsc_register(ctrl->hcor, port - 1);
637 if (!status_reg)
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800638 return -1;
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800639 break;
640 default:
641 status_reg = NULL;
642 break;
643 }
644
645 switch (typeReq) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100646 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
647 switch (le16_to_cpu(req->value) >> 8) {
648 case USB_DT_DEVICE:
michael0a326102008-12-10 17:55:19 +0100649 debug("USB_DT_DEVICE request\n");
650 srcptr = &descriptor.device;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200651 srclen = descriptor.device.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100652 break;
653 case USB_DT_CONFIG:
michael0a326102008-12-10 17:55:19 +0100654 debug("USB_DT_CONFIG config\n");
655 srcptr = &descriptor.config;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200656 srclen = descriptor.config.bLength +
657 descriptor.interface.bLength +
658 descriptor.endpoint.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100659 break;
660 case USB_DT_STRING:
michael0a326102008-12-10 17:55:19 +0100661 debug("USB_DT_STRING config\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100662 switch (le16_to_cpu(req->value) & 0xff) {
663 case 0: /* Language */
664 srcptr = "\4\3\1\0";
665 srclen = 4;
666 break;
667 case 1: /* Vendor */
668 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
669 srclen = 14;
670 break;
671 case 2: /* Product */
672 srcptr = "\52\3E\0H\0C\0I\0 "
673 "\0H\0o\0s\0t\0 "
674 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
675 srclen = 42;
676 break;
677 default:
michael0a326102008-12-10 17:55:19 +0100678 debug("unknown value DT_STRING %x\n",
679 le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100680 goto unknown;
681 }
682 break;
683 default:
michael0a326102008-12-10 17:55:19 +0100684 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100685 goto unknown;
686 }
687 break;
688 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
689 switch (le16_to_cpu(req->value) >> 8) {
690 case USB_DT_HUB:
michael0a326102008-12-10 17:55:19 +0100691 debug("USB_DT_HUB config\n");
692 srcptr = &descriptor.hub;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200693 srclen = descriptor.hub.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100694 break;
695 default:
michael0a326102008-12-10 17:55:19 +0100696 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100697 goto unknown;
698 }
699 break;
700 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michael0a326102008-12-10 17:55:19 +0100701 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach3494a4c2012-09-26 00:14:35 +0200702 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100703 break;
704 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michael0a326102008-12-10 17:55:19 +0100705 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100706 /* Nothing to do */
707 break;
708 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
709 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
710 tmpbuf[1] = 0;
711 srcptr = tmpbuf;
712 srclen = 2;
713 break;
michael0a326102008-12-10 17:55:19 +0100714 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchi241f7512008-11-28 13:20:46 +0100715 memset(tmpbuf, 0, 4);
Remy Böhmer33e87482008-12-13 22:51:58 +0100716 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100717 if (reg & EHCI_PS_CS)
718 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
719 if (reg & EHCI_PS_PE)
720 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
721 if (reg & EHCI_PS_SUSP)
722 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
723 if (reg & EHCI_PS_OCA)
724 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300725 if (reg & EHCI_PS_PR)
726 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100727 if (reg & EHCI_PS_PP)
728 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese497f1842009-01-21 17:12:01 +0100729
730 if (ehci_is_TDI()) {
Jim Lin54f3dfe2013-03-27 00:52:32 +0000731 switch (ehci_get_port_speed(ctrl->hcor, reg)) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200732 case PORTSC_PSPD_FS:
Stefan Roese497f1842009-01-21 17:12:01 +0100733 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200734 case PORTSC_PSPD_LS:
Stefan Roese497f1842009-01-21 17:12:01 +0100735 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
736 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200737 case PORTSC_PSPD_HS:
Stefan Roese497f1842009-01-21 17:12:01 +0100738 default:
739 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
740 break;
741 }
742 } else {
743 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
744 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100745
746 if (reg & EHCI_PS_CSC)
747 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
748 if (reg & EHCI_PS_PEC)
749 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
750 if (reg & EHCI_PS_OCC)
751 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Julius Wernerd4046702013-02-28 18:08:40 +0000752 if (ctrl->portreset & (1 << port))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100753 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmer33e87482008-12-13 22:51:58 +0100754
Michael Trimarchi241f7512008-11-28 13:20:46 +0100755 srcptr = tmpbuf;
756 srclen = 4;
757 break;
michael0a326102008-12-10 17:55:19 +0100758 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmer33e87482008-12-13 22:51:58 +0100759 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100760 reg &= ~EHCI_PS_CLEAR;
761 switch (le16_to_cpu(req->value)) {
michael0bf2a032008-12-11 13:43:55 +0100762 case USB_PORT_FEAT_ENABLE:
763 reg |= EHCI_PS_PE;
Remy Böhmer33e87482008-12-13 22:51:58 +0100764 ehci_writel(status_reg, reg);
michael0bf2a032008-12-11 13:43:55 +0100765 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100766 case USB_PORT_FEAT_POWER:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200767 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmer33e87482008-12-13 22:51:58 +0100768 reg |= EHCI_PS_PP;
769 ehci_writel(status_reg, reg);
770 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100771 break;
772 case USB_PORT_FEAT_RESET:
Remy Böhmer33e87482008-12-13 22:51:58 +0100773 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
774 !ehci_is_TDI() &&
775 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100776 /* Low speed device, give up ownership. */
Remy Böhmer33e87482008-12-13 22:51:58 +0100777 debug("port %d low speed --> companion\n",
Julius Wernerd4046702013-02-28 18:08:40 +0000778 port - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100779 reg |= EHCI_PS_PO;
Remy Böhmer33e87482008-12-13 22:51:58 +0100780 ehci_writel(status_reg, reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100781 break;
Remy Böhmer33e87482008-12-13 22:51:58 +0100782 } else {
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300783 int ret;
784
Remy Böhmer33e87482008-12-13 22:51:58 +0100785 reg |= EHCI_PS_PR;
786 reg &= ~EHCI_PS_PE;
787 ehci_writel(status_reg, reg);
788 /*
789 * caller must wait, then call GetPortStatus
790 * usb 2.0 specification say 50 ms resets on
791 * root
792 */
Marek Vasut09734772011-07-11 02:37:01 +0200793 ehci_powerup_fixup(status_reg, &reg);
794
Chris Zhangfddf6d62010-01-06 13:34:04 -0800795 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300796 /*
797 * A host controller must terminate the reset
798 * and stabilize the state of the port within
799 * 2 milliseconds
800 */
801 ret = handshake(status_reg, EHCI_PS_PR, 0,
802 2 * 1000);
803 if (!ret)
Julius Wernerd4046702013-02-28 18:08:40 +0000804 ctrl->portreset |= 1 << port;
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300805 else
806 printf("port(%d) reset error\n",
Julius Wernerd4046702013-02-28 18:08:40 +0000807 port - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100808 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100809 break;
Julius Wernerd4046702013-02-28 18:08:40 +0000810 case USB_PORT_FEAT_TEST:
811 reg &= ~(0xf << 16);
812 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
813 ehci_writel(status_reg, reg);
814 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100815 default:
michael0a326102008-12-10 17:55:19 +0100816 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100817 goto unknown;
818 }
Remy Böhmer33e87482008-12-13 22:51:58 +0100819 /* unblock posted writes */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200820 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100821 break;
michael0a326102008-12-10 17:55:19 +0100822 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmer33e87482008-12-13 22:51:58 +0100823 reg = ehci_readl(status_reg);
Simon Glass0554ba52013-05-10 19:49:00 -0700824 reg &= ~EHCI_PS_CLEAR;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100825 switch (le16_to_cpu(req->value)) {
826 case USB_PORT_FEAT_ENABLE:
827 reg &= ~EHCI_PS_PE;
828 break;
Remy Böhmer33e87482008-12-13 22:51:58 +0100829 case USB_PORT_FEAT_C_ENABLE:
Simon Glass0554ba52013-05-10 19:49:00 -0700830 reg |= EHCI_PS_PE;
Remy Böhmer33e87482008-12-13 22:51:58 +0100831 break;
832 case USB_PORT_FEAT_POWER:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200833 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Simon Glass0554ba52013-05-10 19:49:00 -0700834 reg &= ~EHCI_PS_PP;
835 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100836 case USB_PORT_FEAT_C_CONNECTION:
Simon Glass0554ba52013-05-10 19:49:00 -0700837 reg |= EHCI_PS_CSC;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100838 break;
michael0bf2a032008-12-11 13:43:55 +0100839 case USB_PORT_FEAT_OVER_CURRENT:
Simon Glass0554ba52013-05-10 19:49:00 -0700840 reg |= EHCI_PS_OCC;
michael0bf2a032008-12-11 13:43:55 +0100841 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100842 case USB_PORT_FEAT_C_RESET:
Julius Wernerd4046702013-02-28 18:08:40 +0000843 ctrl->portreset &= ~(1 << port);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100844 break;
845 default:
michael0a326102008-12-10 17:55:19 +0100846 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100847 goto unknown;
848 }
Remy Böhmer33e87482008-12-13 22:51:58 +0100849 ehci_writel(status_reg, reg);
850 /* unblock posted write */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200851 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100852 break;
853 default:
michael0a326102008-12-10 17:55:19 +0100854 debug("Unknown request\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100855 goto unknown;
856 }
857
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000858 mdelay(1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100859 len = min3(srclen, le16_to_cpu(req->length), length);
860 if (srcptr != NULL && len > 0)
861 memcpy(buffer, srcptr, len);
michael0a326102008-12-10 17:55:19 +0100862 else
863 debug("Len is 0\n");
864
Michael Trimarchi241f7512008-11-28 13:20:46 +0100865 dev->act_len = len;
866 dev->status = 0;
867 return 0;
868
869unknown:
michael0a326102008-12-10 17:55:19 +0100870 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100871 req->requesttype, req->request, le16_to_cpu(req->value),
872 le16_to_cpu(req->index), le16_to_cpu(req->length));
873
874 dev->act_len = 0;
875 dev->status = USB_ST_STALLED;
876 return -1;
877}
878
Lucas Stacha3231282012-09-26 00:14:34 +0200879int usb_lowlevel_stop(int index)
Michael Trimarchi241f7512008-11-28 13:20:46 +0100880{
Lucas Stach3494a4c2012-09-26 00:14:35 +0200881 return ehci_hcd_stop(index);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100882}
883
Lucas Stacha3231282012-09-26 00:14:34 +0200884int usb_lowlevel_init(int index, void **controller)
Michael Trimarchi241f7512008-11-28 13:20:46 +0100885{
886 uint32_t reg;
michael0a326102008-12-10 17:55:19 +0100887 uint32_t cmd;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200888 struct QH *qh_list;
Patrick Georgie55fdac2013-03-06 14:08:31 +0000889 struct QH *periodic;
890 int i;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100891
Lucas Stach3494a4c2012-09-26 00:14:35 +0200892 if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100893 return -1;
894
michael0bf2a032008-12-11 13:43:55 +0100895 /* EHCI spec section 4.1 */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200896 if (ehci_reset(index))
michael0bf2a032008-12-11 13:43:55 +0100897 return -1;
898
Stefan Roese2e98fc72009-01-21 17:12:10 +0100899#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
Lucas Stach3494a4c2012-09-26 00:14:35 +0200900 if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
Stefan Roese2e98fc72009-01-21 17:12:10 +0100901 return -1;
902#endif
Vincent Palatin0d6f77c2012-12-12 17:55:22 -0800903 /* Set the high address word (aka segment) for 64-bit controller */
904 if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
905 ehci_writel(ehcic[index].hcor->or_ctrldssegment, 0);
Stefan Roese2e98fc72009-01-21 17:12:10 +0100906
Lucas Stach3494a4c2012-09-26 00:14:35 +0200907 qh_list = &ehcic[index].qh_list;
908
Michael Trimarchi241f7512008-11-28 13:20:46 +0100909 /* Set head of reclaim list */
Tom Rini2cabcf72012-07-15 22:14:24 +0000910 memset(qh_list, 0, sizeof(*qh_list));
911 qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200912 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
913 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini2cabcf72012-07-15 22:14:24 +0000914 qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
915 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
916 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200917 qh_list->qh_overlay.qt_token =
918 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100919
Stephen Warren36dad662013-05-24 15:03:17 -0600920 flush_dcache_range((uint32_t)qh_list,
921 ALIGN_END_ADDR(struct QH, qh_list, 1));
922
Patrick Georgie55fdac2013-03-06 14:08:31 +0000923 /* Set async. queue head pointer. */
924 ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (uint32_t)qh_list);
925
926 /*
927 * Set up periodic list
928 * Step 1: Parent QH for all periodic transfers.
929 */
930 periodic = &ehcic[index].periodic_queue;
931 memset(periodic, 0, sizeof(*periodic));
932 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
933 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
934 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
935
Stephen Warren36dad662013-05-24 15:03:17 -0600936 flush_dcache_range((uint32_t)periodic,
937 ALIGN_END_ADDR(struct QH, periodic, 1));
938
Patrick Georgie55fdac2013-03-06 14:08:31 +0000939 /*
940 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
941 * In particular, device specifications on polling frequency
942 * are disregarded. Keyboards seem to send NAK/NYet reliably
943 * when polled with an empty buffer.
944 *
945 * Split Transactions will be spread across microframes using
946 * S-mask and C-mask.
947 */
Nikita Kiryanov2f13e442013-07-29 13:27:40 +0300948 if (ehcic[index].periodic_list == NULL)
949 ehcic[index].periodic_list = memalign(4096, 1024 * 4);
950
Patrick Georgie55fdac2013-03-06 14:08:31 +0000951 if (!ehcic[index].periodic_list)
952 return -ENOMEM;
953 for (i = 0; i < 1024; i++) {
954 ehcic[index].periodic_list[i] = (uint32_t)periodic
955 | QH_LINK_TYPE_QH;
956 }
957
Stephen Warren36dad662013-05-24 15:03:17 -0600958 flush_dcache_range((uint32_t)ehcic[index].periodic_list,
959 ALIGN_END_ADDR(uint32_t, ehcic[index].periodic_list,
960 1024));
961
Patrick Georgie55fdac2013-03-06 14:08:31 +0000962 /* Set periodic list base address */
963 ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
964 (uint32_t)ehcic[index].periodic_list);
965
Lucas Stach3494a4c2012-09-26 00:14:35 +0200966 reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
michael0bf2a032008-12-11 13:43:55 +0100967 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stachf5b34082012-09-28 00:26:19 +0200968 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmer33e87482008-12-13 22:51:58 +0100969 /* Port Indicators */
970 if (HCS_INDICATOR(reg))
Lucas Stach835e11e2012-09-06 08:00:13 +0200971 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
972 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmer33e87482008-12-13 22:51:58 +0100973 /* Port Power Control */
974 if (HCS_PPC(reg))
Lucas Stach835e11e2012-09-06 08:00:13 +0200975 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
976 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100977
Michael Trimarchi241f7512008-11-28 13:20:46 +0100978 /* Start the host controller. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200979 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Wolfgang Denkfb718e12009-02-12 00:08:39 +0100980 /*
981 * Philips, Intel, and maybe others need CMD_RUN before the
982 * root hub will detect new devices (why?); NEC doesn't
983 */
michael0bf2a032008-12-11 13:43:55 +0100984 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
985 cmd |= CMD_RUN;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200986 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
michael0bf2a032008-12-11 13:43:55 +0100987
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800988#ifndef CONFIG_USB_EHCI_FARADAY
michael0bf2a032008-12-11 13:43:55 +0100989 /* take control over the ports */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200990 cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
michael0bf2a032008-12-11 13:43:55 +0100991 cmd |= FLAG_CF;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200992 ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800993#endif
994
Remy Böhmer33e87482008-12-13 22:51:58 +0100995 /* unblock posted write */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200996 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000997 mdelay(5);
Lucas Stach3494a4c2012-09-26 00:14:35 +0200998 reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
Remy Böhmer33e87482008-12-13 22:51:58 +0100999 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchi241f7512008-11-28 13:20:46 +01001000
Lucas Stach3494a4c2012-09-26 00:14:35 +02001001 ehcic[index].rootdev = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +01001002
Lucas Stach3494a4c2012-09-26 00:14:35 +02001003 *controller = &ehcic[index];
Michael Trimarchi241f7512008-11-28 13:20:46 +01001004 return 0;
1005}
1006
1007int
1008submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1009 int length)
1010{
1011
1012 if (usb_pipetype(pipe) != PIPE_BULK) {
1013 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1014 return -1;
1015 }
1016 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1017}
1018
1019int
1020submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1021 int length, struct devrequest *setup)
1022{
Lucas Stach3494a4c2012-09-26 00:14:35 +02001023 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchi241f7512008-11-28 13:20:46 +01001024
1025 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1026 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1027 return -1;
1028 }
1029
Lucas Stach3494a4c2012-09-26 00:14:35 +02001030 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1031 if (!ctrl->rootdev)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001032 dev->speed = USB_SPEED_HIGH;
1033 return ehci_submit_root(dev, pipe, buffer, length, setup);
1034 }
1035 return ehci_submit_async(dev, pipe, buffer, length, setup);
1036}
1037
Patrick Georgie55fdac2013-03-06 14:08:31 +00001038struct int_queue {
1039 struct QH *first;
1040 struct QH *current;
1041 struct QH *last;
1042 struct qTD *tds;
1043};
1044
1045#define NEXT_QH(qh) (struct QH *)((qh)->qh_link & ~0x1f)
1046
1047static int
1048enable_periodic(struct ehci_ctrl *ctrl)
1049{
1050 uint32_t cmd;
1051 struct ehci_hcor *hcor = ctrl->hcor;
1052 int ret;
1053
1054 cmd = ehci_readl(&hcor->or_usbcmd);
1055 cmd |= CMD_PSE;
1056 ehci_writel(&hcor->or_usbcmd, cmd);
1057
1058 ret = handshake((uint32_t *)&hcor->or_usbsts,
1059 STS_PSS, STS_PSS, 100 * 1000);
1060 if (ret < 0) {
1061 printf("EHCI failed: timeout when enabling periodic list\n");
1062 return -ETIMEDOUT;
1063 }
1064 udelay(1000);
1065 return 0;
1066}
1067
1068static int
1069disable_periodic(struct ehci_ctrl *ctrl)
1070{
1071 uint32_t cmd;
1072 struct ehci_hcor *hcor = ctrl->hcor;
1073 int ret;
1074
1075 cmd = ehci_readl(&hcor->or_usbcmd);
1076 cmd &= ~CMD_PSE;
1077 ehci_writel(&hcor->or_usbcmd, cmd);
1078
1079 ret = handshake((uint32_t *)&hcor->or_usbsts,
1080 STS_PSS, 0, 100 * 1000);
1081 if (ret < 0) {
1082 printf("EHCI failed: timeout when disabling periodic list\n");
1083 return -ETIMEDOUT;
1084 }
1085 return 0;
1086}
1087
1088static int periodic_schedules;
1089
1090struct int_queue *
1091create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
1092 int elementsize, void *buffer)
1093{
1094 struct ehci_ctrl *ctrl = dev->controller;
1095 struct int_queue *result = NULL;
1096 int i;
1097
1098 debug("Enter create_int_queue\n");
1099 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1100 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1101 return NULL;
1102 }
1103
1104 /* limit to 4 full pages worth of data -
1105 * we can safely fit them in a single TD,
1106 * no matter the alignment
1107 */
1108 if (elementsize >= 16384) {
1109 debug("too large elements for interrupt transfers\n");
1110 return NULL;
1111 }
1112
1113 result = malloc(sizeof(*result));
1114 if (!result) {
1115 debug("ehci intr queue: out of memory\n");
1116 goto fail1;
1117 }
1118 result->first = memalign(32, sizeof(struct QH) * queuesize);
1119 if (!result->first) {
1120 debug("ehci intr queue: out of memory\n");
1121 goto fail2;
1122 }
1123 result->current = result->first;
1124 result->last = result->first + queuesize - 1;
1125 result->tds = memalign(32, sizeof(struct qTD) * queuesize);
1126 if (!result->tds) {
1127 debug("ehci intr queue: out of memory\n");
1128 goto fail3;
1129 }
1130 memset(result->first, 0, sizeof(struct QH) * queuesize);
1131 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1132
1133 for (i = 0; i < queuesize; i++) {
1134 struct QH *qh = result->first + i;
1135 struct qTD *td = result->tds + i;
1136 void **buf = &qh->buffer;
1137
1138 qh->qh_link = (uint32_t)(qh+1) | QH_LINK_TYPE_QH;
1139 if (i == queuesize - 1)
1140 qh->qh_link = QH_LINK_TERMINATE;
1141
1142 qh->qh_overlay.qt_next = (uint32_t)td;
1143 qh->qh_endpt1 = (0 << 28) | /* No NAK reload (ehci 4.9) */
1144 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1145 (1 << 14) |
1146 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1147 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
1148 (usb_pipedevice(pipe) << 0);
1149 qh->qh_endpt2 = (1 << 30) | /* 1 Tx per mframe */
1150 (1 << 0); /* S-mask: microframe 0 */
1151 if (dev->speed == USB_SPEED_LOW ||
1152 dev->speed == USB_SPEED_FULL) {
1153 debug("TT: port: %d, hub address: %d\n",
1154 dev->portnr, dev->parent->devnum);
1155 qh->qh_endpt2 |= (dev->portnr << 23) |
1156 (dev->parent->devnum << 16) |
1157 (0x1c << 8); /* C-mask: microframes 2-4 */
1158 }
1159
1160 td->qt_next = QT_NEXT_TERMINATE;
1161 td->qt_altnext = QT_NEXT_TERMINATE;
1162 debug("communication direction is '%s'\n",
1163 usb_pipein(pipe) ? "in" : "out");
1164 td->qt_token = (elementsize << 16) |
1165 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
1166 0x80; /* active */
1167 td->qt_buffer[0] = (uint32_t)buffer + i * elementsize;
1168 td->qt_buffer[1] = (td->qt_buffer[0] + 0x1000) & ~0xfff;
1169 td->qt_buffer[2] = (td->qt_buffer[0] + 0x2000) & ~0xfff;
1170 td->qt_buffer[3] = (td->qt_buffer[0] + 0x3000) & ~0xfff;
1171 td->qt_buffer[4] = (td->qt_buffer[0] + 0x4000) & ~0xfff;
1172
1173 *buf = buffer + i * elementsize;
1174 }
1175
Stephen Warren36dad662013-05-24 15:03:17 -06001176 flush_dcache_range((uint32_t)buffer,
1177 ALIGN_END_ADDR(char, buffer,
1178 queuesize * elementsize));
1179 flush_dcache_range((uint32_t)result->first,
1180 ALIGN_END_ADDR(struct QH, result->first,
1181 queuesize));
1182 flush_dcache_range((uint32_t)result->tds,
1183 ALIGN_END_ADDR(struct qTD, result->tds,
1184 queuesize));
1185
Patrick Georgie55fdac2013-03-06 14:08:31 +00001186 if (disable_periodic(ctrl) < 0) {
1187 debug("FATAL: periodic should never fail, but did");
1188 goto fail3;
1189 }
1190
1191 /* hook up to periodic list */
1192 struct QH *list = &ctrl->periodic_queue;
1193 result->last->qh_link = list->qh_link;
1194 list->qh_link = (uint32_t)result->first | QH_LINK_TYPE_QH;
1195
Stephen Warren36dad662013-05-24 15:03:17 -06001196 flush_dcache_range((uint32_t)result->last,
1197 ALIGN_END_ADDR(struct QH, result->last, 1));
1198 flush_dcache_range((uint32_t)list,
1199 ALIGN_END_ADDR(struct QH, list, 1));
1200
Patrick Georgie55fdac2013-03-06 14:08:31 +00001201 if (enable_periodic(ctrl) < 0) {
1202 debug("FATAL: periodic should never fail, but did");
1203 goto fail3;
1204 }
1205 periodic_schedules++;
1206
1207 debug("Exit create_int_queue\n");
1208 return result;
1209fail3:
1210 if (result->tds)
1211 free(result->tds);
1212fail2:
1213 if (result->first)
1214 free(result->first);
1215 if (result)
1216 free(result);
1217fail1:
1218 return NULL;
1219}
1220
1221void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1222{
1223 struct QH *cur = queue->current;
1224
1225 /* depleted queue */
1226 if (cur == NULL) {
1227 debug("Exit poll_int_queue with completed queue\n");
1228 return NULL;
1229 }
1230 /* still active */
Stephen Warren36dad662013-05-24 15:03:17 -06001231 invalidate_dcache_range((uint32_t)cur,
1232 ALIGN_END_ADDR(struct QH, cur, 1));
Patrick Georgie55fdac2013-03-06 14:08:31 +00001233 if (cur->qh_overlay.qt_token & 0x80) {
1234 debug("Exit poll_int_queue with no completed intr transfer. "
1235 "token is %x\n", cur->qh_overlay.qt_token);
1236 return NULL;
1237 }
1238 if (!(cur->qh_link & QH_LINK_TERMINATE))
1239 queue->current++;
1240 else
1241 queue->current = NULL;
1242 debug("Exit poll_int_queue with completed intr transfer. "
1243 "token is %x at %p (first at %p)\n", cur->qh_overlay.qt_token,
1244 &cur->qh_overlay.qt_token, queue->first);
1245 return cur->buffer;
1246}
1247
1248/* Do not free buffers associated with QHs, they're owned by someone else */
1249int
1250destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1251{
1252 struct ehci_ctrl *ctrl = dev->controller;
1253 int result = -1;
1254 unsigned long timeout;
1255
1256 if (disable_periodic(ctrl) < 0) {
1257 debug("FATAL: periodic should never fail, but did");
1258 goto out;
1259 }
1260 periodic_schedules--;
1261
1262 struct QH *cur = &ctrl->periodic_queue;
1263 timeout = get_timer(0) + 500; /* abort after 500ms */
1264 while (!(cur->qh_link & QH_LINK_TERMINATE)) {
1265 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1266 if (NEXT_QH(cur) == queue->first) {
1267 debug("found candidate. removing from chain\n");
1268 cur->qh_link = queue->last->qh_link;
1269 result = 0;
1270 break;
1271 }
1272 cur = NEXT_QH(cur);
1273 if (get_timer(0) > timeout) {
1274 printf("Timeout destroying interrupt endpoint queue\n");
1275 result = -1;
1276 goto out;
1277 }
1278 }
1279
1280 if (periodic_schedules > 0) {
1281 result = enable_periodic(ctrl);
1282 if (result < 0)
1283 debug("FATAL: periodic should never fail, but did");
1284 }
1285
1286out:
1287 free(queue->tds);
1288 free(queue->first);
1289 free(queue);
1290
1291 return result;
1292}
1293
Michael Trimarchi241f7512008-11-28 13:20:46 +01001294int
1295submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1296 int length, int interval)
1297{
Patrick Georgie55fdac2013-03-06 14:08:31 +00001298 void *backbuffer;
1299 struct int_queue *queue;
1300 unsigned long timeout;
1301 int result = 0, ret;
1302
Michael Trimarchi241f7512008-11-28 13:20:46 +01001303 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1304 dev, pipe, buffer, length, interval);
Benoît Thébaudeau58c4dfb2012-08-09 23:50:44 +02001305
1306 /*
1307 * Interrupt transfers requiring several transactions are not supported
1308 * because bInterval is ignored.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +02001309 *
1310 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +02001311 * <= PKT_ALIGN if several qTDs are required, while the USB
1312 * specification does not constrain this for interrupt transfers. That
1313 * means that ehci_submit_async() would support interrupt transfers
1314 * requiring several transactions only as long as the transfer size does
1315 * not require more than a single qTD.
Benoît Thébaudeau58c4dfb2012-08-09 23:50:44 +02001316 */
1317 if (length > usb_maxpacket(dev, pipe)) {
Patrick Georgie55fdac2013-03-06 14:08:31 +00001318 printf("%s: Interrupt transfers requiring several "
1319 "transactions are not supported.\n", __func__);
Benoît Thébaudeau58c4dfb2012-08-09 23:50:44 +02001320 return -1;
1321 }
Patrick Georgie55fdac2013-03-06 14:08:31 +00001322
1323 queue = create_int_queue(dev, pipe, 1, length, buffer);
1324
1325 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1326 while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
1327 if (get_timer(0) > timeout) {
1328 printf("Timeout poll on interrupt endpoint\n");
1329 result = -ETIMEDOUT;
1330 break;
1331 }
1332
1333 if (backbuffer != buffer) {
1334 debug("got wrong buffer back (%x instead of %x)\n",
1335 (uint32_t)backbuffer, (uint32_t)buffer);
1336 return -EINVAL;
1337 }
1338
Stephen Warren36dad662013-05-24 15:03:17 -06001339 invalidate_dcache_range((uint32_t)buffer,
1340 ALIGN_END_ADDR(char, buffer, length));
1341
Patrick Georgie55fdac2013-03-06 14:08:31 +00001342 ret = destroy_int_queue(dev, queue);
1343 if (ret < 0)
1344 return ret;
1345
1346 /* everything worked out fine */
1347 return result;
Marek Vasut9b315fe2011-09-25 21:07:56 +02001348}