blob: cc12419ea0f39f396d944109ed66dcdb5de1f0d8 [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{
28 const struct dm_test_pdata *pdata = dev->platdata;
29 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
Heiko Schocher54c5d082014-05-22 12:43:05 +020041static int testfdt_ofdata_to_platdata(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070042{
43 struct dm_test_pdata *pdata = dev_get_platdata(dev);
44
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,
86 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
87 .probe = testfdt_drv_probe,
88 .ops = &test_ops,
89 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
90 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
91};
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,
104 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
105 .probe = testfdt_drv_probe,
106 .ops = &test_ops,
107 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
108 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
109 .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 Glass98561572017-04-23 20:10:44 -0600129struct dm_testprobe_pdata {
130 int probe_err;
131};
132
133static int testprobe_drv_probe(struct udevice *dev)
134{
135 struct dm_testprobe_pdata *pdata = dev_get_platdata(dev);
136
137 return pdata->probe_err;
138}
139
140static const struct udevice_id testprobe_ids[] = {
141 { .compatible = "denx,u-boot-probe-test" },
142 { }
143};
144
145U_BOOT_DRIVER(testprobe_drv) = {
146 .name = "testprobe_drv",
147 .of_match = testprobe_ids,
148 .id = UCLASS_TEST_PROBE,
149 .probe = testprobe_drv_probe,
150 .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata),
151};
152
153UCLASS_DRIVER(testprobe) = {
154 .name = "testprobe",
155 .id = UCLASS_TEST_PROBE,
156 .flags = DM_UC_FLAG_SEQ_ALIAS,
157};
158
Simon Glassdc12ebb2019-12-29 21:19:25 -0700159struct dm_testdevres_pdata {
160 void *ptr;
161};
162
163struct dm_testdevres_priv {
164 void *ptr;
Simon Glass42a0ce52019-12-29 21:19:28 -0700165 void *ptr_ofdata;
Simon Glassdc12ebb2019-12-29 21:19:25 -0700166};
167
168static int testdevres_drv_bind(struct udevice *dev)
169{
170 struct dm_testdevres_pdata *pdata = dev_get_platdata(dev);
171
172 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
173
174 return 0;
175}
176
Simon Glass42a0ce52019-12-29 21:19:28 -0700177static int testdevres_drv_ofdata_to_platdata(struct udevice *dev)
178{
179 struct dm_testdevres_priv *priv = dev_get_priv(dev);
180
181 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
182
183 return 0;
184}
185
Simon Glassdc12ebb2019-12-29 21:19:25 -0700186static int testdevres_drv_probe(struct udevice *dev)
187{
188 struct dm_testdevres_priv *priv = dev_get_priv(dev);
189
190 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
191
192 return 0;
193}
194
195static const struct udevice_id testdevres_ids[] = {
196 { .compatible = "denx,u-boot-devres-test" },
197 { }
198};
199
200U_BOOT_DRIVER(testdevres_drv) = {
201 .name = "testdevres_drv",
202 .of_match = testdevres_ids,
203 .id = UCLASS_TEST_DEVRES,
204 .bind = testdevres_drv_bind,
Simon Glass42a0ce52019-12-29 21:19:28 -0700205 .ofdata_to_platdata = testdevres_drv_ofdata_to_platdata,
Simon Glassdc12ebb2019-12-29 21:19:25 -0700206 .probe = testdevres_drv_probe,
207 .platdata_auto_alloc_size = sizeof(struct dm_testdevres_pdata),
208 .priv_auto_alloc_size = sizeof(struct dm_testdevres_priv),
209};
210
211UCLASS_DRIVER(testdevres) = {
212 .name = "testdevres",
213 .id = UCLASS_TEST_DEVRES,
214 .flags = DM_UC_FLAG_SEQ_ALIAS,
215};
216
Joe Hershbergere721b882015-05-20 14:27:27 -0500217int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700218{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200219 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700220 int ret;
221 int i;
222
Simon Glass2e7d35d2014-02-26 15:59:21 -0700223 /*
224 * Now check that the ping adds are what we expect. This is using the
225 * ping-add property in each node.
226 */
Simon Glass1ca7e202014-07-23 06:55:18 -0600227 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-02-26 15:59:21 -0700228 uint32_t base;
229
230 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
231 ut_assert(!ret);
232
233 /*
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600234 * Get the 'ping-expect' property, which tells us what the
235 * ping add should be. We don't use the platdata because we
236 * want to test the code that sets that up
237 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700238 */
Simon Glasse160f7d2017-01-17 16:52:55 -0700239 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600240 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700241 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glasse160f7d2017-01-17 16:52:55 -0700242 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glass2e7d35d2014-02-26 15:59:21 -0700243
Joe Hershbergere721b882015-05-20 14:27:27 -0500244 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700245 dev_get_priv(dev)));
246 }
247
248 return 0;
249}
Simon Glass1ca7e202014-07-23 06:55:18 -0600250
251/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500252static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600253{
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530254 const int num_devices = 9;
Simon Glass1ca7e202014-07-23 06:55:18 -0600255 struct udevice *dev;
256 struct uclass *uc;
257 int ret;
258 int i;
259
Patrick Delaunay0544ecb2020-02-18 15:43:46 +0100260 ret = dm_extended_scan_fdt(gd->fdt_blob, false);
Simon Glass1ca7e202014-07-23 06:55:18 -0600261 ut_assert(!ret);
262
263 ret = uclass_get(UCLASS_TEST_FDT, &uc);
264 ut_assert(!ret);
265
266 /* These are num_devices compatible root-level device tree nodes */
267 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
268
Simon Glassf8a85442015-01-25 08:27:00 -0700269 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-07-23 06:55:18 -0600270 for (i = 0; i < num_devices; i++) {
271 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
272 ut_assert(!ret);
273 ut_assert(!dev_get_priv(dev));
Simon Glassf8a85442015-01-25 08:27:00 -0700274 ut_assert(dev->platdata);
Simon Glass1ca7e202014-07-23 06:55:18 -0600275 }
276
Joe Hershbergere721b882015-05-20 14:27:27 -0500277 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600278
279 return 0;
280}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700281DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600282
Michal Simeka93eb572019-01-31 16:31:00 +0100283static int dm_test_alias_highest_id(struct unit_test_state *uts)
284{
285 int ret;
286
287 ret = dev_read_alias_highest_id("eth");
288 ut_asserteq(5, ret);
289
290 ret = dev_read_alias_highest_id("gpio");
Patrick Delaunayff526652020-01-13 11:35:14 +0100291 ut_asserteq(3, ret);
Michal Simeka93eb572019-01-31 16:31:00 +0100292
293 ret = dev_read_alias_highest_id("pci");
294 ut_asserteq(2, ret);
295
296 ret = dev_read_alias_highest_id("i2c");
297 ut_asserteq(0, ret);
298
299 ret = dev_read_alias_highest_id("deadbeef");
300 ut_asserteq(-1, ret);
301
302 return 0;
303}
304DM_TEST(dm_test_alias_highest_id, 0);
305
Joe Hershbergere721b882015-05-20 14:27:27 -0500306static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-07-23 06:55:03 -0600307{
308 struct uclass *uc;
309 int ret;
310
311 ret = dm_scan_fdt(gd->fdt_blob, true);
312 ut_assert(!ret);
313
314 ret = uclass_get(UCLASS_TEST_FDT, &uc);
315 ut_assert(!ret);
316
Bin Meng2786cd72018-10-10 22:07:01 -0700317 /*
318 * These are 2 pre-reloc devices:
319 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
320 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
321 */
322 ut_asserteq(2, list_count_items(&uc->dev_head));
Simon Glass00606d72014-07-23 06:55:03 -0600323
324 return 0;
325}
326DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600327
328/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500329static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600330{
331 struct udevice *dev;
332
333 /* A few basic santiy tests */
334 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
335 ut_asserteq_str("b-test", dev->name);
336
Simon Glass9cc36a22015-01-25 08:27:05 -0700337 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600338 ut_asserteq_str("a-test", dev->name);
339
340 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
341 true, &dev));
342 ut_asserteq_ptr(NULL, dev);
343
344 /* Test aliases */
345 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
346 ut_asserteq_str("e-test", dev->name);
347
348 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
349 true, &dev));
350
Simon Glass1ca7e202014-07-23 06:55:18 -0600351 /*
352 * Note that c-test nodes are not probed since it is not a top-level
353 * node
354 */
Simon Glass5a66a8f2014-07-23 06:55:12 -0600355 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
356 ut_asserteq_str("b-test", dev->name);
357
358 /*
359 * d-test wants sequence number 3 also, but it can't have it because
360 * b-test gets it first.
361 */
362 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
363 ut_asserteq_str("d-test", dev->name);
364
Michael Wallebe1a6e92020-06-02 01:47:09 +0200365 /*
366 * d-test actually gets 9, because thats the next free one after the
367 * aliases.
368 */
369 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 9, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600370 ut_asserteq_str("d-test", dev->name);
371
Michael Wallebe1a6e92020-06-02 01:47:09 +0200372 /* initially no one wants seq 10 */
373 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 10,
Simon Glass5a66a8f2014-07-23 06:55:12 -0600374 &dev));
375 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
Simon Glass9cc36a22015-01-25 08:27:05 -0700376 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600377
378 /* But now that it is probed, we can find it */
Michael Wallebe1a6e92020-06-02 01:47:09 +0200379 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 10, &dev));
Simon Glass9cc36a22015-01-25 08:27:05 -0700380 ut_asserteq_str("f-test", dev->name);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600381
Michael Wallebe1a6e92020-06-02 01:47:09 +0200382 /*
383 * And we should still have holes in our sequence numbers, that is 2
384 * and 4 should not be used.
385 */
386 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
387 true, &dev));
388 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
389 true, &dev));
390
Simon Glass5a66a8f2014-07-23 06:55:12 -0600391 return 0;
392}
Simon Glasse180c2b2020-07-28 19:41:12 -0600393DM_TEST(dm_test_fdt_uclass_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600394
395/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500396static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-07-23 06:55:14 -0600397{
398 const void *blob = gd->fdt_blob;
399 struct udevice *dev;
400 int node;
401
402 node = fdt_path_offset(blob, "/e-test");
403 ut_assert(node > 0);
404 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
405 &dev));
406 ut_asserteq_str("e-test", dev->name);
407
408 /* This node should not be bound */
409 node = fdt_path_offset(blob, "/junk");
410 ut_assert(node > 0);
411 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
412 node, &dev));
413
414 /* This is not a top level node so should not be probed */
Simon Glass1ca7e202014-07-23 06:55:18 -0600415 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-07-23 06:55:14 -0600416 ut_assert(node > 0);
417 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
418 node, &dev));
419
420 return 0;
421}
Simon Glass86b54ec2017-05-18 20:09:44 -0600422DM_TEST(dm_test_fdt_offset,
Simon Glasse180c2b2020-07-28 19:41:12 -0600423 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Simon Glass98561572017-04-23 20:10:44 -0600424
425/**
426 * Test various error conditions with uclass_first_device() and
427 * uclass_next_device()
428 */
429static int dm_test_first_next_device(struct unit_test_state *uts)
430{
431 struct dm_testprobe_pdata *pdata;
432 struct udevice *dev, *parent = NULL;
433 int count;
434 int ret;
435
436 /* There should be 4 devices */
437 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
438 dev;
439 ret = uclass_next_device(&dev)) {
440 count++;
441 parent = dev_get_parent(dev);
442 }
443 ut_assertok(ret);
444 ut_asserteq(4, count);
445
446 /* Remove them and try again, with an error on the second one */
447 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
448 pdata = dev_get_platdata(dev);
449 pdata->probe_err = -ENOMEM;
450 device_remove(parent, DM_REMOVE_NORMAL);
451 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
452 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
453 ut_asserteq_ptr(dev, NULL);
454
455 /* Now an error on the first one */
456 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
457 pdata = dev_get_platdata(dev);
458 pdata->probe_err = -ENOENT;
459 device_remove(parent, DM_REMOVE_NORMAL);
460 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
461
462 return 0;
463}
Simon Glasse180c2b2020-07-28 19:41:12 -0600464DM_TEST(dm_test_first_next_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass95ce3852017-04-23 20:10:45 -0600465
Simon Glass3cf0fba2020-02-06 09:54:49 -0700466/* Test iteration through devices in a uclass */
467static int dm_test_uclass_foreach(struct unit_test_state *uts)
468{
469 struct udevice *dev;
470 struct uclass *uc;
471 int count;
472
473 count = 0;
474 uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
475 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530476 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700477
478 count = 0;
479 uclass_foreach_dev(dev, uc)
480 count++;
Jean-Jacques Hiblot88e6a602020-09-11 13:43:35 +0530481 ut_asserteq(9, count);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700482
483 return 0;
484}
Simon Glasse180c2b2020-07-28 19:41:12 -0600485DM_TEST(dm_test_uclass_foreach, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3cf0fba2020-02-06 09:54:49 -0700486
Simon Glass95ce3852017-04-23 20:10:45 -0600487/**
488 * check_devices() - Check return values and pointers
489 *
490 * This runs through a full sequence of uclass_first_device_check()...
491 * uclass_next_device_check() checking that the return values and devices
492 * are correct.
493 *
494 * @uts: Test state
495 * @devlist: List of expected devices
496 * @mask: Indicates which devices should return an error. Device n should
497 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
498 * bit n is clear.
499 */
500static int check_devices(struct unit_test_state *uts,
501 struct udevice *devlist[], int mask)
502{
503 int expected_ret;
504 struct udevice *dev;
505 int i;
506
507 expected_ret = (mask & 1) ? -ENOENT : 0;
508 mask >>= 1;
509 ut_asserteq(expected_ret,
510 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
511 for (i = 0; i < 4; i++) {
512 ut_asserteq_ptr(devlist[i], dev);
513 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
514 mask >>= 1;
515 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
516 }
517 ut_asserteq_ptr(NULL, dev);
518
519 return 0;
520}
521
522/* Test uclass_first_device_check() and uclass_next_device_check() */
523static int dm_test_first_next_ok_device(struct unit_test_state *uts)
524{
525 struct dm_testprobe_pdata *pdata;
526 struct udevice *dev, *parent = NULL, *devlist[4];
527 int count;
528 int ret;
529
530 /* There should be 4 devices */
531 count = 0;
532 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
533 dev;
534 ret = uclass_next_device_check(&dev)) {
535 ut_assertok(ret);
536 devlist[count++] = dev;
537 parent = dev_get_parent(dev);
538 }
539 ut_asserteq(4, count);
540 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
541 ut_assertok(check_devices(uts, devlist, 0));
542
543 /* Remove them and try again, with an error on the second one */
544 pdata = dev_get_platdata(devlist[1]);
545 pdata->probe_err = -ENOENT - 1;
546 device_remove(parent, DM_REMOVE_NORMAL);
547 ut_assertok(check_devices(uts, devlist, 1 << 1));
548
549 /* Now an error on the first one */
550 pdata = dev_get_platdata(devlist[0]);
551 pdata->probe_err = -ENOENT - 0;
552 device_remove(parent, DM_REMOVE_NORMAL);
553 ut_assertok(check_devices(uts, devlist, 3 << 0));
554
555 /* Now errors on all */
556 pdata = dev_get_platdata(devlist[2]);
557 pdata->probe_err = -ENOENT - 2;
558 pdata = dev_get_platdata(devlist[3]);
559 pdata->probe_err = -ENOENT - 3;
560 device_remove(parent, DM_REMOVE_NORMAL);
561 ut_assertok(check_devices(uts, devlist, 0xf << 0));
562
563 return 0;
564}
Simon Glasse180c2b2020-07-28 19:41:12 -0600565DM_TEST(dm_test_first_next_ok_device, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Sixe8d52912018-03-12 14:53:33 +0100566
567static const struct udevice_id fdt_dummy_ids[] = {
568 { .compatible = "denx,u-boot-fdt-dummy", },
569 { }
570};
571
572UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca507cef32018-05-19 14:13:55 +0200573 .name = "fdt-dummy",
Mario Sixe8d52912018-03-12 14:53:33 +0100574 .id = UCLASS_TEST_DUMMY,
575 .flags = DM_UC_FLAG_SEQ_ALIAS,
576};
577
578U_BOOT_DRIVER(fdt_dummy_drv) = {
579 .name = "fdt_dummy_drv",
580 .of_match = fdt_dummy_ids,
581 .id = UCLASS_TEST_DUMMY,
582};
583
584static int dm_test_fdt_translation(struct unit_test_state *uts)
585{
586 struct udevice *dev;
Fabien Dessenne641067f2019-05-31 15:11:30 +0200587 fdt32_t dma_addr[2];
Mario Sixe8d52912018-03-12 14:53:33 +0100588
589 /* Some simple translations */
590 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
591 ut_asserteq_str("dev@0,0", dev->name);
592 ut_asserteq(0x8000, dev_read_addr(dev));
593
594 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
595 ut_asserteq_str("dev@1,100", dev->name);
596 ut_asserteq(0x9000, dev_read_addr(dev));
597
598 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev));
599 ut_asserteq_str("dev@2,200", dev->name);
600 ut_asserteq(0xA000, dev_read_addr(dev));
601
602 /* No translation for busses with #size-cells == 0 */
603 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev));
604 ut_asserteq_str("dev@42", dev->name);
605 ut_asserteq(0x42, dev_read_addr(dev));
606
Fabien Dessenne641067f2019-05-31 15:11:30 +0200607 /* dma address translation */
608 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
609 dma_addr[0] = cpu_to_be32(0);
610 dma_addr[1] = cpu_to_be32(0);
611 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
612
613 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
614 dma_addr[0] = cpu_to_be32(1);
615 dma_addr[1] = cpu_to_be32(0x100);
616 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
617
Mario Sixe8d52912018-03-12 14:53:33 +0100618 return 0;
619}
Simon Glasse180c2b2020-07-28 19:41:12 -0600620DM_TEST(dm_test_fdt_translation, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200621
Ovidiu Panait6e648302020-08-03 22:17:36 +0300622static int dm_test_fdt_get_addr_ptr_flat(struct unit_test_state *uts)
623{
624 struct udevice *gpio, *dev;
625 void *ptr;
626
627 /* Test for missing reg property */
628 ut_assertok(uclass_first_device_err(UCLASS_GPIO, &gpio));
629 ut_assertnull(devfdt_get_addr_ptr(gpio));
630
631 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
632 ptr = devfdt_get_addr_ptr(dev);
633 ut_asserteq_ptr((void *)0x8000, ptr);
634
635 return 0;
636}
637DM_TEST(dm_test_fdt_get_addr_ptr_flat,
638 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
639
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200640static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
641{
642 struct udevice *dev;
643 fdt_addr_t addr;
644 void *paddr;
645
646 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
647
648 addr = devfdt_get_addr(dev);
649 ut_asserteq(0x8000, addr);
650
651 paddr = map_physmem(addr, 0, MAP_NOCACHE);
652 ut_assertnonnull(paddr);
653 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
654
655 return 0;
656}
657DM_TEST(dm_test_fdt_remap_addr_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600658 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200659
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100660static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
661{
662 struct udevice *dev;
663 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530664 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100665 void *paddr;
666
667 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
668
Sekhar Norif5b90472019-08-01 19:12:56 +0530669 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100670 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530671 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100672
673 paddr = map_physmem(addr, 0, MAP_NOCACHE);
674 ut_assertnonnull(paddr);
675 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
676
677 return 0;
678}
679DM_TEST(dm_test_fdt_remap_addr_index_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600680 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100681
682static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
683{
684 struct udevice *dev;
685 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530686 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100687 void *paddr;
688
689 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
690
Sekhar Norif5b90472019-08-01 19:12:56 +0530691 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100692 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530693 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100694
695 paddr = map_physmem(addr, 0, MAP_NOCACHE);
696 ut_assertnonnull(paddr);
697 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
698
699 return 0;
700}
701DM_TEST(dm_test_fdt_remap_addr_name_flat,
Simon Glasse180c2b2020-07-28 19:41:12 -0600702 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_FLAT_TREE);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100703
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200704static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
705{
706 struct udevice *dev;
707 fdt_addr_t addr;
708 void *paddr;
709
710 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
711
712 addr = dev_read_addr(dev);
713 ut_asserteq(0x8000, addr);
714
715 paddr = map_physmem(addr, 0, MAP_NOCACHE);
716 ut_assertnonnull(paddr);
717 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
718
719 return 0;
720}
721DM_TEST(dm_test_fdt_remap_addr_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600722 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six2ea4d0d2018-06-26 08:46:49 +0200723
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100724static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
725{
726 struct udevice *dev;
727 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530728 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100729 void *paddr;
730
731 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
732
Sekhar Norif5b90472019-08-01 19:12:56 +0530733 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100734 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530735 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100736
737 paddr = map_physmem(addr, 0, MAP_NOCACHE);
738 ut_assertnonnull(paddr);
739 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
740
741 return 0;
742}
743DM_TEST(dm_test_fdt_remap_addr_index_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600744 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100745
746static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
747{
748 struct udevice *dev;
749 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530750 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100751 void *paddr;
752
753 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
754
Sekhar Norif5b90472019-08-01 19:12:56 +0530755 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100756 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530757 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100758
759 paddr = map_physmem(addr, 0, MAP_NOCACHE);
760 ut_assertnonnull(paddr);
761 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
762
763 return 0;
764}
765DM_TEST(dm_test_fdt_remap_addr_name_live,
Simon Glasse180c2b2020-07-28 19:41:12 -0600766 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100767
Mario Six2ea4d0d2018-06-26 08:46:49 +0200768static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
769{
770 struct udevice *dev;
771 ofnode node;
772
773 if (!of_live_active()) {
774 printf("Live tree not active; ignore test\n");
775 return 0;
776 }
777
778 /* Test enabling devices */
779
780 node = ofnode_path("/usb@2");
781
782 ut_assert(!of_device_is_available(ofnode_to_np(node)));
783 ofnode_set_enabled(node, true);
784 ut_assert(of_device_is_available(ofnode_to_np(node)));
785
786 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
787 &dev);
788 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
789
790 /* Test string property setting */
791
792 ut_assert(device_is_compatible(dev, "sandbox,usb"));
793 ofnode_write_string(node, "compatible", "gdsys,super-usb");
794 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
795 ofnode_write_string(node, "compatible", "sandbox,usb");
796 ut_assert(device_is_compatible(dev, "sandbox,usb"));
797
798 /* Test setting generic properties */
799
800 /* Non-existent in DTB */
801 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
802 /* reg = 0x42, size = 0x100 */
803 ut_assertok(ofnode_write_prop(node, "reg", 8,
804 "\x00\x00\x00\x42\x00\x00\x01\x00"));
805 ut_asserteq(0x42, dev_read_addr(dev));
806
807 /* Test disabling devices */
808
809 device_remove(dev, DM_REMOVE_NORMAL);
810 device_unbind(dev);
811
812 ut_assert(of_device_is_available(ofnode_to_np(node)));
813 ofnode_set_enabled(node, false);
814 ut_assert(!of_device_is_available(ofnode_to_np(node)));
815
816 return 0;
817}
Simon Glasse180c2b2020-07-28 19:41:12 -0600818DM_TEST(dm_test_fdt_livetree_writing, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Mario Six172942a2018-06-26 08:46:51 +0200819
820static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
821{
822 ofnode node;
823
824 if (!of_live_active()) {
825 printf("Live tree not active; ignore test\n");
826 return 0;
827 }
828
829 node = ofnode_path("/usb@2");
830
831 /* Test enabling devices */
832
833 ut_assert(!of_device_is_available(ofnode_to_np(node)));
834 dev_enable_by_path("/usb@2");
835 ut_assert(of_device_is_available(ofnode_to_np(node)));
836
837 /* Test disabling devices */
838
839 ut_assert(of_device_is_available(ofnode_to_np(node)));
840 dev_disable_by_path("/usb@2");
841 ut_assert(!of_device_is_available(ofnode_to_np(node)));
842
843 return 0;
844}
Simon Glasse180c2b2020-07-28 19:41:12 -0600845DM_TEST(dm_test_fdt_disable_enable_by_path, UT_TESTF_SCAN_PDATA |
846 UT_TESTF_SCAN_FDT);
Simon Glassd0b4f682018-11-18 08:14:30 -0700847
848/* Test a few uclass phandle functions */
849static int dm_test_fdt_phandle(struct unit_test_state *uts)
850{
851 struct udevice *back, *dev, *dev2;
852
853 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200854 ut_assertnonnull(back);
Simon Glassd0b4f682018-11-18 08:14:30 -0700855 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
856 back, "missing", &dev));
857 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
858 "power-supply", &dev));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200859 ut_assertnonnull(dev);
Simon Glassd0b4f682018-11-18 08:14:30 -0700860 ut_asserteq(0, device_active(dev));
861 ut_asserteq_str("ldo1", dev->name);
862 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
863 "power-supply", &dev2));
864 ut_asserteq_ptr(dev, dev2);
865
866 return 0;
867}
Simon Glasse180c2b2020-07-28 19:41:12 -0600868DM_TEST(dm_test_fdt_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass3abe1112018-11-18 08:14:31 -0700869
870/* Test device_find_first_child_by_uclass() */
871static int dm_test_first_child(struct unit_test_state *uts)
872{
873 struct udevice *i2c, *dev, *dev2;
874
875 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
876 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
877 ut_asserteq_str("rtc@43", dev->name);
878 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
879 ut_asserteq_ptr(dev, dev2);
880 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
881 ut_asserteq_str("rtc@61", dev2->name);
882
883 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
884 &dev));
885 ut_asserteq_str("eeprom@2c", dev->name);
886 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
887 ut_asserteq_ptr(dev, dev2);
888
889 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
890 UCLASS_VIDEO, &dev));
891 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
892
893 return 0;
894}
Simon Glasse180c2b2020-07-28 19:41:12 -0600895DM_TEST(dm_test_first_child, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa1b17e42018-12-10 10:37:37 -0700896
897/* Test integer functions in dm_read_...() */
898static int dm_test_read_int(struct unit_test_state *uts)
899{
900 struct udevice *dev;
901 u32 val32;
902 s32 sval;
903 uint val;
Dario Binacchi70573c62020-03-29 18:04:40 +0200904 u64 val64;
Simon Glassa1b17e42018-12-10 10:37:37 -0700905
906 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
907 ut_asserteq_str("a-test", dev->name);
908 ut_assertok(dev_read_u32(dev, "int-value", &val32));
909 ut_asserteq(1234, val32);
910
911 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
912 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
913
914 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
915 ut_asserteq(1234, val32);
916
917 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
918 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
919
920 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
921 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
922 ut_asserteq(-1234, sval);
923
924 val = 0;
925 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
926 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
927 ut_asserteq(-1234, val);
928
Dario Binacchi70573c62020-03-29 18:04:40 +0200929 ut_assertok(dev_read_u64(dev, "int64-value", &val64));
930 ut_asserteq_64(0x1111222233334444, val64);
931
932 ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
933 ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
934
935 ut_asserteq_64(0x1111222233334444,
936 dev_read_u64_default(dev, "int64-value", 6));
937
Simon Glassa1b17e42018-12-10 10:37:37 -0700938 return 0;
939}
Simon Glasse180c2b2020-07-28 19:41:12 -0600940DM_TEST(dm_test_read_int, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass50162342020-02-06 09:54:50 -0700941
Dario Binacchi4bb70752020-03-29 18:04:41 +0200942static int dm_test_read_int_index(struct unit_test_state *uts)
943{
944 struct udevice *dev;
945 u32 val32;
946
947 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
948 ut_asserteq_str("a-test", dev->name);
949
950 ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
951 ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
952
953 ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
954 ut_asserteq(5678, val32);
955 ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
956 ut_asserteq(9123, val32);
957 ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
958 ut_asserteq(4567, val32);
959 ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
960 &val32));
961
962 ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
963 ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
964 ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
965 ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
966
967 return 0;
968}
Simon Glasse180c2b2020-07-28 19:41:12 -0600969DM_TEST(dm_test_read_int_index, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Dario Binacchi4bb70752020-03-29 18:04:41 +0200970
Patrick Delaunaycc72f3e2020-09-25 09:41:16 +0200971static int dm_test_read_phandle(struct unit_test_state *uts)
972{
973 struct udevice *dev;
974 struct ofnode_phandle_args args;
975 int ret;
976 const char prop[] = "test-gpios";
977 const char cell[] = "#gpio-cells";
978 const char prop2[] = "phandle-value";
979
980 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
981 ut_asserteq_str("a-test", dev->name);
982
983 /* Test dev_count_phandle_with_args with cell name */
984 ret = dev_count_phandle_with_args(dev, "missing", cell, 0);
985 ut_asserteq(-ENOENT, ret);
986 ret = dev_count_phandle_with_args(dev, prop, "#invalid", 0);
987 ut_asserteq(-EINVAL, ret);
988 ut_asserteq(5, dev_count_phandle_with_args(dev, prop, cell, 0));
989
990 /* Test dev_read_phandle_with_args with cell name */
991 ret = dev_read_phandle_with_args(dev, "missing", cell, 0, 0, &args);
992 ut_asserteq(-ENOENT, ret);
993 ret = dev_read_phandle_with_args(dev, prop, "#invalid", 0, 0, &args);
994 ut_asserteq(-EINVAL, ret);
995 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 0, &args));
996 ut_asserteq(1, args.args_count);
997 ut_asserteq(1, args.args[0]);
998 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 1, &args));
999 ut_asserteq(1, args.args_count);
1000 ut_asserteq(4, args.args[0]);
1001 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 2, &args));
1002 ut_asserteq(5, args.args_count);
1003 ut_asserteq(5, args.args[0]);
1004 ut_asserteq(1, args.args[4]);
1005 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 3, &args);
1006 ut_asserteq(-ENOENT, ret);
1007 ut_assertok(dev_read_phandle_with_args(dev, prop, cell, 0, 4, &args));
1008 ut_asserteq(1, args.args_count);
1009 ut_asserteq(12, args.args[0]);
1010 ret = dev_read_phandle_with_args(dev, prop, cell, 0, 5, &args);
1011 ut_asserteq(-ENOENT, ret);
1012
1013 /* Test dev_count_phandle_with_args with cell count */
1014 ret = dev_count_phandle_with_args(dev, "missing", NULL, 2);
1015 ut_asserteq(-ENOENT, ret);
1016 ut_asserteq(3, dev_count_phandle_with_args(dev, prop2, NULL, 1));
1017
1018 /* Test dev_read_phandle_with_args with cell count */
1019 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 0, &args));
1020 ut_asserteq(1, ofnode_valid(args.node));
1021 ut_asserteq(1, args.args_count);
1022 ut_asserteq(10, args.args[0]);
1023 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 1, &args);
1024 ut_asserteq(-EINVAL, ret);
1025 ut_assertok(dev_read_phandle_with_args(dev, prop2, NULL, 1, 2, &args));
1026 ut_asserteq(1, ofnode_valid(args.node));
1027 ut_asserteq(1, args.args_count);
1028 ut_asserteq(30, args.args[0]);
1029 ret = dev_read_phandle_with_args(dev, prop2, NULL, 1, 3, &args);
1030 ut_asserteq(-ENOENT, ret);
1031
1032 return 0;
1033}
1034DM_TEST(dm_test_read_phandle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
1035
Simon Glass50162342020-02-06 09:54:50 -07001036/* Test iteration through devices by drvdata */
1037static int dm_test_uclass_drvdata(struct unit_test_state *uts)
1038{
1039 struct udevice *dev;
1040
1041 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1042 DM_TEST_TYPE_FIRST, &dev));
1043 ut_asserteq_str("a-test", dev->name);
1044
1045 ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
1046 DM_TEST_TYPE_SECOND, &dev));
1047 ut_asserteq_str("d-test", dev->name);
1048
1049 ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
1050 DM_TEST_TYPE_COUNT,
1051 &dev));
1052
1053 return 0;
1054}
Simon Glasse180c2b2020-07-28 19:41:12 -06001055DM_TEST(dm_test_uclass_drvdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Tom Rini9a8942b2020-02-11 10:58:41 -05001056
Simon Glassf262d4c2020-01-27 08:49:47 -07001057/* Test device_first_child_ofdata_err(), etc. */
1058static int dm_test_child_ofdata(struct unit_test_state *uts)
1059{
1060 struct udevice *bus, *dev;
1061 int count;
1062
1063 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1064 count = 0;
1065 device_foreach_child_ofdata_to_platdata(dev, bus) {
1066 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1067 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1068 count++;
1069 }
1070 ut_asserteq(3, count);
1071
1072 return 0;
1073}
Simon Glasse180c2b2020-07-28 19:41:12 -06001074DM_TEST(dm_test_child_ofdata, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass903e83e2020-01-27 08:49:48 -07001075
1076/* Test device_first_child_err(), etc. */
1077static int dm_test_first_child_probe(struct unit_test_state *uts)
1078{
1079 struct udevice *bus, *dev;
1080 int count;
1081
1082 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
1083 count = 0;
1084 device_foreach_child_probe(dev, bus) {
1085 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1086 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
1087 count++;
1088 }
1089 ut_asserteq(3, count);
1090
1091 return 0;
1092}
Simon Glasse180c2b2020-07-28 19:41:12 -06001093DM_TEST(dm_test_first_child_probe, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb0dcc872020-04-05 15:38:19 -06001094
1095/* Test that ofdata is read for parents before children */
1096static int dm_test_ofdata_order(struct unit_test_state *uts)
1097{
1098 struct udevice *bus, *dev;
1099
1100 ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1101 ut_assertnonnull(bus);
1102 ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID));
1103
1104 ut_assertok(device_find_first_child(bus, &dev));
1105 ut_assertnonnull(dev);
1106 ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID));
1107
1108 /* read the child's ofdata which should cause the parent's to be read */
1109 ut_assertok(device_ofdata_to_platdata(dev));
1110 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1111 ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID);
1112
1113 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1114 ut_assert(!(bus->flags & DM_FLAG_ACTIVATED));
1115
1116 return 0;
1117}
Simon Glasse180c2b2020-07-28 19:41:12 -06001118DM_TEST(dm_test_ofdata_order, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);