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 /* TODO: remove check for IS_BUILTIN(CONFIG_BCMA) check when
414 * of_default_bus_match_table is exported or in some other way
415 * accessible. This is just a temporary workaround.
417 if (IS_BUILTIN(CONFIG_BCMA) && bus->host_pdev) {
418 struct device *dev = &bus->host_pdev->dev;
420 of_platform_populate(dev->of_node, of_default_bus_match_table,
424 /* Cores providing flash access go before SPROM init */
425 list_for_each_entry(core, &bus->cores, list) {
426 if (bcma_is_core_needed_early(core->id.id))
427 bcma_register_core(bus, core);
430 /* Try to get SPROM */
431 err = bcma_sprom_get(bus);
432 if (err == -ENOENT) {
433 bcma_err(bus, "No SPROM available\n");
435 bcma_err(bus, "Failed to get SPROM: %d\n", err);
438 core = bcma_find_core(bus, bcma_cc_core_id(bus));
440 bus->drv_cc.core = core;
441 bcma_core_chipcommon_init(&bus->drv_cc);
445 core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B);
447 bus->drv_cc_b.core = core;
448 bcma_core_chipcommon_b_init(&bus->drv_cc_b);
452 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
454 bus->drv_mips.core = core;
455 bcma_core_mips_init(&bus->drv_mips);
459 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
461 bus->drv_pci[0].core = core;
462 bcma_core_pci_init(&bus->drv_pci[0]);
466 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
468 bus->drv_pci[1].core = core;
469 bcma_core_pci_init(&bus->drv_pci[1]);
472 /* Init PCIe Gen 2 core */
473 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0);
475 bus->drv_pcie2.core = core;
476 bcma_core_pcie2_init(&bus->drv_pcie2);
479 /* Init GBIT MAC COMMON core */
480 core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
482 bus->drv_gmac_cmn.core = core;
483 bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
486 /* Register found cores */
487 bcma_register_devices(bus);
489 bcma_info(bus, "Bus registered\n");
494 void bcma_bus_unregister(struct bcma_bus *bus)
498 err = bcma_gpio_unregister(&bus->drv_cc);
500 bcma_err(bus, "Some GPIOs are still in use.\n");
502 bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
504 bcma_core_chipcommon_b_free(&bus->drv_cc_b);
506 bcma_unregister_cores(bus);
510 * This is a special version of bus registration function designed for SoCs.
511 * It scans bus and performs basic initialization of main cores only.
512 * Please note it requires memory allocation, however it won't try to sleep.
514 int __init bcma_bus_early_register(struct bcma_bus *bus)
517 struct bcma_device *core;
519 /* Scan for devices (cores) */
520 err = bcma_bus_scan(bus);
522 bcma_err(bus, "Failed to scan bus: %d\n", err);
526 /* Early init CC core */
527 core = bcma_find_core(bus, bcma_cc_core_id(bus));
529 bus->drv_cc.core = core;
530 bcma_core_chipcommon_early_init(&bus->drv_cc);
533 /* Early init MIPS core */
534 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
536 bus->drv_mips.core = core;
537 bcma_core_mips_early_init(&bus->drv_mips);
540 bcma_info(bus, "Early bus registered\n");
546 int bcma_bus_suspend(struct bcma_bus *bus)
548 struct bcma_device *core;
550 list_for_each_entry(core, &bus->cores, list) {
551 struct device_driver *drv = core->dev.driver;
553 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
561 int bcma_bus_resume(struct bcma_bus *bus)
563 struct bcma_device *core;
566 if (bus->drv_cc.core) {
567 bus->drv_cc.setup_done = false;
568 bcma_core_chipcommon_init(&bus->drv_cc);
571 list_for_each_entry(core, &bus->cores, list) {
572 struct device_driver *drv = core->dev.driver;
574 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
584 int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
586 drv->drv.name = drv->name;
587 drv->drv.bus = &bcma_bus_type;
588 drv->drv.owner = owner;
590 return driver_register(&drv->drv);
592 EXPORT_SYMBOL_GPL(__bcma_driver_register);
594 void bcma_driver_unregister(struct bcma_driver *drv)
596 driver_unregister(&drv->drv);
598 EXPORT_SYMBOL_GPL(bcma_driver_unregister);
600 static int bcma_bus_match(struct device *dev, struct device_driver *drv)
602 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
603 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
604 const struct bcma_device_id *cid = &core->id;
605 const struct bcma_device_id *did;
607 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
608 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
609 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
610 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
611 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
617 static int bcma_device_probe(struct device *dev)
619 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
620 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
625 err = adrv->probe(core);
630 static int bcma_device_remove(struct device *dev)
632 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
633 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
642 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
644 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
646 return add_uevent_var(env,
647 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
648 core->id.manuf, core->id.id,
649 core->id.rev, core->id.class);
652 static int __init bcma_modinit(void)
656 err = bus_register(&bcma_bus_type);
660 err = bcma_host_soc_register_driver();
662 pr_err("SoC host initialization failed\n");
665 #ifdef CONFIG_BCMA_HOST_PCI
666 err = bcma_host_pci_init();
668 pr_err("PCI host initialization failed\n");
675 fs_initcall(bcma_modinit);
677 static void __exit bcma_modexit(void)
679 #ifdef CONFIG_BCMA_HOST_PCI
680 bcma_host_pci_exit();
682 bcma_host_soc_unregister_driver();
683 bus_unregister(&bcma_bus_type);
685 module_exit(bcma_modexit)