Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Self tests for device tree subsystem |
| 3 | */ |
| 4 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 5 | #define pr_fmt(fmt) "### dt-test ### " fmt |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 6 | |
| 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> |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 12 | #include <linux/of_irq.h> |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 13 | #include <linux/list.h> |
| 14 | #include <linux/mutex.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/device.h> |
| 17 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 18 | static struct selftest_results { |
| 19 | int passed; |
| 20 | int failed; |
| 21 | } selftest_results; |
| 22 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 23 | #define selftest(result, fmt, ...) { \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 24 | if (!(result)) { \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 25 | selftest_results.failed++; \ |
| 26 | pr_err("FAIL %s():%i " fmt, __func__, __LINE__, ##__VA_ARGS__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 27 | } else { \ |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 28 | selftest_results.passed++; \ |
| 29 | pr_debug("pass %s():%i\n", __func__, __LINE__); \ |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 30 | } \ |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static void __init of_selftest_parse_phandle_with_args(void) |
| 34 | { |
| 35 | struct device_node *np; |
| 36 | struct of_phandle_args args; |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 37 | int i, rc; |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 38 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 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; |
| 43 | } |
| 44 | |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 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); |
| 47 | |
Grant Likely | f7f951c | 2013-02-12 17:41:22 +0000 | [diff] [blame] | 48 | for (i = 0; i < 8; i++) { |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 49 | bool passed = true; |
| 50 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 51 | "#phandle-cells", i, &args); |
| 52 | |
| 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: |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 92 | passed &= (rc == -ENOENT); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 93 | break; |
| 94 | default: |
| 95 | passed = false; |
| 96 | } |
| 97 | |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 98 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 99 | i, args.np->full_name, rc); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /* Check for missing list property */ |
| 103 | rc = of_parse_phandle_with_args(np, "phandle-list-missing", |
| 104 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 105 | selftest(rc == -ENOENT, "expected:%i got:%i\n", -ENOENT, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 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); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 109 | |
| 110 | /* Check for missing cells property */ |
| 111 | rc = of_parse_phandle_with_args(np, "phandle-list", |
| 112 | "#phandle-cells-missing", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 113 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 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); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 117 | |
| 118 | /* Check for bad phandle in list */ |
| 119 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-phandle", |
| 120 | "#phandle-cells", 0, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 121 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 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); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 125 | |
| 126 | /* Check for incorrectly formed argument list */ |
| 127 | rc = of_parse_phandle_with_args(np, "phandle-list-bad-args", |
| 128 | "#phandle-cells", 1, &args); |
Grant Likely | cabb7d5 | 2013-02-12 21:19:37 +0000 | [diff] [blame] | 129 | selftest(rc == -EINVAL, "expected:%i got:%i\n", -EINVAL, rc); |
Grant Likely | bd69f73 | 2013-02-10 22:57:21 +0000 | [diff] [blame] | 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); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 135 | static void __init of_selftest_property_match_string(void) |
| 136 | { |
| 137 | struct device_node *np; |
| 138 | int rc; |
| 139 | |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 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; |
| 144 | } |
| 145 | |
| 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); |
| 160 | } |
| 161 | |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 162 | static void __init of_selftest_parse_interrupts(void) |
| 163 | { |
| 164 | struct device_node *np; |
| 165 | struct of_phandle_args args; |
| 166 | int i, rc; |
| 167 | |
| 168 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts0"); |
| 169 | if (!np) { |
| 170 | pr_err("missing testcase data\n"); |
| 171 | return; |
| 172 | } |
| 173 | |
| 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); |
| 178 | |
| 179 | passed &= !rc; |
| 180 | passed &= (args.args_count == 1); |
| 181 | passed &= (args.args[0] == (i + 1)); |
| 182 | |
| 183 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 184 | i, args.np->full_name, rc); |
| 185 | } |
| 186 | of_node_put(np); |
| 187 | |
| 188 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts1"); |
| 189 | if (!np) { |
| 190 | pr_err("missing testcase data\n"); |
| 191 | return; |
| 192 | } |
| 193 | |
| 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); |
| 198 | |
| 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; |
| 227 | } |
| 228 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 229 | i, args.np->full_name, rc); |
| 230 | } |
| 231 | of_node_put(np); |
| 232 | } |
| 233 | |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 234 | static void __init of_selftest_parse_interrupts_extended(void) |
| 235 | { |
| 236 | struct device_node *np; |
| 237 | struct of_phandle_args args; |
| 238 | int i, rc; |
| 239 | |
| 240 | np = of_find_node_by_path("/testcase-data/interrupts/interrupts-extended0"); |
| 241 | if (!np) { |
| 242 | pr_err("missing testcase data\n"); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | for (i = 0; i < 7; i++) { |
| 247 | bool passed = true; |
| 248 | rc = of_irq_parse_one(np, i, &args); |
| 249 | |
| 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; |
| 295 | } |
| 296 | |
| 297 | selftest(passed, "index %i - data error on node %s rc=%i\n", |
| 298 | i, args.np->full_name, rc); |
| 299 | } |
| 300 | of_node_put(np); |
| 301 | } |
| 302 | |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 303 | static int __init of_selftest(void) |
| 304 | { |
| 305 | struct device_node *np; |
| 306 | |
| 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; |
| 311 | } |
| 312 | of_node_put(np); |
| 313 | |
| 314 | pr_info("start of selftest - you will see error messages\n"); |
| 315 | of_selftest_parse_phandle_with_args(); |
Grant Likely | 7aff0fe | 2011-12-12 09:25:58 -0700 | [diff] [blame] | 316 | of_selftest_property_match_string(); |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 317 | of_selftest_parse_interrupts(); |
Grant Likely | 79d9701 | 2013-09-19 16:47:37 -0500 | [diff] [blame] | 318 | of_selftest_parse_interrupts_extended(); |
Grant Likely | a9f10ca | 2013-10-11 22:04:23 +0100 | [diff] [blame] | 319 | pr_info("end of selftest - %i passed, %i failed\n", |
| 320 | selftest_results.passed, selftest_results.failed); |
Grant Likely | 53a4209 | 2011-12-12 09:25:57 -0700 | [diff] [blame] | 321 | return 0; |
| 322 | } |
| 323 | late_initcall(of_selftest); |