blob: 94c2753ed959f21190a3247e46c80272358180ae [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
Joe Hershberger3a77be52015-05-20 14:27:27 -050020static struct unit_test_state *uts = &global_dm_test_state;
Simon Glassb2c1cac2014-02-26 15:59:21 -070021
Heiko Schocherb74fcb42014-05-22 12:43:05 +020022int test_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glassb2c1cac2014-02-26 15:59:21 -070023{
24 const struct test_ops *ops = device_get_ops(dev);
25
26 if (!ops->ping)
27 return -ENOSYS;
28
29 return ops->ping(dev, pingval, pingret);
30}
31
Heiko Schocherb74fcb42014-05-22 12:43:05 +020032static int test_post_bind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070033{
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +020034 struct dm_test_perdev_uc_pdata *uc_pdata;
35
Simon Glassb2c1cac2014-02-26 15:59:21 -070036 dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
Simon Glass8b04e732015-03-25 12:21:56 -060037 ut_assert(!device_active(dev));
Simon Glassb2c1cac2014-02-26 15:59:21 -070038
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +020039 uc_pdata = dev_get_uclass_platdata(dev);
40 ut_assert(uc_pdata);
41
42 uc_pdata->intval1 = TEST_UC_PDATA_INTVAL1;
43 uc_pdata->intval2 = TEST_UC_PDATA_INTVAL2;
44 uc_pdata->intval3 = TEST_UC_PDATA_INTVAL3;
45
Simon Glassb2c1cac2014-02-26 15:59:21 -070046 return 0;
47}
48
Heiko Schocherb74fcb42014-05-22 12:43:05 +020049static int test_pre_unbind(struct udevice *dev)
Simon Glassb2c1cac2014-02-26 15:59:21 -070050{
51 dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++;
52
53 return 0;
54}
55
Simon Glass9c1f3822015-03-05 12:25:22 -070056static int test_pre_probe(struct udevice *dev)
57{
58 struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
59
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{
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;
Joe Hershberger3a77be52015-05-20 14:27:27 -050074 struct dm_test_state *dms = uts->priv;
Simon Glassb2c1cac2014-02-26 15:59:21 -070075
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;
80 if (dms->skip_post_probe)
81 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 Glassb2c1cac2014-02-26 15:59:21 -070085 struct dm_test_pdata *pdata = prev->platdata;
86
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{
104 dm_testdrv_op_count[DM_TEST_OP_INIT]++;
105 ut_assert(uc->priv);
106
107 return 0;
108}
109
110static int test_destroy(struct uclass *uc)
111{
112 dm_testdrv_op_count[DM_TEST_OP_DESTROY]++;
113
114 return 0;
115}
116
117UCLASS_DRIVER(test) = {
118 .name = "test",
119 .id = UCLASS_TEST,
120 .post_bind = test_post_bind,
121 .pre_unbind = test_pre_unbind,
Simon Glass9c1f3822015-03-05 12:25:22 -0700122 .pre_probe = test_pre_probe,
Simon Glassb2c1cac2014-02-26 15:59:21 -0700123 .post_probe = test_post_probe,
124 .pre_remove = test_pre_remove,
125 .init = test_init,
126 .destroy = test_destroy,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700127 .priv_auto = sizeof(struct dm_test_uclass_priv),
128 .per_device_auto = sizeof(struct dm_test_uclass_perdev_priv),
129 .per_device_platdata_auto =
Przemyslaw Marczak34cbe312015-04-15 13:07:19 +0200130 sizeof(struct dm_test_perdev_uc_pdata),
Simon Glassb2c1cac2014-02-26 15:59:21 -0700131};