Merge tag 'gpio-v3.13-3' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[linux-2.6.git] / drivers / usb / host / ohci-hcd.c
blob8ada13f8dde2c7350977cf343987fbb14c2f303b
1 /*
2 * Open Host Controller Interface (OHCI) driver for USB.
4 * Maintainer: Alan Stern <stern@rowland.harvard.edu>
6 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
7 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
9 * [ Initialisation is based on Linus' ]
10 * [ uhci code and gregs ohci fragments ]
11 * [ (C) Copyright 1999 Linus Torvalds ]
12 * [ (C) Copyright 1999 Gregory P. Smith]
15 * OHCI is the main "non-Intel/VIA" standard for USB 1.1 host controller
16 * interfaces (though some non-x86 Intel chips use it). It supports
17 * smarter hardware than UHCI. A download link for the spec available
18 * through the http://www.usb.org website.
20 * This file is licenced under the GPL.
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/pci.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28 #include <linux/ioport.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/init.h>
33 #include <linux/timer.h>
34 #include <linux/list.h>
35 #include <linux/usb.h>
36 #include <linux/usb/otg.h>
37 #include <linux/usb/hcd.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/dmapool.h>
40 #include <linux/workqueue.h>
41 #include <linux/debugfs.h>
43 #include <asm/io.h>
44 #include <asm/irq.h>
45 #include <asm/unaligned.h>
46 #include <asm/byteorder.h>
49 #define DRIVER_AUTHOR "Roman Weissgaerber, David Brownell"
50 #define DRIVER_DESC "USB 1.1 'Open' Host Controller (OHCI) Driver"
52 /*-------------------------------------------------------------------------*/
54 #undef OHCI_VERBOSE_DEBUG /* not always helpful */
56 /* For initializing controller (mask in an HCFS mode too) */
57 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
58 #define OHCI_INTR_INIT \
59 (OHCI_INTR_MIE | OHCI_INTR_RHSC | OHCI_INTR_UE \
60 | OHCI_INTR_RD | OHCI_INTR_WDH)
62 #ifdef __hppa__
63 /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
64 #define IR_DISABLE
65 #endif
67 #ifdef CONFIG_ARCH_OMAP
68 /* OMAP doesn't support IR (no SMM; not needed) */
69 #define IR_DISABLE
70 #endif
72 /*-------------------------------------------------------------------------*/
74 static const char hcd_name [] = "ohci_hcd";
76 #define STATECHANGE_DELAY msecs_to_jiffies(300)
78 #include "ohci.h"
79 #include "pci-quirks.h"
81 static void ohci_dump (struct ohci_hcd *ohci, int verbose);
82 static void ohci_stop (struct usb_hcd *hcd);
84 #include "ohci-hub.c"
85 #include "ohci-dbg.c"
86 #include "ohci-mem.c"
87 #include "ohci-q.c"
91 * On architectures with edge-triggered interrupts we must never return
92 * IRQ_NONE.
94 #if defined(CONFIG_SA1111) /* ... or other edge-triggered systems */
95 #define IRQ_NOTMINE IRQ_HANDLED
96 #else
97 #define IRQ_NOTMINE IRQ_NONE
98 #endif
101 /* Some boards misreport power switching/overcurrent */
102 static bool distrust_firmware = 1;
103 module_param (distrust_firmware, bool, 0);
104 MODULE_PARM_DESC (distrust_firmware,
105 "true to distrust firmware power/overcurrent setup");
107 /* Some boards leave IR set wrongly, since they fail BIOS/SMM handshakes */
108 static bool no_handshake = 0;
109 module_param (no_handshake, bool, 0);
110 MODULE_PARM_DESC (no_handshake, "true (not default) disables BIOS handshake");
112 /*-------------------------------------------------------------------------*/
115 * queue up an urb for anything except the root hub
117 static int ohci_urb_enqueue (
118 struct usb_hcd *hcd,
119 struct urb *urb,
120 gfp_t mem_flags
122 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
123 struct ed *ed;
124 urb_priv_t *urb_priv;
125 unsigned int pipe = urb->pipe;
126 int i, size = 0;
127 unsigned long flags;
128 int retval = 0;
130 #ifdef OHCI_VERBOSE_DEBUG
131 urb_print(urb, "SUB", usb_pipein(pipe), -EINPROGRESS);
132 #endif
134 /* every endpoint has a ed, locate and maybe (re)initialize it */
135 if (! (ed = ed_get (ohci, urb->ep, urb->dev, pipe, urb->interval)))
136 return -ENOMEM;
138 /* for the private part of the URB we need the number of TDs (size) */
139 switch (ed->type) {
140 case PIPE_CONTROL:
141 /* td_submit_urb() doesn't yet handle these */
142 if (urb->transfer_buffer_length > 4096)
143 return -EMSGSIZE;
145 /* 1 TD for setup, 1 for ACK, plus ... */
146 size = 2;
147 /* FALLTHROUGH */
148 // case PIPE_INTERRUPT:
149 // case PIPE_BULK:
150 default:
151 /* one TD for every 4096 Bytes (can be up to 8K) */
152 size += urb->transfer_buffer_length / 4096;
153 /* ... and for any remaining bytes ... */
154 if ((urb->transfer_buffer_length % 4096) != 0)
155 size++;
156 /* ... and maybe a zero length packet to wrap it up */
157 if (size == 0)
158 size++;
159 else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
160 && (urb->transfer_buffer_length
161 % usb_maxpacket (urb->dev, pipe,
162 usb_pipeout (pipe))) == 0)
163 size++;
164 break;
165 case PIPE_ISOCHRONOUS: /* number of packets from URB */
166 size = urb->number_of_packets;
167 break;
170 /* allocate the private part of the URB */
171 urb_priv = kzalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
172 mem_flags);
173 if (!urb_priv)
174 return -ENOMEM;
175 INIT_LIST_HEAD (&urb_priv->pending);
176 urb_priv->length = size;
177 urb_priv->ed = ed;
179 /* allocate the TDs (deferring hash chain updates) */
180 for (i = 0; i < size; i++) {
181 urb_priv->td [i] = td_alloc (ohci, mem_flags);
182 if (!urb_priv->td [i]) {
183 urb_priv->length = i;
184 urb_free_priv (ohci, urb_priv);
185 return -ENOMEM;
189 spin_lock_irqsave (&ohci->lock, flags);
191 /* don't submit to a dead HC */
192 if (!HCD_HW_ACCESSIBLE(hcd)) {
193 retval = -ENODEV;
194 goto fail;
196 if (ohci->rh_state != OHCI_RH_RUNNING) {
197 retval = -ENODEV;
198 goto fail;
200 retval = usb_hcd_link_urb_to_ep(hcd, urb);
201 if (retval)
202 goto fail;
204 /* schedule the ed if needed */
205 if (ed->state == ED_IDLE) {
206 retval = ed_schedule (ohci, ed);
207 if (retval < 0) {
208 usb_hcd_unlink_urb_from_ep(hcd, urb);
209 goto fail;
211 if (ed->type == PIPE_ISOCHRONOUS) {
212 u16 frame = ohci_frame_no(ohci);
214 /* delay a few frames before the first TD */
215 frame += max_t (u16, 8, ed->interval);
216 frame &= ~(ed->interval - 1);
217 frame |= ed->branch;
218 urb->start_frame = frame;
219 ed->last_iso = frame + ed->interval * (size - 1);
221 } else if (ed->type == PIPE_ISOCHRONOUS) {
222 u16 next = ohci_frame_no(ohci) + 1;
223 u16 frame = ed->last_iso + ed->interval;
224 u16 length = ed->interval * (size - 1);
226 /* Behind the scheduling threshold? */
227 if (unlikely(tick_before(frame, next))) {
229 /* URB_ISO_ASAP: Round up to the first available slot */
230 if (urb->transfer_flags & URB_ISO_ASAP) {
231 frame += (next - frame + ed->interval - 1) &
232 -ed->interval;
235 * Not ASAP: Use the next slot in the stream,
236 * no matter what.
238 } else {
240 * Some OHCI hardware doesn't handle late TDs
241 * correctly. After retiring them it proceeds
242 * to the next ED instead of the next TD.
243 * Therefore we have to omit the late TDs
244 * entirely.
246 urb_priv->td_cnt = DIV_ROUND_UP(
247 (u16) (next - frame),
248 ed->interval);
249 if (urb_priv->td_cnt >= urb_priv->length) {
250 ++urb_priv->td_cnt; /* Mark it */
251 ohci_dbg(ohci, "iso underrun %p (%u+%u < %u)\n",
252 urb, frame, length,
253 next);
257 urb->start_frame = frame;
258 ed->last_iso = frame + length;
261 /* fill the TDs and link them to the ed; and
262 * enable that part of the schedule, if needed
263 * and update count of queued periodic urbs
265 urb->hcpriv = urb_priv;
266 td_submit_urb (ohci, urb);
268 fail:
269 if (retval)
270 urb_free_priv (ohci, urb_priv);
271 spin_unlock_irqrestore (&ohci->lock, flags);
272 return retval;
276 * decouple the URB from the HC queues (TDs, urb_priv).
277 * reporting is always done
278 * asynchronously, and we might be dealing with an urb that's
279 * partially transferred, or an ED with other urbs being unlinked.
281 static int ohci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
283 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
284 unsigned long flags;
285 int rc;
287 #ifdef OHCI_VERBOSE_DEBUG
288 urb_print(urb, "UNLINK", 1, status);
289 #endif
291 spin_lock_irqsave (&ohci->lock, flags);
292 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
293 if (rc) {
294 ; /* Do nothing */
295 } else if (ohci->rh_state == OHCI_RH_RUNNING) {
296 urb_priv_t *urb_priv;
298 /* Unless an IRQ completed the unlink while it was being
299 * handed to us, flag it for unlink and giveback, and force
300 * some upcoming INTR_SF to call finish_unlinks()
302 urb_priv = urb->hcpriv;
303 if (urb_priv) {
304 if (urb_priv->ed->state == ED_OPER)
305 start_ed_unlink (ohci, urb_priv->ed);
307 } else {
309 * with HC dead, we won't respect hc queue pointers
310 * any more ... just clean up every urb's memory.
312 if (urb->hcpriv)
313 finish_urb(ohci, urb, status);
315 spin_unlock_irqrestore (&ohci->lock, flags);
316 return rc;
319 /*-------------------------------------------------------------------------*/
321 /* frees config/altsetting state for endpoints,
322 * including ED memory, dummy TD, and bulk/intr data toggle
325 static void
326 ohci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
328 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
329 unsigned long flags;
330 struct ed *ed = ep->hcpriv;
331 unsigned limit = 1000;
333 /* ASSERT: any requests/urbs are being unlinked */
334 /* ASSERT: nobody can be submitting urbs for this any more */
336 if (!ed)
337 return;
339 rescan:
340 spin_lock_irqsave (&ohci->lock, flags);
342 if (ohci->rh_state != OHCI_RH_RUNNING) {
343 sanitize:
344 ed->state = ED_IDLE;
345 if (quirk_zfmicro(ohci) && ed->type == PIPE_INTERRUPT)
346 ohci->eds_scheduled--;
347 finish_unlinks (ohci, 0);
350 switch (ed->state) {
351 case ED_UNLINK: /* wait for hw to finish? */
352 /* major IRQ delivery trouble loses INTR_SF too... */
353 if (limit-- == 0) {
354 ohci_warn(ohci, "ED unlink timeout\n");
355 if (quirk_zfmicro(ohci)) {
356 ohci_warn(ohci, "Attempting ZF TD recovery\n");
357 ohci->ed_to_check = ed;
358 ohci->zf_delay = 2;
360 goto sanitize;
362 spin_unlock_irqrestore (&ohci->lock, flags);
363 schedule_timeout_uninterruptible(1);
364 goto rescan;
365 case ED_IDLE: /* fully unlinked */
366 if (list_empty (&ed->td_list)) {
367 td_free (ohci, ed->dummy);
368 ed_free (ohci, ed);
369 break;
371 /* else FALL THROUGH */
372 default:
373 /* caller was supposed to have unlinked any requests;
374 * that's not our job. can't recover; must leak ed.
376 ohci_err (ohci, "leak ed %p (#%02x) state %d%s\n",
377 ed, ep->desc.bEndpointAddress, ed->state,
378 list_empty (&ed->td_list) ? "" : " (has tds)");
379 td_free (ohci, ed->dummy);
380 break;
382 ep->hcpriv = NULL;
383 spin_unlock_irqrestore (&ohci->lock, flags);
386 static int ohci_get_frame (struct usb_hcd *hcd)
388 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
390 return ohci_frame_no(ohci);
393 static void ohci_usb_reset (struct ohci_hcd *ohci)
395 ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
396 ohci->hc_control &= OHCI_CTRL_RWC;
397 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
398 ohci->rh_state = OHCI_RH_HALTED;
401 /* ohci_shutdown forcibly disables IRQs and DMA, helping kexec and
402 * other cases where the next software may expect clean state from the
403 * "firmware". this is bus-neutral, unlike shutdown() methods.
405 static void
406 ohci_shutdown (struct usb_hcd *hcd)
408 struct ohci_hcd *ohci;
410 ohci = hcd_to_ohci (hcd);
411 ohci_writel(ohci, (u32) ~0, &ohci->regs->intrdisable);
413 /* Software reset, after which the controller goes into SUSPEND */
414 ohci_writel(ohci, OHCI_HCR, &ohci->regs->cmdstatus);
415 ohci_readl(ohci, &ohci->regs->cmdstatus); /* flush the writes */
416 udelay(10);
418 ohci_writel(ohci, ohci->fminterval, &ohci->regs->fminterval);
421 static int check_ed(struct ohci_hcd *ohci, struct ed *ed)
423 return (hc32_to_cpu(ohci, ed->hwINFO) & ED_IN) != 0
424 && (hc32_to_cpu(ohci, ed->hwHeadP) & TD_MASK)
425 == (hc32_to_cpu(ohci, ed->hwTailP) & TD_MASK)
426 && !list_empty(&ed->td_list);
429 /* ZF Micro watchdog timer callback. The ZF Micro chipset sometimes completes
430 * an interrupt TD but neglects to add it to the donelist. On systems with
431 * this chipset, we need to periodically check the state of the queues to look
432 * for such "lost" TDs.
434 static void unlink_watchdog_func(unsigned long _ohci)
436 unsigned long flags;
437 unsigned max;
438 unsigned seen_count = 0;
439 unsigned i;
440 struct ed **seen = NULL;
441 struct ohci_hcd *ohci = (struct ohci_hcd *) _ohci;
443 spin_lock_irqsave(&ohci->lock, flags);
444 max = ohci->eds_scheduled;
445 if (!max)
446 goto done;
448 if (ohci->ed_to_check)
449 goto out;
451 seen = kcalloc(max, sizeof *seen, GFP_ATOMIC);
452 if (!seen)
453 goto out;
455 for (i = 0; i < NUM_INTS; i++) {
456 struct ed *ed = ohci->periodic[i];
458 while (ed) {
459 unsigned temp;
461 /* scan this branch of the periodic schedule tree */
462 for (temp = 0; temp < seen_count; temp++) {
463 if (seen[temp] == ed) {
464 /* we've checked it and what's after */
465 ed = NULL;
466 break;
469 if (!ed)
470 break;
471 seen[seen_count++] = ed;
472 if (!check_ed(ohci, ed)) {
473 ed = ed->ed_next;
474 continue;
477 /* HC's TD list is empty, but HCD sees at least one
478 * TD that's not been sent through the donelist.
480 ohci->ed_to_check = ed;
481 ohci->zf_delay = 2;
483 /* The HC may wait until the next frame to report the
484 * TD as done through the donelist and INTR_WDH. (We
485 * just *assume* it's not a multi-TD interrupt URB;
486 * those could defer the IRQ more than one frame, using
487 * DI...) Check again after the next INTR_SF.
489 ohci_writel(ohci, OHCI_INTR_SF,
490 &ohci->regs->intrstatus);
491 ohci_writel(ohci, OHCI_INTR_SF,
492 &ohci->regs->intrenable);
494 /* flush those writes */
495 (void) ohci_readl(ohci, &ohci->regs->control);
497 goto out;
500 out:
501 kfree(seen);
502 if (ohci->eds_scheduled)
503 mod_timer(&ohci->unlink_watchdog, round_jiffies(jiffies + HZ));
504 done:
505 spin_unlock_irqrestore(&ohci->lock, flags);
508 /*-------------------------------------------------------------------------*
509 * HC functions
510 *-------------------------------------------------------------------------*/
512 /* init memory, and kick BIOS/SMM off */
514 static int ohci_init (struct ohci_hcd *ohci)
516 int ret;
517 struct usb_hcd *hcd = ohci_to_hcd(ohci);
519 if (distrust_firmware)
520 ohci->flags |= OHCI_QUIRK_HUB_POWER;
522 ohci->rh_state = OHCI_RH_HALTED;
523 ohci->regs = hcd->regs;
525 /* REVISIT this BIOS handshake is now moved into PCI "quirks", and
526 * was never needed for most non-PCI systems ... remove the code?
529 #ifndef IR_DISABLE
530 /* SMM owns the HC? not for long! */
531 if (!no_handshake && ohci_readl (ohci,
532 &ohci->regs->control) & OHCI_CTRL_IR) {
533 u32 temp;
535 ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
537 /* this timeout is arbitrary. we make it long, so systems
538 * depending on usb keyboards may be usable even if the
539 * BIOS/SMM code seems pretty broken.
541 temp = 500; /* arbitrary: five seconds */
543 ohci_writel (ohci, OHCI_INTR_OC, &ohci->regs->intrenable);
544 ohci_writel (ohci, OHCI_OCR, &ohci->regs->cmdstatus);
545 while (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_IR) {
546 msleep (10);
547 if (--temp == 0) {
548 ohci_err (ohci, "USB HC takeover failed!"
549 " (BIOS/SMM bug)\n");
550 return -EBUSY;
553 ohci_usb_reset (ohci);
555 #endif
557 /* Disable HC interrupts */
558 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
560 /* flush the writes, and save key bits like RWC */
561 if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
562 ohci->hc_control |= OHCI_CTRL_RWC;
564 /* Read the number of ports unless overridden */
565 if (ohci->num_ports == 0)
566 ohci->num_ports = roothub_a(ohci) & RH_A_NDP;
568 if (ohci->hcca)
569 return 0;
571 ohci->hcca = dma_alloc_coherent (hcd->self.controller,
572 sizeof *ohci->hcca, &ohci->hcca_dma, 0);
573 if (!ohci->hcca)
574 return -ENOMEM;
576 if ((ret = ohci_mem_init (ohci)) < 0)
577 ohci_stop (hcd);
578 else {
579 create_debug_files (ohci);
582 return ret;
585 /*-------------------------------------------------------------------------*/
587 /* Start an OHCI controller, set the BUS operational
588 * resets USB and controller
589 * enable interrupts
591 static int ohci_run (struct ohci_hcd *ohci)
593 u32 mask, val;
594 int first = ohci->fminterval == 0;
595 struct usb_hcd *hcd = ohci_to_hcd(ohci);
597 ohci->rh_state = OHCI_RH_HALTED;
599 /* boot firmware should have set this up (5.1.1.3.1) */
600 if (first) {
602 val = ohci_readl (ohci, &ohci->regs->fminterval);
603 ohci->fminterval = val & 0x3fff;
604 if (ohci->fminterval != FI)
605 ohci_dbg (ohci, "fminterval delta %d\n",
606 ohci->fminterval - FI);
607 ohci->fminterval |= FSMP (ohci->fminterval) << 16;
608 /* also: power/overcurrent flags in roothub.a */
611 /* Reset USB nearly "by the book". RemoteWakeupConnected has
612 * to be checked in case boot firmware (BIOS/SMM/...) has set up
613 * wakeup in a way the bus isn't aware of (e.g., legacy PCI PM).
614 * If the bus glue detected wakeup capability then it should
615 * already be enabled; if so we'll just enable it again.
617 if ((ohci->hc_control & OHCI_CTRL_RWC) != 0)
618 device_set_wakeup_capable(hcd->self.controller, 1);
620 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
621 case OHCI_USB_OPER:
622 val = 0;
623 break;
624 case OHCI_USB_SUSPEND:
625 case OHCI_USB_RESUME:
626 ohci->hc_control &= OHCI_CTRL_RWC;
627 ohci->hc_control |= OHCI_USB_RESUME;
628 val = 10 /* msec wait */;
629 break;
630 // case OHCI_USB_RESET:
631 default:
632 ohci->hc_control &= OHCI_CTRL_RWC;
633 ohci->hc_control |= OHCI_USB_RESET;
634 val = 50 /* msec wait */;
635 break;
637 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
638 // flush the writes
639 (void) ohci_readl (ohci, &ohci->regs->control);
640 msleep(val);
642 memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
644 /* 2msec timelimit here means no irqs/preempt */
645 spin_lock_irq (&ohci->lock);
647 retry:
648 /* HC Reset requires max 10 us delay */
649 ohci_writel (ohci, OHCI_HCR, &ohci->regs->cmdstatus);
650 val = 30; /* ... allow extra time */
651 while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
652 if (--val == 0) {
653 spin_unlock_irq (&ohci->lock);
654 ohci_err (ohci, "USB HC reset timed out!\n");
655 return -1;
657 udelay (1);
660 /* now we're in the SUSPEND state ... must go OPERATIONAL
661 * within 2msec else HC enters RESUME
663 * ... but some hardware won't init fmInterval "by the book"
664 * (SiS, OPTi ...), so reset again instead. SiS doesn't need
665 * this if we write fmInterval after we're OPERATIONAL.
666 * Unclear about ALi, ServerWorks, and others ... this could
667 * easily be a longstanding bug in chip init on Linux.
669 if (ohci->flags & OHCI_QUIRK_INITRESET) {
670 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
671 // flush those writes
672 (void) ohci_readl (ohci, &ohci->regs->control);
675 /* Tell the controller where the control and bulk lists are
676 * The lists are empty now. */
677 ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
678 ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
680 /* a reset clears this */
681 ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
683 periodic_reinit (ohci);
685 /* some OHCI implementations are finicky about how they init.
686 * bogus values here mean not even enumeration could work.
688 if ((ohci_readl (ohci, &ohci->regs->fminterval) & 0x3fff0000) == 0
689 || !ohci_readl (ohci, &ohci->regs->periodicstart)) {
690 if (!(ohci->flags & OHCI_QUIRK_INITRESET)) {
691 ohci->flags |= OHCI_QUIRK_INITRESET;
692 ohci_dbg (ohci, "enabling initreset quirk\n");
693 goto retry;
695 spin_unlock_irq (&ohci->lock);
696 ohci_err (ohci, "init err (%08x %04x)\n",
697 ohci_readl (ohci, &ohci->regs->fminterval),
698 ohci_readl (ohci, &ohci->regs->periodicstart));
699 return -EOVERFLOW;
702 /* use rhsc irqs after khubd is fully initialized */
703 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
704 hcd->uses_new_polling = 1;
706 /* start controller operations */
707 ohci->hc_control &= OHCI_CTRL_RWC;
708 ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
709 ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
710 ohci->rh_state = OHCI_RH_RUNNING;
712 /* wake on ConnectStatusChange, matching external hubs */
713 ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
715 /* Choose the interrupts we care about now, others later on demand */
716 mask = OHCI_INTR_INIT;
717 ohci_writel (ohci, ~0, &ohci->regs->intrstatus);
718 ohci_writel (ohci, mask, &ohci->regs->intrenable);
720 /* handle root hub init quirks ... */
721 val = roothub_a (ohci);
722 val &= ~(RH_A_PSM | RH_A_OCPM);
723 if (ohci->flags & OHCI_QUIRK_SUPERIO) {
724 /* NSC 87560 and maybe others */
725 val |= RH_A_NOCP;
726 val &= ~(RH_A_POTPGT | RH_A_NPS);
727 ohci_writel (ohci, val, &ohci->regs->roothub.a);
728 } else if ((ohci->flags & OHCI_QUIRK_AMD756) ||
729 (ohci->flags & OHCI_QUIRK_HUB_POWER)) {
730 /* hub power always on; required for AMD-756 and some
731 * Mac platforms. ganged overcurrent reporting, if any.
733 val |= RH_A_NPS;
734 ohci_writel (ohci, val, &ohci->regs->roothub.a);
736 ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
737 ohci_writel (ohci, (val & RH_A_NPS) ? 0 : RH_B_PPCM,
738 &ohci->regs->roothub.b);
739 // flush those writes
740 (void) ohci_readl (ohci, &ohci->regs->control);
742 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
743 spin_unlock_irq (&ohci->lock);
745 // POTPGT delay is bits 24-31, in 2 ms units.
746 mdelay ((val >> 23) & 0x1fe);
748 if (quirk_zfmicro(ohci)) {
749 /* Create timer to watch for bad queue state on ZF Micro */
750 setup_timer(&ohci->unlink_watchdog, unlink_watchdog_func,
751 (unsigned long) ohci);
753 ohci->eds_scheduled = 0;
754 ohci->ed_to_check = NULL;
757 ohci_dump (ohci, 1);
759 return 0;
762 /* ohci_setup routine for generic controller initialization */
764 int ohci_setup(struct usb_hcd *hcd)
766 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
768 ohci_hcd_init(ohci);
770 return ohci_init(ohci);
772 EXPORT_SYMBOL_GPL(ohci_setup);
774 /* ohci_start routine for generic controller start of all OHCI bus glue */
775 static int ohci_start(struct usb_hcd *hcd)
777 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
778 int ret;
780 ret = ohci_run(ohci);
781 if (ret < 0) {
782 ohci_err(ohci, "can't start\n");
783 ohci_stop(hcd);
785 return ret;
788 /*-------------------------------------------------------------------------*/
790 /* an interrupt happens */
792 static irqreturn_t ohci_irq (struct usb_hcd *hcd)
794 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
795 struct ohci_regs __iomem *regs = ohci->regs;
796 int ints;
798 /* Read interrupt status (and flush pending writes). We ignore the
799 * optimization of checking the LSB of hcca->done_head; it doesn't
800 * work on all systems (edge triggering for OHCI can be a factor).
802 ints = ohci_readl(ohci, &regs->intrstatus);
804 /* Check for an all 1's result which is a typical consequence
805 * of dead, unclocked, or unplugged (CardBus...) devices
807 if (ints == ~(u32)0) {
808 ohci->rh_state = OHCI_RH_HALTED;
809 ohci_dbg (ohci, "device removed!\n");
810 usb_hc_died(hcd);
811 return IRQ_HANDLED;
814 /* We only care about interrupts that are enabled */
815 ints &= ohci_readl(ohci, &regs->intrenable);
817 /* interrupt for some other device? */
818 if (ints == 0 || unlikely(ohci->rh_state == OHCI_RH_HALTED))
819 return IRQ_NOTMINE;
821 if (ints & OHCI_INTR_UE) {
822 // e.g. due to PCI Master/Target Abort
823 if (quirk_nec(ohci)) {
824 /* Workaround for a silicon bug in some NEC chips used
825 * in Apple's PowerBooks. Adapted from Darwin code.
827 ohci_err (ohci, "OHCI Unrecoverable Error, scheduling NEC chip restart\n");
829 ohci_writel (ohci, OHCI_INTR_UE, &regs->intrdisable);
831 schedule_work (&ohci->nec_work);
832 } else {
833 ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
834 ohci->rh_state = OHCI_RH_HALTED;
835 usb_hc_died(hcd);
838 ohci_dump (ohci, 1);
839 ohci_usb_reset (ohci);
842 if (ints & OHCI_INTR_RHSC) {
843 ohci_vdbg(ohci, "rhsc\n");
844 ohci->next_statechange = jiffies + STATECHANGE_DELAY;
845 ohci_writel(ohci, OHCI_INTR_RD | OHCI_INTR_RHSC,
846 &regs->intrstatus);
848 /* NOTE: Vendors didn't always make the same implementation
849 * choices for RHSC. Many followed the spec; RHSC triggers
850 * on an edge, like setting and maybe clearing a port status
851 * change bit. With others it's level-triggered, active
852 * until khubd clears all the port status change bits. We'll
853 * always disable it here and rely on polling until khubd
854 * re-enables it.
856 ohci_writel(ohci, OHCI_INTR_RHSC, &regs->intrdisable);
857 usb_hcd_poll_rh_status(hcd);
860 /* For connect and disconnect events, we expect the controller
861 * to turn on RHSC along with RD. But for remote wakeup events
862 * this might not happen.
864 else if (ints & OHCI_INTR_RD) {
865 ohci_vdbg(ohci, "resume detect\n");
866 ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus);
867 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
868 if (ohci->autostop) {
869 spin_lock (&ohci->lock);
870 ohci_rh_resume (ohci);
871 spin_unlock (&ohci->lock);
872 } else
873 usb_hcd_resume_root_hub(hcd);
876 if (ints & OHCI_INTR_WDH) {
877 spin_lock (&ohci->lock);
878 dl_done_list (ohci);
879 spin_unlock (&ohci->lock);
882 if (quirk_zfmicro(ohci) && (ints & OHCI_INTR_SF)) {
883 spin_lock(&ohci->lock);
884 if (ohci->ed_to_check) {
885 struct ed *ed = ohci->ed_to_check;
887 if (check_ed(ohci, ed)) {
888 /* HC thinks the TD list is empty; HCD knows
889 * at least one TD is outstanding
891 if (--ohci->zf_delay == 0) {
892 struct td *td = list_entry(
893 ed->td_list.next,
894 struct td, td_list);
895 ohci_warn(ohci,
896 "Reclaiming orphan TD %p\n",
897 td);
898 takeback_td(ohci, td);
899 ohci->ed_to_check = NULL;
901 } else
902 ohci->ed_to_check = NULL;
904 spin_unlock(&ohci->lock);
907 /* could track INTR_SO to reduce available PCI/... bandwidth */
909 /* handle any pending URB/ED unlinks, leaving INTR_SF enabled
910 * when there's still unlinking to be done (next frame).
912 spin_lock (&ohci->lock);
913 if (ohci->ed_rm_list)
914 finish_unlinks (ohci, ohci_frame_no(ohci));
915 if ((ints & OHCI_INTR_SF) != 0
916 && !ohci->ed_rm_list
917 && !ohci->ed_to_check
918 && ohci->rh_state == OHCI_RH_RUNNING)
919 ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
920 spin_unlock (&ohci->lock);
922 if (ohci->rh_state == OHCI_RH_RUNNING) {
923 ohci_writel (ohci, ints, &regs->intrstatus);
924 ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
925 // flush those writes
926 (void) ohci_readl (ohci, &ohci->regs->control);
929 return IRQ_HANDLED;
932 /*-------------------------------------------------------------------------*/
934 static void ohci_stop (struct usb_hcd *hcd)
936 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
938 ohci_dump (ohci, 1);
940 if (quirk_nec(ohci))
941 flush_work(&ohci->nec_work);
943 ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
944 ohci_usb_reset(ohci);
945 free_irq(hcd->irq, hcd);
946 hcd->irq = 0;
948 if (quirk_zfmicro(ohci))
949 del_timer(&ohci->unlink_watchdog);
950 if (quirk_amdiso(ohci))
951 usb_amd_dev_put();
953 remove_debug_files (ohci);
954 ohci_mem_cleanup (ohci);
955 if (ohci->hcca) {
956 dma_free_coherent (hcd->self.controller,
957 sizeof *ohci->hcca,
958 ohci->hcca, ohci->hcca_dma);
959 ohci->hcca = NULL;
960 ohci->hcca_dma = 0;
964 /*-------------------------------------------------------------------------*/
966 #if defined(CONFIG_PM) || defined(CONFIG_PCI)
968 /* must not be called from interrupt context */
969 int ohci_restart(struct ohci_hcd *ohci)
971 int temp;
972 int i;
973 struct urb_priv *priv;
975 ohci_init(ohci);
976 spin_lock_irq(&ohci->lock);
977 ohci->rh_state = OHCI_RH_HALTED;
979 /* Recycle any "live" eds/tds (and urbs). */
980 if (!list_empty (&ohci->pending))
981 ohci_dbg(ohci, "abort schedule...\n");
982 list_for_each_entry (priv, &ohci->pending, pending) {
983 struct urb *urb = priv->td[0]->urb;
984 struct ed *ed = priv->ed;
986 switch (ed->state) {
987 case ED_OPER:
988 ed->state = ED_UNLINK;
989 ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
990 ed_deschedule (ohci, ed);
992 ed->ed_next = ohci->ed_rm_list;
993 ed->ed_prev = NULL;
994 ohci->ed_rm_list = ed;
995 /* FALLTHROUGH */
996 case ED_UNLINK:
997 break;
998 default:
999 ohci_dbg(ohci, "bogus ed %p state %d\n",
1000 ed, ed->state);
1003 if (!urb->unlinked)
1004 urb->unlinked = -ESHUTDOWN;
1006 finish_unlinks (ohci, 0);
1007 spin_unlock_irq(&ohci->lock);
1009 /* paranoia, in case that didn't work: */
1011 /* empty the interrupt branches */
1012 for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
1013 for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
1015 /* no EDs to remove */
1016 ohci->ed_rm_list = NULL;
1018 /* empty control and bulk lists */
1019 ohci->ed_controltail = NULL;
1020 ohci->ed_bulktail = NULL;
1022 if ((temp = ohci_run (ohci)) < 0) {
1023 ohci_err (ohci, "can't restart, %d\n", temp);
1024 return temp;
1026 ohci_dbg(ohci, "restart complete\n");
1027 return 0;
1029 EXPORT_SYMBOL_GPL(ohci_restart);
1031 #endif
1033 #ifdef CONFIG_PM
1035 int ohci_suspend(struct usb_hcd *hcd, bool do_wakeup)
1037 struct ohci_hcd *ohci = hcd_to_ohci (hcd);
1038 unsigned long flags;
1040 /* Disable irq emission and mark HW unaccessible. Use
1041 * the spinlock to properly synchronize with possible pending
1042 * RH suspend or resume activity.
1044 spin_lock_irqsave (&ohci->lock, flags);
1045 ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
1046 (void)ohci_readl(ohci, &ohci->regs->intrdisable);
1048 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1049 spin_unlock_irqrestore (&ohci->lock, flags);
1051 return 0;
1053 EXPORT_SYMBOL_GPL(ohci_suspend);
1056 int ohci_resume(struct usb_hcd *hcd, bool hibernated)
1058 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
1059 int port;
1060 bool need_reinit = false;
1062 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1064 /* Make sure resume from hibernation re-enumerates everything */
1065 if (hibernated)
1066 ohci_usb_reset(ohci);
1068 /* See if the controller is already running or has been reset */
1069 ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
1070 if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
1071 need_reinit = true;
1072 } else {
1073 switch (ohci->hc_control & OHCI_CTRL_HCFS) {
1074 case OHCI_USB_OPER:
1075 case OHCI_USB_RESET:
1076 need_reinit = true;
1080 /* If needed, reinitialize and suspend the root hub */
1081 if (need_reinit) {
1082 spin_lock_irq(&ohci->lock);
1083 ohci_rh_resume(ohci);
1084 ohci_rh_suspend(ohci, 0);
1085 spin_unlock_irq(&ohci->lock);
1088 /* Normally just turn on port power and enable interrupts */
1089 else {
1090 ohci_dbg(ohci, "powerup ports\n");
1091 for (port = 0; port < ohci->num_ports; port++)
1092 ohci_writel(ohci, RH_PS_PPS,
1093 &ohci->regs->roothub.portstatus[port]);
1095 ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrenable);
1096 ohci_readl(ohci, &ohci->regs->intrenable);
1097 msleep(20);
1100 usb_hcd_resume_root_hub(hcd);
1102 return 0;
1104 EXPORT_SYMBOL_GPL(ohci_resume);
1106 #endif
1108 /*-------------------------------------------------------------------------*/
1111 * Generic structure: This gets copied for platform drivers so that
1112 * individual entries can be overridden as needed.
1115 static const struct hc_driver ohci_hc_driver = {
1116 .description = hcd_name,
1117 .product_desc = "OHCI Host Controller",
1118 .hcd_priv_size = sizeof(struct ohci_hcd),
1121 * generic hardware linkage
1123 .irq = ohci_irq,
1124 .flags = HCD_MEMORY | HCD_USB11,
1127 * basic lifecycle operations
1129 .reset = ohci_setup,
1130 .start = ohci_start,
1131 .stop = ohci_stop,
1132 .shutdown = ohci_shutdown,
1135 * managing i/o requests and associated device resources
1137 .urb_enqueue = ohci_urb_enqueue,
1138 .urb_dequeue = ohci_urb_dequeue,
1139 .endpoint_disable = ohci_endpoint_disable,
1142 * scheduling support
1144 .get_frame_number = ohci_get_frame,
1147 * root hub support
1149 .hub_status_data = ohci_hub_status_data,
1150 .hub_control = ohci_hub_control,
1151 #ifdef CONFIG_PM
1152 .bus_suspend = ohci_bus_suspend,
1153 .bus_resume = ohci_bus_resume,
1154 #endif
1155 .start_port_reset = ohci_start_port_reset,
1158 void ohci_init_driver(struct hc_driver *drv,
1159 const struct ohci_driver_overrides *over)
1161 /* Copy the generic table to drv and then apply the overrides */
1162 *drv = ohci_hc_driver;
1164 if (over) {
1165 drv->product_desc = over->product_desc;
1166 drv->hcd_priv_size += over->extra_priv_size;
1167 if (over->reset)
1168 drv->reset = over->reset;
1171 EXPORT_SYMBOL_GPL(ohci_init_driver);
1173 /*-------------------------------------------------------------------------*/
1175 MODULE_AUTHOR (DRIVER_AUTHOR);
1176 MODULE_DESCRIPTION(DRIVER_DESC);
1177 MODULE_LICENSE ("GPL");
1179 #if defined(CONFIG_ARCH_SA1100) && defined(CONFIG_SA1111)
1180 #include "ohci-sa1111.c"
1181 #define SA1111_DRIVER ohci_hcd_sa1111_driver
1182 #endif
1184 #ifdef CONFIG_ARCH_DAVINCI_DA8XX
1185 #include "ohci-da8xx.c"
1186 #define DAVINCI_PLATFORM_DRIVER ohci_hcd_da8xx_driver
1187 #endif
1189 #ifdef CONFIG_USB_OHCI_HCD_PPC_OF
1190 #include "ohci-ppc-of.c"
1191 #define OF_PLATFORM_DRIVER ohci_hcd_ppc_of_driver
1192 #endif
1194 #ifdef CONFIG_PPC_PS3
1195 #include "ohci-ps3.c"
1196 #define PS3_SYSTEM_BUS_DRIVER ps3_ohci_driver
1197 #endif
1199 #ifdef CONFIG_MFD_SM501
1200 #include "ohci-sm501.c"
1201 #define SM501_OHCI_DRIVER ohci_hcd_sm501_driver
1202 #endif
1204 #ifdef CONFIG_MFD_TC6393XB
1205 #include "ohci-tmio.c"
1206 #define TMIO_OHCI_DRIVER ohci_hcd_tmio_driver
1207 #endif
1209 #ifdef CONFIG_MACH_JZ4740
1210 #include "ohci-jz4740.c"
1211 #define PLATFORM_DRIVER ohci_hcd_jz4740_driver
1212 #endif
1214 #ifdef CONFIG_USB_OCTEON_OHCI
1215 #include "ohci-octeon.c"
1216 #define PLATFORM_DRIVER ohci_octeon_driver
1217 #endif
1219 #ifdef CONFIG_TILE_USB
1220 #include "ohci-tilegx.c"
1221 #define PLATFORM_DRIVER ohci_hcd_tilegx_driver
1222 #endif
1224 static int __init ohci_hcd_mod_init(void)
1226 int retval = 0;
1228 if (usb_disabled())
1229 return -ENODEV;
1231 printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
1232 pr_debug ("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
1233 sizeof (struct ed), sizeof (struct td));
1234 set_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
1236 #ifdef DEBUG
1237 ohci_debug_root = debugfs_create_dir("ohci", usb_debug_root);
1238 if (!ohci_debug_root) {
1239 retval = -ENOENT;
1240 goto error_debug;
1242 #endif
1244 #ifdef PS3_SYSTEM_BUS_DRIVER
1245 retval = ps3_ohci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
1246 if (retval < 0)
1247 goto error_ps3;
1248 #endif
1250 #ifdef PLATFORM_DRIVER
1251 retval = platform_driver_register(&PLATFORM_DRIVER);
1252 if (retval < 0)
1253 goto error_platform;
1254 #endif
1256 #ifdef OF_PLATFORM_DRIVER
1257 retval = platform_driver_register(&OF_PLATFORM_DRIVER);
1258 if (retval < 0)
1259 goto error_of_platform;
1260 #endif
1262 #ifdef SA1111_DRIVER
1263 retval = sa1111_driver_register(&SA1111_DRIVER);
1264 if (retval < 0)
1265 goto error_sa1111;
1266 #endif
1268 #ifdef SM501_OHCI_DRIVER
1269 retval = platform_driver_register(&SM501_OHCI_DRIVER);
1270 if (retval < 0)
1271 goto error_sm501;
1272 #endif
1274 #ifdef TMIO_OHCI_DRIVER
1275 retval = platform_driver_register(&TMIO_OHCI_DRIVER);
1276 if (retval < 0)
1277 goto error_tmio;
1278 #endif
1280 #ifdef DAVINCI_PLATFORM_DRIVER
1281 retval = platform_driver_register(&DAVINCI_PLATFORM_DRIVER);
1282 if (retval < 0)
1283 goto error_davinci;
1284 #endif
1286 return retval;
1288 /* Error path */
1289 #ifdef DAVINCI_PLATFORM_DRIVER
1290 platform_driver_unregister(&DAVINCI_PLATFORM_DRIVER);
1291 error_davinci:
1292 #endif
1293 #ifdef TMIO_OHCI_DRIVER
1294 platform_driver_unregister(&TMIO_OHCI_DRIVER);
1295 error_tmio:
1296 #endif
1297 #ifdef SM501_OHCI_DRIVER
1298 platform_driver_unregister(&SM501_OHCI_DRIVER);
1299 error_sm501:
1300 #endif
1301 #ifdef SA1111_DRIVER
1302 sa1111_driver_unregister(&SA1111_DRIVER);
1303 error_sa1111:
1304 #endif
1305 #ifdef OF_PLATFORM_DRIVER
1306 platform_driver_unregister(&OF_PLATFORM_DRIVER);
1307 error_of_platform:
1308 #endif
1309 #ifdef PLATFORM_DRIVER
1310 platform_driver_unregister(&PLATFORM_DRIVER);
1311 error_platform:
1312 #endif
1313 #ifdef PS3_SYSTEM_BUS_DRIVER
1314 ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1315 error_ps3:
1316 #endif
1317 #ifdef DEBUG
1318 debugfs_remove(ohci_debug_root);
1319 ohci_debug_root = NULL;
1320 error_debug:
1321 #endif
1323 clear_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
1324 return retval;
1326 module_init(ohci_hcd_mod_init);
1328 static void __exit ohci_hcd_mod_exit(void)
1330 #ifdef DAVINCI_PLATFORM_DRIVER
1331 platform_driver_unregister(&DAVINCI_PLATFORM_DRIVER);
1332 #endif
1333 #ifdef TMIO_OHCI_DRIVER
1334 platform_driver_unregister(&TMIO_OHCI_DRIVER);
1335 #endif
1336 #ifdef SM501_OHCI_DRIVER
1337 platform_driver_unregister(&SM501_OHCI_DRIVER);
1338 #endif
1339 #ifdef SA1111_DRIVER
1340 sa1111_driver_unregister(&SA1111_DRIVER);
1341 #endif
1342 #ifdef OF_PLATFORM_DRIVER
1343 platform_driver_unregister(&OF_PLATFORM_DRIVER);
1344 #endif
1345 #ifdef PLATFORM_DRIVER
1346 platform_driver_unregister(&PLATFORM_DRIVER);
1347 #endif
1348 #ifdef PS3_SYSTEM_BUS_DRIVER
1349 ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
1350 #endif
1351 #ifdef DEBUG
1352 debugfs_remove(ohci_debug_root);
1353 #endif
1354 clear_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
1356 module_exit(ohci_hcd_mod_exit);