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)
82 void __iomem *kick_reg;
85 #define to_z_clk(_hw) container_of(_hw, struct cpg_z_clk, hw)
87 static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
88 unsigned long parent_rate)
90 struct cpg_z_clk *zclk = to_z_clk(hw);
93 mult = 32 - FIELD_GET(CPG_FRQCRC_ZFC_MASK, clk_readl(zclk->reg));
95 /* Factor of 2 is for fixed divider */
96 return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, 32 * 2);
99 static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
100 unsigned long *parent_rate)
102 /* Factor of 2 is for fixed divider */
103 unsigned long prate = *parent_rate / 2;
106 mult = div_u64(rate * 32ULL, prate);
107 mult = clamp(mult, 1U, 32U);
109 return (u64)prate * mult / 32;
112 static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
113 unsigned long parent_rate)
115 struct cpg_z_clk *zclk = to_z_clk(hw);
120 /* Factor of 2 is for fixed divider */
121 mult = DIV_ROUND_CLOSEST_ULL(rate * 32ULL * 2, parent_rate);
122 mult = clamp(mult, 1U, 32U);
124 if (clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
127 val = clk_readl(zclk->reg) & ~CPG_FRQCRC_ZFC_MASK;
128 val |= FIELD_PREP(CPG_FRQCRC_ZFC_MASK, 32 - mult);
129 clk_writel(val, zclk->reg);
132 * Set KICK bit in FRQCRB to update hardware setting and wait for
133 * clock change completion.
135 kick = clk_readl(zclk->kick_reg);
136 kick |= CPG_FRQCRB_KICK;
137 clk_writel(kick, zclk->kick_reg);
140 * Note: There is no HW information about the worst case latency.
142 * Using experimental measurements, it seems that no more than
143 * ~10 iterations are needed, independently of the CPU rate.
144 * Since this value might be dependent of external xtal rate, pll1
145 * rate or even the other emulation clocks rate, use 1000 as a
146 * "super" safe value.
148 for (i = 1000; i; i--) {
149 if (!(clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
158 static const struct clk_ops cpg_z_clk_ops = {
159 .recalc_rate = cpg_z_clk_recalc_rate,
160 .round_rate = cpg_z_clk_round_rate,
161 .set_rate = cpg_z_clk_set_rate,
164 static struct clk * __init cpg_z_clk_register(const char *name,
165 const char *parent_name,
168 struct clk_init_data init;
169 struct cpg_z_clk *zclk;
172 zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
174 return ERR_PTR(-ENOMEM);
177 init.ops = &cpg_z_clk_ops;
179 init.parent_names = &parent_name;
180 init.num_parents = 1;
182 zclk->reg = reg + CPG_FRQCRC;
183 zclk->kick_reg = reg + CPG_FRQCRB;
184 zclk->hw.init = &init;
186 clk = clk_register(NULL, &zclk->hw);
196 #define CPG_SD_STP_HCK BIT(9)
197 #define CPG_SD_STP_CK BIT(8)
199 #define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK)
200 #define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0)
202 #define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \
204 .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \
205 ((stp_ck) ? CPG_SD_STP_CK : 0) | \
206 ((sd_srcfc) << 2) | \
211 struct sd_div_table {
218 const struct sd_div_table *div_table;
219 struct cpg_simple_notifier csn;
220 unsigned int div_num;
221 unsigned int div_min;
222 unsigned int div_max;
223 unsigned int cur_div_idx;
228 * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc
229 *-------------------------------------------------------------------
234 * 1 0 4 (16) 1 (4) 64
239 * 1 0 4 (16) 0 (2) 32
241 static const struct sd_div_table cpg_sd_div_table[] = {
242 /* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */
243 CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4),
244 CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8),
245 CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16),
246 CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32),
247 CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64),
248 CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2),
249 CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4),
250 CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8),
251 CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16),
252 CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32),
255 #define to_sd_clock(_hw) container_of(_hw, struct sd_clock, hw)
257 static int cpg_sd_clock_enable(struct clk_hw *hw)
259 struct sd_clock *clock = to_sd_clock(hw);
260 u32 val = readl(clock->csn.reg);
262 val &= ~(CPG_SD_STP_MASK);
263 val |= clock->div_table[clock->cur_div_idx].val & CPG_SD_STP_MASK;
265 writel(val, clock->csn.reg);
270 static void cpg_sd_clock_disable(struct clk_hw *hw)
272 struct sd_clock *clock = to_sd_clock(hw);
274 writel(readl(clock->csn.reg) | CPG_SD_STP_MASK, clock->csn.reg);
277 static int cpg_sd_clock_is_enabled(struct clk_hw *hw)
279 struct sd_clock *clock = to_sd_clock(hw);
281 return !(readl(clock->csn.reg) & CPG_SD_STP_MASK);
284 static unsigned long cpg_sd_clock_recalc_rate(struct clk_hw *hw,
285 unsigned long parent_rate)
287 struct sd_clock *clock = to_sd_clock(hw);
289 return DIV_ROUND_CLOSEST(parent_rate,
290 clock->div_table[clock->cur_div_idx].div);
293 static unsigned int cpg_sd_clock_calc_div(struct sd_clock *clock,
295 unsigned long parent_rate)
302 div = DIV_ROUND_CLOSEST(parent_rate, rate);
304 return clamp_t(unsigned int, div, clock->div_min, clock->div_max);
307 static long cpg_sd_clock_round_rate(struct clk_hw *hw, unsigned long rate,
308 unsigned long *parent_rate)
310 struct sd_clock *clock = to_sd_clock(hw);
311 unsigned int div = cpg_sd_clock_calc_div(clock, rate, *parent_rate);
313 return DIV_ROUND_CLOSEST(*parent_rate, div);
316 static int cpg_sd_clock_set_rate(struct clk_hw *hw, unsigned long rate,
317 unsigned long parent_rate)
319 struct sd_clock *clock = to_sd_clock(hw);
320 unsigned int div = cpg_sd_clock_calc_div(clock, rate, parent_rate);
324 for (i = 0; i < clock->div_num; i++)
325 if (div == clock->div_table[i].div)
328 if (i >= clock->div_num)
331 clock->cur_div_idx = i;
333 val = readl(clock->csn.reg);
334 val &= ~(CPG_SD_STP_MASK | CPG_SD_FC_MASK);
335 val |= clock->div_table[i].val & (CPG_SD_STP_MASK | CPG_SD_FC_MASK);
336 writel(val, clock->csn.reg);
341 static const struct clk_ops cpg_sd_clock_ops = {
342 .enable = cpg_sd_clock_enable,
343 .disable = cpg_sd_clock_disable,
344 .is_enabled = cpg_sd_clock_is_enabled,
345 .recalc_rate = cpg_sd_clock_recalc_rate,
346 .round_rate = cpg_sd_clock_round_rate,
347 .set_rate = cpg_sd_clock_set_rate,
350 static struct clk * __init cpg_sd_clk_register(const struct cpg_core_clk *core,
351 void __iomem *base, const char *parent_name,
352 struct raw_notifier_head *notifiers)
354 struct clk_init_data init;
355 struct sd_clock *clock;
360 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
362 return ERR_PTR(-ENOMEM);
364 init.name = core->name;
365 init.ops = &cpg_sd_clock_ops;
366 init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
367 init.parent_names = &parent_name;
368 init.num_parents = 1;
370 clock->csn.reg = base + core->offset;
371 clock->hw.init = &init;
372 clock->div_table = cpg_sd_div_table;
373 clock->div_num = ARRAY_SIZE(cpg_sd_div_table);
375 sd_fc = readl(clock->csn.reg) & CPG_SD_FC_MASK;
376 for (i = 0; i < clock->div_num; i++)
377 if (sd_fc == (clock->div_table[i].val & CPG_SD_FC_MASK))
380 if (WARN_ON(i >= clock->div_num)) {
382 return ERR_PTR(-EINVAL);
385 clock->cur_div_idx = i;
387 clock->div_max = clock->div_table[0].div;
388 clock->div_min = clock->div_max;
389 for (i = 1; i < clock->div_num; i++) {
390 clock->div_max = max(clock->div_max, clock->div_table[i].div);
391 clock->div_min = min(clock->div_min, clock->div_table[i].div);
394 clk = clk_register(NULL, &clock->hw);
398 cpg_simple_notifier_register(notifiers, &clock->csn);
407 static const struct rcar_gen3_cpg_pll_config *cpg_pll_config __initdata;
408 static unsigned int cpg_clk_extalr __initdata;
409 static u32 cpg_mode __initdata;
410 static u32 cpg_quirks __initdata;
412 #define PLL_ERRATA BIT(0) /* Missing PLL0/2/4 post-divider */
413 #define RCKCR_CKSEL BIT(1) /* Manual RCLK parent selection */
415 static const struct soc_device_attribute cpg_quirks_match[] __initconst = {
417 .soc_id = "r8a7795", .revision = "ES1.0",
418 .data = (void *)(PLL_ERRATA | RCKCR_CKSEL),
421 .soc_id = "r8a7795", .revision = "ES1.*",
422 .data = (void *)RCKCR_CKSEL,
425 .soc_id = "r8a7796", .revision = "ES1.0",
426 .data = (void *)RCKCR_CKSEL,
431 struct clk * __init rcar_gen3_cpg_clk_register(struct device *dev,
432 const struct cpg_core_clk *core, const struct cpg_mssr_info *info,
433 struct clk **clks, void __iomem *base,
434 struct raw_notifier_head *notifiers)
436 const struct clk *parent;
437 unsigned int mult = 1;
438 unsigned int div = 1;
441 parent = clks[core->parent & 0xffff]; /* CLK_TYPE_PE uses high bits */
443 return ERR_CAST(parent);
445 switch (core->type) {
446 case CLK_TYPE_GEN3_MAIN:
447 div = cpg_pll_config->extal_div;
450 case CLK_TYPE_GEN3_PLL0:
452 * PLL0 is a configurable multiplier clock. Register it as a
453 * fixed factor clock for now as there's no generic multiplier
454 * clock implementation and we currently have no need to change
455 * the multiplier value.
457 value = readl(base + CPG_PLL0CR);
458 mult = (((value >> 24) & 0x7f) + 1) * 2;
459 if (cpg_quirks & PLL_ERRATA)
463 case CLK_TYPE_GEN3_PLL1:
464 mult = cpg_pll_config->pll1_mult;
465 div = cpg_pll_config->pll1_div;
468 case CLK_TYPE_GEN3_PLL2:
470 * PLL2 is a configurable multiplier clock. Register it as a
471 * fixed factor clock for now as there's no generic multiplier
472 * clock implementation and we currently have no need to change
473 * the multiplier value.
475 value = readl(base + CPG_PLL2CR);
476 mult = (((value >> 24) & 0x7f) + 1) * 2;
477 if (cpg_quirks & PLL_ERRATA)
481 case CLK_TYPE_GEN3_PLL3:
482 mult = cpg_pll_config->pll3_mult;
483 div = cpg_pll_config->pll3_div;
486 case CLK_TYPE_GEN3_PLL4:
488 * PLL4 is a configurable multiplier clock. Register it as a
489 * fixed factor clock for now as there's no generic multiplier
490 * clock implementation and we currently have no need to change
491 * the multiplier value.
493 value = readl(base + CPG_PLL4CR);
494 mult = (((value >> 24) & 0x7f) + 1) * 2;
495 if (cpg_quirks & PLL_ERRATA)
499 case CLK_TYPE_GEN3_SD:
500 return cpg_sd_clk_register(core, base, __clk_get_name(parent),
503 case CLK_TYPE_GEN3_R:
504 if (cpg_quirks & RCKCR_CKSEL) {
505 struct cpg_simple_notifier *csn;
507 csn = kzalloc(sizeof(*csn), GFP_KERNEL);
509 return ERR_PTR(-ENOMEM);
511 csn->reg = base + CPG_RCKCR;
515 * Only if EXTALR is populated, we switch to it.
517 value = readl(csn->reg) & 0x3f;
519 if (clk_get_rate(clks[cpg_clk_extalr])) {
520 parent = clks[cpg_clk_extalr];
524 writel(value, csn->reg);
525 cpg_simple_notifier_register(notifiers, csn);
529 /* Select parent clock of RCLK by MD28 */
530 if (cpg_mode & BIT(28))
531 parent = clks[cpg_clk_extalr];
534 case CLK_TYPE_GEN3_PE:
536 * Peripheral clock with a fixed divider, selectable between
537 * clean and spread spectrum parents using MD12
539 if (cpg_mode & BIT(12)) {
541 div = core->div & 0xffff;
544 parent = clks[core->parent >> 16];
546 return ERR_CAST(parent);
547 div = core->div >> 16;
552 case CLK_TYPE_GEN3_Z:
553 return cpg_z_clk_register(core->name, __clk_get_name(parent),
557 return ERR_PTR(-EINVAL);
560 return clk_register_fixed_factor(NULL, core->name,
561 __clk_get_name(parent), 0, mult, div);
564 int __init rcar_gen3_cpg_init(const struct rcar_gen3_cpg_pll_config *config,
565 unsigned int clk_extalr, u32 mode)
567 const struct soc_device_attribute *attr;
569 cpg_pll_config = config;
570 cpg_clk_extalr = clk_extalr;
572 attr = soc_device_match(cpg_quirks_match);
574 cpg_quirks = (uintptr_t)attr->data;
575 pr_debug("%s: mode = 0x%x quirks = 0x%x\n", __func__, mode, cpg_quirks);