blob: 5f443bdb6c0220c34dec2ffd07f42d12b990215e [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
Heiko Schocher54c5d082014-05-22 12:43:05 +020026static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070027{
Simon Glasscaa4daa2020-12-03 16:55:18 -070028 const struct dm_test_pdata *pdata = dev->plat;
Simon Glass2e7d35d2014-02-26 15:59:21 -070029 struct dm_test_priv *priv = dev_get_priv(dev);
30
31 *pingret = pingval + pdata->ping_add;
32 priv->ping_total += *pingret;
33
34 return 0;
35}
36
37static const struct test_ops test_ops = {
38 .ping = testfdt_drv_ping,
39};
40
Simon Glassd1998a92020-12-03 16:55:21 -070041static int testfdt_of_to_plat(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070042{
Simon Glassc69cda22020-12-03 16:55:20 -070043 struct dm_test_pdata *pdata = dev_get_plat(dev);
Simon Glass2e7d35d2014-02-26 15:59:21 -070044
Simon Glasse160f7d2017-01-17 16:52:55 -070045 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glass2e7d35d2014-02-26 15:59:21 -070046 "ping-add", -1);
Simon Glasse160f7d2017-01-17 16:52:55 -070047 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -060048 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -070049
50 return 0;
51}
52
Heiko Schocher54c5d082014-05-22 12:43:05 +020053static int testfdt_drv_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070054{
55 struct dm_test_priv *priv = dev_get_priv(dev);
56
57 priv->ping_total += DM_TEST_START_TOTAL;
58
Simon Glass83c7e432015-01-25 08:27:10 -070059 /*
60 * If this device is on a bus, the uclass_flag will be set before
Bin Mengd92878a2018-10-15 02:20:58 -070061 * calling this function. In the meantime the uclass_postp is
62 * initlized to a value -1. These are used respectively by
63 * dm_test_bus_child_pre_probe_uclass() and
64 * dm_test_bus_child_post_probe_uclass().
Simon Glass83c7e432015-01-25 08:27:10 -070065 */
66 priv->uclass_total += priv->uclass_flag;
Bin Mengd92878a2018-10-15 02:20:58 -070067 priv->uclass_postp = -1;
Simon Glass83c7e432015-01-25 08:27:10 -070068
Simon Glass2e7d35d2014-02-26 15:59:21 -070069 return 0;
70}
71
Simon Glassae7f4512014-06-11 23:29:45 -060072static const struct udevice_id testfdt_ids[] = {
Simon Glass2e7d35d2014-02-26 15:59:21 -070073 {
74 .compatible = "denx,u-boot-fdt-test",
75 .data = DM_TEST_TYPE_FIRST },
76 {
77 .compatible = "google,another-fdt-test",
78 .data = DM_TEST_TYPE_SECOND },
79 { }
80};
81
82U_BOOT_DRIVER(testfdt_drv) = {
83 .name = "testfdt_drv",
84 .of_match = testfdt_ids,
85 .id = UCLASS_TEST_FDT,
Simon Glassd1998a92020-12-03 16:55:21 -070086 .of_to_plat = testfdt_of_to_plat,
Simon Glass2e7d35d2014-02-26 15:59:21 -070087 .probe = testfdt_drv_probe,
88 .ops = &test_ops,
Simon Glass41575d82020-12-03 16:55:17 -070089 .priv_auto = sizeof(struct dm_test_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -070090 .plat_auto = sizeof(struct dm_test_pdata),
Simon Glass2e7d35d2014-02-26 15:59:21 -070091};
92
Bin Meng2786cd72018-10-10 22:07:01 -070093static const struct udevice_id testfdt1_ids[] = {
94 {
95 .compatible = "denx,u-boot-fdt-test1",
96 .data = DM_TEST_TYPE_FIRST },
97 { }
98};
99
100U_BOOT_DRIVER(testfdt1_drv) = {
101 .name = "testfdt1_drv",
102 .of_match = testfdt1_ids,
103 .id = UCLASS_TEST_FDT,
Simon Glassd1998a92020-12-03 16:55:21 -0700104 .of_to_plat = testfdt_of_to_plat,
Bin Meng2786cd72018-10-10 22:07:01 -0700105 .probe = testfdt_drv_probe,
106 .ops = &test_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700107 .priv_auto = sizeof(struct dm_test_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700108 .plat_auto = sizeof(struct dm_test_pdata),
Bin Meng2786cd72018-10-10 22:07:01 -0700109 .flags = DM_FLAG_PRE_RELOC,
110};
111
Simon Glass2e7d35d2014-02-26 15:59:21 -0700112/* From here is the testfdt uclass code */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200113int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700114{
115 const struct test_ops *ops = device_get_ops(dev);
116
117 if (!ops->ping)
118 return -ENOSYS;
119
120 return ops->ping(dev, pingval, pingret);
121}
122
123UCLASS_DRIVER(testfdt) = {
124 .name = "testfdt",
125 .id = UCLASS_TEST_FDT,
Simon Glass9cc36a22015-01-25 08:27:05 -0700126 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700127};
128
Simon Glass93f44e82020-12-16 21:20:27 -0700129static const struct udevice_id testfdtm_ids[] = {
130 { .compatible = "denx,u-boot-fdtm-test" },
131 { }
132};
133
134U_BOOT_DRIVER(testfdtm_drv) = {
135 .name = "testfdtm_drv",
136 .of_match = testfdtm_ids,
137 .id = UCLASS_TEST_FDT_MANUAL,
138};
139
140UCLASS_DRIVER(testfdtm) = {
141 .name = "testfdtm",
142 .id = UCLASS_TEST_FDT_MANUAL,
143 .flags = DM_UC_FLAG_SEQ_ALIAS | DM_UC_FLAG_NO_AUTO_SEQ,
144};
145
Simon Glass98561572017-04-23 20:10:44 -0600146struct dm_testprobe_pdata {
147 int probe_err;
148};
149
150static int testprobe_drv_probe(struct udevice *dev)
151{
Simon Glassc69cda22020-12-03 16:55:20 -0700152 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600153
154 return pdata->probe_err;
155}
156
157static const struct udevice_id testprobe_ids[] = {
158 { .compatible = "denx,u-boot-probe-test" },
159 { }
160};
161
162U_BOOT_DRIVER(testprobe_drv) = {
163 .name = "testprobe_drv",
164 .of_match = testprobe_ids,
165 .id = UCLASS_TEST_PROBE,
166 .probe = testprobe_drv_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700167 .plat_auto = sizeof(struct dm_testprobe_pdata),
Simon Glass98561572017-04-23 20:10:44 -0600168};
169
170UCLASS_DRIVER(testprobe) = {
171 .name = "testprobe",
172 .id = UCLASS_TEST_PROBE,
173 .flags = DM_UC_FLAG_SEQ_ALIAS,
174};
175
Simon Glassdc12ebb2019-12-29 21:19:25 -0700176struct dm_testdevres_pdata {
177 void *ptr;
178};
179
180struct dm_testdevres_priv {
181 void *ptr;
Simon Glass42a0ce52019-12-29 21:19:28 -0700182 void *ptr_ofdata;
Simon Glassdc12ebb2019-12-29 21:19:25 -0700183};
184
185static int testdevres_drv_bind(struct udevice *dev)
186{
Simon Glassc69cda22020-12-03 16:55:20 -0700187 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
Simon Glassdc12ebb2019-12-29 21:19:25 -0700188
189 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
190
191 return 0;
192}
193
Simon Glassd1998a92020-12-03 16:55:21 -0700194static int testdevres_drv_of_to_plat(struct udevice *dev)
Simon Glass42a0ce52019-12-29 21:19:28 -0700195{
196 struct dm_testdevres_priv *priv = dev_get_priv(dev);
197
198 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
199
200 return 0;
201}
202
Simon Glassdc12ebb2019-12-29 21:19:25 -0700203static int testdevres_drv_probe(struct udevice *dev)
204{
205 struct dm_testdevres_priv *priv = dev_get_priv(dev);
206
207 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
208
209 return 0;
210}
211
212static const struct udevice_id testdevres_ids[] = {
213 { .compatible = "denx,u-boot-devres-test" },
214 { }
215};
216
217U_BOOT_DRIVER(testdevres_drv) = {
218 .name = "testdevres_drv",
219 .of_match = testdevres_ids,
220 .id = UCLASS_TEST_DEVRES,
221 .bind = testdevres_drv_bind,
Simon Glassd1998a92020-12-03 16:55:21 -0700222 .of_to_plat = testdevres_drv_of_to_plat,
Simon Glassdc12ebb2019-12-29 21:19:25 -0700223 .probe = testdevres_drv_probe,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700224 .plat_auto = sizeof(struct dm_testdevres_pdata),
Simon Glass41575d82020-12-03 16:55:17 -0700225 .priv_auto = sizeof(struct dm_testdevres_priv),
Simon Glassdc12ebb2019-12-29 21:19:25 -0700226};
227
228UCLASS_DRIVER(testdevres) = {
229 .name = "testdevres",
230 .id = UCLASS_TEST_DEVRES,
231 .flags = DM_UC_FLAG_SEQ_ALIAS,
232};
233
Joe Hershbergere721b882015-05-20 14:27:27 -0500234int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700235{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200236 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700237 int ret;
238 int i;
239
Simon Glass2e7d35d2014-02-26 15:59:21 -0700240 /*
241 * Now check that the ping adds are what we expect. This is using the
242 * ping-add property in each node.
243 */
Simon Glass1ca7e202014-07-23 06:55:18 -0600244 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-02-26 15:59:21 -0700245 uint32_t base;
246
247 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
248 ut_assert(!ret);
249
250 /*
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600251 * Get the 'ping-expect' property, which tells us what the
Simon Glasscaa4daa2020-12-03 16:55:18 -0700252 * ping add should be. We don't use the plat because we
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600253 * want to test the code that sets that up
254 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700255 */
Simon Glasse160f7d2017-01-17 16:52:55 -0700256 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600257 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700258 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glasse160f7d2017-01-17 16:52:55 -0700259 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glass2e7d35d2014-02-26 15:59:21 -0700260
Joe Hershbergere721b882015-05-20 14:27:27 -0500261 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700262 dev_get_priv(dev)));
263 }
264
265 return 0;
266}
Simon Glass1ca7e202014-07-23 06:55:18 -0600267
268/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500269static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600270{
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530271 const int num_devices = 9;
Simon Glass1ca7e202014-07-23 06:55:18 -0600272 struct udevice *dev;
273 struct uclass *uc;
274 int ret;
275 int i;
276
Simon Glass8ee05b52020-11-28 17:50:10 -0700277 ret = dm_extended_scan(false);
Simon Glass1ca7e202014-07-23 06:55:18 -0600278 ut_assert(!ret);
279
280 ret = uclass_get(UCLASS_TEST_FDT, &uc);
281 ut_assert(!ret);
282
283 /* These are num_devices compatible root-level device tree nodes */
284 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
285
Simon Glassf8a85442015-01-25 08:27:00 -0700286 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-07-23 06:55:18 -0600287 for (i = 0; i < num_devices; i++) {
288 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
289 ut_assert(!ret);
290 ut_assert(!dev_get_priv(dev));
Simon Glasscaa4daa2020-12-03 16:55:18 -0700291 ut_assert(dev->plat);
Simon Glass1ca7e202014-07-23 06:55:18 -0600292 }
293
Joe Hershbergere721b882015-05-20 14:27:27 -0500294 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600295
296 return 0;
297}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700298DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600299
Michal Simeka93eb572019-01-31 16:31:00 +0100300static int dm_test_alias_highest_id(struct unit_test_state *uts)
301{
302 int ret;
303
304 ret = dev_read_alias_highest_id("eth");
305 ut_asserteq(5, ret);
306
307 ret = dev_read_alias_highest_id("gpio");
Patrick Delaunayff526652020-01-13 11:35:14 +0100308 ut_asserteq(3, ret);
Michal Simeka93eb572019-01-31 16:31:00 +0100309
310 ret = dev_read_alias_highest_id("pci");
311 ut_asserteq(2, ret);
312
313 ret = dev_read_alias_highest_id("i2c");
314 ut_asserteq(0, ret);
315
316 ret = dev_read_alias_highest_id("deadbeef");
317 ut_asserteq(-1, ret);
318
319 return 0;
320}
321DM_TEST(dm_test_alias_highest_id, 0);
322
Joe Hershbergere721b882015-05-20 14:27:27 -0500323static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-07-23 06:55:03 -0600324{
325 struct uclass *uc;
326 int ret;
327
Simon Glass725e4fc2020-11-28 17:50:09 -0700328 ret = dm_scan_fdt(true);
Simon Glass00606d72014-07-23 06:55:03 -0600329 ut_assert(!ret);
330
331 ret = uclass_get(UCLASS_TEST_FDT, &uc);
332 ut_assert(!ret);
333
Bin Meng2786cd72018-10-10 22:07:01 -0700334 /*
335 * These are 2 pre-reloc devices:
336 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
337 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
338 */
339 ut_asserteq(2, list_count_items(&uc->dev_head));
Simon Glass00606d72014-07-23 06:55:03 -0600340
341 return 0;
342}
343DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600344
345/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500346static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600347{
348 struct udevice *dev;
349
350 /* A few basic santiy tests */
Simon Glass99175912020-12-16 21:20:29 -0700351 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600352 ut_asserteq_str("b-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700353 ut_asserteq(3, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600354
Simon Glass99175912020-12-16 21:20:29 -0700355 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600356 ut_asserteq_str("a-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700357 ut_asserteq(8, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600358
Simon Glass981426e2020-12-16 21:20:26 -0700359 /*
360 * This device has no alias so gets the next value after all available
361 * aliases. The last alias is testfdt12
362 */
Simon Glass99175912020-12-16 21:20:29 -0700363 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev));
Simon Glass981426e2020-12-16 21:20:26 -0700364 ut_asserteq_str("d-test", dev->name);
365 ut_asserteq(13, dev_seq(dev));
366
Simon Glass99175912020-12-16 21:20:29 -0700367 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass981426e2020-12-16 21:20:26 -0700368 &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600369 ut_asserteq_ptr(NULL, dev);
370
371 /* Test aliases */
372 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
373 ut_asserteq_str("e-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700374 ut_asserteq(6, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600375
Simon Glass1ca7e202014-07-23 06:55:18 -0600376 /*
377 * Note that c-test nodes are not probed since it is not a top-level
378 * node
379 */
Simon Glass5a66a8f2014-07-23 06:55:12 -0600380 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
381 ut_asserteq_str("b-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700382 ut_asserteq(3, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600383
384 /*
385 * d-test wants sequence number 3 also, but it can't have it because
386 * b-test gets it first.
387 */
388 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
389 ut_asserteq_str("d-test", dev->name);
Simon Glass981426e2020-12-16 21:20:26 -0700390 ut_asserteq(13, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600391
Simon Glass981426e2020-12-16 21:20:26 -0700392 /* g-test gets the next value after f-test */
393 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 15, &dev));
394 ut_asserteq_str("g-test", dev->name);
395 ut_asserteq(15, dev_seq(dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600396
Simon Glass981426e2020-12-16 21:20:26 -0700397 /* And we should still have holes in our sequence numbers */
Simon Glass99175912020-12-16 21:20:29 -0700398 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0,
Simon Glass981426e2020-12-16 21:20:26 -0700399 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700400 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1,
Simon Glass981426e2020-12-16 21:20:26 -0700401 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700402 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
Simon Glass981426e2020-12-16 21:20:26 -0700403 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700404 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
Simon Glass981426e2020-12-16 21:20:26 -0700405 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700406 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
Simon Glass981426e2020-12-16 21:20:26 -0700407 &dev));
Simon Glass99175912020-12-16 21:20:29 -0700408 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass981426e2020-12-16 21:20:26 -0700409 &dev));
410 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
Simon Glass99175912020-12-16 21:20:29 -0700411 &dev));
Simon Glass981426e2020-12-16 21:20:26 -0700412 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
Simon Glass99175912020-12-16 21:20:29 -0700413 &dev));
Michael Wallebe1a6e92020-06-02 01:47:09 +0200414
Simon Glass5a66a8f2014-07-23 06:55:12 -0600415 return 0;
416}
Simon Glasse180c2b2020-07-28 19:41:12 -0600417DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600418
Simon Glass93f44e82020-12-16 21:20:27 -0700419/* More tests for sequence numbers */
420static int dm_test_fdt_uclass_seq_manual(struct unit_test_state *uts)
421{
422 struct udevice *dev;
423
424 /*
425 * Since DM_UC_FLAG_NO_AUTO_SEQ is set for this uclass, only testfdtm1
426 * should get a sequence number assigned
427 */
428 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 0, &dev));
429 ut_asserteq_str("testfdtm0", dev->name);
430 ut_asserteq(-1, dev_seq(dev));
431
432 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT_MANUAL, 1, &dev));
433 ut_asserteq_str("testfdtm1", dev->name);
434 ut_asserteq(1, dev_seq(dev));
435
436 ut_assertok(uclass_get_device(UCLASS_TEST_FDT_MANUAL, 2, &dev));
437 ut_asserteq_str("testfdtm2", dev->name);
438 ut_asserteq(-1, dev_seq(dev));
439
440 return 0;
441}
442DM_TEST(dm_test_fdt_uclass_seq_manual, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
443
Simon Glassf4cdead2014-07-23 06:55:14 -0600444/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500445static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-07-23 06:55:14 -0600446{
447 const void *blob = gd->fdt_blob;
448 struct udevice *dev;
449 int node;
450
451 node = fdt_path_offset(blob, "/e-test");
452 ut_assert(node > 0);
453 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
454 &dev));
455 ut_asserteq_str("e-test", dev->name);
456
457 /* This node should not be bound */
458 node = fdt_path_offset(blob, "/junk");
459 ut_assert(node > 0);
460 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
461 node, &dev));
462
463 /* This is not a top level node so should not be probed */
Simon Glass1ca7e202014-07-23 06:55:18 -0600464 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-07-23 06:55:14 -0600465 ut_assert(node > 0);
466 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
467 node, &dev));
468
469 return 0;
470}
Simon Glass86b54ec2017-05-18 20:09:44 -0600471DM_TEST(dm_test_fdt_offset,
Simon Glasse180c2b2020-07-28 19:41:12 -0600472 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass98561572017-04-23 20:10:44 -0600473
474/**
475 * Test various error conditions with uclass_first_device() and
476 * uclass_next_device()
477 */
478static int dm_test_first_next_device(struct unit_test_state *uts)
479{
480 struct dm_testprobe_pdata *pdata;
481 struct udevice *dev, *parent = NULL;
482 int count;
483 int ret;
484
485 /* There should be 4 devices */
486 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
487 dev;
488 ret = uclass_next_device(&dev)) {
489 count++;
490 parent = dev_get_parent(dev);
491 }
492 ut_assertok(ret);
493 ut_asserteq(4, count);
494
495 /* Remove them and try again, with an error on the second one */
496 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
Simon Glassc69cda22020-12-03 16:55:20 -0700497 pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600498 pdata->probe_err = -ENOMEM;
499 device_remove(parent, DM_REMOVE_NORMAL);
500 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
501 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
502 ut_asserteq_ptr(dev, NULL);
503
504 /* Now an error on the first one */
505 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
Simon Glassc69cda22020-12-03 16:55:20 -0700506 pdata = dev_get_plat(dev);
Simon Glass98561572017-04-23 20:10:44 -0600507 pdata->probe_err = -ENOENT;
508 device_remove(parent, DM_REMOVE_NORMAL);
509 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
510
511 return 0;
512}
Simon Glasse180c2b2020-07-28 19:41:12 -0600513DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass95ce3852017-04-23 20:10:45 -0600514
Simon Glass3cf0fba2020-02-06 09:54:49 -0700515/* Test iteration through devices in a uclass */
516static int dm_test_uclass_foreach(struct unit_test_state *uts)
517{
518 struct udevice *dev;
519 struct uclass *uc;
520 int count;
521
522 count = 0;
523 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
524 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530525 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700526
527 count = 0;
528 uclass_foreach_dev(dev, uc)
529 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530530 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700531
532 return 0;
533}
Simon Glasse180c2b2020-07-28 19:41:12 -0600534DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700535
Simon Glass95ce3852017-04-23 20:10:45 -0600536/**
537 * check_devices() - Check return values and pointers
538 *
539 * This runs through a full sequence of uclass_first_device_check()...
540 * uclass_next_device_check() checking that the return values and devices
541 * are correct.
542 *
543 * @uts: Test state
544 * @devlist: List of expected devices
545 * @mask: Indicates which devices should return an error. Device n should
546 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
547 * bit n is clear.
548 */
549static int check_devices(struct unit_test_state *uts,
550 struct udevice *devlist[], int mask)
551{
552 int expected_ret;
553 struct udevice *dev;
554 int i;
555
556 expected_ret = (mask & 1) ? -ENOENT : 0;
557 mask >>= 1;
558 ut_asserteq(expected_ret,
559 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
560 for (i = 0; i < 4; i++) {
561 ut_asserteq_ptr(devlist[i], dev);
562 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
563 mask >>= 1;
564 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
565 }
566 ut_asserteq_ptr(NULL, dev);
567
568 return 0;
569}
570
571/* Test uclass_first_device_check() and uclass_next_device_check() */
572static int dm_test_first_next_ok_device(struct unit_test_state *uts)
573{
574 struct dm_testprobe_pdata *pdata;
575 struct udevice *dev, *parent = NULL, *devlist[4];
576 int count;
577 int ret;
578
579 /* There should be 4 devices */
580 count = 0;
581 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
582 dev;
583 ret = uclass_next_device_check(&dev)) {
584 ut_assertok(ret);
585 devlist[count++] = dev;
586 parent = dev_get_parent(dev);
587 }
588 ut_asserteq(4, count);
589 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
590 ut_assertok(check_devices(uts, devlist, 0));
591
592 /* Remove them and try again, with an error on the second one */
Simon Glassc69cda22020-12-03 16:55:20 -0700593 pdata = dev_get_plat(devlist[1]);
Simon Glass95ce3852017-04-23 20:10:45 -0600594 pdata->probe_err = -ENOENT - 1;
595 device_remove(parent, DM_REMOVE_NORMAL);
596 ut_assertok(check_devices(uts, devlist, 1 << 1));
597
598 /* Now an error on the first one */
Simon Glassc69cda22020-12-03 16:55:20 -0700599 pdata = dev_get_plat(devlist[0]);
Simon Glass95ce3852017-04-23 20:10:45 -0600600 pdata->probe_err = -ENOENT - 0;
601 device_remove(parent, DM_REMOVE_NORMAL);
602 ut_assertok(check_devices(uts, devlist, 3 << 0));
603
604 /* Now errors on all */
Simon Glassc69cda22020-12-03 16:55:20 -0700605 pdata = dev_get_plat(devlist[2]);
Simon Glass95ce3852017-04-23 20:10:45 -0600606 pdata->probe_err = -ENOENT - 2;
Simon Glassc69cda22020-12-03 16:55:20 -0700607 pdata = dev_get_plat(devlist[3]);
Simon Glass95ce3852017-04-23 20:10:45 -0600608 pdata->probe_err = -ENOENT - 3;
609 device_remove(parent, DM_REMOVE_NORMAL);
610 ut_assertok(check_devices(uts, devlist, 0xf << 0));
611
612 return 0;
613}
Simon Glasse180c2b2020-07-28 19:41:12 -0600614DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Sixe8d52912018-03-12 14:53:33 +0100615
616static const struct udevice_id fdt_dummy_ids[] = {
617 { .compatible = "denx,u-boot-fdt-dummy", },
618 { }
619};
620
621UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca507cef32018-05-19 14:13:55 +0200622 .name = "fdt-dummy",
Mario Sixe8d52912018-03-12 14:53:33 +0100623 .id = UCLASS_TEST_DUMMY,
624 .flags = DM_UC_FLAG_SEQ_ALIAS,
625};
626
627U_BOOT_DRIVER(fdt_dummy_drv) = {
628 .name = "fdt_dummy_drv",
629 .of_match = fdt_dummy_ids,
630 .id = UCLASS_TEST_DUMMY,
631};
632
633static int dm_test_fdt_translation(struct unit_test_state *uts)
634{
635 struct udevice *dev;
Fabien Dessenne641067f2019-05-31 15:11:30 +0200636 fdt32_t dma_addr[2];
Mario Sixe8d52912018-03-12 14:53:33 +0100637
638 /* Some simple translations */
Simon Glass99175912020-12-16 21:20:29 -0700639 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100640 ut_asserteq_str("dev@0,0", dev->name);
641 ut_asserteq(0x8000, dev_read_addr(dev));
642
Simon Glass99175912020-12-16 21:20:29 -0700643 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100644 ut_asserteq_str("dev@1,100", dev->name);
645 ut_asserteq(0x9000, dev_read_addr(dev));
646
Simon Glass99175912020-12-16 21:20:29 -0700647 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100648 ut_asserteq_str("dev@2,200", dev->name);
649 ut_asserteq(0xA000, dev_read_addr(dev));
650
651 /* No translation for busses with #size-cells == 0 */
Simon Glass99175912020-12-16 21:20:29 -0700652 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev));
Mario Sixe8d52912018-03-12 14:53:33 +0100653 ut_asserteq_str("dev@42", dev->name);
654 ut_asserteq(0x42, dev_read_addr(dev));
655
Fabien Dessenne641067f2019-05-31 15:11:30 +0200656 /* dma address translation */
Simon Glass99175912020-12-16 21:20:29 -0700657 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Fabien Dessenne641067f2019-05-31 15:11:30 +0200658 dma_addr[0] = cpu_to_be32(0);
659 dma_addr[1] = cpu_to_be32(0);
660 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
661
Simon Glass99175912020-12-16 21:20:29 -0700662 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Fabien Dessenne641067f2019-05-31 15:11:30 +0200663 dma_addr[0] = cpu_to_be32(1);
664 dma_addr[1] = cpu_to_be32(0x100);
665 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
666
Mario Sixe8d52912018-03-12 14:53:33 +0100667 return 0;
668}
Simon Glasse180c2b2020-07-28 19:41:12 -0600669DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200670
Ovidiu Panait6e648302020-08-03 22:17:36 +0300671static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
672{
673 struct udevice *gpio, *dev;
674 void *ptr;
675
676 /* Test for missing reg property */
677 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
678 ut_assertnull(devfdt_get_addr_ptr(gpio));
679
Simon Glass99175912020-12-16 21:20:29 -0700680 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Ovidiu Panait6e648302020-08-03 22:17:36 +0300681 ptr = devfdt_get_addr_ptr(dev);
682 ut_asserteq_ptr((void *)0x8000, ptr);
683
684 return 0;
685}
686DM_TEST(dm_test_fdt_get_addr_ptr_flat,
687 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
688
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200689static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
690{
691 struct udevice *dev;
692 fdt_addr_t addr;
693 void *paddr;
694
Simon Glass99175912020-12-16 21:20:29 -0700695 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200696
697 addr = devfdt_get_addr(dev);
698 ut_asserteq(0x8000, addr);
699
700 paddr = map_physmem(addr, 0, MAP_NOCACHE);
701 ut_assertnonnull(paddr);
702 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
703
704 return 0;
705}
706DM_TEST(dm_test_fdt_remap_addr_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600707 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200708
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100709static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
710{
711 struct udevice *dev;
712 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530713 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100714 void *paddr;
715
Simon Glass99175912020-12-16 21:20:29 -0700716 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100717
Sekhar Norif5b90472019-08-01 19:12:56 +0530718 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100719 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530720 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100721
722 paddr = map_physmem(addr, 0, MAP_NOCACHE);
723 ut_assertnonnull(paddr);
724 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
725
726 return 0;
727}
728DM_TEST(dm_test_fdt_remap_addr_index_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600729 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100730
731static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
732{
733 struct udevice *dev;
734 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530735 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100736 void *paddr;
737
Simon Glass99175912020-12-16 21:20:29 -0700738 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100739
Sekhar Norif5b90472019-08-01 19:12:56 +0530740 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100741 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530742 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100743
744 paddr = map_physmem(addr, 0, MAP_NOCACHE);
745 ut_assertnonnull(paddr);
746 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
747
748 return 0;
749}
750DM_TEST(dm_test_fdt_remap_addr_name_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600751 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100752
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200753static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
754{
755 struct udevice *dev;
756 fdt_addr_t addr;
757 void *paddr;
758
Simon Glass99175912020-12-16 21:20:29 -0700759 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200760
761 addr = dev_read_addr(dev);
762 ut_asserteq(0x8000, addr);
763
764 paddr = map_physmem(addr, 0, MAP_NOCACHE);
765 ut_assertnonnull(paddr);
766 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
767
768 return 0;
769}
770DM_TEST(dm_test_fdt_remap_addr_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600771 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six2ea4d0d2018-06-26 08:46:49 +0200772
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100773static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
774{
775 struct udevice *dev;
776 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530777 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100778 void *paddr;
779
Simon Glass99175912020-12-16 21:20:29 -0700780 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100781
Sekhar Norif5b90472019-08-01 19:12:56 +0530782 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100783 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530784 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100785
786 paddr = map_physmem(addr, 0, MAP_NOCACHE);
787 ut_assertnonnull(paddr);
788 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
789
790 return 0;
791}
792DM_TEST(dm_test_fdt_remap_addr_index_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600793 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100794
795static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
796{
797 struct udevice *dev;
798 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530799 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100800 void *paddr;
801
Simon Glass99175912020-12-16 21:20:29 -0700802 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100803
Sekhar Norif5b90472019-08-01 19:12:56 +0530804 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100805 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530806 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100807
808 paddr = map_physmem(addr, 0, MAP_NOCACHE);
809 ut_assertnonnull(paddr);
810 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
811
812 return 0;
813}
814DM_TEST(dm_test_fdt_remap_addr_name_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600815 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100816
Mario Six2ea4d0d2018-06-26 08:46:49 +0200817static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
818{
819 struct udevice *dev;
820 ofnode node;
821
822 if (!of_live_active()) {
823 printf("Live tree not active; ignore test\n");
824 return 0;
825 }
826
827 /* Test enabling devices */
828
829 node = ofnode_path("/usb@2");
830
831 ut_assert(!of_device_is_available(ofnode_to_np(node)));
832 ofnode_set_enabled(node, true);
833 ut_assert(of_device_is_available(ofnode_to_np(node)));
834
835 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
836 &dev);
Simon Glass99175912020-12-16 21:20:29 -0700837 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
Mario Six2ea4d0d2018-06-26 08:46:49 +0200838
839 /* Test string property setting */
840
841 ut_assert(device_is_compatible(dev, "sandbox,usb"));
842 ofnode_write_string(node, "compatible", "gdsys,super-usb");
843 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
844 ofnode_write_string(node, "compatible", "sandbox,usb");
845 ut_assert(device_is_compatible(dev, "sandbox,usb"));
846
847 /* Test setting generic properties */
848
849 /* Non-existent in DTB */
850 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
851 /* reg = 0x42, size = 0x100 */
852 ut_assertok(ofnode_write_prop(node, "reg", 8,
853 "\x00\x00\x00\x42\x00\x00\x01\x00"));
854 ut_asserteq(0x42, dev_read_addr(dev));
855
856 /* Test disabling devices */
857
858 device_remove(dev, DM_REMOVE_NORMAL);
859 device_unbind(dev);
860
861 ut_assert(of_device_is_available(ofnode_to_np(node)));
862 ofnode_set_enabled(node, false);
863 ut_assert(!of_device_is_available(ofnode_to_np(node)));
864
865 return 0;
866}
Simon Glasse180c2b2020-07-28 19:41:12 -0600867DM_TEST(dm_test_fdt_livetree_writing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six172942a2018-06-26 08:46:51 +0200868
869static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
870{
871 ofnode node;
872
873 if (!of_live_active()) {
874 printf("Live tree not active; ignore test\n");
875 return 0;
876 }
877
878 node = ofnode_path("/usb@2");
879
880 /* Test enabling devices */
881
882 ut_assert(!of_device_is_available(ofnode_to_np(node)));
883 dev_enable_by_path("/usb@2");
884 ut_assert(of_device_is_available(ofnode_to_np(node)));
885
886 /* Test disabling devices */
887
888 ut_assert(of_device_is_available(ofnode_to_np(node)));
889 dev_disable_by_path("/usb@2");
890 ut_assert(!of_device_is_available(ofnode_to_np(node)));
891
892 return 0;
893}
Simon Glasse180c2b2020-07-28 19:41:12 -0600894DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
895 UT_TESTF_SCAN_FDT);
Simon Glassd0b4f682018-11-18 08:14:30 -0700896
897/* Test a few uclass phandle functions */
898static int dm_test_fdt_phandle(struct unit_test_state *uts)
899{
900 struct udevice *back, *dev, *dev2;
901
902 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200903 ut_assertnonnull(back);
Simon Glassd0b4f682018-11-18 08:14:30 -0700904 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
905 back, "missing", &dev));
906 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
907 "power-supply", &dev));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200908 ut_assertnonnull(dev);
Simon Glassd0b4f682018-11-18 08:14:30 -0700909 ut_asserteq(0, device_active(dev));
910 ut_asserteq_str("ldo1", dev->name);
911 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
912 "power-supply", &dev2));
913 ut_asserteq_ptr(dev, dev2);
914
915 return 0;
916}
Simon Glasse180c2b2020-07-28 19:41:12 -0600917DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3abe1112018-11-18 08:14:31 -0700918
919/* Test device_find_first_child_by_uclass() */
920static int dm_test_first_child(struct unit_test_state *uts)
921{
922 struct udevice *i2c, *dev, *dev2;
923
924 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
925 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
926 ut_asserteq_str("rtc@43", dev->name);
927 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
928 ut_asserteq_ptr(dev, dev2);
929 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
930 ut_asserteq_str("rtc@61", dev2->name);
931
932 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
933 &dev));
934 ut_asserteq_str("eeprom@2c", dev->name);
935 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
936 ut_asserteq_ptr(dev, dev2);
937
938 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
939 UCLASS_VIDEO, &dev));
940 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
941
942 return 0;
943}
Simon Glasse180c2b2020-07-28 19:41:12 -0600944DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa1b17e42018-12-10 10:37:37 -0700945
946/* Test integer functions in dm_read_...() */
947static int dm_test_read_int(struct unit_test_state *uts)
948{
949 struct udevice *dev;
950 u32 val32;
951 s32 sval;
952 uint val;
Dario Binacchi70573c62020-03-29 18:04:40 +0200953 u64 val64;
Simon Glassa1b17e42018-12-10 10:37:37 -0700954
955 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
956 ut_asserteq_str("a-test", dev->name);
957 ut_assertok(dev_read_u32(dev, "int-value", &val32));
958 ut_asserteq(1234, val32);
959
960 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
961 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
962
963 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
964 ut_asserteq(1234, val32);
965
966 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
967 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
968
969 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
970 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
971 ut_asserteq(-1234, sval);
972
973 val = 0;
974 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
975 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
976 ut_asserteq(-1234, val);
977
Dario Binacchi70573c62020-03-29 18:04:40 +0200978 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
979 ut_asserteq_64(0x1111222233334444, val64);
980
981 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
982 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
983
984 ut_asserteq_64(0x1111222233334444,
985 dev_read_u64_default(dev, "int64-value", 6));
986
Simon Glassa1b17e42018-12-10 10:37:37 -0700987 return 0;
988}
Simon Glasse180c2b2020-07-28 19:41:12 -0600989DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass50162342020-02-06 09:54:50 -0700990
Dario Binacchi4bb70752020-03-29 18:04:41 +0200991static int dm_test_read_int_index(struct unit_test_state *uts)
992{
993 struct udevice *dev;
994 u32 val32;
995
996 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
997 ut_asserteq_str("a-test", dev->name);
998
999 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
1000 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
1001
1002 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
1003 ut_asserteq(5678, val32);
1004 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
1005 ut_asserteq(9123, val32);
1006 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
1007 ut_asserteq(4567, val32);
1008 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
1009 &val32));
1010
1011 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
1012 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
1013 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
1014 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
1015
1016 return 0;
1017}
Simon Glasse180c2b2020-07-28 19:41:12 -06001018DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi4bb70752020-03-29 18:04:41 +02001019
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +02001020static int dm_test_read_phandle(struct unit_test_state *uts)
1021{
1022 struct udevice *dev;
1023 struct ofnode_phandle_args args;
1024 int ret;
1025 const char prop[] = "test-gpios";
1026 const char cell[] = "#gpio-cells";
1027 const char prop2[] = "phandle-value";
1028
1029 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1030 ut_asserteq_str("a-test", dev->name);
1031
1032 /* Test dev_count_phandle_with_args with cell name */
1033 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
1034 ut_asserteq(-ENOENT, ret);
1035 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
1036 ut_asserteq(-EINVAL, ret);
1037 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
1038
1039 /* Test dev_read_phandle_with_args with cell name */
1040 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
1041 ut_asserteq(-ENOENT, ret);
1042 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
1043 ut_asserteq(-EINVAL, ret);
1044 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
1045 ut_asserteq(1, args.args_count);
1046 ut_asserteq(1, args.args[0]);
1047 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
1048 ut_asserteq(1, args.args_count);
1049 ut_asserteq(4, args.args[0]);
1050 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
1051 ut_asserteq(5, args.args_count);
1052 ut_asserteq(5, args.args[0]);
1053 ut_asserteq(1, args.args[4]);
1054 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
1055 ut_asserteq(-ENOENT, ret);
1056 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
1057 ut_asserteq(1, args.args_count);
1058 ut_asserteq(12, args.args[0]);
1059 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
1060 ut_asserteq(-ENOENT, ret);
1061
1062 /* Test dev_count_phandle_with_args with cell count */
1063 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
1064 ut_asserteq(-ENOENT, ret);
1065 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
1066
1067 /* Test dev_read_phandle_with_args with cell count */
1068 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
1069 ut_asserteq(1, ofnode_valid(args.node));
1070 ut_asserteq(1, args.args_count);
1071 ut_asserteq(10, args.args[0]);
1072 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
1073 ut_asserteq(-EINVAL, ret);
1074 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
1075 ut_asserteq(1, ofnode_valid(args.node));
1076 ut_asserteq(1, args.args_count);
1077 ut_asserteq(30, args.args[0]);
1078 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
1079 ut_asserteq(-ENOENT, ret);
1080
1081 return 0;
1082}
1083DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1084
Simon Glass50162342020-02-06 09:54:50 -07001085/* Test iteration through devices by drvdata */
1086static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1087{
1088 struct udevice *dev;
1089
1090 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1091 DM_TEST_TYPE_FIRST, &dev));
1092 ut_asserteq_str("a-test", dev->name);
1093
1094 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1095 DM_TEST_TYPE_SECOND, &dev));
1096 ut_asserteq_str("d-test", dev->name);
1097
1098 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1099 DM_TEST_TYPE_COUNT,
1100 &dev));
1101
1102 return 0;
1103}
Simon Glasse180c2b2020-07-28 19:41:12 -06001104DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Tom Rini9a8942b2020-02-11 10:58:41 -05001105
Simon Glassf262d4c2020-01-27 08:49:47 -07001106/* Test device_first_child_ofdata_err(), etc. */
1107static int dm_test_child_ofdata(struct unit_test_state *uts)
1108{
1109 struct udevice *bus, *dev;
1110 int count;
1111
1112 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1113 count = 0;
Simon Glassd1998a92020-12-03 16:55:21 -07001114 device_foreach_child_of_to_plat(dev, bus) {
Simon Glassf262d4c2020-01-27 08:49:47 -07001115 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1116 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1117 count++;
1118 }
1119 ut_asserteq(3, count);
1120
1121 return 0;
1122}
Simon Glasse180c2b2020-07-28 19:41:12 -06001123DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass903e83e2020-01-27 08:49:48 -07001124
1125/* Test device_first_child_err(), etc. */
1126static int dm_test_first_child_probe(struct unit_test_state *uts)
1127{
1128 struct udevice *bus, *dev;
1129 int count;
1130
1131 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1132 count = 0;
1133 device_foreach_child_probe(dev, bus) {
1134 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1135 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
1136 count++;
1137 }
1138 ut_asserteq(3, count);
1139
1140 return 0;
1141}
Simon Glasse180c2b2020-07-28 19:41:12 -06001142DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb0dcc872020-04-05 15:38:19 -06001143
1144/* Test that ofdata is read for parents before children */
1145static int dm_test_ofdata_order(struct unit_test_state *uts)
1146{
1147 struct udevice *bus, *dev;
1148
1149 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1150 ut_assertnonnull(bus);
1151 ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID));
1152
1153 ut_assertok(device_find_first_child(bus, &dev));
1154 ut_assertnonnull(dev);
1155 ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID));
1156
1157 /* read the child's ofdata which should cause the parent's to be read */
Simon Glassd1998a92020-12-03 16:55:21 -07001158 ut_assertok(device_of_to_plat(dev));
Simon Glassb0dcc872020-04-05 15:38:19 -06001159 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1160 ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID);
1161
1162 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1163 ut_assert(!(bus->flags & DM_FLAG_ACTIVATED));
1164
1165 return 0;
1166}
Simon Glasse180c2b2020-07-28 19:41:12 -06001167DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);