blob: 6e35bcaf62c3d10343c679c40f2b885f0d785481 [file] [log] [blame]
Simon Glass25dc2e92020-10-25 20:38:29 -06001// SPDX-License-Identifier: GPL-2.0+
2
Simon Glass1257efc2021-08-07 07:24:09 -06003#include <clk.h>
Simon Glass25dc2e92020-10-25 20:38:29 -06004#include <dm.h>
Simon Glass64401e72020-10-03 11:31:28 -06005#include <dt-structs.h>
Simon Glasse7995f72021-08-07 07:24:11 -06006#include <irq.h>
Simon Glass25dc2e92020-10-25 20:38:29 -06007#include <dm/test.h>
8#include <test/test.h>
9#include <test/ut.h>
Simon Glass2149e112021-08-07 07:24:12 -060010#include <asm-generic/gpio.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glass25dc2e92020-10-25 20:38:29 -060012
13/* Test that we can find a device using of-platdata */
Simon Glassb75b15b2020-12-03 16:55:23 -070014static int dm_test_of_plat_base(struct unit_test_state *uts)
Simon Glass25dc2e92020-10-25 20:38:29 -060015{
16 struct udevice *dev;
17
18 ut_assertok(uclass_first_device_err(UCLASS_SERIAL, &dev));
19 ut_asserteq_str("sandbox_serial", dev->name);
20
21 return 0;
22}
Simon Glass1a92f832024-08-22 07:57:48 -060023DM_TEST(dm_test_of_plat_base, UTF_SCAN_PDATA);
Simon Glass64401e72020-10-03 11:31:28 -060024
25/* Test that we can read properties from a device */
Simon Glassb75b15b2020-12-03 16:55:23 -070026static int dm_test_of_plat_props(struct unit_test_state *uts)
Simon Glass64401e72020-10-03 11:31:28 -060027{
28 struct dtd_sandbox_spl_test *plat;
29 struct udevice *dev;
30 int i;
31
Simon Glasse7995f72021-08-07 07:24:11 -060032 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_spl_test",
33 &dev));
Simon Glass6b927b12020-10-03 11:31:32 -060034
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);
Simon Glass43118322021-07-28 19:23:11 -060043 ut_asserteq(1, ARRAY_SIZE(plat->maybe_empty_int));
44 ut_asserteq(0, plat->maybe_empty_int[0]);
Simon Glass64401e72020-10-03 11:31:28 -060045 ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
46 ut_asserteq(6, plat->bytearray[0]);
47 ut_asserteq(0, plat->bytearray[1]);
48 ut_asserteq(0, plat->bytearray[2]);
49 ut_asserteq(9, ARRAY_SIZE(plat->longbytearray));
50 for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
51 ut_asserteq(9 + i, plat->longbytearray[i]);
52 ut_asserteq_str("message", plat->stringval);
53 ut_asserteq(3, ARRAY_SIZE(plat->stringarray));
54 ut_asserteq_str("multi-word", plat->stringarray[0]);
55 ut_asserteq_str("message", plat->stringarray[1]);
56 ut_asserteq_str("", plat->stringarray[2]);
57
58 ut_assertok(uclass_next_device_err(&dev));
Simon Glassfa20e932020-12-03 16:55:20 -070059 plat = dev_get_plat(dev);
Simon Glass64401e72020-10-03 11:31:28 -060060 ut_assert(!plat->boolval);
61 ut_asserteq(3, plat->intval);
62 ut_asserteq(5, plat->intarray[0]);
63 ut_asserteq(0, plat->intarray[1]);
64 ut_asserteq(0, plat->intarray[2]);
Simon Glass64401e72020-10-03 11:31:28 -060065 ut_asserteq(8, plat->byteval);
66 ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
67 ut_asserteq(1, plat->bytearray[0]);
68 ut_asserteq(0x23, plat->bytearray[1]);
69 ut_asserteq(0x34, plat->bytearray[2]);
70 for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
71 ut_asserteq(i < 4 ? 9 + i : 0, plat->longbytearray[i]);
72 ut_asserteq_str("message2", plat->stringval);
73 ut_asserteq_str("another", plat->stringarray[0]);
74 ut_asserteq_str("multi-word", plat->stringarray[1]);
75 ut_asserteq_str("message", plat->stringarray[2]);
76
77 ut_assertok(uclass_next_device_err(&dev));
Simon Glassfa20e932020-12-03 16:55:20 -070078 plat = dev_get_plat(dev);
Simon Glass64401e72020-10-03 11:31:28 -060079 ut_assert(!plat->boolval);
80 ut_asserteq_str("one", plat->stringarray[0]);
81 ut_asserteq_str("", plat->stringarray[1]);
82 ut_asserteq_str("", plat->stringarray[2]);
Simon Glass43118322021-07-28 19:23:11 -060083 ut_asserteq(1, plat->maybe_empty_int[0]);
Simon Glass64401e72020-10-03 11:31:28 -060084
85 ut_assertok(uclass_next_device_err(&dev));
Simon Glassfa20e932020-12-03 16:55:20 -070086 plat = dev_get_plat(dev);
Simon Glass64401e72020-10-03 11:31:28 -060087 ut_assert(!plat->boolval);
88 ut_asserteq_str("spl", plat->stringarray[0]);
89
90 ut_asserteq(-ENODEV, uclass_next_device_err(&dev));
91
92 return 0;
93}
Simon Glass1a92f832024-08-22 07:57:48 -060094DM_TEST(dm_test_of_plat_props, UTF_SCAN_PDATA);
Simon Glassd825ea72020-10-03 11:31:31 -060095
96/*
97 * find_driver_info - recursively find the driver_info for a device
98 *
99 * This sets found[idx] to true when it finds the driver_info record for a
100 * device, where idx is the index in the driver_info linker list.
101 *
102 * @uts: Test state
103 * @parent: Parent to search
104 * @found: bool array to update
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100105 * Return: 0 if OK, non-zero on error
Simon Glassd825ea72020-10-03 11:31:31 -0600106 */
107static int find_driver_info(struct unit_test_state *uts, struct udevice *parent,
108 bool found[])
109{
110 struct udevice *dev;
111
112 /* If not the root device, find the entry that caused it to be bound */
113 if (parent->parent) {
Simon Glassd825ea72020-10-03 11:31:31 -0600114 const int n_ents =
115 ll_entry_count(struct driver_info, driver_info);
Simon Glassd825ea72020-10-03 11:31:31 -0600116 int idx = -1;
Simon Glasscfd6a002020-10-03 11:31:33 -0600117 int i;
Simon Glassd825ea72020-10-03 11:31:31 -0600118
Simon Glasscfd6a002020-10-03 11:31:33 -0600119 for (i = 0; i < n_ents; i++) {
120 const struct driver_rt *drt = gd_dm_driver_rt() + i;
121
122 if (drt->dev == parent) {
123 idx = i;
Simon Glassd825ea72020-10-03 11:31:31 -0600124 found[idx] = true;
125 break;
126 }
127 }
128
129 ut_assert(idx != -1);
130 }
131
132 device_foreach_child(dev, parent) {
133 int ret;
134
135 ret = find_driver_info(uts, dev, found);
136 if (ret < 0)
137 return ret;
138 }
139
140 return 0;
141}
142
143/* Check that every device is recorded in its driver_info struct */
Simon Glassb75b15b2020-12-03 16:55:23 -0700144static int dm_test_of_plat_dev(struct unit_test_state *uts)
Simon Glassd825ea72020-10-03 11:31:31 -0600145{
Simon Glassd825ea72020-10-03 11:31:31 -0600146 const int n_ents = ll_entry_count(struct driver_info, driver_info);
147 bool found[n_ents];
148 uint i;
149
Simon Glasscb79f072021-03-15 17:25:32 +1300150 /* Skip this test if there is no platform data */
Simon Glass8beeb282021-03-15 17:25:36 +1300151 if (!CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT))
Simon Glassc7599442022-10-20 18:22:49 -0600152 return -EAGAIN;
Simon Glasscb79f072021-03-15 17:25:32 +1300153
Simon Glassd825ea72020-10-03 11:31:31 -0600154 /* Record the indexes that are found */
155 memset(found, '\0', sizeof(found));
156 ut_assertok(find_driver_info(uts, gd->dm_root, found));
157
158 /* Make sure that the driver entries without devices have no ->dev */
159 for (i = 0; i < n_ents; i++) {
Simon Glasscfd6a002020-10-03 11:31:33 -0600160 const struct driver_rt *drt = gd_dm_driver_rt() + i;
Simon Glassd825ea72020-10-03 11:31:31 -0600161 struct udevice *dev;
162
163 if (found[i]) {
164 /* Make sure we can find it */
Simon Glasscfd6a002020-10-03 11:31:33 -0600165 ut_assertnonnull(drt->dev);
Simon Glass0000e0d2021-03-15 17:25:28 +1300166 ut_assertok(device_get_by_ofplat_idx(i, &dev));
Simon Glasscfd6a002020-10-03 11:31:33 -0600167 ut_asserteq_ptr(dev, drt->dev);
Simon Glassd825ea72020-10-03 11:31:31 -0600168 } else {
Simon Glasscfd6a002020-10-03 11:31:33 -0600169 ut_assertnull(drt->dev);
Simon Glass0000e0d2021-03-15 17:25:28 +1300170 ut_asserteq(-ENOENT, device_get_by_ofplat_idx(i, &dev));
Simon Glassd825ea72020-10-03 11:31:31 -0600171 }
172 }
173
174 return 0;
175}
Simon Glass1a92f832024-08-22 07:57:48 -0600176DM_TEST(dm_test_of_plat_dev, UTF_SCAN_PDATA);
Simon Glass6b927b12020-10-03 11:31:32 -0600177
178/* Test handling of phandles that point to other devices */
Simon Glassb75b15b2020-12-03 16:55:23 -0700179static int dm_test_of_plat_phandle(struct unit_test_state *uts)
Simon Glass6b927b12020-10-03 11:31:32 -0600180{
181 struct dtd_sandbox_clk_test *plat;
182 struct udevice *dev, *clk;
183
184 ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
185 ut_asserteq_str("sandbox_clk_test", dev->name);
Simon Glassfa20e932020-12-03 16:55:20 -0700186 plat = dev_get_plat(dev);
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[0].idx, &clk));
Simon Glass9bb88fb2021-03-15 17:25:24 +1300189 ut_asserteq_str("sandbox_fixed_clock", clk->name);
Simon Glass6b927b12020-10-03 11:31:32 -0600190
Simon Glass0000e0d2021-03-15 17:25:28 +1300191 ut_assertok(device_get_by_ofplat_idx(plat->clocks[1].idx, &clk));
Simon Glass6b927b12020-10-03 11:31:32 -0600192 ut_asserteq_str("sandbox_clk", clk->name);
193 ut_asserteq(1, plat->clocks[1].arg[0]);
194
Simon Glass0000e0d2021-03-15 17:25:28 +1300195 ut_assertok(device_get_by_ofplat_idx(plat->clocks[2].idx, &clk));
Simon Glass6b927b12020-10-03 11:31:32 -0600196 ut_asserteq_str("sandbox_clk", clk->name);
197 ut_asserteq(0, plat->clocks[2].arg[0]);
198
Simon Glass0000e0d2021-03-15 17:25:28 +1300199 ut_assertok(device_get_by_ofplat_idx(plat->clocks[3].idx, &clk));
Simon Glass6b927b12020-10-03 11:31:32 -0600200 ut_asserteq_str("sandbox_clk", clk->name);
201 ut_asserteq(3, plat->clocks[3].arg[0]);
202
Simon Glass0000e0d2021-03-15 17:25:28 +1300203 ut_assertok(device_get_by_ofplat_idx(plat->clocks[4].idx, &clk));
Simon Glass6b927b12020-10-03 11:31:32 -0600204 ut_asserteq_str("sandbox_clk", clk->name);
205 ut_asserteq(2, plat->clocks[4].arg[0]);
206
207 return 0;
208}
Simon Glass1a92f832024-08-22 07:57:48 -0600209DM_TEST(dm_test_of_plat_phandle, UTF_SCAN_PDATA);
Simon Glass4d4558e2020-10-03 11:31:36 -0600210
211#if CONFIG_IS_ENABLED(OF_PLATDATA_PARENT)
212/* Test that device parents are correctly set up */
Simon Glassb75b15b2020-12-03 16:55:23 -0700213static int dm_test_of_plat_parent(struct unit_test_state *uts)
Simon Glass4d4558e2020-10-03 11:31:36 -0600214{
Simon Glass77faa972021-03-15 17:25:31 +1300215 struct udevice *rtc, *i2c;
Simon Glass4d4558e2020-10-03 11:31:36 -0600216
Simon Glass77faa972021-03-15 17:25:31 +1300217 ut_assertok(uclass_first_device_err(UCLASS_RTC, &rtc));
218 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
219 ut_asserteq_ptr(i2c, dev_get_parent(rtc));
Simon Glass4d4558e2020-10-03 11:31:36 -0600220
221 return 0;
222}
Simon Glass1a92f832024-08-22 07:57:48 -0600223DM_TEST(dm_test_of_plat_parent, UTF_SCAN_PDATA);
Simon Glass4d4558e2020-10-03 11:31:36 -0600224#endif
Simon Glass1257efc2021-08-07 07:24:09 -0600225
226/* Test clocks with of-platdata */
227static int dm_test_of_plat_clk(struct unit_test_state *uts)
228{
229 struct dtd_sandbox_clk_test *plat;
230 struct udevice *dev;
231 struct clk clk;
232
233 ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
234 ut_asserteq_str("sandbox_clk_test", dev->name);
235 plat = dev_get_plat(dev);
236
237 ut_assertok(clk_get_by_phandle(dev, &plat->clocks[0], &clk));
238 ut_asserteq_str("sandbox_fixed_clock", clk.dev->name);
239
240 return 0;
241}
Simon Glass1a92f832024-08-22 07:57:48 -0600242DM_TEST(dm_test_of_plat_clk, UTF_SCAN_PDATA);
Simon Glasse7995f72021-08-07 07:24:11 -0600243
244/* Test irqs with of-platdata */
245static int dm_test_of_plat_irq(struct unit_test_state *uts)
246{
247 struct dtd_sandbox_irq_test *plat;
248 struct udevice *dev;
249 struct irq irq;
250
251 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_irq_test",
252 &dev));
253 plat = dev_get_plat(dev);
254
255 ut_assertok(irq_get_by_phandle(dev, &plat->interrupts_extended[0],
256 &irq));
257 ut_asserteq_str("sandbox_irq", irq.dev->name);
258
259 return 0;
260}
Simon Glass1a92f832024-08-22 07:57:48 -0600261DM_TEST(dm_test_of_plat_irq, UTF_SCAN_PDATA);
Simon Glass2149e112021-08-07 07:24:12 -0600262
263/* Test GPIOs with of-platdata */
264static int dm_test_of_plat_gpio(struct unit_test_state *uts)
265{
266 struct dtd_sandbox_gpio_test *plat;
267 struct udevice *dev;
268 struct gpio_desc desc;
269
270 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_gpio_test",
271 &dev));
272 plat = dev_get_plat(dev);
273
274 ut_assertok(gpio_request_by_phandle(dev, &plat->test_gpios[0], &desc,
275 GPIOD_IS_OUT));
276 ut_asserteq_str("sandbox_gpio", desc.dev->name);
277
278 return 0;
279}
Simon Glass1a92f832024-08-22 07:57:48 -0600280DM_TEST(dm_test_of_plat_gpio, UTF_SCAN_PDATA);