blob: 067701734a08e20f0b6de0270d824f96d9727527 [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>
Simon Glass75c4d412020-07-19 10:15:37 -060017#include <test/test.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050018#include <test/ut.h>
Simon Glassb2c1cac2014-02-26 15:59:21 -070019
Heiko Schocherb74fcb42014-05-22 12:43:05 +020020int test_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070021{
22 const struct test_ops *ops = device_get_ops(dev);
23
24 if (!ops->ping)
25 return -ENOSYS;
26
27 return ops->ping(dev, pingval, pingret);
28}
29
Heiko Schocherb74fcb42014-05-22 12:43:05 +020030static int test_post_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070031{
Simon Glass4066d8d2021-03-07 17:35:04 -070032 struct unit_test_state *uts = test_get_state();
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
Simon Glass71fa5b42020-12-03 16:55:18 -070038 uc_pdata = dev_get_uclass_plat(dev);
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +020039 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);
Simon Glass4066d8d2021-03-07 17:35:04 -070058 struct unit_test_state *uts = test_get_state();
Simon Glass9c1f3822015-03-05 12:25:22 -070059
60 dm_testdrv_op_count[DM_TEST_OP_PRE_PROBE]++;
61 ut_assert(priv);
Simon Glass8b04e732015-03-25 12:21:56 -060062 ut_assert(device_active(dev));
Simon Glass9c1f3822015-03-05 12:25:22 -070063
64 return 0;
65}
66
Heiko Schocherb74fcb42014-05-22 12:43:05 +020067static int test_post_probe(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070068{
Simon Glass4066d8d2021-03-07 17:35:04 -070069 struct unit_test_state *uts = test_get_state();
Heiko Schocherb74fcb42014-05-22 12:43:05 +020070 struct udevice *prev = list_entry(dev->uclass_node.prev,
71 struct udevice, uclass_node);
72
Simon Glassde0977b2015-03-05 12:25:20 -070073 struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
Simon Glassb2c1cac2014-02-26 15:59:21 -070074 struct uclass *uc = dev->uclass;
75
76 dm_testdrv_op_count[DM_TEST_OP_POST_PROBE]++;
77 ut_assert(priv);
78 ut_assert(device_active(dev));
79 priv->base_add = 0;
Simon Glassb98bfbc2021-03-07 17:34:57 -070080 if (uts->skip_post_probe)
Simon Glassb2c1cac2014-02-26 15:59:21 -070081 return 0;
82 if (&prev->uclass_node != &uc->dev_head) {
83 struct dm_test_uclass_perdev_priv *prev_uc_priv
Simon Glassde0977b2015-03-05 12:25:20 -070084 = dev_get_uclass_priv(prev);
Simon Glass95588622020-12-22 19:30:28 -070085 struct dm_test_pdata *pdata = dev_get_plat(prev);
Simon Glassb2c1cac2014-02-26 15:59:21 -070086
87 ut_assert(pdata);
88 ut_assert(prev_uc_priv);
89 priv->base_add = prev_uc_priv->base_add + pdata->ping_add;
90 }
91
92 return 0;
93}
94
Heiko Schocherb74fcb42014-05-22 12:43:05 +020095static int test_pre_remove(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070096{
97 dm_testdrv_op_count[DM_TEST_OP_PRE_REMOVE]++;
98
99 return 0;
100}
101
102static int test_init(struct uclass *uc)
103{
Simon Glass4066d8d2021-03-07 17:35:04 -0700104 struct unit_test_state *uts = test_get_state();
105
Simon Glassb2c1cac2014-02-26 15:59:21 -0700106 dm_testdrv_op_count[DM_TEST_OP_INIT]++;
Simon Glass95588622020-12-22 19:30:28 -0700107 ut_assert(uclass_get_priv(uc));
Simon Glassb2c1cac2014-02-26 15:59:21 -0700108
109 return 0;
110}
111
112static int test_destroy(struct uclass *uc)
113{
114 dm_testdrv_op_count[DM_TEST_OP_DESTROY]++;
115
116 return 0;
117}
118
119UCLASS_DRIVER(test) = {
120 .name = "test",
121 .id = UCLASS_TEST,
122 .post_bind = test_post_bind,
123 .pre_unbind = test_pre_unbind,
Simon Glass9c1f3822015-03-05 12:25:22 -0700124 .pre_probe = test_pre_probe,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700125 .post_probe = test_post_probe,
126 .pre_remove = test_pre_remove,
127 .init = test_init,
128 .destroy = test_destroy,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700129 .priv_auto = sizeof(struct dm_test_uclass_priv),
130 .per_device_auto = sizeof(struct dm_test_uclass_perdev_priv),
Simon Glass33b2efb2020-12-03 16:55:22 -0700131 .per_device_plat_auto = sizeof(struct dm_test_perdev_uc_pdata),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700132};