blob: 1367b439b83cf70f57a09ae35a8512014229ebc0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ian Campbell49aeca32014-05-05 11:52:23 +01002/*
3 * sun4i, sun5i and sun7i specific clock code
4 *
5 * (C) Copyright 2007-2012
6 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
7 * Tom Cubie <tangliang@allwinnertech.com>
8 *
9 * (C) Copyright 2013 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
Ian Campbell49aeca32014-05-05 11:52:23 +010010 */
11
Ian Campbell49aeca32014-05-05 11:52:23 +010012#include <asm/io.h>
13#include <asm/arch/clock.h>
Ian Campbell49aeca32014-05-05 11:52:23 +010014#include <asm/arch/sys_proto.h>
15
Simon Glass85ed77d2024-09-29 19:49:46 -060016#ifdef CONFIG_XPL_BUILD
Ian Campbell49aeca32014-05-05 11:52:23 +010017void clock_init_safe(void)
18{
19 struct sunxi_ccm_reg * const ccm =
20 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
21
22 /* Set safe defaults until PMU is configured */
23 writel(AXI_DIV_1 << AXI_DIV_SHIFT |
24 AHB_DIV_2 << AHB_DIV_SHIFT |
25 APB0_DIV_1 << APB0_DIV_SHIFT |
26 CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT,
27 &ccm->cpu_ahb_apb0_cfg);
Ludwig Kormannb2d4e4b2024-02-01 09:45:50 +010028 sdelay(20);
Ian Campbell49aeca32014-05-05 11:52:23 +010029 writel(PLL1_CFG_DEFAULT, &ccm->pll1_cfg);
30 sdelay(200);
31 writel(AXI_DIV_1 << AXI_DIV_SHIFT |
32 AHB_DIV_2 << AHB_DIV_SHIFT |
33 APB0_DIV_1 << APB0_DIV_SHIFT |
34 CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT,
35 &ccm->cpu_ahb_apb0_cfg);
Ludwig Kormannb2d4e4b2024-02-01 09:45:50 +010036 sdelay(20);
Ian Campbell8f32aaa2014-10-24 21:20:47 +010037#ifdef CONFIG_MACH_SUN7I
Ian Campbell504166e2014-06-05 19:00:16 +010038 setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_DMA);
Ian Campbell49aeca32014-05-05 11:52:23 +010039#endif
40 writel(PLL6_CFG_DEFAULT, &ccm->pll6_cfg);
Ian Campbella2ebf922014-07-18 20:38:41 +010041#ifdef CONFIG_SUNXI_AHCI
42 setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_SATA);
43 setbits_le32(&ccm->pll6_cfg, 0x1 << CCM_PLL6_CTRL_SATA_EN_SHIFT);
44#endif
Ian Campbell49aeca32014-05-05 11:52:23 +010045}
Ian Campbell49aeca32014-05-05 11:52:23 +010046
47void clock_init_uart(void)
48{
49 struct sunxi_ccm_reg *const ccm =
50 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
51
52 /* uart clock source is apb1 */
53 writel(APB1_CLK_SRC_OSC24M|
54 APB1_CLK_RATE_N_1|
55 APB1_CLK_RATE_M(1),
56 &ccm->apb1_clk_div_cfg);
57
58 /* open the clock for uart */
59 setbits_le32(&ccm->apb1_gate,
Olliver Schinaglfc67dbc2015-12-03 17:49:29 +010060 CLK_GATE_OPEN << (APB1_GATE_UART_SHIFT+CONFIG_CONS_INDEX - 1));
Ian Campbell49aeca32014-05-05 11:52:23 +010061}
62
63int clock_twi_onoff(int port, int state)
64{
65 struct sunxi_ccm_reg *const ccm =
66 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
67
Ian Campbell49aeca32014-05-05 11:52:23 +010068 /* set the apb clock gate for twi */
69 if (state)
70 setbits_le32(&ccm->apb1_gate,
Olliver Schinaglfc67dbc2015-12-03 17:49:29 +010071 CLK_GATE_OPEN << (APB1_GATE_TWI_SHIFT + port));
Ian Campbell49aeca32014-05-05 11:52:23 +010072 else
73 clrbits_le32(&ccm->apb1_gate,
Olliver Schinaglfc67dbc2015-12-03 17:49:29 +010074 CLK_GATE_OPEN << (APB1_GATE_TWI_SHIFT + port));
Ian Campbell49aeca32014-05-05 11:52:23 +010075
76 return 0;
77}
78
Ian Campbell49aeca32014-05-05 11:52:23 +010079#define PLL1_CFG(N, K, M, P) ( 1 << CCM_PLL1_CFG_ENABLE_SHIFT | \
80 0 << CCM_PLL1_CFG_VCO_RST_SHIFT | \
81 8 << CCM_PLL1_CFG_VCO_BIAS_SHIFT | \
82 0 << CCM_PLL1_CFG_PLL4_EXCH_SHIFT | \
83 16 << CCM_PLL1_CFG_BIAS_CUR_SHIFT | \
84 (P)<< CCM_PLL1_CFG_DIVP_SHIFT | \
85 2 << CCM_PLL1_CFG_LCK_TMR_SHIFT | \
86 (N)<< CCM_PLL1_CFG_FACTOR_N_SHIFT | \
87 (K)<< CCM_PLL1_CFG_FACTOR_K_SHIFT | \
88 0 << CCM_PLL1_CFG_SIG_DELT_PAT_IN_SHIFT | \
89 0 << CCM_PLL1_CFG_SIG_DELT_PAT_EN_SHIFT | \
90 (M)<< CCM_PLL1_CFG_FACTOR_M_SHIFT)
91
92static struct {
93 u32 pll1_cfg;
94 unsigned int freq;
95} pll1_para[] = {
96 /* This array must be ordered by frequency. */
Ian Campbell49aeca32014-05-05 11:52:23 +010097 { PLL1_CFG(31, 1, 0, 0), 1488000000},
Iain Patonc4b9dee2015-03-28 10:25:28 +000098 { PLL1_CFG(30, 1, 0, 0), 1440000000},
99 { PLL1_CFG(29, 1, 0, 0), 1392000000},
100 { PLL1_CFG(28, 1, 0, 0), 1344000000},
101 { PLL1_CFG(27, 1, 0, 0), 1296000000},
102 { PLL1_CFG(26, 1, 0, 0), 1248000000},
103 { PLL1_CFG(25, 1, 0, 0), 1200000000},
104 { PLL1_CFG(24, 1, 0, 0), 1152000000},
105 { PLL1_CFG(23, 1, 0, 0), 1104000000},
106 { PLL1_CFG(22, 1, 0, 0), 1056000000},
107 { PLL1_CFG(21, 1, 0, 0), 1008000000},
108 { PLL1_CFG(20, 1, 0, 0), 960000000 },
109 { PLL1_CFG(19, 1, 0, 0), 912000000 },
110 { PLL1_CFG(16, 1, 0, 0), 768000000 },
111 /* Final catchall entry 384MHz*/
112 { PLL1_CFG(16, 0, 0, 0), 0 },
113
Ian Campbell49aeca32014-05-05 11:52:23 +0100114};
115
116void clock_set_pll1(unsigned int hz)
117{
118 int i = 0;
119 int axi, ahb, apb0;
120 struct sunxi_ccm_reg * const ccm =
121 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
122
123 /* Find target frequency */
Iain Patonc4b9dee2015-03-28 10:25:28 +0000124 while (pll1_para[i].freq > hz)
Ian Campbell49aeca32014-05-05 11:52:23 +0100125 i++;
126
127 hz = pll1_para[i].freq;
Iain Patonc4b9dee2015-03-28 10:25:28 +0000128 if (! hz)
129 hz = 384000000;
Ian Campbell49aeca32014-05-05 11:52:23 +0100130
131 /* Calculate system clock divisors */
132 axi = DIV_ROUND_UP(hz, 432000000); /* Max 450MHz */
133 ahb = DIV_ROUND_UP(hz/axi, 204000000); /* Max 250MHz */
134 apb0 = 2; /* Max 150MHz */
135
136 printf("CPU: %uHz, AXI/AHB/APB: %d/%d/%d\n", hz, axi, ahb, apb0);
137
138 /* Map divisors to register values */
139 axi = axi - 1;
140 if (ahb > 4)
141 ahb = 3;
142 else if (ahb > 2)
143 ahb = 2;
144 else if (ahb > 1)
145 ahb = 1;
146 else
147 ahb = 0;
148
149 apb0 = apb0 - 1;
150
151 /* Switch to 24MHz clock while changing PLL1 */
152 writel(AXI_DIV_1 << AXI_DIV_SHIFT |
153 AHB_DIV_2 << AHB_DIV_SHIFT |
154 APB0_DIV_1 << APB0_DIV_SHIFT |
155 CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT,
156 &ccm->cpu_ahb_apb0_cfg);
157 sdelay(20);
158
159 /* Configure sys clock divisors */
160 writel(axi << AXI_DIV_SHIFT |
161 ahb << AHB_DIV_SHIFT |
162 apb0 << APB0_DIV_SHIFT |
163 CPU_CLK_SRC_OSC24M << CPU_CLK_SRC_SHIFT,
164 &ccm->cpu_ahb_apb0_cfg);
165
166 /* Configure PLL1 at the desired frequency */
167 writel(pll1_para[i].pll1_cfg, &ccm->pll1_cfg);
168 sdelay(200);
169
170 /* Switch CPU to PLL1 */
171 writel(axi << AXI_DIV_SHIFT |
172 ahb << AHB_DIV_SHIFT |
173 apb0 << APB0_DIV_SHIFT |
174 CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT,
175 &ccm->cpu_ahb_apb0_cfg);
176 sdelay(20);
177}
Simon Glass85ed77d2024-09-29 19:49:46 -0600178#endif /* CONFIG_XPL_BUILD */
Ian Campbell49aeca32014-05-05 11:52:23 +0100179
Andre Przywarad6f06fb2023-12-07 16:06:51 +0000180/* video, DRAM, PLL_PERIPH clocks */
Hans de Goede70d7ab52014-11-08 14:07:27 +0100181void clock_set_pll3(unsigned int clk)
182{
183 struct sunxi_ccm_reg * const ccm =
184 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
185
186 if (clk == 0) {
187 clrbits_le32(&ccm->pll3_cfg, CCM_PLL3_CTRL_EN);
188 return;
189 }
190
191 /* PLL3 rate = 3000000 * m */
192 writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE |
193 CCM_PLL3_CTRL_M(clk / 3000000), &ccm->pll3_cfg);
194}
195
Hans de Goede957a727292015-08-08 12:36:44 +0200196unsigned int clock_get_pll3(void)
197{
198 struct sunxi_ccm_reg *const ccm =
199 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
200 uint32_t rval = readl(&ccm->pll3_cfg);
201 int m = ((rval & CCM_PLL3_CTRL_M_MASK) >> CCM_PLL3_CTRL_M_SHIFT);
202 return 3000000 * m;
203}
204
Hans de Goede9f072732014-10-22 14:42:48 +0200205unsigned int clock_get_pll5p(void)
206{
207 struct sunxi_ccm_reg *const ccm =
208 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
209 uint32_t rval = readl(&ccm->pll5_cfg);
210 int n = ((rval & CCM_PLL5_CTRL_N_MASK) >> CCM_PLL5_CTRL_N_SHIFT);
211 int k = ((rval & CCM_PLL5_CTRL_K_MASK) >> CCM_PLL5_CTRL_K_SHIFT) + 1;
212 int p = ((rval & CCM_PLL5_CTRL_P_MASK) >> CCM_PLL5_CTRL_P_SHIFT);
213 return (24000000 * n * k) >> p;
214}
215
Ian Campbell49aeca32014-05-05 11:52:23 +0100216unsigned int clock_get_pll6(void)
217{
218 struct sunxi_ccm_reg *const ccm =
219 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
220 uint32_t rval = readl(&ccm->pll6_cfg);
221 int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT);
222 int k = ((rval & CCM_PLL6_CTRL_K_MASK) >> CCM_PLL6_CTRL_K_SHIFT) + 1;
223 return 24000000 * n * k / 2;
224}
Hans de Goede70d7ab52014-11-08 14:07:27 +0100225
226void clock_set_de_mod_clock(u32 *clk_cfg, unsigned int hz)
227{
228 int pll = clock_get_pll5p();
229 int div = 1;
230
231 while ((pll / div) > hz)
232 div++;
233
234 writel(CCM_DE_CTRL_GATE | CCM_DE_CTRL_RST | CCM_DE_CTRL_PLL5P |
235 CCM_DE_CTRL_M(div), clk_cfg);
236}