b85918fa62c6f751dcd4cc08a7d5bac8de738f4b
[linux-2.6-microblaze.git] / drivers / clk / renesas / rcar-gen3-cpg.c
1 /*
2  * R-Car Gen3 Clock Pulse Generator
3  *
4  * Copyright (C) 2015-2016 Glider bvba
5  *
6  * Based on clk-rcar-gen3.c
7  *
8  * Copyright (C) 2015 Renesas Electronics Corp.
9  *
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.
13  */
14
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>
22 #include <linux/io.h>
23 #include <linux/pm.h>
24 #include <linux/slab.h>
25 #include <linux/sys_soc.h>
26
27 #include "renesas-cpg-mssr.h"
28 #include "rcar-gen3-cpg.h"
29
30 #define CPG_PLL0CR              0x00d8
31 #define CPG_PLL2CR              0x002c
32 #define CPG_PLL4CR              0x01f4
33
34 struct cpg_simple_notifier {
35         struct notifier_block nb;
36         void __iomem *reg;
37         u32 saved;
38 };
39
40 static int cpg_simple_notifier_call(struct notifier_block *nb,
41                                     unsigned long action, void *data)
42 {
43         struct cpg_simple_notifier *csn =
44                 container_of(nb, struct cpg_simple_notifier, nb);
45
46         switch (action) {
47         case PM_EVENT_SUSPEND:
48                 csn->saved = readl(csn->reg);
49                 return NOTIFY_OK;
50
51         case PM_EVENT_RESUME:
52                 writel(csn->saved, csn->reg);
53                 return NOTIFY_OK;
54         }
55         return NOTIFY_DONE;
56 }
57
58 static void cpg_simple_notifier_register(struct raw_notifier_head *notifiers,
59                                          struct cpg_simple_notifier *csn)
60 {
61         csn->nb.notifier_call = cpg_simple_notifier_call;
62         raw_notifier_chain_register(notifiers, &csn->nb);
63 }
64
65 /*
66  * Z Clock
67  *
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
73  */
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
79 struct cpg_z_clk {
80         struct clk_hw hw;
81         void __iomem *reg;
82         void __iomem *kick_reg;
83 };
84
85 #define to_z_clk(_hw)   container_of(_hw, struct cpg_z_clk, hw)
86
87 static unsigned long cpg_z_clk_recalc_rate(struct clk_hw *hw,
88                                            unsigned long parent_rate)
89 {
90         struct cpg_z_clk *zclk = to_z_clk(hw);
91         unsigned int mult;
92
93         mult = 32 - FIELD_GET(CPG_FRQCRC_ZFC_MASK, clk_readl(zclk->reg));
94
95         /* Factor of 2 is for fixed divider */
96         return DIV_ROUND_CLOSEST_ULL((u64)parent_rate * mult, 32 * 2);
97 }
98
99 static long cpg_z_clk_round_rate(struct clk_hw *hw, unsigned long rate,
100                                  unsigned long *parent_rate)
101 {
102         /* Factor of 2 is for fixed divider */
103         unsigned long prate = *parent_rate / 2;
104         unsigned int mult;
105
106         mult = div_u64(rate * 32ULL, prate);
107         mult = clamp(mult, 1U, 32U);
108
109         return (u64)prate * mult / 32;
110 }
111
112 static int cpg_z_clk_set_rate(struct clk_hw *hw, unsigned long rate,
113                               unsigned long parent_rate)
114 {
115         struct cpg_z_clk *zclk = to_z_clk(hw);
116         unsigned int mult;
117         unsigned int i;
118         u32 val, kick;
119
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);
123
124         if (clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK)
125                 return -EBUSY;
126
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);
130
131         /*
132          * Set KICK bit in FRQCRB to update hardware setting and wait for
133          * clock change completion.
134          */
135         kick = clk_readl(zclk->kick_reg);
136         kick |= CPG_FRQCRB_KICK;
137         clk_writel(kick, zclk->kick_reg);
138
139         /*
140          * Note: There is no HW information about the worst case latency.
141          *
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.
147          */
148         for (i = 1000; i; i--) {
149                 if (!(clk_readl(zclk->kick_reg) & CPG_FRQCRB_KICK))
150                         return 0;
151
152                 cpu_relax();
153         }
154
155         return -ETIMEDOUT;
156 }
157
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,
162 };
163
164 static struct clk * __init cpg_z_clk_register(const char *name,
165                                               const char *parent_name,
166                                               void __iomem *reg)
167 {
168         struct clk_init_data init;
169         struct cpg_z_clk *zclk;
170         struct clk *clk;
171
172         zclk = kzalloc(sizeof(*zclk), GFP_KERNEL);
173         if (!zclk)
174                 return ERR_PTR(-ENOMEM);
175
176         init.name = name;
177         init.ops = &cpg_z_clk_ops;
178         init.flags = 0;
179         init.parent_names = &parent_name;
180         init.num_parents = 1;
181
182         zclk->reg = reg + CPG_FRQCRC;
183         zclk->kick_reg = reg + CPG_FRQCRB;
184         zclk->hw.init = &init;
185
186         clk = clk_register(NULL, &zclk->hw);
187         if (IS_ERR(clk))
188                 kfree(zclk);
189
190         return clk;
191 }
192
193 /*
194  * SDn Clock
195  */
196 #define CPG_SD_STP_HCK          BIT(9)
197 #define CPG_SD_STP_CK           BIT(8)
198
199 #define CPG_SD_STP_MASK         (CPG_SD_STP_HCK | CPG_SD_STP_CK)
200 #define CPG_SD_FC_MASK          (0x7 << 2 | 0x3 << 0)
201
202 #define CPG_SD_DIV_TABLE_DATA(stp_hck, stp_ck, sd_srcfc, sd_fc, sd_div) \
203 { \
204         .val = ((stp_hck) ? CPG_SD_STP_HCK : 0) | \
205                ((stp_ck) ? CPG_SD_STP_CK : 0) | \
206                ((sd_srcfc) << 2) | \
207                ((sd_fc) << 0), \
208         .div = (sd_div), \
209 }
210
211 struct sd_div_table {
212         u32 val;
213         unsigned int div;
214 };
215
216 struct sd_clock {
217         struct clk_hw hw;
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;
224 };
225
226 /* SDn divider
227  *                     sd_srcfc   sd_fc   div
228  * stp_hck   stp_ck    (div)      (div)     = sd_srcfc x sd_fc
229  *-------------------------------------------------------------------
230  *  0         0         0 (1)      1 (4)      4
231  *  0         0         1 (2)      1 (4)      8
232  *  1         0         2 (4)      1 (4)     16
233  *  1         0         3 (8)      1 (4)     32
234  *  1         0         4 (16)     1 (4)     64
235  *  0         0         0 (1)      0 (2)      2
236  *  0         0         1 (2)      0 (2)      4
237  *  1         0         2 (4)      0 (2)      8
238  *  1         0         3 (8)      0 (2)     16
239  *  1         0         4 (16)     0 (2)     32
240  */
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),
253 };
254
255 #define to_sd_clock(_hw) container_of(_hw, struct sd_clock, hw)
256
257 static int cpg_sd_clock_enable(struct clk_hw *hw)
258 {
259         struct sd_clock *clock = to_sd_clock(hw);
260         u32 val = readl(clock->csn.reg);
261
262         val &= ~(CPG_SD_STP_MASK);
263         val |= clock->div_table[clock->cur_div_idx].val & CPG_SD_STP_MASK;
264
265         writel(val, clock->csn.reg);
266
267         return 0;
268 }
269
270 static void cpg_sd_clock_disable(struct clk_hw *hw)
271 {
272         struct sd_clock *clock = to_sd_clock(hw);
273
274         writel(readl(clock->csn.reg) | CPG_SD_STP_MASK, clock->csn.reg);
275 }
276
277 static int cpg_sd_clock_is_enabled(struct clk_hw *hw)
278 {
279         struct sd_clock *clock = to_sd_clock(hw);
280
281         return !(readl(clock->csn.reg) & CPG_SD_STP_MASK);
282 }
283
284 static unsigned long cpg_sd_clock_recalc_rate(struct clk_hw *hw,
285                                                 unsigned long parent_rate)
286 {
287         struct sd_clock *clock = to_sd_clock(hw);
288
289         return DIV_ROUND_CLOSEST(parent_rate,
290                                  clock->div_table[clock->cur_div_idx].div);
291 }
292
293 static unsigned int cpg_sd_clock_calc_div(struct sd_clock *clock,
294                                           unsigned long rate,
295                                           unsigned long parent_rate)
296 {
297         unsigned int div;
298
299         if (!rate)
300                 rate = 1;
301
302         div = DIV_ROUND_CLOSEST(parent_rate, rate);
303
304         return clamp_t(unsigned int, div, clock->div_min, clock->div_max);
305 }
306
307 static long cpg_sd_clock_round_rate(struct clk_hw *hw, unsigned long rate,
308                                       unsigned long *parent_rate)
309 {
310         struct sd_clock *clock = to_sd_clock(hw);
311         unsigned int div = cpg_sd_clock_calc_div(clock, rate, *parent_rate);
312
313         return DIV_ROUND_CLOSEST(*parent_rate, div);
314 }
315
316 static int cpg_sd_clock_set_rate(struct clk_hw *hw, unsigned long rate,
317                                    unsigned long parent_rate)
318 {
319         struct sd_clock *clock = to_sd_clock(hw);
320         unsigned int div = cpg_sd_clock_calc_div(clock, rate, parent_rate);
321         u32 val;
322         unsigned int i;
323
324         for (i = 0; i < clock->div_num; i++)
325                 if (div == clock->div_table[i].div)
326                         break;
327
328         if (i >= clock->div_num)
329                 return -EINVAL;
330
331         clock->cur_div_idx = i;
332
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);
337
338         return 0;
339 }
340
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,
348 };
349
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)
353 {
354         struct clk_init_data init;
355         struct sd_clock *clock;
356         struct clk *clk;
357         unsigned int i;
358         u32 sd_fc;
359
360         clock = kzalloc(sizeof(*clock), GFP_KERNEL);
361         if (!clock)
362                 return ERR_PTR(-ENOMEM);
363
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;
369
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);
374
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))
378                         break;
379
380         if (WARN_ON(i >= clock->div_num)) {
381                 kfree(clock);
382                 return ERR_PTR(-EINVAL);
383         }
384
385         clock->cur_div_idx = i;
386
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);
392         }
393
394         clk = clk_register(NULL, &clock->hw);
395         if (IS_ERR(clk))
396                 goto free_clock;
397
398         cpg_simple_notifier_register(notifiers, &clock->csn);
399         return clk;
400
401 free_clock:
402         kfree(clock);
403         return clk;
404 }
405
406
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;
411
412 #define PLL_ERRATA      BIT(0)          /* Missing PLL0/2/4 post-divider */
413 #define RCKCR_CKSEL     BIT(1)          /* Manual RCLK parent selection */
414
415 static const struct soc_device_attribute cpg_quirks_match[] __initconst = {
416         {
417                 .soc_id = "r8a7795", .revision = "ES1.0",
418                 .data = (void *)(PLL_ERRATA | RCKCR_CKSEL),
419         },
420         {
421                 .soc_id = "r8a7795", .revision = "ES1.*",
422                 .data = (void *)RCKCR_CKSEL,
423         },
424         {
425                 .soc_id = "r8a7796", .revision = "ES1.0",
426                 .data = (void *)RCKCR_CKSEL,
427         },
428         { /* sentinel */ }
429 };
430
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)
435 {
436         const struct clk *parent;
437         unsigned int mult = 1;
438         unsigned int div = 1;
439         u32 value;
440
441         parent = clks[core->parent & 0xffff];   /* CLK_TYPE_PE uses high bits */
442         if (IS_ERR(parent))
443                 return ERR_CAST(parent);
444
445         switch (core->type) {
446         case CLK_TYPE_GEN3_MAIN:
447                 div = cpg_pll_config->extal_div;
448                 break;
449
450         case CLK_TYPE_GEN3_PLL0:
451                 /*
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.
456                  */
457                 value = readl(base + CPG_PLL0CR);
458                 mult = (((value >> 24) & 0x7f) + 1) * 2;
459                 if (cpg_quirks & PLL_ERRATA)
460                         mult *= 2;
461                 break;
462
463         case CLK_TYPE_GEN3_PLL1:
464                 mult = cpg_pll_config->pll1_mult;
465                 div = cpg_pll_config->pll1_div;
466                 break;
467
468         case CLK_TYPE_GEN3_PLL2:
469                 /*
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.
474                  */
475                 value = readl(base + CPG_PLL2CR);
476                 mult = (((value >> 24) & 0x7f) + 1) * 2;
477                 if (cpg_quirks & PLL_ERRATA)
478                         mult *= 2;
479                 break;
480
481         case CLK_TYPE_GEN3_PLL3:
482                 mult = cpg_pll_config->pll3_mult;
483                 div = cpg_pll_config->pll3_div;
484                 break;
485
486         case CLK_TYPE_GEN3_PLL4:
487                 /*
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.
492                  */
493                 value = readl(base + CPG_PLL4CR);
494                 mult = (((value >> 24) & 0x7f) + 1) * 2;
495                 if (cpg_quirks & PLL_ERRATA)
496                         mult *= 2;
497                 break;
498
499         case CLK_TYPE_GEN3_SD:
500                 return cpg_sd_clk_register(core, base, __clk_get_name(parent),
501                                            notifiers);
502
503         case CLK_TYPE_GEN3_R:
504                 if (cpg_quirks & RCKCR_CKSEL) {
505                         struct cpg_simple_notifier *csn;
506
507                         csn = kzalloc(sizeof(*csn), GFP_KERNEL);
508                         if (!csn)
509                                 return ERR_PTR(-ENOMEM);
510
511                         csn->reg = base + CPG_RCKCR;
512
513                         /*
514                          * RINT is default.
515                          * Only if EXTALR is populated, we switch to it.
516                          */
517                         value = readl(csn->reg) & 0x3f;
518
519                         if (clk_get_rate(clks[cpg_clk_extalr])) {
520                                 parent = clks[cpg_clk_extalr];
521                                 value |= BIT(15);
522                         }
523
524                         writel(value, csn->reg);
525                         cpg_simple_notifier_register(notifiers, csn);
526                         break;
527                 }
528
529                 /* Select parent clock of RCLK by MD28 */
530                 if (cpg_mode & BIT(28))
531                         parent = clks[cpg_clk_extalr];
532                 break;
533
534         case CLK_TYPE_GEN3_PE:
535                 /*
536                  * Peripheral clock with a fixed divider, selectable between
537                  * clean and spread spectrum parents using MD12
538                  */
539                 if (cpg_mode & BIT(12)) {
540                         /* Clean */
541                         div = core->div & 0xffff;
542                 } else {
543                         /* SCCG */
544                         parent = clks[core->parent >> 16];
545                         if (IS_ERR(parent))
546                                 return ERR_CAST(parent);
547                         div = core->div >> 16;
548                 }
549                 mult = 1;
550                 break;
551
552         case CLK_TYPE_GEN3_Z:
553                 return cpg_z_clk_register(core->name, __clk_get_name(parent),
554                                           base);
555
556         default:
557                 return ERR_PTR(-EINVAL);
558         }
559
560         return clk_register_fixed_factor(NULL, core->name,
561                                          __clk_get_name(parent), 0, mult, div);
562 }
563
564 int __init rcar_gen3_cpg_init(const struct rcar_gen3_cpg_pll_config *config,
565                               unsigned int clk_extalr, u32 mode)
566 {
567         const struct soc_device_attribute *attr;
568
569         cpg_pll_config = config;
570         cpg_clk_extalr = clk_extalr;
571         cpg_mode = mode;
572         attr = soc_device_match(cpg_quirks_match);
573         if (attr)
574                 cpg_quirks = (uintptr_t)attr->data;
575         pr_debug("%s: mode = 0x%x quirks = 0x%x\n", __func__, mode, cpg_quirks);
576         return 0;
577 }