blob: eb5117ced95ac0a49dc92a3caa1cc32b56b19022 [file] [log] [blame]
Gabor Juhosd4a67d92011-01-04 21:28:14 +01001/*
2 * Atheros AR71XX/AR724X/AR913X common routines
3 *
Gabor Juhos88896122012-03-14 10:45:22 +01004 * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com>
Gabor Juhosd4a67d92011-01-04 21:28:14 +01005 * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
6 *
Gabor Juhos88896122012-03-14 10:45:22 +01007 * Parts of this file are based on Atheros' 2.6.15/2.6.31 BSP
8 *
Gabor Juhosd4a67d92011-01-04 21:28:14 +01009 * 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.
12 */
13
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>
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +020019#include <linux/clkdev.h>
Alban Bedel411520a2015-04-19 14:30:04 +020020#include <linux/clk-provider.h>
Gabor Juhosd4a67d92011-01-04 21:28:14 +010021
Gabor Juhos97541cc2012-09-08 14:02:21 +020022#include <asm/div64.h>
23
Gabor Juhosd4a67d92011-01-04 21:28:14 +010024#include <asm/mach-ath79/ath79.h>
25#include <asm/mach-ath79/ar71xx_regs.h>
26#include "common.h"
27
28#define AR71XX_BASE_FREQ 40000000
29#define AR724X_BASE_FREQ 5000000
30#define AR913X_BASE_FREQ 5000000
31
Alban Bedel6451af02015-05-31 02:18:22 +020032static struct clk *clks[3];
33static struct clk_onecell_data clk_data = {
34 .clks = clks,
35 .clk_num = ARRAY_SIZE(clks),
36};
37
38static struct clk *__init ath79_add_sys_clkdev(
39 const char *id, unsigned long rate)
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +020040{
41 struct clk *clk;
42 int err;
43
Alban Bedel411520a2015-04-19 14:30:04 +020044 clk = clk_register_fixed_rate(NULL, id, NULL, CLK_IS_ROOT, rate);
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +020045 if (!clk)
46 panic("failed to allocate %s clock structure", id);
47
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +020048 err = clk_register_clkdev(clk, id, NULL);
49 if (err)
50 panic("unable to register %s clock device", id);
Alban Bedel6451af02015-05-31 02:18:22 +020051
52 return clk;
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +020053}
Gabor Juhosd4a67d92011-01-04 21:28:14 +010054
55static void __init ar71xx_clocks_init(void)
56{
Gabor Juhos6612a682013-08-28 10:41:46 +020057 unsigned long ref_rate;
58 unsigned long cpu_rate;
59 unsigned long ddr_rate;
60 unsigned long ahb_rate;
Gabor Juhosd4a67d92011-01-04 21:28:14 +010061 u32 pll;
62 u32 freq;
63 u32 div;
64
Gabor Juhos6612a682013-08-28 10:41:46 +020065 ref_rate = AR71XX_BASE_FREQ;
Gabor Juhosd4a67d92011-01-04 21:28:14 +010066
67 pll = ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG);
68
Alban Bedel626a0692015-04-19 14:30:02 +020069 div = ((pll >> AR71XX_PLL_FB_SHIFT) & AR71XX_PLL_FB_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +020070 freq = div * ref_rate;
Gabor Juhosd4a67d92011-01-04 21:28:14 +010071
72 div = ((pll >> AR71XX_CPU_DIV_SHIFT) & AR71XX_CPU_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +020073 cpu_rate = freq / div;
Gabor Juhosd4a67d92011-01-04 21:28:14 +010074
75 div = ((pll >> AR71XX_DDR_DIV_SHIFT) & AR71XX_DDR_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +020076 ddr_rate = freq / div;
Gabor Juhosd4a67d92011-01-04 21:28:14 +010077
78 div = (((pll >> AR71XX_AHB_DIV_SHIFT) & AR71XX_AHB_DIV_MASK) + 1) * 2;
Gabor Juhos6612a682013-08-28 10:41:46 +020079 ahb_rate = cpu_rate / div;
80
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +020081 ath79_add_sys_clkdev("ref", ref_rate);
Alban Bedel6451af02015-05-31 02:18:22 +020082 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);
Gabor Juhosd4a67d92011-01-04 21:28:14 +010085
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +020086 clk_add_alias("wdt", NULL, "ahb", NULL);
87 clk_add_alias("uart", NULL, "ahb", NULL);
Gabor Juhosd4a67d92011-01-04 21:28:14 +010088}
89
90static void __init ar724x_clocks_init(void)
91{
Gabor Juhos6612a682013-08-28 10:41:46 +020092 unsigned long ref_rate;
93 unsigned long cpu_rate;
94 unsigned long ddr_rate;
95 unsigned long ahb_rate;
Gabor Juhosd4a67d92011-01-04 21:28:14 +010096 u32 pll;
97 u32 freq;
98 u32 div;
99
Gabor Juhos6612a682013-08-28 10:41:46 +0200100 ref_rate = AR724X_BASE_FREQ;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100101 pll = ath79_pll_rr(AR724X_PLL_REG_CPU_CONFIG);
102
Alban Bedel626a0692015-04-19 14:30:02 +0200103 div = ((pll >> AR724X_PLL_FB_SHIFT) & AR724X_PLL_FB_MASK);
Gabor Juhos6612a682013-08-28 10:41:46 +0200104 freq = div * ref_rate;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100105
106 div = ((pll >> AR724X_PLL_REF_DIV_SHIFT) & AR724X_PLL_REF_DIV_MASK);
107 freq *= div;
108
Gabor Juhos6612a682013-08-28 10:41:46 +0200109 cpu_rate = freq;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100110
111 div = ((pll >> AR724X_DDR_DIV_SHIFT) & AR724X_DDR_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +0200112 ddr_rate = freq / div;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100113
114 div = (((pll >> AR724X_AHB_DIV_SHIFT) & AR724X_AHB_DIV_MASK) + 1) * 2;
Gabor Juhos6612a682013-08-28 10:41:46 +0200115 ahb_rate = cpu_rate / div;
116
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200117 ath79_add_sys_clkdev("ref", ref_rate);
Alban Bedel6451af02015-05-31 02:18:22 +0200118 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);
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100121
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200122 clk_add_alias("wdt", NULL, "ahb", NULL);
123 clk_add_alias("uart", NULL, "ahb", NULL);
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100124}
125
126static void __init ar913x_clocks_init(void)
127{
Gabor Juhos6612a682013-08-28 10:41:46 +0200128 unsigned long ref_rate;
129 unsigned long cpu_rate;
130 unsigned long ddr_rate;
131 unsigned long ahb_rate;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100132 u32 pll;
133 u32 freq;
134 u32 div;
135
Gabor Juhos6612a682013-08-28 10:41:46 +0200136 ref_rate = AR913X_BASE_FREQ;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100137 pll = ath79_pll_rr(AR913X_PLL_REG_CPU_CONFIG);
138
Alban Bedel626a0692015-04-19 14:30:02 +0200139 div = ((pll >> AR913X_PLL_FB_SHIFT) & AR913X_PLL_FB_MASK);
Gabor Juhos6612a682013-08-28 10:41:46 +0200140 freq = div * ref_rate;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100141
Gabor Juhos6612a682013-08-28 10:41:46 +0200142 cpu_rate = freq;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100143
144 div = ((pll >> AR913X_DDR_DIV_SHIFT) & AR913X_DDR_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +0200145 ddr_rate = freq / div;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100146
147 div = (((pll >> AR913X_AHB_DIV_SHIFT) & AR913X_AHB_DIV_MASK) + 1) * 2;
Gabor Juhos6612a682013-08-28 10:41:46 +0200148 ahb_rate = cpu_rate / div;
149
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200150 ath79_add_sys_clkdev("ref", ref_rate);
Alban Bedel6451af02015-05-31 02:18:22 +0200151 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);
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100154
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200155 clk_add_alias("wdt", NULL, "ahb", NULL);
156 clk_add_alias("uart", NULL, "ahb", NULL);
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100157}
158
Gabor Juhos04225e12011-06-20 21:26:04 +0200159static void __init ar933x_clocks_init(void)
160{
Gabor Juhos6612a682013-08-28 10:41:46 +0200161 unsigned long ref_rate;
162 unsigned long cpu_rate;
163 unsigned long ddr_rate;
164 unsigned long ahb_rate;
Gabor Juhos04225e12011-06-20 21:26:04 +0200165 u32 clock_ctrl;
166 u32 cpu_config;
167 u32 freq;
168 u32 t;
169
170 t = ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP);
171 if (t & AR933X_BOOTSTRAP_REF_CLK_40)
Gabor Juhos6612a682013-08-28 10:41:46 +0200172 ref_rate = (40 * 1000 * 1000);
Gabor Juhos04225e12011-06-20 21:26:04 +0200173 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200174 ref_rate = (25 * 1000 * 1000);
Gabor Juhos04225e12011-06-20 21:26:04 +0200175
176 clock_ctrl = ath79_pll_rr(AR933X_PLL_CLOCK_CTRL_REG);
177 if (clock_ctrl & AR933X_PLL_CLOCK_CTRL_BYPASS) {
Gabor Juhos6612a682013-08-28 10:41:46 +0200178 cpu_rate = ref_rate;
179 ahb_rate = ref_rate;
180 ddr_rate = ref_rate;
Gabor Juhos04225e12011-06-20 21:26:04 +0200181 } else {
182 cpu_config = ath79_pll_rr(AR933X_PLL_CPU_CONFIG_REG);
183
184 t = (cpu_config >> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
185 AR933X_PLL_CPU_CONFIG_REFDIV_MASK;
Gabor Juhos6612a682013-08-28 10:41:46 +0200186 freq = ref_rate / t;
Gabor Juhos04225e12011-06-20 21:26:04 +0200187
188 t = (cpu_config >> AR933X_PLL_CPU_CONFIG_NINT_SHIFT) &
189 AR933X_PLL_CPU_CONFIG_NINT_MASK;
190 freq *= t;
191
192 t = (cpu_config >> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
193 AR933X_PLL_CPU_CONFIG_OUTDIV_MASK;
194 if (t == 0)
195 t = 1;
196
197 freq >>= t;
198
199 t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT) &
200 AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +0200201 cpu_rate = freq / t;
Gabor Juhos04225e12011-06-20 21:26:04 +0200202
203 t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT) &
204 AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +0200205 ddr_rate = freq / t;
Gabor Juhos04225e12011-06-20 21:26:04 +0200206
207 t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT) &
208 AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +0200209 ahb_rate = freq / t;
Gabor Juhos04225e12011-06-20 21:26:04 +0200210 }
211
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200212 ath79_add_sys_clkdev("ref", ref_rate);
Alban Bedel6451af02015-05-31 02:18:22 +0200213 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);
Gabor Juhos6612a682013-08-28 10:41:46 +0200216
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200217 clk_add_alias("wdt", NULL, "ahb", NULL);
218 clk_add_alias("uart", NULL, "ref", NULL);
Gabor Juhos04225e12011-06-20 21:26:04 +0200219}
220
Gabor Juhos97541cc2012-09-08 14:02:21 +0200221static u32 __init ar934x_get_pll_freq(u32 ref, u32 ref_div, u32 nint, u32 nfrac,
222 u32 frac, u32 out_div)
223{
224 u64 t;
225 u32 ret;
226
Gabor Juhos837f0362013-08-28 10:41:43 +0200227 t = ref;
Gabor Juhos97541cc2012-09-08 14:02:21 +0200228 t *= nint;
229 do_div(t, ref_div);
230 ret = t;
231
Gabor Juhos837f0362013-08-28 10:41:43 +0200232 t = ref;
Gabor Juhos97541cc2012-09-08 14:02:21 +0200233 t *= nfrac;
234 do_div(t, ref_div * frac);
235 ret += t;
236
237 ret /= (1 << out_div);
238 return ret;
239}
240
Gabor Juhos88896122012-03-14 10:45:22 +0100241static void __init ar934x_clocks_init(void)
242{
Gabor Juhos6612a682013-08-28 10:41:46 +0200243 unsigned long ref_rate;
244 unsigned long cpu_rate;
245 unsigned long ddr_rate;
246 unsigned long ahb_rate;
Gabor Juhos97541cc2012-09-08 14:02:21 +0200247 u32 pll, out_div, ref_div, nint, nfrac, frac, clk_ctrl, postdiv;
Gabor Juhos88896122012-03-14 10:45:22 +0100248 u32 cpu_pll, ddr_pll;
249 u32 bootstrap;
Gabor Juhos97541cc2012-09-08 14:02:21 +0200250 void __iomem *dpll_base;
251
252 dpll_base = ioremap(AR934X_SRIF_BASE, AR934X_SRIF_SIZE);
Gabor Juhos88896122012-03-14 10:45:22 +0100253
254 bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP);
Ralf Baechle70342282013-01-22 12:59:30 +0100255 if (bootstrap & AR934X_BOOTSTRAP_REF_CLK_40)
Gabor Juhos6612a682013-08-28 10:41:46 +0200256 ref_rate = 40 * 1000 * 1000;
Gabor Juhos88896122012-03-14 10:45:22 +0100257 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200258 ref_rate = 25 * 1000 * 1000;
Gabor Juhos88896122012-03-14 10:45:22 +0100259
Gabor Juhos97541cc2012-09-08 14:02:21 +0200260 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;
270 frac = 1 << 18;
271 } else {
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;
281 frac = 1 << 6;
282 }
Gabor Juhos88896122012-03-14 10:45:22 +0100283
Gabor Juhos6612a682013-08-28 10:41:46 +0200284 cpu_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint,
Gabor Juhos97541cc2012-09-08 14:02:21 +0200285 nfrac, frac, out_div);
Gabor Juhos88896122012-03-14 10:45:22 +0100286
Gabor Juhos97541cc2012-09-08 14:02:21 +0200287 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;
297 frac = 1 << 18;
298 } else {
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;
308 frac = 1 << 10;
309 }
Gabor Juhos88896122012-03-14 10:45:22 +0100310
Gabor Juhos6612a682013-08-28 10:41:46 +0200311 ddr_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint,
Gabor Juhos97541cc2012-09-08 14:02:21 +0200312 nfrac, frac, out_div);
Gabor Juhos88896122012-03-14 10:45:22 +0100313
314 clk_ctrl = ath79_pll_rr(AR934X_PLL_CPU_DDR_CLK_CTRL_REG);
315
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;
318
319 if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200320 cpu_rate = ref_rate;
Gabor Juhos88896122012-03-14 10:45:22 +0100321 else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_CPUCLK_FROM_CPUPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200322 cpu_rate = cpu_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100323 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200324 cpu_rate = ddr_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100325
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;
328
329 if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200330 ddr_rate = ref_rate;
Gabor Juhos88896122012-03-14 10:45:22 +0100331 else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_DDRCLK_FROM_DDRPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200332 ddr_rate = ddr_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100333 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200334 ddr_rate = cpu_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100335
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;
338
339 if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200340 ahb_rate = ref_rate;
Gabor Juhos88896122012-03-14 10:45:22 +0100341 else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_AHBCLK_FROM_DDRPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200342 ahb_rate = ddr_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100343 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200344 ahb_rate = cpu_pll / (postdiv + 1);
345
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200346 ath79_add_sys_clkdev("ref", ref_rate);
Alban Bedel6451af02015-05-31 02:18:22 +0200347 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);
Gabor Juhos88896122012-03-14 10:45:22 +0100350
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200351 clk_add_alias("wdt", NULL, "ref", NULL);
352 clk_add_alias("uart", NULL, "ref", NULL);
Gabor Juhos97541cc2012-09-08 14:02:21 +0200353
354 iounmap(dpll_base);
Gabor Juhos88896122012-03-14 10:45:22 +0100355}
356
Gabor Juhos41583c02013-02-15 13:38:17 +0000357static void __init qca955x_clocks_init(void)
358{
Gabor Juhos6612a682013-08-28 10:41:46 +0200359 unsigned long ref_rate;
360 unsigned long cpu_rate;
361 unsigned long ddr_rate;
362 unsigned long ahb_rate;
Gabor Juhos41583c02013-02-15 13:38:17 +0000363 u32 pll, out_div, ref_div, nint, frac, clk_ctrl, postdiv;
364 u32 cpu_pll, ddr_pll;
365 u32 bootstrap;
366
367 bootstrap = ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP);
368 if (bootstrap & QCA955X_BOOTSTRAP_REF_CLK_40)
Gabor Juhos6612a682013-08-28 10:41:46 +0200369 ref_rate = 40 * 1000 * 1000;
Gabor Juhos41583c02013-02-15 13:38:17 +0000370 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200371 ref_rate = 25 * 1000 * 1000;
Gabor Juhos41583c02013-02-15 13:38:17 +0000372
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;
382
Gabor Juhos6612a682013-08-28 10:41:46 +0200383 cpu_pll = nint * ref_rate / ref_div;
384 cpu_pll += frac * ref_rate / (ref_div * (1 << 6));
Gabor Juhos41583c02013-02-15 13:38:17 +0000385 cpu_pll /= (1 << out_div);
386
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;
396
Gabor Juhos6612a682013-08-28 10:41:46 +0200397 ddr_pll = nint * ref_rate / ref_div;
398 ddr_pll += frac * ref_rate / (ref_div * (1 << 10));
Gabor Juhos41583c02013-02-15 13:38:17 +0000399 ddr_pll /= (1 << out_div);
400
401 clk_ctrl = ath79_pll_rr(QCA955X_PLL_CLK_CTRL_REG);
402
403 postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) &
404 QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK;
405
406 if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200407 cpu_rate = ref_rate;
Gabor Juhos41583c02013-02-15 13:38:17 +0000408 else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200409 cpu_rate = ddr_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000410 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200411 cpu_rate = cpu_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000412
413 postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) &
414 QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK;
415
416 if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200417 ddr_rate = ref_rate;
Gabor Juhos41583c02013-02-15 13:38:17 +0000418 else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200419 ddr_rate = cpu_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000420 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200421 ddr_rate = ddr_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000422
423 postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) &
424 QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK;
425
426 if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200427 ahb_rate = ref_rate;
Gabor Juhos41583c02013-02-15 13:38:17 +0000428 else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200429 ahb_rate = ddr_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000430 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200431 ahb_rate = cpu_pll / (postdiv + 1);
432
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200433 ath79_add_sys_clkdev("ref", ref_rate);
Alban Bedel6451af02015-05-31 02:18:22 +0200434 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);
Gabor Juhos41583c02013-02-15 13:38:17 +0000437
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200438 clk_add_alias("wdt", NULL, "ref", NULL);
439 clk_add_alias("uart", NULL, "ref", NULL);
Gabor Juhos41583c02013-02-15 13:38:17 +0000440}
441
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100442void __init ath79_clocks_init(void)
443{
444 if (soc_is_ar71xx())
445 ar71xx_clocks_init();
446 else if (soc_is_ar724x())
447 ar724x_clocks_init();
448 else if (soc_is_ar913x())
449 ar913x_clocks_init();
Gabor Juhos04225e12011-06-20 21:26:04 +0200450 else if (soc_is_ar933x())
451 ar933x_clocks_init();
Gabor Juhos88896122012-03-14 10:45:22 +0100452 else if (soc_is_ar934x())
453 ar934x_clocks_init();
Gabor Juhos41583c02013-02-15 13:38:17 +0000454 else if (soc_is_qca955x())
455 qca955x_clocks_init();
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100456 else
457 BUG();
Alban Bedel6451af02015-05-31 02:18:22 +0200458
459 of_clk_init(NULL);
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100460}
461
Gabor Juhos23107802013-08-28 10:41:44 +0200462unsigned long __init
463ath79_get_sys_clk_rate(const char *id)
464{
465 struct clk *clk;
466 unsigned long rate;
467
468 clk = clk_get(NULL, id);
469 if (IS_ERR(clk))
470 panic("unable to get %s clock, err=%d", id, (int) PTR_ERR(clk));
471
472 rate = clk_get_rate(clk);
473 clk_put(clk);
474
475 return rate;
476}
Alban Bedel6451af02015-05-31 02:18:22 +0200477
478#ifdef CONFIG_OF
479static void __init ath79_clocks_init_dt(struct device_node *np)
480{
481 of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
482}
483
484CLK_OF_DECLARE(ar7100, "qca,ar7100-pll", ath79_clocks_init_dt);
485CLK_OF_DECLARE(ar7240, "qca,ar7240-pll", ath79_clocks_init_dt);
486CLK_OF_DECLARE(ar9130, "qca,ar9130-pll", ath79_clocks_init_dt);
487CLK_OF_DECLARE(ar9330, "qca,ar9330-pll", ath79_clocks_init_dt);
488CLK_OF_DECLARE(ar9340, "qca,ar9340-pll", ath79_clocks_init_dt);
489CLK_OF_DECLARE(ar9550, "qca,qca9550-pll", ath79_clocks_init_dt);
490#endif