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