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)
32b09c68dcSSascha Hauer #define KDIV_MIN SHRT_MIN
33b09c68dcSSascha 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),
576a11d3a0SMarek Vasut PLL_1416X_RATE(640000000U, 320, 3, 2),
5843cdaa15SAnson Huang PLL_1416X_RATE(600000000U, 300, 3, 2),
596a11d3a0SMarek Vasut PLL_1416X_RATE(320000000U, 160, 3, 2),
6043cdaa15SAnson Huang };
6143cdaa15SAnson Huang
628f2d3c17SYueHaibing static const struct imx_pll14xx_rate_table imx_pll1443x_tbl[] = {
6357795654SAnson Huang PLL_1443X_RATE(1039500000U, 173, 2, 1, 16384),
6443cdaa15SAnson Huang PLL_1443X_RATE(650000000U, 325, 3, 2, 0),
6543cdaa15SAnson Huang PLL_1443X_RATE(594000000U, 198, 2, 2, 0),
6657795654SAnson Huang PLL_1443X_RATE(519750000U, 173, 2, 2, 16384),
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
imx_get_pll_settings(struct clk_pll14xx * pll,unsigned long rate)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
pll14xx_calc_rate(struct clk_pll14xx * pll,int mdiv,int pdiv,int sdiv,int kdiv,unsigned long prate)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
pll1443x_calc_kdiv(int mdiv,int pdiv,int sdiv,unsigned long rate,unsigned long prate)118b09c68dcSSascha Hauer static long pll1443x_calc_kdiv(int mdiv, int pdiv, int sdiv,
119b09c68dcSSascha Hauer unsigned long rate, unsigned long prate)
120b09c68dcSSascha Hauer {
121b09c68dcSSascha Hauer long kdiv;
122b09c68dcSSascha Hauer
123b09c68dcSSascha Hauer /* calc kdiv = round(rate * pdiv * 65536 * 2^sdiv / prate) - (mdiv * 65536) */
124b09c68dcSSascha Hauer kdiv = ((rate * ((pdiv * 65536) << sdiv) + prate / 2) / prate) - (mdiv * 65536);
125b09c68dcSSascha Hauer
126b09c68dcSSascha Hauer return clamp_t(short, kdiv, KDIV_MIN, KDIV_MAX);
127b09c68dcSSascha Hauer }
128b09c68dcSSascha Hauer
imx_pll14xx_calc_settings(struct clk_pll14xx * pll,unsigned long rate,unsigned long prate,struct imx_pll14xx_rate_table * t)129b09c68dcSSascha Hauer static void imx_pll14xx_calc_settings(struct clk_pll14xx *pll, unsigned long rate,
130b09c68dcSSascha Hauer unsigned long prate, struct imx_pll14xx_rate_table *t)
131b09c68dcSSascha Hauer {
132b09c68dcSSascha Hauer u32 pll_div_ctl0, pll_div_ctl1;
133b09c68dcSSascha Hauer int mdiv, pdiv, sdiv, kdiv;
134b09c68dcSSascha Hauer long fvco, rate_min, rate_max, dist, best = LONG_MAX;
135b09c68dcSSascha Hauer const struct imx_pll14xx_rate_table *tt;
136b09c68dcSSascha Hauer
137b09c68dcSSascha Hauer /*
138b09c68dcSSascha Hauer * Fractional PLL constrains:
139b09c68dcSSascha Hauer *
140*37cfd5e4SMarco Felsch * a) 1 <= p <= 63
141*37cfd5e4SMarco Felsch * b) 64 <= m <= 1023
142*37cfd5e4SMarco Felsch * c) 0 <= s <= 6
143*37cfd5e4SMarco Felsch * d) -32768 <= k <= 32767
144b09c68dcSSascha Hauer *
145b09c68dcSSascha Hauer * fvco = (m * 65536 + k) * prate / (p * 65536)
146b09c68dcSSascha Hauer */
147b09c68dcSSascha Hauer
148b09c68dcSSascha Hauer /* First try if we can get the desired rate from one of the static entries */
149b09c68dcSSascha Hauer tt = imx_get_pll_settings(pll, rate);
150b09c68dcSSascha Hauer if (tt) {
151b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld, Using PLL setting from table\n",
152b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate);
153b09c68dcSSascha Hauer t->rate = tt->rate;
154b09c68dcSSascha Hauer t->mdiv = tt->mdiv;
155b09c68dcSSascha Hauer t->pdiv = tt->pdiv;
156b09c68dcSSascha Hauer t->sdiv = tt->sdiv;
157b09c68dcSSascha Hauer t->kdiv = tt->kdiv;
158b09c68dcSSascha Hauer return;
159b09c68dcSSascha Hauer }
160b09c68dcSSascha Hauer
161b09c68dcSSascha Hauer pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0);
162b09c68dcSSascha Hauer mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0);
163b09c68dcSSascha Hauer pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0);
164b09c68dcSSascha Hauer sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0);
165b09c68dcSSascha Hauer pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1);
166b09c68dcSSascha Hauer
167b09c68dcSSascha Hauer /* Then see if we can get the desired rate by only adjusting kdiv (glitch free) */
168b09c68dcSSascha Hauer rate_min = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MIN, prate);
169b09c68dcSSascha Hauer rate_max = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MAX, prate);
170b09c68dcSSascha Hauer
171b09c68dcSSascha Hauer if (rate >= rate_min && rate <= rate_max) {
172b09c68dcSSascha Hauer kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate);
173b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld Only adjust kdiv %ld -> %d\n",
174b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate,
175b09c68dcSSascha Hauer FIELD_GET(KDIV_MASK, pll_div_ctl1), kdiv);
176b09c68dcSSascha Hauer fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate);
177b09c68dcSSascha Hauer t->rate = (unsigned int)fvco;
178b09c68dcSSascha Hauer t->mdiv = mdiv;
179b09c68dcSSascha Hauer t->pdiv = pdiv;
180b09c68dcSSascha Hauer t->sdiv = sdiv;
181b09c68dcSSascha Hauer t->kdiv = kdiv;
182b09c68dcSSascha Hauer return;
183b09c68dcSSascha Hauer }
184b09c68dcSSascha Hauer
185b09c68dcSSascha Hauer /* Finally calculate best values */
186*37cfd5e4SMarco Felsch for (pdiv = 1; pdiv <= 63; pdiv++) {
187b09c68dcSSascha Hauer for (sdiv = 0; sdiv <= 6; sdiv++) {
188b09c68dcSSascha Hauer /* calc mdiv = round(rate * pdiv * 2^sdiv) / prate) */
189b09c68dcSSascha Hauer mdiv = DIV_ROUND_CLOSEST(rate * (pdiv << sdiv), prate);
190b09c68dcSSascha Hauer mdiv = clamp(mdiv, 64, 1023);
191b09c68dcSSascha Hauer
192b09c68dcSSascha Hauer kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate);
193b09c68dcSSascha Hauer fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate);
194b09c68dcSSascha Hauer
195b09c68dcSSascha Hauer /* best match */
196b09c68dcSSascha Hauer dist = abs((long)rate - (long)fvco);
197b09c68dcSSascha Hauer if (dist < best) {
198b09c68dcSSascha Hauer best = dist;
199b09c68dcSSascha Hauer t->rate = (unsigned int)fvco;
200b09c68dcSSascha Hauer t->mdiv = mdiv;
201b09c68dcSSascha Hauer t->pdiv = pdiv;
202b09c68dcSSascha Hauer t->sdiv = sdiv;
203b09c68dcSSascha Hauer t->kdiv = kdiv;
204b09c68dcSSascha Hauer
205b09c68dcSSascha Hauer if (!dist)
206b09c68dcSSascha Hauer goto found;
207b09c68dcSSascha Hauer }
208b09c68dcSSascha Hauer }
209b09c68dcSSascha Hauer }
210b09c68dcSSascha Hauer found:
211b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld got=%d (pdiv=%d sdiv=%d mdiv=%d kdiv=%d)\n",
212b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate, t->rate, t->pdiv, t->sdiv,
213b09c68dcSSascha Hauer t->mdiv, t->kdiv);
214b09c68dcSSascha Hauer }
215b09c68dcSSascha Hauer
clk_pll1416x_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)216b09c68dcSSascha Hauer static long clk_pll1416x_round_rate(struct clk_hw *hw, unsigned long rate,
2178646d4dcSBai Ping unsigned long *prate)
2188646d4dcSBai Ping {
2198646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw);
2208646d4dcSBai Ping const struct imx_pll14xx_rate_table *rate_table = pll->rate_table;
2218646d4dcSBai Ping int i;
2228646d4dcSBai Ping
2235ff50031SSascha Hauer /* Assuming rate_table is in descending order */
2248646d4dcSBai Ping for (i = 0; i < pll->rate_count; i++)
2258646d4dcSBai Ping if (rate >= rate_table[i].rate)
2268646d4dcSBai Ping return rate_table[i].rate;
2278646d4dcSBai Ping
2288646d4dcSBai Ping /* return minimum supported value */
2295ff50031SSascha Hauer return rate_table[pll->rate_count - 1].rate;
2308646d4dcSBai Ping }
2318646d4dcSBai Ping
clk_pll1443x_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)232b09c68dcSSascha Hauer static long clk_pll1443x_round_rate(struct clk_hw *hw, unsigned long rate,
233b09c68dcSSascha Hauer unsigned long *prate)
234b09c68dcSSascha Hauer {
235b09c68dcSSascha Hauer struct clk_pll14xx *pll = to_clk_pll14xx(hw);
236b09c68dcSSascha Hauer struct imx_pll14xx_rate_table t;
237b09c68dcSSascha Hauer
238b09c68dcSSascha Hauer imx_pll14xx_calc_settings(pll, rate, *prate, &t);
239b09c68dcSSascha Hauer
240b09c68dcSSascha Hauer return t.rate;
241b09c68dcSSascha Hauer }
242b09c68dcSSascha Hauer
clk_pll14xx_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)24353990cf9SSascha Hauer static unsigned long clk_pll14xx_recalc_rate(struct clk_hw *hw,
2448646d4dcSBai Ping unsigned long parent_rate)
2458646d4dcSBai Ping {
2468646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw);
24753990cf9SSascha Hauer u32 mdiv, pdiv, sdiv, kdiv, pll_div_ctl0, pll_div_ctl1;
2488646d4dcSBai Ping
249485b4ff5SSascha Hauer pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0);
25058f4980cSSascha Hauer mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0);
25158f4980cSSascha Hauer pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0);
25258f4980cSSascha Hauer sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0);
25353990cf9SSascha Hauer
25453990cf9SSascha Hauer if (pll->type == PLL_1443X) {
25553990cf9SSascha Hauer pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1);
25602d7bd1eSKevin Groeneveld kdiv = (s16)FIELD_GET(KDIV_MASK, pll_div_ctl1);
25753990cf9SSascha Hauer } else {
25853990cf9SSascha Hauer kdiv = 0;
25953990cf9SSascha Hauer }
2608646d4dcSBai Ping
26153990cf9SSascha Hauer return pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, parent_rate);
2628646d4dcSBai Ping }
2638646d4dcSBai Ping
clk_pll14xx_mp_change(const struct imx_pll14xx_rate_table * rate,u32 pll_div)264094234fcSLeonard Crestez static inline bool clk_pll14xx_mp_change(const struct imx_pll14xx_rate_table *rate,
2658646d4dcSBai Ping u32 pll_div)
2668646d4dcSBai Ping {
2678646d4dcSBai Ping u32 old_mdiv, old_pdiv;
2688646d4dcSBai Ping
26958f4980cSSascha Hauer old_mdiv = FIELD_GET(MDIV_MASK, pll_div);
27058f4980cSSascha Hauer old_pdiv = FIELD_GET(PDIV_MASK, pll_div);
2718646d4dcSBai Ping
2728646d4dcSBai Ping return rate->mdiv != old_mdiv || rate->pdiv != old_pdiv;
2738646d4dcSBai Ping }
2748646d4dcSBai Ping
clk_pll14xx_wait_lock(struct clk_pll14xx * pll)2758646d4dcSBai Ping static int clk_pll14xx_wait_lock(struct clk_pll14xx *pll)
2768646d4dcSBai Ping {
2778646d4dcSBai Ping u32 val;
2788646d4dcSBai Ping
279485b4ff5SSascha Hauer return readl_poll_timeout(pll->base + GNRL_CTL, val, val & LOCK_STATUS, 0,
2808646d4dcSBai Ping LOCK_TIMEOUT_US);
2818646d4dcSBai Ping }
2828646d4dcSBai Ping
clk_pll1416x_set_rate(struct clk_hw * hw,unsigned long drate,unsigned long prate)2838646d4dcSBai Ping static int clk_pll1416x_set_rate(struct clk_hw *hw, unsigned long drate,
2848646d4dcSBai Ping unsigned long prate)
2858646d4dcSBai Ping {
2868646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw);
2878646d4dcSBai Ping const struct imx_pll14xx_rate_table *rate;
2888646d4dcSBai Ping u32 tmp, div_val;
2898646d4dcSBai Ping int ret;
2908646d4dcSBai Ping
2918646d4dcSBai Ping rate = imx_get_pll_settings(pll, drate);
2928646d4dcSBai Ping if (!rate) {
29380cbc806SSascha Hauer pr_err("Invalid rate %lu for pll clk %s\n", drate,
29480cbc806SSascha Hauer clk_hw_get_name(hw));
2958646d4dcSBai Ping return -EINVAL;
2968646d4dcSBai Ping }
2978646d4dcSBai Ping
298485b4ff5SSascha Hauer tmp = readl_relaxed(pll->base + DIV_CTL0);
2998646d4dcSBai Ping
300094234fcSLeonard Crestez if (!clk_pll14xx_mp_change(rate, tmp)) {
301d77461a6SSascha Hauer tmp &= ~SDIV_MASK;
30258f4980cSSascha Hauer tmp |= FIELD_PREP(SDIV_MASK, rate->sdiv);
303485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + DIV_CTL0);
3048646d4dcSBai Ping
3058646d4dcSBai Ping return 0;
3068646d4dcSBai Ping }
3078646d4dcSBai Ping
3088646d4dcSBai Ping /* Bypass clock and set lock to pll output lock */
309485b4ff5SSascha Hauer tmp = readl_relaxed(pll->base + GNRL_CTL);
3108646d4dcSBai Ping tmp |= LOCK_SEL_MASK;
311485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL);
3128646d4dcSBai Ping
3138646d4dcSBai Ping /* Enable RST */
3148646d4dcSBai Ping tmp &= ~RST_MASK;
315485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL);
3168646d4dcSBai Ping
317dee1bc9cSPeng Fan /* Enable BYPASS */
318dee1bc9cSPeng Fan tmp |= BYPASS_MASK;
319485b4ff5SSascha Hauer writel(tmp, pll->base + GNRL_CTL);
320dee1bc9cSPeng Fan
32158f4980cSSascha Hauer div_val = FIELD_PREP(MDIV_MASK, rate->mdiv) | FIELD_PREP(PDIV_MASK, rate->pdiv) |
32258f4980cSSascha Hauer FIELD_PREP(SDIV_MASK, rate->sdiv);
323485b4ff5SSascha Hauer writel_relaxed(div_val, pll->base + DIV_CTL0);
3248646d4dcSBai Ping
3258646d4dcSBai Ping /*
3268646d4dcSBai Ping * According to SPEC, t3 - t2 need to be greater than
3278646d4dcSBai Ping * 1us and 1/FREF, respectively.
3288646d4dcSBai Ping * FREF is FIN / Prediv, the prediv is [1, 63], so choose
3298646d4dcSBai Ping * 3us.
3308646d4dcSBai Ping */
3318646d4dcSBai Ping udelay(3);
3328646d4dcSBai Ping
3338646d4dcSBai Ping /* Disable RST */
3348646d4dcSBai Ping tmp |= RST_MASK;
335485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL);
3368646d4dcSBai Ping
3378646d4dcSBai Ping /* Wait Lock */
3388646d4dcSBai Ping ret = clk_pll14xx_wait_lock(pll);
3398646d4dcSBai Ping if (ret)
3408646d4dcSBai Ping return ret;
3418646d4dcSBai Ping
3428646d4dcSBai Ping /* Bypass */
3438646d4dcSBai Ping tmp &= ~BYPASS_MASK;
344485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL);
3458646d4dcSBai Ping
3468646d4dcSBai Ping return 0;
3478646d4dcSBai Ping }
3488646d4dcSBai Ping
clk_pll1443x_set_rate(struct clk_hw * hw,unsigned long drate,unsigned long prate)3498646d4dcSBai Ping static int clk_pll1443x_set_rate(struct clk_hw *hw, unsigned long drate,
3508646d4dcSBai Ping unsigned long prate)
3518646d4dcSBai Ping {
3528646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw);
353b09c68dcSSascha Hauer struct imx_pll14xx_rate_table rate;
354052d03a0SSascha Hauer u32 gnrl_ctl, div_ctl0;
3558646d4dcSBai Ping int ret;
3568646d4dcSBai Ping
357b09c68dcSSascha Hauer imx_pll14xx_calc_settings(pll, drate, prate, &rate);
3588646d4dcSBai Ping
359052d03a0SSascha Hauer div_ctl0 = readl_relaxed(pll->base + DIV_CTL0);
3608646d4dcSBai Ping
361b09c68dcSSascha Hauer if (!clk_pll14xx_mp_change(&rate, div_ctl0)) {
362b09c68dcSSascha Hauer /* only sdiv and/or kdiv changed - no need to RESET PLL */
363052d03a0SSascha Hauer div_ctl0 &= ~SDIV_MASK;
364b09c68dcSSascha Hauer div_ctl0 |= FIELD_PREP(SDIV_MASK, rate.sdiv);
365052d03a0SSascha Hauer writel_relaxed(div_ctl0, pll->base + DIV_CTL0);
3668646d4dcSBai Ping
367b09c68dcSSascha Hauer writel_relaxed(FIELD_PREP(KDIV_MASK, rate.kdiv),
368052d03a0SSascha Hauer pll->base + DIV_CTL1);
369094234fcSLeonard Crestez
3708646d4dcSBai Ping return 0;
3718646d4dcSBai Ping }
3728646d4dcSBai Ping
3738646d4dcSBai Ping /* Enable RST */
374052d03a0SSascha Hauer gnrl_ctl = readl_relaxed(pll->base + GNRL_CTL);
375052d03a0SSascha Hauer gnrl_ctl &= ~RST_MASK;
376052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL);
3778646d4dcSBai Ping
378dee1bc9cSPeng Fan /* Enable BYPASS */
379052d03a0SSascha Hauer gnrl_ctl |= BYPASS_MASK;
380052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL);
381dee1bc9cSPeng Fan
382b09c68dcSSascha Hauer div_ctl0 = FIELD_PREP(MDIV_MASK, rate.mdiv) |
383b09c68dcSSascha Hauer FIELD_PREP(PDIV_MASK, rate.pdiv) |
384b09c68dcSSascha Hauer FIELD_PREP(SDIV_MASK, rate.sdiv);
385052d03a0SSascha Hauer writel_relaxed(div_ctl0, pll->base + DIV_CTL0);
386b09c68dcSSascha Hauer
387b09c68dcSSascha Hauer writel_relaxed(FIELD_PREP(KDIV_MASK, rate.kdiv), pll->base + DIV_CTL1);
3888646d4dcSBai Ping
3898646d4dcSBai Ping /*
3908646d4dcSBai Ping * According to SPEC, t3 - t2 need to be greater than
3918646d4dcSBai Ping * 1us and 1/FREF, respectively.
3928646d4dcSBai Ping * FREF is FIN / Prediv, the prediv is [1, 63], so choose
3938646d4dcSBai Ping * 3us.
3948646d4dcSBai Ping */
3958646d4dcSBai Ping udelay(3);
3968646d4dcSBai Ping
3978646d4dcSBai Ping /* Disable RST */
398052d03a0SSascha Hauer gnrl_ctl |= RST_MASK;
399052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL);
4008646d4dcSBai Ping
4018646d4dcSBai Ping /* Wait Lock*/
4028646d4dcSBai Ping ret = clk_pll14xx_wait_lock(pll);
4038646d4dcSBai Ping if (ret)
4048646d4dcSBai Ping return ret;
4058646d4dcSBai Ping
4068646d4dcSBai Ping /* Bypass */
407052d03a0SSascha Hauer gnrl_ctl &= ~BYPASS_MASK;
408052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL);
4098646d4dcSBai Ping
4108646d4dcSBai Ping return 0;
4118646d4dcSBai Ping }
4128646d4dcSBai Ping
clk_pll14xx_prepare(struct clk_hw * hw)4138646d4dcSBai Ping static int clk_pll14xx_prepare(struct clk_hw *hw)
4148646d4dcSBai Ping {
4158646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw);
4168646d4dcSBai Ping u32 val;
417dee1bc9cSPeng Fan int ret;
4188646d4dcSBai Ping
4198646d4dcSBai Ping /*
4208646d4dcSBai Ping * RESETB = 1 from 0, PLL starts its normal
4218646d4dcSBai Ping * operation after lock time
4228646d4dcSBai Ping */
4238646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL);
424dee1bc9cSPeng Fan if (val & RST_MASK)
425dee1bc9cSPeng Fan return 0;
426dee1bc9cSPeng Fan val |= BYPASS_MASK;
427dee1bc9cSPeng Fan writel_relaxed(val, pll->base + GNRL_CTL);
4288646d4dcSBai Ping val |= RST_MASK;
4298646d4dcSBai Ping writel_relaxed(val, pll->base + GNRL_CTL);
4308646d4dcSBai Ping
431dee1bc9cSPeng Fan ret = clk_pll14xx_wait_lock(pll);
432dee1bc9cSPeng Fan if (ret)
433dee1bc9cSPeng Fan return ret;
434dee1bc9cSPeng Fan
435dee1bc9cSPeng Fan val &= ~BYPASS_MASK;
436dee1bc9cSPeng Fan writel_relaxed(val, pll->base + GNRL_CTL);
437dee1bc9cSPeng Fan
438dee1bc9cSPeng Fan return 0;
4398646d4dcSBai Ping }
4408646d4dcSBai Ping
clk_pll14xx_is_prepared(struct clk_hw * hw)4418646d4dcSBai Ping static int clk_pll14xx_is_prepared(struct clk_hw *hw)
4428646d4dcSBai Ping {
4438646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw);
4448646d4dcSBai Ping u32 val;
4458646d4dcSBai Ping
4468646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL);
4478646d4dcSBai Ping
4488646d4dcSBai Ping return (val & RST_MASK) ? 1 : 0;
4498646d4dcSBai Ping }
4508646d4dcSBai Ping
clk_pll14xx_unprepare(struct clk_hw * hw)4518646d4dcSBai Ping static void clk_pll14xx_unprepare(struct clk_hw *hw)
4528646d4dcSBai Ping {
4538646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw);
4548646d4dcSBai Ping u32 val;
4558646d4dcSBai Ping
4568646d4dcSBai Ping /*
4578646d4dcSBai Ping * Set RST to 0, power down mode is enabled and
4588646d4dcSBai Ping * every digital block is reset
4598646d4dcSBai Ping */
4608646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL);
4618646d4dcSBai Ping val &= ~RST_MASK;
4628646d4dcSBai Ping writel_relaxed(val, pll->base + GNRL_CTL);
4638646d4dcSBai Ping }
4648646d4dcSBai Ping
4658646d4dcSBai Ping static const struct clk_ops clk_pll1416x_ops = {
4668646d4dcSBai Ping .prepare = clk_pll14xx_prepare,
4678646d4dcSBai Ping .unprepare = clk_pll14xx_unprepare,
4688646d4dcSBai Ping .is_prepared = clk_pll14xx_is_prepared,
46953990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate,
470b09c68dcSSascha Hauer .round_rate = clk_pll1416x_round_rate,
4718646d4dcSBai Ping .set_rate = clk_pll1416x_set_rate,
4728646d4dcSBai Ping };
4738646d4dcSBai Ping
4748646d4dcSBai Ping static const struct clk_ops clk_pll1416x_min_ops = {
47553990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate,
4768646d4dcSBai Ping };
4778646d4dcSBai Ping
4788646d4dcSBai Ping static const struct clk_ops clk_pll1443x_ops = {
4798646d4dcSBai Ping .prepare = clk_pll14xx_prepare,
4808646d4dcSBai Ping .unprepare = clk_pll14xx_unprepare,
4818646d4dcSBai Ping .is_prepared = clk_pll14xx_is_prepared,
48253990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate,
483b09c68dcSSascha Hauer .round_rate = clk_pll1443x_round_rate,
4848646d4dcSBai Ping .set_rate = clk_pll1443x_set_rate,
4858646d4dcSBai Ping };
4868646d4dcSBai Ping
imx_dev_clk_hw_pll14xx(struct device * dev,const char * name,const char * parent_name,void __iomem * base,const struct imx_pll14xx_clk * pll_clk)48755a8b3cdSAbel Vesa struct clk_hw *imx_dev_clk_hw_pll14xx(struct device *dev, const char *name,
48855a8b3cdSAbel Vesa const char *parent_name, void __iomem *base,
4898646d4dcSBai Ping const struct imx_pll14xx_clk *pll_clk)
4908646d4dcSBai Ping {
4918646d4dcSBai Ping struct clk_pll14xx *pll;
49210c34b50SPeng Fan struct clk_hw *hw;
4938646d4dcSBai Ping struct clk_init_data init;
49410c34b50SPeng Fan int ret;
495a9aa8306SPeng Fan u32 val;
4968646d4dcSBai Ping
4978646d4dcSBai Ping pll = kzalloc(sizeof(*pll), GFP_KERNEL);
4988646d4dcSBai Ping if (!pll)
4998646d4dcSBai Ping return ERR_PTR(-ENOMEM);
5008646d4dcSBai Ping
5018646d4dcSBai Ping init.name = name;
5028646d4dcSBai Ping init.flags = pll_clk->flags;
5038646d4dcSBai Ping init.parent_names = &parent_name;
5048646d4dcSBai Ping init.num_parents = 1;
5058646d4dcSBai Ping
5068646d4dcSBai Ping switch (pll_clk->type) {
5078646d4dcSBai Ping case PLL_1416X:
508f89b9e1bSLeonard Crestez if (!pll_clk->rate_table)
5098646d4dcSBai Ping init.ops = &clk_pll1416x_min_ops;
5108646d4dcSBai Ping else
5118646d4dcSBai Ping init.ops = &clk_pll1416x_ops;
5128646d4dcSBai Ping break;
5138646d4dcSBai Ping case PLL_1443X:
5148646d4dcSBai Ping init.ops = &clk_pll1443x_ops;
5158646d4dcSBai Ping break;
5168646d4dcSBai Ping default:
51780cbc806SSascha Hauer pr_err("Unknown pll type for pll clk %s\n", name);
518530cf8d4SAnson Huang kfree(pll);
519530cf8d4SAnson Huang return ERR_PTR(-EINVAL);
5208404c661STom Rix }
5218646d4dcSBai Ping
5228646d4dcSBai Ping pll->base = base;
5238646d4dcSBai Ping pll->hw.init = &init;
5248646d4dcSBai Ping pll->type = pll_clk->type;
5258646d4dcSBai Ping pll->rate_table = pll_clk->rate_table;
5268646d4dcSBai Ping pll->rate_count = pll_clk->rate_count;
5278646d4dcSBai Ping
528a9aa8306SPeng Fan val = readl_relaxed(pll->base + GNRL_CTL);
529a9aa8306SPeng Fan val &= ~BYPASS_MASK;
530a9aa8306SPeng Fan writel_relaxed(val, pll->base + GNRL_CTL);
531a9aa8306SPeng Fan
53210c34b50SPeng Fan hw = &pll->hw;
53310c34b50SPeng Fan
53455a8b3cdSAbel Vesa ret = clk_hw_register(dev, hw);
53510c34b50SPeng Fan if (ret) {
53680cbc806SSascha Hauer pr_err("failed to register pll %s %d\n", name, ret);
5378646d4dcSBai Ping kfree(pll);
53810c34b50SPeng Fan return ERR_PTR(ret);
5398646d4dcSBai Ping }
5408646d4dcSBai Ping
54110c34b50SPeng Fan return hw;
5428646d4dcSBai Ping }
543870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_dev_clk_hw_pll14xx);
544