123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638 |
- #define pr_fmt(fmt) "OF: " fmt
- #include <linux/errno.h>
- #include <linux/module.h>
- #include <linux/amba/bus.h>
- #include <linux/device.h>
- #include <linux/dma-mapping.h>
- #include <linux/slab.h>
- #include <linux/of_address.h>
- #include <linux/of_device.h>
- #include <linux/of_irq.h>
- #include <linux/of_platform.h>
- #include <linux/platform_device.h>
- const struct of_device_id of_default_bus_match_table[] = {
- { .compatible = "simple-bus", },
- { .compatible = "simple-mfd", },
- { .compatible = "isa", },
- #ifdef CONFIG_ARM_AMBA
- { .compatible = "arm,amba-bus", },
- #endif
- {}
- };
- static int of_dev_node_match(struct device *dev, void *data)
- {
- return dev->of_node == data;
- }
- struct platform_device *of_find_device_by_node(struct device_node *np)
- {
- struct device *dev;
- dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match);
- return dev ? to_platform_device(dev) : NULL;
- }
- EXPORT_SYMBOL(of_find_device_by_node);
- #ifdef CONFIG_OF_ADDRESS
- void of_device_make_bus_id(struct device *dev)
- {
- struct device_node *node = dev->of_node;
- const __be32 *reg;
- u64 addr;
-
- while (node->parent) {
-
- reg = of_get_property(node, "reg", NULL);
- if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) {
- dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s",
- (unsigned long long)addr, node->name,
- dev_name(dev));
- return;
- }
-
- dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s",
- strrchr(node->full_name, '/') + 1, dev_name(dev));
- node = node->parent;
- }
- }
- struct platform_device *of_device_alloc(struct device_node *np,
- const char *bus_id,
- struct device *parent)
- {
- struct platform_device *dev;
- int rc, i, num_reg = 0, num_irq;
- struct resource *res, temp_res;
- dev = platform_device_alloc("", -1);
- if (!dev)
- return NULL;
-
- while (of_address_to_resource(np, num_reg, &temp_res) == 0)
- num_reg++;
- num_irq = of_irq_count(np);
-
- if (num_irq || num_reg) {
- res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
- if (!res) {
- platform_device_put(dev);
- return NULL;
- }
- dev->num_resources = num_reg + num_irq;
- dev->resource = res;
- for (i = 0; i < num_reg; i++, res++) {
- rc = of_address_to_resource(np, i, res);
- WARN_ON(rc);
- }
- if (of_irq_to_resource_table(np, res, num_irq) != num_irq)
- pr_debug("not all legacy IRQ resources mapped for %s\n",
- np->name);
- }
- dev->dev.of_node = of_node_get(np);
- dev->dev.fwnode = &np->fwnode;
- dev->dev.parent = parent ? : &platform_bus;
- if (bus_id)
- dev_set_name(&dev->dev, "%s", bus_id);
- else
- of_device_make_bus_id(&dev->dev);
- return dev;
- }
- EXPORT_SYMBOL(of_device_alloc);
- static void of_dma_deconfigure(struct device *dev)
- {
- arch_teardown_dma_ops(dev);
- }
- static struct platform_device *of_platform_device_create_pdata(
- struct device_node *np,
- const char *bus_id,
- void *platform_data,
- struct device *parent)
- {
- struct platform_device *dev;
- if (!of_device_is_available(np) ||
- of_node_test_and_set_flag(np, OF_POPULATED))
- return NULL;
- dev = of_device_alloc(np, bus_id, parent);
- if (!dev)
- goto err_clear_flag;
- dev->dev.bus = &platform_bus_type;
- dev->dev.platform_data = platform_data;
- of_dma_configure(&dev->dev, dev->dev.of_node);
- of_msi_configure(&dev->dev, dev->dev.of_node);
- if (of_device_add(dev) != 0) {
- of_dma_deconfigure(&dev->dev);
- platform_device_put(dev);
- goto err_clear_flag;
- }
- return dev;
- err_clear_flag:
- of_node_clear_flag(np, OF_POPULATED);
- return NULL;
- }
- struct platform_device *of_platform_device_create(struct device_node *np,
- const char *bus_id,
- struct device *parent)
- {
- return of_platform_device_create_pdata(np, bus_id, NULL, parent);
- }
- EXPORT_SYMBOL(of_platform_device_create);
- #ifdef CONFIG_ARM_AMBA
- static struct amba_device *of_amba_device_create(struct device_node *node,
- const char *bus_id,
- void *platform_data,
- struct device *parent)
- {
- struct amba_device *dev;
- const void *prop;
- int i, ret;
- pr_debug("Creating amba device %s\n", node->full_name);
- if (!of_device_is_available(node) ||
- of_node_test_and_set_flag(node, OF_POPULATED))
- return NULL;
- dev = amba_device_alloc(NULL, 0, 0);
- if (!dev)
- goto err_clear_flag;
-
- dev->dev.of_node = of_node_get(node);
- dev->dev.fwnode = &node->fwnode;
- dev->dev.parent = parent ? : &platform_bus;
- dev->dev.platform_data = platform_data;
- if (bus_id)
- dev_set_name(&dev->dev, "%s", bus_id);
- else
- of_device_make_bus_id(&dev->dev);
- of_dma_configure(&dev->dev, dev->dev.of_node);
-
- prop = of_get_property(node, "arm,primecell-periphid", NULL);
- if (prop)
- dev->periphid = of_read_ulong(prop, 1);
-
- for (i = 0; i < AMBA_NR_IRQS; i++)
- dev->irq[i] = irq_of_parse_and_map(node, i);
- ret = of_address_to_resource(node, 0, &dev->res);
- if (ret) {
- pr_err("amba: of_address_to_resource() failed (%d) for %s\n",
- ret, node->full_name);
- goto err_free;
- }
- ret = amba_device_add(dev, &iomem_resource);
- if (ret) {
- pr_err("amba_device_add() failed (%d) for %s\n",
- ret, node->full_name);
- goto err_free;
- }
- return dev;
- err_free:
- amba_device_put(dev);
- err_clear_flag:
- of_node_clear_flag(node, OF_POPULATED);
- return NULL;
- }
- #else
- static struct amba_device *of_amba_device_create(struct device_node *node,
- const char *bus_id,
- void *platform_data,
- struct device *parent)
- {
- return NULL;
- }
- #endif
- static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup,
- struct device_node *np)
- {
- const struct of_dev_auxdata *auxdata;
- struct resource res;
- int compatible = 0;
- if (!lookup)
- return NULL;
- auxdata = lookup;
- for (; auxdata->compatible; auxdata++) {
- if (!of_device_is_compatible(np, auxdata->compatible))
- continue;
- compatible++;
- if (!of_address_to_resource(np, 0, &res))
- if (res.start != auxdata->phys_addr)
- continue;
- pr_debug("%s: devname=%s\n", np->full_name, auxdata->name);
- return auxdata;
- }
- if (!compatible)
- return NULL;
-
- auxdata = lookup;
- for (; auxdata->compatible; auxdata++) {
- if (!of_device_is_compatible(np, auxdata->compatible))
- continue;
- if (!auxdata->phys_addr && !auxdata->name) {
- pr_debug("%s: compatible match\n", np->full_name);
- return auxdata;
- }
- }
- return NULL;
- }
- static int of_platform_bus_create(struct device_node *bus,
- const struct of_device_id *matches,
- const struct of_dev_auxdata *lookup,
- struct device *parent, bool strict)
- {
- const struct of_dev_auxdata *auxdata;
- struct device_node *child;
- struct platform_device *dev;
- const char *bus_id = NULL;
- void *platform_data = NULL;
- int rc = 0;
-
- if (strict && (!of_get_property(bus, "compatible", NULL))) {
- pr_debug("%s() - skipping %s, no compatible prop\n",
- __func__, bus->full_name);
- return 0;
- }
- if (of_node_check_flag(bus, OF_POPULATED_BUS)) {
- pr_debug("%s() - skipping %s, already populated\n",
- __func__, bus->full_name);
- return 0;
- }
- auxdata = of_dev_lookup(lookup, bus);
- if (auxdata) {
- bus_id = auxdata->name;
- platform_data = auxdata->platform_data;
- }
- if (of_device_is_compatible(bus, "arm,primecell")) {
-
- of_amba_device_create(bus, bus_id, platform_data, parent);
- return 0;
- }
- dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent);
- if (!dev || !of_match_node(matches, bus))
- return 0;
- for_each_child_of_node(bus, child) {
- pr_debug(" create child: %s\n", child->full_name);
- rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict);
- if (rc) {
- of_node_put(child);
- break;
- }
- }
- of_node_set_flag(bus, OF_POPULATED_BUS);
- return rc;
- }
- int of_platform_bus_probe(struct device_node *root,
- const struct of_device_id *matches,
- struct device *parent)
- {
- struct device_node *child;
- int rc = 0;
- root = root ? of_node_get(root) : of_find_node_by_path("/");
- if (!root)
- return -EINVAL;
- pr_debug("%s()\n", __func__);
- pr_debug(" starting at: %s\n", root->full_name);
-
- if (of_match_node(matches, root)) {
- rc = of_platform_bus_create(root, matches, NULL, parent, false);
- } else for_each_child_of_node(root, child) {
- if (!of_match_node(matches, child))
- continue;
- rc = of_platform_bus_create(child, matches, NULL, parent, false);
- if (rc) {
- of_node_put(child);
- break;
- }
- }
- of_node_put(root);
- return rc;
- }
- EXPORT_SYMBOL(of_platform_bus_probe);
- int of_platform_populate(struct device_node *root,
- const struct of_device_id *matches,
- const struct of_dev_auxdata *lookup,
- struct device *parent)
- {
- struct device_node *child;
- int rc = 0;
- root = root ? of_node_get(root) : of_find_node_by_path("/");
- if (!root)
- return -EINVAL;
- pr_debug("%s()\n", __func__);
- pr_debug(" starting at: %s\n", root->full_name);
- for_each_child_of_node(root, child) {
- rc = of_platform_bus_create(child, matches, lookup, parent, true);
- if (rc) {
- of_node_put(child);
- break;
- }
- }
- of_node_set_flag(root, OF_POPULATED_BUS);
- of_node_put(root);
- return rc;
- }
- EXPORT_SYMBOL_GPL(of_platform_populate);
- int of_platform_default_populate(struct device_node *root,
- const struct of_dev_auxdata *lookup,
- struct device *parent)
- {
- return of_platform_populate(root, of_default_bus_match_table, lookup,
- parent);
- }
- EXPORT_SYMBOL_GPL(of_platform_default_populate);
- #ifndef CONFIG_PPC
- static int __init of_platform_default_populate_init(void)
- {
- struct device_node *node;
- if (!of_have_populated_dt())
- return -ENODEV;
-
- node = of_find_node_by_path("/reserved-memory");
- if (node) {
- node = of_find_compatible_node(node, NULL, "ramoops");
- if (node)
- of_platform_device_create(node, NULL, NULL);
- }
-
- of_platform_default_populate(NULL, NULL, NULL);
- return 0;
- }
- arch_initcall_sync(of_platform_default_populate_init);
- #endif
- static int of_platform_device_destroy(struct device *dev, void *data)
- {
-
- if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED))
- return 0;
-
- if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS))
- device_for_each_child(dev, NULL, of_platform_device_destroy);
- if (dev->bus == &platform_bus_type)
- platform_device_unregister(to_platform_device(dev));
- #ifdef CONFIG_ARM_AMBA
- else if (dev->bus == &amba_bustype)
- amba_device_unregister(to_amba_device(dev));
- #endif
- of_dma_deconfigure(dev);
- of_node_clear_flag(dev->of_node, OF_POPULATED);
- of_node_clear_flag(dev->of_node, OF_POPULATED_BUS);
- return 0;
- }
- void of_platform_depopulate(struct device *parent)
- {
- if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) {
- device_for_each_child(parent, NULL, of_platform_device_destroy);
- of_node_clear_flag(parent->of_node, OF_POPULATED_BUS);
- }
- }
- EXPORT_SYMBOL_GPL(of_platform_depopulate);
- #ifdef CONFIG_OF_DYNAMIC
- static int of_platform_notify(struct notifier_block *nb,
- unsigned long action, void *arg)
- {
- struct of_reconfig_data *rd = arg;
- struct platform_device *pdev_parent, *pdev;
- bool children_left;
- switch (of_reconfig_get_state_change(action, rd)) {
- case OF_RECONFIG_CHANGE_ADD:
-
- if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS))
- return NOTIFY_OK;
-
- if (of_node_check_flag(rd->dn, OF_POPULATED))
- return NOTIFY_OK;
-
- pdev_parent = of_find_device_by_node(rd->dn->parent);
- pdev = of_platform_device_create(rd->dn, NULL,
- pdev_parent ? &pdev_parent->dev : NULL);
- of_dev_put(pdev_parent);
- if (pdev == NULL) {
- pr_err("%s: failed to create for '%s'\n",
- __func__, rd->dn->full_name);
-
- return notifier_from_errno(-EINVAL);
- }
- break;
- case OF_RECONFIG_CHANGE_REMOVE:
-
- if (!of_node_check_flag(rd->dn, OF_POPULATED))
- return NOTIFY_OK;
-
- pdev = of_find_device_by_node(rd->dn);
- if (pdev == NULL)
- return NOTIFY_OK;
-
- of_platform_device_destroy(&pdev->dev, &children_left);
-
- of_dev_put(pdev);
- break;
- }
- return NOTIFY_OK;
- }
- static struct notifier_block platform_of_notifier = {
- .notifier_call = of_platform_notify,
- };
- void of_platform_register_reconfig_notifier(void)
- {
- WARN_ON(of_reconfig_notifier_register(&platform_of_notifier));
- }
- #endif
- #endif
|