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/platform_device.h>
11 #include <linux/bcma/bcma.h>
12 #include <linux/slab.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
17 MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
18 MODULE_LICENSE("GPL");
20 /* contains the number the next bus should get. */
21 static unsigned int bcma_bus_next_num = 0;
23 /* bcma_buses_mutex locks the bcma_bus_next_num */
24 static DEFINE_MUTEX(bcma_buses_mutex);
26 static int bcma_bus_match(struct device *dev, struct device_driver *drv);
27 static int bcma_device_probe(struct device *dev);
28 static int bcma_device_remove(struct device *dev);
29 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
31 static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
33 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
34 return sprintf(buf, "0x%03X\n", core->id.manuf);
36 static DEVICE_ATTR_RO(manuf);
38 static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
40 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
41 return sprintf(buf, "0x%03X\n", core->id.id);
43 static DEVICE_ATTR_RO(id);
45 static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
47 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
48 return sprintf(buf, "0x%02X\n", core->id.rev);
50 static DEVICE_ATTR_RO(rev);
52 static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
54 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
55 return sprintf(buf, "0x%X\n", core->id.class);
57 static DEVICE_ATTR_RO(class);
59 static struct attribute *bcma_device_attrs[] = {
66 ATTRIBUTE_GROUPS(bcma_device);
68 static struct bus_type bcma_bus_type = {
70 .match = bcma_bus_match,
71 .probe = bcma_device_probe,
72 .remove = bcma_device_remove,
73 .uevent = bcma_device_uevent,
74 .dev_groups = bcma_device_groups,
77 static u16 bcma_cc_core_id(struct bcma_bus *bus)
79 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
80 return BCMA_CORE_4706_CHIPCOMMON;
81 return BCMA_CORE_CHIPCOMMON;
84 struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
87 struct bcma_device *core;
89 list_for_each_entry(core, &bus->cores, list) {
90 if (core->id.id == coreid && core->core_unit == unit)
95 EXPORT_SYMBOL_GPL(bcma_find_core_unit);
97 bool bcma_wait_value(struct bcma_device *core, u16 reg, u32 mask, u32 value,
100 unsigned long deadline = jiffies + timeout;
104 val = bcma_read32(core, reg);
105 if ((val & mask) == value)
109 } while (!time_after_eq(jiffies, deadline));
111 bcma_warn(core->bus, "Timeout waiting for register 0x%04X!\n", reg);
116 static void bcma_release_core_dev(struct device *dev)
118 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
120 iounmap(core->io_addr);
122 iounmap(core->io_wrap);
126 static bool bcma_is_core_needed_early(u16 core_id)
129 case BCMA_CORE_NS_NAND:
130 case BCMA_CORE_NS_QSPI:
137 #if defined(CONFIG_OF) && defined(CONFIG_OF_ADDRESS)
138 static struct device_node *bcma_of_find_child_device(struct platform_device *parent,
139 struct bcma_device *core)
141 struct device_node *node;
145 if (!parent || !parent->dev.of_node)
148 for_each_child_of_node(parent->dev.of_node, node) {
149 reg = of_get_address(node, 0, &size, NULL);
152 if (of_translate_address(node, reg) == core->addr)
158 static int bcma_of_irq_parse(struct platform_device *parent,
159 struct bcma_device *core,
160 struct of_phandle_args *out_irq, int num)
165 if (core->dev.of_node) {
166 rc = of_irq_parse_one(core->dev.of_node, num, out_irq);
171 out_irq->np = parent->dev.of_node;
172 out_irq->args_count = 1;
173 out_irq->args[0] = num;
175 laddr[0] = cpu_to_be32(core->addr);
176 return of_irq_parse_raw(laddr, out_irq);
179 static unsigned int bcma_of_get_irq(struct platform_device *parent,
180 struct bcma_device *core, int num)
182 struct of_phandle_args out_irq;
185 if (!parent || !parent->dev.of_node)
188 ret = bcma_of_irq_parse(parent, core, &out_irq, num);
190 bcma_debug(core->bus, "bcma_of_get_irq() failed with rc=%d\n",
195 return irq_create_of_mapping(&out_irq);
198 static void bcma_of_fill_device(struct platform_device *parent,
199 struct bcma_device *core)
201 struct device_node *node;
203 node = bcma_of_find_child_device(parent, core);
205 core->dev.of_node = node;
207 core->irq = bcma_of_get_irq(parent, core, 0);
210 static void bcma_of_fill_device(struct platform_device *parent,
211 struct bcma_device *core)
214 static inline unsigned int bcma_of_get_irq(struct platform_device *parent,
215 struct bcma_device *core, int num)
219 #endif /* CONFIG_OF */
221 unsigned int bcma_core_irq(struct bcma_device *core, int num)
223 struct bcma_bus *bus = core->bus;
224 unsigned int mips_irq;
226 switch (bus->hosttype) {
227 case BCMA_HOSTTYPE_PCI:
228 return bus->host_pci->irq;
229 case BCMA_HOSTTYPE_SOC:
230 if (bus->drv_mips.core && num == 0) {
231 mips_irq = bcma_core_mips_irq(core);
232 return mips_irq <= 4 ? mips_irq + 2 : 0;
235 return bcma_of_get_irq(bus->host_pdev, core, num);
237 case BCMA_HOSTTYPE_SDIO:
243 EXPORT_SYMBOL(bcma_core_irq);
245 void bcma_prepare_core(struct bcma_bus *bus, struct bcma_device *core)
247 core->dev.release = bcma_release_core_dev;
248 core->dev.bus = &bcma_bus_type;
249 dev_set_name(&core->dev, "bcma%d:%d", bus->num, core->core_index);
251 switch (bus->hosttype) {
252 case BCMA_HOSTTYPE_PCI:
253 core->dev.parent = &bus->host_pci->dev;
254 core->dma_dev = &bus->host_pci->dev;
255 core->irq = bus->host_pci->irq;
257 case BCMA_HOSTTYPE_SOC:
258 core->dev.dma_mask = &core->dev.coherent_dma_mask;
259 if (bus->host_pdev) {
260 core->dma_dev = &bus->host_pdev->dev;
261 core->dev.parent = &bus->host_pdev->dev;
262 bcma_of_fill_device(bus->host_pdev, core);
264 core->dma_dev = &core->dev;
267 case BCMA_HOSTTYPE_SDIO:
272 void bcma_init_bus(struct bcma_bus *bus)
274 mutex_lock(&bcma_buses_mutex);
275 bus->num = bcma_bus_next_num++;
276 mutex_unlock(&bcma_buses_mutex);
278 INIT_LIST_HEAD(&bus->cores);
281 bcma_detect_chip(bus);
284 static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core)
288 err = device_register(&core->dev);
290 bcma_err(bus, "Could not register dev for core 0x%03X\n",
292 put_device(&core->dev);
295 core->dev_registered = true;
298 static int bcma_register_devices(struct bcma_bus *bus)
300 struct bcma_device *core;
303 list_for_each_entry(core, &bus->cores, list) {
304 /* We support that cores ourself */
305 switch (core->id.id) {
306 case BCMA_CORE_4706_CHIPCOMMON:
307 case BCMA_CORE_CHIPCOMMON:
308 case BCMA_CORE_NS_CHIPCOMMON_B:
311 case BCMA_CORE_PCIE2:
312 case BCMA_CORE_MIPS_74K:
313 case BCMA_CORE_4706_MAC_GBIT_COMMON:
317 /* Early cores were already registered */
318 if (bcma_is_core_needed_early(core->id.id))
321 /* Only first GMAC core on BCM4706 is connected and working */
322 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
326 bcma_register_core(bus, core);
329 #ifdef CONFIG_BCMA_DRIVER_MIPS
330 if (bus->drv_cc.pflash.present) {
331 err = platform_device_register(&bcma_pflash_dev);
333 bcma_err(bus, "Error registering parallel flash\n");
337 #ifdef CONFIG_BCMA_SFLASH
338 if (bus->drv_cc.sflash.present) {
339 err = platform_device_register(&bcma_sflash_dev);
341 bcma_err(bus, "Error registering serial flash\n");
345 #ifdef CONFIG_BCMA_NFLASH
346 if (bus->drv_cc.nflash.present) {
347 err = platform_device_register(&bcma_nflash_dev);
349 bcma_err(bus, "Error registering NAND flash\n");
352 err = bcma_gpio_init(&bus->drv_cc);
353 if (err == -ENOTSUPP)
354 bcma_debug(bus, "GPIO driver not activated\n");
356 bcma_err(bus, "Error registering GPIO driver: %i\n", err);
358 if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
359 err = bcma_chipco_watchdog_register(&bus->drv_cc);
361 bcma_err(bus, "Error registering watchdog driver\n");
367 void bcma_unregister_cores(struct bcma_bus *bus)
369 struct bcma_device *core, *tmp;
371 list_for_each_entry_safe(core, tmp, &bus->cores, list) {
372 if (!core->dev_registered)
374 list_del(&core->list);
375 device_unregister(&core->dev);
377 if (bus->hosttype == BCMA_HOSTTYPE_SOC)
378 platform_device_unregister(bus->drv_cc.watchdog);
380 /* Now noone uses internally-handled cores, we can free them */
381 list_for_each_entry_safe(core, tmp, &bus->cores, list) {
382 list_del(&core->list);
387 int bcma_bus_register(struct bcma_bus *bus)
390 struct bcma_device *core;
392 /* Scan for devices (cores) */
393 err = bcma_bus_scan(bus);
395 bcma_err(bus, "Failed to scan: %d\n", err);
399 /* Early init CC core */
400 core = bcma_find_core(bus, bcma_cc_core_id(bus));
402 bus->drv_cc.core = core;
403 bcma_core_chipcommon_early_init(&bus->drv_cc);
406 /* Early init PCIE core */
407 core = bcma_find_core(bus, BCMA_CORE_PCIE);
409 bus->drv_pci[0].core = core;
410 bcma_core_pci_early_init(&bus->drv_pci[0]);
413 if (bus->host_pdev) {
414 struct device *dev = &bus->host_pdev->dev;
416 of_platform_populate(dev->of_node, of_default_bus_match_table,
420 /* Cores providing flash access go before SPROM init */
421 list_for_each_entry(core, &bus->cores, list) {
422 if (bcma_is_core_needed_early(core->id.id))
423 bcma_register_core(bus, core);
426 /* Try to get SPROM */
427 err = bcma_sprom_get(bus);
428 if (err == -ENOENT) {
429 bcma_err(bus, "No SPROM available\n");
431 bcma_err(bus, "Failed to get SPROM: %d\n", err);
434 core = bcma_find_core(bus, bcma_cc_core_id(bus));
436 bus->drv_cc.core = core;
437 bcma_core_chipcommon_init(&bus->drv_cc);
441 core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B);
443 bus->drv_cc_b.core = core;
444 bcma_core_chipcommon_b_init(&bus->drv_cc_b);
448 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
450 bus->drv_mips.core = core;
451 bcma_core_mips_init(&bus->drv_mips);
455 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
457 bus->drv_pci[0].core = core;
458 bcma_core_pci_init(&bus->drv_pci[0]);
462 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
464 bus->drv_pci[1].core = core;
465 bcma_core_pci_init(&bus->drv_pci[1]);
468 /* Init PCIe Gen 2 core */
469 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0);
471 bus->drv_pcie2.core = core;
472 bcma_core_pcie2_init(&bus->drv_pcie2);
475 /* Init GBIT MAC COMMON core */
476 core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
478 bus->drv_gmac_cmn.core = core;
479 bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
482 /* Register found cores */
483 bcma_register_devices(bus);
485 bcma_info(bus, "Bus registered\n");
490 void bcma_bus_unregister(struct bcma_bus *bus)
494 err = bcma_gpio_unregister(&bus->drv_cc);
496 bcma_err(bus, "Some GPIOs are still in use.\n");
498 bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
500 bcma_core_chipcommon_b_free(&bus->drv_cc_b);
502 bcma_unregister_cores(bus);
506 * This is a special version of bus registration function designed for SoCs.
507 * It scans bus and performs basic initialization of main cores only.
508 * Please note it requires memory allocation, however it won't try to sleep.
510 int __init bcma_bus_early_register(struct bcma_bus *bus)
513 struct bcma_device *core;
515 /* Scan for devices (cores) */
516 err = bcma_bus_scan(bus);
518 bcma_err(bus, "Failed to scan bus: %d\n", err);
522 /* Early init CC core */
523 core = bcma_find_core(bus, bcma_cc_core_id(bus));
525 bus->drv_cc.core = core;
526 bcma_core_chipcommon_early_init(&bus->drv_cc);
529 /* Early init MIPS core */
530 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
532 bus->drv_mips.core = core;
533 bcma_core_mips_early_init(&bus->drv_mips);
536 bcma_info(bus, "Early bus registered\n");
542 int bcma_bus_suspend(struct bcma_bus *bus)
544 struct bcma_device *core;
546 list_for_each_entry(core, &bus->cores, list) {
547 struct device_driver *drv = core->dev.driver;
549 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
557 int bcma_bus_resume(struct bcma_bus *bus)
559 struct bcma_device *core;
562 if (bus->drv_cc.core) {
563 bus->drv_cc.setup_done = false;
564 bcma_core_chipcommon_init(&bus->drv_cc);
567 list_for_each_entry(core, &bus->cores, list) {
568 struct device_driver *drv = core->dev.driver;
570 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
580 int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
582 drv->drv.name = drv->name;
583 drv->drv.bus = &bcma_bus_type;
584 drv->drv.owner = owner;
586 return driver_register(&drv->drv);
588 EXPORT_SYMBOL_GPL(__bcma_driver_register);
590 void bcma_driver_unregister(struct bcma_driver *drv)
592 driver_unregister(&drv->drv);
594 EXPORT_SYMBOL_GPL(bcma_driver_unregister);
596 static int bcma_bus_match(struct device *dev, struct device_driver *drv)
598 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
599 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
600 const struct bcma_device_id *cid = &core->id;
601 const struct bcma_device_id *did;
603 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
604 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
605 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
606 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
607 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
613 static int bcma_device_probe(struct device *dev)
615 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
616 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
621 err = adrv->probe(core);
626 static int bcma_device_remove(struct device *dev)
628 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
629 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
638 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
640 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
642 return add_uevent_var(env,
643 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
644 core->id.manuf, core->id.id,
645 core->id.rev, core->id.class);
648 static int __init bcma_modinit(void)
652 err = bus_register(&bcma_bus_type);
656 err = bcma_host_soc_register_driver();
658 pr_err("SoC host initialization failed\n");
661 #ifdef CONFIG_BCMA_HOST_PCI
662 err = bcma_host_pci_init();
664 pr_err("PCI host initialization failed\n");
671 fs_initcall(bcma_modinit);
673 static void __exit bcma_modexit(void)
675 #ifdef CONFIG_BCMA_HOST_PCI
676 bcma_host_pci_exit();
678 bcma_host_soc_unregister_driver();
679 bus_unregister(&bcma_bus_type);
681 module_exit(bcma_modexit)