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 struct property *of_find_property(const struct device_node *np,
72 read_lock(&devtree_lock);
73 for (pp = np->properties; pp != 0; pp = pp->next) {
74 if (of_prop_cmp(pp->name, name) == 0) {
80 read_unlock(&devtree_lock);
84 EXPORT_SYMBOL(of_find_property);
87 * of_find_all_nodes - Get next node in global list
88 * @prev: Previous node or NULL to start iteration
89 * of_node_put() will be called on it
91 * Returns a node pointer with refcount incremented, use
92 * of_node_put() on it when done.
94 struct device_node *of_find_all_nodes(struct device_node *prev)
96 struct device_node *np;
98 read_lock(&devtree_lock);
99 np = prev ? prev->allnext : allnodes;
100 for (; np != NULL; np = np->allnext)
104 read_unlock(&devtree_lock);
107 EXPORT_SYMBOL(of_find_all_nodes);
110 * Find a property with a given name for a given node
111 * and return the value.
113 const void *of_get_property(const struct device_node *np, const char *name,
116 struct property *pp = of_find_property(np, name, lenp);
118 return pp ? pp->value : NULL;
120 EXPORT_SYMBOL(of_get_property);
122 /** Checks if the given "compat" string matches one of the strings in
123 * the device's "compatible" property
125 int of_device_is_compatible(const struct device_node *device,
131 cp = of_get_property(device, "compatible", &cplen);
135 if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
144 EXPORT_SYMBOL(of_device_is_compatible);
147 * machine_is_compatible - Test root of device tree for a given compatible value
148 * @compat: compatible string to look for in root node's compatible property.
150 * Returns true if the root node has the given value in its
151 * compatible property.
153 int machine_is_compatible(const char *compat)
155 struct device_node *root;
158 root = of_find_node_by_path("/");
160 rc = of_device_is_compatible(root, compat);
165 EXPORT_SYMBOL(machine_is_compatible);
168 * of_device_is_available - check if a device is available for use
170 * @device: Node to check for availability
172 * Returns 1 if the status property is absent or set to "okay" or "ok",
175 int of_device_is_available(const struct device_node *device)
180 status = of_get_property(device, "status", &statlen);
185 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
191 EXPORT_SYMBOL(of_device_is_available);
194 * of_get_parent - Get a node's parent if any
195 * @node: Node to get parent
197 * Returns a node pointer with refcount incremented, use
198 * of_node_put() on it when done.
200 struct device_node *of_get_parent(const struct device_node *node)
202 struct device_node *np;
207 read_lock(&devtree_lock);
208 np = of_node_get(node->parent);
209 read_unlock(&devtree_lock);
212 EXPORT_SYMBOL(of_get_parent);
215 * of_get_next_parent - Iterate to a node's parent
216 * @node: Node to get parent of
218 * This is like of_get_parent() except that it drops the
219 * refcount on the passed node, making it suitable for iterating
220 * through a node's parents.
222 * Returns a node pointer with refcount incremented, use
223 * of_node_put() on it when done.
225 struct device_node *of_get_next_parent(struct device_node *node)
227 struct device_node *parent;
232 read_lock(&devtree_lock);
233 parent = of_node_get(node->parent);
235 read_unlock(&devtree_lock);
240 * of_get_next_child - Iterate a node childs
242 * @prev: previous child of the parent node, or NULL to get first
244 * Returns a node pointer with refcount incremented, use
245 * of_node_put() on it when done.
247 struct device_node *of_get_next_child(const struct device_node *node,
248 struct device_node *prev)
250 struct device_node *next;
252 read_lock(&devtree_lock);
253 next = prev ? prev->sibling : node->child;
254 for (; next; next = next->sibling)
255 if (of_node_get(next))
258 read_unlock(&devtree_lock);
261 EXPORT_SYMBOL(of_get_next_child);
264 * of_find_node_by_path - Find a node matching a full OF path
265 * @path: The full path to match
267 * Returns a node pointer with refcount incremented, use
268 * of_node_put() on it when done.
270 struct device_node *of_find_node_by_path(const char *path)
272 struct device_node *np = allnodes;
274 read_lock(&devtree_lock);
275 for (; np; np = np->allnext) {
276 if (np->full_name && (of_node_cmp(np->full_name, path) == 0)
280 read_unlock(&devtree_lock);
283 EXPORT_SYMBOL(of_find_node_by_path);
286 * of_find_node_by_name - Find a node by its "name" property
287 * @from: The node to start searching from or NULL, the node
288 * you pass will not be searched, only the next one
289 * will; typically, you pass what the previous call
290 * returned. of_node_put() will be called on it
291 * @name: The name string to match against
293 * Returns a node pointer with refcount incremented, use
294 * of_node_put() on it when done.
296 struct device_node *of_find_node_by_name(struct device_node *from,
299 struct device_node *np;
301 read_lock(&devtree_lock);
302 np = from ? from->allnext : allnodes;
303 for (; np; np = np->allnext)
304 if (np->name && (of_node_cmp(np->name, name) == 0)
308 read_unlock(&devtree_lock);
311 EXPORT_SYMBOL(of_find_node_by_name);
314 * of_find_node_by_type - Find a node by its "device_type" property
315 * @from: The node to start searching from, or NULL to start searching
316 * the entire device tree. The node you pass will not be
317 * searched, only the next one will; typically, you pass
318 * what the previous call returned. of_node_put() will be
319 * called on from for you.
320 * @type: The type string to match against
322 * Returns a node pointer with refcount incremented, use
323 * of_node_put() on it when done.
325 struct device_node *of_find_node_by_type(struct device_node *from,
328 struct device_node *np;
330 read_lock(&devtree_lock);
331 np = from ? from->allnext : allnodes;
332 for (; np; np = np->allnext)
333 if (np->type && (of_node_cmp(np->type, type) == 0)
337 read_unlock(&devtree_lock);
340 EXPORT_SYMBOL(of_find_node_by_type);
343 * of_find_compatible_node - Find a node based on type and one of the
344 * tokens in its "compatible" property
345 * @from: The node to start searching from or NULL, the node
346 * you pass will not be searched, only the next one
347 * will; typically, you pass what the previous call
348 * returned. of_node_put() will be called on it
349 * @type: The type string to match "device_type" or NULL to ignore
350 * @compatible: The string to match to one of the tokens in the device
353 * Returns a node pointer with refcount incremented, use
354 * of_node_put() on it when done.
356 struct device_node *of_find_compatible_node(struct device_node *from,
357 const char *type, const char *compatible)
359 struct device_node *np;
361 read_lock(&devtree_lock);
362 np = from ? from->allnext : allnodes;
363 for (; np; np = np->allnext) {
365 && !(np->type && (of_node_cmp(np->type, type) == 0)))
367 if (of_device_is_compatible(np, compatible) && of_node_get(np))
371 read_unlock(&devtree_lock);
374 EXPORT_SYMBOL(of_find_compatible_node);
377 * of_find_node_with_property - Find a node which has a property with
379 * @from: The node to start searching from or NULL, the node
380 * you pass will not be searched, only the next one
381 * will; typically, you pass what the previous call
382 * returned. of_node_put() will be called on it
383 * @prop_name: The name of the property to look for.
385 * Returns a node pointer with refcount incremented, use
386 * of_node_put() on it when done.
388 struct device_node *of_find_node_with_property(struct device_node *from,
389 const char *prop_name)
391 struct device_node *np;
394 read_lock(&devtree_lock);
395 np = from ? from->allnext : allnodes;
396 for (; np; np = np->allnext) {
397 for (pp = np->properties; pp != 0; pp = pp->next) {
398 if (of_prop_cmp(pp->name, prop_name) == 0) {
406 read_unlock(&devtree_lock);
409 EXPORT_SYMBOL(of_find_node_with_property);
412 * of_match_node - Tell if an device_node has a matching of_match structure
413 * @matches: array of of device match structures to search in
414 * @node: the of device structure to match against
416 * Low level utility function used by device matching.
418 const struct of_device_id *of_match_node(const struct of_device_id *matches,
419 const struct device_node *node)
421 while (matches->name[0] || matches->type[0] || matches->compatible[0]) {
423 if (matches->name[0])
425 && !strcmp(matches->name, node->name);
426 if (matches->type[0])
428 && !strcmp(matches->type, node->type);
429 if (matches->compatible[0])
430 match &= of_device_is_compatible(node,
431 matches->compatible);
438 EXPORT_SYMBOL(of_match_node);
441 * of_find_matching_node - Find a node based on an of_device_id match
443 * @from: The node to start searching from or NULL, the node
444 * you pass will not be searched, only the next one
445 * will; typically, you pass what the previous call
446 * returned. of_node_put() will be called on it
447 * @matches: array of of device match structures to search in
449 * Returns a node pointer with refcount incremented, use
450 * of_node_put() on it when done.
452 struct device_node *of_find_matching_node(struct device_node *from,
453 const struct of_device_id *matches)
455 struct device_node *np;
457 read_lock(&devtree_lock);
458 np = from ? from->allnext : allnodes;
459 for (; np; np = np->allnext) {
460 if (of_match_node(matches, np) && of_node_get(np))
464 read_unlock(&devtree_lock);
467 EXPORT_SYMBOL(of_find_matching_node);
470 * of_modalias_table: Table of explicit compatible ==> modalias mappings
472 * This table allows particulare compatible property values to be mapped
473 * to modalias strings. This is useful for busses which do not directly
474 * understand the OF device tree but are populated based on data contained
475 * within the device tree. SPI and I2C are the two current users of this
478 * In most cases, devices do not need to be listed in this table because
479 * the modalias value can be derived directly from the compatible table.
480 * However, if for any reason a value cannot be derived, then this table
481 * provides a method to override the implicit derivation.
483 * At the moment, a single table is used for all bus types because it is
484 * assumed that the data size is small and that the compatible values
485 * should already be distinct enough to differentiate between SPI, I2C
488 struct of_modalias_table {
492 static struct of_modalias_table of_modalias_table[] = {
493 { "fsl,mcu-mpc8349emitx", "mcu-mpc8349emitx" },
494 { "mmc-spi-slot", "mmc_spi" },
498 * of_modalias_node - Lookup appropriate modalias for a device node
499 * @node: pointer to a device tree node
500 * @modalias: Pointer to buffer that modalias value will be copied into
501 * @len: Length of modalias value
503 * Based on the value of the compatible property, this routine will determine
504 * an appropriate modalias value for a particular device tree node. Two
505 * separate methods are attempted to derive a modalias value.
507 * First method is to lookup the compatible value in of_modalias_table.
508 * Second is to strip off the manufacturer prefix from the first
509 * compatible entry and use the remainder as modalias
511 * This routine returns 0 on success
513 int of_modalias_node(struct device_node *node, char *modalias, int len)
516 const char *compatible;
519 /* 1. search for exception list entry */
520 for (i = 0; i < ARRAY_SIZE(of_modalias_table); i++) {
521 compatible = of_modalias_table[i].of_device;
522 if (!of_device_is_compatible(node, compatible))
524 strlcpy(modalias, of_modalias_table[i].modalias, len);
528 compatible = of_get_property(node, "compatible", &cplen);
532 /* 2. take first compatible entry and strip manufacturer */
533 p = strchr(compatible, ',');
537 strlcpy(modalias, p, len);
540 EXPORT_SYMBOL_GPL(of_modalias_node);
543 * of_parse_phandle - Resolve a phandle property to a device_node pointer
544 * @np: Pointer to device node holding phandle property
545 * @phandle_name: Name of property holding a phandle value
546 * @index: For properties holding a table of phandles, this is the index into
549 * Returns the device_node pointer with refcount incremented. Use
550 * of_node_put() on it when done.
553 of_parse_phandle(struct device_node *np, const char *phandle_name, int index)
555 const phandle *phandle;
558 phandle = of_get_property(np, phandle_name, &size);
559 if ((!phandle) || (size < sizeof(*phandle) * (index + 1)))
562 return of_find_node_by_phandle(phandle[index]);
564 EXPORT_SYMBOL(of_parse_phandle);
567 * of_parse_phandles_with_args - Find a node pointed by phandle in a list
568 * @np: pointer to a device tree node containing a list
569 * @list_name: property name that contains a list
570 * @cells_name: property name that specifies phandles' arguments count
571 * @index: index of a phandle to parse out
572 * @out_node: optional pointer to device_node struct pointer (will be filled)
573 * @out_args: optional pointer to arguments pointer (will be filled)
575 * This function is useful to parse lists of phandles and their arguments.
576 * Returns 0 on success and fills out_node and out_args, on error returns
577 * appropriate errno value.
590 * list = <&phandle1 1 2 &phandle2 3>;
593 * To get a device_node of the `node2' node you may call this:
594 * of_parse_phandles_with_args(node3, "list", "#list-cells", 2, &node2, &args);
596 int of_parse_phandles_with_args(struct device_node *np, const char *list_name,
597 const char *cells_name, int index,
598 struct device_node **out_node,
599 const void **out_args)
606 struct device_node *node = NULL;
607 const void *args = NULL;
609 list = of_get_property(np, list_name, &size);
614 list_end = list + size / sizeof(*list);
616 while (list < list_end) {
618 const phandle *phandle;
623 /* one cell hole in the list = <>; */
627 node = of_find_node_by_phandle(*phandle);
629 pr_debug("%s: could not find phandle\n",
634 cells = of_get_property(node, cells_name, &size);
635 if (!cells || size != sizeof(*cells)) {
636 pr_debug("%s: could not get %s for %s\n",
637 np->full_name, cells_name, node->full_name);
642 if (list > list_end) {
643 pr_debug("%s: insufficient arguments length\n",
648 if (cur_index == index)
659 * args w/o node indicates that the loop above has stopped at
660 * the 'hole' cell. Report this differently.
678 pr_debug("%s failed with status %d\n", __func__, ret);
681 EXPORT_SYMBOL(of_parse_phandles_with_args);
684 * prom_add_property - Add a property to a node
686 int prom_add_property(struct device_node *np, struct property *prop)
688 struct property **next;
692 write_lock_irqsave(&devtree_lock, flags);
693 next = &np->properties;
695 if (strcmp(prop->name, (*next)->name) == 0) {
696 /* duplicate ! don't insert it */
697 write_unlock_irqrestore(&devtree_lock, flags);
700 next = &(*next)->next;
703 write_unlock_irqrestore(&devtree_lock, flags);
705 #ifdef CONFIG_PROC_DEVICETREE
706 /* try to add to proc as well if it was initialized */
708 proc_device_tree_add_prop(np->pde, prop);
709 #endif /* CONFIG_PROC_DEVICETREE */
715 * prom_remove_property - Remove a property from a node.
717 * Note that we don't actually remove it, since we have given out
718 * who-knows-how-many pointers to the data using get-property.
719 * Instead we just move the property to the "dead properties"
720 * list, so it won't be found any more.
722 int prom_remove_property(struct device_node *np, struct property *prop)
724 struct property **next;
728 write_lock_irqsave(&devtree_lock, flags);
729 next = &np->properties;
734 prop->next = np->deadprops;
735 np->deadprops = prop;
739 next = &(*next)->next;
741 write_unlock_irqrestore(&devtree_lock, flags);
746 #ifdef CONFIG_PROC_DEVICETREE
747 /* try to remove the proc node as well */
749 proc_device_tree_remove_prop(np->pde, prop);
750 #endif /* CONFIG_PROC_DEVICETREE */
756 * prom_update_property - Update a property in a node.
758 * Note that we don't actually remove it, since we have given out
759 * who-knows-how-many pointers to the data using get-property.
760 * Instead we just move the property to the "dead properties" list,
761 * and add the new property to the property list
763 int prom_update_property(struct device_node *np,
764 struct property *newprop,
765 struct property *oldprop)
767 struct property **next;
771 write_lock_irqsave(&devtree_lock, flags);
772 next = &np->properties;
774 if (*next == oldprop) {
776 newprop->next = oldprop->next;
778 oldprop->next = np->deadprops;
779 np->deadprops = oldprop;
783 next = &(*next)->next;
785 write_unlock_irqrestore(&devtree_lock, flags);
790 #ifdef CONFIG_PROC_DEVICETREE
791 /* try to add to proc as well if it was initialized */
793 proc_device_tree_update_prop(np->pde, newprop, oldprop);
794 #endif /* CONFIG_PROC_DEVICETREE */