]> git.sur5r.net Git - u-boot/blob - drivers/usb/isp116x-hcd.c
Merge branch 'master' of git://www.denx.de/git/u-boot-arm
[u-boot] / drivers / usb / isp116x-hcd.c
1 /*
2  * ISP116x HCD (Host Controller Driver) for u-boot.
3  *
4  * Copyright (C) 2006-2007 Rodolfo Giometti <giometti@linux.it>
5  * Copyright (C) 2006-2007 Eurotech S.p.A. <info@eurotech.it>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  *
22  *
23  * Derived in part from the SL811 HCD driver "u-boot/drivers/usb/sl811_usb.c"
24  * (original copyright message follows):
25  *
26  *    (C) Copyright 2004
27  *    Wolfgang Denk, DENX Software Engineering, wd@denx.de.
28  *
29  *    This code is based on linux driver for sl811hs chip, source at
30  *    drivers/usb/host/sl811.c:
31  *
32  *    SL811 Host Controller Interface driver for USB.
33  *
34  *    Copyright (c) 2003/06, Courage Co., Ltd.
35  *
36  *    Based on:
37  *         1.uhci.c by Linus Torvalds, Johannes Erdfelt, Randy Dunlap,
38  *           Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber,
39  *           Adam Richter, Gregory P. Smith;
40  *         2.Original SL811 driver (hc_sl811.o) by Pei Liu <pbl@cypress.com>
41  *         3.Rewrited as sl811.o by Yin Aihua <yinah:couragetech.com.cn>
42  *
43  *    [[GNU/GPL disclaimer]]
44  *
45  * and in part from AU1x00 OHCI HCD driver "u-boot/cpu/mips/au1x00_usb_ohci.c"
46  * (original copyright message follows):
47  *
48  *    URB OHCI HCD (Host Controller Driver) for USB on the AU1x00.
49  *
50  *    (C) Copyright 2003
51  *    Gary Jennejohn, DENX Software Engineering <gj@denx.de>
52  *
53  *    [[GNU/GPL disclaimer]]
54  *
55  *    Note: Part of this code has been derived from linux
56  */
57
58 #include <common.h>
59
60 #ifdef CONFIG_USB_ISP116X_HCD
61 #include <asm/io.h>
62 #include <usb.h>
63 #include <malloc.h>
64 #include <linux/list.h>
65
66 /*
67  * ISP116x chips require certain delays between accesses to its
68  * registers. The following timing options exist.
69  *
70  * 1. Configure your memory controller (the best)
71  * 2. Use ndelay (easiest, poorest). For that, enable the following macro.
72  *
73  * Value is in microseconds.
74  */
75 #ifdef ISP116X_HCD_USE_UDELAY
76 #define UDELAY          1
77 #endif
78
79 /*
80  * On some (slowly?) machines an extra delay after data packing into
81  * controller's FIFOs is required, * otherwise you may get the following
82  * error:
83  *
84  *   uboot> usb start
85  *   (Re)start USB...
86  *   USB:   scanning bus for devices... isp116x: isp116x_submit_job: CTL:TIMEOUT
87  *   isp116x: isp116x_submit_job: ****** FIFO not ready! ******
88  *
89  *         USB device not responding, giving up (status=4)
90  *         isp116x: isp116x_submit_job: ****** FIFO not empty! ******
91  *         isp116x: isp116x_submit_job: ****** FIFO not empty! ******
92  *         isp116x: isp116x_submit_job: ****** FIFO not empty! ******
93  *         3 USB Device(s) found
94  *                scanning bus for storage devices... 0 Storage Device(s) found
95  *
96  * Value is in milliseconds.
97  */
98 #ifdef ISP116X_HCD_USE_EXTRA_DELAY
99 #define EXTRA_DELAY     2
100 #endif
101
102 /*
103  * Enable the following defines if you wish enable debugging messages.
104  */
105 #undef DEBUG                    /* enable debugging messages */
106 #undef TRACE                    /* enable tracing code */
107 #undef VERBOSE                  /* verbose debugging messages */
108
109 #include "isp116x.h"
110
111 #define DRIVER_VERSION  "08 Jan 2007"
112 static const char hcd_name[] = "isp116x-hcd";
113
114 struct isp116x isp116x_dev;
115 struct isp116x_platform_data isp116x_board;
116 static int got_rhsc;            /* root hub status change */
117 struct usb_device *devgone;     /* device which was disconnected */
118 static int rh_devnum;           /* address of Root Hub endpoint */
119
120 /* ------------------------------------------------------------------------- */
121
122 #define ALIGN(x,a)      (((x)+(a)-1UL)&~((a)-1UL))
123 #define min_t(type,x,y) \
124         ({ type __x = (x); type __y = (y); __x < __y ? __x : __y; })
125
126 /* ------------------------------------------------------------------------- */
127
128 static int isp116x_reset(struct isp116x *isp116x);
129
130 /* --- Debugging functions ------------------------------------------------- */
131
132 #define isp116x_show_reg(d, r) {                                \
133         if ((r) < 0x20) {                                       \
134                 DBG("%-12s[%02x]: %08x", #r,                    \
135                         r, isp116x_read_reg32(d, r));           \
136         } else {                                                \
137                 DBG("%-12s[%02x]:     %04x", #r,                \
138                         r, isp116x_read_reg16(d, r));           \
139         }                                                       \
140 }
141
142 #define isp116x_show_regs(d) {                                  \
143         isp116x_show_reg(d, HCREVISION);                        \
144         isp116x_show_reg(d, HCCONTROL);                         \
145         isp116x_show_reg(d, HCCMDSTAT);                         \
146         isp116x_show_reg(d, HCINTSTAT);                         \
147         isp116x_show_reg(d, HCINTENB);                          \
148         isp116x_show_reg(d, HCFMINTVL);                         \
149         isp116x_show_reg(d, HCFMREM);                           \
150         isp116x_show_reg(d, HCFMNUM);                           \
151         isp116x_show_reg(d, HCLSTHRESH);                        \
152         isp116x_show_reg(d, HCRHDESCA);                         \
153         isp116x_show_reg(d, HCRHDESCB);                         \
154         isp116x_show_reg(d, HCRHSTATUS);                        \
155         isp116x_show_reg(d, HCRHPORT1);                         \
156         isp116x_show_reg(d, HCRHPORT2);                         \
157         isp116x_show_reg(d, HCHWCFG);                           \
158         isp116x_show_reg(d, HCDMACFG);                          \
159         isp116x_show_reg(d, HCXFERCTR);                         \
160         isp116x_show_reg(d, HCuPINT);                           \
161         isp116x_show_reg(d, HCuPINTENB);                        \
162         isp116x_show_reg(d, HCCHIPID);                          \
163         isp116x_show_reg(d, HCSCRATCH);                         \
164         isp116x_show_reg(d, HCITLBUFLEN);                       \
165         isp116x_show_reg(d, HCATLBUFLEN);                       \
166         isp116x_show_reg(d, HCBUFSTAT);                         \
167         isp116x_show_reg(d, HCRDITL0LEN);                       \
168         isp116x_show_reg(d, HCRDITL1LEN);                       \
169 }
170
171 #if defined(TRACE)
172
173 static int isp116x_get_current_frame_number(struct usb_device *usb_dev)
174 {
175         struct isp116x *isp116x = &isp116x_dev;
176
177         return isp116x_read_reg32(isp116x, HCFMNUM);
178 }
179
180 static void dump_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
181                      int len, char *str)
182 {
183 #if defined(VERBOSE)
184         int i;
185 #endif
186
187         DBG("%s URB:[%4x] dev:%2d,ep:%2d-%c,type:%s,len:%d stat:%#lx",
188             str,
189             isp116x_get_current_frame_number(dev),
190             usb_pipedevice(pipe),
191             usb_pipeendpoint(pipe),
192             usb_pipeout(pipe) ? 'O' : 'I',
193             usb_pipetype(pipe) < 2 ?
194             (usb_pipeint(pipe) ?
195              "INTR" : "ISOC") :
196             (usb_pipecontrol(pipe) ? "CTRL" : "BULK"), len, dev->status);
197 #if defined(VERBOSE)
198         if (len > 0 && buffer) {
199                 printf(__FILE__ ": data(%d):", len);
200                 for (i = 0; i < 16 && i < len; i++)
201                         printf(" %02x", ((__u8 *) buffer)[i]);
202                 printf("%s\n", i < len ? "..." : "");
203         }
204 #endif
205 }
206
207 #define PTD_DIR_STR(ptd)  ({char __c;           \
208         switch(PTD_GET_DIR(ptd)){               \
209         case 0:  __c = 's'; break;              \
210         case 1:  __c = 'o'; break;              \
211         default: __c = 'i'; break;              \
212         }; __c;})
213
214 /*
215   Dump PTD info. The code documents the format
216   perfectly, right :)
217 */
218 static inline void dump_ptd(struct ptd *ptd)
219 {
220 #if defined(VERBOSE)
221         int k;
222 #endif
223
224         DBG("PTD(ext) : cc:%x %d%c%d %d,%d,%d t:%x %x%x%x",
225             PTD_GET_CC(ptd),
226             PTD_GET_FA(ptd), PTD_DIR_STR(ptd), PTD_GET_EP(ptd),
227             PTD_GET_COUNT(ptd), PTD_GET_LEN(ptd), PTD_GET_MPS(ptd),
228             PTD_GET_TOGGLE(ptd),
229             PTD_GET_ACTIVE(ptd), PTD_GET_SPD(ptd), PTD_GET_LAST(ptd));
230 #if defined(VERBOSE)
231         printf("isp116x: %s: PTD(byte): ", __FUNCTION__);
232         for (k = 0; k < sizeof(struct ptd); ++k)
233                 printf("%02x ", ((u8 *) ptd)[k]);
234         printf("\n");
235 #endif
236 }
237
238 static inline void dump_ptd_data(struct ptd *ptd, u8 * buf, int type)
239 {
240 #if defined(VERBOSE)
241         int k;
242
243         if (type == 0 /* 0ut data */ ) {
244                 printf("isp116x: %s: out data: ", __FUNCTION__);
245                 for (k = 0; k < PTD_GET_LEN(ptd); ++k)
246                         printf("%02x ", ((u8 *) buf)[k]);
247                 printf("\n");
248         }
249         if (type == 1 /* 1n data */ ) {
250                 printf("isp116x: %s: in data: ", __FUNCTION__);
251                 for (k = 0; k < PTD_GET_COUNT(ptd); ++k)
252                         printf("%02x ", ((u8 *) buf)[k]);
253                 printf("\n");
254         }
255
256         if (PTD_GET_LAST(ptd))
257                 DBG("--- last PTD ---");
258 #endif
259 }
260
261 #else
262
263 #define dump_msg(dev, pipe, buffer, len, str)                   do { } while (0)
264 #define dump_pkt(dev, pipe, buffer, len, setup, str, small)     do {} while (0)
265
266 #define dump_ptd(ptd)                   do {} while (0)
267 #define dump_ptd_data(ptd, buf, type)   do {} while (0)
268
269 #endif
270
271 /* --- Virtual Root Hub ---------------------------------------------------- */
272
273 /* Device descriptor */
274 static __u8 root_hub_dev_des[] = {
275         0x12,                   /*  __u8  bLength; */
276         0x01,                   /*  __u8  bDescriptorType; Device */
277         0x10,                   /*  __u16 bcdUSB; v1.1 */
278         0x01,
279         0x09,                   /*  __u8  bDeviceClass; HUB_CLASSCODE */
280         0x00,                   /*  __u8  bDeviceSubClass; */
281         0x00,                   /*  __u8  bDeviceProtocol; */
282         0x08,                   /*  __u8  bMaxPacketSize0; 8 Bytes */
283         0x00,                   /*  __u16 idVendor; */
284         0x00,
285         0x00,                   /*  __u16 idProduct; */
286         0x00,
287         0x00,                   /*  __u16 bcdDevice; */
288         0x00,
289         0x00,                   /*  __u8  iManufacturer; */
290         0x01,                   /*  __u8  iProduct; */
291         0x00,                   /*  __u8  iSerialNumber; */
292         0x01                    /*  __u8  bNumConfigurations; */
293 };
294
295 /* Configuration descriptor */
296 static __u8 root_hub_config_des[] = {
297         0x09,                   /*  __u8  bLength; */
298         0x02,                   /*  __u8  bDescriptorType; Configuration */
299         0x19,                   /*  __u16 wTotalLength; */
300         0x00,
301         0x01,                   /*  __u8  bNumInterfaces; */
302         0x01,                   /*  __u8  bConfigurationValue; */
303         0x00,                   /*  __u8  iConfiguration; */
304         0x40,                   /*  __u8  bmAttributes;
305                                    Bit 7: Bus-powered, 6: Self-powered, 5 Remote-wakwup, 4..0: resvd */
306         0x00,                   /*  __u8  MaxPower; */
307
308         /* interface */
309         0x09,                   /*  __u8  if_bLength; */
310         0x04,                   /*  __u8  if_bDescriptorType; Interface */
311         0x00,                   /*  __u8  if_bInterfaceNumber; */
312         0x00,                   /*  __u8  if_bAlternateSetting; */
313         0x01,                   /*  __u8  if_bNumEndpoints; */
314         0x09,                   /*  __u8  if_bInterfaceClass; HUB_CLASSCODE */
315         0x00,                   /*  __u8  if_bInterfaceSubClass; */
316         0x00,                   /*  __u8  if_bInterfaceProtocol; */
317         0x00,                   /*  __u8  if_iInterface; */
318
319         /* endpoint */
320         0x07,                   /*  __u8  ep_bLength; */
321         0x05,                   /*  __u8  ep_bDescriptorType; Endpoint */
322         0x81,                   /*  __u8  ep_bEndpointAddress; IN Endpoint 1 */
323         0x03,                   /*  __u8  ep_bmAttributes; Interrupt */
324         0x00,                   /*  __u16 ep_wMaxPacketSize; ((MAX_ROOT_PORTS + 1) / 8 */
325         0x02,
326         0xff                    /*  __u8  ep_bInterval; 255 ms */
327 };
328
329 static unsigned char root_hub_str_index0[] = {
330         0x04,                   /*  __u8  bLength; */
331         0x03,                   /*  __u8  bDescriptorType; String-descriptor */
332         0x09,                   /*  __u8  lang ID */
333         0x04,                   /*  __u8  lang ID */
334 };
335
336 static unsigned char root_hub_str_index1[] = {
337         0x22,                   /*  __u8  bLength; */
338         0x03,                   /*  __u8  bDescriptorType; String-descriptor */
339         'I',                    /*  __u8  Unicode */
340         0,                      /*  __u8  Unicode */
341         'S',                    /*  __u8  Unicode */
342         0,                      /*  __u8  Unicode */
343         'P',                    /*  __u8  Unicode */
344         0,                      /*  __u8  Unicode */
345         '1',                    /*  __u8  Unicode */
346         0,                      /*  __u8  Unicode */
347         '1',                    /*  __u8  Unicode */
348         0,                      /*  __u8  Unicode */
349         '6',                    /*  __u8  Unicode */
350         0,                      /*  __u8  Unicode */
351         'x',                    /*  __u8  Unicode */
352         0,                      /*  __u8  Unicode */
353         ' ',                    /*  __u8  Unicode */
354         0,                      /*  __u8  Unicode */
355         'R',                    /*  __u8  Unicode */
356         0,                      /*  __u8  Unicode */
357         'o',                    /*  __u8  Unicode */
358         0,                      /*  __u8  Unicode */
359         'o',                    /*  __u8  Unicode */
360         0,                      /*  __u8  Unicode */
361         't',                    /*  __u8  Unicode */
362         0,                      /*  __u8  Unicode */
363         ' ',                    /*  __u8  Unicode */
364         0,                      /*  __u8  Unicode */
365         'H',                    /*  __u8  Unicode */
366         0,                      /*  __u8  Unicode */
367         'u',                    /*  __u8  Unicode */
368         0,                      /*  __u8  Unicode */
369         'b',                    /*  __u8  Unicode */
370         0,                      /*  __u8  Unicode */
371 };
372
373 /*
374  * Hub class-specific descriptor is constructed dynamically
375  */
376
377 /* --- Virtual root hub management functions ------------------------------- */
378
379 static int rh_check_port_status(struct isp116x *isp116x)
380 {
381         u32 temp, ndp, i;
382         int res;
383
384         res = -1;
385         temp = isp116x_read_reg32(isp116x, HCRHSTATUS);
386         ndp = (temp & RH_A_NDP);
387         for (i = 0; i < ndp; i++) {
388                 temp = isp116x_read_reg32(isp116x, HCRHPORT1 + i);
389                 /* check for a device disconnect */
390                 if (((temp & (RH_PS_PESC | RH_PS_CSC)) ==
391                      (RH_PS_PESC | RH_PS_CSC)) && ((temp & RH_PS_CCS) == 0)) {
392                         res = i;
393                         break;
394                 }
395         }
396         return res;
397 }
398
399 /* --- HC management functions --------------------------------------------- */
400
401 /* Write len bytes to fifo, pad till 32-bit boundary
402  */
403 static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
404 {
405         u8 *dp = (u8 *) buf;
406         u16 *dp2 = (u16 *) buf;
407         u16 w;
408         int quot = len % 4;
409
410         if ((unsigned long)dp2 & 1) {
411                 /* not aligned */
412                 for (; len > 1; len -= 2) {
413                         w = *dp++;
414                         w |= *dp++ << 8;
415                         isp116x_raw_write_data16(isp116x, w);
416                 }
417                 if (len)
418                         isp116x_write_data16(isp116x, (u16) * dp);
419         } else {
420                 /* aligned */
421                 for (; len > 1; len -= 2)
422                         isp116x_raw_write_data16(isp116x, *dp2++);
423                 if (len)
424                         isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
425         }
426         if (quot == 1 || quot == 2)
427                 isp116x_raw_write_data16(isp116x, 0);
428 }
429
430 /* Read len bytes from fifo and then read till 32-bit boundary
431  */
432 static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
433 {
434         u8 *dp = (u8 *) buf;
435         u16 *dp2 = (u16 *) buf;
436         u16 w;
437         int quot = len % 4;
438
439         if ((unsigned long)dp2 & 1) {
440                 /* not aligned */
441                 for (; len > 1; len -= 2) {
442                         w = isp116x_raw_read_data16(isp116x);
443                         *dp++ = w & 0xff;
444                         *dp++ = (w >> 8) & 0xff;
445                 }
446                 if (len)
447                         *dp = 0xff & isp116x_read_data16(isp116x);
448         } else {
449                 /* aligned */
450                 for (; len > 1; len -= 2)
451                         *dp2++ = isp116x_raw_read_data16(isp116x);
452                 if (len)
453                         *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
454         }
455         if (quot == 1 || quot == 2)
456                 isp116x_raw_read_data16(isp116x);
457 }
458
459 /* Write PTD's and data for scheduled transfers into the fifo ram.
460  * Fifo must be empty and ready */
461 static void pack_fifo(struct isp116x *isp116x, struct usb_device *dev,
462                       unsigned long pipe, struct ptd *ptd, int n, void *data,
463                       int len)
464 {
465         int buflen = n * sizeof(struct ptd) + len;
466         int i, done;
467
468         DBG("--- pack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
469
470         isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
471         isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
472         isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
473
474         done = 0;
475         for (i = 0; i < n; i++) {
476                 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
477
478                 dump_ptd(&ptd[i]);
479                 isp116x_write_data16(isp116x, ptd[i].count);
480                 isp116x_write_data16(isp116x, ptd[i].mps);
481                 isp116x_write_data16(isp116x, ptd[i].len);
482                 isp116x_write_data16(isp116x, ptd[i].faddr);
483
484                 dump_ptd_data(&ptd[i], (__u8 *) data + done, 0);
485                 write_ptddata_to_fifo(isp116x,
486                                       (__u8 *) data + done,
487                                       PTD_GET_LEN(&ptd[i]));
488
489                 done += PTD_GET_LEN(&ptd[i]);
490         }
491 }
492
493 /* Read the processed PTD's and data from fifo ram back to URBs' buffers.
494  * Fifo must be full and done */
495 static int unpack_fifo(struct isp116x *isp116x, struct usb_device *dev,
496                        unsigned long pipe, struct ptd *ptd, int n, void *data,
497                        int len)
498 {
499         int buflen = n * sizeof(struct ptd) + len;
500         int i, done, cc, ret;
501
502         isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
503         isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
504         isp116x_write_addr(isp116x, HCATLPORT);
505
506         ret = TD_CC_NOERROR;
507         done = 0;
508         for (i = 0; i < n; i++) {
509                 DBG("i=%d - done=%d - len=%d", i, done, PTD_GET_LEN(&ptd[i]));
510
511                 ptd[i].count = isp116x_read_data16(isp116x);
512                 ptd[i].mps = isp116x_read_data16(isp116x);
513                 ptd[i].len = isp116x_read_data16(isp116x);
514                 ptd[i].faddr = isp116x_read_data16(isp116x);
515                 dump_ptd(&ptd[i]);
516
517                 read_ptddata_from_fifo(isp116x,
518                                        (__u8 *) data + done,
519                                        PTD_GET_LEN(&ptd[i]));
520                 dump_ptd_data(&ptd[i], (__u8 *) data + done, 1);
521
522                 done += PTD_GET_LEN(&ptd[i]);
523
524                 cc = PTD_GET_CC(&ptd[i]);
525
526                 /* Data underrun means basically that we had more buffer space than
527                  * the function had data. It is perfectly normal but upper levels have
528                  * to know how much we actually transferred.
529                  */
530                 if (cc == TD_NOTACCESSED ||
531                                 (cc != TD_CC_NOERROR && (ret == TD_CC_NOERROR || ret == TD_DATAUNDERRUN)))
532                         ret = cc;
533         }
534
535         DBG("--- unpack buffer %p - %d bytes (fifo %d) ---", data, len, buflen);
536
537         return ret;
538 }
539
540 /* Interrupt handling
541  */
542 static int isp116x_interrupt(struct isp116x *isp116x)
543 {
544         u16 irqstat;
545         u32 intstat;
546         int ret = 0;
547
548         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
549         irqstat = isp116x_read_reg16(isp116x, HCuPINT);
550         isp116x_write_reg16(isp116x, HCuPINT, irqstat);
551         DBG(">>>>>> irqstat %x <<<<<<", irqstat);
552
553         if (irqstat & HCuPINT_ATL) {
554                 DBG(">>>>>> HCuPINT_ATL <<<<<<");
555                 udelay(500);
556                 ret = 1;
557         }
558
559         if (irqstat & HCuPINT_OPR) {
560                 intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
561                 isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
562                 DBG(">>>>>> HCuPINT_OPR %x <<<<<<", intstat);
563
564                 if (intstat & HCINT_UE) {
565                         ERR("unrecoverable error, controller disabled");
566
567                         /* FIXME: be optimistic, hope that bug won't repeat
568                          * often. Make some non-interrupt context restart the
569                          * controller. Count and limit the retries though;
570                          * either hardware or software errors can go forever...
571                          */
572                         isp116x_reset(isp116x);
573                         ret = -1;
574                         return -1;
575                 }
576
577                 if (intstat & HCINT_RHSC) {
578                         got_rhsc = 1;
579                         ret = 1;
580                         /* When root hub or any of its ports is going
581                            to come out of suspend, it may take more
582                            than 10ms for status bits to stabilize. */
583                         wait_ms(20);
584                 }
585
586                 if (intstat & HCINT_SO) {
587                         ERR("schedule overrun");
588                         ret = -1;
589                 }
590
591                 irqstat &= ~HCuPINT_OPR;
592         }
593
594         return ret;
595 }
596
597 /* With one PTD we can transfer almost 1K in one go;
598  * HC does the splitting into endpoint digestible transactions
599  */
600 struct ptd ptd[1];
601
602 static inline int max_transfer_len(struct usb_device *dev, unsigned long pipe)
603 {
604         unsigned mpck = usb_maxpacket(dev, pipe);
605
606         /* One PTD can transfer 1023 bytes but try to always
607          * transfer multiples of endpoint buffer size
608          */
609         return 1023 / mpck * mpck;
610 }
611
612 /* Do an USB transfer
613  */
614 static int isp116x_submit_job(struct usb_device *dev, unsigned long pipe,
615                               int dir, void *buffer, int len)
616 {
617         struct isp116x *isp116x = &isp116x_dev;
618         int type = usb_pipetype(pipe);
619         int epnum = usb_pipeendpoint(pipe);
620         int max = usb_maxpacket(dev, pipe);
621         int dir_out = usb_pipeout(pipe);
622         int speed_low = usb_pipeslow(pipe);
623         int i, done = 0, stat, timeout, cc;
624
625         /* 500 frames or 0.5s timeout when function is busy and NAKs transactions for a while */
626         int retries = 500;
627
628         DBG("------------------------------------------------");
629         dump_msg(dev, pipe, buffer, len, "SUBMIT");
630         DBG("------------------------------------------------");
631
632         if (len >= 1024) {
633                 ERR("Too big job");
634                 dev->status = USB_ST_CRC_ERR;
635                 return -1;
636         }
637
638         if (isp116x->disabled) {
639                 ERR("EPIPE");
640                 dev->status = USB_ST_CRC_ERR;
641                 return -1;
642         }
643
644         /* device pulled? Shortcut the action. */
645         if (devgone == dev) {
646                 ERR("ENODEV");
647                 dev->status = USB_ST_CRC_ERR;
648                 return USB_ST_CRC_ERR;
649         }
650
651         if (!max) {
652                 ERR("pipesize for pipe %lx is zero", pipe);
653                 dev->status = USB_ST_CRC_ERR;
654                 return -1;
655         }
656
657         if (type == PIPE_ISOCHRONOUS) {
658                 ERR("isochronous transfers not supported");
659                 dev->status = USB_ST_CRC_ERR;
660                 return -1;
661         }
662
663         /* FIFO not empty? */
664         if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL) {
665                 ERR("****** FIFO not empty! ******");
666                 dev->status = USB_ST_BUF_ERR;
667                 return -1;
668         }
669
670       retry:
671         isp116x_write_reg32(isp116x, HCINTSTAT, 0xff);
672
673         /* Prepare the PTD data */
674         ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK |
675                 PTD_TOGGLE(usb_gettoggle(dev, epnum, dir_out));
676         ptd->mps = PTD_MPS(max) | PTD_SPD(speed_low) | PTD_EP(epnum) | PTD_LAST_MSK;
677         ptd->len = PTD_LEN(len) | PTD_DIR(dir);
678         ptd->faddr = PTD_FA(usb_pipedevice(pipe));
679
680 retry_same:
681         /* Pack data into FIFO ram */
682         pack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
683 #ifdef EXTRA_DELAY
684         wait_ms(EXTRA_DELAY);
685 #endif
686
687         /* Start the data transfer */
688
689         /* Allow more time for a BULK device to react - some are slow */
690         if (usb_pipetype(pipe) == PIPE_BULK)
691                 timeout = 5000;
692         else
693                 timeout = 100;
694
695         /* Wait for it to complete */
696         for (;;) {
697                 /* Check whether the controller is done */
698                 stat = isp116x_interrupt(isp116x);
699
700                 if (stat < 0) {
701                         dev->status = USB_ST_CRC_ERR;
702                         break;
703                 }
704                 if (stat > 0)
705                         break;
706
707                 /* Check the timeout */
708                 if (--timeout)
709                         udelay(1);
710                 else {
711                         ERR("CTL:TIMEOUT ");
712                         stat = USB_ST_CRC_ERR;
713                         break;
714                 }
715         }
716
717         /* We got an Root Hub Status Change interrupt */
718         if (got_rhsc) {
719                 isp116x_show_regs(isp116x);
720
721                 got_rhsc = 0;
722
723                 /* Abuse timeout */
724                 timeout = rh_check_port_status(isp116x);
725                 if (timeout >= 0) {
726                         /*
727                          * FIXME! NOTE! AAAARGH!
728                          * This is potentially dangerous because it assumes
729                          * that only one device is ever plugged in!
730                          */
731                         devgone = dev;
732                 }
733         }
734
735         /* Ok, now we can read transfer status */
736
737         /* FIFO not ready? */
738         if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE)) {
739                 ERR("****** FIFO not ready! ******");
740                 dev->status = USB_ST_BUF_ERR;
741                 return -1;
742         }
743
744         /* Unpack data from FIFO ram */
745         cc = unpack_fifo(isp116x, dev, pipe, ptd, 1, buffer, len);
746
747         i = PTD_GET_COUNT(ptd);
748         done += i;
749         buffer += i;
750         len -= i;
751
752         /* There was some kind of real problem; Prepare the PTD again
753          * and retry from the failed transaction on
754          */
755         if (cc && cc != TD_NOTACCESSED && cc != TD_DATAUNDERRUN) {
756                 if (retries >= 100) {
757                         retries -= 100;
758                         /* The chip will have toggled the toggle bit for the failed
759                          * transaction too. We have to toggle it back.
760                          */
761                         usb_settoggle(dev, epnum, dir_out, !PTD_GET_TOGGLE(ptd));
762                         goto retry;
763                 }
764         }
765         /* "Normal" errors; TD_NOTACCESSED would mean in effect that the function have NAKed
766          * the transactions from the first on for the whole frame. It may be busy and we retry
767          * with the same PTD. PTD_ACTIVE (and not TD_NOTACCESSED) would mean that some of the
768          * PTD didn't make it because the function was busy or the frame ended before the PTD
769          * finished. We prepare the rest of the data and try again.
770          */
771         else if (cc == TD_NOTACCESSED || PTD_GET_ACTIVE(ptd) || (cc != TD_DATAUNDERRUN && PTD_GET_COUNT(ptd) < PTD_GET_LEN(ptd))) {
772                 if (retries) {
773                         --retries;
774                         if (cc == TD_NOTACCESSED && PTD_GET_ACTIVE(ptd) && !PTD_GET_COUNT(ptd)) goto retry_same;
775                         usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
776                         goto retry;
777                 }
778         }
779
780         if (cc != TD_CC_NOERROR && cc != TD_DATAUNDERRUN) {
781                 DBG("****** completition code error %x ******", cc);
782                 switch (cc) {
783                 case TD_CC_BITSTUFFING:
784                         dev->status = USB_ST_BIT_ERR;
785                         break;
786                 case TD_CC_STALL:
787                         dev->status = USB_ST_STALLED;
788                         break;
789                 case TD_BUFFEROVERRUN:
790                 case TD_BUFFERUNDERRUN:
791                         dev->status = USB_ST_BUF_ERR;
792                         break;
793                 default:
794                         dev->status = USB_ST_CRC_ERR;
795                 }
796                 return -cc;
797         }
798         else usb_settoggle(dev, epnum, dir_out, PTD_GET_TOGGLE(ptd));
799
800         dump_msg(dev, pipe, buffer, len, "SUBMIT(ret)");
801
802         dev->status = 0;
803         return done;
804 }
805
806 /* Adapted from au1x00_usb_ohci.c
807  */
808 static int isp116x_submit_rh_msg(struct usb_device *dev, unsigned long pipe,
809                                  void *buffer, int transfer_len,
810                                  struct devrequest *cmd)
811 {
812         struct isp116x *isp116x = &isp116x_dev;
813         u32 tmp = 0;
814
815         int leni = transfer_len;
816         int len = 0;
817         int stat = 0;
818         u32 datab[4];
819         u8 *data_buf = (u8 *) datab;
820         u16 bmRType_bReq;
821         u16 wValue;
822         u16 wIndex;
823         u16 wLength;
824
825         if ((pipe & PIPE_INTERRUPT) == PIPE_INTERRUPT) {
826                 INFO("Root-Hub submit IRQ: NOT implemented");
827                 return 0;
828         }
829
830         bmRType_bReq = cmd->requesttype | (cmd->request << 8);
831         wValue = swap_16(cmd->value);
832         wIndex = swap_16(cmd->index);
833         wLength = swap_16(cmd->length);
834
835         DBG("--- HUB ----------------------------------------");
836         DBG("submit rh urb, req=%x val=%#x index=%#x len=%d",
837             bmRType_bReq, wValue, wIndex, wLength);
838         dump_msg(dev, pipe, buffer, transfer_len, "RH");
839         DBG("------------------------------------------------");
840
841         switch (bmRType_bReq) {
842         case RH_GET_STATUS:
843                 DBG("RH_GET_STATUS");
844
845                 *(__u16 *) data_buf = swap_16(1);
846                 len = 2;
847                 break;
848
849         case RH_GET_STATUS | RH_INTERFACE:
850                 DBG("RH_GET_STATUS | RH_INTERFACE");
851
852                 *(__u16 *) data_buf = swap_16(0);
853                 len = 2;
854                 break;
855
856         case RH_GET_STATUS | RH_ENDPOINT:
857                 DBG("RH_GET_STATUS | RH_ENDPOINT");
858
859                 *(__u16 *) data_buf = swap_16(0);
860                 len = 2;
861                 break;
862
863         case RH_GET_STATUS | RH_CLASS:
864                 DBG("RH_GET_STATUS | RH_CLASS");
865
866                 tmp = isp116x_read_reg32(isp116x, HCRHSTATUS);
867
868                 *(__u32 *) data_buf = swap_32(tmp & ~(RH_HS_CRWE | RH_HS_DRWE));
869                 len = 4;
870                 break;
871
872         case RH_GET_STATUS | RH_OTHER | RH_CLASS:
873                 DBG("RH_GET_STATUS | RH_OTHER | RH_CLASS");
874
875                 tmp = isp116x_read_reg32(isp116x, HCRHPORT1 + wIndex - 1);
876                 *(__u32 *) data_buf = swap_32(tmp);
877                 isp116x_show_regs(isp116x);
878                 len = 4;
879                 break;
880
881         case RH_CLEAR_FEATURE | RH_ENDPOINT:
882                 DBG("RH_CLEAR_FEATURE | RH_ENDPOINT");
883
884                 switch (wValue) {
885                 case RH_ENDPOINT_STALL:
886                         DBG("C_HUB_ENDPOINT_STALL");
887                         len = 0;
888                         break;
889                 }
890                 break;
891
892         case RH_CLEAR_FEATURE | RH_CLASS:
893                 DBG("RH_CLEAR_FEATURE | RH_CLASS");
894
895                 switch (wValue) {
896                 case RH_C_HUB_LOCAL_POWER:
897                         DBG("C_HUB_LOCAL_POWER");
898                         len = 0;
899                         break;
900
901                 case RH_C_HUB_OVER_CURRENT:
902                         DBG("C_HUB_OVER_CURRENT");
903                         isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
904                         len = 0;
905                         break;
906                 }
907                 break;
908
909         case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
910                 DBG("RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS");
911
912                 switch (wValue) {
913                 case RH_PORT_ENABLE:
914                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
915                                             RH_PS_CCS);
916                         len = 0;
917                         break;
918
919                 case RH_PORT_SUSPEND:
920                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
921                                             RH_PS_POCI);
922                         len = 0;
923                         break;
924
925                 case RH_PORT_POWER:
926                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
927                                             RH_PS_LSDA);
928                         len = 0;
929                         break;
930
931                 case RH_C_PORT_CONNECTION:
932                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
933                                             RH_PS_CSC);
934                         len = 0;
935                         break;
936
937                 case RH_C_PORT_ENABLE:
938                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
939                                             RH_PS_PESC);
940                         len = 0;
941                         break;
942
943                 case RH_C_PORT_SUSPEND:
944                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
945                                             RH_PS_PSSC);
946                         len = 0;
947                         break;
948
949                 case RH_C_PORT_OVER_CURRENT:
950                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
951                                             RH_PS_POCI);
952                         len = 0;
953                         break;
954
955                 case RH_C_PORT_RESET:
956                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
957                                             RH_PS_PRSC);
958                         len = 0;
959                         break;
960
961                 default:
962                         ERR("invalid wValue");
963                         stat = USB_ST_STALLED;
964                 }
965
966                 isp116x_show_regs(isp116x);
967
968                 break;
969
970         case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
971                 DBG("RH_SET_FEATURE | RH_OTHER | RH_CLASS");
972
973                 switch (wValue) {
974                 case RH_PORT_SUSPEND:
975                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
976                                             RH_PS_PSS);
977                         len = 0;
978                         break;
979
980                 case RH_PORT_RESET:
981                         /* Spin until any current reset finishes */
982                         while (1) {
983                                 tmp =
984                                     isp116x_read_reg32(isp116x,
985                                                        HCRHPORT1 + wIndex - 1);
986                                 if (!(tmp & RH_PS_PRS))
987                                         break;
988                                 wait_ms(1);
989                         }
990                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
991                                             RH_PS_PRS);
992                         wait_ms(10);
993
994                         len = 0;
995                         break;
996
997                 case RH_PORT_POWER:
998                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
999                                             RH_PS_PPS);
1000                         len = 0;
1001                         break;
1002
1003                 case RH_PORT_ENABLE:
1004                         isp116x_write_reg32(isp116x, HCRHPORT1 + wIndex - 1,
1005                                             RH_PS_PES);
1006                         len = 0;
1007                         break;
1008
1009                 default:
1010                         ERR("invalid wValue");
1011                         stat = USB_ST_STALLED;
1012                 }
1013
1014                 isp116x_show_regs(isp116x);
1015
1016                 break;
1017
1018         case RH_SET_ADDRESS:
1019                 DBG("RH_SET_ADDRESS");
1020
1021                 rh_devnum = wValue;
1022                 len = 0;
1023                 break;
1024
1025         case RH_GET_DESCRIPTOR:
1026                 DBG("RH_GET_DESCRIPTOR: %x, %d", wValue, wLength);
1027
1028                 switch (wValue) {
1029                 case (USB_DT_DEVICE << 8):      /* device descriptor */
1030                         len = min_t(unsigned int,
1031                                     leni, min_t(unsigned int,
1032                                                 sizeof(root_hub_dev_des),
1033                                                 wLength));
1034                         data_buf = root_hub_dev_des;
1035                         break;
1036
1037                 case (USB_DT_CONFIG << 8):      /* configuration descriptor */
1038                         len = min_t(unsigned int,
1039                                     leni, min_t(unsigned int,
1040                                                 sizeof(root_hub_config_des),
1041                                                 wLength));
1042                         data_buf = root_hub_config_des;
1043                         break;
1044
1045                 case ((USB_DT_STRING << 8) | 0x00):     /* string 0 descriptors */
1046                         len = min_t(unsigned int,
1047                                     leni, min_t(unsigned int,
1048                                                 sizeof(root_hub_str_index0),
1049                                                 wLength));
1050                         data_buf = root_hub_str_index0;
1051                         break;
1052
1053                 case ((USB_DT_STRING << 8) | 0x01):     /* string 1 descriptors */
1054                         len = min_t(unsigned int,
1055                                     leni, min_t(unsigned int,
1056                                                 sizeof(root_hub_str_index1),
1057                                                 wLength));
1058                         data_buf = root_hub_str_index1;
1059                         break;
1060
1061                 default:
1062                         ERR("invalid wValue");
1063                         stat = USB_ST_STALLED;
1064                 }
1065
1066                 break;
1067
1068         case RH_GET_DESCRIPTOR | RH_CLASS:
1069                 DBG("RH_GET_DESCRIPTOR | RH_CLASS");
1070
1071                 tmp = isp116x_read_reg32(isp116x, HCRHDESCA);
1072
1073                 data_buf[0] = 0x09;     /* min length; */
1074                 data_buf[1] = 0x29;
1075                 data_buf[2] = tmp & RH_A_NDP;
1076                 data_buf[3] = 0;
1077                 if (tmp & RH_A_PSM)     /* per-port power switching? */
1078                         data_buf[3] |= 0x01;
1079                 if (tmp & RH_A_NOCP)    /* no overcurrent reporting? */
1080                         data_buf[3] |= 0x10;
1081                 else if (tmp & RH_A_OCPM)       /* per-port overcurrent rep? */
1082                         data_buf[3] |= 0x08;
1083
1084                 /* Corresponds to data_buf[4-7] */
1085                 datab[1] = 0;
1086                 data_buf[5] = (tmp & RH_A_POTPGT) >> 24;
1087
1088                 tmp = isp116x_read_reg32(isp116x, HCRHDESCB);
1089
1090                 data_buf[7] = tmp & RH_B_DR;
1091                 if (data_buf[2] < 7)
1092                         data_buf[8] = 0xff;
1093                 else {
1094                         data_buf[0] += 2;
1095                         data_buf[8] = (tmp & RH_B_DR) >> 8;
1096                         data_buf[10] = data_buf[9] = 0xff;
1097                 }
1098
1099                 len = min_t(unsigned int, leni,
1100                             min_t(unsigned int, data_buf[0], wLength));
1101                 break;
1102
1103         case RH_GET_CONFIGURATION:
1104                 DBG("RH_GET_CONFIGURATION");
1105
1106                 *(__u8 *) data_buf = 0x01;
1107                 len = 1;
1108                 break;
1109
1110         case RH_SET_CONFIGURATION:
1111                 DBG("RH_SET_CONFIGURATION");
1112
1113                 isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPSC);
1114                 len = 0;
1115                 break;
1116
1117         default:
1118                 ERR("*** *** *** unsupported root hub command *** *** ***");
1119                 stat = USB_ST_STALLED;
1120         }
1121
1122         len = min_t(int, len, leni);
1123         if (buffer != data_buf)
1124                 memcpy(buffer, data_buf, len);
1125
1126         dev->act_len = len;
1127         dev->status = stat;
1128         DBG("dev act_len %d, status %d", dev->act_len, dev->status);
1129
1130         dump_msg(dev, pipe, buffer, transfer_len, "RH(ret)");
1131
1132         return stat;
1133 }
1134
1135 /* --- Transfer functions -------------------------------------------------- */
1136
1137 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1138                    int len, int interval)
1139 {
1140         DBG("dev=%p pipe=%#lx buf=%p size=%d int=%d",
1141             dev, pipe, buffer, len, interval);
1142
1143         return -1;
1144 }
1145
1146 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1147                        int len, struct devrequest *setup)
1148 {
1149         int devnum = usb_pipedevice(pipe);
1150         int epnum = usb_pipeendpoint(pipe);
1151         int max = max_transfer_len(dev, pipe);
1152         int dir_in = usb_pipein(pipe);
1153         int done, ret;
1154
1155         /* Control message is for the HUB? */
1156         if (devnum == rh_devnum)
1157                 return isp116x_submit_rh_msg(dev, pipe, buffer, len, setup);
1158
1159         /* Ok, no HUB message so send the message to the device */
1160
1161         /* Setup phase */
1162         DBG("--- SETUP PHASE --------------------------------");
1163         usb_settoggle(dev, epnum, 1, 0);
1164         ret = isp116x_submit_job(dev, pipe,
1165                                  PTD_DIR_SETUP,
1166                                  setup, sizeof(struct devrequest));
1167         if (ret < 0) {
1168                 DBG("control setup phase error (ret = %d", ret);
1169                 return -1;
1170         }
1171
1172         /* Data phase */
1173         DBG("--- DATA PHASE ---------------------------------");
1174         done = 0;
1175         usb_settoggle(dev, epnum, !dir_in, 1);
1176         while (done < len) {
1177                 ret = isp116x_submit_job(dev, pipe,
1178                                          dir_in ? PTD_DIR_IN : PTD_DIR_OUT,
1179                                          (__u8 *) buffer + done,
1180                                          max > len - done ? len - done : max);
1181                 if (ret < 0) {
1182                         DBG("control data phase error (ret = %d)", ret);
1183                         return -1;
1184                 }
1185                 done += ret;
1186
1187                 if (dir_in && ret < max)        /* short packet */
1188                         break;
1189         }
1190
1191         /* Status phase */
1192         DBG("--- STATUS PHASE -------------------------------");
1193         usb_settoggle(dev, epnum, !dir_in, 1);
1194         ret = isp116x_submit_job(dev, pipe,
1195                                  !dir_in ? PTD_DIR_IN : PTD_DIR_OUT, NULL, 0);
1196         if (ret < 0) {
1197                 DBG("control status phase error (ret = %d", ret);
1198                 return -1;
1199         }
1200
1201         dev->act_len = done;
1202
1203         dump_msg(dev, pipe, buffer, len, "DEV(ret)");
1204
1205         return done;
1206 }
1207
1208 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1209                     int len)
1210 {
1211         int dir_out = usb_pipeout(pipe);
1212         int max = max_transfer_len(dev, pipe);
1213         int done, ret;
1214
1215         DBG("--- BULK ---------------------------------------");
1216         DBG("dev=%ld pipe=%ld buf=%p size=%d dir_out=%d",
1217             usb_pipedevice(pipe), usb_pipeendpoint(pipe), buffer, len, dir_out);
1218
1219         done = 0;
1220         while (done < len) {
1221                 ret = isp116x_submit_job(dev, pipe,
1222                                          !dir_out ? PTD_DIR_IN : PTD_DIR_OUT,
1223                                          (__u8 *) buffer + done,
1224                                          max > len - done ? len - done : max);
1225                 if (ret < 0) {
1226                         DBG("error on bulk message (ret = %d)", ret);
1227                         return -1;
1228                 }
1229
1230                 done += ret;
1231
1232                 if (!dir_out && ret < max)      /* short packet */
1233                         break;
1234         }
1235
1236         dev->act_len = done;
1237
1238         return 0;
1239 }
1240
1241 /* --- Basic functions ----------------------------------------------------- */
1242
1243 static int isp116x_sw_reset(struct isp116x *isp116x)
1244 {
1245         int retries = 15;
1246         int ret = 0;
1247
1248         DBG("");
1249
1250         isp116x->disabled = 1;
1251
1252         isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1253         isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1254         while (--retries) {
1255                 /* It usually resets within 1 ms */
1256                 wait_ms(1);
1257                 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1258                         break;
1259         }
1260         if (!retries) {
1261                 ERR("software reset timeout");
1262                 ret = -1;
1263         }
1264         return ret;
1265 }
1266
1267 static int isp116x_reset(struct isp116x *isp116x)
1268 {
1269         unsigned long t;
1270         u16 clkrdy = 0;
1271         int ret, timeout = 15 /* ms */ ;
1272
1273         DBG("");
1274
1275         ret = isp116x_sw_reset(isp116x);
1276         if (ret)
1277                 return ret;
1278
1279         for (t = 0; t < timeout; t++) {
1280                 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
1281                 if (clkrdy)
1282                         break;
1283                 wait_ms(1);
1284         }
1285         if (!clkrdy) {
1286                 ERR("clock not ready after %dms", timeout);
1287                 /* After sw_reset the clock won't report to be ready, if
1288                    H_WAKEUP pin is high. */
1289                 ERR("please make sure that the H_WAKEUP pin is pulled low!");
1290                 ret = -1;
1291         }
1292         return ret;
1293 }
1294
1295 static void isp116x_stop(struct isp116x *isp116x)
1296 {
1297         u32 val;
1298
1299         DBG("");
1300
1301         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1302
1303         /* Switch off ports' power, some devices don't come up
1304            after next 'start' without this */
1305         val = isp116x_read_reg32(isp116x, HCRHDESCA);
1306         val &= ~(RH_A_NPS | RH_A_PSM);
1307         isp116x_write_reg32(isp116x, HCRHDESCA, val);
1308         isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
1309
1310         isp116x_sw_reset(isp116x);
1311 }
1312
1313 /*
1314  *  Configure the chip. The chip must be successfully reset by now.
1315  */
1316 static int isp116x_start(struct isp116x *isp116x)
1317 {
1318         struct isp116x_platform_data *board = isp116x->board;
1319         u32 val;
1320
1321         DBG("");
1322
1323         /* Clear interrupt status and disable all interrupt sources */
1324         isp116x_write_reg16(isp116x, HCuPINT, 0xff);
1325         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1326
1327         isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
1328         isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
1329
1330         /* Hardware configuration */
1331         val = HCHWCFG_DBWIDTH(1);
1332         if (board->sel15Kres)
1333                 val |= HCHWCFG_15KRSEL;
1334         /* Remote wakeup won't work without working clock */
1335         if (board->remote_wakeup_enable)
1336                 val |= HCHWCFG_CLKNOTSTOP;
1337         if (board->oc_enable)
1338                 val |= HCHWCFG_ANALOG_OC;
1339         isp116x_write_reg16(isp116x, HCHWCFG, val);
1340
1341         /* --- Root hub configuration */
1342         val = (25 << 24) & RH_A_POTPGT;
1343         /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
1344            be always set. Yet, instead, we request individual port
1345            power switching. */
1346         val |= RH_A_PSM;
1347         /* Report overcurrent per port */
1348         val |= RH_A_OCPM;
1349         isp116x_write_reg32(isp116x, HCRHDESCA, val);
1350         isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1351
1352         val = RH_B_PPCM;
1353         isp116x_write_reg32(isp116x, HCRHDESCB, val);
1354         isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1355
1356         val = 0;
1357         if (board->remote_wakeup_enable)
1358                 val |= RH_HS_DRWE;
1359         isp116x_write_reg32(isp116x, HCRHSTATUS, val);
1360         isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
1361
1362         isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
1363
1364         /* Go operational */
1365         val = HCCONTROL_USB_OPER;
1366         if (board->remote_wakeup_enable)
1367                 val |= HCCONTROL_RWE;
1368         isp116x_write_reg32(isp116x, HCCONTROL, val);
1369
1370         /* Disable ports to avoid race in device enumeration */
1371         isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
1372         isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
1373
1374         isp116x_show_regs(isp116x);
1375
1376         isp116x->disabled = 0;
1377
1378         return 0;
1379 }
1380
1381 /* --- Init functions ------------------------------------------------------ */
1382
1383 int isp116x_check_id(struct isp116x *isp116x)
1384 {
1385         int val;
1386
1387         val = isp116x_read_reg16(isp116x, HCCHIPID);
1388         if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1389                 ERR("invalid chip ID %04x", val);
1390                 return -1;
1391         }
1392
1393         return 0;
1394 }
1395
1396 int usb_lowlevel_init(void)
1397 {
1398         struct isp116x *isp116x = &isp116x_dev;
1399
1400         DBG("");
1401
1402         got_rhsc = rh_devnum = 0;
1403
1404         /* Init device registers addr */
1405         isp116x->addr_reg = (u16 *) ISP116X_HCD_ADDR;
1406         isp116x->data_reg = (u16 *) ISP116X_HCD_DATA;
1407
1408         /* Setup specific board settings */
1409 #ifdef ISP116X_HCD_SEL15kRES
1410         isp116x_board.sel15Kres = 1;
1411 #endif
1412 #ifdef ISP116X_HCD_OC_ENABLE
1413         isp116x_board.oc_enable = 1;
1414 #endif
1415 #ifdef ISP116X_HCD_REMOTE_WAKEUP_ENABLE
1416         isp116x_board.remote_wakeup_enable = 1;
1417 #endif
1418         isp116x->board = &isp116x_board;
1419
1420         /* Try to get ISP116x silicon chip ID */
1421         if (isp116x_check_id(isp116x) < 0)
1422                 return -1;
1423
1424         isp116x->disabled = 1;
1425         isp116x->sleeping = 0;
1426
1427         isp116x_reset(isp116x);
1428         isp116x_start(isp116x);
1429
1430         return 0;
1431 }
1432
1433 int usb_lowlevel_stop(void)
1434 {
1435         struct isp116x *isp116x = &isp116x_dev;
1436
1437         DBG("");
1438
1439         if (!isp116x->disabled)
1440                 isp116x_stop(isp116x);
1441
1442         return 0;
1443 }
1444
1445 #endif                          /* CONFIG_USB_ISP116X_HCD */