summaryrefslogtreecommitdiff
path: root/test/dm/of_platdata.c
blob: cfc43a5b038512ba08b9254a7ed898d9349d2557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-License-Identifier: GPL-2.0+

#include <common.h>
#include <dm.h>
#include <dt-structs.h>
#include <dm/test.h>
#include <test/test.h>
#include <test/ut.h>

/* Test that we can find a device using of-platdata */
static int dm_test_of_plat_base(struct unit_test_state *uts)
{
	struct udevice *dev;

	ut_assertok(uclass_first_device_err(UCLASS_SERIAL, &dev));
	ut_asserteq_str("sandbox_serial", dev->name);

	return 0;
}
DM_TEST(dm_test_of_plat_base, UT_TESTF_SCAN_PDATA);

/* Test that we can read properties from a device */
static int dm_test_of_plat_props(struct unit_test_state *uts)
{
	struct dtd_sandbox_spl_test *plat;
	struct udevice *dev;
	int i;

	/* Skip the clock */
	ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
	ut_asserteq_str("sandbox_clk_test", dev->name);

	ut_assertok(uclass_next_device_err(&dev));
	plat = dev_get_plat(dev);
	ut_assert(plat->boolval);
	ut_asserteq(1, plat->intval);
	ut_asserteq(4, ARRAY_SIZE(plat->intarray));
	ut_asserteq(2, plat->intarray[0]);
	ut_asserteq(3, plat->intarray[1]);
	ut_asserteq(4, plat->intarray[2]);
	ut_asserteq(0, plat->intarray[3]);
	ut_asserteq(5, plat->byteval);
	ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
	ut_asserteq(6, plat->bytearray[0]);
	ut_asserteq(0, plat->bytearray[1]);
	ut_asserteq(0, plat->bytearray[2]);
	ut_asserteq(9, ARRAY_SIZE(plat->longbytearray));
	for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
		ut_asserteq(9 + i, plat->longbytearray[i]);
	ut_asserteq_str("message", plat->stringval);
	ut_asserteq(3, ARRAY_SIZE(plat->stringarray));
	ut_asserteq_str("multi-word", plat->stringarray[0]);
	ut_asserteq_str("message", plat->stringarray[1]);
	ut_asserteq_str("", plat->stringarray[2]);

	ut_assertok(uclass_next_device_err(&dev));
	plat = dev_get_plat(dev);
	ut_assert(!plat->boolval);
	ut_asserteq(3, plat->intval);
	ut_asserteq(5, plat->intarray[0]);
	ut_asserteq(0, plat->intarray[1]);
	ut_asserteq(0, plat->intarray[2]);
	ut_asserteq(0, plat->intarray[3]);
	ut_asserteq(8, plat->byteval);
	ut_asserteq(3, ARRAY_SIZE(plat->bytearray));
	ut_asserteq(1, plat->bytearray[0]);
	ut_asserteq(0x23, plat->bytearray[1]);
	ut_asserteq(0x34, plat->bytearray[2]);
	for (i = 0; i < ARRAY_SIZE(plat->longbytearray); i++)
		ut_asserteq(i < 4 ? 9 + i : 0, plat->longbytearray[i]);
	ut_asserteq_str("message2", plat->stringval);
	ut_asserteq_str("another", plat->stringarray[0]);
	ut_asserteq_str("multi-word", plat->stringarray[1]);
	ut_asserteq_str("message", plat->stringarray[2]);

	ut_assertok(uclass_next_device_err(&dev));
	plat = dev_get_plat(dev);
	ut_assert(!plat->boolval);
	ut_asserteq_str("one", plat->stringarray[0]);
	ut_asserteq_str("", plat->stringarray[1]);
	ut_asserteq_str("", plat->stringarray[2]);

	ut_assertok(uclass_next_device_err(&dev));
	plat = dev_get_plat(dev);
	ut_assert(!plat->boolval);
	ut_asserteq_str("spl", plat->stringarray[0]);

	ut_asserteq(-ENODEV, uclass_next_device_err(&dev));

	return 0;
}
DM_TEST(dm_test_of_plat_props, UT_TESTF_SCAN_PDATA);

/*
 * find_driver_info - recursively find the driver_info for a device
 *
 * This sets found[idx] to true when it finds the driver_info record for a
 * device, where idx is the index in the driver_info linker list.
 *
 * @uts: Test state
 * @parent: Parent to search
 * @found: bool array to update
 * @return 0 if OK, non-zero on error
 */
static int find_driver_info(struct unit_test_state *uts, struct udevice *parent,
			    bool found[])
{
	struct udevice *dev;

	/* If not the root device, find the entry that caused it to be bound */
	if (parent->parent) {
		const int n_ents =
			ll_entry_count(struct driver_info, driver_info);
		int idx = -1;
		int i;

		for (i = 0; i < n_ents; i++) {
			const struct driver_rt *drt = gd_dm_driver_rt() + i;

			if (drt->dev == parent) {
				idx = i;
				found[idx] = true;
				break;
			}
		}

		ut_assert(idx != -1);
	}

	device_foreach_child(dev, parent) {
		int ret;

		ret = find_driver_info(uts, dev, found);
		if (ret < 0)
			return ret;
	}

	return 0;
}

/* Check that every device is recorded in its driver_info struct */
static int dm_test_of_plat_dev(struct unit_test_state *uts)
{
	const struct driver_info *info =
		ll_entry_start(struct driver_info, driver_info);
	const int n_ents = ll_entry_count(struct driver_info, driver_info);
	bool found[n_ents];
	uint i;

	/* Record the indexes that are found */
	memset(found, '\0', sizeof(found));
	ut_assertok(find_driver_info(uts, gd->dm_root, found));

	/* Make sure that the driver entries without devices have no ->dev */
	for (i = 0; i < n_ents; i++) {
		const struct driver_rt *drt = gd_dm_driver_rt() + i;
		const struct driver_info *entry = info + i;
		struct udevice *dev;

		if (found[i]) {
			/* Make sure we can find it */
			ut_assertnonnull(drt->dev);
			ut_assertok(device_get_by_driver_info(entry, &dev));
			ut_asserteq_ptr(dev, drt->dev);
		} else {
			ut_assertnull(drt->dev);
			ut_asserteq(-ENOENT,
				    device_get_by_driver_info(entry, &dev));
		}
	}

	return 0;
}
DM_TEST(dm_test_of_plat_dev, UT_TESTF_SCAN_PDATA);

/* Test handling of phandles that point to other devices */
static int dm_test_of_plat_phandle(struct unit_test_state *uts)
{
	struct dtd_sandbox_clk_test *plat;
	struct udevice *dev, *clk;

	ut_assertok(uclass_first_device_err(UCLASS_MISC, &dev));
	ut_asserteq_str("sandbox_clk_test", dev->name);
	plat = dev_get_plat(dev);

	ut_assertok(device_get_by_driver_info_idx(plat->clocks[0].idx, &clk));
	ut_asserteq_str("fixed_clock", clk->name);

	ut_assertok(device_get_by_driver_info_idx(plat->clocks[1].idx, &clk));
	ut_asserteq_str("sandbox_clk", clk->name);
	ut_asserteq(1, plat->clocks[1].arg[0]);

	ut_assertok(device_get_by_driver_info_idx(plat->clocks[2].idx, &clk));
	ut_asserteq_str("sandbox_clk", clk->name);
	ut_asserteq(0, plat->clocks[2].arg[0]);

	ut_assertok(device_get_by_driver_info_idx(plat->clocks[3].idx, &clk));
	ut_asserteq_str("sandbox_clk", clk->name);
	ut_asserteq(3, plat->clocks[3].arg[0]);

	ut_assertok(device_get_by_driver_info_idx(plat->clocks[4].idx, &clk));
	ut_asserteq_str("sandbox_clk", clk->name);
	ut_asserteq(2, plat->clocks[4].arg[0]);

	return 0;
}
DM_TEST(dm_test_of_plat_phandle, UT_TESTF_SCAN_PDATA);

#if CONFIG_IS_ENABLED(OF_PLATDATA_PARENT)
/* Test that device parents are correctly set up */
static int dm_test_of_plat_parent(struct unit_test_state *uts)
{
	struct udevice *rtc, *i2c;

	ut_assertok(uclass_first_device_err(UCLASS_RTC, &rtc));
	ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
	ut_asserteq_ptr(i2c, dev_get_parent(rtc));

	return 0;
}
DM_TEST(dm_test_of_plat_parent, UT_TESTF_SCAN_PDATA);
#endif