]>
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 SG |
27 | { |
28 | const struct dm_test_pdata *pdata = dev->platdata; | |
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 | ||
54c5d08a | 41 | static int testfdt_ofdata_to_platdata(struct udevice *dev) |
2e7d35d2 SG |
42 | { |
43 | struct dm_test_pdata *pdata = dev_get_platdata(dev); | |
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, | |
86 | .ofdata_to_platdata = testfdt_ofdata_to_platdata, | |
87 | .probe = testfdt_drv_probe, | |
88 | .ops = &test_ops, | |
89 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), | |
90 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), | |
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, | |
104 | .ofdata_to_platdata = testfdt_ofdata_to_platdata, | |
105 | .probe = testfdt_drv_probe, | |
106 | .ops = &test_ops, | |
107 | .priv_auto_alloc_size = sizeof(struct dm_test_priv), | |
108 | .platdata_auto_alloc_size = sizeof(struct dm_test_pdata), | |
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 | { | |
135 | struct dm_testprobe_pdata *pdata = dev_get_platdata(dev); | |
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, | |
150 | .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata), | |
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 | { | |
170 | struct dm_testdevres_pdata *pdata = dev_get_platdata(dev); | |
171 | ||
172 | pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0); | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
42a0ce57 SG |
177 | static int testdevres_drv_ofdata_to_platdata(struct udevice *dev) |
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, | |
42a0ce57 | 205 | .ofdata_to_platdata = testdevres_drv_ofdata_to_platdata, |
dc12ebbb SG |
206 | .probe = testdevres_drv_probe, |
207 | .platdata_auto_alloc_size = sizeof(struct dm_testdevres_pdata), | |
208 | .priv_auto_alloc_size = sizeof(struct dm_testdevres_priv), | |
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 SG |
234 | * Get the 'ping-expect' property, which tells us what the |
235 | * ping add should be. We don't use the platdata because we | |
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)); | |
f8a85449 | 274 | ut_assert(dev->platdata); |
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); | |
336 | ||
9cc36a2b | 337 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev)); |
5a66a8ff SG |
338 | ut_asserteq_str("a-test", dev->name); |
339 | ||
340 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5, | |
341 | true, &dev)); | |
342 | ut_asserteq_ptr(NULL, dev); | |
343 | ||
344 | /* Test aliases */ | |
345 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev)); | |
346 | ut_asserteq_str("e-test", dev->name); | |
347 | ||
348 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7, | |
349 | true, &dev)); | |
350 | ||
1ca7e206 SG |
351 | /* |
352 | * Note that c-test nodes are not probed since it is not a top-level | |
353 | * node | |
354 | */ | |
5a66a8ff SG |
355 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
356 | ut_asserteq_str("b-test", dev->name); | |
357 | ||
358 | /* | |
359 | * d-test wants sequence number 3 also, but it can't have it because | |
360 | * b-test gets it first. | |
361 | */ | |
362 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev)); | |
363 | ut_asserteq_str("d-test", dev->name); | |
364 | ||
be1a6e94 MW |
365 | /* |
366 | * d-test actually gets 9, because thats the next free one after the | |
367 | * aliases. | |
368 | */ | |
369 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 9, &dev)); | |
5a66a8ff SG |
370 | ut_asserteq_str("d-test", dev->name); |
371 | ||
be1a6e94 MW |
372 | /* initially no one wants seq 10 */ |
373 | ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 10, | |
5a66a8ff SG |
374 | &dev)); |
375 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); | |
9cc36a2b | 376 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev)); |
5a66a8ff SG |
377 | |
378 | /* But now that it is probed, we can find it */ | |
be1a6e94 | 379 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 10, &dev)); |
9cc36a2b | 380 | ut_asserteq_str("f-test", dev->name); |
5a66a8ff | 381 | |
be1a6e94 MW |
382 | /* |
383 | * And we should still have holes in our sequence numbers, that is 2 | |
384 | * and 4 should not be used. | |
385 | */ | |
386 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2, | |
387 | true, &dev)); | |
388 | ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4, | |
389 | true, &dev)); | |
390 | ||
5a66a8ff SG |
391 | return 0; |
392 | } | |
e180c2b1 | 393 | DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
f4cdead2 SG |
394 | |
395 | /* Test that we can find a device by device tree offset */ | |
e721b882 | 396 | static int dm_test_fdt_offset(struct unit_test_state *uts) |
f4cdead2 SG |
397 | { |
398 | const void *blob = gd->fdt_blob; | |
399 | struct udevice *dev; | |
400 | int node; | |
401 | ||
402 | node = fdt_path_offset(blob, "/e-test"); | |
403 | ut_assert(node > 0); | |
404 | ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node, | |
405 | &dev)); | |
406 | ut_asserteq_str("e-test", dev->name); | |
407 | ||
408 | /* This node should not be bound */ | |
409 | node = fdt_path_offset(blob, "/junk"); | |
410 | ut_assert(node > 0); | |
411 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, | |
412 | node, &dev)); | |
413 | ||
414 | /* This is not a top level node so should not be probed */ | |
1ca7e206 | 415 | node = fdt_path_offset(blob, "/some-bus/c-test@5"); |
f4cdead2 SG |
416 | ut_assert(node > 0); |
417 | ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT, | |
418 | node, &dev)); | |
419 | ||
420 | return 0; | |
421 | } | |
86b54ece | 422 | DM_TEST(dm_test_fdt_offset, |
e180c2b1 | 423 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
98561572 SG |
424 | |
425 | /** | |
426 | * Test various error conditions with uclass_first_device() and | |
427 | * uclass_next_device() | |
428 | */ | |
429 | static int dm_test_first_next_device(struct unit_test_state *uts) | |
430 | { | |
431 | struct dm_testprobe_pdata *pdata; | |
432 | struct udevice *dev, *parent = NULL; | |
433 | int count; | |
434 | int ret; | |
435 | ||
436 | /* There should be 4 devices */ | |
437 | for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0; | |
438 | dev; | |
439 | ret = uclass_next_device(&dev)) { | |
440 | count++; | |
441 | parent = dev_get_parent(dev); | |
442 | } | |
443 | ut_assertok(ret); | |
444 | ut_asserteq(4, count); | |
445 | ||
446 | /* Remove them and try again, with an error on the second one */ | |
447 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev)); | |
448 | pdata = dev_get_platdata(dev); | |
449 | pdata->probe_err = -ENOMEM; | |
450 | device_remove(parent, DM_REMOVE_NORMAL); | |
451 | ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev)); | |
452 | ut_asserteq(-ENOMEM, uclass_next_device(&dev)); | |
453 | ut_asserteq_ptr(dev, NULL); | |
454 | ||
455 | /* Now an error on the first one */ | |
456 | ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev)); | |
457 | pdata = dev_get_platdata(dev); | |
458 | pdata->probe_err = -ENOENT; | |
459 | device_remove(parent, DM_REMOVE_NORMAL); | |
460 | ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev)); | |
461 | ||
462 | return 0; | |
463 | } | |
e180c2b1 | 464 | DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
95ce385a | 465 | |
3cf0fba4 SG |
466 | /* Test iteration through devices in a uclass */ |
467 | static int dm_test_uclass_foreach(struct unit_test_state *uts) | |
468 | { | |
469 | struct udevice *dev; | |
470 | struct uclass *uc; | |
471 | int count; | |
472 | ||
473 | count = 0; | |
474 | uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc) | |
475 | count++; | |
88e6a60e | 476 | ut_asserteq(9, count); |
3cf0fba4 SG |
477 | |
478 | count = 0; | |
479 | uclass_foreach_dev(dev, uc) | |
480 | count++; | |
88e6a60e | 481 | ut_asserteq(9, count); |
3cf0fba4 SG |
482 | |
483 | return 0; | |
484 | } | |
e180c2b1 | 485 | DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
3cf0fba4 | 486 | |
95ce385a SG |
487 | /** |
488 | * check_devices() - Check return values and pointers | |
489 | * | |
490 | * This runs through a full sequence of uclass_first_device_check()... | |
491 | * uclass_next_device_check() checking that the return values and devices | |
492 | * are correct. | |
493 | * | |
494 | * @uts: Test state | |
495 | * @devlist: List of expected devices | |
496 | * @mask: Indicates which devices should return an error. Device n should | |
497 | * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if | |
498 | * bit n is clear. | |
499 | */ | |
500 | static int check_devices(struct unit_test_state *uts, | |
501 | struct udevice *devlist[], int mask) | |
502 | { | |
503 | int expected_ret; | |
504 | struct udevice *dev; | |
505 | int i; | |
506 | ||
507 | expected_ret = (mask & 1) ? -ENOENT : 0; | |
508 | mask >>= 1; | |
509 | ut_asserteq(expected_ret, | |
510 | uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); | |
511 | for (i = 0; i < 4; i++) { | |
512 | ut_asserteq_ptr(devlist[i], dev); | |
513 | expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0; | |
514 | mask >>= 1; | |
515 | ut_asserteq(expected_ret, uclass_next_device_check(&dev)); | |
516 | } | |
517 | ut_asserteq_ptr(NULL, dev); | |
518 | ||
519 | return 0; | |
520 | } | |
521 | ||
522 | /* Test uclass_first_device_check() and uclass_next_device_check() */ | |
523 | static int dm_test_first_next_ok_device(struct unit_test_state *uts) | |
524 | { | |
525 | struct dm_testprobe_pdata *pdata; | |
526 | struct udevice *dev, *parent = NULL, *devlist[4]; | |
527 | int count; | |
528 | int ret; | |
529 | ||
530 | /* There should be 4 devices */ | |
531 | count = 0; | |
532 | for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev); | |
533 | dev; | |
534 | ret = uclass_next_device_check(&dev)) { | |
535 | ut_assertok(ret); | |
536 | devlist[count++] = dev; | |
537 | parent = dev_get_parent(dev); | |
538 | } | |
539 | ut_asserteq(4, count); | |
540 | ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev)); | |
541 | ut_assertok(check_devices(uts, devlist, 0)); | |
542 | ||
543 | /* Remove them and try again, with an error on the second one */ | |
544 | pdata = dev_get_platdata(devlist[1]); | |
545 | pdata->probe_err = -ENOENT - 1; | |
546 | device_remove(parent, DM_REMOVE_NORMAL); | |
547 | ut_assertok(check_devices(uts, devlist, 1 << 1)); | |
548 | ||
549 | /* Now an error on the first one */ | |
550 | pdata = dev_get_platdata(devlist[0]); | |
551 | pdata->probe_err = -ENOENT - 0; | |
552 | device_remove(parent, DM_REMOVE_NORMAL); | |
553 | ut_assertok(check_devices(uts, devlist, 3 << 0)); | |
554 | ||
555 | /* Now errors on all */ | |
556 | pdata = dev_get_platdata(devlist[2]); | |
557 | pdata->probe_err = -ENOENT - 2; | |
558 | pdata = dev_get_platdata(devlist[3]); | |
559 | pdata->probe_err = -ENOENT - 3; | |
560 | device_remove(parent, DM_REMOVE_NORMAL); | |
561 | ut_assertok(check_devices(uts, devlist, 0xf << 0)); | |
562 | ||
563 | return 0; | |
564 | } | |
e180c2b1 | 565 | DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
e8d52918 MS |
566 | |
567 | static const struct udevice_id fdt_dummy_ids[] = { | |
568 | { .compatible = "denx,u-boot-fdt-dummy", }, | |
569 | { } | |
570 | }; | |
571 | ||
572 | UCLASS_DRIVER(fdt_dummy) = { | |
507cef3d | 573 | .name = "fdt-dummy", |
e8d52918 MS |
574 | .id = UCLASS_TEST_DUMMY, |
575 | .flags = DM_UC_FLAG_SEQ_ALIAS, | |
576 | }; | |
577 | ||
578 | U_BOOT_DRIVER(fdt_dummy_drv) = { | |
579 | .name = "fdt_dummy_drv", | |
580 | .of_match = fdt_dummy_ids, | |
581 | .id = UCLASS_TEST_DUMMY, | |
582 | }; | |
583 | ||
584 | static int dm_test_fdt_translation(struct unit_test_state *uts) | |
585 | { | |
586 | struct udevice *dev; | |
641067fb | 587 | fdt32_t dma_addr[2]; |
e8d52918 MS |
588 | |
589 | /* Some simple translations */ | |
590 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
591 | ut_asserteq_str("dev@0,0", dev->name); | |
592 | ut_asserteq(0x8000, dev_read_addr(dev)); | |
593 | ||
594 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev)); | |
595 | ut_asserteq_str("dev@1,100", dev->name); | |
596 | ut_asserteq(0x9000, dev_read_addr(dev)); | |
597 | ||
598 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev)); | |
599 | ut_asserteq_str("dev@2,200", dev->name); | |
600 | ut_asserteq(0xA000, dev_read_addr(dev)); | |
601 | ||
602 | /* No translation for busses with #size-cells == 0 */ | |
603 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev)); | |
604 | ut_asserteq_str("dev@42", dev->name); | |
605 | ut_asserteq(0x42, dev_read_addr(dev)); | |
606 | ||
641067fb FD |
607 | /* dma address translation */ |
608 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
609 | dma_addr[0] = cpu_to_be32(0); | |
610 | dma_addr[1] = cpu_to_be32(0); | |
611 | ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr)); | |
612 | ||
613 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev)); | |
614 | dma_addr[0] = cpu_to_be32(1); | |
615 | dma_addr[1] = cpu_to_be32(0x100); | |
616 | ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr)); | |
617 | ||
e8d52918 MS |
618 | return 0; |
619 | } | |
e180c2b1 | 620 | DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
30a90f56 | 621 | |
6e64830f OP |
622 | static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts) |
623 | { | |
624 | struct udevice *gpio, *dev; | |
625 | void *ptr; | |
626 | ||
627 | /* Test for missing reg property */ | |
628 | ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio)); | |
629 | ut_assertnull(devfdt_get_addr_ptr(gpio)); | |
630 | ||
631 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
632 | ptr = devfdt_get_addr_ptr(dev); | |
633 | ut_asserteq_ptr((void *)0x8000, ptr); | |
634 | ||
635 | return 0; | |
636 | } | |
637 | DM_TEST(dm_test_fdt_get_addr_ptr_flat, | |
638 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); | |
639 | ||
30a90f56 ÁFR |
640 | static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts) |
641 | { | |
642 | struct udevice *dev; | |
643 | fdt_addr_t addr; | |
644 | void *paddr; | |
645 | ||
646 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
647 | ||
648 | addr = devfdt_get_addr(dev); | |
649 | ut_asserteq(0x8000, addr); | |
650 | ||
651 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
652 | ut_assertnonnull(paddr); | |
653 | ut_asserteq_ptr(paddr, devfdt_remap_addr(dev)); | |
654 | ||
655 | return 0; | |
656 | } | |
657 | DM_TEST(dm_test_fdt_remap_addr_flat, | |
e180c2b1 | 658 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
30a90f56 | 659 | |
79598820 ÁFR |
660 | static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts) |
661 | { | |
662 | struct udevice *dev; | |
663 | fdt_addr_t addr; | |
f5b90479 | 664 | fdt_size_t size; |
79598820 ÁFR |
665 | void *paddr; |
666 | ||
667 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
668 | ||
f5b90479 | 669 | addr = devfdt_get_addr_size_index(dev, 0, &size); |
79598820 | 670 | ut_asserteq(0x8000, addr); |
f5b90479 | 671 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
672 | |
673 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
674 | ut_assertnonnull(paddr); | |
675 | ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0)); | |
676 | ||
677 | return 0; | |
678 | } | |
679 | DM_TEST(dm_test_fdt_remap_addr_index_flat, | |
e180c2b1 | 680 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
79598820 ÁFR |
681 | |
682 | static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts) | |
683 | { | |
684 | struct udevice *dev; | |
685 | fdt_addr_t addr; | |
f5b90479 | 686 | fdt_size_t size; |
79598820 ÁFR |
687 | void *paddr; |
688 | ||
689 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
690 | ||
f5b90479 | 691 | addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size); |
79598820 | 692 | ut_asserteq(0x8000, addr); |
f5b90479 | 693 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
694 | |
695 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
696 | ut_assertnonnull(paddr); | |
697 | ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0")); | |
698 | ||
699 | return 0; | |
700 | } | |
701 | DM_TEST(dm_test_fdt_remap_addr_name_flat, | |
e180c2b1 | 702 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE); |
79598820 | 703 | |
30a90f56 ÁFR |
704 | static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts) |
705 | { | |
706 | struct udevice *dev; | |
707 | fdt_addr_t addr; | |
708 | void *paddr; | |
709 | ||
710 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
711 | ||
712 | addr = dev_read_addr(dev); | |
713 | ut_asserteq(0x8000, addr); | |
714 | ||
715 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
716 | ut_assertnonnull(paddr); | |
717 | ut_asserteq_ptr(paddr, dev_remap_addr(dev)); | |
718 | ||
719 | return 0; | |
720 | } | |
721 | DM_TEST(dm_test_fdt_remap_addr_live, | |
e180c2b1 | 722 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
2ea4d0db | 723 | |
79598820 ÁFR |
724 | static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts) |
725 | { | |
726 | struct udevice *dev; | |
727 | fdt_addr_t addr; | |
f5b90479 | 728 | fdt_size_t size; |
79598820 ÁFR |
729 | void *paddr; |
730 | ||
731 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
732 | ||
f5b90479 | 733 | addr = dev_read_addr_size_index(dev, 0, &size); |
79598820 | 734 | ut_asserteq(0x8000, addr); |
f5b90479 | 735 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
736 | |
737 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
738 | ut_assertnonnull(paddr); | |
739 | ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0)); | |
740 | ||
741 | return 0; | |
742 | } | |
743 | DM_TEST(dm_test_fdt_remap_addr_index_live, | |
e180c2b1 | 744 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
79598820 ÁFR |
745 | |
746 | static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts) | |
747 | { | |
748 | struct udevice *dev; | |
749 | fdt_addr_t addr; | |
f5b90479 | 750 | fdt_size_t size; |
79598820 ÁFR |
751 | void *paddr; |
752 | ||
753 | ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev)); | |
754 | ||
f5b90479 | 755 | addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size); |
79598820 | 756 | ut_asserteq(0x8000, addr); |
f5b90479 | 757 | ut_asserteq(0x1000, size); |
79598820 ÁFR |
758 | |
759 | paddr = map_physmem(addr, 0, MAP_NOCACHE); | |
760 | ut_assertnonnull(paddr); | |
761 | ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0")); | |
762 | ||
763 | return 0; | |
764 | } | |
765 | DM_TEST(dm_test_fdt_remap_addr_name_live, | |
e180c2b1 | 766 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
79598820 | 767 | |
2ea4d0db MS |
768 | static int dm_test_fdt_livetree_writing(struct unit_test_state *uts) |
769 | { | |
770 | struct udevice *dev; | |
771 | ofnode node; | |
772 | ||
773 | if (!of_live_active()) { | |
774 | printf("Live tree not active; ignore test\n"); | |
775 | return 0; | |
776 | } | |
777 | ||
778 | /* Test enabling devices */ | |
779 | ||
780 | node = ofnode_path("/usb@2"); | |
781 | ||
782 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
783 | ofnode_set_enabled(node, true); | |
784 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
785 | ||
786 | device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node, | |
787 | &dev); | |
788 | ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev)); | |
789 | ||
790 | /* Test string property setting */ | |
791 | ||
792 | ut_assert(device_is_compatible(dev, "sandbox,usb")); | |
793 | ofnode_write_string(node, "compatible", "gdsys,super-usb"); | |
794 | ut_assert(device_is_compatible(dev, "gdsys,super-usb")); | |
795 | ofnode_write_string(node, "compatible", "sandbox,usb"); | |
796 | ut_assert(device_is_compatible(dev, "sandbox,usb")); | |
797 | ||
798 | /* Test setting generic properties */ | |
799 | ||
800 | /* Non-existent in DTB */ | |
801 | ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev)); | |
802 | /* reg = 0x42, size = 0x100 */ | |
803 | ut_assertok(ofnode_write_prop(node, "reg", 8, | |
804 | "\x00\x00\x00\x42\x00\x00\x01\x00")); | |
805 | ut_asserteq(0x42, dev_read_addr(dev)); | |
806 | ||
807 | /* Test disabling devices */ | |
808 | ||
809 | device_remove(dev, DM_REMOVE_NORMAL); | |
810 | device_unbind(dev); | |
811 | ||
812 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
813 | ofnode_set_enabled(node, false); | |
814 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
815 | ||
816 | return 0; | |
817 | } | |
e180c2b1 | 818 | DM_TEST(dm_test_fdt_livetree_writing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
172942a4 MS |
819 | |
820 | static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts) | |
821 | { | |
822 | ofnode node; | |
823 | ||
824 | if (!of_live_active()) { | |
825 | printf("Live tree not active; ignore test\n"); | |
826 | return 0; | |
827 | } | |
828 | ||
829 | node = ofnode_path("/usb@2"); | |
830 | ||
831 | /* Test enabling devices */ | |
832 | ||
833 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
834 | dev_enable_by_path("/usb@2"); | |
835 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
836 | ||
837 | /* Test disabling devices */ | |
838 | ||
839 | ut_assert(of_device_is_available(ofnode_to_np(node))); | |
840 | dev_disable_by_path("/usb@2"); | |
841 | ut_assert(!of_device_is_available(ofnode_to_np(node))); | |
842 | ||
843 | return 0; | |
844 | } | |
e180c2b1 SG |
845 | DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA | |
846 | UT_TESTF_SCAN_FDT); | |
d0b4f68d SG |
847 | |
848 | /* Test a few uclass phandle functions */ | |
849 | static int dm_test_fdt_phandle(struct unit_test_state *uts) | |
850 | { | |
851 | struct udevice *back, *dev, *dev2; | |
852 | ||
853 | ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back)); | |
331caeaf | 854 | ut_assertnonnull(back); |
d0b4f68d SG |
855 | ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR, |
856 | back, "missing", &dev)); | |
857 | ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back, | |
858 | "power-supply", &dev)); | |
331caeaf | 859 | ut_assertnonnull(dev); |
d0b4f68d SG |
860 | ut_asserteq(0, device_active(dev)); |
861 | ut_asserteq_str("ldo1", dev->name); | |
862 | ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back, | |
863 | "power-supply", &dev2)); | |
864 | ut_asserteq_ptr(dev, dev2); | |
865 | ||
866 | return 0; | |
867 | } | |
e180c2b1 | 868 | DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
3abe1115 SG |
869 | |
870 | /* Test device_find_first_child_by_uclass() */ | |
871 | static int dm_test_first_child(struct unit_test_state *uts) | |
872 | { | |
873 | struct udevice *i2c, *dev, *dev2; | |
874 | ||
875 | ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c)); | |
876 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev)); | |
877 | ut_asserteq_str("rtc@43", dev->name); | |
878 | ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2)); | |
879 | ut_asserteq_ptr(dev, dev2); | |
880 | ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2)); | |
881 | ut_asserteq_str("rtc@61", dev2->name); | |
882 | ||
883 | ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM, | |
884 | &dev)); | |
885 | ut_asserteq_str("eeprom@2c", dev->name); | |
886 | ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2)); | |
887 | ut_asserteq_ptr(dev, dev2); | |
888 | ||
889 | ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c, | |
890 | UCLASS_VIDEO, &dev)); | |
891 | ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev)); | |
892 | ||
893 | return 0; | |
894 | } | |
e180c2b1 | 895 | DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
a1b17e4f SG |
896 | |
897 | /* Test integer functions in dm_read_...() */ | |
898 | static int dm_test_read_int(struct unit_test_state *uts) | |
899 | { | |
900 | struct udevice *dev; | |
901 | u32 val32; | |
902 | s32 sval; | |
903 | uint val; | |
70573c6c | 904 | u64 val64; |
a1b17e4f SG |
905 | |
906 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
907 | ut_asserteq_str("a-test", dev->name); | |
908 | ut_assertok(dev_read_u32(dev, "int-value", &val32)); | |
909 | ut_asserteq(1234, val32); | |
910 | ||
911 | ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32)); | |
912 | ut_asserteq(6, dev_read_u32_default(dev, "missing", 6)); | |
913 | ||
914 | ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6)); | |
915 | ut_asserteq(1234, val32); | |
916 | ||
917 | ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval)); | |
918 | ut_asserteq(6, dev_read_s32_default(dev, "missing", 6)); | |
919 | ||
920 | ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6)); | |
921 | ut_assertok(dev_read_s32(dev, "uint-value", &sval)); | |
922 | ut_asserteq(-1234, sval); | |
923 | ||
924 | val = 0; | |
925 | ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val)); | |
926 | ut_assertok(dev_read_u32u(dev, "uint-value", &val)); | |
927 | ut_asserteq(-1234, val); | |
928 | ||
70573c6c DB |
929 | ut_assertok(dev_read_u64(dev, "int64-value", &val64)); |
930 | ut_asserteq_64(0x1111222233334444, val64); | |
931 | ||
932 | ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64)); | |
933 | ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6)); | |
934 | ||
935 | ut_asserteq_64(0x1111222233334444, | |
936 | dev_read_u64_default(dev, "int64-value", 6)); | |
937 | ||
a1b17e4f SG |
938 | return 0; |
939 | } | |
e180c2b1 | 940 | DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
50162348 | 941 | |
4bb7075c DB |
942 | static int dm_test_read_int_index(struct unit_test_state *uts) |
943 | { | |
944 | struct udevice *dev; | |
945 | u32 val32; | |
946 | ||
947 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
948 | ut_asserteq_str("a-test", dev->name); | |
949 | ||
950 | ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32)); | |
951 | ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19)); | |
952 | ||
953 | ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32)); | |
954 | ut_asserteq(5678, val32); | |
955 | ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32)); | |
956 | ut_asserteq(9123, val32); | |
957 | ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32)); | |
958 | ut_asserteq(4567, val32); | |
959 | ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3, | |
960 | &val32)); | |
961 | ||
962 | ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2)); | |
963 | ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2)); | |
964 | ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2)); | |
965 | ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2)); | |
966 | ||
967 | return 0; | |
968 | } | |
e180c2b1 | 969 | DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
4bb7075c | 970 | |
cc72f3e0 PD |
971 | static int dm_test_read_phandle(struct unit_test_state *uts) |
972 | { | |
973 | struct udevice *dev; | |
974 | struct ofnode_phandle_args args; | |
975 | int ret; | |
976 | const char prop[] = "test-gpios"; | |
977 | const char cell[] = "#gpio-cells"; | |
978 | const char prop2[] = "phandle-value"; | |
979 | ||
980 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); | |
981 | ut_asserteq_str("a-test", dev->name); | |
982 | ||
983 | /* Test dev_count_phandle_with_args with cell name */ | |
984 | ret = dev_count_phandle_with_args(dev, "missing", cell, 0); | |
985 | ut_asserteq(-ENOENT, ret); | |
986 | ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0); | |
987 | ut_asserteq(-EINVAL, ret); | |
988 | ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0)); | |
989 | ||
990 | /* Test dev_read_phandle_with_args with cell name */ | |
991 | ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args); | |
992 | ut_asserteq(-ENOENT, ret); | |
993 | ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args); | |
994 | ut_asserteq(-EINVAL, ret); | |
995 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args)); | |
996 | ut_asserteq(1, args.args_count); | |
997 | ut_asserteq(1, args.args[0]); | |
998 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args)); | |
999 | ut_asserteq(1, args.args_count); | |
1000 | ut_asserteq(4, args.args[0]); | |
1001 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args)); | |
1002 | ut_asserteq(5, args.args_count); | |
1003 | ut_asserteq(5, args.args[0]); | |
1004 | ut_asserteq(1, args.args[4]); | |
1005 | ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args); | |
1006 | ut_asserteq(-ENOENT, ret); | |
1007 | ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args)); | |
1008 | ut_asserteq(1, args.args_count); | |
1009 | ut_asserteq(12, args.args[0]); | |
1010 | ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args); | |
1011 | ut_asserteq(-ENOENT, ret); | |
1012 | ||
1013 | /* Test dev_count_phandle_with_args with cell count */ | |
1014 | ret = dev_count_phandle_with_args(dev, "missing", NULL, 2); | |
1015 | ut_asserteq(-ENOENT, ret); | |
1016 | ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1)); | |
1017 | ||
1018 | /* Test dev_read_phandle_with_args with cell count */ | |
1019 | ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args)); | |
1020 | ut_asserteq(1, ofnode_valid(args.node)); | |
1021 | ut_asserteq(1, args.args_count); | |
1022 | ut_asserteq(10, args.args[0]); | |
1023 | ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args); | |
1024 | ut_asserteq(-EINVAL, ret); | |
1025 | ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args)); | |
1026 | ut_asserteq(1, ofnode_valid(args.node)); | |
1027 | ut_asserteq(1, args.args_count); | |
1028 | ut_asserteq(30, args.args[0]); | |
1029 | ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args); | |
1030 | ut_asserteq(-ENOENT, ret); | |
1031 | ||
1032 | return 0; | |
1033 | } | |
1034 | DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); | |
1035 | ||
50162348 SG |
1036 | /* Test iteration through devices by drvdata */ |
1037 | static int dm_test_uclass_drvdata(struct unit_test_state *uts) | |
1038 | { | |
1039 | struct udevice *dev; | |
1040 | ||
1041 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, | |
1042 | DM_TEST_TYPE_FIRST, &dev)); | |
1043 | ut_asserteq_str("a-test", dev->name); | |
1044 | ||
1045 | ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT, | |
1046 | DM_TEST_TYPE_SECOND, &dev)); | |
1047 | ut_asserteq_str("d-test", dev->name); | |
1048 | ||
1049 | ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT, | |
1050 | DM_TEST_TYPE_COUNT, | |
1051 | &dev)); | |
1052 | ||
1053 | return 0; | |
1054 | } | |
e180c2b1 | 1055 | DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
f262d4ca SG |
1056 | |
1057 | /* Test device_first_child_ofdata_err(), etc. */ | |
1058 | static int dm_test_child_ofdata(struct unit_test_state *uts) | |
1059 | { | |
1060 | struct udevice *bus, *dev; | |
1061 | int count; | |
1062 | ||
1063 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); | |
1064 | count = 0; | |
1065 | device_foreach_child_ofdata_to_platdata(dev, bus) { | |
1066 | ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); | |
1067 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); | |
1068 | count++; | |
1069 | } | |
1070 | ut_asserteq(3, count); | |
1071 | ||
1072 | return 0; | |
1073 | } | |
e180c2b1 | 1074 | DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
903e83ee SG |
1075 | |
1076 | /* Test device_first_child_err(), etc. */ | |
1077 | static int dm_test_first_child_probe(struct unit_test_state *uts) | |
1078 | { | |
1079 | struct udevice *bus, *dev; | |
1080 | int count; | |
1081 | ||
1082 | ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus)); | |
1083 | count = 0; | |
1084 | device_foreach_child_probe(dev, bus) { | |
1085 | ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); | |
1086 | ut_assert(dev->flags & DM_FLAG_ACTIVATED); | |
1087 | count++; | |
1088 | } | |
1089 | ut_asserteq(3, count); | |
1090 | ||
1091 | return 0; | |
1092 | } | |
e180c2b1 | 1093 | DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
b0dcc871 SG |
1094 | |
1095 | /* Test that ofdata is read for parents before children */ | |
1096 | static int dm_test_ofdata_order(struct unit_test_state *uts) | |
1097 | { | |
1098 | struct udevice *bus, *dev; | |
1099 | ||
1100 | ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus)); | |
1101 | ut_assertnonnull(bus); | |
1102 | ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID)); | |
1103 | ||
1104 | ut_assertok(device_find_first_child(bus, &dev)); | |
1105 | ut_assertnonnull(dev); | |
1106 | ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID)); | |
1107 | ||
1108 | /* read the child's ofdata which should cause the parent's to be read */ | |
1109 | ut_assertok(device_ofdata_to_platdata(dev)); | |
1110 | ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID); | |
1111 | ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID); | |
1112 | ||
1113 | ut_assert(!(dev->flags & DM_FLAG_ACTIVATED)); | |
1114 | ut_assert(!(bus->flags & DM_FLAG_ACTIVATED)); | |
1115 | ||
1116 | return 0; | |
1117 | } | |
e180c2b1 | 1118 | DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |