2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <dm/device-internal.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 static int testbus_drv_probe(struct udevice *dev)
19 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
22 static const struct udevice_id testbus_ids[] = {
24 .compatible = "denx,u-boot-test-bus",
25 .data = DM_TEST_TYPE_FIRST },
29 U_BOOT_DRIVER(testbus_drv) = {
30 .name = "testbus_drv",
31 .of_match = testbus_ids,
32 .id = UCLASS_TEST_BUS,
33 .probe = testbus_drv_probe,
34 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
35 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
36 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
39 UCLASS_DRIVER(testbus) = {
41 .id = UCLASS_TEST_BUS,
44 /* Test that we can probe for children */
45 static int dm_test_bus_children(struct dm_test_state *dms)
51 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
52 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
54 /* Probe the bus, which should yield 3 more devices */
55 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
58 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
59 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
61 ut_assert(!dm_check_devices(dms, num_devices));
65 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
67 /* Test our functions for accessing children */
68 static int dm_test_bus_children_funcs(struct dm_test_state *dms)
70 const void *blob = gd->fdt_blob;
71 struct udevice *bus, *dev;
74 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
76 /* device_get_child() */
77 ut_assertok(device_get_child(bus, 0, &dev));
78 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
79 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
80 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
81 ut_asserteq_str("c-test@5", dev->name);
83 /* Device with sequence number 0 should be accessible */
84 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
85 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
86 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
87 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
88 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
89 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
91 /* There is no device with sequence number 2 */
92 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
93 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
94 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
96 /* Looking for something that is not a child */
97 node = fdt_path_offset(blob, "/junk");
98 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
99 node = fdt_path_offset(blob, "/d-test");
100 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
102 /* Find a valid child */
103 node = fdt_path_offset(blob, "/some-bus/c-test@1");
104 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
105 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
106 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
107 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
111 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
113 /* Test that the bus can store data about each child */
114 static int dm_test_bus_parent_data(struct dm_test_state *dms)
116 struct dm_test_parent_data *parent_data;
117 struct udevice *bus, *dev;
121 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
123 /* Check that parent data is allocated */
124 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
125 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
126 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
127 parent_data = dev_get_parentdata(dev);
128 ut_assert(NULL != parent_data);
130 /* Check that it starts at 0 and goes away when device is removed */
131 parent_data->sum += 5;
132 ut_asserteq(5, parent_data->sum);
134 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
136 /* Check that we can do this twice */
137 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
138 parent_data = dev_get_parentdata(dev);
139 ut_assert(NULL != parent_data);
140 parent_data->sum += 5;
141 ut_asserteq(5, parent_data->sum);
143 /* Add parent data to all children */
144 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
146 uclass_foreach_dev(dev, uc) {
147 /* Ignore these if they are not on this bus */
148 if (dev->parent != bus) {
149 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
152 ut_assertok(device_probe(dev));
153 parent_data = dev_get_parentdata(dev);
155 parent_data->sum = value;
159 /* Check it is still there */
161 uclass_foreach_dev(dev, uc) {
162 /* Ignore these if they are not on this bus */
163 if (dev->parent != bus)
165 parent_data = dev_get_parentdata(dev);
167 ut_asserteq(value, parent_data->sum);
174 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);