blob: 9e8c2906e8466f889498cdb01301feb7ff7c43d4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glassb2c1cac2014-02-26 15:59:21 -07004 */
5
6#include <common.h>
7#include <dm.h>
8#include <errno.h>
9#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glassb2c1cac2014-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 Glass9c433fe2017-04-23 20:10:44 -060015#include <dm/device-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <dm/devres.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070017#include <dm/uclass-internal.h>
18#include <dm/util.h>
Mario Six058ef5b2018-06-26 08:46:49 +020019#include <dm/lists.h>
20#include <dm/of_access.h>
Simon Glass75c4d412020-07-19 10:15:37 -060021#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050022#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070023
24DECLARE_GLOBAL_DATA_PTR;
25
Heiko Schocherb74fcb42014-05-22 12:43:05 +020026static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070027{
Simon Glass95588622020-12-22 19:30:28 -070028 const struct dm_test_pdata *pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-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 Glassaad29ae2020-12-03 16:55:21 -070041static int testfdt_of_to_plat(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070042{
Simon Glassfa20e932020-12-03 16:55:20 -070043 struct dm_test_pdata *pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -070044
Simon Glassdd79d6e2017-01-17 16:52:55 -070045 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glassb2c1cac2014-02-26 15:59:21 -070046 "ping-add", -1);
Simon Glassdd79d6e2017-01-17 16:52:55 -070047 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glassa7bb08a2014-07-23 06:54:57 -060048 "ping-expect");
Simon Glassb2c1cac2014-02-26 15:59:21 -070049
50 return 0;
51}
52
Heiko Schocherb74fcb42014-05-22 12:43:05 +020053static int testfdt_drv_probe(struct udevice *dev)
Simon Glassb2c1cac2014-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 Glass5104b982015-01-25 08:27:10 -070059 /*
60 * If this device is on a bus, the uclass_flag will be set before
Bin Mengd9bad172018-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 Glass5104b982015-01-25 08:27:10 -070065 */
66 priv->uclass_total += priv->uclass_flag;
Bin Mengd9bad172018-10-15 02:20:58 -070067 priv->uclass_postp = -1;
Simon Glass5104b982015-01-25 08:27:10 -070068
Simon Glassb2c1cac2014-02-26 15:59:21 -070069 return 0;
70}
71
Simon Glass767827a2014-06-11 23:29:45 -060072static const struct udevice_id testfdt_ids[] = {
Simon Glassb2c1cac2014-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 Glassaad29ae2020-12-03 16:55:21 -070086 .of_to_plat = testfdt_of_to_plat,
Simon Glassb2c1cac2014-02-26 15:59:21 -070087 .probe = testfdt_drv_probe,
88 .ops = &test_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -070089 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -070090 .plat_auto = sizeof(struct dm_test_pdata),
Simon Glassb2c1cac2014-02-26 15:59:21 -070091};
92
Bin Mengd9d24782018-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 Glassaad29ae2020-12-03 16:55:21 -0700104 .of_to_plat = testfdt_of_to_plat,
Bin Mengd9d24782018-10-10 22:07:01 -0700105 .probe = testfdt_drv_probe,
106 .ops = &test_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700107 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700108 .plat_auto = sizeof(struct dm_test_pdata),
Bin Mengd9d24782018-10-10 22:07:01 -0700109 .flags = DM_FLAG_PRE_RELOC,
110};
111
Simon Glassb2c1cac2014-02-26 15:59:21 -0700112/* From here is the testfdt uclass code */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200113int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-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 Glass0ccb0972015-01-25 08:27:05 -0700126 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700127};
128
Simon Glass791a17f2020-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 Glass9c433fe2017-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 Glassfa20e932020-12-03 16:55:20 -0700152 struct dm_testprobe_pdata *pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-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 Glass71fa5b42020-12-03 16:55:18 -0700167 .plat_auto = sizeof(struct dm_testprobe_pdata),
Simon Glass9c433fe2017-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 Glass204675c2019-12-29 21:19:25 -0700176struct dm_testdevres_pdata {
177 void *ptr;
178};
179
180struct dm_testdevres_priv {
181 void *ptr;
Simon Glass0fe15372019-12-29 21:19:28 -0700182 void *ptr_ofdata;
Simon Glass204675c2019-12-29 21:19:25 -0700183};
184
185static int testdevres_drv_bind(struct udevice *dev)
186{
Simon Glassfa20e932020-12-03 16:55:20 -0700187 struct dm_testdevres_pdata *pdata = dev_get_plat(dev);
Simon Glass204675c2019-12-29 21:19:25 -0700188
189 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
190
191 return 0;
192}
193
Simon Glassaad29ae2020-12-03 16:55:21 -0700194static int testdevres_drv_of_to_plat(struct udevice *dev)
Simon Glass0fe15372019-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 Glass204675c2019-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 Glassaad29ae2020-12-03 16:55:21 -0700222 .of_to_plat = testdevres_drv_of_to_plat,
Simon Glass204675c2019-12-29 21:19:25 -0700223 .probe = testdevres_drv_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700224 .plat_auto = sizeof(struct dm_testdevres_pdata),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700225 .priv_auto = sizeof(struct dm_testdevres_priv),
Simon Glass204675c2019-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 Hershberger3a77be52015-05-20 14:27:27 -0500234int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700235{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200236 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700237 int ret;
238 int i;
239
Simon Glassb2c1cac2014-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 Glass40717422014-07-23 06:55:18 -0600244 for (i = 0; i < num_devices; i++) {
Simon Glassb2c1cac2014-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 Glassa7bb08a2014-07-23 06:54:57 -0600251 * Get the 'ping-expect' property, which tells us what the
Simon Glass71fa5b42020-12-03 16:55:18 -0700252 * ping add should be. We don't use the plat because we
Simon Glassa7bb08a2014-07-23 06:54:57 -0600253 * want to test the code that sets that up
254 * (testfdt_drv_probe()).
Simon Glassb2c1cac2014-02-26 15:59:21 -0700255 */
Simon Glassdd79d6e2017-01-17 16:52:55 -0700256 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glassa7bb08a2014-07-23 06:54:57 -0600257 "ping-expect");
Simon Glassb2c1cac2014-02-26 15:59:21 -0700258 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glassdd79d6e2017-01-17 16:52:55 -0700259 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700260
Joe Hershberger3a77be52015-05-20 14:27:27 -0500261 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700262 dev_get_priv(dev)));
263 }
264
Simon Glass40717422014-07-23 06:55:18 -0600265 return 0;
266}
267
268/* Test that FDT-based binding works correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500269static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -0600270{
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530271 const int num_devices = 9;
Simon Glass40717422014-07-23 06:55:18 -0600272 struct udevice *dev;
273 struct uclass *uc;
274 int ret;
275 int i;
276
Simon Glass4d71d602020-11-28 17:50:10 -0700277 ret = dm_extended_scan(false);
Simon Glass40717422014-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 Glassba750492015-01-25 08:27:00 -0700286 /* Each should have platform data but no private data */
Simon Glass40717422014-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 Glass95588622020-12-22 19:30:28 -0700291 ut_assert(dev_get_plat(dev));
Simon Glass40717422014-07-23 06:55:18 -0600292 }
293
Joe Hershberger3a77be52015-05-20 14:27:27 -0500294 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -0600295
Simon Glassb2c1cac2014-02-26 15:59:21 -0700296 return 0;
297}
298DM_TEST(dm_test_fdt, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600299
Michal Simek4247cf42019-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 Delaunay28bdaa52020-01-13 11:35:14 +0100308 ut_asserteq(3, ret);
Michal Simek4247cf42019-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 Hershberger3a77be52015-05-20 14:27:27 -0500323static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600324{
325 struct uclass *uc;
326 int ret;
327
Simon Glass5039cab2020-11-28 17:50:09 -0700328 ret = dm_scan_fdt(true);
Simon Glassfef72b72014-07-23 06:55:03 -0600329 ut_assert(!ret);
330
331 ret = uclass_get(UCLASS_TEST_FDT, &uc);
332 ut_assert(!ret);
333
Bin Mengd9d24782018-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 Glassfef72b72014-07-23 06:55:03 -0600340
341 return 0;
342}
343DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glassdb6f0202014-07-23 06:55:12 -0600344
345/* Test that sequence numbers are allocated properly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500346static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glassdb6f0202014-07-23 06:55:12 -0600347{
348 struct udevice *dev;
349
350 /* A few basic santiy tests */
Simon Glass07e13382020-12-16 21:20:29 -0700351 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600352 ut_asserteq_str("b-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700353 ut_asserteq(3, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600354
Simon Glass07e13382020-12-16 21:20:29 -0700355 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, &dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600356 ut_asserteq_str("a-test", dev->name);
Simon Glass7d5e4112020-12-16 21:20:26 -0700357 ut_asserteq(8, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600358
Simon Glass7d5e4112020-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 Glass07e13382020-12-16 21:20:29 -0700363 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 13, &dev));
Simon Glass7d5e4112020-12-16 21:20:26 -0700364 ut_asserteq_str("d-test", dev->name);
365 ut_asserteq(13, dev_seq(dev));
366
Simon Glass07e13382020-12-16 21:20:29 -0700367 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass7d5e4112020-12-16 21:20:26 -0700368 &dev));
Simon Glassdb6f0202014-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 Glass7d5e4112020-12-16 21:20:26 -0700374 ut_asserteq(6, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600375
Simon Glass40717422014-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 Glassdb6f0202014-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 Glass7d5e4112020-12-16 21:20:26 -0700382 ut_asserteq(3, dev_seq(dev));
Simon Glassdb6f0202014-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 Glass7d5e4112020-12-16 21:20:26 -0700390 ut_asserteq(13, dev_seq(dev));
Simon Glassdb6f0202014-07-23 06:55:12 -0600391
Simon Glass7d5e4112020-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 Glassdb6f0202014-07-23 06:55:12 -0600396
Simon Glass7d5e4112020-12-16 21:20:26 -0700397 /* And we should still have holes in our sequence numbers */
Simon Glass07e13382020-12-16 21:20:29 -0700398 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 0,
Simon Glass7d5e4112020-12-16 21:20:26 -0700399 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700400 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 1,
Simon Glass7d5e4112020-12-16 21:20:26 -0700401 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700402 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
Simon Glass7d5e4112020-12-16 21:20:26 -0700403 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700404 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
Simon Glass7d5e4112020-12-16 21:20:26 -0700405 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700406 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
Simon Glass7d5e4112020-12-16 21:20:26 -0700407 &dev));
Simon Glass07e13382020-12-16 21:20:29 -0700408 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 9,
Simon Glass7d5e4112020-12-16 21:20:26 -0700409 &dev));
410 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 10,
Simon Glass07e13382020-12-16 21:20:29 -0700411 &dev));
Simon Glass7d5e4112020-12-16 21:20:26 -0700412 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 11,
Simon Glass07e13382020-12-16 21:20:29 -0700413 &dev));
Michael Walle7c41a222020-06-02 01:47:09 +0200414
Simon Glassdb6f0202014-07-23 06:55:12 -0600415 return 0;
416}
Simon Glass974dccd2020-07-28 19:41:12 -0600417DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassc1464ab2014-07-23 06:55:14 -0600418
Simon Glass791a17f2020-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 Glass38040a92020-12-16 21:20:32 -0700444static int dm_test_fdt_uclass_seq_more(struct unit_test_state *uts)
445{
446 struct udevice *dev;
447 ofnode node;
448
449 /* Check creating a device with an alias */
450 node = ofnode_path("/some-bus/c-test@1");
451 ut_assertok(device_bind(dm_root(), DM_GET_DRIVER(testfdt_drv),
452 "c-test@1", NULL, node, &dev));
453 ut_asserteq(12, dev_seq(dev));
454 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 12, &dev));
455 ut_asserteq_str("c-test@1", dev->name);
456
457 /*
458 * Now bind a device without an alias. It should not get the next
459 * sequence number after all aliases, and existing bound devices. The
460 * last alias is 12, so we have:
461 *
462 * 13 d-test
463 * 14 f-test
464 * 15 g-test
465 * 16 h-test
466 * 17 another-test
467 * 18 chosen-test
468 *
469 * So next available is 19
470 */
471 ut_assertok(device_bind(dm_root(), DM_GET_DRIVER(testfdt_drv),
472 "fred", NULL, ofnode_null(), &dev));
473 ut_asserteq(19, dev_seq(dev));
474
475 ut_assertok(device_bind(dm_root(), DM_GET_DRIVER(testfdt_drv),
476 "fred2", NULL, ofnode_null(), &dev));
477 ut_asserteq(20, dev_seq(dev));
478
479 return 0;
480}
481DM_TEST(dm_test_fdt_uclass_seq_more, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
482
Simon Glassc1464ab2014-07-23 06:55:14 -0600483/* Test that we can find a device by device tree offset */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500484static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassc1464ab2014-07-23 06:55:14 -0600485{
486 const void *blob = gd->fdt_blob;
487 struct udevice *dev;
488 int node;
489
490 node = fdt_path_offset(blob, "/e-test");
491 ut_assert(node > 0);
492 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
493 &dev));
494 ut_asserteq_str("e-test", dev->name);
495
496 /* This node should not be bound */
497 node = fdt_path_offset(blob, "/junk");
498 ut_assert(node > 0);
499 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
500 node, &dev));
501
502 /* This is not a top level node so should not be probed */
Simon Glass40717422014-07-23 06:55:18 -0600503 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassc1464ab2014-07-23 06:55:14 -0600504 ut_assert(node > 0);
505 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
506 node, &dev));
507
508 return 0;
509}
Simon Glass70b66e72017-05-18 20:09:44 -0600510DM_TEST(dm_test_fdt_offset,
Simon Glass974dccd2020-07-28 19:41:12 -0600511 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass9c433fe2017-04-23 20:10:44 -0600512
513/**
514 * Test various error conditions with uclass_first_device() and
515 * uclass_next_device()
516 */
517static int dm_test_first_next_device(struct unit_test_state *uts)
518{
519 struct dm_testprobe_pdata *pdata;
520 struct udevice *dev, *parent = NULL;
521 int count;
522 int ret;
523
524 /* There should be 4 devices */
525 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
526 dev;
527 ret = uclass_next_device(&dev)) {
528 count++;
529 parent = dev_get_parent(dev);
530 }
531 ut_assertok(ret);
532 ut_asserteq(4, count);
533
534 /* Remove them and try again, with an error on the second one */
535 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
Simon Glassfa20e932020-12-03 16:55:20 -0700536 pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-04-23 20:10:44 -0600537 pdata->probe_err = -ENOMEM;
538 device_remove(parent, DM_REMOVE_NORMAL);
539 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
540 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
541 ut_asserteq_ptr(dev, NULL);
542
543 /* Now an error on the first one */
544 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
Simon Glassfa20e932020-12-03 16:55:20 -0700545 pdata = dev_get_plat(dev);
Simon Glass9c433fe2017-04-23 20:10:44 -0600546 pdata->probe_err = -ENOENT;
547 device_remove(parent, DM_REMOVE_NORMAL);
548 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
549
550 return 0;
551}
Simon Glass974dccd2020-07-28 19:41:12 -0600552DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600553
Simon Glass27c0e172020-02-06 09:54:49 -0700554/* Test iteration through devices in a uclass */
555static int dm_test_uclass_foreach(struct unit_test_state *uts)
556{
557 struct udevice *dev;
558 struct uclass *uc;
559 int count;
560
561 count = 0;
562 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
563 count++;
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530564 ut_asserteq(9, count);
Simon Glass27c0e172020-02-06 09:54:49 -0700565
566 count = 0;
567 uclass_foreach_dev(dev, uc)
568 count++;
Jean-Jacques Hiblot73873402020-09-11 13:43:35 +0530569 ut_asserteq(9, count);
Simon Glass27c0e172020-02-06 09:54:49 -0700570
571 return 0;
572}
Simon Glass974dccd2020-07-28 19:41:12 -0600573DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass27c0e172020-02-06 09:54:49 -0700574
Simon Glass3ff83bc2017-04-23 20:10:45 -0600575/**
576 * check_devices() - Check return values and pointers
577 *
578 * This runs through a full sequence of uclass_first_device_check()...
579 * uclass_next_device_check() checking that the return values and devices
580 * are correct.
581 *
582 * @uts: Test state
583 * @devlist: List of expected devices
584 * @mask: Indicates which devices should return an error. Device n should
585 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
586 * bit n is clear.
587 */
588static int check_devices(struct unit_test_state *uts,
589 struct udevice *devlist[], int mask)
590{
591 int expected_ret;
592 struct udevice *dev;
593 int i;
594
595 expected_ret = (mask & 1) ? -ENOENT : 0;
596 mask >>= 1;
597 ut_asserteq(expected_ret,
598 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
599 for (i = 0; i < 4; i++) {
600 ut_asserteq_ptr(devlist[i], dev);
601 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
602 mask >>= 1;
603 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
604 }
605 ut_asserteq_ptr(NULL, dev);
606
607 return 0;
608}
609
610/* Test uclass_first_device_check() and uclass_next_device_check() */
611static int dm_test_first_next_ok_device(struct unit_test_state *uts)
612{
613 struct dm_testprobe_pdata *pdata;
614 struct udevice *dev, *parent = NULL, *devlist[4];
615 int count;
616 int ret;
617
618 /* There should be 4 devices */
619 count = 0;
620 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
621 dev;
622 ret = uclass_next_device_check(&dev)) {
623 ut_assertok(ret);
624 devlist[count++] = dev;
625 parent = dev_get_parent(dev);
626 }
627 ut_asserteq(4, count);
628 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
629 ut_assertok(check_devices(uts, devlist, 0));
630
631 /* Remove them and try again, with an error on the second one */
Simon Glassfa20e932020-12-03 16:55:20 -0700632 pdata = dev_get_plat(devlist[1]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600633 pdata->probe_err = -ENOENT - 1;
634 device_remove(parent, DM_REMOVE_NORMAL);
635 ut_assertok(check_devices(uts, devlist, 1 << 1));
636
637 /* Now an error on the first one */
Simon Glassfa20e932020-12-03 16:55:20 -0700638 pdata = dev_get_plat(devlist[0]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600639 pdata->probe_err = -ENOENT - 0;
640 device_remove(parent, DM_REMOVE_NORMAL);
641 ut_assertok(check_devices(uts, devlist, 3 << 0));
642
643 /* Now errors on all */
Simon Glassfa20e932020-12-03 16:55:20 -0700644 pdata = dev_get_plat(devlist[2]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600645 pdata->probe_err = -ENOENT - 2;
Simon Glassfa20e932020-12-03 16:55:20 -0700646 pdata = dev_get_plat(devlist[3]);
Simon Glass3ff83bc2017-04-23 20:10:45 -0600647 pdata->probe_err = -ENOENT - 3;
648 device_remove(parent, DM_REMOVE_NORMAL);
649 ut_assertok(check_devices(uts, devlist, 0xf << 0));
650
651 return 0;
652}
Simon Glass974dccd2020-07-28 19:41:12 -0600653DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six35616ef2018-03-12 14:53:33 +0100654
655static const struct udevice_id fdt_dummy_ids[] = {
656 { .compatible = "denx,u-boot-fdt-dummy", },
657 { }
658};
659
660UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca5ba71e52018-05-19 14:13:55 +0200661 .name = "fdt-dummy",
Mario Six35616ef2018-03-12 14:53:33 +0100662 .id = UCLASS_TEST_DUMMY,
663 .flags = DM_UC_FLAG_SEQ_ALIAS,
664};
665
666U_BOOT_DRIVER(fdt_dummy_drv) = {
667 .name = "fdt_dummy_drv",
668 .of_match = fdt_dummy_ids,
669 .id = UCLASS_TEST_DUMMY,
670};
671
672static int dm_test_fdt_translation(struct unit_test_state *uts)
673{
674 struct udevice *dev;
Fabien Dessenne22236e02019-05-31 15:11:30 +0200675 fdt32_t dma_addr[2];
Mario Six35616ef2018-03-12 14:53:33 +0100676
677 /* Some simple translations */
Simon Glass07e13382020-12-16 21:20:29 -0700678 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100679 ut_asserteq_str("dev@0,0", dev->name);
680 ut_asserteq(0x8000, dev_read_addr(dev));
681
Simon Glass07e13382020-12-16 21:20:29 -0700682 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100683 ut_asserteq_str("dev@1,100", dev->name);
684 ut_asserteq(0x9000, dev_read_addr(dev));
685
Simon Glass07e13382020-12-16 21:20:29 -0700686 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100687 ut_asserteq_str("dev@2,200", dev->name);
688 ut_asserteq(0xA000, dev_read_addr(dev));
689
690 /* No translation for busses with #size-cells == 0 */
Simon Glass07e13382020-12-16 21:20:29 -0700691 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, &dev));
Mario Six35616ef2018-03-12 14:53:33 +0100692 ut_asserteq_str("dev@42", dev->name);
693 ut_asserteq(0x42, dev_read_addr(dev));
694
Fabien Dessenne22236e02019-05-31 15:11:30 +0200695 /* dma address translation */
Simon Glass07e13382020-12-16 21:20:29 -0700696 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Fabien Dessenne22236e02019-05-31 15:11:30 +0200697 dma_addr[0] = cpu_to_be32(0);
698 dma_addr[1] = cpu_to_be32(0);
699 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
700
Simon Glass07e13382020-12-16 21:20:29 -0700701 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, &dev));
Fabien Dessenne22236e02019-05-31 15:11:30 +0200702 dma_addr[0] = cpu_to_be32(1);
703 dma_addr[1] = cpu_to_be32(0x100);
704 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
705
Mario Six35616ef2018-03-12 14:53:33 +0100706 return 0;
707}
Simon Glass974dccd2020-07-28 19:41:12 -0600708DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200709
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300710static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
711{
712 struct udevice *gpio, *dev;
713 void *ptr;
714
715 /* Test for missing reg property */
716 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
717 ut_assertnull(devfdt_get_addr_ptr(gpio));
718
Simon Glass07e13382020-12-16 21:20:29 -0700719 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Ovidiu Panait1b6773c2020-08-03 22:17:36 +0300720 ptr = devfdt_get_addr_ptr(dev);
721 ut_asserteq_ptr((void *)0x8000, ptr);
722
723 return 0;
724}
725DM_TEST(dm_test_fdt_get_addr_ptr_flat,
726 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
727
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200728static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
729{
730 struct udevice *dev;
731 fdt_addr_t addr;
732 void *paddr;
733
Simon Glass07e13382020-12-16 21:20:29 -0700734 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200735
736 addr = devfdt_get_addr(dev);
737 ut_asserteq(0x8000, addr);
738
739 paddr = map_physmem(addr, 0, MAP_NOCACHE);
740 ut_assertnonnull(paddr);
741 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
742
743 return 0;
744}
745DM_TEST(dm_test_fdt_remap_addr_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600746 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200747
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100748static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
749{
750 struct udevice *dev;
751 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530752 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100753 void *paddr;
754
Simon Glass07e13382020-12-16 21:20:29 -0700755 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100756
Sekhar Norif677c6f2019-08-01 19:12:56 +0530757 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100758 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530759 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100760
761 paddr = map_physmem(addr, 0, MAP_NOCACHE);
762 ut_assertnonnull(paddr);
763 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
764
765 return 0;
766}
767DM_TEST(dm_test_fdt_remap_addr_index_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600768 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100769
770static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
771{
772 struct udevice *dev;
773 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530774 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100775 void *paddr;
776
Simon Glass07e13382020-12-16 21:20:29 -0700777 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100778
Sekhar Norif677c6f2019-08-01 19:12:56 +0530779 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100780 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530781 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100782
783 paddr = map_physmem(addr, 0, MAP_NOCACHE);
784 ut_assertnonnull(paddr);
785 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
786
787 return 0;
788}
789DM_TEST(dm_test_fdt_remap_addr_name_flat,
Simon Glass974dccd2020-07-28 19:41:12 -0600790 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100791
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200792static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
793{
794 struct udevice *dev;
795 fdt_addr_t addr;
796 void *paddr;
797
Simon Glass07e13382020-12-16 21:20:29 -0700798 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojas670361292018-04-29 21:56:54 +0200799
800 addr = dev_read_addr(dev);
801 ut_asserteq(0x8000, addr);
802
803 paddr = map_physmem(addr, 0, MAP_NOCACHE);
804 ut_assertnonnull(paddr);
805 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
806
807 return 0;
808}
809DM_TEST(dm_test_fdt_remap_addr_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600810 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six058ef5b2018-06-26 08:46:49 +0200811
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100812static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
813{
814 struct udevice *dev;
815 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530816 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100817 void *paddr;
818
Simon Glass07e13382020-12-16 21:20:29 -0700819 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100820
Sekhar Norif677c6f2019-08-01 19:12:56 +0530821 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100822 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530823 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100824
825 paddr = map_physmem(addr, 0, MAP_NOCACHE);
826 ut_assertnonnull(paddr);
827 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
828
829 return 0;
830}
831DM_TEST(dm_test_fdt_remap_addr_index_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600832 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100833
834static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
835{
836 struct udevice *dev;
837 fdt_addr_t addr;
Sekhar Norif677c6f2019-08-01 19:12:56 +0530838 fdt_size_t size;
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100839 void *paddr;
840
Simon Glass07e13382020-12-16 21:20:29 -0700841 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, &dev));
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100842
Sekhar Norif677c6f2019-08-01 19:12:56 +0530843 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100844 ut_asserteq(0x8000, addr);
Sekhar Norif677c6f2019-08-01 19:12:56 +0530845 ut_asserteq(0x1000, size);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100846
847 paddr = map_physmem(addr, 0, MAP_NOCACHE);
848 ut_assertnonnull(paddr);
849 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
850
851 return 0;
852}
853DM_TEST(dm_test_fdt_remap_addr_name_live,
Simon Glass974dccd2020-07-28 19:41:12 -0600854 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojasa3181152018-12-03 19:37:09 +0100855
Mario Six058ef5b2018-06-26 08:46:49 +0200856static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
857{
858 struct udevice *dev;
859 ofnode node;
860
861 if (!of_live_active()) {
862 printf("Live tree not active; ignore test\n");
863 return 0;
864 }
865
866 /* Test enabling devices */
867
868 node = ofnode_path("/usb@2");
869
870 ut_assert(!of_device_is_available(ofnode_to_np(node)));
871 ofnode_set_enabled(node, true);
872 ut_assert(of_device_is_available(ofnode_to_np(node)));
873
874 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
875 &dev);
Simon Glass07e13382020-12-16 21:20:29 -0700876 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, &dev));
Mario Six058ef5b2018-06-26 08:46:49 +0200877
878 /* Test string property setting */
879
880 ut_assert(device_is_compatible(dev, "sandbox,usb"));
881 ofnode_write_string(node, "compatible", "gdsys,super-usb");
882 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
883 ofnode_write_string(node, "compatible", "sandbox,usb");
884 ut_assert(device_is_compatible(dev, "sandbox,usb"));
885
886 /* Test setting generic properties */
887
888 /* Non-existent in DTB */
889 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
890 /* reg = 0x42, size = 0x100 */
891 ut_assertok(ofnode_write_prop(node, "reg", 8,
892 "\x00\x00\x00\x42\x00\x00\x01\x00"));
893 ut_asserteq(0x42, dev_read_addr(dev));
894
895 /* Test disabling devices */
896
897 device_remove(dev, DM_REMOVE_NORMAL);
898 device_unbind(dev);
899
900 ut_assert(of_device_is_available(ofnode_to_np(node)));
901 ofnode_set_enabled(node, false);
902 ut_assert(!of_device_is_available(ofnode_to_np(node)));
903
904 return 0;
905}
Simon Glass974dccd2020-07-28 19:41:12 -0600906DM_TEST(dm_test_fdt_livetree_writing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Sixd6f6dc32018-06-26 08:46:51 +0200907
908static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
909{
910 ofnode node;
911
912 if (!of_live_active()) {
913 printf("Live tree not active; ignore test\n");
914 return 0;
915 }
916
917 node = ofnode_path("/usb@2");
918
919 /* Test enabling devices */
920
921 ut_assert(!of_device_is_available(ofnode_to_np(node)));
922 dev_enable_by_path("/usb@2");
923 ut_assert(of_device_is_available(ofnode_to_np(node)));
924
925 /* Test disabling devices */
926
927 ut_assert(of_device_is_available(ofnode_to_np(node)));
928 dev_disable_by_path("/usb@2");
929 ut_assert(!of_device_is_available(ofnode_to_np(node)));
930
931 return 0;
932}
Simon Glass974dccd2020-07-28 19:41:12 -0600933DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
934 UT_TESTF_SCAN_FDT);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700935
936/* Test a few uclass phandle functions */
937static int dm_test_fdt_phandle(struct unit_test_state *uts)
938{
939 struct udevice *back, *dev, *dev2;
940
941 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200942 ut_assertnonnull(back);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700943 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
944 back, "missing", &dev));
945 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
946 "power-supply", &dev));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200947 ut_assertnonnull(dev);
Simon Glassdc3efdc2018-11-18 08:14:30 -0700948 ut_asserteq(0, device_active(dev));
949 ut_asserteq_str("ldo1", dev->name);
950 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
951 "power-supply", &dev2));
952 ut_asserteq_ptr(dev, dev2);
953
954 return 0;
955}
Simon Glass974dccd2020-07-28 19:41:12 -0600956DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa11341a2018-11-18 08:14:31 -0700957
958/* Test device_find_first_child_by_uclass() */
959static int dm_test_first_child(struct unit_test_state *uts)
960{
961 struct udevice *i2c, *dev, *dev2;
962
963 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
964 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
965 ut_asserteq_str("rtc@43", dev->name);
966 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
967 ut_asserteq_ptr(dev, dev2);
968 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
969 ut_asserteq_str("rtc@61", dev2->name);
970
971 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
972 &dev));
973 ut_asserteq_str("eeprom@2c", dev->name);
974 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
975 ut_asserteq_ptr(dev, dev2);
976
977 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
978 UCLASS_VIDEO, &dev));
979 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
980
981 return 0;
982}
Simon Glass974dccd2020-07-28 19:41:12 -0600983DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass6df01f92018-12-10 10:37:37 -0700984
985/* Test integer functions in dm_read_...() */
986static int dm_test_read_int(struct unit_test_state *uts)
987{
988 struct udevice *dev;
989 u32 val32;
990 s32 sval;
991 uint val;
Dario Binacchi421e81e2020-03-29 18:04:40 +0200992 u64 val64;
Simon Glass6df01f92018-12-10 10:37:37 -0700993
994 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
995 ut_asserteq_str("a-test", dev->name);
996 ut_assertok(dev_read_u32(dev, "int-value", &val32));
997 ut_asserteq(1234, val32);
998
999 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
1000 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
1001
1002 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
1003 ut_asserteq(1234, val32);
1004
1005 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
1006 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
1007
1008 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
1009 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
1010 ut_asserteq(-1234, sval);
1011
1012 val = 0;
1013 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
1014 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
1015 ut_asserteq(-1234, val);
1016
Dario Binacchi421e81e2020-03-29 18:04:40 +02001017 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
1018 ut_asserteq_64(0x1111222233334444, val64);
1019
1020 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
1021 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
1022
1023 ut_asserteq_64(0x1111222233334444,
1024 dev_read_u64_default(dev, "int64-value", 6));
1025
Simon Glass6df01f92018-12-10 10:37:37 -07001026 return 0;
1027}
Simon Glass974dccd2020-07-28 19:41:12 -06001028DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass35cb2a42020-02-06 09:54:50 -07001029
Dario Binacchi81d80b52020-03-29 18:04:41 +02001030static int dm_test_read_int_index(struct unit_test_state *uts)
1031{
1032 struct udevice *dev;
1033 u32 val32;
1034
1035 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1036 ut_asserteq_str("a-test", dev->name);
1037
1038 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
1039 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
1040
1041 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
1042 ut_asserteq(5678, val32);
1043 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
1044 ut_asserteq(9123, val32);
1045 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
1046 ut_asserteq(4567, val32);
1047 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
1048 &val32));
1049
1050 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
1051 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
1052 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
1053 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
1054
1055 return 0;
1056}
Simon Glass974dccd2020-07-28 19:41:12 -06001057DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi81d80b52020-03-29 18:04:41 +02001058
Patrick Delaunay8cd28012020-09-25 09:41:16 +02001059static int dm_test_read_phandle(struct unit_test_state *uts)
1060{
1061 struct udevice *dev;
1062 struct ofnode_phandle_args args;
1063 int ret;
1064 const char prop[] = "test-gpios";
1065 const char cell[] = "#gpio-cells";
1066 const char prop2[] = "phandle-value";
1067
1068 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
1069 ut_asserteq_str("a-test", dev->name);
1070
1071 /* Test dev_count_phandle_with_args with cell name */
1072 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
1073 ut_asserteq(-ENOENT, ret);
1074 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
1075 ut_asserteq(-EINVAL, ret);
1076 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
1077
1078 /* Test dev_read_phandle_with_args with cell name */
1079 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
1080 ut_asserteq(-ENOENT, ret);
1081 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
1082 ut_asserteq(-EINVAL, ret);
1083 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
1084 ut_asserteq(1, args.args_count);
1085 ut_asserteq(1, args.args[0]);
1086 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
1087 ut_asserteq(1, args.args_count);
1088 ut_asserteq(4, args.args[0]);
1089 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
1090 ut_asserteq(5, args.args_count);
1091 ut_asserteq(5, args.args[0]);
1092 ut_asserteq(1, args.args[4]);
1093 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
1094 ut_asserteq(-ENOENT, ret);
1095 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
1096 ut_asserteq(1, args.args_count);
1097 ut_asserteq(12, args.args[0]);
1098 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
1099 ut_asserteq(-ENOENT, ret);
1100
1101 /* Test dev_count_phandle_with_args with cell count */
1102 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
1103 ut_asserteq(-ENOENT, ret);
1104 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
1105
1106 /* Test dev_read_phandle_with_args with cell count */
1107 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
1108 ut_asserteq(1, ofnode_valid(args.node));
1109 ut_asserteq(1, args.args_count);
1110 ut_asserteq(10, args.args[0]);
1111 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
1112 ut_asserteq(-EINVAL, ret);
1113 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
1114 ut_asserteq(1, ofnode_valid(args.node));
1115 ut_asserteq(1, args.args_count);
1116 ut_asserteq(30, args.args[0]);
1117 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
1118 ut_asserteq(-ENOENT, ret);
1119
1120 return 0;
1121}
1122DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1123
Simon Glass35cb2a42020-02-06 09:54:50 -07001124/* Test iteration through devices by drvdata */
1125static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1126{
1127 struct udevice *dev;
1128
1129 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1130 DM_TEST_TYPE_FIRST, &dev));
1131 ut_asserteq_str("a-test", dev->name);
1132
1133 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1134 DM_TEST_TYPE_SECOND, &dev));
1135 ut_asserteq_str("d-test", dev->name);
1136
1137 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1138 DM_TEST_TYPE_COUNT,
1139 &dev));
1140
1141 return 0;
1142}
Simon Glass974dccd2020-07-28 19:41:12 -06001143DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Tom Rinib7b18382020-02-11 10:58:41 -05001144
Simon Glass000676b2020-01-27 08:49:47 -07001145/* Test device_first_child_ofdata_err(), etc. */
1146static int dm_test_child_ofdata(struct unit_test_state *uts)
1147{
1148 struct udevice *bus, *dev;
1149 int count;
1150
1151 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1152 count = 0;
Simon Glassaad29ae2020-12-03 16:55:21 -07001153 device_foreach_child_of_to_plat(dev, bus) {
Simon Glass000676b2020-01-27 08:49:47 -07001154 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1155 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1156 count++;
1157 }
1158 ut_asserteq(3, count);
1159
1160 return 0;
1161}
Simon Glass974dccd2020-07-28 19:41:12 -06001162DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass76db9b72020-01-27 08:49:48 -07001163
1164/* Test device_first_child_err(), etc. */
1165static int dm_test_first_child_probe(struct unit_test_state *uts)
1166{
1167 struct udevice *bus, *dev;
1168 int count;
1169
1170 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1171 count = 0;
1172 device_foreach_child_probe(dev, bus) {
1173 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1174 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
1175 count++;
1176 }
1177 ut_asserteq(3, count);
1178
1179 return 0;
1180}
Simon Glass974dccd2020-07-28 19:41:12 -06001181DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass44c47892020-04-05 15:38:19 -06001182
1183/* Test that ofdata is read for parents before children */
1184static int dm_test_ofdata_order(struct unit_test_state *uts)
1185{
1186 struct udevice *bus, *dev;
1187
1188 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1189 ut_assertnonnull(bus);
1190 ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID));
1191
1192 ut_assertok(device_find_first_child(bus, &dev));
1193 ut_assertnonnull(dev);
1194 ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID));
1195
1196 /* read the child's ofdata which should cause the parent's to be read */
Simon Glassaad29ae2020-12-03 16:55:21 -07001197 ut_assertok(device_of_to_plat(dev));
Simon Glass44c47892020-04-05 15:38:19 -06001198 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1199 ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID);
1200
1201 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1202 ut_assert(!(bus->flags & DM_FLAG_ACTIVATED));
1203
1204 return 0;
1205}
Simon Glass974dccd2020-07-28 19:41:12 -06001206DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);