blob: abb5b7115a33013f9743688b8d849e0696622fb1 [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>
12#include <malloc.h>
13#include <dm/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050014#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070015#include <asm/io.h>
16
17int dm_testdrv_op_count[DM_TEST_OP_COUNT];
Joe Hershberger3a77be52015-05-20 14:27:27 -050018static struct unit_test_state *uts = &global_dm_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070019
Heiko Schocherb74fcb42014-05-22 12:43:05 +020020static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070021{
22 const struct dm_test_pdata *pdata = dev_get_platdata(dev);
23 struct dm_test_priv *priv = dev_get_priv(dev);
24
25 *pingret = pingval + pdata->ping_add;
26 priv->ping_total += *pingret;
27
28 return 0;
29}
30
31static const struct test_ops test_ops = {
32 .ping = testdrv_ping,
33};
34
Heiko Schocherb74fcb42014-05-22 12:43:05 +020035static int test_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070036{
37 /* Private data should not be allocated */
38 ut_assert(!dev_get_priv(dev));
39
40 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
41 return 0;
42}
43
Heiko Schocherb74fcb42014-05-22 12:43:05 +020044static int test_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070045{
46 struct dm_test_priv *priv = dev_get_priv(dev);
47
48 /* Private data should be allocated */
49 ut_assert(priv);
50
51 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
52 priv->ping_total += DM_TEST_START_TOTAL;
53 return 0;
54}
55
Heiko Schocherb74fcb42014-05-22 12:43:05 +020056static int test_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070057{
58 /* Private data should still be allocated */
59 ut_assert(dev_get_priv(dev));
60
61 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
62 return 0;
63}
64
Heiko Schocherb74fcb42014-05-22 12:43:05 +020065static int test_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070066{
67 /* Private data should not be allocated */
68 ut_assert(!dev->priv);
69
70 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
71 return 0;
72}
73
74U_BOOT_DRIVER(test_drv) = {
75 .name = "test_drv",
76 .id = UCLASS_TEST,
77 .ops = &test_ops,
78 .bind = test_bind,
79 .probe = test_probe,
80 .remove = test_remove,
81 .unbind = test_unbind,
82 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
83};
84
85U_BOOT_DRIVER(test2_drv) = {
86 .name = "test2_drv",
87 .id = UCLASS_TEST,
88 .ops = &test_ops,
89 .bind = test_bind,
90 .probe = test_probe,
91 .remove = test_remove,
92 .unbind = test_unbind,
93 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
94};
95
Heiko Schocherb74fcb42014-05-22 12:43:05 +020096static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070097{
98 *pingret = pingval + 2;
99
100 return 0;
101}
102
103static const struct test_ops test_manual_ops = {
104 .ping = test_manual_drv_ping,
105};
106
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200107static int test_manual_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700108{
109 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
110
111 return 0;
112}
113
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200114static int test_manual_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700115{
Joe Hershberger3a77be52015-05-20 14:27:27 -0500116 struct dm_test_state *dms = uts->priv;
117
Simon Glassb2c1cac2014-02-26 15:59:21 -0700118 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
119 if (!dms->force_fail_alloc)
120 dev->priv = calloc(1, sizeof(struct dm_test_priv));
121 if (!dev->priv)
122 return -ENOMEM;
123
124 return 0;
125}
126
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200127static int test_manual_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700128{
129 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
130 return 0;
131}
132
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200133static int test_manual_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -0700134{
135 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
136 return 0;
137}
138
139U_BOOT_DRIVER(test_manual_drv) = {
140 .name = "test_manual_drv",
141 .id = UCLASS_TEST,
142 .ops = &test_manual_ops,
143 .bind = test_manual_bind,
144 .probe = test_manual_probe,
145 .remove = test_manual_remove,
146 .unbind = test_manual_unbind,
147};
Simon Glassfef72b72014-07-23 06:55:03 -0600148
149U_BOOT_DRIVER(test_pre_reloc_drv) = {
150 .name = "test_pre_reloc_drv",
151 .id = UCLASS_TEST,
152 .ops = &test_manual_ops,
153 .bind = test_manual_bind,
154 .probe = test_manual_probe,
155 .remove = test_manual_remove,
156 .unbind = test_manual_unbind,
157 .flags = DM_FLAG_PRE_RELOC,
158};
Stefan Roeseeaffda72017-03-27 11:02:43 +0200159
160U_BOOT_DRIVER(test_act_dma_drv) = {
161 .name = "test_act_dma_drv",
162 .id = UCLASS_TEST,
163 .ops = &test_manual_ops,
164 .bind = test_manual_bind,
165 .probe = test_manual_probe,
166 .remove = test_manual_remove,
167 .unbind = test_manual_unbind,
168 .flags = DM_FLAG_ACTIVE_DMA,
169};