2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
16 #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
,
42 pdata
->base
= fdtdec_get_addr(gd
->fdt_blob
, dev
->of_offset
,
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
;
57 static const struct udevice_id testfdt_ids
[] = {
59 .compatible
= "denx,u-boot-fdt-test",
60 .data
= DM_TEST_TYPE_FIRST
},
62 .compatible
= "google,another-fdt-test",
63 .data
= DM_TEST_TYPE_SECOND
},
67 U_BOOT_DRIVER(testfdt_drv
) = {
68 .name
= "testfdt_drv",
69 .of_match
= testfdt_ids
,
70 .id
= UCLASS_TEST_FDT
,
71 .ofdata_to_platdata
= testfdt_ofdata_to_platdata
,
72 .probe
= testfdt_drv_probe
,
74 .priv_auto_alloc_size
= sizeof(struct dm_test_priv
),
75 .platdata_auto_alloc_size
= sizeof(struct dm_test_pdata
),
78 /* From here is the testfdt uclass code */
79 int testfdt_ping(struct udevice
*dev
, int pingval
, int *pingret
)
81 const struct test_ops
*ops
= device_get_ops(dev
);
86 return ops
->ping(dev
, pingval
, pingret
);
89 UCLASS_DRIVER(testfdt
) = {
91 .id
= UCLASS_TEST_FDT
,
92 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
95 int dm_check_devices(struct dm_test_state
*dms
, int num_devices
)
102 * Now check that the ping adds are what we expect. This is using the
103 * ping-add property in each node.
105 for (i
= 0; i
< num_devices
; i
++) {
108 ret
= uclass_get_device(UCLASS_TEST_FDT
, i
, &dev
);
112 * Get the 'ping-expect' property, which tells us what the
113 * ping add should be. We don't use the platdata because we
114 * want to test the code that sets that up
115 * (testfdt_drv_probe()).
117 base
= fdtdec_get_addr(gd
->fdt_blob
, dev
->of_offset
,
119 debug("dev=%d, base=%d: %s\n", i
, base
,
120 fdt_get_name(gd
->fdt_blob
, dev
->of_offset
, NULL
));
122 ut_assert(!dm_check_operations(dms
, dev
, base
,
129 /* Test that FDT-based binding works correctly */
130 static int dm_test_fdt(struct dm_test_state
*dms
)
132 const int num_devices
= 6;
138 ret
= dm_scan_fdt(gd
->fdt_blob
, false);
141 ret
= uclass_get(UCLASS_TEST_FDT
, &uc
);
144 /* These are num_devices compatible root-level device tree nodes */
145 ut_asserteq(num_devices
, list_count_items(&uc
->dev_head
));
147 /* Each should have platform data but no private data */
148 for (i
= 0; i
< num_devices
; i
++) {
149 ret
= uclass_find_device(UCLASS_TEST_FDT
, i
, &dev
);
151 ut_assert(!dev_get_priv(dev
));
152 ut_assert(dev
->platdata
);
155 ut_assertok(dm_check_devices(dms
, num_devices
));
159 DM_TEST(dm_test_fdt
, 0);
161 static int dm_test_fdt_pre_reloc(struct dm_test_state
*dms
)
166 ret
= dm_scan_fdt(gd
->fdt_blob
, true);
169 ret
= uclass_get(UCLASS_TEST_FDT
, &uc
);
172 /* These is only one pre-reloc device */
173 ut_asserteq(1, list_count_items(&uc
->dev_head
));
177 DM_TEST(dm_test_fdt_pre_reloc
, 0);
179 /* Test that sequence numbers are allocated properly */
180 static int dm_test_fdt_uclass_seq(struct dm_test_state
*dms
)
184 /* A few basic santiy tests */
185 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT
, 3, true, &dev
));
186 ut_asserteq_str("b-test", dev
->name
);
188 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT
, 8, true, &dev
));
189 ut_asserteq_str("a-test", dev
->name
);
191 ut_asserteq(-ENODEV
, uclass_find_device_by_seq(UCLASS_TEST_FDT
, 5,
193 ut_asserteq_ptr(NULL
, dev
);
196 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 6, &dev
));
197 ut_asserteq_str("e-test", dev
->name
);
199 ut_asserteq(-ENODEV
, uclass_find_device_by_seq(UCLASS_TEST_FDT
, 7,
203 * Note that c-test nodes are not probed since it is not a top-level
206 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 3, &dev
));
207 ut_asserteq_str("b-test", dev
->name
);
210 * d-test wants sequence number 3 also, but it can't have it because
211 * b-test gets it first.
213 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 2, &dev
));
214 ut_asserteq_str("d-test", dev
->name
);
216 /* d-test actually gets 0 */
217 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 0, &dev
));
218 ut_asserteq_str("d-test", dev
->name
);
220 /* initially no one wants seq 1 */
221 ut_asserteq(-ENODEV
, uclass_get_device_by_seq(UCLASS_TEST_FDT
, 1,
223 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 0, &dev
));
224 ut_assertok(uclass_get_device(UCLASS_TEST_FDT
, 4, &dev
));
226 /* But now that it is probed, we can find it */
227 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT
, 1, &dev
));
228 ut_asserteq_str("f-test", dev
->name
);
232 DM_TEST(dm_test_fdt_uclass_seq
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
234 /* Test that we can find a device by device tree offset */
235 static int dm_test_fdt_offset(struct dm_test_state
*dms
)
237 const void *blob
= gd
->fdt_blob
;
241 node
= fdt_path_offset(blob
, "/e-test");
243 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT
, node
,
245 ut_asserteq_str("e-test", dev
->name
);
247 /* This node should not be bound */
248 node
= fdt_path_offset(blob
, "/junk");
250 ut_asserteq(-ENODEV
, uclass_get_device_by_of_offset(UCLASS_TEST_FDT
,
253 /* This is not a top level node so should not be probed */
254 node
= fdt_path_offset(blob
, "/some-bus/c-test@5");
256 ut_asserteq(-ENODEV
, uclass_get_device_by_of_offset(UCLASS_TEST_FDT
,
261 DM_TEST(dm_test_fdt_offset
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);