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/clk.h>
17 #include <linux/clk-provider.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/sys_soc.h>
25 #include "renesas-cpg-mssr.h"
26 #include "rcar-gen3-cpg.h"
28 #define CPG_PLL0CR 0x00d8
29 #define CPG_PLL2CR 0x002c
30 #define CPG_PLL4CR 0x01f4
36 #define CPG_SD_STP_HCK BIT(9)
37 #define CPG_SD_STP_CK BIT(8)
39 #define CPG_SD_STP_MASK (CPG_SD_STP_HCK | CPG_SD_STP_CK)
40 #define CPG_SD_FC_MASK (0x7 << 2 | 0x3 << 0)
42 #define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \
44 .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \
45 ((stp_ck) ? CPG_SD_STP_CK : 0) | \
59 const struct sd_div_table *div_table;
67 * stp_hck stp_ck (div) (div) = sd_srcfc x sd_fc
68 *-------------------------------------------------------------------
80 static const struct sd_div_table cpg_sd_div_table[] = {
81 /* CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) */
82 CPG_SD_DIV_TABLE_DATA(0, 0, 0, 1, 4),
83 CPG_SD_DIV_TABLE_DATA(0, 0, 1, 1, 8),
84 CPG_SD_DIV_TABLE_DATA(1, 0, 2, 1, 16),
85 CPG_SD_DIV_TABLE_DATA(1, 0, 3, 1, 32),
86 CPG_SD_DIV_TABLE_DATA(1, 0, 4, 1, 64),
87 CPG_SD_DIV_TABLE_DATA(0, 0, 0, 0, 2),
88 CPG_SD_DIV_TABLE_DATA(0, 0, 1, 0, 4),
89 CPG_SD_DIV_TABLE_DATA(1, 0, 2, 0, 8),
90 CPG_SD_DIV_TABLE_DATA(1, 0, 3, 0, 16),
91 CPG_SD_DIV_TABLE_DATA(1, 0, 4, 0, 32),
94 #define to_sd_clock(_hw) container_of(_hw, struct sd_clock, hw)
96 static int cpg_sd_clock_enable(struct clk_hw *hw)
98 struct sd_clock *clock = to_sd_clock(hw);
102 val = readl(clock->reg);
104 sd_fc = val & CPG_SD_FC_MASK;
105 for (i = 0; i < clock->div_num; i++)
106 if (sd_fc == (clock->div_table[i].val & CPG_SD_FC_MASK))
109 if (i >= clock->div_num)
112 val &= ~(CPG_SD_STP_MASK);
113 val |= clock->div_table[i].val & CPG_SD_STP_MASK;
115 writel(val, clock->reg);
120 static void cpg_sd_clock_disable(struct clk_hw *hw)
122 struct sd_clock *clock = to_sd_clock(hw);
124 writel(readl(clock->reg) | CPG_SD_STP_MASK, clock->reg);
127 static int cpg_sd_clock_is_enabled(struct clk_hw *hw)
129 struct sd_clock *clock = to_sd_clock(hw);
131 return !(readl(clock->reg) & CPG_SD_STP_MASK);
134 static unsigned long cpg_sd_clock_recalc_rate(struct clk_hw *hw,
135 unsigned long parent_rate)
137 struct sd_clock *clock = to_sd_clock(hw);
138 unsigned long rate = parent_rate;
142 val = readl(clock->reg);
144 sd_fc = val & CPG_SD_FC_MASK;
145 for (i = 0; i < clock->div_num; i++)
146 if (sd_fc == (clock->div_table[i].val & CPG_SD_FC_MASK))
149 if (i >= clock->div_num)
152 return DIV_ROUND_CLOSEST(rate, clock->div_table[i].div);
155 static unsigned int cpg_sd_clock_calc_div(struct sd_clock *clock,
157 unsigned long parent_rate)
164 div = DIV_ROUND_CLOSEST(parent_rate, rate);
166 return clamp_t(unsigned int, div, clock->div_min, clock->div_max);
169 static long cpg_sd_clock_round_rate(struct clk_hw *hw, unsigned long rate,
170 unsigned long *parent_rate)
172 struct sd_clock *clock = to_sd_clock(hw);
173 unsigned int div = cpg_sd_clock_calc_div(clock, rate, *parent_rate);
175 return DIV_ROUND_CLOSEST(*parent_rate, div);
178 static int cpg_sd_clock_set_rate(struct clk_hw *hw, unsigned long rate,
179 unsigned long parent_rate)
181 struct sd_clock *clock = to_sd_clock(hw);
182 unsigned int div = cpg_sd_clock_calc_div(clock, rate, parent_rate);
186 for (i = 0; i < clock->div_num; i++)
187 if (div == clock->div_table[i].div)
190 if (i >= clock->div_num)
193 val = readl(clock->reg);
194 val &= ~(CPG_SD_STP_MASK | CPG_SD_FC_MASK);
195 val |= clock->div_table[i].val & (CPG_SD_STP_MASK | CPG_SD_FC_MASK);
196 writel(val, clock->reg);
201 static const struct clk_ops cpg_sd_clock_ops = {
202 .enable = cpg_sd_clock_enable,
203 .disable = cpg_sd_clock_disable,
204 .is_enabled = cpg_sd_clock_is_enabled,
205 .recalc_rate = cpg_sd_clock_recalc_rate,
206 .round_rate = cpg_sd_clock_round_rate,
207 .set_rate = cpg_sd_clock_set_rate,
210 static struct clk * __init cpg_sd_clk_register(const struct cpg_core_clk *core,
212 const char *parent_name)
214 struct clk_init_data init;
215 struct sd_clock *clock;
219 clock = kzalloc(sizeof(*clock), GFP_KERNEL);
221 return ERR_PTR(-ENOMEM);
223 init.name = core->name;
224 init.ops = &cpg_sd_clock_ops;
225 init.flags = CLK_IS_BASIC | CLK_SET_RATE_PARENT;
226 init.parent_names = &parent_name;
227 init.num_parents = 1;
229 clock->reg = base + core->offset;
230 clock->hw.init = &init;
231 clock->div_table = cpg_sd_div_table;
232 clock->div_num = ARRAY_SIZE(cpg_sd_div_table);
234 clock->div_max = clock->div_table[0].div;
235 clock->div_min = clock->div_max;
236 for (i = 1; i < clock->div_num; i++) {
237 clock->div_max = max(clock->div_max, clock->div_table[i].div);
238 clock->div_min = min(clock->div_min, clock->div_table[i].div);
241 clk = clk_register(NULL, &clock->hw);
249 static const struct rcar_gen3_cpg_pll_config *cpg_pll_config __initdata;
250 static unsigned int cpg_clk_extalr __initdata;
251 static u32 cpg_mode __initdata;
252 static u32 cpg_quirks __initdata;
254 #define PLL_ERRATA BIT(0) /* Missing PLL0/2/4 post-divider */
255 #define RCKCR_CKSEL BIT(1) /* Manual RCLK parent selection */
257 static const struct soc_device_attribute cpg_quirks_match[] __initconst = {
259 .soc_id = "r8a7795", .revision = "ES1.0",
260 .data = (void *)(PLL_ERRATA | RCKCR_CKSEL),
263 .soc_id = "r8a7795", .revision = "ES1.*",
264 .data = (void *)RCKCR_CKSEL,
267 .soc_id = "r8a7796", .revision = "ES1.0",
268 .data = (void *)RCKCR_CKSEL,
273 struct clk * __init rcar_gen3_cpg_clk_register(struct device *dev,
274 const struct cpg_core_clk *core, const struct cpg_mssr_info *info,
275 struct clk **clks, void __iomem *base)
277 const struct clk *parent;
278 unsigned int mult = 1;
279 unsigned int div = 1;
282 parent = clks[core->parent];
284 return ERR_CAST(parent);
286 switch (core->type) {
287 case CLK_TYPE_GEN3_MAIN:
288 div = cpg_pll_config->extal_div;
291 case CLK_TYPE_GEN3_PLL0:
293 * PLL0 is a configurable multiplier clock. Register it as a
294 * fixed factor clock for now as there's no generic multiplier
295 * clock implementation and we currently have no need to change
296 * the multiplier value.
298 value = readl(base + CPG_PLL0CR);
299 mult = (((value >> 24) & 0x7f) + 1) * 2;
300 if (cpg_quirks & PLL_ERRATA)
304 case CLK_TYPE_GEN3_PLL1:
305 mult = cpg_pll_config->pll1_mult;
308 case CLK_TYPE_GEN3_PLL2:
310 * PLL2 is a configurable multiplier clock. Register it as a
311 * fixed factor clock for now as there's no generic multiplier
312 * clock implementation and we currently have no need to change
313 * the multiplier value.
315 value = readl(base + CPG_PLL2CR);
316 mult = (((value >> 24) & 0x7f) + 1) * 2;
317 if (cpg_quirks & PLL_ERRATA)
321 case CLK_TYPE_GEN3_PLL3:
322 mult = cpg_pll_config->pll3_mult;
325 case CLK_TYPE_GEN3_PLL4:
327 * PLL4 is a configurable multiplier clock. Register it as a
328 * fixed factor clock for now as there's no generic multiplier
329 * clock implementation and we currently have no need to change
330 * the multiplier value.
332 value = readl(base + CPG_PLL4CR);
333 mult = (((value >> 24) & 0x7f) + 1) * 2;
334 if (cpg_quirks & PLL_ERRATA)
338 case CLK_TYPE_GEN3_SD:
339 return cpg_sd_clk_register(core, base, __clk_get_name(parent));
341 case CLK_TYPE_GEN3_R:
342 if (cpg_quirks & RCKCR_CKSEL) {
345 * Only if EXTALR is populated, we switch to it.
347 value = readl(base + CPG_RCKCR) & 0x3f;
349 if (clk_get_rate(clks[cpg_clk_extalr])) {
350 parent = clks[cpg_clk_extalr];
354 writel(value, base + CPG_RCKCR);
358 /* Select parent clock of RCLK by MD28 */
359 if (cpg_mode & BIT(28))
360 parent = clks[cpg_clk_extalr];
364 return ERR_PTR(-EINVAL);
367 return clk_register_fixed_factor(NULL, core->name,
368 __clk_get_name(parent), 0, mult, div);
371 int __init rcar_gen3_cpg_init(const struct rcar_gen3_cpg_pll_config *config,
372 unsigned int clk_extalr, u32 mode)
374 const struct soc_device_attribute *attr;
376 cpg_pll_config = config;
377 cpg_clk_extalr = clk_extalr;
379 attr = soc_device_match(cpg_quirks_match);
381 cpg_quirks = (uintptr_t)attr->data;
382 pr_debug("%s: mode = 0x%x quirks = 0x%x\n", __func__, mode, cpg_quirks);