blob: 1e5ac0a796968b61e3de79a4ae2a82f9c2339916 [file] [log] [blame]
Rafał Miłecki8369ae32011-05-09 18:56:46 +02001/*
2 * Broadcom specific AMBA
3 * PCI Host
4 *
5 * Licensed under the GNU/GPL. See COPYING for details.
6 */
7
8#include "bcma_private.h"
Andrew Mortonba7328b2011-05-26 16:24:57 -07009#include <linux/slab.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020010#include <linux/bcma/bcma.h>
11#include <linux/pci.h>
Paul Gortmaker200351c2011-07-01 16:06:37 -040012#include <linux/module.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020013
14static void bcma_host_pci_switch_core(struct bcma_device *core)
15{
16 pci_write_config_dword(core->bus->host_pci, BCMA_PCI_BAR0_WIN,
17 core->addr);
18 pci_write_config_dword(core->bus->host_pci, BCMA_PCI_BAR0_WIN2,
19 core->wrap);
20 core->bus->mapped_core = core;
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +020021 bcma_debug(core->bus, "Switched to core: 0x%X\n", core->id.id);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020022}
23
Rafał Miłecki439678f2011-12-05 19:13:39 +010024/* Provides access to the requested core. Returns base offset that has to be
25 * used. It makes use of fixed windows when possible. */
26static u16 bcma_host_pci_provide_access_to_core(struct bcma_device *core)
Rafał Miłecki8369ae32011-05-09 18:56:46 +020027{
Rafał Miłecki439678f2011-12-05 19:13:39 +010028 switch (core->id.id) {
29 case BCMA_CORE_CHIPCOMMON:
30 return 3 * BCMA_CORE_SIZE;
31 case BCMA_CORE_PCIE:
32 return 2 * BCMA_CORE_SIZE;
33 }
34
Rafał Miłecki8369ae32011-05-09 18:56:46 +020035 if (core->bus->mapped_core != core)
36 bcma_host_pci_switch_core(core);
Rafał Miłecki439678f2011-12-05 19:13:39 +010037 return 0;
38}
39
40static u8 bcma_host_pci_read8(struct bcma_device *core, u16 offset)
41{
42 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020043 return ioread8(core->bus->mmio + offset);
44}
45
46static u16 bcma_host_pci_read16(struct bcma_device *core, u16 offset)
47{
Rafał Miłecki439678f2011-12-05 19:13:39 +010048 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020049 return ioread16(core->bus->mmio + offset);
50}
51
52static u32 bcma_host_pci_read32(struct bcma_device *core, u16 offset)
53{
Rafał Miłecki439678f2011-12-05 19:13:39 +010054 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020055 return ioread32(core->bus->mmio + offset);
56}
57
58static void bcma_host_pci_write8(struct bcma_device *core, u16 offset,
59 u8 value)
60{
Rafał Miłecki439678f2011-12-05 19:13:39 +010061 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020062 iowrite8(value, core->bus->mmio + offset);
63}
64
65static void bcma_host_pci_write16(struct bcma_device *core, u16 offset,
66 u16 value)
67{
Rafał Miłecki439678f2011-12-05 19:13:39 +010068 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020069 iowrite16(value, core->bus->mmio + offset);
70}
71
72static void bcma_host_pci_write32(struct bcma_device *core, u16 offset,
73 u32 value)
74{
Rafał Miłecki439678f2011-12-05 19:13:39 +010075 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020076 iowrite32(value, core->bus->mmio + offset);
77}
78
Rafał Miłecki9d75ef02011-05-20 03:27:06 +020079#ifdef CONFIG_BCMA_BLOCKIO
Hauke Mehrtens94f34572012-08-05 16:54:41 +020080static void bcma_host_pci_block_read(struct bcma_device *core, void *buffer,
81 size_t count, u16 offset, u8 reg_width)
Rafał Miłecki9d75ef02011-05-20 03:27:06 +020082{
83 void __iomem *addr = core->bus->mmio + offset;
84 if (core->bus->mapped_core != core)
85 bcma_host_pci_switch_core(core);
86 switch (reg_width) {
87 case sizeof(u8):
88 ioread8_rep(addr, buffer, count);
89 break;
90 case sizeof(u16):
91 WARN_ON(count & 1);
92 ioread16_rep(addr, buffer, count >> 1);
93 break;
94 case sizeof(u32):
95 WARN_ON(count & 3);
96 ioread32_rep(addr, buffer, count >> 2);
97 break;
98 default:
99 WARN_ON(1);
100 }
101}
102
Hauke Mehrtens94f34572012-08-05 16:54:41 +0200103static void bcma_host_pci_block_write(struct bcma_device *core,
104 const void *buffer, size_t count,
105 u16 offset, u8 reg_width)
Rafał Miłecki9d75ef02011-05-20 03:27:06 +0200106{
107 void __iomem *addr = core->bus->mmio + offset;
108 if (core->bus->mapped_core != core)
109 bcma_host_pci_switch_core(core);
110 switch (reg_width) {
111 case sizeof(u8):
112 iowrite8_rep(addr, buffer, count);
113 break;
114 case sizeof(u16):
115 WARN_ON(count & 1);
116 iowrite16_rep(addr, buffer, count >> 1);
117 break;
118 case sizeof(u32):
119 WARN_ON(count & 3);
120 iowrite32_rep(addr, buffer, count >> 2);
121 break;
122 default:
123 WARN_ON(1);
124 }
125}
126#endif
127
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200128static u32 bcma_host_pci_aread32(struct bcma_device *core, u16 offset)
129{
130 if (core->bus->mapped_core != core)
131 bcma_host_pci_switch_core(core);
132 return ioread32(core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
133}
134
135static void bcma_host_pci_awrite32(struct bcma_device *core, u16 offset,
136 u32 value)
137{
138 if (core->bus->mapped_core != core)
139 bcma_host_pci_switch_core(core);
140 iowrite32(value, core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
141}
142
Hauke Mehrtens94f34572012-08-05 16:54:41 +0200143static const struct bcma_host_ops bcma_host_pci_ops = {
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200144 .read8 = bcma_host_pci_read8,
145 .read16 = bcma_host_pci_read16,
146 .read32 = bcma_host_pci_read32,
147 .write8 = bcma_host_pci_write8,
148 .write16 = bcma_host_pci_write16,
149 .write32 = bcma_host_pci_write32,
Rafał Miłecki9d75ef02011-05-20 03:27:06 +0200150#ifdef CONFIG_BCMA_BLOCKIO
151 .block_read = bcma_host_pci_block_read,
152 .block_write = bcma_host_pci_block_write,
153#endif
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200154 .aread32 = bcma_host_pci_aread32,
155 .awrite32 = bcma_host_pci_awrite32,
156};
157
Greg Kroah-Hartman0f58a012012-12-21 15:12:59 -0800158static int bcma_host_pci_probe(struct pci_dev *dev,
159 const struct pci_device_id *id)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200160{
161 struct bcma_bus *bus;
162 int err = -ENOMEM;
163 const char *name;
164 u32 val;
165
166 /* Alloc */
167 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
168 if (!bus)
169 goto out;
170
171 /* Basic PCI configuration */
172 err = pci_enable_device(dev);
173 if (err)
174 goto err_kfree_bus;
175
176 name = dev_name(&dev->dev);
177 if (dev->driver && dev->driver->name)
178 name = dev->driver->name;
179 err = pci_request_regions(dev, name);
180 if (err)
181 goto err_pci_disable;
182 pci_set_master(dev);
183
184 /* Disable the RETRY_TIMEOUT register (0x41) to keep
185 * PCI Tx retries from interfering with C3 CPU state */
186 pci_read_config_dword(dev, 0x40, &val);
187 if ((val & 0x0000ff00) != 0)
188 pci_write_config_dword(dev, 0x40, val & 0xffff00ff);
189
190 /* SSB needed additional powering up, do we have any AMBA PCI cards? */
Hauke Mehrtensdfa04152013-10-03 13:49:09 +0200191 if (!pci_is_pcie(dev)) {
192 bcma_err(bus, "PCI card detected, they are not supported.\n");
193 err = -ENXIO;
194 goto err_pci_release_regions;
195 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200196
197 /* Map MMIO */
198 err = -ENOMEM;
199 bus->mmio = pci_iomap(dev, 0, ~0UL);
200 if (!bus->mmio)
201 goto err_pci_release_regions;
202
203 /* Host specific */
204 bus->host_pci = dev;
205 bus->hosttype = BCMA_HOSTTYPE_PCI;
206 bus->ops = &bcma_host_pci_ops;
207
Hauke Mehrtens0a2fcaa2012-04-29 02:04:08 +0200208 bus->boardinfo.vendor = bus->host_pci->subsystem_vendor;
209 bus->boardinfo.type = bus->host_pci->subsystem_device;
210
Rafał Miłeckidc8ecdd2014-09-01 23:11:06 +0200211 /* Initialize struct, detect chip */
212 bcma_init_bus(bus);
213
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200214 /* Register */
215 err = bcma_bus_register(bus);
216 if (err)
217 goto err_pci_unmap_mmio;
218
219 pci_set_drvdata(dev, bus);
220
221out:
222 return err;
223
224err_pci_unmap_mmio:
225 pci_iounmap(dev, bus->mmio);
226err_pci_release_regions:
227 pci_release_regions(dev);
228err_pci_disable:
229 pci_disable_device(dev);
230err_kfree_bus:
231 kfree(bus);
232 return err;
233}
234
Greg Kroah-Hartman0f58a012012-12-21 15:12:59 -0800235static void bcma_host_pci_remove(struct pci_dev *dev)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200236{
237 struct bcma_bus *bus = pci_get_drvdata(dev);
238
239 bcma_bus_unregister(bus);
240 pci_iounmap(dev, bus->mmio);
241 pci_release_regions(dev);
242 pci_disable_device(dev);
243 kfree(bus);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200244}
245
Yuanhan Liuccd60952012-10-16 22:59:02 +0800246#ifdef CONFIG_PM_SLEEP
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100247static int bcma_host_pci_suspend(struct device *dev)
Rafał Miłecki775ab522011-12-09 22:16:07 +0100248{
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100249 struct pci_dev *pdev = to_pci_dev(dev);
250 struct bcma_bus *bus = pci_get_drvdata(pdev);
Rafał Miłecki775ab522011-12-09 22:16:07 +0100251
Rafał Miłecki28e7d212012-01-13 23:58:38 +0100252 bus->mapped_core = NULL;
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100253
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100254 return bcma_bus_suspend(bus);
Rafał Miłecki775ab522011-12-09 22:16:07 +0100255}
256
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100257static int bcma_host_pci_resume(struct device *dev)
Rafał Miłecki775ab522011-12-09 22:16:07 +0100258{
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100259 struct pci_dev *pdev = to_pci_dev(dev);
260 struct bcma_bus *bus = pci_get_drvdata(pdev);
Rafał Miłecki775ab522011-12-09 22:16:07 +0100261
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100262 return bcma_bus_resume(bus);
Rafał Miłecki775ab522011-12-09 22:16:07 +0100263}
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100264
265static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bcma_host_pci_suspend,
266 bcma_host_pci_resume);
267#define BCMA_PM_OPS (&bcma_pm_ops)
268
Yuanhan Liuccd60952012-10-16 22:59:02 +0800269#else /* CONFIG_PM_SLEEP */
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100270
271#define BCMA_PM_OPS NULL
272
Yuanhan Liuccd60952012-10-16 22:59:02 +0800273#endif /* CONFIG_PM_SLEEP */
Rafał Miłecki775ab522011-12-09 22:16:07 +0100274
Jingoo Han342a11e2013-12-03 08:00:27 +0900275static const struct pci_device_id bcma_pci_bridge_tbl[] = {
Rafał Miłecki9594b562011-05-14 10:31:46 +0200276 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
Hauke Mehrtensb9f54bd2013-10-03 13:49:10 +0200277 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4313) },
Hauke Mehrtens02817be2012-06-30 01:44:40 +0200278 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43224) },
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200279 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) },
280 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) },
Rafał Miłecki91fa4b02011-06-17 13:15:23 +0200281 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) },
Rafał Miłecki646e0822012-09-21 08:38:38 +0200282 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4358) },
Rafał Miłeckic263c2c2012-07-23 18:20:12 +0200283 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4359) },
Rafał Miłecki88f9b652013-06-26 10:02:11 +0200284 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4365) },
Rafał Miłeckid1d37992014-07-15 19:44:28 +0200285 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43a9) },
Rafał Miłecki27cfdb02014-07-24 15:29:19 +0200286 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43aa) },
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200287 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) },
Hauke Mehrtensed96c032014-08-23 17:58:11 +0200288 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43227) }, /* 0xA8DB */
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200289 { 0, },
290};
291MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl);
292
293static struct pci_driver bcma_pci_bridge_driver = {
294 .name = "bcma-pci-bridge",
295 .id_table = bcma_pci_bridge_tbl,
296 .probe = bcma_host_pci_probe,
Greg Kroah-Hartman0f58a012012-12-21 15:12:59 -0800297 .remove = bcma_host_pci_remove,
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100298 .driver.pm = BCMA_PM_OPS,
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200299};
300
301int __init bcma_host_pci_init(void)
302{
303 return pci_register_driver(&bcma_pci_bridge_driver);
304}
305
306void __exit bcma_host_pci_exit(void)
307{
308 pci_unregister_driver(&bcma_pci_bridge_driver);
309}