blob: 8f1d1b65f007e19dbc357260efcd0c85c9d35307 [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
16#ifdef CONFIG_SPL_BUILD
17void 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);
28 writel(PLL1_CFG_DEFAULT, &ccm->pll1_cfg);
29 sdelay(200);
30 writel(AXI_DIV_1 << AXI_DIV_SHIFT |
31 AHB_DIV_2 << AHB_DIV_SHIFT |
32 APB0_DIV_1 << APB0_DIV_SHIFT |
33 CPU_CLK_SRC_PLL1 << CPU_CLK_SRC_SHIFT,
34 &ccm->cpu_ahb_apb0_cfg);
Ian Campbell8f32aaa2014-10-24 21:20:47 +010035#ifdef CONFIG_MACH_SUN7I
Ian Campbell504166e2014-06-05 19:00:16 +010036 setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_DMA);
Ian Campbell49aeca32014-05-05 11:52:23 +010037#endif
38 writel(PLL6_CFG_DEFAULT, &ccm->pll6_cfg);
Ian Campbella2ebf922014-07-18 20:38:41 +010039#ifdef CONFIG_SUNXI_AHCI
40 setbits_le32(&ccm->ahb_gate0, 0x1 << AHB_GATE_OFFSET_SATA);
41 setbits_le32(&ccm->pll6_cfg, 0x1 << CCM_PLL6_CTRL_SATA_EN_SHIFT);
42#endif
Ian Campbell49aeca32014-05-05 11:52:23 +010043}
44#endif
45
46void clock_init_uart(void)
47{
48 struct sunxi_ccm_reg *const ccm =
49 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
50
51 /* uart clock source is apb1 */
52 writel(APB1_CLK_SRC_OSC24M|
53 APB1_CLK_RATE_N_1|
54 APB1_CLK_RATE_M(1),
55 &ccm->apb1_clk_div_cfg);
56
57 /* open the clock for uart */
58 setbits_le32(&ccm->apb1_gate,
Olliver Schinaglfc67dbc2015-12-03 17:49:29 +010059 CLK_GATE_OPEN << (APB1_GATE_UART_SHIFT+CONFIG_CONS_INDEX - 1));
Ian Campbell49aeca32014-05-05 11:52:23 +010060}
61
62int clock_twi_onoff(int port, int state)
63{
64 struct sunxi_ccm_reg *const ccm =
65 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
66
Ian Campbell49aeca32014-05-05 11:52:23 +010067 /* set the apb clock gate for twi */
68 if (state)
69 setbits_le32(&ccm->apb1_gate,
Olliver Schinaglfc67dbc2015-12-03 17:49:29 +010070 CLK_GATE_OPEN << (APB1_GATE_TWI_SHIFT + port));
Ian Campbell49aeca32014-05-05 11:52:23 +010071 else
72 clrbits_le32(&ccm->apb1_gate,
Olliver Schinaglfc67dbc2015-12-03 17:49:29 +010073 CLK_GATE_OPEN << (APB1_GATE_TWI_SHIFT + port));
Ian Campbell49aeca32014-05-05 11:52:23 +010074
75 return 0;
76}
77
78#ifdef CONFIG_SPL_BUILD
79#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}
178#endif
179
Hans de Goede70d7ab52014-11-08 14:07:27 +0100180void clock_set_pll3(unsigned int clk)
181{
182 struct sunxi_ccm_reg * const ccm =
183 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
184
185 if (clk == 0) {
186 clrbits_le32(&ccm->pll3_cfg, CCM_PLL3_CTRL_EN);
187 return;
188 }
189
190 /* PLL3 rate = 3000000 * m */
191 writel(CCM_PLL3_CTRL_EN | CCM_PLL3_CTRL_INTEGER_MODE |
192 CCM_PLL3_CTRL_M(clk / 3000000), &ccm->pll3_cfg);
193}
194
Hans de Goede957a727292015-08-08 12:36:44 +0200195unsigned int clock_get_pll3(void)
196{
197 struct sunxi_ccm_reg *const ccm =
198 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
199 uint32_t rval = readl(&ccm->pll3_cfg);
200 int m = ((rval & CCM_PLL3_CTRL_M_MASK) >> CCM_PLL3_CTRL_M_SHIFT);
201 return 3000000 * m;
202}
203
Hans de Goede9f072732014-10-22 14:42:48 +0200204unsigned int clock_get_pll5p(void)
205{
206 struct sunxi_ccm_reg *const ccm =
207 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
208 uint32_t rval = readl(&ccm->pll5_cfg);
209 int n = ((rval & CCM_PLL5_CTRL_N_MASK) >> CCM_PLL5_CTRL_N_SHIFT);
210 int k = ((rval & CCM_PLL5_CTRL_K_MASK) >> CCM_PLL5_CTRL_K_SHIFT) + 1;
211 int p = ((rval & CCM_PLL5_CTRL_P_MASK) >> CCM_PLL5_CTRL_P_SHIFT);
212 return (24000000 * n * k) >> p;
213}
214
Ian Campbell49aeca32014-05-05 11:52:23 +0100215unsigned int clock_get_pll6(void)
216{
217 struct sunxi_ccm_reg *const ccm =
218 (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
219 uint32_t rval = readl(&ccm->pll6_cfg);
220 int n = ((rval & CCM_PLL6_CTRL_N_MASK) >> CCM_PLL6_CTRL_N_SHIFT);
221 int k = ((rval & CCM_PLL6_CTRL_K_MASK) >> CCM_PLL6_CTRL_K_SHIFT) + 1;
222 return 24000000 * n * k / 2;
223}
Hans de Goede70d7ab52014-11-08 14:07:27 +0100224
225void clock_set_de_mod_clock(u32 *clk_cfg, unsigned int hz)
226{
227 int pll = clock_get_pll5p();
228 int div = 1;
229
230 while ((pll / div) > hz)
231 div++;
232
233 writel(CCM_DE_CTRL_GATE | CCM_DE_CTRL_RST | CCM_DE_CTRL_PLL5P |
234 CCM_DE_CTRL_M(div), clk_cfg);
235}