blob: 00a155b64dca57f0c899e78c14bc802298b9c59e [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>
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010025#include <usb.h>
26#include <asm/io.h>
michaeldb632992008-12-10 17:55:19 +010027#include <malloc.h>
Stefan Roese67333f72010-11-26 15:43:28 +010028#include <watchdog.h>
Jean-Christophe PLAGNIOL-VILLARD2731b9a2009-04-03 12:46:58 +020029
30#include "ehci.h"
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010031
32int rootdev;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +010033struct ehci_hccr *hccr; /* R/O registers, not need for volatile */
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010034volatile struct ehci_hcor *hcor;
35
36static uint16_t portreset;
Tom Rini71c5de42012-07-15 22:14:24 +000037DEFINE_ALIGN_BUFFER(struct QH, qh_list, 1, USB_DMA_MINALIGN);
38
39#define ALIGN_END_ADDR(type, ptr, size) \
40 ((uint32_t)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +010041
michaeldb632992008-12-10 17:55:19 +010042static struct descriptor {
43 struct usb_hub_descriptor hub;
44 struct usb_device_descriptor device;
45 struct usb_linux_config_descriptor config;
46 struct usb_linux_interface_descriptor interface;
47 struct usb_endpoint_descriptor endpoint;
48} __attribute__ ((packed)) descriptor = {
49 {
50 0x8, /* bDescLength */
51 0x29, /* bDescriptorType: hub descriptor */
52 2, /* bNrPorts -- runtime modified */
53 0, /* wHubCharacteristics */
Vincent Palatin5f4b4f22011-12-05 14:52:22 -080054 10, /* bPwrOn2PwrGood */
michaeldb632992008-12-10 17:55:19 +010055 0, /* bHubCntrCurrent */
56 {}, /* Device removable */
57 {} /* at most 7 ports! XXX */
58 },
59 {
60 0x12, /* bLength */
61 1, /* bDescriptorType: UDESC_DEVICE */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030062 cpu_to_le16(0x0200), /* bcdUSB: v2.0 */
michaeldb632992008-12-10 17:55:19 +010063 9, /* bDeviceClass: UDCLASS_HUB */
64 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
65 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */
66 64, /* bMaxPacketSize: 64 bytes */
67 0x0000, /* idVendor */
68 0x0000, /* idProduct */
Sergei Shtylyov6d313c82010-02-27 21:29:42 +030069 cpu_to_le16(0x0100), /* bcdDevice */
michaeldb632992008-12-10 17:55:19 +010070 1, /* iManufacturer */
71 2, /* iProduct */
72 0, /* iSerialNumber */
73 1 /* bNumConfigurations: 1 */
74 },
75 {
76 0x9,
77 2, /* bDescriptorType: UDESC_CONFIG */
78 cpu_to_le16(0x19),
79 1, /* bNumInterface */
80 1, /* bConfigurationValue */
81 0, /* iConfiguration */
82 0x40, /* bmAttributes: UC_SELF_POWER */
83 0 /* bMaxPower */
84 },
85 {
86 0x9, /* bLength */
87 4, /* bDescriptorType: UDESC_INTERFACE */
88 0, /* bInterfaceNumber */
89 0, /* bAlternateSetting */
90 1, /* bNumEndpoints */
91 9, /* bInterfaceClass: UICLASS_HUB */
92 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
93 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
94 0 /* iInterface */
95 },
96 {
97 0x7, /* bLength */
98 5, /* bDescriptorType: UDESC_ENDPOINT */
99 0x81, /* bEndpointAddress:
100 * UE_DIR_IN | EHCI_INTR_ENDPT
101 */
102 3, /* bmAttributes: UE_INTERRUPT */
Tom Rix8f8bd562009-10-31 12:37:38 -0500103 8, /* wMaxPacketSize */
michaeldb632992008-12-10 17:55:19 +0100104 255 /* bInterval */
105 },
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100106};
107
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100108#if defined(CONFIG_EHCI_IS_TDI)
109#define ehci_is_TDI() (1)
110#else
111#define ehci_is_TDI() (0)
112#endif
113
Marek Vasut3874b6d2011-07-11 02:37:01 +0200114void __ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
115{
116 mdelay(50);
117}
118
119void ehci_powerup_fixup(uint32_t *status_reg, uint32_t *reg)
120 __attribute__((weak, alias("__ehci_powerup_fixup")));
121
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100122static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec)
michaeldb632992008-12-10 17:55:19 +0100123{
michael51ab1422008-12-11 13:43:55 +0100124 uint32_t result;
125 do {
126 result = ehci_readl(ptr);
Wolfgang Denk09c83a42010-10-22 14:23:00 +0200127 udelay(5);
michael51ab1422008-12-11 13:43:55 +0100128 if (result == ~(uint32_t)0)
129 return -1;
130 result &= mask;
131 if (result == done)
132 return 0;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100133 usec--;
134 } while (usec > 0);
michael51ab1422008-12-11 13:43:55 +0100135 return -1;
136}
137
michael51ab1422008-12-11 13:43:55 +0100138static int ehci_reset(void)
139{
140 uint32_t cmd;
141 uint32_t tmp;
142 uint32_t *reg_ptr;
143 int ret = 0;
144
145 cmd = ehci_readl(&hcor->or_usbcmd);
Stefan Roese273d7202010-11-26 15:44:00 +0100146 cmd = (cmd & ~CMD_RUN) | CMD_RESET;
michael51ab1422008-12-11 13:43:55 +0100147 ehci_writel(&hcor->or_usbcmd, cmd);
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100148 ret = handshake((uint32_t *)&hcor->or_usbcmd, CMD_RESET, 0, 250 * 1000);
michael51ab1422008-12-11 13:43:55 +0100149 if (ret < 0) {
150 printf("EHCI fail to reset\n");
151 goto out;
152 }
153
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100154 if (ehci_is_TDI()) {
155 reg_ptr = (uint32_t *)((u8 *)hcor + USBMODE);
156 tmp = ehci_readl(reg_ptr);
157 tmp |= USBMODE_CM_HC;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100158#if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN)
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100159 tmp |= USBMODE_BE;
michael51ab1422008-12-11 13:43:55 +0100160#endif
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100161 ehci_writel(reg_ptr, tmp);
162 }
Simon Glass9ab4ce22012-02-27 10:52:47 +0000163
164#ifdef CONFIG_USB_EHCI_TXFIFO_THRESH
165 cmd = ehci_readl(&hcor->or_txfilltuning);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200166 cmd &= ~TXFIFO_THRESH_MASK;
Simon Glass9ab4ce22012-02-27 10:52:47 +0000167 cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH);
168 ehci_writel(&hcor->or_txfilltuning, cmd);
169#endif
michael51ab1422008-12-11 13:43:55 +0100170out:
171 return ret;
michaeldb632992008-12-10 17:55:19 +0100172}
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100173
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100174static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
175{
Marek Vasutb8adb122012-04-09 04:07:46 +0200176 uint32_t delta, next;
177 uint32_t addr = (uint32_t)buf;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100178 int idx;
179
Ilya Yanok189a6952012-07-15 04:43:49 +0000180 if (addr != ALIGN(addr, ARCH_DMA_MINALIGN))
Marek Vasutb8adb122012-04-09 04:07:46 +0200181 debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf);
182
Ilya Yanok189a6952012-07-15 04:43:49 +0000183 flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN));
184
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100185 idx = 0;
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200186 while (idx < QT_BUFFER_CNT) {
michaeldb632992008-12-10 17:55:19 +0100187 td->qt_buffer[idx] = cpu_to_hc32(addr);
Wolfgang Denk3ed16072010-10-19 16:13:15 +0200188 td->qt_buffer_hi[idx] = 0;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200189 next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100190 delta = next - addr;
191 if (delta >= sz)
192 break;
193 sz -= delta;
194 addr = next;
195 idx++;
196 }
197
Benoît Thébaudeaucdeb9162012-07-19 22:16:38 +0200198 if (idx == QT_BUFFER_CNT) {
Ilya Yanok2af16f82012-07-15 04:43:52 +0000199 printf("out of buffer pointers (%u bytes left)\n", sz);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100200 return -1;
201 }
202
203 return 0;
204}
205
206static int
207ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
208 int length, struct devrequest *req)
209{
Tom Rini71c5de42012-07-15 22:14:24 +0000210 ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200211#define QTD_COUNT 3
212 ALLOC_ALIGN_BUFFER(struct qTD, qtd, QTD_COUNT, USB_DMA_MINALIGN);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200213 int qtd_counter = 0;
214
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100215 volatile struct qTD *vtd;
216 unsigned long ts;
217 uint32_t *tdp;
218 uint32_t endpt, token, usbsts;
219 uint32_t c, toggle;
michaeldb632992008-12-10 17:55:19 +0100220 uint32_t cmd;
Simon Glass96820a32011-02-07 14:42:16 -0800221 int timeout;
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100222 int ret = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100223
michaeldb632992008-12-10 17:55:19 +0100224 debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100225 buffer, length, req);
226 if (req != NULL)
michaeldb632992008-12-10 17:55:19 +0100227 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100228 req->request, req->request,
229 req->requesttype, req->requesttype,
230 le16_to_cpu(req->value), le16_to_cpu(req->value),
michaeldb632992008-12-10 17:55:19 +0100231 le16_to_cpu(req->index));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100232
Tom Rini71c5de42012-07-15 22:14:24 +0000233 memset(qh, 0, sizeof(struct QH));
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200234 memset(qtd, 0, QTD_COUNT * sizeof(*qtd));
Marek Vasutde98e8b2012-04-08 23:32:05 +0200235
Marek Vasutb8adb122012-04-09 04:07:46 +0200236 toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
237
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200238 /*
239 * Setup QH (3.6 in ehci-r10.pdf)
240 *
241 * qh_link ................. 03-00 H
242 * qh_endpt1 ............... 07-04 H
243 * qh_endpt2 ............... 0B-08 H
244 * - qh_curtd
245 * qh_overlay.qt_next ...... 13-10 H
246 * - qh_overlay.qt_altnext
247 */
Tom Rini71c5de42012-07-15 22:14:24 +0000248 qh->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200249 c = usb_pipespeed(pipe) != USB_SPEED_HIGH && !usb_pipeendpoint(pipe);
250 endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) |
251 QH_ENDPT1_MAXPKTLEN(usb_maxpacket(dev, pipe)) | QH_ENDPT1_H(0) |
252 QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) |
253 QH_ENDPT1_EPS(usb_pipespeed(pipe)) |
254 QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) |
255 QH_ENDPT1_DEVADDR(usb_pipedevice(pipe));
Tom Rini71c5de42012-07-15 22:14:24 +0000256 qh->qh_endpt1 = cpu_to_hc32(endpt);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200257 endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_PORTNUM(dev->portnr) |
258 QH_ENDPT2_HUBADDR(dev->parent->devnum) |
259 QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0);
Tom Rini71c5de42012-07-15 22:14:24 +0000260 qh->qh_endpt2 = cpu_to_hc32(endpt);
261 qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100262
Tom Rini71c5de42012-07-15 22:14:24 +0000263 tdp = &qh->qh_overlay.qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100264
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100265 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200266 /*
267 * Setup request qTD (3.5 in ehci-r10.pdf)
268 *
269 * qt_next ................ 03-00 H
270 * qt_altnext ............. 07-04 H
271 * qt_token ............... 0B-08 H
272 *
273 * [ buffer, buffer_hi ] loaded with "req".
274 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200275 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
276 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200277 token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) |
278 QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
279 QT_TOKEN_PID(QT_TOKEN_PID_SETUP) |
280 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200281 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200282 if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) {
283 printf("unable to construct SETUP TD\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100284 goto fail;
285 }
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200286 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200287 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
288 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100289 toggle = 1;
290 }
291
292 if (length > 0 || req == NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200293 /*
294 * Setup request qTD (3.5 in ehci-r10.pdf)
295 *
296 * qt_next ................ 03-00 H
297 * qt_altnext ............. 07-04 H
298 * qt_token ............... 0B-08 H
299 *
300 * [ buffer, buffer_hi ] loaded with "buffer".
301 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200302 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
303 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200304 token = QT_TOKEN_DT(toggle) | QT_TOKEN_TOTALBYTES(length) |
305 QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) |
306 QT_TOKEN_CERR(3) | QT_TOKEN_PID(usb_pipein(pipe) ?
307 QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) |
308 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200309 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200310 if (ehci_td_buffer(&qtd[qtd_counter], buffer, length)) {
311 printf("unable to construct DATA TD\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100312 goto fail;
313 }
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200314 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200315 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
316 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100317 }
318
319 if (req != NULL) {
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200320 /*
321 * Setup request qTD (3.5 in ehci-r10.pdf)
322 *
323 * qt_next ................ 03-00 H
324 * qt_altnext ............. 07-04 H
325 * qt_token ............... 0B-08 H
326 */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200327 qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
328 qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200329 token = QT_TOKEN_DT(toggle) | QT_TOKEN_TOTALBYTES(0) |
330 QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) |
331 QT_TOKEN_PID(usb_pipein(pipe) ?
332 QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) |
333 QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200334 qtd[qtd_counter].qt_token = cpu_to_hc32(token);
Marek Vasut41b1f0a2012-04-09 04:13:00 +0200335 /* Update previous qTD! */
Marek Vasutde98e8b2012-04-08 23:32:05 +0200336 *tdp = cpu_to_hc32((uint32_t)&qtd[qtd_counter]);
337 tdp = &qtd[qtd_counter++].qt_next;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100338 }
339
Tom Rini71c5de42012-07-15 22:14:24 +0000340 qh_list->qh_link = cpu_to_hc32((uint32_t)qh | QH_LINK_TYPE_QH);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100341
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100342 /* Flush dcache */
Tom Rini71c5de42012-07-15 22:14:24 +0000343 flush_dcache_range((uint32_t)qh_list,
344 ALIGN_END_ADDR(struct QH, qh_list, 1));
345 flush_dcache_range((uint32_t)qh, ALIGN_END_ADDR(struct QH, qh, 1));
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200346 flush_dcache_range((uint32_t)qtd,
347 ALIGN_END_ADDR(struct qTD, qtd, QTD_COUNT));
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100348
Ilya Yanokc7701af2012-07-15 22:12:08 +0000349 /* Set async. queue head pointer. */
Tom Rini71c5de42012-07-15 22:14:24 +0000350 ehci_writel(&hcor->or_asynclistaddr, (uint32_t)qh_list);
Ilya Yanokc7701af2012-07-15 22:12:08 +0000351
michael51ab1422008-12-11 13:43:55 +0100352 usbsts = ehci_readl(&hcor->or_usbsts);
353 ehci_writel(&hcor->or_usbsts, (usbsts & 0x3f));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100354
355 /* Enable async. schedule. */
michael51ab1422008-12-11 13:43:55 +0100356 cmd = ehci_readl(&hcor->or_usbcmd);
357 cmd |= CMD_ASE;
358 ehci_writel(&hcor->or_usbcmd, cmd);
michaeldb632992008-12-10 17:55:19 +0100359
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200360 ret = handshake((uint32_t *)&hcor->or_usbsts, STS_ASS, STS_ASS,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100361 100 * 1000);
362 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200363 printf("EHCI fail timeout STS_ASS set\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100364 goto fail;
michael51ab1422008-12-11 13:43:55 +0100365 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100366
367 /* Wait for TDs to be processed. */
368 ts = get_timer(0);
Marek Vasutde98e8b2012-04-08 23:32:05 +0200369 vtd = &qtd[qtd_counter - 1];
Simon Glass96820a32011-02-07 14:42:16 -0800370 timeout = USB_TIMEOUT_MS(pipe);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100371 do {
Stefan Roesedaa2daf2009-01-21 17:12:19 +0100372 /* Invalidate dcache */
Tom Rini71c5de42012-07-15 22:14:24 +0000373 invalidate_dcache_range((uint32_t)qh_list,
374 ALIGN_END_ADDR(struct QH, qh_list, 1));
375 invalidate_dcache_range((uint32_t)qh,
376 ALIGN_END_ADDR(struct QH, qh, 1));
Marek Vasutb8adb122012-04-09 04:07:46 +0200377 invalidate_dcache_range((uint32_t)qtd,
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200378 ALIGN_END_ADDR(struct qTD, qtd, QTD_COUNT));
Marek Vasutb8adb122012-04-09 04:07:46 +0200379
michaeldb632992008-12-10 17:55:19 +0100380 token = hc32_to_cpu(vtd->qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200381 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE))
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100382 break;
Stefan Roese67333f72010-11-26 15:43:28 +0100383 WATCHDOG_RESET();
Simon Glass96820a32011-02-07 14:42:16 -0800384 } while (get_timer(ts) < timeout);
385
Ilya Yanok189a6952012-07-15 04:43:49 +0000386 /*
387 * Invalidate the memory area occupied by buffer
388 * Don't try to fix the buffer alignment, if it isn't properly
389 * aligned it's upper layer's fault so let invalidate_dcache_range()
390 * vow about it. But we have to fix the length as it's actual
391 * transfer length and can be unaligned. This is potentially
392 * dangerous operation, it's responsibility of the calling
393 * code to make sure enough space is reserved.
394 */
395 invalidate_dcache_range((uint32_t)buffer,
396 ALIGN((uint32_t)buffer + length, ARCH_DMA_MINALIGN));
Marek Vasutb8adb122012-04-09 04:07:46 +0200397
Simon Glass96820a32011-02-07 14:42:16 -0800398 /* Check that the TD processing happened */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200399 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)
Simon Glass96820a32011-02-07 14:42:16 -0800400 printf("EHCI timed out on TD - token=%#x\n", token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100401
402 /* Disable async schedule. */
michael51ab1422008-12-11 13:43:55 +0100403 cmd = ehci_readl(&hcor->or_usbcmd);
michaeldb632992008-12-10 17:55:19 +0100404 cmd &= ~CMD_ASE;
michael51ab1422008-12-11 13:43:55 +0100405 ehci_writel(&hcor->or_usbcmd, cmd);
406
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200407 ret = handshake((uint32_t *)&hcor->or_usbsts, STS_ASS, 0,
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100408 100 * 1000);
409 if (ret < 0) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200410 printf("EHCI fail timeout STS_ASS reset\n");
Michael Trimarchi1ed9f9a2008-12-31 10:33:22 +0100411 goto fail;
michael51ab1422008-12-11 13:43:55 +0100412 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100413
Tom Rini71c5de42012-07-15 22:14:24 +0000414 token = hc32_to_cpu(qh->qh_overlay.qt_token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200415 if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) {
michaeldb632992008-12-10 17:55:19 +0100416 debug("TOKEN=%#x\n", token);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200417 switch (QT_TOKEN_GET_STATUS(token) &
418 ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100419 case 0:
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200420 toggle = QT_TOKEN_GET_DT(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100421 usb_settoggle(dev, usb_pipeendpoint(pipe),
422 usb_pipeout(pipe), toggle);
423 dev->status = 0;
424 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200425 case QT_TOKEN_STATUS_HALTED:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100426 dev->status = USB_ST_STALLED;
427 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200428 case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR:
429 case QT_TOKEN_STATUS_DATBUFERR:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100430 dev->status = USB_ST_BUF_ERR;
431 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200432 case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET:
433 case QT_TOKEN_STATUS_BABBLEDET:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100434 dev->status = USB_ST_BABBLE_DET;
435 break;
436 default:
437 dev->status = USB_ST_CRC_ERR;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200438 if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_HALTED)
Anatolij Gustschin222d6df2010-11-02 11:47:29 +0100439 dev->status |= USB_ST_STALLED;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100440 break;
441 }
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200442 dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(token);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100443 } else {
444 dev->act_len = 0;
michaeldb632992008-12-10 17:55:19 +0100445 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
michael51ab1422008-12-11 13:43:55 +0100446 dev->devnum, ehci_readl(&hcor->or_usbsts),
447 ehci_readl(&hcor->or_portsc[0]),
448 ehci_readl(&hcor->or_portsc[1]));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100449 }
450
451 return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
452
453fail:
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100454 return -1;
455}
456
457static inline int min3(int a, int b, int c)
458{
459
460 if (b < a)
461 a = b;
462 if (c < a)
463 a = c;
464 return a;
465}
466
michaeldb632992008-12-10 17:55:19 +0100467int
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100468ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
469 int length, struct devrequest *req)
470{
471 uint8_t tmpbuf[4];
472 u16 typeReq;
michaeldb632992008-12-10 17:55:19 +0100473 void *srcptr = NULL;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100474 int len, srclen;
475 uint32_t reg;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100476 uint32_t *status_reg;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100477
Sergei Shtylyove06a0552010-02-27 21:32:17 +0300478 if (le16_to_cpu(req->index) > CONFIG_SYS_USB_EHCI_MAX_ROOT_PORTS) {
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100479 printf("The request port(%d) is not configured\n",
480 le16_to_cpu(req->index) - 1);
481 return -1;
482 }
483 status_reg = (uint32_t *)&hcor->or_portsc[
484 le16_to_cpu(req->index) - 1];
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100485 srclen = 0;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100486
michaeldb632992008-12-10 17:55:19 +0100487 debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100488 req->request, req->request,
489 req->requesttype, req->requesttype,
490 le16_to_cpu(req->value), le16_to_cpu(req->index));
491
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530492 typeReq = req->request | req->requesttype << 8;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100493
Prafulla Wadaskar44259bb2009-07-17 19:56:30 +0530494 switch (typeReq) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100495 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
496 switch (le16_to_cpu(req->value) >> 8) {
497 case USB_DT_DEVICE:
michaeldb632992008-12-10 17:55:19 +0100498 debug("USB_DT_DEVICE request\n");
499 srcptr = &descriptor.device;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200500 srclen = descriptor.device.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100501 break;
502 case USB_DT_CONFIG:
michaeldb632992008-12-10 17:55:19 +0100503 debug("USB_DT_CONFIG config\n");
504 srcptr = &descriptor.config;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200505 srclen = descriptor.config.bLength +
506 descriptor.interface.bLength +
507 descriptor.endpoint.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100508 break;
509 case USB_DT_STRING:
michaeldb632992008-12-10 17:55:19 +0100510 debug("USB_DT_STRING config\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100511 switch (le16_to_cpu(req->value) & 0xff) {
512 case 0: /* Language */
513 srcptr = "\4\3\1\0";
514 srclen = 4;
515 break;
516 case 1: /* Vendor */
517 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
518 srclen = 14;
519 break;
520 case 2: /* Product */
521 srcptr = "\52\3E\0H\0C\0I\0 "
522 "\0H\0o\0s\0t\0 "
523 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
524 srclen = 42;
525 break;
526 default:
michaeldb632992008-12-10 17:55:19 +0100527 debug("unknown value DT_STRING %x\n",
528 le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100529 goto unknown;
530 }
531 break;
532 default:
michaeldb632992008-12-10 17:55:19 +0100533 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100534 goto unknown;
535 }
536 break;
537 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
538 switch (le16_to_cpu(req->value) >> 8) {
539 case USB_DT_HUB:
michaeldb632992008-12-10 17:55:19 +0100540 debug("USB_DT_HUB config\n");
541 srcptr = &descriptor.hub;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200542 srclen = descriptor.hub.bLength;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100543 break;
544 default:
michaeldb632992008-12-10 17:55:19 +0100545 debug("unknown value %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100546 goto unknown;
547 }
548 break;
549 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
michaeldb632992008-12-10 17:55:19 +0100550 debug("USB_REQ_SET_ADDRESS\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100551 rootdev = le16_to_cpu(req->value);
552 break;
553 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
michaeldb632992008-12-10 17:55:19 +0100554 debug("USB_REQ_SET_CONFIGURATION\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100555 /* Nothing to do */
556 break;
557 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
558 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
559 tmpbuf[1] = 0;
560 srcptr = tmpbuf;
561 srclen = 2;
562 break;
michaeldb632992008-12-10 17:55:19 +0100563 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100564 memset(tmpbuf, 0, 4);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100565 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100566 if (reg & EHCI_PS_CS)
567 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
568 if (reg & EHCI_PS_PE)
569 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
570 if (reg & EHCI_PS_SUSP)
571 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
572 if (reg & EHCI_PS_OCA)
573 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300574 if (reg & EHCI_PS_PR)
575 tmpbuf[0] |= USB_PORT_STAT_RESET;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100576 if (reg & EHCI_PS_PP)
577 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
Stefan Roese597eb282009-01-21 17:12:01 +0100578
579 if (ehci_is_TDI()) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200580 switch (PORTSC_PSPD(reg)) {
581 case PORTSC_PSPD_FS:
Stefan Roese597eb282009-01-21 17:12:01 +0100582 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200583 case PORTSC_PSPD_LS:
Stefan Roese597eb282009-01-21 17:12:01 +0100584 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
585 break;
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200586 case PORTSC_PSPD_HS:
Stefan Roese597eb282009-01-21 17:12:01 +0100587 default:
588 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
589 break;
590 }
591 } else {
592 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
593 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100594
595 if (reg & EHCI_PS_CSC)
596 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
597 if (reg & EHCI_PS_PEC)
598 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
599 if (reg & EHCI_PS_OCC)
600 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
601 if (portreset & (1 << le16_to_cpu(req->index)))
602 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100603
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100604 srcptr = tmpbuf;
605 srclen = 4;
606 break;
michaeldb632992008-12-10 17:55:19 +0100607 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100608 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100609 reg &= ~EHCI_PS_CLEAR;
610 switch (le16_to_cpu(req->value)) {
michael51ab1422008-12-11 13:43:55 +0100611 case USB_PORT_FEAT_ENABLE:
612 reg |= EHCI_PS_PE;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100613 ehci_writel(status_reg, reg);
michael51ab1422008-12-11 13:43:55 +0100614 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100615 case USB_PORT_FEAT_POWER:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100616 if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams))) {
617 reg |= EHCI_PS_PP;
618 ehci_writel(status_reg, reg);
619 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100620 break;
621 case USB_PORT_FEAT_RESET:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100622 if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS &&
623 !ehci_is_TDI() &&
624 EHCI_PS_IS_LOWSPEED(reg)) {
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100625 /* Low speed device, give up ownership. */
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100626 debug("port %d low speed --> companion\n",
627 req->index - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100628 reg |= EHCI_PS_PO;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100629 ehci_writel(status_reg, reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100630 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100631 } else {
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300632 int ret;
633
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100634 reg |= EHCI_PS_PR;
635 reg &= ~EHCI_PS_PE;
636 ehci_writel(status_reg, reg);
637 /*
638 * caller must wait, then call GetPortStatus
639 * usb 2.0 specification say 50 ms resets on
640 * root
641 */
Marek Vasut3874b6d2011-07-11 02:37:01 +0200642 ehci_powerup_fixup(status_reg, &reg);
643
Chris Zhangb4161912010-01-06 13:34:04 -0800644 ehci_writel(status_reg, reg & ~EHCI_PS_PR);
Sergei Shtylyovc8b2d1d2010-02-27 21:33:21 +0300645 /*
646 * A host controller must terminate the reset
647 * and stabilize the state of the port within
648 * 2 milliseconds
649 */
650 ret = handshake(status_reg, EHCI_PS_PR, 0,
651 2 * 1000);
652 if (!ret)
653 portreset |=
654 1 << le16_to_cpu(req->index);
655 else
656 printf("port(%d) reset error\n",
657 le16_to_cpu(req->index) - 1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100658 }
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100659 break;
660 default:
michaeldb632992008-12-10 17:55:19 +0100661 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100662 goto unknown;
663 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100664 /* unblock posted writes */
Kumar Gala50243e32009-07-07 15:48:58 -0500665 (void) ehci_readl(&hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100666 break;
michaeldb632992008-12-10 17:55:19 +0100667 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100668 reg = ehci_readl(status_reg);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100669 switch (le16_to_cpu(req->value)) {
670 case USB_PORT_FEAT_ENABLE:
671 reg &= ~EHCI_PS_PE;
672 break;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100673 case USB_PORT_FEAT_C_ENABLE:
674 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_PE;
675 break;
676 case USB_PORT_FEAT_POWER:
677 if (HCS_PPC(ehci_readl(&hccr->cr_hcsparams)))
678 reg = reg & ~(EHCI_PS_CLEAR | EHCI_PS_PP);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100679 case USB_PORT_FEAT_C_CONNECTION:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100680 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_CSC;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100681 break;
michael51ab1422008-12-11 13:43:55 +0100682 case USB_PORT_FEAT_OVER_CURRENT:
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100683 reg = (reg & ~EHCI_PS_CLEAR) | EHCI_PS_OCC;
michael51ab1422008-12-11 13:43:55 +0100684 break;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100685 case USB_PORT_FEAT_C_RESET:
686 portreset &= ~(1 << le16_to_cpu(req->index));
687 break;
688 default:
michaeldb632992008-12-10 17:55:19 +0100689 debug("unknown feature %x\n", le16_to_cpu(req->value));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100690 goto unknown;
691 }
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100692 ehci_writel(status_reg, reg);
693 /* unblock posted write */
Kumar Gala50243e32009-07-07 15:48:58 -0500694 (void) ehci_readl(&hcor->or_usbcmd);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100695 break;
696 default:
michaeldb632992008-12-10 17:55:19 +0100697 debug("Unknown request\n");
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100698 goto unknown;
699 }
700
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000701 mdelay(1);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100702 len = min3(srclen, le16_to_cpu(req->length), length);
703 if (srcptr != NULL && len > 0)
704 memcpy(buffer, srcptr, len);
michaeldb632992008-12-10 17:55:19 +0100705 else
706 debug("Len is 0\n");
707
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100708 dev->act_len = len;
709 dev->status = 0;
710 return 0;
711
712unknown:
michaeldb632992008-12-10 17:55:19 +0100713 debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100714 req->requesttype, req->request, le16_to_cpu(req->value),
715 le16_to_cpu(req->index), le16_to_cpu(req->length));
716
717 dev->act_len = 0;
718 dev->status = USB_ST_STALLED;
719 return -1;
720}
721
722int usb_lowlevel_stop(void)
723{
724 return ehci_hcd_stop();
725}
726
727int usb_lowlevel_init(void)
728{
729 uint32_t reg;
michaeldb632992008-12-10 17:55:19 +0100730 uint32_t cmd;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100731
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200732 if (ehci_hcd_init())
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100733 return -1;
734
michael51ab1422008-12-11 13:43:55 +0100735 /* EHCI spec section 4.1 */
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200736 if (ehci_reset())
michael51ab1422008-12-11 13:43:55 +0100737 return -1;
738
Stefan Roese832e6142009-01-21 17:12:10 +0100739#if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET)
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200740 if (ehci_hcd_init())
Stefan Roese832e6142009-01-21 17:12:10 +0100741 return -1;
742#endif
743
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100744 /* Set head of reclaim list */
Tom Rini71c5de42012-07-15 22:14:24 +0000745 memset(qh_list, 0, sizeof(*qh_list));
746 qh_list->qh_link = cpu_to_hc32((uint32_t)qh_list | QH_LINK_TYPE_QH);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200747 qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) |
748 QH_ENDPT1_EPS(USB_SPEED_HIGH));
Tom Rini71c5de42012-07-15 22:14:24 +0000749 qh_list->qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
750 qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
751 qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200752 qh_list->qh_overlay.qt_token =
753 cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED));
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100754
michael51ab1422008-12-11 13:43:55 +0100755 reg = ehci_readl(&hccr->cr_hcsparams);
756 descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
757 printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100758 /* Port Indicators */
759 if (HCS_INDICATOR(reg))
michaeldb632992008-12-10 17:55:19 +0100760 descriptor.hub.wHubCharacteristics |= 0x80;
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100761 /* Port Power Control */
762 if (HCS_PPC(reg))
michaeldb632992008-12-10 17:55:19 +0100763 descriptor.hub.wHubCharacteristics |= 0x01;
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100764
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100765 /* Start the host controller. */
michael51ab1422008-12-11 13:43:55 +0100766 cmd = ehci_readl(&hcor->or_usbcmd);
Wolfgang Denkf15c6512009-02-12 00:08:39 +0100767 /*
768 * Philips, Intel, and maybe others need CMD_RUN before the
769 * root hub will detect new devices (why?); NEC doesn't
770 */
michael51ab1422008-12-11 13:43:55 +0100771 cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
772 cmd |= CMD_RUN;
773 ehci_writel(&hcor->or_usbcmd, cmd);
774
775 /* take control over the ports */
776 cmd = ehci_readl(&hcor->or_configflag);
777 cmd |= FLAG_CF;
778 ehci_writel(&hcor->or_configflag, cmd);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100779 /* unblock posted write */
michael51ab1422008-12-11 13:43:55 +0100780 cmd = ehci_readl(&hcor->or_usbcmd);
Mike Frysinger5b84dd62012-03-05 13:47:00 +0000781 mdelay(5);
Remy Böhmerc0d722f2008-12-13 22:51:58 +0100782 reg = HC_VERSION(ehci_readl(&hccr->cr_capbase));
783 printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100784
785 rootdev = 0;
786
787 return 0;
788}
789
790int
791submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
792 int length)
793{
794
795 if (usb_pipetype(pipe) != PIPE_BULK) {
796 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
797 return -1;
798 }
799 return ehci_submit_async(dev, pipe, buffer, length, NULL);
800}
801
802int
803submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
804 int length, struct devrequest *setup)
805{
806
807 if (usb_pipetype(pipe) != PIPE_CONTROL) {
808 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
809 return -1;
810 }
811
812 if (usb_pipedevice(pipe) == rootdev) {
Benoît Thébaudeau14eb79b2012-08-10 18:22:11 +0200813 if (!rootdev)
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100814 dev->speed = USB_SPEED_HIGH;
815 return ehci_submit_root(dev, pipe, buffer, length, setup);
816 }
817 return ehci_submit_async(dev, pipe, buffer, length, setup);
818}
819
820int
821submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
822 int length, int interval)
823{
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100824 debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
825 dev, pipe, buffer, length, interval);
Benoît Thébaudeau44ae0be2012-08-09 23:50:44 +0200826
827 /*
828 * Interrupt transfers requiring several transactions are not supported
829 * because bInterval is ignored.
830 */
831 if (length > usb_maxpacket(dev, pipe)) {
832 printf("%s: Interrupt transfers requiring several transactions "
833 "are not supported.\n", __func__);
834 return -1;
835 }
Marek Vasut7555d5e2011-09-25 21:07:56 +0200836 return ehci_submit_async(dev, pipe, buffer, length, NULL);
Michael Trimarchiaaf098c2008-11-28 13:20:46 +0100837}