blob: 3b2398254147065e7dc90e33aaa9ef6291443f6e [file] [log] [blame]
Lukasz Majewski87e460c2019-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 Glass0e1fad42020-07-19 10:15:37 -060014#include <test/test.h>
Lukasz Majewski87e460c2019-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{
21 struct clk *clk, *pclk;
22 struct udevice *dev;
23 long long rate;
24 int ret;
Claudiu Beznea4d139f32020-09-07 17:46:34 +030025#if CONFIG_IS_ENABLED(CLK_CCF)
26 const char *clkname;
Claudiu Beznea9a5d59d2020-09-07 17:46:35 +030027 int clkid, i;
Claudiu Beznea4d139f32020-09-07 17:46:34 +030028#endif
Lukasz Majewski87e460c2019-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));
32
33 /* Test for clk_get_by_id() */
34 ret = clk_get_by_id(SANDBOX_CLK_ECSPI_ROOT, &clk);
35 ut_assertok(ret);
36 ut_asserteq_str("ecspi_root", clk->dev->name);
Dario Binacchi16bdc852020-04-13 14:36:27 +020037 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Lukasz Majewski87e460c2019-06-24 15:50:50 +020038
39 /* Test for clk_get_parent_rate() */
40 ret = clk_get_by_id(SANDBOX_CLK_ECSPI1, &clk);
41 ut_assertok(ret);
42 ut_asserteq_str("ecspi1", clk->dev->name);
Dario Binacchi16bdc852020-04-13 14:36:27 +020043 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Lukasz Majewski87e460c2019-06-24 15:50:50 +020044
45 rate = clk_get_parent_rate(clk);
46 ut_asserteq(rate, 20000000);
47
Dario Binacchicd16c572020-04-13 14:36:26 +020048 /* test the gate of CCF */
49 ret = clk_get_by_id(SANDBOX_CLK_ECSPI0, &clk);
50 ut_assertok(ret);
51 ut_asserteq_str("ecspi0", clk->dev->name);
Dario Binacchi16bdc852020-04-13 14:36:27 +020052 ut_asserteq(CLK_SET_RATE_PARENT, clk->flags);
Dario Binacchicd16c572020-04-13 14:36:26 +020053
54 rate = clk_get_parent_rate(clk);
55 ut_asserteq(rate, 20000000);
56
Lukasz Majewski87e460c2019-06-24 15:50:50 +020057 /* Test the mux of CCF */
58 ret = clk_get_by_id(SANDBOX_CLK_USDHC1_SEL, &clk);
59 ut_assertok(ret);
60 ut_asserteq_str("usdhc1_sel", clk->dev->name);
Dario Binacchi16bdc852020-04-13 14:36:27 +020061 ut_asserteq(CLK_SET_RATE_NO_REPARENT, clk->flags);
Lukasz Majewski87e460c2019-06-24 15:50:50 +020062
63 rate = clk_get_parent_rate(clk);
64 ut_asserteq(rate, 60000000);
65
Igor Prusov9e025032023-12-06 02:23:34 +030066 rate = clk_set_rate(clk, 60000000);
67 ut_asserteq(rate, -ENOSYS);
68
Dario Binacchi12d15262020-06-03 15:36:25 +020069 rate = clk_get_rate(clk);
70 ut_asserteq(rate, 60000000);
71
72 ret = clk_get_by_id(SANDBOX_CLK_PLL3_80M, &pclk);
73 ut_assertok(ret);
74
75 ret = clk_set_parent(clk, pclk);
76 ut_assertok(ret);
77
78 rate = clk_get_rate(clk);
79 ut_asserteq(rate, 80000000);
80
Lukasz Majewski87e460c2019-06-24 15:50:50 +020081 ret = clk_get_by_id(SANDBOX_CLK_USDHC2_SEL, &clk);
82 ut_assertok(ret);
83 ut_asserteq_str("usdhc2_sel", clk->dev->name);
Dario Binacchi16bdc852020-04-13 14:36:27 +020084 ut_asserteq(CLK_SET_RATE_NO_REPARENT, clk->flags);
Lukasz Majewski87e460c2019-06-24 15:50:50 +020085
86 rate = clk_get_parent_rate(clk);
87 ut_asserteq(rate, 80000000);
88
89 pclk = clk_get_parent(clk);
90 ut_asserteq_str("pll3_80m", pclk->dev->name);
Dario Binacchi16bdc852020-04-13 14:36:27 +020091 ut_asserteq(CLK_SET_RATE_PARENT, pclk->flags);
Lukasz Majewski87e460c2019-06-24 15:50:50 +020092
Igor Prusov9e025032023-12-06 02:23:34 +030093 rate = clk_set_rate(clk, 80000000);
94 ut_asserteq(rate, -ENOSYS);
95
Dario Binacchi12d15262020-06-03 15:36:25 +020096 rate = clk_get_rate(clk);
97 ut_asserteq(rate, 80000000);
98
99 ret = clk_get_by_id(SANDBOX_CLK_PLL3_60M, &pclk);
100 ut_assertok(ret);
101
102 ret = clk_set_parent(clk, pclk);
103 ut_assertok(ret);
104
105 rate = clk_get_rate(clk);
106 ut_asserteq(rate, 60000000);
107
Peng Fan4f895982019-07-31 07:02:05 +0000108 /* Test the composite of CCF */
109 ret = clk_get_by_id(SANDBOX_CLK_I2C, &clk);
110 ut_assertok(ret);
111 ut_asserteq_str("i2c", clk->dev->name);
Dario Binacchi16bdc852020-04-13 14:36:27 +0200112 ut_asserteq(CLK_SET_RATE_UNGATE, clk->flags);
Peng Fan4f895982019-07-31 07:02:05 +0000113
114 rate = clk_get_rate(clk);
115 ut_asserteq(rate, 60000000);
116
Igor Prusov9e025032023-12-06 02:23:34 +0300117 rate = clk_set_rate(clk, 60000000);
118 ut_asserteq(rate, 60000000);
119
Peng Fanc66f4f52019-08-21 13:35:19 +0000120#if CONFIG_IS_ENABLED(CLK_CCF)
121 /* Test clk tree enable/disable */
122 ret = clk_get_by_id(SANDBOX_CLK_I2C_ROOT, &clk);
123 ut_assertok(ret);
124 ut_asserteq_str("i2c_root", clk->dev->name);
125
126 ret = clk_enable(clk);
127 ut_assertok(ret);
128
129 ret = sandbox_clk_enable_count(clk);
130 ut_asserteq(ret, 1);
131
132 ret = clk_get_by_id(SANDBOX_CLK_I2C, &pclk);
133 ut_assertok(ret);
134
135 ret = sandbox_clk_enable_count(pclk);
136 ut_asserteq(ret, 1);
137
138 ret = clk_disable(clk);
139 ut_assertok(ret);
140
141 ret = sandbox_clk_enable_count(clk);
142 ut_asserteq(ret, 0);
143
144 ret = sandbox_clk_enable_count(pclk);
145 ut_asserteq(ret, 0);
Claudiu Beznea4d139f32020-09-07 17:46:34 +0300146
147 /* Test clock re-parenting. */
148 ret = clk_get_by_id(SANDBOX_CLK_USDHC1_SEL, &clk);
149 ut_assertok(ret);
150 ut_asserteq_str("usdhc1_sel", clk->dev->name);
151
152 pclk = clk_get_parent(clk);
153 ut_assertok_ptr(pclk);
154 if (!strcmp(pclk->dev->name, "pll3_60m")) {
155 clkname = "pll3_80m";
156 clkid = SANDBOX_CLK_PLL3_80M;
157 } else {
158 clkname = "pll3_60m";
159 clkid = SANDBOX_CLK_PLL3_60M;
160 }
161
162 ret = clk_get_by_id(clkid, &pclk);
163 ut_assertok(ret);
164 ret = clk_set_parent(clk, pclk);
165 ut_assertok(ret);
166 pclk = clk_get_parent(clk);
167 ut_assertok_ptr(pclk);
168 ut_asserteq_str(clkname, pclk->dev->name);
Claudiu Beznea9a5d59d2020-09-07 17:46:35 +0300169
170 /* Test disabling critical clock. */
171 ret = clk_get_by_id(SANDBOX_CLK_I2C_ROOT, &clk);
172 ut_assertok(ret);
173 ut_asserteq_str("i2c_root", clk->dev->name);
174
175 /* Disable it, if any. */
176 ret = sandbox_clk_enable_count(clk);
177 for (i = 0; i < ret; i++) {
178 ret = clk_disable(clk);
179 ut_assertok(ret);
180 }
181
182 ret = sandbox_clk_enable_count(clk);
183 ut_asserteq(ret, 0);
184
185 clk->flags = CLK_IS_CRITICAL;
186 ret = clk_enable(clk);
187 ut_assertok(ret);
188
189 ret = clk_disable(clk);
190 ut_assertok(ret);
191 ret = sandbox_clk_enable_count(clk);
192 ut_asserteq(ret, 1);
193 clk->flags &= ~CLK_IS_CRITICAL;
194
195 ret = clk_disable(clk);
196 ut_assertok(ret);
197 ret = sandbox_clk_enable_count(clk);
198 ut_asserteq(ret, 0);
Peng Fanc66f4f52019-08-21 13:35:19 +0000199#endif
200
Lukasz Majewski87e460c2019-06-24 15:50:50 +0200201 return 1;
202}
203
Simon Glasse180c2b2020-07-28 19:41:12 -0600204DM_TEST(dm_test_clk_ccf, UT_TESTF_SCAN_FDT);