blob: 41af302e036cf66e2b880240e23ea0ddb507c55b [file] [log] [blame]
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001/*-
2 * Copyright (c) 2007-2008, Juniper Networks, Inc.
michaeldb632992008-12-10 17:55:19 +01003 * Copyright (c) 2008, Excito Elektronik i Skåne AB
Remy Böhmerc0d722f2008-12-13 22:51:58 +01004 * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
5 *
Michael Trimarchiaaf098c2008-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 Trimarchiaaf098c2008-11-28 13:20:46 +010023#include <common.h>
Patrick Georgi8f62ca62013-03-06 14:08:31 +000024#include <errno.h>
michaeldb632992008-12-10 17:55:19 +010025#include <asm/byteorder.h>
Lucas Stach93ad9082012-09-06 08:00:13 +020026#include <asm/unaligned.h>
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010027#include <usb.h>
28#include <asm/io.h>
michaeldb632992008-12-10 17:55:19 +010029#include <malloc.h>
Stefan Roese67333f72010-11-26 15:43:28 +010030#include <watchdog.h>
Patrick Georgi8f62ca62013-03-06 14:08:31 +000031#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020032
33#include "ehci.h"
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010034
Lucas Stach676ae062012-09-26 00:14:35 +020035#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
36#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
37#endif
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010038
Julius Werner5077f962013-09-24 10:53:07 -070039/*
40 * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
41 * Let's time out after 8 to have a little safety margin on top of that.
42 */
43#define HCHALT_TIMEOUT (8 * 1000)
44
Marek Vasutb9596552013-07-10 03:16:31 +020045static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
Tom Rini71c5de42012-07-15 22:14:24 +000046
47#define ALIGN_END_ADDR(type, ptr, size) \
48 ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010049
michaeldb632992008-12-10 17:55:19 +010050static struct descriptor {
51 struct usb_hub_descriptor hub;
52 struct usb_device_descriptor device;
53 struct usb_linux_config_descriptor config;
54 struct usb_linux_interface_descriptor interface;
55 struct usb_endpoint_descriptor endpoint;
56} __attribute__ ((packed)) descriptor = {
57 {
58 0x8, /* bDescLength */
59 0x29, /* bDescriptorType: hub descriptor */
60 2, /* bNrPorts -- runtime modified */
61 0, /* wHubCharacteristics */
Vincent Palatin5f4b4f22011-12-05 14:52:22 -080062 10, /* bPwrOn2PwrGood */
michaeldb632992008-12-10 17:55:19 +010063 0, /* bHubCntrCurrent */
64 {}, /* Device removable */
65 {} /* at most 7 ports! XXX */
66 },
67 {
68 0x12, /* bLength */
69 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030070 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michaeldb632992008-12-10 17:55:19 +010071 9, /* bDeviceClass: UDCLASS_HUB */
72 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
73 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
74 64, /* bMaxPacketSize: 64 bytes */
75 0x0000, /* idVendor */
76 0x0000, /* idProduct */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030077 cpu_to_le16(0x0100), /* bcdDevice */
michaeldb632992008-12-10 17:55:19 +010078 1, /* iManufacturer */
79 2, /* iProduct */
80 0, /* iSerialNumber */
81 1 /* bNumConfigurations: 1 */
82 },
83 {
84 0x9,
85 2, /* bDescriptorType: UDESC_CONFIG */
86 cpu_to_le16(0x19),
87 1, /* bNumInterface */
88 1, /* bConfigurationValue */
89 0, /* iConfiguration */
90 0x40, /* bmAttributes: UC_SELF_POWER */
91 0 /* bMaxPower */
92 },
93 {
94 0x9, /* bLength */
95 4, /* bDescriptorType: UDESC_INTERFACE */
96 0, /* bInterfaceNumber */
97 0, /* bAlternateSetting */
98 1, /* bNumEndpoints */
99 9, /* bInterfaceClass: UICLASS_HUB */
100 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
101 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
102 0 /* iInterface */
103 },
104 {
105 0x7, /* bLength */
106 5, /* bDescriptorType: UDESC_ENDPOINT */
107 0x81, /* bEndpointAddress:
108 * UE_DIR_IN | EHCI_INTR_ENDPT
109 */
110 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix8f8bd562009-10-31 12:37:38 -0500111 8, /* wMaxPacketSize */
michaeldb632992008-12-10 17:55:19 +0100112 255 /* bInterval */
113 },
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100114};
115
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100116#if defined(CONFIG_EHCI_IS_TDI)
117#define ehci_is_TDI() (1)
118#else
119#define ehci_is_TDI() (0)
120#endif
121
Jim Linb068deb2013-03-27 00:52:32 +0000122int __ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
123{
124 return PORTSC_PSPD(reg);
125}
126
127int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
128 __attribute__((weak, alias("__ehci_get_port_speed")));
129
130void __ehci_set_usbmode(int index)
131{
132 uint32_t tmp;
133 uint32_t *reg_ptr;
134
135 reg_ptr = (uint32_t *)((u8 *)&ehcic[index].hcor->or_usbcmd + USBMODE);
136 tmp = ehci_readl(reg_ptr);
137 tmp |= USBMODE_CM_HC;
138#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
139 tmp |= USBMODE_BE;
140#endif
141 ehci_writel(reg_ptr, tmp);
142}
143
144void ehci_set_usbmode(int index)
145 __attribute__((weak, alias("__ehci_set_usbmode")));
146
Marek Vasut3874b6d2011-07-11 02:37:01 +0200147void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
148{
149 mdelay(50);
150}
151
152void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
153 __attribute__((weak, alias("__ehci_powerup_fixup")));
154
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100155static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michaeldb632992008-12-10 17:55:19 +0100156{
michael51ab1422008-12-11 13:43:55 +0100157 uint32_t result;
158 do {
159 result = ehci_readl(ptr);
Wolfgang Denk09c83a42010-10-22 14:23:00 +0200160 udelay(5);
michael51ab1422008-12-11 13:43:55 +0100161 if (result == ~(uint32_t)0)
162 return -1;
163 result &= mask;
164 if (result == done)
165 return 0;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100166 usec--;
167 } while (usec > 0);
michael51ab1422008-12-11 13:43:55 +0100168 return -1;
169}
170
Lucas Stach676ae062012-09-26 00:14:35 +0200171static int ehci_reset(int index)
michael51ab1422008-12-11 13:43:55 +0100172{
173 uint32_t cmd;
michael51ab1422008-12-11 13:43:55 +0100174 int ret = 0;
175
Lucas Stach676ae062012-09-26 00:14:35 +0200176 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Stefan Roese273d7202010-11-26 15:44:00 +0100177 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Lucas Stach676ae062012-09-26 00:14:35 +0200178 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
179 ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
180 CMD_RESET, 0, 250 * 1000);
michael51ab1422008-12-11 13:43:55 +0100181 if (ret < 0) {
182 printf("EHCI fail to reset\n");
183 goto out;
184 }
185
Jim Linb068deb2013-03-27 00:52:32 +0000186 if (ehci_is_TDI())
187 ehci_set_usbmode(index);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000188
189#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Lucas Stach676ae062012-09-26 00:14:35 +0200190 cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200191 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass9ab4ce22012-02-27 10:52:47 +0000192 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Lucas Stach676ae062012-09-26 00:14:35 +0200193 ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000194#endif
michael51ab1422008-12-11 13:43:55 +0100195out:
196 return ret;
michaeldb632992008-12-10 17:55:19 +0100197}
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100198
Julius Werner5077f962013-09-24 10:53:07 -0700199static int ehci_shutdown(struct ehci_ctrl *ctrl)
200{
201 int i, ret = 0;
202 uint32_t cmd, reg;
203
Marek Vasut1e1be6d2013-12-14 02:03:11 +0100204 if (!ctrl || !ctrl->hcor)
205 return -EINVAL;
206
Julius Werner5077f962013-09-24 10:53:07 -0700207 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
208 cmd &= ~(CMD_PSE | CMD_ASE);
209 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
210 ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
211 100 * 1000);
212
213 if (!ret) {
214 for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
215 reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
216 reg |= EHCI_PS_SUSP;
217 ehci_writel(&ctrl->hcor->or_portsc[i], reg);
218 }
219
220 cmd &= ~CMD_RUN;
221 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
222 ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
223 HCHALT_TIMEOUT);
224 }
225
226 if (ret)
227 puts("EHCI failed to shut down host controller.\n");
228
229 return ret;
230}
231
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100232static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
233{
Marek Vasutb8adb122012-04-09 04:07:46 +0200234 uint32_t delta, next;
235 uint32_t addr = (uint32_t)buf;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100236 int idx;
237
Ilya Yanok189a6952012-07-15 04:43:49 +0000238 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutb8adb122012-04-09 04:07:46 +0200239 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
240
Ilya Yanok189a6952012-07-15 04:43:49 +0000241 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
242
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100243 idx = 0;
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200244 while (idx < QT_BUFFER_CNT) {
michaeldb632992008-12-10 17:55:19 +0100245 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denk3ed16072010-10-19 16:13:15 +0200246 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200247 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100248 delta = next - addr;
249 if (delta >= sz)
250 break;
251 sz -= delta;
252 addr = next;
253 idx++;
254 }
255
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200256 if (idx == QT_BUFFER_CNT) {
Ilya Yanok2af16f82012-07-15 04:43:52 +0000257 printf("out of buffer pointers (%u bytes left)\n", sz);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100258 return -1;
259 }
260
261 return 0;
262}
263
Ilya Yanokc60795f2012-11-06 13:48:20 +0000264static inline u8 ehci_encode_speed(enum usb_device_speed speed)
265{
266 #define QH_HIGH_SPEED 2
267 #define QH_FULL_SPEED 0
268 #define QH_LOW_SPEED 1
269 if (speed == USB_SPEED_HIGH)
270 return QH_HIGH_SPEED;
271 if (speed == USB_SPEED_LOW)
272 return QH_LOW_SPEED;
273 return QH_FULL_SPEED;
274}
275
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200276static void ehci_update_endpt2_dev_n_port(struct usb_device *dev,
277 struct QH *qh)
278{
279 struct usb_device *ttdev;
280
281 if (dev->speed != USB_SPEED_LOW && dev->speed != USB_SPEED_FULL)
282 return;
283
284 /*
285 * For full / low speed devices we need to get the devnum and portnr of
286 * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
287 * in the tree before that one!
288 */
289 ttdev = dev;
290 while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
291 ttdev = ttdev->parent;
292 if (!ttdev->parent)
293 return;
294
295 qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
296 QH_ENDPT2_HUBADDR(ttdev->parent->devnum));
297}
298
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100299static int
300ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
301 int length, struct devrequest *req)
302{
Tom Rini71c5de42012-07-15 22:14:24 +0000303 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200304 struct qTD *qtd;
305 int qtd_count = 0;
Marek Vasutde98e8b2012-04-08 23:32:05 +0200306 int qtd_counter = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100307 volatile struct qTD *vtd;
308 unsigned long ts;
309 uint32_t *tdp;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200310 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100311 uint32_t c, toggle;
michaeldb632992008-12-10 17:55:19 +0100312 uint32_t cmd;
Simon Glass96820a32011-02-07 14:42:16 -0800313 int timeout;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100314 int ret = 0;
Lucas Stach676ae062012-09-26 00:14:35 +0200315 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100316
michaeldb632992008-12-10 17:55:19 +0100317 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100318 buffer, length, req);
319 if (req != NULL)
michaeldb632992008-12-10 17:55:19 +0100320 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100321 req->request, req->request,
322 req->requesttype, req->requesttype,
323 le16_to_cpu(req->value), le16_to_cpu(req->value),
michaeldb632992008-12-10 17:55:19 +0100324 le16_to_cpu(req->index));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100325
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200326#define PKT_ALIGN 512
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200327 /*
328 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
329 * described by a transfer descriptor (the qTD). The qTDs form a linked
330 * list with a queue head (QH).
331 *
332 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
333 * have its beginning in a qTD transfer and its end in the following
334 * one, so the qTD transfer lengths have to be chosen accordingly.
335 *
336 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
337 * single pages. The first data buffer can start at any offset within a
338 * page (not considering the cache-line alignment issues), while the
339 * following buffers must be page-aligned. There is no alignment
340 * constraint on the size of a qTD transfer.
341 */
342 if (req != NULL)
343 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
344 qtd_count += 1 + 1;
345 if (length > 0 || req == NULL) {
346 /*
347 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200348 * data payload (not considering the first qTD transfer, which
349 * may be longer or shorter, and the final one, which may be
350 * shorter).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200351 *
352 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200353 * transfer size is aligned to PKT_ALIGN, which is a multiple of
354 * wMaxPacketSize (except in some cases for interrupt transfers,
355 * see comment in submit_int_msg()).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200356 *
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200357 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200358 * QT_BUFFER_CNT full pages will be used.
359 */
360 int xfr_sz = QT_BUFFER_CNT;
361 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200362 * However, if the input buffer is not aligned to PKT_ALIGN, the
363 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200364 * data buffer of each transfer will be page-unaligned.
365 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200366 if ((uint32_t)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200367 xfr_sz--;
368 /* Convert the qTD transfer size to bytes. */
369 xfr_sz *= EHCI_PAGE_SIZE;
370 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200371 * Approximate by excess the number of qTDs that will be
372 * required for the data payload. The exact formula is way more
373 * complicated and saves at most 2 qTDs, i.e. a total of 128
374 * bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200375 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200376 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200377 }
378/*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200379 * Threshold value based on the worst-case total size of the allocated qTDs for
380 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200381 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200382#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200383#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
384#endif
385 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
386 if (qtd == NULL) {
387 printf("unable to allocate TDs\n");
388 return -1;
389 }
390
Tom Rini71c5de42012-07-15 22:14:24 +0000391 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200392 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200393
Marek Vasutb8adb122012-04-09 04:07:46 +0200394 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
395
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200396 /*
397 * Setup QH (3.6 in ehci-r10.pdf)
398 *
399 * qh_link ................. 03-00 H
400 * qh_endpt1 ............... 07-04 H
401 * qh_endpt2 ............... 0B-08 H
402 * - qh_curtd
403 * qh_overlay.qt_next ...... 13-10 H
404 * - qh_overlay.qt_altnext
405 */
Lucas Stach676ae062012-09-26 00:14:35 +0200406 qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
Ilya Yanokc60795f2012-11-06 13:48:20 +0000407 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200408 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200409 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200410 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200411 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
Ilya Yanokc60795f2012-11-06 13:48:20 +0000412 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200413 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
414 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini71c5de42012-07-15 22:14:24 +0000415 qh->qh_endpt1 = cpu_to_hc32(endpt);
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200416 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini71c5de42012-07-15 22:14:24 +0000417 qh->qh_endpt2 = cpu_to_hc32(endpt);
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200418 ehci_update_endpt2_dev_n_port(dev, qh);
Tom Rini71c5de42012-07-15 22:14:24 +0000419 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Stephen Warren2456b972014-02-07 09:53:50 -0700420 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100421
Tom Rini71c5de42012-07-15 22:14:24 +0000422 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100423
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100424 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200425 /*
426 * Setup request qTD (3.5 in ehci-r10.pdf)
427 *
428 * qt_next ................ 03-00 H
429 * qt_altnext ............. 07-04 H
430 * qt_token ............... 0B-08 H
431 *
432 * [ buffer, buffer_hi ] loaded with "req".
433 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200434 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
435 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200436 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
437 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
438 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
439 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200440 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200441 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
442 printf("unable to construct SETUP TD\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100443 goto fail;
444 }
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200445 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200446 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
447 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100448 toggle = 1;
449 }
450
451 if (length > 0 || req == NULL) {
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200452 uint8_t *buf_ptr = buffer;
453 int left_length = length;
454
455 do {
456 /*
457 * Determine the size of this qTD transfer. By default,
458 * QT_BUFFER_CNT full pages can be used.
459 */
460 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
461 /*
462 * However, if the input buffer is not page-aligned, the
463 * portion of the first page before the buffer start
464 * offset within that page is unusable.
465 */
466 xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
467 /*
468 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200469 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200470 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200471 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200472 /*
473 * This transfer may be shorter than the available qTD
474 * transfer size that has just been computed.
475 */
476 xfr_bytes = min(xfr_bytes, left_length);
477
478 /*
479 * Setup request qTD (3.5 in ehci-r10.pdf)
480 *
481 * qt_next ................ 03-00 H
482 * qt_altnext ............. 07-04 H
483 * qt_token ............... 0B-08 H
484 *
485 * [ buffer, buffer_hi ] loaded with "buffer".
486 */
487 qtd[qtd_counter].qt_next =
488 cpu_to_hc32(QT_NEXT_TERMINATE);
489 qtd[qtd_counter].qt_altnext =
490 cpu_to_hc32(QT_NEXT_TERMINATE);
491 token = QT_TOKEN_DT(toggle) |
492 QT_TOKEN_TOTALBYTES(xfr_bytes) |
493 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
494 QT_TOKEN_CERR(3) |
495 QT_TOKEN_PID(usb_pipein(pipe) ?
496 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
497 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
498 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
499 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
500 xfr_bytes)) {
501 printf("unable to construct DATA TD\n");
502 goto fail;
503 }
504 /* Update previous qTD! */
505 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
506 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200507 /*
508 * Data toggle has to be adjusted since the qTD transfer
509 * size is not always an even multiple of
510 * wMaxPacketSize.
511 */
512 if ((xfr_bytes / maxpacket) & 1)
513 toggle ^= 1;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200514 buf_ptr += xfr_bytes;
515 left_length -= xfr_bytes;
516 } while (left_length > 0);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100517 }
518
519 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200520 /*
521 * Setup request qTD (3.5 in ehci-r10.pdf)
522 *
523 * qt_next ................ 03-00 H
524 * qt_altnext ............. 07-04 H
525 * qt_token ............... 0B-08 H
526 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200527 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
528 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200529 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200530 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
531 QT_TOKEN_PID(usb_pipein(pipe) ?
532 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
533 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200534 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200535 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200536 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
537 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100538 }
539
Lucas Stach676ae062012-09-26 00:14:35 +0200540 ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100541
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100542 /* Flush dcache */
Lucas Stach676ae062012-09-26 00:14:35 +0200543 flush_dcache_range((uint32_t)&ctrl->qh_list,
544 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Tom Rini71c5de42012-07-15 22:14:24 +0000545 flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200546 flush_dcache_range((uint32_t)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200547 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100548
Ilya Yanokc7701af2012-07-15 22:12:08 +0000549 /* Set async. queue head pointer. */
Lucas Stach676ae062012-09-26 00:14:35 +0200550 ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
Ilya Yanokc7701af2012-07-15 22:12:08 +0000551
Lucas Stach676ae062012-09-26 00:14:35 +0200552 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
553 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100554
555 /* Enable async. schedule. */
Lucas Stach676ae062012-09-26 00:14:35 +0200556 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael51ab1422008-12-11 13:43:55 +0100557 cmd |= CMD_ASE;
Lucas Stach676ae062012-09-26 00:14:35 +0200558 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michaeldb632992008-12-10 17:55:19 +0100559
Lucas Stach676ae062012-09-26 00:14:35 +0200560 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100561 100 * 1000);
562 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200563 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100564 goto fail;
michael51ab1422008-12-11 13:43:55 +0100565 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100566
567 /* Wait for TDs to be processed. */
568 ts = get_timer(0);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200569 vtd = &qtd[qtd_counter - 1];
Simon Glass96820a32011-02-07 14:42:16 -0800570 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100571 do {
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100572 /* Invalidate dcache */
Lucas Stach676ae062012-09-26 00:14:35 +0200573 invalidate_dcache_range((uint32_t)&ctrl->qh_list,
574 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Tom Rini71c5de42012-07-15 22:14:24 +0000575 invalidate_dcache_range((uint32_t)qh,
576 ALIGN_END_ADDR(struct QH, qh, 1));
Marek Vasutb8adb122012-04-09 04:07:46 +0200577 invalidate_dcache_range((uint32_t)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200578 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutb8adb122012-04-09 04:07:46 +0200579
michaeldb632992008-12-10 17:55:19 +0100580 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200581 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100582 break;
Stefan Roese67333f72010-11-26 15:43:28 +0100583 WATCHDOG_RESET();
Simon Glass96820a32011-02-07 14:42:16 -0800584 } while (get_timer(ts) < timeout);
585
Ilya Yanok189a6952012-07-15 04:43:49 +0000586 /*
587 * Invalidate the memory area occupied by buffer
588 * Don't try to fix the buffer alignment, if it isn't properly
589 * aligned it's upper layer's fault so let invalidate_dcache_range()
590 * vow about it. But we have to fix the length as it's actual
591 * transfer length and can be unaligned. This is potentially
592 * dangerous operation, it's responsibility of the calling
593 * code to make sure enough space is reserved.
594 */
595 invalidate_dcache_range((uint32_t)buffer,
596 ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutb8adb122012-04-09 04:07:46 +0200597
Simon Glass96820a32011-02-07 14:42:16 -0800598 /* Check that the TD processing happened */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200599 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glass96820a32011-02-07 14:42:16 -0800600 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100601
602 /* Disable async schedule. */
Lucas Stach676ae062012-09-26 00:14:35 +0200603 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michaeldb632992008-12-10 17:55:19 +0100604 cmd &= ~CMD_ASE;
Lucas Stach676ae062012-09-26 00:14:35 +0200605 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +0100606
Lucas Stach676ae062012-09-26 00:14:35 +0200607 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100608 100 * 1000);
609 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200610 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100611 goto fail;
michael51ab1422008-12-11 13:43:55 +0100612 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100613
Tom Rini71c5de42012-07-15 22:14:24 +0000614 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200615 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michaeldb632992008-12-10 17:55:19 +0100616 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200617 switch (QT_TOKEN_GET_STATUS(token) &
618 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100619 case 0:
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200620 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100621 usb_settoggle(dev, usb_pipeendpoint(pipe),
622 usb_pipeout(pipe), toggle);
623 dev->status = 0;
624 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200625 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100626 dev->status = USB_ST_STALLED;
627 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200628 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
629 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100630 dev->status = USB_ST_BUF_ERR;
631 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200632 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
633 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100634 dev->status = USB_ST_BABBLE_DET;
635 break;
636 default:
637 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200638 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschin222d6df2010-11-02 11:47:29 +0100639 dev->status |= USB_ST_STALLED;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100640 break;
641 }
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200642 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100643 } else {
644 dev->act_len = 0;
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800645#ifndef CONFIG_USB_EHCI_FARADAY
michaeldb632992008-12-10 17:55:19 +0100646 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach676ae062012-09-26 00:14:35 +0200647 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
648 ehci_readl(&ctrl->hcor->or_portsc[0]),
649 ehci_readl(&ctrl->hcor->or_portsc[1]));
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800650#endif
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100651 }
652
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200653 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100654 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
655
656fail:
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200657 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100658 return -1;
659}
660
Kuo-Jung Su1dde1422013-05-15 15:29:21 +0800661__weak uint32_t *ehci_get_portsc_register(struct ehci_hcor *hcor, int port)
662{
663 if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
664 /* Printing the message would cause a scan failure! */
665 debug("The request port(%u) is not configured\n", port);
666 return NULL;
667 }
668
669 return (uint32_t *)&hcor->or_portsc[port];
670}
671
michaeldb632992008-12-10 17:55:19 +0100672int
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100673ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
674 int length, struct devrequest *req)
675{
676 uint8_t tmpbuf[4];
677 u16 typeReq;
michaeldb632992008-12-10 17:55:19 +0100678 void *srcptr = NULL;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100679 int len, srclen;
680 uint32_t reg;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100681 uint32_t *status_reg;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000682 int port = le16_to_cpu(req->index) & 0xff;
Lucas Stach676ae062012-09-26 00:14:35 +0200683 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100684
685 srclen = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100686
michaeldb632992008-12-10 17:55:19 +0100687 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100688 req->request, req->request,
689 req->requesttype, req->requesttype,
690 le16_to_cpu(req->value), le16_to_cpu(req->index));
691
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530692 typeReq = req->request | req->requesttype << 8;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100693
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530694 switch (typeReq) {
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800695 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
696 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
697 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Kuo-Jung Su1dde1422013-05-15 15:29:21 +0800698 status_reg = ehci_get_portsc_register(ctrl->hcor, port - 1);
699 if (!status_reg)
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800700 return -1;
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800701 break;
702 default:
703 status_reg = NULL;
704 break;
705 }
706
707 switch (typeReq) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100708 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
709 switch (le16_to_cpu(req->value) >> 8) {
710 case USB_DT_DEVICE:
michaeldb632992008-12-10 17:55:19 +0100711 debug("USB_DT_DEVICE request\n");
712 srcptr = &descriptor.device;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200713 srclen = descriptor.device.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100714 break;
715 case USB_DT_CONFIG:
michaeldb632992008-12-10 17:55:19 +0100716 debug("USB_DT_CONFIG config\n");
717 srcptr = &descriptor.config;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200718 srclen = descriptor.config.bLength +
719 descriptor.interface.bLength +
720 descriptor.endpoint.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100721 break;
722 case USB_DT_STRING:
michaeldb632992008-12-10 17:55:19 +0100723 debug("USB_DT_STRING config\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100724 switch (le16_to_cpu(req->value) & 0xff) {
725 case 0: /* Language */
726 srcptr = "\4\3\1\0";
727 srclen = 4;
728 break;
729 case 1: /* Vendor */
730 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
731 srclen = 14;
732 break;
733 case 2: /* Product */
734 srcptr = "\52\3E\0H\0C\0I\0 "
735 "\0H\0o\0s\0t\0 "
736 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
737 srclen = 42;
738 break;
739 default:
michaeldb632992008-12-10 17:55:19 +0100740 debug("unknown value DT_STRING %x\n",
741 le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100742 goto unknown;
743 }
744 break;
745 default:
michaeldb632992008-12-10 17:55:19 +0100746 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100747 goto unknown;
748 }
749 break;
750 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
751 switch (le16_to_cpu(req->value) >> 8) {
752 case USB_DT_HUB:
michaeldb632992008-12-10 17:55:19 +0100753 debug("USB_DT_HUB config\n");
754 srcptr = &descriptor.hub;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200755 srclen = descriptor.hub.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100756 break;
757 default:
michaeldb632992008-12-10 17:55:19 +0100758 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100759 goto unknown;
760 }
761 break;
762 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michaeldb632992008-12-10 17:55:19 +0100763 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach676ae062012-09-26 00:14:35 +0200764 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100765 break;
766 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michaeldb632992008-12-10 17:55:19 +0100767 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100768 /* Nothing to do */
769 break;
770 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
771 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
772 tmpbuf[1] = 0;
773 srcptr = tmpbuf;
774 srclen = 2;
775 break;
michaeldb632992008-12-10 17:55:19 +0100776 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100777 memset(tmpbuf, 0, 4);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100778 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100779 if (reg & EHCI_PS_CS)
780 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
781 if (reg & EHCI_PS_PE)
782 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
783 if (reg & EHCI_PS_SUSP)
784 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
785 if (reg & EHCI_PS_OCA)
786 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300787 if (reg & EHCI_PS_PR)
788 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100789 if (reg & EHCI_PS_PP)
790 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese597eb282009-01-21 17:12:01 +0100791
792 if (ehci_is_TDI()) {
Jim Linb068deb2013-03-27 00:52:32 +0000793 switch (ehci_get_port_speed(ctrl->hcor, reg)) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200794 case PORTSC_PSPD_FS:
Stefan Roese597eb282009-01-21 17:12:01 +0100795 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200796 case PORTSC_PSPD_LS:
Stefan Roese597eb282009-01-21 17:12:01 +0100797 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
798 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200799 case PORTSC_PSPD_HS:
Stefan Roese597eb282009-01-21 17:12:01 +0100800 default:
801 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
802 break;
803 }
804 } else {
805 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
806 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100807
808 if (reg & EHCI_PS_CSC)
809 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
810 if (reg & EHCI_PS_PEC)
811 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
812 if (reg & EHCI_PS_OCC)
813 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000814 if (ctrl->portreset & (1 << port))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100815 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100816
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100817 srcptr = tmpbuf;
818 srclen = 4;
819 break;
michaeldb632992008-12-10 17:55:19 +0100820 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100821 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100822 reg &= ~EHCI_PS_CLEAR;
823 switch (le16_to_cpu(req->value)) {
michael51ab1422008-12-11 13:43:55 +0100824 case USB_PORT_FEAT_ENABLE:
825 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100826 ehci_writel(status_reg, reg);
michael51ab1422008-12-11 13:43:55 +0100827 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100828 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200829 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100830 reg |= EHCI_PS_PP;
831 ehci_writel(status_reg, reg);
832 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100833 break;
834 case USB_PORT_FEAT_RESET:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100835 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
836 !ehci_is_TDI() &&
837 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100838 /* Low speed device, give up ownership. */
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100839 debug("port %d low speed --> companion\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000840 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100841 reg |= EHCI_PS_PO;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100842 ehci_writel(status_reg, reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100843 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100844 } else {
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300845 int ret;
846
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100847 reg |= EHCI_PS_PR;
848 reg &= ~EHCI_PS_PE;
849 ehci_writel(status_reg, reg);
850 /*
851 * caller must wait, then call GetPortStatus
852 * usb 2.0 specification say 50 ms resets on
853 * root
854 */
Marek Vasut3874b6d2011-07-11 02:37:01 +0200855 ehci_powerup_fixup(status_reg, &reg);
856
Chris Zhangb4161912010-01-06 13:34:04 -0800857 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300858 /*
859 * A host controller must terminate the reset
860 * and stabilize the state of the port within
861 * 2 milliseconds
862 */
863 ret = handshake(status_reg, EHCI_PS_PR, 0,
864 2 * 1000);
865 if (!ret)
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000866 ctrl->portreset |= 1 << port;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300867 else
868 printf("port(%d) reset error\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000869 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100870 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100871 break;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000872 case USB_PORT_FEAT_TEST:
Julius Werner5077f962013-09-24 10:53:07 -0700873 ehci_shutdown(ctrl);
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000874 reg &= ~(0xf << 16);
875 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
876 ehci_writel(status_reg, reg);
877 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100878 default:
michaeldb632992008-12-10 17:55:19 +0100879 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100880 goto unknown;
881 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100882 /* unblock posted writes */
Lucas Stach676ae062012-09-26 00:14:35 +0200883 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100884 break;
michaeldb632992008-12-10 17:55:19 +0100885 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100886 reg = ehci_readl(status_reg);
Simon Glassed10e662013-05-10 19:49:00 -0700887 reg &= ~EHCI_PS_CLEAR;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100888 switch (le16_to_cpu(req->value)) {
889 case USB_PORT_FEAT_ENABLE:
890 reg &= ~EHCI_PS_PE;
891 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100892 case USB_PORT_FEAT_C_ENABLE:
Simon Glassed10e662013-05-10 19:49:00 -0700893 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100894 break;
895 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200896 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Simon Glassed10e662013-05-10 19:49:00 -0700897 reg &= ~EHCI_PS_PP;
898 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100899 case USB_PORT_FEAT_C_CONNECTION:
Simon Glassed10e662013-05-10 19:49:00 -0700900 reg |= EHCI_PS_CSC;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100901 break;
michael51ab1422008-12-11 13:43:55 +0100902 case USB_PORT_FEAT_OVER_CURRENT:
Simon Glassed10e662013-05-10 19:49:00 -0700903 reg |= EHCI_PS_OCC;
michael51ab1422008-12-11 13:43:55 +0100904 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100905 case USB_PORT_FEAT_C_RESET:
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000906 ctrl->portreset &= ~(1 << port);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100907 break;
908 default:
michaeldb632992008-12-10 17:55:19 +0100909 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100910 goto unknown;
911 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100912 ehci_writel(status_reg, reg);
913 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +0200914 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100915 break;
916 default:
michaeldb632992008-12-10 17:55:19 +0100917 debug("Unknown request\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100918 goto unknown;
919 }
920
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000921 mdelay(1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100922 len = min3(srclen, le16_to_cpu(req->length), length);
923 if (srcptr != NULL && len > 0)
924 memcpy(buffer, srcptr, len);
michaeldb632992008-12-10 17:55:19 +0100925 else
926 debug("Len is 0\n");
927
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100928 dev->act_len = len;
929 dev->status = 0;
930 return 0;
931
932unknown:
michaeldb632992008-12-10 17:55:19 +0100933 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100934 req->requesttype, req->request, le16_to_cpu(req->value),
935 le16_to_cpu(req->index), le16_to_cpu(req->length));
936
937 dev->act_len = 0;
938 dev->status = USB_ST_STALLED;
939 return -1;
940}
941
Lucas Stachc7e3b2b2012-09-26 00:14:34 +0200942int usb_lowlevel_stop(int index)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100943{
Julius Werner5077f962013-09-24 10:53:07 -0700944 ehci_shutdown(&ehcic[index]);
Lucas Stach676ae062012-09-26 00:14:35 +0200945 return ehci_hcd_stop(index);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100946}
947
Troy Kisky06d513e2013-10-10 15:27:56 -0700948int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100949{
950 uint32_t reg;
michaeldb632992008-12-10 17:55:19 +0100951 uint32_t cmd;
Lucas Stach676ae062012-09-26 00:14:35 +0200952 struct QH *qh_list;
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000953 struct QH *periodic;
954 int i;
Troy Kisky127efc42013-10-10 15:27:57 -0700955 int rc;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100956
Troy Kisky127efc42013-10-10 15:27:57 -0700957 rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
958 if (rc)
959 return rc;
960 if (init == USB_INIT_DEVICE)
961 goto done;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100962
michael51ab1422008-12-11 13:43:55 +0100963 /* EHCI spec section 4.1 */
Lucas Stach676ae062012-09-26 00:14:35 +0200964 if (ehci_reset(index))
michael51ab1422008-12-11 13:43:55 +0100965 return -1;
966
Stefan Roese832e6142009-01-21 17:12:10 +0100967#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
Troy Kisky127efc42013-10-10 15:27:57 -0700968 rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
969 if (rc)
970 return rc;
Stefan Roese832e6142009-01-21 17:12:10 +0100971#endif
Vincent Palatin29828372012-12-12 17:55:22 -0800972 /* Set the high address word (aka segment) for 64-bit controller */
973 if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
Marek Vasuteb632182013-12-14 02:04:52 +0100974 ehci_writel(&ehcic[index].hcor->or_ctrldssegment, 0);
Stefan Roese832e6142009-01-21 17:12:10 +0100975
Lucas Stach676ae062012-09-26 00:14:35 +0200976 qh_list = &ehcic[index].qh_list;
977
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100978 /* Set head of reclaim list */
Tom Rini71c5de42012-07-15 22:14:24 +0000979 memset(qh_list, 0, sizeof(*qh_list));
980 qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200981 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
982 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini71c5de42012-07-15 22:14:24 +0000983 qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
984 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
985 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200986 qh_list->qh_overlay.qt_token =
987 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100988
Stephen Warrend3e07472013-05-24 15:03:17 -0600989 flush_dcache_range((uint32_t)qh_list,
990 ALIGN_END_ADDR(struct QH, qh_list, 1));
991
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000992 /* Set async. queue head pointer. */
993 ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (uint32_t)qh_list);
994
995 /*
996 * Set up periodic list
997 * Step 1: Parent QH for all periodic transfers.
998 */
999 periodic = &ehcic[index].periodic_queue;
1000 memset(periodic, 0, sizeof(*periodic));
1001 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
1002 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1003 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1004
Stephen Warrend3e07472013-05-24 15:03:17 -06001005 flush_dcache_range((uint32_t)periodic,
1006 ALIGN_END_ADDR(struct QH, periodic, 1));
1007
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001008 /*
1009 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
1010 * In particular, device specifications on polling frequency
1011 * are disregarded. Keyboards seem to send NAK/NYet reliably
1012 * when polled with an empty buffer.
1013 *
1014 * Split Transactions will be spread across microframes using
1015 * S-mask and C-mask.
1016 */
Nikita Kiryanov8bc36032013-07-29 13:27:40 +03001017 if (ehcic[index].periodic_list == NULL)
1018 ehcic[index].periodic_list = memalign(4096, 1024 * 4);
1019
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001020 if (!ehcic[index].periodic_list)
1021 return -ENOMEM;
1022 for (i = 0; i < 1024; i++) {
Adrian Coxea427772014-04-10 13:29:45 +01001023 ehcic[index].periodic_list[i] = cpu_to_hc32((uint32_t)periodic
1024 | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001025 }
1026
Stephen Warrend3e07472013-05-24 15:03:17 -06001027 flush_dcache_range((uint32_t)ehcic[index].periodic_list,
1028 ALIGN_END_ADDR(uint32_t, ehcic[index].periodic_list,
1029 1024));
1030
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001031 /* Set periodic list base address */
1032 ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
1033 (uint32_t)ehcic[index].periodic_list);
1034
Lucas Stach676ae062012-09-26 00:14:35 +02001035 reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
michael51ab1422008-12-11 13:43:55 +01001036 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stach7a46b2c2012-09-28 00:26:19 +02001037 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001038 /* Port Indicators */
1039 if (HCS_INDICATOR(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +02001040 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1041 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001042 /* Port Power Control */
1043 if (HCS_PPC(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +02001044 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1045 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001046
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001047 /* Start the host controller. */
Lucas Stach676ae062012-09-26 00:14:35 +02001048 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Wolfgang Denkf15c6512009-02-12 00:08:39 +01001049 /*
1050 * Philips, Intel, and maybe others need CMD_RUN before the
1051 * root hub will detect new devices (why?); NEC doesn't
1052 */
michael51ab1422008-12-11 13:43:55 +01001053 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
1054 cmd |= CMD_RUN;
Lucas Stach676ae062012-09-26 00:14:35 +02001055 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +01001056
Kuo-Jung Sue82a3162013-05-15 15:29:23 +08001057#ifndef CONFIG_USB_EHCI_FARADAY
michael51ab1422008-12-11 13:43:55 +01001058 /* take control over the ports */
Lucas Stach676ae062012-09-26 00:14:35 +02001059 cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
michael51ab1422008-12-11 13:43:55 +01001060 cmd |= FLAG_CF;
Lucas Stach676ae062012-09-26 00:14:35 +02001061 ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
Kuo-Jung Sue82a3162013-05-15 15:29:23 +08001062#endif
1063
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001064 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +02001065 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001066 mdelay(5);
Lucas Stach676ae062012-09-26 00:14:35 +02001067 reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001068 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001069
Lucas Stach676ae062012-09-26 00:14:35 +02001070 ehcic[index].rootdev = 0;
Troy Kisky127efc42013-10-10 15:27:57 -07001071done:
Lucas Stach676ae062012-09-26 00:14:35 +02001072 *controller = &ehcic[index];
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001073 return 0;
1074}
1075
1076int
1077submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1078 int length)
1079{
1080
1081 if (usb_pipetype(pipe) != PIPE_BULK) {
1082 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1083 return -1;
1084 }
1085 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1086}
1087
1088int
1089submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1090 int length, struct devrequest *setup)
1091{
Lucas Stach676ae062012-09-26 00:14:35 +02001092 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001093
1094 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1095 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1096 return -1;
1097 }
1098
Lucas Stach676ae062012-09-26 00:14:35 +02001099 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1100 if (!ctrl->rootdev)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001101 dev->speed = USB_SPEED_HIGH;
1102 return ehci_submit_root(dev, pipe, buffer, length, setup);
1103 }
1104 return ehci_submit_async(dev, pipe, buffer, length, setup);
1105}
1106
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001107struct int_queue {
1108 struct QH *first;
1109 struct QH *current;
1110 struct QH *last;
1111 struct qTD *tds;
1112};
1113
Adrian Coxea427772014-04-10 13:29:45 +01001114#define NEXT_QH(qh) (struct QH *)(hc32_to_cpu((qh)->qh_link) & ~0x1f)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001115
1116static int
1117enable_periodic(struct ehci_ctrl *ctrl)
1118{
1119 uint32_t cmd;
1120 struct ehci_hcor *hcor = ctrl->hcor;
1121 int ret;
1122
1123 cmd = ehci_readl(&hcor->or_usbcmd);
1124 cmd |= CMD_PSE;
1125 ehci_writel(&hcor->or_usbcmd, cmd);
1126
1127 ret = handshake((uint32_t *)&hcor->or_usbsts,
1128 STS_PSS, STS_PSS, 100 * 1000);
1129 if (ret < 0) {
1130 printf("EHCI failed: timeout when enabling periodic list\n");
1131 return -ETIMEDOUT;
1132 }
1133 udelay(1000);
1134 return 0;
1135}
1136
1137static int
1138disable_periodic(struct ehci_ctrl *ctrl)
1139{
1140 uint32_t cmd;
1141 struct ehci_hcor *hcor = ctrl->hcor;
1142 int ret;
1143
1144 cmd = ehci_readl(&hcor->or_usbcmd);
1145 cmd &= ~CMD_PSE;
1146 ehci_writel(&hcor->or_usbcmd, cmd);
1147
1148 ret = handshake((uint32_t *)&hcor->or_usbsts,
1149 STS_PSS, 0, 100 * 1000);
1150 if (ret < 0) {
1151 printf("EHCI failed: timeout when disabling periodic list\n");
1152 return -ETIMEDOUT;
1153 }
1154 return 0;
1155}
1156
1157static int periodic_schedules;
1158
1159struct int_queue *
1160create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
1161 int elementsize, void *buffer)
1162{
1163 struct ehci_ctrl *ctrl = dev->controller;
1164 struct int_queue *result = NULL;
1165 int i;
1166
1167 debug("Enter create_int_queue\n");
1168 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1169 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1170 return NULL;
1171 }
1172
1173 /* limit to 4 full pages worth of data -
1174 * we can safely fit them in a single TD,
1175 * no matter the alignment
1176 */
1177 if (elementsize >= 16384) {
1178 debug("too large elements for interrupt transfers\n");
1179 return NULL;
1180 }
1181
1182 result = malloc(sizeof(*result));
1183 if (!result) {
1184 debug("ehci intr queue: out of memory\n");
1185 goto fail1;
1186 }
Stephen Warren8165e342014-02-06 13:13:06 -07001187 result->first = memalign(USB_DMA_MINALIGN,
1188 sizeof(struct QH) * queuesize);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001189 if (!result->first) {
1190 debug("ehci intr queue: out of memory\n");
1191 goto fail2;
1192 }
1193 result->current = result->first;
1194 result->last = result->first + queuesize - 1;
Stephen Warren8165e342014-02-06 13:13:06 -07001195 result->tds = memalign(USB_DMA_MINALIGN,
1196 sizeof(struct qTD) * queuesize);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001197 if (!result->tds) {
1198 debug("ehci intr queue: out of memory\n");
1199 goto fail3;
1200 }
1201 memset(result->first, 0, sizeof(struct QH) * queuesize);
1202 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1203
1204 for (i = 0; i < queuesize; i++) {
1205 struct QH *qh = result->first + i;
1206 struct qTD *td = result->tds + i;
1207 void **buf = &qh->buffer;
1208
Adrian Coxea427772014-04-10 13:29:45 +01001209 qh->qh_link = cpu_to_hc32((uint32_t)(qh+1) | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001210 if (i == queuesize - 1)
Adrian Coxea427772014-04-10 13:29:45 +01001211 qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001212
Adrian Coxea427772014-04-10 13:29:45 +01001213 qh->qh_overlay.qt_next = cpu_to_hc32((uint32_t)td);
1214 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1215 qh->qh_endpt1 =
1216 cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001217 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1218 (1 << 14) |
1219 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1220 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
Adrian Coxea427772014-04-10 13:29:45 +01001221 (usb_pipedevice(pipe) << 0));
1222 qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1223 (1 << 0)); /* S-mask: microframe 0 */
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001224 if (dev->speed == USB_SPEED_LOW ||
1225 dev->speed == USB_SPEED_FULL) {
Hans de Goede4e2c4ad2014-09-20 16:51:22 +02001226 /* C-mask: microframes 2-4 */
1227 qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001228 }
Hans de Goede4e2c4ad2014-09-20 16:51:22 +02001229 ehci_update_endpt2_dev_n_port(dev, qh);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001230
Adrian Coxea427772014-04-10 13:29:45 +01001231 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1232 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001233 debug("communication direction is '%s'\n",
1234 usb_pipein(pipe) ? "in" : "out");
Adrian Coxea427772014-04-10 13:29:45 +01001235 td->qt_token = cpu_to_hc32((elementsize << 16) |
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001236 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
Adrian Coxea427772014-04-10 13:29:45 +01001237 0x80); /* active */
1238 td->qt_buffer[0] =
1239 cpu_to_hc32((uint32_t)buffer + i * elementsize);
1240 td->qt_buffer[1] =
1241 cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1242 td->qt_buffer[2] =
1243 cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1244 td->qt_buffer[3] =
1245 cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1246 td->qt_buffer[4] =
1247 cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001248
1249 *buf = buffer + i * elementsize;
1250 }
1251
Stephen Warrend3e07472013-05-24 15:03:17 -06001252 flush_dcache_range((uint32_t)buffer,
1253 ALIGN_END_ADDR(char, buffer,
1254 queuesize * elementsize));
1255 flush_dcache_range((uint32_t)result->first,
1256 ALIGN_END_ADDR(struct QH, result->first,
1257 queuesize));
1258 flush_dcache_range((uint32_t)result->tds,
1259 ALIGN_END_ADDR(struct qTD, result->tds,
1260 queuesize));
1261
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001262 if (disable_periodic(ctrl) < 0) {
1263 debug("FATAL: periodic should never fail, but did");
1264 goto fail3;
1265 }
1266
1267 /* hook up to periodic list */
1268 struct QH *list = &ctrl->periodic_queue;
1269 result->last->qh_link = list->qh_link;
Adrian Coxea427772014-04-10 13:29:45 +01001270 list->qh_link = cpu_to_hc32((uint32_t)result->first | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001271
Stephen Warrend3e07472013-05-24 15:03:17 -06001272 flush_dcache_range((uint32_t)result->last,
1273 ALIGN_END_ADDR(struct QH, result->last, 1));
1274 flush_dcache_range((uint32_t)list,
1275 ALIGN_END_ADDR(struct QH, list, 1));
1276
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001277 if (enable_periodic(ctrl) < 0) {
1278 debug("FATAL: periodic should never fail, but did");
1279 goto fail3;
1280 }
1281 periodic_schedules++;
1282
1283 debug("Exit create_int_queue\n");
1284 return result;
1285fail3:
1286 if (result->tds)
1287 free(result->tds);
1288fail2:
1289 if (result->first)
1290 free(result->first);
1291 if (result)
1292 free(result);
1293fail1:
1294 return NULL;
1295}
1296
1297void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1298{
1299 struct QH *cur = queue->current;
1300
1301 /* depleted queue */
1302 if (cur == NULL) {
1303 debug("Exit poll_int_queue with completed queue\n");
1304 return NULL;
1305 }
1306 /* still active */
Stephen Warrend3e07472013-05-24 15:03:17 -06001307 invalidate_dcache_range((uint32_t)cur,
1308 ALIGN_END_ADDR(struct QH, cur, 1));
Adrian Coxea427772014-04-10 13:29:45 +01001309 if (cur->qh_overlay.qt_token & cpu_to_hc32(0x80)) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001310 debug("Exit poll_int_queue with no completed intr transfer. "
1311 "token is %x\n", cur->qh_overlay.qt_token);
1312 return NULL;
1313 }
1314 if (!(cur->qh_link & QH_LINK_TERMINATE))
1315 queue->current++;
1316 else
1317 queue->current = NULL;
1318 debug("Exit poll_int_queue with completed intr transfer. "
1319 "token is %x at %p (first at %p)\n", cur->qh_overlay.qt_token,
1320 &cur->qh_overlay.qt_token, queue->first);
1321 return cur->buffer;
1322}
1323
1324/* Do not free buffers associated with QHs, they're owned by someone else */
1325int
1326destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1327{
1328 struct ehci_ctrl *ctrl = dev->controller;
1329 int result = -1;
1330 unsigned long timeout;
1331
1332 if (disable_periodic(ctrl) < 0) {
1333 debug("FATAL: periodic should never fail, but did");
1334 goto out;
1335 }
1336 periodic_schedules--;
1337
1338 struct QH *cur = &ctrl->periodic_queue;
1339 timeout = get_timer(0) + 500; /* abort after 500ms */
Adrian Coxea427772014-04-10 13:29:45 +01001340 while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001341 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1342 if (NEXT_QH(cur) == queue->first) {
1343 debug("found candidate. removing from chain\n");
1344 cur->qh_link = queue->last->qh_link;
1345 result = 0;
1346 break;
1347 }
1348 cur = NEXT_QH(cur);
1349 if (get_timer(0) > timeout) {
1350 printf("Timeout destroying interrupt endpoint queue\n");
1351 result = -1;
1352 goto out;
1353 }
1354 }
1355
1356 if (periodic_schedules > 0) {
1357 result = enable_periodic(ctrl);
1358 if (result < 0)
1359 debug("FATAL: periodic should never fail, but did");
1360 }
1361
1362out:
1363 free(queue->tds);
1364 free(queue->first);
1365 free(queue);
1366
1367 return result;
1368}
1369
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001370int
1371submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1372 int length, int interval)
1373{
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001374 void *backbuffer;
1375 struct int_queue *queue;
1376 unsigned long timeout;
1377 int result = 0, ret;
1378
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001379 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1380 dev, pipe, buffer, length, interval);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001381
1382 /*
1383 * Interrupt transfers requiring several transactions are not supported
1384 * because bInterval is ignored.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +02001385 *
1386 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
Benoît Thébaudeaudb191342012-08-10 18:27:23 +02001387 * <= PKT_ALIGN if several qTDs are required, while the USB
1388 * specification does not constrain this for interrupt transfers. That
1389 * means that ehci_submit_async() would support interrupt transfers
1390 * requiring several transactions only as long as the transfer size does
1391 * not require more than a single qTD.
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001392 */
1393 if (length > usb_maxpacket(dev, pipe)) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001394 printf("%s: Interrupt transfers requiring several "
1395 "transactions are not supported.\n", __func__);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001396 return -1;
1397 }
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001398
1399 queue = create_int_queue(dev, pipe, 1, length, buffer);
1400
1401 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1402 while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
1403 if (get_timer(0) > timeout) {
1404 printf("Timeout poll on interrupt endpoint\n");
1405 result = -ETIMEDOUT;
1406 break;
1407 }
1408
1409 if (backbuffer != buffer) {
1410 debug("got wrong buffer back (%x instead of %x)\n",
1411 (uint32_t)backbuffer, (uint32_t)buffer);
1412 return -EINVAL;
1413 }
1414
Stephen Warrend3e07472013-05-24 15:03:17 -06001415 invalidate_dcache_range((uint32_t)buffer,
1416 ALIGN_END_ADDR(char, buffer, length));
1417
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001418 ret = destroy_int_queue(dev, queue);
1419 if (ret < 0)
1420 return ret;
1421
1422 /* everything worked out fine */
1423 return result;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001424}