1 // SPDX-License-Identifier: GPL-2.0
3 * sh73a0 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>
14 #include <linux/of_address.h>
15 #include <linux/slab.h>
16 #include <linux/spinlock.h>
19 struct clk_onecell_data data
;
24 #define CPG_FRQCRA 0x00
25 #define CPG_FRQCRB 0x04
26 #define CPG_SD0CKCR 0x74
27 #define CPG_SD1CKCR 0x78
28 #define CPG_SD2CKCR 0x7c
29 #define CPG_PLLECR 0xd0
30 #define CPG_PLL0CR 0xd8
31 #define CPG_PLL1CR 0x28
32 #define CPG_PLL2CR 0x2c
33 #define CPG_PLL3CR 0xdc
34 #define CPG_CKSCR 0xc0
35 #define CPG_DSI0PHYCR 0x6c
36 #define CPG_DSI1PHYCR 0x70
38 #define CLK_ENABLE_ON_INIT BIT(0)
47 static const struct div4_clk div4_clks
[] = {
48 { "zg", "pll0", CPG_FRQCRA
, 16 },
49 { "m3", "pll1", CPG_FRQCRA
, 12 },
50 { "b", "pll1", CPG_FRQCRA
, 8 },
51 { "m1", "pll1", CPG_FRQCRA
, 4 },
52 { "m2", "pll1", CPG_FRQCRA
, 0 },
53 { "zx", "pll1", CPG_FRQCRB
, 12 },
54 { "hp", "pll1", CPG_FRQCRB
, 4 },
58 static const struct clk_div_table div4_div_table
[] = {
59 { 0, 2 }, { 1, 3 }, { 2, 4 }, { 3, 6 }, { 4, 8 }, { 5, 12 },
60 { 6, 16 }, { 7, 18 }, { 8, 24 }, { 10, 36 }, { 11, 48 },
64 static const struct clk_div_table z_div_table
[] = {
66 { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }, { 4, 1 }, { 5, 1 },
67 { 6, 1 }, { 7, 1 }, { 8, 1 }, { 9, 1 }, { 10, 1 }, { 11, 1 },
68 { 12, 1 }, { 13, 1 }, { 14, 1 }, { 15, 1 },
70 { 16, 2 }, { 17, 3 }, { 18, 4 }, { 19, 6 }, { 20, 8 }, { 21, 12 },
71 { 22, 16 }, { 24, 24 }, { 27, 48 }, { 0, 0 }
74 static struct clk
* __init
75 sh73a0_cpg_register_clock(struct device_node
*np
, struct sh73a0_cpg
*cpg
,
78 const struct clk_div_table
*table
= NULL
;
79 unsigned int shift
, reg
, width
;
80 const char *parent_name
= NULL
;
81 unsigned int mult
= 1;
84 if (!strcmp(name
, "main")) {
85 /* extal1, extal1_div2, extal2, extal2_div2 */
86 u32 parent_idx
= (readl(cpg
->reg
+ CPG_CKSCR
) >> 28) & 3;
88 parent_name
= of_clk_get_parent_name(np
, parent_idx
>> 1);
89 div
= (parent_idx
& 1) + 1;
90 } else if (!strncmp(name
, "pll", 3)) {
91 void __iomem
*enable_reg
= cpg
->reg
;
92 u32 enable_bit
= name
[3] - '0';
97 enable_reg
+= CPG_PLL0CR
;
100 enable_reg
+= CPG_PLL1CR
;
103 enable_reg
+= CPG_PLL2CR
;
106 enable_reg
+= CPG_PLL3CR
;
109 return ERR_PTR(-EINVAL
);
111 if (readl(cpg
->reg
+ CPG_PLLECR
) & BIT(enable_bit
)) {
112 mult
= ((readl(enable_reg
) >> 24) & 0x3f) + 1;
113 /* handle CFG bit for PLL1 and PLL2 */
114 if (enable_bit
== 1 || enable_bit
== 2)
115 if (readl(enable_reg
) & BIT(20))
118 } else if (!strcmp(name
, "dsi0phy") || !strcmp(name
, "dsi1phy")) {
119 u32 phy_no
= name
[3] - '0';
120 void __iomem
*dsi_reg
= cpg
->reg
+
121 (phy_no
? CPG_DSI1PHYCR
: CPG_DSI0PHYCR
);
123 parent_name
= phy_no
? "dsi1pck" : "dsi0pck";
124 mult
= __raw_readl(dsi_reg
);
125 if (!(mult
& 0x8000))
128 mult
= (mult
& 0x3f) + 1;
129 } else if (!strcmp(name
, "z")) {
130 parent_name
= "pll0";
136 const struct div4_clk
*c
;
138 for (c
= div4_clks
; c
->name
; c
++) {
139 if (!strcmp(name
, c
->name
)) {
140 parent_name
= c
->parent
;
141 table
= div4_div_table
;
149 return ERR_PTR(-EINVAL
);
153 return clk_register_fixed_factor(NULL
, name
, parent_name
, 0,
156 return clk_register_divider_table(NULL
, name
, parent_name
, 0,
157 cpg
->reg
+ reg
, shift
, width
, 0,
162 static void __init
sh73a0_cpg_clocks_init(struct device_node
*np
)
164 struct sh73a0_cpg
*cpg
;
169 num_clks
= of_property_count_strings(np
, "clock-output-names");
171 pr_err("%s: failed to count clocks\n", __func__
);
175 cpg
= kzalloc(sizeof(*cpg
), GFP_KERNEL
);
176 clks
= kcalloc(num_clks
, sizeof(*clks
), GFP_KERNEL
);
177 if (cpg
== NULL
|| clks
== NULL
) {
178 /* We're leaking memory on purpose, there's no point in cleaning
179 * up as the system won't boot anyway.
184 spin_lock_init(&cpg
->lock
);
186 cpg
->data
.clks
= clks
;
187 cpg
->data
.clk_num
= num_clks
;
189 cpg
->reg
= of_iomap(np
, 0);
190 if (WARN_ON(cpg
->reg
== NULL
))
193 /* Set SDHI clocks to a known state */
194 writel(0x108, cpg
->reg
+ CPG_SD0CKCR
);
195 writel(0x108, cpg
->reg
+ CPG_SD1CKCR
);
196 writel(0x108, cpg
->reg
+ CPG_SD2CKCR
);
198 for (i
= 0; i
< num_clks
; ++i
) {
202 of_property_read_string_index(np
, "clock-output-names", i
,
205 clk
= sh73a0_cpg_register_clock(np
, cpg
, name
);
207 pr_err("%s: failed to register %pOFn %s clock (%ld)\n",
208 __func__
, np
, name
, PTR_ERR(clk
));
210 cpg
->data
.clks
[i
] = clk
;
213 of_clk_add_provider(np
, of_clk_src_onecell_get
, &cpg
->data
);
215 CLK_OF_DECLARE(sh73a0_cpg_clks
, "renesas,sh73a0-cpg-clocks",
216 sh73a0_cpg_clocks_init
);