blob: 9a80cc63667be95bc0f1cda5e618451f984824d8 [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
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070010#include <malloc.h>
11#include <dm.h>
12#include <errno.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070013#include <asm/io.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050014#include <dm/test.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070015#include <linux/list.h>
Simon Glass75c4d412020-07-19 10:15:37 -060016#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050017#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070018
Heiko Schocherb74fcb42014-05-22 12:43:05 +020019int test_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070020{
21 const struct test_ops *ops = device_get_ops(dev);
22
23 if (!ops->ping)
24 return -ENOSYS;
25
26 return ops->ping(dev, pingval, pingret);
27}
28
Heiko Schocherb74fcb42014-05-22 12:43:05 +020029static int test_post_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070030{
Simon Glass4066d8d2021-03-07 17:35:04 -070031 struct unit_test_state *uts = test_get_state();
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +020032 struct dm_test_perdev_uc_pdata *uc_pdata;
33
Simon Glassb2c1cac2014-02-26 15:59:21 -070034 dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
Simon Glass8b04e732015-03-25 12:21:56 -060035 ut_assert(!device_active(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -070036
Simon Glass71fa5b42020-12-03 16:55:18 -070037 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +020038 ut_assert(uc_pdata);
39
40 uc_pdata->intval1 = TEST_UC_PDATA_INTVAL1;
41 uc_pdata->intval2 = TEST_UC_PDATA_INTVAL2;
42 uc_pdata->intval3 = TEST_UC_PDATA_INTVAL3;
43
Simon Glassb2c1cac2014-02-26 15:59:21 -070044 return 0;
45}
46
Heiko Schocherb74fcb42014-05-22 12:43:05 +020047static int test_pre_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070048{
49 dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++;
50
51 return 0;
52}
53
Simon Glass9c1f3822015-03-05 12:25:22 -070054static int test_pre_probe(struct udevice *dev)
55{
56 struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
Simon Glass4066d8d2021-03-07 17:35:04 -070057 struct unit_test_state *uts = test_get_state();
Simon Glass9c1f3822015-03-05 12:25:22 -070058
59 dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++;
60 ut_assert(priv);
Simon Glass8b04e732015-03-25 12:21:56 -060061 ut_assert(device_active(dev));
Simon Glass9c1f3822015-03-05 12:25:22 -070062
63 return 0;
64}
65
Heiko Schocherb74fcb42014-05-22 12:43:05 +020066static int test_post_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070067{
Simon Glass4066d8d2021-03-07 17:35:04 -070068 struct unit_test_state *uts = test_get_state();
Heiko Schocherb74fcb42014-05-22 12:43:05 +020069 struct udevice *prev = list_entry(dev->uclass_node.prev,
70 struct udevice, uclass_node);
71
Simon Glassde0977b2015-03-05 12:25:20 -070072 struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -070073 struct uclass *uc = dev->uclass;
74
75 dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]++;
76 ut_assert(priv);
77 ut_assert(device_active(dev));
78 priv->base_add = 0;
Simon Glassb98bfbc2021-03-07 17:34:57 -070079 if (uts->skip_post_probe)
Simon Glassb2c1cac2014-02-26 15:59:21 -070080 return 0;
81 if (&prev->uclass_node != &uc->dev_head) {
82 struct dm_test_uclass_perdev_priv *prev_uc_priv
Simon Glassde0977b2015-03-05 12:25:20 -070083 = dev_get_uclass_priv(prev);
Simon Glass95588622020-12-22 19:30:28 -070084 struct dm_test_pdata *pdata = dev_get_plat(prev);
Simon Glassb2c1cac2014-02-26 15:59:21 -070085
86 ut_assert(pdata);
87 ut_assert(prev_uc_priv);
88 priv->base_add = prev_uc_priv->base_add + pdata->ping_add;
89 }
90
91 return 0;
92}
93
Heiko Schocherb74fcb42014-05-22 12:43:05 +020094static int test_pre_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070095{
96 dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]++;
97
98 return 0;
99}
100
101static int test_init(struct uclass *uc)
102{
Simon Glass4066d8d2021-03-07 17:35:04 -0700103 struct unit_test_state *uts = test_get_state();
104
Simon Glassb2c1cac2014-02-26 15:59:21 -0700105 dm_testdrv_op_count[DM_TEST_OP_INIT]++;
Simon Glass95588622020-12-22 19:30:28 -0700106 ut_assert(uclass_get_priv(uc));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700107
108 return 0;
109}
110
111static int test_destroy(struct uclass *uc)
112{
113 dm_testdrv_op_count[DM_TEST_OP_DESTROY]++;
114
115 return 0;
116}
117
118UCLASS_DRIVER(test) = {
119 .name = "test",
120 .id = UCLASS_TEST,
121 .post_bind = test_post_bind,
122 .pre_unbind = test_pre_unbind,
Simon Glass9c1f3822015-03-05 12:25:22 -0700123 .pre_probe = test_pre_probe,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700124 .post_probe = test_post_probe,
125 .pre_remove = test_pre_remove,
126 .init = test_init,
127 .destroy = test_destroy,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700128 .priv_auto = sizeof(struct dm_test_uclass_priv),
129 .per_device_auto = sizeof(struct dm_test_uclass_perdev_priv),
Simon Glass33b2efb2020-12-03 16:55:22 -0700130 .per_device_plat_auto = sizeof(struct dm_test_perdev_uc_pdata),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700131};