1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
12 #include <dm/device.h>
13 #include <dm/device-internal.h>
15 #include <dm/uclass-internal.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 struct dm_test_parent_platdata {
28 FLAG_CHILD_PROBED = 10,
29 FLAG_CHILD_REMOVED = -7,
32 static struct dm_test_state *test_state;
34 static int testbus_drv_probe(struct udevice *dev)
36 return dm_scan_fdt_dev(dev);
39 static int testbus_child_post_bind(struct udevice *dev)
41 struct dm_test_parent_platdata *plat;
43 plat = dev_get_parent_platdata(dev);
45 plat->uclass_bind_flag = 2;
50 static int testbus_child_pre_probe(struct udevice *dev)
52 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
54 parent_data->flag += FLAG_CHILD_PROBED;
59 static int testbus_child_pre_probe_uclass(struct udevice *dev)
61 struct dm_test_priv *priv = dev_get_priv(dev);
68 static int testbus_child_post_probe_uclass(struct udevice *dev)
70 struct dm_test_priv *priv = dev_get_priv(dev);
77 static int testbus_child_post_remove(struct udevice *dev)
79 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
80 struct dm_test_state *dms = test_state;
82 parent_data->flag += FLAG_CHILD_REMOVED;
89 static const struct udevice_id testbus_ids[] = {
91 .compatible = "denx,u-boot-test-bus",
92 .data = DM_TEST_TYPE_FIRST },
96 U_BOOT_DRIVER(testbus_drv) = {
97 .name = "testbus_drv",
98 .of_match = testbus_ids,
99 .id = UCLASS_TEST_BUS,
100 .probe = testbus_drv_probe,
101 .child_post_bind = testbus_child_post_bind,
102 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
103 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
104 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
105 .per_child_platdata_auto_alloc_size =
106 sizeof(struct dm_test_parent_platdata),
107 .child_pre_probe = testbus_child_pre_probe,
108 .child_post_remove = testbus_child_post_remove,
111 UCLASS_DRIVER(testbus) = {
113 .id = UCLASS_TEST_BUS,
114 .flags = DM_UC_FLAG_SEQ_ALIAS,
115 .child_pre_probe = testbus_child_pre_probe_uclass,
116 .child_post_probe = testbus_child_post_probe_uclass,
119 /* Test that we can probe for children */
120 static int dm_test_bus_children(struct unit_test_state *uts)
126 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
127 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
129 /* Probe the bus, which should yield 3 more devices */
130 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
133 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
134 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
136 ut_assert(!dm_check_devices(uts, num_devices));
140 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
142 /* Test our functions for accessing children */
143 static int dm_test_bus_children_funcs(struct unit_test_state *uts)
145 const void *blob = gd->fdt_blob;
146 struct udevice *bus, *dev;
149 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
151 /* device_get_child() */
152 ut_assertok(device_get_child(bus, 0, &dev));
153 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
154 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
155 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
156 ut_asserteq_str("c-test@5", dev->name);
158 /* Device with sequence number 0 should be accessible */
159 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
160 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
161 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
162 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
163 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
164 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
166 /* There is no device with sequence number 2 */
167 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
168 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
169 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
171 /* Looking for something that is not a child */
172 node = fdt_path_offset(blob, "/junk");
173 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
174 node = fdt_path_offset(blob, "/d-test");
175 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
179 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
181 static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
183 const void *blob = gd->fdt_blob;
184 struct udevice *bus, *dev;
187 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
188 ut_assertnonnull(bus);
190 /* Find a valid child */
191 node = fdt_path_offset(blob, "/some-bus/c-test@1");
193 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
194 ut_assertnonnull(dev);
195 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
196 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
197 ut_assertnonnull(dev);
198 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
202 DM_TEST(dm_test_bus_children_of_offset,
203 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
205 /* Test that we can iterate through children */
206 static int dm_test_bus_children_iterators(struct unit_test_state *uts)
208 struct udevice *bus, *dev, *child;
210 /* Walk through the children one by one */
211 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
212 ut_assertok(device_find_first_child(bus, &dev));
213 ut_asserteq_str("c-test@5", dev->name);
214 ut_assertok(device_find_next_child(&dev));
215 ut_asserteq_str("c-test@0", dev->name);
216 ut_assertok(device_find_next_child(&dev));
217 ut_asserteq_str("c-test@1", dev->name);
218 ut_assertok(device_find_next_child(&dev));
219 ut_asserteq_ptr(dev, NULL);
221 /* Move to the next child without using device_find_first_child() */
222 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
223 ut_asserteq_str("c-test@5", dev->name);
224 ut_assertok(device_find_next_child(&dev));
225 ut_asserteq_str("c-test@0", dev->name);
227 /* Try a device with no children */
228 ut_assertok(device_find_first_child(dev, &child));
229 ut_asserteq_ptr(child, NULL);
233 DM_TEST(dm_test_bus_children_iterators,
234 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
236 /* Test that the bus can store data about each child */
237 static int test_bus_parent_data(struct unit_test_state *uts)
239 struct dm_test_parent_data *parent_data;
240 struct udevice *bus, *dev;
244 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
246 /* Check that parent data is allocated */
247 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
248 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
249 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
250 parent_data = dev_get_parent_priv(dev);
251 ut_assert(NULL != parent_data);
253 /* Check that it starts at 0 and goes away when device is removed */
254 parent_data->sum += 5;
255 ut_asserteq(5, parent_data->sum);
256 device_remove(dev, DM_REMOVE_NORMAL);
257 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
259 /* Check that we can do this twice */
260 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
261 parent_data = dev_get_parent_priv(dev);
262 ut_assert(NULL != parent_data);
263 parent_data->sum += 5;
264 ut_asserteq(5, parent_data->sum);
266 /* Add parent data to all children */
267 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
269 uclass_foreach_dev(dev, uc) {
270 /* Ignore these if they are not on this bus */
271 if (dev->parent != bus) {
272 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
275 ut_assertok(device_probe(dev));
276 parent_data = dev_get_parent_priv(dev);
278 parent_data->sum = value;
282 /* Check it is still there */
284 uclass_foreach_dev(dev, uc) {
285 /* Ignore these if they are not on this bus */
286 if (dev->parent != bus)
288 parent_data = dev_get_parent_priv(dev);
290 ut_asserteq(value, parent_data->sum);
296 /* Test that the bus can store data about each child */
297 static int dm_test_bus_parent_data(struct unit_test_state *uts)
299 return test_bus_parent_data(uts);
301 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
303 /* As above but the size is controlled by the uclass */
304 static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
311 /* Set the driver size to 0 so that the uclass size is used */
312 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
313 drv = (struct driver *)bus->driver;
314 size = drv->per_child_auto_alloc_size;
316 #ifdef CONFIG_SANDBOX
317 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
318 os_mprotect_allow(drv, sizeof(*drv));
320 bus->uclass->uc_drv->per_child_auto_alloc_size = size;
321 drv->per_child_auto_alloc_size = 0;
322 ret = test_bus_parent_data(uts);
325 bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
326 drv->per_child_auto_alloc_size = size;
330 DM_TEST(dm_test_bus_parent_data_uclass,
331 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
333 /* Test that the bus ops are called when a child is probed/removed */
334 static int dm_test_bus_parent_ops(struct unit_test_state *uts)
336 struct dm_test_parent_data *parent_data;
337 struct dm_test_state *dms = uts->priv;
338 struct udevice *bus, *dev;
342 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
343 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
345 uclass_foreach_dev(dev, uc) {
346 /* Ignore these if they are not on this bus */
347 if (dev->parent != bus)
349 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
351 ut_assertok(device_probe(dev));
352 parent_data = dev_get_parent_priv(dev);
353 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
356 uclass_foreach_dev(dev, uc) {
357 /* Ignore these if they are not on this bus */
358 if (dev->parent != bus)
360 parent_data = dev_get_parent_priv(dev);
361 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
362 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
363 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
364 ut_asserteq_ptr(dms->removed, dev);
370 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
372 static int test_bus_parent_platdata(struct unit_test_state *uts)
374 struct dm_test_parent_platdata *plat;
375 struct udevice *bus, *dev;
377 /* Check that the bus has no children */
378 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
379 device_find_first_child(bus, &dev);
380 ut_asserteq_ptr(NULL, dev);
382 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
384 for (device_find_first_child(bus, &dev);
386 device_find_next_child(&dev)) {
387 /* Check that platform data is allocated */
388 plat = dev_get_parent_platdata(dev);
389 ut_assert(plat != NULL);
392 * Check that it is not affected by the device being
396 ut_asserteq(1, plat->count);
398 device_remove(dev, DM_REMOVE_NORMAL);
400 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
401 ut_asserteq(1, plat->count);
402 ut_assertok(device_probe(dev));
404 ut_asserteq(3, device_get_child_count(bus));
406 /* Removing the bus should also have no effect (it is still bound) */
407 device_remove(bus, DM_REMOVE_NORMAL);
408 for (device_find_first_child(bus, &dev);
410 device_find_next_child(&dev)) {
411 /* Check that platform data is allocated */
412 plat = dev_get_parent_platdata(dev);
413 ut_assert(plat != NULL);
414 ut_asserteq(1, plat->count);
416 ut_asserteq(3, device_get_child_count(bus));
418 /* Unbind all the children */
420 device_find_first_child(bus, &dev);
425 /* Now the child platdata should be removed and re-added */
427 for (device_find_first_child(bus, &dev);
429 device_find_next_child(&dev)) {
430 /* Check that platform data is allocated */
431 plat = dev_get_parent_platdata(dev);
432 ut_assert(plat != NULL);
433 ut_asserteq(0, plat->count);
435 ut_asserteq(3, device_get_child_count(bus));
440 /* Test that the bus can store platform data about each child */
441 static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
443 return test_bus_parent_platdata(uts);
445 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
447 /* As above but the size is controlled by the uclass */
448 static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
455 /* Set the driver size to 0 so that the uclass size is used */
456 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
457 drv = (struct driver *)bus->driver;
458 size = drv->per_child_platdata_auto_alloc_size;
459 #ifdef CONFIG_SANDBOX
460 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
461 os_mprotect_allow(drv, sizeof(*drv));
463 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
464 drv->per_child_platdata_auto_alloc_size = 0;
465 ret = test_bus_parent_platdata(uts);
468 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
469 drv->per_child_platdata_auto_alloc_size = size;
473 DM_TEST(dm_test_bus_parent_platdata_uclass,
474 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
476 /* Test that the child post_bind method is called */
477 static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
479 struct dm_test_parent_platdata *plat;
480 struct udevice *bus, *dev;
482 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
483 for (device_find_first_child(bus, &dev);
485 device_find_next_child(&dev)) {
486 /* Check that platform data is allocated */
487 plat = dev_get_parent_platdata(dev);
488 ut_assert(plat != NULL);
489 ut_asserteq(1, plat->bind_flag);
491 ut_asserteq(3, device_get_child_count(bus));
495 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
497 /* Test that the child post_bind method is called */
498 static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
500 struct dm_test_parent_platdata *plat;
501 struct udevice *bus, *dev;
503 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
504 for (device_find_first_child(bus, &dev);
506 device_find_next_child(&dev)) {
507 /* Check that platform data is allocated */
508 plat = dev_get_parent_platdata(dev);
509 ut_assert(plat != NULL);
510 ut_asserteq(2, plat->uclass_bind_flag);
512 ut_asserteq(3, device_get_child_count(bus));
516 DM_TEST(dm_test_bus_child_post_bind_uclass,
517 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
520 * Test that the bus' uclass' child_pre_probe() is called before the
521 * device's probe() method
523 static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
525 struct udevice *bus, *dev;
528 * See testfdt_drv_probe() which effectively checks that the uclass
529 * flag is set before that method is called
531 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
532 for (device_find_first_child(bus, &dev);
534 device_find_next_child(&dev)) {
535 struct dm_test_priv *priv = dev_get_priv(dev);
537 /* Check that things happened in the right order */
538 ut_asserteq_ptr(NULL, priv);
539 ut_assertok(device_probe(dev));
541 priv = dev_get_priv(dev);
542 ut_assert(priv != NULL);
543 ut_asserteq(1, priv->uclass_flag);
544 ut_asserteq(1, priv->uclass_total);
546 ut_asserteq(3, device_get_child_count(bus));
550 DM_TEST(dm_test_bus_child_pre_probe_uclass,
551 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
554 * Test that the bus' uclass' child_post_probe() is called after the
555 * device's probe() method
557 static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
559 struct udevice *bus, *dev;
562 * See testfdt_drv_probe() which effectively initializes that
563 * the uclass postp flag is set to a value
565 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
566 for (device_find_first_child(bus, &dev);
568 device_find_next_child(&dev)) {
569 struct dm_test_priv *priv = dev_get_priv(dev);
571 /* Check that things happened in the right order */
572 ut_asserteq_ptr(NULL, priv);
573 ut_assertok(device_probe(dev));
575 priv = dev_get_priv(dev);
576 ut_assert(priv != NULL);
577 ut_asserteq(0, priv->uclass_postp);
579 ut_asserteq(3, device_get_child_count(bus));
583 DM_TEST(dm_test_bus_child_post_probe_uclass,
584 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);