blob: 633256821c2fb1fea822a258a843c1d65af21e61 [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>
12#include <asm/io.h>
13#include <dm/test.h>
14#include <dm/root.h>
Simon Glass98561572017-04-23 20:10:44 -060015#include <dm/device-internal.h>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <dm/devres.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070017#include <dm/uclass-internal.h>
18#include <dm/util.h>
Mario Six2ea4d0d2018-06-26 08:46:49 +020019#include <dm/lists.h>
20#include <dm/of_access.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060021#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050022#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070023
24DECLARE_GLOBAL_DATA_PTR;
25
Simon Glass98561572017-04-23 20:10:44 -060026struct dm_testprobe_pdata {
27 int probe_err;
28};
29
30static int testprobe_drv_probe(struct udevice *dev)
31{
Simon Glassc69cda22020-12-03 16:55:20 -070032 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -060033
34 return pdata->probe_err;
35}
36
37static const struct udevice_id testprobe_ids[] = {
38 { .compatible = "denx,u-boot-probe-test" },
39 { }
40};
41
42U_BOOT_DRIVER(testprobe_drv) = {
43 .name = "testprobe_drv",
44 .of_match = testprobe_ids,
45 .id = UCLASS_TEST_PROBE,
46 .probe = testprobe_drv_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -070047 .plat_auto = sizeof(struct dm_testprobe_pdata),
Simon Glass98561572017-04-23 20:10:44 -060048};
49
50UCLASS_DRIVER(testprobe) = {
51 .name = "testprobe",
52 .id = UCLASS_TEST_PROBE,
53 .flags = DM_UC_FLAG_SEQ_ALIAS,
54};
55
Simon Glassdc12ebb2019-12-29 21:19:25 -070056struct dm_testdevres_pdata {
57 void *ptr;
58};
59
60struct dm_testdevres_priv {
61 void *ptr;
Simon Glass42a0ce52019-12-29 21:19:28 -070062 void *ptr_ofdata;
Simon Glassdc12ebb2019-12-29 21:19:25 -070063};
64
65static int testdevres_drv_bind(struct udevice *dev)
66{
Simon Glassc69cda22020-12-03 16:55:20 -070067 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
Simon Glassdc12ebb2019-12-29 21:19:25 -070068
69 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
70
71 return 0;
72}
73
Simon Glassd1998a92020-12-03 16:55:21 -070074static int testdevres_drv_of_to_plat(struct udevice *dev)
Simon Glass42a0ce52019-12-29 21:19:28 -070075{
76 struct dm_testdevres_priv *priv = dev_get_priv(dev);
77
78 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
79
80 return 0;
81}
82
Simon Glassdc12ebb2019-12-29 21:19:25 -070083static int testdevres_drv_probe(struct udevice *dev)
84{
85 struct dm_testdevres_priv *priv = dev_get_priv(dev);
86
87 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
88
89 return 0;
90}
91
92static const struct udevice_id testdevres_ids[] = {
93 { .compatible = "denx,u-boot-devres-test" },
94 { }
95};
96
97U_BOOT_DRIVER(testdevres_drv) = {
98 .name = "testdevres_drv",
99 .of_match = testdevres_ids,
100 .id = UCLASS_TEST_DEVRES,
101 .bind = testdevres_drv_bind,
Simon Glassd1998a92020-12-03 16:55:21 -0700102 .of_to_plat = testdevres_drv_of_to_plat,
Simon Glassdc12ebb2019-12-29 21:19:25 -0700103 .probe = testdevres_drv_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700104 .plat_auto = sizeof(struct dm_testdevres_pdata),
Simon Glass41575d82020-12-03 16:55:17 -0700105 .priv_auto = sizeof(struct dm_testdevres_priv),
Simon Glassdc12ebb2019-12-29 21:19:25 -0700106};
107
108UCLASS_DRIVER(testdevres) = {
109 .name = "testdevres",
110 .id = UCLASS_TEST_DEVRES,
111 .flags = DM_UC_FLAG_SEQ_ALIAS,
112};
113
Joe Hershbergere721b882015-05-20 14:27:27 -0500114int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700115{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200116 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700117 int ret;
118 int i;
119
Simon Glass2e7d35d2014-02-26 15:59:21 -0700120 /*
121 * Now check that the ping adds are what we expect. This is using the
122 * ping-add property in each node.
123 */
Simon Glass1ca7e202014-07-23 06:55:18 -0600124 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-02-26 15:59:21 -0700125 uint32_t base;
126
127 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
128 ut_assert(!ret);
129
130 /*
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600131 * Get the 'ping-expect' property, which tells us what the
Simon Glasscaa4daa2020-12-03 16:55:18 -0700132 * ping add should be. We don't use the plat because we
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600133 * want to test the code that sets that up
134 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700135 */
Simon Glasse160f7d2017-01-17 16:52:55 -0700136 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600137 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700138 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glasse160f7d2017-01-17 16:52:55 -0700139 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glass2e7d35d2014-02-26 15:59:21 -0700140
Joe Hershbergere721b882015-05-20 14:27:27 -0500141 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700142 dev_get_priv(dev)));
143 }
144
145 return 0;
146}
Simon Glass1ca7e202014-07-23 06:55:18 -0600147
148/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500149static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600150{
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530151 const int num_devices = 9;
Simon Glass1ca7e202014-07-23 06:55:18 -0600152 struct udevice *dev;
153 struct uclass *uc;
154 int ret;
155 int i;
156
Simon Glass8ee05b52020-11-28 17:50:10 -0700157 ret = dm_extended_scan(false);
Simon Glass1ca7e202014-07-23 06:55:18 -0600158 ut_assert(!ret);
159
160 ret = uclass_get(UCLASS_TEST_FDT, &uc);
161 ut_assert(!ret);
162
163 /* These are num_devices compatible root-level device tree nodes */
164 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
165
Simon Glassf8a85442015-01-25 08:27:00 -0700166 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-07-23 06:55:18 -0600167 for (i = 0; i < num_devices; i++) {
168 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
169 ut_assert(!ret);
170 ut_assert(!dev_get_priv(dev));
Simon Glass0fd3d912020-12-22 19:30:28 -0700171 ut_assert(dev_get_plat(dev));
Simon Glass1ca7e202014-07-23 06:55:18 -0600172 }
173
Joe Hershbergere721b882015-05-20 14:27:27 -0500174 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600175
176 return 0;
177}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700178DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600179
Michal Simeka93eb572019-01-31 16:31:00 +0100180static int dm_test_alias_highest_id(struct unit_test_state *uts)
181{
182 int ret;
183
184 ret = dev_read_alias_highest_id("eth");
185 ut_asserteq(5, ret);
186
187 ret = dev_read_alias_highest_id("gpio");
Patrick Delaunayff526652020-01-13 11:35:14 +0100188 ut_asserteq(3, ret);
Michal Simeka93eb572019-01-31 16:31:00 +0100189
190 ret = dev_read_alias_highest_id("pci");
191 ut_asserteq(2, ret);
192
193 ret = dev_read_alias_highest_id("i2c");
194 ut_asserteq(0, ret);
195
196 ret = dev_read_alias_highest_id("deadbeef");
197 ut_asserteq(-1, ret);
198
199 return 0;
200}
201DM_TEST(dm_test_alias_highest_id, 0);
202
Joe Hershbergere721b882015-05-20 14:27:27 -0500203static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-07-23 06:55:03 -0600204{
205 struct uclass *uc;
206 int ret;
207
Simon Glass725e4fc2020-11-28 17:50:09 -0700208 ret = dm_scan_fdt(true);
Simon Glass00606d72014-07-23 06:55:03 -0600209 ut_assert(!ret);
210
211 ret = uclass_get(UCLASS_TEST_FDT, &uc);
212 ut_assert(!ret);
213
Bin Meng2786cd72018-10-10 22:07:01 -0700214 /*
215 * These are 2 pre-reloc devices:
216 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
217 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
218 */
219 ut_asserteq(2, list_count_items(&uc->dev_head));
Simon Glass00606d72014-07-23 06:55:03 -0600220
221 return 0;
222}
223DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600224
225/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500226static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600227{
228 struct udevice *dev;
229
230 /* A few basic santiy tests */
Simon Glass99175912020-12-16 21:20:29 -0700231 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600232 ut_asserteq_str("b-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700233 ut_asserteq(3, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600234
Simon Glass99175912020-12-16 21:20:29 -0700235 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600236 ut_asserteq_str("a-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700237 ut_asserteq(8, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600238
Simon Glass981426e2020-12-16 21:20:26 -0700239 /*
240 * This device has no alias so gets the next value after all available
241 * aliases. The last alias is testfdt12
242 */
Simon Glass99175912020-12-16 21:20:29 -0700243 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev));
Simon Glass981426e2020-12-16 21:20:26 -0700244 ut_asserteq_str("d-test", dev->name);
245 ut_asserteq(13, dev_seq(dev));
246
Simon Glass99175912020-12-16 21:20:29 -0700247 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass981426e2020-12-16 21:20:26 -0700248 &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600249 ut_asserteq_ptr(NULL, dev);
250
251 /* Test aliases */
252 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
253 ut_asserteq_str("e-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700254 ut_asserteq(6, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600255
Simon Glass1ca7e202014-07-23 06:55:18 -0600256 /*
257 * Note that c-test nodes are not probed since it is not a top-level
258 * node
259 */
Simon Glass5a66a8f2014-07-23 06:55:12 -0600260 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
261 ut_asserteq_str("b-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700262 ut_asserteq(3, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600263
264 /*
265 * d-test wants sequence number 3 also, but it can't have it because
266 * b-test gets it first.
267 */
268 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
269 ut_asserteq_str("d-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700270 ut_asserteq(13, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600271
Simon Glass981426e2020-12-16 21:20:26 -0700272 /* g-test gets the next value after f-test */
273 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 15, &dev));
274 ut_asserteq_str("g-test", dev->name);
275 ut_asserteq(15, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600276
Simon Glass981426e2020-12-16 21:20:26 -0700277 /* And we should still have holes in our sequence numbers */
Simon Glass99175912020-12-16 21:20:29 -0700278 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0,
Simon Glass981426e2020-12-16 21:20:26 -0700279 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700280 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1,
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, 2,
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, 4,
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, 7,
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, 9,
Simon Glass981426e2020-12-16 21:20:26 -0700289 &dev));
290 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
Simon Glass99175912020-12-16 21:20:29 -0700291 &dev));
Simon Glass981426e2020-12-16 21:20:26 -0700292 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
Simon Glass99175912020-12-16 21:20:29 -0700293 &dev));
Michael Wallebe1a6e92020-06-02 01:47:09 +0200294
Simon Glass5a66a8f2014-07-23 06:55:12 -0600295 return 0;
296}
Simon Glasse180c2b2020-07-28 19:41:12 -0600297DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600298
Simon Glass93f44e82020-12-16 21:20:27 -0700299/* More tests for sequence numbers */
300static int dm_test_fdt_uclass_seq_manual(struct unit_test_state *uts)
301{
302 struct udevice *dev;
303
304 /*
305 * Since DM_UC_FLAG_NO_AUTO_SEQ is set for this uclass, only testfdtm1
306 * should get a sequence number assigned
307 */
308 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 0, &dev));
309 ut_asserteq_str("testfdtm0", dev->name);
310 ut_asserteq(-1, dev_seq(dev));
311
312 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT_MANUAL, 1, &dev));
313 ut_asserteq_str("testfdtm1", dev->name);
314 ut_asserteq(1, dev_seq(dev));
315
316 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 2, &dev));
317 ut_asserteq_str("testfdtm2", dev->name);
318 ut_asserteq(-1, dev_seq(dev));
319
320 return 0;
321}
322DM_TEST(dm_test_fdt_uclass_seq_manual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
323
Simon Glass7f20d1d2020-12-16 21:20:32 -0700324static int dm_test_fdt_uclass_seq_more(struct unit_test_state *uts)
325{
326 struct udevice *dev;
327 ofnode node;
328
329 /* Check creating a device with an alias */
330 node = ofnode_path("/some-bus/c-test@1");
331 ut_assertok(device_bind(dm_root(), DM_GET_DRIVER(testfdt_drv),
332 "c-test@1", NULL, node, &dev));
333 ut_asserteq(12, dev_seq(dev));
334 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 12, &dev));
335 ut_asserteq_str("c-test@1", dev->name);
336
337 /*
338 * Now bind a device without an alias. It should not get the next
339 * sequence number after all aliases, and existing bound devices. The
340 * last alias is 12, so we have:
341 *
342 * 13 d-test
343 * 14 f-test
344 * 15 g-test
345 * 16 h-test
346 * 17 another-test
347 * 18 chosen-test
348 *
349 * So next available is 19
350 */
351 ut_assertok(device_bind(dm_root(), DM_GET_DRIVER(testfdt_drv),
352 "fred", NULL, ofnode_null(), &dev));
353 ut_asserteq(19, dev_seq(dev));
354
355 ut_assertok(device_bind(dm_root(), DM_GET_DRIVER(testfdt_drv),
356 "fred2", NULL, ofnode_null(), &dev));
357 ut_asserteq(20, dev_seq(dev));
358
359 return 0;
360}
361DM_TEST(dm_test_fdt_uclass_seq_more, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
362
Simon Glassf4cdead2014-07-23 06:55:14 -0600363/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500364static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-07-23 06:55:14 -0600365{
366 const void *blob = gd->fdt_blob;
367 struct udevice *dev;
368 int node;
369
370 node = fdt_path_offset(blob, "/e-test");
371 ut_assert(node > 0);
372 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
373 &dev));
374 ut_asserteq_str("e-test", dev->name);
375
376 /* This node should not be bound */
377 node = fdt_path_offset(blob, "/junk");
378 ut_assert(node > 0);
379 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
380 node, &dev));
381
382 /* This is not a top level node so should not be probed */
Simon Glass1ca7e202014-07-23 06:55:18 -0600383 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-07-23 06:55:14 -0600384 ut_assert(node > 0);
385 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
386 node, &dev));
387
388 return 0;
389}
Simon Glass86b54ec2017-05-18 20:09:44 -0600390DM_TEST(dm_test_fdt_offset,
Simon Glasse180c2b2020-07-28 19:41:12 -0600391 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass98561572017-04-23 20:10:44 -0600392
393/**
394 * Test various error conditions with uclass_first_device() and
395 * uclass_next_device()
396 */
397static int dm_test_first_next_device(struct unit_test_state *uts)
398{
399 struct dm_testprobe_pdata *pdata;
400 struct udevice *dev, *parent = NULL;
401 int count;
402 int ret;
403
404 /* There should be 4 devices */
405 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
406 dev;
407 ret = uclass_next_device(&dev)) {
408 count++;
409 parent = dev_get_parent(dev);
410 }
411 ut_assertok(ret);
412 ut_asserteq(4, count);
413
414 /* Remove them and try again, with an error on the second one */
415 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
Simon Glassc69cda22020-12-03 16:55:20 -0700416 pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600417 pdata->probe_err = -ENOMEM;
418 device_remove(parent, DM_REMOVE_NORMAL);
419 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
420 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
421 ut_asserteq_ptr(dev, NULL);
422
423 /* Now an error on the first one */
424 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
Simon Glassc69cda22020-12-03 16:55:20 -0700425 pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600426 pdata->probe_err = -ENOENT;
427 device_remove(parent, DM_REMOVE_NORMAL);
428 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
429
430 return 0;
431}
Simon Glasse180c2b2020-07-28 19:41:12 -0600432DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass95ce3852017-04-23 20:10:45 -0600433
Simon Glass3cf0fba2020-02-06 09:54:49 -0700434/* Test iteration through devices in a uclass */
435static int dm_test_uclass_foreach(struct unit_test_state *uts)
436{
437 struct udevice *dev;
438 struct uclass *uc;
439 int count;
440
441 count = 0;
442 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
443 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530444 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700445
446 count = 0;
447 uclass_foreach_dev(dev, uc)
448 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530449 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700450
451 return 0;
452}
Simon Glasse180c2b2020-07-28 19:41:12 -0600453DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700454
Simon Glass95ce3852017-04-23 20:10:45 -0600455/**
456 * check_devices() - Check return values and pointers
457 *
458 * This runs through a full sequence of uclass_first_device_check()...
459 * uclass_next_device_check() checking that the return values and devices
460 * are correct.
461 *
462 * @uts: Test state
463 * @devlist: List of expected devices
464 * @mask: Indicates which devices should return an error. Device n should
465 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
466 * bit n is clear.
467 */
468static int check_devices(struct unit_test_state *uts,
469 struct udevice *devlist[], int mask)
470{
471 int expected_ret;
472 struct udevice *dev;
473 int i;
474
475 expected_ret = (mask & 1) ? -ENOENT : 0;
476 mask >>= 1;
477 ut_asserteq(expected_ret,
478 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
479 for (i = 0; i < 4; i++) {
480 ut_asserteq_ptr(devlist[i], dev);
481 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
482 mask >>= 1;
483 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
484 }
485 ut_asserteq_ptr(NULL, dev);
486
487 return 0;
488}
489
490/* Test uclass_first_device_check() and uclass_next_device_check() */
491static int dm_test_first_next_ok_device(struct unit_test_state *uts)
492{
493 struct dm_testprobe_pdata *pdata;
494 struct udevice *dev, *parent = NULL, *devlist[4];
495 int count;
496 int ret;
497
498 /* There should be 4 devices */
499 count = 0;
500 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
501 dev;
502 ret = uclass_next_device_check(&dev)) {
503 ut_assertok(ret);
504 devlist[count++] = dev;
505 parent = dev_get_parent(dev);
506 }
507 ut_asserteq(4, count);
508 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
509 ut_assertok(check_devices(uts, devlist, 0));
510
511 /* Remove them and try again, with an error on the second one */
Simon Glassc69cda22020-12-03 16:55:20 -0700512 pdata = dev_get_plat(devlist[1]);
Simon Glass95ce3852017-04-23 20:10:45 -0600513 pdata->probe_err = -ENOENT - 1;
514 device_remove(parent, DM_REMOVE_NORMAL);
515 ut_assertok(check_devices(uts, devlist, 1 << 1));
516
517 /* Now an error on the first one */
Simon Glassc69cda22020-12-03 16:55:20 -0700518 pdata = dev_get_plat(devlist[0]);
Simon Glass95ce3852017-04-23 20:10:45 -0600519 pdata->probe_err = -ENOENT - 0;
520 device_remove(parent, DM_REMOVE_NORMAL);
521 ut_assertok(check_devices(uts, devlist, 3 << 0));
522
523 /* Now errors on all */
Simon Glassc69cda22020-12-03 16:55:20 -0700524 pdata = dev_get_plat(devlist[2]);
Simon Glass95ce3852017-04-23 20:10:45 -0600525 pdata->probe_err = -ENOENT - 2;
Simon Glassc69cda22020-12-03 16:55:20 -0700526 pdata = dev_get_plat(devlist[3]);
Simon Glass95ce3852017-04-23 20:10:45 -0600527 pdata->probe_err = -ENOENT - 3;
528 device_remove(parent, DM_REMOVE_NORMAL);
529 ut_assertok(check_devices(uts, devlist, 0xf << 0));
530
531 return 0;
532}
Simon Glasse180c2b2020-07-28 19:41:12 -0600533DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Sixe8d52912018-03-12 14:53:33 +0100534
535static const struct udevice_id fdt_dummy_ids[] = {
536 { .compatible = "denx,u-boot-fdt-dummy", },
537 { }
538};
539
540UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca507cef32018-05-19 14:13:55 +0200541 .name = "fdt-dummy",
Mario Sixe8d52912018-03-12 14:53:33 +0100542 .id = UCLASS_TEST_DUMMY,
543 .flags = DM_UC_FLAG_SEQ_ALIAS,
544};
545
546U_BOOT_DRIVER(fdt_dummy_drv) = {
547 .name = "fdt_dummy_drv",
548 .of_match = fdt_dummy_ids,
549 .id = UCLASS_TEST_DUMMY,
550};
551
552static int dm_test_fdt_translation(struct unit_test_state *uts)
553{
554 struct udevice *dev;
Fabien Dessenne641067f2019-05-31 15:11:30 +0200555 fdt32_t dma_addr[2];
Mario Sixe8d52912018-03-12 14:53:33 +0100556
557 /* Some simple translations */
Simon Glass99175912020-12-16 21:20:29 -0700558 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100559 ut_asserteq_str("dev@0,0", dev->name);
560 ut_asserteq(0x8000, dev_read_addr(dev));
561
Simon Glass99175912020-12-16 21:20:29 -0700562 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100563 ut_asserteq_str("dev@1,100", dev->name);
564 ut_asserteq(0x9000, dev_read_addr(dev));
565
Simon Glass99175912020-12-16 21:20:29 -0700566 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100567 ut_asserteq_str("dev@2,200", dev->name);
568 ut_asserteq(0xA000, dev_read_addr(dev));
569
570 /* No translation for busses with #size-cells == 0 */
Simon Glass99175912020-12-16 21:20:29 -0700571 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100572 ut_asserteq_str("dev@42", dev->name);
573 ut_asserteq(0x42, dev_read_addr(dev));
574
Fabien Dessenne641067f2019-05-31 15:11:30 +0200575 /* dma address translation */
Simon Glass99175912020-12-16 21:20:29 -0700576 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Fabien Dessenne641067f2019-05-31 15:11:30 +0200577 dma_addr[0] = cpu_to_be32(0);
578 dma_addr[1] = cpu_to_be32(0);
579 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
580
Simon Glass99175912020-12-16 21:20:29 -0700581 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Fabien Dessenne641067f2019-05-31 15:11:30 +0200582 dma_addr[0] = cpu_to_be32(1);
583 dma_addr[1] = cpu_to_be32(0x100);
584 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
585
Mario Sixe8d52912018-03-12 14:53:33 +0100586 return 0;
587}
Simon Glasse180c2b2020-07-28 19:41:12 -0600588DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200589
Ovidiu Panait6e648302020-08-03 22:17:36 +0300590static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
591{
592 struct udevice *gpio, *dev;
593 void *ptr;
594
595 /* Test for missing reg property */
596 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
597 ut_assertnull(devfdt_get_addr_ptr(gpio));
598
Simon Glass99175912020-12-16 21:20:29 -0700599 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Ovidiu Panait6e648302020-08-03 22:17:36 +0300600 ptr = devfdt_get_addr_ptr(dev);
601 ut_asserteq_ptr((void *)0x8000, ptr);
602
603 return 0;
604}
605DM_TEST(dm_test_fdt_get_addr_ptr_flat,
606 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
607
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200608static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
609{
610 struct udevice *dev;
611 fdt_addr_t addr;
612 void *paddr;
613
Simon Glass99175912020-12-16 21:20:29 -0700614 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200615
616 addr = devfdt_get_addr(dev);
617 ut_asserteq(0x8000, addr);
618
619 paddr = map_physmem(addr, 0, MAP_NOCACHE);
620 ut_assertnonnull(paddr);
621 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
622
623 return 0;
624}
625DM_TEST(dm_test_fdt_remap_addr_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600626 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200627
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100628static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
629{
630 struct udevice *dev;
631 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530632 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100633 void *paddr;
634
Simon Glass99175912020-12-16 21:20:29 -0700635 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100636
Sekhar Norif5b90472019-08-01 19:12:56 +0530637 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100638 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530639 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100640
641 paddr = map_physmem(addr, 0, MAP_NOCACHE);
642 ut_assertnonnull(paddr);
643 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
644
645 return 0;
646}
647DM_TEST(dm_test_fdt_remap_addr_index_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600648 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100649
650static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
651{
652 struct udevice *dev;
653 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530654 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100655 void *paddr;
656
Simon Glass99175912020-12-16 21:20:29 -0700657 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100658
Sekhar Norif5b90472019-08-01 19:12:56 +0530659 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100660 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530661 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100662
663 paddr = map_physmem(addr, 0, MAP_NOCACHE);
664 ut_assertnonnull(paddr);
665 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
666
667 return 0;
668}
669DM_TEST(dm_test_fdt_remap_addr_name_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600670 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100671
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200672static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
673{
674 struct udevice *dev;
675 fdt_addr_t addr;
676 void *paddr;
677
Simon Glass99175912020-12-16 21:20:29 -0700678 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200679
680 addr = dev_read_addr(dev);
681 ut_asserteq(0x8000, addr);
682
683 paddr = map_physmem(addr, 0, MAP_NOCACHE);
684 ut_assertnonnull(paddr);
685 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
686
687 return 0;
688}
689DM_TEST(dm_test_fdt_remap_addr_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600690 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six2ea4d0d2018-06-26 08:46:49 +0200691
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100692static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
693{
694 struct udevice *dev;
695 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530696 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100697 void *paddr;
698
Simon Glass99175912020-12-16 21:20:29 -0700699 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100700
Sekhar Norif5b90472019-08-01 19:12:56 +0530701 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100702 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530703 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100704
705 paddr = map_physmem(addr, 0, MAP_NOCACHE);
706 ut_assertnonnull(paddr);
707 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
708
709 return 0;
710}
711DM_TEST(dm_test_fdt_remap_addr_index_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600712 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100713
714static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
715{
716 struct udevice *dev;
717 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530718 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100719 void *paddr;
720
Simon Glass99175912020-12-16 21:20:29 -0700721 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100722
Sekhar Norif5b90472019-08-01 19:12:56 +0530723 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100724 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530725 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100726
727 paddr = map_physmem(addr, 0, MAP_NOCACHE);
728 ut_assertnonnull(paddr);
729 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
730
731 return 0;
732}
733DM_TEST(dm_test_fdt_remap_addr_name_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600734 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100735
Mario Six2ea4d0d2018-06-26 08:46:49 +0200736static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
737{
738 struct udevice *dev;
739 ofnode node;
740
741 if (!of_live_active()) {
742 printf("Live tree not active; ignore test\n");
743 return 0;
744 }
745
746 /* Test enabling devices */
747
748 node = ofnode_path("/usb@2");
749
750 ut_assert(!of_device_is_available(ofnode_to_np(node)));
751 ofnode_set_enabled(node, true);
752 ut_assert(of_device_is_available(ofnode_to_np(node)));
753
754 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
755 &dev);
Simon Glass99175912020-12-16 21:20:29 -0700756 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
Mario Six2ea4d0d2018-06-26 08:46:49 +0200757
758 /* Test string property setting */
759
760 ut_assert(device_is_compatible(dev, "sandbox,usb"));
761 ofnode_write_string(node, "compatible", "gdsys,super-usb");
762 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
763 ofnode_write_string(node, "compatible", "sandbox,usb");
764 ut_assert(device_is_compatible(dev, "sandbox,usb"));
765
766 /* Test setting generic properties */
767
768 /* Non-existent in DTB */
769 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
770 /* reg = 0x42, size = 0x100 */
771 ut_assertok(ofnode_write_prop(node, "reg", 8,
772 "\x00\x00\x00\x42\x00\x00\x01\x00"));
773 ut_asserteq(0x42, dev_read_addr(dev));
774
775 /* Test disabling devices */
776
777 device_remove(dev, DM_REMOVE_NORMAL);
778 device_unbind(dev);
779
780 ut_assert(of_device_is_available(ofnode_to_np(node)));
781 ofnode_set_enabled(node, false);
782 ut_assert(!of_device_is_available(ofnode_to_np(node)));
783
784 return 0;
785}
Simon Glasse180c2b2020-07-28 19:41:12 -0600786DM_TEST(dm_test_fdt_livetree_writing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six172942a2018-06-26 08:46:51 +0200787
788static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
789{
790 ofnode node;
791
792 if (!of_live_active()) {
793 printf("Live tree not active; ignore test\n");
794 return 0;
795 }
796
797 node = ofnode_path("/usb@2");
798
799 /* Test enabling devices */
800
801 ut_assert(!of_device_is_available(ofnode_to_np(node)));
802 dev_enable_by_path("/usb@2");
803 ut_assert(of_device_is_available(ofnode_to_np(node)));
804
805 /* Test disabling devices */
806
807 ut_assert(of_device_is_available(ofnode_to_np(node)));
808 dev_disable_by_path("/usb@2");
809 ut_assert(!of_device_is_available(ofnode_to_np(node)));
810
811 return 0;
812}
Simon Glasse180c2b2020-07-28 19:41:12 -0600813DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
814 UT_TESTF_SCAN_FDT);
Simon Glassd0b4f682018-11-18 08:14:30 -0700815
816/* Test a few uclass phandle functions */
817static int dm_test_fdt_phandle(struct unit_test_state *uts)
818{
819 struct udevice *back, *dev, *dev2;
820
821 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200822 ut_assertnonnull(back);
Simon Glassd0b4f682018-11-18 08:14:30 -0700823 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
824 back, "missing", &dev));
825 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
826 "power-supply", &dev));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200827 ut_assertnonnull(dev);
Simon Glassd0b4f682018-11-18 08:14:30 -0700828 ut_asserteq(0, device_active(dev));
829 ut_asserteq_str("ldo1", dev->name);
830 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
831 "power-supply", &dev2));
832 ut_asserteq_ptr(dev, dev2);
833
834 return 0;
835}
Simon Glasse180c2b2020-07-28 19:41:12 -0600836DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3abe1112018-11-18 08:14:31 -0700837
838/* Test device_find_first_child_by_uclass() */
839static int dm_test_first_child(struct unit_test_state *uts)
840{
841 struct udevice *i2c, *dev, *dev2;
842
843 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
844 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
845 ut_asserteq_str("rtc@43", dev->name);
846 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
847 ut_asserteq_ptr(dev, dev2);
848 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
849 ut_asserteq_str("rtc@61", dev2->name);
850
851 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
852 &dev));
853 ut_asserteq_str("eeprom@2c", dev->name);
854 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
855 ut_asserteq_ptr(dev, dev2);
856
857 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
858 UCLASS_VIDEO, &dev));
859 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
860
861 return 0;
862}
Simon Glasse180c2b2020-07-28 19:41:12 -0600863DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa1b17e42018-12-10 10:37:37 -0700864
865/* Test integer functions in dm_read_...() */
866static int dm_test_read_int(struct unit_test_state *uts)
867{
868 struct udevice *dev;
869 u32 val32;
870 s32 sval;
871 uint val;
Dario Binacchi70573c62020-03-29 18:04:40 +0200872 u64 val64;
Simon Glassa1b17e42018-12-10 10:37:37 -0700873
874 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
875 ut_asserteq_str("a-test", dev->name);
876 ut_assertok(dev_read_u32(dev, "int-value", &val32));
877 ut_asserteq(1234, val32);
878
879 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
880 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
881
882 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
883 ut_asserteq(1234, val32);
884
885 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
886 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
887
888 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
889 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
890 ut_asserteq(-1234, sval);
891
892 val = 0;
893 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
894 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
895 ut_asserteq(-1234, val);
896
Dario Binacchi70573c62020-03-29 18:04:40 +0200897 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
898 ut_asserteq_64(0x1111222233334444, val64);
899
900 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
901 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
902
903 ut_asserteq_64(0x1111222233334444,
904 dev_read_u64_default(dev, "int64-value", 6));
905
Simon Glassa1b17e42018-12-10 10:37:37 -0700906 return 0;
907}
Simon Glasse180c2b2020-07-28 19:41:12 -0600908DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass50162342020-02-06 09:54:50 -0700909
Dario Binacchi4bb70752020-03-29 18:04:41 +0200910static int dm_test_read_int_index(struct unit_test_state *uts)
911{
912 struct udevice *dev;
913 u32 val32;
914
915 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
916 ut_asserteq_str("a-test", dev->name);
917
918 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
919 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
920
921 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
922 ut_asserteq(5678, val32);
923 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
924 ut_asserteq(9123, val32);
925 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
926 ut_asserteq(4567, val32);
927 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
928 &val32));
929
930 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
931 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
932 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
933 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
934
935 return 0;
936}
Simon Glasse180c2b2020-07-28 19:41:12 -0600937DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi4bb70752020-03-29 18:04:41 +0200938
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +0200939static int dm_test_read_phandle(struct unit_test_state *uts)
940{
941 struct udevice *dev;
942 struct ofnode_phandle_args args;
943 int ret;
944 const char prop[] = "test-gpios";
945 const char cell[] = "#gpio-cells";
946 const char prop2[] = "phandle-value";
947
948 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
949 ut_asserteq_str("a-test", dev->name);
950
951 /* Test dev_count_phandle_with_args with cell name */
952 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
953 ut_asserteq(-ENOENT, ret);
954 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
955 ut_asserteq(-EINVAL, ret);
956 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
957
958 /* Test dev_read_phandle_with_args with cell name */
959 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
960 ut_asserteq(-ENOENT, ret);
961 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
962 ut_asserteq(-EINVAL, ret);
963 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
964 ut_asserteq(1, args.args_count);
965 ut_asserteq(1, args.args[0]);
966 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
967 ut_asserteq(1, args.args_count);
968 ut_asserteq(4, args.args[0]);
969 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
970 ut_asserteq(5, args.args_count);
971 ut_asserteq(5, args.args[0]);
972 ut_asserteq(1, args.args[4]);
973 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
974 ut_asserteq(-ENOENT, ret);
975 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
976 ut_asserteq(1, args.args_count);
977 ut_asserteq(12, args.args[0]);
978 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
979 ut_asserteq(-ENOENT, ret);
980
981 /* Test dev_count_phandle_with_args with cell count */
982 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
983 ut_asserteq(-ENOENT, ret);
984 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
985
986 /* Test dev_read_phandle_with_args with cell count */
987 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
988 ut_asserteq(1, ofnode_valid(args.node));
989 ut_asserteq(1, args.args_count);
990 ut_asserteq(10, args.args[0]);
991 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
992 ut_asserteq(-EINVAL, ret);
993 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
994 ut_asserteq(1, ofnode_valid(args.node));
995 ut_asserteq(1, args.args_count);
996 ut_asserteq(30, args.args[0]);
997 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
998 ut_asserteq(-ENOENT, ret);
999
1000 return 0;
1001}
1002DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1003
Simon Glass50162342020-02-06 09:54:50 -07001004/* Test iteration through devices by drvdata */
1005static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1006{
1007 struct udevice *dev;
1008
1009 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1010 DM_TEST_TYPE_FIRST, &dev));
1011 ut_asserteq_str("a-test", dev->name);
1012
1013 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1014 DM_TEST_TYPE_SECOND, &dev));
1015 ut_asserteq_str("d-test", dev->name);
1016
1017 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1018 DM_TEST_TYPE_COUNT,
1019 &dev));
1020
1021 return 0;
1022}
Simon Glasse180c2b2020-07-28 19:41:12 -06001023DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Tom Rini9a8942b2020-02-11 10:58:41 -05001024
Simon Glassf262d4c2020-01-27 08:49:47 -07001025/* Test device_first_child_ofdata_err(), etc. */
1026static int dm_test_child_ofdata(struct unit_test_state *uts)
1027{
1028 struct udevice *bus, *dev;
1029 int count;
1030
1031 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1032 count = 0;
Simon Glassd1998a92020-12-03 16:55:21 -07001033 device_foreach_child_of_to_plat(dev, bus) {
Simon Glassf262d4c2020-01-27 08:49:47 -07001034 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1035 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1036 count++;
1037 }
1038 ut_asserteq(3, count);
1039
1040 return 0;
1041}
Simon Glasse180c2b2020-07-28 19:41:12 -06001042DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass903e83e2020-01-27 08:49:48 -07001043
1044/* Test device_first_child_err(), etc. */
1045static int dm_test_first_child_probe(struct unit_test_state *uts)
1046{
1047 struct udevice *bus, *dev;
1048 int count;
1049
1050 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1051 count = 0;
1052 device_foreach_child_probe(dev, bus) {
1053 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1054 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
1055 count++;
1056 }
1057 ut_asserteq(3, count);
1058
1059 return 0;
1060}
Simon Glasse180c2b2020-07-28 19:41:12 -06001061DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb0dcc872020-04-05 15:38:19 -06001062
1063/* Test that ofdata is read for parents before children */
1064static int dm_test_ofdata_order(struct unit_test_state *uts)
1065{
1066 struct udevice *bus, *dev;
1067
1068 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1069 ut_assertnonnull(bus);
1070 ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID));
1071
1072 ut_assertok(device_find_first_child(bus, &dev));
1073 ut_assertnonnull(dev);
1074 ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID));
1075
1076 /* read the child's ofdata which should cause the parent's to be read */
Simon Glassd1998a92020-12-03 16:55:21 -07001077 ut_assertok(device_of_to_plat(dev));
Simon Glassb0dcc872020-04-05 15:38:19 -06001078 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1079 ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID);
1080
1081 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1082 ut_assert(!(bus->flags & DM_FLAG_ACTIVATED));
1083
1084 return 0;
1085}
Simon Glasse180c2b2020-07-28 19:41:12 -06001086DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);