blob: 6e83aeecd928e747ee4ba95b31cf6399f66786ef [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>
Dario Binacchid64b9cd2020-12-30 00:16:21 +01008#include <dm/device_compat.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -07009#include <errno.h>
10#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070012#include <malloc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070014#include <asm/io.h>
15#include <dm/test.h>
16#include <dm/root.h>
Simon Glass98561572017-04-23 20:10:44 -060017#include <dm/device-internal.h>
Simon Glass61b29b82020-02-03 07:36:15 -070018#include <dm/devres.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070019#include <dm/uclass-internal.h>
20#include <dm/util.h>
Mario Six2ea4d0d2018-06-26 08:46:49 +020021#include <dm/lists.h>
22#include <dm/of_access.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060023#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050024#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070025
26DECLARE_GLOBAL_DATA_PTR;
27
Simon Glass98561572017-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 Glassc69cda22020-12-03 16:55:20 -070034 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
Simon Glass98561572017-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 Glasscaa4daa2020-12-03 16:55:18 -070049 .plat_auto = sizeof(struct dm_testprobe_pdata),
Simon Glass98561572017-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 Glassdc12ebb2019-12-29 21:19:25 -070058struct dm_testdevres_pdata {
59 void *ptr;
60};
61
62struct dm_testdevres_priv {
63 void *ptr;
Simon Glass42a0ce52019-12-29 21:19:28 -070064 void *ptr_ofdata;
Simon Glassdc12ebb2019-12-29 21:19:25 -070065};
66
67static int testdevres_drv_bind(struct udevice *dev)
68{
Simon Glassc69cda22020-12-03 16:55:20 -070069 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
Simon Glassdc12ebb2019-12-29 21:19:25 -070070
71 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
72
73 return 0;
74}
75
Simon Glassd1998a92020-12-03 16:55:21 -070076static int testdevres_drv_of_to_plat(struct udevice *dev)
Simon Glass42a0ce52019-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 Glassdc12ebb2019-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 Glassd1998a92020-12-03 16:55:21 -0700104 .of_to_plat = testdevres_drv_of_to_plat,
Simon Glassdc12ebb2019-12-29 21:19:25 -0700105 .probe = testdevres_drv_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700106 .plat_auto = sizeof(struct dm_testdevres_pdata),
Simon Glass41575d82020-12-03 16:55:17 -0700107 .priv_auto = sizeof(struct dm_testdevres_priv),
Simon Glassdc12ebb2019-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 Hershbergere721b882015-05-20 14:27:27 -0500116int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700117{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200118 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700119 int ret;
120 int i;
121
Simon Glass2e7d35d2014-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 Glass1ca7e202014-07-23 06:55:18 -0600126 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-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 Glasseb9ef5f2014-07-23 06:54:57 -0600133 * Get the 'ping-expect' property, which tells us what the
Simon Glasscaa4daa2020-12-03 16:55:18 -0700134 * ping add should be. We don't use the plat because we
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600135 * want to test the code that sets that up
136 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700137 */
Simon Glasse160f7d2017-01-17 16:52:55 -0700138 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600139 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700140 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glasse160f7d2017-01-17 16:52:55 -0700141 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glass2e7d35d2014-02-26 15:59:21 -0700142
Joe Hershbergere721b882015-05-20 14:27:27 -0500143 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700144 dev_get_priv(dev)));
145 }
146
147 return 0;
148}
Simon Glass1ca7e202014-07-23 06:55:18 -0600149
150/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500151static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600152{
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530153 const int num_devices = 9;
Simon Glass1ca7e202014-07-23 06:55:18 -0600154 struct udevice *dev;
155 struct uclass *uc;
156 int ret;
157 int i;
158
Simon Glass8ee05b52020-11-28 17:50:10 -0700159 ret = dm_extended_scan(false);
Simon Glass1ca7e202014-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 */
166 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
167
Simon Glassf8a85442015-01-25 08:27:00 -0700168 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-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 Glass0fd3d912020-12-22 19:30:28 -0700173 ut_assert(dev_get_plat(dev));
Simon Glass1ca7e202014-07-23 06:55:18 -0600174 }
175
Joe Hershbergere721b882015-05-20 14:27:27 -0500176 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600177
178 return 0;
179}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700180DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600181
Michal Simeka93eb572019-01-31 16:31:00 +0100182static int dm_test_alias_highest_id(struct unit_test_state *uts)
183{
184 int ret;
185
186 ret = dev_read_alias_highest_id("eth");
187 ut_asserteq(5, ret);
188
189 ret = dev_read_alias_highest_id("gpio");
Patrick Delaunayff526652020-01-13 11:35:14 +0100190 ut_asserteq(3, ret);
Michal Simeka93eb572019-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 Hershbergere721b882015-05-20 14:27:27 -0500205static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-07-23 06:55:03 -0600206{
207 struct uclass *uc;
208 int ret;
209
Simon Glass725e4fc2020-11-28 17:50:09 -0700210 ret = dm_scan_fdt(true);
Simon Glass00606d72014-07-23 06:55:03 -0600211 ut_assert(!ret);
212
213 ret = uclass_get(UCLASS_TEST_FDT, &uc);
214 ut_assert(!ret);
215
Bin Meng2786cd72018-10-10 22:07:01 -0700216 /*
217 * These are 2 pre-reloc devices:
218 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
219 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
220 */
221 ut_asserteq(2, list_count_items(&uc->dev_head));
Simon Glass00606d72014-07-23 06:55:03 -0600222
223 return 0;
224}
225DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600226
227/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500228static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600229{
230 struct udevice *dev;
231
232 /* A few basic santiy tests */
Simon Glass99175912020-12-16 21:20:29 -0700233 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600234 ut_asserteq_str("b-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700235 ut_asserteq(3, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600236
Simon Glass99175912020-12-16 21:20:29 -0700237 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600238 ut_asserteq_str("a-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700239 ut_asserteq(8, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600240
Simon Glass981426e2020-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 Glass99175912020-12-16 21:20:29 -0700245 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev));
Simon Glass981426e2020-12-16 21:20:26 -0700246 ut_asserteq_str("d-test", dev->name);
247 ut_asserteq(13, dev_seq(dev));
248
Simon Glass99175912020-12-16 21:20:29 -0700249 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass981426e2020-12-16 21:20:26 -0700250 &dev));
Simon Glass5a66a8f2014-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 Glass981426e2020-12-16 21:20:26 -0700256 ut_asserteq(6, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600257
Simon Glass1ca7e202014-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 Glass5a66a8f2014-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 Glass981426e2020-12-16 21:20:26 -0700264 ut_asserteq(3, dev_seq(dev));
Simon Glass5a66a8f2014-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 Glass981426e2020-12-16 21:20:26 -0700272 ut_asserteq(13, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600273
Simon Glass981426e2020-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 Glass5a66a8f2014-07-23 06:55:12 -0600278
Simon Glass981426e2020-12-16 21:20:26 -0700279 /* And we should still have holes in our sequence numbers */
Simon Glass99175912020-12-16 21:20:29 -0700280 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0,
Simon Glass981426e2020-12-16 21:20:26 -0700281 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700282 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1,
Simon Glass981426e2020-12-16 21:20:26 -0700283 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700284 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
Simon Glass981426e2020-12-16 21:20:26 -0700285 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700286 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
Simon Glass981426e2020-12-16 21:20:26 -0700287 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700288 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
Simon Glass981426e2020-12-16 21:20:26 -0700289 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700290 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass981426e2020-12-16 21:20:26 -0700291 &dev));
292 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
Simon Glass99175912020-12-16 21:20:29 -0700293 &dev));
Simon Glass981426e2020-12-16 21:20:26 -0700294 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
Simon Glass99175912020-12-16 21:20:29 -0700295 &dev));
Michael Wallebe1a6e92020-06-02 01:47:09 +0200296
Simon Glass5a66a8f2014-07-23 06:55:12 -0600297 return 0;
298}
Simon Glasse180c2b2020-07-28 19:41:12 -0600299DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600300
Simon Glass93f44e82020-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 Glass7f20d1d2020-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 Glass65e25be2020-12-28 20:34:56 -0700333 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(testfdt_drv),
Simon Glass7f20d1d2020-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 Glass65e25be2020-12-28 20:34:56 -0700353 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(testfdt_drv),
Simon Glass7f20d1d2020-12-16 21:20:32 -0700354 "fred", NULL, ofnode_null(), &dev));
355 ut_asserteq(19, dev_seq(dev));
356
Simon Glass65e25be2020-12-28 20:34:56 -0700357 ut_assertok(device_bind(dm_root(), DM_DRIVER_GET(testfdt_drv),
Simon Glass7f20d1d2020-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 Glassf4cdead2014-07-23 06:55:14 -0600365/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500366static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-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 Glass1ca7e202014-07-23 06:55:18 -0600385 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-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 Glass86b54ec2017-05-18 20:09:44 -0600392DM_TEST(dm_test_fdt_offset,
Simon Glasse180c2b2020-07-28 19:41:12 -0600393 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass98561572017-04-23 20:10:44 -0600394
395/**
396 * Test various error conditions with uclass_first_device() and
397 * uclass_next_device()
398 */
399static int dm_test_first_next_device(struct unit_test_state *uts)
400{
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 */
407 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
408 dev;
409 ret = uclass_next_device(&dev)) {
410 count++;
411 parent = dev_get_parent(dev);
412 }
413 ut_assertok(ret);
414 ut_asserteq(4, count);
415
416 /* Remove them and try again, with an error on the second one */
417 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
Simon Glassc69cda22020-12-03 16:55:20 -0700418 pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600419 pdata->probe_err = -ENOMEM;
420 device_remove(parent, DM_REMOVE_NORMAL);
421 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
422 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
423 ut_asserteq_ptr(dev, NULL);
424
425 /* Now an error on the first one */
426 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
Simon Glassc69cda22020-12-03 16:55:20 -0700427 pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600428 pdata->probe_err = -ENOENT;
429 device_remove(parent, DM_REMOVE_NORMAL);
430 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
431
432 return 0;
433}
Simon Glasse180c2b2020-07-28 19:41:12 -0600434DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass95ce3852017-04-23 20:10:45 -0600435
Simon Glass3cf0fba2020-02-06 09:54:49 -0700436/* Test iteration through devices in a uclass */
437static int dm_test_uclass_foreach(struct unit_test_state *uts)
438{
439 struct udevice *dev;
440 struct uclass *uc;
441 int count;
442
443 count = 0;
444 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
445 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530446 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700447
448 count = 0;
449 uclass_foreach_dev(dev, uc)
450 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530451 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700452
453 return 0;
454}
Simon Glasse180c2b2020-07-28 19:41:12 -0600455DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700456
Simon Glass95ce3852017-04-23 20:10:45 -0600457/**
458 * check_devices() - Check return values and pointers
459 *
460 * This runs through a full sequence of uclass_first_device_check()...
461 * uclass_next_device_check() checking that the return values and devices
462 * are correct.
463 *
464 * @uts: Test state
465 * @devlist: List of expected devices
466 * @mask: Indicates which devices should return an error. Device n should
467 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
468 * bit n is clear.
469 */
470static int check_devices(struct unit_test_state *uts,
471 struct udevice *devlist[], int mask)
472{
473 int expected_ret;
474 struct udevice *dev;
475 int i;
476
477 expected_ret = (mask & 1) ? -ENOENT : 0;
478 mask >>= 1;
479 ut_asserteq(expected_ret,
480 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
481 for (i = 0; i < 4; i++) {
482 ut_asserteq_ptr(devlist[i], dev);
483 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
484 mask >>= 1;
485 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
486 }
487 ut_asserteq_ptr(NULL, dev);
488
489 return 0;
490}
491
492/* Test uclass_first_device_check() and uclass_next_device_check() */
493static int dm_test_first_next_ok_device(struct unit_test_state *uts)
494{
495 struct dm_testprobe_pdata *pdata;
496 struct udevice *dev, *parent = NULL, *devlist[4];
497 int count;
498 int ret;
499
500 /* There should be 4 devices */
501 count = 0;
502 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
503 dev;
504 ret = uclass_next_device_check(&dev)) {
505 ut_assertok(ret);
506 devlist[count++] = dev;
507 parent = dev_get_parent(dev);
508 }
509 ut_asserteq(4, count);
510 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
511 ut_assertok(check_devices(uts, devlist, 0));
512
513 /* Remove them and try again, with an error on the second one */
Simon Glassc69cda22020-12-03 16:55:20 -0700514 pdata = dev_get_plat(devlist[1]);
Simon Glass95ce3852017-04-23 20:10:45 -0600515 pdata->probe_err = -ENOENT - 1;
516 device_remove(parent, DM_REMOVE_NORMAL);
517 ut_assertok(check_devices(uts, devlist, 1 << 1));
518
519 /* Now an error on the first one */
Simon Glassc69cda22020-12-03 16:55:20 -0700520 pdata = dev_get_plat(devlist[0]);
Simon Glass95ce3852017-04-23 20:10:45 -0600521 pdata->probe_err = -ENOENT - 0;
522 device_remove(parent, DM_REMOVE_NORMAL);
523 ut_assertok(check_devices(uts, devlist, 3 << 0));
524
525 /* Now errors on all */
Simon Glassc69cda22020-12-03 16:55:20 -0700526 pdata = dev_get_plat(devlist[2]);
Simon Glass95ce3852017-04-23 20:10:45 -0600527 pdata->probe_err = -ENOENT - 2;
Simon Glassc69cda22020-12-03 16:55:20 -0700528 pdata = dev_get_plat(devlist[3]);
Simon Glass95ce3852017-04-23 20:10:45 -0600529 pdata->probe_err = -ENOENT - 3;
530 device_remove(parent, DM_REMOVE_NORMAL);
531 ut_assertok(check_devices(uts, devlist, 0xf << 0));
532
533 return 0;
534}
Simon Glasse180c2b2020-07-28 19:41:12 -0600535DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Sixe8d52912018-03-12 14:53:33 +0100536
537static const struct udevice_id fdt_dummy_ids[] = {
538 { .compatible = "denx,u-boot-fdt-dummy", },
539 { }
540};
541
542UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca507cef32018-05-19 14:13:55 +0200543 .name = "fdt-dummy",
Mario Sixe8d52912018-03-12 14:53:33 +0100544 .id = UCLASS_TEST_DUMMY,
545 .flags = DM_UC_FLAG_SEQ_ALIAS,
546};
547
548U_BOOT_DRIVER(fdt_dummy_drv) = {
549 .name = "fdt_dummy_drv",
550 .of_match = fdt_dummy_ids,
551 .id = UCLASS_TEST_DUMMY,
552};
553
Dario Binacchid64b9cd2020-12-30 00:16:21 +0100554static int zero_size_cells_bus_bind(struct udevice *dev)
555{
556 ofnode child;
557 int err;
558
559 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
560 if (ofnode_get_property(child, "compatible", NULL))
561 continue;
562
563 err = device_bind_driver_to_node(dev,
564 "zero_size_cells_bus_child_drv",
565 "zero_size_cells_bus_child",
566 child, NULL);
567 if (err) {
568 dev_err(dev, "%s: failed to bind %s\n", __func__,
569 ofnode_get_name(child));
570 return err;
571 }
572 }
573
574 return 0;
575}
576
577static const struct udevice_id zero_size_cells_bus_ids[] = {
578 { .compatible = "sandbox,zero-size-cells-bus" },
579 { }
580};
581
582U_BOOT_DRIVER(zero_size_cells_bus) = {
583 .name = "zero_size_cells_bus_drv",
584 .id = UCLASS_TEST_DUMMY,
585 .of_match = zero_size_cells_bus_ids,
586 .bind = zero_size_cells_bus_bind,
587};
588
589static int zero_size_cells_bus_child_bind(struct udevice *dev)
590{
591 ofnode child;
592 int err;
593
594 ofnode_for_each_subnode(child, dev_ofnode(dev)) {
595 err = lists_bind_fdt(dev, child, NULL, false);
596 if (err) {
597 dev_err(dev, "%s: lists_bind_fdt, err=%d\n",
598 __func__, err);
599 return err;
600 }
601 }
602
603 return 0;
604}
605
606U_BOOT_DRIVER(zero_size_cells_bus_child_drv) = {
607 .name = "zero_size_cells_bus_child_drv",
608 .id = UCLASS_TEST_DUMMY,
609 .bind = zero_size_cells_bus_child_bind,
610};
611
Mario Sixe8d52912018-03-12 14:53:33 +0100612static int dm_test_fdt_translation(struct unit_test_state *uts)
613{
614 struct udevice *dev;
Fabien Dessenne641067f2019-05-31 15:11:30 +0200615 fdt32_t dma_addr[2];
Mario Sixe8d52912018-03-12 14:53:33 +0100616
617 /* Some simple translations */
Simon Glass99175912020-12-16 21:20:29 -0700618 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100619 ut_asserteq_str("dev@0,0", dev->name);
620 ut_asserteq(0x8000, dev_read_addr(dev));
621
Simon Glass99175912020-12-16 21:20:29 -0700622 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100623 ut_asserteq_str("dev@1,100", dev->name);
624 ut_asserteq(0x9000, dev_read_addr(dev));
625
Simon Glass99175912020-12-16 21:20:29 -0700626 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100627 ut_asserteq_str("dev@2,200", dev->name);
628 ut_asserteq(0xA000, dev_read_addr(dev));
629
630 /* No translation for busses with #size-cells == 0 */
Simon Glass99175912020-12-16 21:20:29 -0700631 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100632 ut_asserteq_str("dev@42", dev->name);
Dario Binacchid64b9cd2020-12-30 00:16:21 +0100633 /* No translation for busses with #size-cells == 0 */
Mario Sixe8d52912018-03-12 14:53:33 +0100634 ut_asserteq(0x42, dev_read_addr(dev));
635
Dario Binacchid64b9cd2020-12-30 00:16:21 +0100636 /* Translation for busses with #size-cells == 0 */
637 gd->dm_flags |= GD_DM_FLG_SIZE_CELLS_0;
638 ut_asserteq(0x8042, dev_read_addr(dev));
639 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 4, &dev));
640 ut_asserteq_str("dev@19", dev->name);
641 ut_asserteq(0xc019, dev_read_addr(dev));
642 gd->dm_flags &= ~GD_DM_FLG_SIZE_CELLS_0;
643
Fabien Dessenne641067f2019-05-31 15:11:30 +0200644 /* dma address translation */
Simon Glass99175912020-12-16 21:20:29 -0700645 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Fabien Dessenne641067f2019-05-31 15:11:30 +0200646 dma_addr[0] = cpu_to_be32(0);
647 dma_addr[1] = cpu_to_be32(0);
648 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
649
Simon Glass99175912020-12-16 21:20:29 -0700650 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Fabien Dessenne641067f2019-05-31 15:11:30 +0200651 dma_addr[0] = cpu_to_be32(1);
652 dma_addr[1] = cpu_to_be32(0x100);
653 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
654
Mario Sixe8d52912018-03-12 14:53:33 +0100655 return 0;
656}
Simon Glasse180c2b2020-07-28 19:41:12 -0600657DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200658
Ovidiu Panait6e648302020-08-03 22:17:36 +0300659static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
660{
661 struct udevice *gpio, *dev;
662 void *ptr;
663
664 /* Test for missing reg property */
665 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
666 ut_assertnull(devfdt_get_addr_ptr(gpio));
667
Simon Glass99175912020-12-16 21:20:29 -0700668 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Ovidiu Panait6e648302020-08-03 22:17:36 +0300669 ptr = devfdt_get_addr_ptr(dev);
670 ut_asserteq_ptr((void *)0x8000, ptr);
671
672 return 0;
673}
674DM_TEST(dm_test_fdt_get_addr_ptr_flat,
675 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
676
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200677static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
678{
679 struct udevice *dev;
680 fdt_addr_t addr;
681 void *paddr;
682
Simon Glass99175912020-12-16 21:20:29 -0700683 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200684
685 addr = devfdt_get_addr(dev);
686 ut_asserteq(0x8000, addr);
687
688 paddr = map_physmem(addr, 0, MAP_NOCACHE);
689 ut_assertnonnull(paddr);
690 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
691
692 return 0;
693}
694DM_TEST(dm_test_fdt_remap_addr_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600695 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200696
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100697static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
698{
699 struct udevice *dev;
700 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530701 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100702 void *paddr;
703
Simon Glass99175912020-12-16 21:20:29 -0700704 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100705
Sekhar Norif5b90472019-08-01 19:12:56 +0530706 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100707 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530708 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100709
710 paddr = map_physmem(addr, 0, MAP_NOCACHE);
711 ut_assertnonnull(paddr);
712 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
713
714 return 0;
715}
716DM_TEST(dm_test_fdt_remap_addr_index_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600717 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100718
719static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
720{
721 struct udevice *dev;
722 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530723 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100724 void *paddr;
725
Simon Glass99175912020-12-16 21:20:29 -0700726 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100727
Sekhar Norif5b90472019-08-01 19:12:56 +0530728 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100729 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530730 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100731
732 paddr = map_physmem(addr, 0, MAP_NOCACHE);
733 ut_assertnonnull(paddr);
734 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
735
736 return 0;
737}
738DM_TEST(dm_test_fdt_remap_addr_name_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600739 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100740
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200741static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
742{
743 struct udevice *dev;
744 fdt_addr_t addr;
745 void *paddr;
746
Simon Glass99175912020-12-16 21:20:29 -0700747 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200748
749 addr = dev_read_addr(dev);
750 ut_asserteq(0x8000, addr);
751
752 paddr = map_physmem(addr, 0, MAP_NOCACHE);
753 ut_assertnonnull(paddr);
754 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
755
756 return 0;
757}
758DM_TEST(dm_test_fdt_remap_addr_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600759 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six2ea4d0d2018-06-26 08:46:49 +0200760
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100761static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
762{
763 struct udevice *dev;
764 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530765 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100766 void *paddr;
767
Simon Glass99175912020-12-16 21:20:29 -0700768 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100769
Sekhar Norif5b90472019-08-01 19:12:56 +0530770 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100771 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530772 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100773
774 paddr = map_physmem(addr, 0, MAP_NOCACHE);
775 ut_assertnonnull(paddr);
776 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
777
778 return 0;
779}
780DM_TEST(dm_test_fdt_remap_addr_index_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600781 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100782
783static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
784{
785 struct udevice *dev;
786 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530787 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100788 void *paddr;
789
Simon Glass99175912020-12-16 21:20:29 -0700790 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100791
Sekhar Norif5b90472019-08-01 19:12:56 +0530792 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100793 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530794 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100795
796 paddr = map_physmem(addr, 0, MAP_NOCACHE);
797 ut_assertnonnull(paddr);
798 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
799
800 return 0;
801}
802DM_TEST(dm_test_fdt_remap_addr_name_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600803 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100804
Mario Six2ea4d0d2018-06-26 08:46:49 +0200805static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
806{
807 struct udevice *dev;
808 ofnode node;
809
810 if (!of_live_active()) {
811 printf("Live tree not active; ignore test\n");
812 return 0;
813 }
814
815 /* Test enabling devices */
816
817 node = ofnode_path("/usb@2");
818
819 ut_assert(!of_device_is_available(ofnode_to_np(node)));
820 ofnode_set_enabled(node, true);
821 ut_assert(of_device_is_available(ofnode_to_np(node)));
822
823 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
824 &dev);
Simon Glass99175912020-12-16 21:20:29 -0700825 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
Mario Six2ea4d0d2018-06-26 08:46:49 +0200826
827 /* Test string property setting */
828
829 ut_assert(device_is_compatible(dev, "sandbox,usb"));
830 ofnode_write_string(node, "compatible", "gdsys,super-usb");
831 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
832 ofnode_write_string(node, "compatible", "sandbox,usb");
833 ut_assert(device_is_compatible(dev, "sandbox,usb"));
834
835 /* Test setting generic properties */
836
837 /* Non-existent in DTB */
838 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
839 /* reg = 0x42, size = 0x100 */
840 ut_assertok(ofnode_write_prop(node, "reg", 8,
841 "\x00\x00\x00\x42\x00\x00\x01\x00"));
842 ut_asserteq(0x42, dev_read_addr(dev));
843
844 /* Test disabling devices */
845
846 device_remove(dev, DM_REMOVE_NORMAL);
847 device_unbind(dev);
848
849 ut_assert(of_device_is_available(ofnode_to_np(node)));
850 ofnode_set_enabled(node, false);
851 ut_assert(!of_device_is_available(ofnode_to_np(node)));
852
853 return 0;
854}
Simon Glasse180c2b2020-07-28 19:41:12 -0600855DM_TEST(dm_test_fdt_livetree_writing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six172942a2018-06-26 08:46:51 +0200856
857static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
858{
859 ofnode node;
860
861 if (!of_live_active()) {
862 printf("Live tree not active; ignore test\n");
863 return 0;
864 }
865
866 node = ofnode_path("/usb@2");
867
868 /* Test enabling devices */
869
870 ut_assert(!of_device_is_available(ofnode_to_np(node)));
871 dev_enable_by_path("/usb@2");
872 ut_assert(of_device_is_available(ofnode_to_np(node)));
873
874 /* Test disabling devices */
875
876 ut_assert(of_device_is_available(ofnode_to_np(node)));
877 dev_disable_by_path("/usb@2");
878 ut_assert(!of_device_is_available(ofnode_to_np(node)));
879
880 return 0;
881}
Simon Glasse180c2b2020-07-28 19:41:12 -0600882DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
883 UT_TESTF_SCAN_FDT);
Simon Glassd0b4f682018-11-18 08:14:30 -0700884
885/* Test a few uclass phandle functions */
886static int dm_test_fdt_phandle(struct unit_test_state *uts)
887{
888 struct udevice *back, *dev, *dev2;
889
890 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200891 ut_assertnonnull(back);
Simon Glassd0b4f682018-11-18 08:14:30 -0700892 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
893 back, "missing", &dev));
894 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
895 "power-supply", &dev));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200896 ut_assertnonnull(dev);
Simon Glassd0b4f682018-11-18 08:14:30 -0700897 ut_asserteq(0, device_active(dev));
898 ut_asserteq_str("ldo1", dev->name);
899 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
900 "power-supply", &dev2));
901 ut_asserteq_ptr(dev, dev2);
902
903 return 0;
904}
Simon Glasse180c2b2020-07-28 19:41:12 -0600905DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3abe1112018-11-18 08:14:31 -0700906
907/* Test device_find_first_child_by_uclass() */
908static int dm_test_first_child(struct unit_test_state *uts)
909{
910 struct udevice *i2c, *dev, *dev2;
911
912 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
913 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
914 ut_asserteq_str("rtc@43", dev->name);
915 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
916 ut_asserteq_ptr(dev, dev2);
917 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
918 ut_asserteq_str("rtc@61", dev2->name);
919
920 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
921 &dev));
922 ut_asserteq_str("eeprom@2c", dev->name);
923 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
924 ut_asserteq_ptr(dev, dev2);
925
926 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
927 UCLASS_VIDEO, &dev));
928 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
929
930 return 0;
931}
Simon Glasse180c2b2020-07-28 19:41:12 -0600932DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa1b17e42018-12-10 10:37:37 -0700933
934/* Test integer functions in dm_read_...() */
935static int dm_test_read_int(struct unit_test_state *uts)
936{
937 struct udevice *dev;
938 u32 val32;
939 s32 sval;
940 uint val;
Dario Binacchi70573c62020-03-29 18:04:40 +0200941 u64 val64;
Simon Glassa1b17e42018-12-10 10:37:37 -0700942
943 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
944 ut_asserteq_str("a-test", dev->name);
945 ut_assertok(dev_read_u32(dev, "int-value", &val32));
946 ut_asserteq(1234, val32);
947
948 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
949 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
950
951 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
952 ut_asserteq(1234, val32);
953
954 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
955 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
956
957 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
958 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
959 ut_asserteq(-1234, sval);
960
961 val = 0;
962 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
963 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
964 ut_asserteq(-1234, val);
965
Dario Binacchi70573c62020-03-29 18:04:40 +0200966 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
967 ut_asserteq_64(0x1111222233334444, val64);
968
969 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
970 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
971
972 ut_asserteq_64(0x1111222233334444,
973 dev_read_u64_default(dev, "int64-value", 6));
974
Simon Glassa1b17e42018-12-10 10:37:37 -0700975 return 0;
976}
Simon Glasse180c2b2020-07-28 19:41:12 -0600977DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass50162342020-02-06 09:54:50 -0700978
Dario Binacchi4bb70752020-03-29 18:04:41 +0200979static int dm_test_read_int_index(struct unit_test_state *uts)
980{
981 struct udevice *dev;
982 u32 val32;
983
984 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
985 ut_asserteq_str("a-test", dev->name);
986
987 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
988 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
989
990 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
991 ut_asserteq(5678, val32);
992 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
993 ut_asserteq(9123, val32);
994 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
995 ut_asserteq(4567, val32);
996 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
997 &val32));
998
999 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
1000 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
1001 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
1002 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
1003
1004 return 0;
1005}
Simon Glasse180c2b2020-07-28 19:41:12 -06001006DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi4bb70752020-03-29 18:04:41 +02001007
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +02001008static int dm_test_read_phandle(struct unit_test_state *uts)
1009{
1010 struct udevice *dev;
1011 struct ofnode_phandle_args args;
1012 int ret;
1013 const char prop[] = "test-gpios";
1014 const char cell[] = "#gpio-cells";
1015 const char prop2[] = "phandle-value";
1016
1017 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1018 ut_asserteq_str("a-test", dev->name);
1019
1020 /* Test dev_count_phandle_with_args with cell name */
1021 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
1022 ut_asserteq(-ENOENT, ret);
1023 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
1024 ut_asserteq(-EINVAL, ret);
1025 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
1026
1027 /* Test dev_read_phandle_with_args with cell name */
1028 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
1029 ut_asserteq(-ENOENT, ret);
1030 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
1031 ut_asserteq(-EINVAL, ret);
1032 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
1033 ut_asserteq(1, args.args_count);
1034 ut_asserteq(1, args.args[0]);
1035 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
1036 ut_asserteq(1, args.args_count);
1037 ut_asserteq(4, args.args[0]);
1038 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
1039 ut_asserteq(5, args.args_count);
1040 ut_asserteq(5, args.args[0]);
1041 ut_asserteq(1, args.args[4]);
1042 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
1043 ut_asserteq(-ENOENT, ret);
1044 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
1045 ut_asserteq(1, args.args_count);
1046 ut_asserteq(12, args.args[0]);
1047 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
1048 ut_asserteq(-ENOENT, ret);
1049
1050 /* Test dev_count_phandle_with_args with cell count */
1051 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
1052 ut_asserteq(-ENOENT, ret);
1053 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
1054
1055 /* Test dev_read_phandle_with_args with cell count */
1056 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
1057 ut_asserteq(1, ofnode_valid(args.node));
1058 ut_asserteq(1, args.args_count);
1059 ut_asserteq(10, args.args[0]);
1060 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
1061 ut_asserteq(-EINVAL, ret);
1062 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
1063 ut_asserteq(1, ofnode_valid(args.node));
1064 ut_asserteq(1, args.args_count);
1065 ut_asserteq(30, args.args[0]);
1066 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
1067 ut_asserteq(-ENOENT, ret);
1068
1069 return 0;
1070}
1071DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1072
Simon Glass50162342020-02-06 09:54:50 -07001073/* Test iteration through devices by drvdata */
1074static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1075{
1076 struct udevice *dev;
1077
1078 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1079 DM_TEST_TYPE_FIRST, &dev));
1080 ut_asserteq_str("a-test", dev->name);
1081
1082 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1083 DM_TEST_TYPE_SECOND, &dev));
1084 ut_asserteq_str("d-test", dev->name);
1085
1086 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1087 DM_TEST_TYPE_COUNT,
1088 &dev));
1089
1090 return 0;
1091}
Simon Glasse180c2b2020-07-28 19:41:12 -06001092DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Tom Rini9a8942b2020-02-11 10:58:41 -05001093
Simon Glassf262d4c2020-01-27 08:49:47 -07001094/* Test device_first_child_ofdata_err(), etc. */
1095static int dm_test_child_ofdata(struct unit_test_state *uts)
1096{
1097 struct udevice *bus, *dev;
1098 int count;
1099
1100 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1101 count = 0;
Simon Glassd1998a92020-12-03 16:55:21 -07001102 device_foreach_child_of_to_plat(dev, bus) {
Simon Glass73466df2020-12-19 10:40:10 -07001103 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1104 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
Simon Glassf262d4c2020-01-27 08:49:47 -07001105 count++;
1106 }
1107 ut_asserteq(3, count);
1108
1109 return 0;
1110}
Simon Glasse180c2b2020-07-28 19:41:12 -06001111DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass903e83e2020-01-27 08:49:48 -07001112
1113/* Test device_first_child_err(), etc. */
1114static int dm_test_first_child_probe(struct unit_test_state *uts)
1115{
1116 struct udevice *bus, *dev;
1117 int count;
1118
1119 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1120 count = 0;
1121 device_foreach_child_probe(dev, bus) {
Simon Glass73466df2020-12-19 10:40:10 -07001122 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1123 ut_assert(dev_get_flags(dev) & DM_FLAG_ACTIVATED);
Simon Glass903e83e2020-01-27 08:49:48 -07001124 count++;
1125 }
1126 ut_asserteq(3, count);
1127
1128 return 0;
1129}
Simon Glasse180c2b2020-07-28 19:41:12 -06001130DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb0dcc872020-04-05 15:38:19 -06001131
1132/* Test that ofdata is read for parents before children */
1133static int dm_test_ofdata_order(struct unit_test_state *uts)
1134{
1135 struct udevice *bus, *dev;
1136
1137 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1138 ut_assertnonnull(bus);
Simon Glass73466df2020-12-19 10:40:10 -07001139 ut_assert(!(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID));
Simon Glassb0dcc872020-04-05 15:38:19 -06001140
1141 ut_assertok(device_find_first_child(bus, &dev));
1142 ut_assertnonnull(dev);
Simon Glass73466df2020-12-19 10:40:10 -07001143 ut_assert(!(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID));
Simon Glassb0dcc872020-04-05 15:38:19 -06001144
1145 /* read the child's ofdata which should cause the parent's to be read */
Simon Glassd1998a92020-12-03 16:55:21 -07001146 ut_assertok(device_of_to_plat(dev));
Simon Glass73466df2020-12-19 10:40:10 -07001147 ut_assert(dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID);
1148 ut_assert(dev_get_flags(bus) & DM_FLAG_PLATDATA_VALID);
Simon Glassb0dcc872020-04-05 15:38:19 -06001149
Simon Glass73466df2020-12-19 10:40:10 -07001150 ut_assert(!(dev_get_flags(dev) & DM_FLAG_ACTIVATED));
1151 ut_assert(!(dev_get_flags(bus) & DM_FLAG_ACTIVATED));
Simon Glassb0dcc872020-04-05 15:38:19 -06001152
1153 return 0;
1154}
Simon Glasse180c2b2020-07-28 19:41:12 -06001155DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi15daa482020-12-30 00:16:26 +01001156
1157/* Test dev_decode_display_timing() */
1158static int dm_test_decode_display_timing(struct unit_test_state *uts)
1159{
1160 struct udevice *dev;
1161 struct display_timing timing;
1162
1163 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1164 ut_asserteq_str("a-test", dev->name);
1165
1166 ut_assertok(dev_decode_display_timing(dev, 0, &timing));
1167 ut_assert(timing.hactive.typ == 240);
1168 ut_assert(timing.hback_porch.typ == 7);
1169 ut_assert(timing.hfront_porch.typ == 6);
1170 ut_assert(timing.hsync_len.typ == 1);
1171 ut_assert(timing.vactive.typ == 320);
1172 ut_assert(timing.vback_porch.typ == 5);
1173 ut_assert(timing.vfront_porch.typ == 8);
1174 ut_assert(timing.vsync_len.typ == 2);
1175 ut_assert(timing.pixelclock.typ == 6500000);
1176 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH);
1177 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1178 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1179 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_LOW);
1180 ut_assert(timing.flags & DISPLAY_FLAGS_DE_HIGH);
1181 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1182 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE);
1183 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1184 ut_assert(timing.flags & DISPLAY_FLAGS_INTERLACED);
1185 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLESCAN);
1186 ut_assert(timing.flags & DISPLAY_FLAGS_DOUBLECLK);
1187
1188 ut_assertok(dev_decode_display_timing(dev, 1, &timing));
1189 ut_assert(timing.hactive.typ == 480);
1190 ut_assert(timing.hback_porch.typ == 59);
1191 ut_assert(timing.hfront_porch.typ == 10);
1192 ut_assert(timing.hsync_len.typ == 12);
1193 ut_assert(timing.vactive.typ == 800);
1194 ut_assert(timing.vback_porch.typ == 15);
1195 ut_assert(timing.vfront_porch.typ == 17);
1196 ut_assert(timing.vsync_len.typ == 16);
1197 ut_assert(timing.pixelclock.typ == 9000000);
1198 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1199 ut_assert(timing.flags & DISPLAY_FLAGS_HSYNC_LOW);
1200 ut_assert(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH);
1201 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1202 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1203 ut_assert(timing.flags & DISPLAY_FLAGS_DE_LOW);
1204 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1205 ut_assert(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE);
1206 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1207 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1208 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1209
1210 ut_assertok(dev_decode_display_timing(dev, 2, &timing));
1211 ut_assert(timing.hactive.typ == 800);
1212 ut_assert(timing.hback_porch.typ == 89);
1213 ut_assert(timing.hfront_porch.typ == 164);
1214 ut_assert(timing.hsync_len.typ == 11);
1215 ut_assert(timing.vactive.typ == 480);
1216 ut_assert(timing.vback_porch.typ == 23);
1217 ut_assert(timing.vfront_porch.typ == 10);
1218 ut_assert(timing.vsync_len.typ == 13);
1219 ut_assert(timing.pixelclock.typ == 33500000);
1220 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_HIGH));
1221 ut_assert(!(timing.flags & DISPLAY_FLAGS_HSYNC_LOW));
1222 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_HIGH));
1223 ut_assert(!(timing.flags & DISPLAY_FLAGS_VSYNC_LOW));
1224 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_HIGH));
1225 ut_assert(!(timing.flags & DISPLAY_FLAGS_DE_LOW));
1226 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_POSEDGE));
1227 ut_assert(!(timing.flags & DISPLAY_FLAGS_PIXDATA_NEGEDGE));
1228 ut_assert(!(timing.flags & DISPLAY_FLAGS_INTERLACED));
1229 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLESCAN));
1230 ut_assert(!(timing.flags & DISPLAY_FLAGS_DOUBLECLK));
1231
1232 ut_assert(dev_decode_display_timing(dev, 3, &timing));
1233 return 0;
1234}
1235DM_TEST(dm_test_decode_display_timing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);