blob: 151bddc57e16a2c9a107023d91000025320f3c3b [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"
Paul Gortmaker200351c2011-07-01 16:06:37 -04009#include <linux/module.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020010#include <linux/bcma/bcma.h>
Geert Uytterhoeveneef72692011-06-26 10:19:44 +020011#include <linux/slab.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020012
13MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
14MODULE_LICENSE("GPL");
15
Hauke Mehrtens8f9ada42012-01-31 00:03:36 +010016/* 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +020022static 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);
David Woodhouse886b66e2011-08-19 22:14:47 +020025static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020026
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,
David Woodhouse886b66e2011-08-19 22:14:47 +020060 .uevent = bcma_device_uevent,
Rafał Miłecki8369ae32011-05-09 18:56:46 +020061 .dev_attrs = bcma_device_attrs,
62};
63
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +020064static 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
Hauke Mehrtens1c9351c2012-02-28 00:56:09 +010071struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
Rafał Miłecki8369ae32011-05-09 18:56:46 +020072{
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}
Hauke Mehrtens1c9351c2012-02-28 00:56:09 +010081EXPORT_SYMBOL_GPL(bcma_find_core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020082
83static void bcma_release_core_dev(struct device *dev)
84{
85 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
Hauke Mehrtensecd177c2011-07-23 01:20:08 +020086 if (core->io_addr)
87 iounmap(core->io_addr);
88 if (core->io_wrap)
89 iounmap(core->io_wrap);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020090 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) {
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200101 case BCMA_CORE_4706_CHIPCOMMON:
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200102 case BCMA_CORE_CHIPCOMMON:
103 case BCMA_CORE_PCI:
104 case BCMA_CORE_PCIE:
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200105 case BCMA_CORE_MIPS_74K:
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200106 continue;
107 }
108
109 core->dev.release = bcma_release_core_dev;
110 core->dev.bus = &bcma_bus_type;
Hauke Mehrtens8f9ada42012-01-31 00:03:36 +0100111 dev_set_name(&core->dev, "bcma%d:%d", bus->num, dev_id);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200112
113 switch (bus->hosttype) {
114 case BCMA_HOSTTYPE_PCI:
115 core->dev.parent = &bus->host_pci->dev;
Rafał Miłecki1bdcd092011-05-18 11:40:22 +0200116 core->dma_dev = &bus->host_pci->dev;
117 core->irq = bus->host_pci->irq;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200118 break;
Hauke Mehrtensecd177c2011-07-23 01:20:08 +0200119 case BCMA_HOSTTYPE_SOC:
120 core->dev.dma_mask = &core->dev.coherent_dma_mask;
121 core->dma_dev = &core->dev;
122 break;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200123 case BCMA_HOSTTYPE_SDIO:
124 break;
125 }
126
127 err = device_register(&core->dev);
128 if (err) {
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200129 bcma_err(bus,
130 "Could not register dev for core 0x%03X\n",
131 core->id.id);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200132 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
Hauke Mehrtensd1a7a8e2012-01-31 00:03:34 +0100151int __devinit bcma_bus_register(struct bcma_bus *bus)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200152{
153 int err;
154 struct bcma_device *core;
155
Hauke Mehrtens8f9ada42012-01-31 00:03:36 +0100156 mutex_lock(&bcma_buses_mutex);
157 bus->num = bcma_bus_next_num++;
158 mutex_unlock(&bcma_buses_mutex);
159
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200160 /* Scan for devices (cores) */
161 err = bcma_bus_scan(bus);
162 if (err) {
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200163 bcma_err(bus, "Failed to scan: %d\n", err);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200164 return -1;
165 }
166
167 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200168 core = bcma_find_core(bus, bcma_cc_core_id(bus));
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200169 if (core) {
170 bus->drv_cc.core = core;
171 bcma_core_chipcommon_init(&bus->drv_cc);
172 }
173
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200174 /* 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200181 /* 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
Rafał Miłecki27f18dc2011-06-02 02:08:51 +0200188 /* Try to get SPROM */
189 err = bcma_sprom_get(bus);
Hauke Mehrtens534e7a42011-07-09 13:22:03 +0200190 if (err == -ENOENT) {
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200191 bcma_err(bus, "No SPROM available\n");
Henrik Rydberg2e6b4112012-01-31 14:22:15 -0500192 } else if (err)
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200193 bcma_err(bus, "Failed to get SPROM: %d\n", err);
Rafał Miłecki27f18dc2011-06-02 02:08:51 +0200194
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200195 /* Register found cores */
196 bcma_register_cores(bus);
197
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200198 bcma_info(bus, "Bus registered\n");
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200199
200 return 0;
201}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200202
203void bcma_bus_unregister(struct bcma_bus *bus)
204{
205 bcma_unregister_cores(bus);
206}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200207
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200208int __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;
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200219 match.id = bcma_cc_core_id(bus);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200220 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) {
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200226 bcma_err(bus, "Failed to scan for common core: %d\n", err);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200227 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) {
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200238 bcma_err(bus, "Failed to scan for mips core: %d\n", err);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200239 return -1;
240 }
241
242 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200243 core = bcma_find_core(bus, bcma_cc_core_id(bus));
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200244 if (core) {
245 bus->drv_cc.core = core;
246 bcma_core_chipcommon_init(&bus->drv_cc);
247 }
248
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200249 /* 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
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200256 bcma_info(bus, "Early bus registered\n");
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200257
258 return 0;
259}
260
Rafał Miłecki775ab522011-12-09 22:16:07 +0100261#ifdef CONFIG_PM
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100262int bcma_bus_suspend(struct bcma_bus *bus)
263{
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100264 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 }
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100274 return 0;
275}
276
Rafał Miłecki775ab522011-12-09 22:16:07 +0100277int bcma_bus_resume(struct bcma_bus *bus)
278{
279 struct bcma_device *core;
280
281 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200282 if (bus->drv_cc.core) {
Rafał Miłecki775ab522011-12-09 22:16:07 +0100283 bus->drv_cc.setup_done = false;
284 bcma_core_chipcommon_init(&bus->drv_cc);
285 }
286
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100287 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
Rafał Miłecki775ab522011-12-09 22:16:07 +0100296 return 0;
297}
298#endif
299
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200300int __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
David Woodhouse886b66e2011-08-19 22:14:47 +0200358static 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200368static 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)