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>
23 #include <linux/proc_fs.h>
25 struct device_node *allnodes;
27 /* use when traversing tree through the allnext, child, sibling,
28 * or parent members of struct device_node.
30 DEFINE_RWLOCK(devtree_lock);
32 int of_n_addr_cells(struct device_node *np)
39 ip = of_get_property(np, "#address-cells", NULL);
41 return be32_to_cpup(ip);
43 /* No #address-cells property for the root node */
44 return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
46 EXPORT_SYMBOL(of_n_addr_cells);
48 int of_n_size_cells(struct device_node *np)
55 ip = of_get_property(np, "#size-cells", NULL);
57 return be32_to_cpup(ip);
59 /* No #size-cells property for the root node */
60 return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
62 EXPORT_SYMBOL(of_n_size_cells);
64 #if !defined(CONFIG_SPARC) /* SPARC doesn't do ref counting (yet) */
66 * of_node_get - Increment refcount of a node
67 * @node: Node to inc refcount, NULL is supported to
68 * simplify writing of callers
72 struct device_node *of_node_get(struct device_node *node)
75 kref_get(&node->kref);
78 EXPORT_SYMBOL(of_node_get);
80 static inline struct device_node *kref_to_device_node(struct kref *kref)
82 return container_of(kref, struct device_node, kref);
86 * of_node_release - release a dynamically allocated node
87 * @kref: kref element of the node to be released
89 * In of_node_put() this function is passed to kref_put()
92 static void of_node_release(struct kref *kref)
94 struct device_node *node = kref_to_device_node(kref);
95 struct property *prop = node->properties;
97 /* We should never be releasing nodes that haven't been detached. */
98 if (!of_node_check_flag(node, OF_DETACHED)) {
99 pr_err("ERROR: Bad of_node_put() on %s\n", node->full_name);
101 kref_init(&node->kref);
105 if (!of_node_check_flag(node, OF_DYNAMIC))
109 struct property *next = prop->next;
116 prop = node->deadprops;
117 node->deadprops = NULL;
120 kfree(node->full_name);
126 * of_node_put - Decrement refcount of a node
127 * @node: Node to dec refcount, NULL is supported to
128 * simplify writing of callers
131 void of_node_put(struct device_node *node)
134 kref_put(&node->kref, of_node_release);
136 EXPORT_SYMBOL(of_node_put);
137 #endif /* !CONFIG_SPARC */
139 struct property *of_find_property(const struct device_node *np,
148 read_lock(&devtree_lock);
149 for (pp = np->properties; pp != 0; pp = pp->next) {
150 if (of_prop_cmp(pp->name, name) == 0) {
156 read_unlock(&devtree_lock);
160 EXPORT_SYMBOL(of_find_property);
163 * of_find_all_nodes - Get next node in global list
164 * @prev: Previous node or NULL to start iteration
165 * of_node_put() will be called on it
167 * Returns a node pointer with refcount incremented, use
168 * of_node_put() on it when done.
170 struct device_node *of_find_all_nodes(struct device_node *prev)
172 struct device_node *np;
174 read_lock(&devtree_lock);
175 np = prev ? prev->allnext : allnodes;
176 for (; np != NULL; np = np->allnext)
180 read_unlock(&devtree_lock);
183 EXPORT_SYMBOL(of_find_all_nodes);
186 * Find a property with a given name for a given node
187 * and return the value.
189 const void *of_get_property(const struct device_node *np, const char *name,
192 struct property *pp = of_find_property(np, name, lenp);
194 return pp ? pp->value : NULL;
196 EXPORT_SYMBOL(of_get_property);
198 /** Checks if the given "compat" string matches one of the strings in
199 * the device's "compatible" property
201 int of_device_is_compatible(const struct device_node *device,
207 cp = of_get_property(device, "compatible", &cplen);
211 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
220 EXPORT_SYMBOL(of_device_is_compatible);
223 * of_machine_is_compatible - Test root of device tree for a given compatible value
224 * @compat: compatible string to look for in root node's compatible property.
226 * Returns true if the root node has the given value in its
227 * compatible property.
229 int of_machine_is_compatible(const char *compat)
231 struct device_node *root;
234 root = of_find_node_by_path("/");
236 rc = of_device_is_compatible(root, compat);
241 EXPORT_SYMBOL(of_machine_is_compatible);
244 * of_device_is_available - check if a device is available for use
246 * @device: Node to check for availability
248 * Returns 1 if the status property is absent or set to "okay" or "ok",
251 int of_device_is_available(const struct device_node *device)
256 status = of_get_property(device, "status", &statlen);
261 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
267 EXPORT_SYMBOL(of_device_is_available);
270 * of_get_parent - Get a node's parent if any
271 * @node: Node to get parent
273 * Returns a node pointer with refcount incremented, use
274 * of_node_put() on it when done.
276 struct device_node *of_get_parent(const struct device_node *node)
278 struct device_node *np;
283 read_lock(&devtree_lock);
284 np = of_node_get(node->parent);
285 read_unlock(&devtree_lock);
288 EXPORT_SYMBOL(of_get_parent);
291 * of_get_next_parent - Iterate to a node's parent
292 * @node: Node to get parent of
294 * This is like of_get_parent() except that it drops the
295 * refcount on the passed node, making it suitable for iterating
296 * through a node's parents.
298 * Returns a node pointer with refcount incremented, use
299 * of_node_put() on it when done.
301 struct device_node *of_get_next_parent(struct device_node *node)
303 struct device_node *parent;
308 read_lock(&devtree_lock);
309 parent = of_node_get(node->parent);
311 read_unlock(&devtree_lock);
316 * of_get_next_child - Iterate a node childs
318 * @prev: previous child of the parent node, or NULL to get first
320 * Returns a node pointer with refcount incremented, use
321 * of_node_put() on it when done.
323 struct device_node *of_get_next_child(const struct device_node *node,
324 struct device_node *prev)
326 struct device_node *next;
328 read_lock(&devtree_lock);
329 next = prev ? prev->sibling : node->child;
330 for (; next; next = next->sibling)
331 if (of_node_get(next))
334 read_unlock(&devtree_lock);
337 EXPORT_SYMBOL(of_get_next_child);
340 * of_find_node_by_path - Find a node matching a full OF path
341 * @path: The full path to match
343 * Returns a node pointer with refcount incremented, use
344 * of_node_put() on it when done.
346 struct device_node *of_find_node_by_path(const char *path)
348 struct device_node *np = allnodes;
350 read_lock(&devtree_lock);
351 for (; np; np = np->allnext) {
352 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
356 read_unlock(&devtree_lock);
359 EXPORT_SYMBOL(of_find_node_by_path);
362 * of_find_node_by_name - Find a node by its "name" property
363 * @from: The node to start searching from or NULL, the node
364 * you pass will not be searched, only the next one
365 * will; typically, you pass what the previous call
366 * returned. of_node_put() will be called on it
367 * @name: The name string to match against
369 * Returns a node pointer with refcount incremented, use
370 * of_node_put() on it when done.
372 struct device_node *of_find_node_by_name(struct device_node *from,
375 struct device_node *np;
377 read_lock(&devtree_lock);
378 np = from ? from->allnext : allnodes;
379 for (; np; np = np->allnext)
380 if (np->name && (of_node_cmp(np->name, name) == 0)
384 read_unlock(&devtree_lock);
387 EXPORT_SYMBOL(of_find_node_by_name);
390 * of_find_node_by_type - Find a node by its "device_type" property
391 * @from: The node to start searching from, or NULL to start searching
392 * the entire device tree. The node you pass will not be
393 * searched, only the next one will; typically, you pass
394 * what the previous call returned. of_node_put() will be
395 * called on from for you.
396 * @type: The type string to match against
398 * Returns a node pointer with refcount incremented, use
399 * of_node_put() on it when done.
401 struct device_node *of_find_node_by_type(struct device_node *from,
404 struct device_node *np;
406 read_lock(&devtree_lock);
407 np = from ? from->allnext : allnodes;
408 for (; np; np = np->allnext)
409 if (np->type && (of_node_cmp(np->type, type) == 0)
413 read_unlock(&devtree_lock);
416 EXPORT_SYMBOL(of_find_node_by_type);
419 * of_find_compatible_node - Find a node based on type and one of the
420 * tokens in its "compatible" property
421 * @from: The node to start searching from or NULL, the node
422 * you pass will not be searched, only the next one
423 * will; typically, you pass what the previous call
424 * returned. of_node_put() will be called on it
425 * @type: The type string to match "device_type" or NULL to ignore
426 * @compatible: The string to match to one of the tokens in the device
429 * Returns a node pointer with refcount incremented, use
430 * of_node_put() on it when done.
432 struct device_node *of_find_compatible_node(struct device_node *from,
433 const char *type, const char *compatible)
435 struct device_node *np;
437 read_lock(&devtree_lock);
438 np = from ? from->allnext : allnodes;
439 for (; np; np = np->allnext) {
441 && !(np->type && (of_node_cmp(np->type, type) == 0)))
443 if (of_device_is_compatible(np, compatible) && of_node_get(np))
447 read_unlock(&devtree_lock);
450 EXPORT_SYMBOL(of_find_compatible_node);
453 * of_find_node_with_property - Find a node which has a property with
455 * @from: The node to start searching from or NULL, the node
456 * you pass will not be searched, only the next one
457 * will; typically, you pass what the previous call
458 * returned. of_node_put() will be called on it
459 * @prop_name: The name of the property to look for.
461 * Returns a node pointer with refcount incremented, use
462 * of_node_put() on it when done.
464 struct device_node *of_find_node_with_property(struct device_node *from,
465 const char *prop_name)
467 struct device_node *np;
470 read_lock(&devtree_lock);
471 np = from ? from->allnext : allnodes;
472 for (; np; np = np->allnext) {
473 for (pp = np->properties; pp != 0; pp = pp->next) {
474 if (of_prop_cmp(pp->name, prop_name) == 0) {
482 read_unlock(&devtree_lock);
485 EXPORT_SYMBOL(of_find_node_with_property);
488 * of_match_node - Tell if an device_node has a matching of_match structure
489 * @matches: array of of device match structures to search in
490 * @node: the of device structure to match against
492 * Low level utility function used by device matching.
494 const struct of_device_id *of_match_node(const struct of_device_id *matches,
495 const struct device_node *node)
497 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
499 if (matches->name[0])
501 && !strcmp(matches->name, node->name);
502 if (matches->type[0])
504 && !strcmp(matches->type, node->type);
505 if (matches->compatible[0])
506 match &= of_device_is_compatible(node,
507 matches->compatible);
514 EXPORT_SYMBOL(of_match_node);
517 * of_find_matching_node - Find a node based on an of_device_id match
519 * @from: The node to start searching from or NULL, the node
520 * you pass will not be searched, only the next one
521 * will; typically, you pass what the previous call
522 * returned. of_node_put() will be called on it
523 * @matches: array of of device match structures to search in
525 * Returns a node pointer with refcount incremented, use
526 * of_node_put() on it when done.
528 struct device_node *of_find_matching_node(struct device_node *from,
529 const struct of_device_id *matches)
531 struct device_node *np;
533 read_lock(&devtree_lock);
534 np = from ? from->allnext : allnodes;
535 for (; np; np = np->allnext) {
536 if (of_match_node(matches, np) && of_node_get(np))
540 read_unlock(&devtree_lock);
543 EXPORT_SYMBOL(of_find_matching_node);
546 * of_modalias_table: Table of explicit compatible ==> modalias mappings
548 * This table allows particulare compatible property values to be mapped
549 * to modalias strings. This is useful for busses which do not directly
550 * understand the OF device tree but are populated based on data contained
551 * within the device tree. SPI and I2C are the two current users of this
554 * In most cases, devices do not need to be listed in this table because
555 * the modalias value can be derived directly from the compatible table.
556 * However, if for any reason a value cannot be derived, then this table
557 * provides a method to override the implicit derivation.
559 * At the moment, a single table is used for all bus types because it is
560 * assumed that the data size is small and that the compatible values
561 * should already be distinct enough to differentiate between SPI, I2C
564 struct of_modalias_table {
568 static struct of_modalias_table of_modalias_table[] = {
569 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
570 { "mmc-spi-slot", "mmc_spi" },
574 * of_modalias_node - Lookup appropriate modalias for a device node
575 * @node: pointer to a device tree node
576 * @modalias: Pointer to buffer that modalias value will be copied into
577 * @len: Length of modalias value
579 * Based on the value of the compatible property, this routine will determine
580 * an appropriate modalias value for a particular device tree node. Two
581 * separate methods are attempted to derive a modalias value.
583 * First method is to lookup the compatible value in of_modalias_table.
584 * Second is to strip off the manufacturer prefix from the first
585 * compatible entry and use the remainder as modalias
587 * This routine returns 0 on success
589 int of_modalias_node(struct device_node *node, char *modalias, int len)
592 const char *compatible;
595 /* 1. search for exception list entry */
596 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
597 compatible = of_modalias_table[i].of_device;
598 if (!of_device_is_compatible(node, compatible))
600 strlcpy(modalias, of_modalias_table[i].modalias, len);
604 compatible = of_get_property(node, "compatible", &cplen);
608 /* 2. take first compatible entry and strip manufacturer */
609 p = strchr(compatible, ',');
613 strlcpy(modalias, p, len);
616 EXPORT_SYMBOL_GPL(of_modalias_node);
619 * of_find_node_by_phandle - Find a node given a phandle
620 * @handle: phandle of the node to find
622 * Returns a node pointer with refcount incremented, use
623 * of_node_put() on it when done.
625 struct device_node *of_find_node_by_phandle(phandle handle)
627 struct device_node *np;
629 read_lock(&devtree_lock);
630 for (np = allnodes; np; np = np->allnext)
631 if (np->phandle == handle)
634 read_unlock(&devtree_lock);
637 EXPORT_SYMBOL(of_find_node_by_phandle);
640 * of_parse_phandle - Resolve a phandle property to a device_node pointer
641 * @np: Pointer to device node holding phandle property
642 * @phandle_name: Name of property holding a phandle value
643 * @index: For properties holding a table of phandles, this is the index into
646 * Returns the device_node pointer with refcount incremented. Use
647 * of_node_put() on it when done.
650 of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
652 const phandle *phandle;
655 phandle = of_get_property(np, phandle_name, &size);
656 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
659 return of_find_node_by_phandle(phandle[index]);
661 EXPORT_SYMBOL(of_parse_phandle);
664 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
665 * @np: pointer to a device tree node containing a list
666 * @list_name: property name that contains a list
667 * @cells_name: property name that specifies phandles' arguments count
668 * @index: index of a phandle to parse out
669 * @out_node: optional pointer to device_node struct pointer (will be filled)
670 * @out_args: optional pointer to arguments pointer (will be filled)
672 * This function is useful to parse lists of phandles and their arguments.
673 * Returns 0 on success and fills out_node and out_args, on error returns
674 * appropriate errno value.
687 * list = <&phandle1 1 2 &phandle2 3>;
690 * To get a device_node of the `node2' node you may call this:
691 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
693 int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
694 const char *cells_name, int index,
695 struct device_node **out_node,
696 const void **out_args)
700 const __be32 *list_end;
703 struct device_node *node = NULL;
704 const void *args = NULL;
706 list = of_get_property(np, list_name, &size);
711 list_end = list + size / sizeof(*list);
713 while (list < list_end) {
715 const phandle *phandle;
720 /* one cell hole in the list = <>; */
724 node = of_find_node_by_phandle(*phandle);
726 pr_debug("%s: could not find phandle\n",
731 cells = of_get_property(node, cells_name, &size);
732 if (!cells || size != sizeof(*cells)) {
733 pr_debug("%s: could not get %s for %s\n",
734 np->full_name, cells_name, node->full_name);
738 list += be32_to_cpup(cells);
739 if (list > list_end) {
740 pr_debug("%s: insufficient arguments length\n",
745 if (cur_index == index)
756 * args w/o node indicates that the loop above has stopped at
757 * the 'hole' cell. Report this differently.
775 pr_debug("%s failed with status %d\n", __func__, ret);
778 EXPORT_SYMBOL(of_parse_phandles_with_args);
781 * prom_add_property - Add a property to a node
783 int prom_add_property(struct device_node *np, struct property *prop)
785 struct property **next;
789 write_lock_irqsave(&devtree_lock, flags);
790 next = &np->properties;
792 if (strcmp(prop->name, (*next)->name) == 0) {
793 /* duplicate ! don't insert it */
794 write_unlock_irqrestore(&devtree_lock, flags);
797 next = &(*next)->next;
800 write_unlock_irqrestore(&devtree_lock, flags);
802 #ifdef CONFIG_PROC_DEVICETREE
803 /* try to add to proc as well if it was initialized */
805 proc_device_tree_add_prop(np->pde, prop);
806 #endif /* CONFIG_PROC_DEVICETREE */
812 * prom_remove_property - Remove a property from a node.
814 * Note that we don't actually remove it, since we have given out
815 * who-knows-how-many pointers to the data using get-property.
816 * Instead we just move the property to the "dead properties"
817 * list, so it won't be found any more.
819 int prom_remove_property(struct device_node *np, struct property *prop)
821 struct property **next;
825 write_lock_irqsave(&devtree_lock, flags);
826 next = &np->properties;
831 prop->next = np->deadprops;
832 np->deadprops = prop;
836 next = &(*next)->next;
838 write_unlock_irqrestore(&devtree_lock, flags);
843 #ifdef CONFIG_PROC_DEVICETREE
844 /* try to remove the proc node as well */
846 proc_device_tree_remove_prop(np->pde, prop);
847 #endif /* CONFIG_PROC_DEVICETREE */
853 * prom_update_property - Update a property in a node.
855 * Note that we don't actually remove it, since we have given out
856 * who-knows-how-many pointers to the data using get-property.
857 * Instead we just move the property to the "dead properties" list,
858 * and add the new property to the property list
860 int prom_update_property(struct device_node *np,
861 struct property *newprop,
862 struct property *oldprop)
864 struct property **next;
868 write_lock_irqsave(&devtree_lock, flags);
869 next = &np->properties;
871 if (*next == oldprop) {
873 newprop->next = oldprop->next;
875 oldprop->next = np->deadprops;
876 np->deadprops = oldprop;
880 next = &(*next)->next;
882 write_unlock_irqrestore(&devtree_lock, flags);
887 #ifdef CONFIG_PROC_DEVICETREE
888 /* try to add to proc as well if it was initialized */
890 proc_device_tree_update_prop(np->pde, newprop, oldprop);
891 #endif /* CONFIG_PROC_DEVICETREE */
896 #if defined(CONFIG_OF_DYNAMIC)
898 * Support for dynamic device trees.
900 * On some platforms, the device tree can be manipulated at runtime.
901 * The routines in this section support adding, removing and changing
906 * of_attach_node - Plug a device node into the tree and global list.
908 void of_attach_node(struct device_node *np)
912 write_lock_irqsave(&devtree_lock, flags);
913 np->sibling = np->parent->child;
914 np->allnext = allnodes;
915 np->parent->child = np;
917 write_unlock_irqrestore(&devtree_lock, flags);
921 * of_detach_node - "Unplug" a node from the device tree.
923 * The caller must hold a reference to the node. The memory associated with
924 * the node is not freed until its refcount goes to zero.
926 void of_detach_node(struct device_node *np)
928 struct device_node *parent;
931 write_lock_irqsave(&devtree_lock, flags);
938 allnodes = np->allnext;
940 struct device_node *prev;
941 for (prev = allnodes;
943 prev = prev->allnext)
945 prev->allnext = np->allnext;
948 if (parent->child == np)
949 parent->child = np->sibling;
951 struct device_node *prevsib;
952 for (prevsib = np->parent->child;
953 prevsib->sibling != np;
954 prevsib = prevsib->sibling)
956 prevsib->sibling = np->sibling;
959 of_node_set_flag(np, OF_DETACHED);
962 write_unlock_irqrestore(&devtree_lock, flags);
964 #endif /* defined(CONFIG_OF_DYNAMIC) */