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