xref: /OK3568_Linux_fs/kernel/drivers/clk/tegra/clk-super.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/kernel.h>
7*4882a593Smuzhiyun #include <linux/io.h>
8*4882a593Smuzhiyun #include <linux/delay.h>
9*4882a593Smuzhiyun #include <linux/err.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/clk-provider.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "clk.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #define SUPER_STATE_IDLE 0
16*4882a593Smuzhiyun #define SUPER_STATE_RUN 1
17*4882a593Smuzhiyun #define SUPER_STATE_IRQ 2
18*4882a593Smuzhiyun #define SUPER_STATE_FIQ 3
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #define SUPER_STATE_SHIFT 28
21*4882a593Smuzhiyun #define SUPER_STATE_MASK ((BIT(SUPER_STATE_IDLE) | BIT(SUPER_STATE_RUN) | \
22*4882a593Smuzhiyun 			   BIT(SUPER_STATE_IRQ) | BIT(SUPER_STATE_FIQ))	\
23*4882a593Smuzhiyun 			  << SUPER_STATE_SHIFT)
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #define SUPER_LP_DIV2_BYPASS (1 << 16)
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define super_state(s) (BIT(s) << SUPER_STATE_SHIFT)
28*4882a593Smuzhiyun #define super_state_to_src_shift(m, s) ((m->width * s))
29*4882a593Smuzhiyun #define super_state_to_src_mask(m) (((1 << m->width) - 1))
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define CCLK_SRC_PLLP_OUT0 4
32*4882a593Smuzhiyun #define CCLK_SRC_PLLP_OUT4 5
33*4882a593Smuzhiyun 
clk_super_get_parent(struct clk_hw * hw)34*4882a593Smuzhiyun static u8 clk_super_get_parent(struct clk_hw *hw)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
37*4882a593Smuzhiyun 	u32 val, state;
38*4882a593Smuzhiyun 	u8 source, shift;
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	val = readl_relaxed(mux->reg);
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	state = val & SUPER_STATE_MASK;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
45*4882a593Smuzhiyun 	       (state != super_state(SUPER_STATE_IDLE)));
46*4882a593Smuzhiyun 	shift = (state == super_state(SUPER_STATE_IDLE)) ?
47*4882a593Smuzhiyun 		super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
48*4882a593Smuzhiyun 		super_state_to_src_shift(mux, SUPER_STATE_RUN);
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	source = (val >> shift) & super_state_to_src_mask(mux);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	/*
53*4882a593Smuzhiyun 	 * If LP_DIV2_BYPASS is not set and PLLX is current parent then
54*4882a593Smuzhiyun 	 * PLLX/2 is the input source to CCLKLP.
55*4882a593Smuzhiyun 	 */
56*4882a593Smuzhiyun 	if ((mux->flags & TEGRA_DIVIDER_2) && !(val & SUPER_LP_DIV2_BYPASS) &&
57*4882a593Smuzhiyun 	    (source == mux->pllx_index))
58*4882a593Smuzhiyun 		source = mux->div2_index;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	return source;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun 
clk_super_set_parent(struct clk_hw * hw,u8 index)63*4882a593Smuzhiyun static int clk_super_set_parent(struct clk_hw *hw, u8 index)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun 	struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
66*4882a593Smuzhiyun 	u32 val, state;
67*4882a593Smuzhiyun 	int err = 0;
68*4882a593Smuzhiyun 	u8 parent_index, shift;
69*4882a593Smuzhiyun 	unsigned long flags = 0;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	if (mux->lock)
72*4882a593Smuzhiyun 		spin_lock_irqsave(mux->lock, flags);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	val = readl_relaxed(mux->reg);
75*4882a593Smuzhiyun 	state = val & SUPER_STATE_MASK;
76*4882a593Smuzhiyun 	BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
77*4882a593Smuzhiyun 	       (state != super_state(SUPER_STATE_IDLE)));
78*4882a593Smuzhiyun 	shift = (state == super_state(SUPER_STATE_IDLE)) ?
79*4882a593Smuzhiyun 		super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
80*4882a593Smuzhiyun 		super_state_to_src_shift(mux, SUPER_STATE_RUN);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	/*
83*4882a593Smuzhiyun 	 * For LP mode super-clock switch between PLLX direct
84*4882a593Smuzhiyun 	 * and divided-by-2 outputs is allowed only when other
85*4882a593Smuzhiyun 	 * than PLLX clock source is current parent.
86*4882a593Smuzhiyun 	 */
87*4882a593Smuzhiyun 	if ((mux->flags & TEGRA_DIVIDER_2) && ((index == mux->div2_index) ||
88*4882a593Smuzhiyun 					       (index == mux->pllx_index))) {
89*4882a593Smuzhiyun 		parent_index = clk_super_get_parent(hw);
90*4882a593Smuzhiyun 		if ((parent_index == mux->div2_index) ||
91*4882a593Smuzhiyun 		    (parent_index == mux->pllx_index)) {
92*4882a593Smuzhiyun 			err = -EINVAL;
93*4882a593Smuzhiyun 			goto out;
94*4882a593Smuzhiyun 		}
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 		val ^= SUPER_LP_DIV2_BYPASS;
97*4882a593Smuzhiyun 		writel_relaxed(val, mux->reg);
98*4882a593Smuzhiyun 		udelay(2);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 		if (index == mux->div2_index)
101*4882a593Smuzhiyun 			index = mux->pllx_index;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	/* enable PLLP branches to CPU before selecting PLLP source */
105*4882a593Smuzhiyun 	if ((mux->flags & TEGRA210_CPU_CLK) &&
106*4882a593Smuzhiyun 	    (index == CCLK_SRC_PLLP_OUT0 || index == CCLK_SRC_PLLP_OUT4))
107*4882a593Smuzhiyun 		tegra_clk_set_pllp_out_cpu(true);
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	val &= ~((super_state_to_src_mask(mux)) << shift);
110*4882a593Smuzhiyun 	val |= (index & (super_state_to_src_mask(mux))) << shift;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	writel_relaxed(val, mux->reg);
113*4882a593Smuzhiyun 	udelay(2);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	/* disable PLLP branches to CPU if not used */
116*4882a593Smuzhiyun 	if ((mux->flags & TEGRA210_CPU_CLK) &&
117*4882a593Smuzhiyun 	    index != CCLK_SRC_PLLP_OUT0 && index != CCLK_SRC_PLLP_OUT4)
118*4882a593Smuzhiyun 		tegra_clk_set_pllp_out_cpu(false);
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun out:
121*4882a593Smuzhiyun 	if (mux->lock)
122*4882a593Smuzhiyun 		spin_unlock_irqrestore(mux->lock, flags);
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	return err;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
clk_super_mux_restore_context(struct clk_hw * hw)127*4882a593Smuzhiyun static void clk_super_mux_restore_context(struct clk_hw *hw)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	int parent_id;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	parent_id = clk_hw_get_parent_index(hw);
132*4882a593Smuzhiyun 	if (WARN_ON(parent_id < 0))
133*4882a593Smuzhiyun 		return;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	clk_super_set_parent(hw, parent_id);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun static const struct clk_ops tegra_clk_super_mux_ops = {
139*4882a593Smuzhiyun 	.get_parent = clk_super_get_parent,
140*4882a593Smuzhiyun 	.set_parent = clk_super_set_parent,
141*4882a593Smuzhiyun 	.restore_context = clk_super_mux_restore_context,
142*4882a593Smuzhiyun };
143*4882a593Smuzhiyun 
clk_super_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)144*4882a593Smuzhiyun static long clk_super_round_rate(struct clk_hw *hw, unsigned long rate,
145*4882a593Smuzhiyun 				 unsigned long *parent_rate)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
148*4882a593Smuzhiyun 	struct clk_hw *div_hw = &super->frac_div.hw;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	__clk_hw_set_clk(div_hw, hw);
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	return super->div_ops->round_rate(div_hw, rate, parent_rate);
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun 
clk_super_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)155*4882a593Smuzhiyun static unsigned long clk_super_recalc_rate(struct clk_hw *hw,
156*4882a593Smuzhiyun 					   unsigned long parent_rate)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
159*4882a593Smuzhiyun 	struct clk_hw *div_hw = &super->frac_div.hw;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	__clk_hw_set_clk(div_hw, hw);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	return super->div_ops->recalc_rate(div_hw, parent_rate);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
clk_super_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)166*4882a593Smuzhiyun static int clk_super_set_rate(struct clk_hw *hw, unsigned long rate,
167*4882a593Smuzhiyun 			      unsigned long parent_rate)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
170*4882a593Smuzhiyun 	struct clk_hw *div_hw = &super->frac_div.hw;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	__clk_hw_set_clk(div_hw, hw);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	return super->div_ops->set_rate(div_hw, rate, parent_rate);
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
clk_super_restore_context(struct clk_hw * hw)177*4882a593Smuzhiyun static void clk_super_restore_context(struct clk_hw *hw)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	struct tegra_clk_super_mux *super = to_clk_super_mux(hw);
180*4882a593Smuzhiyun 	struct clk_hw *div_hw = &super->frac_div.hw;
181*4882a593Smuzhiyun 	int parent_id;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	parent_id = clk_hw_get_parent_index(hw);
184*4882a593Smuzhiyun 	if (WARN_ON(parent_id < 0))
185*4882a593Smuzhiyun 		return;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	super->div_ops->restore_context(div_hw);
188*4882a593Smuzhiyun 	clk_super_set_parent(hw, parent_id);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun const struct clk_ops tegra_clk_super_ops = {
192*4882a593Smuzhiyun 	.get_parent = clk_super_get_parent,
193*4882a593Smuzhiyun 	.set_parent = clk_super_set_parent,
194*4882a593Smuzhiyun 	.set_rate = clk_super_set_rate,
195*4882a593Smuzhiyun 	.round_rate = clk_super_round_rate,
196*4882a593Smuzhiyun 	.recalc_rate = clk_super_recalc_rate,
197*4882a593Smuzhiyun 	.restore_context = clk_super_restore_context,
198*4882a593Smuzhiyun };
199*4882a593Smuzhiyun 
tegra_clk_register_super_mux(const char * name,const char ** parent_names,u8 num_parents,unsigned long flags,void __iomem * reg,u8 clk_super_flags,u8 width,u8 pllx_index,u8 div2_index,spinlock_t * lock)200*4882a593Smuzhiyun struct clk *tegra_clk_register_super_mux(const char *name,
201*4882a593Smuzhiyun 		const char **parent_names, u8 num_parents,
202*4882a593Smuzhiyun 		unsigned long flags, void __iomem *reg, u8 clk_super_flags,
203*4882a593Smuzhiyun 		u8 width, u8 pllx_index, u8 div2_index, spinlock_t *lock)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	struct tegra_clk_super_mux *super;
206*4882a593Smuzhiyun 	struct clk *clk;
207*4882a593Smuzhiyun 	struct clk_init_data init;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	super = kzalloc(sizeof(*super), GFP_KERNEL);
210*4882a593Smuzhiyun 	if (!super)
211*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	init.name = name;
214*4882a593Smuzhiyun 	init.ops = &tegra_clk_super_mux_ops;
215*4882a593Smuzhiyun 	init.flags = flags;
216*4882a593Smuzhiyun 	init.parent_names = parent_names;
217*4882a593Smuzhiyun 	init.num_parents = num_parents;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	super->reg = reg;
220*4882a593Smuzhiyun 	super->pllx_index = pllx_index;
221*4882a593Smuzhiyun 	super->div2_index = div2_index;
222*4882a593Smuzhiyun 	super->lock = lock;
223*4882a593Smuzhiyun 	super->width = width;
224*4882a593Smuzhiyun 	super->flags = clk_super_flags;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	/* Data in .init is copied by clk_register(), so stack variable OK */
227*4882a593Smuzhiyun 	super->hw.init = &init;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	clk = clk_register(NULL, &super->hw);
230*4882a593Smuzhiyun 	if (IS_ERR(clk))
231*4882a593Smuzhiyun 		kfree(super);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	return clk;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
tegra_clk_register_super_clk(const char * name,const char * const * parent_names,u8 num_parents,unsigned long flags,void __iomem * reg,u8 clk_super_flags,spinlock_t * lock)236*4882a593Smuzhiyun struct clk *tegra_clk_register_super_clk(const char *name,
237*4882a593Smuzhiyun 		const char * const *parent_names, u8 num_parents,
238*4882a593Smuzhiyun 		unsigned long flags, void __iomem *reg, u8 clk_super_flags,
239*4882a593Smuzhiyun 		spinlock_t *lock)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun 	struct tegra_clk_super_mux *super;
242*4882a593Smuzhiyun 	struct clk *clk;
243*4882a593Smuzhiyun 	struct clk_init_data init;
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	super = kzalloc(sizeof(*super), GFP_KERNEL);
246*4882a593Smuzhiyun 	if (!super)
247*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	init.name = name;
250*4882a593Smuzhiyun 	init.ops = &tegra_clk_super_ops;
251*4882a593Smuzhiyun 	init.flags = flags;
252*4882a593Smuzhiyun 	init.parent_names = parent_names;
253*4882a593Smuzhiyun 	init.num_parents = num_parents;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	super->reg = reg;
256*4882a593Smuzhiyun 	super->lock = lock;
257*4882a593Smuzhiyun 	super->width = 4;
258*4882a593Smuzhiyun 	super->flags = clk_super_flags;
259*4882a593Smuzhiyun 	super->frac_div.reg = reg + 4;
260*4882a593Smuzhiyun 	super->frac_div.shift = 16;
261*4882a593Smuzhiyun 	super->frac_div.width = 8;
262*4882a593Smuzhiyun 	super->frac_div.frac_width = 1;
263*4882a593Smuzhiyun 	super->frac_div.lock = lock;
264*4882a593Smuzhiyun 	super->div_ops = &tegra_clk_frac_div_ops;
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	/* Data in .init is copied by clk_register(), so stack variable OK */
267*4882a593Smuzhiyun 	super->hw.init = &init;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	clk = clk_register(NULL, &super->hw);
270*4882a593Smuzhiyun 	if (IS_ERR(clk))
271*4882a593Smuzhiyun 		kfree(super);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return clk;
274*4882a593Smuzhiyun }
275