1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  *
5  * (C) Copyright 2012
6  * Pavel Herrmann <morpheus.ibis@gmail.com>
7  */
8 
9 #include <common.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <asm/io.h>
15 #include <dm/test.h>
16 #include <linux/list.h>
17 #include <test/test.h>
18 #include <test/ut.h>
19 
20 static struct unit_test_state *uts = &global_dm_test_state;
21 
test_ping(struct udevice * dev,int pingval,int * pingret)22 int test_ping(struct udevice *dev, int pingval, int *pingret)
23 {
24 	const struct test_ops *ops = device_get_ops(dev);
25 
26 	if (!ops->ping)
27 		return -ENOSYS;
28 
29 	return ops->ping(dev, pingval, pingret);
30 }
31 
test_post_bind(struct udevice * dev)32 static int test_post_bind(struct udevice *dev)
33 {
34 	struct dm_test_perdev_uc_pdata *uc_pdata;
35 
36 	dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
37 	ut_assert(!device_active(dev));
38 
39 	uc_pdata = dev_get_uclass_plat(dev);
40 	ut_assert(uc_pdata);
41 
42 	uc_pdata->intval1 = TEST_UC_PDATA_INTVAL1;
43 	uc_pdata->intval2 = TEST_UC_PDATA_INTVAL2;
44 	uc_pdata->intval3 = TEST_UC_PDATA_INTVAL3;
45 
46 	return 0;
47 }
48 
test_pre_unbind(struct udevice * dev)49 static int test_pre_unbind(struct udevice *dev)
50 {
51 	dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++;
52 
53 	return 0;
54 }
55 
test_pre_probe(struct udevice * dev)56 static int test_pre_probe(struct udevice *dev)
57 {
58 	struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
59 
60 	dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++;
61 	ut_assert(priv);
62 	ut_assert(device_active(dev));
63 
64 	return 0;
65 }
66 
test_post_probe(struct udevice * dev)67 static int test_post_probe(struct udevice *dev)
68 {
69 	struct udevice *prev = list_entry(dev->uclass_node.prev,
70 					    struct udevice, uclass_node);
71 
72 	struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
73 	struct uclass *uc = dev->uclass;
74 	struct dm_test_state *dms = uts->priv;
75 
76 	dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]++;
77 	ut_assert(priv);
78 	ut_assert(device_active(dev));
79 	priv->base_add = 0;
80 	if (dms->skip_post_probe)
81 		return 0;
82 	if (&prev->uclass_node != &uc->dev_head) {
83 		struct dm_test_uclass_perdev_priv *prev_uc_priv
84 				= dev_get_uclass_priv(prev);
85 		struct dm_test_pdata *pdata = dev_get_plat(prev);
86 
87 		ut_assert(pdata);
88 		ut_assert(prev_uc_priv);
89 		priv->base_add = prev_uc_priv->base_add + pdata->ping_add;
90 	}
91 
92 	return 0;
93 }
94 
test_pre_remove(struct udevice * dev)95 static int test_pre_remove(struct udevice *dev)
96 {
97 	dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]++;
98 
99 	return 0;
100 }
101 
test_init(struct uclass * uc)102 static int test_init(struct uclass *uc)
103 {
104 	dm_testdrv_op_count[DM_TEST_OP_INIT]++;
105 	ut_assert(uclass_get_priv(uc));
106 
107 	return 0;
108 }
109 
test_destroy(struct uclass * uc)110 static int test_destroy(struct uclass *uc)
111 {
112 	dm_testdrv_op_count[DM_TEST_OP_DESTROY]++;
113 
114 	return 0;
115 }
116 
117 UCLASS_DRIVER(test) = {
118 	.name		= "test",
119 	.id		= UCLASS_TEST,
120 	.post_bind	= test_post_bind,
121 	.pre_unbind	= test_pre_unbind,
122 	.pre_probe	= test_pre_probe,
123 	.post_probe	= test_post_probe,
124 	.pre_remove	= test_pre_remove,
125 	.init		= test_init,
126 	.destroy	= test_destroy,
127 	.priv_auto	= sizeof(struct dm_test_uclass_priv),
128 	.per_device_auto	= sizeof(struct dm_test_uclass_perdev_priv),
129 	.per_device_plat_auto	= sizeof(struct dm_test_perdev_uc_pdata),
130 };
131