blob: 1edb344d0fb22cfb97ee4c05d87a068ac772ce5c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01002/*-
3 * Copyright (c) 2007-2008, Juniper Networks, Inc.
michaeldb632992008-12-10 17:55:19 +01004 * Copyright (c) 2008, Excito Elektronik i Skåne AB
Remy Böhmerc0d722f2008-12-13 22:51:58 +01005 * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
6 *
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01007 * All rights reserved.
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01008 */
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01009#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glass46b01792015-03-25 12:22:29 -060011#include <dm.h>
Patrick Georgi8f62ca62013-03-06 14:08:31 +000012#include <errno.h>
michaeldb632992008-12-10 17:55:19 +010013#include <asm/byteorder.h>
Lucas Stach93ad9082012-09-06 08:00:13 +020014#include <asm/unaligned.h>
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010015#include <usb.h>
16#include <asm/io.h>
michaeldb632992008-12-10 17:55:19 +010017#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060018#include <memalign.h>
Stefan Roese67333f72010-11-26 15:43:28 +010019#include <watchdog.h>
Simon Glass336d4612020-02-03 07:36:16 -070020#include <dm/device_compat.h>
Patrick Georgi8f62ca62013-03-06 14:08:31 +000021#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020022
23#include "ehci.h"
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010024
Lucas Stach676ae062012-09-26 00:14:35 +020025#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
26#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
27#endif
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010028
Julius Werner5077f962013-09-24 10:53:07 -070029/*
30 * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt.
31 * Let's time out after 8 to have a little safety margin on top of that.
32 */
33#define HCHALT_TIMEOUT (8 * 1000)
34
Sven Schwermerfd09c202018-11-21 08:43:56 +010035#if !CONFIG_IS_ENABLED(DM_USB)
Marek Vasutb9596552013-07-10 03:16:31 +020036static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
Simon Glass46b01792015-03-25 12:22:29 -060037#endif
Tom Rini71c5de42012-07-15 22:14:24 +000038
39#define ALIGN_END_ADDR(type, ptr, size) \
Rob Herring98ae8402015-03-17 15:46:37 -050040 ((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010041
michaeldb632992008-12-10 17:55:19 +010042static struct descriptor {
43 struct usb_hub_descriptor hub;
44 struct usb_device_descriptor device;
45 struct usb_linux_config_descriptor config;
46 struct usb_linux_interface_descriptor interface;
47 struct usb_endpoint_descriptor endpoint;
48} __attribute__ ((packed)) descriptor = {
49 {
50 0x8, /* bDescLength */
51 0x29, /* bDescriptorType: hub descriptor */
52 2, /* bNrPorts -- runtime modified */
53 0, /* wHubCharacteristics */
Vincent Palatin5f4b4f22011-12-05 14:52:22 -080054 10, /* bPwrOn2PwrGood */
michaeldb632992008-12-10 17:55:19 +010055 0, /* bHubCntrCurrent */
Bin Meng337fc7e2017-07-19 21:50:00 +080056 { /* Device removable */
57 } /* at most 7 ports! XXX */
michaeldb632992008-12-10 17:55:19 +010058 },
59 {
60 0x12, /* bLength */
61 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030062 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michaeldb632992008-12-10 17:55:19 +010063 9, /* bDeviceClass: UDCLASS_HUB */
64 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
65 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
66 64, /* bMaxPacketSize: 64 bytes */
67 0x0000, /* idVendor */
68 0x0000, /* idProduct */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030069 cpu_to_le16(0x0100), /* bcdDevice */
michaeldb632992008-12-10 17:55:19 +010070 1, /* iManufacturer */
71 2, /* iProduct */
72 0, /* iSerialNumber */
73 1 /* bNumConfigurations: 1 */
74 },
75 {
76 0x9,
77 2, /* bDescriptorType: UDESC_CONFIG */
78 cpu_to_le16(0x19),
79 1, /* bNumInterface */
80 1, /* bConfigurationValue */
81 0, /* iConfiguration */
82 0x40, /* bmAttributes: UC_SELF_POWER */
83 0 /* bMaxPower */
84 },
85 {
86 0x9, /* bLength */
87 4, /* bDescriptorType: UDESC_INTERFACE */
88 0, /* bInterfaceNumber */
89 0, /* bAlternateSetting */
90 1, /* bNumEndpoints */
91 9, /* bInterfaceClass: UICLASS_HUB */
92 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
93 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
94 0 /* iInterface */
95 },
96 {
97 0x7, /* bLength */
98 5, /* bDescriptorType: UDESC_ENDPOINT */
99 0x81, /* bEndpointAddress:
100 * UE_DIR_IN | EHCI_INTR_ENDPT
101 */
102 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix8f8bd562009-10-31 12:37:38 -0500103 8, /* wMaxPacketSize */
michaeldb632992008-12-10 17:55:19 +0100104 255 /* bInterval */
105 },
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100106};
107
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100108#if defined(CONFIG_EHCI_IS_TDI)
109#define ehci_is_TDI() (1)
110#else
111#define ehci_is_TDI() (0)
112#endif
113
Simon Glass24ed8942015-03-25 12:22:25 -0600114static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev)
115{
Sven Schwermerfd09c202018-11-21 08:43:56 +0100116#if CONFIG_IS_ENABLED(DM_USB)
Hans de Goede25c8ebd2015-05-05 11:54:33 +0200117 return dev_get_priv(usb_get_bus(udev->dev));
Simon Glass46b01792015-03-25 12:22:29 -0600118#else
Simon Glass24ed8942015-03-25 12:22:25 -0600119 return udev->controller;
Simon Glass46b01792015-03-25 12:22:29 -0600120#endif
Simon Glass24ed8942015-03-25 12:22:25 -0600121}
122
Simon Glassdeb85082015-03-25 12:22:27 -0600123static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg)
Jim Linb068deb2013-03-27 00:52:32 +0000124{
125 return PORTSC_PSPD(reg);
126}
127
Simon Glassdeb85082015-03-25 12:22:27 -0600128static void ehci_set_usbmode(struct ehci_ctrl *ctrl)
Jim Linb068deb2013-03-27 00:52:32 +0000129{
130 uint32_t tmp;
131 uint32_t *reg_ptr;
132
Simon Glass11d18a12015-03-25 12:22:23 -0600133 reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE);
Jim Linb068deb2013-03-27 00:52:32 +0000134 tmp = ehci_readl(reg_ptr);
135 tmp |= USBMODE_CM_HC;
136#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
137 tmp |= USBMODE_BE;
Marek Vasut7ab0d352016-01-23 21:04:46 +0100138#else
139 tmp &= ~USBMODE_BE;
Jim Linb068deb2013-03-27 00:52:32 +0000140#endif
141 ehci_writel(reg_ptr, tmp);
142}
143
Simon Glassdeb85082015-03-25 12:22:27 -0600144static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
Simon Glass727fce32015-03-25 12:22:21 -0600145 uint32_t *reg)
Marek Vasut3874b6d2011-07-11 02:37:01 +0200146{
147 mdelay(50);
148}
149
Simon Glassdeb85082015-03-25 12:22:27 -0600150static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port)
Simon Glassaac064f2015-03-25 12:22:17 -0600151{
Bin Meng99c22552017-07-19 21:50:05 +0800152 int max_ports = HCS_N_PORTS(ehci_readl(&ctrl->hccr->cr_hcsparams));
153
154 if (port < 0 || port >= max_ports) {
Simon Glassaac064f2015-03-25 12:22:17 -0600155 /* Printing the message would cause a scan failure! */
Bin Meng99c22552017-07-19 21:50:05 +0800156 debug("The request port(%u) exceeds maximum port number\n",
157 port);
Simon Glassaac064f2015-03-25 12:22:17 -0600158 return NULL;
159 }
160
Simon Glass6a1a8162015-03-25 12:22:24 -0600161 return (uint32_t *)&ctrl->hcor->or_portsc[port];
Simon Glassaac064f2015-03-25 12:22:17 -0600162}
163
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100164static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michaeldb632992008-12-10 17:55:19 +0100165{
michael51ab1422008-12-11 13:43:55 +0100166 uint32_t result;
167 do {
168 result = ehci_readl(ptr);
Wolfgang Denk09c83a42010-10-22 14:23:00 +0200169 udelay(5);
michael51ab1422008-12-11 13:43:55 +0100170 if (result == ~(uint32_t)0)
171 return -1;
172 result &= mask;
173 if (result == done)
174 return 0;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100175 usec--;
176 } while (usec > 0);
michael51ab1422008-12-11 13:43:55 +0100177 return -1;
178}
179
Simon Glassaeca43e2015-03-25 12:22:28 -0600180static int ehci_reset(struct ehci_ctrl *ctrl)
michael51ab1422008-12-11 13:43:55 +0100181{
182 uint32_t cmd;
michael51ab1422008-12-11 13:43:55 +0100183 int ret = 0;
184
Simon Glassaeca43e2015-03-25 12:22:28 -0600185 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Stefan Roese273d7202010-11-26 15:44:00 +0100186 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Simon Glassaeca43e2015-03-25 12:22:28 -0600187 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
188 ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd,
Lucas Stach676ae062012-09-26 00:14:35 +0200189 CMD_RESET, 0, 250 * 1000);
michael51ab1422008-12-11 13:43:55 +0100190 if (ret < 0) {
191 printf("EHCI fail to reset\n");
192 goto out;
193 }
194
Jim Linb068deb2013-03-27 00:52:32 +0000195 if (ehci_is_TDI())
Simon Glassaeca43e2015-03-25 12:22:28 -0600196 ctrl->ops.set_usb_mode(ctrl);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000197
198#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Simon Glassaeca43e2015-03-25 12:22:28 -0600199 cmd = ehci_readl(&ctrl->hcor->or_txfilltuning);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200200 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass9ab4ce22012-02-27 10:52:47 +0000201 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Simon Glassaeca43e2015-03-25 12:22:28 -0600202 ehci_writel(&ctrl->hcor->or_txfilltuning, cmd);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000203#endif
michael51ab1422008-12-11 13:43:55 +0100204out:
205 return ret;
michaeldb632992008-12-10 17:55:19 +0100206}
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100207
Julius Werner5077f962013-09-24 10:53:07 -0700208static int ehci_shutdown(struct ehci_ctrl *ctrl)
209{
210 int i, ret = 0;
211 uint32_t cmd, reg;
Bin Meng99c22552017-07-19 21:50:05 +0800212 int max_ports = HCS_N_PORTS(ehci_readl(&ctrl->hccr->cr_hcsparams));
Julius Werner5077f962013-09-24 10:53:07 -0700213
214 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Peng Fan1e6fb0e2016-06-15 13:15:46 +0800215 /* If not run, directly return */
216 if (!(cmd & CMD_RUN))
217 return 0;
Julius Werner5077f962013-09-24 10:53:07 -0700218 cmd &= ~(CMD_PSE | CMD_ASE);
219 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
220 ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0,
221 100 * 1000);
222
223 if (!ret) {
Bin Meng99c22552017-07-19 21:50:05 +0800224 for (i = 0; i < max_ports; i++) {
Julius Werner5077f962013-09-24 10:53:07 -0700225 reg = ehci_readl(&ctrl->hcor->or_portsc[i]);
226 reg |= EHCI_PS_SUSP;
227 ehci_writel(&ctrl->hcor->or_portsc[i], reg);
228 }
229
230 cmd &= ~CMD_RUN;
231 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
232 ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT,
233 HCHALT_TIMEOUT);
234 }
235
236 if (ret)
237 puts("EHCI failed to shut down host controller.\n");
238
239 return ret;
240}
241
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100242static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
243{
Marek Vasutb8adb122012-04-09 04:07:46 +0200244 uint32_t delta, next;
Marek Vasutabd702f2016-02-26 19:23:27 +0100245 unsigned long addr = (unsigned long)buf;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100246 int idx;
247
Ilya Yanok189a6952012-07-15 04:43:49 +0000248 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutb8adb122012-04-09 04:07:46 +0200249 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
250
Ilya Yanok189a6952012-07-15 04:43:49 +0000251 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
252
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100253 idx = 0;
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200254 while (idx < QT_BUFFER_CNT) {
Marek Vasutcf7c93c2016-01-23 21:04:46 +0100255 td->qt_buffer[idx] = cpu_to_hc32(virt_to_phys((void *)addr));
Wolfgang Denk3ed16072010-10-19 16:13:15 +0200256 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200257 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100258 delta = next - addr;
259 if (delta >= sz)
260 break;
261 sz -= delta;
262 addr = next;
263 idx++;
264 }
265
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200266 if (idx == QT_BUFFER_CNT) {
Rob Herring98ae8402015-03-17 15:46:37 -0500267 printf("out of buffer pointers (%zu bytes left)\n", sz);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100268 return -1;
269 }
270
271 return 0;
272}
273
Ilya Yanokc60795f2012-11-06 13:48:20 +0000274static inline u8 ehci_encode_speed(enum usb_device_speed speed)
275{
276 #define QH_HIGH_SPEED 2
277 #define QH_FULL_SPEED 0
278 #define QH_LOW_SPEED 1
279 if (speed == USB_SPEED_HIGH)
280 return QH_HIGH_SPEED;
281 if (speed == USB_SPEED_LOW)
282 return QH_LOW_SPEED;
283 return QH_FULL_SPEED;
284}
285
Simon Glass46b01792015-03-25 12:22:29 -0600286static void ehci_update_endpt2_dev_n_port(struct usb_device *udev,
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200287 struct QH *qh)
288{
Stefan Brünsfaa7db22015-12-22 01:21:03 +0100289 uint8_t portnr = 0;
290 uint8_t hubaddr = 0;
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200291
Simon Glass46b01792015-03-25 12:22:29 -0600292 if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL)
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200293 return;
294
Stefan Brünsfaa7db22015-12-22 01:21:03 +0100295 usb_find_usb2_hub_address_port(udev, &hubaddr, &portnr);
Simon Glass46b01792015-03-25 12:22:29 -0600296
Stefan Brünsfaa7db22015-12-22 01:21:03 +0100297 qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(portnr) |
298 QH_ENDPT2_HUBADDR(hubaddr));
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200299}
300
Marek Vasut31232de2020-04-06 14:29:44 +0200301static int ehci_enable_async(struct ehci_ctrl *ctrl)
302{
303 u32 cmd;
304 int ret;
305
306 /* Enable async. schedule. */
307 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
308 if (cmd & CMD_ASE)
309 return 0;
310
311 cmd |= CMD_ASE;
312 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
313
314 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
315 100 * 1000);
316 if (ret < 0)
317 printf("EHCI fail timeout STS_ASS set\n");
318
319 return ret;
320}
321
322static int ehci_disable_async(struct ehci_ctrl *ctrl)
323{
324 u32 cmd;
325 int ret;
326
327 if (ctrl->async_locked)
328 return 0;
329
330 /* Disable async schedule. */
331 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
332 if (!(cmd & CMD_ASE))
333 return 0;
334
335 cmd &= ~CMD_ASE;
336 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
337
338 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
339 100 * 1000);
340 if (ret < 0)
341 printf("EHCI fail timeout STS_ASS reset\n");
342
343 return ret;
344}
345
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100346static int
347ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
348 int length, struct devrequest *req)
349{
Tom Rini71c5de42012-07-15 22:14:24 +0000350 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200351 struct qTD *qtd;
352 int qtd_count = 0;
Marek Vasutde98e8b2012-04-08 23:32:05 +0200353 int qtd_counter = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100354 volatile struct qTD *vtd;
355 unsigned long ts;
356 uint32_t *tdp;
Marek Vasut02b0e1a2019-10-06 16:13:38 +0200357 uint32_t endpt, maxpacket, token, usbsts, qhtoken;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100358 uint32_t c, toggle;
Simon Glass96820a32011-02-07 14:42:16 -0800359 int timeout;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100360 int ret = 0;
Simon Glass24ed8942015-03-25 12:22:25 -0600361 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100362
michaeldb632992008-12-10 17:55:19 +0100363 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100364 buffer, length, req);
365 if (req != NULL)
michaeldb632992008-12-10 17:55:19 +0100366 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100367 req->request, req->request,
368 req->requesttype, req->requesttype,
369 le16_to_cpu(req->value), le16_to_cpu(req->value),
michaeldb632992008-12-10 17:55:19 +0100370 le16_to_cpu(req->index));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100371
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200372#define PKT_ALIGN 512
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200373 /*
374 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
375 * described by a transfer descriptor (the qTD). The qTDs form a linked
376 * list with a queue head (QH).
377 *
378 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
379 * have its beginning in a qTD transfer and its end in the following
380 * one, so the qTD transfer lengths have to be chosen accordingly.
381 *
382 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
383 * single pages. The first data buffer can start at any offset within a
384 * page (not considering the cache-line alignment issues), while the
385 * following buffers must be page-aligned. There is no alignment
386 * constraint on the size of a qTD transfer.
387 */
388 if (req != NULL)
389 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
390 qtd_count += 1 + 1;
391 if (length > 0 || req == NULL) {
392 /*
393 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200394 * data payload (not considering the first qTD transfer, which
395 * may be longer or shorter, and the final one, which may be
396 * shorter).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200397 *
398 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200399 * transfer size is aligned to PKT_ALIGN, which is a multiple of
400 * wMaxPacketSize (except in some cases for interrupt transfers,
401 * see comment in submit_int_msg()).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200402 *
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200403 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200404 * QT_BUFFER_CNT full pages will be used.
405 */
406 int xfr_sz = QT_BUFFER_CNT;
407 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200408 * However, if the input buffer is not aligned to PKT_ALIGN, the
409 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200410 * data buffer of each transfer will be page-unaligned.
411 */
Rob Herring98ae8402015-03-17 15:46:37 -0500412 if ((unsigned long)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200413 xfr_sz--;
414 /* Convert the qTD transfer size to bytes. */
415 xfr_sz *= EHCI_PAGE_SIZE;
416 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200417 * Approximate by excess the number of qTDs that will be
418 * required for the data payload. The exact formula is way more
419 * complicated and saves at most 2 qTDs, i.e. a total of 128
420 * bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200421 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200422 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200423 }
424/*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200425 * Threshold value based on the worst-case total size of the allocated qTDs for
426 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200427 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200428#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200429#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
430#endif
431 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
432 if (qtd == NULL) {
433 printf("unable to allocate TDs\n");
434 return -1;
435 }
436
Tom Rini71c5de42012-07-15 22:14:24 +0000437 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200438 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200439
Marek Vasutb8adb122012-04-09 04:07:46 +0200440 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
441
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200442 /*
443 * Setup QH (3.6 in ehci-r10.pdf)
444 *
445 * qh_link ................. 03-00 H
446 * qh_endpt1 ............... 07-04 H
447 * qh_endpt2 ............... 0B-08 H
448 * - qh_curtd
449 * qh_overlay.qt_next ...... 13-10 H
450 * - qh_overlay.qt_altnext
451 */
Marek Vasutcf7c93c2016-01-23 21:04:46 +0100452 qh->qh_link = cpu_to_hc32(virt_to_phys(&ctrl->qh_list) | QH_LINK_TYPE_QH);
Ilya Yanokc60795f2012-11-06 13:48:20 +0000453 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200454 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200455 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200456 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200457 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200458 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
459 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Chris Packham4eaf7f52018-10-04 20:03:53 +1300460
461 /* Force FS for fsl HS quirk */
462 if (!ctrl->has_fsl_erratum_a005275)
463 endpt |= QH_ENDPT1_EPS(ehci_encode_speed(dev->speed));
464 else
465 endpt |= QH_ENDPT1_EPS(ehci_encode_speed(QH_FULL_SPEED));
466
Tom Rini71c5de42012-07-15 22:14:24 +0000467 qh->qh_endpt1 = cpu_to_hc32(endpt);
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200468 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini71c5de42012-07-15 22:14:24 +0000469 qh->qh_endpt2 = cpu_to_hc32(endpt);
Hans de Goede4e2c4ad2014-09-20 16:51:22 +0200470 ehci_update_endpt2_dev_n_port(dev, qh);
Tom Rini71c5de42012-07-15 22:14:24 +0000471 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Stephen Warren2456b972014-02-07 09:53:50 -0700472 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100473
Tom Rini71c5de42012-07-15 22:14:24 +0000474 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100475 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200476 /*
477 * Setup request qTD (3.5 in ehci-r10.pdf)
478 *
479 * qt_next ................ 03-00 H
480 * qt_altnext ............. 07-04 H
481 * qt_token ............... 0B-08 H
482 *
483 * [ buffer, buffer_hi ] loaded with "req".
484 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200485 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
486 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200487 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
488 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
489 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
490 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200491 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200492 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
493 printf("unable to construct SETUP TD\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100494 goto fail;
495 }
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200496 /* Update previous qTD! */
Marek Vasutcf7c93c2016-01-23 21:04:46 +0100497 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200498 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100499 toggle = 1;
500 }
501
502 if (length > 0 || req == NULL) {
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200503 uint8_t *buf_ptr = buffer;
504 int left_length = length;
505
506 do {
507 /*
508 * Determine the size of this qTD transfer. By default,
509 * QT_BUFFER_CNT full pages can be used.
510 */
511 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
512 /*
513 * However, if the input buffer is not page-aligned, the
514 * portion of the first page before the buffer start
515 * offset within that page is unusable.
516 */
Rob Herring98ae8402015-03-17 15:46:37 -0500517 xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200518 /*
519 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200520 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200521 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200522 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200523 /*
524 * This transfer may be shorter than the available qTD
525 * transfer size that has just been computed.
526 */
527 xfr_bytes = min(xfr_bytes, left_length);
528
529 /*
530 * Setup request qTD (3.5 in ehci-r10.pdf)
531 *
532 * qt_next ................ 03-00 H
533 * qt_altnext ............. 07-04 H
534 * qt_token ............... 0B-08 H
535 *
536 * [ buffer, buffer_hi ] loaded with "buffer".
537 */
538 qtd[qtd_counter].qt_next =
539 cpu_to_hc32(QT_NEXT_TERMINATE);
540 qtd[qtd_counter].qt_altnext =
541 cpu_to_hc32(QT_NEXT_TERMINATE);
542 token = QT_TOKEN_DT(toggle) |
543 QT_TOKEN_TOTALBYTES(xfr_bytes) |
544 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
545 QT_TOKEN_CERR(3) |
546 QT_TOKEN_PID(usb_pipein(pipe) ?
547 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
548 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
549 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
550 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
551 xfr_bytes)) {
552 printf("unable to construct DATA TD\n");
553 goto fail;
554 }
555 /* Update previous qTD! */
Marek Vasutcf7c93c2016-01-23 21:04:46 +0100556 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200557 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200558 /*
559 * Data toggle has to be adjusted since the qTD transfer
560 * size is not always an even multiple of
561 * wMaxPacketSize.
562 */
563 if ((xfr_bytes / maxpacket) & 1)
564 toggle ^= 1;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200565 buf_ptr += xfr_bytes;
566 left_length -= xfr_bytes;
567 } while (left_length > 0);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100568 }
569
570 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200571 /*
572 * Setup request qTD (3.5 in ehci-r10.pdf)
573 *
574 * qt_next ................ 03-00 H
575 * qt_altnext ............. 07-04 H
576 * qt_token ............... 0B-08 H
577 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200578 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
579 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200580 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200581 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
582 QT_TOKEN_PID(usb_pipein(pipe) ?
583 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
584 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200585 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200586 /* Update previous qTD! */
Marek Vasutcf7c93c2016-01-23 21:04:46 +0100587 *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter]));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200588 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100589 }
590
Marek Vasutcf7c93c2016-01-23 21:04:46 +0100591 ctrl->qh_list.qh_link = cpu_to_hc32(virt_to_phys(qh) | QH_LINK_TYPE_QH);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100592
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100593 /* Flush dcache */
Rob Herring98ae8402015-03-17 15:46:37 -0500594 flush_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach676ae062012-09-26 00:14:35 +0200595 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500596 flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1));
597 flush_dcache_range((unsigned long)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200598 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100599
Lucas Stach676ae062012-09-26 00:14:35 +0200600 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
601 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100602
Marek Vasut31232de2020-04-06 14:29:44 +0200603 ret = ehci_enable_async(ctrl);
604 if (ret)
605 goto fail;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100606
607 /* Wait for TDs to be processed. */
608 ts = get_timer(0);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200609 vtd = &qtd[qtd_counter - 1];
Simon Glass96820a32011-02-07 14:42:16 -0800610 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100611 do {
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100612 /* Invalidate dcache */
Rob Herring98ae8402015-03-17 15:46:37 -0500613 invalidate_dcache_range((unsigned long)&ctrl->qh_list,
Lucas Stach676ae062012-09-26 00:14:35 +0200614 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500615 invalidate_dcache_range((unsigned long)qh,
Tom Rini71c5de42012-07-15 22:14:24 +0000616 ALIGN_END_ADDR(struct QH, qh, 1));
Rob Herring98ae8402015-03-17 15:46:37 -0500617 invalidate_dcache_range((unsigned long)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200618 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutb8adb122012-04-09 04:07:46 +0200619
michaeldb632992008-12-10 17:55:19 +0100620 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200621 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100622 break;
Stefan Roese67333f72010-11-26 15:43:28 +0100623 WATCHDOG_RESET();
Simon Glass96820a32011-02-07 14:42:16 -0800624 } while (get_timer(ts) < timeout);
Marek Vasut02b0e1a2019-10-06 16:13:38 +0200625 qhtoken = hc32_to_cpu(qh->qh_overlay.qt_token);
626
627 ctrl->qh_list.qh_link = cpu_to_hc32(virt_to_phys(&ctrl->qh_list) | QH_LINK_TYPE_QH);
628 flush_dcache_range((unsigned long)&ctrl->qh_list,
629 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Simon Glass96820a32011-02-07 14:42:16 -0800630
Ilya Yanok189a6952012-07-15 04:43:49 +0000631 /*
632 * Invalidate the memory area occupied by buffer
633 * Don't try to fix the buffer alignment, if it isn't properly
634 * aligned it's upper layer's fault so let invalidate_dcache_range()
635 * vow about it. But we have to fix the length as it's actual
636 * transfer length and can be unaligned. This is potentially
637 * dangerous operation, it's responsibility of the calling
638 * code to make sure enough space is reserved.
639 */
Dirk Behmeb3cbcd92017-11-17 15:28:36 +0100640 if (buffer != NULL && length > 0)
641 invalidate_dcache_range((unsigned long)buffer,
642 ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutb8adb122012-04-09 04:07:46 +0200643
Simon Glass96820a32011-02-07 14:42:16 -0800644 /* Check that the TD processing happened */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200645 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glass96820a32011-02-07 14:42:16 -0800646 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100647
Marek Vasut31232de2020-04-06 14:29:44 +0200648 ret = ehci_disable_async(ctrl);
649 if (ret)
650 goto fail;
651
Marek Vasut02b0e1a2019-10-06 16:13:38 +0200652 if (!(QT_TOKEN_GET_STATUS(qhtoken) & QT_TOKEN_STATUS_ACTIVE)) {
653 debug("TOKEN=%#x\n", qhtoken);
654 switch (QT_TOKEN_GET_STATUS(qhtoken) &
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200655 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100656 case 0:
Marek Vasut02b0e1a2019-10-06 16:13:38 +0200657 toggle = QT_TOKEN_GET_DT(qhtoken);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100658 usb_settoggle(dev, usb_pipeendpoint(pipe),
659 usb_pipeout(pipe), toggle);
660 dev->status = 0;
661 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200662 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100663 dev->status = USB_ST_STALLED;
664 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200665 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
666 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100667 dev->status = USB_ST_BUF_ERR;
668 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200669 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
670 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100671 dev->status = USB_ST_BABBLE_DET;
672 break;
673 default:
674 dev->status = USB_ST_CRC_ERR;
Marek Vasut02b0e1a2019-10-06 16:13:38 +0200675 if (QT_TOKEN_GET_STATUS(qhtoken) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschin222d6df2010-11-02 11:47:29 +0100676 dev->status |= USB_ST_STALLED;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100677 break;
678 }
Marek Vasut02b0e1a2019-10-06 16:13:38 +0200679 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(qhtoken);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100680 } else {
681 dev->act_len = 0;
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800682#ifndef CONFIG_USB_EHCI_FARADAY
michaeldb632992008-12-10 17:55:19 +0100683 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach676ae062012-09-26 00:14:35 +0200684 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
685 ehci_readl(&ctrl->hcor->or_portsc[0]),
686 ehci_readl(&ctrl->hcor->or_portsc[1]));
Kuo-Jung Sue82a3162013-05-15 15:29:23 +0800687#endif
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100688 }
689
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200690 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100691 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
692
693fail:
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200694 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100695 return -1;
696}
697
Simon Glass24ed8942015-03-25 12:22:25 -0600698static int ehci_submit_root(struct usb_device *dev, unsigned long pipe,
699 void *buffer, int length, struct devrequest *req)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100700{
701 uint8_t tmpbuf[4];
702 u16 typeReq;
michaeldb632992008-12-10 17:55:19 +0100703 void *srcptr = NULL;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100704 int len, srclen;
705 uint32_t reg;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100706 uint32_t *status_reg;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000707 int port = le16_to_cpu(req->index) & 0xff;
Simon Glass24ed8942015-03-25 12:22:25 -0600708 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100709
710 srclen = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100711
michaeldb632992008-12-10 17:55:19 +0100712 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100713 req->request, req->request,
714 req->requesttype, req->requesttype,
715 le16_to_cpu(req->value), le16_to_cpu(req->index));
716
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530717 typeReq = req->request | req->requesttype << 8;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100718
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530719 switch (typeReq) {
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800720 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
721 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
722 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Simon Glassdeb85082015-03-25 12:22:27 -0600723 status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1);
Kuo-Jung Su1dde1422013-05-15 15:29:21 +0800724 if (!status_reg)
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800725 return -1;
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800726 break;
727 default:
728 status_reg = NULL;
729 break;
730 }
731
732 switch (typeReq) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100733 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
734 switch (le16_to_cpu(req->value) >> 8) {
735 case USB_DT_DEVICE:
michaeldb632992008-12-10 17:55:19 +0100736 debug("USB_DT_DEVICE request\n");
737 srcptr = &descriptor.device;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200738 srclen = descriptor.device.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100739 break;
740 case USB_DT_CONFIG:
michaeldb632992008-12-10 17:55:19 +0100741 debug("USB_DT_CONFIG config\n");
742 srcptr = &descriptor.config;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200743 srclen = descriptor.config.bLength +
744 descriptor.interface.bLength +
745 descriptor.endpoint.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100746 break;
747 case USB_DT_STRING:
michaeldb632992008-12-10 17:55:19 +0100748 debug("USB_DT_STRING config\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100749 switch (le16_to_cpu(req->value) & 0xff) {
750 case 0: /* Language */
751 srcptr = "\4\3\1\0";
752 srclen = 4;
753 break;
754 case 1: /* Vendor */
755 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
756 srclen = 14;
757 break;
758 case 2: /* Product */
759 srcptr = "\52\3E\0H\0C\0I\0 "
760 "\0H\0o\0s\0t\0 "
761 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
762 srclen = 42;
763 break;
764 default:
michaeldb632992008-12-10 17:55:19 +0100765 debug("unknown value DT_STRING %x\n",
766 le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100767 goto unknown;
768 }
769 break;
770 default:
michaeldb632992008-12-10 17:55:19 +0100771 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100772 goto unknown;
773 }
774 break;
775 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
776 switch (le16_to_cpu(req->value) >> 8) {
777 case USB_DT_HUB:
michaeldb632992008-12-10 17:55:19 +0100778 debug("USB_DT_HUB config\n");
779 srcptr = &descriptor.hub;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200780 srclen = descriptor.hub.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100781 break;
782 default:
michaeldb632992008-12-10 17:55:19 +0100783 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100784 goto unknown;
785 }
786 break;
787 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michaeldb632992008-12-10 17:55:19 +0100788 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach676ae062012-09-26 00:14:35 +0200789 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100790 break;
791 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michaeldb632992008-12-10 17:55:19 +0100792 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100793 /* Nothing to do */
794 break;
795 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
796 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
797 tmpbuf[1] = 0;
798 srcptr = tmpbuf;
799 srclen = 2;
800 break;
michaeldb632992008-12-10 17:55:19 +0100801 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100802 memset(tmpbuf, 0, 4);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100803 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100804 if (reg & EHCI_PS_CS)
805 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
806 if (reg & EHCI_PS_PE)
807 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
808 if (reg & EHCI_PS_SUSP)
809 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
810 if (reg & EHCI_PS_OCA)
811 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300812 if (reg & EHCI_PS_PR)
813 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100814 if (reg & EHCI_PS_PP)
815 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese597eb282009-01-21 17:12:01 +0100816
817 if (ehci_is_TDI()) {
Simon Glassdeb85082015-03-25 12:22:27 -0600818 switch (ctrl->ops.get_port_speed(ctrl, reg)) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200819 case PORTSC_PSPD_FS:
Stefan Roese597eb282009-01-21 17:12:01 +0100820 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200821 case PORTSC_PSPD_LS:
Stefan Roese597eb282009-01-21 17:12:01 +0100822 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
823 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200824 case PORTSC_PSPD_HS:
Stefan Roese597eb282009-01-21 17:12:01 +0100825 default:
826 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
827 break;
828 }
829 } else {
830 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
831 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100832
833 if (reg & EHCI_PS_CSC)
834 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
835 if (reg & EHCI_PS_PEC)
836 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
837 if (reg & EHCI_PS_OCC)
838 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000839 if (ctrl->portreset & (1 << port))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100840 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100841
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100842 srcptr = tmpbuf;
843 srclen = 4;
844 break;
michaeldb632992008-12-10 17:55:19 +0100845 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100846 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100847 reg &= ~EHCI_PS_CLEAR;
848 switch (le16_to_cpu(req->value)) {
michael51ab1422008-12-11 13:43:55 +0100849 case USB_PORT_FEAT_ENABLE:
850 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100851 ehci_writel(status_reg, reg);
michael51ab1422008-12-11 13:43:55 +0100852 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100853 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200854 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100855 reg |= EHCI_PS_PP;
856 ehci_writel(status_reg, reg);
857 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100858 break;
859 case USB_PORT_FEAT_RESET:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100860 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
861 !ehci_is_TDI() &&
862 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100863 /* Low speed device, give up ownership. */
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100864 debug("port %d low speed --> companion\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000865 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100866 reg |= EHCI_PS_PO;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100867 ehci_writel(status_reg, reg);
Hans de Goede45b9ea12015-05-10 14:10:16 +0200868 return -ENXIO;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100869 } else {
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300870 int ret;
871
Chris Packham4eaf7f52018-10-04 20:03:53 +1300872 /* Disable chirp for HS erratum */
873 if (ctrl->has_fsl_erratum_a005275)
874 reg |= PORTSC_FSL_PFSC;
875
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100876 reg |= EHCI_PS_PR;
877 reg &= ~EHCI_PS_PE;
878 ehci_writel(status_reg, reg);
879 /*
880 * caller must wait, then call GetPortStatus
881 * usb 2.0 specification say 50 ms resets on
882 * root
883 */
Simon Glassdeb85082015-03-25 12:22:27 -0600884 ctrl->ops.powerup_fixup(ctrl, status_reg, &reg);
Marek Vasut3874b6d2011-07-11 02:37:01 +0200885
Chris Zhangb4161912010-01-06 13:34:04 -0800886 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300887 /*
888 * A host controller must terminate the reset
889 * and stabilize the state of the port within
890 * 2 milliseconds
891 */
892 ret = handshake(status_reg, EHCI_PS_PR, 0,
893 2 * 1000);
Hans de Goede71b94522015-05-10 14:10:13 +0200894 if (!ret) {
895 reg = ehci_readl(status_reg);
896 if ((reg & (EHCI_PS_PE | EHCI_PS_CS))
897 == EHCI_PS_CS && !ehci_is_TDI()) {
898 debug("port %d full speed --> companion\n", port - 1);
899 reg &= ~EHCI_PS_CLEAR;
900 reg |= EHCI_PS_PO;
901 ehci_writel(status_reg, reg);
Hans de Goede45b9ea12015-05-10 14:10:16 +0200902 return -ENXIO;
Hans de Goede71b94522015-05-10 14:10:13 +0200903 } else {
904 ctrl->portreset |= 1 << port;
905 }
906 } else {
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300907 printf("port(%d) reset error\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000908 port - 1);
Hans de Goede71b94522015-05-10 14:10:13 +0200909 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100910 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100911 break;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000912 case USB_PORT_FEAT_TEST:
Julius Werner5077f962013-09-24 10:53:07 -0700913 ehci_shutdown(ctrl);
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000914 reg &= ~(0xf << 16);
915 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
916 ehci_writel(status_reg, reg);
917 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100918 default:
michaeldb632992008-12-10 17:55:19 +0100919 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100920 goto unknown;
921 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100922 /* unblock posted writes */
Lucas Stach676ae062012-09-26 00:14:35 +0200923 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100924 break;
michaeldb632992008-12-10 17:55:19 +0100925 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100926 reg = ehci_readl(status_reg);
Simon Glassed10e662013-05-10 19:49:00 -0700927 reg &= ~EHCI_PS_CLEAR;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100928 switch (le16_to_cpu(req->value)) {
929 case USB_PORT_FEAT_ENABLE:
930 reg &= ~EHCI_PS_PE;
931 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100932 case USB_PORT_FEAT_C_ENABLE:
Simon Glassed10e662013-05-10 19:49:00 -0700933 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100934 break;
935 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200936 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Simon Glassed10e662013-05-10 19:49:00 -0700937 reg &= ~EHCI_PS_PP;
938 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100939 case USB_PORT_FEAT_C_CONNECTION:
Simon Glassed10e662013-05-10 19:49:00 -0700940 reg |= EHCI_PS_CSC;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100941 break;
michael51ab1422008-12-11 13:43:55 +0100942 case USB_PORT_FEAT_OVER_CURRENT:
Simon Glassed10e662013-05-10 19:49:00 -0700943 reg |= EHCI_PS_OCC;
michael51ab1422008-12-11 13:43:55 +0100944 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100945 case USB_PORT_FEAT_C_RESET:
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000946 ctrl->portreset &= ~(1 << port);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100947 break;
948 default:
michaeldb632992008-12-10 17:55:19 +0100949 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100950 goto unknown;
951 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100952 ehci_writel(status_reg, reg);
953 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +0200954 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100955 break;
956 default:
michaeldb632992008-12-10 17:55:19 +0100957 debug("Unknown request\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100958 goto unknown;
959 }
960
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000961 mdelay(1);
Masahiro Yamadab4141192014-11-07 03:03:31 +0900962 len = min3(srclen, (int)le16_to_cpu(req->length), length);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100963 if (srcptr != NULL && len > 0)
964 memcpy(buffer, srcptr, len);
michaeldb632992008-12-10 17:55:19 +0100965 else
966 debug("Len is 0\n");
967
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100968 dev->act_len = len;
969 dev->status = 0;
970 return 0;
971
972unknown:
michaeldb632992008-12-10 17:55:19 +0100973 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100974 req->requesttype, req->request, le16_to_cpu(req->value),
975 le16_to_cpu(req->index), le16_to_cpu(req->length));
976
977 dev->act_len = 0;
978 dev->status = USB_ST_STALLED;
979 return -1;
980}
981
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900982static const struct ehci_ops default_ehci_ops = {
Simon Glassdeb85082015-03-25 12:22:27 -0600983 .set_usb_mode = ehci_set_usbmode,
984 .get_port_speed = ehci_get_port_speed,
985 .powerup_fixup = ehci_powerup_fixup,
986 .get_portsc_register = ehci_get_portsc_register,
987};
988
989static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops)
Simon Glassc4a31412015-03-25 12:22:19 -0600990{
Simon Glassdeb85082015-03-25 12:22:27 -0600991 if (!ops) {
992 ctrl->ops = default_ehci_ops;
993 } else {
994 ctrl->ops = *ops;
995 if (!ctrl->ops.set_usb_mode)
996 ctrl->ops.set_usb_mode = ehci_set_usbmode;
997 if (!ctrl->ops.get_port_speed)
998 ctrl->ops.get_port_speed = ehci_get_port_speed;
999 if (!ctrl->ops.powerup_fixup)
1000 ctrl->ops.powerup_fixup = ehci_powerup_fixup;
1001 if (!ctrl->ops.get_portsc_register)
1002 ctrl->ops.get_portsc_register =
1003 ehci_get_portsc_register;
1004 }
1005}
1006
Sven Schwermerfd09c202018-11-21 08:43:56 +01001007#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glassdeb85082015-03-25 12:22:27 -06001008void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops)
1009{
1010 struct ehci_ctrl *ctrl = &ehcic[index];
1011
1012 ctrl->priv = priv;
1013 ehci_setup_ops(ctrl, ops);
Simon Glassc4a31412015-03-25 12:22:19 -06001014}
1015
1016void *ehci_get_controller_priv(int index)
1017{
1018 return ehcic[index].priv;
1019}
Simon Glass46b01792015-03-25 12:22:29 -06001020#endif
Simon Glassc4a31412015-03-25 12:22:19 -06001021
Simon Glass7372b5b2015-03-25 12:22:26 -06001022static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001023{
Lucas Stach676ae062012-09-26 00:14:35 +02001024 struct QH *qh_list;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001025 struct QH *periodic;
Simon Glass7372b5b2015-03-25 12:22:26 -06001026 uint32_t reg;
1027 uint32_t cmd;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001028 int i;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001029
Vincent Palatin29828372012-12-12 17:55:22 -08001030 /* Set the high address word (aka segment) for 64-bit controller */
Simon Glass7372b5b2015-03-25 12:22:26 -06001031 if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1)
1032 ehci_writel(&ctrl->hcor->or_ctrldssegment, 0);
Stefan Roese832e6142009-01-21 17:12:10 +01001033
Simon Glass7372b5b2015-03-25 12:22:26 -06001034 qh_list = &ctrl->qh_list;
Lucas Stach676ae062012-09-26 00:14:35 +02001035
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001036 /* Set head of reclaim list */
Tom Rini71c5de42012-07-15 22:14:24 +00001037 memset(qh_list, 0, sizeof(*qh_list));
Marek Vasutcf7c93c2016-01-23 21:04:46 +01001038 qh_list->qh_link = cpu_to_hc32(virt_to_phys(qh_list) | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +02001039 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
1040 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini71c5de42012-07-15 22:14:24 +00001041 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1042 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +02001043 qh_list->qh_overlay.qt_token =
1044 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001045
Rob Herring98ae8402015-03-17 15:46:37 -05001046 flush_dcache_range((unsigned long)qh_list,
Stephen Warrend3e07472013-05-24 15:03:17 -06001047 ALIGN_END_ADDR(struct QH, qh_list, 1));
1048
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001049 /* Set async. queue head pointer. */
Marek Vasutcf7c93c2016-01-23 21:04:46 +01001050 ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(qh_list));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001051
1052 /*
1053 * Set up periodic list
1054 * Step 1: Parent QH for all periodic transfers.
1055 */
Simon Glass7372b5b2015-03-25 12:22:26 -06001056 ctrl->periodic_schedules = 0;
1057 periodic = &ctrl->periodic_queue;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001058 memset(periodic, 0, sizeof(*periodic));
1059 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
1060 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1061 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1062
Rob Herring98ae8402015-03-17 15:46:37 -05001063 flush_dcache_range((unsigned long)periodic,
Stephen Warrend3e07472013-05-24 15:03:17 -06001064 ALIGN_END_ADDR(struct QH, periodic, 1));
1065
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001066 /*
1067 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
1068 * In particular, device specifications on polling frequency
1069 * are disregarded. Keyboards seem to send NAK/NYet reliably
1070 * when polled with an empty buffer.
1071 *
1072 * Split Transactions will be spread across microframes using
1073 * S-mask and C-mask.
1074 */
Simon Glass7372b5b2015-03-25 12:22:26 -06001075 if (ctrl->periodic_list == NULL)
1076 ctrl->periodic_list = memalign(4096, 1024 * 4);
Nikita Kiryanov8bc36032013-07-29 13:27:40 +03001077
Simon Glass7372b5b2015-03-25 12:22:26 -06001078 if (!ctrl->periodic_list)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001079 return -ENOMEM;
1080 for (i = 0; i < 1024; i++) {
Simon Glass7372b5b2015-03-25 12:22:26 -06001081 ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic
Adrian Coxea427772014-04-10 13:29:45 +01001082 | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001083 }
1084
Simon Glass7372b5b2015-03-25 12:22:26 -06001085 flush_dcache_range((unsigned long)ctrl->periodic_list,
1086 ALIGN_END_ADDR(uint32_t, ctrl->periodic_list,
Stephen Warrend3e07472013-05-24 15:03:17 -06001087 1024));
1088
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001089 /* Set periodic list base address */
Simon Glass7372b5b2015-03-25 12:22:26 -06001090 ehci_writel(&ctrl->hcor->or_periodiclistbase,
1091 (unsigned long)ctrl->periodic_list);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001092
Simon Glass7372b5b2015-03-25 12:22:26 -06001093 reg = ehci_readl(&ctrl->hccr->cr_hcsparams);
michael51ab1422008-12-11 13:43:55 +01001094 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stach7a46b2c2012-09-28 00:26:19 +02001095 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001096 /* Port Indicators */
1097 if (HCS_INDICATOR(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +02001098 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1099 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001100 /* Port Power Control */
1101 if (HCS_PPC(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +02001102 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1103 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001104
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001105 /* Start the host controller. */
Simon Glass7372b5b2015-03-25 12:22:26 -06001106 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Wolfgang Denkf15c6512009-02-12 00:08:39 +01001107 /*
1108 * Philips, Intel, and maybe others need CMD_RUN before the
1109 * root hub will detect new devices (why?); NEC doesn't
1110 */
michael51ab1422008-12-11 13:43:55 +01001111 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
1112 cmd |= CMD_RUN;
Simon Glass7372b5b2015-03-25 12:22:26 -06001113 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +01001114
Simon Glass7372b5b2015-03-25 12:22:26 -06001115 if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) {
1116 /* take control over the ports */
1117 cmd = ehci_readl(&ctrl->hcor->or_configflag);
1118 cmd |= FLAG_CF;
1119 ehci_writel(&ctrl->hcor->or_configflag, cmd);
1120 }
Kuo-Jung Sue82a3162013-05-15 15:29:23 +08001121
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001122 /* unblock posted write */
Simon Glass7372b5b2015-03-25 12:22:26 -06001123 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
Mike Frysinger5b84dd62012-03-05 13:47:00 +00001124 mdelay(5);
Simon Glass7372b5b2015-03-25 12:22:26 -06001125 reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase));
Remy Böhmerc0d722f2008-12-13 22:51:58 +01001126 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001127
Simon Glass7372b5b2015-03-25 12:22:26 -06001128 return 0;
1129}
1130
Sven Schwermerfd09c202018-11-21 08:43:56 +01001131#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glass7372b5b2015-03-25 12:22:26 -06001132int usb_lowlevel_stop(int index)
1133{
1134 ehci_shutdown(&ehcic[index]);
1135 return ehci_hcd_stop(index);
1136}
1137
1138int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1139{
1140 struct ehci_ctrl *ctrl = &ehcic[index];
1141 uint tweaks = 0;
1142 int rc;
1143
Simon Glassdeb85082015-03-25 12:22:27 -06001144 /**
1145 * Set ops to default_ehci_ops, ehci_hcd_init should call
1146 * ehci_set_controller_priv to change any of these function pointers.
1147 */
1148 ctrl->ops = default_ehci_ops;
1149
Simon Glass7372b5b2015-03-25 12:22:26 -06001150 rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
1151 if (rc)
1152 return rc;
Heinrich Schuchardt45157d22017-11-20 19:33:39 +01001153 if (!ctrl->hccr || !ctrl->hcor)
1154 return -1;
Simon Glass7372b5b2015-03-25 12:22:26 -06001155 if (init == USB_INIT_DEVICE)
1156 goto done;
1157
1158 /* EHCI spec section 4.1 */
Simon Glassaeca43e2015-03-25 12:22:28 -06001159 if (ehci_reset(ctrl))
Simon Glass7372b5b2015-03-25 12:22:26 -06001160 return -1;
1161
1162#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
1163 rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor);
1164 if (rc)
1165 return rc;
1166#endif
1167#ifdef CONFIG_USB_EHCI_FARADAY
1168 tweaks |= EHCI_TWEAK_NO_INIT_CF;
1169#endif
1170 rc = ehci_common_init(ctrl, tweaks);
1171 if (rc)
1172 return rc;
1173
1174 ctrl->rootdev = 0;
Troy Kisky127efc42013-10-10 15:27:57 -07001175done:
Lucas Stach676ae062012-09-26 00:14:35 +02001176 *controller = &ehcic[index];
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001177 return 0;
1178}
Simon Glass46b01792015-03-25 12:22:29 -06001179#endif
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001180
Simon Glass24ed8942015-03-25 12:22:25 -06001181static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
1182 void *buffer, int length)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001183{
1184
1185 if (usb_pipetype(pipe) != PIPE_BULK) {
1186 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1187 return -1;
1188 }
1189 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1190}
1191
Simon Glass24ed8942015-03-25 12:22:25 -06001192static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe,
1193 void *buffer, int length,
1194 struct devrequest *setup)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001195{
Simon Glass24ed8942015-03-25 12:22:25 -06001196 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001197
1198 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1199 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1200 return -1;
1201 }
1202
Lucas Stach676ae062012-09-26 00:14:35 +02001203 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1204 if (!ctrl->rootdev)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001205 dev->speed = USB_SPEED_HIGH;
1206 return ehci_submit_root(dev, pipe, buffer, length, setup);
1207 }
1208 return ehci_submit_async(dev, pipe, buffer, length, setup);
1209}
1210
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001211struct int_queue {
Hans de Goede8aa26b82014-09-24 14:06:05 +02001212 int elementsize;
Hans de Goede7f59d162015-06-18 22:34:33 +02001213 unsigned long pipe;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001214 struct QH *first;
1215 struct QH *current;
1216 struct QH *last;
1217 struct qTD *tds;
1218};
1219
Rob Herring98ae8402015-03-17 15:46:37 -05001220#define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001221
1222static int
1223enable_periodic(struct ehci_ctrl *ctrl)
1224{
1225 uint32_t cmd;
1226 struct ehci_hcor *hcor = ctrl->hcor;
1227 int ret;
1228
1229 cmd = ehci_readl(&hcor->or_usbcmd);
1230 cmd |= CMD_PSE;
1231 ehci_writel(&hcor->or_usbcmd, cmd);
1232
1233 ret = handshake((uint32_t *)&hcor->or_usbsts,
1234 STS_PSS, STS_PSS, 100 * 1000);
1235 if (ret < 0) {
1236 printf("EHCI failed: timeout when enabling periodic list\n");
1237 return -ETIMEDOUT;
1238 }
1239 udelay(1000);
1240 return 0;
1241}
1242
1243static int
1244disable_periodic(struct ehci_ctrl *ctrl)
1245{
1246 uint32_t cmd;
1247 struct ehci_hcor *hcor = ctrl->hcor;
1248 int ret;
1249
1250 cmd = ehci_readl(&hcor->or_usbcmd);
1251 cmd &= ~CMD_PSE;
1252 ehci_writel(&hcor->or_usbcmd, cmd);
1253
1254 ret = handshake((uint32_t *)&hcor->or_usbsts,
1255 STS_PSS, 0, 100 * 1000);
1256 if (ret < 0) {
1257 printf("EHCI failed: timeout when disabling periodic list\n");
1258 return -ETIMEDOUT;
1259 }
1260 return 0;
1261}
1262
Hans de Goede029fd8e2015-05-11 20:43:52 +02001263static struct int_queue *_ehci_create_int_queue(struct usb_device *dev,
1264 unsigned long pipe, int queuesize, int elementsize,
1265 void *buffer, int interval)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001266{
Simon Glass24ed8942015-03-25 12:22:25 -06001267 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001268 struct int_queue *result = NULL;
Hans de Goede7f59d162015-06-18 22:34:33 +02001269 uint32_t i, toggle;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001270
Hans de Goedebd818d82014-09-24 14:06:04 +02001271 /*
1272 * Interrupt transfers requiring several transactions are not supported
1273 * because bInterval is ignored.
1274 *
1275 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
1276 * <= PKT_ALIGN if several qTDs are required, while the USB
1277 * specification does not constrain this for interrupt transfers. That
1278 * means that ehci_submit_async() would support interrupt transfers
1279 * requiring several transactions only as long as the transfer size does
1280 * not require more than a single qTD.
1281 */
1282 if (elementsize > usb_maxpacket(dev, pipe)) {
1283 printf("%s: xfers requiring several transactions are not supported.\n",
1284 __func__);
1285 return NULL;
1286 }
1287
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001288 debug("Enter create_int_queue\n");
1289 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1290 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1291 return NULL;
1292 }
1293
1294 /* limit to 4 full pages worth of data -
1295 * we can safely fit them in a single TD,
1296 * no matter the alignment
1297 */
1298 if (elementsize >= 16384) {
1299 debug("too large elements for interrupt transfers\n");
1300 return NULL;
1301 }
1302
1303 result = malloc(sizeof(*result));
1304 if (!result) {
1305 debug("ehci intr queue: out of memory\n");
1306 goto fail1;
1307 }
Hans de Goede8aa26b82014-09-24 14:06:05 +02001308 result->elementsize = elementsize;
Hans de Goede7f59d162015-06-18 22:34:33 +02001309 result->pipe = pipe;
Stephen Warren8165e342014-02-06 13:13:06 -07001310 result->first = memalign(USB_DMA_MINALIGN,
1311 sizeof(struct QH) * queuesize);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001312 if (!result->first) {
1313 debug("ehci intr queue: out of memory\n");
1314 goto fail2;
1315 }
1316 result->current = result->first;
1317 result->last = result->first + queuesize - 1;
Stephen Warren8165e342014-02-06 13:13:06 -07001318 result->tds = memalign(USB_DMA_MINALIGN,
1319 sizeof(struct qTD) * queuesize);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001320 if (!result->tds) {
1321 debug("ehci intr queue: out of memory\n");
1322 goto fail3;
1323 }
1324 memset(result->first, 0, sizeof(struct QH) * queuesize);
1325 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1326
Hans de Goede7f59d162015-06-18 22:34:33 +02001327 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
1328
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001329 for (i = 0; i < queuesize; i++) {
1330 struct QH *qh = result->first + i;
1331 struct qTD *td = result->tds + i;
1332 void **buf = &qh->buffer;
1333
Rob Herring98ae8402015-03-17 15:46:37 -05001334 qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001335 if (i == queuesize - 1)
Adrian Coxea427772014-04-10 13:29:45 +01001336 qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001337
Rob Herring98ae8402015-03-17 15:46:37 -05001338 qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td);
Adrian Coxea427772014-04-10 13:29:45 +01001339 qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
1340 qh->qh_endpt1 =
1341 cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001342 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1343 (1 << 14) |
1344 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1345 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
Adrian Coxea427772014-04-10 13:29:45 +01001346 (usb_pipedevice(pipe) << 0));
1347 qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */
1348 (1 << 0)); /* S-mask: microframe 0 */
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001349 if (dev->speed == USB_SPEED_LOW ||
1350 dev->speed == USB_SPEED_FULL) {
Hans de Goede4e2c4ad2014-09-20 16:51:22 +02001351 /* C-mask: microframes 2-4 */
1352 qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001353 }
Hans de Goede4e2c4ad2014-09-20 16:51:22 +02001354 ehci_update_endpt2_dev_n_port(dev, qh);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001355
Adrian Coxea427772014-04-10 13:29:45 +01001356 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
1357 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001358 debug("communication direction is '%s'\n",
1359 usb_pipein(pipe) ? "in" : "out");
Hans de Goede7f59d162015-06-18 22:34:33 +02001360 td->qt_token = cpu_to_hc32(
1361 QT_TOKEN_DT(toggle) |
1362 (elementsize << 16) |
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001363 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
Adrian Coxea427772014-04-10 13:29:45 +01001364 0x80); /* active */
1365 td->qt_buffer[0] =
Rob Herring98ae8402015-03-17 15:46:37 -05001366 cpu_to_hc32((unsigned long)buffer + i * elementsize);
Adrian Coxea427772014-04-10 13:29:45 +01001367 td->qt_buffer[1] =
1368 cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff);
1369 td->qt_buffer[2] =
1370 cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff);
1371 td->qt_buffer[3] =
1372 cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff);
1373 td->qt_buffer[4] =
1374 cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001375
1376 *buf = buffer + i * elementsize;
Hans de Goede7f59d162015-06-18 22:34:33 +02001377 toggle ^= 1;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001378 }
1379
Rob Herring98ae8402015-03-17 15:46:37 -05001380 flush_dcache_range((unsigned long)buffer,
Stephen Warrend3e07472013-05-24 15:03:17 -06001381 ALIGN_END_ADDR(char, buffer,
1382 queuesize * elementsize));
Rob Herring98ae8402015-03-17 15:46:37 -05001383 flush_dcache_range((unsigned long)result->first,
Stephen Warrend3e07472013-05-24 15:03:17 -06001384 ALIGN_END_ADDR(struct QH, result->first,
1385 queuesize));
Rob Herring98ae8402015-03-17 15:46:37 -05001386 flush_dcache_range((unsigned long)result->tds,
Stephen Warrend3e07472013-05-24 15:03:17 -06001387 ALIGN_END_ADDR(struct qTD, result->tds,
1388 queuesize));
1389
Hans de Goede32f2eac2014-09-24 14:06:03 +02001390 if (ctrl->periodic_schedules > 0) {
1391 if (disable_periodic(ctrl) < 0) {
1392 debug("FATAL: periodic should never fail, but did");
1393 goto fail3;
1394 }
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001395 }
1396
1397 /* hook up to periodic list */
1398 struct QH *list = &ctrl->periodic_queue;
1399 result->last->qh_link = list->qh_link;
Rob Herring98ae8402015-03-17 15:46:37 -05001400 list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001401
Rob Herring98ae8402015-03-17 15:46:37 -05001402 flush_dcache_range((unsigned long)result->last,
Stephen Warrend3e07472013-05-24 15:03:17 -06001403 ALIGN_END_ADDR(struct QH, result->last, 1));
Rob Herring98ae8402015-03-17 15:46:37 -05001404 flush_dcache_range((unsigned long)list,
Stephen Warrend3e07472013-05-24 15:03:17 -06001405 ALIGN_END_ADDR(struct QH, list, 1));
1406
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001407 if (enable_periodic(ctrl) < 0) {
1408 debug("FATAL: periodic should never fail, but did");
1409 goto fail3;
1410 }
Hans de Goede36b73102014-09-20 16:51:25 +02001411 ctrl->periodic_schedules++;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001412
1413 debug("Exit create_int_queue\n");
1414 return result;
1415fail3:
1416 if (result->tds)
1417 free(result->tds);
1418fail2:
1419 if (result->first)
1420 free(result->first);
1421 if (result)
1422 free(result);
1423fail1:
1424 return NULL;
1425}
1426
Hans de Goede029fd8e2015-05-11 20:43:52 +02001427static void *_ehci_poll_int_queue(struct usb_device *dev,
1428 struct int_queue *queue)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001429{
1430 struct QH *cur = queue->current;
Hans de Goede415548d2014-09-20 16:51:24 +02001431 struct qTD *cur_td;
Hans de Goede7f59d162015-06-18 22:34:33 +02001432 uint32_t token, toggle;
1433 unsigned long pipe = queue->pipe;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001434
1435 /* depleted queue */
1436 if (cur == NULL) {
1437 debug("Exit poll_int_queue with completed queue\n");
1438 return NULL;
1439 }
1440 /* still active */
Hans de Goede415548d2014-09-20 16:51:24 +02001441 cur_td = &queue->tds[queue->current - queue->first];
Rob Herring98ae8402015-03-17 15:46:37 -05001442 invalidate_dcache_range((unsigned long)cur_td,
Hans de Goede415548d2014-09-20 16:51:24 +02001443 ALIGN_END_ADDR(struct qTD, cur_td, 1));
Hans de Goede7f59d162015-06-18 22:34:33 +02001444 token = hc32_to_cpu(cur_td->qt_token);
1445 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) {
1446 debug("Exit poll_int_queue with no completed intr transfer. token is %x\n", token);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001447 return NULL;
1448 }
Hans de Goede7f59d162015-06-18 22:34:33 +02001449
1450 toggle = QT_TOKEN_GET_DT(token);
1451 usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle);
1452
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001453 if (!(cur->qh_link & QH_LINK_TERMINATE))
1454 queue->current++;
1455 else
1456 queue->current = NULL;
Hans de Goede8aa26b82014-09-24 14:06:05 +02001457
Rob Herring98ae8402015-03-17 15:46:37 -05001458 invalidate_dcache_range((unsigned long)cur->buffer,
Hans de Goede8aa26b82014-09-24 14:06:05 +02001459 ALIGN_END_ADDR(char, cur->buffer,
1460 queue->elementsize));
1461
Hans de Goede415548d2014-09-20 16:51:24 +02001462 debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n",
Hans de Goede7f59d162015-06-18 22:34:33 +02001463 token, cur, queue->first);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001464 return cur->buffer;
1465}
1466
1467/* Do not free buffers associated with QHs, they're owned by someone else */
Hans de Goede029fd8e2015-05-11 20:43:52 +02001468static int _ehci_destroy_int_queue(struct usb_device *dev,
1469 struct int_queue *queue)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001470{
Simon Glass24ed8942015-03-25 12:22:25 -06001471 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001472 int result = -1;
1473 unsigned long timeout;
1474
1475 if (disable_periodic(ctrl) < 0) {
1476 debug("FATAL: periodic should never fail, but did");
1477 goto out;
1478 }
Hans de Goede36b73102014-09-20 16:51:25 +02001479 ctrl->periodic_schedules--;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001480
1481 struct QH *cur = &ctrl->periodic_queue;
1482 timeout = get_timer(0) + 500; /* abort after 500ms */
Adrian Coxea427772014-04-10 13:29:45 +01001483 while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001484 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1485 if (NEXT_QH(cur) == queue->first) {
1486 debug("found candidate. removing from chain\n");
1487 cur->qh_link = queue->last->qh_link;
Rob Herring98ae8402015-03-17 15:46:37 -05001488 flush_dcache_range((unsigned long)cur,
Hans de Goedeea7b30c2014-09-20 16:51:23 +02001489 ALIGN_END_ADDR(struct QH, cur, 1));
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001490 result = 0;
1491 break;
1492 }
1493 cur = NEXT_QH(cur);
1494 if (get_timer(0) > timeout) {
1495 printf("Timeout destroying interrupt endpoint queue\n");
1496 result = -1;
1497 goto out;
1498 }
1499 }
1500
Hans de Goede36b73102014-09-20 16:51:25 +02001501 if (ctrl->periodic_schedules > 0) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001502 result = enable_periodic(ctrl);
1503 if (result < 0)
1504 debug("FATAL: periodic should never fail, but did");
1505 }
1506
1507out:
1508 free(queue->tds);
1509 free(queue->first);
1510 free(queue);
1511
1512 return result;
1513}
1514
Simon Glass24ed8942015-03-25 12:22:25 -06001515static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe,
Michal Suchanek34371212019-08-18 10:55:27 +02001516 void *buffer, int length, int interval,
1517 bool nonblock)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001518{
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001519 void *backbuffer;
1520 struct int_queue *queue;
1521 unsigned long timeout;
1522 int result = 0, ret;
1523
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001524 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1525 dev, pipe, buffer, length, interval);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001526
Hans de Goede029fd8e2015-05-11 20:43:52 +02001527 queue = _ehci_create_int_queue(dev, pipe, 1, length, buffer, interval);
Hans de Goedebd818d82014-09-24 14:06:04 +02001528 if (!queue)
1529 return -1;
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001530
1531 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
Hans de Goede029fd8e2015-05-11 20:43:52 +02001532 while ((backbuffer = _ehci_poll_int_queue(dev, queue)) == NULL)
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001533 if (get_timer(0) > timeout) {
1534 printf("Timeout poll on interrupt endpoint\n");
1535 result = -ETIMEDOUT;
1536 break;
1537 }
1538
1539 if (backbuffer != buffer) {
Rob Herring98ae8402015-03-17 15:46:37 -05001540 debug("got wrong buffer back (%p instead of %p)\n",
1541 backbuffer, buffer);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001542 return -EINVAL;
1543 }
1544
Hans de Goede029fd8e2015-05-11 20:43:52 +02001545 ret = _ehci_destroy_int_queue(dev, queue);
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001546 if (ret < 0)
1547 return ret;
1548
1549 /* everything worked out fine */
1550 return result;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001551}
Simon Glass24ed8942015-03-25 12:22:25 -06001552
Marek Vasut31232de2020-04-06 14:29:44 +02001553static int _ehci_lock_async(struct ehci_ctrl *ctrl, int lock)
1554{
1555 ctrl->async_locked = lock;
1556
1557 if (lock)
1558 return 0;
1559
1560 return ehci_disable_async(ctrl);
1561}
1562
Sven Schwermerfd09c202018-11-21 08:43:56 +01001563#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glass24ed8942015-03-25 12:22:25 -06001564int submit_bulk_msg(struct usb_device *dev, unsigned long pipe,
1565 void *buffer, int length)
1566{
1567 return _ehci_submit_bulk_msg(dev, pipe, buffer, length);
1568}
1569
1570int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1571 int length, struct devrequest *setup)
1572{
1573 return _ehci_submit_control_msg(dev, pipe, buffer, length, setup);
1574}
1575
1576int submit_int_msg(struct usb_device *dev, unsigned long pipe,
Michal Suchanek34371212019-08-18 10:55:27 +02001577 void *buffer, int length, int interval, bool nonblock)
Simon Glass24ed8942015-03-25 12:22:25 -06001578{
Michal Suchanek34371212019-08-18 10:55:27 +02001579 return _ehci_submit_int_msg(dev, pipe, buffer, length, interval,
1580 nonblock);
Simon Glass24ed8942015-03-25 12:22:25 -06001581}
Hans de Goede029fd8e2015-05-11 20:43:52 +02001582
1583struct int_queue *create_int_queue(struct usb_device *dev,
1584 unsigned long pipe, int queuesize, int elementsize,
1585 void *buffer, int interval)
1586{
1587 return _ehci_create_int_queue(dev, pipe, queuesize, elementsize,
1588 buffer, interval);
1589}
1590
1591void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1592{
1593 return _ehci_poll_int_queue(dev, queue);
1594}
1595
1596int destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1597{
1598 return _ehci_destroy_int_queue(dev, queue);
1599}
Marek Vasut31232de2020-04-06 14:29:44 +02001600
1601int usb_lock_async(struct usb_device *dev, int lock)
1602{
1603 struct ehci_ctrl *ctrl = ehci_get_ctrl(dev);
1604
1605 return _ehci_lock_async(ctrl, lock);
1606}
Simon Glass46b01792015-03-25 12:22:29 -06001607#endif
1608
Sven Schwermerfd09c202018-11-21 08:43:56 +01001609#if CONFIG_IS_ENABLED(DM_USB)
Simon Glass46b01792015-03-25 12:22:29 -06001610static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1611 unsigned long pipe, void *buffer, int length,
1612 struct devrequest *setup)
1613{
1614 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1615 dev->name, udev, udev->dev->name, udev->portnr);
1616
1617 return _ehci_submit_control_msg(udev, pipe, buffer, length, setup);
1618}
1619
1620static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1621 unsigned long pipe, void *buffer, int length)
1622{
1623 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1624 return _ehci_submit_bulk_msg(udev, pipe, buffer, length);
1625}
1626
1627static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1628 unsigned long pipe, void *buffer, int length,
Michal Suchanek34371212019-08-18 10:55:27 +02001629 int interval, bool nonblock)
Simon Glass46b01792015-03-25 12:22:29 -06001630{
1631 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
Michal Suchanek34371212019-08-18 10:55:27 +02001632 return _ehci_submit_int_msg(udev, pipe, buffer, length, interval,
1633 nonblock);
Simon Glass46b01792015-03-25 12:22:29 -06001634}
1635
Hans de Goede8a5f0662015-05-10 14:10:18 +02001636static struct int_queue *ehci_create_int_queue(struct udevice *dev,
1637 struct usb_device *udev, unsigned long pipe, int queuesize,
1638 int elementsize, void *buffer, int interval)
1639{
1640 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1641 return _ehci_create_int_queue(udev, pipe, queuesize, elementsize,
1642 buffer, interval);
1643}
1644
1645static void *ehci_poll_int_queue(struct udevice *dev, struct usb_device *udev,
1646 struct int_queue *queue)
1647{
1648 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1649 return _ehci_poll_int_queue(udev, queue);
1650}
1651
1652static int ehci_destroy_int_queue(struct udevice *dev, struct usb_device *udev,
1653 struct int_queue *queue)
1654{
1655 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1656 return _ehci_destroy_int_queue(udev, queue);
1657}
1658
Bin Menga23aa662017-09-07 06:13:19 -07001659static int ehci_get_max_xfer_size(struct udevice *dev, size_t *size)
1660{
1661 /*
1662 * EHCD can handle any transfer length as long as there is enough
1663 * free heap space left, hence set the theoretical max number here.
1664 */
1665 *size = SIZE_MAX;
1666
1667 return 0;
1668}
1669
Marek Vasut31232de2020-04-06 14:29:44 +02001670static int ehci_lock_async(struct udevice *dev, int lock)
1671{
1672 struct ehci_ctrl *ctrl = dev_get_priv(dev);
1673
1674 return _ehci_lock_async(ctrl, lock);
1675}
1676
Simon Glass46b01792015-03-25 12:22:29 -06001677int ehci_register(struct udevice *dev, struct ehci_hccr *hccr,
1678 struct ehci_hcor *hcor, const struct ehci_ops *ops,
1679 uint tweaks, enum usb_init_type init)
1680{
Hans de Goedecb8a2c12015-05-05 11:54:35 +02001681 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
Simon Glass46b01792015-03-25 12:22:29 -06001682 struct ehci_ctrl *ctrl = dev_get_priv(dev);
Heinrich Schuchardt45157d22017-11-20 19:33:39 +01001683 int ret = -1;
Simon Glass46b01792015-03-25 12:22:29 -06001684
1685 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__,
1686 dev->name, ctrl, hccr, hcor, init);
1687
Heinrich Schuchardt45157d22017-11-20 19:33:39 +01001688 if (!ctrl || !hccr || !hcor)
1689 goto err;
1690
Hans de Goedecb8a2c12015-05-05 11:54:35 +02001691 priv->desc_before_addr = true;
1692
Simon Glass46b01792015-03-25 12:22:29 -06001693 ehci_setup_ops(ctrl, ops);
1694 ctrl->hccr = hccr;
1695 ctrl->hcor = hcor;
1696 ctrl->priv = ctrl;
1697
Stephen Warren49b4c5c2015-08-20 17:38:05 -06001698 ctrl->init = init;
1699 if (ctrl->init == USB_INIT_DEVICE)
Simon Glass46b01792015-03-25 12:22:29 -06001700 goto done;
Stephen Warren49b4c5c2015-08-20 17:38:05 -06001701
Simon Glass46b01792015-03-25 12:22:29 -06001702 ret = ehci_reset(ctrl);
1703 if (ret)
1704 goto err;
1705
Mateusz Kulikowskicfb3f1c2016-04-03 13:38:26 +02001706 if (ctrl->ops.init_after_reset) {
1707 ret = ctrl->ops.init_after_reset(ctrl);
Mateusz Kulikowski3f9f8a52016-03-31 23:12:17 +02001708 if (ret)
1709 goto err;
1710 }
1711
Simon Glass46b01792015-03-25 12:22:29 -06001712 ret = ehci_common_init(ctrl, tweaks);
1713 if (ret)
1714 goto err;
1715done:
1716 return 0;
1717err:
1718 free(ctrl);
1719 debug("%s: failed, ret=%d\n", __func__, ret);
1720 return ret;
1721}
1722
1723int ehci_deregister(struct udevice *dev)
1724{
1725 struct ehci_ctrl *ctrl = dev_get_priv(dev);
1726
Stephen Warren49b4c5c2015-08-20 17:38:05 -06001727 if (ctrl->init == USB_INIT_DEVICE)
1728 return 0;
1729
Simon Glass46b01792015-03-25 12:22:29 -06001730 ehci_shutdown(ctrl);
1731
1732 return 0;
1733}
1734
1735struct dm_usb_ops ehci_usb_ops = {
1736 .control = ehci_submit_control_msg,
1737 .bulk = ehci_submit_bulk_msg,
1738 .interrupt = ehci_submit_int_msg,
Hans de Goede8a5f0662015-05-10 14:10:18 +02001739 .create_int_queue = ehci_create_int_queue,
1740 .poll_int_queue = ehci_poll_int_queue,
1741 .destroy_int_queue = ehci_destroy_int_queue,
Bin Menga23aa662017-09-07 06:13:19 -07001742 .get_max_xfer_size = ehci_get_max_xfer_size,
Marek Vasut31232de2020-04-06 14:29:44 +02001743 .lock_async = ehci_lock_async,
Simon Glass46b01792015-03-25 12:22:29 -06001744};
1745
1746#endif
Marek Vasutb43cdf92018-08-08 14:29:55 +02001747
1748#ifdef CONFIG_PHY
1749int ehci_setup_phy(struct udevice *dev, struct phy *phy, int index)
1750{
1751 int ret;
1752
1753 if (!phy)
1754 return 0;
1755
1756 ret = generic_phy_get_by_index(dev, index, phy);
1757 if (ret) {
1758 if (ret != -ENOENT) {
1759 dev_err(dev, "failed to get usb phy\n");
1760 return ret;
1761 }
1762 } else {
1763 ret = generic_phy_init(phy);
1764 if (ret) {
1765 dev_err(dev, "failed to init usb phy\n");
1766 return ret;
1767 }
1768
1769 ret = generic_phy_power_on(phy);
1770 if (ret) {
1771 dev_err(dev, "failed to power on usb phy\n");
1772 return generic_phy_exit(phy);
1773 }
1774 }
1775
1776 return 0;
1777}
1778
1779int ehci_shutdown_phy(struct udevice *dev, struct phy *phy)
1780{
1781 int ret = 0;
1782
1783 if (!phy)
1784 return 0;
1785
1786 if (generic_phy_valid(phy)) {
1787 ret = generic_phy_power_off(phy);
1788 if (ret) {
1789 dev_err(dev, "failed to power off usb phy\n");
1790 return ret;
1791 }
1792
1793 ret = generic_phy_exit(phy);
1794 if (ret) {
1795 dev_err(dev, "failed to power off usb phy\n");
1796 return ret;
1797 }
1798 }
1799
1800 return 0;
1801}
1802#else
1803int ehci_setup_phy(struct udevice *dev, struct phy *phy, int index)
1804{
1805 return 0;
1806}
1807
1808int ehci_shutdown_phy(struct udevice *dev, struct phy *phy)
1809{
1810 return 0;
1811}
1812#endif