blob: 63dc9d335ae3baf09f03ee435bf497427001f09d [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];
Joe Hershbergere721b882015-05-20 14:27:27 -050021static struct unit_test_state *uts = &global_dm_test_state;
Simon Glass2e7d35d2014-02-26 15:59:21 -070022
Heiko Schocher54c5d082014-05-22 12:43:05 +020023static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070024{
Simon Glassc69cda22020-12-03 16:55:20 -070025 const struct dm_test_pdata *pdata = dev_get_plat(dev);
Simon Glass2e7d35d2014-02-26 15:59:21 -070026 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
34static const struct test_ops test_ops = {
35 .ping = testdrv_ping,
36};
37
Heiko Schocher54c5d082014-05-22 12:43:05 +020038static int test_bind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070039{
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
Heiko Schocher54c5d082014-05-22 12:43:05 +020047static int test_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070048{
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
Heiko Schocher54c5d082014-05-22 12:43:05 +020059static int test_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070060{
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
Heiko Schocher54c5d082014-05-22 12:43:05 +020068static int test_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070069{
70 /* Private data should not be allocated */
Simon Glass0fd3d912020-12-22 19:30:28 -070071 ut_assert(!dev_get_priv(dev));
Simon Glass2e7d35d2014-02-26 15:59:21 -070072
73 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
74 return 0;
75}
76
77U_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,
Simon Glass41575d82020-12-03 16:55:17 -070085 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass2e7d35d2014-02-26 15:59:21 -070086};
87
88U_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,
Simon Glass41575d82020-12-03 16:55:17 -070096 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass2e7d35d2014-02-26 15:59:21 -070097};
98
Heiko Schocher54c5d082014-05-22 12:43:05 +020099static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700100{
101 *pingret = pingval + 2;
102
103 return 0;
104}
105
106static const struct test_ops test_manual_ops = {
107 .ping = test_manual_drv_ping,
108};
109
Heiko Schocher54c5d082014-05-22 12:43:05 +0200110static int test_manual_bind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700111{
112 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
113
114 return 0;
115}
116
Heiko Schocher54c5d082014-05-22 12:43:05 +0200117static int test_manual_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700118{
119 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
Simon Glass4a467c62021-03-07 17:34:57 -0700120 if (!uts->force_fail_alloc)
Simon Glass0fd3d912020-12-22 19:30:28 -0700121 dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
122 if (!dev_get_priv(dev))
Simon Glass2e7d35d2014-02-26 15:59:21 -0700123 return -ENOMEM;
124
125 return 0;
126}
127
Heiko Schocher54c5d082014-05-22 12:43:05 +0200128static int test_manual_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700129{
130 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
131 return 0;
132}
133
Heiko Schocher54c5d082014-05-22 12:43:05 +0200134static int test_manual_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700135{
136 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
137 return 0;
138}
139
140U_BOOT_DRIVER(test_manual_drv) = {
141 .name = "test_manual_drv",
142 .id = UCLASS_TEST,
143 .ops = &test_manual_ops,
144 .bind = test_manual_bind,
145 .probe = test_manual_probe,
146 .remove = test_manual_remove,
147 .unbind = test_manual_unbind,
148};
Simon Glass00606d72014-07-23 06:55:03 -0600149
150U_BOOT_DRIVER(test_pre_reloc_drv) = {
151 .name = "test_pre_reloc_drv",
152 .id = UCLASS_TEST,
153 .ops = &test_manual_ops,
154 .bind = test_manual_bind,
155 .probe = test_manual_probe,
156 .remove = test_manual_remove,
157 .unbind = test_manual_unbind,
158 .flags = DM_FLAG_PRE_RELOC,
159};
Stefan Roese24f927c2017-03-27 11:02:43 +0200160
161U_BOOT_DRIVER(test_act_dma_drv) = {
162 .name = "test_act_dma_drv",
163 .id = UCLASS_TEST,
164 .ops = &test_manual_ops,
165 .bind = test_manual_bind,
166 .probe = test_manual_probe,
167 .remove = test_manual_remove,
168 .unbind = test_manual_unbind,
169 .flags = DM_FLAG_ACTIVE_DMA,
170};
Marek Vasutcc6f4c82021-01-24 14:32:46 -0700171
172U_BOOT_DRIVER(test_vital_clk_drv) = {
173 .name = "test_vital_clk_drv",
174 .id = UCLASS_TEST,
175 .ops = &test_manual_ops,
176 .bind = test_manual_bind,
177 .probe = test_manual_probe,
178 .remove = test_manual_remove,
179 .unbind = test_manual_unbind,
180 .flags = DM_FLAG_VITAL,
181};
182
183U_BOOT_DRIVER(test_act_dma_vital_clk_drv) = {
184 .name = "test_act_dma_vital_clk_drv",
185 .id = UCLASS_TEST,
186 .ops = &test_manual_ops,
187 .bind = test_manual_bind,
188 .probe = test_manual_probe,
189 .remove = test_manual_remove,
190 .unbind = test_manual_unbind,
191 .flags = DM_FLAG_VITAL | DM_FLAG_ACTIVE_DMA,
192};