2 * OHCI HCD (Host Controller Driver) for USB.
4 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
5 * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
7 * [ Initialisation is based on Linus' ]
8 * [ uhci code and gregs ahcd fragments ]
9 * [ (C) Copyright 1999 Linus Torvalds ]
10 * [ (C) Copyright 1999 Gregory P. Smith]
13 * OHCI is the main "non-Intel/VIA" standard for USB 1.1 host controller
14 * interfaces (though some non-x86 Intel chips use it). It supports
15 * smarter hardware than UHCI. A download link for the spec available
16 * through the http://www.usb.org website.
18 * This file is licenced under the GPL.
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/pci.h>
24 #include <linux/kernel.h>
25 #include <linux/delay.h>
26 #include <linux/ioport.h>
27 #include <linux/sched.h>
28 #include <linux/slab.h>
29 #include <linux/errno.h>
30 #include <linux/init.h>
31 #include <linux/timer.h>
32 #include <linux/list.h>
33 #include <linux/usb.h>
34 #include <linux/usb/otg.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/dmapool.h>
37 #include <linux/reboot.h>
41 #include <asm/system.h>
42 #include <asm/unaligned.h>
43 #include <asm/byteorder.h>
45 #include "../core/hcd.h"
46 #include "../core/hub.h"
48 #define DRIVER_VERSION "v0.06"
49 #define DRIVER_AUTHOR "Gabor Juhos <juhosg at openwrt.org>"
50 #define DRIVER_DESC "ADMtek USB 1.1 Host Controller Driver"
52 /*-------------------------------------------------------------------------*/
54 #undef ADMHC_VERBOSE_DEBUG /* not always helpful */
56 /* For initializing controller (mask in an HCFS mode too) */
57 #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
59 #define ADMHC_INTR_INIT \
60 ( ADMHC_INTR_MIE | ADMHC_INTR_INSM | ADMHC_INTR_FATI \
61 | ADMHC_INTR_RESI | ADMHC_INTR_TDC | ADMHC_INTR_BABI \
62 | ADMHC_INTR_7 | ADMHC_INTR_6 )
64 /*-------------------------------------------------------------------------*/
66 static const char hcd_name [] = "admhc-hcd";
68 #define STATECHANGE_DELAY msecs_to_jiffies(300)
72 static void admhc_dump(struct admhcd *ahcd, int verbose);
73 static int admhc_init(struct admhcd *ahcd);
74 static void admhc_stop(struct usb_hcd *hcd);
76 #include "adm5120-dbg.c"
77 #include "adm5120-mem.c"
78 #include "adm5120-pm.c"
79 #include "adm5120-hub.c"
80 #include "adm5120-q.c"
82 /*-------------------------------------------------------------------------*/
85 * queue up an urb for anything except the root hub
87 static int admhc_urb_enqueue(struct usb_hcd *hcd, struct usb_host_endpoint *ep,
88 struct urb *urb, gfp_t mem_flags)
90 struct admhcd *ahcd = hcd_to_admhcd(hcd);
92 struct urb_priv *urb_priv;
93 unsigned int pipe = urb->pipe;
98 #ifdef ADMHC_VERBOSE_DEBUG
99 spin_lock_irqsave(&ahcd->lock, flags);
100 urb_print(ahcd, urb, "ENQEUE", usb_pipein(pipe));
101 spin_unlock_irqrestore(&ahcd->lock, flags);
104 /* every endpoint has an ed, locate and maybe (re)initialize it */
105 ed = ed_get(ahcd, ep, urb->dev, pipe, urb->interval);
109 /* for the private part of the URB we need the number of TDs */
112 if (urb->transfer_buffer_length > TD_DATALEN_MAX)
113 /* td_submit_urb() doesn't yet handle these */
116 /* 1 TD for setup, 1 for ACK, plus ... */
118 if (urb->transfer_buffer_length)
122 /* one TD for every 4096 Bytes (can be upto 8K) */
123 td_cnt = urb->transfer_buffer_length / TD_DATALEN_MAX;
124 /* ... and for any remaining bytes ... */
125 if ((urb->transfer_buffer_length % TD_DATALEN_MAX) != 0)
127 /* ... and maybe a zero length packet to wrap it up */
130 else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
131 && (urb->transfer_buffer_length
132 % usb_maxpacket(urb->dev, pipe,
133 usb_pipeout (pipe))) == 0)
138 * for Interrupt IN/OUT transactions, each ED contains
140 * TODO: check transfer_buffer_length?
144 case PIPE_ISOCHRONOUS:
145 /* number of packets from URB */
146 td_cnt = urb->number_of_packets;
150 admhc_err(ahcd, "bad EP type %d", ed->type);
154 urb_priv = urb_priv_alloc(ahcd, td_cnt, mem_flags);
161 spin_lock_irqsave(&ahcd->lock, flags);
162 /* don't submit to a dead HC */
163 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
167 if (!HC_IS_RUNNING(hcd->state)) {
172 /* in case of unlink-during-submit */
173 spin_lock(&urb->lock);
174 if (urb->status != -EINPROGRESS) {
175 spin_unlock(&urb->lock);
176 urb->hcpriv = urb_priv;
177 finish_urb(ahcd, urb);
182 if (ed->type == PIPE_ISOCHRONOUS) {
183 if (ed->state == ED_NEW) {
184 u16 frame = admhc_frame_no(ahcd);
186 /* delay a few frames before the first TD */
187 frame += max_t (u16, 8, ed->interval);
188 frame &= ~(ed->interval - 1);
190 urb->start_frame = frame;
192 /* yes, only URB_ISO_ASAP is supported, and
193 * urb->start_frame is never used as input.
196 urb->start_frame = ed->last_iso + ed->interval;
199 urb->hcpriv = urb_priv;
200 td_submit_urb(ahcd, urb_priv->urb);
202 /* append it to the ED's queue */
203 list_add_tail(&urb_priv->pending, &ed->urb_pending);
205 /* schedule the ED */
206 ret = ed_schedule(ahcd, ed);
208 spin_unlock(&urb->lock);
211 urb_priv = urb->hcpriv;
212 urb_priv_free(ahcd, urb_priv);
215 spin_unlock_irqrestore(&ahcd->lock, flags);
220 * decouple the URB from the HC queues (TDs, urb_priv); it's
221 * already marked using urb->status. reporting is always done
222 * asynchronously, and we might be dealing with an urb that's
223 * partially transferred, or an ED with other urbs being unlinked.
225 static int admhc_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
227 struct admhcd *ahcd = hcd_to_admhcd(hcd);
235 spin_lock_irqsave(&ahcd->lock, flags);
237 #ifdef ADMHC_VERBOSE_DEBUG
238 urb_print(ahcd, urb, "DEQEUE", 1);
241 if (HC_IS_RUNNING(hcd->state)) {
242 /* Unless an IRQ completed the unlink while it was being
243 * handed to us, flag it for unlink and giveback, and force
244 * some upcoming INTR_SF to call finish_unlinks()
246 if (up->ed->urb_active != up) {
247 list_del(&up->pending);
248 finish_urb(ahcd, urb);
250 ed_start_deschedule(ahcd, up->ed);
254 * with HC dead, we won't respect hc queue pointers
255 * any more ... just clean up every urb's memory.
257 if (up->ed->urb_active != up) {
258 list_del(&up->pending);
259 finish_urb(ahcd, urb);
261 finish_urb(ahcd, urb);
262 up->ed->urb_active = NULL;
263 up->ed->state = ED_IDLE;
266 spin_unlock_irqrestore(&ahcd->lock, flags);
271 /*-------------------------------------------------------------------------*/
273 /* frees config/altsetting state for endpoints,
274 * including ED memory, dummy TD, and bulk/intr data toggle
276 static void admhc_endpoint_disable(struct usb_hcd *hcd,
277 struct usb_host_endpoint *ep)
279 struct admhcd *ahcd = hcd_to_admhcd(hcd);
281 struct ed *ed = ep->hcpriv;
282 unsigned limit = 1000;
284 /* ASSERT: any requests/urbs are being unlinked */
285 /* ASSERT: nobody can be submitting urbs for this any more */
290 #ifdef ADMHC_VERBOSE_DEBUG
291 spin_lock_irqsave(&ahcd->lock, flags);
292 admhc_dump_ed(ahcd, "EP-DISABLE", ed, 1);
293 spin_unlock_irqrestore(&ahcd->lock, flags);
297 spin_lock_irqsave(&ahcd->lock, flags);
299 if (!HC_IS_RUNNING(hcd->state)) {
301 ed->state = ED_UNLINK;
302 admhc_finish_unlinks(ahcd, 0);
306 case ED_UNLINK: /* wait for hw to finish? */
307 /* major IRQ delivery trouble loses INTR_SOFI too... */
309 admhc_warn(ahcd, "IRQ INTR_SOFI lossage\n");
312 spin_unlock_irqrestore(&ahcd->lock, flags);
313 schedule_timeout_uninterruptible(1);
316 case ED_NEW: /* fully unlinked */
317 if (list_empty(&ed->urb_pending)) {
318 td_free(ahcd, ed->dummy);
322 /* else FALL THROUGH */
324 /* caller was supposed to have unlinked any requests;
325 * that's not our job. can't recover; must leak ed.
327 admhc_err(ahcd, "leak ed %p (#%02x) %s act %p%s\n",
328 ed, ep->desc.bEndpointAddress,
329 ed_statestring(ed->state),
331 list_empty(&ed->urb_pending) ? "" : " (has urbs)");
337 spin_unlock_irqrestore(&ahcd->lock, flags);
341 static int admhc_get_frame_number(struct usb_hcd *hcd)
343 struct admhcd *ahcd = hcd_to_admhcd(hcd);
345 return admhc_frame_no(ahcd);
348 static void admhc_usb_reset(struct admhcd *ahcd)
350 admhc_dbg(ahcd, "usb reset\n");
351 ahcd->host_control = ADMHC_BUSS_RESET;
352 admhc_writel(ahcd, ahcd->host_control, &ahcd->regs->host_control);
355 /* admhc_shutdown forcibly disables IRQs and DMA, helping kexec and
356 * other cases where the next software may expect clean state from the
357 * "firmware". this is bus-neutral, unlike shutdown() methods.
360 admhc_shutdown(struct usb_hcd *hcd)
364 admhc_dbg(ahcd, "shutdown\n");
366 ahcd = hcd_to_admhcd(hcd);
367 admhc_intr_disable(ahcd, ADMHC_INTR_MIE);
368 admhc_dma_disable(ahcd);
369 admhc_usb_reset(ahcd);
372 /*-------------------------------------------------------------------------*
374 *-------------------------------------------------------------------------*/
376 static void admhc_eds_cleanup(struct admhcd *ahcd)
378 if (ahcd->ed_tails[PIPE_INTERRUPT]) {
379 ed_free(ahcd, ahcd->ed_tails[PIPE_INTERRUPT]);
380 ahcd->ed_tails[PIPE_INTERRUPT] = NULL;
383 if (ahcd->ed_tails[PIPE_ISOCHRONOUS]) {
384 ed_free(ahcd, ahcd->ed_tails[PIPE_ISOCHRONOUS]);
385 ahcd->ed_tails[PIPE_ISOCHRONOUS] = NULL;
388 if (ahcd->ed_tails[PIPE_CONTROL]) {
389 ed_free(ahcd, ahcd->ed_tails[PIPE_CONTROL]);
390 ahcd->ed_tails[PIPE_CONTROL] = NULL;
393 if (ahcd->ed_tails[PIPE_BULK]) {
394 ed_free(ahcd, ahcd->ed_tails[PIPE_BULK]);
395 ahcd->ed_tails[PIPE_BULK] = NULL;
398 ahcd->ed_head = NULL;
401 #define ED_DUMMY_INFO 0
403 static int admhc_eds_init(struct admhcd *ahcd)
407 ed = ed_create(ahcd, PIPE_INTERRUPT, ED_DUMMY_INFO);
411 ahcd->ed_tails[PIPE_INTERRUPT] = ed;
413 ed = ed_create(ahcd, PIPE_ISOCHRONOUS, ED_DUMMY_INFO);
417 ahcd->ed_tails[PIPE_ISOCHRONOUS] = ed;
418 ed->ed_prev = ahcd->ed_tails[PIPE_INTERRUPT];
419 ahcd->ed_tails[PIPE_INTERRUPT]->ed_next = ed;
420 ahcd->ed_tails[PIPE_INTERRUPT]->hwNextED = cpu_to_hc32(ahcd, ed->dma);
422 ed = ed_create(ahcd, PIPE_CONTROL, ED_DUMMY_INFO);
426 ahcd->ed_tails[PIPE_CONTROL] = ed;
427 ed->ed_prev = ahcd->ed_tails[PIPE_ISOCHRONOUS];
428 ahcd->ed_tails[PIPE_ISOCHRONOUS]->ed_next = ed;
429 ahcd->ed_tails[PIPE_ISOCHRONOUS]->hwNextED = cpu_to_hc32(ahcd, ed->dma);
431 ed = ed_create(ahcd, PIPE_BULK, ED_DUMMY_INFO);
435 ahcd->ed_tails[PIPE_BULK] = ed;
436 ed->ed_prev = ahcd->ed_tails[PIPE_CONTROL];
437 ahcd->ed_tails[PIPE_CONTROL]->ed_next = ed;
438 ahcd->ed_tails[PIPE_CONTROL]->hwNextED = cpu_to_hc32(ahcd, ed->dma);
440 ahcd->ed_head = ahcd->ed_tails[PIPE_INTERRUPT];
442 #ifdef ADMHC_VERBOSE_DEBUG
443 admhc_dump_ed(ahcd, "ed intr", ahcd->ed_tails[PIPE_INTERRUPT], 1);
444 admhc_dump_ed(ahcd, "ed isoc", ahcd->ed_tails[PIPE_ISOCHRONOUS], 1);
445 admhc_dump_ed(ahcd, "ed ctrl", ahcd->ed_tails[PIPE_CONTROL], 1);
446 admhc_dump_ed(ahcd, "ed bulk", ahcd->ed_tails[PIPE_BULK], 1);
452 admhc_eds_cleanup(ahcd);
456 /* init memory, and kick BIOS/SMM off */
458 static int admhc_init(struct admhcd *ahcd)
460 struct usb_hcd *hcd = admhcd_to_hcd(ahcd);
464 ahcd->regs = hcd->regs;
466 /* Disable HC interrupts */
467 admhc_intr_disable(ahcd, ADMHC_INTR_MIE);
469 /* Read the number of ports unless overridden */
470 if (ahcd->num_ports == 0)
471 ahcd->num_ports = admhc_read_rhdesc(ahcd) & ADMHC_RH_NUMP;
473 ret = admhc_mem_init(ahcd);
477 /* init dummy endpoints */
478 ret = admhc_eds_init(ahcd);
482 create_debug_files(ahcd);
491 /*-------------------------------------------------------------------------*/
493 /* Start an OHCI controller, set the BUS operational
494 * resets USB and controller
497 static int admhc_run(struct admhcd *ahcd)
500 int first = ahcd->fminterval == 0;
501 struct usb_hcd *hcd = admhcd_to_hcd(ahcd);
505 /* boot firmware should have set this up (5.1.1.3.1) */
507 temp = admhc_readl(ahcd, &ahcd->regs->fminterval);
508 ahcd->fminterval = temp & ADMHC_SFI_FI_MASK;
509 if (ahcd->fminterval != FI)
510 admhc_dbg(ahcd, "fminterval delta %d\n",
511 ahcd->fminterval - FI);
513 (FSLDP(ahcd->fminterval) << ADMHC_SFI_FSLDP_SHIFT);
514 /* also: power/overcurrent flags in rhdesc */
517 switch (ahcd->host_control & ADMHC_HC_BUSS) {
518 case ADMHC_BUSS_OPER:
521 case ADMHC_BUSS_SUSPEND:
523 case ADMHC_BUSS_RESUME:
524 ahcd->host_control = ADMHC_BUSS_RESUME;
525 temp = 10 /* msec wait */;
527 /* case ADMHC_BUSS_RESET: */
529 ahcd->host_control = ADMHC_BUSS_RESET;
530 temp = 50 /* msec wait */;
533 admhc_writel(ahcd, ahcd->host_control, &ahcd->regs->host_control);
536 temp = admhc_read_rhdesc(ahcd);
537 if (!(temp & ADMHC_RH_NPS)) {
538 /* power down each port */
539 for (temp = 0; temp < ahcd->num_ports; temp++)
540 admhc_write_portstatus(ahcd, temp, ADMHC_PS_CPP);
543 /* 2msec timelimit here means no irqs/preempt */
544 spin_lock_irq(&ahcd->lock);
546 admhc_writel(ahcd, ADMHC_CTRL_SR, &ahcd->regs->gencontrol);
547 temp = 30; /* ... allow extra time */
548 while ((admhc_readl(ahcd, &ahcd->regs->gencontrol) & ADMHC_CTRL_SR) != 0) {
550 spin_unlock_irq(&ahcd->lock);
551 admhc_err(ahcd, "USB HC reset timed out!\n");
557 /* enable HOST mode, before access any host specific register */
558 admhc_writel(ahcd, ADMHC_CTRL_UHFE, &ahcd->regs->gencontrol);
560 /* Tell the controller where the descriptor list is */
561 admhc_writel(ahcd, (u32)ahcd->ed_head->dma, &ahcd->regs->hosthead);
563 periodic_reinit(ahcd);
565 /* use rhsc irqs after khubd is fully initialized */
567 hcd->uses_new_polling = 1;
569 /* start controller operations */
570 ahcd->host_control = ADMHC_BUSS_OPER;
571 admhc_writel(ahcd, ahcd->host_control, &ahcd->regs->host_control);
572 hcd->state = HC_STATE_RUNNING;
575 while ((admhc_readl(ahcd, &ahcd->regs->host_control)
576 & ADMHC_HC_BUSS) != ADMHC_BUSS_OPER) {
578 spin_unlock_irq(&ahcd->lock);
579 admhc_err(ahcd, "unable to setup operational mode!\n");
587 /* wake on ConnectStatusChange, matching external hubs */
588 admhc_writel(ahcd, ADMHC_RH_DRWE, &ahcd->regs->rhdesc);
591 /* Choose the interrupts we care about now, others later on demand */
592 temp = ADMHC_INTR_INIT;
593 admhc_intr_ack(ahcd, ~0);
594 admhc_intr_enable(ahcd, temp);
596 admhc_writel(ahcd, ADMHC_RH_NPS | ADMHC_RH_LPSC, &ahcd->regs->rhdesc);
598 ahcd->next_statechange = jiffies + STATECHANGE_DELAY;
599 spin_unlock_irq(&ahcd->lock);
601 mdelay(ADMHC_POTPGT);
602 hcd->state = HC_STATE_RUNNING;
607 /*-------------------------------------------------------------------------*/
609 /* an interrupt happens */
611 static irqreturn_t admhc_irq(struct usb_hcd *hcd)
613 struct admhcd *ahcd = hcd_to_admhcd(hcd);
614 struct admhcd_regs __iomem *regs = ahcd->regs;
617 ints = admhc_readl(ahcd, ®s->int_status);
618 if (!(ints & ADMHC_INTR_INTA)) {
619 /* no unmasked interrupt status is set */
620 admhc_err(ahcd, "spurious interrupt %08x\n", ints);
624 ints &= admhc_readl(ahcd, ®s->int_enable);
626 admhc_err(ahcd, "hardware irq problems?\n");
630 if (ints & ADMHC_INTR_6) {
631 admhc_err(ahcd, "unknown interrupt 6\n");
635 if (ints & ADMHC_INTR_7) {
636 admhc_err(ahcd, "unknown interrupt 7\n");
640 if (ints & ADMHC_INTR_FATI) {
642 admhc_err(ahcd, "Fatal Error, controller disabled\n");
643 admhc_usb_reset(ahcd);
646 if (ints & ADMHC_INTR_BABI) {
648 admhc_err(ahcd, "Babble Detected\n");
649 admhc_usb_reset(ahcd);
652 if (ints & ADMHC_INTR_INSM) {
653 admhc_vdbg(ahcd, "Root Hub Status Change\n");
654 ahcd->next_statechange = jiffies + STATECHANGE_DELAY;
655 admhc_intr_ack(ahcd, ADMHC_INTR_RESI | ADMHC_INTR_INSM);
657 /* NOTE: Vendors didn't always make the same implementation
658 * choices for RHSC. Many followed the spec; RHSC triggers
659 * on an edge, like setting and maybe clearing a port status
660 * change bit. With others it's level-triggered, active
661 * until khubd clears all the port status change bits. We'll
662 * always disable it here and rely on polling until khubd
665 admhc_intr_disable(ahcd, ADMHC_INTR_INSM);
666 usb_hcd_poll_rh_status(hcd);
667 } else if (ints & ADMHC_INTR_RESI) {
668 /* For connect and disconnect events, we expect the controller
669 * to turn on RHSC along with RD. But for remote wakeup events
670 * this might not happen.
672 admhc_vdbg(ahcd, "Resume Detect\n");
673 admhc_intr_ack(ahcd, ADMHC_INTR_RESI);
675 if (ahcd->autostop) {
676 spin_lock(&ahcd->lock);
677 admhc_rh_resume(ahcd);
678 spin_unlock(&ahcd->lock);
680 usb_hcd_resume_root_hub(hcd);
683 if (ints & ADMHC_INTR_TDC) {
684 admhc_vdbg(ahcd, "Transfer Descriptor Complete\n");
685 if (HC_IS_RUNNING(hcd->state))
686 admhc_intr_disable(ahcd, ADMHC_INTR_TDC);
687 spin_lock(&ahcd->lock);
688 admhc_td_complete(ahcd);
689 spin_unlock(&ahcd->lock);
690 if (HC_IS_RUNNING(hcd->state))
691 admhc_intr_enable(ahcd, ADMHC_INTR_TDC);
694 if (ints & ADMHC_INTR_SO) {
695 /* could track INTR_SO to reduce available PCI/... bandwidth */
696 admhc_err(ahcd, "Schedule Overrun\n");
699 if (ints & ADMHC_INTR_SOFI) {
700 spin_lock(&ahcd->lock);
701 /* handle any pending ED removes */
702 admhc_finish_unlinks(ahcd, admhc_frame_no(ahcd));
703 spin_unlock(&ahcd->lock);
706 if (HC_IS_RUNNING(hcd->state)) {
707 admhc_intr_ack(ahcd, ints);
708 admhc_intr_enable(ahcd, ADMHC_INTR_MIE);
714 /*-------------------------------------------------------------------------*/
716 static void admhc_stop(struct usb_hcd *hcd)
718 struct admhcd *ahcd = hcd_to_admhcd(hcd);
722 flush_scheduled_work();
724 admhc_usb_reset(ahcd);
725 admhc_intr_disable(ahcd, ~0);
727 free_irq(hcd->irq, hcd);
730 remove_debug_files(ahcd);
731 admhc_eds_cleanup(ahcd);
732 admhc_mem_cleanup(ahcd);
735 /*-------------------------------------------------------------------------*/
737 #ifdef CONFIG_MIPS_ADM5120
738 #include "adm5120-drv.c"
739 #define PLATFORM_DRIVER usb_hcd_adm5120_driver
742 #if !defined(PLATFORM_DRIVER)
743 #error "missing bus glue for admhc-hcd"
746 #define DRIVER_INFO DRIVER_DESC " " DRIVER_VERSION
748 static int __init admhc_hcd_mod_init(void)
755 pr_info("%s: " DRIVER_INFO "\n", hcd_name);
756 pr_info("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
757 sizeof (struct ed), sizeof (struct td));
759 #ifdef PLATFORM_DRIVER
760 ret = platform_driver_register(&PLATFORM_DRIVER);
767 #ifdef PLATFORM_DRIVER
768 platform_driver_unregister(&PLATFORM_DRIVER);
773 module_init(admhc_hcd_mod_init);
775 static void __exit admhc_hcd_mod_exit(void)
777 platform_driver_unregister(&PLATFORM_DRIVER);
779 module_exit(admhc_hcd_mod_exit);
781 MODULE_AUTHOR(DRIVER_AUTHOR);
782 MODULE_DESCRIPTION(DRIVER_INFO);
783 MODULE_LICENSE("GPL");