blob: 012f2f455f6b84640ef12e72b39eef2da639539d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2e7d35d2014-02-26 15:59:21 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass2e7d35d2014-02-26 15:59:21 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070011#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070013#include <asm/io.h>
14#include <dm/test.h>
15#include <dm/root.h>
Simon Glass98561572017-04-23 20:10:44 -060016#include <dm/device-internal.h>
Simon Glass61b29b82020-02-03 07:36:15 -070017#include <dm/devres.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070018#include <dm/uclass-internal.h>
19#include <dm/util.h>
Mario Six2ea4d0d2018-06-26 08:46:49 +020020#include <dm/of_access.h>
Patrick Delaunay269fa842021-05-21 12:25:00 +020021#include <linux/ioport.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060022#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050023#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070024
25DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass98561572017-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 Glassc69cda22020-12-03 16:55:20 -070033 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
Simon Glass98561572017-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 Glasscaa4daa2020-12-03 16:55:18 -070048 .plat_auto = sizeof(struct dm_testprobe_pdata),
Simon Glass98561572017-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 Glassdc12ebb2019-12-29 21:19:25 -070057struct dm_testdevres_pdata {
58 void *ptr;
59};
60
61struct dm_testdevres_priv {
62 void *ptr;
Simon Glass42a0ce52019-12-29 21:19:28 -070063 void *ptr_ofdata;
Simon Glassdc12ebb2019-12-29 21:19:25 -070064};
65
66static int testdevres_drv_bind(struct udevice *dev)
67{
Simon Glassc69cda22020-12-03 16:55:20 -070068 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
Simon Glassdc12ebb2019-12-29 21:19:25 -070069
70 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
71
72 return 0;
73}
74
Simon Glassd1998a92020-12-03 16:55:21 -070075static int testdevres_drv_of_to_plat(struct udevice *dev)
Simon Glass42a0ce52019-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 Glassdc12ebb2019-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 Glassd1998a92020-12-03 16:55:21 -0700103 .of_to_plat = testdevres_drv_of_to_plat,
Simon Glassdc12ebb2019-12-29 21:19:25 -0700104 .probe = testdevres_drv_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700105 .plat_auto = sizeof(struct dm_testdevres_pdata),
Simon Glass41575d82020-12-03 16:55:17 -0700106 .priv_auto = sizeof(struct dm_testdevres_priv),
Simon Glassdc12ebb2019-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 Hershbergere721b882015-05-20 14:27:27 -0500115int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700116{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200117 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700118 int ret;
119 int i;
120
Simon Glass2e7d35d2014-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 Glass1ca7e202014-07-23 06:55:18 -0600125 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-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 Glasseb9ef5f2014-07-23 06:54:57 -0600132 * Get the 'ping-expect' property, which tells us what the
Simon Glasscaa4daa2020-12-03 16:55:18 -0700133 * ping add should be. We don't use the plat because we
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600134 * want to test the code that sets that up
135 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700136 */
Simon Glasse160f7d2017-01-17 16:52:55 -0700137 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600138 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700139 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glasse160f7d2017-01-17 16:52:55 -0700140 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glass2e7d35d2014-02-26 15:59:21 -0700141
Joe Hershbergere721b882015-05-20 14:27:27 -0500142 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700143 dev_get_priv(dev)));
144 }
145
146 return 0;
147}
Simon Glass1ca7e202014-07-23 06:55:18 -0600148
149/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500150static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600151{
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530152 const int num_devices = 9;
Simon Glass1ca7e202014-07-23 06:55:18 -0600153 struct udevice *dev;
154 struct uclass *uc;
155 int ret;
156 int i;
157
Simon Glass8ee05b52020-11-28 17:50:10 -0700158 ret = dm_extended_scan(false);
Simon Glass1ca7e202014-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 Glassf8a85442015-01-25 08:27:00 -0700167 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-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 Glass0fd3d912020-12-22 19:30:28 -0700172 ut_assert(dev_get_plat(dev));
Simon Glass1ca7e202014-07-23 06:55:18 -0600173 }
174
Joe Hershbergere721b882015-05-20 14:27:27 -0500175 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600176
177 return 0;
178}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700179DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600180
Michal Simeka93eb572019-01-31 16:31:00 +0100181static int dm_test_alias_highest_id(struct unit_test_state *uts)
182{
183 int ret;
184
Michael Walle82a3c9e2021-02-25 16:51:11 +0100185 ret = dev_read_alias_highest_id("ethernet");
Sean Andersonbedb1822022-05-05 13:11:30 -0400186 ut_asserteq(8, ret);
Michal Simeka93eb572019-01-31 16:31:00 +0100187
188 ret = dev_read_alias_highest_id("gpio");
Patrick Delaunayff526652020-01-13 11:35:14 +0100189 ut_asserteq(3, ret);
Michal Simeka93eb572019-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 Hershbergere721b882015-05-20 14:27:27 -0500204static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-07-23 06:55:03 -0600205{
206 struct uclass *uc;
207 int ret;
208
Simon Glass725e4fc2020-11-28 17:50:09 -0700209 ret = dm_scan_fdt(true);
Simon Glass00606d72014-07-23 06:55:03 -0600210 ut_assert(!ret);
211
212 ret = uclass_get(UCLASS_TEST_FDT, &uc);
213 ut_assert(!ret);
214
Bin Meng2786cd72018-10-10 22:07:01 -0700215 /*
216 * These are 2 pre-reloc devices:
217 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
218 * 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 Glass00606d72014-07-23 06:55:03 -0600221
222 return 0;
223}
224DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600225
226/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500227static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600228{
229 struct udevice *dev;
230
231 /* A few basic santiy tests */
Simon Glass99175912020-12-16 21:20:29 -0700232 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600233 ut_asserteq_str("b-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700234 ut_asserteq(3, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600235
Simon Glass99175912020-12-16 21:20:29 -0700236 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600237 ut_asserteq_str("a-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700238 ut_asserteq(8, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600239
Simon Glass981426e2020-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 Glass99175912020-12-16 21:20:29 -0700244 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev));
Simon Glass981426e2020-12-16 21:20:26 -0700245 ut_asserteq_str("d-test", dev->name);
246 ut_asserteq(13, dev_seq(dev));
247
Simon Glass99175912020-12-16 21:20:29 -0700248 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass981426e2020-12-16 21:20:26 -0700249 &dev));
Simon Glass5a66a8f2014-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 Glass981426e2020-12-16 21:20:26 -0700255 ut_asserteq(6, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600256
Simon Glass1ca7e202014-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 Glass5a66a8f2014-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 Glass981426e2020-12-16 21:20:26 -0700263 ut_asserteq(3, dev_seq(dev));
Simon Glass5a66a8f2014-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 Glass981426e2020-12-16 21:20:26 -0700271 ut_asserteq(13, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600272
Simon Glass981426e2020-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 Glass5a66a8f2014-07-23 06:55:12 -0600277
Simon Glass981426e2020-12-16 21:20:26 -0700278 /* And we should still have holes in our sequence numbers */
Simon Glass99175912020-12-16 21:20:29 -0700279 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0,
Simon Glass981426e2020-12-16 21:20:26 -0700280 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700281 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1,
Simon Glass981426e2020-12-16 21:20:26 -0700282 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700283 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
Simon Glass981426e2020-12-16 21:20:26 -0700284 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700285 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
Simon Glass981426e2020-12-16 21:20:26 -0700286 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700287 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
Simon Glass981426e2020-12-16 21:20:26 -0700288 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700289 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass981426e2020-12-16 21:20:26 -0700290 &dev));
291 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
Simon Glass99175912020-12-16 21:20:29 -0700292 &dev));
Simon Glass981426e2020-12-16 21:20:26 -0700293 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
Simon Glass99175912020-12-16 21:20:29 -0700294 &dev));
Michael Wallebe1a6e92020-06-02 01:47:09 +0200295
Simon Glass5a66a8f2014-07-23 06:55:12 -0600296 return 0;
297}
Simon Glasse180c2b2020-07-28 19:41:12 -0600298DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600299
Simon Glass93f44e82020-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 Glass7f20d1d2020-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 Glass074197a2021-02-03 06:01:09 -0700332 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass7f20d1d2020-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 Glass074197a2021-02-03 06:01:09 -0700352 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass7f20d1d2020-12-16 21:20:32 -0700353 "fred", NULL, ofnode_null(), &dev));
354 ut_asserteq(19, dev_seq(dev));
355
Simon Glass074197a2021-02-03 06:01:09 -0700356 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(denx_u_boot_fdt_test),
Simon Glass7f20d1d2020-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 Glassf4cdead2014-07-23 06:55:14 -0600364/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500365static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-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 Glass1ca7e202014-07-23 06:55:18 -0600384 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-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 Glass86b54ec2017-05-18 20:09:44 -0600391DM_TEST(dm_test_fdt_offset,
Simon Glasse180c2b2020-07-28 19:41:12 -0600392 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass98561572017-04-23 20:10:44 -0600393
394/**
395 * Test various error conditions with uclass_first_device() and
396 * uclass_next_device()
397 */
398static int dm_test_first_next_device(struct unit_test_state *uts)
399{
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 */
406 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
407 dev;
408 ret = uclass_next_device(&dev)) {
409 count++;
410 parent = dev_get_parent(dev);
411 }
412 ut_assertok(ret);
413 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 Glassc69cda22020-12-03 16:55:20 -0700417 pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600418 pdata->probe_err = -ENOMEM;
419 device_remove(parent, DM_REMOVE_NORMAL);
420 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
421 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
422 ut_asserteq_ptr(dev, NULL);
423
424 /* Now an error on the first one */
425 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
Simon Glassc69cda22020-12-03 16:55:20 -0700426 pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600427 pdata->probe_err = -ENOENT;
428 device_remove(parent, DM_REMOVE_NORMAL);
429 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
430
431 return 0;
432}
Simon Glasse180c2b2020-07-28 19:41:12 -0600433DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass95ce3852017-04-23 20:10:45 -0600434
Simon Glass3cf0fba2020-02-06 09:54:49 -0700435/* Test iteration through devices in a uclass */
436static int dm_test_uclass_foreach(struct unit_test_state *uts)
437{
438 struct udevice *dev;
439 struct uclass *uc;
440 int count;
441
442 count = 0;
443 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
444 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530445 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700446
447 count = 0;
448 uclass_foreach_dev(dev, uc)
449 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530450 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700451
452 return 0;
453}
Simon Glasse180c2b2020-07-28 19:41:12 -0600454DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700455
Simon Glass95ce3852017-04-23 20:10:45 -0600456/**
457 * check_devices() - Check return values and pointers
458 *
459 * This runs through a full sequence of uclass_first_device_check()...
460 * uclass_next_device_check() checking that the return values and devices
461 * are correct.
462 *
463 * @uts: Test state
464 * @devlist: List of expected devices
465 * @mask: Indicates which devices should return an error. Device n should
466 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
467 * bit n is clear.
468 */
469static int check_devices(struct unit_test_state *uts,
470 struct udevice *devlist[], int mask)
471{
472 int expected_ret;
473 struct udevice *dev;
474 int i;
475
476 expected_ret = (mask & 1) ? -ENOENT : 0;
477 mask >>= 1;
478 ut_asserteq(expected_ret,
479 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
480 for (i = 0; i < 4; i++) {
481 ut_asserteq_ptr(devlist[i], dev);
482 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
483 mask >>= 1;
484 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
485 }
486 ut_asserteq_ptr(NULL, dev);
487
488 return 0;
489}
490
491/* Test uclass_first_device_check() and uclass_next_device_check() */
492static int dm_test_first_next_ok_device(struct unit_test_state *uts)
493{
494 struct dm_testprobe_pdata *pdata;
495 struct udevice *dev, *parent = NULL, *devlist[4];
496 int count;
497 int ret;
498
499 /* There should be 4 devices */
500 count = 0;
501 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
502 dev;
503 ret = uclass_next_device_check(&dev)) {
504 ut_assertok(ret);
505 devlist[count++] = dev;
506 parent = dev_get_parent(dev);
507 }
508 ut_asserteq(4, count);
509 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
510 ut_assertok(check_devices(uts, devlist, 0));
511
512 /* Remove them and try again, with an error on the second one */
Simon Glassc69cda22020-12-03 16:55:20 -0700513 pdata = dev_get_plat(devlist[1]);
Simon Glass95ce3852017-04-23 20:10:45 -0600514 pdata->probe_err = -ENOENT - 1;
515 device_remove(parent, DM_REMOVE_NORMAL);
516 ut_assertok(check_devices(uts, devlist, 1 << 1));
517
518 /* Now an error on the first one */
Simon Glassc69cda22020-12-03 16:55:20 -0700519 pdata = dev_get_plat(devlist[0]);
Simon Glass95ce3852017-04-23 20:10:45 -0600520 pdata->probe_err = -ENOENT - 0;
521 device_remove(parent, DM_REMOVE_NORMAL);
522 ut_assertok(check_devices(uts, devlist, 3 << 0));
523
524 /* Now errors on all */
Simon Glassc69cda22020-12-03 16:55:20 -0700525 pdata = dev_get_plat(devlist[2]);
Simon Glass95ce3852017-04-23 20:10:45 -0600526 pdata->probe_err = -ENOENT - 2;
Simon Glassc69cda22020-12-03 16:55:20 -0700527 pdata = dev_get_plat(devlist[3]);
Simon Glass95ce3852017-04-23 20:10:45 -0600528 pdata->probe_err = -ENOENT - 3;
529 device_remove(parent, DM_REMOVE_NORMAL);
530 ut_assertok(check_devices(uts, devlist, 0xf << 0));
531
532 return 0;
533}
Simon Glasse180c2b2020-07-28 19:41:12 -0600534DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Sixe8d52912018-03-12 14:53:33 +0100535
536static const struct udevice_id fdt_dummy_ids[] = {
537 { .compatible = "denx,u-boot-fdt-dummy", },
538 { }
539};
540
541UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca507cef32018-05-19 14:13:55 +0200542 .name = "fdt-dummy",
Mario Sixe8d52912018-03-12 14:53:33 +0100543 .id = UCLASS_TEST_DUMMY,
544 .flags = DM_UC_FLAG_SEQ_ALIAS,
545};
546
547U_BOOT_DRIVER(fdt_dummy_drv) = {
548 .name = "fdt_dummy_drv",
549 .of_match = fdt_dummy_ids,
550 .id = UCLASS_TEST_DUMMY,
551};
552
553static int dm_test_fdt_translation(struct unit_test_state *uts)
554{
555 struct udevice *dev;
Fabien Dessenne641067f2019-05-31 15:11:30 +0200556 fdt32_t dma_addr[2];
Mario Sixe8d52912018-03-12 14:53:33 +0100557
558 /* Some simple translations */
Simon Glass99175912020-12-16 21:20:29 -0700559 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100560 ut_asserteq_str("dev@0,0", dev->name);
561 ut_asserteq(0x8000, dev_read_addr(dev));
562
Simon Glass99175912020-12-16 21:20:29 -0700563 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100564 ut_asserteq_str("dev@1,100", dev->name);
565 ut_asserteq(0x9000, dev_read_addr(dev));
566
Simon Glass99175912020-12-16 21:20:29 -0700567 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100568 ut_asserteq_str("dev@2,200", dev->name);
569 ut_asserteq(0xA000, dev_read_addr(dev));
570
571 /* No translation for busses with #size-cells == 0 */
Simon Glass99175912020-12-16 21:20:29 -0700572 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100573 ut_asserteq_str("dev@42", dev->name);
574 ut_asserteq(0x42, dev_read_addr(dev));
575
Fabien Dessenne641067f2019-05-31 15:11:30 +0200576 /* dma address translation */
Simon Glass99175912020-12-16 21:20:29 -0700577 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Fabien Dessenne641067f2019-05-31 15:11:30 +0200578 dma_addr[0] = cpu_to_be32(0);
579 dma_addr[1] = cpu_to_be32(0);
580 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
581
Simon Glass99175912020-12-16 21:20:29 -0700582 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Fabien Dessenne641067f2019-05-31 15:11:30 +0200583 dma_addr[0] = cpu_to_be32(1);
584 dma_addr[1] = cpu_to_be32(0x100);
585 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
586
Mario Sixe8d52912018-03-12 14:53:33 +0100587 return 0;
588}
Simon Glasse180c2b2020-07-28 19:41:12 -0600589DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200590
Ovidiu Panait6e648302020-08-03 22:17:36 +0300591static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
592{
593 struct udevice *gpio, *dev;
594 void *ptr;
595
596 /* Test for missing reg property */
597 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
598 ut_assertnull(devfdt_get_addr_ptr(gpio));
599
Simon Glass99175912020-12-16 21:20:29 -0700600 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Ovidiu Panait6e648302020-08-03 22:17:36 +0300601 ptr = devfdt_get_addr_ptr(dev);
602 ut_asserteq_ptr((void *)0x8000, ptr);
603
604 return 0;
605}
606DM_TEST(dm_test_fdt_get_addr_ptr_flat,
607 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
608
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200609static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
610{
611 struct udevice *dev;
612 fdt_addr_t addr;
613 void *paddr;
614
Simon Glass99175912020-12-16 21:20:29 -0700615 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200616
617 addr = devfdt_get_addr(dev);
618 ut_asserteq(0x8000, addr);
619
620 paddr = map_physmem(addr, 0, MAP_NOCACHE);
621 ut_assertnonnull(paddr);
622 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
623
624 return 0;
625}
626DM_TEST(dm_test_fdt_remap_addr_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600627 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200628
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100629static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
630{
631 struct udevice *dev;
632 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530633 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100634 void *paddr;
635
Simon Glass99175912020-12-16 21:20:29 -0700636 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100637
Sekhar Norif5b90472019-08-01 19:12:56 +0530638 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100639 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530640 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100641
642 paddr = map_physmem(addr, 0, MAP_NOCACHE);
643 ut_assertnonnull(paddr);
644 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
645
646 return 0;
647}
648DM_TEST(dm_test_fdt_remap_addr_index_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600649 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100650
651static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
652{
653 struct udevice *dev;
654 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530655 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100656 void *paddr;
657
Simon Glass99175912020-12-16 21:20:29 -0700658 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100659
Sekhar Norif5b90472019-08-01 19:12:56 +0530660 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100661 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530662 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100663
664 paddr = map_physmem(addr, 0, MAP_NOCACHE);
665 ut_assertnonnull(paddr);
666 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
667
668 return 0;
669}
670DM_TEST(dm_test_fdt_remap_addr_name_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600671 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100672
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200673static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
674{
675 struct udevice *dev;
676 fdt_addr_t addr;
677 void *paddr;
678
Simon Glass99175912020-12-16 21:20:29 -0700679 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200680
681 addr = dev_read_addr(dev);
682 ut_asserteq(0x8000, addr);
683
684 paddr = map_physmem(addr, 0, MAP_NOCACHE);
685 ut_assertnonnull(paddr);
686 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
687
688 return 0;
689}
690DM_TEST(dm_test_fdt_remap_addr_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600691 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six2ea4d0d2018-06-26 08:46:49 +0200692
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100693static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
694{
695 struct udevice *dev;
696 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530697 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100698 void *paddr;
699
Simon Glass99175912020-12-16 21:20:29 -0700700 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100701
Sekhar Norif5b90472019-08-01 19:12:56 +0530702 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100703 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530704 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100705
706 paddr = map_physmem(addr, 0, MAP_NOCACHE);
707 ut_assertnonnull(paddr);
708 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
709
710 return 0;
711}
712DM_TEST(dm_test_fdt_remap_addr_index_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600713 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100714
715static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
716{
717 struct udevice *dev;
718 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530719 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100720 void *paddr;
721
Simon Glass99175912020-12-16 21:20:29 -0700722 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100723
Sekhar Norif5b90472019-08-01 19:12:56 +0530724 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100725 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530726 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100727
728 paddr = map_physmem(addr, 0, MAP_NOCACHE);
729 ut_assertnonnull(paddr);
730 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
731
732 return 0;
733}
734DM_TEST(dm_test_fdt_remap_addr_name_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600735 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100736
Mario Six172942a2018-06-26 08:46:51 +0200737static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
738{
739 ofnode node;
740
741 if (!of_live_active()) {
742 printf("Live tree not active; ignore test\n");
743 return 0;
744 }
745
746 node = ofnode_path("/usb@2");
747
748 /* Test enabling devices */
749
750 ut_assert(!of_device_is_available(ofnode_to_np(node)));
751 dev_enable_by_path("/usb@2");
752 ut_assert(of_device_is_available(ofnode_to_np(node)));
753
754 /* Test disabling devices */
755
756 ut_assert(of_device_is_available(ofnode_to_np(node)));
757 dev_disable_by_path("/usb@2");
758 ut_assert(!of_device_is_available(ofnode_to_np(node)));
759
760 return 0;
761}
Simon Glasse180c2b2020-07-28 19:41:12 -0600762DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
763 UT_TESTF_SCAN_FDT);
Simon Glassd0b4f682018-11-18 08:14:30 -0700764
765/* Test a few uclass phandle functions */
766static int dm_test_fdt_phandle(struct unit_test_state *uts)
767{
768 struct udevice *back, *dev, *dev2;
769
770 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200771 ut_assertnonnull(back);
Simon Glassd0b4f682018-11-18 08:14:30 -0700772 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
773 back, "missing", &dev));
774 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
775 "power-supply", &dev));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200776 ut_assertnonnull(dev);
Simon Glassd0b4f682018-11-18 08:14:30 -0700777 ut_asserteq(0, device_active(dev));
778 ut_asserteq_str("ldo1", dev->name);
779 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
780 "power-supply", &dev2));
781 ut_asserteq_ptr(dev, dev2);
782
783 return 0;
784}
Simon Glasse180c2b2020-07-28 19:41:12 -0600785DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3abe1112018-11-18 08:14:31 -0700786
787/* Test device_find_first_child_by_uclass() */
788static int dm_test_first_child(struct unit_test_state *uts)
789{
790 struct udevice *i2c, *dev, *dev2;
791
792 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
793 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
794 ut_asserteq_str("rtc@43", dev->name);
795 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
796 ut_asserteq_ptr(dev, dev2);
797 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
798 ut_asserteq_str("rtc@61", dev2->name);
799
800 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
801 &dev));
802 ut_asserteq_str("eeprom@2c", dev->name);
803 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
804 ut_asserteq_ptr(dev, dev2);
805
806 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
807 UCLASS_VIDEO, &dev));
808 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
809
810 return 0;
811}
Simon Glasse180c2b2020-07-28 19:41:12 -0600812DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa1b17e42018-12-10 10:37:37 -0700813
814/* Test integer functions in dm_read_...() */
815static int dm_test_read_int(struct unit_test_state *uts)
816{
817 struct udevice *dev;
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +0200818 u8 val8;
819 u16 val16;
Simon Glassa1b17e42018-12-10 10:37:37 -0700820 u32 val32;
821 s32 sval;
822 uint val;
Dario Binacchi70573c62020-03-29 18:04:40 +0200823 u64 val64;
Simon Glassa1b17e42018-12-10 10:37:37 -0700824
825 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
826 ut_asserteq_str("a-test", dev->name);
Stefan Herbrechtsmeierb471bdc2022-06-14 15:21:30 +0200827
828 ut_assertok(dev_read_u8(dev, "int8-value", &val8));
829 ut_asserteq(0x12, val8);
830
831 ut_asserteq(-EINVAL, dev_read_u8(dev, "missing", &val8));
832 ut_asserteq(6, dev_read_u8_default(dev, "missing", 6));
833
834 ut_asserteq(0x12, dev_read_u8_default(dev, "int8-value", 6));
835
836 ut_assertok(dev_read_u16(dev, "int16-value", &val16));
837 ut_asserteq(0x1234, val16);
838
839 ut_asserteq(-EINVAL, dev_read_u16(dev, "missing", &val16));
840 ut_asserteq(6, dev_read_u16_default(dev, "missing", 6));
841
842 ut_asserteq(0x1234, dev_read_u16_default(dev, "int16-value", 6));
843
Simon Glassa1b17e42018-12-10 10:37:37 -0700844 ut_assertok(dev_read_u32(dev, "int-value", &val32));
845 ut_asserteq(1234, val32);
846
847 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
848 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
849
850 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
851 ut_asserteq(1234, val32);
852
853 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
854 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
855
856 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
857 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
858 ut_asserteq(-1234, sval);
859
860 val = 0;
861 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
862 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
863 ut_asserteq(-1234, val);
864
Dario Binacchi70573c62020-03-29 18:04:40 +0200865 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
866 ut_asserteq_64(0x1111222233334444, val64);
867
868 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
869 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
870
871 ut_asserteq_64(0x1111222233334444,
872 dev_read_u64_default(dev, "int64-value", 6));
873
Simon Glassa1b17e42018-12-10 10:37:37 -0700874 return 0;
875}
Simon Glasse180c2b2020-07-28 19:41:12 -0600876DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass50162342020-02-06 09:54:50 -0700877
Dario Binacchi4bb70752020-03-29 18:04:41 +0200878static int dm_test_read_int_index(struct unit_test_state *uts)
879{
880 struct udevice *dev;
881 u32 val32;
882
883 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
884 ut_asserteq_str("a-test", dev->name);
885
886 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
887 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
888
889 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
890 ut_asserteq(5678, val32);
891 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
892 ut_asserteq(9123, val32);
893 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
894 ut_asserteq(4567, val32);
895 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
896 &val32));
897
898 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
899 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
900 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
901 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
902
903 return 0;
904}
Simon Glasse180c2b2020-07-28 19:41:12 -0600905DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi4bb70752020-03-29 18:04:41 +0200906
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +0200907static int dm_test_read_phandle(struct unit_test_state *uts)
908{
909 struct udevice *dev;
910 struct ofnode_phandle_args args;
911 int ret;
912 const char prop[] = "test-gpios";
913 const char cell[] = "#gpio-cells";
914 const char prop2[] = "phandle-value";
915
916 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
917 ut_asserteq_str("a-test", dev->name);
918
919 /* Test dev_count_phandle_with_args with cell name */
920 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
921 ut_asserteq(-ENOENT, ret);
922 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
923 ut_asserteq(-EINVAL, ret);
924 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
925
926 /* Test dev_read_phandle_with_args with cell name */
927 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
928 ut_asserteq(-ENOENT, ret);
929 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
930 ut_asserteq(-EINVAL, ret);
931 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
932 ut_asserteq(1, args.args_count);
933 ut_asserteq(1, args.args[0]);
934 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
935 ut_asserteq(1, args.args_count);
936 ut_asserteq(4, args.args[0]);
937 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
938 ut_asserteq(5, args.args_count);
939 ut_asserteq(5, args.args[0]);
940 ut_asserteq(1, args.args[4]);
941 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
942 ut_asserteq(-ENOENT, ret);
943 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
944 ut_asserteq(1, args.args_count);
945 ut_asserteq(12, args.args[0]);
946 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
947 ut_asserteq(-ENOENT, ret);
948
949 /* Test dev_count_phandle_with_args with cell count */
950 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
951 ut_asserteq(-ENOENT, ret);
952 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
953
954 /* Test dev_read_phandle_with_args with cell count */
955 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
956 ut_asserteq(1, ofnode_valid(args.node));
957 ut_asserteq(1, args.args_count);
958 ut_asserteq(10, args.args[0]);
959 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
960 ut_asserteq(-EINVAL, ret);
961 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
962 ut_asserteq(1, ofnode_valid(args.node));
963 ut_asserteq(1, args.args_count);
964 ut_asserteq(30, args.args[0]);
965 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
966 ut_asserteq(-ENOENT, ret);
967
968 return 0;
969}
970DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
971
Simon Glass50162342020-02-06 09:54:50 -0700972/* Test iteration through devices by drvdata */
973static int dm_test_uclass_drvdata(struct unit_test_state *uts)
974{
975 struct udevice *dev;
976
977 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
978 DM_TEST_TYPE_FIRST, &dev));
979 ut_asserteq_str("a-test", dev->name);
980
981 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
982 DM_TEST_TYPE_SECOND, &dev));
983 ut_asserteq_str("d-test", dev->name);
984
985 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
986 DM_TEST_TYPE_COUNT,
987 &dev));
988
989 return 0;
990}
Simon Glasse180c2b2020-07-28 19:41:12 -0600991DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Tom Rini9a8942b2020-02-11 10:58:41 -0500992
Simon Glassf262d4c2020-01-27 08:49:47 -0700993/* Test device_first_child_ofdata_err(), etc. */
994static int dm_test_child_ofdata(struct unit_test_state *uts)
995{
996 struct udevice *bus, *dev;
997 int count;
998
999 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1000 count = 0;
Simon Glassd1998a92020-12-03 16:55:21 -07001001 device_foreach_child_of_to_plat(dev, bus) {
Simon Glass73466df2020-12-19 10:40:10 -07001002 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1003 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glassf262d4c2020-01-27 08:49:47 -07001004 count++;
1005 }
1006 ut_asserteq(3, count);
1007
1008 return 0;
1009}
Simon Glasse180c2b2020-07-28 19:41:12 -06001010DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass903e83e2020-01-27 08:49:48 -07001011
1012/* Test device_first_child_err(), etc. */
1013static int dm_test_first_child_probe(struct unit_test_state *uts)
1014{
1015 struct udevice *bus, *dev;
1016 int count;
1017
1018 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1019 count = 0;
1020 device_foreach_child_probe(dev, bus) {
Simon Glass73466df2020-12-19 10:40:10 -07001021 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1022 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass903e83e2020-01-27 08:49:48 -07001023 count++;
1024 }
1025 ut_asserteq(3, count);
1026
1027 return 0;
1028}
Simon Glasse180c2b2020-07-28 19:41:12 -06001029DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb0dcc872020-04-05 15:38:19 -06001030
1031/* Test that ofdata is read for parents before children */
1032static int dm_test_ofdata_order(struct unit_test_state *uts)
1033{
1034 struct udevice *bus, *dev;
1035
1036 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1037 ut_assertnonnull(bus);
Simon Glass73466df2020-12-19 10:40:10 -07001038 ut_assert(!(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID));
Simon Glassb0dcc872020-04-05 15:38:19 -06001039
1040 ut_assertok(device_find_first_child(bus, &dev));
1041 ut_assertnonnull(dev);
Simon Glass73466df2020-12-19 10:40:10 -07001042 ut_assert(!(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID));
Simon Glassb0dcc872020-04-05 15:38:19 -06001043
1044 /* read the child's ofdata which should cause the parent's to be read */
Simon Glassd1998a92020-12-03 16:55:21 -07001045 ut_assertok(device_of_to_plat(dev));
Simon Glass73466df2020-12-19 10:40:10 -07001046 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1047 ut_assert(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID);
Simon Glassb0dcc872020-04-05 15:38:19 -06001048
Simon Glass73466df2020-12-19 10:40:10 -07001049 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
1050 ut_assert(!(dev_get_flags(bus) & DM_FLAG_ACTIVATED));
Simon Glassb0dcc872020-04-05 15:38:19 -06001051
1052 return 0;
1053}
Simon Glasse180c2b2020-07-28 19:41:12 -06001054DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi15daa482020-12-30 00:16:26 +01001055
1056/* Test dev_decode_display_timing() */
1057static int dm_test_decode_display_timing(struct unit_test_state *uts)
1058{
1059 struct udevice *dev;
1060 struct display_timing timing;
1061
1062 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1063 ut_asserteq_str("a-test", dev->name);
1064
1065 ut_assertok(dev_decode_display_timing(dev, 0, &timing));
1066 ut_assert(timing.hactive.typ == 240);
1067 ut_assert(timing.hback_porch.typ == 7);
1068 ut_assert(timing.hfront_porch.typ == 6);
1069 ut_assert(timing.hsync_len.typ == 1);
1070 ut_assert(timing.vactive.typ == 320);
1071 ut_assert(timing.vback_porch.typ == 5);
1072 ut_assert(timing.vfront_porch.typ == 8);
1073 ut_assert(timing.vsync_len.typ == 2);
1074 ut_assert(timing.pixelclock.typ == 6500000);
1075 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH);
1076 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1077 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1078 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW);
1079 ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH);
1080 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1081 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE);
1082 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1083 ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED);
1084 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN);
1085 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK);
1086
1087 ut_assertok(dev_decode_display_timing(dev, 1, &timing));
1088 ut_assert(timing.hactive.typ == 480);
1089 ut_assert(timing.hback_porch.typ == 59);
1090 ut_assert(timing.hfront_porch.typ == 10);
1091 ut_assert(timing.hsync_len.typ == 12);
1092 ut_assert(timing.vactive.typ == 800);
1093 ut_assert(timing.vback_porch.typ == 15);
1094 ut_assert(timing.vfront_porch.typ == 17);
1095 ut_assert(timing.vsync_len.typ == 16);
1096 ut_assert(timing.pixelclock.typ == 9000000);
1097 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1098 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_LOW);
1099 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH);
1100 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1101 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1102 ut_assert(timing.flags & DISPLAY_FLAGS_DE_LOW);
1103 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1104 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE);
1105 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1106 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1107 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1108
1109 ut_assertok(dev_decode_display_timing(dev, 2, &timing));
1110 ut_assert(timing.hactive.typ == 800);
1111 ut_assert(timing.hback_porch.typ == 89);
1112 ut_assert(timing.hfront_porch.typ == 164);
1113 ut_assert(timing.hsync_len.typ == 11);
1114 ut_assert(timing.vactive.typ == 480);
1115 ut_assert(timing.vback_porch.typ == 23);
1116 ut_assert(timing.vfront_porch.typ == 10);
1117 ut_assert(timing.vsync_len.typ == 13);
1118 ut_assert(timing.pixelclock.typ == 33500000);
1119 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1120 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1121 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1122 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1123 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1124 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1125 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1126 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1127 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1128 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1129 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1130
1131 ut_assert(dev_decode_display_timing(dev, 3, &timing));
1132 return 0;
1133}
1134DM_TEST(dm_test_decode_display_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Patrick Delaunay269fa842021-05-21 12:25:00 +02001135
1136/* Test read_resourcee() */
1137static int dm_test_read_resource(struct unit_test_state *uts)
1138{
1139 struct udevice *dev;
1140 struct resource res;
1141
1142 /* test resource without translation */
1143 ut_assertok(uclass_find_device_by_name(UCLASS_SIMPLE_BUS, "syscon@2", &dev));
1144 ut_assertok(dev_read_resource(dev, 0, &res));
1145 ut_asserteq(0x40, res.start);
1146 ut_asserteq(0x44, res.end);
1147 ut_assertok(dev_read_resource(dev, 1, &res));
1148 ut_asserteq(0x48, res.start);
1149 ut_asserteq(0x4d, res.end);
1150
1151 /* test resource with translation */
1152 ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@1,100", &dev));
1153 ut_assertok(dev_read_resource(dev, 0, &res));
1154 ut_asserteq(0x9000, res.start);
1155 ut_asserteq(0x9fff, res.end);
1156
1157 /* test named resource */
1158 ut_assertok(uclass_find_device_by_name(UCLASS_TEST_DUMMY, "dev@0,0", &dev));
1159 ut_assertok(dev_read_resource_byname(dev, "sandbox-dummy-0", &res));
1160 ut_asserteq(0x8000, res.start);
1161 ut_asserteq(0x8fff, res.end);
1162
1163 return 0;
1164}
1165
1166DM_TEST(dm_test_read_resource, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);