blob: e18a6f724999e8011993b3e4155a76344f5b971e [file] [log] [blame]
Simon Glass1ca7e202014-07-23 06:55:18 -06001/*
2 * Copyright (c) 2014 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
Simon Glasse59f4582014-07-23 06:55:20 -06009#include <dm/device-internal.h>
Simon Glass1ca7e202014-07-23 06:55:18 -060010#include <dm/root.h>
11#include <dm/test.h>
Simon Glasscdc133b2015-01-25 08:27:01 -070012#include <dm/uclass-internal.h>
Simon Glass1ca7e202014-07-23 06:55:18 -060013#include <dm/ut.h>
14#include <dm/util.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
Simon Glasscdc133b2015-01-25 08:27:01 -070018struct dm_test_parent_platdata {
19 int count;
Simon Glass0118ce72015-01-25 08:27:03 -070020 int bind_flag;
Simon Glasscdc133b2015-01-25 08:27:01 -070021};
22
Simon Glassa327dee2014-07-23 06:55:21 -060023enum {
24 FLAG_CHILD_PROBED = 10,
25 FLAG_CHILD_REMOVED = -7,
26};
27
28static struct dm_test_state *test_state;
29
Simon Glass1ca7e202014-07-23 06:55:18 -060030static int testbus_drv_probe(struct udevice *dev)
31{
32 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
33}
34
Simon Glass0118ce72015-01-25 08:27:03 -070035static int testbus_child_post_bind(struct udevice *dev)
36{
37 struct dm_test_parent_platdata *plat;
38
39 plat = dev_get_parent_platdata(dev);
40 plat->bind_flag = 1;
41
42 return 0;
43}
44
Simon Glassa327dee2014-07-23 06:55:21 -060045static int testbus_child_pre_probe(struct udevice *dev)
46{
47 struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
48
49 parent_data->flag += FLAG_CHILD_PROBED;
50
51 return 0;
52}
53
54static int testbus_child_post_remove(struct udevice *dev)
55{
56 struct dm_test_parent_data *parent_data = dev_get_parentdata(dev);
57 struct dm_test_state *dms = test_state;
58
59 parent_data->flag += FLAG_CHILD_REMOVED;
60 if (dms)
61 dms->removed = dev;
62
63 return 0;
64}
65
Simon Glass1ca7e202014-07-23 06:55:18 -060066static const struct udevice_id testbus_ids[] = {
67 {
68 .compatible = "denx,u-boot-test-bus",
69 .data = DM_TEST_TYPE_FIRST },
70 { }
71};
72
73U_BOOT_DRIVER(testbus_drv) = {
74 .name = "testbus_drv",
75 .of_match = testbus_ids,
76 .id = UCLASS_TEST_BUS,
77 .probe = testbus_drv_probe,
Simon Glass0118ce72015-01-25 08:27:03 -070078 .child_post_bind = testbus_child_post_bind,
Simon Glass1ca7e202014-07-23 06:55:18 -060079 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
80 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
Simon Glasse59f4582014-07-23 06:55:20 -060081 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
Simon Glasscdc133b2015-01-25 08:27:01 -070082 .per_child_platdata_auto_alloc_size =
83 sizeof(struct dm_test_parent_platdata),
Simon Glassa327dee2014-07-23 06:55:21 -060084 .child_pre_probe = testbus_child_pre_probe,
85 .child_post_remove = testbus_child_post_remove,
Simon Glass1ca7e202014-07-23 06:55:18 -060086};
87
88UCLASS_DRIVER(testbus) = {
89 .name = "testbus",
90 .id = UCLASS_TEST_BUS,
91};
92
93/* Test that we can probe for children */
94static int dm_test_bus_children(struct dm_test_state *dms)
95{
96 int num_devices = 4;
97 struct udevice *bus;
98 struct uclass *uc;
99
100 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
101 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
102
103 /* Probe the bus, which should yield 3 more devices */
104 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
105 num_devices += 3;
106
107 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
108 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
109
110 ut_assert(!dm_check_devices(dms, num_devices));
111
112 return 0;
113}
114DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass997c87b2014-07-23 06:55:19 -0600115
116/* Test our functions for accessing children */
117static int dm_test_bus_children_funcs(struct dm_test_state *dms)
118{
119 const void *blob = gd->fdt_blob;
120 struct udevice *bus, *dev;
121 int node;
122
123 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
124
125 /* device_get_child() */
126 ut_assertok(device_get_child(bus, 0, &dev));
127 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
128 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
129 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
130 ut_asserteq_str("c-test@5", dev->name);
131
132 /* Device with sequence number 0 should be accessible */
133 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
134 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
135 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
136 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
137 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
138 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
139
140 /* There is no device with sequence number 2 */
141 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
142 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
143 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
144
145 /* Looking for something that is not a child */
146 node = fdt_path_offset(blob, "/junk");
147 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
148 node = fdt_path_offset(blob, "/d-test");
149 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
150
151 /* Find a valid child */
152 node = fdt_path_offset(blob, "/some-bus/c-test@1");
153 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
154 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
155 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
156 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
157
158 return 0;
159}
160DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glasse59f4582014-07-23 06:55:20 -0600161
Simon Glassa8981d42014-10-13 23:41:49 -0600162/* Test that we can iterate through children */
163static int dm_test_bus_children_iterators(struct dm_test_state *dms)
164{
165 struct udevice *bus, *dev, *child;
166
167 /* Walk through the children one by one */
168 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
169 ut_assertok(device_find_first_child(bus, &dev));
170 ut_asserteq_str("c-test@5", dev->name);
171 ut_assertok(device_find_next_child(&dev));
172 ut_asserteq_str("c-test@0", dev->name);
173 ut_assertok(device_find_next_child(&dev));
174 ut_asserteq_str("c-test@1", dev->name);
175 ut_assertok(device_find_next_child(&dev));
176 ut_asserteq_ptr(dev, NULL);
177
178 /* Move to the next child without using device_find_first_child() */
179 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
180 ut_asserteq_str("c-test@5", dev->name);
181 ut_assertok(device_find_next_child(&dev));
182 ut_asserteq_str("c-test@0", dev->name);
183
184 /* Try a device with no children */
185 ut_assertok(device_find_first_child(dev, &child));
186 ut_asserteq_ptr(child, NULL);
187
188 return 0;
189}
190DM_TEST(dm_test_bus_children_iterators,
191 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
192
Simon Glasse59f4582014-07-23 06:55:20 -0600193/* Test that the bus can store data about each child */
194static int dm_test_bus_parent_data(struct dm_test_state *dms)
195{
196 struct dm_test_parent_data *parent_data;
197 struct udevice *bus, *dev;
198 struct uclass *uc;
199 int value;
200
201 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
202
203 /* Check that parent data is allocated */
204 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
205 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
206 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
207 parent_data = dev_get_parentdata(dev);
208 ut_assert(NULL != parent_data);
209
210 /* Check that it starts at 0 and goes away when device is removed */
211 parent_data->sum += 5;
212 ut_asserteq(5, parent_data->sum);
213 device_remove(dev);
214 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
215
216 /* Check that we can do this twice */
217 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
218 parent_data = dev_get_parentdata(dev);
219 ut_assert(NULL != parent_data);
220 parent_data->sum += 5;
221 ut_asserteq(5, parent_data->sum);
222
223 /* Add parent data to all children */
224 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
225 value = 5;
226 uclass_foreach_dev(dev, uc) {
227 /* Ignore these if they are not on this bus */
228 if (dev->parent != bus) {
229 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
230 continue;
231 }
232 ut_assertok(device_probe(dev));
233 parent_data = dev_get_parentdata(dev);
234
235 parent_data->sum = value;
236 value += 5;
237 }
238
239 /* Check it is still there */
240 value = 5;
241 uclass_foreach_dev(dev, uc) {
242 /* Ignore these if they are not on this bus */
243 if (dev->parent != bus)
244 continue;
245 parent_data = dev_get_parentdata(dev);
246
247 ut_asserteq(value, parent_data->sum);
248 value += 5;
249 }
250
251 return 0;
252}
253
254DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassa327dee2014-07-23 06:55:21 -0600255
256/* Test that the bus ops are called when a child is probed/removed */
257static int dm_test_bus_parent_ops(struct dm_test_state *dms)
258{
259 struct dm_test_parent_data *parent_data;
260 struct udevice *bus, *dev;
261 struct uclass *uc;
262
263 test_state = dms;
264 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
265 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
266
267 uclass_foreach_dev(dev, uc) {
268 /* Ignore these if they are not on this bus */
269 if (dev->parent != bus)
270 continue;
271 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
272
273 ut_assertok(device_probe(dev));
274 parent_data = dev_get_parentdata(dev);
275 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
276 }
277
278 uclass_foreach_dev(dev, uc) {
279 /* Ignore these if they are not on this bus */
280 if (dev->parent != bus)
281 continue;
282 parent_data = dev_get_parentdata(dev);
283 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
284 ut_assertok(device_remove(dev));
285 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
286 ut_asserteq_ptr(dms->removed, dev);
287 }
288 test_state = NULL;
289
290 return 0;
291}
292DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glasscdc133b2015-01-25 08:27:01 -0700293
Simon Glassba8da9d2015-01-25 08:27:02 -0700294static int test_bus_parent_platdata(struct dm_test_state *dms)
Simon Glasscdc133b2015-01-25 08:27:01 -0700295{
296 struct dm_test_parent_platdata *plat;
297 struct udevice *bus, *dev;
298 int child_count;
299
300 /* Check that the bus has no children */
301 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
302 device_find_first_child(bus, &dev);
303 ut_asserteq_ptr(NULL, dev);
304
305 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
306
307 for (device_find_first_child(bus, &dev), child_count = 0;
308 dev;
309 device_find_next_child(&dev)) {
310 /* Check that platform data is allocated */
311 plat = dev_get_parent_platdata(dev);
312 ut_assert(plat != NULL);
313
314 /*
315 * Check that it is not affected by the device being
316 * probed/removed
317 */
318 plat->count++;
319 ut_asserteq(1, plat->count);
320 device_probe(dev);
321 device_remove(dev);
322
323 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
324 ut_asserteq(1, plat->count);
325 ut_assertok(device_probe(dev));
326 child_count++;
327 }
328 ut_asserteq(3, child_count);
329
330 /* Removing the bus should also have no effect (it is still bound) */
331 device_remove(bus);
332 for (device_find_first_child(bus, &dev), child_count = 0;
333 dev;
334 device_find_next_child(&dev)) {
335 /* Check that platform data is allocated */
336 plat = dev_get_parent_platdata(dev);
337 ut_assert(plat != NULL);
338 ut_asserteq(1, plat->count);
339 child_count++;
340 }
341 ut_asserteq(3, child_count);
342
343 /* Unbind all the children */
344 do {
345 device_find_first_child(bus, &dev);
346 if (dev)
347 device_unbind(dev);
348 } while (dev);
349
350 /* Now the child platdata should be removed and re-added */
351 device_probe(bus);
352 for (device_find_first_child(bus, &dev), child_count = 0;
353 dev;
354 device_find_next_child(&dev)) {
355 /* Check that platform data is allocated */
356 plat = dev_get_parent_platdata(dev);
357 ut_assert(plat != NULL);
358 ut_asserteq(0, plat->count);
359 child_count++;
360 }
361 ut_asserteq(3, child_count);
362
363 return 0;
364}
Simon Glassba8da9d2015-01-25 08:27:02 -0700365
366/* Test that the bus can store platform data about each child */
367static int dm_test_bus_parent_platdata(struct dm_test_state *dms)
368{
369 return test_bus_parent_platdata(dms);
370}
Simon Glasscdc133b2015-01-25 08:27:01 -0700371DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassba8da9d2015-01-25 08:27:02 -0700372
373/* As above but the size is controlled by the uclass */
374static int dm_test_bus_parent_platdata_uclass(struct dm_test_state *dms)
375{
376 struct udevice *bus;
377 int size;
378 int ret;
379
380 /* Set the driver size to 0 so that the uclass size is used */
381 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
382 size = bus->driver->per_child_platdata_auto_alloc_size;
383 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
384 bus->driver->per_child_platdata_auto_alloc_size = 0;
385 ret = test_bus_parent_platdata(dms);
386 if (ret)
387 return ret;
388 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
389 bus->driver->per_child_platdata_auto_alloc_size = size;
390
391 return 0;
392}
393DM_TEST(dm_test_bus_parent_platdata_uclass,
394 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass0118ce72015-01-25 08:27:03 -0700395
396/* Test that the child post_bind method is called */
397static int dm_test_bus_child_post_bind(struct dm_test_state *dms)
398{
399 struct dm_test_parent_platdata *plat;
400 struct udevice *bus, *dev;
401 int child_count;
402
403 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
404 for (device_find_first_child(bus, &dev), child_count = 0;
405 dev;
406 device_find_next_child(&dev)) {
407 /* Check that platform data is allocated */
408 plat = dev_get_parent_platdata(dev);
409 ut_assert(plat != NULL);
410 ut_asserteq(1, plat->bind_flag);
411 child_count++;
412 }
413 ut_asserteq(3, child_count);
414
415 return 0;
416}
417DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);