blob: d10af5114738cc62f3e077a7b47de5d2c90f58ae [file] [log] [blame]
Simon Glass2e7d35d2014-02-26 15:59:21 -07001/*
2 * Copyright (c) 2013 Google, Inc
3 *
4 * (C) Copyright 2012
5 * Pavel Herrmann <morpheus.ibis@gmail.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
13#include <malloc.h>
14#include <dm/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050015#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070016#include <asm/io.h>
17
18int dm_testdrv_op_count[DM_TEST_OP_COUNT];
Joe Hershbergere721b882015-05-20 14:27:27 -050019static struct unit_test_state *uts = &global_dm_test_state;
Simon Glass2e7d35d2014-02-26 15:59:21 -070020
Heiko Schocher54c5d082014-05-22 12:43:05 +020021static int testdrv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070022{
23 const struct dm_test_pdata *pdata = dev_get_platdata(dev);
24 struct dm_test_priv *priv = dev_get_priv(dev);
25
26 *pingret = pingval + pdata->ping_add;
27 priv->ping_total += *pingret;
28
29 return 0;
30}
31
32static const struct test_ops test_ops = {
33 .ping = testdrv_ping,
34};
35
Heiko Schocher54c5d082014-05-22 12:43:05 +020036static int test_bind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070037{
38 /* Private data should not be allocated */
39 ut_assert(!dev_get_priv(dev));
40
41 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
42 return 0;
43}
44
Heiko Schocher54c5d082014-05-22 12:43:05 +020045static int test_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070046{
47 struct dm_test_priv *priv = dev_get_priv(dev);
48
49 /* Private data should be allocated */
50 ut_assert(priv);
51
52 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
53 priv->ping_total += DM_TEST_START_TOTAL;
54 return 0;
55}
56
Heiko Schocher54c5d082014-05-22 12:43:05 +020057static int test_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070058{
59 /* Private data should still be allocated */
60 ut_assert(dev_get_priv(dev));
61
62 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
63 return 0;
64}
65
Heiko Schocher54c5d082014-05-22 12:43:05 +020066static int test_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070067{
68 /* Private data should not be allocated */
69 ut_assert(!dev->priv);
70
71 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
72 return 0;
73}
74
75U_BOOT_DRIVER(test_drv) = {
76 .name = "test_drv",
77 .id = UCLASS_TEST,
78 .ops = &test_ops,
79 .bind = test_bind,
80 .probe = test_probe,
81 .remove = test_remove,
82 .unbind = test_unbind,
83 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
84};
85
86U_BOOT_DRIVER(test2_drv) = {
87 .name = "test2_drv",
88 .id = UCLASS_TEST,
89 .ops = &test_ops,
90 .bind = test_bind,
91 .probe = test_probe,
92 .remove = test_remove,
93 .unbind = test_unbind,
94 .priv_auto_alloc_size = sizeof(struct dm_test_priv),
95};
96
Heiko Schocher54c5d082014-05-22 12:43:05 +020097static int test_manual_drv_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-02-26 15:59:21 -070098{
99 *pingret = pingval + 2;
100
101 return 0;
102}
103
104static const struct test_ops test_manual_ops = {
105 .ping = test_manual_drv_ping,
106};
107
Heiko Schocher54c5d082014-05-22 12:43:05 +0200108static int test_manual_bind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700109{
110 dm_testdrv_op_count[DM_TEST_OP_BIND]++;
111
112 return 0;
113}
114
Heiko Schocher54c5d082014-05-22 12:43:05 +0200115static int test_manual_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700116{
Joe Hershbergere721b882015-05-20 14:27:27 -0500117 struct dm_test_state *dms = uts->priv;
118
Simon Glass2e7d35d2014-02-26 15:59:21 -0700119 dm_testdrv_op_count[DM_TEST_OP_PROBE]++;
120 if (!dms->force_fail_alloc)
121 dev->priv = calloc(1, sizeof(struct dm_test_priv));
122 if (!dev->priv)
123 return -ENOMEM;
124
125 return 0;
126}
127
Heiko Schocher54c5d082014-05-22 12:43:05 +0200128static int test_manual_remove(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700129{
130 dm_testdrv_op_count[DM_TEST_OP_REMOVE]++;
131 return 0;
132}
133
Heiko Schocher54c5d082014-05-22 12:43:05 +0200134static int test_manual_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -0700135{
136 dm_testdrv_op_count[DM_TEST_OP_UNBIND]++;
137 return 0;
138}
139
140U_BOOT_DRIVER(test_manual_drv) = {
141 .name = "test_manual_drv",
142 .id = UCLASS_TEST,
143 .ops = &test_manual_ops,
144 .bind = test_manual_bind,
145 .probe = test_manual_probe,
146 .remove = test_manual_remove,
147 .unbind = test_manual_unbind,
148};
Simon Glass00606d72014-07-23 06:55:03 -0600149
150U_BOOT_DRIVER(test_pre_reloc_drv) = {
151 .name = "test_pre_reloc_drv",
152 .id = UCLASS_TEST,
153 .ops = &test_manual_ops,
154 .bind = test_manual_bind,
155 .probe = test_manual_probe,
156 .remove = test_manual_remove,
157 .unbind = test_manual_unbind,
158 .flags = DM_FLAG_PRE_RELOC,
159};