xref: /rk3399_rockchip-uboot/test/dm/core.c (revision a7b8250210cd1b449a06f7d20769944ebeca1a41)
1 /*
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>
16 #include <dm/ut.h>
17 #include <dm/util.h>
18 #include <dm/test.h>
19 #include <dm/uclass-internal.h>
20 
21 DECLARE_GLOBAL_DATA_PTR;
22 
23 enum {
24 	TEST_INTVAL1		= 0,
25 	TEST_INTVAL2		= 3,
26 	TEST_INTVAL3		= 6,
27 	TEST_INTVAL_MANUAL	= 101112,
28 	TEST_INTVAL_PRE_RELOC	= 7,
29 };
30 
31 static const struct dm_test_pdata test_pdata[] = {
32 	{ .ping_add		= TEST_INTVAL1, },
33 	{ .ping_add		= TEST_INTVAL2, },
34 	{ .ping_add		= TEST_INTVAL3, },
35 };
36 
37 static const struct dm_test_pdata test_pdata_manual = {
38 	.ping_add		= TEST_INTVAL_MANUAL,
39 };
40 
41 static const struct dm_test_pdata test_pdata_pre_reloc = {
42 	.ping_add		= TEST_INTVAL_PRE_RELOC,
43 };
44 
45 U_BOOT_DEVICE(dm_test_info1) = {
46 	.name = "test_drv",
47 	.platdata = &test_pdata[0],
48 };
49 
50 U_BOOT_DEVICE(dm_test_info2) = {
51 	.name = "test_drv",
52 	.platdata = &test_pdata[1],
53 };
54 
55 U_BOOT_DEVICE(dm_test_info3) = {
56 	.name = "test_drv",
57 	.platdata = &test_pdata[2],
58 };
59 
60 static struct driver_info driver_info_manual = {
61 	.name = "test_manual_drv",
62 	.platdata = &test_pdata_manual,
63 };
64 
65 static struct driver_info driver_info_pre_reloc = {
66 	.name = "test_pre_reloc_drv",
67 	.platdata = &test_pdata_manual,
68 };
69 
70 void dm_leak_check_start(struct dm_test_state *dms)
71 {
72 	dms->start = mallinfo();
73 	if (!dms->start.uordblks)
74 		puts("Warning: Please add '#define DEBUG' to the top of common/dlmalloc.c\n");
75 }
76 
77 int dm_leak_check_end(struct dm_test_state *dms)
78 {
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();
93 	ut_asserteq(dms->start.uordblks, end.uordblks);
94 
95 	return 0;
96 }
97 
98 /* Test that binding with platdata occurs correctly */
99 static int dm_test_autobind(struct dm_test_state *dms)
100 {
101 	struct udevice *dev;
102 
103 	/*
104 	 * We should have a single class (UCLASS_ROOT) and a single root
105 	 * device with no children.
106 	 */
107 	ut_assert(dms->root);
108 	ut_asserteq(1, list_count_items(&gd->uclass_root));
109 	ut_asserteq(0, list_count_items(&gd->dm_root->child_head));
110 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
111 
112 	ut_assertok(dm_scan_platdata(false));
113 
114 	/* We should have our test class now at least, plus more children */
115 	ut_assert(1 < list_count_items(&gd->uclass_root));
116 	ut_assert(0 < list_count_items(&gd->dm_root->child_head));
117 
118 	/* Our 3 dm_test_infox children should be bound to the test uclass */
119 	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_BIND]);
120 
121 	/* No devices should be probed */
122 	list_for_each_entry(dev, &gd->dm_root->child_head, sibling_node)
123 		ut_assert(!(dev->flags & DM_FLAG_ACTIVATED));
124 
125 	/* Our test driver should have been bound 3 times */
126 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND] == 3);
127 
128 	return 0;
129 }
130 DM_TEST(dm_test_autobind, 0);
131 
132 /* Test that binding with uclass platdata allocation occurs correctly */
133 static int dm_test_autobind_uclass_pdata_alloc(struct dm_test_state *dms)
134 {
135 	struct dm_test_perdev_uc_pdata *uc_pdata;
136 	struct udevice *dev;
137 	struct uclass *uc;
138 
139 	ut_assertok(uclass_get(UCLASS_TEST, &uc));
140 	ut_assert(uc);
141 
142 	/**
143 	 * Test if test uclass driver requires allocation for the uclass
144 	 * platform data and then check the dev->uclass_platdata pointer.
145 	 */
146 	ut_assert(uc->uc_drv->per_device_platdata_auto_alloc_size);
147 
148 	for (uclass_find_first_device(UCLASS_TEST, &dev);
149 	     dev;
150 	     uclass_find_next_device(&dev)) {
151 		ut_assert(dev);
152 
153 		uc_pdata = dev_get_uclass_platdata(dev);
154 		ut_assert(uc_pdata);
155 	}
156 
157 	return 0;
158 }
159 DM_TEST(dm_test_autobind_uclass_pdata_alloc, DM_TESTF_SCAN_PDATA);
160 
161 /* Test that binding with uclass platdata setting occurs correctly */
162 static int dm_test_autobind_uclass_pdata_valid(struct dm_test_state *dms)
163 {
164 	struct dm_test_perdev_uc_pdata *uc_pdata;
165 	struct udevice *dev;
166 
167 	/**
168 	 * In the test_postbind() method of test uclass driver, the uclass
169 	 * platform data should be set to three test int values - test it.
170 	 */
171 	for (uclass_find_first_device(UCLASS_TEST, &dev);
172 	     dev;
173 	     uclass_find_next_device(&dev)) {
174 		ut_assert(dev);
175 
176 		uc_pdata = dev_get_uclass_platdata(dev);
177 		ut_assert(uc_pdata);
178 		ut_assert(uc_pdata->intval1 == TEST_UC_PDATA_INTVAL1);
179 		ut_assert(uc_pdata->intval2 == TEST_UC_PDATA_INTVAL2);
180 		ut_assert(uc_pdata->intval3 == TEST_UC_PDATA_INTVAL3);
181 	}
182 
183 	return 0;
184 }
185 DM_TEST(dm_test_autobind_uclass_pdata_valid, DM_TESTF_SCAN_PDATA);
186 
187 /* Test that autoprobe finds all the expected devices */
188 static int dm_test_autoprobe(struct dm_test_state *dms)
189 {
190 	int expected_base_add;
191 	struct udevice *dev;
192 	struct uclass *uc;
193 	int i;
194 
195 	ut_assertok(uclass_get(UCLASS_TEST, &uc));
196 	ut_assert(uc);
197 
198 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
199 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
200 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
201 
202 	/* The root device should not be activated until needed */
203 	ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
204 
205 	/*
206 	 * We should be able to find the three test devices, and they should
207 	 * all be activated as they are used (lazy activation, required by
208 	 * U-Boot)
209 	 */
210 	for (i = 0; i < 3; i++) {
211 		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
212 		ut_assert(dev);
213 		ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
214 			   "Driver %d/%s already activated", i, dev->name);
215 
216 		/* This should activate it */
217 		ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
218 		ut_assert(dev);
219 		ut_assert(dev->flags & DM_FLAG_ACTIVATED);
220 
221 		/* Activating a device should activate the root device */
222 		if (!i)
223 			ut_assert(dms->root->flags & DM_FLAG_ACTIVATED);
224 	}
225 
226 	/*
227 	 * Our 3 dm_test_info children should be passed to pre_probe and
228 	 * post_probe
229 	 */
230 	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]);
231 	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]);
232 
233 	/* Also we can check the per-device data */
234 	expected_base_add = 0;
235 	for (i = 0; i < 3; i++) {
236 		struct dm_test_uclass_perdev_priv *priv;
237 		struct dm_test_pdata *pdata;
238 
239 		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
240 		ut_assert(dev);
241 
242 		priv = dev_get_uclass_priv(dev);
243 		ut_assert(priv);
244 		ut_asserteq(expected_base_add, priv->base_add);
245 
246 		pdata = dev->platdata;
247 		expected_base_add += pdata->ping_add;
248 	}
249 
250 	return 0;
251 }
252 DM_TEST(dm_test_autoprobe, DM_TESTF_SCAN_PDATA);
253 
254 /* Check that we see the correct platdata in each device */
255 static int dm_test_platdata(struct dm_test_state *dms)
256 {
257 	const struct dm_test_pdata *pdata;
258 	struct udevice *dev;
259 	int i;
260 
261 	for (i = 0; i < 3; i++) {
262 		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
263 		ut_assert(dev);
264 		pdata = dev->platdata;
265 		ut_assert(pdata->ping_add == test_pdata[i].ping_add);
266 	}
267 
268 	return 0;
269 }
270 DM_TEST(dm_test_platdata, DM_TESTF_SCAN_PDATA);
271 
272 /* Test that we can bind, probe, remove, unbind a driver */
273 static int dm_test_lifecycle(struct dm_test_state *dms)
274 {
275 	int op_count[DM_TEST_OP_COUNT];
276 	struct udevice *dev, *test_dev;
277 	int pingret;
278 	int ret;
279 
280 	memcpy(op_count, dm_testdrv_op_count, sizeof(op_count));
281 
282 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
283 					&dev));
284 	ut_assert(dev);
285 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_BIND]
286 			== op_count[DM_TEST_OP_BIND] + 1);
287 	ut_assert(!dev->priv);
288 
289 	/* Probe the device - it should fail allocating private data */
290 	dms->force_fail_alloc = 1;
291 	ret = device_probe(dev);
292 	ut_assert(ret == -ENOMEM);
293 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
294 			== op_count[DM_TEST_OP_PROBE] + 1);
295 	ut_assert(!dev->priv);
296 
297 	/* Try again without the alloc failure */
298 	dms->force_fail_alloc = 0;
299 	ut_assertok(device_probe(dev));
300 	ut_assert(dm_testdrv_op_count[DM_TEST_OP_PROBE]
301 			== op_count[DM_TEST_OP_PROBE] + 2);
302 	ut_assert(dev->priv);
303 
304 	/* This should be device 3 in the uclass */
305 	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
306 	ut_assert(dev == test_dev);
307 
308 	/* Try ping */
309 	ut_assertok(test_ping(dev, 100, &pingret));
310 	ut_assert(pingret == 102);
311 
312 	/* Now remove device 3 */
313 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
314 	ut_assertok(device_remove(dev));
315 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]);
316 
317 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
318 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
319 	ut_assertok(device_unbind(dev));
320 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
321 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]);
322 
323 	return 0;
324 }
325 DM_TEST(dm_test_lifecycle, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
326 
327 /* Test that we can bind/unbind and the lists update correctly */
328 static int dm_test_ordering(struct dm_test_state *dms)
329 {
330 	struct udevice *dev, *dev_penultimate, *dev_last, *test_dev;
331 	int pingret;
332 
333 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
334 					&dev));
335 	ut_assert(dev);
336 
337 	/* Bind two new devices (numbers 4 and 5) */
338 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
339 					&dev_penultimate));
340 	ut_assert(dev_penultimate);
341 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
342 					&dev_last));
343 	ut_assert(dev_last);
344 
345 	/* Now remove device 3 */
346 	ut_assertok(device_remove(dev));
347 	ut_assertok(device_unbind(dev));
348 
349 	/* The device numbering should have shifted down one */
350 	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
351 	ut_assert(dev_penultimate == test_dev);
352 	ut_assertok(uclass_find_device(UCLASS_TEST, 4, &test_dev));
353 	ut_assert(dev_last == test_dev);
354 
355 	/* Add back the original device 3, now in position 5 */
356 	ut_assertok(device_bind_by_name(dms->root, false, &driver_info_manual,
357 					&dev));
358 	ut_assert(dev);
359 
360 	/* Try ping */
361 	ut_assertok(test_ping(dev, 100, &pingret));
362 	ut_assert(pingret == 102);
363 
364 	/* Remove 3 and 4 */
365 	ut_assertok(device_remove(dev_penultimate));
366 	ut_assertok(device_unbind(dev_penultimate));
367 	ut_assertok(device_remove(dev_last));
368 	ut_assertok(device_unbind(dev_last));
369 
370 	/* Our device should now be in position 3 */
371 	ut_assertok(uclass_find_device(UCLASS_TEST, 3, &test_dev));
372 	ut_assert(dev == test_dev);
373 
374 	/* Now remove device 3 */
375 	ut_assertok(device_remove(dev));
376 	ut_assertok(device_unbind(dev));
377 
378 	return 0;
379 }
380 DM_TEST(dm_test_ordering, DM_TESTF_SCAN_PDATA);
381 
382 /* Check that we can perform operations on a device (do a ping) */
383 int dm_check_operations(struct dm_test_state *dms, struct udevice *dev,
384 			uint32_t base, struct dm_test_priv *priv)
385 {
386 	int expected;
387 	int pingret;
388 
389 	/* Getting the child device should allocate platdata / priv */
390 	ut_assertok(testfdt_ping(dev, 10, &pingret));
391 	ut_assert(dev->priv);
392 	ut_assert(dev->platdata);
393 
394 	expected = 10 + base;
395 	ut_asserteq(expected, pingret);
396 
397 	/* Do another ping */
398 	ut_assertok(testfdt_ping(dev, 20, &pingret));
399 	expected = 20 + base;
400 	ut_asserteq(expected, pingret);
401 
402 	/* Now check the ping_total */
403 	priv = dev->priv;
404 	ut_asserteq(DM_TEST_START_TOTAL + 10 + 20 + base * 2,
405 		    priv->ping_total);
406 
407 	return 0;
408 }
409 
410 /* Check that we can perform operations on devices */
411 static int dm_test_operations(struct dm_test_state *dms)
412 {
413 	struct udevice *dev;
414 	int i;
415 
416 	/*
417 	 * Now check that the ping adds are what we expect. This is using the
418 	 * ping-add property in each node.
419 	 */
420 	for (i = 0; i < ARRAY_SIZE(test_pdata); i++) {
421 		uint32_t base;
422 
423 		ut_assertok(uclass_get_device(UCLASS_TEST, i, &dev));
424 
425 		/*
426 		 * Get the 'reg' property, which tells us what the ping add
427 		 * should be. We don't use the platdata because we want
428 		 * to test the code that sets that up (testfdt_drv_probe()).
429 		 */
430 		base = test_pdata[i].ping_add;
431 		debug("dev=%d, base=%d\n", i, base);
432 
433 		ut_assert(!dm_check_operations(dms, dev, base, dev->priv));
434 	}
435 
436 	return 0;
437 }
438 DM_TEST(dm_test_operations, DM_TESTF_SCAN_PDATA);
439 
440 /* Remove all drivers and check that things work */
441 static int dm_test_remove(struct dm_test_state *dms)
442 {
443 	struct udevice *dev;
444 	int i;
445 
446 	for (i = 0; i < 3; i++) {
447 		ut_assertok(uclass_find_device(UCLASS_TEST, i, &dev));
448 		ut_assert(dev);
449 		ut_assertf(dev->flags & DM_FLAG_ACTIVATED,
450 			   "Driver %d/%s not activated", i, dev->name);
451 		ut_assertok(device_remove(dev));
452 		ut_assertf(!(dev->flags & DM_FLAG_ACTIVATED),
453 			   "Driver %d/%s should have deactivated", i,
454 			   dev->name);
455 		ut_assert(!dev->priv);
456 	}
457 
458 	return 0;
459 }
460 DM_TEST(dm_test_remove, DM_TESTF_SCAN_PDATA | DM_TESTF_PROBE_TEST);
461 
462 /* Remove and recreate everything, check for memory leaks */
463 static int dm_test_leak(struct dm_test_state *dms)
464 {
465 	int i;
466 
467 	for (i = 0; i < 2; i++) {
468 		struct udevice *dev;
469 		int ret;
470 		int id;
471 
472 		dm_leak_check_start(dms);
473 
474 		ut_assertok(dm_scan_platdata(false));
475 		ut_assertok(dm_scan_fdt(gd->fdt_blob, false));
476 
477 		/* Scanning the uclass is enough to probe all the devices */
478 		for (id = UCLASS_ROOT; id < UCLASS_COUNT; id++) {
479 			for (ret = uclass_first_device(UCLASS_TEST, &dev);
480 			     dev;
481 			     ret = uclass_next_device(&dev))
482 				;
483 			ut_assertok(ret);
484 		}
485 
486 		ut_assertok(dm_leak_check_end(dms));
487 	}
488 
489 	return 0;
490 }
491 DM_TEST(dm_test_leak, 0);
492 
493 /* Test uclass init/destroy methods */
494 static int dm_test_uclass(struct dm_test_state *dms)
495 {
496 	struct uclass *uc;
497 
498 	ut_assertok(uclass_get(UCLASS_TEST, &uc));
499 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
500 	ut_asserteq(0, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
501 	ut_assert(uc->priv);
502 
503 	ut_assertok(uclass_destroy(uc));
504 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_INIT]);
505 	ut_asserteq(1, dm_testdrv_op_count[DM_TEST_OP_DESTROY]);
506 
507 	return 0;
508 }
509 DM_TEST(dm_test_uclass, 0);
510 
511 /**
512  * create_children() - Create children of a parent node
513  *
514  * @dms:	Test system state
515  * @parent:	Parent device
516  * @count:	Number of children to create
517  * @key:	Key value to put in first child. Subsequence children
518  *		receive an incrementing value
519  * @child:	If not NULL, then the child device pointers are written into
520  *		this array.
521  * @return 0 if OK, -ve on error
522  */
523 static int create_children(struct dm_test_state *dms, struct udevice *parent,
524 			   int count, int key, struct udevice *child[])
525 {
526 	struct udevice *dev;
527 	int i;
528 
529 	for (i = 0; i < count; i++) {
530 		struct dm_test_pdata *pdata;
531 
532 		ut_assertok(device_bind_by_name(parent, false,
533 						&driver_info_manual, &dev));
534 		pdata = calloc(1, sizeof(*pdata));
535 		pdata->ping_add = key + i;
536 		dev->platdata = pdata;
537 		if (child)
538 			child[i] = dev;
539 	}
540 
541 	return 0;
542 }
543 
544 #define NODE_COUNT	10
545 
546 static int dm_test_children(struct dm_test_state *dms)
547 {
548 	struct udevice *top[NODE_COUNT];
549 	struct udevice *child[NODE_COUNT];
550 	struct udevice *grandchild[NODE_COUNT];
551 	struct udevice *dev;
552 	int total;
553 	int ret;
554 	int i;
555 
556 	/* We don't care about the numbering for this test */
557 	dms->skip_post_probe = 1;
558 
559 	ut_assert(NODE_COUNT > 5);
560 
561 	/* First create 10 top-level children */
562 	ut_assertok(create_children(dms, dms->root, NODE_COUNT, 0, top));
563 
564 	/* Now a few have their own children */
565 	ut_assertok(create_children(dms, top[2], NODE_COUNT, 2, NULL));
566 	ut_assertok(create_children(dms, top[5], NODE_COUNT, 5, child));
567 
568 	/* And grandchildren */
569 	for (i = 0; i < NODE_COUNT; i++)
570 		ut_assertok(create_children(dms, child[i], NODE_COUNT, 50 * i,
571 					    i == 2 ? grandchild : NULL));
572 
573 	/* Check total number of devices */
574 	total = NODE_COUNT * (3 + NODE_COUNT);
575 	ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_BIND]);
576 
577 	/* Try probing one of the grandchildren */
578 	ut_assertok(uclass_get_device(UCLASS_TEST,
579 				      NODE_COUNT * 3 + 2 * NODE_COUNT, &dev));
580 	ut_asserteq_ptr(grandchild[0], dev);
581 
582 	/*
583 	 * This should have probed the child and top node also, for a total
584 	 * of 3 nodes.
585 	 */
586 	ut_asserteq(3, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
587 
588 	/* Probe the other grandchildren */
589 	for (i = 1; i < NODE_COUNT; i++)
590 		ut_assertok(device_probe(grandchild[i]));
591 
592 	ut_asserteq(2 + NODE_COUNT, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
593 
594 	/* Probe everything */
595 	for (ret = uclass_first_device(UCLASS_TEST, &dev);
596 	     dev;
597 	     ret = uclass_next_device(&dev))
598 		;
599 	ut_assertok(ret);
600 
601 	ut_asserteq(total, dm_testdrv_op_count[DM_TEST_OP_PROBE]);
602 
603 	/* Remove a top-level child and check that the children are removed */
604 	ut_assertok(device_remove(top[2]));
605 	ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
606 	dm_testdrv_op_count[DM_TEST_OP_REMOVE] = 0;
607 
608 	/* Try one with grandchildren */
609 	ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
610 	ut_asserteq_ptr(dev, top[5]);
611 	ut_assertok(device_remove(dev));
612 	ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
613 		    dm_testdrv_op_count[DM_TEST_OP_REMOVE]);
614 
615 	/* Try the same with unbind */
616 	ut_assertok(device_unbind(top[2]));
617 	ut_asserteq(NODE_COUNT + 1, dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
618 	dm_testdrv_op_count[DM_TEST_OP_UNBIND] = 0;
619 
620 	/* Try one with grandchildren */
621 	ut_assertok(uclass_get_device(UCLASS_TEST, 5, &dev));
622 	ut_asserteq_ptr(dev, top[6]);
623 	ut_assertok(device_unbind(top[5]));
624 	ut_asserteq(1 + NODE_COUNT * (1 + NODE_COUNT),
625 		    dm_testdrv_op_count[DM_TEST_OP_UNBIND]);
626 
627 	return 0;
628 }
629 DM_TEST(dm_test_children, 0);
630 
631 /* Test that pre-relocation devices work as expected */
632 static int dm_test_pre_reloc(struct dm_test_state *dms)
633 {
634 	struct udevice *dev;
635 
636 	/* The normal driver should refuse to bind before relocation */
637 	ut_asserteq(-EPERM, device_bind_by_name(dms->root, true,
638 						&driver_info_manual, &dev));
639 
640 	/* But this one is marked pre-reloc */
641 	ut_assertok(device_bind_by_name(dms->root, true,
642 					&driver_info_pre_reloc, &dev));
643 
644 	return 0;
645 }
646 DM_TEST(dm_test_pre_reloc, 0);
647 
648 static int dm_test_uclass_before_ready(struct dm_test_state *dms)
649 {
650 	struct uclass *uc;
651 
652 	ut_assertok(uclass_get(UCLASS_TEST, &uc));
653 
654 	memset(gd, '\0', sizeof(*gd));
655 	ut_asserteq_ptr(NULL, uclass_find(UCLASS_TEST));
656 
657 	return 0;
658 }
659 DM_TEST(dm_test_uclass_before_ready, 0);
660 
661 static int dm_test_uclass_devices_find(struct dm_test_state *dms)
662 {
663 	struct udevice *dev;
664 	int ret;
665 
666 	for (ret = uclass_find_first_device(UCLASS_TEST, &dev);
667 	     dev;
668 	     ret = uclass_find_next_device(&dev)) {
669 		ut_assert(!ret);
670 		ut_assert(dev);
671 	}
672 
673 	return 0;
674 }
675 DM_TEST(dm_test_uclass_devices_find, DM_TESTF_SCAN_PDATA);
676 
677 static int dm_test_uclass_devices_find_by_name(struct dm_test_state *dms)
678 {
679 	struct udevice *finddev;
680 	struct udevice *testdev;
681 	int findret, ret;
682 
683 	/*
684 	 * For each test device found in fdt like: "a-test", "b-test", etc.,
685 	 * use its name and try to find it by uclass_find_device_by_name().
686 	 * Then, on success check if:
687 	 * - current 'testdev' name is equal to the returned 'finddev' name
688 	 * - current 'testdev' pointer is equal to the returned 'finddev'
689 	 *
690 	 * We assume that, each uclass's device name is unique, so if not, then
691 	 * this will fail on checking condition: testdev == finddev, since the
692 	 * uclass_find_device_by_name(), returns the first device by given name.
693 	*/
694 	for (ret = uclass_find_first_device(UCLASS_TEST_FDT, &testdev);
695 	     testdev;
696 	     ret = uclass_find_next_device(&testdev)) {
697 		ut_assertok(ret);
698 		ut_assert(testdev);
699 
700 		findret = uclass_find_device_by_name(UCLASS_TEST_FDT,
701 						     testdev->name,
702 						     &finddev);
703 
704 		ut_assertok(findret);
705 		ut_assert(testdev);
706 		ut_asserteq_str(testdev->name, finddev->name);
707 		ut_asserteq_ptr(testdev, finddev);
708 	}
709 
710 	return 0;
711 }
712 DM_TEST(dm_test_uclass_devices_find_by_name, DM_TESTF_SCAN_FDT);
713 
714 static int dm_test_uclass_devices_get(struct dm_test_state *dms)
715 {
716 	struct udevice *dev;
717 	int ret;
718 
719 	for (ret = uclass_first_device(UCLASS_TEST, &dev);
720 	     dev;
721 	     ret = uclass_next_device(&dev)) {
722 		ut_assert(!ret);
723 		ut_assert(dev);
724 		ut_assert(device_active(dev));
725 	}
726 
727 	return 0;
728 }
729 DM_TEST(dm_test_uclass_devices_get, DM_TESTF_SCAN_PDATA);
730 
731 static int dm_test_uclass_devices_get_by_name(struct dm_test_state *dms)
732 {
733 	struct udevice *finddev;
734 	struct udevice *testdev;
735 	int ret, findret;
736 
737 	/*
738 	 * For each test device found in fdt like: "a-test", "b-test", etc.,
739 	 * use its name and try to get it by uclass_get_device_by_name().
740 	 * On success check if:
741 	 * - returned finddev' is active
742 	 * - current 'testdev' name is equal to the returned 'finddev' name
743 	 * - current 'testdev' pointer is equal to the returned 'finddev'
744 	 *
745 	 * We asserts that the 'testdev' is active on each loop entry, so we
746 	 * could be sure that the 'finddev' is activated too, but for sure
747 	 * we check it again.
748 	 *
749 	 * We assume that, each uclass's device name is unique, so if not, then
750 	 * this will fail on checking condition: testdev == finddev, since the
751 	 * uclass_get_device_by_name(), returns the first device by given name.
752 	*/
753 	for (ret = uclass_first_device(UCLASS_TEST_FDT, &testdev);
754 	     testdev;
755 	     ret = uclass_next_device(&testdev)) {
756 		ut_assertok(ret);
757 		ut_assert(testdev);
758 		ut_assert(device_active(testdev));
759 
760 		findret = uclass_get_device_by_name(UCLASS_TEST_FDT,
761 						    testdev->name,
762 						    &finddev);
763 
764 		ut_assertok(findret);
765 		ut_assert(finddev);
766 		ut_assert(device_active(finddev));
767 		ut_asserteq_str(testdev->name, finddev->name);
768 		ut_asserteq_ptr(testdev, finddev);
769 	}
770 
771 	return 0;
772 }
773 DM_TEST(dm_test_uclass_devices_get_by_name, DM_TESTF_SCAN_FDT);
774 
775 static int dm_test_device_get_uclass_id(struct dm_test_state *dms)
776 {
777 	struct udevice *dev;
778 
779 	ut_assertok(uclass_get_device(UCLASS_TEST, 0, &dev));
780 	ut_asserteq(UCLASS_TEST, device_get_uclass_id(dev));
781 
782 	return 0;
783 }
784 DM_TEST(dm_test_device_get_uclass_id, DM_TESTF_SCAN_PDATA);
785