2 * Copyright (c) 2000-2004 by David Brownell
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/config.h>
21 #ifdef CONFIG_USB_DEBUG
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include <linux/dmapool.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/ioport.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/smp_lock.h>
36 #include <linux/errno.h>
37 #include <linux/init.h>
38 #include <linux/timer.h>
39 #include <linux/list.h>
40 #include <linux/interrupt.h>
41 #include <linux/reboot.h>
42 #include <linux/usb.h>
43 #include <linux/moduleparam.h>
44 #include <linux/dma-mapping.h>
45 #ifdef CONFIG_ARCH_MOXACPU
46 #include <asm/arch/cpe/cpe.h>
47 #include <asm/arch/cpe_int.h>
48 #include <asm/arch-moxacpu/gpio.h>
51 #include "../core/hcd.h"
53 #include <asm/byteorder.h>
56 #include <asm/system.h>
57 #include <asm/unaligned.h>
59 //#define VICTOR_DEBUG
61 #define victor_printk(x...) printk(x)
63 #define victor_printk(x...)
64 #endif // VICTOR_DEBUG
65 /*-------------------------------------------------------------------------*/
68 * EHCI hc_driver implementation ... experimental, incomplete.
69 * Based on the final 1.0 register interface specification.
71 * USB 2.0 shows up in upcoming www.pcmcia.org technology.
72 * First was PCMCIA, like ISA; then CardBus, which is PCI.
73 * Next comes "CardBay", using USB 2.0 signals.
75 * Contains additional contributions by Brad Hards, Rory Bolt, and others.
76 * Special thanks to Intel and VIA for providing host controllers to
77 * test this driver on, and Cypress (including In-System Design) for
78 * providing early devices for those host controllers to talk to!
82 * 2004-05-10 Root hub and PCI suspend/resume support; remote wakeup. (db)
83 * 2004-02-24 Replace pci_* with generic dma_* API calls (dsaxena@plexity.net)
84 * 2003-12-29 Rewritten high speed iso transfer support (by Michal Sojka,
85 * <sojkam@centrum.cz>, updates by DB).
87 * 2002-11-29 Correct handling for hw async_next register.
88 * 2002-08-06 Handling for bulk and interrupt transfers is mostly shared;
89 * only scheduling is different, no arbitrary limitations.
90 * 2002-07-25 Sanity check PCI reads, mostly for better cardbus support,
91 * clean up HC run state handshaking.
92 * 2002-05-24 Preliminary FS/LS interrupts, using scheduling shortcuts
93 * 2002-05-11 Clear TT errors for FS/LS ctrl/bulk. Fill in some other
94 * missing pieces: enabling 64bit dma, handoff from BIOS/SMM.
95 * 2002-05-07 Some error path cleanups to report better errors; wmb();
96 * use non-CVS version id; better iso bandwidth claim.
97 * 2002-04-19 Control/bulk/interrupt submit no longer uses giveback() on
98 * errors in submit path. Bugfixes to interrupt scheduling/processing.
99 * 2002-03-05 Initial high-speed ISO support; reduce ITD memory; shift
100 * more checking to generic hcd framework (db). Make it work with
101 * Philips EHCI; reduce PCI traffic; shorten IRQ path (Rory Bolt).
102 * 2002-01-14 Minor cleanup; version synch.
103 * 2002-01-08 Fix roothub handoff of FS/LS to companion controllers.
104 * 2002-01-04 Control/Bulk queuing behaves.
106 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
107 * 2001-June Works with usb-storage and NEC EHCI on 2.4
110 #define DRIVER_VERSION "2004-May-10"
111 #define DRIVER_AUTHOR "David Brownell"
112 #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
114 static const char hcd_name
[] = "ehci_hcd";
117 #undef EHCI_VERBOSE_DEBUG
118 #undef EHCI_URB_TRACE
124 /* magic numbers that can affect system performance */
125 #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
126 #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
127 #define EHCI_TUNE_RL_TT 0
128 #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
129 #define EHCI_TUNE_MULT_TT 1
130 #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
132 #define EHCI_IAA_JIFFIES (HZ/100) /* arbitrary; ~10 msec */
133 #define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
134 #define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
135 #define EHCI_SHRINK_JIFFIES (HZ/200) /* async qh unlink delay */
137 /* Initial IRQ latency: lower than default */
138 static int log2_irq_thresh
= 0; // 0 to 6
139 module_param (log2_irq_thresh
, int, S_IRUGO
);
140 MODULE_PARM_DESC (log2_irq_thresh
, "log2 IRQ latency, 1-64 microframes");
142 #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
144 /*-------------------------------------------------------------------------*/
147 #include "ehci-dbg.c"
149 /*-------------------------------------------------------------------------*/
152 * handshake - spin reading hc until handshake completes or fails
153 * @ptr: address of hc register to be read
154 * @mask: bits to look at in result of read
155 * @done: value of those bits when handshake succeeds
156 * @usec: timeout in microseconds
158 * Returns negative errno, or zero on success
160 * Success happens when the "mask" bits have the specified value (hardware
161 * handshake done). There are two failure modes: "usec" have passed (major
162 * hardware flakeout), or the register reads as all-ones (hardware removed).
164 * That last failure should_only happen in cases like physical cardbus eject
165 * before driver shutdown. But it also seems to be caused by bugs in cardbus
166 * bridge shutdown: shutting down the bridge before the devices using it.
168 static int handshake (u32 __iomem
*ptr
, u32 mask
, u32 done
, int usec
)
173 result
= readl (ptr
);
174 if (result
== ~(u32
)0) /* card removed */
186 * hc states include: unknown, halted, ready, running
187 * transitional states are messy just now
188 * trying to avoid "running" unless urbs are active
189 * a "ready" hc can be finishing prefetched work
192 /* force HC to halt state from unknown (EHCI spec section 2.3) */
193 static int ehci_halt (struct ehci_hcd
*ehci
)
195 u32 temp
= readl (&ehci
->regs
->status
);
197 if ((temp
& STS_HALT
) != 0)
200 temp
= readl (&ehci
->regs
->command
);
202 writel (temp
, &ehci
->regs
->command
);
203 return handshake (&ehci
->regs
->status
, STS_HALT
, STS_HALT
, 16 * 125);
206 /* reset a non-running (STS_HALT == 1) controller */
207 static int ehci_reset (struct ehci_hcd
*ehci
)
209 u32 command
= readl (&ehci
->regs
->command
);
211 command
|= CMD_RESET
;
212 dbg_cmd (ehci
, "reset", command
);
213 writel (command
, &ehci
->regs
->command
);
214 ehci
->hcd
.state
= USB_STATE_HALT
;
215 ehci
->next_statechange
= jiffies
;
216 return handshake (&ehci
->regs
->command
, CMD_RESET
, 0, 250 * 1000);
219 /* idle the controller (from running) */
220 static void ehci_ready (struct ehci_hcd
*ehci
)
225 if (!HCD_IS_RUNNING (ehci
->hcd
.state
))
229 /* wait for any schedule enables/disables to take effect */
231 if (ehci
->async
->qh_next
.qh
)
233 if (ehci
->next_uframe
!= -1)
235 if (handshake (&ehci
->regs
->status
, STS_ASS
| STS_PSS
,
236 temp
, 16 * 125) != 0) {
237 ehci
->hcd
.state
= USB_STATE_HALT
;
241 /* then disable anything that's still active */
242 temp
= readl (&ehci
->regs
->command
);
243 temp
&= ~(CMD_ASE
| CMD_IAAD
| CMD_PSE
);
244 writel (temp
, &ehci
->regs
->command
);
246 /* hardware can take 16 microframes to turn off ... */
247 if (handshake (&ehci
->regs
->status
, STS_ASS
| STS_PSS
,
249 ehci
->hcd
.state
= USB_STATE_HALT
;
254 /*-------------------------------------------------------------------------*/
256 static void ehci_work(struct ehci_hcd
*ehci
, struct pt_regs
*regs
);
258 #include "ehci-hub.c"
259 #include "ehci-mem.c"
261 #include "ehci-sched.c"
263 /*-------------------------------------------------------------------------*/
265 static void ehci_watchdog (unsigned long param
)
267 struct ehci_hcd
*ehci
= (struct ehci_hcd
*) param
;
270 spin_lock_irqsave (&ehci
->lock
, flags
);
272 /* lost IAA irqs wedge things badly; seen with a vt8235 */
274 u32 status
= readl (&ehci
->regs
->status
);
276 if (status
& STS_IAA
) {
277 ehci_vdbg (ehci
, "lost IAA\n");
278 COUNT (ehci
->stats
.lost_iaa
);
279 writel (STS_IAA
, &ehci
->regs
->status
);
280 ehci
->reclaim_ready
= 1;
284 /* stop async processing after it's idled a bit */
285 if (test_bit (TIMER_ASYNC_OFF
, &ehci
->actions
))
286 start_unlink_async (ehci
, ehci
->async
);
288 /* ehci could run by timer, without IRQs ... */
289 ehci_work (ehci
, NULL
);
291 spin_unlock_irqrestore (&ehci
->lock
, flags
);
296 #ifndef CONFIG_ARCH_MOXACPU // add by Victor Yu. 03-08-2006
297 /* EHCI 0.96 (and later) section 5.1 says how to kick BIOS/SMM/...
298 * off the controller (maybe it can boot from highspeed USB disks).
300 static int bios_handoff (struct ehci_hcd
*ehci
, int where
, u32 cap
)
302 if (cap
& (1 << 16)) {
304 struct pci_dev
*pdev
= to_pci_dev(ehci
->hcd
.self
.controller
);
306 /* request handoff to OS */
308 pci_write_config_dword(pdev
, where
, cap
);
310 /* and wait a while for it to happen */
314 pci_read_config_dword(pdev
, where
, &cap
);
315 } while ((cap
& (1 << 16)) && msec
);
316 if (cap
& (1 << 16)) {
317 ehci_err (ehci
, "BIOS handoff failed (%d, %04x)\n",
319 // some BIOS versions seem buggy...
321 ehci_warn (ehci
, "continuing after BIOS bug...\n");
324 ehci_dbg (ehci
, "BIOS handoff succeeded\n");
328 #endif // CONFIG_ARCH_MOXACPU
333 ehci_reboot (struct notifier_block
*self
, unsigned long code
, void *null
)
335 struct ehci_hcd
*ehci
;
337 ehci
= container_of (self
, struct ehci_hcd
, reboot_notifier
);
339 /* make BIOS/etc use companion controller during reboot */
340 #ifndef CONFIG_ARCH_MOXACPU // add by Victor Yu. 11-15-2005
341 writel (0, &ehci
->regs
->configured_flag
);
342 #endif // CONFIG_ARCH_MOXACPU
347 /* called by khubd or root hub init threads */
349 static int ehci_hc_reset (struct usb_hcd
*hcd
)
351 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
353 unsigned count
= 256/4;
355 spin_lock_init (&ehci
->lock
);
357 ehci
->caps
= hcd
->regs
;
358 ehci
->regs
= (hcd
->regs
+
359 HC_LENGTH (readl (&ehci
->caps
->hc_capbase
)));
360 victor_printk("ehci_hc_reset: ehci->caps=0x%p, ehci->regs=0x%p\n", ehci
->caps
, ehci
->regs
);
361 dbg_hcs_params (ehci
, "reset");
362 dbg_hcc_params (ehci
, "reset");
365 #ifndef CONFIG_ARCH_MOXACPU // add by Victor Yu. 03-08-2006
366 /* EHCI 0.96 and later may have "extended capabilities" */
367 if (hcd
->self
.controller
->bus
== &pci_bus_type
)
368 temp
= HCC_EXT_CAPS (readl (&ehci
->caps
->hcc_params
));
371 while (temp
&& count
--) {
374 pci_read_config_dword (to_pci_dev(ehci
->hcd
.self
.controller
),
376 ehci_dbg (ehci
, "capability %04x at %02x\n", cap
, temp
);
377 switch (cap
& 0xff) {
378 case 1: /* BIOS/SMM/... handoff */
379 if (bios_handoff (ehci
, temp
, cap
) != 0)
382 case 0x0a: /* appendix C */
383 ehci_dbg (ehci
, "debug registers, BAR %d offset %d\n",
384 (cap
>> 29) & 0x07, (cap
>> 16) & 0x0fff);
386 case 0: /* illegal reserved capability */
387 ehci_warn (ehci
, "illegal capability!\n");
390 default: /* unknown */
393 temp
= (cap
>> 8) & 0xff;
396 ehci_err (ehci
, "bogus capabilities ... PCI problems!\n");
399 #endif // CONFIG_ARCH_MOXACPU
402 /* cache this readonly data; minimize PCI reads */
403 ehci
->hcs_params
= readl (&ehci
->caps
->hcs_params
);
405 /* force HC to halt state */
406 return ehci_halt (ehci
);
409 static int ehci_start (struct usb_hcd
*hcd
)
411 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
413 struct usb_device
*udev
;
419 init_timer (&ehci
->watchdog
);
420 ehci
->watchdog
.function
= ehci_watchdog
;
421 ehci
->watchdog
.data
= (unsigned long) ehci
;
424 * hw default: 1K periodic list heads, one per frame.
425 * periodic_size can shrink by USBCMD update if hcc_params allows.
427 ehci
->periodic_size
= DEFAULT_I_TDPS
;
428 if ((retval
= ehci_mem_init (ehci
, GFP_KERNEL
)) < 0)
431 /* controllers may cache some of the periodic schedule ... */
432 hcc_params
= readl (&ehci
->caps
->hcc_params
);
433 if (HCC_ISOC_CACHE (hcc_params
)) // full frame cache
435 else // N microframes cached
436 ehci
->i_thresh
= 2 + HCC_ISOC_THRES (hcc_params
);
438 ehci
->reclaim
= NULL
;
439 ehci
->next_uframe
= -1;
441 /* controller state: unknown --> reset */
442 #ifdef CONFIG_ARCH_MOXACPU
443 temp
= readl(&ehci
->regs
->bus_mon
) & ~VBUS_OFF
;
444 #if 0 // add by Victor Yu. 01-03-2006, because the USB IP has bugs
445 // 01-10-2006, change to wirte command CMD_RUN when the device disconnected
446 temp
|= FORCE_FULL_SPEED
;
447 temp
&= ~FORCE_HIGH_SPEED
;
449 writel(temp
, &ehci
->regs
->bus_mon
);
450 // wait the USB host ready
451 while ( !(readl(&ehci
->regs
->bus_mon
) & VBUS_INVLD
) );
454 /* EHCI spec section 4.1 */
455 if ((retval
= ehci_reset (ehci
)) != 0) {
456 victor_printk("ehci_start: reset host controller fail !\n");
457 ehci_mem_cleanup (ehci
);
460 writel (ehci
->periodic_dma
, &ehci
->regs
->frame_list
);
463 #ifndef CONFIG_ARCH_MOXACPU // add by Victor Yu. 03-08-2006
464 if (hcd
->self
.controller
->bus
== &pci_bus_type
) {
465 struct pci_dev
*pdev
;
468 pdev
= to_pci_dev(hcd
->self
.controller
);
470 /* Serial Bus Release Number is at PCI 0x60 offset */
471 pci_read_config_byte(pdev
, 0x60, &sbrn
);
473 /* port wake capability, reported by boot firmware */
474 pci_read_config_word(pdev
, 0x62, &port_wake
);
475 hcd
->can_wakeup
= (port_wake
& 1) != 0;
477 /* help hc dma work well with cachelines */
480 /* chip-specific init */
481 switch (pdev
->vendor
) {
482 case PCI_VENDOR_ID_ARC
:
483 if (pdev
->device
== PCI_DEVICE_ID_ARC_EHCI
)
484 ehci
->is_arc_rh_tt
= 1;
489 #endif // CONFIG_ARCH_MOXACPU
493 * dedicate a qh for the async ring head, since we couldn't unlink
494 * a 'real' qh without stopping the async schedule [4.8]. use it
495 * as the 'reclamation list head' too.
496 * its dummy is used in hw_alt_next of many tds, to prevent the qh
497 * from automatically advancing to the next td after short reads.
499 ehci
->async
->qh_next
.qh
= NULL
;
500 ehci
->async
->hw_next
= QH_NEXT (ehci
->async
->qh_dma
);
501 ehci
->async
->hw_info1
= cpu_to_le32 (QH_HEAD
);
502 ehci
->async
->hw_token
= cpu_to_le32 (QTD_STS_HALT
);
503 ehci
->async
->hw_qtd_next
= EHCI_LIST_END
;
504 ehci
->async
->qh_state
= QH_STATE_LINKED
;
505 ehci
->async
->hw_alt_next
= QTD_NEXT (ehci
->async
->dummy
->qtd_dma
);
506 writel ((u32
)ehci
->async
->qh_dma
, &ehci
->regs
->async_next
);
509 * hcc_params controls whether ehci->regs->segment must (!!!)
510 * be used; it constrains QH/ITD/SITD and QTD locations.
511 * pci_pool consistent memory always uses segment zero.
512 * streaming mappings for I/O buffers, like pci_map_single(),
513 * can return segments above 4GB, if the device allows.
515 * NOTE: the dma mask is visible through dma_supported(), so
516 * drivers can pass this info along ... like NETIF_F_HIGHDMA,
517 * Scsi_Host.highmem_io, and so forth. It's readonly to all
518 * host side drivers though.
520 if (HCC_64BIT_ADDR (hcc_params
)) {
521 writel (0, &ehci
->regs
->segment
);
523 // this is deeply broken on almost all architectures
524 if (!pci_set_dma_mask (to_pci_dev(ehci
->hcd
.self
.controller
), 0xffffffffffffffffULL
))
525 ehci_info (ehci
, "enabled 64bit PCI DMA\n");
529 /* clear interrupt enables, set irq latency */
530 temp
= readl (&ehci
->regs
->command
) & 0x0fff;
531 if (log2_irq_thresh
< 0 || log2_irq_thresh
> 6)
533 temp
|= 1 << (16 + log2_irq_thresh
);
534 // if hc can park (ehci >= 0.96), default is 3 packets per async QH
535 if (HCC_PGM_FRAMELISTLEN (hcc_params
)) {
536 /* periodic schedule size can be smaller than default */
538 temp
|= (EHCI_TUNE_FLS
<< 2);
539 switch (EHCI_TUNE_FLS
) {
540 case 0: ehci
->periodic_size
= 1024; break;
541 case 1: ehci
->periodic_size
= 512; break;
542 case 2: ehci
->periodic_size
= 256; break;
546 temp
&= ~(CMD_IAAD
| CMD_ASE
| CMD_PSE
),
547 // Philips, Intel, and maybe others need CMD_RUN before the
548 // root hub will detect new devices (why?); NEC doesn't
550 writel (temp
, &ehci
->regs
->command
);
551 dbg_cmd (ehci
, "init", temp
);
553 /* set async sleep time = 10 us ... ? */
555 /* wire up the root hub */
556 bus
= hcd_to_bus (hcd
);
557 udev
= usb_alloc_dev (NULL
, bus
, 0);
560 ehci_mem_cleanup (ehci
);
565 * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
566 * are explicitly handed to companion controller(s), so no TT is
567 * involved with the root hub. (Except where one is integrated,
568 * and there's no companion controller unless maybe for USB OTG.)
570 ehci
->reboot_notifier
.notifier_call
= ehci_reboot
;
571 register_reboot_notifier (&ehci
->reboot_notifier
);
573 ehci
->hcd
.state
= USB_STATE_RUNNING
;
574 #ifndef CONFIG_ARCH_MOXACPU // add by Victor Yu. 11-15-2005
575 writel (FLAG_CF
, &ehci
->regs
->configured_flag
);
576 #endif // CONFIG_ARCH_MOXACPU
577 readl (&ehci
->regs
->command
); /* unblock posted write */
579 temp
= HC_VERSION(readl (&ehci
->caps
->hc_capbase
));
581 "USB %x.%x enabled, EHCI %x.%02x, driver %s\n",
582 ((sbrn
& 0xf0)>>4), (sbrn
& 0x0f),
583 temp
>> 8, temp
& 0xff, DRIVER_VERSION
);
586 * From here on, khubd concurrently accesses the root
587 * hub; drivers will be talking to enumerated devices.
589 * Before this point the HC was idle/ready. After, khubd
590 * and device drivers may start it running.
592 udev
->speed
= USB_SPEED_HIGH
;
593 victor_printk("ehci_start-0: status=0x%x,inter_enable=0x%x,port_status=0x%x\n", readl(&ehci
->regs
->status
), readl(&ehci
->regs
->intr_enable
), readl(&ehci
->regs
->port_status
[0]));
594 if (hcd_register_root (udev
, hcd
) != 0) {
595 if (hcd
->state
== USB_STATE_RUNNING
)
603 writel (INTR_MASK
, &ehci
->regs
->intr_enable
); /* Turn On Interrupts */
605 create_debug_files (ehci
);
606 victor_printk("ehci_start: status=0x%x,inter_enable=0x%x,port_status=0x%x\n", readl(&ehci
->regs
->status
), readl(&ehci
->regs
->intr_enable
), readl(&ehci
->regs
->port_status
[0]));
611 /* always called by thread; normally rmmod */
613 static void ehci_stop (struct usb_hcd
*hcd
)
615 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
618 ehci_dbg (ehci
, "stop\n");
620 /* no more interrupts ... */
621 if (hcd
->state
== USB_STATE_RUNNING
)
623 if (in_interrupt ()) { /* must not happen!! */
624 ehci_err (ehci
, "stopped in_interrupt!\n");
627 del_timer_sync (&ehci
->watchdog
);
629 /* Turn off port power on all root hub ports. */
630 rh_ports
= HCS_N_PORTS (ehci
->hcs_params
);
631 for (port
= 1; port
<= rh_ports
; port
++) {
632 ehci_hub_control(hcd
, ClearPortFeature
, USB_PORT_FEAT_POWER
,
637 writel (0, &ehci
->regs
->intr_enable
);
639 /* let companion controllers work when we aren't */
640 #ifndef CONFIG_ARCH_MOXACPU // add by Victor Yu. 11-15-2005
641 writel (0, &ehci
->regs
->configured_flag
);
642 #endif // CONFIG_ARCH_MOXACPU
643 unregister_reboot_notifier (&ehci
->reboot_notifier
);
645 remove_debug_files (ehci
);
647 /* root hub is shut down separately (first, when possible) */
648 spin_lock_irq (&ehci
->lock
);
650 ehci_work (ehci
, NULL
);
651 spin_unlock_irq (&ehci
->lock
);
652 ehci_mem_cleanup (ehci
);
655 ehci_dbg (ehci
, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
656 ehci
->stats
.normal
, ehci
->stats
.error
, ehci
->stats
.reclaim
,
657 ehci
->stats
.lost_iaa
);
658 ehci_dbg (ehci
, "complete %ld unlink %ld\n",
659 ehci
->stats
.complete
, ehci
->stats
.unlink
);
662 dbg_status (ehci
, "ehci_stop completed", readl (&ehci
->regs
->status
));
665 static int ehci_get_frame (struct usb_hcd
*hcd
)
667 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
668 return (readl (&ehci
->regs
->frame_index
) >> 3) % ehci
->periodic_size
;
671 /*-------------------------------------------------------------------------*/
675 /* suspend/resume, section 4.3 */
677 /* These routines rely on PCI to handle powerdown and wakeup, and
678 * transceivers that don't need any software attention to set up
679 * the right sort of wakeup.
682 static int ehci_suspend (struct usb_hcd
*hcd
, u32 state
)
684 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
686 while (time_before (jiffies
, ehci
->next_statechange
))
689 #ifdef CONFIG_USB_SUSPEND
690 (void) usb_suspend_device (hcd
->self
.root_hub
, state
);
692 /* FIXME lock root hub */
693 (void) ehci_hub_suspend (hcd
);
696 // save (PCI) FLADJ in case of Vaux power loss
701 static int ehci_resume (struct usb_hcd
*hcd
)
703 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
706 // maybe restore (PCI) FLADJ
708 while (time_before (jiffies
, ehci
->next_statechange
))
711 #ifdef CONFIG_USB_SUSPEND
712 retval
= usb_resume_device (hcd
->self
.root_hub
);
714 /* FIXME lock root hub */
715 retval
= ehci_hub_resume (hcd
);
718 hcd
->self
.controller
->power
.power_state
= 0;
724 /*-------------------------------------------------------------------------*/
727 * ehci_work is called from some interrupts, timers, and so on.
728 * it calls driver completion functions, after dropping ehci->lock.
730 static void ehci_work (struct ehci_hcd
*ehci
, struct pt_regs
*regs
)
732 timer_action_done (ehci
, TIMER_IO_WATCHDOG
);
733 if (ehci
->reclaim_ready
)
734 end_unlink_async (ehci
, regs
);
736 /* another CPU may drop ehci->lock during a schedule scan while
737 * it reports urb completions. this flag guards against bogus
738 * attempts at re-entrant schedule scanning.
743 scan_async (ehci
, regs
);
744 if (ehci
->next_uframe
!= -1)
745 scan_periodic (ehci
, regs
);
748 /* the IO watchdog guards against hardware or driver bugs that
749 * misplace IRQs, and should let us run completely without IRQs.
750 * such lossage has been observed on both VT6202 and VT8235.
752 if ((ehci
->async
->qh_next
.ptr
!= 0) || (ehci
->periodic_sched
!= 0))
753 timer_action (ehci
, TIMER_IO_WATCHDOG
);
756 /*-------------------------------------------------------------------------*/
758 static irqreturn_t
ehci_irq (struct usb_hcd
*hcd
, struct pt_regs
*regs
)
760 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
764 spin_lock (&ehci
->lock
);
766 status
= readl (&ehci
->regs
->status
);
768 /* e.g. cardbus physical eject */
769 if (status
== ~(u32
) 0) {
770 ehci_dbg (ehci
, "device removed\n");
775 if (!status
) { /* irq sharing? */
776 spin_unlock(&ehci
->lock
);
780 /* clear (just) interrupts */
781 writel (status
, &ehci
->regs
->status
);
782 readl (&ehci
->regs
->command
); /* unblock posted write */
785 //#ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 01-06-2006, hardware has bug
787 if ( (status
& STS_PCD
) && !(readl(&ehci
->regs
->port_status
[0]) & PORT_CONNECT
) ) {
789 printk("ehci usb reset PHY, address = 0x%p\n", &ehci
->regs
->phy_ctrl
);
790 temp
= readl(&ehci
->regs
->phy_ctrl
);
792 writel(temp
, &ehci
->regs
->phy_ctrl
);
794 writel(temp
, &ehci
->regs
->phy_ctrl
);
798 #ifdef EHCI_VERBOSE_DEBUG
799 /* unrequested/ignored: Frame List Rollover */
800 dbg_status (ehci
, "irq", status
);
803 /* INT, ERR, and IAA interrupt rates can be throttled */
805 /* normal [4.15.1.2] or error [4.15.1.1] completion */
806 if (likely ((status
& (STS_INT
|STS_ERR
)) != 0)) {
807 if (likely ((status
& STS_ERR
) == 0))
808 COUNT (ehci
->stats
.normal
);
810 COUNT (ehci
->stats
.error
);
814 /* complete the unlinking of some qh [4.15.2.3] */
815 if (status
& STS_IAA
) {
816 COUNT (ehci
->stats
.reclaim
);
817 ehci
->reclaim_ready
= 1;
821 /* remote wakeup [4.3.1] */
822 if ((status
& STS_PCD
) && ehci
->hcd
.remote_wakeup
) {
823 unsigned i
= HCS_N_PORTS (ehci
->hcs_params
);
825 /* resume root hub? */
826 status
= readl (&ehci
->regs
->command
);
827 if (!(status
& CMD_RUN
))
828 writel (status
| CMD_RUN
, &ehci
->regs
->command
);
831 status
= readl (&ehci
->regs
->port_status
[i
]);
832 if (status
& PORT_OWNER
)
834 if (!(status
& PORT_RESUME
)
835 || ehci
->reset_done
[i
] != 0)
838 /* start 20 msec resume signaling from this port,
839 * and make khubd collect PORT_STAT_C_SUSPEND to
840 * stop that signaling.
842 ehci
->reset_done
[i
] = jiffies
+ msecs_to_jiffies (20);
843 mod_timer (&ehci
->hcd
.rh_timer
,
844 ehci
->reset_done
[i
] + 1);
845 ehci_dbg (ehci
, "port %d remote wakeup\n", i
+ 1);
849 /* PCI errors [4.15.2.4] */
850 if (unlikely ((status
& STS_FATAL
) != 0)) {
851 ehci_err (ehci
, "fatal error\n");
854 /* generic layer kills/unlinks all urbs, then
855 * uses ehci_stop to clean up the rest
861 ehci_work (ehci
, regs
);
863 spin_unlock (&ehci
->lock
);
867 /*-------------------------------------------------------------------------*/
870 * non-error returns are a promise to giveback() the urb later
871 * we drop ownership so next owner (or urb unlink) can get it
873 * urb + dev is in hcd.self.controller.urb_list
874 * we're queueing TDs onto software and hardware lists
876 * hcd-specific init for hcpriv hasn't been done yet
878 * NOTE: control, bulk, and interrupt share the same code to append TDs
879 * to a (possibly active) QH, and the same QH scanning code.
881 static int ehci_urb_enqueue (
886 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
887 struct list_head qtd_list
;
889 INIT_LIST_HEAD (&qtd_list
);
891 switch (usb_pipetype (urb
->pipe
)) {
892 // case PIPE_CONTROL:
895 if (!qh_urb_transaction (ehci
, urb
, &qtd_list
, mem_flags
))
897 return submit_async (ehci
, urb
, &qtd_list
, mem_flags
);
900 if (!qh_urb_transaction (ehci
, urb
, &qtd_list
, mem_flags
))
902 return intr_submit (ehci
, urb
, &qtd_list
, mem_flags
);
904 case PIPE_ISOCHRONOUS
:
905 if (urb
->dev
->speed
== USB_SPEED_HIGH
)
906 return itd_submit (ehci
, urb
, mem_flags
);
908 return sitd_submit (ehci
, urb
, mem_flags
);
912 /* remove from hardware lists
913 * completions normally happen asynchronously
916 static int ehci_urb_dequeue (struct usb_hcd
*hcd
, struct urb
*urb
)
918 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
922 spin_lock_irqsave (&ehci
->lock
, flags
);
923 switch (usb_pipetype (urb
->pipe
)) {
924 // case PIPE_CONTROL:
927 qh
= (struct ehci_qh
*) urb
->hcpriv
;
931 /* if we need to use IAA and it's busy, defer */
932 if (qh
->qh_state
== QH_STATE_LINKED
934 && HCD_IS_RUNNING (ehci
->hcd
.state
)
936 struct ehci_qh
*last
;
938 for (last
= ehci
->reclaim
;
940 last
= last
->reclaim
)
942 qh
->qh_state
= QH_STATE_UNLINK_WAIT
;
945 /* bypass IAA if the hc can't care */
946 } else if (!HCD_IS_RUNNING (ehci
->hcd
.state
) && ehci
->reclaim
)
947 end_unlink_async (ehci
, NULL
);
949 /* something else might have unlinked the qh by now */
950 if (qh
->qh_state
== QH_STATE_LINKED
)
951 start_unlink_async (ehci
, qh
);
955 qh
= (struct ehci_qh
*) urb
->hcpriv
;
958 if (qh
->qh_state
== QH_STATE_LINKED
) {
959 /* messy, can spin or block a microframe ... */
960 intr_deschedule (ehci
, qh
, 1);
961 /* qh_state == IDLE */
963 qh_completions (ehci
, qh
, NULL
);
965 /* reschedule QH iff another request is queued */
966 if (!list_empty (&qh
->qtd_list
)
967 && HCD_IS_RUNNING (ehci
->hcd
.state
)) {
970 status
= qh_schedule (ehci
, qh
);
971 spin_unlock_irqrestore (&ehci
->lock
, flags
);
974 // shouldn't happen often, but ...
975 // FIXME kill those tds' urbs
976 err ("can't reschedule qh %p, err %d",
983 case PIPE_ISOCHRONOUS
:
986 // wait till next completion, do it then.
987 // completion irqs can wait up to 1024 msec,
990 spin_unlock_irqrestore (&ehci
->lock
, flags
);
994 /*-------------------------------------------------------------------------*/
996 // bulk qh holds the data toggle
999 ehci_endpoint_disable (struct usb_hcd
*hcd
, struct hcd_dev
*dev
, int ep
)
1001 struct ehci_hcd
*ehci
= hcd_to_ehci (hcd
);
1003 unsigned long flags
;
1006 /* ASSERT: any requests/urbs are being unlinked */
1007 /* ASSERT: nobody can be submitting urbs for this any more */
1009 epnum
= ep
& USB_ENDPOINT_NUMBER_MASK
;
1010 if (epnum
!= 0 && (ep
& USB_DIR_IN
))
1014 spin_lock_irqsave (&ehci
->lock
, flags
);
1015 qh
= (struct ehci_qh
*) dev
->ep
[epnum
];
1019 /* endpoints can be iso streams. for now, we don't
1020 * accelerate iso completions ... so spin a while.
1022 if (qh
->hw_info1
== 0) {
1023 ehci_vdbg (ehci
, "iso delay\n");
1027 if (!HCD_IS_RUNNING (ehci
->hcd
.state
))
1028 qh
->qh_state
= QH_STATE_IDLE
;
1029 switch (qh
->qh_state
) {
1030 case QH_STATE_UNLINK
: /* wait for hw to finish? */
1032 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1033 set_current_state (TASK_UNINTERRUPTIBLE
);
1034 schedule_timeout (1);
1036 case QH_STATE_IDLE
: /* fully unlinked */
1037 if (list_empty (&qh
->qtd_list
)) {
1041 /* else FALL THROUGH */
1043 /* caller was supposed to have unlinked any requests;
1044 * that's not our job. just leak this memory.
1046 ehci_err (ehci
, "qh %p (#%d) state %d%s\n",
1047 qh
, epnum
, qh
->qh_state
,
1048 list_empty (&qh
->qtd_list
) ? "" : "(has tds)");
1051 dev
->ep
[epnum
] = NULL
;
1053 spin_unlock_irqrestore (&ehci
->lock
, flags
);
1057 /*-------------------------------------------------------------------------*/
1059 static const struct hc_driver ehci_driver
= {
1060 .description
= hcd_name
,
1063 * generic hardware linkage
1066 .flags
= HCD_MEMORY
| HCD_USB2
,
1069 * basic lifecycle operations
1071 .reset
= ehci_hc_reset
,
1072 .start
= ehci_start
,
1074 .suspend
= ehci_suspend
,
1075 .resume
= ehci_resume
,
1080 * memory lifecycle (except per-request)
1082 .hcd_alloc
= ehci_hcd_alloc
,
1083 .hcd_free
= ehci_hcd_free
,
1086 * managing i/o requests and associated device resources
1088 .urb_enqueue
= ehci_urb_enqueue
,
1089 .urb_dequeue
= ehci_urb_dequeue
,
1090 .endpoint_disable
= ehci_endpoint_disable
,
1093 * scheduling support
1095 .get_frame_number
= ehci_get_frame
,
1100 .hub_status_data
= ehci_hub_status_data
,
1101 .hub_control
= ehci_hub_control
,
1102 .hub_suspend
= ehci_hub_suspend
,
1103 .hub_resume
= ehci_hub_resume
,
1106 /*-------------------------------------------------------------------------*/
1108 /* EHCI 1.0 doesn't require PCI */
1112 #ifndef CONFIG_ARCH_MOXACPU // add by Victor Yu. 11-15-2005
1113 /* PCI driver selection metadata; PCI hotplugging uses this */
1114 static const struct pci_device_id pci_ids
[] = { {
1115 /* handle any USB 2.0 EHCI controller */
1116 PCI_DEVICE_CLASS(((PCI_CLASS_SERIAL_USB
<< 8) | 0x20), ~0),
1117 .driver_data
= (unsigned long) &ehci_driver
,
1119 { /* end: all zeroes */ }
1121 MODULE_DEVICE_TABLE (pci
, pci_ids
);
1123 /* pci driver glue; this is a "new style" PCI driver module */
1124 static struct pci_driver ehci_pci_driver
= {
1125 .name
= (char *) hcd_name
,
1126 .id_table
= pci_ids
,
1128 .probe
= usb_hcd_pci_probe
,
1129 .remove
= usb_hcd_pci_remove
,
1132 .suspend
= usb_hcd_pci_suspend
,
1133 .resume
= usb_hcd_pci_resume
,
1136 #endif // CONFIG_ARCH_MOXACPU
1141 #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
1143 MODULE_DESCRIPTION (DRIVER_INFO
);
1144 MODULE_AUTHOR (DRIVER_AUTHOR
);
1145 MODULE_LICENSE ("GPL");
1147 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 11-08-2005
1148 static void usb_hcd_mcpu_release(struct usb_bus
*bus
)
1150 struct usb_hcd
*hcd
= bus
->hcpriv
;
1153 hcd
->driver
->hcd_free(hcd
);
1156 static void usb_hcd_mcpu_remove(struct usb_hcd
*hcd
, struct platform_device
*pdev
)
1160 dev_info (hcd
->self
.controller
, "remove, state %x\n", hcd
->state
);
1162 if (in_interrupt ())
1165 if (HCD_IS_RUNNING (hcd
->state
))
1166 hcd
->state
= USB_STATE_QUIESCING
;
1168 dev_dbg (hcd
->self
.controller
, "roothub graceful disconnect\n");
1169 usb_disconnect (&hcd
->self
.root_hub
);
1171 hcd
->driver
->stop (hcd
);
1172 hcd_buffer_destroy (hcd
);
1173 hcd
->state
= USB_STATE_HALT
;
1175 free_irq (hcd
->irq
, hcd
);
1177 release_mem_region(pdev
->resource
[0].start
,
1178 pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1);
1180 usb_deregister_bus (&hcd
->self
);
1183 static int usb_hcd_mcpu_probe(const struct hc_driver
*driver
, struct platform_device
*pdev
)
1185 struct usb_hcd
*hcd
;
1187 victor_printk("mcpu_usb_host_probe test01\n");
1192 victor_printk("mcpu_usb_host_probe test02\n");
1193 if (pdev
->num_resources
!= 2) {
1194 printk(KERN_ERR
"ehci probe: invalid num_resources: %i\n", pdev
->num_resources
);
1197 if (pdev
->resource
[0].flags
!= IORESOURCE_MEM
||
1198 pdev
->resource
[1].flags
!= IORESOURCE_IRQ
) {
1199 printk(KERN_ERR
"ehci probe: invalid resource type\n");
1202 if (!request_mem_region(pdev
->resource
[0].start
,
1203 pdev
->resource
[0].end
- pdev
->resource
[0].start
+ 1, hcd_name
)) {
1204 dev_dbg(&pdev
->dev
, "request_mem_region failed\n");
1207 victor_printk("mcpu_usb_host_probe test03\n");
1210 // driver->reset(), later on, will transfer device from
1211 // control by SMM/BIOS to control by Linux (if needed)
1213 hcd
= driver
->hcd_alloc ();
1215 dev_dbg(&pdev
->dev
, "hcd_alloc failed\n");
1219 release_mem_region (CPE_USBHOST_BASE
, SZ_4K
);
1223 victor_printk("mcpu_usb_host_probe test05\n");
1224 // hcd zeroed everything
1225 dev_set_drvdata(&pdev
->dev
, hcd
);
1226 hcd
->regs
= (void *)pdev
->resource
[0].start
;;
1228 hcd
->driver
= (struct hc_driver
*)driver
;
1229 hcd
->description
= driver
->description
;
1230 hcd
->irq
= pdev
->resource
[1].start
;
1231 hcd
->self
.controller
= &pdev
->dev
;
1232 hcd
->self
.bus_name
= pdev
->dev
.bus_id
;
1233 hcd
->product_desc
= "Moxa CPU EHCI";
1235 victor_printk("mcpu_usb_host_probe test06\n");
1236 if ((retval
= hcd_buffer_create (hcd
)) != 0) {
1238 driver
->hcd_free (hcd
);
1239 dev_set_drvdata(&pdev
->dev
, NULL
);
1242 victor_printk("mcpu_usb_host_probe test07\n");
1244 dev_info (hcd
->self
.controller
, "%s\n", hcd
->product_desc
);
1246 /* till now HC has been in an indeterminate state ... */
1247 if (driver
->reset
&& (retval
= driver
->reset (hcd
)) < 0) {
1248 dev_err (hcd
->self
.controller
, "can't reset\n");
1251 victor_printk("mcpu_usb_host_probe test08\n");
1252 hcd
->state
= USB_STATE_HALT
;
1254 victor_printk("mcpu_usb_host_probe test09\n");
1255 #ifdef CONFIG_ARCH_MOXACPU
1256 cpe_int_set_irq(hcd
->irq
, LEVEL
, H_ACTIVE
);
1258 retval
= request_irq (hcd
->irq
, usb_hcd_irq
, SA_INTERRUPT
,
1259 hcd
->description
, hcd
);
1261 dev_err (hcd
->self
.controller
,
1262 "request interrupt %d failed\n", hcd
->irq
);
1265 victor_printk("mcpu_usb_host_probe test09\n");
1266 dev_info (hcd
->self
.controller
, "irq %d, memory %p\n", hcd
->irq
, hcd
->regs
);
1268 usb_bus_init (&hcd
->self
);
1269 hcd
->self
.op
= &usb_hcd_operations
;
1270 hcd
->self
.hcpriv
= (void *) hcd
;
1271 hcd
->self
.release
= &usb_hcd_mcpu_release
;
1272 init_timer (&hcd
->rh_timer
);
1274 INIT_LIST_HEAD (&hcd
->dev_list
);
1276 usb_register_bus (&hcd
->self
);
1277 victor_printk("mcpu_usb_host_probe test10\n");
1278 if ((retval
= driver
->start (hcd
)) < 0) {
1279 dev_err (hcd
->self
.controller
, "init error %d\n", retval
);
1280 usb_hcd_mcpu_remove(hcd
, pdev
);
1282 victor_printk("mcpu_usb_host_probe test11\n");
1287 static int ehci_hcd_mcpu_drv_probe(struct device
*dev
)
1289 return usb_hcd_mcpu_probe(&ehci_driver
, to_platform_device(dev
));
1292 static int ehci_hcd_mcpu_drv_remove(struct device
*dev
)
1294 usb_hcd_mcpu_remove(dev_get_drvdata(dev
), to_platform_device(dev
));
1295 dev_set_drvdata(dev
, NULL
);
1299 static struct device_driver mcpu_usb_host_driver
= {
1301 .bus
= &platform_bus_type
,
1302 .probe
= ehci_hcd_mcpu_drv_probe
,
1303 .remove
= ehci_hcd_mcpu_drv_remove
,
1305 #endif // CONFIG_ARCH_MOXACPU
1307 static int __init
init (void)
1312 pr_debug ("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
1314 sizeof (struct ehci_qh
), sizeof (struct ehci_qtd
),
1315 sizeof (struct ehci_itd
), sizeof (struct ehci_sitd
));
1317 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 11-08-2005
1318 return driver_register(&mcpu_usb_host_driver
);
1319 #else // CONFIG_ARCH_MOXACPU
1320 return pci_module_init (&ehci_pci_driver
);
1321 #endif // CONFIG_ARCH_MOXACPU
1325 static void __exit
cleanup (void)
1327 #ifdef CONFIG_ARCH_MOXACPU // add by Victor Yu. 11-15-2005
1328 driver_unregister(&mcpu_usb_host_driver
);
1329 #else // CONFIG_ARCH_MOXACPU
1330 pci_unregister_driver (&ehci_pci_driver
);
1331 #endif // CONFIG_ARCH_MOXACPU
1333 module_exit (cleanup
);