blob: a67f5d3f982ac2ab6298549b94cb4084729a8d83 [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{
Joe Hershbergere721b882015-05-20 14:27:27 -0500119 struct dm_test_state *dms = uts->priv;
120
Simon Glass2e7d35d2014-02-26 15:59:21 -0700121 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
122 if (!dms->force_fail_alloc)
Simon Glass0fd3d912020-12-22 19:30:28 -0700123 dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
124 if (!dev_get_priv(dev))
Simon Glass2e7d35d2014-02-26 15:59:21 -0700125 return -ENOMEM;
126
127 return 0;
128}
129
Heiko Schocher54c5d082014-05-22 12:43:05 +0200130static int test_manual_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700131{
132 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
133 return 0;
134}
135
Heiko Schocher54c5d082014-05-22 12:43:05 +0200136static int test_manual_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700137{
138 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
139 return 0;
140}
141
142U_BOOT_DRIVER(test_manual_drv) = {
143 .name = "test_manual_drv",
144 .id = UCLASS_TEST,
145 .ops = &test_manual_ops,
146 .bind = test_manual_bind,
147 .probe = test_manual_probe,
148 .remove = test_manual_remove,
149 .unbind = test_manual_unbind,
150};
Simon Glass00606d72014-07-23 06:55:03 -0600151
152U_BOOT_DRIVER(test_pre_reloc_drv) = {
153 .name = "test_pre_reloc_drv",
154 .id = UCLASS_TEST,
155 .ops = &test_manual_ops,
156 .bind = test_manual_bind,
157 .probe = test_manual_probe,
158 .remove = test_manual_remove,
159 .unbind = test_manual_unbind,
160 .flags = DM_FLAG_PRE_RELOC,
161};
Stefan Roese24f927c2017-03-27 11:02:43 +0200162
163U_BOOT_DRIVER(test_act_dma_drv) = {
164 .name = "test_act_dma_drv",
165 .id = UCLASS_TEST,
166 .ops = &test_manual_ops,
167 .bind = test_manual_bind,
168 .probe = test_manual_probe,
169 .remove = test_manual_remove,
170 .unbind = test_manual_unbind,
171 .flags = DM_FLAG_ACTIVE_DMA,
172};