Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Atheros AR71XX/AR724X/AR913X common routines |
| 3 | * |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 4 | * Copyright (C) 2010-2011 Jaiganesh Narayanan <jnarayanan@atheros.com> |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 5 | * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org> |
| 6 | * |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 7 | * Parts of this file are based on Atheros' 2.6.15/2.6.31 BSP |
| 8 | * |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 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. |
| 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 Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 19 | #include <linux/clkdev.h> |
Alban Bedel | 411520a | 2015-04-19 14:30:04 +0200 | [diff] [blame] | 20 | #include <linux/clk-provider.h> |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 21 | #include <linux/of.h> |
| 22 | #include <linux/of_address.h> |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame] | 23 | #include <dt-bindings/clock/ath79-clk.h> |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 24 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 25 | #include <asm/div64.h> |
| 26 | |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 27 | #include <asm/mach-ath79/ath79.h> |
| 28 | #include <asm/mach-ath79/ar71xx_regs.h> |
| 29 | #include "common.h" |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 30 | #include "machtypes.h" |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 31 | |
| 32 | #define AR71XX_BASE_FREQ 40000000 |
Weijie Gao | c338d59 | 2016-03-17 06:34:09 +0300 | [diff] [blame] | 33 | #define AR724X_BASE_FREQ 40000000 |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 34 | |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame] | 35 | static struct clk *clks[ATH79_CLK_END]; |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 36 | static struct clk_onecell_data clk_data = { |
| 37 | .clks = clks, |
| 38 | .clk_num = ARRAY_SIZE(clks), |
| 39 | }; |
| 40 | |
| 41 | static struct clk *__init ath79_add_sys_clkdev( |
| 42 | const char *id, unsigned long rate) |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 43 | { |
| 44 | struct clk *clk; |
| 45 | int err; |
| 46 | |
Stephen Boyd | 9c938a0 | 2016-04-19 18:33:21 -0700 | [diff] [blame] | 47 | clk = clk_register_fixed_rate(NULL, id, NULL, 0, rate); |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 48 | if (!clk) |
| 49 | panic("failed to allocate %s clock structure", id); |
| 50 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 51 | err = clk_register_clkdev(clk, id, NULL); |
| 52 | if (err) |
| 53 | panic("unable to register %s clock device", id); |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 54 | |
| 55 | return clk; |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 56 | } |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 57 | |
| 58 | static void __init ar71xx_clocks_init(void) |
| 59 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 60 | unsigned long ref_rate; |
| 61 | unsigned long cpu_rate; |
| 62 | unsigned long ddr_rate; |
| 63 | unsigned long ahb_rate; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 64 | u32 pll; |
| 65 | u32 freq; |
| 66 | u32 div; |
| 67 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 68 | ref_rate = AR71XX_BASE_FREQ; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 69 | |
| 70 | pll = ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG); |
| 71 | |
Alban Bedel | 626a069 | 2015-04-19 14:30:02 +0200 | [diff] [blame] | 72 | div = ((pll >> AR71XX_PLL_FB_SHIFT) & AR71XX_PLL_FB_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 73 | freq = div * ref_rate; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 74 | |
| 75 | div = ((pll >> AR71XX_CPU_DIV_SHIFT) & AR71XX_CPU_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 76 | cpu_rate = freq / div; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 77 | |
| 78 | div = ((pll >> AR71XX_DDR_DIV_SHIFT) & AR71XX_DDR_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 79 | ddr_rate = freq / div; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 80 | |
| 81 | div = (((pll >> AR71XX_AHB_DIV_SHIFT) & AR71XX_AHB_DIV_MASK) + 1) * 2; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 82 | ahb_rate = cpu_rate / div; |
| 83 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 84 | ath79_add_sys_clkdev("ref", ref_rate); |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame] | 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); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 88 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 89 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 90 | clk_add_alias("uart", NULL, "ahb", NULL); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 93 | static struct clk * __init ath79_reg_ffclk(const char *name, |
| 94 | const char *parent_name, unsigned int mult, unsigned int div) |
| 95 | { |
| 96 | struct clk *clk; |
| 97 | |
| 98 | clk = clk_register_fixed_factor(NULL, name, parent_name, 0, mult, div); |
Amitoj Kaur Chawla | e3b2314 | 2016-08-12 08:36:54 +0530 | [diff] [blame] | 99 | if (IS_ERR(clk)) |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 100 | panic("failed to allocate %s clock structure", name); |
| 101 | |
| 102 | return clk; |
| 103 | } |
| 104 | |
| 105 | static void __init ar724x_clk_init(struct clk *ref_clk, void __iomem *pll_base) |
| 106 | { |
| 107 | u32 pll; |
| 108 | u32 mult, div, ddr_div, ahb_div; |
| 109 | |
| 110 | pll = __raw_readl(pll_base + AR724X_PLL_REG_CPU_CONFIG); |
| 111 | |
| 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; |
| 114 | |
| 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; |
| 117 | |
| 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); |
| 121 | } |
| 122 | |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 123 | static void __init ar724x_clocks_init(void) |
| 124 | { |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 125 | struct clk *ref_clk; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 126 | |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 127 | ref_clk = ath79_add_sys_clkdev("ref", AR724X_BASE_FREQ); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 128 | |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 129 | ar724x_clk_init(ref_clk, ath79_pll_base); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 130 | |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 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); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 135 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 136 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 137 | clk_add_alias("uart", NULL, "ahb", NULL); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 140 | static void __init ar9330_clk_init(struct clk *ref_clk, void __iomem *pll_base) |
| 141 | { |
| 142 | u32 clock_ctrl; |
| 143 | u32 ref_div; |
| 144 | u32 ninit_mul; |
| 145 | u32 out_div; |
| 146 | |
| 147 | u32 cpu_div; |
| 148 | u32 ddr_div; |
| 149 | u32 ahb_div; |
| 150 | |
| 151 | clock_ctrl = __raw_readl(pll_base + AR933X_PLL_CLOCK_CTRL_REG); |
| 152 | if (clock_ctrl & AR933X_PLL_CLOCK_CTRL_BYPASS) { |
| 153 | ref_div = 1; |
| 154 | ninit_mul = 1; |
| 155 | out_div = 1; |
| 156 | |
| 157 | cpu_div = 1; |
| 158 | ddr_div = 1; |
| 159 | ahb_div = 1; |
| 160 | } else { |
| 161 | u32 cpu_config; |
| 162 | u32 t; |
| 163 | |
| 164 | cpu_config = __raw_readl(pll_base + AR933X_PLL_CPU_CONFIG_REG); |
| 165 | |
| 166 | t = (cpu_config >> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT) & |
| 167 | AR933X_PLL_CPU_CONFIG_REFDIV_MASK; |
| 168 | ref_div = t; |
| 169 | |
| 170 | ninit_mul = (cpu_config >> AR933X_PLL_CPU_CONFIG_NINT_SHIFT) & |
| 171 | AR933X_PLL_CPU_CONFIG_NINT_MASK; |
| 172 | |
| 173 | t = (cpu_config >> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT) & |
| 174 | AR933X_PLL_CPU_CONFIG_OUTDIV_MASK; |
| 175 | if (t == 0) |
| 176 | t = 1; |
| 177 | |
| 178 | out_div = (1 << t); |
| 179 | |
| 180 | cpu_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT) & |
| 181 | AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK) + 1; |
| 182 | |
| 183 | ddr_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT) & |
| 184 | AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK) + 1; |
| 185 | |
| 186 | ahb_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT) & |
| 187 | AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK) + 1; |
| 188 | } |
| 189 | |
| 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); |
| 196 | } |
| 197 | |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 198 | static void __init ar933x_clocks_init(void) |
| 199 | { |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 200 | struct clk *ref_clk; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 201 | unsigned long ref_rate; |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 202 | u32 t; |
| 203 | |
| 204 | t = ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP); |
| 205 | if (t & AR933X_BOOTSTRAP_REF_CLK_40) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 206 | ref_rate = (40 * 1000 * 1000); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 207 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 208 | ref_rate = (25 * 1000 * 1000); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 209 | |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 210 | ref_clk = ath79_add_sys_clkdev("ref", ref_rate); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 211 | |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 212 | ar9330_clk_init(ref_clk, ath79_pll_base); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 213 | |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 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); |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 218 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 219 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 220 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 223 | static u32 __init ar934x_get_pll_freq(u32 ref, u32 ref_div, u32 nint, u32 nfrac, |
| 224 | u32 frac, u32 out_div) |
| 225 | { |
| 226 | u64 t; |
| 227 | u32 ret; |
| 228 | |
Gabor Juhos | 837f036 | 2013-08-28 10:41:43 +0200 | [diff] [blame] | 229 | t = ref; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 230 | t *= nint; |
| 231 | do_div(t, ref_div); |
| 232 | ret = t; |
| 233 | |
Gabor Juhos | 837f036 | 2013-08-28 10:41:43 +0200 | [diff] [blame] | 234 | t = ref; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 235 | t *= nfrac; |
| 236 | do_div(t, ref_div * frac); |
| 237 | ret += t; |
| 238 | |
| 239 | ret /= (1 << out_div); |
| 240 | return ret; |
| 241 | } |
| 242 | |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 243 | static void __init ar934x_clocks_init(void) |
| 244 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 245 | unsigned long ref_rate; |
| 246 | unsigned long cpu_rate; |
| 247 | unsigned long ddr_rate; |
| 248 | unsigned long ahb_rate; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 249 | u32 pll, out_div, ref_div, nint, nfrac, frac, clk_ctrl, postdiv; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 250 | u32 cpu_pll, ddr_pll; |
| 251 | u32 bootstrap; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 252 | void __iomem *dpll_base; |
| 253 | |
| 254 | dpll_base = ioremap(AR934X_SRIF_BASE, AR934X_SRIF_SIZE); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 255 | |
| 256 | bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP); |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 257 | if (bootstrap & AR934X_BOOTSTRAP_REF_CLK_40) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 258 | ref_rate = 40 * 1000 * 1000; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 259 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 260 | ref_rate = 25 * 1000 * 1000; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 261 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 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; |
| 272 | frac = 1 << 18; |
| 273 | } else { |
| 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; |
| 283 | frac = 1 << 6; |
| 284 | } |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 285 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 286 | cpu_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint, |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 287 | nfrac, frac, out_div); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 288 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 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; |
| 299 | frac = 1 << 18; |
| 300 | } else { |
| 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; |
| 310 | frac = 1 << 10; |
| 311 | } |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 312 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 313 | ddr_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint, |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 314 | nfrac, frac, out_div); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 315 | |
| 316 | clk_ctrl = ath79_pll_rr(AR934X_PLL_CPU_DDR_CLK_CTRL_REG); |
| 317 | |
| 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; |
| 320 | |
| 321 | if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 322 | cpu_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 323 | else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_CPUCLK_FROM_CPUPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 324 | cpu_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 325 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 326 | cpu_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 327 | |
| 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; |
| 330 | |
| 331 | if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 332 | ddr_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 333 | else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_DDRCLK_FROM_DDRPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 334 | ddr_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 335 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 336 | ddr_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 337 | |
| 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; |
| 340 | |
| 341 | if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 342 | ahb_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 343 | else if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_AHBCLK_FROM_DDRPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 344 | ahb_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 345 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 346 | ahb_rate = cpu_pll / (postdiv + 1); |
| 347 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 348 | ath79_add_sys_clkdev("ref", ref_rate); |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame] | 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); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 352 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 353 | clk_add_alias("wdt", NULL, "ref", NULL); |
| 354 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 355 | |
| 356 | iounmap(dpll_base); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 359 | static void __init qca955x_clocks_init(void) |
| 360 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 361 | unsigned long ref_rate; |
| 362 | unsigned long cpu_rate; |
| 363 | unsigned long ddr_rate; |
| 364 | unsigned long ahb_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 365 | u32 pll, out_div, ref_div, nint, frac, clk_ctrl, postdiv; |
| 366 | u32 cpu_pll, ddr_pll; |
| 367 | u32 bootstrap; |
| 368 | |
| 369 | bootstrap = ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP); |
| 370 | if (bootstrap & QCA955X_BOOTSTRAP_REF_CLK_40) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 371 | ref_rate = 40 * 1000 * 1000; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 372 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 373 | ref_rate = 25 * 1000 * 1000; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 374 | |
| 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; |
| 384 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 385 | cpu_pll = nint * ref_rate / ref_div; |
| 386 | cpu_pll += frac * ref_rate / (ref_div * (1 << 6)); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 387 | cpu_pll /= (1 << out_div); |
| 388 | |
| 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; |
| 398 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 399 | ddr_pll = nint * ref_rate / ref_div; |
| 400 | ddr_pll += frac * ref_rate / (ref_div * (1 << 10)); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 401 | ddr_pll /= (1 << out_div); |
| 402 | |
| 403 | clk_ctrl = ath79_pll_rr(QCA955X_PLL_CLK_CTRL_REG); |
| 404 | |
| 405 | postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) & |
| 406 | QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK; |
| 407 | |
| 408 | if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 409 | cpu_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 410 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 411 | cpu_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 412 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 413 | cpu_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 414 | |
| 415 | postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) & |
| 416 | QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK; |
| 417 | |
| 418 | if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 419 | ddr_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 420 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 421 | ddr_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 422 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 423 | ddr_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 424 | |
| 425 | postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) & |
| 426 | QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK; |
| 427 | |
| 428 | if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 429 | ahb_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 430 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 431 | ahb_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 432 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 433 | ahb_rate = cpu_pll / (postdiv + 1); |
| 434 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 435 | ath79_add_sys_clkdev("ref", ref_rate); |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame] | 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); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 439 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 440 | clk_add_alias("wdt", NULL, "ref", NULL); |
| 441 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 444 | void __init ath79_clocks_init(void) |
| 445 | { |
| 446 | if (soc_is_ar71xx()) |
| 447 | ar71xx_clocks_init(); |
Alban Bedel | f4c87b7 | 2016-03-17 06:34:10 +0300 | [diff] [blame] | 448 | else if (soc_is_ar724x() || soc_is_ar913x()) |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 449 | ar724x_clocks_init(); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 450 | else if (soc_is_ar933x()) |
| 451 | ar933x_clocks_init(); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 452 | else if (soc_is_ar934x()) |
| 453 | ar934x_clocks_init(); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 454 | else if (soc_is_qca955x()) |
| 455 | qca955x_clocks_init(); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 456 | else |
| 457 | BUG(); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Gabor Juhos | 2310780 | 2013-08-28 10:41:44 +0200 | [diff] [blame] | 460 | unsigned long __init |
| 461 | ath79_get_sys_clk_rate(const char *id) |
| 462 | { |
| 463 | struct clk *clk; |
| 464 | unsigned long rate; |
| 465 | |
| 466 | clk = clk_get(NULL, id); |
| 467 | if (IS_ERR(clk)) |
| 468 | panic("unable to get %s clock, err=%d", id, (int) PTR_ERR(clk)); |
| 469 | |
| 470 | rate = clk_get_rate(clk); |
| 471 | clk_put(clk); |
| 472 | |
| 473 | return rate; |
| 474 | } |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 475 | |
| 476 | #ifdef CONFIG_OF |
| 477 | static void __init ath79_clocks_init_dt(struct device_node *np) |
| 478 | { |
| 479 | of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data); |
| 480 | } |
| 481 | |
| 482 | CLK_OF_DECLARE(ar7100, "qca,ar7100-pll", ath79_clocks_init_dt); |
| 483 | CLK_OF_DECLARE(ar7240, "qca,ar7240-pll", ath79_clocks_init_dt); |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 484 | CLK_OF_DECLARE(ar9340, "qca,ar9340-pll", ath79_clocks_init_dt); |
| 485 | CLK_OF_DECLARE(ar9550, "qca,qca9550-pll", ath79_clocks_init_dt); |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 486 | |
| 487 | static void __init ath79_clocks_init_dt_ng(struct device_node *np) |
| 488 | { |
| 489 | struct clk *ref_clk; |
| 490 | void __iomem *pll_base; |
| 491 | const char *dnfn = of_node_full_name(np); |
| 492 | |
| 493 | ref_clk = of_clk_get(np, 0); |
| 494 | if (IS_ERR(ref_clk)) { |
| 495 | pr_err("%s: of_clk_get failed\n", dnfn); |
| 496 | goto err; |
| 497 | } |
| 498 | |
| 499 | pll_base = of_iomap(np, 0); |
| 500 | if (!pll_base) { |
| 501 | pr_err("%s: can't map pll registers\n", dnfn); |
| 502 | goto err_clk; |
| 503 | } |
| 504 | |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 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); |
| 509 | else { |
| 510 | pr_err("%s: could not find any appropriate clk_init()\n", dnfn); |
| 511 | goto err_clk; |
| 512 | } |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 513 | |
| 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); |
| 516 | goto err_clk; |
| 517 | } |
| 518 | |
| 519 | return; |
| 520 | |
| 521 | err_clk: |
| 522 | clk_put(ref_clk); |
| 523 | |
| 524 | err: |
| 525 | return; |
| 526 | } |
| 527 | CLK_OF_DECLARE(ar9130_clk, "qca,ar9130-pll", ath79_clocks_init_dt_ng); |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 528 | CLK_OF_DECLARE(ar9330_clk, "qca,ar9330-pll", ath79_clocks_init_dt_ng); |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 529 | #endif |