2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
10 * Adapted for sparc and sparc64 by David S. Miller davem@davemloft.net
12 * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version
18 * 2 of the License, or (at your option) any later version.
20 #include <linux/module.h>
22 #include <linux/spinlock.h>
24 struct device_node *allnodes;
26 /* use when traversing tree through the allnext, child, sibling,
27 * or parent members of struct device_node.
29 DEFINE_RWLOCK(devtree_lock);
31 int of_n_addr_cells(struct device_node *np)
38 ip = of_get_property(np, "#address-cells", NULL);
42 /* No #address-cells property for the root node */
43 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
45 EXPORT_SYMBOL(of_n_addr_cells);
47 int of_n_size_cells(struct device_node *np)
54 ip = of_get_property(np, "#size-cells", NULL);
58 /* No #size-cells property for the root node */
59 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
61 EXPORT_SYMBOL(of_n_size_cells);
63 #if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */
65 * of_node_get - Increment refcount of a node
66 * @node: Node to inc refcount, NULL is supported to
67 * simplify writing of callers
71 struct device_node *of_node_get(struct device_node *node)
74 kref_get(&node->kref);
77 EXPORT_SYMBOL(of_node_get);
79 static inline struct device_node *kref_to_device_node(struct kref *kref)
81 return container_of(kref, struct device_node, kref);
85 * of_node_release - release a dynamically allocated node
86 * @kref: kref element of the node to be released
88 * In of_node_put() this function is passed to kref_put()
91 static void of_node_release(struct kref *kref)
93 struct device_node *node = kref_to_device_node(kref);
94 struct property *prop = node->properties;
96 /* We should never be releasing nodes that haven't been detached. */
97 if (!of_node_check_flag(node, OF_DETACHED)) {
98 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
100 kref_init(&node->kref);
104 if (!of_node_check_flag(node, OF_DYNAMIC))
108 struct property *next = prop->next;
115 prop = node->deadprops;
116 node->deadprops = NULL;
119 kfree(node->full_name);
125 * of_node_put - Decrement refcount of a node
126 * @node: Node to dec refcount, NULL is supported to
127 * simplify writing of callers
130 void of_node_put(struct device_node *node)
133 kref_put(&node->kref, of_node_release);
135 EXPORT_SYMBOL(of_node_put);
136 #endif /* !CONFIG_SPARC */
138 struct property *of_find_property(const struct device_node *np,
147 read_lock(&devtree_lock);
148 for (pp = np->properties; pp != 0; pp = pp->next) {
149 if (of_prop_cmp(pp->name, name) == 0) {
155 read_unlock(&devtree_lock);
159 EXPORT_SYMBOL(of_find_property);
162 * of_find_all_nodes - Get next node in global list
163 * @prev: Previous node or NULL to start iteration
164 * of_node_put() will be called on it
166 * Returns a node pointer with refcount incremented, use
167 * of_node_put() on it when done.
169 struct device_node *of_find_all_nodes(struct device_node *prev)
171 struct device_node *np;
173 read_lock(&devtree_lock);
174 np = prev ? prev->allnext : allnodes;
175 for (; np != NULL; np = np->allnext)
179 read_unlock(&devtree_lock);
182 EXPORT_SYMBOL(of_find_all_nodes);
185 * Find a property with a given name for a given node
186 * and return the value.
188 const void *of_get_property(const struct device_node *np, const char *name,
191 struct property *pp = of_find_property(np, name, lenp);
193 return pp ? pp->value : NULL;
195 EXPORT_SYMBOL(of_get_property);
197 /** Checks if the given "compat" string matches one of the strings in
198 * the device's "compatible" property
200 int of_device_is_compatible(const struct device_node *device,
206 cp = of_get_property(device, "compatible", &cplen);
210 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
219 EXPORT_SYMBOL(of_device_is_compatible);
222 * machine_is_compatible - Test root of device tree for a given compatible value
223 * @compat: compatible string to look for in root node's compatible property.
225 * Returns true if the root node has the given value in its
226 * compatible property.
228 int machine_is_compatible(const char *compat)
230 struct device_node *root;
233 root = of_find_node_by_path("/");
235 rc = of_device_is_compatible(root, compat);
240 EXPORT_SYMBOL(machine_is_compatible);
243 * of_device_is_available - check if a device is available for use
245 * @device: Node to check for availability
247 * Returns 1 if the status property is absent or set to "okay" or "ok",
250 int of_device_is_available(const struct device_node *device)
255 status = of_get_property(device, "status", &statlen);
260 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
266 EXPORT_SYMBOL(of_device_is_available);
269 * of_get_parent - Get a node's parent if any
270 * @node: Node to get parent
272 * Returns a node pointer with refcount incremented, use
273 * of_node_put() on it when done.
275 struct device_node *of_get_parent(const struct device_node *node)
277 struct device_node *np;
282 read_lock(&devtree_lock);
283 np = of_node_get(node->parent);
284 read_unlock(&devtree_lock);
287 EXPORT_SYMBOL(of_get_parent);
290 * of_get_next_parent - Iterate to a node's parent
291 * @node: Node to get parent of
293 * This is like of_get_parent() except that it drops the
294 * refcount on the passed node, making it suitable for iterating
295 * through a node's parents.
297 * Returns a node pointer with refcount incremented, use
298 * of_node_put() on it when done.
300 struct device_node *of_get_next_parent(struct device_node *node)
302 struct device_node *parent;
307 read_lock(&devtree_lock);
308 parent = of_node_get(node->parent);
310 read_unlock(&devtree_lock);
315 * of_get_next_child - Iterate a node childs
317 * @prev: previous child of the parent node, or NULL to get first
319 * Returns a node pointer with refcount incremented, use
320 * of_node_put() on it when done.
322 struct device_node *of_get_next_child(const struct device_node *node,
323 struct device_node *prev)
325 struct device_node *next;
327 read_lock(&devtree_lock);
328 next = prev ? prev->sibling : node->child;
329 for (; next; next = next->sibling)
330 if (of_node_get(next))
333 read_unlock(&devtree_lock);
336 EXPORT_SYMBOL(of_get_next_child);
339 * of_find_node_by_path - Find a node matching a full OF path
340 * @path: The full path to match
342 * Returns a node pointer with refcount incremented, use
343 * of_node_put() on it when done.
345 struct device_node *of_find_node_by_path(const char *path)
347 struct device_node *np = allnodes;
349 read_lock(&devtree_lock);
350 for (; np; np = np->allnext) {
351 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
355 read_unlock(&devtree_lock);
358 EXPORT_SYMBOL(of_find_node_by_path);
361 * of_find_node_by_name - Find a node by its "name" property
362 * @from: The node to start searching from or NULL, the node
363 * you pass will not be searched, only the next one
364 * will; typically, you pass what the previous call
365 * returned. of_node_put() will be called on it
366 * @name: The name string to match against
368 * Returns a node pointer with refcount incremented, use
369 * of_node_put() on it when done.
371 struct device_node *of_find_node_by_name(struct device_node *from,
374 struct device_node *np;
376 read_lock(&devtree_lock);
377 np = from ? from->allnext : allnodes;
378 for (; np; np = np->allnext)
379 if (np->name && (of_node_cmp(np->name, name) == 0)
383 read_unlock(&devtree_lock);
386 EXPORT_SYMBOL(of_find_node_by_name);
389 * of_find_node_by_type - Find a node by its "device_type" property
390 * @from: The node to start searching from, or NULL to start searching
391 * the entire device tree. The node you pass will not be
392 * searched, only the next one will; typically, you pass
393 * what the previous call returned. of_node_put() will be
394 * called on from for you.
395 * @type: The type string to match against
397 * Returns a node pointer with refcount incremented, use
398 * of_node_put() on it when done.
400 struct device_node *of_find_node_by_type(struct device_node *from,
403 struct device_node *np;
405 read_lock(&devtree_lock);
406 np = from ? from->allnext : allnodes;
407 for (; np; np = np->allnext)
408 if (np->type && (of_node_cmp(np->type, type) == 0)
412 read_unlock(&devtree_lock);
415 EXPORT_SYMBOL(of_find_node_by_type);
418 * of_find_compatible_node - Find a node based on type and one of the
419 * tokens in its "compatible" property
420 * @from: The node to start searching from or NULL, the node
421 * you pass will not be searched, only the next one
422 * will; typically, you pass what the previous call
423 * returned. of_node_put() will be called on it
424 * @type: The type string to match "device_type" or NULL to ignore
425 * @compatible: The string to match to one of the tokens in the device
428 * Returns a node pointer with refcount incremented, use
429 * of_node_put() on it when done.
431 struct device_node *of_find_compatible_node(struct device_node *from,
432 const char *type, const char *compatible)
434 struct device_node *np;
436 read_lock(&devtree_lock);
437 np = from ? from->allnext : allnodes;
438 for (; np; np = np->allnext) {
440 && !(np->type && (of_node_cmp(np->type, type) == 0)))
442 if (of_device_is_compatible(np, compatible) && of_node_get(np))
446 read_unlock(&devtree_lock);
449 EXPORT_SYMBOL(of_find_compatible_node);
452 * of_find_node_with_property - Find a node which has a property with
454 * @from: The node to start searching from or NULL, the node
455 * you pass will not be searched, only the next one
456 * will; typically, you pass what the previous call
457 * returned. of_node_put() will be called on it
458 * @prop_name: The name of the property to look for.
460 * Returns a node pointer with refcount incremented, use
461 * of_node_put() on it when done.
463 struct device_node *of_find_node_with_property(struct device_node *from,
464 const char *prop_name)
466 struct device_node *np;
469 read_lock(&devtree_lock);
470 np = from ? from->allnext : allnodes;
471 for (; np; np = np->allnext) {
472 for (pp = np->properties; pp != 0; pp = pp->next) {
473 if (of_prop_cmp(pp->name, prop_name) == 0) {
481 read_unlock(&devtree_lock);
484 EXPORT_SYMBOL(of_find_node_with_property);
487 * of_match_node - Tell if an device_node has a matching of_match structure
488 * @matches: array of of device match structures to search in
489 * @node: the of device structure to match against
491 * Low level utility function used by device matching.
493 const struct of_device_id *of_match_node(const struct of_device_id *matches,
494 const struct device_node *node)
496 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
498 if (matches->name[0])
500 && !strcmp(matches->name, node->name);
501 if (matches->type[0])
503 && !strcmp(matches->type, node->type);
504 if (matches->compatible[0])
505 match &= of_device_is_compatible(node,
506 matches->compatible);
513 EXPORT_SYMBOL(of_match_node);
516 * of_find_matching_node - Find a node based on an of_device_id match
518 * @from: The node to start searching from or NULL, the node
519 * you pass will not be searched, only the next one
520 * will; typically, you pass what the previous call
521 * returned. of_node_put() will be called on it
522 * @matches: array of of device match structures to search in
524 * Returns a node pointer with refcount incremented, use
525 * of_node_put() on it when done.
527 struct device_node *of_find_matching_node(struct device_node *from,
528 const struct of_device_id *matches)
530 struct device_node *np;
532 read_lock(&devtree_lock);
533 np = from ? from->allnext : allnodes;
534 for (; np; np = np->allnext) {
535 if (of_match_node(matches, np) && of_node_get(np))
539 read_unlock(&devtree_lock);
542 EXPORT_SYMBOL(of_find_matching_node);
545 * of_modalias_table: Table of explicit compatible ==> modalias mappings
547 * This table allows particulare compatible property values to be mapped
548 * to modalias strings. This is useful for busses which do not directly
549 * understand the OF device tree but are populated based on data contained
550 * within the device tree. SPI and I2C are the two current users of this
553 * In most cases, devices do not need to be listed in this table because
554 * the modalias value can be derived directly from the compatible table.
555 * However, if for any reason a value cannot be derived, then this table
556 * provides a method to override the implicit derivation.
558 * At the moment, a single table is used for all bus types because it is
559 * assumed that the data size is small and that the compatible values
560 * should already be distinct enough to differentiate between SPI, I2C
563 struct of_modalias_table {
567 static struct of_modalias_table of_modalias_table[] = {
568 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
569 { "mmc-spi-slot", "mmc_spi" },
573 * of_modalias_node - Lookup appropriate modalias for a device node
574 * @node: pointer to a device tree node
575 * @modalias: Pointer to buffer that modalias value will be copied into
576 * @len: Length of modalias value
578 * Based on the value of the compatible property, this routine will determine
579 * an appropriate modalias value for a particular device tree node. Two
580 * separate methods are attempted to derive a modalias value.
582 * First method is to lookup the compatible value in of_modalias_table.
583 * Second is to strip off the manufacturer prefix from the first
584 * compatible entry and use the remainder as modalias
586 * This routine returns 0 on success
588 int of_modalias_node(struct device_node *node, char *modalias, int len)
591 const char *compatible;
594 /* 1. search for exception list entry */
595 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
596 compatible = of_modalias_table[i].of_device;
597 if (!of_device_is_compatible(node, compatible))
599 strlcpy(modalias, of_modalias_table[i].modalias, len);
603 compatible = of_get_property(node, "compatible", &cplen);
607 /* 2. take first compatible entry and strip manufacturer */
608 p = strchr(compatible, ',');
612 strlcpy(modalias, p, len);
615 EXPORT_SYMBOL_GPL(of_modalias_node);
618 * of_find_node_by_phandle - Find a node given a phandle
619 * @handle: phandle of the node to find
621 * Returns a node pointer with refcount incremented, use
622 * of_node_put() on it when done.
624 struct device_node *of_find_node_by_phandle(phandle handle)
626 struct device_node *np;
628 read_lock(&devtree_lock);
629 for (np = allnodes; np; np = np->allnext)
630 if (np->phandle == handle)
633 read_unlock(&devtree_lock);
636 EXPORT_SYMBOL(of_find_node_by_phandle);
639 * of_parse_phandle - Resolve a phandle property to a device_node pointer
640 * @np: Pointer to device node holding phandle property
641 * @phandle_name: Name of property holding a phandle value
642 * @index: For properties holding a table of phandles, this is the index into
645 * Returns the device_node pointer with refcount incremented. Use
646 * of_node_put() on it when done.
649 of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
651 const phandle *phandle;
654 phandle = of_get_property(np, phandle_name, &size);
655 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
658 return of_find_node_by_phandle(phandle[index]);
660 EXPORT_SYMBOL(of_parse_phandle);
663 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
664 * @np: pointer to a device tree node containing a list
665 * @list_name: property name that contains a list
666 * @cells_name: property name that specifies phandles' arguments count
667 * @index: index of a phandle to parse out
668 * @out_node: optional pointer to device_node struct pointer (will be filled)
669 * @out_args: optional pointer to arguments pointer (will be filled)
671 * This function is useful to parse lists of phandles and their arguments.
672 * Returns 0 on success and fills out_node and out_args, on error returns
673 * appropriate errno value.
686 * list = <&phandle1 1 2 &phandle2 3>;
689 * To get a device_node of the `node2' node you may call this:
690 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
692 int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
693 const char *cells_name, int index,
694 struct device_node **out_node,
695 const void **out_args)
702 struct device_node *node = NULL;
703 const void *args = NULL;
705 list = of_get_property(np, list_name, &size);
710 list_end = list + size / sizeof(*list);
712 while (list < list_end) {
714 const phandle *phandle;
719 /* one cell hole in the list = <>; */
723 node = of_find_node_by_phandle(*phandle);
725 pr_debug("%s: could not find phandle\n",
730 cells = of_get_property(node, cells_name, &size);
731 if (!cells || size != sizeof(*cells)) {
732 pr_debug("%s: could not get %s for %s\n",
733 np->full_name, cells_name, node->full_name);
738 if (list > list_end) {
739 pr_debug("%s: insufficient arguments length\n",
744 if (cur_index == index)
755 * args w/o node indicates that the loop above has stopped at
756 * the 'hole' cell. Report this differently.
774 pr_debug("%s failed with status %d\n", __func__, ret);
777 EXPORT_SYMBOL(of_parse_phandles_with_args);
780 * prom_add_property - Add a property to a node
782 int prom_add_property(struct device_node *np, struct property *prop)
784 struct property **next;
788 write_lock_irqsave(&devtree_lock, flags);
789 next = &np->properties;
791 if (strcmp(prop->name, (*next)->name) == 0) {
792 /* duplicate ! don't insert it */
793 write_unlock_irqrestore(&devtree_lock, flags);
796 next = &(*next)->next;
799 write_unlock_irqrestore(&devtree_lock, flags);
801 #ifdef CONFIG_PROC_DEVICETREE
802 /* try to add to proc as well if it was initialized */
804 proc_device_tree_add_prop(np->pde, prop);
805 #endif /* CONFIG_PROC_DEVICETREE */
811 * prom_remove_property - Remove a property from a node.
813 * Note that we don't actually remove it, since we have given out
814 * who-knows-how-many pointers to the data using get-property.
815 * Instead we just move the property to the "dead properties"
816 * list, so it won't be found any more.
818 int prom_remove_property(struct device_node *np, struct property *prop)
820 struct property **next;
824 write_lock_irqsave(&devtree_lock, flags);
825 next = &np->properties;
830 prop->next = np->deadprops;
831 np->deadprops = prop;
835 next = &(*next)->next;
837 write_unlock_irqrestore(&devtree_lock, flags);
842 #ifdef CONFIG_PROC_DEVICETREE
843 /* try to remove the proc node as well */
845 proc_device_tree_remove_prop(np->pde, prop);
846 #endif /* CONFIG_PROC_DEVICETREE */
852 * prom_update_property - Update a property in a node.
854 * Note that we don't actually remove it, since we have given out
855 * who-knows-how-many pointers to the data using get-property.
856 * Instead we just move the property to the "dead properties" list,
857 * and add the new property to the property list
859 int prom_update_property(struct device_node *np,
860 struct property *newprop,
861 struct property *oldprop)
863 struct property **next;
867 write_lock_irqsave(&devtree_lock, flags);
868 next = &np->properties;
870 if (*next == oldprop) {
872 newprop->next = oldprop->next;
874 oldprop->next = np->deadprops;
875 np->deadprops = oldprop;
879 next = &(*next)->next;
881 write_unlock_irqrestore(&devtree_lock, flags);
886 #ifdef CONFIG_PROC_DEVICETREE
887 /* try to add to proc as well if it was initialized */
889 proc_device_tree_update_prop(np->pde, newprop, oldprop);
890 #endif /* CONFIG_PROC_DEVICETREE */
895 #if defined(CONFIG_OF_DYNAMIC)
897 * Support for dynamic device trees.
899 * On some platforms, the device tree can be manipulated at runtime.
900 * The routines in this section support adding, removing and changing
905 * of_attach_node - Plug a device node into the tree and global list.
907 void of_attach_node(struct device_node *np)
911 write_lock_irqsave(&devtree_lock, flags);
912 np->sibling = np->parent->child;
913 np->allnext = allnodes;
914 np->parent->child = np;
916 write_unlock_irqrestore(&devtree_lock, flags);
920 * of_detach_node - "Unplug" a node from the device tree.
922 * The caller must hold a reference to the node. The memory associated with
923 * the node is not freed until its refcount goes to zero.
925 void of_detach_node(struct device_node *np)
927 struct device_node *parent;
930 write_lock_irqsave(&devtree_lock, flags);
937 allnodes = np->allnext;
939 struct device_node *prev;
940 for (prev = allnodes;
942 prev = prev->allnext)
944 prev->allnext = np->allnext;
947 if (parent->child == np)
948 parent->child = np->sibling;
950 struct device_node *prevsib;
951 for (prevsib = np->parent->child;
952 prevsib->sibling != np;
953 prevsib = prevsib->sibling)
955 prevsib->sibling = np->sibling;
958 of_node_set_flag(np, OF_DETACHED);
961 write_unlock_irqrestore(&devtree_lock, flags);
963 #endif /* defined(CONFIG_OF_DYNAMIC) */