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