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 | |
Jonas Bonn | c608558 | 2010-07-23 19:19:35 +0200 | [diff] [blame] | 25 | static int of_dev_node_match(struct device *dev, void *data) |
| 26 | { |
| 27 | return dev->of_node == data; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * of_find_device_by_node - Find the platform_device associated with a node |
| 32 | * @np: Pointer to device tree node |
| 33 | * |
| 34 | * Returns platform_device pointer, or NULL if not found |
| 35 | */ |
| 36 | struct platform_device *of_find_device_by_node(struct device_node *np) |
| 37 | { |
| 38 | struct device *dev; |
| 39 | |
| 40 | dev = bus_find_device(&platform_bus_type, NULL, np, of_dev_node_match); |
| 41 | return dev ? to_platform_device(dev) : NULL; |
| 42 | } |
| 43 | EXPORT_SYMBOL(of_find_device_by_node); |
| 44 | |
Grant Likely | 596c955 | 2010-07-14 23:51:43 -0600 | [diff] [blame] | 45 | #if defined(CONFIG_PPC_DCR) |
| 46 | #include <asm/dcr.h> |
| 47 | #endif |
| 48 | |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 49 | #if !defined(CONFIG_SPARC) |
| 50 | /* |
| 51 | * The following routines scan a subtree and registers a device for |
| 52 | * each applicable node. |
| 53 | * |
| 54 | * Note: sparc doesn't use these routines because it has a different |
| 55 | * mechanism for creating devices from device tree nodes. |
| 56 | */ |
| 57 | |
| 58 | /** |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 59 | * of_device_make_bus_id - Use the device node data to assign a unique name |
| 60 | * @dev: pointer to device structure that is linked to a device tree node |
| 61 | * |
| 62 | * This routine will first try using either the dcr-reg or the reg property |
| 63 | * value to derive a unique name. As a last resort it will use the node |
| 64 | * name followed by a unique number. |
| 65 | */ |
Grant Likely | c660122 | 2010-07-23 15:04:01 -0600 | [diff] [blame] | 66 | void of_device_make_bus_id(struct device *dev) |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 67 | { |
| 68 | static atomic_t bus_no_reg_magic; |
| 69 | struct device_node *node = dev->of_node; |
| 70 | const u32 *reg; |
| 71 | u64 addr; |
| 72 | int magic; |
| 73 | |
| 74 | #ifdef CONFIG_PPC_DCR |
| 75 | /* |
| 76 | * If it's a DCR based device, use 'd' for native DCRs |
| 77 | * and 'D' for MMIO DCRs. |
| 78 | */ |
| 79 | reg = of_get_property(node, "dcr-reg", NULL); |
| 80 | if (reg) { |
| 81 | #ifdef CONFIG_PPC_DCR_NATIVE |
| 82 | dev_set_name(dev, "d%x.%s", *reg, node->name); |
| 83 | #else /* CONFIG_PPC_DCR_NATIVE */ |
| 84 | u64 addr = of_translate_dcr_address(node, *reg, NULL); |
| 85 | if (addr != OF_BAD_ADDR) { |
| 86 | dev_set_name(dev, "D%llx.%s", |
| 87 | (unsigned long long)addr, node->name); |
| 88 | return; |
| 89 | } |
| 90 | #endif /* !CONFIG_PPC_DCR_NATIVE */ |
| 91 | } |
| 92 | #endif /* CONFIG_PPC_DCR */ |
| 93 | |
| 94 | /* |
| 95 | * For MMIO, get the physical address |
| 96 | */ |
| 97 | reg = of_get_property(node, "reg", NULL); |
| 98 | if (reg) { |
| 99 | addr = of_translate_address(node, reg); |
| 100 | if (addr != OF_BAD_ADDR) { |
| 101 | dev_set_name(dev, "%llx.%s", |
| 102 | (unsigned long long)addr, node->name); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * No BusID, use the node name and add a globally incremented |
| 109 | * counter (and pray...) |
| 110 | */ |
| 111 | magic = atomic_add_return(1, &bus_no_reg_magic); |
| 112 | dev_set_name(dev, "%s.%d", node->name, magic - 1); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * of_device_alloc - Allocate and initialize an of_device |
| 117 | * @np: device node to assign to device |
| 118 | * @bus_id: Name to assign to the device. May be null to use default name. |
| 119 | * @parent: Parent device. |
| 120 | */ |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 121 | struct platform_device *of_device_alloc(struct device_node *np, |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 122 | const char *bus_id, |
| 123 | struct device *parent) |
| 124 | { |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 125 | struct platform_device *dev; |
Andres Salomon | 52f6537 | 2010-10-10 21:35:05 -0600 | [diff] [blame] | 126 | int rc, i, num_reg = 0, num_irq; |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 127 | struct resource *res, temp_res; |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 128 | |
Grant Likely | 7096d04 | 2010-10-20 11:45:13 -0600 | [diff] [blame] | 129 | dev = platform_device_alloc("", -1); |
| 130 | if (!dev) |
| 131 | return NULL; |
| 132 | |
| 133 | /* count the io and irq resources */ |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 134 | while (of_address_to_resource(np, num_reg, &temp_res) == 0) |
| 135 | num_reg++; |
Andres Salomon | 52f6537 | 2010-10-10 21:35:05 -0600 | [diff] [blame] | 136 | num_irq = of_irq_count(np); |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 137 | |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 138 | /* Populate the resource table */ |
| 139 | if (num_irq || num_reg) { |
Grant Likely | 7096d04 | 2010-10-20 11:45:13 -0600 | [diff] [blame] | 140 | res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL); |
| 141 | if (!res) { |
| 142 | platform_device_put(dev); |
| 143 | return NULL; |
| 144 | } |
| 145 | |
Grant Likely | ac80a51 | 2010-06-08 07:48:15 -0600 | [diff] [blame] | 146 | dev->num_resources = num_reg + num_irq; |
| 147 | dev->resource = res; |
| 148 | for (i = 0; i < num_reg; i++, res++) { |
| 149 | rc = of_address_to_resource(np, i, res); |
| 150 | WARN_ON(rc); |
| 151 | } |
Andres Salomon | 52f6537 | 2010-10-10 21:35:05 -0600 | [diff] [blame] | 152 | 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] | 153 | } |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 154 | |
| 155 | dev->dev.of_node = of_node_get(np); |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 156 | #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE) |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 157 | dev->dev.dma_mask = &dev->archdata.dma_mask; |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 158 | #endif |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 159 | dev->dev.parent = parent; |
Grant Likely | 94c0931 | 2010-06-08 07:48:14 -0600 | [diff] [blame] | 160 | |
| 161 | if (bus_id) |
| 162 | dev_set_name(&dev->dev, "%s", bus_id); |
| 163 | else |
| 164 | of_device_make_bus_id(&dev->dev); |
| 165 | |
| 166 | return dev; |
| 167 | } |
| 168 | EXPORT_SYMBOL(of_device_alloc); |
| 169 | |
| 170 | /** |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 171 | * of_platform_device_create - Alloc, initialize and register an of_device |
| 172 | * @np: pointer to node to create device for |
| 173 | * @bus_id: name to assign device |
| 174 | * @parent: Linux device model parent device. |
Grant Likely | cd1e650 | 2011-01-03 15:51:11 -0700 | [diff] [blame] | 175 | * |
| 176 | * Returns pointer to created platform device, or NULL if a device was not |
| 177 | * registered. Unavailable devices will not get registered. |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 178 | */ |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 179 | struct platform_device *of_platform_device_create(struct device_node *np, |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 180 | const char *bus_id, |
| 181 | struct device *parent) |
| 182 | { |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 183 | struct platform_device *dev; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 184 | |
Grant Likely | cd1e650 | 2011-01-03 15:51:11 -0700 | [diff] [blame] | 185 | if (!of_device_is_available(np)) |
| 186 | return NULL; |
| 187 | |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 188 | dev = of_device_alloc(np, bus_id, parent); |
| 189 | if (!dev) |
| 190 | return NULL; |
| 191 | |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 192 | #if defined(CONFIG_PPC) || defined(CONFIG_MICROBLAZE) |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 193 | dev->archdata.dma_mask = 0xffffffffUL; |
Grant Likely | 9e3288d | 2010-06-08 07:48:26 -0600 | [diff] [blame] | 194 | #endif |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 195 | dev->dev.coherent_dma_mask = DMA_BIT_MASK(32); |
Grant Likely | eca3930 | 2010-06-08 07:48:21 -0600 | [diff] [blame] | 196 | dev->dev.bus = &platform_bus_type; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 197 | |
| 198 | /* We do not fill the DMA ops for platform devices by default. |
| 199 | * This is currently the responsibility of the platform code |
| 200 | * to do such, possibly using a device notifier |
| 201 | */ |
| 202 | |
Grant Likely | 7096d04 | 2010-10-20 11:45:13 -0600 | [diff] [blame] | 203 | if (of_device_add(dev) != 0) { |
| 204 | platform_device_put(dev); |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 205 | return NULL; |
| 206 | } |
| 207 | |
| 208 | return dev; |
| 209 | } |
| 210 | EXPORT_SYMBOL(of_platform_device_create); |
| 211 | |
| 212 | /** |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 213 | * 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] | 214 | * @bus: device node of the bus to instantiate |
Grant Likely | 1eed4c0 | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 215 | * @matches: match table for bus nodes |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 216 | * disallow recursive creation of child buses |
| 217 | * @parent: parent for new device, or NULL for top level. |
| 218 | * |
| 219 | * Creates a platform_device for the provided device_node, and optionally |
| 220 | * recursively create devices for all the child nodes. |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 221 | */ |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 222 | static int of_platform_bus_create(struct device_node *bus, |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 223 | const struct of_device_id *matches, |
| 224 | struct device *parent) |
| 225 | { |
| 226 | struct device_node *child; |
Grant Likely | 94a0cb1 | 2010-07-22 13:59:23 -0600 | [diff] [blame] | 227 | struct platform_device *dev; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 228 | int rc = 0; |
| 229 | |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 230 | dev = of_platform_device_create(bus, NULL, parent); |
| 231 | if (!dev || !of_match_node(matches, bus)) |
| 232 | return 0; |
| 233 | |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 234 | for_each_child_of_node(bus, child) { |
| 235 | pr_debug(" create child: %s\n", child->full_name); |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 236 | rc = of_platform_bus_create(child, matches, &dev->dev); |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 237 | if (rc) { |
| 238 | of_node_put(child); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | return rc; |
| 243 | } |
| 244 | |
| 245 | /** |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 246 | * of_platform_bus_probe() - Probe the device-tree for platform buses |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 247 | * @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] | 248 | * @matches: match table for bus nodes |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 249 | * @parent: parent to hook devices from, NULL for toplevel |
| 250 | * |
| 251 | * Note that children of the provided root are not instantiated as devices |
| 252 | * unless the specified root itself matches the bus list and is not NULL. |
| 253 | */ |
| 254 | int of_platform_bus_probe(struct device_node *root, |
| 255 | const struct of_device_id *matches, |
| 256 | struct device *parent) |
| 257 | { |
| 258 | struct device_node *child; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 259 | int rc = 0; |
| 260 | |
Grant Likely | 1eed4c0 | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 261 | root = root ? of_node_get(root) : of_find_node_by_path("/"); |
| 262 | if (!root) |
Grant Likely | 60d5991 | 2010-06-08 07:48:25 -0600 | [diff] [blame] | 263 | return -EINVAL; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 264 | |
| 265 | pr_debug("of_platform_bus_probe()\n"); |
| 266 | pr_debug(" starting at: %s\n", root->full_name); |
| 267 | |
Grant Likely | 1eed4c0 | 2011-03-18 10:21:29 -0600 | [diff] [blame] | 268 | /* 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] | 269 | if (of_match_node(matches, root)) { |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 270 | rc = of_platform_bus_create(root, matches, parent); |
| 271 | } else for_each_child_of_node(root, child) { |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 272 | if (!of_match_node(matches, child)) |
| 273 | continue; |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 274 | rc = of_platform_bus_create(child, matches, parent); |
| 275 | if (rc) |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 276 | break; |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 277 | } |
Grant Likely | 38e9e21 | 2011-03-18 10:21:28 -0600 | [diff] [blame] | 278 | |
Grant Likely | 5fd200f | 2010-06-08 07:48:13 -0600 | [diff] [blame] | 279 | of_node_put(root); |
| 280 | return rc; |
| 281 | } |
| 282 | EXPORT_SYMBOL(of_platform_bus_probe); |
| 283 | #endif /* !CONFIG_SPARC */ |