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>
21 #include <asm/div64.h>
23 #include <asm/mach-ath79/ath79.h>
24 #include <asm/mach-ath79/ar71xx_regs.h>
27 #define AR71XX_BASE_FREQ 40000000
28 #define AR724X_BASE_FREQ 5000000
29 #define AR913X_BASE_FREQ 5000000
35 static void __init
ath79_add_sys_clkdev(const char *id
, unsigned long rate
)
40 clk
= kzalloc(sizeof(*clk
), GFP_KERNEL
);
42 panic("failed to allocate %s clock structure", id
);
46 err
= clk_register_clkdev(clk
, id
, NULL
);
48 panic("unable to register %s clock device", id
);
51 static void __init
ar71xx_clocks_init(void)
53 unsigned long ref_rate
;
54 unsigned long cpu_rate
;
55 unsigned long ddr_rate
;
56 unsigned long ahb_rate
;
61 ref_rate
= AR71XX_BASE_FREQ
;
63 pll
= ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG
);
65 div
= ((pll
>> AR71XX_PLL_DIV_SHIFT
) & AR71XX_PLL_DIV_MASK
) + 1;
66 freq
= div
* ref_rate
;
68 div
= ((pll
>> AR71XX_CPU_DIV_SHIFT
) & AR71XX_CPU_DIV_MASK
) + 1;
69 cpu_rate
= freq
/ div
;
71 div
= ((pll
>> AR71XX_DDR_DIV_SHIFT
) & AR71XX_DDR_DIV_MASK
) + 1;
72 ddr_rate
= freq
/ div
;
74 div
= (((pll
>> AR71XX_AHB_DIV_SHIFT
) & AR71XX_AHB_DIV_MASK
) + 1) * 2;
75 ahb_rate
= cpu_rate
/ div
;
77 ath79_add_sys_clkdev("ref", ref_rate
);
78 ath79_add_sys_clkdev("cpu", cpu_rate
);
79 ath79_add_sys_clkdev("ddr", ddr_rate
);
80 ath79_add_sys_clkdev("ahb", ahb_rate
);
82 clk_add_alias("wdt", NULL
, "ahb", NULL
);
83 clk_add_alias("uart", NULL
, "ahb", NULL
);
86 static void __init
ar724x_clocks_init(void)
88 unsigned long ref_rate
;
89 unsigned long cpu_rate
;
90 unsigned long ddr_rate
;
91 unsigned long ahb_rate
;
96 ref_rate
= AR724X_BASE_FREQ
;
97 pll
= ath79_pll_rr(AR724X_PLL_REG_CPU_CONFIG
);
99 div
= ((pll
>> AR724X_PLL_DIV_SHIFT
) & AR724X_PLL_DIV_MASK
);
100 freq
= div
* ref_rate
;
102 div
= ((pll
>> AR724X_PLL_REF_DIV_SHIFT
) & AR724X_PLL_REF_DIV_MASK
);
107 div
= ((pll
>> AR724X_DDR_DIV_SHIFT
) & AR724X_DDR_DIV_MASK
) + 1;
108 ddr_rate
= freq
/ div
;
110 div
= (((pll
>> AR724X_AHB_DIV_SHIFT
) & AR724X_AHB_DIV_MASK
) + 1) * 2;
111 ahb_rate
= cpu_rate
/ div
;
113 ath79_add_sys_clkdev("ref", ref_rate
);
114 ath79_add_sys_clkdev("cpu", cpu_rate
);
115 ath79_add_sys_clkdev("ddr", ddr_rate
);
116 ath79_add_sys_clkdev("ahb", ahb_rate
);
118 clk_add_alias("wdt", NULL
, "ahb", NULL
);
119 clk_add_alias("uart", NULL
, "ahb", NULL
);
122 static void __init
ar913x_clocks_init(void)
124 unsigned long ref_rate
;
125 unsigned long cpu_rate
;
126 unsigned long ddr_rate
;
127 unsigned long ahb_rate
;
132 ref_rate
= AR913X_BASE_FREQ
;
133 pll
= ath79_pll_rr(AR913X_PLL_REG_CPU_CONFIG
);
135 div
= ((pll
>> AR913X_PLL_DIV_SHIFT
) & AR913X_PLL_DIV_MASK
);
136 freq
= div
* ref_rate
;
140 div
= ((pll
>> AR913X_DDR_DIV_SHIFT
) & AR913X_DDR_DIV_MASK
) + 1;
141 ddr_rate
= freq
/ div
;
143 div
= (((pll
>> AR913X_AHB_DIV_SHIFT
) & AR913X_AHB_DIV_MASK
) + 1) * 2;
144 ahb_rate
= cpu_rate
/ div
;
146 ath79_add_sys_clkdev("ref", ref_rate
);
147 ath79_add_sys_clkdev("cpu", cpu_rate
);
148 ath79_add_sys_clkdev("ddr", ddr_rate
);
149 ath79_add_sys_clkdev("ahb", ahb_rate
);
151 clk_add_alias("wdt", NULL
, "ahb", NULL
);
152 clk_add_alias("uart", NULL
, "ahb", NULL
);
155 static void __init
ar933x_clocks_init(void)
157 unsigned long ref_rate
;
158 unsigned long cpu_rate
;
159 unsigned long ddr_rate
;
160 unsigned long ahb_rate
;
166 t
= ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP
);
167 if (t
& AR933X_BOOTSTRAP_REF_CLK_40
)
168 ref_rate
= (40 * 1000 * 1000);
170 ref_rate
= (25 * 1000 * 1000);
172 clock_ctrl
= ath79_pll_rr(AR933X_PLL_CLOCK_CTRL_REG
);
173 if (clock_ctrl
& AR933X_PLL_CLOCK_CTRL_BYPASS
) {
178 cpu_config
= ath79_pll_rr(AR933X_PLL_CPU_CONFIG_REG
);
180 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
181 AR933X_PLL_CPU_CONFIG_REFDIV_MASK
;
184 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_NINT_SHIFT
) &
185 AR933X_PLL_CPU_CONFIG_NINT_MASK
;
188 t
= (cpu_config
>> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
189 AR933X_PLL_CPU_CONFIG_OUTDIV_MASK
;
195 t
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT
) &
196 AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK
) + 1;
199 t
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT
) &
200 AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK
) + 1;
203 t
= ((clock_ctrl
>> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT
) &
204 AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK
) + 1;
208 ath79_add_sys_clkdev("ref", ref_rate
);
209 ath79_add_sys_clkdev("cpu", cpu_rate
);
210 ath79_add_sys_clkdev("ddr", ddr_rate
);
211 ath79_add_sys_clkdev("ahb", ahb_rate
);
213 clk_add_alias("wdt", NULL
, "ahb", NULL
);
214 clk_add_alias("uart", NULL
, "ref", NULL
);
217 static u32 __init
ar934x_get_pll_freq(u32 ref
, u32 ref_div
, u32 nint
, u32 nfrac
,
218 u32 frac
, u32 out_div
)
230 do_div(t
, ref_div
* frac
);
233 ret
/= (1 << out_div
);
237 static void __init
ar934x_clocks_init(void)
239 unsigned long ref_rate
;
240 unsigned long cpu_rate
;
241 unsigned long ddr_rate
;
242 unsigned long ahb_rate
;
243 u32 pll
, out_div
, ref_div
, nint
, nfrac
, frac
, clk_ctrl
, postdiv
;
244 u32 cpu_pll
, ddr_pll
;
246 void __iomem
*dpll_base
;
248 dpll_base
= ioremap(AR934X_SRIF_BASE
, AR934X_SRIF_SIZE
);
250 bootstrap
= ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP
);
251 if (bootstrap
& AR934X_BOOTSTRAP_REF_CLK_40
)
252 ref_rate
= 40 * 1000 * 1000;
254 ref_rate
= 25 * 1000 * 1000;
256 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_CPU_DPLL2_REG
);
257 if (pll
& AR934X_SRIF_DPLL2_LOCAL_PLL
) {
258 out_div
= (pll
>> AR934X_SRIF_DPLL2_OUTDIV_SHIFT
) &
259 AR934X_SRIF_DPLL2_OUTDIV_MASK
;
260 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_CPU_DPLL1_REG
);
261 nint
= (pll
>> AR934X_SRIF_DPLL1_NINT_SHIFT
) &
262 AR934X_SRIF_DPLL1_NINT_MASK
;
263 nfrac
= pll
& AR934X_SRIF_DPLL1_NFRAC_MASK
;
264 ref_div
= (pll
>> AR934X_SRIF_DPLL1_REFDIV_SHIFT
) &
265 AR934X_SRIF_DPLL1_REFDIV_MASK
;
268 pll
= ath79_pll_rr(AR934X_PLL_CPU_CONFIG_REG
);
269 out_div
= (pll
>> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
270 AR934X_PLL_CPU_CONFIG_OUTDIV_MASK
;
271 ref_div
= (pll
>> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
272 AR934X_PLL_CPU_CONFIG_REFDIV_MASK
;
273 nint
= (pll
>> AR934X_PLL_CPU_CONFIG_NINT_SHIFT
) &
274 AR934X_PLL_CPU_CONFIG_NINT_MASK
;
275 nfrac
= (pll
>> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT
) &
276 AR934X_PLL_CPU_CONFIG_NFRAC_MASK
;
280 cpu_pll
= ar934x_get_pll_freq(ref_rate
, ref_div
, nint
,
281 nfrac
, frac
, out_div
);
283 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_DDR_DPLL2_REG
);
284 if (pll
& AR934X_SRIF_DPLL2_LOCAL_PLL
) {
285 out_div
= (pll
>> AR934X_SRIF_DPLL2_OUTDIV_SHIFT
) &
286 AR934X_SRIF_DPLL2_OUTDIV_MASK
;
287 pll
= __raw_readl(dpll_base
+ AR934X_SRIF_DDR_DPLL1_REG
);
288 nint
= (pll
>> AR934X_SRIF_DPLL1_NINT_SHIFT
) &
289 AR934X_SRIF_DPLL1_NINT_MASK
;
290 nfrac
= pll
& AR934X_SRIF_DPLL1_NFRAC_MASK
;
291 ref_div
= (pll
>> AR934X_SRIF_DPLL1_REFDIV_SHIFT
) &
292 AR934X_SRIF_DPLL1_REFDIV_MASK
;
295 pll
= ath79_pll_rr(AR934X_PLL_DDR_CONFIG_REG
);
296 out_div
= (pll
>> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT
) &
297 AR934X_PLL_DDR_CONFIG_OUTDIV_MASK
;
298 ref_div
= (pll
>> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT
) &
299 AR934X_PLL_DDR_CONFIG_REFDIV_MASK
;
300 nint
= (pll
>> AR934X_PLL_DDR_CONFIG_NINT_SHIFT
) &
301 AR934X_PLL_DDR_CONFIG_NINT_MASK
;
302 nfrac
= (pll
>> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT
) &
303 AR934X_PLL_DDR_CONFIG_NFRAC_MASK
;
307 ddr_pll
= ar934x_get_pll_freq(ref_rate
, ref_div
, nint
,
308 nfrac
, frac
, out_div
);
310 clk_ctrl
= ath79_pll_rr(AR934X_PLL_CPU_DDR_CLK_CTRL_REG
);
312 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_SHIFT
) &
313 AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_MASK
;
315 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS
)
317 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_CPUCLK_FROM_CPUPLL
)
318 cpu_rate
= cpu_pll
/ (postdiv
+ 1);
320 cpu_rate
= ddr_pll
/ (postdiv
+ 1);
322 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_SHIFT
) &
323 AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_MASK
;
325 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS
)
327 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_DDRCLK_FROM_DDRPLL
)
328 ddr_rate
= ddr_pll
/ (postdiv
+ 1);
330 ddr_rate
= cpu_pll
/ (postdiv
+ 1);
332 postdiv
= (clk_ctrl
>> AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_SHIFT
) &
333 AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_MASK
;
335 if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS
)
337 else if (clk_ctrl
& AR934X_PLL_CPU_DDR_CLK_CTRL_AHBCLK_FROM_DDRPLL
)
338 ahb_rate
= ddr_pll
/ (postdiv
+ 1);
340 ahb_rate
= cpu_pll
/ (postdiv
+ 1);
342 ath79_add_sys_clkdev("ref", ref_rate
);
343 ath79_add_sys_clkdev("cpu", cpu_rate
);
344 ath79_add_sys_clkdev("ddr", ddr_rate
);
345 ath79_add_sys_clkdev("ahb", ahb_rate
);
347 clk_add_alias("wdt", NULL
, "ref", NULL
);
348 clk_add_alias("uart", NULL
, "ref", NULL
);
353 static void __init
qca955x_clocks_init(void)
355 unsigned long ref_rate
;
356 unsigned long cpu_rate
;
357 unsigned long ddr_rate
;
358 unsigned long ahb_rate
;
359 u32 pll
, out_div
, ref_div
, nint
, frac
, clk_ctrl
, postdiv
;
360 u32 cpu_pll
, ddr_pll
;
363 bootstrap
= ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP
);
364 if (bootstrap
& QCA955X_BOOTSTRAP_REF_CLK_40
)
365 ref_rate
= 40 * 1000 * 1000;
367 ref_rate
= 25 * 1000 * 1000;
369 pll
= ath79_pll_rr(QCA955X_PLL_CPU_CONFIG_REG
);
370 out_div
= (pll
>> QCA955X_PLL_CPU_CONFIG_OUTDIV_SHIFT
) &
371 QCA955X_PLL_CPU_CONFIG_OUTDIV_MASK
;
372 ref_div
= (pll
>> QCA955X_PLL_CPU_CONFIG_REFDIV_SHIFT
) &
373 QCA955X_PLL_CPU_CONFIG_REFDIV_MASK
;
374 nint
= (pll
>> QCA955X_PLL_CPU_CONFIG_NINT_SHIFT
) &
375 QCA955X_PLL_CPU_CONFIG_NINT_MASK
;
376 frac
= (pll
>> QCA955X_PLL_CPU_CONFIG_NFRAC_SHIFT
) &
377 QCA955X_PLL_CPU_CONFIG_NFRAC_MASK
;
379 cpu_pll
= nint
* ref_rate
/ ref_div
;
380 cpu_pll
+= frac
* ref_rate
/ (ref_div
* (1 << 6));
381 cpu_pll
/= (1 << out_div
);
383 pll
= ath79_pll_rr(QCA955X_PLL_DDR_CONFIG_REG
);
384 out_div
= (pll
>> QCA955X_PLL_DDR_CONFIG_OUTDIV_SHIFT
) &
385 QCA955X_PLL_DDR_CONFIG_OUTDIV_MASK
;
386 ref_div
= (pll
>> QCA955X_PLL_DDR_CONFIG_REFDIV_SHIFT
) &
387 QCA955X_PLL_DDR_CONFIG_REFDIV_MASK
;
388 nint
= (pll
>> QCA955X_PLL_DDR_CONFIG_NINT_SHIFT
) &
389 QCA955X_PLL_DDR_CONFIG_NINT_MASK
;
390 frac
= (pll
>> QCA955X_PLL_DDR_CONFIG_NFRAC_SHIFT
) &
391 QCA955X_PLL_DDR_CONFIG_NFRAC_MASK
;
393 ddr_pll
= nint
* ref_rate
/ ref_div
;
394 ddr_pll
+= frac
* ref_rate
/ (ref_div
* (1 << 10));
395 ddr_pll
/= (1 << out_div
);
397 clk_ctrl
= ath79_pll_rr(QCA955X_PLL_CLK_CTRL_REG
);
399 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT
) &
400 QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK
;
402 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS
)
404 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL
)
405 cpu_rate
= ddr_pll
/ (postdiv
+ 1);
407 cpu_rate
= cpu_pll
/ (postdiv
+ 1);
409 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT
) &
410 QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK
;
412 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS
)
414 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL
)
415 ddr_rate
= cpu_pll
/ (postdiv
+ 1);
417 ddr_rate
= ddr_pll
/ (postdiv
+ 1);
419 postdiv
= (clk_ctrl
>> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT
) &
420 QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK
;
422 if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS
)
424 else if (clk_ctrl
& QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL
)
425 ahb_rate
= ddr_pll
/ (postdiv
+ 1);
427 ahb_rate
= cpu_pll
/ (postdiv
+ 1);
429 ath79_add_sys_clkdev("ref", ref_rate
);
430 ath79_add_sys_clkdev("cpu", cpu_rate
);
431 ath79_add_sys_clkdev("ddr", ddr_rate
);
432 ath79_add_sys_clkdev("ahb", ahb_rate
);
434 clk_add_alias("wdt", NULL
, "ref", NULL
);
435 clk_add_alias("uart", NULL
, "ref", NULL
);
438 void __init
ath79_clocks_init(void)
441 ar71xx_clocks_init();
442 else if (soc_is_ar724x())
443 ar724x_clocks_init();
444 else if (soc_is_ar913x())
445 ar913x_clocks_init();
446 else if (soc_is_ar933x())
447 ar933x_clocks_init();
448 else if (soc_is_ar934x())
449 ar934x_clocks_init();
450 else if (soc_is_qca955x())
451 qca955x_clocks_init();
457 ath79_get_sys_clk_rate(const char *id
)
462 clk
= clk_get(NULL
, id
);
464 panic("unable to get %s clock, err=%d", id
, (int) PTR_ERR(clk
));
466 rate
= clk_get_rate(clk
);
475 int clk_enable(struct clk
*clk
)
479 EXPORT_SYMBOL(clk_enable
);
481 void clk_disable(struct clk
*clk
)
484 EXPORT_SYMBOL(clk_disable
);
486 unsigned long clk_get_rate(struct clk
*clk
)
490 EXPORT_SYMBOL(clk_get_rate
);