2 * r8a73a4 Core CPG Clocks
4 * Copyright (C) 2014 Ulrich Hecht
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
11 #include <linux/clk-provider.h>
12 #include <linux/clk/renesas.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
17 #include <linux/of_address.h>
18 #include <linux/spinlock.h>
21 struct clk_onecell_data data
;
26 #define CPG_CKSCR 0xc0
27 #define CPG_FRQCRA 0x00
28 #define CPG_FRQCRB 0x04
29 #define CPG_FRQCRC 0xe0
30 #define CPG_PLL0CR 0xd8
31 #define CPG_PLL1CR 0x28
32 #define CPG_PLL2CR 0x2c
33 #define CPG_PLL2HCR 0xe4
34 #define CPG_PLL2SCR 0xf4
36 #define CLK_ENABLE_ON_INIT BIT(0)
44 static struct div4_clk div4_clks
[] = {
45 { "i", CPG_FRQCRA
, 20 },
46 { "m3", CPG_FRQCRA
, 12 },
47 { "b", CPG_FRQCRA
, 8 },
48 { "m1", CPG_FRQCRA
, 4 },
49 { "m2", CPG_FRQCRA
, 0 },
50 { "zx", CPG_FRQCRB
, 12 },
51 { "zs", CPG_FRQCRB
, 8 },
52 { "hp", CPG_FRQCRB
, 4 },
56 static const struct clk_div_table div4_div_table
[] = {
57 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 12 },
58 { 6, 16 }, { 7, 18 }, { 8, 24 }, { 10, 36 }, { 11, 48 },
62 static struct clk
* __init
63 r8a73a4_cpg_register_clock(struct device_node
*np
, struct r8a73a4_cpg
*cpg
,
66 const struct clk_div_table
*table
= NULL
;
67 const char *parent_name
;
68 unsigned int shift
, reg
;
69 unsigned int mult
= 1;
73 if (!strcmp(name
, "main")) {
74 u32 ckscr
= readl(cpg
->reg
+ CPG_CKSCR
);
76 switch ((ckscr
>> 28) & 3) {
78 parent_name
= of_clk_get_parent_name(np
, 0);
80 case 1: /* extal1 / 2 */
81 parent_name
= of_clk_get_parent_name(np
, 0);
85 parent_name
= of_clk_get_parent_name(np
, 1);
87 case 3: /* extal2 / 2 */
88 parent_name
= of_clk_get_parent_name(np
, 1);
92 } else if (!strcmp(name
, "pll0")) {
93 /* PLL0/1 are configurable multiplier clocks. Register them as
94 * fixed factor clocks for now as there's no generic multiplier
95 * clock implementation and we currently have no need to change
96 * the multiplier value.
98 u32 value
= readl(cpg
->reg
+ CPG_PLL0CR
);
100 parent_name
= "main";
101 mult
= ((value
>> 24) & 0x7f) + 1;
104 } else if (!strcmp(name
, "pll1")) {
105 u32 value
= readl(cpg
->reg
+ CPG_PLL1CR
);
107 parent_name
= "main";
108 /* XXX: enable bit? */
109 mult
= ((value
>> 24) & 0x7f) + 1;
112 } else if (!strncmp(name
, "pll2", 4)) {
126 return ERR_PTR(-EINVAL
);
128 value
= readl(cpg
->reg
+ cr
);
129 switch ((value
>> 5) & 7) {
131 parent_name
= "main";
135 parent_name
= "extal2";
139 parent_name
= "extal2";
143 parent_name
= "main";
146 parent_name
= "extal2";
149 pr_warn("%s: unexpected parent of %s\n", __func__
,
151 return ERR_PTR(-EINVAL
);
153 /* XXX: enable bit? */
154 mult
= ((value
>> 24) & 0x7f) + 1;
155 } else if (!strcmp(name
, "z") || !strcmp(name
, "z2")) {
158 parent_name
= "pll0";
159 if (name
[1] == '2') {
164 mult
= 0x20 - ((readl(cpg
->reg
+ CPG_FRQCRC
) >> shift
) & 0x1f);
168 for (c
= div4_clks
; c
->name
; c
++) {
169 if (!strcmp(name
, c
->name
))
173 return ERR_PTR(-EINVAL
);
175 parent_name
= "pll1";
176 table
= div4_div_table
;
182 return clk_register_fixed_factor(NULL
, name
, parent_name
, 0,
185 return clk_register_divider_table(NULL
, name
, parent_name
, 0,
186 cpg
->reg
+ reg
, shift
, 4, 0,
191 static void __init
r8a73a4_cpg_clocks_init(struct device_node
*np
)
193 struct r8a73a4_cpg
*cpg
;
198 num_clks
= of_property_count_strings(np
, "clock-output-names");
200 pr_err("%s: failed to count clocks\n", __func__
);
204 cpg
= kzalloc(sizeof(*cpg
), GFP_KERNEL
);
205 clks
= kcalloc(num_clks
, sizeof(*clks
), GFP_KERNEL
);
206 if (cpg
== NULL
|| clks
== NULL
) {
207 /* We're leaking memory on purpose, there's no point in cleaning
208 * up as the system won't boot anyway.
213 spin_lock_init(&cpg
->lock
);
215 cpg
->data
.clks
= clks
;
216 cpg
->data
.clk_num
= num_clks
;
218 cpg
->reg
= of_iomap(np
, 0);
219 if (WARN_ON(cpg
->reg
== NULL
))
222 for (i
= 0; i
< num_clks
; ++i
) {
226 of_property_read_string_index(np
, "clock-output-names", i
,
229 clk
= r8a73a4_cpg_register_clock(np
, cpg
, name
);
231 pr_err("%s: failed to register %s %s clock (%ld)\n",
232 __func__
, np
->name
, name
, PTR_ERR(clk
));
234 cpg
->data
.clks
[i
] = clk
;
237 of_clk_add_provider(np
, of_clk_src_onecell_get
, &cpg
->data
);
239 CLK_OF_DECLARE(r8a73a4_cpg_clks
, "renesas,r8a73a4-cpg-clocks",
240 r8a73a4_cpg_clocks_init
);