1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2015 Google, Inc
4 */
5
6 #include <common.h>
7 #include <clk.h>
8 #include <dm.h>
9 #include <log.h>
10 #include <malloc.h>
11 #include <asm/clk.h>
12 #include <dm/test.h>
13 #include <dm/device-internal.h>
14 #include <linux/err.h>
15 #include <test/test.h>
16 #include <test/ut.h>
17
18 /* Base test of the clk uclass */
dm_test_clk_base(struct unit_test_state * uts)19 static int dm_test_clk_base(struct unit_test_state *uts)
20 {
21 struct udevice *dev;
22 struct clk clk_method1;
23 struct clk clk_method2;
24
25 /* Get the device using the clk device */
26 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", &dev));
27
28 /* Get the same clk port in 2 different ways and compare */
29 ut_assertok(clk_get_by_index(dev, 1, &clk_method1));
30 ut_assertok(clk_get_by_index_nodev(dev_ofnode(dev), 1, &clk_method2));
31 ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true);
32 ut_asserteq(clk_method1.id, clk_method2.id);
33
34 return 0;
35 }
36
37 DM_TEST(dm_test_clk_base, UT_TESTF_SCAN_FDT);
38
dm_test_clk(struct unit_test_state * uts)39 static int dm_test_clk(struct unit_test_state *uts)
40 {
41 struct udevice *dev_fixed, *dev_fixed_factor, *dev_clk, *dev_test;
42 ulong rate;
43
44 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed",
45 &dev_fixed));
46
47 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed-factor",
48 &dev_fixed_factor));
49
50 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
51 &dev_clk));
52 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
53 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
54 ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
55 ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
56
57 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
58 &dev_test));
59 ut_assertok(sandbox_clk_test_get(dev_test));
60 ut_assertok(sandbox_clk_test_devm_get(dev_test));
61 ut_assertok(sandbox_clk_test_valid(dev_test));
62
63 ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
64 SANDBOX_CLK_TEST_ID_DEVM_NULL));
65 ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
66 SANDBOX_CLK_TEST_ID_DEVM_NULL,
67 0));
68 ut_asserteq(0, sandbox_clk_test_enable(dev_test,
69 SANDBOX_CLK_TEST_ID_DEVM_NULL));
70 ut_asserteq(0, sandbox_clk_test_disable(dev_test,
71 SANDBOX_CLK_TEST_ID_DEVM_NULL));
72
73 ut_asserteq(1234,
74 sandbox_clk_test_get_rate(dev_test,
75 SANDBOX_CLK_TEST_ID_FIXED));
76 ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
77 SANDBOX_CLK_TEST_ID_SPI));
78 ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
79 SANDBOX_CLK_TEST_ID_I2C));
80 ut_asserteq(321, sandbox_clk_test_get_rate(dev_test,
81 SANDBOX_CLK_TEST_ID_DEVM1));
82 ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
83 SANDBOX_CLK_TEST_ID_DEVM2));
84
85 rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED,
86 12345);
87 ut_assert(IS_ERR_VALUE(rate));
88 rate = sandbox_clk_test_get_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED);
89 ut_asserteq(1234, rate);
90
91 ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
92 SANDBOX_CLK_TEST_ID_SPI,
93 1000));
94 ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
95 SANDBOX_CLK_TEST_ID_I2C,
96 2000));
97
98 ut_asserteq(1000, sandbox_clk_test_get_rate(dev_test,
99 SANDBOX_CLK_TEST_ID_SPI));
100 ut_asserteq(2000, sandbox_clk_test_get_rate(dev_test,
101 SANDBOX_CLK_TEST_ID_I2C));
102
103 ut_asserteq(1000, sandbox_clk_test_set_rate(dev_test,
104 SANDBOX_CLK_TEST_ID_SPI,
105 10000));
106 ut_asserteq(2000, sandbox_clk_test_set_rate(dev_test,
107 SANDBOX_CLK_TEST_ID_I2C,
108 20000));
109
110 rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0);
111 ut_assert(IS_ERR_VALUE(rate));
112 rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0);
113 ut_assert(IS_ERR_VALUE(rate));
114
115 ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
116 SANDBOX_CLK_TEST_ID_SPI));
117 ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
118 SANDBOX_CLK_TEST_ID_I2C));
119
120 ut_asserteq(5000, sandbox_clk_test_round_rate(dev_test,
121 SANDBOX_CLK_TEST_ID_SPI,
122 5000));
123 ut_asserteq(7000, sandbox_clk_test_round_rate(dev_test,
124 SANDBOX_CLK_TEST_ID_I2C,
125 7000));
126
127 ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
128 SANDBOX_CLK_TEST_ID_SPI));
129 ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
130 SANDBOX_CLK_TEST_ID_I2C));
131
132 rate = sandbox_clk_test_round_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0);
133 ut_assert(IS_ERR_VALUE(rate));
134 rate = sandbox_clk_test_round_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0);
135 ut_assert(IS_ERR_VALUE(rate));
136
137 ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
138 SANDBOX_CLK_TEST_ID_SPI));
139 ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
140 SANDBOX_CLK_TEST_ID_I2C));
141
142 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
143 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
144 ut_asserteq(10000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
145 ut_asserteq(20000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
146
147 ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_SPI));
148 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
149 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
150
151 ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_I2C));
152 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
153 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
154
155 ut_assertok(sandbox_clk_test_disable(dev_test,
156 SANDBOX_CLK_TEST_ID_SPI));
157 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
158 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
159
160 ut_assertok(sandbox_clk_test_disable(dev_test,
161 SANDBOX_CLK_TEST_ID_I2C));
162 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
163 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
164
165 ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
166 SANDBOX_CLK_ID_SPI));
167 ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
168 SANDBOX_CLK_ID_I2C));
169 ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
170 SANDBOX_CLK_ID_UART2));
171 ut_assertok(sandbox_clk_test_free(dev_test));
172 ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
173 SANDBOX_CLK_ID_SPI));
174 ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
175 SANDBOX_CLK_ID_I2C));
176 ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
177 SANDBOX_CLK_ID_UART2));
178
179 ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
180 SANDBOX_CLK_ID_UART1));
181 ut_assertok(device_remove(dev_test, DM_REMOVE_NORMAL));
182 ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
183 SANDBOX_CLK_ID_UART1));
184 return 0;
185 }
186 DM_TEST(dm_test_clk, UT_TESTF_SCAN_FDT);
187
dm_test_clk_bulk(struct unit_test_state * uts)188 static int dm_test_clk_bulk(struct unit_test_state *uts)
189 {
190 struct udevice *dev_clk, *dev_test;
191
192 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
193 &dev_clk));
194 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
195 &dev_test));
196 ut_assertok(sandbox_clk_test_get_bulk(dev_test));
197
198 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
199 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
200
201 /* Fixed clock does not support enable, thus should not fail */
202 ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
203 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
204 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
205
206 /* Fixed clock does not support disable, thus should not fail */
207 ut_assertok(sandbox_clk_test_disable_bulk(dev_test));
208 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
209 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
210
211 /* Fixed clock does not support enable, thus should not fail */
212 ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
213 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
214 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
215
216 /* Fixed clock does not support disable, thus should not fail */
217 ut_assertok(sandbox_clk_test_release_bulk(dev_test));
218 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
219 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
220 ut_assertok(device_remove(dev_test, DM_REMOVE_NORMAL));
221
222 return 0;
223 }
224 DM_TEST(dm_test_clk_bulk, UT_TESTF_SCAN_FDT);
225