1 // SPDX-License-Identifier: GPL-2.0
3 * rcar_gen2 Core CPG Clocks
5 * Copyright (C) 2013 Ideas On Board SPRL
7 * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
10 #include <linux/clk-provider.h>
11 #include <linux/clk/renesas.h>
12 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/math64.h>
17 #include <linux/of_address.h>
18 #include <linux/slab.h>
19 #include <linux/spinlock.h>
20 #include <linux/soc/renesas/rcar-rst.h>
22 struct rcar_gen2_cpg {
23 struct clk_onecell_data data;
28 #define CPG_FRQCRB 0x00000004
29 #define CPG_FRQCRB_KICK BIT(31)
30 #define CPG_SDCKCR 0x00000074
31 #define CPG_PLL0CR 0x000000d8
32 #define CPG_FRQCRC 0x000000e0
33 #define CPG_FRQCRC_ZFC_MASK (0x1f << 8)
34 #define CPG_FRQCRC_ZFC_SHIFT 8
35 #define CPG_ADSPCKCR 0x0000025c
36 #define CPG_RCANCKCR 0x00000270
38 /* -----------------------------------------------------------------------------
41 * Traits of this clock:
42 * prepare - clk_prepare only ensures that parents are prepared
43 * enable - clk_enable only ensures that parents are enabled
44 * rate - rate is adjustable. clk->rate = parent->rate * mult / 32
45 * parent - fixed parent. No clk_set_parent support
51 void __iomem *kick_reg;
54 #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
56 static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
57 unsigned long parent_rate)
59 struct cpg_z_clk *zclk = to_z_clk(hw);
63 val = (readl(zclk->reg) & CPG_FRQCRC_ZFC_MASK) >> CPG_FRQCRC_ZFC_SHIFT;
66 return div_u64((u64)parent_rate * mult, 32);
69 static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
70 unsigned long *parent_rate)
72 unsigned long prate = *parent_rate;
78 mult = div_u64((u64)rate * 32, prate);
79 mult = clamp(mult, 1U, 32U);
81 return *parent_rate / 32 * mult;
84 static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
85 unsigned long parent_rate)
87 struct cpg_z_clk *zclk = to_z_clk(hw);
92 mult = div_u64((u64)rate * 32, parent_rate);
93 mult = clamp(mult, 1U, 32U);
95 if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
98 val = readl(zclk->reg);
99 val &= ~CPG_FRQCRC_ZFC_MASK;
100 val |= (32 - mult) << CPG_FRQCRC_ZFC_SHIFT;
101 writel(val, zclk->reg);
104 * Set KICK bit in FRQCRB to update hardware setting and wait for
105 * clock change completion.
107 kick = readl(zclk->kick_reg);
108 kick |= CPG_FRQCRB_KICK;
109 writel(kick, zclk->kick_reg);
112 * Note: There is no HW information about the worst case latency.
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 dependent on external xtal rate, pll1
117 * rate or even the other emulation clocks rate, use 1000 as a
118 * "super" safe value.
120 for (i = 1000; i; i--) {
121 if (!(readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
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,
136 static struct clk * __init cpg_z_clk_register(struct rcar_gen2_cpg *cpg)
138 static const char *parent_name = "pll0";
139 struct clk_init_data init;
140 struct cpg_z_clk *zclk;
143 zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
145 return ERR_PTR(-ENOMEM);
148 init.ops = &cpg_z_clk_ops;
150 init.parent_names = &parent_name;
151 init.num_parents = 1;
153 zclk->reg = cpg->reg + CPG_FRQCRC;
154 zclk->kick_reg = cpg->reg + CPG_FRQCRB;
155 zclk->hw.init = &init;
157 clk = clk_register(NULL, &zclk->hw);
164 static struct clk * __init cpg_rcan_clk_register(struct rcar_gen2_cpg *cpg,
165 struct device_node *np)
167 const char *parent_name = of_clk_get_parent_name(np, 1);
168 struct clk_fixed_factor *fixed;
169 struct clk_gate *gate;
172 fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
174 return ERR_PTR(-ENOMEM);
179 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
182 return ERR_PTR(-ENOMEM);
185 gate->reg = cpg->reg + CPG_RCANCKCR;
187 gate->flags = CLK_GATE_SET_TO_DISABLE;
188 gate->lock = &cpg->lock;
190 clk = clk_register_composite(NULL, "rcan", &parent_name, 1, NULL, NULL,
191 &fixed->hw, &clk_fixed_factor_ops,
192 &gate->hw, &clk_gate_ops, 0);
202 static const struct clk_div_table cpg_adsp_div_table[] = {
203 { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 },
204 { 5, 12 }, { 6, 16 }, { 7, 18 }, { 8, 24 },
205 { 10, 36 }, { 11, 48 }, { 0, 0 },
208 static struct clk * __init cpg_adsp_clk_register(struct rcar_gen2_cpg *cpg)
210 const char *parent_name = "pll1";
211 struct clk_divider *div;
212 struct clk_gate *gate;
215 div = kzalloc(sizeof(*div), GFP_KERNEL);
217 return ERR_PTR(-ENOMEM);
219 div->reg = cpg->reg + CPG_ADSPCKCR;
221 div->table = cpg_adsp_div_table;
222 div->lock = &cpg->lock;
224 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
227 return ERR_PTR(-ENOMEM);
230 gate->reg = cpg->reg + CPG_ADSPCKCR;
232 gate->flags = CLK_GATE_SET_TO_DISABLE;
233 gate->lock = &cpg->lock;
235 clk = clk_register_composite(NULL, "adsp", &parent_name, 1, NULL, NULL,
236 &div->hw, &clk_divider_ops,
237 &gate->hw, &clk_gate_ops, 0);
246 /* -----------------------------------------------------------------------------
251 * MD EXTAL PLL0 PLL1 PLL3
252 * 14 13 19 (MHz) *1 *1
253 *---------------------------------------------------
254 * 0 0 0 15 x 1 x172/2 x208/2 x106
255 * 0 0 1 15 x 1 x172/2 x208/2 x88
256 * 0 1 0 20 x 1 x130/2 x156/2 x80
257 * 0 1 1 20 x 1 x130/2 x156/2 x66
258 * 1 0 0 26 / 2 x200/2 x240/2 x122
259 * 1 0 1 26 / 2 x200/2 x240/2 x102
260 * 1 1 0 30 / 2 x172/2 x208/2 x106
261 * 1 1 1 30 / 2 x172/2 x208/2 x88
263 * *1 : Table 7.6 indicates VCO output (PLLx = VCO/2)
265 #define CPG_PLL_CONFIG_INDEX(md) ((((md) & BIT(14)) >> 12) | \
266 (((md) & BIT(13)) >> 12) | \
267 (((md) & BIT(19)) >> 19))
268 struct cpg_pll_config {
269 unsigned int extal_div;
270 unsigned int pll1_mult;
271 unsigned int pll3_mult;
272 unsigned int pll0_mult; /* For R-Car V2H and E2 only */
275 static const struct cpg_pll_config cpg_pll_configs[8] __initconst = {
276 { 1, 208, 106, 200 }, { 1, 208, 88, 200 },
277 { 1, 156, 80, 150 }, { 1, 156, 66, 150 },
278 { 2, 240, 122, 230 }, { 2, 240, 102, 230 },
279 { 2, 208, 106, 200 }, { 2, 208, 88, 200 },
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 const char * const pll0_mult_match[] = {
302 "renesas,r8a7792-cpg-clocks",
303 "renesas,r8a7794-cpg-clocks",
307 static struct clk * __init
308 rcar_gen2_cpg_register_clock(struct device_node *np, struct rcar_gen2_cpg *cpg,
309 const struct cpg_pll_config *config,
312 const struct clk_div_table *table = NULL;
313 const char *parent_name;
315 unsigned int mult = 1;
316 unsigned int div = 1;
318 if (!strcmp(name, "main")) {
319 parent_name = of_clk_get_parent_name(np, 0);
320 div = config->extal_div;
321 } else if (!strcmp(name, "pll0")) {
322 /* PLL0 is a configurable multiplier clock. Register it as a
323 * fixed factor clock for now as there's no generic multiplier
324 * clock implementation and we currently have no need to change
325 * the multiplier value.
327 if (of_device_compatible_match(np, pll0_mult_match)) {
328 /* R-Car V2H and E2 do not have PLL0CR */
329 mult = config->pll0_mult;
332 u32 value = readl(cpg->reg + CPG_PLL0CR);
333 mult = ((value >> 24) & ((1 << 7) - 1)) + 1;
335 parent_name = "main";
336 } else if (!strcmp(name, "pll1")) {
337 parent_name = "main";
338 mult = config->pll1_mult / 2;
339 } else if (!strcmp(name, "pll3")) {
340 parent_name = "main";
341 mult = config->pll3_mult;
342 } else if (!strcmp(name, "lb")) {
343 parent_name = "pll1";
344 div = cpg_mode & BIT(18) ? 36 : 24;
345 } else if (!strcmp(name, "qspi")) {
346 parent_name = "pll1_div2";
347 div = (cpg_mode & (BIT(3) | BIT(2) | BIT(1))) == BIT(2)
349 } else if (!strcmp(name, "sdh")) {
350 parent_name = "pll1";
351 table = cpg_sdh_div_table;
353 } else if (!strcmp(name, "sd0")) {
354 parent_name = "pll1";
355 table = cpg_sd01_div_table;
357 } else if (!strcmp(name, "sd1")) {
358 parent_name = "pll1";
359 table = cpg_sd01_div_table;
361 } else if (!strcmp(name, "z")) {
362 return cpg_z_clk_register(cpg);
363 } else if (!strcmp(name, "rcan")) {
364 return cpg_rcan_clk_register(cpg, np);
365 } else if (!strcmp(name, "adsp")) {
366 return cpg_adsp_clk_register(cpg);
368 return ERR_PTR(-EINVAL);
372 return clk_register_fixed_factor(NULL, name, parent_name, 0,
375 return clk_register_divider_table(NULL, name, parent_name, 0,
376 cpg->reg + CPG_SDCKCR, shift,
377 4, 0, table, &cpg->lock);
381 * Reset register definitions.
383 #define MODEMR 0xe6160060
385 static u32 __init rcar_gen2_read_mode_pins(void)
387 void __iomem *modemr = ioremap_nocache(MODEMR, 4);
391 mode = ioread32(modemr);
397 static void __init rcar_gen2_cpg_clocks_init(struct device_node *np)
399 const struct cpg_pll_config *config;
400 struct rcar_gen2_cpg *cpg;
405 if (rcar_rst_read_mode_pins(&cpg_mode)) {
406 /* Backward-compatibility with old DT */
407 pr_warn("%pOF: failed to obtain mode pins from RST\n", np);
408 cpg_mode = rcar_gen2_read_mode_pins();
411 num_clks = of_property_count_strings(np, "clock-output-names");
413 pr_err("%s: failed to count clocks\n", __func__);
417 cpg = kzalloc(sizeof(*cpg), GFP_KERNEL);
418 clks = kcalloc(num_clks, sizeof(*clks), GFP_KERNEL);
419 if (cpg == NULL || clks == NULL) {
420 /* We're leaking memory on purpose, there's no point in cleaning
421 * up as the system won't boot anyway.
426 spin_lock_init(&cpg->lock);
428 cpg->data.clks = clks;
429 cpg->data.clk_num = num_clks;
431 cpg->reg = of_iomap(np, 0);
432 if (WARN_ON(cpg->reg == NULL))
435 config = &cpg_pll_configs[CPG_PLL_CONFIG_INDEX(cpg_mode)];
437 for (i = 0; i < num_clks; ++i) {
441 of_property_read_string_index(np, "clock-output-names", i,
444 clk = rcar_gen2_cpg_register_clock(np, cpg, config, name);
446 pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
447 __func__, np, name, PTR_ERR(clk));
449 cpg->data.clks[i] = clk;
452 of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
454 cpg_mstp_add_clk_domain(np);
456 CLK_OF_DECLARE(rcar_gen2_cpg_clks, "renesas,rcar-gen2-cpg-clocks",
457 rcar_gen2_cpg_clocks_init);