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