blob: bd9861dd68e352450c476949bca59253b31e95ec [file] [log] [blame]
Michael Trimarchi241f7512008-11-28 13:20:46 +01001/*-
2 * Copyright (c) 2007-2008, Juniper Networks, Inc.
michael0a326102008-12-10 17:55:19 +01003 * Copyright (c) 2008, Excito Elektronik i Skåne AB
Remy Böhmer33e87482008-12-13 22:51:58 +01004 * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
5 *
Michael Trimarchi241f7512008-11-28 13:20:46 +01006 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2 of
11 * the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
Michael Trimarchi241f7512008-11-28 13:20:46 +010023#include <common.h>
Simon Glassa194b252015-03-25 12:22:29 -060024#include <dm.h>
Patrick Georgie55fdac2013-03-06 14:08:31 +000025#include <errno.h>
michael0a326102008-12-10 17:55:19 +010026#include <asm/byteorder.h>
Lucas Stach835e11e2012-09-06 08:00:13 +020027#include <asm/unaligned.h>
Michael Trimarchi241f7512008-11-28 13:20:46 +010028#include <usb.h>
29#include <asm/io.h>
michael0a326102008-12-10 17:55:19 +010030#include <malloc.h>
Stefan Roese86b34cf2010-11-26 15:43:28 +010031#include <watchdog.h>
Patrick Georgie55fdac2013-03-06 14:08:31 +000032#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020033
34#include "ehci.h"
Michael Trimarchi241f7512008-11-28 13:20:46 +010035
Lucas Stach3494a4c2012-09-26 00:14:35 +020036#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
37#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
38#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +010039
Julius Werner5c1a1ad2013-09-24 10:53:07 -070040/*
41 * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
42 * Let's time out after 8 to have a little safety margin on top of that.
43 */
44#define HCHALT_TIMEOUT (8 * 1000)
45
Simon Glassa194b252015-03-25 12:22:29 -060046#ifndef CONFIG_DM_USB
Marek Vasutfd349a12013-07-10 03:16:31 +020047static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
Simon Glassa194b252015-03-25 12:22:29 -060048#endif
Tom Rini2cabcf72012-07-15 22:14:24 +000049
50#define ALIGN_END_ADDR(type, ptr, size) \
Rob Herringf14d54b2015-03-17 15:46:37 -050051 ((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchi241f7512008-11-28 13:20:46 +010052
michael0a326102008-12-10 17:55:19 +010053static struct descriptor {
54 struct usb_hub_descriptor hub;
55 struct usb_device_descriptor device;
56 struct usb_linux_config_descriptor config;
57 struct usb_linux_interface_descriptor interface;
58 struct usb_endpoint_descriptor endpoint;
59} __attribute__ ((packed)) descriptor = {
60 {
61 0x8, /* bDescLength */
62 0x29, /* bDescriptorType: hub descriptor */
63 2, /* bNrPorts -- runtime modified */
64 0, /* wHubCharacteristics */
Vincent Palatin8277b502011-12-05 14:52:22 -080065 10, /* bPwrOn2PwrGood */
michael0a326102008-12-10 17:55:19 +010066 0, /* bHubCntrCurrent */
67 {}, /* Device removable */
68 {} /* at most 7 ports! XXX */
69 },
70 {
71 0x12, /* bLength */
72 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyovfa30a272010-02-27 21:29:42 +030073 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michael0a326102008-12-10 17:55:19 +010074 9, /* bDeviceClass: UDCLASS_HUB */
75 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
76 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
77 64, /* bMaxPacketSize: 64 bytes */
78 0x0000, /* idVendor */
79 0x0000, /* idProduct */
Sergei Shtylyovfa30a272010-02-27 21:29:42 +030080 cpu_to_le16(0x0100), /* bcdDevice */
michael0a326102008-12-10 17:55:19 +010081 1, /* iManufacturer */
82 2, /* iProduct */
83 0, /* iSerialNumber */
84 1 /* bNumConfigurations: 1 */
85 },
86 {
87 0x9,
88 2, /* bDescriptorType: UDESC_CONFIG */
89 cpu_to_le16(0x19),
90 1, /* bNumInterface */
91 1, /* bConfigurationValue */
92 0, /* iConfiguration */
93 0x40, /* bmAttributes: UC_SELF_POWER */
94 0 /* bMaxPower */
95 },
96 {
97 0x9, /* bLength */
98 4, /* bDescriptorType: UDESC_INTERFACE */
99 0, /* bInterfaceNumber */
100 0, /* bAlternateSetting */
101 1, /* bNumEndpoints */
102 9, /* bInterfaceClass: UICLASS_HUB */
103 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
104 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
105 0 /* iInterface */
106 },
107 {
108 0x7, /* bLength */
109 5, /* bDescriptorType: UDESC_ENDPOINT */
110 0x81, /* bEndpointAddress:
111 * UE_DIR_IN | EHCI_INTR_ENDPT
112 */
113 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix83b9e1d2009-10-31 12:37:38 -0500114 8, /* wMaxPacketSize */
michael0a326102008-12-10 17:55:19 +0100115 255 /* bInterval */
116 },
Michael Trimarchi241f7512008-11-28 13:20:46 +0100117};
118
Remy Böhmer33e87482008-12-13 22:51:58 +0100119#if defined(CONFIG_EHCI_IS_TDI)
120#define ehci_is_TDI() (1)
121#else
122#define ehci_is_TDI() (0)
123#endif
124
Simon Glasscb7cf602015-03-25 12:22:25 -0600125static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev)
126{
Simon Glassa194b252015-03-25 12:22:29 -0600127#ifdef CONFIG_DM_USB
128 struct udevice *dev;
129
130 /* Find the USB controller */
131 for (dev = udev->dev;
132 device_get_uclass_id(dev) != UCLASS_USB;
133 dev = dev->parent)
134 ;
135 return dev_get_priv(dev);
136#else
Simon Glasscb7cf602015-03-25 12:22:25 -0600137 return udev->controller;
Simon Glassa194b252015-03-25 12:22:29 -0600138#endif
Simon Glasscb7cf602015-03-25 12:22:25 -0600139}
140
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600141static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
Jim Lin54f3dfe2013-03-27 00:52:32 +0000142{
143 return PORTSC_PSPD(reg);
144}
145
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600146static void ehci_set_usbmode(struct ehci_ctrl *ctrl)
Jim Lin54f3dfe2013-03-27 00:52:32 +0000147{
148 uint32_t tmp;
149 uint32_t *reg_ptr;
150
Simon Glass2d387ab2015-03-25 12:22:23 -0600151 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE);
Jim Lin54f3dfe2013-03-27 00:52:32 +0000152 tmp = ehci_readl(reg_ptr);
153 tmp |= USBMODE_CM_HC;
154#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
155 tmp |= USBMODE_BE;
156#endif
157 ehci_writel(reg_ptr, tmp);
158}
159
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600160static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
Simon Glasscc0dc6b2015-03-25 12:22:21 -0600161 uint32_t *reg)
Marek Vasut09734772011-07-11 02:37:01 +0200162{
163 mdelay(50);
164}
165
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600166static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
Simon Glass0bec1282015-03-25 12:22:17 -0600167{
168 if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
169 /* Printing the message would cause a scan failure! */
170 debug("The request port(%u) is not configured\n", port);
171 return NULL;
172 }
173
Simon Glassdfbf1862015-03-25 12:22:24 -0600174 return (uint32_t *)&ctrl->hcor->or_portsc[port];
Simon Glass0bec1282015-03-25 12:22:17 -0600175}
176
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100177static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michael0a326102008-12-10 17:55:19 +0100178{
michael0bf2a032008-12-11 13:43:55 +0100179 uint32_t result;
180 do {
181 result = ehci_readl(ptr);
Wolfgang Denkcdc5a7a2010-10-22 14:23:00 +0200182 udelay(5);
michael0bf2a032008-12-11 13:43:55 +0100183 if (result == ~(uint32_t)0)
184 return -1;
185 result &= mask;
186 if (result == done)
187 return 0;
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100188 usec--;
189 } while (usec > 0);
michael0bf2a032008-12-11 13:43:55 +0100190 return -1;
191}
192
Simon Glass302696b2015-03-25 12:22:28 -0600193static int ehci_reset(struct ehci_ctrl *ctrl)
michael0bf2a032008-12-11 13:43:55 +0100194{
195 uint32_t cmd;
michael0bf2a032008-12-11 13:43:55 +0100196 int ret = 0;
197
Simon Glass302696b2015-03-25 12:22:28 -0600198 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Stefan Roese745af442010-11-26 15:44:00 +0100199 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Simon Glass302696b2015-03-25 12:22:28 -0600200 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
201 ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
Lucas Stach3494a4c2012-09-26 00:14:35 +0200202 CMD_RESET, 0, 250 * 1000);
michael0bf2a032008-12-11 13:43:55 +0100203 if (ret < 0) {
204 printf("EHCI fail to reset\n");
205 goto out;
206 }
207
Jim Lin54f3dfe2013-03-27 00:52:32 +0000208 if (ehci_is_TDI())
Simon Glass302696b2015-03-25 12:22:28 -0600209 ctrl->ops.set_usb_mode(ctrl);
Simon Glass5978cdb2012-02-27 10:52:47 +0000210
211#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Simon Glass302696b2015-03-25 12:22:28 -0600212 cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200213 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass5978cdb2012-02-27 10:52:47 +0000214 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Simon Glass302696b2015-03-25 12:22:28 -0600215 ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
Simon Glass5978cdb2012-02-27 10:52:47 +0000216#endif
michael0bf2a032008-12-11 13:43:55 +0100217out:
218 return ret;
michael0a326102008-12-10 17:55:19 +0100219}
Michael Trimarchi241f7512008-11-28 13:20:46 +0100220
Julius Werner5c1a1ad2013-09-24 10:53:07 -0700221static int ehci_shutdown(struct ehci_ctrl *ctrl)
222{
223 int i, ret = 0;
224 uint32_t cmd, reg;
225
Marek Vasut919d00a2013-12-14 02:03:11 +0100226 if (!ctrl || !ctrl->hcor)
227 return -EINVAL;
228
Julius Werner5c1a1ad2013-09-24 10:53:07 -0700229 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
230 cmd &= ~(CMD_PSE | CMD_ASE);
231 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
232 ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
233 100 * 1000);
234
235 if (!ret) {
236 for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
237 reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
238 reg |= EHCI_PS_SUSP;
239 ehci_writel(&ctrl->hcor->or_portsc[i], reg);
240 }
241
242 cmd &= ~CMD_RUN;
243 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
244 ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
245 HCHALT_TIMEOUT);
246 }
247
248 if (ret)
249 puts("EHCI failed to shut down host controller.\n");
250
251 return ret;
252}
253
Michael Trimarchi241f7512008-11-28 13:20:46 +0100254static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
255{
Marek Vasutff24dc32012-04-09 04:07:46 +0200256 uint32_t delta, next;
Rob Herringf14d54b2015-03-17 15:46:37 -0500257 uint32_t addr = (unsigned long)buf;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100258 int idx;
259
Ilya Yanokfb113712012-07-15 04:43:49 +0000260 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutff24dc32012-04-09 04:07:46 +0200261 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
262
Ilya Yanokfb113712012-07-15 04:43:49 +0000263 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
264
Michael Trimarchi241f7512008-11-28 13:20:46 +0100265 idx = 0;
Benoît Thébaudeaue68f48a2012-07-19 22:16:38 +0200266 while (idx < QT_BUFFER_CNT) {
michael0a326102008-12-10 17:55:19 +0100267 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denkebb829f2010-10-19 16:13:15 +0200268 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200269 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100270 delta = next - addr;
271 if (delta >= sz)
272 break;
273 sz -= delta;
274 addr = next;
275 idx++;
276 }
277
Benoît Thébaudeaue68f48a2012-07-19 22:16:38 +0200278 if (idx == QT_BUFFER_CNT) {
Rob Herringf14d54b2015-03-17 15:46:37 -0500279 printf("out of buffer pointers (%zu bytes left)\n", sz);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100280 return -1;
281 }
282
283 return 0;
284}
285
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000286static inline u8 ehci_encode_speed(enum usb_device_speed speed)
287{
288 #define QH_HIGH_SPEED 2
289 #define QH_FULL_SPEED 0
290 #define QH_LOW_SPEED 1
291 if (speed == USB_SPEED_HIGH)
292 return QH_HIGH_SPEED;
293 if (speed == USB_SPEED_LOW)
294 return QH_LOW_SPEED;
295 return QH_FULL_SPEED;
296}
297
Simon Glassa194b252015-03-25 12:22:29 -0600298static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
Hans de Goededa166772014-09-20 16:51:22 +0200299 struct QH *qh)
300{
301 struct usb_device *ttdev;
Simon Glassa194b252015-03-25 12:22:29 -0600302 int parent_devnum;
Hans de Goededa166772014-09-20 16:51:22 +0200303
Simon Glassa194b252015-03-25 12:22:29 -0600304 if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
Hans de Goededa166772014-09-20 16:51:22 +0200305 return;
306
307 /*
308 * For full / low speed devices we need to get the devnum and portnr of
309 * the tt, so of the first upstream usb-2 hub, there may be usb-1 hubs
310 * in the tree before that one!
311 */
Simon Glassa194b252015-03-25 12:22:29 -0600312#ifdef CONFIG_DM_USB
313 struct udevice *parent;
314
315 for (ttdev = udev; ; ) {
316 struct udevice *dev = ttdev->dev;
317
318 if (dev->parent &&
319 device_get_uclass_id(dev->parent) == UCLASS_USB_HUB)
320 parent = dev->parent;
321 else
322 parent = NULL;
323 if (!parent)
324 return;
325 ttdev = dev_get_parentdata(parent);
326 if (!ttdev->speed != USB_SPEED_HIGH)
327 break;
328 }
329 parent_devnum = ttdev->devnum;
330#else
331 ttdev = udev;
Hans de Goededa166772014-09-20 16:51:22 +0200332 while (ttdev->parent && ttdev->parent->speed != USB_SPEED_HIGH)
333 ttdev = ttdev->parent;
334 if (!ttdev->parent)
335 return;
Simon Glassa194b252015-03-25 12:22:29 -0600336 parent_devnum = ttdev->parent->devnum;
337#endif
Hans de Goededa166772014-09-20 16:51:22 +0200338
339 qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(ttdev->portnr) |
Simon Glassa194b252015-03-25 12:22:29 -0600340 QH_ENDPT2_HUBADDR(parent_devnum));
Hans de Goededa166772014-09-20 16:51:22 +0200341}
342
Michael Trimarchi241f7512008-11-28 13:20:46 +0100343static int
344ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
345 int length, struct devrequest *req)
346{
Tom Rini2cabcf72012-07-15 22:14:24 +0000347 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200348 struct qTD *qtd;
349 int qtd_count = 0;
Marek Vasut4f668312012-04-08 23:32:05 +0200350 int qtd_counter = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100351 volatile struct qTD *vtd;
352 unsigned long ts;
353 uint32_t *tdp;
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200354 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100355 uint32_t c, toggle;
michael0a326102008-12-10 17:55:19 +0100356 uint32_t cmd;
Simon Glassfd7f5132011-02-07 14:42:16 -0800357 int timeout;
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100358 int ret = 0;
Simon Glasscb7cf602015-03-25 12:22:25 -0600359 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100360
michael0a326102008-12-10 17:55:19 +0100361 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchi241f7512008-11-28 13:20:46 +0100362 buffer, length, req);
363 if (req != NULL)
michael0a326102008-12-10 17:55:19 +0100364 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100365 req->request, req->request,
366 req->requesttype, req->requesttype,
367 le16_to_cpu(req->value), le16_to_cpu(req->value),
michael0a326102008-12-10 17:55:19 +0100368 le16_to_cpu(req->index));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100369
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200370#define PKT_ALIGN 512
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200371 /*
372 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
373 * described by a transfer descriptor (the qTD). The qTDs form a linked
374 * list with a queue head (QH).
375 *
376 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
377 * have its beginning in a qTD transfer and its end in the following
378 * one, so the qTD transfer lengths have to be chosen accordingly.
379 *
380 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
381 * single pages. The first data buffer can start at any offset within a
382 * page (not considering the cache-line alignment issues), while the
383 * following buffers must be page-aligned. There is no alignment
384 * constraint on the size of a qTD transfer.
385 */
386 if (req != NULL)
387 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
388 qtd_count += 1 + 1;
389 if (length > 0 || req == NULL) {
390 /*
391 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200392 * data payload (not considering the first qTD transfer, which
393 * may be longer or shorter, and the final one, which may be
394 * shorter).
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200395 *
396 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200397 * transfer size is aligned to PKT_ALIGN, which is a multiple of
398 * wMaxPacketSize (except in some cases for interrupt transfers,
399 * see comment in submit_int_msg()).
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200400 *
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200401 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200402 * QT_BUFFER_CNT full pages will be used.
403 */
404 int xfr_sz = QT_BUFFER_CNT;
405 /*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200406 * However, if the input buffer is not aligned to PKT_ALIGN, the
407 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200408 * data buffer of each transfer will be page-unaligned.
409 */
Rob Herringf14d54b2015-03-17 15:46:37 -0500410 if ((unsigned long)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200411 xfr_sz--;
412 /* Convert the qTD transfer size to bytes. */
413 xfr_sz *= EHCI_PAGE_SIZE;
414 /*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200415 * Approximate by excess the number of qTDs that will be
416 * required for the data payload. The exact formula is way more
417 * complicated and saves at most 2 qTDs, i.e. a total of 128
418 * bytes.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200419 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200420 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200421 }
422/*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200423 * Threshold value based on the worst-case total size of the allocated qTDs for
424 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200425 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200426#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200427#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
428#endif
429 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
430 if (qtd == NULL) {
431 printf("unable to allocate TDs\n");
432 return -1;
433 }
434
Tom Rini2cabcf72012-07-15 22:14:24 +0000435 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200436 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasut4f668312012-04-08 23:32:05 +0200437
Marek Vasutff24dc32012-04-09 04:07:46 +0200438 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
439
Marek Vasut285c8b32012-04-09 04:13:00 +0200440 /*
441 * Setup QH (3.6 in ehci-r10.pdf)
442 *
443 * qh_link ................. 03-00 H
444 * qh_endpt1 ............... 07-04 H
445 * qh_endpt2 ............... 0B-08 H
446 * - qh_curtd
447 * qh_overlay.qt_next ...... 13-10 H
448 * - qh_overlay.qt_altnext
449 */
Rob Herringf14d54b2015-03-17 15:46:37 -0500450 qh->qh_link = cpu_to_hc32((unsigned long)&ctrl->qh_list | QH_LINK_TYPE_QH);
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000451 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200452 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200453 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200454 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200455 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000456 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200457 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
458 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini2cabcf72012-07-15 22:14:24 +0000459 qh->qh_endpt1 = cpu_to_hc32(endpt);
Hans de Goededa166772014-09-20 16:51:22 +0200460 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini2cabcf72012-07-15 22:14:24 +0000461 qh->qh_endpt2 = cpu_to_hc32(endpt);
Hans de Goededa166772014-09-20 16:51:22 +0200462 ehci_update_endpt2_dev_n_port(dev, qh);
Tom Rini2cabcf72012-07-15 22:14:24 +0000463 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Stephen Warren1907e5a2014-02-07 09:53:50 -0700464 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100465
Tom Rini2cabcf72012-07-15 22:14:24 +0000466 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100467
Michael Trimarchi241f7512008-11-28 13:20:46 +0100468 if (req != NULL) {
Marek Vasut285c8b32012-04-09 04:13:00 +0200469 /*
470 * Setup request qTD (3.5 in ehci-r10.pdf)
471 *
472 * qt_next ................ 03-00 H
473 * qt_altnext ............. 07-04 H
474 * qt_token ............... 0B-08 H
475 *
476 * [ buffer, buffer_hi ] loaded with "req".
477 */
Marek Vasut4f668312012-04-08 23:32:05 +0200478 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
479 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200480 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
481 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
482 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
483 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasut4f668312012-04-08 23:32:05 +0200484 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200485 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
486 printf("unable to construct SETUP TD\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100487 goto fail;
488 }
Marek Vasut285c8b32012-04-09 04:13:00 +0200489 /* Update previous qTD! */
Rob Herringf14d54b2015-03-17 15:46:37 -0500490 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Marek Vasut4f668312012-04-08 23:32:05 +0200491 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100492 toggle = 1;
493 }
494
495 if (length > 0 || req == NULL) {
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200496 uint8_t *buf_ptr = buffer;
497 int left_length = length;
498
499 do {
500 /*
501 * Determine the size of this qTD transfer. By default,
502 * QT_BUFFER_CNT full pages can be used.
503 */
504 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
505 /*
506 * However, if the input buffer is not page-aligned, the
507 * portion of the first page before the buffer start
508 * offset within that page is unusable.
509 */
Rob Herringf14d54b2015-03-17 15:46:37 -0500510 xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200511 /*
512 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200513 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200514 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200515 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200516 /*
517 * This transfer may be shorter than the available qTD
518 * transfer size that has just been computed.
519 */
520 xfr_bytes = min(xfr_bytes, left_length);
521
522 /*
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 *
529 * [ buffer, buffer_hi ] loaded with "buffer".
530 */
531 qtd[qtd_counter].qt_next =
532 cpu_to_hc32(QT_NEXT_TERMINATE);
533 qtd[qtd_counter].qt_altnext =
534 cpu_to_hc32(QT_NEXT_TERMINATE);
535 token = QT_TOKEN_DT(toggle) |
536 QT_TOKEN_TOTALBYTES(xfr_bytes) |
537 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
538 QT_TOKEN_CERR(3) |
539 QT_TOKEN_PID(usb_pipein(pipe) ?
540 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
541 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
542 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
543 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
544 xfr_bytes)) {
545 printf("unable to construct DATA TD\n");
546 goto fail;
547 }
548 /* Update previous qTD! */
Rob Herringf14d54b2015-03-17 15:46:37 -0500549 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200550 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200551 /*
552 * Data toggle has to be adjusted since the qTD transfer
553 * size is not always an even multiple of
554 * wMaxPacketSize.
555 */
556 if ((xfr_bytes / maxpacket) & 1)
557 toggle ^= 1;
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200558 buf_ptr += xfr_bytes;
559 left_length -= xfr_bytes;
560 } while (left_length > 0);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100561 }
562
563 if (req != NULL) {
Marek Vasut285c8b32012-04-09 04:13:00 +0200564 /*
565 * Setup request qTD (3.5 in ehci-r10.pdf)
566 *
567 * qt_next ................ 03-00 H
568 * qt_altnext ............. 07-04 H
569 * qt_token ............... 0B-08 H
570 */
Marek Vasut4f668312012-04-08 23:32:05 +0200571 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
572 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200573 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200574 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
575 QT_TOKEN_PID(usb_pipein(pipe) ?
576 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
577 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasut4f668312012-04-08 23:32:05 +0200578 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut285c8b32012-04-09 04:13:00 +0200579 /* Update previous qTD! */
Rob Herringf14d54b2015-03-17 15:46:37 -0500580 *tdp = cpu_to_hc32((unsigned long)&qtd[qtd_counter]);
Marek Vasut4f668312012-04-08 23:32:05 +0200581 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100582 }
583
Rob Herringf14d54b2015-03-17 15:46:37 -0500584 ctrl->qh_list.qh_link = cpu_to_hc32((unsigned long)qh | QH_LINK_TYPE_QH);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100585
Stefan Roese25983c12009-01-21 17:12:19 +0100586 /* Flush dcache */
Rob Herringf14d54b2015-03-17 15:46:37 -0500587 flush_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach3494a4c2012-09-26 00:14:35 +0200588 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herringf14d54b2015-03-17 15:46:37 -0500589 flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
590 flush_dcache_range((unsigned long)qtd,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200591 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roese25983c12009-01-21 17:12:19 +0100592
Ilya Yanok84309bb2012-07-15 22:12:08 +0000593 /* Set async. queue head pointer. */
Rob Herringf14d54b2015-03-17 15:46:37 -0500594 ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)&ctrl->qh_list);
Ilya Yanok84309bb2012-07-15 22:12:08 +0000595
Lucas Stach3494a4c2012-09-26 00:14:35 +0200596 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
597 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100598
599 /* Enable async. schedule. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200600 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael0bf2a032008-12-11 13:43:55 +0100601 cmd |= CMD_ASE;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200602 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0a326102008-12-10 17:55:19 +0100603
Lucas Stach3494a4c2012-09-26 00:14:35 +0200604 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100605 100 * 1000);
606 if (ret < 0) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200607 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100608 goto fail;
michael0bf2a032008-12-11 13:43:55 +0100609 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100610
611 /* Wait for TDs to be processed. */
612 ts = get_timer(0);
Marek Vasut4f668312012-04-08 23:32:05 +0200613 vtd = &qtd[qtd_counter - 1];
Simon Glassfd7f5132011-02-07 14:42:16 -0800614 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100615 do {
Stefan Roese25983c12009-01-21 17:12:19 +0100616 /* Invalidate dcache */
Rob Herringf14d54b2015-03-17 15:46:37 -0500617 invalidate_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach3494a4c2012-09-26 00:14:35 +0200618 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herringf14d54b2015-03-17 15:46:37 -0500619 invalidate_dcache_range((unsigned long)qh,
Tom Rini2cabcf72012-07-15 22:14:24 +0000620 ALIGN_END_ADDR(struct QH, qh, 1));
Rob Herringf14d54b2015-03-17 15:46:37 -0500621 invalidate_dcache_range((unsigned long)qtd,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200622 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutff24dc32012-04-09 04:07:46 +0200623
michael0a326102008-12-10 17:55:19 +0100624 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200625 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100626 break;
Stefan Roese86b34cf2010-11-26 15:43:28 +0100627 WATCHDOG_RESET();
Simon Glassfd7f5132011-02-07 14:42:16 -0800628 } while (get_timer(ts) < timeout);
629
Ilya Yanokfb113712012-07-15 04:43:49 +0000630 /*
631 * Invalidate the memory area occupied by buffer
632 * Don't try to fix the buffer alignment, if it isn't properly
633 * aligned it's upper layer's fault so let invalidate_dcache_range()
634 * vow about it. But we have to fix the length as it's actual
635 * transfer length and can be unaligned. This is potentially
636 * dangerous operation, it's responsibility of the calling
637 * code to make sure enough space is reserved.
638 */
Rob Herringf14d54b2015-03-17 15:46:37 -0500639 invalidate_dcache_range((unsigned long)buffer,
640 ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutff24dc32012-04-09 04:07:46 +0200641
Simon Glassfd7f5132011-02-07 14:42:16 -0800642 /* Check that the TD processing happened */
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200643 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glassfd7f5132011-02-07 14:42:16 -0800644 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100645
646 /* Disable async schedule. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200647 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael0a326102008-12-10 17:55:19 +0100648 cmd &= ~CMD_ASE;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200649 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0bf2a032008-12-11 13:43:55 +0100650
Lucas Stach3494a4c2012-09-26 00:14:35 +0200651 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100652 100 * 1000);
653 if (ret < 0) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200654 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100655 goto fail;
michael0bf2a032008-12-11 13:43:55 +0100656 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100657
Tom Rini2cabcf72012-07-15 22:14:24 +0000658 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200659 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michael0a326102008-12-10 17:55:19 +0100660 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200661 switch (QT_TOKEN_GET_STATUS(token) &
662 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100663 case 0:
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200664 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100665 usb_settoggle(dev, usb_pipeendpoint(pipe),
666 usb_pipeout(pipe), toggle);
667 dev->status = 0;
668 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200669 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100670 dev->status = USB_ST_STALLED;
671 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200672 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
673 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100674 dev->status = USB_ST_BUF_ERR;
675 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200676 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
677 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100678 dev->status = USB_ST_BABBLE_DET;
679 break;
680 default:
681 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200682 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschine1e09312010-11-02 11:47:29 +0100683 dev->status |= USB_ST_STALLED;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100684 break;
685 }
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200686 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100687 } else {
688 dev->act_len = 0;
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800689#ifndef CONFIG_USB_EHCI_FARADAY
michael0a326102008-12-10 17:55:19 +0100690 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach3494a4c2012-09-26 00:14:35 +0200691 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
692 ehci_readl(&ctrl->hcor->or_portsc[0]),
693 ehci_readl(&ctrl->hcor->or_portsc[1]));
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800694#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +0100695 }
696
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200697 free(qtd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100698 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
699
700fail:
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200701 free(qtd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100702 return -1;
703}
704
Simon Glasscb7cf602015-03-25 12:22:25 -0600705static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
706 void *buffer, int length, struct devrequest *req)
Michael Trimarchi241f7512008-11-28 13:20:46 +0100707{
708 uint8_t tmpbuf[4];
709 u16 typeReq;
michael0a326102008-12-10 17:55:19 +0100710 void *srcptr = NULL;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100711 int len, srclen;
712 uint32_t reg;
Remy Böhmer33e87482008-12-13 22:51:58 +0100713 uint32_t *status_reg;
Julius Wernerd4046702013-02-28 18:08:40 +0000714 int port = le16_to_cpu(req->index) & 0xff;
Simon Glasscb7cf602015-03-25 12:22:25 -0600715 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100716
717 srclen = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100718
michael0a326102008-12-10 17:55:19 +0100719 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100720 req->request, req->request,
721 req->requesttype, req->requesttype,
722 le16_to_cpu(req->value), le16_to_cpu(req->index));
723
Prafulla Wadaskar22810292009-07-17 19:56:30 +0530724 typeReq = req->request | req->requesttype << 8;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100725
Prafulla Wadaskar22810292009-07-17 19:56:30 +0530726 switch (typeReq) {
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800727 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
728 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
729 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600730 status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
Kuo-Jung Su6a656df2013-05-15 15:29:21 +0800731 if (!status_reg)
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800732 return -1;
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800733 break;
734 default:
735 status_reg = NULL;
736 break;
737 }
738
739 switch (typeReq) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100740 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
741 switch (le16_to_cpu(req->value) >> 8) {
742 case USB_DT_DEVICE:
michael0a326102008-12-10 17:55:19 +0100743 debug("USB_DT_DEVICE request\n");
744 srcptr = &descriptor.device;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200745 srclen = descriptor.device.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100746 break;
747 case USB_DT_CONFIG:
michael0a326102008-12-10 17:55:19 +0100748 debug("USB_DT_CONFIG config\n");
749 srcptr = &descriptor.config;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200750 srclen = descriptor.config.bLength +
751 descriptor.interface.bLength +
752 descriptor.endpoint.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100753 break;
754 case USB_DT_STRING:
michael0a326102008-12-10 17:55:19 +0100755 debug("USB_DT_STRING config\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100756 switch (le16_to_cpu(req->value) & 0xff) {
757 case 0: /* Language */
758 srcptr = "\4\3\1\0";
759 srclen = 4;
760 break;
761 case 1: /* Vendor */
762 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
763 srclen = 14;
764 break;
765 case 2: /* Product */
766 srcptr = "\52\3E\0H\0C\0I\0 "
767 "\0H\0o\0s\0t\0 "
768 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
769 srclen = 42;
770 break;
771 default:
michael0a326102008-12-10 17:55:19 +0100772 debug("unknown value DT_STRING %x\n",
773 le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100774 goto unknown;
775 }
776 break;
777 default:
michael0a326102008-12-10 17:55:19 +0100778 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100779 goto unknown;
780 }
781 break;
782 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
783 switch (le16_to_cpu(req->value) >> 8) {
784 case USB_DT_HUB:
michael0a326102008-12-10 17:55:19 +0100785 debug("USB_DT_HUB config\n");
786 srcptr = &descriptor.hub;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200787 srclen = descriptor.hub.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100788 break;
789 default:
michael0a326102008-12-10 17:55:19 +0100790 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100791 goto unknown;
792 }
793 break;
794 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michael0a326102008-12-10 17:55:19 +0100795 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach3494a4c2012-09-26 00:14:35 +0200796 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100797 break;
798 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michael0a326102008-12-10 17:55:19 +0100799 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100800 /* Nothing to do */
801 break;
802 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
803 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
804 tmpbuf[1] = 0;
805 srcptr = tmpbuf;
806 srclen = 2;
807 break;
michael0a326102008-12-10 17:55:19 +0100808 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchi241f7512008-11-28 13:20:46 +0100809 memset(tmpbuf, 0, 4);
Remy Böhmer33e87482008-12-13 22:51:58 +0100810 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100811 if (reg & EHCI_PS_CS)
812 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
813 if (reg & EHCI_PS_PE)
814 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
815 if (reg & EHCI_PS_SUSP)
816 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
817 if (reg & EHCI_PS_OCA)
818 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300819 if (reg & EHCI_PS_PR)
820 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100821 if (reg & EHCI_PS_PP)
822 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese497f1842009-01-21 17:12:01 +0100823
824 if (ehci_is_TDI()) {
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600825 switch (ctrl->ops.get_port_speed(ctrl, reg)) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200826 case PORTSC_PSPD_FS:
Stefan Roese497f1842009-01-21 17:12:01 +0100827 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200828 case PORTSC_PSPD_LS:
Stefan Roese497f1842009-01-21 17:12:01 +0100829 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
830 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200831 case PORTSC_PSPD_HS:
Stefan Roese497f1842009-01-21 17:12:01 +0100832 default:
833 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
834 break;
835 }
836 } else {
837 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
838 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100839
840 if (reg & EHCI_PS_CSC)
841 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
842 if (reg & EHCI_PS_PEC)
843 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
844 if (reg & EHCI_PS_OCC)
845 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Julius Wernerd4046702013-02-28 18:08:40 +0000846 if (ctrl->portreset & (1 << port))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100847 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmer33e87482008-12-13 22:51:58 +0100848
Michael Trimarchi241f7512008-11-28 13:20:46 +0100849 srcptr = tmpbuf;
850 srclen = 4;
851 break;
michael0a326102008-12-10 17:55:19 +0100852 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmer33e87482008-12-13 22:51:58 +0100853 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100854 reg &= ~EHCI_PS_CLEAR;
855 switch (le16_to_cpu(req->value)) {
michael0bf2a032008-12-11 13:43:55 +0100856 case USB_PORT_FEAT_ENABLE:
857 reg |= EHCI_PS_PE;
Remy Böhmer33e87482008-12-13 22:51:58 +0100858 ehci_writel(status_reg, reg);
michael0bf2a032008-12-11 13:43:55 +0100859 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100860 case USB_PORT_FEAT_POWER:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200861 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmer33e87482008-12-13 22:51:58 +0100862 reg |= EHCI_PS_PP;
863 ehci_writel(status_reg, reg);
864 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100865 break;
866 case USB_PORT_FEAT_RESET:
Remy Böhmer33e87482008-12-13 22:51:58 +0100867 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
868 !ehci_is_TDI() &&
869 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100870 /* Low speed device, give up ownership. */
Remy Böhmer33e87482008-12-13 22:51:58 +0100871 debug("port %d low speed --> companion\n",
Julius Wernerd4046702013-02-28 18:08:40 +0000872 port - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100873 reg |= EHCI_PS_PO;
Remy Böhmer33e87482008-12-13 22:51:58 +0100874 ehci_writel(status_reg, reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100875 break;
Remy Böhmer33e87482008-12-13 22:51:58 +0100876 } else {
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300877 int ret;
878
Remy Böhmer33e87482008-12-13 22:51:58 +0100879 reg |= EHCI_PS_PR;
880 reg &= ~EHCI_PS_PE;
881 ehci_writel(status_reg, reg);
882 /*
883 * caller must wait, then call GetPortStatus
884 * usb 2.0 specification say 50 ms resets on
885 * root
886 */
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600887 ctrl->ops.powerup_fixup(ctrl, status_reg, &reg);
Marek Vasut09734772011-07-11 02:37:01 +0200888
Chris Zhangfddf6d62010-01-06 13:34:04 -0800889 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300890 /*
891 * A host controller must terminate the reset
892 * and stabilize the state of the port within
893 * 2 milliseconds
894 */
895 ret = handshake(status_reg, EHCI_PS_PR, 0,
896 2 * 1000);
897 if (!ret)
Julius Wernerd4046702013-02-28 18:08:40 +0000898 ctrl->portreset |= 1 << port;
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300899 else
900 printf("port(%d) reset error\n",
Julius Wernerd4046702013-02-28 18:08:40 +0000901 port - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100902 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100903 break;
Julius Wernerd4046702013-02-28 18:08:40 +0000904 case USB_PORT_FEAT_TEST:
Julius Werner5c1a1ad2013-09-24 10:53:07 -0700905 ehci_shutdown(ctrl);
Julius Wernerd4046702013-02-28 18:08:40 +0000906 reg &= ~(0xf << 16);
907 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
908 ehci_writel(status_reg, reg);
909 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100910 default:
michael0a326102008-12-10 17:55:19 +0100911 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100912 goto unknown;
913 }
Remy Böhmer33e87482008-12-13 22:51:58 +0100914 /* unblock posted writes */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200915 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100916 break;
michael0a326102008-12-10 17:55:19 +0100917 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmer33e87482008-12-13 22:51:58 +0100918 reg = ehci_readl(status_reg);
Simon Glass0554ba52013-05-10 19:49:00 -0700919 reg &= ~EHCI_PS_CLEAR;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100920 switch (le16_to_cpu(req->value)) {
921 case USB_PORT_FEAT_ENABLE:
922 reg &= ~EHCI_PS_PE;
923 break;
Remy Böhmer33e87482008-12-13 22:51:58 +0100924 case USB_PORT_FEAT_C_ENABLE:
Simon Glass0554ba52013-05-10 19:49:00 -0700925 reg |= EHCI_PS_PE;
Remy Böhmer33e87482008-12-13 22:51:58 +0100926 break;
927 case USB_PORT_FEAT_POWER:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200928 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Simon Glass0554ba52013-05-10 19:49:00 -0700929 reg &= ~EHCI_PS_PP;
930 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100931 case USB_PORT_FEAT_C_CONNECTION:
Simon Glass0554ba52013-05-10 19:49:00 -0700932 reg |= EHCI_PS_CSC;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100933 break;
michael0bf2a032008-12-11 13:43:55 +0100934 case USB_PORT_FEAT_OVER_CURRENT:
Simon Glass0554ba52013-05-10 19:49:00 -0700935 reg |= EHCI_PS_OCC;
michael0bf2a032008-12-11 13:43:55 +0100936 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100937 case USB_PORT_FEAT_C_RESET:
Julius Wernerd4046702013-02-28 18:08:40 +0000938 ctrl->portreset &= ~(1 << port);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100939 break;
940 default:
michael0a326102008-12-10 17:55:19 +0100941 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100942 goto unknown;
943 }
Remy Böhmer33e87482008-12-13 22:51:58 +0100944 ehci_writel(status_reg, reg);
945 /* unblock posted write */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200946 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100947 break;
948 default:
michael0a326102008-12-10 17:55:19 +0100949 debug("Unknown request\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100950 goto unknown;
951 }
952
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000953 mdelay(1);
Masahiro Yamadadb204642014-11-07 03:03:31 +0900954 len = min3(srclen, (int)le16_to_cpu(req->length), length);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100955 if (srcptr != NULL && len > 0)
956 memcpy(buffer, srcptr, len);
michael0a326102008-12-10 17:55:19 +0100957 else
958 debug("Len is 0\n");
959
Michael Trimarchi241f7512008-11-28 13:20:46 +0100960 dev->act_len = len;
961 dev->status = 0;
962 return 0;
963
964unknown:
michael0a326102008-12-10 17:55:19 +0100965 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100966 req->requesttype, req->request, le16_to_cpu(req->value),
967 le16_to_cpu(req->index), le16_to_cpu(req->length));
968
969 dev->act_len = 0;
970 dev->status = USB_ST_STALLED;
971 return -1;
972}
973
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600974const struct ehci_ops default_ehci_ops = {
975 .set_usb_mode = ehci_set_usbmode,
976 .get_port_speed = ehci_get_port_speed,
977 .powerup_fixup = ehci_powerup_fixup,
978 .get_portsc_register = ehci_get_portsc_register,
979};
980
981static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
Simon Glass0851caa2015-03-25 12:22:19 -0600982{
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600983 if (!ops) {
984 ctrl->ops = default_ehci_ops;
985 } else {
986 ctrl->ops = *ops;
987 if (!ctrl->ops.set_usb_mode)
988 ctrl->ops.set_usb_mode = ehci_set_usbmode;
989 if (!ctrl->ops.get_port_speed)
990 ctrl->ops.get_port_speed = ehci_get_port_speed;
991 if (!ctrl->ops.powerup_fixup)
992 ctrl->ops.powerup_fixup = ehci_powerup_fixup;
993 if (!ctrl->ops.get_portsc_register)
994 ctrl->ops.get_portsc_register =
995 ehci_get_portsc_register;
996 }
Simon Glass0851caa2015-03-25 12:22:19 -0600997}
998
Simon Glassa194b252015-03-25 12:22:29 -0600999#ifndef CONFIG_DM_USB
Simon Glassdc9f3ed2015-03-25 12:22:27 -06001000void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
1001{
1002 struct ehci_ctrl *ctrl = &ehcic[index];
1003
1004 ctrl->priv = priv;
1005 ehci_setup_ops(ctrl, ops);
1006}
1007
Simon Glass0851caa2015-03-25 12:22:19 -06001008void *ehci_get_controller_priv(int index)
1009{
1010 return ehcic[index].priv;
1011}
Simon Glassa194b252015-03-25 12:22:29 -06001012#endif
Simon Glass0851caa2015-03-25 12:22:19 -06001013
Simon Glassccc40fd2015-03-25 12:22:26 -06001014static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001015{
Lucas Stach3494a4c2012-09-26 00:14:35 +02001016 struct QH *qh_list;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001017 struct QH *periodic;
Simon Glassccc40fd2015-03-25 12:22:26 -06001018 uint32_t reg;
1019 uint32_t cmd;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001020 int i;
michael0bf2a032008-12-11 13:43:55 +01001021
Vincent Palatin0d6f77c2012-12-12 17:55:22 -08001022 /* Set the high address word (aka segment) for 64-bit controller */
Simon Glassccc40fd2015-03-25 12:22:26 -06001023 if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
1024 ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
Stefan Roese2e98fc72009-01-21 17:12:10 +01001025
Simon Glassccc40fd2015-03-25 12:22:26 -06001026 qh_list = &ctrl->qh_list;
Lucas Stach3494a4c2012-09-26 00:14:35 +02001027
Michael Trimarchi241f7512008-11-28 13:20:46 +01001028 /* Set head of reclaim list */
Tom Rini2cabcf72012-07-15 22:14:24 +00001029 memset(qh_list, 0, sizeof(*qh_list));
Rob Herringf14d54b2015-03-17 15:46:37 -05001030 qh_list->qh_link = cpu_to_hc32((unsigned long)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +02001031 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
1032 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini2cabcf72012-07-15 22:14:24 +00001033 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1034 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +02001035 qh_list->qh_overlay.qt_token =
1036 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchi241f7512008-11-28 13:20:46 +01001037
Rob Herringf14d54b2015-03-17 15:46:37 -05001038 flush_dcache_range((unsigned long)qh_list,
Stephen Warren36dad662013-05-24 15:03:17 -06001039 ALIGN_END_ADDR(struct QH, qh_list, 1));
1040
Patrick Georgie55fdac2013-03-06 14:08:31 +00001041 /* Set async. queue head pointer. */
Simon Glassccc40fd2015-03-25 12:22:26 -06001042 ehci_writel(&ctrl->hcor->or_asynclistaddr, (unsigned long)qh_list);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001043
1044 /*
1045 * Set up periodic list
1046 * Step 1: Parent QH for all periodic transfers.
1047 */
Simon Glassccc40fd2015-03-25 12:22:26 -06001048 ctrl->periodic_schedules = 0;
1049 periodic = &ctrl->periodic_queue;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001050 memset(periodic, 0, sizeof(*periodic));
1051 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
1052 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1053 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1054
Rob Herringf14d54b2015-03-17 15:46:37 -05001055 flush_dcache_range((unsigned long)periodic,
Stephen Warren36dad662013-05-24 15:03:17 -06001056 ALIGN_END_ADDR(struct QH, periodic, 1));
1057
Patrick Georgie55fdac2013-03-06 14:08:31 +00001058 /*
1059 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
1060 * In particular, device specifications on polling frequency
1061 * are disregarded. Keyboards seem to send NAK/NYet reliably
1062 * when polled with an empty buffer.
1063 *
1064 * Split Transactions will be spread across microframes using
1065 * S-mask and C-mask.
1066 */
Simon Glassccc40fd2015-03-25 12:22:26 -06001067 if (ctrl->periodic_list == NULL)
1068 ctrl->periodic_list = memalign(4096, 1024 * 4);
Nikita Kiryanov2f13e442013-07-29 13:27:40 +03001069
Simon Glassccc40fd2015-03-25 12:22:26 -06001070 if (!ctrl->periodic_list)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001071 return -ENOMEM;
1072 for (i = 0; i < 1024; i++) {
Simon Glassccc40fd2015-03-25 12:22:26 -06001073 ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
Adrian Cox29d05872014-04-10 13:29:45 +01001074 | QH_LINK_TYPE_QH);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001075 }
1076
Simon Glassccc40fd2015-03-25 12:22:26 -06001077 flush_dcache_range((unsigned long)ctrl->periodic_list,
1078 ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
Stephen Warren36dad662013-05-24 15:03:17 -06001079 1024));
1080
Patrick Georgie55fdac2013-03-06 14:08:31 +00001081 /* Set periodic list base address */
Simon Glassccc40fd2015-03-25 12:22:26 -06001082 ehci_writel(&ctrl->hcor->or_periodiclistbase,
1083 (unsigned long)ctrl->periodic_list);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001084
Simon Glassccc40fd2015-03-25 12:22:26 -06001085 reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
michael0bf2a032008-12-11 13:43:55 +01001086 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stachf5b34082012-09-28 00:26:19 +02001087 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmer33e87482008-12-13 22:51:58 +01001088 /* Port Indicators */
1089 if (HCS_INDICATOR(reg))
Lucas Stach835e11e2012-09-06 08:00:13 +02001090 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1091 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmer33e87482008-12-13 22:51:58 +01001092 /* Port Power Control */
1093 if (HCS_PPC(reg))
Lucas Stach835e11e2012-09-06 08:00:13 +02001094 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1095 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchi241f7512008-11-28 13:20:46 +01001096
Michael Trimarchi241f7512008-11-28 13:20:46 +01001097 /* Start the host controller. */
Simon Glassccc40fd2015-03-25 12:22:26 -06001098 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Wolfgang Denkfb718e12009-02-12 00:08:39 +01001099 /*
1100 * Philips, Intel, and maybe others need CMD_RUN before the
1101 * root hub will detect new devices (why?); NEC doesn't
1102 */
michael0bf2a032008-12-11 13:43:55 +01001103 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
1104 cmd |= CMD_RUN;
Simon Glassccc40fd2015-03-25 12:22:26 -06001105 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0bf2a032008-12-11 13:43:55 +01001106
Simon Glassccc40fd2015-03-25 12:22:26 -06001107 if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
1108 /* take control over the ports */
1109 cmd = ehci_readl(&ctrl->hcor->or_configflag);
1110 cmd |= FLAG_CF;
1111 ehci_writel(&ctrl->hcor->or_configflag, cmd);
1112 }
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +08001113
Remy Böhmer33e87482008-12-13 22:51:58 +01001114 /* unblock posted write */
Simon Glassccc40fd2015-03-25 12:22:26 -06001115 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001116 mdelay(5);
Simon Glassccc40fd2015-03-25 12:22:26 -06001117 reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
Remy Böhmer33e87482008-12-13 22:51:58 +01001118 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchi241f7512008-11-28 13:20:46 +01001119
Simon Glassccc40fd2015-03-25 12:22:26 -06001120 return 0;
1121}
1122
Simon Glassa194b252015-03-25 12:22:29 -06001123#ifndef CONFIG_DM_USB
Simon Glassccc40fd2015-03-25 12:22:26 -06001124int usb_lowlevel_stop(int index)
1125{
1126 ehci_shutdown(&ehcic[index]);
1127 return ehci_hcd_stop(index);
1128}
1129
1130int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1131{
1132 struct ehci_ctrl *ctrl = &ehcic[index];
1133 uint tweaks = 0;
1134 int rc;
1135
Simon Glassdc9f3ed2015-03-25 12:22:27 -06001136 /**
1137 * Set ops to default_ehci_ops, ehci_hcd_init should call
1138 * ehci_set_controller_priv to change any of these function pointers.
1139 */
1140 ctrl->ops = default_ehci_ops;
1141
Simon Glassccc40fd2015-03-25 12:22:26 -06001142 rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
1143 if (rc)
1144 return rc;
1145 if (init == USB_INIT_DEVICE)
1146 goto done;
1147
1148 /* EHCI spec section 4.1 */
Simon Glass302696b2015-03-25 12:22:28 -06001149 if (ehci_reset(ctrl))
Simon Glassccc40fd2015-03-25 12:22:26 -06001150 return -1;
1151
1152#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
1153 rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
1154 if (rc)
1155 return rc;
1156#endif
1157#ifdef CONFIG_USB_EHCI_FARADAY
1158 tweaks |= EHCI_TWEAK_NO_INIT_CF;
1159#endif
1160 rc = ehci_common_init(ctrl, tweaks);
1161 if (rc)
1162 return rc;
1163
1164 ctrl->rootdev = 0;
Troy Kisky7d6bbb92013-10-10 15:27:57 -07001165done:
Lucas Stach3494a4c2012-09-26 00:14:35 +02001166 *controller = &ehcic[index];
Michael Trimarchi241f7512008-11-28 13:20:46 +01001167 return 0;
1168}
Simon Glassa194b252015-03-25 12:22:29 -06001169#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +01001170
Simon Glasscb7cf602015-03-25 12:22:25 -06001171static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
1172 void *buffer, int length)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001173{
1174
1175 if (usb_pipetype(pipe) != PIPE_BULK) {
1176 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1177 return -1;
1178 }
1179 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1180}
1181
Simon Glasscb7cf602015-03-25 12:22:25 -06001182static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
1183 void *buffer, int length,
1184 struct devrequest *setup)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001185{
Simon Glasscb7cf602015-03-25 12:22:25 -06001186 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchi241f7512008-11-28 13:20:46 +01001187
1188 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1189 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1190 return -1;
1191 }
1192
Lucas Stach3494a4c2012-09-26 00:14:35 +02001193 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1194 if (!ctrl->rootdev)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001195 dev->speed = USB_SPEED_HIGH;
1196 return ehci_submit_root(dev, pipe, buffer, length, setup);
1197 }
1198 return ehci_submit_async(dev, pipe, buffer, length, setup);
1199}
1200
Patrick Georgie55fdac2013-03-06 14:08:31 +00001201struct int_queue {
Hans de Goede8c5c5ca2014-09-24 14:06:05 +02001202 int elementsize;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001203 struct QH *first;
1204 struct QH *current;
1205 struct QH *last;
1206 struct qTD *tds;
1207};
1208
Rob Herringf14d54b2015-03-17 15:46:37 -05001209#define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001210
1211static int
1212enable_periodic(struct ehci_ctrl *ctrl)
1213{
1214 uint32_t cmd;
1215 struct ehci_hcor *hcor = ctrl->hcor;
1216 int ret;
1217
1218 cmd = ehci_readl(&hcor->or_usbcmd);
1219 cmd |= CMD_PSE;
1220 ehci_writel(&hcor->or_usbcmd, cmd);
1221
1222 ret = handshake((uint32_t *)&hcor->or_usbsts,
1223 STS_PSS, STS_PSS, 100 * 1000);
1224 if (ret < 0) {
1225 printf("EHCI failed: timeout when enabling periodic list\n");
1226 return -ETIMEDOUT;
1227 }
1228 udelay(1000);
1229 return 0;
1230}
1231
1232static int
1233disable_periodic(struct ehci_ctrl *ctrl)
1234{
1235 uint32_t cmd;
1236 struct ehci_hcor *hcor = ctrl->hcor;
1237 int ret;
1238
1239 cmd = ehci_readl(&hcor->or_usbcmd);
1240 cmd &= ~CMD_PSE;
1241 ehci_writel(&hcor->or_usbcmd, cmd);
1242
1243 ret = handshake((uint32_t *)&hcor->or_usbsts,
1244 STS_PSS, 0, 100 * 1000);
1245 if (ret < 0) {
1246 printf("EHCI failed: timeout when disabling periodic list\n");
1247 return -ETIMEDOUT;
1248 }
1249 return 0;
1250}
1251
Patrick Georgie55fdac2013-03-06 14:08:31 +00001252struct int_queue *
1253create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
Hans de Goedebbd2d2a2015-01-11 20:38:28 +01001254 int elementsize, void *buffer, int interval)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001255{
Simon Glasscb7cf602015-03-25 12:22:25 -06001256 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001257 struct int_queue *result = NULL;
1258 int i;
1259
Hans de Goede7f7cb732014-09-24 14:06:04 +02001260 /*
1261 * Interrupt transfers requiring several transactions are not supported
1262 * because bInterval is ignored.
1263 *
1264 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1265 * <= PKT_ALIGN if several qTDs are required, while the USB
1266 * specification does not constrain this for interrupt transfers. That
1267 * means that ehci_submit_async() would support interrupt transfers
1268 * requiring several transactions only as long as the transfer size does
1269 * not require more than a single qTD.
1270 */
1271 if (elementsize > usb_maxpacket(dev, pipe)) {
1272 printf("%s: xfers requiring several transactions are not supported.\n",
1273 __func__);
1274 return NULL;
1275 }
1276
Patrick Georgie55fdac2013-03-06 14:08:31 +00001277 debug("Enter create_int_queue\n");
1278 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1279 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1280 return NULL;
1281 }
1282
1283 /* limit to 4 full pages worth of data -
1284 * we can safely fit them in a single TD,
1285 * no matter the alignment
1286 */
1287 if (elementsize >= 16384) {
1288 debug("too large elements for interrupt transfers\n");
1289 return NULL;
1290 }
1291
1292 result = malloc(sizeof(*result));
1293 if (!result) {
1294 debug("ehci intr queue: out of memory\n");
1295 goto fail1;
1296 }
Hans de Goede8c5c5ca2014-09-24 14:06:05 +02001297 result->elementsize = elementsize;
Stephen Warrend7fe61d2014-02-06 13:13:06 -07001298 result->first = memalign(USB_DMA_MINALIGN,
1299 sizeof(struct QH) * queuesize);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001300 if (!result->first) {
1301 debug("ehci intr queue: out of memory\n");
1302 goto fail2;
1303 }
1304 result->current = result->first;
1305 result->last = result->first + queuesize - 1;
Stephen Warrend7fe61d2014-02-06 13:13:06 -07001306 result->tds = memalign(USB_DMA_MINALIGN,
1307 sizeof(struct qTD) * queuesize);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001308 if (!result->tds) {
1309 debug("ehci intr queue: out of memory\n");
1310 goto fail3;
1311 }
1312 memset(result->first, 0, sizeof(struct QH) * queuesize);
1313 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1314
1315 for (i = 0; i < queuesize; i++) {
1316 struct QH *qh = result->first + i;
1317 struct qTD *td = result->tds + i;
1318 void **buf = &qh->buffer;
1319
Rob Herringf14d54b2015-03-17 15:46:37 -05001320 qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001321 if (i == queuesize - 1)
Adrian Cox29d05872014-04-10 13:29:45 +01001322 qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001323
Rob Herringf14d54b2015-03-17 15:46:37 -05001324 qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
Adrian Cox29d05872014-04-10 13:29:45 +01001325 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1326 qh->qh_endpt1 =
1327 cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
Patrick Georgie55fdac2013-03-06 14:08:31 +00001328 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1329 (1 << 14) |
1330 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1331 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
Adrian Cox29d05872014-04-10 13:29:45 +01001332 (usb_pipedevice(pipe) << 0));
1333 qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1334 (1 << 0)); /* S-mask: microframe 0 */
Patrick Georgie55fdac2013-03-06 14:08:31 +00001335 if (dev->speed == USB_SPEED_LOW ||
1336 dev->speed == USB_SPEED_FULL) {
Hans de Goededa166772014-09-20 16:51:22 +02001337 /* C-mask: microframes 2-4 */
1338 qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
Patrick Georgie55fdac2013-03-06 14:08:31 +00001339 }
Hans de Goededa166772014-09-20 16:51:22 +02001340 ehci_update_endpt2_dev_n_port(dev, qh);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001341
Adrian Cox29d05872014-04-10 13:29:45 +01001342 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1343 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001344 debug("communication direction is '%s'\n",
1345 usb_pipein(pipe) ? "in" : "out");
Adrian Cox29d05872014-04-10 13:29:45 +01001346 td->qt_token = cpu_to_hc32((elementsize << 16) |
Patrick Georgie55fdac2013-03-06 14:08:31 +00001347 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
Adrian Cox29d05872014-04-10 13:29:45 +01001348 0x80); /* active */
1349 td->qt_buffer[0] =
Rob Herringf14d54b2015-03-17 15:46:37 -05001350 cpu_to_hc32((unsigned long)buffer + i * elementsize);
Adrian Cox29d05872014-04-10 13:29:45 +01001351 td->qt_buffer[1] =
1352 cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1353 td->qt_buffer[2] =
1354 cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1355 td->qt_buffer[3] =
1356 cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1357 td->qt_buffer[4] =
1358 cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001359
1360 *buf = buffer + i * elementsize;
1361 }
1362
Rob Herringf14d54b2015-03-17 15:46:37 -05001363 flush_dcache_range((unsigned long)buffer,
Stephen Warren36dad662013-05-24 15:03:17 -06001364 ALIGN_END_ADDR(char, buffer,
1365 queuesize * elementsize));
Rob Herringf14d54b2015-03-17 15:46:37 -05001366 flush_dcache_range((unsigned long)result->first,
Stephen Warren36dad662013-05-24 15:03:17 -06001367 ALIGN_END_ADDR(struct QH, result->first,
1368 queuesize));
Rob Herringf14d54b2015-03-17 15:46:37 -05001369 flush_dcache_range((unsigned long)result->tds,
Stephen Warren36dad662013-05-24 15:03:17 -06001370 ALIGN_END_ADDR(struct qTD, result->tds,
1371 queuesize));
1372
Hans de Goede8ba55ed2014-09-24 14:06:03 +02001373 if (ctrl->periodic_schedules > 0) {
1374 if (disable_periodic(ctrl) < 0) {
1375 debug("FATAL: periodic should never fail, but did");
1376 goto fail3;
1377 }
Patrick Georgie55fdac2013-03-06 14:08:31 +00001378 }
1379
1380 /* hook up to periodic list */
1381 struct QH *list = &ctrl->periodic_queue;
1382 result->last->qh_link = list->qh_link;
Rob Herringf14d54b2015-03-17 15:46:37 -05001383 list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001384
Rob Herringf14d54b2015-03-17 15:46:37 -05001385 flush_dcache_range((unsigned long)result->last,
Stephen Warren36dad662013-05-24 15:03:17 -06001386 ALIGN_END_ADDR(struct QH, result->last, 1));
Rob Herringf14d54b2015-03-17 15:46:37 -05001387 flush_dcache_range((unsigned long)list,
Stephen Warren36dad662013-05-24 15:03:17 -06001388 ALIGN_END_ADDR(struct QH, list, 1));
1389
Patrick Georgie55fdac2013-03-06 14:08:31 +00001390 if (enable_periodic(ctrl) < 0) {
1391 debug("FATAL: periodic should never fail, but did");
1392 goto fail3;
1393 }
Hans de Goede8f5f4f72014-09-20 16:51:25 +02001394 ctrl->periodic_schedules++;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001395
1396 debug("Exit create_int_queue\n");
1397 return result;
1398fail3:
1399 if (result->tds)
1400 free(result->tds);
1401fail2:
1402 if (result->first)
1403 free(result->first);
1404 if (result)
1405 free(result);
1406fail1:
1407 return NULL;
1408}
1409
1410void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1411{
1412 struct QH *cur = queue->current;
Hans de Goede9db174c2014-09-20 16:51:24 +02001413 struct qTD *cur_td;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001414
1415 /* depleted queue */
1416 if (cur == NULL) {
1417 debug("Exit poll_int_queue with completed queue\n");
1418 return NULL;
1419 }
1420 /* still active */
Hans de Goede9db174c2014-09-20 16:51:24 +02001421 cur_td = &queue->tds[queue->current - queue->first];
Rob Herringf14d54b2015-03-17 15:46:37 -05001422 invalidate_dcache_range((unsigned long)cur_td,
Hans de Goede9db174c2014-09-20 16:51:24 +02001423 ALIGN_END_ADDR(struct qTD, cur_td, 1));
1424 if (QT_TOKEN_GET_STATUS(hc32_to_cpu(cur_td->qt_token)) &
1425 QT_TOKEN_STATUS_ACTIVE) {
1426 debug("Exit poll_int_queue with no completed intr transfer. token is %x\n",
1427 hc32_to_cpu(cur_td->qt_token));
Patrick Georgie55fdac2013-03-06 14:08:31 +00001428 return NULL;
1429 }
1430 if (!(cur->qh_link & QH_LINK_TERMINATE))
1431 queue->current++;
1432 else
1433 queue->current = NULL;
Hans de Goede8c5c5ca2014-09-24 14:06:05 +02001434
Rob Herringf14d54b2015-03-17 15:46:37 -05001435 invalidate_dcache_range((unsigned long)cur->buffer,
Hans de Goede8c5c5ca2014-09-24 14:06:05 +02001436 ALIGN_END_ADDR(char, cur->buffer,
1437 queue->elementsize));
1438
Hans de Goede9db174c2014-09-20 16:51:24 +02001439 debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
1440 hc32_to_cpu(cur_td->qt_token), cur, queue->first);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001441 return cur->buffer;
1442}
1443
1444/* Do not free buffers associated with QHs, they're owned by someone else */
Hans de Goede6f681222014-09-24 14:06:06 +02001445int
Patrick Georgie55fdac2013-03-06 14:08:31 +00001446destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1447{
Simon Glasscb7cf602015-03-25 12:22:25 -06001448 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001449 int result = -1;
1450 unsigned long timeout;
1451
1452 if (disable_periodic(ctrl) < 0) {
1453 debug("FATAL: periodic should never fail, but did");
1454 goto out;
1455 }
Hans de Goede8f5f4f72014-09-20 16:51:25 +02001456 ctrl->periodic_schedules--;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001457
1458 struct QH *cur = &ctrl->periodic_queue;
1459 timeout = get_timer(0) + 500; /* abort after 500ms */
Adrian Cox29d05872014-04-10 13:29:45 +01001460 while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
Patrick Georgie55fdac2013-03-06 14:08:31 +00001461 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1462 if (NEXT_QH(cur) == queue->first) {
1463 debug("found candidate. removing from chain\n");
1464 cur->qh_link = queue->last->qh_link;
Rob Herringf14d54b2015-03-17 15:46:37 -05001465 flush_dcache_range((unsigned long)cur,
Hans de Goede8e00cf62014-09-20 16:51:23 +02001466 ALIGN_END_ADDR(struct QH, cur, 1));
Patrick Georgie55fdac2013-03-06 14:08:31 +00001467 result = 0;
1468 break;
1469 }
1470 cur = NEXT_QH(cur);
1471 if (get_timer(0) > timeout) {
1472 printf("Timeout destroying interrupt endpoint queue\n");
1473 result = -1;
1474 goto out;
1475 }
1476 }
1477
Hans de Goede8f5f4f72014-09-20 16:51:25 +02001478 if (ctrl->periodic_schedules > 0) {
Patrick Georgie55fdac2013-03-06 14:08:31 +00001479 result = enable_periodic(ctrl);
1480 if (result < 0)
1481 debug("FATAL: periodic should never fail, but did");
1482 }
1483
1484out:
1485 free(queue->tds);
1486 free(queue->first);
1487 free(queue);
1488
1489 return result;
1490}
1491
Simon Glasscb7cf602015-03-25 12:22:25 -06001492static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe,
1493 void *buffer, int length, int interval)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001494{
Patrick Georgie55fdac2013-03-06 14:08:31 +00001495 void *backbuffer;
1496 struct int_queue *queue;
1497 unsigned long timeout;
1498 int result = 0, ret;
1499
Michael Trimarchi241f7512008-11-28 13:20:46 +01001500 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1501 dev, pipe, buffer, length, interval);
Benoît Thébaudeau58c4dfb2012-08-09 23:50:44 +02001502
Hans de Goedebbd2d2a2015-01-11 20:38:28 +01001503 queue = create_int_queue(dev, pipe, 1, length, buffer, interval);
Hans de Goede7f7cb732014-09-24 14:06:04 +02001504 if (!queue)
1505 return -1;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001506
1507 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1508 while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
1509 if (get_timer(0) > timeout) {
1510 printf("Timeout poll on interrupt endpoint\n");
1511 result = -ETIMEDOUT;
1512 break;
1513 }
1514
1515 if (backbuffer != buffer) {
Rob Herringf14d54b2015-03-17 15:46:37 -05001516 debug("got wrong buffer back (%p instead of %p)\n",
1517 backbuffer, buffer);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001518 return -EINVAL;
1519 }
1520
1521 ret = destroy_int_queue(dev, queue);
1522 if (ret < 0)
1523 return ret;
1524
1525 /* everything worked out fine */
1526 return result;
Marek Vasut9b315fe2011-09-25 21:07:56 +02001527}
Simon Glasscb7cf602015-03-25 12:22:25 -06001528
Simon Glassa194b252015-03-25 12:22:29 -06001529#ifndef CONFIG_DM_USB
Simon Glasscb7cf602015-03-25 12:22:25 -06001530int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
1531 void *buffer, int length)
1532{
1533 return _ehci_submit_bulk_msg(dev, pipe, buffer, length);
1534}
1535
1536int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1537 int length, struct devrequest *setup)
1538{
1539 return _ehci_submit_control_msg(dev, pipe, buffer, length, setup);
1540}
1541
1542int submit_int_msg(struct usb_device *dev, unsigned long pipe,
1543 void *buffer, int length, int interval)
1544{
1545 return _ehci_submit_int_msg(dev, pipe, buffer, length, interval);
1546}
Simon Glassa194b252015-03-25 12:22:29 -06001547#endif
1548
1549#ifdef CONFIG_DM_USB
1550static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1551 unsigned long pipe, void *buffer, int length,
1552 struct devrequest *setup)
1553{
1554 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1555 dev->name, udev, udev->dev->name, udev->portnr);
1556
1557 return _ehci_submit_control_msg(udev, pipe, buffer, length, setup);
1558}
1559
1560static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1561 unsigned long pipe, void *buffer, int length)
1562{
1563 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1564 return _ehci_submit_bulk_msg(udev, pipe, buffer, length);
1565}
1566
1567static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1568 unsigned long pipe, void *buffer, int length,
1569 int interval)
1570{
1571 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1572 return _ehci_submit_int_msg(udev, pipe, buffer, length, interval);
1573}
1574
1575int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
1576 struct ehci_hcor *hcor, const struct ehci_ops *ops,
1577 uint tweaks, enum usb_init_type init)
1578{
1579 struct ehci_ctrl *ctrl = dev_get_priv(dev);
1580 int ret;
1581
1582 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
1583 dev->name, ctrl, hccr, hcor, init);
1584
1585 ehci_setup_ops(ctrl, ops);
1586 ctrl->hccr = hccr;
1587 ctrl->hcor = hcor;
1588 ctrl->priv = ctrl;
1589
1590 if (init == USB_INIT_DEVICE)
1591 goto done;
1592 ret = ehci_reset(ctrl);
1593 if (ret)
1594 goto err;
1595
1596 ret = ehci_common_init(ctrl, tweaks);
1597 if (ret)
1598 goto err;
1599done:
1600 return 0;
1601err:
1602 free(ctrl);
1603 debug("%s: failed, ret=%d\n", __func__, ret);
1604 return ret;
1605}
1606
1607int ehci_deregister(struct udevice *dev)
1608{
1609 struct ehci_ctrl *ctrl = dev_get_priv(dev);
1610
1611 ehci_shutdown(ctrl);
1612
1613 return 0;
1614}
1615
1616struct dm_usb_ops ehci_usb_ops = {
1617 .control = ehci_submit_control_msg,
1618 .bulk = ehci_submit_bulk_msg,
1619 .interrupt = ehci_submit_int_msg,
1620};
1621
1622#endif