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 <asm/div64.h>
24 #include <asm/mach-ath79/ath79.h>
25 #include <asm/mach-ath79/ar71xx_regs.h>
28 #define AR71XX_BASE_FREQ 40000000
29 #define AR724X_BASE_FREQ 5000000
30 #define AR913X_BASE_FREQ 5000000
32 static struct clk
*clks
[3];
33 static struct clk_onecell_data clk_data
= {
35 .clk_num
= ARRAY_SIZE(clks
),
38 static struct clk
*__init
ath79_add_sys_clkdev(
39 const char *id
, unsigned long rate
)
44 clk
= clk_register_fixed_rate(NULL
, id
, NULL
, CLK_IS_ROOT
, rate
);
46 panic("failed to allocate %s clock structure", id
);
48 err
= clk_register_clkdev(clk
, id
, NULL
);
50 panic("unable to register %s clock device", id
);
55 static void __init
ar71xx_clocks_init(void)
57 unsigned long ref_rate
;
58 unsigned long cpu_rate
;
59 unsigned long ddr_rate
;
60 unsigned long ahb_rate
;
65 ref_rate
= AR71XX_BASE_FREQ
;
67 pll
= ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG
);
69 div
= ((pll
>> AR71XX_PLL_FB_SHIFT
) & AR71XX_PLL_FB_MASK
) + 1;
70 freq
= div
* ref_rate
;
72 div
= ((pll
>> AR71XX_CPU_DIV_SHIFT
) & AR71XX_CPU_DIV_MASK
) + 1;
73 cpu_rate
= freq
/ div
;
75 div
= ((pll
>> AR71XX_DDR_DIV_SHIFT
) & AR71XX_DDR_DIV_MASK
) + 1;
76 ddr_rate
= freq
/ div
;
78 div
= (((pll
>> AR71XX_AHB_DIV_SHIFT
) & AR71XX_AHB_DIV_MASK
) + 1) * 2;
79 ahb_rate
= cpu_rate
/ div
;
81 ath79_add_sys_clkdev("ref", ref_rate
);
82 clks
[0] = ath79_add_sys_clkdev("cpu", cpu_rate
);
83 clks
[1] = ath79_add_sys_clkdev("ddr", ddr_rate
);
84 clks
[2] = ath79_add_sys_clkdev("ahb", ahb_rate
);
86 clk_add_alias("wdt", NULL
, "ahb", NULL
);
87 clk_add_alias("uart", NULL
, "ahb", NULL
);
90 static void __init
ar724x_clocks_init(void)
92 unsigned long ref_rate
;
93 unsigned long cpu_rate
;
94 unsigned long ddr_rate
;
95 unsigned long ahb_rate
;
100 ref_rate
= AR724X_BASE_FREQ
;
101 pll
= ath79_pll_rr(AR724X_PLL_REG_CPU_CONFIG
);
103 div
= ((pll
>> AR724X_PLL_FB_SHIFT
) & AR724X_PLL_FB_MASK
);
104 freq
= div
* ref_rate
;
106 div
= ((pll
>> AR724X_PLL_REF_DIV_SHIFT
) & AR724X_PLL_REF_DIV_MASK
);
111 div
= ((pll
>> AR724X_DDR_DIV_SHIFT
) & AR724X_DDR_DIV_MASK
) + 1;
112 ddr_rate
= freq
/ div
;
114 div
= (((pll
>> AR724X_AHB_DIV_SHIFT
) & AR724X_AHB_DIV_MASK
) + 1) * 2;
115 ahb_rate
= cpu_rate
/ div
;
117 ath79_add_sys_clkdev("ref", ref_rate
);
118 clks
[0] = ath79_add_sys_clkdev("cpu", cpu_rate
);
119 clks
[1] = ath79_add_sys_clkdev("ddr", ddr_rate
);
120 clks
[2] = ath79_add_sys_clkdev("ahb", ahb_rate
);
122 clk_add_alias("wdt", NULL
, "ahb", NULL
);
123 clk_add_alias("uart", NULL
, "ahb", NULL
);
126 static void __init
ar913x_clocks_init(void)
128 unsigned long ref_rate
;
129 unsigned long cpu_rate
;
130 unsigned long ddr_rate
;
131 unsigned long ahb_rate
;
136 ref_rate
= AR913X_BASE_FREQ
;
137 pll
= ath79_pll_rr(AR913X_PLL_REG_CPU_CONFIG
);
139 div
= ((pll
>> AR913X_PLL_FB_SHIFT
) & AR913X_PLL_FB_MASK
);
140 freq
= div
* ref_rate
;
144 div
= ((pll
>> AR913X_DDR_DIV_SHIFT
) & AR913X_DDR_DIV_MASK
) + 1;
145 ddr_rate
= freq
/ div
;
147 div
= (((pll
>> AR913X_AHB_DIV_SHIFT
) & AR913X_AHB_DIV_MASK
) + 1) * 2;
148 ahb_rate
= cpu_rate
/ div
;
150 ath79_add_sys_clkdev("ref", ref_rate
);
151 clks
[0] = ath79_add_sys_clkdev("cpu", cpu_rate
);
152 clks
[1] = ath79_add_sys_clkdev("ddr", ddr_rate
);
153 clks
[2] = ath79_add_sys_clkdev("ahb", ahb_rate
);
155 clk_add_alias("wdt", NULL
, "ahb", NULL
);
156 clk_add_alias("uart", NULL
, "ahb", NULL
);
159 static void __init
ar933x_clocks_init(void)
161 unsigned long ref_rate
;
162 unsigned long cpu_rate
;
163 unsigned long ddr_rate
;
164 unsigned long ahb_rate
;
170 t
= ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP
);
171 if (t
& AR933X_BOOTSTRAP_REF_CLK_40
)
172 ref_rate
= (40 * 1000 * 1000);
174 ref_rate
= (25 * 1000 * 1000);
176 clock_ctrl
= ath79_pll_rr(AR933X_PLL_CLOCK_CTRL_REG
);
177 if (clock_ctrl
& AR933X_PLL_CLOCK_CTRL_BYPASS
) {
182 cpu_config
= ath79_pll_rr(AR933X_PLL_CPU_CONFIG_REG
);
184 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
185 AR933X_PLL_CPU_CONFIG_REFDIV_MASK
;
188 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_NINT_SHIFT
) &
189 AR933X_PLL_CPU_CONFIG_NINT_MASK
;
192 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
193 AR933X_PLL_CPU_CONFIG_OUTDIV_MASK
;
199 t
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT
) &
200 AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK
) + 1;
203 t
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT
) &
204 AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK
) + 1;
207 t
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT
) &
208 AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK
) + 1;
212 ath79_add_sys_clkdev("ref", ref_rate
);
213 clks
[0] = ath79_add_sys_clkdev("cpu", cpu_rate
);
214 clks
[1] = ath79_add_sys_clkdev("ddr", ddr_rate
);
215 clks
[2] = ath79_add_sys_clkdev("ahb", ahb_rate
);
217 clk_add_alias("wdt", NULL
, "ahb", NULL
);
218 clk_add_alias("uart", NULL
, "ref", NULL
);
221 static u32 __init
ar934x_get_pll_freq(u32 ref
, u32 ref_div
, u32 nint
, u32 nfrac
,
222 u32 frac
, u32 out_div
)
234 do_div(t
, ref_div
* frac
);
237 ret
/= (1 << out_div
);
241 static void __init
ar934x_clocks_init(void)
243 unsigned long ref_rate
;
244 unsigned long cpu_rate
;
245 unsigned long ddr_rate
;
246 unsigned long ahb_rate
;
247 u32 pll
, out_div
, ref_div
, nint
, nfrac
, frac
, clk_ctrl
, postdiv
;
248 u32 cpu_pll
, ddr_pll
;
250 void __iomem
*dpll_base
;
252 dpll_base
= ioremap(AR934X_SRIF_BASE
, AR934X_SRIF_SIZE
);
254 bootstrap
= ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP
);
255 if (bootstrap
& AR934X_BOOTSTRAP_REF_CLK_40
)
256 ref_rate
= 40 * 1000 * 1000;
258 ref_rate
= 25 * 1000 * 1000;
260 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_CPU_DPLL2_REG
);
261 if (pll
& AR934X_SRIF_DPLL2_LOCAL_PLL
) {
262 out_div
= (pll
>> AR934X_SRIF_DPLL2_OUTDIV_SHIFT
) &
263 AR934X_SRIF_DPLL2_OUTDIV_MASK
;
264 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_CPU_DPLL1_REG
);
265 nint
= (pll
>> AR934X_SRIF_DPLL1_NINT_SHIFT
) &
266 AR934X_SRIF_DPLL1_NINT_MASK
;
267 nfrac
= pll
& AR934X_SRIF_DPLL1_NFRAC_MASK
;
268 ref_div
= (pll
>> AR934X_SRIF_DPLL1_REFDIV_SHIFT
) &
269 AR934X_SRIF_DPLL1_REFDIV_MASK
;
272 pll
= ath79_pll_rr(AR934X_PLL_CPU_CONFIG_REG
);
273 out_div
= (pll
>> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
274 AR934X_PLL_CPU_CONFIG_OUTDIV_MASK
;
275 ref_div
= (pll
>> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
276 AR934X_PLL_CPU_CONFIG_REFDIV_MASK
;
277 nint
= (pll
>> AR934X_PLL_CPU_CONFIG_NINT_SHIFT
) &
278 AR934X_PLL_CPU_CONFIG_NINT_MASK
;
279 nfrac
= (pll
>> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT
) &
280 AR934X_PLL_CPU_CONFIG_NFRAC_MASK
;
284 cpu_pll
= ar934x_get_pll_freq(ref_rate
, ref_div
, nint
,
285 nfrac
, frac
, out_div
);
287 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_DDR_DPLL2_REG
);
288 if (pll
& AR934X_SRIF_DPLL2_LOCAL_PLL
) {
289 out_div
= (pll
>> AR934X_SRIF_DPLL2_OUTDIV_SHIFT
) &
290 AR934X_SRIF_DPLL2_OUTDIV_MASK
;
291 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_DDR_DPLL1_REG
);
292 nint
= (pll
>> AR934X_SRIF_DPLL1_NINT_SHIFT
) &
293 AR934X_SRIF_DPLL1_NINT_MASK
;
294 nfrac
= pll
& AR934X_SRIF_DPLL1_NFRAC_MASK
;
295 ref_div
= (pll
>> AR934X_SRIF_DPLL1_REFDIV_SHIFT
) &
296 AR934X_SRIF_DPLL1_REFDIV_MASK
;
299 pll
= ath79_pll_rr(AR934X_PLL_DDR_CONFIG_REG
);
300 out_div
= (pll
>> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT
) &
301 AR934X_PLL_DDR_CONFIG_OUTDIV_MASK
;
302 ref_div
= (pll
>> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT
) &
303 AR934X_PLL_DDR_CONFIG_REFDIV_MASK
;
304 nint
= (pll
>> AR934X_PLL_DDR_CONFIG_NINT_SHIFT
) &
305 AR934X_PLL_DDR_CONFIG_NINT_MASK
;
306 nfrac
= (pll
>> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT
) &
307 AR934X_PLL_DDR_CONFIG_NFRAC_MASK
;
311 ddr_pll
= ar934x_get_pll_freq(ref_rate
, ref_div
, nint
,
312 nfrac
, frac
, out_div
);
314 clk_ctrl
= ath79_pll_rr(AR934X_PLL_CPU_DDR_CLK_CTRL_REG
);
316 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_SHIFT
) &
317 AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_MASK
;
319 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS
)
321 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_CPUCLK_FROM_CPUPLL
)
322 cpu_rate
= cpu_pll
/ (postdiv
+ 1);
324 cpu_rate
= ddr_pll
/ (postdiv
+ 1);
326 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_SHIFT
) &
327 AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_MASK
;
329 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS
)
331 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_DDRCLK_FROM_DDRPLL
)
332 ddr_rate
= ddr_pll
/ (postdiv
+ 1);
334 ddr_rate
= cpu_pll
/ (postdiv
+ 1);
336 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_SHIFT
) &
337 AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_MASK
;
339 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS
)
341 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_AHBCLK_FROM_DDRPLL
)
342 ahb_rate
= ddr_pll
/ (postdiv
+ 1);
344 ahb_rate
= cpu_pll
/ (postdiv
+ 1);
346 ath79_add_sys_clkdev("ref", ref_rate
);
347 clks
[0] = ath79_add_sys_clkdev("cpu", cpu_rate
);
348 clks
[1] = ath79_add_sys_clkdev("ddr", ddr_rate
);
349 clks
[2] = ath79_add_sys_clkdev("ahb", ahb_rate
);
351 clk_add_alias("wdt", NULL
, "ref", NULL
);
352 clk_add_alias("uart", NULL
, "ref", NULL
);
357 static void __init
qca955x_clocks_init(void)
359 unsigned long ref_rate
;
360 unsigned long cpu_rate
;
361 unsigned long ddr_rate
;
362 unsigned long ahb_rate
;
363 u32 pll
, out_div
, ref_div
, nint
, frac
, clk_ctrl
, postdiv
;
364 u32 cpu_pll
, ddr_pll
;
367 bootstrap
= ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP
);
368 if (bootstrap
& QCA955X_BOOTSTRAP_REF_CLK_40
)
369 ref_rate
= 40 * 1000 * 1000;
371 ref_rate
= 25 * 1000 * 1000;
373 pll
= ath79_pll_rr(QCA955X_PLL_CPU_CONFIG_REG
);
374 out_div
= (pll
>> QCA955X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
375 QCA955X_PLL_CPU_CONFIG_OUTDIV_MASK
;
376 ref_div
= (pll
>> QCA955X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
377 QCA955X_PLL_CPU_CONFIG_REFDIV_MASK
;
378 nint
= (pll
>> QCA955X_PLL_CPU_CONFIG_NINT_SHIFT
) &
379 QCA955X_PLL_CPU_CONFIG_NINT_MASK
;
380 frac
= (pll
>> QCA955X_PLL_CPU_CONFIG_NFRAC_SHIFT
) &
381 QCA955X_PLL_CPU_CONFIG_NFRAC_MASK
;
383 cpu_pll
= nint
* ref_rate
/ ref_div
;
384 cpu_pll
+= frac
* ref_rate
/ (ref_div
* (1 << 6));
385 cpu_pll
/= (1 << out_div
);
387 pll
= ath79_pll_rr(QCA955X_PLL_DDR_CONFIG_REG
);
388 out_div
= (pll
>> QCA955X_PLL_DDR_CONFIG_OUTDIV_SHIFT
) &
389 QCA955X_PLL_DDR_CONFIG_OUTDIV_MASK
;
390 ref_div
= (pll
>> QCA955X_PLL_DDR_CONFIG_REFDIV_SHIFT
) &
391 QCA955X_PLL_DDR_CONFIG_REFDIV_MASK
;
392 nint
= (pll
>> QCA955X_PLL_DDR_CONFIG_NINT_SHIFT
) &
393 QCA955X_PLL_DDR_CONFIG_NINT_MASK
;
394 frac
= (pll
>> QCA955X_PLL_DDR_CONFIG_NFRAC_SHIFT
) &
395 QCA955X_PLL_DDR_CONFIG_NFRAC_MASK
;
397 ddr_pll
= nint
* ref_rate
/ ref_div
;
398 ddr_pll
+= frac
* ref_rate
/ (ref_div
* (1 << 10));
399 ddr_pll
/= (1 << out_div
);
401 clk_ctrl
= ath79_pll_rr(QCA955X_PLL_CLK_CTRL_REG
);
403 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT
) &
404 QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK
;
406 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS
)
408 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL
)
409 cpu_rate
= ddr_pll
/ (postdiv
+ 1);
411 cpu_rate
= cpu_pll
/ (postdiv
+ 1);
413 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT
) &
414 QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK
;
416 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS
)
418 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL
)
419 ddr_rate
= cpu_pll
/ (postdiv
+ 1);
421 ddr_rate
= ddr_pll
/ (postdiv
+ 1);
423 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT
) &
424 QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK
;
426 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS
)
428 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL
)
429 ahb_rate
= ddr_pll
/ (postdiv
+ 1);
431 ahb_rate
= cpu_pll
/ (postdiv
+ 1);
433 ath79_add_sys_clkdev("ref", ref_rate
);
434 clks
[0] = ath79_add_sys_clkdev("cpu", cpu_rate
);
435 clks
[1] = ath79_add_sys_clkdev("ddr", ddr_rate
);
436 clks
[2] = ath79_add_sys_clkdev("ahb", ahb_rate
);
438 clk_add_alias("wdt", NULL
, "ref", NULL
);
439 clk_add_alias("uart", NULL
, "ref", NULL
);
442 void __init
ath79_clocks_init(void)
445 ar71xx_clocks_init();
446 else if (soc_is_ar724x())
447 ar724x_clocks_init();
448 else if (soc_is_ar913x())
449 ar913x_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();
463 ath79_get_sys_clk_rate(const char *id
)
468 clk
= clk_get(NULL
, id
);
470 panic("unable to get %s clock, err=%d", id
, (int) PTR_ERR(clk
));
472 rate
= clk_get_rate(clk
);
479 static void __init
ath79_clocks_init_dt(struct device_node
*np
)
481 of_clk_add_provider(np
, of_clk_src_onecell_get
, &clk_data
);
484 CLK_OF_DECLARE(ar7100
, "qca,ar7100-pll", ath79_clocks_init_dt
);
485 CLK_OF_DECLARE(ar7240
, "qca,ar7240-pll", ath79_clocks_init_dt
);
486 CLK_OF_DECLARE(ar9130
, "qca,ar9130-pll", ath79_clocks_init_dt
);
487 CLK_OF_DECLARE(ar9330
, "qca,ar9330-pll", ath79_clocks_init_dt
);
488 CLK_OF_DECLARE(ar9340
, "qca,ar9340-pll", ath79_clocks_init_dt
);
489 CLK_OF_DECLARE(ar9550
, "qca,qca9550-pll", ath79_clocks_init_dt
);