blob: edf4300a3f7a75d748776ab3fe3d69fbf1a6f66d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * (C) Copyright David Brownell 2000-2002
3 *
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.
8 *
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
12 * for more details.
13 *
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.
17 */
18
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/pci.h>
David Brownell21b18612005-11-23 15:45:42 -080022#include <linux/usb.h>
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/io.h>
25#include <asm/irq.h>
David Brownell21b18612005-11-23 15:45:42 -080026
27#ifdef CONFIG_PPC_PMAC
28#include <asm/machdep.h>
29#include <asm/pmac_feature.h>
30#include <asm/pci-bridge.h>
31#include <asm/prom.h>
32#endif
David Brownell5f827ea2005-09-22 22:38:16 -070033
34#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "hcd.h"
36
37
David Brownellc6053ec2005-04-18 17:39:22 -070038/* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
41/*-------------------------------------------------------------------------*/
42
43/* configure so an HC device and id are always provided */
44/* always called with process context; sleeping is OK */
45
46/**
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()
51 *
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.
55 *
56 * Store this function in the HCD's struct pci_driver as probe().
57 */
58int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
59{
60 struct hc_driver *driver;
61 struct usb_hcd *hcd;
62 int retval;
63
64 if (usb_disabled())
65 return -ENODEV;
66
67 if (!id || !(driver = (struct hc_driver *) id->driver_data))
68 return -EINVAL;
69
70 if (pci_enable_device (dev) < 0)
71 return -ENODEV;
David Brownellc6053ec2005-04-18 17:39:22 -070072 dev->current_state = PCI_D0;
73 dev->dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (!dev->irq) {
76 dev_err (&dev->dev,
77 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
78 pci_name(dev));
79 retval = -ENODEV;
80 goto err1;
81 }
82
83 hcd = usb_create_hcd (driver, &dev->dev, pci_name(dev));
84 if (!hcd) {
85 retval = -ENOMEM;
86 goto err1;
87 }
88
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");
95 retval = -EBUSY;
96 goto err2;
97 }
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");
101 retval = -EFAULT;
102 goto err3;
103 }
104
105 } else { // UHCI
106 int region;
107
108 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
109 if (!(pci_resource_flags (dev, region) &
110 IORESOURCE_IO))
111 continue;
112
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))
117 break;
118 }
119 if (region == PCI_ROM_RESOURCE) {
120 dev_dbg (&dev->dev, "no i/o regions available\n");
121 retval = -EBUSY;
122 goto err1;
123 }
124 }
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 pci_set_master (dev);
127
Thomas Gleixnerd54b5ca2006-07-01 19:29:44 -0700128 retval = usb_add_hcd (hcd, dev->irq, IRQF_SHARED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (retval != 0)
130 goto err4;
131 return retval;
132
133 err4:
134 if (driver->flags & HCD_MEMORY) {
135 iounmap (hcd->regs);
136 err3:
137 release_mem_region (hcd->rsrc_start, hcd->rsrc_len);
138 } else
139 release_region (hcd->rsrc_start, hcd->rsrc_len);
140 err2:
141 usb_put_hcd (hcd);
142 err1:
143 pci_disable_device (dev);
144 dev_err (&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
145 return retval;
146}
147EXPORT_SYMBOL (usb_hcd_pci_probe);
148
149
150/* may be called without controller electrically present */
151/* may be called with controller, bus, and devices active */
152
153/**
154 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
155 * @dev: USB Host Controller being removed
156 * Context: !in_interrupt()
157 *
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.
161 *
162 * Store this function in the HCD's struct pci_driver as remove().
163 */
164void usb_hcd_pci_remove (struct pci_dev *dev)
165{
166 struct usb_hcd *hcd;
167
168 hcd = pci_get_drvdata(dev);
169 if (!hcd)
170 return;
171
172 usb_remove_hcd (hcd);
173 if (hcd->driver->flags & HCD_MEMORY) {
174 iounmap (hcd->regs);
175 release_mem_region (hcd->rsrc_start, hcd->rsrc_len);
176 } else {
177 release_region (hcd->rsrc_start, hcd->rsrc_len);
178 }
179 usb_put_hcd (hcd);
180 pci_disable_device(dev);
181}
182EXPORT_SYMBOL (usb_hcd_pci_remove);
183
184
185#ifdef CONFIG_PM
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/**
188 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
189 * @dev: USB Host Controller being suspended
David Brownellc6053ec2005-04-18 17:39:22 -0700190 * @message: semantics in flux
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 *
192 * Store this function in the HCD's struct pci_driver as suspend().
193 */
David Brownellc6053ec2005-04-18 17:39:22 -0700194int usb_hcd_pci_suspend (struct pci_dev *dev, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct usb_hcd *hcd;
197 int retval = 0;
198 int has_pci_pm;
199
200 hcd = pci_get_drvdata(dev);
201
David Brownell5f827ea2005-09-22 22:38:16 -0700202 /* 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...
206 *
207 * We must ignore the FREEZE vs SUSPEND distinction here, because
208 * otherwise the swsusp will save (and restore) garbage state.
209 */
210 if (hcd->self.root_hub->dev.power.power_state.event == PM_EVENT_ON)
211 return -EBUSY;
212
213 if (hcd->driver->suspend) {
214 retval = hcd->driver->suspend(hcd, message);
Andrew Morton02669492006-03-23 01:38:34 -0800215 suspend_report_result(hcd->driver->suspend, retval);
216 if (retval)
David Brownell5f827ea2005-09-22 22:38:16 -0700217 goto done;
David Brownell5f827ea2005-09-22 22:38:16 -0700218 }
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100219 synchronize_irq(dev->irq);
David Brownell5f827ea2005-09-22 22:38:16 -0700220
David Brownellc6053ec2005-04-18 17:39:22 -0700221 /* FIXME until the generic PM interfaces change a lot more, this
222 * can't use PCI D1 and D2 states. For example, the confusion
223 * between messages and states will need to vanish, and messages
224 * will need to provide a target system state again.
225 *
226 * It'll be important to learn characteristics of the target state,
227 * especially on embedded hardware where the HCD will often be in
228 * charge of an external VBUS power supply and one or more clocks.
229 * Some target system states will leave them active; others won't.
230 * (With PCI, that's often handled by platform BIOS code.)
231 */
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* even when the PCI layer rejects some of the PCI calls
234 * below, HCs can try global suspend and reduce DMA traffic.
235 * PM-sensitive HCDs may already have done this.
236 */
237 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
David Brownell5f827ea2005-09-22 22:38:16 -0700239 /* Downstream ports from this root hub should already be quiesced, so
240 * there will be no DMA activity. Now we can shut down the upstream
241 * link (except maybe for PME# resume signaling) and enter some PCI
242 * low power state, if the hardware allows.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
David Brownell5f827ea2005-09-22 22:38:16 -0700244 if (hcd->state == HC_STATE_SUSPENDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David Brownellc6053ec2005-04-18 17:39:22 -0700246 /* no DMA or IRQs except when HC is active */
247 if (dev->current_state == PCI_D0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 pci_save_state (dev);
249 pci_disable_device (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 if (!has_pci_pm) {
253 dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n");
David Brownell5f827ea2005-09-22 22:38:16 -0700254 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
David Brownellc6053ec2005-04-18 17:39:22 -0700257 /* NOTE: dev->current_state becomes nonzero only here, and
258 * only for devices that support PCI PM. Also, exiting
259 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset
260 * some device state (e.g. as part of clock reinit).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
David Brownellc6053ec2005-04-18 17:39:22 -0700262 retval = pci_set_power_state (dev, PCI_D3hot);
Andrew Morton02669492006-03-23 01:38:34 -0800263 suspend_report_result(pci_set_power_state, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (retval == 0) {
David Brownellfb669cc2006-01-24 08:40:27 -0800265 int wake = device_can_wakeup(&hcd->self.root_hub->dev);
266
267 wake = wake && device_may_wakeup(hcd->self.controller);
268
269 dev_dbg (hcd->self.controller, "--> PCI D3%s\n",
270 wake ? "/wakeup" : "");
David Brownell5f827ea2005-09-22 22:38:16 -0700271
272 /* Ignore these return values. We rely on pci code to
273 * reject requests the hardware can't implement, rather
274 * than coding the same thing.
275 */
David Brownellfb669cc2006-01-24 08:40:27 -0800276 (void) pci_enable_wake (dev, PCI_D3hot, wake);
277 (void) pci_enable_wake (dev, PCI_D3cold, wake);
David Brownell5f827ea2005-09-22 22:38:16 -0700278 } else {
David Brownellc6053ec2005-04-18 17:39:22 -0700279 dev_dbg (&dev->dev, "PCI D3 suspend fail, %d\n",
280 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 (void) usb_hcd_pci_resume (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
David Brownell5f827ea2005-09-22 22:38:16 -0700283
David Brownell18584992006-08-14 23:11:06 -0700284 } else if (hcd->state != HC_STATE_HALT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n",
286 hcd->state);
David Brownellc6053ec2005-04-18 17:39:22 -0700287 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
David Brownell5f827ea2005-09-22 22:38:16 -0700291done:
David Brownell21b18612005-11-23 15:45:42 -0800292 if (retval == 0) {
David Brownell5f827ea2005-09-22 22:38:16 -0700293 dev->dev.power.power_state = PMSG_SUSPEND;
David Brownell21b18612005-11-23 15:45:42 -0800294
295#ifdef CONFIG_PPC_PMAC
296 /* Disable ASIC clocks for USB */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100297 if (machine_is(powermac)) {
David Brownell21b18612005-11-23 15:45:42 -0800298 struct device_node *of_node;
299
300 of_node = pci_device_to_OF_node (dev);
301 if (of_node)
302 pmac_call_feature(PMAC_FTR_USB_ENABLE,
303 of_node, 0, 0);
304 }
305#endif
306 }
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return retval;
309}
310EXPORT_SYMBOL (usb_hcd_pci_suspend);
311
312/**
313 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
314 * @dev: USB Host Controller being resumed
315 *
316 * Store this function in the HCD's struct pci_driver as resume().
317 */
318int usb_hcd_pci_resume (struct pci_dev *dev)
319{
320 struct usb_hcd *hcd;
321 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 hcd = pci_get_drvdata(dev);
324 if (hcd->state != HC_STATE_SUSPENDED) {
325 dev_dbg (hcd->self.controller,
326 "can't resume, not suspended!\n");
327 return 0;
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
David Brownell21b18612005-11-23 15:45:42 -0800330#ifdef CONFIG_PPC_PMAC
331 /* Reenable ASIC clocks for USB */
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100332 if (machine_is(powermac)) {
David Brownell21b18612005-11-23 15:45:42 -0800333 struct device_node *of_node;
334
335 of_node = pci_device_to_OF_node (dev);
336 if (of_node)
337 pmac_call_feature (PMAC_FTR_USB_ENABLE,
338 of_node, 0, 1);
339 }
340#endif
341
David Brownellc6053ec2005-04-18 17:39:22 -0700342 /* NOTE: chip docs cover clean "real suspend" cases (what Linux
343 * calls "standby", "suspend to RAM", and so on). There are also
344 * dirty cases when swsusp fakes a suspend in "shutdown" mode.
345 */
346 if (dev->current_state != PCI_D0) {
347#ifdef DEBUG
348 int pci_pm;
349 u16 pmcr;
350
351 pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
352 pci_read_config_word(dev, pci_pm + PCI_PM_CTRL, &pmcr);
353 pmcr &= PCI_PM_CTRL_STATE_MASK;
354 if (pmcr) {
355 /* Clean case: power to USB and to HC registers was
356 * maintained; remote wakeup is easy.
357 */
358 dev_dbg(hcd->self.controller, "resume from PCI D%d\n",
359 pmcr);
360 } else {
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
365 *
366 * Dirty: D0 semi-initialized cases with swsusp
367 * + after BIOS init
368 * + after Linux init (HCD statically linked)
369 */
370 dev_dbg(hcd->self.controller,
371 "PCI D0, from previous PCI D%d\n",
372 dev->current_state);
373 }
374#endif
David Brownell5f827ea2005-09-22 22:38:16 -0700375 /* yes, ignore these results too... */
376 (void) pci_enable_wake (dev, dev->current_state, 0);
377 (void) pci_enable_wake (dev, PCI_D3cold, 0);
David Brownellc6053ec2005-04-18 17:39:22 -0700378 } else {
379 /* Same basic cases: clean (powered/not), dirty */
380 dev_dbg(hcd->self.controller, "PCI legacy resume\n");
381 }
382
383 /* NOTE: the PCI API itself is asymmetric here. We don't need to
384 * pci_set_power_state(PCI_D0) since that's part of re-enabling;
385 * but that won't re-enable bus mastering. Yet pci_disable_device()
386 * explicitly disables bus mastering...
387 */
388 retval = pci_enable_device (dev);
389 if (retval < 0) {
390 dev_err (hcd->self.controller,
391 "can't re-enable after resume, %d!\n", retval);
392 return retval;
393 }
394 pci_set_master (dev);
395 pci_restore_state (dev);
396
397 dev->dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Benjamin Herrenschmidt8de98402005-11-25 09:59:46 +1100399 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Brownell5f827ea2005-09-22 22:38:16 -0700401 if (hcd->driver->resume) {
402 retval = hcd->driver->resume(hcd);
403 if (retval) {
404 dev_err (hcd->self.controller,
405 "PCI post-resume error %d!\n", retval);
406 usb_hc_died (hcd);
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return retval;
411}
412EXPORT_SYMBOL (usb_hcd_pci_resume);
413
414#endif /* CONFIG_PM */
415
Aleksey Gorelov64a21d02006-08-08 17:24:08 -0700416/**
417 * usb_hcd_pci_shutdown - shutdown host controller
418 * @dev: USB Host Controller being shutdown
419 */
420void usb_hcd_pci_shutdown (struct pci_dev *dev)
421{
422 struct usb_hcd *hcd;
423
424 hcd = pci_get_drvdata(dev);
425 if (!hcd)
426 return;
427
428 if (hcd->driver->shutdown)
429 hcd->driver->shutdown(hcd);
430}
431EXPORT_SYMBOL (usb_hcd_pci_shutdown);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432