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>
19 DECLARE_GLOBAL_DATA_PTR
;
21 static int testfdt_drv_ping(struct udevice
*dev
, int pingval
, int *pingret
)
23 const struct dm_test_pdata
*pdata
= dev
->platdata
;
24 struct dm_test_priv
*priv
= dev_get_priv(dev
);
26 *pingret
= pingval
+ pdata
->ping_add
;
27 priv
->ping_total
+= *pingret
;
32 static const struct test_ops test_ops
= {
33 .ping
= testfdt_drv_ping
,
36 static int testfdt_ofdata_to_platdata(struct udevice
*dev
)
38 struct dm_test_pdata
*pdata
= dev_get_platdata(dev
);
40 pdata
->ping_add
= fdtdec_get_int(gd
->fdt_blob
, dev_of_offset(dev
),
42 pdata
->base
= fdtdec_get_addr(gd
->fdt_blob
, dev_of_offset(dev
),
48 static int testfdt_drv_probe(struct udevice
*dev
)
50 struct dm_test_priv
*priv
= dev_get_priv(dev
);
52 priv
->ping_total
+= DM_TEST_START_TOTAL
;
55 * If this device is on a bus, the uclass_flag will be set before
56 * calling this function. This is used by
57 * dm_test_bus_child_pre_probe_uclass().
59 priv
->uclass_total
+= priv
->uclass_flag
;
64 static const struct udevice_id testfdt_ids
[] = {
66 .compatible
= "denx,u-boot-fdt-test",
67 .data
= DM_TEST_TYPE_FIRST
},
69 .compatible
= "google,another-fdt-test",
70 .data
= DM_TEST_TYPE_SECOND
},
74 U_BOOT_DRIVER(testfdt_drv
) = {
75 .name
= "testfdt_drv",
76 .of_match
= testfdt_ids
,
77 .id
= UCLASS_TEST_FDT
,
78 .ofdata_to_platdata
= testfdt_ofdata_to_platdata
,
79 .probe
= testfdt_drv_probe
,
81 .priv_auto_alloc_size
= sizeof(struct dm_test_priv
),
82 .platdata_auto_alloc_size
= sizeof(struct dm_test_pdata
),
85 /* From here is the testfdt uclass code */
86 int testfdt_ping(struct udevice
*dev
, int pingval
, int *pingret
)
88 const struct test_ops
*ops
= device_get_ops(dev
);
93 return ops
->ping(dev
, pingval
, pingret
);
96 UCLASS_DRIVER(testfdt
) = {
98 .id
= UCLASS_TEST_FDT
,
99 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
102 struct dm_testprobe_pdata
{
106 static int testprobe_drv_probe(struct udevice
*dev
)
108 struct dm_testprobe_pdata
*pdata
= dev_get_platdata(dev
);
110 return pdata
->probe_err
;
113 static const struct udevice_id testprobe_ids
[] = {
114 { .compatible
= "denx,u-boot-probe-test" },
118 U_BOOT_DRIVER(testprobe_drv
) = {
119 .name
= "testprobe_drv",
120 .of_match
= testprobe_ids
,
121 .id
= UCLASS_TEST_PROBE
,
122 .probe
= testprobe_drv_probe
,
123 .platdata_auto_alloc_size
= sizeof(struct dm_testprobe_pdata
),
126 UCLASS_DRIVER(testprobe
) = {
128 .id
= UCLASS_TEST_PROBE
,
129 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
132 int dm_check_devices(struct unit_test_state
*uts
, int num_devices
)
139 * Now check that the ping adds are what we expect. This is using the
140 * ping-add property in each node.
142 for (i
= 0; i
< num_devices
; i
++) {
145 ret
= uclass_get_device(UCLASS_TEST_FDT
, i
, &dev
);
149 * Get the 'ping-expect' property, which tells us what the
150 * ping add should be. We don't use the platdata because we
151 * want to test the code that sets that up
152 * (testfdt_drv_probe()).
154 base
= fdtdec_get_addr(gd
->fdt_blob
, dev_of_offset(dev
),
156 debug("dev=%d, base=%d: %s\n", i
, base
,
157 fdt_get_name(gd
->fdt_blob
, dev_of_offset(dev
), NULL
));
159 ut_assert(!dm_check_operations(uts
, dev
, base
,
166 /* Test that FDT-based binding works correctly */
167 static int dm_test_fdt(struct unit_test_state
*uts
)
169 const int num_devices
= 7;
175 ret
= dm_scan_fdt(gd
->fdt_blob
, false);
178 ret
= uclass_get(UCLASS_TEST_FDT
, &uc
);
181 /* These are num_devices compatible root-level device tree nodes */
182 ut_asserteq(num_devices
, list_count_items(&uc
->dev_head
));
184 /* Each should have platform data but no private data */
185 for (i
= 0; i
< num_devices
; i
++) {
186 ret
= uclass_find_device(UCLASS_TEST_FDT
, i
, &dev
);
188 ut_assert(!dev_get_priv(dev
));
189 ut_assert(dev
->platdata
);
192 ut_assertok(dm_check_devices(uts
, num_devices
));
196 DM_TEST(dm_test_fdt
, 0);
198 static int dm_test_fdt_pre_reloc(struct unit_test_state
*uts
)
203 ret
= dm_scan_fdt(gd
->fdt_blob
, true);
206 ret
= uclass_get(UCLASS_TEST_FDT
, &uc
);
209 /* These is only one pre-reloc device */
210 ut_asserteq(1, list_count_items(&uc
->dev_head
));
214 DM_TEST(dm_test_fdt_pre_reloc
, 0);
216 /* Test that sequence numbers are allocated properly */
217 static int dm_test_fdt_uclass_seq(struct unit_test_state
*uts
)
221 /* A few basic santiy tests */
222 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT
, 3, true, &dev
));
223 ut_asserteq_str("b-test", dev
->name
);
225 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT
, 8, true, &dev
));
226 ut_asserteq_str("a-test", dev
->name
);
228 ut_asserteq(-ENODEV
, uclass_find_device_by_seq(UCLASS_TEST_FDT
, 5,
230 ut_asserteq_ptr(NULL
, dev
);
233 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 6, &dev
));
234 ut_asserteq_str("e-test", dev
->name
);
236 ut_asserteq(-ENODEV
, uclass_find_device_by_seq(UCLASS_TEST_FDT
, 7,
240 * Note that c-test nodes are not probed since it is not a top-level
243 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 3, &dev
));
244 ut_asserteq_str("b-test", dev
->name
);
247 * d-test wants sequence number 3 also, but it can't have it because
248 * b-test gets it first.
250 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 2, &dev
));
251 ut_asserteq_str("d-test", dev
->name
);
253 /* d-test actually gets 0 */
254 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 0, &dev
));
255 ut_asserteq_str("d-test", dev
->name
);
257 /* initially no one wants seq 1 */
258 ut_asserteq(-ENODEV
, uclass_get_device_by_seq(UCLASS_TEST_FDT
, 1,
260 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 0, &dev
));
261 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 4, &dev
));
263 /* But now that it is probed, we can find it */
264 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 1, &dev
));
265 ut_asserteq_str("f-test", dev
->name
);
269 DM_TEST(dm_test_fdt_uclass_seq
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
271 /* Test that we can find a device by device tree offset */
272 static int dm_test_fdt_offset(struct unit_test_state
*uts
)
274 const void *blob
= gd
->fdt_blob
;
278 node
= fdt_path_offset(blob
, "/e-test");
280 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT
, node
,
282 ut_asserteq_str("e-test", dev
->name
);
284 /* This node should not be bound */
285 node
= fdt_path_offset(blob
, "/junk");
287 ut_asserteq(-ENODEV
, uclass_get_device_by_of_offset(UCLASS_TEST_FDT
,
290 /* This is not a top level node so should not be probed */
291 node
= fdt_path_offset(blob
, "/some-bus/c-test@5");
293 ut_asserteq(-ENODEV
, uclass_get_device_by_of_offset(UCLASS_TEST_FDT
,
298 DM_TEST(dm_test_fdt_offset
,
299 DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
| DM_TESTF_FLAT_TREE
);
302 * Test various error conditions with uclass_first_device() and
303 * uclass_next_device()
305 static int dm_test_first_next_device(struct unit_test_state
*uts
)
307 struct dm_testprobe_pdata
*pdata
;
308 struct udevice
*dev
, *parent
= NULL
;
312 /* There should be 4 devices */
313 for (ret
= uclass_first_device(UCLASS_TEST_PROBE
, &dev
), count
= 0;
315 ret
= uclass_next_device(&dev
)) {
317 parent
= dev_get_parent(dev
);
320 ut_asserteq(4, count
);
322 /* Remove them and try again, with an error on the second one */
323 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE
, 1, &dev
));
324 pdata
= dev_get_platdata(dev
);
325 pdata
->probe_err
= -ENOMEM
;
326 device_remove(parent
, DM_REMOVE_NORMAL
);
327 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE
, &dev
));
328 ut_asserteq(-ENOMEM
, uclass_next_device(&dev
));
329 ut_asserteq_ptr(dev
, NULL
);
331 /* Now an error on the first one */
332 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE
, 0, &dev
));
333 pdata
= dev_get_platdata(dev
);
334 pdata
->probe_err
= -ENOENT
;
335 device_remove(parent
, DM_REMOVE_NORMAL
);
336 ut_asserteq(-ENOENT
, uclass_first_device(UCLASS_TEST_PROBE
, &dev
));
340 DM_TEST(dm_test_first_next_device
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
343 * check_devices() - Check return values and pointers
345 * This runs through a full sequence of uclass_first_device_check()...
346 * uclass_next_device_check() checking that the return values and devices
350 * @devlist: List of expected devices
351 * @mask: Indicates which devices should return an error. Device n should
352 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
355 static int check_devices(struct unit_test_state
*uts
,
356 struct udevice
*devlist
[], int mask
)
362 expected_ret
= (mask
& 1) ? -ENOENT
: 0;
364 ut_asserteq(expected_ret
,
365 uclass_first_device_check(UCLASS_TEST_PROBE
, &dev
));
366 for (i
= 0; i
< 4; i
++) {
367 ut_asserteq_ptr(devlist
[i
], dev
);
368 expected_ret
= (mask
& 1) ? -ENOENT
- (i
+ 1) : 0;
370 ut_asserteq(expected_ret
, uclass_next_device_check(&dev
));
372 ut_asserteq_ptr(NULL
, dev
);
377 /* Test uclass_first_device_check() and uclass_next_device_check() */
378 static int dm_test_first_next_ok_device(struct unit_test_state
*uts
)
380 struct dm_testprobe_pdata
*pdata
;
381 struct udevice
*dev
, *parent
= NULL
, *devlist
[4];
385 /* There should be 4 devices */
387 for (ret
= uclass_first_device_check(UCLASS_TEST_PROBE
, &dev
);
389 ret
= uclass_next_device_check(&dev
)) {
391 devlist
[count
++] = dev
;
392 parent
= dev_get_parent(dev
);
394 ut_asserteq(4, count
);
395 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE
, &dev
));
396 ut_assertok(check_devices(uts
, devlist
, 0));
398 /* Remove them and try again, with an error on the second one */
399 pdata
= dev_get_platdata(devlist
[1]);
400 pdata
->probe_err
= -ENOENT
- 1;
401 device_remove(parent
, DM_REMOVE_NORMAL
);
402 ut_assertok(check_devices(uts
, devlist
, 1 << 1));
404 /* Now an error on the first one */
405 pdata
= dev_get_platdata(devlist
[0]);
406 pdata
->probe_err
= -ENOENT
- 0;
407 device_remove(parent
, DM_REMOVE_NORMAL
);
408 ut_assertok(check_devices(uts
, devlist
, 3 << 0));
410 /* Now errors on all */
411 pdata
= dev_get_platdata(devlist
[2]);
412 pdata
->probe_err
= -ENOENT
- 2;
413 pdata
= dev_get_platdata(devlist
[3]);
414 pdata
->probe_err
= -ENOENT
- 3;
415 device_remove(parent
, DM_REMOVE_NORMAL
);
416 ut_assertok(check_devices(uts
, devlist
, 0xf << 0));
420 DM_TEST(dm_test_first_next_ok_device
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
422 static const struct udevice_id fdt_dummy_ids
[] = {
423 { .compatible
= "denx,u-boot-fdt-dummy", },
427 UCLASS_DRIVER(fdt_dummy
) = {
429 .id
= UCLASS_TEST_DUMMY
,
430 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
433 U_BOOT_DRIVER(fdt_dummy_drv
) = {
434 .name
= "fdt_dummy_drv",
435 .of_match
= fdt_dummy_ids
,
436 .id
= UCLASS_TEST_DUMMY
,
439 static int dm_test_fdt_translation(struct unit_test_state
*uts
)
443 /* Some simple translations */
444 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 0, true, &dev
));
445 ut_asserteq_str("dev@0,0", dev
->name
);
446 ut_asserteq(0x8000, dev_read_addr(dev
));
448 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 1, true, &dev
));
449 ut_asserteq_str("dev@1,100", dev
->name
);
450 ut_asserteq(0x9000, dev_read_addr(dev
));
452 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 2, true, &dev
));
453 ut_asserteq_str("dev@2,200", dev
->name
);
454 ut_asserteq(0xA000, dev_read_addr(dev
));
456 /* No translation for busses with #size-cells == 0 */
457 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 3, true, &dev
));
458 ut_asserteq_str("dev@42", dev
->name
);
459 ut_asserteq(0x42, dev_read_addr(dev
));
463 DM_TEST(dm_test_fdt_translation
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
465 /* Test devfdt_remap_addr_index() */
466 static int dm_test_fdt_remap_addr_flat(struct unit_test_state
*uts
)
472 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 0, true, &dev
));
474 addr
= devfdt_get_addr(dev
);
475 ut_asserteq(0x8000, addr
);
477 paddr
= map_physmem(addr
, 0, MAP_NOCACHE
);
478 ut_assertnonnull(paddr
);
479 ut_asserteq_ptr(paddr
, devfdt_remap_addr(dev
));
483 DM_TEST(dm_test_fdt_remap_addr_flat
,
484 DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
| DM_TESTF_FLAT_TREE
);
486 /* Test dev_remap_addr_index() */
487 static int dm_test_fdt_remap_addr_live(struct unit_test_state
*uts
)
493 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY
, 0, true, &dev
));
495 addr
= dev_read_addr(dev
);
496 ut_asserteq(0x8000, addr
);
498 paddr
= map_physmem(addr
, 0, MAP_NOCACHE
);
499 ut_assertnonnull(paddr
);
500 ut_asserteq_ptr(paddr
, dev_remap_addr(dev
));
504 DM_TEST(dm_test_fdt_remap_addr_live
,
505 DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);