0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #define pr_fmt(fmt) "OF: " fmt
0011
0012 #include <linux/errno.h>
0013 #include <linux/module.h>
0014 #include <linux/amba/bus.h>
0015 #include <linux/device.h>
0016 #include <linux/dma-mapping.h>
0017 #include <linux/slab.h>
0018 #include <linux/of_address.h>
0019 #include <linux/of_device.h>
0020 #include <linux/of_irq.h>
0021 #include <linux/of_platform.h>
0022 #include <linux/platform_device.h>
0023
0024 const struct of_device_id of_default_bus_match_table[] = {
0025 { .compatible = "simple-bus", },
0026 { .compatible = "simple-mfd", },
0027 { .compatible = "isa", },
0028 #ifdef CONFIG_ARM_AMBA
0029 { .compatible = "arm,amba-bus", },
0030 #endif
0031 {}
0032 };
0033
0034 static const struct of_device_id of_skipped_node_table[] = {
0035 { .compatible = "operating-points-v2", },
0036 {}
0037 };
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 struct platform_device *of_find_device_by_node(struct device_node *np)
0049 {
0050 struct device *dev;
0051
0052 dev = bus_find_device_by_of_node(&platform_bus_type, np);
0053 return dev ? to_platform_device(dev) : NULL;
0054 }
0055 EXPORT_SYMBOL(of_find_device_by_node);
0056
0057 #ifdef CONFIG_OF_ADDRESS
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 static void of_device_make_bus_id(struct device *dev)
0075 {
0076 struct device_node *node = dev->of_node;
0077 const __be32 *reg;
0078 u64 addr;
0079 u32 mask;
0080
0081
0082 while (node->parent) {
0083
0084
0085
0086
0087 reg = of_get_property(node, "reg", NULL);
0088 if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
0089 if (!of_property_read_u32(node, "mask", &mask))
0090 dev_set_name(dev, dev_name(dev) ? "%llx.%x.%pOFn:%s" : "%llx.%x.%pOFn",
0091 addr, ffs(mask) - 1, node, dev_name(dev));
0092
0093 else
0094 dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn",
0095 addr, node, dev_name(dev));
0096 return;
0097 }
0098
0099
0100 dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
0101 kbasename(node->full_name), dev_name(dev));
0102 node = node->parent;
0103 }
0104 }
0105
0106
0107
0108
0109
0110
0111
0112 struct platform_device *of_device_alloc(struct device_node *np,
0113 const char *bus_id,
0114 struct device *parent)
0115 {
0116 struct platform_device *dev;
0117 int rc, i, num_reg = 0;
0118 struct resource *res, temp_res;
0119
0120 dev = platform_device_alloc("", PLATFORM_DEVID_NONE);
0121 if (!dev)
0122 return NULL;
0123
0124
0125 while (of_address_to_resource(np, num_reg, &temp_res) == 0)
0126 num_reg++;
0127
0128
0129 if (num_reg) {
0130 res = kcalloc(num_reg, sizeof(*res), GFP_KERNEL);
0131 if (!res) {
0132 platform_device_put(dev);
0133 return NULL;
0134 }
0135
0136 dev->num_resources = num_reg;
0137 dev->resource = res;
0138 for (i = 0; i < num_reg; i++, res++) {
0139 rc = of_address_to_resource(np, i, res);
0140 WARN_ON(rc);
0141 }
0142 }
0143
0144 dev->dev.of_node = of_node_get(np);
0145 dev->dev.fwnode = &np->fwnode;
0146 dev->dev.parent = parent ? : &platform_bus;
0147
0148 if (bus_id)
0149 dev_set_name(&dev->dev, "%s", bus_id);
0150 else
0151 of_device_make_bus_id(&dev->dev);
0152
0153 return dev;
0154 }
0155 EXPORT_SYMBOL(of_device_alloc);
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167 static struct platform_device *of_platform_device_create_pdata(
0168 struct device_node *np,
0169 const char *bus_id,
0170 void *platform_data,
0171 struct device *parent)
0172 {
0173 struct platform_device *dev;
0174
0175 if (!of_device_is_available(np) ||
0176 of_node_test_and_set_flag(np, OF_POPULATED))
0177 return NULL;
0178
0179 dev = of_device_alloc(np, bus_id, parent);
0180 if (!dev)
0181 goto err_clear_flag;
0182
0183 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
0184 if (!dev->dev.dma_mask)
0185 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
0186 dev->dev.bus = &platform_bus_type;
0187 dev->dev.platform_data = platform_data;
0188 of_msi_configure(&dev->dev, dev->dev.of_node);
0189
0190 if (of_device_add(dev) != 0) {
0191 platform_device_put(dev);
0192 goto err_clear_flag;
0193 }
0194
0195 return dev;
0196
0197 err_clear_flag:
0198 of_node_clear_flag(np, OF_POPULATED);
0199 return NULL;
0200 }
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211 struct platform_device *of_platform_device_create(struct device_node *np,
0212 const char *bus_id,
0213 struct device *parent)
0214 {
0215 return of_platform_device_create_pdata(np, bus_id, NULL, parent);
0216 }
0217 EXPORT_SYMBOL(of_platform_device_create);
0218
0219 #ifdef CONFIG_ARM_AMBA
0220 static struct amba_device *of_amba_device_create(struct device_node *node,
0221 const char *bus_id,
0222 void *platform_data,
0223 struct device *parent)
0224 {
0225 struct amba_device *dev;
0226 const void *prop;
0227 int ret;
0228
0229 pr_debug("Creating amba device %pOF\n", node);
0230
0231 if (!of_device_is_available(node) ||
0232 of_node_test_and_set_flag(node, OF_POPULATED))
0233 return NULL;
0234
0235 dev = amba_device_alloc(NULL, 0, 0);
0236 if (!dev)
0237 goto err_clear_flag;
0238
0239
0240 dev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
0241 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
0242
0243
0244 dev->dev.of_node = of_node_get(node);
0245 dev->dev.fwnode = &node->fwnode;
0246 dev->dev.parent = parent ? : &platform_bus;
0247 dev->dev.platform_data = platform_data;
0248 if (bus_id)
0249 dev_set_name(&dev->dev, "%s", bus_id);
0250 else
0251 of_device_make_bus_id(&dev->dev);
0252
0253
0254 prop = of_get_property(node, "arm,primecell-periphid", NULL);
0255 if (prop)
0256 dev->periphid = of_read_ulong(prop, 1);
0257
0258 ret = of_address_to_resource(node, 0, &dev->res);
0259 if (ret) {
0260 pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n",
0261 ret, node);
0262 goto err_free;
0263 }
0264
0265 ret = amba_device_add(dev, &iomem_resource);
0266 if (ret) {
0267 pr_err("amba_device_add() failed (%d) for %pOF\n",
0268 ret, node);
0269 goto err_free;
0270 }
0271
0272 return dev;
0273
0274 err_free:
0275 amba_device_put(dev);
0276 err_clear_flag:
0277 of_node_clear_flag(node, OF_POPULATED);
0278 return NULL;
0279 }
0280 #else
0281 static struct amba_device *of_amba_device_create(struct device_node *node,
0282 const char *bus_id,
0283 void *platform_data,
0284 struct device *parent)
0285 {
0286 return NULL;
0287 }
0288 #endif
0289
0290
0291
0292
0293 static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
0294 struct device_node *np)
0295 {
0296 const struct of_dev_auxdata *auxdata;
0297 struct resource res;
0298 int compatible = 0;
0299
0300 if (!lookup)
0301 return NULL;
0302
0303 auxdata = lookup;
0304 for (; auxdata->compatible; auxdata++) {
0305 if (!of_device_is_compatible(np, auxdata->compatible))
0306 continue;
0307 compatible++;
0308 if (!of_address_to_resource(np, 0, &res))
0309 if (res.start != auxdata->phys_addr)
0310 continue;
0311 pr_debug("%pOF: devname=%s\n", np, auxdata->name);
0312 return auxdata;
0313 }
0314
0315 if (!compatible)
0316 return NULL;
0317
0318
0319 auxdata = lookup;
0320 for (; auxdata->compatible; auxdata++) {
0321 if (!of_device_is_compatible(np, auxdata->compatible))
0322 continue;
0323 if (!auxdata->phys_addr && !auxdata->name) {
0324 pr_debug("%pOF: compatible match\n", np);
0325 return auxdata;
0326 }
0327 }
0328
0329 return NULL;
0330 }
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343 static int of_platform_bus_create(struct device_node *bus,
0344 const struct of_device_id *matches,
0345 const struct of_dev_auxdata *lookup,
0346 struct device *parent, bool strict)
0347 {
0348 const struct of_dev_auxdata *auxdata;
0349 struct device_node *child;
0350 struct platform_device *dev;
0351 const char *bus_id = NULL;
0352 void *platform_data = NULL;
0353 int rc = 0;
0354
0355
0356 if (strict && (!of_get_property(bus, "compatible", NULL))) {
0357 pr_debug("%s() - skipping %pOF, no compatible prop\n",
0358 __func__, bus);
0359 return 0;
0360 }
0361
0362
0363 if (unlikely(of_match_node(of_skipped_node_table, bus))) {
0364 pr_debug("%s() - skipping %pOF node\n", __func__, bus);
0365 return 0;
0366 }
0367
0368 if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
0369 pr_debug("%s() - skipping %pOF, already populated\n",
0370 __func__, bus);
0371 return 0;
0372 }
0373
0374 auxdata = of_dev_lookup(lookup, bus);
0375 if (auxdata) {
0376 bus_id = auxdata->name;
0377 platform_data = auxdata->platform_data;
0378 }
0379
0380 if (of_device_is_compatible(bus, "arm,primecell")) {
0381
0382
0383
0384
0385 of_amba_device_create(bus, bus_id, platform_data, parent);
0386 return 0;
0387 }
0388
0389 dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
0390 if (!dev || !of_match_node(matches, bus))
0391 return 0;
0392
0393 for_each_child_of_node(bus, child) {
0394 pr_debug(" create child: %pOF\n", child);
0395 rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
0396 if (rc) {
0397 of_node_put(child);
0398 break;
0399 }
0400 }
0401 of_node_set_flag(bus, OF_POPULATED_BUS);
0402 return rc;
0403 }
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414 int of_platform_bus_probe(struct device_node *root,
0415 const struct of_device_id *matches,
0416 struct device *parent)
0417 {
0418 struct device_node *child;
0419 int rc = 0;
0420
0421 root = root ? of_node_get(root) : of_find_node_by_path("/");
0422 if (!root)
0423 return -EINVAL;
0424
0425 pr_debug("%s()\n", __func__);
0426 pr_debug(" starting at: %pOF\n", root);
0427
0428
0429 if (of_match_node(matches, root)) {
0430 rc = of_platform_bus_create(root, matches, NULL, parent, false);
0431 } else for_each_child_of_node(root, child) {
0432 if (!of_match_node(matches, child))
0433 continue;
0434 rc = of_platform_bus_create(child, matches, NULL, parent, false);
0435 if (rc) {
0436 of_node_put(child);
0437 break;
0438 }
0439 }
0440
0441 of_node_put(root);
0442 return rc;
0443 }
0444 EXPORT_SYMBOL(of_platform_bus_probe);
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465 int of_platform_populate(struct device_node *root,
0466 const struct of_device_id *matches,
0467 const struct of_dev_auxdata *lookup,
0468 struct device *parent)
0469 {
0470 struct device_node *child;
0471 int rc = 0;
0472
0473 root = root ? of_node_get(root) : of_find_node_by_path("/");
0474 if (!root)
0475 return -EINVAL;
0476
0477 pr_debug("%s()\n", __func__);
0478 pr_debug(" starting at: %pOF\n", root);
0479
0480 device_links_supplier_sync_state_pause();
0481 for_each_child_of_node(root, child) {
0482 rc = of_platform_bus_create(child, matches, lookup, parent, true);
0483 if (rc) {
0484 of_node_put(child);
0485 break;
0486 }
0487 }
0488 device_links_supplier_sync_state_resume();
0489
0490 of_node_set_flag(root, OF_POPULATED_BUS);
0491
0492 of_node_put(root);
0493 return rc;
0494 }
0495 EXPORT_SYMBOL_GPL(of_platform_populate);
0496
0497 int of_platform_default_populate(struct device_node *root,
0498 const struct of_dev_auxdata *lookup,
0499 struct device *parent)
0500 {
0501 return of_platform_populate(root, of_default_bus_match_table, lookup,
0502 parent);
0503 }
0504 EXPORT_SYMBOL_GPL(of_platform_default_populate);
0505
0506 static const struct of_device_id reserved_mem_matches[] = {
0507 { .compatible = "phram" },
0508 { .compatible = "qcom,rmtfs-mem" },
0509 { .compatible = "qcom,cmd-db" },
0510 { .compatible = "qcom,smem" },
0511 { .compatible = "ramoops" },
0512 { .compatible = "nvmem-rmem" },
0513 { .compatible = "google,open-dice" },
0514 {}
0515 };
0516
0517 static int __init of_platform_default_populate_init(void)
0518 {
0519 struct device_node *node;
0520
0521 device_links_supplier_sync_state_pause();
0522
0523 if (!of_have_populated_dt())
0524 return -ENODEV;
0525
0526 if (IS_ENABLED(CONFIG_PPC)) {
0527 struct device_node *boot_display = NULL;
0528 struct platform_device *dev;
0529 int ret;
0530
0531
0532 if (of_get_property(of_chosen, "linux,bootx-noscreen", NULL)) {
0533
0534
0535
0536
0537
0538
0539
0540 dev = platform_device_alloc("bootx-noscreen", 0);
0541 if (WARN_ON(!dev))
0542 return -ENOMEM;
0543 ret = platform_device_add(dev);
0544 if (WARN_ON(ret)) {
0545 platform_device_put(dev);
0546 return ret;
0547 }
0548 }
0549
0550
0551
0552
0553
0554
0555 for_each_node_by_type(node, "display") {
0556 if (!of_get_property(node, "linux,opened", NULL) ||
0557 !of_get_property(node, "linux,boot-display", NULL))
0558 continue;
0559 dev = of_platform_device_create(node, "of-display", NULL);
0560 if (WARN_ON(!dev))
0561 return -ENOMEM;
0562 boot_display = node;
0563 break;
0564 }
0565 for_each_node_by_type(node, "display") {
0566 if (!of_get_property(node, "linux,opened", NULL) || node == boot_display)
0567 continue;
0568 of_platform_device_create(node, "of-display", NULL);
0569 }
0570
0571 } else {
0572
0573
0574
0575
0576
0577 for_each_matching_node(node, reserved_mem_matches)
0578 of_platform_device_create(node, NULL, NULL);
0579
0580 node = of_find_node_by_path("/firmware");
0581 if (node) {
0582 of_platform_populate(node, NULL, NULL, NULL);
0583 of_node_put(node);
0584 }
0585
0586 node = of_get_compatible_child(of_chosen, "simple-framebuffer");
0587 of_platform_device_create(node, NULL, NULL);
0588 of_node_put(node);
0589
0590
0591 of_platform_default_populate(NULL, NULL, NULL);
0592 }
0593
0594 return 0;
0595 }
0596 arch_initcall_sync(of_platform_default_populate_init);
0597
0598 static int __init of_platform_sync_state_init(void)
0599 {
0600 device_links_supplier_sync_state_resume();
0601 return 0;
0602 }
0603 late_initcall_sync(of_platform_sync_state_init);
0604
0605 int of_platform_device_destroy(struct device *dev, void *data)
0606 {
0607
0608 if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
0609 return 0;
0610
0611
0612 if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
0613 device_for_each_child(dev, NULL, of_platform_device_destroy);
0614
0615 of_node_clear_flag(dev->of_node, OF_POPULATED);
0616 of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
0617
0618 if (dev->bus == &platform_bus_type)
0619 platform_device_unregister(to_platform_device(dev));
0620 #ifdef CONFIG_ARM_AMBA
0621 else if (dev->bus == &amba_bustype)
0622 amba_device_unregister(to_amba_device(dev));
0623 #endif
0624
0625 return 0;
0626 }
0627 EXPORT_SYMBOL_GPL(of_platform_device_destroy);
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638 void of_platform_depopulate(struct device *parent)
0639 {
0640 if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
0641 device_for_each_child_reverse(parent, NULL, of_platform_device_destroy);
0642 of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
0643 }
0644 }
0645 EXPORT_SYMBOL_GPL(of_platform_depopulate);
0646
0647 static void devm_of_platform_populate_release(struct device *dev, void *res)
0648 {
0649 of_platform_depopulate(*(struct device **)res);
0650 }
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660
0661 int devm_of_platform_populate(struct device *dev)
0662 {
0663 struct device **ptr;
0664 int ret;
0665
0666 if (!dev)
0667 return -EINVAL;
0668
0669 ptr = devres_alloc(devm_of_platform_populate_release,
0670 sizeof(*ptr), GFP_KERNEL);
0671 if (!ptr)
0672 return -ENOMEM;
0673
0674 ret = of_platform_populate(dev->of_node, NULL, NULL, dev);
0675 if (ret) {
0676 devres_free(ptr);
0677 } else {
0678 *ptr = dev;
0679 devres_add(dev, ptr);
0680 }
0681
0682 return ret;
0683 }
0684 EXPORT_SYMBOL_GPL(devm_of_platform_populate);
0685
0686 static int devm_of_platform_match(struct device *dev, void *res, void *data)
0687 {
0688 struct device **ptr = res;
0689
0690 if (!ptr) {
0691 WARN_ON(!ptr);
0692 return 0;
0693 }
0694
0695 return *ptr == data;
0696 }
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707 void devm_of_platform_depopulate(struct device *dev)
0708 {
0709 int ret;
0710
0711 ret = devres_release(dev, devm_of_platform_populate_release,
0712 devm_of_platform_match, dev);
0713
0714 WARN_ON(ret);
0715 }
0716 EXPORT_SYMBOL_GPL(devm_of_platform_depopulate);
0717
0718 #ifdef CONFIG_OF_DYNAMIC
0719 static int of_platform_notify(struct notifier_block *nb,
0720 unsigned long action, void *arg)
0721 {
0722 struct of_reconfig_data *rd = arg;
0723 struct platform_device *pdev_parent, *pdev;
0724 bool children_left;
0725
0726 switch (of_reconfig_get_state_change(action, rd)) {
0727 case OF_RECONFIG_CHANGE_ADD:
0728
0729 if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
0730 return NOTIFY_OK;
0731
0732
0733 if (of_node_check_flag(rd->dn, OF_POPULATED))
0734 return NOTIFY_OK;
0735
0736
0737 pdev_parent = of_find_device_by_node(rd->dn->parent);
0738 pdev = of_platform_device_create(rd->dn, NULL,
0739 pdev_parent ? &pdev_parent->dev : NULL);
0740 platform_device_put(pdev_parent);
0741
0742 if (pdev == NULL) {
0743 pr_err("%s: failed to create for '%pOF'\n",
0744 __func__, rd->dn);
0745
0746 return notifier_from_errno(-EINVAL);
0747 }
0748 break;
0749
0750 case OF_RECONFIG_CHANGE_REMOVE:
0751
0752
0753 if (!of_node_check_flag(rd->dn, OF_POPULATED))
0754 return NOTIFY_OK;
0755
0756
0757 pdev = of_find_device_by_node(rd->dn);
0758 if (pdev == NULL)
0759 return NOTIFY_OK;
0760
0761
0762 of_platform_device_destroy(&pdev->dev, &children_left);
0763
0764
0765 platform_device_put(pdev);
0766 break;
0767 }
0768
0769 return NOTIFY_OK;
0770 }
0771
0772 static struct notifier_block platform_of_notifier = {
0773 .notifier_call = of_platform_notify,
0774 };
0775
0776 void of_platform_register_reconfig_notifier(void)
0777 {
0778 WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
0779 }
0780 #endif
0781
0782 #endif