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_parentdata(dev);
51 parent_data->flag += FLAG_CHILD_PROBED;
56 static int testbus_child_post_remove(struct udevice *dev)
58 struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
59 struct dm_test_state *dms = test_state;
61 parent_data->flag += FLAG_CHILD_REMOVED;
68 static const struct udevice_id testbus_ids[] = {
70 .compatible = "denx,u-boot-test-bus",
71 .data = DM_TEST_TYPE_FIRST },
75 U_BOOT_DRIVER(testbus_drv) = {
76 .name = "testbus_drv",
77 .of_match = testbus_ids,
78 .id = UCLASS_TEST_BUS,
79 .probe = testbus_drv_probe,
80 .child_post_bind = testbus_child_post_bind,
81 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
82 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
83 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
84 .per_child_platdata_auto_alloc_size =
85 sizeof(struct dm_test_parent_platdata),
86 .child_pre_probe = testbus_child_pre_probe,
87 .child_post_remove = testbus_child_post_remove,
90 UCLASS_DRIVER(testbus) = {
92 .id = UCLASS_TEST_BUS,
93 .flags = DM_UC_FLAG_SEQ_ALIAS,
96 /* Test that we can probe for children */
97 static int dm_test_bus_children(struct dm_test_state *dms)
103 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
104 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
106 /* Probe the bus, which should yield 3 more devices */
107 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
110 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
111 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
113 ut_assert(!dm_check_devices(dms, num_devices));
117 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
119 /* Test our functions for accessing children */
120 static int dm_test_bus_children_funcs(struct dm_test_state *dms)
122 const void *blob = gd->fdt_blob;
123 struct udevice *bus, *dev;
126 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
128 /* device_get_child() */
129 ut_assertok(device_get_child(bus, 0, &dev));
130 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
131 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
132 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
133 ut_asserteq_str("c-test@5", dev->name);
135 /* Device with sequence number 0 should be accessible */
136 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
137 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
138 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
139 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
140 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
141 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
143 /* There is no device with sequence number 2 */
144 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
145 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
146 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
148 /* Looking for something that is not a child */
149 node = fdt_path_offset(blob, "/junk");
150 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
151 node = fdt_path_offset(blob, "/d-test");
152 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
154 /* Find a valid child */
155 node = fdt_path_offset(blob, "/some-bus/c-test@1");
156 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
157 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
158 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
159 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
163 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
165 /* Test that we can iterate through children */
166 static int dm_test_bus_children_iterators(struct dm_test_state *dms)
168 struct udevice *bus, *dev, *child;
170 /* Walk through the children one by one */
171 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
172 ut_assertok(device_find_first_child(bus, &dev));
173 ut_asserteq_str("c-test@5", dev->name);
174 ut_assertok(device_find_next_child(&dev));
175 ut_asserteq_str("c-test@0", dev->name);
176 ut_assertok(device_find_next_child(&dev));
177 ut_asserteq_str("c-test@1", dev->name);
178 ut_assertok(device_find_next_child(&dev));
179 ut_asserteq_ptr(dev, NULL);
181 /* Move to the next child without using device_find_first_child() */
182 ut_assertok(device_find_child_by_seq(bus, 5, true, &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);
187 /* Try a device with no children */
188 ut_assertok(device_find_first_child(dev, &child));
189 ut_asserteq_ptr(child, NULL);
193 DM_TEST(dm_test_bus_children_iterators,
194 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
196 /* Test that the bus can store data about each child */
197 static int test_bus_parent_data(struct dm_test_state *dms)
199 struct dm_test_parent_data *parent_data;
200 struct udevice *bus, *dev;
204 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
206 /* Check that parent data is allocated */
207 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
208 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
209 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
210 parent_data = dev_get_parentdata(dev);
211 ut_assert(NULL != parent_data);
213 /* Check that it starts at 0 and goes away when device is removed */
214 parent_data->sum += 5;
215 ut_asserteq(5, parent_data->sum);
217 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
219 /* Check that we can do this twice */
220 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
221 parent_data = dev_get_parentdata(dev);
222 ut_assert(NULL != parent_data);
223 parent_data->sum += 5;
224 ut_asserteq(5, parent_data->sum);
226 /* Add parent data to all children */
227 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
229 uclass_foreach_dev(dev, uc) {
230 /* Ignore these if they are not on this bus */
231 if (dev->parent != bus) {
232 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
235 ut_assertok(device_probe(dev));
236 parent_data = dev_get_parentdata(dev);
238 parent_data->sum = value;
242 /* Check it is still there */
244 uclass_foreach_dev(dev, uc) {
245 /* Ignore these if they are not on this bus */
246 if (dev->parent != bus)
248 parent_data = dev_get_parentdata(dev);
250 ut_asserteq(value, parent_data->sum);
256 /* Test that the bus can store data about each child */
257 static int dm_test_bus_parent_data(struct dm_test_state *dms)
259 return test_bus_parent_data(dms);
261 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
263 /* As above but the size is controlled by the uclass */
264 static int dm_test_bus_parent_data_uclass(struct dm_test_state *dms)
270 /* Set the driver size to 0 so that the uclass size is used */
271 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
272 size = bus->driver->per_child_auto_alloc_size;
273 bus->uclass->uc_drv->per_child_auto_alloc_size = size;
274 bus->driver->per_child_auto_alloc_size = 0;
275 ret = test_bus_parent_data(dms);
278 bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
279 bus->driver->per_child_auto_alloc_size = size;
283 DM_TEST(dm_test_bus_parent_data_uclass,
284 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
286 /* Test that the bus ops are called when a child is probed/removed */
287 static int dm_test_bus_parent_ops(struct dm_test_state *dms)
289 struct dm_test_parent_data *parent_data;
290 struct udevice *bus, *dev;
294 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
295 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
297 uclass_foreach_dev(dev, uc) {
298 /* Ignore these if they are not on this bus */
299 if (dev->parent != bus)
301 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
303 ut_assertok(device_probe(dev));
304 parent_data = dev_get_parentdata(dev);
305 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
308 uclass_foreach_dev(dev, uc) {
309 /* Ignore these if they are not on this bus */
310 if (dev->parent != bus)
312 parent_data = dev_get_parentdata(dev);
313 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
314 ut_assertok(device_remove(dev));
315 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
316 ut_asserteq_ptr(dms->removed, dev);
322 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
324 static int test_bus_parent_platdata(struct dm_test_state *dms)
326 struct dm_test_parent_platdata *plat;
327 struct udevice *bus, *dev;
330 /* Check that the bus has no children */
331 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
332 device_find_first_child(bus, &dev);
333 ut_asserteq_ptr(NULL, dev);
335 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
337 for (device_find_first_child(bus, &dev), child_count = 0;
339 device_find_next_child(&dev)) {
340 /* Check that platform data is allocated */
341 plat = dev_get_parent_platdata(dev);
342 ut_assert(plat != NULL);
345 * Check that it is not affected by the device being
349 ut_asserteq(1, plat->count);
353 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
354 ut_asserteq(1, plat->count);
355 ut_assertok(device_probe(dev));
358 ut_asserteq(3, child_count);
360 /* Removing the bus should also have no effect (it is still bound) */
362 for (device_find_first_child(bus, &dev), child_count = 0;
364 device_find_next_child(&dev)) {
365 /* Check that platform data is allocated */
366 plat = dev_get_parent_platdata(dev);
367 ut_assert(plat != NULL);
368 ut_asserteq(1, plat->count);
371 ut_asserteq(3, child_count);
373 /* Unbind all the children */
375 device_find_first_child(bus, &dev);
380 /* Now the child platdata should be removed and re-added */
382 for (device_find_first_child(bus, &dev), child_count = 0;
384 device_find_next_child(&dev)) {
385 /* Check that platform data is allocated */
386 plat = dev_get_parent_platdata(dev);
387 ut_assert(plat != NULL);
388 ut_asserteq(0, plat->count);
391 ut_asserteq(3, child_count);
396 /* Test that the bus can store platform data about each child */
397 static int dm_test_bus_parent_platdata(struct dm_test_state *dms)
399 return test_bus_parent_platdata(dms);
401 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
403 /* As above but the size is controlled by the uclass */
404 static int dm_test_bus_parent_platdata_uclass(struct dm_test_state *dms)
410 /* Set the driver size to 0 so that the uclass size is used */
411 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
412 size = bus->driver->per_child_platdata_auto_alloc_size;
413 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
414 bus->driver->per_child_platdata_auto_alloc_size = 0;
415 ret = test_bus_parent_platdata(dms);
418 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
419 bus->driver->per_child_platdata_auto_alloc_size = size;
423 DM_TEST(dm_test_bus_parent_platdata_uclass,
424 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
426 /* Test that the child post_bind method is called */
427 static int dm_test_bus_child_post_bind(struct dm_test_state *dms)
429 struct dm_test_parent_platdata *plat;
430 struct udevice *bus, *dev;
433 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
434 for (device_find_first_child(bus, &dev), child_count = 0;
436 device_find_next_child(&dev)) {
437 /* Check that platform data is allocated */
438 plat = dev_get_parent_platdata(dev);
439 ut_assert(plat != NULL);
440 ut_asserteq(1, plat->bind_flag);
443 ut_asserteq(3, child_count);
447 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
449 /* Test that the child post_bind method is called */
450 static int dm_test_bus_child_post_bind_uclass(struct dm_test_state *dms)
452 struct dm_test_parent_platdata *plat;
453 struct udevice *bus, *dev;
456 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
457 for (device_find_first_child(bus, &dev), child_count = 0;
459 device_find_next_child(&dev)) {
460 /* Check that platform data is allocated */
461 plat = dev_get_parent_platdata(dev);
462 ut_assert(plat != NULL);
463 ut_asserteq(2, plat->uclass_bind_flag);
466 ut_asserteq(3, child_count);
470 DM_TEST(dm_test_bus_child_post_bind_uclass,
471 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);