13f23de10SStephen Rothwell /* 23f23de10SStephen Rothwell * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp. 33f23de10SStephen Rothwell * <benh@kernel.crashing.org> 43f23de10SStephen Rothwell * and Arnd Bergmann, IBM Corp. 53f23de10SStephen Rothwell * Merged from powerpc/kernel/of_platform.c and 63f23de10SStephen Rothwell * sparc{,64}/kernel/of_device.c by Stephen Rothwell 73f23de10SStephen Rothwell * 83f23de10SStephen Rothwell * This program is free software; you can redistribute it and/or 93f23de10SStephen Rothwell * modify it under the terms of the GNU General Public License 103f23de10SStephen Rothwell * as published by the Free Software Foundation; either version 113f23de10SStephen Rothwell * 2 of the License, or (at your option) any later version. 123f23de10SStephen Rothwell * 133f23de10SStephen Rothwell */ 14606ad42aSRob Herring 15606ad42aSRob Herring #define pr_fmt(fmt) "OF: " fmt 16606ad42aSRob Herring 173f23de10SStephen Rothwell #include <linux/errno.h> 185c457083SStephen Rothwell #include <linux/module.h> 195de1540bSGrant Likely #include <linux/amba/bus.h> 203f23de10SStephen Rothwell #include <linux/device.h> 215fd200f3SGrant Likely #include <linux/dma-mapping.h> 2250ef5284SGrant Likely #include <linux/slab.h> 239e3288dcSGrant Likely #include <linux/of_address.h> 243f23de10SStephen Rothwell #include <linux/of_device.h> 259e3288dcSGrant Likely #include <linux/of_irq.h> 263f23de10SStephen Rothwell #include <linux/of_platform.h> 27eca39301SGrant Likely #include <linux/platform_device.h> 28eca39301SGrant Likely 29cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = { 30cbb49c26SGrant Likely { .compatible = "simple-bus", }, 3122869a9eSLinus Walleij { .compatible = "simple-mfd", }, 32ecd76edeSPaul Burton { .compatible = "isa", }, 33cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA 34cbb49c26SGrant Likely { .compatible = "arm,amba-bus", }, 35cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */ 36cbb49c26SGrant Likely {} /* Empty terminated list */ 37cbb49c26SGrant Likely }; 38cbb49c26SGrant Likely 39c6085584SJonas Bonn static int of_dev_node_match(struct device *dev, void *data) 40c6085584SJonas Bonn { 41c6085584SJonas Bonn return dev->of_node == data; 42c6085584SJonas Bonn } 43c6085584SJonas Bonn 44c6085584SJonas Bonn /** 45c6085584SJonas Bonn * of_find_device_by_node - Find the platform_device associated with a node 46c6085584SJonas Bonn * @np: Pointer to device tree node 47c6085584SJonas Bonn * 484fb373dfSJohan Hovold * Takes a reference to the embedded struct device which needs to be dropped 494fb373dfSJohan Hovold * after use. 504fb373dfSJohan Hovold * 51c6085584SJonas Bonn * Returns platform_device pointer, or NULL if not found 52c6085584SJonas Bonn */ 53c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np) 54c6085584SJonas Bonn { 55c6085584SJonas Bonn struct device *dev; 56c6085584SJonas Bonn 57c6085584SJonas Bonn dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match); 58c6085584SJonas Bonn return dev ? to_platform_device(dev) : NULL; 59c6085584SJonas Bonn } 60c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node); 61c6085584SJonas Bonn 62964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS 635fd200f3SGrant Likely /* 645fd200f3SGrant Likely * The following routines scan a subtree and registers a device for 655fd200f3SGrant Likely * each applicable node. 665fd200f3SGrant Likely * 675fd200f3SGrant Likely * Note: sparc doesn't use these routines because it has a different 685fd200f3SGrant Likely * mechanism for creating devices from device tree nodes. 695fd200f3SGrant Likely */ 705fd200f3SGrant Likely 715fd200f3SGrant Likely /** 7294c09319SGrant Likely * of_device_make_bus_id - Use the device node data to assign a unique name 7394c09319SGrant Likely * @dev: pointer to device structure that is linked to a device tree node 7494c09319SGrant Likely * 7507e461cdSGrant Likely * This routine will first try using the translated bus address to 7607e461cdSGrant Likely * derive a unique name. If it cannot, then it will prepend names from 7707e461cdSGrant Likely * parent nodes until a unique name can be derived. 7894c09319SGrant Likely */ 79c6601225SGrant Likely void of_device_make_bus_id(struct device *dev) 8094c09319SGrant Likely { 8194c09319SGrant Likely struct device_node *node = dev->of_node; 8224fb530fSKim Phillips const __be32 *reg; 8394c09319SGrant Likely u64 addr; 8494c09319SGrant Likely 8507e461cdSGrant Likely /* Construct the name, using parent nodes if necessary to ensure uniqueness */ 8607e461cdSGrant Likely while (node->parent) { 8794c09319SGrant Likely /* 8807e461cdSGrant Likely * If the address can be translated, then that is as much 8907e461cdSGrant Likely * uniqueness as we need. Make it the first component and return 9094c09319SGrant Likely */ 9194c09319SGrant Likely reg = of_get_property(node, "reg", NULL); 9207e461cdSGrant Likely if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) { 9307e461cdSGrant Likely dev_set_name(dev, dev_name(dev) ? "%llx.%s:%s" : "%llx.%s", 9407e461cdSGrant Likely (unsigned long long)addr, node->name, 9507e461cdSGrant Likely dev_name(dev)); 9694c09319SGrant Likely return; 9794c09319SGrant Likely } 9894c09319SGrant Likely 9907e461cdSGrant Likely /* format arguments only used if dev_name() resolves to NULL */ 10007e461cdSGrant Likely dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s", 10107e461cdSGrant Likely strrchr(node->full_name, '/') + 1, dev_name(dev)); 10207e461cdSGrant Likely node = node->parent; 10307e461cdSGrant Likely } 10494c09319SGrant Likely } 10594c09319SGrant Likely 10694c09319SGrant Likely /** 10794c09319SGrant Likely * of_device_alloc - Allocate and initialize an of_device 10894c09319SGrant Likely * @np: device node to assign to device 10994c09319SGrant Likely * @bus_id: Name to assign to the device. May be null to use default name. 11094c09319SGrant Likely * @parent: Parent device. 11194c09319SGrant Likely */ 11294a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np, 11394c09319SGrant Likely const char *bus_id, 11494c09319SGrant Likely struct device *parent) 11594c09319SGrant Likely { 11694a0cb1fSGrant Likely struct platform_device *dev; 11752f6537cSAndres Salomon int rc, i, num_reg = 0, num_irq; 118ac80a51eSGrant Likely struct resource *res, temp_res; 11994c09319SGrant Likely 1207096d042SGrant Likely dev = platform_device_alloc("", -1); 1217096d042SGrant Likely if (!dev) 1227096d042SGrant Likely return NULL; 1237096d042SGrant Likely 1247096d042SGrant Likely /* count the io and irq resources */ 125ac80a51eSGrant Likely while (of_address_to_resource(np, num_reg, &temp_res) == 0) 126ac80a51eSGrant Likely num_reg++; 12752f6537cSAndres Salomon num_irq = of_irq_count(np); 128ac80a51eSGrant Likely 129ac80a51eSGrant Likely /* Populate the resource table */ 130ac80a51eSGrant Likely if (num_irq || num_reg) { 1317096d042SGrant Likely res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL); 1327096d042SGrant Likely if (!res) { 1337096d042SGrant Likely platform_device_put(dev); 1347096d042SGrant Likely return NULL; 1357096d042SGrant Likely } 1367096d042SGrant Likely 137ac80a51eSGrant Likely dev->num_resources = num_reg + num_irq; 138ac80a51eSGrant Likely dev->resource = res; 139ac80a51eSGrant Likely for (i = 0; i < num_reg; i++, res++) { 140ac80a51eSGrant Likely rc = of_address_to_resource(np, i, res); 141ac80a51eSGrant Likely WARN_ON(rc); 142ac80a51eSGrant Likely } 1439ec36cafSRob Herring if (of_irq_to_resource_table(np, res, num_irq) != num_irq) 1449ec36cafSRob Herring pr_debug("not all legacy IRQ resources mapped for %s\n", 1459ec36cafSRob Herring np->name); 146ac80a51eSGrant Likely } 14794c09319SGrant Likely 14894c09319SGrant Likely dev->dev.of_node = of_node_get(np); 149f94277afSRobin Murphy dev->dev.fwnode = &np->fwnode; 15043c0767eSGrant Likely dev->dev.parent = parent ? : &platform_bus; 15194c09319SGrant Likely 15294c09319SGrant Likely if (bus_id) 15394c09319SGrant Likely dev_set_name(&dev->dev, "%s", bus_id); 15494c09319SGrant Likely else 15594c09319SGrant Likely of_device_make_bus_id(&dev->dev); 15694c09319SGrant Likely 15794c09319SGrant Likely return dev; 15894c09319SGrant Likely } 15994c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc); 16094c09319SGrant Likely 16197890ba9SWill Deacon static void of_dma_deconfigure(struct device *dev) 16297890ba9SWill Deacon { 16397890ba9SWill Deacon arch_teardown_dma_ops(dev); 164591c1ee4SSantosh Shilimkar } 165591c1ee4SSantosh Shilimkar 166591c1ee4SSantosh Shilimkar /** 16715c3597dSGrant Likely * of_platform_device_create_pdata - Alloc, initialize and register an of_device 1685fd200f3SGrant Likely * @np: pointer to node to create device for 1695fd200f3SGrant Likely * @bus_id: name to assign device 17015c3597dSGrant Likely * @platform_data: pointer to populate platform_data pointer with 1715fd200f3SGrant Likely * @parent: Linux device model parent device. 172cd1e6504SGrant Likely * 173cd1e6504SGrant Likely * Returns pointer to created platform device, or NULL if a device was not 174cd1e6504SGrant Likely * registered. Unavailable devices will not get registered. 1755fd200f3SGrant Likely */ 176245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata( 17715c3597dSGrant Likely struct device_node *np, 1785fd200f3SGrant Likely const char *bus_id, 17915c3597dSGrant Likely void *platform_data, 1805fd200f3SGrant Likely struct device *parent) 1815fd200f3SGrant Likely { 18294a0cb1fSGrant Likely struct platform_device *dev; 1835fd200f3SGrant Likely 184c6e126deSPawel Moll if (!of_device_is_available(np) || 185c6e126deSPawel Moll of_node_test_and_set_flag(np, OF_POPULATED)) 186cd1e6504SGrant Likely return NULL; 187cd1e6504SGrant Likely 1885fd200f3SGrant Likely dev = of_device_alloc(np, bus_id, parent); 1895fd200f3SGrant Likely if (!dev) 190c6e126deSPawel Moll goto err_clear_flag; 1915fd200f3SGrant Likely 192eca39301SGrant Likely dev->dev.bus = &platform_bus_type; 19315c3597dSGrant Likely dev->dev.platform_data = platform_data; 1941f5c69aaSMurali Karicheri of_dma_configure(&dev->dev, dev->dev.of_node); 195c706c239SMarc Zyngier of_msi_configure(&dev->dev, dev->dev.of_node); 1965fd200f3SGrant Likely 1977096d042SGrant Likely if (of_device_add(dev) != 0) { 19897890ba9SWill Deacon of_dma_deconfigure(&dev->dev); 1997096d042SGrant Likely platform_device_put(dev); 200c6e126deSPawel Moll goto err_clear_flag; 2015fd200f3SGrant Likely } 2025fd200f3SGrant Likely 2035fd200f3SGrant Likely return dev; 204c6e126deSPawel Moll 205c6e126deSPawel Moll err_clear_flag: 206c6e126deSPawel Moll of_node_clear_flag(np, OF_POPULATED); 207c6e126deSPawel Moll return NULL; 2085fd200f3SGrant Likely } 20915c3597dSGrant Likely 21015c3597dSGrant Likely /** 21115c3597dSGrant Likely * of_platform_device_create - Alloc, initialize and register an of_device 21215c3597dSGrant Likely * @np: pointer to node to create device for 21315c3597dSGrant Likely * @bus_id: name to assign device 21415c3597dSGrant Likely * @parent: Linux device model parent device. 21515c3597dSGrant Likely * 21615c3597dSGrant Likely * Returns pointer to created platform device, or NULL if a device was not 21715c3597dSGrant Likely * registered. Unavailable devices will not get registered. 21815c3597dSGrant Likely */ 21915c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np, 22015c3597dSGrant Likely const char *bus_id, 22115c3597dSGrant Likely struct device *parent) 22215c3597dSGrant Likely { 22315c3597dSGrant Likely return of_platform_device_create_pdata(np, bus_id, NULL, parent); 22415c3597dSGrant Likely } 2255fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create); 2265fd200f3SGrant Likely 2275de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA 2285de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node, 2295de1540bSGrant Likely const char *bus_id, 2305de1540bSGrant Likely void *platform_data, 2315de1540bSGrant Likely struct device *parent) 2325de1540bSGrant Likely { 2335de1540bSGrant Likely struct amba_device *dev; 2345de1540bSGrant Likely const void *prop; 2355de1540bSGrant Likely int i, ret; 2365de1540bSGrant Likely 2375de1540bSGrant Likely pr_debug("Creating amba device %s\n", node->full_name); 2385de1540bSGrant Likely 239c6e126deSPawel Moll if (!of_device_is_available(node) || 240c6e126deSPawel Moll of_node_test_and_set_flag(node, OF_POPULATED)) 2415de1540bSGrant Likely return NULL; 2425de1540bSGrant Likely 243c0f72f8aSRussell King dev = amba_device_alloc(NULL, 0, 0); 244606ad42aSRob Herring if (!dev) 245c6e126deSPawel Moll goto err_clear_flag; 2465de1540bSGrant Likely 2475de1540bSGrant Likely /* setup generic device info */ 2485de1540bSGrant Likely dev->dev.of_node = of_node_get(node); 249f94277afSRobin Murphy dev->dev.fwnode = &node->fwnode; 25043c0767eSGrant Likely dev->dev.parent = parent ? : &platform_bus; 2515de1540bSGrant Likely dev->dev.platform_data = platform_data; 2525de1540bSGrant Likely if (bus_id) 2535de1540bSGrant Likely dev_set_name(&dev->dev, "%s", bus_id); 2545de1540bSGrant Likely else 2555de1540bSGrant Likely of_device_make_bus_id(&dev->dev); 2561f5c69aaSMurali Karicheri of_dma_configure(&dev->dev, dev->dev.of_node); 2575de1540bSGrant Likely 2585de1540bSGrant Likely /* Allow the HW Peripheral ID to be overridden */ 2595de1540bSGrant Likely prop = of_get_property(node, "arm,primecell-periphid", NULL); 2605de1540bSGrant Likely if (prop) 2615de1540bSGrant Likely dev->periphid = of_read_ulong(prop, 1); 2625de1540bSGrant Likely 2635de1540bSGrant Likely /* Decode the IRQs and address ranges */ 2645de1540bSGrant Likely for (i = 0; i < AMBA_NR_IRQS; i++) 2655de1540bSGrant Likely dev->irq[i] = irq_of_parse_and_map(node, i); 2665de1540bSGrant Likely 2675de1540bSGrant Likely ret = of_address_to_resource(node, 0, &dev->res); 2682bc552dfSBartlomiej Zolnierkiewicz if (ret) { 269606ad42aSRob Herring pr_err("amba: of_address_to_resource() failed (%d) for %s\n", 270606ad42aSRob Herring ret, node->full_name); 2715de1540bSGrant Likely goto err_free; 2722bc552dfSBartlomiej Zolnierkiewicz } 2735de1540bSGrant Likely 274c0f72f8aSRussell King ret = amba_device_add(dev, &iomem_resource); 2752bc552dfSBartlomiej Zolnierkiewicz if (ret) { 276606ad42aSRob Herring pr_err("amba_device_add() failed (%d) for %s\n", 277606ad42aSRob Herring ret, node->full_name); 2785de1540bSGrant Likely goto err_free; 2792bc552dfSBartlomiej Zolnierkiewicz } 2805de1540bSGrant Likely 2815de1540bSGrant Likely return dev; 2825de1540bSGrant Likely 2835de1540bSGrant Likely err_free: 284c0f72f8aSRussell King amba_device_put(dev); 285c6e126deSPawel Moll err_clear_flag: 286c6e126deSPawel Moll of_node_clear_flag(node, OF_POPULATED); 2875de1540bSGrant Likely return NULL; 2885de1540bSGrant Likely } 2895de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */ 2905de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node, 2915de1540bSGrant Likely const char *bus_id, 2925de1540bSGrant Likely void *platform_data, 2935de1540bSGrant Likely struct device *parent) 2945de1540bSGrant Likely { 2955de1540bSGrant Likely return NULL; 2965de1540bSGrant Likely } 2975de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */ 2985de1540bSGrant Likely 2995fd200f3SGrant Likely /** 30015c3597dSGrant Likely * of_devname_lookup() - Given a device node, lookup the preferred Linux name 30115c3597dSGrant Likely */ 30215c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup, 30315c3597dSGrant Likely struct device_node *np) 30415c3597dSGrant Likely { 305fc5cf80aSTony Lindgren const struct of_dev_auxdata *auxdata; 30615c3597dSGrant Likely struct resource res; 307fc5cf80aSTony Lindgren int compatible = 0; 308303f59d1SOlof Johansson 309303f59d1SOlof Johansson if (!lookup) 310303f59d1SOlof Johansson return NULL; 311303f59d1SOlof Johansson 312fc5cf80aSTony Lindgren auxdata = lookup; 313fc5cf80aSTony Lindgren for (; auxdata->compatible; auxdata++) { 314fc5cf80aSTony Lindgren if (!of_device_is_compatible(np, auxdata->compatible)) 31515c3597dSGrant Likely continue; 316fc5cf80aSTony Lindgren compatible++; 31784774e61SLee Jones if (!of_address_to_resource(np, 0, &res)) 318fc5cf80aSTony Lindgren if (res.start != auxdata->phys_addr) 31915c3597dSGrant Likely continue; 320fc5cf80aSTony Lindgren pr_debug("%s: devname=%s\n", np->full_name, auxdata->name); 321fc5cf80aSTony Lindgren return auxdata; 322fc5cf80aSTony Lindgren } 323fc5cf80aSTony Lindgren 324fc5cf80aSTony Lindgren if (!compatible) 325fc5cf80aSTony Lindgren return NULL; 326fc5cf80aSTony Lindgren 327fc5cf80aSTony Lindgren /* Try compatible match if no phys_addr and name are specified */ 328fc5cf80aSTony Lindgren auxdata = lookup; 329fc5cf80aSTony Lindgren for (; auxdata->compatible; auxdata++) { 330fc5cf80aSTony Lindgren if (!of_device_is_compatible(np, auxdata->compatible)) 331fc5cf80aSTony Lindgren continue; 332fc5cf80aSTony Lindgren if (!auxdata->phys_addr && !auxdata->name) { 333fc5cf80aSTony Lindgren pr_debug("%s: compatible match\n", np->full_name); 334fc5cf80aSTony Lindgren return auxdata; 335fc5cf80aSTony Lindgren } 33615c3597dSGrant Likely } 337303f59d1SOlof Johansson 33815c3597dSGrant Likely return NULL; 33915c3597dSGrant Likely } 34015c3597dSGrant Likely 34115c3597dSGrant Likely /** 34238e9e21dSGrant Likely * of_platform_bus_create() - Create a device for a node and its children. 3435fd200f3SGrant Likely * @bus: device node of the bus to instantiate 3441eed4c07SGrant Likely * @matches: match table for bus nodes 345303f59d1SOlof Johansson * @lookup: auxdata table for matching id and platform_data with device nodes 34638e9e21dSGrant Likely * @parent: parent for new device, or NULL for top level. 347303f59d1SOlof Johansson * @strict: require compatible property 34838e9e21dSGrant Likely * 34938e9e21dSGrant Likely * Creates a platform_device for the provided device_node, and optionally 35038e9e21dSGrant Likely * recursively create devices for all the child nodes. 3515fd200f3SGrant Likely */ 35238e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus, 3535fd200f3SGrant Likely const struct of_device_id *matches, 35415c3597dSGrant Likely const struct of_dev_auxdata *lookup, 35529d4f8a4SGrant Likely struct device *parent, bool strict) 3565fd200f3SGrant Likely { 35715c3597dSGrant Likely const struct of_dev_auxdata *auxdata; 3585fd200f3SGrant Likely struct device_node *child; 35994a0cb1fSGrant Likely struct platform_device *dev; 36015c3597dSGrant Likely const char *bus_id = NULL; 36115c3597dSGrant Likely void *platform_data = NULL; 3625fd200f3SGrant Likely int rc = 0; 3635fd200f3SGrant Likely 36429d4f8a4SGrant Likely /* Make sure it has a compatible property */ 36529d4f8a4SGrant Likely if (strict && (!of_get_property(bus, "compatible", NULL))) { 36629d4f8a4SGrant Likely pr_debug("%s() - skipping %s, no compatible prop\n", 36729d4f8a4SGrant Likely __func__, bus->full_name); 36829d4f8a4SGrant Likely return 0; 36929d4f8a4SGrant Likely } 37029d4f8a4SGrant Likely 37144a7185cSKefeng Wang if (of_node_check_flag(bus, OF_POPULATED_BUS)) { 37244a7185cSKefeng Wang pr_debug("%s() - skipping %s, already populated\n", 37344a7185cSKefeng Wang __func__, bus->full_name); 37444a7185cSKefeng Wang return 0; 37544a7185cSKefeng Wang } 37644a7185cSKefeng Wang 37715c3597dSGrant Likely auxdata = of_dev_lookup(lookup, bus); 37815c3597dSGrant Likely if (auxdata) { 37915c3597dSGrant Likely bus_id = auxdata->name; 38015c3597dSGrant Likely platform_data = auxdata->platform_data; 38115c3597dSGrant Likely } 38215c3597dSGrant Likely 3835de1540bSGrant Likely if (of_device_is_compatible(bus, "arm,primecell")) { 3842bc552dfSBartlomiej Zolnierkiewicz /* 3852bc552dfSBartlomiej Zolnierkiewicz * Don't return an error here to keep compatibility with older 3862bc552dfSBartlomiej Zolnierkiewicz * device tree files. 3872bc552dfSBartlomiej Zolnierkiewicz */ 38815c3597dSGrant Likely of_amba_device_create(bus, bus_id, platform_data, parent); 3895de1540bSGrant Likely return 0; 3905de1540bSGrant Likely } 3915de1540bSGrant Likely 39215c3597dSGrant Likely dev = of_platform_device_create_pdata(bus, bus_id, platform_data, parent); 39338e9e21dSGrant Likely if (!dev || !of_match_node(matches, bus)) 39438e9e21dSGrant Likely return 0; 39538e9e21dSGrant Likely 3965fd200f3SGrant Likely for_each_child_of_node(bus, child) { 3975fd200f3SGrant Likely pr_debug(" create child: %s\n", child->full_name); 39815c3597dSGrant Likely rc = of_platform_bus_create(child, matches, lookup, &dev->dev, strict); 3995fd200f3SGrant Likely if (rc) { 4005fd200f3SGrant Likely of_node_put(child); 4015fd200f3SGrant Likely break; 4025fd200f3SGrant Likely } 4035fd200f3SGrant Likely } 40475f353b6SGrant Likely of_node_set_flag(bus, OF_POPULATED_BUS); 4055fd200f3SGrant Likely return rc; 4065fd200f3SGrant Likely } 4075fd200f3SGrant Likely 4085fd200f3SGrant Likely /** 40938e9e21dSGrant Likely * of_platform_bus_probe() - Probe the device-tree for platform buses 4105fd200f3SGrant Likely * @root: parent of the first level to probe or NULL for the root of the tree 4111eed4c07SGrant Likely * @matches: match table for bus nodes 4125fd200f3SGrant Likely * @parent: parent to hook devices from, NULL for toplevel 4135fd200f3SGrant Likely * 4145fd200f3SGrant Likely * Note that children of the provided root are not instantiated as devices 4155fd200f3SGrant Likely * unless the specified root itself matches the bus list and is not NULL. 4165fd200f3SGrant Likely */ 4175fd200f3SGrant Likely int of_platform_bus_probe(struct device_node *root, 4185fd200f3SGrant Likely const struct of_device_id *matches, 4195fd200f3SGrant Likely struct device *parent) 4205fd200f3SGrant Likely { 4215fd200f3SGrant Likely struct device_node *child; 4225fd200f3SGrant Likely int rc = 0; 4235fd200f3SGrant Likely 4241eed4c07SGrant Likely root = root ? of_node_get(root) : of_find_node_by_path("/"); 4251eed4c07SGrant Likely if (!root) 42660d59913SGrant Likely return -EINVAL; 4275fd200f3SGrant Likely 42844a7185cSKefeng Wang pr_debug("%s()\n", __func__); 4295fd200f3SGrant Likely pr_debug(" starting at: %s\n", root->full_name); 4305fd200f3SGrant Likely 4311eed4c07SGrant Likely /* Do a self check of bus type, if there's a match, create children */ 4325fd200f3SGrant Likely if (of_match_node(matches, root)) { 43315c3597dSGrant Likely rc = of_platform_bus_create(root, matches, NULL, parent, false); 43438e9e21dSGrant Likely } else for_each_child_of_node(root, child) { 4355fd200f3SGrant Likely if (!of_match_node(matches, child)) 4365fd200f3SGrant Likely continue; 43715c3597dSGrant Likely rc = of_platform_bus_create(child, matches, NULL, parent, false); 4387fad948aSJulia Lawall if (rc) { 4397fad948aSJulia Lawall of_node_put(child); 4405fd200f3SGrant Likely break; 4415fd200f3SGrant Likely } 4427fad948aSJulia Lawall } 44338e9e21dSGrant Likely 4445fd200f3SGrant Likely of_node_put(root); 4455fd200f3SGrant Likely return rc; 4465fd200f3SGrant Likely } 4475fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_bus_probe); 44829d4f8a4SGrant Likely 44929d4f8a4SGrant Likely /** 45029d4f8a4SGrant Likely * of_platform_populate() - Populate platform_devices from device tree data 45129d4f8a4SGrant Likely * @root: parent of the first level to probe or NULL for the root of the tree 45229d4f8a4SGrant Likely * @matches: match table, NULL to use the default 45392039ed1SJavi Merino * @lookup: auxdata table for matching id and platform_data with device nodes 45429d4f8a4SGrant Likely * @parent: parent to hook devices from, NULL for toplevel 45529d4f8a4SGrant Likely * 45629d4f8a4SGrant Likely * Similar to of_platform_bus_probe(), this function walks the device tree 45729d4f8a4SGrant Likely * and creates devices from nodes. It differs in that it follows the modern 45829d4f8a4SGrant Likely * convention of requiring all device nodes to have a 'compatible' property, 45929d4f8a4SGrant Likely * and it is suitable for creating devices which are children of the root 46029d4f8a4SGrant Likely * node (of_platform_bus_probe will only create children of the root which 46129d4f8a4SGrant Likely * are selected by the @matches argument). 46229d4f8a4SGrant Likely * 46329d4f8a4SGrant Likely * New board support should be using this function instead of 46429d4f8a4SGrant Likely * of_platform_bus_probe(). 46529d4f8a4SGrant Likely * 46629d4f8a4SGrant Likely * Returns 0 on success, < 0 on failure. 46729d4f8a4SGrant Likely */ 46829d4f8a4SGrant Likely int of_platform_populate(struct device_node *root, 46929d4f8a4SGrant Likely const struct of_device_id *matches, 47015c3597dSGrant Likely const struct of_dev_auxdata *lookup, 47129d4f8a4SGrant Likely struct device *parent) 47229d4f8a4SGrant Likely { 47329d4f8a4SGrant Likely struct device_node *child; 47429d4f8a4SGrant Likely int rc = 0; 47529d4f8a4SGrant Likely 47629d4f8a4SGrant Likely root = root ? of_node_get(root) : of_find_node_by_path("/"); 47729d4f8a4SGrant Likely if (!root) 47829d4f8a4SGrant Likely return -EINVAL; 47929d4f8a4SGrant Likely 48044a7185cSKefeng Wang pr_debug("%s()\n", __func__); 48144a7185cSKefeng Wang pr_debug(" starting at: %s\n", root->full_name); 48244a7185cSKefeng Wang 48329d4f8a4SGrant Likely for_each_child_of_node(root, child) { 48415c3597dSGrant Likely rc = of_platform_bus_create(child, matches, lookup, parent, true); 4857fad948aSJulia Lawall if (rc) { 4867fad948aSJulia Lawall of_node_put(child); 48729d4f8a4SGrant Likely break; 48829d4f8a4SGrant Likely } 4897fad948aSJulia Lawall } 4902d0747c4SGrant Likely of_node_set_flag(root, OF_POPULATED_BUS); 49129d4f8a4SGrant Likely 49229d4f8a4SGrant Likely of_node_put(root); 49329d4f8a4SGrant Likely return rc; 49429d4f8a4SGrant Likely } 495e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate); 496c6e126deSPawel Moll 49743443ad6SHauke Mehrtens int of_platform_default_populate(struct device_node *root, 49843443ad6SHauke Mehrtens const struct of_dev_auxdata *lookup, 49943443ad6SHauke Mehrtens struct device *parent) 50043443ad6SHauke Mehrtens { 50143443ad6SHauke Mehrtens return of_platform_populate(root, of_default_bus_match_table, lookup, 50243443ad6SHauke Mehrtens parent); 50343443ad6SHauke Mehrtens } 50443443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate); 50543443ad6SHauke Mehrtens 506fc520f8bSKevin Hao #ifndef CONFIG_PPC 50744a7185cSKefeng Wang static int __init of_platform_default_populate_init(void) 50844a7185cSKefeng Wang { 509529182e2SKees Cook struct device_node *node; 510529182e2SKees Cook 511529182e2SKees Cook if (!of_have_populated_dt()) 512529182e2SKees Cook return -ENODEV; 513529182e2SKees Cook 514529182e2SKees Cook /* 515529182e2SKees Cook * Handle ramoops explicitly, since it is inside /reserved-memory, 516529182e2SKees Cook * which lacks a "compatible" property. 517529182e2SKees Cook */ 518529182e2SKees Cook node = of_find_node_by_path("/reserved-memory"); 519529182e2SKees Cook if (node) { 520529182e2SKees Cook node = of_find_compatible_node(node, NULL, "ramoops"); 521529182e2SKees Cook if (node) 522529182e2SKees Cook of_platform_device_create(node, NULL, NULL); 523529182e2SKees Cook } 524529182e2SKees Cook 525529182e2SKees Cook /* Populate everything else. */ 52644a7185cSKefeng Wang of_platform_default_populate(NULL, NULL, NULL); 52744a7185cSKefeng Wang 52844a7185cSKefeng Wang return 0; 52944a7185cSKefeng Wang } 53044a7185cSKefeng Wang arch_initcall_sync(of_platform_default_populate_init); 531fc520f8bSKevin Hao #endif 53244a7185cSKefeng Wang 533c6e126deSPawel Moll static int of_platform_device_destroy(struct device *dev, void *data) 534c6e126deSPawel Moll { 535c6e126deSPawel Moll /* Do not touch devices not populated from the device tree */ 53675f353b6SGrant Likely if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED)) 537c6e126deSPawel Moll return 0; 538c6e126deSPawel Moll 53975f353b6SGrant Likely /* Recurse for any nodes that were treated as busses */ 54075f353b6SGrant Likely if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS)) 54175f353b6SGrant Likely device_for_each_child(dev, NULL, of_platform_device_destroy); 542c6e126deSPawel Moll 543c6e126deSPawel Moll if (dev->bus == &platform_bus_type) 544c6e126deSPawel Moll platform_device_unregister(to_platform_device(dev)); 545c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA 546c6e126deSPawel Moll else if (dev->bus == &amba_bustype) 547c6e126deSPawel Moll amba_device_unregister(to_amba_device(dev)); 548c6e126deSPawel Moll #endif 549c6e126deSPawel Moll 5500495cb75SWill Deacon of_dma_deconfigure(dev); 551c6e126deSPawel Moll of_node_clear_flag(dev->of_node, OF_POPULATED); 55275f353b6SGrant Likely of_node_clear_flag(dev->of_node, OF_POPULATED_BUS); 553c6e126deSPawel Moll return 0; 554c6e126deSPawel Moll } 555c6e126deSPawel Moll 556c6e126deSPawel Moll /** 557c6e126deSPawel Moll * of_platform_depopulate() - Remove devices populated from device tree 55875f353b6SGrant Likely * @parent: device which children will be removed 559c6e126deSPawel Moll * 560c6e126deSPawel Moll * Complementary to of_platform_populate(), this function removes children 561c6e126deSPawel Moll * of the given device (and, recurrently, their children) that have been 562c6e126deSPawel Moll * created from their respective device tree nodes (and only those, 563c6e126deSPawel Moll * leaving others - eg. manually created - unharmed). 564c6e126deSPawel Moll */ 56575f353b6SGrant Likely void of_platform_depopulate(struct device *parent) 566c6e126deSPawel Moll { 5672d0747c4SGrant Likely if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) { 56875f353b6SGrant Likely device_for_each_child(parent, NULL, of_platform_device_destroy); 5692d0747c4SGrant Likely of_node_clear_flag(parent->of_node, OF_POPULATED_BUS); 5702d0747c4SGrant Likely } 571c6e126deSPawel Moll } 572c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate); 573c6e126deSPawel Moll 574*38b0b219SBenjamin Gaignard static void devm_of_platform_populate_release(struct device *dev, void *res) 575*38b0b219SBenjamin Gaignard { 576*38b0b219SBenjamin Gaignard of_platform_depopulate(*(struct device **)res); 577*38b0b219SBenjamin Gaignard } 578*38b0b219SBenjamin Gaignard 579*38b0b219SBenjamin Gaignard /** 580*38b0b219SBenjamin Gaignard * devm_of_platform_populate() - Populate platform_devices from device tree data 581*38b0b219SBenjamin Gaignard * @dev: device that requested to populate from device tree data 582*38b0b219SBenjamin Gaignard * 583*38b0b219SBenjamin Gaignard * Similar to of_platform_populate(), but will automatically call 584*38b0b219SBenjamin Gaignard * of_platform_depopulate() when the device is unbound from the bus. 585*38b0b219SBenjamin Gaignard * 586*38b0b219SBenjamin Gaignard * Returns 0 on success, < 0 on failure. 587*38b0b219SBenjamin Gaignard */ 588*38b0b219SBenjamin Gaignard int devm_of_platform_populate(struct device *dev) 589*38b0b219SBenjamin Gaignard { 590*38b0b219SBenjamin Gaignard struct device **ptr; 591*38b0b219SBenjamin Gaignard int ret; 592*38b0b219SBenjamin Gaignard 593*38b0b219SBenjamin Gaignard if (!dev) 594*38b0b219SBenjamin Gaignard return -EINVAL; 595*38b0b219SBenjamin Gaignard 596*38b0b219SBenjamin Gaignard ptr = devres_alloc(devm_of_platform_populate_release, 597*38b0b219SBenjamin Gaignard sizeof(*ptr), GFP_KERNEL); 598*38b0b219SBenjamin Gaignard if (!ptr) 599*38b0b219SBenjamin Gaignard return -ENOMEM; 600*38b0b219SBenjamin Gaignard 601*38b0b219SBenjamin Gaignard ret = of_platform_populate(dev->of_node, NULL, NULL, dev); 602*38b0b219SBenjamin Gaignard if (ret) { 603*38b0b219SBenjamin Gaignard devres_free(ptr); 604*38b0b219SBenjamin Gaignard } else { 605*38b0b219SBenjamin Gaignard *ptr = dev; 606*38b0b219SBenjamin Gaignard devres_add(dev, ptr); 607*38b0b219SBenjamin Gaignard } 608*38b0b219SBenjamin Gaignard 609*38b0b219SBenjamin Gaignard return ret; 610*38b0b219SBenjamin Gaignard } 611*38b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_populate); 612*38b0b219SBenjamin Gaignard 613*38b0b219SBenjamin Gaignard static int devm_of_platform_match(struct device *dev, void *res, void *data) 614*38b0b219SBenjamin Gaignard { 615*38b0b219SBenjamin Gaignard struct device **ptr = res; 616*38b0b219SBenjamin Gaignard 617*38b0b219SBenjamin Gaignard if (!ptr) { 618*38b0b219SBenjamin Gaignard WARN_ON(!ptr); 619*38b0b219SBenjamin Gaignard return 0; 620*38b0b219SBenjamin Gaignard } 621*38b0b219SBenjamin Gaignard 622*38b0b219SBenjamin Gaignard return *ptr == data; 623*38b0b219SBenjamin Gaignard } 624*38b0b219SBenjamin Gaignard 625*38b0b219SBenjamin Gaignard /** 626*38b0b219SBenjamin Gaignard * devm_of_platform_depopulate() - Remove devices populated from device tree 627*38b0b219SBenjamin Gaignard * @dev: device that requested to depopulate from device tree data 628*38b0b219SBenjamin Gaignard * 629*38b0b219SBenjamin Gaignard * Complementary to devm_of_platform_populate(), this function removes children 630*38b0b219SBenjamin Gaignard * of the given device (and, recurrently, their children) that have been 631*38b0b219SBenjamin Gaignard * created from their respective device tree nodes (and only those, 632*38b0b219SBenjamin Gaignard * leaving others - eg. manually created - unharmed). 633*38b0b219SBenjamin Gaignard */ 634*38b0b219SBenjamin Gaignard void devm_of_platform_depopulate(struct device *dev) 635*38b0b219SBenjamin Gaignard { 636*38b0b219SBenjamin Gaignard int ret; 637*38b0b219SBenjamin Gaignard 638*38b0b219SBenjamin Gaignard ret = devres_release(dev, devm_of_platform_populate_release, 639*38b0b219SBenjamin Gaignard devm_of_platform_match, dev); 640*38b0b219SBenjamin Gaignard 641*38b0b219SBenjamin Gaignard WARN_ON(ret); 642*38b0b219SBenjamin Gaignard } 643*38b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_depopulate); 644*38b0b219SBenjamin Gaignard 645801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC 646801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb, 647801d728cSPantelis Antoniou unsigned long action, void *arg) 648801d728cSPantelis Antoniou { 649801d728cSPantelis Antoniou struct of_reconfig_data *rd = arg; 650801d728cSPantelis Antoniou struct platform_device *pdev_parent, *pdev; 651801d728cSPantelis Antoniou bool children_left; 652801d728cSPantelis Antoniou 653801d728cSPantelis Antoniou switch (of_reconfig_get_state_change(action, rd)) { 654801d728cSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 655801d728cSPantelis Antoniou /* verify that the parent is a bus */ 656801d728cSPantelis Antoniou if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS)) 657801d728cSPantelis Antoniou return NOTIFY_OK; /* not for us */ 658801d728cSPantelis Antoniou 65915204ab1SPantelis Antoniou /* already populated? (driver using of_populate manually) */ 66015204ab1SPantelis Antoniou if (of_node_check_flag(rd->dn, OF_POPULATED)) 66115204ab1SPantelis Antoniou return NOTIFY_OK; 66215204ab1SPantelis Antoniou 663801d728cSPantelis Antoniou /* pdev_parent may be NULL when no bus platform device */ 664801d728cSPantelis Antoniou pdev_parent = of_find_device_by_node(rd->dn->parent); 665801d728cSPantelis Antoniou pdev = of_platform_device_create(rd->dn, NULL, 666801d728cSPantelis Antoniou pdev_parent ? &pdev_parent->dev : NULL); 667801d728cSPantelis Antoniou of_dev_put(pdev_parent); 668801d728cSPantelis Antoniou 669801d728cSPantelis Antoniou if (pdev == NULL) { 670801d728cSPantelis Antoniou pr_err("%s: failed to create for '%s'\n", 671801d728cSPantelis Antoniou __func__, rd->dn->full_name); 672801d728cSPantelis Antoniou /* of_platform_device_create tosses the error code */ 673801d728cSPantelis Antoniou return notifier_from_errno(-EINVAL); 674801d728cSPantelis Antoniou } 675801d728cSPantelis Antoniou break; 676801d728cSPantelis Antoniou 677801d728cSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 67815204ab1SPantelis Antoniou 67915204ab1SPantelis Antoniou /* already depopulated? */ 68015204ab1SPantelis Antoniou if (!of_node_check_flag(rd->dn, OF_POPULATED)) 68115204ab1SPantelis Antoniou return NOTIFY_OK; 68215204ab1SPantelis Antoniou 683801d728cSPantelis Antoniou /* find our device by node */ 684801d728cSPantelis Antoniou pdev = of_find_device_by_node(rd->dn); 685801d728cSPantelis Antoniou if (pdev == NULL) 686801d728cSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 687801d728cSPantelis Antoniou 688801d728cSPantelis Antoniou /* unregister takes one ref away */ 689801d728cSPantelis Antoniou of_platform_device_destroy(&pdev->dev, &children_left); 690801d728cSPantelis Antoniou 691801d728cSPantelis Antoniou /* and put the reference of the find */ 692801d728cSPantelis Antoniou of_dev_put(pdev); 693801d728cSPantelis Antoniou break; 694801d728cSPantelis Antoniou } 695801d728cSPantelis Antoniou 696801d728cSPantelis Antoniou return NOTIFY_OK; 697801d728cSPantelis Antoniou } 698801d728cSPantelis Antoniou 699801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = { 700801d728cSPantelis Antoniou .notifier_call = of_platform_notify, 701801d728cSPantelis Antoniou }; 702801d728cSPantelis Antoniou 703801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void) 704801d728cSPantelis Antoniou { 705801d728cSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&platform_of_notifier)); 706801d728cSPantelis Antoniou } 707801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */ 708801d728cSPantelis Antoniou 709964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */ 710