xref: /OK3568_Linux_fs/kernel/drivers/clk/bcm/clk-kona.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright (C) 2013 Broadcom Corporation
3*4882a593Smuzhiyun  * Copyright 2013 Linaro Limited
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or
6*4882a593Smuzhiyun  * modify it under the terms of the GNU General Public License as
7*4882a593Smuzhiyun  * published by the Free Software Foundation version 2.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
10*4882a593Smuzhiyun  * kind, whether express or implied; without even the implied warranty
11*4882a593Smuzhiyun  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*4882a593Smuzhiyun  * GNU General Public License for more details.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #ifndef _CLK_KONA_H
16*4882a593Smuzhiyun #define _CLK_KONA_H
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #include <linux/kernel.h>
19*4882a593Smuzhiyun #include <linux/list.h>
20*4882a593Smuzhiyun #include <linux/spinlock.h>
21*4882a593Smuzhiyun #include <linux/slab.h>
22*4882a593Smuzhiyun #include <linux/device.h>
23*4882a593Smuzhiyun #include <linux/of.h>
24*4882a593Smuzhiyun #include <linux/clk-provider.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define	BILLION		1000000000
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* The common clock framework uses u8 to represent a parent index */
29*4882a593Smuzhiyun #define PARENT_COUNT_MAX	((u32)U8_MAX)
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define BAD_CLK_INDEX		U8_MAX	/* Can't ever be valid */
32*4882a593Smuzhiyun #define BAD_CLK_NAME		((const char *)-1)
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define BAD_SCALED_DIV_VALUE	U64_MAX
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /*
37*4882a593Smuzhiyun  * Utility macros for object flag management.  If possible, flags
38*4882a593Smuzhiyun  * should be defined such that 0 is the desired default value.
39*4882a593Smuzhiyun  */
40*4882a593Smuzhiyun #define FLAG(type, flag)		BCM_CLK_ ## type ## _FLAGS_ ## flag
41*4882a593Smuzhiyun #define FLAG_SET(obj, type, flag)	((obj)->flags |= FLAG(type, flag))
42*4882a593Smuzhiyun #define FLAG_CLEAR(obj, type, flag)	((obj)->flags &= ~(FLAG(type, flag)))
43*4882a593Smuzhiyun #define FLAG_FLIP(obj, type, flag)	((obj)->flags ^= FLAG(type, flag))
44*4882a593Smuzhiyun #define FLAG_TEST(obj, type, flag)	(!!((obj)->flags & FLAG(type, flag)))
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* CCU field state tests */
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define ccu_policy_exists(ccu_policy)	((ccu_policy)->enable.offset != 0)
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /* Clock field state tests */
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define policy_exists(policy)		((policy)->offset != 0)
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun #define gate_exists(gate)		FLAG_TEST(gate, GATE, EXISTS)
55*4882a593Smuzhiyun #define gate_is_enabled(gate)		FLAG_TEST(gate, GATE, ENABLED)
56*4882a593Smuzhiyun #define gate_is_hw_controllable(gate)	FLAG_TEST(gate, GATE, HW)
57*4882a593Smuzhiyun #define gate_is_sw_controllable(gate)	FLAG_TEST(gate, GATE, SW)
58*4882a593Smuzhiyun #define gate_is_sw_managed(gate)	FLAG_TEST(gate, GATE, SW_MANAGED)
59*4882a593Smuzhiyun #define gate_is_no_disable(gate)	FLAG_TEST(gate, GATE, NO_DISABLE)
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun #define gate_flip_enabled(gate)		FLAG_FLIP(gate, GATE, ENABLED)
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #define hyst_exists(hyst)		((hyst)->offset != 0)
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #define divider_exists(div)		FLAG_TEST(div, DIV, EXISTS)
66*4882a593Smuzhiyun #define divider_is_fixed(div)		FLAG_TEST(div, DIV, FIXED)
67*4882a593Smuzhiyun #define divider_has_fraction(div)	(!divider_is_fixed(div) && \
68*4882a593Smuzhiyun 						(div)->u.s.frac_width > 0)
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun #define selector_exists(sel)		((sel)->width != 0)
71*4882a593Smuzhiyun #define trigger_exists(trig)		FLAG_TEST(trig, TRIG, EXISTS)
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #define policy_lvm_en_exists(enable)	((enable)->offset != 0)
74*4882a593Smuzhiyun #define policy_ctl_exists(control)	((control)->offset != 0)
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun /* Clock type, used to tell common block what it's part of */
77*4882a593Smuzhiyun enum bcm_clk_type {
78*4882a593Smuzhiyun 	bcm_clk_none,		/* undefined clock type */
79*4882a593Smuzhiyun 	bcm_clk_bus,
80*4882a593Smuzhiyun 	bcm_clk_core,
81*4882a593Smuzhiyun 	bcm_clk_peri
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * CCU policy control for clocks.  Clocks can be enabled or disabled
86*4882a593Smuzhiyun  * based on the CCU policy in effect.  One bit in each policy mask
87*4882a593Smuzhiyun  * register (one per CCU policy) represents whether the clock is
88*4882a593Smuzhiyun  * enabled when that policy is effect or not.  The CCU policy engine
89*4882a593Smuzhiyun  * must be stopped to update these bits, and must be restarted again
90*4882a593Smuzhiyun  * afterward.
91*4882a593Smuzhiyun  */
92*4882a593Smuzhiyun struct bcm_clk_policy {
93*4882a593Smuzhiyun 	u32 offset;		/* first policy mask register offset */
94*4882a593Smuzhiyun 	u32 bit;		/* bit used in all mask registers */
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /* Policy initialization macro */
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun #define POLICY(_offset, _bit)						\
100*4882a593Smuzhiyun 	{								\
101*4882a593Smuzhiyun 		.offset = (_offset),					\
102*4882a593Smuzhiyun 		.bit = (_bit),						\
103*4882a593Smuzhiyun 	}
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun  * Gating control and status is managed by a 32-bit gate register.
107*4882a593Smuzhiyun  *
108*4882a593Smuzhiyun  * There are several types of gating available:
109*4882a593Smuzhiyun  * - (no gate)
110*4882a593Smuzhiyun  *     A clock with no gate is assumed to be always enabled.
111*4882a593Smuzhiyun  * - hardware-only gating (auto-gating)
112*4882a593Smuzhiyun  *     Enabling or disabling clocks with this type of gate is
113*4882a593Smuzhiyun  *     managed automatically by the hardware.  Such clocks can be
114*4882a593Smuzhiyun  *     considered by the software to be enabled.  The current status
115*4882a593Smuzhiyun  *     of auto-gated clocks can be read from the gate status bit.
116*4882a593Smuzhiyun  * - software-only gating
117*4882a593Smuzhiyun  *     Auto-gating is not available for this type of clock.
118*4882a593Smuzhiyun  *     Instead, software manages whether it's enabled by setting or
119*4882a593Smuzhiyun  *     clearing the enable bit.  The current gate status of a gate
120*4882a593Smuzhiyun  *     under software control can be read from the gate status bit.
121*4882a593Smuzhiyun  *     To ensure a change to the gating status is complete, the
122*4882a593Smuzhiyun  *     status bit can be polled to verify that the gate has entered
123*4882a593Smuzhiyun  *     the desired state.
124*4882a593Smuzhiyun  * - selectable hardware or software gating
125*4882a593Smuzhiyun  *     Gating for this type of clock can be configured to be either
126*4882a593Smuzhiyun  *     under software or hardware control.  Which type is in use is
127*4882a593Smuzhiyun  *     determined by the hw_sw_sel bit of the gate register.
128*4882a593Smuzhiyun  */
129*4882a593Smuzhiyun struct bcm_clk_gate {
130*4882a593Smuzhiyun 	u32 offset;		/* gate register offset */
131*4882a593Smuzhiyun 	u32 status_bit;		/* 0: gate is disabled; 0: gatge is enabled */
132*4882a593Smuzhiyun 	u32 en_bit;		/* 0: disable; 1: enable */
133*4882a593Smuzhiyun 	u32 hw_sw_sel_bit;	/* 0: hardware gating; 1: software gating */
134*4882a593Smuzhiyun 	u32 flags;		/* BCM_CLK_GATE_FLAGS_* below */
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun  * Gate flags:
139*4882a593Smuzhiyun  *   HW         means this gate can be auto-gated
140*4882a593Smuzhiyun  *   SW         means the state of this gate can be software controlled
141*4882a593Smuzhiyun  *   NO_DISABLE means this gate is (only) enabled if under software control
142*4882a593Smuzhiyun  *   SW_MANAGED means the status of this gate is under software control
143*4882a593Smuzhiyun  *   ENABLED    means this software-managed gate is *supposed* to be enabled
144*4882a593Smuzhiyun  */
145*4882a593Smuzhiyun #define BCM_CLK_GATE_FLAGS_EXISTS	((u32)1 << 0)	/* Gate is valid */
146*4882a593Smuzhiyun #define BCM_CLK_GATE_FLAGS_HW		((u32)1 << 1)	/* Can auto-gate */
147*4882a593Smuzhiyun #define BCM_CLK_GATE_FLAGS_SW		((u32)1 << 2)	/* Software control */
148*4882a593Smuzhiyun #define BCM_CLK_GATE_FLAGS_NO_DISABLE	((u32)1 << 3)	/* HW or enabled */
149*4882a593Smuzhiyun #define BCM_CLK_GATE_FLAGS_SW_MANAGED	((u32)1 << 4)	/* SW now in control */
150*4882a593Smuzhiyun #define BCM_CLK_GATE_FLAGS_ENABLED	((u32)1 << 5)	/* If SW_MANAGED */
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun  * Gate initialization macros.
154*4882a593Smuzhiyun  *
155*4882a593Smuzhiyun  * Any gate initially under software control will be enabled.
156*4882a593Smuzhiyun  */
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /* A hardware/software gate initially under software control */
159*4882a593Smuzhiyun #define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
160*4882a593Smuzhiyun 	{								\
161*4882a593Smuzhiyun 		.offset = (_offset),					\
162*4882a593Smuzhiyun 		.status_bit = (_status_bit),				\
163*4882a593Smuzhiyun 		.en_bit = (_en_bit),					\
164*4882a593Smuzhiyun 		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
165*4882a593Smuzhiyun 		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
166*4882a593Smuzhiyun 			FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)|	\
167*4882a593Smuzhiyun 			FLAG(GATE, EXISTS),				\
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /* A hardware/software gate initially under hardware control */
171*4882a593Smuzhiyun #define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
172*4882a593Smuzhiyun 	{								\
173*4882a593Smuzhiyun 		.offset = (_offset),					\
174*4882a593Smuzhiyun 		.status_bit = (_status_bit),				\
175*4882a593Smuzhiyun 		.en_bit = (_en_bit),					\
176*4882a593Smuzhiyun 		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
177*4882a593Smuzhiyun 		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
178*4882a593Smuzhiyun 			FLAG(GATE, EXISTS),				\
179*4882a593Smuzhiyun 	}
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /* A hardware-or-enabled gate (enabled if not under hardware control) */
182*4882a593Smuzhiyun #define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
183*4882a593Smuzhiyun 	{								\
184*4882a593Smuzhiyun 		.offset = (_offset),					\
185*4882a593Smuzhiyun 		.status_bit = (_status_bit),				\
186*4882a593Smuzhiyun 		.en_bit = (_en_bit),					\
187*4882a593Smuzhiyun 		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
188*4882a593Smuzhiyun 		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
189*4882a593Smuzhiyun 			FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS),	\
190*4882a593Smuzhiyun 	}
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /* A software-only gate */
193*4882a593Smuzhiyun #define SW_ONLY_GATE(_offset, _status_bit, _en_bit)			\
194*4882a593Smuzhiyun 	{								\
195*4882a593Smuzhiyun 		.offset = (_offset),					\
196*4882a593Smuzhiyun 		.status_bit = (_status_bit),				\
197*4882a593Smuzhiyun 		.en_bit = (_en_bit),					\
198*4882a593Smuzhiyun 		.flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)|		\
199*4882a593Smuzhiyun 			FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS),		\
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun /* A hardware-only gate */
203*4882a593Smuzhiyun #define HW_ONLY_GATE(_offset, _status_bit)				\
204*4882a593Smuzhiyun 	{								\
205*4882a593Smuzhiyun 		.offset = (_offset),					\
206*4882a593Smuzhiyun 		.status_bit = (_status_bit),				\
207*4882a593Smuzhiyun 		.flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS),		\
208*4882a593Smuzhiyun 	}
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /* Gate hysteresis for clocks */
211*4882a593Smuzhiyun struct bcm_clk_hyst {
212*4882a593Smuzhiyun 	u32 offset;		/* hyst register offset (normally CLKGATE) */
213*4882a593Smuzhiyun 	u32 en_bit;		/* bit used to enable hysteresis */
214*4882a593Smuzhiyun 	u32 val_bit;		/* if enabled: 0 = low delay; 1 = high delay */
215*4882a593Smuzhiyun };
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun /* Hysteresis initialization macro */
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun #define HYST(_offset, _en_bit, _val_bit)				\
220*4882a593Smuzhiyun 	{								\
221*4882a593Smuzhiyun 		.offset = (_offset),					\
222*4882a593Smuzhiyun 		.en_bit = (_en_bit),					\
223*4882a593Smuzhiyun 		.val_bit = (_val_bit),					\
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * Each clock can have zero, one, or two dividers which change the
228*4882a593Smuzhiyun  * output rate of the clock.  Each divider can be either fixed or
229*4882a593Smuzhiyun  * variable.  If there are two dividers, they are the "pre-divider"
230*4882a593Smuzhiyun  * and the "regular" or "downstream" divider.  If there is only one,
231*4882a593Smuzhiyun  * there is no pre-divider.
232*4882a593Smuzhiyun  *
233*4882a593Smuzhiyun  * A fixed divider is any non-zero (positive) value, and it
234*4882a593Smuzhiyun  * indicates how the input rate is affected by the divider.
235*4882a593Smuzhiyun  *
236*4882a593Smuzhiyun  * The value of a variable divider is maintained in a sub-field of a
237*4882a593Smuzhiyun  * 32-bit divider register.  The position of the field in the
238*4882a593Smuzhiyun  * register is defined by its offset and width.  The value recorded
239*4882a593Smuzhiyun  * in this field is always 1 less than the value it represents.
240*4882a593Smuzhiyun  *
241*4882a593Smuzhiyun  * In addition, a variable divider can indicate that some subset
242*4882a593Smuzhiyun  * of its bits represent a "fractional" part of the divider.  Such
243*4882a593Smuzhiyun  * bits comprise the low-order portion of the divider field, and can
244*4882a593Smuzhiyun  * be viewed as representing the portion of the divider that lies to
245*4882a593Smuzhiyun  * the right of the decimal point.  Most variable dividers have zero
246*4882a593Smuzhiyun  * fractional bits.  Variable dividers with non-zero fraction width
247*4882a593Smuzhiyun  * still record a value 1 less than the value they represent; the
248*4882a593Smuzhiyun  * added 1 does *not* affect the low-order bit in this case, it
249*4882a593Smuzhiyun  * affects the bits above the fractional part only.  (Often in this
250*4882a593Smuzhiyun  * code a divider field value is distinguished from the value it
251*4882a593Smuzhiyun  * represents by referring to the latter as a "divisor".)
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * In order to avoid dealing with fractions, divider arithmetic is
254*4882a593Smuzhiyun  * performed using "scaled" values.  A scaled value is one that's
255*4882a593Smuzhiyun  * been left-shifted by the fractional width of a divider.  Dividing
256*4882a593Smuzhiyun  * a scaled value by a scaled divisor produces the desired quotient
257*4882a593Smuzhiyun  * without loss of precision and without any other special handling
258*4882a593Smuzhiyun  * for fractions.
259*4882a593Smuzhiyun  *
260*4882a593Smuzhiyun  * The recorded value of a variable divider can be modified.  To
261*4882a593Smuzhiyun  * modify either divider (or both), a clock must be enabled (i.e.,
262*4882a593Smuzhiyun  * using its gate).  In addition, a trigger register (described
263*4882a593Smuzhiyun  * below) must be used to commit the change, and polled to verify
264*4882a593Smuzhiyun  * the change is complete.
265*4882a593Smuzhiyun  */
266*4882a593Smuzhiyun struct bcm_clk_div {
267*4882a593Smuzhiyun 	union {
268*4882a593Smuzhiyun 		struct {	/* variable divider */
269*4882a593Smuzhiyun 			u32 offset;	/* divider register offset */
270*4882a593Smuzhiyun 			u32 shift;	/* field shift */
271*4882a593Smuzhiyun 			u32 width;	/* field width */
272*4882a593Smuzhiyun 			u32 frac_width;	/* field fraction width */
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 			u64 scaled_div;	/* scaled divider value */
275*4882a593Smuzhiyun 		} s;
276*4882a593Smuzhiyun 		u32 fixed;	/* non-zero fixed divider value */
277*4882a593Smuzhiyun 	} u;
278*4882a593Smuzhiyun 	u32 flags;		/* BCM_CLK_DIV_FLAGS_* below */
279*4882a593Smuzhiyun };
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun /*
282*4882a593Smuzhiyun  * Divider flags:
283*4882a593Smuzhiyun  *   EXISTS means this divider exists
284*4882a593Smuzhiyun  *   FIXED means it is a fixed-rate divider
285*4882a593Smuzhiyun  */
286*4882a593Smuzhiyun #define BCM_CLK_DIV_FLAGS_EXISTS	((u32)1 << 0)	/* Divider is valid */
287*4882a593Smuzhiyun #define BCM_CLK_DIV_FLAGS_FIXED		((u32)1 << 1)	/* Fixed-value */
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun /* Divider initialization macros */
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun /* A fixed (non-zero) divider */
292*4882a593Smuzhiyun #define FIXED_DIVIDER(_value)						\
293*4882a593Smuzhiyun 	{								\
294*4882a593Smuzhiyun 		.u.fixed = (_value),					\
295*4882a593Smuzhiyun 		.flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED),		\
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun /* A divider with an integral divisor */
299*4882a593Smuzhiyun #define DIVIDER(_offset, _shift, _width)				\
300*4882a593Smuzhiyun 	{								\
301*4882a593Smuzhiyun 		.u.s.offset = (_offset),				\
302*4882a593Smuzhiyun 		.u.s.shift = (_shift),					\
303*4882a593Smuzhiyun 		.u.s.width = (_width),					\
304*4882a593Smuzhiyun 		.u.s.scaled_div = BAD_SCALED_DIV_VALUE,			\
305*4882a593Smuzhiyun 		.flags = FLAG(DIV, EXISTS),				\
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun /* A divider whose divisor has an integer and fractional part */
309*4882a593Smuzhiyun #define FRAC_DIVIDER(_offset, _shift, _width, _frac_width)		\
310*4882a593Smuzhiyun 	{								\
311*4882a593Smuzhiyun 		.u.s.offset = (_offset),				\
312*4882a593Smuzhiyun 		.u.s.shift = (_shift),					\
313*4882a593Smuzhiyun 		.u.s.width = (_width),					\
314*4882a593Smuzhiyun 		.u.s.frac_width = (_frac_width),			\
315*4882a593Smuzhiyun 		.u.s.scaled_div = BAD_SCALED_DIV_VALUE,			\
316*4882a593Smuzhiyun 		.flags = FLAG(DIV, EXISTS),				\
317*4882a593Smuzhiyun 	}
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun /*
320*4882a593Smuzhiyun  * Clocks may have multiple "parent" clocks.  If there is more than
321*4882a593Smuzhiyun  * one, a selector must be specified to define which of the parent
322*4882a593Smuzhiyun  * clocks is currently in use.  The selected clock is indicated in a
323*4882a593Smuzhiyun  * sub-field of a 32-bit selector register.  The range of
324*4882a593Smuzhiyun  * representable selector values typically exceeds the number of
325*4882a593Smuzhiyun  * available parent clocks.  Occasionally the reset value of a
326*4882a593Smuzhiyun  * selector field is explicitly set to a (specific) value that does
327*4882a593Smuzhiyun  * not correspond to a defined input clock.
328*4882a593Smuzhiyun  *
329*4882a593Smuzhiyun  * We register all known parent clocks with the common clock code
330*4882a593Smuzhiyun  * using a packed array (i.e., no empty slots) of (parent) clock
331*4882a593Smuzhiyun  * names, and refer to them later using indexes into that array.
332*4882a593Smuzhiyun  * We maintain an array of selector values indexed by common clock
333*4882a593Smuzhiyun  * index values in order to map between these common clock indexes
334*4882a593Smuzhiyun  * and the selector values used by the hardware.
335*4882a593Smuzhiyun  *
336*4882a593Smuzhiyun  * Like dividers, a selector can be modified, but to do so a clock
337*4882a593Smuzhiyun  * must be enabled, and a trigger must be used to commit the change.
338*4882a593Smuzhiyun  */
339*4882a593Smuzhiyun struct bcm_clk_sel {
340*4882a593Smuzhiyun 	u32 offset;		/* selector register offset */
341*4882a593Smuzhiyun 	u32 shift;		/* field shift */
342*4882a593Smuzhiyun 	u32 width;		/* field width */
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	u32 parent_count;	/* number of entries in parent_sel[] */
345*4882a593Smuzhiyun 	u32 *parent_sel;	/* array of parent selector values */
346*4882a593Smuzhiyun 	u8 clk_index;		/* current selected index in parent_sel[] */
347*4882a593Smuzhiyun };
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun /* Selector initialization macro */
350*4882a593Smuzhiyun #define SELECTOR(_offset, _shift, _width)				\
351*4882a593Smuzhiyun 	{								\
352*4882a593Smuzhiyun 		.offset = (_offset),					\
353*4882a593Smuzhiyun 		.shift = (_shift),					\
354*4882a593Smuzhiyun 		.width = (_width),					\
355*4882a593Smuzhiyun 		.clk_index = BAD_CLK_INDEX,				\
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun /*
359*4882a593Smuzhiyun  * Making changes to a variable divider or a selector for a clock
360*4882a593Smuzhiyun  * requires the use of a trigger.  A trigger is defined by a single
361*4882a593Smuzhiyun  * bit within a register.  To signal a change, a 1 is written into
362*4882a593Smuzhiyun  * that bit.  To determine when the change has been completed, that
363*4882a593Smuzhiyun  * trigger bit is polled; the read value will be 1 while the change
364*4882a593Smuzhiyun  * is in progress, and 0 when it is complete.
365*4882a593Smuzhiyun  *
366*4882a593Smuzhiyun  * Occasionally a clock will have more than one trigger.  In this
367*4882a593Smuzhiyun  * case, the "pre-trigger" will be used when changing a clock's
368*4882a593Smuzhiyun  * selector and/or its pre-divider.
369*4882a593Smuzhiyun  */
370*4882a593Smuzhiyun struct bcm_clk_trig {
371*4882a593Smuzhiyun 	u32 offset;		/* trigger register offset */
372*4882a593Smuzhiyun 	u32 bit;		/* trigger bit */
373*4882a593Smuzhiyun 	u32 flags;		/* BCM_CLK_TRIG_FLAGS_* below */
374*4882a593Smuzhiyun };
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun /*
377*4882a593Smuzhiyun  * Trigger flags:
378*4882a593Smuzhiyun  *   EXISTS means this trigger exists
379*4882a593Smuzhiyun  */
380*4882a593Smuzhiyun #define BCM_CLK_TRIG_FLAGS_EXISTS	((u32)1 << 0)	/* Trigger is valid */
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun /* Trigger initialization macro */
383*4882a593Smuzhiyun #define TRIGGER(_offset, _bit)						\
384*4882a593Smuzhiyun 	{								\
385*4882a593Smuzhiyun 		.offset = (_offset),					\
386*4882a593Smuzhiyun 		.bit = (_bit),						\
387*4882a593Smuzhiyun 		.flags = FLAG(TRIG, EXISTS),				\
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun struct peri_clk_data {
391*4882a593Smuzhiyun 	struct bcm_clk_policy policy;
392*4882a593Smuzhiyun 	struct bcm_clk_gate gate;
393*4882a593Smuzhiyun 	struct bcm_clk_hyst hyst;
394*4882a593Smuzhiyun 	struct bcm_clk_trig pre_trig;
395*4882a593Smuzhiyun 	struct bcm_clk_div pre_div;
396*4882a593Smuzhiyun 	struct bcm_clk_trig trig;
397*4882a593Smuzhiyun 	struct bcm_clk_div div;
398*4882a593Smuzhiyun 	struct bcm_clk_sel sel;
399*4882a593Smuzhiyun 	const char *clocks[];	/* must be last; use CLOCKS() to declare */
400*4882a593Smuzhiyun };
401*4882a593Smuzhiyun #define CLOCKS(...)	{ __VA_ARGS__, NULL, }
402*4882a593Smuzhiyun #define NO_CLOCKS	{ NULL, }	/* Must use of no parent clocks */
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun struct kona_clk {
405*4882a593Smuzhiyun 	struct clk_hw hw;
406*4882a593Smuzhiyun 	struct clk_init_data init_data;	/* includes name of this clock */
407*4882a593Smuzhiyun 	struct ccu_data *ccu;	/* ccu this clock is associated with */
408*4882a593Smuzhiyun 	enum bcm_clk_type type;
409*4882a593Smuzhiyun 	union {
410*4882a593Smuzhiyun 		void *data;
411*4882a593Smuzhiyun 		struct peri_clk_data *peri;
412*4882a593Smuzhiyun 	} u;
413*4882a593Smuzhiyun };
414*4882a593Smuzhiyun #define to_kona_clk(_hw) \
415*4882a593Smuzhiyun 	container_of(_hw, struct kona_clk, hw)
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun /* Initialization macro for an entry in a CCU's kona_clks[] array. */
418*4882a593Smuzhiyun #define KONA_CLK(_ccu_name, _clk_name, _type)				\
419*4882a593Smuzhiyun 	{								\
420*4882a593Smuzhiyun 		.init_data	= {					\
421*4882a593Smuzhiyun 			.name = #_clk_name,				\
422*4882a593Smuzhiyun 			.ops = &kona_ ## _type ## _clk_ops,		\
423*4882a593Smuzhiyun 		},							\
424*4882a593Smuzhiyun 		.ccu		= &_ccu_name ## _ccu_data,		\
425*4882a593Smuzhiyun 		.type		= bcm_clk_ ## _type,			\
426*4882a593Smuzhiyun 		.u.data		= &_clk_name ## _data,			\
427*4882a593Smuzhiyun 	}
428*4882a593Smuzhiyun #define LAST_KONA_CLK	{ .type = bcm_clk_none }
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun /*
431*4882a593Smuzhiyun  * CCU policy control.  To enable software update of the policy
432*4882a593Smuzhiyun  * tables the CCU policy engine must be stopped by setting the
433*4882a593Smuzhiyun  * software update enable bit (LVM_EN).  After an update the engine
434*4882a593Smuzhiyun  * is restarted using the GO bit and either the GO_ATL or GO_AC bit.
435*4882a593Smuzhiyun  */
436*4882a593Smuzhiyun struct bcm_lvm_en {
437*4882a593Smuzhiyun 	u32 offset;		/* LVM_EN register offset */
438*4882a593Smuzhiyun 	u32 bit;		/* POLICY_CONFIG_EN bit in register */
439*4882a593Smuzhiyun };
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun /* Policy enable initialization macro */
442*4882a593Smuzhiyun #define CCU_LVM_EN(_offset, _bit)					\
443*4882a593Smuzhiyun 	{								\
444*4882a593Smuzhiyun 		.offset = (_offset),					\
445*4882a593Smuzhiyun 		.bit = (_bit),						\
446*4882a593Smuzhiyun 	}
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun struct bcm_policy_ctl {
449*4882a593Smuzhiyun 	u32 offset;		/* POLICY_CTL register offset */
450*4882a593Smuzhiyun 	u32 go_bit;
451*4882a593Smuzhiyun 	u32 atl_bit;		/* GO, GO_ATL, and GO_AC bits */
452*4882a593Smuzhiyun 	u32 ac_bit;
453*4882a593Smuzhiyun };
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun /* Policy control initialization macro */
456*4882a593Smuzhiyun #define CCU_POLICY_CTL(_offset, _go_bit, _ac_bit, _atl_bit)		\
457*4882a593Smuzhiyun 	{								\
458*4882a593Smuzhiyun 		.offset = (_offset),					\
459*4882a593Smuzhiyun 		.go_bit = (_go_bit),					\
460*4882a593Smuzhiyun 		.ac_bit = (_ac_bit),					\
461*4882a593Smuzhiyun 		.atl_bit = (_atl_bit),					\
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun struct ccu_policy {
465*4882a593Smuzhiyun 	struct bcm_lvm_en enable;
466*4882a593Smuzhiyun 	struct bcm_policy_ctl control;
467*4882a593Smuzhiyun };
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun /*
470*4882a593Smuzhiyun  * Each CCU defines a mapped area of memory containing registers
471*4882a593Smuzhiyun  * used to manage clocks implemented by the CCU.  Access to memory
472*4882a593Smuzhiyun  * within the CCU's space is serialized by a spinlock.  Before any
473*4882a593Smuzhiyun  * (other) address can be written, a special access "password" value
474*4882a593Smuzhiyun  * must be written to its WR_ACCESS register (located at the base
475*4882a593Smuzhiyun  * address of the range).  We keep track of the name of each CCU as
476*4882a593Smuzhiyun  * it is set up, and maintain them in a list.
477*4882a593Smuzhiyun  */
478*4882a593Smuzhiyun struct ccu_data {
479*4882a593Smuzhiyun 	void __iomem *base;	/* base of mapped address space */
480*4882a593Smuzhiyun 	spinlock_t lock;	/* serialization lock */
481*4882a593Smuzhiyun 	bool write_enabled;	/* write access is currently enabled */
482*4882a593Smuzhiyun 	struct ccu_policy policy;
483*4882a593Smuzhiyun 	struct device_node *node;
484*4882a593Smuzhiyun 	size_t clk_num;
485*4882a593Smuzhiyun 	const char *name;
486*4882a593Smuzhiyun 	u32 range;		/* byte range of address space */
487*4882a593Smuzhiyun 	struct kona_clk kona_clks[];	/* must be last */
488*4882a593Smuzhiyun };
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun /* Initialization for common fields in a Kona ccu_data structure */
491*4882a593Smuzhiyun #define KONA_CCU_COMMON(_prefix, _name, _ccuname)			    \
492*4882a593Smuzhiyun 	.name		= #_name "_ccu",				    \
493*4882a593Smuzhiyun 	.lock		= __SPIN_LOCK_UNLOCKED(_name ## _ccu_data.lock),    \
494*4882a593Smuzhiyun 	.clk_num	= _prefix ## _ ## _ccuname ## _CCU_CLOCK_COUNT
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun /* Exported globals */
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun extern struct clk_ops kona_peri_clk_ops;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /* Externally visible functions */
501*4882a593Smuzhiyun 
502*4882a593Smuzhiyun extern u64 scaled_div_max(struct bcm_clk_div *div);
503*4882a593Smuzhiyun extern u64 scaled_div_build(struct bcm_clk_div *div, u32 div_value,
504*4882a593Smuzhiyun 				u32 billionths);
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun extern void __init kona_dt_ccu_setup(struct ccu_data *ccu,
507*4882a593Smuzhiyun 				struct device_node *node);
508*4882a593Smuzhiyun extern bool __init kona_ccu_init(struct ccu_data *ccu);
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun #endif /* _CLK_KONA_H */
511