1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2014 Google, Inc
11 #include <dm/device.h>
12 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 struct dm_test_parent_platdata {
27 FLAG_CHILD_PROBED = 10,
28 FLAG_CHILD_REMOVED = -7,
31 static struct dm_test_state *test_state;
33 static int testbus_drv_probe(struct udevice *dev)
35 return dm_scan_fdt_dev(dev);
38 static int testbus_child_post_bind(struct udevice *dev)
40 struct dm_test_parent_platdata *plat;
42 plat = dev_get_parent_platdata(dev);
44 plat->uclass_bind_flag = 2;
49 static int testbus_child_pre_probe(struct udevice *dev)
51 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
53 parent_data->flag += FLAG_CHILD_PROBED;
58 static int testbus_child_pre_probe_uclass(struct udevice *dev)
60 struct dm_test_priv *priv = dev_get_priv(dev);
67 static int testbus_child_post_probe_uclass(struct udevice *dev)
69 struct dm_test_priv *priv = dev_get_priv(dev);
76 static int testbus_child_post_remove(struct udevice *dev)
78 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
79 struct dm_test_state *dms = test_state;
81 parent_data->flag += FLAG_CHILD_REMOVED;
88 static const struct udevice_id testbus_ids[] = {
90 .compatible = "denx,u-boot-test-bus",
91 .data = DM_TEST_TYPE_FIRST },
95 U_BOOT_DRIVER(testbus_drv) = {
96 .name = "testbus_drv",
97 .of_match = testbus_ids,
98 .id = UCLASS_TEST_BUS,
99 .probe = testbus_drv_probe,
100 .child_post_bind = testbus_child_post_bind,
101 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
102 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
103 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
104 .per_child_platdata_auto_alloc_size =
105 sizeof(struct dm_test_parent_platdata),
106 .child_pre_probe = testbus_child_pre_probe,
107 .child_post_remove = testbus_child_post_remove,
110 UCLASS_DRIVER(testbus) = {
112 .id = UCLASS_TEST_BUS,
113 .flags = DM_UC_FLAG_SEQ_ALIAS,
114 .child_pre_probe = testbus_child_pre_probe_uclass,
115 .child_post_probe = testbus_child_post_probe_uclass,
118 /* Test that we can probe for children */
119 static int dm_test_bus_children(struct unit_test_state *uts)
125 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
126 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
128 /* Probe the bus, which should yield 3 more devices */
129 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
132 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
133 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
135 ut_assert(!dm_check_devices(uts, num_devices));
139 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
141 /* Test our functions for accessing children */
142 static int dm_test_bus_children_funcs(struct unit_test_state *uts)
144 const void *blob = gd->fdt_blob;
145 struct udevice *bus, *dev;
148 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
150 /* device_get_child() */
151 ut_assertok(device_get_child(bus, 0, &dev));
152 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
153 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
154 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
155 ut_asserteq_str("c-test@5", dev->name);
157 /* Device with sequence number 0 should be accessible */
158 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
159 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
160 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
161 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
162 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
163 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
165 /* There is no device with sequence number 2 */
166 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
167 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
168 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
170 /* Looking for something that is not a child */
171 node = fdt_path_offset(blob, "/junk");
172 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
173 node = fdt_path_offset(blob, "/d-test");
174 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
178 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
180 static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
182 const void *blob = gd->fdt_blob;
183 struct udevice *bus, *dev;
186 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
187 ut_assertnonnull(bus);
189 /* Find a valid child */
190 node = fdt_path_offset(blob, "/some-bus/c-test@1");
192 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
193 ut_assertnonnull(dev);
194 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
195 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
196 ut_assertnonnull(dev);
197 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
201 DM_TEST(dm_test_bus_children_of_offset,
202 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
204 /* Test that we can iterate through children */
205 static int dm_test_bus_children_iterators(struct unit_test_state *uts)
207 struct udevice *bus, *dev, *child;
209 /* Walk through the children one by one */
210 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
211 ut_assertok(device_find_first_child(bus, &dev));
212 ut_asserteq_str("c-test@5", dev->name);
213 ut_assertok(device_find_next_child(&dev));
214 ut_asserteq_str("c-test@0", dev->name);
215 ut_assertok(device_find_next_child(&dev));
216 ut_asserteq_str("c-test@1", dev->name);
217 ut_assertok(device_find_next_child(&dev));
218 ut_asserteq_ptr(dev, NULL);
220 /* Move to the next child without using device_find_first_child() */
221 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
222 ut_asserteq_str("c-test@5", dev->name);
223 ut_assertok(device_find_next_child(&dev));
224 ut_asserteq_str("c-test@0", dev->name);
226 /* Try a device with no children */
227 ut_assertok(device_find_first_child(dev, &child));
228 ut_asserteq_ptr(child, NULL);
232 DM_TEST(dm_test_bus_children_iterators,
233 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
235 /* Test that the bus can store data about each child */
236 static int test_bus_parent_data(struct unit_test_state *uts)
238 struct dm_test_parent_data *parent_data;
239 struct udevice *bus, *dev;
243 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
245 /* Check that parent data is allocated */
246 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
247 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
248 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
249 parent_data = dev_get_parent_priv(dev);
250 ut_assert(NULL != parent_data);
252 /* Check that it starts at 0 and goes away when device is removed */
253 parent_data->sum += 5;
254 ut_asserteq(5, parent_data->sum);
255 device_remove(dev, DM_REMOVE_NORMAL);
256 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
258 /* Check that we can do this twice */
259 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
260 parent_data = dev_get_parent_priv(dev);
261 ut_assert(NULL != parent_data);
262 parent_data->sum += 5;
263 ut_asserteq(5, parent_data->sum);
265 /* Add parent data to all children */
266 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
268 uclass_foreach_dev(dev, uc) {
269 /* Ignore these if they are not on this bus */
270 if (dev->parent != bus) {
271 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
274 ut_assertok(device_probe(dev));
275 parent_data = dev_get_parent_priv(dev);
277 parent_data->sum = value;
281 /* Check it is still there */
283 uclass_foreach_dev(dev, uc) {
284 /* Ignore these if they are not on this bus */
285 if (dev->parent != bus)
287 parent_data = dev_get_parent_priv(dev);
289 ut_asserteq(value, parent_data->sum);
295 /* Test that the bus can store data about each child */
296 static int dm_test_bus_parent_data(struct unit_test_state *uts)
298 return test_bus_parent_data(uts);
300 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
302 /* As above but the size is controlled by the uclass */
303 static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
310 /* Set the driver size to 0 so that the uclass size is used */
311 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
312 drv = (struct driver *)bus->driver;
313 size = drv->per_child_auto_alloc_size;
315 #ifdef CONFIG_SANDBOX
316 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
317 os_mprotect_allow(drv, sizeof(*drv));
319 bus->uclass->uc_drv->per_child_auto_alloc_size = size;
320 drv->per_child_auto_alloc_size = 0;
321 ret = test_bus_parent_data(uts);
324 bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
325 drv->per_child_auto_alloc_size = size;
329 DM_TEST(dm_test_bus_parent_data_uclass,
330 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
332 /* Test that the bus ops are called when a child is probed/removed */
333 static int dm_test_bus_parent_ops(struct unit_test_state *uts)
335 struct dm_test_parent_data *parent_data;
336 struct dm_test_state *dms = uts->priv;
337 struct udevice *bus, *dev;
341 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
342 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
344 uclass_foreach_dev(dev, uc) {
345 /* Ignore these if they are not on this bus */
346 if (dev->parent != bus)
348 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
350 ut_assertok(device_probe(dev));
351 parent_data = dev_get_parent_priv(dev);
352 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
355 uclass_foreach_dev(dev, uc) {
356 /* Ignore these if they are not on this bus */
357 if (dev->parent != bus)
359 parent_data = dev_get_parent_priv(dev);
360 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
361 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
362 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
363 ut_asserteq_ptr(dms->removed, dev);
369 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
371 static int test_bus_parent_platdata(struct unit_test_state *uts)
373 struct dm_test_parent_platdata *plat;
374 struct udevice *bus, *dev;
376 /* Check that the bus has no children */
377 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
378 device_find_first_child(bus, &dev);
379 ut_asserteq_ptr(NULL, dev);
381 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
383 for (device_find_first_child(bus, &dev);
385 device_find_next_child(&dev)) {
386 /* Check that platform data is allocated */
387 plat = dev_get_parent_platdata(dev);
388 ut_assert(plat != NULL);
391 * Check that it is not affected by the device being
395 ut_asserteq(1, plat->count);
397 device_remove(dev, DM_REMOVE_NORMAL);
399 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
400 ut_asserteq(1, plat->count);
401 ut_assertok(device_probe(dev));
403 ut_asserteq(3, device_get_child_count(bus));
405 /* Removing the bus should also have no effect (it is still bound) */
406 device_remove(bus, DM_REMOVE_NORMAL);
407 for (device_find_first_child(bus, &dev);
409 device_find_next_child(&dev)) {
410 /* Check that platform data is allocated */
411 plat = dev_get_parent_platdata(dev);
412 ut_assert(plat != NULL);
413 ut_asserteq(1, plat->count);
415 ut_asserteq(3, device_get_child_count(bus));
417 /* Unbind all the children */
419 device_find_first_child(bus, &dev);
424 /* Now the child platdata should be removed and re-added */
426 for (device_find_first_child(bus, &dev);
428 device_find_next_child(&dev)) {
429 /* Check that platform data is allocated */
430 plat = dev_get_parent_platdata(dev);
431 ut_assert(plat != NULL);
432 ut_asserteq(0, plat->count);
434 ut_asserteq(3, device_get_child_count(bus));
439 /* Test that the bus can store platform data about each child */
440 static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
442 return test_bus_parent_platdata(uts);
444 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
446 /* As above but the size is controlled by the uclass */
447 static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
454 /* Set the driver size to 0 so that the uclass size is used */
455 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
456 drv = (struct driver *)bus->driver;
457 size = drv->per_child_platdata_auto_alloc_size;
458 #ifdef CONFIG_SANDBOX
459 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
460 os_mprotect_allow(drv, sizeof(*drv));
462 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
463 drv->per_child_platdata_auto_alloc_size = 0;
464 ret = test_bus_parent_platdata(uts);
467 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
468 drv->per_child_platdata_auto_alloc_size = size;
472 DM_TEST(dm_test_bus_parent_platdata_uclass,
473 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
475 /* Test that the child post_bind method is called */
476 static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
478 struct dm_test_parent_platdata *plat;
479 struct udevice *bus, *dev;
481 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
482 for (device_find_first_child(bus, &dev);
484 device_find_next_child(&dev)) {
485 /* Check that platform data is allocated */
486 plat = dev_get_parent_platdata(dev);
487 ut_assert(plat != NULL);
488 ut_asserteq(1, plat->bind_flag);
490 ut_asserteq(3, device_get_child_count(bus));
494 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
496 /* Test that the child post_bind method is called */
497 static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
499 struct dm_test_parent_platdata *plat;
500 struct udevice *bus, *dev;
502 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
503 for (device_find_first_child(bus, &dev);
505 device_find_next_child(&dev)) {
506 /* Check that platform data is allocated */
507 plat = dev_get_parent_platdata(dev);
508 ut_assert(plat != NULL);
509 ut_asserteq(2, plat->uclass_bind_flag);
511 ut_asserteq(3, device_get_child_count(bus));
515 DM_TEST(dm_test_bus_child_post_bind_uclass,
516 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
519 * Test that the bus' uclass' child_pre_probe() is called before the
520 * device's probe() method
522 static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
524 struct udevice *bus, *dev;
527 * See testfdt_drv_probe() which effectively checks that the uclass
528 * flag is set before that method is called
530 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
531 for (device_find_first_child(bus, &dev);
533 device_find_next_child(&dev)) {
534 struct dm_test_priv *priv = dev_get_priv(dev);
536 /* Check that things happened in the right order */
537 ut_asserteq_ptr(NULL, priv);
538 ut_assertok(device_probe(dev));
540 priv = dev_get_priv(dev);
541 ut_assert(priv != NULL);
542 ut_asserteq(1, priv->uclass_flag);
543 ut_asserteq(1, priv->uclass_total);
545 ut_asserteq(3, device_get_child_count(bus));
549 DM_TEST(dm_test_bus_child_pre_probe_uclass,
550 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
553 * Test that the bus' uclass' child_post_probe() is called after the
554 * device's probe() method
556 static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
558 struct udevice *bus, *dev;
561 * See testfdt_drv_probe() which effectively initializes that
562 * the uclass postp flag is set to a value
564 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
565 for (device_find_first_child(bus, &dev);
567 device_find_next_child(&dev)) {
568 struct dm_test_priv *priv = dev_get_priv(dev);
570 /* Check that things happened in the right order */
571 ut_asserteq_ptr(NULL, priv);
572 ut_assertok(device_probe(dev));
574 priv = dev_get_priv(dev);
575 ut_assert(priv != NULL);
576 ut_asserteq(0, priv->uclass_postp);
578 ut_asserteq(3, device_get_child_count(bus));
582 DM_TEST(dm_test_bus_child_post_probe_uclass,
583 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);