blob: 18c89eef43fce6a03dff7693e1b004e303737fa1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glassb2c1cac2014-02-26 15:59:21 -07004 */
5
Simon Glassb2c1cac2014-02-26 15:59:21 -07006#include <dm.h>
7#include <errno.h>
8#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070010#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070012#include <asm/io.h>
13#include <dm/test.h>
Nikhil M Jainbb9d1312023-01-31 15:35:15 +053014#include <dm/read.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070015#include <dm/root.h>
Simon Glass9c433fe2017-04-23 20:10:44 -060016#include <dm/device-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <dm/devres.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070018#include <dm/uclass-internal.h>
19#include <dm/util.h>
Mario Six058ef5b2018-06-26 08:46:49 +020020#include <dm/of_access.h>
Patrick Delaunayda37ed32021-05-21 12:25:00 +020021#include <linux/ioport.h>
Simon Glass75c4d412020-07-19 10:15:37 -060022#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050023#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070024
25DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass9c433fe2017-04-23 20:10:44 -060027struct dm_testprobe_pdata {
28 int probe_err;
29};
30
31static int testprobe_drv_probe(struct udevice *dev)
32{
Simon Glassfa20e932020-12-03 16:55:20 -070033 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-04-23 20:10:44 -060034
35 return pdata->probe_err;
36}
37
38static const struct udevice_id testprobe_ids[] = {
39 { .compatible = "denx,u-boot-probe-test" },
40 { }
41};
42
43U_BOOT_DRIVER(testprobe_drv) = {
44 .name = "testprobe_drv",
45 .of_match = testprobe_ids,
46 .id = UCLASS_TEST_PROBE,
47 .probe = testprobe_drv_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -070048 .plat_auto = sizeof(struct dm_testprobe_pdata),
Simon Glass9c433fe2017-04-23 20:10:44 -060049};
50
51UCLASS_DRIVER(testprobe) = {
52 .name = "testprobe",
53 .id = UCLASS_TEST_PROBE,
54 .flags = DM_UC_FLAG_SEQ_ALIAS,
55};
56
Simon Glass204675c2019-12-29 21:19:25 -070057struct dm_testdevres_pdata {
58 void *ptr;
59};
60
61struct dm_testdevres_priv {
62 void *ptr;
Simon Glass0fe15372019-12-29 21:19:28 -070063 void *ptr_ofdata;
Simon Glass204675c2019-12-29 21:19:25 -070064};
65
66static int testdevres_drv_bind(struct udevice *dev)
67{
Simon Glassfa20e932020-12-03 16:55:20 -070068 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
Simon Glass204675c2019-12-29 21:19:25 -070069
70 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
71
72 return 0;
73}
74
Simon Glassaad29ae2020-12-03 16:55:21 -070075static int testdevres_drv_of_to_plat(struct udevice *dev)
Simon Glass0fe15372019-12-29 21:19:28 -070076{
77 struct dm_testdevres_priv *priv = dev_get_priv(dev);
78
79 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
80
81 return 0;
82}
83
Simon Glass204675c2019-12-29 21:19:25 -070084static int testdevres_drv_probe(struct udevice *dev)
85{
86 struct dm_testdevres_priv *priv = dev_get_priv(dev);
87
88 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
89
90 return 0;
91}
92
93static const struct udevice_id testdevres_ids[] = {
94 { .compatible = "denx,u-boot-devres-test" },
95 { }
96};
97
98U_BOOT_DRIVER(testdevres_drv) = {
99 .name = "testdevres_drv",
100 .of_match = testdevres_ids,
101 .id = UCLASS_TEST_DEVRES,
102 .bind = testdevres_drv_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700103 .of_to_plat = testdevres_drv_of_to_plat,
Simon Glass204675c2019-12-29 21:19:25 -0700104 .probe = testdevres_drv_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700105 .plat_auto = sizeof(struct dm_testdevres_pdata),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700106 .priv_auto = sizeof(struct dm_testdevres_priv),
Simon Glass204675c2019-12-29 21:19:25 -0700107};
108
109UCLASS_DRIVER(testdevres) = {
110 .name = "testdevres",
111 .id = UCLASS_TEST_DEVRES,
112 .flags = DM_UC_FLAG_SEQ_ALIAS,
113};
114
Joe Hershberger3a77be52015-05-20 14:27:27 -0500115int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700116{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200117 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700118 int ret;
119 int i;
120
Simon Glassb2c1cac2014-02-26 15:59:21 -0700121 /*
122 * Now check that the ping adds are what we expect. This is using the
123 * ping-add property in each node.
124 */
Simon Glass40717422014-07-23 06:55:18 -0600125 for (i = 0; i < num_devices; i++) {
Simon Glassb2c1cac2014-02-26 15:59:21 -0700126 uint32_t base;
127
128 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
129 ut_assert(!ret);
130
131 /*
Simon Glassa7bb08a2014-07-23 06:54:57 -0600132 * Get the 'ping-expect' property, which tells us what the
Simon Glass71fa5b42020-12-03 16:55:18 -0700133 * ping add should be. We don't use the plat because we
Simon Glassa7bb08a2014-07-23 06:54:57 -0600134 * want to test the code that sets that up
135 * (testfdt_drv_probe()).
Simon Glassb2c1cac2014-02-26 15:59:21 -0700136 */
Marek Vasutdd7af672023-08-23 21:37:44 +0200137 base = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
138 "ping-expect", -1);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700139 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glassdd79d6e2017-01-17 16:52:55 -0700140 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700141
Joe Hershberger3a77be52015-05-20 14:27:27 -0500142 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700143 dev_get_priv(dev)));
144 }
145
Simon Glass40717422014-07-23 06:55:18 -0600146 return 0;
147}
148
149/* Test that FDT-based binding works correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500150static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -0600151{
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530152 const int num_devices = 9;
Simon Glass40717422014-07-23 06:55:18 -0600153 struct udevice *dev;
154 struct uclass *uc;
155 int ret;
156 int i;
157
Simon Glass4d71d602020-11-28 17:50:10 -0700158 ret = dm_extended_scan(false);
Simon Glass40717422014-07-23 06:55:18 -0600159 ut_assert(!ret);
160
161 ret = uclass_get(UCLASS_TEST_FDT, &uc);
162 ut_assert(!ret);
163
164 /* These are num_devices compatible root-level device tree nodes */
165 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
166
Simon Glassba750492015-01-25 08:27:00 -0700167 /* Each should have platform data but no private data */
Simon Glass40717422014-07-23 06:55:18 -0600168 for (i = 0; i < num_devices; i++) {
169 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
170 ut_assert(!ret);
171 ut_assert(!dev_get_priv(dev));
Simon Glass95588622020-12-22 19:30:28 -0700172 ut_assert(dev_get_plat(dev));
Simon Glass40717422014-07-23 06:55:18 -0600173 }
174
Joe Hershberger3a77be52015-05-20 14:27:27 -0500175 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -0600176
Simon Glassb2c1cac2014-02-26 15:59:21 -0700177 return 0;
178}
179DM_TEST(dm_test_fdt, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600180
Michal Simek4247cf42019-01-31 16:31:00 +0100181static int dm_test_alias_highest_id(struct unit_test_state *uts)
182{
183 int ret;
184
Michael Walle7efcdfd2021-02-25 16:51:11 +0100185 ret = dev_read_alias_highest_id("ethernet");
Sean Anderson67d93a42022-05-05 13:11:30 -0400186 ut_asserteq(8, ret);
Michal Simek4247cf42019-01-31 16:31:00 +0100187
188 ret = dev_read_alias_highest_id("gpio");
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100189 ut_asserteq(3, ret);
Michal Simek4247cf42019-01-31 16:31:00 +0100190
191 ret = dev_read_alias_highest_id("pci");
192 ut_asserteq(2, ret);
193
194 ret = dev_read_alias_highest_id("i2c");
195 ut_asserteq(0, ret);
196
197 ret = dev_read_alias_highest_id("deadbeef");
198 ut_asserteq(-1, ret);
199
200 return 0;
201}
202DM_TEST(dm_test_alias_highest_id, 0);
203
Joe Hershberger3a77be52015-05-20 14:27:27 -0500204static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600205{
206 struct uclass *uc;
207 int ret;
208
Simon Glass5039cab2020-11-28 17:50:09 -0700209 ret = dm_scan_fdt(true);
Simon Glassfef72b72014-07-23 06:55:03 -0600210 ut_assert(!ret);
211
212 ret = uclass_get(UCLASS_TEST_FDT, &uc);
213 ut_assert(!ret);
214
Bin Mengd9d24782018-10-10 22:07:01 -0700215 /*
216 * These are 2 pre-reloc devices:
Simon Glassfc1aa352023-02-13 08:56:34 -0700217 * one with "bootph-all" property (a-test node), and the other
Bin Mengd9d24782018-10-10 22:07:01 -0700218 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
219 */
220 ut_asserteq(2, list_count_items(&uc->dev_head));
Simon Glassfef72b72014-07-23 06:55:03 -0600221
222 return 0;
223}
224DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glassdb6f0202014-07-23 06:55:12 -0600225
226/* Test that sequence numbers are allocated properly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500227static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glassdb6f0202014-07-23 06:55:12 -0600228{
229 struct udevice *dev;
230
231 /* A few basic santiy tests */
Simon Glass07e13382020-12-16 21:20:29 -0700232 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600233 ut_asserteq_str("b-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700234 ut_asserteq(3, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600235
Simon Glass07e13382020-12-16 21:20:29 -0700236 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600237 ut_asserteq_str("a-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700238 ut_asserteq(8, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600239
Simon Glass7d5e4112020-12-16 21:20:26 -0700240 /*
241 * This device has no alias so gets the next value after all available
242 * aliases. The last alias is testfdt12
243 */
Simon Glass07e13382020-12-16 21:20:29 -0700244 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev));
Simon Glass7d5e4112020-12-16 21:20:26 -0700245 ut_asserteq_str("d-test", dev->name);
246 ut_asserteq(13, dev_seq(dev));
247
Simon Glass07e13382020-12-16 21:20:29 -0700248 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass7d5e4112020-12-16 21:20:26 -0700249 &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600250 ut_asserteq_ptr(NULL, dev);
251
252 /* Test aliases */
253 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
254 ut_asserteq_str("e-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700255 ut_asserteq(6, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600256
Simon Glass40717422014-07-23 06:55:18 -0600257 /*
258 * Note that c-test nodes are not probed since it is not a top-level
259 * node
260 */
Simon Glassdb6f0202014-07-23 06:55:12 -0600261 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
262 ut_asserteq_str("b-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700263 ut_asserteq(3, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600264
265 /*
266 * d-test wants sequence number 3 also, but it can't have it because
267 * b-test gets it first.
268 */
269 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
270 ut_asserteq_str("d-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700271 ut_asserteq(13, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600272
Simon Glass7d5e4112020-12-16 21:20:26 -0700273 /* g-test gets the next value after f-test */
274 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 15, &dev));
275 ut_asserteq_str("g-test", dev->name);
276 ut_asserteq(15, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600277
Simon Glass7d5e4112020-12-16 21:20:26 -0700278 /* And we should still have holes in our sequence numbers */
Simon Glass07e13382020-12-16 21:20:29 -0700279 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0,
Simon Glass7d5e4112020-12-16 21:20:26 -0700280 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700281 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1,
Simon Glass7d5e4112020-12-16 21:20:26 -0700282 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700283 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
Simon Glass7d5e4112020-12-16 21:20:26 -0700284 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700285 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
Simon Glass7d5e4112020-12-16 21:20:26 -0700286 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700287 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
Simon Glass7d5e4112020-12-16 21:20:26 -0700288 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700289 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass7d5e4112020-12-16 21:20:26 -0700290 &dev));
291 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
Simon Glass07e13382020-12-16 21:20:29 -0700292 &dev));
Simon Glass7d5e4112020-12-16 21:20:26 -0700293 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
Simon Glass07e13382020-12-16 21:20:29 -0700294 &dev));
Michael Walle7c41a222020-06-02 01:47:09 +0200295
Simon Glassdb6f0202014-07-23 06:55:12 -0600296 return 0;
297}
Simon Glass974dccd2020-07-28 19:41:12 -0600298DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassc1464ab2014-07-23 06:55:14 -0600299
Simon Glass791a17f2020-12-16 21:20:27 -0700300/* More tests for sequence numbers */
301static int dm_test_fdt_uclass_seq_manual(struct unit_test_state *uts)
302{
303 struct udevice *dev;
304
305 /*
306 * Since DM_UC_FLAG_NO_AUTO_SEQ is set for this uclass, only testfdtm1
307 * should get a sequence number assigned
308 */
309 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 0, &dev));
310 ut_asserteq_str("testfdtm0", dev->name);
311 ut_asserteq(-1, dev_seq(dev));
312
313 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT_MANUAL, 1, &dev));
314 ut_asserteq_str("testfdtm1", dev->name);
315 ut_asserteq(1, dev_seq(dev));
316
317 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 2, &dev));
318 ut_asserteq_str("testfdtm2", dev->name);
319 ut_asserteq(-1, dev_seq(dev));
320
321 return 0;
322}
323DM_TEST(dm_test_fdt_uclass_seq_manual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
324
Simon Glass38040a92020-12-16 21:20:32 -0700325static int dm_test_fdt_uclass_seq_more(struct unit_test_state *uts)
326{
327 struct udevice *dev;
328 ofnode node;
329
330 /* Check creating a device with an alias */
331 node = ofnode_path("/some-bus/c-test@1");
Simon Glassdf56e0b2021-02-03 06:01:09 -0700332 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass38040a92020-12-16 21:20:32 -0700333 "c-test@1", NULL, node, &dev));
334 ut_asserteq(12, dev_seq(dev));
335 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 12, &dev));
336 ut_asserteq_str("c-test@1", dev->name);
337
338 /*
339 * Now bind a device without an alias. It should not get the next
340 * sequence number after all aliases, and existing bound devices. The
341 * last alias is 12, so we have:
342 *
343 * 13 d-test
344 * 14 f-test
345 * 15 g-test
346 * 16 h-test
347 * 17 another-test
348 * 18 chosen-test
349 *
350 * So next available is 19
351 */
Simon Glassdf56e0b2021-02-03 06:01:09 -0700352 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass38040a92020-12-16 21:20:32 -0700353 "fred", NULL, ofnode_null(), &dev));
354 ut_asserteq(19, dev_seq(dev));
355
Simon Glassdf56e0b2021-02-03 06:01:09 -0700356 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass38040a92020-12-16 21:20:32 -0700357 "fred2", NULL, ofnode_null(), &dev));
358 ut_asserteq(20, dev_seq(dev));
359
360 return 0;
361}
362DM_TEST(dm_test_fdt_uclass_seq_more, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
363
Simon Glassc1464ab2014-07-23 06:55:14 -0600364/* Test that we can find a device by device tree offset */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500365static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassc1464ab2014-07-23 06:55:14 -0600366{
367 const void *blob = gd->fdt_blob;
368 struct udevice *dev;
369 int node;
370
371 node = fdt_path_offset(blob, "/e-test");
372 ut_assert(node > 0);
373 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
374 &dev));
375 ut_asserteq_str("e-test", dev->name);
376
377 /* This node should not be bound */
378 node = fdt_path_offset(blob, "/junk");
379 ut_assert(node > 0);
380 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
381 node, &dev));
382
383 /* This is not a top level node so should not be probed */
Simon Glass40717422014-07-23 06:55:18 -0600384 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassc1464ab2014-07-23 06:55:14 -0600385 ut_assert(node > 0);
386 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
387 node, &dev));
388
389 return 0;
390}
Simon Glass70b66e72017-05-18 20:09:44 -0600391DM_TEST(dm_test_fdt_offset,
Simon Glass974dccd2020-07-28 19:41:12 -0600392 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass9c433fe2017-04-23 20:10:44 -0600393
394/**
Michal Suchanekf5a59022022-10-12 21:57:50 +0200395 * Test various error conditions with uclass_first_device(),
396 * uclass_next_device(), and uclass_probe_all()
Simon Glass9c433fe2017-04-23 20:10:44 -0600397 */
Michal Suchanekf5a59022022-10-12 21:57:50 +0200398static int dm_test_first_next_device_probeall(struct unit_test_state *uts)
Simon Glass9c433fe2017-04-23 20:10:44 -0600399{
400 struct dm_testprobe_pdata *pdata;
401 struct udevice *dev, *parent = NULL;
402 int count;
403 int ret;
404
405 /* There should be 4 devices */
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200406 for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
Simon Glass9c433fe2017-04-23 20:10:44 -0600407 dev;
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200408 uclass_next_device(&dev)) {
Simon Glass9c433fe2017-04-23 20:10:44 -0600409 count++;
410 parent = dev_get_parent(dev);
411 }
Simon Glass9c433fe2017-04-23 20:10:44 -0600412 ut_asserteq(4, count);
413
414 /* Remove them and try again, with an error on the second one */
415 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
Simon Glassfa20e932020-12-03 16:55:20 -0700416 pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-04-23 20:10:44 -0600417 pdata->probe_err = -ENOMEM;
418 device_remove(parent, DM_REMOVE_NORMAL);
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200419 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev),
420 count = 0;
421 dev;
422 ret = uclass_next_device_check(&dev)) {
423 if (!ret)
424 count++;
425 else
426 ut_asserteq(-ENOMEM, ret);
427 parent = dev_get_parent(dev);
428 }
429 ut_asserteq(3, count);
Simon Glass9c433fe2017-04-23 20:10:44 -0600430
431 /* Now an error on the first one */
432 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
Simon Glassfa20e932020-12-03 16:55:20 -0700433 pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-04-23 20:10:44 -0600434 pdata->probe_err = -ENOENT;
435 device_remove(parent, DM_REMOVE_NORMAL);
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200436 for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
437 dev;
438 uclass_next_device(&dev)) {
439 count++;
440 parent = dev_get_parent(dev);
441 }
442 ut_asserteq(2, count);
Simon Glass9c433fe2017-04-23 20:10:44 -0600443
Michal Suchanekf5a59022022-10-12 21:57:50 +0200444 /* Now that broken devices are set up test probe_all */
445 device_remove(parent, DM_REMOVE_NORMAL);
446 /* There are broken devices so an error should be returned */
447 ut_assert(uclass_probe_all(UCLASS_TEST_PROBE) < 0);
448 /* but non-error device should be probed nonetheless */
449 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 2, &dev));
450 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
451 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 3, &dev));
452 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
453
Simon Glass9c433fe2017-04-23 20:10:44 -0600454 return 0;
455}
Michal Suchanekf5a59022022-10-12 21:57:50 +0200456DM_TEST(dm_test_first_next_device_probeall,
457 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600458
Simon Glass27c0e172020-02-06 09:54:49 -0700459/* Test iteration through devices in a uclass */
460static int dm_test_uclass_foreach(struct unit_test_state *uts)
461{
462 struct udevice *dev;
463 struct uclass *uc;
464 int count;
465
466 count = 0;
467 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
468 count++;
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530469 ut_asserteq(9, count);
Simon Glass27c0e172020-02-06 09:54:49 -0700470
471 count = 0;
472 uclass_foreach_dev(dev, uc)
473 count++;
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530474 ut_asserteq(9, count);
Simon Glass27c0e172020-02-06 09:54:49 -0700475
476 return 0;
477}
Simon Glass974dccd2020-07-28 19:41:12 -0600478DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass27c0e172020-02-06 09:54:49 -0700479
Simon Glass3ff83bc2017-04-23 20:10:45 -0600480/**
481 * check_devices() - Check return values and pointers
482 *
483 * This runs through a full sequence of uclass_first_device_check()...
484 * uclass_next_device_check() checking that the return values and devices
485 * are correct.
486 *
487 * @uts: Test state
488 * @devlist: List of expected devices
489 * @mask: Indicates which devices should return an error. Device n should
490 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
491 * bit n is clear.
492 */
493static int check_devices(struct unit_test_state *uts,
494 struct udevice *devlist[], int mask)
495{
496 int expected_ret;
497 struct udevice *dev;
498 int i;
499
500 expected_ret = (mask & 1) ? -ENOENT : 0;
501 mask >>= 1;
502 ut_asserteq(expected_ret,
503 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
504 for (i = 0; i < 4; i++) {
505 ut_asserteq_ptr(devlist[i], dev);
506 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
507 mask >>= 1;
508 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
509 }
510 ut_asserteq_ptr(NULL, dev);
511
512 return 0;
513}
514
515/* Test uclass_first_device_check() and uclass_next_device_check() */
516static int dm_test_first_next_ok_device(struct unit_test_state *uts)
517{
518 struct dm_testprobe_pdata *pdata;
519 struct udevice *dev, *parent = NULL, *devlist[4];
520 int count;
521 int ret;
522
523 /* There should be 4 devices */
524 count = 0;
525 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
526 dev;
527 ret = uclass_next_device_check(&dev)) {
528 ut_assertok(ret);
529 devlist[count++] = dev;
530 parent = dev_get_parent(dev);
531 }
532 ut_asserteq(4, count);
533 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
534 ut_assertok(check_devices(uts, devlist, 0));
535
536 /* Remove them and try again, with an error on the second one */
Simon Glassfa20e932020-12-03 16:55:20 -0700537 pdata = dev_get_plat(devlist[1]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600538 pdata->probe_err = -ENOENT - 1;
539 device_remove(parent, DM_REMOVE_NORMAL);
540 ut_assertok(check_devices(uts, devlist, 1 << 1));
541
542 /* Now an error on the first one */
Simon Glassfa20e932020-12-03 16:55:20 -0700543 pdata = dev_get_plat(devlist[0]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600544 pdata->probe_err = -ENOENT - 0;
545 device_remove(parent, DM_REMOVE_NORMAL);
546 ut_assertok(check_devices(uts, devlist, 3 << 0));
547
548 /* Now errors on all */
Simon Glassfa20e932020-12-03 16:55:20 -0700549 pdata = dev_get_plat(devlist[2]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600550 pdata->probe_err = -ENOENT - 2;
Simon Glassfa20e932020-12-03 16:55:20 -0700551 pdata = dev_get_plat(devlist[3]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600552 pdata->probe_err = -ENOENT - 3;
553 device_remove(parent, DM_REMOVE_NORMAL);
554 ut_assertok(check_devices(uts, devlist, 0xf << 0));
555
556 return 0;
557}
Simon Glass974dccd2020-07-28 19:41:12 -0600558DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six35616ef2018-03-12 14:53:33 +0100559
560static const struct udevice_id fdt_dummy_ids[] = {
561 { .compatible = "denx,u-boot-fdt-dummy", },
562 { }
563};
564
565UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca5ba71e52018-05-19 14:13:55 +0200566 .name = "fdt-dummy",
Mario Six35616ef2018-03-12 14:53:33 +0100567 .id = UCLASS_TEST_DUMMY,
568 .flags = DM_UC_FLAG_SEQ_ALIAS,
569};
570
571U_BOOT_DRIVER(fdt_dummy_drv) = {
572 .name = "fdt_dummy_drv",
573 .of_match = fdt_dummy_ids,
574 .id = UCLASS_TEST_DUMMY,
575};
576
577static int dm_test_fdt_translation(struct unit_test_state *uts)
578{
579 struct udevice *dev;
Fabien Dessenne22236e02019-05-31 15:11:30 +0200580 fdt32_t dma_addr[2];
Mario Six35616ef2018-03-12 14:53:33 +0100581
582 /* Some simple translations */
Simon Glass07e13382020-12-16 21:20:29 -0700583 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100584 ut_asserteq_str("dev@0,0", dev->name);
585 ut_asserteq(0x8000, dev_read_addr(dev));
586
Simon Glass07e13382020-12-16 21:20:29 -0700587 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100588 ut_asserteq_str("dev@1,100", dev->name);
589 ut_asserteq(0x9000, dev_read_addr(dev));
590
Simon Glass07e13382020-12-16 21:20:29 -0700591 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100592 ut_asserteq_str("dev@2,200", dev->name);
593 ut_asserteq(0xA000, dev_read_addr(dev));
594
Yuepeng Xing71cc0212022-12-02 14:23:07 +0800595 /* No translation for buses with #size-cells == 0 */
Simon Glass07e13382020-12-16 21:20:29 -0700596 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100597 ut_asserteq_str("dev@42", dev->name);
598 ut_asserteq(0x42, dev_read_addr(dev));
599
Fabien Dessenne22236e02019-05-31 15:11:30 +0200600 /* dma address translation */
Simon Glass07e13382020-12-16 21:20:29 -0700601 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Fabien Dessenne22236e02019-05-31 15:11:30 +0200602 dma_addr[0] = cpu_to_be32(0);
603 dma_addr[1] = cpu_to_be32(0);
604 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
605
Simon Glass07e13382020-12-16 21:20:29 -0700606 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Fabien Dessenne22236e02019-05-31 15:11:30 +0200607 dma_addr[0] = cpu_to_be32(1);
608 dma_addr[1] = cpu_to_be32(0x100);
609 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
610
Mario Six35616ef2018-03-12 14:53:33 +0100611 return 0;
612}
Simon Glass974dccd2020-07-28 19:41:12 -0600613DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200614
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300615static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
616{
617 struct udevice *gpio, *dev;
618 void *ptr;
Johan Jonker86648602023-04-23 11:19:41 +0200619 void *paddr;
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300620
621 /* Test for missing reg property */
622 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
623 ut_assertnull(devfdt_get_addr_ptr(gpio));
624
Simon Glass07e13382020-12-16 21:20:29 -0700625 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300626 ptr = devfdt_get_addr_ptr(dev);
Johan Jonker86648602023-04-23 11:19:41 +0200627
628 paddr = map_sysmem(0x8000, 0);
629 ut_asserteq_ptr(paddr, ptr);
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300630
631 return 0;
632}
633DM_TEST(dm_test_fdt_get_addr_ptr_flat,
634 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
635
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200636static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
637{
638 struct udevice *dev;
639 fdt_addr_t addr;
640 void *paddr;
641
Simon Glass07e13382020-12-16 21:20:29 -0700642 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200643
644 addr = devfdt_get_addr(dev);
645 ut_asserteq(0x8000, addr);
646
647 paddr = map_physmem(addr, 0, MAP_NOCACHE);
648 ut_assertnonnull(paddr);
649 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
650
651 return 0;
652}
653DM_TEST(dm_test_fdt_remap_addr_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600654 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200655
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100656static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
657{
658 struct udevice *dev;
659 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530660 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100661 void *paddr;
662
Simon Glass07e13382020-12-16 21:20:29 -0700663 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100664
Sekhar Norif677c6f2019-08-01 19:12:56 +0530665 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100666 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530667 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100668
669 paddr = map_physmem(addr, 0, MAP_NOCACHE);
670 ut_assertnonnull(paddr);
671 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
672
673 return 0;
674}
675DM_TEST(dm_test_fdt_remap_addr_index_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600676 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100677
678static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
679{
680 struct udevice *dev;
681 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530682 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100683 void *paddr;
684
Simon Glass07e13382020-12-16 21:20:29 -0700685 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100686
Sekhar Norif677c6f2019-08-01 19:12:56 +0530687 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100688 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530689 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100690
691 paddr = map_physmem(addr, 0, MAP_NOCACHE);
692 ut_assertnonnull(paddr);
693 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
694
695 return 0;
696}
697DM_TEST(dm_test_fdt_remap_addr_name_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600698 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100699
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200700static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
701{
702 struct udevice *dev;
703 fdt_addr_t addr;
704 void *paddr;
705
Simon Glass07e13382020-12-16 21:20:29 -0700706 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200707
708 addr = dev_read_addr(dev);
709 ut_asserteq(0x8000, addr);
710
711 paddr = map_physmem(addr, 0, MAP_NOCACHE);
712 ut_assertnonnull(paddr);
713 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
714
715 return 0;
716}
717DM_TEST(dm_test_fdt_remap_addr_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600718 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six058ef5b2018-06-26 08:46:49 +0200719
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100720static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
721{
722 struct udevice *dev;
723 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530724 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100725 void *paddr;
726
Simon Glass07e13382020-12-16 21:20:29 -0700727 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100728
Sekhar Norif677c6f2019-08-01 19:12:56 +0530729 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100730 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530731 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100732
733 paddr = map_physmem(addr, 0, MAP_NOCACHE);
734 ut_assertnonnull(paddr);
735 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
736
737 return 0;
738}
739DM_TEST(dm_test_fdt_remap_addr_index_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600740 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100741
742static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
743{
744 struct udevice *dev;
745 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530746 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100747 void *paddr;
748
Simon Glass07e13382020-12-16 21:20:29 -0700749 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100750
Sekhar Norif677c6f2019-08-01 19:12:56 +0530751 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100752 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530753 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100754
755 paddr = map_physmem(addr, 0, MAP_NOCACHE);
756 ut_assertnonnull(paddr);
757 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
758
759 return 0;
760}
761DM_TEST(dm_test_fdt_remap_addr_name_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600762 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100763
Mario Sixd6f6dc32018-06-26 08:46:51 +0200764static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
765{
766 ofnode node;
767
768 if (!of_live_active()) {
769 printf("Live tree not active; ignore test\n");
770 return 0;
771 }
772
773 node = ofnode_path("/usb@2");
774
775 /* Test enabling devices */
776
777 ut_assert(!of_device_is_available(ofnode_to_np(node)));
778 dev_enable_by_path("/usb@2");
779 ut_assert(of_device_is_available(ofnode_to_np(node)));
780
781 /* Test disabling devices */
782
783 ut_assert(of_device_is_available(ofnode_to_np(node)));
784 dev_disable_by_path("/usb@2");
785 ut_assert(!of_device_is_available(ofnode_to_np(node)));
786
787 return 0;
788}
Simon Glass974dccd2020-07-28 19:41:12 -0600789DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
790 UT_TESTF_SCAN_FDT);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700791
792/* Test a few uclass phandle functions */
793static int dm_test_fdt_phandle(struct unit_test_state *uts)
794{
795 struct udevice *back, *dev, *dev2;
796
797 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200798 ut_assertnonnull(back);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700799 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
800 back, "missing", &dev));
801 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
802 "power-supply", &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200803 ut_assertnonnull(dev);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700804 ut_asserteq(0, device_active(dev));
805 ut_asserteq_str("ldo1", dev->name);
806 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
807 "power-supply", &dev2));
808 ut_asserteq_ptr(dev, dev2);
809
810 return 0;
811}
Simon Glass974dccd2020-07-28 19:41:12 -0600812DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa11341a2018-11-18 08:14:31 -0700813
814/* Test device_find_first_child_by_uclass() */
815static int dm_test_first_child(struct unit_test_state *uts)
816{
817 struct udevice *i2c, *dev, *dev2;
818
819 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
820 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
821 ut_asserteq_str("rtc@43", dev->name);
822 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
823 ut_asserteq_ptr(dev, dev2);
824 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
825 ut_asserteq_str("rtc@61", dev2->name);
826
827 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
828 &dev));
829 ut_asserteq_str("eeprom@2c", dev->name);
830 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
831 ut_asserteq_ptr(dev, dev2);
832
833 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
834 UCLASS_VIDEO, &dev));
835 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
836
837 return 0;
838}
Simon Glass974dccd2020-07-28 19:41:12 -0600839DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass6df01f92018-12-10 10:37:37 -0700840
841/* Test integer functions in dm_read_...() */
842static int dm_test_read_int(struct unit_test_state *uts)
843{
844 struct udevice *dev;
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200845 u8 val8;
846 u16 val16;
Simon Glass6df01f92018-12-10 10:37:37 -0700847 u32 val32;
848 s32 sval;
849 uint val;
Dario Binacchi421e81e2020-03-29 18:04:40 +0200850 u64 val64;
Simon Glass6df01f92018-12-10 10:37:37 -0700851
852 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
853 ut_asserteq_str("a-test", dev->name);
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200854
855 ut_assertok(dev_read_u8(dev, "int8-value", &val8));
856 ut_asserteq(0x12, val8);
857
858 ut_asserteq(-EINVAL, dev_read_u8(dev, "missing", &val8));
859 ut_asserteq(6, dev_read_u8_default(dev, "missing", 6));
860
861 ut_asserteq(0x12, dev_read_u8_default(dev, "int8-value", 6));
862
863 ut_assertok(dev_read_u16(dev, "int16-value", &val16));
864 ut_asserteq(0x1234, val16);
865
866 ut_asserteq(-EINVAL, dev_read_u16(dev, "missing", &val16));
867 ut_asserteq(6, dev_read_u16_default(dev, "missing", 6));
868
869 ut_asserteq(0x1234, dev_read_u16_default(dev, "int16-value", 6));
870
Simon Glass6df01f92018-12-10 10:37:37 -0700871 ut_assertok(dev_read_u32(dev, "int-value", &val32));
872 ut_asserteq(1234, val32);
873
874 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
875 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
876
877 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
878 ut_asserteq(1234, val32);
879
880 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
881 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
882
883 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
884 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
885 ut_asserteq(-1234, sval);
886
887 val = 0;
888 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
889 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
890 ut_asserteq(-1234, val);
891
Dario Binacchi421e81e2020-03-29 18:04:40 +0200892 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
893 ut_asserteq_64(0x1111222233334444, val64);
894
895 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
896 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
897
898 ut_asserteq_64(0x1111222233334444,
899 dev_read_u64_default(dev, "int64-value", 6));
900
Simon Glass6df01f92018-12-10 10:37:37 -0700901 return 0;
902}
Simon Glass974dccd2020-07-28 19:41:12 -0600903DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass35cb2a42020-02-06 09:54:50 -0700904
Dario Binacchi81d80b52020-03-29 18:04:41 +0200905static int dm_test_read_int_index(struct unit_test_state *uts)
906{
907 struct udevice *dev;
908 u32 val32;
909
910 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
911 ut_asserteq_str("a-test", dev->name);
912
913 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
914 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
915
916 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
917 ut_asserteq(5678, val32);
918 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
919 ut_asserteq(9123, val32);
920 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
921 ut_asserteq(4567, val32);
922 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
923 &val32));
924
925 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
926 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
927 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
928 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
929
930 return 0;
931}
Simon Glass974dccd2020-07-28 19:41:12 -0600932DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi81d80b52020-03-29 18:04:41 +0200933
Patrick Delaunay8cd28012020-09-25 09:41:16 +0200934static int dm_test_read_phandle(struct unit_test_state *uts)
935{
936 struct udevice *dev;
937 struct ofnode_phandle_args args;
938 int ret;
939 const char prop[] = "test-gpios";
940 const char cell[] = "#gpio-cells";
941 const char prop2[] = "phandle-value";
942
943 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
944 ut_asserteq_str("a-test", dev->name);
945
946 /* Test dev_count_phandle_with_args with cell name */
947 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
948 ut_asserteq(-ENOENT, ret);
949 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
950 ut_asserteq(-EINVAL, ret);
951 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
952
953 /* Test dev_read_phandle_with_args with cell name */
954 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
955 ut_asserteq(-ENOENT, ret);
956 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
957 ut_asserteq(-EINVAL, ret);
958 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
959 ut_asserteq(1, args.args_count);
960 ut_asserteq(1, args.args[0]);
961 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
962 ut_asserteq(1, args.args_count);
963 ut_asserteq(4, args.args[0]);
964 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
965 ut_asserteq(5, args.args_count);
966 ut_asserteq(5, args.args[0]);
967 ut_asserteq(1, args.args[4]);
968 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
969 ut_asserteq(-ENOENT, ret);
970 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
971 ut_asserteq(1, args.args_count);
972 ut_asserteq(12, args.args[0]);
973 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
974 ut_asserteq(-ENOENT, ret);
975
976 /* Test dev_count_phandle_with_args with cell count */
977 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
978 ut_asserteq(-ENOENT, ret);
979 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
980
981 /* Test dev_read_phandle_with_args with cell count */
982 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
983 ut_asserteq(1, ofnode_valid(args.node));
984 ut_asserteq(1, args.args_count);
985 ut_asserteq(10, args.args[0]);
986 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
987 ut_asserteq(-EINVAL, ret);
988 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
989 ut_asserteq(1, ofnode_valid(args.node));
990 ut_asserteq(1, args.args_count);
991 ut_asserteq(30, args.args[0]);
992 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
993 ut_asserteq(-ENOENT, ret);
994
995 return 0;
996}
997DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
998
Simon Glass35cb2a42020-02-06 09:54:50 -0700999/* Test iteration through devices by drvdata */
1000static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1001{
1002 struct udevice *dev;
1003
1004 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1005 DM_TEST_TYPE_FIRST, &dev));
1006 ut_asserteq_str("a-test", dev->name);
1007
1008 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1009 DM_TEST_TYPE_SECOND, &dev));
1010 ut_asserteq_str("d-test", dev->name);
1011
1012 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1013 DM_TEST_TYPE_COUNT,
1014 &dev));
1015
1016 return 0;
1017}
Simon Glass974dccd2020-07-28 19:41:12 -06001018DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Tom Rinib7b18382020-02-11 10:58:41 -05001019
Simon Glass000676b2020-01-27 08:49:47 -07001020/* Test device_first_child_ofdata_err(), etc. */
1021static int dm_test_child_ofdata(struct unit_test_state *uts)
1022{
1023 struct udevice *bus, *dev;
1024 int count;
1025
1026 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1027 count = 0;
Simon Glassaad29ae2020-12-03 16:55:21 -07001028 device_foreach_child_of_to_plat(dev, bus) {
Simon Glass6211d762020-12-19 10:40:10 -07001029 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1030 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glass000676b2020-01-27 08:49:47 -07001031 count++;
1032 }
1033 ut_asserteq(3, count);
1034
1035 return 0;
1036}
Simon Glass974dccd2020-07-28 19:41:12 -06001037DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass76db9b72020-01-27 08:49:48 -07001038
1039/* Test device_first_child_err(), etc. */
1040static int dm_test_first_child_probe(struct unit_test_state *uts)
1041{
1042 struct udevice *bus, *dev;
1043 int count;
1044
1045 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1046 count = 0;
1047 device_foreach_child_probe(dev, bus) {
Simon Glass6211d762020-12-19 10:40:10 -07001048 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1049 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass76db9b72020-01-27 08:49:48 -07001050 count++;
1051 }
1052 ut_asserteq(3, count);
1053
1054 return 0;
1055}
Simon Glass974dccd2020-07-28 19:41:12 -06001056DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass44c47892020-04-05 15:38:19 -06001057
1058/* Test that ofdata is read for parents before children */
1059static int dm_test_ofdata_order(struct unit_test_state *uts)
1060{
1061 struct udevice *bus, *dev;
1062
1063 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1064 ut_assertnonnull(bus);
Simon Glass6211d762020-12-19 10:40:10 -07001065 ut_assert(!(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID));
Simon Glass44c47892020-04-05 15:38:19 -06001066
1067 ut_assertok(device_find_first_child(bus, &dev));
1068 ut_assertnonnull(dev);
Simon Glass6211d762020-12-19 10:40:10 -07001069 ut_assert(!(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID));
Simon Glass44c47892020-04-05 15:38:19 -06001070
1071 /* read the child's ofdata which should cause the parent's to be read */
Simon Glassaad29ae2020-12-03 16:55:21 -07001072 ut_assertok(device_of_to_plat(dev));
Simon Glass6211d762020-12-19 10:40:10 -07001073 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1074 ut_assert(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID);
Simon Glass44c47892020-04-05 15:38:19 -06001075
Simon Glass6211d762020-12-19 10:40:10 -07001076 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
1077 ut_assert(!(dev_get_flags(bus) & DM_FLAG_ACTIVATED));
Simon Glass44c47892020-04-05 15:38:19 -06001078
1079 return 0;
1080}
Simon Glass974dccd2020-07-28 19:41:12 -06001081DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi836cc9d2020-12-30 00:16:26 +01001082
1083/* Test dev_decode_display_timing() */
1084static int dm_test_decode_display_timing(struct unit_test_state *uts)
1085{
1086 struct udevice *dev;
1087 struct display_timing timing;
1088
1089 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1090 ut_asserteq_str("a-test", dev->name);
1091
1092 ut_assertok(dev_decode_display_timing(dev, 0, &timing));
1093 ut_assert(timing.hactive.typ == 240);
1094 ut_assert(timing.hback_porch.typ == 7);
1095 ut_assert(timing.hfront_porch.typ == 6);
1096 ut_assert(timing.hsync_len.typ == 1);
1097 ut_assert(timing.vactive.typ == 320);
1098 ut_assert(timing.vback_porch.typ == 5);
1099 ut_assert(timing.vfront_porch.typ == 8);
1100 ut_assert(timing.vsync_len.typ == 2);
1101 ut_assert(timing.pixelclock.typ == 6500000);
1102 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH);
1103 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1104 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1105 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW);
1106 ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH);
1107 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1108 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE);
1109 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1110 ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED);
1111 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN);
1112 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK);
1113
1114 ut_assertok(dev_decode_display_timing(dev, 1, &timing));
1115 ut_assert(timing.hactive.typ == 480);
1116 ut_assert(timing.hback_porch.typ == 59);
1117 ut_assert(timing.hfront_porch.typ == 10);
1118 ut_assert(timing.hsync_len.typ == 12);
1119 ut_assert(timing.vactive.typ == 800);
1120 ut_assert(timing.vback_porch.typ == 15);
1121 ut_assert(timing.vfront_porch.typ == 17);
1122 ut_assert(timing.vsync_len.typ == 16);
1123 ut_assert(timing.pixelclock.typ == 9000000);
1124 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1125 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_LOW);
1126 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH);
1127 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1128 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1129 ut_assert(timing.flags & DISPLAY_FLAGS_DE_LOW);
1130 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1131 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE);
1132 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1133 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1134 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1135
1136 ut_assertok(dev_decode_display_timing(dev, 2, &timing));
1137 ut_assert(timing.hactive.typ == 800);
1138 ut_assert(timing.hback_porch.typ == 89);
1139 ut_assert(timing.hfront_porch.typ == 164);
1140 ut_assert(timing.hsync_len.typ == 11);
1141 ut_assert(timing.vactive.typ == 480);
1142 ut_assert(timing.vback_porch.typ == 23);
1143 ut_assert(timing.vfront_porch.typ == 10);
1144 ut_assert(timing.vsync_len.typ == 13);
1145 ut_assert(timing.pixelclock.typ == 33500000);
1146 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1147 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1148 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1149 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1150 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1151 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1152 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1153 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1154 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1155 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1156 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1157
1158 ut_assert(dev_decode_display_timing(dev, 3, &timing));
1159 return 0;
1160}
1161DM_TEST(dm_test_decode_display_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Patrick Delaunayda37ed32021-05-21 12:25:00 +02001162
Nikhil M Jainbb9d1312023-01-31 15:35:15 +05301163/* Test dev_decode_panel_timing() */
1164static int dm_test_decode_panel_timing(struct unit_test_state *uts)
1165{
1166 struct udevice *dev;
1167 struct display_timing timing;
1168
1169 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1170 ut_asserteq_str("a-test", dev->name);
1171
1172 ut_assertok(dev_decode_panel_timing(dev, &timing));
1173 ut_assert(timing.hactive.typ == 240);
1174 ut_assert(timing.hback_porch.typ == 7);
1175 ut_assert(timing.hfront_porch.typ == 6);
1176 ut_assert(timing.hsync_len.typ == 1);
1177 ut_assert(timing.vactive.typ == 320);
1178 ut_assert(timing.vback_porch.typ == 5);
1179 ut_assert(timing.vfront_porch.typ == 8);
1180 ut_assert(timing.vsync_len.typ == 2);
1181 ut_assert(timing.pixelclock.typ == 6500000);
1182 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH);
1183 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1184 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1185 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW);
1186 ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH);
1187 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1188 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE);
1189 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1190 ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED);
1191 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN);
1192 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK);
1193
1194 return 0;
1195}
1196DM_TEST(dm_test_decode_panel_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1197
Patrick Delaunayda37ed32021-05-21 12:25:00 +02001198/* Test read_resourcee() */
1199static int dm_test_read_resource(struct unit_test_state *uts)
1200{
1201 struct udevice *dev;
1202 struct resource res;
1203
1204 /* test resource without translation */
1205 ut_assertok(uclass_find_device_by_name(UCLASS_SIMPLE_BUS, "syscon@2", &dev));
1206 ut_assertok(dev_read_resource(dev, 0, &res));
1207 ut_asserteq(0x40, res.start);
1208 ut_asserteq(0x44, res.end);
1209 ut_assertok(dev_read_resource(dev, 1, &res));
1210 ut_asserteq(0x48, res.start);
1211 ut_asserteq(0x4d, res.end);
1212
1213 /* test resource with translation */
1214 ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@1,100", &dev));
1215 ut_assertok(dev_read_resource(dev, 0, &res));
1216 ut_asserteq(0x9000, res.start);
1217 ut_asserteq(0x9fff, res.end);
1218
1219 /* test named resource */
1220 ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@0,0", &dev));
1221 ut_assertok(dev_read_resource_byname(dev, "sandbox-dummy-0", &res));
1222 ut_asserteq(0x8000, res.start);
1223 ut_asserteq(0x8fff, res.end);
1224
1225 return 0;
1226}
1227
1228DM_TEST(dm_test_read_resource, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);