blob: f54db759b65c74872bc417b5b43bde9d0724fe9f [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>
10#include <malloc.h>
11#include <asm/io.h>
12#include <dm/test.h>
13#include <dm/root.h>
Simon Glass98561572017-04-23 20:10:44 -060014#include <dm/device-internal.h>
Simon Glass61b29b82020-02-03 07:36:15 -070015#include <dm/devres.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070016#include <dm/uclass-internal.h>
17#include <dm/util.h>
Mario Six2ea4d0d2018-06-26 08:46:49 +020018#include <dm/lists.h>
19#include <dm/of_access.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050020#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070021
22DECLARE_GLOBAL_DATA_PTR;
23
Heiko Schocher54c5d082014-05-22 12:43:05 +020024static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070025{
26 const struct dm_test_pdata *pdata = dev->platdata;
27 struct dm_test_priv *priv = dev_get_priv(dev);
28
29 *pingret = pingval + pdata->ping_add;
30 priv->ping_total += *pingret;
31
32 return 0;
33}
34
35static const struct test_ops test_ops = {
36 .ping = testfdt_drv_ping,
37};
38
Heiko Schocher54c5d082014-05-22 12:43:05 +020039static int testfdt_ofdata_to_platdata(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070040{
41 struct dm_test_pdata *pdata = dev_get_platdata(dev);
42
Simon Glasse160f7d2017-01-17 16:52:55 -070043 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glass2e7d35d2014-02-26 15:59:21 -070044 "ping-add", -1);
Simon Glasse160f7d2017-01-17 16:52:55 -070045 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -060046 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -070047
48 return 0;
49}
50
Heiko Schocher54c5d082014-05-22 12:43:05 +020051static int testfdt_drv_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070052{
53 struct dm_test_priv *priv = dev_get_priv(dev);
54
55 priv->ping_total += DM_TEST_START_TOTAL;
56
Simon Glass83c7e432015-01-25 08:27:10 -070057 /*
58 * If this device is on a bus, the uclass_flag will be set before
Bin Mengd92878a2018-10-15 02:20:58 -070059 * calling this function. In the meantime the uclass_postp is
60 * initlized to a value -1. These are used respectively by
61 * dm_test_bus_child_pre_probe_uclass() and
62 * dm_test_bus_child_post_probe_uclass().
Simon Glass83c7e432015-01-25 08:27:10 -070063 */
64 priv->uclass_total += priv->uclass_flag;
Bin Mengd92878a2018-10-15 02:20:58 -070065 priv->uclass_postp = -1;
Simon Glass83c7e432015-01-25 08:27:10 -070066
Simon Glass2e7d35d2014-02-26 15:59:21 -070067 return 0;
68}
69
Simon Glassae7f4512014-06-11 23:29:45 -060070static const struct udevice_id testfdt_ids[] = {
Simon Glass2e7d35d2014-02-26 15:59:21 -070071 {
72 .compatible = "denx,u-boot-fdt-test",
73 .data = DM_TEST_TYPE_FIRST },
74 {
75 .compatible = "google,another-fdt-test",
76 .data = DM_TEST_TYPE_SECOND },
77 { }
78};
79
80U_BOOT_DRIVER(testfdt_drv) = {
81 .name = "testfdt_drv",
82 .of_match = testfdt_ids,
83 .id = UCLASS_TEST_FDT,
84 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
85 .probe = testfdt_drv_probe,
86 .ops = &test_ops,
87 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
88 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
89};
90
Bin Meng2786cd72018-10-10 22:07:01 -070091static const struct udevice_id testfdt1_ids[] = {
92 {
93 .compatible = "denx,u-boot-fdt-test1",
94 .data = DM_TEST_TYPE_FIRST },
95 { }
96};
97
98U_BOOT_DRIVER(testfdt1_drv) = {
99 .name = "testfdt1_drv",
100 .of_match = testfdt1_ids,
101 .id = UCLASS_TEST_FDT,
102 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
103 .probe = testfdt_drv_probe,
104 .ops = &test_ops,
105 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
106 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
107 .flags = DM_FLAG_PRE_RELOC,
108};
109
Simon Glass2e7d35d2014-02-26 15:59:21 -0700110/* From here is the testfdt uclass code */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200111int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700112{
113 const struct test_ops *ops = device_get_ops(dev);
114
115 if (!ops->ping)
116 return -ENOSYS;
117
118 return ops->ping(dev, pingval, pingret);
119}
120
121UCLASS_DRIVER(testfdt) = {
122 .name = "testfdt",
123 .id = UCLASS_TEST_FDT,
Simon Glass9cc36a22015-01-25 08:27:05 -0700124 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700125};
126
Simon Glass98561572017-04-23 20:10:44 -0600127struct dm_testprobe_pdata {
128 int probe_err;
129};
130
131static int testprobe_drv_probe(struct udevice *dev)
132{
133 struct dm_testprobe_pdata *pdata = dev_get_platdata(dev);
134
135 return pdata->probe_err;
136}
137
138static const struct udevice_id testprobe_ids[] = {
139 { .compatible = "denx,u-boot-probe-test" },
140 { }
141};
142
143U_BOOT_DRIVER(testprobe_drv) = {
144 .name = "testprobe_drv",
145 .of_match = testprobe_ids,
146 .id = UCLASS_TEST_PROBE,
147 .probe = testprobe_drv_probe,
148 .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata),
149};
150
151UCLASS_DRIVER(testprobe) = {
152 .name = "testprobe",
153 .id = UCLASS_TEST_PROBE,
154 .flags = DM_UC_FLAG_SEQ_ALIAS,
155};
156
Simon Glassdc12ebb2019-12-29 21:19:25 -0700157struct dm_testdevres_pdata {
158 void *ptr;
159};
160
161struct dm_testdevres_priv {
162 void *ptr;
Simon Glass42a0ce52019-12-29 21:19:28 -0700163 void *ptr_ofdata;
Simon Glassdc12ebb2019-12-29 21:19:25 -0700164};
165
166static int testdevres_drv_bind(struct udevice *dev)
167{
168 struct dm_testdevres_pdata *pdata = dev_get_platdata(dev);
169
170 pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
171
172 return 0;
173}
174
Simon Glass42a0ce52019-12-29 21:19:28 -0700175static int testdevres_drv_ofdata_to_platdata(struct udevice *dev)
176{
177 struct dm_testdevres_priv *priv = dev_get_priv(dev);
178
179 priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
180
181 return 0;
182}
183
Simon Glassdc12ebb2019-12-29 21:19:25 -0700184static int testdevres_drv_probe(struct udevice *dev)
185{
186 struct dm_testdevres_priv *priv = dev_get_priv(dev);
187
188 priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
189
190 return 0;
191}
192
193static const struct udevice_id testdevres_ids[] = {
194 { .compatible = "denx,u-boot-devres-test" },
195 { }
196};
197
198U_BOOT_DRIVER(testdevres_drv) = {
199 .name = "testdevres_drv",
200 .of_match = testdevres_ids,
201 .id = UCLASS_TEST_DEVRES,
202 .bind = testdevres_drv_bind,
Simon Glass42a0ce52019-12-29 21:19:28 -0700203 .ofdata_to_platdata = testdevres_drv_ofdata_to_platdata,
Simon Glassdc12ebb2019-12-29 21:19:25 -0700204 .probe = testdevres_drv_probe,
205 .platdata_auto_alloc_size = sizeof(struct dm_testdevres_pdata),
206 .priv_auto_alloc_size = sizeof(struct dm_testdevres_priv),
207};
208
209UCLASS_DRIVER(testdevres) = {
210 .name = "testdevres",
211 .id = UCLASS_TEST_DEVRES,
212 .flags = DM_UC_FLAG_SEQ_ALIAS,
213};
214
Joe Hershbergere721b882015-05-20 14:27:27 -0500215int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700216{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200217 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700218 int ret;
219 int i;
220
Simon Glass2e7d35d2014-02-26 15:59:21 -0700221 /*
222 * Now check that the ping adds are what we expect. This is using the
223 * ping-add property in each node.
224 */
Simon Glass1ca7e202014-07-23 06:55:18 -0600225 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-02-26 15:59:21 -0700226 uint32_t base;
227
228 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
229 ut_assert(!ret);
230
231 /*
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600232 * Get the 'ping-expect' property, which tells us what the
233 * ping add should be. We don't use the platdata because we
234 * want to test the code that sets that up
235 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700236 */
Simon Glasse160f7d2017-01-17 16:52:55 -0700237 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600238 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700239 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glasse160f7d2017-01-17 16:52:55 -0700240 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glass2e7d35d2014-02-26 15:59:21 -0700241
Joe Hershbergere721b882015-05-20 14:27:27 -0500242 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700243 dev_get_priv(dev)));
244 }
245
246 return 0;
247}
Simon Glass1ca7e202014-07-23 06:55:18 -0600248
249/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500250static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600251{
Bin Meng2786cd72018-10-10 22:07:01 -0700252 const int num_devices = 8;
Simon Glass1ca7e202014-07-23 06:55:18 -0600253 struct udevice *dev;
254 struct uclass *uc;
255 int ret;
256 int i;
257
258 ret = dm_scan_fdt(gd->fdt_blob, false);
259 ut_assert(!ret);
260
261 ret = uclass_get(UCLASS_TEST_FDT, &uc);
262 ut_assert(!ret);
263
264 /* These are num_devices compatible root-level device tree nodes */
265 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
266
Simon Glassf8a85442015-01-25 08:27:00 -0700267 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-07-23 06:55:18 -0600268 for (i = 0; i < num_devices; i++) {
269 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
270 ut_assert(!ret);
271 ut_assert(!dev_get_priv(dev));
Simon Glassf8a85442015-01-25 08:27:00 -0700272 ut_assert(dev->platdata);
Simon Glass1ca7e202014-07-23 06:55:18 -0600273 }
274
Joe Hershbergere721b882015-05-20 14:27:27 -0500275 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600276
277 return 0;
278}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700279DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600280
Michal Simeka93eb572019-01-31 16:31:00 +0100281static int dm_test_alias_highest_id(struct unit_test_state *uts)
282{
283 int ret;
284
285 ret = dev_read_alias_highest_id("eth");
286 ut_asserteq(5, ret);
287
288 ret = dev_read_alias_highest_id("gpio");
289 ut_asserteq(2, ret);
290
291 ret = dev_read_alias_highest_id("pci");
292 ut_asserteq(2, ret);
293
294 ret = dev_read_alias_highest_id("i2c");
295 ut_asserteq(0, ret);
296
297 ret = dev_read_alias_highest_id("deadbeef");
298 ut_asserteq(-1, ret);
299
300 return 0;
301}
302DM_TEST(dm_test_alias_highest_id, 0);
303
Joe Hershbergere721b882015-05-20 14:27:27 -0500304static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-07-23 06:55:03 -0600305{
306 struct uclass *uc;
307 int ret;
308
309 ret = dm_scan_fdt(gd->fdt_blob, true);
310 ut_assert(!ret);
311
312 ret = uclass_get(UCLASS_TEST_FDT, &uc);
313 ut_assert(!ret);
314
Bin Meng2786cd72018-10-10 22:07:01 -0700315 /*
316 * These are 2 pre-reloc devices:
317 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
318 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
319 */
320 ut_asserteq(2, list_count_items(&uc->dev_head));
Simon Glass00606d72014-07-23 06:55:03 -0600321
322 return 0;
323}
324DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600325
326/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500327static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600328{
329 struct udevice *dev;
330
331 /* A few basic santiy tests */
332 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
333 ut_asserteq_str("b-test", dev->name);
334
Simon Glass9cc36a22015-01-25 08:27:05 -0700335 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600336 ut_asserteq_str("a-test", dev->name);
337
338 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
339 true, &dev));
340 ut_asserteq_ptr(NULL, dev);
341
342 /* Test aliases */
343 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
344 ut_asserteq_str("e-test", dev->name);
345
346 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
347 true, &dev));
348
Simon Glass1ca7e202014-07-23 06:55:18 -0600349 /*
350 * Note that c-test nodes are not probed since it is not a top-level
351 * node
352 */
Simon Glass5a66a8f2014-07-23 06:55:12 -0600353 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
354 ut_asserteq_str("b-test", dev->name);
355
356 /*
357 * d-test wants sequence number 3 also, but it can't have it because
358 * b-test gets it first.
359 */
360 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
361 ut_asserteq_str("d-test", dev->name);
362
363 /* d-test actually gets 0 */
364 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
365 ut_asserteq_str("d-test", dev->name);
366
367 /* initially no one wants seq 1 */
368 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
369 &dev));
370 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
Simon Glass9cc36a22015-01-25 08:27:05 -0700371 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600372
373 /* But now that it is probed, we can find it */
374 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
Simon Glass9cc36a22015-01-25 08:27:05 -0700375 ut_asserteq_str("f-test", dev->name);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600376
377 return 0;
378}
379DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600380
381/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500382static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-07-23 06:55:14 -0600383{
384 const void *blob = gd->fdt_blob;
385 struct udevice *dev;
386 int node;
387
388 node = fdt_path_offset(blob, "/e-test");
389 ut_assert(node > 0);
390 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
391 &dev));
392 ut_asserteq_str("e-test", dev->name);
393
394 /* This node should not be bound */
395 node = fdt_path_offset(blob, "/junk");
396 ut_assert(node > 0);
397 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
398 node, &dev));
399
400 /* This is not a top level node so should not be probed */
Simon Glass1ca7e202014-07-23 06:55:18 -0600401 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-07-23 06:55:14 -0600402 ut_assert(node > 0);
403 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
404 node, &dev));
405
406 return 0;
407}
Simon Glass86b54ec2017-05-18 20:09:44 -0600408DM_TEST(dm_test_fdt_offset,
409 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
Simon Glass98561572017-04-23 20:10:44 -0600410
411/**
412 * Test various error conditions with uclass_first_device() and
413 * uclass_next_device()
414 */
415static int dm_test_first_next_device(struct unit_test_state *uts)
416{
417 struct dm_testprobe_pdata *pdata;
418 struct udevice *dev, *parent = NULL;
419 int count;
420 int ret;
421
422 /* There should be 4 devices */
423 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
424 dev;
425 ret = uclass_next_device(&dev)) {
426 count++;
427 parent = dev_get_parent(dev);
428 }
429 ut_assertok(ret);
430 ut_asserteq(4, count);
431
432 /* Remove them and try again, with an error on the second one */
433 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
434 pdata = dev_get_platdata(dev);
435 pdata->probe_err = -ENOMEM;
436 device_remove(parent, DM_REMOVE_NORMAL);
437 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
438 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
439 ut_asserteq_ptr(dev, NULL);
440
441 /* Now an error on the first one */
442 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
443 pdata = dev_get_platdata(dev);
444 pdata->probe_err = -ENOENT;
445 device_remove(parent, DM_REMOVE_NORMAL);
446 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
447
448 return 0;
449}
450DM_TEST(dm_test_first_next_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass95ce3852017-04-23 20:10:45 -0600451
452/**
453 * check_devices() - Check return values and pointers
454 *
455 * This runs through a full sequence of uclass_first_device_check()...
456 * uclass_next_device_check() checking that the return values and devices
457 * are correct.
458 *
459 * @uts: Test state
460 * @devlist: List of expected devices
461 * @mask: Indicates which devices should return an error. Device n should
462 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
463 * bit n is clear.
464 */
465static int check_devices(struct unit_test_state *uts,
466 struct udevice *devlist[], int mask)
467{
468 int expected_ret;
469 struct udevice *dev;
470 int i;
471
472 expected_ret = (mask & 1) ? -ENOENT : 0;
473 mask >>= 1;
474 ut_asserteq(expected_ret,
475 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
476 for (i = 0; i < 4; i++) {
477 ut_asserteq_ptr(devlist[i], dev);
478 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
479 mask >>= 1;
480 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
481 }
482 ut_asserteq_ptr(NULL, dev);
483
484 return 0;
485}
486
487/* Test uclass_first_device_check() and uclass_next_device_check() */
488static int dm_test_first_next_ok_device(struct unit_test_state *uts)
489{
490 struct dm_testprobe_pdata *pdata;
491 struct udevice *dev, *parent = NULL, *devlist[4];
492 int count;
493 int ret;
494
495 /* There should be 4 devices */
496 count = 0;
497 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
498 dev;
499 ret = uclass_next_device_check(&dev)) {
500 ut_assertok(ret);
501 devlist[count++] = dev;
502 parent = dev_get_parent(dev);
503 }
504 ut_asserteq(4, count);
505 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
506 ut_assertok(check_devices(uts, devlist, 0));
507
508 /* Remove them and try again, with an error on the second one */
509 pdata = dev_get_platdata(devlist[1]);
510 pdata->probe_err = -ENOENT - 1;
511 device_remove(parent, DM_REMOVE_NORMAL);
512 ut_assertok(check_devices(uts, devlist, 1 << 1));
513
514 /* Now an error on the first one */
515 pdata = dev_get_platdata(devlist[0]);
516 pdata->probe_err = -ENOENT - 0;
517 device_remove(parent, DM_REMOVE_NORMAL);
518 ut_assertok(check_devices(uts, devlist, 3 << 0));
519
520 /* Now errors on all */
521 pdata = dev_get_platdata(devlist[2]);
522 pdata->probe_err = -ENOENT - 2;
523 pdata = dev_get_platdata(devlist[3]);
524 pdata->probe_err = -ENOENT - 3;
525 device_remove(parent, DM_REMOVE_NORMAL);
526 ut_assertok(check_devices(uts, devlist, 0xf << 0));
527
528 return 0;
529}
530DM_TEST(dm_test_first_next_ok_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Sixe8d52912018-03-12 14:53:33 +0100531
532static const struct udevice_id fdt_dummy_ids[] = {
533 { .compatible = "denx,u-boot-fdt-dummy", },
534 { }
535};
536
537UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca507cef32018-05-19 14:13:55 +0200538 .name = "fdt-dummy",
Mario Sixe8d52912018-03-12 14:53:33 +0100539 .id = UCLASS_TEST_DUMMY,
540 .flags = DM_UC_FLAG_SEQ_ALIAS,
541};
542
543U_BOOT_DRIVER(fdt_dummy_drv) = {
544 .name = "fdt_dummy_drv",
545 .of_match = fdt_dummy_ids,
546 .id = UCLASS_TEST_DUMMY,
547};
548
549static int dm_test_fdt_translation(struct unit_test_state *uts)
550{
551 struct udevice *dev;
Fabien Dessenne641067f2019-05-31 15:11:30 +0200552 fdt32_t dma_addr[2];
Mario Sixe8d52912018-03-12 14:53:33 +0100553
554 /* Some simple translations */
555 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
556 ut_asserteq_str("dev@0,0", dev->name);
557 ut_asserteq(0x8000, dev_read_addr(dev));
558
559 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
560 ut_asserteq_str("dev@1,100", dev->name);
561 ut_asserteq(0x9000, dev_read_addr(dev));
562
563 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev));
564 ut_asserteq_str("dev@2,200", dev->name);
565 ut_asserteq(0xA000, dev_read_addr(dev));
566
567 /* No translation for busses with #size-cells == 0 */
568 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev));
569 ut_asserteq_str("dev@42", dev->name);
570 ut_asserteq(0x42, dev_read_addr(dev));
571
Fabien Dessenne641067f2019-05-31 15:11:30 +0200572 /* dma address translation */
573 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
574 dma_addr[0] = cpu_to_be32(0);
575 dma_addr[1] = cpu_to_be32(0);
576 ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
577
578 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
579 dma_addr[0] = cpu_to_be32(1);
580 dma_addr[1] = cpu_to_be32(0x100);
581 ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
582
Mario Sixe8d52912018-03-12 14:53:33 +0100583 return 0;
584}
585DM_TEST(dm_test_fdt_translation, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200586
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200587static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
588{
589 struct udevice *dev;
590 fdt_addr_t addr;
591 void *paddr;
592
593 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
594
595 addr = devfdt_get_addr(dev);
596 ut_asserteq(0x8000, addr);
597
598 paddr = map_physmem(addr, 0, MAP_NOCACHE);
599 ut_assertnonnull(paddr);
600 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
601
602 return 0;
603}
604DM_TEST(dm_test_fdt_remap_addr_flat,
605 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
606
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100607static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
608{
609 struct udevice *dev;
610 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530611 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100612 void *paddr;
613
614 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
615
Sekhar Norif5b90472019-08-01 19:12:56 +0530616 addr = devfdt_get_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100617 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530618 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100619
620 paddr = map_physmem(addr, 0, MAP_NOCACHE);
621 ut_assertnonnull(paddr);
622 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
623
624 return 0;
625}
626DM_TEST(dm_test_fdt_remap_addr_index_flat,
627 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
628
629static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
630{
631 struct udevice *dev;
632 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530633 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100634 void *paddr;
635
636 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
637
Sekhar Norif5b90472019-08-01 19:12:56 +0530638 addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100639 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530640 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100641
642 paddr = map_physmem(addr, 0, MAP_NOCACHE);
643 ut_assertnonnull(paddr);
644 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
645
646 return 0;
647}
648DM_TEST(dm_test_fdt_remap_addr_name_flat,
649 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
650
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200651static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
652{
653 struct udevice *dev;
654 fdt_addr_t addr;
655 void *paddr;
656
657 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
658
659 addr = dev_read_addr(dev);
660 ut_asserteq(0x8000, addr);
661
662 paddr = map_physmem(addr, 0, MAP_NOCACHE);
663 ut_assertnonnull(paddr);
664 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
665
666 return 0;
667}
668DM_TEST(dm_test_fdt_remap_addr_live,
669 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Six2ea4d0d2018-06-26 08:46:49 +0200670
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100671static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
672{
673 struct udevice *dev;
674 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530675 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100676 void *paddr;
677
678 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
679
Sekhar Norif5b90472019-08-01 19:12:56 +0530680 addr = dev_read_addr_size_index(dev, 0, &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100681 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530682 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100683
684 paddr = map_physmem(addr, 0, MAP_NOCACHE);
685 ut_assertnonnull(paddr);
686 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
687
688 return 0;
689}
690DM_TEST(dm_test_fdt_remap_addr_index_live,
691 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
692
693static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
694{
695 struct udevice *dev;
696 fdt_addr_t addr;
Sekhar Norif5b90472019-08-01 19:12:56 +0530697 fdt_size_t size;
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100698 void *paddr;
699
700 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
701
Sekhar Norif5b90472019-08-01 19:12:56 +0530702 addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100703 ut_asserteq(0x8000, addr);
Sekhar Norif5b90472019-08-01 19:12:56 +0530704 ut_asserteq(0x1000, size);
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100705
706 paddr = map_physmem(addr, 0, MAP_NOCACHE);
707 ut_assertnonnull(paddr);
708 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
709
710 return 0;
711}
712DM_TEST(dm_test_fdt_remap_addr_name_live,
713 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
714
Mario Six2ea4d0d2018-06-26 08:46:49 +0200715static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
716{
717 struct udevice *dev;
718 ofnode node;
719
720 if (!of_live_active()) {
721 printf("Live tree not active; ignore test\n");
722 return 0;
723 }
724
725 /* Test enabling devices */
726
727 node = ofnode_path("/usb@2");
728
729 ut_assert(!of_device_is_available(ofnode_to_np(node)));
730 ofnode_set_enabled(node, true);
731 ut_assert(of_device_is_available(ofnode_to_np(node)));
732
733 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
734 &dev);
735 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
736
737 /* Test string property setting */
738
739 ut_assert(device_is_compatible(dev, "sandbox,usb"));
740 ofnode_write_string(node, "compatible", "gdsys,super-usb");
741 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
742 ofnode_write_string(node, "compatible", "sandbox,usb");
743 ut_assert(device_is_compatible(dev, "sandbox,usb"));
744
745 /* Test setting generic properties */
746
747 /* Non-existent in DTB */
748 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
749 /* reg = 0x42, size = 0x100 */
750 ut_assertok(ofnode_write_prop(node, "reg", 8,
751 "\x00\x00\x00\x42\x00\x00\x01\x00"));
752 ut_asserteq(0x42, dev_read_addr(dev));
753
754 /* Test disabling devices */
755
756 device_remove(dev, DM_REMOVE_NORMAL);
757 device_unbind(dev);
758
759 ut_assert(of_device_is_available(ofnode_to_np(node)));
760 ofnode_set_enabled(node, false);
761 ut_assert(!of_device_is_available(ofnode_to_np(node)));
762
763 return 0;
764}
765DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Six172942a2018-06-26 08:46:51 +0200766
767static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
768{
769 ofnode node;
770
771 if (!of_live_active()) {
772 printf("Live tree not active; ignore test\n");
773 return 0;
774 }
775
776 node = ofnode_path("/usb@2");
777
778 /* Test enabling devices */
779
780 ut_assert(!of_device_is_available(ofnode_to_np(node)));
781 dev_enable_by_path("/usb@2");
782 ut_assert(of_device_is_available(ofnode_to_np(node)));
783
784 /* Test disabling devices */
785
786 ut_assert(of_device_is_available(ofnode_to_np(node)));
787 dev_disable_by_path("/usb@2");
788 ut_assert(!of_device_is_available(ofnode_to_np(node)));
789
790 return 0;
791}
792DM_TEST(dm_test_fdt_disable_enable_by_path, DM_TESTF_SCAN_PDATA |
793 DM_TESTF_SCAN_FDT);
Simon Glassd0b4f682018-11-18 08:14:30 -0700794
795/* Test a few uclass phandle functions */
796static int dm_test_fdt_phandle(struct unit_test_state *uts)
797{
798 struct udevice *back, *dev, *dev2;
799
800 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
801 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
802 back, "missing", &dev));
803 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
804 "power-supply", &dev));
805 ut_asserteq(0, device_active(dev));
806 ut_asserteq_str("ldo1", dev->name);
807 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
808 "power-supply", &dev2));
809 ut_asserteq_ptr(dev, dev2);
810
811 return 0;
812}
813DM_TEST(dm_test_fdt_phandle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass3abe1112018-11-18 08:14:31 -0700814
815/* Test device_find_first_child_by_uclass() */
816static int dm_test_first_child(struct unit_test_state *uts)
817{
818 struct udevice *i2c, *dev, *dev2;
819
820 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
821 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
822 ut_asserteq_str("rtc@43", dev->name);
823 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
824 ut_asserteq_ptr(dev, dev2);
825 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
826 ut_asserteq_str("rtc@61", dev2->name);
827
828 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
829 &dev));
830 ut_asserteq_str("eeprom@2c", dev->name);
831 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
832 ut_asserteq_ptr(dev, dev2);
833
834 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
835 UCLASS_VIDEO, &dev));
836 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
837
838 return 0;
839}
840DM_TEST(dm_test_first_child, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassa1b17e42018-12-10 10:37:37 -0700841
842/* Test integer functions in dm_read_...() */
843static int dm_test_read_int(struct unit_test_state *uts)
844{
845 struct udevice *dev;
846 u32 val32;
847 s32 sval;
848 uint val;
849
850 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
851 ut_asserteq_str("a-test", dev->name);
852 ut_assertok(dev_read_u32(dev, "int-value", &val32));
853 ut_asserteq(1234, val32);
854
855 ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
856 ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
857
858 ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
859 ut_asserteq(1234, val32);
860
861 ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
862 ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
863
864 ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
865 ut_assertok(dev_read_s32(dev, "uint-value", &sval));
866 ut_asserteq(-1234, sval);
867
868 val = 0;
869 ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
870 ut_assertok(dev_read_u32u(dev, "uint-value", &val));
871 ut_asserteq(-1234, val);
872
873 return 0;
874}
875DM_TEST(dm_test_read_int, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassf262d4c2020-01-27 08:49:47 -0700876
877/* Test device_first_child_ofdata_err(), etc. */
878static int dm_test_child_ofdata(struct unit_test_state *uts)
879{
880 struct udevice *bus, *dev;
881 int count;
882
883 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
884 count = 0;
885 device_foreach_child_ofdata_to_platdata(dev, bus) {
886 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
887 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
888 count++;
889 }
890 ut_asserteq(3, count);
891
892 return 0;
893}
894DM_TEST(dm_test_child_ofdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass903e83e2020-01-27 08:49:48 -0700895
896/* Test device_first_child_err(), etc. */
897static int dm_test_first_child_probe(struct unit_test_state *uts)
898{
899 struct udevice *bus, *dev;
900 int count;
901
902 ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
903 count = 0;
904 device_foreach_child_probe(dev, bus) {
905 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
906 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
907 count++;
908 }
909 ut_asserteq(3, count);
910
911 return 0;
912}
913DM_TEST(dm_test_first_child_probe, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);