blob: b6d629a285ce9d13547518b919a4a943d71dbfe1 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070011#include <malloc.h>
12#include <dm.h>
13#include <errno.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070014#include <asm/io.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050015#include <dm/test.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070016#include <linux/list.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050017#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070018
Joe Hershberger3a77be52015-05-20 14:27:27 -050019static struct unit_test_state *uts = &global_dm_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070020
Heiko Schocherb74fcb42014-05-22 12:43:05 +020021int test_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070022{
23 const struct test_ops *ops = device_get_ops(dev);
24
25 if (!ops->ping)
26 return -ENOSYS;
27
28 return ops->ping(dev, pingval, pingret);
29}
30
Heiko Schocherb74fcb42014-05-22 12:43:05 +020031static int test_post_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070032{
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +020033 struct dm_test_perdev_uc_pdata *uc_pdata;
34
Simon Glassb2c1cac2014-02-26 15:59:21 -070035 dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
Simon Glass8b04e732015-03-25 12:21:56 -060036 ut_assert(!device_active(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -070037
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +020038 uc_pdata = dev_get_uclass_platdata(dev);
39 ut_assert(uc_pdata);
40
41 uc_pdata->intval1 = TEST_UC_PDATA_INTVAL1;
42 uc_pdata->intval2 = TEST_UC_PDATA_INTVAL2;
43 uc_pdata->intval3 = TEST_UC_PDATA_INTVAL3;
44
Simon Glassb2c1cac2014-02-26 15:59:21 -070045 return 0;
46}
47
Heiko Schocherb74fcb42014-05-22 12:43:05 +020048static int test_pre_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070049{
50 dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++;
51
52 return 0;
53}
54
Simon Glass9c1f3822015-03-05 12:25:22 -070055static int test_pre_probe(struct udevice *dev)
56{
57 struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
58
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{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020068 struct udevice *prev = list_entry(dev->uclass_node.prev,
69 struct udevice, uclass_node);
70
Simon Glassde0977b2015-03-05 12:25:20 -070071 struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -070072 struct uclass *uc = dev->uclass;
Joe Hershberger3a77be52015-05-20 14:27:27 -050073 struct dm_test_state *dms = uts->priv;
Simon Glassb2c1cac2014-02-26 15:59:21 -070074
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;
79 if (dms->skip_post_probe)
80 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 Glassb2c1cac2014-02-26 15:59:21 -070084 struct dm_test_pdata *pdata = prev->platdata;
85
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{
103 dm_testdrv_op_count[DM_TEST_OP_INIT]++;
104 ut_assert(uc->priv);
105
106 return 0;
107}
108
109static int test_destroy(struct uclass *uc)
110{
111 dm_testdrv_op_count[DM_TEST_OP_DESTROY]++;
112
113 return 0;
114}
115
116UCLASS_DRIVER(test) = {
117 .name = "test",
118 .id = UCLASS_TEST,
119 .post_bind = test_post_bind,
120 .pre_unbind = test_pre_unbind,
Simon Glass9c1f3822015-03-05 12:25:22 -0700121 .pre_probe = test_pre_probe,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700122 .post_probe = test_post_probe,
123 .pre_remove = test_pre_remove,
124 .init = test_init,
125 .destroy = test_destroy,
126 .priv_auto_alloc_size = sizeof(struct dm_test_uclass_priv),
127 .per_device_auto_alloc_size = sizeof(struct dm_test_uclass_perdev_priv),
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200128 .per_device_platdata_auto_alloc_size =
129 sizeof(struct dm_test_perdev_uc_pdata),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700130};