blob: a241c42793671de65db6e836417ab403bd2fb0ac [file] [log] [blame]
Simon Glass217293e2020-10-25 20:38:29 -06001// SPDX-License-Identifier: GPL-2.0+
2
3#include <common.h>
Simon Glassf0ab8f92021-08-07 07:24:09 -06004#include <clk.h>
Simon Glass217293e2020-10-25 20:38:29 -06005#include <dm.h>
Simon Glassb3252482020-10-03 11:31:28 -06006#include <dt-structs.h>
Simon Glass3e57ad92021-08-07 07:24:11 -06007#include <irq.h>
Simon Glass217293e2020-10-25 20:38:29 -06008#include <dm/test.h>
9#include <test/test.h>
10#include <test/ut.h>
Simon Glass48609d02021-08-07 07:24:12 -060011#include <asm-generic/gpio.h>
Simon Glass401d1c42020-10-30 21:38:53 -060012#include <asm/global_data.h>
Simon Glass217293e2020-10-25 20:38:29 -060013
14/* Test that we can find a device using of-platdata */
Simon Glass8a8d24b2020-12-03 16:55:23 -070015static int dm_test_of_plat_base(struct unit_test_state *uts)
Simon Glass217293e2020-10-25 20:38:29 -060016{
17 struct udevice *dev;
18
19 ut_assertok(uclass_first_device_err(UCLASS_SERIAL, &dev));
20 ut_asserteq_str("sandbox_serial", dev->name);
21
22 return 0;
23}
Simon Glass8a8d24b2020-12-03 16:55:23 -070024DM_TEST(dm_test_of_plat_base, UT_TESTF_SCAN_PDATA);
Simon Glassb3252482020-10-03 11:31:28 -060025
26/* Test that we can read properties from a device */
Simon Glass8a8d24b2020-12-03 16:55:23 -070027static int dm_test_of_plat_props(struct unit_test_state *uts)
Simon Glassb3252482020-10-03 11:31:28 -060028{
29 struct dtd_sandbox_spl_test *plat;
30 struct udevice *dev;
31 int i;
32
Simon Glass3e57ad92021-08-07 07:24:11 -060033 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_spl_test",
34 &dev));
Simon Glass88280522020-10-03 11:31:32 -060035
Simon Glassc69cda22020-12-03 16:55:20 -070036 plat = dev_get_plat(dev);
Simon Glassb3252482020-10-03 11:31:28 -060037 ut_assert(plat->boolval);
38 ut_asserteq(1, plat->intval);
Simon Glassca044942021-07-28 19:23:10 -060039 ut_asserteq(3, ARRAY_SIZE(plat->intarray));
Simon Glassb3252482020-10-03 11:31:28 -060040 ut_asserteq(2, plat->intarray[0]);
41 ut_asserteq(3, plat->intarray[1]);
42 ut_asserteq(4, plat->intarray[2]);
Simon Glassb3252482020-10-03 11:31:28 -060043 ut_asserteq(5, plat->byteval);
Simon Glasseec44c72021-07-28 19:23:11 -060044 ut_asserteq(1, ARRAY_SIZE(plat->maybe_empty_int));
45 ut_asserteq(0, plat->maybe_empty_int[0]);
Simon Glassb3252482020-10-03 11:31:28 -060046 ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
47 ut_asserteq(6, plat->bytearray[0]);
48 ut_asserteq(0, plat->bytearray[1]);
49 ut_asserteq(0, plat->bytearray[2]);
50 ut_asserteq(9, ARRAY_SIZE(plat->longbytearray));
51 for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
52 ut_asserteq(9 + i, plat->longbytearray[i]);
53 ut_asserteq_str("message", plat->stringval);
54 ut_asserteq(3, ARRAY_SIZE(plat->stringarray));
55 ut_asserteq_str("multi-word", plat->stringarray[0]);
56 ut_asserteq_str("message", plat->stringarray[1]);
57 ut_asserteq_str("", plat->stringarray[2]);
58
59 ut_assertok(uclass_next_device_err(&dev));
Simon Glassc69cda22020-12-03 16:55:20 -070060 plat = dev_get_plat(dev);
Simon Glassb3252482020-10-03 11:31:28 -060061 ut_assert(!plat->boolval);
62 ut_asserteq(3, plat->intval);
63 ut_asserteq(5, plat->intarray[0]);
64 ut_asserteq(0, plat->intarray[1]);
65 ut_asserteq(0, plat->intarray[2]);
Simon Glassb3252482020-10-03 11:31:28 -060066 ut_asserteq(8, plat->byteval);
67 ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
68 ut_asserteq(1, plat->bytearray[0]);
69 ut_asserteq(0x23, plat->bytearray[1]);
70 ut_asserteq(0x34, plat->bytearray[2]);
71 for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
72 ut_asserteq(i < 4 ? 9 + i : 0, plat->longbytearray[i]);
73 ut_asserteq_str("message2", plat->stringval);
74 ut_asserteq_str("another", plat->stringarray[0]);
75 ut_asserteq_str("multi-word", plat->stringarray[1]);
76 ut_asserteq_str("message", plat->stringarray[2]);
77
78 ut_assertok(uclass_next_device_err(&dev));
Simon Glassc69cda22020-12-03 16:55:20 -070079 plat = dev_get_plat(dev);
Simon Glassb3252482020-10-03 11:31:28 -060080 ut_assert(!plat->boolval);
81 ut_asserteq_str("one", plat->stringarray[0]);
82 ut_asserteq_str("", plat->stringarray[1]);
83 ut_asserteq_str("", plat->stringarray[2]);
Simon Glasseec44c72021-07-28 19:23:11 -060084 ut_asserteq(1, plat->maybe_empty_int[0]);
Simon Glassb3252482020-10-03 11:31:28 -060085
86 ut_assertok(uclass_next_device_err(&dev));
Simon Glassc69cda22020-12-03 16:55:20 -070087 plat = dev_get_plat(dev);
Simon Glassb3252482020-10-03 11:31:28 -060088 ut_assert(!plat->boolval);
89 ut_asserteq_str("spl", plat->stringarray[0]);
90
91 ut_asserteq(-ENODEV, uclass_next_device_err(&dev));
92
93 return 0;
94}
Simon Glass8a8d24b2020-12-03 16:55:23 -070095DM_TEST(dm_test_of_plat_props, UT_TESTF_SCAN_PDATA);
Simon Glass36af37b2020-10-03 11:31:31 -060096
97/*
98 * find_driver_info - recursively find the driver_info for a device
99 *
100 * This sets found[idx] to true when it finds the driver_info record for a
101 * device, where idx is the index in the driver_info linker list.
102 *
103 * @uts: Test state
104 * @parent: Parent to search
105 * @found: bool array to update
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100106 * Return: 0 if OK, non-zero on error
Simon Glass36af37b2020-10-03 11:31:31 -0600107 */
108static int find_driver_info(struct unit_test_state *uts, struct udevice *parent,
109 bool found[])
110{
111 struct udevice *dev;
112
113 /* If not the root device, find the entry that caused it to be bound */
114 if (parent->parent) {
Simon Glass36af37b2020-10-03 11:31:31 -0600115 const int n_ents =
116 ll_entry_count(struct driver_info, driver_info);
Simon Glass36af37b2020-10-03 11:31:31 -0600117 int idx = -1;
Simon Glassa294ead2020-10-03 11:31:33 -0600118 int i;
Simon Glass36af37b2020-10-03 11:31:31 -0600119
Simon Glassa294ead2020-10-03 11:31:33 -0600120 for (i = 0; i < n_ents; i++) {
121 const struct driver_rt *drt = gd_dm_driver_rt() + i;
122
123 if (drt->dev == parent) {
124 idx = i;
Simon Glass36af37b2020-10-03 11:31:31 -0600125 found[idx] = true;
126 break;
127 }
128 }
129
130 ut_assert(idx != -1);
131 }
132
133 device_foreach_child(dev, parent) {
134 int ret;
135
136 ret = find_driver_info(uts, dev, found);
137 if (ret < 0)
138 return ret;
139 }
140
141 return 0;
142}
143
144/* Check that every device is recorded in its driver_info struct */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700145static int dm_test_of_plat_dev(struct unit_test_state *uts)
Simon Glass36af37b2020-10-03 11:31:31 -0600146{
Simon Glass36af37b2020-10-03 11:31:31 -0600147 const int n_ents = ll_entry_count(struct driver_info, driver_info);
148 bool found[n_ents];
149 uint i;
150
Simon Glass424994c2021-03-15 17:25:32 +1300151 /* Skip this test if there is no platform data */
Simon Glassab933d82021-03-15 17:25:36 +1300152 if (!CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT))
Simon Glassc43635b2022-10-20 18:22:49 -0600153 return -EAGAIN;
Simon Glass424994c2021-03-15 17:25:32 +1300154
Simon Glass36af37b2020-10-03 11:31:31 -0600155 /* Record the indexes that are found */
156 memset(found, '\0', sizeof(found));
157 ut_assertok(find_driver_info(uts, gd->dm_root, found));
158
159 /* Make sure that the driver entries without devices have no ->dev */
160 for (i = 0; i < n_ents; i++) {
Simon Glassa294ead2020-10-03 11:31:33 -0600161 const struct driver_rt *drt = gd_dm_driver_rt() + i;
Simon Glass36af37b2020-10-03 11:31:31 -0600162 struct udevice *dev;
163
164 if (found[i]) {
165 /* Make sure we can find it */
Simon Glassa294ead2020-10-03 11:31:33 -0600166 ut_assertnonnull(drt->dev);
Simon Glasscc469b72021-03-15 17:25:28 +1300167 ut_assertok(device_get_by_ofplat_idx(i, &dev));
Simon Glassa294ead2020-10-03 11:31:33 -0600168 ut_asserteq_ptr(dev, drt->dev);
Simon Glass36af37b2020-10-03 11:31:31 -0600169 } else {
Simon Glassa294ead2020-10-03 11:31:33 -0600170 ut_assertnull(drt->dev);
Simon Glasscc469b72021-03-15 17:25:28 +1300171 ut_asserteq(-ENOENT, device_get_by_ofplat_idx(i, &dev));
Simon Glass36af37b2020-10-03 11:31:31 -0600172 }
173 }
174
175 return 0;
176}
Simon Glass8a8d24b2020-12-03 16:55:23 -0700177DM_TEST(dm_test_of_plat_dev, UT_TESTF_SCAN_PDATA);
Simon Glass88280522020-10-03 11:31:32 -0600178
179/* Test handling of phandles that point to other devices */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700180static int dm_test_of_plat_phandle(struct unit_test_state *uts)
Simon Glass88280522020-10-03 11:31:32 -0600181{
182 struct dtd_sandbox_clk_test *plat;
183 struct udevice *dev, *clk;
184
185 ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
186 ut_asserteq_str("sandbox_clk_test", dev->name);
Simon Glassc69cda22020-12-03 16:55:20 -0700187 plat = dev_get_plat(dev);
Simon Glass88280522020-10-03 11:31:32 -0600188
Simon Glasscc469b72021-03-15 17:25:28 +1300189 ut_assertok(device_get_by_ofplat_idx(plat->clocks[0].idx, &clk));
Simon Glass88139862021-03-15 17:25:24 +1300190 ut_asserteq_str("sandbox_fixed_clock", clk->name);
Simon Glass88280522020-10-03 11:31:32 -0600191
Simon Glasscc469b72021-03-15 17:25:28 +1300192 ut_assertok(device_get_by_ofplat_idx(plat->clocks[1].idx, &clk));
Simon Glass88280522020-10-03 11:31:32 -0600193 ut_asserteq_str("sandbox_clk", clk->name);
194 ut_asserteq(1, plat->clocks[1].arg[0]);
195
Simon Glasscc469b72021-03-15 17:25:28 +1300196 ut_assertok(device_get_by_ofplat_idx(plat->clocks[2].idx, &clk));
Simon Glass88280522020-10-03 11:31:32 -0600197 ut_asserteq_str("sandbox_clk", clk->name);
198 ut_asserteq(0, plat->clocks[2].arg[0]);
199
Simon Glasscc469b72021-03-15 17:25:28 +1300200 ut_assertok(device_get_by_ofplat_idx(plat->clocks[3].idx, &clk));
Simon Glass88280522020-10-03 11:31:32 -0600201 ut_asserteq_str("sandbox_clk", clk->name);
202 ut_asserteq(3, plat->clocks[3].arg[0]);
203
Simon Glasscc469b72021-03-15 17:25:28 +1300204 ut_assertok(device_get_by_ofplat_idx(plat->clocks[4].idx, &clk));
Simon Glass88280522020-10-03 11:31:32 -0600205 ut_asserteq_str("sandbox_clk", clk->name);
206 ut_asserteq(2, plat->clocks[4].arg[0]);
207
208 return 0;
209}
Simon Glass8a8d24b2020-12-03 16:55:23 -0700210DM_TEST(dm_test_of_plat_phandle, UT_TESTF_SCAN_PDATA);
Simon Glassfbe27a52020-10-03 11:31:36 -0600211
212#if CONFIG_IS_ENABLED(OF_PLATDATA_PARENT)
213/* Test that device parents are correctly set up */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700214static int dm_test_of_plat_parent(struct unit_test_state *uts)
Simon Glassfbe27a52020-10-03 11:31:36 -0600215{
Simon Glass3a825d32021-03-15 17:25:31 +1300216 struct udevice *rtc, *i2c;
Simon Glassfbe27a52020-10-03 11:31:36 -0600217
Simon Glass3a825d32021-03-15 17:25:31 +1300218 ut_assertok(uclass_first_device_err(UCLASS_RTC, &rtc));
219 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
220 ut_asserteq_ptr(i2c, dev_get_parent(rtc));
Simon Glassfbe27a52020-10-03 11:31:36 -0600221
222 return 0;
223}
Simon Glass8a8d24b2020-12-03 16:55:23 -0700224DM_TEST(dm_test_of_plat_parent, UT_TESTF_SCAN_PDATA);
Simon Glassfbe27a52020-10-03 11:31:36 -0600225#endif
Simon Glassf0ab8f92021-08-07 07:24:09 -0600226
227/* Test clocks with of-platdata */
228static int dm_test_of_plat_clk(struct unit_test_state *uts)
229{
230 struct dtd_sandbox_clk_test *plat;
231 struct udevice *dev;
232 struct clk clk;
233
234 ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
235 ut_asserteq_str("sandbox_clk_test", dev->name);
236 plat = dev_get_plat(dev);
237
238 ut_assertok(clk_get_by_phandle(dev, &plat->clocks[0], &clk));
239 ut_asserteq_str("sandbox_fixed_clock", clk.dev->name);
240
241 return 0;
242}
243DM_TEST(dm_test_of_plat_clk, UT_TESTF_SCAN_PDATA);
Simon Glass3e57ad92021-08-07 07:24:11 -0600244
245/* Test irqs with of-platdata */
246static int dm_test_of_plat_irq(struct unit_test_state *uts)
247{
248 struct dtd_sandbox_irq_test *plat;
249 struct udevice *dev;
250 struct irq irq;
251
252 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_irq_test",
253 &dev));
254 plat = dev_get_plat(dev);
255
256 ut_assertok(irq_get_by_phandle(dev, &plat->interrupts_extended[0],
257 &irq));
258 ut_asserteq_str("sandbox_irq", irq.dev->name);
259
260 return 0;
261}
262DM_TEST(dm_test_of_plat_irq, UT_TESTF_SCAN_PDATA);
Simon Glass48609d02021-08-07 07:24:12 -0600263
264/* Test GPIOs with of-platdata */
265static int dm_test_of_plat_gpio(struct unit_test_state *uts)
266{
267 struct dtd_sandbox_gpio_test *plat;
268 struct udevice *dev;
269 struct gpio_desc desc;
270
271 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "sandbox_gpio_test",
272 &dev));
273 plat = dev_get_plat(dev);
274
275 ut_assertok(gpio_request_by_phandle(dev, &plat->test_gpios[0], &desc,
276 GPIOD_IS_OUT));
277 ut_asserteq_str("sandbox_gpio", desc.dev->name);
278
279 return 0;
280}
281DM_TEST(dm_test_of_plat_gpio, UT_TESTF_SCAN_PDATA);