blob: f48de05436bf2abcb6546b5dfce9a33276fc3784 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8cc4d822015-07-06 12:54:24 -06002/*
3 * Copyright (C) 2015 Google, Inc
Simon Glass8cc4d822015-07-06 12:54:24 -06004 */
5
6#include <common.h>
Jagan Tekiab127ba2019-03-05 19:42:44 +05307#include <clk.h>
Simon Glass8cc4d822015-07-06 12:54:24 -06008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Stephen Warrena9622432016-06-17 09:44:00 -060011#include <asm/clk.h>
Simon Glass8cc4d822015-07-06 12:54:24 -060012#include <dm/test.h>
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020013#include <dm/device-internal.h>
Simon Glass8cc4d822015-07-06 12:54:24 -060014#include <linux/err.h>
Simon Glass75c4d412020-07-19 10:15:37 -060015#include <test/test.h>
Simon Glass8cc4d822015-07-06 12:54:24 -060016#include <test/ut.h>
17
Jagan Tekiab127ba2019-03-05 19:42:44 +053018/* Base test of the clk uclass */
19static 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 */
Samuel Hollandbae0f4f2023-01-21 18:02:51 -060029 ut_assertok(clk_get_by_index(dev, 0, &clk_method1));
30 ut_assertok(clk_get_by_name(dev, NULL, &clk_method2));
31 ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true);
32 ut_asserteq(clk_method1.id, clk_method2.id);
33
Jagan Tekiab127ba2019-03-05 19:42:44 +053034 ut_assertok(clk_get_by_index(dev, 1, &clk_method1));
35 ut_assertok(clk_get_by_index_nodev(dev_ofnode(dev), 1, &clk_method2));
Sekhar Noricf3119d2019-08-01 19:12:55 +053036 ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true);
Jagan Tekiab127ba2019-03-05 19:42:44 +053037 ut_asserteq(clk_method1.id, clk_method2.id);
38
39 return 0;
40}
41
Simon Glass974dccd2020-07-28 19:41:12 -060042DM_TEST(dm_test_clk_base, UT_TESTF_SCAN_FDT);
Jagan Tekiab127ba2019-03-05 19:42:44 +053043
Stephen Warrena9622432016-06-17 09:44:00 -060044static int dm_test_clk(struct unit_test_state *uts)
Simon Glass8cc4d822015-07-06 12:54:24 -060045{
Anup Patel8d28c3c2019-02-25 08:14:55 +000046 struct udevice *dev_fixed, *dev_fixed_factor, *dev_clk, *dev_test;
Simon Glass8cc4d822015-07-06 12:54:24 -060047 ulong rate;
48
Stephen Warrena9622432016-06-17 09:44:00 -060049 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed",
50 &dev_fixed));
Simon Glass8cc4d822015-07-06 12:54:24 -060051
Anup Patel8d28c3c2019-02-25 08:14:55 +000052 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-fixed-factor",
53 &dev_fixed_factor));
54
Stephen Warrena9622432016-06-17 09:44:00 -060055 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
56 &dev_clk));
57 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
58 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
59 ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
60 ut_asserteq(0, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
Simon Glass8cc4d822015-07-06 12:54:24 -060061
Stephen Warrena9622432016-06-17 09:44:00 -060062 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
63 &dev_test));
64 ut_assertok(sandbox_clk_test_get(dev_test));
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020065 ut_assertok(sandbox_clk_test_devm_get(dev_test));
Fabrice Gasnier11192712018-07-24 16:31:28 +020066 ut_assertok(sandbox_clk_test_valid(dev_test));
Simon Glass8cc4d822015-07-06 12:54:24 -060067
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020068 ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
69 SANDBOX_CLK_TEST_ID_DEVM_NULL));
70 ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
71 SANDBOX_CLK_TEST_ID_DEVM_NULL,
72 0));
73 ut_asserteq(0, sandbox_clk_test_enable(dev_test,
74 SANDBOX_CLK_TEST_ID_DEVM_NULL));
75 ut_asserteq(0, sandbox_clk_test_disable(dev_test,
76 SANDBOX_CLK_TEST_ID_DEVM_NULL));
77
Stephen Warrena9622432016-06-17 09:44:00 -060078 ut_asserteq(1234,
79 sandbox_clk_test_get_rate(dev_test,
80 SANDBOX_CLK_TEST_ID_FIXED));
81 ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
82 SANDBOX_CLK_TEST_ID_SPI));
83 ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
84 SANDBOX_CLK_TEST_ID_I2C));
Jean-Jacques Hiblotc1e9c942019-10-22 14:00:07 +020085 ut_asserteq(321, sandbox_clk_test_get_rate(dev_test,
86 SANDBOX_CLK_TEST_ID_DEVM1));
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +020087 ut_asserteq(0, sandbox_clk_test_get_rate(dev_test,
88 SANDBOX_CLK_TEST_ID_DEVM2));
Simon Glass8cc4d822015-07-06 12:54:24 -060089
Stephen Warrena9622432016-06-17 09:44:00 -060090 rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED,
91 12345);
92 ut_assert(IS_ERR_VALUE(rate));
93 rate = sandbox_clk_test_get_rate(dev_test, SANDBOX_CLK_TEST_ID_FIXED);
94 ut_asserteq(1234, rate);
Simon Glass8cc4d822015-07-06 12:54:24 -060095
Stephen Warrena9622432016-06-17 09:44:00 -060096 ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
97 SANDBOX_CLK_TEST_ID_SPI,
98 1000));
99 ut_asserteq(0, sandbox_clk_test_set_rate(dev_test,
100 SANDBOX_CLK_TEST_ID_I2C,
101 2000));
Simon Glass8cc4d822015-07-06 12:54:24 -0600102
Stephen Warrena9622432016-06-17 09:44:00 -0600103 ut_asserteq(1000, sandbox_clk_test_get_rate(dev_test,
104 SANDBOX_CLK_TEST_ID_SPI));
105 ut_asserteq(2000, sandbox_clk_test_get_rate(dev_test,
106 SANDBOX_CLK_TEST_ID_I2C));
Simon Glass8cc4d822015-07-06 12:54:24 -0600107
Stephen Warrena9622432016-06-17 09:44:00 -0600108 ut_asserteq(1000, sandbox_clk_test_set_rate(dev_test,
109 SANDBOX_CLK_TEST_ID_SPI,
110 10000));
111 ut_asserteq(2000, sandbox_clk_test_set_rate(dev_test,
112 SANDBOX_CLK_TEST_ID_I2C,
113 20000));
114
115 rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0);
116 ut_assert(IS_ERR_VALUE(rate));
117 rate = sandbox_clk_test_set_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0);
118 ut_assert(IS_ERR_VALUE(rate));
Simon Glass8cc4d822015-07-06 12:54:24 -0600119
Stephen Warrena9622432016-06-17 09:44:00 -0600120 ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
121 SANDBOX_CLK_TEST_ID_SPI));
122 ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
123 SANDBOX_CLK_TEST_ID_I2C));
124
Dario Binacchib7f85892020-12-30 00:06:31 +0100125 ut_asserteq(5000, sandbox_clk_test_round_rate(dev_test,
126 SANDBOX_CLK_TEST_ID_SPI,
127 5000));
128 ut_asserteq(7000, sandbox_clk_test_round_rate(dev_test,
129 SANDBOX_CLK_TEST_ID_I2C,
130 7000));
131
132 ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
133 SANDBOX_CLK_TEST_ID_SPI));
134 ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
135 SANDBOX_CLK_TEST_ID_I2C));
136
137 rate = sandbox_clk_test_round_rate(dev_test, SANDBOX_CLK_TEST_ID_SPI, 0);
138 ut_assert(IS_ERR_VALUE(rate));
139 rate = sandbox_clk_test_round_rate(dev_test, SANDBOX_CLK_TEST_ID_I2C, 0);
140 ut_assert(IS_ERR_VALUE(rate));
141
142 ut_asserteq(10000, sandbox_clk_test_get_rate(dev_test,
143 SANDBOX_CLK_TEST_ID_SPI));
144 ut_asserteq(20000, sandbox_clk_test_get_rate(dev_test,
145 SANDBOX_CLK_TEST_ID_I2C));
146
Stephen Warrena9622432016-06-17 09:44:00 -0600147 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
148 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
149 ut_asserteq(10000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_SPI));
150 ut_asserteq(20000, sandbox_clk_query_rate(dev_clk, SANDBOX_CLK_ID_I2C));
151
152 ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_SPI));
153 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
154 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
155
156 ut_assertok(sandbox_clk_test_enable(dev_test, SANDBOX_CLK_TEST_ID_I2C));
157 ut_asserteq(1, 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_SPI));
162 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
163 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
164
165 ut_assertok(sandbox_clk_test_disable(dev_test,
166 SANDBOX_CLK_TEST_ID_I2C));
167 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
168 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
169
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200170 ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
171 SANDBOX_CLK_ID_SPI));
172 ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
173 SANDBOX_CLK_ID_I2C));
174 ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
175 SANDBOX_CLK_ID_UART2));
Stephen Warrena9622432016-06-17 09:44:00 -0600176 ut_assertok(sandbox_clk_test_free(dev_test));
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200177 ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
178 SANDBOX_CLK_ID_SPI));
179 ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
180 SANDBOX_CLK_ID_I2C));
181 ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
182 SANDBOX_CLK_ID_UART2));
Simon Glass8cc4d822015-07-06 12:54:24 -0600183
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200184 ut_asserteq(1, sandbox_clk_query_requested(dev_clk,
185 SANDBOX_CLK_ID_UART1));
186 ut_assertok(device_remove(dev_test, DM_REMOVE_NORMAL));
187 ut_asserteq(0, sandbox_clk_query_requested(dev_clk,
188 SANDBOX_CLK_ID_UART1));
Simon Glass8cc4d822015-07-06 12:54:24 -0600189 return 0;
190}
Simon Glass974dccd2020-07-28 19:41:12 -0600191DM_TEST(dm_test_clk, UT_TESTF_SCAN_FDT);
Neil Armstrong567a38b2018-04-03 11:44:19 +0200192
193static int dm_test_clk_bulk(struct unit_test_state *uts)
194{
195 struct udevice *dev_clk, *dev_test;
196
197 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-sbox",
198 &dev_clk));
199 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test",
200 &dev_test));
201 ut_assertok(sandbox_clk_test_get_bulk(dev_test));
202
203 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
204 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
205
206 /* Fixed clock does not support enable, thus should not fail */
207 ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
208 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
209 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
210
211 /* Fixed clock does not support disable, thus should not fail */
212 ut_assertok(sandbox_clk_test_disable_bulk(dev_test));
213 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
214 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
215
216 /* Fixed clock does not support enable, thus should not fail */
217 ut_assertok(sandbox_clk_test_enable_bulk(dev_test));
218 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
219 ut_asserteq(1, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
220
221 /* Fixed clock does not support disable, thus should not fail */
222 ut_assertok(sandbox_clk_test_release_bulk(dev_test));
223 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_SPI));
224 ut_asserteq(0, sandbox_clk_query_enable(dev_clk, SANDBOX_CLK_ID_I2C));
Jean-Jacques Hiblot98e84182019-10-22 14:00:05 +0200225 ut_assertok(device_remove(dev_test, DM_REMOVE_NORMAL));
Neil Armstrong567a38b2018-04-03 11:44:19 +0200226
227 return 0;
228}
Simon Glass974dccd2020-07-28 19:41:12 -0600229DM_TEST(dm_test_clk_bulk, UT_TESTF_SCAN_FDT);