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), 57*6a11d3a0SMarek Vasut PLL_1416X_RATE(640000000U, 320, 3, 2), 5843cdaa15SAnson Huang PLL_1416X_RATE(600000000U, 300, 3, 2), 59*6a11d3a0SMarek 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 * 142b09c68dcSSascha Hauer * a) 6MHz <= prate <= 25MHz 143b09c68dcSSascha Hauer * b) 1 <= p <= 63 (1 <= p <= 4 prate = 24MHz) 144b09c68dcSSascha Hauer * c) 64 <= m <= 1023 145b09c68dcSSascha Hauer * d) 0 <= s <= 6 146b09c68dcSSascha Hauer * e) -32768 <= k <= 32767 147b09c68dcSSascha Hauer * 148b09c68dcSSascha Hauer * fvco = (m * 65536 + k) * prate / (p * 65536) 149b09c68dcSSascha Hauer */ 150b09c68dcSSascha Hauer 151b09c68dcSSascha Hauer /* First try if we can get the desired rate from one of the static entries */ 152b09c68dcSSascha Hauer tt = imx_get_pll_settings(pll, rate); 153b09c68dcSSascha Hauer if (tt) { 154b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld, Using PLL setting from table\n", 155b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate); 156b09c68dcSSascha Hauer t->rate = tt->rate; 157b09c68dcSSascha Hauer t->mdiv = tt->mdiv; 158b09c68dcSSascha Hauer t->pdiv = tt->pdiv; 159b09c68dcSSascha Hauer t->sdiv = tt->sdiv; 160b09c68dcSSascha Hauer t->kdiv = tt->kdiv; 161b09c68dcSSascha Hauer return; 162b09c68dcSSascha Hauer } 163b09c68dcSSascha Hauer 164b09c68dcSSascha Hauer pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0); 165b09c68dcSSascha Hauer mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0); 166b09c68dcSSascha Hauer pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0); 167b09c68dcSSascha Hauer sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0); 168b09c68dcSSascha Hauer pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1); 169b09c68dcSSascha Hauer 170b09c68dcSSascha Hauer /* Then see if we can get the desired rate by only adjusting kdiv (glitch free) */ 171b09c68dcSSascha Hauer rate_min = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MIN, prate); 172b09c68dcSSascha Hauer rate_max = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, KDIV_MAX, prate); 173b09c68dcSSascha Hauer 174b09c68dcSSascha Hauer if (rate >= rate_min && rate <= rate_max) { 175b09c68dcSSascha Hauer kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate); 176b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld Only adjust kdiv %ld -> %d\n", 177b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate, 178b09c68dcSSascha Hauer FIELD_GET(KDIV_MASK, pll_div_ctl1), kdiv); 179b09c68dcSSascha Hauer fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate); 180b09c68dcSSascha Hauer t->rate = (unsigned int)fvco; 181b09c68dcSSascha Hauer t->mdiv = mdiv; 182b09c68dcSSascha Hauer t->pdiv = pdiv; 183b09c68dcSSascha Hauer t->sdiv = sdiv; 184b09c68dcSSascha Hauer t->kdiv = kdiv; 185b09c68dcSSascha Hauer return; 186b09c68dcSSascha Hauer } 187b09c68dcSSascha Hauer 188b09c68dcSSascha Hauer /* Finally calculate best values */ 189b09c68dcSSascha Hauer for (pdiv = 1; pdiv <= 7; pdiv++) { 190b09c68dcSSascha Hauer for (sdiv = 0; sdiv <= 6; sdiv++) { 191b09c68dcSSascha Hauer /* calc mdiv = round(rate * pdiv * 2^sdiv) / prate) */ 192b09c68dcSSascha Hauer mdiv = DIV_ROUND_CLOSEST(rate * (pdiv << sdiv), prate); 193b09c68dcSSascha Hauer mdiv = clamp(mdiv, 64, 1023); 194b09c68dcSSascha Hauer 195b09c68dcSSascha Hauer kdiv = pll1443x_calc_kdiv(mdiv, pdiv, sdiv, rate, prate); 196b09c68dcSSascha Hauer fvco = pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, prate); 197b09c68dcSSascha Hauer 198b09c68dcSSascha Hauer /* best match */ 199b09c68dcSSascha Hauer dist = abs((long)rate - (long)fvco); 200b09c68dcSSascha Hauer if (dist < best) { 201b09c68dcSSascha Hauer best = dist; 202b09c68dcSSascha Hauer t->rate = (unsigned int)fvco; 203b09c68dcSSascha Hauer t->mdiv = mdiv; 204b09c68dcSSascha Hauer t->pdiv = pdiv; 205b09c68dcSSascha Hauer t->sdiv = sdiv; 206b09c68dcSSascha Hauer t->kdiv = kdiv; 207b09c68dcSSascha Hauer 208b09c68dcSSascha Hauer if (!dist) 209b09c68dcSSascha Hauer goto found; 210b09c68dcSSascha Hauer } 211b09c68dcSSascha Hauer } 212b09c68dcSSascha Hauer } 213b09c68dcSSascha Hauer found: 214b09c68dcSSascha Hauer pr_debug("%s: in=%ld, want=%ld got=%d (pdiv=%d sdiv=%d mdiv=%d kdiv=%d)\n", 215b09c68dcSSascha Hauer clk_hw_get_name(&pll->hw), prate, rate, t->rate, t->pdiv, t->sdiv, 216b09c68dcSSascha Hauer t->mdiv, t->kdiv); 217b09c68dcSSascha Hauer } 218b09c68dcSSascha Hauer 219b09c68dcSSascha Hauer static long clk_pll1416x_round_rate(struct clk_hw *hw, unsigned long rate, 2208646d4dcSBai Ping unsigned long *prate) 2218646d4dcSBai Ping { 2228646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 2238646d4dcSBai Ping const struct imx_pll14xx_rate_table *rate_table = pll->rate_table; 2248646d4dcSBai Ping int i; 2258646d4dcSBai Ping 2265ff50031SSascha Hauer /* Assuming rate_table is in descending order */ 2278646d4dcSBai Ping for (i = 0; i < pll->rate_count; i++) 2288646d4dcSBai Ping if (rate >= rate_table[i].rate) 2298646d4dcSBai Ping return rate_table[i].rate; 2308646d4dcSBai Ping 2318646d4dcSBai Ping /* return minimum supported value */ 2325ff50031SSascha Hauer return rate_table[pll->rate_count - 1].rate; 2338646d4dcSBai Ping } 2348646d4dcSBai Ping 235b09c68dcSSascha Hauer static long clk_pll1443x_round_rate(struct clk_hw *hw, unsigned long rate, 236b09c68dcSSascha Hauer unsigned long *prate) 237b09c68dcSSascha Hauer { 238b09c68dcSSascha Hauer struct clk_pll14xx *pll = to_clk_pll14xx(hw); 239b09c68dcSSascha Hauer struct imx_pll14xx_rate_table t; 240b09c68dcSSascha Hauer 241b09c68dcSSascha Hauer imx_pll14xx_calc_settings(pll, rate, *prate, &t); 242b09c68dcSSascha Hauer 243b09c68dcSSascha Hauer return t.rate; 244b09c68dcSSascha Hauer } 245b09c68dcSSascha Hauer 24653990cf9SSascha Hauer static unsigned long clk_pll14xx_recalc_rate(struct clk_hw *hw, 2478646d4dcSBai Ping unsigned long parent_rate) 2488646d4dcSBai Ping { 2498646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 25053990cf9SSascha Hauer u32 mdiv, pdiv, sdiv, kdiv, pll_div_ctl0, pll_div_ctl1; 2518646d4dcSBai Ping 252485b4ff5SSascha Hauer pll_div_ctl0 = readl_relaxed(pll->base + DIV_CTL0); 25358f4980cSSascha Hauer mdiv = FIELD_GET(MDIV_MASK, pll_div_ctl0); 25458f4980cSSascha Hauer pdiv = FIELD_GET(PDIV_MASK, pll_div_ctl0); 25558f4980cSSascha Hauer sdiv = FIELD_GET(SDIV_MASK, pll_div_ctl0); 25653990cf9SSascha Hauer 25753990cf9SSascha Hauer if (pll->type == PLL_1443X) { 25853990cf9SSascha Hauer pll_div_ctl1 = readl_relaxed(pll->base + DIV_CTL1); 25958f4980cSSascha Hauer kdiv = FIELD_GET(KDIV_MASK, pll_div_ctl1); 26053990cf9SSascha Hauer } else { 26153990cf9SSascha Hauer kdiv = 0; 26253990cf9SSascha Hauer } 2638646d4dcSBai Ping 26453990cf9SSascha Hauer return pll14xx_calc_rate(pll, mdiv, pdiv, sdiv, kdiv, parent_rate); 2658646d4dcSBai Ping } 2668646d4dcSBai Ping 267094234fcSLeonard Crestez static inline bool clk_pll14xx_mp_change(const struct imx_pll14xx_rate_table *rate, 2688646d4dcSBai Ping u32 pll_div) 2698646d4dcSBai Ping { 2708646d4dcSBai Ping u32 old_mdiv, old_pdiv; 2718646d4dcSBai Ping 27258f4980cSSascha Hauer old_mdiv = FIELD_GET(MDIV_MASK, pll_div); 27358f4980cSSascha Hauer old_pdiv = FIELD_GET(PDIV_MASK, pll_div); 2748646d4dcSBai Ping 2758646d4dcSBai Ping return rate->mdiv != old_mdiv || rate->pdiv != old_pdiv; 2768646d4dcSBai Ping } 2778646d4dcSBai Ping 2788646d4dcSBai Ping static int clk_pll14xx_wait_lock(struct clk_pll14xx *pll) 2798646d4dcSBai Ping { 2808646d4dcSBai Ping u32 val; 2818646d4dcSBai Ping 282485b4ff5SSascha Hauer return readl_poll_timeout(pll->base + GNRL_CTL, val, val & LOCK_STATUS, 0, 2838646d4dcSBai Ping LOCK_TIMEOUT_US); 2848646d4dcSBai Ping } 2858646d4dcSBai Ping 2868646d4dcSBai Ping static int clk_pll1416x_set_rate(struct clk_hw *hw, unsigned long drate, 2878646d4dcSBai Ping unsigned long prate) 2888646d4dcSBai Ping { 2898646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 2908646d4dcSBai Ping const struct imx_pll14xx_rate_table *rate; 2918646d4dcSBai Ping u32 tmp, div_val; 2928646d4dcSBai Ping int ret; 2938646d4dcSBai Ping 2948646d4dcSBai Ping rate = imx_get_pll_settings(pll, drate); 2958646d4dcSBai Ping if (!rate) { 29680cbc806SSascha Hauer pr_err("Invalid rate %lu for pll clk %s\n", drate, 29780cbc806SSascha Hauer clk_hw_get_name(hw)); 2988646d4dcSBai Ping return -EINVAL; 2998646d4dcSBai Ping } 3008646d4dcSBai Ping 301485b4ff5SSascha Hauer tmp = readl_relaxed(pll->base + DIV_CTL0); 3028646d4dcSBai Ping 303094234fcSLeonard Crestez if (!clk_pll14xx_mp_change(rate, tmp)) { 304d77461a6SSascha Hauer tmp &= ~SDIV_MASK; 30558f4980cSSascha Hauer tmp |= FIELD_PREP(SDIV_MASK, rate->sdiv); 306485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + DIV_CTL0); 3078646d4dcSBai Ping 3088646d4dcSBai Ping return 0; 3098646d4dcSBai Ping } 3108646d4dcSBai Ping 3118646d4dcSBai Ping /* Bypass clock and set lock to pll output lock */ 312485b4ff5SSascha Hauer tmp = readl_relaxed(pll->base + GNRL_CTL); 3138646d4dcSBai Ping tmp |= LOCK_SEL_MASK; 314485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL); 3158646d4dcSBai Ping 3168646d4dcSBai Ping /* Enable RST */ 3178646d4dcSBai Ping tmp &= ~RST_MASK; 318485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL); 3198646d4dcSBai Ping 320dee1bc9cSPeng Fan /* Enable BYPASS */ 321dee1bc9cSPeng Fan tmp |= BYPASS_MASK; 322485b4ff5SSascha Hauer writel(tmp, pll->base + GNRL_CTL); 323dee1bc9cSPeng Fan 32458f4980cSSascha Hauer div_val = FIELD_PREP(MDIV_MASK, rate->mdiv) | FIELD_PREP(PDIV_MASK, rate->pdiv) | 32558f4980cSSascha Hauer FIELD_PREP(SDIV_MASK, rate->sdiv); 326485b4ff5SSascha Hauer writel_relaxed(div_val, pll->base + DIV_CTL0); 3278646d4dcSBai Ping 3288646d4dcSBai Ping /* 3298646d4dcSBai Ping * According to SPEC, t3 - t2 need to be greater than 3308646d4dcSBai Ping * 1us and 1/FREF, respectively. 3318646d4dcSBai Ping * FREF is FIN / Prediv, the prediv is [1, 63], so choose 3328646d4dcSBai Ping * 3us. 3338646d4dcSBai Ping */ 3348646d4dcSBai Ping udelay(3); 3358646d4dcSBai Ping 3368646d4dcSBai Ping /* Disable RST */ 3378646d4dcSBai Ping tmp |= RST_MASK; 338485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL); 3398646d4dcSBai Ping 3408646d4dcSBai Ping /* Wait Lock */ 3418646d4dcSBai Ping ret = clk_pll14xx_wait_lock(pll); 3428646d4dcSBai Ping if (ret) 3438646d4dcSBai Ping return ret; 3448646d4dcSBai Ping 3458646d4dcSBai Ping /* Bypass */ 3468646d4dcSBai Ping tmp &= ~BYPASS_MASK; 347485b4ff5SSascha Hauer writel_relaxed(tmp, pll->base + GNRL_CTL); 3488646d4dcSBai Ping 3498646d4dcSBai Ping return 0; 3508646d4dcSBai Ping } 3518646d4dcSBai Ping 3528646d4dcSBai Ping static int clk_pll1443x_set_rate(struct clk_hw *hw, unsigned long drate, 3538646d4dcSBai Ping unsigned long prate) 3548646d4dcSBai Ping { 3558646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 356b09c68dcSSascha Hauer struct imx_pll14xx_rate_table rate; 357052d03a0SSascha Hauer u32 gnrl_ctl, div_ctl0; 3588646d4dcSBai Ping int ret; 3598646d4dcSBai Ping 360b09c68dcSSascha Hauer imx_pll14xx_calc_settings(pll, drate, prate, &rate); 3618646d4dcSBai Ping 362052d03a0SSascha Hauer div_ctl0 = readl_relaxed(pll->base + DIV_CTL0); 3638646d4dcSBai Ping 364b09c68dcSSascha Hauer if (!clk_pll14xx_mp_change(&rate, div_ctl0)) { 365b09c68dcSSascha Hauer /* only sdiv and/or kdiv changed - no need to RESET PLL */ 366052d03a0SSascha Hauer div_ctl0 &= ~SDIV_MASK; 367b09c68dcSSascha Hauer div_ctl0 |= FIELD_PREP(SDIV_MASK, rate.sdiv); 368052d03a0SSascha Hauer writel_relaxed(div_ctl0, pll->base + DIV_CTL0); 3698646d4dcSBai Ping 370b09c68dcSSascha Hauer writel_relaxed(FIELD_PREP(KDIV_MASK, rate.kdiv), 371052d03a0SSascha Hauer pll->base + DIV_CTL1); 372094234fcSLeonard Crestez 3738646d4dcSBai Ping return 0; 3748646d4dcSBai Ping } 3758646d4dcSBai Ping 3768646d4dcSBai Ping /* Enable RST */ 377052d03a0SSascha Hauer gnrl_ctl = readl_relaxed(pll->base + GNRL_CTL); 378052d03a0SSascha Hauer gnrl_ctl &= ~RST_MASK; 379052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL); 3808646d4dcSBai Ping 381dee1bc9cSPeng Fan /* Enable BYPASS */ 382052d03a0SSascha Hauer gnrl_ctl |= BYPASS_MASK; 383052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL); 384dee1bc9cSPeng Fan 385b09c68dcSSascha Hauer div_ctl0 = FIELD_PREP(MDIV_MASK, rate.mdiv) | 386b09c68dcSSascha Hauer FIELD_PREP(PDIV_MASK, rate.pdiv) | 387b09c68dcSSascha Hauer FIELD_PREP(SDIV_MASK, rate.sdiv); 388052d03a0SSascha Hauer writel_relaxed(div_ctl0, pll->base + DIV_CTL0); 389b09c68dcSSascha Hauer 390b09c68dcSSascha Hauer writel_relaxed(FIELD_PREP(KDIV_MASK, rate.kdiv), pll->base + DIV_CTL1); 3918646d4dcSBai Ping 3928646d4dcSBai Ping /* 3938646d4dcSBai Ping * According to SPEC, t3 - t2 need to be greater than 3948646d4dcSBai Ping * 1us and 1/FREF, respectively. 3958646d4dcSBai Ping * FREF is FIN / Prediv, the prediv is [1, 63], so choose 3968646d4dcSBai Ping * 3us. 3978646d4dcSBai Ping */ 3988646d4dcSBai Ping udelay(3); 3998646d4dcSBai Ping 4008646d4dcSBai Ping /* Disable RST */ 401052d03a0SSascha Hauer gnrl_ctl |= RST_MASK; 402052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL); 4038646d4dcSBai Ping 4048646d4dcSBai Ping /* Wait Lock*/ 4058646d4dcSBai Ping ret = clk_pll14xx_wait_lock(pll); 4068646d4dcSBai Ping if (ret) 4078646d4dcSBai Ping return ret; 4088646d4dcSBai Ping 4098646d4dcSBai Ping /* Bypass */ 410052d03a0SSascha Hauer gnrl_ctl &= ~BYPASS_MASK; 411052d03a0SSascha Hauer writel_relaxed(gnrl_ctl, pll->base + GNRL_CTL); 4128646d4dcSBai Ping 4138646d4dcSBai Ping return 0; 4148646d4dcSBai Ping } 4158646d4dcSBai Ping 4168646d4dcSBai Ping static int clk_pll14xx_prepare(struct clk_hw *hw) 4178646d4dcSBai Ping { 4188646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 4198646d4dcSBai Ping u32 val; 420dee1bc9cSPeng Fan int ret; 4218646d4dcSBai Ping 4228646d4dcSBai Ping /* 4238646d4dcSBai Ping * RESETB = 1 from 0, PLL starts its normal 4248646d4dcSBai Ping * operation after lock time 4258646d4dcSBai Ping */ 4268646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL); 427dee1bc9cSPeng Fan if (val & RST_MASK) 428dee1bc9cSPeng Fan return 0; 429dee1bc9cSPeng Fan val |= BYPASS_MASK; 430dee1bc9cSPeng Fan writel_relaxed(val, pll->base + GNRL_CTL); 4318646d4dcSBai Ping val |= RST_MASK; 4328646d4dcSBai Ping writel_relaxed(val, pll->base + GNRL_CTL); 4338646d4dcSBai Ping 434dee1bc9cSPeng Fan ret = clk_pll14xx_wait_lock(pll); 435dee1bc9cSPeng Fan if (ret) 436dee1bc9cSPeng Fan return ret; 437dee1bc9cSPeng Fan 438dee1bc9cSPeng Fan val &= ~BYPASS_MASK; 439dee1bc9cSPeng Fan writel_relaxed(val, pll->base + GNRL_CTL); 440dee1bc9cSPeng Fan 441dee1bc9cSPeng Fan return 0; 4428646d4dcSBai Ping } 4438646d4dcSBai Ping 4448646d4dcSBai Ping static int clk_pll14xx_is_prepared(struct clk_hw *hw) 4458646d4dcSBai Ping { 4468646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 4478646d4dcSBai Ping u32 val; 4488646d4dcSBai Ping 4498646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL); 4508646d4dcSBai Ping 4518646d4dcSBai Ping return (val & RST_MASK) ? 1 : 0; 4528646d4dcSBai Ping } 4538646d4dcSBai Ping 4548646d4dcSBai Ping static void clk_pll14xx_unprepare(struct clk_hw *hw) 4558646d4dcSBai Ping { 4568646d4dcSBai Ping struct clk_pll14xx *pll = to_clk_pll14xx(hw); 4578646d4dcSBai Ping u32 val; 4588646d4dcSBai Ping 4598646d4dcSBai Ping /* 4608646d4dcSBai Ping * Set RST to 0, power down mode is enabled and 4618646d4dcSBai Ping * every digital block is reset 4628646d4dcSBai Ping */ 4638646d4dcSBai Ping val = readl_relaxed(pll->base + GNRL_CTL); 4648646d4dcSBai Ping val &= ~RST_MASK; 4658646d4dcSBai Ping writel_relaxed(val, pll->base + GNRL_CTL); 4668646d4dcSBai Ping } 4678646d4dcSBai Ping 4688646d4dcSBai Ping static const struct clk_ops clk_pll1416x_ops = { 4698646d4dcSBai Ping .prepare = clk_pll14xx_prepare, 4708646d4dcSBai Ping .unprepare = clk_pll14xx_unprepare, 4718646d4dcSBai Ping .is_prepared = clk_pll14xx_is_prepared, 47253990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate, 473b09c68dcSSascha Hauer .round_rate = clk_pll1416x_round_rate, 4748646d4dcSBai Ping .set_rate = clk_pll1416x_set_rate, 4758646d4dcSBai Ping }; 4768646d4dcSBai Ping 4778646d4dcSBai Ping static const struct clk_ops clk_pll1416x_min_ops = { 47853990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate, 4798646d4dcSBai Ping }; 4808646d4dcSBai Ping 4818646d4dcSBai Ping static const struct clk_ops clk_pll1443x_ops = { 4828646d4dcSBai Ping .prepare = clk_pll14xx_prepare, 4838646d4dcSBai Ping .unprepare = clk_pll14xx_unprepare, 4848646d4dcSBai Ping .is_prepared = clk_pll14xx_is_prepared, 48553990cf9SSascha Hauer .recalc_rate = clk_pll14xx_recalc_rate, 486b09c68dcSSascha Hauer .round_rate = clk_pll1443x_round_rate, 4878646d4dcSBai Ping .set_rate = clk_pll1443x_set_rate, 4888646d4dcSBai Ping }; 4898646d4dcSBai Ping 49055a8b3cdSAbel Vesa struct clk_hw *imx_dev_clk_hw_pll14xx(struct device *dev, const char *name, 49155a8b3cdSAbel Vesa const char *parent_name, void __iomem *base, 4928646d4dcSBai Ping const struct imx_pll14xx_clk *pll_clk) 4938646d4dcSBai Ping { 4948646d4dcSBai Ping struct clk_pll14xx *pll; 49510c34b50SPeng Fan struct clk_hw *hw; 4968646d4dcSBai Ping struct clk_init_data init; 49710c34b50SPeng Fan int ret; 498a9aa8306SPeng Fan u32 val; 4998646d4dcSBai Ping 5008646d4dcSBai Ping pll = kzalloc(sizeof(*pll), GFP_KERNEL); 5018646d4dcSBai Ping if (!pll) 5028646d4dcSBai Ping return ERR_PTR(-ENOMEM); 5038646d4dcSBai Ping 5048646d4dcSBai Ping init.name = name; 5058646d4dcSBai Ping init.flags = pll_clk->flags; 5068646d4dcSBai Ping init.parent_names = &parent_name; 5078646d4dcSBai Ping init.num_parents = 1; 5088646d4dcSBai Ping 5098646d4dcSBai Ping switch (pll_clk->type) { 5108646d4dcSBai Ping case PLL_1416X: 511f89b9e1bSLeonard Crestez if (!pll_clk->rate_table) 5128646d4dcSBai Ping init.ops = &clk_pll1416x_min_ops; 5138646d4dcSBai Ping else 5148646d4dcSBai Ping init.ops = &clk_pll1416x_ops; 5158646d4dcSBai Ping break; 5168646d4dcSBai Ping case PLL_1443X: 5178646d4dcSBai Ping init.ops = &clk_pll1443x_ops; 5188646d4dcSBai Ping break; 5198646d4dcSBai Ping default: 52080cbc806SSascha Hauer pr_err("Unknown pll type for pll clk %s\n", name); 521530cf8d4SAnson Huang kfree(pll); 522530cf8d4SAnson Huang return ERR_PTR(-EINVAL); 5238404c661STom Rix } 5248646d4dcSBai Ping 5258646d4dcSBai Ping pll->base = base; 5268646d4dcSBai Ping pll->hw.init = &init; 5278646d4dcSBai Ping pll->type = pll_clk->type; 5288646d4dcSBai Ping pll->rate_table = pll_clk->rate_table; 5298646d4dcSBai Ping pll->rate_count = pll_clk->rate_count; 5308646d4dcSBai Ping 531a9aa8306SPeng Fan val = readl_relaxed(pll->base + GNRL_CTL); 532a9aa8306SPeng Fan val &= ~BYPASS_MASK; 533a9aa8306SPeng Fan writel_relaxed(val, pll->base + GNRL_CTL); 534a9aa8306SPeng Fan 53510c34b50SPeng Fan hw = &pll->hw; 53610c34b50SPeng Fan 53755a8b3cdSAbel Vesa ret = clk_hw_register(dev, hw); 53810c34b50SPeng Fan if (ret) { 53980cbc806SSascha Hauer pr_err("failed to register pll %s %d\n", name, ret); 5408646d4dcSBai Ping kfree(pll); 54110c34b50SPeng Fan return ERR_PTR(ret); 5428646d4dcSBai Ping } 5438646d4dcSBai Ping 54410c34b50SPeng Fan return hw; 5458646d4dcSBai Ping } 546870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_dev_clk_hw_pll14xx); 547