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 <dm.h>
11 #include <errno.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <asm/io.h>
15 #include <dm/device-internal.h>
16 #include <dm/test.h>
17 #include <test/test.h>
18 #include <test/ut.h>
19 
20 int dm_testdrv_op_count[DM_TEST_OP_COUNT];
21 static struct unit_test_state *uts = &global_dm_test_state;
22 
testdrv_ping(struct udevice * dev,int pingval,int * pingret)23 static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
24 {
25 	const struct dm_test_pdata *pdata = dev_get_plat(dev);
26 	struct dm_test_priv *priv = dev_get_priv(dev);
27 
28 	*pingret = pingval + pdata->ping_add;
29 	priv->ping_total += *pingret;
30 
31 	return 0;
32 }
33 
34 static const struct test_ops test_ops = {
35 	.ping = testdrv_ping,
36 };
37 
test_bind(struct udevice * dev)38 static int test_bind(struct udevice *dev)
39 {
40 	/* Private data should not be allocated */
41 	ut_assert(!dev_get_priv(dev));
42 
43 	dm_testdrv_op_count[DM_TEST_OP_BIND]++;
44 	return 0;
45 }
46 
test_probe(struct udevice * dev)47 static int test_probe(struct udevice *dev)
48 {
49 	struct dm_test_priv *priv = dev_get_priv(dev);
50 
51 	/* Private data should be allocated */
52 	ut_assert(priv);
53 
54 	dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
55 	priv->ping_total += DM_TEST_START_TOTAL;
56 	return 0;
57 }
58 
test_remove(struct udevice * dev)59 static int test_remove(struct udevice *dev)
60 {
61 	/* Private data should still be allocated */
62 	ut_assert(dev_get_priv(dev));
63 
64 	dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
65 	return 0;
66 }
67 
test_unbind(struct udevice * dev)68 static int test_unbind(struct udevice *dev)
69 {
70 	/* Private data should not be allocated */
71 	ut_assert(!dev_get_priv(dev));
72 
73 	dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
74 	return 0;
75 }
76 
77 U_BOOT_DRIVER(test_drv) = {
78 	.name	= "test_drv",
79 	.id	= UCLASS_TEST,
80 	.ops	= &test_ops,
81 	.bind	= test_bind,
82 	.probe	= test_probe,
83 	.remove	= test_remove,
84 	.unbind	= test_unbind,
85 	.priv_auto	= sizeof(struct dm_test_priv),
86 };
87 
88 U_BOOT_DRIVER(test2_drv) = {
89 	.name	= "test2_drv",
90 	.id	= UCLASS_TEST,
91 	.ops	= &test_ops,
92 	.bind	= test_bind,
93 	.probe	= test_probe,
94 	.remove	= test_remove,
95 	.unbind	= test_unbind,
96 	.priv_auto	= sizeof(struct dm_test_priv),
97 };
98 
test_manual_drv_ping(struct udevice * dev,int pingval,int * pingret)99 static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
100 {
101 	*pingret = pingval + 2;
102 
103 	return 0;
104 }
105 
106 static const struct test_ops test_manual_ops = {
107 	.ping = test_manual_drv_ping,
108 };
109 
test_manual_bind(struct udevice * dev)110 static int test_manual_bind(struct udevice *dev)
111 {
112 	dm_testdrv_op_count[DM_TEST_OP_BIND]++;
113 
114 	return 0;
115 }
116 
test_manual_probe(struct udevice * dev)117 static int test_manual_probe(struct udevice *dev)
118 {
119 	struct dm_test_state *dms = uts->priv;
120 
121 	dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
122 	if (!dms->force_fail_alloc)
123 		dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
124 	if (!dev_get_priv(dev))
125 		return -ENOMEM;
126 
127 	return 0;
128 }
129 
test_manual_remove(struct udevice * dev)130 static int test_manual_remove(struct udevice *dev)
131 {
132 	dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
133 	return 0;
134 }
135 
test_manual_unbind(struct udevice * dev)136 static int test_manual_unbind(struct udevice *dev)
137 {
138 	dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
139 	return 0;
140 }
141 
142 U_BOOT_DRIVER(test_manual_drv) = {
143 	.name	= "test_manual_drv",
144 	.id	= UCLASS_TEST,
145 	.ops	= &test_manual_ops,
146 	.bind	= test_manual_bind,
147 	.probe	= test_manual_probe,
148 	.remove	= test_manual_remove,
149 	.unbind	= test_manual_unbind,
150 };
151 
152 U_BOOT_DRIVER(test_pre_reloc_drv) = {
153 	.name	= "test_pre_reloc_drv",
154 	.id	= UCLASS_TEST,
155 	.ops	= &test_manual_ops,
156 	.bind	= test_manual_bind,
157 	.probe	= test_manual_probe,
158 	.remove	= test_manual_remove,
159 	.unbind	= test_manual_unbind,
160 	.flags	= DM_FLAG_PRE_RELOC,
161 };
162 
163 U_BOOT_DRIVER(test_act_dma_drv) = {
164 	.name	= "test_act_dma_drv",
165 	.id	= UCLASS_TEST,
166 	.ops	= &test_manual_ops,
167 	.bind	= test_manual_bind,
168 	.probe	= test_manual_probe,
169 	.remove	= test_manual_remove,
170 	.unbind	= test_manual_unbind,
171 	.flags	= DM_FLAG_ACTIVE_DMA,
172 };
173 
174 U_BOOT_DRIVER(test_vital_clk_drv) = {
175 	.name	= "test_vital_clk_drv",
176 	.id	= UCLASS_TEST,
177 	.ops	= &test_manual_ops,
178 	.bind	= test_manual_bind,
179 	.probe	= test_manual_probe,
180 	.remove	= test_manual_remove,
181 	.unbind	= test_manual_unbind,
182 	.flags	= DM_FLAG_VITAL,
183 };
184 
185 U_BOOT_DRIVER(test_act_dma_vital_clk_drv) = {
186 	.name	= "test_act_dma_vital_clk_drv",
187 	.id	= UCLASS_TEST,
188 	.ops	= &test_manual_ops,
189 	.bind	= test_manual_bind,
190 	.probe	= test_manual_probe,
191 	.remove	= test_manual_remove,
192 	.unbind	= test_manual_unbind,
193 	.flags	= DM_FLAG_VITAL | DM_FLAG_ACTIVE_DMA,
194 };
195