Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corp. |
| 3 | * <benh@kernel.crashing.org> |
| 4 | * and Arnd Bergmann, IBM Corp. |
| 5 | * Merged from powerpc/kernel/of_platform.c and |
| 6 | * sparc{,64}/kernel/of_device.c by Stephen Rothwell |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | */ |
| 14 | #include <linux/errno.h> |
Stephen Rothwell | 5c45708 | 2007-10-17 21:17:42 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 16 | #include <linux/device.h> |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 17 | #include <linux/dma-mapping.h> |
Grant Likely | 50ef528 | 2010-06-08 07:48:20 -0600 | [diff] [blame] | 18 | #include <linux/slab.h> |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 19 | #include <linux/of_address.h> |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 20 | #include <linux/of_device.h> |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 21 | #include <linux/of_irq.h> |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 22 | #include <linux/of_platform.h> |
Grant Likely | eca3930 | 2010-06-08 07:48:21 -0600 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
| 24 | |
Grant Likely | cbb49c2 | 2011-06-21 10:59:34 -0600 | [diff] [blame] | 25 | const struct of_device_id of_default_bus_match_table[] = { |
| 26 | { .compatible = "simple-bus", }, |
| 27 | #ifdef CONFIG_ARM_AMBA |
| 28 | { .compatible = "arm,amba-bus", }, |
| 29 | #endif /* CONFIG_ARM_AMBA */ |
| 30 | {} /* Empty terminated list */ |
| 31 | }; |
| 32 | |
Jonas Bonn | c608558 | 2010-07-23 19:19:35 +0200 | [diff] [blame] | 33 | static int of_dev_node_match(struct device *dev, void *data) |
| 34 | { |
| 35 | return dev->of_node == data; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * of_find_device_by_node - Find the platform_device associated with a node |
| 40 | * @np: Pointer to device tree node |
| 41 | * |
| 42 | * Returns platform_device pointer, or NULL if not found |
| 43 | */ |
| 44 | struct platform_device *of_find_device_by_node(struct device_node *np) |
| 45 | { |
| 46 | struct device *dev; |
| 47 | |
| 48 | dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match); |
| 49 | return dev ? to_platform_device(dev) : NULL; |
| 50 | } |
| 51 | EXPORT_SYMBOL(of_find_device_by_node); |
| 52 | |
Grant Likely | 596c955 | 2010-07-14 23:51:43 -0600 | [diff] [blame] | 53 | #if defined(CONFIG_PPC_DCR) |
| 54 | #include <asm/dcr.h> |
| 55 | #endif |
| 56 | |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 57 | #if !defined(CONFIG_SPARC) |
| 58 | /* |
| 59 | * The following routines scan a subtree and registers a device for |
| 60 | * each applicable node. |
| 61 | * |
| 62 | * Note: sparc doesn't use these routines because it has a different |
| 63 | * mechanism for creating devices from device tree nodes. |
| 64 | */ |
| 65 | |
| 66 | /** |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 67 | * of_device_make_bus_id - Use the device node data to assign a unique name |
| 68 | * @dev: pointer to device structure that is linked to a device tree node |
| 69 | * |
| 70 | * This routine will first try using either the dcr-reg or the reg property |
| 71 | * value to derive a unique name. As a last resort it will use the node |
| 72 | * name followed by a unique number. |
| 73 | */ |
Grant Likely | c660122 | 2010-07-23 15:04:01 -0600 | [diff] [blame] | 74 | void of_device_make_bus_id(struct device *dev) |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 75 | { |
| 76 | static atomic_t bus_no_reg_magic; |
| 77 | struct device_node *node = dev->of_node; |
| 78 | const u32 *reg; |
| 79 | u64 addr; |
| 80 | int magic; |
| 81 | |
| 82 | #ifdef CONFIG_PPC_DCR |
| 83 | /* |
| 84 | * If it's a DCR based device, use 'd' for native DCRs |
| 85 | * and 'D' for MMIO DCRs. |
| 86 | */ |
| 87 | reg = of_get_property(node, "dcr-reg", NULL); |
| 88 | if (reg) { |
| 89 | #ifdef CONFIG_PPC_DCR_NATIVE |
| 90 | dev_set_name(dev, "d%x.%s", *reg, node->name); |
| 91 | #else /* CONFIG_PPC_DCR_NATIVE */ |
| 92 | u64 addr = of_translate_dcr_address(node, *reg, NULL); |
| 93 | if (addr != OF_BAD_ADDR) { |
| 94 | dev_set_name(dev, "D%llx.%s", |
| 95 | (unsigned long long)addr, node->name); |
| 96 | return; |
| 97 | } |
| 98 | #endif /* !CONFIG_PPC_DCR_NATIVE */ |
| 99 | } |
| 100 | #endif /* CONFIG_PPC_DCR */ |
| 101 | |
| 102 | /* |
| 103 | * For MMIO, get the physical address |
| 104 | */ |
| 105 | reg = of_get_property(node, "reg", NULL); |
| 106 | if (reg) { |
| 107 | addr = of_translate_address(node, reg); |
| 108 | if (addr != OF_BAD_ADDR) { |
| 109 | dev_set_name(dev, "%llx.%s", |
| 110 | (unsigned long long)addr, node->name); |
| 111 | return; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * No BusID, use the node name and add a globally incremented |
| 117 | * counter (and pray...) |
| 118 | */ |
| 119 | magic = atomic_add_return(1, &bus_no_reg_magic); |
| 120 | dev_set_name(dev, "%s.%d", node->name, magic - 1); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * of_device_alloc - Allocate and initialize an of_device |
| 125 | * @np: device node to assign to device |
| 126 | * @bus_id: Name to assign to the device. May be null to use default name. |
| 127 | * @parent: Parent device. |
| 128 | */ |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 129 | struct platform_device *of_device_alloc(struct device_node *np, |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 130 | const char *bus_id, |
| 131 | struct device *parent) |
| 132 | { |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 133 | struct platform_device *dev; |
Andres Salomon | 52f6537 | 2010-10-10 21:35:05 -0600 | [diff] [blame] | 134 | int rc, i, num_reg = 0, num_irq; |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 135 | struct resource *res, temp_res; |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 136 | |
Grant Likely | 7096d04 | 2010-10-20 11:45:13 -0600 | [diff] [blame] | 137 | dev = platform_device_alloc("", -1); |
| 138 | if (!dev) |
| 139 | return NULL; |
| 140 | |
| 141 | /* count the io and irq resources */ |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 142 | while (of_address_to_resource(np, num_reg, &temp_res) == 0) |
| 143 | num_reg++; |
Andres Salomon | 52f6537 | 2010-10-10 21:35:05 -0600 | [diff] [blame] | 144 | num_irq = of_irq_count(np); |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 145 | |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 146 | /* Populate the resource table */ |
| 147 | if (num_irq || num_reg) { |
Grant Likely | 7096d04 | 2010-10-20 11:45:13 -0600 | [diff] [blame] | 148 | res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL); |
| 149 | if (!res) { |
| 150 | platform_device_put(dev); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 154 | dev->num_resources = num_reg + num_irq; |
| 155 | dev->resource = res; |
| 156 | for (i = 0; i < num_reg; i++, res++) { |
| 157 | rc = of_address_to_resource(np, i, res); |
| 158 | WARN_ON(rc); |
| 159 | } |
Andres Salomon | 52f6537 | 2010-10-10 21:35:05 -0600 | [diff] [blame] | 160 | WARN_ON(of_irq_to_resource_table(np, res, num_irq) != num_irq); |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 161 | } |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 162 | |
| 163 | dev->dev.of_node = of_node_get(np); |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 164 | #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE) |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 165 | dev->dev.dma_mask = &dev->archdata.dma_mask; |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 166 | #endif |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 167 | dev->dev.parent = parent; |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 168 | |
| 169 | if (bus_id) |
| 170 | dev_set_name(&dev->dev, "%s", bus_id); |
| 171 | else |
| 172 | of_device_make_bus_id(&dev->dev); |
| 173 | |
| 174 | return dev; |
| 175 | } |
| 176 | EXPORT_SYMBOL(of_device_alloc); |
| 177 | |
| 178 | /** |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 179 | * of_platform_device_create - Alloc, initialize and register an of_device |
| 180 | * @np: pointer to node to create device for |
| 181 | * @bus_id: name to assign device |
| 182 | * @parent: Linux device model parent device. |
Grant Likely | cd1e650 | 2011-01-03 15:51:11 -0700 | [diff] [blame] | 183 | * |
| 184 | * Returns pointer to created platform device, or NULL if a device was not |
| 185 | * registered. Unavailable devices will not get registered. |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 186 | */ |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 187 | struct platform_device *of_platform_device_create(struct device_node *np, |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 188 | const char *bus_id, |
| 189 | struct device *parent) |
| 190 | { |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 191 | struct platform_device *dev; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 192 | |
Grant Likely | cd1e650 | 2011-01-03 15:51:11 -0700 | [diff] [blame] | 193 | if (!of_device_is_available(np)) |
| 194 | return NULL; |
| 195 | |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 196 | dev = of_device_alloc(np, bus_id, parent); |
| 197 | if (!dev) |
| 198 | return NULL; |
| 199 | |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 200 | #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE) |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 201 | dev->archdata.dma_mask = 0xffffffffUL; |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 202 | #endif |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 203 | dev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
Grant Likely | eca3930 | 2010-06-08 07:48:21 -0600 | [diff] [blame] | 204 | dev->dev.bus = &platform_bus_type; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 205 | |
| 206 | /* We do not fill the DMA ops for platform devices by default. |
| 207 | * This is currently the responsibility of the platform code |
| 208 | * to do such, possibly using a device notifier |
| 209 | */ |
| 210 | |
Grant Likely | 7096d04 | 2010-10-20 11:45:13 -0600 | [diff] [blame] | 211 | if (of_device_add(dev) != 0) { |
| 212 | platform_device_put(dev); |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | return dev; |
| 217 | } |
| 218 | EXPORT_SYMBOL(of_platform_device_create); |
| 219 | |
| 220 | /** |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 221 | * of_platform_bus_create() - Create a device for a node and its children. |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 222 | * @bus: device node of the bus to instantiate |
Grant Likely | 1eed4c0 | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 223 | * @matches: match table for bus nodes |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 224 | * disallow recursive creation of child buses |
| 225 | * @parent: parent for new device, or NULL for top level. |
| 226 | * |
| 227 | * Creates a platform_device for the provided device_node, and optionally |
| 228 | * recursively create devices for all the child nodes. |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 229 | */ |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 230 | static int of_platform_bus_create(struct device_node *bus, |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 231 | const struct of_device_id *matches, |
Grant Likely | 29d4f8a | 2011-06-21 10:59:34 -0600 | [diff] [blame^] | 232 | struct device *parent, bool strict) |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 233 | { |
| 234 | struct device_node *child; |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 235 | struct platform_device *dev; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 236 | int rc = 0; |
| 237 | |
Grant Likely | 29d4f8a | 2011-06-21 10:59:34 -0600 | [diff] [blame^] | 238 | /* Make sure it has a compatible property */ |
| 239 | if (strict && (!of_get_property(bus, "compatible", NULL))) { |
| 240 | pr_debug("%s() - skipping %s, no compatible prop\n", |
| 241 | __func__, bus->full_name); |
| 242 | return 0; |
| 243 | } |
| 244 | |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 245 | dev = of_platform_device_create(bus, NULL, parent); |
| 246 | if (!dev || !of_match_node(matches, bus)) |
| 247 | return 0; |
| 248 | |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 249 | for_each_child_of_node(bus, child) { |
| 250 | pr_debug(" create child: %s\n", child->full_name); |
Grant Likely | 29d4f8a | 2011-06-21 10:59:34 -0600 | [diff] [blame^] | 251 | rc = of_platform_bus_create(child, matches, &dev->dev, strict); |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 252 | if (rc) { |
| 253 | of_node_put(child); |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | return rc; |
| 258 | } |
| 259 | |
| 260 | /** |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 261 | * of_platform_bus_probe() - Probe the device-tree for platform buses |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 262 | * @root: parent of the first level to probe or NULL for the root of the tree |
Grant Likely | 1eed4c0 | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 263 | * @matches: match table for bus nodes |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 264 | * @parent: parent to hook devices from, NULL for toplevel |
| 265 | * |
| 266 | * Note that children of the provided root are not instantiated as devices |
| 267 | * unless the specified root itself matches the bus list and is not NULL. |
| 268 | */ |
| 269 | int of_platform_bus_probe(struct device_node *root, |
| 270 | const struct of_device_id *matches, |
| 271 | struct device *parent) |
| 272 | { |
| 273 | struct device_node *child; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 274 | int rc = 0; |
| 275 | |
Grant Likely | 1eed4c0 | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 276 | root = root ? of_node_get(root) : of_find_node_by_path("/"); |
| 277 | if (!root) |
Grant Likely | 60d5991 | 2010-06-08 07:48:25 -0600 | [diff] [blame] | 278 | return -EINVAL; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 279 | |
| 280 | pr_debug("of_platform_bus_probe()\n"); |
| 281 | pr_debug(" starting at: %s\n", root->full_name); |
| 282 | |
Grant Likely | 1eed4c0 | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 283 | /* Do a self check of bus type, if there's a match, create children */ |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 284 | if (of_match_node(matches, root)) { |
Grant Likely | 29d4f8a | 2011-06-21 10:59:34 -0600 | [diff] [blame^] | 285 | rc = of_platform_bus_create(root, matches, parent, false); |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 286 | } else for_each_child_of_node(root, child) { |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 287 | if (!of_match_node(matches, child)) |
| 288 | continue; |
Grant Likely | 29d4f8a | 2011-06-21 10:59:34 -0600 | [diff] [blame^] | 289 | rc = of_platform_bus_create(child, matches, parent, false); |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 290 | if (rc) |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 291 | break; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 292 | } |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 293 | |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 294 | of_node_put(root); |
| 295 | return rc; |
| 296 | } |
| 297 | EXPORT_SYMBOL(of_platform_bus_probe); |
Grant Likely | 29d4f8a | 2011-06-21 10:59:34 -0600 | [diff] [blame^] | 298 | |
| 299 | /** |
| 300 | * of_platform_populate() - Populate platform_devices from device tree data |
| 301 | * @root: parent of the first level to probe or NULL for the root of the tree |
| 302 | * @matches: match table, NULL to use the default |
| 303 | * @parent: parent to hook devices from, NULL for toplevel |
| 304 | * |
| 305 | * Similar to of_platform_bus_probe(), this function walks the device tree |
| 306 | * and creates devices from nodes. It differs in that it follows the modern |
| 307 | * convention of requiring all device nodes to have a 'compatible' property, |
| 308 | * and it is suitable for creating devices which are children of the root |
| 309 | * node (of_platform_bus_probe will only create children of the root which |
| 310 | * are selected by the @matches argument). |
| 311 | * |
| 312 | * New board support should be using this function instead of |
| 313 | * of_platform_bus_probe(). |
| 314 | * |
| 315 | * Returns 0 on success, < 0 on failure. |
| 316 | */ |
| 317 | int of_platform_populate(struct device_node *root, |
| 318 | const struct of_device_id *matches, |
| 319 | struct device *parent) |
| 320 | { |
| 321 | struct device_node *child; |
| 322 | int rc = 0; |
| 323 | |
| 324 | root = root ? of_node_get(root) : of_find_node_by_path("/"); |
| 325 | if (!root) |
| 326 | return -EINVAL; |
| 327 | |
| 328 | for_each_child_of_node(root, child) { |
| 329 | rc = of_platform_bus_create(child, matches, parent, true); |
| 330 | if (rc) |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | of_node_put(root); |
| 335 | return rc; |
| 336 | } |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 337 | #endif /* !CONFIG_SPARC */ |