xref: /rk3399_rockchip-uboot/arch/arm/cpu/armv7/bcm281xx/clk-core.h (revision cba1da495daa3bfa29372b5eb28cb49302ef0a72)
1*989ce049SDarwin Rambo /*
2*989ce049SDarwin Rambo  * Copyright 2013 Broadcom Corporation.
3*989ce049SDarwin Rambo  *
4*989ce049SDarwin Rambo  * SPDX-License-Identifier:      GPL-2.0+
5*989ce049SDarwin Rambo  */
6*989ce049SDarwin Rambo 
7*989ce049SDarwin Rambo #include <linux/stddef.h>
8*989ce049SDarwin Rambo 
9*989ce049SDarwin Rambo #ifdef CONFIG_CLK_DEBUG
10*989ce049SDarwin Rambo #undef writel
11*989ce049SDarwin Rambo #undef readl
writel(u32 val,void * addr)12*989ce049SDarwin Rambo static inline void writel(u32 val, void *addr)
13*989ce049SDarwin Rambo {
14*989ce049SDarwin Rambo 	printf("Write [0x%p] = 0x%08x\n", addr, val);
15*989ce049SDarwin Rambo 	*(u32 *)addr = val;
16*989ce049SDarwin Rambo }
17*989ce049SDarwin Rambo 
readl(void * addr)18*989ce049SDarwin Rambo static inline u32 readl(void *addr)
19*989ce049SDarwin Rambo {
20*989ce049SDarwin Rambo 	u32 val = *(u32 *)addr;
21*989ce049SDarwin Rambo 	printf("Read  [0x%p] = 0x%08x\n", addr, val);
22*989ce049SDarwin Rambo 	return val;
23*989ce049SDarwin Rambo }
24*989ce049SDarwin Rambo #endif
25*989ce049SDarwin Rambo 
26*989ce049SDarwin Rambo struct clk;
27*989ce049SDarwin Rambo 
28*989ce049SDarwin Rambo struct clk_lookup {
29*989ce049SDarwin Rambo 	const char *dev_id;
30*989ce049SDarwin Rambo 	const char *con_id;
31*989ce049SDarwin Rambo 	struct clk *clk;
32*989ce049SDarwin Rambo };
33*989ce049SDarwin Rambo 
34*989ce049SDarwin Rambo extern struct clk_lookup arch_clk_tbl[];
35*989ce049SDarwin Rambo extern unsigned int arch_clk_tbl_array_size;
36*989ce049SDarwin Rambo 
37*989ce049SDarwin Rambo /**
38*989ce049SDarwin Rambo  * struct clk_ops - standard clock operations
39*989ce049SDarwin Rambo  * @enable: enable/disable clock, see clk_enable() and clk_disable()
40*989ce049SDarwin Rambo  * @set_rate: set the clock rate, see clk_set_rate().
41*989ce049SDarwin Rambo  * @get_rate: get the clock rate, see clk_get_rate().
42*989ce049SDarwin Rambo  * @round_rate: round a given clock rate, see clk_round_rate().
43*989ce049SDarwin Rambo  * @set_parent: set the clock's parent, see clk_set_parent().
44*989ce049SDarwin Rambo  *
45*989ce049SDarwin Rambo  * Group the common clock implementations together so that we
46*989ce049SDarwin Rambo  * don't have to keep setting the same fiels again. We leave
47*989ce049SDarwin Rambo  * enable in struct clk.
48*989ce049SDarwin Rambo  *
49*989ce049SDarwin Rambo  */
50*989ce049SDarwin Rambo struct clk_ops {
51*989ce049SDarwin Rambo 	int (*enable) (struct clk *c, int enable);
52*989ce049SDarwin Rambo 	int (*set_rate) (struct clk *c, unsigned long rate);
53*989ce049SDarwin Rambo 	unsigned long (*get_rate) (struct clk *c);
54*989ce049SDarwin Rambo 	unsigned long (*round_rate) (struct clk *c, unsigned long rate);
55*989ce049SDarwin Rambo 	int (*set_parent) (struct clk *c, struct clk *parent);
56*989ce049SDarwin Rambo };
57*989ce049SDarwin Rambo 
58*989ce049SDarwin Rambo struct clk {
59*989ce049SDarwin Rambo 	struct clk *parent;
60*989ce049SDarwin Rambo 	const char *name;
61*989ce049SDarwin Rambo 	int use_cnt;
62*989ce049SDarwin Rambo 	unsigned long rate;	/* in HZ */
63*989ce049SDarwin Rambo 
64*989ce049SDarwin Rambo 	/* programmable divider. 0 means fixed ratio to parent clock */
65*989ce049SDarwin Rambo 	unsigned long div;
66*989ce049SDarwin Rambo 
67*989ce049SDarwin Rambo 	struct clk_src *src;
68*989ce049SDarwin Rambo 	struct clk_ops *ops;
69*989ce049SDarwin Rambo 
70*989ce049SDarwin Rambo 	unsigned long ccu_clk_mgr_base;
71*989ce049SDarwin Rambo 	int sel;
72*989ce049SDarwin Rambo };
73*989ce049SDarwin Rambo 
74*989ce049SDarwin Rambo struct refclk *refclk_str_to_clk(const char *name);
75*989ce049SDarwin Rambo 
76*989ce049SDarwin Rambo /* The common clock framework uses u8 to represent a parent index */
77*989ce049SDarwin Rambo #define PARENT_COUNT_MAX	((u32)U8_MAX)
78*989ce049SDarwin Rambo 
79*989ce049SDarwin Rambo #define BAD_CLK_INDEX		U8_MAX	/* Can't ever be valid */
80*989ce049SDarwin Rambo #define BAD_CLK_NAME		((const char *)-1)
81*989ce049SDarwin Rambo 
82*989ce049SDarwin Rambo #define BAD_SCALED_DIV_VALUE	U64_MAX
83*989ce049SDarwin Rambo 
84*989ce049SDarwin Rambo /*
85*989ce049SDarwin Rambo  * Utility macros for object flag management.  If possible, flags
86*989ce049SDarwin Rambo  * should be defined such that 0 is the desired default value.
87*989ce049SDarwin Rambo  */
88*989ce049SDarwin Rambo #define FLAG(type, flag)		BCM_CLK_ ## type ## _FLAGS_ ## flag
89*989ce049SDarwin Rambo #define FLAG_SET(obj, type, flag)	((obj)->flags |= FLAG(type, flag))
90*989ce049SDarwin Rambo #define FLAG_CLEAR(obj, type, flag)	((obj)->flags &= ~(FLAG(type, flag)))
91*989ce049SDarwin Rambo #define FLAG_FLIP(obj, type, flag)	((obj)->flags ^= FLAG(type, flag))
92*989ce049SDarwin Rambo #define FLAG_TEST(obj, type, flag)	(!!((obj)->flags & FLAG(type, flag)))
93*989ce049SDarwin Rambo 
94*989ce049SDarwin Rambo /* Clock field state tests */
95*989ce049SDarwin Rambo 
96*989ce049SDarwin Rambo #define gate_exists(gate)		FLAG_TEST(gate, GATE, EXISTS)
97*989ce049SDarwin Rambo #define gate_is_enabled(gate)		FLAG_TEST(gate, GATE, ENABLED)
98*989ce049SDarwin Rambo #define gate_is_hw_controllable(gate)	FLAG_TEST(gate, GATE, HW)
99*989ce049SDarwin Rambo #define gate_is_sw_controllable(gate)	FLAG_TEST(gate, GATE, SW)
100*989ce049SDarwin Rambo #define gate_is_sw_managed(gate)	FLAG_TEST(gate, GATE, SW_MANAGED)
101*989ce049SDarwin Rambo #define gate_is_no_disable(gate)	FLAG_TEST(gate, GATE, NO_DISABLE)
102*989ce049SDarwin Rambo 
103*989ce049SDarwin Rambo #define gate_flip_enabled(gate)		FLAG_FLIP(gate, GATE, ENABLED)
104*989ce049SDarwin Rambo 
105*989ce049SDarwin Rambo #define divider_exists(div)		FLAG_TEST(div, DIV, EXISTS)
106*989ce049SDarwin Rambo #define divider_is_fixed(div)		FLAG_TEST(div, DIV, FIXED)
107*989ce049SDarwin Rambo #define divider_has_fraction(div)	(!divider_is_fixed(div) && \
108*989ce049SDarwin Rambo 						(div)->frac_width > 0)
109*989ce049SDarwin Rambo 
110*989ce049SDarwin Rambo #define selector_exists(sel)		((sel)->width != 0)
111*989ce049SDarwin Rambo #define trigger_exists(trig)		FLAG_TEST(trig, TRIG, EXISTS)
112*989ce049SDarwin Rambo 
113*989ce049SDarwin Rambo /* Clock type, used to tell common block what it's part of */
114*989ce049SDarwin Rambo enum bcm_clk_type {
115*989ce049SDarwin Rambo 	bcm_clk_none,		/* undefined clock type */
116*989ce049SDarwin Rambo 	bcm_clk_bus,
117*989ce049SDarwin Rambo 	bcm_clk_core,
118*989ce049SDarwin Rambo 	bcm_clk_peri
119*989ce049SDarwin Rambo };
120*989ce049SDarwin Rambo 
121*989ce049SDarwin Rambo /*
122*989ce049SDarwin Rambo  * Gating control and status is managed by a 32-bit gate register.
123*989ce049SDarwin Rambo  *
124*989ce049SDarwin Rambo  * There are several types of gating available:
125*989ce049SDarwin Rambo  * - (no gate)
126*989ce049SDarwin Rambo  *     A clock with no gate is assumed to be always enabled.
127*989ce049SDarwin Rambo  * - hardware-only gating (auto-gating)
128*989ce049SDarwin Rambo  *     Enabling or disabling clocks with this type of gate is
129*989ce049SDarwin Rambo  *     managed automatically by the hardware.  Such clocks can be
130*989ce049SDarwin Rambo  *     considered by the software to be enabled.  The current status
131*989ce049SDarwin Rambo  *     of auto-gated clocks can be read from the gate status bit.
132*989ce049SDarwin Rambo  * - software-only gating
133*989ce049SDarwin Rambo  *     Auto-gating is not available for this type of clock.
134*989ce049SDarwin Rambo  *     Instead, software manages whether it's enabled by setting or
135*989ce049SDarwin Rambo  *     clearing the enable bit.  The current gate status of a gate
136*989ce049SDarwin Rambo  *     under software control can be read from the gate status bit.
137*989ce049SDarwin Rambo  *     To ensure a change to the gating status is complete, the
138*989ce049SDarwin Rambo  *     status bit can be polled to verify that the gate has entered
139*989ce049SDarwin Rambo  *     the desired state.
140*989ce049SDarwin Rambo  * - selectable hardware or software gating
141*989ce049SDarwin Rambo  *     Gating for this type of clock can be configured to be either
142*989ce049SDarwin Rambo  *     under software or hardware control.  Which type is in use is
143*989ce049SDarwin Rambo  *     determined by the hw_sw_sel bit of the gate register.
144*989ce049SDarwin Rambo  */
145*989ce049SDarwin Rambo struct bcm_clk_gate {
146*989ce049SDarwin Rambo 	u32 offset;		/* gate register offset */
147*989ce049SDarwin Rambo 	u32 status_bit;		/* 0: gate is disabled; 0: gatge is enabled */
148*989ce049SDarwin Rambo 	u32 en_bit;		/* 0: disable; 1: enable */
149*989ce049SDarwin Rambo 	u32 hw_sw_sel_bit;	/* 0: hardware gating; 1: software gating */
150*989ce049SDarwin Rambo 	u32 flags;		/* BCM_CLK_GATE_FLAGS_* below */
151*989ce049SDarwin Rambo };
152*989ce049SDarwin Rambo 
153*989ce049SDarwin Rambo /*
154*989ce049SDarwin Rambo  * Gate flags:
155*989ce049SDarwin Rambo  *   HW         means this gate can be auto-gated
156*989ce049SDarwin Rambo  *   SW         means the state of this gate can be software controlled
157*989ce049SDarwin Rambo  *   NO_DISABLE means this gate is (only) enabled if under software control
158*989ce049SDarwin Rambo  *   SW_MANAGED means the status of this gate is under software control
159*989ce049SDarwin Rambo  *   ENABLED    means this software-managed gate is *supposed* to be enabled
160*989ce049SDarwin Rambo  */
161*989ce049SDarwin Rambo #define BCM_CLK_GATE_FLAGS_EXISTS	((u32)1 << 0)	/* Gate is valid */
162*989ce049SDarwin Rambo #define BCM_CLK_GATE_FLAGS_HW		((u32)1 << 1)	/* Can auto-gate */
163*989ce049SDarwin Rambo #define BCM_CLK_GATE_FLAGS_SW		((u32)1 << 2)	/* Software control */
164*989ce049SDarwin Rambo #define BCM_CLK_GATE_FLAGS_NO_DISABLE	((u32)1 << 3)	/* HW or enabled */
165*989ce049SDarwin Rambo #define BCM_CLK_GATE_FLAGS_SW_MANAGED	((u32)1 << 4)	/* SW now in control */
166*989ce049SDarwin Rambo #define BCM_CLK_GATE_FLAGS_ENABLED	((u32)1 << 5)	/* If SW_MANAGED */
167*989ce049SDarwin Rambo 
168*989ce049SDarwin Rambo /*
169*989ce049SDarwin Rambo  * Gate initialization macros.
170*989ce049SDarwin Rambo  *
171*989ce049SDarwin Rambo  * Any gate initially under software control will be enabled.
172*989ce049SDarwin Rambo  */
173*989ce049SDarwin Rambo 
174*989ce049SDarwin Rambo /* A hardware/software gate initially under software control */
175*989ce049SDarwin Rambo #define HW_SW_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
176*989ce049SDarwin Rambo 	{								\
177*989ce049SDarwin Rambo 		.offset = (_offset),					\
178*989ce049SDarwin Rambo 		.status_bit = (_status_bit),				\
179*989ce049SDarwin Rambo 		.en_bit = (_en_bit),					\
180*989ce049SDarwin Rambo 		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
181*989ce049SDarwin Rambo 		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
182*989ce049SDarwin Rambo 			FLAG(GATE, SW_MANAGED)|FLAG(GATE, ENABLED)|	\
183*989ce049SDarwin Rambo 			FLAG(GATE, EXISTS),				\
184*989ce049SDarwin Rambo 	}
185*989ce049SDarwin Rambo 
186*989ce049SDarwin Rambo /* A hardware/software gate initially under hardware control */
187*989ce049SDarwin Rambo #define HW_SW_GATE_AUTO(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
188*989ce049SDarwin Rambo 	{								\
189*989ce049SDarwin Rambo 		.offset = (_offset),					\
190*989ce049SDarwin Rambo 		.status_bit = (_status_bit),				\
191*989ce049SDarwin Rambo 		.en_bit = (_en_bit),					\
192*989ce049SDarwin Rambo 		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
193*989ce049SDarwin Rambo 		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
194*989ce049SDarwin Rambo 			FLAG(GATE, EXISTS),				\
195*989ce049SDarwin Rambo 	}
196*989ce049SDarwin Rambo 
197*989ce049SDarwin Rambo /* A hardware-or-enabled gate (enabled if not under hardware control) */
198*989ce049SDarwin Rambo #define HW_ENABLE_GATE(_offset, _status_bit, _en_bit, _hw_sw_sel_bit)	\
199*989ce049SDarwin Rambo 	{								\
200*989ce049SDarwin Rambo 		.offset = (_offset),					\
201*989ce049SDarwin Rambo 		.status_bit = (_status_bit),				\
202*989ce049SDarwin Rambo 		.en_bit = (_en_bit),					\
203*989ce049SDarwin Rambo 		.hw_sw_sel_bit = (_hw_sw_sel_bit),			\
204*989ce049SDarwin Rambo 		.flags = FLAG(GATE, HW)|FLAG(GATE, SW)|			\
205*989ce049SDarwin Rambo 			FLAG(GATE, NO_DISABLE)|FLAG(GATE, EXISTS),	\
206*989ce049SDarwin Rambo 	}
207*989ce049SDarwin Rambo 
208*989ce049SDarwin Rambo /* A software-only gate */
209*989ce049SDarwin Rambo #define SW_ONLY_GATE(_offset, _status_bit, _en_bit)			\
210*989ce049SDarwin Rambo 	{								\
211*989ce049SDarwin Rambo 		.offset = (_offset),					\
212*989ce049SDarwin Rambo 		.status_bit = (_status_bit),				\
213*989ce049SDarwin Rambo 		.en_bit = (_en_bit),					\
214*989ce049SDarwin Rambo 		.flags = FLAG(GATE, SW)|FLAG(GATE, SW_MANAGED)|		\
215*989ce049SDarwin Rambo 			FLAG(GATE, ENABLED)|FLAG(GATE, EXISTS),		\
216*989ce049SDarwin Rambo 	}
217*989ce049SDarwin Rambo 
218*989ce049SDarwin Rambo /* A hardware-only gate */
219*989ce049SDarwin Rambo #define HW_ONLY_GATE(_offset, _status_bit)				\
220*989ce049SDarwin Rambo 	{								\
221*989ce049SDarwin Rambo 		.offset = (_offset),					\
222*989ce049SDarwin Rambo 		.status_bit = (_status_bit),				\
223*989ce049SDarwin Rambo 		.flags = FLAG(GATE, HW)|FLAG(GATE, EXISTS),		\
224*989ce049SDarwin Rambo 	}
225*989ce049SDarwin Rambo 
226*989ce049SDarwin Rambo /*
227*989ce049SDarwin Rambo  * Each clock can have zero, one, or two dividers which change the
228*989ce049SDarwin Rambo  * output rate of the clock.  Each divider can be either fixed or
229*989ce049SDarwin Rambo  * variable.  If there are two dividers, they are the "pre-divider"
230*989ce049SDarwin Rambo  * and the "regular" or "downstream" divider.  If there is only one,
231*989ce049SDarwin Rambo  * there is no pre-divider.
232*989ce049SDarwin Rambo  *
233*989ce049SDarwin Rambo  * A fixed divider is any non-zero (positive) value, and it
234*989ce049SDarwin Rambo  * indicates how the input rate is affected by the divider.
235*989ce049SDarwin Rambo  *
236*989ce049SDarwin Rambo  * The value of a variable divider is maintained in a sub-field of a
237*989ce049SDarwin Rambo  * 32-bit divider register.  The position of the field in the
238*989ce049SDarwin Rambo  * register is defined by its offset and width.  The value recorded
239*989ce049SDarwin Rambo  * in this field is always 1 less than the value it represents.
240*989ce049SDarwin Rambo  *
241*989ce049SDarwin Rambo  * In addition, a variable divider can indicate that some subset
242*989ce049SDarwin Rambo  * of its bits represent a "fractional" part of the divider.  Such
243*989ce049SDarwin Rambo  * bits comprise the low-order portion of the divider field, and can
244*989ce049SDarwin Rambo  * be viewed as representing the portion of the divider that lies to
245*989ce049SDarwin Rambo  * the right of the decimal point.  Most variable dividers have zero
246*989ce049SDarwin Rambo  * fractional bits.  Variable dividers with non-zero fraction width
247*989ce049SDarwin Rambo  * still record a value 1 less than the value they represent; the
248*989ce049SDarwin Rambo  * added 1 does *not* affect the low-order bit in this case, it
249*989ce049SDarwin Rambo  * affects the bits above the fractional part only.  (Often in this
250*989ce049SDarwin Rambo  * code a divider field value is distinguished from the value it
251*989ce049SDarwin Rambo  * represents by referring to the latter as a "divisor".)
252*989ce049SDarwin Rambo  *
253*989ce049SDarwin Rambo  * In order to avoid dealing with fractions, divider arithmetic is
254*989ce049SDarwin Rambo  * performed using "scaled" values.  A scaled value is one that's
255*989ce049SDarwin Rambo  * been left-shifted by the fractional width of a divider.  Dividing
256*989ce049SDarwin Rambo  * a scaled value by a scaled divisor produces the desired quotient
257*989ce049SDarwin Rambo  * without loss of precision and without any other special handling
258*989ce049SDarwin Rambo  * for fractions.
259*989ce049SDarwin Rambo  *
260*989ce049SDarwin Rambo  * The recorded value of a variable divider can be modified.  To
261*989ce049SDarwin Rambo  * modify either divider (or both), a clock must be enabled (i.e.,
262*989ce049SDarwin Rambo  * using its gate).  In addition, a trigger register (described
263*989ce049SDarwin Rambo  * below) must be used to commit the change, and polled to verify
264*989ce049SDarwin Rambo  * the change is complete.
265*989ce049SDarwin Rambo  */
266*989ce049SDarwin Rambo struct bcm_clk_div {
267*989ce049SDarwin Rambo 	union {
268*989ce049SDarwin Rambo 		struct {	/* variable divider */
269*989ce049SDarwin Rambo 			u32 offset;	/* divider register offset */
270*989ce049SDarwin Rambo 			u32 shift;	/* field shift */
271*989ce049SDarwin Rambo 			u32 width;	/* field width */
272*989ce049SDarwin Rambo 			u32 frac_width;	/* field fraction width */
273*989ce049SDarwin Rambo 
274*989ce049SDarwin Rambo 			u64 scaled_div;	/* scaled divider value */
275*989ce049SDarwin Rambo 		};
276*989ce049SDarwin Rambo 		u32 fixed;	/* non-zero fixed divider value */
277*989ce049SDarwin Rambo 	};
278*989ce049SDarwin Rambo 	u32 flags;		/* BCM_CLK_DIV_FLAGS_* below */
279*989ce049SDarwin Rambo };
280*989ce049SDarwin Rambo 
281*989ce049SDarwin Rambo /*
282*989ce049SDarwin Rambo  * Divider flags:
283*989ce049SDarwin Rambo  *   EXISTS means this divider exists
284*989ce049SDarwin Rambo  *   FIXED means it is a fixed-rate divider
285*989ce049SDarwin Rambo  */
286*989ce049SDarwin Rambo #define BCM_CLK_DIV_FLAGS_EXISTS	((u32)1 << 0)	/* Divider is valid */
287*989ce049SDarwin Rambo #define BCM_CLK_DIV_FLAGS_FIXED		((u32)1 << 1)	/* Fixed-value */
288*989ce049SDarwin Rambo 
289*989ce049SDarwin Rambo /* Divider initialization macros */
290*989ce049SDarwin Rambo 
291*989ce049SDarwin Rambo /* A fixed (non-zero) divider */
292*989ce049SDarwin Rambo #define FIXED_DIVIDER(_value)						\
293*989ce049SDarwin Rambo 	{								\
294*989ce049SDarwin Rambo 		.fixed = (_value),					\
295*989ce049SDarwin Rambo 		.flags = FLAG(DIV, EXISTS)|FLAG(DIV, FIXED),		\
296*989ce049SDarwin Rambo 	}
297*989ce049SDarwin Rambo 
298*989ce049SDarwin Rambo /* A divider with an integral divisor */
299*989ce049SDarwin Rambo #define DIVIDER(_offset, _shift, _width)				\
300*989ce049SDarwin Rambo 	{								\
301*989ce049SDarwin Rambo 		.offset = (_offset),					\
302*989ce049SDarwin Rambo 		.shift = (_shift),					\
303*989ce049SDarwin Rambo 		.width = (_width),					\
304*989ce049SDarwin Rambo 		.scaled_div = BAD_SCALED_DIV_VALUE,			\
305*989ce049SDarwin Rambo 		.flags = FLAG(DIV, EXISTS),				\
306*989ce049SDarwin Rambo 	}
307*989ce049SDarwin Rambo 
308*989ce049SDarwin Rambo /* A divider whose divisor has an integer and fractional part */
309*989ce049SDarwin Rambo #define FRAC_DIVIDER(_offset, _shift, _width, _frac_width)		\
310*989ce049SDarwin Rambo 	{								\
311*989ce049SDarwin Rambo 		.offset = (_offset),					\
312*989ce049SDarwin Rambo 		.shift = (_shift),					\
313*989ce049SDarwin Rambo 		.width = (_width),					\
314*989ce049SDarwin Rambo 		.frac_width = (_frac_width),				\
315*989ce049SDarwin Rambo 		.scaled_div = BAD_SCALED_DIV_VALUE,			\
316*989ce049SDarwin Rambo 		.flags = FLAG(DIV, EXISTS),				\
317*989ce049SDarwin Rambo 	}
318*989ce049SDarwin Rambo 
319*989ce049SDarwin Rambo /*
320*989ce049SDarwin Rambo  * Clocks may have multiple "parent" clocks.  If there is more than
321*989ce049SDarwin Rambo  * one, a selector must be specified to define which of the parent
322*989ce049SDarwin Rambo  * clocks is currently in use.  The selected clock is indicated in a
323*989ce049SDarwin Rambo  * sub-field of a 32-bit selector register.  The range of
324*989ce049SDarwin Rambo  * representable selector values typically exceeds the number of
325*989ce049SDarwin Rambo  * available parent clocks.  Occasionally the reset value of a
326*989ce049SDarwin Rambo  * selector field is explicitly set to a (specific) value that does
327*989ce049SDarwin Rambo  * not correspond to a defined input clock.
328*989ce049SDarwin Rambo  *
329*989ce049SDarwin Rambo  * We register all known parent clocks with the common clock code
330*989ce049SDarwin Rambo  * using a packed array (i.e., no empty slots) of (parent) clock
331*989ce049SDarwin Rambo  * names, and refer to them later using indexes into that array.
332*989ce049SDarwin Rambo  * We maintain an array of selector values indexed by common clock
333*989ce049SDarwin Rambo  * index values in order to map between these common clock indexes
334*989ce049SDarwin Rambo  * and the selector values used by the hardware.
335*989ce049SDarwin Rambo  *
336*989ce049SDarwin Rambo  * Like dividers, a selector can be modified, but to do so a clock
337*989ce049SDarwin Rambo  * must be enabled, and a trigger must be used to commit the change.
338*989ce049SDarwin Rambo  */
339*989ce049SDarwin Rambo struct bcm_clk_sel {
340*989ce049SDarwin Rambo 	u32 offset;		/* selector register offset */
341*989ce049SDarwin Rambo 	u32 shift;		/* field shift */
342*989ce049SDarwin Rambo 	u32 width;		/* field width */
343*989ce049SDarwin Rambo 
344*989ce049SDarwin Rambo 	u32 parent_count;	/* number of entries in parent_sel[] */
345*989ce049SDarwin Rambo 	u32 *parent_sel;	/* array of parent selector values */
346*989ce049SDarwin Rambo 	u8 clk_index;		/* current selected index in parent_sel[] */
347*989ce049SDarwin Rambo };
348*989ce049SDarwin Rambo 
349*989ce049SDarwin Rambo /* Selector initialization macro */
350*989ce049SDarwin Rambo #define SELECTOR(_offset, _shift, _width)				\
351*989ce049SDarwin Rambo 	{								\
352*989ce049SDarwin Rambo 		.offset = (_offset),					\
353*989ce049SDarwin Rambo 		.shift = (_shift),					\
354*989ce049SDarwin Rambo 		.width = (_width),					\
355*989ce049SDarwin Rambo 		.clk_index = BAD_CLK_INDEX,				\
356*989ce049SDarwin Rambo 	}
357*989ce049SDarwin Rambo 
358*989ce049SDarwin Rambo /*
359*989ce049SDarwin Rambo  * Making changes to a variable divider or a selector for a clock
360*989ce049SDarwin Rambo  * requires the use of a trigger.  A trigger is defined by a single
361*989ce049SDarwin Rambo  * bit within a register.  To signal a change, a 1 is written into
362*989ce049SDarwin Rambo  * that bit.  To determine when the change has been completed, that
363*989ce049SDarwin Rambo  * trigger bit is polled; the read value will be 1 while the change
364*989ce049SDarwin Rambo  * is in progress, and 0 when it is complete.
365*989ce049SDarwin Rambo  *
366*989ce049SDarwin Rambo  * Occasionally a clock will have more than one trigger.  In this
367*989ce049SDarwin Rambo  * case, the "pre-trigger" will be used when changing a clock's
368*989ce049SDarwin Rambo  * selector and/or its pre-divider.
369*989ce049SDarwin Rambo  */
370*989ce049SDarwin Rambo struct bcm_clk_trig {
371*989ce049SDarwin Rambo 	u32 offset;		/* trigger register offset */
372*989ce049SDarwin Rambo 	u32 bit;		/* trigger bit */
373*989ce049SDarwin Rambo 	u32 flags;		/* BCM_CLK_TRIG_FLAGS_* below */
374*989ce049SDarwin Rambo };
375*989ce049SDarwin Rambo 
376*989ce049SDarwin Rambo /*
377*989ce049SDarwin Rambo  * Trigger flags:
378*989ce049SDarwin Rambo  *   EXISTS means this trigger exists
379*989ce049SDarwin Rambo  */
380*989ce049SDarwin Rambo #define BCM_CLK_TRIG_FLAGS_EXISTS	((u32)1 << 0)	/* Trigger is valid */
381*989ce049SDarwin Rambo 
382*989ce049SDarwin Rambo /* Trigger initialization macro */
383*989ce049SDarwin Rambo #define TRIGGER(_offset, _bit)						\
384*989ce049SDarwin Rambo 	{								\
385*989ce049SDarwin Rambo 		.offset = (_offset),					\
386*989ce049SDarwin Rambo 		.bit = (_bit),						\
387*989ce049SDarwin Rambo 		.flags = FLAG(TRIG, EXISTS),				\
388*989ce049SDarwin Rambo 	}
389*989ce049SDarwin Rambo 
390*989ce049SDarwin Rambo struct bus_clk_data {
391*989ce049SDarwin Rambo 	struct bcm_clk_gate gate;
392*989ce049SDarwin Rambo };
393*989ce049SDarwin Rambo 
394*989ce049SDarwin Rambo struct core_clk_data {
395*989ce049SDarwin Rambo 	struct bcm_clk_gate gate;
396*989ce049SDarwin Rambo };
397*989ce049SDarwin Rambo 
398*989ce049SDarwin Rambo struct peri_clk_data {
399*989ce049SDarwin Rambo 	struct bcm_clk_gate gate;
400*989ce049SDarwin Rambo 	struct bcm_clk_trig pre_trig;
401*989ce049SDarwin Rambo 	struct bcm_clk_div pre_div;
402*989ce049SDarwin Rambo 	struct bcm_clk_trig trig;
403*989ce049SDarwin Rambo 	struct bcm_clk_div div;
404*989ce049SDarwin Rambo 	struct bcm_clk_sel sel;
405*989ce049SDarwin Rambo 	const char *clocks[];	/* must be last; use CLOCKS() to declare */
406*989ce049SDarwin Rambo };
407*989ce049SDarwin Rambo #define CLOCKS(...)	{ __VA_ARGS__, NULL, }
408*989ce049SDarwin Rambo #define NO_CLOCKS	{ NULL, }	/* Must use of no parent clocks */
409*989ce049SDarwin Rambo 
410*989ce049SDarwin Rambo struct refclk {
411*989ce049SDarwin Rambo 	struct clk clk;
412*989ce049SDarwin Rambo };
413*989ce049SDarwin Rambo 
414*989ce049SDarwin Rambo struct peri_clock {
415*989ce049SDarwin Rambo 	struct clk clk;
416*989ce049SDarwin Rambo 	struct peri_clk_data *data;
417*989ce049SDarwin Rambo };
418*989ce049SDarwin Rambo 
419*989ce049SDarwin Rambo struct ccu_clock {
420*989ce049SDarwin Rambo 	struct clk clk;
421*989ce049SDarwin Rambo 
422*989ce049SDarwin Rambo 	int num_policy_masks;
423*989ce049SDarwin Rambo 	unsigned long policy_freq_offset;
424*989ce049SDarwin Rambo 	int freq_bit_shift;	/* 8 for most CCUs */
425*989ce049SDarwin Rambo 	unsigned long policy_ctl_offset;
426*989ce049SDarwin Rambo 	unsigned long policy0_mask_offset;
427*989ce049SDarwin Rambo 	unsigned long policy1_mask_offset;
428*989ce049SDarwin Rambo 	unsigned long policy2_mask_offset;
429*989ce049SDarwin Rambo 	unsigned long policy3_mask_offset;
430*989ce049SDarwin Rambo 	unsigned long policy0_mask2_offset;
431*989ce049SDarwin Rambo 	unsigned long policy1_mask2_offset;
432*989ce049SDarwin Rambo 	unsigned long policy2_mask2_offset;
433*989ce049SDarwin Rambo 	unsigned long policy3_mask2_offset;
434*989ce049SDarwin Rambo 	unsigned long lvm_en_offset;
435*989ce049SDarwin Rambo 
436*989ce049SDarwin Rambo 	int freq_id;
437*989ce049SDarwin Rambo 	unsigned long *freq_tbl;
438*989ce049SDarwin Rambo };
439*989ce049SDarwin Rambo 
440*989ce049SDarwin Rambo struct bus_clock {
441*989ce049SDarwin Rambo 	struct clk clk;
442*989ce049SDarwin Rambo 	struct bus_clk_data *data;
443*989ce049SDarwin Rambo 	unsigned long *freq_tbl;
444*989ce049SDarwin Rambo };
445*989ce049SDarwin Rambo 
446*989ce049SDarwin Rambo struct ref_clock {
447*989ce049SDarwin Rambo 	struct clk clk;
448*989ce049SDarwin Rambo };
449*989ce049SDarwin Rambo 
is_same_clock(struct clk * a,struct clk * b)450*989ce049SDarwin Rambo static inline int is_same_clock(struct clk *a, struct clk *b)
451*989ce049SDarwin Rambo {
452*989ce049SDarwin Rambo 	return (a == b);
453*989ce049SDarwin Rambo }
454*989ce049SDarwin Rambo 
455*989ce049SDarwin Rambo #define to_clk(p) (&((p)->clk))
456*989ce049SDarwin Rambo #define name_to_clk(name) (&((name##_clk).clk))
457*989ce049SDarwin Rambo /* declare a struct clk_lookup */
458*989ce049SDarwin Rambo #define CLK_LK(name) \
459*989ce049SDarwin Rambo {.con_id = __stringify(name##_clk), .clk = name_to_clk(name),}
460*989ce049SDarwin Rambo 
to_refclk(struct clk * clock)461*989ce049SDarwin Rambo static inline struct refclk *to_refclk(struct clk *clock)
462*989ce049SDarwin Rambo {
463*989ce049SDarwin Rambo 	return container_of(clock, struct refclk, clk);
464*989ce049SDarwin Rambo }
465*989ce049SDarwin Rambo 
to_peri_clk(struct clk * clock)466*989ce049SDarwin Rambo static inline struct peri_clock *to_peri_clk(struct clk *clock)
467*989ce049SDarwin Rambo {
468*989ce049SDarwin Rambo 	return container_of(clock, struct peri_clock, clk);
469*989ce049SDarwin Rambo }
470*989ce049SDarwin Rambo 
to_ccu_clk(struct clk * clock)471*989ce049SDarwin Rambo static inline struct ccu_clock *to_ccu_clk(struct clk *clock)
472*989ce049SDarwin Rambo {
473*989ce049SDarwin Rambo 	return container_of(clock, struct ccu_clock, clk);
474*989ce049SDarwin Rambo }
475*989ce049SDarwin Rambo 
to_bus_clk(struct clk * clock)476*989ce049SDarwin Rambo static inline struct bus_clock *to_bus_clk(struct clk *clock)
477*989ce049SDarwin Rambo {
478*989ce049SDarwin Rambo 	return container_of(clock, struct bus_clock, clk);
479*989ce049SDarwin Rambo }
480*989ce049SDarwin Rambo 
to_ref_clk(struct clk * clock)481*989ce049SDarwin Rambo static inline struct ref_clock *to_ref_clk(struct clk *clock)
482*989ce049SDarwin Rambo {
483*989ce049SDarwin Rambo 	return container_of(clock, struct ref_clock, clk);
484*989ce049SDarwin Rambo }
485*989ce049SDarwin Rambo 
486*989ce049SDarwin Rambo extern struct clk_ops peri_clk_ops;
487*989ce049SDarwin Rambo extern struct clk_ops ccu_clk_ops;
488*989ce049SDarwin Rambo extern struct clk_ops bus_clk_ops;
489*989ce049SDarwin Rambo extern struct clk_ops ref_clk_ops;
490*989ce049SDarwin Rambo 
491*989ce049SDarwin Rambo extern int clk_get_and_enable(char *clkstr);
492