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