xref: /linux/drivers/usb/host/ohci-hcd.c (revision 7b81cb6bddd2c4f2489506771070924bd0ae9902)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-1.0+
21da177e4SLinus Torvalds /*
3578333abSAlan Stern  * Open Host Controller Interface (OHCI) driver for USB.
4578333abSAlan Stern  *
5578333abSAlan Stern  * Maintainer: Alan Stern <stern@rowland.harvard.edu>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
81da177e4SLinus Torvalds  * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * [ Initialisation is based on Linus'  ]
111da177e4SLinus Torvalds  * [ uhci code and gregs ohci fragments ]
121da177e4SLinus Torvalds  * [ (C) Copyright 1999 Linus Torvalds  ]
131da177e4SLinus Torvalds  * [ (C) Copyright 1999 Gregory P. Smith]
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * OHCI is the main "non-Intel/VIA" standard for USB 1.1 host controller
171da177e4SLinus Torvalds  * interfaces (though some non-x86 Intel chips use it).  It supports
181da177e4SLinus Torvalds  * smarter hardware than UHCI.  A download link for the spec available
191da177e4SLinus Torvalds  * through the http://www.usb.org website.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * This file is licenced under the GPL.
221da177e4SLinus Torvalds  */
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <linux/module.h>
251da177e4SLinus Torvalds #include <linux/moduleparam.h>
261da177e4SLinus Torvalds #include <linux/pci.h>
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/delay.h>
291da177e4SLinus Torvalds #include <linux/ioport.h>
301da177e4SLinus Torvalds #include <linux/sched.h>
311da177e4SLinus Torvalds #include <linux/slab.h>
321da177e4SLinus Torvalds #include <linux/errno.h>
331da177e4SLinus Torvalds #include <linux/init.h>
341da177e4SLinus Torvalds #include <linux/timer.h>
351da177e4SLinus Torvalds #include <linux/list.h>
361da177e4SLinus Torvalds #include <linux/usb.h>
373a16f7b4SDavid Brownell #include <linux/usb/otg.h>
3827729aadSEric Lescouet #include <linux/usb/hcd.h>
391da177e4SLinus Torvalds #include <linux/dma-mapping.h>
40f4df0e33SDavid Brownell #include <linux/dmapool.h>
41d576bb9fSMichael Hanselmann #include <linux/workqueue.h>
42684c19e0STony Jones #include <linux/debugfs.h>
43b0310c2fSLaurentiu Tudor #include <linux/genalloc.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <asm/io.h>
461da177e4SLinus Torvalds #include <asm/irq.h>
471da177e4SLinus Torvalds #include <asm/unaligned.h>
481da177e4SLinus Torvalds #include <asm/byteorder.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define DRIVER_AUTHOR "Roman Weissgaerber, David Brownell"
521da177e4SLinus Torvalds #define DRIVER_DESC "USB 1.1 'Open' Host Controller (OHCI) Driver"
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* For initializing controller (mask in an HCFS mode too) */
571da177e4SLinus Torvalds #define	OHCI_CONTROL_INIT	OHCI_CTRL_CBSR
581da177e4SLinus Torvalds #define	OHCI_INTR_INIT \
59d413984aSDavid Brownell 		(OHCI_INTR_MIE | OHCI_INTR_RHSC | OHCI_INTR_UE \
60d413984aSDavid Brownell 		| OHCI_INTR_RD | OHCI_INTR_WDH)
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #ifdef __hppa__
631da177e4SLinus Torvalds /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
641da177e4SLinus Torvalds #define	IR_DISABLE
651da177e4SLinus Torvalds #endif
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #ifdef CONFIG_ARCH_OMAP
681da177e4SLinus Torvalds /* OMAP doesn't support IR (no SMM; not needed) */
691da177e4SLinus Torvalds #define	IR_DISABLE
701da177e4SLinus Torvalds #endif
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds static const char	hcd_name [] = "ohci_hcd";
751da177e4SLinus Torvalds 
76d413984aSDavid Brownell #define	STATECHANGE_DELAY	msecs_to_jiffies(300)
77ed6d6f8fSBryan Paluch #define	IO_WATCHDOG_DELAY	msecs_to_jiffies(275)
78b2685bdaSShigeru Yoshida #define	IO_WATCHDOG_OFF		0xffffff00
79d413984aSDavid Brownell 
801da177e4SLinus Torvalds #include "ohci.h"
81ad93562bSAndiry Xu #include "pci-quirks.h"
821da177e4SLinus Torvalds 
83256dbcd8SAlan Stern static void ohci_dump(struct ohci_hcd *ohci);
841da177e4SLinus Torvalds static void ohci_stop(struct usb_hcd *hcd);
85e99e88a9SKees Cook static void io_watchdog_func(struct timer_list *t);
86ab1666c1SLibin Yang 
871da177e4SLinus Torvalds #include "ohci-hub.c"
881da177e4SLinus Torvalds #include "ohci-dbg.c"
891da177e4SLinus Torvalds #include "ohci-mem.c"
901da177e4SLinus Torvalds #include "ohci-q.c"
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /*
941da177e4SLinus Torvalds  * On architectures with edge-triggered interrupts we must never return
951da177e4SLinus Torvalds  * IRQ_NONE.
961da177e4SLinus Torvalds  */
971da177e4SLinus Torvalds #if defined(CONFIG_SA1111)  /* ... or other edge-triggered systems */
981da177e4SLinus Torvalds #define IRQ_NOTMINE	IRQ_HANDLED
991da177e4SLinus Torvalds #else
1001da177e4SLinus Torvalds #define IRQ_NOTMINE	IRQ_NONE
1011da177e4SLinus Torvalds #endif
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds /* Some boards misreport power switching/overcurrent */
105900937c0SGeyslan G. Bem static bool distrust_firmware = true;
1061da177e4SLinus Torvalds module_param (distrust_firmware, bool, 0);
1071da177e4SLinus Torvalds MODULE_PARM_DESC (distrust_firmware,
1081da177e4SLinus Torvalds 	"true to distrust firmware power/overcurrent setup");
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /* Some boards leave IR set wrongly, since they fail BIOS/SMM handshakes */
111900937c0SGeyslan G. Bem static bool no_handshake;
1121da177e4SLinus Torvalds module_param (no_handshake, bool, 0);
1131da177e4SLinus Torvalds MODULE_PARM_DESC (no_handshake, "true (not default) disables BIOS handshake");
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
1161da177e4SLinus Torvalds 
1176f65126cSAlan Stern static int number_of_tds(struct urb *urb)
1186f65126cSAlan Stern {
1196f65126cSAlan Stern 	int			len, i, num, this_sg_len;
1206f65126cSAlan Stern 	struct scatterlist	*sg;
1216f65126cSAlan Stern 
1226f65126cSAlan Stern 	len = urb->transfer_buffer_length;
1236f65126cSAlan Stern 	i = urb->num_mapped_sgs;
1246f65126cSAlan Stern 
1256f65126cSAlan Stern 	if (len > 0 && i > 0) {		/* Scatter-gather transfer */
1266f65126cSAlan Stern 		num = 0;
1276f65126cSAlan Stern 		sg = urb->sg;
1286f65126cSAlan Stern 		for (;;) {
1296f65126cSAlan Stern 			this_sg_len = min_t(int, sg_dma_len(sg), len);
1306f65126cSAlan Stern 			num += DIV_ROUND_UP(this_sg_len, 4096);
1316f65126cSAlan Stern 			len -= this_sg_len;
1326f65126cSAlan Stern 			if (--i <= 0 || len <= 0)
1336f65126cSAlan Stern 				break;
1346f65126cSAlan Stern 			sg = sg_next(sg);
1356f65126cSAlan Stern 		}
1366f65126cSAlan Stern 
1376f65126cSAlan Stern 	} else {			/* Non-SG transfer */
1386f65126cSAlan Stern 		/* one TD for every 4096 Bytes (could be up to 8K) */
1396f65126cSAlan Stern 		num = DIV_ROUND_UP(len, 4096);
1406f65126cSAlan Stern 	}
1416f65126cSAlan Stern 	return num;
1426f65126cSAlan Stern }
1436f65126cSAlan Stern 
1441da177e4SLinus Torvalds /*
1451da177e4SLinus Torvalds  * queue up an urb for anything except the root hub
1461da177e4SLinus Torvalds  */
1471da177e4SLinus Torvalds static int ohci_urb_enqueue (
1481da177e4SLinus Torvalds 	struct usb_hcd	*hcd,
1491da177e4SLinus Torvalds 	struct urb	*urb,
15055016f10SAl Viro 	gfp_t		mem_flags
1511da177e4SLinus Torvalds ) {
1521da177e4SLinus Torvalds 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
1531da177e4SLinus Torvalds 	struct ed	*ed;
1541da177e4SLinus Torvalds 	urb_priv_t	*urb_priv;
1551da177e4SLinus Torvalds 	unsigned int	pipe = urb->pipe;
1561da177e4SLinus Torvalds 	int		i, size = 0;
1571da177e4SLinus Torvalds 	unsigned long	flags;
1581da177e4SLinus Torvalds 	int		retval = 0;
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	/* every endpoint has a ed, locate and maybe (re)initialize it */
16171f46340SGreg Kroah-Hartman 	ed = ed_get(ohci, urb->ep, urb->dev, pipe, urb->interval);
16271f46340SGreg Kroah-Hartman 	if (! ed)
1631da177e4SLinus Torvalds 		return -ENOMEM;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	/* for the private part of the URB we need the number of TDs (size) */
1661da177e4SLinus Torvalds 	switch (ed->type) {
1671da177e4SLinus Torvalds 		case PIPE_CONTROL:
1681da177e4SLinus Torvalds 			/* td_submit_urb() doesn't yet handle these */
1691da177e4SLinus Torvalds 			if (urb->transfer_buffer_length > 4096)
1701da177e4SLinus Torvalds 				return -EMSGSIZE;
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 			/* 1 TD for setup, 1 for ACK, plus ... */
1731da177e4SLinus Torvalds 			size = 2;
1741da177e4SLinus Torvalds 			/* FALLTHROUGH */
1751da177e4SLinus Torvalds 		// case PIPE_INTERRUPT:
1761da177e4SLinus Torvalds 		// case PIPE_BULK:
1771da177e4SLinus Torvalds 		default:
1786f65126cSAlan Stern 			size += number_of_tds(urb);
1796f65126cSAlan Stern 			/* maybe a zero-length packet to wrap it up */
1801da177e4SLinus Torvalds 			if (size == 0)
1811da177e4SLinus Torvalds 				size++;
1821da177e4SLinus Torvalds 			else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
1831da177e4SLinus Torvalds 				&& (urb->transfer_buffer_length
1841da177e4SLinus Torvalds 					% usb_maxpacket (urb->dev, pipe,
1851da177e4SLinus Torvalds 						usb_pipeout (pipe))) == 0)
1861da177e4SLinus Torvalds 				size++;
1871da177e4SLinus Torvalds 			break;
1881da177e4SLinus Torvalds 		case PIPE_ISOCHRONOUS: /* number of packets from URB */
1891da177e4SLinus Torvalds 			size = urb->number_of_packets;
1901da177e4SLinus Torvalds 			break;
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	/* allocate the private part of the URB */
194dd00cc48SYoann Padioleau 	urb_priv = kzalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
1951da177e4SLinus Torvalds 			mem_flags);
1961da177e4SLinus Torvalds 	if (!urb_priv)
1971da177e4SLinus Torvalds 		return -ENOMEM;
1981da177e4SLinus Torvalds 	INIT_LIST_HEAD (&urb_priv->pending);
1991da177e4SLinus Torvalds 	urb_priv->length = size;
2001da177e4SLinus Torvalds 	urb_priv->ed = ed;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	/* allocate the TDs (deferring hash chain updates) */
2031da177e4SLinus Torvalds 	for (i = 0; i < size; i++) {
2041da177e4SLinus Torvalds 		urb_priv->td [i] = td_alloc (ohci, mem_flags);
2051da177e4SLinus Torvalds 		if (!urb_priv->td [i]) {
2061da177e4SLinus Torvalds 			urb_priv->length = i;
2071da177e4SLinus Torvalds 			urb_free_priv (ohci, urb_priv);
2081da177e4SLinus Torvalds 			return -ENOMEM;
2091da177e4SLinus Torvalds 		}
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	/* don't submit to a dead HC */
215541c7d43SAlan Stern 	if (!HCD_HW_ACCESSIBLE(hcd)) {
2168de98402SBenjamin Herrenschmidt 		retval = -ENODEV;
2178de98402SBenjamin Herrenschmidt 		goto fail;
2188de98402SBenjamin Herrenschmidt 	}
219b7463c71SAlan Stern 	if (ohci->rh_state != OHCI_RH_RUNNING) {
2201da177e4SLinus Torvalds 		retval = -ENODEV;
2211da177e4SLinus Torvalds 		goto fail;
2221da177e4SLinus Torvalds 	}
223e9df41c5SAlan Stern 	retval = usb_hcd_link_urb_to_ep(hcd, urb);
224e9df41c5SAlan Stern 	if (retval)
2251da177e4SLinus Torvalds 		goto fail;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	/* schedule the ed if needed */
2281da177e4SLinus Torvalds 	if (ed->state == ED_IDLE) {
2291da177e4SLinus Torvalds 		retval = ed_schedule (ohci, ed);
230e9df41c5SAlan Stern 		if (retval < 0) {
231e9df41c5SAlan Stern 			usb_hcd_unlink_urb_from_ep(hcd, urb);
232e9df41c5SAlan Stern 			goto fail;
233e9df41c5SAlan Stern 		}
23481e38333SAlan Stern 
23581e38333SAlan Stern 		/* Start up the I/O watchdog timer, if it's not running */
236b2685bdaSShigeru Yoshida 		if (ohci->prev_frame_no == IO_WATCHDOG_OFF &&
23721a60f6eSGerd Hoffmann 				list_empty(&ohci->eds_in_use) &&
23821a60f6eSGerd Hoffmann 				!(ohci->flags & OHCI_QUIRK_QEMU)) {
239499b3803SAlan Stern 			ohci->prev_frame_no = ohci_frame_no(ohci);
24081e38333SAlan Stern 			mod_timer(&ohci->io_watchdog,
24181e38333SAlan Stern 					jiffies + IO_WATCHDOG_DELAY);
242499b3803SAlan Stern 		}
24381e38333SAlan Stern 		list_add(&ed->in_use_list, &ohci->eds_in_use);
24481e38333SAlan Stern 
2451da177e4SLinus Torvalds 		if (ed->type == PIPE_ISOCHRONOUS) {
2461da177e4SLinus Torvalds 			u16	frame = ohci_frame_no(ohci);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 			/* delay a few frames before the first TD */
2491da177e4SLinus Torvalds 			frame += max_t (u16, 8, ed->interval);
2501da177e4SLinus Torvalds 			frame &= ~(ed->interval - 1);
2511da177e4SLinus Torvalds 			frame |= ed->branch;
2521da177e4SLinus Torvalds 			urb->start_frame = frame;
253a8693424SAlan Stern 			ed->last_iso = frame + ed->interval * (size - 1);
2541da177e4SLinus Torvalds 		}
2556a41b4d3SAlan Stern 	} else if (ed->type == PIPE_ISOCHRONOUS) {
256e1944017SAlan Stern 		u16	next = ohci_frame_no(ohci) + 1;
2576a41b4d3SAlan Stern 		u16	frame = ed->last_iso + ed->interval;
258a8693424SAlan Stern 		u16	length = ed->interval * (size - 1);
2596a41b4d3SAlan Stern 
2606a41b4d3SAlan Stern 		/* Behind the scheduling threshold? */
2616a41b4d3SAlan Stern 		if (unlikely(tick_before(frame, next))) {
2626a41b4d3SAlan Stern 
263a8693424SAlan Stern 			/* URB_ISO_ASAP: Round up to the first available slot */
264815fa7b9SAlan Stern 			if (urb->transfer_flags & URB_ISO_ASAP) {
2656a41b4d3SAlan Stern 				frame += (next - frame + ed->interval - 1) &
2666a41b4d3SAlan Stern 						-ed->interval;
2676a41b4d3SAlan Stern 
2686a41b4d3SAlan Stern 			/*
269a8693424SAlan Stern 			 * Not ASAP: Use the next slot in the stream,
270a8693424SAlan Stern 			 * no matter what.
2716a41b4d3SAlan Stern 			 */
272815fa7b9SAlan Stern 			} else {
2736a41b4d3SAlan Stern 				/*
2746a41b4d3SAlan Stern 				 * Some OHCI hardware doesn't handle late TDs
275815fa7b9SAlan Stern 				 * correctly.  After retiring them it proceeds
276815fa7b9SAlan Stern 				 * to the next ED instead of the next TD.
277815fa7b9SAlan Stern 				 * Therefore we have to omit the late TDs
278815fa7b9SAlan Stern 				 * entirely.
2796a41b4d3SAlan Stern 				 */
280815fa7b9SAlan Stern 				urb_priv->td_cnt = DIV_ROUND_UP(
281815fa7b9SAlan Stern 						(u16) (next - frame),
2826a41b4d3SAlan Stern 						ed->interval);
283a8693424SAlan Stern 				if (urb_priv->td_cnt >= urb_priv->length) {
284a8693424SAlan Stern 					++urb_priv->td_cnt;	/* Mark it */
285a8693424SAlan Stern 					ohci_dbg(ohci, "iso underrun %p (%u+%u < %u)\n",
286a8693424SAlan Stern 							urb, frame, length,
287a8693424SAlan Stern 							next);
288a8693424SAlan Stern 				}
2896a41b4d3SAlan Stern 			}
290815fa7b9SAlan Stern 		}
2916a41b4d3SAlan Stern 		urb->start_frame = frame;
292a8693424SAlan Stern 		ed->last_iso = frame + length;
2936a41b4d3SAlan Stern 	}
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	/* fill the TDs and link them to the ed; and
2961da177e4SLinus Torvalds 	 * enable that part of the schedule, if needed
2971da177e4SLinus Torvalds 	 * and update count of queued periodic urbs
2981da177e4SLinus Torvalds 	 */
2991da177e4SLinus Torvalds 	urb->hcpriv = urb_priv;
3001da177e4SLinus Torvalds 	td_submit_urb (ohci, urb);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds fail:
3031da177e4SLinus Torvalds 	if (retval)
3041da177e4SLinus Torvalds 		urb_free_priv (ohci, urb_priv);
3051da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
3061da177e4SLinus Torvalds 	return retval;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /*
31055d84968SAlan Stern  * decouple the URB from the HC queues (TDs, urb_priv).
31155d84968SAlan Stern  * reporting is always done
3121da177e4SLinus Torvalds  * asynchronously, and we might be dealing with an urb that's
3131da177e4SLinus Torvalds  * partially transferred, or an ED with other urbs being unlinked.
3141da177e4SLinus Torvalds  */
315e9df41c5SAlan Stern static int ohci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
3181da177e4SLinus Torvalds 	unsigned long		flags;
319e9df41c5SAlan Stern 	int			rc;
3208b3ab0edSAlan Stern 	urb_priv_t		*urb_priv;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
323e9df41c5SAlan Stern 	rc = usb_hcd_check_unlink_urb(hcd, urb, status);
3248b3ab0edSAlan Stern 	if (rc == 0) {
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->ed->state == ED_OPER)
3321da177e4SLinus Torvalds 			start_ed_unlink(ohci, urb_priv->ed);
3338b3ab0edSAlan Stern 
3348b3ab0edSAlan Stern 		if (ohci->rh_state != OHCI_RH_RUNNING) {
3358b3ab0edSAlan Stern 			/* With HC dead, we can clean up right away */
336cdb4dd15SAlan Stern 			ohci_work(ohci);
3371da177e4SLinus Torvalds 		}
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
340e9df41c5SAlan Stern 	return rc;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /* frees config/altsetting state for endpoints,
3461da177e4SLinus Torvalds  * including ED memory, dummy TD, and bulk/intr data toggle
3471da177e4SLinus Torvalds  */
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds static void
3501da177e4SLinus Torvalds ohci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
3531da177e4SLinus Torvalds 	unsigned long		flags;
3541da177e4SLinus Torvalds 	struct ed		*ed = ep->hcpriv;
3551da177e4SLinus Torvalds 	unsigned		limit = 1000;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	/* ASSERT:  any requests/urbs are being unlinked */
3581da177e4SLinus Torvalds 	/* ASSERT:  nobody can be submitting urbs for this any more */
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	if (!ed)
3611da177e4SLinus Torvalds 		return;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds rescan:
3641da177e4SLinus Torvalds 	spin_lock_irqsave (&ohci->lock, flags);
3651da177e4SLinus Torvalds 
366b7463c71SAlan Stern 	if (ohci->rh_state != OHCI_RH_RUNNING) {
3671da177e4SLinus Torvalds sanitize:
3681da177e4SLinus Torvalds 		ed->state = ED_IDLE;
369cdb4dd15SAlan Stern 		ohci_work(ohci);
3701da177e4SLinus Torvalds 	}
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	switch (ed->state) {
3731da177e4SLinus Torvalds 	case ED_UNLINK:		/* wait for hw to finish? */
3741da177e4SLinus Torvalds 		/* major IRQ delivery trouble loses INTR_SF too... */
3751da177e4SLinus Torvalds 		if (limit-- == 0) {
37689a0fd18SMike Nuss 			ohci_warn(ohci, "ED unlink timeout\n");
3771da177e4SLinus Torvalds 			goto sanitize;
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 		spin_unlock_irqrestore (&ohci->lock, flags);
38022c43863SNishanth Aravamudan 		schedule_timeout_uninterruptible(1);
3811da177e4SLinus Torvalds 		goto rescan;
3821da177e4SLinus Torvalds 	case ED_IDLE:		/* fully unlinked */
3831da177e4SLinus Torvalds 		if (list_empty (&ed->td_list)) {
3841da177e4SLinus Torvalds 			td_free (ohci, ed->dummy);
3851da177e4SLinus Torvalds 			ed_free (ohci, ed);
3861da177e4SLinus Torvalds 			break;
3871da177e4SLinus Torvalds 		}
388f5a3908eSGustavo A. R. Silva 		/* fall through */
3891da177e4SLinus Torvalds 	default:
3901da177e4SLinus Torvalds 		/* caller was supposed to have unlinked any requests;
3911da177e4SLinus Torvalds 		 * that's not our job.  can't recover; must leak ed.
3921da177e4SLinus Torvalds 		 */
3931da177e4SLinus Torvalds 		ohci_err (ohci, "leak ed %p (#%02x) state %d%s\n",
3941da177e4SLinus Torvalds 			ed, ep->desc.bEndpointAddress, ed->state,
3951da177e4SLinus Torvalds 			list_empty (&ed->td_list) ? "" : " (has tds)");
3961da177e4SLinus Torvalds 		td_free (ohci, ed->dummy);
3971da177e4SLinus Torvalds 		break;
3981da177e4SLinus Torvalds 	}
3991da177e4SLinus Torvalds 	ep->hcpriv = NULL;
4001da177e4SLinus Torvalds 	spin_unlock_irqrestore (&ohci->lock, flags);
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds static int ohci_get_frame (struct usb_hcd *hcd)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	return ohci_frame_no(ohci);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds static void ohci_usb_reset (struct ohci_hcd *ohci)
4111da177e4SLinus Torvalds {
4121da177e4SLinus Torvalds 	ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
4131da177e4SLinus Torvalds 	ohci->hc_control &= OHCI_CTRL_RWC;
4141da177e4SLinus Torvalds 	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
415b7463c71SAlan Stern 	ohci->rh_state = OHCI_RH_HALTED;
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds 
41864a21d02SAleksey Gorelov /* ohci_shutdown forcibly disables IRQs and DMA, helping kexec and
419f4df0e33SDavid Brownell  * other cases where the next software may expect clean state from the
420f4df0e33SDavid Brownell  * "firmware".  this is bus-neutral, unlike shutdown() methods.
421f4df0e33SDavid Brownell  */
42264a21d02SAleksey Gorelov static void
42364a21d02SAleksey Gorelov ohci_shutdown (struct usb_hcd *hcd)
424f4df0e33SDavid Brownell {
425f4df0e33SDavid Brownell 	struct ohci_hcd *ohci;
426f4df0e33SDavid Brownell 
42764a21d02SAleksey Gorelov 	ohci = hcd_to_ohci (hcd);
428c6187597SAlan Stern 	ohci_writel(ohci, (u32) ~0, &ohci->regs->intrdisable);
4293df7169eSAlan Stern 
430c6187597SAlan Stern 	/* Software reset, after which the controller goes into SUSPEND */
431c6187597SAlan Stern 	ohci_writel(ohci, OHCI_HCR, &ohci->regs->cmdstatus);
432c6187597SAlan Stern 	ohci_readl(ohci, &ohci->regs->cmdstatus);	/* flush the writes */
433c6187597SAlan Stern 	udelay(10);
4343df7169eSAlan Stern 
435c6187597SAlan Stern 	ohci_writel(ohci, ohci->fminterval, &ohci->regs->fminterval);
43681e38333SAlan Stern 	ohci->rh_state = OHCI_RH_HALTED;
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 
4506f65126cSAlan Stern 	/* Accept arbitrarily long scatter-gather lists */
4512d7a3dc3SLaurentiu Tudor 	if (!hcd->localmem_pool)
4526f65126cSAlan Stern 		hcd->self.sg_tablesize = ~0;
4536f65126cSAlan Stern 
4541133cd8aSDmitry Baryshkov 	if (distrust_firmware)
4551133cd8aSDmitry Baryshkov 		ohci->flags |= OHCI_QUIRK_HUB_POWER;
4561133cd8aSDmitry Baryshkov 
457b7463c71SAlan Stern 	ohci->rh_state = OHCI_RH_HALTED;
4586a9062f3SDavid Brownell 	ohci->regs = hcd->regs;
4591da177e4SLinus Torvalds 
4606a9062f3SDavid Brownell 	/* REVISIT this BIOS handshake is now moved into PCI "quirks", and
4616a9062f3SDavid Brownell 	 * was never needed for most non-PCI systems ... remove the code?
4626a9062f3SDavid Brownell 	 */
4636a9062f3SDavid Brownell 
4641da177e4SLinus Torvalds #ifndef IR_DISABLE
4651da177e4SLinus Torvalds 	/* SMM owns the HC?  not for long! */
4661da177e4SLinus Torvalds 	if (!no_handshake && ohci_readl (ohci,
4671da177e4SLinus Torvalds 					&ohci->regs->control) & OHCI_CTRL_IR) {
4681da177e4SLinus Torvalds 		u32 temp;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 		ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 		/* this timeout is arbitrary.  we make it long, so systems
4731da177e4SLinus Torvalds 		 * depending on usb keyboards may be usable even if the
4741da177e4SLinus Torvalds 		 * BIOS/SMM code seems pretty broken.
4751da177e4SLinus Torvalds 		 */
4761da177e4SLinus Torvalds 		temp = 500;	/* arbitrary: five seconds */
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_OC, &ohci->regs->intrenable);
4791da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_OCR, &ohci->regs->cmdstatus);
4801da177e4SLinus Torvalds 		while (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_IR) {
4811da177e4SLinus Torvalds 			msleep (10);
4821da177e4SLinus Torvalds 			if (--temp == 0) {
4831da177e4SLinus Torvalds 				ohci_err (ohci, "USB HC takeover failed!"
4841da177e4SLinus Torvalds 					"  (BIOS/SMM bug)\n");
4851da177e4SLinus Torvalds 				return -EBUSY;
4861da177e4SLinus Torvalds 			}
4871da177e4SLinus Torvalds 		}
4881da177e4SLinus Torvalds 		ohci_usb_reset (ohci);
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds #endif
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* Disable HC interrupts */
4931da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
4946a9062f3SDavid Brownell 
4956a9062f3SDavid Brownell 	/* flush the writes, and save key bits like RWC */
4966a9062f3SDavid Brownell 	if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
4976a9062f3SDavid Brownell 		ohci->hc_control |= OHCI_CTRL_RWC;
4981da177e4SLinus Torvalds 
499fdd13b36SDavid Brownell 	/* Read the number of ports unless overridden */
500fdd13b36SDavid Brownell 	if (ohci->num_ports == 0)
501fdd13b36SDavid Brownell 		ohci->num_ports = roothub_a(ohci) & RH_A_NDP;
502fdd13b36SDavid Brownell 
5031da177e4SLinus Torvalds 	if (ohci->hcca)
5041da177e4SLinus Torvalds 		return 0;
5051da177e4SLinus Torvalds 
506e99e88a9SKees Cook 	timer_setup(&ohci->io_watchdog, io_watchdog_func, 0);
507b2685bdaSShigeru Yoshida 	ohci->prev_frame_no = IO_WATCHDOG_OFF;
50881e38333SAlan Stern 
509b0310c2fSLaurentiu Tudor 	if (hcd->localmem_pool)
510ff2437beSFredrik Noring 		ohci->hcca = gen_pool_dma_alloc_align(hcd->localmem_pool,
511b0310c2fSLaurentiu Tudor 						sizeof(*ohci->hcca),
512ff2437beSFredrik Noring 						&ohci->hcca_dma, 256);
513b0310c2fSLaurentiu Tudor 	else
5146a9062f3SDavid Brownell 		ohci->hcca = dma_alloc_coherent(hcd->self.controller,
515b0310c2fSLaurentiu Tudor 						sizeof(*ohci->hcca),
516b0310c2fSLaurentiu Tudor 						&ohci->hcca_dma,
517b0310c2fSLaurentiu Tudor 						GFP_KERNEL);
5181da177e4SLinus Torvalds 	if (!ohci->hcca)
5191da177e4SLinus Torvalds 		return -ENOMEM;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if ((ret = ohci_mem_init (ohci)) < 0)
5226a9062f3SDavid Brownell 		ohci_stop (hcd);
5236a9062f3SDavid Brownell 	else {
5246a9062f3SDavid Brownell 		create_debug_files (ohci);
5256a9062f3SDavid Brownell 	}
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	return ret;
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds /* Start an OHCI controller, set the BUS operational
5331da177e4SLinus Torvalds  * resets USB and controller
5341da177e4SLinus Torvalds  * enable interrupts
5351da177e4SLinus Torvalds  */
5361da177e4SLinus Torvalds static int ohci_run (struct ohci_hcd *ohci)
5371da177e4SLinus Torvalds {
53896f90a8bSH Hartley Sweeten 	u32			mask, val;
5391da177e4SLinus Torvalds 	int			first = ohci->fminterval == 0;
5406a9062f3SDavid Brownell 	struct usb_hcd		*hcd = ohci_to_hcd(ohci);
5411da177e4SLinus Torvalds 
542b7463c71SAlan Stern 	ohci->rh_state = OHCI_RH_HALTED;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	/* boot firmware should have set this up (5.1.1.3.1) */
5451da177e4SLinus Torvalds 	if (first) {
5461da177e4SLinus Torvalds 
54796f90a8bSH Hartley Sweeten 		val = ohci_readl (ohci, &ohci->regs->fminterval);
54896f90a8bSH Hartley Sweeten 		ohci->fminterval = val & 0x3fff;
5491da177e4SLinus Torvalds 		if (ohci->fminterval != FI)
5501da177e4SLinus Torvalds 			ohci_dbg (ohci, "fminterval delta %d\n",
5511da177e4SLinus Torvalds 				ohci->fminterval - FI);
5521da177e4SLinus Torvalds 		ohci->fminterval |= FSMP (ohci->fminterval) << 16;
5531da177e4SLinus Torvalds 		/* also: power/overcurrent flags in roothub.a */
5541da177e4SLinus Torvalds 	}
5551da177e4SLinus Torvalds 
5566fd9086aSAlan Stern 	/* Reset USB nearly "by the book".  RemoteWakeupConnected has
5576fd9086aSAlan Stern 	 * to be checked in case boot firmware (BIOS/SMM/...) has set up
5586fd9086aSAlan Stern 	 * wakeup in a way the bus isn't aware of (e.g., legacy PCI PM).
5596fd9086aSAlan Stern 	 * If the bus glue detected wakeup capability then it should
560bcca06efSAlan Stern 	 * already be enabled; if so we'll just enable it again.
5611da177e4SLinus Torvalds 	 */
562bcca06efSAlan Stern 	if ((ohci->hc_control & OHCI_CTRL_RWC) != 0)
563bcca06efSAlan Stern 		device_set_wakeup_capable(hcd->self.controller, 1);
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	switch (ohci->hc_control & OHCI_CTRL_HCFS) {
5661da177e4SLinus Torvalds 	case OHCI_USB_OPER:
56796f90a8bSH Hartley Sweeten 		val = 0;
5681da177e4SLinus Torvalds 		break;
5691da177e4SLinus Torvalds 	case OHCI_USB_SUSPEND:
5701da177e4SLinus Torvalds 	case OHCI_USB_RESUME:
5711da177e4SLinus Torvalds 		ohci->hc_control &= OHCI_CTRL_RWC;
5721da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_USB_RESUME;
57396f90a8bSH Hartley Sweeten 		val = 10 /* msec wait */;
5741da177e4SLinus Torvalds 		break;
5751da177e4SLinus Torvalds 	// case OHCI_USB_RESET:
5761da177e4SLinus Torvalds 	default:
5771da177e4SLinus Torvalds 		ohci->hc_control &= OHCI_CTRL_RWC;
5781da177e4SLinus Torvalds 		ohci->hc_control |= OHCI_USB_RESET;
57996f90a8bSH Hartley Sweeten 		val = 50 /* msec wait */;
5801da177e4SLinus Torvalds 		break;
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
5831da177e4SLinus Torvalds 	// flush the writes
5841da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
58596f90a8bSH Hartley Sweeten 	msleep(val);
586383975d7SAlan Stern 
5871da177e4SLinus Torvalds 	memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	/* 2msec timelimit here means no irqs/preempt */
5901da177e4SLinus Torvalds 	spin_lock_irq (&ohci->lock);
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds retry:
5931da177e4SLinus Torvalds 	/* HC Reset requires max 10 us delay */
5941da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_HCR,  &ohci->regs->cmdstatus);
59596f90a8bSH Hartley Sweeten 	val = 30;	/* ... allow extra time */
5961da177e4SLinus Torvalds 	while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
59796f90a8bSH Hartley Sweeten 		if (--val == 0) {
5981da177e4SLinus Torvalds 			spin_unlock_irq (&ohci->lock);
5991da177e4SLinus Torvalds 			ohci_err (ohci, "USB HC reset timed out!\n");
6001da177e4SLinus Torvalds 			return -1;
6011da177e4SLinus Torvalds 		}
6021da177e4SLinus Torvalds 		udelay (1);
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	/* now we're in the SUSPEND state ... must go OPERATIONAL
6061da177e4SLinus Torvalds 	 * within 2msec else HC enters RESUME
6071da177e4SLinus Torvalds 	 *
6081da177e4SLinus Torvalds 	 * ... but some hardware won't init fmInterval "by the book"
6091da177e4SLinus Torvalds 	 * (SiS, OPTi ...), so reset again instead.  SiS doesn't need
6101da177e4SLinus Torvalds 	 * this if we write fmInterval after we're OPERATIONAL.
6111da177e4SLinus Torvalds 	 * Unclear about ALi, ServerWorks, and others ... this could
6121da177e4SLinus Torvalds 	 * easily be a longstanding bug in chip init on Linux.
6131da177e4SLinus Torvalds 	 */
6141da177e4SLinus Torvalds 	if (ohci->flags & OHCI_QUIRK_INITRESET) {
6151da177e4SLinus Torvalds 		ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
6161da177e4SLinus Torvalds 		// flush those writes
6171da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	/* Tell the controller where the control and bulk lists are
6211da177e4SLinus Torvalds 	 * The lists are empty now. */
6221da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
6231da177e4SLinus Torvalds 	ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds 	/* a reset clears this */
6261da177e4SLinus Torvalds 	ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	periodic_reinit (ohci);
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	/* some OHCI implementations are finicky about how they init.
6311da177e4SLinus Torvalds 	 * bogus values here mean not even enumeration could work.
6321da177e4SLinus Torvalds 	 */
6331da177e4SLinus Torvalds 	if ((ohci_readl (ohci, &ohci->regs->fminterval) & 0x3fff0000) == 0
6341da177e4SLinus Torvalds 			|| !ohci_readl (ohci, &ohci->regs->periodicstart)) {
6351da177e4SLinus Torvalds 		if (!(ohci->flags & OHCI_QUIRK_INITRESET)) {
6361da177e4SLinus Torvalds 			ohci->flags |= OHCI_QUIRK_INITRESET;
6371da177e4SLinus Torvalds 			ohci_dbg (ohci, "enabling initreset quirk\n");
6381da177e4SLinus Torvalds 			goto retry;
6391da177e4SLinus Torvalds 		}
6401da177e4SLinus Torvalds 		spin_unlock_irq (&ohci->lock);
6411da177e4SLinus Torvalds 		ohci_err (ohci, "init err (%08x %04x)\n",
6421da177e4SLinus Torvalds 			ohci_readl (ohci, &ohci->regs->fminterval),
6431da177e4SLinus Torvalds 			ohci_readl (ohci, &ohci->regs->periodicstart));
6441da177e4SLinus Torvalds 		return -EOVERFLOW;
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 
64737ebb549SPetr Mladek 	/* use rhsc irqs after hub_wq is allocated */
648541c7d43SAlan Stern 	set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
649d413984aSDavid Brownell 	hcd->uses_new_polling = 1;
650d413984aSDavid Brownell 
6511da177e4SLinus Torvalds 	/* start controller operations */
6521da177e4SLinus Torvalds 	ohci->hc_control &= OHCI_CTRL_RWC;
6531da177e4SLinus Torvalds 	ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
6541da177e4SLinus Torvalds 	ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
655b7463c71SAlan Stern 	ohci->rh_state = OHCI_RH_RUNNING;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	/* wake on ConnectStatusChange, matching external hubs */
6581da177e4SLinus Torvalds 	ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
6591da177e4SLinus Torvalds 
6601da177e4SLinus Torvalds 	/* Choose the interrupts we care about now, others later on demand */
6611da177e4SLinus Torvalds 	mask = OHCI_INTR_INIT;
662d413984aSDavid Brownell 	ohci_writel (ohci, ~0, &ohci->regs->intrstatus);
6631da177e4SLinus Torvalds 	ohci_writel (ohci, mask, &ohci->regs->intrenable);
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	/* handle root hub init quirks ... */
66696f90a8bSH Hartley Sweeten 	val = roothub_a (ohci);
66796f90a8bSH Hartley Sweeten 	val &= ~(RH_A_PSM | RH_A_OCPM);
6681da177e4SLinus Torvalds 	if (ohci->flags & OHCI_QUIRK_SUPERIO) {
6691da177e4SLinus Torvalds 		/* NSC 87560 and maybe others */
67096f90a8bSH Hartley Sweeten 		val |= RH_A_NOCP;
67196f90a8bSH Hartley Sweeten 		val &= ~(RH_A_POTPGT | RH_A_NPS);
67296f90a8bSH Hartley Sweeten 		ohci_writel (ohci, val, &ohci->regs->roothub.a);
6731133cd8aSDmitry Baryshkov 	} else if ((ohci->flags & OHCI_QUIRK_AMD756) ||
6741133cd8aSDmitry Baryshkov 			(ohci->flags & OHCI_QUIRK_HUB_POWER)) {
6751da177e4SLinus Torvalds 		/* hub power always on; required for AMD-756 and some
6761da177e4SLinus Torvalds 		 * Mac platforms.  ganged overcurrent reporting, if any.
6771da177e4SLinus Torvalds 		 */
67896f90a8bSH Hartley Sweeten 		val |= RH_A_NPS;
67996f90a8bSH Hartley Sweeten 		ohci_writel (ohci, val, &ohci->regs->roothub.a);
6801da177e4SLinus Torvalds 	}
6811da177e4SLinus Torvalds 	ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
68296f90a8bSH Hartley Sweeten 	ohci_writel (ohci, (val & RH_A_NPS) ? 0 : RH_B_PPCM,
6831da177e4SLinus Torvalds 						&ohci->regs->roothub.b);
6841da177e4SLinus Torvalds 	// flush those writes
6851da177e4SLinus Torvalds 	(void) ohci_readl (ohci, &ohci->regs->control);
6861da177e4SLinus Torvalds 
687d413984aSDavid Brownell 	ohci->next_statechange = jiffies + STATECHANGE_DELAY;
6881da177e4SLinus Torvalds 	spin_unlock_irq (&ohci->lock);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	// POTPGT delay is bits 24-31, in 2 ms units.
69196f90a8bSH Hartley Sweeten 	mdelay ((val >> 23) & 0x1fe);
6921da177e4SLinus Torvalds 
693256dbcd8SAlan Stern 	ohci_dump(ohci);
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds 	return 0;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
69895e44d44SManjunath Goudar /* ohci_setup routine for generic controller initialization */
69995e44d44SManjunath Goudar 
70095e44d44SManjunath Goudar int ohci_setup(struct usb_hcd *hcd)
70195e44d44SManjunath Goudar {
70295e44d44SManjunath Goudar 	struct ohci_hcd		*ohci = hcd_to_ohci(hcd);
70395e44d44SManjunath Goudar 
70495e44d44SManjunath Goudar 	ohci_hcd_init(ohci);
70595e44d44SManjunath Goudar 
70695e44d44SManjunath Goudar 	return ohci_init(ohci);
70795e44d44SManjunath Goudar }
70895e44d44SManjunath Goudar EXPORT_SYMBOL_GPL(ohci_setup);
70995e44d44SManjunath Goudar 
71095e44d44SManjunath Goudar /* ohci_start routine for generic controller start of all OHCI bus glue */
71195e44d44SManjunath Goudar static int ohci_start(struct usb_hcd *hcd)
71295e44d44SManjunath Goudar {
71395e44d44SManjunath Goudar 	struct ohci_hcd		*ohci = hcd_to_ohci(hcd);
71495e44d44SManjunath Goudar 	int	ret;
71595e44d44SManjunath Goudar 
71695e44d44SManjunath Goudar 	ret = ohci_run(ohci);
71795e44d44SManjunath Goudar 	if (ret < 0) {
71895e44d44SManjunath Goudar 		ohci_err(ohci, "can't start\n");
71995e44d44SManjunath Goudar 		ohci_stop(hcd);
72095e44d44SManjunath Goudar 	}
72195e44d44SManjunath Goudar 	return ret;
72295e44d44SManjunath Goudar }
72395e44d44SManjunath Goudar 
7241da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
7251da177e4SLinus Torvalds 
72681e38333SAlan Stern /*
72781e38333SAlan Stern  * Some OHCI controllers are known to lose track of completed TDs.  They
72881e38333SAlan Stern  * don't add the TDs to the hardware done queue, which means we never see
72981e38333SAlan Stern  * them as being completed.
73081e38333SAlan Stern  *
73181e38333SAlan Stern  * This watchdog routine checks for such problems.  Without some way to
73281e38333SAlan Stern  * tell when those TDs have completed, we would never take their EDs off
73381e38333SAlan Stern  * the unlink list.  As a result, URBs could never be dequeued and
73481e38333SAlan Stern  * endpoints could never be released.
73581e38333SAlan Stern  */
736e99e88a9SKees Cook static void io_watchdog_func(struct timer_list *t)
73781e38333SAlan Stern {
738e99e88a9SKees Cook 	struct ohci_hcd	*ohci = from_timer(ohci, t, io_watchdog);
73981e38333SAlan Stern 	bool		takeback_all_pending = false;
74081e38333SAlan Stern 	u32		status;
74181e38333SAlan Stern 	u32		head;
74281e38333SAlan Stern 	struct ed	*ed;
74381e38333SAlan Stern 	struct td	*td, *td_start, *td_next;
744b2685bdaSShigeru Yoshida 	unsigned	frame_no, prev_frame_no = IO_WATCHDOG_OFF;
74581e38333SAlan Stern 	unsigned long	flags;
74681e38333SAlan Stern 
74781e38333SAlan Stern 	spin_lock_irqsave(&ohci->lock, flags);
74881e38333SAlan Stern 
74981e38333SAlan Stern 	/*
75081e38333SAlan Stern 	 * One way to lose track of completed TDs is if the controller
75181e38333SAlan Stern 	 * never writes back the done queue head.  If it hasn't been
75281e38333SAlan Stern 	 * written back since the last time this function ran and if it
75381e38333SAlan Stern 	 * was non-empty at that time, something is badly wrong with the
75481e38333SAlan Stern 	 * hardware.
75581e38333SAlan Stern 	 */
75681e38333SAlan Stern 	status = ohci_readl(ohci, &ohci->regs->intrstatus);
75781e38333SAlan Stern 	if (!(status & OHCI_INTR_WDH) && ohci->wdh_cnt == ohci->prev_wdh_cnt) {
75881e38333SAlan Stern 		if (ohci->prev_donehead) {
75981e38333SAlan Stern 			ohci_err(ohci, "HcDoneHead not written back; disabled\n");
760499b3803SAlan Stern  died:
76181e38333SAlan Stern 			usb_hc_died(ohci_to_hcd(ohci));
76281e38333SAlan Stern 			ohci_dump(ohci);
76381e38333SAlan Stern 			ohci_shutdown(ohci_to_hcd(ohci));
76481e38333SAlan Stern 			goto done;
76581e38333SAlan Stern 		} else {
76681e38333SAlan Stern 			/* No write back because the done queue was empty */
76781e38333SAlan Stern 			takeback_all_pending = true;
76881e38333SAlan Stern 		}
76981e38333SAlan Stern 	}
77081e38333SAlan Stern 
77181e38333SAlan Stern 	/* Check every ED which might have pending TDs */
77281e38333SAlan Stern 	list_for_each_entry(ed, &ohci->eds_in_use, in_use_list) {
77381e38333SAlan Stern 		if (ed->pending_td) {
77481e38333SAlan Stern 			if (takeback_all_pending ||
77581e38333SAlan Stern 					OKAY_TO_TAKEBACK(ohci, ed)) {
77681e38333SAlan Stern 				unsigned tmp = hc32_to_cpu(ohci, ed->hwINFO);
77781e38333SAlan Stern 
77881e38333SAlan Stern 				ohci_dbg(ohci, "takeback pending TD for dev %d ep 0x%x\n",
77981e38333SAlan Stern 						0x007f & tmp,
78081e38333SAlan Stern 						(0x000f & (tmp >> 7)) +
78181e38333SAlan Stern 							((tmp & ED_IN) >> 5));
78281e38333SAlan Stern 				add_to_done_list(ohci, ed->pending_td);
78381e38333SAlan Stern 			}
78481e38333SAlan Stern 		}
78581e38333SAlan Stern 
78681e38333SAlan Stern 		/* Starting from the latest pending TD, */
78781e38333SAlan Stern 		td = ed->pending_td;
78881e38333SAlan Stern 
78981e38333SAlan Stern 		/* or the last TD on the done list, */
79081e38333SAlan Stern 		if (!td) {
79181e38333SAlan Stern 			list_for_each_entry(td_next, &ed->td_list, td_list) {
79281e38333SAlan Stern 				if (!td_next->next_dl_td)
79381e38333SAlan Stern 					break;
79481e38333SAlan Stern 				td = td_next;
79581e38333SAlan Stern 			}
79681e38333SAlan Stern 		}
79781e38333SAlan Stern 
79881e38333SAlan Stern 		/* find the last TD processed by the controller. */
7996aa7de05SMark Rutland 		head = hc32_to_cpu(ohci, READ_ONCE(ed->hwHeadP)) & TD_MASK;
80081e38333SAlan Stern 		td_start = td;
80181e38333SAlan Stern 		td_next = list_prepare_entry(td, &ed->td_list, td_list);
80281e38333SAlan Stern 		list_for_each_entry_continue(td_next, &ed->td_list, td_list) {
80381e38333SAlan Stern 			if (head == (u32) td_next->td_dma)
80481e38333SAlan Stern 				break;
80581e38333SAlan Stern 			td = td_next;	/* head pointer has passed this TD */
80681e38333SAlan Stern 		}
80781e38333SAlan Stern 		if (td != td_start) {
80881e38333SAlan Stern 			/*
80981e38333SAlan Stern 			 * In case a WDH cycle is in progress, we will wait
81081e38333SAlan Stern 			 * for the next two cycles to complete before assuming
81181e38333SAlan Stern 			 * this TD will never get on the done queue.
81281e38333SAlan Stern 			 */
81381e38333SAlan Stern 			ed->takeback_wdh_cnt = ohci->wdh_cnt + 2;
81481e38333SAlan Stern 			ed->pending_td = td;
81581e38333SAlan Stern 		}
81681e38333SAlan Stern 	}
81781e38333SAlan Stern 
81881e38333SAlan Stern 	ohci_work(ohci);
81981e38333SAlan Stern 
82081e38333SAlan Stern 	if (ohci->rh_state == OHCI_RH_RUNNING) {
821499b3803SAlan Stern 
822499b3803SAlan Stern 		/*
823499b3803SAlan Stern 		 * Sometimes a controller just stops working.  We can tell
824499b3803SAlan Stern 		 * by checking that the frame counter has advanced since
825499b3803SAlan Stern 		 * the last time we ran.
826499b3803SAlan Stern 		 *
827499b3803SAlan Stern 		 * But be careful: Some controllers violate the spec by
828499b3803SAlan Stern 		 * stopping their frame counter when no ports are active.
829499b3803SAlan Stern 		 */
830499b3803SAlan Stern 		frame_no = ohci_frame_no(ohci);
831499b3803SAlan Stern 		if (frame_no == ohci->prev_frame_no) {
832499b3803SAlan Stern 			int		active_cnt = 0;
833499b3803SAlan Stern 			int		i;
834499b3803SAlan Stern 			unsigned	tmp;
835499b3803SAlan Stern 
836499b3803SAlan Stern 			for (i = 0; i < ohci->num_ports; ++i) {
837499b3803SAlan Stern 				tmp = roothub_portstatus(ohci, i);
838499b3803SAlan Stern 				/* Enabled and not suspended? */
839499b3803SAlan Stern 				if ((tmp & RH_PS_PES) && !(tmp & RH_PS_PSS))
840499b3803SAlan Stern 					++active_cnt;
841499b3803SAlan Stern 			}
842499b3803SAlan Stern 
843499b3803SAlan Stern 			if (active_cnt > 0) {
844499b3803SAlan Stern 				ohci_err(ohci, "frame counter not updating; disabled\n");
845499b3803SAlan Stern 				goto died;
846499b3803SAlan Stern 			}
847499b3803SAlan Stern 		}
84881e38333SAlan Stern 		if (!list_empty(&ohci->eds_in_use)) {
849b2685bdaSShigeru Yoshida 			prev_frame_no = frame_no;
85081e38333SAlan Stern 			ohci->prev_wdh_cnt = ohci->wdh_cnt;
85181e38333SAlan Stern 			ohci->prev_donehead = ohci_readl(ohci,
85281e38333SAlan Stern 					&ohci->regs->donehead);
85381e38333SAlan Stern 			mod_timer(&ohci->io_watchdog,
85481e38333SAlan Stern 					jiffies + IO_WATCHDOG_DELAY);
85581e38333SAlan Stern 		}
85681e38333SAlan Stern 	}
85781e38333SAlan Stern 
85881e38333SAlan Stern  done:
859b2685bdaSShigeru Yoshida 	ohci->prev_frame_no = prev_frame_no;
86081e38333SAlan Stern 	spin_unlock_irqrestore(&ohci->lock, flags);
86181e38333SAlan Stern }
86281e38333SAlan Stern 
8631da177e4SLinus Torvalds /* an interrupt happens */
8641da177e4SLinus Torvalds 
8657d12e780SDavid Howells static irqreturn_t ohci_irq (struct usb_hcd *hcd)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
8681da177e4SLinus Torvalds 	struct ohci_regs __iomem *regs = ohci->regs;
8691da177e4SLinus Torvalds 	int			ints;
8701da177e4SLinus Torvalds 
871565227c0SBenjamin Herrenschmidt 	/* Read interrupt status (and flush pending writes).  We ignore the
872565227c0SBenjamin Herrenschmidt 	 * optimization of checking the LSB of hcca->done_head; it doesn't
873565227c0SBenjamin Herrenschmidt 	 * work on all systems (edge triggering for OHCI can be a factor).
87489a0fd18SMike Nuss 	 */
875565227c0SBenjamin Herrenschmidt 	ints = ohci_readl(ohci, &regs->intrstatus);
8761da177e4SLinus Torvalds 
877565227c0SBenjamin Herrenschmidt 	/* Check for an all 1's result which is a typical consequence
878565227c0SBenjamin Herrenschmidt 	 * of dead, unclocked, or unplugged (CardBus...) devices
879565227c0SBenjamin Herrenschmidt 	 */
880565227c0SBenjamin Herrenschmidt 	if (ints == ~(u32)0) {
881b7463c71SAlan Stern 		ohci->rh_state = OHCI_RH_HALTED;
8821da177e4SLinus Torvalds 		ohci_dbg (ohci, "device removed!\n");
88369fff59dSAlan Stern 		usb_hc_died(hcd);
8841da177e4SLinus Torvalds 		return IRQ_HANDLED;
885565227c0SBenjamin Herrenschmidt 	}
886565227c0SBenjamin Herrenschmidt 
887565227c0SBenjamin Herrenschmidt 	/* We only care about interrupts that are enabled */
888565227c0SBenjamin Herrenschmidt 	ints &= ohci_readl(ohci, &regs->intrenable);
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds 	/* interrupt for some other device? */
891b7463c71SAlan Stern 	if (ints == 0 || unlikely(ohci->rh_state == OHCI_RH_HALTED))
8921da177e4SLinus Torvalds 		return IRQ_NOTMINE;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	if (ints & OHCI_INTR_UE) {
895d576bb9fSMichael Hanselmann 		// e.g. due to PCI Master/Target Abort
89689a0fd18SMike Nuss 		if (quirk_nec(ohci)) {
897d576bb9fSMichael Hanselmann 			/* Workaround for a silicon bug in some NEC chips used
898d576bb9fSMichael Hanselmann 			 * in Apple's PowerBooks. Adapted from Darwin code.
899d576bb9fSMichael Hanselmann 			 */
900d576bb9fSMichael Hanselmann 			ohci_err (ohci, "OHCI Unrecoverable Error, scheduling NEC chip restart\n");
901d576bb9fSMichael Hanselmann 
902d576bb9fSMichael Hanselmann 			ohci_writel (ohci, OHCI_INTR_UE, &regs->intrdisable);
903d576bb9fSMichael Hanselmann 
904d576bb9fSMichael Hanselmann 			schedule_work (&ohci->nec_work);
905d576bb9fSMichael Hanselmann 		} else {
9061da177e4SLinus Torvalds 			ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
907b7463c71SAlan Stern 			ohci->rh_state = OHCI_RH_HALTED;
90869fff59dSAlan Stern 			usb_hc_died(hcd);
909d576bb9fSMichael Hanselmann 		}
9101da177e4SLinus Torvalds 
911256dbcd8SAlan Stern 		ohci_dump(ohci);
9121da177e4SLinus Torvalds 		ohci_usb_reset (ohci);
9131da177e4SLinus Torvalds 	}
9141da177e4SLinus Torvalds 
915583ceadaSAlan Stern 	if (ints & OHCI_INTR_RHSC) {
916d2c4254fSOliver Neukum 		ohci_dbg(ohci, "rhsc\n");
917583ceadaSAlan Stern 		ohci->next_statechange = jiffies + STATECHANGE_DELAY;
918583ceadaSAlan Stern 		ohci_writel(ohci, OHCI_INTR_RD | OHCI_INTR_RHSC,
919583ceadaSAlan Stern 				&regs->intrstatus);
920052ac01aSAlan Stern 
921052ac01aSAlan Stern 		/* NOTE: Vendors didn't always make the same implementation
922052ac01aSAlan Stern 		 * choices for RHSC.  Many followed the spec; RHSC triggers
923052ac01aSAlan Stern 		 * on an edge, like setting and maybe clearing a port status
924052ac01aSAlan Stern 		 * change bit.  With others it's level-triggered, active
92537ebb549SPetr Mladek 		 * until hub_wq clears all the port status change bits.  We'll
92637ebb549SPetr Mladek 		 * always disable it here and rely on polling until hub_wq
927052ac01aSAlan Stern 		 * re-enables it.
928052ac01aSAlan Stern 		 */
929052ac01aSAlan Stern 		ohci_writel(ohci, OHCI_INTR_RHSC, &regs->intrdisable);
930583ceadaSAlan Stern 		usb_hcd_poll_rh_status(hcd);
931583ceadaSAlan Stern 	}
932583ceadaSAlan Stern 
933583ceadaSAlan Stern 	/* For connect and disconnect events, we expect the controller
934583ceadaSAlan Stern 	 * to turn on RHSC along with RD.  But for remote wakeup events
935583ceadaSAlan Stern 	 * this might not happen.
936583ceadaSAlan Stern 	 */
937583ceadaSAlan Stern 	else if (ints & OHCI_INTR_RD) {
938d2c4254fSOliver Neukum 		ohci_dbg(ohci, "resume detect\n");
939e0fd3cbcSDavid Brownell 		ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus);
940541c7d43SAlan Stern 		set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
9418d1a243bSAlan Stern 		if (ohci->autostop) {
9428d1a243bSAlan Stern 			spin_lock (&ohci->lock);
9438d1a243bSAlan Stern 			ohci_rh_resume (ohci);
9448d1a243bSAlan Stern 			spin_unlock (&ohci->lock);
9458d1a243bSAlan Stern 		} else
946f197b2c5SDavid Brownell 			usb_hcd_resume_root_hub(hcd);
9471da177e4SLinus Torvalds 	}
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	spin_lock(&ohci->lock);
950c6fcb85eSAlan Stern 	if (ints & OHCI_INTR_WDH)
951c6fcb85eSAlan Stern 		update_done_list(ohci);
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	/* could track INTR_SO to reduce available PCI/... bandwidth */
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	/* handle any pending URB/ED unlinks, leaving INTR_SF enabled
9561da177e4SLinus Torvalds 	 * when there's still unlinking to be done (next frame).
9571da177e4SLinus Torvalds 	 */
958cdb4dd15SAlan Stern 	ohci_work(ohci);
95995d9a01dSAlan Stern 	if ((ints & OHCI_INTR_SF) != 0 && !ohci->ed_rm_list
960b7463c71SAlan Stern 			&& ohci->rh_state == OHCI_RH_RUNNING)
9611da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
9621da177e4SLinus Torvalds 
963b7463c71SAlan Stern 	if (ohci->rh_state == OHCI_RH_RUNNING) {
9641da177e4SLinus Torvalds 		ohci_writel (ohci, ints, &regs->intrstatus);
96581e38333SAlan Stern 		if (ints & OHCI_INTR_WDH)
96681e38333SAlan Stern 			++ohci->wdh_cnt;
96781e38333SAlan Stern 
9681da177e4SLinus Torvalds 		ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
9691da177e4SLinus Torvalds 		// flush those writes
9701da177e4SLinus Torvalds 		(void) ohci_readl (ohci, &ohci->regs->control);
9711da177e4SLinus Torvalds 	}
972c6fcb85eSAlan Stern 	spin_unlock(&ohci->lock);
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	return IRQ_HANDLED;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
9781da177e4SLinus Torvalds 
9791da177e4SLinus Torvalds static void ohci_stop (struct usb_hcd *hcd)
9801da177e4SLinus Torvalds {
9811da177e4SLinus Torvalds 	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
9821da177e4SLinus Torvalds 
983256dbcd8SAlan Stern 	ohci_dump(ohci);
9841da177e4SLinus Torvalds 
985569ff2deSTejun Heo 	if (quirk_nec(ohci))
98643829731STejun Heo 		flush_work(&ohci->nec_work);
98781e38333SAlan Stern 	del_timer_sync(&ohci->io_watchdog);
988b2685bdaSShigeru Yoshida 	ohci->prev_frame_no = IO_WATCHDOG_OFF;
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds 	ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
991435932f2Scaizhiyong 	ohci_usb_reset(ohci);
99271795c1dSPete Zaitcev 	free_irq(hcd->irq, hcd);
993cd70469dSFelipe Balbi 	hcd->irq = 0;
9941da177e4SLinus Torvalds 
995ab1666c1SLibin Yang 	if (quirk_amdiso(ohci))
996ad93562bSAndiry Xu 		usb_amd_dev_put();
99789a0fd18SMike Nuss 
9981da177e4SLinus Torvalds 	remove_debug_files (ohci);
9991da177e4SLinus Torvalds 	ohci_mem_cleanup (ohci);
10001da177e4SLinus Torvalds 	if (ohci->hcca) {
1001b0310c2fSLaurentiu Tudor 		if (hcd->localmem_pool)
1002b0310c2fSLaurentiu Tudor 			gen_pool_free(hcd->localmem_pool,
1003b0310c2fSLaurentiu Tudor 				      (unsigned long)ohci->hcca,
1004b0310c2fSLaurentiu Tudor 				      sizeof(*ohci->hcca));
1005b0310c2fSLaurentiu Tudor 		else
10061da177e4SLinus Torvalds 			dma_free_coherent(hcd->self.controller,
1007b0310c2fSLaurentiu Tudor 					  sizeof(*ohci->hcca),
10081da177e4SLinus Torvalds 					  ohci->hcca, ohci->hcca_dma);
10091da177e4SLinus Torvalds 		ohci->hcca = NULL;
10101da177e4SLinus Torvalds 		ohci->hcca_dma = 0;
10111da177e4SLinus Torvalds 	}
10121da177e4SLinus Torvalds }
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds /*-------------------------------------------------------------------------*/
10151da177e4SLinus Torvalds 
10162c93e790Syuan linyu #if defined(CONFIG_PM) || defined(CONFIG_USB_PCI)
1017da6fb570SDavid Brownell 
10181da177e4SLinus Torvalds /* must not be called from interrupt context */
101995e44d44SManjunath Goudar int ohci_restart(struct ohci_hcd *ohci)
10201da177e4SLinus Torvalds {
10211da177e4SLinus Torvalds 	int temp;
10221da177e4SLinus Torvalds 	int i;
10231da177e4SLinus Torvalds 	struct urb_priv *priv;
10241da177e4SLinus Torvalds 
102595e44d44SManjunath Goudar 	ohci_init(ohci);
10261da177e4SLinus Torvalds 	spin_lock_irq(&ohci->lock);
1027b7463c71SAlan Stern 	ohci->rh_state = OHCI_RH_HALTED;
1028d576bb9fSMichael Hanselmann 
1029d576bb9fSMichael Hanselmann 	/* Recycle any "live" eds/tds (and urbs). */
10301da177e4SLinus Torvalds 	if (!list_empty (&ohci->pending))
10311da177e4SLinus Torvalds 		ohci_dbg(ohci, "abort schedule...\n");
10321da177e4SLinus Torvalds 	list_for_each_entry (priv, &ohci->pending, pending) {
10331da177e4SLinus Torvalds 		struct urb	*urb = priv->td[0]->urb;
10341da177e4SLinus Torvalds 		struct ed	*ed = priv->ed;
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 		switch (ed->state) {
10371da177e4SLinus Torvalds 		case ED_OPER:
10381da177e4SLinus Torvalds 			ed->state = ED_UNLINK;
10391da177e4SLinus Torvalds 			ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
10401da177e4SLinus Torvalds 			ed_deschedule (ohci, ed);
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 			ed->ed_next = ohci->ed_rm_list;
10431da177e4SLinus Torvalds 			ed->ed_prev = NULL;
10441da177e4SLinus Torvalds 			ohci->ed_rm_list = ed;
10451da177e4SLinus Torvalds 			/* FALLTHROUGH */
10461da177e4SLinus Torvalds 		case ED_UNLINK:
10471da177e4SLinus Torvalds 			break;
10481da177e4SLinus Torvalds 		default:
10491da177e4SLinus Torvalds 			ohci_dbg(ohci, "bogus ed %p state %d\n",
10501da177e4SLinus Torvalds 					ed, ed->state);
10511da177e4SLinus Torvalds 		}
10521da177e4SLinus Torvalds 
105355d84968SAlan Stern 		if (!urb->unlinked)
105455d84968SAlan Stern 			urb->unlinked = -ESHUTDOWN;
10551da177e4SLinus Torvalds 	}
1056cdb4dd15SAlan Stern 	ohci_work(ohci);
10571da177e4SLinus Torvalds 	spin_unlock_irq(&ohci->lock);
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	/* paranoia, in case that didn't work: */
10601da177e4SLinus Torvalds 
10611da177e4SLinus Torvalds 	/* empty the interrupt branches */
10621da177e4SLinus Torvalds 	for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
10631da177e4SLinus Torvalds 	for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	/* no EDs to remove */
10661da177e4SLinus Torvalds 	ohci->ed_rm_list = NULL;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	/* empty control and bulk lists */
10691da177e4SLinus Torvalds 	ohci->ed_controltail = NULL;
10701da177e4SLinus Torvalds 	ohci->ed_bulktail    = NULL;
10711da177e4SLinus Torvalds 
10721da177e4SLinus Torvalds 	if ((temp = ohci_run (ohci)) < 0) {
10731da177e4SLinus Torvalds 		ohci_err (ohci, "can't restart, %d\n", temp);
10741da177e4SLinus Torvalds 		return temp;
10751da177e4SLinus Torvalds 	}
1076383975d7SAlan Stern 	ohci_dbg(ohci, "restart complete\n");
10771da177e4SLinus Torvalds 	return 0;
10781da177e4SLinus Torvalds }
107995e44d44SManjunath Goudar EXPORT_SYMBOL_GPL(ohci_restart);
1080d576bb9fSMichael Hanselmann 
1081da6fb570SDavid Brownell #endif
1082da6fb570SDavid Brownell 
1083cd1965dbSFlorian Fainelli #ifdef CONFIG_PM
1084cd1965dbSFlorian Fainelli 
108595e44d44SManjunath Goudar int ohci_suspend(struct usb_hcd *hcd, bool do_wakeup)
1086cd1965dbSFlorian Fainelli {
1087cd1965dbSFlorian Fainelli 	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
1088cd1965dbSFlorian Fainelli 	unsigned long	flags;
1089e1bffbf6SMajunath Goudar 	int		rc = 0;
1090cd1965dbSFlorian Fainelli 
1091d4ae47dcSFlorian Fainelli 	/* Disable irq emission and mark HW unaccessible. Use
1092cd1965dbSFlorian Fainelli 	 * the spinlock to properly synchronize with possible pending
1093cd1965dbSFlorian Fainelli 	 * RH suspend or resume activity.
1094cd1965dbSFlorian Fainelli 	 */
1095cd1965dbSFlorian Fainelli 	spin_lock_irqsave (&ohci->lock, flags);
1096cd1965dbSFlorian Fainelli 	ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
1097cd1965dbSFlorian Fainelli 	(void)ohci_readl(ohci, &ohci->regs->intrdisable);
1098cd1965dbSFlorian Fainelli 
1099cd1965dbSFlorian Fainelli 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1100cd1965dbSFlorian Fainelli 	spin_unlock_irqrestore (&ohci->lock, flags);
1101cd1965dbSFlorian Fainelli 
1102e1bffbf6SMajunath Goudar 	synchronize_irq(hcd->irq);
1103e1bffbf6SMajunath Goudar 
1104e1bffbf6SMajunath Goudar 	if (do_wakeup && HCD_WAKEUP_PENDING(hcd)) {
1105e1bffbf6SMajunath Goudar 		ohci_resume(hcd, false);
1106e1bffbf6SMajunath Goudar 		rc = -EBUSY;
1107e1bffbf6SMajunath Goudar 	}
1108e1bffbf6SMajunath Goudar 	return rc;
1109cd1965dbSFlorian Fainelli }
111095e44d44SManjunath Goudar EXPORT_SYMBOL_GPL(ohci_suspend);
1111cd1965dbSFlorian Fainelli 
1112cd1965dbSFlorian Fainelli 
111395e44d44SManjunath Goudar int ohci_resume(struct usb_hcd *hcd, bool hibernated)
1114cd1965dbSFlorian Fainelli {
1115cfa49b4bSFlorian Fainelli 	struct ohci_hcd		*ohci = hcd_to_ohci(hcd);
1116cfa49b4bSFlorian Fainelli 	int			port;
1117cfa49b4bSFlorian Fainelli 	bool			need_reinit = false;
1118cfa49b4bSFlorian Fainelli 
1119cd1965dbSFlorian Fainelli 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1120cd1965dbSFlorian Fainelli 
1121cd1965dbSFlorian Fainelli 	/* Make sure resume from hibernation re-enumerates everything */
1122cd1965dbSFlorian Fainelli 	if (hibernated)
1123cfa49b4bSFlorian Fainelli 		ohci_usb_reset(ohci);
1124cd1965dbSFlorian Fainelli 
1125cfa49b4bSFlorian Fainelli 	/* See if the controller is already running or has been reset */
1126cfa49b4bSFlorian Fainelli 	ohci->hc_control = ohci_readl(ohci, &ohci->regs->control);
1127cfa49b4bSFlorian Fainelli 	if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
1128cfa49b4bSFlorian Fainelli 		need_reinit = true;
1129cfa49b4bSFlorian Fainelli 	} else {
1130cfa49b4bSFlorian Fainelli 		switch (ohci->hc_control & OHCI_CTRL_HCFS) {
1131cfa49b4bSFlorian Fainelli 		case OHCI_USB_OPER:
1132cfa49b4bSFlorian Fainelli 		case OHCI_USB_RESET:
1133cfa49b4bSFlorian Fainelli 			need_reinit = true;
1134cfa49b4bSFlorian Fainelli 		}
1135cfa49b4bSFlorian Fainelli 	}
1136cfa49b4bSFlorian Fainelli 
1137cfa49b4bSFlorian Fainelli 	/* If needed, reinitialize and suspend the root hub */
1138cfa49b4bSFlorian Fainelli 	if (need_reinit) {
1139cfa49b4bSFlorian Fainelli 		spin_lock_irq(&ohci->lock);
1140cfa49b4bSFlorian Fainelli 		ohci_rh_resume(ohci);
1141cfa49b4bSFlorian Fainelli 		ohci_rh_suspend(ohci, 0);
1142cfa49b4bSFlorian Fainelli 		spin_unlock_irq(&ohci->lock);
1143cfa49b4bSFlorian Fainelli 	}
1144cfa49b4bSFlorian Fainelli 
1145cfa49b4bSFlorian Fainelli 	/* Normally just turn on port power and enable interrupts */
1146cfa49b4bSFlorian Fainelli 	else {
1147cfa49b4bSFlorian Fainelli 		ohci_dbg(ohci, "powerup ports\n");
1148cfa49b4bSFlorian Fainelli 		for (port = 0; port < ohci->num_ports; port++)
1149cfa49b4bSFlorian Fainelli 			ohci_writel(ohci, RH_PS_PPS,
1150cfa49b4bSFlorian Fainelli 					&ohci->regs->roothub.portstatus[port]);
1151cfa49b4bSFlorian Fainelli 
1152cfa49b4bSFlorian Fainelli 		ohci_writel(ohci, OHCI_INTR_MIE, &ohci->regs->intrenable);
1153cfa49b4bSFlorian Fainelli 		ohci_readl(ohci, &ohci->regs->intrenable);
1154cfa49b4bSFlorian Fainelli 		msleep(20);
1155cfa49b4bSFlorian Fainelli 	}
1156cfa49b4bSFlorian Fainelli 
1157cfa49b4bSFlorian Fainelli 	usb_hcd_resume_root_hub(hcd);
1158cfa49b4bSFlorian Fainelli 
1159cd1965dbSFlorian Fainelli 	return 0;
1160cd1965dbSFlorian Fainelli }
116195e44d44SManjunath Goudar EXPORT_SYMBOL_GPL(ohci_resume);
1162cd1965dbSFlorian Fainelli 
1163cd1965dbSFlorian Fainelli #endif
1164cd1965dbSFlorian Fainelli 
1165d576bb9fSMichael Hanselmann /*-------------------------------------------------------------------------*/
1166d576bb9fSMichael Hanselmann 
116795e44d44SManjunath Goudar /*
116895e44d44SManjunath Goudar  * Generic structure: This gets copied for platform drivers so that
116995e44d44SManjunath Goudar  * individual entries can be overridden as needed.
117095e44d44SManjunath Goudar  */
117195e44d44SManjunath Goudar 
117295e44d44SManjunath Goudar static const struct hc_driver ohci_hc_driver = {
117395e44d44SManjunath Goudar 	.description =          hcd_name,
117495e44d44SManjunath Goudar 	.product_desc =         "OHCI Host Controller",
117595e44d44SManjunath Goudar 	.hcd_priv_size =        sizeof(struct ohci_hcd),
117695e44d44SManjunath Goudar 
117795e44d44SManjunath Goudar 	/*
117895e44d44SManjunath Goudar 	 * generic hardware linkage
117995e44d44SManjunath Goudar 	*/
118095e44d44SManjunath Goudar 	.irq =                  ohci_irq,
1181*7b81cb6bSChristoph Hellwig 	.flags =                HCD_MEMORY | HCD_DMA | HCD_USB11,
118295e44d44SManjunath Goudar 
118395e44d44SManjunath Goudar 	/*
118495e44d44SManjunath Goudar 	* basic lifecycle operations
118595e44d44SManjunath Goudar 	*/
118695e44d44SManjunath Goudar 	.reset =                ohci_setup,
118795e44d44SManjunath Goudar 	.start =                ohci_start,
118895e44d44SManjunath Goudar 	.stop =                 ohci_stop,
118995e44d44SManjunath Goudar 	.shutdown =             ohci_shutdown,
119095e44d44SManjunath Goudar 
119195e44d44SManjunath Goudar 	/*
119295e44d44SManjunath Goudar 	 * managing i/o requests and associated device resources
119395e44d44SManjunath Goudar 	*/
119495e44d44SManjunath Goudar 	.urb_enqueue =          ohci_urb_enqueue,
119595e44d44SManjunath Goudar 	.urb_dequeue =          ohci_urb_dequeue,
119695e44d44SManjunath Goudar 	.endpoint_disable =     ohci_endpoint_disable,
119795e44d44SManjunath Goudar 
119895e44d44SManjunath Goudar 	/*
119995e44d44SManjunath Goudar 	* scheduling support
120095e44d44SManjunath Goudar 	*/
120195e44d44SManjunath Goudar 	.get_frame_number =     ohci_get_frame,
120295e44d44SManjunath Goudar 
120395e44d44SManjunath Goudar 	/*
120495e44d44SManjunath Goudar 	* root hub support
120595e44d44SManjunath Goudar 	*/
120695e44d44SManjunath Goudar 	.hub_status_data =      ohci_hub_status_data,
120795e44d44SManjunath Goudar 	.hub_control =          ohci_hub_control,
120895e44d44SManjunath Goudar #ifdef CONFIG_PM
120995e44d44SManjunath Goudar 	.bus_suspend =          ohci_bus_suspend,
121095e44d44SManjunath Goudar 	.bus_resume =           ohci_bus_resume,
121195e44d44SManjunath Goudar #endif
121295e44d44SManjunath Goudar 	.start_port_reset =	ohci_start_port_reset,
121395e44d44SManjunath Goudar };
121495e44d44SManjunath Goudar 
121595e44d44SManjunath Goudar void ohci_init_driver(struct hc_driver *drv,
121695e44d44SManjunath Goudar 		const struct ohci_driver_overrides *over)
121795e44d44SManjunath Goudar {
121895e44d44SManjunath Goudar 	/* Copy the generic table to drv and then apply the overrides */
121995e44d44SManjunath Goudar 	*drv = ohci_hc_driver;
122095e44d44SManjunath Goudar 
1221c80ad6d1SKevin Hilman 	if (over) {
122295e44d44SManjunath Goudar 		drv->product_desc = over->product_desc;
122395e44d44SManjunath Goudar 		drv->hcd_priv_size += over->extra_priv_size;
122495e44d44SManjunath Goudar 		if (over->reset)
122595e44d44SManjunath Goudar 			drv->reset = over->reset;
122695e44d44SManjunath Goudar 	}
1227c80ad6d1SKevin Hilman }
122895e44d44SManjunath Goudar EXPORT_SYMBOL_GPL(ohci_init_driver);
122995e44d44SManjunath Goudar 
123095e44d44SManjunath Goudar /*-------------------------------------------------------------------------*/
123195e44d44SManjunath Goudar 
12321da177e4SLinus Torvalds MODULE_AUTHOR (DRIVER_AUTHOR);
12332b70f073SAlan Stern MODULE_DESCRIPTION(DRIVER_DESC);
12341da177e4SLinus Torvalds MODULE_LICENSE ("GPL");
12351da177e4SLinus Torvalds 
12366381fad7SEric Miao #if defined(CONFIG_ARCH_SA1100) && defined(CONFIG_SA1111)
12371da177e4SLinus Torvalds #include "ohci-sa1111.c"
12385e16fabeSSylvain Munaut #define SA1111_DRIVER		ohci_hcd_sa1111_driver
12391da177e4SLinus Torvalds #endif
12401da177e4SLinus Torvalds 
1241495a678fSSylvain Munaut #ifdef CONFIG_USB_OHCI_HCD_PPC_OF
1242495a678fSSylvain Munaut #include "ohci-ppc-of.c"
1243495a678fSSylvain Munaut #define OF_PLATFORM_DRIVER	ohci_hcd_ppc_of_driver
1244495a678fSSylvain Munaut #endif
1245495a678fSSylvain Munaut 
12466a6c957eSGeoff Levand #ifdef CONFIG_PPC_PS3
12476a6c957eSGeoff Levand #include "ohci-ps3.c"
12487a4eb7fdSGeoff Levand #define PS3_SYSTEM_BUS_DRIVER	ps3_ohci_driver
12496a6c957eSGeoff Levand #endif
12506a6c957eSGeoff Levand 
1251f54aab6eSMagnus Damm #ifdef CONFIG_MFD_SM501
1252f54aab6eSMagnus Damm #include "ohci-sm501.c"
12533ee38d8bSBen Dooks #define SM501_OHCI_DRIVER	ohci_hcd_sm501_driver
1254f54aab6eSMagnus Damm #endif
1255f54aab6eSMagnus Damm 
125678c73414SDmitry Baryshkov #ifdef CONFIG_MFD_TC6393XB
125778c73414SDmitry Baryshkov #include "ohci-tmio.c"
125878c73414SDmitry Baryshkov #define TMIO_OHCI_DRIVER	ohci_hcd_tmio_driver
125978c73414SDmitry Baryshkov #endif
126078c73414SDmitry Baryshkov 
12615e16fabeSSylvain Munaut static int __init ohci_hcd_mod_init(void)
12625e16fabeSSylvain Munaut {
12635e16fabeSSylvain Munaut 	int retval = 0;
12645e16fabeSSylvain Munaut 
12655e16fabeSSylvain Munaut 	if (usb_disabled())
12665e16fabeSSylvain Munaut 		return -ENODEV;
12675e16fabeSSylvain Munaut 
12682b70f073SAlan Stern 	printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
12695b5e0928SAlexey Dobriyan 	pr_debug ("%s: block sizes: ed %zd td %zd\n", hcd_name,
12705e16fabeSSylvain Munaut 		sizeof (struct ed), sizeof (struct td));
12719beeee65SAlan Stern 	set_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
12725e16fabeSSylvain Munaut 
1273485f4f39SGreg Kroah-Hartman 	ohci_debug_root = debugfs_create_dir("ohci", usb_debug_root);
1274684c19e0STony Jones 
12756a6c957eSGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
12767a4eb7fdSGeoff Levand 	retval = ps3_ohci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
12776a6c957eSGeoff Levand 	if (retval < 0)
12786a6c957eSGeoff Levand 		goto error_ps3;
12796a6c957eSGeoff Levand #endif
12806a6c957eSGeoff Levand 
1281495a678fSSylvain Munaut #ifdef OF_PLATFORM_DRIVER
1282d35fb641SGrant Likely 	retval = platform_driver_register(&OF_PLATFORM_DRIVER);
1283495a678fSSylvain Munaut 	if (retval < 0)
1284de44743bSBenjamin Herrenschmidt 		goto error_of_platform;
1285495a678fSSylvain Munaut #endif
1286495a678fSSylvain Munaut 
12875e16fabeSSylvain Munaut #ifdef SA1111_DRIVER
12885e16fabeSSylvain Munaut 	retval = sa1111_driver_register(&SA1111_DRIVER);
12895e16fabeSSylvain Munaut 	if (retval < 0)
1290de44743bSBenjamin Herrenschmidt 		goto error_sa1111;
12915e16fabeSSylvain Munaut #endif
12925e16fabeSSylvain Munaut 
12933ee38d8bSBen Dooks #ifdef SM501_OHCI_DRIVER
12943ee38d8bSBen Dooks 	retval = platform_driver_register(&SM501_OHCI_DRIVER);
12953ee38d8bSBen Dooks 	if (retval < 0)
12963ee38d8bSBen Dooks 		goto error_sm501;
12973ee38d8bSBen Dooks #endif
12983ee38d8bSBen Dooks 
129978c73414SDmitry Baryshkov #ifdef TMIO_OHCI_DRIVER
130078c73414SDmitry Baryshkov 	retval = platform_driver_register(&TMIO_OHCI_DRIVER);
130178c73414SDmitry Baryshkov 	if (retval < 0)
130278c73414SDmitry Baryshkov 		goto error_tmio;
130378c73414SDmitry Baryshkov #endif
130478c73414SDmitry Baryshkov 
13055e16fabeSSylvain Munaut 	return retval;
13065e16fabeSSylvain Munaut 
13075e16fabeSSylvain Munaut 	/* Error path */
130878c73414SDmitry Baryshkov #ifdef TMIO_OHCI_DRIVER
130978c73414SDmitry Baryshkov 	platform_driver_unregister(&TMIO_OHCI_DRIVER);
131078c73414SDmitry Baryshkov  error_tmio:
131178c73414SDmitry Baryshkov #endif
13123ee38d8bSBen Dooks #ifdef SM501_OHCI_DRIVER
131378c73414SDmitry Baryshkov 	platform_driver_unregister(&SM501_OHCI_DRIVER);
13143ee38d8bSBen Dooks  error_sm501:
13153ee38d8bSBen Dooks #endif
13165e16fabeSSylvain Munaut #ifdef SA1111_DRIVER
13175e16fabeSSylvain Munaut 	sa1111_driver_unregister(&SA1111_DRIVER);
1318de44743bSBenjamin Herrenschmidt  error_sa1111:
1319de44743bSBenjamin Herrenschmidt #endif
1320de44743bSBenjamin Herrenschmidt #ifdef OF_PLATFORM_DRIVER
1321d35fb641SGrant Likely 	platform_driver_unregister(&OF_PLATFORM_DRIVER);
1322de44743bSBenjamin Herrenschmidt  error_of_platform:
1323de44743bSBenjamin Herrenschmidt #endif
13246a6c957eSGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
13257a4eb7fdSGeoff Levand 	ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
13266a6c957eSGeoff Levand  error_ps3:
13276a6c957eSGeoff Levand #endif
1328684c19e0STony Jones 	debugfs_remove(ohci_debug_root);
1329684c19e0STony Jones 	ohci_debug_root = NULL;
1330684c19e0STony Jones 
13319beeee65SAlan Stern 	clear_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
13325e16fabeSSylvain Munaut 	return retval;
13335e16fabeSSylvain Munaut }
13345e16fabeSSylvain Munaut module_init(ohci_hcd_mod_init);
13355e16fabeSSylvain Munaut 
13365e16fabeSSylvain Munaut static void __exit ohci_hcd_mod_exit(void)
13375e16fabeSSylvain Munaut {
133878c73414SDmitry Baryshkov #ifdef TMIO_OHCI_DRIVER
133978c73414SDmitry Baryshkov 	platform_driver_unregister(&TMIO_OHCI_DRIVER);
134078c73414SDmitry Baryshkov #endif
13413ee38d8bSBen Dooks #ifdef SM501_OHCI_DRIVER
13423ee38d8bSBen Dooks 	platform_driver_unregister(&SM501_OHCI_DRIVER);
13433ee38d8bSBen Dooks #endif
13445e16fabeSSylvain Munaut #ifdef SA1111_DRIVER
13455e16fabeSSylvain Munaut 	sa1111_driver_unregister(&SA1111_DRIVER);
13465e16fabeSSylvain Munaut #endif
1347495a678fSSylvain Munaut #ifdef OF_PLATFORM_DRIVER
1348d35fb641SGrant Likely 	platform_driver_unregister(&OF_PLATFORM_DRIVER);
1349495a678fSSylvain Munaut #endif
13506a6c957eSGeoff Levand #ifdef PS3_SYSTEM_BUS_DRIVER
13517a4eb7fdSGeoff Levand 	ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
13526a6c957eSGeoff Levand #endif
1353684c19e0STony Jones 	debugfs_remove(ohci_debug_root);
13549beeee65SAlan Stern 	clear_bit(USB_OHCI_LOADED, &usb_hcds_loaded);
13555e16fabeSSylvain Munaut }
13565e16fabeSSylvain Munaut module_exit(ohci_hcd_mod_exit);
13575e16fabeSSylvain Munaut 
1358