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,
91 .flags = DM_UC_FLAG_SEQ_ALIAS,
94 /* Test that we can probe for children */
95 static int dm_test_bus_children(struct dm_test_state *dms)
101 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
102 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
104 /* Probe the bus, which should yield 3 more devices */
105 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
108 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
109 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
111 ut_assert(!dm_check_devices(dms, num_devices));
115 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
117 /* Test our functions for accessing children */
118 static int dm_test_bus_children_funcs(struct dm_test_state *dms)
120 const void *blob = gd->fdt_blob;
121 struct udevice *bus, *dev;
124 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
126 /* device_get_child() */
127 ut_assertok(device_get_child(bus, 0, &dev));
128 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
129 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
130 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
131 ut_asserteq_str("c-test@5", dev->name);
133 /* Device with sequence number 0 should be accessible */
134 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
135 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
136 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
137 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
138 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
139 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
141 /* There is no device with sequence number 2 */
142 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
143 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
144 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
146 /* Looking for something that is not a child */
147 node = fdt_path_offset(blob, "/junk");
148 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
149 node = fdt_path_offset(blob, "/d-test");
150 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
152 /* Find a valid child */
153 node = fdt_path_offset(blob, "/some-bus/c-test@1");
154 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
155 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
156 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
157 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
161 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
163 /* Test that we can iterate through children */
164 static int dm_test_bus_children_iterators(struct dm_test_state *dms)
166 struct udevice *bus, *dev, *child;
168 /* Walk through the children one by one */
169 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
170 ut_assertok(device_find_first_child(bus, &dev));
171 ut_asserteq_str("c-test@5", dev->name);
172 ut_assertok(device_find_next_child(&dev));
173 ut_asserteq_str("c-test@0", dev->name);
174 ut_assertok(device_find_next_child(&dev));
175 ut_asserteq_str("c-test@1", dev->name);
176 ut_assertok(device_find_next_child(&dev));
177 ut_asserteq_ptr(dev, NULL);
179 /* Move to the next child without using device_find_first_child() */
180 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
181 ut_asserteq_str("c-test@5", dev->name);
182 ut_assertok(device_find_next_child(&dev));
183 ut_asserteq_str("c-test@0", dev->name);
185 /* Try a device with no children */
186 ut_assertok(device_find_first_child(dev, &child));
187 ut_asserteq_ptr(child, NULL);
191 DM_TEST(dm_test_bus_children_iterators,
192 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
194 /* Test that the bus can store data about each child */
195 static int dm_test_bus_parent_data(struct dm_test_state *dms)
197 struct dm_test_parent_data *parent_data;
198 struct udevice *bus, *dev;
202 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
204 /* Check that parent data is allocated */
205 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
206 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
207 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
208 parent_data = dev_get_parentdata(dev);
209 ut_assert(NULL != parent_data);
211 /* Check that it starts at 0 and goes away when device is removed */
212 parent_data->sum += 5;
213 ut_asserteq(5, parent_data->sum);
215 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
217 /* Check that we can do this twice */
218 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
219 parent_data = dev_get_parentdata(dev);
220 ut_assert(NULL != parent_data);
221 parent_data->sum += 5;
222 ut_asserteq(5, parent_data->sum);
224 /* Add parent data to all children */
225 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
227 uclass_foreach_dev(dev, uc) {
228 /* Ignore these if they are not on this bus */
229 if (dev->parent != bus) {
230 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
233 ut_assertok(device_probe(dev));
234 parent_data = dev_get_parentdata(dev);
236 parent_data->sum = value;
240 /* Check it is still there */
242 uclass_foreach_dev(dev, uc) {
243 /* Ignore these if they are not on this bus */
244 if (dev->parent != bus)
246 parent_data = dev_get_parentdata(dev);
248 ut_asserteq(value, parent_data->sum);
255 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
257 /* Test that the bus ops are called when a child is probed/removed */
258 static int dm_test_bus_parent_ops(struct dm_test_state *dms)
260 struct dm_test_parent_data *parent_data;
261 struct udevice *bus, *dev;
265 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
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)
272 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
274 ut_assertok(device_probe(dev));
275 parent_data = dev_get_parentdata(dev);
276 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
279 uclass_foreach_dev(dev, uc) {
280 /* Ignore these if they are not on this bus */
281 if (dev->parent != bus)
283 parent_data = dev_get_parentdata(dev);
284 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
285 ut_assertok(device_remove(dev));
286 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
287 ut_asserteq_ptr(dms->removed, dev);
293 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
295 static int test_bus_parent_platdata(struct dm_test_state *dms)
297 struct dm_test_parent_platdata *plat;
298 struct udevice *bus, *dev;
301 /* Check that the bus has no children */
302 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
303 device_find_first_child(bus, &dev);
304 ut_asserteq_ptr(NULL, dev);
306 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
308 for (device_find_first_child(bus, &dev), child_count = 0;
310 device_find_next_child(&dev)) {
311 /* Check that platform data is allocated */
312 plat = dev_get_parent_platdata(dev);
313 ut_assert(plat != NULL);
316 * Check that it is not affected by the device being
320 ut_asserteq(1, plat->count);
324 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
325 ut_asserteq(1, plat->count);
326 ut_assertok(device_probe(dev));
329 ut_asserteq(3, child_count);
331 /* Removing the bus should also have no effect (it is still bound) */
333 for (device_find_first_child(bus, &dev), child_count = 0;
335 device_find_next_child(&dev)) {
336 /* Check that platform data is allocated */
337 plat = dev_get_parent_platdata(dev);
338 ut_assert(plat != NULL);
339 ut_asserteq(1, plat->count);
342 ut_asserteq(3, child_count);
344 /* Unbind all the children */
346 device_find_first_child(bus, &dev);
351 /* Now the child platdata should be removed and re-added */
353 for (device_find_first_child(bus, &dev), child_count = 0;
355 device_find_next_child(&dev)) {
356 /* Check that platform data is allocated */
357 plat = dev_get_parent_platdata(dev);
358 ut_assert(plat != NULL);
359 ut_asserteq(0, plat->count);
362 ut_asserteq(3, child_count);
367 /* Test that the bus can store platform data about each child */
368 static int dm_test_bus_parent_platdata(struct dm_test_state *dms)
370 return test_bus_parent_platdata(dms);
372 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
374 /* As above but the size is controlled by the uclass */
375 static int dm_test_bus_parent_platdata_uclass(struct dm_test_state *dms)
381 /* Set the driver size to 0 so that the uclass size is used */
382 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
383 size = bus->driver->per_child_platdata_auto_alloc_size;
384 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
385 bus->driver->per_child_platdata_auto_alloc_size = 0;
386 ret = test_bus_parent_platdata(dms);
389 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
390 bus->driver->per_child_platdata_auto_alloc_size = size;
394 DM_TEST(dm_test_bus_parent_platdata_uclass,
395 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
397 /* Test that the child post_bind method is called */
398 static int dm_test_bus_child_post_bind(struct dm_test_state *dms)
400 struct dm_test_parent_platdata *plat;
401 struct udevice *bus, *dev;
404 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
405 for (device_find_first_child(bus, &dev), child_count = 0;
407 device_find_next_child(&dev)) {
408 /* Check that platform data is allocated */
409 plat = dev_get_parent_platdata(dev);
410 ut_assert(plat != NULL);
411 ut_asserteq(1, plat->bind_flag);
414 ut_asserteq(3, child_count);
418 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);