Merge https://gitlab.denx.de/u-boot/custodians/u-boot-spi into next
[platform/kernel/u-boot.git] / test / dm / test-fdt.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  */
5
6 #include <common.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <fdtdec.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <asm/io.h>
13 #include <dm/test.h>
14 #include <dm/root.h>
15 #include <dm/device-internal.h>
16 #include <dm/devres.h>
17 #include <dm/uclass-internal.h>
18 #include <dm/util.h>
19 #include <dm/lists.h>
20 #include <dm/of_access.h>
21 #include <test/ut.h>
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
26 {
27         const struct dm_test_pdata *pdata = dev->platdata;
28         struct dm_test_priv *priv = dev_get_priv(dev);
29
30         *pingret = pingval + pdata->ping_add;
31         priv->ping_total += *pingret;
32
33         return 0;
34 }
35
36 static const struct test_ops test_ops = {
37         .ping = testfdt_drv_ping,
38 };
39
40 static int testfdt_ofdata_to_platdata(struct udevice *dev)
41 {
42         struct dm_test_pdata *pdata = dev_get_platdata(dev);
43
44         pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
45                                         "ping-add", -1);
46         pdata->base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
47                                       "ping-expect");
48
49         return 0;
50 }
51
52 static int testfdt_drv_probe(struct udevice *dev)
53 {
54         struct dm_test_priv *priv = dev_get_priv(dev);
55
56         priv->ping_total += DM_TEST_START_TOTAL;
57
58         /*
59          * If this device is on a bus, the uclass_flag will be set before
60          * calling this function. In the meantime the uclass_postp is
61          * initlized to a value -1. These are used respectively by
62          * dm_test_bus_child_pre_probe_uclass() and
63          * dm_test_bus_child_post_probe_uclass().
64          */
65         priv->uclass_total += priv->uclass_flag;
66         priv->uclass_postp = -1;
67
68         return 0;
69 }
70
71 static const struct udevice_id testfdt_ids[] = {
72         {
73                 .compatible = "denx,u-boot-fdt-test",
74                 .data = DM_TEST_TYPE_FIRST },
75         {
76                 .compatible = "google,another-fdt-test",
77                 .data = DM_TEST_TYPE_SECOND },
78         { }
79 };
80
81 U_BOOT_DRIVER(testfdt_drv) = {
82         .name   = "testfdt_drv",
83         .of_match       = testfdt_ids,
84         .id     = UCLASS_TEST_FDT,
85         .ofdata_to_platdata = testfdt_ofdata_to_platdata,
86         .probe  = testfdt_drv_probe,
87         .ops    = &test_ops,
88         .priv_auto_alloc_size = sizeof(struct dm_test_priv),
89         .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
90 };
91
92 static const struct udevice_id testfdt1_ids[] = {
93         {
94                 .compatible = "denx,u-boot-fdt-test1",
95                 .data = DM_TEST_TYPE_FIRST },
96         { }
97 };
98
99 U_BOOT_DRIVER(testfdt1_drv) = {
100         .name   = "testfdt1_drv",
101         .of_match       = testfdt1_ids,
102         .id     = UCLASS_TEST_FDT,
103         .ofdata_to_platdata = testfdt_ofdata_to_platdata,
104         .probe  = testfdt_drv_probe,
105         .ops    = &test_ops,
106         .priv_auto_alloc_size = sizeof(struct dm_test_priv),
107         .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
108         .flags = DM_FLAG_PRE_RELOC,
109 };
110
111 /* From here is the testfdt uclass code */
112 int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
113 {
114         const struct test_ops *ops = device_get_ops(dev);
115
116         if (!ops->ping)
117                 return -ENOSYS;
118
119         return ops->ping(dev, pingval, pingret);
120 }
121
122 UCLASS_DRIVER(testfdt) = {
123         .name           = "testfdt",
124         .id             = UCLASS_TEST_FDT,
125         .flags          = DM_UC_FLAG_SEQ_ALIAS,
126 };
127
128 struct dm_testprobe_pdata {
129         int probe_err;
130 };
131
132 static int testprobe_drv_probe(struct udevice *dev)
133 {
134         struct dm_testprobe_pdata *pdata = dev_get_platdata(dev);
135
136         return pdata->probe_err;
137 }
138
139 static const struct udevice_id testprobe_ids[] = {
140         { .compatible = "denx,u-boot-probe-test" },
141         { }
142 };
143
144 U_BOOT_DRIVER(testprobe_drv) = {
145         .name   = "testprobe_drv",
146         .of_match       = testprobe_ids,
147         .id     = UCLASS_TEST_PROBE,
148         .probe  = testprobe_drv_probe,
149         .platdata_auto_alloc_size       = sizeof(struct dm_testprobe_pdata),
150 };
151
152 UCLASS_DRIVER(testprobe) = {
153         .name           = "testprobe",
154         .id             = UCLASS_TEST_PROBE,
155         .flags          = DM_UC_FLAG_SEQ_ALIAS,
156 };
157
158 struct dm_testdevres_pdata {
159         void *ptr;
160 };
161
162 struct dm_testdevres_priv {
163         void *ptr;
164         void *ptr_ofdata;
165 };
166
167 static int testdevres_drv_bind(struct udevice *dev)
168 {
169         struct dm_testdevres_pdata *pdata = dev_get_platdata(dev);
170
171         pdata->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE, 0);
172
173         return 0;
174 }
175
176 static int testdevres_drv_ofdata_to_platdata(struct udevice *dev)
177 {
178         struct dm_testdevres_priv *priv = dev_get_priv(dev);
179
180         priv->ptr_ofdata = devm_kmalloc(dev, TEST_DEVRES_SIZE3, 0);
181
182         return 0;
183 }
184
185 static int testdevres_drv_probe(struct udevice *dev)
186 {
187         struct dm_testdevres_priv *priv = dev_get_priv(dev);
188
189         priv->ptr = devm_kmalloc(dev, TEST_DEVRES_SIZE2, 0);
190
191         return 0;
192 }
193
194 static const struct udevice_id testdevres_ids[] = {
195         { .compatible = "denx,u-boot-devres-test" },
196         { }
197 };
198
199 U_BOOT_DRIVER(testdevres_drv) = {
200         .name   = "testdevres_drv",
201         .of_match       = testdevres_ids,
202         .id     = UCLASS_TEST_DEVRES,
203         .bind   = testdevres_drv_bind,
204         .ofdata_to_platdata     = testdevres_drv_ofdata_to_platdata,
205         .probe  = testdevres_drv_probe,
206         .platdata_auto_alloc_size       = sizeof(struct dm_testdevres_pdata),
207         .priv_auto_alloc_size   = sizeof(struct dm_testdevres_priv),
208 };
209
210 UCLASS_DRIVER(testdevres) = {
211         .name           = "testdevres",
212         .id             = UCLASS_TEST_DEVRES,
213         .flags          = DM_UC_FLAG_SEQ_ALIAS,
214 };
215
216 int dm_check_devices(struct unit_test_state *uts, int num_devices)
217 {
218         struct udevice *dev;
219         int ret;
220         int i;
221
222         /*
223          * Now check that the ping adds are what we expect. This is using the
224          * ping-add property in each node.
225          */
226         for (i = 0; i < num_devices; i++) {
227                 uint32_t base;
228
229                 ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
230                 ut_assert(!ret);
231
232                 /*
233                  * Get the 'ping-expect' property, which tells us what the
234                  * ping add should be. We don't use the platdata because we
235                  * want to test the code that sets that up
236                  * (testfdt_drv_probe()).
237                  */
238                 base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev),
239                                        "ping-expect");
240                 debug("dev=%d, base=%d: %s\n", i, base,
241                       fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL));
242
243                 ut_assert(!dm_check_operations(uts, dev, base,
244                                                dev_get_priv(dev)));
245         }
246
247         return 0;
248 }
249
250 /* Test that FDT-based binding works correctly */
251 static int dm_test_fdt(struct unit_test_state *uts)
252 {
253         const int num_devices = 8;
254         struct udevice *dev;
255         struct uclass *uc;
256         int ret;
257         int i;
258
259         ret = dm_extended_scan_fdt(gd->fdt_blob, false);
260         ut_assert(!ret);
261
262         ret = uclass_get(UCLASS_TEST_FDT, &uc);
263         ut_assert(!ret);
264
265         /* These are num_devices compatible root-level device tree nodes */
266         ut_asserteq(num_devices, list_count_items(&uc->dev_head));
267
268         /* Each should have platform data but no private data */
269         for (i = 0; i < num_devices; i++) {
270                 ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
271                 ut_assert(!ret);
272                 ut_assert(!dev_get_priv(dev));
273                 ut_assert(dev->platdata);
274         }
275
276         ut_assertok(dm_check_devices(uts, num_devices));
277
278         return 0;
279 }
280 DM_TEST(dm_test_fdt, 0);
281
282 static int dm_test_alias_highest_id(struct unit_test_state *uts)
283 {
284         int ret;
285
286         ret = dev_read_alias_highest_id("eth");
287         ut_asserteq(5, ret);
288
289         ret = dev_read_alias_highest_id("gpio");
290         ut_asserteq(3, ret);
291
292         ret = dev_read_alias_highest_id("pci");
293         ut_asserteq(2, ret);
294
295         ret = dev_read_alias_highest_id("i2c");
296         ut_asserteq(0, ret);
297
298         ret = dev_read_alias_highest_id("deadbeef");
299         ut_asserteq(-1, ret);
300
301         return 0;
302 }
303 DM_TEST(dm_test_alias_highest_id, 0);
304
305 static int dm_test_fdt_pre_reloc(struct unit_test_state *uts)
306 {
307         struct uclass *uc;
308         int ret;
309
310         ret = dm_scan_fdt(gd->fdt_blob, true);
311         ut_assert(!ret);
312
313         ret = uclass_get(UCLASS_TEST_FDT, &uc);
314         ut_assert(!ret);
315
316         /*
317          * These are 2 pre-reloc devices:
318          * one with "u-boot,dm-pre-reloc" property (a-test node), and the other
319          * one whose driver marked with DM_FLAG_PRE_RELOC flag (h-test node).
320          */
321         ut_asserteq(2, list_count_items(&uc->dev_head));
322
323         return 0;
324 }
325 DM_TEST(dm_test_fdt_pre_reloc, 0);
326
327 /* Test that sequence numbers are allocated properly */
328 static int dm_test_fdt_uclass_seq(struct unit_test_state *uts)
329 {
330         struct udevice *dev;
331
332         /* A few basic santiy tests */
333         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
334         ut_asserteq_str("b-test", dev->name);
335
336         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 8, true, &dev));
337         ut_asserteq_str("a-test", dev->name);
338
339         ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
340                                                        true, &dev));
341         ut_asserteq_ptr(NULL, dev);
342
343         /* Test aliases */
344         ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
345         ut_asserteq_str("e-test", dev->name);
346
347         ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
348                                                        true, &dev));
349
350         /*
351          * Note that c-test nodes are not probed since it is not a top-level
352          * node
353          */
354         ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
355         ut_asserteq_str("b-test", dev->name);
356
357         /*
358          * d-test wants sequence number 3 also, but it can't have it because
359          * b-test gets it first.
360          */
361         ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
362         ut_asserteq_str("d-test", dev->name);
363
364         /*
365          * d-test actually gets 9, because thats the next free one after the
366          * aliases.
367          */
368         ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 9, &dev));
369         ut_asserteq_str("d-test", dev->name);
370
371         /* initially no one wants seq 10 */
372         ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 10,
373                                                       &dev));
374         ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
375         ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 4, &dev));
376
377         /* But now that it is probed, we can find it */
378         ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 10, &dev));
379         ut_asserteq_str("f-test", dev->name);
380
381         /*
382          * And we should still have holes in our sequence numbers, that is 2
383          * and 4 should not be used.
384          */
385         ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 2,
386                                                        true, &dev));
387         ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 4,
388                                                        true, &dev));
389
390         return 0;
391 }
392 DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
393
394 /* Test that we can find a device by device tree offset */
395 static int dm_test_fdt_offset(struct unit_test_state *uts)
396 {
397         const void *blob = gd->fdt_blob;
398         struct udevice *dev;
399         int node;
400
401         node = fdt_path_offset(blob, "/e-test");
402         ut_assert(node > 0);
403         ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
404                                                    &dev));
405         ut_asserteq_str("e-test", dev->name);
406
407         /* This node should not be bound */
408         node = fdt_path_offset(blob, "/junk");
409         ut_assert(node > 0);
410         ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
411                                                             node, &dev));
412
413         /* This is not a top level node so should not be probed */
414         node = fdt_path_offset(blob, "/some-bus/c-test@5");
415         ut_assert(node > 0);
416         ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
417                                                             node, &dev));
418
419         return 0;
420 }
421 DM_TEST(dm_test_fdt_offset,
422         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
423
424 /**
425  * Test various error conditions with uclass_first_device() and
426  * uclass_next_device()
427  */
428 static int dm_test_first_next_device(struct unit_test_state *uts)
429 {
430         struct dm_testprobe_pdata *pdata;
431         struct udevice *dev, *parent = NULL;
432         int count;
433         int ret;
434
435         /* There should be 4 devices */
436         for (ret = uclass_first_device(UCLASS_TEST_PROBE, &dev), count = 0;
437              dev;
438              ret = uclass_next_device(&dev)) {
439                 count++;
440                 parent = dev_get_parent(dev);
441                 }
442         ut_assertok(ret);
443         ut_asserteq(4, count);
444
445         /* Remove them and try again, with an error on the second one */
446         ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 1, &dev));
447         pdata = dev_get_platdata(dev);
448         pdata->probe_err = -ENOMEM;
449         device_remove(parent, DM_REMOVE_NORMAL);
450         ut_assertok(uclass_first_device(UCLASS_TEST_PROBE, &dev));
451         ut_asserteq(-ENOMEM, uclass_next_device(&dev));
452         ut_asserteq_ptr(dev, NULL);
453
454         /* Now an error on the first one */
455         ut_assertok(uclass_get_device(UCLASS_TEST_PROBE, 0, &dev));
456         pdata = dev_get_platdata(dev);
457         pdata->probe_err = -ENOENT;
458         device_remove(parent, DM_REMOVE_NORMAL);
459         ut_asserteq(-ENOENT, uclass_first_device(UCLASS_TEST_PROBE, &dev));
460
461         return 0;
462 }
463 DM_TEST(dm_test_first_next_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
464
465 /* Test iteration through devices in a uclass */
466 static int dm_test_uclass_foreach(struct unit_test_state *uts)
467 {
468         struct udevice *dev;
469         struct uclass *uc;
470         int count;
471
472         count = 0;
473         uclass_id_foreach_dev(UCLASS_TEST_FDT, dev, uc)
474                 count++;
475         ut_asserteq(8, count);
476
477         count = 0;
478         uclass_foreach_dev(dev, uc)
479                 count++;
480         ut_asserteq(8, count);
481
482         return 0;
483 }
484 DM_TEST(dm_test_uclass_foreach, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
485
486 /**
487  * check_devices() - Check return values and pointers
488  *
489  * This runs through a full sequence of uclass_first_device_check()...
490  * uclass_next_device_check() checking that the return values and devices
491  * are correct.
492  *
493  * @uts: Test state
494  * @devlist: List of expected devices
495  * @mask: Indicates which devices should return an error. Device n should
496  *        return error (-NOENT - n) if bit n is set, or no error (i.e. 0) if
497  *        bit n is clear.
498  */
499 static int check_devices(struct unit_test_state *uts,
500                          struct udevice *devlist[], int mask)
501 {
502         int expected_ret;
503         struct udevice *dev;
504         int i;
505
506         expected_ret = (mask & 1) ? -ENOENT : 0;
507         mask >>= 1;
508         ut_asserteq(expected_ret,
509                     uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
510         for (i = 0; i < 4; i++) {
511                 ut_asserteq_ptr(devlist[i], dev);
512                 expected_ret = (mask & 1) ? -ENOENT - (i + 1) : 0;
513                 mask >>= 1;
514                 ut_asserteq(expected_ret, uclass_next_device_check(&dev));
515         }
516         ut_asserteq_ptr(NULL, dev);
517
518         return 0;
519 }
520
521 /* Test uclass_first_device_check() and uclass_next_device_check() */
522 static int dm_test_first_next_ok_device(struct unit_test_state *uts)
523 {
524         struct dm_testprobe_pdata *pdata;
525         struct udevice *dev, *parent = NULL, *devlist[4];
526         int count;
527         int ret;
528
529         /* There should be 4 devices */
530         count = 0;
531         for (ret = uclass_first_device_check(UCLASS_TEST_PROBE, &dev);
532              dev;
533              ret = uclass_next_device_check(&dev)) {
534                 ut_assertok(ret);
535                 devlist[count++] = dev;
536                 parent = dev_get_parent(dev);
537                 }
538         ut_asserteq(4, count);
539         ut_assertok(uclass_first_device_check(UCLASS_TEST_PROBE, &dev));
540         ut_assertok(check_devices(uts, devlist, 0));
541
542         /* Remove them and try again, with an error on the second one */
543         pdata = dev_get_platdata(devlist[1]);
544         pdata->probe_err = -ENOENT - 1;
545         device_remove(parent, DM_REMOVE_NORMAL);
546         ut_assertok(check_devices(uts, devlist, 1 << 1));
547
548         /* Now an error on the first one */
549         pdata = dev_get_platdata(devlist[0]);
550         pdata->probe_err = -ENOENT - 0;
551         device_remove(parent, DM_REMOVE_NORMAL);
552         ut_assertok(check_devices(uts, devlist, 3 << 0));
553
554         /* Now errors on all */
555         pdata = dev_get_platdata(devlist[2]);
556         pdata->probe_err = -ENOENT - 2;
557         pdata = dev_get_platdata(devlist[3]);
558         pdata->probe_err = -ENOENT - 3;
559         device_remove(parent, DM_REMOVE_NORMAL);
560         ut_assertok(check_devices(uts, devlist, 0xf << 0));
561
562         return 0;
563 }
564 DM_TEST(dm_test_first_next_ok_device, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
565
566 static const struct udevice_id fdt_dummy_ids[] = {
567         { .compatible = "denx,u-boot-fdt-dummy", },
568         { }
569 };
570
571 UCLASS_DRIVER(fdt_dummy) = {
572         .name           = "fdt-dummy",
573         .id             = UCLASS_TEST_DUMMY,
574         .flags          = DM_UC_FLAG_SEQ_ALIAS,
575 };
576
577 U_BOOT_DRIVER(fdt_dummy_drv) = {
578         .name   = "fdt_dummy_drv",
579         .of_match       = fdt_dummy_ids,
580         .id     = UCLASS_TEST_DUMMY,
581 };
582
583 static int dm_test_fdt_translation(struct unit_test_state *uts)
584 {
585         struct udevice *dev;
586         fdt32_t dma_addr[2];
587
588         /* Some simple translations */
589         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
590         ut_asserteq_str("dev@0,0", dev->name);
591         ut_asserteq(0x8000, dev_read_addr(dev));
592
593         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
594         ut_asserteq_str("dev@1,100", dev->name);
595         ut_asserteq(0x9000, dev_read_addr(dev));
596
597         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 2, true, &dev));
598         ut_asserteq_str("dev@2,200", dev->name);
599         ut_asserteq(0xA000, dev_read_addr(dev));
600
601         /* No translation for busses with #size-cells == 0 */
602         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 3, true, &dev));
603         ut_asserteq_str("dev@42", dev->name);
604         ut_asserteq(0x42, dev_read_addr(dev));
605
606         /* dma address translation */
607         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
608         dma_addr[0] = cpu_to_be32(0);
609         dma_addr[1] = cpu_to_be32(0);
610         ut_asserteq(0x10000000, dev_translate_dma_address(dev, dma_addr));
611
612         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 1, true, &dev));
613         dma_addr[0] = cpu_to_be32(1);
614         dma_addr[1] = cpu_to_be32(0x100);
615         ut_asserteq(0x20000000, dev_translate_dma_address(dev, dma_addr));
616
617         return 0;
618 }
619 DM_TEST(dm_test_fdt_translation, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
620
621 static int dm_test_fdt_remap_addr_flat(struct unit_test_state *uts)
622 {
623         struct udevice *dev;
624         fdt_addr_t addr;
625         void *paddr;
626
627         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
628
629         addr = devfdt_get_addr(dev);
630         ut_asserteq(0x8000, addr);
631
632         paddr = map_physmem(addr, 0, MAP_NOCACHE);
633         ut_assertnonnull(paddr);
634         ut_asserteq_ptr(paddr, devfdt_remap_addr(dev));
635
636         return 0;
637 }
638 DM_TEST(dm_test_fdt_remap_addr_flat,
639         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
640
641 static int dm_test_fdt_remap_addr_index_flat(struct unit_test_state *uts)
642 {
643         struct udevice *dev;
644         fdt_addr_t addr;
645         fdt_size_t size;
646         void *paddr;
647
648         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
649
650         addr = devfdt_get_addr_size_index(dev, 0, &size);
651         ut_asserteq(0x8000, addr);
652         ut_asserteq(0x1000, size);
653
654         paddr = map_physmem(addr, 0, MAP_NOCACHE);
655         ut_assertnonnull(paddr);
656         ut_asserteq_ptr(paddr, devfdt_remap_addr_index(dev, 0));
657
658         return 0;
659 }
660 DM_TEST(dm_test_fdt_remap_addr_index_flat,
661         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
662
663 static int dm_test_fdt_remap_addr_name_flat(struct unit_test_state *uts)
664 {
665         struct udevice *dev;
666         fdt_addr_t addr;
667         fdt_size_t size;
668         void *paddr;
669
670         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
671
672         addr = devfdt_get_addr_size_name(dev, "sandbox-dummy-0", &size);
673         ut_asserteq(0x8000, addr);
674         ut_asserteq(0x1000, size);
675
676         paddr = map_physmem(addr, 0, MAP_NOCACHE);
677         ut_assertnonnull(paddr);
678         ut_asserteq_ptr(paddr, devfdt_remap_addr_name(dev, "sandbox-dummy-0"));
679
680         return 0;
681 }
682 DM_TEST(dm_test_fdt_remap_addr_name_flat,
683         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
684
685 static int dm_test_fdt_remap_addr_live(struct unit_test_state *uts)
686 {
687         struct udevice *dev;
688         fdt_addr_t addr;
689         void *paddr;
690
691         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
692
693         addr = dev_read_addr(dev);
694         ut_asserteq(0x8000, addr);
695
696         paddr = map_physmem(addr, 0, MAP_NOCACHE);
697         ut_assertnonnull(paddr);
698         ut_asserteq_ptr(paddr, dev_remap_addr(dev));
699
700         return 0;
701 }
702 DM_TEST(dm_test_fdt_remap_addr_live,
703         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
704
705 static int dm_test_fdt_remap_addr_index_live(struct unit_test_state *uts)
706 {
707         struct udevice *dev;
708         fdt_addr_t addr;
709         fdt_size_t size;
710         void *paddr;
711
712         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
713
714         addr = dev_read_addr_size_index(dev, 0, &size);
715         ut_asserteq(0x8000, addr);
716         ut_asserteq(0x1000, size);
717
718         paddr = map_physmem(addr, 0, MAP_NOCACHE);
719         ut_assertnonnull(paddr);
720         ut_asserteq_ptr(paddr, dev_remap_addr_index(dev, 0));
721
722         return 0;
723 }
724 DM_TEST(dm_test_fdt_remap_addr_index_live,
725         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
726
727 static int dm_test_fdt_remap_addr_name_live(struct unit_test_state *uts)
728 {
729         struct udevice *dev;
730         fdt_addr_t addr;
731         fdt_size_t size;
732         void *paddr;
733
734         ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_DUMMY, 0, true, &dev));
735
736         addr = dev_read_addr_size_name(dev, "sandbox-dummy-0", &size);
737         ut_asserteq(0x8000, addr);
738         ut_asserteq(0x1000, size);
739
740         paddr = map_physmem(addr, 0, MAP_NOCACHE);
741         ut_assertnonnull(paddr);
742         ut_asserteq_ptr(paddr, dev_remap_addr_name(dev, "sandbox-dummy-0"));
743
744         return 0;
745 }
746 DM_TEST(dm_test_fdt_remap_addr_name_live,
747         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
748
749 static int dm_test_fdt_livetree_writing(struct unit_test_state *uts)
750 {
751         struct udevice *dev;
752         ofnode node;
753
754         if (!of_live_active()) {
755                 printf("Live tree not active; ignore test\n");
756                 return 0;
757         }
758
759         /* Test enabling devices */
760
761         node = ofnode_path("/usb@2");
762
763         ut_assert(!of_device_is_available(ofnode_to_np(node)));
764         ofnode_set_enabled(node, true);
765         ut_assert(of_device_is_available(ofnode_to_np(node)));
766
767         device_bind_driver_to_node(dm_root(), "usb_sandbox", "usb@2", node,
768                                    &dev);
769         ut_assertok(uclass_find_device_by_seq(UCLASS_USB, 2, true, &dev));
770
771         /* Test string property setting */
772
773         ut_assert(device_is_compatible(dev, "sandbox,usb"));
774         ofnode_write_string(node, "compatible", "gdsys,super-usb");
775         ut_assert(device_is_compatible(dev, "gdsys,super-usb"));
776         ofnode_write_string(node, "compatible", "sandbox,usb");
777         ut_assert(device_is_compatible(dev, "sandbox,usb"));
778
779         /* Test setting generic properties */
780
781         /* Non-existent in DTB */
782         ut_asserteq(FDT_ADDR_T_NONE, dev_read_addr(dev));
783         /* reg = 0x42, size = 0x100 */
784         ut_assertok(ofnode_write_prop(node, "reg", 8,
785                                       "\x00\x00\x00\x42\x00\x00\x01\x00"));
786         ut_asserteq(0x42, dev_read_addr(dev));
787
788         /* Test disabling devices */
789
790         device_remove(dev, DM_REMOVE_NORMAL);
791         device_unbind(dev);
792
793         ut_assert(of_device_is_available(ofnode_to_np(node)));
794         ofnode_set_enabled(node, false);
795         ut_assert(!of_device_is_available(ofnode_to_np(node)));
796
797         return 0;
798 }
799 DM_TEST(dm_test_fdt_livetree_writing, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
800
801 static int dm_test_fdt_disable_enable_by_path(struct unit_test_state *uts)
802 {
803         ofnode node;
804
805         if (!of_live_active()) {
806                 printf("Live tree not active; ignore test\n");
807                 return 0;
808         }
809
810         node = ofnode_path("/usb@2");
811
812         /* Test enabling devices */
813
814         ut_assert(!of_device_is_available(ofnode_to_np(node)));
815         dev_enable_by_path("/usb@2");
816         ut_assert(of_device_is_available(ofnode_to_np(node)));
817
818         /* Test disabling devices */
819
820         ut_assert(of_device_is_available(ofnode_to_np(node)));
821         dev_disable_by_path("/usb@2");
822         ut_assert(!of_device_is_available(ofnode_to_np(node)));
823
824         return 0;
825 }
826 DM_TEST(dm_test_fdt_disable_enable_by_path, DM_TESTF_SCAN_PDATA |
827                                             DM_TESTF_SCAN_FDT);
828
829 /* Test a few uclass phandle functions */
830 static int dm_test_fdt_phandle(struct unit_test_state *uts)
831 {
832         struct udevice *back, *dev, *dev2;
833
834         ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &back));
835         ut_asserteq(-ENOENT, uclass_find_device_by_phandle(UCLASS_REGULATOR,
836                                                         back, "missing", &dev));
837         ut_assertok(uclass_find_device_by_phandle(UCLASS_REGULATOR, back,
838                                                   "power-supply", &dev));
839         ut_asserteq(0, device_active(dev));
840         ut_asserteq_str("ldo1", dev->name);
841         ut_assertok(uclass_get_device_by_phandle(UCLASS_REGULATOR, back,
842                                                  "power-supply", &dev2));
843         ut_asserteq_ptr(dev, dev2);
844
845         return 0;
846 }
847 DM_TEST(dm_test_fdt_phandle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
848
849 /* Test device_find_first_child_by_uclass() */
850 static int dm_test_first_child(struct unit_test_state *uts)
851 {
852         struct udevice *i2c, *dev, *dev2;
853
854         ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
855         ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_RTC, &dev));
856         ut_asserteq_str("rtc@43", dev->name);
857         ut_assertok(device_find_child_by_name(i2c, "rtc@43", &dev2));
858         ut_asserteq_ptr(dev, dev2);
859         ut_assertok(device_find_child_by_name(i2c, "rtc@61", &dev2));
860         ut_asserteq_str("rtc@61", dev2->name);
861
862         ut_assertok(device_find_first_child_by_uclass(i2c, UCLASS_I2C_EEPROM,
863                                                       &dev));
864         ut_asserteq_str("eeprom@2c", dev->name);
865         ut_assertok(device_find_child_by_name(i2c, "eeprom@2c", &dev2));
866         ut_asserteq_ptr(dev, dev2);
867
868         ut_asserteq(-ENODEV, device_find_first_child_by_uclass(i2c,
869                                                         UCLASS_VIDEO, &dev));
870         ut_asserteq(-ENODEV, device_find_child_by_name(i2c, "missing", &dev));
871
872         return 0;
873 }
874 DM_TEST(dm_test_first_child, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
875
876 /* Test integer functions in dm_read_...() */
877 static int dm_test_read_int(struct unit_test_state *uts)
878 {
879         struct udevice *dev;
880         u32 val32;
881         s32 sval;
882         uint val;
883         u64 val64;
884
885         ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
886         ut_asserteq_str("a-test", dev->name);
887         ut_assertok(dev_read_u32(dev, "int-value", &val32));
888         ut_asserteq(1234, val32);
889
890         ut_asserteq(-EINVAL, dev_read_u32(dev, "missing", &val32));
891         ut_asserteq(6, dev_read_u32_default(dev, "missing", 6));
892
893         ut_asserteq(1234, dev_read_u32_default(dev, "int-value", 6));
894         ut_asserteq(1234, val32);
895
896         ut_asserteq(-EINVAL, dev_read_s32(dev, "missing", &sval));
897         ut_asserteq(6, dev_read_s32_default(dev, "missing", 6));
898
899         ut_asserteq(-1234, dev_read_s32_default(dev, "uint-value", 6));
900         ut_assertok(dev_read_s32(dev, "uint-value", &sval));
901         ut_asserteq(-1234, sval);
902
903         val = 0;
904         ut_asserteq(-EINVAL, dev_read_u32u(dev, "missing", &val));
905         ut_assertok(dev_read_u32u(dev, "uint-value", &val));
906         ut_asserteq(-1234, val);
907
908         ut_assertok(dev_read_u64(dev, "int64-value", &val64));
909         ut_asserteq_64(0x1111222233334444, val64);
910
911         ut_asserteq_64(-EINVAL, dev_read_u64(dev, "missing", &val64));
912         ut_asserteq_64(6, dev_read_u64_default(dev, "missing", 6));
913
914         ut_asserteq_64(0x1111222233334444,
915                        dev_read_u64_default(dev, "int64-value", 6));
916
917         return 0;
918 }
919 DM_TEST(dm_test_read_int, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
920
921 static int dm_test_read_int_index(struct unit_test_state *uts)
922 {
923         struct udevice *dev;
924         u32 val32;
925
926         ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
927         ut_asserteq_str("a-test", dev->name);
928
929         ut_asserteq(-EINVAL, dev_read_u32_index(dev, "missing", 0, &val32));
930         ut_asserteq(19, dev_read_u32_index_default(dev, "missing", 0, 19));
931
932         ut_assertok(dev_read_u32_index(dev, "int-array", 0, &val32));
933         ut_asserteq(5678, val32);
934         ut_assertok(dev_read_u32_index(dev, "int-array", 1, &val32));
935         ut_asserteq(9123, val32);
936         ut_assertok(dev_read_u32_index(dev, "int-array", 2, &val32));
937         ut_asserteq(4567, val32);
938         ut_asserteq(-EOVERFLOW, dev_read_u32_index(dev, "int-array", 3,
939                                                    &val32));
940
941         ut_asserteq(5678, dev_read_u32_index_default(dev, "int-array", 0, 2));
942         ut_asserteq(9123, dev_read_u32_index_default(dev, "int-array", 1, 2));
943         ut_asserteq(4567, dev_read_u32_index_default(dev, "int-array", 2, 2));
944         ut_asserteq(2, dev_read_u32_index_default(dev, "int-array", 3, 2));
945
946         return 0;
947 }
948 DM_TEST(dm_test_read_int_index, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
949
950 /* Test iteration through devices by drvdata */
951 static int dm_test_uclass_drvdata(struct unit_test_state *uts)
952 {
953         struct udevice *dev;
954
955         ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
956                                                 DM_TEST_TYPE_FIRST, &dev));
957         ut_asserteq_str("a-test", dev->name);
958
959         ut_assertok(uclass_first_device_drvdata(UCLASS_TEST_FDT,
960                                                 DM_TEST_TYPE_SECOND, &dev));
961         ut_asserteq_str("d-test", dev->name);
962
963         ut_asserteq(-ENODEV, uclass_first_device_drvdata(UCLASS_TEST_FDT,
964                                                          DM_TEST_TYPE_COUNT,
965                                                          &dev));
966
967         return 0;
968 }
969 DM_TEST(dm_test_uclass_drvdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
970
971 /* Test device_first_child_ofdata_err(), etc. */
972 static int dm_test_child_ofdata(struct unit_test_state *uts)
973 {
974         struct udevice *bus, *dev;
975         int count;
976
977         ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
978         count = 0;
979         device_foreach_child_ofdata_to_platdata(dev, bus) {
980                 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
981                 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
982                 count++;
983         }
984         ut_asserteq(3, count);
985
986         return 0;
987 }
988 DM_TEST(dm_test_child_ofdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
989
990 /* Test device_first_child_err(), etc. */
991 static int dm_test_first_child_probe(struct unit_test_state *uts)
992 {
993         struct udevice *bus, *dev;
994         int count;
995
996         ut_assertok(uclass_first_device_err(UCLASS_TEST_BUS, &bus));
997         count = 0;
998         device_foreach_child_probe(dev, bus) {
999                 ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1000                 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
1001                 count++;
1002         }
1003         ut_asserteq(3, count);
1004
1005         return 0;
1006 }
1007 DM_TEST(dm_test_first_child_probe, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
1008
1009 /* Test that ofdata is read for parents before children */
1010 static int dm_test_ofdata_order(struct unit_test_state *uts)
1011 {
1012         struct udevice *bus, *dev;
1013
1014         ut_assertok(uclass_find_first_device(UCLASS_I2C, &bus));
1015         ut_assertnonnull(bus);
1016         ut_assert(!(bus->flags & DM_FLAG_PLATDATA_VALID));
1017
1018         ut_assertok(device_find_first_child(bus, &dev));
1019         ut_assertnonnull(dev);
1020         ut_assert(!(dev->flags & DM_FLAG_PLATDATA_VALID));
1021
1022         /* read the child's ofdata which should cause the parent's to be read */
1023         ut_assertok(device_ofdata_to_platdata(dev));
1024         ut_assert(dev->flags & DM_FLAG_PLATDATA_VALID);
1025         ut_assert(bus->flags & DM_FLAG_PLATDATA_VALID);
1026
1027         ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
1028         ut_assert(!(bus->flags & DM_FLAG_ACTIVATED));
1029
1030         return 0;
1031 }
1032 DM_TEST(dm_test_ofdata_order, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);