xref: /openbmc/u-boot/test/dm/bus.c (revision 9cc36a2b)
1 /*
2  * Copyright (c) 2014 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <dm/device-internal.h>
10 #include <dm/root.h>
11 #include <dm/test.h>
12 #include <dm/uclass-internal.h>
13 #include <dm/ut.h>
14 #include <dm/util.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 struct dm_test_parent_platdata {
19 	int count;
20 	int bind_flag;
21 };
22 
23 enum {
24 	FLAG_CHILD_PROBED	= 10,
25 	FLAG_CHILD_REMOVED	= -7,
26 };
27 
28 static struct dm_test_state *test_state;
29 
30 static int testbus_drv_probe(struct udevice *dev)
31 {
32 	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
33 }
34 
35 static int testbus_child_post_bind(struct udevice *dev)
36 {
37 	struct dm_test_parent_platdata *plat;
38 
39 	plat = dev_get_parent_platdata(dev);
40 	plat->bind_flag = 1;
41 
42 	return 0;
43 }
44 
45 static int testbus_child_pre_probe(struct udevice *dev)
46 {
47 	struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
48 
49 	parent_data->flag += FLAG_CHILD_PROBED;
50 
51 	return 0;
52 }
53 
54 static int testbus_child_post_remove(struct udevice *dev)
55 {
56 	struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
57 	struct dm_test_state *dms = test_state;
58 
59 	parent_data->flag += FLAG_CHILD_REMOVED;
60 	if (dms)
61 		dms->removed = dev;
62 
63 	return 0;
64 }
65 
66 static const struct udevice_id testbus_ids[] = {
67 	{
68 		.compatible = "denx,u-boot-test-bus",
69 		.data = DM_TEST_TYPE_FIRST },
70 	{ }
71 };
72 
73 U_BOOT_DRIVER(testbus_drv) = {
74 	.name	= "testbus_drv",
75 	.of_match	= testbus_ids,
76 	.id	= UCLASS_TEST_BUS,
77 	.probe	= testbus_drv_probe,
78 	.child_post_bind = testbus_child_post_bind,
79 	.priv_auto_alloc_size = sizeof(struct dm_test_priv),
80 	.platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
81 	.per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
82 	.per_child_platdata_auto_alloc_size =
83 			sizeof(struct dm_test_parent_platdata),
84 	.child_pre_probe = testbus_child_pre_probe,
85 	.child_post_remove = testbus_child_post_remove,
86 };
87 
88 UCLASS_DRIVER(testbus) = {
89 	.name		= "testbus",
90 	.id		= UCLASS_TEST_BUS,
91 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
92 };
93 
94 /* Test that we can probe for children */
95 static int dm_test_bus_children(struct dm_test_state *dms)
96 {
97 	int num_devices = 6;
98 	struct udevice *bus;
99 	struct uclass *uc;
100 
101 	ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
102 	ut_asserteq(num_devices, list_count_items(&uc->dev_head));
103 
104 	/* Probe the bus, which should yield 3 more devices */
105 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
106 	num_devices += 3;
107 
108 	ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
109 	ut_asserteq(num_devices, list_count_items(&uc->dev_head));
110 
111 	ut_assert(!dm_check_devices(dms, num_devices));
112 
113 	return 0;
114 }
115 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
116 
117 /* Test our functions for accessing children */
118 static int dm_test_bus_children_funcs(struct dm_test_state *dms)
119 {
120 	const void *blob = gd->fdt_blob;
121 	struct udevice *bus, *dev;
122 	int node;
123 
124 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
125 
126 	/* device_get_child() */
127 	ut_assertok(device_get_child(bus, 0, &dev));
128 	ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
129 	ut_assertok(device_get_child_by_seq(bus, 5, &dev));
130 	ut_assert(dev->flags & DM_FLAG_ACTIVATED);
131 	ut_asserteq_str("c-test@5", dev->name);
132 
133 	/* Device with sequence number 0 should be accessible */
134 	ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
135 	ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
136 	ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
137 	ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
138 	ut_assertok(device_get_child_by_seq(bus, 0, &dev));
139 	ut_assert(dev->flags & DM_FLAG_ACTIVATED);
140 
141 	/* There is no device with sequence number 2 */
142 	ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
143 	ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
144 	ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
145 
146 	/* Looking for something that is not a child */
147 	node = fdt_path_offset(blob, "/junk");
148 	ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
149 	node = fdt_path_offset(blob, "/d-test");
150 	ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
151 
152 	/* Find a valid child */
153 	node = fdt_path_offset(blob, "/some-bus/c-test@1");
154 	ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
155 	ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
156 	ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
157 	ut_assert(dev->flags & DM_FLAG_ACTIVATED);
158 
159 	return 0;
160 }
161 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
162 
163 /* Test that we can iterate through children */
164 static int dm_test_bus_children_iterators(struct dm_test_state *dms)
165 {
166 	struct udevice *bus, *dev, *child;
167 
168 	/* Walk through the children one by one */
169 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
170 	ut_assertok(device_find_first_child(bus, &dev));
171 	ut_asserteq_str("c-test@5", dev->name);
172 	ut_assertok(device_find_next_child(&dev));
173 	ut_asserteq_str("c-test@0", dev->name);
174 	ut_assertok(device_find_next_child(&dev));
175 	ut_asserteq_str("c-test@1", dev->name);
176 	ut_assertok(device_find_next_child(&dev));
177 	ut_asserteq_ptr(dev, NULL);
178 
179 	/* Move to the next child without using device_find_first_child() */
180 	ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
181 	ut_asserteq_str("c-test@5", dev->name);
182 	ut_assertok(device_find_next_child(&dev));
183 	ut_asserteq_str("c-test@0", dev->name);
184 
185 	/* Try a device with no children */
186 	ut_assertok(device_find_first_child(dev, &child));
187 	ut_asserteq_ptr(child, NULL);
188 
189 	return 0;
190 }
191 DM_TEST(dm_test_bus_children_iterators,
192 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
193 
194 /* Test that the bus can store data about each child */
195 static int dm_test_bus_parent_data(struct dm_test_state *dms)
196 {
197 	struct dm_test_parent_data *parent_data;
198 	struct udevice *bus, *dev;
199 	struct uclass *uc;
200 	int value;
201 
202 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
203 
204 	/* Check that parent data is allocated */
205 	ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
206 	ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
207 	ut_assertok(device_get_child_by_seq(bus, 0, &dev));
208 	parent_data = dev_get_parentdata(dev);
209 	ut_assert(NULL != parent_data);
210 
211 	/* Check that it starts at 0 and goes away when device is removed */
212 	parent_data->sum += 5;
213 	ut_asserteq(5, parent_data->sum);
214 	device_remove(dev);
215 	ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
216 
217 	/* Check that we can do this twice */
218 	ut_assertok(device_get_child_by_seq(bus, 0, &dev));
219 	parent_data = dev_get_parentdata(dev);
220 	ut_assert(NULL != parent_data);
221 	parent_data->sum += 5;
222 	ut_asserteq(5, parent_data->sum);
223 
224 	/* Add parent data to all children */
225 	ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
226 	value = 5;
227 	uclass_foreach_dev(dev, uc) {
228 		/* Ignore these if they are not on this bus */
229 		if (dev->parent != bus) {
230 			ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
231 			continue;
232 		}
233 		ut_assertok(device_probe(dev));
234 		parent_data = dev_get_parentdata(dev);
235 
236 		parent_data->sum = value;
237 		value += 5;
238 	}
239 
240 	/* Check it is still there */
241 	value = 5;
242 	uclass_foreach_dev(dev, uc) {
243 		/* Ignore these if they are not on this bus */
244 		if (dev->parent != bus)
245 			continue;
246 		parent_data = dev_get_parentdata(dev);
247 
248 		ut_asserteq(value, parent_data->sum);
249 		value += 5;
250 	}
251 
252 	return 0;
253 }
254 
255 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
256 
257 /* Test that the bus ops are called when a child is probed/removed */
258 static int dm_test_bus_parent_ops(struct dm_test_state *dms)
259 {
260 	struct dm_test_parent_data *parent_data;
261 	struct udevice *bus, *dev;
262 	struct uclass *uc;
263 
264 	test_state = dms;
265 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
266 	ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
267 
268 	uclass_foreach_dev(dev, uc) {
269 		/* Ignore these if they are not on this bus */
270 		if (dev->parent != bus)
271 			continue;
272 		ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
273 
274 		ut_assertok(device_probe(dev));
275 		parent_data = dev_get_parentdata(dev);
276 		ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
277 	}
278 
279 	uclass_foreach_dev(dev, uc) {
280 		/* Ignore these if they are not on this bus */
281 		if (dev->parent != bus)
282 			continue;
283 		parent_data = dev_get_parentdata(dev);
284 		ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
285 		ut_assertok(device_remove(dev));
286 		ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
287 		ut_asserteq_ptr(dms->removed, dev);
288 	}
289 	test_state = NULL;
290 
291 	return 0;
292 }
293 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
294 
295 static int test_bus_parent_platdata(struct dm_test_state *dms)
296 {
297 	struct dm_test_parent_platdata *plat;
298 	struct udevice *bus, *dev;
299 	int child_count;
300 
301 	/* Check that the bus has no children */
302 	ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
303 	device_find_first_child(bus, &dev);
304 	ut_asserteq_ptr(NULL, dev);
305 
306 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
307 
308 	for (device_find_first_child(bus, &dev), child_count = 0;
309 	     dev;
310 	     device_find_next_child(&dev)) {
311 		/* Check that platform data is allocated */
312 		plat = dev_get_parent_platdata(dev);
313 		ut_assert(plat != NULL);
314 
315 		/*
316 		 * Check that it is not affected by the device being
317 		 * probed/removed
318 		 */
319 		plat->count++;
320 		ut_asserteq(1, plat->count);
321 		device_probe(dev);
322 		device_remove(dev);
323 
324 		ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
325 		ut_asserteq(1, plat->count);
326 		ut_assertok(device_probe(dev));
327 		child_count++;
328 	}
329 	ut_asserteq(3, child_count);
330 
331 	/* Removing the bus should also have no effect (it is still bound) */
332 	device_remove(bus);
333 	for (device_find_first_child(bus, &dev), child_count = 0;
334 	     dev;
335 	     device_find_next_child(&dev)) {
336 		/* Check that platform data is allocated */
337 		plat = dev_get_parent_platdata(dev);
338 		ut_assert(plat != NULL);
339 		ut_asserteq(1, plat->count);
340 		child_count++;
341 	}
342 	ut_asserteq(3, child_count);
343 
344 	/* Unbind all the children */
345 	do {
346 		device_find_first_child(bus, &dev);
347 		if (dev)
348 			device_unbind(dev);
349 	} while (dev);
350 
351 	/* Now the child platdata should be removed and re-added */
352 	device_probe(bus);
353 	for (device_find_first_child(bus, &dev), child_count = 0;
354 	     dev;
355 	     device_find_next_child(&dev)) {
356 		/* Check that platform data is allocated */
357 		plat = dev_get_parent_platdata(dev);
358 		ut_assert(plat != NULL);
359 		ut_asserteq(0, plat->count);
360 		child_count++;
361 	}
362 	ut_asserteq(3, child_count);
363 
364 	return 0;
365 }
366 
367 /* Test that the bus can store platform data about each child */
368 static int dm_test_bus_parent_platdata(struct dm_test_state *dms)
369 {
370 	return test_bus_parent_platdata(dms);
371 }
372 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
373 
374 /* As above but the size is controlled by the uclass */
375 static int dm_test_bus_parent_platdata_uclass(struct dm_test_state *dms)
376 {
377 	struct udevice *bus;
378 	int size;
379 	int ret;
380 
381 	/* Set the driver size to 0 so that the uclass size is used */
382 	ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
383 	size = bus->driver->per_child_platdata_auto_alloc_size;
384 	bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
385 	bus->driver->per_child_platdata_auto_alloc_size = 0;
386 	ret = test_bus_parent_platdata(dms);
387 	if (ret)
388 		return ret;
389 	bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
390 	bus->driver->per_child_platdata_auto_alloc_size = size;
391 
392 	return 0;
393 }
394 DM_TEST(dm_test_bus_parent_platdata_uclass,
395 	DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
396 
397 /* Test that the child post_bind method is called */
398 static int dm_test_bus_child_post_bind(struct dm_test_state *dms)
399 {
400 	struct dm_test_parent_platdata *plat;
401 	struct udevice *bus, *dev;
402 	int child_count;
403 
404 	ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
405 	for (device_find_first_child(bus, &dev), child_count = 0;
406 	     dev;
407 	     device_find_next_child(&dev)) {
408 		/* Check that platform data is allocated */
409 		plat = dev_get_parent_platdata(dev);
410 		ut_assert(plat != NULL);
411 		ut_asserteq(1, plat->bind_flag);
412 		child_count++;
413 	}
414 	ut_asserteq(3, child_count);
415 
416 	return 0;
417 }
418 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
419