xref: /linux/drivers/usb/host/ehci-pci.c (revision 3a0bac0676d7f433c12389fc0bc574f048f921c3)
17ff71d6aSMatt Porter /*
27ff71d6aSMatt Porter  * EHCI HCD (Host Controller Driver) PCI Bus Glue.
37ff71d6aSMatt Porter  *
47ff71d6aSMatt Porter  * Copyright (c) 2000-2004 by David Brownell
57ff71d6aSMatt Porter  *
67ff71d6aSMatt Porter  * This program is free software; you can redistribute it and/or modify it
77ff71d6aSMatt Porter  * under the terms of the GNU General Public License as published by the
87ff71d6aSMatt Porter  * Free Software Foundation; either version 2 of the License, or (at your
97ff71d6aSMatt Porter  * option) any later version.
107ff71d6aSMatt Porter  *
117ff71d6aSMatt Porter  * This program is distributed in the hope that it will be useful, but
127ff71d6aSMatt Porter  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
137ff71d6aSMatt Porter  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
147ff71d6aSMatt Porter  * for more details.
157ff71d6aSMatt Porter  *
167ff71d6aSMatt Porter  * You should have received a copy of the GNU General Public License
177ff71d6aSMatt Porter  * along with this program; if not, write to the Free Software Foundation,
187ff71d6aSMatt Porter  * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
197ff71d6aSMatt Porter  */
207ff71d6aSMatt Porter 
217ff71d6aSMatt Porter #ifndef CONFIG_PCI
227ff71d6aSMatt Porter #error "This file is PCI bus glue.  CONFIG_PCI must be defined."
237ff71d6aSMatt Porter #endif
247ff71d6aSMatt Porter 
254f683843SDirk Brandewie /* defined here to avoid adding to pci_ids.h for single instance use */
264f683843SDirk Brandewie #define PCI_DEVICE_ID_INTEL_CE4100_USB	0x2e70
274f683843SDirk Brandewie 
287ff71d6aSMatt Porter /*-------------------------------------------------------------------------*/
297ff71d6aSMatt Porter 
3018807521SDavid Brownell /* called after powerup, by probe or system-pm "wakeup" */
3118807521SDavid Brownell static int ehci_pci_reinit(struct ehci_hcd *ehci, struct pci_dev *pdev)
3218807521SDavid Brownell {
3318807521SDavid Brownell 	int			retval;
3418807521SDavid Brownell 
35401feafaSDavid Brownell 	/* we expect static quirk code to handle the "extended capabilities"
36401feafaSDavid Brownell 	 * (currently just BIOS handoff) allowed starting with EHCI 0.96
37401feafaSDavid Brownell 	 */
3818807521SDavid Brownell 
3918807521SDavid Brownell 	/* PCI Memory-Write-Invalidate cycle support is optional (uncommon) */
4018807521SDavid Brownell 	retval = pci_set_mwi(pdev);
4118807521SDavid Brownell 	if (!retval)
4218807521SDavid Brownell 		ehci_dbg(ehci, "MWI active\n");
4318807521SDavid Brownell 
4418807521SDavid Brownell 	return 0;
4518807521SDavid Brownell }
4618807521SDavid Brownell 
478926bfa7SDavid Brownell /* called during probe() after chip reset completes */
488926bfa7SDavid Brownell static int ehci_pci_setup(struct usb_hcd *hcd)
497ff71d6aSMatt Porter {
507ff71d6aSMatt Porter 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
51abcc9448SDavid Brownell 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
52b09bc6cbSAndiry Xu 	struct pci_dev		*p_smbus;
53b09bc6cbSAndiry Xu 	u8			rev;
547ff71d6aSMatt Porter 	u32			temp;
5518807521SDavid Brownell 	int			retval;
567ff71d6aSMatt Porter 
57083522d7SBenjamin Herrenschmidt 	switch (pdev->vendor) {
58083522d7SBenjamin Herrenschmidt 	case PCI_VENDOR_ID_TOSHIBA_2:
59083522d7SBenjamin Herrenschmidt 		/* celleb's companion chip */
60083522d7SBenjamin Herrenschmidt 		if (pdev->device == 0x01b5) {
61083522d7SBenjamin Herrenschmidt #ifdef CONFIG_USB_EHCI_BIG_ENDIAN_MMIO
62083522d7SBenjamin Herrenschmidt 			ehci->big_endian_mmio = 1;
63083522d7SBenjamin Herrenschmidt #else
64083522d7SBenjamin Herrenschmidt 			ehci_warn(ehci,
65083522d7SBenjamin Herrenschmidt 				  "unsupported big endian Toshiba quirk\n");
66083522d7SBenjamin Herrenschmidt #endif
67083522d7SBenjamin Herrenschmidt 		}
68083522d7SBenjamin Herrenschmidt 		break;
69083522d7SBenjamin Herrenschmidt 	}
70083522d7SBenjamin Herrenschmidt 
717ff71d6aSMatt Porter 	ehci->caps = hcd->regs;
72083522d7SBenjamin Herrenschmidt 	ehci->regs = hcd->regs +
73c430131aSJan Andersson 		HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
74083522d7SBenjamin Herrenschmidt 
757ff71d6aSMatt Porter 	dbg_hcs_params(ehci, "reset");
767ff71d6aSMatt Porter 	dbg_hcc_params(ehci, "reset");
777ff71d6aSMatt Porter 
78c32ba30fSPaul Serice         /* ehci_init() causes memory for DMA transfers to be
79c32ba30fSPaul Serice          * allocated.  Thus, any vendor-specific workarounds based on
80c32ba30fSPaul Serice          * limiting the type of memory used for DMA transfers must
81c32ba30fSPaul Serice          * happen before ehci_init() is called. */
82c32ba30fSPaul Serice 	switch (pdev->vendor) {
83c32ba30fSPaul Serice 	case PCI_VENDOR_ID_NVIDIA:
84c32ba30fSPaul Serice 		/* NVidia reports that certain chips don't handle
85c32ba30fSPaul Serice 		 * QH, ITD, or SITD addresses above 2GB.  (But TD,
86c32ba30fSPaul Serice 		 * data buffer, and periodic schedule are normal.)
87c32ba30fSPaul Serice 		 */
88c32ba30fSPaul Serice 		switch (pdev->device) {
89c32ba30fSPaul Serice 		case 0x003c:	/* MCP04 */
90c32ba30fSPaul Serice 		case 0x005b:	/* CK804 */
91c32ba30fSPaul Serice 		case 0x00d8:	/* CK8 */
92c32ba30fSPaul Serice 		case 0x00e8:	/* CK8S */
93c32ba30fSPaul Serice 			if (pci_set_consistent_dma_mask(pdev,
94929a22a5SYang Hongyang 						DMA_BIT_MASK(31)) < 0)
95c32ba30fSPaul Serice 				ehci_warn(ehci, "can't enable NVidia "
96c32ba30fSPaul Serice 					"workaround for >2GB RAM\n");
97c32ba30fSPaul Serice 			break;
98c32ba30fSPaul Serice 		}
99c32ba30fSPaul Serice 		break;
100c32ba30fSPaul Serice 	}
101c32ba30fSPaul Serice 
1027ff71d6aSMatt Porter 	/* cache this readonly data; minimize chip reads */
103083522d7SBenjamin Herrenschmidt 	ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
1047ff71d6aSMatt Porter 
10518807521SDavid Brownell 	retval = ehci_halt(ehci);
10618807521SDavid Brownell 	if (retval)
10718807521SDavid Brownell 		return retval;
10818807521SDavid Brownell 
1093d091a6fSAndiry Xu 	if ((pdev->vendor == PCI_VENDOR_ID_AMD && pdev->device == 0x7808) ||
1103d091a6fSAndiry Xu 	    (pdev->vendor == PCI_VENDOR_ID_ATI && pdev->device == 0x4396)) {
1113d091a6fSAndiry Xu 		/* EHCI controller on AMD SB700/SB800/Hudson-2/3 platforms may
1123d091a6fSAndiry Xu 		 * read/write memory space which does not belong to it when
1133d091a6fSAndiry Xu 		 * there is NULL pointer with T-bit set to 1 in the frame list
1143d091a6fSAndiry Xu 		 * table. To avoid the issue, the frame list link pointer
1153d091a6fSAndiry Xu 		 * should always contain a valid pointer to a inactive qh.
1163d091a6fSAndiry Xu 		 */
1173d091a6fSAndiry Xu 		ehci->use_dummy_qh = 1;
1183d091a6fSAndiry Xu 		ehci_info(ehci, "applying AMD SB700/SB800/Hudson-2/3 EHCI "
1193d091a6fSAndiry Xu 				"dummy qh workaround\n");
1203d091a6fSAndiry Xu 	}
1213d091a6fSAndiry Xu 
1228926bfa7SDavid Brownell 	/* data structure init */
1238926bfa7SDavid Brownell 	retval = ehci_init(hcd);
1248926bfa7SDavid Brownell 	if (retval)
1258926bfa7SDavid Brownell 		return retval;
1268926bfa7SDavid Brownell 
1277ff71d6aSMatt Porter 	switch (pdev->vendor) {
1283681d8f3SDavid Miller 	case PCI_VENDOR_ID_NEC:
1293681d8f3SDavid Miller 		ehci->need_io_watchdog = 0;
1303681d8f3SDavid Miller 		break;
131403dbd36SAlek Du 	case PCI_VENDOR_ID_INTEL:
132403dbd36SAlek Du 		ehci->need_io_watchdog = 0;
133ae68a83bSAlan Stern 		ehci->fs_i_thresh = 1;
134ee4ecb8aSOliver Neukum 		if (pdev->device == 0x27cc) {
135ee4ecb8aSOliver Neukum 			ehci->broken_periodic = 1;
136ee4ecb8aSOliver Neukum 			ehci_info(ehci, "using broken periodic workaround\n");
137ee4ecb8aSOliver Neukum 		}
138fc928250SAlek Du 		if (pdev->device == 0x0806 || pdev->device == 0x0811
139fc928250SAlek Du 				|| pdev->device == 0x0829) {
140fc928250SAlek Du 			ehci_info(ehci, "disable lpm for langwell/penwell\n");
141fc928250SAlek Du 			ehci->has_lpm = 0;
142fc928250SAlek Du 		}
1434f683843SDirk Brandewie 		if (pdev->device == PCI_DEVICE_ID_INTEL_CE4100_USB) {
1444f683843SDirk Brandewie 			hcd->has_tt = 1;
1454f683843SDirk Brandewie 			tdi_reset(ehci);
1464f683843SDirk Brandewie 		}
147403dbd36SAlek Du 		break;
1487ff71d6aSMatt Porter 	case PCI_VENDOR_ID_TDI:
1497ff71d6aSMatt Porter 		if (pdev->device == PCI_DEVICE_ID_TDI_EHCI) {
1507329e211SAlan Stern 			hcd->has_tt = 1;
1517ff71d6aSMatt Porter 			tdi_reset(ehci);
1527ff71d6aSMatt Porter 		}
1537ff71d6aSMatt Porter 		break;
1547ff71d6aSMatt Porter 	case PCI_VENDOR_ID_AMD:
155ad93562bSAndiry Xu 		/* AMD PLL quirk */
156ad93562bSAndiry Xu 		if (usb_amd_find_chipset_info())
157ad93562bSAndiry Xu 			ehci->amd_pll_fix = 1;
1587ff71d6aSMatt Porter 		/* AMD8111 EHCI doesn't work, according to AMD errata */
1597ff71d6aSMatt Porter 		if (pdev->device == 0x7463) {
1607ff71d6aSMatt Porter 			ehci_info(ehci, "ignoring AMD8111 (errata)\n");
1618926bfa7SDavid Brownell 			retval = -EIO;
1628926bfa7SDavid Brownell 			goto done;
1637ff71d6aSMatt Porter 		}
1647ff71d6aSMatt Porter 		break;
1657ff71d6aSMatt Porter 	case PCI_VENDOR_ID_NVIDIA:
166f8aeb3bbSDavid Brownell 		switch (pdev->device) {
167f8aeb3bbSDavid Brownell 		/* Some NForce2 chips have problems with selective suspend;
168f8aeb3bbSDavid Brownell 		 * fixed in newer silicon.
169f8aeb3bbSDavid Brownell 		 */
170f8aeb3bbSDavid Brownell 		case 0x0068:
17144c10138SAuke Kok 			if (pdev->revision < 0xa4)
172f8aeb3bbSDavid Brownell 				ehci->no_selective_suspend = 1;
173f8aeb3bbSDavid Brownell 			break;
174a85b4e7fSBrian J. Tarricone 
175a85b4e7fSBrian J. Tarricone 		/* MCP89 chips on the MacBookAir3,1 give EPROTO when
176a85b4e7fSBrian J. Tarricone 		 * fetching device descriptors unless LPM is disabled.
177a85b4e7fSBrian J. Tarricone 		 * There are also intermittent problems enumerating
178a85b4e7fSBrian J. Tarricone 		 * devices with PPCD enabled.
179a85b4e7fSBrian J. Tarricone 		 */
180a85b4e7fSBrian J. Tarricone 		case 0x0d9d:
181a85b4e7fSBrian J. Tarricone 			ehci_info(ehci, "disable lpm/ppcd for nvidia mcp89");
182a85b4e7fSBrian J. Tarricone 			ehci->has_lpm = 0;
183a85b4e7fSBrian J. Tarricone 			ehci->has_ppcd = 0;
184a85b4e7fSBrian J. Tarricone 			ehci->command &= ~CMD_PPCEE;
185a85b4e7fSBrian J. Tarricone 			break;
1867ff71d6aSMatt Porter 		}
1877ff71d6aSMatt Porter 		break;
188055b93c9SRene Herman 	case PCI_VENDOR_ID_VIA:
189055b93c9SRene Herman 		if (pdev->device == 0x3104 && (pdev->revision & 0xf0) == 0x60) {
190055b93c9SRene Herman 			u8 tmp;
191055b93c9SRene Herman 
192055b93c9SRene Herman 			/* The VT6212 defaults to a 1 usec EHCI sleep time which
193055b93c9SRene Herman 			 * hogs the PCI bus *badly*. Setting bit 5 of 0x4B makes
194055b93c9SRene Herman 			 * that sleep time use the conventional 10 usec.
195055b93c9SRene Herman 			 */
196055b93c9SRene Herman 			pci_read_config_byte(pdev, 0x4b, &tmp);
197055b93c9SRene Herman 			if (tmp & 0x20)
198055b93c9SRene Herman 				break;
199055b93c9SRene Herman 			pci_write_config_byte(pdev, 0x4b, tmp | 0x20);
200055b93c9SRene Herman 		}
201055b93c9SRene Herman 		break;
202b09bc6cbSAndiry Xu 	case PCI_VENDOR_ID_ATI:
203ad93562bSAndiry Xu 		/* AMD PLL quirk */
204ad93562bSAndiry Xu 		if (usb_amd_find_chipset_info())
205ad93562bSAndiry Xu 			ehci->amd_pll_fix = 1;
2060a99e8acSShane Huang 		/* SB600 and old version of SB700 have a bug in EHCI controller,
207b09bc6cbSAndiry Xu 		 * which causes usb devices lose response in some cases.
208b09bc6cbSAndiry Xu 		 */
2090a99e8acSShane Huang 		if ((pdev->device == 0x4386) || (pdev->device == 0x4396)) {
210b09bc6cbSAndiry Xu 			p_smbus = pci_get_device(PCI_VENDOR_ID_ATI,
211b09bc6cbSAndiry Xu 						 PCI_DEVICE_ID_ATI_SBX00_SMBUS,
212b09bc6cbSAndiry Xu 						 NULL);
213b09bc6cbSAndiry Xu 			if (!p_smbus)
214b09bc6cbSAndiry Xu 				break;
215b09bc6cbSAndiry Xu 			rev = p_smbus->revision;
2160a99e8acSShane Huang 			if ((pdev->device == 0x4386) || (rev == 0x3a)
2170a99e8acSShane Huang 			    || (rev == 0x3b)) {
218b09bc6cbSAndiry Xu 				u8 tmp;
2190a99e8acSShane Huang 				ehci_info(ehci, "applying AMD SB600/SB700 USB "
2200a99e8acSShane Huang 					"freeze workaround\n");
221b09bc6cbSAndiry Xu 				pci_read_config_byte(pdev, 0x53, &tmp);
222b09bc6cbSAndiry Xu 				pci_write_config_byte(pdev, 0x53, tmp | (1<<3));
223b09bc6cbSAndiry Xu 			}
224b09bc6cbSAndiry Xu 			pci_dev_put(p_smbus);
225b09bc6cbSAndiry Xu 		}
226b09bc6cbSAndiry Xu 		break;
22768aa95d5SAlan Stern 	case PCI_VENDOR_ID_NETMOS:
22868aa95d5SAlan Stern 		/* MosChip frame-index-register bug */
22968aa95d5SAlan Stern 		ehci_info(ehci, "applying MosChip frame-index workaround\n");
23068aa95d5SAlan Stern 		ehci->frame_index_bug = 1;
23168aa95d5SAlan Stern 		break;
2327ff71d6aSMatt Porter 	}
2337ff71d6aSMatt Porter 
2348d053c79SJason Wessel 	/* optional debug port, normally in the first BAR */
2358d053c79SJason Wessel 	temp = pci_find_capability(pdev, 0x0a);
2368d053c79SJason Wessel 	if (temp) {
2378d053c79SJason Wessel 		pci_read_config_dword(pdev, temp, &temp);
2388d053c79SJason Wessel 		temp >>= 16;
2398d053c79SJason Wessel 		if ((temp & (3 << 13)) == (1 << 13)) {
2408d053c79SJason Wessel 			temp &= 0x1fff;
2418d053c79SJason Wessel 			ehci->debug = ehci_to_hcd(ehci)->regs + temp;
2428d053c79SJason Wessel 			temp = ehci_readl(ehci, &ehci->debug->control);
2438d053c79SJason Wessel 			ehci_info(ehci, "debug port %d%s\n",
2448d053c79SJason Wessel 				HCS_DEBUG_PORT(ehci->hcs_params),
2458d053c79SJason Wessel 				(temp & DBGP_ENABLED)
2468d053c79SJason Wessel 					? " IN USE"
2478d053c79SJason Wessel 					: "");
2488d053c79SJason Wessel 			if (!(temp & DBGP_ENABLED))
2498d053c79SJason Wessel 				ehci->debug = NULL;
2508d053c79SJason Wessel 		}
2518d053c79SJason Wessel 	}
2528d053c79SJason Wessel 
2537ff71d6aSMatt Porter 	ehci_reset(ehci);
2547ff71d6aSMatt Porter 
2557ff71d6aSMatt Porter 	/* at least the Genesys GL880S needs fixup here */
2567ff71d6aSMatt Porter 	temp = HCS_N_CC(ehci->hcs_params) * HCS_N_PCC(ehci->hcs_params);
2577ff71d6aSMatt Porter 	temp &= 0x0f;
2587ff71d6aSMatt Porter 	if (temp && HCS_N_PORTS(ehci->hcs_params) > temp) {
2597ff71d6aSMatt Porter 		ehci_dbg(ehci, "bogus port configuration: "
2607ff71d6aSMatt Porter 			"cc=%d x pcc=%d < ports=%d\n",
2617ff71d6aSMatt Porter 			HCS_N_CC(ehci->hcs_params),
2627ff71d6aSMatt Porter 			HCS_N_PCC(ehci->hcs_params),
2637ff71d6aSMatt Porter 			HCS_N_PORTS(ehci->hcs_params));
2647ff71d6aSMatt Porter 
2657ff71d6aSMatt Porter 		switch (pdev->vendor) {
2667ff71d6aSMatt Porter 		case 0x17a0:		/* GENESYS */
2677ff71d6aSMatt Porter 			/* GL880S: should be PORTS=2 */
2687ff71d6aSMatt Porter 			temp |= (ehci->hcs_params & ~0xf);
2697ff71d6aSMatt Porter 			ehci->hcs_params = temp;
2707ff71d6aSMatt Porter 			break;
2717ff71d6aSMatt Porter 		case PCI_VENDOR_ID_NVIDIA:
2727ff71d6aSMatt Porter 			/* NF4: should be PCC=10 */
2737ff71d6aSMatt Porter 			break;
2747ff71d6aSMatt Porter 		}
2757ff71d6aSMatt Porter 	}
2767ff71d6aSMatt Porter 
2777ff71d6aSMatt Porter 	/* Serial Bus Release Number is at PCI 0x60 offset */
2787ff71d6aSMatt Porter 	pci_read_config_byte(pdev, 0x60, &ehci->sbrn);
279*3a0bac06SAlessandro Rubini 	if (pdev->vendor == PCI_VENDOR_ID_STMICRO
280*3a0bac06SAlessandro Rubini 	    && pdev->device == PCI_DEVICE_ID_STMICRO_USB_HOST)
281*3a0bac06SAlessandro Rubini 		ehci->sbrn = 0x20; /* ConneXT has no sbrn register */
2827ff71d6aSMatt Porter 
2836fd9086aSAlan Stern 	/* Keep this around for a while just in case some EHCI
2846fd9086aSAlan Stern 	 * implementation uses legacy PCI PM support.  This test
2856fd9086aSAlan Stern 	 * can be removed on 17 Dec 2009 if the dev_warn() hasn't
2866fd9086aSAlan Stern 	 * been triggered by then.
2872c1c3c4cSDavid Brownell 	 */
2882c1c3c4cSDavid Brownell 	if (!device_can_wakeup(&pdev->dev)) {
2892c1c3c4cSDavid Brownell 		u16	port_wake;
2902c1c3c4cSDavid Brownell 
2912c1c3c4cSDavid Brownell 		pci_read_config_word(pdev, 0x62, &port_wake);
2926fd9086aSAlan Stern 		if (port_wake & 0x0001) {
2936fd9086aSAlan Stern 			dev_warn(&pdev->dev, "Enabling legacy PCI PM\n");
294bcca06efSAlan Stern 			device_set_wakeup_capable(&pdev->dev, 1);
2952c1c3c4cSDavid Brownell 		}
2966fd9086aSAlan Stern 	}
2977ff71d6aSMatt Porter 
298f8aeb3bbSDavid Brownell #ifdef	CONFIG_USB_SUSPEND
299f8aeb3bbSDavid Brownell 	/* REVISIT: the controller works fine for wakeup iff the root hub
300f8aeb3bbSDavid Brownell 	 * itself is "globally" suspended, but usbcore currently doesn't
301f8aeb3bbSDavid Brownell 	 * understand such things.
302f8aeb3bbSDavid Brownell 	 *
303f8aeb3bbSDavid Brownell 	 * System suspend currently expects to be able to suspend the entire
304f8aeb3bbSDavid Brownell 	 * device tree, device-at-a-time.  If we failed selective suspend
305f8aeb3bbSDavid Brownell 	 * reports, system suspend would fail; so the root hub code must claim
306411c9403SAnand Gadiyar 	 * success.  That's lying to usbcore, and it matters for runtime
307f8aeb3bbSDavid Brownell 	 * PM scenarios with selective suspend and remote wakeup...
308f8aeb3bbSDavid Brownell 	 */
309f8aeb3bbSDavid Brownell 	if (ehci->no_selective_suspend && device_can_wakeup(&pdev->dev))
310f8aeb3bbSDavid Brownell 		ehci_warn(ehci, "selective suspend/wakeup unavailable\n");
311f8aeb3bbSDavid Brownell #endif
312f8aeb3bbSDavid Brownell 
313aff6d18fSAlan Stern 	ehci_port_power(ehci, 1);
31418807521SDavid Brownell 	retval = ehci_pci_reinit(ehci, pdev);
3158926bfa7SDavid Brownell done:
3168926bfa7SDavid Brownell 	return retval;
3177ff71d6aSMatt Porter }
3187ff71d6aSMatt Porter 
3197ff71d6aSMatt Porter /*-------------------------------------------------------------------------*/
3207ff71d6aSMatt Porter 
3217ff71d6aSMatt Porter #ifdef	CONFIG_PM
3227ff71d6aSMatt Porter 
3237ff71d6aSMatt Porter /* suspend/resume, section 4.3 */
3247ff71d6aSMatt Porter 
325f03c17fcSDavid Brownell /* These routines rely on the PCI bus glue
3267ff71d6aSMatt Porter  * to handle powerdown and wakeup, and currently also on
3277ff71d6aSMatt Porter  * transceivers that don't need any software attention to set up
3287ff71d6aSMatt Porter  * the right sort of wakeup.
329f03c17fcSDavid Brownell  * Also they depend on separate root hub suspend/resume.
3307ff71d6aSMatt Porter  */
3317ff71d6aSMatt Porter 
3324147200dSAlan Stern static int ehci_pci_suspend(struct usb_hcd *hcd, bool do_wakeup)
3337ff71d6aSMatt Porter {
3347ff71d6aSMatt Porter 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
3358de98402SBenjamin Herrenschmidt 	unsigned long		flags;
3368de98402SBenjamin Herrenschmidt 	int			rc = 0;
3377ff71d6aSMatt Porter 
3387ff71d6aSMatt Porter 	if (time_before(jiffies, ehci->next_statechange))
339f03c17fcSDavid Brownell 		msleep(10);
3407ff71d6aSMatt Porter 
3418de98402SBenjamin Herrenschmidt 	/* Root hub was already suspended. Disable irq emission and
34216032c4fSAlan Stern 	 * mark HW unaccessible.  The PM and USB cores make sure that
34316032c4fSAlan Stern 	 * the root hub is either suspended or stopped.
3448de98402SBenjamin Herrenschmidt 	 */
3454147200dSAlan Stern 	ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup);
346148fc55fSYin Kangkai 	spin_lock_irqsave (&ehci->lock, flags);
347083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
348083522d7SBenjamin Herrenschmidt 	(void)ehci_readl(ehci, &ehci->regs->intr_enable);
3498de98402SBenjamin Herrenschmidt 
3508de98402SBenjamin Herrenschmidt 	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
3518de98402SBenjamin Herrenschmidt 	spin_unlock_irqrestore (&ehci->lock, flags);
3528de98402SBenjamin Herrenschmidt 
353f03c17fcSDavid Brownell 	// could save FLADJ in case of Vaux power loss
3547ff71d6aSMatt Porter 	// ... we'd only use it to handle clock skew
3557ff71d6aSMatt Porter 
3568de98402SBenjamin Herrenschmidt 	return rc;
3577ff71d6aSMatt Porter }
3587ff71d6aSMatt Porter 
35969e848c2SSarah Sharp static bool usb_is_intel_switchable_ehci(struct pci_dev *pdev)
36069e848c2SSarah Sharp {
36169e848c2SSarah Sharp 	return pdev->class == PCI_CLASS_SERIAL_USB_EHCI &&
36269e848c2SSarah Sharp 		pdev->vendor == PCI_VENDOR_ID_INTEL &&
36369e848c2SSarah Sharp 		pdev->device == 0x1E26;
36469e848c2SSarah Sharp }
36569e848c2SSarah Sharp 
36669e848c2SSarah Sharp static void ehci_enable_xhci_companion(void)
36769e848c2SSarah Sharp {
36869e848c2SSarah Sharp 	struct pci_dev		*companion = NULL;
36969e848c2SSarah Sharp 
37069e848c2SSarah Sharp 	/* The xHCI and EHCI controllers are not on the same PCI slot */
37169e848c2SSarah Sharp 	for_each_pci_dev(companion) {
37269e848c2SSarah Sharp 		if (!usb_is_intel_switchable_xhci(companion))
37369e848c2SSarah Sharp 			continue;
37469e848c2SSarah Sharp 		usb_enable_xhci_ports(companion);
37569e848c2SSarah Sharp 		return;
37669e848c2SSarah Sharp 	}
37769e848c2SSarah Sharp }
37869e848c2SSarah Sharp 
3796ec4beb5SAlan Stern static int ehci_pci_resume(struct usb_hcd *hcd, bool hibernated)
3807ff71d6aSMatt Porter {
3817ff71d6aSMatt Porter 	struct ehci_hcd		*ehci = hcd_to_ehci(hcd);
38218807521SDavid Brownell 	struct pci_dev		*pdev = to_pci_dev(hcd->self.controller);
3837ff71d6aSMatt Porter 
38469e848c2SSarah Sharp 	/* The BIOS on systems with the Intel Panther Point chipset may or may
38569e848c2SSarah Sharp 	 * not support xHCI natively.  That means that during system resume, it
38669e848c2SSarah Sharp 	 * may switch the ports back to EHCI so that users can use their
38769e848c2SSarah Sharp 	 * keyboard to select a kernel from GRUB after resume from hibernate.
38869e848c2SSarah Sharp 	 *
38969e848c2SSarah Sharp 	 * The BIOS is supposed to remember whether the OS had xHCI ports
39069e848c2SSarah Sharp 	 * enabled before resume, and switch the ports back to xHCI when the
39169e848c2SSarah Sharp 	 * BIOS/OS semaphore is written, but we all know we can't trust BIOS
39269e848c2SSarah Sharp 	 * writers.
39369e848c2SSarah Sharp 	 *
39469e848c2SSarah Sharp 	 * Unconditionally switch the ports back to xHCI after a system resume.
39569e848c2SSarah Sharp 	 * We can't tell whether the EHCI or xHCI controller will be resumed
39669e848c2SSarah Sharp 	 * first, so we have to do the port switchover in both drivers.  Writing
39769e848c2SSarah Sharp 	 * a '1' to the port switchover registers should have no effect if the
39869e848c2SSarah Sharp 	 * port was already switched over.
39969e848c2SSarah Sharp 	 */
40069e848c2SSarah Sharp 	if (usb_is_intel_switchable_ehci(pdev))
40169e848c2SSarah Sharp 		ehci_enable_xhci_companion();
40269e848c2SSarah Sharp 
403f03c17fcSDavid Brownell 	// maybe restore FLADJ
4047ff71d6aSMatt Porter 
4057ff71d6aSMatt Porter 	if (time_before(jiffies, ehci->next_statechange))
4067ff71d6aSMatt Porter 		msleep(100);
4077ff71d6aSMatt Porter 
4088de98402SBenjamin Herrenschmidt 	/* Mark hardware accessible again as we are out of D3 state by now */
4098de98402SBenjamin Herrenschmidt 	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4108de98402SBenjamin Herrenschmidt 
4116ec4beb5SAlan Stern 	/* If CF is still set and we aren't resuming from hibernation
4126ec4beb5SAlan Stern 	 * then we maintained PCI Vaux power.
4138c03356aSAlan Stern 	 * Just undo the effect of ehci_pci_suspend().
4147ff71d6aSMatt Porter 	 */
4156ec4beb5SAlan Stern 	if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
4166ec4beb5SAlan Stern 				!hibernated) {
4178c03356aSAlan Stern 		int	mask = INTR_MASK;
4188c03356aSAlan Stern 
41916032c4fSAlan Stern 		ehci_prepare_ports_for_controller_resume(ehci);
42058a97ffeSAlan Stern 		if (!hcd->self.root_hub->do_remote_wakeup)
4218c03356aSAlan Stern 			mask &= ~STS_PCD;
422083522d7SBenjamin Herrenschmidt 		ehci_writel(ehci, mask, &ehci->regs->intr_enable);
423083522d7SBenjamin Herrenschmidt 		ehci_readl(ehci, &ehci->regs->intr_enable);
424f03c17fcSDavid Brownell 		return 0;
4257ff71d6aSMatt Porter 	}
426f03c17fcSDavid Brownell 
4271c50c317SAlan Stern 	usb_root_hub_lost_power(hcd->self.root_hub);
4287ff71d6aSMatt Porter 
4297ff71d6aSMatt Porter 	/* Else reset, to cope with power loss or flush-to-storage
430f03c17fcSDavid Brownell 	 * style "resume" having let BIOS kick in during reboot.
4317ff71d6aSMatt Porter 	 */
4327ff71d6aSMatt Porter 	(void) ehci_halt(ehci);
4337ff71d6aSMatt Porter 	(void) ehci_reset(ehci);
43418807521SDavid Brownell 	(void) ehci_pci_reinit(ehci, pdev);
4357ff71d6aSMatt Porter 
4367ff71d6aSMatt Porter 	/* emptying the schedule aborts any urbs */
4377ff71d6aSMatt Porter 	spin_lock_irq(&ehci->lock);
4387ff71d6aSMatt Porter 	if (ehci->reclaim)
43907d29b63SAlan Stern 		end_unlink_async(ehci);
4407d12e780SDavid Howells 	ehci_work(ehci);
4417ff71d6aSMatt Porter 	spin_unlock_irq(&ehci->lock);
4427ff71d6aSMatt Porter 
443083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, ehci->command, &ehci->regs->command);
444083522d7SBenjamin Herrenschmidt 	ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
445083522d7SBenjamin Herrenschmidt 	ehci_readl(ehci, &ehci->regs->command);	/* unblock posted writes */
4468c03356aSAlan Stern 
447383975d7SAlan Stern 	/* here we "know" root ports should always stay powered */
448383975d7SAlan Stern 	ehci_port_power(ehci, 1);
449383975d7SAlan Stern 
450e8799906SAlan Stern 	ehci->rh_state = EHCI_RH_SUSPENDED;
4518c03356aSAlan Stern 	return 0;
4527ff71d6aSMatt Porter }
4537ff71d6aSMatt Porter #endif
4547ff71d6aSMatt Porter 
45548f24970SAlek Du static int ehci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
45648f24970SAlek Du {
45748f24970SAlek Du 	struct ehci_hcd *ehci = hcd_to_ehci(hcd);
45848f24970SAlek Du 	int rc = 0;
45948f24970SAlek Du 
46048f24970SAlek Du 	if (!udev->parent) /* udev is root hub itself, impossible */
46148f24970SAlek Du 		rc = -1;
46248f24970SAlek Du 	/* we only support lpm device connected to root hub yet */
46348f24970SAlek Du 	if (ehci->has_lpm && !udev->parent->parent) {
46448f24970SAlek Du 		rc = ehci_lpm_set_da(ehci, udev->devnum, udev->portnum);
46548f24970SAlek Du 		if (!rc)
46648f24970SAlek Du 			rc = ehci_lpm_check(ehci, udev->portnum);
46748f24970SAlek Du 	}
46848f24970SAlek Du 	return rc;
46948f24970SAlek Du }
47048f24970SAlek Du 
4717ff71d6aSMatt Porter static const struct hc_driver ehci_pci_hc_driver = {
4727ff71d6aSMatt Porter 	.description =		hcd_name,
4737ff71d6aSMatt Porter 	.product_desc =		"EHCI Host Controller",
4747ff71d6aSMatt Porter 	.hcd_priv_size =	sizeof(struct ehci_hcd),
4757ff71d6aSMatt Porter 
4767ff71d6aSMatt Porter 	/*
4777ff71d6aSMatt Porter 	 * generic hardware linkage
4787ff71d6aSMatt Porter 	 */
4797ff71d6aSMatt Porter 	.irq =			ehci_irq,
4807ff71d6aSMatt Porter 	.flags =		HCD_MEMORY | HCD_USB2,
4817ff71d6aSMatt Porter 
4827ff71d6aSMatt Porter 	/*
4837ff71d6aSMatt Porter 	 * basic lifecycle operations
4847ff71d6aSMatt Porter 	 */
4858926bfa7SDavid Brownell 	.reset =		ehci_pci_setup,
48618807521SDavid Brownell 	.start =		ehci_run,
4877ff71d6aSMatt Porter #ifdef	CONFIG_PM
4887be7d741SAlan Stern 	.pci_suspend =		ehci_pci_suspend,
4897be7d741SAlan Stern 	.pci_resume =		ehci_pci_resume,
4907ff71d6aSMatt Porter #endif
49118807521SDavid Brownell 	.stop =			ehci_stop,
49264a21d02SAleksey Gorelov 	.shutdown =		ehci_shutdown,
4937ff71d6aSMatt Porter 
4947ff71d6aSMatt Porter 	/*
4957ff71d6aSMatt Porter 	 * managing i/o requests and associated device resources
4967ff71d6aSMatt Porter 	 */
4977ff71d6aSMatt Porter 	.urb_enqueue =		ehci_urb_enqueue,
4987ff71d6aSMatt Porter 	.urb_dequeue =		ehci_urb_dequeue,
4997ff71d6aSMatt Porter 	.endpoint_disable =	ehci_endpoint_disable,
500b18ffd49SAlan Stern 	.endpoint_reset =	ehci_endpoint_reset,
5017ff71d6aSMatt Porter 
5027ff71d6aSMatt Porter 	/*
5037ff71d6aSMatt Porter 	 * scheduling support
5047ff71d6aSMatt Porter 	 */
5057ff71d6aSMatt Porter 	.get_frame_number =	ehci_get_frame,
5067ff71d6aSMatt Porter 
5077ff71d6aSMatt Porter 	/*
5087ff71d6aSMatt Porter 	 * root hub support
5097ff71d6aSMatt Porter 	 */
5107ff71d6aSMatt Porter 	.hub_status_data =	ehci_hub_status_data,
5117ff71d6aSMatt Porter 	.hub_control =		ehci_hub_control,
5120c0382e3SAlan Stern 	.bus_suspend =		ehci_bus_suspend,
5130c0382e3SAlan Stern 	.bus_resume =		ehci_bus_resume,
51490da096eSBalaji Rao 	.relinquish_port =	ehci_relinquish_port,
5153a31155cSAlan Stern 	.port_handed_over =	ehci_port_handed_over,
516914b7012SAlan Stern 
51748f24970SAlek Du 	/*
51848f24970SAlek Du 	 * call back when device connected and addressed
51948f24970SAlek Du 	 */
52048f24970SAlek Du 	.update_device =	ehci_update_device,
52148f24970SAlek Du 
522914b7012SAlan Stern 	.clear_tt_buffer_complete	= ehci_clear_tt_buffer_complete,
5237ff71d6aSMatt Porter };
5247ff71d6aSMatt Porter 
5257ff71d6aSMatt Porter /*-------------------------------------------------------------------------*/
5267ff71d6aSMatt Porter 
5277ff71d6aSMatt Porter /* PCI driver selection metadata; PCI hotplugging uses this */
5287ff71d6aSMatt Porter static const struct pci_device_id pci_ids [] = { {
5297ff71d6aSMatt Porter 	/* handle any USB 2.0 EHCI controller */
530c67808eeSJean Delvare 	PCI_DEVICE_CLASS(PCI_CLASS_SERIAL_USB_EHCI, ~0),
5317ff71d6aSMatt Porter 	.driver_data =	(unsigned long) &ehci_pci_hc_driver,
532*3a0bac06SAlessandro Rubini 	}, {
533*3a0bac06SAlessandro Rubini 	PCI_VDEVICE(STMICRO, PCI_DEVICE_ID_STMICRO_USB_HOST),
534*3a0bac06SAlessandro Rubini 	.driver_data = (unsigned long) &ehci_pci_hc_driver,
5357ff71d6aSMatt Porter 	},
5367ff71d6aSMatt Porter 	{ /* end: all zeroes */ }
5377ff71d6aSMatt Porter };
5387ff71d6aSMatt Porter MODULE_DEVICE_TABLE(pci, pci_ids);
5397ff71d6aSMatt Porter 
5407ff71d6aSMatt Porter /* pci driver glue; this is a "new style" PCI driver module */
5417ff71d6aSMatt Porter static struct pci_driver ehci_pci_driver = {
5427ff71d6aSMatt Porter 	.name =		(char *) hcd_name,
5437ff71d6aSMatt Porter 	.id_table =	pci_ids,
5447ff71d6aSMatt Porter 
5457ff71d6aSMatt Porter 	.probe =	usb_hcd_pci_probe,
5467ff71d6aSMatt Porter 	.remove =	usb_hcd_pci_remove,
54764a21d02SAleksey Gorelov 	.shutdown = 	usb_hcd_pci_shutdown,
548abb30641SAlan Stern 
549abb30641SAlan Stern #ifdef CONFIG_PM_SLEEP
550abb30641SAlan Stern 	.driver =	{
551abb30641SAlan Stern 		.pm =	&usb_hcd_pci_pm_ops
552abb30641SAlan Stern 	},
553abb30641SAlan Stern #endif
5547ff71d6aSMatt Porter };
555