0001
0002
0003
0004
0005
0006
0007
0008 #include "bcma_private.h"
0009 #include <linux/module.h>
0010 #include <linux/mmc/sdio_func.h>
0011 #include <linux/platform_device.h>
0012 #include <linux/pci.h>
0013 #include <linux/bcma/bcma.h>
0014 #include <linux/slab.h>
0015 #include <linux/of_address.h>
0016 #include <linux/of_irq.h>
0017 #include <linux/of_platform.h>
0018
0019 MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
0020 MODULE_LICENSE("GPL");
0021
0022
0023 static unsigned int bcma_bus_next_num;
0024
0025
0026 static DEFINE_MUTEX(bcma_buses_mutex);
0027
0028 static int bcma_bus_match(struct device *dev, struct device_driver *drv);
0029 static int bcma_device_probe(struct device *dev);
0030 static void bcma_device_remove(struct device *dev);
0031 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
0032
0033 static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
0034 {
0035 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0036 return sprintf(buf, "0x%03X\n", core->id.manuf);
0037 }
0038 static DEVICE_ATTR_RO(manuf);
0039
0040 static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
0041 {
0042 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0043 return sprintf(buf, "0x%03X\n", core->id.id);
0044 }
0045 static DEVICE_ATTR_RO(id);
0046
0047 static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
0048 {
0049 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0050 return sprintf(buf, "0x%02X\n", core->id.rev);
0051 }
0052 static DEVICE_ATTR_RO(rev);
0053
0054 static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
0055 {
0056 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0057 return sprintf(buf, "0x%X\n", core->id.class);
0058 }
0059 static DEVICE_ATTR_RO(class);
0060
0061 static struct attribute *bcma_device_attrs[] = {
0062 &dev_attr_manuf.attr,
0063 &dev_attr_id.attr,
0064 &dev_attr_rev.attr,
0065 &dev_attr_class.attr,
0066 NULL,
0067 };
0068 ATTRIBUTE_GROUPS(bcma_device);
0069
0070 static struct bus_type bcma_bus_type = {
0071 .name = "bcma",
0072 .match = bcma_bus_match,
0073 .probe = bcma_device_probe,
0074 .remove = bcma_device_remove,
0075 .uevent = bcma_device_uevent,
0076 .dev_groups = bcma_device_groups,
0077 };
0078
0079 static u16 bcma_cc_core_id(struct bcma_bus *bus)
0080 {
0081 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
0082 return BCMA_CORE_4706_CHIPCOMMON;
0083 return BCMA_CORE_CHIPCOMMON;
0084 }
0085
0086 struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
0087 u8 unit)
0088 {
0089 struct bcma_device *core;
0090
0091 list_for_each_entry(core, &bus->cores, list) {
0092 if (core->id.id == coreid && core->core_unit == unit)
0093 return core;
0094 }
0095 return NULL;
0096 }
0097 EXPORT_SYMBOL_GPL(bcma_find_core_unit);
0098
0099 bool bcma_wait_value(struct bcma_device *core, u16 reg, u32 mask, u32 value,
0100 int timeout)
0101 {
0102 unsigned long deadline = jiffies + timeout;
0103 u32 val;
0104
0105 do {
0106 val = bcma_read32(core, reg);
0107 if ((val & mask) == value)
0108 return true;
0109 cpu_relax();
0110 udelay(10);
0111 } while (!time_after_eq(jiffies, deadline));
0112
0113 bcma_warn(core->bus, "Timeout waiting for register 0x%04X!\n", reg);
0114
0115 return false;
0116 }
0117
0118 static void bcma_release_core_dev(struct device *dev)
0119 {
0120 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0121 if (core->io_addr)
0122 iounmap(core->io_addr);
0123 if (core->io_wrap)
0124 iounmap(core->io_wrap);
0125 kfree(core);
0126 }
0127
0128 static bool bcma_is_core_needed_early(u16 core_id)
0129 {
0130 switch (core_id) {
0131 case BCMA_CORE_NS_NAND:
0132 case BCMA_CORE_NS_QSPI:
0133 return true;
0134 }
0135
0136 return false;
0137 }
0138
0139 static struct device_node *bcma_of_find_child_device(struct device *parent,
0140 struct bcma_device *core)
0141 {
0142 struct device_node *node;
0143 u64 size;
0144 const __be32 *reg;
0145
0146 if (!parent->of_node)
0147 return NULL;
0148
0149 for_each_child_of_node(parent->of_node, node) {
0150 reg = of_get_address(node, 0, &size, NULL);
0151 if (!reg)
0152 continue;
0153 if (of_translate_address(node, reg) == core->addr)
0154 return node;
0155 }
0156 return NULL;
0157 }
0158
0159 static int bcma_of_irq_parse(struct device *parent,
0160 struct bcma_device *core,
0161 struct of_phandle_args *out_irq, int num)
0162 {
0163 __be32 laddr[1];
0164 int rc;
0165
0166 if (core->dev.of_node) {
0167 rc = of_irq_parse_one(core->dev.of_node, num, out_irq);
0168 if (!rc)
0169 return rc;
0170 }
0171
0172 out_irq->np = parent->of_node;
0173 out_irq->args_count = 1;
0174 out_irq->args[0] = num;
0175
0176 laddr[0] = cpu_to_be32(core->addr);
0177 return of_irq_parse_raw(laddr, out_irq);
0178 }
0179
0180 static unsigned int bcma_of_get_irq(struct device *parent,
0181 struct bcma_device *core, int num)
0182 {
0183 struct of_phandle_args out_irq;
0184 int ret;
0185
0186 if (!IS_ENABLED(CONFIG_OF_IRQ) || !parent->of_node)
0187 return 0;
0188
0189 ret = bcma_of_irq_parse(parent, core, &out_irq, num);
0190 if (ret) {
0191 bcma_debug(core->bus, "bcma_of_get_irq() failed with rc=%d\n",
0192 ret);
0193 return 0;
0194 }
0195
0196 return irq_create_of_mapping(&out_irq);
0197 }
0198
0199 static void bcma_of_fill_device(struct device *parent,
0200 struct bcma_device *core)
0201 {
0202 struct device_node *node;
0203
0204 node = bcma_of_find_child_device(parent, core);
0205 if (node)
0206 core->dev.of_node = node;
0207
0208 core->irq = bcma_of_get_irq(parent, core, 0);
0209
0210 of_dma_configure(&core->dev, node, false);
0211 }
0212
0213 unsigned int bcma_core_irq(struct bcma_device *core, int num)
0214 {
0215 struct bcma_bus *bus = core->bus;
0216 unsigned int mips_irq;
0217
0218 switch (bus->hosttype) {
0219 case BCMA_HOSTTYPE_PCI:
0220 return bus->host_pci->irq;
0221 case BCMA_HOSTTYPE_SOC:
0222 if (bus->drv_mips.core && num == 0) {
0223 mips_irq = bcma_core_mips_irq(core);
0224 return mips_irq <= 4 ? mips_irq + 2 : 0;
0225 }
0226 if (bus->dev)
0227 return bcma_of_get_irq(bus->dev, core, num);
0228 return 0;
0229 case BCMA_HOSTTYPE_SDIO:
0230 return 0;
0231 }
0232
0233 return 0;
0234 }
0235 EXPORT_SYMBOL(bcma_core_irq);
0236
0237 void bcma_prepare_core(struct bcma_bus *bus, struct bcma_device *core)
0238 {
0239 device_initialize(&core->dev);
0240 core->dev.release = bcma_release_core_dev;
0241 core->dev.bus = &bcma_bus_type;
0242 dev_set_name(&core->dev, "bcma%d:%d", bus->num, core->core_index);
0243 core->dev.parent = bus->dev;
0244 if (bus->dev)
0245 bcma_of_fill_device(bus->dev, core);
0246
0247 switch (bus->hosttype) {
0248 case BCMA_HOSTTYPE_PCI:
0249 core->dma_dev = bus->dev;
0250 core->irq = bus->host_pci->irq;
0251 break;
0252 case BCMA_HOSTTYPE_SOC:
0253 if (IS_ENABLED(CONFIG_OF) && bus->dev) {
0254 core->dma_dev = bus->dev;
0255 } else {
0256 core->dev.dma_mask = &core->dev.coherent_dma_mask;
0257 core->dma_dev = &core->dev;
0258 }
0259 break;
0260 case BCMA_HOSTTYPE_SDIO:
0261 break;
0262 }
0263 }
0264
0265 void bcma_init_bus(struct bcma_bus *bus)
0266 {
0267 mutex_lock(&bcma_buses_mutex);
0268 bus->num = bcma_bus_next_num++;
0269 mutex_unlock(&bcma_buses_mutex);
0270
0271 INIT_LIST_HEAD(&bus->cores);
0272 bus->nr_cores = 0;
0273
0274 bcma_detect_chip(bus);
0275 }
0276
0277 static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core)
0278 {
0279 int err;
0280
0281 err = device_add(&core->dev);
0282 if (err) {
0283 bcma_err(bus, "Could not register dev for core 0x%03X\n",
0284 core->id.id);
0285 return;
0286 }
0287 core->dev_registered = true;
0288 }
0289
0290 static int bcma_register_devices(struct bcma_bus *bus)
0291 {
0292 struct bcma_device *core;
0293 int err;
0294
0295 list_for_each_entry(core, &bus->cores, list) {
0296
0297 switch (core->id.id) {
0298 case BCMA_CORE_4706_CHIPCOMMON:
0299 case BCMA_CORE_CHIPCOMMON:
0300 case BCMA_CORE_NS_CHIPCOMMON_B:
0301 case BCMA_CORE_PCI:
0302 case BCMA_CORE_PCIE:
0303 case BCMA_CORE_PCIE2:
0304 case BCMA_CORE_MIPS_74K:
0305 case BCMA_CORE_4706_MAC_GBIT_COMMON:
0306 continue;
0307 }
0308
0309
0310 if (bcma_is_core_needed_early(core->id.id))
0311 continue;
0312
0313
0314 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
0315 core->core_unit > 0)
0316 continue;
0317
0318 bcma_register_core(bus, core);
0319 }
0320
0321 #ifdef CONFIG_BCMA_PFLASH
0322 if (bus->drv_cc.pflash.present) {
0323 err = platform_device_register(&bcma_pflash_dev);
0324 if (err)
0325 bcma_err(bus, "Error registering parallel flash\n");
0326 }
0327 #endif
0328
0329 #ifdef CONFIG_BCMA_SFLASH
0330 if (bus->drv_cc.sflash.present) {
0331 err = platform_device_register(&bcma_sflash_dev);
0332 if (err)
0333 bcma_err(bus, "Error registering serial flash\n");
0334 }
0335 #endif
0336
0337 #ifdef CONFIG_BCMA_NFLASH
0338 if (bus->drv_cc.nflash.present) {
0339 err = platform_device_register(&bcma_nflash_dev);
0340 if (err)
0341 bcma_err(bus, "Error registering NAND flash\n");
0342 }
0343 #endif
0344 err = bcma_gpio_init(&bus->drv_cc);
0345 if (err == -ENOTSUPP)
0346 bcma_debug(bus, "GPIO driver not activated\n");
0347 else if (err)
0348 bcma_err(bus, "Error registering GPIO driver: %i\n", err);
0349
0350 if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
0351 err = bcma_chipco_watchdog_register(&bus->drv_cc);
0352 if (err)
0353 bcma_err(bus, "Error registering watchdog driver\n");
0354 }
0355
0356 return 0;
0357 }
0358
0359 void bcma_unregister_cores(struct bcma_bus *bus)
0360 {
0361 struct bcma_device *core, *tmp;
0362
0363 list_for_each_entry_safe(core, tmp, &bus->cores, list) {
0364 if (!core->dev_registered)
0365 continue;
0366 list_del(&core->list);
0367 device_unregister(&core->dev);
0368 }
0369 if (bus->hosttype == BCMA_HOSTTYPE_SOC)
0370 platform_device_unregister(bus->drv_cc.watchdog);
0371
0372
0373 list_for_each_entry_safe(core, tmp, &bus->cores, list) {
0374 list_del(&core->list);
0375 put_device(&core->dev);
0376 }
0377 }
0378
0379 int bcma_bus_register(struct bcma_bus *bus)
0380 {
0381 int err;
0382 struct bcma_device *core;
0383
0384
0385 err = bcma_bus_scan(bus);
0386 if (err) {
0387 bcma_err(bus, "Failed to scan: %d\n", err);
0388 return err;
0389 }
0390
0391
0392 core = bcma_find_core(bus, bcma_cc_core_id(bus));
0393 if (core) {
0394 bus->drv_cc.core = core;
0395 bcma_core_chipcommon_early_init(&bus->drv_cc);
0396 }
0397
0398
0399 core = bcma_find_core(bus, BCMA_CORE_PCIE);
0400 if (core) {
0401 bus->drv_pci[0].core = core;
0402 bcma_core_pci_early_init(&bus->drv_pci[0]);
0403 }
0404
0405 if (bus->dev)
0406 of_platform_default_populate(bus->dev->of_node, NULL, bus->dev);
0407
0408
0409 list_for_each_entry(core, &bus->cores, list) {
0410 if (bcma_is_core_needed_early(core->id.id))
0411 bcma_register_core(bus, core);
0412 }
0413
0414
0415 err = bcma_sprom_get(bus);
0416 if (err == -ENOENT) {
0417 bcma_err(bus, "No SPROM available\n");
0418 } else if (err)
0419 bcma_err(bus, "Failed to get SPROM: %d\n", err);
0420
0421
0422 core = bcma_find_core(bus, bcma_cc_core_id(bus));
0423 if (core) {
0424 bus->drv_cc.core = core;
0425 bcma_core_chipcommon_init(&bus->drv_cc);
0426 }
0427
0428
0429 core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B);
0430 if (core) {
0431 bus->drv_cc_b.core = core;
0432 bcma_core_chipcommon_b_init(&bus->drv_cc_b);
0433 }
0434
0435
0436 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
0437 if (core) {
0438 bus->drv_mips.core = core;
0439 bcma_core_mips_init(&bus->drv_mips);
0440 }
0441
0442
0443 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
0444 if (core) {
0445 bus->drv_pci[0].core = core;
0446 bcma_core_pci_init(&bus->drv_pci[0]);
0447 }
0448
0449
0450 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
0451 if (core) {
0452 bus->drv_pci[1].core = core;
0453 bcma_core_pci_init(&bus->drv_pci[1]);
0454 }
0455
0456
0457 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0);
0458 if (core) {
0459 bus->drv_pcie2.core = core;
0460 bcma_core_pcie2_init(&bus->drv_pcie2);
0461 }
0462
0463
0464 core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
0465 if (core) {
0466 bus->drv_gmac_cmn.core = core;
0467 bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
0468 }
0469
0470
0471 bcma_register_devices(bus);
0472
0473 bcma_info(bus, "Bus registered\n");
0474
0475 return 0;
0476 }
0477
0478 void bcma_bus_unregister(struct bcma_bus *bus)
0479 {
0480 int err;
0481
0482 err = bcma_gpio_unregister(&bus->drv_cc);
0483 if (err == -EBUSY)
0484 bcma_err(bus, "Some GPIOs are still in use.\n");
0485 else if (err)
0486 bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
0487
0488 bcma_core_chipcommon_b_free(&bus->drv_cc_b);
0489
0490 bcma_unregister_cores(bus);
0491 }
0492
0493
0494
0495
0496
0497
0498 int __init bcma_bus_early_register(struct bcma_bus *bus)
0499 {
0500 int err;
0501 struct bcma_device *core;
0502
0503
0504 err = bcma_bus_scan(bus);
0505 if (err) {
0506 bcma_err(bus, "Failed to scan bus: %d\n", err);
0507 return -1;
0508 }
0509
0510
0511 core = bcma_find_core(bus, bcma_cc_core_id(bus));
0512 if (core) {
0513 bus->drv_cc.core = core;
0514 bcma_core_chipcommon_early_init(&bus->drv_cc);
0515 }
0516
0517
0518 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
0519 if (core) {
0520 bus->drv_mips.core = core;
0521 bcma_core_mips_early_init(&bus->drv_mips);
0522 }
0523
0524 bcma_info(bus, "Early bus registered\n");
0525
0526 return 0;
0527 }
0528
0529 #ifdef CONFIG_PM
0530 int bcma_bus_suspend(struct bcma_bus *bus)
0531 {
0532 struct bcma_device *core;
0533
0534 list_for_each_entry(core, &bus->cores, list) {
0535 struct device_driver *drv = core->dev.driver;
0536 if (drv) {
0537 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
0538 if (adrv->suspend)
0539 adrv->suspend(core);
0540 }
0541 }
0542 return 0;
0543 }
0544
0545 int bcma_bus_resume(struct bcma_bus *bus)
0546 {
0547 struct bcma_device *core;
0548
0549
0550 if (bus->drv_cc.core) {
0551 bus->drv_cc.setup_done = false;
0552 bcma_core_chipcommon_init(&bus->drv_cc);
0553 }
0554
0555 list_for_each_entry(core, &bus->cores, list) {
0556 struct device_driver *drv = core->dev.driver;
0557 if (drv) {
0558 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
0559 if (adrv->resume)
0560 adrv->resume(core);
0561 }
0562 }
0563
0564 return 0;
0565 }
0566 #endif
0567
0568 int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
0569 {
0570 drv->drv.name = drv->name;
0571 drv->drv.bus = &bcma_bus_type;
0572 drv->drv.owner = owner;
0573
0574 return driver_register(&drv->drv);
0575 }
0576 EXPORT_SYMBOL_GPL(__bcma_driver_register);
0577
0578 void bcma_driver_unregister(struct bcma_driver *drv)
0579 {
0580 driver_unregister(&drv->drv);
0581 }
0582 EXPORT_SYMBOL_GPL(bcma_driver_unregister);
0583
0584 static int bcma_bus_match(struct device *dev, struct device_driver *drv)
0585 {
0586 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0587 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
0588 const struct bcma_device_id *cid = &core->id;
0589 const struct bcma_device_id *did;
0590
0591 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
0592 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
0593 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
0594 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
0595 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
0596 return 1;
0597 }
0598 return 0;
0599 }
0600
0601 static int bcma_device_probe(struct device *dev)
0602 {
0603 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0604 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
0605 drv);
0606 int err = 0;
0607
0608 get_device(dev);
0609 if (adrv->probe)
0610 err = adrv->probe(core);
0611 if (err)
0612 put_device(dev);
0613
0614 return err;
0615 }
0616
0617 static void bcma_device_remove(struct device *dev)
0618 {
0619 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0620 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
0621 drv);
0622
0623 if (adrv->remove)
0624 adrv->remove(core);
0625 put_device(dev);
0626 }
0627
0628 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
0629 {
0630 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
0631
0632 return add_uevent_var(env,
0633 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
0634 core->id.manuf, core->id.id,
0635 core->id.rev, core->id.class);
0636 }
0637
0638 static unsigned int bcma_bus_registered;
0639
0640
0641
0642
0643
0644
0645 static int __init bcma_init_bus_register(void)
0646 {
0647 int err;
0648
0649 if (bcma_bus_registered)
0650 return 0;
0651
0652 err = bus_register(&bcma_bus_type);
0653 if (!err)
0654 bcma_bus_registered = 1;
0655
0656 return err;
0657 }
0658 #ifndef MODULE
0659 fs_initcall(bcma_init_bus_register);
0660 #endif
0661
0662
0663 static int __init bcma_modinit(void)
0664 {
0665 int err;
0666
0667 err = bcma_init_bus_register();
0668 if (err)
0669 return err;
0670
0671 err = bcma_host_soc_register_driver();
0672 if (err) {
0673 pr_err("SoC host initialization failed\n");
0674 err = 0;
0675 }
0676 #ifdef CONFIG_BCMA_HOST_PCI
0677 err = bcma_host_pci_init();
0678 if (err) {
0679 pr_err("PCI host initialization failed\n");
0680 err = 0;
0681 }
0682 #endif
0683
0684 return err;
0685 }
0686 module_init(bcma_modinit);
0687
0688 static void __exit bcma_modexit(void)
0689 {
0690 #ifdef CONFIG_BCMA_HOST_PCI
0691 bcma_host_pci_exit();
0692 #endif
0693 bcma_host_soc_unregister_driver();
0694 bus_unregister(&bcma_bus_type);
0695 }
0696 module_exit(bcma_modexit)