xref: /linux/drivers/usb/host/ohci-hcd.c (revision d413984ae936fad46678403b38d79c595e5aaafe)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * OHCI HCD (Host Controller Driver) for USB.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
51da177e4SLinus Torvalds  * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * [ Initialisation is based on Linus'  ]
81da177e4SLinus Torvalds  * [ uhci code and gregs ohci fragments ]
91da177e4SLinus Torvalds  * [ (C) Copyright 1999 Linus Torvalds  ]
101da177e4SLinus Torvalds  * [ (C) Copyright 1999 Gregory P. Smith]
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * OHCI is the main "non-Intel/VIA" standard for USB 1.1 host controller
141da177e4SLinus Torvalds  * interfaces (though some non-x86 Intel chips use it).  It supports
151da177e4SLinus Torvalds  * smarter hardware than UHCI.  A download link for the spec available
161da177e4SLinus Torvalds  * through the http://www.usb.org website.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * History:
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * 2004/03/24 LH7A404 support (Durgesh Pattamatta & Marc Singer)
211da177e4SLinus Torvalds  * 2004/02/04 use generic dma_* functions instead of pci_* (dsaxena@plexity.net)
221da177e4SLinus Torvalds  * 2003/02/24 show registers in sysfs (Kevin Brosius)
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * 2002/09/03 get rid of ed hashtables, rework periodic scheduling and
251da177e4SLinus Torvalds  * 	bandwidth accounting; if debugging, show schedules in driverfs
261da177e4SLinus Torvalds  * 2002/07/19 fixes to management of ED and schedule state.
271da177e4SLinus Torvalds  * 2002/06/09 SA-1111 support (Christopher Hoover)
281da177e4SLinus Torvalds  * 2002/06/01 remember frame when HC won't see EDs any more; use that info
291da177e4SLinus Torvalds  *	to fix urb unlink races caused by interrupt latency assumptions;
301da177e4SLinus Torvalds  *	minor ED field and function naming updates
311da177e4SLinus Torvalds  * 2002/01/18 package as a patch for 2.5.3; this should match the
321da177e4SLinus Torvalds  *	2.4.17 kernel modulo some bugs being fixed.
331da177e4SLinus Torvalds  *
341da177e4SLinus Torvalds  * 2001/10/18 merge pmac cleanup (Benjamin Herrenschmidt) and bugfixes
351da177e4SLinus Torvalds  *	from post-2.4.5 patches.
361da177e4SLinus Torvalds  * 2001/09/20 URB_ZERO_PACKET support; hcca_dma portability, OPTi warning
371da177e4SLinus Torvalds  * 2001/09/07 match PCI PM changes, errnos from Linus' tree
381da177e4SLinus Torvalds  * 2001/05/05 fork 2.4.5 version into "hcd" framework, cleanup, simplify;
391da177e4SLinus Torvalds  *	pbook pci quirks gone (please fix pbook pci sw!) (db)
401da177e4SLinus Torvalds  *
411da177e4SLinus Torvalds  * 2001/04/08 Identify version on module load (gb)
421da177e4SLinus Torvalds  * 2001/03/24 td/ed hashing to remove bus_to_virt (Steve Longerbeam);
431da177e4SLinus Torvalds  	pci_map_single (db)
441da177e4SLinus Torvalds  * 2001/03/21 td and dev/ed allocation uses new pci_pool API (db)
451da177e4SLinus Torvalds  * 2001/03/07 hcca allocation uses pci_alloc_consistent (Steve Longerbeam)
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * 2000/09/26 fixed races in removing the private portion of the urb
481da177e4SLinus Torvalds  * 2000/09/07 disable bulk and control lists when unlinking the last
491da177e4SLinus Torvalds  *	endpoint descriptor in order to avoid unrecoverable errors on
501da177e4SLinus Torvalds  *	the Lucent chips. (rwc@sgi)
511da177e4SLinus Torvalds  * 2000/08/29 use bandwidth claiming hooks (thanks Randy!), fix some
521da177e4SLinus Torvalds  *	urb unlink probs, indentation fixes
531da177e4SLinus Torvalds  * 2000/08/11 various oops fixes mostly affecting iso and cleanup from
541da177e4SLinus Torvalds  *	device unplugs.
551da177e4SLinus Torvalds  * 2000/06/28 use PCI hotplug framework, for better power management
561da177e4SLinus Torvalds  *	and for Cardbus support (David Brownell)
571da177e4SLinus Torvalds  * 2000/earlier:  fixes for NEC/Lucent chips; suspend/resume handling
581da177e4SLinus Torvalds  *	when the controller loses power; handle UE; cleanup; ...
591da177e4SLinus Torvalds  *
601da177e4SLinus Torvalds  * v5.2 1999/12/07 URB 3rd preview,
611da177e4SLinus Torvalds  * v5.1 1999/11/30 URB 2nd preview, cpia, (usb-scsi)
621da177e4SLinus Torvalds  * v5.0 1999/11/22 URB Technical preview, Paul Mackerras powerbook susp/resume
631da177e4SLinus Torvalds  * 	i386: HUB, Keyboard, Mouse, Printer
641da177e4SLinus Torvalds  *
651da177e4SLinus Torvalds  * v4.3 1999/10/27 multiple HCs, bulk_request
661da177e4SLinus Torvalds  * v4.2 1999/09/05 ISO API alpha, new dev alloc, neg Error-codes
671da177e4SLinus Torvalds  * v4.1 1999/08/27 Randy Dunlap's - ISO API first impl.
681da177e4SLinus Torvalds  * v4.0 1999/08/18
691da177e4SLinus Torvalds  * v3.0 1999/06/25
701da177e4SLinus Torvalds  * v2.1 1999/05/09  code clean up
711da177e4SLinus Torvalds  * v2.0 1999/05/04
721da177e4SLinus Torvalds  * v1.0 1999/04/27 initial release
731da177e4SLinus Torvalds  *
741da177e4SLinus Torvalds  * This file is licenced under the GPL.
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds #include <linux/module.h>
781da177e4SLinus Torvalds #include <linux/moduleparam.h>
791da177e4SLinus Torvalds #include <linux/pci.h>
801da177e4SLinus Torvalds #include <linux/kernel.h>
811da177e4SLinus Torvalds #include <linux/delay.h>
821da177e4SLinus Torvalds #include <linux/ioport.h>
831da177e4SLinus Torvalds #include <linux/sched.h>
841da177e4SLinus Torvalds #include <linux/slab.h>
851da177e4SLinus Torvalds #include <linux/smp_lock.h>
861da177e4SLinus Torvalds #include <linux/errno.h>
871da177e4SLinus Torvalds #include <linux/init.h>
881da177e4SLinus Torvalds #include <linux/timer.h>
891da177e4SLinus Torvalds #include <linux/list.h>
901da177e4SLinus Torvalds #include <linux/usb.h>
911da177e4SLinus Torvalds #include <linux/usb_otg.h>
921da177e4SLinus Torvalds #include <linux/dma-mapping.h>
93f4df0e33SDavid Brownell #include <linux/dmapool.h>
94f4df0e33SDavid Brownell #include <linux/reboot.h>
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #include <asm/io.h>
971da177e4SLinus Torvalds #include <asm/irq.h>
981da177e4SLinus Torvalds #include <asm/system.h>
991da177e4SLinus Torvalds #include <asm/unaligned.h>
1001da177e4SLinus Torvalds #include <asm/byteorder.h>
1011da177e4SLinus Torvalds 
102f4df0e33SDavid Brownell #include "../core/hcd.h"
1031da177e4SLinus Torvalds 
104*d413984aSDavid Brownell #define DRIVER_VERSION "2006 August 04"
1051da177e4SLinus Torvalds #define DRIVER_AUTHOR "Roman Weissgaerber, David Brownell"
1061da177e4SLinus Torvalds #define DRIVER_DESC "USB 1.1 'Open' Host Controller (OHCI) Driver"
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1091da177e4SLinus Torvalds 
1108de98402SBenjamin Herrenschmidt #undef OHCI_VERBOSE_DEBUG	/* not always helpful */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* For initializing controller (mask in an HCFS mode too) */
1131da177e4SLinus Torvalds #define	OHCI_CONTROL_INIT	OHCI_CTRL_CBSR
1141da177e4SLinus Torvalds #define	OHCI_INTR_INIT \
115*d413984aSDavid Brownell 		(OHCI_INTR_MIE | OHCI_INTR_RHSC | OHCI_INTR_UE \
116*d413984aSDavid Brownell 		| OHCI_INTR_RD | OHCI_INTR_WDH)
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds #ifdef __hppa__
1191da177e4SLinus Torvalds /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
1201da177e4SLinus Torvalds #define	IR_DISABLE
1211da177e4SLinus Torvalds #endif
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #ifdef CONFIG_ARCH_OMAP
1241da177e4SLinus Torvalds /* OMAP doesn't support IR (no SMM; not needed) */
1251da177e4SLinus Torvalds #define	IR_DISABLE
1261da177e4SLinus Torvalds #endif
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds static const char	hcd_name [] = "ohci_hcd";
1311da177e4SLinus Torvalds 
132*d413984aSDavid Brownell #define	STATECHANGE_DELAY	msecs_to_jiffies(300)
133*d413984aSDavid Brownell 
1341da177e4SLinus Torvalds #include "ohci.h"
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds static void ohci_dump (struct ohci_hcd *ohci, int verbose);
1371da177e4SLinus Torvalds static int ohci_init (struct ohci_hcd *ohci);
1381da177e4SLinus Torvalds static void ohci_stop (struct usb_hcd *hcd);
139f4df0e33SDavid Brownell static int ohci_reboot (struct notifier_block *, unsigned long , void *);
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds #include "ohci-hub.c"
1421da177e4SLinus Torvalds #include "ohci-dbg.c"
1431da177e4SLinus Torvalds #include "ohci-mem.c"
1441da177e4SLinus Torvalds #include "ohci-q.c"
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds /*
1481da177e4SLinus Torvalds  * On architectures with edge-triggered interrupts we must never return
1491da177e4SLinus Torvalds  * IRQ_NONE.
1501da177e4SLinus Torvalds  */
1511da177e4SLinus Torvalds #if defined(CONFIG_SA1111)  /* ... or other edge-triggered systems */
1521da177e4SLinus Torvalds #define IRQ_NOTMINE	IRQ_HANDLED
1531da177e4SLinus Torvalds #else
1541da177e4SLinus Torvalds #define IRQ_NOTMINE	IRQ_NONE
1551da177e4SLinus Torvalds #endif
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds /* Some boards misreport power switching/overcurrent */
1591da177e4SLinus Torvalds static int distrust_firmware = 1;
1601da177e4SLinus Torvalds module_param (distrust_firmware, bool, 0);
1611da177e4SLinus Torvalds MODULE_PARM_DESC (distrust_firmware,
1621da177e4SLinus Torvalds 	"true to distrust firmware power/overcurrent setup");
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /* Some boards leave IR set wrongly, since they fail BIOS/SMM handshakes */
1651da177e4SLinus Torvalds static int no_handshake = 0;
1661da177e4SLinus Torvalds module_param (no_handshake, bool, 0);
1671da177e4SLinus Torvalds MODULE_PARM_DESC (no_handshake, "true (not default) disables BIOS handshake");
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds /*
1721da177e4SLinus Torvalds  * queue up an urb for anything except the root hub
1731da177e4SLinus Torvalds  */
1741da177e4SLinus Torvalds static int ohci_urb_enqueue (
1751da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
1761da177e4SLinus Torvalds 	struct usb_host_endpoint *ep,
1771da177e4SLinus Torvalds 	struct urb	*urb,
17855016f10SAl Viro 	gfp_t		mem_flags
1791da177e4SLinus Torvalds ) {
1801da177e4SLinus Torvalds 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
1811da177e4SLinus Torvalds 	struct ed	*ed;
1821da177e4SLinus Torvalds 	urb_priv_t	*urb_priv;
1831da177e4SLinus Torvalds 	unsigned int	pipe = urb->pipe;
1841da177e4SLinus Torvalds 	int		i, size = 0;
1851da177e4SLinus Torvalds 	unsigned long	flags;
1861da177e4SLinus Torvalds 	int		retval = 0;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds #ifdef OHCI_VERBOSE_DEBUG
1891da177e4SLinus Torvalds 	urb_print (urb, "SUB", usb_pipein (pipe));
1901da177e4SLinus Torvalds #endif
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	/* every endpoint has a ed, locate and maybe (re)initialize it */
1931da177e4SLinus Torvalds 	if (! (ed = ed_get (ohci, ep, urb->dev, pipe, urb->interval)))
1941da177e4SLinus Torvalds 		return -ENOMEM;
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds 	/* for the private part of the URB we need the number of TDs (size) */
1971da177e4SLinus Torvalds 	switch (ed->type) {
1981da177e4SLinus Torvalds 		case PIPE_CONTROL:
1991da177e4SLinus Torvalds 			/* td_submit_urb() doesn't yet handle these */
2001da177e4SLinus Torvalds 			if (urb->transfer_buffer_length > 4096)
2011da177e4SLinus Torvalds 				return -EMSGSIZE;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 			/* 1 TD for setup, 1 for ACK, plus ... */
2041da177e4SLinus Torvalds 			size = 2;
2051da177e4SLinus Torvalds 			/* FALLTHROUGH */
2061da177e4SLinus Torvalds 		// case PIPE_INTERRUPT:
2071da177e4SLinus Torvalds 		// case PIPE_BULK:
2081da177e4SLinus Torvalds 		default:
2091da177e4SLinus Torvalds 			/* one TD for every 4096 Bytes (can be upto 8K) */
2101da177e4SLinus Torvalds 			size += urb->transfer_buffer_length / 4096;
2111da177e4SLinus Torvalds 			/* ... and for any remaining bytes ... */
2121da177e4SLinus Torvalds 			if ((urb->transfer_buffer_length % 4096) != 0)
2131da177e4SLinus Torvalds 				size++;
2141da177e4SLinus Torvalds 			/* ... and maybe a zero length packet to wrap it up */
2151da177e4SLinus Torvalds 			if (size == 0)
2161da177e4SLinus Torvalds 				size++;
2171da177e4SLinus Torvalds 			else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
2181da177e4SLinus Torvalds 				&& (urb->transfer_buffer_length
2191da177e4SLinus Torvalds 					% usb_maxpacket (urb->dev, pipe,
2201da177e4SLinus Torvalds 						usb_pipeout (pipe))) == 0)
2211da177e4SLinus Torvalds 				size++;
2221da177e4SLinus Torvalds 			break;
2231da177e4SLinus Torvalds 		case PIPE_ISOCHRONOUS: /* number of packets from URB */
2241da177e4SLinus Torvalds 			size = urb->number_of_packets;
2251da177e4SLinus Torvalds 			break;
2261da177e4SLinus Torvalds 	}
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	/* allocate the private part of the URB */
2291da177e4SLinus Torvalds 	urb_priv = kmalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
2301da177e4SLinus Torvalds 			mem_flags);
2311da177e4SLinus Torvalds 	if (!urb_priv)
2321da177e4SLinus Torvalds 		return -ENOMEM;
2331da177e4SLinus Torvalds 	memset (urb_priv, 0, sizeof (urb_priv_t) + size * sizeof (struct td *));
2341da177e4SLinus Torvalds 	INIT_LIST_HEAD (&urb_priv->pending);
2351da177e4SLinus Torvalds 	urb_priv->length = size;
2361da177e4SLinus Torvalds 	urb_priv->ed = ed;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	/* allocate the TDs (deferring hash chain updates) */
2391da177e4SLinus Torvalds 	for (i = 0; i < size; i++) {
2401da177e4SLinus Torvalds 		urb_priv->td [i] = td_alloc (ohci, mem_flags);
2411da177e4SLinus Torvalds 		if (!urb_priv->td [i]) {
2421da177e4SLinus Torvalds 			urb_priv->length = i;
2431da177e4SLinus Torvalds 			urb_free_priv (ohci, urb_priv);
2441da177e4SLinus Torvalds 			return -ENOMEM;
2451da177e4SLinus Torvalds 		}
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	/* don't submit to a dead HC */
2518de98402SBenjamin Herrenschmidt 	if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
2528de98402SBenjamin Herrenschmidt 		retval = -ENODEV;
2538de98402SBenjamin Herrenschmidt 		goto fail;
2548de98402SBenjamin Herrenschmidt 	}
2551da177e4SLinus Torvalds 	if (!HC_IS_RUNNING(hcd->state)) {
2561da177e4SLinus Torvalds 		retval = -ENODEV;
2571da177e4SLinus Torvalds 		goto fail;
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	/* in case of unlink-during-submit */
2611da177e4SLinus Torvalds 	spin_lock (&urb->lock);
2621da177e4SLinus Torvalds 	if (urb->status != -EINPROGRESS) {
2631da177e4SLinus Torvalds 		spin_unlock (&urb->lock);
2641da177e4SLinus Torvalds 		urb->hcpriv = urb_priv;
2651da177e4SLinus Torvalds 		finish_urb (ohci, urb, NULL);
2661da177e4SLinus Torvalds 		retval = 0;
2671da177e4SLinus Torvalds 		goto fail;
2681da177e4SLinus Torvalds 	}
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	/* schedule the ed if needed */
2711da177e4SLinus Torvalds 	if (ed->state == ED_IDLE) {
2721da177e4SLinus Torvalds 		retval = ed_schedule (ohci, ed);
2731da177e4SLinus Torvalds 		if (retval < 0)
2741da177e4SLinus Torvalds 			goto fail0;
2751da177e4SLinus Torvalds 		if (ed->type == PIPE_ISOCHRONOUS) {
2761da177e4SLinus Torvalds 			u16	frame = ohci_frame_no(ohci);
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 			/* delay a few frames before the first TD */
2791da177e4SLinus Torvalds 			frame += max_t (u16, 8, ed->interval);
2801da177e4SLinus Torvalds 			frame &= ~(ed->interval - 1);
2811da177e4SLinus Torvalds 			frame |= ed->branch;
2821da177e4SLinus Torvalds 			urb->start_frame = frame;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 			/* yes, only URB_ISO_ASAP is supported, and
2851da177e4SLinus Torvalds 			 * urb->start_frame is never used as input.
2861da177e4SLinus Torvalds 			 */
2871da177e4SLinus Torvalds 		}
2881da177e4SLinus Torvalds 	} else if (ed->type == PIPE_ISOCHRONOUS)
2891da177e4SLinus Torvalds 		urb->start_frame = ed->last_iso + ed->interval;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* fill the TDs and link them to the ed; and
2921da177e4SLinus Torvalds 	 * enable that part of the schedule, if needed
2931da177e4SLinus Torvalds 	 * and update count of queued periodic urbs
2941da177e4SLinus Torvalds 	 */
2951da177e4SLinus Torvalds 	urb->hcpriv = urb_priv;
2961da177e4SLinus Torvalds 	td_submit_urb (ohci, urb);
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds fail0:
2991da177e4SLinus Torvalds 	spin_unlock (&urb->lock);
3001da177e4SLinus Torvalds fail:
3011da177e4SLinus Torvalds 	if (retval)
3021da177e4SLinus Torvalds 		urb_free_priv (ohci, urb_priv);
3031da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
3041da177e4SLinus Torvalds 	return retval;
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds /*
3081da177e4SLinus Torvalds  * decouple the URB from the HC queues (TDs, urb_priv); it's
3091da177e4SLinus Torvalds  * already marked using urb->status.  reporting is always done
3101da177e4SLinus Torvalds  * asynchronously, and we might be dealing with an urb that's
3111da177e4SLinus Torvalds  * partially transferred, or an ED with other urbs being unlinked.
3121da177e4SLinus Torvalds  */
3131da177e4SLinus Torvalds static int ohci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
3141da177e4SLinus Torvalds {
3151da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
3161da177e4SLinus Torvalds 	unsigned long		flags;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds #ifdef OHCI_VERBOSE_DEBUG
3191da177e4SLinus Torvalds 	urb_print (urb, "UNLINK", 1);
3201da177e4SLinus Torvalds #endif
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
3231da177e4SLinus Torvalds  	if (HC_IS_RUNNING(hcd->state)) {
3241da177e4SLinus Torvalds 		urb_priv_t  *urb_priv;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 		/* Unless an IRQ completed the unlink while it was being
3271da177e4SLinus Torvalds 		 * handed to us, flag it for unlink and giveback, and force
3281da177e4SLinus Torvalds 		 * some upcoming INTR_SF to call finish_unlinks()
3291da177e4SLinus Torvalds 		 */
3301da177e4SLinus Torvalds 		urb_priv = urb->hcpriv;
3311da177e4SLinus Torvalds 		if (urb_priv) {
3321da177e4SLinus Torvalds 			if (urb_priv->ed->state == ED_OPER)
3331da177e4SLinus Torvalds 				start_ed_unlink (ohci, urb_priv->ed);
3341da177e4SLinus Torvalds 		}
3351da177e4SLinus Torvalds 	} else {
3361da177e4SLinus Torvalds 		/*
3371da177e4SLinus Torvalds 		 * with HC dead, we won't respect hc queue pointers
3381da177e4SLinus Torvalds 		 * any more ... just clean up every urb's memory.
3391da177e4SLinus Torvalds 		 */
3401da177e4SLinus Torvalds 		if (urb->hcpriv)
3411da177e4SLinus Torvalds 			finish_urb (ohci, urb, NULL);
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
3441da177e4SLinus Torvalds 	return 0;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds /* frees config/altsetting state for endpoints,
3501da177e4SLinus Torvalds  * including ED memory, dummy TD, and bulk/intr data toggle
3511da177e4SLinus Torvalds  */
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds static void
3541da177e4SLinus Torvalds ohci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
3551da177e4SLinus Torvalds {
3561da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
3571da177e4SLinus Torvalds 	unsigned long		flags;
3581da177e4SLinus Torvalds 	struct ed		*ed = ep->hcpriv;
3591da177e4SLinus Torvalds 	unsigned		limit = 1000;
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	/* ASSERT:  any requests/urbs are being unlinked */
3621da177e4SLinus Torvalds 	/* ASSERT:  nobody can be submitting urbs for this any more */
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	if (!ed)
3651da177e4SLinus Torvalds 		return;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds rescan:
3681da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	if (!HC_IS_RUNNING (hcd->state)) {
3711da177e4SLinus Torvalds sanitize:
3721da177e4SLinus Torvalds 		ed->state = ED_IDLE;
3731da177e4SLinus Torvalds 		finish_unlinks (ohci, 0, NULL);
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	switch (ed->state) {
3771da177e4SLinus Torvalds 	case ED_UNLINK:		/* wait for hw to finish? */
3781da177e4SLinus Torvalds 		/* major IRQ delivery trouble loses INTR_SF too... */
3791da177e4SLinus Torvalds 		if (limit-- == 0) {
3801da177e4SLinus Torvalds 			ohci_warn (ohci, "IRQ INTR_SF lossage\n");
3811da177e4SLinus Torvalds 			goto sanitize;
3821da177e4SLinus Torvalds 		}
3831da177e4SLinus Torvalds 		spin_unlock_irqrestore (&ohci->lock, flags);
38422c43863SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
3851da177e4SLinus Torvalds 		goto rescan;
3861da177e4SLinus Torvalds 	case ED_IDLE:		/* fully unlinked */
3871da177e4SLinus Torvalds 		if (list_empty (&ed->td_list)) {
3881da177e4SLinus Torvalds 			td_free (ohci, ed->dummy);
3891da177e4SLinus Torvalds 			ed_free (ohci, ed);
3901da177e4SLinus Torvalds 			break;
3911da177e4SLinus Torvalds 		}
3921da177e4SLinus Torvalds 		/* else FALL THROUGH */
3931da177e4SLinus Torvalds 	default:
3941da177e4SLinus Torvalds 		/* caller was supposed to have unlinked any requests;
3951da177e4SLinus Torvalds 		 * that's not our job.  can't recover; must leak ed.
3961da177e4SLinus Torvalds 		 */
3971da177e4SLinus Torvalds 		ohci_err (ohci, "leak ed %p (#%02x) state %d%s\n",
3981da177e4SLinus Torvalds 			ed, ep->desc.bEndpointAddress, ed->state,
3991da177e4SLinus Torvalds 			list_empty (&ed->td_list) ? "" : " (has tds)");
4001da177e4SLinus Torvalds 		td_free (ohci, ed->dummy);
4011da177e4SLinus Torvalds 		break;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 	ep->hcpriv = NULL;
4041da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
4051da177e4SLinus Torvalds 	return;
4061da177e4SLinus Torvalds }
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds static int ohci_get_frame (struct usb_hcd *hcd)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	return ohci_frame_no(ohci);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static void ohci_usb_reset (struct ohci_hcd *ohci)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
4181da177e4SLinus Torvalds 	ohci->hc_control &= OHCI_CTRL_RWC;
4191da177e4SLinus Torvalds 	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
4201da177e4SLinus Torvalds }
4211da177e4SLinus Torvalds 
422f4df0e33SDavid Brownell /* reboot notifier forcibly disables IRQs and DMA, helping kexec and
423f4df0e33SDavid Brownell  * other cases where the next software may expect clean state from the
424f4df0e33SDavid Brownell  * "firmware".  this is bus-neutral, unlike shutdown() methods.
425f4df0e33SDavid Brownell  */
426f4df0e33SDavid Brownell static int
427f4df0e33SDavid Brownell ohci_reboot (struct notifier_block *block, unsigned long code, void *null)
428f4df0e33SDavid Brownell {
429f4df0e33SDavid Brownell 	struct ohci_hcd *ohci;
430f4df0e33SDavid Brownell 
431f4df0e33SDavid Brownell 	ohci = container_of (block, struct ohci_hcd, reboot_notifier);
432f4df0e33SDavid Brownell 	ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
433f4df0e33SDavid Brownell 	ohci_usb_reset (ohci);
434f4df0e33SDavid Brownell 	/* flush the writes */
435f4df0e33SDavid Brownell 	(void) ohci_readl (ohci, &ohci->regs->control);
436f4df0e33SDavid Brownell 	return 0;
437f4df0e33SDavid Brownell }
438f4df0e33SDavid Brownell 
4391da177e4SLinus Torvalds /*-------------------------------------------------------------------------*
4401da177e4SLinus Torvalds  * HC functions
4411da177e4SLinus Torvalds  *-------------------------------------------------------------------------*/
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /* init memory, and kick BIOS/SMM off */
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds static int ohci_init (struct ohci_hcd *ohci)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	int ret;
4486a9062f3SDavid Brownell 	struct usb_hcd *hcd = ohci_to_hcd(ohci);
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	disable (ohci);
4516a9062f3SDavid Brownell 	ohci->regs = hcd->regs;
4521da177e4SLinus Torvalds 
4536a9062f3SDavid Brownell 	/* REVISIT this BIOS handshake is now moved into PCI "quirks", and
4546a9062f3SDavid Brownell 	 * was never needed for most non-PCI systems ... remove the code?
4556a9062f3SDavid Brownell 	 */
4566a9062f3SDavid Brownell 
4571da177e4SLinus Torvalds #ifndef IR_DISABLE
4581da177e4SLinus Torvalds 	/* SMM owns the HC?  not for long! */
4591da177e4SLinus Torvalds 	if (!no_handshake && ohci_readl (ohci,
4601da177e4SLinus Torvalds 					&ohci->regs->control) & OHCI_CTRL_IR) {
4611da177e4SLinus Torvalds 		u32 temp;
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds 		ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 		/* this timeout is arbitrary.  we make it long, so systems
4661da177e4SLinus Torvalds 		 * depending on usb keyboards may be usable even if the
4671da177e4SLinus Torvalds 		 * BIOS/SMM code seems pretty broken.
4681da177e4SLinus Torvalds 		 */
4691da177e4SLinus Torvalds 		temp = 500;	/* arbitrary: five seconds */
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_OC, &ohci->regs->intrenable);
4721da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_OCR, &ohci->regs->cmdstatus);
4731da177e4SLinus Torvalds 		while (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_IR) {
4741da177e4SLinus Torvalds 			msleep (10);
4751da177e4SLinus Torvalds 			if (--temp == 0) {
4761da177e4SLinus Torvalds 				ohci_err (ohci, "USB HC takeover failed!"
4771da177e4SLinus Torvalds 					"  (BIOS/SMM bug)\n");
4781da177e4SLinus Torvalds 				return -EBUSY;
4791da177e4SLinus Torvalds 			}
4801da177e4SLinus Torvalds 		}
4811da177e4SLinus Torvalds 		ohci_usb_reset (ohci);
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds #endif
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	/* Disable HC interrupts */
4861da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
4876a9062f3SDavid Brownell 
4886a9062f3SDavid Brownell 	/* flush the writes, and save key bits like RWC */
4896a9062f3SDavid Brownell 	if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
4906a9062f3SDavid Brownell 		ohci->hc_control |= OHCI_CTRL_RWC;
4911da177e4SLinus Torvalds 
492fdd13b36SDavid Brownell 	/* Read the number of ports unless overridden */
493fdd13b36SDavid Brownell 	if (ohci->num_ports == 0)
494fdd13b36SDavid Brownell 		ohci->num_ports = roothub_a(ohci) & RH_A_NDP;
495fdd13b36SDavid Brownell 
4961da177e4SLinus Torvalds 	if (ohci->hcca)
4971da177e4SLinus Torvalds 		return 0;
4981da177e4SLinus Torvalds 
4996a9062f3SDavid Brownell 	ohci->hcca = dma_alloc_coherent (hcd->self.controller,
5001da177e4SLinus Torvalds 			sizeof *ohci->hcca, &ohci->hcca_dma, 0);
5011da177e4SLinus Torvalds 	if (!ohci->hcca)
5021da177e4SLinus Torvalds 		return -ENOMEM;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	if ((ret = ohci_mem_init (ohci)) < 0)
5056a9062f3SDavid Brownell 		ohci_stop (hcd);
5066a9062f3SDavid Brownell 	else {
5076a9062f3SDavid Brownell 		register_reboot_notifier (&ohci->reboot_notifier);
5086a9062f3SDavid Brownell 		create_debug_files (ohci);
5096a9062f3SDavid Brownell 	}
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	return ret;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds /* Start an OHCI controller, set the BUS operational
5171da177e4SLinus Torvalds  * resets USB and controller
5181da177e4SLinus Torvalds  * enable interrupts
5191da177e4SLinus Torvalds  */
5201da177e4SLinus Torvalds static int ohci_run (struct ohci_hcd *ohci)
5211da177e4SLinus Torvalds {
5221da177e4SLinus Torvalds   	u32			mask, temp;
5231da177e4SLinus Torvalds 	int			first = ohci->fminterval == 0;
5246a9062f3SDavid Brownell 	struct usb_hcd		*hcd = ohci_to_hcd(ohci);
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	disable (ohci);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* boot firmware should have set this up (5.1.1.3.1) */
5291da177e4SLinus Torvalds 	if (first) {
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 		temp = ohci_readl (ohci, &ohci->regs->fminterval);
5321da177e4SLinus Torvalds 		ohci->fminterval = temp & 0x3fff;
5331da177e4SLinus Torvalds 		if (ohci->fminterval != FI)
5341da177e4SLinus Torvalds 			ohci_dbg (ohci, "fminterval delta %d\n",
5351da177e4SLinus Torvalds 				ohci->fminterval - FI);
5361da177e4SLinus Torvalds 		ohci->fminterval |= FSMP (ohci->fminterval) << 16;
5371da177e4SLinus Torvalds 		/* also: power/overcurrent flags in roothub.a */
5381da177e4SLinus Torvalds 	}
5391da177e4SLinus Torvalds 
5406a9062f3SDavid Brownell   	/* Reset USB nearly "by the book".  RemoteWakeupConnected was
5416a9062f3SDavid Brownell 	 * saved if boot firmware (BIOS/SMM/...) told us it's connected,
5426a9062f3SDavid Brownell 	 * or if bus glue did the same (e.g. for PCI add-in cards with
5436a9062f3SDavid Brownell 	 * PCI PM support).
5441da177e4SLinus Torvalds 	 */
5451da177e4SLinus Torvalds 	ohci_dbg (ohci, "resetting from state '%s', control = 0x%x\n",
5461da177e4SLinus Torvalds 			hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
5476a9062f3SDavid Brownell 			ohci_readl (ohci, &ohci->regs->control));
5486a9062f3SDavid Brownell 	if ((ohci->hc_control & OHCI_CTRL_RWC) != 0
5496a9062f3SDavid Brownell 			&& !device_may_wakeup(hcd->self.controller))
5506a9062f3SDavid Brownell 		device_init_wakeup(hcd->self.controller, 1);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
5531da177e4SLinus Torvalds 	case OHCI_USB_OPER:
5541da177e4SLinus Torvalds 		temp = 0;
5551da177e4SLinus Torvalds 		break;
5561da177e4SLinus Torvalds 	case OHCI_USB_SUSPEND:
5571da177e4SLinus Torvalds 	case OHCI_USB_RESUME:
5581da177e4SLinus Torvalds 		ohci->hc_control &= OHCI_CTRL_RWC;
5591da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_USB_RESUME;
5601da177e4SLinus Torvalds 		temp = 10 /* msec wait */;
5611da177e4SLinus Torvalds 		break;
5621da177e4SLinus Torvalds 	// case OHCI_USB_RESET:
5631da177e4SLinus Torvalds 	default:
5641da177e4SLinus Torvalds 		ohci->hc_control &= OHCI_CTRL_RWC;
5651da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_USB_RESET;
5661da177e4SLinus Torvalds 		temp = 50 /* msec wait */;
5671da177e4SLinus Torvalds 		break;
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
5701da177e4SLinus Torvalds 	// flush the writes
5711da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
5721da177e4SLinus Torvalds 	msleep(temp);
5731da177e4SLinus Torvalds 	temp = roothub_a (ohci);
5741da177e4SLinus Torvalds 	if (!(temp & RH_A_NPS)) {
5751da177e4SLinus Torvalds 		/* power down each port */
576fdd13b36SDavid Brownell 		for (temp = 0; temp < ohci->num_ports; temp++)
5771da177e4SLinus Torvalds 			ohci_writel (ohci, RH_PS_LSDA,
5781da177e4SLinus Torvalds 				&ohci->regs->roothub.portstatus [temp]);
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 	// flush those writes
5811da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
5821da177e4SLinus Torvalds 	memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	/* 2msec timelimit here means no irqs/preempt */
5851da177e4SLinus Torvalds 	spin_lock_irq (&ohci->lock);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds retry:
5881da177e4SLinus Torvalds 	/* HC Reset requires max 10 us delay */
5891da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_HCR,  &ohci->regs->cmdstatus);
5901da177e4SLinus Torvalds 	temp = 30;	/* ... allow extra time */
5911da177e4SLinus Torvalds 	while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
5921da177e4SLinus Torvalds 		if (--temp == 0) {
5931da177e4SLinus Torvalds 			spin_unlock_irq (&ohci->lock);
5941da177e4SLinus Torvalds 			ohci_err (ohci, "USB HC reset timed out!\n");
5951da177e4SLinus Torvalds 			return -1;
5961da177e4SLinus Torvalds 		}
5971da177e4SLinus Torvalds 		udelay (1);
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 	/* now we're in the SUSPEND state ... must go OPERATIONAL
6011da177e4SLinus Torvalds 	 * within 2msec else HC enters RESUME
6021da177e4SLinus Torvalds 	 *
6031da177e4SLinus Torvalds 	 * ... but some hardware won't init fmInterval "by the book"
6041da177e4SLinus Torvalds 	 * (SiS, OPTi ...), so reset again instead.  SiS doesn't need
6051da177e4SLinus Torvalds 	 * this if we write fmInterval after we're OPERATIONAL.
6061da177e4SLinus Torvalds 	 * Unclear about ALi, ServerWorks, and others ... this could
6071da177e4SLinus Torvalds 	 * easily be a longstanding bug in chip init on Linux.
6081da177e4SLinus Torvalds 	 */
6091da177e4SLinus Torvalds 	if (ohci->flags & OHCI_QUIRK_INITRESET) {
6101da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
6111da177e4SLinus Torvalds 		// flush those writes
6121da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	/* Tell the controller where the control and bulk lists are
6161da177e4SLinus Torvalds 	 * The lists are empty now. */
6171da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
6181da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	/* a reset clears this */
6211da177e4SLinus Torvalds 	ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	periodic_reinit (ohci);
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	/* some OHCI implementations are finicky about how they init.
6261da177e4SLinus Torvalds 	 * bogus values here mean not even enumeration could work.
6271da177e4SLinus Torvalds 	 */
6281da177e4SLinus Torvalds 	if ((ohci_readl (ohci, &ohci->regs->fminterval) & 0x3fff0000) == 0
6291da177e4SLinus Torvalds 			|| !ohci_readl (ohci, &ohci->regs->periodicstart)) {
6301da177e4SLinus Torvalds 		if (!(ohci->flags & OHCI_QUIRK_INITRESET)) {
6311da177e4SLinus Torvalds 			ohci->flags |= OHCI_QUIRK_INITRESET;
6321da177e4SLinus Torvalds 			ohci_dbg (ohci, "enabling initreset quirk\n");
6331da177e4SLinus Torvalds 			goto retry;
6341da177e4SLinus Torvalds 		}
6351da177e4SLinus Torvalds 		spin_unlock_irq (&ohci->lock);
6361da177e4SLinus Torvalds 		ohci_err (ohci, "init err (%08x %04x)\n",
6371da177e4SLinus Torvalds 			ohci_readl (ohci, &ohci->regs->fminterval),
6381da177e4SLinus Torvalds 			ohci_readl (ohci, &ohci->regs->periodicstart));
6391da177e4SLinus Torvalds 		return -EOVERFLOW;
6401da177e4SLinus Torvalds 	}
6411da177e4SLinus Torvalds 
642*d413984aSDavid Brownell 	/* use rhsc irqs after khubd is fully initialized */
643*d413984aSDavid Brownell 	hcd->poll_rh = 1;
644*d413984aSDavid Brownell 	hcd->uses_new_polling = 1;
645*d413984aSDavid Brownell 
6461da177e4SLinus Torvalds 	/* start controller operations */
6471da177e4SLinus Torvalds 	ohci->hc_control &= OHCI_CTRL_RWC;
6481da177e4SLinus Torvalds 	ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
6491da177e4SLinus Torvalds 	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
6506a9062f3SDavid Brownell 	hcd->state = HC_STATE_RUNNING;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	/* wake on ConnectStatusChange, matching external hubs */
6531da177e4SLinus Torvalds 	ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 	/* Choose the interrupts we care about now, others later on demand */
6561da177e4SLinus Torvalds 	mask = OHCI_INTR_INIT;
657*d413984aSDavid Brownell 	ohci_writel (ohci, ~0, &ohci->regs->intrstatus);
6581da177e4SLinus Torvalds 	ohci_writel (ohci, mask, &ohci->regs->intrenable);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	/* handle root hub init quirks ... */
6611da177e4SLinus Torvalds 	temp = roothub_a (ohci);
6621da177e4SLinus Torvalds 	temp &= ~(RH_A_PSM | RH_A_OCPM);
6631da177e4SLinus Torvalds 	if (ohci->flags & OHCI_QUIRK_SUPERIO) {
6641da177e4SLinus Torvalds 		/* NSC 87560 and maybe others */
6651da177e4SLinus Torvalds 		temp |= RH_A_NOCP;
6661da177e4SLinus Torvalds 		temp &= ~(RH_A_POTPGT | RH_A_NPS);
6671da177e4SLinus Torvalds 		ohci_writel (ohci, temp, &ohci->regs->roothub.a);
6681da177e4SLinus Torvalds 	} else if ((ohci->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
6691da177e4SLinus Torvalds 		/* hub power always on; required for AMD-756 and some
6701da177e4SLinus Torvalds 		 * Mac platforms.  ganged overcurrent reporting, if any.
6711da177e4SLinus Torvalds 		 */
6721da177e4SLinus Torvalds 		temp |= RH_A_NPS;
6731da177e4SLinus Torvalds 		ohci_writel (ohci, temp, &ohci->regs->roothub.a);
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds 	ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
6761da177e4SLinus Torvalds 	ohci_writel (ohci, (temp & RH_A_NPS) ? 0 : RH_B_PPCM,
6771da177e4SLinus Torvalds 						&ohci->regs->roothub.b);
6781da177e4SLinus Torvalds 	// flush those writes
6791da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
6801da177e4SLinus Torvalds 
681*d413984aSDavid Brownell 	ohci->next_statechange = jiffies + STATECHANGE_DELAY;
6821da177e4SLinus Torvalds 	spin_unlock_irq (&ohci->lock);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	// POTPGT delay is bits 24-31, in 2 ms units.
6851da177e4SLinus Torvalds 	mdelay ((temp >> 23) & 0x1fe);
6866a9062f3SDavid Brownell 	hcd->state = HC_STATE_RUNNING;
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	ohci_dump (ohci, 1);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	return 0;
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds /* an interrupt happens */
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds static irqreturn_t ohci_irq (struct usb_hcd *hcd, struct pt_regs *ptregs)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
7001da177e4SLinus Torvalds 	struct ohci_regs __iomem *regs = ohci->regs;
7011da177e4SLinus Torvalds  	int			ints;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	/* we can eliminate a (slow) ohci_readl()
7041da177e4SLinus Torvalds 	   if _only_ WDH caused this irq */
7051da177e4SLinus Torvalds 	if ((ohci->hcca->done_head != 0)
7061da177e4SLinus Torvalds 			&& ! (hc32_to_cpup (ohci, &ohci->hcca->done_head)
7071da177e4SLinus Torvalds 				& 0x01)) {
7081da177e4SLinus Torvalds 		ints =  OHCI_INTR_WDH;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	/* cardbus/... hardware gone before remove() */
7111da177e4SLinus Torvalds 	} else if ((ints = ohci_readl (ohci, &regs->intrstatus)) == ~(u32)0) {
7121da177e4SLinus Torvalds 		disable (ohci);
7131da177e4SLinus Torvalds 		ohci_dbg (ohci, "device removed!\n");
7141da177e4SLinus Torvalds 		return IRQ_HANDLED;
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/* interrupt for some other device? */
7171da177e4SLinus Torvalds 	} else if ((ints &= ohci_readl (ohci, &regs->intrenable)) == 0) {
7181da177e4SLinus Torvalds 		return IRQ_NOTMINE;
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 
721*d413984aSDavid Brownell 	/* NOTE:  vendors didn't always make the same implementation
722*d413984aSDavid Brownell 	 * choices for RHSC.  Sometimes it triggers on an edge (like
723*d413984aSDavid Brownell 	 * setting and maybe clearing a port status change bit); and
724*d413984aSDavid Brownell 	 * it's level-triggered on other silicon, active until khubd
725*d413984aSDavid Brownell 	 * clears all active port status change bits.  Poll by timer
726*d413984aSDavid Brownell 	 * til it's fully debounced and the difference won't matter.
727*d413984aSDavid Brownell 	 */
728*d413984aSDavid Brownell 	if (ints & OHCI_INTR_RHSC) {
729*d413984aSDavid Brownell 		ohci_vdbg (ohci, "rhsc\n");
730*d413984aSDavid Brownell 		ohci_writel (ohci, OHCI_INTR_RHSC, &regs->intrdisable);
731*d413984aSDavid Brownell 		hcd->poll_rh = 1;
732*d413984aSDavid Brownell 		ohci->next_statechange = jiffies + STATECHANGE_DELAY;
733*d413984aSDavid Brownell 		ohci_writel (ohci, OHCI_INTR_RHSC, &regs->intrstatus);
734*d413984aSDavid Brownell 		usb_hcd_poll_rh_status(hcd);
735*d413984aSDavid Brownell 	}
736*d413984aSDavid Brownell 
7371da177e4SLinus Torvalds 	if (ints & OHCI_INTR_UE) {
7381da177e4SLinus Torvalds 		disable (ohci);
7391da177e4SLinus Torvalds 		ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
7401da177e4SLinus Torvalds 		// e.g. due to PCI Master/Target Abort
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 		ohci_dump (ohci, 1);
7431da177e4SLinus Torvalds 		ohci_usb_reset (ohci);
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	if (ints & OHCI_INTR_RD) {
7471da177e4SLinus Torvalds 		ohci_vdbg (ohci, "resume detect\n");
748e0fd3cbcSDavid Brownell 		ohci_writel (ohci, OHCI_INTR_RD, &regs->intrstatus);
7491da177e4SLinus Torvalds 		if (hcd->state != HC_STATE_QUIESCING)
750f197b2c5SDavid Brownell 			usb_hcd_resume_root_hub(hcd);
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	if (ints & OHCI_INTR_WDH) {
7541da177e4SLinus Torvalds 		if (HC_IS_RUNNING(hcd->state))
7551da177e4SLinus Torvalds 			ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrdisable);
7561da177e4SLinus Torvalds 		spin_lock (&ohci->lock);
7571da177e4SLinus Torvalds 		dl_done_list (ohci, ptregs);
7581da177e4SLinus Torvalds 		spin_unlock (&ohci->lock);
7591da177e4SLinus Torvalds 		if (HC_IS_RUNNING(hcd->state))
7601da177e4SLinus Torvalds 			ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrenable);
7611da177e4SLinus Torvalds 	}
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	/* could track INTR_SO to reduce available PCI/... bandwidth */
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	/* handle any pending URB/ED unlinks, leaving INTR_SF enabled
7661da177e4SLinus Torvalds 	 * when there's still unlinking to be done (next frame).
7671da177e4SLinus Torvalds 	 */
7681da177e4SLinus Torvalds 	spin_lock (&ohci->lock);
7691da177e4SLinus Torvalds 	if (ohci->ed_rm_list)
7701da177e4SLinus Torvalds 		finish_unlinks (ohci, ohci_frame_no(ohci), ptregs);
7711da177e4SLinus Torvalds 	if ((ints & OHCI_INTR_SF) != 0 && !ohci->ed_rm_list
7721da177e4SLinus Torvalds 			&& HC_IS_RUNNING(hcd->state))
7731da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
7741da177e4SLinus Torvalds 	spin_unlock (&ohci->lock);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	if (HC_IS_RUNNING(hcd->state)) {
7771da177e4SLinus Torvalds 		ohci_writel (ohci, ints, &regs->intrstatus);
7781da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
7791da177e4SLinus Torvalds 		// flush those writes
7801da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
7811da177e4SLinus Torvalds 	}
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds 	return IRQ_HANDLED;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds static void ohci_stop (struct usb_hcd *hcd)
7891da177e4SLinus Torvalds {
7901da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	ohci_dbg (ohci, "stop %s controller (state 0x%02x)\n",
7931da177e4SLinus Torvalds 		hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
7941da177e4SLinus Torvalds 		hcd->state);
7951da177e4SLinus Torvalds 	ohci_dump (ohci, 1);
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	flush_scheduled_work();
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	ohci_usb_reset (ohci);
8001da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
8011da177e4SLinus Torvalds 
8021da177e4SLinus Torvalds 	remove_debug_files (ohci);
803f4df0e33SDavid Brownell 	unregister_reboot_notifier (&ohci->reboot_notifier);
8041da177e4SLinus Torvalds 	ohci_mem_cleanup (ohci);
8051da177e4SLinus Torvalds 	if (ohci->hcca) {
8061da177e4SLinus Torvalds 		dma_free_coherent (hcd->self.controller,
8071da177e4SLinus Torvalds 				sizeof *ohci->hcca,
8081da177e4SLinus Torvalds 				ohci->hcca, ohci->hcca_dma);
8091da177e4SLinus Torvalds 		ohci->hcca = NULL;
8101da177e4SLinus Torvalds 		ohci->hcca_dma = 0;
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds /* must not be called from interrupt context */
8171da177e4SLinus Torvalds 
8188ad7fe16SDavid Brownell #ifdef	CONFIG_PM
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds static int ohci_restart (struct ohci_hcd *ohci)
8211da177e4SLinus Torvalds {
8221da177e4SLinus Torvalds 	int temp;
8231da177e4SLinus Torvalds 	int i;
8241da177e4SLinus Torvalds 	struct urb_priv *priv;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	/* mark any devices gone, so they do nothing till khubd disconnects.
8271da177e4SLinus Torvalds 	 * recycle any "live" eds/tds (and urbs) right away.
8281da177e4SLinus Torvalds 	 * later, khubd disconnect processing will recycle the other state,
8291da177e4SLinus Torvalds 	 * (either as disconnect/reconnect, or maybe someday as a reset).
8301da177e4SLinus Torvalds 	 */
8311da177e4SLinus Torvalds 	spin_lock_irq(&ohci->lock);
8321da177e4SLinus Torvalds 	disable (ohci);
8331c50c317SAlan Stern 	usb_root_hub_lost_power(ohci_to_hcd(ohci)->self.root_hub);
8341da177e4SLinus Torvalds 	if (!list_empty (&ohci->pending))
8351da177e4SLinus Torvalds 		ohci_dbg(ohci, "abort schedule...\n");
8361da177e4SLinus Torvalds 	list_for_each_entry (priv, &ohci->pending, pending) {
8371da177e4SLinus Torvalds 		struct urb	*urb = priv->td[0]->urb;
8381da177e4SLinus Torvalds 		struct ed	*ed = priv->ed;
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 		switch (ed->state) {
8411da177e4SLinus Torvalds 		case ED_OPER:
8421da177e4SLinus Torvalds 			ed->state = ED_UNLINK;
8431da177e4SLinus Torvalds 			ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
8441da177e4SLinus Torvalds 			ed_deschedule (ohci, ed);
8451da177e4SLinus Torvalds 
8461da177e4SLinus Torvalds 			ed->ed_next = ohci->ed_rm_list;
8471da177e4SLinus Torvalds 			ed->ed_prev = NULL;
8481da177e4SLinus Torvalds 			ohci->ed_rm_list = ed;
8491da177e4SLinus Torvalds 			/* FALLTHROUGH */
8501da177e4SLinus Torvalds 		case ED_UNLINK:
8511da177e4SLinus Torvalds 			break;
8521da177e4SLinus Torvalds 		default:
8531da177e4SLinus Torvalds 			ohci_dbg(ohci, "bogus ed %p state %d\n",
8541da177e4SLinus Torvalds 					ed, ed->state);
8551da177e4SLinus Torvalds 		}
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 		spin_lock (&urb->lock);
8581da177e4SLinus Torvalds 		urb->status = -ESHUTDOWN;
8591da177e4SLinus Torvalds 		spin_unlock (&urb->lock);
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 	finish_unlinks (ohci, 0, NULL);
8621da177e4SLinus Torvalds 	spin_unlock_irq(&ohci->lock);
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds 	/* paranoia, in case that didn't work: */
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds 	/* empty the interrupt branches */
8671da177e4SLinus Torvalds 	for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
8681da177e4SLinus Torvalds 	for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	/* no EDs to remove */
8711da177e4SLinus Torvalds 	ohci->ed_rm_list = NULL;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	/* empty control and bulk lists */
8741da177e4SLinus Torvalds 	ohci->ed_controltail = NULL;
8751da177e4SLinus Torvalds 	ohci->ed_bulktail    = NULL;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	if ((temp = ohci_run (ohci)) < 0) {
8781da177e4SLinus Torvalds 		ohci_err (ohci, "can't restart, %d\n", temp);
8791da177e4SLinus Torvalds 		return temp;
8801da177e4SLinus Torvalds 	} else {
8811da177e4SLinus Torvalds 		/* here we "know" root ports should always stay powered,
8821da177e4SLinus Torvalds 		 * and that if we try to turn them back on the root hub
8831da177e4SLinus Torvalds 		 * will respond to CSC processing.
8841da177e4SLinus Torvalds 		 */
885fdd13b36SDavid Brownell 		i = ohci->num_ports;
8861da177e4SLinus Torvalds 		while (i--)
8871da177e4SLinus Torvalds 			ohci_writel (ohci, RH_PS_PSS,
888839ab1d4SDavid Brownell 				&ohci->regs->roothub.portstatus [i]);
8891da177e4SLinus Torvalds 		ohci_dbg (ohci, "restart complete\n");
8901da177e4SLinus Torvalds 	}
8911da177e4SLinus Torvalds 	return 0;
8921da177e4SLinus Torvalds }
8931da177e4SLinus Torvalds #endif
8941da177e4SLinus Torvalds 
8951da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds MODULE_AUTHOR (DRIVER_AUTHOR);
9001da177e4SLinus Torvalds MODULE_DESCRIPTION (DRIVER_INFO);
9011da177e4SLinus Torvalds MODULE_LICENSE ("GPL");
9021da177e4SLinus Torvalds 
9031da177e4SLinus Torvalds #ifdef CONFIG_PCI
9041da177e4SLinus Torvalds #include "ohci-pci.c"
9051da177e4SLinus Torvalds #endif
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds #ifdef CONFIG_SA1111
9081da177e4SLinus Torvalds #include "ohci-sa1111.c"
9091da177e4SLinus Torvalds #endif
9101da177e4SLinus Torvalds 
9113eb0c5f4SBen Dooks #ifdef CONFIG_ARCH_S3C2410
9123eb0c5f4SBen Dooks #include "ohci-s3c2410.c"
9133eb0c5f4SBen Dooks #endif
9143eb0c5f4SBen Dooks 
9151da177e4SLinus Torvalds #ifdef CONFIG_ARCH_OMAP
9161da177e4SLinus Torvalds #include "ohci-omap.c"
9171da177e4SLinus Torvalds #endif
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds #ifdef CONFIG_ARCH_LH7A404
9201da177e4SLinus Torvalds #include "ohci-lh7a404.c"
9211da177e4SLinus Torvalds #endif
9221da177e4SLinus Torvalds 
9231da177e4SLinus Torvalds #ifdef CONFIG_PXA27x
9241da177e4SLinus Torvalds #include "ohci-pxa27x.c"
9251da177e4SLinus Torvalds #endif
9261da177e4SLinus Torvalds 
927a5b7474aSLennert Buytenhek #ifdef CONFIG_ARCH_EP93XX
928a5b7474aSLennert Buytenhek #include "ohci-ep93xx.c"
929a5b7474aSLennert Buytenhek #endif
930a5b7474aSLennert Buytenhek 
9311da177e4SLinus Torvalds #ifdef CONFIG_SOC_AU1X00
9321da177e4SLinus Torvalds #include "ohci-au1xxx.c"
9331da177e4SLinus Torvalds #endif
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds #ifdef CONFIG_USB_OHCI_HCD_PPC_SOC
9361da177e4SLinus Torvalds #include "ohci-ppc-soc.c"
9371da177e4SLinus Torvalds #endif
9381da177e4SLinus Torvalds 
9390365ee0aSDavid Brownell #if defined(CONFIG_ARCH_AT91RM9200) || defined(CONFIG_ARCH_AT91SAM9261)
94039a269c0SAndrew Victor #include "ohci-at91.c"
94139a269c0SAndrew Victor #endif
94239a269c0SAndrew Victor 
9431da177e4SLinus Torvalds #if !(defined(CONFIG_PCI) \
9441da177e4SLinus Torvalds       || defined(CONFIG_SA1111) \
9453eb0c5f4SBen Dooks       || defined(CONFIG_ARCH_S3C2410) \
9461da177e4SLinus Torvalds       || defined(CONFIG_ARCH_OMAP) \
9471da177e4SLinus Torvalds       || defined (CONFIG_ARCH_LH7A404) \
9481da177e4SLinus Torvalds       || defined (CONFIG_PXA27x) \
949a5b7474aSLennert Buytenhek       || defined (CONFIG_ARCH_EP93XX) \
9501da177e4SLinus Torvalds       || defined (CONFIG_SOC_AU1X00) \
9511da177e4SLinus Torvalds       || defined (CONFIG_USB_OHCI_HCD_PPC_SOC) \
95239a269c0SAndrew Victor       || defined (CONFIG_ARCH_AT91RM9200) \
9530365ee0aSDavid Brownell       || defined (CONFIG_ARCH_AT91SAM9261) \
9541da177e4SLinus Torvalds 	)
9551da177e4SLinus Torvalds #error "missing bus glue for ohci-hcd"
9561da177e4SLinus Torvalds #endif
957