2 * Atheros AR71XX/AR724X/AR913X common routines
4 * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
5 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
7 * Parts of this file are based on Atheros' 2.6.15/2.6.31 BSP
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published
11 * by the Free Software Foundation.
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/clk.h>
18 #include <linux/clkdev.h>
19 #include <linux/clk-provider.h>
21 #include <linux/of_address.h>
22 #include <dt-bindings/clock/ath79-clk.h>
24 #include <asm/div64.h>
26 #include <asm/mach-ath79/ath79.h>
27 #include <asm/mach-ath79/ar71xx_regs.h>
29 #include "machtypes.h"
31 #define AR71XX_BASE_FREQ 40000000
32 #define AR724X_BASE_FREQ 40000000
34 static struct clk
*clks
[ATH79_CLK_END
];
35 static struct clk_onecell_data clk_data
= {
37 .clk_num
= ARRAY_SIZE(clks
),
40 static struct clk
*__init
ath79_add_sys_clkdev(
41 const char *id
, unsigned long rate
)
46 clk
= clk_register_fixed_rate(NULL
, id
, NULL
, 0, rate
);
48 panic("failed to allocate %s clock structure", id
);
50 err
= clk_register_clkdev(clk
, id
, NULL
);
52 panic("unable to register %s clock device", id
);
57 static void __init
ar71xx_clocks_init(void)
59 unsigned long ref_rate
;
60 unsigned long cpu_rate
;
61 unsigned long ddr_rate
;
62 unsigned long ahb_rate
;
67 ref_rate
= AR71XX_BASE_FREQ
;
69 pll
= ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG
);
71 div
= ((pll
>> AR71XX_PLL_FB_SHIFT
) & AR71XX_PLL_FB_MASK
) + 1;
72 freq
= div
* ref_rate
;
74 div
= ((pll
>> AR71XX_CPU_DIV_SHIFT
) & AR71XX_CPU_DIV_MASK
) + 1;
75 cpu_rate
= freq
/ div
;
77 div
= ((pll
>> AR71XX_DDR_DIV_SHIFT
) & AR71XX_DDR_DIV_MASK
) + 1;
78 ddr_rate
= freq
/ div
;
80 div
= (((pll
>> AR71XX_AHB_DIV_SHIFT
) & AR71XX_AHB_DIV_MASK
) + 1) * 2;
81 ahb_rate
= cpu_rate
/ div
;
83 ath79_add_sys_clkdev("ref", ref_rate
);
84 clks
[ATH79_CLK_CPU
] = ath79_add_sys_clkdev("cpu", cpu_rate
);
85 clks
[ATH79_CLK_DDR
] = ath79_add_sys_clkdev("ddr", ddr_rate
);
86 clks
[ATH79_CLK_AHB
] = ath79_add_sys_clkdev("ahb", ahb_rate
);
88 clk_add_alias("wdt", NULL
, "ahb", NULL
);
89 clk_add_alias("uart", NULL
, "ahb", NULL
);
92 static struct clk
* __init
ath79_reg_ffclk(const char *name
,
93 const char *parent_name
, unsigned int mult
, unsigned int div
)
97 clk
= clk_register_fixed_factor(NULL
, name
, parent_name
, 0, mult
, div
);
99 panic("failed to allocate %s clock structure", name
);
104 static void __init
ar724x_clk_init(struct clk
*ref_clk
, void __iomem
*pll_base
)
107 u32 mult
, div
, ddr_div
, ahb_div
;
109 pll
= __raw_readl(pll_base
+ AR724X_PLL_REG_CPU_CONFIG
);
111 mult
= ((pll
>> AR724X_PLL_FB_SHIFT
) & AR724X_PLL_FB_MASK
);
112 div
= ((pll
>> AR724X_PLL_REF_DIV_SHIFT
) & AR724X_PLL_REF_DIV_MASK
) * 2;
114 ddr_div
= ((pll
>> AR724X_DDR_DIV_SHIFT
) & AR724X_DDR_DIV_MASK
) + 1;
115 ahb_div
= (((pll
>> AR724X_AHB_DIV_SHIFT
) & AR724X_AHB_DIV_MASK
) + 1) * 2;
117 clks
[ATH79_CLK_CPU
] = ath79_reg_ffclk("cpu", "ref", mult
, div
);
118 clks
[ATH79_CLK_DDR
] = ath79_reg_ffclk("ddr", "ref", mult
, div
* ddr_div
);
119 clks
[ATH79_CLK_AHB
] = ath79_reg_ffclk("ahb", "ref", mult
, div
* ahb_div
);
122 static void __init
ar724x_clocks_init(void)
126 ref_clk
= ath79_add_sys_clkdev("ref", AR724X_BASE_FREQ
);
128 ar724x_clk_init(ref_clk
, ath79_pll_base
);
130 /* just make happy plat_time_init() from arch/mips/ath79/setup.c */
131 clk_register_clkdev(clks
[ATH79_CLK_CPU
], "cpu", NULL
);
132 clk_register_clkdev(clks
[ATH79_CLK_DDR
], "ddr", NULL
);
133 clk_register_clkdev(clks
[ATH79_CLK_AHB
], "ahb", NULL
);
135 clk_add_alias("wdt", NULL
, "ahb", NULL
);
136 clk_add_alias("uart", NULL
, "ahb", NULL
);
139 static void __init
ar9330_clk_init(struct clk
*ref_clk
, void __iomem
*pll_base
)
150 clock_ctrl
= __raw_readl(pll_base
+ AR933X_PLL_CLOCK_CTRL_REG
);
151 if (clock_ctrl
& AR933X_PLL_CLOCK_CTRL_BYPASS
) {
163 cpu_config
= __raw_readl(pll_base
+ AR933X_PLL_CPU_CONFIG_REG
);
165 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
166 AR933X_PLL_CPU_CONFIG_REFDIV_MASK
;
169 ninit_mul
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_NINT_SHIFT
) &
170 AR933X_PLL_CPU_CONFIG_NINT_MASK
;
172 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
173 AR933X_PLL_CPU_CONFIG_OUTDIV_MASK
;
179 cpu_div
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT
) &
180 AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK
) + 1;
182 ddr_div
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT
) &
183 AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK
) + 1;
185 ahb_div
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT
) &
186 AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK
) + 1;
189 clks
[ATH79_CLK_CPU
] = ath79_reg_ffclk("cpu", "ref",
190 ninit_mul
, ref_div
* out_div
* cpu_div
);
191 clks
[ATH79_CLK_DDR
] = ath79_reg_ffclk("ddr", "ref",
192 ninit_mul
, ref_div
* out_div
* ddr_div
);
193 clks
[ATH79_CLK_AHB
] = ath79_reg_ffclk("ahb", "ref",
194 ninit_mul
, ref_div
* out_div
* ahb_div
);
197 static void __init
ar933x_clocks_init(void)
200 unsigned long ref_rate
;
203 t
= ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP
);
204 if (t
& AR933X_BOOTSTRAP_REF_CLK_40
)
205 ref_rate
= (40 * 1000 * 1000);
207 ref_rate
= (25 * 1000 * 1000);
209 ref_clk
= ath79_add_sys_clkdev("ref", ref_rate
);
211 ar9330_clk_init(ref_clk
, ath79_pll_base
);
213 /* just make happy plat_time_init() from arch/mips/ath79/setup.c */
214 clk_register_clkdev(clks
[ATH79_CLK_CPU
], "cpu", NULL
);
215 clk_register_clkdev(clks
[ATH79_CLK_DDR
], "ddr", NULL
);
216 clk_register_clkdev(clks
[ATH79_CLK_AHB
], "ahb", NULL
);
218 clk_add_alias("wdt", NULL
, "ahb", NULL
);
219 clk_add_alias("uart", NULL
, "ref", NULL
);
222 static u32 __init
ar934x_get_pll_freq(u32 ref
, u32 ref_div
, u32 nint
, u32 nfrac
,
223 u32 frac
, u32 out_div
)
235 do_div(t
, ref_div
* frac
);
238 ret
/= (1 << out_div
);
242 static void __init
ar934x_clocks_init(void)
244 unsigned long ref_rate
;
245 unsigned long cpu_rate
;
246 unsigned long ddr_rate
;
247 unsigned long ahb_rate
;
248 u32 pll
, out_div
, ref_div
, nint
, nfrac
, frac
, clk_ctrl
, postdiv
;
249 u32 cpu_pll
, ddr_pll
;
251 void __iomem
*dpll_base
;
253 dpll_base
= ioremap(AR934X_SRIF_BASE
, AR934X_SRIF_SIZE
);
255 bootstrap
= ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP
);
256 if (bootstrap
& AR934X_BOOTSTRAP_REF_CLK_40
)
257 ref_rate
= 40 * 1000 * 1000;
259 ref_rate
= 25 * 1000 * 1000;
261 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_CPU_DPLL2_REG
);
262 if (pll
& AR934X_SRIF_DPLL2_LOCAL_PLL
) {
263 out_div
= (pll
>> AR934X_SRIF_DPLL2_OUTDIV_SHIFT
) &
264 AR934X_SRIF_DPLL2_OUTDIV_MASK
;
265 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_CPU_DPLL1_REG
);
266 nint
= (pll
>> AR934X_SRIF_DPLL1_NINT_SHIFT
) &
267 AR934X_SRIF_DPLL1_NINT_MASK
;
268 nfrac
= pll
& AR934X_SRIF_DPLL1_NFRAC_MASK
;
269 ref_div
= (pll
>> AR934X_SRIF_DPLL1_REFDIV_SHIFT
) &
270 AR934X_SRIF_DPLL1_REFDIV_MASK
;
273 pll
= ath79_pll_rr(AR934X_PLL_CPU_CONFIG_REG
);
274 out_div
= (pll
>> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
275 AR934X_PLL_CPU_CONFIG_OUTDIV_MASK
;
276 ref_div
= (pll
>> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
277 AR934X_PLL_CPU_CONFIG_REFDIV_MASK
;
278 nint
= (pll
>> AR934X_PLL_CPU_CONFIG_NINT_SHIFT
) &
279 AR934X_PLL_CPU_CONFIG_NINT_MASK
;
280 nfrac
= (pll
>> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT
) &
281 AR934X_PLL_CPU_CONFIG_NFRAC_MASK
;
285 cpu_pll
= ar934x_get_pll_freq(ref_rate
, ref_div
, nint
,
286 nfrac
, frac
, out_div
);
288 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_DDR_DPLL2_REG
);
289 if (pll
& AR934X_SRIF_DPLL2_LOCAL_PLL
) {
290 out_div
= (pll
>> AR934X_SRIF_DPLL2_OUTDIV_SHIFT
) &
291 AR934X_SRIF_DPLL2_OUTDIV_MASK
;
292 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_DDR_DPLL1_REG
);
293 nint
= (pll
>> AR934X_SRIF_DPLL1_NINT_SHIFT
) &
294 AR934X_SRIF_DPLL1_NINT_MASK
;
295 nfrac
= pll
& AR934X_SRIF_DPLL1_NFRAC_MASK
;
296 ref_div
= (pll
>> AR934X_SRIF_DPLL1_REFDIV_SHIFT
) &
297 AR934X_SRIF_DPLL1_REFDIV_MASK
;
300 pll
= ath79_pll_rr(AR934X_PLL_DDR_CONFIG_REG
);
301 out_div
= (pll
>> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT
) &
302 AR934X_PLL_DDR_CONFIG_OUTDIV_MASK
;
303 ref_div
= (pll
>> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT
) &
304 AR934X_PLL_DDR_CONFIG_REFDIV_MASK
;
305 nint
= (pll
>> AR934X_PLL_DDR_CONFIG_NINT_SHIFT
) &
306 AR934X_PLL_DDR_CONFIG_NINT_MASK
;
307 nfrac
= (pll
>> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT
) &
308 AR934X_PLL_DDR_CONFIG_NFRAC_MASK
;
312 ddr_pll
= ar934x_get_pll_freq(ref_rate
, ref_div
, nint
,
313 nfrac
, frac
, out_div
);
315 clk_ctrl
= ath79_pll_rr(AR934X_PLL_CPU_DDR_CLK_CTRL_REG
);
317 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_SHIFT
) &
318 AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_MASK
;
320 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS
)
322 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_CPUCLK_FROM_CPUPLL
)
323 cpu_rate
= cpu_pll
/ (postdiv
+ 1);
325 cpu_rate
= ddr_pll
/ (postdiv
+ 1);
327 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_SHIFT
) &
328 AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_MASK
;
330 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS
)
332 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_DDRCLK_FROM_DDRPLL
)
333 ddr_rate
= ddr_pll
/ (postdiv
+ 1);
335 ddr_rate
= cpu_pll
/ (postdiv
+ 1);
337 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_SHIFT
) &
338 AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_MASK
;
340 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS
)
342 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_AHBCLK_FROM_DDRPLL
)
343 ahb_rate
= ddr_pll
/ (postdiv
+ 1);
345 ahb_rate
= cpu_pll
/ (postdiv
+ 1);
347 ath79_add_sys_clkdev("ref", ref_rate
);
348 clks
[ATH79_CLK_CPU
] = ath79_add_sys_clkdev("cpu", cpu_rate
);
349 clks
[ATH79_CLK_DDR
] = ath79_add_sys_clkdev("ddr", ddr_rate
);
350 clks
[ATH79_CLK_AHB
] = ath79_add_sys_clkdev("ahb", ahb_rate
);
352 clk_add_alias("wdt", NULL
, "ref", NULL
);
353 clk_add_alias("uart", NULL
, "ref", NULL
);
358 static void __init
qca955x_clocks_init(void)
360 unsigned long ref_rate
;
361 unsigned long cpu_rate
;
362 unsigned long ddr_rate
;
363 unsigned long ahb_rate
;
364 u32 pll
, out_div
, ref_div
, nint
, frac
, clk_ctrl
, postdiv
;
365 u32 cpu_pll
, ddr_pll
;
368 bootstrap
= ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP
);
369 if (bootstrap
& QCA955X_BOOTSTRAP_REF_CLK_40
)
370 ref_rate
= 40 * 1000 * 1000;
372 ref_rate
= 25 * 1000 * 1000;
374 pll
= ath79_pll_rr(QCA955X_PLL_CPU_CONFIG_REG
);
375 out_div
= (pll
>> QCA955X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
376 QCA955X_PLL_CPU_CONFIG_OUTDIV_MASK
;
377 ref_div
= (pll
>> QCA955X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
378 QCA955X_PLL_CPU_CONFIG_REFDIV_MASK
;
379 nint
= (pll
>> QCA955X_PLL_CPU_CONFIG_NINT_SHIFT
) &
380 QCA955X_PLL_CPU_CONFIG_NINT_MASK
;
381 frac
= (pll
>> QCA955X_PLL_CPU_CONFIG_NFRAC_SHIFT
) &
382 QCA955X_PLL_CPU_CONFIG_NFRAC_MASK
;
384 cpu_pll
= nint
* ref_rate
/ ref_div
;
385 cpu_pll
+= frac
* ref_rate
/ (ref_div
* (1 << 6));
386 cpu_pll
/= (1 << out_div
);
388 pll
= ath79_pll_rr(QCA955X_PLL_DDR_CONFIG_REG
);
389 out_div
= (pll
>> QCA955X_PLL_DDR_CONFIG_OUTDIV_SHIFT
) &
390 QCA955X_PLL_DDR_CONFIG_OUTDIV_MASK
;
391 ref_div
= (pll
>> QCA955X_PLL_DDR_CONFIG_REFDIV_SHIFT
) &
392 QCA955X_PLL_DDR_CONFIG_REFDIV_MASK
;
393 nint
= (pll
>> QCA955X_PLL_DDR_CONFIG_NINT_SHIFT
) &
394 QCA955X_PLL_DDR_CONFIG_NINT_MASK
;
395 frac
= (pll
>> QCA955X_PLL_DDR_CONFIG_NFRAC_SHIFT
) &
396 QCA955X_PLL_DDR_CONFIG_NFRAC_MASK
;
398 ddr_pll
= nint
* ref_rate
/ ref_div
;
399 ddr_pll
+= frac
* ref_rate
/ (ref_div
* (1 << 10));
400 ddr_pll
/= (1 << out_div
);
402 clk_ctrl
= ath79_pll_rr(QCA955X_PLL_CLK_CTRL_REG
);
404 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT
) &
405 QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK
;
407 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS
)
409 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL
)
410 cpu_rate
= ddr_pll
/ (postdiv
+ 1);
412 cpu_rate
= cpu_pll
/ (postdiv
+ 1);
414 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT
) &
415 QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK
;
417 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS
)
419 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL
)
420 ddr_rate
= cpu_pll
/ (postdiv
+ 1);
422 ddr_rate
= ddr_pll
/ (postdiv
+ 1);
424 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT
) &
425 QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK
;
427 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS
)
429 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL
)
430 ahb_rate
= ddr_pll
/ (postdiv
+ 1);
432 ahb_rate
= cpu_pll
/ (postdiv
+ 1);
434 ath79_add_sys_clkdev("ref", ref_rate
);
435 clks
[ATH79_CLK_CPU
] = ath79_add_sys_clkdev("cpu", cpu_rate
);
436 clks
[ATH79_CLK_DDR
] = ath79_add_sys_clkdev("ddr", ddr_rate
);
437 clks
[ATH79_CLK_AHB
] = ath79_add_sys_clkdev("ahb", ahb_rate
);
439 clk_add_alias("wdt", NULL
, "ref", NULL
);
440 clk_add_alias("uart", NULL
, "ref", NULL
);
443 void __init
ath79_clocks_init(void)
446 ar71xx_clocks_init();
447 else if (soc_is_ar724x() || soc_is_ar913x())
448 ar724x_clocks_init();
449 else if (soc_is_ar933x())
450 ar933x_clocks_init();
451 else if (soc_is_ar934x())
452 ar934x_clocks_init();
453 else if (soc_is_qca955x())
454 qca955x_clocks_init();
460 ath79_get_sys_clk_rate(const char *id
)
465 clk
= clk_get(NULL
, id
);
467 panic("unable to get %s clock, err=%d", id
, (int) PTR_ERR(clk
));
469 rate
= clk_get_rate(clk
);
476 static void __init
ath79_clocks_init_dt(struct device_node
*np
)
478 of_clk_add_provider(np
, of_clk_src_onecell_get
, &clk_data
);
481 CLK_OF_DECLARE(ar7100
, "qca,ar7100-pll", ath79_clocks_init_dt
);
482 CLK_OF_DECLARE(ar7240
, "qca,ar7240-pll", ath79_clocks_init_dt
);
483 CLK_OF_DECLARE(ar9340
, "qca,ar9340-pll", ath79_clocks_init_dt
);
484 CLK_OF_DECLARE(ar9550
, "qca,qca9550-pll", ath79_clocks_init_dt
);
486 static void __init
ath79_clocks_init_dt_ng(struct device_node
*np
)
489 void __iomem
*pll_base
;
490 const char *dnfn
= of_node_full_name(np
);
492 ref_clk
= of_clk_get(np
, 0);
493 if (IS_ERR(ref_clk
)) {
494 pr_err("%s: of_clk_get failed\n", dnfn
);
498 pll_base
= of_iomap(np
, 0);
500 pr_err("%s: can't map pll registers\n", dnfn
);
504 if (of_device_is_compatible(np
, "qca,ar9130-pll"))
505 ar724x_clk_init(ref_clk
, pll_base
);
506 else if (of_device_is_compatible(np
, "qca,ar9330-pll"))
507 ar9330_clk_init(ref_clk
, pll_base
);
509 pr_err("%s: could not find any appropriate clk_init()\n", dnfn
);
513 if (of_clk_add_provider(np
, of_clk_src_onecell_get
, &clk_data
)) {
514 pr_err("%s: could not register clk provider\n", dnfn
);
529 CLK_OF_DECLARE(ar9130_clk
, "qca,ar9130-pll", ath79_clocks_init_dt_ng
);
530 CLK_OF_DECLARE(ar9330_clk
, "qca,ar9330-pll", ath79_clocks_init_dt_ng
);