blob: 760731b0667580f8202553a5c0ef75d900ae5335 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass2e7d35d2014-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 Glass2e7d35d2014-02-26 15:59:21 -07007 */
8
9#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070011#include <malloc.h>
12#include <dm.h>
13#include <errno.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070014#include <asm/io.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050015#include <dm/test.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070016#include <linux/list.h>
Simon Glass0e1fad42020-07-19 10:15:37 -060017#include <test/test.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050018#include <test/ut.h>
Simon Glass2e7d35d2014-02-26 15:59:21 -070019
Joe Hershbergere721b882015-05-20 14:27:27 -050020static struct unit_test_state *uts = &global_dm_test_state;
Simon Glass2e7d35d2014-02-26 15:59:21 -070021
Heiko Schocher54c5d082014-05-22 12:43:05 +020022int test_ping(struct udevice *dev, int pingval, int *pingret)
Simon Glass2e7d35d2014-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 Schocher54c5d082014-05-22 12:43:05 +020032static int test_post_bind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070033{
Przemyslaw Marczak754e71e2015-04-15 13:07:19 +020034 struct dm_test_perdev_uc_pdata *uc_pdata;
35
Simon Glass2e7d35d2014-02-26 15:59:21 -070036 dm_testdrv_op_count[DM_TEST_OP_POST_BIND]++;
Simon Glass206d4d22015-03-25 12:21:56 -060037 ut_assert(!device_active(dev));
Simon Glass2e7d35d2014-02-26 15:59:21 -070038
Przemyslaw Marczak754e71e2015-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 Glass2e7d35d2014-02-26 15:59:21 -070046 return 0;
47}
48
Heiko Schocher54c5d082014-05-22 12:43:05 +020049static int test_pre_unbind(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070050{
51 dm_testdrv_op_count[DM_TEST_OP_PRE_UNBIND]++;
52
53 return 0;
54}
55
Simon Glass02c07b32015-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 Glass206d4d22015-03-25 12:21:56 -060062 ut_assert(device_active(dev));
Simon Glass02c07b32015-03-05 12:25:22 -070063
64 return 0;
65}
66
Heiko Schocher54c5d082014-05-22 12:43:05 +020067static int test_post_probe(struct udevice *dev)
Simon Glass2e7d35d2014-02-26 15:59:21 -070068{
Heiko Schocher54c5d082014-05-22 12:43:05 +020069 struct udevice *prev = list_entry(dev->uclass_node.prev,
70 struct udevice, uclass_node);
71
Simon Glasse564f052015-03-05 12:25:20 -070072 struct dm_test_uclass_perdev_priv *priv = dev_get_uclass_priv(dev);
Simon Glass2e7d35d2014-02-26 15:59:21 -070073 struct uclass *uc = dev->uclass;
Joe Hershbergere721b882015-05-20 14:27:27 -050074 struct dm_test_state *dms = uts->priv;
Simon Glass2e7d35d2014-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 Glasse564f052015-03-05 12:25:20 -070084 = dev_get_uclass_priv(prev);
Simon Glass2e7d35d2014-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 Schocher54c5d082014-05-22 12:43:05 +020095static int test_pre_remove(struct udevice *dev)
Simon Glass2e7d35d2014-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 Glass02c07b32015-03-05 12:25:22 -0700122 .pre_probe = test_pre_probe,
Simon Glass2e7d35d2014-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,
127 .priv_auto_alloc_size = sizeof(struct dm_test_uclass_priv),
128 .per_device_auto_alloc_size = sizeof(struct dm_test_uclass_perdev_priv),
Przemyslaw Marczak754e71e2015-04-15 13:07:19 +0200129 .per_device_platdata_auto_alloc_size =
130 sizeof(struct dm_test_perdev_uc_pdata),
Simon Glass2e7d35d2014-02-26 15:59:21 -0700131};