blob: 93f3acd4308e97691a45e1594db2c7d7ea43afb8 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass40717422014-07-23 06:55:18 -06002/*
3 * Copyright (c) 2014 Google, Inc
Simon Glass40717422014-07-23 06:55:18 -06004 */
5
6#include <common.h>
Simon Glass4e766c22018-10-01 21:12:32 -06007#ifdef CONFIG_SANDBOX
8#include <os.h>
9#endif
Simon Glass40717422014-07-23 06:55:18 -060010#include <dm.h>
Simon Glass60d971b2014-07-23 06:55:20 -060011#include <dm/device-internal.h>
Simon Glass40717422014-07-23 06:55:18 -060012#include <dm/test.h>
Simon Glass11b61732015-01-25 08:27:01 -070013#include <dm/uclass-internal.h>
Simon Glass40717422014-07-23 06:55:18 -060014#include <dm/util.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050015#include <test/ut.h>
Simon Glass40717422014-07-23 06:55:18 -060016
17DECLARE_GLOBAL_DATA_PTR;
18
Simon Glass11b61732015-01-25 08:27:01 -070019struct dm_test_parent_platdata {
20 int count;
Simon Glassa4a51a02015-01-25 08:27:03 -070021 int bind_flag;
Simon Glassf4c9b3e2015-01-25 08:27:08 -070022 int uclass_bind_flag;
Simon Glass11b61732015-01-25 08:27:01 -070023};
24
Simon Glassd45560d2014-07-23 06:55:21 -060025enum {
26 FLAG_CHILD_PROBED = 10,
27 FLAG_CHILD_REMOVED = -7,
28};
29
30static struct dm_test_state *test_state;
31
Simon Glass40717422014-07-23 06:55:18 -060032static int testbus_drv_probe(struct udevice *dev)
33{
Simon Glass09128c52016-07-05 17:10:09 -060034 return dm_scan_fdt_dev(dev);
Simon Glass40717422014-07-23 06:55:18 -060035}
36
Simon Glassa4a51a02015-01-25 08:27:03 -070037static int testbus_child_post_bind(struct udevice *dev)
38{
39 struct dm_test_parent_platdata *plat;
40
41 plat = dev_get_parent_platdata(dev);
42 plat->bind_flag = 1;
Simon Glassf4c9b3e2015-01-25 08:27:08 -070043 plat->uclass_bind_flag = 2;
Simon Glassa4a51a02015-01-25 08:27:03 -070044
45 return 0;
46}
47
Simon Glassd45560d2014-07-23 06:55:21 -060048static int testbus_child_pre_probe(struct udevice *dev)
49{
Simon Glassde44acf2015-09-28 23:32:01 -060050 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -060051
52 parent_data->flag += FLAG_CHILD_PROBED;
53
54 return 0;
55}
56
Simon Glass5104b982015-01-25 08:27:10 -070057static int testbus_child_pre_probe_uclass(struct udevice *dev)
58{
59 struct dm_test_priv *priv = dev_get_priv(dev);
60
61 priv->uclass_flag++;
62
63 return 0;
64}
65
Bin Mengd9bad172018-10-15 02:20:58 -070066static int testbus_child_post_probe_uclass(struct udevice *dev)
67{
68 struct dm_test_priv *priv = dev_get_priv(dev);
69
70 priv->uclass_postp++;
71
72 return 0;
73}
74
Simon Glassd45560d2014-07-23 06:55:21 -060075static int testbus_child_post_remove(struct udevice *dev)
76{
Simon Glassde44acf2015-09-28 23:32:01 -060077 struct dm_test_parent_data *parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -060078 struct dm_test_state *dms = test_state;
79
80 parent_data->flag += FLAG_CHILD_REMOVED;
81 if (dms)
82 dms->removed = dev;
83
84 return 0;
85}
86
Simon Glass40717422014-07-23 06:55:18 -060087static const struct udevice_id testbus_ids[] = {
88 {
89 .compatible = "denx,u-boot-test-bus",
90 .data = DM_TEST_TYPE_FIRST },
91 { }
92};
93
94U_BOOT_DRIVER(testbus_drv) = {
95 .name = "testbus_drv",
96 .of_match = testbus_ids,
97 .id = UCLASS_TEST_BUS,
98 .probe = testbus_drv_probe,
Simon Glassa4a51a02015-01-25 08:27:03 -070099 .child_post_bind = testbus_child_post_bind,
Simon Glass40717422014-07-23 06:55:18 -0600100 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
101 .platdata_auto_alloc_size = sizeof(struct dm_test_pdata),
Simon Glass60d971b2014-07-23 06:55:20 -0600102 .per_child_auto_alloc_size = sizeof(struct dm_test_parent_data),
Simon Glass11b61732015-01-25 08:27:01 -0700103 .per_child_platdata_auto_alloc_size =
104 sizeof(struct dm_test_parent_platdata),
Simon Glassd45560d2014-07-23 06:55:21 -0600105 .child_pre_probe = testbus_child_pre_probe,
106 .child_post_remove = testbus_child_post_remove,
Simon Glass40717422014-07-23 06:55:18 -0600107};
108
109UCLASS_DRIVER(testbus) = {
110 .name = "testbus",
111 .id = UCLASS_TEST_BUS,
Simon Glass0ccb0972015-01-25 08:27:05 -0700112 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass5104b982015-01-25 08:27:10 -0700113 .child_pre_probe = testbus_child_pre_probe_uclass,
Bin Mengd9bad172018-10-15 02:20:58 -0700114 .child_post_probe = testbus_child_post_probe_uclass,
Simon Glass40717422014-07-23 06:55:18 -0600115};
116
117/* Test that we can probe for children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500118static int dm_test_bus_children(struct unit_test_state *uts)
Simon Glass40717422014-07-23 06:55:18 -0600119{
Bin Mengd9d24782018-10-10 22:07:01 -0700120 int num_devices = 8;
Simon Glass40717422014-07-23 06:55:18 -0600121 struct udevice *bus;
122 struct uclass *uc;
123
124 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
125 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
126
127 /* Probe the bus, which should yield 3 more devices */
128 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
129 num_devices += 3;
130
131 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
132 ut_asserteq(num_devices, list_count_items(&uc->dev_head));
133
Joe Hershberger3a77be52015-05-20 14:27:27 -0500134 ut_assert(!dm_check_devices(uts, num_devices));
Simon Glass40717422014-07-23 06:55:18 -0600135
136 return 0;
137}
138DM_TEST(dm_test_bus_children, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass48d4e292014-07-23 06:55:19 -0600139
140/* Test our functions for accessing children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500141static int dm_test_bus_children_funcs(struct unit_test_state *uts)
Simon Glass48d4e292014-07-23 06:55:19 -0600142{
143 const void *blob = gd->fdt_blob;
144 struct udevice *bus, *dev;
145 int node;
146
147 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
148
149 /* device_get_child() */
150 ut_assertok(device_get_child(bus, 0, &dev));
151 ut_asserteq(-ENODEV, device_get_child(bus, 4, &dev));
152 ut_assertok(device_get_child_by_seq(bus, 5, &dev));
153 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
154 ut_asserteq_str("c-test@5", dev->name);
155
156 /* Device with sequence number 0 should be accessible */
157 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, -1, true, &dev));
158 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
159 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
160 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 0, false, &dev));
161 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
162 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
163
164 /* There is no device with sequence number 2 */
165 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, false, &dev));
166 ut_asserteq(-ENODEV, device_find_child_by_seq(bus, 2, true, &dev));
167 ut_asserteq(-ENODEV, device_get_child_by_seq(bus, 2, &dev));
168
169 /* Looking for something that is not a child */
170 node = fdt_path_offset(blob, "/junk");
171 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
172 node = fdt_path_offset(blob, "/d-test");
173 ut_asserteq(-ENODEV, device_find_child_by_of_offset(bus, node, &dev));
174
Simon Glass9b4057a2017-05-18 20:09:43 -0600175 return 0;
176}
177DM_TEST(dm_test_bus_children_funcs, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
178
179static int dm_test_bus_children_of_offset(struct unit_test_state *uts)
180{
181 const void *blob = gd->fdt_blob;
182 struct udevice *bus, *dev;
183 int node;
184
185 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glassac855522017-06-07 10:28:44 -0600186 ut_assertnonnull(bus);
Simon Glass9b4057a2017-05-18 20:09:43 -0600187
Simon Glass48d4e292014-07-23 06:55:19 -0600188 /* Find a valid child */
189 node = fdt_path_offset(blob, "/some-bus/c-test@1");
Simon Glass9b4057a2017-05-18 20:09:43 -0600190 ut_assert(node > 0);
Simon Glass48d4e292014-07-23 06:55:19 -0600191 ut_assertok(device_find_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -0600192 ut_assertnonnull(dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600193 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
194 ut_assertok(device_get_child_by_of_offset(bus, node, &dev));
Simon Glassac855522017-06-07 10:28:44 -0600195 ut_assertnonnull(dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600196 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
197
198 return 0;
199}
Simon Glass9b4057a2017-05-18 20:09:43 -0600200DM_TEST(dm_test_bus_children_of_offset,
201 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT | DM_TESTF_FLAT_TREE);
Simon Glass60d971b2014-07-23 06:55:20 -0600202
Simon Glass44da7352014-10-13 23:41:49 -0600203/* Test that we can iterate through children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500204static int dm_test_bus_children_iterators(struct unit_test_state *uts)
Simon Glass44da7352014-10-13 23:41:49 -0600205{
206 struct udevice *bus, *dev, *child;
207
208 /* Walk through the children one by one */
209 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
210 ut_assertok(device_find_first_child(bus, &dev));
211 ut_asserteq_str("c-test@5", dev->name);
212 ut_assertok(device_find_next_child(&dev));
213 ut_asserteq_str("c-test@0", dev->name);
214 ut_assertok(device_find_next_child(&dev));
215 ut_asserteq_str("c-test@1", dev->name);
216 ut_assertok(device_find_next_child(&dev));
217 ut_asserteq_ptr(dev, NULL);
218
219 /* Move to the next child without using device_find_first_child() */
220 ut_assertok(device_find_child_by_seq(bus, 5, true, &dev));
221 ut_asserteq_str("c-test@5", dev->name);
222 ut_assertok(device_find_next_child(&dev));
223 ut_asserteq_str("c-test@0", dev->name);
224
225 /* Try a device with no children */
226 ut_assertok(device_find_first_child(dev, &child));
227 ut_asserteq_ptr(child, NULL);
228
229 return 0;
230}
231DM_TEST(dm_test_bus_children_iterators,
232 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
233
Simon Glass60d971b2014-07-23 06:55:20 -0600234/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500235static int test_bus_parent_data(struct unit_test_state *uts)
Simon Glass60d971b2014-07-23 06:55:20 -0600236{
237 struct dm_test_parent_data *parent_data;
238 struct udevice *bus, *dev;
239 struct uclass *uc;
240 int value;
241
242 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
243
244 /* Check that parent data is allocated */
245 ut_assertok(device_find_child_by_seq(bus, 0, true, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600246 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600247 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600248 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600249 ut_assert(NULL != parent_data);
250
251 /* Check that it starts at 0 and goes away when device is removed */
252 parent_data->sum += 5;
253 ut_asserteq(5, parent_data->sum);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100254 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassde44acf2015-09-28 23:32:01 -0600255 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600256
257 /* Check that we can do this twice */
258 ut_assertok(device_get_child_by_seq(bus, 0, &dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600259 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600260 ut_assert(NULL != parent_data);
261 parent_data->sum += 5;
262 ut_asserteq(5, parent_data->sum);
263
264 /* Add parent data to all children */
265 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
266 value = 5;
267 uclass_foreach_dev(dev, uc) {
268 /* Ignore these if they are not on this bus */
269 if (dev->parent != bus) {
Simon Glassde44acf2015-09-28 23:32:01 -0600270 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glass60d971b2014-07-23 06:55:20 -0600271 continue;
272 }
273 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600274 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600275
276 parent_data->sum = value;
277 value += 5;
278 }
279
280 /* Check it is still there */
281 value = 5;
282 uclass_foreach_dev(dev, uc) {
283 /* Ignore these if they are not on this bus */
284 if (dev->parent != bus)
285 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600286 parent_data = dev_get_parent_priv(dev);
Simon Glass60d971b2014-07-23 06:55:20 -0600287
288 ut_asserteq(value, parent_data->sum);
289 value += 5;
290 }
291
292 return 0;
293}
Simon Glassc23b4282015-01-25 08:27:06 -0700294/* Test that the bus can store data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500295static int dm_test_bus_parent_data(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700296{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500297 return test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700298}
Simon Glass60d971b2014-07-23 06:55:20 -0600299DM_TEST(dm_test_bus_parent_data, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassd45560d2014-07-23 06:55:21 -0600300
Simon Glassc23b4282015-01-25 08:27:06 -0700301/* As above but the size is controlled by the uclass */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500302static int dm_test_bus_parent_data_uclass(struct unit_test_state *uts)
Simon Glassc23b4282015-01-25 08:27:06 -0700303{
Simon Glass2410d882015-03-25 12:21:51 -0600304 struct driver *drv;
Simon Glassc23b4282015-01-25 08:27:06 -0700305 struct udevice *bus;
306 int size;
307 int ret;
308
309 /* Set the driver size to 0 so that the uclass size is used */
310 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass2410d882015-03-25 12:21:51 -0600311 drv = (struct driver *)bus->driver;
312 size = drv->per_child_auto_alloc_size;
Simon Glass4e766c22018-10-01 21:12:32 -0600313
314#ifdef CONFIG_SANDBOX
315 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
316 os_mprotect_allow(drv, sizeof(*drv));
317#endif
Simon Glassc23b4282015-01-25 08:27:06 -0700318 bus->uclass->uc_drv->per_child_auto_alloc_size = size;
Simon Glass2410d882015-03-25 12:21:51 -0600319 drv->per_child_auto_alloc_size = 0;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500320 ret = test_bus_parent_data(uts);
Simon Glassc23b4282015-01-25 08:27:06 -0700321 if (ret)
322 return ret;
323 bus->uclass->uc_drv->per_child_auto_alloc_size = 0;
Simon Glass2410d882015-03-25 12:21:51 -0600324 drv->per_child_auto_alloc_size = size;
Simon Glassc23b4282015-01-25 08:27:06 -0700325
326 return 0;
327}
328DM_TEST(dm_test_bus_parent_data_uclass,
329 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
330
Simon Glassd45560d2014-07-23 06:55:21 -0600331/* Test that the bus ops are called when a child is probed/removed */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500332static int dm_test_bus_parent_ops(struct unit_test_state *uts)
Simon Glassd45560d2014-07-23 06:55:21 -0600333{
334 struct dm_test_parent_data *parent_data;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500335 struct dm_test_state *dms = uts->priv;
Simon Glassd45560d2014-07-23 06:55:21 -0600336 struct udevice *bus, *dev;
337 struct uclass *uc;
338
339 test_state = dms;
340 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
341 ut_assertok(uclass_get(UCLASS_TEST_FDT, &uc));
342
343 uclass_foreach_dev(dev, uc) {
344 /* Ignore these if they are not on this bus */
345 if (dev->parent != bus)
346 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600347 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassd45560d2014-07-23 06:55:21 -0600348
349 ut_assertok(device_probe(dev));
Simon Glassde44acf2015-09-28 23:32:01 -0600350 parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600351 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
352 }
353
354 uclass_foreach_dev(dev, uc) {
355 /* Ignore these if they are not on this bus */
356 if (dev->parent != bus)
357 continue;
Simon Glassde44acf2015-09-28 23:32:01 -0600358 parent_data = dev_get_parent_priv(dev);
Simon Glassd45560d2014-07-23 06:55:21 -0600359 ut_asserteq(FLAG_CHILD_PROBED, parent_data->flag);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100360 ut_assertok(device_remove(dev, DM_REMOVE_NORMAL));
Simon Glassde44acf2015-09-28 23:32:01 -0600361 ut_asserteq_ptr(NULL, dev_get_parent_priv(dev));
Simon Glassd45560d2014-07-23 06:55:21 -0600362 ut_asserteq_ptr(dms->removed, dev);
363 }
364 test_state = NULL;
365
366 return 0;
367}
368DM_TEST(dm_test_bus_parent_ops, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass11b61732015-01-25 08:27:01 -0700369
Joe Hershberger3a77be52015-05-20 14:27:27 -0500370static int test_bus_parent_platdata(struct unit_test_state *uts)
Simon Glass11b61732015-01-25 08:27:01 -0700371{
372 struct dm_test_parent_platdata *plat;
373 struct udevice *bus, *dev;
374 int child_count;
375
376 /* Check that the bus has no children */
377 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
378 device_find_first_child(bus, &dev);
379 ut_asserteq_ptr(NULL, dev);
380
381 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
382
383 for (device_find_first_child(bus, &dev), child_count = 0;
384 dev;
385 device_find_next_child(&dev)) {
386 /* Check that platform data is allocated */
387 plat = dev_get_parent_platdata(dev);
388 ut_assert(plat != NULL);
389
390 /*
391 * Check that it is not affected by the device being
392 * probed/removed
393 */
394 plat->count++;
395 ut_asserteq(1, plat->count);
396 device_probe(dev);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100397 device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass11b61732015-01-25 08:27:01 -0700398
399 ut_asserteq_ptr(plat, dev_get_parent_platdata(dev));
400 ut_asserteq(1, plat->count);
401 ut_assertok(device_probe(dev));
402 child_count++;
403 }
404 ut_asserteq(3, child_count);
405
406 /* Removing the bus should also have no effect (it is still bound) */
Stefan Roese80b5bc92017-03-20 12:51:48 +0100407 device_remove(bus, DM_REMOVE_NORMAL);
Simon Glass11b61732015-01-25 08:27:01 -0700408 for (device_find_first_child(bus, &dev), child_count = 0;
409 dev;
410 device_find_next_child(&dev)) {
411 /* Check that platform data is allocated */
412 plat = dev_get_parent_platdata(dev);
413 ut_assert(plat != NULL);
414 ut_asserteq(1, plat->count);
415 child_count++;
416 }
417 ut_asserteq(3, child_count);
418
419 /* Unbind all the children */
420 do {
421 device_find_first_child(bus, &dev);
422 if (dev)
423 device_unbind(dev);
424 } while (dev);
425
426 /* Now the child platdata should be removed and re-added */
427 device_probe(bus);
428 for (device_find_first_child(bus, &dev), child_count = 0;
429 dev;
430 device_find_next_child(&dev)) {
431 /* Check that platform data is allocated */
432 plat = dev_get_parent_platdata(dev);
433 ut_assert(plat != NULL);
434 ut_asserteq(0, plat->count);
435 child_count++;
436 }
437 ut_asserteq(3, child_count);
438
439 return 0;
440}
Simon Glass57f95402015-01-25 08:27:02 -0700441
442/* Test that the bus can store platform data about each child */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500443static int dm_test_bus_parent_platdata(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700444{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500445 return test_bus_parent_platdata(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700446}
Simon Glass11b61732015-01-25 08:27:01 -0700447DM_TEST(dm_test_bus_parent_platdata, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass57f95402015-01-25 08:27:02 -0700448
449/* As above but the size is controlled by the uclass */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500450static int dm_test_bus_parent_platdata_uclass(struct unit_test_state *uts)
Simon Glass57f95402015-01-25 08:27:02 -0700451{
452 struct udevice *bus;
Simon Glass2410d882015-03-25 12:21:51 -0600453 struct driver *drv;
Simon Glass57f95402015-01-25 08:27:02 -0700454 int size;
455 int ret;
456
457 /* Set the driver size to 0 so that the uclass size is used */
458 ut_assertok(uclass_find_device(UCLASS_TEST_BUS, 0, &bus));
Simon Glass2410d882015-03-25 12:21:51 -0600459 drv = (struct driver *)bus->driver;
460 size = drv->per_child_platdata_auto_alloc_size;
Simon Glass4e766c22018-10-01 21:12:32 -0600461#ifdef CONFIG_SANDBOX
462 os_mprotect_allow(bus->uclass->uc_drv, sizeof(*bus->uclass->uc_drv));
463 os_mprotect_allow(drv, sizeof(*drv));
464#endif
Simon Glass57f95402015-01-25 08:27:02 -0700465 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = size;
Simon Glass2410d882015-03-25 12:21:51 -0600466 drv->per_child_platdata_auto_alloc_size = 0;
Joe Hershberger3a77be52015-05-20 14:27:27 -0500467 ret = test_bus_parent_platdata(uts);
Simon Glass57f95402015-01-25 08:27:02 -0700468 if (ret)
469 return ret;
470 bus->uclass->uc_drv->per_child_platdata_auto_alloc_size = 0;
Simon Glass2410d882015-03-25 12:21:51 -0600471 drv->per_child_platdata_auto_alloc_size = size;
Simon Glass57f95402015-01-25 08:27:02 -0700472
473 return 0;
474}
475DM_TEST(dm_test_bus_parent_platdata_uclass,
476 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassa4a51a02015-01-25 08:27:03 -0700477
478/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500479static int dm_test_bus_child_post_bind(struct unit_test_state *uts)
Simon Glassa4a51a02015-01-25 08:27:03 -0700480{
481 struct dm_test_parent_platdata *plat;
482 struct udevice *bus, *dev;
483 int child_count;
484
485 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
486 for (device_find_first_child(bus, &dev), child_count = 0;
487 dev;
488 device_find_next_child(&dev)) {
489 /* Check that platform data is allocated */
490 plat = dev_get_parent_platdata(dev);
491 ut_assert(plat != NULL);
492 ut_asserteq(1, plat->bind_flag);
493 child_count++;
494 }
495 ut_asserteq(3, child_count);
496
497 return 0;
498}
499DM_TEST(dm_test_bus_child_post_bind, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700500
501/* Test that the child post_bind method is called */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500502static int dm_test_bus_child_post_bind_uclass(struct unit_test_state *uts)
Simon Glassf4c9b3e2015-01-25 08:27:08 -0700503{
504 struct dm_test_parent_platdata *plat;
505 struct udevice *bus, *dev;
506 int child_count;
507
508 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
509 for (device_find_first_child(bus, &dev), child_count = 0;
510 dev;
511 device_find_next_child(&dev)) {
512 /* Check that platform data is allocated */
513 plat = dev_get_parent_platdata(dev);
514 ut_assert(plat != NULL);
515 ut_asserteq(2, plat->uclass_bind_flag);
516 child_count++;
517 }
518 ut_asserteq(3, child_count);
519
520 return 0;
521}
522DM_TEST(dm_test_bus_child_post_bind_uclass,
523 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass5104b982015-01-25 08:27:10 -0700524
525/*
526 * Test that the bus' uclass' child_pre_probe() is called before the
527 * device's probe() method
528 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500529static int dm_test_bus_child_pre_probe_uclass(struct unit_test_state *uts)
Simon Glass5104b982015-01-25 08:27:10 -0700530{
531 struct udevice *bus, *dev;
532 int child_count;
533
534 /*
535 * See testfdt_drv_probe() which effectively checks that the uclass
536 * flag is set before that method is called
537 */
538 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
539 for (device_find_first_child(bus, &dev), child_count = 0;
540 dev;
541 device_find_next_child(&dev)) {
542 struct dm_test_priv *priv = dev_get_priv(dev);
543
544 /* Check that things happened in the right order */
545 ut_asserteq_ptr(NULL, priv);
546 ut_assertok(device_probe(dev));
547
548 priv = dev_get_priv(dev);
549 ut_assert(priv != NULL);
550 ut_asserteq(1, priv->uclass_flag);
551 ut_asserteq(1, priv->uclass_total);
552 child_count++;
553 }
554 ut_asserteq(3, child_count);
555
556 return 0;
557}
558DM_TEST(dm_test_bus_child_pre_probe_uclass,
559 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Bin Mengd9bad172018-10-15 02:20:58 -0700560
561/*
562 * Test that the bus' uclass' child_post_probe() is called after the
563 * device's probe() method
564 */
565static int dm_test_bus_child_post_probe_uclass(struct unit_test_state *uts)
566{
567 struct udevice *bus, *dev;
568 int child_count;
569
570 /*
571 * See testfdt_drv_probe() which effectively initializes that
572 * the uclass postp flag is set to a value
573 */
574 ut_assertok(uclass_get_device(UCLASS_TEST_BUS, 0, &bus));
575 for (device_find_first_child(bus, &dev), child_count = 0;
576 dev;
577 device_find_next_child(&dev)) {
578 struct dm_test_priv *priv = dev_get_priv(dev);
579
580 /* Check that things happened in the right order */
581 ut_asserteq_ptr(NULL, priv);
582 ut_assertok(device_probe(dev));
583
584 priv = dev_get_priv(dev);
585 ut_assert(priv != NULL);
586 ut_asserteq(0, priv->uclass_postp);
587 child_count++;
588 }
589 ut_asserteq(3, child_count);
590
591 return 0;
592}
593DM_TEST(dm_test_bus_child_post_probe_uclass,
594 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);