[PATCH] USB: small cleanups
[linux-2.6] / drivers / usb / host / isp116x-hcd.c
1 /*
2  * ISP116x HCD (Host Controller Driver) for USB.
3  *
4  * Derived from the SL811 HCD, rewritten for ISP116x.
5  * Copyright (C) 2005 Olav Kongas <ok@artecdesign.ee>
6  *
7  * Portions:
8  * Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
9  * Copyright (C) 2004 David Brownell
10  *
11  * Periodic scheduling is based on Roman's OHCI code
12  * Copyright (C) 1999 Roman Weissgaerber
13  *
14  */
15
16 /*
17  * The driver basically works. A number of people have used it with a range
18  * of devices.
19  *
20  * The driver passes all usbtests 1-14.
21  *
22  * Suspending/resuming of root hub via sysfs works. Remote wakeup works too.
23  * And suspending/resuming of platform device works too. Suspend/resume
24  * via HCD operations vector is not implemented.
25  *
26  * Iso transfer support is not implemented. Adding this would include
27  * implementing recovery from the failure to service the processed ITL
28  * fifo ram in time, which will involve chip reset.
29  *
30  * TODO:
31  + More testing of suspend/resume.
32 */
33
34 /*
35   ISP116x chips require certain delays between accesses to its
36   registers. The following timing options exist.
37
38   1. Configure your memory controller (the best)
39   2. Implement platform-specific delay function possibly
40   combined with configuring the memory controller; see
41   include/linux/usb-isp116x.h for more info. Some broken
42   memory controllers line LH7A400 SMC need this. Also,
43   uncomment for that to work the following
44   USE_PLATFORM_DELAY macro.
45   3. Use ndelay (easiest, poorest). For that, uncomment
46   the following USE_NDELAY macro.
47 */
48 #define USE_PLATFORM_DELAY
49 //#define USE_NDELAY
50
51 //#define DEBUG
52 //#define VERBOSE
53 /* Transfer descriptors. See dump_ptd() for printout format  */
54 //#define PTD_TRACE
55 /* enqueuing/finishing log of urbs */
56 //#define URB_TRACE
57
58 #include <linux/module.h>
59 #include <linux/delay.h>
60 #include <linux/debugfs.h>
61 #include <linux/seq_file.h>
62 #include <linux/errno.h>
63 #include <linux/init.h>
64 #include <linux/list.h>
65 #include <linux/usb.h>
66 #include <linux/usb_isp116x.h>
67 #include <linux/platform_device.h>
68
69 #include <asm/io.h>
70 #include <asm/irq.h>
71 #include <asm/system.h>
72 #include <asm/byteorder.h>
73
74 #include "../core/hcd.h"
75 #include "isp116x.h"
76
77 #define DRIVER_VERSION  "03 Nov 2005"
78 #define DRIVER_DESC     "ISP116x USB Host Controller Driver"
79
80 MODULE_DESCRIPTION(DRIVER_DESC);
81 MODULE_LICENSE("GPL");
82
83 static const char hcd_name[] = "isp116x-hcd";
84
85 /*-----------------------------------------------------------------*/
86
87 /*
88   Write len bytes to fifo, pad till 32-bit boundary
89  */
90 static void write_ptddata_to_fifo(struct isp116x *isp116x, void *buf, int len)
91 {
92         u8 *dp = (u8 *) buf;
93         u16 *dp2 = (u16 *) buf;
94         u16 w;
95         int quot = len % 4;
96
97         if ((unsigned long)dp2 & 1) {
98                 /* not aligned */
99                 for (; len > 1; len -= 2) {
100                         w = *dp++;
101                         w |= *dp++ << 8;
102                         isp116x_raw_write_data16(isp116x, w);
103                 }
104                 if (len)
105                         isp116x_write_data16(isp116x, (u16) * dp);
106         } else {
107                 /* aligned */
108                 for (; len > 1; len -= 2)
109                         isp116x_raw_write_data16(isp116x, *dp2++);
110                 if (len)
111                         isp116x_write_data16(isp116x, 0xff & *((u8 *) dp2));
112         }
113         if (quot == 1 || quot == 2)
114                 isp116x_raw_write_data16(isp116x, 0);
115 }
116
117 /*
118   Read len bytes from fifo and then read till 32-bit boundary.
119  */
120 static void read_ptddata_from_fifo(struct isp116x *isp116x, void *buf, int len)
121 {
122         u8 *dp = (u8 *) buf;
123         u16 *dp2 = (u16 *) buf;
124         u16 w;
125         int quot = len % 4;
126
127         if ((unsigned long)dp2 & 1) {
128                 /* not aligned */
129                 for (; len > 1; len -= 2) {
130                         w = isp116x_raw_read_data16(isp116x);
131                         *dp++ = w & 0xff;
132                         *dp++ = (w >> 8) & 0xff;
133                 }
134                 if (len)
135                         *dp = 0xff & isp116x_read_data16(isp116x);
136         } else {
137                 /* aligned */
138                 for (; len > 1; len -= 2)
139                         *dp2++ = isp116x_raw_read_data16(isp116x);
140                 if (len)
141                         *(u8 *) dp2 = 0xff & isp116x_read_data16(isp116x);
142         }
143         if (quot == 1 || quot == 2)
144                 isp116x_raw_read_data16(isp116x);
145 }
146
147 /*
148   Write ptd's and data for scheduled transfers into
149   the fifo ram. Fifo must be empty and ready.
150 */
151 static void pack_fifo(struct isp116x *isp116x)
152 {
153         struct isp116x_ep *ep;
154         struct ptd *ptd;
155         int buflen = isp116x->atl_last_dir == PTD_DIR_IN
156             ? isp116x->atl_bufshrt : isp116x->atl_buflen;
157         int ptd_count = 0;
158
159         isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
160         isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
161         isp116x_write_addr(isp116x, HCATLPORT | ISP116x_WRITE_OFFSET);
162         for (ep = isp116x->atl_active; ep; ep = ep->active) {
163                 ++ptd_count;
164                 ptd = &ep->ptd;
165                 dump_ptd(ptd);
166                 dump_ptd_out_data(ptd, ep->data);
167                 isp116x_write_data16(isp116x, ptd->count);
168                 isp116x_write_data16(isp116x, ptd->mps);
169                 isp116x_write_data16(isp116x, ptd->len);
170                 isp116x_write_data16(isp116x, ptd->faddr);
171                 buflen -= sizeof(struct ptd);
172                 /* Skip writing data for last IN PTD */
173                 if (ep->active || (isp116x->atl_last_dir != PTD_DIR_IN)) {
174                         write_ptddata_to_fifo(isp116x, ep->data, ep->length);
175                         buflen -= ALIGN(ep->length, 4);
176                 }
177         }
178         BUG_ON(buflen);
179 }
180
181 /*
182   Read the processed ptd's and data from fifo ram back to
183   URBs' buffers. Fifo must be full and done
184 */
185 static void unpack_fifo(struct isp116x *isp116x)
186 {
187         struct isp116x_ep *ep;
188         struct ptd *ptd;
189         int buflen = isp116x->atl_last_dir == PTD_DIR_IN
190             ? isp116x->atl_buflen : isp116x->atl_bufshrt;
191
192         isp116x_write_reg16(isp116x, HCuPINT, HCuPINT_AIIEOT);
193         isp116x_write_reg16(isp116x, HCXFERCTR, buflen);
194         isp116x_write_addr(isp116x, HCATLPORT);
195         for (ep = isp116x->atl_active; ep; ep = ep->active) {
196                 ptd = &ep->ptd;
197                 ptd->count = isp116x_read_data16(isp116x);
198                 ptd->mps = isp116x_read_data16(isp116x);
199                 ptd->len = isp116x_read_data16(isp116x);
200                 ptd->faddr = isp116x_read_data16(isp116x);
201                 buflen -= sizeof(struct ptd);
202                 /* Skip reading data for last Setup or Out PTD */
203                 if (ep->active || (isp116x->atl_last_dir == PTD_DIR_IN)) {
204                         read_ptddata_from_fifo(isp116x, ep->data, ep->length);
205                         buflen -= ALIGN(ep->length, 4);
206                 }
207                 dump_ptd(ptd);
208                 dump_ptd_in_data(ptd, ep->data);
209         }
210         BUG_ON(buflen);
211 }
212
213 /*---------------------------------------------------------------*/
214
215 /*
216   Set up PTD's.
217 */
218 static void preproc_atl_queue(struct isp116x *isp116x)
219 {
220         struct isp116x_ep *ep;
221         struct urb *urb;
222         struct ptd *ptd;
223         u16 len;
224
225         for (ep = isp116x->atl_active; ep; ep = ep->active) {
226                 u16 toggle = 0, dir = PTD_DIR_SETUP;
227
228                 BUG_ON(list_empty(&ep->hep->urb_list));
229                 urb = container_of(ep->hep->urb_list.next,
230                                    struct urb, urb_list);
231                 ptd = &ep->ptd;
232                 len = ep->length;
233                 spin_lock(&urb->lock);
234                 ep->data = (unsigned char *)urb->transfer_buffer
235                     + urb->actual_length;
236
237                 switch (ep->nextpid) {
238                 case USB_PID_IN:
239                         toggle = usb_gettoggle(urb->dev, ep->epnum, 0);
240                         dir = PTD_DIR_IN;
241                         break;
242                 case USB_PID_OUT:
243                         toggle = usb_gettoggle(urb->dev, ep->epnum, 1);
244                         dir = PTD_DIR_OUT;
245                         break;
246                 case USB_PID_SETUP:
247                         len = sizeof(struct usb_ctrlrequest);
248                         ep->data = urb->setup_packet;
249                         break;
250                 case USB_PID_ACK:
251                         toggle = 1;
252                         len = 0;
253                         dir = (urb->transfer_buffer_length
254                                && usb_pipein(urb->pipe))
255                             ? PTD_DIR_OUT : PTD_DIR_IN;
256                         break;
257                 default:
258                         ERR("%s %d: ep->nextpid %d\n", __func__, __LINE__,
259                             ep->nextpid);
260                         BUG();
261                 }
262
263                 ptd->count = PTD_CC_MSK | PTD_ACTIVE_MSK | PTD_TOGGLE(toggle);
264                 ptd->mps = PTD_MPS(ep->maxpacket)
265                     | PTD_SPD(urb->dev->speed == USB_SPEED_LOW)
266                     | PTD_EP(ep->epnum);
267                 ptd->len = PTD_LEN(len) | PTD_DIR(dir);
268                 ptd->faddr = PTD_FA(usb_pipedevice(urb->pipe));
269                 spin_unlock(&urb->lock);
270                 if (!ep->active) {
271                         ptd->mps |= PTD_LAST_MSK;
272                         isp116x->atl_last_dir = dir;
273                 }
274                 isp116x->atl_bufshrt = sizeof(struct ptd) + isp116x->atl_buflen;
275                 isp116x->atl_buflen = isp116x->atl_bufshrt + ALIGN(len, 4);
276         }
277 }
278
279 /*
280   Analyze transfer results, handle partial transfers and errors
281 */
282 static void postproc_atl_queue(struct isp116x *isp116x)
283 {
284         struct isp116x_ep *ep;
285         struct urb *urb;
286         struct usb_device *udev;
287         struct ptd *ptd;
288         int short_not_ok;
289         u8 cc;
290
291         for (ep = isp116x->atl_active; ep; ep = ep->active) {
292                 BUG_ON(list_empty(&ep->hep->urb_list));
293                 urb =
294                     container_of(ep->hep->urb_list.next, struct urb, urb_list);
295                 udev = urb->dev;
296                 ptd = &ep->ptd;
297                 cc = PTD_GET_CC(ptd);
298                 short_not_ok = 1;
299                 spin_lock(&urb->lock);
300
301                 /* Data underrun is special. For allowed underrun
302                    we clear the error and continue as normal. For
303                    forbidden underrun we finish the DATA stage
304                    immediately while for control transfer,
305                    we do a STATUS stage. */
306                 if (cc == TD_DATAUNDERRUN) {
307                         if (!(urb->transfer_flags & URB_SHORT_NOT_OK)) {
308                                 DBG("Allowed data underrun\n");
309                                 cc = TD_CC_NOERROR;
310                                 short_not_ok = 0;
311                         } else {
312                                 ep->error_count = 1;
313                                 if (usb_pipecontrol(urb->pipe))
314                                         ep->nextpid = USB_PID_ACK;
315                                 else
316                                         usb_settoggle(udev, ep->epnum,
317                                                       ep->nextpid ==
318                                                       USB_PID_OUT,
319                                                       PTD_GET_TOGGLE(ptd));
320                                 urb->actual_length += PTD_GET_COUNT(ptd);
321                                 urb->status = cc_to_error[TD_DATAUNDERRUN];
322                                 spin_unlock(&urb->lock);
323                                 continue;
324                         }
325                 }
326                 /* Keep underrun error through the STATUS stage */
327                 if (urb->status == cc_to_error[TD_DATAUNDERRUN])
328                         cc = TD_DATAUNDERRUN;
329
330                 if (cc != TD_CC_NOERROR && cc != TD_NOTACCESSED
331                     && (++ep->error_count >= 3 || cc == TD_CC_STALL
332                         || cc == TD_DATAOVERRUN)) {
333                         if (urb->status == -EINPROGRESS)
334                                 urb->status = cc_to_error[cc];
335                         if (ep->nextpid == USB_PID_ACK)
336                                 ep->nextpid = 0;
337                         spin_unlock(&urb->lock);
338                         continue;
339                 }
340                 /* According to usb spec, zero-length Int transfer signals
341                    finishing of the urb. Hey, does this apply only
342                    for IN endpoints? */
343                 if (usb_pipeint(urb->pipe) && !PTD_GET_LEN(ptd)) {
344                         if (urb->status == -EINPROGRESS)
345                                 urb->status = 0;
346                         spin_unlock(&urb->lock);
347                         continue;
348                 }
349
350                 /* Relax after previously failed, but later succeeded
351                    or correctly NAK'ed retransmission attempt */
352                 if (ep->error_count
353                     && (cc == TD_CC_NOERROR || cc == TD_NOTACCESSED))
354                         ep->error_count = 0;
355
356                 /* Take into account idiosyncracies of the isp116x chip
357                    regarding toggle bit for failed transfers */
358                 if (ep->nextpid == USB_PID_OUT)
359                         usb_settoggle(udev, ep->epnum, 1, PTD_GET_TOGGLE(ptd)
360                                       ^ (ep->error_count > 0));
361                 else if (ep->nextpid == USB_PID_IN)
362                         usb_settoggle(udev, ep->epnum, 0, PTD_GET_TOGGLE(ptd)
363                                       ^ (ep->error_count > 0));
364
365                 switch (ep->nextpid) {
366                 case USB_PID_IN:
367                 case USB_PID_OUT:
368                         urb->actual_length += PTD_GET_COUNT(ptd);
369                         if (PTD_GET_ACTIVE(ptd)
370                             || (cc != TD_CC_NOERROR && cc < 0x0E))
371                                 break;
372                         if (urb->transfer_buffer_length != urb->actual_length) {
373                                 if (short_not_ok)
374                                         break;
375                         } else {
376                                 if (urb->transfer_flags & URB_ZERO_PACKET
377                                     && ep->nextpid == USB_PID_OUT
378                                     && !(PTD_GET_COUNT(ptd) % ep->maxpacket)) {
379                                         DBG("Zero packet requested\n");
380                                         break;
381                                 }
382                         }
383                         /* All data for this URB is transferred, let's finish */
384                         if (usb_pipecontrol(urb->pipe))
385                                 ep->nextpid = USB_PID_ACK;
386                         else if (urb->status == -EINPROGRESS)
387                                 urb->status = 0;
388                         break;
389                 case USB_PID_SETUP:
390                         if (PTD_GET_ACTIVE(ptd)
391                             || (cc != TD_CC_NOERROR && cc < 0x0E))
392                                 break;
393                         if (urb->transfer_buffer_length == urb->actual_length)
394                                 ep->nextpid = USB_PID_ACK;
395                         else if (usb_pipeout(urb->pipe)) {
396                                 usb_settoggle(udev, 0, 1, 1);
397                                 ep->nextpid = USB_PID_OUT;
398                         } else {
399                                 usb_settoggle(udev, 0, 0, 1);
400                                 ep->nextpid = USB_PID_IN;
401                         }
402                         break;
403                 case USB_PID_ACK:
404                         if (PTD_GET_ACTIVE(ptd)
405                             || (cc != TD_CC_NOERROR && cc < 0x0E))
406                                 break;
407                         if (urb->status == -EINPROGRESS)
408                                 urb->status = 0;
409                         ep->nextpid = 0;
410                         break;
411                 default:
412                         BUG();
413                 }
414                 spin_unlock(&urb->lock);
415         }
416 }
417
418 /*
419   Take done or failed requests out of schedule. Give back
420   processed urbs.
421 */
422 static void finish_request(struct isp116x *isp116x, struct isp116x_ep *ep,
423                            struct urb *urb, struct pt_regs *regs)
424 __releases(isp116x->lock) __acquires(isp116x->lock)
425 {
426         unsigned i;
427
428         urb->hcpriv = NULL;
429         ep->error_count = 0;
430
431         if (usb_pipecontrol(urb->pipe))
432                 ep->nextpid = USB_PID_SETUP;
433
434         urb_dbg(urb, "Finish");
435
436         spin_unlock(&isp116x->lock);
437         usb_hcd_giveback_urb(isp116x_to_hcd(isp116x), urb, regs);
438         spin_lock(&isp116x->lock);
439
440         /* take idle endpoints out of the schedule */
441         if (!list_empty(&ep->hep->urb_list))
442                 return;
443
444         /* async deschedule */
445         if (!list_empty(&ep->schedule)) {
446                 list_del_init(&ep->schedule);
447                 return;
448         }
449
450         /* periodic deschedule */
451         DBG("deschedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
452         for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
453                 struct isp116x_ep *temp;
454                 struct isp116x_ep **prev = &isp116x->periodic[i];
455
456                 while (*prev && ((temp = *prev) != ep))
457                         prev = &temp->next;
458                 if (*prev)
459                         *prev = ep->next;
460                 isp116x->load[i] -= ep->load;
461         }
462         ep->branch = PERIODIC_SIZE;
463         isp116x_to_hcd(isp116x)->self.bandwidth_allocated -=
464             ep->load / ep->period;
465
466         /* switch irq type? */
467         if (!--isp116x->periodic_count) {
468                 isp116x->irqenb &= ~HCuPINT_SOF;
469                 isp116x->irqenb |= HCuPINT_ATL;
470         }
471 }
472
473 /*
474   Scan transfer lists, schedule transfers, send data off
475   to chip.
476  */
477 static void start_atl_transfers(struct isp116x *isp116x)
478 {
479         struct isp116x_ep *last_ep = NULL, *ep;
480         struct urb *urb;
481         u16 load = 0;
482         int len, index, speed, byte_time;
483
484         if (atomic_read(&isp116x->atl_finishing))
485                 return;
486
487         if (!HC_IS_RUNNING(isp116x_to_hcd(isp116x)->state))
488                 return;
489
490         /* FIFO not empty? */
491         if (isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_FULL)
492                 return;
493
494         isp116x->atl_active = NULL;
495         isp116x->atl_buflen = isp116x->atl_bufshrt = 0;
496
497         /* Schedule int transfers */
498         if (isp116x->periodic_count) {
499                 isp116x->fmindex = index =
500                     (isp116x->fmindex + 1) & (PERIODIC_SIZE - 1);
501                 if ((load = isp116x->load[index])) {
502                         /* Bring all int transfers for this frame
503                            into the active queue */
504                         isp116x->atl_active = last_ep =
505                             isp116x->periodic[index];
506                         while (last_ep->next)
507                                 last_ep = (last_ep->active = last_ep->next);
508                         last_ep->active = NULL;
509                 }
510         }
511
512         /* Schedule control/bulk transfers */
513         list_for_each_entry(ep, &isp116x->async, schedule) {
514                 urb = container_of(ep->hep->urb_list.next,
515                                    struct urb, urb_list);
516                 speed = urb->dev->speed;
517                 byte_time = speed == USB_SPEED_LOW
518                     ? BYTE_TIME_LOWSPEED : BYTE_TIME_FULLSPEED;
519
520                 if (ep->nextpid == USB_PID_SETUP) {
521                         len = sizeof(struct usb_ctrlrequest);
522                 } else if (ep->nextpid == USB_PID_ACK) {
523                         len = 0;
524                 } else {
525                         /* Find current free length ... */
526                         len = (MAX_LOAD_LIMIT - load) / byte_time;
527
528                         /* ... then limit it to configured max size ... */
529                         len = min(len, speed == USB_SPEED_LOW ?
530                                   MAX_TRANSFER_SIZE_LOWSPEED :
531                                   MAX_TRANSFER_SIZE_FULLSPEED);
532
533                         /* ... and finally cut to the multiple of MaxPacketSize,
534                            or to the real length if there's enough room. */
535                         if (len <
536                             (urb->transfer_buffer_length -
537                              urb->actual_length)) {
538                                 len -= len % ep->maxpacket;
539                                 if (!len)
540                                         continue;
541                         } else
542                                 len = urb->transfer_buffer_length -
543                                     urb->actual_length;
544                         BUG_ON(len < 0);
545                 }
546
547                 load += len * byte_time;
548                 if (load > MAX_LOAD_LIMIT)
549                         break;
550
551                 ep->active = NULL;
552                 ep->length = len;
553                 if (last_ep)
554                         last_ep->active = ep;
555                 else
556                         isp116x->atl_active = ep;
557                 last_ep = ep;
558         }
559
560         /* Avoid starving of endpoints */
561         if ((&isp116x->async)->next != (&isp116x->async)->prev)
562                 list_move(&isp116x->async, (&isp116x->async)->next);
563
564         if (isp116x->atl_active) {
565                 preproc_atl_queue(isp116x);
566                 pack_fifo(isp116x);
567         }
568 }
569
570 /*
571   Finish the processed transfers
572 */
573 static void finish_atl_transfers(struct isp116x *isp116x, struct pt_regs *regs)
574 {
575         struct isp116x_ep *ep;
576         struct urb *urb;
577
578         if (!isp116x->atl_active)
579                 return;
580         /* Fifo not ready? */
581         if (!(isp116x_read_reg16(isp116x, HCBUFSTAT) & HCBUFSTAT_ATL_DONE))
582                 return;
583
584         atomic_inc(&isp116x->atl_finishing);
585         unpack_fifo(isp116x);
586         postproc_atl_queue(isp116x);
587         for (ep = isp116x->atl_active; ep; ep = ep->active) {
588                 urb =
589                     container_of(ep->hep->urb_list.next, struct urb, urb_list);
590                 /* USB_PID_ACK check here avoids finishing of
591                    control transfers, for which TD_DATAUNDERRUN
592                    occured, while URB_SHORT_NOT_OK was set */
593                 if (urb && urb->status != -EINPROGRESS
594                     && ep->nextpid != USB_PID_ACK)
595                         finish_request(isp116x, ep, urb, regs);
596         }
597         atomic_dec(&isp116x->atl_finishing);
598 }
599
600 static irqreturn_t isp116x_irq(struct usb_hcd *hcd, struct pt_regs *regs)
601 {
602         struct isp116x *isp116x = hcd_to_isp116x(hcd);
603         u16 irqstat;
604         irqreturn_t ret = IRQ_NONE;
605
606         spin_lock(&isp116x->lock);
607         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
608         irqstat = isp116x_read_reg16(isp116x, HCuPINT);
609         isp116x_write_reg16(isp116x, HCuPINT, irqstat);
610
611         if (irqstat & (HCuPINT_ATL | HCuPINT_SOF)) {
612                 ret = IRQ_HANDLED;
613                 finish_atl_transfers(isp116x, regs);
614         }
615
616         if (irqstat & HCuPINT_OPR) {
617                 u32 intstat = isp116x_read_reg32(isp116x, HCINTSTAT);
618                 isp116x_write_reg32(isp116x, HCINTSTAT, intstat);
619                 if (intstat & HCINT_UE) {
620                         ERR("Unrecoverable error, HC is dead!\n");
621                         /* IRQ's are off, we do no DMA,
622                            perfectly ready to die ... */
623                         hcd->state = HC_STATE_HALT;
624                         ret = IRQ_HANDLED;
625                         goto done;
626                 }
627                 if (intstat & HCINT_RHSC)
628                         /* When root hub or any of its ports is going
629                            to come out of suspend, it may take more
630                            than 10ms for status bits to stabilize. */
631                         mod_timer(&hcd->rh_timer, jiffies
632                                   + msecs_to_jiffies(20) + 1);
633                 if (intstat & HCINT_RD) {
634                         DBG("---- remote wakeup\n");
635                         usb_hcd_resume_root_hub(hcd);
636                 }
637                 irqstat &= ~HCuPINT_OPR;
638                 ret = IRQ_HANDLED;
639         }
640
641         if (irqstat & (HCuPINT_ATL | HCuPINT_SOF)) {
642                 start_atl_transfers(isp116x);
643         }
644
645         isp116x_write_reg16(isp116x, HCuPINTENB, isp116x->irqenb);
646       done:
647         spin_unlock(&isp116x->lock);
648         return ret;
649 }
650
651 /*-----------------------------------------------------------------*/
652
653 /* usb 1.1 says max 90% of a frame is available for periodic transfers.
654  * this driver doesn't promise that much since it's got to handle an
655  * IRQ per packet; irq handling latencies also use up that time.
656  */
657
658 /* out of 1000 us */
659 #define MAX_PERIODIC_LOAD       600
660 static int balance(struct isp116x *isp116x, u16 period, u16 load)
661 {
662         int i, branch = -ENOSPC;
663
664         /* search for the least loaded schedule branch of that period
665            which has enough bandwidth left unreserved. */
666         for (i = 0; i < period; i++) {
667                 if (branch < 0 || isp116x->load[branch] > isp116x->load[i]) {
668                         int j;
669
670                         for (j = i; j < PERIODIC_SIZE; j += period) {
671                                 if ((isp116x->load[j] + load)
672                                     > MAX_PERIODIC_LOAD)
673                                         break;
674                         }
675                         if (j < PERIODIC_SIZE)
676                                 continue;
677                         branch = i;
678                 }
679         }
680         return branch;
681 }
682
683 /* NB! ALL the code above this point runs with isp116x->lock
684    held, irqs off
685 */
686
687 /*-----------------------------------------------------------------*/
688
689 static int isp116x_urb_enqueue(struct usb_hcd *hcd,
690                                struct usb_host_endpoint *hep, struct urb *urb,
691                                gfp_t mem_flags)
692 {
693         struct isp116x *isp116x = hcd_to_isp116x(hcd);
694         struct usb_device *udev = urb->dev;
695         unsigned int pipe = urb->pipe;
696         int is_out = !usb_pipein(pipe);
697         int type = usb_pipetype(pipe);
698         int epnum = usb_pipeendpoint(pipe);
699         struct isp116x_ep *ep = NULL;
700         unsigned long flags;
701         int i;
702         int ret = 0;
703
704         urb_dbg(urb, "Enqueue");
705
706         if (type == PIPE_ISOCHRONOUS) {
707                 ERR("Isochronous transfers not supported\n");
708                 urb_dbg(urb, "Refused to enqueue");
709                 return -ENXIO;
710         }
711         /* avoid all allocations within spinlocks: request or endpoint */
712         if (!hep->hcpriv) {
713                 ep = kzalloc(sizeof *ep, mem_flags);
714                 if (!ep)
715                         return -ENOMEM;
716         }
717
718         spin_lock_irqsave(&isp116x->lock, flags);
719         if (!HC_IS_RUNNING(hcd->state)) {
720                 kfree(ep);
721                 ret = -ENODEV;
722                 goto fail;
723         }
724
725         if (hep->hcpriv)
726                 ep = hep->hcpriv;
727         else {
728                 INIT_LIST_HEAD(&ep->schedule);
729                 ep->udev = usb_get_dev(udev);
730                 ep->epnum = epnum;
731                 ep->maxpacket = usb_maxpacket(udev, urb->pipe, is_out);
732                 usb_settoggle(udev, epnum, is_out, 0);
733
734                 if (type == PIPE_CONTROL) {
735                         ep->nextpid = USB_PID_SETUP;
736                 } else if (is_out) {
737                         ep->nextpid = USB_PID_OUT;
738                 } else {
739                         ep->nextpid = USB_PID_IN;
740                 }
741
742                 if (urb->interval) {
743                         /*
744                            With INT URBs submitted, the driver works with SOF
745                            interrupt enabled and ATL interrupt disabled. After
746                            the PTDs are written to fifo ram, the chip starts
747                            fifo processing and usb transfers after the next
748                            SOF and continues until the transfers are finished
749                            (succeeded or failed) or the frame ends. Therefore,
750                            the transfers occur only in every second frame,
751                            while fifo reading/writing and data processing
752                            occur in every other second frame. */
753                         if (urb->interval < 2)
754                                 urb->interval = 2;
755                         if (urb->interval > 2 * PERIODIC_SIZE)
756                                 urb->interval = 2 * PERIODIC_SIZE;
757                         ep->period = urb->interval >> 1;
758                         ep->branch = PERIODIC_SIZE;
759                         ep->load = usb_calc_bus_time(udev->speed,
760                                                      !is_out,
761                                                      (type == PIPE_ISOCHRONOUS),
762                                                      usb_maxpacket(udev, pipe,
763                                                                    is_out)) /
764                             1000;
765                 }
766                 hep->hcpriv = ep;
767                 ep->hep = hep;
768         }
769
770         /* maybe put endpoint into schedule */
771         switch (type) {
772         case PIPE_CONTROL:
773         case PIPE_BULK:
774                 if (list_empty(&ep->schedule))
775                         list_add_tail(&ep->schedule, &isp116x->async);
776                 break;
777         case PIPE_INTERRUPT:
778                 urb->interval = ep->period;
779                 ep->length = min((int)ep->maxpacket,
780                                  urb->transfer_buffer_length);
781
782                 /* urb submitted for already existing endpoint */
783                 if (ep->branch < PERIODIC_SIZE)
784                         break;
785
786                 ret = ep->branch = balance(isp116x, ep->period, ep->load);
787                 if (ret < 0)
788                         goto fail;
789                 ret = 0;
790
791                 urb->start_frame = (isp116x->fmindex & (PERIODIC_SIZE - 1))
792                     + ep->branch;
793
794                 /* sort each schedule branch by period (slow before fast)
795                    to share the faster parts of the tree without needing
796                    dummy/placeholder nodes */
797                 DBG("schedule qh%d/%p branch %d\n", ep->period, ep, ep->branch);
798                 for (i = ep->branch; i < PERIODIC_SIZE; i += ep->period) {
799                         struct isp116x_ep **prev = &isp116x->periodic[i];
800                         struct isp116x_ep *here = *prev;
801
802                         while (here && ep != here) {
803                                 if (ep->period > here->period)
804                                         break;
805                                 prev = &here->next;
806                                 here = *prev;
807                         }
808                         if (ep != here) {
809                                 ep->next = here;
810                                 *prev = ep;
811                         }
812                         isp116x->load[i] += ep->load;
813                 }
814                 hcd->self.bandwidth_allocated += ep->load / ep->period;
815
816                 /* switch over to SOFint */
817                 if (!isp116x->periodic_count++) {
818                         isp116x->irqenb &= ~HCuPINT_ATL;
819                         isp116x->irqenb |= HCuPINT_SOF;
820                         isp116x_write_reg16(isp116x, HCuPINTENB,
821                                             isp116x->irqenb);
822                 }
823         }
824
825         /* in case of unlink-during-submit */
826         spin_lock(&urb->lock);
827         if (urb->status != -EINPROGRESS) {
828                 spin_unlock(&urb->lock);
829                 finish_request(isp116x, ep, urb, NULL);
830                 ret = 0;
831                 goto fail;
832         }
833         urb->hcpriv = hep;
834         spin_unlock(&urb->lock);
835         start_atl_transfers(isp116x);
836
837       fail:
838         spin_unlock_irqrestore(&isp116x->lock, flags);
839         return ret;
840 }
841
842 /*
843    Dequeue URBs.
844 */
845 static int isp116x_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
846 {
847         struct isp116x *isp116x = hcd_to_isp116x(hcd);
848         struct usb_host_endpoint *hep;
849         struct isp116x_ep *ep, *ep_act;
850         unsigned long flags;
851
852         spin_lock_irqsave(&isp116x->lock, flags);
853         hep = urb->hcpriv;
854         /* URB already unlinked (or never linked)? */
855         if (!hep) {
856                 spin_unlock_irqrestore(&isp116x->lock, flags);
857                 return 0;
858         }
859         ep = hep->hcpriv;
860         WARN_ON(hep != ep->hep);
861
862         /* In front of queue? */
863         if (ep->hep->urb_list.next == &urb->urb_list)
864                 /* active? */
865                 for (ep_act = isp116x->atl_active; ep_act;
866                      ep_act = ep_act->active)
867                         if (ep_act == ep) {
868                                 VDBG("dequeue, urb %p active; wait for irq\n",
869                                      urb);
870                                 urb = NULL;
871                                 break;
872                         }
873
874         if (urb)
875                 finish_request(isp116x, ep, urb, NULL);
876
877         spin_unlock_irqrestore(&isp116x->lock, flags);
878         return 0;
879 }
880
881 static void isp116x_endpoint_disable(struct usb_hcd *hcd,
882                                      struct usb_host_endpoint *hep)
883 {
884         int i;
885         struct isp116x_ep *ep = hep->hcpriv;
886
887         if (!ep)
888                 return;
889
890         /* assume we'd just wait for the irq */
891         for (i = 0; i < 100 && !list_empty(&hep->urb_list); i++)
892                 msleep(3);
893         if (!list_empty(&hep->urb_list))
894                 WARN("ep %p not empty?\n", ep);
895
896         usb_put_dev(ep->udev);
897         kfree(ep);
898         hep->hcpriv = NULL;
899 }
900
901 static int isp116x_get_frame(struct usb_hcd *hcd)
902 {
903         struct isp116x *isp116x = hcd_to_isp116x(hcd);
904         u32 fmnum;
905         unsigned long flags;
906
907         spin_lock_irqsave(&isp116x->lock, flags);
908         fmnum = isp116x_read_reg32(isp116x, HCFMNUM);
909         spin_unlock_irqrestore(&isp116x->lock, flags);
910         return (int)fmnum;
911 }
912
913 /*
914   Adapted from ohci-hub.c. Currently we don't support autosuspend.
915 */
916 static int isp116x_hub_status_data(struct usb_hcd *hcd, char *buf)
917 {
918         struct isp116x *isp116x = hcd_to_isp116x(hcd);
919         int ports, i, changed = 0;
920         unsigned long flags;
921
922         if (!HC_IS_RUNNING(hcd->state))
923                 return -ESHUTDOWN;
924
925         /* Report no status change now, if we are scheduled to be
926            called later */
927         if (timer_pending(&hcd->rh_timer))
928                 return 0;
929
930         ports = isp116x->rhdesca & RH_A_NDP;
931         spin_lock_irqsave(&isp116x->lock, flags);
932         isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
933         if (isp116x->rhstatus & (RH_HS_LPSC | RH_HS_OCIC))
934                 buf[0] = changed = 1;
935         else
936                 buf[0] = 0;
937
938         for (i = 0; i < ports; i++) {
939                 u32 status = isp116x->rhport[i] =
940                     isp116x_read_reg32(isp116x, i ? HCRHPORT2 : HCRHPORT1);
941
942                 if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
943                               | RH_PS_OCIC | RH_PS_PRSC)) {
944                         changed = 1;
945                         buf[0] |= 1 << (i + 1);
946                         continue;
947                 }
948         }
949         spin_unlock_irqrestore(&isp116x->lock, flags);
950         return changed;
951 }
952
953 static void isp116x_hub_descriptor(struct isp116x *isp116x,
954                                    struct usb_hub_descriptor *desc)
955 {
956         u32 reg = isp116x->rhdesca;
957
958         desc->bDescriptorType = 0x29;
959         desc->bDescLength = 9;
960         desc->bHubContrCurrent = 0;
961         desc->bNbrPorts = (u8) (reg & 0x3);
962         /* Power switching, device type, overcurrent. */
963         desc->wHubCharacteristics = cpu_to_le16((u16) ((reg >> 8) & 0x1f));
964         desc->bPwrOn2PwrGood = (u8) ((reg >> 24) & 0xff);
965         /* two bitmaps:  ports removable, and legacy PortPwrCtrlMask */
966         desc->bitmap[0] = 0;
967         desc->bitmap[1] = ~0;
968 }
969
970 /* Perform reset of a given port.
971    It would be great to just start the reset and let the
972    USB core to clear the reset in due time. However,
973    root hub ports should be reset for at least 50 ms, while
974    our chip stays in reset for about 10 ms. I.e., we must
975    repeatedly reset it ourself here.
976 */
977 static inline void root_port_reset(struct isp116x *isp116x, unsigned port)
978 {
979         u32 tmp;
980         unsigned long flags, t;
981
982         /* Root hub reset should be 50 ms, but some devices
983            want it even longer. */
984         t = jiffies + msecs_to_jiffies(100);
985
986         while (time_before(jiffies, t)) {
987                 spin_lock_irqsave(&isp116x->lock, flags);
988                 /* spin until any current reset finishes */
989                 for (;;) {
990                         tmp = isp116x_read_reg32(isp116x, port ?
991                                                  HCRHPORT2 : HCRHPORT1);
992                         if (!(tmp & RH_PS_PRS))
993                                 break;
994                         udelay(500);
995                 }
996                 /* Don't reset a disconnected port */
997                 if (!(tmp & RH_PS_CCS)) {
998                         spin_unlock_irqrestore(&isp116x->lock, flags);
999                         break;
1000                 }
1001                 /* Reset lasts 10ms (claims datasheet) */
1002                 isp116x_write_reg32(isp116x, port ? HCRHPORT2 :
1003                                     HCRHPORT1, (RH_PS_PRS));
1004                 spin_unlock_irqrestore(&isp116x->lock, flags);
1005                 msleep(10);
1006         }
1007 }
1008
1009 /* Adapted from ohci-hub.c */
1010 static int isp116x_hub_control(struct usb_hcd *hcd,
1011                                u16 typeReq,
1012                                u16 wValue, u16 wIndex, char *buf, u16 wLength)
1013 {
1014         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1015         int ret = 0;
1016         unsigned long flags;
1017         int ports = isp116x->rhdesca & RH_A_NDP;
1018         u32 tmp = 0;
1019
1020         switch (typeReq) {
1021         case ClearHubFeature:
1022                 DBG("ClearHubFeature: ");
1023                 switch (wValue) {
1024                 case C_HUB_OVER_CURRENT:
1025                         DBG("C_HUB_OVER_CURRENT\n");
1026                         spin_lock_irqsave(&isp116x->lock, flags);
1027                         isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_OCIC);
1028                         spin_unlock_irqrestore(&isp116x->lock, flags);
1029                 case C_HUB_LOCAL_POWER:
1030                         DBG("C_HUB_LOCAL_POWER\n");
1031                         break;
1032                 default:
1033                         goto error;
1034                 }
1035                 break;
1036         case SetHubFeature:
1037                 DBG("SetHubFeature: ");
1038                 switch (wValue) {
1039                 case C_HUB_OVER_CURRENT:
1040                 case C_HUB_LOCAL_POWER:
1041                         DBG("C_HUB_OVER_CURRENT or C_HUB_LOCAL_POWER\n");
1042                         break;
1043                 default:
1044                         goto error;
1045                 }
1046                 break;
1047         case GetHubDescriptor:
1048                 DBG("GetHubDescriptor\n");
1049                 isp116x_hub_descriptor(isp116x,
1050                                        (struct usb_hub_descriptor *)buf);
1051                 break;
1052         case GetHubStatus:
1053                 DBG("GetHubStatus\n");
1054                 *(__le32 *) buf = 0;
1055                 break;
1056         case GetPortStatus:
1057                 DBG("GetPortStatus\n");
1058                 if (!wIndex || wIndex > ports)
1059                         goto error;
1060                 tmp = isp116x->rhport[--wIndex];
1061                 *(__le32 *) buf = cpu_to_le32(tmp);
1062                 DBG("GetPortStatus: port[%d]  %08x\n", wIndex + 1, tmp);
1063                 break;
1064         case ClearPortFeature:
1065                 DBG("ClearPortFeature: ");
1066                 if (!wIndex || wIndex > ports)
1067                         goto error;
1068                 wIndex--;
1069
1070                 switch (wValue) {
1071                 case USB_PORT_FEAT_ENABLE:
1072                         DBG("USB_PORT_FEAT_ENABLE\n");
1073                         tmp = RH_PS_CCS;
1074                         break;
1075                 case USB_PORT_FEAT_C_ENABLE:
1076                         DBG("USB_PORT_FEAT_C_ENABLE\n");
1077                         tmp = RH_PS_PESC;
1078                         break;
1079                 case USB_PORT_FEAT_SUSPEND:
1080                         DBG("USB_PORT_FEAT_SUSPEND\n");
1081                         tmp = RH_PS_POCI;
1082                         break;
1083                 case USB_PORT_FEAT_C_SUSPEND:
1084                         DBG("USB_PORT_FEAT_C_SUSPEND\n");
1085                         tmp = RH_PS_PSSC;
1086                         break;
1087                 case USB_PORT_FEAT_POWER:
1088                         DBG("USB_PORT_FEAT_POWER\n");
1089                         tmp = RH_PS_LSDA;
1090                         break;
1091                 case USB_PORT_FEAT_C_CONNECTION:
1092                         DBG("USB_PORT_FEAT_C_CONNECTION\n");
1093                         tmp = RH_PS_CSC;
1094                         break;
1095                 case USB_PORT_FEAT_C_OVER_CURRENT:
1096                         DBG("USB_PORT_FEAT_C_OVER_CURRENT\n");
1097                         tmp = RH_PS_OCIC;
1098                         break;
1099                 case USB_PORT_FEAT_C_RESET:
1100                         DBG("USB_PORT_FEAT_C_RESET\n");
1101                         tmp = RH_PS_PRSC;
1102                         break;
1103                 default:
1104                         goto error;
1105                 }
1106                 spin_lock_irqsave(&isp116x->lock, flags);
1107                 isp116x_write_reg32(isp116x, wIndex
1108                                     ? HCRHPORT2 : HCRHPORT1, tmp);
1109                 isp116x->rhport[wIndex] =
1110                     isp116x_read_reg32(isp116x, wIndex ? HCRHPORT2 : HCRHPORT1);
1111                 spin_unlock_irqrestore(&isp116x->lock, flags);
1112                 break;
1113         case SetPortFeature:
1114                 DBG("SetPortFeature: ");
1115                 if (!wIndex || wIndex > ports)
1116                         goto error;
1117                 wIndex--;
1118                 switch (wValue) {
1119                 case USB_PORT_FEAT_SUSPEND:
1120                         DBG("USB_PORT_FEAT_SUSPEND\n");
1121                         spin_lock_irqsave(&isp116x->lock, flags);
1122                         isp116x_write_reg32(isp116x, wIndex
1123                                             ? HCRHPORT2 : HCRHPORT1, RH_PS_PSS);
1124                         break;
1125                 case USB_PORT_FEAT_POWER:
1126                         DBG("USB_PORT_FEAT_POWER\n");
1127                         spin_lock_irqsave(&isp116x->lock, flags);
1128                         isp116x_write_reg32(isp116x, wIndex
1129                                             ? HCRHPORT2 : HCRHPORT1, RH_PS_PPS);
1130                         break;
1131                 case USB_PORT_FEAT_RESET:
1132                         DBG("USB_PORT_FEAT_RESET\n");
1133                         root_port_reset(isp116x, wIndex);
1134                         spin_lock_irqsave(&isp116x->lock, flags);
1135                         break;
1136                 default:
1137                         goto error;
1138                 }
1139                 isp116x->rhport[wIndex] =
1140                     isp116x_read_reg32(isp116x, wIndex ? HCRHPORT2 : HCRHPORT1);
1141                 spin_unlock_irqrestore(&isp116x->lock, flags);
1142                 break;
1143
1144         default:
1145               error:
1146                 /* "protocol stall" on error */
1147                 DBG("PROTOCOL STALL\n");
1148                 ret = -EPIPE;
1149         }
1150         return ret;
1151 }
1152
1153 /*-----------------------------------------------------------------*/
1154
1155 #ifdef CONFIG_DEBUG_FS
1156
1157 static void dump_irq(struct seq_file *s, char *label, u16 mask)
1158 {
1159         seq_printf(s, "%s %04x%s%s%s%s%s%s\n", label, mask,
1160                    mask & HCuPINT_CLKRDY ? " clkrdy" : "",
1161                    mask & HCuPINT_SUSP ? " susp" : "",
1162                    mask & HCuPINT_OPR ? " opr" : "",
1163                    mask & HCuPINT_AIIEOT ? " eot" : "",
1164                    mask & HCuPINT_ATL ? " atl" : "",
1165                    mask & HCuPINT_SOF ? " sof" : "");
1166 }
1167
1168 static void dump_int(struct seq_file *s, char *label, u32 mask)
1169 {
1170         seq_printf(s, "%s %08x%s%s%s%s%s%s%s\n", label, mask,
1171                    mask & HCINT_MIE ? " MIE" : "",
1172                    mask & HCINT_RHSC ? " rhsc" : "",
1173                    mask & HCINT_FNO ? " fno" : "",
1174                    mask & HCINT_UE ? " ue" : "",
1175                    mask & HCINT_RD ? " rd" : "",
1176                    mask & HCINT_SF ? " sof" : "", mask & HCINT_SO ? " so" : "");
1177 }
1178
1179 static int isp116x_show_dbg(struct seq_file *s, void *unused)
1180 {
1181         struct isp116x *isp116x = s->private;
1182
1183         seq_printf(s, "%s\n%s version %s\n",
1184                    isp116x_to_hcd(isp116x)->product_desc, hcd_name,
1185                    DRIVER_VERSION);
1186
1187         if (HC_IS_SUSPENDED(isp116x_to_hcd(isp116x)->state)) {
1188                 seq_printf(s, "HCD is suspended\n");
1189                 return 0;
1190         }
1191         if (!HC_IS_RUNNING(isp116x_to_hcd(isp116x)->state)) {
1192                 seq_printf(s, "HCD not running\n");
1193                 return 0;
1194         }
1195
1196         spin_lock_irq(&isp116x->lock);
1197         dump_irq(s, "hc_irq_enable", isp116x_read_reg16(isp116x, HCuPINTENB));
1198         dump_irq(s, "hc_irq_status", isp116x_read_reg16(isp116x, HCuPINT));
1199         dump_int(s, "hc_int_enable", isp116x_read_reg32(isp116x, HCINTENB));
1200         dump_int(s, "hc_int_status", isp116x_read_reg32(isp116x, HCINTSTAT));
1201         isp116x_show_regs_seq(isp116x, s);
1202         spin_unlock_irq(&isp116x->lock);
1203         seq_printf(s, "\n");
1204
1205         return 0;
1206 }
1207
1208 static int isp116x_open_seq(struct inode *inode, struct file *file)
1209 {
1210         return single_open(file, isp116x_show_dbg, inode->u.generic_ip);
1211 }
1212
1213 static struct file_operations isp116x_debug_fops = {
1214         .open = isp116x_open_seq,
1215         .read = seq_read,
1216         .llseek = seq_lseek,
1217         .release = single_release,
1218 };
1219
1220 static int create_debug_file(struct isp116x *isp116x)
1221 {
1222         isp116x->dentry = debugfs_create_file(hcd_name,
1223                                               S_IRUGO, NULL, isp116x,
1224                                               &isp116x_debug_fops);
1225         if (!isp116x->dentry)
1226                 return -ENOMEM;
1227         return 0;
1228 }
1229
1230 static void remove_debug_file(struct isp116x *isp116x)
1231 {
1232         debugfs_remove(isp116x->dentry);
1233 }
1234
1235 #else
1236
1237 #define create_debug_file(d)    0
1238 #define remove_debug_file(d)    do{}while(0)
1239
1240 #endif                          /* CONFIG_DEBUG_FS */
1241
1242 /*-----------------------------------------------------------------*/
1243
1244 /*
1245   Software reset - can be called from any contect.
1246 */
1247 static int isp116x_sw_reset(struct isp116x *isp116x)
1248 {
1249         int retries = 15;
1250         unsigned long flags;
1251         int ret = 0;
1252
1253         spin_lock_irqsave(&isp116x->lock, flags);
1254         isp116x_write_reg16(isp116x, HCSWRES, HCSWRES_MAGIC);
1255         isp116x_write_reg32(isp116x, HCCMDSTAT, HCCMDSTAT_HCR);
1256         while (--retries) {
1257                 /* It usually resets within 1 ms */
1258                 mdelay(1);
1259                 if (!(isp116x_read_reg32(isp116x, HCCMDSTAT) & HCCMDSTAT_HCR))
1260                         break;
1261         }
1262         if (!retries) {
1263                 ERR("Software reset timeout\n");
1264                 ret = -ETIME;
1265         }
1266         spin_unlock_irqrestore(&isp116x->lock, flags);
1267         return ret;
1268 }
1269
1270 static int isp116x_reset(struct usb_hcd *hcd)
1271 {
1272         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1273         unsigned long t;
1274         u16 clkrdy = 0;
1275         int ret, timeout = 15 /* ms */ ;
1276
1277         ret = isp116x_sw_reset(isp116x);
1278         if (ret)
1279                 return ret;
1280
1281         t = jiffies + msecs_to_jiffies(timeout);
1282         while (time_before_eq(jiffies, t)) {
1283                 msleep(4);
1284                 spin_lock_irq(&isp116x->lock);
1285                 clkrdy = isp116x_read_reg16(isp116x, HCuPINT) & HCuPINT_CLKRDY;
1286                 spin_unlock_irq(&isp116x->lock);
1287                 if (clkrdy)
1288                         break;
1289         }
1290         if (!clkrdy) {
1291                 ERR("Clock not ready after %dms\n", timeout);
1292                 /* After sw_reset the clock won't report to be ready, if
1293                    H_WAKEUP pin is high. */
1294                 ERR("Please make sure that the H_WAKEUP pin is pulled low!\n");
1295                 ret = -ENODEV;
1296         }
1297         return ret;
1298 }
1299
1300 static void isp116x_stop(struct usb_hcd *hcd)
1301 {
1302         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1303         unsigned long flags;
1304         u32 val;
1305
1306         spin_lock_irqsave(&isp116x->lock, flags);
1307         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1308
1309         /* Switch off ports' power, some devices don't come up
1310            after next 'insmod' without this */
1311         val = isp116x_read_reg32(isp116x, HCRHDESCA);
1312         val &= ~(RH_A_NPS | RH_A_PSM);
1313         isp116x_write_reg32(isp116x, HCRHDESCA, val);
1314         isp116x_write_reg32(isp116x, HCRHSTATUS, RH_HS_LPS);
1315         spin_unlock_irqrestore(&isp116x->lock, flags);
1316
1317         isp116x_sw_reset(isp116x);
1318 }
1319
1320 /*
1321   Configure the chip. The chip must be successfully reset by now.
1322 */
1323 static int isp116x_start(struct usb_hcd *hcd)
1324 {
1325         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1326         struct isp116x_platform_data *board = isp116x->board;
1327         u32 val;
1328         unsigned long flags;
1329
1330         spin_lock_irqsave(&isp116x->lock, flags);
1331
1332         /* clear interrupt status and disable all interrupt sources */
1333         isp116x_write_reg16(isp116x, HCuPINT, 0xff);
1334         isp116x_write_reg16(isp116x, HCuPINTENB, 0);
1335
1336         val = isp116x_read_reg16(isp116x, HCCHIPID);
1337         if ((val & HCCHIPID_MASK) != HCCHIPID_MAGIC) {
1338                 ERR("Invalid chip ID %04x\n", val);
1339                 spin_unlock_irqrestore(&isp116x->lock, flags);
1340                 return -ENODEV;
1341         }
1342
1343         /* To be removed in future */
1344         hcd->uses_new_polling = 1;
1345
1346         isp116x_write_reg16(isp116x, HCITLBUFLEN, ISP116x_ITL_BUFSIZE);
1347         isp116x_write_reg16(isp116x, HCATLBUFLEN, ISP116x_ATL_BUFSIZE);
1348
1349         /* ----- HW conf */
1350         val = HCHWCFG_INT_ENABLE | HCHWCFG_DBWIDTH(1);
1351         if (board->sel15Kres)
1352                 val |= HCHWCFG_15KRSEL;
1353         /* Remote wakeup won't work without working clock */
1354         if (board->remote_wakeup_enable)
1355                 val |= HCHWCFG_CLKNOTSTOP;
1356         if (board->oc_enable)
1357                 val |= HCHWCFG_ANALOG_OC;
1358         if (board->int_act_high)
1359                 val |= HCHWCFG_INT_POL;
1360         if (board->int_edge_triggered)
1361                 val |= HCHWCFG_INT_TRIGGER;
1362         isp116x_write_reg16(isp116x, HCHWCFG, val);
1363
1364         /* ----- Root hub conf */
1365         val = (25 << 24) & RH_A_POTPGT;
1366         /* AN10003_1.pdf recommends RH_A_NPS (no power switching) to
1367            be always set. Yet, instead, we request individual port
1368            power switching. */
1369         val |= RH_A_PSM;
1370         /* Report overcurrent per port */
1371         val |= RH_A_OCPM;
1372         isp116x_write_reg32(isp116x, HCRHDESCA, val);
1373         isp116x->rhdesca = isp116x_read_reg32(isp116x, HCRHDESCA);
1374
1375         val = RH_B_PPCM;
1376         isp116x_write_reg32(isp116x, HCRHDESCB, val);
1377         isp116x->rhdescb = isp116x_read_reg32(isp116x, HCRHDESCB);
1378
1379         val = 0;
1380         if (board->remote_wakeup_enable) {
1381                 if (!device_can_wakeup(hcd->self.controller))
1382                         device_init_wakeup(hcd->self.controller, 1);
1383                 val |= RH_HS_DRWE;
1384         }
1385         isp116x_write_reg32(isp116x, HCRHSTATUS, val);
1386         isp116x->rhstatus = isp116x_read_reg32(isp116x, HCRHSTATUS);
1387
1388         isp116x_write_reg32(isp116x, HCFMINTVL, 0x27782edf);
1389
1390         hcd->state = HC_STATE_RUNNING;
1391
1392         /* Set up interrupts */
1393         isp116x->intenb = HCINT_MIE | HCINT_RHSC | HCINT_UE;
1394         if (board->remote_wakeup_enable)
1395                 isp116x->intenb |= HCINT_RD;
1396         isp116x->irqenb = HCuPINT_ATL | HCuPINT_OPR;    /* | HCuPINT_SUSP; */
1397         isp116x_write_reg32(isp116x, HCINTENB, isp116x->intenb);
1398         isp116x_write_reg16(isp116x, HCuPINTENB, isp116x->irqenb);
1399
1400         /* Go operational */
1401         val = HCCONTROL_USB_OPER;
1402         if (board->remote_wakeup_enable)
1403                 val |= HCCONTROL_RWE;
1404         isp116x_write_reg32(isp116x, HCCONTROL, val);
1405
1406         /* Disable ports to avoid race in device enumeration */
1407         isp116x_write_reg32(isp116x, HCRHPORT1, RH_PS_CCS);
1408         isp116x_write_reg32(isp116x, HCRHPORT2, RH_PS_CCS);
1409
1410         isp116x_show_regs_log(isp116x);
1411         spin_unlock_irqrestore(&isp116x->lock, flags);
1412         return 0;
1413 }
1414
1415 #ifdef  CONFIG_PM
1416
1417 static int isp116x_bus_suspend(struct usb_hcd *hcd)
1418 {
1419         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1420         unsigned long flags;
1421         u32 val;
1422         int ret = 0;
1423
1424         spin_lock_irqsave(&isp116x->lock, flags);
1425
1426         val = isp116x_read_reg32(isp116x, HCCONTROL);
1427         switch (val & HCCONTROL_HCFS) {
1428         case HCCONTROL_USB_OPER:
1429                 hcd->state = HC_STATE_QUIESCING;
1430                 val &= (~HCCONTROL_HCFS & ~HCCONTROL_RWE);
1431                 val |= HCCONTROL_USB_SUSPEND;
1432                 if (device_may_wakeup(&hcd->self.root_hub->dev))
1433                         val |= HCCONTROL_RWE;
1434                 /* Wait for usb transfers to finish */
1435                 mdelay(2);
1436                 isp116x_write_reg32(isp116x, HCCONTROL, val);
1437                 hcd->state = HC_STATE_SUSPENDED;
1438                 /* Wait for devices to suspend */
1439                 mdelay(5);
1440         case HCCONTROL_USB_SUSPEND:
1441                 break;
1442         case HCCONTROL_USB_RESUME:
1443                 isp116x_write_reg32(isp116x, HCCONTROL,
1444                                     (val & ~HCCONTROL_HCFS) |
1445                                     HCCONTROL_USB_RESET);
1446         case HCCONTROL_USB_RESET:
1447                 ret = -EBUSY;
1448                 break;
1449         default:
1450                 ret = -EINVAL;
1451         }
1452
1453         spin_unlock_irqrestore(&isp116x->lock, flags);
1454         return ret;
1455 }
1456
1457 static int isp116x_bus_resume(struct usb_hcd *hcd)
1458 {
1459         struct isp116x *isp116x = hcd_to_isp116x(hcd);
1460         u32 val;
1461
1462         msleep(5);
1463         spin_lock_irq(&isp116x->lock);
1464
1465         val = isp116x_read_reg32(isp116x, HCCONTROL);
1466         switch (val & HCCONTROL_HCFS) {
1467         case HCCONTROL_USB_SUSPEND:
1468                 val &= ~HCCONTROL_HCFS;
1469                 val |= HCCONTROL_USB_RESUME;
1470                 isp116x_write_reg32(isp116x, HCCONTROL, val);
1471         case HCCONTROL_USB_RESUME:
1472                 break;
1473         case HCCONTROL_USB_OPER:
1474                 spin_unlock_irq(&isp116x->lock);
1475                 /* Without setting power_state here the
1476                    SUSPENDED state won't be removed from
1477                    sysfs/usbN/power.state as a response to remote
1478                    wakeup. Maybe in the future. */
1479                 hcd->self.root_hub->dev.power.power_state = PMSG_ON;
1480                 return 0;
1481         default:
1482                 /* HCCONTROL_USB_RESET: this may happen, when during
1483                    suspension the HC lost power. Reinitialize completely */
1484                 spin_unlock_irq(&isp116x->lock);
1485                 DBG("Chip has been reset while suspended. Reinit from scratch.\n");
1486                 isp116x_reset(hcd);
1487                 isp116x_start(hcd);
1488                 isp116x_hub_control(hcd, SetPortFeature,
1489                                     USB_PORT_FEAT_POWER, 1, NULL, 0);
1490                 if ((isp116x->rhdesca & RH_A_NDP) == 2)
1491                         isp116x_hub_control(hcd, SetPortFeature,
1492                                             USB_PORT_FEAT_POWER, 2, NULL, 0);
1493                 hcd->self.root_hub->dev.power.power_state = PMSG_ON;
1494                 return 0;
1495         }
1496
1497         val = isp116x->rhdesca & RH_A_NDP;
1498         while (val--) {
1499                 u32 stat =
1500                     isp116x_read_reg32(isp116x, val ? HCRHPORT2 : HCRHPORT1);
1501                 /* force global, not selective, resume */
1502                 if (!(stat & RH_PS_PSS))
1503                         continue;
1504                 DBG("%s: Resuming port %d\n", __func__, val);
1505                 isp116x_write_reg32(isp116x, RH_PS_POCI, val
1506                                     ? HCRHPORT2 : HCRHPORT1);
1507         }
1508         spin_unlock_irq(&isp116x->lock);
1509
1510         hcd->state = HC_STATE_RESUMING;
1511         msleep(20);
1512
1513         /* Go operational */
1514         spin_lock_irq(&isp116x->lock);
1515         val = isp116x_read_reg32(isp116x, HCCONTROL);
1516         isp116x_write_reg32(isp116x, HCCONTROL,
1517                             (val & ~HCCONTROL_HCFS) | HCCONTROL_USB_OPER);
1518         spin_unlock_irq(&isp116x->lock);
1519         /* see analogous comment above */
1520         hcd->self.root_hub->dev.power.power_state = PMSG_ON;
1521         hcd->state = HC_STATE_RUNNING;
1522
1523         return 0;
1524 }
1525
1526 #else
1527
1528 #define isp116x_bus_suspend     NULL
1529 #define isp116x_bus_resume      NULL
1530
1531 #endif
1532
1533 static struct hc_driver isp116x_hc_driver = {
1534         .description = hcd_name,
1535         .product_desc = "ISP116x Host Controller",
1536         .hcd_priv_size = sizeof(struct isp116x),
1537
1538         .irq = isp116x_irq,
1539         .flags = HCD_USB11,
1540
1541         .reset = isp116x_reset,
1542         .start = isp116x_start,
1543         .stop = isp116x_stop,
1544
1545         .urb_enqueue = isp116x_urb_enqueue,
1546         .urb_dequeue = isp116x_urb_dequeue,
1547         .endpoint_disable = isp116x_endpoint_disable,
1548
1549         .get_frame_number = isp116x_get_frame,
1550
1551         .hub_status_data = isp116x_hub_status_data,
1552         .hub_control = isp116x_hub_control,
1553         .bus_suspend = isp116x_bus_suspend,
1554         .bus_resume = isp116x_bus_resume,
1555 };
1556
1557 /*----------------------------------------------------------------*/
1558
1559 static int __init_or_module isp116x_remove(struct platform_device *pdev)
1560 {
1561         struct usb_hcd *hcd = platform_get_drvdata(pdev);
1562         struct isp116x *isp116x;
1563         struct resource *res;
1564
1565         if (!hcd)
1566                 return 0;
1567         isp116x = hcd_to_isp116x(hcd);
1568         remove_debug_file(isp116x);
1569         usb_remove_hcd(hcd);
1570
1571         iounmap(isp116x->data_reg);
1572         res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1573         release_mem_region(res->start, 2);
1574         iounmap(isp116x->addr_reg);
1575         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1576         release_mem_region(res->start, 2);
1577
1578         usb_put_hcd(hcd);
1579         return 0;
1580 }
1581
1582 #define resource_len(r) (((r)->end - (r)->start) + 1)
1583
1584 static int __init isp116x_probe(struct platform_device *pdev)
1585 {
1586         struct usb_hcd *hcd;
1587         struct isp116x *isp116x;
1588         struct resource *addr, *data;
1589         void __iomem *addr_reg;
1590         void __iomem *data_reg;
1591         int irq;
1592         int ret = 0;
1593
1594         if (pdev->num_resources < 3) {
1595                 ret = -ENODEV;
1596                 goto err1;
1597         }
1598
1599         data = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1600         addr = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1601         irq = platform_get_irq(pdev, 0);
1602         if (!addr || !data || irq < 0) {
1603                 ret = -ENODEV;
1604                 goto err1;
1605         }
1606
1607         if (pdev->dev.dma_mask) {
1608                 DBG("DMA not supported\n");
1609                 ret = -EINVAL;
1610                 goto err1;
1611         }
1612
1613         if (!request_mem_region(addr->start, 2, hcd_name)) {
1614                 ret = -EBUSY;
1615                 goto err1;
1616         }
1617         addr_reg = ioremap(addr->start, resource_len(addr));
1618         if (addr_reg == NULL) {
1619                 ret = -ENOMEM;
1620                 goto err2;
1621         }
1622         if (!request_mem_region(data->start, 2, hcd_name)) {
1623                 ret = -EBUSY;
1624                 goto err3;
1625         }
1626         data_reg = ioremap(data->start, resource_len(data));
1627         if (data_reg == NULL) {
1628                 ret = -ENOMEM;
1629                 goto err4;
1630         }
1631
1632         /* allocate and initialize hcd */
1633         hcd = usb_create_hcd(&isp116x_hc_driver, &pdev->dev, pdev->dev.bus_id);
1634         if (!hcd) {
1635                 ret = -ENOMEM;
1636                 goto err5;
1637         }
1638         /* this rsrc_start is bogus */
1639         hcd->rsrc_start = addr->start;
1640         isp116x = hcd_to_isp116x(hcd);
1641         isp116x->data_reg = data_reg;
1642         isp116x->addr_reg = addr_reg;
1643         spin_lock_init(&isp116x->lock);
1644         INIT_LIST_HEAD(&isp116x->async);
1645         isp116x->board = pdev->dev.platform_data;
1646
1647         if (!isp116x->board) {
1648                 ERR("Platform data structure not initialized\n");
1649                 ret = -ENODEV;
1650                 goto err6;
1651         }
1652         if (isp116x_check_platform_delay(isp116x)) {
1653                 ERR("USE_PLATFORM_DELAY defined, but delay function not "
1654                     "implemented.\n");
1655                 ERR("See comments in drivers/usb/host/isp116x-hcd.c\n");
1656                 ret = -ENODEV;
1657                 goto err6;
1658         }
1659
1660         ret = usb_add_hcd(hcd, irq, SA_INTERRUPT);
1661         if (ret)
1662                 goto err6;
1663
1664         ret = create_debug_file(isp116x);
1665         if (ret) {
1666                 ERR("Couldn't create debugfs entry\n");
1667                 goto err7;
1668         }
1669
1670         return 0;
1671
1672       err7:
1673         usb_remove_hcd(hcd);
1674       err6:
1675         usb_put_hcd(hcd);
1676       err5:
1677         iounmap(data_reg);
1678       err4:
1679         release_mem_region(data->start, 2);
1680       err3:
1681         iounmap(addr_reg);
1682       err2:
1683         release_mem_region(addr->start, 2);
1684       err1:
1685         ERR("init error, %d\n", ret);
1686         return ret;
1687 }
1688
1689 #ifdef  CONFIG_PM
1690 /*
1691   Suspend of platform device
1692 */
1693 static int isp116x_suspend(struct platform_device *dev, pm_message_t state)
1694 {
1695         VDBG("%s: state %x\n", __func__, state.event);
1696         dev->dev.power.power_state = state;
1697         return 0;
1698 }
1699
1700 /*
1701   Resume platform device
1702 */
1703 static int isp116x_resume(struct platform_device *dev)
1704 {
1705         VDBG("%s:  state %x\n", __func__, dev->power.power_state.event);
1706         dev->dev.power.power_state = PMSG_ON;
1707         return 0;
1708 }
1709
1710 #else
1711
1712 #define isp116x_suspend    NULL
1713 #define isp116x_resume     NULL
1714
1715 #endif
1716
1717 static struct platform_driver isp116x_driver = {
1718         .probe = isp116x_probe,
1719         .remove = isp116x_remove,
1720         .suspend = isp116x_suspend,
1721         .resume = isp116x_resume,
1722         .driver = {
1723                 .name = (char *)hcd_name,
1724         },
1725 };
1726
1727 /*-----------------------------------------------------------------*/
1728
1729 static int __init isp116x_init(void)
1730 {
1731         if (usb_disabled())
1732                 return -ENODEV;
1733
1734         INFO("driver %s, %s\n", hcd_name, DRIVER_VERSION);
1735         return platform_driver_register(&isp116x_driver);
1736 }
1737
1738 module_init(isp116x_init);
1739
1740 static void __exit isp116x_cleanup(void)
1741 {
1742         platform_driver_unregister(&isp116x_driver);
1743 }
1744
1745 module_exit(isp116x_cleanup);