111f68120SShawn Guo #include <linux/kernel.h> 211f68120SShawn Guo #include <linux/clk.h> 311f68120SShawn Guo #include <linux/io.h> 411f68120SShawn Guo #include <linux/errno.h> 511f68120SShawn Guo #include <linux/delay.h> 611f68120SShawn Guo #include <linux/slab.h> 711f68120SShawn Guo #include <linux/err.h> 811f68120SShawn Guo 911f68120SShawn Guo #include <asm/div64.h> 1011f68120SShawn Guo 1111f68120SShawn Guo #include "clk.h" 1211f68120SShawn Guo 1311f68120SShawn Guo #define to_clk_pllv2(clk) (container_of(clk, struct clk_pllv2, clk)) 1411f68120SShawn Guo 1511f68120SShawn Guo /* PLL Register Offsets */ 1611f68120SShawn Guo #define MXC_PLL_DP_CTL 0x00 1711f68120SShawn Guo #define MXC_PLL_DP_CONFIG 0x04 1811f68120SShawn Guo #define MXC_PLL_DP_OP 0x08 1911f68120SShawn Guo #define MXC_PLL_DP_MFD 0x0C 2011f68120SShawn Guo #define MXC_PLL_DP_MFN 0x10 2111f68120SShawn Guo #define MXC_PLL_DP_MFNMINUS 0x14 2211f68120SShawn Guo #define MXC_PLL_DP_MFNPLUS 0x18 2311f68120SShawn Guo #define MXC_PLL_DP_HFS_OP 0x1C 2411f68120SShawn Guo #define MXC_PLL_DP_HFS_MFD 0x20 2511f68120SShawn Guo #define MXC_PLL_DP_HFS_MFN 0x24 2611f68120SShawn Guo #define MXC_PLL_DP_MFN_TOGC 0x28 2711f68120SShawn Guo #define MXC_PLL_DP_DESTAT 0x2c 2811f68120SShawn Guo 2911f68120SShawn Guo /* PLL Register Bit definitions */ 3011f68120SShawn Guo #define MXC_PLL_DP_CTL_MUL_CTRL 0x2000 3111f68120SShawn Guo #define MXC_PLL_DP_CTL_DPDCK0_2_EN 0x1000 3211f68120SShawn Guo #define MXC_PLL_DP_CTL_DPDCK0_2_OFFSET 12 3311f68120SShawn Guo #define MXC_PLL_DP_CTL_ADE 0x800 3411f68120SShawn Guo #define MXC_PLL_DP_CTL_REF_CLK_DIV 0x400 3511f68120SShawn Guo #define MXC_PLL_DP_CTL_REF_CLK_SEL_MASK (3 << 8) 3611f68120SShawn Guo #define MXC_PLL_DP_CTL_REF_CLK_SEL_OFFSET 8 3711f68120SShawn Guo #define MXC_PLL_DP_CTL_HFSM 0x80 3811f68120SShawn Guo #define MXC_PLL_DP_CTL_PRE 0x40 3911f68120SShawn Guo #define MXC_PLL_DP_CTL_UPEN 0x20 4011f68120SShawn Guo #define MXC_PLL_DP_CTL_RST 0x10 4111f68120SShawn Guo #define MXC_PLL_DP_CTL_RCP 0x8 4211f68120SShawn Guo #define MXC_PLL_DP_CTL_PLM 0x4 4311f68120SShawn Guo #define MXC_PLL_DP_CTL_BRM0 0x2 4411f68120SShawn Guo #define MXC_PLL_DP_CTL_LRF 0x1 4511f68120SShawn Guo 4611f68120SShawn Guo #define MXC_PLL_DP_CONFIG_BIST 0x8 4711f68120SShawn Guo #define MXC_PLL_DP_CONFIG_SJC_CE 0x4 4811f68120SShawn Guo #define MXC_PLL_DP_CONFIG_AREN 0x2 4911f68120SShawn Guo #define MXC_PLL_DP_CONFIG_LDREQ 0x1 5011f68120SShawn Guo 5111f68120SShawn Guo #define MXC_PLL_DP_OP_MFI_OFFSET 4 5211f68120SShawn Guo #define MXC_PLL_DP_OP_MFI_MASK (0xF << 4) 5311f68120SShawn Guo #define MXC_PLL_DP_OP_PDF_OFFSET 0 5411f68120SShawn Guo #define MXC_PLL_DP_OP_PDF_MASK 0xF 5511f68120SShawn Guo 5611f68120SShawn Guo #define MXC_PLL_DP_MFD_OFFSET 0 5711f68120SShawn Guo #define MXC_PLL_DP_MFD_MASK 0x07FFFFFF 5811f68120SShawn Guo 5911f68120SShawn Guo #define MXC_PLL_DP_MFN_OFFSET 0x0 6011f68120SShawn Guo #define MXC_PLL_DP_MFN_MASK 0x07FFFFFF 6111f68120SShawn Guo 6211f68120SShawn Guo #define MXC_PLL_DP_MFN_TOGC_TOG_DIS (1 << 17) 6311f68120SShawn Guo #define MXC_PLL_DP_MFN_TOGC_TOG_EN (1 << 16) 6411f68120SShawn Guo #define MXC_PLL_DP_MFN_TOGC_CNT_OFFSET 0x0 6511f68120SShawn Guo #define MXC_PLL_DP_MFN_TOGC_CNT_MASK 0xFFFF 6611f68120SShawn Guo 6711f68120SShawn Guo #define MXC_PLL_DP_DESTAT_TOG_SEL (1 << 31) 6811f68120SShawn Guo #define MXC_PLL_DP_DESTAT_MFN 0x07FFFFFF 6911f68120SShawn Guo 7011f68120SShawn Guo #define MAX_DPLL_WAIT_TRIES 1000 /* 1000 * udelay(1) = 1ms */ 7111f68120SShawn Guo 7211f68120SShawn Guo struct clk_pllv2 { 7311f68120SShawn Guo struct clk_hw hw; 7411f68120SShawn Guo void __iomem *base; 7511f68120SShawn Guo }; 7611f68120SShawn Guo 7711f68120SShawn Guo static unsigned long __clk_pllv2_recalc_rate(unsigned long parent_rate, 7811f68120SShawn Guo u32 dp_ctl, u32 dp_op, u32 dp_mfd, u32 dp_mfn) 7911f68120SShawn Guo { 80*53fdc8fdSMartin Kepplinger long mfi, mfn, mfd, pdf, ref_clk; 8111f68120SShawn Guo unsigned long dbl; 8211f68120SShawn Guo s64 temp; 8311f68120SShawn Guo 8411f68120SShawn Guo dbl = dp_ctl & MXC_PLL_DP_CTL_DPDCK0_2_EN; 8511f68120SShawn Guo 8611f68120SShawn Guo pdf = dp_op & MXC_PLL_DP_OP_PDF_MASK; 8711f68120SShawn Guo mfi = (dp_op & MXC_PLL_DP_OP_MFI_MASK) >> MXC_PLL_DP_OP_MFI_OFFSET; 8811f68120SShawn Guo mfi = (mfi <= 5) ? 5 : mfi; 8911f68120SShawn Guo mfd = dp_mfd & MXC_PLL_DP_MFD_MASK; 90*53fdc8fdSMartin Kepplinger mfn = dp_mfn & MXC_PLL_DP_MFN_MASK; 91*53fdc8fdSMartin Kepplinger mfn = sign_extend32(mfn, 26); 9211f68120SShawn Guo 9311f68120SShawn Guo ref_clk = 2 * parent_rate; 9411f68120SShawn Guo if (dbl != 0) 9511f68120SShawn Guo ref_clk *= 2; 9611f68120SShawn Guo 9711f68120SShawn Guo ref_clk /= (pdf + 1); 98*53fdc8fdSMartin Kepplinger temp = (u64) ref_clk * abs(mfn); 9911f68120SShawn Guo do_div(temp, mfd + 1); 10011f68120SShawn Guo if (mfn < 0) 10111f68120SShawn Guo temp = -temp; 10211f68120SShawn Guo temp = (ref_clk * mfi) + temp; 10311f68120SShawn Guo 10411f68120SShawn Guo return temp; 10511f68120SShawn Guo } 10611f68120SShawn Guo 10711f68120SShawn Guo static unsigned long clk_pllv2_recalc_rate(struct clk_hw *hw, 10811f68120SShawn Guo unsigned long parent_rate) 10911f68120SShawn Guo { 11011f68120SShawn Guo u32 dp_op, dp_mfd, dp_mfn, dp_ctl; 11111f68120SShawn Guo void __iomem *pllbase; 11211f68120SShawn Guo struct clk_pllv2 *pll = to_clk_pllv2(hw); 11311f68120SShawn Guo 11411f68120SShawn Guo pllbase = pll->base; 11511f68120SShawn Guo 11611f68120SShawn Guo dp_ctl = __raw_readl(pllbase + MXC_PLL_DP_CTL); 11711f68120SShawn Guo dp_op = __raw_readl(pllbase + MXC_PLL_DP_OP); 11811f68120SShawn Guo dp_mfd = __raw_readl(pllbase + MXC_PLL_DP_MFD); 11911f68120SShawn Guo dp_mfn = __raw_readl(pllbase + MXC_PLL_DP_MFN); 12011f68120SShawn Guo 12111f68120SShawn Guo return __clk_pllv2_recalc_rate(parent_rate, dp_ctl, dp_op, dp_mfd, dp_mfn); 12211f68120SShawn Guo } 12311f68120SShawn Guo 12411f68120SShawn Guo static int __clk_pllv2_set_rate(unsigned long rate, unsigned long parent_rate, 12511f68120SShawn Guo u32 *dp_op, u32 *dp_mfd, u32 *dp_mfn) 12611f68120SShawn Guo { 12711f68120SShawn Guo u32 reg; 12811f68120SShawn Guo long mfi, pdf, mfn, mfd = 999999; 12911f68120SShawn Guo s64 temp64; 13011f68120SShawn Guo unsigned long quad_parent_rate; 13111f68120SShawn Guo 13211f68120SShawn Guo quad_parent_rate = 4 * parent_rate; 13311f68120SShawn Guo pdf = mfi = -1; 13411f68120SShawn Guo while (++pdf < 16 && mfi < 5) 13511f68120SShawn Guo mfi = rate * (pdf+1) / quad_parent_rate; 13611f68120SShawn Guo if (mfi > 15) 13711f68120SShawn Guo return -EINVAL; 13811f68120SShawn Guo pdf--; 13911f68120SShawn Guo 14011f68120SShawn Guo temp64 = rate * (pdf + 1) - quad_parent_rate * mfi; 14111f68120SShawn Guo do_div(temp64, quad_parent_rate / 1000000); 14211f68120SShawn Guo mfn = (long)temp64; 14311f68120SShawn Guo 14411f68120SShawn Guo reg = mfi << 4 | pdf; 14511f68120SShawn Guo 14611f68120SShawn Guo *dp_op = reg; 14711f68120SShawn Guo *dp_mfd = mfd; 14811f68120SShawn Guo *dp_mfn = mfn; 14911f68120SShawn Guo 15011f68120SShawn Guo return 0; 15111f68120SShawn Guo } 15211f68120SShawn Guo 15311f68120SShawn Guo static int clk_pllv2_set_rate(struct clk_hw *hw, unsigned long rate, 15411f68120SShawn Guo unsigned long parent_rate) 15511f68120SShawn Guo { 15611f68120SShawn Guo struct clk_pllv2 *pll = to_clk_pllv2(hw); 15711f68120SShawn Guo void __iomem *pllbase; 15811f68120SShawn Guo u32 dp_ctl, dp_op, dp_mfd, dp_mfn; 15911f68120SShawn Guo int ret; 16011f68120SShawn Guo 16111f68120SShawn Guo pllbase = pll->base; 16211f68120SShawn Guo 16311f68120SShawn Guo 16411f68120SShawn Guo ret = __clk_pllv2_set_rate(rate, parent_rate, &dp_op, &dp_mfd, &dp_mfn); 16511f68120SShawn Guo if (ret) 16611f68120SShawn Guo return ret; 16711f68120SShawn Guo 16811f68120SShawn Guo dp_ctl = __raw_readl(pllbase + MXC_PLL_DP_CTL); 16911f68120SShawn Guo /* use dpdck0_2 */ 17011f68120SShawn Guo __raw_writel(dp_ctl | 0x1000L, pllbase + MXC_PLL_DP_CTL); 17111f68120SShawn Guo 17211f68120SShawn Guo __raw_writel(dp_op, pllbase + MXC_PLL_DP_OP); 17311f68120SShawn Guo __raw_writel(dp_mfd, pllbase + MXC_PLL_DP_MFD); 17411f68120SShawn Guo __raw_writel(dp_mfn, pllbase + MXC_PLL_DP_MFN); 17511f68120SShawn Guo 17611f68120SShawn Guo return 0; 17711f68120SShawn Guo } 17811f68120SShawn Guo 17911f68120SShawn Guo static long clk_pllv2_round_rate(struct clk_hw *hw, unsigned long rate, 18011f68120SShawn Guo unsigned long *prate) 18111f68120SShawn Guo { 18211f68120SShawn Guo u32 dp_op, dp_mfd, dp_mfn; 18311f68120SShawn Guo 18411f68120SShawn Guo __clk_pllv2_set_rate(rate, *prate, &dp_op, &dp_mfd, &dp_mfn); 18511f68120SShawn Guo return __clk_pllv2_recalc_rate(*prate, MXC_PLL_DP_CTL_DPDCK0_2_EN, 18611f68120SShawn Guo dp_op, dp_mfd, dp_mfn); 18711f68120SShawn Guo } 18811f68120SShawn Guo 18911f68120SShawn Guo static int clk_pllv2_prepare(struct clk_hw *hw) 19011f68120SShawn Guo { 19111f68120SShawn Guo struct clk_pllv2 *pll = to_clk_pllv2(hw); 19211f68120SShawn Guo u32 reg; 19311f68120SShawn Guo void __iomem *pllbase; 19411f68120SShawn Guo int i = 0; 19511f68120SShawn Guo 19611f68120SShawn Guo pllbase = pll->base; 19711f68120SShawn Guo reg = __raw_readl(pllbase + MXC_PLL_DP_CTL) | MXC_PLL_DP_CTL_UPEN; 19811f68120SShawn Guo __raw_writel(reg, pllbase + MXC_PLL_DP_CTL); 19911f68120SShawn Guo 20011f68120SShawn Guo /* Wait for lock */ 20111f68120SShawn Guo do { 20211f68120SShawn Guo reg = __raw_readl(pllbase + MXC_PLL_DP_CTL); 20311f68120SShawn Guo if (reg & MXC_PLL_DP_CTL_LRF) 20411f68120SShawn Guo break; 20511f68120SShawn Guo 20611f68120SShawn Guo udelay(1); 20711f68120SShawn Guo } while (++i < MAX_DPLL_WAIT_TRIES); 20811f68120SShawn Guo 20911f68120SShawn Guo if (i == MAX_DPLL_WAIT_TRIES) { 21011f68120SShawn Guo pr_err("MX5: pll locking failed\n"); 21111f68120SShawn Guo return -EINVAL; 21211f68120SShawn Guo } 21311f68120SShawn Guo 21411f68120SShawn Guo return 0; 21511f68120SShawn Guo } 21611f68120SShawn Guo 21711f68120SShawn Guo static void clk_pllv2_unprepare(struct clk_hw *hw) 21811f68120SShawn Guo { 21911f68120SShawn Guo struct clk_pllv2 *pll = to_clk_pllv2(hw); 22011f68120SShawn Guo u32 reg; 22111f68120SShawn Guo void __iomem *pllbase; 22211f68120SShawn Guo 22311f68120SShawn Guo pllbase = pll->base; 22411f68120SShawn Guo reg = __raw_readl(pllbase + MXC_PLL_DP_CTL) & ~MXC_PLL_DP_CTL_UPEN; 22511f68120SShawn Guo __raw_writel(reg, pllbase + MXC_PLL_DP_CTL); 22611f68120SShawn Guo } 22711f68120SShawn Guo 22811f68120SShawn Guo static struct clk_ops clk_pllv2_ops = { 22911f68120SShawn Guo .prepare = clk_pllv2_prepare, 23011f68120SShawn Guo .unprepare = clk_pllv2_unprepare, 23111f68120SShawn Guo .recalc_rate = clk_pllv2_recalc_rate, 23211f68120SShawn Guo .round_rate = clk_pllv2_round_rate, 23311f68120SShawn Guo .set_rate = clk_pllv2_set_rate, 23411f68120SShawn Guo }; 23511f68120SShawn Guo 23611f68120SShawn Guo struct clk *imx_clk_pllv2(const char *name, const char *parent, 23711f68120SShawn Guo void __iomem *base) 23811f68120SShawn Guo { 23911f68120SShawn Guo struct clk_pllv2 *pll; 24011f68120SShawn Guo struct clk *clk; 24111f68120SShawn Guo struct clk_init_data init; 24211f68120SShawn Guo 24311f68120SShawn Guo pll = kzalloc(sizeof(*pll), GFP_KERNEL); 24411f68120SShawn Guo if (!pll) 24511f68120SShawn Guo return ERR_PTR(-ENOMEM); 24611f68120SShawn Guo 24711f68120SShawn Guo pll->base = base; 24811f68120SShawn Guo 24911f68120SShawn Guo init.name = name; 25011f68120SShawn Guo init.ops = &clk_pllv2_ops; 25111f68120SShawn Guo init.flags = 0; 25211f68120SShawn Guo init.parent_names = &parent; 25311f68120SShawn Guo init.num_parents = 1; 25411f68120SShawn Guo 25511f68120SShawn Guo pll->hw.init = &init; 25611f68120SShawn Guo 25711f68120SShawn Guo clk = clk_register(NULL, &pll->hw); 25811f68120SShawn Guo if (IS_ERR(clk)) 25911f68120SShawn Guo kfree(pll); 26011f68120SShawn Guo 26111f68120SShawn Guo return clk; 26211f68120SShawn Guo } 263