blob: 5c074cdd09fa0c7bb95e420203dc9fd6d4863bc0 [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 Glass0e1fad42020-07-19 10:15:37 -060015#include <dm/test.h>
16#include <test/test.h>
17#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070018
19int dm_testdrv_op_count[DM_TEST_OP_COUNT];
Joe Hershbergere721b882015-05-20 14:27:27 -050020static struct unit_test_state *uts = &global_dm_test_state;
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{
24 const struct dm_test_pdata *pdata = dev_get_platdata(dev);
25 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{
39 /* Private data should not be allocated */
40 ut_assert(!dev_get_priv(dev));
41
42 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
43 return 0;
44}
45
Heiko Schocher54c5d082014-05-22 12:43:05 +020046static int test_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070047{
48 struct dm_test_priv *priv = dev_get_priv(dev);
49
50 /* Private data should be allocated */
51 ut_assert(priv);
52
53 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
54 priv->ping_total += DM_TEST_START_TOTAL;
55 return 0;
56}
57
Heiko Schocher54c5d082014-05-22 12:43:05 +020058static int test_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070059{
60 /* Private data should still be allocated */
61 ut_assert(dev_get_priv(dev));
62
63 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
64 return 0;
65}
66
Heiko Schocher54c5d082014-05-22 12:43:05 +020067static int test_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070068{
69 /* Private data should not be allocated */
70 ut_assert(!dev->priv);
71
72 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
73 return 0;
74}
75
76U_BOOT_DRIVER(test_drv) = {
77 .name = "test_drv",
78 .id = UCLASS_TEST,
79 .ops = &test_ops,
80 .bind = test_bind,
81 .probe = test_probe,
82 .remove = test_remove,
83 .unbind = test_unbind,
Simon Glass41575d82020-12-03 16:55:17 -070084 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass2e7d35d2014-02-26 15:59:21 -070085};
86
87U_BOOT_DRIVER(test2_drv) = {
88 .name = "test2_drv",
89 .id = UCLASS_TEST,
90 .ops = &test_ops,
91 .bind = test_bind,
92 .probe = test_probe,
93 .remove = test_remove,
94 .unbind = test_unbind,
Simon Glass41575d82020-12-03 16:55:17 -070095 .priv_auto = sizeof(struct dm_test_priv),
Simon Glass2e7d35d2014-02-26 15:59:21 -070096};
97
Heiko Schocher54c5d082014-05-22 12:43:05 +020098static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070099{
100 *pingret = pingval + 2;
101
102 return 0;
103}
104
105static const struct test_ops test_manual_ops = {
106 .ping = test_manual_drv_ping,
107};
108
Heiko Schocher54c5d082014-05-22 12:43:05 +0200109static int test_manual_bind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700110{
111 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
112
113 return 0;
114}
115
Heiko Schocher54c5d082014-05-22 12:43:05 +0200116static int test_manual_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700117{
Joe Hershbergere721b882015-05-20 14:27:27 -0500118 struct dm_test_state *dms = uts->priv;
119
Simon Glass2e7d35d2014-02-26 15:59:21 -0700120 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
121 if (!dms->force_fail_alloc)
122 dev->priv = calloc(1, sizeof(struct dm_test_priv));
123 if (!dev->priv)
124 return -ENOMEM;
125
126 return 0;
127}
128
Heiko Schocher54c5d082014-05-22 12:43:05 +0200129static int test_manual_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700130{
131 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
132 return 0;
133}
134
Heiko Schocher54c5d082014-05-22 12:43:05 +0200135static int test_manual_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700136{
137 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
138 return 0;
139}
140
141U_BOOT_DRIVER(test_manual_drv) = {
142 .name = "test_manual_drv",
143 .id = UCLASS_TEST,
144 .ops = &test_manual_ops,
145 .bind = test_manual_bind,
146 .probe = test_manual_probe,
147 .remove = test_manual_remove,
148 .unbind = test_manual_unbind,
149};
Simon Glass00606d72014-07-23 06:55:03 -0600150
151U_BOOT_DRIVER(test_pre_reloc_drv) = {
152 .name = "test_pre_reloc_drv",
153 .id = UCLASS_TEST,
154 .ops = &test_manual_ops,
155 .bind = test_manual_bind,
156 .probe = test_manual_probe,
157 .remove = test_manual_remove,
158 .unbind = test_manual_unbind,
159 .flags = DM_FLAG_PRE_RELOC,
160};
Stefan Roese24f927c2017-03-27 11:02:43 +0200161
162U_BOOT_DRIVER(test_act_dma_drv) = {
163 .name = "test_act_dma_drv",
164 .id = UCLASS_TEST,
165 .ops = &test_manual_ops,
166 .bind = test_manual_bind,
167 .probe = test_manual_probe,
168 .remove = test_manual_remove,
169 .unbind = test_manual_unbind,
170 .flags = DM_FLAG_ACTIVE_DMA,
171};