xref: /OK3568_Linux_fs/kernel/drivers/clk/imx/clk-frac-pll.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2018 NXP.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This driver supports the fractional plls found in the imx8m SOCs
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Documentation for this fractional pll can be found at:
8*4882a593Smuzhiyun  *   https://www.nxp.com/docs/en/reference-manual/IMX8MDQLQRM.pdf#page=834
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/clk-provider.h>
12*4882a593Smuzhiyun #include <linux/err.h>
13*4882a593Smuzhiyun #include <linux/export.h>
14*4882a593Smuzhiyun #include <linux/io.h>
15*4882a593Smuzhiyun #include <linux/iopoll.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <linux/bitfield.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #include "clk.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun #define PLL_CFG0		0x0
22*4882a593Smuzhiyun #define PLL_CFG1		0x4
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define PLL_LOCK_STATUS		BIT(31)
25*4882a593Smuzhiyun #define PLL_PD_MASK		BIT(19)
26*4882a593Smuzhiyun #define PLL_BYPASS_MASK		BIT(14)
27*4882a593Smuzhiyun #define PLL_NEWDIV_VAL		BIT(12)
28*4882a593Smuzhiyun #define PLL_NEWDIV_ACK		BIT(11)
29*4882a593Smuzhiyun #define PLL_FRAC_DIV_MASK	GENMASK(30, 7)
30*4882a593Smuzhiyun #define PLL_INT_DIV_MASK	GENMASK(6, 0)
31*4882a593Smuzhiyun #define PLL_OUTPUT_DIV_MASK	GENMASK(4, 0)
32*4882a593Smuzhiyun #define PLL_FRAC_DENOM		0x1000000
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #define PLL_FRAC_LOCK_TIMEOUT	10000
35*4882a593Smuzhiyun #define PLL_FRAC_ACK_TIMEOUT	500000
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun struct clk_frac_pll {
38*4882a593Smuzhiyun 	struct clk_hw	hw;
39*4882a593Smuzhiyun 	void __iomem	*base;
40*4882a593Smuzhiyun };
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun #define to_clk_frac_pll(_hw) container_of(_hw, struct clk_frac_pll, hw)
43*4882a593Smuzhiyun 
clk_wait_lock(struct clk_frac_pll * pll)44*4882a593Smuzhiyun static int clk_wait_lock(struct clk_frac_pll *pll)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	u32 val;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	return readl_poll_timeout(pll->base, val, val & PLL_LOCK_STATUS, 0,
49*4882a593Smuzhiyun 					PLL_FRAC_LOCK_TIMEOUT);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
clk_wait_ack(struct clk_frac_pll * pll)52*4882a593Smuzhiyun static int clk_wait_ack(struct clk_frac_pll *pll)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	u32 val;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	/* return directly if the pll is in powerdown or in bypass */
57*4882a593Smuzhiyun 	if (readl_relaxed(pll->base) & (PLL_PD_MASK | PLL_BYPASS_MASK))
58*4882a593Smuzhiyun 		return 0;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	/* Wait for the pll's divfi and divff to be reloaded */
61*4882a593Smuzhiyun 	return readl_poll_timeout(pll->base, val, val & PLL_NEWDIV_ACK, 0,
62*4882a593Smuzhiyun 					PLL_FRAC_ACK_TIMEOUT);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
clk_pll_prepare(struct clk_hw * hw)65*4882a593Smuzhiyun static int clk_pll_prepare(struct clk_hw *hw)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	struct clk_frac_pll *pll = to_clk_frac_pll(hw);
68*4882a593Smuzhiyun 	u32 val;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG0);
71*4882a593Smuzhiyun 	val &= ~PLL_PD_MASK;
72*4882a593Smuzhiyun 	writel_relaxed(val, pll->base + PLL_CFG0);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	return clk_wait_lock(pll);
75*4882a593Smuzhiyun }
76*4882a593Smuzhiyun 
clk_pll_unprepare(struct clk_hw * hw)77*4882a593Smuzhiyun static void clk_pll_unprepare(struct clk_hw *hw)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun 	struct clk_frac_pll *pll = to_clk_frac_pll(hw);
80*4882a593Smuzhiyun 	u32 val;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG0);
83*4882a593Smuzhiyun 	val |= PLL_PD_MASK;
84*4882a593Smuzhiyun 	writel_relaxed(val, pll->base + PLL_CFG0);
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
clk_pll_is_prepared(struct clk_hw * hw)87*4882a593Smuzhiyun static int clk_pll_is_prepared(struct clk_hw *hw)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	struct clk_frac_pll *pll = to_clk_frac_pll(hw);
90*4882a593Smuzhiyun 	u32 val;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG0);
93*4882a593Smuzhiyun 	return (val & PLL_PD_MASK) ? 0 : 1;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
clk_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)96*4882a593Smuzhiyun static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
97*4882a593Smuzhiyun 					 unsigned long parent_rate)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct clk_frac_pll *pll = to_clk_frac_pll(hw);
100*4882a593Smuzhiyun 	u32 val, divff, divfi, divq;
101*4882a593Smuzhiyun 	u64 temp64 = parent_rate;
102*4882a593Smuzhiyun 	u64 rate;
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG0);
105*4882a593Smuzhiyun 	divq = (FIELD_GET(PLL_OUTPUT_DIV_MASK, val) + 1) * 2;
106*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG1);
107*4882a593Smuzhiyun 	divff = FIELD_GET(PLL_FRAC_DIV_MASK, val);
108*4882a593Smuzhiyun 	divfi = FIELD_GET(PLL_INT_DIV_MASK, val);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	temp64 *= 8;
111*4882a593Smuzhiyun 	temp64 *= divff;
112*4882a593Smuzhiyun 	do_div(temp64, PLL_FRAC_DENOM);
113*4882a593Smuzhiyun 	do_div(temp64, divq);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	rate = parent_rate * 8 * (divfi + 1);
116*4882a593Smuzhiyun 	do_div(rate, divq);
117*4882a593Smuzhiyun 	rate += temp64;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	return rate;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
clk_pll_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)122*4882a593Smuzhiyun static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
123*4882a593Smuzhiyun 			       unsigned long *prate)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	u64 parent_rate = *prate;
126*4882a593Smuzhiyun 	u32 divff, divfi;
127*4882a593Smuzhiyun 	u64 temp64;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	parent_rate *= 8;
130*4882a593Smuzhiyun 	rate *= 2;
131*4882a593Smuzhiyun 	temp64 = rate;
132*4882a593Smuzhiyun 	do_div(temp64, parent_rate);
133*4882a593Smuzhiyun 	divfi = temp64;
134*4882a593Smuzhiyun 	temp64 = rate - divfi * parent_rate;
135*4882a593Smuzhiyun 	temp64 *= PLL_FRAC_DENOM;
136*4882a593Smuzhiyun 	do_div(temp64, parent_rate);
137*4882a593Smuzhiyun 	divff = temp64;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	temp64 = parent_rate;
140*4882a593Smuzhiyun 	temp64 *= divff;
141*4882a593Smuzhiyun 	do_div(temp64, PLL_FRAC_DENOM);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 	rate = parent_rate * divfi + temp64;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	return rate / 2;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun /*
149*4882a593Smuzhiyun  * To simplify the clock calculation, we can keep the 'PLL_OUTPUT_VAL' at zero
150*4882a593Smuzhiyun  * (means the PLL output will be divided by 2). So the PLL output can use
151*4882a593Smuzhiyun  * the below formula:
152*4882a593Smuzhiyun  * pllout = parent_rate * 8 / 2 * DIVF_VAL;
153*4882a593Smuzhiyun  * where DIVF_VAL = 1 + DIVFI + DIVFF / 2^24.
154*4882a593Smuzhiyun  */
clk_pll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)155*4882a593Smuzhiyun static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
156*4882a593Smuzhiyun 			    unsigned long parent_rate)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	struct clk_frac_pll *pll = to_clk_frac_pll(hw);
159*4882a593Smuzhiyun 	u32 val, divfi, divff;
160*4882a593Smuzhiyun 	u64 temp64;
161*4882a593Smuzhiyun 	int ret;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	parent_rate *= 8;
164*4882a593Smuzhiyun 	rate *= 2;
165*4882a593Smuzhiyun 	divfi = rate / parent_rate;
166*4882a593Smuzhiyun 	temp64 = parent_rate * divfi;
167*4882a593Smuzhiyun 	temp64 = rate - temp64;
168*4882a593Smuzhiyun 	temp64 *= PLL_FRAC_DENOM;
169*4882a593Smuzhiyun 	do_div(temp64, parent_rate);
170*4882a593Smuzhiyun 	divff = temp64;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG1);
173*4882a593Smuzhiyun 	val &= ~(PLL_FRAC_DIV_MASK | PLL_INT_DIV_MASK);
174*4882a593Smuzhiyun 	val |= (divff << 7) | (divfi - 1);
175*4882a593Smuzhiyun 	writel_relaxed(val, pll->base + PLL_CFG1);
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG0);
178*4882a593Smuzhiyun 	val &= ~0x1f;
179*4882a593Smuzhiyun 	writel_relaxed(val, pll->base + PLL_CFG0);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* Set the NEV_DIV_VAL to reload the DIVFI and DIVFF */
182*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG0);
183*4882a593Smuzhiyun 	val |= PLL_NEWDIV_VAL;
184*4882a593Smuzhiyun 	writel_relaxed(val, pll->base + PLL_CFG0);
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	ret = clk_wait_ack(pll);
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	/* clear the NEV_DIV_VAL */
189*4882a593Smuzhiyun 	val = readl_relaxed(pll->base + PLL_CFG0);
190*4882a593Smuzhiyun 	val &= ~PLL_NEWDIV_VAL;
191*4882a593Smuzhiyun 	writel_relaxed(val, pll->base + PLL_CFG0);
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	return ret;
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun static const struct clk_ops clk_frac_pll_ops = {
197*4882a593Smuzhiyun 	.prepare	= clk_pll_prepare,
198*4882a593Smuzhiyun 	.unprepare	= clk_pll_unprepare,
199*4882a593Smuzhiyun 	.is_prepared	= clk_pll_is_prepared,
200*4882a593Smuzhiyun 	.recalc_rate	= clk_pll_recalc_rate,
201*4882a593Smuzhiyun 	.round_rate	= clk_pll_round_rate,
202*4882a593Smuzhiyun 	.set_rate	= clk_pll_set_rate,
203*4882a593Smuzhiyun };
204*4882a593Smuzhiyun 
imx_clk_hw_frac_pll(const char * name,const char * parent_name,void __iomem * base)205*4882a593Smuzhiyun struct clk_hw *imx_clk_hw_frac_pll(const char *name,
206*4882a593Smuzhiyun 				   const char *parent_name,
207*4882a593Smuzhiyun 				   void __iomem *base)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	struct clk_init_data init;
210*4882a593Smuzhiyun 	struct clk_frac_pll *pll;
211*4882a593Smuzhiyun 	struct clk_hw *hw;
212*4882a593Smuzhiyun 	int ret;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
215*4882a593Smuzhiyun 	if (!pll)
216*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	init.name = name;
219*4882a593Smuzhiyun 	init.ops = &clk_frac_pll_ops;
220*4882a593Smuzhiyun 	init.flags = 0;
221*4882a593Smuzhiyun 	init.parent_names = &parent_name;
222*4882a593Smuzhiyun 	init.num_parents = 1;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	pll->base = base;
225*4882a593Smuzhiyun 	pll->hw.init = &init;
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun 	hw = &pll->hw;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	ret = clk_hw_register(NULL, hw);
230*4882a593Smuzhiyun 	if (ret) {
231*4882a593Smuzhiyun 		kfree(pll);
232*4882a593Smuzhiyun 		return ERR_PTR(ret);
233*4882a593Smuzhiyun 	}
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	return hw;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(imx_clk_hw_frac_pll);
238