summaryrefslogtreecommitdiff
path: root/test/dm/clk.c
blob: 003b78934f35d75fb64d01b40c4c0e221f88c20d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2015 Google, Inc
 */

#include <common.h>
#include <clk.h>
#include <dm.h>
#include <malloc.h>
#include <asm/clk.h>
#include <dm/test.h>
#include <dm/device-internal.h>
#include <linux/err.h>
#include <test/ut.h>

/* Base test of the clk uclass */
static int dm_test_clk_base(struct unit_test_state *uts)
{
	struct udevice *dev;
	struct clk clk_method1;
	struct clk clk_method2;

	/* Get the device using the clk device */
	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", &dev));

	/* Get the same clk port in 2 different ways and compare */
	ut_assertok(clk_get_by_index(dev, 1, &clk_method1));
	ut_assertok(clk_get_by_index_nodev(dev_ofnode(dev), 1, &clk_method2));
	ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true);
	ut_asserteq(clk_method1.id, clk_method2.id);

	return 0;
}

DM_TEST(dm_test_clk_base, DM_TESTF_SCAN_FDT);

static int dm_test_clk(struct unit_test_state *uts)
{
	struct udevice *dev_fixed, *dev_fixed_factor, *dev_clk, *dev_test;
	ulong rate;

	ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed",
					      &dev_fixed));

	ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed-factor",
					      &dev_fixed_factor));

	ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
					      &dev_clk));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
	ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));

	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
					      &dev_test));
	ut_assertok(sandbox_clk_test_get(dev_test));
	ut_assertok(sandbox_clk_test_devm_get(dev_test));
	ut_assertok(sandbox_clk_test_valid(dev_test));

	ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
						 SANDBOX_CLK_TEST_ID_DEVM_NULL));
	ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
						 SANDBOX_CLK_TEST_ID_DEVM_NULL,
						 0));
	ut_asserteq(0, sandbox_clk_test_enable(dev_test,
					       SANDBOX_CLK_TEST_ID_DEVM_NULL));
	ut_asserteq(0, sandbox_clk_test_disable(dev_test,
						SANDBOX_CLK_TEST_ID_DEVM_NULL));

	ut_asserteq(1234,
		    sandbox_clk_test_get_rate(dev_test,
					      SANDBOX_CLK_TEST_ID_FIXED));
	ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
						 SANDBOX_CLK_TEST_ID_SPI));
	ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
						 SANDBOX_CLK_TEST_ID_I2C));
	ut_asserteq(321, sandbox_clk_test_get_rate(dev_test,
						   SANDBOX_CLK_TEST_ID_DEVM1));
	ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
						 SANDBOX_CLK_TEST_ID_DEVM2));

	rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED,
					 12345);
	ut_assert(IS_ERR_VALUE(rate));
	rate = sandbox_clk_test_get_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED);
	ut_asserteq(1234, rate);

	ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
						 SANDBOX_CLK_TEST_ID_SPI,
						 1000));
	ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
						 SANDBOX_CLK_TEST_ID_I2C,
						 2000));

	ut_asserteq(1000, sandbox_clk_test_get_rate(dev_test,
						    SANDBOX_CLK_TEST_ID_SPI));
	ut_asserteq(2000, sandbox_clk_test_get_rate(dev_test,
						    SANDBOX_CLK_TEST_ID_I2C));

	ut_asserteq(1000, sandbox_clk_test_set_rate(dev_test,
						    SANDBOX_CLK_TEST_ID_SPI,
						    10000));
	ut_asserteq(2000, sandbox_clk_test_set_rate(dev_test,
						    SANDBOX_CLK_TEST_ID_I2C,
						    20000));

	rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0);
	ut_assert(IS_ERR_VALUE(rate));
	rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0);
	ut_assert(IS_ERR_VALUE(rate));

	ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
						     SANDBOX_CLK_TEST_ID_SPI));
	ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
						     SANDBOX_CLK_TEST_ID_I2C));

	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
	ut_asserteq(10000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(20000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));

	ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_SPI));
	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));

	ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_I2C));
	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));

	ut_assertok(sandbox_clk_test_disable(dev_test,
					     SANDBOX_CLK_TEST_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));

	ut_assertok(sandbox_clk_test_disable(dev_test,
					     SANDBOX_CLK_TEST_ID_I2C));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));

	ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
						   SANDBOX_CLK_ID_SPI));
	ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
						   SANDBOX_CLK_ID_I2C));
	ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
						   SANDBOX_CLK_ID_UART2));
	ut_assertok(sandbox_clk_test_free(dev_test));
	ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
						   SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
						   SANDBOX_CLK_ID_I2C));
	ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
						   SANDBOX_CLK_ID_UART2));

	ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
						   SANDBOX_CLK_ID_UART1));
	ut_assertok(device_remove(dev_test, DM_REMOVE_NORMAL));
	ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
						   SANDBOX_CLK_ID_UART1));
	return 0;
}
DM_TEST(dm_test_clk, DM_TESTF_SCAN_FDT);

static int dm_test_clk_bulk(struct unit_test_state *uts)
{
	struct udevice *dev_clk, *dev_test;

	ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
					      &dev_clk));
	ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
					      &dev_test));
	ut_assertok(sandbox_clk_test_get_bulk(dev_test));

	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));

	/* Fixed clock does not support enable, thus should not fail */
	ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));

	/* Fixed clock does not support disable, thus should not fail */
	ut_assertok(sandbox_clk_test_disable_bulk(dev_test));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));

	/* Fixed clock does not support enable, thus should not fail */
	ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));

	/* Fixed clock does not support disable, thus should not fail */
	ut_assertok(sandbox_clk_test_release_bulk(dev_test));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
	ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
	ut_assertok(device_remove(dev_test, DM_REMOVE_NORMAL));

	return 0;
}
DM_TEST(dm_test_clk_bulk, DM_TESTF_SCAN_FDT);