blob: 61dad8d85273e9547e52d19956cdaa7294375135 [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
7#include <common.h>
8#include <clk.h>
9#include <dm.h>
10#include <asm/clk.h>
11#include <dm/test.h>
12#include <dm/uclass.h>
13#include <linux/err.h>
Simon Glass75c4d412020-07-19 10:15:37 -060014#include <test/test.h>
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020015#include <test/ut.h>
16#include <sandbox-clk.h>
17
18/* Tests for Common Clock Framework driver */
19static int dm_test_clk_ccf(struct unit_test_state *uts)
20{
Yang Xiwen873de892023-12-16 04:21:11 +080021 struct clk *clk, *pclk;
Yang Xiwene89289c2023-12-16 02:28:52 +080022 struct udevice *dev, *test_dev;
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020023 long long rate;
24 int ret;
Claudiu Bezneac8c16002020-09-07 17:46:34 +030025#if CONFIG_IS_ENABLED(CLK_CCF)
Yang Xiwen873de892023-12-16 04:21:11 +080026 struct clk clk_ccf;
Claudiu Bezneac8c16002020-09-07 17:46:34 +030027 const char *clkname;
Claudiu Bezneab02e8dd2020-09-07 17:46:35 +030028 int clkid, i;
Claudiu Bezneac8c16002020-09-07 17:46:34 +030029#endif
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020030
31 /* Get the device using the clk device */
32 ut_assertok(uclass_get_device_by_name(UCLASS_CLK, "clk-ccf", &dev));
Yang Xiwene89289c2023-12-16 02:28:52 +080033 ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test", &test_dev));
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020034
35 /* Test for clk_get_by_id() */
36 ret = clk_get_by_id(SANDBOX_CLK_ECSPI_ROOT, &clk);
37 ut_assertok(ret);
38 ut_asserteq_str("ecspi_root", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020039 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020040
41 /* Test for clk_get_parent_rate() */
42 ret = clk_get_by_id(SANDBOX_CLK_ECSPI1, &clk);
43 ut_assertok(ret);
44 ut_asserteq_str("ecspi1", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020045 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020046
47 rate = clk_get_parent_rate(clk);
48 ut_asserteq(rate, 20000000);
49
Dario Binacchic98b8022020-04-13 14:36:26 +020050 /* test the gate of CCF */
51 ret = clk_get_by_id(SANDBOX_CLK_ECSPI0, &clk);
52 ut_assertok(ret);
53 ut_asserteq_str("ecspi0", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020054 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Dario Binacchic98b8022020-04-13 14:36:26 +020055
56 rate = clk_get_parent_rate(clk);
57 ut_asserteq(rate, 20000000);
58
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020059 /* Test the mux of CCF */
60 ret = clk_get_by_id(SANDBOX_CLK_USDHC1_SEL, &clk);
61 ut_assertok(ret);
62 ut_asserteq_str("usdhc1_sel", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020063 ut_asserteq(CLK_SET_RATE_NO_REPARENT, clk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020064
65 rate = clk_get_parent_rate(clk);
66 ut_asserteq(rate, 60000000);
67
Igor Prusovbde44c92023-12-06 02:23:34 +030068 rate = clk_set_rate(clk, 60000000);
69 ut_asserteq(rate, -ENOSYS);
70
Dario Binacchie7ce74d2020-06-03 15:36:25 +020071 rate = clk_get_rate(clk);
72 ut_asserteq(rate, 60000000);
73
74 ret = clk_get_by_id(SANDBOX_CLK_PLL3_80M, &pclk);
75 ut_assertok(ret);
76
77 ret = clk_set_parent(clk, pclk);
78 ut_assertok(ret);
79
80 rate = clk_get_rate(clk);
81 ut_asserteq(rate, 80000000);
82
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020083 ret = clk_get_by_id(SANDBOX_CLK_USDHC2_SEL, &clk);
84 ut_assertok(ret);
85 ut_asserteq_str("usdhc2_sel", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020086 ut_asserteq(CLK_SET_RATE_NO_REPARENT, clk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020087
88 rate = clk_get_parent_rate(clk);
89 ut_asserteq(rate, 80000000);
90
91 pclk = clk_get_parent(clk);
92 ut_asserteq_str("pll3_80m", pclk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +020093 ut_asserteq(CLK_SET_RATE_PARENT, pclk->flags);
Lukasz Majewski8c0709b2019-06-24 15:50:50 +020094
Igor Prusovbde44c92023-12-06 02:23:34 +030095 rate = clk_set_rate(clk, 80000000);
96 ut_asserteq(rate, -ENOSYS);
97
Dario Binacchie7ce74d2020-06-03 15:36:25 +020098 rate = clk_get_rate(clk);
99 ut_asserteq(rate, 80000000);
100
101 ret = clk_get_by_id(SANDBOX_CLK_PLL3_60M, &pclk);
102 ut_assertok(ret);
103
104 ret = clk_set_parent(clk, pclk);
105 ut_assertok(ret);
106
107 rate = clk_get_rate(clk);
108 ut_asserteq(rate, 60000000);
109
Peng Fan91f053f2019-07-31 07:02:05 +0000110 /* Test the composite of CCF */
111 ret = clk_get_by_id(SANDBOX_CLK_I2C, &clk);
112 ut_assertok(ret);
113 ut_asserteq_str("i2c", clk->dev->name);
Dario Binacchi1a62dc12020-04-13 14:36:27 +0200114 ut_asserteq(CLK_SET_RATE_UNGATE, clk->flags);
Peng Fan91f053f2019-07-31 07:02:05 +0000115
116 rate = clk_get_rate(clk);
117 ut_asserteq(rate, 60000000);
118
Igor Prusovbde44c92023-12-06 02:23:34 +0300119 rate = clk_set_rate(clk, 60000000);
120 ut_asserteq(rate, 60000000);
121
Peng Fan8eec5102019-08-21 13:35:19 +0000122#if CONFIG_IS_ENABLED(CLK_CCF)
123 /* Test clk tree enable/disable */
Yang Xiwene89289c2023-12-16 02:28:52 +0800124
125 ret = clk_get_by_index(test_dev, SANDBOX_CLK_TEST_ID_I2C_ROOT, &clk_ccf);
126 ut_assertok(ret);
127 ut_asserteq_str("clk-ccf", clk_ccf.dev->name);
128 ut_asserteq(clk_ccf.id, SANDBOX_CLK_I2C_ROOT);
129
Peng Fan8eec5102019-08-21 13:35:19 +0000130 ret = clk_get_by_id(SANDBOX_CLK_I2C_ROOT, &clk);
131 ut_assertok(ret);
132 ut_asserteq_str("i2c_root", clk->dev->name);
Yang Xiwene89289c2023-12-16 02:28:52 +0800133 ut_asserteq(clk->id, SANDBOX_CLK_I2C_ROOT);
Peng Fan8eec5102019-08-21 13:35:19 +0000134
Yang Xiwene89289c2023-12-16 02:28:52 +0800135 ret = clk_enable(&clk_ccf);
Peng Fan8eec5102019-08-21 13:35:19 +0000136 ut_assertok(ret);
137
138 ret = sandbox_clk_enable_count(clk);
139 ut_asserteq(ret, 1);
140
141 ret = clk_get_by_id(SANDBOX_CLK_I2C, &pclk);
142 ut_assertok(ret);
143
144 ret = sandbox_clk_enable_count(pclk);
145 ut_asserteq(ret, 1);
146
147 ret = clk_disable(clk);
148 ut_assertok(ret);
149
150 ret = sandbox_clk_enable_count(clk);
151 ut_asserteq(ret, 0);
152
153 ret = sandbox_clk_enable_count(pclk);
154 ut_asserteq(ret, 0);
Claudiu Bezneac8c16002020-09-07 17:46:34 +0300155
156 /* Test clock re-parenting. */
157 ret = clk_get_by_id(SANDBOX_CLK_USDHC1_SEL, &clk);
158 ut_assertok(ret);
159 ut_asserteq_str("usdhc1_sel", clk->dev->name);
160
161 pclk = clk_get_parent(clk);
162 ut_assertok_ptr(pclk);
163 if (!strcmp(pclk->dev->name, "pll3_60m")) {
164 clkname = "pll3_80m";
165 clkid = SANDBOX_CLK_PLL3_80M;
166 } else {
167 clkname = "pll3_60m";
168 clkid = SANDBOX_CLK_PLL3_60M;
169 }
170
171 ret = clk_get_by_id(clkid, &pclk);
172 ut_assertok(ret);
173 ret = clk_set_parent(clk, pclk);
174 ut_assertok(ret);
175 pclk = clk_get_parent(clk);
176 ut_assertok_ptr(pclk);
177 ut_asserteq_str(clkname, pclk->dev->name);
Claudiu Bezneab02e8dd2020-09-07 17:46:35 +0300178
179 /* Test disabling critical clock. */
180 ret = clk_get_by_id(SANDBOX_CLK_I2C_ROOT, &clk);
181 ut_assertok(ret);
182 ut_asserteq_str("i2c_root", clk->dev->name);
183
184 /* Disable it, if any. */
185 ret = sandbox_clk_enable_count(clk);
186 for (i = 0; i < ret; i++) {
187 ret = clk_disable(clk);
188 ut_assertok(ret);
189 }
190
191 ret = sandbox_clk_enable_count(clk);
192 ut_asserteq(ret, 0);
193
194 clk->flags = CLK_IS_CRITICAL;
195 ret = clk_enable(clk);
196 ut_assertok(ret);
197
198 ret = clk_disable(clk);
199 ut_assertok(ret);
200 ret = sandbox_clk_enable_count(clk);
201 ut_asserteq(ret, 1);
202 clk->flags &= ~CLK_IS_CRITICAL;
203
204 ret = clk_disable(clk);
205 ut_assertok(ret);
206 ret = sandbox_clk_enable_count(clk);
207 ut_asserteq(ret, 0);
Peng Fan8eec5102019-08-21 13:35:19 +0000208#endif
209
Lukasz Majewski8c0709b2019-06-24 15:50:50 +0200210 return 1;
211}
212
Simon Glass974dccd2020-07-28 19:41:12 -0600213DM_TEST(dm_test_clk_ccf, UT_TESTF_SCAN_FDT);