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