blob: 443cc2313465fcc872f21fbca52914a9bf50c2f2 [file] [log] [blame]
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001/*-
2 * Copyright (c) 2007-2008, Juniper Networks, Inc.
michaeldb632992008-12-10 17:55:19 +01003 * Copyright (c) 2008, Excito Elektronik i Skåne AB
Remy Böhmerc0d722f2008-12-13 22:51:58 +01004 * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it>
5 *
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01006 * All rights reserved.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation version 2 of
11 * the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010023#include <common.h>
Patrick Georgi8f62ca62013-03-06 14:08:31 +000024#include <errno.h>
michaeldb632992008-12-10 17:55:19 +010025#include <asm/byteorder.h>
Lucas Stach93ad9082012-09-06 08:00:13 +020026#include <asm/unaligned.h>
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010027#include <usb.h>
28#include <asm/io.h>
michaeldb632992008-12-10 17:55:19 +010029#include <malloc.h>
Stefan Roese67333f72010-11-26 15:43:28 +010030#include <watchdog.h>
Patrick Georgi8f62ca62013-03-06 14:08:31 +000031#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020032
33#include "ehci.h"
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010034
Lucas Stach676ae062012-09-26 00:14:35 +020035#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
36#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
37#endif
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010038
Lucas Stach676ae062012-09-26 00:14:35 +020039static struct ehci_ctrl {
40 struct ehci_hccr *hccr; /* R/O registers, not need for volatile */
41 struct ehci_hcor *hcor;
42 int rootdev;
43 uint16_t portreset;
Patrick Georgi8f62ca62013-03-06 14:08:31 +000044 struct QH qh_list __aligned(USB_DMA_MINALIGN);
45 struct QH periodic_queue __aligned(USB_DMA_MINALIGN);
46 uint32_t *periodic_list;
47 int ntds;
Lucas Stach676ae062012-09-26 00:14:35 +020048} ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
Tom Rini71c5de42012-07-15 22:14:24 +000049
50#define ALIGN_END_ADDR(type, ptr, size) \
51 ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010052
michaeldb632992008-12-10 17:55:19 +010053static struct descriptor {
54 struct usb_hub_descriptor hub;
55 struct usb_device_descriptor device;
56 struct usb_linux_config_descriptor config;
57 struct usb_linux_interface_descriptor interface;
58 struct usb_endpoint_descriptor endpoint;
59} __attribute__ ((packed)) descriptor = {
60 {
61 0x8, /* bDescLength */
62 0x29, /* bDescriptorType: hub descriptor */
63 2, /* bNrPorts -- runtime modified */
64 0, /* wHubCharacteristics */
Vincent Palatin5f4b4f22011-12-05 14:52:22 -080065 10, /* bPwrOn2PwrGood */
michaeldb632992008-12-10 17:55:19 +010066 0, /* bHubCntrCurrent */
67 {}, /* Device removable */
68 {} /* at most 7 ports! XXX */
69 },
70 {
71 0x12, /* bLength */
72 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030073 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michaeldb632992008-12-10 17:55:19 +010074 9, /* bDeviceClass: UDCLASS_HUB */
75 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
76 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
77 64, /* bMaxPacketSize: 64 bytes */
78 0x0000, /* idVendor */
79 0x0000, /* idProduct */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030080 cpu_to_le16(0x0100), /* bcdDevice */
michaeldb632992008-12-10 17:55:19 +010081 1, /* iManufacturer */
82 2, /* iProduct */
83 0, /* iSerialNumber */
84 1 /* bNumConfigurations: 1 */
85 },
86 {
87 0x9,
88 2, /* bDescriptorType: UDESC_CONFIG */
89 cpu_to_le16(0x19),
90 1, /* bNumInterface */
91 1, /* bConfigurationValue */
92 0, /* iConfiguration */
93 0x40, /* bmAttributes: UC_SELF_POWER */
94 0 /* bMaxPower */
95 },
96 {
97 0x9, /* bLength */
98 4, /* bDescriptorType: UDESC_INTERFACE */
99 0, /* bInterfaceNumber */
100 0, /* bAlternateSetting */
101 1, /* bNumEndpoints */
102 9, /* bInterfaceClass: UICLASS_HUB */
103 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
104 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
105 0 /* iInterface */
106 },
107 {
108 0x7, /* bLength */
109 5, /* bDescriptorType: UDESC_ENDPOINT */
110 0x81, /* bEndpointAddress:
111 * UE_DIR_IN | EHCI_INTR_ENDPT
112 */
113 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix8f8bd562009-10-31 12:37:38 -0500114 8, /* wMaxPacketSize */
michaeldb632992008-12-10 17:55:19 +0100115 255 /* bInterval */
116 },
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100117};
118
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100119#if defined(CONFIG_EHCI_IS_TDI)
120#define ehci_is_TDI() (1)
121#else
122#define ehci_is_TDI() (0)
123#endif
124
Jim Linb068deb2013-03-27 00:52:32 +0000125int __ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
126{
127 return PORTSC_PSPD(reg);
128}
129
130int ehci_get_port_speed(struct ehci_hcor *hcor, uint32_t reg)
131 __attribute__((weak, alias("__ehci_get_port_speed")));
132
133void __ehci_set_usbmode(int index)
134{
135 uint32_t tmp;
136 uint32_t *reg_ptr;
137
138 reg_ptr = (uint32_t *)((u8 *)&ehcic[index].hcor->or_usbcmd + USBMODE);
139 tmp = ehci_readl(reg_ptr);
140 tmp |= USBMODE_CM_HC;
141#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
142 tmp |= USBMODE_BE;
143#endif
144 ehci_writel(reg_ptr, tmp);
145}
146
147void ehci_set_usbmode(int index)
148 __attribute__((weak, alias("__ehci_set_usbmode")));
149
Marek Vasut3874b6d2011-07-11 02:37:01 +0200150void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
151{
152 mdelay(50);
153}
154
155void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
156 __attribute__((weak, alias("__ehci_powerup_fixup")));
157
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100158static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michaeldb632992008-12-10 17:55:19 +0100159{
michael51ab1422008-12-11 13:43:55 +0100160 uint32_t result;
161 do {
162 result = ehci_readl(ptr);
Wolfgang Denk09c83a42010-10-22 14:23:00 +0200163 udelay(5);
michael51ab1422008-12-11 13:43:55 +0100164 if (result == ~(uint32_t)0)
165 return -1;
166 result &= mask;
167 if (result == done)
168 return 0;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100169 usec--;
170 } while (usec > 0);
michael51ab1422008-12-11 13:43:55 +0100171 return -1;
172}
173
Lucas Stach676ae062012-09-26 00:14:35 +0200174static int ehci_reset(int index)
michael51ab1422008-12-11 13:43:55 +0100175{
176 uint32_t cmd;
michael51ab1422008-12-11 13:43:55 +0100177 int ret = 0;
178
Lucas Stach676ae062012-09-26 00:14:35 +0200179 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Stefan Roese273d7202010-11-26 15:44:00 +0100180 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
Lucas Stach676ae062012-09-26 00:14:35 +0200181 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
182 ret = handshake((uint32_t *)&ehcic[index].hcor->or_usbcmd,
183 CMD_RESET, 0, 250 * 1000);
michael51ab1422008-12-11 13:43:55 +0100184 if (ret < 0) {
185 printf("EHCI fail to reset\n");
186 goto out;
187 }
188
Jim Linb068deb2013-03-27 00:52:32 +0000189 if (ehci_is_TDI())
190 ehci_set_usbmode(index);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000191
192#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
Lucas Stach676ae062012-09-26 00:14:35 +0200193 cmd = ehci_readl(&ehcic[index].hcor->or_txfilltuning);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200194 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass9ab4ce22012-02-27 10:52:47 +0000195 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
Lucas Stach676ae062012-09-26 00:14:35 +0200196 ehci_writel(&ehcic[index].hcor->or_txfilltuning, cmd);
Simon Glass9ab4ce22012-02-27 10:52:47 +0000197#endif
michael51ab1422008-12-11 13:43:55 +0100198out:
199 return ret;
michaeldb632992008-12-10 17:55:19 +0100200}
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100201
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100202static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
203{
Marek Vasutb8adb122012-04-09 04:07:46 +0200204 uint32_t delta, next;
205 uint32_t addr = (uint32_t)buf;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100206 int idx;
207
Ilya Yanok189a6952012-07-15 04:43:49 +0000208 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutb8adb122012-04-09 04:07:46 +0200209 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
210
Ilya Yanok189a6952012-07-15 04:43:49 +0000211 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
212
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100213 idx = 0;
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200214 while (idx < QT_BUFFER_CNT) {
michaeldb632992008-12-10 17:55:19 +0100215 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denk3ed16072010-10-19 16:13:15 +0200216 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200217 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100218 delta = next - addr;
219 if (delta >= sz)
220 break;
221 sz -= delta;
222 addr = next;
223 idx++;
224 }
225
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200226 if (idx == QT_BUFFER_CNT) {
Ilya Yanok2af16f82012-07-15 04:43:52 +0000227 printf("out of buffer pointers (%u bytes left)\n", sz);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100228 return -1;
229 }
230
231 return 0;
232}
233
Ilya Yanokc60795f2012-11-06 13:48:20 +0000234static inline u8 ehci_encode_speed(enum usb_device_speed speed)
235{
236 #define QH_HIGH_SPEED 2
237 #define QH_FULL_SPEED 0
238 #define QH_LOW_SPEED 1
239 if (speed == USB_SPEED_HIGH)
240 return QH_HIGH_SPEED;
241 if (speed == USB_SPEED_LOW)
242 return QH_LOW_SPEED;
243 return QH_FULL_SPEED;
244}
245
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100246static int
247ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
248 int length, struct devrequest *req)
249{
Tom Rini71c5de42012-07-15 22:14:24 +0000250 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200251 struct qTD *qtd;
252 int qtd_count = 0;
Marek Vasutde98e8b2012-04-08 23:32:05 +0200253 int qtd_counter = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100254 volatile struct qTD *vtd;
255 unsigned long ts;
256 uint32_t *tdp;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200257 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100258 uint32_t c, toggle;
michaeldb632992008-12-10 17:55:19 +0100259 uint32_t cmd;
Simon Glass96820a32011-02-07 14:42:16 -0800260 int timeout;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100261 int ret = 0;
Lucas Stach676ae062012-09-26 00:14:35 +0200262 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100263
michaeldb632992008-12-10 17:55:19 +0100264 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100265 buffer, length, req);
266 if (req != NULL)
michaeldb632992008-12-10 17:55:19 +0100267 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100268 req->request, req->request,
269 req->requesttype, req->requesttype,
270 le16_to_cpu(req->value), le16_to_cpu(req->value),
michaeldb632992008-12-10 17:55:19 +0100271 le16_to_cpu(req->index));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100272
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200273#define PKT_ALIGN 512
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200274 /*
275 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
276 * described by a transfer descriptor (the qTD). The qTDs form a linked
277 * list with a queue head (QH).
278 *
279 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
280 * have its beginning in a qTD transfer and its end in the following
281 * one, so the qTD transfer lengths have to be chosen accordingly.
282 *
283 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
284 * single pages. The first data buffer can start at any offset within a
285 * page (not considering the cache-line alignment issues), while the
286 * following buffers must be page-aligned. There is no alignment
287 * constraint on the size of a qTD transfer.
288 */
289 if (req != NULL)
290 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
291 qtd_count += 1 + 1;
292 if (length > 0 || req == NULL) {
293 /*
294 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200295 * data payload (not considering the first qTD transfer, which
296 * may be longer or shorter, and the final one, which may be
297 * shorter).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200298 *
299 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200300 * transfer size is aligned to PKT_ALIGN, which is a multiple of
301 * wMaxPacketSize (except in some cases for interrupt transfers,
302 * see comment in submit_int_msg()).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200303 *
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200304 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200305 * QT_BUFFER_CNT full pages will be used.
306 */
307 int xfr_sz = QT_BUFFER_CNT;
308 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200309 * However, if the input buffer is not aligned to PKT_ALIGN, the
310 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200311 * data buffer of each transfer will be page-unaligned.
312 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200313 if ((uint32_t)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200314 xfr_sz--;
315 /* Convert the qTD transfer size to bytes. */
316 xfr_sz *= EHCI_PAGE_SIZE;
317 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200318 * Approximate by excess the number of qTDs that will be
319 * required for the data payload. The exact formula is way more
320 * complicated and saves at most 2 qTDs, i.e. a total of 128
321 * bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200322 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200323 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200324 }
325/*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200326 * Threshold value based on the worst-case total size of the allocated qTDs for
327 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200328 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200329#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200330#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
331#endif
332 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
333 if (qtd == NULL) {
334 printf("unable to allocate TDs\n");
335 return -1;
336 }
337
Tom Rini71c5de42012-07-15 22:14:24 +0000338 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200339 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200340
Marek Vasutb8adb122012-04-09 04:07:46 +0200341 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
342
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200343 /*
344 * Setup QH (3.6 in ehci-r10.pdf)
345 *
346 * qh_link ................. 03-00 H
347 * qh_endpt1 ............... 07-04 H
348 * qh_endpt2 ............... 0B-08 H
349 * - qh_curtd
350 * qh_overlay.qt_next ...... 13-10 H
351 * - qh_overlay.qt_altnext
352 */
Lucas Stach676ae062012-09-26 00:14:35 +0200353 qh->qh_link = cpu_to_hc32((uint32_t)&ctrl->qh_list | QH_LINK_TYPE_QH);
Ilya Yanokc60795f2012-11-06 13:48:20 +0000354 c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200355 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200356 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200357 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200358 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
Ilya Yanokc60795f2012-11-06 13:48:20 +0000359 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200360 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
361 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini71c5de42012-07-15 22:14:24 +0000362 qh->qh_endpt1 = cpu_to_hc32(endpt);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200363 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
364 QH_ENDPT2_HUBADDR(dev->parent->devnum) |
365 QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini71c5de42012-07-15 22:14:24 +0000366 qh->qh_endpt2 = cpu_to_hc32(endpt);
367 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100368
Tom Rini71c5de42012-07-15 22:14:24 +0000369 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100370
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100371 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200372 /*
373 * Setup request qTD (3.5 in ehci-r10.pdf)
374 *
375 * qt_next ................ 03-00 H
376 * qt_altnext ............. 07-04 H
377 * qt_token ............... 0B-08 H
378 *
379 * [ buffer, buffer_hi ] loaded with "req".
380 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200381 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
382 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200383 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
384 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
385 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
386 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200387 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200388 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
389 printf("unable to construct SETUP TD\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100390 goto fail;
391 }
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200392 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200393 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
394 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100395 toggle = 1;
396 }
397
398 if (length > 0 || req == NULL) {
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200399 uint8_t *buf_ptr = buffer;
400 int left_length = length;
401
402 do {
403 /*
404 * Determine the size of this qTD transfer. By default,
405 * QT_BUFFER_CNT full pages can be used.
406 */
407 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
408 /*
409 * However, if the input buffer is not page-aligned, the
410 * portion of the first page before the buffer start
411 * offset within that page is unusable.
412 */
413 xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
414 /*
415 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200416 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200417 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200418 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200419 /*
420 * This transfer may be shorter than the available qTD
421 * transfer size that has just been computed.
422 */
423 xfr_bytes = min(xfr_bytes, left_length);
424
425 /*
426 * Setup request qTD (3.5 in ehci-r10.pdf)
427 *
428 * qt_next ................ 03-00 H
429 * qt_altnext ............. 07-04 H
430 * qt_token ............... 0B-08 H
431 *
432 * [ buffer, buffer_hi ] loaded with "buffer".
433 */
434 qtd[qtd_counter].qt_next =
435 cpu_to_hc32(QT_NEXT_TERMINATE);
436 qtd[qtd_counter].qt_altnext =
437 cpu_to_hc32(QT_NEXT_TERMINATE);
438 token = QT_TOKEN_DT(toggle) |
439 QT_TOKEN_TOTALBYTES(xfr_bytes) |
440 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
441 QT_TOKEN_CERR(3) |
442 QT_TOKEN_PID(usb_pipein(pipe) ?
443 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
444 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
445 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
446 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
447 xfr_bytes)) {
448 printf("unable to construct DATA TD\n");
449 goto fail;
450 }
451 /* Update previous qTD! */
452 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
453 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200454 /*
455 * Data toggle has to be adjusted since the qTD transfer
456 * size is not always an even multiple of
457 * wMaxPacketSize.
458 */
459 if ((xfr_bytes / maxpacket) & 1)
460 toggle ^= 1;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200461 buf_ptr += xfr_bytes;
462 left_length -= xfr_bytes;
463 } while (left_length > 0);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100464 }
465
466 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200467 /*
468 * Setup request qTD (3.5 in ehci-r10.pdf)
469 *
470 * qt_next ................ 03-00 H
471 * qt_altnext ............. 07-04 H
472 * qt_token ............... 0B-08 H
473 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200474 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
475 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200476 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200477 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
478 QT_TOKEN_PID(usb_pipein(pipe) ?
479 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
480 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200481 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200482 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200483 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
484 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100485 }
486
Lucas Stach676ae062012-09-26 00:14:35 +0200487 ctrl->qh_list.qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100488
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100489 /* Flush dcache */
Lucas Stach676ae062012-09-26 00:14:35 +0200490 flush_dcache_range((uint32_t)&ctrl->qh_list,
491 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Tom Rini71c5de42012-07-15 22:14:24 +0000492 flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200493 flush_dcache_range((uint32_t)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200494 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100495
Ilya Yanokc7701af2012-07-15 22:12:08 +0000496 /* Set async. queue head pointer. */
Lucas Stach676ae062012-09-26 00:14:35 +0200497 ehci_writel(&ctrl->hcor->or_asynclistaddr, (uint32_t)&ctrl->qh_list);
Ilya Yanokc7701af2012-07-15 22:12:08 +0000498
Lucas Stach676ae062012-09-26 00:14:35 +0200499 usbsts = ehci_readl(&ctrl->hcor->or_usbsts);
500 ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100501
502 /* Enable async. schedule. */
Lucas Stach676ae062012-09-26 00:14:35 +0200503 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michael51ab1422008-12-11 13:43:55 +0100504 cmd |= CMD_ASE;
Lucas Stach676ae062012-09-26 00:14:35 +0200505 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michaeldb632992008-12-10 17:55:19 +0100506
Lucas Stach676ae062012-09-26 00:14:35 +0200507 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100508 100 * 1000);
509 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200510 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100511 goto fail;
michael51ab1422008-12-11 13:43:55 +0100512 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100513
514 /* Wait for TDs to be processed. */
515 ts = get_timer(0);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200516 vtd = &qtd[qtd_counter - 1];
Simon Glass96820a32011-02-07 14:42:16 -0800517 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100518 do {
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100519 /* Invalidate dcache */
Lucas Stach676ae062012-09-26 00:14:35 +0200520 invalidate_dcache_range((uint32_t)&ctrl->qh_list,
521 ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1));
Tom Rini71c5de42012-07-15 22:14:24 +0000522 invalidate_dcache_range((uint32_t)qh,
523 ALIGN_END_ADDR(struct QH, qh, 1));
Marek Vasutb8adb122012-04-09 04:07:46 +0200524 invalidate_dcache_range((uint32_t)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200525 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutb8adb122012-04-09 04:07:46 +0200526
michaeldb632992008-12-10 17:55:19 +0100527 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200528 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100529 break;
Stefan Roese67333f72010-11-26 15:43:28 +0100530 WATCHDOG_RESET();
Simon Glass96820a32011-02-07 14:42:16 -0800531 } while (get_timer(ts) < timeout);
532
Ilya Yanok189a6952012-07-15 04:43:49 +0000533 /*
534 * Invalidate the memory area occupied by buffer
535 * Don't try to fix the buffer alignment, if it isn't properly
536 * aligned it's upper layer's fault so let invalidate_dcache_range()
537 * vow about it. But we have to fix the length as it's actual
538 * transfer length and can be unaligned. This is potentially
539 * dangerous operation, it's responsibility of the calling
540 * code to make sure enough space is reserved.
541 */
542 invalidate_dcache_range((uint32_t)buffer,
543 ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutb8adb122012-04-09 04:07:46 +0200544
Simon Glass96820a32011-02-07 14:42:16 -0800545 /* Check that the TD processing happened */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200546 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glass96820a32011-02-07 14:42:16 -0800547 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100548
549 /* Disable async schedule. */
Lucas Stach676ae062012-09-26 00:14:35 +0200550 cmd = ehci_readl(&ctrl->hcor->or_usbcmd);
michaeldb632992008-12-10 17:55:19 +0100551 cmd &= ~CMD_ASE;
Lucas Stach676ae062012-09-26 00:14:35 +0200552 ehci_writel(&ctrl->hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +0100553
Lucas Stach676ae062012-09-26 00:14:35 +0200554 ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100555 100 * 1000);
556 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200557 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100558 goto fail;
michael51ab1422008-12-11 13:43:55 +0100559 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100560
Tom Rini71c5de42012-07-15 22:14:24 +0000561 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200562 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michaeldb632992008-12-10 17:55:19 +0100563 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200564 switch (QT_TOKEN_GET_STATUS(token) &
565 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100566 case 0:
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200567 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100568 usb_settoggle(dev, usb_pipeendpoint(pipe),
569 usb_pipeout(pipe), toggle);
570 dev->status = 0;
571 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200572 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100573 dev->status = USB_ST_STALLED;
574 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200575 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
576 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100577 dev->status = USB_ST_BUF_ERR;
578 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200579 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
580 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100581 dev->status = USB_ST_BABBLE_DET;
582 break;
583 default:
584 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200585 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschin222d6df2010-11-02 11:47:29 +0100586 dev->status |= USB_ST_STALLED;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100587 break;
588 }
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200589 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100590 } else {
591 dev->act_len = 0;
michaeldb632992008-12-10 17:55:19 +0100592 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
Lucas Stach676ae062012-09-26 00:14:35 +0200593 dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts),
594 ehci_readl(&ctrl->hcor->or_portsc[0]),
595 ehci_readl(&ctrl->hcor->or_portsc[1]));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100596 }
597
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200598 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100599 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
600
601fail:
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200602 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100603 return -1;
604}
605
michaeldb632992008-12-10 17:55:19 +0100606int
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100607ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
608 int length, struct devrequest *req)
609{
610 uint8_t tmpbuf[4];
611 u16 typeReq;
michaeldb632992008-12-10 17:55:19 +0100612 void *srcptr = NULL;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100613 int len, srclen;
614 uint32_t reg;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100615 uint32_t *status_reg;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000616 int port = le16_to_cpu(req->index) & 0xff;
Lucas Stach676ae062012-09-26 00:14:35 +0200617 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100618
619 srclen = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100620
michaeldb632992008-12-10 17:55:19 +0100621 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100622 req->request, req->request,
623 req->requesttype, req->requesttype,
624 le16_to_cpu(req->value), le16_to_cpu(req->index));
625
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530626 typeReq = req->request | req->requesttype << 8;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100627
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530628 switch (typeReq) {
Kuo-Jung Su9c6a9d72013-05-15 15:29:20 +0800629 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
630 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
631 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
632 if (!port || port > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
633 printf("The request port(%d) is not configured\n", port - 1);
634 return -1;
635 }
636 status_reg = (uint32_t *)&ctrl->hcor->or_portsc[port - 1];
637 break;
638 default:
639 status_reg = NULL;
640 break;
641 }
642
643 switch (typeReq) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100644 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
645 switch (le16_to_cpu(req->value) >> 8) {
646 case USB_DT_DEVICE:
michaeldb632992008-12-10 17:55:19 +0100647 debug("USB_DT_DEVICE request\n");
648 srcptr = &descriptor.device;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200649 srclen = descriptor.device.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100650 break;
651 case USB_DT_CONFIG:
michaeldb632992008-12-10 17:55:19 +0100652 debug("USB_DT_CONFIG config\n");
653 srcptr = &descriptor.config;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200654 srclen = descriptor.config.bLength +
655 descriptor.interface.bLength +
656 descriptor.endpoint.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100657 break;
658 case USB_DT_STRING:
michaeldb632992008-12-10 17:55:19 +0100659 debug("USB_DT_STRING config\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100660 switch (le16_to_cpu(req->value) & 0xff) {
661 case 0: /* Language */
662 srcptr = "\4\3\1\0";
663 srclen = 4;
664 break;
665 case 1: /* Vendor */
666 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
667 srclen = 14;
668 break;
669 case 2: /* Product */
670 srcptr = "\52\3E\0H\0C\0I\0 "
671 "\0H\0o\0s\0t\0 "
672 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
673 srclen = 42;
674 break;
675 default:
michaeldb632992008-12-10 17:55:19 +0100676 debug("unknown value DT_STRING %x\n",
677 le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100678 goto unknown;
679 }
680 break;
681 default:
michaeldb632992008-12-10 17:55:19 +0100682 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100683 goto unknown;
684 }
685 break;
686 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
687 switch (le16_to_cpu(req->value) >> 8) {
688 case USB_DT_HUB:
michaeldb632992008-12-10 17:55:19 +0100689 debug("USB_DT_HUB config\n");
690 srcptr = &descriptor.hub;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200691 srclen = descriptor.hub.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100692 break;
693 default:
michaeldb632992008-12-10 17:55:19 +0100694 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100695 goto unknown;
696 }
697 break;
698 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michaeldb632992008-12-10 17:55:19 +0100699 debug("USB_REQ_SET_ADDRESS\n");
Lucas Stach676ae062012-09-26 00:14:35 +0200700 ctrl->rootdev = le16_to_cpu(req->value);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100701 break;
702 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michaeldb632992008-12-10 17:55:19 +0100703 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100704 /* Nothing to do */
705 break;
706 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
707 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
708 tmpbuf[1] = 0;
709 srcptr = tmpbuf;
710 srclen = 2;
711 break;
michaeldb632992008-12-10 17:55:19 +0100712 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100713 memset(tmpbuf, 0, 4);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100714 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100715 if (reg & EHCI_PS_CS)
716 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
717 if (reg & EHCI_PS_PE)
718 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
719 if (reg & EHCI_PS_SUSP)
720 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
721 if (reg & EHCI_PS_OCA)
722 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300723 if (reg & EHCI_PS_PR)
724 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100725 if (reg & EHCI_PS_PP)
726 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese597eb282009-01-21 17:12:01 +0100727
728 if (ehci_is_TDI()) {
Jim Linb068deb2013-03-27 00:52:32 +0000729 switch (ehci_get_port_speed(ctrl->hcor, reg)) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200730 case PORTSC_PSPD_FS:
Stefan Roese597eb282009-01-21 17:12:01 +0100731 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200732 case PORTSC_PSPD_LS:
Stefan Roese597eb282009-01-21 17:12:01 +0100733 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
734 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200735 case PORTSC_PSPD_HS:
Stefan Roese597eb282009-01-21 17:12:01 +0100736 default:
737 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
738 break;
739 }
740 } else {
741 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
742 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100743
744 if (reg & EHCI_PS_CSC)
745 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
746 if (reg & EHCI_PS_PEC)
747 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
748 if (reg & EHCI_PS_OCC)
749 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000750 if (ctrl->portreset & (1 << port))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100751 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100752
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100753 srcptr = tmpbuf;
754 srclen = 4;
755 break;
michaeldb632992008-12-10 17:55:19 +0100756 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100757 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100758 reg &= ~EHCI_PS_CLEAR;
759 switch (le16_to_cpu(req->value)) {
michael51ab1422008-12-11 13:43:55 +0100760 case USB_PORT_FEAT_ENABLE:
761 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100762 ehci_writel(status_reg, reg);
michael51ab1422008-12-11 13:43:55 +0100763 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100764 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200765 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) {
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100766 reg |= EHCI_PS_PP;
767 ehci_writel(status_reg, reg);
768 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100769 break;
770 case USB_PORT_FEAT_RESET:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100771 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
772 !ehci_is_TDI() &&
773 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100774 /* Low speed device, give up ownership. */
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100775 debug("port %d low speed --> companion\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000776 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100777 reg |= EHCI_PS_PO;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100778 ehci_writel(status_reg, reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100779 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100780 } else {
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300781 int ret;
782
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100783 reg |= EHCI_PS_PR;
784 reg &= ~EHCI_PS_PE;
785 ehci_writel(status_reg, reg);
786 /*
787 * caller must wait, then call GetPortStatus
788 * usb 2.0 specification say 50 ms resets on
789 * root
790 */
Marek Vasut3874b6d2011-07-11 02:37:01 +0200791 ehci_powerup_fixup(status_reg, &reg);
792
Chris Zhangb4161912010-01-06 13:34:04 -0800793 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300794 /*
795 * A host controller must terminate the reset
796 * and stabilize the state of the port within
797 * 2 milliseconds
798 */
799 ret = handshake(status_reg, EHCI_PS_PR, 0,
800 2 * 1000);
801 if (!ret)
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000802 ctrl->portreset |= 1 << port;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300803 else
804 printf("port(%d) reset error\n",
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000805 port - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100806 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100807 break;
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000808 case USB_PORT_FEAT_TEST:
809 reg &= ~(0xf << 16);
810 reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16;
811 ehci_writel(status_reg, reg);
812 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100813 default:
michaeldb632992008-12-10 17:55:19 +0100814 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100815 goto unknown;
816 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100817 /* unblock posted writes */
Lucas Stach676ae062012-09-26 00:14:35 +0200818 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100819 break;
michaeldb632992008-12-10 17:55:19 +0100820 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100821 reg = ehci_readl(status_reg);
Simon Glassed10e662013-05-10 19:49:00 -0700822 reg &= ~EHCI_PS_CLEAR;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100823 switch (le16_to_cpu(req->value)) {
824 case USB_PORT_FEAT_ENABLE:
825 reg &= ~EHCI_PS_PE;
826 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100827 case USB_PORT_FEAT_C_ENABLE:
Simon Glassed10e662013-05-10 19:49:00 -0700828 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100829 break;
830 case USB_PORT_FEAT_POWER:
Lucas Stach676ae062012-09-26 00:14:35 +0200831 if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams)))
Simon Glassed10e662013-05-10 19:49:00 -0700832 reg &= ~EHCI_PS_PP;
833 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100834 case USB_PORT_FEAT_C_CONNECTION:
Simon Glassed10e662013-05-10 19:49:00 -0700835 reg |= EHCI_PS_CSC;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100836 break;
michael51ab1422008-12-11 13:43:55 +0100837 case USB_PORT_FEAT_OVER_CURRENT:
Simon Glassed10e662013-05-10 19:49:00 -0700838 reg |= EHCI_PS_OCC;
michael51ab1422008-12-11 13:43:55 +0100839 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100840 case USB_PORT_FEAT_C_RESET:
Julius Werner7d9aa8f2013-02-28 18:08:40 +0000841 ctrl->portreset &= ~(1 << port);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100842 break;
843 default:
michaeldb632992008-12-10 17:55:19 +0100844 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100845 goto unknown;
846 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100847 ehci_writel(status_reg, reg);
848 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +0200849 (void) ehci_readl(&ctrl->hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100850 break;
851 default:
michaeldb632992008-12-10 17:55:19 +0100852 debug("Unknown request\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100853 goto unknown;
854 }
855
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000856 mdelay(1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100857 len = min3(srclen, le16_to_cpu(req->length), length);
858 if (srcptr != NULL && len > 0)
859 memcpy(buffer, srcptr, len);
michaeldb632992008-12-10 17:55:19 +0100860 else
861 debug("Len is 0\n");
862
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100863 dev->act_len = len;
864 dev->status = 0;
865 return 0;
866
867unknown:
michaeldb632992008-12-10 17:55:19 +0100868 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100869 req->requesttype, req->request, le16_to_cpu(req->value),
870 le16_to_cpu(req->index), le16_to_cpu(req->length));
871
872 dev->act_len = 0;
873 dev->status = USB_ST_STALLED;
874 return -1;
875}
876
Lucas Stachc7e3b2b2012-09-26 00:14:34 +0200877int usb_lowlevel_stop(int index)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100878{
Lucas Stach676ae062012-09-26 00:14:35 +0200879 return ehci_hcd_stop(index);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100880}
881
Lucas Stachc7e3b2b2012-09-26 00:14:34 +0200882int usb_lowlevel_init(int index, void **controller)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100883{
884 uint32_t reg;
michaeldb632992008-12-10 17:55:19 +0100885 uint32_t cmd;
Lucas Stach676ae062012-09-26 00:14:35 +0200886 struct QH *qh_list;
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000887 struct QH *periodic;
888 int i;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100889
Lucas Stach676ae062012-09-26 00:14:35 +0200890 if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100891 return -1;
892
michael51ab1422008-12-11 13:43:55 +0100893 /* EHCI spec section 4.1 */
Lucas Stach676ae062012-09-26 00:14:35 +0200894 if (ehci_reset(index))
michael51ab1422008-12-11 13:43:55 +0100895 return -1;
896
Stefan Roese832e6142009-01-21 17:12:10 +0100897#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
Lucas Stach676ae062012-09-26 00:14:35 +0200898 if (ehci_hcd_init(index, &ehcic[index].hccr, &ehcic[index].hcor))
Stefan Roese832e6142009-01-21 17:12:10 +0100899 return -1;
900#endif
Vincent Palatin29828372012-12-12 17:55:22 -0800901 /* Set the high address word (aka segment) for 64-bit controller */
902 if (ehci_readl(&ehcic[index].hccr->cr_hccparams) & 1)
903 ehci_writel(ehcic[index].hcor->or_ctrldssegment, 0);
Stefan Roese832e6142009-01-21 17:12:10 +0100904
Lucas Stach676ae062012-09-26 00:14:35 +0200905 qh_list = &ehcic[index].qh_list;
906
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100907 /* Set head of reclaim list */
Tom Rini71c5de42012-07-15 22:14:24 +0000908 memset(qh_list, 0, sizeof(*qh_list));
909 qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200910 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
911 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini71c5de42012-07-15 22:14:24 +0000912 qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
913 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
914 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200915 qh_list->qh_overlay.qt_token =
916 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100917
Patrick Georgi8f62ca62013-03-06 14:08:31 +0000918 /* Set async. queue head pointer. */
919 ehci_writel(&ehcic[index].hcor->or_asynclistaddr, (uint32_t)qh_list);
920
921 /*
922 * Set up periodic list
923 * Step 1: Parent QH for all periodic transfers.
924 */
925 periodic = &ehcic[index].periodic_queue;
926 memset(periodic, 0, sizeof(*periodic));
927 periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE);
928 periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
929 periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
930
931 /*
932 * Step 2: Setup frame-list: Every microframe, USB tries the same list.
933 * In particular, device specifications on polling frequency
934 * are disregarded. Keyboards seem to send NAK/NYet reliably
935 * when polled with an empty buffer.
936 *
937 * Split Transactions will be spread across microframes using
938 * S-mask and C-mask.
939 */
940 ehcic[index].periodic_list = memalign(4096, 1024*4);
941 if (!ehcic[index].periodic_list)
942 return -ENOMEM;
943 for (i = 0; i < 1024; i++) {
944 ehcic[index].periodic_list[i] = (uint32_t)periodic
945 | QH_LINK_TYPE_QH;
946 }
947
948 /* Set periodic list base address */
949 ehci_writel(&ehcic[index].hcor->or_periodiclistbase,
950 (uint32_t)ehcic[index].periodic_list);
951
Lucas Stach676ae062012-09-26 00:14:35 +0200952 reg = ehci_readl(&ehcic[index].hccr->cr_hcsparams);
michael51ab1422008-12-11 13:43:55 +0100953 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
Lucas Stach7a46b2c2012-09-28 00:26:19 +0200954 debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100955 /* Port Indicators */
956 if (HCS_INDICATOR(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +0200957 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
958 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100959 /* Port Power Control */
960 if (HCS_PPC(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +0200961 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
962 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100963
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100964 /* Start the host controller. */
Lucas Stach676ae062012-09-26 00:14:35 +0200965 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Wolfgang Denkf15c6512009-02-12 00:08:39 +0100966 /*
967 * Philips, Intel, and maybe others need CMD_RUN before the
968 * root hub will detect new devices (why?); NEC doesn't
969 */
michael51ab1422008-12-11 13:43:55 +0100970 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
971 cmd |= CMD_RUN;
Lucas Stach676ae062012-09-26 00:14:35 +0200972 ehci_writel(&ehcic[index].hcor->or_usbcmd, cmd);
michael51ab1422008-12-11 13:43:55 +0100973
974 /* take control over the ports */
Lucas Stach676ae062012-09-26 00:14:35 +0200975 cmd = ehci_readl(&ehcic[index].hcor->or_configflag);
michael51ab1422008-12-11 13:43:55 +0100976 cmd |= FLAG_CF;
Lucas Stach676ae062012-09-26 00:14:35 +0200977 ehci_writel(&ehcic[index].hcor->or_configflag, cmd);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100978 /* unblock posted write */
Lucas Stach676ae062012-09-26 00:14:35 +0200979 cmd = ehci_readl(&ehcic[index].hcor->or_usbcmd);
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000980 mdelay(5);
Lucas Stach676ae062012-09-26 00:14:35 +0200981 reg = HC_VERSION(ehci_readl(&ehcic[index].hccr->cr_capbase));
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100982 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100983
Lucas Stach676ae062012-09-26 00:14:35 +0200984 ehcic[index].rootdev = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100985
Lucas Stach676ae062012-09-26 00:14:35 +0200986 *controller = &ehcic[index];
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100987 return 0;
988}
989
990int
991submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
992 int length)
993{
994
995 if (usb_pipetype(pipe) != PIPE_BULK) {
996 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
997 return -1;
998 }
999 return ehci_submit_async(dev, pipe, buffer, length, NULL);
1000}
1001
1002int
1003submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1004 int length, struct devrequest *setup)
1005{
Lucas Stach676ae062012-09-26 00:14:35 +02001006 struct ehci_ctrl *ctrl = dev->controller;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001007
1008 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1009 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
1010 return -1;
1011 }
1012
Lucas Stach676ae062012-09-26 00:14:35 +02001013 if (usb_pipedevice(pipe) == ctrl->rootdev) {
1014 if (!ctrl->rootdev)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001015 dev->speed = USB_SPEED_HIGH;
1016 return ehci_submit_root(dev, pipe, buffer, length, setup);
1017 }
1018 return ehci_submit_async(dev, pipe, buffer, length, setup);
1019}
1020
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001021struct int_queue {
1022 struct QH *first;
1023 struct QH *current;
1024 struct QH *last;
1025 struct qTD *tds;
1026};
1027
1028#define NEXT_QH(qh) (struct QH *)((qh)->qh_link & ~0x1f)
1029
1030static int
1031enable_periodic(struct ehci_ctrl *ctrl)
1032{
1033 uint32_t cmd;
1034 struct ehci_hcor *hcor = ctrl->hcor;
1035 int ret;
1036
1037 cmd = ehci_readl(&hcor->or_usbcmd);
1038 cmd |= CMD_PSE;
1039 ehci_writel(&hcor->or_usbcmd, cmd);
1040
1041 ret = handshake((uint32_t *)&hcor->or_usbsts,
1042 STS_PSS, STS_PSS, 100 * 1000);
1043 if (ret < 0) {
1044 printf("EHCI failed: timeout when enabling periodic list\n");
1045 return -ETIMEDOUT;
1046 }
1047 udelay(1000);
1048 return 0;
1049}
1050
1051static int
1052disable_periodic(struct ehci_ctrl *ctrl)
1053{
1054 uint32_t cmd;
1055 struct ehci_hcor *hcor = ctrl->hcor;
1056 int ret;
1057
1058 cmd = ehci_readl(&hcor->or_usbcmd);
1059 cmd &= ~CMD_PSE;
1060 ehci_writel(&hcor->or_usbcmd, cmd);
1061
1062 ret = handshake((uint32_t *)&hcor->or_usbsts,
1063 STS_PSS, 0, 100 * 1000);
1064 if (ret < 0) {
1065 printf("EHCI failed: timeout when disabling periodic list\n");
1066 return -ETIMEDOUT;
1067 }
1068 return 0;
1069}
1070
1071static int periodic_schedules;
1072
1073struct int_queue *
1074create_int_queue(struct usb_device *dev, unsigned long pipe, int queuesize,
1075 int elementsize, void *buffer)
1076{
1077 struct ehci_ctrl *ctrl = dev->controller;
1078 struct int_queue *result = NULL;
1079 int i;
1080
1081 debug("Enter create_int_queue\n");
1082 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1083 debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1084 return NULL;
1085 }
1086
1087 /* limit to 4 full pages worth of data -
1088 * we can safely fit them in a single TD,
1089 * no matter the alignment
1090 */
1091 if (elementsize >= 16384) {
1092 debug("too large elements for interrupt transfers\n");
1093 return NULL;
1094 }
1095
1096 result = malloc(sizeof(*result));
1097 if (!result) {
1098 debug("ehci intr queue: out of memory\n");
1099 goto fail1;
1100 }
1101 result->first = memalign(32, sizeof(struct QH) * queuesize);
1102 if (!result->first) {
1103 debug("ehci intr queue: out of memory\n");
1104 goto fail2;
1105 }
1106 result->current = result->first;
1107 result->last = result->first + queuesize - 1;
1108 result->tds = memalign(32, sizeof(struct qTD) * queuesize);
1109 if (!result->tds) {
1110 debug("ehci intr queue: out of memory\n");
1111 goto fail3;
1112 }
1113 memset(result->first, 0, sizeof(struct QH) * queuesize);
1114 memset(result->tds, 0, sizeof(struct qTD) * queuesize);
1115
1116 for (i = 0; i < queuesize; i++) {
1117 struct QH *qh = result->first + i;
1118 struct qTD *td = result->tds + i;
1119 void **buf = &qh->buffer;
1120
1121 qh->qh_link = (uint32_t)(qh+1) | QH_LINK_TYPE_QH;
1122 if (i == queuesize - 1)
1123 qh->qh_link = QH_LINK_TERMINATE;
1124
1125 qh->qh_overlay.qt_next = (uint32_t)td;
1126 qh->qh_endpt1 = (0 << 28) | /* No NAK reload (ehci 4.9) */
1127 (usb_maxpacket(dev, pipe) << 16) | /* MPS */
1128 (1 << 14) |
1129 QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) |
1130 (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */
1131 (usb_pipedevice(pipe) << 0);
1132 qh->qh_endpt2 = (1 << 30) | /* 1 Tx per mframe */
1133 (1 << 0); /* S-mask: microframe 0 */
1134 if (dev->speed == USB_SPEED_LOW ||
1135 dev->speed == USB_SPEED_FULL) {
1136 debug("TT: port: %d, hub address: %d\n",
1137 dev->portnr, dev->parent->devnum);
1138 qh->qh_endpt2 |= (dev->portnr << 23) |
1139 (dev->parent->devnum << 16) |
1140 (0x1c << 8); /* C-mask: microframes 2-4 */
1141 }
1142
1143 td->qt_next = QT_NEXT_TERMINATE;
1144 td->qt_altnext = QT_NEXT_TERMINATE;
1145 debug("communication direction is '%s'\n",
1146 usb_pipein(pipe) ? "in" : "out");
1147 td->qt_token = (elementsize << 16) |
1148 ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */
1149 0x80; /* active */
1150 td->qt_buffer[0] = (uint32_t)buffer + i * elementsize;
1151 td->qt_buffer[1] = (td->qt_buffer[0] + 0x1000) & ~0xfff;
1152 td->qt_buffer[2] = (td->qt_buffer[0] + 0x2000) & ~0xfff;
1153 td->qt_buffer[3] = (td->qt_buffer[0] + 0x3000) & ~0xfff;
1154 td->qt_buffer[4] = (td->qt_buffer[0] + 0x4000) & ~0xfff;
1155
1156 *buf = buffer + i * elementsize;
1157 }
1158
1159 if (disable_periodic(ctrl) < 0) {
1160 debug("FATAL: periodic should never fail, but did");
1161 goto fail3;
1162 }
1163
1164 /* hook up to periodic list */
1165 struct QH *list = &ctrl->periodic_queue;
1166 result->last->qh_link = list->qh_link;
1167 list->qh_link = (uint32_t)result->first | QH_LINK_TYPE_QH;
1168
1169 if (enable_periodic(ctrl) < 0) {
1170 debug("FATAL: periodic should never fail, but did");
1171 goto fail3;
1172 }
1173 periodic_schedules++;
1174
1175 debug("Exit create_int_queue\n");
1176 return result;
1177fail3:
1178 if (result->tds)
1179 free(result->tds);
1180fail2:
1181 if (result->first)
1182 free(result->first);
1183 if (result)
1184 free(result);
1185fail1:
1186 return NULL;
1187}
1188
1189void *poll_int_queue(struct usb_device *dev, struct int_queue *queue)
1190{
1191 struct QH *cur = queue->current;
1192
1193 /* depleted queue */
1194 if (cur == NULL) {
1195 debug("Exit poll_int_queue with completed queue\n");
1196 return NULL;
1197 }
1198 /* still active */
1199 if (cur->qh_overlay.qt_token & 0x80) {
1200 debug("Exit poll_int_queue with no completed intr transfer. "
1201 "token is %x\n", cur->qh_overlay.qt_token);
1202 return NULL;
1203 }
1204 if (!(cur->qh_link & QH_LINK_TERMINATE))
1205 queue->current++;
1206 else
1207 queue->current = NULL;
1208 debug("Exit poll_int_queue with completed intr transfer. "
1209 "token is %x at %p (first at %p)\n", cur->qh_overlay.qt_token,
1210 &cur->qh_overlay.qt_token, queue->first);
1211 return cur->buffer;
1212}
1213
1214/* Do not free buffers associated with QHs, they're owned by someone else */
1215int
1216destroy_int_queue(struct usb_device *dev, struct int_queue *queue)
1217{
1218 struct ehci_ctrl *ctrl = dev->controller;
1219 int result = -1;
1220 unsigned long timeout;
1221
1222 if (disable_periodic(ctrl) < 0) {
1223 debug("FATAL: periodic should never fail, but did");
1224 goto out;
1225 }
1226 periodic_schedules--;
1227
1228 struct QH *cur = &ctrl->periodic_queue;
1229 timeout = get_timer(0) + 500; /* abort after 500ms */
1230 while (!(cur->qh_link & QH_LINK_TERMINATE)) {
1231 debug("considering %p, with qh_link %x\n", cur, cur->qh_link);
1232 if (NEXT_QH(cur) == queue->first) {
1233 debug("found candidate. removing from chain\n");
1234 cur->qh_link = queue->last->qh_link;
1235 result = 0;
1236 break;
1237 }
1238 cur = NEXT_QH(cur);
1239 if (get_timer(0) > timeout) {
1240 printf("Timeout destroying interrupt endpoint queue\n");
1241 result = -1;
1242 goto out;
1243 }
1244 }
1245
1246 if (periodic_schedules > 0) {
1247 result = enable_periodic(ctrl);
1248 if (result < 0)
1249 debug("FATAL: periodic should never fail, but did");
1250 }
1251
1252out:
1253 free(queue->tds);
1254 free(queue->first);
1255 free(queue);
1256
1257 return result;
1258}
1259
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001260int
1261submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1262 int length, int interval)
1263{
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001264 void *backbuffer;
1265 struct int_queue *queue;
1266 unsigned long timeout;
1267 int result = 0, ret;
1268
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001269 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
1270 dev, pipe, buffer, length, interval);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001271
1272 /*
1273 * Interrupt transfers requiring several transactions are not supported
1274 * because bInterval is ignored.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +02001275 *
1276 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
Benoît Thébaudeaudb191342012-08-10 18:27:23 +02001277 * <= PKT_ALIGN if several qTDs are required, while the USB
1278 * specification does not constrain this for interrupt transfers. That
1279 * means that ehci_submit_async() would support interrupt transfers
1280 * requiring several transactions only as long as the transfer size does
1281 * not require more than a single qTD.
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001282 */
1283 if (length > usb_maxpacket(dev, pipe)) {
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001284 printf("%s: Interrupt transfers requiring several "
1285 "transactions are not supported.\n", __func__);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +02001286 return -1;
1287 }
Patrick Georgi8f62ca62013-03-06 14:08:31 +00001288
1289 queue = create_int_queue(dev, pipe, 1, length, buffer);
1290
1291 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1292 while ((backbuffer = poll_int_queue(dev, queue)) == NULL)
1293 if (get_timer(0) > timeout) {
1294 printf("Timeout poll on interrupt endpoint\n");
1295 result = -ETIMEDOUT;
1296 break;
1297 }
1298
1299 if (backbuffer != buffer) {
1300 debug("got wrong buffer back (%x instead of %x)\n",
1301 (uint32_t)backbuffer, (uint32_t)buffer);
1302 return -EINVAL;
1303 }
1304
1305 ret = destroy_int_queue(dev, queue);
1306 if (ret < 0)
1307 return ret;
1308
1309 /* everything worked out fine */
1310 return result;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +01001311}