]> Git Repo - linux.git/blob - drivers/clk/renesas/clk-rcar-gen2.c
Merge tag 'pwm/for-4.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[linux.git] / drivers / clk / renesas / clk-rcar-gen2.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * rcar_gen2 Core CPG Clocks
4  *
5  * Copyright (C) 2013  Ideas On Board SPRL
6  *
7  * Contact: Laurent Pinchart <[email protected]>
8  */
9
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>
15 #include <linux/of.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>
20
21 struct rcar_gen2_cpg {
22         struct clk_onecell_data data;
23         spinlock_t lock;
24         void __iomem *reg;
25 };
26
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
36
37 /* -----------------------------------------------------------------------------
38  * Z Clock
39  *
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
45  */
46
47 struct cpg_z_clk {
48         struct clk_hw hw;
49         void __iomem *reg;
50         void __iomem *kick_reg;
51 };
52
53 #define to_z_clk(_hw)   container_of(_hw, struct cpg_z_clk, hw)
54
55 static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
56                                            unsigned long parent_rate)
57 {
58         struct cpg_z_clk *zclk = to_z_clk(hw);
59         unsigned int mult;
60         unsigned int val;
61
62         val = (readl(zclk->reg) & CPG_FRQCRC_ZFC_MASK) >> CPG_FRQCRC_ZFC_SHIFT;
63         mult = 32 - val;
64
65         return div_u64((u64)parent_rate * mult, 32);
66 }
67
68 static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
69                                  unsigned long *parent_rate)
70 {
71         unsigned long prate  = *parent_rate;
72         unsigned int mult;
73
74         if (!prate)
75                 prate = 1;
76
77         mult = div_u64((u64)rate * 32, prate);
78         mult = clamp(mult, 1U, 32U);
79
80         return *parent_rate / 32 * mult;
81 }
82
83 static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
84                               unsigned long parent_rate)
85 {
86         struct cpg_z_clk *zclk = to_z_clk(hw);
87         unsigned int mult;
88         u32 val, kick;
89         unsigned int i;
90
91         mult = div_u64((u64)rate * 32, parent_rate);
92         mult = clamp(mult, 1U, 32U);
93
94         if (readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
95                 return -EBUSY;
96
97         val = readl(zclk->reg);
98         val &= ~CPG_FRQCRC_ZFC_MASK;
99         val |= (32 - mult) << CPG_FRQCRC_ZFC_SHIFT;
100         writel(val, zclk->reg);
101
102         /*
103          * Set KICK bit in FRQCRB to update hardware setting and wait for
104          * clock change completion.
105          */
106         kick = readl(zclk->kick_reg);
107         kick |= CPG_FRQCRB_KICK;
108         writel(kick, zclk->kick_reg);
109
110         /*
111          * Note: There is no HW information about the worst case latency.
112          *
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.
118          */
119         for (i = 1000; i; i--) {
120                 if (!(readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
121                         return 0;
122
123                 cpu_relax();
124         }
125
126         return -ETIMEDOUT;
127 }
128
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,
133 };
134
135 static struct clk * __init cpg_z_clk_register(struct rcar_gen2_cpg *cpg)
136 {
137         static const char *parent_name = "pll0";
138         struct clk_init_data init;
139         struct cpg_z_clk *zclk;
140         struct clk *clk;
141
142         zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
143         if (!zclk)
144                 return ERR_PTR(-ENOMEM);
145
146         init.name = "z";
147         init.ops = &cpg_z_clk_ops;
148         init.flags = 0;
149         init.parent_names = &parent_name;
150         init.num_parents = 1;
151
152         zclk->reg = cpg->reg + CPG_FRQCRC;
153         zclk->kick_reg = cpg->reg + CPG_FRQCRB;
154         zclk->hw.init = &init;
155
156         clk = clk_register(NULL, &zclk->hw);
157         if (IS_ERR(clk))
158                 kfree(zclk);
159
160         return clk;
161 }
162
163 static struct clk * __init cpg_rcan_clk_register(struct rcar_gen2_cpg *cpg,
164                                                  struct device_node *np)
165 {
166         const char *parent_name = of_clk_get_parent_name(np, 1);
167         struct clk_fixed_factor *fixed;
168         struct clk_gate *gate;
169         struct clk *clk;
170
171         fixed = kzalloc(sizeof(*fixed), GFP_KERNEL);
172         if (!fixed)
173                 return ERR_PTR(-ENOMEM);
174
175         fixed->mult = 1;
176         fixed->div = 6;
177
178         gate = kzalloc(sizeof(*gate), GFP_KERNEL);
179         if (!gate) {
180                 kfree(fixed);
181                 return ERR_PTR(-ENOMEM);
182         }
183
184         gate->reg = cpg->reg + CPG_RCANCKCR;
185         gate->bit_idx = 8;
186         gate->flags = CLK_GATE_SET_TO_DISABLE;
187         gate->lock = &cpg->lock;
188
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);
192         if (IS_ERR(clk)) {
193                 kfree(gate);
194                 kfree(fixed);
195         }
196
197         return clk;
198 }
199
200 /* ADSP divisors */
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 },
205 };
206
207 static struct clk * __init cpg_adsp_clk_register(struct rcar_gen2_cpg *cpg)
208 {
209         const char *parent_name = "pll1";
210         struct clk_divider *div;
211         struct clk_gate *gate;
212         struct clk *clk;
213
214         div = kzalloc(sizeof(*div), GFP_KERNEL);
215         if (!div)
216                 return ERR_PTR(-ENOMEM);
217
218         div->reg = cpg->reg + CPG_ADSPCKCR;
219         div->width = 4;
220         div->table = cpg_adsp_div_table;
221         div->lock = &cpg->lock;
222
223         gate = kzalloc(sizeof(*gate), GFP_KERNEL);
224         if (!gate) {
225                 kfree(div);
226                 return ERR_PTR(-ENOMEM);
227         }
228
229         gate->reg = cpg->reg + CPG_ADSPCKCR;
230         gate->bit_idx = 8;
231         gate->flags = CLK_GATE_SET_TO_DISABLE;
232         gate->lock = &cpg->lock;
233
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);
237         if (IS_ERR(clk)) {
238                 kfree(gate);
239                 kfree(div);
240         }
241
242         return clk;
243 }
244
245 /* -----------------------------------------------------------------------------
246  * CPG Clock Data
247  */
248
249 /*
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
261  *
262  * *1 : Table 7.6 indicates VCO output (PLLx = VCO/2)
263  */
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 */
272 };
273
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 },
279 };
280
281 /* SDHI divisors */
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 },
286 };
287
288 static const struct clk_div_table cpg_sd01_div_table[] = {
289         {  4,  8 },
290         {  5, 12 }, {  6, 16 }, {  7, 18 }, {  8, 24 },
291         { 10, 36 }, { 11, 48 }, { 12, 10 }, {  0,  0 },
292 };
293
294 /* -----------------------------------------------------------------------------
295  * Initialization
296  */
297
298 static u32 cpg_mode __initdata;
299
300 static const char * const pll0_mult_match[] = {
301         "renesas,r8a7792-cpg-clocks",
302         "renesas,r8a7794-cpg-clocks",
303         NULL
304 };
305
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,
309                              const char *name)
310 {
311         const struct clk_div_table *table = NULL;
312         const char *parent_name;
313         unsigned int shift;
314         unsigned int mult = 1;
315         unsigned int div = 1;
316
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.
325                  */
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;
329                         div = 3;
330                 } else {
331                         u32 value = readl(cpg->reg + CPG_PLL0CR);
332                         mult = ((value >> 24) & ((1 << 7) - 1)) + 1;
333                 }
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)
347                     ? 8 : 10;
348         } else if (!strcmp(name, "sdh")) {
349                 parent_name = "pll1";
350                 table = cpg_sdh_div_table;
351                 shift = 8;
352         } else if (!strcmp(name, "sd0")) {
353                 parent_name = "pll1";
354                 table = cpg_sd01_div_table;
355                 shift = 4;
356         } else if (!strcmp(name, "sd1")) {
357                 parent_name = "pll1";
358                 table = cpg_sd01_div_table;
359                 shift = 0;
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);
366         } else {
367                 return ERR_PTR(-EINVAL);
368         }
369
370         if (!table)
371                 return clk_register_fixed_factor(NULL, name, parent_name, 0,
372                                                  mult, div);
373         else
374                 return clk_register_divider_table(NULL, name, parent_name, 0,
375                                                  cpg->reg + CPG_SDCKCR, shift,
376                                                  4, 0, table, &cpg->lock);
377 }
378
379 /*
380  * Reset register definitions.
381  */
382 #define MODEMR  0xe6160060
383
384 static u32 __init rcar_gen2_read_mode_pins(void)
385 {
386         void __iomem *modemr = ioremap_nocache(MODEMR, 4);
387         u32 mode;
388
389         BUG_ON(!modemr);
390         mode = ioread32(modemr);
391         iounmap(modemr);
392
393         return mode;
394 }
395
396 static void __init rcar_gen2_cpg_clocks_init(struct device_node *np)
397 {
398         const struct cpg_pll_config *config;
399         struct rcar_gen2_cpg *cpg;
400         struct clk **clks;
401         unsigned int i;
402         int num_clks;
403
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();
408         }
409
410         num_clks = of_property_count_strings(np, "clock-output-names");
411         if (num_clks < 0) {
412                 pr_err("%s: failed to count clocks\n", __func__);
413                 return;
414         }
415
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.
421                  */
422                 return;
423         }
424
425         spin_lock_init(&cpg->lock);
426
427         cpg->data.clks = clks;
428         cpg->data.clk_num = num_clks;
429
430         cpg->reg = of_iomap(np, 0);
431         if (WARN_ON(cpg->reg == NULL))
432                 return;
433
434         config = &cpg_pll_configs[CPG_PLL_CONFIG_INDEX(cpg_mode)];
435
436         for (i = 0; i < num_clks; ++i) {
437                 const char *name;
438                 struct clk *clk;
439
440                 of_property_read_string_index(np, "clock-output-names", i,
441                                               &name);
442
443                 clk = rcar_gen2_cpg_register_clock(np, cpg, config, name);
444                 if (IS_ERR(clk))
445                         pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
446                                __func__, np, name, PTR_ERR(clk));
447                 else
448                         cpg->data.clks[i] = clk;
449         }
450
451         of_clk_add_provider(np, of_clk_src_onecell_get, &cpg->data);
452
453         cpg_mstp_add_clk_domain(np);
454 }
455 CLK_OF_DECLARE(rcar_gen2_cpg_clks, "renesas,rcar-gen2-cpg-clocks",
456                rcar_gen2_cpg_clocks_init);
This page took 0.063251 seconds and 4 git commands to generate.