blob: 69c10a7b7c617b0155637c42e53821693445b826 [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{
Rafał Miłecki8be08a32015-01-25 13:41:19 +010016 int win2 = core->bus->host_is_pcie2 ?
17 BCMA_PCIE2_BAR0_WIN2 : BCMA_PCI_BAR0_WIN2;
18
Rafał Miłecki8369ae32011-05-09 18:56:46 +020019 pci_write_config_dword(core->bus->host_pci, BCMA_PCI_BAR0_WIN,
20 core->addr);
Rafał Miłecki8be08a32015-01-25 13:41:19 +010021 pci_write_config_dword(core->bus->host_pci, win2, core->wrap);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020022 core->bus->mapped_core = core;
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +020023 bcma_debug(core->bus, "Switched to core: 0x%X\n", core->id.id);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020024}
25
Rafał Miłecki439678f2011-12-05 19:13:39 +010026/* Provides access to the requested core. Returns base offset that has to be
27 * used. It makes use of fixed windows when possible. */
28static u16 bcma_host_pci_provide_access_to_core(struct bcma_device *core)
Rafał Miłecki8369ae32011-05-09 18:56:46 +020029{
Rafał Miłecki439678f2011-12-05 19:13:39 +010030 switch (core->id.id) {
31 case BCMA_CORE_CHIPCOMMON:
32 return 3 * BCMA_CORE_SIZE;
33 case BCMA_CORE_PCIE:
34 return 2 * BCMA_CORE_SIZE;
35 }
36
Rafał Miłecki8369ae32011-05-09 18:56:46 +020037 if (core->bus->mapped_core != core)
38 bcma_host_pci_switch_core(core);
Rafał Miłecki439678f2011-12-05 19:13:39 +010039 return 0;
40}
41
42static u8 bcma_host_pci_read8(struct bcma_device *core, u16 offset)
43{
44 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020045 return ioread8(core->bus->mmio + offset);
46}
47
48static u16 bcma_host_pci_read16(struct bcma_device *core, u16 offset)
49{
Rafał Miłecki439678f2011-12-05 19:13:39 +010050 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020051 return ioread16(core->bus->mmio + offset);
52}
53
54static u32 bcma_host_pci_read32(struct bcma_device *core, u16 offset)
55{
Rafał Miłecki439678f2011-12-05 19:13:39 +010056 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020057 return ioread32(core->bus->mmio + offset);
58}
59
60static void bcma_host_pci_write8(struct bcma_device *core, u16 offset,
61 u8 value)
62{
Rafał Miłecki439678f2011-12-05 19:13:39 +010063 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020064 iowrite8(value, core->bus->mmio + offset);
65}
66
67static void bcma_host_pci_write16(struct bcma_device *core, u16 offset,
68 u16 value)
69{
Rafał Miłecki439678f2011-12-05 19:13:39 +010070 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020071 iowrite16(value, core->bus->mmio + offset);
72}
73
74static void bcma_host_pci_write32(struct bcma_device *core, u16 offset,
75 u32 value)
76{
Rafał Miłecki439678f2011-12-05 19:13:39 +010077 offset += bcma_host_pci_provide_access_to_core(core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020078 iowrite32(value, core->bus->mmio + offset);
79}
80
Rafał Miłecki9d75ef02011-05-20 03:27:06 +020081#ifdef CONFIG_BCMA_BLOCKIO
Hauke Mehrtens94f34572012-08-05 16:54:41 +020082static void bcma_host_pci_block_read(struct bcma_device *core, void *buffer,
83 size_t count, u16 offset, u8 reg_width)
Rafał Miłecki9d75ef02011-05-20 03:27:06 +020084{
85 void __iomem *addr = core->bus->mmio + offset;
86 if (core->bus->mapped_core != core)
87 bcma_host_pci_switch_core(core);
88 switch (reg_width) {
89 case sizeof(u8):
90 ioread8_rep(addr, buffer, count);
91 break;
92 case sizeof(u16):
93 WARN_ON(count & 1);
94 ioread16_rep(addr, buffer, count >> 1);
95 break;
96 case sizeof(u32):
97 WARN_ON(count & 3);
98 ioread32_rep(addr, buffer, count >> 2);
99 break;
100 default:
101 WARN_ON(1);
102 }
103}
104
Hauke Mehrtens94f34572012-08-05 16:54:41 +0200105static void bcma_host_pci_block_write(struct bcma_device *core,
106 const void *buffer, size_t count,
107 u16 offset, u8 reg_width)
Rafał Miłecki9d75ef02011-05-20 03:27:06 +0200108{
109 void __iomem *addr = core->bus->mmio + offset;
110 if (core->bus->mapped_core != core)
111 bcma_host_pci_switch_core(core);
112 switch (reg_width) {
113 case sizeof(u8):
114 iowrite8_rep(addr, buffer, count);
115 break;
116 case sizeof(u16):
117 WARN_ON(count & 1);
118 iowrite16_rep(addr, buffer, count >> 1);
119 break;
120 case sizeof(u32):
121 WARN_ON(count & 3);
122 iowrite32_rep(addr, buffer, count >> 2);
123 break;
124 default:
125 WARN_ON(1);
126 }
127}
128#endif
129
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200130static u32 bcma_host_pci_aread32(struct bcma_device *core, u16 offset)
131{
132 if (core->bus->mapped_core != core)
133 bcma_host_pci_switch_core(core);
134 return ioread32(core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
135}
136
137static void bcma_host_pci_awrite32(struct bcma_device *core, u16 offset,
138 u32 value)
139{
140 if (core->bus->mapped_core != core)
141 bcma_host_pci_switch_core(core);
142 iowrite32(value, core->bus->mmio + (1 * BCMA_CORE_SIZE) + offset);
143}
144
Hauke Mehrtens94f34572012-08-05 16:54:41 +0200145static const struct bcma_host_ops bcma_host_pci_ops = {
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200146 .read8 = bcma_host_pci_read8,
147 .read16 = bcma_host_pci_read16,
148 .read32 = bcma_host_pci_read32,
149 .write8 = bcma_host_pci_write8,
150 .write16 = bcma_host_pci_write16,
151 .write32 = bcma_host_pci_write32,
Rafał Miłecki9d75ef02011-05-20 03:27:06 +0200152#ifdef CONFIG_BCMA_BLOCKIO
153 .block_read = bcma_host_pci_block_read,
154 .block_write = bcma_host_pci_block_write,
155#endif
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200156 .aread32 = bcma_host_pci_aread32,
157 .awrite32 = bcma_host_pci_awrite32,
158};
159
Greg Kroah-Hartman0f58a012012-12-21 15:12:59 -0800160static int bcma_host_pci_probe(struct pci_dev *dev,
161 const struct pci_device_id *id)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200162{
163 struct bcma_bus *bus;
164 int err = -ENOMEM;
165 const char *name;
166 u32 val;
167
168 /* Alloc */
169 bus = kzalloc(sizeof(*bus), GFP_KERNEL);
170 if (!bus)
171 goto out;
172
173 /* Basic PCI configuration */
174 err = pci_enable_device(dev);
175 if (err)
176 goto err_kfree_bus;
177
178 name = dev_name(&dev->dev);
179 if (dev->driver && dev->driver->name)
180 name = dev->driver->name;
181 err = pci_request_regions(dev, name);
182 if (err)
183 goto err_pci_disable;
184 pci_set_master(dev);
185
186 /* Disable the RETRY_TIMEOUT register (0x41) to keep
187 * PCI Tx retries from interfering with C3 CPU state */
188 pci_read_config_dword(dev, 0x40, &val);
189 if ((val & 0x0000ff00) != 0)
190 pci_write_config_dword(dev, 0x40, val & 0xffff00ff);
191
192 /* SSB needed additional powering up, do we have any AMBA PCI cards? */
Hauke Mehrtensdfa04152013-10-03 13:49:09 +0200193 if (!pci_is_pcie(dev)) {
194 bcma_err(bus, "PCI card detected, they are not supported.\n");
195 err = -ENXIO;
196 goto err_pci_release_regions;
197 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200198
Rafał Miłecki5a1c18b2019-01-02 00:00:01 +0100199 bus->dev = &dev->dev;
200
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200201 /* Map MMIO */
202 err = -ENOMEM;
203 bus->mmio = pci_iomap(dev, 0, ~0UL);
204 if (!bus->mmio)
205 goto err_pci_release_regions;
206
207 /* Host specific */
208 bus->host_pci = dev;
209 bus->hosttype = BCMA_HOSTTYPE_PCI;
210 bus->ops = &bcma_host_pci_ops;
211
Hauke Mehrtens0a2fcaa2012-04-29 02:04:08 +0200212 bus->boardinfo.vendor = bus->host_pci->subsystem_vendor;
213 bus->boardinfo.type = bus->host_pci->subsystem_device;
214
Rafał Miłeckidc8ecdd2014-09-01 23:11:06 +0200215 /* Initialize struct, detect chip */
216 bcma_init_bus(bus);
217
Rafał Miłecki9b6cc9a2015-02-08 17:11:50 +0100218 /* Scan bus to find out generation of PCIe core */
219 err = bcma_bus_scan(bus);
220 if (err)
221 goto err_pci_unmap_mmio;
222
223 if (bcma_find_core(bus, BCMA_CORE_PCIE2))
224 bus->host_is_pcie2 = true;
225
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200226 /* Register */
227 err = bcma_bus_register(bus);
228 if (err)
Rafał Miłecki9b6cc9a2015-02-08 17:11:50 +0100229 goto err_unregister_cores;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200230
231 pci_set_drvdata(dev, bus);
232
233out:
234 return err;
235
Rafał Miłecki9b6cc9a2015-02-08 17:11:50 +0100236err_unregister_cores:
237 bcma_unregister_cores(bus);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200238err_pci_unmap_mmio:
239 pci_iounmap(dev, bus->mmio);
240err_pci_release_regions:
241 pci_release_regions(dev);
242err_pci_disable:
243 pci_disable_device(dev);
244err_kfree_bus:
245 kfree(bus);
246 return err;
247}
248
Greg Kroah-Hartman0f58a012012-12-21 15:12:59 -0800249static void bcma_host_pci_remove(struct pci_dev *dev)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200250{
251 struct bcma_bus *bus = pci_get_drvdata(dev);
252
253 bcma_bus_unregister(bus);
254 pci_iounmap(dev, bus->mmio);
255 pci_release_regions(dev);
256 pci_disable_device(dev);
257 kfree(bus);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200258}
259
Yuanhan Liuccd60952012-10-16 22:59:02 +0800260#ifdef CONFIG_PM_SLEEP
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100261static int bcma_host_pci_suspend(struct device *dev)
Rafał Miłecki775ab522011-12-09 22:16:07 +0100262{
Chuhong Yuan9236c122019-07-23 16:29:19 +0800263 struct bcma_bus *bus = dev_get_drvdata(dev);
Rafał Miłecki775ab522011-12-09 22:16:07 +0100264
Rafał Miłecki28e7d212012-01-13 23:58:38 +0100265 bus->mapped_core = NULL;
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100266
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100267 return bcma_bus_suspend(bus);
Rafał Miłecki775ab522011-12-09 22:16:07 +0100268}
269
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100270static int bcma_host_pci_resume(struct device *dev)
Rafał Miłecki775ab522011-12-09 22:16:07 +0100271{
Chuhong Yuan9236c122019-07-23 16:29:19 +0800272 struct bcma_bus *bus = dev_get_drvdata(dev);
Rafał Miłecki775ab522011-12-09 22:16:07 +0100273
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100274 return bcma_bus_resume(bus);
Rafał Miłecki775ab522011-12-09 22:16:07 +0100275}
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100276
277static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bcma_host_pci_suspend,
278 bcma_host_pci_resume);
279#define BCMA_PM_OPS (&bcma_pm_ops)
280
Yuanhan Liuccd60952012-10-16 22:59:02 +0800281#else /* CONFIG_PM_SLEEP */
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100282
283#define BCMA_PM_OPS NULL
284
Yuanhan Liuccd60952012-10-16 22:59:02 +0800285#endif /* CONFIG_PM_SLEEP */
Rafał Miłecki775ab522011-12-09 22:16:07 +0100286
Jingoo Han342a11e2013-12-03 08:00:27 +0900287static const struct pci_device_id bcma_pci_bridge_tbl[] = {
Rafał Miłecki9594b562011-05-14 10:31:46 +0200288 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
Hauke Mehrtensb9f54bd2013-10-03 13:49:10 +0200289 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4313) },
Rafał Miłecki34b6d422014-10-15 07:51:44 +0200290 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43224) }, /* 0xa8d8 */
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200291 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) },
292 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) },
Rafał Miłecki91fa4b02011-06-17 13:15:23 +0200293 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) },
Rafał Miłecki646e0822012-09-21 08:38:38 +0200294 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4358) },
Rafał Miłeckic263c2c2012-07-23 18:20:12 +0200295 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4359) },
Rafał Miłecki9b6cc9a2015-02-08 17:11:50 +0100296 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4360) },
Rafał Miłecki515b3992016-01-26 17:02:09 +0100297 { PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, 0x4365, PCI_VENDOR_ID_DELL, 0x0016) },
Jiri Slaby59391a92016-11-28 08:57:12 +0100298 { PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, 0x4365, PCI_VENDOR_ID_DELL, 0x0018) },
Rafał Miłecki1bea0512016-07-11 23:01:36 +0200299 { PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, 0x4365, PCI_VENDOR_ID_FOXCONN, 0xe092) },
Denis 'GNUtoo' Carikli985324a2018-02-08 17:25:42 +0100300 { PCI_DEVICE_SUB(PCI_VENDOR_ID_BROADCOM, 0x4365, PCI_VENDOR_ID_HP, 0x804a) },
Rafał Miłecki9b6cc9a2015-02-08 17:11:50 +0100301 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43a0) },
Rafał Miłeckid1d37992014-07-15 19:44:28 +0200302 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43a9) },
Rafał Miłecki27cfdb02014-07-24 15:29:19 +0200303 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43aa) },
Rafał Miłecki9b6cc9a2015-02-08 17:11:50 +0100304 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x43b1) },
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200305 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) },
Rafał Miłecki34b6d422014-10-15 07:51:44 +0200306 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43227) }, /* 0xa8db, BCM43217 (sic!) */
307 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43228) }, /* 0xa8dc */
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200308 { 0, },
309};
310MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl);
311
312static struct pci_driver bcma_pci_bridge_driver = {
313 .name = "bcma-pci-bridge",
314 .id_table = bcma_pci_bridge_tbl,
315 .probe = bcma_host_pci_probe,
Greg Kroah-Hartman0f58a012012-12-21 15:12:59 -0800316 .remove = bcma_host_pci_remove,
Linus Torvalds5d2031f2012-01-13 23:58:39 +0100317 .driver.pm = BCMA_PM_OPS,
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200318};
319
320int __init bcma_host_pci_init(void)
321{
322 return pci_register_driver(&bcma_pci_bridge_driver);
323}
324
325void __exit bcma_host_pci_exit(void)
326{
327 pci_unregister_driver(&bcma_pci_bridge_driver);
328}
Rafał Miłecki41867212015-02-08 17:11:47 +0100329
330/**************************************************
331 * Runtime ops for drivers.
332 **************************************************/
333
334/* See also pcicore_up */
335void bcma_host_pci_up(struct bcma_bus *bus)
336{
337 if (bus->hosttype != BCMA_HOSTTYPE_PCI)
338 return;
339
340 if (bus->host_is_pcie2)
Rafał Miłecki804e27d2015-02-08 17:11:49 +0100341 bcma_core_pcie2_up(&bus->drv_pcie2);
Rafał Miłecki41867212015-02-08 17:11:47 +0100342 else
343 bcma_core_pci_up(&bus->drv_pci[0]);
344}
345EXPORT_SYMBOL_GPL(bcma_host_pci_up);
346
347/* See also pcicore_down */
348void bcma_host_pci_down(struct bcma_bus *bus)
349{
350 if (bus->hosttype != BCMA_HOSTTYPE_PCI)
351 return;
352
353 if (!bus->host_is_pcie2)
354 bcma_core_pci_down(&bus->drv_pci[0]);
355}
356EXPORT_SYMBOL_GPL(bcma_host_pci_down);
Rafał Miłecki702131e2015-03-05 18:25:10 +0100357
358/* See also si_pci_setup */
359int bcma_host_pci_irq_ctl(struct bcma_bus *bus, struct bcma_device *core,
360 bool enable)
361{
362 struct pci_dev *pdev;
363 u32 coremask, tmp;
364 int err = 0;
365
366 if (bus->hosttype != BCMA_HOSTTYPE_PCI) {
367 /* This bcma device is not on a PCI host-bus. So the IRQs are
368 * not routed through the PCI core.
369 * So we must not enable routing through the PCI core. */
370 goto out;
371 }
372
373 pdev = bus->host_pci;
374
375 err = pci_read_config_dword(pdev, BCMA_PCI_IRQMASK, &tmp);
376 if (err)
377 goto out;
378
379 coremask = BIT(core->core_index) << 8;
380 if (enable)
381 tmp |= coremask;
382 else
383 tmp &= ~coremask;
384
385 err = pci_write_config_dword(pdev, BCMA_PCI_IRQMASK, tmp);
386
387out:
388 return err;
389}
390EXPORT_SYMBOL_GPL(bcma_host_pci_irq_ctl);