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> |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 15 | #include <linux/init.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/clk.h> |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 18 | #include <linux/clkdev.h> |
Alban Bedel | 411520a | 2015-04-19 14:30:04 +0200 | [diff] [blame] | 19 | #include <linux/clk-provider.h> |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 20 | #include <linux/of.h> |
| 21 | #include <linux/of_address.h> |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame] | 22 | #include <dt-bindings/clock/ath79-clk.h> |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 23 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 24 | #include <asm/div64.h> |
| 25 | |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 26 | #include <asm/mach-ath79/ath79.h> |
| 27 | #include <asm/mach-ath79/ar71xx_regs.h> |
| 28 | #include "common.h" |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 29 | #include "machtypes.h" |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 30 | |
| 31 | #define AR71XX_BASE_FREQ 40000000 |
Weijie Gao | c338d59 | 2016-03-17 06:34:09 +0300 | [diff] [blame] | 32 | #define AR724X_BASE_FREQ 40000000 |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 33 | |
Antony Pavlov | af5ad0d | 2016-03-17 06:34:14 +0300 | [diff] [blame] | 34 | static struct clk *clks[ATH79_CLK_END]; |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 35 | static struct clk_onecell_data clk_data = { |
| 36 | .clks = clks, |
| 37 | .clk_num = ARRAY_SIZE(clks), |
| 38 | }; |
| 39 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 40 | static const char * const clk_names[ATH79_CLK_END] = { |
| 41 | [ATH79_CLK_CPU] = "cpu", |
| 42 | [ATH79_CLK_DDR] = "ddr", |
| 43 | [ATH79_CLK_AHB] = "ahb", |
| 44 | [ATH79_CLK_REF] = "ref", |
| 45 | }; |
| 46 | |
| 47 | static const char * __init ath79_clk_name(int type) |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 48 | { |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 49 | BUG_ON(type >= ARRAY_SIZE(clk_names) || !clk_names[type]); |
| 50 | return clk_names[type]; |
| 51 | } |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 52 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 53 | static void __init __ath79_set_clk(int type, const char *name, struct clk *clk) |
| 54 | { |
Christophe JAILLET | 20d6f0c | 2016-10-30 09:25:46 +0100 | [diff] [blame] | 55 | if (IS_ERR(clk)) |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 56 | panic("failed to allocate %s clock structure", clk_names[type]); |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 57 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 58 | clks[type] = clk; |
| 59 | clk_register_clkdev(clk, name, NULL); |
| 60 | } |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 61 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 62 | static struct clk * __init ath79_set_clk(int type, unsigned long rate) |
| 63 | { |
| 64 | const char *name = ath79_clk_name(type); |
| 65 | struct clk *clk; |
| 66 | |
| 67 | clk = clk_register_fixed_rate(NULL, name, NULL, 0, rate); |
| 68 | __ath79_set_clk(type, name, clk); |
| 69 | return clk; |
| 70 | } |
| 71 | |
| 72 | static struct clk * __init ath79_set_ff_clk(int type, const char *parent, |
| 73 | unsigned int mult, unsigned int div) |
| 74 | { |
| 75 | const char *name = ath79_clk_name(type); |
| 76 | struct clk *clk; |
| 77 | |
| 78 | clk = clk_register_fixed_factor(NULL, name, parent, 0, mult, div); |
| 79 | __ath79_set_clk(type, name, clk); |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 80 | return clk; |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 81 | } |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 82 | |
| 83 | static void __init ar71xx_clocks_init(void) |
| 84 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 85 | unsigned long ref_rate; |
| 86 | unsigned long cpu_rate; |
| 87 | unsigned long ddr_rate; |
| 88 | unsigned long ahb_rate; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 89 | u32 pll; |
| 90 | u32 freq; |
| 91 | u32 div; |
| 92 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 93 | ref_rate = AR71XX_BASE_FREQ; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 94 | |
| 95 | pll = ath79_pll_rr(AR71XX_PLL_REG_CPU_CONFIG); |
| 96 | |
Alban Bedel | 626a069 | 2015-04-19 14:30:02 +0200 | [diff] [blame] | 97 | div = ((pll >> AR71XX_PLL_FB_SHIFT) & AR71XX_PLL_FB_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 98 | freq = div * ref_rate; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 99 | |
| 100 | div = ((pll >> AR71XX_CPU_DIV_SHIFT) & AR71XX_CPU_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 101 | cpu_rate = freq / div; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 102 | |
| 103 | div = ((pll >> AR71XX_DDR_DIV_SHIFT) & AR71XX_DDR_DIV_MASK) + 1; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 104 | ddr_rate = freq / div; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 105 | |
| 106 | div = (((pll >> AR71XX_AHB_DIV_SHIFT) & AR71XX_AHB_DIV_MASK) + 1) * 2; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 107 | ahb_rate = cpu_rate / div; |
| 108 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 109 | ath79_set_clk(ATH79_CLK_REF, ref_rate); |
| 110 | ath79_set_clk(ATH79_CLK_CPU, cpu_rate); |
| 111 | ath79_set_clk(ATH79_CLK_DDR, ddr_rate); |
| 112 | ath79_set_clk(ATH79_CLK_AHB, ahb_rate); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 113 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 114 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 115 | clk_add_alias("uart", NULL, "ahb", NULL); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 118 | static void __init ar724x_clk_init(struct clk *ref_clk, void __iomem *pll_base) |
| 119 | { |
| 120 | u32 pll; |
| 121 | u32 mult, div, ddr_div, ahb_div; |
| 122 | |
| 123 | pll = __raw_readl(pll_base + AR724X_PLL_REG_CPU_CONFIG); |
| 124 | |
| 125 | mult = ((pll >> AR724X_PLL_FB_SHIFT) & AR724X_PLL_FB_MASK); |
| 126 | div = ((pll >> AR724X_PLL_REF_DIV_SHIFT) & AR724X_PLL_REF_DIV_MASK) * 2; |
| 127 | |
| 128 | ddr_div = ((pll >> AR724X_DDR_DIV_SHIFT) & AR724X_DDR_DIV_MASK) + 1; |
| 129 | ahb_div = (((pll >> AR724X_AHB_DIV_SHIFT) & AR724X_AHB_DIV_MASK) + 1) * 2; |
| 130 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 131 | ath79_set_ff_clk(ATH79_CLK_CPU, "ref", mult, div); |
| 132 | ath79_set_ff_clk(ATH79_CLK_DDR, "ref", mult, div * ddr_div); |
| 133 | ath79_set_ff_clk(ATH79_CLK_AHB, "ref", mult, div * ahb_div); |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 134 | } |
| 135 | |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 136 | static void __init ar724x_clocks_init(void) |
| 137 | { |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 138 | struct clk *ref_clk; |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 139 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 140 | ref_clk = ath79_set_clk(ATH79_CLK_REF, AR724X_BASE_FREQ); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 141 | |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 142 | ar724x_clk_init(ref_clk, ath79_pll_base); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 143 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 144 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 145 | clk_add_alias("uart", NULL, "ahb", NULL); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 148 | static void __init ar9330_clk_init(struct clk *ref_clk, void __iomem *pll_base) |
| 149 | { |
| 150 | u32 clock_ctrl; |
| 151 | u32 ref_div; |
| 152 | u32 ninit_mul; |
| 153 | u32 out_div; |
| 154 | |
| 155 | u32 cpu_div; |
| 156 | u32 ddr_div; |
| 157 | u32 ahb_div; |
| 158 | |
| 159 | clock_ctrl = __raw_readl(pll_base + AR933X_PLL_CLOCK_CTRL_REG); |
| 160 | if (clock_ctrl & AR933X_PLL_CLOCK_CTRL_BYPASS) { |
| 161 | ref_div = 1; |
| 162 | ninit_mul = 1; |
| 163 | out_div = 1; |
| 164 | |
| 165 | cpu_div = 1; |
| 166 | ddr_div = 1; |
| 167 | ahb_div = 1; |
| 168 | } else { |
| 169 | u32 cpu_config; |
| 170 | u32 t; |
| 171 | |
| 172 | cpu_config = __raw_readl(pll_base + AR933X_PLL_CPU_CONFIG_REG); |
| 173 | |
| 174 | t = (cpu_config >> AR933X_PLL_CPU_CONFIG_REFDIV_SHIFT) & |
| 175 | AR933X_PLL_CPU_CONFIG_REFDIV_MASK; |
| 176 | ref_div = t; |
| 177 | |
| 178 | ninit_mul = (cpu_config >> AR933X_PLL_CPU_CONFIG_NINT_SHIFT) & |
| 179 | AR933X_PLL_CPU_CONFIG_NINT_MASK; |
| 180 | |
| 181 | t = (cpu_config >> AR933X_PLL_CPU_CONFIG_OUTDIV_SHIFT) & |
| 182 | AR933X_PLL_CPU_CONFIG_OUTDIV_MASK; |
| 183 | if (t == 0) |
| 184 | t = 1; |
| 185 | |
| 186 | out_div = (1 << t); |
| 187 | |
| 188 | cpu_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_CPU_DIV_SHIFT) & |
| 189 | AR933X_PLL_CLOCK_CTRL_CPU_DIV_MASK) + 1; |
| 190 | |
| 191 | ddr_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_DDR_DIV_SHIFT) & |
| 192 | AR933X_PLL_CLOCK_CTRL_DDR_DIV_MASK) + 1; |
| 193 | |
| 194 | ahb_div = ((clock_ctrl >> AR933X_PLL_CLOCK_CTRL_AHB_DIV_SHIFT) & |
| 195 | AR933X_PLL_CLOCK_CTRL_AHB_DIV_MASK) + 1; |
| 196 | } |
| 197 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 198 | ath79_set_ff_clk(ATH79_CLK_CPU, "ref", ninit_mul, |
| 199 | ref_div * out_div * cpu_div); |
| 200 | ath79_set_ff_clk(ATH79_CLK_DDR, "ref", ninit_mul, |
| 201 | ref_div * out_div * ddr_div); |
| 202 | ath79_set_ff_clk(ATH79_CLK_AHB, "ref", ninit_mul, |
| 203 | ref_div * out_div * ahb_div); |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 204 | } |
| 205 | |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 206 | static void __init ar933x_clocks_init(void) |
| 207 | { |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 208 | struct clk *ref_clk; |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 209 | unsigned long ref_rate; |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 210 | u32 t; |
| 211 | |
| 212 | t = ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP); |
| 213 | if (t & AR933X_BOOTSTRAP_REF_CLK_40) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 214 | ref_rate = (40 * 1000 * 1000); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 215 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 216 | ref_rate = (25 * 1000 * 1000); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 217 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 218 | ref_clk = ath79_set_clk(ATH79_CLK_REF, ref_rate); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 219 | |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 220 | ar9330_clk_init(ref_clk, ath79_pll_base); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 221 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 222 | clk_add_alias("wdt", NULL, "ahb", NULL); |
| 223 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 226 | static u32 __init ar934x_get_pll_freq(u32 ref, u32 ref_div, u32 nint, u32 nfrac, |
| 227 | u32 frac, u32 out_div) |
| 228 | { |
| 229 | u64 t; |
| 230 | u32 ret; |
| 231 | |
Gabor Juhos | 837f036 | 2013-08-28 10:41:43 +0200 | [diff] [blame] | 232 | t = ref; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 233 | t *= nint; |
| 234 | do_div(t, ref_div); |
| 235 | ret = t; |
| 236 | |
Gabor Juhos | 837f036 | 2013-08-28 10:41:43 +0200 | [diff] [blame] | 237 | t = ref; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 238 | t *= nfrac; |
| 239 | do_div(t, ref_div * frac); |
| 240 | ret += t; |
| 241 | |
| 242 | ret /= (1 << out_div); |
| 243 | return ret; |
| 244 | } |
| 245 | |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 246 | static void __init ar934x_clocks_init(void) |
| 247 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 248 | unsigned long ref_rate; |
| 249 | unsigned long cpu_rate; |
| 250 | unsigned long ddr_rate; |
| 251 | unsigned long ahb_rate; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 252 | u32 pll, out_div, ref_div, nint, nfrac, frac, clk_ctrl, postdiv; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 253 | u32 cpu_pll, ddr_pll; |
| 254 | u32 bootstrap; |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 255 | void __iomem *dpll_base; |
| 256 | |
| 257 | dpll_base = ioremap(AR934X_SRIF_BASE, AR934X_SRIF_SIZE); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 258 | |
| 259 | bootstrap = ath79_reset_rr(AR934X_RESET_REG_BOOTSTRAP); |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 260 | if (bootstrap & AR934X_BOOTSTRAP_REF_CLK_40) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 261 | ref_rate = 40 * 1000 * 1000; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 262 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 263 | ref_rate = 25 * 1000 * 1000; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 264 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 265 | pll = __raw_readl(dpll_base + AR934X_SRIF_CPU_DPLL2_REG); |
| 266 | if (pll & AR934X_SRIF_DPLL2_LOCAL_PLL) { |
| 267 | out_div = (pll >> AR934X_SRIF_DPLL2_OUTDIV_SHIFT) & |
| 268 | AR934X_SRIF_DPLL2_OUTDIV_MASK; |
| 269 | pll = __raw_readl(dpll_base + AR934X_SRIF_CPU_DPLL1_REG); |
| 270 | nint = (pll >> AR934X_SRIF_DPLL1_NINT_SHIFT) & |
| 271 | AR934X_SRIF_DPLL1_NINT_MASK; |
| 272 | nfrac = pll & AR934X_SRIF_DPLL1_NFRAC_MASK; |
| 273 | ref_div = (pll >> AR934X_SRIF_DPLL1_REFDIV_SHIFT) & |
| 274 | AR934X_SRIF_DPLL1_REFDIV_MASK; |
| 275 | frac = 1 << 18; |
| 276 | } else { |
| 277 | pll = ath79_pll_rr(AR934X_PLL_CPU_CONFIG_REG); |
| 278 | out_div = (pll >> AR934X_PLL_CPU_CONFIG_OUTDIV_SHIFT) & |
| 279 | AR934X_PLL_CPU_CONFIG_OUTDIV_MASK; |
| 280 | ref_div = (pll >> AR934X_PLL_CPU_CONFIG_REFDIV_SHIFT) & |
| 281 | AR934X_PLL_CPU_CONFIG_REFDIV_MASK; |
| 282 | nint = (pll >> AR934X_PLL_CPU_CONFIG_NINT_SHIFT) & |
| 283 | AR934X_PLL_CPU_CONFIG_NINT_MASK; |
| 284 | nfrac = (pll >> AR934X_PLL_CPU_CONFIG_NFRAC_SHIFT) & |
| 285 | AR934X_PLL_CPU_CONFIG_NFRAC_MASK; |
| 286 | frac = 1 << 6; |
| 287 | } |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 288 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 289 | cpu_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint, |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 290 | nfrac, frac, out_div); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 291 | |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 292 | pll = __raw_readl(dpll_base + AR934X_SRIF_DDR_DPLL2_REG); |
| 293 | if (pll & AR934X_SRIF_DPLL2_LOCAL_PLL) { |
| 294 | out_div = (pll >> AR934X_SRIF_DPLL2_OUTDIV_SHIFT) & |
| 295 | AR934X_SRIF_DPLL2_OUTDIV_MASK; |
| 296 | pll = __raw_readl(dpll_base + AR934X_SRIF_DDR_DPLL1_REG); |
| 297 | nint = (pll >> AR934X_SRIF_DPLL1_NINT_SHIFT) & |
| 298 | AR934X_SRIF_DPLL1_NINT_MASK; |
| 299 | nfrac = pll & AR934X_SRIF_DPLL1_NFRAC_MASK; |
| 300 | ref_div = (pll >> AR934X_SRIF_DPLL1_REFDIV_SHIFT) & |
| 301 | AR934X_SRIF_DPLL1_REFDIV_MASK; |
| 302 | frac = 1 << 18; |
| 303 | } else { |
| 304 | pll = ath79_pll_rr(AR934X_PLL_DDR_CONFIG_REG); |
| 305 | out_div = (pll >> AR934X_PLL_DDR_CONFIG_OUTDIV_SHIFT) & |
| 306 | AR934X_PLL_DDR_CONFIG_OUTDIV_MASK; |
| 307 | ref_div = (pll >> AR934X_PLL_DDR_CONFIG_REFDIV_SHIFT) & |
| 308 | AR934X_PLL_DDR_CONFIG_REFDIV_MASK; |
| 309 | nint = (pll >> AR934X_PLL_DDR_CONFIG_NINT_SHIFT) & |
| 310 | AR934X_PLL_DDR_CONFIG_NINT_MASK; |
| 311 | nfrac = (pll >> AR934X_PLL_DDR_CONFIG_NFRAC_SHIFT) & |
| 312 | AR934X_PLL_DDR_CONFIG_NFRAC_MASK; |
| 313 | frac = 1 << 10; |
| 314 | } |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 315 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 316 | ddr_pll = ar934x_get_pll_freq(ref_rate, ref_div, nint, |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 317 | nfrac, frac, out_div); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 318 | |
| 319 | clk_ctrl = ath79_pll_rr(AR934X_PLL_CPU_DDR_CLK_CTRL_REG); |
| 320 | |
| 321 | postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_SHIFT) & |
| 322 | AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_POST_DIV_MASK; |
| 323 | |
| 324 | if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_CPU_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 325 | cpu_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 326 | 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] | 327 | cpu_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 328 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 329 | cpu_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 330 | |
| 331 | postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_SHIFT) & |
| 332 | AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_POST_DIV_MASK; |
| 333 | |
| 334 | if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_DDR_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 335 | ddr_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 336 | 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] | 337 | ddr_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 338 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 339 | ddr_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 340 | |
| 341 | postdiv = (clk_ctrl >> AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_SHIFT) & |
| 342 | AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_POST_DIV_MASK; |
| 343 | |
| 344 | if (clk_ctrl & AR934X_PLL_CPU_DDR_CLK_CTRL_AHB_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 345 | ahb_rate = ref_rate; |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 346 | 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] | 347 | ahb_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 348 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 349 | ahb_rate = cpu_pll / (postdiv + 1); |
| 350 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 351 | ath79_set_clk(ATH79_CLK_REF, ref_rate); |
| 352 | ath79_set_clk(ATH79_CLK_CPU, cpu_rate); |
| 353 | ath79_set_clk(ATH79_CLK_DDR, ddr_rate); |
| 354 | ath79_set_clk(ATH79_CLK_AHB, ahb_rate); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 355 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 356 | clk_add_alias("wdt", NULL, "ref", NULL); |
| 357 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 97541cc | 2012-09-08 14:02:21 +0200 | [diff] [blame] | 358 | |
| 359 | iounmap(dpll_base); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Matthias Schiffer | af2d1b5 | 2018-07-20 13:58:19 +0200 | [diff] [blame] | 362 | static void __init qca953x_clocks_init(void) |
| 363 | { |
| 364 | unsigned long ref_rate; |
| 365 | unsigned long cpu_rate; |
| 366 | unsigned long ddr_rate; |
| 367 | unsigned long ahb_rate; |
| 368 | u32 pll, out_div, ref_div, nint, frac, clk_ctrl, postdiv; |
| 369 | u32 cpu_pll, ddr_pll; |
| 370 | u32 bootstrap; |
| 371 | |
| 372 | bootstrap = ath79_reset_rr(QCA953X_RESET_REG_BOOTSTRAP); |
| 373 | if (bootstrap & QCA953X_BOOTSTRAP_REF_CLK_40) |
| 374 | ref_rate = 40 * 1000 * 1000; |
| 375 | else |
| 376 | ref_rate = 25 * 1000 * 1000; |
| 377 | |
| 378 | pll = ath79_pll_rr(QCA953X_PLL_CPU_CONFIG_REG); |
| 379 | out_div = (pll >> QCA953X_PLL_CPU_CONFIG_OUTDIV_SHIFT) & |
| 380 | QCA953X_PLL_CPU_CONFIG_OUTDIV_MASK; |
| 381 | ref_div = (pll >> QCA953X_PLL_CPU_CONFIG_REFDIV_SHIFT) & |
| 382 | QCA953X_PLL_CPU_CONFIG_REFDIV_MASK; |
| 383 | nint = (pll >> QCA953X_PLL_CPU_CONFIG_NINT_SHIFT) & |
| 384 | QCA953X_PLL_CPU_CONFIG_NINT_MASK; |
| 385 | frac = (pll >> QCA953X_PLL_CPU_CONFIG_NFRAC_SHIFT) & |
| 386 | QCA953X_PLL_CPU_CONFIG_NFRAC_MASK; |
| 387 | |
| 388 | cpu_pll = nint * ref_rate / ref_div; |
| 389 | cpu_pll += frac * (ref_rate >> 6) / ref_div; |
| 390 | cpu_pll /= (1 << out_div); |
| 391 | |
| 392 | pll = ath79_pll_rr(QCA953X_PLL_DDR_CONFIG_REG); |
| 393 | out_div = (pll >> QCA953X_PLL_DDR_CONFIG_OUTDIV_SHIFT) & |
| 394 | QCA953X_PLL_DDR_CONFIG_OUTDIV_MASK; |
| 395 | ref_div = (pll >> QCA953X_PLL_DDR_CONFIG_REFDIV_SHIFT) & |
| 396 | QCA953X_PLL_DDR_CONFIG_REFDIV_MASK; |
| 397 | nint = (pll >> QCA953X_PLL_DDR_CONFIG_NINT_SHIFT) & |
| 398 | QCA953X_PLL_DDR_CONFIG_NINT_MASK; |
| 399 | frac = (pll >> QCA953X_PLL_DDR_CONFIG_NFRAC_SHIFT) & |
| 400 | QCA953X_PLL_DDR_CONFIG_NFRAC_MASK; |
| 401 | |
| 402 | ddr_pll = nint * ref_rate / ref_div; |
| 403 | ddr_pll += frac * (ref_rate >> 6) / (ref_div << 4); |
| 404 | ddr_pll /= (1 << out_div); |
| 405 | |
| 406 | clk_ctrl = ath79_pll_rr(QCA953X_PLL_CLK_CTRL_REG); |
| 407 | |
| 408 | postdiv = (clk_ctrl >> QCA953X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) & |
| 409 | QCA953X_PLL_CLK_CTRL_CPU_POST_DIV_MASK; |
| 410 | |
| 411 | if (clk_ctrl & QCA953X_PLL_CLK_CTRL_CPU_PLL_BYPASS) |
| 412 | cpu_rate = ref_rate; |
| 413 | else if (clk_ctrl & QCA953X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL) |
| 414 | cpu_rate = cpu_pll / (postdiv + 1); |
| 415 | else |
| 416 | cpu_rate = ddr_pll / (postdiv + 1); |
| 417 | |
| 418 | postdiv = (clk_ctrl >> QCA953X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) & |
| 419 | QCA953X_PLL_CLK_CTRL_DDR_POST_DIV_MASK; |
| 420 | |
| 421 | if (clk_ctrl & QCA953X_PLL_CLK_CTRL_DDR_PLL_BYPASS) |
| 422 | ddr_rate = ref_rate; |
| 423 | else if (clk_ctrl & QCA953X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL) |
| 424 | ddr_rate = ddr_pll / (postdiv + 1); |
| 425 | else |
| 426 | ddr_rate = cpu_pll / (postdiv + 1); |
| 427 | |
| 428 | postdiv = (clk_ctrl >> QCA953X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) & |
| 429 | QCA953X_PLL_CLK_CTRL_AHB_POST_DIV_MASK; |
| 430 | |
| 431 | if (clk_ctrl & QCA953X_PLL_CLK_CTRL_AHB_PLL_BYPASS) |
| 432 | ahb_rate = ref_rate; |
| 433 | else if (clk_ctrl & QCA953X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL) |
| 434 | ahb_rate = ddr_pll / (postdiv + 1); |
| 435 | else |
| 436 | ahb_rate = cpu_pll / (postdiv + 1); |
| 437 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 438 | ath79_set_clk(ATH79_CLK_REF, ref_rate); |
| 439 | ath79_set_clk(ATH79_CLK_CPU, cpu_rate); |
| 440 | ath79_set_clk(ATH79_CLK_DDR, ddr_rate); |
| 441 | ath79_set_clk(ATH79_CLK_AHB, ahb_rate); |
Matthias Schiffer | af2d1b5 | 2018-07-20 13:58:19 +0200 | [diff] [blame] | 442 | |
| 443 | clk_add_alias("wdt", NULL, "ref", NULL); |
| 444 | clk_add_alias("uart", NULL, "ref", NULL); |
| 445 | } |
| 446 | |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 447 | static void __init qca955x_clocks_init(void) |
| 448 | { |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 449 | unsigned long ref_rate; |
| 450 | unsigned long cpu_rate; |
| 451 | unsigned long ddr_rate; |
| 452 | unsigned long ahb_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 453 | u32 pll, out_div, ref_div, nint, frac, clk_ctrl, postdiv; |
| 454 | u32 cpu_pll, ddr_pll; |
| 455 | u32 bootstrap; |
| 456 | |
| 457 | bootstrap = ath79_reset_rr(QCA955X_RESET_REG_BOOTSTRAP); |
| 458 | if (bootstrap & QCA955X_BOOTSTRAP_REF_CLK_40) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 459 | ref_rate = 40 * 1000 * 1000; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 460 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 461 | ref_rate = 25 * 1000 * 1000; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 462 | |
| 463 | pll = ath79_pll_rr(QCA955X_PLL_CPU_CONFIG_REG); |
| 464 | out_div = (pll >> QCA955X_PLL_CPU_CONFIG_OUTDIV_SHIFT) & |
| 465 | QCA955X_PLL_CPU_CONFIG_OUTDIV_MASK; |
| 466 | ref_div = (pll >> QCA955X_PLL_CPU_CONFIG_REFDIV_SHIFT) & |
| 467 | QCA955X_PLL_CPU_CONFIG_REFDIV_MASK; |
| 468 | nint = (pll >> QCA955X_PLL_CPU_CONFIG_NINT_SHIFT) & |
| 469 | QCA955X_PLL_CPU_CONFIG_NINT_MASK; |
| 470 | frac = (pll >> QCA955X_PLL_CPU_CONFIG_NFRAC_SHIFT) & |
| 471 | QCA955X_PLL_CPU_CONFIG_NFRAC_MASK; |
| 472 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 473 | cpu_pll = nint * ref_rate / ref_div; |
| 474 | cpu_pll += frac * ref_rate / (ref_div * (1 << 6)); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 475 | cpu_pll /= (1 << out_div); |
| 476 | |
| 477 | pll = ath79_pll_rr(QCA955X_PLL_DDR_CONFIG_REG); |
| 478 | out_div = (pll >> QCA955X_PLL_DDR_CONFIG_OUTDIV_SHIFT) & |
| 479 | QCA955X_PLL_DDR_CONFIG_OUTDIV_MASK; |
| 480 | ref_div = (pll >> QCA955X_PLL_DDR_CONFIG_REFDIV_SHIFT) & |
| 481 | QCA955X_PLL_DDR_CONFIG_REFDIV_MASK; |
| 482 | nint = (pll >> QCA955X_PLL_DDR_CONFIG_NINT_SHIFT) & |
| 483 | QCA955X_PLL_DDR_CONFIG_NINT_MASK; |
| 484 | frac = (pll >> QCA955X_PLL_DDR_CONFIG_NFRAC_SHIFT) & |
| 485 | QCA955X_PLL_DDR_CONFIG_NFRAC_MASK; |
| 486 | |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 487 | ddr_pll = nint * ref_rate / ref_div; |
| 488 | ddr_pll += frac * ref_rate / (ref_div * (1 << 10)); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 489 | ddr_pll /= (1 << out_div); |
| 490 | |
| 491 | clk_ctrl = ath79_pll_rr(QCA955X_PLL_CLK_CTRL_REG); |
| 492 | |
| 493 | postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) & |
| 494 | QCA955X_PLL_CLK_CTRL_CPU_POST_DIV_MASK; |
| 495 | |
| 496 | if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPU_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 497 | cpu_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 498 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_CPUCLK_FROM_CPUPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 499 | cpu_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 500 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 501 | cpu_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 502 | |
| 503 | postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) & |
| 504 | QCA955X_PLL_CLK_CTRL_DDR_POST_DIV_MASK; |
| 505 | |
| 506 | if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDR_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 507 | ddr_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 508 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_DDRCLK_FROM_DDRPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 509 | ddr_rate = cpu_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 510 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 511 | ddr_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 512 | |
| 513 | postdiv = (clk_ctrl >> QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) & |
| 514 | QCA955X_PLL_CLK_CTRL_AHB_POST_DIV_MASK; |
| 515 | |
| 516 | if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHB_PLL_BYPASS) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 517 | ahb_rate = ref_rate; |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 518 | else if (clk_ctrl & QCA955X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL) |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 519 | ahb_rate = ddr_pll / (postdiv + 1); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 520 | else |
Gabor Juhos | 6612a68 | 2013-08-28 10:41:46 +0200 | [diff] [blame] | 521 | ahb_rate = cpu_pll / (postdiv + 1); |
| 522 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 523 | ath79_set_clk(ATH79_CLK_REF, ref_rate); |
| 524 | ath79_set_clk(ATH79_CLK_CPU, cpu_rate); |
| 525 | ath79_set_clk(ATH79_CLK_DDR, ddr_rate); |
| 526 | ath79_set_clk(ATH79_CLK_AHB, ahb_rate); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 527 | |
Gabor Juhos | 2c4f1ac | 2013-08-28 10:41:47 +0200 | [diff] [blame] | 528 | clk_add_alias("wdt", NULL, "ref", NULL); |
| 529 | clk_add_alias("uart", NULL, "ref", NULL); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Matthias Schiffer | af2d1b5 | 2018-07-20 13:58:19 +0200 | [diff] [blame] | 532 | static void __init qca956x_clocks_init(void) |
| 533 | { |
| 534 | unsigned long ref_rate; |
| 535 | unsigned long cpu_rate; |
| 536 | unsigned long ddr_rate; |
| 537 | unsigned long ahb_rate; |
| 538 | u32 pll, out_div, ref_div, nint, hfrac, lfrac, clk_ctrl, postdiv; |
| 539 | u32 cpu_pll, ddr_pll; |
| 540 | u32 bootstrap; |
| 541 | |
| 542 | /* |
| 543 | * QCA956x timer init workaround has to be applied right before setting |
| 544 | * up the clock. Else, there will be no jiffies |
| 545 | */ |
| 546 | u32 misc; |
| 547 | |
| 548 | misc = ath79_reset_rr(AR71XX_RESET_REG_MISC_INT_ENABLE); |
| 549 | misc |= MISC_INT_MIPS_SI_TIMERINT_MASK; |
| 550 | ath79_reset_wr(AR71XX_RESET_REG_MISC_INT_ENABLE, misc); |
| 551 | |
| 552 | bootstrap = ath79_reset_rr(QCA956X_RESET_REG_BOOTSTRAP); |
| 553 | if (bootstrap & QCA956X_BOOTSTRAP_REF_CLK_40) |
| 554 | ref_rate = 40 * 1000 * 1000; |
| 555 | else |
| 556 | ref_rate = 25 * 1000 * 1000; |
| 557 | |
| 558 | pll = ath79_pll_rr(QCA956X_PLL_CPU_CONFIG_REG); |
| 559 | out_div = (pll >> QCA956X_PLL_CPU_CONFIG_OUTDIV_SHIFT) & |
| 560 | QCA956X_PLL_CPU_CONFIG_OUTDIV_MASK; |
| 561 | ref_div = (pll >> QCA956X_PLL_CPU_CONFIG_REFDIV_SHIFT) & |
| 562 | QCA956X_PLL_CPU_CONFIG_REFDIV_MASK; |
| 563 | |
| 564 | pll = ath79_pll_rr(QCA956X_PLL_CPU_CONFIG1_REG); |
| 565 | nint = (pll >> QCA956X_PLL_CPU_CONFIG1_NINT_SHIFT) & |
| 566 | QCA956X_PLL_CPU_CONFIG1_NINT_MASK; |
| 567 | hfrac = (pll >> QCA956X_PLL_CPU_CONFIG1_NFRAC_H_SHIFT) & |
| 568 | QCA956X_PLL_CPU_CONFIG1_NFRAC_H_MASK; |
| 569 | lfrac = (pll >> QCA956X_PLL_CPU_CONFIG1_NFRAC_L_SHIFT) & |
| 570 | QCA956X_PLL_CPU_CONFIG1_NFRAC_L_MASK; |
| 571 | |
| 572 | cpu_pll = nint * ref_rate / ref_div; |
| 573 | cpu_pll += (lfrac * ref_rate) / ((ref_div * 25) << 13); |
| 574 | cpu_pll += (hfrac >> 13) * ref_rate / ref_div; |
| 575 | cpu_pll /= (1 << out_div); |
| 576 | |
| 577 | pll = ath79_pll_rr(QCA956X_PLL_DDR_CONFIG_REG); |
| 578 | out_div = (pll >> QCA956X_PLL_DDR_CONFIG_OUTDIV_SHIFT) & |
| 579 | QCA956X_PLL_DDR_CONFIG_OUTDIV_MASK; |
| 580 | ref_div = (pll >> QCA956X_PLL_DDR_CONFIG_REFDIV_SHIFT) & |
| 581 | QCA956X_PLL_DDR_CONFIG_REFDIV_MASK; |
| 582 | pll = ath79_pll_rr(QCA956X_PLL_DDR_CONFIG1_REG); |
| 583 | nint = (pll >> QCA956X_PLL_DDR_CONFIG1_NINT_SHIFT) & |
| 584 | QCA956X_PLL_DDR_CONFIG1_NINT_MASK; |
| 585 | hfrac = (pll >> QCA956X_PLL_DDR_CONFIG1_NFRAC_H_SHIFT) & |
| 586 | QCA956X_PLL_DDR_CONFIG1_NFRAC_H_MASK; |
| 587 | lfrac = (pll >> QCA956X_PLL_DDR_CONFIG1_NFRAC_L_SHIFT) & |
| 588 | QCA956X_PLL_DDR_CONFIG1_NFRAC_L_MASK; |
| 589 | |
| 590 | ddr_pll = nint * ref_rate / ref_div; |
| 591 | ddr_pll += (lfrac * ref_rate) / ((ref_div * 25) << 13); |
| 592 | ddr_pll += (hfrac >> 13) * ref_rate / ref_div; |
| 593 | ddr_pll /= (1 << out_div); |
| 594 | |
| 595 | clk_ctrl = ath79_pll_rr(QCA956X_PLL_CLK_CTRL_REG); |
| 596 | |
| 597 | postdiv = (clk_ctrl >> QCA956X_PLL_CLK_CTRL_CPU_POST_DIV_SHIFT) & |
| 598 | QCA956X_PLL_CLK_CTRL_CPU_POST_DIV_MASK; |
| 599 | |
| 600 | if (clk_ctrl & QCA956X_PLL_CLK_CTRL_CPU_PLL_BYPASS) |
| 601 | cpu_rate = ref_rate; |
| 602 | else if (clk_ctrl & QCA956X_PLL_CLK_CTRL_CPU_DDRCLK_FROM_CPUPLL) |
| 603 | cpu_rate = ddr_pll / (postdiv + 1); |
| 604 | else |
| 605 | cpu_rate = cpu_pll / (postdiv + 1); |
| 606 | |
| 607 | postdiv = (clk_ctrl >> QCA956X_PLL_CLK_CTRL_DDR_POST_DIV_SHIFT) & |
| 608 | QCA956X_PLL_CLK_CTRL_DDR_POST_DIV_MASK; |
| 609 | |
| 610 | if (clk_ctrl & QCA956X_PLL_CLK_CTRL_DDR_PLL_BYPASS) |
| 611 | ddr_rate = ref_rate; |
| 612 | else if (clk_ctrl & QCA956X_PLL_CLK_CTRL_CPU_DDRCLK_FROM_DDRPLL) |
| 613 | ddr_rate = cpu_pll / (postdiv + 1); |
| 614 | else |
| 615 | ddr_rate = ddr_pll / (postdiv + 1); |
| 616 | |
| 617 | postdiv = (clk_ctrl >> QCA956X_PLL_CLK_CTRL_AHB_POST_DIV_SHIFT) & |
| 618 | QCA956X_PLL_CLK_CTRL_AHB_POST_DIV_MASK; |
| 619 | |
| 620 | if (clk_ctrl & QCA956X_PLL_CLK_CTRL_AHB_PLL_BYPASS) |
| 621 | ahb_rate = ref_rate; |
| 622 | else if (clk_ctrl & QCA956X_PLL_CLK_CTRL_AHBCLK_FROM_DDRPLL) |
| 623 | ahb_rate = ddr_pll / (postdiv + 1); |
| 624 | else |
| 625 | ahb_rate = cpu_pll / (postdiv + 1); |
| 626 | |
Felix Fietkau | 9b56e0d | 2019-01-11 15:22:30 +0100 | [diff] [blame^] | 627 | ath79_set_clk(ATH79_CLK_REF, ref_rate); |
| 628 | ath79_set_clk(ATH79_CLK_CPU, cpu_rate); |
| 629 | ath79_set_clk(ATH79_CLK_DDR, ddr_rate); |
| 630 | ath79_set_clk(ATH79_CLK_AHB, ahb_rate); |
Matthias Schiffer | af2d1b5 | 2018-07-20 13:58:19 +0200 | [diff] [blame] | 631 | |
| 632 | clk_add_alias("wdt", NULL, "ref", NULL); |
| 633 | clk_add_alias("uart", NULL, "ref", NULL); |
| 634 | } |
| 635 | |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 636 | void __init ath79_clocks_init(void) |
| 637 | { |
| 638 | if (soc_is_ar71xx()) |
| 639 | ar71xx_clocks_init(); |
Alban Bedel | f4c87b7 | 2016-03-17 06:34:10 +0300 | [diff] [blame] | 640 | else if (soc_is_ar724x() || soc_is_ar913x()) |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 641 | ar724x_clocks_init(); |
Gabor Juhos | 04225e1 | 2011-06-20 21:26:04 +0200 | [diff] [blame] | 642 | else if (soc_is_ar933x()) |
| 643 | ar933x_clocks_init(); |
Gabor Juhos | 8889612 | 2012-03-14 10:45:22 +0100 | [diff] [blame] | 644 | else if (soc_is_ar934x()) |
| 645 | ar934x_clocks_init(); |
Matthias Schiffer | af2d1b5 | 2018-07-20 13:58:19 +0200 | [diff] [blame] | 646 | else if (soc_is_qca953x()) |
| 647 | qca953x_clocks_init(); |
Gabor Juhos | 41583c0 | 2013-02-15 13:38:17 +0000 | [diff] [blame] | 648 | else if (soc_is_qca955x()) |
| 649 | qca955x_clocks_init(); |
Matthias Schiffer | af2d1b5 | 2018-07-20 13:58:19 +0200 | [diff] [blame] | 650 | else if (soc_is_qca956x() || soc_is_tp9343()) |
| 651 | qca956x_clocks_init(); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 652 | else |
| 653 | BUG(); |
Gabor Juhos | d4a67d9 | 2011-01-04 21:28:14 +0100 | [diff] [blame] | 654 | } |
| 655 | |
Gabor Juhos | 2310780 | 2013-08-28 10:41:44 +0200 | [diff] [blame] | 656 | unsigned long __init |
| 657 | ath79_get_sys_clk_rate(const char *id) |
| 658 | { |
| 659 | struct clk *clk; |
| 660 | unsigned long rate; |
| 661 | |
| 662 | clk = clk_get(NULL, id); |
| 663 | if (IS_ERR(clk)) |
| 664 | panic("unable to get %s clock, err=%d", id, (int) PTR_ERR(clk)); |
| 665 | |
| 666 | rate = clk_get_rate(clk); |
| 667 | clk_put(clk); |
| 668 | |
| 669 | return rate; |
| 670 | } |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 671 | |
| 672 | #ifdef CONFIG_OF |
| 673 | static void __init ath79_clocks_init_dt(struct device_node *np) |
| 674 | { |
| 675 | of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data); |
| 676 | } |
| 677 | |
| 678 | CLK_OF_DECLARE(ar7100, "qca,ar7100-pll", ath79_clocks_init_dt); |
| 679 | CLK_OF_DECLARE(ar7240, "qca,ar7240-pll", ath79_clocks_init_dt); |
Alban Bedel | 6451af0 | 2015-05-31 02:18:22 +0200 | [diff] [blame] | 680 | CLK_OF_DECLARE(ar9340, "qca,ar9340-pll", ath79_clocks_init_dt); |
| 681 | CLK_OF_DECLARE(ar9550, "qca,qca9550-pll", ath79_clocks_init_dt); |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 682 | |
| 683 | static void __init ath79_clocks_init_dt_ng(struct device_node *np) |
| 684 | { |
| 685 | struct clk *ref_clk; |
| 686 | void __iomem *pll_base; |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 687 | |
| 688 | ref_clk = of_clk_get(np, 0); |
| 689 | if (IS_ERR(ref_clk)) { |
Rob Herring | 7f27b5b | 2017-07-18 16:42:45 -0500 | [diff] [blame] | 690 | pr_err("%pOF: of_clk_get failed\n", np); |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 691 | goto err; |
| 692 | } |
| 693 | |
| 694 | pll_base = of_iomap(np, 0); |
| 695 | if (!pll_base) { |
Rob Herring | 7f27b5b | 2017-07-18 16:42:45 -0500 | [diff] [blame] | 696 | pr_err("%pOF: can't map pll registers\n", np); |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 697 | goto err_clk; |
| 698 | } |
| 699 | |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 700 | if (of_device_is_compatible(np, "qca,ar9130-pll")) |
| 701 | ar724x_clk_init(ref_clk, pll_base); |
| 702 | else if (of_device_is_compatible(np, "qca,ar9330-pll")) |
| 703 | ar9330_clk_init(ref_clk, pll_base); |
| 704 | else { |
Rob Herring | 7f27b5b | 2017-07-18 16:42:45 -0500 | [diff] [blame] | 705 | pr_err("%pOF: could not find any appropriate clk_init()\n", np); |
Arvind Yadav | b3d91db | 2017-01-02 15:18:21 +0530 | [diff] [blame] | 706 | goto err_iounmap; |
Antony Pavlov | 5ae5c45 | 2016-03-17 06:34:18 +0300 | [diff] [blame] | 707 | } |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 708 | |
| 709 | if (of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data)) { |
Rob Herring | 7f27b5b | 2017-07-18 16:42:45 -0500 | [diff] [blame] | 710 | pr_err("%pOF: could not register clk provider\n", np); |
Arvind Yadav | b3d91db | 2017-01-02 15:18:21 +0530 | [diff] [blame] | 711 | goto err_iounmap; |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | return; |
| 715 | |
Arvind Yadav | b3d91db | 2017-01-02 15:18:21 +0530 | [diff] [blame] | 716 | err_iounmap: |
| 717 | iounmap(pll_base); |
| 718 | |
Antony Pavlov | 3bdf107 | 2016-03-17 06:34:15 +0300 | [diff] [blame] | 719 | err_clk: |
| 720 | clk_put(ref_clk); |
| 721 | |
| 722 | err: |
| 723 | return; |
| 724 | } |
| 725 | 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] | 726 | 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] | 727 | #endif |