2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <dm/device-internal.h>
12 #include <dm/uclass-internal.h>
16 DECLARE_GLOBAL_DATA_PTR;
18 struct dm_test_parent_platdata {
25 FLAG_CHILD_PROBED = 10,
26 FLAG_CHILD_REMOVED = -7,
29 static struct dm_test_state *test_state;
31 static int testbus_drv_probe(struct udevice *dev)
33 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
36 static int testbus_child_post_bind(struct udevice *dev)
38 struct dm_test_parent_platdata *plat;
40 plat = dev_get_parent_platdata(dev);
42 plat->uclass_bind_flag = 2;
47 static int testbus_child_pre_probe(struct udevice *dev)
49 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
51 parent_data->flag += FLAG_CHILD_PROBED;
56 static int testbus_child_pre_probe_uclass(struct udevice *dev)
58 struct dm_test_priv *priv = dev_get_priv(dev);
65 static int testbus_child_post_remove(struct udevice *dev)
67 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
68 struct dm_test_state *dms = test_state;
70 parent_data->flag += FLAG_CHILD_REMOVED;
77 static const struct udevice_id testbus_ids[] = {
79 .compatible = "denx,u-boot-test-bus",
80 .data = DM_TEST_TYPE_FIRST },
84 U_BOOT_DRIVER(testbus_drv) = {
85 .name = "testbus_drv",
86 .of_match = testbus_ids,
87 .id = UCLASS_TEST_BUS,
88 .probe = testbus_drv_probe,
89 .child_post_bind = testbus_child_post_bind,
90 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
91 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
92 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
93 .per_child_platdata_auto_alloc_size =
94 sizeof(struct dm_test_parent_platdata),
95 .child_pre_probe = testbus_child_pre_probe,
96 .child_post_remove = testbus_child_post_remove,
99 UCLASS_DRIVER(testbus) = {
101 .id = UCLASS_TEST_BUS,
102 .flags = DM_UC_FLAG_SEQ_ALIAS,
103 .child_pre_probe = testbus_child_pre_probe_uclass,
106 /* Test that we can probe for children */
107 static int dm_test_bus_children(struct unit_test_state *uts)
113 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
114 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
116 /* Probe the bus, which should yield 3 more devices */
117 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
120 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
121 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
123 ut_assert(!dm_check_devices(uts, num_devices));
127 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
129 /* Test our functions for accessing children */
130 static int dm_test_bus_children_funcs(struct unit_test_state *uts)
132 const void *blob = gd->fdt_blob;
133 struct udevice *bus, *dev;
136 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
138 /* device_get_child() */
139 ut_assertok(device_get_child(bus, 0, &dev));
140 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
141 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
142 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
143 ut_asserteq_str("c-test@5", dev->name);
145 /* Device with sequence number 0 should be accessible */
146 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
147 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
148 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
149 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
150 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
151 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
153 /* There is no device with sequence number 2 */
154 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
155 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
156 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
158 /* Looking for something that is not a child */
159 node = fdt_path_offset(blob, "/junk");
160 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
161 node = fdt_path_offset(blob, "/d-test");
162 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
164 /* Find a valid child */
165 node = fdt_path_offset(blob, "/some-bus/c-test@1");
166 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
167 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
168 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
169 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
173 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
175 /* Test that we can iterate through children */
176 static int dm_test_bus_children_iterators(struct unit_test_state *uts)
178 struct udevice *bus, *dev, *child;
180 /* Walk through the children one by one */
181 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
182 ut_assertok(device_find_first_child(bus, &dev));
183 ut_asserteq_str("c-test@5", dev->name);
184 ut_assertok(device_find_next_child(&dev));
185 ut_asserteq_str("c-test@0", dev->name);
186 ut_assertok(device_find_next_child(&dev));
187 ut_asserteq_str("c-test@1", dev->name);
188 ut_assertok(device_find_next_child(&dev));
189 ut_asserteq_ptr(dev, NULL);
191 /* Move to the next child without using device_find_first_child() */
192 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
193 ut_asserteq_str("c-test@5", dev->name);
194 ut_assertok(device_find_next_child(&dev));
195 ut_asserteq_str("c-test@0", dev->name);
197 /* Try a device with no children */
198 ut_assertok(device_find_first_child(dev, &child));
199 ut_asserteq_ptr(child, NULL);
203 DM_TEST(dm_test_bus_children_iterators,
204 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
206 /* Test that the bus can store data about each child */
207 static int test_bus_parent_data(struct unit_test_state *uts)
209 struct dm_test_parent_data *parent_data;
210 struct udevice *bus, *dev;
214 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
216 /* Check that parent data is allocated */
217 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
218 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
219 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
220 parent_data = dev_get_parent_priv(dev);
221 ut_assert(NULL != parent_data);
223 /* Check that it starts at 0 and goes away when device is removed */
224 parent_data->sum += 5;
225 ut_asserteq(5, parent_data->sum);
227 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
229 /* Check that we can do this twice */
230 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
231 parent_data = dev_get_parent_priv(dev);
232 ut_assert(NULL != parent_data);
233 parent_data->sum += 5;
234 ut_asserteq(5, parent_data->sum);
236 /* Add parent data to all children */
237 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
239 uclass_foreach_dev(dev, uc) {
240 /* Ignore these if they are not on this bus */
241 if (dev->parent != bus) {
242 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
245 ut_assertok(device_probe(dev));
246 parent_data = dev_get_parent_priv(dev);
248 parent_data->sum = value;
252 /* Check it is still there */
254 uclass_foreach_dev(dev, uc) {
255 /* Ignore these if they are not on this bus */
256 if (dev->parent != bus)
258 parent_data = dev_get_parent_priv(dev);
260 ut_asserteq(value, parent_data->sum);
266 /* Test that the bus can store data about each child */
267 static int dm_test_bus_parent_data(struct unit_test_state *uts)
269 return test_bus_parent_data(uts);
271 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
273 /* As above but the size is controlled by the uclass */
274 static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
281 /* Set the driver size to 0 so that the uclass size is used */
282 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
283 drv = (struct driver *)bus->driver;
284 size = drv->per_child_auto_alloc_size;
285 bus->uclass->uc_drv->per_child_auto_alloc_size = size;
286 drv->per_child_auto_alloc_size = 0;
287 ret = test_bus_parent_data(uts);
290 bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
291 drv->per_child_auto_alloc_size = size;
295 DM_TEST(dm_test_bus_parent_data_uclass,
296 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
298 /* Test that the bus ops are called when a child is probed/removed */
299 static int dm_test_bus_parent_ops(struct unit_test_state *uts)
301 struct dm_test_parent_data *parent_data;
302 struct dm_test_state *dms = uts->priv;
303 struct udevice *bus, *dev;
307 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
308 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
310 uclass_foreach_dev(dev, uc) {
311 /* Ignore these if they are not on this bus */
312 if (dev->parent != bus)
314 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
316 ut_assertok(device_probe(dev));
317 parent_data = dev_get_parent_priv(dev);
318 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
321 uclass_foreach_dev(dev, uc) {
322 /* Ignore these if they are not on this bus */
323 if (dev->parent != bus)
325 parent_data = dev_get_parent_priv(dev);
326 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
327 ut_assertok(device_remove(dev));
328 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
329 ut_asserteq_ptr(dms->removed, dev);
335 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
337 static int test_bus_parent_platdata(struct unit_test_state *uts)
339 struct dm_test_parent_platdata *plat;
340 struct udevice *bus, *dev;
343 /* Check that the bus has no children */
344 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
345 device_find_first_child(bus, &dev);
346 ut_asserteq_ptr(NULL, dev);
348 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
350 for (device_find_first_child(bus, &dev), child_count = 0;
352 device_find_next_child(&dev)) {
353 /* Check that platform data is allocated */
354 plat = dev_get_parent_platdata(dev);
355 ut_assert(plat != NULL);
358 * Check that it is not affected by the device being
362 ut_asserteq(1, plat->count);
366 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
367 ut_asserteq(1, plat->count);
368 ut_assertok(device_probe(dev));
371 ut_asserteq(3, child_count);
373 /* Removing the bus should also have no effect (it is still bound) */
375 for (device_find_first_child(bus, &dev), child_count = 0;
377 device_find_next_child(&dev)) {
378 /* Check that platform data is allocated */
379 plat = dev_get_parent_platdata(dev);
380 ut_assert(plat != NULL);
381 ut_asserteq(1, plat->count);
384 ut_asserteq(3, child_count);
386 /* Unbind all the children */
388 device_find_first_child(bus, &dev);
393 /* Now the child platdata should be removed and re-added */
395 for (device_find_first_child(bus, &dev), child_count = 0;
397 device_find_next_child(&dev)) {
398 /* Check that platform data is allocated */
399 plat = dev_get_parent_platdata(dev);
400 ut_assert(plat != NULL);
401 ut_asserteq(0, plat->count);
404 ut_asserteq(3, child_count);
409 /* Test that the bus can store platform data about each child */
410 static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
412 return test_bus_parent_platdata(uts);
414 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
416 /* As above but the size is controlled by the uclass */
417 static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
424 /* Set the driver size to 0 so that the uclass size is used */
425 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
426 drv = (struct driver *)bus->driver;
427 size = drv->per_child_platdata_auto_alloc_size;
428 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
429 drv->per_child_platdata_auto_alloc_size = 0;
430 ret = test_bus_parent_platdata(uts);
433 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
434 drv->per_child_platdata_auto_alloc_size = size;
438 DM_TEST(dm_test_bus_parent_platdata_uclass,
439 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
441 /* Test that the child post_bind method is called */
442 static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
444 struct dm_test_parent_platdata *plat;
445 struct udevice *bus, *dev;
448 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
449 for (device_find_first_child(bus, &dev), child_count = 0;
451 device_find_next_child(&dev)) {
452 /* Check that platform data is allocated */
453 plat = dev_get_parent_platdata(dev);
454 ut_assert(plat != NULL);
455 ut_asserteq(1, plat->bind_flag);
458 ut_asserteq(3, child_count);
462 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
464 /* Test that the child post_bind method is called */
465 static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
467 struct dm_test_parent_platdata *plat;
468 struct udevice *bus, *dev;
471 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
472 for (device_find_first_child(bus, &dev), child_count = 0;
474 device_find_next_child(&dev)) {
475 /* Check that platform data is allocated */
476 plat = dev_get_parent_platdata(dev);
477 ut_assert(plat != NULL);
478 ut_asserteq(2, plat->uclass_bind_flag);
481 ut_asserteq(3, child_count);
485 DM_TEST(dm_test_bus_child_post_bind_uclass,
486 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
489 * Test that the bus' uclass' child_pre_probe() is called before the
490 * device's probe() method
492 static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
494 struct udevice *bus, *dev;
498 * See testfdt_drv_probe() which effectively checks that the uclass
499 * flag is set before that method is called
501 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
502 for (device_find_first_child(bus, &dev), child_count = 0;
504 device_find_next_child(&dev)) {
505 struct dm_test_priv *priv = dev_get_priv(dev);
507 /* Check that things happened in the right order */
508 ut_asserteq_ptr(NULL, priv);
509 ut_assertok(device_probe(dev));
511 priv = dev_get_priv(dev);
512 ut_assert(priv != NULL);
513 ut_asserteq(1, priv->uclass_flag);
514 ut_asserteq(1, priv->uclass_total);
517 ut_asserteq(3, child_count);
521 DM_TEST(dm_test_bus_child_pre_probe_uclass,
522 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);