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