bcma: fix typo - reading number of slave wrappers
[linux-2.6-block.git] / drivers / bcma / main.c
CommitLineData
8369ae33
RM
1/*
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"
200351c7 9#include <linux/module.h>
8369ae33 10#include <linux/bcma/bcma.h>
eef72699 11#include <linux/slab.h>
8369ae33
RM
12
13MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
14MODULE_LICENSE("GPL");
15
8f9ada4f
HM
16/* contains the number the next bus should get. */
17static unsigned int bcma_bus_next_num = 0;
18
19/* bcma_buses_mutex locks the bcma_bus_next_num */
20static DEFINE_MUTEX(bcma_buses_mutex);
21
8369ae33
RM
22static int bcma_bus_match(struct device *dev, struct device_driver *drv);
23static int bcma_device_probe(struct device *dev);
24static int bcma_device_remove(struct device *dev);
886b66ef 25static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
8369ae33
RM
26
27static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
28{
29 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
30 return sprintf(buf, "0x%03X\n", core->id.manuf);
31}
32static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
33{
34 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
35 return sprintf(buf, "0x%03X\n", core->id.id);
36}
37static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
38{
39 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
40 return sprintf(buf, "0x%02X\n", core->id.rev);
41}
42static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
43{
44 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
45 return sprintf(buf, "0x%X\n", core->id.class);
46}
47static struct device_attribute bcma_device_attrs[] = {
48 __ATTR_RO(manuf),
49 __ATTR_RO(id),
50 __ATTR_RO(rev),
51 __ATTR_RO(class),
52 __ATTR_NULL,
53};
54
55static struct bus_type bcma_bus_type = {
56 .name = "bcma",
57 .match = bcma_bus_match,
58 .probe = bcma_device_probe,
59 .remove = bcma_device_remove,
886b66ef 60 .uevent = bcma_device_uevent,
8369ae33
RM
61 .dev_attrs = bcma_device_attrs,
62};
63
6d5cfc9f
RM
64static u16 bcma_cc_core_id(struct bcma_bus *bus)
65{
66 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
67 return BCMA_CORE_4706_CHIPCOMMON;
68 return BCMA_CORE_CHIPCOMMON;
69}
70
1c9351cf 71struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
8369ae33
RM
72{
73 struct bcma_device *core;
74
75 list_for_each_entry(core, &bus->cores, list) {
76 if (core->id.id == coreid)
77 return core;
78 }
79 return NULL;
80}
1c9351cf 81EXPORT_SYMBOL_GPL(bcma_find_core);
8369ae33
RM
82
83static void bcma_release_core_dev(struct device *dev)
84{
85 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
ecd177c2
HM
86 if (core->io_addr)
87 iounmap(core->io_addr);
88 if (core->io_wrap)
89 iounmap(core->io_wrap);
8369ae33
RM
90 kfree(core);
91}
92
93static int bcma_register_cores(struct bcma_bus *bus)
94{
95 struct bcma_device *core;
96 int err, dev_id = 0;
97
98 list_for_each_entry(core, &bus->cores, list) {
99 /* We support that cores ourself */
100 switch (core->id.id) {
6d5cfc9f 101 case BCMA_CORE_4706_CHIPCOMMON:
8369ae33
RM
102 case BCMA_CORE_CHIPCOMMON:
103 case BCMA_CORE_PCI:
104 case BCMA_CORE_PCIE:
21e0534a 105 case BCMA_CORE_MIPS_74K:
8369ae33
RM
106 continue;
107 }
108
109 core->dev.release = bcma_release_core_dev;
110 core->dev.bus = &bcma_bus_type;
8f9ada4f 111 dev_set_name(&core->dev, "bcma%d:%d", bus->num, dev_id);
8369ae33
RM
112
113 switch (bus->hosttype) {
114 case BCMA_HOSTTYPE_PCI:
115 core->dev.parent = &bus->host_pci->dev;
1bdcd095
RM
116 core->dma_dev = &bus->host_pci->dev;
117 core->irq = bus->host_pci->irq;
8369ae33 118 break;
ecd177c2
HM
119 case BCMA_HOSTTYPE_SOC:
120 core->dev.dma_mask = &core->dev.coherent_dma_mask;
121 core->dma_dev = &core->dev;
122 break;
8369ae33
RM
123 case BCMA_HOSTTYPE_SDIO:
124 break;
125 }
126
127 err = device_register(&core->dev);
128 if (err) {
3d9d8af3
RM
129 bcma_err(bus,
130 "Could not register dev for core 0x%03X\n",
131 core->id.id);
8369ae33
RM
132 continue;
133 }
134 core->dev_registered = true;
135 dev_id++;
136 }
137
138 return 0;
139}
140
141static void bcma_unregister_cores(struct bcma_bus *bus)
142{
143 struct bcma_device *core;
144
145 list_for_each_entry(core, &bus->cores, list) {
146 if (core->dev_registered)
147 device_unregister(&core->dev);
148 }
149}
150
d1a7a8e1 151int __devinit bcma_bus_register(struct bcma_bus *bus)
8369ae33
RM
152{
153 int err;
154 struct bcma_device *core;
155
8f9ada4f
HM
156 mutex_lock(&bcma_buses_mutex);
157 bus->num = bcma_bus_next_num++;
158 mutex_unlock(&bcma_buses_mutex);
159
8369ae33
RM
160 /* Scan for devices (cores) */
161 err = bcma_bus_scan(bus);
162 if (err) {
3d9d8af3 163 bcma_err(bus, "Failed to scan: %d\n", err);
8369ae33
RM
164 return -1;
165 }
166
167 /* Init CC core */
6d5cfc9f 168 core = bcma_find_core(bus, bcma_cc_core_id(bus));
8369ae33
RM
169 if (core) {
170 bus->drv_cc.core = core;
171 bcma_core_chipcommon_init(&bus->drv_cc);
172 }
173
21e0534a
HM
174 /* Init MIPS core */
175 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
176 if (core) {
177 bus->drv_mips.core = core;
178 bcma_core_mips_init(&bus->drv_mips);
179 }
180
8369ae33
RM
181 /* Init PCIE core */
182 core = bcma_find_core(bus, BCMA_CORE_PCIE);
183 if (core) {
184 bus->drv_pci.core = core;
185 bcma_core_pci_init(&bus->drv_pci);
186 }
187
27f18dc2
RM
188 /* Try to get SPROM */
189 err = bcma_sprom_get(bus);
534e7a45 190 if (err == -ENOENT) {
3d9d8af3 191 bcma_err(bus, "No SPROM available\n");
2e6b4119 192 } else if (err)
3d9d8af3 193 bcma_err(bus, "Failed to get SPROM: %d\n", err);
27f18dc2 194
8369ae33
RM
195 /* Register found cores */
196 bcma_register_cores(bus);
197
3d9d8af3 198 bcma_info(bus, "Bus registered\n");
8369ae33
RM
199
200 return 0;
201}
8369ae33
RM
202
203void bcma_bus_unregister(struct bcma_bus *bus)
204{
205 bcma_unregister_cores(bus);
206}
8369ae33 207
517f43e5
HM
208int __init bcma_bus_early_register(struct bcma_bus *bus,
209 struct bcma_device *core_cc,
210 struct bcma_device *core_mips)
211{
212 int err;
213 struct bcma_device *core;
214 struct bcma_device_id match;
215
216 bcma_init_bus(bus);
217
218 match.manuf = BCMA_MANUF_BCM;
6d5cfc9f 219 match.id = bcma_cc_core_id(bus);
517f43e5
HM
220 match.class = BCMA_CL_SIM;
221 match.rev = BCMA_ANY_REV;
222
223 /* Scan for chip common core */
224 err = bcma_bus_scan_early(bus, &match, core_cc);
225 if (err) {
3d9d8af3 226 bcma_err(bus, "Failed to scan for common core: %d\n", err);
517f43e5
HM
227 return -1;
228 }
229
230 match.manuf = BCMA_MANUF_MIPS;
231 match.id = BCMA_CORE_MIPS_74K;
232 match.class = BCMA_CL_SIM;
233 match.rev = BCMA_ANY_REV;
234
235 /* Scan for mips core */
236 err = bcma_bus_scan_early(bus, &match, core_mips);
237 if (err) {
3d9d8af3 238 bcma_err(bus, "Failed to scan for mips core: %d\n", err);
517f43e5
HM
239 return -1;
240 }
241
242 /* Init CC core */
6d5cfc9f 243 core = bcma_find_core(bus, bcma_cc_core_id(bus));
517f43e5
HM
244 if (core) {
245 bus->drv_cc.core = core;
246 bcma_core_chipcommon_init(&bus->drv_cc);
247 }
248
21e0534a
HM
249 /* Init MIPS core */
250 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
251 if (core) {
252 bus->drv_mips.core = core;
253 bcma_core_mips_init(&bus->drv_mips);
254 }
255
3d9d8af3 256 bcma_info(bus, "Early bus registered\n");
517f43e5
HM
257
258 return 0;
259}
260
775ab521 261#ifdef CONFIG_PM
685a4ef0
LT
262int bcma_bus_suspend(struct bcma_bus *bus)
263{
7d5869e7
LT
264 struct bcma_device *core;
265
266 list_for_each_entry(core, &bus->cores, list) {
267 struct device_driver *drv = core->dev.driver;
268 if (drv) {
269 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
270 if (adrv->suspend)
271 adrv->suspend(core);
272 }
273 }
685a4ef0
LT
274 return 0;
275}
276
775ab521
RM
277int bcma_bus_resume(struct bcma_bus *bus)
278{
279 struct bcma_device *core;
280
281 /* Init CC core */
6d5cfc9f 282 if (bus->drv_cc.core) {
775ab521
RM
283 bus->drv_cc.setup_done = false;
284 bcma_core_chipcommon_init(&bus->drv_cc);
285 }
286
7d5869e7
LT
287 list_for_each_entry(core, &bus->cores, list) {
288 struct device_driver *drv = core->dev.driver;
289 if (drv) {
290 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
291 if (adrv->resume)
292 adrv->resume(core);
293 }
294 }
295
775ab521
RM
296 return 0;
297}
298#endif
299
8369ae33
RM
300int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
301{
302 drv->drv.name = drv->name;
303 drv->drv.bus = &bcma_bus_type;
304 drv->drv.owner = owner;
305
306 return driver_register(&drv->drv);
307}
308EXPORT_SYMBOL_GPL(__bcma_driver_register);
309
310void bcma_driver_unregister(struct bcma_driver *drv)
311{
312 driver_unregister(&drv->drv);
313}
314EXPORT_SYMBOL_GPL(bcma_driver_unregister);
315
316static int bcma_bus_match(struct device *dev, struct device_driver *drv)
317{
318 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
319 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
320 const struct bcma_device_id *cid = &core->id;
321 const struct bcma_device_id *did;
322
323 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
324 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
325 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
326 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
327 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
328 return 1;
329 }
330 return 0;
331}
332
333static int bcma_device_probe(struct device *dev)
334{
335 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
336 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
337 drv);
338 int err = 0;
339
340 if (adrv->probe)
341 err = adrv->probe(core);
342
343 return err;
344}
345
346static int bcma_device_remove(struct device *dev)
347{
348 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
349 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
350 drv);
351
352 if (adrv->remove)
353 adrv->remove(core);
354
355 return 0;
356}
357
886b66ef
DW
358static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
359{
360 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
361
362 return add_uevent_var(env,
363 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
364 core->id.manuf, core->id.id,
365 core->id.rev, core->id.class);
366}
367
8369ae33
RM
368static int __init bcma_modinit(void)
369{
370 int err;
371
372 err = bus_register(&bcma_bus_type);
373 if (err)
374 return err;
375
376#ifdef CONFIG_BCMA_HOST_PCI
377 err = bcma_host_pci_init();
378 if (err) {
379 pr_err("PCI host initialization failed\n");
380 err = 0;
381 }
382#endif
383
384 return err;
385}
386fs_initcall(bcma_modinit);
387
388static void __exit bcma_modexit(void)
389{
390#ifdef CONFIG_BCMA_HOST_PCI
391 bcma_host_pci_exit();
392#endif
393 bus_unregister(&bcma_bus_type);
394}
395module_exit(bcma_modexit)