blob: 09bdc253ae23a3225f94bf61394f3800aa74568b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut6a713ea2016-05-06 20:10:40 +02002/*
3 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
Marek Vasut6a713ea2016-05-06 20:10:40 +02004 */
5
6#include <common.h>
Simon Glass85d65312019-12-28 10:44:58 -07007#include <clock_legacy.h>
Simon Glassf11478f2019-12-28 10:45:07 -07008#include <hang.h>
Marek Vasut6a713ea2016-05-06 20:10:40 +02009#include <asm/io.h>
10#include <asm/addrspace.h>
11#include <asm/types.h>
12#include <mach/ar71xx_regs.h>
Wills Wangddc05522016-05-30 22:54:50 +080013#include <mach/ath79.h>
Marek Vasut6a713ea2016-05-06 20:10:40 +020014#include <wait_bit.h>
15
16DECLARE_GLOBAL_DATA_PTR;
17
18/*
19 * The math for calculating PLL:
20 * NFRAC * 2^8
21 * NINT + -------------
22 * XTAL [MHz] 2^(18 - 1)
23 * PLL [MHz] = ------------ * ----------------------
24 * REFDIV 2^OUTDIV
25 *
26 * Unfortunatelly, there is no way to reliably compute the variables.
27 * The vendor U-Boot port contains macros for various combinations of
28 * CPU PLL / DDR PLL / AHB bus speed and there is no obvious pattern
29 * in those numbers.
30 */
31struct ar934x_pll_config {
32 u8 range;
33 u8 refdiv;
34 u8 outdiv;
35 /* Index 0 is for XTAL=25MHz , Index 1 is for XTAL=40MHz */
36 u8 nint[2];
37};
38
39struct ar934x_clock_config {
40 u16 cpu_freq;
41 u16 ddr_freq;
42 u16 ahb_freq;
43
44 struct ar934x_pll_config cpu_pll;
45 struct ar934x_pll_config ddr_pll;
46};
47
48static const struct ar934x_clock_config ar934x_clock_config[] = {
49 { 300, 300, 150, { 1, 1, 1, { 24, 15 } }, { 1, 1, 1, { 24, 15 } } },
50 { 400, 200, 200, { 1, 1, 1, { 32, 20 } }, { 1, 1, 2, { 32, 20 } } },
51 { 400, 400, 200, { 0, 1, 1, { 32, 20 } }, { 0, 1, 1, { 32, 20 } } },
52 { 500, 400, 200, { 1, 1, 0, { 20, 12 } }, { 0, 1, 1, { 32, 20 } } },
53 { 533, 400, 200, { 1, 1, 0, { 21, 13 } }, { 0, 1, 1, { 32, 20 } } },
54 { 533, 500, 250, { 1, 1, 0, { 21, 13 } }, { 0, 1, 0, { 20, 12 } } },
55 { 560, 480, 240, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 19, 12 } } },
56 { 566, 400, 200, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 16, 10 } } },
57 { 566, 450, 225, { 1, 1, 0, { 22, 14 } }, { 0, 1, 1, { 36, 22 } } },
58 { 566, 475, 237, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 19, 11 } } },
59 { 566, 500, 250, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 20, 12 } } },
60 { 566, 525, 262, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 21, 13 } } },
61 { 566, 550, 275, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 22, 13 } } },
62 { 600, 266, 133, { 0, 1, 0, { 24, 15 } }, { 1, 1, 1, { 21, 16 } } },
63 { 600, 266, 200, { 0, 1, 0, { 24, 15 } }, { 1, 1, 1, { 21, 16 } } },
64 { 600, 300, 150, { 0, 1, 0, { 24, 15 } }, { 0, 1, 1, { 24, 15 } } },
65 { 600, 332, 166, { 0, 1, 0, { 24, 15 } }, { 1, 1, 1, { 26, 16 } } },
66 { 600, 332, 200, { 0, 1, 0, { 24, 15 } }, { 1, 1, 1, { 26, 16 } } },
67 { 600, 400, 200, { 0, 1, 0, { 24, 15 } }, { 0, 1, 1, { 32, 20 } } },
68 { 600, 450, 200, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 18, 20 } } },
69 { 600, 500, 250, { 0, 1, 0, { 24, 15 } }, { 1, 1, 0, { 20, 12 } } },
70 { 600, 525, 262, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 21, 20 } } },
71 { 600, 550, 275, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 22, 20 } } },
72 { 600, 575, 287, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 23, 14 } } },
73 { 600, 600, 300, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 24, 20 } } },
74 { 600, 650, 325, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 26, 20 } } },
75 { 650, 600, 300, { 0, 1, 0, { 26, 15 } }, { 0, 1, 0, { 24, 20 } } },
76 { 700, 400, 200, { 3, 1, 0, { 28, 17 } }, { 0, 1, 1, { 32, 20 } } },
77};
78
79static void ar934x_srif_pll_cfg(void __iomem *pll_reg_base, const u32 srif_val)
80{
81 u32 reg;
82 do {
83 writel(0x10810f00, pll_reg_base + 0x4);
84 writel(srif_val, pll_reg_base + 0x0);
85 writel(0xd0810f00, pll_reg_base + 0x4);
86 writel(0x03000000, pll_reg_base + 0x8);
87 writel(0xd0800f00, pll_reg_base + 0x4);
88
89 clrbits_be32(pll_reg_base + 0x8, BIT(30));
90 udelay(5);
91 setbits_be32(pll_reg_base + 0x8, BIT(30));
92 udelay(5);
93
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +010094 wait_for_bit_le32(pll_reg_base + 0xc, BIT(3), 1, 10, 0);
Marek Vasut6a713ea2016-05-06 20:10:40 +020095
96 clrbits_be32(pll_reg_base + 0x8, BIT(30));
97 udelay(5);
98
99 /* Check if CPU SRIF PLL locked. */
100 reg = readl(pll_reg_base + 0x8);
101 reg = (reg & 0x7ffff8) >> 3;
102 } while (reg >= 0x40000);
103}
104
105void ar934x_pll_init(const u16 cpu_mhz, const u16 ddr_mhz, const u16 ahb_mhz)
106{
107 void __iomem *srif_regs = map_physmem(AR934X_SRIF_BASE,
108 AR934X_SRIF_SIZE, MAP_NOCACHE);
109 void __iomem *pll_regs = map_physmem(AR71XX_PLL_BASE,
110 AR71XX_PLL_SIZE, MAP_NOCACHE);
111 const struct ar934x_pll_config *pll_cfg;
112 int i, pll_nint, pll_refdiv, xtal_40 = 0;
113 u32 reg, cpu_pll, cpu_srif, ddr_pll, ddr_srif;
114
115 /* Configure SRIF PLL with initial values. */
116 writel(0x13210f00, srif_regs + AR934X_SRIF_CPU_DPLL2_REG);
117 writel(0x03000000, srif_regs + AR934X_SRIF_CPU_DPLL3_REG);
118 writel(0x13210f00, srif_regs + AR934X_SRIF_DDR_DPLL2_REG);
119 writel(0x03000000, srif_regs + AR934X_SRIF_DDR_DPLL3_REG);
120 writel(0x03000000, srif_regs + 0x188); /* Undocumented reg :-) */
121
122 /* Test for 40MHz XTAL */
Wills Wangddc05522016-05-30 22:54:50 +0800123 reg = ath79_get_bootstrap();
Marek Vasut6a713ea2016-05-06 20:10:40 +0200124 if (reg & AR934X_BOOTSTRAP_REF_CLK_40) {
125 xtal_40 = 1;
126 cpu_srif = 0x41c00000;
127 ddr_srif = 0x41680000;
128 } else {
129 xtal_40 = 0;
130 cpu_srif = 0x29c00000;
131 ddr_srif = 0x29680000;
132 }
133
134 /* Locate CPU/DDR PLL configuration */
135 for (i = 0; i < ARRAY_SIZE(ar934x_clock_config); i++) {
136 if (cpu_mhz != ar934x_clock_config[i].cpu_freq)
137 continue;
138 if (ddr_mhz != ar934x_clock_config[i].ddr_freq)
139 continue;
140 if (ahb_mhz != ar934x_clock_config[i].ahb_freq)
141 continue;
142
143 /* Entry found */
144 pll_cfg = &ar934x_clock_config[i].cpu_pll;
145 pll_nint = pll_cfg->nint[xtal_40];
146 pll_refdiv = pll_cfg->refdiv;
147 cpu_pll =
148 (pll_nint << AR934X_PLL_CPU_CONFIG_NINT_SHIFT) |
149 (pll_refdiv << AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT) |
150 (pll_cfg->range << AR934X_PLL_CPU_CONFIG_RANGE_SHIFT) |
151 (pll_cfg->outdiv << AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT);
152
153 pll_cfg = &ar934x_clock_config[i].ddr_pll;
154 pll_nint = pll_cfg->nint[xtal_40];
155 pll_refdiv = pll_cfg->refdiv;
156 ddr_pll =
157 (pll_nint << AR934X_PLL_DDR_CONFIG_NINT_SHIFT) |
158 (pll_refdiv << AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT) |
159 (pll_cfg->range << AR934X_PLL_DDR_CONFIG_RANGE_SHIFT) |
160 (pll_cfg->outdiv << AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT);
161 break;
162 }
163
164 /* PLL configuration not found, hang. */
165 if (i == ARRAY_SIZE(ar934x_clock_config))
166 hang();
167
168 /* Set PLL Bypass */
169 setbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
170 AR934X_PLL_CLK_CTRL_CPU_PLL_BYPASS);
171 setbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
172 AR934X_PLL_CLK_CTRL_DDR_PLL_BYPASS);
173 setbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
174 AR934X_PLL_CLK_CTRL_AHB_PLL_BYPASS);
175
176 /* Configure CPU PLL */
177 writel(cpu_pll | AR934X_PLL_CPU_CONFIG_PLLPWD,
178 pll_regs + AR934X_PLL_CPU_CONFIG_REG);
179 /* Configure DDR PLL */
180 writel(ddr_pll | AR934X_PLL_DDR_CONFIG_PLLPWD,
181 pll_regs + AR934X_PLL_DDR_CONFIG_REG);
182 /* Configure PLL routing */
183 writel(AR934X_PLL_CLK_CTRL_CPU_PLL_BYPASS |
184 AR934X_PLL_CLK_CTRL_DDR_PLL_BYPASS |
185 AR934X_PLL_CLK_CTRL_AHB_PLL_BYPASS |
186 (0 << AR934X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) |
187 (0 << AR934X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) |
188 (1 << AR934X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) |
189 AR934X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL |
190 AR934X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL |
191 AR934X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL,
192 pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG);
193
194 /* Configure SRIF PLLs, which is completely undocumented :-) */
195 ar934x_srif_pll_cfg(srif_regs + AR934X_SRIF_CPU_DPLL1_REG, cpu_srif);
196 ar934x_srif_pll_cfg(srif_regs + AR934X_SRIF_DDR_DPLL1_REG, ddr_srif);
197
198 /* Unset PLL Bypass */
199 clrbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
200 AR934X_PLL_CLK_CTRL_CPU_PLL_BYPASS);
201 clrbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
202 AR934X_PLL_CLK_CTRL_DDR_PLL_BYPASS);
203 clrbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
204 AR934X_PLL_CLK_CTRL_AHB_PLL_BYPASS);
205
206 /* Enable PLL dithering */
207 writel((1 << AR934X_PLL_DDR_DIT_FRAC_STEP_SHIFT) |
208 (0xf << AR934X_PLL_DDR_DIT_UPD_CNT_SHIFT),
209 pll_regs + AR934X_PLL_DDR_DIT_FRAC_REG);
210 writel(48 << AR934X_PLL_CPU_DIT_UPD_CNT_SHIFT,
211 pll_regs + AR934X_PLL_CPU_DIT_FRAC_REG);
212}
213
214static u32 ar934x_get_xtal(void)
215{
216 u32 val;
217
Wills Wangddc05522016-05-30 22:54:50 +0800218 val = ath79_get_bootstrap();
Marek Vasut6a713ea2016-05-06 20:10:40 +0200219 if (val & AR934X_BOOTSTRAP_REF_CLK_40)
220 return 40000000;
221 else
222 return 25000000;
223}
224
225int get_serial_clock(void)
226{
227 return ar934x_get_xtal();
228}
229
230static u32 ar934x_cpupll_to_hz(const u32 regval)
231{
232 const u32 outdiv = (regval >> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
233 AR934X_PLL_CPU_CONFIG_OUTDIV_MASK;
234 const u32 refdiv = (regval >> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
235 AR934X_PLL_CPU_CONFIG_REFDIV_MASK;
236 const u32 nint = (regval >> AR934X_PLL_CPU_CONFIG_NINT_SHIFT) &
237 AR934X_PLL_CPU_CONFIG_NINT_MASK;
238 const u32 nfrac = (regval >> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT) &
239 AR934X_PLL_CPU_CONFIG_NFRAC_MASK;
240 const u32 xtal = ar934x_get_xtal();
241
242 return (xtal * (nint + (nfrac >> 9))) / (refdiv * (1 << outdiv));
243}
244
245static u32 ar934x_ddrpll_to_hz(const u32 regval)
246{
247 const u32 outdiv = (regval >> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT) &
248 AR934X_PLL_DDR_CONFIG_OUTDIV_MASK;
249 const u32 refdiv = (regval >> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT) &
250 AR934X_PLL_DDR_CONFIG_REFDIV_MASK;
251 const u32 nint = (regval >> AR934X_PLL_DDR_CONFIG_NINT_SHIFT) &
252 AR934X_PLL_DDR_CONFIG_NINT_MASK;
253 const u32 nfrac = (regval >> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT) &
254 AR934X_PLL_DDR_CONFIG_NFRAC_MASK;
255 const u32 xtal = ar934x_get_xtal();
256
257 return (xtal * (nint + (nfrac >> 9))) / (refdiv * (1 << outdiv));
258}
259
260static void ar934x_update_clock(void)
261{
262 void __iomem *regs;
263 u32 ctrl, cpu, cpupll, ddr, ddrpll;
264 u32 cpudiv, ddrdiv, busdiv;
265 u32 cpuclk, ddrclk, busclk;
266
267 regs = map_physmem(AR71XX_PLL_BASE, AR71XX_PLL_SIZE,
268 MAP_NOCACHE);
269
270 cpu = readl(regs + AR934X_PLL_CPU_CONFIG_REG);
271 ddr = readl(regs + AR934X_PLL_DDR_CONFIG_REG);
272 ctrl = readl(regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG);
273
274 cpupll = ar934x_cpupll_to_hz(cpu);
275 ddrpll = ar934x_ddrpll_to_hz(ddr);
276
277 if (ctrl & AR934X_PLL_CLK_CTRL_CPU_PLL_BYPASS)
278 cpuclk = ar934x_get_xtal();
279 else if (ctrl & AR934X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL)
280 cpuclk = cpupll;
281 else
282 cpuclk = ddrpll;
283
284 if (ctrl & AR934X_PLL_CLK_CTRL_DDR_PLL_BYPASS)
285 ddrclk = ar934x_get_xtal();
286 else if (ctrl & AR934X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL)
287 ddrclk = ddrpll;
288 else
289 ddrclk = cpupll;
290
291 if (ctrl & AR934X_PLL_CLK_CTRL_AHB_PLL_BYPASS)
292 busclk = ar934x_get_xtal();
293 else if (ctrl & AR934X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL)
294 busclk = ddrpll;
295 else
296 busclk = cpupll;
297
298 cpudiv = (ctrl >> AR934X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) &
299 AR934X_PLL_CLK_CTRL_CPU_POST_DIV_MASK;
300 ddrdiv = (ctrl >> AR934X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) &
301 AR934X_PLL_CLK_CTRL_DDR_POST_DIV_MASK;
302 busdiv = (ctrl >> AR934X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) &
303 AR934X_PLL_CLK_CTRL_AHB_POST_DIV_MASK;
304
305 gd->cpu_clk = cpuclk / (cpudiv + 1);
306 gd->mem_clk = ddrclk / (ddrdiv + 1);
307 gd->bus_clk = busclk / (busdiv + 1);
308}
309
310ulong get_bus_freq(ulong dummy)
311{
312 ar934x_update_clock();
313 return gd->bus_clk;
314}
315
316ulong get_ddr_freq(ulong dummy)
317{
318 ar934x_update_clock();
319 return gd->mem_clk;
320}
321
322int do_ar934x_showclk(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
323{
324 ar934x_update_clock();
325 printf("CPU: %8ld MHz\n", gd->cpu_clk / 1000000);
326 printf("Memory: %8ld MHz\n", gd->mem_clk / 1000000);
327 printf("AHB: %8ld MHz\n", gd->bus_clk / 1000000);
328 return 0;
329}
330
331U_BOOT_CMD(
332 clocks, CONFIG_SYS_MAXARGS, 1, do_ar934x_showclk,
333 "display clocks",
334 ""
335);