blob: 851177c3018ae5561787550c0caa42c8d96fb98d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb2c1cac2014-02-26 15:59:21 -07002/*
3 * Copyright (c) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassb2c1cac2014-02-26 15:59:21 -07007 */
8
Simon Glassb2c1cac2014-02-26 15:59:21 -07009#include <dm.h>
10#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070012#include <malloc.h>
Simon Glass75c4d412020-07-19 10:15:37 -060013#include <asm/io.h>
Simon Glass95588622020-12-22 19:30:28 -070014#include <dm/device-internal.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070015#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060016#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050017#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070018
19int dm_testdrv_op_count[DM_TEST_OP_COUNT];
Simon Glassb2c1cac2014-02-26 15:59:21 -070020
Heiko Schocherb74fcb42014-05-22 12:43:05 +020021static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070022{
Simon Glassfa20e932020-12-03 16:55:20 -070023 const struct dm_test_pdata *pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -070024 struct dm_test_priv *priv = dev_get_priv(dev);
25
26 *pingret = pingval + pdata->ping_add;
27 priv->ping_total += *pingret;
28
29 return 0;
30}
31
32static const struct test_ops test_ops = {
33 .ping = testdrv_ping,
34};
35
Heiko Schocherb74fcb42014-05-22 12:43:05 +020036static int test_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070037{
Simon Glass4066d8d2021-03-07 17:35:04 -070038 struct unit_test_state *uts = test_get_state();
39
Simon Glassb2c1cac2014-02-26 15:59:21 -070040 /* 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
Heiko Schocherb74fcb42014-05-22 12:43:05 +020047static int test_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070048{
Simon Glass4066d8d2021-03-07 17:35:04 -070049 struct unit_test_state *uts = test_get_state();
Simon Glassb2c1cac2014-02-26 15:59:21 -070050 struct dm_test_priv *priv = dev_get_priv(dev);
51
52 /* Private data should be allocated */
53 ut_assert(priv);
54
55 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
56 priv->ping_total += DM_TEST_START_TOTAL;
57 return 0;
58}
59
Heiko Schocherb74fcb42014-05-22 12:43:05 +020060static int test_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070061{
Simon Glass4066d8d2021-03-07 17:35:04 -070062 struct unit_test_state *uts = test_get_state();
63
Simon Glassb2c1cac2014-02-26 15:59:21 -070064 /* Private data should still be allocated */
65 ut_assert(dev_get_priv(dev));
66
67 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
68 return 0;
69}
70
Heiko Schocherb74fcb42014-05-22 12:43:05 +020071static int test_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070072{
Simon Glass4066d8d2021-03-07 17:35:04 -070073 struct unit_test_state *uts = test_get_state();
74
Simon Glassb2c1cac2014-02-26 15:59:21 -070075 /* Private data should not be allocated */
Simon Glass95588622020-12-22 19:30:28 -070076 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -070077
78 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
79 return 0;
80}
81
82U_BOOT_DRIVER(test_drv) = {
83 .name = "test_drv",
84 .id = UCLASS_TEST,
85 .ops = &test_ops,
86 .bind = test_bind,
87 .probe = test_probe,
88 .remove = test_remove,
89 .unbind = test_unbind,
Simon Glass8a2b47f2020-12-03 16:55:17 -070090 .priv_auto = sizeof(struct dm_test_priv),
Simon Glassb2c1cac2014-02-26 15:59:21 -070091};
92
93U_BOOT_DRIVER(test2_drv) = {
94 .name = "test2_drv",
95 .id = UCLASS_TEST,
96 .ops = &test_ops,
97 .bind = test_bind,
98 .probe = test_probe,
99 .remove = test_remove,
100 .unbind = test_unbind,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700101 .priv_auto = sizeof(struct dm_test_priv),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700102};
103
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200104static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700105{
106 *pingret = pingval + 2;
107
108 return 0;
109}
110
111static const struct test_ops test_manual_ops = {
112 .ping = test_manual_drv_ping,
113};
114
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200115static int test_manual_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700116{
117 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
118
119 return 0;
120}
121
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200122static int test_manual_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700123{
Simon Glass4066d8d2021-03-07 17:35:04 -0700124 struct unit_test_state *uts = test_get_state();
125
Simon Glassb2c1cac2014-02-26 15:59:21 -0700126 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
Simon Glassb98bfbc2021-03-07 17:34:57 -0700127 if (!uts->force_fail_alloc)
Simon Glass95588622020-12-22 19:30:28 -0700128 dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
129 if (!dev_get_priv(dev))
Simon Glassb2c1cac2014-02-26 15:59:21 -0700130 return -ENOMEM;
131
132 return 0;
133}
134
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200135static int test_manual_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700136{
137 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
138 return 0;
139}
140
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200141static int test_manual_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700142{
143 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
144 return 0;
145}
146
147U_BOOT_DRIVER(test_manual_drv) = {
148 .name = "test_manual_drv",
149 .id = UCLASS_TEST,
150 .ops = &test_manual_ops,
151 .bind = test_manual_bind,
152 .probe = test_manual_probe,
153 .remove = test_manual_remove,
154 .unbind = test_manual_unbind,
155};
Simon Glassfef72b72014-07-23 06:55:03 -0600156
157U_BOOT_DRIVER(test_pre_reloc_drv) = {
158 .name = "test_pre_reloc_drv",
159 .id = UCLASS_TEST,
160 .ops = &test_manual_ops,
161 .bind = test_manual_bind,
162 .probe = test_manual_probe,
163 .remove = test_manual_remove,
164 .unbind = test_manual_unbind,
165 .flags = DM_FLAG_PRE_RELOC,
166};
Stefan Roeseeaffda72017-03-27 11:02:43 +0200167
168U_BOOT_DRIVER(test_act_dma_drv) = {
169 .name = "test_act_dma_drv",
170 .id = UCLASS_TEST,
171 .ops = &test_manual_ops,
172 .bind = test_manual_bind,
173 .probe = test_manual_probe,
174 .remove = test_manual_remove,
175 .unbind = test_manual_unbind,
176 .flags = DM_FLAG_ACTIVE_DMA,
177};
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700178
179U_BOOT_DRIVER(test_vital_clk_drv) = {
180 .name = "test_vital_clk_drv",
181 .id = UCLASS_TEST,
182 .ops = &test_manual_ops,
183 .bind = test_manual_bind,
184 .probe = test_manual_probe,
185 .remove = test_manual_remove,
186 .unbind = test_manual_unbind,
187 .flags = DM_FLAG_VITAL,
188};
189
190U_BOOT_DRIVER(test_act_dma_vital_clk_drv) = {
191 .name = "test_act_dma_vital_clk_drv",
192 .id = UCLASS_TEST,
193 .ops = &test_manual_ops,
194 .bind = test_manual_bind,
195 .probe = test_manual_probe,
196 .remove = test_manual_remove,
197 .unbind = test_manual_unbind,
198 .flags = DM_FLAG_VITAL | DM_FLAG_ACTIVE_DMA,
199};