Merge branch 'for-linus' of git://github.com/chrismason/linux
[linux/fpc-iii.git] / drivers / bcma / main.c
blob73b7b1a18fab6466db1ca9aa3f53a6bd4a420ca5
1 /*
2 * Broadcom specific AMBA
3 * Bus subsystem
5 * Licensed under the GNU/GPL. See COPYING for details.
6 */
8 #include "bcma_private.h"
9 #include <linux/bcma/bcma.h>
10 #include <linux/slab.h>
12 MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
13 MODULE_LICENSE("GPL");
15 static int bcma_bus_match(struct device *dev, struct device_driver *drv);
16 static int bcma_device_probe(struct device *dev);
17 static int bcma_device_remove(struct device *dev);
18 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
20 static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
22 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
23 return sprintf(buf, "0x%03X\n", core->id.manuf);
25 static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
27 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
28 return sprintf(buf, "0x%03X\n", core->id.id);
30 static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
32 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
33 return sprintf(buf, "0x%02X\n", core->id.rev);
35 static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
37 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
38 return sprintf(buf, "0x%X\n", core->id.class);
40 static struct device_attribute bcma_device_attrs[] = {
41 __ATTR_RO(manuf),
42 __ATTR_RO(id),
43 __ATTR_RO(rev),
44 __ATTR_RO(class),
45 __ATTR_NULL,
48 static struct bus_type bcma_bus_type = {
49 .name = "bcma",
50 .match = bcma_bus_match,
51 .probe = bcma_device_probe,
52 .remove = bcma_device_remove,
53 .uevent = bcma_device_uevent,
54 .dev_attrs = bcma_device_attrs,
57 static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
59 struct bcma_device *core;
61 list_for_each_entry(core, &bus->cores, list) {
62 if (core->id.id == coreid)
63 return core;
65 return NULL;
68 static void bcma_release_core_dev(struct device *dev)
70 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
71 kfree(core);
74 static int bcma_register_cores(struct bcma_bus *bus)
76 struct bcma_device *core;
77 int err, dev_id = 0;
79 list_for_each_entry(core, &bus->cores, list) {
80 /* We support that cores ourself */
81 switch (core->id.id) {
82 case BCMA_CORE_CHIPCOMMON:
83 case BCMA_CORE_PCI:
84 case BCMA_CORE_PCIE:
85 continue;
88 core->dev.release = bcma_release_core_dev;
89 core->dev.bus = &bcma_bus_type;
90 dev_set_name(&core->dev, "bcma%d:%d", 0/*bus->num*/, dev_id);
92 switch (bus->hosttype) {
93 case BCMA_HOSTTYPE_PCI:
94 core->dev.parent = &bus->host_pci->dev;
95 core->dma_dev = &bus->host_pci->dev;
96 core->irq = bus->host_pci->irq;
97 break;
98 case BCMA_HOSTTYPE_NONE:
99 case BCMA_HOSTTYPE_SDIO:
100 break;
103 err = device_register(&core->dev);
104 if (err) {
105 pr_err("Could not register dev for core 0x%03X\n",
106 core->id.id);
107 continue;
109 core->dev_registered = true;
110 dev_id++;
113 return 0;
116 static void bcma_unregister_cores(struct bcma_bus *bus)
118 struct bcma_device *core;
120 list_for_each_entry(core, &bus->cores, list) {
121 if (core->dev_registered)
122 device_unregister(&core->dev);
126 int bcma_bus_register(struct bcma_bus *bus)
128 int err;
129 struct bcma_device *core;
131 /* Scan for devices (cores) */
132 err = bcma_bus_scan(bus);
133 if (err) {
134 pr_err("Failed to scan: %d\n", err);
135 return -1;
138 /* Init CC core */
139 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
140 if (core) {
141 bus->drv_cc.core = core;
142 bcma_core_chipcommon_init(&bus->drv_cc);
145 /* Init PCIE core */
146 core = bcma_find_core(bus, BCMA_CORE_PCIE);
147 if (core) {
148 bus->drv_pci.core = core;
149 bcma_core_pci_init(&bus->drv_pci);
152 /* Try to get SPROM */
153 err = bcma_sprom_get(bus);
154 if (err == -ENOENT) {
155 pr_err("No SPROM available\n");
156 } else if (err) {
157 pr_err("Failed to get SPROM: %d\n", err);
158 return -ENOENT;
161 /* Register found cores */
162 bcma_register_cores(bus);
164 pr_info("Bus registered\n");
166 return 0;
169 void bcma_bus_unregister(struct bcma_bus *bus)
171 bcma_unregister_cores(bus);
174 int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
176 drv->drv.name = drv->name;
177 drv->drv.bus = &bcma_bus_type;
178 drv->drv.owner = owner;
180 return driver_register(&drv->drv);
182 EXPORT_SYMBOL_GPL(__bcma_driver_register);
184 void bcma_driver_unregister(struct bcma_driver *drv)
186 driver_unregister(&drv->drv);
188 EXPORT_SYMBOL_GPL(bcma_driver_unregister);
190 static int bcma_bus_match(struct device *dev, struct device_driver *drv)
192 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
193 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
194 const struct bcma_device_id *cid = &core->id;
195 const struct bcma_device_id *did;
197 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
198 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
199 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
200 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
201 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
202 return 1;
204 return 0;
207 static int bcma_device_probe(struct device *dev)
209 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
210 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
211 drv);
212 int err = 0;
214 if (adrv->probe)
215 err = adrv->probe(core);
217 return err;
220 static int bcma_device_remove(struct device *dev)
222 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
223 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
224 drv);
226 if (adrv->remove)
227 adrv->remove(core);
229 return 0;
232 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
234 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
236 return add_uevent_var(env,
237 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
238 core->id.manuf, core->id.id,
239 core->id.rev, core->id.class);
242 static int __init bcma_modinit(void)
244 int err;
246 err = bus_register(&bcma_bus_type);
247 if (err)
248 return err;
250 #ifdef CONFIG_BCMA_HOST_PCI
251 err = bcma_host_pci_init();
252 if (err) {
253 pr_err("PCI host initialization failed\n");
254 err = 0;
256 #endif
258 return err;
260 fs_initcall(bcma_modinit);
262 static void __exit bcma_modexit(void)
264 #ifdef CONFIG_BCMA_HOST_PCI
265 bcma_host_pci_exit();
266 #endif
267 bus_unregister(&bcma_bus_type);
269 module_exit(bcma_modexit)