1 // SPDX-License-Identifier: GPL-2.0
3 * rcar_gen2 Core CPG Clocks
5 * Copyright (C) 2013 Ideas On Board SPRL
10 #include <linux/clk-provider.h>
11 #include <linux/clk/renesas.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/math64.h>
16 #include <linux/of_address.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/soc/renesas/rcar-rst.h>
21 struct rcar_gen2_cpg {
22 struct clk_onecell_data data;
27 #define CPG_FRQCRB 0x00000004
28 #define CPG_FRQCRB_KICK BIT(31)
29 #define CPG_SDCKCR 0x00000074
30 #define CPG_PLL0CR 0x000000d8
31 #define CPG_FRQCRC 0x000000e0
32 #define CPG_FRQCRC_ZFC_MASK (0x1f << 8)
33 #define CPG_FRQCRC_ZFC_SHIFT 8
34 #define CPG_ADSPCKCR 0x0000025c
35 #define CPG_RCANCKCR 0x00000270
37 /* -----------------------------------------------------------------------------
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
50 void __iomem *kick_reg;
53 #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
55 static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
56 unsigned long parent_rate)
58 struct cpg_z_clk *zclk = to_z_clk(hw);
62 val = (readl(zclk->reg) & CPG_FRQCRC_ZFC_MASK) >> CPG_FRQCRC_ZFC_SHIFT;
65 return div_u64((u64)parent_rate * mult, 32);
68 static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
69 unsigned long *parent_rate)
71 unsigned long prate = *parent_rate;
77 mult = div_u64((u64)rate * 32, prate);
78 mult = clamp(mult, 1U, 32U);
80 return *parent_rate / 32 * mult;
83 static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
84 unsigned long parent_rate)
86 struct cpg_z_clk *zclk = to_z_clk(hw);
91 mult = div_u64((u64)rate * 32, parent_rate);
92 mult = clamp(mult, 1U, 32U);
94 if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
97 val = readl(zclk->reg);
98 val &= ~CPG_FRQCRC_ZFC_MASK;
99 val |= (32 - mult) << CPG_FRQCRC_ZFC_SHIFT;
100 writel(val, zclk->reg);
103 * Set KICK bit in FRQCRB to update hardware setting and wait for
104 * clock change completion.
106 kick = readl(zclk->kick_reg);
107 kick |= CPG_FRQCRB_KICK;
108 writel(kick, zclk->kick_reg);
111 * Note: There is no HW information about the worst case latency.
113 * Using experimental measurements, it seems that no more than
114 * ~10 iterations are needed, independently of the CPU rate.
115 * Since this value might be dependent on external xtal rate, pll1
116 * rate or even the other emulation clocks rate, use 1000 as a
117 * "super" safe value.
119 for (i = 1000; i; i--) {
120 if (!(readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
129 static const struct clk_ops cpg_z_clk_ops = {
130 .recalc_rate = cpg_z_clk_recalc_rate,
131 .round_rate = cpg_z_clk_round_rate,
132 .set_rate = cpg_z_clk_set_rate,
135 static struct clk * __init cpg_z_clk_register(struct rcar_gen2_cpg *cpg)
137 static const char *parent_name = "pll0";
138 struct clk_init_data init;
139 struct cpg_z_clk *zclk;
142 zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
144 return ERR_PTR(-ENOMEM);
147 init.ops = &cpg_z_clk_ops;
149 init.parent_names = &parent_name;
150 init.num_parents = 1;
152 zclk->reg = cpg->reg + CPG_FRQCRC;
153 zclk->kick_reg = cpg->reg + CPG_FRQCRB;
154 zclk->hw.init = &init;
156 clk = clk_register(NULL, &zclk->hw);
163 static struct clk * __init cpg_rcan_clk_register(struct rcar_gen2_cpg *cpg,
164 struct device_node *np)
166 const char *parent_name = of_clk_get_parent_name(np, 1);
167 struct clk_fixed_factor *fixed;
168 struct clk_gate *gate;
171 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
173 return ERR_PTR(-ENOMEM);
178 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
181 return ERR_PTR(-ENOMEM);
184 gate->reg = cpg->reg + CPG_RCANCKCR;
186 gate->flags = CLK_GATE_SET_TO_DISABLE;
187 gate->lock = &cpg->lock;
189 clk = clk_register_composite(NULL, "rcan", &parent_name, 1, NULL, NULL,
190 &fixed->hw, &clk_fixed_factor_ops,
191 &gate->hw, &clk_gate_ops, 0);
201 static const struct clk_div_table cpg_adsp_div_table[] = {
202 { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 },
203 { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 },
204 { 10, 36 }, { 11, 48 }, { 0, 0 },
207 static struct clk * __init cpg_adsp_clk_register(struct rcar_gen2_cpg *cpg)
209 const char *parent_name = "pll1";
210 struct clk_divider *div;
211 struct clk_gate *gate;
214 div = kzalloc(sizeof(*div), GFP_KERNEL);
216 return ERR_PTR(-ENOMEM);
218 div->reg = cpg->reg + CPG_ADSPCKCR;
220 div->table = cpg_adsp_div_table;
221 div->lock = &cpg->lock;
223 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
226 return ERR_PTR(-ENOMEM);
229 gate->reg = cpg->reg + CPG_ADSPCKCR;
231 gate->flags = CLK_GATE_SET_TO_DISABLE;
232 gate->lock = &cpg->lock;
234 clk = clk_register_composite(NULL, "adsp", &parent_name, 1, NULL, NULL,
235 &div->hw, &clk_divider_ops,
236 &gate->hw, &clk_gate_ops, 0);
245 /* -----------------------------------------------------------------------------
250 * MD EXTAL PLL0 PLL1 PLL3
251 * 14 13 19 (MHz) *1 *1
252 *---------------------------------------------------
253 * 0 0 0 15 x 1 x172/2 x208/2 x106
254 * 0 0 1 15 x 1 x172/2 x208/2 x88
255 * 0 1 0 20 x 1 x130/2 x156/2 x80
256 * 0 1 1 20 x 1 x130/2 x156/2 x66
257 * 1 0 0 26 / 2 x200/2 x240/2 x122
258 * 1 0 1 26 / 2 x200/2 x240/2 x102
259 * 1 1 0 30 / 2 x172/2 x208/2 x106
260 * 1 1 1 30 / 2 x172/2 x208/2 x88
262 * *1 : Table 7.6 indicates VCO output (PLLx = VCO/2)
264 #define CPG_PLL_CONFIG_INDEX(md) ((((md) & BIT(14)) >> 12) | \
265 (((md) & BIT(13)) >> 12) | \
266 (((md) & BIT(19)) >> 19))
267 struct cpg_pll_config {
268 unsigned int extal_div;
269 unsigned int pll1_mult;
270 unsigned int pll3_mult;
271 unsigned int pll0_mult; /* For R-Car V2H and E2 only */
274 static const struct cpg_pll_config cpg_pll_configs[8] __initconst = {
275 { 1, 208, 106, 200 }, { 1, 208, 88, 200 },
276 { 1, 156, 80, 150 }, { 1, 156, 66, 150 },
277 { 2, 240, 122, 230 }, { 2, 240, 102, 230 },
278 { 2, 208, 106, 200 }, { 2, 208, 88, 200 },
282 static const struct clk_div_table cpg_sdh_div_table[] = {
283 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 },
284 { 4, 8 }, { 5, 12 }, { 6, 16 }, { 7, 18 },
285 { 8, 24 }, { 10, 36 }, { 11, 48 }, { 0, 0 },
288 static const struct clk_div_table cpg_sd01_div_table[] = {
290 { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 },
291 { 10, 36 }, { 11, 48 }, { 12, 10 }, { 0, 0 },
294 /* -----------------------------------------------------------------------------
298 static u32 cpg_mode __initdata;
300 static const char * const pll0_mult_match[] = {
301 "renesas,r8a7792-cpg-clocks",
302 "renesas,r8a7794-cpg-clocks",
306 static struct clk * __init
307 rcar_gen2_cpg_register_clock(struct device_node *np, struct rcar_gen2_cpg *cpg,
308 const struct cpg_pll_config *config,
311 const struct clk_div_table *table = NULL;
312 const char *parent_name;
314 unsigned int mult = 1;
315 unsigned int div = 1;
317 if (!strcmp(name, "main")) {
318 parent_name = of_clk_get_parent_name(np, 0);
319 div = config->extal_div;
320 } else if (!strcmp(name, "pll0")) {
321 /* PLL0 is a configurable multiplier clock. Register it as a
322 * fixed factor clock for now as there's no generic multiplier
323 * clock implementation and we currently have no need to change
324 * the multiplier value.
326 if (of_device_compatible_match(np, pll0_mult_match)) {
327 /* R-Car V2H and E2 do not have PLL0CR */
328 mult = config->pll0_mult;
331 u32 value = readl(cpg->reg + CPG_PLL0CR);
332 mult = ((value >> 24) & ((1 << 7) - 1)) + 1;
334 parent_name = "main";
335 } else if (!strcmp(name, "pll1")) {
336 parent_name = "main";
337 mult = config->pll1_mult / 2;
338 } else if (!strcmp(name, "pll3")) {
339 parent_name = "main";
340 mult = config->pll3_mult;
341 } else if (!strcmp(name, "lb")) {
342 parent_name = "pll1";
343 div = cpg_mode & BIT(18) ? 36 : 24;
344 } else if (!strcmp(name, "qspi")) {
345 parent_name = "pll1_div2";
346 div = (cpg_mode & (BIT(3) | BIT(2) | BIT(1))) == BIT(2)
348 } else if (!strcmp(name, "sdh")) {
349 parent_name = "pll1";
350 table = cpg_sdh_div_table;
352 } else if (!strcmp(name, "sd0")) {
353 parent_name = "pll1";
354 table = cpg_sd01_div_table;
356 } else if (!strcmp(name, "sd1")) {
357 parent_name = "pll1";
358 table = cpg_sd01_div_table;
360 } else if (!strcmp(name, "z")) {
361 return cpg_z_clk_register(cpg);
362 } else if (!strcmp(name, "rcan")) {
363 return cpg_rcan_clk_register(cpg, np);
364 } else if (!strcmp(name, "adsp")) {
365 return cpg_adsp_clk_register(cpg);
367 return ERR_PTR(-EINVAL);
371 return clk_register_fixed_factor(NULL, name, parent_name, 0,
374 return clk_register_divider_table(NULL, name, parent_name, 0,
375 cpg->reg + CPG_SDCKCR, shift,
376 4, 0, table, &cpg->lock);
380 * Reset register definitions.
382 #define MODEMR 0xe6160060
384 static u32 __init rcar_gen2_read_mode_pins(void)
386 void __iomem *modemr = ioremap_nocache(MODEMR, 4);
390 mode = ioread32(modemr);
396 static void __init rcar_gen2_cpg_clocks_init(struct device_node *np)
398 const struct cpg_pll_config *config;
399 struct rcar_gen2_cpg *cpg;
404 if (rcar_rst_read_mode_pins(&cpg_mode)) {
405 /* Backward-compatibility with old DT */
406 pr_warn("%pOF: failed to obtain mode pins from RST\n", np);
407 cpg_mode = rcar_gen2_read_mode_pins();
410 num_clks = of_property_count_strings(np, "clock-output-names");
412 pr_err("%s: failed to count clocks\n", __func__);
416 cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
417 clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);
418 if (cpg == NULL || clks == NULL) {
419 /* We're leaking memory on purpose, there's no point in cleaning
420 * up as the system won't boot anyway.
425 spin_lock_init(&cpg->lock);
427 cpg->data.clks = clks;
428 cpg->data.clk_num = num_clks;
430 cpg->reg = of_iomap(np, 0);
431 if (WARN_ON(cpg->reg == NULL))
434 config = &cpg_pll_configs[CPG_PLL_CONFIG_INDEX(cpg_mode)];
436 for (i = 0; i < num_clks; ++i) {
440 of_property_read_string_index(np, "clock-output-names", i,
443 clk = rcar_gen2_cpg_register_clock(np, cpg, config, name);
445 pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
446 __func__, np, name, PTR_ERR(clk));
448 cpg->data.clks[i] = clk;
451 of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
453 cpg_mstp_add_clk_domain(np);
455 CLK_OF_DECLARE(rcar_gen2_cpg_clks, "renesas,rcar-gen2-cpg-clocks",
456 rcar_gen2_cpg_clocks_init);