blob: 4adf98c112fb61f79fd76972de4870b139226476 [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
Simon Glass727fce32015-03-25 12:22:21 -0600141__weak void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
142 uint32_t *reg)
Marek Vasut3874b6d2011-07-11 02:37:01 +0200143{
144 mdelay(50);
145}
146
Simon Glassaac064f2015-03-25 12:22:17 -0600147__weak uint32_t *ehci_get_portsc_register(struct ehci_hcor *hcor, int port)
148{
149 if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
150 /* Printing the message would cause a scan failure! */
151 debug("The request port(%u) is not configured\n", port);
152 return NULL;
153 }
154
155 return (uint32_t *)&hcor->or_portsc[port];
156}
157
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100158static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michaeldb632992008-12-10 17:55:19 +0100159{
michael51ab1422008-12-11 13:43:55 +0100160 uint32_t result;
161 do {
162 result = ehci_readl(ptr);
Wolfgang Denk09c83a42010-10-22 14:23:00 +0200163 udelay(5);
michael51ab1422008-12-11 13:43:55 +0100164 if (result == ~(uint32_t)0)
165 return -1;
166 result &= mask;
167 if (result == done)
168 return 0;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100169 usec--;
170 } while (usec > 0);
michael51ab1422008-12-11 13:43:55 +0100171 return -1;
172}
173
Lucas Stach676ae062012-09-26 00:14:35 +0200174static int ehci_reset(int index)
michael51ab1422008-12-11 13:43:55 +0100175{
176 uint32_t cmd;
michael51ab1422008-12-11 13:43:55 +0100177 int ret = 0;
178
Lucas Stach676ae062012-09-26 00:14:35 +0200179 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Stefan Roese273d7202010-11-26 15:44:00 +0100180 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Lucas Stach676ae062012-09-26 00:14:35 +0200181 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
182 ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
183 CMD_RESET, 0, 250 * 1000);
michael51ab1422008-12-11 13:43:55 +0100184 if (ret < 0) {
185 printf("EHCI fail to reset\n");
186 goto out;
187 }
188
Jim Linb068deb2013-03-27 00:52:32 +0000189 if (ehci_is_TDI())
190 ehci_set_usbmode(index);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000191
192#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Lucas Stach676ae062012-09-26 00:14:35 +0200193 cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200194 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass9ab4ce22012-02-27 10:52:47 +0000195 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Lucas Stach676ae062012-09-26 00:14:35 +0200196 ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000197#endif
michael51ab1422008-12-11 13:43:55 +0100198out:
199 return ret;
michaeldb632992008-12-10 17:55:19 +0100200}
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100201
Julius Werner5077f962013-09-24 10:53:07 -0700202static int ehci_shutdown(struct ehci_ctrl *ctrl)
203{
204 int i, ret = 0;
205 uint32_t cmd, reg;
206
Marek Vasut1e1be6d2013-12-14 02:03:11 +0100207 if (!ctrl || !ctrl->hcor)
208 return -EINVAL;
209
Julius Werner5077f962013-09-24 10:53:07 -0700210 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
211 cmd &= ~(CMD_PSE | CMD_ASE);
212 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
213 ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
214 100 * 1000);
215
216 if (!ret) {
217 for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
218 reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
219 reg |= EHCI_PS_SUSP;
220 ehci_writel(&ctrl->hcor->or_portsc[i], reg);
221 }
222
223 cmd &= ~CMD_RUN;
224 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
225 ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
226 HCHALT_TIMEOUT);
227 }
228
229 if (ret)
230 puts("EHCI failed to shut down host controller.\n");
231
232 return ret;
233}
234
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100235static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
236{
Marek Vasutb8adb122012-04-09 04:07:46 +0200237 uint32_t delta, next;
Rob Herring98ae8402015-03-17 15:46:37 -0500238 uint32_t addr = (unsigned long)buf;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100239 int idx;
240
Ilya Yanok189a6952012-07-15 04:43:49 +0000241 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutb8adb122012-04-09 04:07:46 +0200242 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
243
Ilya Yanok189a6952012-07-15 04:43:49 +0000244 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
245
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100246 idx = 0;
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200247 while (idx < QT_BUFFER_CNT) {
michaeldb632992008-12-10 17:55:19 +0100248 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denk3ed16072010-10-19 16:13:15 +0200249 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200250 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100251 delta = next - addr;
252 if (delta >= sz)
253 break;
254 sz -= delta;
255 addr = next;
256 idx++;
257 }
258
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200259 if (idx == QT_BUFFER_CNT) {
Rob Herring98ae8402015-03-17 15:46:37 -0500260 printf("out of buffer pointers (%zu bytes left)\n", sz);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100261 return -1;
262 }
263
264 return 0;
265}
266
Ilya Yanokc60795f2012-11-06 13:48:20 +0000267static inline u8 ehci_encode_speed(enum usb_device_speed speed)
268{
269 #define QH_HIGH_SPEED 2
270 #define QH_FULL_SPEED 0
271 #define QH_LOW_SPEED 1
272 if (speed == USB_SPEED_HIGH)
273 return QH_HIGH_SPEED;
274 if (speed == USB_SPEED_LOW)
275 return QH_LOW_SPEED;
276 return QH_FULL_SPEED;
277}
278
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200279static void ehci_update_endpt2_dev_n_port(struct usb_device *dev,
280 struct QH *qh)
281{
282 struct usb_device *ttdev;
283
284 if (dev->speed != USB_SPEED_LOW && dev->speed != USB_SPEED_FULL)
285 return;
286
287 /*
288 * For full / low speed devices we need to get the devnum and portnr of
289 * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
290 * in the tree before that one!
291 */
292 ttdev = dev;
293 while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
294 ttdev = ttdev->parent;
295 if (!ttdev->parent)
296 return;
297
298 qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
299 QH_ENDPT2_HUBADDR(ttdev->parent->devnum));
300}
301
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100302static int
303ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
304 int length, struct devrequest *req)
305{
Tom Rini71c5de42012-07-15 22:14:24 +0000306 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200307 struct qTD *qtd;
308 int qtd_count = 0;
Marek Vasutde98e8b2012-04-08 23:32:05 +0200309 int qtd_counter = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100310 volatile struct qTD *vtd;
311 unsigned long ts;
312 uint32_t *tdp;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200313 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100314 uint32_t c, toggle;
michaeldb632992008-12-10 17:55:19 +0100315 uint32_t cmd;
Simon Glass96820a32011-02-07 14:42:16 -0800316 int timeout;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100317 int ret = 0;
Lucas Stach676ae062012-09-26 00:14:35 +0200318 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100319
michaeldb632992008-12-10 17:55:19 +0100320 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100321 buffer, length, req);
322 if (req != NULL)
michaeldb632992008-12-10 17:55:19 +0100323 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100324 req->request, req->request,
325 req->requesttype, req->requesttype,
326 le16_to_cpu(req->value), le16_to_cpu(req->value),
michaeldb632992008-12-10 17:55:19 +0100327 le16_to_cpu(req->index));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100328
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200329#define PKT_ALIGN 512
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200330 /*
331 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
332 * described by a transfer descriptor (the qTD). The qTDs form a linked
333 * list with a queue head (QH).
334 *
335 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
336 * have its beginning in a qTD transfer and its end in the following
337 * one, so the qTD transfer lengths have to be chosen accordingly.
338 *
339 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
340 * single pages. The first data buffer can start at any offset within a
341 * page (not considering the cache-line alignment issues), while the
342 * following buffers must be page-aligned. There is no alignment
343 * constraint on the size of a qTD transfer.
344 */
345 if (req != NULL)
346 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
347 qtd_count += 1 + 1;
348 if (length > 0 || req == NULL) {
349 /*
350 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200351 * data payload (not considering the first qTD transfer, which
352 * may be longer or shorter, and the final one, which may be
353 * shorter).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200354 *
355 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200356 * transfer size is aligned to PKT_ALIGN, which is a multiple of
357 * wMaxPacketSize (except in some cases for interrupt transfers,
358 * see comment in submit_int_msg()).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200359 *
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200360 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200361 * QT_BUFFER_CNT full pages will be used.
362 */
363 int xfr_sz = QT_BUFFER_CNT;
364 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200365 * However, if the input buffer is not aligned to PKT_ALIGN, the
366 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200367 * data buffer of each transfer will be page-unaligned.
368 */
Rob Herring98ae8402015-03-17 15:46:37 -0500369 if ((unsigned long)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200370 xfr_sz--;
371 /* Convert the qTD transfer size to bytes. */
372 xfr_sz *= EHCI_PAGE_SIZE;
373 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200374 * Approximate by excess the number of qTDs that will be
375 * required for the data payload. The exact formula is way more
376 * complicated and saves at most 2 qTDs, i.e. a total of 128
377 * bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200378 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200379 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200380 }
381/*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200382 * Threshold value based on the worst-case total size of the allocated qTDs for
383 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200384 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200385#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200386#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
387#endif
388 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
389 if (qtd == NULL) {
390 printf("unable to allocate TDs\n");
391 return -1;
392 }
393
Tom Rini71c5de42012-07-15 22:14:24 +0000394 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200395 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200396
Marek Vasutb8adb122012-04-09 04:07:46 +0200397 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
398
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200399 /*
400 * Setup QH (3.6 in ehci-r10.pdf)
401 *
402 * qh_link ................. 03-00 H
403 * qh_endpt1 ............... 07-04 H
404 * qh_endpt2 ............... 0B-08 H
405 * - qh_curtd
406 * qh_overlay.qt_next ...... 13-10 H
407 * - qh_overlay.qt_altnext
408 */
Rob Herring98ae8402015-03-17 15:46:37 -0500409 qh->qh_link = cpu_to_hc32((unsigned long)&ctrl->qh_list | QH_LINK_TYPE_QH);
Ilya Yanokc60795f2012-11-06 13:48:20 +0000410 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200411 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200412 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200413 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200414 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
Ilya Yanokc60795f2012-11-06 13:48:20 +0000415 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200416 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
417 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini71c5de42012-07-15 22:14:24 +0000418 qh->qh_endpt1 = cpu_to_hc32(endpt);
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200419 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini71c5de42012-07-15 22:14:24 +0000420 qh->qh_endpt2 = cpu_to_hc32(endpt);
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200421 ehci_update_endpt2_dev_n_port(dev, qh);
Tom Rini71c5de42012-07-15 22:14:24 +0000422 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Stephen Warren2456b972014-02-07 09:53:50 -0700423 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100424
Tom Rini71c5de42012-07-15 22:14:24 +0000425 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100426
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100427 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200428 /*
429 * Setup request qTD (3.5 in ehci-r10.pdf)
430 *
431 * qt_next ................ 03-00 H
432 * qt_altnext ............. 07-04 H
433 * qt_token ............... 0B-08 H
434 *
435 * [ buffer, buffer_hi ] loaded with "req".
436 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200437 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
438 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200439 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
440 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
441 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
442 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200443 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200444 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
445 printf("unable to construct SETUP TD\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100446 goto fail;
447 }
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200448 /* Update previous qTD! */
Rob Herring98ae8402015-03-17 15:46:37 -0500449 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200450 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100451 toggle = 1;
452 }
453
454 if (length > 0 || req == NULL) {
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200455 uint8_t *buf_ptr = buffer;
456 int left_length = length;
457
458 do {
459 /*
460 * Determine the size of this qTD transfer. By default,
461 * QT_BUFFER_CNT full pages can be used.
462 */
463 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
464 /*
465 * However, if the input buffer is not page-aligned, the
466 * portion of the first page before the buffer start
467 * offset within that page is unusable.
468 */
Rob Herring98ae8402015-03-17 15:46:37 -0500469 xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200470 /*
471 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200472 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200473 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200474 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200475 /*
476 * This transfer may be shorter than the available qTD
477 * transfer size that has just been computed.
478 */
479 xfr_bytes = min(xfr_bytes, left_length);
480
481 /*
482 * Setup request qTD (3.5 in ehci-r10.pdf)
483 *
484 * qt_next ................ 03-00 H
485 * qt_altnext ............. 07-04 H
486 * qt_token ............... 0B-08 H
487 *
488 * [ buffer, buffer_hi ] loaded with "buffer".
489 */
490 qtd[qtd_counter].qt_next =
491 cpu_to_hc32(QT_NEXT_TERMINATE);
492 qtd[qtd_counter].qt_altnext =
493 cpu_to_hc32(QT_NEXT_TERMINATE);
494 token = QT_TOKEN_DT(toggle) |
495 QT_TOKEN_TOTALBYTES(xfr_bytes) |
496 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
497 QT_TOKEN_CERR(3) |
498 QT_TOKEN_PID(usb_pipein(pipe) ?
499 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
500 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
501 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
502 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
503 xfr_bytes)) {
504 printf("unable to construct DATA TD\n");
505 goto fail;
506 }
507 /* Update previous qTD! */
Rob Herring98ae8402015-03-17 15:46:37 -0500508 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200509 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200510 /*
511 * Data toggle has to be adjusted since the qTD transfer
512 * size is not always an even multiple of
513 * wMaxPacketSize.
514 */
515 if ((xfr_bytes / maxpacket) & 1)
516 toggle ^= 1;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200517 buf_ptr += xfr_bytes;
518 left_length -= xfr_bytes;
519 } while (left_length > 0);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100520 }
521
522 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200523 /*
524 * Setup request qTD (3.5 in ehci-r10.pdf)
525 *
526 * qt_next ................ 03-00 H
527 * qt_altnext ............. 07-04 H
528 * qt_token ............... 0B-08 H
529 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200530 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
531 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200532 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200533 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
534 QT_TOKEN_PID(usb_pipein(pipe) ?
535 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
536 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200537 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200538 /* Update previous qTD! */
Rob Herring98ae8402015-03-17 15:46:37 -0500539 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200540 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100541 }
542
Rob Herring98ae8402015-03-17 15:46:37 -0500543 ctrl->qh_list.qh_link = cpu_to_hc32((unsigned long)qh | QH_LINK_TYPE_QH);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100544
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100545 /* Flush dcache */
Rob Herring98ae8402015-03-17 15:46:37 -0500546 flush_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach676ae062012-09-26 00:14:35 +0200547 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500548 flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
549 flush_dcache_range((unsigned long)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200550 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100551
Ilya Yanokc7701af2012-07-15 22:12:08 +0000552 /* Set async. queue head pointer. */
Rob Herring98ae8402015-03-17 15:46:37 -0500553 ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)&ctrl->qh_list);
Ilya Yanokc7701af2012-07-15 22:12:08 +0000554
Lucas Stach676ae062012-09-26 00:14:35 +0200555 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
556 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100557
558 /* Enable async. schedule. */
Lucas Stach676ae062012-09-26 00:14:35 +0200559 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael51ab1422008-12-11 13:43:55 +0100560 cmd |= CMD_ASE;
Lucas Stach676ae062012-09-26 00:14:35 +0200561 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michaeldb632992008-12-10 17:55:19 +0100562
Lucas Stach676ae062012-09-26 00:14:35 +0200563 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100564 100 * 1000);
565 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200566 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100567 goto fail;
michael51ab1422008-12-11 13:43:55 +0100568 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100569
570 /* Wait for TDs to be processed. */
571 ts = get_timer(0);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200572 vtd = &qtd[qtd_counter - 1];
Simon Glass96820a32011-02-07 14:42:16 -0800573 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100574 do {
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100575 /* Invalidate dcache */
Rob Herring98ae8402015-03-17 15:46:37 -0500576 invalidate_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach676ae062012-09-26 00:14:35 +0200577 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500578 invalidate_dcache_range((unsigned long)qh,
Tom Rini71c5de42012-07-15 22:14:24 +0000579 ALIGN_END_ADDR(struct QH, qh, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500580 invalidate_dcache_range((unsigned long)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200581 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutb8adb122012-04-09 04:07:46 +0200582
michaeldb632992008-12-10 17:55:19 +0100583 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200584 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100585 break;
Stefan Roese67333f72010-11-26 15:43:28 +0100586 WATCHDOG_RESET();
Simon Glass96820a32011-02-07 14:42:16 -0800587 } while (get_timer(ts) < timeout);
588
Ilya Yanok189a6952012-07-15 04:43:49 +0000589 /*
590 * Invalidate the memory area occupied by buffer
591 * Don't try to fix the buffer alignment, if it isn't properly
592 * aligned it's upper layer's fault so let invalidate_dcache_range()
593 * vow about it. But we have to fix the length as it's actual
594 * transfer length and can be unaligned. This is potentially
595 * dangerous operation, it's responsibility of the calling
596 * code to make sure enough space is reserved.
597 */
Rob Herring98ae8402015-03-17 15:46:37 -0500598 invalidate_dcache_range((unsigned long)buffer,
599 ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutb8adb122012-04-09 04:07:46 +0200600
Simon Glass96820a32011-02-07 14:42:16 -0800601 /* Check that the TD processing happened */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200602 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glass96820a32011-02-07 14:42:16 -0800603 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100604
605 /* Disable async schedule. */
Lucas Stach676ae062012-09-26 00:14:35 +0200606 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michaeldb632992008-12-10 17:55:19 +0100607 cmd &= ~CMD_ASE;
Lucas Stach676ae062012-09-26 00:14:35 +0200608 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +0100609
Lucas Stach676ae062012-09-26 00:14:35 +0200610 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100611 100 * 1000);
612 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200613 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100614 goto fail;
michael51ab1422008-12-11 13:43:55 +0100615 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100616
Tom Rini71c5de42012-07-15 22:14:24 +0000617 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200618 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michaeldb632992008-12-10 17:55:19 +0100619 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200620 switch (QT_TOKEN_GET_STATUS(token) &
621 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100622 case 0:
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200623 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100624 usb_settoggle(dev, usb_pipeendpoint(pipe),
625 usb_pipeout(pipe), toggle);
626 dev->status = 0;
627 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200628 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100629 dev->status = USB_ST_STALLED;
630 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200631 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
632 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100633 dev->status = USB_ST_BUF_ERR;
634 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200635 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
636 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100637 dev->status = USB_ST_BABBLE_DET;
638 break;
639 default:
640 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200641 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschin222d6df2010-11-02 11:47:29 +0100642 dev->status |= USB_ST_STALLED;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100643 break;
644 }
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200645 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100646 } else {
647 dev->act_len = 0;
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800648#ifndef CONFIG_USB_EHCI_FARADAY
michaeldb632992008-12-10 17:55:19 +0100649 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach676ae062012-09-26 00:14:35 +0200650 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
651 ehci_readl(&ctrl->hcor->or_portsc[0]),
652 ehci_readl(&ctrl->hcor->or_portsc[1]));
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800653#endif
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100654 }
655
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200656 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100657 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
658
659fail:
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200660 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100661 return -1;
662}
663
michaeldb632992008-12-10 17:55:19 +0100664int
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100665ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
666 int length, struct devrequest *req)
667{
668 uint8_t tmpbuf[4];
669 u16 typeReq;
michaeldb632992008-12-10 17:55:19 +0100670 void *srcptr = NULL;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100671 int len, srclen;
672 uint32_t reg;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100673 uint32_t *status_reg;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000674 int port = le16_to_cpu(req->index) & 0xff;
Lucas Stach676ae062012-09-26 00:14:35 +0200675 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100676
677 srclen = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100678
michaeldb632992008-12-10 17:55:19 +0100679 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100680 req->request, req->request,
681 req->requesttype, req->requesttype,
682 le16_to_cpu(req->value), le16_to_cpu(req->index));
683
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530684 typeReq = req->request | req->requesttype << 8;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100685
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530686 switch (typeReq) {
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800687 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
688 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
689 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Kuo-Jung Su1dde1422013-05-15 15:29:21 +0800690 status_reg = ehci_get_portsc_register(ctrl->hcor, port - 1);
691 if (!status_reg)
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800692 return -1;
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800693 break;
694 default:
695 status_reg = NULL;
696 break;
697 }
698
699 switch (typeReq) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100700 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
701 switch (le16_to_cpu(req->value) >> 8) {
702 case USB_DT_DEVICE:
michaeldb632992008-12-10 17:55:19 +0100703 debug("USB_DT_DEVICE request\n");
704 srcptr = &descriptor.device;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200705 srclen = descriptor.device.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100706 break;
707 case USB_DT_CONFIG:
michaeldb632992008-12-10 17:55:19 +0100708 debug("USB_DT_CONFIG config\n");
709 srcptr = &descriptor.config;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200710 srclen = descriptor.config.bLength +
711 descriptor.interface.bLength +
712 descriptor.endpoint.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100713 break;
714 case USB_DT_STRING:
michaeldb632992008-12-10 17:55:19 +0100715 debug("USB_DT_STRING config\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100716 switch (le16_to_cpu(req->value) & 0xff) {
717 case 0: /* Language */
718 srcptr = "\4\3\1\0";
719 srclen = 4;
720 break;
721 case 1: /* Vendor */
722 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
723 srclen = 14;
724 break;
725 case 2: /* Product */
726 srcptr = "\52\3E\0H\0C\0I\0 "
727 "\0H\0o\0s\0t\0 "
728 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
729 srclen = 42;
730 break;
731 default:
michaeldb632992008-12-10 17:55:19 +0100732 debug("unknown value DT_STRING %x\n",
733 le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100734 goto unknown;
735 }
736 break;
737 default:
michaeldb632992008-12-10 17:55:19 +0100738 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100739 goto unknown;
740 }
741 break;
742 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
743 switch (le16_to_cpu(req->value) >> 8) {
744 case USB_DT_HUB:
michaeldb632992008-12-10 17:55:19 +0100745 debug("USB_DT_HUB config\n");
746 srcptr = &descriptor.hub;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200747 srclen = descriptor.hub.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100748 break;
749 default:
michaeldb632992008-12-10 17:55:19 +0100750 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100751 goto unknown;
752 }
753 break;
754 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michaeldb632992008-12-10 17:55:19 +0100755 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach676ae062012-09-26 00:14:35 +0200756 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100757 break;
758 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michaeldb632992008-12-10 17:55:19 +0100759 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100760 /* Nothing to do */
761 break;
762 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
763 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
764 tmpbuf[1] = 0;
765 srcptr = tmpbuf;
766 srclen = 2;
767 break;
michaeldb632992008-12-10 17:55:19 +0100768 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100769 memset(tmpbuf, 0, 4);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100770 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100771 if (reg & EHCI_PS_CS)
772 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
773 if (reg & EHCI_PS_PE)
774 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
775 if (reg & EHCI_PS_SUSP)
776 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
777 if (reg & EHCI_PS_OCA)
778 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300779 if (reg & EHCI_PS_PR)
780 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100781 if (reg & EHCI_PS_PP)
782 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese597eb282009-01-21 17:12:01 +0100783
784 if (ehci_is_TDI()) {
Simon Glass73382872015-03-25 12:22:18 -0600785 switch (ehci_get_port_speed(ctrl, reg)) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200786 case PORTSC_PSPD_FS:
Stefan Roese597eb282009-01-21 17:12:01 +0100787 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200788 case PORTSC_PSPD_LS:
Stefan Roese597eb282009-01-21 17:12:01 +0100789 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
790 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200791 case PORTSC_PSPD_HS:
Stefan Roese597eb282009-01-21 17:12:01 +0100792 default:
793 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
794 break;
795 }
796 } else {
797 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
798 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100799
800 if (reg & EHCI_PS_CSC)
801 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
802 if (reg & EHCI_PS_PEC)
803 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
804 if (reg & EHCI_PS_OCC)
805 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000806 if (ctrl->portreset & (1 << port))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100807 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100808
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100809 srcptr = tmpbuf;
810 srclen = 4;
811 break;
michaeldb632992008-12-10 17:55:19 +0100812 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100813 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100814 reg &= ~EHCI_PS_CLEAR;
815 switch (le16_to_cpu(req->value)) {
michael51ab1422008-12-11 13:43:55 +0100816 case USB_PORT_FEAT_ENABLE:
817 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100818 ehci_writel(status_reg, reg);
michael51ab1422008-12-11 13:43:55 +0100819 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100820 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200821 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100822 reg |= EHCI_PS_PP;
823 ehci_writel(status_reg, reg);
824 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100825 break;
826 case USB_PORT_FEAT_RESET:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100827 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
828 !ehci_is_TDI() &&
829 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100830 /* Low speed device, give up ownership. */
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100831 debug("port %d low speed --> companion\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000832 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100833 reg |= EHCI_PS_PO;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100834 ehci_writel(status_reg, reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100835 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100836 } else {
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300837 int ret;
838
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100839 reg |= EHCI_PS_PR;
840 reg &= ~EHCI_PS_PE;
841 ehci_writel(status_reg, reg);
842 /*
843 * caller must wait, then call GetPortStatus
844 * usb 2.0 specification say 50 ms resets on
845 * root
846 */
Simon Glass727fce32015-03-25 12:22:21 -0600847 ehci_powerup_fixup(ctrl, status_reg, &reg);
Marek Vasut3874b6d2011-07-11 02:37:01 +0200848
Chris Zhangb4161912010-01-06 13:34:04 -0800849 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300850 /*
851 * A host controller must terminate the reset
852 * and stabilize the state of the port within
853 * 2 milliseconds
854 */
855 ret = handshake(status_reg, EHCI_PS_PR, 0,
856 2 * 1000);
857 if (!ret)
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000858 ctrl->portreset |= 1 << port;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300859 else
860 printf("port(%d) reset error\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000861 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100862 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100863 break;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000864 case USB_PORT_FEAT_TEST:
Julius Werner5077f962013-09-24 10:53:07 -0700865 ehci_shutdown(ctrl);
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000866 reg &= ~(0xf << 16);
867 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
868 ehci_writel(status_reg, reg);
869 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100870 default:
michaeldb632992008-12-10 17:55:19 +0100871 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100872 goto unknown;
873 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100874 /* unblock posted writes */
Lucas Stach676ae062012-09-26 00:14:35 +0200875 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100876 break;
michaeldb632992008-12-10 17:55:19 +0100877 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100878 reg = ehci_readl(status_reg);
Simon Glassed10e662013-05-10 19:49:00 -0700879 reg &= ~EHCI_PS_CLEAR;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100880 switch (le16_to_cpu(req->value)) {
881 case USB_PORT_FEAT_ENABLE:
882 reg &= ~EHCI_PS_PE;
883 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100884 case USB_PORT_FEAT_C_ENABLE:
Simon Glassed10e662013-05-10 19:49:00 -0700885 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100886 break;
887 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200888 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Simon Glassed10e662013-05-10 19:49:00 -0700889 reg &= ~EHCI_PS_PP;
890 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100891 case USB_PORT_FEAT_C_CONNECTION:
Simon Glassed10e662013-05-10 19:49:00 -0700892 reg |= EHCI_PS_CSC;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100893 break;
michael51ab1422008-12-11 13:43:55 +0100894 case USB_PORT_FEAT_OVER_CURRENT:
Simon Glassed10e662013-05-10 19:49:00 -0700895 reg |= EHCI_PS_OCC;
michael51ab1422008-12-11 13:43:55 +0100896 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100897 case USB_PORT_FEAT_C_RESET:
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000898 ctrl->portreset &= ~(1 << port);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100899 break;
900 default:
michaeldb632992008-12-10 17:55:19 +0100901 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100902 goto unknown;
903 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100904 ehci_writel(status_reg, reg);
905 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +0200906 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100907 break;
908 default:
michaeldb632992008-12-10 17:55:19 +0100909 debug("Unknown request\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100910 goto unknown;
911 }
912
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000913 mdelay(1);
Masahiro Yamadab4141192014-11-07 03:03:31 +0900914 len = min3(srclen, (int)le16_to_cpu(req->length), length);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100915 if (srcptr != NULL && len > 0)
916 memcpy(buffer, srcptr, len);
michaeldb632992008-12-10 17:55:19 +0100917 else
918 debug("Len is 0\n");
919
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100920 dev->act_len = len;
921 dev->status = 0;
922 return 0;
923
924unknown:
michaeldb632992008-12-10 17:55:19 +0100925 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100926 req->requesttype, req->request, le16_to_cpu(req->value),
927 le16_to_cpu(req->index), le16_to_cpu(req->length));
928
929 dev->act_len = 0;
930 dev->status = USB_ST_STALLED;
931 return -1;
932}
933
Simon Glassc4a31412015-03-25 12:22:19 -0600934void ehci_set_controller_priv(int index, void *priv)
935{
936 ehcic[index].priv = priv;
937}
938
939void *ehci_get_controller_priv(int index)
940{
941 return ehcic[index].priv;
942}
943
Lucas Stachc7e3b2b2012-09-26 00:14:34 +0200944int usb_lowlevel_stop(int index)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100945{
Julius Werner5077f962013-09-24 10:53:07 -0700946 ehci_shutdown(&ehcic[index]);
Lucas Stach676ae062012-09-26 00:14:35 +0200947 return ehci_hcd_stop(index);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100948}
949
Troy Kisky06d513e2013-10-10 15:27:56 -0700950int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100951{
952 uint32_t reg;
michaeldb632992008-12-10 17:55:19 +0100953 uint32_t cmd;
Lucas Stach676ae062012-09-26 00:14:35 +0200954 struct QH *qh_list;
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000955 struct QH *periodic;
956 int i;
Troy Kisky127efc42013-10-10 15:27:57 -0700957 int rc;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100958
Troy Kisky127efc42013-10-10 15:27:57 -0700959 rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
960 if (rc)
961 return rc;
962 if (init == USB_INIT_DEVICE)
963 goto done;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100964
michael51ab1422008-12-11 13:43:55 +0100965 /* EHCI spec section 4.1 */
Lucas Stach676ae062012-09-26 00:14:35 +0200966 if (ehci_reset(index))
michael51ab1422008-12-11 13:43:55 +0100967 return -1;
968
Stefan Roese832e6142009-01-21 17:12:10 +0100969#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
Troy Kisky127efc42013-10-10 15:27:57 -0700970 rc = ehci_hcd_init(index, init, &ehcic[index].hccr, &ehcic[index].hcor);
971 if (rc)
972 return rc;
Stefan Roese832e6142009-01-21 17:12:10 +0100973#endif
Vincent Palatin29828372012-12-12 17:55:22 -0800974 /* Set the high address word (aka segment) for 64-bit controller */
975 if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
Marek Vasuteb632182013-12-14 02:04:52 +0100976 ehci_writel(&ehcic[index].hcor->or_ctrldssegment, 0);
Stefan Roese832e6142009-01-21 17:12:10 +0100977
Lucas Stach676ae062012-09-26 00:14:35 +0200978 qh_list = &ehcic[index].qh_list;
979
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100980 /* Set head of reclaim list */
Tom Rini71c5de42012-07-15 22:14:24 +0000981 memset(qh_list, 0, sizeof(*qh_list));
Rob Herring98ae8402015-03-17 15:46:37 -0500982 qh_list->qh_link = cpu_to_hc32((unsigned long)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200983 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
984 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini71c5de42012-07-15 22:14:24 +0000985 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
986 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200987 qh_list->qh_overlay.qt_token =
988 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100989
Rob Herring98ae8402015-03-17 15:46:37 -0500990 flush_dcache_range((unsigned long)qh_list,
Stephen Warrend3e07472013-05-24 15:03:17 -0600991 ALIGN_END_ADDR(struct QH, qh_list, 1));
992
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000993 /* Set async. queue head pointer. */
Rob Herring98ae8402015-03-17 15:46:37 -0500994 ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (unsigned long)qh_list);
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000995
996 /*
997 * Set up periodic list
998 * Step 1: Parent QH for all periodic transfers.
999 */
Hans de Goede36b73102014-09-20 16:51:25 +02001000 ehcic[index].periodic_schedules = 0;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001001 periodic = &ehcic[index].periodic_queue;
1002 memset(periodic, 0, sizeof(*periodic));
1003 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
1004 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1005 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1006
Rob Herring98ae8402015-03-17 15:46:37 -05001007 flush_dcache_range((unsigned long)periodic,
Stephen Warrend3e07472013-05-24 15:03:17 -06001008 ALIGN_END_ADDR(struct QH, periodic, 1));
1009
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001010 /*
1011 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
1012 * In particular, device specifications on polling frequency
1013 * are disregarded. Keyboards seem to send NAK/NYet reliably
1014 * when polled with an empty buffer.
1015 *
1016 * Split Transactions will be spread across microframes using
1017 * S-mask and C-mask.
1018 */
Nikita Kiryanov8bc36032013-07-29 13:27:40 +03001019 if (ehcic[index].periodic_list == NULL)
1020 ehcic[index].periodic_list = memalign(4096, 1024 * 4);
1021
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001022 if (!ehcic[index].periodic_list)
1023 return -ENOMEM;
1024 for (i = 0; i < 1024; i++) {
Rob Herring98ae8402015-03-17 15:46:37 -05001025 ehcic[index].periodic_list[i] = cpu_to_hc32((unsigned long)periodic
Adrian Coxea427772014-04-10 13:29:45 +01001026 | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001027 }
1028
Rob Herring98ae8402015-03-17 15:46:37 -05001029 flush_dcache_range((unsigned long)ehcic[index].periodic_list,
Stephen Warrend3e07472013-05-24 15:03:17 -06001030 ALIGN_END_ADDR(uint32_t, ehcic[index].periodic_list,
1031 1024));
1032
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001033 /* Set periodic list base address */
1034 ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
Rob Herring98ae8402015-03-17 15:46:37 -05001035 (unsigned long)ehcic[index].periodic_list);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001036
Lucas Stach676ae062012-09-26 00:14:35 +02001037 reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
michael51ab1422008-12-11 13:43:55 +01001038 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stach7a46b2c2012-09-28 00:26:19 +02001039 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001040 /* Port Indicators */
1041 if (HCS_INDICATOR(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +02001042 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1043 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001044 /* Port Power Control */
1045 if (HCS_PPC(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +02001046 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1047 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001048
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001049 /* Start the host controller. */
Lucas Stach676ae062012-09-26 00:14:35 +02001050 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Wolfgang Denkf15c6512009-02-12 00:08:39 +01001051 /*
1052 * Philips, Intel, and maybe others need CMD_RUN before the
1053 * root hub will detect new devices (why?); NEC doesn't
1054 */
michael51ab1422008-12-11 13:43:55 +01001055 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
1056 cmd |= CMD_RUN;
Lucas Stach676ae062012-09-26 00:14:35 +02001057 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +01001058
Kuo-Jung Sue82a3162013-05-15 15:29:23 +08001059#ifndef CONFIG_USB_EHCI_FARADAY
michael51ab1422008-12-11 13:43:55 +01001060 /* take control over the ports */
Lucas Stach676ae062012-09-26 00:14:35 +02001061 cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
michael51ab1422008-12-11 13:43:55 +01001062 cmd |= FLAG_CF;
Lucas Stach676ae062012-09-26 00:14:35 +02001063 ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
Kuo-Jung Sue82a3162013-05-15 15:29:23 +08001064#endif
1065
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001066 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +02001067 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001068 mdelay(5);
Lucas Stach676ae062012-09-26 00:14:35 +02001069 reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001070 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001071
Lucas Stach676ae062012-09-26 00:14:35 +02001072 ehcic[index].rootdev = 0;
Troy Kisky127efc42013-10-10 15:27:57 -07001073done:
Lucas Stach676ae062012-09-26 00:14:35 +02001074 *controller = &ehcic[index];
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001075 return 0;
1076}
1077
1078int
1079submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1080 int length)
1081{
1082
1083 if (usb_pipetype(pipe) != PIPE_BULK) {
1084 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1085 return -1;
1086 }
1087 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1088}
1089
1090int
1091submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1092 int length, struct devrequest *setup)
1093{
Lucas Stach676ae062012-09-26 00:14:35 +02001094 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001095
1096 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1097 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1098 return -1;
1099 }
1100
Lucas Stach676ae062012-09-26 00:14:35 +02001101 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1102 if (!ctrl->rootdev)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001103 dev->speed = USB_SPEED_HIGH;
1104 return ehci_submit_root(dev, pipe, buffer, length, setup);
1105 }
1106 return ehci_submit_async(dev, pipe, buffer, length, setup);
1107}
1108
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001109struct int_queue {
Hans de Goede8aa26b82014-09-24 14:06:05 +02001110 int elementsize;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001111 struct QH *first;
1112 struct QH *current;
1113 struct QH *last;
1114 struct qTD *tds;
1115};
1116
Rob Herring98ae8402015-03-17 15:46:37 -05001117#define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001118
1119static int
1120enable_periodic(struct ehci_ctrl *ctrl)
1121{
1122 uint32_t cmd;
1123 struct ehci_hcor *hcor = ctrl->hcor;
1124 int ret;
1125
1126 cmd = ehci_readl(&hcor->or_usbcmd);
1127 cmd |= CMD_PSE;
1128 ehci_writel(&hcor->or_usbcmd, cmd);
1129
1130 ret = handshake((uint32_t *)&hcor->or_usbsts,
1131 STS_PSS, STS_PSS, 100 * 1000);
1132 if (ret < 0) {
1133 printf("EHCI failed: timeout when enabling periodic list\n");
1134 return -ETIMEDOUT;
1135 }
1136 udelay(1000);
1137 return 0;
1138}
1139
1140static int
1141disable_periodic(struct ehci_ctrl *ctrl)
1142{
1143 uint32_t cmd;
1144 struct ehci_hcor *hcor = ctrl->hcor;
1145 int ret;
1146
1147 cmd = ehci_readl(&hcor->or_usbcmd);
1148 cmd &= ~CMD_PSE;
1149 ehci_writel(&hcor->or_usbcmd, cmd);
1150
1151 ret = handshake((uint32_t *)&hcor->or_usbsts,
1152 STS_PSS, 0, 100 * 1000);
1153 if (ret < 0) {
1154 printf("EHCI failed: timeout when disabling periodic list\n");
1155 return -ETIMEDOUT;
1156 }
1157 return 0;
1158}
1159
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001160struct int_queue *
1161create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
Hans de Goede8bb6c1d2015-01-11 20:38:28 +01001162 int elementsize, void *buffer, int interval)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001163{
1164 struct ehci_ctrl *ctrl = dev->controller;
1165 struct int_queue *result = NULL;
1166 int i;
1167
Hans de Goedebd818d82014-09-24 14:06:04 +02001168 /*
1169 * Interrupt transfers requiring several transactions are not supported
1170 * because bInterval is ignored.
1171 *
1172 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1173 * <= PKT_ALIGN if several qTDs are required, while the USB
1174 * specification does not constrain this for interrupt transfers. That
1175 * means that ehci_submit_async() would support interrupt transfers
1176 * requiring several transactions only as long as the transfer size does
1177 * not require more than a single qTD.
1178 */
1179 if (elementsize > usb_maxpacket(dev, pipe)) {
1180 printf("%s: xfers requiring several transactions are not supported.\n",
1181 __func__);
1182 return NULL;
1183 }
1184
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001185 debug("Enter create_int_queue\n");
1186 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1187 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1188 return NULL;
1189 }
1190
1191 /* limit to 4 full pages worth of data -
1192 * we can safely fit them in a single TD,
1193 * no matter the alignment
1194 */
1195 if (elementsize >= 16384) {
1196 debug("too large elements for interrupt transfers\n");
1197 return NULL;
1198 }
1199
1200 result = malloc(sizeof(*result));
1201 if (!result) {
1202 debug("ehci intr queue: out of memory\n");
1203 goto fail1;
1204 }
Hans de Goede8aa26b82014-09-24 14:06:05 +02001205 result->elementsize = elementsize;
Stephen Warren8165e342014-02-06 13:13:06 -07001206 result->first = memalign(USB_DMA_MINALIGN,
1207 sizeof(struct QH) * queuesize);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001208 if (!result->first) {
1209 debug("ehci intr queue: out of memory\n");
1210 goto fail2;
1211 }
1212 result->current = result->first;
1213 result->last = result->first + queuesize - 1;
Stephen Warren8165e342014-02-06 13:13:06 -07001214 result->tds = memalign(USB_DMA_MINALIGN,
1215 sizeof(struct qTD) * queuesize);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001216 if (!result->tds) {
1217 debug("ehci intr queue: out of memory\n");
1218 goto fail3;
1219 }
1220 memset(result->first, 0, sizeof(struct QH) * queuesize);
1221 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1222
1223 for (i = 0; i < queuesize; i++) {
1224 struct QH *qh = result->first + i;
1225 struct qTD *td = result->tds + i;
1226 void **buf = &qh->buffer;
1227
Rob Herring98ae8402015-03-17 15:46:37 -05001228 qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001229 if (i == queuesize - 1)
Adrian Coxea427772014-04-10 13:29:45 +01001230 qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001231
Rob Herring98ae8402015-03-17 15:46:37 -05001232 qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
Adrian Coxea427772014-04-10 13:29:45 +01001233 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1234 qh->qh_endpt1 =
1235 cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001236 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1237 (1 << 14) |
1238 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1239 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
Adrian Coxea427772014-04-10 13:29:45 +01001240 (usb_pipedevice(pipe) << 0));
1241 qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1242 (1 << 0)); /* S-mask: microframe 0 */
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001243 if (dev->speed == USB_SPEED_LOW ||
1244 dev->speed == USB_SPEED_FULL) {
Hans de Goede4e2c4ad2014-09-20 16:51:22 +02001245 /* C-mask: microframes 2-4 */
1246 qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001247 }
Hans de Goede4e2c4ad2014-09-20 16:51:22 +02001248 ehci_update_endpt2_dev_n_port(dev, qh);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001249
Adrian Coxea427772014-04-10 13:29:45 +01001250 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1251 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001252 debug("communication direction is '%s'\n",
1253 usb_pipein(pipe) ? "in" : "out");
Adrian Coxea427772014-04-10 13:29:45 +01001254 td->qt_token = cpu_to_hc32((elementsize << 16) |
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001255 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
Adrian Coxea427772014-04-10 13:29:45 +01001256 0x80); /* active */
1257 td->qt_buffer[0] =
Rob Herring98ae8402015-03-17 15:46:37 -05001258 cpu_to_hc32((unsigned long)buffer + i * elementsize);
Adrian Coxea427772014-04-10 13:29:45 +01001259 td->qt_buffer[1] =
1260 cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1261 td->qt_buffer[2] =
1262 cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1263 td->qt_buffer[3] =
1264 cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1265 td->qt_buffer[4] =
1266 cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001267
1268 *buf = buffer + i * elementsize;
1269 }
1270
Rob Herring98ae8402015-03-17 15:46:37 -05001271 flush_dcache_range((unsigned long)buffer,
Stephen Warrend3e07472013-05-24 15:03:17 -06001272 ALIGN_END_ADDR(char, buffer,
1273 queuesize * elementsize));
Rob Herring98ae8402015-03-17 15:46:37 -05001274 flush_dcache_range((unsigned long)result->first,
Stephen Warrend3e07472013-05-24 15:03:17 -06001275 ALIGN_END_ADDR(struct QH, result->first,
1276 queuesize));
Rob Herring98ae8402015-03-17 15:46:37 -05001277 flush_dcache_range((unsigned long)result->tds,
Stephen Warrend3e07472013-05-24 15:03:17 -06001278 ALIGN_END_ADDR(struct qTD, result->tds,
1279 queuesize));
1280
Hans de Goede32f2eac2014-09-24 14:06:03 +02001281 if (ctrl->periodic_schedules > 0) {
1282 if (disable_periodic(ctrl) < 0) {
1283 debug("FATAL: periodic should never fail, but did");
1284 goto fail3;
1285 }
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001286 }
1287
1288 /* hook up to periodic list */
1289 struct QH *list = &ctrl->periodic_queue;
1290 result->last->qh_link = list->qh_link;
Rob Herring98ae8402015-03-17 15:46:37 -05001291 list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001292
Rob Herring98ae8402015-03-17 15:46:37 -05001293 flush_dcache_range((unsigned long)result->last,
Stephen Warrend3e07472013-05-24 15:03:17 -06001294 ALIGN_END_ADDR(struct QH, result->last, 1));
Rob Herring98ae8402015-03-17 15:46:37 -05001295 flush_dcache_range((unsigned long)list,
Stephen Warrend3e07472013-05-24 15:03:17 -06001296 ALIGN_END_ADDR(struct QH, list, 1));
1297
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001298 if (enable_periodic(ctrl) < 0) {
1299 debug("FATAL: periodic should never fail, but did");
1300 goto fail3;
1301 }
Hans de Goede36b73102014-09-20 16:51:25 +02001302 ctrl->periodic_schedules++;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001303
1304 debug("Exit create_int_queue\n");
1305 return result;
1306fail3:
1307 if (result->tds)
1308 free(result->tds);
1309fail2:
1310 if (result->first)
1311 free(result->first);
1312 if (result)
1313 free(result);
1314fail1:
1315 return NULL;
1316}
1317
1318void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1319{
1320 struct QH *cur = queue->current;
Hans de Goede415548d2014-09-20 16:51:24 +02001321 struct qTD *cur_td;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001322
1323 /* depleted queue */
1324 if (cur == NULL) {
1325 debug("Exit poll_int_queue with completed queue\n");
1326 return NULL;
1327 }
1328 /* still active */
Hans de Goede415548d2014-09-20 16:51:24 +02001329 cur_td = &queue->tds[queue->current - queue->first];
Rob Herring98ae8402015-03-17 15:46:37 -05001330 invalidate_dcache_range((unsigned long)cur_td,
Hans de Goede415548d2014-09-20 16:51:24 +02001331 ALIGN_END_ADDR(struct qTD, cur_td, 1));
1332 if (QT_TOKEN_GET_STATUS(hc32_to_cpu(cur_td->qt_token)) &
1333 QT_TOKEN_STATUS_ACTIVE) {
1334 debug("Exit poll_int_queue with no completed intr transfer. token is %x\n",
1335 hc32_to_cpu(cur_td->qt_token));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001336 return NULL;
1337 }
1338 if (!(cur->qh_link & QH_LINK_TERMINATE))
1339 queue->current++;
1340 else
1341 queue->current = NULL;
Hans de Goede8aa26b82014-09-24 14:06:05 +02001342
Rob Herring98ae8402015-03-17 15:46:37 -05001343 invalidate_dcache_range((unsigned long)cur->buffer,
Hans de Goede8aa26b82014-09-24 14:06:05 +02001344 ALIGN_END_ADDR(char, cur->buffer,
1345 queue->elementsize));
1346
Hans de Goede415548d2014-09-20 16:51:24 +02001347 debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
1348 hc32_to_cpu(cur_td->qt_token), cur, queue->first);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001349 return cur->buffer;
1350}
1351
1352/* Do not free buffers associated with QHs, they're owned by someone else */
Hans de Goede8460b892014-09-24 14:06:06 +02001353int
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001354destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1355{
1356 struct ehci_ctrl *ctrl = dev->controller;
1357 int result = -1;
1358 unsigned long timeout;
1359
1360 if (disable_periodic(ctrl) < 0) {
1361 debug("FATAL: periodic should never fail, but did");
1362 goto out;
1363 }
Hans de Goede36b73102014-09-20 16:51:25 +02001364 ctrl->periodic_schedules--;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001365
1366 struct QH *cur = &ctrl->periodic_queue;
1367 timeout = get_timer(0) + 500; /* abort after 500ms */
Adrian Coxea427772014-04-10 13:29:45 +01001368 while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001369 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1370 if (NEXT_QH(cur) == queue->first) {
1371 debug("found candidate. removing from chain\n");
1372 cur->qh_link = queue->last->qh_link;
Rob Herring98ae8402015-03-17 15:46:37 -05001373 flush_dcache_range((unsigned long)cur,
Hans de Goedeea7b30c2014-09-20 16:51:23 +02001374 ALIGN_END_ADDR(struct QH, cur, 1));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001375 result = 0;
1376 break;
1377 }
1378 cur = NEXT_QH(cur);
1379 if (get_timer(0) > timeout) {
1380 printf("Timeout destroying interrupt endpoint queue\n");
1381 result = -1;
1382 goto out;
1383 }
1384 }
1385
Hans de Goede36b73102014-09-20 16:51:25 +02001386 if (ctrl->periodic_schedules > 0) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001387 result = enable_periodic(ctrl);
1388 if (result < 0)
1389 debug("FATAL: periodic should never fail, but did");
1390 }
1391
1392out:
1393 free(queue->tds);
1394 free(queue->first);
1395 free(queue);
1396
1397 return result;
1398}
1399
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001400int
1401submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1402 int length, int interval)
1403{
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001404 void *backbuffer;
1405 struct int_queue *queue;
1406 unsigned long timeout;
1407 int result = 0, ret;
1408
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001409 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1410 dev, pipe, buffer, length, interval);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001411
Hans de Goede8bb6c1d2015-01-11 20:38:28 +01001412 queue = create_int_queue(dev, pipe, 1, length, buffer, interval);
Hans de Goedebd818d82014-09-24 14:06:04 +02001413 if (!queue)
1414 return -1;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001415
1416 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1417 while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
1418 if (get_timer(0) > timeout) {
1419 printf("Timeout poll on interrupt endpoint\n");
1420 result = -ETIMEDOUT;
1421 break;
1422 }
1423
1424 if (backbuffer != buffer) {
Rob Herring98ae8402015-03-17 15:46:37 -05001425 debug("got wrong buffer back (%p instead of %p)\n",
1426 backbuffer, buffer);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001427 return -EINVAL;
1428 }
1429
1430 ret = destroy_int_queue(dev, queue);
1431 if (ret < 0)
1432 return ret;
1433
1434 /* everything worked out fine */
1435 return result;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001436}