aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/of/base.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-12-11 16:06:58 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2014-12-11 16:06:58 -0500
commit7ef58b32f571bffb7763c6252ad7527562081f34 (patch)
tree6d1493304ec7a47e4d9e3e84dc9f6e53547dff91 /drivers/of/base.c
parent413fd0e3fbf52873f2310eb75bfa6c7b72847277 (diff)
parentc46ca3c8310b61d253a39ff1375ea97912794cd1 (diff)
Merge tag 'devicetree-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/glikely/linux
Pull devicetree changes from Grant Likely: "Lots of activity in the devicetree code for v3.18. Most of it is related to getting all of the overlay support code in place, but there are other important things in there. Highlights: - OF_RECONFIG notifiers for SPI, I2C and Platform devices. Those subsystems can now respond to live changes to the device tree. - CONFIG_OF_OVERLAY method for applying live changes to the device tree - Removal of the of_allnodes list. This used to be used to iterate over all the nodes in the device tree, but it is unnecessary because the same thing can be done by iterating over the list of child pointers. Getting rid of of_allnodes saves some memory and avoids the possibility of of_allnodes being sorted differently from the child lists. - Support for retrieving original DTB blob via sysfs. Needed by kexec. - More unittests - Documentation and minor bug fixes" * tag 'devicetree-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/glikely/linux: (42 commits) of: Delete unnecessary check before calling "of_node_put()" of: Drop ->next pointer from struct device_node spi: Check for spi_of_notifier when CONFIG_OF_DYNAMIC=y of: support passing console options with stdout-path of: add optional options parameter to of_find_node_by_path() of: Add bindings for chosen node, stdout-path of: Remove unneeded and incorrect MODULE_DEVICE_TABLE ARM: dt: fix up PL011 device tree bindings of: base, fix of_property_read_string_helper kernel-doc of: remove select of non-existant OF_DEVICE config symbol spi/of: Add OF notifier handler spi/of: Create new device registration method and accessors i2c/of: Add OF_RECONFIG notifier handler i2c/of: Factor out Devicetree registration code of/overlay: Add overlay unittests of/overlay: Introduce DT overlay support of/reconfig: Add OF_DYNAMIC notifier for platform_bus_type of/reconfig: Always use the same structure for notifiers of/reconfig: Add debug output for OF_RECONFIG notifiers of/reconfig: Add empty stubs for the of_reconfig methods ...
Diffstat (limited to 'drivers/of/base.c')
-rw-r--r--drivers/of/base.c135
1 files changed, 79 insertions, 56 deletions
diff --git a/drivers/of/base.c b/drivers/of/base.c
index 4c2ccde42427..36536b6a8834 100644
--- a/drivers/of/base.c
+++ b/drivers/of/base.c
@@ -32,11 +32,12 @@
32 32
33LIST_HEAD(aliases_lookup); 33LIST_HEAD(aliases_lookup);
34 34
35struct device_node *of_allnodes; 35struct device_node *of_root;
36EXPORT_SYMBOL(of_allnodes); 36EXPORT_SYMBOL(of_root);
37struct device_node *of_chosen; 37struct device_node *of_chosen;
38struct device_node *of_aliases; 38struct device_node *of_aliases;
39struct device_node *of_stdout; 39struct device_node *of_stdout;
40static const char *of_stdout_options;
40 41
41struct kset *of_kset; 42struct kset *of_kset;
42 43
@@ -48,7 +49,7 @@ struct kset *of_kset;
48 */ 49 */
49DEFINE_MUTEX(of_mutex); 50DEFINE_MUTEX(of_mutex);
50 51
51/* use when traversing tree through the allnext, child, sibling, 52/* use when traversing tree through the child, sibling,
52 * or parent members of struct device_node. 53 * or parent members of struct device_node.
53 */ 54 */
54DEFINE_RAW_SPINLOCK(devtree_lock); 55DEFINE_RAW_SPINLOCK(devtree_lock);
@@ -204,7 +205,7 @@ static int __init of_init(void)
204 mutex_unlock(&of_mutex); 205 mutex_unlock(&of_mutex);
205 206
206 /* Symlink in /proc as required by userspace ABI */ 207 /* Symlink in /proc as required by userspace ABI */
207 if (of_allnodes) 208 if (of_root)
208 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base"); 209 proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
209 210
210 return 0; 211 return 0;
@@ -245,6 +246,23 @@ struct property *of_find_property(const struct device_node *np,
245} 246}
246EXPORT_SYMBOL(of_find_property); 247EXPORT_SYMBOL(of_find_property);
247 248
249struct device_node *__of_find_all_nodes(struct device_node *prev)
250{
251 struct device_node *np;
252 if (!prev) {
253 np = of_root;
254 } else if (prev->child) {
255 np = prev->child;
256 } else {
257 /* Walk back up looking for a sibling, or the end of the structure */
258 np = prev;
259 while (np->parent && !np->sibling)
260 np = np->parent;
261 np = np->sibling; /* Might be null at the end of the tree */
262 }
263 return np;
264}
265
248/** 266/**
249 * of_find_all_nodes - Get next node in global list 267 * of_find_all_nodes - Get next node in global list
250 * @prev: Previous node or NULL to start iteration 268 * @prev: Previous node or NULL to start iteration
@@ -259,10 +277,8 @@ struct device_node *of_find_all_nodes(struct device_node *prev)
259 unsigned long flags; 277 unsigned long flags;
260 278
261 raw_spin_lock_irqsave(&devtree_lock, flags); 279 raw_spin_lock_irqsave(&devtree_lock, flags);
262 np = prev ? prev->allnext : of_allnodes; 280 np = __of_find_all_nodes(prev);
263 for (; np != NULL; np = np->allnext) 281 of_node_get(np);
264 if (of_node_get(np))
265 break;
266 of_node_put(prev); 282 of_node_put(prev);
267 raw_spin_unlock_irqrestore(&devtree_lock, flags); 283 raw_spin_unlock_irqrestore(&devtree_lock, flags);
268 return np; 284 return np;
@@ -485,7 +501,7 @@ EXPORT_SYMBOL(of_device_is_compatible);
485 * of_machine_is_compatible - Test root of device tree for a given compatible value 501 * of_machine_is_compatible - Test root of device tree for a given compatible value
486 * @compat: compatible string to look for in root node's compatible property. 502 * @compat: compatible string to look for in root node's compatible property.
487 * 503 *
488 * Returns true if the root node has the given value in its 504 * Returns a positive integer if the root node has the given value in its
489 * compatible property. 505 * compatible property.
490 */ 506 */
491int of_machine_is_compatible(const char *compat) 507int of_machine_is_compatible(const char *compat)
@@ -507,27 +523,27 @@ EXPORT_SYMBOL(of_machine_is_compatible);
507 * 523 *
508 * @device: Node to check for availability, with locks already held 524 * @device: Node to check for availability, with locks already held
509 * 525 *
510 * Returns 1 if the status property is absent or set to "okay" or "ok", 526 * Returns true if the status property is absent or set to "okay" or "ok",
511 * 0 otherwise 527 * false otherwise
512 */ 528 */
513static int __of_device_is_available(const struct device_node *device) 529static bool __of_device_is_available(const struct device_node *device)
514{ 530{
515 const char *status; 531 const char *status;
516 int statlen; 532 int statlen;
517 533
518 if (!device) 534 if (!device)
519 return 0; 535 return false;
520 536
521 status = __of_get_property(device, "status", &statlen); 537 status = __of_get_property(device, "status", &statlen);
522 if (status == NULL) 538 if (status == NULL)
523 return 1; 539 return true;
524 540
525 if (statlen > 0) { 541 if (statlen > 0) {
526 if (!strcmp(status, "okay") || !strcmp(status, "ok")) 542 if (!strcmp(status, "okay") || !strcmp(status, "ok"))
527 return 1; 543 return true;
528 } 544 }
529 545
530 return 0; 546 return false;
531} 547}
532 548
533/** 549/**
@@ -535,13 +551,13 @@ static int __of_device_is_available(const struct device_node *device)
535 * 551 *
536 * @device: Node to check for availability 552 * @device: Node to check for availability
537 * 553 *
538 * Returns 1 if the status property is absent or set to "okay" or "ok", 554 * Returns true if the status property is absent or set to "okay" or "ok",
539 * 0 otherwise 555 * false otherwise
540 */ 556 */
541int of_device_is_available(const struct device_node *device) 557bool of_device_is_available(const struct device_node *device)
542{ 558{
543 unsigned long flags; 559 unsigned long flags;
544 int res; 560 bool res;
545 561
546 raw_spin_lock_irqsave(&devtree_lock, flags); 562 raw_spin_lock_irqsave(&devtree_lock, flags);
547 res = __of_device_is_available(device); 563 res = __of_device_is_available(device);
@@ -577,9 +593,9 @@ EXPORT_SYMBOL(of_get_parent);
577 * of_get_next_parent - Iterate to a node's parent 593 * of_get_next_parent - Iterate to a node's parent
578 * @node: Node to get parent of 594 * @node: Node to get parent of
579 * 595 *
580 * This is like of_get_parent() except that it drops the 596 * This is like of_get_parent() except that it drops the
581 * refcount on the passed node, making it suitable for iterating 597 * refcount on the passed node, making it suitable for iterating
582 * through a node's parents. 598 * through a node's parents.
583 * 599 *
584 * Returns a node pointer with refcount incremented, use 600 * Returns a node pointer with refcount incremented, use
585 * of_node_put() on it when done. 601 * of_node_put() on it when done.
@@ -699,10 +715,15 @@ static struct device_node *__of_find_node_by_path(struct device_node *parent,
699{ 715{
700 struct device_node *child; 716 struct device_node *child;
701 int len = strchrnul(path, '/') - path; 717 int len = strchrnul(path, '/') - path;
718 int term;
702 719
703 if (!len) 720 if (!len)
704 return NULL; 721 return NULL;
705 722
723 term = strchrnul(path, ':') - path;
724 if (term < len)
725 len = term;
726
706 __for_each_child_of_node(parent, child) { 727 __for_each_child_of_node(parent, child) {
707 const char *name = strrchr(child->full_name, '/'); 728 const char *name = strrchr(child->full_name, '/');
708 if (WARN(!name, "malformed device_node %s\n", child->full_name)) 729 if (WARN(!name, "malformed device_node %s\n", child->full_name))
@@ -715,11 +736,14 @@ static struct device_node *__of_find_node_by_path(struct device_node *parent,
715} 736}
716 737
717/** 738/**
718 * of_find_node_by_path - Find a node matching a full OF path 739 * of_find_node_opts_by_path - Find a node matching a full OF path
719 * @path: Either the full path to match, or if the path does not 740 * @path: Either the full path to match, or if the path does not
720 * start with '/', the name of a property of the /aliases 741 * start with '/', the name of a property of the /aliases
721 * node (an alias). In the case of an alias, the node 742 * node (an alias). In the case of an alias, the node
722 * matching the alias' value will be returned. 743 * matching the alias' value will be returned.
744 * @opts: Address of a pointer into which to store the start of
745 * an options string appended to the end of the path with
746 * a ':' separator.
723 * 747 *
724 * Valid paths: 748 * Valid paths:
725 * /foo/bar Full path 749 * /foo/bar Full path
@@ -729,19 +753,23 @@ static struct device_node *__of_find_node_by_path(struct device_node *parent,
729 * Returns a node pointer with refcount incremented, use 753 * Returns a node pointer with refcount incremented, use
730 * of_node_put() on it when done. 754 * of_node_put() on it when done.
731 */ 755 */
732struct device_node *of_find_node_by_path(const char *path) 756struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
733{ 757{
734 struct device_node *np = NULL; 758 struct device_node *np = NULL;
735 struct property *pp; 759 struct property *pp;
736 unsigned long flags; 760 unsigned long flags;
761 const char *separator = strchr(path, ':');
762
763 if (opts)
764 *opts = separator ? separator + 1 : NULL;
737 765
738 if (strcmp(path, "/") == 0) 766 if (strcmp(path, "/") == 0)
739 return of_node_get(of_allnodes); 767 return of_node_get(of_root);
740 768
741 /* The path could begin with an alias */ 769 /* The path could begin with an alias */
742 if (*path != '/') { 770 if (*path != '/') {
743 char *p = strchrnul(path, '/'); 771 char *p = strchrnul(path, '/');
744 int len = p - path; 772 int len = separator ? separator - path : p - path;
745 773
746 /* of_aliases must not be NULL */ 774 /* of_aliases must not be NULL */
747 if (!of_aliases) 775 if (!of_aliases)
@@ -761,7 +789,7 @@ struct device_node *of_find_node_by_path(const char *path)
761 /* Step down the tree matching path components */ 789 /* Step down the tree matching path components */
762 raw_spin_lock_irqsave(&devtree_lock, flags); 790 raw_spin_lock_irqsave(&devtree_lock, flags);
763 if (!np) 791 if (!np)
764 np = of_node_get(of_allnodes); 792 np = of_node_get(of_root);
765 while (np && *path == '/') { 793 while (np && *path == '/') {
766 path++; /* Increment past '/' delimiter */ 794 path++; /* Increment past '/' delimiter */
767 np = __of_find_node_by_path(np, path); 795 np = __of_find_node_by_path(np, path);
@@ -770,7 +798,7 @@ struct device_node *of_find_node_by_path(const char *path)
770 raw_spin_unlock_irqrestore(&devtree_lock, flags); 798 raw_spin_unlock_irqrestore(&devtree_lock, flags);
771 return np; 799 return np;
772} 800}
773EXPORT_SYMBOL(of_find_node_by_path); 801EXPORT_SYMBOL(of_find_node_opts_by_path);
774 802
775/** 803/**
776 * of_find_node_by_name - Find a node by its "name" property 804 * of_find_node_by_name - Find a node by its "name" property
@@ -790,8 +818,7 @@ struct device_node *of_find_node_by_name(struct device_node *from,
790 unsigned long flags; 818 unsigned long flags;
791 819
792 raw_spin_lock_irqsave(&devtree_lock, flags); 820 raw_spin_lock_irqsave(&devtree_lock, flags);
793 np = from ? from->allnext : of_allnodes; 821 for_each_of_allnodes_from(from, np)
794 for (; np; np = np->allnext)
795 if (np->name && (of_node_cmp(np->name, name) == 0) 822 if (np->name && (of_node_cmp(np->name, name) == 0)
796 && of_node_get(np)) 823 && of_node_get(np))
797 break; 824 break;
@@ -820,8 +847,7 @@ struct device_node *of_find_node_by_type(struct device_node *from,
820 unsigned long flags; 847 unsigned long flags;
821 848
822 raw_spin_lock_irqsave(&devtree_lock, flags); 849 raw_spin_lock_irqsave(&devtree_lock, flags);
823 np = from ? from->allnext : of_allnodes; 850 for_each_of_allnodes_from(from, np)
824 for (; np; np = np->allnext)
825 if (np->type && (of_node_cmp(np->type, type) == 0) 851 if (np->type && (of_node_cmp(np->type, type) == 0)
826 && of_node_get(np)) 852 && of_node_get(np))
827 break; 853 break;
@@ -852,12 +878,10 @@ struct device_node *of_find_compatible_node(struct device_node *from,
852 unsigned long flags; 878 unsigned long flags;
853 879
854 raw_spin_lock_irqsave(&devtree_lock, flags); 880 raw_spin_lock_irqsave(&devtree_lock, flags);
855 np = from ? from->allnext : of_allnodes; 881 for_each_of_allnodes_from(from, np)
856 for (; np; np = np->allnext) {
857 if (__of_device_is_compatible(np, compatible, type, NULL) && 882 if (__of_device_is_compatible(np, compatible, type, NULL) &&
858 of_node_get(np)) 883 of_node_get(np))
859 break; 884 break;
860 }
861 of_node_put(from); 885 of_node_put(from);
862 raw_spin_unlock_irqrestore(&devtree_lock, flags); 886 raw_spin_unlock_irqrestore(&devtree_lock, flags);
863 return np; 887 return np;
@@ -884,8 +908,7 @@ struct device_node *of_find_node_with_property(struct device_node *from,
884 unsigned long flags; 908 unsigned long flags;
885 909
886 raw_spin_lock_irqsave(&devtree_lock, flags); 910 raw_spin_lock_irqsave(&devtree_lock, flags);
887 np = from ? from->allnext : of_allnodes; 911 for_each_of_allnodes_from(from, np) {
888 for (; np; np = np->allnext) {
889 for (pp = np->properties; pp; pp = pp->next) { 912 for (pp = np->properties; pp; pp = pp->next) {
890 if (of_prop_cmp(pp->name, prop_name) == 0) { 913 if (of_prop_cmp(pp->name, prop_name) == 0) {
891 of_node_get(np); 914 of_node_get(np);
@@ -923,7 +946,7 @@ const struct of_device_id *__of_match_node(const struct of_device_id *matches,
923} 946}
924 947
925/** 948/**
926 * of_match_node - Tell if an device_node has a matching of_match structure 949 * of_match_node - Tell if a device_node has a matching of_match structure
927 * @matches: array of of device match structures to search in 950 * @matches: array of of device match structures to search in
928 * @node: the of device structure to match against 951 * @node: the of device structure to match against
929 * 952 *
@@ -967,8 +990,7 @@ struct device_node *of_find_matching_node_and_match(struct device_node *from,
967 *match = NULL; 990 *match = NULL;
968 991
969 raw_spin_lock_irqsave(&devtree_lock, flags); 992 raw_spin_lock_irqsave(&devtree_lock, flags);
970 np = from ? from->allnext : of_allnodes; 993 for_each_of_allnodes_from(from, np) {
971 for (; np; np = np->allnext) {
972 m = __of_match_node(matches, np); 994 m = __of_match_node(matches, np);
973 if (m && of_node_get(np)) { 995 if (m && of_node_get(np)) {
974 if (match) 996 if (match)
@@ -1025,7 +1047,7 @@ struct device_node *of_find_node_by_phandle(phandle handle)
1025 return NULL; 1047 return NULL;
1026 1048
1027 raw_spin_lock_irqsave(&devtree_lock, flags); 1049 raw_spin_lock_irqsave(&devtree_lock, flags);
1028 for (np = of_allnodes; np; np = np->allnext) 1050 for_each_of_allnodes(np)
1029 if (np->phandle == handle) 1051 if (np->phandle == handle)
1030 break; 1052 break;
1031 of_node_get(np); 1053 of_node_get(np);
@@ -1350,7 +1372,7 @@ int of_property_match_string(struct device_node *np, const char *propname,
1350EXPORT_SYMBOL_GPL(of_property_match_string); 1372EXPORT_SYMBOL_GPL(of_property_match_string);
1351 1373
1352/** 1374/**
1353 * of_property_read_string_util() - Utility helper for parsing string properties 1375 * of_property_read_string_helper() - Utility helper for parsing string properties
1354 * @np: device node from which the property value is to be read. 1376 * @np: device node from which the property value is to be read.
1355 * @propname: name of the property to be searched. 1377 * @propname: name of the property to be searched.
1356 * @out_strs: output array of string pointers. 1378 * @out_strs: output array of string pointers.
@@ -1549,21 +1571,21 @@ EXPORT_SYMBOL(of_parse_phandle);
1549 * Returns 0 on success and fills out_args, on error returns appropriate 1571 * Returns 0 on success and fills out_args, on error returns appropriate
1550 * errno value. 1572 * errno value.
1551 * 1573 *
1552 * Caller is responsible to call of_node_put() on the returned out_args->node 1574 * Caller is responsible to call of_node_put() on the returned out_args->np
1553 * pointer. 1575 * pointer.
1554 * 1576 *
1555 * Example: 1577 * Example:
1556 * 1578 *
1557 * phandle1: node1 { 1579 * phandle1: node1 {
1558 * #list-cells = <2>; 1580 * #list-cells = <2>;
1559 * } 1581 * }
1560 * 1582 *
1561 * phandle2: node2 { 1583 * phandle2: node2 {
1562 * #list-cells = <1>; 1584 * #list-cells = <1>;
1563 * } 1585 * }
1564 * 1586 *
1565 * node3 { 1587 * node3 {
1566 * list = <&phandle1 1 2 &phandle2 3>; 1588 * list = <&phandle1 1 2 &phandle2 3>;
1567 * } 1589 * }
1568 * 1590 *
1569 * To get a device_node of the `node2' node you may call this: 1591 * To get a device_node of the `node2' node you may call this:
@@ -1592,7 +1614,7 @@ EXPORT_SYMBOL(of_parse_phandle_with_args);
1592 * Returns 0 on success and fills out_args, on error returns appropriate 1614 * Returns 0 on success and fills out_args, on error returns appropriate
1593 * errno value. 1615 * errno value.
1594 * 1616 *
1595 * Caller is responsible to call of_node_put() on the returned out_args->node 1617 * Caller is responsible to call of_node_put() on the returned out_args->np
1596 * pointer. 1618 * pointer.
1597 * 1619 *
1598 * Example: 1620 * Example:
@@ -1604,7 +1626,7 @@ EXPORT_SYMBOL(of_parse_phandle_with_args);
1604 * } 1626 * }
1605 * 1627 *
1606 * node3 { 1628 * node3 {
1607 * list = <&phandle1 0 2 &phandle2 2 3>; 1629 * list = <&phandle1 0 2 &phandle2 2 3>;
1608 * } 1630 * }
1609 * 1631 *
1610 * To get a device_node of the `node2' node you may call this: 1632 * To get a device_node of the `node2' node you may call this:
@@ -1838,14 +1860,14 @@ static void of_alias_add(struct alias_prop *ap, struct device_node *np,
1838} 1860}
1839 1861
1840/** 1862/**
1841 * of_alias_scan - Scan all properties of 'aliases' node 1863 * of_alias_scan - Scan all properties of the 'aliases' node
1842 * 1864 *
1843 * The function scans all the properties of 'aliases' node and populate 1865 * The function scans all the properties of the 'aliases' node and populates
1844 * the the global lookup table with the properties. It returns the 1866 * the global lookup table with the properties. It returns the
1845 * number of alias_prop found, or error code in error case. 1867 * number of alias properties found, or an error code in case of failure.
1846 * 1868 *
1847 * @dt_alloc: An allocator that provides a virtual address to memory 1869 * @dt_alloc: An allocator that provides a virtual address to memory
1848 * for the resulting tree 1870 * for storing the resulting tree
1849 */ 1871 */
1850void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align)) 1872void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1851{ 1873{
@@ -1864,7 +1886,7 @@ void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
1864 if (IS_ENABLED(CONFIG_PPC) && !name) 1886 if (IS_ENABLED(CONFIG_PPC) && !name)
1865 name = of_get_property(of_aliases, "stdout", NULL); 1887 name = of_get_property(of_aliases, "stdout", NULL);
1866 if (name) 1888 if (name)
1867 of_stdout = of_find_node_by_path(name); 1889 of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
1868 } 1890 }
1869 1891
1870 if (!of_aliases) 1892 if (!of_aliases)
@@ -1990,7 +2012,8 @@ bool of_console_check(struct device_node *dn, char *name, int index)
1990{ 2012{
1991 if (!dn || dn != of_stdout || console_set_on_cmdline) 2013 if (!dn || dn != of_stdout || console_set_on_cmdline)
1992 return false; 2014 return false;
1993 return !add_preferred_console(name, index, NULL); 2015 return !add_preferred_console(name, index,
2016 kstrdup(of_stdout_options, GFP_KERNEL));
1994} 2017}
1995EXPORT_SYMBOL_GPL(of_console_check); 2018EXPORT_SYMBOL_GPL(of_console_check);
1996 2019