blob: ac56f17b775dd8c4e71d54478174453dabeee3c5 [file] [log] [blame]
Lukasz Majewski8c0709b2019-06-24 15:50:50 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5 */
6
Lukasz Majewski8c0709b2019-06-24 15:50:50 +02007#include <clk.h>
8#include <dm.h>
9#include <asm/clk.h>
10#include <dm/test.h>
11#include <dm/uclass.h>
12#include <linux/err.h>
Simon Glass75c4d412020-07-19 10:15:37 -060013#include <test/test.h>
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020014#include <test/ut.h>
15#include <sandbox-clk.h>
16
17/* Tests for Common Clock Framework driver */
18static int dm_test_clk_ccf(struct unit_test_state *uts)
19{
Yang Xiwen873de892023-12-16 04:21:11 +080020 struct clk *clk, *pclk;
Yang Xiwene89289c2023-12-16 02:28:52 +080021 struct udevice *dev, *test_dev;
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020022 long long rate;
23 int ret;
Claudiu Bezneac8c16002020-09-07 17:46:34 +030024#if CONFIG_IS_ENABLED(CLK_CCF)
Yang Xiwen873de892023-12-16 04:21:11 +080025 struct clk clk_ccf;
Claudiu Bezneac8c16002020-09-07 17:46:34 +030026 const char *clkname;
Claudiu Bezneab02e8dd2020-09-07 17:46:35 +030027 int clkid, i;
Claudiu Bezneac8c16002020-09-07 17:46:34 +030028#endif
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020029
30 /* Get the device using the clk device */
31 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-ccf", &dev));
Yang Xiwene89289c2023-12-16 02:28:52 +080032 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", &test_dev));
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020033
34 /* Test for clk_get_by_id() */
35 ret = clk_get_by_id(SANDBOX_CLK_ECSPI_ROOT, &clk);
36 ut_assertok(ret);
37 ut_asserteq_str("ecspi_root", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020038 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020039
40 /* Test for clk_get_parent_rate() */
41 ret = clk_get_by_id(SANDBOX_CLK_ECSPI1, &clk);
42 ut_assertok(ret);
43 ut_asserteq_str("ecspi1", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020044 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020045
46 rate = clk_get_parent_rate(clk);
47 ut_asserteq(rate, 20000000);
48
Dario Binacchic98b8022020-04-13 14:36:26 +020049 /* test the gate of CCF */
50 ret = clk_get_by_id(SANDBOX_CLK_ECSPI0, &clk);
51 ut_assertok(ret);
52 ut_asserteq_str("ecspi0", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020053 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Dario Binacchic98b8022020-04-13 14:36:26 +020054
55 rate = clk_get_parent_rate(clk);
56 ut_asserteq(rate, 20000000);
57
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020058 /* Test the mux of CCF */
59 ret = clk_get_by_id(SANDBOX_CLK_USDHC1_SEL, &clk);
60 ut_assertok(ret);
61 ut_asserteq_str("usdhc1_sel", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020062 ut_asserteq(CLK_SET_RATE_NO_REPARENT, clk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020063
64 rate = clk_get_parent_rate(clk);
65 ut_asserteq(rate, 60000000);
66
Igor Prusovbde44c92023-12-06 02:23:34 +030067 rate = clk_set_rate(clk, 60000000);
68 ut_asserteq(rate, -ENOSYS);
69
Dario Binacchie7ce74d2020-06-03 15:36:25 +020070 rate = clk_get_rate(clk);
71 ut_asserteq(rate, 60000000);
72
73 ret = clk_get_by_id(SANDBOX_CLK_PLL3_80M, &pclk);
74 ut_assertok(ret);
75
76 ret = clk_set_parent(clk, pclk);
77 ut_assertok(ret);
78
79 rate = clk_get_rate(clk);
80 ut_asserteq(rate, 80000000);
81
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020082 ret = clk_get_by_id(SANDBOX_CLK_USDHC2_SEL, &clk);
83 ut_assertok(ret);
84 ut_asserteq_str("usdhc2_sel", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020085 ut_asserteq(CLK_SET_RATE_NO_REPARENT, clk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020086
87 rate = clk_get_parent_rate(clk);
88 ut_asserteq(rate, 80000000);
89
90 pclk = clk_get_parent(clk);
91 ut_asserteq_str("pll3_80m", pclk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020092 ut_asserteq(CLK_SET_RATE_PARENT, pclk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020093
Igor Prusovbde44c92023-12-06 02:23:34 +030094 rate = clk_set_rate(clk, 80000000);
95 ut_asserteq(rate, -ENOSYS);
96
Dario Binacchie7ce74d2020-06-03 15:36:25 +020097 rate = clk_get_rate(clk);
98 ut_asserteq(rate, 80000000);
99
100 ret = clk_get_by_id(SANDBOX_CLK_PLL3_60M, &pclk);
101 ut_assertok(ret);
102
103 ret = clk_set_parent(clk, pclk);
104 ut_assertok(ret);
105
106 rate = clk_get_rate(clk);
107 ut_asserteq(rate, 60000000);
108
Peng Fan91f053f2019-07-31 07:02:05 +0000109 /* Test the composite of CCF */
110 ret = clk_get_by_id(SANDBOX_CLK_I2C, &clk);
111 ut_assertok(ret);
112 ut_asserteq_str("i2c", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +0200113 ut_asserteq(CLK_SET_RATE_UNGATE, clk->flags);
Peng Fan91f053f2019-07-31 07:02:05 +0000114
115 rate = clk_get_rate(clk);
116 ut_asserteq(rate, 60000000);
117
Igor Prusovbde44c92023-12-06 02:23:34 +0300118 rate = clk_set_rate(clk, 60000000);
119 ut_asserteq(rate, 60000000);
120
Peng Fan8eec5102019-08-21 13:35:19 +0000121#if CONFIG_IS_ENABLED(CLK_CCF)
122 /* Test clk tree enable/disable */
Yang Xiwene89289c2023-12-16 02:28:52 +0800123
124 ret = clk_get_by_index(test_dev, SANDBOX_CLK_TEST_ID_I2C_ROOT, &clk_ccf);
125 ut_assertok(ret);
126 ut_asserteq_str("clk-ccf", clk_ccf.dev->name);
127 ut_asserteq(clk_ccf.id, SANDBOX_CLK_I2C_ROOT);
128
Peng Fan8eec5102019-08-21 13:35:19 +0000129 ret = clk_get_by_id(SANDBOX_CLK_I2C_ROOT, &clk);
130 ut_assertok(ret);
131 ut_asserteq_str("i2c_root", clk->dev->name);
Yang Xiwene89289c2023-12-16 02:28:52 +0800132 ut_asserteq(clk->id, SANDBOX_CLK_I2C_ROOT);
Peng Fan8eec5102019-08-21 13:35:19 +0000133
Yang Xiwene89289c2023-12-16 02:28:52 +0800134 ret = clk_enable(&clk_ccf);
Peng Fan8eec5102019-08-21 13:35:19 +0000135 ut_assertok(ret);
136
137 ret = sandbox_clk_enable_count(clk);
138 ut_asserteq(ret, 1);
139
140 ret = clk_get_by_id(SANDBOX_CLK_I2C, &pclk);
141 ut_assertok(ret);
142
143 ret = sandbox_clk_enable_count(pclk);
144 ut_asserteq(ret, 1);
145
146 ret = clk_disable(clk);
147 ut_assertok(ret);
148
149 ret = sandbox_clk_enable_count(clk);
150 ut_asserteq(ret, 0);
151
152 ret = sandbox_clk_enable_count(pclk);
153 ut_asserteq(ret, 0);
Claudiu Bezneac8c16002020-09-07 17:46:34 +0300154
155 /* Test clock re-parenting. */
156 ret = clk_get_by_id(SANDBOX_CLK_USDHC1_SEL, &clk);
157 ut_assertok(ret);
158 ut_asserteq_str("usdhc1_sel", clk->dev->name);
159
160 pclk = clk_get_parent(clk);
161 ut_assertok_ptr(pclk);
162 if (!strcmp(pclk->dev->name, "pll3_60m")) {
163 clkname = "pll3_80m";
164 clkid = SANDBOX_CLK_PLL3_80M;
165 } else {
166 clkname = "pll3_60m";
167 clkid = SANDBOX_CLK_PLL3_60M;
168 }
169
170 ret = clk_get_by_id(clkid, &pclk);
171 ut_assertok(ret);
172 ret = clk_set_parent(clk, pclk);
173 ut_assertok(ret);
174 pclk = clk_get_parent(clk);
175 ut_assertok_ptr(pclk);
176 ut_asserteq_str(clkname, pclk->dev->name);
Claudiu Bezneab02e8dd2020-09-07 17:46:35 +0300177
178 /* Test disabling critical clock. */
179 ret = clk_get_by_id(SANDBOX_CLK_I2C_ROOT, &clk);
180 ut_assertok(ret);
181 ut_asserteq_str("i2c_root", clk->dev->name);
182
183 /* Disable it, if any. */
184 ret = sandbox_clk_enable_count(clk);
185 for (i = 0; i < ret; i++) {
186 ret = clk_disable(clk);
187 ut_assertok(ret);
188 }
189
190 ret = sandbox_clk_enable_count(clk);
191 ut_asserteq(ret, 0);
192
193 clk->flags = CLK_IS_CRITICAL;
194 ret = clk_enable(clk);
195 ut_assertok(ret);
196
197 ret = clk_disable(clk);
198 ut_assertok(ret);
199 ret = sandbox_clk_enable_count(clk);
200 ut_asserteq(ret, 1);
201 clk->flags &= ~CLK_IS_CRITICAL;
202
203 ret = clk_disable(clk);
204 ut_assertok(ret);
205 ret = sandbox_clk_enable_count(clk);
206 ut_asserteq(ret, 0);
Peng Fan8eec5102019-08-21 13:35:19 +0000207#endif
208
Lukasz Majewski8c0709b2019-06-24 15:50:50 +0200209 return 1;
210}
Simon Glass1a92f832024-08-22 07:57:48 -0600211DM_TEST(dm_test_clk_ccf, UTF_SCAN_FDT);