2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <dm/device-internal.h>
11 #include <dm/uclass-internal.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 struct dm_test_parent_platdata {
24 FLAG_CHILD_PROBED = 10,
25 FLAG_CHILD_REMOVED = -7,
28 static struct dm_test_state *test_state;
30 static int testbus_drv_probe(struct udevice *dev)
32 return dm_scan_fdt_dev(dev);
35 static int testbus_child_post_bind(struct udevice *dev)
37 struct dm_test_parent_platdata *plat;
39 plat = dev_get_parent_platdata(dev);
41 plat->uclass_bind_flag = 2;
46 static int testbus_child_pre_probe(struct udevice *dev)
48 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
50 parent_data->flag += FLAG_CHILD_PROBED;
55 static int testbus_child_pre_probe_uclass(struct udevice *dev)
57 struct dm_test_priv *priv = dev_get_priv(dev);
64 static int testbus_child_post_remove(struct udevice *dev)
66 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
67 struct dm_test_state *dms = test_state;
69 parent_data->flag += FLAG_CHILD_REMOVED;
76 static const struct udevice_id testbus_ids[] = {
78 .compatible = "denx,u-boot-test-bus",
79 .data = DM_TEST_TYPE_FIRST },
83 U_BOOT_DRIVER(testbus_drv) = {
84 .name = "testbus_drv",
85 .of_match = testbus_ids,
86 .id = UCLASS_TEST_BUS,
87 .probe = testbus_drv_probe,
88 .child_post_bind = testbus_child_post_bind,
89 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
90 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
91 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
92 .per_child_platdata_auto_alloc_size =
93 sizeof(struct dm_test_parent_platdata),
94 .child_pre_probe = testbus_child_pre_probe,
95 .child_post_remove = testbus_child_post_remove,
98 UCLASS_DRIVER(testbus) = {
100 .id = UCLASS_TEST_BUS,
101 .flags = DM_UC_FLAG_SEQ_ALIAS,
102 .child_pre_probe = testbus_child_pre_probe_uclass,
105 /* Test that we can probe for children */
106 static int dm_test_bus_children(struct unit_test_state *uts)
112 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
113 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
115 /* Probe the bus, which should yield 3 more devices */
116 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
119 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
120 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
122 ut_assert(!dm_check_devices(uts, num_devices));
126 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
128 /* Test our functions for accessing children */
129 static int dm_test_bus_children_funcs(struct unit_test_state *uts)
131 const void *blob = gd->fdt_blob;
132 struct udevice *bus, *dev;
135 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
137 /* device_get_child() */
138 ut_assertok(device_get_child(bus, 0, &dev));
139 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
140 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
141 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
142 ut_asserteq_str("c-test@5", dev->name);
144 /* Device with sequence number 0 should be accessible */
145 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
146 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
147 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
148 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
149 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
150 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
152 /* There is no device with sequence number 2 */
153 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
154 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
155 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
157 /* Looking for something that is not a child */
158 node = fdt_path_offset(blob, "/junk");
159 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
160 node = fdt_path_offset(blob, "/d-test");
161 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
165 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
167 static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
169 const void *blob = gd->fdt_blob;
170 struct udevice *bus, *dev;
173 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
175 /* Find a valid child */
176 node = fdt_path_offset(blob, "/some-bus/c-test@1");
178 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
179 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
180 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
181 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
185 DM_TEST(dm_test_bus_children_of_offset,
186 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
188 /* Test that we can iterate through children */
189 static int dm_test_bus_children_iterators(struct unit_test_state *uts)
191 struct udevice *bus, *dev, *child;
193 /* Walk through the children one by one */
194 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
195 ut_assertok(device_find_first_child(bus, &dev));
196 ut_asserteq_str("c-test@5", dev->name);
197 ut_assertok(device_find_next_child(&dev));
198 ut_asserteq_str("c-test@0", dev->name);
199 ut_assertok(device_find_next_child(&dev));
200 ut_asserteq_str("c-test@1", dev->name);
201 ut_assertok(device_find_next_child(&dev));
202 ut_asserteq_ptr(dev, NULL);
204 /* Move to the next child without using device_find_first_child() */
205 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
206 ut_asserteq_str("c-test@5", dev->name);
207 ut_assertok(device_find_next_child(&dev));
208 ut_asserteq_str("c-test@0", dev->name);
210 /* Try a device with no children */
211 ut_assertok(device_find_first_child(dev, &child));
212 ut_asserteq_ptr(child, NULL);
216 DM_TEST(dm_test_bus_children_iterators,
217 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
219 /* Test that the bus can store data about each child */
220 static int test_bus_parent_data(struct unit_test_state *uts)
222 struct dm_test_parent_data *parent_data;
223 struct udevice *bus, *dev;
227 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
229 /* Check that parent data is allocated */
230 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
231 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
232 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
233 parent_data = dev_get_parent_priv(dev);
234 ut_assert(NULL != parent_data);
236 /* Check that it starts at 0 and goes away when device is removed */
237 parent_data->sum += 5;
238 ut_asserteq(5, parent_data->sum);
239 device_remove(dev, DM_REMOVE_NORMAL);
240 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
242 /* Check that we can do this twice */
243 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
244 parent_data = dev_get_parent_priv(dev);
245 ut_assert(NULL != parent_data);
246 parent_data->sum += 5;
247 ut_asserteq(5, parent_data->sum);
249 /* Add parent data to all children */
250 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
252 uclass_foreach_dev(dev, uc) {
253 /* Ignore these if they are not on this bus */
254 if (dev->parent != bus) {
255 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
258 ut_assertok(device_probe(dev));
259 parent_data = dev_get_parent_priv(dev);
261 parent_data->sum = value;
265 /* Check it is still there */
267 uclass_foreach_dev(dev, uc) {
268 /* Ignore these if they are not on this bus */
269 if (dev->parent != bus)
271 parent_data = dev_get_parent_priv(dev);
273 ut_asserteq(value, parent_data->sum);
279 /* Test that the bus can store data about each child */
280 static int dm_test_bus_parent_data(struct unit_test_state *uts)
282 return test_bus_parent_data(uts);
284 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
286 /* As above but the size is controlled by the uclass */
287 static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
294 /* Set the driver size to 0 so that the uclass size is used */
295 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
296 drv = (struct driver *)bus->driver;
297 size = drv->per_child_auto_alloc_size;
298 bus->uclass->uc_drv->per_child_auto_alloc_size = size;
299 drv->per_child_auto_alloc_size = 0;
300 ret = test_bus_parent_data(uts);
303 bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
304 drv->per_child_auto_alloc_size = size;
308 DM_TEST(dm_test_bus_parent_data_uclass,
309 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
311 /* Test that the bus ops are called when a child is probed/removed */
312 static int dm_test_bus_parent_ops(struct unit_test_state *uts)
314 struct dm_test_parent_data *parent_data;
315 struct dm_test_state *dms = uts->priv;
316 struct udevice *bus, *dev;
320 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
321 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
323 uclass_foreach_dev(dev, uc) {
324 /* Ignore these if they are not on this bus */
325 if (dev->parent != bus)
327 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
329 ut_assertok(device_probe(dev));
330 parent_data = dev_get_parent_priv(dev);
331 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
334 uclass_foreach_dev(dev, uc) {
335 /* Ignore these if they are not on this bus */
336 if (dev->parent != bus)
338 parent_data = dev_get_parent_priv(dev);
339 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
340 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
341 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
342 ut_asserteq_ptr(dms->removed, dev);
348 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
350 static int test_bus_parent_platdata(struct unit_test_state *uts)
352 struct dm_test_parent_platdata *plat;
353 struct udevice *bus, *dev;
356 /* Check that the bus has no children */
357 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
358 device_find_first_child(bus, &dev);
359 ut_asserteq_ptr(NULL, dev);
361 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
363 for (device_find_first_child(bus, &dev), child_count = 0;
365 device_find_next_child(&dev)) {
366 /* Check that platform data is allocated */
367 plat = dev_get_parent_platdata(dev);
368 ut_assert(plat != NULL);
371 * Check that it is not affected by the device being
375 ut_asserteq(1, plat->count);
377 device_remove(dev, DM_REMOVE_NORMAL);
379 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
380 ut_asserteq(1, plat->count);
381 ut_assertok(device_probe(dev));
384 ut_asserteq(3, child_count);
386 /* Removing the bus should also have no effect (it is still bound) */
387 device_remove(bus, DM_REMOVE_NORMAL);
388 for (device_find_first_child(bus, &dev), child_count = 0;
390 device_find_next_child(&dev)) {
391 /* Check that platform data is allocated */
392 plat = dev_get_parent_platdata(dev);
393 ut_assert(plat != NULL);
394 ut_asserteq(1, plat->count);
397 ut_asserteq(3, child_count);
399 /* Unbind all the children */
401 device_find_first_child(bus, &dev);
406 /* Now the child platdata should be removed and re-added */
408 for (device_find_first_child(bus, &dev), child_count = 0;
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(0, plat->count);
417 ut_asserteq(3, child_count);
422 /* Test that the bus can store platform data about each child */
423 static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
425 return test_bus_parent_platdata(uts);
427 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
429 /* As above but the size is controlled by the uclass */
430 static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
437 /* Set the driver size to 0 so that the uclass size is used */
438 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
439 drv = (struct driver *)bus->driver;
440 size = drv->per_child_platdata_auto_alloc_size;
441 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
442 drv->per_child_platdata_auto_alloc_size = 0;
443 ret = test_bus_parent_platdata(uts);
446 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
447 drv->per_child_platdata_auto_alloc_size = size;
451 DM_TEST(dm_test_bus_parent_platdata_uclass,
452 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
454 /* Test that the child post_bind method is called */
455 static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
457 struct dm_test_parent_platdata *plat;
458 struct udevice *bus, *dev;
461 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
462 for (device_find_first_child(bus, &dev), child_count = 0;
464 device_find_next_child(&dev)) {
465 /* Check that platform data is allocated */
466 plat = dev_get_parent_platdata(dev);
467 ut_assert(plat != NULL);
468 ut_asserteq(1, plat->bind_flag);
471 ut_asserteq(3, child_count);
475 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
477 /* Test that the child post_bind method is called */
478 static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
480 struct dm_test_parent_platdata *plat;
481 struct udevice *bus, *dev;
484 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
485 for (device_find_first_child(bus, &dev), child_count = 0;
487 device_find_next_child(&dev)) {
488 /* Check that platform data is allocated */
489 plat = dev_get_parent_platdata(dev);
490 ut_assert(plat != NULL);
491 ut_asserteq(2, plat->uclass_bind_flag);
494 ut_asserteq(3, child_count);
498 DM_TEST(dm_test_bus_child_post_bind_uclass,
499 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
502 * Test that the bus' uclass' child_pre_probe() is called before the
503 * device's probe() method
505 static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
507 struct udevice *bus, *dev;
511 * See testfdt_drv_probe() which effectively checks that the uclass
512 * flag is set before that method is called
514 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
515 for (device_find_first_child(bus, &dev), child_count = 0;
517 device_find_next_child(&dev)) {
518 struct dm_test_priv *priv = dev_get_priv(dev);
520 /* Check that things happened in the right order */
521 ut_asserteq_ptr(NULL, priv);
522 ut_assertok(device_probe(dev));
524 priv = dev_get_priv(dev);
525 ut_assert(priv != NULL);
526 ut_asserteq(1, priv->uclass_flag);
527 ut_asserteq(1, priv->uclass_total);
530 ut_asserteq(3, child_count);
534 DM_TEST(dm_test_bus_child_pre_probe_uclass,
535 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);