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