David S. Miller | 372b07b | 2006-06-21 15:35:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Procedures for creating, accessing and interpreting the device tree. |
| 3 | * |
| 4 | * Paul Mackerras August 1996. |
| 5 | * Copyright (C) 1996-2005 Paul Mackerras. |
| 6 | * |
| 7 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner. |
| 8 | * {engebret|bergner}@us.ibm.com |
| 9 | * |
| 10 | * Adapted for sparc64 by David S. Miller davem@davemloft.net |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version |
| 15 | * 2 of the License, or (at your option) any later version. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/types.h> |
| 20 | #include <linux/string.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/bootmem.h> |
| 23 | |
| 24 | #include <asm/prom.h> |
| 25 | #include <asm/oplib.h> |
| 26 | |
| 27 | static struct device_node *allnodes; |
| 28 | |
| 29 | struct device_node *of_get_parent(const struct device_node *node) |
| 30 | { |
| 31 | struct device_node *np; |
| 32 | |
| 33 | if (!node) |
| 34 | return NULL; |
| 35 | |
| 36 | np = node->parent; |
| 37 | |
| 38 | return np; |
| 39 | } |
| 40 | |
| 41 | struct device_node *of_get_next_child(const struct device_node *node, |
| 42 | struct device_node *prev) |
| 43 | { |
| 44 | struct device_node *next; |
| 45 | |
| 46 | next = prev ? prev->sibling : node->child; |
| 47 | for (; next != 0; next = next->sibling) { |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | return next; |
| 52 | } |
| 53 | |
| 54 | struct device_node *of_find_node_by_path(const char *path) |
| 55 | { |
| 56 | struct device_node *np = allnodes; |
| 57 | |
| 58 | for (; np != 0; np = np->allnext) { |
| 59 | if (np->full_name != 0 && strcmp(np->full_name, path) == 0) |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | return np; |
| 64 | } |
| 65 | |
David S. Miller | aaf7cec | 2006-06-21 16:33:54 -0700 | [diff] [blame] | 66 | struct device_node *of_find_node_by_name(struct device_node *from, |
| 67 | const char *name) |
| 68 | { |
| 69 | struct device_node *np; |
| 70 | |
| 71 | np = from ? from->allnext : allnodes; |
| 72 | for (; np != NULL; np = np->allnext) |
| 73 | if (np->name != NULL && strcmp(np->name, name) == 0) |
| 74 | break; |
| 75 | |
| 76 | return np; |
| 77 | } |
| 78 | |
| 79 | struct device_node *of_find_node_by_type(struct device_node *from, |
| 80 | const char *type) |
| 81 | { |
| 82 | struct device_node *np; |
| 83 | |
| 84 | np = from ? from->allnext : allnodes; |
| 85 | for (; np != 0; np = np->allnext) |
| 86 | if (np->type != 0 && strcmp(np->type, type) == 0) |
| 87 | break; |
| 88 | |
| 89 | return np; |
| 90 | } |
| 91 | |
David S. Miller | 372b07b | 2006-06-21 15:35:28 -0700 | [diff] [blame] | 92 | struct property *of_find_property(struct device_node *np, const char *name, |
| 93 | int *lenp) |
| 94 | { |
| 95 | struct property *pp; |
| 96 | |
| 97 | for (pp = np->properties; pp != 0; pp = pp->next) { |
| 98 | if (strcmp(pp->name, name) == 0) { |
| 99 | if (lenp != 0) |
| 100 | *lenp = pp->length; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | return pp; |
| 105 | } |
| 106 | |
David S. Miller | 6d30772 | 2006-06-21 23:07:29 -0700 | [diff] [blame^] | 107 | int of_getintprop_default(struct device_node *np, const char *name, int def) |
| 108 | { |
| 109 | struct property *prop; |
| 110 | int len; |
| 111 | |
| 112 | prop = of_find_property(np, name, &len); |
| 113 | if (!prop || len != 4) |
| 114 | return def; |
| 115 | |
| 116 | return *(int *) prop->value; |
| 117 | } |
| 118 | |
David S. Miller | 372b07b | 2006-06-21 15:35:28 -0700 | [diff] [blame] | 119 | static unsigned int prom_early_allocated; |
| 120 | |
| 121 | static void * __init prom_early_alloc(unsigned long size) |
| 122 | { |
| 123 | void *ret; |
| 124 | |
| 125 | ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL); |
| 126 | if (ret != NULL) |
| 127 | memset(ret, 0, size); |
| 128 | |
| 129 | prom_early_allocated += size; |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | static int is_root_node(const struct device_node *dp) |
| 135 | { |
| 136 | if (!dp) |
| 137 | return 0; |
| 138 | |
| 139 | return (dp->parent == NULL); |
| 140 | } |
| 141 | |
| 142 | /* The following routines deal with the black magic of fully naming a |
| 143 | * node. |
| 144 | * |
| 145 | * Certain well known named nodes are just the simple name string. |
| 146 | * |
| 147 | * Actual devices have an address specifier appended to the base name |
| 148 | * string, like this "foo@addr". The "addr" can be in any number of |
| 149 | * formats, and the platform plus the type of the node determine the |
| 150 | * format and how it is constructed. |
| 151 | * |
| 152 | * For children of the ROOT node, the naming convention is fixed and |
| 153 | * determined by whether this is a sun4u or sun4v system. |
| 154 | * |
| 155 | * For children of other nodes, it is bus type specific. So |
| 156 | * we walk up the tree until we discover a "device_type" property |
| 157 | * we recognize and we go from there. |
| 158 | * |
| 159 | * As an example, the boot device on my workstation has a full path: |
| 160 | * |
| 161 | * /pci@1e,600000/ide@d/disk@0,0:c |
| 162 | */ |
| 163 | static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf) |
| 164 | { |
| 165 | struct linux_prom64_registers *regs; |
| 166 | struct property *rprop; |
| 167 | u32 high_bits, low_bits, type; |
| 168 | |
| 169 | rprop = of_find_property(dp, "reg", NULL); |
| 170 | if (!rprop) |
| 171 | return; |
| 172 | |
| 173 | regs = rprop->value; |
| 174 | if (!is_root_node(dp->parent)) { |
| 175 | sprintf(tmp_buf, "%s@%x,%x", |
| 176 | dp->name, |
| 177 | (unsigned int) (regs->phys_addr >> 32UL), |
| 178 | (unsigned int) (regs->phys_addr & 0xffffffffUL)); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | type = regs->phys_addr >> 60UL; |
| 183 | high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL; |
| 184 | low_bits = (regs->phys_addr & 0xffffffffUL); |
| 185 | |
| 186 | if (type == 0 || type == 8) { |
| 187 | const char *prefix = (type == 0) ? "m" : "i"; |
| 188 | |
| 189 | if (low_bits) |
| 190 | sprintf(tmp_buf, "%s@%s%x,%x", |
| 191 | dp->name, prefix, |
| 192 | high_bits, low_bits); |
| 193 | else |
| 194 | sprintf(tmp_buf, "%s@%s%x", |
| 195 | dp->name, |
| 196 | prefix, |
| 197 | high_bits); |
| 198 | } else if (type == 12) { |
| 199 | sprintf(tmp_buf, "%s@%x", |
| 200 | dp->name, high_bits); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf) |
| 205 | { |
| 206 | struct linux_prom64_registers *regs; |
| 207 | struct property *prop; |
| 208 | |
| 209 | prop = of_find_property(dp, "reg", NULL); |
| 210 | if (!prop) |
| 211 | return; |
| 212 | |
| 213 | regs = prop->value; |
| 214 | if (!is_root_node(dp->parent)) { |
| 215 | sprintf(tmp_buf, "%s@%x,%x", |
| 216 | dp->name, |
| 217 | (unsigned int) (regs->phys_addr >> 32UL), |
| 218 | (unsigned int) (regs->phys_addr & 0xffffffffUL)); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | prop = of_find_property(dp, "upa-portid", NULL); |
| 223 | if (!prop) |
| 224 | prop = of_find_property(dp, "portid", NULL); |
| 225 | if (prop) { |
| 226 | unsigned long mask = 0xffffffffUL; |
| 227 | |
| 228 | if (tlb_type >= cheetah) |
| 229 | mask = 0x7fffff; |
| 230 | |
| 231 | sprintf(tmp_buf, "%s@%x,%x", |
| 232 | dp->name, |
| 233 | *(u32 *)prop->value, |
| 234 | (unsigned int) (regs->phys_addr & mask)); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* "name@slot,offset" */ |
| 239 | static void __init sbus_path_component(struct device_node *dp, char *tmp_buf) |
| 240 | { |
| 241 | struct linux_prom_registers *regs; |
| 242 | struct property *prop; |
| 243 | |
| 244 | prop = of_find_property(dp, "reg", NULL); |
| 245 | if (!prop) |
| 246 | return; |
| 247 | |
| 248 | regs = prop->value; |
| 249 | sprintf(tmp_buf, "%s@%x,%x", |
| 250 | dp->name, |
| 251 | regs->which_io, |
| 252 | regs->phys_addr); |
| 253 | } |
| 254 | |
| 255 | /* "name@devnum[,func]" */ |
| 256 | static void __init pci_path_component(struct device_node *dp, char *tmp_buf) |
| 257 | { |
| 258 | struct linux_prom_pci_registers *regs; |
| 259 | struct property *prop; |
| 260 | unsigned int devfn; |
| 261 | |
| 262 | prop = of_find_property(dp, "reg", NULL); |
| 263 | if (!prop) |
| 264 | return; |
| 265 | |
| 266 | regs = prop->value; |
| 267 | devfn = (regs->phys_hi >> 8) & 0xff; |
| 268 | if (devfn & 0x07) { |
| 269 | sprintf(tmp_buf, "%s@%x,%x", |
| 270 | dp->name, |
| 271 | devfn >> 3, |
| 272 | devfn & 0x07); |
| 273 | } else { |
| 274 | sprintf(tmp_buf, "%s@%x", |
| 275 | dp->name, |
| 276 | devfn >> 3); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /* "name@UPA_PORTID,offset" */ |
| 281 | static void __init upa_path_component(struct device_node *dp, char *tmp_buf) |
| 282 | { |
| 283 | struct linux_prom64_registers *regs; |
| 284 | struct property *prop; |
| 285 | |
| 286 | prop = of_find_property(dp, "reg", NULL); |
| 287 | if (!prop) |
| 288 | return; |
| 289 | |
| 290 | regs = prop->value; |
| 291 | |
| 292 | prop = of_find_property(dp, "upa-portid", NULL); |
| 293 | if (!prop) |
| 294 | return; |
| 295 | |
| 296 | sprintf(tmp_buf, "%s@%x,%x", |
| 297 | dp->name, |
| 298 | *(u32 *) prop->value, |
| 299 | (unsigned int) (regs->phys_addr & 0xffffffffUL)); |
| 300 | } |
| 301 | |
| 302 | /* "name@reg" */ |
| 303 | static void __init vdev_path_component(struct device_node *dp, char *tmp_buf) |
| 304 | { |
| 305 | struct property *prop; |
| 306 | u32 *regs; |
| 307 | |
| 308 | prop = of_find_property(dp, "reg", NULL); |
| 309 | if (!prop) |
| 310 | return; |
| 311 | |
| 312 | regs = prop->value; |
| 313 | |
| 314 | sprintf(tmp_buf, "%s@%x", dp->name, *regs); |
| 315 | } |
| 316 | |
| 317 | /* "name@addrhi,addrlo" */ |
| 318 | static void __init ebus_path_component(struct device_node *dp, char *tmp_buf) |
| 319 | { |
| 320 | struct linux_prom64_registers *regs; |
| 321 | struct property *prop; |
| 322 | |
| 323 | prop = of_find_property(dp, "reg", NULL); |
| 324 | if (!prop) |
| 325 | return; |
| 326 | |
| 327 | regs = prop->value; |
| 328 | |
| 329 | sprintf(tmp_buf, "%s@%x,%x", |
| 330 | dp->name, |
| 331 | (unsigned int) (regs->phys_addr >> 32UL), |
| 332 | (unsigned int) (regs->phys_addr & 0xffffffffUL)); |
| 333 | } |
| 334 | |
| 335 | /* "name@bus,addr" */ |
| 336 | static void __init i2c_path_component(struct device_node *dp, char *tmp_buf) |
| 337 | { |
| 338 | struct property *prop; |
| 339 | u32 *regs; |
| 340 | |
| 341 | prop = of_find_property(dp, "reg", NULL); |
| 342 | if (!prop) |
| 343 | return; |
| 344 | |
| 345 | regs = prop->value; |
| 346 | |
| 347 | /* This actually isn't right... should look at the #address-cells |
| 348 | * property of the i2c bus node etc. etc. |
| 349 | */ |
| 350 | sprintf(tmp_buf, "%s@%x,%x", |
| 351 | dp->name, regs[0], regs[1]); |
| 352 | } |
| 353 | |
| 354 | /* "name@reg0[,reg1]" */ |
| 355 | static void __init usb_path_component(struct device_node *dp, char *tmp_buf) |
| 356 | { |
| 357 | struct property *prop; |
| 358 | u32 *regs; |
| 359 | |
| 360 | prop = of_find_property(dp, "reg", NULL); |
| 361 | if (!prop) |
| 362 | return; |
| 363 | |
| 364 | regs = prop->value; |
| 365 | |
| 366 | if (prop->length == sizeof(u32) || regs[1] == 1) { |
| 367 | sprintf(tmp_buf, "%s@%x", |
| 368 | dp->name, regs[0]); |
| 369 | } else { |
| 370 | sprintf(tmp_buf, "%s@%x,%x", |
| 371 | dp->name, regs[0], regs[1]); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* "name@reg0reg1[,reg2reg3]" */ |
| 376 | static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf) |
| 377 | { |
| 378 | struct property *prop; |
| 379 | u32 *regs; |
| 380 | |
| 381 | prop = of_find_property(dp, "reg", NULL); |
| 382 | if (!prop) |
| 383 | return; |
| 384 | |
| 385 | regs = prop->value; |
| 386 | |
| 387 | if (regs[2] || regs[3]) { |
| 388 | sprintf(tmp_buf, "%s@%08x%08x,%04x%08x", |
| 389 | dp->name, regs[0], regs[1], regs[2], regs[3]); |
| 390 | } else { |
| 391 | sprintf(tmp_buf, "%s@%08x%08x", |
| 392 | dp->name, regs[0], regs[1]); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | static void __init __build_path_component(struct device_node *dp, char *tmp_buf) |
| 397 | { |
| 398 | struct device_node *parent = dp->parent; |
| 399 | |
| 400 | if (parent != NULL) { |
| 401 | if (!strcmp(parent->type, "pci") || |
| 402 | !strcmp(parent->type, "pciex")) |
| 403 | return pci_path_component(dp, tmp_buf); |
| 404 | if (!strcmp(parent->type, "sbus")) |
| 405 | return sbus_path_component(dp, tmp_buf); |
| 406 | if (!strcmp(parent->type, "upa")) |
| 407 | return upa_path_component(dp, tmp_buf); |
| 408 | if (!strcmp(parent->type, "ebus")) |
| 409 | return ebus_path_component(dp, tmp_buf); |
| 410 | if (!strcmp(parent->name, "usb") || |
| 411 | !strcmp(parent->name, "hub")) |
| 412 | return usb_path_component(dp, tmp_buf); |
| 413 | if (!strcmp(parent->type, "i2c")) |
| 414 | return i2c_path_component(dp, tmp_buf); |
| 415 | if (!strcmp(parent->type, "firewire")) |
| 416 | return ieee1394_path_component(dp, tmp_buf); |
| 417 | if (!strcmp(parent->type, "virtual-devices")) |
| 418 | return vdev_path_component(dp, tmp_buf); |
| 419 | |
| 420 | /* "isa" is handled with platform naming */ |
| 421 | } |
| 422 | |
| 423 | /* Use platform naming convention. */ |
| 424 | if (tlb_type == hypervisor) |
| 425 | return sun4v_path_component(dp, tmp_buf); |
| 426 | else |
| 427 | return sun4u_path_component(dp, tmp_buf); |
| 428 | } |
| 429 | |
| 430 | static char * __init build_path_component(struct device_node *dp) |
| 431 | { |
| 432 | char tmp_buf[64], *n; |
| 433 | |
| 434 | tmp_buf[0] = '\0'; |
| 435 | __build_path_component(dp, tmp_buf); |
| 436 | if (tmp_buf[0] == '\0') |
| 437 | strcpy(tmp_buf, dp->name); |
| 438 | |
| 439 | n = prom_early_alloc(strlen(tmp_buf) + 1); |
| 440 | strcpy(n, tmp_buf); |
| 441 | |
| 442 | return n; |
| 443 | } |
| 444 | |
| 445 | static char * __init build_full_name(struct device_node *dp) |
| 446 | { |
| 447 | int len, ourlen, plen; |
| 448 | char *n; |
| 449 | |
| 450 | plen = strlen(dp->parent->full_name); |
| 451 | ourlen = strlen(dp->path_component_name); |
| 452 | len = ourlen + plen + 2; |
| 453 | |
| 454 | n = prom_early_alloc(len); |
| 455 | strcpy(n, dp->parent->full_name); |
| 456 | if (!is_root_node(dp->parent)) { |
| 457 | strcpy(n + plen, "/"); |
| 458 | plen++; |
| 459 | } |
| 460 | strcpy(n + plen, dp->path_component_name); |
| 461 | |
| 462 | return n; |
| 463 | } |
| 464 | |
| 465 | static struct property * __init build_one_prop(phandle node, char *prev) |
| 466 | { |
| 467 | static struct property *tmp = NULL; |
| 468 | struct property *p; |
| 469 | |
| 470 | if (tmp) { |
| 471 | p = tmp; |
| 472 | memset(p, 0, sizeof(*p) + 32); |
| 473 | tmp = NULL; |
| 474 | } else |
| 475 | p = prom_early_alloc(sizeof(struct property) + 32); |
| 476 | |
| 477 | p->name = (char *) (p + 1); |
| 478 | if (prev == NULL) { |
| 479 | prom_firstprop(node, p->name); |
| 480 | } else { |
| 481 | prom_nextprop(node, prev, p->name); |
| 482 | } |
| 483 | if (strlen(p->name) == 0) { |
| 484 | tmp = p; |
| 485 | return NULL; |
| 486 | } |
| 487 | p->length = prom_getproplen(node, p->name); |
| 488 | if (p->length <= 0) { |
| 489 | p->length = 0; |
| 490 | } else { |
| 491 | p->value = prom_early_alloc(p->length); |
| 492 | prom_getproperty(node, p->name, p->value, p->length); |
| 493 | } |
| 494 | return p; |
| 495 | } |
| 496 | |
| 497 | static struct property * __init build_prop_list(phandle node) |
| 498 | { |
| 499 | struct property *head, *tail; |
| 500 | |
| 501 | head = tail = build_one_prop(node, NULL); |
| 502 | while(tail) { |
| 503 | tail->next = build_one_prop(node, tail->name); |
| 504 | tail = tail->next; |
| 505 | } |
| 506 | |
| 507 | return head; |
| 508 | } |
| 509 | |
| 510 | static char * __init get_one_property(phandle node, const char *name) |
| 511 | { |
| 512 | char *buf = "<NULL>"; |
| 513 | int len; |
| 514 | |
| 515 | len = prom_getproplen(node, name); |
| 516 | if (len > 0) { |
| 517 | buf = prom_early_alloc(len); |
| 518 | prom_getproperty(node, name, buf, len); |
| 519 | } |
| 520 | |
| 521 | return buf; |
| 522 | } |
| 523 | |
| 524 | static struct device_node * __init create_node(phandle node) |
| 525 | { |
| 526 | struct device_node *dp; |
| 527 | |
| 528 | if (!node) |
| 529 | return NULL; |
| 530 | |
| 531 | dp = prom_early_alloc(sizeof(*dp)); |
| 532 | |
| 533 | kref_init(&dp->kref); |
| 534 | |
| 535 | dp->name = get_one_property(node, "name"); |
| 536 | dp->type = get_one_property(node, "device_type"); |
| 537 | dp->node = node; |
| 538 | |
| 539 | /* Build interrupts later... */ |
| 540 | |
| 541 | dp->properties = build_prop_list(node); |
| 542 | |
| 543 | return dp; |
| 544 | } |
| 545 | |
| 546 | static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp) |
| 547 | { |
| 548 | struct device_node *dp; |
| 549 | |
| 550 | dp = create_node(node); |
| 551 | if (dp) { |
| 552 | *(*nextp) = dp; |
| 553 | *nextp = &dp->allnext; |
| 554 | |
| 555 | dp->parent = parent; |
| 556 | dp->path_component_name = build_path_component(dp); |
| 557 | dp->full_name = build_full_name(dp); |
| 558 | |
| 559 | dp->child = build_tree(dp, prom_getchild(node), nextp); |
| 560 | |
| 561 | dp->sibling = build_tree(parent, prom_getsibling(node), nextp); |
| 562 | } |
| 563 | |
| 564 | return dp; |
| 565 | } |
| 566 | |
| 567 | void __init prom_build_devicetree(void) |
| 568 | { |
| 569 | struct device_node **nextp; |
| 570 | |
| 571 | allnodes = create_node(prom_root_node); |
| 572 | allnodes->path_component_name = ""; |
| 573 | allnodes->full_name = "/"; |
| 574 | |
| 575 | nextp = &allnodes->allnext; |
| 576 | allnodes->child = build_tree(allnodes, |
| 577 | prom_getchild(allnodes->node), |
| 578 | &nextp); |
| 579 | printk("PROM: Built device tree with %u bytes of memory.\n", |
| 580 | prom_early_allocated); |
| 581 | } |