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/module.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/clk.h>
19 #include <linux/clkdev.h>
20 #include <linux/clk-provider.h>
22 #include <linux/of_address.h>
23 #include <dt-bindings/clock/ath79-clk.h>
25 #include <asm/div64.h>
27 #include <asm/mach-ath79/ath79.h>
28 #include <asm/mach-ath79/ar71xx_regs.h>
30 #include "machtypes.h"
32 #define AR71XX_BASE_FREQ 40000000
33 #define AR724X_BASE_FREQ 40000000
35 static struct clk
*clks
[ATH79_CLK_END
];
36 static struct clk_onecell_data clk_data
= {
38 .clk_num
= ARRAY_SIZE(clks
),
41 static struct clk
*__init
ath79_add_sys_clkdev(
42 const char *id
, unsigned long rate
)
47 clk
= clk_register_fixed_rate(NULL
, id
, NULL
, 0, rate
);
49 panic("failed to allocate %s clock structure", id
);
51 err
= clk_register_clkdev(clk
, id
, NULL
);
53 panic("unable to register %s clock device", id
);
58 static void __init
ar71xx_clocks_init(void)
60 unsigned long ref_rate
;
61 unsigned long cpu_rate
;
62 unsigned long ddr_rate
;
63 unsigned long ahb_rate
;
68 ref_rate
= AR71XX_BASE_FREQ
;
70 pll
= ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG
);
72 div
= ((pll
>> AR71XX_PLL_FB_SHIFT
) & AR71XX_PLL_FB_MASK
) + 1;
73 freq
= div
* ref_rate
;
75 div
= ((pll
>> AR71XX_CPU_DIV_SHIFT
) & AR71XX_CPU_DIV_MASK
) + 1;
76 cpu_rate
= freq
/ div
;
78 div
= ((pll
>> AR71XX_DDR_DIV_SHIFT
) & AR71XX_DDR_DIV_MASK
) + 1;
79 ddr_rate
= freq
/ div
;
81 div
= (((pll
>> AR71XX_AHB_DIV_SHIFT
) & AR71XX_AHB_DIV_MASK
) + 1) * 2;
82 ahb_rate
= cpu_rate
/ div
;
84 ath79_add_sys_clkdev("ref", ref_rate
);
85 clks
[ATH79_CLK_CPU
] = ath79_add_sys_clkdev("cpu", cpu_rate
);
86 clks
[ATH79_CLK_DDR
] = ath79_add_sys_clkdev("ddr", ddr_rate
);
87 clks
[ATH79_CLK_AHB
] = ath79_add_sys_clkdev("ahb", ahb_rate
);
89 clk_add_alias("wdt", NULL
, "ahb", NULL
);
90 clk_add_alias("uart", NULL
, "ahb", NULL
);
93 static struct clk
* __init
ath79_reg_ffclk(const char *name
,
94 const char *parent_name
, unsigned int mult
, unsigned int div
)
98 clk
= clk_register_fixed_factor(NULL
, name
, parent_name
, 0, mult
, div
);
100 panic("failed to allocate %s clock structure", name
);
105 static void __init
ar724x_clk_init(struct clk
*ref_clk
, void __iomem
*pll_base
)
108 u32 mult
, div
, ddr_div
, ahb_div
;
110 pll
= __raw_readl(pll_base
+ AR724X_PLL_REG_CPU_CONFIG
);
112 mult
= ((pll
>> AR724X_PLL_FB_SHIFT
) & AR724X_PLL_FB_MASK
);
113 div
= ((pll
>> AR724X_PLL_REF_DIV_SHIFT
) & AR724X_PLL_REF_DIV_MASK
) * 2;
115 ddr_div
= ((pll
>> AR724X_DDR_DIV_SHIFT
) & AR724X_DDR_DIV_MASK
) + 1;
116 ahb_div
= (((pll
>> AR724X_AHB_DIV_SHIFT
) & AR724X_AHB_DIV_MASK
) + 1) * 2;
118 clks
[ATH79_CLK_CPU
] = ath79_reg_ffclk("cpu", "ref", mult
, div
);
119 clks
[ATH79_CLK_DDR
] = ath79_reg_ffclk("ddr", "ref", mult
, div
* ddr_div
);
120 clks
[ATH79_CLK_AHB
] = ath79_reg_ffclk("ahb", "ref", mult
, div
* ahb_div
);
123 static void __init
ar724x_clocks_init(void)
127 ref_clk
= ath79_add_sys_clkdev("ref", AR724X_BASE_FREQ
);
129 ar724x_clk_init(ref_clk
, ath79_pll_base
);
131 /* just make happy plat_time_init() from arch/mips/ath79/setup.c */
132 clk_register_clkdev(clks
[ATH79_CLK_CPU
], "cpu", NULL
);
133 clk_register_clkdev(clks
[ATH79_CLK_DDR
], "ddr", NULL
);
134 clk_register_clkdev(clks
[ATH79_CLK_AHB
], "ahb", NULL
);
136 clk_add_alias("wdt", NULL
, "ahb", NULL
);
137 clk_add_alias("uart", NULL
, "ahb", NULL
);
140 static void __init
ar9330_clk_init(struct clk
*ref_clk
, void __iomem
*pll_base
)
151 clock_ctrl
= __raw_readl(pll_base
+ AR933X_PLL_CLOCK_CTRL_REG
);
152 if (clock_ctrl
& AR933X_PLL_CLOCK_CTRL_BYPASS
) {
164 cpu_config
= __raw_readl(pll_base
+ AR933X_PLL_CPU_CONFIG_REG
);
166 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
167 AR933X_PLL_CPU_CONFIG_REFDIV_MASK
;
170 ninit_mul
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_NINT_SHIFT
) &
171 AR933X_PLL_CPU_CONFIG_NINT_MASK
;
173 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
174 AR933X_PLL_CPU_CONFIG_OUTDIV_MASK
;
180 cpu_div
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT
) &
181 AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK
) + 1;
183 ddr_div
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT
) &
184 AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK
) + 1;
186 ahb_div
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT
) &
187 AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK
) + 1;
190 clks
[ATH79_CLK_CPU
] = ath79_reg_ffclk("cpu", "ref",
191 ninit_mul
, ref_div
* out_div
* cpu_div
);
192 clks
[ATH79_CLK_DDR
] = ath79_reg_ffclk("ddr", "ref",
193 ninit_mul
, ref_div
* out_div
* ddr_div
);
194 clks
[ATH79_CLK_AHB
] = ath79_reg_ffclk("ahb", "ref",
195 ninit_mul
, ref_div
* out_div
* ahb_div
);
198 static void __init
ar933x_clocks_init(void)
201 unsigned long ref_rate
;
204 t
= ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP
);
205 if (t
& AR933X_BOOTSTRAP_REF_CLK_40
)
206 ref_rate
= (40 * 1000 * 1000);
208 ref_rate
= (25 * 1000 * 1000);
210 ref_clk
= ath79_add_sys_clkdev("ref", ref_rate
);
212 ar9330_clk_init(ref_clk
, ath79_pll_base
);
214 /* just make happy plat_time_init() from arch/mips/ath79/setup.c */
215 clk_register_clkdev(clks
[ATH79_CLK_CPU
], "cpu", NULL
);
216 clk_register_clkdev(clks
[ATH79_CLK_DDR
], "ddr", NULL
);
217 clk_register_clkdev(clks
[ATH79_CLK_AHB
], "ahb", NULL
);
219 clk_add_alias("wdt", NULL
, "ahb", NULL
);
220 clk_add_alias("uart", NULL
, "ref", NULL
);
223 static u32 __init
ar934x_get_pll_freq(u32 ref
, u32 ref_div
, u32 nint
, u32 nfrac
,
224 u32 frac
, u32 out_div
)
236 do_div(t
, ref_div
* frac
);
239 ret
/= (1 << out_div
);
243 static void __init
ar934x_clocks_init(void)
245 unsigned long ref_rate
;
246 unsigned long cpu_rate
;
247 unsigned long ddr_rate
;
248 unsigned long ahb_rate
;
249 u32 pll
, out_div
, ref_div
, nint
, nfrac
, frac
, clk_ctrl
, postdiv
;
250 u32 cpu_pll
, ddr_pll
;
252 void __iomem
*dpll_base
;
254 dpll_base
= ioremap(AR934X_SRIF_BASE
, AR934X_SRIF_SIZE
);
256 bootstrap
= ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP
);
257 if (bootstrap
& AR934X_BOOTSTRAP_REF_CLK_40
)
258 ref_rate
= 40 * 1000 * 1000;
260 ref_rate
= 25 * 1000 * 1000;
262 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_CPU_DPLL2_REG
);
263 if (pll
& AR934X_SRIF_DPLL2_LOCAL_PLL
) {
264 out_div
= (pll
>> AR934X_SRIF_DPLL2_OUTDIV_SHIFT
) &
265 AR934X_SRIF_DPLL2_OUTDIV_MASK
;
266 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_CPU_DPLL1_REG
);
267 nint
= (pll
>> AR934X_SRIF_DPLL1_NINT_SHIFT
) &
268 AR934X_SRIF_DPLL1_NINT_MASK
;
269 nfrac
= pll
& AR934X_SRIF_DPLL1_NFRAC_MASK
;
270 ref_div
= (pll
>> AR934X_SRIF_DPLL1_REFDIV_SHIFT
) &
271 AR934X_SRIF_DPLL1_REFDIV_MASK
;
274 pll
= ath79_pll_rr(AR934X_PLL_CPU_CONFIG_REG
);
275 out_div
= (pll
>> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
276 AR934X_PLL_CPU_CONFIG_OUTDIV_MASK
;
277 ref_div
= (pll
>> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
278 AR934X_PLL_CPU_CONFIG_REFDIV_MASK
;
279 nint
= (pll
>> AR934X_PLL_CPU_CONFIG_NINT_SHIFT
) &
280 AR934X_PLL_CPU_CONFIG_NINT_MASK
;
281 nfrac
= (pll
>> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT
) &
282 AR934X_PLL_CPU_CONFIG_NFRAC_MASK
;
286 cpu_pll
= ar934x_get_pll_freq(ref_rate
, ref_div
, nint
,
287 nfrac
, frac
, out_div
);
289 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_DDR_DPLL2_REG
);
290 if (pll
& AR934X_SRIF_DPLL2_LOCAL_PLL
) {
291 out_div
= (pll
>> AR934X_SRIF_DPLL2_OUTDIV_SHIFT
) &
292 AR934X_SRIF_DPLL2_OUTDIV_MASK
;
293 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_DDR_DPLL1_REG
);
294 nint
= (pll
>> AR934X_SRIF_DPLL1_NINT_SHIFT
) &
295 AR934X_SRIF_DPLL1_NINT_MASK
;
296 nfrac
= pll
& AR934X_SRIF_DPLL1_NFRAC_MASK
;
297 ref_div
= (pll
>> AR934X_SRIF_DPLL1_REFDIV_SHIFT
) &
298 AR934X_SRIF_DPLL1_REFDIV_MASK
;
301 pll
= ath79_pll_rr(AR934X_PLL_DDR_CONFIG_REG
);
302 out_div
= (pll
>> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT
) &
303 AR934X_PLL_DDR_CONFIG_OUTDIV_MASK
;
304 ref_div
= (pll
>> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT
) &
305 AR934X_PLL_DDR_CONFIG_REFDIV_MASK
;
306 nint
= (pll
>> AR934X_PLL_DDR_CONFIG_NINT_SHIFT
) &
307 AR934X_PLL_DDR_CONFIG_NINT_MASK
;
308 nfrac
= (pll
>> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT
) &
309 AR934X_PLL_DDR_CONFIG_NFRAC_MASK
;
313 ddr_pll
= ar934x_get_pll_freq(ref_rate
, ref_div
, nint
,
314 nfrac
, frac
, out_div
);
316 clk_ctrl
= ath79_pll_rr(AR934X_PLL_CPU_DDR_CLK_CTRL_REG
);
318 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_SHIFT
) &
319 AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_MASK
;
321 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS
)
323 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_CPUCLK_FROM_CPUPLL
)
324 cpu_rate
= cpu_pll
/ (postdiv
+ 1);
326 cpu_rate
= ddr_pll
/ (postdiv
+ 1);
328 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_SHIFT
) &
329 AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_MASK
;
331 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS
)
333 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_DDRCLK_FROM_DDRPLL
)
334 ddr_rate
= ddr_pll
/ (postdiv
+ 1);
336 ddr_rate
= cpu_pll
/ (postdiv
+ 1);
338 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_SHIFT
) &
339 AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_MASK
;
341 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS
)
343 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_AHBCLK_FROM_DDRPLL
)
344 ahb_rate
= ddr_pll
/ (postdiv
+ 1);
346 ahb_rate
= cpu_pll
/ (postdiv
+ 1);
348 ath79_add_sys_clkdev("ref", ref_rate
);
349 clks
[ATH79_CLK_CPU
] = ath79_add_sys_clkdev("cpu", cpu_rate
);
350 clks
[ATH79_CLK_DDR
] = ath79_add_sys_clkdev("ddr", ddr_rate
);
351 clks
[ATH79_CLK_AHB
] = ath79_add_sys_clkdev("ahb", ahb_rate
);
353 clk_add_alias("wdt", NULL
, "ref", NULL
);
354 clk_add_alias("uart", NULL
, "ref", NULL
);
359 static void __init
qca955x_clocks_init(void)
361 unsigned long ref_rate
;
362 unsigned long cpu_rate
;
363 unsigned long ddr_rate
;
364 unsigned long ahb_rate
;
365 u32 pll
, out_div
, ref_div
, nint
, frac
, clk_ctrl
, postdiv
;
366 u32 cpu_pll
, ddr_pll
;
369 bootstrap
= ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP
);
370 if (bootstrap
& QCA955X_BOOTSTRAP_REF_CLK_40
)
371 ref_rate
= 40 * 1000 * 1000;
373 ref_rate
= 25 * 1000 * 1000;
375 pll
= ath79_pll_rr(QCA955X_PLL_CPU_CONFIG_REG
);
376 out_div
= (pll
>> QCA955X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
377 QCA955X_PLL_CPU_CONFIG_OUTDIV_MASK
;
378 ref_div
= (pll
>> QCA955X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
379 QCA955X_PLL_CPU_CONFIG_REFDIV_MASK
;
380 nint
= (pll
>> QCA955X_PLL_CPU_CONFIG_NINT_SHIFT
) &
381 QCA955X_PLL_CPU_CONFIG_NINT_MASK
;
382 frac
= (pll
>> QCA955X_PLL_CPU_CONFIG_NFRAC_SHIFT
) &
383 QCA955X_PLL_CPU_CONFIG_NFRAC_MASK
;
385 cpu_pll
= nint
* ref_rate
/ ref_div
;
386 cpu_pll
+= frac
* ref_rate
/ (ref_div
* (1 << 6));
387 cpu_pll
/= (1 << out_div
);
389 pll
= ath79_pll_rr(QCA955X_PLL_DDR_CONFIG_REG
);
390 out_div
= (pll
>> QCA955X_PLL_DDR_CONFIG_OUTDIV_SHIFT
) &
391 QCA955X_PLL_DDR_CONFIG_OUTDIV_MASK
;
392 ref_div
= (pll
>> QCA955X_PLL_DDR_CONFIG_REFDIV_SHIFT
) &
393 QCA955X_PLL_DDR_CONFIG_REFDIV_MASK
;
394 nint
= (pll
>> QCA955X_PLL_DDR_CONFIG_NINT_SHIFT
) &
395 QCA955X_PLL_DDR_CONFIG_NINT_MASK
;
396 frac
= (pll
>> QCA955X_PLL_DDR_CONFIG_NFRAC_SHIFT
) &
397 QCA955X_PLL_DDR_CONFIG_NFRAC_MASK
;
399 ddr_pll
= nint
* ref_rate
/ ref_div
;
400 ddr_pll
+= frac
* ref_rate
/ (ref_div
* (1 << 10));
401 ddr_pll
/= (1 << out_div
);
403 clk_ctrl
= ath79_pll_rr(QCA955X_PLL_CLK_CTRL_REG
);
405 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT
) &
406 QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK
;
408 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS
)
410 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL
)
411 cpu_rate
= ddr_pll
/ (postdiv
+ 1);
413 cpu_rate
= cpu_pll
/ (postdiv
+ 1);
415 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT
) &
416 QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK
;
418 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS
)
420 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL
)
421 ddr_rate
= cpu_pll
/ (postdiv
+ 1);
423 ddr_rate
= ddr_pll
/ (postdiv
+ 1);
425 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT
) &
426 QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK
;
428 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS
)
430 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL
)
431 ahb_rate
= ddr_pll
/ (postdiv
+ 1);
433 ahb_rate
= cpu_pll
/ (postdiv
+ 1);
435 ath79_add_sys_clkdev("ref", ref_rate
);
436 clks
[ATH79_CLK_CPU
] = ath79_add_sys_clkdev("cpu", cpu_rate
);
437 clks
[ATH79_CLK_DDR
] = ath79_add_sys_clkdev("ddr", ddr_rate
);
438 clks
[ATH79_CLK_AHB
] = ath79_add_sys_clkdev("ahb", ahb_rate
);
440 clk_add_alias("wdt", NULL
, "ref", NULL
);
441 clk_add_alias("uart", NULL
, "ref", NULL
);
444 void __init
ath79_clocks_init(void)
447 ar71xx_clocks_init();
448 else if (soc_is_ar724x() || soc_is_ar913x())
449 ar724x_clocks_init();
450 else if (soc_is_ar933x())
451 ar933x_clocks_init();
452 else if (soc_is_ar934x())
453 ar934x_clocks_init();
454 else if (soc_is_qca955x())
455 qca955x_clocks_init();
461 ath79_get_sys_clk_rate(const char *id
)
466 clk
= clk_get(NULL
, id
);
468 panic("unable to get %s clock, err=%d", id
, (int) PTR_ERR(clk
));
470 rate
= clk_get_rate(clk
);
477 static void __init
ath79_clocks_init_dt(struct device_node
*np
)
479 of_clk_add_provider(np
, of_clk_src_onecell_get
, &clk_data
);
482 CLK_OF_DECLARE(ar7100
, "qca,ar7100-pll", ath79_clocks_init_dt
);
483 CLK_OF_DECLARE(ar7240
, "qca,ar7240-pll", ath79_clocks_init_dt
);
484 CLK_OF_DECLARE(ar9340
, "qca,ar9340-pll", ath79_clocks_init_dt
);
485 CLK_OF_DECLARE(ar9550
, "qca,qca9550-pll", ath79_clocks_init_dt
);
487 static void __init
ath79_clocks_init_dt_ng(struct device_node
*np
)
490 void __iomem
*pll_base
;
491 const char *dnfn
= of_node_full_name(np
);
493 ref_clk
= of_clk_get(np
, 0);
494 if (IS_ERR(ref_clk
)) {
495 pr_err("%s: of_clk_get failed\n", dnfn
);
499 pll_base
= of_iomap(np
, 0);
501 pr_err("%s: can't map pll registers\n", dnfn
);
505 if (of_device_is_compatible(np
, "qca,ar9130-pll"))
506 ar724x_clk_init(ref_clk
, pll_base
);
507 else if (of_device_is_compatible(np
, "qca,ar9330-pll"))
508 ar9330_clk_init(ref_clk
, pll_base
);
510 pr_err("%s: could not find any appropriate clk_init()\n", dnfn
);
514 if (of_clk_add_provider(np
, of_clk_src_onecell_get
, &clk_data
)) {
515 pr_err("%s: could not register clk provider\n", dnfn
);
527 CLK_OF_DECLARE(ar9130_clk
, "qca,ar9130-pll", ath79_clocks_init_dt_ng
);
528 CLK_OF_DECLARE(ar9330_clk
, "qca,ar9330-pll", ath79_clocks_init_dt_ng
);