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
;
67 if (!id
|| !(driver
= (struct hc_driver
*) id
->driver_data
))
70 if (pci_enable_device (dev
) < 0)
72 dev
->current_state
= PCI_D0
;
73 dev
->dev
.power
.power_state
= PMSG_ON
;
77 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
83 hcd
= usb_create_hcd (driver
, &dev
->dev
, pci_name(dev
));
89 if (driver
->flags
& HCD_MEMORY
) { // EHCI, OHCI
90 hcd
->rsrc_start
= pci_resource_start (dev
, 0);
91 hcd
->rsrc_len
= pci_resource_len (dev
, 0);
92 if (!request_mem_region (hcd
->rsrc_start
, hcd
->rsrc_len
,
93 driver
->description
)) {
94 dev_dbg (&dev
->dev
, "controller already in use\n");
98 hcd
->regs
= ioremap_nocache (hcd
->rsrc_start
, hcd
->rsrc_len
);
99 if (hcd
->regs
== NULL
) {
100 dev_dbg (&dev
->dev
, "error mapping memory\n");
108 for (region
= 0; region
< PCI_ROM_RESOURCE
; region
++) {
109 if (!(pci_resource_flags (dev
, region
) &
113 hcd
->rsrc_start
= pci_resource_start (dev
, region
);
114 hcd
->rsrc_len
= pci_resource_len (dev
, region
);
115 if (request_region (hcd
->rsrc_start
, hcd
->rsrc_len
,
116 driver
->description
))
119 if (region
== PCI_ROM_RESOURCE
) {
120 dev_dbg (&dev
->dev
, "no i/o regions available\n");
126 pci_set_master (dev
);
128 retval
= usb_add_hcd(hcd
, dev
->irq
, IRQF_DISABLED
| IRQF_SHARED
);
134 if (driver
->flags
& HCD_MEMORY
) {
137 release_mem_region (hcd
->rsrc_start
, hcd
->rsrc_len
);
139 release_region (hcd
->rsrc_start
, hcd
->rsrc_len
);
143 pci_disable_device (dev
);
144 dev_err (&dev
->dev
, "init %s fail, %d\n", pci_name(dev
), retval
);
147 EXPORT_SYMBOL_GPL(usb_hcd_pci_probe
);
150 /* may be called without controller electrically present */
151 /* may be called with controller, bus, and devices active */
154 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
155 * @dev: USB Host Controller being removed
156 * Context: !in_interrupt()
158 * Reverses the effect of usb_hcd_pci_probe(), first invoking
159 * the HCD's stop() method. It is always called from a thread
160 * context, normally "rmmod", "apmd", or something similar.
162 * Store this function in the HCD's struct pci_driver as remove().
164 void usb_hcd_pci_remove (struct pci_dev
*dev
)
168 hcd
= pci_get_drvdata(dev
);
172 usb_remove_hcd (hcd
);
173 if (hcd
->driver
->flags
& HCD_MEMORY
) {
175 release_mem_region (hcd
->rsrc_start
, hcd
->rsrc_len
);
177 release_region (hcd
->rsrc_start
, hcd
->rsrc_len
);
180 pci_disable_device(dev
);
182 EXPORT_SYMBOL_GPL(usb_hcd_pci_remove
);
188 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
189 * @dev: USB Host Controller being suspended
190 * @message: semantics in flux
192 * Store this function in the HCD's struct pci_driver as suspend().
194 int usb_hcd_pci_suspend (struct pci_dev
*dev
, pm_message_t message
)
200 hcd
= pci_get_drvdata(dev
);
202 /* Root hub suspend should have stopped all downstream traffic,
203 * and all bus master traffic. And done so for both the interface
204 * and the stub usb_device (which we check here). But maybe it
205 * didn't; writing sysfs power/state files ignores such rules...
207 * We must ignore the FREEZE vs SUSPEND distinction here, because
208 * otherwise the swsusp will save (and restore) garbage state.
210 if (!(hcd
->state
== HC_STATE_SUSPENDED
||
211 hcd
->state
== HC_STATE_HALT
))
214 if (hcd
->driver
->suspend
) {
215 retval
= hcd
->driver
->suspend(hcd
, message
);
216 suspend_report_result(hcd
->driver
->suspend
, retval
);
220 synchronize_irq(dev
->irq
);
222 /* FIXME until the generic PM interfaces change a lot more, this
223 * can't use PCI D1 and D2 states. For example, the confusion
224 * between messages and states will need to vanish, and messages
225 * will need to provide a target system state again.
227 * It'll be important to learn characteristics of the target state,
228 * especially on embedded hardware where the HCD will often be in
229 * charge of an external VBUS power supply and one or more clocks.
230 * Some target system states will leave them active; others won't.
231 * (With PCI, that's often handled by platform BIOS code.)
234 /* even when the PCI layer rejects some of the PCI calls
235 * below, HCs can try global suspend and reduce DMA traffic.
236 * PM-sensitive HCDs may already have done this.
238 has_pci_pm
= pci_find_capability(dev
, PCI_CAP_ID_PM
);
240 /* Downstream ports from this root hub should already be quiesced, so
241 * there will be no DMA activity. Now we can shut down the upstream
242 * link (except maybe for PME# resume signaling) and enter some PCI
243 * low power state, if the hardware allows.
245 if (hcd
->state
== HC_STATE_SUSPENDED
) {
247 /* no DMA or IRQs except when HC is active */
248 if (dev
->current_state
== PCI_D0
) {
249 pci_save_state (dev
);
250 pci_disable_device (dev
);
253 if (message
.event
== PM_EVENT_FREEZE
||
254 message
.event
== PM_EVENT_PRETHAW
) {
255 dev_dbg(hcd
->self
.controller
, "--> no state change\n");
260 dev_dbg (hcd
->self
.controller
, "--> PCI D0/legacy\n");
264 /* NOTE: dev->current_state becomes nonzero only here, and
265 * only for devices that support PCI PM. Also, exiting
266 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset
267 * some device state (e.g. as part of clock reinit).
269 retval
= pci_set_power_state (dev
, PCI_D3hot
);
270 suspend_report_result(pci_set_power_state
, retval
);
272 int wake
= device_can_wakeup(&hcd
->self
.root_hub
->dev
);
274 wake
= wake
&& device_may_wakeup(hcd
->self
.controller
);
276 dev_dbg (hcd
->self
.controller
, "--> PCI D3%s\n",
277 wake
? "/wakeup" : "");
279 /* Ignore these return values. We rely on pci code to
280 * reject requests the hardware can't implement, rather
281 * than coding the same thing.
283 (void) pci_enable_wake (dev
, PCI_D3hot
, wake
);
284 (void) pci_enable_wake (dev
, PCI_D3cold
, wake
);
286 dev_dbg (&dev
->dev
, "PCI D3 suspend fail, %d\n",
288 (void) usb_hcd_pci_resume (dev
);
291 } else if (hcd
->state
!= HC_STATE_HALT
) {
292 dev_dbg (hcd
->self
.controller
, "hcd state %d; not suspended\n",
300 dev
->dev
.power
.power_state
= PMSG_SUSPEND
;
302 #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
,
317 EXPORT_SYMBOL_GPL(usb_hcd_pci_suspend
);
320 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
321 * @dev: USB Host Controller being resumed
323 * Store this function in the HCD's struct pci_driver as resume().
325 int usb_hcd_pci_resume (struct pci_dev
*dev
)
330 hcd
= pci_get_drvdata(dev
);
331 if (hcd
->state
!= HC_STATE_SUSPENDED
) {
332 dev_dbg (hcd
->self
.controller
,
333 "can't resume, not suspended!\n");
337 #ifdef CONFIG_PPC_PMAC
338 /* Reenable ASIC clocks for USB */
339 if (machine_is(powermac
)) {
340 struct device_node
*of_node
;
342 of_node
= pci_device_to_OF_node (dev
);
344 pmac_call_feature (PMAC_FTR_USB_ENABLE
,
349 /* NOTE: chip docs cover clean "real suspend" cases (what Linux
350 * calls "standby", "suspend to RAM", and so on). There are also
351 * dirty cases when swsusp fakes a suspend in "shutdown" mode.
353 if (dev
->current_state
!= PCI_D0
) {
358 pci_pm
= pci_find_capability(dev
, PCI_CAP_ID_PM
);
359 pci_read_config_word(dev
, pci_pm
+ PCI_PM_CTRL
, &pmcr
);
360 pmcr
&= PCI_PM_CTRL_STATE_MASK
;
362 /* Clean case: power to USB and to HC registers was
363 * maintained; remote wakeup is easy.
365 dev_dbg(hcd
->self
.controller
, "resume from PCI D%d\n",
368 /* Clean: HC lost Vcc power, D0 uninitialized
369 * + Vaux may have preserved port and transceiver
370 * state ... for remote wakeup from D3cold
371 * + or not; HCD must reinit + re-enumerate
373 * Dirty: D0 semi-initialized cases with swsusp
375 * + after Linux init (HCD statically linked)
377 dev_dbg(hcd
->self
.controller
,
378 "PCI D0, from previous PCI D%d\n",
382 /* yes, ignore these results too... */
383 (void) pci_enable_wake (dev
, dev
->current_state
, 0);
384 (void) pci_enable_wake (dev
, PCI_D3cold
, 0);
386 /* Same basic cases: clean (powered/not), dirty */
387 dev_dbg(hcd
->self
.controller
, "PCI legacy resume\n");
390 /* NOTE: the PCI API itself is asymmetric here. We don't need to
391 * pci_set_power_state(PCI_D0) since that's part of re-enabling;
392 * but that won't re-enable bus mastering. Yet pci_disable_device()
393 * explicitly disables bus mastering...
395 retval
= pci_enable_device (dev
);
397 dev_err (hcd
->self
.controller
,
398 "can't re-enable after resume, %d!\n", retval
);
401 pci_set_master (dev
);
402 pci_restore_state (dev
);
404 dev
->dev
.power
.power_state
= PMSG_ON
;
406 clear_bit(HCD_FLAG_SAW_IRQ
, &hcd
->flags
);
408 if (hcd
->driver
->resume
) {
409 retval
= hcd
->driver
->resume(hcd
);
411 dev_err (hcd
->self
.controller
,
412 "PCI post-resume error %d!\n", retval
);
419 EXPORT_SYMBOL_GPL(usb_hcd_pci_resume
);
421 #endif /* CONFIG_PM */
424 * usb_hcd_pci_shutdown - shutdown host controller
425 * @dev: USB Host Controller being shutdown
427 void usb_hcd_pci_shutdown (struct pci_dev
*dev
)
431 hcd
= pci_get_drvdata(dev
);
435 if (hcd
->driver
->shutdown
)
436 hcd
->driver
->shutdown(hcd
);
438 EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown
);