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 | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame^] | 21 | #include <dt-bindings/clock/ath79-clk.h> |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 22 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 23 | #include <asm/div64.h> |
| 24 | |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 25 | #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 Gao | c338d59 | 2016-03-17 06:34:09 +0300 | [diff] [blame] | 30 | #define AR724X_BASE_FREQ 40000000 |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 31 | |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame^] | 32 | static struct clk *clks[ATH79_CLK_END]; |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 33 | static struct clk_onecell_data clk_data = { |
| 34 | .clks = clks, |
| 35 | .clk_num = ARRAY_SIZE(clks), |
| 36 | }; |
| 37 | |
| 38 | static struct clk *__init ath79_add_sys_clkdev( |
| 39 | const char *id, unsigned long rate) |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 40 | { |
| 41 | struct clk *clk; |
| 42 | int err; |
| 43 | |
Alban Bedel | 411520a | 2015-04-19 14:30:04 +0200 | [diff] [blame] | 44 | clk = clk_register_fixed_rate(NULL, id, NULL, CLK_IS_ROOT, rate); |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 45 | if (!clk) |
| 46 | panic("failed to allocate %s clock structure", id); |
| 47 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 48 | err = clk_register_clkdev(clk, id, NULL); |
| 49 | if (err) |
| 50 | panic("unable to register %s clock device", id); |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 51 | |
| 52 | return clk; |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 53 | } |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 54 | |
| 55 | static void __init ar71xx_clocks_init(void) |
| 56 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 57 | unsigned long ref_rate; |
| 58 | unsigned long cpu_rate; |
| 59 | unsigned long ddr_rate; |
| 60 | unsigned long ahb_rate; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 61 | u32 pll; |
| 62 | u32 freq; |
| 63 | u32 div; |
| 64 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 65 | ref_rate = AR71XX_BASE_FREQ; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 66 | |
| 67 | pll = ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG); |
| 68 | |
Alban Bedel | 626a069 | 2015-04-19 14:30:02 +0200 | [diff] [blame] | 69 | div = ((pll >> AR71XX_PLL_FB_SHIFT) & AR71XX_PLL_FB_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 70 | freq = div * ref_rate; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 71 | |
| 72 | div = ((pll >> AR71XX_CPU_DIV_SHIFT) & AR71XX_CPU_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 73 | cpu_rate = freq / div; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 74 | |
| 75 | div = ((pll >> AR71XX_DDR_DIV_SHIFT) & AR71XX_DDR_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 76 | ddr_rate = freq / div; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 77 | |
| 78 | div = (((pll >> AR71XX_AHB_DIV_SHIFT) & AR71XX_AHB_DIV_MASK) + 1) * 2; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 79 | ahb_rate = cpu_rate / div; |
| 80 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 81 | ath79_add_sys_clkdev("ref", ref_rate); |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame^] | 82 | 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 Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 85 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 86 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 87 | clk_add_alias("uart", NULL, "ahb", NULL); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void __init ar724x_clocks_init(void) |
| 91 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 92 | unsigned long ref_rate; |
| 93 | unsigned long cpu_rate; |
| 94 | unsigned long ddr_rate; |
| 95 | unsigned long ahb_rate; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 96 | u32 pll; |
| 97 | u32 freq; |
| 98 | u32 div; |
| 99 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 100 | ref_rate = AR724X_BASE_FREQ; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 101 | pll = ath79_pll_rr(AR724X_PLL_REG_CPU_CONFIG); |
| 102 | |
Alban Bedel | 626a069 | 2015-04-19 14:30:02 +0200 | [diff] [blame] | 103 | div = ((pll >> AR724X_PLL_FB_SHIFT) & AR724X_PLL_FB_MASK); |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 104 | freq = div * ref_rate; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 105 | |
Weijie Gao | c338d59 | 2016-03-17 06:34:09 +0300 | [diff] [blame] | 106 | div = ((pll >> AR724X_PLL_REF_DIV_SHIFT) & AR724X_PLL_REF_DIV_MASK) * 2; |
| 107 | freq /= div; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 108 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 109 | cpu_rate = freq; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 110 | |
| 111 | div = ((pll >> AR724X_DDR_DIV_SHIFT) & AR724X_DDR_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 112 | ddr_rate = freq / div; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 113 | |
| 114 | div = (((pll >> AR724X_AHB_DIV_SHIFT) & AR724X_AHB_DIV_MASK) + 1) * 2; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 115 | ahb_rate = cpu_rate / div; |
| 116 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 117 | ath79_add_sys_clkdev("ref", ref_rate); |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame^] | 118 | 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 Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 121 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 122 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 123 | clk_add_alias("uart", NULL, "ahb", NULL); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 126 | static void __init ar933x_clocks_init(void) |
| 127 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 128 | unsigned long ref_rate; |
| 129 | unsigned long cpu_rate; |
| 130 | unsigned long ddr_rate; |
| 131 | unsigned long ahb_rate; |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 132 | 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 139 | ref_rate = (40 * 1000 * 1000); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 140 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 141 | ref_rate = (25 * 1000 * 1000); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 142 | |
| 143 | clock_ctrl = ath79_pll_rr(AR933X_PLL_CLOCK_CTRL_REG); |
| 144 | if (clock_ctrl & AR933X_PLL_CLOCK_CTRL_BYPASS) { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 145 | cpu_rate = ref_rate; |
| 146 | ahb_rate = ref_rate; |
| 147 | ddr_rate = ref_rate; |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 148 | } 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 153 | freq = ref_rate / t; |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 154 | |
| 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 168 | cpu_rate = freq / t; |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 169 | |
| 170 | t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT) & |
| 171 | AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 172 | ddr_rate = freq / t; |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 173 | |
| 174 | t = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT) & |
| 175 | AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 176 | ahb_rate = freq / t; |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 179 | ath79_add_sys_clkdev("ref", ref_rate); |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame^] | 180 | 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 183 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 184 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 185 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 188 | static 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 Juhos | 837f036 | 2013-08-28 10:41:43 +0200 | [diff] [blame] | 194 | t = ref; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 195 | t *= nint; |
| 196 | do_div(t, ref_div); |
| 197 | ret = t; |
| 198 | |
Gabor Juhos | 837f036 | 2013-08-28 10:41:43 +0200 | [diff] [blame] | 199 | t = ref; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 200 | t *= nfrac; |
| 201 | do_div(t, ref_div * frac); |
| 202 | ret += t; |
| 203 | |
| 204 | ret /= (1 << out_div); |
| 205 | return ret; |
| 206 | } |
| 207 | |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 208 | static void __init ar934x_clocks_init(void) |
| 209 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 210 | unsigned long ref_rate; |
| 211 | unsigned long cpu_rate; |
| 212 | unsigned long ddr_rate; |
| 213 | unsigned long ahb_rate; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 214 | u32 pll, out_div, ref_div, nint, nfrac, frac, clk_ctrl, postdiv; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 215 | u32 cpu_pll, ddr_pll; |
| 216 | u32 bootstrap; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 217 | void __iomem *dpll_base; |
| 218 | |
| 219 | dpll_base = ioremap(AR934X_SRIF_BASE, AR934X_SRIF_SIZE); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 220 | |
| 221 | bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP); |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 222 | if (bootstrap & AR934X_BOOTSTRAP_REF_CLK_40) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 223 | ref_rate = 40 * 1000 * 1000; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 224 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 225 | ref_rate = 25 * 1000 * 1000; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 226 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 227 | 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 Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 250 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 251 | cpu_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint, |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 252 | nfrac, frac, out_div); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 253 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 254 | 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 Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 277 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 278 | ddr_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint, |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 279 | nfrac, frac, out_div); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 280 | |
| 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 287 | cpu_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 288 | 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] | 289 | cpu_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 290 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 291 | cpu_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 292 | |
| 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 297 | ddr_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 298 | 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] | 299 | ddr_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 300 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 301 | ddr_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 302 | |
| 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 307 | ahb_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 308 | 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] | 309 | ahb_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 310 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 311 | ahb_rate = cpu_pll / (postdiv + 1); |
| 312 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 313 | ath79_add_sys_clkdev("ref", ref_rate); |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame^] | 314 | 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 Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 317 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 318 | clk_add_alias("wdt", NULL, "ref", NULL); |
| 319 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 320 | |
| 321 | iounmap(dpll_base); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 324 | static void __init qca955x_clocks_init(void) |
| 325 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 326 | unsigned long ref_rate; |
| 327 | unsigned long cpu_rate; |
| 328 | unsigned long ddr_rate; |
| 329 | unsigned long ahb_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 330 | 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 336 | ref_rate = 40 * 1000 * 1000; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 337 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 338 | ref_rate = 25 * 1000 * 1000; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 339 | |
| 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 350 | cpu_pll = nint * ref_rate / ref_div; |
| 351 | cpu_pll += frac * ref_rate / (ref_div * (1 << 6)); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 352 | 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 364 | ddr_pll = nint * ref_rate / ref_div; |
| 365 | ddr_pll += frac * ref_rate / (ref_div * (1 << 10)); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 366 | 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 374 | cpu_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 375 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 376 | cpu_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 377 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 378 | cpu_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 379 | |
| 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 384 | ddr_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 385 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 386 | ddr_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 387 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 388 | ddr_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 389 | |
| 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 Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 394 | ahb_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 395 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 396 | ahb_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 397 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 398 | ahb_rate = cpu_pll / (postdiv + 1); |
| 399 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 400 | ath79_add_sys_clkdev("ref", ref_rate); |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame^] | 401 | 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 Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 404 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 405 | clk_add_alias("wdt", NULL, "ref", NULL); |
| 406 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 409 | void __init ath79_clocks_init(void) |
| 410 | { |
| 411 | if (soc_is_ar71xx()) |
| 412 | ar71xx_clocks_init(); |
Alban Bedel | f4c87b7 | 2016-03-17 06:34:10 +0300 | [diff] [blame] | 413 | else if (soc_is_ar724x() || soc_is_ar913x()) |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 414 | ar724x_clocks_init(); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 415 | else if (soc_is_ar933x()) |
| 416 | ar933x_clocks_init(); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 417 | else if (soc_is_ar934x()) |
| 418 | ar934x_clocks_init(); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 419 | else if (soc_is_qca955x()) |
| 420 | qca955x_clocks_init(); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 421 | else |
| 422 | BUG(); |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 423 | |
| 424 | of_clk_init(NULL); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Gabor Juhos | 2310780 | 2013-08-28 10:41:44 +0200 | [diff] [blame] | 427 | unsigned long __init |
| 428 | ath79_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 Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 442 | |
| 443 | #ifdef CONFIG_OF |
| 444 | static 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 | |
| 449 | CLK_OF_DECLARE(ar7100, "qca,ar7100-pll", ath79_clocks_init_dt); |
| 450 | CLK_OF_DECLARE(ar7240, "qca,ar7240-pll", ath79_clocks_init_dt); |
| 451 | CLK_OF_DECLARE(ar9130, "qca,ar9130-pll", ath79_clocks_init_dt); |
| 452 | CLK_OF_DECLARE(ar9330, "qca,ar9330-pll", ath79_clocks_init_dt); |
| 453 | CLK_OF_DECLARE(ar9340, "qca,ar9340-pll", ath79_clocks_init_dt); |
| 454 | CLK_OF_DECLARE(ar9550, "qca,qca9550-pll", ath79_clocks_init_dt); |
| 455 | #endif |