2 * Copyright (c) 2014 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <dm/device-internal.h>
15 DECLARE_GLOBAL_DATA_PTR
;
18 FLAG_CHILD_PROBED
= 10,
19 FLAG_CHILD_REMOVED
= -7,
22 static struct dm_test_state
*test_state
;
24 static int testbus_drv_probe(struct udevice
*dev
)
26 return dm_scan_fdt_node(dev
, gd
->fdt_blob
, dev
->of_offset
, false);
29 static int testbus_child_pre_probe(struct udevice
*dev
)
31 struct dm_test_parent_data
*parent_data
= dev_get_parentdata(dev
);
33 parent_data
->flag
+= FLAG_CHILD_PROBED
;
38 static int testbus_child_post_remove(struct udevice
*dev
)
40 struct dm_test_parent_data
*parent_data
= dev_get_parentdata(dev
);
41 struct dm_test_state
*dms
= test_state
;
43 parent_data
->flag
+= FLAG_CHILD_REMOVED
;
50 static const struct udevice_id testbus_ids
[] = {
52 .compatible
= "denx,u-boot-test-bus",
53 .data
= DM_TEST_TYPE_FIRST
},
57 U_BOOT_DRIVER(testbus_drv
) = {
58 .name
= "testbus_drv",
59 .of_match
= testbus_ids
,
60 .id
= UCLASS_TEST_BUS
,
61 .probe
= testbus_drv_probe
,
62 .priv_auto_alloc_size
= sizeof(struct dm_test_priv
),
63 .platdata_auto_alloc_size
= sizeof(struct dm_test_pdata
),
64 .per_child_auto_alloc_size
= sizeof(struct dm_test_parent_data
),
65 .child_pre_probe
= testbus_child_pre_probe
,
66 .child_post_remove
= testbus_child_post_remove
,
69 UCLASS_DRIVER(testbus
) = {
71 .id
= UCLASS_TEST_BUS
,
74 /* Test that we can probe for children */
75 static int dm_test_bus_children(struct dm_test_state
*dms
)
81 ut_assertok(uclass_get(UCLASS_TEST_FDT
, &uc
));
82 ut_asserteq(num_devices
, list_count_items(&uc
->dev_head
));
84 /* Probe the bus, which should yield 3 more devices */
85 ut_assertok(uclass_get_device(UCLASS_TEST_BUS
, 0, &bus
));
88 ut_assertok(uclass_get(UCLASS_TEST_FDT
, &uc
));
89 ut_asserteq(num_devices
, list_count_items(&uc
->dev_head
));
91 ut_assert(!dm_check_devices(dms
, num_devices
));
95 DM_TEST(dm_test_bus_children
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
97 /* Test our functions for accessing children */
98 static int dm_test_bus_children_funcs(struct dm_test_state
*dms
)
100 const void *blob
= gd
->fdt_blob
;
101 struct udevice
*bus
, *dev
;
104 ut_assertok(uclass_get_device(UCLASS_TEST_BUS
, 0, &bus
));
106 /* device_get_child() */
107 ut_assertok(device_get_child(bus
, 0, &dev
));
108 ut_asserteq(-ENODEV
, device_get_child(bus
, 4, &dev
));
109 ut_assertok(device_get_child_by_seq(bus
, 5, &dev
));
110 ut_assert(dev
->flags
& DM_FLAG_ACTIVATED
);
111 ut_asserteq_str("c-test@5", dev
->name
);
113 /* Device with sequence number 0 should be accessible */
114 ut_asserteq(-ENODEV
, device_find_child_by_seq(bus
, -1, true, &dev
));
115 ut_assertok(device_find_child_by_seq(bus
, 0, true, &dev
));
116 ut_assert(!(dev
->flags
& DM_FLAG_ACTIVATED
));
117 ut_asserteq(-ENODEV
, device_find_child_by_seq(bus
, 0, false, &dev
));
118 ut_assertok(device_get_child_by_seq(bus
, 0, &dev
));
119 ut_assert(dev
->flags
& DM_FLAG_ACTIVATED
);
121 /* There is no device with sequence number 2 */
122 ut_asserteq(-ENODEV
, device_find_child_by_seq(bus
, 2, false, &dev
));
123 ut_asserteq(-ENODEV
, device_find_child_by_seq(bus
, 2, true, &dev
));
124 ut_asserteq(-ENODEV
, device_get_child_by_seq(bus
, 2, &dev
));
126 /* Looking for something that is not a child */
127 node
= fdt_path_offset(blob
, "/junk");
128 ut_asserteq(-ENODEV
, device_find_child_by_of_offset(bus
, node
, &dev
));
129 node
= fdt_path_offset(blob
, "/d-test");
130 ut_asserteq(-ENODEV
, device_find_child_by_of_offset(bus
, node
, &dev
));
132 /* Find a valid child */
133 node
= fdt_path_offset(blob
, "/some-bus/c-test@1");
134 ut_assertok(device_find_child_by_of_offset(bus
, node
, &dev
));
135 ut_assert(!(dev
->flags
& DM_FLAG_ACTIVATED
));
136 ut_assertok(device_get_child_by_of_offset(bus
, node
, &dev
));
137 ut_assert(dev
->flags
& DM_FLAG_ACTIVATED
);
141 DM_TEST(dm_test_bus_children_funcs
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
143 /* Test that we can iterate through children */
144 static int dm_test_bus_children_iterators(struct dm_test_state
*dms
)
146 struct udevice
*bus
, *dev
, *child
;
148 /* Walk through the children one by one */
149 ut_assertok(uclass_get_device(UCLASS_TEST_BUS
, 0, &bus
));
150 ut_assertok(device_find_first_child(bus
, &dev
));
151 ut_asserteq_str("c-test@5", dev
->name
);
152 ut_assertok(device_find_next_child(&dev
));
153 ut_asserteq_str("c-test@0", dev
->name
);
154 ut_assertok(device_find_next_child(&dev
));
155 ut_asserteq_str("c-test@1", dev
->name
);
156 ut_assertok(device_find_next_child(&dev
));
157 ut_asserteq_ptr(dev
, NULL
);
159 /* Move to the next child without using device_find_first_child() */
160 ut_assertok(device_find_child_by_seq(bus
, 5, true, &dev
));
161 ut_asserteq_str("c-test@5", dev
->name
);
162 ut_assertok(device_find_next_child(&dev
));
163 ut_asserteq_str("c-test@0", dev
->name
);
165 /* Try a device with no children */
166 ut_assertok(device_find_first_child(dev
, &child
));
167 ut_asserteq_ptr(child
, NULL
);
171 DM_TEST(dm_test_bus_children_iterators
,
172 DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
174 /* Test that the bus can store data about each child */
175 static int dm_test_bus_parent_data(struct dm_test_state
*dms
)
177 struct dm_test_parent_data
*parent_data
;
178 struct udevice
*bus
, *dev
;
182 ut_assertok(uclass_get_device(UCLASS_TEST_BUS
, 0, &bus
));
184 /* Check that parent data is allocated */
185 ut_assertok(device_find_child_by_seq(bus
, 0, true, &dev
));
186 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
187 ut_assertok(device_get_child_by_seq(bus
, 0, &dev
));
188 parent_data
= dev_get_parentdata(dev
);
189 ut_assert(NULL
!= parent_data
);
191 /* Check that it starts at 0 and goes away when device is removed */
192 parent_data
->sum
+= 5;
193 ut_asserteq(5, parent_data
->sum
);
195 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
197 /* Check that we can do this twice */
198 ut_assertok(device_get_child_by_seq(bus
, 0, &dev
));
199 parent_data
= dev_get_parentdata(dev
);
200 ut_assert(NULL
!= parent_data
);
201 parent_data
->sum
+= 5;
202 ut_asserteq(5, parent_data
->sum
);
204 /* Add parent data to all children */
205 ut_assertok(uclass_get(UCLASS_TEST_FDT
, &uc
));
207 uclass_foreach_dev(dev
, uc
) {
208 /* Ignore these if they are not on this bus */
209 if (dev
->parent
!= bus
) {
210 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
213 ut_assertok(device_probe(dev
));
214 parent_data
= dev_get_parentdata(dev
);
216 parent_data
->sum
= value
;
220 /* Check it is still there */
222 uclass_foreach_dev(dev
, uc
) {
223 /* Ignore these if they are not on this bus */
224 if (dev
->parent
!= bus
)
226 parent_data
= dev_get_parentdata(dev
);
228 ut_asserteq(value
, parent_data
->sum
);
235 DM_TEST(dm_test_bus_parent_data
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
237 /* Test that the bus ops are called when a child is probed/removed */
238 static int dm_test_bus_parent_ops(struct dm_test_state
*dms
)
240 struct dm_test_parent_data
*parent_data
;
241 struct udevice
*bus
, *dev
;
245 ut_assertok(uclass_get_device(UCLASS_TEST_BUS
, 0, &bus
));
246 ut_assertok(uclass_get(UCLASS_TEST_FDT
, &uc
));
248 uclass_foreach_dev(dev
, uc
) {
249 /* Ignore these if they are not on this bus */
250 if (dev
->parent
!= bus
)
252 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
254 ut_assertok(device_probe(dev
));
255 parent_data
= dev_get_parentdata(dev
);
256 ut_asserteq(FLAG_CHILD_PROBED
, parent_data
->flag
);
259 uclass_foreach_dev(dev
, uc
) {
260 /* Ignore these if they are not on this bus */
261 if (dev
->parent
!= bus
)
263 parent_data
= dev_get_parentdata(dev
);
264 ut_asserteq(FLAG_CHILD_PROBED
, parent_data
->flag
);
265 ut_assertok(device_remove(dev
));
266 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
267 ut_asserteq_ptr(dms
->removed
, dev
);
273 DM_TEST(dm_test_bus_parent_ops
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);