2 * R-Car Gen3 Clock Pulse Generator
4 * Copyright (C) 2015-2016 Glider bvba
6 * Based on clk-rcar-gen3.c
8 * Copyright (C) 2015 Renesas Electronics Corp.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
15 #include <linux/bug.h>
16 #include <linux/bitfield.h>
17 #include <linux/clk.h>
18 #include <linux/clk-provider.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/sys_soc.h>
27 #include "renesas-cpg-mssr.h"
28 #include "rcar-gen3-cpg.h"
30 #define CPG_PLL0CR 0x00d8
31 #define CPG_PLL2CR 0x002c
32 #define CPG_PLL4CR 0x01f4
34 struct cpg_simple_notifier {
35 struct notifier_block nb;
40 static int cpg_simple_notifier_call(struct notifier_block *nb,
41 unsigned long action, void *data)
43 struct cpg_simple_notifier *csn =
44 container_of(nb, struct cpg_simple_notifier, nb);
47 case PM_EVENT_SUSPEND:
48 csn->saved = readl(csn->reg);
52 writel(csn->saved, csn->reg);
58 static void cpg_simple_notifier_register(struct raw_notifier_head *notifiers,
59 struct cpg_simple_notifier *csn)
61 csn->nb.notifier_call = cpg_simple_notifier_call;
62 raw_notifier_chain_register(notifiers, &csn->nb);
68 * Traits of this clock:
69 * prepare - clk_prepare only ensures that parents are prepared
70 * enable - clk_enable only ensures that parents are enabled
71 * rate - rate is adjustable. clk->rate = (parent->rate * mult / 32 ) / 2
72 * parent - fixed parent. No clk_set_parent support
74 #define CPG_FRQCRB 0x00000004
75 #define CPG_FRQCRB_KICK BIT(31)
76 #define CPG_FRQCRC 0x000000e0
77 #define CPG_FRQCRC_ZFC_MASK GENMASK(12, 8)
78 #define CPG_FRQCRC_Z2FC_MASK GENMASK(4, 0)
83 void __iomem *kick_reg;
87 #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
89 static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
90 unsigned long parent_rate)
92 struct cpg_z_clk *zclk = to_z_clk(hw);
96 val = readl(zclk->reg) & zclk->mask;
97 mult = 32 - (val >> __ffs(zclk->mask));
99 /* Factor of 2 is for fixed divider */
100 return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, 32 * 2);
103 static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
104 unsigned long *parent_rate)
106 /* Factor of 2 is for fixed divider */
107 unsigned long prate = *parent_rate / 2;
110 mult = div_u64(rate * 32ULL, prate);
111 mult = clamp(mult, 1U, 32U);
113 return (u64)prate * mult / 32;
116 static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
117 unsigned long parent_rate)
119 struct cpg_z_clk *zclk = to_z_clk(hw);
124 /* Factor of 2 is for fixed divider */
125 mult = DIV_ROUND_CLOSEST_ULL(rate * 32ULL * 2, parent_rate);
126 mult = clamp(mult, 1U, 32U);
128 if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
131 val = readl(zclk->reg) & ~zclk->mask;
132 val |= ((32 - mult) << __ffs(zclk->mask)) & zclk->mask;
133 writel(val, zclk->reg);
136 * Set KICK bit in FRQCRB to update hardware setting and wait for
137 * clock change completion.
139 kick = readl(zclk->kick_reg);
140 kick |= CPG_FRQCRB_KICK;
141 writel(kick, zclk->kick_reg);
144 * Note: There is no HW information about the worst case latency.
146 * Using experimental measurements, it seems that no more than
147 * ~10 iterations are needed, independently of the CPU rate.
148 * Since this value might be dependent of external xtal rate, pll1
149 * rate or even the other emulation clocks rate, use 1000 as a
150 * "super" safe value.
152 for (i = 1000; i; i--) {
153 if (!(readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
162 static const struct clk_ops cpg_z_clk_ops = {
163 .recalc_rate = cpg_z_clk_recalc_rate,
164 .round_rate = cpg_z_clk_round_rate,
165 .set_rate = cpg_z_clk_set_rate,
168 static struct clk * __init cpg_z_clk_register(const char *name,
169 const char *parent_name,
173 struct clk_init_data init;
174 struct cpg_z_clk *zclk;
177 zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
179 return ERR_PTR(-ENOMEM);
182 init.ops = &cpg_z_clk_ops;
184 init.parent_names = &parent_name;
185 init.num_parents = 1;
187 zclk->reg = reg + CPG_FRQCRC;
188 zclk->kick_reg = reg + CPG_FRQCRB;
189 zclk->hw.init = &init;
192 clk = clk_register(NULL, &zclk->hw);
202 #define CPG_SD_STP_HCK BIT(9)
203 #define CPG_SD_STP_CK BIT(8)
205 #define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK)
206 #define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0)
208 #define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \
210 .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \
211 ((stp_ck) ? CPG_SD_STP_CK : 0) | \
212 ((sd_srcfc) << 2) | \
217 struct sd_div_table {
224 const struct sd_div_table *div_table;
225 struct cpg_simple_notifier csn;
226 unsigned int div_num;
227 unsigned int div_min;
228 unsigned int div_max;
229 unsigned int cur_div_idx;
234 * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc
235 *-------------------------------------------------------------------
240 * 1 0 4 (16) 1 (4) 64
245 * 1 0 4 (16) 0 (2) 32
247 static const struct sd_div_table cpg_sd_div_table[] = {
248 /* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */
249 CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4),
250 CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8),
251 CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16),
252 CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32),
253 CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64),
254 CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2),
255 CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4),
256 CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8),
257 CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16),
258 CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32),
261 #define to_sd_clock(_hw) container_of(_hw, struct sd_clock, hw)
263 static int cpg_sd_clock_enable(struct clk_hw *hw)
265 struct sd_clock *clock = to_sd_clock(hw);
266 u32 val = readl(clock->csn.reg);
268 val &= ~(CPG_SD_STP_MASK);
269 val |= clock->div_table[clock->cur_div_idx].val & CPG_SD_STP_MASK;
271 writel(val, clock->csn.reg);
276 static void cpg_sd_clock_disable(struct clk_hw *hw)
278 struct sd_clock *clock = to_sd_clock(hw);
280 writel(readl(clock->csn.reg) | CPG_SD_STP_MASK, clock->csn.reg);
283 static int cpg_sd_clock_is_enabled(struct clk_hw *hw)
285 struct sd_clock *clock = to_sd_clock(hw);
287 return !(readl(clock->csn.reg) & CPG_SD_STP_MASK);
290 static unsigned long cpg_sd_clock_recalc_rate(struct clk_hw *hw,
291 unsigned long parent_rate)
293 struct sd_clock *clock = to_sd_clock(hw);
295 return DIV_ROUND_CLOSEST(parent_rate,
296 clock->div_table[clock->cur_div_idx].div);
299 static unsigned int cpg_sd_clock_calc_div(struct sd_clock *clock,
301 unsigned long parent_rate)
308 div = DIV_ROUND_CLOSEST(parent_rate, rate);
310 return clamp_t(unsigned int, div, clock->div_min, clock->div_max);
313 static long cpg_sd_clock_round_rate(struct clk_hw *hw, unsigned long rate,
314 unsigned long *parent_rate)
316 struct sd_clock *clock = to_sd_clock(hw);
317 unsigned int div = cpg_sd_clock_calc_div(clock, rate, *parent_rate);
319 return DIV_ROUND_CLOSEST(*parent_rate, div);
322 static int cpg_sd_clock_set_rate(struct clk_hw *hw, unsigned long rate,
323 unsigned long parent_rate)
325 struct sd_clock *clock = to_sd_clock(hw);
326 unsigned int div = cpg_sd_clock_calc_div(clock, rate, parent_rate);
330 for (i = 0; i < clock->div_num; i++)
331 if (div == clock->div_table[i].div)
334 if (i >= clock->div_num)
337 clock->cur_div_idx = i;
339 val = readl(clock->csn.reg);
340 val &= ~(CPG_SD_STP_MASK | CPG_SD_FC_MASK);
341 val |= clock->div_table[i].val & (CPG_SD_STP_MASK | CPG_SD_FC_MASK);
342 writel(val, clock->csn.reg);
347 static const struct clk_ops cpg_sd_clock_ops = {
348 .enable = cpg_sd_clock_enable,
349 .disable = cpg_sd_clock_disable,
350 .is_enabled = cpg_sd_clock_is_enabled,
351 .recalc_rate = cpg_sd_clock_recalc_rate,
352 .round_rate = cpg_sd_clock_round_rate,
353 .set_rate = cpg_sd_clock_set_rate,
356 static struct clk * __init cpg_sd_clk_register(const struct cpg_core_clk *core,
357 void __iomem *base, const char *parent_name,
358 struct raw_notifier_head *notifiers)
360 struct clk_init_data init;
361 struct sd_clock *clock;
366 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
368 return ERR_PTR(-ENOMEM);
370 init.name = core->name;
371 init.ops = &cpg_sd_clock_ops;
372 init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
373 init.parent_names = &parent_name;
374 init.num_parents = 1;
376 clock->csn.reg = base + core->offset;
377 clock->hw.init = &init;
378 clock->div_table = cpg_sd_div_table;
379 clock->div_num = ARRAY_SIZE(cpg_sd_div_table);
381 val = readl(clock->csn.reg) & ~CPG_SD_FC_MASK;
382 val |= CPG_SD_STP_MASK | (clock->div_table[0].val & CPG_SD_FC_MASK);
383 writel(val, clock->csn.reg);
385 clock->div_max = clock->div_table[0].div;
386 clock->div_min = clock->div_max;
387 for (i = 1; i < clock->div_num; i++) {
388 clock->div_max = max(clock->div_max, clock->div_table[i].div);
389 clock->div_min = min(clock->div_min, clock->div_table[i].div);
392 clk = clk_register(NULL, &clock->hw);
396 cpg_simple_notifier_register(notifiers, &clock->csn);
405 static const struct rcar_gen3_cpg_pll_config *cpg_pll_config __initdata;
406 static unsigned int cpg_clk_extalr __initdata;
407 static u32 cpg_mode __initdata;
408 static u32 cpg_quirks __initdata;
410 #define PLL_ERRATA BIT(0) /* Missing PLL0/2/4 post-divider */
411 #define RCKCR_CKSEL BIT(1) /* Manual RCLK parent selection */
413 static const struct soc_device_attribute cpg_quirks_match[] __initconst = {
415 .soc_id = "r8a7795", .revision = "ES1.0",
416 .data = (void *)(PLL_ERRATA | RCKCR_CKSEL),
419 .soc_id = "r8a7795", .revision = "ES1.*",
420 .data = (void *)RCKCR_CKSEL,
423 .soc_id = "r8a7796", .revision = "ES1.0",
424 .data = (void *)RCKCR_CKSEL,
429 struct clk * __init rcar_gen3_cpg_clk_register(struct device *dev,
430 const struct cpg_core_clk *core, const struct cpg_mssr_info *info,
431 struct clk **clks, void __iomem *base,
432 struct raw_notifier_head *notifiers)
434 const struct clk *parent;
435 unsigned int mult = 1;
436 unsigned int div = 1;
439 parent = clks[core->parent & 0xffff]; /* CLK_TYPE_PE uses high bits */
441 return ERR_CAST(parent);
443 switch (core->type) {
444 case CLK_TYPE_GEN3_MAIN:
445 div = cpg_pll_config->extal_div;
448 case CLK_TYPE_GEN3_PLL0:
450 * PLL0 is a configurable multiplier clock. Register it as a
451 * fixed factor clock for now as there's no generic multiplier
452 * clock implementation and we currently have no need to change
453 * the multiplier value.
455 value = readl(base + CPG_PLL0CR);
456 mult = (((value >> 24) & 0x7f) + 1) * 2;
457 if (cpg_quirks & PLL_ERRATA)
461 case CLK_TYPE_GEN3_PLL1:
462 mult = cpg_pll_config->pll1_mult;
463 div = cpg_pll_config->pll1_div;
466 case CLK_TYPE_GEN3_PLL2:
468 * PLL2 is a configurable multiplier clock. Register it as a
469 * fixed factor clock for now as there's no generic multiplier
470 * clock implementation and we currently have no need to change
471 * the multiplier value.
473 value = readl(base + CPG_PLL2CR);
474 mult = (((value >> 24) & 0x7f) + 1) * 2;
475 if (cpg_quirks & PLL_ERRATA)
479 case CLK_TYPE_GEN3_PLL3:
480 mult = cpg_pll_config->pll3_mult;
481 div = cpg_pll_config->pll3_div;
484 case CLK_TYPE_GEN3_PLL4:
486 * PLL4 is a configurable multiplier clock. Register it as a
487 * fixed factor clock for now as there's no generic multiplier
488 * clock implementation and we currently have no need to change
489 * the multiplier value.
491 value = readl(base + CPG_PLL4CR);
492 mult = (((value >> 24) & 0x7f) + 1) * 2;
493 if (cpg_quirks & PLL_ERRATA)
497 case CLK_TYPE_GEN3_SD:
498 return cpg_sd_clk_register(core, base, __clk_get_name(parent),
501 case CLK_TYPE_GEN3_R:
502 if (cpg_quirks & RCKCR_CKSEL) {
503 struct cpg_simple_notifier *csn;
505 csn = kzalloc(sizeof(*csn), GFP_KERNEL);
507 return ERR_PTR(-ENOMEM);
509 csn->reg = base + CPG_RCKCR;
513 * Only if EXTALR is populated, we switch to it.
515 value = readl(csn->reg) & 0x3f;
517 if (clk_get_rate(clks[cpg_clk_extalr])) {
518 parent = clks[cpg_clk_extalr];
522 writel(value, csn->reg);
523 cpg_simple_notifier_register(notifiers, csn);
527 /* Select parent clock of RCLK by MD28 */
528 if (cpg_mode & BIT(28))
529 parent = clks[cpg_clk_extalr];
532 case CLK_TYPE_GEN3_PE:
534 * Peripheral clock with a fixed divider, selectable between
535 * clean and spread spectrum parents using MD12
537 if (cpg_mode & BIT(12)) {
539 div = core->div & 0xffff;
542 parent = clks[core->parent >> 16];
544 return ERR_CAST(parent);
545 div = core->div >> 16;
550 case CLK_TYPE_GEN3_Z:
551 return cpg_z_clk_register(core->name, __clk_get_name(parent),
552 base, CPG_FRQCRC_ZFC_MASK);
554 case CLK_TYPE_GEN3_Z2:
555 return cpg_z_clk_register(core->name, __clk_get_name(parent),
556 base, CPG_FRQCRC_Z2FC_MASK);
559 return ERR_PTR(-EINVAL);
562 return clk_register_fixed_factor(NULL, core->name,
563 __clk_get_name(parent), 0, mult, div);
566 int __init rcar_gen3_cpg_init(const struct rcar_gen3_cpg_pll_config *config,
567 unsigned int clk_extalr, u32 mode)
569 const struct soc_device_attribute *attr;
571 cpg_pll_config = config;
572 cpg_clk_extalr = clk_extalr;
574 attr = soc_device_match(cpg_quirks_match);
576 cpg_quirks = (uintptr_t)attr->data;
577 pr_debug("%s: mode = 0x%x quirks = 0x%x\n", __func__, mode, cpg_quirks);