blob: 02cb974b0f74eb68f60f554bf82789cd2bd2292c [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
9#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070013#include <malloc.h>
Simon Glass75c4d412020-07-19 10:15:37 -060014#include <asm/io.h>
Simon Glass95588622020-12-22 19:30:28 -070015#include <dm/device-internal.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070016#include <dm/test.h>
Simon Glass75c4d412020-07-19 10:15:37 -060017#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050018#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070019
20int dm_testdrv_op_count[DM_TEST_OP_COUNT];
Simon Glassb2c1cac2014-02-26 15:59:21 -070021
Heiko Schocherb74fcb42014-05-22 12:43:05 +020022static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070023{
Simon Glassfa20e932020-12-03 16:55:20 -070024 const struct dm_test_pdata *pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +020037static int test_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070038{
Simon Glass4066d8d2021-03-07 17:35:04 -070039 struct unit_test_state *uts = test_get_state();
40
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +020048static int test_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070049{
Simon Glass4066d8d2021-03-07 17:35:04 -070050 struct unit_test_state *uts = test_get_state();
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +020061static int test_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070062{
Simon Glass4066d8d2021-03-07 17:35:04 -070063 struct unit_test_state *uts = test_get_state();
64
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +020072static int test_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070073{
Simon Glass4066d8d2021-03-07 17:35:04 -070074 struct unit_test_state *uts = test_get_state();
75
Simon Glassb2c1cac2014-02-26 15:59:21 -070076 /* Private data should not be allocated */
Simon Glass95588622020-12-22 19:30:28 -070077 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-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 Glass8a2b47f2020-12-03 16:55:17 -070091 .priv_auto = sizeof(struct dm_test_priv),
Simon Glassb2c1cac2014-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 Glass8a2b47f2020-12-03 16:55:17 -0700102 .priv_auto = sizeof(struct dm_test_priv),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700103};
104
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200105static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +0200116static int test_manual_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700117{
118 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
119
120 return 0;
121}
122
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200123static int test_manual_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700124{
Simon Glass4066d8d2021-03-07 17:35:04 -0700125 struct unit_test_state *uts = test_get_state();
126
Simon Glassb2c1cac2014-02-26 15:59:21 -0700127 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
Simon Glassb98bfbc2021-03-07 17:34:57 -0700128 if (!uts->force_fail_alloc)
Simon Glass95588622020-12-22 19:30:28 -0700129 dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
130 if (!dev_get_priv(dev))
Simon Glassb2c1cac2014-02-26 15:59:21 -0700131 return -ENOMEM;
132
133 return 0;
134}
135
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200136static int test_manual_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700137{
138 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
139 return 0;
140}
141
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200142static int test_manual_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-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 Glassfef72b72014-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 Roeseeaffda72017-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 Vasutabbdbbd2021-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};