blob: 18bdc08971bfea55635d9d43e7219ce240ae7b7f [file] [log] [blame]
Peng Fan6a8e5f92019-03-05 02:32:33 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2018 NXP
4 * Peng Fan <peng.fan@nxp.com>
5 */
6
Peng Fan6a8e5f92019-03-05 02:32:33 +00007#include <clk-uclass.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Peng Fan2e0644a2023-04-28 12:08:09 +080010#include <firmware/imx/sci/sci.h>
Peng Fan6a8e5f92019-03-05 02:32:33 +000011#include <asm/arch/clock.h>
12#include <dt-bindings/clock/imx8qxp-clock.h>
13#include <dt-bindings/soc/imx_rsrc.h>
14#include <misc.h>
15
16#include "clk-imx8.h"
17
Simon Glass495e80f2023-02-05 15:36:26 -070018#if IS_ENABLED(CONFIG_CMD_CLK)
Peng Fan6a8e5f92019-03-05 02:32:33 +000019struct imx8_clks imx8_clk_names[] = {
20 { IMX8QXP_A35_DIV, "A35_DIV" },
21 { IMX8QXP_I2C0_CLK, "I2C0" },
22 { IMX8QXP_I2C1_CLK, "I2C1" },
23 { IMX8QXP_I2C2_CLK, "I2C2" },
24 { IMX8QXP_I2C3_CLK, "I2C3" },
25 { IMX8QXP_UART0_CLK, "UART0" },
26 { IMX8QXP_UART1_CLK, "UART1" },
27 { IMX8QXP_UART2_CLK, "UART2" },
28 { IMX8QXP_UART3_CLK, "UART3" },
29 { IMX8QXP_SDHC0_CLK, "SDHC0" },
30 { IMX8QXP_SDHC1_CLK, "SDHC1" },
31 { IMX8QXP_ENET0_AHB_CLK, "ENET0_AHB" },
32 { IMX8QXP_ENET0_IPG_CLK, "ENET0_IPG" },
33 { IMX8QXP_ENET0_REF_DIV, "ENET0_REF" },
34 { IMX8QXP_ENET0_PTP_CLK, "ENET0_PTP" },
35 { IMX8QXP_ENET1_AHB_CLK, "ENET1_AHB" },
36 { IMX8QXP_ENET1_IPG_CLK, "ENET1_IPG" },
37 { IMX8QXP_ENET1_REF_DIV, "ENET1_REF" },
38 { IMX8QXP_ENET1_PTP_CLK, "ENET1_PTP" },
39};
40
41int num_clks = ARRAY_SIZE(imx8_clk_names);
42#endif
43
44ulong imx8_clk_get_rate(struct clk *clk)
45{
46 sc_pm_clk_t pm_clk;
47 ulong rate;
48 u16 resource;
49 int ret;
50
51 debug("%s(#%lu)\n", __func__, clk->id);
52
53 switch (clk->id) {
54 case IMX8QXP_A35_DIV:
55 resource = SC_R_A35;
56 pm_clk = SC_PM_CLK_CPU;
57 break;
58 case IMX8QXP_I2C0_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +010059 case IMX8QXP_I2C0_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +000060 resource = SC_R_I2C_0;
61 pm_clk = SC_PM_CLK_PER;
62 break;
63 case IMX8QXP_I2C1_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +010064 case IMX8QXP_I2C1_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +000065 resource = SC_R_I2C_1;
66 pm_clk = SC_PM_CLK_PER;
67 break;
68 case IMX8QXP_I2C2_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +010069 case IMX8QXP_I2C2_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +000070 resource = SC_R_I2C_2;
71 pm_clk = SC_PM_CLK_PER;
72 break;
73 case IMX8QXP_I2C3_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +010074 case IMX8QXP_I2C3_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +000075 resource = SC_R_I2C_3;
76 pm_clk = SC_PM_CLK_PER;
77 break;
78 case IMX8QXP_SDHC0_IPG_CLK:
79 case IMX8QXP_SDHC0_CLK:
80 case IMX8QXP_SDHC0_DIV:
81 resource = SC_R_SDHC_0;
82 pm_clk = SC_PM_CLK_PER;
83 break;
84 case IMX8QXP_SDHC1_IPG_CLK:
85 case IMX8QXP_SDHC1_CLK:
86 case IMX8QXP_SDHC1_DIV:
87 resource = SC_R_SDHC_1;
88 pm_clk = SC_PM_CLK_PER;
89 break;
Peng Fan6a8e5f92019-03-05 02:32:33 +000090 case IMX8QXP_UART0_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -030091 case IMX8QXP_UART0_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +000092 resource = SC_R_UART_0;
93 pm_clk = SC_PM_CLK_PER;
94 break;
95 case IMX8QXP_UART1_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -030096 case IMX8QXP_UART1_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +000097 resource = SC_R_UART_1;
98 pm_clk = SC_PM_CLK_PER;
99 break;
100 case IMX8QXP_UART2_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300101 case IMX8QXP_UART2_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000102 resource = SC_R_UART_2;
103 pm_clk = SC_PM_CLK_PER;
104 break;
105 case IMX8QXP_UART3_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300106 case IMX8QXP_UART3_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000107 resource = SC_R_UART_3;
108 pm_clk = SC_PM_CLK_PER;
109 break;
110 case IMX8QXP_ENET0_IPG_CLK:
111 case IMX8QXP_ENET0_AHB_CLK:
112 case IMX8QXP_ENET0_REF_DIV:
113 case IMX8QXP_ENET0_PTP_CLK:
114 resource = SC_R_ENET_0;
115 pm_clk = SC_PM_CLK_PER;
116 break;
117 case IMX8QXP_ENET1_IPG_CLK:
118 case IMX8QXP_ENET1_AHB_CLK:
119 case IMX8QXP_ENET1_REF_DIV:
120 case IMX8QXP_ENET1_PTP_CLK:
121 resource = SC_R_ENET_1;
122 pm_clk = SC_PM_CLK_PER;
123 break;
124 default:
125 if (clk->id < IMX8QXP_UART0_IPG_CLK ||
126 clk->id >= IMX8QXP_CLK_END) {
127 printf("%s(Invalid clk ID #%lu)\n",
128 __func__, clk->id);
129 return -EINVAL;
130 }
Simon Glass29ff16a2021-03-25 10:26:08 +1300131 return -EINVAL;
Peng Fan6a8e5f92019-03-05 02:32:33 +0000132 };
133
134 ret = sc_pm_get_clock_rate(-1, resource, pm_clk,
135 (sc_pm_clock_rate_t *)&rate);
136 if (ret) {
137 printf("%s err %d\n", __func__, ret);
138 return ret;
139 }
140
141 return rate;
142}
143
144ulong imx8_clk_set_rate(struct clk *clk, unsigned long rate)
145{
146 sc_pm_clk_t pm_clk;
147 u32 new_rate = rate;
148 u16 resource;
149 int ret;
150
151 debug("%s(#%lu), rate: %lu\n", __func__, clk->id, rate);
152
153 switch (clk->id) {
154 case IMX8QXP_I2C0_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +0100155 case IMX8QXP_I2C0_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000156 resource = SC_R_I2C_0;
157 pm_clk = SC_PM_CLK_PER;
158 break;
159 case IMX8QXP_I2C1_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +0100160 case IMX8QXP_I2C1_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000161 resource = SC_R_I2C_1;
162 pm_clk = SC_PM_CLK_PER;
163 break;
164 case IMX8QXP_I2C2_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +0100165 case IMX8QXP_I2C2_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000166 resource = SC_R_I2C_2;
167 pm_clk = SC_PM_CLK_PER;
168 break;
169 case IMX8QXP_I2C3_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +0100170 case IMX8QXP_I2C3_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000171 resource = SC_R_I2C_3;
172 pm_clk = SC_PM_CLK_PER;
173 break;
174 case IMX8QXP_UART0_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300175 case IMX8QXP_UART0_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000176 resource = SC_R_UART_0;
177 pm_clk = SC_PM_CLK_PER;
178 break;
179 case IMX8QXP_UART1_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300180 case IMX8QXP_UART1_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000181 resource = SC_R_UART_1;
182 pm_clk = SC_PM_CLK_PER;
183 break;
184 case IMX8QXP_UART2_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300185 case IMX8QXP_UART2_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000186 resource = SC_R_UART_2;
187 pm_clk = SC_PM_CLK_PER;
188 break;
189 case IMX8QXP_UART3_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300190 case IMX8QXP_UART3_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000191 resource = SC_R_UART_3;
192 pm_clk = SC_PM_CLK_PER;
193 break;
194 case IMX8QXP_SDHC0_IPG_CLK:
195 case IMX8QXP_SDHC0_CLK:
196 case IMX8QXP_SDHC0_DIV:
197 resource = SC_R_SDHC_0;
198 pm_clk = SC_PM_CLK_PER;
199 break;
200 case IMX8QXP_SDHC1_SEL:
201 case IMX8QXP_SDHC0_SEL:
202 return 0;
203 case IMX8QXP_SDHC1_IPG_CLK:
204 case IMX8QXP_SDHC1_CLK:
205 case IMX8QXP_SDHC1_DIV:
206 resource = SC_R_SDHC_1;
207 pm_clk = SC_PM_CLK_PER;
208 break;
209 case IMX8QXP_ENET0_IPG_CLK:
210 case IMX8QXP_ENET0_AHB_CLK:
211 case IMX8QXP_ENET0_REF_DIV:
212 case IMX8QXP_ENET0_PTP_CLK:
213 resource = SC_R_ENET_0;
214 pm_clk = SC_PM_CLK_PER;
215 break;
216 case IMX8QXP_ENET1_IPG_CLK:
217 case IMX8QXP_ENET1_AHB_CLK:
218 case IMX8QXP_ENET1_REF_DIV:
219 case IMX8QXP_ENET1_PTP_CLK:
220 resource = SC_R_ENET_1;
221 pm_clk = SC_PM_CLK_PER;
222 break;
223 default:
224 if (clk->id < IMX8QXP_UART0_IPG_CLK ||
225 clk->id >= IMX8QXP_CLK_END) {
226 printf("%s(Invalid clk ID #%lu)\n",
227 __func__, clk->id);
228 return -EINVAL;
229 }
Simon Glass29ff16a2021-03-25 10:26:08 +1300230 return -EINVAL;
Peng Fan6a8e5f92019-03-05 02:32:33 +0000231 };
232
233 ret = sc_pm_set_clock_rate(-1, resource, pm_clk, &new_rate);
234 if (ret) {
235 printf("%s err %d\n", __func__, ret);
236 return ret;
237 }
238
239 return new_rate;
240}
241
242int __imx8_clk_enable(struct clk *clk, bool enable)
243{
244 sc_pm_clk_t pm_clk;
245 u16 resource;
246 int ret;
247
248 debug("%s(#%lu)\n", __func__, clk->id);
249
250 switch (clk->id) {
251 case IMX8QXP_I2C0_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +0100252 case IMX8QXP_I2C0_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000253 resource = SC_R_I2C_0;
254 pm_clk = SC_PM_CLK_PER;
255 break;
256 case IMX8QXP_I2C1_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +0100257 case IMX8QXP_I2C1_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000258 resource = SC_R_I2C_1;
259 pm_clk = SC_PM_CLK_PER;
260 break;
261 case IMX8QXP_I2C2_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +0100262 case IMX8QXP_I2C2_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000263 resource = SC_R_I2C_2;
264 pm_clk = SC_PM_CLK_PER;
265 break;
266 case IMX8QXP_I2C3_CLK:
Anatolij Gustschinb9a1e512020-01-07 14:03:03 +0100267 case IMX8QXP_I2C3_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000268 resource = SC_R_I2C_3;
269 pm_clk = SC_PM_CLK_PER;
270 break;
271 case IMX8QXP_UART0_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300272 case IMX8QXP_UART0_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000273 resource = SC_R_UART_0;
274 pm_clk = SC_PM_CLK_PER;
275 break;
276 case IMX8QXP_UART1_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300277 case IMX8QXP_UART1_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000278 resource = SC_R_UART_1;
279 pm_clk = SC_PM_CLK_PER;
280 break;
281 case IMX8QXP_UART2_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300282 case IMX8QXP_UART2_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000283 resource = SC_R_UART_2;
284 pm_clk = SC_PM_CLK_PER;
285 break;
286 case IMX8QXP_UART3_CLK:
Fabio Estevamb08666d2024-03-08 17:13:15 -0300287 case IMX8QXP_UART3_IPG_CLK:
Peng Fan6a8e5f92019-03-05 02:32:33 +0000288 resource = SC_R_UART_3;
289 pm_clk = SC_PM_CLK_PER;
290 break;
291 case IMX8QXP_SDHC0_IPG_CLK:
292 case IMX8QXP_SDHC0_CLK:
293 case IMX8QXP_SDHC0_DIV:
294 resource = SC_R_SDHC_0;
295 pm_clk = SC_PM_CLK_PER;
296 break;
297 case IMX8QXP_SDHC1_IPG_CLK:
298 case IMX8QXP_SDHC1_CLK:
299 case IMX8QXP_SDHC1_DIV:
300 resource = SC_R_SDHC_1;
301 pm_clk = SC_PM_CLK_PER;
302 break;
303 case IMX8QXP_ENET0_IPG_CLK:
304 case IMX8QXP_ENET0_AHB_CLK:
305 case IMX8QXP_ENET0_REF_DIV:
306 case IMX8QXP_ENET0_PTP_CLK:
307 resource = SC_R_ENET_0;
308 pm_clk = SC_PM_CLK_PER;
309 break;
310 case IMX8QXP_ENET1_IPG_CLK:
311 case IMX8QXP_ENET1_AHB_CLK:
312 case IMX8QXP_ENET1_REF_DIV:
313 case IMX8QXP_ENET1_PTP_CLK:
314 resource = SC_R_ENET_1;
315 pm_clk = SC_PM_CLK_PER;
316 break;
317 default:
318 if (clk->id < IMX8QXP_UART0_IPG_CLK ||
319 clk->id >= IMX8QXP_CLK_END) {
320 printf("%s(Invalid clk ID #%lu)\n",
321 __func__, clk->id);
322 return -EINVAL;
323 }
Simon Glass29ff16a2021-03-25 10:26:08 +1300324 return -EINVAL;
Peng Fan6a8e5f92019-03-05 02:32:33 +0000325 }
326
327 ret = sc_pm_clock_enable(-1, resource, pm_clk, enable, 0);
328 if (ret) {
329 printf("%s err %d\n", __func__, ret);
330 return ret;
331 }
332
333 return 0;
334}