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 PLL_1443X_RATE(393216000U, 262, 2, 3, 9437), 6843cdaa15SAnson Huang PLL_1443X_RATE(361267200U, 361, 3, 3, 17511), 6943cdaa15SAnson Huang }; 7043cdaa15SAnson Huang 7143cdaa15SAnson Huang struct imx_pll14xx_clk imx_1443x_pll = { 7243cdaa15SAnson Huang .type = PLL_1443X, 7343cdaa15SAnson Huang .rate_table = imx_pll1443x_tbl, 7443cdaa15SAnson Huang .rate_count = ARRAY_SIZE(imx_pll1443x_tbl), 7543cdaa15SAnson Huang }; 76870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_1443x_pll); 7743cdaa15SAnson Huang 78e18f6471SLeonard Crestez struct imx_pll14xx_clk imx_1443x_dram_pll = { 79e18f6471SLeonard Crestez .type = PLL_1443X, 80e18f6471SLeonard Crestez .rate_table = imx_pll1443x_tbl, 81e18f6471SLeonard Crestez .rate_count = ARRAY_SIZE(imx_pll1443x_tbl), 82e18f6471SLeonard Crestez .flags = CLK_GET_RATE_NOCACHE, 83e18f6471SLeonard Crestez }; 84870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_1443x_dram_pll); 85e18f6471SLeonard Crestez 8643cdaa15SAnson Huang struct imx_pll14xx_clk imx_1416x_pll = { 8743cdaa15SAnson Huang .type = PLL_1416X, 8843cdaa15SAnson Huang .rate_table = imx_pll1416x_tbl, 8943cdaa15SAnson Huang .rate_count = ARRAY_SIZE(imx_pll1416x_tbl), 9043cdaa15SAnson Huang }; 91870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_1416x_pll); 9243cdaa15SAnson Huang 938646d4dcSBai Ping static const struct imx_pll14xx_rate_table *imx_get_pll_settings( 948646d4dcSBai Ping struct clk_pll14xx *pll, unsigned long rate) 958646d4dcSBai Ping { 968646d4dcSBai Ping const struct imx_pll14xx_rate_table *rate_table = pll->rate_table; 978646d4dcSBai Ping int i; 988646d4dcSBai Ping 998646d4dcSBai Ping for (i = 0; i < pll->rate_count; i++) 1008646d4dcSBai Ping if (rate == rate_table[i].rate) 1018646d4dcSBai Ping return &rate_table[i]; 1028646d4dcSBai Ping 1038646d4dcSBai Ping return NULL; 1048646d4dcSBai Ping } 1058646d4dcSBai Ping 10653990cf9SSascha Hauer static long pll14xx_calc_rate(struct clk_pll14xx *pll, int mdiv, int pdiv, 10753990cf9SSascha Hauer int sdiv, int kdiv, unsigned long prate) 10853990cf9SSascha Hauer { 10953990cf9SSascha Hauer u64 fvco = prate; 11053990cf9SSascha Hauer 11153990cf9SSascha Hauer /* fvco = (m * 65536 + k) * Fin / (p * 65536) */ 11253990cf9SSascha Hauer fvco *= (mdiv * 65536 + kdiv); 11353990cf9SSascha Hauer pdiv *= 65536; 11453990cf9SSascha Hauer 11553990cf9SSascha Hauer do_div(fvco, pdiv << sdiv); 11653990cf9SSascha Hauer 11753990cf9SSascha Hauer return fvco; 11853990cf9SSascha Hauer } 11953990cf9SSascha Hauer 120b09c68dcSSascha Hauer static long pll1443x_calc_kdiv(int mdiv, int pdiv, int sdiv, 121b09c68dcSSascha Hauer unsigned long rate, unsigned long prate) 122b09c68dcSSascha Hauer { 123b09c68dcSSascha Hauer long kdiv; 124b09c68dcSSascha Hauer 125b09c68dcSSascha Hauer /* calc kdiv = round(rate * pdiv * 65536 * 2^sdiv / prate) - (mdiv * 65536) */ 126b09c68dcSSascha Hauer kdiv = ((rate * ((pdiv * 65536) << sdiv) + prate / 2) / prate) - (mdiv * 65536); 127b09c68dcSSascha Hauer 128b09c68dcSSascha Hauer return clamp_t(short, kdiv, KDIV_MIN, KDIV_MAX); 129b09c68dcSSascha Hauer } 130b09c68dcSSascha Hauer 131b09c68dcSSascha Hauer static void imx_pll14xx_calc_settings(struct clk_pll14xx *pll, unsigned long rate, 132b09c68dcSSascha Hauer unsigned long prate, struct imx_pll14xx_rate_table *t) 133b09c68dcSSascha Hauer { 134b09c68dcSSascha Hauer u32 pll_div_ctl0, pll_div_ctl1; 135b09c68dcSSascha Hauer int mdiv, pdiv, sdiv, kdiv; 136b09c68dcSSascha Hauer long fvco, rate_min, rate_max, dist, best = LONG_MAX; 137b09c68dcSSascha Hauer const struct imx_pll14xx_rate_table *tt; 138b09c68dcSSascha Hauer 139b09c68dcSSascha Hauer /* 140b09c68dcSSascha Hauer * Fractional PLL constrains: 141b09c68dcSSascha Hauer * 142*37cfd5e4SMarco Felsch * a) 1 <= p <= 63 143*37cfd5e4SMarco Felsch * b) 64 <= m <= 1023 144*37cfd5e4SMarco Felsch * c) 0 <= s <= 6 145*37cfd5e4SMarco Felsch * d) -32768 <= k <= 32767 146b09c68dcSSascha Hauer * 147b09c68dcSSascha Hauer * fvco = (m * 65536 + k) * prate / (p * 65536) 148b09c68dcSSascha Hauer */ 149b09c68dcSSascha Hauer 150b09c68dcSSascha Hauer /* First try if we can get the desired rate from one of the static entries */ 151b09c68dcSSascha Hauer tt = imx_get_pll_settings(pll, rate); 152b09c68dcSSascha Hauer if (tt) { 153b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld, Using PLL setting from table\n", 154b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate); 155b09c68dcSSascha Hauer t->rate = tt->rate; 156b09c68dcSSascha Hauer t->mdiv = tt->mdiv; 157b09c68dcSSascha Hauer t->pdiv = tt->pdiv; 158b09c68dcSSascha Hauer t->sdiv = tt->sdiv; 159b09c68dcSSascha Hauer t->kdiv = tt->kdiv; 160b09c68dcSSascha Hauer return; 161b09c68dcSSascha Hauer } 162b09c68dcSSascha Hauer 163b09c68dcSSascha Hauer pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0); 164b09c68dcSSascha Hauer mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0); 165b09c68dcSSascha Hauer pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0); 166b09c68dcSSascha Hauer sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0); 167b09c68dcSSascha Hauer pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1); 168b09c68dcSSascha Hauer 169b09c68dcSSascha Hauer /* Then see if we can get the desired rate by only adjusting kdiv (glitch free) */ 170b09c68dcSSascha Hauer rate_min = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MIN, prate); 171b09c68dcSSascha Hauer rate_max = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MAX, prate); 172b09c68dcSSascha Hauer 173b09c68dcSSascha Hauer if (rate >= rate_min && rate <= rate_max) { 174b09c68dcSSascha Hauer kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate); 175b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld Only adjust kdiv %ld -> %d\n", 176b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate, 177b09c68dcSSascha Hauer FIELD_GET(KDIV_MASK, pll_div_ctl1), kdiv); 178b09c68dcSSascha Hauer fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate); 179b09c68dcSSascha Hauer t->rate = (unsigned int)fvco; 180b09c68dcSSascha Hauer t->mdiv = mdiv; 181b09c68dcSSascha Hauer t->pdiv = pdiv; 182b09c68dcSSascha Hauer t->sdiv = sdiv; 183b09c68dcSSascha Hauer t->kdiv = kdiv; 184b09c68dcSSascha Hauer return; 185b09c68dcSSascha Hauer } 186b09c68dcSSascha Hauer 187b09c68dcSSascha Hauer /* Finally calculate best values */ 188*37cfd5e4SMarco Felsch for (pdiv = 1; pdiv <= 63; pdiv++) { 189b09c68dcSSascha Hauer for (sdiv = 0; sdiv <= 6; sdiv++) { 190b09c68dcSSascha Hauer /* calc mdiv = round(rate * pdiv * 2^sdiv) / prate) */ 191b09c68dcSSascha Hauer mdiv = DIV_ROUND_CLOSEST(rate * (pdiv << sdiv), prate); 192b09c68dcSSascha Hauer mdiv = clamp(mdiv, 64, 1023); 193b09c68dcSSascha Hauer 194b09c68dcSSascha Hauer kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate); 195b09c68dcSSascha Hauer fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate); 196b09c68dcSSascha Hauer 197b09c68dcSSascha Hauer /* best match */ 198b09c68dcSSascha Hauer dist = abs((long)rate - (long)fvco); 199b09c68dcSSascha Hauer if (dist < best) { 200b09c68dcSSascha Hauer best = dist; 201b09c68dcSSascha Hauer t->rate = (unsigned int)fvco; 202b09c68dcSSascha Hauer t->mdiv = mdiv; 203b09c68dcSSascha Hauer t->pdiv = pdiv; 204b09c68dcSSascha Hauer t->sdiv = sdiv; 205b09c68dcSSascha Hauer t->kdiv = kdiv; 206b09c68dcSSascha Hauer 207b09c68dcSSascha Hauer if (!dist) 208b09c68dcSSascha Hauer goto found; 209b09c68dcSSascha Hauer } 210b09c68dcSSascha Hauer } 211b09c68dcSSascha Hauer } 212b09c68dcSSascha Hauer found: 213b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld got=%d (pdiv=%d sdiv=%d mdiv=%d kdiv=%d)\n", 214b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate, t->rate, t->pdiv, t->sdiv, 215b09c68dcSSascha Hauer t->mdiv, t->kdiv); 216b09c68dcSSascha Hauer } 217b09c68dcSSascha Hauer 218b09c68dcSSascha Hauer static long clk_pll1416x_round_rate(struct clk_hw *hw, unsigned long rate, 2198646d4dcSBai Ping unsigned long *prate) 2208646d4dcSBai Ping { 2218646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 2228646d4dcSBai Ping const struct imx_pll14xx_rate_table *rate_table = pll->rate_table; 2238646d4dcSBai Ping int i; 2248646d4dcSBai Ping 2255ff50031SSascha Hauer /* Assuming rate_table is in descending order */ 2268646d4dcSBai Ping for (i = 0; i < pll->rate_count; i++) 2278646d4dcSBai Ping if (rate >= rate_table[i].rate) 2288646d4dcSBai Ping return rate_table[i].rate; 2298646d4dcSBai Ping 2308646d4dcSBai Ping /* return minimum supported value */ 2315ff50031SSascha Hauer return rate_table[pll->rate_count - 1].rate; 2328646d4dcSBai Ping } 2338646d4dcSBai Ping 234b09c68dcSSascha Hauer static long clk_pll1443x_round_rate(struct clk_hw *hw, unsigned long rate, 235b09c68dcSSascha Hauer unsigned long *prate) 236b09c68dcSSascha Hauer { 237b09c68dcSSascha Hauer struct clk_pll14xx *pll = to_clk_pll14xx(hw); 238b09c68dcSSascha Hauer struct imx_pll14xx_rate_table t; 239b09c68dcSSascha Hauer 240b09c68dcSSascha Hauer imx_pll14xx_calc_settings(pll, rate, *prate, &t); 241b09c68dcSSascha Hauer 242b09c68dcSSascha Hauer return t.rate; 243b09c68dcSSascha Hauer } 244b09c68dcSSascha Hauer 24553990cf9SSascha Hauer static unsigned long clk_pll14xx_recalc_rate(struct clk_hw *hw, 2468646d4dcSBai Ping unsigned long parent_rate) 2478646d4dcSBai Ping { 2488646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 24953990cf9SSascha Hauer u32 mdiv, pdiv, sdiv, kdiv, pll_div_ctl0, pll_div_ctl1; 2508646d4dcSBai Ping 251485b4ff5SSascha Hauer pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0); 25258f4980cSSascha Hauer mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0); 25358f4980cSSascha Hauer pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0); 25458f4980cSSascha Hauer sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0); 25553990cf9SSascha Hauer 25653990cf9SSascha Hauer if (pll->type == PLL_1443X) { 25753990cf9SSascha Hauer pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1); 25802d7bd1eSKevin Groeneveld kdiv = (s16)FIELD_GET(KDIV_MASK, pll_div_ctl1); 25953990cf9SSascha Hauer } else { 26053990cf9SSascha Hauer kdiv = 0; 26153990cf9SSascha Hauer } 2628646d4dcSBai Ping 26353990cf9SSascha Hauer return pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, parent_rate); 2648646d4dcSBai Ping } 2658646d4dcSBai Ping 266094234fcSLeonard Crestez static inline bool clk_pll14xx_mp_change(const struct imx_pll14xx_rate_table *rate, 2678646d4dcSBai Ping u32 pll_div) 2688646d4dcSBai Ping { 2698646d4dcSBai Ping u32 old_mdiv, old_pdiv; 2708646d4dcSBai Ping 27158f4980cSSascha Hauer old_mdiv = FIELD_GET(MDIV_MASK, pll_div); 27258f4980cSSascha Hauer old_pdiv = FIELD_GET(PDIV_MASK, pll_div); 2738646d4dcSBai Ping 2748646d4dcSBai Ping return rate->mdiv != old_mdiv || rate->pdiv != old_pdiv; 2758646d4dcSBai Ping } 2768646d4dcSBai Ping 2778646d4dcSBai Ping static int clk_pll14xx_wait_lock(struct clk_pll14xx *pll) 2788646d4dcSBai Ping { 2798646d4dcSBai Ping u32 val; 2808646d4dcSBai Ping 281485b4ff5SSascha Hauer return readl_poll_timeout(pll->base + GNRL_CTL, val, val & LOCK_STATUS, 0, 2828646d4dcSBai Ping LOCK_TIMEOUT_US); 2838646d4dcSBai Ping } 2848646d4dcSBai Ping 2858646d4dcSBai Ping static int clk_pll1416x_set_rate(struct clk_hw *hw, unsigned long drate, 2868646d4dcSBai Ping unsigned long prate) 2878646d4dcSBai Ping { 2888646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 2898646d4dcSBai Ping const struct imx_pll14xx_rate_table *rate; 2908646d4dcSBai Ping u32 tmp, div_val; 2918646d4dcSBai Ping int ret; 2928646d4dcSBai Ping 2938646d4dcSBai Ping rate = imx_get_pll_settings(pll, drate); 2948646d4dcSBai Ping if (!rate) { 29580cbc806SSascha Hauer pr_err("Invalid rate %lu for pll clk %s\n", drate, 29680cbc806SSascha Hauer clk_hw_get_name(hw)); 2978646d4dcSBai Ping return -EINVAL; 2988646d4dcSBai Ping } 2998646d4dcSBai Ping 300485b4ff5SSascha Hauer tmp = readl_relaxed(pll->base + DIV_CTL0); 3018646d4dcSBai Ping 302094234fcSLeonard Crestez if (!clk_pll14xx_mp_change(rate, tmp)) { 303d77461a6SSascha Hauer tmp &= ~SDIV_MASK; 30458f4980cSSascha Hauer tmp |= FIELD_PREP(SDIV_MASK, rate->sdiv); 305485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + DIV_CTL0); 3068646d4dcSBai Ping 3078646d4dcSBai Ping return 0; 3088646d4dcSBai Ping } 3098646d4dcSBai Ping 3108646d4dcSBai Ping /* Bypass clock and set lock to pll output lock */ 311485b4ff5SSascha Hauer tmp = readl_relaxed(pll->base + GNRL_CTL); 3128646d4dcSBai Ping tmp |= LOCK_SEL_MASK; 313485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL); 3148646d4dcSBai Ping 3158646d4dcSBai Ping /* Enable RST */ 3168646d4dcSBai Ping tmp &= ~RST_MASK; 317485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL); 3188646d4dcSBai Ping 319dee1bc9cSPeng Fan /* Enable BYPASS */ 320dee1bc9cSPeng Fan tmp |= BYPASS_MASK; 321485b4ff5SSascha Hauer writel(tmp, pll->base + GNRL_CTL); 322dee1bc9cSPeng Fan 32358f4980cSSascha Hauer div_val = FIELD_PREP(MDIV_MASK, rate->mdiv) | FIELD_PREP(PDIV_MASK, rate->pdiv) | 32458f4980cSSascha Hauer FIELD_PREP(SDIV_MASK, rate->sdiv); 325485b4ff5SSascha Hauer writel_relaxed(div_val, pll->base + DIV_CTL0); 3268646d4dcSBai Ping 3278646d4dcSBai Ping /* 3288646d4dcSBai Ping * According to SPEC, t3 - t2 need to be greater than 3298646d4dcSBai Ping * 1us and 1/FREF, respectively. 3308646d4dcSBai Ping * FREF is FIN / Prediv, the prediv is [1, 63], so choose 3318646d4dcSBai Ping * 3us. 3328646d4dcSBai Ping */ 3338646d4dcSBai Ping udelay(3); 3348646d4dcSBai Ping 3358646d4dcSBai Ping /* Disable RST */ 3368646d4dcSBai Ping tmp |= RST_MASK; 337485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL); 3388646d4dcSBai Ping 3398646d4dcSBai Ping /* Wait Lock */ 3408646d4dcSBai Ping ret = clk_pll14xx_wait_lock(pll); 3418646d4dcSBai Ping if (ret) 3428646d4dcSBai Ping return ret; 3438646d4dcSBai Ping 3448646d4dcSBai Ping /* Bypass */ 3458646d4dcSBai Ping tmp &= ~BYPASS_MASK; 346485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL); 3478646d4dcSBai Ping 3488646d4dcSBai Ping return 0; 3498646d4dcSBai Ping } 3508646d4dcSBai Ping 3518646d4dcSBai Ping static int clk_pll1443x_set_rate(struct clk_hw *hw, unsigned long drate, 3528646d4dcSBai Ping unsigned long prate) 3538646d4dcSBai Ping { 3548646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 355b09c68dcSSascha Hauer struct imx_pll14xx_rate_table rate; 356052d03a0SSascha Hauer u32 gnrl_ctl, div_ctl0; 3578646d4dcSBai Ping int ret; 3588646d4dcSBai Ping 359b09c68dcSSascha Hauer imx_pll14xx_calc_settings(pll, drate, prate, &rate); 3608646d4dcSBai Ping 361052d03a0SSascha Hauer div_ctl0 = readl_relaxed(pll->base + DIV_CTL0); 3628646d4dcSBai Ping 363b09c68dcSSascha Hauer if (!clk_pll14xx_mp_change(&rate, div_ctl0)) { 364b09c68dcSSascha Hauer /* only sdiv and/or kdiv changed - no need to RESET PLL */ 365052d03a0SSascha Hauer div_ctl0 &= ~SDIV_MASK; 366b09c68dcSSascha Hauer div_ctl0 |= FIELD_PREP(SDIV_MASK, rate.sdiv); 367052d03a0SSascha Hauer writel_relaxed(div_ctl0, pll->base + DIV_CTL0); 3688646d4dcSBai Ping 369b09c68dcSSascha Hauer writel_relaxed(FIELD_PREP(KDIV_MASK, rate.kdiv), 370052d03a0SSascha Hauer pll->base + DIV_CTL1); 371094234fcSLeonard Crestez 3728646d4dcSBai Ping return 0; 3738646d4dcSBai Ping } 3748646d4dcSBai Ping 3758646d4dcSBai Ping /* Enable RST */ 376052d03a0SSascha Hauer gnrl_ctl = readl_relaxed(pll->base + GNRL_CTL); 377052d03a0SSascha Hauer gnrl_ctl &= ~RST_MASK; 378052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL); 3798646d4dcSBai Ping 380dee1bc9cSPeng Fan /* Enable BYPASS */ 381052d03a0SSascha Hauer gnrl_ctl |= BYPASS_MASK; 382052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL); 383dee1bc9cSPeng Fan 384b09c68dcSSascha Hauer div_ctl0 = FIELD_PREP(MDIV_MASK, rate.mdiv) | 385b09c68dcSSascha Hauer FIELD_PREP(PDIV_MASK, rate.pdiv) | 386b09c68dcSSascha Hauer FIELD_PREP(SDIV_MASK, rate.sdiv); 387052d03a0SSascha Hauer writel_relaxed(div_ctl0, pll->base + DIV_CTL0); 388b09c68dcSSascha Hauer 389b09c68dcSSascha Hauer writel_relaxed(FIELD_PREP(KDIV_MASK, rate.kdiv), pll->base + DIV_CTL1); 3908646d4dcSBai Ping 3918646d4dcSBai Ping /* 3928646d4dcSBai Ping * According to SPEC, t3 - t2 need to be greater than 3938646d4dcSBai Ping * 1us and 1/FREF, respectively. 3948646d4dcSBai Ping * FREF is FIN / Prediv, the prediv is [1, 63], so choose 3958646d4dcSBai Ping * 3us. 3968646d4dcSBai Ping */ 3978646d4dcSBai Ping udelay(3); 3988646d4dcSBai Ping 3998646d4dcSBai Ping /* Disable RST */ 400052d03a0SSascha Hauer gnrl_ctl |= RST_MASK; 401052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL); 4028646d4dcSBai Ping 4038646d4dcSBai Ping /* Wait Lock*/ 4048646d4dcSBai Ping ret = clk_pll14xx_wait_lock(pll); 4058646d4dcSBai Ping if (ret) 4068646d4dcSBai Ping return ret; 4078646d4dcSBai Ping 4088646d4dcSBai Ping /* Bypass */ 409052d03a0SSascha Hauer gnrl_ctl &= ~BYPASS_MASK; 410052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL); 4118646d4dcSBai Ping 4128646d4dcSBai Ping return 0; 4138646d4dcSBai Ping } 4148646d4dcSBai Ping 4158646d4dcSBai Ping static int clk_pll14xx_prepare(struct clk_hw *hw) 4168646d4dcSBai Ping { 4178646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 4188646d4dcSBai Ping u32 val; 419dee1bc9cSPeng Fan int ret; 4208646d4dcSBai Ping 4218646d4dcSBai Ping /* 4228646d4dcSBai Ping * RESETB = 1 from 0, PLL starts its normal 4238646d4dcSBai Ping * operation after lock time 4248646d4dcSBai Ping */ 4258646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL); 426dee1bc9cSPeng Fan if (val & RST_MASK) 427dee1bc9cSPeng Fan return 0; 428dee1bc9cSPeng Fan val |= BYPASS_MASK; 429dee1bc9cSPeng Fan writel_relaxed(val, pll->base + GNRL_CTL); 4308646d4dcSBai Ping val |= RST_MASK; 4318646d4dcSBai Ping writel_relaxed(val, pll->base + GNRL_CTL); 4328646d4dcSBai Ping 433dee1bc9cSPeng Fan ret = clk_pll14xx_wait_lock(pll); 434dee1bc9cSPeng Fan if (ret) 435dee1bc9cSPeng Fan return ret; 436dee1bc9cSPeng Fan 437dee1bc9cSPeng Fan val &= ~BYPASS_MASK; 438dee1bc9cSPeng Fan writel_relaxed(val, pll->base + GNRL_CTL); 439dee1bc9cSPeng Fan 440dee1bc9cSPeng Fan return 0; 4418646d4dcSBai Ping } 4428646d4dcSBai Ping 4438646d4dcSBai Ping static int clk_pll14xx_is_prepared(struct clk_hw *hw) 4448646d4dcSBai Ping { 4458646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 4468646d4dcSBai Ping u32 val; 4478646d4dcSBai Ping 4488646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL); 4498646d4dcSBai Ping 4508646d4dcSBai Ping return (val & RST_MASK) ? 1 : 0; 4518646d4dcSBai Ping } 4528646d4dcSBai Ping 4538646d4dcSBai Ping static void clk_pll14xx_unprepare(struct clk_hw *hw) 4548646d4dcSBai Ping { 4558646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 4568646d4dcSBai Ping u32 val; 4578646d4dcSBai Ping 4588646d4dcSBai Ping /* 4598646d4dcSBai Ping * Set RST to 0, power down mode is enabled and 4608646d4dcSBai Ping * every digital block is reset 4618646d4dcSBai Ping */ 4628646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL); 4638646d4dcSBai Ping val &= ~RST_MASK; 4648646d4dcSBai Ping writel_relaxed(val, pll->base + GNRL_CTL); 4658646d4dcSBai Ping } 4668646d4dcSBai Ping 4678646d4dcSBai Ping static const struct clk_ops clk_pll1416x_ops = { 4688646d4dcSBai Ping .prepare = clk_pll14xx_prepare, 4698646d4dcSBai Ping .unprepare = clk_pll14xx_unprepare, 4708646d4dcSBai Ping .is_prepared = clk_pll14xx_is_prepared, 47153990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate, 472b09c68dcSSascha Hauer .round_rate = clk_pll1416x_round_rate, 4738646d4dcSBai Ping .set_rate = clk_pll1416x_set_rate, 4748646d4dcSBai Ping }; 4758646d4dcSBai Ping 4768646d4dcSBai Ping static const struct clk_ops clk_pll1416x_min_ops = { 47753990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate, 4788646d4dcSBai Ping }; 4798646d4dcSBai Ping 4808646d4dcSBai Ping static const struct clk_ops clk_pll1443x_ops = { 4818646d4dcSBai Ping .prepare = clk_pll14xx_prepare, 4828646d4dcSBai Ping .unprepare = clk_pll14xx_unprepare, 4838646d4dcSBai Ping .is_prepared = clk_pll14xx_is_prepared, 48453990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate, 485b09c68dcSSascha Hauer .round_rate = clk_pll1443x_round_rate, 4868646d4dcSBai Ping .set_rate = clk_pll1443x_set_rate, 4878646d4dcSBai Ping }; 4888646d4dcSBai Ping 48955a8b3cdSAbel Vesa struct clk_hw *imx_dev_clk_hw_pll14xx(struct device *dev, const char *name, 49055a8b3cdSAbel Vesa const char *parent_name, void __iomem *base, 4918646d4dcSBai Ping const struct imx_pll14xx_clk *pll_clk) 4928646d4dcSBai Ping { 4938646d4dcSBai Ping struct clk_pll14xx *pll; 49410c34b50SPeng Fan struct clk_hw *hw; 4958646d4dcSBai Ping struct clk_init_data init; 49610c34b50SPeng Fan int ret; 497a9aa8306SPeng Fan u32 val; 4988646d4dcSBai Ping 4998646d4dcSBai Ping pll = kzalloc(sizeof(*pll), GFP_KERNEL); 5008646d4dcSBai Ping if (!pll) 5018646d4dcSBai Ping return ERR_PTR(-ENOMEM); 5028646d4dcSBai Ping 5038646d4dcSBai Ping init.name = name; 5048646d4dcSBai Ping init.flags = pll_clk->flags; 5058646d4dcSBai Ping init.parent_names = &parent_name; 5068646d4dcSBai Ping init.num_parents = 1; 5078646d4dcSBai Ping 5088646d4dcSBai Ping switch (pll_clk->type) { 5098646d4dcSBai Ping case PLL_1416X: 510f89b9e1bSLeonard Crestez if (!pll_clk->rate_table) 5118646d4dcSBai Ping init.ops = &clk_pll1416x_min_ops; 5128646d4dcSBai Ping else 5138646d4dcSBai Ping init.ops = &clk_pll1416x_ops; 5148646d4dcSBai Ping break; 5158646d4dcSBai Ping case PLL_1443X: 5168646d4dcSBai Ping init.ops = &clk_pll1443x_ops; 5178646d4dcSBai Ping break; 5188646d4dcSBai Ping default: 51980cbc806SSascha Hauer pr_err("Unknown pll type for pll clk %s\n", name); 520530cf8d4SAnson Huang kfree(pll); 521530cf8d4SAnson Huang return ERR_PTR(-EINVAL); 5228404c661STom Rix } 5238646d4dcSBai Ping 5248646d4dcSBai Ping pll->base = base; 5258646d4dcSBai Ping pll->hw.init = &init; 5268646d4dcSBai Ping pll->type = pll_clk->type; 5278646d4dcSBai Ping pll->rate_table = pll_clk->rate_table; 5288646d4dcSBai Ping pll->rate_count = pll_clk->rate_count; 5298646d4dcSBai Ping 530a9aa8306SPeng Fan val = readl_relaxed(pll->base + GNRL_CTL); 531a9aa8306SPeng Fan val &= ~BYPASS_MASK; 532a9aa8306SPeng Fan writel_relaxed(val, pll->base + GNRL_CTL); 533a9aa8306SPeng Fan 53410c34b50SPeng Fan hw = &pll->hw; 53510c34b50SPeng Fan 53655a8b3cdSAbel Vesa ret = clk_hw_register(dev, hw); 53710c34b50SPeng Fan if (ret) { 53880cbc806SSascha Hauer pr_err("failed to register pll %s %d\n", name, ret); 5398646d4dcSBai Ping kfree(pll); 54010c34b50SPeng Fan return ERR_PTR(ret); 5418646d4dcSBai Ping } 5428646d4dcSBai Ping 54310c34b50SPeng Fan return hw; 5448646d4dcSBai Ping } 545870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_dev_clk_hw_pll14xx); 546