blob: 5c71882e3a40b47c3989c7d06743f91b1ece2e53 [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) \
Rob Herring98ae8402015-03-17 15:46:37 -050048 ((unsigned long)(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
Simon Glass73382872015-03-25 12:22:18 -0600122__weak int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
Jim Linb068deb2013-03-27 00:52:32 +0000123{
124 return PORTSC_PSPD(reg);
125}
126
Jeroen Hofstee3dd80aa2014-10-08 22:57:29 +0200127__weak void ehci_set_usbmode(int index)
Jim Linb068deb2013-03-27 00:52:32 +0000128{
129 uint32_t tmp;
130 uint32_t *reg_ptr;
131
132 reg_ptr = (uint32_t *)((u8 *)&ehcic[index].hcor->or_usbcmd + USBMODE);
133 tmp = ehci_readl(reg_ptr);
134 tmp |= USBMODE_CM_HC;
135#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
136 tmp |= USBMODE_BE;
137#endif
138 ehci_writel(reg_ptr, tmp);
139}
140
Jeroen Hofstee3dd80aa2014-10-08 22:57:29 +0200141__weak void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
Marek Vasut3874b6d2011-07-11 02:37:01 +0200142{
143 mdelay(50);
144}
145
Simon Glassaac064f2015-03-25 12:22:17 -0600146__weak uint32_t *ehci_get_portsc_register(struct ehci_hcor *hcor, int port)
147{
148 if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
149 /* Printing the message would cause a scan failure! */
150 debug("The request port(%u) is not configured\n", port);
151 return NULL;
152 }
153
154 return (uint32_t *)&hcor->or_portsc[port];
155}
156
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100157static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michaeldb632992008-12-10 17:55:19 +0100158{
michael51ab1422008-12-11 13:43:55 +0100159 uint32_t result;
160 do {
161 result = ehci_readl(ptr);
Wolfgang Denk09c83a42010-10-22 14:23:00 +0200162 udelay(5);
michael51ab1422008-12-11 13:43:55 +0100163 if (result == ~(uint32_t)0)
164 return -1;
165 result &= mask;
166 if (result == done)
167 return 0;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100168 usec--;
169 } while (usec > 0);
michael51ab1422008-12-11 13:43:55 +0100170 return -1;
171}
172
Lucas Stach676ae062012-09-26 00:14:35 +0200173static int ehci_reset(int index)
michael51ab1422008-12-11 13:43:55 +0100174{
175 uint32_t cmd;
michael51ab1422008-12-11 13:43:55 +0100176 int ret = 0;
177
Lucas Stach676ae062012-09-26 00:14:35 +0200178 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Stefan Roese273d7202010-11-26 15:44:00 +0100179 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Lucas Stach676ae062012-09-26 00:14:35 +0200180 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
181 ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
182 CMD_RESET, 0, 250 * 1000);
michael51ab1422008-12-11 13:43:55 +0100183 if (ret < 0) {
184 printf("EHCI fail to reset\n");
185 goto out;
186 }
187
Jim Linb068deb2013-03-27 00:52:32 +0000188 if (ehci_is_TDI())
189 ehci_set_usbmode(index);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000190
191#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Lucas Stach676ae062012-09-26 00:14:35 +0200192 cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200193 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass9ab4ce22012-02-27 10:52:47 +0000194 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Lucas Stach676ae062012-09-26 00:14:35 +0200195 ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000196#endif
michael51ab1422008-12-11 13:43:55 +0100197out:
198 return ret;
michaeldb632992008-12-10 17:55:19 +0100199}
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100200
Julius Werner5077f962013-09-24 10:53:07 -0700201static int ehci_shutdown(struct ehci_ctrl *ctrl)
202{
203 int i, ret = 0;
204 uint32_t cmd, reg;
205
Marek Vasut1e1be6d2013-12-14 02:03:11 +0100206 if (!ctrl || !ctrl->hcor)
207 return -EINVAL;
208
Julius Werner5077f962013-09-24 10:53:07 -0700209 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
210 cmd &= ~(CMD_PSE | CMD_ASE);
211 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
212 ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
213 100 * 1000);
214
215 if (!ret) {
216 for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
217 reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
218 reg |= EHCI_PS_SUSP;
219 ehci_writel(&ctrl->hcor->or_portsc[i], reg);
220 }
221
222 cmd &= ~CMD_RUN;
223 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
224 ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
225 HCHALT_TIMEOUT);
226 }
227
228 if (ret)
229 puts("EHCI failed to shut down host controller.\n");
230
231 return ret;
232}
233
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100234static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
235{
Marek Vasutb8adb122012-04-09 04:07:46 +0200236 uint32_t delta, next;
Rob Herring98ae8402015-03-17 15:46:37 -0500237 uint32_t addr = (unsigned long)buf;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100238 int idx;
239
Ilya Yanok189a6952012-07-15 04:43:49 +0000240 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutb8adb122012-04-09 04:07:46 +0200241 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
242
Ilya Yanok189a6952012-07-15 04:43:49 +0000243 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
244
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100245 idx = 0;
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200246 while (idx < QT_BUFFER_CNT) {
michaeldb632992008-12-10 17:55:19 +0100247 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denk3ed16072010-10-19 16:13:15 +0200248 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200249 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100250 delta = next - addr;
251 if (delta >= sz)
252 break;
253 sz -= delta;
254 addr = next;
255 idx++;
256 }
257
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200258 if (idx == QT_BUFFER_CNT) {
Rob Herring98ae8402015-03-17 15:46:37 -0500259 printf("out of buffer pointers (%zu bytes left)\n", sz);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100260 return -1;
261 }
262
263 return 0;
264}
265
Ilya Yanokc60795f2012-11-06 13:48:20 +0000266static inline u8 ehci_encode_speed(enum usb_device_speed speed)
267{
268 #define QH_HIGH_SPEED 2
269 #define QH_FULL_SPEED 0
270 #define QH_LOW_SPEED 1
271 if (speed == USB_SPEED_HIGH)
272 return QH_HIGH_SPEED;
273 if (speed == USB_SPEED_LOW)
274 return QH_LOW_SPEED;
275 return QH_FULL_SPEED;
276}
277
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200278static void ehci_update_endpt2_dev_n_port(struct usb_device *dev,
279 struct QH *qh)
280{
281 struct usb_device *ttdev;
282
283 if (dev->speed != USB_SPEED_LOW && dev->speed != USB_SPEED_FULL)
284 return;
285
286 /*
287 * For full / low speed devices we need to get the devnum and portnr of
288 * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
289 * in the tree before that one!
290 */
291 ttdev = dev;
292 while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
293 ttdev = ttdev->parent;
294 if (!ttdev->parent)
295 return;
296
297 qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
298 QH_ENDPT2_HUBADDR(ttdev->parent->devnum));
299}
300
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100301static int
302ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
303 int length, struct devrequest *req)
304{
Tom Rini71c5de42012-07-15 22:14:24 +0000305 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200306 struct qTD *qtd;
307 int qtd_count = 0;
Marek Vasutde98e8b2012-04-08 23:32:05 +0200308 int qtd_counter = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100309 volatile struct qTD *vtd;
310 unsigned long ts;
311 uint32_t *tdp;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200312 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100313 uint32_t c, toggle;
michaeldb632992008-12-10 17:55:19 +0100314 uint32_t cmd;
Simon Glass96820a32011-02-07 14:42:16 -0800315 int timeout;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100316 int ret = 0;
Lucas Stach676ae062012-09-26 00:14:35 +0200317 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100318
michaeldb632992008-12-10 17:55:19 +0100319 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100320 buffer, length, req);
321 if (req != NULL)
michaeldb632992008-12-10 17:55:19 +0100322 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100323 req->request, req->request,
324 req->requesttype, req->requesttype,
325 le16_to_cpu(req->value), le16_to_cpu(req->value),
michaeldb632992008-12-10 17:55:19 +0100326 le16_to_cpu(req->index));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100327
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200328#define PKT_ALIGN 512
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200329 /*
330 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
331 * described by a transfer descriptor (the qTD). The qTDs form a linked
332 * list with a queue head (QH).
333 *
334 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
335 * have its beginning in a qTD transfer and its end in the following
336 * one, so the qTD transfer lengths have to be chosen accordingly.
337 *
338 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
339 * single pages. The first data buffer can start at any offset within a
340 * page (not considering the cache-line alignment issues), while the
341 * following buffers must be page-aligned. There is no alignment
342 * constraint on the size of a qTD transfer.
343 */
344 if (req != NULL)
345 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
346 qtd_count += 1 + 1;
347 if (length > 0 || req == NULL) {
348 /*
349 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200350 * data payload (not considering the first qTD transfer, which
351 * may be longer or shorter, and the final one, which may be
352 * shorter).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200353 *
354 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200355 * transfer size is aligned to PKT_ALIGN, which is a multiple of
356 * wMaxPacketSize (except in some cases for interrupt transfers,
357 * see comment in submit_int_msg()).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200358 *
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200359 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200360 * QT_BUFFER_CNT full pages will be used.
361 */
362 int xfr_sz = QT_BUFFER_CNT;
363 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200364 * However, if the input buffer is not aligned to PKT_ALIGN, the
365 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200366 * data buffer of each transfer will be page-unaligned.
367 */
Rob Herring98ae8402015-03-17 15:46:37 -0500368 if ((unsigned long)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200369 xfr_sz--;
370 /* Convert the qTD transfer size to bytes. */
371 xfr_sz *= EHCI_PAGE_SIZE;
372 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200373 * Approximate by excess the number of qTDs that will be
374 * required for the data payload. The exact formula is way more
375 * complicated and saves at most 2 qTDs, i.e. a total of 128
376 * bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200377 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200378 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200379 }
380/*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200381 * Threshold value based on the worst-case total size of the allocated qTDs for
382 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200383 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200384#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200385#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
386#endif
387 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
388 if (qtd == NULL) {
389 printf("unable to allocate TDs\n");
390 return -1;
391 }
392
Tom Rini71c5de42012-07-15 22:14:24 +0000393 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200394 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200395
Marek Vasutb8adb122012-04-09 04:07:46 +0200396 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
397
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200398 /*
399 * Setup QH (3.6 in ehci-r10.pdf)
400 *
401 * qh_link ................. 03-00 H
402 * qh_endpt1 ............... 07-04 H
403 * qh_endpt2 ............... 0B-08 H
404 * - qh_curtd
405 * qh_overlay.qt_next ...... 13-10 H
406 * - qh_overlay.qt_altnext
407 */
Rob Herring98ae8402015-03-17 15:46:37 -0500408 qh->qh_link = cpu_to_hc32((unsigned long)&ctrl->qh_list | QH_LINK_TYPE_QH);
Ilya Yanokc60795f2012-11-06 13:48:20 +0000409 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200410 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200411 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200412 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200413 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
Ilya Yanokc60795f2012-11-06 13:48:20 +0000414 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200415 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
416 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini71c5de42012-07-15 22:14:24 +0000417 qh->qh_endpt1 = cpu_to_hc32(endpt);
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200418 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini71c5de42012-07-15 22:14:24 +0000419 qh->qh_endpt2 = cpu_to_hc32(endpt);
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200420 ehci_update_endpt2_dev_n_port(dev, qh);
Tom Rini71c5de42012-07-15 22:14:24 +0000421 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Stephen Warren2456b972014-02-07 09:53:50 -0700422 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100423
Tom Rini71c5de42012-07-15 22:14:24 +0000424 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100425
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100426 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200427 /*
428 * Setup request qTD (3.5 in ehci-r10.pdf)
429 *
430 * qt_next ................ 03-00 H
431 * qt_altnext ............. 07-04 H
432 * qt_token ............... 0B-08 H
433 *
434 * [ buffer, buffer_hi ] loaded with "req".
435 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200436 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
437 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200438 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
439 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
440 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
441 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200442 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200443 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
444 printf("unable to construct SETUP TD\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100445 goto fail;
446 }
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200447 /* Update previous qTD! */
Rob Herring98ae8402015-03-17 15:46:37 -0500448 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200449 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100450 toggle = 1;
451 }
452
453 if (length > 0 || req == NULL) {
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200454 uint8_t *buf_ptr = buffer;
455 int left_length = length;
456
457 do {
458 /*
459 * Determine the size of this qTD transfer. By default,
460 * QT_BUFFER_CNT full pages can be used.
461 */
462 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
463 /*
464 * However, if the input buffer is not page-aligned, the
465 * portion of the first page before the buffer start
466 * offset within that page is unusable.
467 */
Rob Herring98ae8402015-03-17 15:46:37 -0500468 xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200469 /*
470 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200471 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200472 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200473 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200474 /*
475 * This transfer may be shorter than the available qTD
476 * transfer size that has just been computed.
477 */
478 xfr_bytes = min(xfr_bytes, left_length);
479
480 /*
481 * Setup request qTD (3.5 in ehci-r10.pdf)
482 *
483 * qt_next ................ 03-00 H
484 * qt_altnext ............. 07-04 H
485 * qt_token ............... 0B-08 H
486 *
487 * [ buffer, buffer_hi ] loaded with "buffer".
488 */
489 qtd[qtd_counter].qt_next =
490 cpu_to_hc32(QT_NEXT_TERMINATE);
491 qtd[qtd_counter].qt_altnext =
492 cpu_to_hc32(QT_NEXT_TERMINATE);
493 token = QT_TOKEN_DT(toggle) |
494 QT_TOKEN_TOTALBYTES(xfr_bytes) |
495 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
496 QT_TOKEN_CERR(3) |
497 QT_TOKEN_PID(usb_pipein(pipe) ?
498 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
499 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
500 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
501 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
502 xfr_bytes)) {
503 printf("unable to construct DATA TD\n");
504 goto fail;
505 }
506 /* Update previous qTD! */
Rob Herring98ae8402015-03-17 15:46:37 -0500507 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200508 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200509 /*
510 * Data toggle has to be adjusted since the qTD transfer
511 * size is not always an even multiple of
512 * wMaxPacketSize.
513 */
514 if ((xfr_bytes / maxpacket) & 1)
515 toggle ^= 1;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200516 buf_ptr += xfr_bytes;
517 left_length -= xfr_bytes;
518 } while (left_length > 0);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100519 }
520
521 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200522 /*
523 * Setup request qTD (3.5 in ehci-r10.pdf)
524 *
525 * qt_next ................ 03-00 H
526 * qt_altnext ............. 07-04 H
527 * qt_token ............... 0B-08 H
528 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200529 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
530 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200531 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200532 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
533 QT_TOKEN_PID(usb_pipein(pipe) ?
534 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
535 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200536 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200537 /* Update previous qTD! */
Rob Herring98ae8402015-03-17 15:46:37 -0500538 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200539 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100540 }
541
Rob Herring98ae8402015-03-17 15:46:37 -0500542 ctrl->qh_list.qh_link = cpu_to_hc32((unsigned long)qh | QH_LINK_TYPE_QH);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100543
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100544 /* Flush dcache */
Rob Herring98ae8402015-03-17 15:46:37 -0500545 flush_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach676ae062012-09-26 00:14:35 +0200546 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500547 flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
548 flush_dcache_range((unsigned long)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200549 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100550
Ilya Yanokc7701af2012-07-15 22:12:08 +0000551 /* Set async. queue head pointer. */
Rob Herring98ae8402015-03-17 15:46:37 -0500552 ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)&ctrl->qh_list);
Ilya Yanokc7701af2012-07-15 22:12:08 +0000553
Lucas Stach676ae062012-09-26 00:14:35 +0200554 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
555 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100556
557 /* Enable async. schedule. */
Lucas Stach676ae062012-09-26 00:14:35 +0200558 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael51ab1422008-12-11 13:43:55 +0100559 cmd |= CMD_ASE;
Lucas Stach676ae062012-09-26 00:14:35 +0200560 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michaeldb632992008-12-10 17:55:19 +0100561
Lucas Stach676ae062012-09-26 00:14:35 +0200562 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100563 100 * 1000);
564 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200565 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100566 goto fail;
michael51ab1422008-12-11 13:43:55 +0100567 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100568
569 /* Wait for TDs to be processed. */
570 ts = get_timer(0);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200571 vtd = &qtd[qtd_counter - 1];
Simon Glass96820a32011-02-07 14:42:16 -0800572 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100573 do {
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100574 /* Invalidate dcache */
Rob Herring98ae8402015-03-17 15:46:37 -0500575 invalidate_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach676ae062012-09-26 00:14:35 +0200576 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500577 invalidate_dcache_range((unsigned long)qh,
Tom Rini71c5de42012-07-15 22:14:24 +0000578 ALIGN_END_ADDR(struct QH, qh, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500579 invalidate_dcache_range((unsigned long)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200580 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutb8adb122012-04-09 04:07:46 +0200581
michaeldb632992008-12-10 17:55:19 +0100582 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200583 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100584 break;
Stefan Roese67333f72010-11-26 15:43:28 +0100585 WATCHDOG_RESET();
Simon Glass96820a32011-02-07 14:42:16 -0800586 } while (get_timer(ts) < timeout);
587
Ilya Yanok189a6952012-07-15 04:43:49 +0000588 /*
589 * Invalidate the memory area occupied by buffer
590 * Don't try to fix the buffer alignment, if it isn't properly
591 * aligned it's upper layer's fault so let invalidate_dcache_range()
592 * vow about it. But we have to fix the length as it's actual
593 * transfer length and can be unaligned. This is potentially
594 * dangerous operation, it's responsibility of the calling
595 * code to make sure enough space is reserved.
596 */
Rob Herring98ae8402015-03-17 15:46:37 -0500597 invalidate_dcache_range((unsigned long)buffer,
598 ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutb8adb122012-04-09 04:07:46 +0200599
Simon Glass96820a32011-02-07 14:42:16 -0800600 /* Check that the TD processing happened */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200601 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glass96820a32011-02-07 14:42:16 -0800602 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100603
604 /* Disable async schedule. */
Lucas Stach676ae062012-09-26 00:14:35 +0200605 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michaeldb632992008-12-10 17:55:19 +0100606 cmd &= ~CMD_ASE;
Lucas Stach676ae062012-09-26 00:14:35 +0200607 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +0100608
Lucas Stach676ae062012-09-26 00:14:35 +0200609 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100610 100 * 1000);
611 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200612 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100613 goto fail;
michael51ab1422008-12-11 13:43:55 +0100614 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100615
Tom Rini71c5de42012-07-15 22:14:24 +0000616 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200617 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michaeldb632992008-12-10 17:55:19 +0100618 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200619 switch (QT_TOKEN_GET_STATUS(token) &
620 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100621 case 0:
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200622 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100623 usb_settoggle(dev, usb_pipeendpoint(pipe),
624 usb_pipeout(pipe), toggle);
625 dev->status = 0;
626 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200627 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100628 dev->status = USB_ST_STALLED;
629 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200630 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
631 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100632 dev->status = USB_ST_BUF_ERR;
633 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200634 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
635 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100636 dev->status = USB_ST_BABBLE_DET;
637 break;
638 default:
639 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200640 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschin222d6df2010-11-02 11:47:29 +0100641 dev->status |= USB_ST_STALLED;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100642 break;
643 }
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200644 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100645 } else {
646 dev->act_len = 0;
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800647#ifndef CONFIG_USB_EHCI_FARADAY
michaeldb632992008-12-10 17:55:19 +0100648 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach676ae062012-09-26 00:14:35 +0200649 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
650 ehci_readl(&ctrl->hcor->or_portsc[0]),
651 ehci_readl(&ctrl->hcor->or_portsc[1]));
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800652#endif
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100653 }
654
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200655 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100656 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
657
658fail:
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200659 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100660 return -1;
661}
662
michaeldb632992008-12-10 17:55:19 +0100663int
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100664ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
665 int length, struct devrequest *req)
666{
667 uint8_t tmpbuf[4];
668 u16 typeReq;
michaeldb632992008-12-10 17:55:19 +0100669 void *srcptr = NULL;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100670 int len, srclen;
671 uint32_t reg;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100672 uint32_t *status_reg;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000673 int port = le16_to_cpu(req->index) & 0xff;
Lucas Stach676ae062012-09-26 00:14:35 +0200674 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100675
676 srclen = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100677
michaeldb632992008-12-10 17:55:19 +0100678 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100679 req->request, req->request,
680 req->requesttype, req->requesttype,
681 le16_to_cpu(req->value), le16_to_cpu(req->index));
682
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530683 typeReq = req->request | req->requesttype << 8;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100684
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530685 switch (typeReq) {
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800686 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
687 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
688 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Kuo-Jung Su1dde1422013-05-15 15:29:21 +0800689 status_reg = ehci_get_portsc_register(ctrl->hcor, port - 1);
690 if (!status_reg)
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800691 return -1;
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800692 break;
693 default:
694 status_reg = NULL;
695 break;
696 }
697
698 switch (typeReq) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100699 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
700 switch (le16_to_cpu(req->value) >> 8) {
701 case USB_DT_DEVICE:
michaeldb632992008-12-10 17:55:19 +0100702 debug("USB_DT_DEVICE request\n");
703 srcptr = &descriptor.device;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200704 srclen = descriptor.device.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100705 break;
706 case USB_DT_CONFIG:
michaeldb632992008-12-10 17:55:19 +0100707 debug("USB_DT_CONFIG config\n");
708 srcptr = &descriptor.config;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200709 srclen = descriptor.config.bLength +
710 descriptor.interface.bLength +
711 descriptor.endpoint.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100712 break;
713 case USB_DT_STRING:
michaeldb632992008-12-10 17:55:19 +0100714 debug("USB_DT_STRING config\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100715 switch (le16_to_cpu(req->value) & 0xff) {
716 case 0: /* Language */
717 srcptr = "\4\3\1\0";
718 srclen = 4;
719 break;
720 case 1: /* Vendor */
721 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
722 srclen = 14;
723 break;
724 case 2: /* Product */
725 srcptr = "\52\3E\0H\0C\0I\0 "
726 "\0H\0o\0s\0t\0 "
727 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
728 srclen = 42;
729 break;
730 default:
michaeldb632992008-12-10 17:55:19 +0100731 debug("unknown value DT_STRING %x\n",
732 le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100733 goto unknown;
734 }
735 break;
736 default:
michaeldb632992008-12-10 17:55:19 +0100737 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100738 goto unknown;
739 }
740 break;
741 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
742 switch (le16_to_cpu(req->value) >> 8) {
743 case USB_DT_HUB:
michaeldb632992008-12-10 17:55:19 +0100744 debug("USB_DT_HUB config\n");
745 srcptr = &descriptor.hub;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200746 srclen = descriptor.hub.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100747 break;
748 default:
michaeldb632992008-12-10 17:55:19 +0100749 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100750 goto unknown;
751 }
752 break;
753 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michaeldb632992008-12-10 17:55:19 +0100754 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach676ae062012-09-26 00:14:35 +0200755 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100756 break;
757 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michaeldb632992008-12-10 17:55:19 +0100758 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100759 /* Nothing to do */
760 break;
761 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
762 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
763 tmpbuf[1] = 0;
764 srcptr = tmpbuf;
765 srclen = 2;
766 break;
michaeldb632992008-12-10 17:55:19 +0100767 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100768 memset(tmpbuf, 0, 4);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100769 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100770 if (reg & EHCI_PS_CS)
771 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
772 if (reg & EHCI_PS_PE)
773 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
774 if (reg & EHCI_PS_SUSP)
775 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
776 if (reg & EHCI_PS_OCA)
777 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300778 if (reg & EHCI_PS_PR)
779 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100780 if (reg & EHCI_PS_PP)
781 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese597eb282009-01-21 17:12:01 +0100782
783 if (ehci_is_TDI()) {
Simon Glass73382872015-03-25 12:22:18 -0600784 switch (ehci_get_port_speed(ctrl, reg)) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200785 case PORTSC_PSPD_FS:
Stefan Roese597eb282009-01-21 17:12:01 +0100786 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200787 case PORTSC_PSPD_LS:
Stefan Roese597eb282009-01-21 17:12:01 +0100788 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
789 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200790 case PORTSC_PSPD_HS:
Stefan Roese597eb282009-01-21 17:12:01 +0100791 default:
792 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
793 break;
794 }
795 } else {
796 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
797 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100798
799 if (reg & EHCI_PS_CSC)
800 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
801 if (reg & EHCI_PS_PEC)
802 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
803 if (reg & EHCI_PS_OCC)
804 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000805 if (ctrl->portreset & (1 << port))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100806 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100807
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100808 srcptr = tmpbuf;
809 srclen = 4;
810 break;
michaeldb632992008-12-10 17:55:19 +0100811 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100812 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100813 reg &= ~EHCI_PS_CLEAR;
814 switch (le16_to_cpu(req->value)) {
michael51ab1422008-12-11 13:43:55 +0100815 case USB_PORT_FEAT_ENABLE:
816 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100817 ehci_writel(status_reg, reg);
michael51ab1422008-12-11 13:43:55 +0100818 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100819 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200820 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100821 reg |= EHCI_PS_PP;
822 ehci_writel(status_reg, reg);
823 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100824 break;
825 case USB_PORT_FEAT_RESET:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100826 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
827 !ehci_is_TDI() &&
828 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100829 /* Low speed device, give up ownership. */
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100830 debug("port %d low speed --> companion\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000831 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100832 reg |= EHCI_PS_PO;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100833 ehci_writel(status_reg, reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100834 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100835 } else {
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300836 int ret;
837
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100838 reg |= EHCI_PS_PR;
839 reg &= ~EHCI_PS_PE;
840 ehci_writel(status_reg, reg);
841 /*
842 * caller must wait, then call GetPortStatus
843 * usb 2.0 specification say 50 ms resets on
844 * root
845 */
Marek Vasut3874b6d2011-07-11 02:37:01 +0200846 ehci_powerup_fixup(status_reg, &reg);
847
Chris Zhangb4161912010-01-06 13:34:04 -0800848 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300849 /*
850 * A host controller must terminate the reset
851 * and stabilize the state of the port within
852 * 2 milliseconds
853 */
854 ret = handshake(status_reg, EHCI_PS_PR, 0,
855 2 * 1000);
856 if (!ret)
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000857 ctrl->portreset |= 1 << port;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300858 else
859 printf("port(%d) reset error\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000860 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100861 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100862 break;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000863 case USB_PORT_FEAT_TEST:
Julius Werner5077f962013-09-24 10:53:07 -0700864 ehci_shutdown(ctrl);
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000865 reg &= ~(0xf << 16);
866 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
867 ehci_writel(status_reg, reg);
868 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100869 default:
michaeldb632992008-12-10 17:55:19 +0100870 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100871 goto unknown;
872 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100873 /* unblock posted writes */
Lucas Stach676ae062012-09-26 00:14:35 +0200874 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100875 break;
michaeldb632992008-12-10 17:55:19 +0100876 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100877 reg = ehci_readl(status_reg);
Simon Glassed10e662013-05-10 19:49:00 -0700878 reg &= ~EHCI_PS_CLEAR;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100879 switch (le16_to_cpu(req->value)) {
880 case USB_PORT_FEAT_ENABLE:
881 reg &= ~EHCI_PS_PE;
882 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100883 case USB_PORT_FEAT_C_ENABLE:
Simon Glassed10e662013-05-10 19:49:00 -0700884 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100885 break;
886 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200887 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Simon Glassed10e662013-05-10 19:49:00 -0700888 reg &= ~EHCI_PS_PP;
889 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100890 case USB_PORT_FEAT_C_CONNECTION:
Simon Glassed10e662013-05-10 19:49:00 -0700891 reg |= EHCI_PS_CSC;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100892 break;
michael51ab1422008-12-11 13:43:55 +0100893 case USB_PORT_FEAT_OVER_CURRENT:
Simon Glassed10e662013-05-10 19:49:00 -0700894 reg |= EHCI_PS_OCC;
michael51ab1422008-12-11 13:43:55 +0100895 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100896 case USB_PORT_FEAT_C_RESET:
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000897 ctrl->portreset &= ~(1 << port);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100898 break;
899 default:
michaeldb632992008-12-10 17:55:19 +0100900 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100901 goto unknown;
902 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100903 ehci_writel(status_reg, reg);
904 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +0200905 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100906 break;
907 default:
michaeldb632992008-12-10 17:55:19 +0100908 debug("Unknown request\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100909 goto unknown;
910 }
911
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000912 mdelay(1);
Masahiro Yamadab4141192014-11-07 03:03:31 +0900913 len = min3(srclen, (int)le16_to_cpu(req->length), length);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100914 if (srcptr != NULL && len > 0)
915 memcpy(buffer, srcptr, len);
michaeldb632992008-12-10 17:55:19 +0100916 else
917 debug("Len is 0\n");
918
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100919 dev->act_len = len;
920 dev->status = 0;
921 return 0;
922
923unknown:
michaeldb632992008-12-10 17:55:19 +0100924 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100925 req->requesttype, req->request, le16_to_cpu(req->value),
926 le16_to_cpu(req->index), le16_to_cpu(req->length));
927
928 dev->act_len = 0;
929 dev->status = USB_ST_STALLED;
930 return -1;
931}
932
Simon Glassc4a31412015-03-25 12:22:19 -0600933void ehci_set_controller_priv(int index, void *priv)
934{
935 ehcic[index].priv = priv;
936}
937
938void *ehci_get_controller_priv(int index)
939{
940 return ehcic[index].priv;
941}
942
Lucas Stachc7e3b2b2012-09-26 00:14:34 +0200943int usb_lowlevel_stop(int index)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100944{
Julius Werner5077f962013-09-24 10:53:07 -0700945 ehci_shutdown(&ehcic[index]);
Lucas Stach676ae062012-09-26 00:14:35 +0200946 return ehci_hcd_stop(index);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100947}
948
Troy Kisky06d513e2013-10-10 15:27:56 -0700949int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100950{
951 uint32_t reg;
michaeldb632992008-12-10 17:55:19 +0100952 uint32_t cmd;
Lucas Stach676ae062012-09-26 00:14:35 +0200953 struct QH *qh_list;
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000954 struct QH *periodic;
955 int i;
Troy Kisky127efc42013-10-10 15:27:57 -0700956 int rc;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100957
Troy Kisky127efc42013-10-10 15:27:57 -0700958 rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
959 if (rc)
960 return rc;
961 if (init == USB_INIT_DEVICE)
962 goto done;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100963
michael51ab1422008-12-11 13:43:55 +0100964 /* EHCI spec section 4.1 */
Lucas Stach676ae062012-09-26 00:14:35 +0200965 if (ehci_reset(index))
michael51ab1422008-12-11 13:43:55 +0100966 return -1;
967
Stefan Roese832e6142009-01-21 17:12:10 +0100968#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
Troy Kisky127efc42013-10-10 15:27:57 -0700969 rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
970 if (rc)
971 return rc;
Stefan Roese832e6142009-01-21 17:12:10 +0100972#endif
Vincent Palatin29828372012-12-12 17:55:22 -0800973 /* Set the high address word (aka segment) for 64-bit controller */
974 if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
Marek Vasuteb632182013-12-14 02:04:52 +0100975 ehci_writel(&ehcic[index].hcor->or_ctrldssegment, 0);
Stefan Roese832e6142009-01-21 17:12:10 +0100976
Lucas Stach676ae062012-09-26 00:14:35 +0200977 qh_list = &ehcic[index].qh_list;
978
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100979 /* Set head of reclaim list */
Tom Rini71c5de42012-07-15 22:14:24 +0000980 memset(qh_list, 0, sizeof(*qh_list));
Rob Herring98ae8402015-03-17 15:46:37 -0500981 qh_list->qh_link = cpu_to_hc32((unsigned long)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200982 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
983 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini71c5de42012-07-15 22:14:24 +0000984 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
Rob Herring98ae8402015-03-17 15:46:37 -0500989 flush_dcache_range((unsigned long)qh_list,
Stephen Warrend3e07472013-05-24 15:03:17 -0600990 ALIGN_END_ADDR(struct QH, qh_list, 1));
991
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000992 /* Set async. queue head pointer. */
Rob Herring98ae8402015-03-17 15:46:37 -0500993 ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (unsigned long)qh_list);
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000994
995 /*
996 * Set up periodic list
997 * Step 1: Parent QH for all periodic transfers.
998 */
Hans de Goede36b73102014-09-20 16:51:25 +0200999 ehcic[index].periodic_schedules = 0;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001000 periodic = &ehcic[index].periodic_queue;
1001 memset(periodic, 0, sizeof(*periodic));
1002 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
1003 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1004 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1005
Rob Herring98ae8402015-03-17 15:46:37 -05001006 flush_dcache_range((unsigned long)periodic,
Stephen Warrend3e07472013-05-24 15:03:17 -06001007 ALIGN_END_ADDR(struct QH, periodic, 1));
1008
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001009 /*
1010 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
1011 * In particular, device specifications on polling frequency
1012 * are disregarded. Keyboards seem to send NAK/NYet reliably
1013 * when polled with an empty buffer.
1014 *
1015 * Split Transactions will be spread across microframes using
1016 * S-mask and C-mask.
1017 */
Nikita Kiryanov8bc36032013-07-29 13:27:40 +03001018 if (ehcic[index].periodic_list == NULL)
1019 ehcic[index].periodic_list = memalign(4096, 1024 * 4);
1020
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001021 if (!ehcic[index].periodic_list)
1022 return -ENOMEM;
1023 for (i = 0; i < 1024; i++) {
Rob Herring98ae8402015-03-17 15:46:37 -05001024 ehcic[index].periodic_list[i] = cpu_to_hc32((unsigned long)periodic
Adrian Coxea427772014-04-10 13:29:45 +01001025 | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001026 }
1027
Rob Herring98ae8402015-03-17 15:46:37 -05001028 flush_dcache_range((unsigned long)ehcic[index].periodic_list,
Stephen Warrend3e07472013-05-24 15:03:17 -06001029 ALIGN_END_ADDR(uint32_t, ehcic[index].periodic_list,
1030 1024));
1031
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001032 /* Set periodic list base address */
1033 ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
Rob Herring98ae8402015-03-17 15:46:37 -05001034 (unsigned long)ehcic[index].periodic_list);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001035
Lucas Stach676ae062012-09-26 00:14:35 +02001036 reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
michael51ab1422008-12-11 13:43:55 +01001037 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stach7a46b2c2012-09-28 00:26:19 +02001038 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001039 /* Port Indicators */
1040 if (HCS_INDICATOR(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +02001041 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1042 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001043 /* Port Power Control */
1044 if (HCS_PPC(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +02001045 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1046 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001047
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001048 /* Start the host controller. */
Lucas Stach676ae062012-09-26 00:14:35 +02001049 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Wolfgang Denkf15c6512009-02-12 00:08:39 +01001050 /*
1051 * Philips, Intel, and maybe others need CMD_RUN before the
1052 * root hub will detect new devices (why?); NEC doesn't
1053 */
michael51ab1422008-12-11 13:43:55 +01001054 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
1055 cmd |= CMD_RUN;
Lucas Stach676ae062012-09-26 00:14:35 +02001056 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +01001057
Kuo-Jung Sue82a3162013-05-15 15:29:23 +08001058#ifndef CONFIG_USB_EHCI_FARADAY
michael51ab1422008-12-11 13:43:55 +01001059 /* take control over the ports */
Lucas Stach676ae062012-09-26 00:14:35 +02001060 cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
michael51ab1422008-12-11 13:43:55 +01001061 cmd |= FLAG_CF;
Lucas Stach676ae062012-09-26 00:14:35 +02001062 ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
Kuo-Jung Sue82a3162013-05-15 15:29:23 +08001063#endif
1064
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001065 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +02001066 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001067 mdelay(5);
Lucas Stach676ae062012-09-26 00:14:35 +02001068 reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001069 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001070
Lucas Stach676ae062012-09-26 00:14:35 +02001071 ehcic[index].rootdev = 0;
Troy Kisky127efc42013-10-10 15:27:57 -07001072done:
Lucas Stach676ae062012-09-26 00:14:35 +02001073 *controller = &ehcic[index];
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001074 return 0;
1075}
1076
1077int
1078submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1079 int length)
1080{
1081
1082 if (usb_pipetype(pipe) != PIPE_BULK) {
1083 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1084 return -1;
1085 }
1086 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1087}
1088
1089int
1090submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1091 int length, struct devrequest *setup)
1092{
Lucas Stach676ae062012-09-26 00:14:35 +02001093 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001094
1095 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1096 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1097 return -1;
1098 }
1099
Lucas Stach676ae062012-09-26 00:14:35 +02001100 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1101 if (!ctrl->rootdev)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001102 dev->speed = USB_SPEED_HIGH;
1103 return ehci_submit_root(dev, pipe, buffer, length, setup);
1104 }
1105 return ehci_submit_async(dev, pipe, buffer, length, setup);
1106}
1107
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001108struct int_queue {
Hans de Goede8aa26b82014-09-24 14:06:05 +02001109 int elementsize;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001110 struct QH *first;
1111 struct QH *current;
1112 struct QH *last;
1113 struct qTD *tds;
1114};
1115
Rob Herring98ae8402015-03-17 15:46:37 -05001116#define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001117
1118static int
1119enable_periodic(struct ehci_ctrl *ctrl)
1120{
1121 uint32_t cmd;
1122 struct ehci_hcor *hcor = ctrl->hcor;
1123 int ret;
1124
1125 cmd = ehci_readl(&hcor->or_usbcmd);
1126 cmd |= CMD_PSE;
1127 ehci_writel(&hcor->or_usbcmd, cmd);
1128
1129 ret = handshake((uint32_t *)&hcor->or_usbsts,
1130 STS_PSS, STS_PSS, 100 * 1000);
1131 if (ret < 0) {
1132 printf("EHCI failed: timeout when enabling periodic list\n");
1133 return -ETIMEDOUT;
1134 }
1135 udelay(1000);
1136 return 0;
1137}
1138
1139static int
1140disable_periodic(struct ehci_ctrl *ctrl)
1141{
1142 uint32_t cmd;
1143 struct ehci_hcor *hcor = ctrl->hcor;
1144 int ret;
1145
1146 cmd = ehci_readl(&hcor->or_usbcmd);
1147 cmd &= ~CMD_PSE;
1148 ehci_writel(&hcor->or_usbcmd, cmd);
1149
1150 ret = handshake((uint32_t *)&hcor->or_usbsts,
1151 STS_PSS, 0, 100 * 1000);
1152 if (ret < 0) {
1153 printf("EHCI failed: timeout when disabling periodic list\n");
1154 return -ETIMEDOUT;
1155 }
1156 return 0;
1157}
1158
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001159struct int_queue *
1160create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
Hans de Goede8bb6c1d2015-01-11 20:38:28 +01001161 int elementsize, void *buffer, int interval)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001162{
1163 struct ehci_ctrl *ctrl = dev->controller;
1164 struct int_queue *result = NULL;
1165 int i;
1166
Hans de Goedebd818d82014-09-24 14:06:04 +02001167 /*
1168 * Interrupt transfers requiring several transactions are not supported
1169 * because bInterval is ignored.
1170 *
1171 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1172 * <= PKT_ALIGN if several qTDs are required, while the USB
1173 * specification does not constrain this for interrupt transfers. That
1174 * means that ehci_submit_async() would support interrupt transfers
1175 * requiring several transactions only as long as the transfer size does
1176 * not require more than a single qTD.
1177 */
1178 if (elementsize > usb_maxpacket(dev, pipe)) {
1179 printf("%s: xfers requiring several transactions are not supported.\n",
1180 __func__);
1181 return NULL;
1182 }
1183
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001184 debug("Enter create_int_queue\n");
1185 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1186 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1187 return NULL;
1188 }
1189
1190 /* limit to 4 full pages worth of data -
1191 * we can safely fit them in a single TD,
1192 * no matter the alignment
1193 */
1194 if (elementsize >= 16384) {
1195 debug("too large elements for interrupt transfers\n");
1196 return NULL;
1197 }
1198
1199 result = malloc(sizeof(*result));
1200 if (!result) {
1201 debug("ehci intr queue: out of memory\n");
1202 goto fail1;
1203 }
Hans de Goede8aa26b82014-09-24 14:06:05 +02001204 result->elementsize = elementsize;
Stephen Warren8165e342014-02-06 13:13:06 -07001205 result->first = memalign(USB_DMA_MINALIGN,
1206 sizeof(struct QH) * queuesize);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001207 if (!result->first) {
1208 debug("ehci intr queue: out of memory\n");
1209 goto fail2;
1210 }
1211 result->current = result->first;
1212 result->last = result->first + queuesize - 1;
Stephen Warren8165e342014-02-06 13:13:06 -07001213 result->tds = memalign(USB_DMA_MINALIGN,
1214 sizeof(struct qTD) * queuesize);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001215 if (!result->tds) {
1216 debug("ehci intr queue: out of memory\n");
1217 goto fail3;
1218 }
1219 memset(result->first, 0, sizeof(struct QH) * queuesize);
1220 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1221
1222 for (i = 0; i < queuesize; i++) {
1223 struct QH *qh = result->first + i;
1224 struct qTD *td = result->tds + i;
1225 void **buf = &qh->buffer;
1226
Rob Herring98ae8402015-03-17 15:46:37 -05001227 qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001228 if (i == queuesize - 1)
Adrian Coxea427772014-04-10 13:29:45 +01001229 qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001230
Rob Herring98ae8402015-03-17 15:46:37 -05001231 qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
Adrian Coxea427772014-04-10 13:29:45 +01001232 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1233 qh->qh_endpt1 =
1234 cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001235 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1236 (1 << 14) |
1237 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1238 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
Adrian Coxea427772014-04-10 13:29:45 +01001239 (usb_pipedevice(pipe) << 0));
1240 qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1241 (1 << 0)); /* S-mask: microframe 0 */
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001242 if (dev->speed == USB_SPEED_LOW ||
1243 dev->speed == USB_SPEED_FULL) {
Hans de Goede4e2c4ad2014-09-20 16:51:22 +02001244 /* C-mask: microframes 2-4 */
1245 qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001246 }
Hans de Goede4e2c4ad2014-09-20 16:51:22 +02001247 ehci_update_endpt2_dev_n_port(dev, qh);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001248
Adrian Coxea427772014-04-10 13:29:45 +01001249 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1250 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001251 debug("communication direction is '%s'\n",
1252 usb_pipein(pipe) ? "in" : "out");
Adrian Coxea427772014-04-10 13:29:45 +01001253 td->qt_token = cpu_to_hc32((elementsize << 16) |
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001254 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
Adrian Coxea427772014-04-10 13:29:45 +01001255 0x80); /* active */
1256 td->qt_buffer[0] =
Rob Herring98ae8402015-03-17 15:46:37 -05001257 cpu_to_hc32((unsigned long)buffer + i * elementsize);
Adrian Coxea427772014-04-10 13:29:45 +01001258 td->qt_buffer[1] =
1259 cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1260 td->qt_buffer[2] =
1261 cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1262 td->qt_buffer[3] =
1263 cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1264 td->qt_buffer[4] =
1265 cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001266
1267 *buf = buffer + i * elementsize;
1268 }
1269
Rob Herring98ae8402015-03-17 15:46:37 -05001270 flush_dcache_range((unsigned long)buffer,
Stephen Warrend3e07472013-05-24 15:03:17 -06001271 ALIGN_END_ADDR(char, buffer,
1272 queuesize * elementsize));
Rob Herring98ae8402015-03-17 15:46:37 -05001273 flush_dcache_range((unsigned long)result->first,
Stephen Warrend3e07472013-05-24 15:03:17 -06001274 ALIGN_END_ADDR(struct QH, result->first,
1275 queuesize));
Rob Herring98ae8402015-03-17 15:46:37 -05001276 flush_dcache_range((unsigned long)result->tds,
Stephen Warrend3e07472013-05-24 15:03:17 -06001277 ALIGN_END_ADDR(struct qTD, result->tds,
1278 queuesize));
1279
Hans de Goede32f2eac2014-09-24 14:06:03 +02001280 if (ctrl->periodic_schedules > 0) {
1281 if (disable_periodic(ctrl) < 0) {
1282 debug("FATAL: periodic should never fail, but did");
1283 goto fail3;
1284 }
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001285 }
1286
1287 /* hook up to periodic list */
1288 struct QH *list = &ctrl->periodic_queue;
1289 result->last->qh_link = list->qh_link;
Rob Herring98ae8402015-03-17 15:46:37 -05001290 list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001291
Rob Herring98ae8402015-03-17 15:46:37 -05001292 flush_dcache_range((unsigned long)result->last,
Stephen Warrend3e07472013-05-24 15:03:17 -06001293 ALIGN_END_ADDR(struct QH, result->last, 1));
Rob Herring98ae8402015-03-17 15:46:37 -05001294 flush_dcache_range((unsigned long)list,
Stephen Warrend3e07472013-05-24 15:03:17 -06001295 ALIGN_END_ADDR(struct QH, list, 1));
1296
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001297 if (enable_periodic(ctrl) < 0) {
1298 debug("FATAL: periodic should never fail, but did");
1299 goto fail3;
1300 }
Hans de Goede36b73102014-09-20 16:51:25 +02001301 ctrl->periodic_schedules++;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001302
1303 debug("Exit create_int_queue\n");
1304 return result;
1305fail3:
1306 if (result->tds)
1307 free(result->tds);
1308fail2:
1309 if (result->first)
1310 free(result->first);
1311 if (result)
1312 free(result);
1313fail1:
1314 return NULL;
1315}
1316
1317void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1318{
1319 struct QH *cur = queue->current;
Hans de Goede415548d2014-09-20 16:51:24 +02001320 struct qTD *cur_td;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001321
1322 /* depleted queue */
1323 if (cur == NULL) {
1324 debug("Exit poll_int_queue with completed queue\n");
1325 return NULL;
1326 }
1327 /* still active */
Hans de Goede415548d2014-09-20 16:51:24 +02001328 cur_td = &queue->tds[queue->current - queue->first];
Rob Herring98ae8402015-03-17 15:46:37 -05001329 invalidate_dcache_range((unsigned long)cur_td,
Hans de Goede415548d2014-09-20 16:51:24 +02001330 ALIGN_END_ADDR(struct qTD, cur_td, 1));
1331 if (QT_TOKEN_GET_STATUS(hc32_to_cpu(cur_td->qt_token)) &
1332 QT_TOKEN_STATUS_ACTIVE) {
1333 debug("Exit poll_int_queue with no completed intr transfer. token is %x\n",
1334 hc32_to_cpu(cur_td->qt_token));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001335 return NULL;
1336 }
1337 if (!(cur->qh_link & QH_LINK_TERMINATE))
1338 queue->current++;
1339 else
1340 queue->current = NULL;
Hans de Goede8aa26b82014-09-24 14:06:05 +02001341
Rob Herring98ae8402015-03-17 15:46:37 -05001342 invalidate_dcache_range((unsigned long)cur->buffer,
Hans de Goede8aa26b82014-09-24 14:06:05 +02001343 ALIGN_END_ADDR(char, cur->buffer,
1344 queue->elementsize));
1345
Hans de Goede415548d2014-09-20 16:51:24 +02001346 debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
1347 hc32_to_cpu(cur_td->qt_token), cur, queue->first);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001348 return cur->buffer;
1349}
1350
1351/* Do not free buffers associated with QHs, they're owned by someone else */
Hans de Goede8460b892014-09-24 14:06:06 +02001352int
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001353destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1354{
1355 struct ehci_ctrl *ctrl = dev->controller;
1356 int result = -1;
1357 unsigned long timeout;
1358
1359 if (disable_periodic(ctrl) < 0) {
1360 debug("FATAL: periodic should never fail, but did");
1361 goto out;
1362 }
Hans de Goede36b73102014-09-20 16:51:25 +02001363 ctrl->periodic_schedules--;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001364
1365 struct QH *cur = &ctrl->periodic_queue;
1366 timeout = get_timer(0) + 500; /* abort after 500ms */
Adrian Coxea427772014-04-10 13:29:45 +01001367 while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001368 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1369 if (NEXT_QH(cur) == queue->first) {
1370 debug("found candidate. removing from chain\n");
1371 cur->qh_link = queue->last->qh_link;
Rob Herring98ae8402015-03-17 15:46:37 -05001372 flush_dcache_range((unsigned long)cur,
Hans de Goedeea7b30c2014-09-20 16:51:23 +02001373 ALIGN_END_ADDR(struct QH, cur, 1));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001374 result = 0;
1375 break;
1376 }
1377 cur = NEXT_QH(cur);
1378 if (get_timer(0) > timeout) {
1379 printf("Timeout destroying interrupt endpoint queue\n");
1380 result = -1;
1381 goto out;
1382 }
1383 }
1384
Hans de Goede36b73102014-09-20 16:51:25 +02001385 if (ctrl->periodic_schedules > 0) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001386 result = enable_periodic(ctrl);
1387 if (result < 0)
1388 debug("FATAL: periodic should never fail, but did");
1389 }
1390
1391out:
1392 free(queue->tds);
1393 free(queue->first);
1394 free(queue);
1395
1396 return result;
1397}
1398
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001399int
1400submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1401 int length, int interval)
1402{
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001403 void *backbuffer;
1404 struct int_queue *queue;
1405 unsigned long timeout;
1406 int result = 0, ret;
1407
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001408 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1409 dev, pipe, buffer, length, interval);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001410
Hans de Goede8bb6c1d2015-01-11 20:38:28 +01001411 queue = create_int_queue(dev, pipe, 1, length, buffer, interval);
Hans de Goedebd818d82014-09-24 14:06:04 +02001412 if (!queue)
1413 return -1;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001414
1415 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1416 while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
1417 if (get_timer(0) > timeout) {
1418 printf("Timeout poll on interrupt endpoint\n");
1419 result = -ETIMEDOUT;
1420 break;
1421 }
1422
1423 if (backbuffer != buffer) {
Rob Herring98ae8402015-03-17 15:46:37 -05001424 debug("got wrong buffer back (%p instead of %p)\n",
1425 backbuffer, buffer);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001426 return -EINVAL;
1427 }
1428
1429 ret = destroy_int_queue(dev, queue);
1430 if (ret < 0)
1431 return ret;
1432
1433 /* everything worked out fine */
1434 return result;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001435}