]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - test/dm/bus.c
dm: core: Allow uclasses to specify private data for a device's children
[karo-tx-uboot.git] / test / dm / bus.c
1 /*
2  * Copyright (c) 2014 Google, Inc
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <dm/device-internal.h>
10 #include <dm/root.h>
11 #include <dm/test.h>
12 #include <dm/uclass-internal.h>
13 #include <dm/ut.h>
14 #include <dm/util.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 struct dm_test_parent_platdata {
19         int count;
20         int bind_flag;
21 };
22
23 enum {
24         FLAG_CHILD_PROBED       = 10,
25         FLAG_CHILD_REMOVED      = -7,
26 };
27
28 static struct dm_test_state *test_state;
29
30 static int testbus_drv_probe(struct udevice *dev)
31 {
32         return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
33 }
34
35 static 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
45 static 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
54 static 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
66 static const struct udevice_id testbus_ids[] = {
67         {
68                 .compatible = "denx,u-boot-test-bus",
69                 .data = DM_TEST_TYPE_FIRST },
70         { }
71 };
72
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,
86 };
87
88 UCLASS_DRIVER(testbus) = {
89         .name           = "testbus",
90         .id             = UCLASS_TEST_BUS,
91         .flags          = DM_UC_FLAG_SEQ_ALIAS,
92 };
93
94 /* Test that we can probe for children */
95 static int dm_test_bus_children(struct dm_test_state *dms)
96 {
97         int num_devices = 6;
98         struct udevice *bus;
99         struct uclass *uc;
100
101         ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
102         ut_asserteq(num_devices, list_count_items(&uc->dev_head));
103
104         /* Probe the bus, which should yield 3 more devices */
105         ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
106         num_devices += 3;
107
108         ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
109         ut_asserteq(num_devices, list_count_items(&uc->dev_head));
110
111         ut_assert(!dm_check_devices(dms, num_devices));
112
113         return 0;
114 }
115 DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
116
117 /* Test our functions for accessing children */
118 static int dm_test_bus_children_funcs(struct dm_test_state *dms)
119 {
120         const void *blob = gd->fdt_blob;
121         struct udevice *bus, *dev;
122         int node;
123
124         ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
125
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);
132
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);
140
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));
145
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));
151
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);
158
159         return 0;
160 }
161 DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
162
163 /* Test that we can iterate through children */
164 static int dm_test_bus_children_iterators(struct dm_test_state *dms)
165 {
166         struct udevice *bus, *dev, *child;
167
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);
178
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);
184
185         /* Try a device with no children */
186         ut_assertok(device_find_first_child(dev, &child));
187         ut_asserteq_ptr(child, NULL);
188
189         return 0;
190 }
191 DM_TEST(dm_test_bus_children_iterators,
192         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
193
194 /* Test that the bus can store data about each child */
195 static int test_bus_parent_data(struct dm_test_state *dms)
196 {
197         struct dm_test_parent_data *parent_data;
198         struct udevice *bus, *dev;
199         struct uclass *uc;
200         int value;
201
202         ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
203
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);
210
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);
214         device_remove(dev);
215         ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
216
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);
223
224         /* Add parent data to all children */
225         ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
226         value = 5;
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));
231                         continue;
232                 }
233                 ut_assertok(device_probe(dev));
234                 parent_data = dev_get_parentdata(dev);
235
236                 parent_data->sum = value;
237                 value += 5;
238         }
239
240         /* Check it is still there */
241         value = 5;
242         uclass_foreach_dev(dev, uc) {
243                 /* Ignore these if they are not on this bus */
244                 if (dev->parent != bus)
245                         continue;
246                 parent_data = dev_get_parentdata(dev);
247
248                 ut_asserteq(value, parent_data->sum);
249                 value += 5;
250         }
251
252         return 0;
253 }
254 /* Test that the bus can store data about each child */
255 static int dm_test_bus_parent_data(struct dm_test_state *dms)
256 {
257         return test_bus_parent_data(dms);
258 }
259 DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
260
261 /* As above but the size is controlled by the uclass */
262 static int dm_test_bus_parent_data_uclass(struct dm_test_state *dms)
263 {
264         struct udevice *bus;
265         int size;
266         int ret;
267
268         /* Set the driver size to 0 so that the uclass size is used */
269         ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
270         size = bus->driver->per_child_auto_alloc_size;
271         bus->uclass->uc_drv->per_child_auto_alloc_size = size;
272         bus->driver->per_child_auto_alloc_size = 0;
273         ret = test_bus_parent_data(dms);
274         if (ret)
275                 return ret;
276         bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
277         bus->driver->per_child_auto_alloc_size = size;
278
279         return 0;
280 }
281 DM_TEST(dm_test_bus_parent_data_uclass,
282         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
283
284 /* Test that the bus ops are called when a child is probed/removed */
285 static int dm_test_bus_parent_ops(struct dm_test_state *dms)
286 {
287         struct dm_test_parent_data *parent_data;
288         struct udevice *bus, *dev;
289         struct uclass *uc;
290
291         test_state = dms;
292         ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
293         ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
294
295         uclass_foreach_dev(dev, uc) {
296                 /* Ignore these if they are not on this bus */
297                 if (dev->parent != bus)
298                         continue;
299                 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
300
301                 ut_assertok(device_probe(dev));
302                 parent_data = dev_get_parentdata(dev);
303                 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
304         }
305
306         uclass_foreach_dev(dev, uc) {
307                 /* Ignore these if they are not on this bus */
308                 if (dev->parent != bus)
309                         continue;
310                 parent_data = dev_get_parentdata(dev);
311                 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
312                 ut_assertok(device_remove(dev));
313                 ut_asserteq_ptr(NULL, dev_get_parentdata(dev));
314                 ut_asserteq_ptr(dms->removed, dev);
315         }
316         test_state = NULL;
317
318         return 0;
319 }
320 DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
321
322 static int test_bus_parent_platdata(struct dm_test_state *dms)
323 {
324         struct dm_test_parent_platdata *plat;
325         struct udevice *bus, *dev;
326         int child_count;
327
328         /* Check that the bus has no children */
329         ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
330         device_find_first_child(bus, &dev);
331         ut_asserteq_ptr(NULL, dev);
332
333         ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
334
335         for (device_find_first_child(bus, &dev), child_count = 0;
336              dev;
337              device_find_next_child(&dev)) {
338                 /* Check that platform data is allocated */
339                 plat = dev_get_parent_platdata(dev);
340                 ut_assert(plat != NULL);
341
342                 /*
343                  * Check that it is not affected by the device being
344                  * probed/removed
345                  */
346                 plat->count++;
347                 ut_asserteq(1, plat->count);
348                 device_probe(dev);
349                 device_remove(dev);
350
351                 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
352                 ut_asserteq(1, plat->count);
353                 ut_assertok(device_probe(dev));
354                 child_count++;
355         }
356         ut_asserteq(3, child_count);
357
358         /* Removing the bus should also have no effect (it is still bound) */
359         device_remove(bus);
360         for (device_find_first_child(bus, &dev), child_count = 0;
361              dev;
362              device_find_next_child(&dev)) {
363                 /* Check that platform data is allocated */
364                 plat = dev_get_parent_platdata(dev);
365                 ut_assert(plat != NULL);
366                 ut_asserteq(1, plat->count);
367                 child_count++;
368         }
369         ut_asserteq(3, child_count);
370
371         /* Unbind all the children */
372         do {
373                 device_find_first_child(bus, &dev);
374                 if (dev)
375                         device_unbind(dev);
376         } while (dev);
377
378         /* Now the child platdata should be removed and re-added */
379         device_probe(bus);
380         for (device_find_first_child(bus, &dev), child_count = 0;
381              dev;
382              device_find_next_child(&dev)) {
383                 /* Check that platform data is allocated */
384                 plat = dev_get_parent_platdata(dev);
385                 ut_assert(plat != NULL);
386                 ut_asserteq(0, plat->count);
387                 child_count++;
388         }
389         ut_asserteq(3, child_count);
390
391         return 0;
392 }
393
394 /* Test that the bus can store platform data about each child */
395 static int dm_test_bus_parent_platdata(struct dm_test_state *dms)
396 {
397         return test_bus_parent_platdata(dms);
398 }
399 DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
400
401 /* As above but the size is controlled by the uclass */
402 static int dm_test_bus_parent_platdata_uclass(struct dm_test_state *dms)
403 {
404         struct udevice *bus;
405         int size;
406         int ret;
407
408         /* Set the driver size to 0 so that the uclass size is used */
409         ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
410         size = bus->driver->per_child_platdata_auto_alloc_size;
411         bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
412         bus->driver->per_child_platdata_auto_alloc_size = 0;
413         ret = test_bus_parent_platdata(dms);
414         if (ret)
415                 return ret;
416         bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
417         bus->driver->per_child_platdata_auto_alloc_size = size;
418
419         return 0;
420 }
421 DM_TEST(dm_test_bus_parent_platdata_uclass,
422         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
423
424 /* Test that the child post_bind method is called */
425 static int dm_test_bus_child_post_bind(struct dm_test_state *dms)
426 {
427         struct dm_test_parent_platdata *plat;
428         struct udevice *bus, *dev;
429         int child_count;
430
431         ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
432         for (device_find_first_child(bus, &dev), child_count = 0;
433              dev;
434              device_find_next_child(&dev)) {
435                 /* Check that platform data is allocated */
436                 plat = dev_get_parent_platdata(dev);
437                 ut_assert(plat != NULL);
438                 ut_asserteq(1, plat->bind_flag);
439                 child_count++;
440         }
441         ut_asserteq(3, child_count);
442
443         return 0;
444 }
445 DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);