1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Renesas RZ/V2H(P) Clock Pulse Generator
4  *
5  * Copyright (C) 2024 Renesas Electronics Corp.
6  */
7 
8 #ifndef __RENESAS_RZV2H_CPG_H__
9 #define __RENESAS_RZV2H_CPG_H__
10 
11 #include <linux/bitfield.h>
12 
13 /**
14  * struct ddiv - Structure for dynamic switching divider
15  *
16  * @offset: register offset
17  * @shift: position of the divider bit
18  * @width: width of the divider
19  * @monbit: monitor bit in CPG_CLKSTATUS0 register
20  */
21 struct ddiv {
22 	unsigned int offset:11;
23 	unsigned int shift:4;
24 	unsigned int width:4;
25 	unsigned int monbit:5;
26 };
27 
28 #define DDIV_PACK(_offset, _shift, _width, _monbit) \
29 	((struct ddiv){ \
30 		.offset = _offset, \
31 		.shift = _shift, \
32 		.width = _width, \
33 		.monbit = _monbit \
34 	})
35 
36 #define CPG_CDDIV0		(0x400)
37 #define CPG_CDDIV1		(0x404)
38 #define CPG_CDDIV3		(0x40C)
39 #define CPG_CDDIV4		(0x410)
40 
41 #define CDDIV0_DIVCTL1	DDIV_PACK(CPG_CDDIV0, 4, 3, 1)
42 #define CDDIV0_DIVCTL2	DDIV_PACK(CPG_CDDIV0, 8, 3, 2)
43 #define CDDIV1_DIVCTL0	DDIV_PACK(CPG_CDDIV1, 0, 2, 4)
44 #define CDDIV1_DIVCTL1	DDIV_PACK(CPG_CDDIV1, 4, 2, 5)
45 #define CDDIV1_DIVCTL2	DDIV_PACK(CPG_CDDIV1, 8, 2, 6)
46 #define CDDIV1_DIVCTL3	DDIV_PACK(CPG_CDDIV1, 12, 2, 7)
47 #define CDDIV3_DIVCTL2	DDIV_PACK(CPG_CDDIV3, 8, 3, 14)
48 #define CDDIV3_DIVCTL3	DDIV_PACK(CPG_CDDIV3, 12, 1, 15)
49 #define CDDIV4_DIVCTL0	DDIV_PACK(CPG_CDDIV4, 0, 1, 16)
50 #define CDDIV4_DIVCTL1	DDIV_PACK(CPG_CDDIV4, 4, 1, 17)
51 #define CDDIV4_DIVCTL2	DDIV_PACK(CPG_CDDIV4, 8, 1, 18)
52 
53 #define BUS_MSTOP_IDX_MASK	GENMASK(31, 16)
54 #define BUS_MSTOP_BITS_MASK	GENMASK(15, 0)
55 #define BUS_MSTOP(idx, mask)	(FIELD_PREP_CONST(BUS_MSTOP_IDX_MASK, (idx)) | \
56 				 FIELD_PREP_CONST(BUS_MSTOP_BITS_MASK, (mask)))
57 #define BUS_MSTOP_NONE		GENMASK(31, 0)
58 
59 /**
60  * Definitions of CPG Core Clocks
61  *
62  * These include:
63  *   - Clock outputs exported to DT
64  *   - External input clocks
65  *   - Internal CPG clocks
66  */
67 struct cpg_core_clk {
68 	const char *name;
69 	unsigned int id;
70 	unsigned int parent;
71 	unsigned int div;
72 	unsigned int mult;
73 	unsigned int type;
74 	union {
75 		unsigned int conf;
76 		struct ddiv ddiv;
77 	} cfg;
78 	const struct clk_div_table *dtable;
79 	u32 flag;
80 };
81 
82 enum clk_types {
83 	/* Generic */
84 	CLK_TYPE_IN,		/* External Clock Input */
85 	CLK_TYPE_FF,		/* Fixed Factor Clock */
86 	CLK_TYPE_PLL,
87 	CLK_TYPE_DDIV,		/* Dynamic Switching Divider */
88 };
89 
90 /* BIT(31) indicates if CLK1/2 are accessible or not */
91 #define PLL_CONF(n)		(BIT(31) | ((n) & ~GENMASK(31, 16)))
92 #define PLL_CLK_ACCESS(n)	((n) & BIT(31) ? 1 : 0)
93 #define PLL_CLK1_OFFSET(n)	((n) & ~GENMASK(31, 16))
94 #define PLL_CLK2_OFFSET(n)	(((n) & ~GENMASK(31, 16)) + (0x4))
95 
96 #define DEF_TYPE(_name, _id, _type...) \
97 	{ .name = _name, .id = _id, .type = _type }
98 #define DEF_BASE(_name, _id, _type, _parent...) \
99 	DEF_TYPE(_name, _id, _type, .parent = _parent)
100 #define DEF_PLL(_name, _id, _parent, _conf) \
101 	DEF_TYPE(_name, _id, CLK_TYPE_PLL, .parent = _parent, .cfg.conf = _conf)
102 #define DEF_INPUT(_name, _id) \
103 	DEF_TYPE(_name, _id, CLK_TYPE_IN)
104 #define DEF_FIXED(_name, _id, _parent, _mult, _div) \
105 	DEF_BASE(_name, _id, CLK_TYPE_FF, _parent, .div = _div, .mult = _mult)
106 #define DEF_DDIV(_name, _id, _parent, _ddiv_packed, _dtable) \
107 	DEF_TYPE(_name, _id, CLK_TYPE_DDIV, \
108 		.cfg.ddiv = _ddiv_packed, \
109 		.parent = _parent, \
110 		.dtable = _dtable, \
111 		.flag = CLK_DIVIDER_HIWORD_MASK)
112 
113 /**
114  * struct rzv2h_mod_clk - Module Clocks definitions
115  *
116  * @name: handle between common and hardware-specific interfaces
117  * @mstop_data: packed data mstop register offset and mask
118  * @parent: id of parent clock
119  * @critical: flag to indicate the clock is critical
120  * @no_pm: flag to indicate PM is not supported
121  * @on_index: control register index
122  * @on_bit: ON bit
123  * @mon_index: monitor register index
124  * @mon_bit: monitor bit
125  */
126 struct rzv2h_mod_clk {
127 	const char *name;
128 	u32 mstop_data;
129 	u16 parent;
130 	bool critical;
131 	bool no_pm;
132 	u8 on_index;
133 	u8 on_bit;
134 	s8 mon_index;
135 	u8 mon_bit;
136 };
137 
138 #define DEF_MOD_BASE(_name, _mstop, _parent, _critical, _no_pm, _onindex, _onbit, _monindex, _monbit) \
139 	{ \
140 		.name = (_name), \
141 		.mstop_data = (_mstop), \
142 		.parent = (_parent), \
143 		.critical = (_critical), \
144 		.no_pm = (_no_pm), \
145 		.on_index = (_onindex), \
146 		.on_bit = (_onbit), \
147 		.mon_index = (_monindex), \
148 		.mon_bit = (_monbit), \
149 	}
150 
151 #define DEF_MOD(_name, _parent, _onindex, _onbit, _monindex, _monbit, _mstop) \
152 	DEF_MOD_BASE(_name, _mstop, _parent, false, false, _onindex, _onbit, _monindex, _monbit)
153 
154 #define DEF_MOD_CRITICAL(_name, _parent, _onindex, _onbit, _monindex, _monbit, _mstop) \
155 	DEF_MOD_BASE(_name, _mstop, _parent, true, false, _onindex, _onbit, _monindex, _monbit)
156 
157 #define DEF_MOD_NO_PM(_name, _parent, _onindex, _onbit, _monindex, _monbit, _mstop) \
158 	DEF_MOD_BASE(_name, _mstop, _parent, false, true, _onindex, _onbit, _monindex, _monbit)
159 
160 /**
161  * struct rzv2h_reset - Reset definitions
162  *
163  * @reset_index: reset register index
164  * @reset_bit: reset bit
165  * @mon_index: monitor register index
166  * @mon_bit: monitor bit
167  */
168 struct rzv2h_reset {
169 	u8 reset_index;
170 	u8 reset_bit;
171 	u8 mon_index;
172 	u8 mon_bit;
173 };
174 
175 #define DEF_RST_BASE(_resindex, _resbit, _monindex, _monbit)	\
176 	{ \
177 		.reset_index = (_resindex), \
178 		.reset_bit = (_resbit), \
179 		.mon_index = (_monindex), \
180 		.mon_bit = (_monbit), \
181 	}
182 
183 #define DEF_RST(_resindex, _resbit, _monindex, _monbit)	\
184 	DEF_RST_BASE(_resindex, _resbit, _monindex, _monbit)
185 
186 /**
187  * struct rzv2h_cpg_info - SoC-specific CPG Description
188  *
189  * @core_clks: Array of Core Clock definitions
190  * @num_core_clks: Number of entries in core_clks[]
191  * @last_dt_core_clk: ID of the last Core Clock exported to DT
192  * @num_total_core_clks: Total number of Core Clocks (exported + internal)
193  *
194  * @mod_clks: Array of Module Clock definitions
195  * @num_mod_clks: Number of entries in mod_clks[]
196  * @num_hw_mod_clks: Number of Module Clocks supported by the hardware
197  *
198  * @resets: Array of Module Reset definitions
199  * @num_resets: Number of entries in resets[]
200  *
201  * @num_mstop_bits: Maximum number of MSTOP bits supported, equivalent to the
202  *		    number of CPG_BUS_m_MSTOP registers multiplied by 16.
203  */
204 struct rzv2h_cpg_info {
205 	/* Core Clocks */
206 	const struct cpg_core_clk *core_clks;
207 	unsigned int num_core_clks;
208 	unsigned int last_dt_core_clk;
209 	unsigned int num_total_core_clks;
210 
211 	/* Module Clocks */
212 	const struct rzv2h_mod_clk *mod_clks;
213 	unsigned int num_mod_clks;
214 	unsigned int num_hw_mod_clks;
215 
216 	/* Resets */
217 	const struct rzv2h_reset *resets;
218 	unsigned int num_resets;
219 
220 	unsigned int num_mstop_bits;
221 };
222 
223 extern const struct rzv2h_cpg_info r9a09g047_cpg_info;
224 extern const struct rzv2h_cpg_info r9a09g057_cpg_info;
225 
226 #endif	/* __RENESAS_RZV2H_CPG_H__ */
227