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