xref: /openbmc/u-boot/test/dm/test-fdt.c (revision a4884831)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <fdtdec.h>
11 #include <malloc.h>
12 #include <asm/io.h>
13 #include <dm/test.h>
14 #include <dm/root.h>
15 #include <dm/ut.h>
16 #include <dm/uclass-internal.h>
17 #include <dm/util.h>
18 
19 DECLARE_GLOBAL_DATA_PTR;
20 
21 static int testfdt_drv_ping(struct udevice *dev, int pingval, int *pingret)
22 {
23 	const struct dm_test_pdata *pdata = dev->platdata;
24 	struct dm_test_priv *priv = dev_get_priv(dev);
25 
26 	*pingret = pingval + pdata->ping_add;
27 	priv->ping_total += *pingret;
28 
29 	return 0;
30 }
31 
32 static const struct test_ops test_ops = {
33 	.ping = testfdt_drv_ping,
34 };
35 
36 static int testfdt_ofdata_to_platdata(struct udevice *dev)
37 {
38 	struct dm_test_pdata *pdata = dev_get_platdata(dev);
39 
40 	pdata->ping_add = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
41 					"ping-add", -1);
42 	pdata->base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
43 				      "ping-expect");
44 
45 	return 0;
46 }
47 
48 static int testfdt_drv_probe(struct udevice *dev)
49 {
50 	struct dm_test_priv *priv = dev_get_priv(dev);
51 
52 	priv->ping_total += DM_TEST_START_TOTAL;
53 
54 	return 0;
55 }
56 
57 static const struct udevice_id testfdt_ids[] = {
58 	{
59 		.compatible = "denx,u-boot-fdt-test",
60 		.data = DM_TEST_TYPE_FIRST },
61 	{
62 		.compatible = "google,another-fdt-test",
63 		.data = DM_TEST_TYPE_SECOND },
64 	{ }
65 };
66 
67 U_BOOT_DRIVER(testfdt_drv) = {
68 	.name	= "testfdt_drv",
69 	.of_match	= testfdt_ids,
70 	.id	= UCLASS_TEST_FDT,
71 	.ofdata_to_platdata = testfdt_ofdata_to_platdata,
72 	.probe	= testfdt_drv_probe,
73 	.ops	= &test_ops,
74 	.priv_auto_alloc_size = sizeof(struct dm_test_priv),
75 	.platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
76 };
77 
78 /* From here is the testfdt uclass code */
79 int testfdt_ping(struct udevice *dev, int pingval, int *pingret)
80 {
81 	const struct test_ops *ops = device_get_ops(dev);
82 
83 	if (!ops->ping)
84 		return -ENOSYS;
85 
86 	return ops->ping(dev, pingval, pingret);
87 }
88 
89 UCLASS_DRIVER(testfdt) = {
90 	.name		= "testfdt",
91 	.id		= UCLASS_TEST_FDT,
92 };
93 
94 int dm_check_devices(struct dm_test_state *dms, int num_devices)
95 {
96 	struct udevice *dev;
97 	int ret;
98 	int i;
99 
100 	/*
101 	 * Now check that the ping adds are what we expect. This is using the
102 	 * ping-add property in each node.
103 	 */
104 	for (i = 0; i < num_devices; i++) {
105 		uint32_t base;
106 
107 		ret = uclass_get_device(UCLASS_TEST_FDT, i, &dev);
108 		ut_assert(!ret);
109 
110 		/*
111 		 * Get the 'ping-expect' property, which tells us what the
112 		 * ping add should be. We don't use the platdata because we
113 		 * want to test the code that sets that up
114 		 * (testfdt_drv_probe()).
115 		 */
116 		base = fdtdec_get_addr(gd->fdt_blob, dev->of_offset,
117 				       "ping-expect");
118 		debug("dev=%d, base=%d: %s\n", i, base,
119 		      fdt_get_name(gd->fdt_blob, dev->of_offset, NULL));
120 
121 		ut_assert(!dm_check_operations(dms, dev, base,
122 					       dev_get_priv(dev)));
123 	}
124 
125 	return 0;
126 }
127 
128 /* Test that FDT-based binding works correctly */
129 static int dm_test_fdt(struct dm_test_state *dms)
130 {
131 	const int num_devices = 4;
132 	struct udevice *dev;
133 	struct uclass *uc;
134 	int ret;
135 	int i;
136 
137 	ret = dm_scan_fdt(gd->fdt_blob, false);
138 	ut_assert(!ret);
139 
140 	ret = uclass_get(UCLASS_TEST_FDT, &uc);
141 	ut_assert(!ret);
142 
143 	/* These are num_devices compatible root-level device tree nodes */
144 	ut_asserteq(num_devices, list_count_items(&uc->dev_head));
145 
146 	/* Each should have no platdata / priv */
147 	for (i = 0; i < num_devices; i++) {
148 		ret = uclass_find_device(UCLASS_TEST_FDT, i, &dev);
149 		ut_assert(!ret);
150 		ut_assert(!dev_get_priv(dev));
151 		ut_assert(!dev->platdata);
152 	}
153 
154 	ut_assertok(dm_check_devices(dms, num_devices));
155 
156 	return 0;
157 }
158 DM_TEST(dm_test_fdt, 0);
159 
160 static int dm_test_fdt_pre_reloc(struct dm_test_state *dms)
161 {
162 	struct uclass *uc;
163 	int ret;
164 
165 	ret = dm_scan_fdt(gd->fdt_blob, true);
166 	ut_assert(!ret);
167 
168 	ret = uclass_get(UCLASS_TEST_FDT, &uc);
169 	ut_assert(!ret);
170 
171 	/* These is only one pre-reloc device */
172 	ut_asserteq(1, list_count_items(&uc->dev_head));
173 
174 	return 0;
175 }
176 DM_TEST(dm_test_fdt_pre_reloc, 0);
177 
178 /* Test that sequence numbers are allocated properly */
179 static int dm_test_fdt_uclass_seq(struct dm_test_state *dms)
180 {
181 	struct udevice *dev;
182 
183 	/* A few basic santiy tests */
184 	ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 3, true, &dev));
185 	ut_asserteq_str("b-test", dev->name);
186 
187 	ut_assertok(uclass_find_device_by_seq(UCLASS_TEST_FDT, 0, true, &dev));
188 	ut_asserteq_str("a-test", dev->name);
189 
190 	ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 5,
191 						       true, &dev));
192 	ut_asserteq_ptr(NULL, dev);
193 
194 	/* Test aliases */
195 	ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 6, &dev));
196 	ut_asserteq_str("e-test", dev->name);
197 
198 	ut_asserteq(-ENODEV, uclass_find_device_by_seq(UCLASS_TEST_FDT, 7,
199 						       true, &dev));
200 
201 	/*
202 	 * Note that c-test nodes are not probed since it is not a top-level
203 	 * node
204 	 */
205 	ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
206 	ut_asserteq_str("b-test", dev->name);
207 
208 	/*
209 	 * d-test wants sequence number 3 also, but it can't have it because
210 	 * b-test gets it first.
211 	 */
212 	ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 2, &dev));
213 	ut_asserteq_str("d-test", dev->name);
214 
215 	/* d-test actually gets 0 */
216 	ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 0, &dev));
217 	ut_asserteq_str("d-test", dev->name);
218 
219 	/* initially no one wants seq 1 */
220 	ut_asserteq(-ENODEV, uclass_get_device_by_seq(UCLASS_TEST_FDT, 1,
221 						      &dev));
222 	ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
223 	ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 1, &dev));
224 
225 	/* But now that it is probed, we can find it */
226 	ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 1, &dev));
227 	ut_asserteq_str("a-test", dev->name);
228 
229 	return 0;
230 }
231 DM_TEST(dm_test_fdt_uclass_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
232 
233 /* Test that we can find a device by device tree offset */
234 static int dm_test_fdt_offset(struct dm_test_state *dms)
235 {
236 	const void *blob = gd->fdt_blob;
237 	struct udevice *dev;
238 	int node;
239 
240 	node = fdt_path_offset(blob, "/e-test");
241 	ut_assert(node > 0);
242 	ut_assertok(uclass_get_device_by_of_offset(UCLASS_TEST_FDT, node,
243 						   &dev));
244 	ut_asserteq_str("e-test", dev->name);
245 
246 	/* This node should not be bound */
247 	node = fdt_path_offset(blob, "/junk");
248 	ut_assert(node > 0);
249 	ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
250 							    node, &dev));
251 
252 	/* This is not a top level node so should not be probed */
253 	node = fdt_path_offset(blob, "/some-bus/c-test@5");
254 	ut_assert(node > 0);
255 	ut_asserteq(-ENODEV, uclass_get_device_by_of_offset(UCLASS_TEST_FDT,
256 							    node, &dev));
257 
258 	return 0;
259 }
260 DM_TEST(dm_test_fdt_offset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
261