16209624bSLucas Stach // SPDX-License-Identifier: GPL-2.0
26209624bSLucas Stach /*
36209624bSLucas Stach * Copyright 2018 NXP.
46209624bSLucas Stach *
56209624bSLucas Stach * This driver supports the fractional plls found in the imx8m SOCs
66209624bSLucas Stach *
76209624bSLucas Stach * Documentation for this fractional pll can be found at:
86209624bSLucas Stach * https://www.nxp.com/docs/en/reference-manual/IMX8MDQLQRM.pdf#page=834
96209624bSLucas Stach */
106209624bSLucas Stach
116209624bSLucas Stach #include <linux/clk-provider.h>
126209624bSLucas Stach #include <linux/err.h>
13*870ed5e2SAnson Huang #include <linux/export.h>
1462e59c4eSStephen Boyd #include <linux/io.h>
156209624bSLucas Stach #include <linux/iopoll.h>
166209624bSLucas Stach #include <linux/slab.h>
176209624bSLucas Stach #include <linux/bitfield.h>
186209624bSLucas Stach
196209624bSLucas Stach #include "clk.h"
206209624bSLucas Stach
216209624bSLucas Stach #define PLL_CFG0 0x0
226209624bSLucas Stach #define PLL_CFG1 0x4
236209624bSLucas Stach
246209624bSLucas Stach #define PLL_LOCK_STATUS BIT(31)
256209624bSLucas Stach #define PLL_PD_MASK BIT(19)
266209624bSLucas Stach #define PLL_BYPASS_MASK BIT(14)
276209624bSLucas Stach #define PLL_NEWDIV_VAL BIT(12)
286209624bSLucas Stach #define PLL_NEWDIV_ACK BIT(11)
296209624bSLucas Stach #define PLL_FRAC_DIV_MASK GENMASK(30, 7)
306209624bSLucas Stach #define PLL_INT_DIV_MASK GENMASK(6, 0)
316209624bSLucas Stach #define PLL_OUTPUT_DIV_MASK GENMASK(4, 0)
326209624bSLucas Stach #define PLL_FRAC_DENOM 0x1000000
336209624bSLucas Stach
346209624bSLucas Stach #define PLL_FRAC_LOCK_TIMEOUT 10000
356209624bSLucas Stach #define PLL_FRAC_ACK_TIMEOUT 500000
366209624bSLucas Stach
376209624bSLucas Stach struct clk_frac_pll {
386209624bSLucas Stach struct clk_hw hw;
396209624bSLucas Stach void __iomem *base;
406209624bSLucas Stach };
416209624bSLucas Stach
426209624bSLucas Stach #define to_clk_frac_pll(_hw) container_of(_hw, struct clk_frac_pll, hw)
436209624bSLucas Stach
clk_wait_lock(struct clk_frac_pll * pll)446209624bSLucas Stach static int clk_wait_lock(struct clk_frac_pll *pll)
456209624bSLucas Stach {
466209624bSLucas Stach u32 val;
476209624bSLucas Stach
486209624bSLucas Stach return readl_poll_timeout(pll->base, val, val & PLL_LOCK_STATUS, 0,
496209624bSLucas Stach PLL_FRAC_LOCK_TIMEOUT);
506209624bSLucas Stach }
516209624bSLucas Stach
clk_wait_ack(struct clk_frac_pll * pll)526209624bSLucas Stach static int clk_wait_ack(struct clk_frac_pll *pll)
536209624bSLucas Stach {
546209624bSLucas Stach u32 val;
556209624bSLucas Stach
566209624bSLucas Stach /* return directly if the pll is in powerdown or in bypass */
576209624bSLucas Stach if (readl_relaxed(pll->base) & (PLL_PD_MASK | PLL_BYPASS_MASK))
586209624bSLucas Stach return 0;
596209624bSLucas Stach
606209624bSLucas Stach /* Wait for the pll's divfi and divff to be reloaded */
616209624bSLucas Stach return readl_poll_timeout(pll->base, val, val & PLL_NEWDIV_ACK, 0,
626209624bSLucas Stach PLL_FRAC_ACK_TIMEOUT);
636209624bSLucas Stach }
646209624bSLucas Stach
clk_pll_prepare(struct clk_hw * hw)656209624bSLucas Stach static int clk_pll_prepare(struct clk_hw *hw)
666209624bSLucas Stach {
676209624bSLucas Stach struct clk_frac_pll *pll = to_clk_frac_pll(hw);
686209624bSLucas Stach u32 val;
696209624bSLucas Stach
706209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG0);
716209624bSLucas Stach val &= ~PLL_PD_MASK;
726209624bSLucas Stach writel_relaxed(val, pll->base + PLL_CFG0);
736209624bSLucas Stach
746209624bSLucas Stach return clk_wait_lock(pll);
756209624bSLucas Stach }
766209624bSLucas Stach
clk_pll_unprepare(struct clk_hw * hw)776209624bSLucas Stach static void clk_pll_unprepare(struct clk_hw *hw)
786209624bSLucas Stach {
796209624bSLucas Stach struct clk_frac_pll *pll = to_clk_frac_pll(hw);
806209624bSLucas Stach u32 val;
816209624bSLucas Stach
826209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG0);
836209624bSLucas Stach val |= PLL_PD_MASK;
846209624bSLucas Stach writel_relaxed(val, pll->base + PLL_CFG0);
856209624bSLucas Stach }
866209624bSLucas Stach
clk_pll_is_prepared(struct clk_hw * hw)876209624bSLucas Stach static int clk_pll_is_prepared(struct clk_hw *hw)
886209624bSLucas Stach {
896209624bSLucas Stach struct clk_frac_pll *pll = to_clk_frac_pll(hw);
906209624bSLucas Stach u32 val;
916209624bSLucas Stach
926209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG0);
936209624bSLucas Stach return (val & PLL_PD_MASK) ? 0 : 1;
946209624bSLucas Stach }
956209624bSLucas Stach
clk_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)966209624bSLucas Stach static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
976209624bSLucas Stach unsigned long parent_rate)
986209624bSLucas Stach {
996209624bSLucas Stach struct clk_frac_pll *pll = to_clk_frac_pll(hw);
1006209624bSLucas Stach u32 val, divff, divfi, divq;
1016209624bSLucas Stach u64 temp64 = parent_rate;
1026209624bSLucas Stach u64 rate;
1036209624bSLucas Stach
1046209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG0);
1056209624bSLucas Stach divq = (FIELD_GET(PLL_OUTPUT_DIV_MASK, val) + 1) * 2;
1066209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG1);
1076209624bSLucas Stach divff = FIELD_GET(PLL_FRAC_DIV_MASK, val);
1086209624bSLucas Stach divfi = FIELD_GET(PLL_INT_DIV_MASK, val);
1096209624bSLucas Stach
1106209624bSLucas Stach temp64 *= 8;
1116209624bSLucas Stach temp64 *= divff;
1126209624bSLucas Stach do_div(temp64, PLL_FRAC_DENOM);
1136209624bSLucas Stach do_div(temp64, divq);
1146209624bSLucas Stach
1156209624bSLucas Stach rate = parent_rate * 8 * (divfi + 1);
1166209624bSLucas Stach do_div(rate, divq);
1176209624bSLucas Stach rate += temp64;
1186209624bSLucas Stach
1196209624bSLucas Stach return rate;
1206209624bSLucas Stach }
1216209624bSLucas Stach
clk_pll_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)1226209624bSLucas Stach static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
1236209624bSLucas Stach unsigned long *prate)
1246209624bSLucas Stach {
1256209624bSLucas Stach u64 parent_rate = *prate;
1266209624bSLucas Stach u32 divff, divfi;
1276209624bSLucas Stach u64 temp64;
1286209624bSLucas Stach
1296209624bSLucas Stach parent_rate *= 8;
1306209624bSLucas Stach rate *= 2;
1316209624bSLucas Stach temp64 = rate;
1326209624bSLucas Stach do_div(temp64, parent_rate);
1336209624bSLucas Stach divfi = temp64;
1346209624bSLucas Stach temp64 = rate - divfi * parent_rate;
1356209624bSLucas Stach temp64 *= PLL_FRAC_DENOM;
1366209624bSLucas Stach do_div(temp64, parent_rate);
1376209624bSLucas Stach divff = temp64;
1386209624bSLucas Stach
1396209624bSLucas Stach temp64 = parent_rate;
1406209624bSLucas Stach temp64 *= divff;
1416209624bSLucas Stach do_div(temp64, PLL_FRAC_DENOM);
1426209624bSLucas Stach
1436209624bSLucas Stach rate = parent_rate * divfi + temp64;
1446209624bSLucas Stach
1456209624bSLucas Stach return rate / 2;
1466209624bSLucas Stach }
1476209624bSLucas Stach
1486209624bSLucas Stach /*
1496209624bSLucas Stach * To simplify the clock calculation, we can keep the 'PLL_OUTPUT_VAL' at zero
1506209624bSLucas Stach * (means the PLL output will be divided by 2). So the PLL output can use
1516209624bSLucas Stach * the below formula:
1526209624bSLucas Stach * pllout = parent_rate * 8 / 2 * DIVF_VAL;
1536209624bSLucas Stach * where DIVF_VAL = 1 + DIVFI + DIVFF / 2^24.
1546209624bSLucas Stach */
clk_pll_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)1556209624bSLucas Stach static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
1566209624bSLucas Stach unsigned long parent_rate)
1576209624bSLucas Stach {
1586209624bSLucas Stach struct clk_frac_pll *pll = to_clk_frac_pll(hw);
1596209624bSLucas Stach u32 val, divfi, divff;
160a64a9c08SAbel Vesa u64 temp64;
1616209624bSLucas Stach int ret;
1626209624bSLucas Stach
1636209624bSLucas Stach parent_rate *= 8;
1646209624bSLucas Stach rate *= 2;
1656209624bSLucas Stach divfi = rate / parent_rate;
166a64a9c08SAbel Vesa temp64 = parent_rate * divfi;
167a64a9c08SAbel Vesa temp64 = rate - temp64;
1686209624bSLucas Stach temp64 *= PLL_FRAC_DENOM;
1696209624bSLucas Stach do_div(temp64, parent_rate);
1706209624bSLucas Stach divff = temp64;
1716209624bSLucas Stach
1726209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG1);
1736209624bSLucas Stach val &= ~(PLL_FRAC_DIV_MASK | PLL_INT_DIV_MASK);
1746209624bSLucas Stach val |= (divff << 7) | (divfi - 1);
1756209624bSLucas Stach writel_relaxed(val, pll->base + PLL_CFG1);
1766209624bSLucas Stach
1776209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG0);
1786209624bSLucas Stach val &= ~0x1f;
1796209624bSLucas Stach writel_relaxed(val, pll->base + PLL_CFG0);
1806209624bSLucas Stach
1816209624bSLucas Stach /* Set the NEV_DIV_VAL to reload the DIVFI and DIVFF */
1826209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG0);
1836209624bSLucas Stach val |= PLL_NEWDIV_VAL;
1846209624bSLucas Stach writel_relaxed(val, pll->base + PLL_CFG0);
1856209624bSLucas Stach
1866209624bSLucas Stach ret = clk_wait_ack(pll);
1876209624bSLucas Stach
1886209624bSLucas Stach /* clear the NEV_DIV_VAL */
1896209624bSLucas Stach val = readl_relaxed(pll->base + PLL_CFG0);
1906209624bSLucas Stach val &= ~PLL_NEWDIV_VAL;
1916209624bSLucas Stach writel_relaxed(val, pll->base + PLL_CFG0);
1926209624bSLucas Stach
1936209624bSLucas Stach return ret;
1946209624bSLucas Stach }
1956209624bSLucas Stach
1966209624bSLucas Stach static const struct clk_ops clk_frac_pll_ops = {
1976209624bSLucas Stach .prepare = clk_pll_prepare,
1986209624bSLucas Stach .unprepare = clk_pll_unprepare,
1996209624bSLucas Stach .is_prepared = clk_pll_is_prepared,
2006209624bSLucas Stach .recalc_rate = clk_pll_recalc_rate,
2016209624bSLucas Stach .round_rate = clk_pll_round_rate,
2026209624bSLucas Stach .set_rate = clk_pll_set_rate,
2036209624bSLucas Stach };
2046209624bSLucas Stach
imx_clk_hw_frac_pll(const char * name,const char * parent_name,void __iomem * base)205179c1f7cSAbel Vesa struct clk_hw *imx_clk_hw_frac_pll(const char *name,
206179c1f7cSAbel Vesa const char *parent_name,
2076209624bSLucas Stach void __iomem *base)
2086209624bSLucas Stach {
2096209624bSLucas Stach struct clk_init_data init;
2106209624bSLucas Stach struct clk_frac_pll *pll;
2116209624bSLucas Stach struct clk_hw *hw;
2126209624bSLucas Stach int ret;
2136209624bSLucas Stach
2146209624bSLucas Stach pll = kzalloc(sizeof(*pll), GFP_KERNEL);
2156209624bSLucas Stach if (!pll)
2166209624bSLucas Stach return ERR_PTR(-ENOMEM);
2176209624bSLucas Stach
2186209624bSLucas Stach init.name = name;
2196209624bSLucas Stach init.ops = &clk_frac_pll_ops;
2206209624bSLucas Stach init.flags = 0;
2216209624bSLucas Stach init.parent_names = &parent_name;
2226209624bSLucas Stach init.num_parents = 1;
2236209624bSLucas Stach
2246209624bSLucas Stach pll->base = base;
2256209624bSLucas Stach pll->hw.init = &init;
2266209624bSLucas Stach
2276209624bSLucas Stach hw = &pll->hw;
2286209624bSLucas Stach
2296209624bSLucas Stach ret = clk_hw_register(NULL, hw);
2306209624bSLucas Stach if (ret) {
2316209624bSLucas Stach kfree(pll);
2326209624bSLucas Stach return ERR_PTR(ret);
2336209624bSLucas Stach }
2346209624bSLucas Stach
235179c1f7cSAbel Vesa return hw;
2366209624bSLucas Stach }
237*870ed5e2SAnson Huang EXPORT_SYMBOL_GPL(imx_clk_hw_frac_pll);
238