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 the bus can store data about each child */
144 static int dm_test_bus_parent_data(struct dm_test_state
*dms
)
146 struct dm_test_parent_data
*parent_data
;
147 struct udevice
*bus
, *dev
;
151 ut_assertok(uclass_get_device(UCLASS_TEST_BUS
, 0, &bus
));
153 /* Check that parent data is allocated */
154 ut_assertok(device_find_child_by_seq(bus
, 0, true, &dev
));
155 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
156 ut_assertok(device_get_child_by_seq(bus
, 0, &dev
));
157 parent_data
= dev_get_parentdata(dev
);
158 ut_assert(NULL
!= parent_data
);
160 /* Check that it starts at 0 and goes away when device is removed */
161 parent_data
->sum
+= 5;
162 ut_asserteq(5, parent_data
->sum
);
164 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
166 /* Check that we can do this twice */
167 ut_assertok(device_get_child_by_seq(bus
, 0, &dev
));
168 parent_data
= dev_get_parentdata(dev
);
169 ut_assert(NULL
!= parent_data
);
170 parent_data
->sum
+= 5;
171 ut_asserteq(5, parent_data
->sum
);
173 /* Add parent data to all children */
174 ut_assertok(uclass_get(UCLASS_TEST_FDT
, &uc
));
176 uclass_foreach_dev(dev
, uc
) {
177 /* Ignore these if they are not on this bus */
178 if (dev
->parent
!= bus
) {
179 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
182 ut_assertok(device_probe(dev
));
183 parent_data
= dev_get_parentdata(dev
);
185 parent_data
->sum
= value
;
189 /* Check it is still there */
191 uclass_foreach_dev(dev
, uc
) {
192 /* Ignore these if they are not on this bus */
193 if (dev
->parent
!= bus
)
195 parent_data
= dev_get_parentdata(dev
);
197 ut_asserteq(value
, parent_data
->sum
);
204 DM_TEST(dm_test_bus_parent_data
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);
206 /* Test that the bus ops are called when a child is probed/removed */
207 static int dm_test_bus_parent_ops(struct dm_test_state
*dms
)
209 struct dm_test_parent_data
*parent_data
;
210 struct udevice
*bus
, *dev
;
214 ut_assertok(uclass_get_device(UCLASS_TEST_BUS
, 0, &bus
));
215 ut_assertok(uclass_get(UCLASS_TEST_FDT
, &uc
));
217 uclass_foreach_dev(dev
, uc
) {
218 /* Ignore these if they are not on this bus */
219 if (dev
->parent
!= bus
)
221 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
223 ut_assertok(device_probe(dev
));
224 parent_data
= dev_get_parentdata(dev
);
225 ut_asserteq(FLAG_CHILD_PROBED
, parent_data
->flag
);
228 uclass_foreach_dev(dev
, uc
) {
229 /* Ignore these if they are not on this bus */
230 if (dev
->parent
!= bus
)
232 parent_data
= dev_get_parentdata(dev
);
233 ut_asserteq(FLAG_CHILD_PROBED
, parent_data
->flag
);
234 ut_assertok(device_remove(dev
));
235 ut_asserteq_ptr(NULL
, dev_get_parentdata(dev
));
236 ut_asserteq_ptr(dms
->removed
, dev
);
242 DM_TEST(dm_test_bus_parent_ops
, DM_TESTF_SCAN_PDATA
| DM_TESTF_SCAN_FDT
);