xref: /OK3568_Linux_fs/kernel/drivers/clk/baikal-t1/ccu-pll.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Authors:
6*4882a593Smuzhiyun  *   Serge Semin <Sergey.Semin@baikalelectronics.ru>
7*4882a593Smuzhiyun  *   Dmitry Dunaev <dmitry.dunaev@baikalelectronics.ru>
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * Baikal-T1 CCU PLL interface driver
10*4882a593Smuzhiyun  */
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #define pr_fmt(fmt) "bt1-ccu-pll: " fmt
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/printk.h>
16*4882a593Smuzhiyun #include <linux/limits.h>
17*4882a593Smuzhiyun #include <linux/bits.h>
18*4882a593Smuzhiyun #include <linux/bitfield.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/clk-provider.h>
21*4882a593Smuzhiyun #include <linux/of.h>
22*4882a593Smuzhiyun #include <linux/spinlock.h>
23*4882a593Smuzhiyun #include <linux/regmap.h>
24*4882a593Smuzhiyun #include <linux/iopoll.h>
25*4882a593Smuzhiyun #include <linux/time64.h>
26*4882a593Smuzhiyun #include <linux/rational.h>
27*4882a593Smuzhiyun #include <linux/debugfs.h>
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #include "ccu-pll.h"
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define CCU_PLL_CTL			0x000
32*4882a593Smuzhiyun #define CCU_PLL_CTL_EN			BIT(0)
33*4882a593Smuzhiyun #define CCU_PLL_CTL_RST			BIT(1)
34*4882a593Smuzhiyun #define CCU_PLL_CTL_CLKR_FLD		2
35*4882a593Smuzhiyun #define CCU_PLL_CTL_CLKR_MASK		GENMASK(7, CCU_PLL_CTL_CLKR_FLD)
36*4882a593Smuzhiyun #define CCU_PLL_CTL_CLKF_FLD		8
37*4882a593Smuzhiyun #define CCU_PLL_CTL_CLKF_MASK		GENMASK(20, CCU_PLL_CTL_CLKF_FLD)
38*4882a593Smuzhiyun #define CCU_PLL_CTL_CLKOD_FLD		21
39*4882a593Smuzhiyun #define CCU_PLL_CTL_CLKOD_MASK		GENMASK(24, CCU_PLL_CTL_CLKOD_FLD)
40*4882a593Smuzhiyun #define CCU_PLL_CTL_BYPASS		BIT(30)
41*4882a593Smuzhiyun #define CCU_PLL_CTL_LOCK		BIT(31)
42*4882a593Smuzhiyun #define CCU_PLL_CTL1			0x004
43*4882a593Smuzhiyun #define CCU_PLL_CTL1_BWADJ_FLD		3
44*4882a593Smuzhiyun #define CCU_PLL_CTL1_BWADJ_MASK		GENMASK(14, CCU_PLL_CTL1_BWADJ_FLD)
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #define CCU_PLL_LOCK_CHECK_RETRIES	50
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #define CCU_PLL_NR_MAX \
49*4882a593Smuzhiyun 	((CCU_PLL_CTL_CLKR_MASK >> CCU_PLL_CTL_CLKR_FLD) + 1)
50*4882a593Smuzhiyun #define CCU_PLL_NF_MAX \
51*4882a593Smuzhiyun 	((CCU_PLL_CTL_CLKF_MASK >> (CCU_PLL_CTL_CLKF_FLD + 1)) + 1)
52*4882a593Smuzhiyun #define CCU_PLL_OD_MAX \
53*4882a593Smuzhiyun 	((CCU_PLL_CTL_CLKOD_MASK >> CCU_PLL_CTL_CLKOD_FLD) + 1)
54*4882a593Smuzhiyun #define CCU_PLL_NB_MAX \
55*4882a593Smuzhiyun 	((CCU_PLL_CTL1_BWADJ_MASK >> CCU_PLL_CTL1_BWADJ_FLD) + 1)
56*4882a593Smuzhiyun #define CCU_PLL_FDIV_MIN		427000UL
57*4882a593Smuzhiyun #define CCU_PLL_FDIV_MAX		3500000000UL
58*4882a593Smuzhiyun #define CCU_PLL_FOUT_MIN		200000000UL
59*4882a593Smuzhiyun #define CCU_PLL_FOUT_MAX		2500000000UL
60*4882a593Smuzhiyun #define CCU_PLL_FVCO_MIN		700000000UL
61*4882a593Smuzhiyun #define CCU_PLL_FVCO_MAX		3500000000UL
62*4882a593Smuzhiyun #define CCU_PLL_CLKOD_FACTOR		2
63*4882a593Smuzhiyun 
ccu_pll_lock_delay_us(unsigned long ref_clk,unsigned long nr)64*4882a593Smuzhiyun static inline unsigned long ccu_pll_lock_delay_us(unsigned long ref_clk,
65*4882a593Smuzhiyun 						  unsigned long nr)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	u64 us = 500ULL * nr * USEC_PER_SEC;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	do_div(us, ref_clk);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	return us;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
ccu_pll_calc_freq(unsigned long ref_clk,unsigned long nr,unsigned long nf,unsigned long od)74*4882a593Smuzhiyun static inline unsigned long ccu_pll_calc_freq(unsigned long ref_clk,
75*4882a593Smuzhiyun 					      unsigned long nr,
76*4882a593Smuzhiyun 					      unsigned long nf,
77*4882a593Smuzhiyun 					      unsigned long od)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	u64 tmp = ref_clk;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	do_div(tmp, nr);
82*4882a593Smuzhiyun 	tmp *= nf;
83*4882a593Smuzhiyun 	do_div(tmp, od);
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	return tmp;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun 
ccu_pll_reset(struct ccu_pll * pll,unsigned long ref_clk,unsigned long nr)88*4882a593Smuzhiyun static int ccu_pll_reset(struct ccu_pll *pll, unsigned long ref_clk,
89*4882a593Smuzhiyun 			 unsigned long nr)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun 	unsigned long ud, ut;
92*4882a593Smuzhiyun 	u32 val;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	ud = ccu_pll_lock_delay_us(ref_clk, nr);
95*4882a593Smuzhiyun 	ut = ud * CCU_PLL_LOCK_CHECK_RETRIES;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	regmap_update_bits(pll->sys_regs, pll->reg_ctl,
98*4882a593Smuzhiyun 			   CCU_PLL_CTL_RST, CCU_PLL_CTL_RST);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	return regmap_read_poll_timeout_atomic(pll->sys_regs, pll->reg_ctl, val,
101*4882a593Smuzhiyun 					       val & CCU_PLL_CTL_LOCK, ud, ut);
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun 
ccu_pll_enable(struct clk_hw * hw)104*4882a593Smuzhiyun static int ccu_pll_enable(struct clk_hw *hw)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun 	struct clk_hw *parent_hw = clk_hw_get_parent(hw);
107*4882a593Smuzhiyun 	struct ccu_pll *pll = to_ccu_pll(hw);
108*4882a593Smuzhiyun 	unsigned long flags;
109*4882a593Smuzhiyun 	u32 val = 0;
110*4882a593Smuzhiyun 	int ret;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	if (!parent_hw) {
113*4882a593Smuzhiyun 		pr_err("Can't enable '%s' with no parent", clk_hw_get_name(hw));
114*4882a593Smuzhiyun 		return -EINVAL;
115*4882a593Smuzhiyun 	}
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	regmap_read(pll->sys_regs, pll->reg_ctl, &val);
118*4882a593Smuzhiyun 	if (val & CCU_PLL_CTL_EN)
119*4882a593Smuzhiyun 		return 0;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	spin_lock_irqsave(&pll->lock, flags);
122*4882a593Smuzhiyun 	regmap_write(pll->sys_regs, pll->reg_ctl, val | CCU_PLL_CTL_EN);
123*4882a593Smuzhiyun 	ret = ccu_pll_reset(pll, clk_hw_get_rate(parent_hw),
124*4882a593Smuzhiyun 			    FIELD_GET(CCU_PLL_CTL_CLKR_MASK, val) + 1);
125*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pll->lock, flags);
126*4882a593Smuzhiyun 	if (ret)
127*4882a593Smuzhiyun 		pr_err("PLL '%s' reset timed out\n", clk_hw_get_name(hw));
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	return ret;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
ccu_pll_disable(struct clk_hw * hw)132*4882a593Smuzhiyun static void ccu_pll_disable(struct clk_hw *hw)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	struct ccu_pll *pll = to_ccu_pll(hw);
135*4882a593Smuzhiyun 	unsigned long flags;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	spin_lock_irqsave(&pll->lock, flags);
138*4882a593Smuzhiyun 	regmap_update_bits(pll->sys_regs, pll->reg_ctl, CCU_PLL_CTL_EN, 0);
139*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pll->lock, flags);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
ccu_pll_is_enabled(struct clk_hw * hw)142*4882a593Smuzhiyun static int ccu_pll_is_enabled(struct clk_hw *hw)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	struct ccu_pll *pll = to_ccu_pll(hw);
145*4882a593Smuzhiyun 	u32 val = 0;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	regmap_read(pll->sys_regs, pll->reg_ctl, &val);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	return !!(val & CCU_PLL_CTL_EN);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
ccu_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)152*4882a593Smuzhiyun static unsigned long ccu_pll_recalc_rate(struct clk_hw *hw,
153*4882a593Smuzhiyun 					 unsigned long parent_rate)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	struct ccu_pll *pll = to_ccu_pll(hw);
156*4882a593Smuzhiyun 	unsigned long nr, nf, od;
157*4882a593Smuzhiyun 	u32 val = 0;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	regmap_read(pll->sys_regs, pll->reg_ctl, &val);
160*4882a593Smuzhiyun 	nr = FIELD_GET(CCU_PLL_CTL_CLKR_MASK, val) + 1;
161*4882a593Smuzhiyun 	nf = FIELD_GET(CCU_PLL_CTL_CLKF_MASK, val) + 1;
162*4882a593Smuzhiyun 	od = FIELD_GET(CCU_PLL_CTL_CLKOD_MASK, val) + 1;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	return ccu_pll_calc_freq(parent_rate, nr, nf, od);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
ccu_pll_calc_factors(unsigned long rate,unsigned long parent_rate,unsigned long * nr,unsigned long * nf,unsigned long * od)167*4882a593Smuzhiyun static void ccu_pll_calc_factors(unsigned long rate, unsigned long parent_rate,
168*4882a593Smuzhiyun 				 unsigned long *nr, unsigned long *nf,
169*4882a593Smuzhiyun 				 unsigned long *od)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	unsigned long err, freq, min_err = ULONG_MAX;
172*4882a593Smuzhiyun 	unsigned long num, denom, n1, d1, nri;
173*4882a593Smuzhiyun 	unsigned long nr_max, nf_max, od_max;
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	/*
176*4882a593Smuzhiyun 	 * Make sure PLL is working with valid input signal (Fdiv). If
177*4882a593Smuzhiyun 	 * you want to speed the function up just reduce CCU_PLL_NR_MAX.
178*4882a593Smuzhiyun 	 * This will cause a worse approximation though.
179*4882a593Smuzhiyun 	 */
180*4882a593Smuzhiyun 	nri = (parent_rate / CCU_PLL_FDIV_MAX) + 1;
181*4882a593Smuzhiyun 	nr_max = min(parent_rate / CCU_PLL_FDIV_MIN, CCU_PLL_NR_MAX);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	/*
184*4882a593Smuzhiyun 	 * Find a closest [nr;nf;od] vector taking into account the
185*4882a593Smuzhiyun 	 * limitations like: 1) 700MHz <= Fvco <= 3.5GHz, 2) PLL Od is
186*4882a593Smuzhiyun 	 * either 1 or even number within the acceptable range (alas 1s
187*4882a593Smuzhiyun 	 * is also excluded by the next loop).
188*4882a593Smuzhiyun 	 */
189*4882a593Smuzhiyun 	for (; nri <= nr_max; ++nri) {
190*4882a593Smuzhiyun 		/* Use Od factor to fulfill the limitation 2). */
191*4882a593Smuzhiyun 		num = CCU_PLL_CLKOD_FACTOR * rate;
192*4882a593Smuzhiyun 		denom = parent_rate / nri;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 		/*
195*4882a593Smuzhiyun 		 * Make sure Fvco is within the acceptable range to fulfill
196*4882a593Smuzhiyun 		 * the condition 1). Note due to the CCU_PLL_CLKOD_FACTOR value
197*4882a593Smuzhiyun 		 * the actual upper limit is also divided by that factor.
198*4882a593Smuzhiyun 		 * It's not big problem for us since practically there is no
199*4882a593Smuzhiyun 		 * need in clocks with that high frequency.
200*4882a593Smuzhiyun 		 */
201*4882a593Smuzhiyun 		nf_max = min(CCU_PLL_FVCO_MAX / denom, CCU_PLL_NF_MAX);
202*4882a593Smuzhiyun 		od_max = CCU_PLL_OD_MAX / CCU_PLL_CLKOD_FACTOR;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 		/*
205*4882a593Smuzhiyun 		 * Bypass the out-of-bound values, which can't be properly
206*4882a593Smuzhiyun 		 * handled by the rational fraction approximation algorithm.
207*4882a593Smuzhiyun 		 */
208*4882a593Smuzhiyun 		if (num / denom >= nf_max) {
209*4882a593Smuzhiyun 			n1 = nf_max;
210*4882a593Smuzhiyun 			d1 = 1;
211*4882a593Smuzhiyun 		} else if (denom / num >= od_max) {
212*4882a593Smuzhiyun 			n1 = 1;
213*4882a593Smuzhiyun 			d1 = od_max;
214*4882a593Smuzhiyun 		} else {
215*4882a593Smuzhiyun 			rational_best_approximation(num, denom, nf_max, od_max,
216*4882a593Smuzhiyun 						    &n1, &d1);
217*4882a593Smuzhiyun 		}
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 		/* Select the best approximation of the target rate. */
220*4882a593Smuzhiyun 		freq = ccu_pll_calc_freq(parent_rate, nri, n1, d1);
221*4882a593Smuzhiyun 		err = abs((int64_t)freq - num);
222*4882a593Smuzhiyun 		if (err < min_err) {
223*4882a593Smuzhiyun 			min_err = err;
224*4882a593Smuzhiyun 			*nr = nri;
225*4882a593Smuzhiyun 			*nf = n1;
226*4882a593Smuzhiyun 			*od = CCU_PLL_CLKOD_FACTOR * d1;
227*4882a593Smuzhiyun 		}
228*4882a593Smuzhiyun 	}
229*4882a593Smuzhiyun }
230*4882a593Smuzhiyun 
ccu_pll_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)231*4882a593Smuzhiyun static long ccu_pll_round_rate(struct clk_hw *hw, unsigned long rate,
232*4882a593Smuzhiyun 			       unsigned long *parent_rate)
233*4882a593Smuzhiyun {
234*4882a593Smuzhiyun 	unsigned long nr = 1, nf = 1, od = 1;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun 	ccu_pll_calc_factors(rate, *parent_rate, &nr, &nf, &od);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	return ccu_pll_calc_freq(*parent_rate, nr, nf, od);
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun  * This method is used for PLLs, which support the on-the-fly dividers
243*4882a593Smuzhiyun  * adjustment. So there is no need in gating such clocks.
244*4882a593Smuzhiyun  */
ccu_pll_set_rate_reset(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)245*4882a593Smuzhiyun static int ccu_pll_set_rate_reset(struct clk_hw *hw, unsigned long rate,
246*4882a593Smuzhiyun 				  unsigned long parent_rate)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	struct ccu_pll *pll = to_ccu_pll(hw);
249*4882a593Smuzhiyun 	unsigned long nr, nf, od;
250*4882a593Smuzhiyun 	unsigned long flags;
251*4882a593Smuzhiyun 	u32 mask, val;
252*4882a593Smuzhiyun 	int ret;
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	ccu_pll_calc_factors(rate, parent_rate, &nr, &nf, &od);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	mask = CCU_PLL_CTL_CLKR_MASK | CCU_PLL_CTL_CLKF_MASK |
257*4882a593Smuzhiyun 	       CCU_PLL_CTL_CLKOD_MASK;
258*4882a593Smuzhiyun 	val = FIELD_PREP(CCU_PLL_CTL_CLKR_MASK, nr - 1) |
259*4882a593Smuzhiyun 	      FIELD_PREP(CCU_PLL_CTL_CLKF_MASK, nf - 1) |
260*4882a593Smuzhiyun 	      FIELD_PREP(CCU_PLL_CTL_CLKOD_MASK, od - 1);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	spin_lock_irqsave(&pll->lock, flags);
263*4882a593Smuzhiyun 	regmap_update_bits(pll->sys_regs, pll->reg_ctl, mask, val);
264*4882a593Smuzhiyun 	ret = ccu_pll_reset(pll, parent_rate, nr);
265*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pll->lock, flags);
266*4882a593Smuzhiyun 	if (ret)
267*4882a593Smuzhiyun 		pr_err("PLL '%s' reset timed out\n", clk_hw_get_name(hw));
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	return ret;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun /*
273*4882a593Smuzhiyun  * This method is used for PLLs, which don't support the on-the-fly dividers
274*4882a593Smuzhiyun  * adjustment. So the corresponding clocks are supposed to be gated first.
275*4882a593Smuzhiyun  */
ccu_pll_set_rate_norst(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)276*4882a593Smuzhiyun static int ccu_pll_set_rate_norst(struct clk_hw *hw, unsigned long rate,
277*4882a593Smuzhiyun 				  unsigned long parent_rate)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	struct ccu_pll *pll = to_ccu_pll(hw);
280*4882a593Smuzhiyun 	unsigned long nr, nf, od;
281*4882a593Smuzhiyun 	unsigned long flags;
282*4882a593Smuzhiyun 	u32 mask, val;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	ccu_pll_calc_factors(rate, parent_rate, &nr, &nf, &od);
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	/*
287*4882a593Smuzhiyun 	 * Disable PLL if it was enabled by default or left enabled by the
288*4882a593Smuzhiyun 	 * system bootloader.
289*4882a593Smuzhiyun 	 */
290*4882a593Smuzhiyun 	mask = CCU_PLL_CTL_CLKR_MASK | CCU_PLL_CTL_CLKF_MASK |
291*4882a593Smuzhiyun 	       CCU_PLL_CTL_CLKOD_MASK | CCU_PLL_CTL_EN;
292*4882a593Smuzhiyun 	val = FIELD_PREP(CCU_PLL_CTL_CLKR_MASK, nr - 1) |
293*4882a593Smuzhiyun 	      FIELD_PREP(CCU_PLL_CTL_CLKF_MASK, nf - 1) |
294*4882a593Smuzhiyun 	      FIELD_PREP(CCU_PLL_CTL_CLKOD_MASK, od - 1);
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	spin_lock_irqsave(&pll->lock, flags);
297*4882a593Smuzhiyun 	regmap_update_bits(pll->sys_regs, pll->reg_ctl, mask, val);
298*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pll->lock, flags);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	return 0;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_FS
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun struct ccu_pll_dbgfs_bit {
306*4882a593Smuzhiyun 	struct ccu_pll *pll;
307*4882a593Smuzhiyun 	const char *name;
308*4882a593Smuzhiyun 	unsigned int reg;
309*4882a593Smuzhiyun 	u32 mask;
310*4882a593Smuzhiyun };
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun struct ccu_pll_dbgfs_fld {
313*4882a593Smuzhiyun 	struct ccu_pll *pll;
314*4882a593Smuzhiyun 	const char *name;
315*4882a593Smuzhiyun 	unsigned int reg;
316*4882a593Smuzhiyun 	unsigned int lsb;
317*4882a593Smuzhiyun 	u32 mask;
318*4882a593Smuzhiyun 	u32 min;
319*4882a593Smuzhiyun 	u32 max;
320*4882a593Smuzhiyun };
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun #define CCU_PLL_DBGFS_BIT_ATTR(_name, _reg, _mask)	\
323*4882a593Smuzhiyun 	{						\
324*4882a593Smuzhiyun 		.name = _name,				\
325*4882a593Smuzhiyun 		.reg = _reg,				\
326*4882a593Smuzhiyun 		.mask = _mask				\
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun #define CCU_PLL_DBGFS_FLD_ATTR(_name, _reg, _lsb, _mask, _min, _max)	\
330*4882a593Smuzhiyun 	{								\
331*4882a593Smuzhiyun 		.name = _name,						\
332*4882a593Smuzhiyun 		.reg = _reg,						\
333*4882a593Smuzhiyun 		.lsb = _lsb,						\
334*4882a593Smuzhiyun 		.mask = _mask,						\
335*4882a593Smuzhiyun 		.min = _min,						\
336*4882a593Smuzhiyun 		.max = _max						\
337*4882a593Smuzhiyun 	}
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun static const struct ccu_pll_dbgfs_bit ccu_pll_bits[] = {
340*4882a593Smuzhiyun 	CCU_PLL_DBGFS_BIT_ATTR("pll_en", CCU_PLL_CTL, CCU_PLL_CTL_EN),
341*4882a593Smuzhiyun 	CCU_PLL_DBGFS_BIT_ATTR("pll_rst", CCU_PLL_CTL, CCU_PLL_CTL_RST),
342*4882a593Smuzhiyun 	CCU_PLL_DBGFS_BIT_ATTR("pll_bypass", CCU_PLL_CTL, CCU_PLL_CTL_BYPASS),
343*4882a593Smuzhiyun 	CCU_PLL_DBGFS_BIT_ATTR("pll_lock", CCU_PLL_CTL, CCU_PLL_CTL_LOCK)
344*4882a593Smuzhiyun };
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun #define CCU_PLL_DBGFS_BIT_NUM	ARRAY_SIZE(ccu_pll_bits)
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun static const struct ccu_pll_dbgfs_fld ccu_pll_flds[] = {
349*4882a593Smuzhiyun 	CCU_PLL_DBGFS_FLD_ATTR("pll_nr", CCU_PLL_CTL, CCU_PLL_CTL_CLKR_FLD,
350*4882a593Smuzhiyun 				CCU_PLL_CTL_CLKR_MASK, 1, CCU_PLL_NR_MAX),
351*4882a593Smuzhiyun 	CCU_PLL_DBGFS_FLD_ATTR("pll_nf", CCU_PLL_CTL, CCU_PLL_CTL_CLKF_FLD,
352*4882a593Smuzhiyun 				CCU_PLL_CTL_CLKF_MASK, 1, CCU_PLL_NF_MAX),
353*4882a593Smuzhiyun 	CCU_PLL_DBGFS_FLD_ATTR("pll_od", CCU_PLL_CTL, CCU_PLL_CTL_CLKOD_FLD,
354*4882a593Smuzhiyun 				CCU_PLL_CTL_CLKOD_MASK, 1, CCU_PLL_OD_MAX),
355*4882a593Smuzhiyun 	CCU_PLL_DBGFS_FLD_ATTR("pll_nb", CCU_PLL_CTL1, CCU_PLL_CTL1_BWADJ_FLD,
356*4882a593Smuzhiyun 				CCU_PLL_CTL1_BWADJ_MASK, 1, CCU_PLL_NB_MAX)
357*4882a593Smuzhiyun };
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun #define CCU_PLL_DBGFS_FLD_NUM	ARRAY_SIZE(ccu_pll_flds)
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun /*
362*4882a593Smuzhiyun  * It can be dangerous to change the PLL settings behind clock framework back,
363*4882a593Smuzhiyun  * therefore we don't provide any kernel config based compile time option for
364*4882a593Smuzhiyun  * this feature to enable.
365*4882a593Smuzhiyun  */
366*4882a593Smuzhiyun #undef CCU_PLL_ALLOW_WRITE_DEBUGFS
367*4882a593Smuzhiyun #ifdef CCU_PLL_ALLOW_WRITE_DEBUGFS
368*4882a593Smuzhiyun 
ccu_pll_dbgfs_bit_set(void * priv,u64 val)369*4882a593Smuzhiyun static int ccu_pll_dbgfs_bit_set(void *priv, u64 val)
370*4882a593Smuzhiyun {
371*4882a593Smuzhiyun 	const struct ccu_pll_dbgfs_bit *bit = priv;
372*4882a593Smuzhiyun 	struct ccu_pll *pll = bit->pll;
373*4882a593Smuzhiyun 	unsigned long flags;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	spin_lock_irqsave(&pll->lock, flags);
376*4882a593Smuzhiyun 	regmap_update_bits(pll->sys_regs, pll->reg_ctl + bit->reg,
377*4882a593Smuzhiyun 			   bit->mask, val ? bit->mask : 0);
378*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pll->lock, flags);
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	return 0;
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun 
ccu_pll_dbgfs_fld_set(void * priv,u64 val)383*4882a593Smuzhiyun static int ccu_pll_dbgfs_fld_set(void *priv, u64 val)
384*4882a593Smuzhiyun {
385*4882a593Smuzhiyun 	struct ccu_pll_dbgfs_fld *fld = priv;
386*4882a593Smuzhiyun 	struct ccu_pll *pll = fld->pll;
387*4882a593Smuzhiyun 	unsigned long flags;
388*4882a593Smuzhiyun 	u32 data;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	val = clamp_t(u64, val, fld->min, fld->max);
391*4882a593Smuzhiyun 	data = ((val - 1) << fld->lsb) & fld->mask;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	spin_lock_irqsave(&pll->lock, flags);
394*4882a593Smuzhiyun 	regmap_update_bits(pll->sys_regs, pll->reg_ctl + fld->reg, fld->mask,
395*4882a593Smuzhiyun 			   data);
396*4882a593Smuzhiyun 	spin_unlock_irqrestore(&pll->lock, flags);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	return 0;
399*4882a593Smuzhiyun }
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun #define ccu_pll_dbgfs_mode	0644
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun #else /* !CCU_PLL_ALLOW_WRITE_DEBUGFS */
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun #define ccu_pll_dbgfs_bit_set	NULL
406*4882a593Smuzhiyun #define ccu_pll_dbgfs_fld_set	NULL
407*4882a593Smuzhiyun #define ccu_pll_dbgfs_mode	0444
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun #endif /* !CCU_PLL_ALLOW_WRITE_DEBUGFS */
410*4882a593Smuzhiyun 
ccu_pll_dbgfs_bit_get(void * priv,u64 * val)411*4882a593Smuzhiyun static int ccu_pll_dbgfs_bit_get(void *priv, u64 *val)
412*4882a593Smuzhiyun {
413*4882a593Smuzhiyun 	struct ccu_pll_dbgfs_bit *bit = priv;
414*4882a593Smuzhiyun 	struct ccu_pll *pll = bit->pll;
415*4882a593Smuzhiyun 	u32 data = 0;
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	regmap_read(pll->sys_regs, pll->reg_ctl + bit->reg, &data);
418*4882a593Smuzhiyun 	*val = !!(data & bit->mask);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	return 0;
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(ccu_pll_dbgfs_bit_fops,
423*4882a593Smuzhiyun 	ccu_pll_dbgfs_bit_get, ccu_pll_dbgfs_bit_set, "%llu\n");
424*4882a593Smuzhiyun 
ccu_pll_dbgfs_fld_get(void * priv,u64 * val)425*4882a593Smuzhiyun static int ccu_pll_dbgfs_fld_get(void *priv, u64 *val)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct ccu_pll_dbgfs_fld *fld = priv;
428*4882a593Smuzhiyun 	struct ccu_pll *pll = fld->pll;
429*4882a593Smuzhiyun 	u32 data = 0;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	regmap_read(pll->sys_regs, pll->reg_ctl + fld->reg, &data);
432*4882a593Smuzhiyun 	*val = ((data & fld->mask) >> fld->lsb) + 1;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	return 0;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun DEFINE_DEBUGFS_ATTRIBUTE(ccu_pll_dbgfs_fld_fops,
437*4882a593Smuzhiyun 	ccu_pll_dbgfs_fld_get, ccu_pll_dbgfs_fld_set, "%llu\n");
438*4882a593Smuzhiyun 
ccu_pll_debug_init(struct clk_hw * hw,struct dentry * dentry)439*4882a593Smuzhiyun static void ccu_pll_debug_init(struct clk_hw *hw, struct dentry *dentry)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	struct ccu_pll *pll = to_ccu_pll(hw);
442*4882a593Smuzhiyun 	struct ccu_pll_dbgfs_bit *bits;
443*4882a593Smuzhiyun 	struct ccu_pll_dbgfs_fld *flds;
444*4882a593Smuzhiyun 	int idx;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	bits = kcalloc(CCU_PLL_DBGFS_BIT_NUM, sizeof(*bits), GFP_KERNEL);
447*4882a593Smuzhiyun 	if (!bits)
448*4882a593Smuzhiyun 		return;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	for (idx = 0; idx < CCU_PLL_DBGFS_BIT_NUM; ++idx) {
451*4882a593Smuzhiyun 		bits[idx] = ccu_pll_bits[idx];
452*4882a593Smuzhiyun 		bits[idx].pll = pll;
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 		debugfs_create_file_unsafe(bits[idx].name, ccu_pll_dbgfs_mode,
455*4882a593Smuzhiyun 					   dentry, &bits[idx],
456*4882a593Smuzhiyun 					   &ccu_pll_dbgfs_bit_fops);
457*4882a593Smuzhiyun 	}
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	flds = kcalloc(CCU_PLL_DBGFS_FLD_NUM, sizeof(*flds), GFP_KERNEL);
460*4882a593Smuzhiyun 	if (!flds)
461*4882a593Smuzhiyun 		return;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	for (idx = 0; idx < CCU_PLL_DBGFS_FLD_NUM; ++idx) {
464*4882a593Smuzhiyun 		flds[idx] = ccu_pll_flds[idx];
465*4882a593Smuzhiyun 		flds[idx].pll = pll;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 		debugfs_create_file_unsafe(flds[idx].name, ccu_pll_dbgfs_mode,
468*4882a593Smuzhiyun 					   dentry, &flds[idx],
469*4882a593Smuzhiyun 					   &ccu_pll_dbgfs_fld_fops);
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun #else /* !CONFIG_DEBUG_FS */
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun #define ccu_pll_debug_init NULL
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun #endif /* !CONFIG_DEBUG_FS */
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun static const struct clk_ops ccu_pll_gate_to_set_ops = {
480*4882a593Smuzhiyun 	.enable = ccu_pll_enable,
481*4882a593Smuzhiyun 	.disable = ccu_pll_disable,
482*4882a593Smuzhiyun 	.is_enabled = ccu_pll_is_enabled,
483*4882a593Smuzhiyun 	.recalc_rate = ccu_pll_recalc_rate,
484*4882a593Smuzhiyun 	.round_rate = ccu_pll_round_rate,
485*4882a593Smuzhiyun 	.set_rate = ccu_pll_set_rate_norst,
486*4882a593Smuzhiyun 	.debug_init = ccu_pll_debug_init
487*4882a593Smuzhiyun };
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun static const struct clk_ops ccu_pll_straight_set_ops = {
490*4882a593Smuzhiyun 	.enable = ccu_pll_enable,
491*4882a593Smuzhiyun 	.disable = ccu_pll_disable,
492*4882a593Smuzhiyun 	.is_enabled = ccu_pll_is_enabled,
493*4882a593Smuzhiyun 	.recalc_rate = ccu_pll_recalc_rate,
494*4882a593Smuzhiyun 	.round_rate = ccu_pll_round_rate,
495*4882a593Smuzhiyun 	.set_rate = ccu_pll_set_rate_reset,
496*4882a593Smuzhiyun 	.debug_init = ccu_pll_debug_init
497*4882a593Smuzhiyun };
498*4882a593Smuzhiyun 
ccu_pll_hw_register(const struct ccu_pll_init_data * pll_init)499*4882a593Smuzhiyun struct ccu_pll *ccu_pll_hw_register(const struct ccu_pll_init_data *pll_init)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun 	struct clk_parent_data parent_data = { };
502*4882a593Smuzhiyun 	struct clk_init_data hw_init = { };
503*4882a593Smuzhiyun 	struct ccu_pll *pll;
504*4882a593Smuzhiyun 	int ret;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	if (!pll_init)
507*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
510*4882a593Smuzhiyun 	if (!pll)
511*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	/*
514*4882a593Smuzhiyun 	 * Note since Baikal-T1 System Controller registers are MMIO-backed
515*4882a593Smuzhiyun 	 * we won't check the regmap IO operations return status, because it
516*4882a593Smuzhiyun 	 * must be zero anyway.
517*4882a593Smuzhiyun 	 */
518*4882a593Smuzhiyun 	pll->hw.init = &hw_init;
519*4882a593Smuzhiyun 	pll->reg_ctl = pll_init->base + CCU_PLL_CTL;
520*4882a593Smuzhiyun 	pll->reg_ctl1 = pll_init->base + CCU_PLL_CTL1;
521*4882a593Smuzhiyun 	pll->sys_regs = pll_init->sys_regs;
522*4882a593Smuzhiyun 	pll->id = pll_init->id;
523*4882a593Smuzhiyun 	spin_lock_init(&pll->lock);
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 	hw_init.name = pll_init->name;
526*4882a593Smuzhiyun 	hw_init.flags = pll_init->flags;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	if (hw_init.flags & CLK_SET_RATE_GATE)
529*4882a593Smuzhiyun 		hw_init.ops = &ccu_pll_gate_to_set_ops;
530*4882a593Smuzhiyun 	else
531*4882a593Smuzhiyun 		hw_init.ops = &ccu_pll_straight_set_ops;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	if (!pll_init->parent_name) {
534*4882a593Smuzhiyun 		ret = -EINVAL;
535*4882a593Smuzhiyun 		goto err_free_pll;
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 	parent_data.fw_name = pll_init->parent_name;
538*4882a593Smuzhiyun 	hw_init.parent_data = &parent_data;
539*4882a593Smuzhiyun 	hw_init.num_parents = 1;
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun 	ret = of_clk_hw_register(pll_init->np, &pll->hw);
542*4882a593Smuzhiyun 	if (ret)
543*4882a593Smuzhiyun 		goto err_free_pll;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	return pll;
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun err_free_pll:
548*4882a593Smuzhiyun 	kfree(pll);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	return ERR_PTR(ret);
551*4882a593Smuzhiyun }
552*4882a593Smuzhiyun 
ccu_pll_hw_unregister(struct ccu_pll * pll)553*4882a593Smuzhiyun void ccu_pll_hw_unregister(struct ccu_pll *pll)
554*4882a593Smuzhiyun {
555*4882a593Smuzhiyun 	clk_hw_unregister(&pll->hw);
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun 	kfree(pll);
558*4882a593Smuzhiyun }
559