blob: 96d2528accbd9f88ad2564c2f502c5aad5f3dc3a [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 Glass2e7d35d2014-02-26 15:59:21 -070015#include <dm/uclass-internal.h>
16#include <dm/util.h>
Mario Six2ea4d0d2018-06-26 08:46:49 +020017#include <dm/lists.h>
18#include <dm/of_access.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070020
21DECLARE_GLOBAL_DATA_PTR;
22
Heiko Schocher54c5d082014-05-22 12:43:05 +020023static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070024{
25 const struct dm_test_pdata *pdata = dev->platdata;
26 struct dm_test_priv *priv = dev_get_priv(dev);
27
28 *pingret = pingval + pdata->ping_add;
29 priv->ping_total += *pingret;
30
31 return 0;
32}
33
34static const struct test_ops test_ops = {
35 .ping = testfdt_drv_ping,
36};
37
Heiko Schocher54c5d082014-05-22 12:43:05 +020038static int testfdt_ofdata_to_platdata(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070039{
40 struct dm_test_pdata *pdata = dev_get_platdata(dev);
41
Simon Glasse160f7d2017-01-17 16:52:55 -070042 pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
Simon Glass2e7d35d2014-02-26 15:59:21 -070043 "ping-add", -1);
Simon Glasse160f7d2017-01-17 16:52:55 -070044 pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -060045 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -070046
47 return 0;
48}
49
Heiko Schocher54c5d082014-05-22 12:43:05 +020050static int testfdt_drv_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070051{
52 struct dm_test_priv *priv = dev_get_priv(dev);
53
54 priv->ping_total += DM_TEST_START_TOTAL;
55
Simon Glass83c7e432015-01-25 08:27:10 -070056 /*
57 * If this device is on a bus, the uclass_flag will be set before
Bin Mengd92878a2018-10-15 02:20:58 -070058 * calling this function. In the meantime the uclass_postp is
59 * initlized to a value -1. These are used respectively by
60 * dm_test_bus_child_pre_probe_uclass() and
61 * dm_test_bus_child_post_probe_uclass().
Simon Glass83c7e432015-01-25 08:27:10 -070062 */
63 priv->uclass_total += priv->uclass_flag;
Bin Mengd92878a2018-10-15 02:20:58 -070064 priv->uclass_postp = -1;
Simon Glass83c7e432015-01-25 08:27:10 -070065
Simon Glass2e7d35d2014-02-26 15:59:21 -070066 return 0;
67}
68
Simon Glassae7f4512014-06-11 23:29:45 -060069static const struct udevice_id testfdt_ids[] = {
Simon Glass2e7d35d2014-02-26 15:59:21 -070070 {
71 .compatible = "denx,u-boot-fdt-test",
72 .data = DM_TEST_TYPE_FIRST },
73 {
74 .compatible = "google,another-fdt-test",
75 .data = DM_TEST_TYPE_SECOND },
76 { }
77};
78
79U_BOOT_DRIVER(testfdt_drv) = {
80 .name = "testfdt_drv",
81 .of_match = testfdt_ids,
82 .id = UCLASS_TEST_FDT,
83 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
84 .probe = testfdt_drv_probe,
85 .ops = &test_ops,
86 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
87 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
88};
89
Bin Meng2786cd72018-10-10 22:07:01 -070090static const struct udevice_id testfdt1_ids[] = {
91 {
92 .compatible = "denx,u-boot-fdt-test1",
93 .data = DM_TEST_TYPE_FIRST },
94 { }
95};
96
97U_BOOT_DRIVER(testfdt1_drv) = {
98 .name = "testfdt1_drv",
99 .of_match = testfdt1_ids,
100 .id = UCLASS_TEST_FDT,
101 .ofdata_to_platdata = testfdt_ofdata_to_platdata,
102 .probe = testfdt_drv_probe,
103 .ops = &test_ops,
104 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
105 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
106 .flags = DM_FLAG_PRE_RELOC,
107};
108
Simon Glass2e7d35d2014-02-26 15:59:21 -0700109/* From here is the testfdt uclass code */
Heiko Schocher54c5d082014-05-22 12:43:05 +0200110int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700111{
112 const struct test_ops *ops = device_get_ops(dev);
113
114 if (!ops->ping)
115 return -ENOSYS;
116
117 return ops->ping(dev, pingval, pingret);
118}
119
120UCLASS_DRIVER(testfdt) = {
121 .name = "testfdt",
122 .id = UCLASS_TEST_FDT,
Simon Glass9cc36a22015-01-25 08:27:05 -0700123 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700124};
125
Simon Glass98561572017-04-23 20:10:44 -0600126struct dm_testprobe_pdata {
127 int probe_err;
128};
129
130static int testprobe_drv_probe(struct udevice *dev)
131{
132 struct dm_testprobe_pdata *pdata = dev_get_platdata(dev);
133
134 return pdata->probe_err;
135}
136
137static const struct udevice_id testprobe_ids[] = {
138 { .compatible = "denx,u-boot-probe-test" },
139 { }
140};
141
142U_BOOT_DRIVER(testprobe_drv) = {
143 .name = "testprobe_drv",
144 .of_match = testprobe_ids,
145 .id = UCLASS_TEST_PROBE,
146 .probe = testprobe_drv_probe,
147 .platdata_auto_alloc_size = sizeof(struct dm_testprobe_pdata),
148};
149
150UCLASS_DRIVER(testprobe) = {
151 .name = "testprobe",
152 .id = UCLASS_TEST_PROBE,
153 .flags = DM_UC_FLAG_SEQ_ALIAS,
154};
155
Joe Hershbergere721b882015-05-20 14:27:27 -0500156int dm_check_devices(struct unit_test_state *uts, int num_devices)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700157{
Heiko Schocher54c5d082014-05-22 12:43:05 +0200158 struct udevice *dev;
Simon Glass2e7d35d2014-02-26 15:59:21 -0700159 int ret;
160 int i;
161
Simon Glass2e7d35d2014-02-26 15:59:21 -0700162 /*
163 * Now check that the ping adds are what we expect. This is using the
164 * ping-add property in each node.
165 */
Simon Glass1ca7e202014-07-23 06:55:18 -0600166 for (i = 0; i < num_devices; i++) {
Simon Glass2e7d35d2014-02-26 15:59:21 -0700167 uint32_t base;
168
169 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
170 ut_assert(!ret);
171
172 /*
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600173 * Get the 'ping-expect' property, which tells us what the
174 * ping add should be. We don't use the platdata because we
175 * want to test the code that sets that up
176 * (testfdt_drv_probe()).
Simon Glass2e7d35d2014-02-26 15:59:21 -0700177 */
Simon Glasse160f7d2017-01-17 16:52:55 -0700178 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
Simon Glasseb9ef5f2014-07-23 06:54:57 -0600179 "ping-expect");
Simon Glass2e7d35d2014-02-26 15:59:21 -0700180 debug("dev=%d, base=%d: %s\n", i, base,
Simon Glasse160f7d2017-01-17 16:52:55 -0700181 fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
Simon Glass2e7d35d2014-02-26 15:59:21 -0700182
Joe Hershbergere721b882015-05-20 14:27:27 -0500183 ut_assert(!dm_check_operations(uts, dev, base,
Simon Glass2e7d35d2014-02-26 15:59:21 -0700184 dev_get_priv(dev)));
185 }
186
187 return 0;
188}
Simon Glass1ca7e202014-07-23 06:55:18 -0600189
190/* Test that FDT-based binding works correctly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500191static int dm_test_fdt(struct unit_test_state *uts)
Simon Glass1ca7e202014-07-23 06:55:18 -0600192{
Bin Meng2786cd72018-10-10 22:07:01 -0700193 const int num_devices = 8;
Simon Glass1ca7e202014-07-23 06:55:18 -0600194 struct udevice *dev;
195 struct uclass *uc;
196 int ret;
197 int i;
198
199 ret = dm_scan_fdt(gd->fdt_blob, false);
200 ut_assert(!ret);
201
202 ret = uclass_get(UCLASS_TEST_FDT, &uc);
203 ut_assert(!ret);
204
205 /* These are num_devices compatible root-level device tree nodes */
206 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
207
Simon Glassf8a85442015-01-25 08:27:00 -0700208 /* Each should have platform data but no private data */
Simon Glass1ca7e202014-07-23 06:55:18 -0600209 for (i = 0; i < num_devices; i++) {
210 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
211 ut_assert(!ret);
212 ut_assert(!dev_get_priv(dev));
Simon Glassf8a85442015-01-25 08:27:00 -0700213 ut_assert(dev->platdata);
Simon Glass1ca7e202014-07-23 06:55:18 -0600214 }
215
Joe Hershbergere721b882015-05-20 14:27:27 -0500216 ut_assertok(dm_check_devices(uts, num_devices));
Simon Glass1ca7e202014-07-23 06:55:18 -0600217
218 return 0;
219}
Simon Glass2e7d35d2014-02-26 15:59:21 -0700220DM_TEST(dm_test_fdt, 0);
Simon Glass00606d72014-07-23 06:55:03 -0600221
Joe Hershbergere721b882015-05-20 14:27:27 -0500222static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
Simon Glass00606d72014-07-23 06:55:03 -0600223{
224 struct uclass *uc;
225 int ret;
226
227 ret = dm_scan_fdt(gd->fdt_blob, true);
228 ut_assert(!ret);
229
230 ret = uclass_get(UCLASS_TEST_FDT, &uc);
231 ut_assert(!ret);
232
Bin Meng2786cd72018-10-10 22:07:01 -0700233 /*
234 * These are 2 pre-reloc devices:
235 * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
236 * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
237 */
238 ut_asserteq(2, list_count_items(&uc->dev_head));
Simon Glass00606d72014-07-23 06:55:03 -0600239
240 return 0;
241}
242DM_TEST(dm_test_fdt_pre_reloc, 0);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600243
244/* Test that sequence numbers are allocated properly */
Joe Hershbergere721b882015-05-20 14:27:27 -0500245static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
Simon Glass5a66a8f2014-07-23 06:55:12 -0600246{
247 struct udevice *dev;
248
249 /* A few basic santiy tests */
250 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
251 ut_asserteq_str("b-test", dev->name);
252
Simon Glass9cc36a22015-01-25 08:27:05 -0700253 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600254 ut_asserteq_str("a-test", dev->name);
255
256 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
257 true, &dev));
258 ut_asserteq_ptr(NULL, dev);
259
260 /* Test aliases */
261 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
262 ut_asserteq_str("e-test", dev->name);
263
264 ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
265 true, &dev));
266
Simon Glass1ca7e202014-07-23 06:55:18 -0600267 /*
268 * Note that c-test nodes are not probed since it is not a top-level
269 * node
270 */
Simon Glass5a66a8f2014-07-23 06:55:12 -0600271 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
272 ut_asserteq_str("b-test", dev->name);
273
274 /*
275 * d-test wants sequence number 3 also, but it can't have it because
276 * b-test gets it first.
277 */
278 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
279 ut_asserteq_str("d-test", dev->name);
280
281 /* d-test actually gets 0 */
282 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
283 ut_asserteq_str("d-test", dev->name);
284
285 /* initially no one wants seq 1 */
286 ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
287 &dev));
288 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
Simon Glass9cc36a22015-01-25 08:27:05 -0700289 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
Simon Glass5a66a8f2014-07-23 06:55:12 -0600290
291 /* But now that it is probed, we can find it */
292 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
Simon Glass9cc36a22015-01-25 08:27:05 -0700293 ut_asserteq_str("f-test", dev->name);
Simon Glass5a66a8f2014-07-23 06:55:12 -0600294
295 return 0;
296}
297DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassf4cdead2014-07-23 06:55:14 -0600298
299/* Test that we can find a device by device tree offset */
Joe Hershbergere721b882015-05-20 14:27:27 -0500300static int dm_test_fdt_offset(struct unit_test_state *uts)
Simon Glassf4cdead2014-07-23 06:55:14 -0600301{
302 const void *blob = gd->fdt_blob;
303 struct udevice *dev;
304 int node;
305
306 node = fdt_path_offset(blob, "/e-test");
307 ut_assert(node > 0);
308 ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
309 &dev));
310 ut_asserteq_str("e-test", dev->name);
311
312 /* This node should not be bound */
313 node = fdt_path_offset(blob, "/junk");
314 ut_assert(node > 0);
315 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
316 node, &dev));
317
318 /* This is not a top level node so should not be probed */
Simon Glass1ca7e202014-07-23 06:55:18 -0600319 node = fdt_path_offset(blob, "/some-bus/c-test@5");
Simon Glassf4cdead2014-07-23 06:55:14 -0600320 ut_assert(node > 0);
321 ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
322 node, &dev));
323
324 return 0;
325}
Simon Glass86b54ec2017-05-18 20:09:44 -0600326DM_TEST(dm_test_fdt_offset,
327 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
Simon Glass98561572017-04-23 20:10:44 -0600328
329/**
330 * Test various error conditions with uclass_first_device() and
331 * uclass_next_device()
332 */
333static int dm_test_first_next_device(struct unit_test_state *uts)
334{
335 struct dm_testprobe_pdata *pdata;
336 struct udevice *dev, *parent = NULL;
337 int count;
338 int ret;
339
340 /* There should be 4 devices */
341 for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
342 dev;
343 ret = uclass_next_device(&dev)) {
344 count++;
345 parent = dev_get_parent(dev);
346 }
347 ut_assertok(ret);
348 ut_asserteq(4, count);
349
350 /* Remove them and try again, with an error on the second one */
351 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
352 pdata = dev_get_platdata(dev);
353 pdata->probe_err = -ENOMEM;
354 device_remove(parent, DM_REMOVE_NORMAL);
355 ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
356 ut_asserteq(-ENOMEM, uclass_next_device(&dev));
357 ut_asserteq_ptr(dev, NULL);
358
359 /* Now an error on the first one */
360 ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
361 pdata = dev_get_platdata(dev);
362 pdata->probe_err = -ENOENT;
363 device_remove(parent, DM_REMOVE_NORMAL);
364 ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
365
366 return 0;
367}
368DM_TEST(dm_test_first_next_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass95ce3852017-04-23 20:10:45 -0600369
370/**
371 * check_devices() - Check return values and pointers
372 *
373 * This runs through a full sequence of uclass_first_device_check()...
374 * uclass_next_device_check() checking that the return values and devices
375 * are correct.
376 *
377 * @uts: Test state
378 * @devlist: List of expected devices
379 * @mask: Indicates which devices should return an error. Device n should
380 * return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
381 * bit n is clear.
382 */
383static int check_devices(struct unit_test_state *uts,
384 struct udevice *devlist[], int mask)
385{
386 int expected_ret;
387 struct udevice *dev;
388 int i;
389
390 expected_ret = (mask & 1) ? -ENOENT : 0;
391 mask >>= 1;
392 ut_asserteq(expected_ret,
393 uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
394 for (i = 0; i < 4; i++) {
395 ut_asserteq_ptr(devlist[i], dev);
396 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
397 mask >>= 1;
398 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
399 }
400 ut_asserteq_ptr(NULL, dev);
401
402 return 0;
403}
404
405/* Test uclass_first_device_check() and uclass_next_device_check() */
406static int dm_test_first_next_ok_device(struct unit_test_state *uts)
407{
408 struct dm_testprobe_pdata *pdata;
409 struct udevice *dev, *parent = NULL, *devlist[4];
410 int count;
411 int ret;
412
413 /* There should be 4 devices */
414 count = 0;
415 for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
416 dev;
417 ret = uclass_next_device_check(&dev)) {
418 ut_assertok(ret);
419 devlist[count++] = dev;
420 parent = dev_get_parent(dev);
421 }
422 ut_asserteq(4, count);
423 ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
424 ut_assertok(check_devices(uts, devlist, 0));
425
426 /* Remove them and try again, with an error on the second one */
427 pdata = dev_get_platdata(devlist[1]);
428 pdata->probe_err = -ENOENT - 1;
429 device_remove(parent, DM_REMOVE_NORMAL);
430 ut_assertok(check_devices(uts, devlist, 1 << 1));
431
432 /* Now an error on the first one */
433 pdata = dev_get_platdata(devlist[0]);
434 pdata->probe_err = -ENOENT - 0;
435 device_remove(parent, DM_REMOVE_NORMAL);
436 ut_assertok(check_devices(uts, devlist, 3 << 0));
437
438 /* Now errors on all */
439 pdata = dev_get_platdata(devlist[2]);
440 pdata->probe_err = -ENOENT - 2;
441 pdata = dev_get_platdata(devlist[3]);
442 pdata->probe_err = -ENOENT - 3;
443 device_remove(parent, DM_REMOVE_NORMAL);
444 ut_assertok(check_devices(uts, devlist, 0xf << 0));
445
446 return 0;
447}
448DM_TEST(dm_test_first_next_ok_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Sixe8d52912018-03-12 14:53:33 +0100449
450static const struct udevice_id fdt_dummy_ids[] = {
451 { .compatible = "denx,u-boot-fdt-dummy", },
452 { }
453};
454
455UCLASS_DRIVER(fdt_dummy) = {
Eugeniu Rosca507cef32018-05-19 14:13:55 +0200456 .name = "fdt-dummy",
Mario Sixe8d52912018-03-12 14:53:33 +0100457 .id = UCLASS_TEST_DUMMY,
458 .flags = DM_UC_FLAG_SEQ_ALIAS,
459};
460
461U_BOOT_DRIVER(fdt_dummy_drv) = {
462 .name = "fdt_dummy_drv",
463 .of_match = fdt_dummy_ids,
464 .id = UCLASS_TEST_DUMMY,
465};
466
467static int dm_test_fdt_translation(struct unit_test_state *uts)
468{
469 struct udevice *dev;
470
471 /* Some simple translations */
472 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
473 ut_asserteq_str("dev@0,0", dev->name);
474 ut_asserteq(0x8000, dev_read_addr(dev));
475
476 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
477 ut_asserteq_str("dev@1,100", dev->name);
478 ut_asserteq(0x9000, dev_read_addr(dev));
479
480 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev));
481 ut_asserteq_str("dev@2,200", dev->name);
482 ut_asserteq(0xA000, dev_read_addr(dev));
483
484 /* No translation for busses with #size-cells == 0 */
485 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev));
486 ut_asserteq_str("dev@42", dev->name);
487 ut_asserteq(0x42, dev_read_addr(dev));
488
489 return 0;
490}
491DM_TEST(dm_test_fdt_translation, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200492
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200493static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
494{
495 struct udevice *dev;
496 fdt_addr_t addr;
497 void *paddr;
498
499 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
500
501 addr = devfdt_get_addr(dev);
502 ut_asserteq(0x8000, addr);
503
504 paddr = map_physmem(addr, 0, MAP_NOCACHE);
505 ut_assertnonnull(paddr);
506 ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
507
508 return 0;
509}
510DM_TEST(dm_test_fdt_remap_addr_flat,
511 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
512
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100513static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
514{
515 struct udevice *dev;
516 fdt_addr_t addr;
517 void *paddr;
518
519 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
520
521 addr = devfdt_get_addr_index(dev, 0);
522 ut_asserteq(0x8000, addr);
523
524 paddr = map_physmem(addr, 0, MAP_NOCACHE);
525 ut_assertnonnull(paddr);
526 ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
527
528 return 0;
529}
530DM_TEST(dm_test_fdt_remap_addr_index_flat,
531 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
532
533static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
534{
535 struct udevice *dev;
536 fdt_addr_t addr;
537 void *paddr;
538
539 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
540
541 addr = devfdt_get_addr_name(dev, "sandbox-dummy-0");
542 ut_asserteq(0x8000, addr);
543
544 paddr = map_physmem(addr, 0, MAP_NOCACHE);
545 ut_assertnonnull(paddr);
546 ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
547
548 return 0;
549}
550DM_TEST(dm_test_fdt_remap_addr_name_flat,
551 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
552
Álvaro Fernández Rojas30a90f52018-04-29 21:56:54 +0200553static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
554{
555 struct udevice *dev;
556 fdt_addr_t addr;
557 void *paddr;
558
559 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
560
561 addr = dev_read_addr(dev);
562 ut_asserteq(0x8000, addr);
563
564 paddr = map_physmem(addr, 0, MAP_NOCACHE);
565 ut_assertnonnull(paddr);
566 ut_asserteq_ptr(paddr, dev_remap_addr(dev));
567
568 return 0;
569}
570DM_TEST(dm_test_fdt_remap_addr_live,
571 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Six2ea4d0d2018-06-26 08:46:49 +0200572
Álvaro Fernández Rojas79598822018-12-03 19:37:09 +0100573static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
574{
575 struct udevice *dev;
576 fdt_addr_t addr;
577 void *paddr;
578
579 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
580
581 addr = dev_read_addr_index(dev, 0);
582 ut_asserteq(0x8000, addr);
583
584 paddr = map_physmem(addr, 0, MAP_NOCACHE);
585 ut_assertnonnull(paddr);
586 ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
587
588 return 0;
589}
590DM_TEST(dm_test_fdt_remap_addr_index_live,
591 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
592
593static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
594{
595 struct udevice *dev;
596 fdt_addr_t addr;
597 void *paddr;
598
599 ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
600
601 addr = dev_read_addr_name(dev, "sandbox-dummy-0");
602 ut_asserteq(0x8000, addr);
603
604 paddr = map_physmem(addr, 0, MAP_NOCACHE);
605 ut_assertnonnull(paddr);
606 ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
607
608 return 0;
609}
610DM_TEST(dm_test_fdt_remap_addr_name_live,
611 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
612
Mario Six2ea4d0d2018-06-26 08:46:49 +0200613static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
614{
615 struct udevice *dev;
616 ofnode node;
617
618 if (!of_live_active()) {
619 printf("Live tree not active; ignore test\n");
620 return 0;
621 }
622
623 /* Test enabling devices */
624
625 node = ofnode_path("/usb@2");
626
627 ut_assert(!of_device_is_available(ofnode_to_np(node)));
628 ofnode_set_enabled(node, true);
629 ut_assert(of_device_is_available(ofnode_to_np(node)));
630
631 device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
632 &dev);
633 ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
634
635 /* Test string property setting */
636
637 ut_assert(device_is_compatible(dev, "sandbox,usb"));
638 ofnode_write_string(node, "compatible", "gdsys,super-usb");
639 ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
640 ofnode_write_string(node, "compatible", "sandbox,usb");
641 ut_assert(device_is_compatible(dev, "sandbox,usb"));
642
643 /* Test setting generic properties */
644
645 /* Non-existent in DTB */
646 ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
647 /* reg = 0x42, size = 0x100 */
648 ut_assertok(ofnode_write_prop(node, "reg", 8,
649 "\x00\x00\x00\x42\x00\x00\x01\x00"));
650 ut_asserteq(0x42, dev_read_addr(dev));
651
652 /* Test disabling devices */
653
654 device_remove(dev, DM_REMOVE_NORMAL);
655 device_unbind(dev);
656
657 ut_assert(of_device_is_available(ofnode_to_np(node)));
658 ofnode_set_enabled(node, false);
659 ut_assert(!of_device_is_available(ofnode_to_np(node)));
660
661 return 0;
662}
663DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Mario Six172942a2018-06-26 08:46:51 +0200664
665static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
666{
667 ofnode node;
668
669 if (!of_live_active()) {
670 printf("Live tree not active; ignore test\n");
671 return 0;
672 }
673
674 node = ofnode_path("/usb@2");
675
676 /* Test enabling devices */
677
678 ut_assert(!of_device_is_available(ofnode_to_np(node)));
679 dev_enable_by_path("/usb@2");
680 ut_assert(of_device_is_available(ofnode_to_np(node)));
681
682 /* Test disabling devices */
683
684 ut_assert(of_device_is_available(ofnode_to_np(node)));
685 dev_disable_by_path("/usb@2");
686 ut_assert(!of_device_is_available(ofnode_to_np(node)));
687
688 return 0;
689}
690DM_TEST(dm_test_fdt_disable_enable_by_path, DM_TESTF_SCAN_PDATA |
691 DM_TESTF_SCAN_FDT);
Simon Glassd0b4f682018-11-18 08:14:30 -0700692
693/* Test a few uclass phandle functions */
694static int dm_test_fdt_phandle(struct unit_test_state *uts)
695{
696 struct udevice *back, *dev, *dev2;
697
698 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
699 ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
700 back, "missing", &dev));
701 ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
702 "power-supply", &dev));
703 ut_asserteq(0, device_active(dev));
704 ut_asserteq_str("ldo1", dev->name);
705 ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
706 "power-supply", &dev2));
707 ut_asserteq_ptr(dev, dev2);
708
709 return 0;
710}
711DM_TEST(dm_test_fdt_phandle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass3abe1112018-11-18 08:14:31 -0700712
713/* Test device_find_first_child_by_uclass() */
714static int dm_test_first_child(struct unit_test_state *uts)
715{
716 struct udevice *i2c, *dev, *dev2;
717
718 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
719 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
720 ut_asserteq_str("rtc@43", dev->name);
721 ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
722 ut_asserteq_ptr(dev, dev2);
723 ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
724 ut_asserteq_str("rtc@61", dev2->name);
725
726 ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
727 &dev));
728 ut_asserteq_str("eeprom@2c", dev->name);
729 ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
730 ut_asserteq_ptr(dev, dev2);
731
732 ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
733 UCLASS_VIDEO, &dev));
734 ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
735
736 return 0;
737}
738DM_TEST(dm_test_first_child, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);