summaryrefslogtreecommitdiff
path: root/test/dm/test-driver.c
blob: ba85fa313e8e2cb8d1b42c13d155d40272c36c71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2013 Google, Inc
 *
 * (C) Copyright 2012
 * Pavel Herrmann <morpheus.ibis@gmail.com>
 */

#include <common.h>
#include <dm.h>
#include <errno.h>
#include <log.h>
#include <malloc.h>
#include <dm/test.h>
#include <test/ut.h>
#include <asm/io.h>

int dm_testdrv_op_count[DM_TEST_OP_COUNT];
static struct unit_test_state *uts = &global_dm_test_state;

static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
{
	const struct dm_test_pdata *pdata = dev_get_platdata(dev);
	struct dm_test_priv *priv = dev_get_priv(dev);

	*pingret = pingval + pdata->ping_add;
	priv->ping_total += *pingret;

	return 0;
}

static const struct test_ops test_ops = {
	.ping = testdrv_ping,
};

static int test_bind(struct udevice *dev)
{
	/* Private data should not be allocated */
	ut_assert(!dev_get_priv(dev));

	dm_testdrv_op_count[DM_TEST_OP_BIND]++;
	return 0;
}

static int test_probe(struct udevice *dev)
{
	struct dm_test_priv *priv = dev_get_priv(dev);

	/* Private data should be allocated */
	ut_assert(priv);

	dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
	priv->ping_total += DM_TEST_START_TOTAL;
	return 0;
}

static int test_remove(struct udevice *dev)
{
	/* Private data should still be allocated */
	ut_assert(dev_get_priv(dev));

	dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
	return 0;
}

static int test_unbind(struct udevice *dev)
{
	/* Private data should not be allocated */
	ut_assert(!dev->priv);

	dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
	return 0;
}

U_BOOT_DRIVER(test_drv) = {
	.name	= "test_drv",
	.id	= UCLASS_TEST,
	.ops	= &test_ops,
	.bind	= test_bind,
	.probe	= test_probe,
	.remove	= test_remove,
	.unbind	= test_unbind,
	.priv_auto_alloc_size = sizeof(struct dm_test_priv),
};

U_BOOT_DRIVER(test2_drv) = {
	.name	= "test2_drv",
	.id	= UCLASS_TEST,
	.ops	= &test_ops,
	.bind	= test_bind,
	.probe	= test_probe,
	.remove	= test_remove,
	.unbind	= test_unbind,
	.priv_auto_alloc_size = sizeof(struct dm_test_priv),
};

static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
{
	*pingret = pingval + 2;

	return 0;
}

static const struct test_ops test_manual_ops = {
	.ping = test_manual_drv_ping,
};

static int test_manual_bind(struct udevice *dev)
{
	dm_testdrv_op_count[DM_TEST_OP_BIND]++;

	return 0;
}

static int test_manual_probe(struct udevice *dev)
{
	struct dm_test_state *dms = uts->priv;

	dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
	if (!dms->force_fail_alloc)
		dev->priv = calloc(1, sizeof(struct dm_test_priv));
	if (!dev->priv)
		return -ENOMEM;

	return 0;
}

static int test_manual_remove(struct udevice *dev)
{
	dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
	return 0;
}

static int test_manual_unbind(struct udevice *dev)
{
	dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
	return 0;
}

U_BOOT_DRIVER(test_manual_drv) = {
	.name	= "test_manual_drv",
	.id	= UCLASS_TEST,
	.ops	= &test_manual_ops,
	.bind	= test_manual_bind,
	.probe	= test_manual_probe,
	.remove	= test_manual_remove,
	.unbind	= test_manual_unbind,
};

U_BOOT_DRIVER(test_pre_reloc_drv) = {
	.name	= "test_pre_reloc_drv",
	.id	= UCLASS_TEST,
	.ops	= &test_manual_ops,
	.bind	= test_manual_bind,
	.probe	= test_manual_probe,
	.remove	= test_manual_remove,
	.unbind	= test_manual_unbind,
	.flags	= DM_FLAG_PRE_RELOC,
};

U_BOOT_DRIVER(test_act_dma_drv) = {
	.name	= "test_act_dma_drv",
	.id	= UCLASS_TEST,
	.ops	= &test_manual_ops,
	.bind	= test_manual_bind,
	.probe	= test_manual_probe,
	.remove	= test_manual_remove,
	.unbind	= test_manual_unbind,
	.flags	= DM_FLAG_ACTIVE_DMA,
};