1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
14 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
18 #include <dm/of_access.h>
21 DECLARE_GLOBAL_DATA_PTR
;
23 static int testfdt_drv_ping(struct udevice
*dev
, int pingval
, int *pingret
)
25 const struct dm_test_pdata
*pdata
= dev
->platdata
;
26 struct dm_test_priv
*priv
= dev_get_priv(dev
);
28 *pingret
= pingval
+ pdata
->ping_add
;
29 priv
->ping_total
+= *pingret
;
34 static const struct test_ops test_ops
= {
35 .ping
= testfdt_drv_ping
,
38 static int testfdt_ofdata_to_platdata(struct udevice
*dev
)
40 struct dm_test_pdata
*pdata
= dev_get_platdata(dev
);
42 pdata
->ping_add
= fdtdec_get_int(gd
->fdt_blob
, dev_of_offset(dev
),
44 pdata
->base
= fdtdec_get_addr(gd
->fdt_blob
, dev_of_offset(dev
),
50 static int testfdt_drv_probe(struct udevice
*dev
)
52 struct dm_test_priv
*priv
= dev_get_priv(dev
);
54 priv
->ping_total
+= DM_TEST_START_TOTAL
;
57 * If this device is on a bus, the uclass_flag will be set before
58 * calling this function. This is used by
59 * dm_test_bus_child_pre_probe_uclass().
61 priv
->uclass_total
+= priv
->uclass_flag
;
66 static const struct udevice_id testfdt_ids
[] = {
68 .compatible
= "denx,u-boot-fdt-test",
69 .data
= DM_TEST_TYPE_FIRST
},
71 .compatible
= "google,another-fdt-test",
72 .data
= DM_TEST_TYPE_SECOND
},
76 U_BOOT_DRIVER(testfdt_drv
) = {
77 .name
= "testfdt_drv",
78 .of_match
= testfdt_ids
,
79 .id
= UCLASS_TEST_FDT
,
80 .ofdata_to_platdata
= testfdt_ofdata_to_platdata
,
81 .probe
= testfdt_drv_probe
,
83 .priv_auto_alloc_size
= sizeof(struct dm_test_priv
),
84 .platdata_auto_alloc_size
= sizeof(struct dm_test_pdata
),
87 /* From here is the testfdt uclass code */
88 int testfdt_ping(struct udevice
*dev
, int pingval
, int *pingret
)
90 const struct test_ops
*ops
= device_get_ops(dev
);
95 return ops
->ping(dev
, pingval
, pingret
);
98 UCLASS_DRIVER(testfdt
) = {
100 .id
= UCLASS_TEST_FDT
,
101 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
104 struct dm_testprobe_pdata
{
108 static int testprobe_drv_probe(struct udevice
*dev
)
110 struct dm_testprobe_pdata
*pdata
= dev_get_platdata(dev
);
112 return pdata
->probe_err
;
115 static const struct udevice_id testprobe_ids
[] = {
116 { .compatible
= "denx,u-boot-probe-test" },
120 U_BOOT_DRIVER(testprobe_drv
) = {
121 .name
= "testprobe_drv",
122 .of_match
= testprobe_ids
,
123 .id
= UCLASS_TEST_PROBE
,
124 .probe
= testprobe_drv_probe
,
125 .platdata_auto_alloc_size
= sizeof(struct dm_testprobe_pdata
),
128 UCLASS_DRIVER(testprobe
) = {
130 .id
= UCLASS_TEST_PROBE
,
131 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
134 int dm_check_devices(struct unit_test_state
*uts
, int num_devices
)
141 * Now check that the ping adds are what we expect. This is using the
142 * ping-add property in each node.
144 for (i
= 0; i
< num_devices
; i
++) {
147 ret
= uclass_get_device(UCLASS_TEST_FDT
, i
, &dev
);
151 * Get the 'ping-expect' property, which tells us what the
152 * ping add should be. We don't use the platdata because we
153 * want to test the code that sets that up
154 * (testfdt_drv_probe()).
156 base
= fdtdec_get_addr(gd
->fdt_blob
, dev_of_offset(dev
),
158 debug("dev=%d, base=%d: %s\n", i
, base
,
159 fdt_get_name(gd
->fdt_blob
, dev_of_offset(dev
), NULL
));
161 ut_assert(!dm_check_operations(uts
, dev
, base
,
168 /* Test that FDT-based binding works correctly */
169 static int dm_test_fdt(struct unit_test_state
*uts
)
171 const int num_devices
= 7;
177 ret
= dm_scan_fdt(gd
->fdt_blob
, false);
180 ret
= uclass_get(UCLASS_TEST_FDT
, &uc
);
183 /* These are num_devices compatible root-level device tree nodes */
184 ut_asserteq(num_devices
, list_count_items(&uc
->dev_head
));
186 /* Each should have platform data but no private data */
187 for (i
= 0; i
< num_devices
; i
++) {
188 ret
= uclass_find_device(UCLASS_TEST_FDT
, i
, &dev
);
190 ut_assert(!dev_get_priv(dev
));
191 ut_assert(dev
->platdata
);
194 ut_assertok(dm_check_devices(uts
, num_devices
));
198 DM_TEST(dm_test_fdt
, 0);
200 static int dm_test_fdt_pre_reloc(struct unit_test_state
*uts
)
205 ret
= dm_scan_fdt(gd
->fdt_blob
, true);
208 ret
= uclass_get(UCLASS_TEST_FDT
, &uc
);
211 /* These is only one pre-reloc device */
212 ut_asserteq(1, list_count_items(&uc
->dev_head
));
216 DM_TEST(dm_test_fdt_pre_reloc
, 0);
218 /* Test that sequence numbers are allocated properly */
219 static int dm_test_fdt_uclass_seq(struct unit_test_state
*uts
)
223 /* A few basic santiy tests */
224 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT
, 3, true, &dev
));
225 ut_asserteq_str("b-test", dev
->name
);
227 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT
, 8, true, &dev
));
228 ut_asserteq_str("a-test", dev
->name
);
230 ut_asserteq(-ENODEV
, uclass_find_device_by_seq(UCLASS_TEST_FDT
, 5,
232 ut_asserteq_ptr(NULL
, dev
);
235 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 6, &dev
));
236 ut_asserteq_str("e-test", dev
->name
);
238 ut_asserteq(-ENODEV
, uclass_find_device_by_seq(UCLASS_TEST_FDT
, 7,
242 * Note that c-test nodes are not probed since it is not a top-level
245 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 3, &dev
));
246 ut_asserteq_str("b-test", dev
->name
);
249 * d-test wants sequence number 3 also, but it can't have it because
250 * b-test gets it first.
252 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 2, &dev
));
253 ut_asserteq_str("d-test", dev
->name
);
255 /* d-test actually gets 0 */
256 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 0, &dev
));
257 ut_asserteq_str("d-test", dev
->name
);
259 /* initially no one wants seq 1 */
260 ut_asserteq(-ENODEV
, uclass_get_device_by_seq(UCLASS_TEST_FDT
, 1,
262 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 0, &dev
));
263 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 4, &dev
));
265 /* But now that it is probed, we can find it */
266 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 1, &dev
));
267 ut_asserteq_str("f-test", dev
->name
);
271 DM_TEST(dm_test_fdt_uclass_seq
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
273 /* Test that we can find a device by device tree offset */
274 static int dm_test_fdt_offset(struct unit_test_state
*uts
)
276 const void *blob
= gd
->fdt_blob
;
280 node
= fdt_path_offset(blob
, "/e-test");
282 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT
, node
,
284 ut_asserteq_str("e-test", dev
->name
);
286 /* This node should not be bound */
287 node
= fdt_path_offset(blob
, "/junk");
289 ut_asserteq(-ENODEV
, uclass_get_device_by_of_offset(UCLASS_TEST_FDT
,
292 /* This is not a top level node so should not be probed */
293 node
= fdt_path_offset(blob
, "/some-bus/c-test@5");
295 ut_asserteq(-ENODEV
, uclass_get_device_by_of_offset(UCLASS_TEST_FDT
,
300 DM_TEST(dm_test_fdt_offset
,
301 DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
| DM_TESTF_FLAT_TREE
);
304 * Test various error conditions with uclass_first_device() and
305 * uclass_next_device()
307 static int dm_test_first_next_device(struct unit_test_state
*uts
)
309 struct dm_testprobe_pdata
*pdata
;
310 struct udevice
*dev
, *parent
= NULL
;
314 /* There should be 4 devices */
315 for (ret
= uclass_first_device(UCLASS_TEST_PROBE
, &dev
), count
= 0;
317 ret
= uclass_next_device(&dev
)) {
319 parent
= dev_get_parent(dev
);
322 ut_asserteq(4, count
);
324 /* Remove them and try again, with an error on the second one */
325 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE
, 1, &dev
));
326 pdata
= dev_get_platdata(dev
);
327 pdata
->probe_err
= -ENOMEM
;
328 device_remove(parent
, DM_REMOVE_NORMAL
);
329 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE
, &dev
));
330 ut_asserteq(-ENOMEM
, uclass_next_device(&dev
));
331 ut_asserteq_ptr(dev
, NULL
);
333 /* Now an error on the first one */
334 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE
, 0, &dev
));
335 pdata
= dev_get_platdata(dev
);
336 pdata
->probe_err
= -ENOENT
;
337 device_remove(parent
, DM_REMOVE_NORMAL
);
338 ut_asserteq(-ENOENT
, uclass_first_device(UCLASS_TEST_PROBE
, &dev
));
342 DM_TEST(dm_test_first_next_device
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
345 * check_devices() - Check return values and pointers
347 * This runs through a full sequence of uclass_first_device_check()...
348 * uclass_next_device_check() checking that the return values and devices
352 * @devlist: List of expected devices
353 * @mask: Indicates which devices should return an error. Device n should
354 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
357 static int check_devices(struct unit_test_state
*uts
,
358 struct udevice
*devlist
[], int mask
)
364 expected_ret
= (mask
& 1) ? -ENOENT
: 0;
366 ut_asserteq(expected_ret
,
367 uclass_first_device_check(UCLASS_TEST_PROBE
, &dev
));
368 for (i
= 0; i
< 4; i
++) {
369 ut_asserteq_ptr(devlist
[i
], dev
);
370 expected_ret
= (mask
& 1) ? -ENOENT
- (i
+ 1) : 0;
372 ut_asserteq(expected_ret
, uclass_next_device_check(&dev
));
374 ut_asserteq_ptr(NULL
, dev
);
379 /* Test uclass_first_device_check() and uclass_next_device_check() */
380 static int dm_test_first_next_ok_device(struct unit_test_state
*uts
)
382 struct dm_testprobe_pdata
*pdata
;
383 struct udevice
*dev
, *parent
= NULL
, *devlist
[4];
387 /* There should be 4 devices */
389 for (ret
= uclass_first_device_check(UCLASS_TEST_PROBE
, &dev
);
391 ret
= uclass_next_device_check(&dev
)) {
393 devlist
[count
++] = dev
;
394 parent
= dev_get_parent(dev
);
396 ut_asserteq(4, count
);
397 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE
, &dev
));
398 ut_assertok(check_devices(uts
, devlist
, 0));
400 /* Remove them and try again, with an error on the second one */
401 pdata
= dev_get_platdata(devlist
[1]);
402 pdata
->probe_err
= -ENOENT
- 1;
403 device_remove(parent
, DM_REMOVE_NORMAL
);
404 ut_assertok(check_devices(uts
, devlist
, 1 << 1));
406 /* Now an error on the first one */
407 pdata
= dev_get_platdata(devlist
[0]);
408 pdata
->probe_err
= -ENOENT
- 0;
409 device_remove(parent
, DM_REMOVE_NORMAL
);
410 ut_assertok(check_devices(uts
, devlist
, 3 << 0));
412 /* Now errors on all */
413 pdata
= dev_get_platdata(devlist
[2]);
414 pdata
->probe_err
= -ENOENT
- 2;
415 pdata
= dev_get_platdata(devlist
[3]);
416 pdata
->probe_err
= -ENOENT
- 3;
417 device_remove(parent
, DM_REMOVE_NORMAL
);
418 ut_assertok(check_devices(uts
, devlist
, 0xf << 0));
422 DM_TEST(dm_test_first_next_ok_device
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
424 static const struct udevice_id fdt_dummy_ids
[] = {
425 { .compatible
= "denx,u-boot-fdt-dummy", },
429 UCLASS_DRIVER(fdt_dummy
) = {
431 .id
= UCLASS_TEST_DUMMY
,
432 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
435 U_BOOT_DRIVER(fdt_dummy_drv
) = {
436 .name
= "fdt_dummy_drv",
437 .of_match
= fdt_dummy_ids
,
438 .id
= UCLASS_TEST_DUMMY
,
441 static int dm_test_fdt_translation(struct unit_test_state
*uts
)
445 /* Some simple translations */
446 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 0, true, &dev
));
447 ut_asserteq_str("dev@0,0", dev
->name
);
448 ut_asserteq(0x8000, dev_read_addr(dev
));
450 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 1, true, &dev
));
451 ut_asserteq_str("dev@1,100", dev
->name
);
452 ut_asserteq(0x9000, dev_read_addr(dev
));
454 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 2, true, &dev
));
455 ut_asserteq_str("dev@2,200", dev
->name
);
456 ut_asserteq(0xA000, dev_read_addr(dev
));
458 /* No translation for busses with #size-cells == 0 */
459 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 3, true, &dev
));
460 ut_asserteq_str("dev@42", dev
->name
);
461 ut_asserteq(0x42, dev_read_addr(dev
));
465 DM_TEST(dm_test_fdt_translation
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
467 /* Test devfdt_remap_addr_index() */
468 static int dm_test_fdt_remap_addr_flat(struct unit_test_state
*uts
)
474 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 0, true, &dev
));
476 addr
= devfdt_get_addr(dev
);
477 ut_asserteq(0x8000, addr
);
479 paddr
= map_physmem(addr
, 0, MAP_NOCACHE
);
480 ut_assertnonnull(paddr
);
481 ut_asserteq_ptr(paddr
, devfdt_remap_addr(dev
));
485 DM_TEST(dm_test_fdt_remap_addr_flat
,
486 DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
| DM_TESTF_FLAT_TREE
);
488 /* Test dev_remap_addr_index() */
489 static int dm_test_fdt_remap_addr_live(struct unit_test_state
*uts
)
495 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 0, true, &dev
));
497 addr
= dev_read_addr(dev
);
498 ut_asserteq(0x8000, addr
);
500 paddr
= map_physmem(addr
, 0, MAP_NOCACHE
);
501 ut_assertnonnull(paddr
);
502 ut_asserteq_ptr(paddr
, dev_remap_addr(dev
));
506 DM_TEST(dm_test_fdt_remap_addr_live
,
507 DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
509 static int dm_test_fdt_livetree_writing(struct unit_test_state
*uts
)
514 if (!of_live_active()) {
515 printf("Live tree not active; ignore test\n");
519 /* Test enabling devices */
521 node
= ofnode_path("/usb@2");
523 ut_assert(!of_device_is_available(ofnode_to_np(node
)));
524 ofnode_set_enabled(node
, true);
525 ut_assert(of_device_is_available(ofnode_to_np(node
)));
527 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node
,
529 ut_assertok(uclass_find_device_by_seq(UCLASS_USB
, 2, true, &dev
));
531 /* Test string property setting */
533 ut_assert(device_is_compatible(dev
, "sandbox,usb"));
534 ofnode_write_string(node
, "compatible", "gdsys,super-usb");
535 ut_assert(device_is_compatible(dev
, "gdsys,super-usb"));
536 ofnode_write_string(node
, "compatible", "sandbox,usb");
537 ut_assert(device_is_compatible(dev
, "sandbox,usb"));
539 /* Test setting generic properties */
541 /* Non-existent in DTB */
542 ut_asserteq(FDT_ADDR_T_NONE
, dev_read_addr(dev
));
543 /* reg = 0x42, size = 0x100 */
544 ut_assertok(ofnode_write_prop(node
, "reg", 8,
545 "\x00\x00\x00\x42\x00\x00\x01\x00"));
546 ut_asserteq(0x42, dev_read_addr(dev
));
548 /* Test disabling devices */
550 device_remove(dev
, DM_REMOVE_NORMAL
);
553 ut_assert(of_device_is_available(ofnode_to_np(node
)));
554 ofnode_set_enabled(node
, false);
555 ut_assert(!of_device_is_available(ofnode_to_np(node
)));
559 DM_TEST(dm_test_fdt_livetree_writing
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);