Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / of / selftest.c
blobe21012bde639cde305a8c14aab011fad020a7da2
1 /*
2 * Self tests for device tree subsystem
3 */
5 #define pr_fmt(fmt) "### dt-test ### " fmt
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/of_irq.h>
13 #include <linux/list.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
18 static struct selftest_results {
19 int passed;
20 int failed;
21 } selftest_results;
23 #define selftest(result, fmt, ...) { \
24 if (!(result)) { \
25 selftest_results.failed++; \
26 pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \
27 } else { \
28 selftest_results.passed++; \
29 pr_debug("pass %s():%i\n", __func__, __LINE__); \
30 } \
33 static void __init of_selftest_parse_phandle_with_args(void)
35 struct device_node *np;
36 struct of_phandle_args args;
37 int i, rc;
39 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
40 if (!np) {
41 pr_err("missing testcase data\n");
42 return;
45 rc = of_count_phandle_with_args(np, "phandle-list", "#phandle-cells");
46 selftest(rc == 7, "of_count_phandle_with_args() returned %i, expected 7\n", rc);
48 for (i = 0; i < 8; i++) {
49 bool passed = true;
50 rc = of_parse_phandle_with_args(np, "phandle-list",
51 "#phandle-cells", i, &args);
53 /* Test the values from tests-phandle.dtsi */
54 switch (i) {
55 case 0:
56 passed &= !rc;
57 passed &= (args.args_count == 1);
58 passed &= (args.args[0] == (i + 1));
59 break;
60 case 1:
61 passed &= !rc;
62 passed &= (args.args_count == 2);
63 passed &= (args.args[0] == (i + 1));
64 passed &= (args.args[1] == 0);
65 break;
66 case 2:
67 passed &= (rc == -ENOENT);
68 break;
69 case 3:
70 passed &= !rc;
71 passed &= (args.args_count == 3);
72 passed &= (args.args[0] == (i + 1));
73 passed &= (args.args[1] == 4);
74 passed &= (args.args[2] == 3);
75 break;
76 case 4:
77 passed &= !rc;
78 passed &= (args.args_count == 2);
79 passed &= (args.args[0] == (i + 1));
80 passed &= (args.args[1] == 100);
81 break;
82 case 5:
83 passed &= !rc;
84 passed &= (args.args_count == 0);
85 break;
86 case 6:
87 passed &= !rc;
88 passed &= (args.args_count == 1);
89 passed &= (args.args[0] == (i + 1));
90 break;
91 case 7:
92 passed &= (rc == -ENOENT);
93 break;
94 default:
95 passed = false;
98 selftest(passed, "index %i - data error on node %s rc=%i\n",
99 i, args.np->full_name, rc);
102 /* Check for missing list property */
103 rc = of_parse_phandle_with_args(np, "phandle-list-missing",
104 "#phandle-cells", 0, &args);
105 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
106 rc = of_count_phandle_with_args(np, "phandle-list-missing",
107 "#phandle-cells");
108 selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc);
110 /* Check for missing cells property */
111 rc = of_parse_phandle_with_args(np, "phandle-list",
112 "#phandle-cells-missing", 0, &args);
113 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
114 rc = of_count_phandle_with_args(np, "phandle-list",
115 "#phandle-cells-missing");
116 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
118 /* Check for bad phandle in list */
119 rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle",
120 "#phandle-cells", 0, &args);
121 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
122 rc = of_count_phandle_with_args(np, "phandle-list-bad-phandle",
123 "#phandle-cells");
124 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
126 /* Check for incorrectly formed argument list */
127 rc = of_parse_phandle_with_args(np, "phandle-list-bad-args",
128 "#phandle-cells", 1, &args);
129 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
130 rc = of_count_phandle_with_args(np, "phandle-list-bad-args",
131 "#phandle-cells");
132 selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc);
135 static void __init of_selftest_property_match_string(void)
137 struct device_node *np;
138 int rc;
140 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
141 if (!np) {
142 pr_err("No testcase data in device tree\n");
143 return;
146 rc = of_property_match_string(np, "phandle-list-names", "first");
147 selftest(rc == 0, "first expected:0 got:%i\n", rc);
148 rc = of_property_match_string(np, "phandle-list-names", "second");
149 selftest(rc == 1, "second expected:0 got:%i\n", rc);
150 rc = of_property_match_string(np, "phandle-list-names", "third");
151 selftest(rc == 2, "third expected:0 got:%i\n", rc);
152 rc = of_property_match_string(np, "phandle-list-names", "fourth");
153 selftest(rc == -ENODATA, "unmatched string; rc=%i", rc);
154 rc = of_property_match_string(np, "missing-property", "blah");
155 selftest(rc == -EINVAL, "missing property; rc=%i", rc);
156 rc = of_property_match_string(np, "empty-property", "blah");
157 selftest(rc == -ENODATA, "empty property; rc=%i", rc);
158 rc = of_property_match_string(np, "unterminated-string", "blah");
159 selftest(rc == -EILSEQ, "unterminated string; rc=%i", rc);
162 static void __init of_selftest_parse_interrupts(void)
164 struct device_node *np;
165 struct of_phandle_args args;
166 int i, rc;
168 np = of_find_node_by_path("/testcase-data/interrupts/interrupts0");
169 if (!np) {
170 pr_err("missing testcase data\n");
171 return;
174 for (i = 0; i < 4; i++) {
175 bool passed = true;
176 args.args_count = 0;
177 rc = of_irq_parse_one(np, i, &args);
179 passed &= !rc;
180 passed &= (args.args_count == 1);
181 passed &= (args.args[0] == (i + 1));
183 selftest(passed, "index %i - data error on node %s rc=%i\n",
184 i, args.np->full_name, rc);
186 of_node_put(np);
188 np = of_find_node_by_path("/testcase-data/interrupts/interrupts1");
189 if (!np) {
190 pr_err("missing testcase data\n");
191 return;
194 for (i = 0; i < 4; i++) {
195 bool passed = true;
196 args.args_count = 0;
197 rc = of_irq_parse_one(np, i, &args);
199 /* Test the values from tests-phandle.dtsi */
200 switch (i) {
201 case 0:
202 passed &= !rc;
203 passed &= (args.args_count == 1);
204 passed &= (args.args[0] == 9);
205 break;
206 case 1:
207 passed &= !rc;
208 passed &= (args.args_count == 3);
209 passed &= (args.args[0] == 10);
210 passed &= (args.args[1] == 11);
211 passed &= (args.args[2] == 12);
212 break;
213 case 2:
214 passed &= !rc;
215 passed &= (args.args_count == 2);
216 passed &= (args.args[0] == 13);
217 passed &= (args.args[1] == 14);
218 break;
219 case 3:
220 passed &= !rc;
221 passed &= (args.args_count == 2);
222 passed &= (args.args[0] == 15);
223 passed &= (args.args[1] == 16);
224 break;
225 default:
226 passed = false;
228 selftest(passed, "index %i - data error on node %s rc=%i\n",
229 i, args.np->full_name, rc);
231 of_node_put(np);
234 static void __init of_selftest_parse_interrupts_extended(void)
236 struct device_node *np;
237 struct of_phandle_args args;
238 int i, rc;
240 np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0");
241 if (!np) {
242 pr_err("missing testcase data\n");
243 return;
246 for (i = 0; i < 7; i++) {
247 bool passed = true;
248 rc = of_irq_parse_one(np, i, &args);
250 /* Test the values from tests-phandle.dtsi */
251 switch (i) {
252 case 0:
253 passed &= !rc;
254 passed &= (args.args_count == 1);
255 passed &= (args.args[0] == 1);
256 break;
257 case 1:
258 passed &= !rc;
259 passed &= (args.args_count == 3);
260 passed &= (args.args[0] == 2);
261 passed &= (args.args[1] == 3);
262 passed &= (args.args[2] == 4);
263 break;
264 case 2:
265 passed &= !rc;
266 passed &= (args.args_count == 2);
267 passed &= (args.args[0] == 5);
268 passed &= (args.args[1] == 6);
269 break;
270 case 3:
271 passed &= !rc;
272 passed &= (args.args_count == 1);
273 passed &= (args.args[0] == 9);
274 break;
275 case 4:
276 passed &= !rc;
277 passed &= (args.args_count == 3);
278 passed &= (args.args[0] == 10);
279 passed &= (args.args[1] == 11);
280 passed &= (args.args[2] == 12);
281 break;
282 case 5:
283 passed &= !rc;
284 passed &= (args.args_count == 2);
285 passed &= (args.args[0] == 13);
286 passed &= (args.args[1] == 14);
287 break;
288 case 6:
289 passed &= !rc;
290 passed &= (args.args_count == 1);
291 passed &= (args.args[0] == 15);
292 break;
293 default:
294 passed = false;
297 selftest(passed, "index %i - data error on node %s rc=%i\n",
298 i, args.np->full_name, rc);
300 of_node_put(np);
303 static int __init of_selftest(void)
305 struct device_node *np;
307 np = of_find_node_by_path("/testcase-data/phandle-tests/consumer-a");
308 if (!np) {
309 pr_info("No testcase data in device tree; not running tests\n");
310 return 0;
312 of_node_put(np);
314 pr_info("start of selftest - you will see error messages\n");
315 of_selftest_parse_phandle_with_args();
316 of_selftest_property_match_string();
317 of_selftest_parse_interrupts();
318 of_selftest_parse_interrupts_extended();
319 pr_info("end of selftest - %i passed, %i failed\n",
320 selftest_results.passed, selftest_results.failed);
321 return 0;
323 late_initcall(of_selftest);