blob: 01b4174a3b468974e242c1c745d1b1aa8aa51322 [file] [log] [blame]
Grant Likely53a42092011-12-12 09:25:57 -07001/*
2 * Self tests for device tree subsystem
3 */
4
Grant Likelycabb7d52013-02-12 21:19:37 +00005#define pr_fmt(fmt) "### dt-test ### " fmt
Grant Likely53a42092011-12-12 09:25:57 -07006
7#include <linux/clk.h>
8#include <linux/err.h>
9#include <linux/errno.h>
10#include <linux/module.h>
11#include <linux/of.h>
12#include <linux/list.h>
13#include <linux/mutex.h>
14#include <linux/slab.h>
15#include <linux/device.h>
16
17static bool selftest_passed = true;
18#define selftest(result, fmt, ...) { \
Grant Likelycabb7d52013-02-12 21:19:37 +000019 if (!(result)) { \
Grant Likely53a42092011-12-12 09:25:57 -070020 pr_err("FAIL %s:%i " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
Grant Likelycabb7d52013-02-12 21:19:37 +000021 selftest_passed = false; \
22 } else { \
23 pr_info("pass %s:%i\n", __FILE__, __LINE__); \
24 } \
Grant Likely53a42092011-12-12 09:25:57 -070025}
26
27static void __init of_selftest_parse_phandle_with_args(void)
28{
29 struct device_node *np;
30 struct of_phandle_args args;
Grant Likelycabb7d52013-02-12 21:19:37 +000031 int i, rc;
Grant Likely53a42092011-12-12 09:25:57 -070032
Grant Likely53a42092011-12-12 09:25:57 -070033 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
34 if (!np) {
35 pr_err("missing testcase data\n");
36 return;
37 }
38
Grant Likelyf7f951c2013-02-12 17:41:22 +000039 for (i = 0; i < 8; i++) {
Grant Likely53a42092011-12-12 09:25:57 -070040 bool passed = true;
41 rc = of_parse_phandle_with_args(np, "phandle-list",
42 "#phandle-cells", i, &args);
43
44 /* Test the values from tests-phandle.dtsi */
45 switch (i) {
46 case 0:
47 passed &= !rc;
48 passed &= (args.args_count == 1);
49 passed &= (args.args[0] == (i + 1));
50 break;
51 case 1:
52 passed &= !rc;
53 passed &= (args.args_count == 2);
54 passed &= (args.args[0] == (i + 1));
55 passed &= (args.args[1] == 0);
56 break;
57 case 2:
58 passed &= (rc == -ENOENT);
59 break;
60 case 3:
61 passed &= !rc;
62 passed &= (args.args_count == 3);
63 passed &= (args.args[0] == (i + 1));
64 passed &= (args.args[1] == 4);
65 passed &= (args.args[2] == 3);
66 break;
67 case 4:
68 passed &= !rc;
69 passed &= (args.args_count == 2);
70 passed &= (args.args[0] == (i + 1));
71 passed &= (args.args[1] == 100);
72 break;
73 case 5:
74 passed &= !rc;
75 passed &= (args.args_count == 0);
76 break;
77 case 6:
78 passed &= !rc;
79 passed &= (args.args_count == 1);
80 passed &= (args.args[0] == (i + 1));
81 break;
82 case 7:
Grant Likelycabb7d52013-02-12 21:19:37 +000083 passed &= (rc == -ENOENT);
Grant Likely53a42092011-12-12 09:25:57 -070084 break;
85 default:
86 passed = false;
87 }
88
Grant Likelycabb7d52013-02-12 21:19:37 +000089 selftest(passed, "index %i - data error on node %s rc=%i\n",
90 i, args.np->full_name, rc);
Grant Likely53a42092011-12-12 09:25:57 -070091 }
92
93 /* Check for missing list property */
94 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
95 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +000096 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
Grant Likely53a42092011-12-12 09:25:57 -070097
98 /* Check for missing cells property */
99 rc = of_parse_phandle_with_args(np, "phandle-list",
100 "#phandle-cells-missing", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000101 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700102
103 /* Check for bad phandle in list */
104 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
105 "#phandle-cells", 0, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000106 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700107
108 /* Check for incorrectly formed argument list */
109 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
110 "#phandle-cells", 1, &args);
Grant Likelycabb7d52013-02-12 21:19:37 +0000111 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
Grant Likely53a42092011-12-12 09:25:57 -0700112}
113
Grant Likely7aff0fe2011-12-12 09:25:58 -0700114static void __init of_selftest_property_match_string(void)
115{
116 struct device_node *np;
117 int rc;
118
119 pr_info("start\n");
120 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
121 if (!np) {
122 pr_err("No testcase data in device tree\n");
123 return;
124 }
125
126 rc = of_property_match_string(np, "phandle-list-names", "first");
127 selftest(rc == 0, "first expected:0 got:%i\n", rc);
128 rc = of_property_match_string(np, "phandle-list-names", "second");
129 selftest(rc == 1, "second expected:0 got:%i\n", rc);
130 rc = of_property_match_string(np, "phandle-list-names", "third");
131 selftest(rc == 2, "third expected:0 got:%i\n", rc);
132 rc = of_property_match_string(np, "phandle-list-names", "fourth");
133 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
134 rc = of_property_match_string(np, "missing-property", "blah");
135 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
136 rc = of_property_match_string(np, "empty-property", "blah");
137 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
138 rc = of_property_match_string(np, "unterminated-string", "blah");
139 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
140}
141
Grant Likely53a42092011-12-12 09:25:57 -0700142static int __init of_selftest(void)
143{
144 struct device_node *np;
145
146 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
147 if (!np) {
148 pr_info("No testcase data in device tree; not running tests\n");
149 return 0;
150 }
151 of_node_put(np);
152
153 pr_info("start of selftest - you will see error messages\n");
154 of_selftest_parse_phandle_with_args();
Grant Likely7aff0fe2011-12-12 09:25:58 -0700155 of_selftest_property_match_string();
Grant Likely53a42092011-12-12 09:25:57 -0700156 pr_info("end of selftest - %s\n", selftest_passed ? "PASS" : "FAIL");
157 return 0;
158}
159late_initcall(of_selftest);