blob: e3fa01afddfd66fe42370c615e8bb55ca85c98c9 [file] [log] [blame]
Simon Glass25dc2e92020-10-25 20:38:29 -06001// SPDX-License-Identifier: GPL-2.0+
2
3#include <common.h>
4#include <dm.h>
Simon Glass64401e72020-10-03 11:31:28 -06005#include <dt-structs.h>
Simon Glass25dc2e92020-10-25 20:38:29 -06006#include <dm/test.h>
7#include <test/test.h>
8#include <test/ut.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass25dc2e92020-10-25 20:38:29 -060010
11/* Test that we can find a device using of-platdata */
Simon Glassb75b15b2020-12-03 16:55:23 -070012static int dm_test_of_plat_base(struct unit_test_state *uts)
Simon Glass25dc2e92020-10-25 20:38:29 -060013{
14 struct udevice *dev;
15
16 ut_assertok(uclass_first_device_err(UCLASS_SERIAL, &dev));
17 ut_asserteq_str("sandbox_serial", dev->name);
18
19 return 0;
20}
Simon Glassb75b15b2020-12-03 16:55:23 -070021DM_TEST(dm_test_of_plat_base, UT_TESTF_SCAN_PDATA);
Simon Glass64401e72020-10-03 11:31:28 -060022
23/* Test that we can read properties from a device */
Simon Glassb75b15b2020-12-03 16:55:23 -070024static int dm_test_of_plat_props(struct unit_test_state *uts)
Simon Glass64401e72020-10-03 11:31:28 -060025{
26 struct dtd_sandbox_spl_test *plat;
27 struct udevice *dev;
28 int i;
29
Simon Glass6b927b12020-10-03 11:31:32 -060030 /* Skip the clock */
Simon Glass64401e72020-10-03 11:31:28 -060031 ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
Simon Glass6b927b12020-10-03 11:31:32 -060032 ut_asserteq_str("sandbox_clk_test", dev->name);
33
34 ut_assertok(uclass_next_device_err(&dev));
Simon Glassfa20e932020-12-03 16:55:20 -070035 plat = dev_get_plat(dev);
Simon Glass64401e72020-10-03 11:31:28 -060036 ut_assert(plat->boolval);
37 ut_asserteq(1, plat->intval);
Simon Glassa7d66982021-07-28 19:23:10 -060038 ut_asserteq(3, ARRAY_SIZE(plat->intarray));
Simon Glass64401e72020-10-03 11:31:28 -060039 ut_asserteq(2, plat->intarray[0]);
40 ut_asserteq(3, plat->intarray[1]);
41 ut_asserteq(4, plat->intarray[2]);
Simon Glass64401e72020-10-03 11:31:28 -060042 ut_asserteq(5, plat->byteval);
43 ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
44 ut_asserteq(6, plat->bytearray[0]);
45 ut_asserteq(0, plat->bytearray[1]);
46 ut_asserteq(0, plat->bytearray[2]);
47 ut_asserteq(9, ARRAY_SIZE(plat->longbytearray));
48 for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
49 ut_asserteq(9 + i, plat->longbytearray[i]);
50 ut_asserteq_str("message", plat->stringval);
51 ut_asserteq(3, ARRAY_SIZE(plat->stringarray));
52 ut_asserteq_str("multi-word", plat->stringarray[0]);
53 ut_asserteq_str("message", plat->stringarray[1]);
54 ut_asserteq_str("", plat->stringarray[2]);
55
56 ut_assertok(uclass_next_device_err(&dev));
Simon Glassfa20e932020-12-03 16:55:20 -070057 plat = dev_get_plat(dev);
Simon Glass64401e72020-10-03 11:31:28 -060058 ut_assert(!plat->boolval);
59 ut_asserteq(3, plat->intval);
60 ut_asserteq(5, plat->intarray[0]);
61 ut_asserteq(0, plat->intarray[1]);
62 ut_asserteq(0, plat->intarray[2]);
Simon Glass64401e72020-10-03 11:31:28 -060063 ut_asserteq(8, plat->byteval);
64 ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
65 ut_asserteq(1, plat->bytearray[0]);
66 ut_asserteq(0x23, plat->bytearray[1]);
67 ut_asserteq(0x34, plat->bytearray[2]);
68 for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
69 ut_asserteq(i < 4 ? 9 + i : 0, plat->longbytearray[i]);
70 ut_asserteq_str("message2", plat->stringval);
71 ut_asserteq_str("another", plat->stringarray[0]);
72 ut_asserteq_str("multi-word", plat->stringarray[1]);
73 ut_asserteq_str("message", plat->stringarray[2]);
74
75 ut_assertok(uclass_next_device_err(&dev));
Simon Glassfa20e932020-12-03 16:55:20 -070076 plat = dev_get_plat(dev);
Simon Glass64401e72020-10-03 11:31:28 -060077 ut_assert(!plat->boolval);
78 ut_asserteq_str("one", plat->stringarray[0]);
79 ut_asserteq_str("", plat->stringarray[1]);
80 ut_asserteq_str("", plat->stringarray[2]);
81
82 ut_assertok(uclass_next_device_err(&dev));
Simon Glassfa20e932020-12-03 16:55:20 -070083 plat = dev_get_plat(dev);
Simon Glass64401e72020-10-03 11:31:28 -060084 ut_assert(!plat->boolval);
85 ut_asserteq_str("spl", plat->stringarray[0]);
86
87 ut_asserteq(-ENODEV, uclass_next_device_err(&dev));
88
89 return 0;
90}
Simon Glassb75b15b2020-12-03 16:55:23 -070091DM_TEST(dm_test_of_plat_props, UT_TESTF_SCAN_PDATA);
Simon Glassd825ea72020-10-03 11:31:31 -060092
93/*
94 * find_driver_info - recursively find the driver_info for a device
95 *
96 * This sets found[idx] to true when it finds the driver_info record for a
97 * device, where idx is the index in the driver_info linker list.
98 *
99 * @uts: Test state
100 * @parent: Parent to search
101 * @found: bool array to update
102 * @return 0 if OK, non-zero on error
103 */
104static int find_driver_info(struct unit_test_state *uts, struct udevice *parent,
105 bool found[])
106{
107 struct udevice *dev;
108
109 /* If not the root device, find the entry that caused it to be bound */
110 if (parent->parent) {
Simon Glassd825ea72020-10-03 11:31:31 -0600111 const int n_ents =
112 ll_entry_count(struct driver_info, driver_info);
Simon Glassd825ea72020-10-03 11:31:31 -0600113 int idx = -1;
Simon Glasscfd6a002020-10-03 11:31:33 -0600114 int i;
Simon Glassd825ea72020-10-03 11:31:31 -0600115
Simon Glasscfd6a002020-10-03 11:31:33 -0600116 for (i = 0; i < n_ents; i++) {
117 const struct driver_rt *drt = gd_dm_driver_rt() + i;
118
119 if (drt->dev == parent) {
120 idx = i;
Simon Glassd825ea72020-10-03 11:31:31 -0600121 found[idx] = true;
122 break;
123 }
124 }
125
126 ut_assert(idx != -1);
127 }
128
129 device_foreach_child(dev, parent) {
130 int ret;
131
132 ret = find_driver_info(uts, dev, found);
133 if (ret < 0)
134 return ret;
135 }
136
137 return 0;
138}
139
140/* Check that every device is recorded in its driver_info struct */
Simon Glassb75b15b2020-12-03 16:55:23 -0700141static int dm_test_of_plat_dev(struct unit_test_state *uts)
Simon Glassd825ea72020-10-03 11:31:31 -0600142{
Simon Glassd825ea72020-10-03 11:31:31 -0600143 const int n_ents = ll_entry_count(struct driver_info, driver_info);
144 bool found[n_ents];
145 uint i;
146
Simon Glasscb79f072021-03-15 17:25:32 +1300147 /* Skip this test if there is no platform data */
Simon Glass8beeb282021-03-15 17:25:36 +1300148 if (!CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT))
Simon Glasscb79f072021-03-15 17:25:32 +1300149 return 0;
150
Simon Glassd825ea72020-10-03 11:31:31 -0600151 /* Record the indexes that are found */
152 memset(found, '\0', sizeof(found));
153 ut_assertok(find_driver_info(uts, gd->dm_root, found));
154
155 /* Make sure that the driver entries without devices have no ->dev */
156 for (i = 0; i < n_ents; i++) {
Simon Glasscfd6a002020-10-03 11:31:33 -0600157 const struct driver_rt *drt = gd_dm_driver_rt() + i;
Simon Glassd825ea72020-10-03 11:31:31 -0600158 struct udevice *dev;
159
160 if (found[i]) {
161 /* Make sure we can find it */
Simon Glasscfd6a002020-10-03 11:31:33 -0600162 ut_assertnonnull(drt->dev);
Simon Glass0000e0d2021-03-15 17:25:28 +1300163 ut_assertok(device_get_by_ofplat_idx(i, &dev));
Simon Glasscfd6a002020-10-03 11:31:33 -0600164 ut_asserteq_ptr(dev, drt->dev);
Simon Glassd825ea72020-10-03 11:31:31 -0600165 } else {
Simon Glasscfd6a002020-10-03 11:31:33 -0600166 ut_assertnull(drt->dev);
Simon Glass0000e0d2021-03-15 17:25:28 +1300167 ut_asserteq(-ENOENT, device_get_by_ofplat_idx(i, &dev));
Simon Glassd825ea72020-10-03 11:31:31 -0600168 }
169 }
170
171 return 0;
172}
Simon Glassb75b15b2020-12-03 16:55:23 -0700173DM_TEST(dm_test_of_plat_dev, UT_TESTF_SCAN_PDATA);
Simon Glass6b927b12020-10-03 11:31:32 -0600174
175/* Test handling of phandles that point to other devices */
Simon Glassb75b15b2020-12-03 16:55:23 -0700176static int dm_test_of_plat_phandle(struct unit_test_state *uts)
Simon Glass6b927b12020-10-03 11:31:32 -0600177{
178 struct dtd_sandbox_clk_test *plat;
179 struct udevice *dev, *clk;
180
181 ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
182 ut_asserteq_str("sandbox_clk_test", dev->name);
Simon Glassfa20e932020-12-03 16:55:20 -0700183 plat = dev_get_plat(dev);
Simon Glass6b927b12020-10-03 11:31:32 -0600184
Simon Glass0000e0d2021-03-15 17:25:28 +1300185 ut_assertok(device_get_by_ofplat_idx(plat->clocks[0].idx, &clk));
Simon Glass9bb88fb2021-03-15 17:25:24 +1300186 ut_asserteq_str("sandbox_fixed_clock", clk->name);
Simon Glass6b927b12020-10-03 11:31:32 -0600187
Simon Glass0000e0d2021-03-15 17:25:28 +1300188 ut_assertok(device_get_by_ofplat_idx(plat->clocks[1].idx, &clk));
Simon Glass6b927b12020-10-03 11:31:32 -0600189 ut_asserteq_str("sandbox_clk", clk->name);
190 ut_asserteq(1, plat->clocks[1].arg[0]);
191
Simon Glass0000e0d2021-03-15 17:25:28 +1300192 ut_assertok(device_get_by_ofplat_idx(plat->clocks[2].idx, &clk));
Simon Glass6b927b12020-10-03 11:31:32 -0600193 ut_asserteq_str("sandbox_clk", clk->name);
194 ut_asserteq(0, plat->clocks[2].arg[0]);
195
Simon Glass0000e0d2021-03-15 17:25:28 +1300196 ut_assertok(device_get_by_ofplat_idx(plat->clocks[3].idx, &clk));
Simon Glass6b927b12020-10-03 11:31:32 -0600197 ut_asserteq_str("sandbox_clk", clk->name);
198 ut_asserteq(3, plat->clocks[3].arg[0]);
199
Simon Glass0000e0d2021-03-15 17:25:28 +1300200 ut_assertok(device_get_by_ofplat_idx(plat->clocks[4].idx, &clk));
Simon Glass6b927b12020-10-03 11:31:32 -0600201 ut_asserteq_str("sandbox_clk", clk->name);
202 ut_asserteq(2, plat->clocks[4].arg[0]);
203
204 return 0;
205}
Simon Glassb75b15b2020-12-03 16:55:23 -0700206DM_TEST(dm_test_of_plat_phandle, UT_TESTF_SCAN_PDATA);
Simon Glass4d4558e2020-10-03 11:31:36 -0600207
208#if CONFIG_IS_ENABLED(OF_PLATDATA_PARENT)
209/* Test that device parents are correctly set up */
Simon Glassb75b15b2020-12-03 16:55:23 -0700210static int dm_test_of_plat_parent(struct unit_test_state *uts)
Simon Glass4d4558e2020-10-03 11:31:36 -0600211{
Simon Glass77faa972021-03-15 17:25:31 +1300212 struct udevice *rtc, *i2c;
Simon Glass4d4558e2020-10-03 11:31:36 -0600213
Simon Glass77faa972021-03-15 17:25:31 +1300214 ut_assertok(uclass_first_device_err(UCLASS_RTC, &rtc));
215 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
216 ut_asserteq_ptr(i2c, dev_get_parent(rtc));
Simon Glass4d4558e2020-10-03 11:31:36 -0600217
218 return 0;
219}
Simon Glassb75b15b2020-12-03 16:55:23 -0700220DM_TEST(dm_test_of_plat_parent, UT_TESTF_SCAN_PDATA);
Simon Glass4d4558e2020-10-03 11:31:36 -0600221#endif