blob: 410215c169208f1735ed3df0e76189bab8c980c8 [file] [log] [blame]
Michael Buesch61e115a2007-09-18 15:12:50 -04001/*
2 * Sonics Silicon Backplane
3 * PCI Hostdevice wrapper
4 *
5 * Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>
6 * Copyright (c) 2005 Stefano Brivio <st3@riseup.net>
7 * Copyright (c) 2005 Danny van Dyk <kugelfang@gentoo.org>
8 * Copyright (c) 2005 Andreas Jaggi <andreas.jaggi@waterwave.ch>
Michael Büscheb032b92011-07-04 20:50:05 +02009 * Copyright (c) 2005-2007 Michael Buesch <m@bues.ch>
Michael Buesch61e115a2007-09-18 15:12:50 -040010 *
11 * Licensed under the GNU/GPL. See COPYING for details.
12 */
13
Andrey Skvortsov55803732014-12-04 11:32:46 -050014#include <linux/pm.h>
Michael Buesch61e115a2007-09-18 15:12:50 -040015#include <linux/pci.h>
Paul Gortmaker1014c222011-07-27 22:07:02 -040016#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Michael Buesch61e115a2007-09-18 15:12:50 -040018#include <linux/ssb/ssb.h>
19
20
Andrey Skvortsov55803732014-12-04 11:32:46 -050021#ifdef CONFIG_PM_SLEEP
22static int ssb_pcihost_suspend(struct device *d)
Michael Buesch61e115a2007-09-18 15:12:50 -040023{
Andrey Skvortsov55803732014-12-04 11:32:46 -050024 struct pci_dev *dev = to_pci_dev(d);
Michael Buesch8fe2b652008-03-30 00:10:50 +010025 struct ssb_bus *ssb = pci_get_drvdata(dev);
26 int err;
27
28 err = ssb_bus_suspend(ssb);
29 if (err)
30 return err;
Michael Buesch61e115a2007-09-18 15:12:50 -040031 pci_save_state(dev);
32 pci_disable_device(dev);
Andrey Skvortsov55803732014-12-04 11:32:46 -050033
34 /* if there is a wakeup enabled child device on ssb bus,
35 enable pci wakeup posibility. */
36 device_set_wakeup_enable(d, d->power.wakeup_path);
37
38 pci_prepare_to_sleep(dev);
Michael Buesch61e115a2007-09-18 15:12:50 -040039
40 return 0;
41}
42
Andrey Skvortsov55803732014-12-04 11:32:46 -050043static int ssb_pcihost_resume(struct device *d)
Michael Buesch61e115a2007-09-18 15:12:50 -040044{
Andrey Skvortsov55803732014-12-04 11:32:46 -050045 struct pci_dev *dev = to_pci_dev(d);
Michael Buesch8fe2b652008-03-30 00:10:50 +010046 struct ssb_bus *ssb = pci_get_drvdata(dev);
Michael Buesch61e115a2007-09-18 15:12:50 -040047 int err;
48
Andrey Skvortsov55803732014-12-04 11:32:46 -050049 pci_back_from_sleep(dev);
Michael Buesch61e115a2007-09-18 15:12:50 -040050 err = pci_enable_device(dev);
51 if (err)
52 return err;
53 pci_restore_state(dev);
Michael Buesch8fe2b652008-03-30 00:10:50 +010054 err = ssb_bus_resume(ssb);
55 if (err)
56 return err;
Michael Buesch61e115a2007-09-18 15:12:50 -040057
58 return 0;
59}
Andrey Skvortsov55803732014-12-04 11:32:46 -050060
61static const struct dev_pm_ops ssb_pcihost_pm_ops = {
62 SET_SYSTEM_SLEEP_PM_OPS(ssb_pcihost_suspend, ssb_pcihost_resume)
63};
64
65#endif /* CONFIG_PM_SLEEP */
Michael Buesch61e115a2007-09-18 15:12:50 -040066
Greg Kroah-Hartman163247c2012-12-21 15:10:52 -080067static int ssb_pcihost_probe(struct pci_dev *dev,
68 const struct pci_device_id *id)
Michael Buesch61e115a2007-09-18 15:12:50 -040069{
70 struct ssb_bus *ssb;
71 int err = -ENOMEM;
72 const char *name;
Larry Fingere0816852010-10-20 09:59:33 -050073 u32 val;
Michael Buesch61e115a2007-09-18 15:12:50 -040074
75 ssb = kzalloc(sizeof(*ssb), GFP_KERNEL);
76 if (!ssb)
77 goto out;
78 err = pci_enable_device(dev);
79 if (err)
80 goto err_kfree_ssb;
Kay Sieversb7b05fe2008-10-30 15:51:57 +010081 name = dev_name(&dev->dev);
Michael Buesch61e115a2007-09-18 15:12:50 -040082 if (dev->driver && dev->driver->name)
83 name = dev->driver->name;
84 err = pci_request_regions(dev, name);
85 if (err)
86 goto err_pci_disable;
87 pci_set_master(dev);
88
Larry Fingere0816852010-10-20 09:59:33 -050089 /* Disable the RETRY_TIMEOUT register (0x41) to keep
90 * PCI Tx retries from interfering with C3 CPU state */
91 pci_read_config_dword(dev, 0x40, &val);
92 if ((val & 0x0000ff00) != 0)
93 pci_write_config_dword(dev, 0x40, val & 0xffff00ff);
94
Michael Buesch61e115a2007-09-18 15:12:50 -040095 err = ssb_bus_pcibus_register(ssb, dev);
96 if (err)
97 goto err_pci_release_regions;
98
99 pci_set_drvdata(dev, ssb);
100
101out:
102 return err;
103
104err_pci_release_regions:
105 pci_release_regions(dev);
106err_pci_disable:
107 pci_disable_device(dev);
108err_kfree_ssb:
109 kfree(ssb);
110 return err;
111}
112
113static void ssb_pcihost_remove(struct pci_dev *dev)
114{
115 struct ssb_bus *ssb = pci_get_drvdata(dev);
116
117 ssb_bus_unregister(ssb);
118 pci_release_regions(dev);
119 pci_disable_device(dev);
120 kfree(ssb);
121 pci_set_drvdata(dev, NULL);
122}
123
Greg Kroah-Hartman163247c2012-12-21 15:10:52 -0800124int ssb_pcihost_register(struct pci_driver *driver)
Michael Buesch61e115a2007-09-18 15:12:50 -0400125{
126 driver->probe = ssb_pcihost_probe;
127 driver->remove = ssb_pcihost_remove;
Andrey Skvortsov55803732014-12-04 11:32:46 -0500128#ifdef CONFIG_PM_SLEEP
129 driver->driver.pm = &ssb_pcihost_pm_ops;
130#endif
Michael Buesch61e115a2007-09-18 15:12:50 -0400131
132 return pci_register_driver(driver);
133}
134EXPORT_SYMBOL(ssb_pcihost_register);