blob: 870ff5b1e0983dd2e72a02a7194792cf27b71ed0 [file] [log] [blame]
Chen-Yu Tsai3a045422014-10-03 20:16:25 +08001/*
2 * sun6i specific clock code
3 *
4 * (C) Copyright 2007-2012
5 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
6 * Tom Cubie <tangliang@allwinnertech.com>
7 *
8 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 */
12
13#include <common.h>
14#include <asm/io.h>
15#include <asm/arch/clock.h>
Chen-Yu Tsai6ee63882014-10-22 16:47:47 +080016#include <asm/arch/prcm.h>
Chen-Yu Tsai3a045422014-10-03 20:16:25 +080017#include <asm/arch/sys_proto.h>
18
Hans de Goedec27d68d2014-10-25 20:16:33 +020019#ifdef CONFIG_SPL_BUILD
20void clock_init_safe(void)
21{
22 struct sunxi_ccm_reg * const ccm =
23 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Andre Przywara79b59ef2017-01-02 11:48:25 +000024
Andre Przywara5fb97432017-02-16 01:20:27 +000025#if !defined(CONFIG_MACH_SUNXI_H3_H5) && !defined(CONFIG_MACH_SUN50I)
Hans de Goedec27d68d2014-10-25 20:16:33 +020026 struct sunxi_prcm_reg * const prcm =
27 (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
28
29 /* Set PLL ldo voltage without this PLL6 does not work properly */
30 clrsetbits_le32(&prcm->pll_ctrl1, PRCM_PLL_CTRL_LDO_KEY_MASK,
31 PRCM_PLL_CTRL_LDO_KEY);
32 clrsetbits_le32(&prcm->pll_ctrl1, ~PRCM_PLL_CTRL_LDO_KEY_MASK,
33 PRCM_PLL_CTRL_LDO_DIGITAL_EN | PRCM_PLL_CTRL_LDO_ANALOG_EN |
34 PRCM_PLL_CTRL_EXT_OSC_EN | PRCM_PLL_CTRL_LDO_OUT_L(1140));
35 clrbits_le32(&prcm->pll_ctrl1, PRCM_PLL_CTRL_LDO_KEY_MASK);
Andre Przywara79b59ef2017-01-02 11:48:25 +000036#endif
Hans de Goedec27d68d2014-10-25 20:16:33 +020037
Jernej Skrabec9b4ca922017-03-27 19:22:31 +020038#if defined(CONFIG_MACH_SUN8I_R40) || defined(CONFIG_MACH_SUN50I)
Chen-Yu Tsai5eddcbb2016-11-30 16:54:34 +080039 /* Set PLL lock enable bits and switch to old lock mode */
40 writel(GENMASK(12, 0), &ccm->pll_lock_ctrl);
41#endif
42
Hans de Goedec27d68d2014-10-25 20:16:33 +020043 clock_set_pll1(408000000);
44
Hans de Goedec27d68d2014-10-25 20:16:33 +020045 writel(PLL6_CFG_DEFAULT, &ccm->pll6_cfg);
Siarhei Siamashka2b8bd912015-11-20 07:07:48 +020046 while (!(readl(&ccm->pll6_cfg) & CCM_PLL6_CTRL_LOCK))
47 ;
48
49 writel(AHB1_ABP1_DIV_DEFAULT, &ccm->ahb1_apb1_div);
Hans de Goedec27d68d2014-10-25 20:16:33 +020050
51 writel(MBUS_CLK_DEFAULT, &ccm->mbus0_clk_cfg);
Andre Przywara79b59ef2017-01-02 11:48:25 +000052 if (IS_ENABLED(CONFIG_MACH_SUN6I))
53 writel(MBUS_CLK_DEFAULT, &ccm->mbus1_clk_cfg);
Icenowy Zheng32796612017-05-01 14:31:56 +080054
55#if defined(CONFIG_MACH_SUN8I_R40) && defined(CONFIG_SUNXI_AHCI)
56 setbits_le32(&ccm->sata_pll_cfg, CCM_SATA_PLL_DEFAULT);
57 setbits_le32(&ccm->ahb_reset0_cfg, 0x1 << AHB_GATE_OFFSET_SATA);
58 setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_SATA);
59 setbits_le32(&ccm->sata_clk_cfg, CCM_SATA_CTRL_ENABLE);
60#endif
Hans de Goedec27d68d2014-10-25 20:16:33 +020061}
62#endif
63
Chen-Yu Tsai6daddfe2016-01-06 15:13:07 +080064void clock_init_sec(void)
65{
Andre Przywara5fb97432017-02-16 01:20:27 +000066#ifdef CONFIG_MACH_SUNXI_H3_H5
Chen-Yu Tsai6daddfe2016-01-06 15:13:07 +080067 struct sunxi_ccm_reg * const ccm =
68 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Icenowy Zheng883b3c02017-07-20 14:00:32 +080069 struct sunxi_prcm_reg * const prcm =
70 (struct sunxi_prcm_reg *)SUNXI_PRCM_BASE;
Chen-Yu Tsai6daddfe2016-01-06 15:13:07 +080071
72 setbits_le32(&ccm->ccu_sec_switch,
73 CCM_SEC_SWITCH_MBUS_NONSEC |
74 CCM_SEC_SWITCH_BUS_NONSEC |
75 CCM_SEC_SWITCH_PLL_NONSEC);
Icenowy Zheng883b3c02017-07-20 14:00:32 +080076 setbits_le32(&prcm->prcm_sec_switch,
77 PRCM_SEC_SWITCH_APB0_CLK_NONSEC |
78 PRCM_SEC_SWITCH_PLL_CFG_NONSEC |
79 PRCM_SEC_SWITCH_PWR_GATE_NONSEC);
Chen-Yu Tsai6daddfe2016-01-06 15:13:07 +080080#endif
81}
82
Chen-Yu Tsai3a045422014-10-03 20:16:25 +080083void clock_init_uart(void)
84{
Hans de Goede627bc692015-01-14 19:28:38 +010085#if CONFIG_CONS_INDEX < 5
Chen-Yu Tsai3a045422014-10-03 20:16:25 +080086 struct sunxi_ccm_reg *const ccm =
87 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
88
89 /* uart clock source is apb2 */
90 writel(APB2_CLK_SRC_OSC24M|
91 APB2_CLK_RATE_N_1|
92 APB2_CLK_RATE_M(1),
93 &ccm->apb2_div);
94
95 /* open the clock for uart */
96 setbits_le32(&ccm->apb2_gate,
97 CLK_GATE_OPEN << (APB2_GATE_UART_SHIFT +
98 CONFIG_CONS_INDEX - 1));
99
100 /* deassert uart reset */
101 setbits_le32(&ccm->apb2_reset_cfg,
102 1 << (APB2_RESET_UART_SHIFT +
103 CONFIG_CONS_INDEX - 1));
Chen-Yu Tsai6ee63882014-10-22 16:47:47 +0800104#else
105 /* enable R_PIO and R_UART clocks, and de-assert resets */
106 prcm_apb0_enable(PRCM_APB0_GATE_PIO | PRCM_APB0_GATE_UART);
107#endif
Chen-Yu Tsai3a045422014-10-03 20:16:25 +0800108}
109
Hans de Goedec27d68d2014-10-25 20:16:33 +0200110#ifdef CONFIG_SPL_BUILD
111void clock_set_pll1(unsigned int clk)
112{
113 struct sunxi_ccm_reg * const ccm =
114 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede645d4d52014-12-27 17:56:59 +0100115 const int p = 0;
Hans de Goedec27d68d2014-10-25 20:16:33 +0200116 int k = 1;
117 int m = 1;
118
119 if (clk > 1152000000) {
120 k = 2;
121 } else if (clk > 768000000) {
122 k = 3;
123 m = 2;
124 }
125
126 /* Switch to 24MHz clock while changing PLL1 */
127 writel(AXI_DIV_3 << AXI_DIV_SHIFT |
128 ATB_DIV_2 << ATB_DIV_SHIFT |
129 CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT,
130 &ccm->cpu_axi_cfg);
131
Hans de Goede645d4d52014-12-27 17:56:59 +0100132 /*
133 * sun6i: PLL1 rate = ((24000000 * n * k) >> 0) / m (p is ignored)
134 * sun8i: PLL1 rate = ((24000000 * n * k) >> p) / m
135 */
136 writel(CCM_PLL1_CTRL_EN | CCM_PLL1_CTRL_P(p) |
Hans de Goedec27d68d2014-10-25 20:16:33 +0200137 CCM_PLL1_CTRL_N(clk / (24000000 * k / m)) |
138 CCM_PLL1_CTRL_K(k) | CCM_PLL1_CTRL_M(m), &ccm->pll1_cfg);
139 sdelay(200);
140
141 /* Switch CPU to PLL1 */
142 writel(AXI_DIV_3 << AXI_DIV_SHIFT |
143 ATB_DIV_2 << ATB_DIV_SHIFT |
144 CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT,
145 &ccm->cpu_axi_cfg);
146}
147#endif
148
Hans de Goede70d7ab52014-11-08 14:07:27 +0100149void clock_set_pll3(unsigned int clk)
150{
151 struct sunxi_ccm_reg * const ccm =
152 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
153 const int m = 8; /* 3 MHz steps just like sun4i, sun5i and sun7i */
154
155 if (clk == 0) {
156 clrbits_le32(&ccm->pll3_cfg, CCM_PLL3_CTRL_EN);
157 return;
158 }
159
160 /* PLL3 rate = 24000000 * n / m */
161 writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE |
162 CCM_PLL3_CTRL_N(clk / (24000000 / m)) | CCM_PLL3_CTRL_M(m),
163 &ccm->pll3_cfg);
164}
165
Jernej Skrabec9b4ca922017-03-27 19:22:31 +0200166#ifdef CONFIG_SUNXI_DE2
167void clock_set_pll3_factors(int m, int n)
168{
169 struct sunxi_ccm_reg * const ccm =
170 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
171
172 /* PLL3 rate = 24000000 * n / m */
173 writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE |
174 CCM_PLL3_CTRL_N(n) | CCM_PLL3_CTRL_M(m),
175 &ccm->pll3_cfg);
176
177 while (!(readl(&ccm->pll3_cfg) & CCM_PLL3_CTRL_LOCK))
178 ;
179}
180#endif
181
Hans de Goede0cbc4cb2014-11-30 11:58:17 +0100182void clock_set_pll5(unsigned int clk, bool sigma_delta_enable)
Hans de Goedec27d68d2014-10-25 20:16:33 +0200183{
184 struct sunxi_ccm_reg * const ccm =
185 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Hans de Goede0bfa7742014-12-07 21:09:31 +0100186 const int max_n = 32;
187 int k = 1, m = 2;
Hans de Goedec27d68d2014-10-25 20:16:33 +0200188
Andre Przywara5fb97432017-02-16 01:20:27 +0000189#ifdef CONFIG_MACH_SUNXI_H3_H5
Jens Kuske213407e2016-08-19 13:40:46 +0200190 clrsetbits_le32(&ccm->pll5_tuning_cfg, CCM_PLL5_TUN_LOCK_TIME_MASK |
191 CCM_PLL5_TUN_INIT_FREQ_MASK,
192 CCM_PLL5_TUN_LOCK_TIME(2) | CCM_PLL5_TUN_INIT_FREQ(16));
193#endif
194
Hans de Goede0cbc4cb2014-11-30 11:58:17 +0100195 if (sigma_delta_enable)
196 writel(CCM_PLL5_PATTERN, &ccm->pll5_pattern_cfg);
197
Hans de Goedec27d68d2014-10-25 20:16:33 +0200198 /* PLL5 rate = 24000000 * n * k / m */
Hans de Goede0bfa7742014-12-07 21:09:31 +0100199 if (clk > 24000000 * k * max_n / m) {
200 m = 1;
201 if (clk > 24000000 * k * max_n / m)
202 k = 2;
203 }
Hans de Goede0cbc4cb2014-11-30 11:58:17 +0100204 writel(CCM_PLL5_CTRL_EN |
205 (sigma_delta_enable ? CCM_PLL5_CTRL_SIGMA_DELTA_EN : 0) |
206 CCM_PLL5_CTRL_UPD |
Hans de Goedec27d68d2014-10-25 20:16:33 +0200207 CCM_PLL5_CTRL_N(clk / (24000000 * k / m)) |
208 CCM_PLL5_CTRL_K(k) | CCM_PLL5_CTRL_M(m), &ccm->pll5_cfg);
209
210 udelay(5500);
211}
212
Hans de Goeded6eaadc2015-08-08 14:05:35 +0200213#ifdef CONFIG_MACH_SUN6I
214void clock_set_mipi_pll(unsigned int clk)
215{
216 struct sunxi_ccm_reg * const ccm =
217 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
218 unsigned int k, m, n, value, diff;
219 unsigned best_k = 0, best_m = 0, best_n = 0, best_diff = 0xffffffff;
220 unsigned int src = clock_get_pll3();
221
222 /* All calculations are in KHz to avoid overflows */
223 clk /= 1000;
224 src /= 1000;
225
226 /* Pick the closest lower clock */
227 for (k = 1; k <= 4; k++) {
228 for (m = 1; m <= 16; m++) {
229 for (n = 1; n <= 16; n++) {
230 value = src * n * k / m;
231 if (value > clk)
232 continue;
233
234 diff = clk - value;
235 if (diff < best_diff) {
236 best_diff = diff;
237 best_k = k;
238 best_m = m;
239 best_n = n;
240 }
241 if (diff == 0)
242 goto done;
243 }
244 }
245 }
246
247done:
248 writel(CCM_MIPI_PLL_CTRL_EN | CCM_MIPI_PLL_CTRL_LDO_EN |
249 CCM_MIPI_PLL_CTRL_N(best_n) | CCM_MIPI_PLL_CTRL_K(best_k) |
250 CCM_MIPI_PLL_CTRL_M(best_m), &ccm->mipi_pll_cfg);
251}
252#endif
253
Jernej Skrabec9b4ca922017-03-27 19:22:31 +0200254#ifdef CONFIG_SUNXI_DE2
255void clock_set_pll10(unsigned int clk)
256{
257 struct sunxi_ccm_reg * const ccm =
258 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
259 const int m = 2; /* 12 MHz steps */
260
261 if (clk == 0) {
262 clrbits_le32(&ccm->pll10_cfg, CCM_PLL10_CTRL_EN);
263 return;
264 }
265
266 /* PLL10 rate = 24000000 * n / m */
267 writel(CCM_PLL10_CTRL_EN | CCM_PLL10_CTRL_INTEGER_MODE |
268 CCM_PLL10_CTRL_N(clk / (24000000 / m)) | CCM_PLL10_CTRL_M(m),
269 &ccm->pll10_cfg);
270
271 while (!(readl(&ccm->pll10_cfg) & CCM_PLL10_CTRL_LOCK))
272 ;
273}
274#endif
275
Chen-Yu Tsai143ef792016-12-01 19:09:57 +0800276#if defined(CONFIG_MACH_SUN8I_A33) || \
277 defined(CONFIG_MACH_SUN8I_R40) || \
278 defined(CONFIG_MACH_SUN50I)
Hans de Goede0fdbe202015-04-12 11:46:41 +0200279void clock_set_pll11(unsigned int clk, bool sigma_delta_enable)
280{
281 struct sunxi_ccm_reg * const ccm =
282 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
283
284 if (sigma_delta_enable)
Philipp Tomsichced4a9a2017-01-02 11:48:41 +0000285 writel(CCM_PLL11_PATTERN, &ccm->pll11_pattern_cfg0);
Hans de Goede0fdbe202015-04-12 11:46:41 +0200286
287 writel(CCM_PLL11_CTRL_EN | CCM_PLL11_CTRL_UPD |
288 (sigma_delta_enable ? CCM_PLL11_CTRL_SIGMA_DELTA_EN : 0) |
289 CCM_PLL11_CTRL_N(clk / 24000000), &ccm->pll11_cfg);
290
291 while (readl(&ccm->pll11_cfg) & CCM_PLL11_CTRL_UPD)
292 ;
293}
294#endif
295
Hans de Goede957a727292015-08-08 12:36:44 +0200296unsigned int clock_get_pll3(void)
297{
298 struct sunxi_ccm_reg *const ccm =
299 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
300 uint32_t rval = readl(&ccm->pll3_cfg);
301 int n = ((rval & CCM_PLL3_CTRL_N_MASK) >> CCM_PLL3_CTRL_N_SHIFT) + 1;
302 int m = ((rval & CCM_PLL3_CTRL_M_MASK) >> CCM_PLL3_CTRL_M_SHIFT) + 1;
303
304 /* Multiply by 1000 after dividing by m to avoid integer overflows */
305 return (24000 * n / m) * 1000;
306}
307
Chen-Yu Tsai3a045422014-10-03 20:16:25 +0800308unsigned int clock_get_pll6(void)
309{
310 struct sunxi_ccm_reg *const ccm =
311 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
312 uint32_t rval = readl(&ccm->pll6_cfg);
313 int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT) + 1;
314 int k = ((rval & CCM_PLL6_CTRL_K_MASK) >> CCM_PLL6_CTRL_K_SHIFT) + 1;
315 return 24000000 * n * k / 2;
316}
Hans de Goede70d7ab52014-11-08 14:07:27 +0100317
Hans de Goeded6eaadc2015-08-08 14:05:35 +0200318unsigned int clock_get_mipi_pll(void)
319{
320 struct sunxi_ccm_reg *const ccm =
321 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
322 uint32_t rval = readl(&ccm->mipi_pll_cfg);
323 unsigned int n = ((rval & CCM_MIPI_PLL_CTRL_N_MASK) >> CCM_MIPI_PLL_CTRL_N_SHIFT) + 1;
324 unsigned int k = ((rval & CCM_MIPI_PLL_CTRL_K_MASK) >> CCM_MIPI_PLL_CTRL_K_SHIFT) + 1;
325 unsigned int m = ((rval & CCM_MIPI_PLL_CTRL_M_MASK) >> CCM_MIPI_PLL_CTRL_M_SHIFT) + 1;
326 unsigned int src = clock_get_pll3();
327
328 /* Multiply by 1000 after dividing by m to avoid integer overflows */
329 return ((src / 1000) * n * k / m) * 1000;
330}
331
Hans de Goede70d7ab52014-11-08 14:07:27 +0100332void clock_set_de_mod_clock(u32 *clk_cfg, unsigned int hz)
333{
334 int pll = clock_get_pll6() * 2;
335 int div = 1;
336
337 while ((pll / div) > hz)
338 div++;
339
340 writel(CCM_DE_CTRL_GATE | CCM_DE_CTRL_PLL6_2X | CCM_DE_CTRL_M(div),
341 clk_cfg);
342}