2 * (C) Copyright David Brownell 2000-2002
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/usb.h>
27 #ifdef CONFIG_PPC_PMAC
28 #include <asm/machdep.h>
29 #include <asm/pmac_feature.h>
30 #include <asm/pci-bridge.h>
38 /* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
41 /*-------------------------------------------------------------------------*/
43 /* configure so an HC device and id are always provided */
44 /* always called with process context; sleeping is OK */
47 * usb_hcd_pci_probe - initialize PCI-based HCDs
48 * @dev: USB Host Controller being probed
49 * @id: pci hotplug id connecting controller to HCD framework
50 * Context: !in_interrupt()
52 * Allocates basic PCI resources for this USB host controller, and
53 * then invokes the start() method for the HCD associated with it
54 * through the hotplug entry's driver_data.
56 * Store this function in the HCD's struct pci_driver as probe().
58 int usb_hcd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
60 struct hc_driver *driver;
69 driver = (struct hc_driver *)id->driver_data;
73 if (pci_enable_device(dev) < 0)
75 dev->current_state = PCI_D0;
79 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
85 hcd = usb_create_hcd(driver, &dev->dev, pci_name(dev));
91 if (driver->flags & HCD_MEMORY) {
93 hcd->rsrc_start = pci_resource_start(dev, 0);
94 hcd->rsrc_len = pci_resource_len(dev, 0);
95 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
96 driver->description)) {
97 dev_dbg(&dev->dev, "controller already in use\n");
101 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
102 if (hcd->regs == NULL) {
103 dev_dbg(&dev->dev, "error mapping memory\n");
112 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
113 if (!(pci_resource_flags(dev, region) &
117 hcd->rsrc_start = pci_resource_start(dev, region);
118 hcd->rsrc_len = pci_resource_len(dev, region);
119 if (request_region(hcd->rsrc_start, hcd->rsrc_len,
120 driver->description))
123 if (region == PCI_ROM_RESOURCE) {
124 dev_dbg(&dev->dev, "no i/o regions available\n");
131 device_set_wakeup_enable(&dev->dev, 1);
133 retval = usb_add_hcd(hcd, dev->irq, IRQF_DISABLED | IRQF_SHARED);
139 if (driver->flags & HCD_MEMORY) {
142 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
144 release_region(hcd->rsrc_start, hcd->rsrc_len);
148 pci_disable_device(dev);
149 dev_err(&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
152 EXPORT_SYMBOL_GPL(usb_hcd_pci_probe);
155 /* may be called without controller electrically present */
156 /* may be called with controller, bus, and devices active */
159 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
160 * @dev: USB Host Controller being removed
161 * Context: !in_interrupt()
163 * Reverses the effect of usb_hcd_pci_probe(), first invoking
164 * the HCD's stop() method. It is always called from a thread
165 * context, normally "rmmod", "apmd", or something similar.
167 * Store this function in the HCD's struct pci_driver as remove().
169 void usb_hcd_pci_remove(struct pci_dev *dev)
173 hcd = pci_get_drvdata(dev);
178 if (hcd->driver->flags & HCD_MEMORY) {
180 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
182 release_region(hcd->rsrc_start, hcd->rsrc_len);
185 pci_disable_device(dev);
187 EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
193 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
194 * @dev: USB Host Controller being suspended
195 * @message: Power Management message describing this state transition
197 * Store this function in the HCD's struct pci_driver as .suspend.
199 int usb_hcd_pci_suspend(struct pci_dev *dev, pm_message_t message)
201 struct usb_hcd *hcd = pci_get_drvdata(dev);
205 /* Root hub suspend should have stopped all downstream traffic,
206 * and all bus master traffic. And done so for both the interface
207 * and the stub usb_device (which we check here). But maybe it
208 * didn't; writing sysfs power/state files ignores such rules...
210 * We must ignore the FREEZE vs SUSPEND distinction here, because
211 * otherwise the swsusp will save (and restore) garbage state.
213 if (!(hcd->state == HC_STATE_SUSPENDED ||
214 hcd->state == HC_STATE_HALT)) {
215 dev_warn(&dev->dev, "Root hub is not suspended\n");
220 /* We might already be suspended (runtime PM -- not yet written) */
221 if (dev->current_state != PCI_D0)
224 if (hcd->driver->pci_suspend) {
225 retval = hcd->driver->pci_suspend(hcd, message);
226 suspend_report_result(hcd->driver->pci_suspend, retval);
231 synchronize_irq(dev->irq);
233 /* Don't fail on error to enable wakeup. We rely on pci code
234 * to reject requests the hardware can't implement, rather
235 * than coding the same thing.
237 wake = (hcd->state == HC_STATE_SUSPENDED &&
238 device_may_wakeup(&dev->dev));
239 w = pci_wake_from_d3(dev, wake);
242 dev_dbg(&dev->dev, "wakeup: %d\n", wake);
244 /* Downstream ports from this root hub should already be quiesced, so
245 * there will be no DMA activity. Now we can shut down the upstream
246 * link (except maybe for PME# resume signaling) and enter some PCI
247 * low power state, if the hardware allows.
249 pci_disable_device(dev);
253 EXPORT_SYMBOL_GPL(usb_hcd_pci_suspend);
256 * usb_hcd_pci_suspend_late - suspend a PCI-based HCD after IRQs are disabled
257 * @dev: USB Host Controller being suspended
258 * @message: Power Management message describing this state transition
260 * Store this function in the HCD's struct pci_driver as .suspend_late.
262 int usb_hcd_pci_suspend_late(struct pci_dev *dev, pm_message_t message)
267 /* We might already be suspended (runtime PM -- not yet written) */
268 if (dev->current_state != PCI_D0)
273 /* Don't change state if we don't need to */
274 if (message.event == PM_EVENT_FREEZE ||
275 message.event == PM_EVENT_PRETHAW) {
276 dev_dbg(&dev->dev, "--> no state change\n");
280 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
282 dev_dbg(&dev->dev, "--> PCI D0 legacy\n");
285 /* NOTE: dev->current_state becomes nonzero only here, and
286 * only for devices that support PCI PM. Also, exiting
287 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset
288 * some device state (e.g. as part of clock reinit).
290 retval = pci_set_power_state(dev, PCI_D3hot);
291 suspend_report_result(pci_set_power_state, retval);
293 dev_dbg(&dev->dev, "--> PCI D3\n");
295 dev_dbg(&dev->dev, "PCI D3 suspend fail, %d\n",
297 pci_restore_state(dev);
301 #ifdef CONFIG_PPC_PMAC
303 /* Disable ASIC clocks for USB */
304 if (machine_is(powermac)) {
305 struct device_node *of_node;
307 of_node = pci_device_to_OF_node(dev);
309 pmac_call_feature(PMAC_FTR_USB_ENABLE,
318 EXPORT_SYMBOL_GPL(usb_hcd_pci_suspend_late);
321 * usb_hcd_pci_resume_early - resume a PCI-based HCD before IRQs are enabled
322 * @dev: USB Host Controller being resumed
324 * Store this function in the HCD's struct pci_driver as .resume_early.
326 int usb_hcd_pci_resume_early(struct pci_dev *dev)
329 pci_power_t state = dev->current_state;
331 #ifdef CONFIG_PPC_PMAC
332 /* Reenable ASIC clocks for USB */
333 if (machine_is(powermac)) {
334 struct device_node *of_node;
336 of_node = pci_device_to_OF_node(dev);
338 pmac_call_feature(PMAC_FTR_USB_ENABLE,
343 /* NOTE: chip docs cover clean "real suspend" cases (what Linux
344 * calls "standby", "suspend to RAM", and so on). There are also
345 * dirty cases when swsusp fakes a suspend in "shutdown" mode.
347 if (state != PCI_D0) {
352 pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
353 pci_read_config_word(dev, pci_pm + PCI_PM_CTRL, &pmcr);
354 pmcr &= PCI_PM_CTRL_STATE_MASK;
356 /* Clean case: power to USB and to HC registers was
357 * maintained; remote wakeup is easy.
359 dev_dbg(&dev->dev, "resume from PCI D%d\n", pmcr);
361 /* Clean: HC lost Vcc power, D0 uninitialized
362 * + Vaux may have preserved port and transceiver
363 * state ... for remote wakeup from D3cold
364 * + or not; HCD must reinit + re-enumerate
366 * Dirty: D0 semi-initialized cases with swsusp
368 * + after Linux init (HCD statically linked)
370 dev_dbg(&dev->dev, "resume from previous PCI D%d\n",
375 retval = pci_set_power_state(dev, PCI_D0);
377 /* Same basic cases: clean (powered/not), dirty */
378 dev_dbg(&dev->dev, "PCI legacy resume\n");
382 dev_err(&dev->dev, "can't resume: %d\n", retval);
384 pci_restore_state(dev);
388 EXPORT_SYMBOL_GPL(usb_hcd_pci_resume_early);
391 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
392 * @dev: USB Host Controller being resumed
394 * Store this function in the HCD's struct pci_driver as .resume.
396 int usb_hcd_pci_resume(struct pci_dev *dev)
401 hcd = pci_get_drvdata(dev);
402 if (hcd->state != HC_STATE_SUSPENDED) {
403 dev_dbg(hcd->self.controller,
404 "can't resume, not suspended!\n");
408 retval = pci_enable_device(dev);
410 dev_err(&dev->dev, "can't re-enable after resume, %d!\n",
417 /* yes, ignore this result too... */
418 (void) pci_wake_from_d3(dev, 0);
420 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
422 if (hcd->driver->pci_resume) {
423 retval = hcd->driver->pci_resume(hcd);
425 dev_err(hcd->self.controller,
426 "PCI post-resume error %d!\n", retval);
432 EXPORT_SYMBOL_GPL(usb_hcd_pci_resume);
434 #endif /* CONFIG_PM */
437 * usb_hcd_pci_shutdown - shutdown host controller
438 * @dev: USB Host Controller being shutdown
440 void usb_hcd_pci_shutdown(struct pci_dev *dev)
444 hcd = pci_get_drvdata(dev);
448 if (hcd->driver->shutdown)
449 hcd->driver->shutdown(hcd);
451 EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);