blob: a67f5d3f982ac2ab6298549b94cb4084729a8d83 [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];
Joe Hershberger3a77be52015-05-20 14:27:27 -050021static struct unit_test_state *uts = &global_dm_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070022
Heiko Schocherb74fcb42014-05-22 12:43:05 +020023static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070024{
Simon Glassfa20e932020-12-03 16:55:20 -070025 const struct dm_test_pdata *pdata = dev_get_plat(dev);
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +020038static int test_bind(struct udevice *dev)
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +020047static int test_probe(struct udevice *dev)
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +020059static int test_remove(struct udevice *dev)
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +020068static int test_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070069{
70 /* Private data should not be allocated */
Simon Glass95588622020-12-22 19:30:28 -070071 ut_assert(!dev_get_priv(dev));
Simon Glassb2c1cac2014-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 Glass8a2b47f2020-12-03 16:55:17 -070085 .priv_auto = sizeof(struct dm_test_priv),
Simon Glassb2c1cac2014-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 Glass8a2b47f2020-12-03 16:55:17 -070096 .priv_auto = sizeof(struct dm_test_priv),
Simon Glassb2c1cac2014-02-26 15:59:21 -070097};
98
Heiko Schocherb74fcb42014-05-22 12:43:05 +020099static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-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 Schocherb74fcb42014-05-22 12:43:05 +0200110static int test_manual_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700111{
112 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
113
114 return 0;
115}
116
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200117static int test_manual_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700118{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500119 struct dm_test_state *dms = uts->priv;
120
Simon Glassb2c1cac2014-02-26 15:59:21 -0700121 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
122 if (!dms->force_fail_alloc)
Simon Glass95588622020-12-22 19:30:28 -0700123 dev_set_priv(dev, calloc(1, sizeof(struct dm_test_priv)));
124 if (!dev_get_priv(dev))
Simon Glassb2c1cac2014-02-26 15:59:21 -0700125 return -ENOMEM;
126
127 return 0;
128}
129
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200130static int test_manual_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700131{
132 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
133 return 0;
134}
135
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200136static int test_manual_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-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 Glassfef72b72014-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 Roeseeaffda72017-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};