David S. Miller | fd53143 | 2006-06-23 15:55:17 -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 | fd53143 | 2006-06-23 15:55:17 -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> |
| 9 | #include <linux/of_device.h> |
| 10 | #include <linux/of_platform.h> |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 11 | |
David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 12 | static int node_match(struct device *dev, void *data) |
| 13 | { |
| 14 | struct of_device *op = to_of_device(dev); |
| 15 | struct device_node *dp = data; |
| 16 | |
| 17 | return (op->node == dp); |
| 18 | } |
| 19 | |
| 20 | struct of_device *of_find_device_by_node(struct device_node *dp) |
| 21 | { |
Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 22 | struct device *dev = bus_find_device(&of_platform_bus_type, NULL, |
David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 23 | dp, node_match); |
| 24 | |
| 25 | if (dev) |
| 26 | return to_of_device(dev); |
| 27 | |
| 28 | return NULL; |
| 29 | } |
| 30 | EXPORT_SYMBOL(of_find_device_by_node); |
| 31 | |
David S. Miller | 51e0f00 | 2008-08-25 16:44:58 -0700 | [diff] [blame^] | 32 | 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] | 33 | { |
| 34 | struct of_device *op = of_find_device_by_node(node); |
| 35 | |
| 36 | if (!op || index >= op->num_irqs) |
David S. Miller | 51e0f00 | 2008-08-25 16:44:58 -0700 | [diff] [blame^] | 37 | return 0; |
David S. Miller | 4426621 | 2008-08-20 16:34:39 -0700 | [diff] [blame] | 38 | |
| 39 | return op->irqs[index]; |
| 40 | } |
| 41 | EXPORT_SYMBOL(irq_of_parse_and_map); |
| 42 | |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 43 | #ifdef CONFIG_PCI |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 44 | struct bus_type ebus_bus_type; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 45 | EXPORT_SYMBOL(ebus_bus_type); |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | #ifdef CONFIG_SBUS |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 49 | struct bus_type sbus_bus_type; |
David S. Miller | 6985391 | 2006-06-25 01:21:38 -0700 | [diff] [blame] | 50 | EXPORT_SYMBOL(sbus_bus_type); |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 51 | #endif |
| 52 | |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 53 | struct bus_type of_platform_bus_type; |
Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 54 | EXPORT_SYMBOL(of_platform_bus_type); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 55 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 56 | static inline u64 of_read_addr(const u32 *cell, int size) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 57 | { |
| 58 | u64 r = 0; |
| 59 | while (size--) |
| 60 | r = (r << 32) | *(cell++); |
| 61 | return r; |
| 62 | } |
| 63 | |
| 64 | static void __init get_cells(struct device_node *dp, |
| 65 | int *addrc, int *sizec) |
| 66 | { |
| 67 | if (addrc) |
| 68 | *addrc = of_n_addr_cells(dp); |
| 69 | if (sizec) |
| 70 | *sizec = of_n_size_cells(dp); |
| 71 | } |
| 72 | |
| 73 | /* Max address size we deal with */ |
| 74 | #define OF_MAX_ADDR_CELLS 4 |
| 75 | |
| 76 | struct of_bus { |
| 77 | const char *name; |
| 78 | const char *addr_prop_name; |
| 79 | int (*match)(struct device_node *parent); |
| 80 | void (*count_cells)(struct device_node *child, |
| 81 | int *addrc, int *sizec); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 82 | int (*map)(u32 *addr, const u32 *range, |
| 83 | int na, int ns, int pna); |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 84 | unsigned int (*get_flags)(const u32 *addr); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /* |
| 88 | * Default translator (generic bus) |
| 89 | */ |
| 90 | |
| 91 | static void of_bus_default_count_cells(struct device_node *dev, |
| 92 | int *addrc, int *sizec) |
| 93 | { |
| 94 | get_cells(dev, addrc, sizec); |
| 95 | } |
| 96 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 97 | /* Make sure the least significant 64-bits are in-range. Even |
| 98 | * for 3 or 4 cell values it is a good enough approximation. |
| 99 | */ |
| 100 | static int of_out_of_range(const u32 *addr, const u32 *base, |
| 101 | const u32 *size, int na, int ns) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 102 | { |
| 103 | u64 a = of_read_addr(addr, na); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 104 | u64 b = of_read_addr(base, na); |
| 105 | |
| 106 | if (a < b) |
| 107 | return 1; |
| 108 | |
| 109 | b += of_read_addr(size, ns); |
| 110 | if (a >= b) |
| 111 | return 1; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int of_bus_default_map(u32 *addr, const u32 *range, |
| 117 | int na, int ns, int pna) |
| 118 | { |
| 119 | u32 result[OF_MAX_ADDR_CELLS]; |
| 120 | int i; |
| 121 | |
| 122 | if (ns > 2) { |
| 123 | printk("of_device: Cannot handle size cells (%d) > 2.", ns); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | if (of_out_of_range(addr, range, range + na + pna, na, ns)) |
| 128 | return -EINVAL; |
| 129 | |
| 130 | /* Start with the parent range base. */ |
| 131 | memcpy(result, range + na, pna * 4); |
| 132 | |
| 133 | /* Add in the child address offset. */ |
| 134 | for (i = 0; i < na; i++) |
| 135 | result[pna - 1 - i] += |
| 136 | (addr[na - 1 - i] - |
| 137 | range[na - 1 - i]); |
| 138 | |
| 139 | memcpy(addr, result, pna * 4); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 144 | static unsigned int of_bus_default_get_flags(const u32 *addr) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 145 | { |
| 146 | return IORESOURCE_MEM; |
| 147 | } |
| 148 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 149 | /* |
| 150 | * PCI bus specific translator |
| 151 | */ |
| 152 | |
| 153 | static int of_bus_pci_match(struct device_node *np) |
| 154 | { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 155 | if (!strcmp(np->type, "pci") || !strcmp(np->type, "pciex")) { |
| 156 | /* Do not do PCI specific frobbing if the |
| 157 | * PCI bridge lacks a ranges property. We |
| 158 | * want to pass it through up to the next |
| 159 | * parent as-is, not with the PCI translate |
| 160 | * method which chops off the top address cell. |
| 161 | */ |
| 162 | if (!of_find_property(np, "ranges", NULL)) |
| 163 | return 0; |
| 164 | |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void of_bus_pci_count_cells(struct device_node *np, |
| 172 | int *addrc, int *sizec) |
| 173 | { |
| 174 | if (addrc) |
| 175 | *addrc = 3; |
| 176 | if (sizec) |
| 177 | *sizec = 2; |
| 178 | } |
| 179 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 180 | static int of_bus_pci_map(u32 *addr, const u32 *range, |
| 181 | int na, int ns, int pna) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 182 | { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 183 | u32 result[OF_MAX_ADDR_CELLS]; |
| 184 | int i; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 185 | |
| 186 | /* Check address type match */ |
| 187 | if ((addr[0] ^ range[0]) & 0x03000000) |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 188 | return -EINVAL; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 189 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 190 | if (of_out_of_range(addr + 1, range + 1, range + na + pna, |
| 191 | na - 1, ns)) |
| 192 | return -EINVAL; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 193 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 194 | /* Start with the parent range base. */ |
| 195 | memcpy(result, range + na, pna * 4); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 196 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 197 | /* Add in the child address offset, skipping high cell. */ |
| 198 | for (i = 0; i < na - 1; i++) |
| 199 | result[pna - 1 - i] += |
| 200 | (addr[na - 1 - i] - |
| 201 | range[na - 1 - i]); |
| 202 | |
| 203 | memcpy(addr, result, pna * 4); |
| 204 | |
| 205 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 208 | static unsigned int of_bus_pci_get_flags(const u32 *addr) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 209 | { |
| 210 | unsigned int flags = 0; |
| 211 | u32 w = addr[0]; |
| 212 | |
| 213 | switch((w >> 24) & 0x03) { |
| 214 | case 0x01: |
| 215 | flags |= IORESOURCE_IO; |
| 216 | case 0x02: /* 32 bits */ |
| 217 | case 0x03: /* 64 bits */ |
| 218 | flags |= IORESOURCE_MEM; |
| 219 | } |
| 220 | if (w & 0x40000000) |
| 221 | flags |= IORESOURCE_PREFETCH; |
| 222 | return flags; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * SBUS bus specific translator |
| 227 | */ |
| 228 | |
| 229 | static int of_bus_sbus_match(struct device_node *np) |
| 230 | { |
| 231 | return !strcmp(np->name, "sbus") || |
| 232 | !strcmp(np->name, "sbi"); |
| 233 | } |
| 234 | |
| 235 | static void of_bus_sbus_count_cells(struct device_node *child, |
| 236 | int *addrc, int *sizec) |
| 237 | { |
| 238 | if (addrc) |
| 239 | *addrc = 2; |
| 240 | if (sizec) |
| 241 | *sizec = 1; |
| 242 | } |
| 243 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 244 | static int of_bus_sbus_map(u32 *addr, const u32 *range, int na, int ns, int pna) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 245 | { |
| 246 | return of_bus_default_map(addr, range, na, ns, pna); |
| 247 | } |
| 248 | |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 249 | static unsigned int of_bus_sbus_get_flags(const u32 *addr) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 250 | { |
| 251 | return IORESOURCE_MEM; |
| 252 | } |
| 253 | |
| 254 | |
| 255 | /* |
| 256 | * Array of bus specific translators |
| 257 | */ |
| 258 | |
| 259 | static struct of_bus of_busses[] = { |
| 260 | /* PCI */ |
| 261 | { |
| 262 | .name = "pci", |
| 263 | .addr_prop_name = "assigned-addresses", |
| 264 | .match = of_bus_pci_match, |
| 265 | .count_cells = of_bus_pci_count_cells, |
| 266 | .map = of_bus_pci_map, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 267 | .get_flags = of_bus_pci_get_flags, |
| 268 | }, |
| 269 | /* SBUS */ |
| 270 | { |
| 271 | .name = "sbus", |
| 272 | .addr_prop_name = "reg", |
| 273 | .match = of_bus_sbus_match, |
| 274 | .count_cells = of_bus_sbus_count_cells, |
| 275 | .map = of_bus_sbus_map, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 276 | .get_flags = of_bus_sbus_get_flags, |
| 277 | }, |
| 278 | /* Default */ |
| 279 | { |
| 280 | .name = "default", |
| 281 | .addr_prop_name = "reg", |
| 282 | .match = NULL, |
| 283 | .count_cells = of_bus_default_count_cells, |
| 284 | .map = of_bus_default_map, |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 285 | .get_flags = of_bus_default_get_flags, |
| 286 | }, |
| 287 | }; |
| 288 | |
| 289 | static struct of_bus *of_match_bus(struct device_node *np) |
| 290 | { |
| 291 | int i; |
| 292 | |
| 293 | for (i = 0; i < ARRAY_SIZE(of_busses); i ++) |
| 294 | if (!of_busses[i].match || of_busses[i].match(np)) |
| 295 | return &of_busses[i]; |
| 296 | BUG(); |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | static int __init build_one_resource(struct device_node *parent, |
| 301 | struct of_bus *bus, |
| 302 | struct of_bus *pbus, |
| 303 | u32 *addr, |
| 304 | int na, int ns, int pna) |
| 305 | { |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 306 | const u32 *ranges; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 307 | unsigned int rlen; |
| 308 | int rone; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 309 | |
| 310 | ranges = of_get_property(parent, "ranges", &rlen); |
| 311 | if (ranges == NULL || rlen == 0) { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 312 | u32 result[OF_MAX_ADDR_CELLS]; |
| 313 | int i; |
| 314 | |
| 315 | memset(result, 0, pna * 4); |
| 316 | for (i = 0; i < na; i++) |
| 317 | result[pna - 1 - i] = |
| 318 | addr[na - 1 - i]; |
| 319 | |
| 320 | memcpy(addr, result, pna * 4); |
| 321 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* Now walk through the ranges */ |
| 325 | rlen /= 4; |
| 326 | rone = na + pna + ns; |
| 327 | for (; rlen >= rone; rlen -= rone, ranges += rone) { |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 328 | if (!bus->map(addr, ranges, na, ns, pna)) |
| 329 | return 0; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 330 | } |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 331 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 332 | return 1; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 333 | } |
| 334 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 335 | static int of_resource_verbose; |
| 336 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 337 | static void __init build_device_resources(struct of_device *op, |
| 338 | struct device *parent) |
| 339 | { |
| 340 | struct of_device *p_op; |
| 341 | struct of_bus *bus; |
| 342 | int na, ns; |
| 343 | int index, num_reg; |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 344 | const void *preg; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 345 | |
| 346 | if (!parent) |
| 347 | return; |
| 348 | |
| 349 | p_op = to_of_device(parent); |
| 350 | bus = of_match_bus(p_op->node); |
| 351 | bus->count_cells(op->node, &na, &ns); |
| 352 | |
| 353 | preg = of_get_property(op->node, bus->addr_prop_name, &num_reg); |
| 354 | if (!preg || num_reg == 0) |
| 355 | return; |
| 356 | |
| 357 | /* Convert to num-cells. */ |
| 358 | num_reg /= 4; |
| 359 | |
| 360 | /* Conver to num-entries. */ |
| 361 | num_reg /= na + ns; |
| 362 | |
| 363 | for (index = 0; index < num_reg; index++) { |
| 364 | struct resource *r = &op->resource[index]; |
| 365 | u32 addr[OF_MAX_ADDR_CELLS]; |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 366 | const u32 *reg = (preg + (index * ((na + ns) * 4))); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 367 | struct device_node *dp = op->node; |
| 368 | struct device_node *pp = p_op->node; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 369 | struct of_bus *pbus, *dbus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 370 | u64 size, result = OF_BAD_ADDR; |
| 371 | unsigned long flags; |
| 372 | int dna, dns; |
| 373 | int pna, pns; |
| 374 | |
| 375 | size = of_read_addr(reg + na, ns); |
| 376 | flags = bus->get_flags(reg); |
| 377 | |
| 378 | memcpy(addr, reg, na * 4); |
| 379 | |
| 380 | /* If the immediate parent has no ranges property to apply, |
| 381 | * just use a 1<->1 mapping. |
| 382 | */ |
| 383 | if (of_find_property(pp, "ranges", NULL) == NULL) { |
| 384 | result = of_read_addr(addr, na); |
| 385 | goto build_res; |
| 386 | } |
| 387 | |
| 388 | dna = na; |
| 389 | dns = ns; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 390 | dbus = bus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 391 | |
| 392 | while (1) { |
| 393 | dp = pp; |
| 394 | pp = dp->parent; |
| 395 | if (!pp) { |
| 396 | result = of_read_addr(addr, dna); |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | pbus = of_match_bus(pp); |
| 401 | pbus->count_cells(dp, &pna, &pns); |
| 402 | |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 403 | if (build_one_resource(dp, dbus, pbus, addr, |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 404 | dna, dns, pna)) |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 405 | break; |
| 406 | |
| 407 | dna = pna; |
| 408 | dns = pns; |
David S. Miller | b85cdd4 | 2007-02-28 23:20:12 -0800 | [diff] [blame] | 409 | dbus = pbus; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | build_res: |
| 413 | memset(r, 0, sizeof(*r)); |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 414 | |
| 415 | if (of_resource_verbose) |
| 416 | printk("%s reg[%d] -> %llx\n", |
| 417 | op->node->full_name, index, |
| 418 | result); |
| 419 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 420 | if (result != OF_BAD_ADDR) { |
David S. Miller | 95714e1 | 2006-06-29 14:35:14 -0700 | [diff] [blame] | 421 | r->start = result & 0xffffffff; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 422 | r->end = result + size - 1; |
David S. Miller | 95714e1 | 2006-06-29 14:35:14 -0700 | [diff] [blame] | 423 | r->flags = flags | ((result >> 32ULL) & 0xffUL); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 424 | } |
| 425 | r->name = op->node->name; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | static struct of_device * __init scan_one_device(struct device_node *dp, |
| 430 | struct device *parent) |
| 431 | { |
| 432 | struct of_device *op = kzalloc(sizeof(*op), GFP_KERNEL); |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 433 | const struct linux_prom_irqs *intr; |
David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 434 | struct dev_archdata *sd; |
David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 435 | int len, i; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 436 | |
| 437 | if (!op) |
| 438 | return NULL; |
| 439 | |
David S. Miller | 3d6e470 | 2007-07-18 22:03:25 -0700 | [diff] [blame] | 440 | sd = &op->dev.archdata; |
| 441 | sd->prom_node = dp; |
| 442 | sd->op = op; |
| 443 | |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 444 | op->node = dp; |
| 445 | |
| 446 | op->clock_freq = of_getintprop_default(dp, "clock-frequency", |
| 447 | (25*1000*1000)); |
| 448 | op->portid = of_getintprop_default(dp, "upa-portid", -1); |
| 449 | if (op->portid == -1) |
| 450 | op->portid = of_getintprop_default(dp, "portid", -1); |
| 451 | |
David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 452 | intr = of_get_property(dp, "intr", &len); |
| 453 | if (intr) { |
| 454 | op->num_irqs = len / sizeof(struct linux_prom_irqs); |
| 455 | for (i = 0; i < op->num_irqs; i++) |
| 456 | op->irqs[i] = intr[i].pri; |
| 457 | } else { |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 458 | const unsigned int *irq = |
| 459 | of_get_property(dp, "interrupts", &len); |
David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 460 | |
| 461 | if (irq) { |
| 462 | op->num_irqs = len / sizeof(unsigned int); |
| 463 | for (i = 0; i < op->num_irqs; i++) |
| 464 | op->irqs[i] = irq[i]; |
| 465 | } else { |
| 466 | op->num_irqs = 0; |
| 467 | } |
| 468 | } |
| 469 | if (sparc_cpu_model == sun4d) { |
| 470 | static int pil_to_sbus[] = { |
| 471 | 0, 0, 1, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 0, |
| 472 | }; |
David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 473 | struct device_node *io_unit, *sbi = dp->parent; |
Stephen Rothwell | 8271f04 | 2007-03-29 00:47:23 -0700 | [diff] [blame] | 474 | const struct linux_prom_registers *regs; |
David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 475 | int board, slot; |
| 476 | |
| 477 | while (sbi) { |
| 478 | if (!strcmp(sbi->name, "sbi")) |
| 479 | break; |
| 480 | |
| 481 | sbi = sbi->parent; |
| 482 | } |
| 483 | if (!sbi) |
| 484 | goto build_resources; |
David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 485 | |
| 486 | regs = of_get_property(dp, "reg", NULL); |
David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 487 | if (!regs) |
| 488 | goto build_resources; |
| 489 | |
David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 490 | slot = regs->which_io; |
| 491 | |
David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 492 | /* If SBI's parent is not io-unit or the io-unit lacks |
| 493 | * a "board#" property, something is very wrong. |
| 494 | */ |
| 495 | if (!sbi->parent || strcmp(sbi->parent->name, "io-unit")) { |
| 496 | printk("%s: Error, parent is not io-unit.\n", |
| 497 | sbi->full_name); |
| 498 | goto build_resources; |
| 499 | } |
| 500 | io_unit = sbi->parent; |
| 501 | board = of_getintprop_default(io_unit, "board#", -1); |
| 502 | if (board == -1) { |
| 503 | printk("%s: Error, lacks board# property.\n", |
| 504 | io_unit->full_name); |
| 505 | goto build_resources; |
| 506 | } |
| 507 | |
David S. Miller | 8f96cd1 | 2006-06-29 15:08:02 -0700 | [diff] [blame] | 508 | for (i = 0; i < op->num_irqs; i++) { |
| 509 | int this_irq = op->irqs[i]; |
| 510 | int sbusl = pil_to_sbus[this_irq]; |
| 511 | |
| 512 | if (sbusl) |
| 513 | this_irq = (((board + 1) << 5) + |
| 514 | (sbusl << 2) + |
| 515 | slot); |
| 516 | |
| 517 | op->irqs[i] = this_irq; |
| 518 | } |
| 519 | } |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 520 | |
David S. Miller | 9d7ab1f | 2006-07-17 21:39:09 -0700 | [diff] [blame] | 521 | build_resources: |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 522 | build_device_resources(op, parent); |
| 523 | |
| 524 | op->dev.parent = parent; |
Stephen Rothwell | 37b7754 | 2007-04-30 17:43:56 +1000 | [diff] [blame] | 525 | op->dev.bus = &of_platform_bus_type; |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 526 | if (!parent) |
| 527 | strcpy(op->dev.bus_id, "root"); |
| 528 | else |
David S. Miller | f5ef9d1 | 2006-10-27 01:03:31 -0700 | [diff] [blame] | 529 | sprintf(op->dev.bus_id, "%08x", dp->node); |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 530 | |
| 531 | if (of_device_register(op)) { |
| 532 | printk("%s: Could not register of device.\n", |
| 533 | dp->full_name); |
| 534 | kfree(op); |
| 535 | op = NULL; |
| 536 | } |
| 537 | |
| 538 | return op; |
| 539 | } |
| 540 | |
| 541 | static void __init scan_tree(struct device_node *dp, struct device *parent) |
| 542 | { |
| 543 | while (dp) { |
| 544 | struct of_device *op = scan_one_device(dp, parent); |
| 545 | |
| 546 | if (op) |
| 547 | scan_tree(dp->child, &op->dev); |
| 548 | |
| 549 | dp = dp->sibling; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | static void __init scan_of_devices(void) |
| 554 | { |
| 555 | struct device_node *root = of_find_node_by_path("/"); |
| 556 | struct of_device *parent; |
| 557 | |
| 558 | parent = scan_one_device(root, NULL); |
| 559 | if (!parent) |
| 560 | return; |
| 561 | |
| 562 | scan_tree(root->child, &parent->dev); |
| 563 | } |
| 564 | |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 565 | static int __init of_bus_driver_init(void) |
| 566 | { |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 567 | int err; |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 568 | |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 569 | err = of_bus_type_init(&of_platform_bus_type, "of"); |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 570 | #ifdef CONFIG_PCI |
| 571 | if (!err) |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 572 | err = of_bus_type_init(&ebus_bus_type, "ebus"); |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 573 | #endif |
| 574 | #ifdef CONFIG_SBUS |
| 575 | if (!err) |
Stephen Rothwell | 3f23de1 | 2007-05-03 02:38:57 +1000 | [diff] [blame] | 576 | err = of_bus_type_init(&sbus_bus_type, "sbus"); |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 577 | #endif |
David S. Miller | cf44bbc | 2006-06-29 14:34:50 -0700 | [diff] [blame] | 578 | |
| 579 | if (!err) |
| 580 | scan_of_devices(); |
| 581 | |
| 582 | return err; |
David S. Miller | fd53143 | 2006-06-23 15:55:17 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | postcore_initcall(of_bus_driver_init); |
| 586 | |
David S. Miller | a83f982 | 2006-07-12 23:19:31 -0700 | [diff] [blame] | 587 | static int __init of_debug(char *str) |
| 588 | { |
| 589 | int val = 0; |
| 590 | |
| 591 | get_option(&str, &val); |
| 592 | if (val & 1) |
| 593 | of_resource_verbose = 1; |
| 594 | return 1; |
| 595 | } |
| 596 | |
| 597 | __setup("of_debug=", of_debug); |