1 // SPDX-License-Identifier: GPL-2.0+
7 #include <clock_legacy.h>
11 #include <asm/addrspace.h>
12 #include <asm/types.h>
13 #include <linux/delay.h>
14 #include <mach/ar71xx_regs.h>
15 #include <mach/ath79.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 * The math for calculating PLL:
23 * NINT + -------------
24 * XTAL [MHz] 2^(18 - 1)
25 * PLL [MHz] = ------------ * ----------------------
28 * Unfortunatelly, there is no way to reliably compute the variables.
29 * The vendor U-Boot port contains macros for various combinations of
30 * CPU PLL / DDR PLL / AHB bus speed and there is no obvious pattern
33 struct ar934x_pll_config {
37 /* Index 0 is for XTAL=25MHz , Index 1 is for XTAL=40MHz */
41 struct ar934x_clock_config {
46 struct ar934x_pll_config cpu_pll;
47 struct ar934x_pll_config ddr_pll;
50 static const struct ar934x_clock_config ar934x_clock_config[] = {
51 { 300, 300, 150, { 1, 1, 1, { 24, 15 } }, { 1, 1, 1, { 24, 15 } } },
52 { 400, 200, 200, { 1, 1, 1, { 32, 20 } }, { 1, 1, 2, { 32, 20 } } },
53 { 400, 400, 200, { 0, 1, 1, { 32, 20 } }, { 0, 1, 1, { 32, 20 } } },
54 { 500, 400, 200, { 1, 1, 0, { 20, 12 } }, { 0, 1, 1, { 32, 20 } } },
55 { 533, 400, 200, { 1, 1, 0, { 21, 13 } }, { 0, 1, 1, { 32, 20 } } },
56 { 533, 500, 250, { 1, 1, 0, { 21, 13 } }, { 0, 1, 0, { 20, 12 } } },
57 { 560, 480, 240, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 19, 12 } } },
58 { 566, 400, 200, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 16, 10 } } },
59 { 566, 450, 225, { 1, 1, 0, { 22, 14 } }, { 0, 1, 1, { 36, 22 } } },
60 { 566, 475, 237, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 19, 11 } } },
61 { 566, 500, 250, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 20, 12 } } },
62 { 566, 525, 262, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 21, 13 } } },
63 { 566, 550, 275, { 1, 1, 0, { 22, 14 } }, { 1, 1, 0, { 22, 13 } } },
64 { 600, 266, 133, { 0, 1, 0, { 24, 15 } }, { 1, 1, 1, { 21, 16 } } },
65 { 600, 266, 200, { 0, 1, 0, { 24, 15 } }, { 1, 1, 1, { 21, 16 } } },
66 { 600, 300, 150, { 0, 1, 0, { 24, 15 } }, { 0, 1, 1, { 24, 15 } } },
67 { 600, 332, 166, { 0, 1, 0, { 24, 15 } }, { 1, 1, 1, { 26, 16 } } },
68 { 600, 332, 200, { 0, 1, 0, { 24, 15 } }, { 1, 1, 1, { 26, 16 } } },
69 { 600, 400, 200, { 0, 1, 0, { 24, 15 } }, { 0, 1, 1, { 32, 20 } } },
70 { 600, 450, 200, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 18, 20 } } },
71 { 600, 500, 250, { 0, 1, 0, { 24, 15 } }, { 1, 1, 0, { 20, 12 } } },
72 { 600, 525, 262, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 21, 20 } } },
73 { 600, 550, 275, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 22, 20 } } },
74 { 600, 575, 287, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 23, 14 } } },
75 { 600, 600, 300, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 24, 20 } } },
76 { 600, 650, 325, { 0, 1, 0, { 24, 15 } }, { 0, 1, 0, { 26, 20 } } },
77 { 650, 600, 300, { 0, 1, 0, { 26, 15 } }, { 0, 1, 0, { 24, 20 } } },
78 { 700, 400, 200, { 3, 1, 0, { 28, 17 } }, { 0, 1, 1, { 32, 20 } } },
81 static void ar934x_srif_pll_cfg(void __iomem *pll_reg_base, const u32 srif_val)
85 writel(0x10810f00, pll_reg_base + 0x4);
86 writel(srif_val, pll_reg_base + 0x0);
87 writel(0xd0810f00, pll_reg_base + 0x4);
88 writel(0x03000000, pll_reg_base + 0x8);
89 writel(0xd0800f00, pll_reg_base + 0x4);
91 clrbits_be32(pll_reg_base + 0x8, BIT(30));
93 setbits_be32(pll_reg_base + 0x8, BIT(30));
96 wait_for_bit_le32(pll_reg_base + 0xc, BIT(3), 1, 10, 0);
98 clrbits_be32(pll_reg_base + 0x8, BIT(30));
101 /* Check if CPU SRIF PLL locked. */
102 reg = readl(pll_reg_base + 0x8);
103 reg = (reg & 0x7ffff8) >> 3;
104 } while (reg >= 0x40000);
107 void ar934x_pll_init(const u16 cpu_mhz, const u16 ddr_mhz, const u16 ahb_mhz)
109 void __iomem *srif_regs = map_physmem(AR934X_SRIF_BASE,
110 AR934X_SRIF_SIZE, MAP_NOCACHE);
111 void __iomem *pll_regs = map_physmem(AR71XX_PLL_BASE,
112 AR71XX_PLL_SIZE, MAP_NOCACHE);
113 const struct ar934x_pll_config *pll_cfg;
114 int i, pll_nint, pll_refdiv, xtal_40 = 0;
115 u32 reg, cpu_pll, cpu_srif, ddr_pll, ddr_srif;
117 /* Configure SRIF PLL with initial values. */
118 writel(0x13210f00, srif_regs + AR934X_SRIF_CPU_DPLL2_REG);
119 writel(0x03000000, srif_regs + AR934X_SRIF_CPU_DPLL3_REG);
120 writel(0x13210f00, srif_regs + AR934X_SRIF_DDR_DPLL2_REG);
121 writel(0x03000000, srif_regs + AR934X_SRIF_DDR_DPLL3_REG);
122 writel(0x03000000, srif_regs + 0x188); /* Undocumented reg :-) */
124 /* Test for 40MHz XTAL */
125 reg = ath79_get_bootstrap();
126 if (reg & AR934X_BOOTSTRAP_REF_CLK_40) {
128 cpu_srif = 0x41c00000;
129 ddr_srif = 0x41680000;
132 cpu_srif = 0x29c00000;
133 ddr_srif = 0x29680000;
136 /* Locate CPU/DDR PLL configuration */
137 for (i = 0; i < ARRAY_SIZE(ar934x_clock_config); i++) {
138 if (cpu_mhz != ar934x_clock_config[i].cpu_freq)
140 if (ddr_mhz != ar934x_clock_config[i].ddr_freq)
142 if (ahb_mhz != ar934x_clock_config[i].ahb_freq)
146 pll_cfg = &ar934x_clock_config[i].cpu_pll;
147 pll_nint = pll_cfg->nint[xtal_40];
148 pll_refdiv = pll_cfg->refdiv;
150 (pll_nint << AR934X_PLL_CPU_CONFIG_NINT_SHIFT) |
151 (pll_refdiv << AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT) |
152 (pll_cfg->range << AR934X_PLL_CPU_CONFIG_RANGE_SHIFT) |
153 (pll_cfg->outdiv << AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT);
155 pll_cfg = &ar934x_clock_config[i].ddr_pll;
156 pll_nint = pll_cfg->nint[xtal_40];
157 pll_refdiv = pll_cfg->refdiv;
159 (pll_nint << AR934X_PLL_DDR_CONFIG_NINT_SHIFT) |
160 (pll_refdiv << AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT) |
161 (pll_cfg->range << AR934X_PLL_DDR_CONFIG_RANGE_SHIFT) |
162 (pll_cfg->outdiv << AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT);
166 /* PLL configuration not found, hang. */
167 if (i == ARRAY_SIZE(ar934x_clock_config))
171 setbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
172 AR934X_PLL_CLK_CTRL_CPU_PLL_BYPASS);
173 setbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
174 AR934X_PLL_CLK_CTRL_DDR_PLL_BYPASS);
175 setbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
176 AR934X_PLL_CLK_CTRL_AHB_PLL_BYPASS);
178 /* Configure CPU PLL */
179 writel(cpu_pll | AR934X_PLL_CPU_CONFIG_PLLPWD,
180 pll_regs + AR934X_PLL_CPU_CONFIG_REG);
181 /* Configure DDR PLL */
182 writel(ddr_pll | AR934X_PLL_DDR_CONFIG_PLLPWD,
183 pll_regs + AR934X_PLL_DDR_CONFIG_REG);
184 /* Configure PLL routing */
185 writel(AR934X_PLL_CLK_CTRL_CPU_PLL_BYPASS |
186 AR934X_PLL_CLK_CTRL_DDR_PLL_BYPASS |
187 AR934X_PLL_CLK_CTRL_AHB_PLL_BYPASS |
188 (0 << AR934X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) |
189 (0 << AR934X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) |
190 (1 << AR934X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) |
191 AR934X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL |
192 AR934X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL |
193 AR934X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL,
194 pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG);
196 /* Configure SRIF PLLs, which is completely undocumented :-) */
197 ar934x_srif_pll_cfg(srif_regs + AR934X_SRIF_CPU_DPLL1_REG, cpu_srif);
198 ar934x_srif_pll_cfg(srif_regs + AR934X_SRIF_DDR_DPLL1_REG, ddr_srif);
200 /* Unset PLL Bypass */
201 clrbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
202 AR934X_PLL_CLK_CTRL_CPU_PLL_BYPASS);
203 clrbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
204 AR934X_PLL_CLK_CTRL_DDR_PLL_BYPASS);
205 clrbits_be32(pll_regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG,
206 AR934X_PLL_CLK_CTRL_AHB_PLL_BYPASS);
208 /* Enable PLL dithering */
209 writel((1 << AR934X_PLL_DDR_DIT_FRAC_STEP_SHIFT) |
210 (0xf << AR934X_PLL_DDR_DIT_UPD_CNT_SHIFT),
211 pll_regs + AR934X_PLL_DDR_DIT_FRAC_REG);
212 writel(48 << AR934X_PLL_CPU_DIT_UPD_CNT_SHIFT,
213 pll_regs + AR934X_PLL_CPU_DIT_FRAC_REG);
216 static u32 ar934x_get_xtal(void)
220 val = ath79_get_bootstrap();
221 if (val & AR934X_BOOTSTRAP_REF_CLK_40)
227 int get_serial_clock(void)
229 return ar934x_get_xtal();
232 static u32 ar934x_cpupll_to_hz(const u32 regval)
234 const u32 outdiv = (regval >> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
235 AR934X_PLL_CPU_CONFIG_OUTDIV_MASK;
236 const u32 refdiv = (regval >> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
237 AR934X_PLL_CPU_CONFIG_REFDIV_MASK;
238 const u32 nint = (regval >> AR934X_PLL_CPU_CONFIG_NINT_SHIFT) &
239 AR934X_PLL_CPU_CONFIG_NINT_MASK;
240 const u32 nfrac = (regval >> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT) &
241 AR934X_PLL_CPU_CONFIG_NFRAC_MASK;
242 const u32 xtal = ar934x_get_xtal();
244 return (xtal * (nint + (nfrac >> 9))) / (refdiv * (1 << outdiv));
247 static u32 ar934x_ddrpll_to_hz(const u32 regval)
249 const u32 outdiv = (regval >> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT) &
250 AR934X_PLL_DDR_CONFIG_OUTDIV_MASK;
251 const u32 refdiv = (regval >> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT) &
252 AR934X_PLL_DDR_CONFIG_REFDIV_MASK;
253 const u32 nint = (regval >> AR934X_PLL_DDR_CONFIG_NINT_SHIFT) &
254 AR934X_PLL_DDR_CONFIG_NINT_MASK;
255 const u32 nfrac = (regval >> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT) &
256 AR934X_PLL_DDR_CONFIG_NFRAC_MASK;
257 const u32 xtal = ar934x_get_xtal();
259 return (xtal * (nint + (nfrac >> 9))) / (refdiv * (1 << outdiv));
262 static void ar934x_update_clock(void)
265 u32 ctrl, cpu, cpupll, ddr, ddrpll;
266 u32 cpudiv, ddrdiv, busdiv;
267 u32 cpuclk, ddrclk, busclk;
269 regs = map_physmem(AR71XX_PLL_BASE, AR71XX_PLL_SIZE,
272 cpu = readl(regs + AR934X_PLL_CPU_CONFIG_REG);
273 ddr = readl(regs + AR934X_PLL_DDR_CONFIG_REG);
274 ctrl = readl(regs + AR934X_PLL_CPU_DDR_CLK_CTRL_REG);
276 cpupll = ar934x_cpupll_to_hz(cpu);
277 ddrpll = ar934x_ddrpll_to_hz(ddr);
279 if (ctrl & AR934X_PLL_CLK_CTRL_CPU_PLL_BYPASS)
280 cpuclk = ar934x_get_xtal();
281 else if (ctrl & AR934X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL)
286 if (ctrl & AR934X_PLL_CLK_CTRL_DDR_PLL_BYPASS)
287 ddrclk = ar934x_get_xtal();
288 else if (ctrl & AR934X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL)
293 if (ctrl & AR934X_PLL_CLK_CTRL_AHB_PLL_BYPASS)
294 busclk = ar934x_get_xtal();
295 else if (ctrl & AR934X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL)
300 cpudiv = (ctrl >> AR934X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) &
301 AR934X_PLL_CLK_CTRL_CPU_POST_DIV_MASK;
302 ddrdiv = (ctrl >> AR934X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) &
303 AR934X_PLL_CLK_CTRL_DDR_POST_DIV_MASK;
304 busdiv = (ctrl >> AR934X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) &
305 AR934X_PLL_CLK_CTRL_AHB_POST_DIV_MASK;
307 gd->cpu_clk = cpuclk / (cpudiv + 1);
308 gd->mem_clk = ddrclk / (ddrdiv + 1);
309 gd->bus_clk = busclk / (busdiv + 1);
312 ulong get_bus_freq(ulong dummy)
314 ar934x_update_clock();
318 ulong get_ddr_freq(ulong dummy)
320 ar934x_update_clock();
324 int do_ar934x_showclk(struct cmd_tbl *cmdtp, int flag, int argc,
327 ar934x_update_clock();
328 printf("CPU: %8ld MHz\n", gd->cpu_clk / 1000000);
329 printf("Memory: %8ld MHz\n", gd->mem_clk / 1000000);
330 printf("AHB: %8ld MHz\n", gd->bus_clk / 1000000);
335 clocks, CONFIG_SYS_MAXARGS, 1, do_ar934x_showclk,