Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * rcar_gen2 Core CPG Clocks |
| 3 | * |
| 4 | * Copyright (C) 2013 Ideas On Board SPRL |
| 5 | * |
| 6 | * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/clk-provider.h> |
| 14 | #include <linux/clkdev.h> |
| 15 | #include <linux/clk/shmobile.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/math64.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | |
| 23 | struct rcar_gen2_cpg { |
| 24 | struct clk_onecell_data data; |
| 25 | spinlock_t lock; |
| 26 | void __iomem *reg; |
| 27 | }; |
| 28 | |
Benoit Cousson | d912019 | 2014-02-28 14:12:05 +0100 | [diff] [blame] | 29 | #define CPG_FRQCRB 0x00000004 |
| 30 | #define CPG_FRQCRB_KICK BIT(31) |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 31 | #define CPG_SDCKCR 0x00000074 |
| 32 | #define CPG_PLL0CR 0x000000d8 |
| 33 | #define CPG_FRQCRC 0x000000e0 |
| 34 | #define CPG_FRQCRC_ZFC_MASK (0x1f << 8) |
| 35 | #define CPG_FRQCRC_ZFC_SHIFT 8 |
| 36 | |
| 37 | /* ----------------------------------------------------------------------------- |
| 38 | * Z Clock |
| 39 | * |
| 40 | * Traits of this clock: |
| 41 | * prepare - clk_prepare only ensures that parents are prepared |
| 42 | * enable - clk_enable only ensures that parents are enabled |
| 43 | * rate - rate is adjustable. clk->rate = parent->rate * mult / 32 |
| 44 | * parent - fixed parent. No clk_set_parent support |
| 45 | */ |
| 46 | |
| 47 | struct cpg_z_clk { |
| 48 | struct clk_hw hw; |
| 49 | void __iomem *reg; |
Benoit Cousson | d912019 | 2014-02-28 14:12:05 +0100 | [diff] [blame] | 50 | void __iomem *kick_reg; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw) |
| 54 | |
| 55 | static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw, |
| 56 | unsigned long parent_rate) |
| 57 | { |
| 58 | struct cpg_z_clk *zclk = to_z_clk(hw); |
| 59 | unsigned int mult; |
| 60 | unsigned int val; |
| 61 | |
| 62 | val = (clk_readl(zclk->reg) & CPG_FRQCRC_ZFC_MASK) |
| 63 | >> CPG_FRQCRC_ZFC_SHIFT; |
| 64 | mult = 32 - val; |
| 65 | |
| 66 | return div_u64((u64)parent_rate * mult, 32); |
| 67 | } |
| 68 | |
| 69 | static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate, |
| 70 | unsigned long *parent_rate) |
| 71 | { |
| 72 | unsigned long prate = *parent_rate; |
| 73 | unsigned int mult; |
| 74 | |
| 75 | if (!prate) |
| 76 | prate = 1; |
| 77 | |
| 78 | mult = div_u64((u64)rate * 32, prate); |
| 79 | mult = clamp(mult, 1U, 32U); |
| 80 | |
| 81 | return *parent_rate / 32 * mult; |
| 82 | } |
| 83 | |
| 84 | static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate, |
| 85 | unsigned long parent_rate) |
| 86 | { |
| 87 | struct cpg_z_clk *zclk = to_z_clk(hw); |
| 88 | unsigned int mult; |
Benoit Cousson | d912019 | 2014-02-28 14:12:05 +0100 | [diff] [blame] | 89 | u32 val, kick; |
| 90 | unsigned int i; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 91 | |
| 92 | mult = div_u64((u64)rate * 32, parent_rate); |
| 93 | mult = clamp(mult, 1U, 32U); |
| 94 | |
Benoit Cousson | d912019 | 2014-02-28 14:12:05 +0100 | [diff] [blame] | 95 | if (clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK) |
| 96 | return -EBUSY; |
| 97 | |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 98 | val = clk_readl(zclk->reg); |
| 99 | val &= ~CPG_FRQCRC_ZFC_MASK; |
| 100 | val |= (32 - mult) << CPG_FRQCRC_ZFC_SHIFT; |
| 101 | clk_writel(val, zclk->reg); |
| 102 | |
Benoit Cousson | d912019 | 2014-02-28 14:12:05 +0100 | [diff] [blame] | 103 | /* |
| 104 | * Set KICK bit in FRQCRB to update hardware setting and wait for |
| 105 | * clock change completion. |
| 106 | */ |
| 107 | kick = clk_readl(zclk->kick_reg); |
| 108 | kick |= CPG_FRQCRB_KICK; |
| 109 | clk_writel(kick, zclk->kick_reg); |
| 110 | |
| 111 | /* |
| 112 | * Note: There is no HW information about the worst case latency. |
| 113 | * |
| 114 | * Using experimental measurements, it seems that no more than |
| 115 | * ~10 iterations are needed, independently of the CPU rate. |
| 116 | * Since this value might be dependant of external xtal rate, pll1 |
| 117 | * rate or even the other emulation clocks rate, use 1000 as a |
| 118 | * "super" safe value. |
| 119 | */ |
| 120 | for (i = 1000; i; i--) { |
| 121 | if (!(clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK)) |
| 122 | return 0; |
| 123 | |
| 124 | cpu_relax(); |
| 125 | } |
| 126 | |
| 127 | return -ETIMEDOUT; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static const struct clk_ops cpg_z_clk_ops = { |
| 131 | .recalc_rate = cpg_z_clk_recalc_rate, |
| 132 | .round_rate = cpg_z_clk_round_rate, |
| 133 | .set_rate = cpg_z_clk_set_rate, |
| 134 | }; |
| 135 | |
| 136 | static struct clk * __init cpg_z_clk_register(struct rcar_gen2_cpg *cpg) |
| 137 | { |
| 138 | static const char *parent_name = "pll0"; |
| 139 | struct clk_init_data init; |
| 140 | struct cpg_z_clk *zclk; |
| 141 | struct clk *clk; |
| 142 | |
| 143 | zclk = kzalloc(sizeof(*zclk), GFP_KERNEL); |
| 144 | if (!zclk) |
| 145 | return ERR_PTR(-ENOMEM); |
| 146 | |
| 147 | init.name = "z"; |
| 148 | init.ops = &cpg_z_clk_ops; |
| 149 | init.flags = 0; |
| 150 | init.parent_names = &parent_name; |
| 151 | init.num_parents = 1; |
| 152 | |
| 153 | zclk->reg = cpg->reg + CPG_FRQCRC; |
Benoit Cousson | d912019 | 2014-02-28 14:12:05 +0100 | [diff] [blame] | 154 | zclk->kick_reg = cpg->reg + CPG_FRQCRB; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 155 | zclk->hw.init = &init; |
| 156 | |
| 157 | clk = clk_register(NULL, &zclk->hw); |
| 158 | if (IS_ERR(clk)) |
| 159 | kfree(zclk); |
| 160 | |
| 161 | return clk; |
| 162 | } |
| 163 | |
| 164 | /* ----------------------------------------------------------------------------- |
| 165 | * CPG Clock Data |
| 166 | */ |
| 167 | |
| 168 | /* |
| 169 | * MD EXTAL PLL0 PLL1 PLL3 |
| 170 | * 14 13 19 (MHz) *1 *1 |
| 171 | *--------------------------------------------------- |
| 172 | * 0 0 0 15 x 1 x172/2 x208/2 x106 |
| 173 | * 0 0 1 15 x 1 x172/2 x208/2 x88 |
| 174 | * 0 1 0 20 x 1 x130/2 x156/2 x80 |
| 175 | * 0 1 1 20 x 1 x130/2 x156/2 x66 |
| 176 | * 1 0 0 26 / 2 x200/2 x240/2 x122 |
| 177 | * 1 0 1 26 / 2 x200/2 x240/2 x102 |
| 178 | * 1 1 0 30 / 2 x172/2 x208/2 x106 |
| 179 | * 1 1 1 30 / 2 x172/2 x208/2 x88 |
| 180 | * |
| 181 | * *1 : Table 7.6 indicates VCO ouput (PLLx = VCO/2) |
| 182 | */ |
| 183 | #define CPG_PLL_CONFIG_INDEX(md) ((((md) & BIT(14)) >> 12) | \ |
| 184 | (((md) & BIT(13)) >> 12) | \ |
| 185 | (((md) & BIT(19)) >> 19)) |
| 186 | struct cpg_pll_config { |
| 187 | unsigned int extal_div; |
| 188 | unsigned int pll1_mult; |
| 189 | unsigned int pll3_mult; |
| 190 | }; |
| 191 | |
| 192 | static const struct cpg_pll_config cpg_pll_configs[8] __initconst = { |
| 193 | { 1, 208, 106 }, { 1, 208, 88 }, { 1, 156, 80 }, { 1, 156, 66 }, |
| 194 | { 2, 240, 122 }, { 2, 240, 102 }, { 2, 208, 106 }, { 2, 208, 88 }, |
| 195 | }; |
| 196 | |
| 197 | /* SDHI divisors */ |
| 198 | static const struct clk_div_table cpg_sdh_div_table[] = { |
| 199 | { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, |
| 200 | { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 }, |
| 201 | { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 }, |
| 202 | }; |
| 203 | |
| 204 | static const struct clk_div_table cpg_sd01_div_table[] = { |
| 205 | { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 }, |
| 206 | { 10, 36 }, { 11, 48 }, { 12, 10 }, { 0, 0 }, |
| 207 | }; |
| 208 | |
| 209 | /* ----------------------------------------------------------------------------- |
| 210 | * Initialization |
| 211 | */ |
| 212 | |
| 213 | static u32 cpg_mode __initdata; |
| 214 | |
| 215 | static struct clk * __init |
| 216 | rcar_gen2_cpg_register_clock(struct device_node *np, struct rcar_gen2_cpg *cpg, |
| 217 | const struct cpg_pll_config *config, |
| 218 | const char *name) |
| 219 | { |
| 220 | const struct clk_div_table *table = NULL; |
Laurent Pinchart | 995a919 | 2014-01-07 17:47:52 +0100 | [diff] [blame] | 221 | const char *parent_name; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 222 | unsigned int shift; |
| 223 | unsigned int mult = 1; |
| 224 | unsigned int div = 1; |
| 225 | |
| 226 | if (!strcmp(name, "main")) { |
| 227 | parent_name = of_clk_get_parent_name(np, 0); |
| 228 | div = config->extal_div; |
| 229 | } else if (!strcmp(name, "pll0")) { |
| 230 | /* PLL0 is a configurable multiplier clock. Register it as a |
| 231 | * fixed factor clock for now as there's no generic multiplier |
| 232 | * clock implementation and we currently have no need to change |
| 233 | * the multiplier value. |
| 234 | */ |
| 235 | u32 value = clk_readl(cpg->reg + CPG_PLL0CR); |
Laurent Pinchart | 995a919 | 2014-01-07 17:47:52 +0100 | [diff] [blame] | 236 | parent_name = "main"; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 237 | mult = ((value >> 24) & ((1 << 7) - 1)) + 1; |
| 238 | } else if (!strcmp(name, "pll1")) { |
Laurent Pinchart | 995a919 | 2014-01-07 17:47:52 +0100 | [diff] [blame] | 239 | parent_name = "main"; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 240 | mult = config->pll1_mult / 2; |
| 241 | } else if (!strcmp(name, "pll3")) { |
Laurent Pinchart | 995a919 | 2014-01-07 17:47:52 +0100 | [diff] [blame] | 242 | parent_name = "main"; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 243 | mult = config->pll3_mult; |
| 244 | } else if (!strcmp(name, "lb")) { |
Ben Dooks | 365b018 | 2014-03-31 15:50:34 +0100 | [diff] [blame] | 245 | parent_name = "pll1"; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 246 | div = cpg_mode & BIT(18) ? 36 : 24; |
| 247 | } else if (!strcmp(name, "qspi")) { |
Laurent Pinchart | 995a919 | 2014-01-07 17:47:52 +0100 | [diff] [blame] | 248 | parent_name = "pll1_div2"; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 249 | div = (cpg_mode & (BIT(3) | BIT(2) | BIT(1))) == BIT(2) |
Laurent Pinchart | 8510e726 | 2014-01-07 17:47:53 +0100 | [diff] [blame] | 250 | ? 8 : 10; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 251 | } else if (!strcmp(name, "sdh")) { |
Ben Dooks | 365b018 | 2014-03-31 15:50:34 +0100 | [diff] [blame] | 252 | parent_name = "pll1"; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 253 | table = cpg_sdh_div_table; |
| 254 | shift = 8; |
| 255 | } else if (!strcmp(name, "sd0")) { |
Ben Dooks | 365b018 | 2014-03-31 15:50:34 +0100 | [diff] [blame] | 256 | parent_name = "pll1"; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 257 | table = cpg_sd01_div_table; |
| 258 | shift = 4; |
| 259 | } else if (!strcmp(name, "sd1")) { |
Ben Dooks | 365b018 | 2014-03-31 15:50:34 +0100 | [diff] [blame] | 260 | parent_name = "pll1"; |
Laurent Pinchart | 10cdfe9 | 2013-11-06 13:14:19 +0100 | [diff] [blame] | 261 | table = cpg_sd01_div_table; |
| 262 | shift = 0; |
| 263 | } else if (!strcmp(name, "z")) { |
| 264 | return cpg_z_clk_register(cpg); |
| 265 | } else { |
| 266 | return ERR_PTR(-EINVAL); |
| 267 | } |
| 268 | |
| 269 | if (!table) |
| 270 | return clk_register_fixed_factor(NULL, name, parent_name, 0, |
| 271 | mult, div); |
| 272 | else |
| 273 | return clk_register_divider_table(NULL, name, parent_name, 0, |
| 274 | cpg->reg + CPG_SDCKCR, shift, |
| 275 | 4, 0, table, &cpg->lock); |
| 276 | } |
| 277 | |
| 278 | static void __init rcar_gen2_cpg_clocks_init(struct device_node *np) |
| 279 | { |
| 280 | const struct cpg_pll_config *config; |
| 281 | struct rcar_gen2_cpg *cpg; |
| 282 | struct clk **clks; |
| 283 | unsigned int i; |
| 284 | int num_clks; |
| 285 | |
| 286 | num_clks = of_property_count_strings(np, "clock-output-names"); |
| 287 | if (num_clks < 0) { |
| 288 | pr_err("%s: failed to count clocks\n", __func__); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | cpg = kzalloc(sizeof(*cpg), GFP_KERNEL); |
| 293 | clks = kzalloc(num_clks * sizeof(*clks), GFP_KERNEL); |
| 294 | if (cpg == NULL || clks == NULL) { |
| 295 | /* We're leaking memory on purpose, there's no point in cleaning |
| 296 | * up as the system won't boot anyway. |
| 297 | */ |
| 298 | pr_err("%s: failed to allocate cpg\n", __func__); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | spin_lock_init(&cpg->lock); |
| 303 | |
| 304 | cpg->data.clks = clks; |
| 305 | cpg->data.clk_num = num_clks; |
| 306 | |
| 307 | cpg->reg = of_iomap(np, 0); |
| 308 | if (WARN_ON(cpg->reg == NULL)) |
| 309 | return; |
| 310 | |
| 311 | config = &cpg_pll_configs[CPG_PLL_CONFIG_INDEX(cpg_mode)]; |
| 312 | |
| 313 | for (i = 0; i < num_clks; ++i) { |
| 314 | const char *name; |
| 315 | struct clk *clk; |
| 316 | |
| 317 | of_property_read_string_index(np, "clock-output-names", i, |
| 318 | &name); |
| 319 | |
| 320 | clk = rcar_gen2_cpg_register_clock(np, cpg, config, name); |
| 321 | if (IS_ERR(clk)) |
| 322 | pr_err("%s: failed to register %s %s clock (%ld)\n", |
| 323 | __func__, np->name, name, PTR_ERR(clk)); |
| 324 | else |
| 325 | cpg->data.clks[i] = clk; |
| 326 | } |
| 327 | |
| 328 | of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data); |
| 329 | } |
| 330 | CLK_OF_DECLARE(rcar_gen2_cpg_clks, "renesas,rcar-gen2-cpg-clocks", |
| 331 | rcar_gen2_cpg_clocks_init); |
| 332 | |
| 333 | void __init rcar_gen2_clocks_init(u32 mode) |
| 334 | { |
| 335 | cpg_mode = mode; |
| 336 | |
| 337 | of_clk_init(NULL); |
| 338 | } |