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 sparc32 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#include <linux/module.h> 24 25#include <asm/prom.h> 26#include <asm/oplib.h> 27 28static struct device_node *allnodes; 29 30/* use when traversing tree through the allnext, child, sibling, 31 * or parent members of struct device_node. 32 */ 33static DEFINE_RWLOCK(devtree_lock); 34 35int of_device_is_compatible(const struct device_node *device, 36 const char *compat) 37{ 38 const char* cp; 39 int cplen, l; 40 41 cp = of_get_property(device, "compatible", &cplen); 42 if (cp == NULL) 43 return 0; 44 while (cplen > 0) { 45 if (strncmp(cp, compat, strlen(compat)) == 0) 46 return 1; 47 l = strlen(cp) + 1; 48 cp += l; 49 cplen -= l; 50 } 51 52 return 0; 53} 54EXPORT_SYMBOL(of_device_is_compatible); 55 56struct device_node *of_get_parent(const struct device_node *node) 57{ 58 struct device_node *np; 59 60 if (!node) 61 return NULL; 62 63 np = node->parent; 64 65 return np; 66} 67EXPORT_SYMBOL(of_get_parent); 68 69struct device_node *of_get_next_child(const struct device_node *node, 70 struct device_node *prev) 71{ 72 struct device_node *next; 73 74 next = prev ? prev->sibling : node->child; 75 for (; next != 0; next = next->sibling) { 76 break; 77 } 78 79 return next; 80} 81EXPORT_SYMBOL(of_get_next_child); 82 83struct device_node *of_find_node_by_path(const char *path) 84{ 85 struct device_node *np = allnodes; 86 87 for (; np != 0; np = np->allnext) { 88 if (np->full_name != 0 && strcmp(np->full_name, path) == 0) 89 break; 90 } 91 92 return np; 93} 94EXPORT_SYMBOL(of_find_node_by_path); 95 96struct device_node *of_find_node_by_phandle(phandle handle) 97{ 98 struct device_node *np; 99 100 for (np = allnodes; np != 0; np = np->allnext) 101 if (np->node == handle) 102 break; 103 104 return np; 105} 106EXPORT_SYMBOL(of_find_node_by_phandle); 107 108struct device_node *of_find_node_by_name(struct device_node *from, 109 const char *name) 110{ 111 struct device_node *np; 112 113 np = from ? from->allnext : allnodes; 114 for (; np != NULL; np = np->allnext) 115 if (np->name != NULL && strcmp(np->name, name) == 0) 116 break; 117 118 return np; 119} 120EXPORT_SYMBOL(of_find_node_by_name); 121 122struct device_node *of_find_node_by_type(struct device_node *from, 123 const char *type) 124{ 125 struct device_node *np; 126 127 np = from ? from->allnext : allnodes; 128 for (; np != 0; np = np->allnext) 129 if (np->type != 0 && strcmp(np->type, type) == 0) 130 break; 131 132 return np; 133} 134EXPORT_SYMBOL(of_find_node_by_type); 135 136struct device_node *of_find_compatible_node(struct device_node *from, 137 const char *type, const char *compatible) 138{ 139 struct device_node *np; 140 141 np = from ? from->allnext : allnodes; 142 for (; np != 0; np = np->allnext) { 143 if (type != NULL 144 && !(np->type != 0 && strcmp(np->type, type) == 0)) 145 continue; 146 if (of_device_is_compatible(np, compatible)) 147 break; 148 } 149 150 return np; 151} 152EXPORT_SYMBOL(of_find_compatible_node); 153 154struct property *of_find_property(const struct device_node *np, 155 const char *name, 156 int *lenp) 157{ 158 struct property *pp; 159 160 for (pp = np->properties; pp != 0; pp = pp->next) { 161 if (strcasecmp(pp->name, name) == 0) { 162 if (lenp != 0) 163 *lenp = pp->length; 164 break; 165 } 166 } 167 return pp; 168} 169EXPORT_SYMBOL(of_find_property); 170 171/* 172 * Find a property with a given name for a given node 173 * and return the value. 174 */ 175const void *of_get_property(const struct device_node *np, const char *name, 176 int *lenp) 177{ 178 struct property *pp = of_find_property(np,name,lenp); 179 return pp ? pp->value : NULL; 180} 181EXPORT_SYMBOL(of_get_property); 182 183int of_getintprop_default(struct device_node *np, const char *name, int def) 184{ 185 struct property *prop; 186 int len; 187 188 prop = of_find_property(np, name, &len); 189 if (!prop || len != 4) 190 return def; 191 192 return *(int *) prop->value; 193} 194EXPORT_SYMBOL(of_getintprop_default); 195 196int of_n_addr_cells(struct device_node *np) 197{ 198 const int* ip; 199 do { 200 if (np->parent) 201 np = np->parent; 202 ip = of_get_property(np, "#address-cells", NULL); 203 if (ip != NULL) 204 return *ip; 205 } while (np->parent); 206 /* No #address-cells property for the root node, default to 2 */ 207 return 2; 208} 209EXPORT_SYMBOL(of_n_addr_cells); 210 211int of_n_size_cells(struct device_node *np) 212{ 213 const int* ip; 214 do { 215 if (np->parent) 216 np = np->parent; 217 ip = of_get_property(np, "#size-cells", NULL); 218 if (ip != NULL) 219 return *ip; 220 } while (np->parent); 221 /* No #size-cells property for the root node, default to 1 */ 222 return 1; 223} 224EXPORT_SYMBOL(of_n_size_cells); 225 226int of_set_property(struct device_node *dp, const char *name, void *val, int len) 227{ 228 struct property **prevp; 229 void *new_val; 230 int err; 231 232 new_val = kmalloc(len, GFP_KERNEL); 233 if (!new_val) 234 return -ENOMEM; 235 236 memcpy(new_val, val, len); 237 238 err = -ENODEV; 239 240 write_lock(&devtree_lock); 241 prevp = &dp->properties; 242 while (*prevp) { 243 struct property *prop = *prevp; 244 245 if (!strcasecmp(prop->name, name)) { 246 void *old_val = prop->value; 247 int ret; 248 249 ret = prom_setprop(dp->node, (char *) name, val, len); 250 err = -EINVAL; 251 if (ret >= 0) { 252 prop->value = new_val; 253 prop->length = len; 254 255 if (OF_IS_DYNAMIC(prop)) 256 kfree(old_val); 257 258 OF_MARK_DYNAMIC(prop); 259 260 err = 0; 261 } 262 break; 263 } 264 prevp = &(*prevp)->next; 265 } 266 write_unlock(&devtree_lock); 267 268 269 return err; 270} 271EXPORT_SYMBOL(of_set_property); 272 273static unsigned int prom_early_allocated; 274 275static void * __init prom_early_alloc(unsigned long size) 276{ 277 void *ret; 278 279 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL); 280 if (ret != NULL) 281 memset(ret, 0, size); 282 283 prom_early_allocated += size; 284 285 return ret; 286} 287 288static int is_root_node(const struct device_node *dp) 289{ 290 if (!dp) 291 return 0; 292 293 return (dp->parent == NULL); 294} 295 296/* The following routines deal with the black magic of fully naming a 297 * node. 298 * 299 * Certain well known named nodes are just the simple name string. 300 * 301 * Actual devices have an address specifier appended to the base name 302 * string, like this "foo@addr". The "addr" can be in any number of 303 * formats, and the platform plus the type of the node determine the 304 * format and how it is constructed. 305 * 306 * For children of the ROOT node, the naming convention is fixed and 307 * determined by whether this is a sun4u or sun4v system. 308 * 309 * For children of other nodes, it is bus type specific. So 310 * we walk up the tree until we discover a "device_type" property 311 * we recognize and we go from there. 312 */ 313static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf) 314{ 315 struct linux_prom_registers *regs; 316 struct property *rprop; 317 318 rprop = of_find_property(dp, "reg", NULL); 319 if (!rprop) 320 return; 321 322 regs = rprop->value; 323 sprintf(tmp_buf, "%s@%x,%x", 324 dp->name, 325 regs->which_io, regs->phys_addr); 326} 327 328/* "name@slot,offset" */ 329static void __init sbus_path_component(struct device_node *dp, char *tmp_buf) 330{ 331 struct linux_prom_registers *regs; 332 struct property *prop; 333 334 prop = of_find_property(dp, "reg", NULL); 335 if (!prop) 336 return; 337 338 regs = prop->value; 339 sprintf(tmp_buf, "%s@%x,%x", 340 dp->name, 341 regs->which_io, 342 regs->phys_addr); 343} 344 345/* "name@devnum[,func]" */ 346static void __init pci_path_component(struct device_node *dp, char *tmp_buf) 347{ 348 struct linux_prom_pci_registers *regs; 349 struct property *prop; 350 unsigned int devfn; 351 352 prop = of_find_property(dp, "reg", NULL); 353 if (!prop) 354 return; 355 356 regs = prop->value; 357 devfn = (regs->phys_hi >> 8) & 0xff; 358 if (devfn & 0x07) { 359 sprintf(tmp_buf, "%s@%x,%x", 360 dp->name, 361 devfn >> 3, 362 devfn & 0x07); 363 } else { 364 sprintf(tmp_buf, "%s@%x", 365 dp->name, 366 devfn >> 3); 367 } 368} 369 370/* "name@addrhi,addrlo" */ 371static void __init ebus_path_component(struct device_node *dp, char *tmp_buf) 372{ 373 struct linux_prom_registers *regs; 374 struct property *prop; 375 376 prop = of_find_property(dp, "reg", NULL); 377 if (!prop) 378 return; 379 380 regs = prop->value; 381 382 sprintf(tmp_buf, "%s@%x,%x", 383 dp->name, 384 regs->which_io, regs->phys_addr); 385} 386 387static void __init __build_path_component(struct device_node *dp, char *tmp_buf) 388{ 389 struct device_node *parent = dp->parent; 390 391 if (parent != NULL) { 392 if (!strcmp(parent->type, "pci") || 393 !strcmp(parent->type, "pciex")) 394 return pci_path_component(dp, tmp_buf); 395 if (!strcmp(parent->type, "sbus")) 396 return sbus_path_component(dp, tmp_buf); 397 if (!strcmp(parent->type, "ebus")) 398 return ebus_path_component(dp, tmp_buf); 399 400 /* "isa" is handled with platform naming */ 401 } 402 403 /* Use platform naming convention. */ 404 return sparc32_path_component(dp, tmp_buf); 405} 406 407static char * __init build_path_component(struct device_node *dp) 408{ 409 char tmp_buf[64], *n; 410 411 tmp_buf[0] = '\0'; 412 __build_path_component(dp, tmp_buf); 413 if (tmp_buf[0] == '\0') 414 strcpy(tmp_buf, dp->name); 415 416 n = prom_early_alloc(strlen(tmp_buf) + 1); 417 strcpy(n, tmp_buf); 418 419 return n; 420} 421 422static char * __init build_full_name(struct device_node *dp) 423{ 424 int len, ourlen, plen; 425 char *n; 426 427 plen = strlen(dp->parent->full_name); 428 ourlen = strlen(dp->path_component_name); 429 len = ourlen + plen + 2; 430 431 n = prom_early_alloc(len); 432 strcpy(n, dp->parent->full_name); 433 if (!is_root_node(dp->parent)) { 434 strcpy(n + plen, "/"); 435 plen++; 436 } 437 strcpy(n + plen, dp->path_component_name); 438 439 return n; 440} 441 442static unsigned int unique_id; 443 444static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len) 445{ 446 static struct property *tmp = NULL; 447 struct property *p; 448 int len; 449 const char *name; 450 451 if (tmp) { 452 p = tmp; 453 memset(p, 0, sizeof(*p) + 32); 454 tmp = NULL; 455 } else { 456 p = prom_early_alloc(sizeof(struct property) + 32); 457 p->unique_id = unique_id++; 458 } 459 460 p->name = (char *) (p + 1); 461 if (special_name) { 462 strcpy(p->name, special_name); 463 p->length = special_len; 464 p->value = prom_early_alloc(special_len); 465 memcpy(p->value, special_val, special_len); 466 } else { 467 if (prev == NULL) { 468 name = prom_firstprop(node, NULL); 469 } else { 470 name = prom_nextprop(node, prev, NULL); 471 } 472 if (strlen(name) == 0) { 473 tmp = p; 474 return NULL; 475 } 476 strcpy(p->name, name); 477 p->length = prom_getproplen(node, p->name); 478 if (p->length <= 0) { 479 p->length = 0; 480 } else { 481 p->value = prom_early_alloc(p->length + 1); 482 len = prom_getproperty(node, p->name, p->value, 483 p->length); 484 if (len <= 0) 485 p->length = 0; 486 ((unsigned char *)p->value)[p->length] = '\0'; 487 } 488 } 489 return p; 490} 491 492static struct property * __init build_prop_list(phandle node) 493{ 494 struct property *head, *tail; 495 496 head = tail = build_one_prop(node, NULL, 497 ".node", &node, sizeof(node)); 498 499 tail->next = build_one_prop(node, NULL, NULL, NULL, 0); 500 tail = tail->next; 501 while(tail) { 502 tail->next = build_one_prop(node, tail->name, 503 NULL, NULL, 0); 504 tail = tail->next; 505 } 506 507 return head; 508} 509 510static char * __init get_one_property(phandle node, 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 len = prom_getproperty(node, name, buf, len); 519 } 520 521 return buf; 522} 523 524static 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 dp->unique_id = unique_id++; 533 534 kref_init(&dp->kref); 535 536 dp->name = get_one_property(node, "name"); 537 dp->type = get_one_property(node, "device_type"); 538 dp->node = node; 539 540 /* Build interrupts later... */ 541 542 dp->properties = build_prop_list(node); 543 544 return dp; 545} 546 547static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp) 548{ 549 struct device_node *dp; 550 551 dp = create_node(node); 552 if (dp) { 553 *(*nextp) = dp; 554 *nextp = &dp->allnext; 555 556 dp->parent = parent; 557 dp->path_component_name = build_path_component(dp); 558 dp->full_name = build_full_name(dp); 559 560 dp->child = build_tree(dp, prom_getchild(node), nextp); 561 562 dp->sibling = build_tree(parent, prom_getsibling(node), nextp); 563 } 564 565 return dp; 566} 567 568void __init prom_build_devicetree(void) 569{ 570 struct device_node **nextp; 571 572 allnodes = create_node(prom_root_node); 573 allnodes->path_component_name = ""; 574 allnodes->full_name = "/"; 575 576 nextp = &allnodes->allnext; 577 allnodes->child = build_tree(allnodes, 578 prom_getchild(allnodes->node), 579 &nextp); 580 printk("PROM: Built device tree with %u bytes of memory.\n", 581 prom_early_allocated); 582} 583