1 // SPDX-License-Identifier: GPL-2.0
3 * r8a73a4 Core CPG Clocks
5 * Copyright (C) 2014 Ulrich Hecht
8 #include <linux/clk-provider.h>
9 #include <linux/clk/renesas.h>
10 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/of_address.h>
16 #include <linux/spinlock.h>
19 struct clk_onecell_data data
;
23 #define CPG_CKSCR 0xc0
24 #define CPG_FRQCRA 0x00
25 #define CPG_FRQCRB 0x04
26 #define CPG_FRQCRC 0xe0
27 #define CPG_PLL0CR 0xd8
28 #define CPG_PLL1CR 0x28
29 #define CPG_PLL2CR 0x2c
30 #define CPG_PLL2HCR 0xe4
31 #define CPG_PLL2SCR 0xf4
39 static struct div4_clk div4_clks
[] = {
40 { "i", CPG_FRQCRA
, 20 },
41 { "m3", CPG_FRQCRA
, 12 },
42 { "b", CPG_FRQCRA
, 8 },
43 { "m1", CPG_FRQCRA
, 4 },
44 { "m2", CPG_FRQCRA
, 0 },
45 { "zx", CPG_FRQCRB
, 12 },
46 { "zs", CPG_FRQCRB
, 8 },
47 { "hp", CPG_FRQCRB
, 4 },
51 static const struct clk_div_table div4_div_table
[] = {
52 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 12 },
53 { 6, 16 }, { 7, 18 }, { 8, 24 }, { 10, 36 }, { 11, 48 },
57 static struct clk
* __init
58 r8a73a4_cpg_register_clock(struct device_node
*np
, struct r8a73a4_cpg
*cpg
,
59 void __iomem
*base
, const char *name
)
61 const struct clk_div_table
*table
= NULL
;
62 const char *parent_name
;
63 unsigned int shift
, reg
;
64 unsigned int mult
= 1;
67 if (!strcmp(name
, "main")) {
68 u32 ckscr
= readl(base
+ CPG_CKSCR
);
70 switch ((ckscr
>> 28) & 3) {
72 parent_name
= of_clk_get_parent_name(np
, 0);
74 case 1: /* extal1 / 2 */
75 parent_name
= of_clk_get_parent_name(np
, 0);
79 parent_name
= of_clk_get_parent_name(np
, 1);
81 case 3: /* extal2 / 2 */
82 parent_name
= of_clk_get_parent_name(np
, 1);
86 } else if (!strcmp(name
, "pll0")) {
87 /* PLL0/1 are configurable multiplier clocks. Register them as
88 * fixed factor clocks for now as there's no generic multiplier
89 * clock implementation and we currently have no need to change
90 * the multiplier value.
92 u32 value
= readl(base
+ CPG_PLL0CR
);
95 mult
= ((value
>> 24) & 0x7f) + 1;
98 } else if (!strcmp(name
, "pll1")) {
99 u32 value
= readl(base
+ CPG_PLL1CR
);
101 parent_name
= "main";
102 /* XXX: enable bit? */
103 mult
= ((value
>> 24) & 0x7f) + 1;
106 } else if (!strncmp(name
, "pll2", 4)) {
120 return ERR_PTR(-EINVAL
);
122 value
= readl(base
+ cr
);
123 switch ((value
>> 5) & 7) {
125 parent_name
= "main";
129 parent_name
= "extal2";
133 parent_name
= "extal2";
137 parent_name
= "main";
140 parent_name
= "extal2";
143 pr_warn("%s: unexpected parent of %s\n", __func__
,
145 return ERR_PTR(-EINVAL
);
147 /* XXX: enable bit? */
148 mult
= ((value
>> 24) & 0x7f) + 1;
149 } else if (!strcmp(name
, "z") || !strcmp(name
, "z2")) {
152 parent_name
= "pll0";
153 if (name
[1] == '2') {
158 mult
= 0x20 - ((readl(base
+ CPG_FRQCRC
) >> shift
) & 0x1f);
162 for (c
= div4_clks
; c
->name
; c
++) {
163 if (!strcmp(name
, c
->name
))
167 return ERR_PTR(-EINVAL
);
169 parent_name
= "pll1";
170 table
= div4_div_table
;
176 return clk_register_fixed_factor(NULL
, name
, parent_name
, 0,
179 return clk_register_divider_table(NULL
, name
, parent_name
, 0,
180 base
+ reg
, shift
, 4, 0,
185 static void __init
r8a73a4_cpg_clocks_init(struct device_node
*np
)
187 struct r8a73a4_cpg
*cpg
;
193 num_clks
= of_property_count_strings(np
, "clock-output-names");
195 pr_err("%s: failed to count clocks\n", __func__
);
199 cpg
= kzalloc(sizeof(*cpg
), GFP_KERNEL
);
200 clks
= kcalloc(num_clks
, sizeof(*clks
), GFP_KERNEL
);
201 if (cpg
== NULL
|| clks
== NULL
) {
202 /* We're leaking memory on purpose, there's no point in cleaning
203 * up as the system won't boot anyway.
208 spin_lock_init(&cpg
->lock
);
210 cpg
->data
.clks
= clks
;
211 cpg
->data
.clk_num
= num_clks
;
213 base
= of_iomap(np
, 0);
214 if (WARN_ON(base
== NULL
))
217 for (i
= 0; i
< num_clks
; ++i
) {
221 of_property_read_string_index(np
, "clock-output-names", i
,
224 clk
= r8a73a4_cpg_register_clock(np
, cpg
, base
, name
);
226 pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
227 __func__
, np
, name
, PTR_ERR(clk
));
229 cpg
->data
.clks
[i
] = clk
;
232 of_clk_add_provider(np
, of_clk_src_onecell_get
, &cpg
->data
);
234 CLK_OF_DECLARE(r8a73a4_cpg_clks
, "renesas,r8a73a4-cpg-clocks",
235 r8a73a4_cpg_clocks_init
);