blob: 31effff59c2194d290a57dca8d93ff02e7bdae82 [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>
Sughosh Ganu6012c1e2024-07-30 16:41:32 +053022#include <linux/list.h>
Simon Glass75c4d412020-07-19 10:15:37 -060023#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050024#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070025
26DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass9c433fe2017-04-23 20:10:44 -060028struct dm_testprobe_pdata {
29 int probe_err;
30};
31
32static int testprobe_drv_probe(struct udevice *dev)
33{
Simon Glassfa20e932020-12-03 16:55:20 -070034 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-04-23 20:10:44 -060035
36 return pdata->probe_err;
37}
38
39static const struct udevice_id testprobe_ids[] = {
40 { .compatible = "denx,u-boot-probe-test" },
41 { }
42};
43
44U_BOOT_DRIVER(testprobe_drv) = {
45 .name = "testprobe_drv",
46 .of_match = testprobe_ids,
47 .id = UCLASS_TEST_PROBE,
48 .probe = testprobe_drv_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -070049 .plat_auto = sizeof(struct dm_testprobe_pdata),
Simon Glass9c433fe2017-04-23 20:10:44 -060050};
51
52UCLASS_DRIVER(testprobe) = {
53 .name = "testprobe",
54 .id = UCLASS_TEST_PROBE,
55 .flags = DM_UC_FLAG_SEQ_ALIAS,
56};
57
Simon Glass204675c2019-12-29 21:19:25 -070058struct dm_testdevres_pdata {
59 void *ptr;
60};
61
62struct dm_testdevres_priv {
63 void *ptr;
Simon Glass0fe15372019-12-29 21:19:28 -070064 void *ptr_ofdata;
Simon Glass204675c2019-12-29 21:19:25 -070065};
66
67static int testdevres_drv_bind(struct udevice *dev)
68{
Simon Glassfa20e932020-12-03 16:55:20 -070069 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
Simon Glass204675c2019-12-29 21:19:25 -070070
71 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
72
73 return 0;
74}
75
Simon Glassaad29ae2020-12-03 16:55:21 -070076static int testdevres_drv_of_to_plat(struct udevice *dev)
Simon Glass0fe15372019-12-29 21:19:28 -070077{
78 struct dm_testdevres_priv *priv = dev_get_priv(dev);
79
80 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
81
82 return 0;
83}
84
Simon Glass204675c2019-12-29 21:19:25 -070085static int testdevres_drv_probe(struct udevice *dev)
86{
87 struct dm_testdevres_priv *priv = dev_get_priv(dev);
88
89 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
90
91 return 0;
92}
93
94static const struct udevice_id testdevres_ids[] = {
95 { .compatible = "denx,u-boot-devres-test" },
96 { }
97};
98
99U_BOOT_DRIVER(testdevres_drv) = {
100 .name = "testdevres_drv",
101 .of_match = testdevres_ids,
102 .id = UCLASS_TEST_DEVRES,
103 .bind = testdevres_drv_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700104 .of_to_plat = testdevres_drv_of_to_plat,
Simon Glass204675c2019-12-29 21:19:25 -0700105 .probe = testdevres_drv_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700106 .plat_auto = sizeof(struct dm_testdevres_pdata),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700107 .priv_auto = sizeof(struct dm_testdevres_priv),
Simon Glass204675c2019-12-29 21:19:25 -0700108};
109
110UCLASS_DRIVER(testdevres) = {
111 .name = "testdevres",
112 .id = UCLASS_TEST_DEVRES,
113 .flags = DM_UC_FLAG_SEQ_ALIAS,
114};
115
Joe Hershberger3a77be52015-05-20 14:27:27 -0500116int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700117{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200118 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700119 int ret;
120 int i;
121
Simon Glassb2c1cac2014-02-26 15:59:21 -0700122 /*
123 * Now check that the ping adds are what we expect. This is using the
124 * ping-add property in each node.
125 */
Simon Glass40717422014-07-23 06:55:18 -0600126 for (i = 0; i < num_devices; i++) {
Simon Glassb2c1cac2014-02-26 15:59:21 -0700127 uint32_t base;
128
129 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
130 ut_assert(!ret);
131
132 /*
Simon Glassa7bb08a2014-07-23 06:54:57 -0600133 * Get the 'ping-expect' property, which tells us what the
Simon Glass71fa5b42020-12-03 16:55:18 -0700134 * ping add should be. We don't use the plat because we
Simon Glassa7bb08a2014-07-23 06:54:57 -0600135 * want to test the code that sets that up
136 * (testfdt_drv_probe()).
Simon Glassb2c1cac2014-02-26 15:59:21 -0700137 */
Marek Vasutdd7af672023-08-23 21:37:44 +0200138 base = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
139 "ping-expect", -1);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700140 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glassdd79d6e2017-01-17 16:52:55 -0700141 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700142
Joe Hershberger3a77be52015-05-20 14:27:27 -0500143 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700144 dev_get_priv(dev)));
145 }
146
Simon Glass40717422014-07-23 06:55:18 -0600147 return 0;
148}
149
150/* Test that FDT-based binding works correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500151static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -0600152{
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530153 const int num_devices = 9;
Simon Glass40717422014-07-23 06:55:18 -0600154 struct udevice *dev;
155 struct uclass *uc;
156 int ret;
157 int i;
158
Simon Glass4d71d602020-11-28 17:50:10 -0700159 ret = dm_extended_scan(false);
Simon Glass40717422014-07-23 06:55:18 -0600160 ut_assert(!ret);
161
162 ret = uclass_get(UCLASS_TEST_FDT, &uc);
163 ut_assert(!ret);
164
165 /* These are num_devices compatible root-level device tree nodes */
Sughosh Ganu6012c1e2024-07-30 16:41:32 +0530166 ut_asserteq(num_devices, list_count_nodes(&uc->dev_head));
Simon Glass40717422014-07-23 06:55:18 -0600167
Simon Glassba750492015-01-25 08:27:00 -0700168 /* Each should have platform data but no private data */
Simon Glass40717422014-07-23 06:55:18 -0600169 for (i = 0; i < num_devices; i++) {
170 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
171 ut_assert(!ret);
172 ut_assert(!dev_get_priv(dev));
Simon Glass95588622020-12-22 19:30:28 -0700173 ut_assert(dev_get_plat(dev));
Simon Glass40717422014-07-23 06:55:18 -0600174 }
175
Joe Hershberger3a77be52015-05-20 14:27:27 -0500176 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -0600177
Simon Glassb2c1cac2014-02-26 15:59:21 -0700178 return 0;
179}
180DM_TEST(dm_test_fdt, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600181
Michal Simek4247cf42019-01-31 16:31:00 +0100182static int dm_test_alias_highest_id(struct unit_test_state *uts)
183{
184 int ret;
185
Michael Walle7efcdfd2021-02-25 16:51:11 +0100186 ret = dev_read_alias_highest_id("ethernet");
Sean Anderson67d93a42022-05-05 13:11:30 -0400187 ut_asserteq(8, ret);
Michal Simek4247cf42019-01-31 16:31:00 +0100188
189 ret = dev_read_alias_highest_id("gpio");
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100190 ut_asserteq(3, ret);
Michal Simek4247cf42019-01-31 16:31:00 +0100191
192 ret = dev_read_alias_highest_id("pci");
193 ut_asserteq(2, ret);
194
195 ret = dev_read_alias_highest_id("i2c");
196 ut_asserteq(0, ret);
197
198 ret = dev_read_alias_highest_id("deadbeef");
199 ut_asserteq(-1, ret);
200
201 return 0;
202}
203DM_TEST(dm_test_alias_highest_id, 0);
204
Joe Hershberger3a77be52015-05-20 14:27:27 -0500205static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600206{
207 struct uclass *uc;
208 int ret;
209
Simon Glass5039cab2020-11-28 17:50:09 -0700210 ret = dm_scan_fdt(true);
Simon Glassfef72b72014-07-23 06:55:03 -0600211 ut_assert(!ret);
212
213 ret = uclass_get(UCLASS_TEST_FDT, &uc);
214 ut_assert(!ret);
215
Bin Mengd9d24782018-10-10 22:07:01 -0700216 /*
217 * These are 2 pre-reloc devices:
Simon Glassfc1aa352023-02-13 08:56:34 -0700218 * one with "bootph-all" property (a-test node), and the other
Bin Mengd9d24782018-10-10 22:07:01 -0700219 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
220 */
Sughosh Ganu6012c1e2024-07-30 16:41:32 +0530221 ut_asserteq(2, list_count_nodes(&uc->dev_head));
Simon Glassfef72b72014-07-23 06:55:03 -0600222
223 return 0;
224}
225DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glassdb6f0202014-07-23 06:55:12 -0600226
227/* Test that sequence numbers are allocated properly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500228static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glassdb6f0202014-07-23 06:55:12 -0600229{
230 struct udevice *dev;
231
232 /* A few basic santiy tests */
Simon Glass07e13382020-12-16 21:20:29 -0700233 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600234 ut_asserteq_str("b-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700235 ut_asserteq(3, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600236
Simon Glass07e13382020-12-16 21:20:29 -0700237 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600238 ut_asserteq_str("a-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700239 ut_asserteq(8, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600240
Simon Glass7d5e4112020-12-16 21:20:26 -0700241 /*
242 * This device has no alias so gets the next value after all available
243 * aliases. The last alias is testfdt12
244 */
Simon Glass07e13382020-12-16 21:20:29 -0700245 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev));
Simon Glass7d5e4112020-12-16 21:20:26 -0700246 ut_asserteq_str("d-test", dev->name);
247 ut_asserteq(13, dev_seq(dev));
248
Simon Glass07e13382020-12-16 21:20:29 -0700249 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass7d5e4112020-12-16 21:20:26 -0700250 &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600251 ut_asserteq_ptr(NULL, dev);
252
253 /* Test aliases */
254 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
255 ut_asserteq_str("e-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700256 ut_asserteq(6, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600257
Simon Glass40717422014-07-23 06:55:18 -0600258 /*
259 * Note that c-test nodes are not probed since it is not a top-level
260 * node
261 */
Simon Glassdb6f0202014-07-23 06:55:12 -0600262 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
263 ut_asserteq_str("b-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700264 ut_asserteq(3, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600265
266 /*
267 * d-test wants sequence number 3 also, but it can't have it because
268 * b-test gets it first.
269 */
270 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
271 ut_asserteq_str("d-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700272 ut_asserteq(13, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600273
Simon Glass7d5e4112020-12-16 21:20:26 -0700274 /* g-test gets the next value after f-test */
275 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 15, &dev));
276 ut_asserteq_str("g-test", dev->name);
277 ut_asserteq(15, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600278
Simon Glass7d5e4112020-12-16 21:20:26 -0700279 /* And we should still have holes in our sequence numbers */
Simon Glass07e13382020-12-16 21:20:29 -0700280 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0,
Simon Glass7d5e4112020-12-16 21:20:26 -0700281 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700282 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1,
Simon Glass7d5e4112020-12-16 21:20:26 -0700283 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700284 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
Simon Glass7d5e4112020-12-16 21:20:26 -0700285 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700286 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
Simon Glass7d5e4112020-12-16 21:20:26 -0700287 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700288 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
Simon Glass7d5e4112020-12-16 21:20:26 -0700289 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700290 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass7d5e4112020-12-16 21:20:26 -0700291 &dev));
292 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
Simon Glass07e13382020-12-16 21:20:29 -0700293 &dev));
Simon Glass7d5e4112020-12-16 21:20:26 -0700294 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
Simon Glass07e13382020-12-16 21:20:29 -0700295 &dev));
Michael Walle7c41a222020-06-02 01:47:09 +0200296
Simon Glassdb6f0202014-07-23 06:55:12 -0600297 return 0;
298}
Simon Glass974dccd2020-07-28 19:41:12 -0600299DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassc1464ab2014-07-23 06:55:14 -0600300
Simon Glass791a17f2020-12-16 21:20:27 -0700301/* More tests for sequence numbers */
302static int dm_test_fdt_uclass_seq_manual(struct unit_test_state *uts)
303{
304 struct udevice *dev;
305
306 /*
307 * Since DM_UC_FLAG_NO_AUTO_SEQ is set for this uclass, only testfdtm1
308 * should get a sequence number assigned
309 */
310 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 0, &dev));
311 ut_asserteq_str("testfdtm0", dev->name);
312 ut_asserteq(-1, dev_seq(dev));
313
314 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT_MANUAL, 1, &dev));
315 ut_asserteq_str("testfdtm1", dev->name);
316 ut_asserteq(1, dev_seq(dev));
317
318 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 2, &dev));
319 ut_asserteq_str("testfdtm2", dev->name);
320 ut_asserteq(-1, dev_seq(dev));
321
322 return 0;
323}
324DM_TEST(dm_test_fdt_uclass_seq_manual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
325
Simon Glass38040a92020-12-16 21:20:32 -0700326static int dm_test_fdt_uclass_seq_more(struct unit_test_state *uts)
327{
328 struct udevice *dev;
329 ofnode node;
330
331 /* Check creating a device with an alias */
332 node = ofnode_path("/some-bus/c-test@1");
Simon Glassdf56e0b2021-02-03 06:01:09 -0700333 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass38040a92020-12-16 21:20:32 -0700334 "c-test@1", NULL, node, &dev));
335 ut_asserteq(12, dev_seq(dev));
336 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 12, &dev));
337 ut_asserteq_str("c-test@1", dev->name);
338
339 /*
340 * Now bind a device without an alias. It should not get the next
341 * sequence number after all aliases, and existing bound devices. The
342 * last alias is 12, so we have:
343 *
344 * 13 d-test
345 * 14 f-test
346 * 15 g-test
347 * 16 h-test
348 * 17 another-test
349 * 18 chosen-test
350 *
351 * So next available is 19
352 */
Simon Glassdf56e0b2021-02-03 06:01:09 -0700353 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass38040a92020-12-16 21:20:32 -0700354 "fred", NULL, ofnode_null(), &dev));
355 ut_asserteq(19, dev_seq(dev));
356
Simon Glassdf56e0b2021-02-03 06:01:09 -0700357 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass38040a92020-12-16 21:20:32 -0700358 "fred2", NULL, ofnode_null(), &dev));
359 ut_asserteq(20, dev_seq(dev));
360
361 return 0;
362}
363DM_TEST(dm_test_fdt_uclass_seq_more, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
364
Simon Glassc1464ab2014-07-23 06:55:14 -0600365/* Test that we can find a device by device tree offset */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500366static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassc1464ab2014-07-23 06:55:14 -0600367{
368 const void *blob = gd->fdt_blob;
369 struct udevice *dev;
370 int node;
371
372 node = fdt_path_offset(blob, "/e-test");
373 ut_assert(node > 0);
374 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
375 &dev));
376 ut_asserteq_str("e-test", dev->name);
377
378 /* This node should not be bound */
379 node = fdt_path_offset(blob, "/junk");
380 ut_assert(node > 0);
381 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
382 node, &dev));
383
384 /* This is not a top level node so should not be probed */
Simon Glass40717422014-07-23 06:55:18 -0600385 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassc1464ab2014-07-23 06:55:14 -0600386 ut_assert(node > 0);
387 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
388 node, &dev));
389
390 return 0;
391}
Simon Glass70b66e72017-05-18 20:09:44 -0600392DM_TEST(dm_test_fdt_offset,
Simon Glass974dccd2020-07-28 19:41:12 -0600393 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass9c433fe2017-04-23 20:10:44 -0600394
395/**
Michal Suchanekf5a59022022-10-12 21:57:50 +0200396 * Test various error conditions with uclass_first_device(),
397 * uclass_next_device(), and uclass_probe_all()
Simon Glass9c433fe2017-04-23 20:10:44 -0600398 */
Michal Suchanekf5a59022022-10-12 21:57:50 +0200399static int dm_test_first_next_device_probeall(struct unit_test_state *uts)
Simon Glass9c433fe2017-04-23 20:10:44 -0600400{
401 struct dm_testprobe_pdata *pdata;
402 struct udevice *dev, *parent = NULL;
403 int count;
404 int ret;
405
406 /* There should be 4 devices */
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200407 for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
Simon Glass9c433fe2017-04-23 20:10:44 -0600408 dev;
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200409 uclass_next_device(&dev)) {
Simon Glass9c433fe2017-04-23 20:10:44 -0600410 count++;
411 parent = dev_get_parent(dev);
412 }
Simon Glass9c433fe2017-04-23 20:10:44 -0600413 ut_asserteq(4, count);
414
415 /* Remove them and try again, with an error on the second one */
416 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
Simon Glassfa20e932020-12-03 16:55:20 -0700417 pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-04-23 20:10:44 -0600418 pdata->probe_err = -ENOMEM;
419 device_remove(parent, DM_REMOVE_NORMAL);
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200420 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev),
421 count = 0;
422 dev;
423 ret = uclass_next_device_check(&dev)) {
424 if (!ret)
425 count++;
426 else
427 ut_asserteq(-ENOMEM, ret);
428 parent = dev_get_parent(dev);
429 }
430 ut_asserteq(3, count);
Simon Glass9c433fe2017-04-23 20:10:44 -0600431
432 /* Now an error on the first one */
433 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
Simon Glassfa20e932020-12-03 16:55:20 -0700434 pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-04-23 20:10:44 -0600435 pdata->probe_err = -ENOENT;
436 device_remove(parent, DM_REMOVE_NORMAL);
Michal Suchanek98e1ada2022-10-12 21:58:09 +0200437 for (uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
438 dev;
439 uclass_next_device(&dev)) {
440 count++;
441 parent = dev_get_parent(dev);
442 }
443 ut_asserteq(2, count);
Simon Glass9c433fe2017-04-23 20:10:44 -0600444
Michal Suchanekf5a59022022-10-12 21:57:50 +0200445 /* Now that broken devices are set up test probe_all */
446 device_remove(parent, DM_REMOVE_NORMAL);
447 /* There are broken devices so an error should be returned */
448 ut_assert(uclass_probe_all(UCLASS_TEST_PROBE) < 0);
449 /* but non-error device should be probed nonetheless */
450 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 2, &dev));
451 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
452 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 3, &dev));
453 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
454
Simon Glass9c433fe2017-04-23 20:10:44 -0600455 return 0;
456}
Michal Suchanekf5a59022022-10-12 21:57:50 +0200457DM_TEST(dm_test_first_next_device_probeall,
458 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600459
Simon Glass27c0e172020-02-06 09:54:49 -0700460/* Test iteration through devices in a uclass */
461static int dm_test_uclass_foreach(struct unit_test_state *uts)
462{
463 struct udevice *dev;
464 struct uclass *uc;
465 int count;
466
467 count = 0;
468 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
469 count++;
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530470 ut_asserteq(9, count);
Simon Glass27c0e172020-02-06 09:54:49 -0700471
472 count = 0;
473 uclass_foreach_dev(dev, uc)
474 count++;
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530475 ut_asserteq(9, count);
Simon Glass27c0e172020-02-06 09:54:49 -0700476
477 return 0;
478}
Simon Glass974dccd2020-07-28 19:41:12 -0600479DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass27c0e172020-02-06 09:54:49 -0700480
Simon Glass3ff83bc2017-04-23 20:10:45 -0600481/**
482 * check_devices() - Check return values and pointers
483 *
484 * This runs through a full sequence of uclass_first_device_check()...
485 * uclass_next_device_check() checking that the return values and devices
486 * are correct.
487 *
488 * @uts: Test state
489 * @devlist: List of expected devices
490 * @mask: Indicates which devices should return an error. Device n should
491 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
492 * bit n is clear.
493 */
494static int check_devices(struct unit_test_state *uts,
495 struct udevice *devlist[], int mask)
496{
497 int expected_ret;
498 struct udevice *dev;
499 int i;
500
501 expected_ret = (mask & 1) ? -ENOENT : 0;
502 mask >>= 1;
503 ut_asserteq(expected_ret,
504 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
505 for (i = 0; i < 4; i++) {
506 ut_asserteq_ptr(devlist[i], dev);
507 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
508 mask >>= 1;
509 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
510 }
511 ut_asserteq_ptr(NULL, dev);
512
513 return 0;
514}
515
516/* Test uclass_first_device_check() and uclass_next_device_check() */
517static int dm_test_first_next_ok_device(struct unit_test_state *uts)
518{
519 struct dm_testprobe_pdata *pdata;
520 struct udevice *dev, *parent = NULL, *devlist[4];
521 int count;
522 int ret;
523
524 /* There should be 4 devices */
525 count = 0;
526 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
527 dev;
528 ret = uclass_next_device_check(&dev)) {
529 ut_assertok(ret);
530 devlist[count++] = dev;
531 parent = dev_get_parent(dev);
532 }
533 ut_asserteq(4, count);
534 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
535 ut_assertok(check_devices(uts, devlist, 0));
536
537 /* Remove them and try again, with an error on the second one */
Simon Glassfa20e932020-12-03 16:55:20 -0700538 pdata = dev_get_plat(devlist[1]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600539 pdata->probe_err = -ENOENT - 1;
540 device_remove(parent, DM_REMOVE_NORMAL);
541 ut_assertok(check_devices(uts, devlist, 1 << 1));
542
543 /* Now an error on the first one */
Simon Glassfa20e932020-12-03 16:55:20 -0700544 pdata = dev_get_plat(devlist[0]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600545 pdata->probe_err = -ENOENT - 0;
546 device_remove(parent, DM_REMOVE_NORMAL);
547 ut_assertok(check_devices(uts, devlist, 3 << 0));
548
549 /* Now errors on all */
Simon Glassfa20e932020-12-03 16:55:20 -0700550 pdata = dev_get_plat(devlist[2]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600551 pdata->probe_err = -ENOENT - 2;
Simon Glassfa20e932020-12-03 16:55:20 -0700552 pdata = dev_get_plat(devlist[3]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600553 pdata->probe_err = -ENOENT - 3;
554 device_remove(parent, DM_REMOVE_NORMAL);
555 ut_assertok(check_devices(uts, devlist, 0xf << 0));
556
557 return 0;
558}
Simon Glass974dccd2020-07-28 19:41:12 -0600559DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six35616ef2018-03-12 14:53:33 +0100560
561static const struct udevice_id fdt_dummy_ids[] = {
562 { .compatible = "denx,u-boot-fdt-dummy", },
563 { }
564};
565
566UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca5ba71e52018-05-19 14:13:55 +0200567 .name = "fdt-dummy",
Mario Six35616ef2018-03-12 14:53:33 +0100568 .id = UCLASS_TEST_DUMMY,
569 .flags = DM_UC_FLAG_SEQ_ALIAS,
570};
571
572U_BOOT_DRIVER(fdt_dummy_drv) = {
573 .name = "fdt_dummy_drv",
574 .of_match = fdt_dummy_ids,
575 .id = UCLASS_TEST_DUMMY,
576};
577
578static int dm_test_fdt_translation(struct unit_test_state *uts)
579{
580 struct udevice *dev;
Fabien Dessenne22236e02019-05-31 15:11:30 +0200581 fdt32_t dma_addr[2];
Mario Six35616ef2018-03-12 14:53:33 +0100582
583 /* Some simple translations */
Simon Glass07e13382020-12-16 21:20:29 -0700584 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100585 ut_asserteq_str("dev@0,0", dev->name);
586 ut_asserteq(0x8000, dev_read_addr(dev));
587
Simon Glass07e13382020-12-16 21:20:29 -0700588 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100589 ut_asserteq_str("dev@1,100", dev->name);
590 ut_asserteq(0x9000, dev_read_addr(dev));
591
Simon Glass07e13382020-12-16 21:20:29 -0700592 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100593 ut_asserteq_str("dev@2,200", dev->name);
594 ut_asserteq(0xA000, dev_read_addr(dev));
595
Yuepeng Xing71cc0212022-12-02 14:23:07 +0800596 /* No translation for buses with #size-cells == 0 */
Simon Glass07e13382020-12-16 21:20:29 -0700597 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100598 ut_asserteq_str("dev@42", dev->name);
599 ut_asserteq(0x42, dev_read_addr(dev));
600
Fabien Dessenne22236e02019-05-31 15:11:30 +0200601 /* dma address translation */
Simon Glass07e13382020-12-16 21:20:29 -0700602 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Fabien Dessenne22236e02019-05-31 15:11:30 +0200603 dma_addr[0] = cpu_to_be32(0);
604 dma_addr[1] = cpu_to_be32(0);
605 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
606
Simon Glass07e13382020-12-16 21:20:29 -0700607 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Fabien Dessenne22236e02019-05-31 15:11:30 +0200608 dma_addr[0] = cpu_to_be32(1);
609 dma_addr[1] = cpu_to_be32(0x100);
610 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
611
Mario Six35616ef2018-03-12 14:53:33 +0100612 return 0;
613}
Simon Glass974dccd2020-07-28 19:41:12 -0600614DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200615
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300616static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
617{
618 struct udevice *gpio, *dev;
619 void *ptr;
Johan Jonker86648602023-04-23 11:19:41 +0200620 void *paddr;
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300621
622 /* Test for missing reg property */
623 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
624 ut_assertnull(devfdt_get_addr_ptr(gpio));
625
Simon Glass07e13382020-12-16 21:20:29 -0700626 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300627 ptr = devfdt_get_addr_ptr(dev);
Johan Jonker86648602023-04-23 11:19:41 +0200628
629 paddr = map_sysmem(0x8000, 0);
630 ut_asserteq_ptr(paddr, ptr);
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300631
632 return 0;
633}
634DM_TEST(dm_test_fdt_get_addr_ptr_flat,
635 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
636
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200637static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
638{
639 struct udevice *dev;
640 fdt_addr_t addr;
641 void *paddr;
642
Simon Glass07e13382020-12-16 21:20:29 -0700643 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200644
645 addr = devfdt_get_addr(dev);
646 ut_asserteq(0x8000, addr);
647
648 paddr = map_physmem(addr, 0, MAP_NOCACHE);
649 ut_assertnonnull(paddr);
650 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
651
652 return 0;
653}
654DM_TEST(dm_test_fdt_remap_addr_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600655 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200656
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100657static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
658{
659 struct udevice *dev;
660 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530661 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100662 void *paddr;
663
Simon Glass07e13382020-12-16 21:20:29 -0700664 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100665
Sekhar Norif677c6f2019-08-01 19:12:56 +0530666 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100667 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530668 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100669
670 paddr = map_physmem(addr, 0, MAP_NOCACHE);
671 ut_assertnonnull(paddr);
672 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
673
674 return 0;
675}
676DM_TEST(dm_test_fdt_remap_addr_index_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600677 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100678
679static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
680{
681 struct udevice *dev;
682 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530683 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100684 void *paddr;
685
Simon Glass07e13382020-12-16 21:20:29 -0700686 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100687
Sekhar Norif677c6f2019-08-01 19:12:56 +0530688 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100689 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530690 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100691
692 paddr = map_physmem(addr, 0, MAP_NOCACHE);
693 ut_assertnonnull(paddr);
694 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
695
696 return 0;
697}
698DM_TEST(dm_test_fdt_remap_addr_name_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600699 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100700
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200701static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
702{
703 struct udevice *dev;
704 fdt_addr_t addr;
705 void *paddr;
706
Simon Glass07e13382020-12-16 21:20:29 -0700707 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200708
709 addr = dev_read_addr(dev);
710 ut_asserteq(0x8000, addr);
711
712 paddr = map_physmem(addr, 0, MAP_NOCACHE);
713 ut_assertnonnull(paddr);
714 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
715
716 return 0;
717}
718DM_TEST(dm_test_fdt_remap_addr_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600719 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six058ef5b2018-06-26 08:46:49 +0200720
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100721static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
722{
723 struct udevice *dev;
724 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530725 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100726 void *paddr;
727
Simon Glass07e13382020-12-16 21:20:29 -0700728 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100729
Sekhar Norif677c6f2019-08-01 19:12:56 +0530730 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100731 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530732 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100733
734 paddr = map_physmem(addr, 0, MAP_NOCACHE);
735 ut_assertnonnull(paddr);
736 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
737
738 return 0;
739}
740DM_TEST(dm_test_fdt_remap_addr_index_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600741 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100742
743static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
744{
745 struct udevice *dev;
746 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530747 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100748 void *paddr;
749
Simon Glass07e13382020-12-16 21:20:29 -0700750 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100751
Sekhar Norif677c6f2019-08-01 19:12:56 +0530752 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100753 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530754 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100755
756 paddr = map_physmem(addr, 0, MAP_NOCACHE);
757 ut_assertnonnull(paddr);
758 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
759
760 return 0;
761}
762DM_TEST(dm_test_fdt_remap_addr_name_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600763 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100764
Mario Sixd6f6dc32018-06-26 08:46:51 +0200765static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
766{
767 ofnode node;
768
769 if (!of_live_active()) {
770 printf("Live tree not active; ignore test\n");
771 return 0;
772 }
773
774 node = ofnode_path("/usb@2");
775
776 /* Test enabling devices */
777
778 ut_assert(!of_device_is_available(ofnode_to_np(node)));
779 dev_enable_by_path("/usb@2");
780 ut_assert(of_device_is_available(ofnode_to_np(node)));
781
782 /* Test disabling devices */
783
784 ut_assert(of_device_is_available(ofnode_to_np(node)));
785 dev_disable_by_path("/usb@2");
786 ut_assert(!of_device_is_available(ofnode_to_np(node)));
787
788 return 0;
789}
Simon Glass974dccd2020-07-28 19:41:12 -0600790DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
791 UT_TESTF_SCAN_FDT);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700792
793/* Test a few uclass phandle functions */
794static int dm_test_fdt_phandle(struct unit_test_state *uts)
795{
796 struct udevice *back, *dev, *dev2;
797
798 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200799 ut_assertnonnull(back);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700800 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
801 back, "missing", &dev));
802 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
803 "power-supply", &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200804 ut_assertnonnull(dev);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700805 ut_asserteq(0, device_active(dev));
806 ut_asserteq_str("ldo1", dev->name);
807 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
808 "power-supply", &dev2));
809 ut_asserteq_ptr(dev, dev2);
810
811 return 0;
812}
Simon Glass974dccd2020-07-28 19:41:12 -0600813DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa11341a2018-11-18 08:14:31 -0700814
815/* Test device_find_first_child_by_uclass() */
816static int dm_test_first_child(struct unit_test_state *uts)
817{
818 struct udevice *i2c, *dev, *dev2;
819
820 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
821 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
822 ut_asserteq_str("rtc@43", dev->name);
823 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
824 ut_asserteq_ptr(dev, dev2);
825 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
826 ut_asserteq_str("rtc@61", dev2->name);
827
828 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
829 &dev));
830 ut_asserteq_str("eeprom@2c", dev->name);
831 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
832 ut_asserteq_ptr(dev, dev2);
833
834 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
835 UCLASS_VIDEO, &dev));
836 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
837
838 return 0;
839}
Simon Glass974dccd2020-07-28 19:41:12 -0600840DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass6df01f92018-12-10 10:37:37 -0700841
842/* Test integer functions in dm_read_...() */
843static int dm_test_read_int(struct unit_test_state *uts)
844{
845 struct udevice *dev;
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200846 u8 val8;
847 u16 val16;
Simon Glass6df01f92018-12-10 10:37:37 -0700848 u32 val32;
849 s32 sval;
850 uint val;
Dario Binacchi421e81e2020-03-29 18:04:40 +0200851 u64 val64;
Simon Glass6df01f92018-12-10 10:37:37 -0700852
853 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
854 ut_asserteq_str("a-test", dev->name);
Stefan Herbrechtsmeier1b090e62022-06-14 15:21:30 +0200855
856 ut_assertok(dev_read_u8(dev, "int8-value", &val8));
857 ut_asserteq(0x12, val8);
858
859 ut_asserteq(-EINVAL, dev_read_u8(dev, "missing", &val8));
860 ut_asserteq(6, dev_read_u8_default(dev, "missing", 6));
861
862 ut_asserteq(0x12, dev_read_u8_default(dev, "int8-value", 6));
863
864 ut_assertok(dev_read_u16(dev, "int16-value", &val16));
865 ut_asserteq(0x1234, val16);
866
867 ut_asserteq(-EINVAL, dev_read_u16(dev, "missing", &val16));
868 ut_asserteq(6, dev_read_u16_default(dev, "missing", 6));
869
870 ut_asserteq(0x1234, dev_read_u16_default(dev, "int16-value", 6));
871
Simon Glass6df01f92018-12-10 10:37:37 -0700872 ut_assertok(dev_read_u32(dev, "int-value", &val32));
873 ut_asserteq(1234, val32);
874
875 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
876 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
877
878 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
879 ut_asserteq(1234, val32);
880
881 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
882 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
883
884 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
885 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
886 ut_asserteq(-1234, sval);
887
888 val = 0;
889 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
890 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
891 ut_asserteq(-1234, val);
892
Dario Binacchi421e81e2020-03-29 18:04:40 +0200893 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
894 ut_asserteq_64(0x1111222233334444, val64);
895
896 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
897 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
898
899 ut_asserteq_64(0x1111222233334444,
900 dev_read_u64_default(dev, "int64-value", 6));
901
Simon Glass6df01f92018-12-10 10:37:37 -0700902 return 0;
903}
Simon Glass974dccd2020-07-28 19:41:12 -0600904DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass35cb2a42020-02-06 09:54:50 -0700905
Dario Binacchi81d80b52020-03-29 18:04:41 +0200906static int dm_test_read_int_index(struct unit_test_state *uts)
907{
908 struct udevice *dev;
909 u32 val32;
910
911 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
912 ut_asserteq_str("a-test", dev->name);
913
914 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
915 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
916
917 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
918 ut_asserteq(5678, val32);
919 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
920 ut_asserteq(9123, val32);
921 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
922 ut_asserteq(4567, val32);
923 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
924 &val32));
925
926 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
927 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
928 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
929 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
930
931 return 0;
932}
Simon Glass974dccd2020-07-28 19:41:12 -0600933DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi81d80b52020-03-29 18:04:41 +0200934
Patrick Delaunay8cd28012020-09-25 09:41:16 +0200935static int dm_test_read_phandle(struct unit_test_state *uts)
936{
937 struct udevice *dev;
938 struct ofnode_phandle_args args;
939 int ret;
940 const char prop[] = "test-gpios";
941 const char cell[] = "#gpio-cells";
942 const char prop2[] = "phandle-value";
943
944 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
945 ut_asserteq_str("a-test", dev->name);
946
947 /* Test dev_count_phandle_with_args with cell name */
948 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
949 ut_asserteq(-ENOENT, ret);
950 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
951 ut_asserteq(-EINVAL, ret);
952 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
953
954 /* Test dev_read_phandle_with_args with cell name */
955 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
956 ut_asserteq(-ENOENT, ret);
957 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
958 ut_asserteq(-EINVAL, ret);
959 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
960 ut_asserteq(1, args.args_count);
961 ut_asserteq(1, args.args[0]);
962 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
963 ut_asserteq(1, args.args_count);
964 ut_asserteq(4, args.args[0]);
965 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
966 ut_asserteq(5, args.args_count);
967 ut_asserteq(5, args.args[0]);
968 ut_asserteq(1, args.args[4]);
969 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
970 ut_asserteq(-ENOENT, ret);
971 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
972 ut_asserteq(1, args.args_count);
973 ut_asserteq(12, args.args[0]);
974 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
975 ut_asserteq(-ENOENT, ret);
976
977 /* Test dev_count_phandle_with_args with cell count */
978 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
979 ut_asserteq(-ENOENT, ret);
980 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
981
982 /* Test dev_read_phandle_with_args with cell count */
983 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
984 ut_asserteq(1, ofnode_valid(args.node));
985 ut_asserteq(1, args.args_count);
986 ut_asserteq(10, args.args[0]);
987 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
988 ut_asserteq(-EINVAL, ret);
989 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
990 ut_asserteq(1, ofnode_valid(args.node));
991 ut_asserteq(1, args.args_count);
992 ut_asserteq(30, args.args[0]);
993 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
994 ut_asserteq(-ENOENT, ret);
995
996 return 0;
997}
998DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
999
Simon Glass35cb2a42020-02-06 09:54:50 -07001000/* Test iteration through devices by drvdata */
1001static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1002{
1003 struct udevice *dev;
1004
1005 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1006 DM_TEST_TYPE_FIRST, &dev));
1007 ut_asserteq_str("a-test", dev->name);
1008
1009 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1010 DM_TEST_TYPE_SECOND, &dev));
1011 ut_asserteq_str("d-test", dev->name);
1012
1013 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1014 DM_TEST_TYPE_COUNT,
1015 &dev));
1016
1017 return 0;
1018}
Simon Glass974dccd2020-07-28 19:41:12 -06001019DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Tom Rinib7b18382020-02-11 10:58:41 -05001020
Simon Glass000676b2020-01-27 08:49:47 -07001021/* Test device_first_child_ofdata_err(), etc. */
1022static int dm_test_child_ofdata(struct unit_test_state *uts)
1023{
1024 struct udevice *bus, *dev;
1025 int count;
1026
1027 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1028 count = 0;
Simon Glassaad29ae2020-12-03 16:55:21 -07001029 device_foreach_child_of_to_plat(dev, bus) {
Simon Glass6211d762020-12-19 10:40:10 -07001030 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1031 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glass000676b2020-01-27 08:49:47 -07001032 count++;
1033 }
1034 ut_asserteq(3, count);
1035
1036 return 0;
1037}
Simon Glass974dccd2020-07-28 19:41:12 -06001038DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass76db9b72020-01-27 08:49:48 -07001039
1040/* Test device_first_child_err(), etc. */
1041static int dm_test_first_child_probe(struct unit_test_state *uts)
1042{
1043 struct udevice *bus, *dev;
1044 int count;
1045
1046 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1047 count = 0;
1048 device_foreach_child_probe(dev, bus) {
Simon Glass6211d762020-12-19 10:40:10 -07001049 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1050 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass76db9b72020-01-27 08:49:48 -07001051 count++;
1052 }
1053 ut_asserteq(3, count);
1054
1055 return 0;
1056}
Simon Glass974dccd2020-07-28 19:41:12 -06001057DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass44c47892020-04-05 15:38:19 -06001058
1059/* Test that ofdata is read for parents before children */
1060static int dm_test_ofdata_order(struct unit_test_state *uts)
1061{
1062 struct udevice *bus, *dev;
1063
1064 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1065 ut_assertnonnull(bus);
Simon Glass6211d762020-12-19 10:40:10 -07001066 ut_assert(!(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID));
Simon Glass44c47892020-04-05 15:38:19 -06001067
1068 ut_assertok(device_find_first_child(bus, &dev));
1069 ut_assertnonnull(dev);
Simon Glass6211d762020-12-19 10:40:10 -07001070 ut_assert(!(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID));
Simon Glass44c47892020-04-05 15:38:19 -06001071
1072 /* read the child's ofdata which should cause the parent's to be read */
Simon Glassaad29ae2020-12-03 16:55:21 -07001073 ut_assertok(device_of_to_plat(dev));
Simon Glass6211d762020-12-19 10:40:10 -07001074 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1075 ut_assert(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID);
Simon Glass44c47892020-04-05 15:38:19 -06001076
Simon Glass6211d762020-12-19 10:40:10 -07001077 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
1078 ut_assert(!(dev_get_flags(bus) & DM_FLAG_ACTIVATED));
Simon Glass44c47892020-04-05 15:38:19 -06001079
1080 return 0;
1081}
Simon Glass974dccd2020-07-28 19:41:12 -06001082DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi836cc9d2020-12-30 00:16:26 +01001083
1084/* Test dev_decode_display_timing() */
1085static int dm_test_decode_display_timing(struct unit_test_state *uts)
1086{
1087 struct udevice *dev;
1088 struct display_timing timing;
1089
1090 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1091 ut_asserteq_str("a-test", dev->name);
1092
1093 ut_assertok(dev_decode_display_timing(dev, 0, &timing));
1094 ut_assert(timing.hactive.typ == 240);
1095 ut_assert(timing.hback_porch.typ == 7);
1096 ut_assert(timing.hfront_porch.typ == 6);
1097 ut_assert(timing.hsync_len.typ == 1);
1098 ut_assert(timing.vactive.typ == 320);
1099 ut_assert(timing.vback_porch.typ == 5);
1100 ut_assert(timing.vfront_porch.typ == 8);
1101 ut_assert(timing.vsync_len.typ == 2);
1102 ut_assert(timing.pixelclock.typ == 6500000);
1103 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH);
1104 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1105 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1106 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW);
1107 ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH);
1108 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1109 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE);
1110 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1111 ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED);
1112 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN);
1113 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK);
1114
1115 ut_assertok(dev_decode_display_timing(dev, 1, &timing));
1116 ut_assert(timing.hactive.typ == 480);
1117 ut_assert(timing.hback_porch.typ == 59);
1118 ut_assert(timing.hfront_porch.typ == 10);
1119 ut_assert(timing.hsync_len.typ == 12);
1120 ut_assert(timing.vactive.typ == 800);
1121 ut_assert(timing.vback_porch.typ == 15);
1122 ut_assert(timing.vfront_porch.typ == 17);
1123 ut_assert(timing.vsync_len.typ == 16);
1124 ut_assert(timing.pixelclock.typ == 9000000);
1125 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1126 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_LOW);
1127 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH);
1128 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1129 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1130 ut_assert(timing.flags & DISPLAY_FLAGS_DE_LOW);
1131 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1132 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE);
1133 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1134 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1135 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1136
1137 ut_assertok(dev_decode_display_timing(dev, 2, &timing));
1138 ut_assert(timing.hactive.typ == 800);
1139 ut_assert(timing.hback_porch.typ == 89);
1140 ut_assert(timing.hfront_porch.typ == 164);
1141 ut_assert(timing.hsync_len.typ == 11);
1142 ut_assert(timing.vactive.typ == 480);
1143 ut_assert(timing.vback_porch.typ == 23);
1144 ut_assert(timing.vfront_porch.typ == 10);
1145 ut_assert(timing.vsync_len.typ == 13);
1146 ut_assert(timing.pixelclock.typ == 33500000);
1147 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1148 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1149 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1150 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1151 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1152 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1153 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1154 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1155 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1156 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1157 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1158
1159 ut_assert(dev_decode_display_timing(dev, 3, &timing));
1160 return 0;
1161}
1162DM_TEST(dm_test_decode_display_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Patrick Delaunayda37ed32021-05-21 12:25:00 +02001163
Nikhil M Jainbb9d1312023-01-31 15:35:15 +05301164/* Test dev_decode_panel_timing() */
1165static int dm_test_decode_panel_timing(struct unit_test_state *uts)
1166{
1167 struct udevice *dev;
1168 struct display_timing timing;
1169
1170 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1171 ut_asserteq_str("a-test", dev->name);
1172
1173 ut_assertok(dev_decode_panel_timing(dev, &timing));
1174 ut_assert(timing.hactive.typ == 240);
1175 ut_assert(timing.hback_porch.typ == 7);
1176 ut_assert(timing.hfront_porch.typ == 6);
1177 ut_assert(timing.hsync_len.typ == 1);
1178 ut_assert(timing.vactive.typ == 320);
1179 ut_assert(timing.vback_porch.typ == 5);
1180 ut_assert(timing.vfront_porch.typ == 8);
1181 ut_assert(timing.vsync_len.typ == 2);
1182 ut_assert(timing.pixelclock.typ == 6500000);
1183 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH);
1184 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1185 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1186 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW);
1187 ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH);
1188 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1189 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE);
1190 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1191 ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED);
1192 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN);
1193 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK);
1194
1195 return 0;
1196}
1197DM_TEST(dm_test_decode_panel_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1198
Patrick Delaunayda37ed32021-05-21 12:25:00 +02001199/* Test read_resourcee() */
1200static int dm_test_read_resource(struct unit_test_state *uts)
1201{
1202 struct udevice *dev;
1203 struct resource res;
1204
1205 /* test resource without translation */
1206 ut_assertok(uclass_find_device_by_name(UCLASS_SIMPLE_BUS, "syscon@2", &dev));
1207 ut_assertok(dev_read_resource(dev, 0, &res));
1208 ut_asserteq(0x40, res.start);
1209 ut_asserteq(0x44, res.end);
1210 ut_assertok(dev_read_resource(dev, 1, &res));
1211 ut_asserteq(0x48, res.start);
1212 ut_asserteq(0x4d, res.end);
1213
1214 /* test resource with translation */
1215 ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@1,100", &dev));
1216 ut_assertok(dev_read_resource(dev, 0, &res));
1217 ut_asserteq(0x9000, res.start);
1218 ut_asserteq(0x9fff, res.end);
1219
1220 /* test named resource */
1221 ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@0,0", &dev));
1222 ut_assertok(dev_read_resource_byname(dev, "sandbox-dummy-0", &res));
1223 ut_asserteq(0x8000, res.start);
1224 ut_asserteq(0x8fff, res.end);
1225
1226 return 0;
1227}
1228
1229DM_TEST(dm_test_read_resource, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);