David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 1 | #include <linux/string.h> |
| 2 | #include <linux/kernel.h> |
Stephen Rothwell | f85ff30 | 2007-05-01 16:40:36 +1000 | [diff] [blame] | 3 | #include <linux/of.h> |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 4 | #include <linux/init.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/mod_devicetable.h> |
| 7 | #include <linux/slab.h> |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 8 | #include <linux/errno.h> |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 9 | #include <linux/irq.h> |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 10 | #include <linux/of_device.h> |
| 11 | #include <linux/of_platform.h> |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 12 | |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 13 | void __iomem *of_ioremap(struct resource *res, unsigned long offset, unsigned long size, char *name) |
| 14 | { |
| 15 | unsigned long ret = res->start + offset; |
David S. Miller | 6bda573 | 2006-10-18 23:00:35 -0700 | [diff] [blame] | 16 | struct resource *r; |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 17 | |
David S. Miller | 6bda573 | 2006-10-18 23:00:35 -0700 | [diff] [blame] | 18 | if (res->flags & IORESOURCE_MEM) |
| 19 | r = request_mem_region(ret, size, name); |
| 20 | else |
| 21 | r = request_region(ret, size, name); |
| 22 | if (!r) |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 23 | ret = 0; |
| 24 | |
| 25 | return (void __iomem *) ret; |
| 26 | } |
| 27 | EXPORT_SYMBOL(of_ioremap); |
| 28 | |
David S. Miller | e3a411a | 2006-12-28 21:01:32 -0800 | [diff] [blame] | 29 | void of_iounmap(struct resource *res, void __iomem *base, unsigned long size) |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 30 | { |
David S. Miller | e3a411a | 2006-12-28 21:01:32 -0800 | [diff] [blame] | 31 | if (res->flags & IORESOURCE_MEM) |
| 32 | release_mem_region((unsigned long) base, size); |
| 33 | else |
| 34 | release_region((unsigned long) base, size); |
David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 35 | } |
| 36 | EXPORT_SYMBOL(of_iounmap); |
| 37 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 38 | static int node_match(struct device *dev, void *data) |
| 39 | { |
| 40 | struct of_device *op = to_of_device(dev); |
| 41 | struct device_node *dp = data; |
| 42 | |
| 43 | return (op->node == dp); |
| 44 | } |
| 45 | |
| 46 | struct of_device *of_find_device_by_node(struct device_node *dp) |
| 47 | { |
Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 48 | struct device *dev = bus_find_device(&of_platform_bus_type, NULL, |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 49 | dp, node_match); |
| 50 | |
| 51 | if (dev) |
| 52 | return to_of_device(dev); |
| 53 | |
| 54 | return NULL; |
| 55 | } |
| 56 | EXPORT_SYMBOL(of_find_device_by_node); |
| 57 | |
David S. Miller | 4426621 | 2008-08-20 16:34:39 -0700 | [diff] [blame^] | 58 | int irq_of_parse_and_map(struct device_node *node, int index) |
| 59 | { |
| 60 | struct of_device *op = of_find_device_by_node(node); |
| 61 | |
| 62 | if (!op || index >= op->num_irqs) |
| 63 | return 0xffffffff; |
| 64 | |
| 65 | return op->irqs[index]; |
| 66 | } |
| 67 | EXPORT_SYMBOL(irq_of_parse_and_map); |
| 68 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 69 | #ifdef CONFIG_PCI |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 70 | struct bus_type ebus_bus_type; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 71 | EXPORT_SYMBOL(ebus_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 72 | #endif |
| 73 | |
| 74 | #ifdef CONFIG_SBUS |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 75 | struct bus_type sbus_bus_type; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 76 | EXPORT_SYMBOL(sbus_bus_type); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 77 | #endif |
| 78 | |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 79 | struct bus_type of_platform_bus_type; |
Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 80 | EXPORT_SYMBOL(of_platform_bus_type); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 81 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 82 | static inline u64 of_read_addr(const u32 *cell, int size) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 83 | { |
| 84 | u64 r = 0; |
| 85 | while (size--) |
| 86 | r = (r << 32) | *(cell++); |
| 87 | return r; |
| 88 | } |
| 89 | |
| 90 | static void __init get_cells(struct device_node *dp, |
| 91 | int *addrc, int *sizec) |
| 92 | { |
| 93 | if (addrc) |
| 94 | *addrc = of_n_addr_cells(dp); |
| 95 | if (sizec) |
| 96 | *sizec = of_n_size_cells(dp); |
| 97 | } |
| 98 | |
| 99 | /* Max address size we deal with */ |
| 100 | #define OF_MAX_ADDR_CELLS 4 |
| 101 | |
| 102 | struct of_bus { |
| 103 | const char *name; |
| 104 | const char *addr_prop_name; |
| 105 | int (*match)(struct device_node *parent); |
| 106 | void (*count_cells)(struct device_node *child, |
| 107 | int *addrc, int *sizec); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 108 | int (*map)(u32 *addr, const u32 *range, |
| 109 | int na, int ns, int pna); |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 110 | unsigned int (*get_flags)(const u32 *addr); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * Default translator (generic bus) |
| 115 | */ |
| 116 | |
| 117 | static void of_bus_default_count_cells(struct device_node *dev, |
| 118 | int *addrc, int *sizec) |
| 119 | { |
| 120 | get_cells(dev, addrc, sizec); |
| 121 | } |
| 122 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 123 | /* Make sure the least significant 64-bits are in-range. Even |
| 124 | * for 3 or 4 cell values it is a good enough approximation. |
| 125 | */ |
| 126 | static int of_out_of_range(const u32 *addr, const u32 *base, |
| 127 | const u32 *size, int na, int ns) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 128 | { |
| 129 | u64 a = of_read_addr(addr, na); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 130 | u64 b = of_read_addr(base, na); |
| 131 | |
| 132 | if (a < b) |
| 133 | return 1; |
| 134 | |
| 135 | b += of_read_addr(size, ns); |
| 136 | if (a >= b) |
| 137 | return 1; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int of_bus_default_map(u32 *addr, const u32 *range, |
| 143 | int na, int ns, int pna) |
| 144 | { |
| 145 | u32 result[OF_MAX_ADDR_CELLS]; |
| 146 | int i; |
| 147 | |
| 148 | if (ns > 2) { |
| 149 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | |
| 153 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | /* Start with the parent range base. */ |
| 157 | memcpy(result, range + na, pna * 4); |
| 158 | |
| 159 | /* Add in the child address offset. */ |
| 160 | for (i = 0; i < na; i++) |
| 161 | result[pna - 1 - i] += |
| 162 | (addr[na - 1 - i] - |
| 163 | range[na - 1 - i]); |
| 164 | |
| 165 | memcpy(addr, result, pna * 4); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 170 | static unsigned int of_bus_default_get_flags(const u32 *addr) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 171 | { |
| 172 | return IORESOURCE_MEM; |
| 173 | } |
| 174 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 175 | /* |
| 176 | * PCI bus specific translator |
| 177 | */ |
| 178 | |
| 179 | static int of_bus_pci_match(struct device_node *np) |
| 180 | { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 181 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { |
David S. Miller | a165b42 | 2007-03-29 01:50:16 -0700 | [diff] [blame] | 182 | const char *model = of_get_property(np, "model", NULL); |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 183 | |
| 184 | if (model && !strcmp(model, "SUNW,simba")) |
| 185 | return 0; |
| 186 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 187 | /* Do not do PCI specific frobbing if the |
| 188 | * PCI bridge lacks a ranges property. We |
| 189 | * want to pass it through up to the next |
| 190 | * parent as-is, not with the PCI translate |
| 191 | * method which chops off the top address cell. |
| 192 | */ |
| 193 | if (!of_find_property(np, "ranges", NULL)) |
| 194 | return 0; |
| 195 | |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 200 | } |
| 201 | |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 202 | static int of_bus_simba_match(struct device_node *np) |
| 203 | { |
David S. Miller | a165b42 | 2007-03-29 01:50:16 -0700 | [diff] [blame] | 204 | const char *model = of_get_property(np, "model", NULL); |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 205 | |
| 206 | if (model && !strcmp(model, "SUNW,simba")) |
| 207 | return 1; |
David S. Miller | 8c2786c | 2007-06-07 21:59:44 -0700 | [diff] [blame] | 208 | |
| 209 | /* Treat PCI busses lacking ranges property just like |
| 210 | * simba. |
| 211 | */ |
| 212 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { |
| 213 | if (!of_find_property(np, "ranges", NULL)) |
| 214 | return 1; |
| 215 | } |
| 216 | |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int of_bus_simba_map(u32 *addr, const u32 *range, |
| 221 | int na, int ns, int pna) |
| 222 | { |
| 223 | return 0; |
| 224 | } |
| 225 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 226 | static void of_bus_pci_count_cells(struct device_node *np, |
| 227 | int *addrc, int *sizec) |
| 228 | { |
| 229 | if (addrc) |
| 230 | *addrc = 3; |
| 231 | if (sizec) |
| 232 | *sizec = 2; |
| 233 | } |
| 234 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 235 | static int of_bus_pci_map(u32 *addr, const u32 *range, |
| 236 | int na, int ns, int pna) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 237 | { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 238 | u32 result[OF_MAX_ADDR_CELLS]; |
| 239 | int i; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 240 | |
| 241 | /* Check address type match */ |
| 242 | if ((addr[0] ^ range[0]) & 0x03000000) |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 243 | return -EINVAL; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 244 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 245 | if (of_out_of_range(addr + 1, range + 1, range + na + pna, |
| 246 | na - 1, ns)) |
| 247 | return -EINVAL; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 248 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 249 | /* Start with the parent range base. */ |
| 250 | memcpy(result, range + na, pna * 4); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 251 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 252 | /* Add in the child address offset, skipping high cell. */ |
| 253 | for (i = 0; i < na - 1; i++) |
| 254 | result[pna - 1 - i] += |
| 255 | (addr[na - 1 - i] - |
| 256 | range[na - 1 - i]); |
| 257 | |
| 258 | memcpy(addr, result, pna * 4); |
| 259 | |
| 260 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 263 | static unsigned int of_bus_pci_get_flags(const u32 *addr) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 264 | { |
| 265 | unsigned int flags = 0; |
| 266 | u32 w = addr[0]; |
| 267 | |
| 268 | switch((w >> 24) & 0x03) { |
| 269 | case 0x01: |
| 270 | flags |= IORESOURCE_IO; |
| 271 | case 0x02: /* 32 bits */ |
| 272 | case 0x03: /* 64 bits */ |
| 273 | flags |= IORESOURCE_MEM; |
| 274 | } |
| 275 | if (w & 0x40000000) |
| 276 | flags |= IORESOURCE_PREFETCH; |
| 277 | return flags; |
| 278 | } |
| 279 | |
| 280 | /* |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 281 | * SBUS bus specific translator |
| 282 | */ |
| 283 | |
| 284 | static int of_bus_sbus_match(struct device_node *np) |
| 285 | { |
| 286 | return !strcmp(np->name, "sbus") || |
| 287 | !strcmp(np->name, "sbi"); |
| 288 | } |
| 289 | |
| 290 | static void of_bus_sbus_count_cells(struct device_node *child, |
| 291 | int *addrc, int *sizec) |
| 292 | { |
| 293 | if (addrc) |
| 294 | *addrc = 2; |
| 295 | if (sizec) |
| 296 | *sizec = 1; |
| 297 | } |
| 298 | |
David S. Miller | 4130a4b | 2006-10-25 22:31:06 -0700 | [diff] [blame] | 299 | /* |
| 300 | * FHC/Central bus specific translator. |
| 301 | * |
| 302 | * This is just needed to hard-code the address and size cell |
| 303 | * counts. 'fhc' and 'central' nodes lack the #address-cells and |
| 304 | * #size-cells properties, and if you walk to the root on such |
| 305 | * Enterprise boxes all you'll get is a #size-cells of 2 which is |
| 306 | * not what we want to use. |
| 307 | */ |
| 308 | static int of_bus_fhc_match(struct device_node *np) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 309 | { |
David S. Miller | 4130a4b | 2006-10-25 22:31:06 -0700 | [diff] [blame] | 310 | return !strcmp(np->name, "fhc") || |
| 311 | !strcmp(np->name, "central"); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 312 | } |
| 313 | |
David S. Miller | 4130a4b | 2006-10-25 22:31:06 -0700 | [diff] [blame] | 314 | #define of_bus_fhc_count_cells of_bus_sbus_count_cells |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 315 | |
| 316 | /* |
| 317 | * Array of bus specific translators |
| 318 | */ |
| 319 | |
| 320 | static struct of_bus of_busses[] = { |
| 321 | /* PCI */ |
| 322 | { |
| 323 | .name = "pci", |
| 324 | .addr_prop_name = "assigned-addresses", |
| 325 | .match = of_bus_pci_match, |
| 326 | .count_cells = of_bus_pci_count_cells, |
| 327 | .map = of_bus_pci_map, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 328 | .get_flags = of_bus_pci_get_flags, |
| 329 | }, |
David S. Miller | 01f94c4 | 2007-03-04 12:53:19 -0800 | [diff] [blame] | 330 | /* SIMBA */ |
| 331 | { |
| 332 | .name = "simba", |
| 333 | .addr_prop_name = "assigned-addresses", |
| 334 | .match = of_bus_simba_match, |
| 335 | .count_cells = of_bus_pci_count_cells, |
| 336 | .map = of_bus_simba_map, |
| 337 | .get_flags = of_bus_pci_get_flags, |
| 338 | }, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 339 | /* SBUS */ |
| 340 | { |
| 341 | .name = "sbus", |
| 342 | .addr_prop_name = "reg", |
| 343 | .match = of_bus_sbus_match, |
| 344 | .count_cells = of_bus_sbus_count_cells, |
David S. Miller | 4130a4b | 2006-10-25 22:31:06 -0700 | [diff] [blame] | 345 | .map = of_bus_default_map, |
| 346 | .get_flags = of_bus_default_get_flags, |
| 347 | }, |
| 348 | /* FHC */ |
| 349 | { |
| 350 | .name = "fhc", |
| 351 | .addr_prop_name = "reg", |
| 352 | .match = of_bus_fhc_match, |
| 353 | .count_cells = of_bus_fhc_count_cells, |
| 354 | .map = of_bus_default_map, |
| 355 | .get_flags = of_bus_default_get_flags, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 356 | }, |
| 357 | /* Default */ |
| 358 | { |
| 359 | .name = "default", |
| 360 | .addr_prop_name = "reg", |
| 361 | .match = NULL, |
| 362 | .count_cells = of_bus_default_count_cells, |
| 363 | .map = of_bus_default_map, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 364 | .get_flags = of_bus_default_get_flags, |
| 365 | }, |
| 366 | }; |
| 367 | |
| 368 | static struct of_bus *of_match_bus(struct device_node *np) |
| 369 | { |
| 370 | int i; |
| 371 | |
| 372 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) |
| 373 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 374 | return &of_busses[i]; |
| 375 | BUG(); |
| 376 | return NULL; |
| 377 | } |
| 378 | |
| 379 | static int __init build_one_resource(struct device_node *parent, |
| 380 | struct of_bus *bus, |
| 381 | struct of_bus *pbus, |
| 382 | u32 *addr, |
| 383 | int na, int ns, int pna) |
| 384 | { |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 385 | const u32 *ranges; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 386 | unsigned int rlen; |
| 387 | int rone; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 388 | |
| 389 | ranges = of_get_property(parent, "ranges", &rlen); |
| 390 | if (ranges == NULL || rlen == 0) { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 391 | u32 result[OF_MAX_ADDR_CELLS]; |
| 392 | int i; |
| 393 | |
| 394 | memset(result, 0, pna * 4); |
| 395 | for (i = 0; i < na; i++) |
| 396 | result[pna - 1 - i] = |
| 397 | addr[na - 1 - i]; |
| 398 | |
| 399 | memcpy(addr, result, pna * 4); |
| 400 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /* Now walk through the ranges */ |
| 404 | rlen /= 4; |
| 405 | rone = na + pna + ns; |
| 406 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 407 | if (!bus->map(addr, ranges, na, ns, pna)) |
| 408 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 409 | } |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 410 | |
David S. Miller | 49d23cf | 2007-05-13 22:01:18 -0700 | [diff] [blame] | 411 | /* When we miss an I/O space match on PCI, just pass it up |
| 412 | * to the next PCI bridge and/or controller. |
| 413 | */ |
| 414 | if (!strcmp(bus->name, "pci") && |
| 415 | (addr[0] & 0x03000000) == 0x01000000) |
| 416 | return 0; |
| 417 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | static int __init use_1to1_mapping(struct device_node *pp) |
| 422 | { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 423 | /* If we have a ranges property in the parent, use it. */ |
| 424 | if (of_find_property(pp, "ranges", NULL) != NULL) |
| 425 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 426 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 427 | /* If the parent is the dma node of an ISA bus, pass |
| 428 | * the translation up to the root. |
| 429 | */ |
| 430 | if (!strcmp(pp->name, "dma")) |
| 431 | return 0; |
| 432 | |
David S. Miller | 8c2786c | 2007-06-07 21:59:44 -0700 | [diff] [blame] | 433 | /* Similarly for all PCI bridges, if we get this far |
| 434 | * it lacks a ranges property, and this will include |
| 435 | * cases like Simba. |
| 436 | */ |
| 437 | if (!strcmp(pp->type, "pci") || !strcmp(pp->type, "pciex")) |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 438 | return 0; |
| 439 | |
| 440 | return 1; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 441 | } |
| 442 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 443 | static int of_resource_verbose; |
| 444 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 445 | static void __init build_device_resources(struct of_device *op, |
| 446 | struct device *parent) |
| 447 | { |
| 448 | struct of_device *p_op; |
| 449 | struct of_bus *bus; |
| 450 | int na, ns; |
| 451 | int index, num_reg; |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 452 | const void *preg; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 453 | |
| 454 | if (!parent) |
| 455 | return; |
| 456 | |
| 457 | p_op = to_of_device(parent); |
| 458 | bus = of_match_bus(p_op->node); |
| 459 | bus->count_cells(op->node, &na, &ns); |
| 460 | |
| 461 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); |
| 462 | if (!preg || num_reg == 0) |
| 463 | return; |
| 464 | |
| 465 | /* Convert to num-cells. */ |
| 466 | num_reg /= 4; |
| 467 | |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 468 | /* Convert to num-entries. */ |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 469 | num_reg /= na + ns; |
| 470 | |
Simon Arlott | e5dd42e | 2007-05-11 13:52:08 -0700 | [diff] [blame] | 471 | /* Prevent overrunning the op->resources[] array. */ |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 472 | if (num_reg > PROMREG_MAX) { |
| 473 | printk(KERN_WARNING "%s: Too many regs (%d), " |
| 474 | "limiting to %d.\n", |
| 475 | op->node->full_name, num_reg, PROMREG_MAX); |
| 476 | num_reg = PROMREG_MAX; |
| 477 | } |
| 478 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 479 | for (index = 0; index < num_reg; index++) { |
| 480 | struct resource *r = &op->resource[index]; |
| 481 | u32 addr[OF_MAX_ADDR_CELLS]; |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 482 | const u32 *reg = (preg + (index * ((na + ns) * 4))); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 483 | struct device_node *dp = op->node; |
| 484 | struct device_node *pp = p_op->node; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 485 | struct of_bus *pbus, *dbus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 486 | u64 size, result = OF_BAD_ADDR; |
| 487 | unsigned long flags; |
| 488 | int dna, dns; |
| 489 | int pna, pns; |
| 490 | |
| 491 | size = of_read_addr(reg + na, ns); |
| 492 | flags = bus->get_flags(reg); |
| 493 | |
| 494 | memcpy(addr, reg, na * 4); |
| 495 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 496 | if (use_1to1_mapping(pp)) { |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 497 | result = of_read_addr(addr, na); |
| 498 | goto build_res; |
| 499 | } |
| 500 | |
| 501 | dna = na; |
| 502 | dns = ns; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 503 | dbus = bus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 504 | |
| 505 | while (1) { |
| 506 | dp = pp; |
| 507 | pp = dp->parent; |
| 508 | if (!pp) { |
| 509 | result = of_read_addr(addr, dna); |
| 510 | break; |
| 511 | } |
| 512 | |
| 513 | pbus = of_match_bus(pp); |
| 514 | pbus->count_cells(dp, &pna, &pns); |
| 515 | |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 516 | if (build_one_resource(dp, dbus, pbus, addr, |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 517 | dna, dns, pna)) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 518 | break; |
| 519 | |
| 520 | dna = pna; |
| 521 | dns = pns; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 522 | dbus = pbus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | build_res: |
| 526 | memset(r, 0, sizeof(*r)); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 527 | |
| 528 | if (of_resource_verbose) |
| 529 | printk("%s reg[%d] -> %lx\n", |
| 530 | op->node->full_name, index, |
| 531 | result); |
| 532 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 533 | if (result != OF_BAD_ADDR) { |
David S. Miller | 1815aed | 2006-06-29 19:58:28 -0700 | [diff] [blame] | 534 | if (tlb_type == hypervisor) |
| 535 | result &= 0x0fffffffffffffffUL; |
| 536 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 537 | r->start = result; |
| 538 | r->end = result + size - 1; |
| 539 | r->flags = flags; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 540 | } |
| 541 | r->name = op->node->name; |
| 542 | } |
| 543 | } |
| 544 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 545 | static struct device_node * __init |
| 546 | apply_interrupt_map(struct device_node *dp, struct device_node *pp, |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 547 | const u32 *imap, int imlen, const u32 *imask, |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 548 | unsigned int *irq_p) |
| 549 | { |
| 550 | struct device_node *cp; |
| 551 | unsigned int irq = *irq_p; |
| 552 | struct of_bus *bus; |
| 553 | phandle handle; |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 554 | const u32 *reg; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 555 | int na, num_reg, i; |
| 556 | |
| 557 | bus = of_match_bus(pp); |
| 558 | bus->count_cells(dp, &na, NULL); |
| 559 | |
| 560 | reg = of_get_property(dp, "reg", &num_reg); |
| 561 | if (!reg || !num_reg) |
| 562 | return NULL; |
| 563 | |
| 564 | imlen /= ((na + 3) * 4); |
| 565 | handle = 0; |
| 566 | for (i = 0; i < imlen; i++) { |
| 567 | int j; |
| 568 | |
| 569 | for (j = 0; j < na; j++) { |
| 570 | if ((reg[j] & imask[j]) != imap[j]) |
| 571 | goto next; |
| 572 | } |
| 573 | if (imap[na] == irq) { |
| 574 | handle = imap[na + 1]; |
| 575 | irq = imap[na + 2]; |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | next: |
| 580 | imap += (na + 3); |
| 581 | } |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 582 | if (i == imlen) { |
| 583 | /* Psycho and Sabre PCI controllers can have 'interrupt-map' |
| 584 | * properties that do not include the on-board device |
| 585 | * interrupts. Instead, the device's 'interrupts' property |
| 586 | * is already a fully specified INO value. |
| 587 | * |
| 588 | * Handle this by deciding that, if we didn't get a |
| 589 | * match in the parent's 'interrupt-map', and the |
| 590 | * parent is an IRQ translater, then use the parent as |
| 591 | * our IRQ controller. |
| 592 | */ |
| 593 | if (pp->irq_trans) |
| 594 | return pp; |
| 595 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 596 | return NULL; |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 597 | } |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 598 | |
| 599 | *irq_p = irq; |
| 600 | cp = of_find_node_by_phandle(handle); |
| 601 | |
| 602 | return cp; |
| 603 | } |
| 604 | |
| 605 | static unsigned int __init pci_irq_swizzle(struct device_node *dp, |
| 606 | struct device_node *pp, |
| 607 | unsigned int irq) |
| 608 | { |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 609 | const struct linux_prom_pci_registers *regs; |
David S. Miller | bb4c18c | 2007-02-26 14:55:06 -0800 | [diff] [blame] | 610 | unsigned int bus, devfn, slot, ret; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 611 | |
| 612 | if (irq < 1 || irq > 4) |
| 613 | return irq; |
| 614 | |
| 615 | regs = of_get_property(dp, "reg", NULL); |
| 616 | if (!regs) |
| 617 | return irq; |
| 618 | |
David S. Miller | bb4c18c | 2007-02-26 14:55:06 -0800 | [diff] [blame] | 619 | bus = (regs->phys_hi >> 16) & 0xff; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 620 | devfn = (regs->phys_hi >> 8) & 0xff; |
| 621 | slot = (devfn >> 3) & 0x1f; |
| 622 | |
David S. Miller | bb4c18c | 2007-02-26 14:55:06 -0800 | [diff] [blame] | 623 | if (pp->irq_trans) { |
| 624 | /* Derived from Table 8-3, U2P User's Manual. This branch |
| 625 | * is handling a PCI controller that lacks a proper set of |
| 626 | * interrupt-map and interrupt-map-mask properties. The |
| 627 | * Ultra-E450 is one example. |
| 628 | * |
| 629 | * The bit layout is BSSLL, where: |
| 630 | * B: 0 on bus A, 1 on bus B |
| 631 | * D: 2-bit slot number, derived from PCI device number as |
| 632 | * (dev - 1) for bus A, or (dev - 2) for bus B |
| 633 | * L: 2-bit line number |
David S. Miller | bb4c18c | 2007-02-26 14:55:06 -0800 | [diff] [blame] | 634 | */ |
| 635 | if (bus & 0x80) { |
| 636 | /* PBM-A */ |
| 637 | bus = 0x00; |
| 638 | slot = (slot - 1) << 2; |
| 639 | } else { |
| 640 | /* PBM-B */ |
| 641 | bus = 0x10; |
| 642 | slot = (slot - 2) << 2; |
| 643 | } |
| 644 | irq -= 1; |
| 645 | |
| 646 | ret = (bus | slot | irq); |
| 647 | } else { |
| 648 | /* Going through a PCI-PCI bridge that lacks a set of |
| 649 | * interrupt-map and interrupt-map-mask properties. |
| 650 | */ |
| 651 | ret = ((irq - 1 + (slot & 3)) & 3) + 1; |
| 652 | } |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 653 | |
| 654 | return ret; |
| 655 | } |
| 656 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 657 | static int of_irq_verbose; |
| 658 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 659 | static unsigned int __init build_one_device_irq(struct of_device *op, |
| 660 | struct device *parent, |
| 661 | unsigned int irq) |
| 662 | { |
| 663 | struct device_node *dp = op->node; |
| 664 | struct device_node *pp, *ip; |
| 665 | unsigned int orig_irq = irq; |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 666 | int nid; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 667 | |
| 668 | if (irq == 0xffffffff) |
| 669 | return irq; |
| 670 | |
| 671 | if (dp->irq_trans) { |
| 672 | irq = dp->irq_trans->irq_build(dp, irq, |
| 673 | dp->irq_trans->data); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 674 | |
| 675 | if (of_irq_verbose) |
| 676 | printk("%s: direct translate %x --> %x\n", |
| 677 | dp->full_name, orig_irq, irq); |
| 678 | |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 679 | goto out; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* Something more complicated. Walk up to the root, applying |
| 683 | * interrupt-map or bus specific translations, until we hit |
| 684 | * an IRQ translator. |
| 685 | * |
| 686 | * If we hit a bus type or situation we cannot handle, we |
| 687 | * stop and assume that the original IRQ number was in a |
| 688 | * format which has special meaning to it's immediate parent. |
| 689 | */ |
| 690 | pp = dp->parent; |
| 691 | ip = NULL; |
| 692 | while (pp) { |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 693 | const void *imap, *imsk; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 694 | int imlen; |
| 695 | |
| 696 | imap = of_get_property(pp, "interrupt-map", &imlen); |
| 697 | imsk = of_get_property(pp, "interrupt-map-mask", NULL); |
| 698 | if (imap && imsk) { |
| 699 | struct device_node *iret; |
| 700 | int this_orig_irq = irq; |
| 701 | |
| 702 | iret = apply_interrupt_map(dp, pp, |
| 703 | imap, imlen, imsk, |
| 704 | &irq); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 705 | |
| 706 | if (of_irq_verbose) |
| 707 | printk("%s: Apply [%s:%x] imap --> [%s:%x]\n", |
| 708 | op->node->full_name, |
| 709 | pp->full_name, this_orig_irq, |
| 710 | (iret ? iret->full_name : "NULL"), irq); |
| 711 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 712 | if (!iret) |
| 713 | break; |
| 714 | |
| 715 | if (iret->irq_trans) { |
| 716 | ip = iret; |
| 717 | break; |
| 718 | } |
| 719 | } else { |
| 720 | if (!strcmp(pp->type, "pci") || |
| 721 | !strcmp(pp->type, "pciex")) { |
| 722 | unsigned int this_orig_irq = irq; |
| 723 | |
| 724 | irq = pci_irq_swizzle(dp, pp, irq); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 725 | if (of_irq_verbose) |
| 726 | printk("%s: PCI swizzle [%s] " |
| 727 | "%x --> %x\n", |
| 728 | op->node->full_name, |
| 729 | pp->full_name, this_orig_irq, |
| 730 | irq); |
| 731 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | if (pp->irq_trans) { |
| 735 | ip = pp; |
| 736 | break; |
| 737 | } |
| 738 | } |
| 739 | dp = pp; |
| 740 | pp = pp->parent; |
| 741 | } |
| 742 | if (!ip) |
| 743 | return orig_irq; |
| 744 | |
| 745 | irq = ip->irq_trans->irq_build(op->node, irq, |
| 746 | ip->irq_trans->data); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 747 | if (of_irq_verbose) |
| 748 | printk("%s: Apply IRQ trans [%s] %x --> %x\n", |
| 749 | op->node->full_name, ip->full_name, orig_irq, irq); |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 750 | |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 751 | out: |
| 752 | nid = of_node_to_nid(dp); |
| 753 | if (nid != -1) { |
| 754 | cpumask_t numa_mask = node_to_cpumask(nid); |
| 755 | |
| 756 | irq_set_affinity(irq, numa_mask); |
| 757 | } |
| 758 | |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 759 | return irq; |
| 760 | } |
| 761 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 762 | static struct of_device * __init scan_one_device(struct device_node *dp, |
| 763 | struct device *parent) |
| 764 | { |
| 765 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); |
Stephen Rothwell | 6a23acf | 2007-04-23 15:53:27 -0700 | [diff] [blame] | 766 | const unsigned int *irq; |
David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 767 | struct dev_archdata *sd; |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 768 | int len, i; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 769 | |
| 770 | if (!op) |
| 771 | return NULL; |
| 772 | |
David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 773 | sd = &op->dev.archdata; |
| 774 | sd->prom_node = dp; |
| 775 | sd->op = op; |
| 776 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 777 | op->node = dp; |
| 778 | |
| 779 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", |
| 780 | (25*1000*1000)); |
| 781 | op->portid = of_getintprop_default(dp, "upa-portid", -1); |
| 782 | if (op->portid == -1) |
| 783 | op->portid = of_getintprop_default(dp, "portid", -1); |
| 784 | |
| 785 | irq = of_get_property(dp, "interrupts", &len); |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 786 | if (irq) { |
| 787 | memcpy(op->irqs, irq, len); |
| 788 | op->num_irqs = len / 4; |
| 789 | } else { |
| 790 | op->num_irqs = 0; |
| 791 | } |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 792 | |
Simon Arlott | e5dd42e | 2007-05-11 13:52:08 -0700 | [diff] [blame] | 793 | /* Prevent overrunning the op->irqs[] array. */ |
David S. Miller | 46ba6d7 | 2006-07-16 22:10:44 -0700 | [diff] [blame] | 794 | if (op->num_irqs > PROMINTR_MAX) { |
| 795 | printk(KERN_WARNING "%s: Too many irqs (%d), " |
| 796 | "limiting to %d.\n", |
| 797 | dp->full_name, op->num_irqs, PROMINTR_MAX); |
| 798 | op->num_irqs = PROMINTR_MAX; |
| 799 | } |
| 800 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 801 | build_device_resources(op, parent); |
David S. Miller | 2b1e597 | 2006-06-29 15:07:37 -0700 | [diff] [blame] | 802 | for (i = 0; i < op->num_irqs; i++) |
| 803 | op->irqs[i] = build_one_device_irq(op, parent, op->irqs[i]); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 804 | |
| 805 | op->dev.parent = parent; |
Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 806 | op->dev.bus = &of_platform_bus_type; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 807 | if (!parent) |
Greg Kroah-Hartman | 2222c31 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 808 | dev_set_name(&op->dev, "root"); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 809 | else |
Greg Kroah-Hartman | 2222c31 | 2008-05-02 06:02:41 +0200 | [diff] [blame] | 810 | dev_set_name(&op->dev, "%08x", dp->node); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 811 | |
| 812 | if (of_device_register(op)) { |
| 813 | printk("%s: Could not register of device.\n", |
| 814 | dp->full_name); |
| 815 | kfree(op); |
| 816 | op = NULL; |
| 817 | } |
| 818 | |
| 819 | return op; |
| 820 | } |
| 821 | |
| 822 | static void __init scan_tree(struct device_node *dp, struct device *parent) |
| 823 | { |
| 824 | while (dp) { |
| 825 | struct of_device *op = scan_one_device(dp, parent); |
| 826 | |
| 827 | if (op) |
| 828 | scan_tree(dp->child, &op->dev); |
| 829 | |
| 830 | dp = dp->sibling; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | static void __init scan_of_devices(void) |
| 835 | { |
| 836 | struct device_node *root = of_find_node_by_path("/"); |
| 837 | struct of_device *parent; |
| 838 | |
| 839 | parent = scan_one_device(root, NULL); |
| 840 | if (!parent) |
| 841 | return; |
| 842 | |
| 843 | scan_tree(root->child, &parent->dev); |
| 844 | } |
| 845 | |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 846 | static int __init of_bus_driver_init(void) |
| 847 | { |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 848 | int err; |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 849 | |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 850 | err = of_bus_type_init(&of_platform_bus_type, "of"); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 851 | #ifdef CONFIG_PCI |
| 852 | if (!err) |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 853 | err = of_bus_type_init(&ebus_bus_type, "ebus"); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 854 | #endif |
| 855 | #ifdef CONFIG_SBUS |
| 856 | if (!err) |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 857 | err = of_bus_type_init(&sbus_bus_type, "sbus"); |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 858 | #endif |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 859 | |
| 860 | if (!err) |
| 861 | scan_of_devices(); |
| 862 | |
| 863 | return err; |
David S. Miller | a2bd4fd | 2006-06-23 01:44:10 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | postcore_initcall(of_bus_driver_init); |
| 867 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 868 | static int __init of_debug(char *str) |
| 869 | { |
| 870 | int val = 0; |
| 871 | |
| 872 | get_option(&str, &val); |
| 873 | if (val & 1) |
| 874 | of_resource_verbose = 1; |
| 875 | if (val & 2) |
| 876 | of_irq_verbose = 1; |
| 877 | return 1; |
| 878 | } |
| 879 | |
| 880 | __setup("of_debug=", of_debug); |