blob: 02cb974b0f74eb68f60f554bf82789cd2bd2292c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2e7d35d2014-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 Glass2e7d35d2014-02-26 15:59:21 -07007 */
8
9#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070013#include <malloc.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070014#include <asm/io.h>
Simon Glass0fd3d912020-12-22 19:30:28 -070015#include <dm/device-internal.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060016#include <dm/test.h>
17#include <test/test.h>
18#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070019
20int dm_testdrv_op_count[DM_TEST_OP_COUNT];
Simon Glass2e7d35d2014-02-26 15:59:21 -070021
Heiko Schocher54c5d082014-05-22 12:43:05 +020022static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070023{
Simon Glassc69cda22020-12-03 16:55:20 -070024 const struct dm_test_pdata *pdata = dev_get_plat(dev);
Simon Glass2e7d35d2014-02-26 15:59:21 -070025 struct dm_test_priv *priv = dev_get_priv(dev);
26
27 *pingret = pingval + pdata->ping_add;
28 priv->ping_total += *pingret;
29
30 return 0;
31}
32
33static const struct test_ops test_ops = {
34 .ping = testdrv_ping,
35};
36
Heiko Schocher54c5d082014-05-22 12:43:05 +020037static int test_bind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070038{
Simon Glassfe806862021-03-07 17:35:04 -070039 struct unit_test_state *uts = test_get_state();
40
Simon Glass2e7d35d2014-02-26 15:59:21 -070041 /* Private data should not be allocated */
42 ut_assert(!dev_get_priv(dev));
43
44 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
45 return 0;
46}
47
Heiko Schocher54c5d082014-05-22 12:43:05 +020048static int test_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070049{
Simon Glassfe806862021-03-07 17:35:04 -070050 struct unit_test_state *uts = test_get_state();
Simon Glass2e7d35d2014-02-26 15:59:21 -070051 struct dm_test_priv *priv = dev_get_priv(dev);
52
53 /* Private data should be allocated */
54 ut_assert(priv);
55
56 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
57 priv->ping_total += DM_TEST_START_TOTAL;
58 return 0;
59}
60
Heiko Schocher54c5d082014-05-22 12:43:05 +020061static int test_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070062{
Simon Glassfe806862021-03-07 17:35:04 -070063 struct unit_test_state *uts = test_get_state();
64
Simon Glass2e7d35d2014-02-26 15:59:21 -070065 /* Private data should still be allocated */
66 ut_assert(dev_get_priv(dev));
67
68 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
69 return 0;
70}
71
Heiko Schocher54c5d082014-05-22 12:43:05 +020072static int test_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070073{
Simon Glassfe806862021-03-07 17:35:04 -070074 struct unit_test_state *uts = test_get_state();
75
Simon Glass2e7d35d2014-02-26 15:59:21 -070076 /* Private data should not be allocated */
Simon Glass0fd3d912020-12-22 19:30:28 -070077 ut_assert(!dev_get_priv(dev));
Simon Glass2e7d35d2014-02-26 15:59:21 -070078
79 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
80 return 0;
81}
82
83U_BOOT_DRIVER(test_drv) = {
84 .name = "test_drv",
85 .id = UCLASS_TEST,
86 .ops = &test_ops,
87 .bind = test_bind,
88 .probe = test_probe,
89 .remove = test_remove,
90 .unbind = test_unbind,
Simon Glass41575d82020-12-03 16:55:17 -070091 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass2e7d35d2014-02-26 15:59:21 -070092};
93
94U_BOOT_DRIVER(test2_drv) = {
95 .name = "test2_drv",
96 .id = UCLASS_TEST,
97 .ops = &test_ops,
98 .bind = test_bind,
99 .probe = test_probe,
100 .remove = test_remove,
101 .unbind = test_unbind,
Simon Glass41575d82020-12-03 16:55:17 -0700102 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass2e7d35d2014-02-26 15:59:21 -0700103};
104
Heiko Schocher54c5d082014-05-22 12:43:05 +0200105static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700106{
107 *pingret = pingval + 2;
108
109 return 0;
110}
111
112static const struct test_ops test_manual_ops = {
113 .ping = test_manual_drv_ping,
114};
115
Heiko Schocher54c5d082014-05-22 12:43:05 +0200116static int test_manual_bind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700117{
118 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
119
120 return 0;
121}
122
Heiko Schocher54c5d082014-05-22 12:43:05 +0200123static int test_manual_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700124{
Simon Glassfe806862021-03-07 17:35:04 -0700125 struct unit_test_state *uts = test_get_state();
126
Simon Glass2e7d35d2014-02-26 15:59:21 -0700127 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
Simon Glass4a467c62021-03-07 17:34:57 -0700128 if (!uts->force_fail_alloc)
Simon Glass0fd3d912020-12-22 19:30:28 -0700129 dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
130 if (!dev_get_priv(dev))
Simon Glass2e7d35d2014-02-26 15:59:21 -0700131 return -ENOMEM;
132
133 return 0;
134}
135
Heiko Schocher54c5d082014-05-22 12:43:05 +0200136static int test_manual_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700137{
138 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
139 return 0;
140}
141
Heiko Schocher54c5d082014-05-22 12:43:05 +0200142static int test_manual_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700143{
144 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
145 return 0;
146}
147
148U_BOOT_DRIVER(test_manual_drv) = {
149 .name = "test_manual_drv",
150 .id = UCLASS_TEST,
151 .ops = &test_manual_ops,
152 .bind = test_manual_bind,
153 .probe = test_manual_probe,
154 .remove = test_manual_remove,
155 .unbind = test_manual_unbind,
156};
Simon Glass00606d72014-07-23 06:55:03 -0600157
158U_BOOT_DRIVER(test_pre_reloc_drv) = {
159 .name = "test_pre_reloc_drv",
160 .id = UCLASS_TEST,
161 .ops = &test_manual_ops,
162 .bind = test_manual_bind,
163 .probe = test_manual_probe,
164 .remove = test_manual_remove,
165 .unbind = test_manual_unbind,
166 .flags = DM_FLAG_PRE_RELOC,
167};
Stefan Roese24f927c2017-03-27 11:02:43 +0200168
169U_BOOT_DRIVER(test_act_dma_drv) = {
170 .name = "test_act_dma_drv",
171 .id = UCLASS_TEST,
172 .ops = &test_manual_ops,
173 .bind = test_manual_bind,
174 .probe = test_manual_probe,
175 .remove = test_manual_remove,
176 .unbind = test_manual_unbind,
177 .flags = DM_FLAG_ACTIVE_DMA,
178};
Marek Vasutcc6f4c82021-01-24 14:32:46 -0700179
180U_BOOT_DRIVER(test_vital_clk_drv) = {
181 .name = "test_vital_clk_drv",
182 .id = UCLASS_TEST,
183 .ops = &test_manual_ops,
184 .bind = test_manual_bind,
185 .probe = test_manual_probe,
186 .remove = test_manual_remove,
187 .unbind = test_manual_unbind,
188 .flags = DM_FLAG_VITAL,
189};
190
191U_BOOT_DRIVER(test_act_dma_vital_clk_drv) = {
192 .name = "test_act_dma_vital_clk_drv",
193 .id = UCLASS_TEST,
194 .ops = &test_manual_ops,
195 .bind = test_manual_bind,
196 .probe = test_manual_probe,
197 .remove = test_manual_remove,
198 .unbind = test_manual_unbind,
199 .flags = DM_FLAG_VITAL | DM_FLAG_ACTIVE_DMA,
200};