Merge tag 'v3.10.76' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / bcma / host_pci.c
CommitLineData
8369ae33
RM
1/*
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"
ba7328b2 9#include <linux/slab.h>
8369ae33
RM
10#include <linux/bcma/bcma.h>
11#include <linux/pci.h>
200351c7 12#include <linux/module.h>
8369ae33
RM
13
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;
3d9d8af3 21 bcma_debug(core->bus, "Switched to core: 0x%X\n", core->id.id);
8369ae33
RM
22}
23
439678f8
RM
24/* 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)
8369ae33 27{
439678f8
RM
28 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
8369ae33
RM
35 if (core->bus->mapped_core != core)
36 bcma_host_pci_switch_core(core);
439678f8
RM
37 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);
8369ae33
RM
43 return ioread8(core->bus->mmio + offset);
44}
45
46static u16 bcma_host_pci_read16(struct bcma_device *core, u16 offset)
47{
439678f8 48 offset += bcma_host_pci_provide_access_to_core(core);
8369ae33
RM
49 return ioread16(core->bus->mmio + offset);
50}
51
52static u32 bcma_host_pci_read32(struct bcma_device *core, u16 offset)
53{
439678f8 54 offset += bcma_host_pci_provide_access_to_core(core);
8369ae33
RM
55 return ioread32(core->bus->mmio + offset);
56}
57
58static void bcma_host_pci_write8(struct bcma_device *core, u16 offset,
59 u8 value)
60{
439678f8 61 offset += bcma_host_pci_provide_access_to_core(core);
8369ae33
RM
62 iowrite8(value, core->bus->mmio + offset);
63}
64
65static void bcma_host_pci_write16(struct bcma_device *core, u16 offset,
66 u16 value)
67{
439678f8 68 offset += bcma_host_pci_provide_access_to_core(core);
8369ae33
RM
69 iowrite16(value, core->bus->mmio + offset);
70}
71
72static void bcma_host_pci_write32(struct bcma_device *core, u16 offset,
73 u32 value)
74{
439678f8 75 offset += bcma_host_pci_provide_access_to_core(core);
8369ae33
RM
76 iowrite32(value, core->bus->mmio + offset);
77}
78
9d75ef0f 79#ifdef CONFIG_BCMA_BLOCKIO
94f3457f
HM
80static void bcma_host_pci_block_read(struct bcma_device *core, void *buffer,
81 size_t count, u16 offset, u8 reg_width)
9d75ef0f
RM
82{
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
94f3457f
HM
103static void bcma_host_pci_block_write(struct bcma_device *core,
104 const void *buffer, size_t count,
105 u16 offset, u8 reg_width)
9d75ef0f
RM
106{
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
8369ae33
RM
128static 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
94f3457f 143static const struct bcma_host_ops bcma_host_pci_ops = {
8369ae33
RM
144 .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,
9d75ef0f
RM
150#ifdef CONFIG_BCMA_BLOCKIO
151 .block_read = bcma_host_pci_block_read,
152 .block_write = bcma_host_pci_block_write,
153#endif
8369ae33
RM
154 .aread32 = bcma_host_pci_aread32,
155 .awrite32 = bcma_host_pci_awrite32,
156};
157
0f58a01d
GKH
158static int bcma_host_pci_probe(struct pci_dev *dev,
159 const struct pci_device_id *id)
8369ae33
RM
160{
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? */
191 if (!pci_is_pcie(dev))
3d9d8af3 192 bcma_err(bus, "PCI card detected, report problems.\n");
8369ae33
RM
193
194 /* Map MMIO */
195 err = -ENOMEM;
196 bus->mmio = pci_iomap(dev, 0, ~0UL);
197 if (!bus->mmio)
198 goto err_pci_release_regions;
199
200 /* Host specific */
201 bus->host_pci = dev;
202 bus->hosttype = BCMA_HOSTTYPE_PCI;
203 bus->ops = &bcma_host_pci_ops;
204
0a2fcaa7
HM
205 bus->boardinfo.vendor = bus->host_pci->subsystem_vendor;
206 bus->boardinfo.type = bus->host_pci->subsystem_device;
207
8369ae33
RM
208 /* Register */
209 err = bcma_bus_register(bus);
210 if (err)
211 goto err_pci_unmap_mmio;
212
213 pci_set_drvdata(dev, bus);
214
215out:
216 return err;
217
218err_pci_unmap_mmio:
219 pci_iounmap(dev, bus->mmio);
220err_pci_release_regions:
221 pci_release_regions(dev);
222err_pci_disable:
223 pci_disable_device(dev);
224err_kfree_bus:
225 kfree(bus);
226 return err;
227}
228
0f58a01d 229static void bcma_host_pci_remove(struct pci_dev *dev)
8369ae33
RM
230{
231 struct bcma_bus *bus = pci_get_drvdata(dev);
232
233 bcma_bus_unregister(bus);
234 pci_iounmap(dev, bus->mmio);
235 pci_release_regions(dev);
236 pci_disable_device(dev);
237 kfree(bus);
238 pci_set_drvdata(dev, NULL);
239}
240
ccd60953 241#ifdef CONFIG_PM_SLEEP
5d2031f2 242static int bcma_host_pci_suspend(struct device *dev)
775ab521 243{
5d2031f2
LT
244 struct pci_dev *pdev = to_pci_dev(dev);
245 struct bcma_bus *bus = pci_get_drvdata(pdev);
775ab521 246
28e7d218 247 bus->mapped_core = NULL;
5d2031f2 248
685a4ef0 249 return bcma_bus_suspend(bus);
775ab521
RM
250}
251
5d2031f2 252static int bcma_host_pci_resume(struct device *dev)
775ab521 253{
5d2031f2
LT
254 struct pci_dev *pdev = to_pci_dev(dev);
255 struct bcma_bus *bus = pci_get_drvdata(pdev);
775ab521 256
5d2031f2
LT
257 return bcma_bus_resume(bus);
258}
775ab521 259
5d2031f2
LT
260static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bcma_host_pci_suspend,
261 bcma_host_pci_resume);
262#define BCMA_PM_OPS (&bcma_pm_ops)
775ab521 263
ccd60953 264#else /* CONFIG_PM_SLEEP */
5d2031f2
LT
265
266#define BCMA_PM_OPS NULL
267
ccd60953 268#endif /* CONFIG_PM_SLEEP */
775ab521 269
8369ae33 270static DEFINE_PCI_DEVICE_TABLE(bcma_pci_bridge_tbl) = {
9594b56d 271 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
02817be0 272 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43224) },
8369ae33
RM
273 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4331) },
274 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4353) },
91fa4b0a 275 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4357) },
646e0827 276 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4358) },
c263c2c1 277 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4359) },
8369ae33
RM
278 { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4727) },
279 { 0, },
280};
281MODULE_DEVICE_TABLE(pci, bcma_pci_bridge_tbl);
282
283static struct pci_driver bcma_pci_bridge_driver = {
284 .name = "bcma-pci-bridge",
285 .id_table = bcma_pci_bridge_tbl,
286 .probe = bcma_host_pci_probe,
0f58a01d 287 .remove = bcma_host_pci_remove,
5d2031f2 288 .driver.pm = BCMA_PM_OPS,
8369ae33
RM
289};
290
291int __init bcma_host_pci_init(void)
292{
293 return pci_register_driver(&bcma_pci_bridge_driver);
294}
295
296void __exit bcma_host_pci_exit(void)
297{
298 pci_unregister_driver(&bcma_pci_bridge_driver);
299}