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 {
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_node(dev, gd->fdt_blob, dev->of_offset, false);
35 static int testbus_child_post_bind(struct udevice *dev)
37 struct dm_test_parent_platdata *plat;
39 plat = dev_get_parent_platdata(dev);
45 static int testbus_child_pre_probe(struct udevice *dev)
47 struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
49 parent_data->flag += FLAG_CHILD_PROBED;
54 static int testbus_child_post_remove(struct udevice *dev)
56 struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
57 struct dm_test_state *dms = test_state;
59 parent_data->flag += FLAG_CHILD_REMOVED;
66 static const struct udevice_id testbus_ids[] = {
68 .compatible = "denx,u-boot-test-bus",
69 .data = DM_TEST_TYPE_FIRST },
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,
88 UCLASS_DRIVER(testbus) = {
90 .id = UCLASS_TEST_BUS,
93 /* Test that we can probe for children */
94 static int dm_test_bus_children(struct dm_test_state *dms)
100 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
101 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
103 /* Probe the bus, which should yield 3 more devices */
104 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
107 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
108 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
110 ut_assert(!dm_check_devices(dms, num_devices));
114 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
116 /* Test our functions for accessing children */
117 static int dm_test_bus_children_funcs(struct dm_test_state *dms)
119 const void *blob = gd->fdt_blob;
120 struct udevice *bus, *dev;
123 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
125 /* device_get_child() */
126 ut_assertok(device_get_child(bus, 0, &dev));
127 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
128 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
129 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
130 ut_asserteq_str("c-test@5", dev->name);
132 /* Device with sequence number 0 should be accessible */
133 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
134 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
135 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
136 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
137 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
138 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
140 /* There is no device with sequence number 2 */
141 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
142 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
143 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
145 /* Looking for something that is not a child */
146 node = fdt_path_offset(blob, "/junk");
147 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
148 node = fdt_path_offset(blob, "/d-test");
149 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
151 /* Find a valid child */
152 node = fdt_path_offset(blob, "/some-bus/c-test@1");
153 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
154 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
155 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
156 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
160 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
162 /* Test that we can iterate through children */
163 static int dm_test_bus_children_iterators(struct dm_test_state *dms)
165 struct udevice *bus, *dev, *child;
167 /* Walk through the children one by one */
168 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
169 ut_assertok(device_find_first_child(bus, &dev));
170 ut_asserteq_str("c-test@5", dev->name);
171 ut_assertok(device_find_next_child(&dev));
172 ut_asserteq_str("c-test@0", dev->name);
173 ut_assertok(device_find_next_child(&dev));
174 ut_asserteq_str("c-test@1", dev->name);
175 ut_assertok(device_find_next_child(&dev));
176 ut_asserteq_ptr(dev, NULL);
178 /* Move to the next child without using device_find_first_child() */
179 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
180 ut_asserteq_str("c-test@5", dev->name);
181 ut_assertok(device_find_next_child(&dev));
182 ut_asserteq_str("c-test@0", dev->name);
184 /* Try a device with no children */
185 ut_assertok(device_find_first_child(dev, &child));
186 ut_asserteq_ptr(child, NULL);
190 DM_TEST(dm_test_bus_children_iterators,
191 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
193 /* Test that the bus can store data about each child */
194 static int dm_test_bus_parent_data(struct dm_test_state *dms)
196 struct dm_test_parent_data *parent_data;
197 struct udevice *bus, *dev;
201 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
203 /* Check that parent data is allocated */
204 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
205 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
206 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
207 parent_data = dev_get_parentdata(dev);
208 ut_assert(NULL != parent_data);
210 /* Check that it starts at 0 and goes away when device is removed */
211 parent_data->sum += 5;
212 ut_asserteq(5, parent_data->sum);
214 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
216 /* Check that we can do this twice */
217 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
218 parent_data = dev_get_parentdata(dev);
219 ut_assert(NULL != parent_data);
220 parent_data->sum += 5;
221 ut_asserteq(5, parent_data->sum);
223 /* Add parent data to all children */
224 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
226 uclass_foreach_dev(dev, uc) {
227 /* Ignore these if they are not on this bus */
228 if (dev->parent != bus) {
229 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
232 ut_assertok(device_probe(dev));
233 parent_data = dev_get_parentdata(dev);
235 parent_data->sum = value;
239 /* Check it is still there */
241 uclass_foreach_dev(dev, uc) {
242 /* Ignore these if they are not on this bus */
243 if (dev->parent != bus)
245 parent_data = dev_get_parentdata(dev);
247 ut_asserteq(value, parent_data->sum);
254 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
256 /* Test that the bus ops are called when a child is probed/removed */
257 static int dm_test_bus_parent_ops(struct dm_test_state *dms)
259 struct dm_test_parent_data *parent_data;
260 struct udevice *bus, *dev;
264 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
265 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
267 uclass_foreach_dev(dev, uc) {
268 /* Ignore these if they are not on this bus */
269 if (dev->parent != bus)
271 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
273 ut_assertok(device_probe(dev));
274 parent_data = dev_get_parentdata(dev);
275 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
278 uclass_foreach_dev(dev, uc) {
279 /* Ignore these if they are not on this bus */
280 if (dev->parent != bus)
282 parent_data = dev_get_parentdata(dev);
283 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
284 ut_assertok(device_remove(dev));
285 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
286 ut_asserteq_ptr(dms->removed, dev);
292 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
294 static int test_bus_parent_platdata(struct dm_test_state *dms)
296 struct dm_test_parent_platdata *plat;
297 struct udevice *bus, *dev;
300 /* Check that the bus has no children */
301 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
302 device_find_first_child(bus, &dev);
303 ut_asserteq_ptr(NULL, dev);
305 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
307 for (device_find_first_child(bus, &dev), child_count = 0;
309 device_find_next_child(&dev)) {
310 /* Check that platform data is allocated */
311 plat = dev_get_parent_platdata(dev);
312 ut_assert(plat != NULL);
315 * Check that it is not affected by the device being
319 ut_asserteq(1, plat->count);
323 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
324 ut_asserteq(1, plat->count);
325 ut_assertok(device_probe(dev));
328 ut_asserteq(3, child_count);
330 /* Removing the bus should also have no effect (it is still bound) */
332 for (device_find_first_child(bus, &dev), child_count = 0;
334 device_find_next_child(&dev)) {
335 /* Check that platform data is allocated */
336 plat = dev_get_parent_platdata(dev);
337 ut_assert(plat != NULL);
338 ut_asserteq(1, plat->count);
341 ut_asserteq(3, child_count);
343 /* Unbind all the children */
345 device_find_first_child(bus, &dev);
350 /* Now the child platdata should be removed and re-added */
352 for (device_find_first_child(bus, &dev), child_count = 0;
354 device_find_next_child(&dev)) {
355 /* Check that platform data is allocated */
356 plat = dev_get_parent_platdata(dev);
357 ut_assert(plat != NULL);
358 ut_asserteq(0, plat->count);
361 ut_asserteq(3, child_count);
366 /* Test that the bus can store platform data about each child */
367 static int dm_test_bus_parent_platdata(struct dm_test_state *dms)
369 return test_bus_parent_platdata(dms);
371 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
373 /* As above but the size is controlled by the uclass */
374 static int dm_test_bus_parent_platdata_uclass(struct dm_test_state *dms)
380 /* Set the driver size to 0 so that the uclass size is used */
381 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
382 size = bus->driver->per_child_platdata_auto_alloc_size;
383 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
384 bus->driver->per_child_platdata_auto_alloc_size = 0;
385 ret = test_bus_parent_platdata(dms);
388 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
389 bus->driver->per_child_platdata_auto_alloc_size = size;
393 DM_TEST(dm_test_bus_parent_platdata_uclass,
394 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
396 /* Test that the child post_bind method is called */
397 static int dm_test_bus_child_post_bind(struct dm_test_state *dms)
399 struct dm_test_parent_platdata *plat;
400 struct udevice *bus, *dev;
403 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
404 for (device_find_first_child(bus, &dev), child_count = 0;
406 device_find_next_child(&dev)) {
407 /* Check that platform data is allocated */
408 plat = dev_get_parent_platdata(dev);
409 ut_assert(plat != NULL);
410 ut_asserteq(1, plat->bind_flag);
413 ut_asserteq(3, child_count);
417 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);