blob: 392e2862245a322530424d923aff3812a45b5f6d [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>
michaeldb632992008-12-10 17:55:19 +010024#include <asm/byteorder.h>
Lucas Stach93ad9082012-09-06 08:00:13 +020025#include <asm/unaligned.h>
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010026#include <usb.h>
27#include <asm/io.h>
michaeldb632992008-12-10 17:55:19 +010028#include <malloc.h>
Stefan Roese67333f72010-11-26 15:43:28 +010029#include <watchdog.h>
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020030
31#include "ehci.h"
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010032
33int rootdev;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +010034struct ehci_hccr *hccr; /* R/O registers, not need for volatile */
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010035volatile struct ehci_hcor *hcor;
36
37static uint16_t portreset;
Tom Rini71c5de42012-07-15 22:14:24 +000038DEFINE_ALIGN_BUFFER(struct QH, qh_list, 1, USB_DMA_MINALIGN);
39
40#define ALIGN_END_ADDR(type, ptr, size) \
41 ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010042
michaeldb632992008-12-10 17:55:19 +010043static struct descriptor {
44 struct usb_hub_descriptor hub;
45 struct usb_device_descriptor device;
46 struct usb_linux_config_descriptor config;
47 struct usb_linux_interface_descriptor interface;
48 struct usb_endpoint_descriptor endpoint;
49} __attribute__ ((packed)) descriptor = {
50 {
51 0x8, /* bDescLength */
52 0x29, /* bDescriptorType: hub descriptor */
53 2, /* bNrPorts -- runtime modified */
54 0, /* wHubCharacteristics */
Vincent Palatin5f4b4f22011-12-05 14:52:22 -080055 10, /* bPwrOn2PwrGood */
michaeldb632992008-12-10 17:55:19 +010056 0, /* bHubCntrCurrent */
57 {}, /* Device removable */
58 {} /* at most 7 ports! XXX */
59 },
60 {
61 0x12, /* bLength */
62 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030063 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michaeldb632992008-12-10 17:55:19 +010064 9, /* bDeviceClass: UDCLASS_HUB */
65 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
66 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
67 64, /* bMaxPacketSize: 64 bytes */
68 0x0000, /* idVendor */
69 0x0000, /* idProduct */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030070 cpu_to_le16(0x0100), /* bcdDevice */
michaeldb632992008-12-10 17:55:19 +010071 1, /* iManufacturer */
72 2, /* iProduct */
73 0, /* iSerialNumber */
74 1 /* bNumConfigurations: 1 */
75 },
76 {
77 0x9,
78 2, /* bDescriptorType: UDESC_CONFIG */
79 cpu_to_le16(0x19),
80 1, /* bNumInterface */
81 1, /* bConfigurationValue */
82 0, /* iConfiguration */
83 0x40, /* bmAttributes: UC_SELF_POWER */
84 0 /* bMaxPower */
85 },
86 {
87 0x9, /* bLength */
88 4, /* bDescriptorType: UDESC_INTERFACE */
89 0, /* bInterfaceNumber */
90 0, /* bAlternateSetting */
91 1, /* bNumEndpoints */
92 9, /* bInterfaceClass: UICLASS_HUB */
93 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
94 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
95 0 /* iInterface */
96 },
97 {
98 0x7, /* bLength */
99 5, /* bDescriptorType: UDESC_ENDPOINT */
100 0x81, /* bEndpointAddress:
101 * UE_DIR_IN | EHCI_INTR_ENDPT
102 */
103 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix8f8bd562009-10-31 12:37:38 -0500104 8, /* wMaxPacketSize */
michaeldb632992008-12-10 17:55:19 +0100105 255 /* bInterval */
106 },
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100107};
108
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100109#if defined(CONFIG_EHCI_IS_TDI)
110#define ehci_is_TDI() (1)
111#else
112#define ehci_is_TDI() (0)
113#endif
114
Marek Vasut3874b6d2011-07-11 02:37:01 +0200115void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
116{
117 mdelay(50);
118}
119
120void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
121 __attribute__((weak, alias("__ehci_powerup_fixup")));
122
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100123static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michaeldb632992008-12-10 17:55:19 +0100124{
michael51ab1422008-12-11 13:43:55 +0100125 uint32_t result;
126 do {
127 result = ehci_readl(ptr);
Wolfgang Denk09c83a42010-10-22 14:23:00 +0200128 udelay(5);
michael51ab1422008-12-11 13:43:55 +0100129 if (result == ~(uint32_t)0)
130 return -1;
131 result &= mask;
132 if (result == done)
133 return 0;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100134 usec--;
135 } while (usec > 0);
michael51ab1422008-12-11 13:43:55 +0100136 return -1;
137}
138
michael51ab1422008-12-11 13:43:55 +0100139static int ehci_reset(void)
140{
141 uint32_t cmd;
142 uint32_t tmp;
143 uint32_t *reg_ptr;
144 int ret = 0;
145
146 cmd = ehci_readl(&hcor->or_usbcmd);
Stefan Roese273d7202010-11-26 15:44:00 +0100147 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
michael51ab1422008-12-11 13:43:55 +0100148 ehci_writel(&hcor->or_usbcmd, cmd);
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100149 ret = handshake((uint32_t *)&hcor->or_usbcmd, CMD_RESET, 0, 250 * 1000);
michael51ab1422008-12-11 13:43:55 +0100150 if (ret < 0) {
151 printf("EHCI fail to reset\n");
152 goto out;
153 }
154
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100155 if (ehci_is_TDI()) {
156 reg_ptr = (uint32_t *)((u8 *)hcor + USBMODE);
157 tmp = ehci_readl(reg_ptr);
158 tmp |= USBMODE_CM_HC;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100159#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100160 tmp |= USBMODE_BE;
michael51ab1422008-12-11 13:43:55 +0100161#endif
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100162 ehci_writel(reg_ptr, tmp);
163 }
Simon Glass9ab4ce22012-02-27 10:52:47 +0000164
165#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
166 cmd = ehci_readl(&hcor->or_txfilltuning);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200167 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass9ab4ce22012-02-27 10:52:47 +0000168 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
169 ehci_writel(&hcor->or_txfilltuning, cmd);
170#endif
michael51ab1422008-12-11 13:43:55 +0100171out:
172 return ret;
michaeldb632992008-12-10 17:55:19 +0100173}
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100174
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100175static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
176{
Marek Vasutb8adb122012-04-09 04:07:46 +0200177 uint32_t delta, next;
178 uint32_t addr = (uint32_t)buf;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100179 int idx;
180
Ilya Yanok189a6952012-07-15 04:43:49 +0000181 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutb8adb122012-04-09 04:07:46 +0200182 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
183
Ilya Yanok189a6952012-07-15 04:43:49 +0000184 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
185
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100186 idx = 0;
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200187 while (idx < QT_BUFFER_CNT) {
michaeldb632992008-12-10 17:55:19 +0100188 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denk3ed16072010-10-19 16:13:15 +0200189 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200190 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100191 delta = next - addr;
192 if (delta >= sz)
193 break;
194 sz -= delta;
195 addr = next;
196 idx++;
197 }
198
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200199 if (idx == QT_BUFFER_CNT) {
Ilya Yanok2af16f82012-07-15 04:43:52 +0000200 printf("out of buffer pointers (%u bytes left)\n", sz);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100201 return -1;
202 }
203
204 return 0;
205}
206
207static int
208ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
209 int length, struct devrequest *req)
210{
Tom Rini71c5de42012-07-15 22:14:24 +0000211 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200212 struct qTD *qtd;
213 int qtd_count = 0;
Marek Vasutde98e8b2012-04-08 23:32:05 +0200214 int qtd_counter = 0;
215
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100216 volatile struct qTD *vtd;
217 unsigned long ts;
218 uint32_t *tdp;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200219 uint32_t endpt, maxpacket, token, usbsts;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100220 uint32_t c, toggle;
michaeldb632992008-12-10 17:55:19 +0100221 uint32_t cmd;
Simon Glass96820a32011-02-07 14:42:16 -0800222 int timeout;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100223 int ret = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100224
michaeldb632992008-12-10 17:55:19 +0100225 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100226 buffer, length, req);
227 if (req != NULL)
michaeldb632992008-12-10 17:55:19 +0100228 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100229 req->request, req->request,
230 req->requesttype, req->requesttype,
231 le16_to_cpu(req->value), le16_to_cpu(req->value),
michaeldb632992008-12-10 17:55:19 +0100232 le16_to_cpu(req->index));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100233
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200234#define PKT_ALIGN 512
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200235 /*
236 * The USB transfer is split into qTD transfers. Eeach qTD transfer is
237 * described by a transfer descriptor (the qTD). The qTDs form a linked
238 * list with a queue head (QH).
239 *
240 * Each qTD transfer starts with a new USB packet, i.e. a packet cannot
241 * have its beginning in a qTD transfer and its end in the following
242 * one, so the qTD transfer lengths have to be chosen accordingly.
243 *
244 * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to
245 * single pages. The first data buffer can start at any offset within a
246 * page (not considering the cache-line alignment issues), while the
247 * following buffers must be page-aligned. There is no alignment
248 * constraint on the size of a qTD transfer.
249 */
250 if (req != NULL)
251 /* 1 qTD will be needed for SETUP, and 1 for ACK. */
252 qtd_count += 1 + 1;
253 if (length > 0 || req == NULL) {
254 /*
255 * Determine the qTD transfer size that will be used for the
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200256 * data payload (not considering the first qTD transfer, which
257 * may be longer or shorter, and the final one, which may be
258 * shorter).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200259 *
260 * In order to keep each packet within a qTD transfer, the qTD
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200261 * transfer size is aligned to PKT_ALIGN, which is a multiple of
262 * wMaxPacketSize (except in some cases for interrupt transfers,
263 * see comment in submit_int_msg()).
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200264 *
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200265 * By default, i.e. if the input buffer is aligned to PKT_ALIGN,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200266 * QT_BUFFER_CNT full pages will be used.
267 */
268 int xfr_sz = QT_BUFFER_CNT;
269 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200270 * However, if the input buffer is not aligned to PKT_ALIGN, the
271 * qTD transfer size will be one page shorter, and the first qTD
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200272 * data buffer of each transfer will be page-unaligned.
273 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200274 if ((uint32_t)buffer & (PKT_ALIGN - 1))
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200275 xfr_sz--;
276 /* Convert the qTD transfer size to bytes. */
277 xfr_sz *= EHCI_PAGE_SIZE;
278 /*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200279 * Approximate by excess the number of qTDs that will be
280 * required for the data payload. The exact formula is way more
281 * complicated and saves at most 2 qTDs, i.e. a total of 128
282 * bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200283 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200284 qtd_count += 2 + length / xfr_sz;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200285 }
286/*
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200287 * Threshold value based on the worst-case total size of the allocated qTDs for
288 * a mass-storage transfer of 65535 blocks of 512 bytes.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200289 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200290#if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200291#warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI
292#endif
293 qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD));
294 if (qtd == NULL) {
295 printf("unable to allocate TDs\n");
296 return -1;
297 }
298
Tom Rini71c5de42012-07-15 22:14:24 +0000299 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200300 memset(qtd, 0, qtd_count * sizeof(*qtd));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200301
Marek Vasutb8adb122012-04-09 04:07:46 +0200302 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
303
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200304 /*
305 * Setup QH (3.6 in ehci-r10.pdf)
306 *
307 * qh_link ................. 03-00 H
308 * qh_endpt1 ............... 07-04 H
309 * qh_endpt2 ............... 0B-08 H
310 * - qh_curtd
311 * qh_overlay.qt_next ...... 13-10 H
312 * - qh_overlay.qt_altnext
313 */
Tom Rini71c5de42012-07-15 22:14:24 +0000314 qh->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200315 c = usb_pipespeed(pipe) != USB_SPEED_HIGH && !usb_pipeendpoint(pipe);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200316 maxpacket = usb_maxpacket(dev, pipe);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200317 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200318 QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200319 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
320 QH_ENDPT1_EPS(usb_pipespeed(pipe)) |
321 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
322 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini71c5de42012-07-15 22:14:24 +0000323 qh->qh_endpt1 = cpu_to_hc32(endpt);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200324 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
325 QH_ENDPT2_HUBADDR(dev->parent->devnum) |
326 QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini71c5de42012-07-15 22:14:24 +0000327 qh->qh_endpt2 = cpu_to_hc32(endpt);
328 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100329
Tom Rini71c5de42012-07-15 22:14:24 +0000330 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100331
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100332 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200333 /*
334 * Setup request qTD (3.5 in ehci-r10.pdf)
335 *
336 * qt_next ................ 03-00 H
337 * qt_altnext ............. 07-04 H
338 * qt_token ............... 0B-08 H
339 *
340 * [ buffer, buffer_hi ] loaded with "req".
341 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200342 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
343 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200344 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
345 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
346 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
347 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200348 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200349 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
350 printf("unable to construct SETUP TD\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100351 goto fail;
352 }
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200353 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200354 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
355 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100356 toggle = 1;
357 }
358
359 if (length > 0 || req == NULL) {
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200360 uint8_t *buf_ptr = buffer;
361 int left_length = length;
362
363 do {
364 /*
365 * Determine the size of this qTD transfer. By default,
366 * QT_BUFFER_CNT full pages can be used.
367 */
368 int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE;
369 /*
370 * However, if the input buffer is not page-aligned, the
371 * portion of the first page before the buffer start
372 * offset within that page is unusable.
373 */
374 xfr_bytes -= (uint32_t)buf_ptr & (EHCI_PAGE_SIZE - 1);
375 /*
376 * In order to keep each packet within a qTD transfer,
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200377 * align the qTD transfer size to PKT_ALIGN.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200378 */
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200379 xfr_bytes &= ~(PKT_ALIGN - 1);
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200380 /*
381 * This transfer may be shorter than the available qTD
382 * transfer size that has just been computed.
383 */
384 xfr_bytes = min(xfr_bytes, left_length);
385
386 /*
387 * Setup request qTD (3.5 in ehci-r10.pdf)
388 *
389 * qt_next ................ 03-00 H
390 * qt_altnext ............. 07-04 H
391 * qt_token ............... 0B-08 H
392 *
393 * [ buffer, buffer_hi ] loaded with "buffer".
394 */
395 qtd[qtd_counter].qt_next =
396 cpu_to_hc32(QT_NEXT_TERMINATE);
397 qtd[qtd_counter].qt_altnext =
398 cpu_to_hc32(QT_NEXT_TERMINATE);
399 token = QT_TOKEN_DT(toggle) |
400 QT_TOKEN_TOTALBYTES(xfr_bytes) |
401 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
402 QT_TOKEN_CERR(3) |
403 QT_TOKEN_PID(usb_pipein(pipe) ?
404 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
405 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
406 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
407 if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr,
408 xfr_bytes)) {
409 printf("unable to construct DATA TD\n");
410 goto fail;
411 }
412 /* Update previous qTD! */
413 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
414 tdp = &qtd[qtd_counter++].qt_next;
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200415 /*
416 * Data toggle has to be adjusted since the qTD transfer
417 * size is not always an even multiple of
418 * wMaxPacketSize.
419 */
420 if ((xfr_bytes / maxpacket) & 1)
421 toggle ^= 1;
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200422 buf_ptr += xfr_bytes;
423 left_length -= xfr_bytes;
424 } while (left_length > 0);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100425 }
426
427 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200428 /*
429 * Setup request qTD (3.5 in ehci-r10.pdf)
430 *
431 * qt_next ................ 03-00 H
432 * qt_altnext ............. 07-04 H
433 * qt_token ............... 0B-08 H
434 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200435 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
436 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200437 token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) |
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200438 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
439 QT_TOKEN_PID(usb_pipein(pipe) ?
440 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
441 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200442 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200443 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200444 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
445 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100446 }
447
Tom Rini71c5de42012-07-15 22:14:24 +0000448 qh_list->qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100449
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100450 /* Flush dcache */
Tom Rini71c5de42012-07-15 22:14:24 +0000451 flush_dcache_range((uint32_t)qh_list,
452 ALIGN_END_ADDR(struct QH, qh_list, 1));
453 flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200454 flush_dcache_range((uint32_t)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200455 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100456
Ilya Yanokc7701af2012-07-15 22:12:08 +0000457 /* Set async. queue head pointer. */
Tom Rini71c5de42012-07-15 22:14:24 +0000458 ehci_writel(&hcor->or_asynclistaddr, (uint32_t)qh_list);
Ilya Yanokc7701af2012-07-15 22:12:08 +0000459
michael51ab1422008-12-11 13:43:55 +0100460 usbsts = ehci_readl(&hcor->or_usbsts);
461 ehci_writel(&hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100462
463 /* Enable async. schedule. */
michael51ab1422008-12-11 13:43:55 +0100464 cmd = ehci_readl(&hcor->or_usbcmd);
465 cmd |= CMD_ASE;
466 ehci_writel(&hcor->or_usbcmd, cmd);
michaeldb632992008-12-10 17:55:19 +0100467
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200468 ret = handshake((uint32_t *)&hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100469 100 * 1000);
470 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200471 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100472 goto fail;
michael51ab1422008-12-11 13:43:55 +0100473 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100474
475 /* Wait for TDs to be processed. */
476 ts = get_timer(0);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200477 vtd = &qtd[qtd_counter - 1];
Simon Glass96820a32011-02-07 14:42:16 -0800478 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100479 do {
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100480 /* Invalidate dcache */
Tom Rini71c5de42012-07-15 22:14:24 +0000481 invalidate_dcache_range((uint32_t)qh_list,
482 ALIGN_END_ADDR(struct QH, qh_list, 1));
483 invalidate_dcache_range((uint32_t)qh,
484 ALIGN_END_ADDR(struct QH, qh, 1));
Marek Vasutb8adb122012-04-09 04:07:46 +0200485 invalidate_dcache_range((uint32_t)qtd,
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200486 ALIGN_END_ADDR(struct qTD, qtd, qtd_count));
Marek Vasutb8adb122012-04-09 04:07:46 +0200487
michaeldb632992008-12-10 17:55:19 +0100488 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200489 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100490 break;
Stefan Roese67333f72010-11-26 15:43:28 +0100491 WATCHDOG_RESET();
Simon Glass96820a32011-02-07 14:42:16 -0800492 } while (get_timer(ts) < timeout);
493
Ilya Yanok189a6952012-07-15 04:43:49 +0000494 /*
495 * Invalidate the memory area occupied by buffer
496 * Don't try to fix the buffer alignment, if it isn't properly
497 * aligned it's upper layer's fault so let invalidate_dcache_range()
498 * vow about it. But we have to fix the length as it's actual
499 * transfer length and can be unaligned. This is potentially
500 * dangerous operation, it's responsibility of the calling
501 * code to make sure enough space is reserved.
502 */
503 invalidate_dcache_range((uint32_t)buffer,
504 ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutb8adb122012-04-09 04:07:46 +0200505
Simon Glass96820a32011-02-07 14:42:16 -0800506 /* Check that the TD processing happened */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200507 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glass96820a32011-02-07 14:42:16 -0800508 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100509
510 /* Disable async schedule. */
michael51ab1422008-12-11 13:43:55 +0100511 cmd = ehci_readl(&hcor->or_usbcmd);
michaeldb632992008-12-10 17:55:19 +0100512 cmd &= ~CMD_ASE;
michael51ab1422008-12-11 13:43:55 +0100513 ehci_writel(&hcor->or_usbcmd, cmd);
514
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200515 ret = handshake((uint32_t *)&hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100516 100 * 1000);
517 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200518 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100519 goto fail;
michael51ab1422008-12-11 13:43:55 +0100520 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100521
Tom Rini71c5de42012-07-15 22:14:24 +0000522 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200523 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michaeldb632992008-12-10 17:55:19 +0100524 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200525 switch (QT_TOKEN_GET_STATUS(token) &
526 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100527 case 0:
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200528 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100529 usb_settoggle(dev, usb_pipeendpoint(pipe),
530 usb_pipeout(pipe), toggle);
531 dev->status = 0;
532 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200533 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100534 dev->status = USB_ST_STALLED;
535 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200536 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
537 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100538 dev->status = USB_ST_BUF_ERR;
539 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200540 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
541 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100542 dev->status = USB_ST_BABBLE_DET;
543 break;
544 default:
545 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200546 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschin222d6df2010-11-02 11:47:29 +0100547 dev->status |= USB_ST_STALLED;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100548 break;
549 }
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200550 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100551 } else {
552 dev->act_len = 0;
michaeldb632992008-12-10 17:55:19 +0100553 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
michael51ab1422008-12-11 13:43:55 +0100554 dev->devnum, ehci_readl(&hcor->or_usbsts),
555 ehci_readl(&hcor->or_portsc[0]),
556 ehci_readl(&hcor->or_portsc[1]));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100557 }
558
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200559 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100560 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
561
562fail:
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200563 free(qtd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100564 return -1;
565}
566
567static inline int min3(int a, int b, int c)
568{
569
570 if (b < a)
571 a = b;
572 if (c < a)
573 a = c;
574 return a;
575}
576
michaeldb632992008-12-10 17:55:19 +0100577int
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100578ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
579 int length, struct devrequest *req)
580{
581 uint8_t tmpbuf[4];
582 u16 typeReq;
michaeldb632992008-12-10 17:55:19 +0100583 void *srcptr = NULL;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100584 int len, srclen;
585 uint32_t reg;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100586 uint32_t *status_reg;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100587
Sergei Shtylyove06a0552010-02-27 21:32:17 +0300588 if (le16_to_cpu(req->index) > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100589 printf("The request port(%d) is not configured\n",
590 le16_to_cpu(req->index) - 1);
591 return -1;
592 }
593 status_reg = (uint32_t *)&hcor->or_portsc[
594 le16_to_cpu(req->index) - 1];
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100595 srclen = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100596
michaeldb632992008-12-10 17:55:19 +0100597 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100598 req->request, req->request,
599 req->requesttype, req->requesttype,
600 le16_to_cpu(req->value), le16_to_cpu(req->index));
601
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530602 typeReq = req->request | req->requesttype << 8;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100603
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530604 switch (typeReq) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100605 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
606 switch (le16_to_cpu(req->value) >> 8) {
607 case USB_DT_DEVICE:
michaeldb632992008-12-10 17:55:19 +0100608 debug("USB_DT_DEVICE request\n");
609 srcptr = &descriptor.device;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200610 srclen = descriptor.device.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100611 break;
612 case USB_DT_CONFIG:
michaeldb632992008-12-10 17:55:19 +0100613 debug("USB_DT_CONFIG config\n");
614 srcptr = &descriptor.config;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200615 srclen = descriptor.config.bLength +
616 descriptor.interface.bLength +
617 descriptor.endpoint.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100618 break;
619 case USB_DT_STRING:
michaeldb632992008-12-10 17:55:19 +0100620 debug("USB_DT_STRING config\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100621 switch (le16_to_cpu(req->value) & 0xff) {
622 case 0: /* Language */
623 srcptr = "\4\3\1\0";
624 srclen = 4;
625 break;
626 case 1: /* Vendor */
627 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
628 srclen = 14;
629 break;
630 case 2: /* Product */
631 srcptr = "\52\3E\0H\0C\0I\0 "
632 "\0H\0o\0s\0t\0 "
633 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
634 srclen = 42;
635 break;
636 default:
michaeldb632992008-12-10 17:55:19 +0100637 debug("unknown value DT_STRING %x\n",
638 le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100639 goto unknown;
640 }
641 break;
642 default:
michaeldb632992008-12-10 17:55:19 +0100643 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100644 goto unknown;
645 }
646 break;
647 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
648 switch (le16_to_cpu(req->value) >> 8) {
649 case USB_DT_HUB:
michaeldb632992008-12-10 17:55:19 +0100650 debug("USB_DT_HUB config\n");
651 srcptr = &descriptor.hub;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200652 srclen = descriptor.hub.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100653 break;
654 default:
michaeldb632992008-12-10 17:55:19 +0100655 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100656 goto unknown;
657 }
658 break;
659 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michaeldb632992008-12-10 17:55:19 +0100660 debug("USB_REQ_SET_ADDRESS\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100661 rootdev = le16_to_cpu(req->value);
662 break;
663 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michaeldb632992008-12-10 17:55:19 +0100664 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100665 /* Nothing to do */
666 break;
667 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
668 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
669 tmpbuf[1] = 0;
670 srcptr = tmpbuf;
671 srclen = 2;
672 break;
michaeldb632992008-12-10 17:55:19 +0100673 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100674 memset(tmpbuf, 0, 4);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100675 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100676 if (reg & EHCI_PS_CS)
677 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
678 if (reg & EHCI_PS_PE)
679 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
680 if (reg & EHCI_PS_SUSP)
681 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
682 if (reg & EHCI_PS_OCA)
683 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300684 if (reg & EHCI_PS_PR)
685 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100686 if (reg & EHCI_PS_PP)
687 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese597eb282009-01-21 17:12:01 +0100688
689 if (ehci_is_TDI()) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200690 switch (PORTSC_PSPD(reg)) {
691 case PORTSC_PSPD_FS:
Stefan Roese597eb282009-01-21 17:12:01 +0100692 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200693 case PORTSC_PSPD_LS:
Stefan Roese597eb282009-01-21 17:12:01 +0100694 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
695 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200696 case PORTSC_PSPD_HS:
Stefan Roese597eb282009-01-21 17:12:01 +0100697 default:
698 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
699 break;
700 }
701 } else {
702 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
703 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100704
705 if (reg & EHCI_PS_CSC)
706 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
707 if (reg & EHCI_PS_PEC)
708 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
709 if (reg & EHCI_PS_OCC)
710 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
711 if (portreset & (1 << le16_to_cpu(req->index)))
712 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100713
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100714 srcptr = tmpbuf;
715 srclen = 4;
716 break;
michaeldb632992008-12-10 17:55:19 +0100717 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100718 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100719 reg &= ~EHCI_PS_CLEAR;
720 switch (le16_to_cpu(req->value)) {
michael51ab1422008-12-11 13:43:55 +0100721 case USB_PORT_FEAT_ENABLE:
722 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100723 ehci_writel(status_reg, reg);
michael51ab1422008-12-11 13:43:55 +0100724 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100725 case USB_PORT_FEAT_POWER:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100726 if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams))) {
727 reg |= EHCI_PS_PP;
728 ehci_writel(status_reg, reg);
729 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100730 break;
731 case USB_PORT_FEAT_RESET:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100732 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
733 !ehci_is_TDI() &&
734 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100735 /* Low speed device, give up ownership. */
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100736 debug("port %d low speed --> companion\n",
737 req->index - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100738 reg |= EHCI_PS_PO;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100739 ehci_writel(status_reg, reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100740 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100741 } else {
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300742 int ret;
743
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100744 reg |= EHCI_PS_PR;
745 reg &= ~EHCI_PS_PE;
746 ehci_writel(status_reg, reg);
747 /*
748 * caller must wait, then call GetPortStatus
749 * usb 2.0 specification say 50 ms resets on
750 * root
751 */
Marek Vasut3874b6d2011-07-11 02:37:01 +0200752 ehci_powerup_fixup(status_reg, &reg);
753
Chris Zhangb4161912010-01-06 13:34:04 -0800754 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300755 /*
756 * A host controller must terminate the reset
757 * and stabilize the state of the port within
758 * 2 milliseconds
759 */
760 ret = handshake(status_reg, EHCI_PS_PR, 0,
761 2 * 1000);
762 if (!ret)
763 portreset |=
764 1 << le16_to_cpu(req->index);
765 else
766 printf("port(%d) reset error\n",
767 le16_to_cpu(req->index) - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100768 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100769 break;
770 default:
michaeldb632992008-12-10 17:55:19 +0100771 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100772 goto unknown;
773 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100774 /* unblock posted writes */
Kumar Gala50243e32009-07-07 15:48:58 -0500775 (void) ehci_readl(&hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100776 break;
michaeldb632992008-12-10 17:55:19 +0100777 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100778 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100779 switch (le16_to_cpu(req->value)) {
780 case USB_PORT_FEAT_ENABLE:
781 reg &= ~EHCI_PS_PE;
782 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100783 case USB_PORT_FEAT_C_ENABLE:
784 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_PE;
785 break;
786 case USB_PORT_FEAT_POWER:
787 if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams)))
788 reg = reg & ~(EHCI_PS_CLEAR | EHCI_PS_PP);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100789 case USB_PORT_FEAT_C_CONNECTION:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100790 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_CSC;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100791 break;
michael51ab1422008-12-11 13:43:55 +0100792 case USB_PORT_FEAT_OVER_CURRENT:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100793 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_OCC;
michael51ab1422008-12-11 13:43:55 +0100794 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100795 case USB_PORT_FEAT_C_RESET:
796 portreset &= ~(1 << le16_to_cpu(req->index));
797 break;
798 default:
michaeldb632992008-12-10 17:55:19 +0100799 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100800 goto unknown;
801 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100802 ehci_writel(status_reg, reg);
803 /* unblock posted write */
Kumar Gala50243e32009-07-07 15:48:58 -0500804 (void) ehci_readl(&hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100805 break;
806 default:
michaeldb632992008-12-10 17:55:19 +0100807 debug("Unknown request\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100808 goto unknown;
809 }
810
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000811 mdelay(1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100812 len = min3(srclen, le16_to_cpu(req->length), length);
813 if (srcptr != NULL && len > 0)
814 memcpy(buffer, srcptr, len);
michaeldb632992008-12-10 17:55:19 +0100815 else
816 debug("Len is 0\n");
817
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100818 dev->act_len = len;
819 dev->status = 0;
820 return 0;
821
822unknown:
michaeldb632992008-12-10 17:55:19 +0100823 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100824 req->requesttype, req->request, le16_to_cpu(req->value),
825 le16_to_cpu(req->index), le16_to_cpu(req->length));
826
827 dev->act_len = 0;
828 dev->status = USB_ST_STALLED;
829 return -1;
830}
831
832int usb_lowlevel_stop(void)
833{
834 return ehci_hcd_stop();
835}
836
837int usb_lowlevel_init(void)
838{
839 uint32_t reg;
michaeldb632992008-12-10 17:55:19 +0100840 uint32_t cmd;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100841
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200842 if (ehci_hcd_init())
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100843 return -1;
844
michael51ab1422008-12-11 13:43:55 +0100845 /* EHCI spec section 4.1 */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200846 if (ehci_reset())
michael51ab1422008-12-11 13:43:55 +0100847 return -1;
848
Stefan Roese832e6142009-01-21 17:12:10 +0100849#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200850 if (ehci_hcd_init())
Stefan Roese832e6142009-01-21 17:12:10 +0100851 return -1;
852#endif
853
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100854 /* Set head of reclaim list */
Tom Rini71c5de42012-07-15 22:14:24 +0000855 memset(qh_list, 0, sizeof(*qh_list));
856 qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200857 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
858 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini71c5de42012-07-15 22:14:24 +0000859 qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
860 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
861 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200862 qh_list->qh_overlay.qt_token =
863 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100864
michael51ab1422008-12-11 13:43:55 +0100865 reg = ehci_readl(&hccr->cr_hcsparams);
866 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
867 printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100868 /* Port Indicators */
869 if (HCS_INDICATOR(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +0200870 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
871 | 0x80, &descriptor.hub.wHubCharacteristics);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100872 /* Port Power Control */
873 if (HCS_PPC(reg))
Lucas Stach93ad9082012-09-06 08:00:13 +0200874 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
875 | 0x01, &descriptor.hub.wHubCharacteristics);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100876
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100877 /* Start the host controller. */
michael51ab1422008-12-11 13:43:55 +0100878 cmd = ehci_readl(&hcor->or_usbcmd);
Wolfgang Denkf15c6512009-02-12 00:08:39 +0100879 /*
880 * Philips, Intel, and maybe others need CMD_RUN before the
881 * root hub will detect new devices (why?); NEC doesn't
882 */
michael51ab1422008-12-11 13:43:55 +0100883 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
884 cmd |= CMD_RUN;
885 ehci_writel(&hcor->or_usbcmd, cmd);
886
887 /* take control over the ports */
888 cmd = ehci_readl(&hcor->or_configflag);
889 cmd |= FLAG_CF;
890 ehci_writel(&hcor->or_configflag, cmd);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100891 /* unblock posted write */
michael51ab1422008-12-11 13:43:55 +0100892 cmd = ehci_readl(&hcor->or_usbcmd);
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000893 mdelay(5);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100894 reg = HC_VERSION(ehci_readl(&hccr->cr_capbase));
895 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100896
897 rootdev = 0;
898
899 return 0;
900}
901
902int
903submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
904 int length)
905{
906
907 if (usb_pipetype(pipe) != PIPE_BULK) {
908 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
909 return -1;
910 }
911 return ehci_submit_async(dev, pipe, buffer, length, NULL);
912}
913
914int
915submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
916 int length, struct devrequest *setup)
917{
918
919 if (usb_pipetype(pipe) != PIPE_CONTROL) {
920 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
921 return -1;
922 }
923
924 if (usb_pipedevice(pipe) == rootdev) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200925 if (!rootdev)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100926 dev->speed = USB_SPEED_HIGH;
927 return ehci_submit_root(dev, pipe, buffer, length, setup);
928 }
929 return ehci_submit_async(dev, pipe, buffer, length, setup);
930}
931
932int
933submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
934 int length, int interval)
935{
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100936 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
937 dev, pipe, buffer, length, interval);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +0200938
939 /*
940 * Interrupt transfers requiring several transactions are not supported
941 * because bInterval is ignored.
Benoît Thébaudeau5cec2142012-08-10 18:22:32 +0200942 *
943 * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2
Benoît Thébaudeaudb191342012-08-10 18:27:23 +0200944 * <= PKT_ALIGN if several qTDs are required, while the USB
945 * specification does not constrain this for interrupt transfers. That
946 * means that ehci_submit_async() would support interrupt transfers
947 * requiring several transactions only as long as the transfer size does
948 * not require more than a single qTD.
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +0200949 */
950 if (length > usb_maxpacket(dev, pipe)) {
951 printf("%s: Interrupt transfers requiring several transactions "
952 "are not supported.\n", __func__);
953 return -1;
954 }
Marek Vasut7555d5e2011-09-25 21:07:56 +0200955 return ehci_submit_async(dev, pipe, buffer, length, NULL);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100956}