blob: c3a94ead06abe05d6b72f42294ae1b4467826898 [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>
Antony Pavlovaf5ad0d2016-03-17 06:34:14 +030021#include <dt-bindings/clock/ath79-clk.h>
Gabor Juhosd4a67d92011-01-04 21:28:14 +010022
Gabor Juhos97541cc2012-09-08 14:02:21 +020023#include <asm/div64.h>
24
Gabor Juhosd4a67d92011-01-04 21:28:14 +010025#include <asm/mach-ath79/ath79.h>
26#include <asm/mach-ath79/ar71xx_regs.h>
27#include "common.h"
28
29#define AR71XX_BASE_FREQ 40000000
Weijie Gaoc338d592016-03-17 06:34:09 +030030#define AR724X_BASE_FREQ 40000000
Gabor Juhosd4a67d92011-01-04 21:28:14 +010031
Antony Pavlovaf5ad0d2016-03-17 06:34:14 +030032static struct clk *clks[ATH79_CLK_END];
Alban Bedel6451af02015-05-31 02:18:22 +020033static 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);
Antony Pavlovaf5ad0d2016-03-17 06:34:14 +030082 clks[ATH79_CLK_CPU] = ath79_add_sys_clkdev("cpu", cpu_rate);
83 clks[ATH79_CLK_DDR] = ath79_add_sys_clkdev("ddr", ddr_rate);
84 clks[ATH79_CLK_AHB] = 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
Weijie Gaoc338d592016-03-17 06:34:09 +0300106 div = ((pll >> AR724X_PLL_REF_DIV_SHIFT) & AR724X_PLL_REF_DIV_MASK) * 2;
107 freq /= div;
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100108
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);
Antony Pavlovaf5ad0d2016-03-17 06:34:14 +0300118 clks[ATH79_CLK_CPU] = ath79_add_sys_clkdev("cpu", cpu_rate);
119 clks[ATH79_CLK_DDR] = ath79_add_sys_clkdev("ddr", ddr_rate);
120 clks[ATH79_CLK_AHB] = 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
Gabor Juhos04225e12011-06-20 21:26:04 +0200126static void __init ar933x_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 Juhos04225e12011-06-20 21:26:04 +0200132 u32 clock_ctrl;
133 u32 cpu_config;
134 u32 freq;
135 u32 t;
136
137 t = ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP);
138 if (t & AR933X_BOOTSTRAP_REF_CLK_40)
Gabor Juhos6612a682013-08-28 10:41:46 +0200139 ref_rate = (40 * 1000 * 1000);
Gabor Juhos04225e12011-06-20 21:26:04 +0200140 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200141 ref_rate = (25 * 1000 * 1000);
Gabor Juhos04225e12011-06-20 21:26:04 +0200142
143 clock_ctrl = ath79_pll_rr(AR933X_PLL_CLOCK_CTRL_REG);
144 if (clock_ctrl & AR933X_PLL_CLOCK_CTRL_BYPASS) {
Gabor Juhos6612a682013-08-28 10:41:46 +0200145 cpu_rate = ref_rate;
146 ahb_rate = ref_rate;
147 ddr_rate = ref_rate;
Gabor Juhos04225e12011-06-20 21:26:04 +0200148 } else {
149 cpu_config = ath79_pll_rr(AR933X_PLL_CPU_CONFIG_REG);
150
151 t = (cpu_config >> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
152 AR933X_PLL_CPU_CONFIG_REFDIV_MASK;
Gabor Juhos6612a682013-08-28 10:41:46 +0200153 freq = ref_rate / t;
Gabor Juhos04225e12011-06-20 21:26:04 +0200154
155 t = (cpu_config >> AR933X_PLL_CPU_CONFIG_NINT_SHIFT) &
156 AR933X_PLL_CPU_CONFIG_NINT_MASK;
157 freq *= t;
158
159 t = (cpu_config >> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
160 AR933X_PLL_CPU_CONFIG_OUTDIV_MASK;
161 if (t == 0)
162 t = 1;
163
164 freq >>= t;
165
166 t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT) &
167 AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +0200168 cpu_rate = freq / t;
Gabor Juhos04225e12011-06-20 21:26:04 +0200169
170 t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT) &
171 AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +0200172 ddr_rate = freq / t;
Gabor Juhos04225e12011-06-20 21:26:04 +0200173
174 t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT) &
175 AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK) + 1;
Gabor Juhos6612a682013-08-28 10:41:46 +0200176 ahb_rate = freq / t;
Gabor Juhos04225e12011-06-20 21:26:04 +0200177 }
178
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200179 ath79_add_sys_clkdev("ref", ref_rate);
Antony Pavlovaf5ad0d2016-03-17 06:34:14 +0300180 clks[ATH79_CLK_CPU] = ath79_add_sys_clkdev("cpu", cpu_rate);
181 clks[ATH79_CLK_DDR] = ath79_add_sys_clkdev("ddr", ddr_rate);
182 clks[ATH79_CLK_AHB] = ath79_add_sys_clkdev("ahb", ahb_rate);
Gabor Juhos6612a682013-08-28 10:41:46 +0200183
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200184 clk_add_alias("wdt", NULL, "ahb", NULL);
185 clk_add_alias("uart", NULL, "ref", NULL);
Gabor Juhos04225e12011-06-20 21:26:04 +0200186}
187
Gabor Juhos97541cc2012-09-08 14:02:21 +0200188static u32 __init ar934x_get_pll_freq(u32 ref, u32 ref_div, u32 nint, u32 nfrac,
189 u32 frac, u32 out_div)
190{
191 u64 t;
192 u32 ret;
193
Gabor Juhos837f0362013-08-28 10:41:43 +0200194 t = ref;
Gabor Juhos97541cc2012-09-08 14:02:21 +0200195 t *= nint;
196 do_div(t, ref_div);
197 ret = t;
198
Gabor Juhos837f0362013-08-28 10:41:43 +0200199 t = ref;
Gabor Juhos97541cc2012-09-08 14:02:21 +0200200 t *= nfrac;
201 do_div(t, ref_div * frac);
202 ret += t;
203
204 ret /= (1 << out_div);
205 return ret;
206}
207
Gabor Juhos88896122012-03-14 10:45:22 +0100208static void __init ar934x_clocks_init(void)
209{
Gabor Juhos6612a682013-08-28 10:41:46 +0200210 unsigned long ref_rate;
211 unsigned long cpu_rate;
212 unsigned long ddr_rate;
213 unsigned long ahb_rate;
Gabor Juhos97541cc2012-09-08 14:02:21 +0200214 u32 pll, out_div, ref_div, nint, nfrac, frac, clk_ctrl, postdiv;
Gabor Juhos88896122012-03-14 10:45:22 +0100215 u32 cpu_pll, ddr_pll;
216 u32 bootstrap;
Gabor Juhos97541cc2012-09-08 14:02:21 +0200217 void __iomem *dpll_base;
218
219 dpll_base = ioremap(AR934X_SRIF_BASE, AR934X_SRIF_SIZE);
Gabor Juhos88896122012-03-14 10:45:22 +0100220
221 bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP);
Ralf Baechle70342282013-01-22 12:59:30 +0100222 if (bootstrap & AR934X_BOOTSTRAP_REF_CLK_40)
Gabor Juhos6612a682013-08-28 10:41:46 +0200223 ref_rate = 40 * 1000 * 1000;
Gabor Juhos88896122012-03-14 10:45:22 +0100224 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200225 ref_rate = 25 * 1000 * 1000;
Gabor Juhos88896122012-03-14 10:45:22 +0100226
Gabor Juhos97541cc2012-09-08 14:02:21 +0200227 pll = __raw_readl(dpll_base + AR934X_SRIF_CPU_DPLL2_REG);
228 if (pll & AR934X_SRIF_DPLL2_LOCAL_PLL) {
229 out_div = (pll >> AR934X_SRIF_DPLL2_OUTDIV_SHIFT) &
230 AR934X_SRIF_DPLL2_OUTDIV_MASK;
231 pll = __raw_readl(dpll_base + AR934X_SRIF_CPU_DPLL1_REG);
232 nint = (pll >> AR934X_SRIF_DPLL1_NINT_SHIFT) &
233 AR934X_SRIF_DPLL1_NINT_MASK;
234 nfrac = pll & AR934X_SRIF_DPLL1_NFRAC_MASK;
235 ref_div = (pll >> AR934X_SRIF_DPLL1_REFDIV_SHIFT) &
236 AR934X_SRIF_DPLL1_REFDIV_MASK;
237 frac = 1 << 18;
238 } else {
239 pll = ath79_pll_rr(AR934X_PLL_CPU_CONFIG_REG);
240 out_div = (pll >> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
241 AR934X_PLL_CPU_CONFIG_OUTDIV_MASK;
242 ref_div = (pll >> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
243 AR934X_PLL_CPU_CONFIG_REFDIV_MASK;
244 nint = (pll >> AR934X_PLL_CPU_CONFIG_NINT_SHIFT) &
245 AR934X_PLL_CPU_CONFIG_NINT_MASK;
246 nfrac = (pll >> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT) &
247 AR934X_PLL_CPU_CONFIG_NFRAC_MASK;
248 frac = 1 << 6;
249 }
Gabor Juhos88896122012-03-14 10:45:22 +0100250
Gabor Juhos6612a682013-08-28 10:41:46 +0200251 cpu_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint,
Gabor Juhos97541cc2012-09-08 14:02:21 +0200252 nfrac, frac, out_div);
Gabor Juhos88896122012-03-14 10:45:22 +0100253
Gabor Juhos97541cc2012-09-08 14:02:21 +0200254 pll = __raw_readl(dpll_base + AR934X_SRIF_DDR_DPLL2_REG);
255 if (pll & AR934X_SRIF_DPLL2_LOCAL_PLL) {
256 out_div = (pll >> AR934X_SRIF_DPLL2_OUTDIV_SHIFT) &
257 AR934X_SRIF_DPLL2_OUTDIV_MASK;
258 pll = __raw_readl(dpll_base + AR934X_SRIF_DDR_DPLL1_REG);
259 nint = (pll >> AR934X_SRIF_DPLL1_NINT_SHIFT) &
260 AR934X_SRIF_DPLL1_NINT_MASK;
261 nfrac = pll & AR934X_SRIF_DPLL1_NFRAC_MASK;
262 ref_div = (pll >> AR934X_SRIF_DPLL1_REFDIV_SHIFT) &
263 AR934X_SRIF_DPLL1_REFDIV_MASK;
264 frac = 1 << 18;
265 } else {
266 pll = ath79_pll_rr(AR934X_PLL_DDR_CONFIG_REG);
267 out_div = (pll >> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT) &
268 AR934X_PLL_DDR_CONFIG_OUTDIV_MASK;
269 ref_div = (pll >> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT) &
270 AR934X_PLL_DDR_CONFIG_REFDIV_MASK;
271 nint = (pll >> AR934X_PLL_DDR_CONFIG_NINT_SHIFT) &
272 AR934X_PLL_DDR_CONFIG_NINT_MASK;
273 nfrac = (pll >> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT) &
274 AR934X_PLL_DDR_CONFIG_NFRAC_MASK;
275 frac = 1 << 10;
276 }
Gabor Juhos88896122012-03-14 10:45:22 +0100277
Gabor Juhos6612a682013-08-28 10:41:46 +0200278 ddr_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint,
Gabor Juhos97541cc2012-09-08 14:02:21 +0200279 nfrac, frac, out_div);
Gabor Juhos88896122012-03-14 10:45:22 +0100280
281 clk_ctrl = ath79_pll_rr(AR934X_PLL_CPU_DDR_CLK_CTRL_REG);
282
283 postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_SHIFT) &
284 AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_MASK;
285
286 if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200287 cpu_rate = ref_rate;
Gabor Juhos88896122012-03-14 10:45:22 +0100288 else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_CPUCLK_FROM_CPUPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200289 cpu_rate = cpu_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100290 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200291 cpu_rate = ddr_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100292
293 postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_SHIFT) &
294 AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_MASK;
295
296 if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200297 ddr_rate = ref_rate;
Gabor Juhos88896122012-03-14 10:45:22 +0100298 else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_DDRCLK_FROM_DDRPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200299 ddr_rate = ddr_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100300 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200301 ddr_rate = cpu_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100302
303 postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_SHIFT) &
304 AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_MASK;
305
306 if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200307 ahb_rate = ref_rate;
Gabor Juhos88896122012-03-14 10:45:22 +0100308 else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_AHBCLK_FROM_DDRPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200309 ahb_rate = ddr_pll / (postdiv + 1);
Gabor Juhos88896122012-03-14 10:45:22 +0100310 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200311 ahb_rate = cpu_pll / (postdiv + 1);
312
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200313 ath79_add_sys_clkdev("ref", ref_rate);
Antony Pavlovaf5ad0d2016-03-17 06:34:14 +0300314 clks[ATH79_CLK_CPU] = ath79_add_sys_clkdev("cpu", cpu_rate);
315 clks[ATH79_CLK_DDR] = ath79_add_sys_clkdev("ddr", ddr_rate);
316 clks[ATH79_CLK_AHB] = ath79_add_sys_clkdev("ahb", ahb_rate);
Gabor Juhos88896122012-03-14 10:45:22 +0100317
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200318 clk_add_alias("wdt", NULL, "ref", NULL);
319 clk_add_alias("uart", NULL, "ref", NULL);
Gabor Juhos97541cc2012-09-08 14:02:21 +0200320
321 iounmap(dpll_base);
Gabor Juhos88896122012-03-14 10:45:22 +0100322}
323
Gabor Juhos41583c02013-02-15 13:38:17 +0000324static void __init qca955x_clocks_init(void)
325{
Gabor Juhos6612a682013-08-28 10:41:46 +0200326 unsigned long ref_rate;
327 unsigned long cpu_rate;
328 unsigned long ddr_rate;
329 unsigned long ahb_rate;
Gabor Juhos41583c02013-02-15 13:38:17 +0000330 u32 pll, out_div, ref_div, nint, frac, clk_ctrl, postdiv;
331 u32 cpu_pll, ddr_pll;
332 u32 bootstrap;
333
334 bootstrap = ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP);
335 if (bootstrap & QCA955X_BOOTSTRAP_REF_CLK_40)
Gabor Juhos6612a682013-08-28 10:41:46 +0200336 ref_rate = 40 * 1000 * 1000;
Gabor Juhos41583c02013-02-15 13:38:17 +0000337 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200338 ref_rate = 25 * 1000 * 1000;
Gabor Juhos41583c02013-02-15 13:38:17 +0000339
340 pll = ath79_pll_rr(QCA955X_PLL_CPU_CONFIG_REG);
341 out_div = (pll >> QCA955X_PLL_CPU_CONFIG_OUTDIV_SHIFT) &
342 QCA955X_PLL_CPU_CONFIG_OUTDIV_MASK;
343 ref_div = (pll >> QCA955X_PLL_CPU_CONFIG_REFDIV_SHIFT) &
344 QCA955X_PLL_CPU_CONFIG_REFDIV_MASK;
345 nint = (pll >> QCA955X_PLL_CPU_CONFIG_NINT_SHIFT) &
346 QCA955X_PLL_CPU_CONFIG_NINT_MASK;
347 frac = (pll >> QCA955X_PLL_CPU_CONFIG_NFRAC_SHIFT) &
348 QCA955X_PLL_CPU_CONFIG_NFRAC_MASK;
349
Gabor Juhos6612a682013-08-28 10:41:46 +0200350 cpu_pll = nint * ref_rate / ref_div;
351 cpu_pll += frac * ref_rate / (ref_div * (1 << 6));
Gabor Juhos41583c02013-02-15 13:38:17 +0000352 cpu_pll /= (1 << out_div);
353
354 pll = ath79_pll_rr(QCA955X_PLL_DDR_CONFIG_REG);
355 out_div = (pll >> QCA955X_PLL_DDR_CONFIG_OUTDIV_SHIFT) &
356 QCA955X_PLL_DDR_CONFIG_OUTDIV_MASK;
357 ref_div = (pll >> QCA955X_PLL_DDR_CONFIG_REFDIV_SHIFT) &
358 QCA955X_PLL_DDR_CONFIG_REFDIV_MASK;
359 nint = (pll >> QCA955X_PLL_DDR_CONFIG_NINT_SHIFT) &
360 QCA955X_PLL_DDR_CONFIG_NINT_MASK;
361 frac = (pll >> QCA955X_PLL_DDR_CONFIG_NFRAC_SHIFT) &
362 QCA955X_PLL_DDR_CONFIG_NFRAC_MASK;
363
Gabor Juhos6612a682013-08-28 10:41:46 +0200364 ddr_pll = nint * ref_rate / ref_div;
365 ddr_pll += frac * ref_rate / (ref_div * (1 << 10));
Gabor Juhos41583c02013-02-15 13:38:17 +0000366 ddr_pll /= (1 << out_div);
367
368 clk_ctrl = ath79_pll_rr(QCA955X_PLL_CLK_CTRL_REG);
369
370 postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) &
371 QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK;
372
373 if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200374 cpu_rate = ref_rate;
Gabor Juhos41583c02013-02-15 13:38:17 +0000375 else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200376 cpu_rate = ddr_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000377 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200378 cpu_rate = cpu_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000379
380 postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) &
381 QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK;
382
383 if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200384 ddr_rate = ref_rate;
Gabor Juhos41583c02013-02-15 13:38:17 +0000385 else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200386 ddr_rate = cpu_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000387 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200388 ddr_rate = ddr_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000389
390 postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) &
391 QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK;
392
393 if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS)
Gabor Juhos6612a682013-08-28 10:41:46 +0200394 ahb_rate = ref_rate;
Gabor Juhos41583c02013-02-15 13:38:17 +0000395 else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL)
Gabor Juhos6612a682013-08-28 10:41:46 +0200396 ahb_rate = ddr_pll / (postdiv + 1);
Gabor Juhos41583c02013-02-15 13:38:17 +0000397 else
Gabor Juhos6612a682013-08-28 10:41:46 +0200398 ahb_rate = cpu_pll / (postdiv + 1);
399
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200400 ath79_add_sys_clkdev("ref", ref_rate);
Antony Pavlovaf5ad0d2016-03-17 06:34:14 +0300401 clks[ATH79_CLK_CPU] = ath79_add_sys_clkdev("cpu", cpu_rate);
402 clks[ATH79_CLK_DDR] = ath79_add_sys_clkdev("ddr", ddr_rate);
403 clks[ATH79_CLK_AHB] = ath79_add_sys_clkdev("ahb", ahb_rate);
Gabor Juhos41583c02013-02-15 13:38:17 +0000404
Gabor Juhos2c4f1ac2013-08-28 10:41:47 +0200405 clk_add_alias("wdt", NULL, "ref", NULL);
406 clk_add_alias("uart", NULL, "ref", NULL);
Gabor Juhos41583c02013-02-15 13:38:17 +0000407}
408
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100409void __init ath79_clocks_init(void)
410{
411 if (soc_is_ar71xx())
412 ar71xx_clocks_init();
Alban Bedelf4c87b72016-03-17 06:34:10 +0300413 else if (soc_is_ar724x() || soc_is_ar913x())
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100414 ar724x_clocks_init();
Gabor Juhos04225e12011-06-20 21:26:04 +0200415 else if (soc_is_ar933x())
416 ar933x_clocks_init();
Gabor Juhos88896122012-03-14 10:45:22 +0100417 else if (soc_is_ar934x())
418 ar934x_clocks_init();
Gabor Juhos41583c02013-02-15 13:38:17 +0000419 else if (soc_is_qca955x())
420 qca955x_clocks_init();
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100421 else
422 BUG();
Alban Bedel6451af02015-05-31 02:18:22 +0200423
424 of_clk_init(NULL);
Gabor Juhosd4a67d92011-01-04 21:28:14 +0100425}
426
Gabor Juhos23107802013-08-28 10:41:44 +0200427unsigned long __init
428ath79_get_sys_clk_rate(const char *id)
429{
430 struct clk *clk;
431 unsigned long rate;
432
433 clk = clk_get(NULL, id);
434 if (IS_ERR(clk))
435 panic("unable to get %s clock, err=%d", id, (int) PTR_ERR(clk));
436
437 rate = clk_get_rate(clk);
438 clk_put(clk);
439
440 return rate;
441}
Alban Bedel6451af02015-05-31 02:18:22 +0200442
443#ifdef CONFIG_OF
444static void __init ath79_clocks_init_dt(struct device_node *np)
445{
446 of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data);
447}
448
449CLK_OF_DECLARE(ar7100, "qca,ar7100-pll", ath79_clocks_init_dt);
450CLK_OF_DECLARE(ar7240, "qca,ar7240-pll", ath79_clocks_init_dt);
451CLK_OF_DECLARE(ar9130, "qca,ar9130-pll", ath79_clocks_init_dt);
452CLK_OF_DECLARE(ar9330, "qca,ar9330-pll", ath79_clocks_init_dt);
453CLK_OF_DECLARE(ar9340, "qca,ar9340-pll", ath79_clocks_init_dt);
454CLK_OF_DECLARE(ar9550, "qca,qca9550-pll", ath79_clocks_init_dt);
455#endif