2 * Broadcom specific AMBA
5 * Licensed under the GNU/GPL. See COPYING for details.
8 #include "bcma_private.h"
9 #include <linux/module.h>
10 #include <linux/bcma/bcma.h>
11 #include <linux/slab.h>
13 MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
14 MODULE_LICENSE("GPL");
16 /* contains the number the next bus should get. */
17 static unsigned int bcma_bus_next_num = 0;
19 /* bcma_buses_mutex locks the bcma_bus_next_num */
20 static DEFINE_MUTEX(bcma_buses_mutex);
22 static int bcma_bus_match(struct device *dev, struct device_driver *drv);
23 static int bcma_device_probe(struct device *dev);
24 static int bcma_device_remove(struct device *dev);
25 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
27 static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
29 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
30 return sprintf(buf, "0x%03X\n", core->id.manuf);
32 static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
34 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
35 return sprintf(buf, "0x%03X\n", core->id.id);
37 static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
39 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
40 return sprintf(buf, "0x%02X\n", core->id.rev);
42 static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
44 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
45 return sprintf(buf, "0x%X\n", core->id.class);
47 static struct device_attribute bcma_device_attrs[] = {
55 static struct bus_type bcma_bus_type = {
57 .match = bcma_bus_match,
58 .probe = bcma_device_probe,
59 .remove = bcma_device_remove,
60 .uevent = bcma_device_uevent,
61 .dev_attrs = bcma_device_attrs,
64 static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
66 struct bcma_device *core;
68 list_for_each_entry(core, &bus->cores, list) {
69 if (core->id.id == coreid)
75 static void bcma_release_core_dev(struct device *dev)
77 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
79 iounmap(core->io_addr);
81 iounmap(core->io_wrap);
85 static int bcma_register_cores(struct bcma_bus *bus)
87 struct bcma_device *core;
90 list_for_each_entry(core, &bus->cores, list) {
91 /* We support that cores ourself */
92 switch (core->id.id) {
93 case BCMA_CORE_CHIPCOMMON:
96 case BCMA_CORE_MIPS_74K:
100 core->dev.release = bcma_release_core_dev;
101 core->dev.bus = &bcma_bus_type;
102 dev_set_name(&core->dev, "bcma%d:%d", bus->num, dev_id);
104 switch (bus->hosttype) {
105 case BCMA_HOSTTYPE_PCI:
106 core->dev.parent = &bus->host_pci->dev;
107 core->dma_dev = &bus->host_pci->dev;
108 core->irq = bus->host_pci->irq;
110 case BCMA_HOSTTYPE_SOC:
111 core->dev.dma_mask = &core->dev.coherent_dma_mask;
112 core->dma_dev = &core->dev;
114 case BCMA_HOSTTYPE_SDIO:
118 err = device_register(&core->dev);
120 pr_err("Could not register dev for core 0x%03X\n",
124 core->dev_registered = true;
131 static void bcma_unregister_cores(struct bcma_bus *bus)
133 struct bcma_device *core;
135 list_for_each_entry(core, &bus->cores, list) {
136 if (core->dev_registered)
137 device_unregister(&core->dev);
141 int __devinit bcma_bus_register(struct bcma_bus *bus)
144 struct bcma_device *core;
146 mutex_lock(&bcma_buses_mutex);
147 bus->num = bcma_bus_next_num++;
148 mutex_unlock(&bcma_buses_mutex);
150 /* Scan for devices (cores) */
151 err = bcma_bus_scan(bus);
153 pr_err("Failed to scan: %d\n", err);
158 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
160 bus->drv_cc.core = core;
161 bcma_core_chipcommon_init(&bus->drv_cc);
165 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
167 bus->drv_mips.core = core;
168 bcma_core_mips_init(&bus->drv_mips);
172 core = bcma_find_core(bus, BCMA_CORE_PCIE);
174 bus->drv_pci.core = core;
175 bcma_core_pci_init(&bus->drv_pci);
178 /* Try to get SPROM */
179 err = bcma_sprom_get(bus);
180 if (err == -ENOENT) {
181 pr_err("No SPROM available\n");
183 pr_err("Failed to get SPROM: %d\n", err);
185 /* Register found cores */
186 bcma_register_cores(bus);
188 pr_info("Bus registered\n");
193 void bcma_bus_unregister(struct bcma_bus *bus)
195 bcma_unregister_cores(bus);
198 int __init bcma_bus_early_register(struct bcma_bus *bus,
199 struct bcma_device *core_cc,
200 struct bcma_device *core_mips)
203 struct bcma_device *core;
204 struct bcma_device_id match;
208 match.manuf = BCMA_MANUF_BCM;
209 match.id = BCMA_CORE_CHIPCOMMON;
210 match.class = BCMA_CL_SIM;
211 match.rev = BCMA_ANY_REV;
213 /* Scan for chip common core */
214 err = bcma_bus_scan_early(bus, &match, core_cc);
216 pr_err("Failed to scan for common core: %d\n", err);
220 match.manuf = BCMA_MANUF_MIPS;
221 match.id = BCMA_CORE_MIPS_74K;
222 match.class = BCMA_CL_SIM;
223 match.rev = BCMA_ANY_REV;
225 /* Scan for mips core */
226 err = bcma_bus_scan_early(bus, &match, core_mips);
228 pr_err("Failed to scan for mips core: %d\n", err);
233 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
235 bus->drv_cc.core = core;
236 bcma_core_chipcommon_init(&bus->drv_cc);
240 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
242 bus->drv_mips.core = core;
243 bcma_core_mips_init(&bus->drv_mips);
246 pr_info("Early bus registered\n");
252 int bcma_bus_suspend(struct bcma_bus *bus)
254 struct bcma_device *core;
256 list_for_each_entry(core, &bus->cores, list) {
257 struct device_driver *drv = core->dev.driver;
259 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
267 int bcma_bus_resume(struct bcma_bus *bus)
269 struct bcma_device *core;
272 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
274 bus->drv_cc.setup_done = false;
275 bcma_core_chipcommon_init(&bus->drv_cc);
278 list_for_each_entry(core, &bus->cores, list) {
279 struct device_driver *drv = core->dev.driver;
281 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
291 int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
293 drv->drv.name = drv->name;
294 drv->drv.bus = &bcma_bus_type;
295 drv->drv.owner = owner;
297 return driver_register(&drv->drv);
299 EXPORT_SYMBOL_GPL(__bcma_driver_register);
301 void bcma_driver_unregister(struct bcma_driver *drv)
303 driver_unregister(&drv->drv);
305 EXPORT_SYMBOL_GPL(bcma_driver_unregister);
307 static int bcma_bus_match(struct device *dev, struct device_driver *drv)
309 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
310 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
311 const struct bcma_device_id *cid = &core->id;
312 const struct bcma_device_id *did;
314 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
315 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
316 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
317 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
318 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
324 static int bcma_device_probe(struct device *dev)
326 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
327 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
332 err = adrv->probe(core);
337 static int bcma_device_remove(struct device *dev)
339 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
340 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
349 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
351 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
353 return add_uevent_var(env,
354 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
355 core->id.manuf, core->id.id,
356 core->id.rev, core->id.class);
359 static int __init bcma_modinit(void)
363 err = bus_register(&bcma_bus_type);
367 #ifdef CONFIG_BCMA_HOST_PCI
368 err = bcma_host_pci_init();
370 pr_err("PCI host initialization failed\n");
377 fs_initcall(bcma_modinit);
379 static void __exit bcma_modexit(void)
381 #ifdef CONFIG_BCMA_HOST_PCI
382 bcma_host_pci_exit();
384 bus_unregister(&bcma_bus_type);
386 module_exit(bcma_modexit)