blob: 7072216a2a3f02c2d41f426cb6f318a791c622b2 [file] [log] [blame]
Rafał Miłecki8369ae32011-05-09 18:56:46 +02001/*
2 * Broadcom specific AMBA
3 * Bus subsystem
4 *
5 * Licensed under the GNU/GPL. See COPYING for details.
6 */
7
8#include "bcma_private.h"
9#include <linux/bcma/bcma.h>
Geert Uytterhoeveneef72692011-06-26 10:19:44 +020010#include <linux/slab.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020011
12MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
13MODULE_LICENSE("GPL");
14
15static int bcma_bus_match(struct device *dev, struct device_driver *drv);
16static int bcma_device_probe(struct device *dev);
17static int bcma_device_remove(struct device *dev);
18
19static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
20{
21 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
22 return sprintf(buf, "0x%03X\n", core->id.manuf);
23}
24static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
25{
26 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
27 return sprintf(buf, "0x%03X\n", core->id.id);
28}
29static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
30{
31 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
32 return sprintf(buf, "0x%02X\n", core->id.rev);
33}
34static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
35{
36 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
37 return sprintf(buf, "0x%X\n", core->id.class);
38}
39static struct device_attribute bcma_device_attrs[] = {
40 __ATTR_RO(manuf),
41 __ATTR_RO(id),
42 __ATTR_RO(rev),
43 __ATTR_RO(class),
44 __ATTR_NULL,
45};
46
47static struct bus_type bcma_bus_type = {
48 .name = "bcma",
49 .match = bcma_bus_match,
50 .probe = bcma_device_probe,
51 .remove = bcma_device_remove,
52 .dev_attrs = bcma_device_attrs,
53};
54
55static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
56{
57 struct bcma_device *core;
58
59 list_for_each_entry(core, &bus->cores, list) {
60 if (core->id.id == coreid)
61 return core;
62 }
63 return NULL;
64}
65
66static void bcma_release_core_dev(struct device *dev)
67{
68 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
Hauke Mehrtensecd177c2011-07-23 01:20:08 +020069 if (core->io_addr)
70 iounmap(core->io_addr);
71 if (core->io_wrap)
72 iounmap(core->io_wrap);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020073 kfree(core);
74}
75
76static int bcma_register_cores(struct bcma_bus *bus)
77{
78 struct bcma_device *core;
79 int err, dev_id = 0;
80
81 list_for_each_entry(core, &bus->cores, list) {
82 /* We support that cores ourself */
83 switch (core->id.id) {
84 case BCMA_CORE_CHIPCOMMON:
85 case BCMA_CORE_PCI:
86 case BCMA_CORE_PCIE:
Hauke Mehrtens21e05342011-07-23 01:20:09 +020087 case BCMA_CORE_MIPS_74K:
Rafał Miłecki8369ae32011-05-09 18:56:46 +020088 continue;
89 }
90
91 core->dev.release = bcma_release_core_dev;
92 core->dev.bus = &bcma_bus_type;
93 dev_set_name(&core->dev, "bcma%d:%d", 0/*bus->num*/, dev_id);
94
95 switch (bus->hosttype) {
96 case BCMA_HOSTTYPE_PCI:
97 core->dev.parent = &bus->host_pci->dev;
Rafał Miłecki1bdcd092011-05-18 11:40:22 +020098 core->dma_dev = &bus->host_pci->dev;
99 core->irq = bus->host_pci->irq;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200100 break;
Hauke Mehrtensecd177c2011-07-23 01:20:08 +0200101 case BCMA_HOSTTYPE_SOC:
102 core->dev.dma_mask = &core->dev.coherent_dma_mask;
103 core->dma_dev = &core->dev;
104 break;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200105 case BCMA_HOSTTYPE_SDIO:
106 break;
107 }
108
109 err = device_register(&core->dev);
110 if (err) {
111 pr_err("Could not register dev for core 0x%03X\n",
112 core->id.id);
113 continue;
114 }
115 core->dev_registered = true;
116 dev_id++;
117 }
118
119 return 0;
120}
121
122static void bcma_unregister_cores(struct bcma_bus *bus)
123{
124 struct bcma_device *core;
125
126 list_for_each_entry(core, &bus->cores, list) {
127 if (core->dev_registered)
128 device_unregister(&core->dev);
129 }
130}
131
132int bcma_bus_register(struct bcma_bus *bus)
133{
134 int err;
135 struct bcma_device *core;
136
137 /* Scan for devices (cores) */
138 err = bcma_bus_scan(bus);
139 if (err) {
140 pr_err("Failed to scan: %d\n", err);
141 return -1;
142 }
143
144 /* Init CC core */
145 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
146 if (core) {
147 bus->drv_cc.core = core;
148 bcma_core_chipcommon_init(&bus->drv_cc);
149 }
150
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200151 /* Init MIPS core */
152 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
153 if (core) {
154 bus->drv_mips.core = core;
155 bcma_core_mips_init(&bus->drv_mips);
156 }
157
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200158 /* Init PCIE core */
159 core = bcma_find_core(bus, BCMA_CORE_PCIE);
160 if (core) {
161 bus->drv_pci.core = core;
162 bcma_core_pci_init(&bus->drv_pci);
163 }
164
Rafał Miłecki27f18dc2011-06-02 02:08:51 +0200165 /* Try to get SPROM */
166 err = bcma_sprom_get(bus);
Hauke Mehrtens534e7a42011-07-09 13:22:03 +0200167 if (err == -ENOENT) {
168 pr_err("No SPROM available\n");
169 } else if (err) {
Rafał Miłecki27f18dc2011-06-02 02:08:51 +0200170 pr_err("Failed to get SPROM: %d\n", err);
171 return -ENOENT;
172 }
173
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200174 /* Register found cores */
175 bcma_register_cores(bus);
176
177 pr_info("Bus registered\n");
178
179 return 0;
180}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200181
182void bcma_bus_unregister(struct bcma_bus *bus)
183{
184 bcma_unregister_cores(bus);
185}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200186
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200187int __init bcma_bus_early_register(struct bcma_bus *bus,
188 struct bcma_device *core_cc,
189 struct bcma_device *core_mips)
190{
191 int err;
192 struct bcma_device *core;
193 struct bcma_device_id match;
194
195 bcma_init_bus(bus);
196
197 match.manuf = BCMA_MANUF_BCM;
198 match.id = BCMA_CORE_CHIPCOMMON;
199 match.class = BCMA_CL_SIM;
200 match.rev = BCMA_ANY_REV;
201
202 /* Scan for chip common core */
203 err = bcma_bus_scan_early(bus, &match, core_cc);
204 if (err) {
205 pr_err("Failed to scan for common core: %d\n", err);
206 return -1;
207 }
208
209 match.manuf = BCMA_MANUF_MIPS;
210 match.id = BCMA_CORE_MIPS_74K;
211 match.class = BCMA_CL_SIM;
212 match.rev = BCMA_ANY_REV;
213
214 /* Scan for mips core */
215 err = bcma_bus_scan_early(bus, &match, core_mips);
216 if (err) {
217 pr_err("Failed to scan for mips core: %d\n", err);
218 return -1;
219 }
220
221 /* Init CC core */
222 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
223 if (core) {
224 bus->drv_cc.core = core;
225 bcma_core_chipcommon_init(&bus->drv_cc);
226 }
227
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200228 /* Init MIPS core */
229 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
230 if (core) {
231 bus->drv_mips.core = core;
232 bcma_core_mips_init(&bus->drv_mips);
233 }
234
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200235 pr_info("Early bus registered\n");
236
237 return 0;
238}
239
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200240int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
241{
242 drv->drv.name = drv->name;
243 drv->drv.bus = &bcma_bus_type;
244 drv->drv.owner = owner;
245
246 return driver_register(&drv->drv);
247}
248EXPORT_SYMBOL_GPL(__bcma_driver_register);
249
250void bcma_driver_unregister(struct bcma_driver *drv)
251{
252 driver_unregister(&drv->drv);
253}
254EXPORT_SYMBOL_GPL(bcma_driver_unregister);
255
256static int bcma_bus_match(struct device *dev, struct device_driver *drv)
257{
258 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
259 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
260 const struct bcma_device_id *cid = &core->id;
261 const struct bcma_device_id *did;
262
263 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
264 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
265 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
266 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
267 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
268 return 1;
269 }
270 return 0;
271}
272
273static int bcma_device_probe(struct device *dev)
274{
275 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
276 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
277 drv);
278 int err = 0;
279
280 if (adrv->probe)
281 err = adrv->probe(core);
282
283 return err;
284}
285
286static int bcma_device_remove(struct device *dev)
287{
288 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
289 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
290 drv);
291
292 if (adrv->remove)
293 adrv->remove(core);
294
295 return 0;
296}
297
298static int __init bcma_modinit(void)
299{
300 int err;
301
302 err = bus_register(&bcma_bus_type);
303 if (err)
304 return err;
305
306#ifdef CONFIG_BCMA_HOST_PCI
307 err = bcma_host_pci_init();
308 if (err) {
309 pr_err("PCI host initialization failed\n");
310 err = 0;
311 }
312#endif
313
314 return err;
315}
316fs_initcall(bcma_modinit);
317
318static void __exit bcma_modexit(void)
319{
320#ifdef CONFIG_BCMA_HOST_PCI
321 bcma_host_pci_exit();
322#endif
323 bus_unregister(&bcma_bus_type);
324}
325module_exit(bcma_modexit)