xref: /linux/drivers/clk/renesas/r7s9210-cpg-mssr.c (revision b9553c13b10e90c7ee3c1830b88d6af3cb4fd371)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * R7S9210 Clock Pulse Generator / Module Standby
4  *
5  * Based on r8a7795-cpg-mssr.c
6  *
7  * Copyright (C) 2018 Chris Brandt
8  * Copyright (C) 2018 Renesas Electronics Corp.
9  *
10  */
11 
12 #include <linux/clk.h>
13 #include <linux/clk-provider.h>
14 #include <dt-bindings/clock/r7s9210-cpg-mssr.h>
15 #include "renesas-cpg-mssr.h"
16 
17 #define CPG_FRQCR	0x00
18 
19 static u8 cpg_mode;
20 
21 /* Internal Clock ratio table */
22 static const struct {
23 	unsigned int i;
24 	unsigned int g;
25 	unsigned int b;
26 	unsigned int p1;
27 	/* p0 is always 32 */;
28 } ratio_tab[5] = {	/* I,  G,  B, P1 */
29 			{  2,  4,  8, 16},	/* FRQCR = 0x012 */
30 			{  4,  4,  8, 16},	/* FRQCR = 0x112 */
31 			{  8,  4,  8, 16},	/* FRQCR = 0x212 */
32 			{ 16,  8, 16, 16},	/* FRQCR = 0x322 */
33 			{ 16, 16, 32, 32},	/* FRQCR = 0x333 */
34 			};
35 
36 enum rz_clk_types {
37 	CLK_TYPE_RZA_MAIN = CLK_TYPE_CUSTOM,
38 	CLK_TYPE_RZA_PLL,
39 };
40 
41 enum clk_ids {
42 	/* Core Clock Outputs exported to DT */
43 	LAST_DT_CORE_CLK = R7S9210_CLK_P0,
44 
45 	/* External Input Clocks */
46 	CLK_EXTAL,
47 
48 	/* Internal Core Clocks */
49 	CLK_MAIN,
50 	CLK_PLL,
51 
52 	/* Module Clocks */
53 	MOD_CLK_BASE
54 };
55 
56 static struct cpg_core_clk r7s9210_early_core_clks[] = {
57 	/* External Clock Inputs */
58 	DEF_INPUT("extal",     CLK_EXTAL),
59 
60 	/* Internal Core Clocks */
61 	DEF_BASE(".main",       CLK_MAIN, CLK_TYPE_RZA_MAIN, CLK_EXTAL),
62 	DEF_BASE(".pll",       CLK_PLL, CLK_TYPE_RZA_PLL, CLK_MAIN),
63 
64 	/* Core Clock Outputs */
65 	DEF_FIXED("p1c",    R7S9210_CLK_P1C,   CLK_PLL,         16, 1),
66 };
67 
68 static const struct mssr_mod_clk r7s9210_early_mod_clks[] __initconst = {
69 	DEF_MOD_STB("ostm2",	 34,	R7S9210_CLK_P1C),
70 	DEF_MOD_STB("ostm1",	 35,	R7S9210_CLK_P1C),
71 	DEF_MOD_STB("ostm0",	 36,	R7S9210_CLK_P1C),
72 };
73 
74 static struct cpg_core_clk r7s9210_core_clks[] = {
75 	/* Core Clock Outputs */
76 	DEF_FIXED("i",      R7S9210_CLK_I,     CLK_PLL,          2, 1),
77 	DEF_FIXED("g",      R7S9210_CLK_G,     CLK_PLL,          4, 1),
78 	DEF_FIXED("b",      R7S9210_CLK_B,     CLK_PLL,          8, 1),
79 	DEF_FIXED("p1",     R7S9210_CLK_P1,    CLK_PLL,         16, 1),
80 	DEF_FIXED("p0",     R7S9210_CLK_P0,    CLK_PLL,         32, 1),
81 };
82 
83 static const struct mssr_mod_clk r7s9210_mod_clks[] __initconst = {
84 	DEF_MOD_STB("scif4",	 43,	R7S9210_CLK_P1C),
85 	DEF_MOD_STB("scif3",	 44,	R7S9210_CLK_P1C),
86 	DEF_MOD_STB("scif2",	 45,	R7S9210_CLK_P1C),
87 	DEF_MOD_STB("scif1",	 46,	R7S9210_CLK_P1C),
88 	DEF_MOD_STB("scif0",	 47,	R7S9210_CLK_P1C),
89 
90 	DEF_MOD_STB("ether1",	 64,	R7S9210_CLK_B),
91 	DEF_MOD_STB("ether0",	 65,	R7S9210_CLK_B),
92 
93 	DEF_MOD_STB("i2c3",	 84,	R7S9210_CLK_P1),
94 	DEF_MOD_STB("i2c2",	 85,	R7S9210_CLK_P1),
95 	DEF_MOD_STB("i2c1",	 86,	R7S9210_CLK_P1),
96 	DEF_MOD_STB("i2c0",	 87,	R7S9210_CLK_P1),
97 
98 };
99 
100 struct clk * __init rza2_cpg_clk_register(struct device *dev,
101 	const struct cpg_core_clk *core, const struct cpg_mssr_info *info,
102 	struct clk **clks, void __iomem *base,
103 	struct raw_notifier_head *notifiers)
104 {
105 	struct clk *parent;
106 	unsigned int mult = 1;
107 	unsigned int div = 1;
108 	u16 frqcr;
109 	u8 index;
110 	int i;
111 
112 	parent = clks[core->parent];
113 	if (IS_ERR(parent))
114 		return ERR_CAST(parent);
115 
116 	switch (core->id) {
117 	case CLK_MAIN:
118 		break;
119 
120 	case CLK_PLL:
121 		if (cpg_mode)
122 			mult = 44;	/* Divider 1 is 1/2 */
123 		else
124 			mult = 88;	/* Divider 1 is 1 */
125 		break;
126 
127 	default:
128 		return ERR_PTR(-EINVAL);
129 	}
130 
131 	/* Adjust the dividers based on the current FRQCR setting */
132 	if (core->id == CLK_MAIN) {
133 
134 		/* If EXTAL is above 12MHz, then we know it is Mode 1 */
135 		if (clk_get_rate(parent) > 12000000)
136 			cpg_mode = 1;
137 
138 		frqcr = clk_readl(base + CPG_FRQCR) & 0xFFF;
139 		if (frqcr == 0x012)
140 			index = 0;
141 		else if (frqcr == 0x112)
142 			index = 1;
143 		else if (frqcr == 0x212)
144 			index = 2;
145 		else if (frqcr == 0x322)
146 			index = 3;
147 		else if (frqcr == 0x333)
148 			index = 4;
149 		else
150 			BUG_ON(1);	/* Illegal FRQCR value */
151 
152 		for (i = 0; i < ARRAY_SIZE(r7s9210_core_clks); i++) {
153 			switch (r7s9210_core_clks[i].id) {
154 			case R7S9210_CLK_I:
155 				r7s9210_core_clks[i].div = ratio_tab[index].i;
156 				break;
157 			case R7S9210_CLK_G:
158 				r7s9210_core_clks[i].div = ratio_tab[index].g;
159 				break;
160 			case R7S9210_CLK_B:
161 				r7s9210_core_clks[i].div = ratio_tab[index].b;
162 				break;
163 			case R7S9210_CLK_P1:
164 			case R7S9210_CLK_P1C:
165 				r7s9210_core_clks[i].div = ratio_tab[index].p1;
166 				break;
167 			case R7S9210_CLK_P0:
168 				r7s9210_core_clks[i].div = 32;
169 				break;
170 			}
171 		}
172 	}
173 
174 	return clk_register_fixed_factor(NULL, core->name,
175 					 __clk_get_name(parent), 0, mult, div);
176 }
177 
178 const struct cpg_mssr_info r7s9210_cpg_mssr_info __initconst = {
179 	/* Early Clocks */
180 	.early_core_clks = r7s9210_early_core_clks,
181 	.num_early_core_clks = ARRAY_SIZE(r7s9210_early_core_clks),
182 	.early_mod_clks = r7s9210_early_mod_clks,
183 	.num_early_mod_clks = ARRAY_SIZE(r7s9210_early_mod_clks),
184 
185 	/* Core Clocks */
186 	.core_clks = r7s9210_core_clks,
187 	.num_core_clks = ARRAY_SIZE(r7s9210_core_clks),
188 	.last_dt_core_clk = LAST_DT_CORE_CLK,
189 	.num_total_core_clks = MOD_CLK_BASE,
190 
191 	/* Module Clocks */
192 	.mod_clks = r7s9210_mod_clks,
193 	.num_mod_clks = ARRAY_SIZE(r7s9210_mod_clks),
194 	.num_hw_mod_clks = 11 * 32, /* includes STBCR0 which doesn't exist */
195 
196 	/* Callbacks */
197 	.cpg_clk_register = rza2_cpg_clk_register,
198 
199 	/* RZ/A2 has Standby Control Registers */
200 	.stbyctrl = true,
201 };
202 
203 static void __init r7s9210_cpg_mssr_early_init(struct device_node *np)
204 {
205 	cpg_mssr_early_init(np, &r7s9210_cpg_mssr_info);
206 }
207 
208 CLK_OF_DECLARE_DRIVER(cpg_mstp_clks, "renesas,r7s9210-cpg-mssr",
209 		      r7s9210_cpg_mssr_early_init);
210