blob: fa5d584b82e4a4c7f61168436049fa1d267a58a0 [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 *
Simon Glass1b2e3652015-07-06 16:47:42 -06008 * SPDX-License-Identifier: GPL-2.0
Michael Trimarchi241f7512008-11-28 13:20:46 +01009 */
Michael Trimarchi241f7512008-11-28 13:20:46 +010010#include <common.h>
Simon Glassa194b252015-03-25 12:22:29 -060011#include <dm.h>
Patrick Georgie55fdac2013-03-06 14:08:31 +000012#include <errno.h>
michael0a326102008-12-10 17:55:19 +010013#include <asm/byteorder.h>
Lucas Stach835e11e2012-09-06 08:00:13 +020014#include <asm/unaligned.h>
Michael Trimarchi241f7512008-11-28 13:20:46 +010015#include <usb.h>
16#include <asm/io.h>
michael0a326102008-12-10 17:55:19 +010017#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060018#include <memalign.h>
Stefan Roese86b34cf2010-11-26 15:43:28 +010019#include <watchdog.h>
Patrick Georgie55fdac2013-03-06 14:08:31 +000020#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARD8f6bcf42009-04-03 12:46:58 +020021
22#include "ehci.h"
Michael Trimarchi241f7512008-11-28 13:20:46 +010023
Lucas Stach3494a4c2012-09-26 00:14:35 +020024#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
25#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
26#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +010027
Julius Werner5c1a1ad2013-09-24 10:53:07 -070028/*
29 * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
30 * Let's time out after 8 to have a little safety margin on top of that.
31 */
32#define HCHALT_TIMEOUT (8 * 1000)
33
Simon Glassa194b252015-03-25 12:22:29 -060034#ifndef CONFIG_DM_USB
Marek Vasutfd349a12013-07-10 03:16:31 +020035static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
Simon Glassa194b252015-03-25 12:22:29 -060036#endif
Tom Rini2cabcf72012-07-15 22:14:24 +000037
38#define ALIGN_END_ADDR(type, ptr, size) \
Rob Herringf14d54b2015-03-17 15:46:37 -050039 ((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchi241f7512008-11-28 13:20:46 +010040
michael0a326102008-12-10 17:55:19 +010041static struct descriptor {
42 struct usb_hub_descriptor hub;
43 struct usb_device_descriptor device;
44 struct usb_linux_config_descriptor config;
45 struct usb_linux_interface_descriptor interface;
46 struct usb_endpoint_descriptor endpoint;
47} __attribute__ ((packed)) descriptor = {
48 {
49 0x8, /* bDescLength */
50 0x29, /* bDescriptorType: hub descriptor */
51 2, /* bNrPorts -- runtime modified */
52 0, /* wHubCharacteristics */
Vincent Palatin8277b502011-12-05 14:52:22 -080053 10, /* bPwrOn2PwrGood */
michael0a326102008-12-10 17:55:19 +010054 0, /* bHubCntrCurrent */
55 {}, /* Device removable */
56 {} /* at most 7 ports! XXX */
57 },
58 {
59 0x12, /* bLength */
60 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyovfa30a272010-02-27 21:29:42 +030061 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michael0a326102008-12-10 17:55:19 +010062 9, /* bDeviceClass: UDCLASS_HUB */
63 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
64 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
65 64, /* bMaxPacketSize: 64 bytes */
66 0x0000, /* idVendor */
67 0x0000, /* idProduct */
Sergei Shtylyovfa30a272010-02-27 21:29:42 +030068 cpu_to_le16(0x0100), /* bcdDevice */
michael0a326102008-12-10 17:55:19 +010069 1, /* iManufacturer */
70 2, /* iProduct */
71 0, /* iSerialNumber */
72 1 /* bNumConfigurations: 1 */
73 },
74 {
75 0x9,
76 2, /* bDescriptorType: UDESC_CONFIG */
77 cpu_to_le16(0x19),
78 1, /* bNumInterface */
79 1, /* bConfigurationValue */
80 0, /* iConfiguration */
81 0x40, /* bmAttributes: UC_SELF_POWER */
82 0 /* bMaxPower */
83 },
84 {
85 0x9, /* bLength */
86 4, /* bDescriptorType: UDESC_INTERFACE */
87 0, /* bInterfaceNumber */
88 0, /* bAlternateSetting */
89 1, /* bNumEndpoints */
90 9, /* bInterfaceClass: UICLASS_HUB */
91 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
92 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
93 0 /* iInterface */
94 },
95 {
96 0x7, /* bLength */
97 5, /* bDescriptorType: UDESC_ENDPOINT */
98 0x81, /* bEndpointAddress:
99 * UE_DIR_IN | EHCI_INTR_ENDPT
100 */
101 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix83b9e1d2009-10-31 12:37:38 -0500102 8, /* wMaxPacketSize */
michael0a326102008-12-10 17:55:19 +0100103 255 /* bInterval */
104 },
Michael Trimarchi241f7512008-11-28 13:20:46 +0100105};
106
Remy Böhmer33e87482008-12-13 22:51:58 +0100107#if defined(CONFIG_EHCI_IS_TDI)
108#define ehci_is_TDI() (1)
109#else
110#define ehci_is_TDI() (0)
111#endif
112
Simon Glasscb7cf602015-03-25 12:22:25 -0600113static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev)
114{
Simon Glassa194b252015-03-25 12:22:29 -0600115#ifdef CONFIG_DM_USB
Hans de Goede6be39d12015-05-05 11:54:33 +0200116 return dev_get_priv(usb_get_bus(udev->dev));
Simon Glassa194b252015-03-25 12:22:29 -0600117#else
Simon Glasscb7cf602015-03-25 12:22:25 -0600118 return udev->controller;
Simon Glassa194b252015-03-25 12:22:29 -0600119#endif
Simon Glasscb7cf602015-03-25 12:22:25 -0600120}
121
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600122static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
Jim Lin54f3dfe2013-03-27 00:52:32 +0000123{
124 return PORTSC_PSPD(reg);
125}
126
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600127static void ehci_set_usbmode(struct ehci_ctrl *ctrl)
Jim Lin54f3dfe2013-03-27 00:52:32 +0000128{
129 uint32_t tmp;
130 uint32_t *reg_ptr;
131
Simon Glass2d387ab2015-03-25 12:22:23 -0600132 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE);
Jim Lin54f3dfe2013-03-27 00:52:32 +0000133 tmp = ehci_readl(reg_ptr);
134 tmp |= USBMODE_CM_HC;
135#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
136 tmp |= USBMODE_BE;
Marek Vasutd9fa0482016-01-23 21:04:46 +0100137#else
138 tmp &= ~USBMODE_BE;
Jim Lin54f3dfe2013-03-27 00:52:32 +0000139#endif
140 ehci_writel(reg_ptr, tmp);
141}
142
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600143static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
Simon Glasscc0dc6b2015-03-25 12:22:21 -0600144 uint32_t *reg)
Marek Vasut09734772011-07-11 02:37:01 +0200145{
146 mdelay(50);
147}
148
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600149static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
Simon Glass0bec1282015-03-25 12:22:17 -0600150{
151 if (port < 0 || port >= CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
152 /* Printing the message would cause a scan failure! */
153 debug("The request port(%u) is not configured\n", port);
154 return NULL;
155 }
156
Simon Glassdfbf1862015-03-25 12:22:24 -0600157 return (uint32_t *)&ctrl->hcor->or_portsc[port];
Simon Glass0bec1282015-03-25 12:22:17 -0600158}
159
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100160static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michael0a326102008-12-10 17:55:19 +0100161{
michael0bf2a032008-12-11 13:43:55 +0100162 uint32_t result;
163 do {
164 result = ehci_readl(ptr);
Wolfgang Denkcdc5a7a2010-10-22 14:23:00 +0200165 udelay(5);
michael0bf2a032008-12-11 13:43:55 +0100166 if (result == ~(uint32_t)0)
167 return -1;
168 result &= mask;
169 if (result == done)
170 return 0;
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100171 usec--;
172 } while (usec > 0);
michael0bf2a032008-12-11 13:43:55 +0100173 return -1;
174}
175
Simon Glass302696b2015-03-25 12:22:28 -0600176static int ehci_reset(struct ehci_ctrl *ctrl)
michael0bf2a032008-12-11 13:43:55 +0100177{
178 uint32_t cmd;
michael0bf2a032008-12-11 13:43:55 +0100179 int ret = 0;
180
Simon Glass302696b2015-03-25 12:22:28 -0600181 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Stefan Roese745af442010-11-26 15:44:00 +0100182 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Simon Glass302696b2015-03-25 12:22:28 -0600183 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
184 ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
Lucas Stach3494a4c2012-09-26 00:14:35 +0200185 CMD_RESET, 0, 250 * 1000);
michael0bf2a032008-12-11 13:43:55 +0100186 if (ret < 0) {
187 printf("EHCI fail to reset\n");
188 goto out;
189 }
190
Jim Lin54f3dfe2013-03-27 00:52:32 +0000191 if (ehci_is_TDI())
Simon Glass302696b2015-03-25 12:22:28 -0600192 ctrl->ops.set_usb_mode(ctrl);
Simon Glass5978cdb2012-02-27 10:52:47 +0000193
194#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Simon Glass302696b2015-03-25 12:22:28 -0600195 cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200196 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass5978cdb2012-02-27 10:52:47 +0000197 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Simon Glass302696b2015-03-25 12:22:28 -0600198 ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
Simon Glass5978cdb2012-02-27 10:52:47 +0000199#endif
michael0bf2a032008-12-11 13:43:55 +0100200out:
201 return ret;
michael0a326102008-12-10 17:55:19 +0100202}
Michael Trimarchi241f7512008-11-28 13:20:46 +0100203
Julius Werner5c1a1ad2013-09-24 10:53:07 -0700204static int ehci_shutdown(struct ehci_ctrl *ctrl)
205{
206 int i, ret = 0;
207 uint32_t cmd, reg;
208
Marek Vasut919d00a2013-12-14 02:03:11 +0100209 if (!ctrl || !ctrl->hcor)
210 return -EINVAL;
211
Julius Werner5c1a1ad2013-09-24 10:53:07 -0700212 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
213 cmd &= ~(CMD_PSE | CMD_ASE);
214 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
215 ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
216 100 * 1000);
217
218 if (!ret) {
219 for (i = 0; i < CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS; i++) {
220 reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
221 reg |= EHCI_PS_SUSP;
222 ehci_writel(&ctrl->hcor->or_portsc[i], reg);
223 }
224
225 cmd &= ~CMD_RUN;
226 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
227 ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
228 HCHALT_TIMEOUT);
229 }
230
231 if (ret)
232 puts("EHCI failed to shut down host controller.\n");
233
234 return ret;
235}
236
Michael Trimarchi241f7512008-11-28 13:20:46 +0100237static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
238{
Marek Vasutff24dc32012-04-09 04:07:46 +0200239 uint32_t delta, next;
Marek Vasutcadf42c2016-02-26 19:23:27 +0100240 unsigned long addr = (unsigned long)buf;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100241 int idx;
242
Ilya Yanokfb113712012-07-15 04:43:49 +0000243 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutff24dc32012-04-09 04:07:46 +0200244 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
245
Ilya Yanokfb113712012-07-15 04:43:49 +0000246 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
247
Michael Trimarchi241f7512008-11-28 13:20:46 +0100248 idx = 0;
Benoît Thébaudeaue68f48a2012-07-19 22:16:38 +0200249 while (idx < QT_BUFFER_CNT) {
Marek Vasutdf0b6242016-01-23 21:04:46 +0100250 td->qt_buffer[idx] = cpu_to_hc32(virt_to_phys((void *)addr));
Wolfgang Denkebb829f2010-10-19 16:13:15 +0200251 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200252 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100253 delta = next - addr;
254 if (delta >= sz)
255 break;
256 sz -= delta;
257 addr = next;
258 idx++;
259 }
260
Benoît Thébaudeaue68f48a2012-07-19 22:16:38 +0200261 if (idx == QT_BUFFER_CNT) {
Rob Herringf14d54b2015-03-17 15:46:37 -0500262 printf("out of buffer pointers (%zu bytes left)\n", sz);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100263 return -1;
264 }
265
266 return 0;
267}
268
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000269static inline u8 ehci_encode_speed(enum usb_device_speed speed)
270{
271 #define QH_HIGH_SPEED 2
272 #define QH_FULL_SPEED 0
273 #define QH_LOW_SPEED 1
274 if (speed == USB_SPEED_HIGH)
275 return QH_HIGH_SPEED;
276 if (speed == USB_SPEED_LOW)
277 return QH_LOW_SPEED;
278 return QH_FULL_SPEED;
279}
280
Simon Glassa194b252015-03-25 12:22:29 -0600281static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
Hans de Goededa166772014-09-20 16:51:22 +0200282 struct QH *qh)
283{
Stefan Brünsa0105682015-12-22 01:21:03 +0100284 uint8_t portnr = 0;
285 uint8_t hubaddr = 0;
Hans de Goededa166772014-09-20 16:51:22 +0200286
Simon Glassa194b252015-03-25 12:22:29 -0600287 if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
Hans de Goededa166772014-09-20 16:51:22 +0200288 return;
289
Stefan Brünsa0105682015-12-22 01:21:03 +0100290 usb_find_usb2_hub_address_port(udev, &hubaddr, &portnr);
Hans de Goededa166772014-09-20 16:51:22 +0200291
Stefan Brünsa0105682015-12-22 01:21:03 +0100292 qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(portnr) |
293 QH_ENDPT2_HUBADDR(hubaddr));
Hans de Goededa166772014-09-20 16:51:22 +0200294}
295
Michael Trimarchi241f7512008-11-28 13:20:46 +0100296static int
297ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
298 int length, struct devrequest *req)
299{
Tom Rini2cabcf72012-07-15 22:14:24 +0000300 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200301 struct qTD *qtd;
302 int qtd_count = 0;
Marek Vasut4f668312012-04-08 23:32:05 +0200303 int qtd_counter = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100304 volatile struct qTD *vtd;
305 unsigned long ts;
306 uint32_t *tdp;
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200307 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100308 uint32_t c, toggle;
michael0a326102008-12-10 17:55:19 +0100309 uint32_t cmd;
Simon Glassfd7f5132011-02-07 14:42:16 -0800310 int timeout;
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100311 int ret = 0;
Simon Glasscb7cf602015-03-25 12:22:25 -0600312 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100313
michael0a326102008-12-10 17:55:19 +0100314 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchi241f7512008-11-28 13:20:46 +0100315 buffer, length, req);
316 if (req != NULL)
michael0a326102008-12-10 17:55:19 +0100317 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100318 req->request, req->request,
319 req->requesttype, req->requesttype,
320 le16_to_cpu(req->value), le16_to_cpu(req->value),
michael0a326102008-12-10 17:55:19 +0100321 le16_to_cpu(req->index));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100322
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200323#define PKT_ALIGN 512
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200324 /*
325 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
326 * described by a transfer descriptor (the qTD). The qTDs form a linked
327 * list with a queue head (QH).
328 *
329 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
330 * have its beginning in a qTD transfer and its end in the following
331 * one, so the qTD transfer lengths have to be chosen accordingly.
332 *
333 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
334 * single pages. The first data buffer can start at any offset within a
335 * page (not considering the cache-line alignment issues), while the
336 * following buffers must be page-aligned. There is no alignment
337 * constraint on the size of a qTD transfer.
338 */
339 if (req != NULL)
340 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
341 qtd_count += 1 + 1;
342 if (length > 0 || req == NULL) {
343 /*
344 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200345 * data payload (not considering the first qTD transfer, which
346 * may be longer or shorter, and the final one, which may be
347 * shorter).
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200348 *
349 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200350 * transfer size is aligned to PKT_ALIGN, which is a multiple of
351 * wMaxPacketSize (except in some cases for interrupt transfers,
352 * see comment in submit_int_msg()).
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200353 *
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200354 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200355 * QT_BUFFER_CNT full pages will be used.
356 */
357 int xfr_sz = QT_BUFFER_CNT;
358 /*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200359 * However, if the input buffer is not aligned to PKT_ALIGN, the
360 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200361 * data buffer of each transfer will be page-unaligned.
362 */
Rob Herringf14d54b2015-03-17 15:46:37 -0500363 if ((unsigned long)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200364 xfr_sz--;
365 /* Convert the qTD transfer size to bytes. */
366 xfr_sz *= EHCI_PAGE_SIZE;
367 /*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200368 * Approximate by excess the number of qTDs that will be
369 * required for the data payload. The exact formula is way more
370 * complicated and saves at most 2 qTDs, i.e. a total of 128
371 * bytes.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200372 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200373 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200374 }
375/*
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200376 * Threshold value based on the worst-case total size of the allocated qTDs for
377 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200378 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200379#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200380#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
381#endif
382 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
383 if (qtd == NULL) {
384 printf("unable to allocate TDs\n");
385 return -1;
386 }
387
Tom Rini2cabcf72012-07-15 22:14:24 +0000388 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200389 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasut4f668312012-04-08 23:32:05 +0200390
Marek Vasutff24dc32012-04-09 04:07:46 +0200391 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
392
Marek Vasut285c8b32012-04-09 04:13:00 +0200393 /*
394 * Setup QH (3.6 in ehci-r10.pdf)
395 *
396 * qh_link ................. 03-00 H
397 * qh_endpt1 ............... 07-04 H
398 * qh_endpt2 ............... 0B-08 H
399 * - qh_curtd
400 * qh_overlay.qt_next ...... 13-10 H
401 * - qh_overlay.qt_altnext
402 */
Marek Vasutdf0b6242016-01-23 21:04:46 +0100403 qh->qh_link = cpu_to_hc32(virt_to_phys(&ctrl->qh_list) | QH_LINK_TYPE_QH);
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000404 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200405 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200406 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200407 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200408 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
Ilya Yanoka1cf10f2012-11-06 13:48:20 +0000409 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200410 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
411 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini2cabcf72012-07-15 22:14:24 +0000412 qh->qh_endpt1 = cpu_to_hc32(endpt);
Hans de Goededa166772014-09-20 16:51:22 +0200413 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini2cabcf72012-07-15 22:14:24 +0000414 qh->qh_endpt2 = cpu_to_hc32(endpt);
Hans de Goededa166772014-09-20 16:51:22 +0200415 ehci_update_endpt2_dev_n_port(dev, qh);
Tom Rini2cabcf72012-07-15 22:14:24 +0000416 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Stephen Warren1907e5a2014-02-07 09:53:50 -0700417 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100418
Tom Rini2cabcf72012-07-15 22:14:24 +0000419 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100420 if (req != NULL) {
Marek Vasut285c8b32012-04-09 04:13:00 +0200421 /*
422 * Setup request qTD (3.5 in ehci-r10.pdf)
423 *
424 * qt_next ................ 03-00 H
425 * qt_altnext ............. 07-04 H
426 * qt_token ............... 0B-08 H
427 *
428 * [ buffer, buffer_hi ] loaded with "req".
429 */
Marek Vasut4f668312012-04-08 23:32:05 +0200430 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
431 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200432 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
433 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
434 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
435 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasut4f668312012-04-08 23:32:05 +0200436 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200437 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
438 printf("unable to construct SETUP TD\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100439 goto fail;
440 }
Marek Vasut285c8b32012-04-09 04:13:00 +0200441 /* Update previous qTD! */
Marek Vasutdf0b6242016-01-23 21:04:46 +0100442 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
Marek Vasut4f668312012-04-08 23:32:05 +0200443 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100444 toggle = 1;
445 }
446
447 if (length > 0 || req == NULL) {
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200448 uint8_t *buf_ptr = buffer;
449 int left_length = length;
450
451 do {
452 /*
453 * Determine the size of this qTD transfer. By default,
454 * QT_BUFFER_CNT full pages can be used.
455 */
456 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
457 /*
458 * However, if the input buffer is not page-aligned, the
459 * portion of the first page before the buffer start
460 * offset within that page is unusable.
461 */
Rob Herringf14d54b2015-03-17 15:46:37 -0500462 xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200463 /*
464 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200465 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200466 */
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200467 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200468 /*
469 * This transfer may be shorter than the available qTD
470 * transfer size that has just been computed.
471 */
472 xfr_bytes = min(xfr_bytes, left_length);
473
474 /*
475 * Setup request qTD (3.5 in ehci-r10.pdf)
476 *
477 * qt_next ................ 03-00 H
478 * qt_altnext ............. 07-04 H
479 * qt_token ............... 0B-08 H
480 *
481 * [ buffer, buffer_hi ] loaded with "buffer".
482 */
483 qtd[qtd_counter].qt_next =
484 cpu_to_hc32(QT_NEXT_TERMINATE);
485 qtd[qtd_counter].qt_altnext =
486 cpu_to_hc32(QT_NEXT_TERMINATE);
487 token = QT_TOKEN_DT(toggle) |
488 QT_TOKEN_TOTALBYTES(xfr_bytes) |
489 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
490 QT_TOKEN_CERR(3) |
491 QT_TOKEN_PID(usb_pipein(pipe) ?
492 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
493 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
494 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
495 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
496 xfr_bytes)) {
497 printf("unable to construct DATA TD\n");
498 goto fail;
499 }
500 /* Update previous qTD! */
Marek Vasutdf0b6242016-01-23 21:04:46 +0100501 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200502 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200503 /*
504 * Data toggle has to be adjusted since the qTD transfer
505 * size is not always an even multiple of
506 * wMaxPacketSize.
507 */
508 if ((xfr_bytes / maxpacket) & 1)
509 toggle ^= 1;
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200510 buf_ptr += xfr_bytes;
511 left_length -= xfr_bytes;
512 } while (left_length > 0);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100513 }
514
515 if (req != NULL) {
Marek Vasut285c8b32012-04-09 04:13:00 +0200516 /*
517 * Setup request qTD (3.5 in ehci-r10.pdf)
518 *
519 * qt_next ................ 03-00 H
520 * qt_altnext ............. 07-04 H
521 * qt_token ............... 0B-08 H
522 */
Marek Vasut4f668312012-04-08 23:32:05 +0200523 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
524 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau4e23df12012-08-10 18:27:23 +0200525 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200526 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
527 QT_TOKEN_PID(usb_pipein(pipe) ?
528 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
529 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasut4f668312012-04-08 23:32:05 +0200530 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut285c8b32012-04-09 04:13:00 +0200531 /* Update previous qTD! */
Marek Vasutdf0b6242016-01-23 21:04:46 +0100532 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
Marek Vasut4f668312012-04-08 23:32:05 +0200533 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100534 }
535
Marek Vasutdf0b6242016-01-23 21:04:46 +0100536 ctrl->qh_list.qh_link = cpu_to_hc32(virt_to_phys(qh) | QH_LINK_TYPE_QH);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100537
Stefan Roese25983c12009-01-21 17:12:19 +0100538 /* Flush dcache */
Rob Herringf14d54b2015-03-17 15:46:37 -0500539 flush_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach3494a4c2012-09-26 00:14:35 +0200540 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herringf14d54b2015-03-17 15:46:37 -0500541 flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
542 flush_dcache_range((unsigned long)qtd,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200543 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roese25983c12009-01-21 17:12:19 +0100544
Ilya Yanok84309bb2012-07-15 22:12:08 +0000545 /* Set async. queue head pointer. */
Marek Vasutdf0b6242016-01-23 21:04:46 +0100546 ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(&ctrl->qh_list));
Ilya Yanok84309bb2012-07-15 22:12:08 +0000547
Lucas Stach3494a4c2012-09-26 00:14:35 +0200548 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
549 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100550
551 /* Enable async. schedule. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200552 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael0bf2a032008-12-11 13:43:55 +0100553 cmd |= CMD_ASE;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200554 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0a326102008-12-10 17:55:19 +0100555
Lucas Stach3494a4c2012-09-26 00:14:35 +0200556 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100557 100 * 1000);
558 if (ret < 0) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200559 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100560 goto fail;
michael0bf2a032008-12-11 13:43:55 +0100561 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100562
563 /* Wait for TDs to be processed. */
564 ts = get_timer(0);
Marek Vasut4f668312012-04-08 23:32:05 +0200565 vtd = &qtd[qtd_counter - 1];
Simon Glassfd7f5132011-02-07 14:42:16 -0800566 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100567 do {
Stefan Roese25983c12009-01-21 17:12:19 +0100568 /* Invalidate dcache */
Rob Herringf14d54b2015-03-17 15:46:37 -0500569 invalidate_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach3494a4c2012-09-26 00:14:35 +0200570 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herringf14d54b2015-03-17 15:46:37 -0500571 invalidate_dcache_range((unsigned long)qh,
Tom Rini2cabcf72012-07-15 22:14:24 +0000572 ALIGN_END_ADDR(struct QH, qh, 1));
Rob Herringf14d54b2015-03-17 15:46:37 -0500573 invalidate_dcache_range((unsigned long)qtd,
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200574 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutff24dc32012-04-09 04:07:46 +0200575
michael0a326102008-12-10 17:55:19 +0100576 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200577 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100578 break;
Stefan Roese86b34cf2010-11-26 15:43:28 +0100579 WATCHDOG_RESET();
Simon Glassfd7f5132011-02-07 14:42:16 -0800580 } while (get_timer(ts) < timeout);
581
Ilya Yanokfb113712012-07-15 04:43:49 +0000582 /*
583 * Invalidate the memory area occupied by buffer
584 * Don't try to fix the buffer alignment, if it isn't properly
585 * aligned it's upper layer's fault so let invalidate_dcache_range()
586 * vow about it. But we have to fix the length as it's actual
587 * transfer length and can be unaligned. This is potentially
588 * dangerous operation, it's responsibility of the calling
589 * code to make sure enough space is reserved.
590 */
Rob Herringf14d54b2015-03-17 15:46:37 -0500591 invalidate_dcache_range((unsigned long)buffer,
592 ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutff24dc32012-04-09 04:07:46 +0200593
Simon Glassfd7f5132011-02-07 14:42:16 -0800594 /* Check that the TD processing happened */
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200595 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glassfd7f5132011-02-07 14:42:16 -0800596 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100597
598 /* Disable async schedule. */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200599 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael0a326102008-12-10 17:55:19 +0100600 cmd &= ~CMD_ASE;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200601 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0bf2a032008-12-11 13:43:55 +0100602
Lucas Stach3494a4c2012-09-26 00:14:35 +0200603 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100604 100 * 1000);
605 if (ret < 0) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200606 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi6d4b91c2008-12-31 10:33:22 +0100607 goto fail;
michael0bf2a032008-12-11 13:43:55 +0100608 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100609
Tom Rini2cabcf72012-07-15 22:14:24 +0000610 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200611 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michael0a326102008-12-10 17:55:19 +0100612 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200613 switch (QT_TOKEN_GET_STATUS(token) &
614 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100615 case 0:
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200616 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100617 usb_settoggle(dev, usb_pipeendpoint(pipe),
618 usb_pipeout(pipe), toggle);
619 dev->status = 0;
620 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200621 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100622 dev->status = USB_ST_STALLED;
623 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200624 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
625 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100626 dev->status = USB_ST_BUF_ERR;
627 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200628 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
629 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchi241f7512008-11-28 13:20:46 +0100630 dev->status = USB_ST_BABBLE_DET;
631 break;
632 default:
633 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200634 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschine1e09312010-11-02 11:47:29 +0100635 dev->status |= USB_ST_STALLED;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100636 break;
637 }
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200638 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100639 } else {
640 dev->act_len = 0;
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800641#ifndef CONFIG_USB_EHCI_FARADAY
michael0a326102008-12-10 17:55:19 +0100642 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach3494a4c2012-09-26 00:14:35 +0200643 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
644 ehci_readl(&ctrl->hcor->or_portsc[0]),
645 ehci_readl(&ctrl->hcor->or_portsc[1]));
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +0800646#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +0100647 }
648
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200649 free(qtd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100650 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
651
652fail:
Benoît Thébaudeaub39f8b52012-08-10 18:22:32 +0200653 free(qtd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100654 return -1;
655}
656
Simon Glasscb7cf602015-03-25 12:22:25 -0600657static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
658 void *buffer, int length, struct devrequest *req)
Michael Trimarchi241f7512008-11-28 13:20:46 +0100659{
660 uint8_t tmpbuf[4];
661 u16 typeReq;
michael0a326102008-12-10 17:55:19 +0100662 void *srcptr = NULL;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100663 int len, srclen;
664 uint32_t reg;
Remy Böhmer33e87482008-12-13 22:51:58 +0100665 uint32_t *status_reg;
Julius Wernerd4046702013-02-28 18:08:40 +0000666 int port = le16_to_cpu(req->index) & 0xff;
Simon Glasscb7cf602015-03-25 12:22:25 -0600667 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100668
669 srclen = 0;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100670
michael0a326102008-12-10 17:55:19 +0100671 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100672 req->request, req->request,
673 req->requesttype, req->requesttype,
674 le16_to_cpu(req->value), le16_to_cpu(req->index));
675
Prafulla Wadaskar22810292009-07-17 19:56:30 +0530676 typeReq = req->request | req->requesttype << 8;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100677
Prafulla Wadaskar22810292009-07-17 19:56:30 +0530678 switch (typeReq) {
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800679 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
680 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
681 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600682 status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
Kuo-Jung Su6a656df2013-05-15 15:29:21 +0800683 if (!status_reg)
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800684 return -1;
Kuo-Jung Su9930e9f2013-05-15 15:29:20 +0800685 break;
686 default:
687 status_reg = NULL;
688 break;
689 }
690
691 switch (typeReq) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100692 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
693 switch (le16_to_cpu(req->value) >> 8) {
694 case USB_DT_DEVICE:
michael0a326102008-12-10 17:55:19 +0100695 debug("USB_DT_DEVICE request\n");
696 srcptr = &descriptor.device;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200697 srclen = descriptor.device.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100698 break;
699 case USB_DT_CONFIG:
michael0a326102008-12-10 17:55:19 +0100700 debug("USB_DT_CONFIG config\n");
701 srcptr = &descriptor.config;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200702 srclen = descriptor.config.bLength +
703 descriptor.interface.bLength +
704 descriptor.endpoint.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100705 break;
706 case USB_DT_STRING:
michael0a326102008-12-10 17:55:19 +0100707 debug("USB_DT_STRING config\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100708 switch (le16_to_cpu(req->value) & 0xff) {
709 case 0: /* Language */
710 srcptr = "\4\3\1\0";
711 srclen = 4;
712 break;
713 case 1: /* Vendor */
714 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
715 srclen = 14;
716 break;
717 case 2: /* Product */
718 srcptr = "\52\3E\0H\0C\0I\0 "
719 "\0H\0o\0s\0t\0 "
720 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
721 srclen = 42;
722 break;
723 default:
michael0a326102008-12-10 17:55:19 +0100724 debug("unknown value DT_STRING %x\n",
725 le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100726 goto unknown;
727 }
728 break;
729 default:
michael0a326102008-12-10 17:55:19 +0100730 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100731 goto unknown;
732 }
733 break;
734 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
735 switch (le16_to_cpu(req->value) >> 8) {
736 case USB_DT_HUB:
michael0a326102008-12-10 17:55:19 +0100737 debug("USB_DT_HUB config\n");
738 srcptr = &descriptor.hub;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200739 srclen = descriptor.hub.bLength;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100740 break;
741 default:
michael0a326102008-12-10 17:55:19 +0100742 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100743 goto unknown;
744 }
745 break;
746 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michael0a326102008-12-10 17:55:19 +0100747 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach3494a4c2012-09-26 00:14:35 +0200748 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100749 break;
750 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michael0a326102008-12-10 17:55:19 +0100751 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100752 /* Nothing to do */
753 break;
754 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
755 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
756 tmpbuf[1] = 0;
757 srcptr = tmpbuf;
758 srclen = 2;
759 break;
michael0a326102008-12-10 17:55:19 +0100760 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchi241f7512008-11-28 13:20:46 +0100761 memset(tmpbuf, 0, 4);
Remy Böhmer33e87482008-12-13 22:51:58 +0100762 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100763 if (reg & EHCI_PS_CS)
764 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
765 if (reg & EHCI_PS_PE)
766 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
767 if (reg & EHCI_PS_SUSP)
768 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
769 if (reg & EHCI_PS_OCA)
770 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300771 if (reg & EHCI_PS_PR)
772 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100773 if (reg & EHCI_PS_PP)
774 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese497f1842009-01-21 17:12:01 +0100775
776 if (ehci_is_TDI()) {
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600777 switch (ctrl->ops.get_port_speed(ctrl, reg)) {
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200778 case PORTSC_PSPD_FS:
Stefan Roese497f1842009-01-21 17:12:01 +0100779 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200780 case PORTSC_PSPD_LS:
Stefan Roese497f1842009-01-21 17:12:01 +0100781 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
782 break;
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200783 case PORTSC_PSPD_HS:
Stefan Roese497f1842009-01-21 17:12:01 +0100784 default:
785 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
786 break;
787 }
788 } else {
789 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
790 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100791
792 if (reg & EHCI_PS_CSC)
793 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
794 if (reg & EHCI_PS_PEC)
795 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
796 if (reg & EHCI_PS_OCC)
797 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Julius Wernerd4046702013-02-28 18:08:40 +0000798 if (ctrl->portreset & (1 << port))
Michael Trimarchi241f7512008-11-28 13:20:46 +0100799 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmer33e87482008-12-13 22:51:58 +0100800
Michael Trimarchi241f7512008-11-28 13:20:46 +0100801 srcptr = tmpbuf;
802 srclen = 4;
803 break;
michael0a326102008-12-10 17:55:19 +0100804 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmer33e87482008-12-13 22:51:58 +0100805 reg = ehci_readl(status_reg);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100806 reg &= ~EHCI_PS_CLEAR;
807 switch (le16_to_cpu(req->value)) {
michael0bf2a032008-12-11 13:43:55 +0100808 case USB_PORT_FEAT_ENABLE:
809 reg |= EHCI_PS_PE;
Remy Böhmer33e87482008-12-13 22:51:58 +0100810 ehci_writel(status_reg, reg);
michael0bf2a032008-12-11 13:43:55 +0100811 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100812 case USB_PORT_FEAT_POWER:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200813 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmer33e87482008-12-13 22:51:58 +0100814 reg |= EHCI_PS_PP;
815 ehci_writel(status_reg, reg);
816 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100817 break;
818 case USB_PORT_FEAT_RESET:
Remy Böhmer33e87482008-12-13 22:51:58 +0100819 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
820 !ehci_is_TDI() &&
821 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchi241f7512008-11-28 13:20:46 +0100822 /* Low speed device, give up ownership. */
Remy Böhmer33e87482008-12-13 22:51:58 +0100823 debug("port %d low speed --> companion\n",
Julius Wernerd4046702013-02-28 18:08:40 +0000824 port - 1);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100825 reg |= EHCI_PS_PO;
Remy Böhmer33e87482008-12-13 22:51:58 +0100826 ehci_writel(status_reg, reg);
Hans de Goede63f34ca2015-05-10 14:10:16 +0200827 return -ENXIO;
Remy Böhmer33e87482008-12-13 22:51:58 +0100828 } else {
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300829 int ret;
830
Remy Böhmer33e87482008-12-13 22:51:58 +0100831 reg |= EHCI_PS_PR;
832 reg &= ~EHCI_PS_PE;
833 ehci_writel(status_reg, reg);
834 /*
835 * caller must wait, then call GetPortStatus
836 * usb 2.0 specification say 50 ms resets on
837 * root
838 */
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600839 ctrl->ops.powerup_fixup(ctrl, status_reg, &reg);
Marek Vasut09734772011-07-11 02:37:01 +0200840
Chris Zhangfddf6d62010-01-06 13:34:04 -0800841 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300842 /*
843 * A host controller must terminate the reset
844 * and stabilize the state of the port within
845 * 2 milliseconds
846 */
847 ret = handshake(status_reg, EHCI_PS_PR, 0,
848 2 * 1000);
Hans de Goedeb5b3ef22015-05-10 14:10:13 +0200849 if (!ret) {
850 reg = ehci_readl(status_reg);
851 if ((reg & (EHCI_PS_PE | EHCI_PS_CS))
852 == EHCI_PS_CS && !ehci_is_TDI()) {
853 debug("port %d full speed --> companion\n", port - 1);
854 reg &= ~EHCI_PS_CLEAR;
855 reg |= EHCI_PS_PO;
856 ehci_writel(status_reg, reg);
Hans de Goede63f34ca2015-05-10 14:10:16 +0200857 return -ENXIO;
Hans de Goedeb5b3ef22015-05-10 14:10:13 +0200858 } else {
859 ctrl->portreset |= 1 << port;
860 }
861 } else {
Sergei Shtylyov23dec682010-02-27 21:33:21 +0300862 printf("port(%d) reset error\n",
Julius Wernerd4046702013-02-28 18:08:40 +0000863 port - 1);
Hans de Goedeb5b3ef22015-05-10 14:10:13 +0200864 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100865 }
Michael Trimarchi241f7512008-11-28 13:20:46 +0100866 break;
Julius Wernerd4046702013-02-28 18:08:40 +0000867 case USB_PORT_FEAT_TEST:
Julius Werner5c1a1ad2013-09-24 10:53:07 -0700868 ehci_shutdown(ctrl);
Julius Wernerd4046702013-02-28 18:08:40 +0000869 reg &= ~(0xf << 16);
870 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
871 ehci_writel(status_reg, reg);
872 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100873 default:
michael0a326102008-12-10 17:55:19 +0100874 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100875 goto unknown;
876 }
Remy Böhmer33e87482008-12-13 22:51:58 +0100877 /* unblock posted writes */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200878 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100879 break;
michael0a326102008-12-10 17:55:19 +0100880 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmer33e87482008-12-13 22:51:58 +0100881 reg = ehci_readl(status_reg);
Simon Glass0554ba52013-05-10 19:49:00 -0700882 reg &= ~EHCI_PS_CLEAR;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100883 switch (le16_to_cpu(req->value)) {
884 case USB_PORT_FEAT_ENABLE:
885 reg &= ~EHCI_PS_PE;
886 break;
Remy Böhmer33e87482008-12-13 22:51:58 +0100887 case USB_PORT_FEAT_C_ENABLE:
Simon Glass0554ba52013-05-10 19:49:00 -0700888 reg |= EHCI_PS_PE;
Remy Böhmer33e87482008-12-13 22:51:58 +0100889 break;
890 case USB_PORT_FEAT_POWER:
Lucas Stach3494a4c2012-09-26 00:14:35 +0200891 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Simon Glass0554ba52013-05-10 19:49:00 -0700892 reg &= ~EHCI_PS_PP;
893 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100894 case USB_PORT_FEAT_C_CONNECTION:
Simon Glass0554ba52013-05-10 19:49:00 -0700895 reg |= EHCI_PS_CSC;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100896 break;
michael0bf2a032008-12-11 13:43:55 +0100897 case USB_PORT_FEAT_OVER_CURRENT:
Simon Glass0554ba52013-05-10 19:49:00 -0700898 reg |= EHCI_PS_OCC;
michael0bf2a032008-12-11 13:43:55 +0100899 break;
Michael Trimarchi241f7512008-11-28 13:20:46 +0100900 case USB_PORT_FEAT_C_RESET:
Julius Wernerd4046702013-02-28 18:08:40 +0000901 ctrl->portreset &= ~(1 << port);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100902 break;
903 default:
michael0a326102008-12-10 17:55:19 +0100904 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchi241f7512008-11-28 13:20:46 +0100905 goto unknown;
906 }
Remy Böhmer33e87482008-12-13 22:51:58 +0100907 ehci_writel(status_reg, reg);
908 /* unblock posted write */
Lucas Stach3494a4c2012-09-26 00:14:35 +0200909 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100910 break;
911 default:
michael0a326102008-12-10 17:55:19 +0100912 debug("Unknown request\n");
Michael Trimarchi241f7512008-11-28 13:20:46 +0100913 goto unknown;
914 }
915
Mike Frysinger60ce19a2012-03-05 13:47:00 +0000916 mdelay(1);
Masahiro Yamadadb204642014-11-07 03:03:31 +0900917 len = min3(srclen, (int)le16_to_cpu(req->length), length);
Michael Trimarchi241f7512008-11-28 13:20:46 +0100918 if (srcptr != NULL && len > 0)
919 memcpy(buffer, srcptr, len);
michael0a326102008-12-10 17:55:19 +0100920 else
921 debug("Len is 0\n");
922
Michael Trimarchi241f7512008-11-28 13:20:46 +0100923 dev->act_len = len;
924 dev->status = 0;
925 return 0;
926
927unknown:
michael0a326102008-12-10 17:55:19 +0100928 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchi241f7512008-11-28 13:20:46 +0100929 req->requesttype, req->request, le16_to_cpu(req->value),
930 le16_to_cpu(req->index), le16_to_cpu(req->length));
931
932 dev->act_len = 0;
933 dev->status = USB_ST_STALLED;
934 return -1;
935}
936
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600937const struct ehci_ops default_ehci_ops = {
938 .set_usb_mode = ehci_set_usbmode,
939 .get_port_speed = ehci_get_port_speed,
940 .powerup_fixup = ehci_powerup_fixup,
941 .get_portsc_register = ehci_get_portsc_register,
942};
943
944static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
Simon Glass0851caa2015-03-25 12:22:19 -0600945{
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600946 if (!ops) {
947 ctrl->ops = default_ehci_ops;
948 } else {
949 ctrl->ops = *ops;
950 if (!ctrl->ops.set_usb_mode)
951 ctrl->ops.set_usb_mode = ehci_set_usbmode;
952 if (!ctrl->ops.get_port_speed)
953 ctrl->ops.get_port_speed = ehci_get_port_speed;
954 if (!ctrl->ops.powerup_fixup)
955 ctrl->ops.powerup_fixup = ehci_powerup_fixup;
956 if (!ctrl->ops.get_portsc_register)
957 ctrl->ops.get_portsc_register =
958 ehci_get_portsc_register;
959 }
Simon Glass0851caa2015-03-25 12:22:19 -0600960}
961
Simon Glassa194b252015-03-25 12:22:29 -0600962#ifndef CONFIG_DM_USB
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600963void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
964{
965 struct ehci_ctrl *ctrl = &ehcic[index];
966
967 ctrl->priv = priv;
968 ehci_setup_ops(ctrl, ops);
969}
970
Simon Glass0851caa2015-03-25 12:22:19 -0600971void *ehci_get_controller_priv(int index)
972{
973 return ehcic[index].priv;
974}
Simon Glassa194b252015-03-25 12:22:29 -0600975#endif
Simon Glass0851caa2015-03-25 12:22:19 -0600976
Simon Glassccc40fd2015-03-25 12:22:26 -0600977static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
Michael Trimarchi241f7512008-11-28 13:20:46 +0100978{
Lucas Stach3494a4c2012-09-26 00:14:35 +0200979 struct QH *qh_list;
Patrick Georgie55fdac2013-03-06 14:08:31 +0000980 struct QH *periodic;
Simon Glassccc40fd2015-03-25 12:22:26 -0600981 uint32_t reg;
982 uint32_t cmd;
Patrick Georgie55fdac2013-03-06 14:08:31 +0000983 int i;
michael0bf2a032008-12-11 13:43:55 +0100984
Vincent Palatin0d6f77c2012-12-12 17:55:22 -0800985 /* Set the high address word (aka segment) for 64-bit controller */
Simon Glassccc40fd2015-03-25 12:22:26 -0600986 if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
987 ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
Stefan Roese2e98fc72009-01-21 17:12:10 +0100988
Simon Glassccc40fd2015-03-25 12:22:26 -0600989 qh_list = &ctrl->qh_list;
Lucas Stach3494a4c2012-09-26 00:14:35 +0200990
Michael Trimarchi241f7512008-11-28 13:20:46 +0100991 /* Set head of reclaim list */
Tom Rini2cabcf72012-07-15 22:14:24 +0000992 memset(qh_list, 0, sizeof(*qh_list));
Marek Vasutdf0b6242016-01-23 21:04:46 +0100993 qh_list->qh_link = cpu_to_hc32(virt_to_phys(qh_list) | QH_LINK_TYPE_QH);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200994 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
995 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini2cabcf72012-07-15 22:14:24 +0000996 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
997 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau458fb1e2012-08-10 18:22:11 +0200998 qh_list->qh_overlay.qt_token =
999 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchi241f7512008-11-28 13:20:46 +01001000
Rob Herringf14d54b2015-03-17 15:46:37 -05001001 flush_dcache_range((unsigned long)qh_list,
Stephen Warren36dad662013-05-24 15:03:17 -06001002 ALIGN_END_ADDR(struct QH, qh_list, 1));
1003
Patrick Georgie55fdac2013-03-06 14:08:31 +00001004 /* Set async. queue head pointer. */
Marek Vasutdf0b6242016-01-23 21:04:46 +01001005 ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(qh_list));
Patrick Georgie55fdac2013-03-06 14:08:31 +00001006
1007 /*
1008 * Set up periodic list
1009 * Step 1: Parent QH for all periodic transfers.
1010 */
Simon Glassccc40fd2015-03-25 12:22:26 -06001011 ctrl->periodic_schedules = 0;
1012 periodic = &ctrl->periodic_queue;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001013 memset(periodic, 0, sizeof(*periodic));
1014 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
1015 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1016 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1017
Rob Herringf14d54b2015-03-17 15:46:37 -05001018 flush_dcache_range((unsigned long)periodic,
Stephen Warren36dad662013-05-24 15:03:17 -06001019 ALIGN_END_ADDR(struct QH, periodic, 1));
1020
Patrick Georgie55fdac2013-03-06 14:08:31 +00001021 /*
1022 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
1023 * In particular, device specifications on polling frequency
1024 * are disregarded. Keyboards seem to send NAK/NYet reliably
1025 * when polled with an empty buffer.
1026 *
1027 * Split Transactions will be spread across microframes using
1028 * S-mask and C-mask.
1029 */
Simon Glassccc40fd2015-03-25 12:22:26 -06001030 if (ctrl->periodic_list == NULL)
1031 ctrl->periodic_list = memalign(4096, 1024 * 4);
Nikita Kiryanov2f13e442013-07-29 13:27:40 +03001032
Simon Glassccc40fd2015-03-25 12:22:26 -06001033 if (!ctrl->periodic_list)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001034 return -ENOMEM;
1035 for (i = 0; i < 1024; i++) {
Simon Glassccc40fd2015-03-25 12:22:26 -06001036 ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
Adrian Cox29d05872014-04-10 13:29:45 +01001037 | QH_LINK_TYPE_QH);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001038 }
1039
Simon Glassccc40fd2015-03-25 12:22:26 -06001040 flush_dcache_range((unsigned long)ctrl->periodic_list,
1041 ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
Stephen Warren36dad662013-05-24 15:03:17 -06001042 1024));
1043
Patrick Georgie55fdac2013-03-06 14:08:31 +00001044 /* Set periodic list base address */
Simon Glassccc40fd2015-03-25 12:22:26 -06001045 ehci_writel(&ctrl->hcor->or_periodiclistbase,
1046 (unsigned long)ctrl->periodic_list);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001047
Simon Glassccc40fd2015-03-25 12:22:26 -06001048 reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
michael0bf2a032008-12-11 13:43:55 +01001049 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stachf5b34082012-09-28 00:26:19 +02001050 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmer33e87482008-12-13 22:51:58 +01001051 /* Port Indicators */
1052 if (HCS_INDICATOR(reg))
Lucas Stach835e11e2012-09-06 08:00:13 +02001053 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1054 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmer33e87482008-12-13 22:51:58 +01001055 /* Port Power Control */
1056 if (HCS_PPC(reg))
Lucas Stach835e11e2012-09-06 08:00:13 +02001057 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1058 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchi241f7512008-11-28 13:20:46 +01001059
Michael Trimarchi241f7512008-11-28 13:20:46 +01001060 /* Start the host controller. */
Simon Glassccc40fd2015-03-25 12:22:26 -06001061 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Wolfgang Denkfb718e12009-02-12 00:08:39 +01001062 /*
1063 * Philips, Intel, and maybe others need CMD_RUN before the
1064 * root hub will detect new devices (why?); NEC doesn't
1065 */
michael0bf2a032008-12-11 13:43:55 +01001066 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
1067 cmd |= CMD_RUN;
Simon Glassccc40fd2015-03-25 12:22:26 -06001068 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael0bf2a032008-12-11 13:43:55 +01001069
Simon Glassccc40fd2015-03-25 12:22:26 -06001070 if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
1071 /* take control over the ports */
1072 cmd = ehci_readl(&ctrl->hcor->or_configflag);
1073 cmd |= FLAG_CF;
1074 ehci_writel(&ctrl->hcor->or_configflag, cmd);
1075 }
Kuo-Jung Sub5d59de2013-05-15 15:29:23 +08001076
Remy Böhmer33e87482008-12-13 22:51:58 +01001077 /* unblock posted write */
Simon Glassccc40fd2015-03-25 12:22:26 -06001078 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Mike Frysinger60ce19a2012-03-05 13:47:00 +00001079 mdelay(5);
Simon Glassccc40fd2015-03-25 12:22:26 -06001080 reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
Remy Böhmer33e87482008-12-13 22:51:58 +01001081 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchi241f7512008-11-28 13:20:46 +01001082
Simon Glassccc40fd2015-03-25 12:22:26 -06001083 return 0;
1084}
1085
Simon Glassa194b252015-03-25 12:22:29 -06001086#ifndef CONFIG_DM_USB
Simon Glassccc40fd2015-03-25 12:22:26 -06001087int usb_lowlevel_stop(int index)
1088{
1089 ehci_shutdown(&ehcic[index]);
1090 return ehci_hcd_stop(index);
1091}
1092
1093int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1094{
1095 struct ehci_ctrl *ctrl = &ehcic[index];
1096 uint tweaks = 0;
1097 int rc;
1098
Simon Glassdc9f3ed2015-03-25 12:22:27 -06001099 /**
1100 * Set ops to default_ehci_ops, ehci_hcd_init should call
1101 * ehci_set_controller_priv to change any of these function pointers.
1102 */
1103 ctrl->ops = default_ehci_ops;
1104
Simon Glassccc40fd2015-03-25 12:22:26 -06001105 rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
1106 if (rc)
1107 return rc;
1108 if (init == USB_INIT_DEVICE)
1109 goto done;
1110
1111 /* EHCI spec section 4.1 */
Simon Glass302696b2015-03-25 12:22:28 -06001112 if (ehci_reset(ctrl))
Simon Glassccc40fd2015-03-25 12:22:26 -06001113 return -1;
1114
1115#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
1116 rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
1117 if (rc)
1118 return rc;
1119#endif
1120#ifdef CONFIG_USB_EHCI_FARADAY
1121 tweaks |= EHCI_TWEAK_NO_INIT_CF;
1122#endif
1123 rc = ehci_common_init(ctrl, tweaks);
1124 if (rc)
1125 return rc;
1126
1127 ctrl->rootdev = 0;
Troy Kisky7d6bbb92013-10-10 15:27:57 -07001128done:
Lucas Stach3494a4c2012-09-26 00:14:35 +02001129 *controller = &ehcic[index];
Michael Trimarchi241f7512008-11-28 13:20:46 +01001130 return 0;
1131}
Simon Glassa194b252015-03-25 12:22:29 -06001132#endif
Michael Trimarchi241f7512008-11-28 13:20:46 +01001133
Simon Glasscb7cf602015-03-25 12:22:25 -06001134static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
1135 void *buffer, int length)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001136{
1137
1138 if (usb_pipetype(pipe) != PIPE_BULK) {
1139 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1140 return -1;
1141 }
1142 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1143}
1144
Simon Glasscb7cf602015-03-25 12:22:25 -06001145static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
1146 void *buffer, int length,
1147 struct devrequest *setup)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001148{
Simon Glasscb7cf602015-03-25 12:22:25 -06001149 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchi241f7512008-11-28 13:20:46 +01001150
1151 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1152 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1153 return -1;
1154 }
1155
Lucas Stach3494a4c2012-09-26 00:14:35 +02001156 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1157 if (!ctrl->rootdev)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001158 dev->speed = USB_SPEED_HIGH;
1159 return ehci_submit_root(dev, pipe, buffer, length, setup);
1160 }
1161 return ehci_submit_async(dev, pipe, buffer, length, setup);
1162}
1163
Patrick Georgie55fdac2013-03-06 14:08:31 +00001164struct int_queue {
Hans de Goede8c5c5ca2014-09-24 14:06:05 +02001165 int elementsize;
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001166 unsigned long pipe;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001167 struct QH *first;
1168 struct QH *current;
1169 struct QH *last;
1170 struct qTD *tds;
1171};
1172
Rob Herringf14d54b2015-03-17 15:46:37 -05001173#define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001174
1175static int
1176enable_periodic(struct ehci_ctrl *ctrl)
1177{
1178 uint32_t cmd;
1179 struct ehci_hcor *hcor = ctrl->hcor;
1180 int ret;
1181
1182 cmd = ehci_readl(&hcor->or_usbcmd);
1183 cmd |= CMD_PSE;
1184 ehci_writel(&hcor->or_usbcmd, cmd);
1185
1186 ret = handshake((uint32_t *)&hcor->or_usbsts,
1187 STS_PSS, STS_PSS, 100 * 1000);
1188 if (ret < 0) {
1189 printf("EHCI failed: timeout when enabling periodic list\n");
1190 return -ETIMEDOUT;
1191 }
1192 udelay(1000);
1193 return 0;
1194}
1195
1196static int
1197disable_periodic(struct ehci_ctrl *ctrl)
1198{
1199 uint32_t cmd;
1200 struct ehci_hcor *hcor = ctrl->hcor;
1201 int ret;
1202
1203 cmd = ehci_readl(&hcor->or_usbcmd);
1204 cmd &= ~CMD_PSE;
1205 ehci_writel(&hcor->or_usbcmd, cmd);
1206
1207 ret = handshake((uint32_t *)&hcor->or_usbsts,
1208 STS_PSS, 0, 100 * 1000);
1209 if (ret < 0) {
1210 printf("EHCI failed: timeout when disabling periodic list\n");
1211 return -ETIMEDOUT;
1212 }
1213 return 0;
1214}
1215
Hans de Goede53ca9de2015-05-11 20:43:52 +02001216static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
1217 unsigned long pipe, int queuesize, int elementsize,
1218 void *buffer, int interval)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001219{
Simon Glasscb7cf602015-03-25 12:22:25 -06001220 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001221 struct int_queue *result = NULL;
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001222 uint32_t i, toggle;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001223
Hans de Goede7f7cb732014-09-24 14:06:04 +02001224 /*
1225 * Interrupt transfers requiring several transactions are not supported
1226 * because bInterval is ignored.
1227 *
1228 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1229 * <= PKT_ALIGN if several qTDs are required, while the USB
1230 * specification does not constrain this for interrupt transfers. That
1231 * means that ehci_submit_async() would support interrupt transfers
1232 * requiring several transactions only as long as the transfer size does
1233 * not require more than a single qTD.
1234 */
1235 if (elementsize > usb_maxpacket(dev, pipe)) {
1236 printf("%s: xfers requiring several transactions are not supported.\n",
1237 __func__);
1238 return NULL;
1239 }
1240
Patrick Georgie55fdac2013-03-06 14:08:31 +00001241 debug("Enter create_int_queue\n");
1242 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1243 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1244 return NULL;
1245 }
1246
1247 /* limit to 4 full pages worth of data -
1248 * we can safely fit them in a single TD,
1249 * no matter the alignment
1250 */
1251 if (elementsize >= 16384) {
1252 debug("too large elements for interrupt transfers\n");
1253 return NULL;
1254 }
1255
1256 result = malloc(sizeof(*result));
1257 if (!result) {
1258 debug("ehci intr queue: out of memory\n");
1259 goto fail1;
1260 }
Hans de Goede8c5c5ca2014-09-24 14:06:05 +02001261 result->elementsize = elementsize;
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001262 result->pipe = pipe;
Stephen Warrend7fe61d2014-02-06 13:13:06 -07001263 result->first = memalign(USB_DMA_MINALIGN,
1264 sizeof(struct QH) * queuesize);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001265 if (!result->first) {
1266 debug("ehci intr queue: out of memory\n");
1267 goto fail2;
1268 }
1269 result->current = result->first;
1270 result->last = result->first + queuesize - 1;
Stephen Warrend7fe61d2014-02-06 13:13:06 -07001271 result->tds = memalign(USB_DMA_MINALIGN,
1272 sizeof(struct qTD) * queuesize);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001273 if (!result->tds) {
1274 debug("ehci intr queue: out of memory\n");
1275 goto fail3;
1276 }
1277 memset(result->first, 0, sizeof(struct QH) * queuesize);
1278 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1279
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001280 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
1281
Patrick Georgie55fdac2013-03-06 14:08:31 +00001282 for (i = 0; i < queuesize; i++) {
1283 struct QH *qh = result->first + i;
1284 struct qTD *td = result->tds + i;
1285 void **buf = &qh->buffer;
1286
Rob Herringf14d54b2015-03-17 15:46:37 -05001287 qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001288 if (i == queuesize - 1)
Adrian Cox29d05872014-04-10 13:29:45 +01001289 qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001290
Rob Herringf14d54b2015-03-17 15:46:37 -05001291 qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
Adrian Cox29d05872014-04-10 13:29:45 +01001292 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1293 qh->qh_endpt1 =
1294 cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
Patrick Georgie55fdac2013-03-06 14:08:31 +00001295 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1296 (1 << 14) |
1297 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1298 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
Adrian Cox29d05872014-04-10 13:29:45 +01001299 (usb_pipedevice(pipe) << 0));
1300 qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1301 (1 << 0)); /* S-mask: microframe 0 */
Patrick Georgie55fdac2013-03-06 14:08:31 +00001302 if (dev->speed == USB_SPEED_LOW ||
1303 dev->speed == USB_SPEED_FULL) {
Hans de Goededa166772014-09-20 16:51:22 +02001304 /* C-mask: microframes 2-4 */
1305 qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
Patrick Georgie55fdac2013-03-06 14:08:31 +00001306 }
Hans de Goededa166772014-09-20 16:51:22 +02001307 ehci_update_endpt2_dev_n_port(dev, qh);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001308
Adrian Cox29d05872014-04-10 13:29:45 +01001309 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1310 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001311 debug("communication direction is '%s'\n",
1312 usb_pipein(pipe) ? "in" : "out");
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001313 td->qt_token = cpu_to_hc32(
1314 QT_TOKEN_DT(toggle) |
1315 (elementsize << 16) |
Patrick Georgie55fdac2013-03-06 14:08:31 +00001316 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
Adrian Cox29d05872014-04-10 13:29:45 +01001317 0x80); /* active */
1318 td->qt_buffer[0] =
Rob Herringf14d54b2015-03-17 15:46:37 -05001319 cpu_to_hc32((unsigned long)buffer + i * elementsize);
Adrian Cox29d05872014-04-10 13:29:45 +01001320 td->qt_buffer[1] =
1321 cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1322 td->qt_buffer[2] =
1323 cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1324 td->qt_buffer[3] =
1325 cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1326 td->qt_buffer[4] =
1327 cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001328
1329 *buf = buffer + i * elementsize;
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001330 toggle ^= 1;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001331 }
1332
Rob Herringf14d54b2015-03-17 15:46:37 -05001333 flush_dcache_range((unsigned long)buffer,
Stephen Warren36dad662013-05-24 15:03:17 -06001334 ALIGN_END_ADDR(char, buffer,
1335 queuesize * elementsize));
Rob Herringf14d54b2015-03-17 15:46:37 -05001336 flush_dcache_range((unsigned long)result->first,
Stephen Warren36dad662013-05-24 15:03:17 -06001337 ALIGN_END_ADDR(struct QH, result->first,
1338 queuesize));
Rob Herringf14d54b2015-03-17 15:46:37 -05001339 flush_dcache_range((unsigned long)result->tds,
Stephen Warren36dad662013-05-24 15:03:17 -06001340 ALIGN_END_ADDR(struct qTD, result->tds,
1341 queuesize));
1342
Hans de Goede8ba55ed2014-09-24 14:06:03 +02001343 if (ctrl->periodic_schedules > 0) {
1344 if (disable_periodic(ctrl) < 0) {
1345 debug("FATAL: periodic should never fail, but did");
1346 goto fail3;
1347 }
Patrick Georgie55fdac2013-03-06 14:08:31 +00001348 }
1349
1350 /* hook up to periodic list */
1351 struct QH *list = &ctrl->periodic_queue;
1352 result->last->qh_link = list->qh_link;
Rob Herringf14d54b2015-03-17 15:46:37 -05001353 list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001354
Rob Herringf14d54b2015-03-17 15:46:37 -05001355 flush_dcache_range((unsigned long)result->last,
Stephen Warren36dad662013-05-24 15:03:17 -06001356 ALIGN_END_ADDR(struct QH, result->last, 1));
Rob Herringf14d54b2015-03-17 15:46:37 -05001357 flush_dcache_range((unsigned long)list,
Stephen Warren36dad662013-05-24 15:03:17 -06001358 ALIGN_END_ADDR(struct QH, list, 1));
1359
Patrick Georgie55fdac2013-03-06 14:08:31 +00001360 if (enable_periodic(ctrl) < 0) {
1361 debug("FATAL: periodic should never fail, but did");
1362 goto fail3;
1363 }
Hans de Goede8f5f4f72014-09-20 16:51:25 +02001364 ctrl->periodic_schedules++;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001365
1366 debug("Exit create_int_queue\n");
1367 return result;
1368fail3:
1369 if (result->tds)
1370 free(result->tds);
1371fail2:
1372 if (result->first)
1373 free(result->first);
1374 if (result)
1375 free(result);
1376fail1:
1377 return NULL;
1378}
1379
Hans de Goede53ca9de2015-05-11 20:43:52 +02001380static void *_ehci_poll_int_queue(struct usb_device *dev,
1381 struct int_queue *queue)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001382{
1383 struct QH *cur = queue->current;
Hans de Goede9db174c2014-09-20 16:51:24 +02001384 struct qTD *cur_td;
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001385 uint32_t token, toggle;
1386 unsigned long pipe = queue->pipe;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001387
1388 /* depleted queue */
1389 if (cur == NULL) {
1390 debug("Exit poll_int_queue with completed queue\n");
1391 return NULL;
1392 }
1393 /* still active */
Hans de Goede9db174c2014-09-20 16:51:24 +02001394 cur_td = &queue->tds[queue->current - queue->first];
Rob Herringf14d54b2015-03-17 15:46:37 -05001395 invalidate_dcache_range((unsigned long)cur_td,
Hans de Goede9db174c2014-09-20 16:51:24 +02001396 ALIGN_END_ADDR(struct qTD, cur_td, 1));
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001397 token = hc32_to_cpu(cur_td->qt_token);
1398 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) {
1399 debug("Exit poll_int_queue with no completed intr transfer. token is %x\n", token);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001400 return NULL;
1401 }
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001402
1403 toggle = QT_TOKEN_GET_DT(token);
1404 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle);
1405
Patrick Georgie55fdac2013-03-06 14:08:31 +00001406 if (!(cur->qh_link & QH_LINK_TERMINATE))
1407 queue->current++;
1408 else
1409 queue->current = NULL;
Hans de Goede8c5c5ca2014-09-24 14:06:05 +02001410
Rob Herringf14d54b2015-03-17 15:46:37 -05001411 invalidate_dcache_range((unsigned long)cur->buffer,
Hans de Goede8c5c5ca2014-09-24 14:06:05 +02001412 ALIGN_END_ADDR(char, cur->buffer,
1413 queue->elementsize));
1414
Hans de Goede9db174c2014-09-20 16:51:24 +02001415 debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
Hans de Goede61a5a1c2015-06-18 22:34:33 +02001416 token, cur, queue->first);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001417 return cur->buffer;
1418}
1419
1420/* Do not free buffers associated with QHs, they're owned by someone else */
Hans de Goede53ca9de2015-05-11 20:43:52 +02001421static int _ehci_destroy_int_queue(struct usb_device *dev,
1422 struct int_queue *queue)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001423{
Simon Glasscb7cf602015-03-25 12:22:25 -06001424 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001425 int result = -1;
1426 unsigned long timeout;
1427
1428 if (disable_periodic(ctrl) < 0) {
1429 debug("FATAL: periodic should never fail, but did");
1430 goto out;
1431 }
Hans de Goede8f5f4f72014-09-20 16:51:25 +02001432 ctrl->periodic_schedules--;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001433
1434 struct QH *cur = &ctrl->periodic_queue;
1435 timeout = get_timer(0) + 500; /* abort after 500ms */
Adrian Cox29d05872014-04-10 13:29:45 +01001436 while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
Patrick Georgie55fdac2013-03-06 14:08:31 +00001437 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1438 if (NEXT_QH(cur) == queue->first) {
1439 debug("found candidate. removing from chain\n");
1440 cur->qh_link = queue->last->qh_link;
Rob Herringf14d54b2015-03-17 15:46:37 -05001441 flush_dcache_range((unsigned long)cur,
Hans de Goede8e00cf62014-09-20 16:51:23 +02001442 ALIGN_END_ADDR(struct QH, cur, 1));
Patrick Georgie55fdac2013-03-06 14:08:31 +00001443 result = 0;
1444 break;
1445 }
1446 cur = NEXT_QH(cur);
1447 if (get_timer(0) > timeout) {
1448 printf("Timeout destroying interrupt endpoint queue\n");
1449 result = -1;
1450 goto out;
1451 }
1452 }
1453
Hans de Goede8f5f4f72014-09-20 16:51:25 +02001454 if (ctrl->periodic_schedules > 0) {
Patrick Georgie55fdac2013-03-06 14:08:31 +00001455 result = enable_periodic(ctrl);
1456 if (result < 0)
1457 debug("FATAL: periodic should never fail, but did");
1458 }
1459
1460out:
1461 free(queue->tds);
1462 free(queue->first);
1463 free(queue);
1464
1465 return result;
1466}
1467
Simon Glasscb7cf602015-03-25 12:22:25 -06001468static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe,
1469 void *buffer, int length, int interval)
Michael Trimarchi241f7512008-11-28 13:20:46 +01001470{
Patrick Georgie55fdac2013-03-06 14:08:31 +00001471 void *backbuffer;
1472 struct int_queue *queue;
1473 unsigned long timeout;
1474 int result = 0, ret;
1475
Michael Trimarchi241f7512008-11-28 13:20:46 +01001476 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1477 dev, pipe, buffer, length, interval);
Benoît Thébaudeau58c4dfb2012-08-09 23:50:44 +02001478
Hans de Goede53ca9de2015-05-11 20:43:52 +02001479 queue = _ehci_create_int_queue(dev, pipe, 1, length, buffer, interval);
Hans de Goede7f7cb732014-09-24 14:06:04 +02001480 if (!queue)
1481 return -1;
Patrick Georgie55fdac2013-03-06 14:08:31 +00001482
1483 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
Hans de Goede53ca9de2015-05-11 20:43:52 +02001484 while ((backbuffer = _ehci_poll_int_queue(dev, queue)) == NULL)
Patrick Georgie55fdac2013-03-06 14:08:31 +00001485 if (get_timer(0) > timeout) {
1486 printf("Timeout poll on interrupt endpoint\n");
1487 result = -ETIMEDOUT;
1488 break;
1489 }
1490
1491 if (backbuffer != buffer) {
Rob Herringf14d54b2015-03-17 15:46:37 -05001492 debug("got wrong buffer back (%p instead of %p)\n",
1493 backbuffer, buffer);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001494 return -EINVAL;
1495 }
1496
Hans de Goede53ca9de2015-05-11 20:43:52 +02001497 ret = _ehci_destroy_int_queue(dev, queue);
Patrick Georgie55fdac2013-03-06 14:08:31 +00001498 if (ret < 0)
1499 return ret;
1500
1501 /* everything worked out fine */
1502 return result;
Marek Vasut9b315fe2011-09-25 21:07:56 +02001503}
Simon Glasscb7cf602015-03-25 12:22:25 -06001504
Simon Glassa194b252015-03-25 12:22:29 -06001505#ifndef CONFIG_DM_USB
Simon Glasscb7cf602015-03-25 12:22:25 -06001506int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
1507 void *buffer, int length)
1508{
1509 return _ehci_submit_bulk_msg(dev, pipe, buffer, length);
1510}
1511
1512int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1513 int length, struct devrequest *setup)
1514{
1515 return _ehci_submit_control_msg(dev, pipe, buffer, length, setup);
1516}
1517
1518int submit_int_msg(struct usb_device *dev, unsigned long pipe,
1519 void *buffer, int length, int interval)
1520{
1521 return _ehci_submit_int_msg(dev, pipe, buffer, length, interval);
1522}
Hans de Goede53ca9de2015-05-11 20:43:52 +02001523
1524struct int_queue *create_int_queue(struct usb_device *dev,
1525 unsigned long pipe, int queuesize, int elementsize,
1526 void *buffer, int interval)
1527{
1528 return _ehci_create_int_queue(dev, pipe, queuesize, elementsize,
1529 buffer, interval);
1530}
1531
1532void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1533{
1534 return _ehci_poll_int_queue(dev, queue);
1535}
1536
1537int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1538{
1539 return _ehci_destroy_int_queue(dev, queue);
1540}
Simon Glassa194b252015-03-25 12:22:29 -06001541#endif
1542
1543#ifdef CONFIG_DM_USB
1544static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1545 unsigned long pipe, void *buffer, int length,
1546 struct devrequest *setup)
1547{
1548 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1549 dev->name, udev, udev->dev->name, udev->portnr);
1550
1551 return _ehci_submit_control_msg(udev, pipe, buffer, length, setup);
1552}
1553
1554static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1555 unsigned long pipe, void *buffer, int length)
1556{
1557 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1558 return _ehci_submit_bulk_msg(udev, pipe, buffer, length);
1559}
1560
1561static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1562 unsigned long pipe, void *buffer, int length,
1563 int interval)
1564{
1565 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1566 return _ehci_submit_int_msg(udev, pipe, buffer, length, interval);
1567}
1568
Hans de Goede0a7fa272015-05-10 14:10:18 +02001569static struct int_queue *ehci_create_int_queue(struct udevice *dev,
1570 struct usb_device *udev, unsigned long pipe, int queuesize,
1571 int elementsize, void *buffer, int interval)
1572{
1573 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1574 return _ehci_create_int_queue(udev, pipe, queuesize, elementsize,
1575 buffer, interval);
1576}
1577
1578static void *ehci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
1579 struct int_queue *queue)
1580{
1581 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1582 return _ehci_poll_int_queue(udev, queue);
1583}
1584
1585static int ehci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
1586 struct int_queue *queue)
1587{
1588 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1589 return _ehci_destroy_int_queue(udev, queue);
1590}
1591
Simon Glassa194b252015-03-25 12:22:29 -06001592int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
1593 struct ehci_hcor *hcor, const struct ehci_ops *ops,
1594 uint tweaks, enum usb_init_type init)
1595{
Hans de Goede76bc7f42015-05-05 11:54:35 +02001596 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
Simon Glassa194b252015-03-25 12:22:29 -06001597 struct ehci_ctrl *ctrl = dev_get_priv(dev);
1598 int ret;
1599
1600 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
1601 dev->name, ctrl, hccr, hcor, init);
1602
Hans de Goede76bc7f42015-05-05 11:54:35 +02001603 priv->desc_before_addr = true;
1604
Simon Glassa194b252015-03-25 12:22:29 -06001605 ehci_setup_ops(ctrl, ops);
1606 ctrl->hccr = hccr;
1607 ctrl->hcor = hcor;
1608 ctrl->priv = ctrl;
1609
Stephen Warren71eced32015-08-20 17:38:05 -06001610 ctrl->init = init;
1611 if (ctrl->init == USB_INIT_DEVICE)
Simon Glassa194b252015-03-25 12:22:29 -06001612 goto done;
Stephen Warren71eced32015-08-20 17:38:05 -06001613
Simon Glassa194b252015-03-25 12:22:29 -06001614 ret = ehci_reset(ctrl);
1615 if (ret)
1616 goto err;
1617
Mateusz Kulikowski3e13f392016-04-03 13:38:26 +02001618 if (ctrl->ops.init_after_reset) {
1619 ret = ctrl->ops.init_after_reset(ctrl);
Mateusz Kulikowskiaab5a5a2016-03-31 23:12:17 +02001620 if (ret)
1621 goto err;
1622 }
1623
Simon Glassa194b252015-03-25 12:22:29 -06001624 ret = ehci_common_init(ctrl, tweaks);
1625 if (ret)
1626 goto err;
1627done:
1628 return 0;
1629err:
1630 free(ctrl);
1631 debug("%s: failed, ret=%d\n", __func__, ret);
1632 return ret;
1633}
1634
1635int ehci_deregister(struct udevice *dev)
1636{
1637 struct ehci_ctrl *ctrl = dev_get_priv(dev);
1638
Stephen Warren71eced32015-08-20 17:38:05 -06001639 if (ctrl->init == USB_INIT_DEVICE)
1640 return 0;
1641
Simon Glassa194b252015-03-25 12:22:29 -06001642 ehci_shutdown(ctrl);
1643
1644 return 0;
1645}
1646
1647struct dm_usb_ops ehci_usb_ops = {
1648 .control = ehci_submit_control_msg,
1649 .bulk = ehci_submit_bulk_msg,
1650 .interrupt = ehci_submit_int_msg,
Hans de Goede0a7fa272015-05-10 14:10:18 +02001651 .create_int_queue = ehci_create_int_queue,
1652 .poll_int_queue = ehci_poll_int_queue,
1653 .destroy_int_queue = ehci_destroy_int_queue,
Simon Glassa194b252015-03-25 12:22:29 -06001654};
1655
1656#endif