blob: 976a70604fbda21e3c95a6242a06c40937d24b42 [file] [log] [blame]
Simon Glassb2c1cac2014-02-26 15:59:21 -07001/*
2 * Tests for the core driver model code
3 *
4 * Copyright (c) 2013 Google, Inc
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <errno.h>
11#include <dm.h>
12#include <fdtdec.h>
13#include <malloc.h>
14#include <dm/device-internal.h>
15#include <dm/root.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070016#include <dm/util.h>
17#include <dm/test.h>
18#include <dm/uclass-internal.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050019#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070020
21DECLARE_GLOBAL_DATA_PTR;
22
23enum {
24 TEST_INTVAL1 = 0,
25 TEST_INTVAL2 = 3,
26 TEST_INTVAL3 = 6,
27 TEST_INTVAL_MANUAL = 101112,
Simon Glassfef72b72014-07-23 06:55:03 -060028 TEST_INTVAL_PRE_RELOC = 7,
Simon Glassb2c1cac2014-02-26 15:59:21 -070029};
30
31static const struct dm_test_pdata test_pdata[] = {
32 { .ping_add = TEST_INTVAL1, },
33 { .ping_add = TEST_INTVAL2, },
34 { .ping_add = TEST_INTVAL3, },
35};
36
37static const struct dm_test_pdata test_pdata_manual = {
38 .ping_add = TEST_INTVAL_MANUAL,
39};
40
Simon Glassfef72b72014-07-23 06:55:03 -060041static const struct dm_test_pdata test_pdata_pre_reloc = {
42 .ping_add = TEST_INTVAL_PRE_RELOC,
43};
44
Simon Glassb2c1cac2014-02-26 15:59:21 -070045U_BOOT_DEVICE(dm_test_info1) = {
46 .name = "test_drv",
47 .platdata = &test_pdata[0],
48};
49
50U_BOOT_DEVICE(dm_test_info2) = {
51 .name = "test_drv",
52 .platdata = &test_pdata[1],
53};
54
55U_BOOT_DEVICE(dm_test_info3) = {
56 .name = "test_drv",
57 .platdata = &test_pdata[2],
58};
59
60static struct driver_info driver_info_manual = {
61 .name = "test_manual_drv",
62 .platdata = &test_pdata_manual,
63};
64
Simon Glassfef72b72014-07-23 06:55:03 -060065static struct driver_info driver_info_pre_reloc = {
66 .name = "test_pre_reloc_drv",
67 .platdata = &test_pdata_manual,
68};
69
Joe Hershberger3a77be52015-05-20 14:27:27 -050070void dm_leak_check_start(struct unit_test_state *uts)
Simon Glass0927a6f2014-10-04 11:29:50 -060071{
Joe Hershberger3a77be52015-05-20 14:27:27 -050072 uts->start = mallinfo();
73 if (!uts->start.uordblks)
Simon Glass0927a6f2014-10-04 11:29:50 -060074 puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
75}
76
Joe Hershberger3a77be52015-05-20 14:27:27 -050077int dm_leak_check_end(struct unit_test_state *uts)
Simon Glass0927a6f2014-10-04 11:29:50 -060078{
79 struct mallinfo end;
80 int id;
81
82 /* Don't delete the root class, since we started with that */
83 for (id = UCLASS_ROOT + 1; id < UCLASS_COUNT; id++) {
84 struct uclass *uc;
85
86 uc = uclass_find(id);
87 if (!uc)
88 continue;
89 ut_assertok(uclass_destroy(uc));
90 }
91
92 end = mallinfo();
Joe Hershberger3a77be52015-05-20 14:27:27 -050093 ut_asserteq(uts->start.uordblks, end.uordblks);
Simon Glass0927a6f2014-10-04 11:29:50 -060094
95 return 0;
96}
97
Simon Glassb2c1cac2014-02-26 15:59:21 -070098/* Test that binding with platdata occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -050099static int dm_test_autobind(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700100{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500101 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200102 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700103
104 /*
105 * We should have a single class (UCLASS_ROOT) and a single root
106 * device with no children.
107 */
108 ut_assert(dms->root);
109 ut_asserteq(1, list_count_items(&gd->uclass_root));
110 ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
111 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
112
Simon Glassfef72b72014-07-23 06:55:03 -0600113 ut_assertok(dm_scan_platdata(false));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700114
115 /* We should have our test class now at least, plus more children */
116 ut_assert(1 < list_count_items(&gd->uclass_root));
117 ut_assert(0 < list_count_items(&gd->dm_root->child_head));
118
119 /* Our 3 dm_test_infox children should be bound to the test uclass */
120 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
121
122 /* No devices should be probed */
123 list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
124 ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
125
126 /* Our test driver should have been bound 3 times */
127 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
128
129 return 0;
130}
131DM_TEST(dm_test_autobind, 0);
132
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200133/* Test that binding with uclass platdata allocation occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500134static int dm_test_autobind_uclass_pdata_alloc(struct unit_test_state *uts)
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200135{
136 struct dm_test_perdev_uc_pdata *uc_pdata;
137 struct udevice *dev;
138 struct uclass *uc;
139
140 ut_assertok(uclass_get(UCLASS_TEST, &uc));
141 ut_assert(uc);
142
143 /**
144 * Test if test uclass driver requires allocation for the uclass
145 * platform data and then check the dev->uclass_platdata pointer.
146 */
147 ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
148
149 for (uclass_find_first_device(UCLASS_TEST, &dev);
150 dev;
151 uclass_find_next_device(&dev)) {
152 ut_assert(dev);
153
154 uc_pdata = dev_get_uclass_platdata(dev);
155 ut_assert(uc_pdata);
156 }
157
158 return 0;
159}
160DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
161
162/* Test that binding with uclass platdata setting occurs correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500163static int dm_test_autobind_uclass_pdata_valid(struct unit_test_state *uts)
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200164{
165 struct dm_test_perdev_uc_pdata *uc_pdata;
166 struct udevice *dev;
167
168 /**
169 * In the test_postbind() method of test uclass driver, the uclass
170 * platform data should be set to three test int values - test it.
171 */
172 for (uclass_find_first_device(UCLASS_TEST, &dev);
173 dev;
174 uclass_find_next_device(&dev)) {
175 ut_assert(dev);
176
177 uc_pdata = dev_get_uclass_platdata(dev);
178 ut_assert(uc_pdata);
179 ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
180 ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
181 ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
182 }
183
184 return 0;
185}
186DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
187
Simon Glassb2c1cac2014-02-26 15:59:21 -0700188/* Test that autoprobe finds all the expected devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500189static int dm_test_autoprobe(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700190{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500191 struct dm_test_state *dms = uts->priv;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700192 int expected_base_add;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200193 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700194 struct uclass *uc;
195 int i;
196
197 ut_assertok(uclass_get(UCLASS_TEST, &uc));
198 ut_assert(uc);
199
200 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700201 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700202 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
203
204 /* The root device should not be activated until needed */
Simon Glass6d3b3e22014-07-23 06:55:00 -0600205 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700206
207 /*
208 * We should be able to find the three test devices, and they should
209 * all be activated as they are used (lazy activation, required by
210 * U-Boot)
211 */
212 for (i = 0; i < 3; i++) {
213 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
214 ut_assert(dev);
215 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
216 "Driver %d/%s already activated", i, dev->name);
217
218 /* This should activate it */
219 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
220 ut_assert(dev);
221 ut_assert(dev->flags & DM_FLAG_ACTIVATED);
222
223 /* Activating a device should activate the root device */
224 if (!i)
225 ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
226 }
227
Simon Glass9c1f3822015-03-05 12:25:22 -0700228 /*
229 * Our 3 dm_test_info children should be passed to pre_probe and
230 * post_probe
231 */
Simon Glassb2c1cac2014-02-26 15:59:21 -0700232 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
Simon Glass9c1f3822015-03-05 12:25:22 -0700233 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700234
235 /* Also we can check the per-device data */
236 expected_base_add = 0;
237 for (i = 0; i < 3; i++) {
238 struct dm_test_uclass_perdev_priv *priv;
239 struct dm_test_pdata *pdata;
240
241 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
242 ut_assert(dev);
243
Simon Glassde0977b2015-03-05 12:25:20 -0700244 priv = dev_get_uclass_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700245 ut_assert(priv);
246 ut_asserteq(expected_base_add, priv->base_add);
247
248 pdata = dev->platdata;
249 expected_base_add += pdata->ping_add;
250 }
251
252 return 0;
253}
254DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
255
256/* Check that we see the correct platdata in each device */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500257static int dm_test_platdata(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700258{
259 const struct dm_test_pdata *pdata;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200260 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700261 int i;
262
263 for (i = 0; i < 3; i++) {
264 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
265 ut_assert(dev);
266 pdata = dev->platdata;
267 ut_assert(pdata->ping_add == test_pdata[i].ping_add);
268 }
269
270 return 0;
271}
272DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
273
274/* Test that we can bind, probe, remove, unbind a driver */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500275static int dm_test_lifecycle(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700276{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500277 struct dm_test_state *dms = uts->priv;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700278 int op_count[DM_TEST_OP_COUNT];
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200279 struct udevice *dev, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700280 int pingret;
281 int ret;
282
283 memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
284
Simon Glassfef72b72014-07-23 06:55:03 -0600285 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700286 &dev));
287 ut_assert(dev);
288 ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
289 == op_count[DM_TEST_OP_BIND] + 1);
290 ut_assert(!dev->priv);
291
292 /* Probe the device - it should fail allocating private data */
293 dms->force_fail_alloc = 1;
294 ret = device_probe(dev);
295 ut_assert(ret == -ENOMEM);
296 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
297 == op_count[DM_TEST_OP_PROBE] + 1);
298 ut_assert(!dev->priv);
299
300 /* Try again without the alloc failure */
301 dms->force_fail_alloc = 0;
302 ut_assertok(device_probe(dev));
303 ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
304 == op_count[DM_TEST_OP_PROBE] + 2);
305 ut_assert(dev->priv);
306
307 /* This should be device 3 in the uclass */
308 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
309 ut_assert(dev == test_dev);
310
311 /* Try ping */
312 ut_assertok(test_ping(dev, 100, &pingret));
313 ut_assert(pingret == 102);
314
315 /* Now remove device 3 */
316 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
317 ut_assertok(device_remove(dev));
318 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
319
320 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
321 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
322 ut_assertok(device_unbind(dev));
323 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
324 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
325
326 return 0;
327}
328DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
329
330/* Test that we can bind/unbind and the lists update correctly */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500331static int dm_test_ordering(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700332{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500333 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200334 struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700335 int pingret;
336
Simon Glassfef72b72014-07-23 06:55:03 -0600337 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700338 &dev));
339 ut_assert(dev);
340
341 /* Bind two new devices (numbers 4 and 5) */
Simon Glassfef72b72014-07-23 06:55:03 -0600342 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700343 &dev_penultimate));
344 ut_assert(dev_penultimate);
Simon Glassfef72b72014-07-23 06:55:03 -0600345 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700346 &dev_last));
347 ut_assert(dev_last);
348
349 /* Now remove device 3 */
350 ut_assertok(device_remove(dev));
351 ut_assertok(device_unbind(dev));
352
353 /* The device numbering should have shifted down one */
354 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
355 ut_assert(dev_penultimate == test_dev);
356 ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
357 ut_assert(dev_last == test_dev);
358
359 /* Add back the original device 3, now in position 5 */
Simon Glassfef72b72014-07-23 06:55:03 -0600360 ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
361 &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700362 ut_assert(dev);
363
364 /* Try ping */
365 ut_assertok(test_ping(dev, 100, &pingret));
366 ut_assert(pingret == 102);
367
368 /* Remove 3 and 4 */
369 ut_assertok(device_remove(dev_penultimate));
370 ut_assertok(device_unbind(dev_penultimate));
371 ut_assertok(device_remove(dev_last));
372 ut_assertok(device_unbind(dev_last));
373
374 /* Our device should now be in position 3 */
375 ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
376 ut_assert(dev == test_dev);
377
378 /* Now remove device 3 */
379 ut_assertok(device_remove(dev));
380 ut_assertok(device_unbind(dev));
381
382 return 0;
383}
384DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
385
386/* Check that we can perform operations on a device (do a ping) */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500387int dm_check_operations(struct unit_test_state *uts, struct udevice *dev,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700388 uint32_t base, struct dm_test_priv *priv)
389{
390 int expected;
391 int pingret;
392
393 /* Getting the child device should allocate platdata / priv */
394 ut_assertok(testfdt_ping(dev, 10, &pingret));
395 ut_assert(dev->priv);
396 ut_assert(dev->platdata);
397
398 expected = 10 + base;
399 ut_asserteq(expected, pingret);
400
401 /* Do another ping */
402 ut_assertok(testfdt_ping(dev, 20, &pingret));
403 expected = 20 + base;
404 ut_asserteq(expected, pingret);
405
406 /* Now check the ping_total */
407 priv = dev->priv;
408 ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
409 priv->ping_total);
410
411 return 0;
412}
413
414/* Check that we can perform operations on devices */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500415static int dm_test_operations(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700416{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200417 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700418 int i;
419
420 /*
421 * Now check that the ping adds are what we expect. This is using the
422 * ping-add property in each node.
423 */
424 for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
425 uint32_t base;
426
427 ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
428
429 /*
430 * Get the 'reg' property, which tells us what the ping add
431 * should be. We don't use the platdata because we want
432 * to test the code that sets that up (testfdt_drv_probe()).
433 */
434 base = test_pdata[i].ping_add;
435 debug("dev=%d, base=%d\n", i, base);
436
Joe Hershberger3a77be52015-05-20 14:27:27 -0500437 ut_assert(!dm_check_operations(uts, dev, base, dev->priv));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700438 }
439
440 return 0;
441}
442DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
443
444/* Remove all drivers and check that things work */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500445static int dm_test_remove(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700446{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200447 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700448 int i;
449
450 for (i = 0; i < 3; i++) {
451 ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
452 ut_assert(dev);
453 ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
454 "Driver %d/%s not activated", i, dev->name);
455 ut_assertok(device_remove(dev));
456 ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
457 "Driver %d/%s should have deactivated", i,
458 dev->name);
459 ut_assert(!dev->priv);
460 }
461
462 return 0;
463}
464DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
465
466/* Remove and recreate everything, check for memory leaks */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500467static int dm_test_leak(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700468{
469 int i;
470
471 for (i = 0; i < 2; i++) {
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200472 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700473 int ret;
474 int id;
475
Joe Hershberger3a77be52015-05-20 14:27:27 -0500476 dm_leak_check_start(uts);
Simon Glassb2c1cac2014-02-26 15:59:21 -0700477
Simon Glassfef72b72014-07-23 06:55:03 -0600478 ut_assertok(dm_scan_platdata(false));
479 ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700480
481 /* Scanning the uclass is enough to probe all the devices */
482 for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
483 for (ret = uclass_first_device(UCLASS_TEST, &dev);
484 dev;
485 ret = uclass_next_device(&dev))
486 ;
487 ut_assertok(ret);
488 }
489
Joe Hershberger3a77be52015-05-20 14:27:27 -0500490 ut_assertok(dm_leak_check_end(uts));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700491 }
492
493 return 0;
494}
495DM_TEST(dm_test_leak, 0);
496
497/* Test uclass init/destroy methods */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500498static int dm_test_uclass(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700499{
500 struct uclass *uc;
501
502 ut_assertok(uclass_get(UCLASS_TEST, &uc));
503 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
504 ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
505 ut_assert(uc->priv);
506
507 ut_assertok(uclass_destroy(uc));
508 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
509 ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
510
511 return 0;
512}
513DM_TEST(dm_test_uclass, 0);
514
515/**
516 * create_children() - Create children of a parent node
517 *
518 * @dms: Test system state
519 * @parent: Parent device
520 * @count: Number of children to create
521 * @key: Key value to put in first child. Subsequence children
522 * receive an incrementing value
523 * @child: If not NULL, then the child device pointers are written into
524 * this array.
525 * @return 0 if OK, -ve on error
526 */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500527static int create_children(struct unit_test_state *uts, struct udevice *parent,
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200528 int count, int key, struct udevice *child[])
Simon Glassb2c1cac2014-02-26 15:59:21 -0700529{
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200530 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700531 int i;
532
533 for (i = 0; i < count; i++) {
534 struct dm_test_pdata *pdata;
535
Simon Glassfef72b72014-07-23 06:55:03 -0600536 ut_assertok(device_bind_by_name(parent, false,
537 &driver_info_manual, &dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700538 pdata = calloc(1, sizeof(*pdata));
539 pdata->ping_add = key + i;
540 dev->platdata = pdata;
541 if (child)
542 child[i] = dev;
543 }
544
545 return 0;
546}
547
548#define NODE_COUNT 10
549
Joe Hershberger3a77be52015-05-20 14:27:27 -0500550static int dm_test_children(struct unit_test_state *uts)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700551{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500552 struct dm_test_state *dms = uts->priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200553 struct udevice *top[NODE_COUNT];
554 struct udevice *child[NODE_COUNT];
555 struct udevice *grandchild[NODE_COUNT];
556 struct udevice *dev;
Simon Glassb2c1cac2014-02-26 15:59:21 -0700557 int total;
558 int ret;
559 int i;
560
561 /* We don't care about the numbering for this test */
562 dms->skip_post_probe = 1;
563
564 ut_assert(NODE_COUNT > 5);
565
566 /* First create 10 top-level children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500567 ut_assertok(create_children(uts, dms->root, NODE_COUNT, 0, top));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700568
569 /* Now a few have their own children */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500570 ut_assertok(create_children(uts, top[2], NODE_COUNT, 2, NULL));
571 ut_assertok(create_children(uts, top[5], NODE_COUNT, 5, child));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700572
573 /* And grandchildren */
574 for (i = 0; i < NODE_COUNT; i++)
Joe Hershberger3a77be52015-05-20 14:27:27 -0500575 ut_assertok(create_children(uts, child[i], NODE_COUNT, 50 * i,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700576 i == 2 ? grandchild : NULL));
577
578 /* Check total number of devices */
579 total = NODE_COUNT * (3 + NODE_COUNT);
580 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
581
582 /* Try probing one of the grandchildren */
583 ut_assertok(uclass_get_device(UCLASS_TEST,
584 NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
585 ut_asserteq_ptr(grandchild[0], dev);
586
587 /*
588 * This should have probed the child and top node also, for a total
589 * of 3 nodes.
590 */
591 ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
592
593 /* Probe the other grandchildren */
594 for (i = 1; i < NODE_COUNT; i++)
595 ut_assertok(device_probe(grandchild[i]));
596
597 ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
598
599 /* Probe everything */
600 for (ret = uclass_first_device(UCLASS_TEST, &dev);
601 dev;
602 ret = uclass_next_device(&dev))
603 ;
604 ut_assertok(ret);
605
606 ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
607
608 /* Remove a top-level child and check that the children are removed */
609 ut_assertok(device_remove(top[2]));
610 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
611 dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
612
613 /* Try one with grandchildren */
614 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
615 ut_asserteq_ptr(dev, top[5]);
616 ut_assertok(device_remove(dev));
617 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
618 dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
619
620 /* Try the same with unbind */
621 ut_assertok(device_unbind(top[2]));
622 ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
623 dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
624
625 /* Try one with grandchildren */
626 ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
627 ut_asserteq_ptr(dev, top[6]);
628 ut_assertok(device_unbind(top[5]));
629 ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
630 dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
631
632 return 0;
633}
634DM_TEST(dm_test_children, 0);
Simon Glassfef72b72014-07-23 06:55:03 -0600635
636/* Test that pre-relocation devices work as expected */
Joe Hershberger3a77be52015-05-20 14:27:27 -0500637static int dm_test_pre_reloc(struct unit_test_state *uts)
Simon Glassfef72b72014-07-23 06:55:03 -0600638{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500639 struct dm_test_state *dms = uts->priv;
Simon Glassfef72b72014-07-23 06:55:03 -0600640 struct udevice *dev;
641
642 /* The normal driver should refuse to bind before relocation */
643 ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
644 &driver_info_manual, &dev));
645
646 /* But this one is marked pre-reloc */
647 ut_assertok(device_bind_by_name(dms->root, true,
648 &driver_info_pre_reloc, &dev));
649
650 return 0;
651}
652DM_TEST(dm_test_pre_reloc, 0);
Simon Glassde708672014-07-23 06:55:15 -0600653
Joe Hershberger3a77be52015-05-20 14:27:27 -0500654static int dm_test_uclass_before_ready(struct unit_test_state *uts)
Simon Glassde708672014-07-23 06:55:15 -0600655{
656 struct uclass *uc;
657
658 ut_assertok(uclass_get(UCLASS_TEST, &uc));
659
Simon Glass51a0eac2015-04-19 07:21:02 -0600660 gd->dm_root = NULL;
661 gd->dm_root_f = NULL;
662 memset(&gd->uclass_root, '\0', sizeof(gd->uclass_root));
663
Simon Glassde708672014-07-23 06:55:15 -0600664 ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
665
666 return 0;
667}
Simon Glassde708672014-07-23 06:55:15 -0600668DM_TEST(dm_test_uclass_before_ready, 0);
Simon Glass98fd5d12015-01-25 08:27:04 -0700669
Joe Hershberger3a77be52015-05-20 14:27:27 -0500670static int dm_test_uclass_devices_find(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +0200671{
672 struct udevice *dev;
673 int ret;
674
675 for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
676 dev;
677 ret = uclass_find_next_device(&dev)) {
678 ut_assert(!ret);
679 ut_assert(dev);
680 }
681
682 return 0;
683}
684DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
685
Joe Hershberger3a77be52015-05-20 14:27:27 -0500686static int dm_test_uclass_devices_find_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +0200687{
688 struct udevice *finddev;
689 struct udevice *testdev;
690 int findret, ret;
691
692 /*
693 * For each test device found in fdt like: "a-test", "b-test", etc.,
694 * use its name and try to find it by uclass_find_device_by_name().
695 * Then, on success check if:
696 * - current 'testdev' name is equal to the returned 'finddev' name
697 * - current 'testdev' pointer is equal to the returned 'finddev'
698 *
699 * We assume that, each uclass's device name is unique, so if not, then
700 * this will fail on checking condition: testdev == finddev, since the
701 * uclass_find_device_by_name(), returns the first device by given name.
702 */
703 for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
704 testdev;
705 ret = uclass_find_next_device(&testdev)) {
706 ut_assertok(ret);
707 ut_assert(testdev);
708
709 findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
710 testdev->name,
711 &finddev);
712
713 ut_assertok(findret);
714 ut_assert(testdev);
715 ut_asserteq_str(testdev->name, finddev->name);
716 ut_asserteq_ptr(testdev, finddev);
717 }
718
719 return 0;
720}
721DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
722
Joe Hershberger3a77be52015-05-20 14:27:27 -0500723static int dm_test_uclass_devices_get(struct unit_test_state *uts)
Przemyslaw Marczak1e0a7f22015-04-15 13:07:20 +0200724{
725 struct udevice *dev;
726 int ret;
727
728 for (ret = uclass_first_device(UCLASS_TEST, &dev);
729 dev;
730 ret = uclass_next_device(&dev)) {
731 ut_assert(!ret);
732 ut_assert(dev);
733 ut_assert(device_active(dev));
734 }
735
736 return 0;
737}
738DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
739
Joe Hershberger3a77be52015-05-20 14:27:27 -0500740static int dm_test_uclass_devices_get_by_name(struct unit_test_state *uts)
Przemyslaw Marczak2eff02f2015-04-20 13:32:33 +0200741{
742 struct udevice *finddev;
743 struct udevice *testdev;
744 int ret, findret;
745
746 /*
747 * For each test device found in fdt like: "a-test", "b-test", etc.,
748 * use its name and try to get it by uclass_get_device_by_name().
749 * On success check if:
750 * - returned finddev' is active
751 * - current 'testdev' name is equal to the returned 'finddev' name
752 * - current 'testdev' pointer is equal to the returned 'finddev'
753 *
754 * We asserts that the 'testdev' is active on each loop entry, so we
755 * could be sure that the 'finddev' is activated too, but for sure
756 * we check it again.
757 *
758 * We assume that, each uclass's device name is unique, so if not, then
759 * this will fail on checking condition: testdev == finddev, since the
760 * uclass_get_device_by_name(), returns the first device by given name.
761 */
762 for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
763 testdev;
764 ret = uclass_next_device(&testdev)) {
765 ut_assertok(ret);
766 ut_assert(testdev);
767 ut_assert(device_active(testdev));
768
769 findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
770 testdev->name,
771 &finddev);
772
773 ut_assertok(findret);
774 ut_assert(finddev);
775 ut_assert(device_active(finddev));
776 ut_asserteq_str(testdev->name, finddev->name);
777 ut_asserteq_ptr(testdev, finddev);
778 }
779
780 return 0;
781}
782DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
783
Joe Hershberger3a77be52015-05-20 14:27:27 -0500784static int dm_test_device_get_uclass_id(struct unit_test_state *uts)
Simon Glass98fd5d12015-01-25 08:27:04 -0700785{
786 struct udevice *dev;
787
788 ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
789 ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
790
791 return 0;
792}
793DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);