blob: 5131d88e8c5bd7c5b39b0e9b1f27d3a2c5a85eda [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
19#include <linux/config.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/pci.h>
David Brownell21b18612005-11-23 15:45:42 -080023#include <linux/usb.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
26#include <asm/irq.h>
David Brownell21b18612005-11-23 15:45:42 -080027
28#ifdef CONFIG_PPC_PMAC
29#include <asm/machdep.h>
30#include <asm/pmac_feature.h>
31#include <asm/pci-bridge.h>
32#include <asm/prom.h>
33#endif
David Brownell5f827ea2005-09-22 22:38:16 -070034
35#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "hcd.h"
37
38
David Brownellc6053ec2005-04-18 17:39:22 -070039/* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41
42/*-------------------------------------------------------------------------*/
43
44/* configure so an HC device and id are always provided */
45/* always called with process context; sleeping is OK */
46
47/**
48 * usb_hcd_pci_probe - initialize PCI-based HCDs
49 * @dev: USB Host Controller being probed
50 * @id: pci hotplug id connecting controller to HCD framework
51 * Context: !in_interrupt()
52 *
53 * Allocates basic PCI resources for this USB host controller, and
54 * then invokes the start() method for the HCD associated with it
55 * through the hotplug entry's driver_data.
56 *
57 * Store this function in the HCD's struct pci_driver as probe().
58 */
59int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
60{
61 struct hc_driver *driver;
62 struct usb_hcd *hcd;
63 int retval;
64
65 if (usb_disabled())
66 return -ENODEV;
67
68 if (!id || !(driver = (struct hc_driver *) id->driver_data))
69 return -EINVAL;
70
71 if (pci_enable_device (dev) < 0)
72 return -ENODEV;
David Brownellc6053ec2005-04-18 17:39:22 -070073 dev->current_state = PCI_D0;
74 dev->dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (!dev->irq) {
77 dev_err (&dev->dev,
78 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
79 pci_name(dev));
80 retval = -ENODEV;
81 goto err1;
82 }
83
84 hcd = usb_create_hcd (driver, &dev->dev, pci_name(dev));
85 if (!hcd) {
86 retval = -ENOMEM;
87 goto err1;
88 }
89
90 if (driver->flags & HCD_MEMORY) { // EHCI, OHCI
91 hcd->rsrc_start = pci_resource_start (dev, 0);
92 hcd->rsrc_len = pci_resource_len (dev, 0);
93 if (!request_mem_region (hcd->rsrc_start, hcd->rsrc_len,
94 driver->description)) {
95 dev_dbg (&dev->dev, "controller already in use\n");
96 retval = -EBUSY;
97 goto err2;
98 }
99 hcd->regs = ioremap_nocache (hcd->rsrc_start, hcd->rsrc_len);
100 if (hcd->regs == NULL) {
101 dev_dbg (&dev->dev, "error mapping memory\n");
102 retval = -EFAULT;
103 goto err3;
104 }
105
106 } else { // UHCI
107 int region;
108
109 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
110 if (!(pci_resource_flags (dev, region) &
111 IORESOURCE_IO))
112 continue;
113
114 hcd->rsrc_start = pci_resource_start (dev, region);
115 hcd->rsrc_len = pci_resource_len (dev, region);
116 if (request_region (hcd->rsrc_start, hcd->rsrc_len,
117 driver->description))
118 break;
119 }
120 if (region == PCI_ROM_RESOURCE) {
121 dev_dbg (&dev->dev, "no i/o regions available\n");
122 retval = -EBUSY;
123 goto err1;
124 }
125 }
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 pci_set_master (dev);
128
129 retval = usb_add_hcd (hcd, dev->irq, SA_SHIRQ);
130 if (retval != 0)
131 goto err4;
132 return retval;
133
134 err4:
135 if (driver->flags & HCD_MEMORY) {
136 iounmap (hcd->regs);
137 err3:
138 release_mem_region (hcd->rsrc_start, hcd->rsrc_len);
139 } else
140 release_region (hcd->rsrc_start, hcd->rsrc_len);
141 err2:
142 usb_put_hcd (hcd);
143 err1:
144 pci_disable_device (dev);
145 dev_err (&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
146 return retval;
147}
148EXPORT_SYMBOL (usb_hcd_pci_probe);
149
150
151/* may be called without controller electrically present */
152/* may be called with controller, bus, and devices active */
153
154/**
155 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
156 * @dev: USB Host Controller being removed
157 * Context: !in_interrupt()
158 *
159 * Reverses the effect of usb_hcd_pci_probe(), first invoking
160 * the HCD's stop() method. It is always called from a thread
161 * context, normally "rmmod", "apmd", or something similar.
162 *
163 * Store this function in the HCD's struct pci_driver as remove().
164 */
165void usb_hcd_pci_remove (struct pci_dev *dev)
166{
167 struct usb_hcd *hcd;
168
169 hcd = pci_get_drvdata(dev);
170 if (!hcd)
171 return;
172
173 usb_remove_hcd (hcd);
174 if (hcd->driver->flags & HCD_MEMORY) {
175 iounmap (hcd->regs);
176 release_mem_region (hcd->rsrc_start, hcd->rsrc_len);
177 } else {
178 release_region (hcd->rsrc_start, hcd->rsrc_len);
179 }
180 usb_put_hcd (hcd);
181 pci_disable_device(dev);
182}
183EXPORT_SYMBOL (usb_hcd_pci_remove);
184
185
186#ifdef CONFIG_PM
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/**
189 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
190 * @dev: USB Host Controller being suspended
David Brownellc6053ec2005-04-18 17:39:22 -0700191 * @message: semantics in flux
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
193 * Store this function in the HCD's struct pci_driver as suspend().
194 */
David Brownellc6053ec2005-04-18 17:39:22 -0700195int usb_hcd_pci_suspend (struct pci_dev *dev, pm_message_t message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct usb_hcd *hcd;
198 int retval = 0;
199 int has_pci_pm;
200
201 hcd = pci_get_drvdata(dev);
202
David Brownell5f827ea2005-09-22 22:38:16 -0700203 /* Root hub suspend should have stopped all downstream traffic,
204 * and all bus master traffic. And done so for both the interface
205 * and the stub usb_device (which we check here). But maybe it
206 * didn't; writing sysfs power/state files ignores such rules...
207 *
208 * We must ignore the FREEZE vs SUSPEND distinction here, because
209 * otherwise the swsusp will save (and restore) garbage state.
210 */
211 if (hcd->self.root_hub->dev.power.power_state.event == PM_EVENT_ON)
212 return -EBUSY;
213
214 if (hcd->driver->suspend) {
215 retval = hcd->driver->suspend(hcd, message);
216 if (retval) {
217 dev_dbg (&dev->dev, "PCI pre-suspend fail, %d\n",
218 retval);
219 goto done;
220 }
221 }
222
David Brownellc6053ec2005-04-18 17:39:22 -0700223 /* FIXME until the generic PM interfaces change a lot more, this
224 * can't use PCI D1 and D2 states. For example, the confusion
225 * between messages and states will need to vanish, and messages
226 * will need to provide a target system state again.
227 *
228 * It'll be important to learn characteristics of the target state,
229 * especially on embedded hardware where the HCD will often be in
230 * charge of an external VBUS power supply and one or more clocks.
231 * Some target system states will leave them active; others won't.
232 * (With PCI, that's often handled by platform BIOS code.)
233 */
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* even when the PCI layer rejects some of the PCI calls
236 * below, HCs can try global suspend and reduce DMA traffic.
237 * PM-sensitive HCDs may already have done this.
238 */
239 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
David Brownell5f827ea2005-09-22 22:38:16 -0700241 /* Downstream ports from this root hub should already be quiesced, so
242 * there will be no DMA activity. Now we can shut down the upstream
243 * link (except maybe for PME# resume signaling) and enter some PCI
244 * low power state, if the hardware allows.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
David Brownell5f827ea2005-09-22 22:38:16 -0700246 if (hcd->state == HC_STATE_SUSPENDED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David Brownellc6053ec2005-04-18 17:39:22 -0700248 /* no DMA or IRQs except when HC is active */
249 if (dev->current_state == PCI_D0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 pci_save_state (dev);
251 pci_disable_device (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 if (!has_pci_pm) {
255 dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n");
David Brownell5f827ea2005-09-22 22:38:16 -0700256 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
David Brownellc6053ec2005-04-18 17:39:22 -0700259 /* NOTE: dev->current_state becomes nonzero only here, and
260 * only for devices that support PCI PM. Also, exiting
261 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset
262 * some device state (e.g. as part of clock reinit).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
David Brownellc6053ec2005-04-18 17:39:22 -0700264 retval = pci_set_power_state (dev, PCI_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (retval == 0) {
David Brownellc6053ec2005-04-18 17:39:22 -0700266 dev_dbg (hcd->self.controller, "--> PCI D3\n");
David Brownell5f827ea2005-09-22 22:38:16 -0700267
268 /* Ignore these return values. We rely on pci code to
269 * reject requests the hardware can't implement, rather
270 * than coding the same thing.
271 */
272 (void) pci_enable_wake (dev, PCI_D3hot, hcd->remote_wakeup);
273 (void) pci_enable_wake (dev, PCI_D3cold, hcd->remote_wakeup);
274 } else {
David Brownellc6053ec2005-04-18 17:39:22 -0700275 dev_dbg (&dev->dev, "PCI D3 suspend fail, %d\n",
276 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 (void) usb_hcd_pci_resume (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
David Brownell5f827ea2005-09-22 22:38:16 -0700279
280 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n",
282 hcd->state);
David Brownellc6053ec2005-04-18 17:39:22 -0700283 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
David Brownell5f827ea2005-09-22 22:38:16 -0700287done:
David Brownell21b18612005-11-23 15:45:42 -0800288 if (retval == 0) {
David Brownell5f827ea2005-09-22 22:38:16 -0700289 dev->dev.power.power_state = PMSG_SUSPEND;
David Brownell21b18612005-11-23 15:45:42 -0800290
291#ifdef CONFIG_PPC_PMAC
292 /* Disable ASIC clocks for USB */
293 if (_machine == _MACH_Pmac) {
294 struct device_node *of_node;
295
296 of_node = pci_device_to_OF_node (dev);
297 if (of_node)
298 pmac_call_feature(PMAC_FTR_USB_ENABLE,
299 of_node, 0, 0);
300 }
301#endif
302 }
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return retval;
305}
306EXPORT_SYMBOL (usb_hcd_pci_suspend);
307
308/**
309 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
310 * @dev: USB Host Controller being resumed
311 *
312 * Store this function in the HCD's struct pci_driver as resume().
313 */
314int usb_hcd_pci_resume (struct pci_dev *dev)
315{
316 struct usb_hcd *hcd;
317 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 hcd = pci_get_drvdata(dev);
320 if (hcd->state != HC_STATE_SUSPENDED) {
321 dev_dbg (hcd->self.controller,
322 "can't resume, not suspended!\n");
323 return 0;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David Brownell21b18612005-11-23 15:45:42 -0800326#ifdef CONFIG_PPC_PMAC
327 /* Reenable ASIC clocks for USB */
328 if (_machine == _MACH_Pmac) {
329 struct device_node *of_node;
330
331 of_node = pci_device_to_OF_node (dev);
332 if (of_node)
333 pmac_call_feature (PMAC_FTR_USB_ENABLE,
334 of_node, 0, 1);
335 }
336#endif
337
David Brownellc6053ec2005-04-18 17:39:22 -0700338 /* NOTE: chip docs cover clean "real suspend" cases (what Linux
339 * calls "standby", "suspend to RAM", and so on). There are also
340 * dirty cases when swsusp fakes a suspend in "shutdown" mode.
341 */
342 if (dev->current_state != PCI_D0) {
343#ifdef DEBUG
344 int pci_pm;
345 u16 pmcr;
346
347 pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
348 pci_read_config_word(dev, pci_pm + PCI_PM_CTRL, &pmcr);
349 pmcr &= PCI_PM_CTRL_STATE_MASK;
350 if (pmcr) {
351 /* Clean case: power to USB and to HC registers was
352 * maintained; remote wakeup is easy.
353 */
354 dev_dbg(hcd->self.controller, "resume from PCI D%d\n",
355 pmcr);
356 } else {
357 /* Clean: HC lost Vcc power, D0 uninitialized
358 * + Vaux may have preserved port and transceiver
359 * state ... for remote wakeup from D3cold
360 * + or not; HCD must reinit + re-enumerate
361 *
362 * Dirty: D0 semi-initialized cases with swsusp
363 * + after BIOS init
364 * + after Linux init (HCD statically linked)
365 */
366 dev_dbg(hcd->self.controller,
367 "PCI D0, from previous PCI D%d\n",
368 dev->current_state);
369 }
370#endif
David Brownell5f827ea2005-09-22 22:38:16 -0700371 /* yes, ignore these results too... */
372 (void) pci_enable_wake (dev, dev->current_state, 0);
373 (void) pci_enable_wake (dev, PCI_D3cold, 0);
David Brownellc6053ec2005-04-18 17:39:22 -0700374 } else {
375 /* Same basic cases: clean (powered/not), dirty */
376 dev_dbg(hcd->self.controller, "PCI legacy resume\n");
377 }
378
379 /* NOTE: the PCI API itself is asymmetric here. We don't need to
380 * pci_set_power_state(PCI_D0) since that's part of re-enabling;
381 * but that won't re-enable bus mastering. Yet pci_disable_device()
382 * explicitly disables bus mastering...
383 */
384 retval = pci_enable_device (dev);
385 if (retval < 0) {
386 dev_err (hcd->self.controller,
387 "can't re-enable after resume, %d!\n", retval);
388 return retval;
389 }
390 pci_set_master (dev);
391 pci_restore_state (dev);
392
393 dev->dev.power.power_state = PMSG_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David Brownellc6053ec2005-04-18 17:39:22 -0700395 hcd->saw_irq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Brownell5f827ea2005-09-22 22:38:16 -0700397 if (hcd->driver->resume) {
398 retval = hcd->driver->resume(hcd);
399 if (retval) {
400 dev_err (hcd->self.controller,
401 "PCI post-resume error %d!\n", retval);
402 usb_hc_died (hcd);
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 }
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return retval;
407}
408EXPORT_SYMBOL (usb_hcd_pci_resume);
409
410#endif /* CONFIG_PM */
411
412