]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/usb/usb_ehci_core.c
ec50874f718850dcc2e06c466a4469d78f6b07f0
[karo-tx-uboot.git] / drivers / usb / usb_ehci_core.c
1 /*-
2  * Copyright (c) 2007-2008, Juniper Networks, Inc.
3  * Copyright (c) 2008, Excito Elektronik i Skåne AB
4  * All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation version 2 of
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21 #define DEBUG
22 #include <common.h>
23 #include <asm/byteorder.h>
24 #include <usb.h>
25 #include <asm/io.h>
26 #include <malloc.h>
27 #include "usb_ehci.h"
28
29 int rootdev;
30 struct ehci_hccr *hccr;         /* R/O registers, not need for volatile */
31 volatile struct ehci_hcor *hcor;
32
33 static uint16_t portreset;
34 static struct QH qh_list __attribute__((aligned(32)));
35
36 static struct descriptor {
37         struct usb_hub_descriptor hub;
38         struct usb_device_descriptor device;
39         struct usb_linux_config_descriptor config;
40         struct usb_linux_interface_descriptor interface;
41         struct usb_endpoint_descriptor endpoint;
42 }  __attribute__ ((packed)) descriptor = {
43         {
44                 0x8,            /* bDescLength */
45                 0x29,           /* bDescriptorType: hub descriptor */
46                 2,              /* bNrPorts -- runtime modified */
47                 0,              /* wHubCharacteristics */
48                 0xff,           /* bPwrOn2PwrGood */
49                 0,              /* bHubCntrCurrent */
50                 {},             /* Device removable */
51                 {}              /* at most 7 ports! XXX */
52         },
53         {
54                 0x12,           /* bLength */
55                 1,              /* bDescriptorType: UDESC_DEVICE */
56                 0x0002,         /* bcdUSB: v2.0 */
57                 9,              /* bDeviceClass: UDCLASS_HUB */
58                 0,              /* bDeviceSubClass: UDSUBCLASS_HUB */
59                 1,              /* bDeviceProtocol: UDPROTO_HSHUBSTT */
60                 64,             /* bMaxPacketSize: 64 bytes */
61                 0x0000,         /* idVendor */
62                 0x0000,         /* idProduct */
63                 0x0001,         /* bcdDevice */
64                 1,              /* iManufacturer */
65                 2,              /* iProduct */
66                 0,              /* iSerialNumber */
67                 1               /* bNumConfigurations: 1 */
68         },
69         {
70                 0x9,
71                 2,              /* bDescriptorType: UDESC_CONFIG */
72                 cpu_to_le16(0x19),
73                 1,              /* bNumInterface */
74                 1,              /* bConfigurationValue */
75                 0,              /* iConfiguration */
76                 0x40,           /* bmAttributes: UC_SELF_POWER */
77                 0               /* bMaxPower */
78         },
79         {
80                 0x9,            /* bLength */
81                 4,              /* bDescriptorType: UDESC_INTERFACE */
82                 0,              /* bInterfaceNumber */
83                 0,              /* bAlternateSetting */
84                 1,              /* bNumEndpoints */
85                 9,              /* bInterfaceClass: UICLASS_HUB */
86                 0,              /* bInterfaceSubClass: UISUBCLASS_HUB */
87                 0,              /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
88                 0               /* iInterface */
89         },
90         {
91                 0x7,            /* bLength */
92                 5,              /* bDescriptorType: UDESC_ENDPOINT */
93                 0x81,           /* bEndpointAddress:
94                                  * UE_DIR_IN | EHCI_INTR_ENDPT
95                                  */
96                 3,              /* bmAttributes: UE_INTERRUPT */
97                 8, 0,           /* wMaxPacketSize */
98                 255             /* bInterval */
99         },
100 };
101
102 static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int msec)
103 {
104         uint32_t result;
105         do {
106                 result = ehci_readl(ptr);
107                 debug("handshake read reg(%x)=%x\n", (uint32_t)ptr, result);
108                 if (result == ~(uint32_t)0)
109                         return -1;
110                 result &= mask;
111                 if (result == done)
112                         return 0;
113                 wait_ms(1);
114                 msec--;
115         } while (msec > 0);
116         return -1;
117 }
118
119 static void ehci_free(void *p, size_t sz)
120 {
121
122 }
123
124 static int ehci_reset(void)
125 {
126         uint32_t cmd;
127         uint32_t tmp;
128         uint32_t *reg_ptr;
129         int ret = 0;
130
131         cmd = ehci_readl(&hcor->or_usbcmd);
132         cmd |= CMD_RESET;
133         ehci_writel(&hcor->or_usbcmd, cmd);
134         ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, 250);
135         if (ret < 0) {
136                 printf("EHCI fail to reset\n");
137                 goto out;
138         }
139
140 #if defined CONFIG_EHCI_IS_TDI
141         reg_ptr = (uint32_t *)((u8 *)hcor + USBMODE);
142         tmp = ehci_readl(reg_ptr);
143         tmp |= USBMODE_CM_HC;
144 #if defined CONFIG_EHCI_MMIO_BIG_ENDIAN
145         tmp |= USBMODE_BE;
146 #endif
147         ehci_writel(reg_ptr, tmp);
148 #endif
149 out:
150         return ret;
151 }
152
153 static void *ehci_alloc(size_t sz, size_t align)
154 {
155         static struct QH qh __attribute__((aligned(32)));
156         static struct qTD td[3] __attribute__((aligned (32)));
157         static int ntds;
158         void *p;
159
160         switch (sz) {
161         case sizeof(struct QH):
162                 p = &qh;
163                 ntds = 0;
164                 break;
165         case sizeof(struct qTD):
166                 if (ntds == 3) {
167                         debug("out of TDs\n");
168                         return NULL;
169                 }
170                 p = &td[ntds];
171                 ntds++;
172                 break;
173         default:
174                 debug("unknown allocation size\n");
175                 return NULL;
176         }
177
178         memset(p, sz, 0);
179         return p;
180 }
181
182 static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz)
183 {
184         uint32_t addr, delta, next;
185         int idx;
186
187         addr = (uint32_t) buf;
188         idx = 0;
189         while (idx < 5) {
190                 td->qt_buffer[idx] = cpu_to_hc32(addr);
191                 next = (addr + 4096) & ~4095;
192                 delta = next - addr;
193                 if (delta >= sz)
194                         break;
195                 sz -= delta;
196                 addr = next;
197                 idx++;
198         }
199
200         if (idx == 5) {
201                 debug("out of buffer pointers (%u bytes left)\n", sz);
202                 return -1;
203         }
204
205         return 0;
206 }
207
208 static int
209 ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer,
210                    int length, struct devrequest *req)
211 {
212         struct QH *qh;
213         struct qTD *td;
214         volatile struct qTD *vtd;
215         unsigned long ts;
216         uint32_t *tdp;
217         uint32_t endpt, token, usbsts;
218         uint32_t c, toggle;
219         uint32_t cmd;
220         uint32_t sts;
221
222         debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe,
223               buffer, length, req);
224         if (req != NULL)
225                 debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n",
226                       req->request, req->request,
227                       req->requesttype, req->requesttype,
228                       le16_to_cpu(req->value), le16_to_cpu(req->value),
229                       le16_to_cpu(req->index));
230
231         qh = ehci_alloc(sizeof(struct QH), 32);
232         if (qh == NULL) {
233                 debug("unable to allocate QH\n");
234                 return -1;
235         }
236         qh->qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
237         c = (usb_pipespeed(pipe) != USB_SPEED_HIGH &&
238              usb_pipeendpoint(pipe) == 0) ? 1 : 0;
239         endpt = (8 << 28) |
240             (c << 27) |
241             (usb_maxpacket(dev, pipe) << 16) |
242             (0 << 15) |
243             (1 << 14) |
244             (usb_pipespeed(pipe) << 12) |
245             (usb_pipeendpoint(pipe) << 8) |
246             (0 << 7) | (usb_pipedevice(pipe) << 0);
247         qh->qh_endpt1 = cpu_to_hc32(endpt);
248         endpt = (1 << 30) |
249             (dev->portnr << 23) |
250             (dev->parent->devnum << 16) | (0 << 8) | (0 << 0);
251         qh->qh_endpt2 = cpu_to_hc32(endpt);
252         qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
253         qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
254
255         td = NULL;
256         tdp = &qh->qh_overlay.qt_next;
257
258         toggle =
259             usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
260
261         if (req != NULL) {
262                 td = ehci_alloc(sizeof(struct qTD), 32);
263                 if (td == NULL) {
264                         debug("unable to allocate SETUP td\n");
265                         goto fail;
266                 }
267                 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
268                 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
269                 token = (0 << 31) |
270                     (sizeof(*req) << 16) |
271                     (0 << 15) | (0 << 12) | (3 << 10) | (2 << 8) | (0x80 << 0);
272                 td->qt_token = cpu_to_hc32(token);
273                 if (ehci_td_buffer(td, req, sizeof(*req)) != 0) {
274                         debug("unable construct SETUP td\n");
275                         ehci_free(td, sizeof(*td));
276                         goto fail;
277                 }
278                 *tdp = cpu_to_hc32((uint32_t) td);
279                 tdp = &td->qt_next;
280                 toggle = 1;
281         }
282
283         if (length > 0 || req == NULL) {
284                 td = ehci_alloc(sizeof(struct qTD), 32);
285                 if (td == NULL) {
286                         debug("unable to allocate DATA td\n");
287                         goto fail;
288                 }
289                 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
290                 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
291                 token = (toggle << 31) |
292                     (length << 16) |
293                     ((req == NULL ? 1 : 0) << 15) |
294                     (0 << 12) |
295                     (3 << 10) |
296                     ((usb_pipein(pipe) ? 1 : 0) << 8) | (0x80 << 0);
297                 td->qt_token = cpu_to_hc32(token);
298                 if (ehci_td_buffer(td, buffer, length) != 0) {
299                         debug("unable construct DATA td\n");
300                         ehci_free(td, sizeof(*td));
301                         goto fail;
302                 }
303                 *tdp = cpu_to_hc32((uint32_t) td);
304                 tdp = &td->qt_next;
305         }
306
307         if (req != NULL) {
308                 td = ehci_alloc(sizeof(struct qTD), 32);
309                 if (td == NULL) {
310                         debug("unable to allocate ACK td\n");
311                         goto fail;
312                 }
313                 td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
314                 td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
315                 token = (toggle << 31) |
316                     (0 << 16) |
317                     (1 << 15) |
318                     (0 << 12) |
319                     (3 << 10) |
320                     ((usb_pipein(pipe) ? 0 : 1) << 8) | (0x80 << 0);
321                 td->qt_token = cpu_to_hc32(token);
322                 *tdp = cpu_to_hc32((uint32_t) td);
323                 tdp = &td->qt_next;
324         }
325
326         qh_list.qh_link = cpu_to_hc32((uint32_t) qh | QH_LINK_TYPE_QH);
327
328         usbsts = ehci_readl(&hcor->or_usbsts);
329         ehci_writel(&hcor->or_usbsts, (usbsts & 0x3f));
330
331         /* Enable async. schedule. */
332         cmd = ehci_readl(&hcor->or_usbcmd);
333         cmd |= CMD_ASE;
334         ehci_writel(&hcor->or_usbcmd, cmd);
335
336         sts = ehci_readl(&hcor->or_usbsts);
337         while ((sts & STD_ASS) == 0) {
338                 sts = ehci_readl(&hcor->or_usbsts);
339                 udelay(10);
340         }
341
342         /* Wait for TDs to be processed. */
343         ts = get_timer(0);
344         vtd = td;
345         do {
346                 token = hc32_to_cpu(vtd->qt_token);
347                 if (!(token & 0x80))
348                         break;
349         } while (get_timer(ts) < CONFIG_SYS_HZ);
350
351         /* Disable async schedule. */
352         cmd = ehci_readl(&hcor->or_usbcmd);
353         cmd &= ~CMD_ASE;
354         ehci_writel(&hcor->or_usbcmd, cmd);
355
356         sts = ehci_readl(&hcor->or_usbsts);
357         while ((sts & STD_ASS) != 0) {
358                 sts = ehci_readl(&hcor->or_usbsts);
359                 udelay(10);
360         }
361
362         qh_list.qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
363
364         token = hc32_to_cpu(qh->qh_overlay.qt_token);
365         if (!(token & 0x80)) {
366                 debug("TOKEN=%#x\n", token);
367                 switch (token & 0xfc) {
368                 case 0:
369                         toggle = token >> 31;
370                         usb_settoggle(dev, usb_pipeendpoint(pipe),
371                                        usb_pipeout(pipe), toggle);
372                         dev->status = 0;
373                         break;
374                 case 0x40:
375                         dev->status = USB_ST_STALLED;
376                         break;
377                 case 0xa0:
378                 case 0x20:
379                         dev->status = USB_ST_BUF_ERR;
380                         break;
381                 case 0x50:
382                 case 0x10:
383                         dev->status = USB_ST_BABBLE_DET;
384                         break;
385                 default:
386                         dev->status = USB_ST_CRC_ERR;
387                         break;
388                 }
389                 dev->act_len = length - ((token >> 16) & 0x7fff);
390         } else {
391                 dev->act_len = 0;
392                 debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n",
393                       dev->devnum, ehci_readl(&hcor->or_usbsts),
394                       ehci_readl(&hcor->or_portsc[0]),
395                       ehci_readl(&hcor->or_portsc[1]));
396         }
397
398         return (dev->status != USB_ST_NOT_PROC) ? 0 : -1;
399
400 fail:
401         td = (void *)hc32_to_cpu(qh->qh_overlay.qt_next);
402         while (td != (void *)QT_NEXT_TERMINATE) {
403                 qh->qh_overlay.qt_next = td->qt_next;
404                 ehci_free(td, sizeof(*td));
405                 td = (void *)hc32_to_cpu(qh->qh_overlay.qt_next);
406         }
407         ehci_free(qh, sizeof(*qh));
408         return -1;
409 }
410
411 static inline int min3(int a, int b, int c)
412 {
413
414         if (b < a)
415                 a = b;
416         if (c < a)
417                 a = c;
418         return a;
419 }
420
421 int
422 ehci_submit_root(struct usb_device *dev, unsigned long pipe, void *buffer,
423                  int length, struct devrequest *req)
424 {
425         uint8_t tmpbuf[4];
426         u16 typeReq;
427         void *srcptr = NULL;
428         int len, srclen;
429         uint32_t reg;
430
431         srclen = 0;
432
433         debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n",
434               req->request, req->request,
435               req->requesttype, req->requesttype,
436               le16_to_cpu(req->value), le16_to_cpu(req->index));
437
438         typeReq = req->request << 8 | req->requesttype;
439
440         switch (le16_to_cpu(typeReq)) {
441         case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
442                 switch (le16_to_cpu(req->value) >> 8) {
443                 case USB_DT_DEVICE:
444                         debug("USB_DT_DEVICE request\n");
445                         srcptr = &descriptor.device;
446                         srclen = 0x12;
447                         break;
448                 case USB_DT_CONFIG:
449                         debug("USB_DT_CONFIG config\n");
450                         srcptr = &descriptor.config;
451                         srclen = 0x19;
452                         break;
453                 case USB_DT_STRING:
454                         debug("USB_DT_STRING config\n");
455                         switch (le16_to_cpu(req->value) & 0xff) {
456                         case 0: /* Language */
457                                 srcptr = "\4\3\1\0";
458                                 srclen = 4;
459                                 break;
460                         case 1: /* Vendor */
461                                 srcptr = "\16\3u\0-\0b\0o\0o\0t\0";
462                                 srclen = 14;
463                                 break;
464                         case 2: /* Product */
465                                 srcptr = "\52\3E\0H\0C\0I\0 "
466                                          "\0H\0o\0s\0t\0 "
467                                          "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
468                                 srclen = 42;
469                                 break;
470                         default:
471                                 debug("unknown value DT_STRING %x\n",
472                                         le16_to_cpu(req->value));
473                                 goto unknown;
474                         }
475                         break;
476                 default:
477                         debug("unknown value %x\n", le16_to_cpu(req->value));
478                         goto unknown;
479                 }
480                 break;
481         case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
482                 switch (le16_to_cpu(req->value) >> 8) {
483                 case USB_DT_HUB:
484                         debug("USB_DT_HUB config\n");
485                         srcptr = &descriptor.hub;
486                         srclen = 0x8;
487                         break;
488                 default:
489                         debug("unknown value %x\n", le16_to_cpu(req->value));
490                         goto unknown;
491                 }
492                 break;
493         case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
494                 debug("USB_REQ_SET_ADDRESS\n");
495                 rootdev = le16_to_cpu(req->value);
496                 break;
497         case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
498                 debug("USB_REQ_SET_CONFIGURATION\n");
499                 /* Nothing to do */
500                 break;
501         case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
502                 tmpbuf[0] = 1;  /* USB_STATUS_SELFPOWERED */
503                 tmpbuf[1] = 0;
504                 srcptr = tmpbuf;
505                 srclen = 2;
506                 break;
507         case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
508                 memset(tmpbuf, 0, 4);
509                 reg = ehci_readl(&hcor->or_portsc[le16_to_cpu(req->index)
510                                    - 1]);
511                 if (reg & EHCI_PS_CS)
512                         tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
513                 if (reg & EHCI_PS_PE)
514                         tmpbuf[0] |= USB_PORT_STAT_ENABLE;
515                 if (reg & EHCI_PS_SUSP)
516                         tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
517                 if (reg & EHCI_PS_OCA)
518                         tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
519                 if (reg & EHCI_PS_PR)
520                         tmpbuf[0] |= USB_PORT_STAT_RESET;
521                 if (reg & EHCI_PS_PP)
522                         tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
523                 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
524
525                 if (reg & EHCI_PS_CSC)
526                         tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
527                 if (reg & EHCI_PS_PEC)
528                         tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
529                 if (reg & EHCI_PS_OCC)
530                         tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
531                 if (portreset & (1 << le16_to_cpu(req->index)))
532                         tmpbuf[2] |= USB_PORT_STAT_C_RESET;
533                 srcptr = tmpbuf;
534                 srclen = 4;
535                 break;
536         case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
537                 reg = ehci_readl(&hcor->or_portsc[le16_to_cpu(req->index) - 1]);
538                 reg &= ~EHCI_PS_CLEAR;
539                 switch (le16_to_cpu(req->value)) {
540                 case USB_PORT_FEAT_ENABLE:
541                         reg |= EHCI_PS_PE;
542                         break;
543                 case USB_PORT_FEAT_POWER:
544                         reg |= EHCI_PS_PP;
545                         break;
546                 case USB_PORT_FEAT_RESET:
547                         debug("USB FEAT RESET\n");
548                         if (EHCI_PS_IS_LOWSPEED(reg)) {
549                                 /* Low speed device, give up ownership. */
550                                 reg |= EHCI_PS_PO;
551                                 break;
552                         }
553                         /* Start reset sequence. */
554                         reg &= ~EHCI_PS_PE;
555                         reg |= EHCI_PS_PR;
556                         ehci_writel(&hcor->or_portsc[
557                                 le16_to_cpu(req->index) - 1], reg);
558                         /* Wait for reset to complete. */
559                         wait_ms(500);
560                         /* Terminate reset sequence. */
561                         reg &= ~EHCI_PS_PR;
562                         /* TODO: is it only fsl chip that requires this
563                          * manual setting of port enable?
564                          */
565                         reg |= EHCI_PS_PE;
566                         ehci_writel(&hcor->or_portsc[
567                                 le16_to_cpu(req->index) - 1], reg);
568                         /* Wait for HC to complete reset. */
569                         wait_ms(10);
570                         reg =
571                             ehci_readl(&hcor->or_portsc[le16_to_cpu(req->index)
572                                                         - 1]);
573                         reg &= ~EHCI_PS_CLEAR;
574                         if ((reg & EHCI_PS_PE) == 0) {
575                                 /* Not a high speed device, give up
576                                  * ownership. */
577                                 reg |= EHCI_PS_PO;
578                                 break;
579                         }
580                         portreset |= 1 << le16_to_cpu(req->index);
581                         break;
582                 default:
583                         debug("unknown feature %x\n", le16_to_cpu(req->value));
584                         goto unknown;
585                 }
586                 ehci_writel(&hcor->or_portsc[le16_to_cpu(req->index) - 1], reg);
587                 break;
588         case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
589                 reg = ehci_readl(&hcor->or_portsc[le16_to_cpu(req->index) - 1]);
590                 reg &= ~EHCI_PS_CLEAR;
591                 switch (le16_to_cpu(req->value)) {
592                 case USB_PORT_FEAT_ENABLE:
593                         reg &= ~EHCI_PS_PE;
594                         break;
595                 case USB_PORT_FEAT_C_CONNECTION:
596                         reg |= EHCI_PS_CSC;
597                         break;
598                 case USB_PORT_FEAT_OVER_CURRENT:
599                         reg |= EHCI_PS_OCC;
600                         break;
601                 case USB_PORT_FEAT_C_RESET:
602                         portreset &= ~(1 << le16_to_cpu(req->index));
603                         break;
604                 default:
605                         debug("unknown feature %x\n", le16_to_cpu(req->value));
606                         goto unknown;
607                 }
608                 ehci_writel(&hcor->or_portsc[le16_to_cpu(req->index) - 1], reg);
609                 break;
610         default:
611                 debug("Unknown request\n");
612                 goto unknown;
613         }
614
615         wait_ms(1);
616         len = min3(srclen, le16_to_cpu(req->length), length);
617         if (srcptr != NULL && len > 0)
618                 memcpy(buffer, srcptr, len);
619         else
620                 debug("Len is 0\n");
621
622         dev->act_len = len;
623         dev->status = 0;
624         return 0;
625
626 unknown:
627         debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n",
628               req->requesttype, req->request, le16_to_cpu(req->value),
629               le16_to_cpu(req->index), le16_to_cpu(req->length));
630
631         dev->act_len = 0;
632         dev->status = USB_ST_STALLED;
633         return -1;
634 }
635
636 int usb_lowlevel_stop(void)
637 {
638         return ehci_hcd_stop();
639 }
640
641 int usb_lowlevel_init(void)
642 {
643         uint32_t reg;
644         uint32_t cmd;
645
646         if (ehci_hcd_init() != 0)
647                 return -1;
648
649         /* EHCI spec section 4.1 */
650         if (ehci_reset() != 0)
651                 return -1;
652
653         /* Set head of reclaim list */
654         memset(&qh_list, 0, sizeof(qh_list));
655         qh_list.qh_link = cpu_to_hc32((uint32_t)&qh_list | QH_LINK_TYPE_QH);
656         qh_list.qh_endpt1 = cpu_to_hc32((1 << 15) | (USB_SPEED_HIGH << 12));
657         qh_list.qh_curtd = cpu_to_hc32(QT_NEXT_TERMINATE);
658         qh_list.qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE);
659         qh_list.qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE);
660         qh_list.qh_overlay.qt_token = cpu_to_hc32(0x40);
661
662         /* Set async. queue head pointer. */
663         ehci_writel(&hcor->or_asynclistaddr, (uint32_t)&qh_list);
664
665         reg = ehci_readl(&hccr->cr_hcsparams);
666         descriptor.hub.bNbrPorts = HCS_N_PORTS(reg);
667         printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
668         if (reg & 0x10000)      /* Port Indicators */
669                 descriptor.hub.wHubCharacteristics |= 0x80;
670         if (reg & 0x10)         /* Port Power Control */
671                 descriptor.hub.wHubCharacteristics |= 0x01;
672
673         /* Start the host controller. */
674         cmd = ehci_readl(&hcor->or_usbcmd);
675         /* Philips, Intel, and maybe others need CMD_RUN before the
676          * root hub will detect new devices (why?); NEC doesn't */
677         cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
678         cmd |= CMD_RUN;
679         ehci_writel(&hcor->or_usbcmd, cmd);
680
681         /* take control over the ports */
682         cmd = ehci_readl(&hcor->or_configflag);
683         cmd |= FLAG_CF;
684         ehci_writel(&hcor->or_configflag, cmd);
685         /* unblock posted writes */
686         cmd = ehci_readl(&hcor->or_usbcmd);
687         wait_ms(5);
688
689         rootdev = 0;
690
691         return 0;
692 }
693
694 int
695 submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
696                 int length)
697 {
698
699         if (usb_pipetype(pipe) != PIPE_BULK) {
700                 debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
701                 return -1;
702         }
703         return ehci_submit_async(dev, pipe, buffer, length, NULL);
704 }
705
706 int
707 submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
708                    int length, struct devrequest *setup)
709 {
710
711         if (usb_pipetype(pipe) != PIPE_CONTROL) {
712                 debug("non-control pipe (type=%lu)", usb_pipetype(pipe));
713                 return -1;
714         }
715
716         if (usb_pipedevice(pipe) == rootdev) {
717                 if (rootdev == 0)
718                         dev->speed = USB_SPEED_HIGH;
719                 return ehci_submit_root(dev, pipe, buffer, length, setup);
720         }
721         return ehci_submit_async(dev, pipe, buffer, length, setup);
722 }
723
724 int
725 submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
726                int length, int interval)
727 {
728
729         debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d",
730               dev, pipe, buffer, length, interval);
731         return -1;
732 }