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