1af6074fcSRob Herring // SPDX-License-Identifier: GPL-2.0+ 23f23de10SStephen Rothwell /* 33f23de10SStephen Rothwell * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp. 43f23de10SStephen Rothwell * <benh@kernel.crashing.org> 53f23de10SStephen Rothwell * and Arnd Bergmann, IBM Corp. 63f23de10SStephen Rothwell * Merged from powerpc/kernel/of_platform.c and 73f23de10SStephen Rothwell * sparc{,64}/kernel/of_device.c by Stephen Rothwell 83f23de10SStephen Rothwell */ 9606ad42aSRob Herring 10606ad42aSRob Herring #define pr_fmt(fmt) "OF: " fmt 11606ad42aSRob Herring 123f23de10SStephen Rothwell #include <linux/errno.h> 135c457083SStephen Rothwell #include <linux/module.h> 145de1540bSGrant Likely #include <linux/amba/bus.h> 153f23de10SStephen Rothwell #include <linux/device.h> 165fd200f3SGrant Likely #include <linux/dma-mapping.h> 1750ef5284SGrant Likely #include <linux/slab.h> 189e3288dcSGrant Likely #include <linux/of_address.h> 193f23de10SStephen Rothwell #include <linux/of_device.h> 2009515ef5SSricharan R #include <linux/of_iommu.h> 219e3288dcSGrant Likely #include <linux/of_irq.h> 223f23de10SStephen Rothwell #include <linux/of_platform.h> 23eca39301SGrant Likely #include <linux/platform_device.h> 24eca39301SGrant Likely 25cbb49c26SGrant Likely const struct of_device_id of_default_bus_match_table[] = { 26cbb49c26SGrant Likely { .compatible = "simple-bus", }, 2722869a9eSLinus Walleij { .compatible = "simple-mfd", }, 28ecd76edeSPaul Burton { .compatible = "isa", }, 29cbb49c26SGrant Likely #ifdef CONFIG_ARM_AMBA 30cbb49c26SGrant Likely { .compatible = "arm,amba-bus", }, 31cbb49c26SGrant Likely #endif /* CONFIG_ARM_AMBA */ 32cbb49c26SGrant Likely {} /* Empty terminated list */ 33cbb49c26SGrant Likely }; 34cbb49c26SGrant Likely 354550fe63SViresh Kumar static const struct of_device_id of_skipped_node_table[] = { 364550fe63SViresh Kumar { .compatible = "operating-points-v2", }, 374550fe63SViresh Kumar {} /* Empty terminated list */ 384550fe63SViresh Kumar }; 394550fe63SViresh Kumar 40c6085584SJonas Bonn /** 41c6085584SJonas Bonn * of_find_device_by_node - Find the platform_device associated with a node 42c6085584SJonas Bonn * @np: Pointer to device tree node 43c6085584SJonas Bonn * 444fb373dfSJohan Hovold * Takes a reference to the embedded struct device which needs to be dropped 454fb373dfSJohan Hovold * after use. 464fb373dfSJohan Hovold * 47c6085584SJonas Bonn * Returns platform_device pointer, or NULL if not found 48c6085584SJonas Bonn */ 49c6085584SJonas Bonn struct platform_device *of_find_device_by_node(struct device_node *np) 50c6085584SJonas Bonn { 51c6085584SJonas Bonn struct device *dev; 52c6085584SJonas Bonn 53cfba5de9SSuzuki K Poulose dev = bus_find_device_by_of_node(&platform_bus_type, np); 54c6085584SJonas Bonn return dev ? to_platform_device(dev) : NULL; 55c6085584SJonas Bonn } 56c6085584SJonas Bonn EXPORT_SYMBOL(of_find_device_by_node); 57c6085584SJonas Bonn 58964dba28SGrant Likely #ifdef CONFIG_OF_ADDRESS 595fd200f3SGrant Likely /* 605fd200f3SGrant Likely * The following routines scan a subtree and registers a device for 615fd200f3SGrant Likely * each applicable node. 625fd200f3SGrant Likely * 635fd200f3SGrant Likely * Note: sparc doesn't use these routines because it has a different 645fd200f3SGrant Likely * mechanism for creating devices from device tree nodes. 655fd200f3SGrant Likely */ 665fd200f3SGrant Likely 675fd200f3SGrant Likely /** 6894c09319SGrant Likely * of_device_make_bus_id - Use the device node data to assign a unique name 6994c09319SGrant Likely * @dev: pointer to device structure that is linked to a device tree node 7094c09319SGrant Likely * 7107e461cdSGrant Likely * This routine will first try using the translated bus address to 7207e461cdSGrant Likely * derive a unique name. If it cannot, then it will prepend names from 7307e461cdSGrant Likely * parent nodes until a unique name can be derived. 7494c09319SGrant Likely */ 75e553f539SFrank Rowand static void of_device_make_bus_id(struct device *dev) 7694c09319SGrant Likely { 7794c09319SGrant Likely struct device_node *node = dev->of_node; 7824fb530fSKim Phillips const __be32 *reg; 7994c09319SGrant Likely u64 addr; 8094c09319SGrant Likely 8107e461cdSGrant Likely /* Construct the name, using parent nodes if necessary to ensure uniqueness */ 8207e461cdSGrant Likely while (node->parent) { 8394c09319SGrant Likely /* 8407e461cdSGrant Likely * If the address can be translated, then that is as much 8507e461cdSGrant Likely * uniqueness as we need. Make it the first component and return 8694c09319SGrant Likely */ 8794c09319SGrant Likely reg = of_get_property(node, "reg", NULL); 8807e461cdSGrant Likely if (reg && (addr = of_translate_address(node, reg)) != OF_BAD_ADDR) { 89a613b26aSRob Herring dev_set_name(dev, dev_name(dev) ? "%llx.%pOFn:%s" : "%llx.%pOFn", 90d88590dcSGeert Uytterhoeven addr, node, dev_name(dev)); 9194c09319SGrant Likely return; 9294c09319SGrant Likely } 9394c09319SGrant Likely 9407e461cdSGrant Likely /* format arguments only used if dev_name() resolves to NULL */ 9507e461cdSGrant Likely dev_set_name(dev, dev_name(dev) ? "%s:%s" : "%s", 9695e6b1faSRob Herring kbasename(node->full_name), dev_name(dev)); 9707e461cdSGrant Likely node = node->parent; 9807e461cdSGrant Likely } 9994c09319SGrant Likely } 10094c09319SGrant Likely 10194c09319SGrant Likely /** 10294c09319SGrant Likely * of_device_alloc - Allocate and initialize an of_device 10394c09319SGrant Likely * @np: device node to assign to device 10494c09319SGrant Likely * @bus_id: Name to assign to the device. May be null to use default name. 10594c09319SGrant Likely * @parent: Parent device. 10694c09319SGrant Likely */ 10794a0cb1fSGrant Likely struct platform_device *of_device_alloc(struct device_node *np, 10894c09319SGrant Likely const char *bus_id, 10994c09319SGrant Likely struct device *parent) 11094c09319SGrant Likely { 11194a0cb1fSGrant Likely struct platform_device *dev; 11252f6537cSAndres Salomon int rc, i, num_reg = 0, num_irq; 113ac80a51eSGrant Likely struct resource *res, temp_res; 11494c09319SGrant Likely 1156d7e3bf8SAndy Shevchenko dev = platform_device_alloc("", PLATFORM_DEVID_NONE); 1167096d042SGrant Likely if (!dev) 1177096d042SGrant Likely return NULL; 1187096d042SGrant Likely 1197096d042SGrant Likely /* count the io and irq resources */ 120ac80a51eSGrant Likely while (of_address_to_resource(np, num_reg, &temp_res) == 0) 121ac80a51eSGrant Likely num_reg++; 12252f6537cSAndres Salomon num_irq = of_irq_count(np); 123ac80a51eSGrant Likely 124ac80a51eSGrant Likely /* Populate the resource table */ 125ac80a51eSGrant Likely if (num_irq || num_reg) { 1266396bb22SKees Cook res = kcalloc(num_irq + num_reg, sizeof(*res), GFP_KERNEL); 1277096d042SGrant Likely if (!res) { 1287096d042SGrant Likely platform_device_put(dev); 1297096d042SGrant Likely return NULL; 1307096d042SGrant Likely } 1317096d042SGrant Likely 132ac80a51eSGrant Likely dev->num_resources = num_reg + num_irq; 133ac80a51eSGrant Likely dev->resource = res; 134ac80a51eSGrant Likely for (i = 0; i < num_reg; i++, res++) { 135ac80a51eSGrant Likely rc = of_address_to_resource(np, i, res); 136ac80a51eSGrant Likely WARN_ON(rc); 137ac80a51eSGrant Likely } 1389ec36cafSRob Herring if (of_irq_to_resource_table(np, res, num_irq) != num_irq) 139a613b26aSRob Herring pr_debug("not all legacy IRQ resources mapped for %pOFn\n", 140a613b26aSRob Herring np); 141ac80a51eSGrant Likely } 14294c09319SGrant Likely 14394c09319SGrant Likely dev->dev.of_node = of_node_get(np); 144f94277afSRobin Murphy dev->dev.fwnode = &np->fwnode; 14543c0767eSGrant Likely dev->dev.parent = parent ? : &platform_bus; 14694c09319SGrant Likely 14794c09319SGrant Likely if (bus_id) 14894c09319SGrant Likely dev_set_name(&dev->dev, "%s", bus_id); 14994c09319SGrant Likely else 15094c09319SGrant Likely of_device_make_bus_id(&dev->dev); 15194c09319SGrant Likely 15294c09319SGrant Likely return dev; 15394c09319SGrant Likely } 15494c09319SGrant Likely EXPORT_SYMBOL(of_device_alloc); 15594c09319SGrant Likely 156591c1ee4SSantosh Shilimkar /** 15715c3597dSGrant Likely * of_platform_device_create_pdata - Alloc, initialize and register an of_device 1585fd200f3SGrant Likely * @np: pointer to node to create device for 1595fd200f3SGrant Likely * @bus_id: name to assign device 16015c3597dSGrant Likely * @platform_data: pointer to populate platform_data pointer with 1615fd200f3SGrant Likely * @parent: Linux device model parent device. 162cd1e6504SGrant Likely * 163cd1e6504SGrant Likely * Returns pointer to created platform device, or NULL if a device was not 164cd1e6504SGrant Likely * registered. Unavailable devices will not get registered. 1655fd200f3SGrant Likely */ 166245d9641SMark Brown static struct platform_device *of_platform_device_create_pdata( 16715c3597dSGrant Likely struct device_node *np, 1685fd200f3SGrant Likely const char *bus_id, 16915c3597dSGrant Likely void *platform_data, 1705fd200f3SGrant Likely struct device *parent) 1715fd200f3SGrant Likely { 17294a0cb1fSGrant Likely struct platform_device *dev; 1735fd200f3SGrant Likely 174c6e126deSPawel Moll if (!of_device_is_available(np) || 175c6e126deSPawel Moll of_node_test_and_set_flag(np, OF_POPULATED)) 176cd1e6504SGrant Likely return NULL; 177cd1e6504SGrant Likely 1785fd200f3SGrant Likely dev = of_device_alloc(np, bus_id, parent); 1795fd200f3SGrant Likely if (!dev) 180c6e126deSPawel Moll goto err_clear_flag; 1815fd200f3SGrant Likely 182a5516219SRobin Murphy dev->dev.coherent_dma_mask = DMA_BIT_MASK(32); 183a5516219SRobin Murphy if (!dev->dev.dma_mask) 184a5516219SRobin Murphy dev->dev.dma_mask = &dev->dev.coherent_dma_mask; 185eca39301SGrant Likely dev->dev.bus = &platform_bus_type; 18615c3597dSGrant Likely dev->dev.platform_data = platform_data; 187c706c239SMarc Zyngier of_msi_configure(&dev->dev, dev->dev.of_node); 1885fd200f3SGrant Likely 1897096d042SGrant Likely if (of_device_add(dev) != 0) { 1907096d042SGrant Likely platform_device_put(dev); 191c6e126deSPawel Moll goto err_clear_flag; 1925fd200f3SGrant Likely } 1935fd200f3SGrant Likely 1945fd200f3SGrant Likely return dev; 195c6e126deSPawel Moll 196c6e126deSPawel Moll err_clear_flag: 197c6e126deSPawel Moll of_node_clear_flag(np, OF_POPULATED); 198c6e126deSPawel Moll return NULL; 1995fd200f3SGrant Likely } 20015c3597dSGrant Likely 20115c3597dSGrant Likely /** 20215c3597dSGrant Likely * of_platform_device_create - Alloc, initialize and register an of_device 20315c3597dSGrant Likely * @np: pointer to node to create device for 20415c3597dSGrant Likely * @bus_id: name to assign device 20515c3597dSGrant Likely * @parent: Linux device model parent device. 20615c3597dSGrant Likely * 20715c3597dSGrant Likely * Returns pointer to created platform device, or NULL if a device was not 20815c3597dSGrant Likely * registered. Unavailable devices will not get registered. 20915c3597dSGrant Likely */ 21015c3597dSGrant Likely struct platform_device *of_platform_device_create(struct device_node *np, 21115c3597dSGrant Likely const char *bus_id, 21215c3597dSGrant Likely struct device *parent) 21315c3597dSGrant Likely { 21415c3597dSGrant Likely return of_platform_device_create_pdata(np, bus_id, NULL, parent); 21515c3597dSGrant Likely } 2165fd200f3SGrant Likely EXPORT_SYMBOL(of_platform_device_create); 2175fd200f3SGrant Likely 2185de1540bSGrant Likely #ifdef CONFIG_ARM_AMBA 2195de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node, 2205de1540bSGrant Likely const char *bus_id, 2215de1540bSGrant Likely void *platform_data, 2225de1540bSGrant Likely struct device *parent) 2235de1540bSGrant Likely { 2245de1540bSGrant Likely struct amba_device *dev; 2255de1540bSGrant Likely const void *prop; 2265de1540bSGrant Likely int i, ret; 2275de1540bSGrant Likely 2280d638a07SRob Herring pr_debug("Creating amba device %pOF\n", node); 2295de1540bSGrant Likely 230c6e126deSPawel Moll if (!of_device_is_available(node) || 231c6e126deSPawel Moll of_node_test_and_set_flag(node, OF_POPULATED)) 2325de1540bSGrant Likely return NULL; 2335de1540bSGrant Likely 234c0f72f8aSRussell King dev = amba_device_alloc(NULL, 0, 0); 235606ad42aSRob Herring if (!dev) 236c6e126deSPawel Moll goto err_clear_flag; 2375de1540bSGrant Likely 2388c89ef7bSLinus Walleij /* AMBA devices only support a single DMA mask */ 2398c89ef7bSLinus Walleij dev->dev.coherent_dma_mask = DMA_BIT_MASK(32); 2408c89ef7bSLinus Walleij dev->dev.dma_mask = &dev->dev.coherent_dma_mask; 2418c89ef7bSLinus Walleij 2425de1540bSGrant Likely /* setup generic device info */ 2435de1540bSGrant Likely dev->dev.of_node = of_node_get(node); 244f94277afSRobin Murphy dev->dev.fwnode = &node->fwnode; 24543c0767eSGrant Likely dev->dev.parent = parent ? : &platform_bus; 2465de1540bSGrant Likely dev->dev.platform_data = platform_data; 2475de1540bSGrant Likely if (bus_id) 2485de1540bSGrant Likely dev_set_name(&dev->dev, "%s", bus_id); 2495de1540bSGrant Likely else 2505de1540bSGrant Likely of_device_make_bus_id(&dev->dev); 2515de1540bSGrant Likely 2525de1540bSGrant Likely /* Allow the HW Peripheral ID to be overridden */ 2535de1540bSGrant Likely prop = of_get_property(node, "arm,primecell-periphid", NULL); 2545de1540bSGrant Likely if (prop) 2555de1540bSGrant Likely dev->periphid = of_read_ulong(prop, 1); 2565de1540bSGrant Likely 2575de1540bSGrant Likely /* Decode the IRQs and address ranges */ 2585de1540bSGrant Likely for (i = 0; i < AMBA_NR_IRQS; i++) 2595de1540bSGrant Likely dev->irq[i] = irq_of_parse_and_map(node, i); 2605de1540bSGrant Likely 2615de1540bSGrant Likely ret = of_address_to_resource(node, 0, &dev->res); 2622bc552dfSBartlomiej Zolnierkiewicz if (ret) { 2630d638a07SRob Herring pr_err("amba: of_address_to_resource() failed (%d) for %pOF\n", 2640d638a07SRob Herring ret, node); 2655de1540bSGrant Likely goto err_free; 2662bc552dfSBartlomiej Zolnierkiewicz } 2675de1540bSGrant Likely 268c0f72f8aSRussell King ret = amba_device_add(dev, &iomem_resource); 2692bc552dfSBartlomiej Zolnierkiewicz if (ret) { 2700d638a07SRob Herring pr_err("amba_device_add() failed (%d) for %pOF\n", 2710d638a07SRob Herring ret, node); 2725de1540bSGrant Likely goto err_free; 2732bc552dfSBartlomiej Zolnierkiewicz } 2745de1540bSGrant Likely 2755de1540bSGrant Likely return dev; 2765de1540bSGrant Likely 2775de1540bSGrant Likely err_free: 278c0f72f8aSRussell King amba_device_put(dev); 279c6e126deSPawel Moll err_clear_flag: 280c6e126deSPawel Moll of_node_clear_flag(node, OF_POPULATED); 2815de1540bSGrant Likely return NULL; 2825de1540bSGrant Likely } 2835de1540bSGrant Likely #else /* CONFIG_ARM_AMBA */ 2845de1540bSGrant Likely static struct amba_device *of_amba_device_create(struct device_node *node, 2855de1540bSGrant Likely const char *bus_id, 2865de1540bSGrant Likely void *platform_data, 2875de1540bSGrant Likely struct device *parent) 2885de1540bSGrant Likely { 2895de1540bSGrant Likely return NULL; 2905de1540bSGrant Likely } 2915de1540bSGrant Likely #endif /* CONFIG_ARM_AMBA */ 2925de1540bSGrant Likely 2935fd200f3SGrant Likely /** 294f9a529b9SQi Zheng * of_dev_lookup() - Given a device node, lookup the preferred Linux name 29515c3597dSGrant Likely */ 29615c3597dSGrant Likely static const struct of_dev_auxdata *of_dev_lookup(const struct of_dev_auxdata *lookup, 29715c3597dSGrant Likely struct device_node *np) 29815c3597dSGrant Likely { 299fc5cf80aSTony Lindgren const struct of_dev_auxdata *auxdata; 30015c3597dSGrant Likely struct resource res; 301fc5cf80aSTony Lindgren int compatible = 0; 302303f59d1SOlof Johansson 303303f59d1SOlof Johansson if (!lookup) 304303f59d1SOlof Johansson return NULL; 305303f59d1SOlof Johansson 306fc5cf80aSTony Lindgren auxdata = lookup; 307fc5cf80aSTony Lindgren for (; auxdata->compatible; auxdata++) { 308fc5cf80aSTony Lindgren if (!of_device_is_compatible(np, auxdata->compatible)) 30915c3597dSGrant Likely continue; 310fc5cf80aSTony Lindgren compatible++; 31184774e61SLee Jones if (!of_address_to_resource(np, 0, &res)) 312fc5cf80aSTony Lindgren if (res.start != auxdata->phys_addr) 31315c3597dSGrant Likely continue; 3140d638a07SRob Herring pr_debug("%pOF: devname=%s\n", np, auxdata->name); 315fc5cf80aSTony Lindgren return auxdata; 316fc5cf80aSTony Lindgren } 317fc5cf80aSTony Lindgren 318fc5cf80aSTony Lindgren if (!compatible) 319fc5cf80aSTony Lindgren return NULL; 320fc5cf80aSTony Lindgren 321fc5cf80aSTony Lindgren /* Try compatible match if no phys_addr and name are specified */ 322fc5cf80aSTony Lindgren auxdata = lookup; 323fc5cf80aSTony Lindgren for (; auxdata->compatible; auxdata++) { 324fc5cf80aSTony Lindgren if (!of_device_is_compatible(np, auxdata->compatible)) 325fc5cf80aSTony Lindgren continue; 326fc5cf80aSTony Lindgren if (!auxdata->phys_addr && !auxdata->name) { 3270d638a07SRob Herring pr_debug("%pOF: compatible match\n", np); 328fc5cf80aSTony Lindgren return auxdata; 329fc5cf80aSTony Lindgren } 33015c3597dSGrant Likely } 331303f59d1SOlof Johansson 33215c3597dSGrant Likely return NULL; 33315c3597dSGrant Likely } 33415c3597dSGrant Likely 33515c3597dSGrant Likely /** 33638e9e21dSGrant Likely * of_platform_bus_create() - Create a device for a node and its children. 3375fd200f3SGrant Likely * @bus: device node of the bus to instantiate 3381eed4c07SGrant Likely * @matches: match table for bus nodes 339303f59d1SOlof Johansson * @lookup: auxdata table for matching id and platform_data with device nodes 34038e9e21dSGrant Likely * @parent: parent for new device, or NULL for top level. 341303f59d1SOlof Johansson * @strict: require compatible property 34238e9e21dSGrant Likely * 34338e9e21dSGrant Likely * Creates a platform_device for the provided device_node, and optionally 34438e9e21dSGrant Likely * recursively create devices for all the child nodes. 3455fd200f3SGrant Likely */ 34638e9e21dSGrant Likely static int of_platform_bus_create(struct device_node *bus, 3475fd200f3SGrant Likely const struct of_device_id *matches, 34815c3597dSGrant Likely const struct of_dev_auxdata *lookup, 34929d4f8a4SGrant Likely struct device *parent, bool strict) 3505fd200f3SGrant Likely { 35115c3597dSGrant Likely const struct of_dev_auxdata *auxdata; 3525fd200f3SGrant Likely struct device_node *child; 35394a0cb1fSGrant Likely struct platform_device *dev; 35415c3597dSGrant Likely const char *bus_id = NULL; 35515c3597dSGrant Likely void *platform_data = NULL; 3565fd200f3SGrant Likely int rc = 0; 3575fd200f3SGrant Likely 35829d4f8a4SGrant Likely /* Make sure it has a compatible property */ 35929d4f8a4SGrant Likely if (strict && (!of_get_property(bus, "compatible", NULL))) { 3600d638a07SRob Herring pr_debug("%s() - skipping %pOF, no compatible prop\n", 3610d638a07SRob Herring __func__, bus); 36229d4f8a4SGrant Likely return 0; 36329d4f8a4SGrant Likely } 36429d4f8a4SGrant Likely 3654550fe63SViresh Kumar /* Skip nodes for which we don't want to create devices */ 3664550fe63SViresh Kumar if (unlikely(of_match_node(of_skipped_node_table, bus))) { 3674550fe63SViresh Kumar pr_debug("%s() - skipping %pOF node\n", __func__, bus); 3684550fe63SViresh Kumar return 0; 3694550fe63SViresh Kumar } 3704550fe63SViresh Kumar 37144a7185cSKefeng Wang if (of_node_check_flag(bus, OF_POPULATED_BUS)) { 3720d638a07SRob Herring pr_debug("%s() - skipping %pOF, already populated\n", 3730d638a07SRob Herring __func__, bus); 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) { 3970d638a07SRob Herring pr_debug(" create child: %pOF\n", child); 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__); 4290d638a07SRob Herring pr_debug(" starting at: %pOF\n", root); 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__); 4810d638a07SRob Herring pr_debug(" starting at: %pOF\n", root); 48244a7185cSKefeng Wang 4835e666938SSaravana Kannan device_links_supplier_sync_state_pause(); 48429d4f8a4SGrant Likely for_each_child_of_node(root, child) { 48515c3597dSGrant Likely rc = of_platform_bus_create(child, matches, lookup, parent, true); 4867fad948aSJulia Lawall if (rc) { 4877fad948aSJulia Lawall of_node_put(child); 48829d4f8a4SGrant Likely break; 48929d4f8a4SGrant Likely } 4907fad948aSJulia Lawall } 4915e666938SSaravana Kannan device_links_supplier_sync_state_resume(); 4925e666938SSaravana Kannan 4932d0747c4SGrant Likely of_node_set_flag(root, OF_POPULATED_BUS); 49429d4f8a4SGrant Likely 49529d4f8a4SGrant Likely of_node_put(root); 49629d4f8a4SGrant Likely return rc; 49729d4f8a4SGrant Likely } 498e001f1c8SStephen Warren EXPORT_SYMBOL_GPL(of_platform_populate); 499c6e126deSPawel Moll 50043443ad6SHauke Mehrtens int of_platform_default_populate(struct device_node *root, 50143443ad6SHauke Mehrtens const struct of_dev_auxdata *lookup, 50243443ad6SHauke Mehrtens struct device *parent) 50343443ad6SHauke Mehrtens { 50443443ad6SHauke Mehrtens return of_platform_populate(root, of_default_bus_match_table, lookup, 50543443ad6SHauke Mehrtens parent); 50643443ad6SHauke Mehrtens } 50743443ad6SHauke Mehrtens EXPORT_SYMBOL_GPL(of_platform_default_populate); 50843443ad6SHauke Mehrtens 509fc520f8bSKevin Hao #ifndef CONFIG_PPC 510a50ff19dSBjorn Andersson static const struct of_device_id reserved_mem_matches[] = { 511d1de6d6cSBjorn Andersson { .compatible = "qcom,rmtfs-mem" }, 512312416d9SMahesh Sivasubramanian { .compatible = "qcom,cmd-db" }, 513a50ff19dSBjorn Andersson { .compatible = "ramoops" }, 514a50ff19dSBjorn Andersson {} 515a50ff19dSBjorn Andersson }; 516a50ff19dSBjorn Andersson 51744a7185cSKefeng Wang static int __init of_platform_default_populate_init(void) 51844a7185cSKefeng Wang { 519529182e2SKees Cook struct device_node *node; 520529182e2SKees Cook 521ee9b280eSSaravana Kannan device_links_supplier_sync_state_pause(); 522ee9b280eSSaravana Kannan 523529182e2SKees Cook if (!of_have_populated_dt()) 524529182e2SKees Cook return -ENODEV; 525529182e2SKees Cook 526529182e2SKees Cook /* 527a50ff19dSBjorn Andersson * Handle certain compatibles explicitly, since we don't want to create 528a50ff19dSBjorn Andersson * platform_devices for every node in /reserved-memory with a 529a50ff19dSBjorn Andersson * "compatible", 530529182e2SKees Cook */ 531a50ff19dSBjorn Andersson for_each_matching_node(node, reserved_mem_matches) 532529182e2SKees Cook of_platform_device_create(node, NULL, NULL); 533529182e2SKees Cook 5343aa0582fSSudeep Holla node = of_find_node_by_path("/firmware"); 535e2105ca8SSudeep Holla if (node) { 5363aa0582fSSudeep Holla of_platform_populate(node, NULL, NULL, NULL); 537e2105ca8SSudeep Holla of_node_put(node); 538e2105ca8SSudeep Holla } 5393aa0582fSSudeep Holla 540529182e2SKees Cook /* Populate everything else. */ 54144a7185cSKefeng Wang of_platform_default_populate(NULL, NULL, NULL); 54244a7185cSKefeng Wang 54344a7185cSKefeng Wang return 0; 54444a7185cSKefeng Wang } 54544a7185cSKefeng Wang arch_initcall_sync(of_platform_default_populate_init); 5465e666938SSaravana Kannan 5475e666938SSaravana Kannan static int __init of_platform_sync_state_init(void) 5485e666938SSaravana Kannan { 5495e666938SSaravana Kannan device_links_supplier_sync_state_resume(); 5505e666938SSaravana Kannan return 0; 5515e666938SSaravana Kannan } 5525e666938SSaravana Kannan late_initcall_sync(of_platform_sync_state_init); 553fc520f8bSKevin Hao #endif 55444a7185cSKefeng Wang 555c2372c20SJan Glauber int of_platform_device_destroy(struct device *dev, void *data) 556c6e126deSPawel Moll { 557c6e126deSPawel Moll /* Do not touch devices not populated from the device tree */ 55875f353b6SGrant Likely if (!dev->of_node || !of_node_check_flag(dev->of_node, OF_POPULATED)) 559c6e126deSPawel Moll return 0; 560c6e126deSPawel Moll 56175f353b6SGrant Likely /* Recurse for any nodes that were treated as busses */ 56275f353b6SGrant Likely if (of_node_check_flag(dev->of_node, OF_POPULATED_BUS)) 56375f353b6SGrant Likely device_for_each_child(dev, NULL, of_platform_device_destroy); 564c6e126deSPawel Moll 565522811e9SSrinivas Kandagatla of_node_clear_flag(dev->of_node, OF_POPULATED); 566522811e9SSrinivas Kandagatla of_node_clear_flag(dev->of_node, OF_POPULATED_BUS); 567522811e9SSrinivas Kandagatla 568c6e126deSPawel Moll if (dev->bus == &platform_bus_type) 569c6e126deSPawel Moll platform_device_unregister(to_platform_device(dev)); 570c6e126deSPawel Moll #ifdef CONFIG_ARM_AMBA 571c6e126deSPawel Moll else if (dev->bus == &amba_bustype) 572c6e126deSPawel Moll amba_device_unregister(to_amba_device(dev)); 573c6e126deSPawel Moll #endif 574c6e126deSPawel Moll 575c6e126deSPawel Moll return 0; 576c6e126deSPawel Moll } 577c2372c20SJan Glauber EXPORT_SYMBOL_GPL(of_platform_device_destroy); 578c6e126deSPawel Moll 579c6e126deSPawel Moll /** 580c6e126deSPawel Moll * of_platform_depopulate() - Remove devices populated from device tree 58175f353b6SGrant Likely * @parent: device which children will be removed 582c6e126deSPawel Moll * 583c6e126deSPawel Moll * Complementary to of_platform_populate(), this function removes children 584c6e126deSPawel Moll * of the given device (and, recurrently, their children) that have been 585c6e126deSPawel Moll * created from their respective device tree nodes (and only those, 586c6e126deSPawel Moll * leaving others - eg. manually created - unharmed). 587c6e126deSPawel Moll */ 58875f353b6SGrant Likely void of_platform_depopulate(struct device *parent) 589c6e126deSPawel Moll { 5902d0747c4SGrant Likely if (parent->of_node && of_node_check_flag(parent->of_node, OF_POPULATED_BUS)) { 59170a29209SThierry Reding device_for_each_child_reverse(parent, NULL, of_platform_device_destroy); 5922d0747c4SGrant Likely of_node_clear_flag(parent->of_node, OF_POPULATED_BUS); 5932d0747c4SGrant Likely } 594c6e126deSPawel Moll } 595c6e126deSPawel Moll EXPORT_SYMBOL_GPL(of_platform_depopulate); 596c6e126deSPawel Moll 59738b0b219SBenjamin Gaignard static void devm_of_platform_populate_release(struct device *dev, void *res) 59838b0b219SBenjamin Gaignard { 59938b0b219SBenjamin Gaignard of_platform_depopulate(*(struct device **)res); 60038b0b219SBenjamin Gaignard } 60138b0b219SBenjamin Gaignard 60238b0b219SBenjamin Gaignard /** 60338b0b219SBenjamin Gaignard * devm_of_platform_populate() - Populate platform_devices from device tree data 60438b0b219SBenjamin Gaignard * @dev: device that requested to populate from device tree data 60538b0b219SBenjamin Gaignard * 60638b0b219SBenjamin Gaignard * Similar to of_platform_populate(), but will automatically call 60738b0b219SBenjamin Gaignard * of_platform_depopulate() when the device is unbound from the bus. 60838b0b219SBenjamin Gaignard * 60938b0b219SBenjamin Gaignard * Returns 0 on success, < 0 on failure. 61038b0b219SBenjamin Gaignard */ 61138b0b219SBenjamin Gaignard int devm_of_platform_populate(struct device *dev) 61238b0b219SBenjamin Gaignard { 61338b0b219SBenjamin Gaignard struct device **ptr; 61438b0b219SBenjamin Gaignard int ret; 61538b0b219SBenjamin Gaignard 61638b0b219SBenjamin Gaignard if (!dev) 61738b0b219SBenjamin Gaignard return -EINVAL; 61838b0b219SBenjamin Gaignard 61938b0b219SBenjamin Gaignard ptr = devres_alloc(devm_of_platform_populate_release, 62038b0b219SBenjamin Gaignard sizeof(*ptr), GFP_KERNEL); 62138b0b219SBenjamin Gaignard if (!ptr) 62238b0b219SBenjamin Gaignard return -ENOMEM; 62338b0b219SBenjamin Gaignard 62438b0b219SBenjamin Gaignard ret = of_platform_populate(dev->of_node, NULL, NULL, dev); 62538b0b219SBenjamin Gaignard if (ret) { 62638b0b219SBenjamin Gaignard devres_free(ptr); 62738b0b219SBenjamin Gaignard } else { 62838b0b219SBenjamin Gaignard *ptr = dev; 62938b0b219SBenjamin Gaignard devres_add(dev, ptr); 63038b0b219SBenjamin Gaignard } 63138b0b219SBenjamin Gaignard 63238b0b219SBenjamin Gaignard return ret; 63338b0b219SBenjamin Gaignard } 63438b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_populate); 63538b0b219SBenjamin Gaignard 63638b0b219SBenjamin Gaignard static int devm_of_platform_match(struct device *dev, void *res, void *data) 63738b0b219SBenjamin Gaignard { 63838b0b219SBenjamin Gaignard struct device **ptr = res; 63938b0b219SBenjamin Gaignard 64038b0b219SBenjamin Gaignard if (!ptr) { 64138b0b219SBenjamin Gaignard WARN_ON(!ptr); 64238b0b219SBenjamin Gaignard return 0; 64338b0b219SBenjamin Gaignard } 64438b0b219SBenjamin Gaignard 64538b0b219SBenjamin Gaignard return *ptr == data; 64638b0b219SBenjamin Gaignard } 64738b0b219SBenjamin Gaignard 64838b0b219SBenjamin Gaignard /** 64938b0b219SBenjamin Gaignard * devm_of_platform_depopulate() - Remove devices populated from device tree 65038b0b219SBenjamin Gaignard * @dev: device that requested to depopulate from device tree data 65138b0b219SBenjamin Gaignard * 65238b0b219SBenjamin Gaignard * Complementary to devm_of_platform_populate(), this function removes children 65338b0b219SBenjamin Gaignard * of the given device (and, recurrently, their children) that have been 65438b0b219SBenjamin Gaignard * created from their respective device tree nodes (and only those, 65538b0b219SBenjamin Gaignard * leaving others - eg. manually created - unharmed). 65638b0b219SBenjamin Gaignard */ 65738b0b219SBenjamin Gaignard void devm_of_platform_depopulate(struct device *dev) 65838b0b219SBenjamin Gaignard { 65938b0b219SBenjamin Gaignard int ret; 66038b0b219SBenjamin Gaignard 66138b0b219SBenjamin Gaignard ret = devres_release(dev, devm_of_platform_populate_release, 66238b0b219SBenjamin Gaignard devm_of_platform_match, dev); 66338b0b219SBenjamin Gaignard 66438b0b219SBenjamin Gaignard WARN_ON(ret); 66538b0b219SBenjamin Gaignard } 66638b0b219SBenjamin Gaignard EXPORT_SYMBOL_GPL(devm_of_platform_depopulate); 66738b0b219SBenjamin Gaignard 668801d728cSPantelis Antoniou #ifdef CONFIG_OF_DYNAMIC 669801d728cSPantelis Antoniou static int of_platform_notify(struct notifier_block *nb, 670801d728cSPantelis Antoniou unsigned long action, void *arg) 671801d728cSPantelis Antoniou { 672801d728cSPantelis Antoniou struct of_reconfig_data *rd = arg; 673801d728cSPantelis Antoniou struct platform_device *pdev_parent, *pdev; 674801d728cSPantelis Antoniou bool children_left; 675801d728cSPantelis Antoniou 676801d728cSPantelis Antoniou switch (of_reconfig_get_state_change(action, rd)) { 677801d728cSPantelis Antoniou case OF_RECONFIG_CHANGE_ADD: 678801d728cSPantelis Antoniou /* verify that the parent is a bus */ 679801d728cSPantelis Antoniou if (!of_node_check_flag(rd->dn->parent, OF_POPULATED_BUS)) 680801d728cSPantelis Antoniou return NOTIFY_OK; /* not for us */ 681801d728cSPantelis Antoniou 68215204ab1SPantelis Antoniou /* already populated? (driver using of_populate manually) */ 68315204ab1SPantelis Antoniou if (of_node_check_flag(rd->dn, OF_POPULATED)) 68415204ab1SPantelis Antoniou return NOTIFY_OK; 68515204ab1SPantelis Antoniou 686801d728cSPantelis Antoniou /* pdev_parent may be NULL when no bus platform device */ 687801d728cSPantelis Antoniou pdev_parent = of_find_device_by_node(rd->dn->parent); 688801d728cSPantelis Antoniou pdev = of_platform_device_create(rd->dn, NULL, 689801d728cSPantelis Antoniou pdev_parent ? &pdev_parent->dev : NULL); 690*83c4a4eeSRob Herring platform_device_put(pdev_parent); 691801d728cSPantelis Antoniou 692801d728cSPantelis Antoniou if (pdev == NULL) { 6930d638a07SRob Herring pr_err("%s: failed to create for '%pOF'\n", 6940d638a07SRob Herring __func__, rd->dn); 695801d728cSPantelis Antoniou /* of_platform_device_create tosses the error code */ 696801d728cSPantelis Antoniou return notifier_from_errno(-EINVAL); 697801d728cSPantelis Antoniou } 698801d728cSPantelis Antoniou break; 699801d728cSPantelis Antoniou 700801d728cSPantelis Antoniou case OF_RECONFIG_CHANGE_REMOVE: 70115204ab1SPantelis Antoniou 70215204ab1SPantelis Antoniou /* already depopulated? */ 70315204ab1SPantelis Antoniou if (!of_node_check_flag(rd->dn, OF_POPULATED)) 70415204ab1SPantelis Antoniou return NOTIFY_OK; 70515204ab1SPantelis Antoniou 706801d728cSPantelis Antoniou /* find our device by node */ 707801d728cSPantelis Antoniou pdev = of_find_device_by_node(rd->dn); 708801d728cSPantelis Antoniou if (pdev == NULL) 709801d728cSPantelis Antoniou return NOTIFY_OK; /* no? not meant for us */ 710801d728cSPantelis Antoniou 711801d728cSPantelis Antoniou /* unregister takes one ref away */ 712801d728cSPantelis Antoniou of_platform_device_destroy(&pdev->dev, &children_left); 713801d728cSPantelis Antoniou 714801d728cSPantelis Antoniou /* and put the reference of the find */ 715*83c4a4eeSRob Herring platform_device_put(pdev); 716801d728cSPantelis Antoniou break; 717801d728cSPantelis Antoniou } 718801d728cSPantelis Antoniou 719801d728cSPantelis Antoniou return NOTIFY_OK; 720801d728cSPantelis Antoniou } 721801d728cSPantelis Antoniou 722801d728cSPantelis Antoniou static struct notifier_block platform_of_notifier = { 723801d728cSPantelis Antoniou .notifier_call = of_platform_notify, 724801d728cSPantelis Antoniou }; 725801d728cSPantelis Antoniou 726801d728cSPantelis Antoniou void of_platform_register_reconfig_notifier(void) 727801d728cSPantelis Antoniou { 728801d728cSPantelis Antoniou WARN_ON(of_reconfig_notifier_register(&platform_of_notifier)); 729801d728cSPantelis Antoniou } 730801d728cSPantelis Antoniou #endif /* CONFIG_OF_DYNAMIC */ 731801d728cSPantelis Antoniou 732964dba28SGrant Likely #endif /* CONFIG_OF_ADDRESS */ 733