]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
2e7d35d2 SG |
2 | /* |
3 | * Copyright (c) 2013 Google, Inc | |
2e7d35d2 SG |
4 | */ |
5 | ||
2e7d35d2 SG |
6 | #include <dm.h> |
7 | #include <errno.h> | |
8 | #include <fdtdec.h> | |
f7ae49fc | 9 | #include <log.h> |
2e7d35d2 | 10 | #include <malloc.h> |
401d1c4f | 11 | #include <asm/global_data.h> |
2e7d35d2 SG |
12 | #include <asm/io.h> |
13 | #include <dm/test.h> | |
2f3d6a42 | 14 | #include <dm/read.h> |
2e7d35d2 | 15 | #include <dm/root.h> |
98561572 | 16 | #include <dm/device-internal.h> |
61b29b82 | 17 | #include <dm/devres.h> |
2e7d35d2 SG |
18 | #include <dm/uclass-internal.h> |
19 | #include <dm/util.h> | |
2ea4d0db | 20 | #include <dm/of_access.h> |
269fa846 | 21 | #include <linux/ioport.h> |
0e1fad43 | 22 | #include <test/test.h> |
e721b882 | 23 | #include <test/ut.h> |
2e7d35d2 SG |
24 | |
25 | DECLARE_GLOBAL_DATA_PTR; | |
26 | ||
98561572 SG |
27 | struct dm_testprobe_pdata { |
28 | int probe_err; | |
29 | }; | |
30 | ||
31 | static int testprobe_drv_probe(struct udevice *dev) | |
32 | { | |
c69cda25 | 33 | struct dm_testprobe_pdata *pdata = dev_get_plat(dev); |
98561572 SG |
34 | |
35 | return pdata->probe_err; | |
36 | } | |
37 | ||
38 | static const struct udevice_id testprobe_ids[] = { | |
39 | { .compatible = "denx,u-boot-probe-test" }, | |
40 | { } | |
41 | }; | |
42 | ||
43 | U_BOOT_DRIVER(testprobe_drv) = { | |
44 | .name = "testprobe_drv", | |
45 | .of_match = testprobe_ids, | |
46 | .id = UCLASS_TEST_PROBE, | |
47 | .probe = testprobe_drv_probe, | |
caa4daa2 | 48 | .plat_auto = sizeof(struct dm_testprobe_pdata), |
98561572 SG |
49 | }; |
50 | ||
51 | UCLASS_DRIVER(testprobe) = { | |
52 | .name = "testprobe", | |
53 | .id = UCLASS_TEST_PROBE, | |
54 | .flags = DM_UC_FLAG_SEQ_ALIAS, | |
55 | }; | |
56 | ||
dc12ebbb SG |
57 | struct dm_testdevres_pdata { |
58 | void *ptr; | |
59 | }; | |
60 | ||
61 | struct dm_testdevres_priv { | |
62 | void *ptr; | |
42a0ce57 | 63 | void *ptr_ofdata; |
dc12ebbb SG |
64 | }; |
65 | ||
66 | static int testdevres_drv_bind(struct udevice *dev) | |
67 | { | |
c69cda25 | 68 | struct dm_testdevres_pdata *pdata = dev_get_plat(dev); |
dc12ebbb SG |
69 | |
70 | pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0); | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
d1998a9f | 75 | static int testdevres_drv_of_to_plat(struct udevice *dev) |
42a0ce57 SG |
76 | { |
77 | struct dm_testdevres_priv *priv = dev_get_priv(dev); | |
78 | ||
79 | priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0); | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
dc12ebbb SG |
84 | static int testdevres_drv_probe(struct udevice *dev) |
85 | { | |
86 | struct dm_testdevres_priv *priv = dev_get_priv(dev); | |
87 | ||
88 | priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0); | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | static const struct udevice_id testdevres_ids[] = { | |
94 | { .compatible = "denx,u-boot-devres-test" }, | |
95 | { } | |
96 | }; | |
97 | ||
98 | U_BOOT_DRIVER(testdevres_drv) = { | |
99 | .name = "testdevres_drv", | |
100 | .of_match = testdevres_ids, | |
101 | .id = UCLASS_TEST_DEVRES, | |
102 | .bind = testdevres_drv_bind, | |
d1998a9f | 103 | .of_to_plat = testdevres_drv_of_to_plat, |
dc12ebbb | 104 | .probe = testdevres_drv_probe, |
caa4daa2 | 105 | .plat_auto = sizeof(struct dm_testdevres_pdata), |
41575d8e | 106 | .priv_auto = sizeof(struct dm_testdevres_priv), |
dc12ebbb SG |
107 | }; |
108 | ||
109 | UCLASS_DRIVER(testdevres) = { | |
110 | .name = "testdevres", | |
111 | .id = UCLASS_TEST_DEVRES, | |
112 | .flags = DM_UC_FLAG_SEQ_ALIAS, | |
113 | }; | |
114 | ||
e721b882 | 115 | int dm_check_devices(struct unit_test_state *uts, int num_devices) |
2e7d35d2 | 116 | { |
54c5d08a | 117 | struct udevice *dev; |
2e7d35d2 SG |
118 | int ret; |
119 | int i; | |
120 | ||
2e7d35d2 SG |
121 | /* |
122 | * Now check that the ping adds are what we expect. This is using the | |
123 | * ping-add property in each node. | |
124 | */ | |
1ca7e206 | 125 | for (i = 0; i < num_devices; i++) { |
2e7d35d2 SG |
126 | uint32_t base; |
127 | ||
128 | ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev); | |
129 | ut_assert(!ret); | |
130 | ||
131 | /* | |
eb9ef5fe | 132 | * Get the 'ping-expect' property, which tells us what the |
caa4daa2 | 133 | * ping add should be. We don't use the plat because we |
eb9ef5fe SG |
134 | * want to test the code that sets that up |
135 | * (testfdt_drv_probe()). | |
2e7d35d2 | 136 | */ |
ed48da33 MV |
137 | base = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
138 | "ping-expect", -1); | |
2e7d35d2 | 139 | debug("dev=%d, base=%d: %s\n", i, base, |
e160f7d4 | 140 | fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL)); |
2e7d35d2 | 141 | |
e721b882 | 142 | ut_assert(!dm_check_operations(uts, dev, base, |
2e7d35d2 SG |
143 | dev_get_priv(dev))); |
144 | } | |
145 | ||
146 | return 0; | |
147 | } | |
1ca7e206 SG |
148 | |
149 | /* Test that FDT-based binding works correctly */ | |
e721b882 | 150 | static int dm_test_fdt(struct unit_test_state *uts) |
1ca7e206 | 151 | { |
88e6a60e | 152 | const int num_devices = 9; |
1ca7e206 SG |
153 | struct udevice *dev; |
154 | struct uclass *uc; | |
155 | int ret; | |
156 | int i; | |
157 | ||
8ee05b5f | 158 | ret = dm_extended_scan(false); |
1ca7e206 SG |
159 | ut_assert(!ret); |
160 | ||
161 | ret = uclass_get(UCLASS_TEST_FDT, &uc); | |
162 | ut_assert(!ret); | |
163 | ||
164 | /* These are num_devices compatible root-level device tree nodes */ | |
165 | ut_asserteq(num_devices, list_count_items(&uc->dev_head)); | |
166 | ||
f8a85449 | 167 | /* Each should have platform data but no private data */ |
1ca7e206 SG |
168 | for (i = 0; i < num_devices; i++) { |
169 | ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev); | |
170 | ut_assert(!ret); | |
171 | ut_assert(!dev_get_priv(dev)); | |
0fd3d911 | 172 | ut_assert(dev_get_plat(dev)); |
1ca7e206 SG |
173 | } |
174 | ||
e721b882 | 175 | ut_assertok(dm_check_devices(uts, num_devices)); |
1ca7e206 SG |
176 | |
177 | return 0; | |
178 | } | |
2e7d35d2 | 179 | DM_TEST(dm_test_fdt, 0); |
00606d7e | 180 | |
a93eb577 MS |
181 | static int dm_test_alias_highest_id(struct unit_test_state *uts) |
182 | { | |
183 | int ret; | |
184 | ||
82a3c9ef | 185 | ret = dev_read_alias_highest_id("ethernet"); |
bedb182e | 186 | ut_asserteq(8, ret); |
a93eb577 MS |
187 | |
188 | ret = dev_read_alias_highest_id("gpio"); | |
ff52665d | 189 | ut_asserteq(3, ret); |
a93eb577 MS |
190 | |
191 | ret = dev_read_alias_highest_id("pci"); | |
192 | ut_asserteq(2, ret); | |
193 | ||
194 | ret = dev_read_alias_highest_id("i2c"); | |
195 | ut_asserteq(0, ret); | |
196 | ||
197 | ret = dev_read_alias_highest_id("deadbeef"); | |
198 | ut_asserteq(-1, ret); | |
199 | ||
200 | return 0; | |
201 | } | |
202 | DM_TEST(dm_test_alias_highest_id, 0); | |
203 | ||
e721b882 | 204 | static int dm_test_fdt_pre_reloc(struct unit_test_state *uts) |
00606d7e SG |
205 | { |
206 | struct uclass *uc; | |
207 | int ret; | |
208 | ||
725e4fce | 209 | ret = dm_scan_fdt(true); |
00606d7e SG |
210 | ut_assert(!ret); |
211 | ||
212 | ret = uclass_get(UCLASS_TEST_FDT, &uc); | |
213 | ut_assert(!ret); | |
214 | ||
2786cd74 BM |
215 | /* |
216 | * These are 2 pre-reloc devices: | |
e316fbab | 217 | * one with "bootph-all" property (a-test node), and the other |
2786cd74 BM |
218 | * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node). |
219 | */ | |
220 | ut_asserteq(2, list_count_items(&uc->dev_head)); | |
00606d7e SG |
221 | |
222 | return 0; | |
223 | } | |
224 | DM_TEST(dm_test_fdt_pre_reloc, 0); | |
5a66a8ff SG |
225 | |
226 | /* Test that sequence numbers are allocated properly */ | |
e721b882 | 227 | static int dm_test_fdt_uclass_seq(struct unit_test_state *uts) |
5a66a8ff SG |
228 | { |
229 | struct udevice *dev; | |
230 | ||
231 | /* A few basic santiy tests */ | |
99175919 | 232 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
5a66a8ff | 233 | ut_asserteq_str("b-test", dev->name); |
981426e3 | 234 | ut_asserteq(3, dev_seq(dev)); |
5a66a8ff | 235 | |
99175919 | 236 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev)); |
5a66a8ff | 237 | ut_asserteq_str("a-test", dev->name); |
981426e3 | 238 | ut_asserteq(8, dev_seq(dev)); |
5a66a8ff | 239 | |
981426e3 SG |
240 | /* |
241 | * This device has no alias so gets the next value after all available | |
242 | * aliases. The last alias is testfdt12 | |
243 | */ | |
99175919 | 244 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev)); |
981426e3 SG |
245 | ut_asserteq_str("d-test", dev->name); |
246 | ut_asserteq(13, dev_seq(dev)); | |
247 | ||
99175919 | 248 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9, |
981426e3 | 249 | &dev)); |
5a66a8ff SG |
250 | ut_asserteq_ptr(NULL, dev); |
251 | ||
252 | /* Test aliases */ | |
253 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev)); | |
254 | ut_asserteq_str("e-test", dev->name); | |
981426e3 | 255 | ut_asserteq(6, dev_seq(dev)); |
5a66a8ff | 256 | |
1ca7e206 SG |
257 | /* |
258 | * Note that c-test nodes are not probed since it is not a top-level | |
259 | * node | |
260 | */ | |
5a66a8ff SG |
261 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
262 | ut_asserteq_str("b-test", dev->name); | |
981426e3 | 263 | ut_asserteq(3, dev_seq(dev)); |
5a66a8ff SG |
264 | |
265 | /* | |
266 | * d-test wants sequence number 3 also, but it can't have it because | |
267 | * b-test gets it first. | |
268 | */ | |
269 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev)); | |
270 | ut_asserteq_str("d-test", dev->name); | |
981426e3 SG |
271 | ut_asserteq(13, dev_seq(dev)); |
272 | ||
273 | /* g-test gets the next value after f-test */ | |
274 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 15, &dev)); | |
275 | ut_asserteq_str("g-test", dev->name); | |
276 | ut_asserteq(15, dev_seq(dev)); | |
277 | ||
278 | /* And we should still have holes in our sequence numbers */ | |
99175919 | 279 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0, |
981426e3 | 280 | &dev)); |
99175919 | 281 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1, |
981426e3 | 282 | &dev)); |
99175919 | 283 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2, |
981426e3 | 284 | &dev)); |
99175919 | 285 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4, |
981426e3 | 286 | &dev)); |
99175919 | 287 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7, |
981426e3 | 288 | &dev)); |
99175919 | 289 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9, |
981426e3 SG |
290 | &dev)); |
291 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10, | |
99175919 | 292 | &dev)); |
981426e3 | 293 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11, |
99175919 | 294 | &dev)); |
be1a6e94 | 295 | |
5a66a8ff SG |
296 | return 0; |
297 | } | |
e180c2b1 | 298 | DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
f4cdead2 | 299 | |
93f44e8a SG |
300 | /* More tests for sequence numbers */ |
301 | static int dm_test_fdt_uclass_seq_manual(struct unit_test_state *uts) | |
302 | { | |
303 | struct udevice *dev; | |
304 | ||
305 | /* | |
306 | * Since DM_UC_FLAG_NO_AUTO_SEQ is set for this uclass, only testfdtm1 | |
307 | * should get a sequence number assigned | |
308 | */ | |
309 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 0, &dev)); | |
310 | ut_asserteq_str("testfdtm0", dev->name); | |
311 | ut_asserteq(-1, dev_seq(dev)); | |
312 | ||
313 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT_MANUAL, 1, &dev)); | |
314 | ut_asserteq_str("testfdtm1", dev->name); | |
315 | ut_asserteq(1, dev_seq(dev)); | |
316 | ||
317 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 2, &dev)); | |
318 | ut_asserteq_str("testfdtm2", dev->name); | |
319 | ut_asserteq(-1, dev_seq(dev)); | |
320 | ||
321 | return 0; | |
322 | } | |
323 | DM_TEST(dm_test_fdt_uclass_seq_manual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); | |
324 | ||
7f20d1d2 SG |
325 | static int dm_test_fdt_uclass_seq_more(struct unit_test_state *uts) |
326 | { | |
327 | struct udevice *dev; | |
328 | ofnode node; | |
329 | ||
330 | /* Check creating a device with an alias */ | |
331 | node = ofnode_path("/some-bus/c-test@1"); | |
074197aa | 332 | ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test), |
7f20d1d2 SG |
333 | "c-test@1", NULL, node, &dev)); |
334 | ut_asserteq(12, dev_seq(dev)); | |
335 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 12, &dev)); | |
336 | ut_asserteq_str("c-test@1", dev->name); | |
337 | ||
338 | /* | |
339 | * Now bind a device without an alias. It should not get the next | |
340 | * sequence number after all aliases, and existing bound devices. The | |
341 | * last alias is 12, so we have: | |
342 | * | |
343 | * 13 d-test | |
344 | * 14 f-test | |
345 | * 15 g-test | |
346 | * 16 h-test | |
347 | * 17 another-test | |
348 | * 18 chosen-test | |
349 | * | |
350 | * So next available is 19 | |
351 | */ | |
074197aa | 352 | ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test), |
7f20d1d2 SG |
353 | "fred", NULL, ofnode_null(), &dev)); |
354 | ut_asserteq(19, dev_seq(dev)); | |
355 | ||
074197aa | 356 | ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test), |
7f20d1d2 SG |
357 | "fred2", NULL, ofnode_null(), &dev)); |
358 | ut_asserteq(20, dev_seq(dev)); | |
359 | ||
360 | return 0; | |
361 | } | |
362 | DM_TEST(dm_test_fdt_uclass_seq_more, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); | |
363 | ||
f4cdead2 | 364 | /* Test that we can find a device by device tree offset */ |
e721b882 | 365 | static int dm_test_fdt_offset(struct unit_test_state *uts) |
f4cdead2 SG |
366 | { |
367 | const void *blob = gd->fdt_blob; | |
368 | struct udevice *dev; | |
369 | int node; | |
370 | ||
371 | node = fdt_path_offset(blob, "/e-test"); | |
372 | ut_assert(node > 0); | |
373 | ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node, | |
374 | &dev)); | |
375 | ut_asserteq_str("e-test", dev->name); | |
376 | ||
377 | /* This node should not be bound */ | |
378 | node = fdt_path_offset(blob, "/junk"); | |
379 | ut_assert(node > 0); | |
380 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, | |
381 | node, &dev)); | |
382 | ||
383 | /* This is not a top level node so should not be probed */ | |
1ca7e206 | 384 | node = fdt_path_offset(blob, "/some-bus/c-test@5"); |
f4cdead2 SG |
385 | ut_assert(node > 0); |
386 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, | |
387 | node, &dev)); | |
388 | ||
389 | return 0; | |
390 | } | |
86b54ece | 391 | DM_TEST(dm_test_fdt_offset, |
e180c2b1 | 392 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
98561572 SG |
393 | |
394 | /** | |
dfecd631 MS |
395 | * Test various error conditions with uclass_first_device(), |
396 | * uclass_next_device(), and uclass_probe_all() | |
98561572 | 397 | */ |
dfecd631 | 398 | static int dm_test_first_next_device_probeall(struct unit_test_state *uts) |
98561572 SG |
399 | { |
400 | struct dm_testprobe_pdata *pdata; | |
401 | struct udevice *dev, *parent = NULL; | |
402 | int count; | |
403 | int ret; | |
404 | ||
405 | /* There should be 4 devices */ | |
f2195475 | 406 | for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; |
98561572 | 407 | dev; |
f2195475 | 408 | uclass_next_device(&dev)) { |
98561572 SG |
409 | count++; |
410 | parent = dev_get_parent(dev); | |
411 | } | |
98561572 SG |
412 | ut_asserteq(4, count); |
413 | ||
414 | /* Remove them and try again, with an error on the second one */ | |
415 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev)); | |
c69cda25 | 416 | pdata = dev_get_plat(dev); |
98561572 SG |
417 | pdata->probe_err = -ENOMEM; |
418 | device_remove(parent, DM_REMOVE_NORMAL); | |
f2195475 MS |
419 | for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev), |
420 | count = 0; | |
421 | dev; | |
422 | ret = uclass_next_device_check(&dev)) { | |
423 | if (!ret) | |
424 | count++; | |
425 | else | |
426 | ut_asserteq(-ENOMEM, ret); | |
427 | parent = dev_get_parent(dev); | |
428 | } | |
429 | ut_asserteq(3, count); | |
98561572 SG |
430 | |
431 | /* Now an error on the first one */ | |
432 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev)); | |
c69cda25 | 433 | pdata = dev_get_plat(dev); |
98561572 SG |
434 | pdata->probe_err = -ENOENT; |
435 | device_remove(parent, DM_REMOVE_NORMAL); | |
f2195475 MS |
436 | for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; |
437 | dev; | |
438 | uclass_next_device(&dev)) { | |
439 | count++; | |
440 | parent = dev_get_parent(dev); | |
441 | } | |
442 | ut_asserteq(2, count); | |
98561572 | 443 | |
dfecd631 MS |
444 | /* Now that broken devices are set up test probe_all */ |
445 | device_remove(parent, DM_REMOVE_NORMAL); | |
446 | /* There are broken devices so an error should be returned */ | |
447 | ut_assert(uclass_probe_all(UCLASS_TEST_PROBE) < 0); | |
448 | /* but non-error device should be probed nonetheless */ | |
449 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 2, &dev)); | |
450 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); | |
451 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 3, &dev)); | |
452 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); | |
453 | ||
98561572 SG |
454 | return 0; |
455 | } | |
dfecd631 MS |
456 | DM_TEST(dm_test_first_next_device_probeall, |
457 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); | |
95ce385a | 458 | |
3cf0fba4 SG |
459 | /* Test iteration through devices in a uclass */ |
460 | static int dm_test_uclass_foreach(struct unit_test_state *uts) | |
461 | { | |
462 | struct udevice *dev; | |
463 | struct uclass *uc; | |
464 | int count; | |
465 | ||
466 | count = 0; | |
467 | uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc) | |
468 | count++; | |
88e6a60e | 469 | ut_asserteq(9, count); |
3cf0fba4 SG |
470 | |
471 | count = 0; | |
472 | uclass_foreach_dev(dev, uc) | |
473 | count++; | |
88e6a60e | 474 | ut_asserteq(9, count); |
3cf0fba4 SG |
475 | |
476 | return 0; | |
477 | } | |
e180c2b1 | 478 | DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
3cf0fba4 | 479 | |
95ce385a SG |
480 | /** |
481 | * check_devices() - Check return values and pointers | |
482 | * | |
483 | * This runs through a full sequence of uclass_first_device_check()... | |
484 | * uclass_next_device_check() checking that the return values and devices | |
485 | * are correct. | |
486 | * | |
487 | * @uts: Test state | |
488 | * @devlist: List of expected devices | |
489 | * @mask: Indicates which devices should return an error. Device n should | |
490 | * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if | |
491 | * bit n is clear. | |
492 | */ | |
493 | static int check_devices(struct unit_test_state *uts, | |
494 | struct udevice *devlist[], int mask) | |
495 | { | |
496 | int expected_ret; | |
497 | struct udevice *dev; | |
498 | int i; | |
499 | ||
500 | expected_ret = (mask & 1) ? -ENOENT : 0; | |
501 | mask >>= 1; | |
502 | ut_asserteq(expected_ret, | |
503 | uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); | |
504 | for (i = 0; i < 4; i++) { | |
505 | ut_asserteq_ptr(devlist[i], dev); | |
506 | expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0; | |
507 | mask >>= 1; | |
508 | ut_asserteq(expected_ret, uclass_next_device_check(&dev)); | |
509 | } | |
510 | ut_asserteq_ptr(NULL, dev); | |
511 | ||
512 | return 0; | |
513 | } | |
514 | ||
515 | /* Test uclass_first_device_check() and uclass_next_device_check() */ | |
516 | static int dm_test_first_next_ok_device(struct unit_test_state *uts) | |
517 | { | |
518 | struct dm_testprobe_pdata *pdata; | |
519 | struct udevice *dev, *parent = NULL, *devlist[4]; | |
520 | int count; | |
521 | int ret; | |
522 | ||
523 | /* There should be 4 devices */ | |
524 | count = 0; | |
525 | for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev); | |
526 | dev; | |
527 | ret = uclass_next_device_check(&dev)) { | |
528 | ut_assertok(ret); | |
529 | devlist[count++] = dev; | |
530 | parent = dev_get_parent(dev); | |
531 | } | |
532 | ut_asserteq(4, count); | |
533 | ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); | |
534 | ut_assertok(check_devices(uts, devlist, 0)); | |
535 | ||
536 | /* Remove them and try again, with an error on the second one */ | |
c69cda25 | 537 | pdata = dev_get_plat(devlist[1]); |
95ce385a SG |
538 | pdata->probe_err = -ENOENT - 1; |
539 | device_remove(parent, DM_REMOVE_NORMAL); | |
540 | ut_assertok(check_devices(uts, devlist, 1 << 1)); | |
541 | ||
542 | /* Now an error on the first one */ | |
c69cda25 | 543 | pdata = dev_get_plat(devlist[0]); |
95ce385a SG |
544 | pdata->probe_err = -ENOENT - 0; |
545 | device_remove(parent, DM_REMOVE_NORMAL); | |
546 | ut_assertok(check_devices(uts, devlist, 3 << 0)); | |
547 | ||
548 | /* Now errors on all */ | |
c69cda25 | 549 | pdata = dev_get_plat(devlist[2]); |
95ce385a | 550 | pdata->probe_err = -ENOENT - 2; |
c69cda25 | 551 | pdata = dev_get_plat(devlist[3]); |
95ce385a SG |
552 | pdata->probe_err = -ENOENT - 3; |
553 | device_remove(parent, DM_REMOVE_NORMAL); | |
554 | ut_assertok(check_devices(uts, devlist, 0xf << 0)); | |
555 | ||
556 | return 0; | |
557 | } | |
e180c2b1 | 558 | DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
e8d52918 MS |
559 | |
560 | static const struct udevice_id fdt_dummy_ids[] = { | |
561 | { .compatible = "denx,u-boot-fdt-dummy", }, | |
562 | { } | |
563 | }; | |
564 | ||
565 | UCLASS_DRIVER(fdt_dummy) = { | |
507cef3d | 566 | .name = "fdt-dummy", |
e8d52918 MS |
567 | .id = UCLASS_TEST_DUMMY, |
568 | .flags = DM_UC_FLAG_SEQ_ALIAS, | |
569 | }; | |
570 | ||
571 | U_BOOT_DRIVER(fdt_dummy_drv) = { | |
572 | .name = "fdt_dummy_drv", | |
573 | .of_match = fdt_dummy_ids, | |
574 | .id = UCLASS_TEST_DUMMY, | |
575 | }; | |
576 | ||
577 | static int dm_test_fdt_translation(struct unit_test_state *uts) | |
578 | { | |
579 | struct udevice *dev; | |
641067fb | 580 | fdt32_t dma_addr[2]; |
e8d52918 MS |
581 | |
582 | /* Some simple translations */ | |
99175919 | 583 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
e8d52918 MS |
584 | ut_asserteq_str("dev@0,0", dev->name); |
585 | ut_asserteq(0x8000, dev_read_addr(dev)); | |
586 | ||
99175919 | 587 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev)); |
e8d52918 MS |
588 | ut_asserteq_str("dev@1,100", dev->name); |
589 | ut_asserteq(0x9000, dev_read_addr(dev)); | |
590 | ||
99175919 | 591 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev)); |
e8d52918 MS |
592 | ut_asserteq_str("dev@2,200", dev->name); |
593 | ut_asserteq(0xA000, dev_read_addr(dev)); | |
594 | ||
7943ae24 | 595 | /* No translation for buses with #size-cells == 0 */ |
99175919 | 596 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev)); |
e8d52918 MS |
597 | ut_asserteq_str("dev@42", dev->name); |
598 | ut_asserteq(0x42, dev_read_addr(dev)); | |
599 | ||
641067fb | 600 | /* dma address translation */ |
99175919 | 601 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
641067fb FD |
602 | dma_addr[0] = cpu_to_be32(0); |
603 | dma_addr[1] = cpu_to_be32(0); | |
604 | ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr)); | |
605 | ||
99175919 | 606 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev)); |
641067fb FD |
607 | dma_addr[0] = cpu_to_be32(1); |
608 | dma_addr[1] = cpu_to_be32(0x100); | |
609 | ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr)); | |
610 | ||
e8d52918 MS |
611 | return 0; |
612 | } | |
e180c2b1 | 613 | DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
30a90f56 | 614 | |
6e64830f OP |
615 | static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts) |
616 | { | |
617 | struct udevice *gpio, *dev; | |
618 | void *ptr; | |
37937aba | 619 | void *paddr; |
6e64830f OP |
620 | |
621 | /* Test for missing reg property */ | |
622 | ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio)); | |
623 | ut_assertnull(devfdt_get_addr_ptr(gpio)); | |
624 | ||
99175919 | 625 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
6e64830f | 626 | ptr = devfdt_get_addr_ptr(dev); |
37937aba JJ |
627 | |
628 | paddr = map_sysmem(0x8000, 0); | |
629 | ut_asserteq_ptr(paddr, ptr); | |
6e64830f OP |
630 | |
631 | return 0; | |
632 | } | |
633 | DM_TEST(dm_test_fdt_get_addr_ptr_flat, | |
634 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); | |
635 | ||
30a90f56 ÁFR |
636 | static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts) |
637 | { | |
638 | struct udevice *dev; | |
639 | fdt_addr_t addr; | |
640 | void *paddr; | |
641 | ||
99175919 | 642 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
30a90f56 ÁFR |
643 | |
644 | addr = devfdt_get_addr(dev); | |
645 | ut_asserteq(0x8000, addr); | |
646 | ||
647 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
648 | ut_assertnonnull(paddr); | |
649 | ut_asserteq_ptr(paddr, devfdt_remap_addr(dev)); | |
650 | ||
651 | return 0; | |
652 | } | |
653 | DM_TEST(dm_test_fdt_remap_addr_flat, | |
e180c2b1 | 654 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
30a90f56 | 655 | |
79598820 ÁFR |
656 | static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts) |
657 | { | |
658 | struct udevice *dev; | |
659 | fdt_addr_t addr; | |
f5b90479 | 660 | fdt_size_t size; |
79598820 ÁFR |
661 | void *paddr; |
662 | ||
99175919 | 663 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
79598820 | 664 | |
f5b90479 | 665 | addr = devfdt_get_addr_size_index(dev, 0, &size); |
79598820 | 666 | ut_asserteq(0x8000, addr); |
f5b90479 | 667 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
668 | |
669 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
670 | ut_assertnonnull(paddr); | |
671 | ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0)); | |
672 | ||
673 | return 0; | |
674 | } | |
675 | DM_TEST(dm_test_fdt_remap_addr_index_flat, | |
e180c2b1 | 676 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
79598820 ÁFR |
677 | |
678 | static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts) | |
679 | { | |
680 | struct udevice *dev; | |
681 | fdt_addr_t addr; | |
f5b90479 | 682 | fdt_size_t size; |
79598820 ÁFR |
683 | void *paddr; |
684 | ||
99175919 | 685 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
79598820 | 686 | |
f5b90479 | 687 | addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size); |
79598820 | 688 | ut_asserteq(0x8000, addr); |
f5b90479 | 689 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
690 | |
691 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
692 | ut_assertnonnull(paddr); | |
693 | ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0")); | |
694 | ||
695 | return 0; | |
696 | } | |
697 | DM_TEST(dm_test_fdt_remap_addr_name_flat, | |
e180c2b1 | 698 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
79598820 | 699 | |
30a90f56 ÁFR |
700 | static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts) |
701 | { | |
702 | struct udevice *dev; | |
703 | fdt_addr_t addr; | |
704 | void *paddr; | |
705 | ||
99175919 | 706 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
30a90f56 ÁFR |
707 | |
708 | addr = dev_read_addr(dev); | |
709 | ut_asserteq(0x8000, addr); | |
710 | ||
711 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
712 | ut_assertnonnull(paddr); | |
713 | ut_asserteq_ptr(paddr, dev_remap_addr(dev)); | |
714 | ||
715 | return 0; | |
716 | } | |
717 | DM_TEST(dm_test_fdt_remap_addr_live, | |
e180c2b1 | 718 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
2ea4d0db | 719 | |
79598820 ÁFR |
720 | static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts) |
721 | { | |
722 | struct udevice *dev; | |
723 | fdt_addr_t addr; | |
f5b90479 | 724 | fdt_size_t size; |
79598820 ÁFR |
725 | void *paddr; |
726 | ||
99175919 | 727 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
79598820 | 728 | |
f5b90479 | 729 | addr = dev_read_addr_size_index(dev, 0, &size); |
79598820 | 730 | ut_asserteq(0x8000, addr); |
f5b90479 | 731 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
732 | |
733 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
734 | ut_assertnonnull(paddr); | |
735 | ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0)); | |
736 | ||
737 | return 0; | |
738 | } | |
739 | DM_TEST(dm_test_fdt_remap_addr_index_live, | |
e180c2b1 | 740 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
79598820 ÁFR |
741 | |
742 | static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts) | |
743 | { | |
744 | struct udevice *dev; | |
745 | fdt_addr_t addr; | |
f5b90479 | 746 | fdt_size_t size; |
79598820 ÁFR |
747 | void *paddr; |
748 | ||
99175919 | 749 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev)); |
79598820 | 750 | |
f5b90479 | 751 | addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size); |
79598820 | 752 | ut_asserteq(0x8000, addr); |
f5b90479 | 753 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
754 | |
755 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
756 | ut_assertnonnull(paddr); | |
757 | ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0")); | |
758 | ||
759 | return 0; | |
760 | } | |
761 | DM_TEST(dm_test_fdt_remap_addr_name_live, | |
e180c2b1 | 762 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
79598820 | 763 | |
172942a4 MS |
764 | static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts) |
765 | { | |
766 | ofnode node; | |
767 | ||
768 | if (!of_live_active()) { | |
769 | printf("Live tree not active; ignore test\n"); | |
770 | return 0; | |
771 | } | |
772 | ||
773 | node = ofnode_path("/usb@2"); | |
774 | ||
775 | /* Test enabling devices */ | |
776 | ||
777 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
778 | dev_enable_by_path("/usb@2"); | |
779 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
780 | ||
781 | /* Test disabling devices */ | |
782 | ||
783 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
784 | dev_disable_by_path("/usb@2"); | |
785 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
786 | ||
787 | return 0; | |
788 | } | |
e180c2b1 SG |
789 | DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA | |
790 | UT_TESTF_SCAN_FDT); | |
d0b4f68d SG |
791 | |
792 | /* Test a few uclass phandle functions */ | |
793 | static int dm_test_fdt_phandle(struct unit_test_state *uts) | |
794 | { | |
795 | struct udevice *back, *dev, *dev2; | |
796 | ||
797 | ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back)); | |
331caeaf | 798 | ut_assertnonnull(back); |
d0b4f68d SG |
799 | ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR, |
800 | back, "missing", &dev)); | |
801 | ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back, | |
802 | "power-supply", &dev)); | |
331caeaf | 803 | ut_assertnonnull(dev); |
d0b4f68d SG |
804 | ut_asserteq(0, device_active(dev)); |
805 | ut_asserteq_str("ldo1", dev->name); | |
806 | ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back, | |
807 | "power-supply", &dev2)); | |
808 | ut_asserteq_ptr(dev, dev2); | |
809 | ||
810 | return 0; | |
811 | } | |
e180c2b1 | 812 | DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
3abe1115 SG |
813 | |
814 | /* Test device_find_first_child_by_uclass() */ | |
815 | static int dm_test_first_child(struct unit_test_state *uts) | |
816 | { | |
817 | struct udevice *i2c, *dev, *dev2; | |
818 | ||
819 | ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c)); | |
820 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev)); | |
821 | ut_asserteq_str("rtc@43", dev->name); | |
822 | ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2)); | |
823 | ut_asserteq_ptr(dev, dev2); | |
824 | ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2)); | |
825 | ut_asserteq_str("rtc@61", dev2->name); | |
826 | ||
827 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM, | |
828 | &dev)); | |
829 | ut_asserteq_str("eeprom@2c", dev->name); | |
830 | ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2)); | |
831 | ut_asserteq_ptr(dev, dev2); | |
832 | ||
833 | ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c, | |
834 | UCLASS_VIDEO, &dev)); | |
835 | ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev)); | |
836 | ||
837 | return 0; | |
838 | } | |
e180c2b1 | 839 | DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
a1b17e4f SG |
840 | |
841 | /* Test integer functions in dm_read_...() */ | |
842 | static int dm_test_read_int(struct unit_test_state *uts) | |
843 | { | |
844 | struct udevice *dev; | |
b471bdc4 SH |
845 | u8 val8; |
846 | u16 val16; | |
a1b17e4f SG |
847 | u32 val32; |
848 | s32 sval; | |
849 | uint val; | |
70573c6c | 850 | u64 val64; |
a1b17e4f SG |
851 | |
852 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
853 | ut_asserteq_str("a-test", dev->name); | |
b471bdc4 SH |
854 | |
855 | ut_assertok(dev_read_u8(dev, "int8-value", &val8)); | |
856 | ut_asserteq(0x12, val8); | |
857 | ||
858 | ut_asserteq(-EINVAL, dev_read_u8(dev, "missing", &val8)); | |
859 | ut_asserteq(6, dev_read_u8_default(dev, "missing", 6)); | |
860 | ||
861 | ut_asserteq(0x12, dev_read_u8_default(dev, "int8-value", 6)); | |
862 | ||
863 | ut_assertok(dev_read_u16(dev, "int16-value", &val16)); | |
864 | ut_asserteq(0x1234, val16); | |
865 | ||
866 | ut_asserteq(-EINVAL, dev_read_u16(dev, "missing", &val16)); | |
867 | ut_asserteq(6, dev_read_u16_default(dev, "missing", 6)); | |
868 | ||
869 | ut_asserteq(0x1234, dev_read_u16_default(dev, "int16-value", 6)); | |
870 | ||
a1b17e4f SG |
871 | ut_assertok(dev_read_u32(dev, "int-value", &val32)); |
872 | ut_asserteq(1234, val32); | |
873 | ||
874 | ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32)); | |
875 | ut_asserteq(6, dev_read_u32_default(dev, "missing", 6)); | |
876 | ||
877 | ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6)); | |
878 | ut_asserteq(1234, val32); | |
879 | ||
880 | ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval)); | |
881 | ut_asserteq(6, dev_read_s32_default(dev, "missing", 6)); | |
882 | ||
883 | ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6)); | |
884 | ut_assertok(dev_read_s32(dev, "uint-value", &sval)); | |
885 | ut_asserteq(-1234, sval); | |
886 | ||
887 | val = 0; | |
888 | ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val)); | |
889 | ut_assertok(dev_read_u32u(dev, "uint-value", &val)); | |
890 | ut_asserteq(-1234, val); | |
891 | ||
70573c6c DB |
892 | ut_assertok(dev_read_u64(dev, "int64-value", &val64)); |
893 | ut_asserteq_64(0x1111222233334444, val64); | |
894 | ||
895 | ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64)); | |
896 | ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6)); | |
897 | ||
898 | ut_asserteq_64(0x1111222233334444, | |
899 | dev_read_u64_default(dev, "int64-value", 6)); | |
900 | ||
a1b17e4f SG |
901 | return 0; |
902 | } | |
e180c2b1 | 903 | DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
50162348 | 904 | |
4bb7075c DB |
905 | static int dm_test_read_int_index(struct unit_test_state *uts) |
906 | { | |
907 | struct udevice *dev; | |
908 | u32 val32; | |
909 | ||
910 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
911 | ut_asserteq_str("a-test", dev->name); | |
912 | ||
913 | ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32)); | |
914 | ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19)); | |
915 | ||
916 | ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32)); | |
917 | ut_asserteq(5678, val32); | |
918 | ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32)); | |
919 | ut_asserteq(9123, val32); | |
920 | ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32)); | |
921 | ut_asserteq(4567, val32); | |
922 | ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3, | |
923 | &val32)); | |
924 | ||
925 | ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2)); | |
926 | ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2)); | |
927 | ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2)); | |
928 | ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2)); | |
929 | ||
930 | return 0; | |
931 | } | |
e180c2b1 | 932 | DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
4bb7075c | 933 | |
cc72f3e0 PD |
934 | static int dm_test_read_phandle(struct unit_test_state *uts) |
935 | { | |
936 | struct udevice *dev; | |
937 | struct ofnode_phandle_args args; | |
938 | int ret; | |
939 | const char prop[] = "test-gpios"; | |
940 | const char cell[] = "#gpio-cells"; | |
941 | const char prop2[] = "phandle-value"; | |
942 | ||
943 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
944 | ut_asserteq_str("a-test", dev->name); | |
945 | ||
946 | /* Test dev_count_phandle_with_args with cell name */ | |
947 | ret = dev_count_phandle_with_args(dev, "missing", cell, 0); | |
948 | ut_asserteq(-ENOENT, ret); | |
949 | ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0); | |
950 | ut_asserteq(-EINVAL, ret); | |
951 | ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0)); | |
952 | ||
953 | /* Test dev_read_phandle_with_args with cell name */ | |
954 | ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args); | |
955 | ut_asserteq(-ENOENT, ret); | |
956 | ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args); | |
957 | ut_asserteq(-EINVAL, ret); | |
958 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args)); | |
959 | ut_asserteq(1, args.args_count); | |
960 | ut_asserteq(1, args.args[0]); | |
961 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args)); | |
962 | ut_asserteq(1, args.args_count); | |
963 | ut_asserteq(4, args.args[0]); | |
964 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args)); | |
965 | ut_asserteq(5, args.args_count); | |
966 | ut_asserteq(5, args.args[0]); | |
967 | ut_asserteq(1, args.args[4]); | |
968 | ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args); | |
969 | ut_asserteq(-ENOENT, ret); | |
970 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args)); | |
971 | ut_asserteq(1, args.args_count); | |
972 | ut_asserteq(12, args.args[0]); | |
973 | ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args); | |
974 | ut_asserteq(-ENOENT, ret); | |
975 | ||
976 | /* Test dev_count_phandle_with_args with cell count */ | |
977 | ret = dev_count_phandle_with_args(dev, "missing", NULL, 2); | |
978 | ut_asserteq(-ENOENT, ret); | |
979 | ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1)); | |
980 | ||
981 | /* Test dev_read_phandle_with_args with cell count */ | |
982 | ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args)); | |
983 | ut_asserteq(1, ofnode_valid(args.node)); | |
984 | ut_asserteq(1, args.args_count); | |
985 | ut_asserteq(10, args.args[0]); | |
986 | ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args); | |
987 | ut_asserteq(-EINVAL, ret); | |
988 | ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args)); | |
989 | ut_asserteq(1, ofnode_valid(args.node)); | |
990 | ut_asserteq(1, args.args_count); | |
991 | ut_asserteq(30, args.args[0]); | |
992 | ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args); | |
993 | ut_asserteq(-ENOENT, ret); | |
994 | ||
995 | return 0; | |
996 | } | |
997 | DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); | |
998 | ||
50162348 SG |
999 | /* Test iteration through devices by drvdata */ |
1000 | static int dm_test_uclass_drvdata(struct unit_test_state *uts) | |
1001 | { | |
1002 | struct udevice *dev; | |
1003 | ||
1004 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, | |
1005 | DM_TEST_TYPE_FIRST, &dev)); | |
1006 | ut_asserteq_str("a-test", dev->name); | |
1007 | ||
1008 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, | |
1009 | DM_TEST_TYPE_SECOND, &dev)); | |
1010 | ut_asserteq_str("d-test", dev->name); | |
1011 | ||
1012 | ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT, | |
1013 | DM_TEST_TYPE_COUNT, | |
1014 | &dev)); | |
1015 | ||
1016 | return 0; | |
1017 | } | |
e180c2b1 | 1018 | DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
f262d4ca SG |
1019 | |
1020 | /* Test device_first_child_ofdata_err(), etc. */ | |
1021 | static int dm_test_child_ofdata(struct unit_test_state *uts) | |
1022 | { | |
1023 | struct udevice *bus, *dev; | |
1024 | int count; | |
1025 | ||
1026 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); | |
1027 | count = 0; | |
d1998a9f | 1028 | device_foreach_child_of_to_plat(dev, bus) { |
73466df3 SG |
1029 | ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID); |
1030 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); | |
f262d4ca SG |
1031 | count++; |
1032 | } | |
1033 | ut_asserteq(3, count); | |
1034 | ||
1035 | return 0; | |
1036 | } | |
e180c2b1 | 1037 | DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
903e83ee SG |
1038 | |
1039 | /* Test device_first_child_err(), etc. */ | |
1040 | static int dm_test_first_child_probe(struct unit_test_state *uts) | |
1041 | { | |
1042 | struct udevice *bus, *dev; | |
1043 | int count; | |
1044 | ||
1045 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); | |
1046 | count = 0; | |
1047 | device_foreach_child_probe(dev, bus) { | |
73466df3 SG |
1048 | ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID); |
1049 | ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED); | |
903e83ee SG |
1050 | count++; |
1051 | } | |
1052 | ut_asserteq(3, count); | |
1053 | ||
1054 | return 0; | |
1055 | } | |
e180c2b1 | 1056 | DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
b0dcc871 SG |
1057 | |
1058 | /* Test that ofdata is read for parents before children */ | |
1059 | static int dm_test_ofdata_order(struct unit_test_state *uts) | |
1060 | { | |
1061 | struct udevice *bus, *dev; | |
1062 | ||
1063 | ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus)); | |
1064 | ut_assertnonnull(bus); | |
73466df3 | 1065 | ut_assert(!(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID)); |
b0dcc871 SG |
1066 | |
1067 | ut_assertok(device_find_first_child(bus, &dev)); | |
1068 | ut_assertnonnull(dev); | |
73466df3 | 1069 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)); |
b0dcc871 SG |
1070 | |
1071 | /* read the child's ofdata which should cause the parent's to be read */ | |
d1998a9f | 1072 | ut_assertok(device_of_to_plat(dev)); |
73466df3 SG |
1073 | ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID); |
1074 | ut_assert(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID); | |
b0dcc871 | 1075 | |
73466df3 SG |
1076 | ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED)); |
1077 | ut_assert(!(dev_get_flags(bus) & DM_FLAG_ACTIVATED)); | |
b0dcc871 SG |
1078 | |
1079 | return 0; | |
1080 | } | |
e180c2b1 | 1081 | DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
15daa486 DB |
1082 | |
1083 | /* Test dev_decode_display_timing() */ | |
1084 | static int dm_test_decode_display_timing(struct unit_test_state *uts) | |
1085 | { | |
1086 | struct udevice *dev; | |
1087 | struct display_timing timing; | |
1088 | ||
1089 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
1090 | ut_asserteq_str("a-test", dev->name); | |
1091 | ||
1092 | ut_assertok(dev_decode_display_timing(dev, 0, &timing)); | |
1093 | ut_assert(timing.hactive.typ == 240); | |
1094 | ut_assert(timing.hback_porch.typ == 7); | |
1095 | ut_assert(timing.hfront_porch.typ == 6); | |
1096 | ut_assert(timing.hsync_len.typ == 1); | |
1097 | ut_assert(timing.vactive.typ == 320); | |
1098 | ut_assert(timing.vback_porch.typ == 5); | |
1099 | ut_assert(timing.vfront_porch.typ == 8); | |
1100 | ut_assert(timing.vsync_len.typ == 2); | |
1101 | ut_assert(timing.pixelclock.typ == 6500000); | |
1102 | ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH); | |
1103 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW)); | |
1104 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH)); | |
1105 | ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW); | |
1106 | ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH); | |
1107 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW)); | |
1108 | ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE); | |
1109 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)); | |
1110 | ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED); | |
1111 | ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN); | |
1112 | ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK); | |
1113 | ||
1114 | ut_assertok(dev_decode_display_timing(dev, 1, &timing)); | |
1115 | ut_assert(timing.hactive.typ == 480); | |
1116 | ut_assert(timing.hback_porch.typ == 59); | |
1117 | ut_assert(timing.hfront_porch.typ == 10); | |
1118 | ut_assert(timing.hsync_len.typ == 12); | |
1119 | ut_assert(timing.vactive.typ == 800); | |
1120 | ut_assert(timing.vback_porch.typ == 15); | |
1121 | ut_assert(timing.vfront_porch.typ == 17); | |
1122 | ut_assert(timing.vsync_len.typ == 16); | |
1123 | ut_assert(timing.pixelclock.typ == 9000000); | |
1124 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH)); | |
1125 | ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_LOW); | |
1126 | ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH); | |
1127 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW)); | |
1128 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH)); | |
1129 | ut_assert(timing.flags & DISPLAY_FLAGS_DE_LOW); | |
1130 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)); | |
1131 | ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE); | |
1132 | ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED)); | |
1133 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN)); | |
1134 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK)); | |
1135 | ||
1136 | ut_assertok(dev_decode_display_timing(dev, 2, &timing)); | |
1137 | ut_assert(timing.hactive.typ == 800); | |
1138 | ut_assert(timing.hback_porch.typ == 89); | |
1139 | ut_assert(timing.hfront_porch.typ == 164); | |
1140 | ut_assert(timing.hsync_len.typ == 11); | |
1141 | ut_assert(timing.vactive.typ == 480); | |
1142 | ut_assert(timing.vback_porch.typ == 23); | |
1143 | ut_assert(timing.vfront_porch.typ == 10); | |
1144 | ut_assert(timing.vsync_len.typ == 13); | |
1145 | ut_assert(timing.pixelclock.typ == 33500000); | |
1146 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH)); | |
1147 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW)); | |
1148 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH)); | |
1149 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW)); | |
1150 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH)); | |
1151 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW)); | |
1152 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE)); | |
1153 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)); | |
1154 | ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED)); | |
1155 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN)); | |
1156 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK)); | |
1157 | ||
1158 | ut_assert(dev_decode_display_timing(dev, 3, &timing)); | |
1159 | return 0; | |
1160 | } | |
1161 | DM_TEST(dm_test_decode_display_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); | |
269fa846 | 1162 | |
2f3d6a42 NJ |
1163 | /* Test dev_decode_panel_timing() */ |
1164 | static int dm_test_decode_panel_timing(struct unit_test_state *uts) | |
1165 | { | |
1166 | struct udevice *dev; | |
1167 | struct display_timing timing; | |
1168 | ||
1169 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
1170 | ut_asserteq_str("a-test", dev->name); | |
1171 | ||
1172 | ut_assertok(dev_decode_panel_timing(dev, &timing)); | |
1173 | ut_assert(timing.hactive.typ == 240); | |
1174 | ut_assert(timing.hback_porch.typ == 7); | |
1175 | ut_assert(timing.hfront_porch.typ == 6); | |
1176 | ut_assert(timing.hsync_len.typ == 1); | |
1177 | ut_assert(timing.vactive.typ == 320); | |
1178 | ut_assert(timing.vback_porch.typ == 5); | |
1179 | ut_assert(timing.vfront_porch.typ == 8); | |
1180 | ut_assert(timing.vsync_len.typ == 2); | |
1181 | ut_assert(timing.pixelclock.typ == 6500000); | |
1182 | ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH); | |
1183 | ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW)); | |
1184 | ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH)); | |
1185 | ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW); | |
1186 | ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH); | |
1187 | ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW)); | |
1188 | ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE); | |
1189 | ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE)); | |
1190 | ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED); | |
1191 | ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN); | |
1192 | ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK); | |
1193 | ||
1194 | return 0; | |
1195 | } | |
1196 | DM_TEST(dm_test_decode_panel_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); | |
1197 | ||
269fa846 PD |
1198 | /* Test read_resourcee() */ |
1199 | static int dm_test_read_resource(struct unit_test_state *uts) | |
1200 | { | |
1201 | struct udevice *dev; | |
1202 | struct resource res; | |
1203 | ||
1204 | /* test resource without translation */ | |
1205 | ut_assertok(uclass_find_device_by_name(UCLASS_SIMPLE_BUS, "syscon@2", &dev)); | |
1206 | ut_assertok(dev_read_resource(dev, 0, &res)); | |
1207 | ut_asserteq(0x40, res.start); | |
1208 | ut_asserteq(0x44, res.end); | |
1209 | ut_assertok(dev_read_resource(dev, 1, &res)); | |
1210 | ut_asserteq(0x48, res.start); | |
1211 | ut_asserteq(0x4d, res.end); | |
1212 | ||
1213 | /* test resource with translation */ | |
1214 | ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@1,100", &dev)); | |
1215 | ut_assertok(dev_read_resource(dev, 0, &res)); | |
1216 | ut_asserteq(0x9000, res.start); | |
1217 | ut_asserteq(0x9fff, res.end); | |
1218 | ||
1219 | /* test named resource */ | |
1220 | ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@0,0", &dev)); | |
1221 | ut_assertok(dev_read_resource_byname(dev, "sandbox-dummy-0", &res)); | |
1222 | ut_asserteq(0x8000, res.start); | |
1223 | ut_asserteq(0x8fff, res.end); | |
1224 | ||
1225 | return 0; | |
1226 | } | |
1227 | ||
1228 | DM_TEST(dm_test_read_resource, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |