xref: /openbmc/linux/drivers/clk/imx/clk-pll14xx.c (revision b09c68dc57c9d44071d83bb935b733f53ea2b2b4)
18646d4dcSBai Ping // SPDX-License-Identifier: GPL-2.0
28646d4dcSBai Ping /*
38646d4dcSBai Ping  * Copyright 2017-2018 NXP.
48646d4dcSBai Ping  */
58646d4dcSBai Ping 
680cbc806SSascha Hauer #define pr_fmt(fmt) "pll14xx: " fmt
780cbc806SSascha Hauer 
858f4980cSSascha Hauer #include <linux/bitfield.h>
97d6b5e4fSAnson Huang #include <linux/bits.h>
108646d4dcSBai Ping #include <linux/clk-provider.h>
118646d4dcSBai Ping #include <linux/err.h>
12870ed5e2SAnson Huang #include <linux/export.h>
138646d4dcSBai Ping #include <linux/io.h>
148646d4dcSBai Ping #include <linux/iopoll.h>
158646d4dcSBai Ping #include <linux/slab.h>
168646d4dcSBai Ping #include <linux/jiffies.h>
178646d4dcSBai Ping 
188646d4dcSBai Ping #include "clk.h"
198646d4dcSBai Ping 
208646d4dcSBai Ping #define GNRL_CTL	0x0
21485b4ff5SSascha Hauer #define DIV_CTL0	0x4
22485b4ff5SSascha Hauer #define DIV_CTL1	0x8
238646d4dcSBai Ping #define LOCK_STATUS	BIT(31)
248646d4dcSBai Ping #define LOCK_SEL_MASK	BIT(29)
258646d4dcSBai Ping #define CLKE_MASK	BIT(11)
268646d4dcSBai Ping #define RST_MASK	BIT(9)
278646d4dcSBai Ping #define BYPASS_MASK	BIT(4)
288646d4dcSBai Ping #define MDIV_MASK	GENMASK(21, 12)
298646d4dcSBai Ping #define PDIV_MASK	GENMASK(9, 4)
308646d4dcSBai Ping #define SDIV_MASK	GENMASK(2, 0)
318646d4dcSBai Ping #define KDIV_MASK	GENMASK(15, 0)
32*b09c68dcSSascha Hauer #define KDIV_MIN	SHRT_MIN
33*b09c68dcSSascha Hauer #define KDIV_MAX	SHRT_MAX
348646d4dcSBai Ping 
358646d4dcSBai Ping #define LOCK_TIMEOUT_US		10000
368646d4dcSBai Ping 
378646d4dcSBai Ping struct clk_pll14xx {
388646d4dcSBai Ping 	struct clk_hw			hw;
398646d4dcSBai Ping 	void __iomem			*base;
408646d4dcSBai Ping 	enum imx_pll14xx_type		type;
418646d4dcSBai Ping 	const struct imx_pll14xx_rate_table *rate_table;
428646d4dcSBai Ping 	int rate_count;
438646d4dcSBai Ping };
448646d4dcSBai Ping 
458646d4dcSBai Ping #define to_clk_pll14xx(_hw) container_of(_hw, struct clk_pll14xx, hw)
468646d4dcSBai Ping 
478f2d3c17SYueHaibing static const struct imx_pll14xx_rate_table imx_pll1416x_tbl[] = {
4843cdaa15SAnson Huang 	PLL_1416X_RATE(1800000000U, 225, 3, 0),
4943cdaa15SAnson Huang 	PLL_1416X_RATE(1600000000U, 200, 3, 0),
500ae4fbc6SAnson Huang 	PLL_1416X_RATE(1500000000U, 375, 3, 1),
510ae4fbc6SAnson Huang 	PLL_1416X_RATE(1400000000U, 350, 3, 1),
5243cdaa15SAnson Huang 	PLL_1416X_RATE(1200000000U, 300, 3, 1),
5343cdaa15SAnson Huang 	PLL_1416X_RATE(1000000000U, 250, 3, 1),
5443cdaa15SAnson Huang 	PLL_1416X_RATE(800000000U,  200, 3, 1),
5543cdaa15SAnson Huang 	PLL_1416X_RATE(750000000U,  250, 2, 2),
5643cdaa15SAnson Huang 	PLL_1416X_RATE(700000000U,  350, 3, 2),
5743cdaa15SAnson Huang 	PLL_1416X_RATE(600000000U,  300, 3, 2),
5843cdaa15SAnson Huang };
5943cdaa15SAnson Huang 
608f2d3c17SYueHaibing static const struct imx_pll14xx_rate_table imx_pll1443x_tbl[] = {
6157795654SAnson Huang 	PLL_1443X_RATE(1039500000U, 173, 2, 1, 16384),
6243cdaa15SAnson Huang 	PLL_1443X_RATE(650000000U, 325, 3, 2, 0),
6343cdaa15SAnson Huang 	PLL_1443X_RATE(594000000U, 198, 2, 2, 0),
6457795654SAnson Huang 	PLL_1443X_RATE(519750000U, 173, 2, 2, 16384),
6543cdaa15SAnson Huang 	PLL_1443X_RATE(393216000U, 262, 2, 3, 9437),
6643cdaa15SAnson Huang 	PLL_1443X_RATE(361267200U, 361, 3, 3, 17511),
6743cdaa15SAnson Huang };
6843cdaa15SAnson Huang 
6943cdaa15SAnson Huang struct imx_pll14xx_clk imx_1443x_pll = {
7043cdaa15SAnson Huang 	.type = PLL_1443X,
7143cdaa15SAnson Huang 	.rate_table = imx_pll1443x_tbl,
7243cdaa15SAnson Huang 	.rate_count = ARRAY_SIZE(imx_pll1443x_tbl),
7343cdaa15SAnson Huang };
74870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_1443x_pll);
7543cdaa15SAnson Huang 
76e18f6471SLeonard Crestez struct imx_pll14xx_clk imx_1443x_dram_pll = {
77e18f6471SLeonard Crestez 	.type = PLL_1443X,
78e18f6471SLeonard Crestez 	.rate_table = imx_pll1443x_tbl,
79e18f6471SLeonard Crestez 	.rate_count = ARRAY_SIZE(imx_pll1443x_tbl),
80e18f6471SLeonard Crestez 	.flags = CLK_GET_RATE_NOCACHE,
81e18f6471SLeonard Crestez };
82870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_1443x_dram_pll);
83e18f6471SLeonard Crestez 
8443cdaa15SAnson Huang struct imx_pll14xx_clk imx_1416x_pll = {
8543cdaa15SAnson Huang 	.type = PLL_1416X,
8643cdaa15SAnson Huang 	.rate_table = imx_pll1416x_tbl,
8743cdaa15SAnson Huang 	.rate_count = ARRAY_SIZE(imx_pll1416x_tbl),
8843cdaa15SAnson Huang };
89870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_1416x_pll);
9043cdaa15SAnson Huang 
918646d4dcSBai Ping static const struct imx_pll14xx_rate_table *imx_get_pll_settings(
928646d4dcSBai Ping 		struct clk_pll14xx *pll, unsigned long rate)
938646d4dcSBai Ping {
948646d4dcSBai Ping 	const struct imx_pll14xx_rate_table *rate_table = pll->rate_table;
958646d4dcSBai Ping 	int i;
968646d4dcSBai Ping 
978646d4dcSBai Ping 	for (i = 0; i < pll->rate_count; i++)
988646d4dcSBai Ping 		if (rate == rate_table[i].rate)
998646d4dcSBai Ping 			return &rate_table[i];
1008646d4dcSBai Ping 
1018646d4dcSBai Ping 	return NULL;
1028646d4dcSBai Ping }
1038646d4dcSBai Ping 
10453990cf9SSascha Hauer static long pll14xx_calc_rate(struct clk_pll14xx *pll, int mdiv, int pdiv,
10553990cf9SSascha Hauer 			      int sdiv, int kdiv, unsigned long prate)
10653990cf9SSascha Hauer {
10753990cf9SSascha Hauer 	u64 fvco = prate;
10853990cf9SSascha Hauer 
10953990cf9SSascha Hauer 	/* fvco = (m * 65536 + k) * Fin / (p * 65536) */
11053990cf9SSascha Hauer 	fvco *= (mdiv * 65536 + kdiv);
11153990cf9SSascha Hauer 	pdiv *= 65536;
11253990cf9SSascha Hauer 
11353990cf9SSascha Hauer 	do_div(fvco, pdiv << sdiv);
11453990cf9SSascha Hauer 
11553990cf9SSascha Hauer 	return fvco;
11653990cf9SSascha Hauer }
11753990cf9SSascha Hauer 
118*b09c68dcSSascha Hauer static long pll1443x_calc_kdiv(int mdiv, int pdiv, int sdiv,
119*b09c68dcSSascha Hauer 		unsigned long rate, unsigned long prate)
120*b09c68dcSSascha Hauer {
121*b09c68dcSSascha Hauer 	long kdiv;
122*b09c68dcSSascha Hauer 
123*b09c68dcSSascha Hauer 	/* calc kdiv = round(rate * pdiv * 65536 * 2^sdiv / prate) - (mdiv * 65536) */
124*b09c68dcSSascha Hauer 	kdiv = ((rate * ((pdiv * 65536) << sdiv) + prate / 2) / prate) - (mdiv * 65536);
125*b09c68dcSSascha Hauer 
126*b09c68dcSSascha Hauer 	return clamp_t(short, kdiv, KDIV_MIN, KDIV_MAX);
127*b09c68dcSSascha Hauer }
128*b09c68dcSSascha Hauer 
129*b09c68dcSSascha Hauer static void imx_pll14xx_calc_settings(struct clk_pll14xx *pll, unsigned long rate,
130*b09c68dcSSascha Hauer 				      unsigned long prate, struct imx_pll14xx_rate_table *t)
131*b09c68dcSSascha Hauer {
132*b09c68dcSSascha Hauer 	u32 pll_div_ctl0, pll_div_ctl1;
133*b09c68dcSSascha Hauer 	int mdiv, pdiv, sdiv, kdiv;
134*b09c68dcSSascha Hauer 	long fvco, rate_min, rate_max, dist, best = LONG_MAX;
135*b09c68dcSSascha Hauer 	const struct imx_pll14xx_rate_table *tt;
136*b09c68dcSSascha Hauer 
137*b09c68dcSSascha Hauer 	/*
138*b09c68dcSSascha Hauer 	 * Fractional PLL constrains:
139*b09c68dcSSascha Hauer 	 *
140*b09c68dcSSascha Hauer 	 * a) 6MHz <= prate <= 25MHz
141*b09c68dcSSascha Hauer 	 * b) 1 <= p <= 63 (1 <= p <= 4 prate = 24MHz)
142*b09c68dcSSascha Hauer 	 * c) 64 <= m <= 1023
143*b09c68dcSSascha Hauer 	 * d) 0 <= s <= 6
144*b09c68dcSSascha Hauer 	 * e) -32768 <= k <= 32767
145*b09c68dcSSascha Hauer 	 *
146*b09c68dcSSascha Hauer 	 * fvco = (m * 65536 + k) * prate / (p * 65536)
147*b09c68dcSSascha Hauer 	 */
148*b09c68dcSSascha Hauer 
149*b09c68dcSSascha Hauer 	/* First try if we can get the desired rate from one of the static entries */
150*b09c68dcSSascha Hauer 	tt = imx_get_pll_settings(pll, rate);
151*b09c68dcSSascha Hauer 	if (tt) {
152*b09c68dcSSascha Hauer 		pr_debug("%s: in=%ld, want=%ld, Using PLL setting from table\n",
153*b09c68dcSSascha Hauer 			 clk_hw_get_name(&pll->hw), prate, rate);
154*b09c68dcSSascha Hauer 		t->rate = tt->rate;
155*b09c68dcSSascha Hauer 		t->mdiv = tt->mdiv;
156*b09c68dcSSascha Hauer 		t->pdiv = tt->pdiv;
157*b09c68dcSSascha Hauer 		t->sdiv = tt->sdiv;
158*b09c68dcSSascha Hauer 		t->kdiv = tt->kdiv;
159*b09c68dcSSascha Hauer 		return;
160*b09c68dcSSascha Hauer 	}
161*b09c68dcSSascha Hauer 
162*b09c68dcSSascha Hauer 	pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0);
163*b09c68dcSSascha Hauer 	mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0);
164*b09c68dcSSascha Hauer 	pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0);
165*b09c68dcSSascha Hauer 	sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0);
166*b09c68dcSSascha Hauer 	pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1);
167*b09c68dcSSascha Hauer 
168*b09c68dcSSascha Hauer 	/* Then see if we can get the desired rate by only adjusting kdiv (glitch free) */
169*b09c68dcSSascha Hauer 	rate_min = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MIN, prate);
170*b09c68dcSSascha Hauer 	rate_max = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MAX, prate);
171*b09c68dcSSascha Hauer 
172*b09c68dcSSascha Hauer 	if (rate >= rate_min && rate <= rate_max) {
173*b09c68dcSSascha Hauer 		kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate);
174*b09c68dcSSascha Hauer 		pr_debug("%s: in=%ld, want=%ld Only adjust kdiv %ld -> %d\n",
175*b09c68dcSSascha Hauer 			 clk_hw_get_name(&pll->hw), prate, rate,
176*b09c68dcSSascha Hauer 			 FIELD_GET(KDIV_MASK, pll_div_ctl1), kdiv);
177*b09c68dcSSascha Hauer 		fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate);
178*b09c68dcSSascha Hauer 		t->rate = (unsigned int)fvco;
179*b09c68dcSSascha Hauer 		t->mdiv = mdiv;
180*b09c68dcSSascha Hauer 		t->pdiv = pdiv;
181*b09c68dcSSascha Hauer 		t->sdiv = sdiv;
182*b09c68dcSSascha Hauer 		t->kdiv = kdiv;
183*b09c68dcSSascha Hauer 		return;
184*b09c68dcSSascha Hauer 	}
185*b09c68dcSSascha Hauer 
186*b09c68dcSSascha Hauer 	/* Finally calculate best values */
187*b09c68dcSSascha Hauer 	for (pdiv = 1; pdiv <= 7; pdiv++) {
188*b09c68dcSSascha Hauer 		for (sdiv = 0; sdiv <= 6; sdiv++) {
189*b09c68dcSSascha Hauer 			/* calc mdiv = round(rate * pdiv * 2^sdiv) / prate) */
190*b09c68dcSSascha Hauer 			mdiv = DIV_ROUND_CLOSEST(rate * (pdiv << sdiv), prate);
191*b09c68dcSSascha Hauer 			mdiv = clamp(mdiv, 64, 1023);
192*b09c68dcSSascha Hauer 
193*b09c68dcSSascha Hauer 			kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate);
194*b09c68dcSSascha Hauer 			fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate);
195*b09c68dcSSascha Hauer 
196*b09c68dcSSascha Hauer 			/* best match */
197*b09c68dcSSascha Hauer 			dist = abs((long)rate - (long)fvco);
198*b09c68dcSSascha Hauer 			if (dist < best) {
199*b09c68dcSSascha Hauer 				best = dist;
200*b09c68dcSSascha Hauer 				t->rate = (unsigned int)fvco;
201*b09c68dcSSascha Hauer 				t->mdiv = mdiv;
202*b09c68dcSSascha Hauer 				t->pdiv = pdiv;
203*b09c68dcSSascha Hauer 				t->sdiv = sdiv;
204*b09c68dcSSascha Hauer 				t->kdiv = kdiv;
205*b09c68dcSSascha Hauer 
206*b09c68dcSSascha Hauer 				if (!dist)
207*b09c68dcSSascha Hauer 					goto found;
208*b09c68dcSSascha Hauer 			}
209*b09c68dcSSascha Hauer 		}
210*b09c68dcSSascha Hauer 	}
211*b09c68dcSSascha Hauer found:
212*b09c68dcSSascha Hauer 	pr_debug("%s: in=%ld, want=%ld got=%d (pdiv=%d sdiv=%d mdiv=%d kdiv=%d)\n",
213*b09c68dcSSascha Hauer 		 clk_hw_get_name(&pll->hw), prate, rate, t->rate, t->pdiv, t->sdiv,
214*b09c68dcSSascha Hauer 		 t->mdiv, t->kdiv);
215*b09c68dcSSascha Hauer }
216*b09c68dcSSascha Hauer 
217*b09c68dcSSascha Hauer static long clk_pll1416x_round_rate(struct clk_hw *hw, unsigned long rate,
2188646d4dcSBai Ping 			unsigned long *prate)
2198646d4dcSBai Ping {
2208646d4dcSBai Ping 	struct clk_pll14xx *pll = to_clk_pll14xx(hw);
2218646d4dcSBai Ping 	const struct imx_pll14xx_rate_table *rate_table = pll->rate_table;
2228646d4dcSBai Ping 	int i;
2238646d4dcSBai Ping 
2245ff50031SSascha Hauer 	/* Assuming rate_table is in descending order */
2258646d4dcSBai Ping 	for (i = 0; i < pll->rate_count; i++)
2268646d4dcSBai Ping 		if (rate >= rate_table[i].rate)
2278646d4dcSBai Ping 			return rate_table[i].rate;
2288646d4dcSBai Ping 
2298646d4dcSBai Ping 	/* return minimum supported value */
2305ff50031SSascha Hauer 	return rate_table[pll->rate_count - 1].rate;
2318646d4dcSBai Ping }
2328646d4dcSBai Ping 
233*b09c68dcSSascha Hauer static long clk_pll1443x_round_rate(struct clk_hw *hw, unsigned long rate,
234*b09c68dcSSascha Hauer 			unsigned long *prate)
235*b09c68dcSSascha Hauer {
236*b09c68dcSSascha Hauer 	struct clk_pll14xx *pll = to_clk_pll14xx(hw);
237*b09c68dcSSascha Hauer 	struct imx_pll14xx_rate_table t;
238*b09c68dcSSascha Hauer 
239*b09c68dcSSascha Hauer 	imx_pll14xx_calc_settings(pll, rate, *prate, &t);
240*b09c68dcSSascha Hauer 
241*b09c68dcSSascha Hauer 	return t.rate;
242*b09c68dcSSascha Hauer }
243*b09c68dcSSascha Hauer 
24453990cf9SSascha Hauer static unsigned long clk_pll14xx_recalc_rate(struct clk_hw *hw,
2458646d4dcSBai Ping 						  unsigned long parent_rate)
2468646d4dcSBai Ping {
2478646d4dcSBai Ping 	struct clk_pll14xx *pll = to_clk_pll14xx(hw);
24853990cf9SSascha Hauer 	u32 mdiv, pdiv, sdiv, kdiv, pll_div_ctl0, pll_div_ctl1;
2498646d4dcSBai Ping 
250485b4ff5SSascha Hauer 	pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0);
25158f4980cSSascha Hauer 	mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0);
25258f4980cSSascha Hauer 	pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0);
25358f4980cSSascha Hauer 	sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0);
25453990cf9SSascha Hauer 
25553990cf9SSascha Hauer 	if (pll->type == PLL_1443X) {
25653990cf9SSascha Hauer 		pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1);
25758f4980cSSascha Hauer 		kdiv = FIELD_GET(KDIV_MASK, pll_div_ctl1);
25853990cf9SSascha Hauer 	} else {
25953990cf9SSascha Hauer 		kdiv = 0;
26053990cf9SSascha Hauer 	}
2618646d4dcSBai Ping 
26253990cf9SSascha Hauer 	return pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, parent_rate);
2638646d4dcSBai Ping }
2648646d4dcSBai Ping 
265094234fcSLeonard Crestez static inline bool clk_pll14xx_mp_change(const struct imx_pll14xx_rate_table *rate,
2668646d4dcSBai Ping 					  u32 pll_div)
2678646d4dcSBai Ping {
2688646d4dcSBai Ping 	u32 old_mdiv, old_pdiv;
2698646d4dcSBai Ping 
27058f4980cSSascha Hauer 	old_mdiv = FIELD_GET(MDIV_MASK, pll_div);
27158f4980cSSascha Hauer 	old_pdiv = FIELD_GET(PDIV_MASK, pll_div);
2728646d4dcSBai Ping 
2738646d4dcSBai Ping 	return rate->mdiv != old_mdiv || rate->pdiv != old_pdiv;
2748646d4dcSBai Ping }
2758646d4dcSBai Ping 
2768646d4dcSBai Ping static int clk_pll14xx_wait_lock(struct clk_pll14xx *pll)
2778646d4dcSBai Ping {
2788646d4dcSBai Ping 	u32 val;
2798646d4dcSBai Ping 
280485b4ff5SSascha Hauer 	return readl_poll_timeout(pll->base + GNRL_CTL, val, val & LOCK_STATUS, 0,
2818646d4dcSBai Ping 			LOCK_TIMEOUT_US);
2828646d4dcSBai Ping }
2838646d4dcSBai Ping 
2848646d4dcSBai Ping static int clk_pll1416x_set_rate(struct clk_hw *hw, unsigned long drate,
2858646d4dcSBai Ping 				 unsigned long prate)
2868646d4dcSBai Ping {
2878646d4dcSBai Ping 	struct clk_pll14xx *pll = to_clk_pll14xx(hw);
2888646d4dcSBai Ping 	const struct imx_pll14xx_rate_table *rate;
2898646d4dcSBai Ping 	u32 tmp, div_val;
2908646d4dcSBai Ping 	int ret;
2918646d4dcSBai Ping 
2928646d4dcSBai Ping 	rate = imx_get_pll_settings(pll, drate);
2938646d4dcSBai Ping 	if (!rate) {
29480cbc806SSascha Hauer 		pr_err("Invalid rate %lu for pll clk %s\n", drate,
29580cbc806SSascha Hauer 		       clk_hw_get_name(hw));
2968646d4dcSBai Ping 		return -EINVAL;
2978646d4dcSBai Ping 	}
2988646d4dcSBai Ping 
299485b4ff5SSascha Hauer 	tmp = readl_relaxed(pll->base + DIV_CTL0);
3008646d4dcSBai Ping 
301094234fcSLeonard Crestez 	if (!clk_pll14xx_mp_change(rate, tmp)) {
302d77461a6SSascha Hauer 		tmp &= ~SDIV_MASK;
30358f4980cSSascha Hauer 		tmp |= FIELD_PREP(SDIV_MASK, rate->sdiv);
304485b4ff5SSascha Hauer 		writel_relaxed(tmp, pll->base + DIV_CTL0);
3058646d4dcSBai Ping 
3068646d4dcSBai Ping 		return 0;
3078646d4dcSBai Ping 	}
3088646d4dcSBai Ping 
3098646d4dcSBai Ping 	/* Bypass clock and set lock to pll output lock */
310485b4ff5SSascha Hauer 	tmp = readl_relaxed(pll->base + GNRL_CTL);
3118646d4dcSBai Ping 	tmp |= LOCK_SEL_MASK;
312485b4ff5SSascha Hauer 	writel_relaxed(tmp, pll->base + GNRL_CTL);
3138646d4dcSBai Ping 
3148646d4dcSBai Ping 	/* Enable RST */
3158646d4dcSBai Ping 	tmp &= ~RST_MASK;
316485b4ff5SSascha Hauer 	writel_relaxed(tmp, pll->base + GNRL_CTL);
3178646d4dcSBai Ping 
318dee1bc9cSPeng Fan 	/* Enable BYPASS */
319dee1bc9cSPeng Fan 	tmp |= BYPASS_MASK;
320485b4ff5SSascha Hauer 	writel(tmp, pll->base + GNRL_CTL);
321dee1bc9cSPeng Fan 
32258f4980cSSascha Hauer 	div_val = FIELD_PREP(MDIV_MASK, rate->mdiv) | FIELD_PREP(PDIV_MASK, rate->pdiv) |
32358f4980cSSascha Hauer 		FIELD_PREP(SDIV_MASK, rate->sdiv);
324485b4ff5SSascha Hauer 	writel_relaxed(div_val, pll->base + DIV_CTL0);
3258646d4dcSBai Ping 
3268646d4dcSBai Ping 	/*
3278646d4dcSBai Ping 	 * According to SPEC, t3 - t2 need to be greater than
3288646d4dcSBai Ping 	 * 1us and 1/FREF, respectively.
3298646d4dcSBai Ping 	 * FREF is FIN / Prediv, the prediv is [1, 63], so choose
3308646d4dcSBai Ping 	 * 3us.
3318646d4dcSBai Ping 	 */
3328646d4dcSBai Ping 	udelay(3);
3338646d4dcSBai Ping 
3348646d4dcSBai Ping 	/* Disable RST */
3358646d4dcSBai Ping 	tmp |= RST_MASK;
336485b4ff5SSascha Hauer 	writel_relaxed(tmp, pll->base + GNRL_CTL);
3378646d4dcSBai Ping 
3388646d4dcSBai Ping 	/* Wait Lock */
3398646d4dcSBai Ping 	ret = clk_pll14xx_wait_lock(pll);
3408646d4dcSBai Ping 	if (ret)
3418646d4dcSBai Ping 		return ret;
3428646d4dcSBai Ping 
3438646d4dcSBai Ping 	/* Bypass */
3448646d4dcSBai Ping 	tmp &= ~BYPASS_MASK;
345485b4ff5SSascha Hauer 	writel_relaxed(tmp, pll->base + GNRL_CTL);
3468646d4dcSBai Ping 
3478646d4dcSBai Ping 	return 0;
3488646d4dcSBai Ping }
3498646d4dcSBai Ping 
3508646d4dcSBai Ping static int clk_pll1443x_set_rate(struct clk_hw *hw, unsigned long drate,
3518646d4dcSBai Ping 				 unsigned long prate)
3528646d4dcSBai Ping {
3538646d4dcSBai Ping 	struct clk_pll14xx *pll = to_clk_pll14xx(hw);
354*b09c68dcSSascha Hauer 	struct imx_pll14xx_rate_table rate;
355052d03a0SSascha Hauer 	u32 gnrl_ctl, div_ctl0;
3568646d4dcSBai Ping 	int ret;
3578646d4dcSBai Ping 
358*b09c68dcSSascha Hauer 	imx_pll14xx_calc_settings(pll, drate, prate, &rate);
3598646d4dcSBai Ping 
360052d03a0SSascha Hauer 	div_ctl0 = readl_relaxed(pll->base + DIV_CTL0);
3618646d4dcSBai Ping 
362*b09c68dcSSascha Hauer 	if (!clk_pll14xx_mp_change(&rate, div_ctl0)) {
363*b09c68dcSSascha Hauer 		/* only sdiv and/or kdiv changed - no need to RESET PLL */
364052d03a0SSascha Hauer 		div_ctl0 &= ~SDIV_MASK;
365*b09c68dcSSascha Hauer 		div_ctl0 |= FIELD_PREP(SDIV_MASK, rate.sdiv);
366052d03a0SSascha Hauer 		writel_relaxed(div_ctl0, pll->base + DIV_CTL0);
3678646d4dcSBai Ping 
368*b09c68dcSSascha Hauer 		writel_relaxed(FIELD_PREP(KDIV_MASK, rate.kdiv),
369052d03a0SSascha Hauer 			       pll->base + DIV_CTL1);
370094234fcSLeonard Crestez 
3718646d4dcSBai Ping 		return 0;
3728646d4dcSBai Ping 	}
3738646d4dcSBai Ping 
3748646d4dcSBai Ping 	/* Enable RST */
375052d03a0SSascha Hauer 	gnrl_ctl = readl_relaxed(pll->base + GNRL_CTL);
376052d03a0SSascha Hauer 	gnrl_ctl &= ~RST_MASK;
377052d03a0SSascha Hauer 	writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL);
3788646d4dcSBai Ping 
379dee1bc9cSPeng Fan 	/* Enable BYPASS */
380052d03a0SSascha Hauer 	gnrl_ctl |= BYPASS_MASK;
381052d03a0SSascha Hauer 	writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL);
382dee1bc9cSPeng Fan 
383*b09c68dcSSascha Hauer 	div_ctl0 = FIELD_PREP(MDIV_MASK, rate.mdiv) |
384*b09c68dcSSascha Hauer 		   FIELD_PREP(PDIV_MASK, rate.pdiv) |
385*b09c68dcSSascha Hauer 		   FIELD_PREP(SDIV_MASK, rate.sdiv);
386052d03a0SSascha Hauer 	writel_relaxed(div_ctl0, pll->base + DIV_CTL0);
387*b09c68dcSSascha Hauer 
388*b09c68dcSSascha Hauer 	writel_relaxed(FIELD_PREP(KDIV_MASK, rate.kdiv), pll->base + DIV_CTL1);
3898646d4dcSBai Ping 
3908646d4dcSBai Ping 	/*
3918646d4dcSBai Ping 	 * According to SPEC, t3 - t2 need to be greater than
3928646d4dcSBai Ping 	 * 1us and 1/FREF, respectively.
3938646d4dcSBai Ping 	 * FREF is FIN / Prediv, the prediv is [1, 63], so choose
3948646d4dcSBai Ping 	 * 3us.
3958646d4dcSBai Ping 	 */
3968646d4dcSBai Ping 	udelay(3);
3978646d4dcSBai Ping 
3988646d4dcSBai Ping 	/* Disable RST */
399052d03a0SSascha Hauer 	gnrl_ctl |= RST_MASK;
400052d03a0SSascha Hauer 	writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL);
4018646d4dcSBai Ping 
4028646d4dcSBai Ping 	/* Wait Lock*/
4038646d4dcSBai Ping 	ret = clk_pll14xx_wait_lock(pll);
4048646d4dcSBai Ping 	if (ret)
4058646d4dcSBai Ping 		return ret;
4068646d4dcSBai Ping 
4078646d4dcSBai Ping 	/* Bypass */
408052d03a0SSascha Hauer 	gnrl_ctl &= ~BYPASS_MASK;
409052d03a0SSascha Hauer 	writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL);
4108646d4dcSBai Ping 
4118646d4dcSBai Ping 	return 0;
4128646d4dcSBai Ping }
4138646d4dcSBai Ping 
4148646d4dcSBai Ping static int clk_pll14xx_prepare(struct clk_hw *hw)
4158646d4dcSBai Ping {
4168646d4dcSBai Ping 	struct clk_pll14xx *pll = to_clk_pll14xx(hw);
4178646d4dcSBai Ping 	u32 val;
418dee1bc9cSPeng Fan 	int ret;
4198646d4dcSBai Ping 
4208646d4dcSBai Ping 	/*
4218646d4dcSBai Ping 	 * RESETB = 1 from 0, PLL starts its normal
4228646d4dcSBai Ping 	 * operation after lock time
4238646d4dcSBai Ping 	 */
4248646d4dcSBai Ping 	val = readl_relaxed(pll->base + GNRL_CTL);
425dee1bc9cSPeng Fan 	if (val & RST_MASK)
426dee1bc9cSPeng Fan 		return 0;
427dee1bc9cSPeng Fan 	val |= BYPASS_MASK;
428dee1bc9cSPeng Fan 	writel_relaxed(val, pll->base + GNRL_CTL);
4298646d4dcSBai Ping 	val |= RST_MASK;
4308646d4dcSBai Ping 	writel_relaxed(val, pll->base + GNRL_CTL);
4318646d4dcSBai Ping 
432dee1bc9cSPeng Fan 	ret = clk_pll14xx_wait_lock(pll);
433dee1bc9cSPeng Fan 	if (ret)
434dee1bc9cSPeng Fan 		return ret;
435dee1bc9cSPeng Fan 
436dee1bc9cSPeng Fan 	val &= ~BYPASS_MASK;
437dee1bc9cSPeng Fan 	writel_relaxed(val, pll->base + GNRL_CTL);
438dee1bc9cSPeng Fan 
439dee1bc9cSPeng Fan 	return 0;
4408646d4dcSBai Ping }
4418646d4dcSBai Ping 
4428646d4dcSBai Ping static int clk_pll14xx_is_prepared(struct clk_hw *hw)
4438646d4dcSBai Ping {
4448646d4dcSBai Ping 	struct clk_pll14xx *pll = to_clk_pll14xx(hw);
4458646d4dcSBai Ping 	u32 val;
4468646d4dcSBai Ping 
4478646d4dcSBai Ping 	val = readl_relaxed(pll->base + GNRL_CTL);
4488646d4dcSBai Ping 
4498646d4dcSBai Ping 	return (val & RST_MASK) ? 1 : 0;
4508646d4dcSBai Ping }
4518646d4dcSBai Ping 
4528646d4dcSBai Ping static void clk_pll14xx_unprepare(struct clk_hw *hw)
4538646d4dcSBai Ping {
4548646d4dcSBai Ping 	struct clk_pll14xx *pll = to_clk_pll14xx(hw);
4558646d4dcSBai Ping 	u32 val;
4568646d4dcSBai Ping 
4578646d4dcSBai Ping 	/*
4588646d4dcSBai Ping 	 * Set RST to 0, power down mode is enabled and
4598646d4dcSBai Ping 	 * every digital block is reset
4608646d4dcSBai Ping 	 */
4618646d4dcSBai Ping 	val = readl_relaxed(pll->base + GNRL_CTL);
4628646d4dcSBai Ping 	val &= ~RST_MASK;
4638646d4dcSBai Ping 	writel_relaxed(val, pll->base + GNRL_CTL);
4648646d4dcSBai Ping }
4658646d4dcSBai Ping 
4668646d4dcSBai Ping static const struct clk_ops clk_pll1416x_ops = {
4678646d4dcSBai Ping 	.prepare	= clk_pll14xx_prepare,
4688646d4dcSBai Ping 	.unprepare	= clk_pll14xx_unprepare,
4698646d4dcSBai Ping 	.is_prepared	= clk_pll14xx_is_prepared,
47053990cf9SSascha Hauer 	.recalc_rate	= clk_pll14xx_recalc_rate,
471*b09c68dcSSascha Hauer 	.round_rate	= clk_pll1416x_round_rate,
4728646d4dcSBai Ping 	.set_rate	= clk_pll1416x_set_rate,
4738646d4dcSBai Ping };
4748646d4dcSBai Ping 
4758646d4dcSBai Ping static const struct clk_ops clk_pll1416x_min_ops = {
47653990cf9SSascha Hauer 	.recalc_rate	= clk_pll14xx_recalc_rate,
4778646d4dcSBai Ping };
4788646d4dcSBai Ping 
4798646d4dcSBai Ping static const struct clk_ops clk_pll1443x_ops = {
4808646d4dcSBai Ping 	.prepare	= clk_pll14xx_prepare,
4818646d4dcSBai Ping 	.unprepare	= clk_pll14xx_unprepare,
4828646d4dcSBai Ping 	.is_prepared	= clk_pll14xx_is_prepared,
48353990cf9SSascha Hauer 	.recalc_rate	= clk_pll14xx_recalc_rate,
484*b09c68dcSSascha Hauer 	.round_rate	= clk_pll1443x_round_rate,
4858646d4dcSBai Ping 	.set_rate	= clk_pll1443x_set_rate,
4868646d4dcSBai Ping };
4878646d4dcSBai Ping 
48855a8b3cdSAbel Vesa struct clk_hw *imx_dev_clk_hw_pll14xx(struct device *dev, const char *name,
48955a8b3cdSAbel Vesa 				const char *parent_name, void __iomem *base,
4908646d4dcSBai Ping 				const struct imx_pll14xx_clk *pll_clk)
4918646d4dcSBai Ping {
4928646d4dcSBai Ping 	struct clk_pll14xx *pll;
49310c34b50SPeng Fan 	struct clk_hw *hw;
4948646d4dcSBai Ping 	struct clk_init_data init;
49510c34b50SPeng Fan 	int ret;
496a9aa8306SPeng Fan 	u32 val;
4978646d4dcSBai Ping 
4988646d4dcSBai Ping 	pll = kzalloc(sizeof(*pll), GFP_KERNEL);
4998646d4dcSBai Ping 	if (!pll)
5008646d4dcSBai Ping 		return ERR_PTR(-ENOMEM);
5018646d4dcSBai Ping 
5028646d4dcSBai Ping 	init.name = name;
5038646d4dcSBai Ping 	init.flags = pll_clk->flags;
5048646d4dcSBai Ping 	init.parent_names = &parent_name;
5058646d4dcSBai Ping 	init.num_parents = 1;
5068646d4dcSBai Ping 
5078646d4dcSBai Ping 	switch (pll_clk->type) {
5088646d4dcSBai Ping 	case PLL_1416X:
509f89b9e1bSLeonard Crestez 		if (!pll_clk->rate_table)
5108646d4dcSBai Ping 			init.ops = &clk_pll1416x_min_ops;
5118646d4dcSBai Ping 		else
5128646d4dcSBai Ping 			init.ops = &clk_pll1416x_ops;
5138646d4dcSBai Ping 		break;
5148646d4dcSBai Ping 	case PLL_1443X:
5158646d4dcSBai Ping 		init.ops = &clk_pll1443x_ops;
5168646d4dcSBai Ping 		break;
5178646d4dcSBai Ping 	default:
51880cbc806SSascha Hauer 		pr_err("Unknown pll type for pll clk %s\n", name);
519530cf8d4SAnson Huang 		kfree(pll);
520530cf8d4SAnson Huang 		return ERR_PTR(-EINVAL);
5218404c661STom Rix 	}
5228646d4dcSBai Ping 
5238646d4dcSBai Ping 	pll->base = base;
5248646d4dcSBai Ping 	pll->hw.init = &init;
5258646d4dcSBai Ping 	pll->type = pll_clk->type;
5268646d4dcSBai Ping 	pll->rate_table = pll_clk->rate_table;
5278646d4dcSBai Ping 	pll->rate_count = pll_clk->rate_count;
5288646d4dcSBai Ping 
529a9aa8306SPeng Fan 	val = readl_relaxed(pll->base + GNRL_CTL);
530a9aa8306SPeng Fan 	val &= ~BYPASS_MASK;
531a9aa8306SPeng Fan 	writel_relaxed(val, pll->base + GNRL_CTL);
532a9aa8306SPeng Fan 
53310c34b50SPeng Fan 	hw = &pll->hw;
53410c34b50SPeng Fan 
53555a8b3cdSAbel Vesa 	ret = clk_hw_register(dev, hw);
53610c34b50SPeng Fan 	if (ret) {
53780cbc806SSascha Hauer 		pr_err("failed to register pll %s %d\n", name, ret);
5388646d4dcSBai Ping 		kfree(pll);
53910c34b50SPeng Fan 		return ERR_PTR(ret);
5408646d4dcSBai Ping 	}
5418646d4dcSBai Ping 
54210c34b50SPeng Fan 	return hw;
5438646d4dcSBai Ping }
544870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_dev_clk_hw_pll14xx);
545