2 * rcar_gen2 Core CPG Clocks
4 * Copyright (C) 2013 Ideas On Board SPRL
6 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
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.
13 #include <linux/clk-provider.h>
14 #include <linux/clk/renesas.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/math64.h>
19 #include <linux/of_address.h>
20 #include <linux/slab.h>
21 #include <linux/spinlock.h>
22 #include <linux/soc/renesas/rcar-rst.h>
24 struct rcar_gen2_cpg {
25 struct clk_onecell_data data;
30 #define CPG_FRQCRB 0x00000004
31 #define CPG_FRQCRB_KICK BIT(31)
32 #define CPG_SDCKCR 0x00000074
33 #define CPG_PLL0CR 0x000000d8
34 #define CPG_FRQCRC 0x000000e0
35 #define CPG_FRQCRC_ZFC_MASK (0x1f << 8)
36 #define CPG_FRQCRC_ZFC_SHIFT 8
37 #define CPG_ADSPCKCR 0x0000025c
38 #define CPG_RCANCKCR 0x00000270
40 /* -----------------------------------------------------------------------------
43 * Traits of this clock:
44 * prepare - clk_prepare only ensures that parents are prepared
45 * enable - clk_enable only ensures that parents are enabled
46 * rate - rate is adjustable. clk->rate = parent->rate * mult / 32
47 * parent - fixed parent. No clk_set_parent support
53 void __iomem *kick_reg;
56 #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
58 static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
59 unsigned long parent_rate)
61 struct cpg_z_clk *zclk = to_z_clk(hw);
65 val = (clk_readl(zclk->reg) & CPG_FRQCRC_ZFC_MASK)
66 >> CPG_FRQCRC_ZFC_SHIFT;
69 return div_u64((u64)parent_rate * mult, 32);
72 static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
73 unsigned long *parent_rate)
75 unsigned long prate = *parent_rate;
81 mult = div_u64((u64)rate * 32, prate);
82 mult = clamp(mult, 1U, 32U);
84 return *parent_rate / 32 * mult;
87 static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
88 unsigned long parent_rate)
90 struct cpg_z_clk *zclk = to_z_clk(hw);
95 mult = div_u64((u64)rate * 32, parent_rate);
96 mult = clamp(mult, 1U, 32U);
98 if (clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
101 val = clk_readl(zclk->reg);
102 val &= ~CPG_FRQCRC_ZFC_MASK;
103 val |= (32 - mult) << CPG_FRQCRC_ZFC_SHIFT;
104 clk_writel(val, zclk->reg);
107 * Set KICK bit in FRQCRB to update hardware setting and wait for
108 * clock change completion.
110 kick = clk_readl(zclk->kick_reg);
111 kick |= CPG_FRQCRB_KICK;
112 clk_writel(kick, zclk->kick_reg);
115 * Note: There is no HW information about the worst case latency.
117 * Using experimental measurements, it seems that no more than
118 * ~10 iterations are needed, independently of the CPU rate.
119 * Since this value might be dependent on external xtal rate, pll1
120 * rate or even the other emulation clocks rate, use 1000 as a
121 * "super" safe value.
123 for (i = 1000; i; i--) {
124 if (!(clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
133 static const struct clk_ops cpg_z_clk_ops = {
134 .recalc_rate = cpg_z_clk_recalc_rate,
135 .round_rate = cpg_z_clk_round_rate,
136 .set_rate = cpg_z_clk_set_rate,
139 static struct clk * __init cpg_z_clk_register(struct rcar_gen2_cpg *cpg)
141 static const char *parent_name = "pll0";
142 struct clk_init_data init;
143 struct cpg_z_clk *zclk;
146 zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
148 return ERR_PTR(-ENOMEM);
151 init.ops = &cpg_z_clk_ops;
153 init.parent_names = &parent_name;
154 init.num_parents = 1;
156 zclk->reg = cpg->reg + CPG_FRQCRC;
157 zclk->kick_reg = cpg->reg + CPG_FRQCRB;
158 zclk->hw.init = &init;
160 clk = clk_register(NULL, &zclk->hw);
167 static struct clk * __init cpg_rcan_clk_register(struct rcar_gen2_cpg *cpg,
168 struct device_node *np)
170 const char *parent_name = of_clk_get_parent_name(np, 1);
171 struct clk_fixed_factor *fixed;
172 struct clk_gate *gate;
175 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
177 return ERR_PTR(-ENOMEM);
182 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
185 return ERR_PTR(-ENOMEM);
188 gate->reg = cpg->reg + CPG_RCANCKCR;
190 gate->flags = CLK_GATE_SET_TO_DISABLE;
191 gate->lock = &cpg->lock;
193 clk = clk_register_composite(NULL, "rcan", &parent_name, 1, NULL, NULL,
194 &fixed->hw, &clk_fixed_factor_ops,
195 &gate->hw, &clk_gate_ops, 0);
205 static const struct clk_div_table cpg_adsp_div_table[] = {
206 { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 },
207 { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 },
208 { 10, 36 }, { 11, 48 }, { 0, 0 },
211 static struct clk * __init cpg_adsp_clk_register(struct rcar_gen2_cpg *cpg)
213 const char *parent_name = "pll1";
214 struct clk_divider *div;
215 struct clk_gate *gate;
218 div = kzalloc(sizeof(*div), GFP_KERNEL);
220 return ERR_PTR(-ENOMEM);
222 div->reg = cpg->reg + CPG_ADSPCKCR;
224 div->table = cpg_adsp_div_table;
225 div->lock = &cpg->lock;
227 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
230 return ERR_PTR(-ENOMEM);
233 gate->reg = cpg->reg + CPG_ADSPCKCR;
235 gate->flags = CLK_GATE_SET_TO_DISABLE;
236 gate->lock = &cpg->lock;
238 clk = clk_register_composite(NULL, "adsp", &parent_name, 1, NULL, NULL,
239 &div->hw, &clk_divider_ops,
240 &gate->hw, &clk_gate_ops, 0);
249 /* -----------------------------------------------------------------------------
254 * MD EXTAL PLL0 PLL1 PLL3
255 * 14 13 19 (MHz) *1 *1
256 *---------------------------------------------------
257 * 0 0 0 15 x 1 x172/2 x208/2 x106
258 * 0 0 1 15 x 1 x172/2 x208/2 x88
259 * 0 1 0 20 x 1 x130/2 x156/2 x80
260 * 0 1 1 20 x 1 x130/2 x156/2 x66
261 * 1 0 0 26 / 2 x200/2 x240/2 x122
262 * 1 0 1 26 / 2 x200/2 x240/2 x102
263 * 1 1 0 30 / 2 x172/2 x208/2 x106
264 * 1 1 1 30 / 2 x172/2 x208/2 x88
266 * *1 : Table 7.6 indicates VCO output (PLLx = VCO/2)
268 #define CPG_PLL_CONFIG_INDEX(md) ((((md) & BIT(14)) >> 12) | \
269 (((md) & BIT(13)) >> 12) | \
270 (((md) & BIT(19)) >> 19))
271 struct cpg_pll_config {
272 unsigned int extal_div;
273 unsigned int pll1_mult;
274 unsigned int pll3_mult;
277 static const struct cpg_pll_config cpg_pll_configs[8] __initconst = {
278 { 1, 208, 106 }, { 1, 208, 88 }, { 1, 156, 80 }, { 1, 156, 66 },
279 { 2, 240, 122 }, { 2, 240, 102 }, { 2, 208, 106 }, { 2, 208, 88 },
283 static const struct clk_div_table cpg_sdh_div_table[] = {
284 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
285 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
286 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
289 static const struct clk_div_table cpg_sd01_div_table[] = {
291 { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 },
292 { 10, 36 }, { 11, 48 }, { 12, 10 }, { 0, 0 },
295 /* -----------------------------------------------------------------------------
299 static u32 cpg_mode __initdata;
301 static struct clk * __init
302 rcar_gen2_cpg_register_clock(struct device_node *np, struct rcar_gen2_cpg *cpg,
303 const struct cpg_pll_config *config,
306 const struct clk_div_table *table = NULL;
307 const char *parent_name;
309 unsigned int mult = 1;
310 unsigned int div = 1;
312 if (!strcmp(name, "main")) {
313 parent_name = of_clk_get_parent_name(np, 0);
314 div = config->extal_div;
315 } else if (!strcmp(name, "pll0")) {
316 /* PLL0 is a configurable multiplier clock. Register it as a
317 * fixed factor clock for now as there's no generic multiplier
318 * clock implementation and we currently have no need to change
319 * the multiplier value.
321 u32 value = clk_readl(cpg->reg + CPG_PLL0CR);
322 parent_name = "main";
323 mult = ((value >> 24) & ((1 << 7) - 1)) + 1;
324 } else if (!strcmp(name, "pll1")) {
325 parent_name = "main";
326 mult = config->pll1_mult / 2;
327 } else if (!strcmp(name, "pll3")) {
328 parent_name = "main";
329 mult = config->pll3_mult;
330 } else if (!strcmp(name, "lb")) {
331 parent_name = "pll1";
332 div = cpg_mode & BIT(18) ? 36 : 24;
333 } else if (!strcmp(name, "qspi")) {
334 parent_name = "pll1_div2";
335 div = (cpg_mode & (BIT(3) | BIT(2) | BIT(1))) == BIT(2)
337 } else if (!strcmp(name, "sdh")) {
338 parent_name = "pll1";
339 table = cpg_sdh_div_table;
341 } else if (!strcmp(name, "sd0")) {
342 parent_name = "pll1";
343 table = cpg_sd01_div_table;
345 } else if (!strcmp(name, "sd1")) {
346 parent_name = "pll1";
347 table = cpg_sd01_div_table;
349 } else if (!strcmp(name, "z")) {
350 return cpg_z_clk_register(cpg);
351 } else if (!strcmp(name, "rcan")) {
352 return cpg_rcan_clk_register(cpg, np);
353 } else if (!strcmp(name, "adsp")) {
354 return cpg_adsp_clk_register(cpg);
356 return ERR_PTR(-EINVAL);
360 return clk_register_fixed_factor(NULL, name, parent_name, 0,
363 return clk_register_divider_table(NULL, name, parent_name, 0,
364 cpg->reg + CPG_SDCKCR, shift,
365 4, 0, table, &cpg->lock);
369 * Reset register definitions.
371 #define MODEMR 0xe6160060
373 static u32 __init rcar_gen2_read_mode_pins(void)
375 void __iomem *modemr = ioremap_nocache(MODEMR, 4);
379 mode = ioread32(modemr);
385 static void __init rcar_gen2_cpg_clocks_init(struct device_node *np)
387 const struct cpg_pll_config *config;
388 struct rcar_gen2_cpg *cpg;
393 if (rcar_rst_read_mode_pins(&cpg_mode)) {
394 /* Backward-compatibility with old DT */
395 pr_warn("%s: failed to obtain mode pins from RST\n",
397 cpg_mode = rcar_gen2_read_mode_pins();
400 num_clks = of_property_count_strings(np, "clock-output-names");
402 pr_err("%s: failed to count clocks\n", __func__);
406 cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
407 clks = kzalloc(num_clks * sizeof(*clks), GFP_KERNEL);
408 if (cpg == NULL || clks == NULL) {
409 /* We're leaking memory on purpose, there's no point in cleaning
410 * up as the system won't boot anyway.
412 pr_err("%s: failed to allocate cpg\n", __func__);
416 spin_lock_init(&cpg->lock);
418 cpg->data.clks = clks;
419 cpg->data.clk_num = num_clks;
421 cpg->reg = of_iomap(np, 0);
422 if (WARN_ON(cpg->reg == NULL))
425 config = &cpg_pll_configs[CPG_PLL_CONFIG_INDEX(cpg_mode)];
427 for (i = 0; i < num_clks; ++i) {
431 of_property_read_string_index(np, "clock-output-names", i,
434 clk = rcar_gen2_cpg_register_clock(np, cpg, config, name);
436 pr_err("%s: failed to register %s %s clock (%ld)\n",
437 __func__, np->name, name, PTR_ERR(clk));
439 cpg->data.clks[i] = clk;
442 of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
444 cpg_mstp_add_clk_domain(np);
446 CLK_OF_DECLARE(rcar_gen2_cpg_clks, "renesas,rcar-gen2-cpg-clocks",
447 rcar_gen2_cpg_clocks_init);