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/clkdev.h>
13 #include <linux/clk/shmobile.h>
14 #include <linux/init.h>
15 #include <linux/kernel.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
= clk_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
= clk_readl(cpg
->reg
+ CPG_PLL0CR
);
100 parent_name
= "main";
101 mult
= ((value
>> 24) & 0x7f) + 1;
104 } else if (!strcmp(name
, "pll1")) {
105 u32 value
= clk_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
= clk_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 - ((clk_readl(cpg
->reg
+ CPG_FRQCRC
) >> shift
)
169 for (c
= div4_clks
; c
->name
; c
++) {
170 if (!strcmp(name
, c
->name
))
174 return ERR_PTR(-EINVAL
);
176 parent_name
= "pll1";
177 table
= div4_div_table
;
183 return clk_register_fixed_factor(NULL
, name
, parent_name
, 0,
186 return clk_register_divider_table(NULL
, name
, parent_name
, 0,
187 cpg
->reg
+ reg
, shift
, 4, 0,
192 static void __init
r8a73a4_cpg_clocks_init(struct device_node
*np
)
194 struct r8a73a4_cpg
*cpg
;
199 num_clks
= of_property_count_strings(np
, "clock-output-names");
201 pr_err("%s: failed to count clocks\n", __func__
);
205 cpg
= kzalloc(sizeof(*cpg
), GFP_KERNEL
);
206 clks
= kcalloc(num_clks
, sizeof(*clks
), GFP_KERNEL
);
207 if (cpg
== NULL
|| clks
== NULL
) {
208 /* We're leaking memory on purpose, there's no point in cleaning
209 * up as the system won't boot anyway.
214 spin_lock_init(&cpg
->lock
);
216 cpg
->data
.clks
= clks
;
217 cpg
->data
.clk_num
= num_clks
;
219 cpg
->reg
= of_iomap(np
, 0);
220 if (WARN_ON(cpg
->reg
== NULL
))
223 for (i
= 0; i
< num_clks
; ++i
) {
227 of_property_read_string_index(np
, "clock-output-names", i
,
230 clk
= r8a73a4_cpg_register_clock(np
, cpg
, name
);
232 pr_err("%s: failed to register %s %s clock (%ld)\n",
233 __func__
, np
->name
, name
, PTR_ERR(clk
));
235 cpg
->data
.clks
[i
] = clk
;
238 of_clk_add_provider(np
, of_clk_src_onecell_get
, &cpg
->data
);
240 CLK_OF_DECLARE(r8a73a4_cpg_clks
, "renesas,r8a73a4-cpg-clocks",
241 r8a73a4_cpg_clocks_init
);