blob: 1b0307195a6ed9913cfa0fe42cbf75bb9fab346d [file] [log] [blame]
Sascha Hauera547b812012-03-19 12:36:10 +01001#include <linux/kernel.h>
2#include <linux/clk.h>
3#include <linux/io.h>
4#include <linux/errno.h>
5#include <linux/delay.h>
6#include <linux/slab.h>
7#include <linux/err.h>
8
9#include <asm/div64.h>
10
11#include "clk.h"
12
13#define to_clk_pllv2(clk) (container_of(clk, struct clk_pllv2, clk))
14
15/* PLL Register Offsets */
16#define MXC_PLL_DP_CTL 0x00
17#define MXC_PLL_DP_CONFIG 0x04
18#define MXC_PLL_DP_OP 0x08
19#define MXC_PLL_DP_MFD 0x0C
20#define MXC_PLL_DP_MFN 0x10
21#define MXC_PLL_DP_MFNMINUS 0x14
22#define MXC_PLL_DP_MFNPLUS 0x18
23#define MXC_PLL_DP_HFS_OP 0x1C
24#define MXC_PLL_DP_HFS_MFD 0x20
25#define MXC_PLL_DP_HFS_MFN 0x24
26#define MXC_PLL_DP_MFN_TOGC 0x28
27#define MXC_PLL_DP_DESTAT 0x2c
28
29/* PLL Register Bit definitions */
30#define MXC_PLL_DP_CTL_MUL_CTRL 0x2000
31#define MXC_PLL_DP_CTL_DPDCK0_2_EN 0x1000
32#define MXC_PLL_DP_CTL_DPDCK0_2_OFFSET 12
33#define MXC_PLL_DP_CTL_ADE 0x800
34#define MXC_PLL_DP_CTL_REF_CLK_DIV 0x400
35#define MXC_PLL_DP_CTL_REF_CLK_SEL_MASK (3 << 8)
36#define MXC_PLL_DP_CTL_REF_CLK_SEL_OFFSET 8
37#define MXC_PLL_DP_CTL_HFSM 0x80
38#define MXC_PLL_DP_CTL_PRE 0x40
39#define MXC_PLL_DP_CTL_UPEN 0x20
40#define MXC_PLL_DP_CTL_RST 0x10
41#define MXC_PLL_DP_CTL_RCP 0x8
42#define MXC_PLL_DP_CTL_PLM 0x4
43#define MXC_PLL_DP_CTL_BRM0 0x2
44#define MXC_PLL_DP_CTL_LRF 0x1
45
46#define MXC_PLL_DP_CONFIG_BIST 0x8
47#define MXC_PLL_DP_CONFIG_SJC_CE 0x4
48#define MXC_PLL_DP_CONFIG_AREN 0x2
49#define MXC_PLL_DP_CONFIG_LDREQ 0x1
50
51#define MXC_PLL_DP_OP_MFI_OFFSET 4
52#define MXC_PLL_DP_OP_MFI_MASK (0xF << 4)
53#define MXC_PLL_DP_OP_PDF_OFFSET 0
54#define MXC_PLL_DP_OP_PDF_MASK 0xF
55
56#define MXC_PLL_DP_MFD_OFFSET 0
57#define MXC_PLL_DP_MFD_MASK 0x07FFFFFF
58
59#define MXC_PLL_DP_MFN_OFFSET 0x0
60#define MXC_PLL_DP_MFN_MASK 0x07FFFFFF
61
62#define MXC_PLL_DP_MFN_TOGC_TOG_DIS (1 << 17)
63#define MXC_PLL_DP_MFN_TOGC_TOG_EN (1 << 16)
64#define MXC_PLL_DP_MFN_TOGC_CNT_OFFSET 0x0
65#define MXC_PLL_DP_MFN_TOGC_CNT_MASK 0xFFFF
66
67#define MXC_PLL_DP_DESTAT_TOG_SEL (1 << 31)
68#define MXC_PLL_DP_DESTAT_MFN 0x07FFFFFF
69
70#define MAX_DPLL_WAIT_TRIES 1000 /* 1000 * udelay(1) = 1ms */
71
72struct clk_pllv2 {
73 struct clk_hw hw;
74 void __iomem *base;
75};
76
77static unsigned long clk_pllv2_recalc_rate(struct clk_hw *hw,
78 unsigned long parent_rate)
79{
80 long mfi, mfn, mfd, pdf, ref_clk, mfn_abs;
Sascha Hauer6cc90d62012-06-04 12:21:21 +020081 unsigned long dp_op, dp_mfd, dp_mfn, dp_ctl, dbl;
Sascha Hauera547b812012-03-19 12:36:10 +010082 void __iomem *pllbase;
83 s64 temp;
84 struct clk_pllv2 *pll = to_clk_pllv2(hw);
85
86 pllbase = pll->base;
87
88 dp_ctl = __raw_readl(pllbase + MXC_PLL_DP_CTL);
Sascha Hauera547b812012-03-19 12:36:10 +010089 dbl = dp_ctl & MXC_PLL_DP_CTL_DPDCK0_2_EN;
90
Sascha Hauer6cc90d62012-06-04 12:21:21 +020091 dp_op = __raw_readl(pllbase + MXC_PLL_DP_OP);
92 dp_mfd = __raw_readl(pllbase + MXC_PLL_DP_MFD);
93 dp_mfn = __raw_readl(pllbase + MXC_PLL_DP_MFN);
94
Sascha Hauera547b812012-03-19 12:36:10 +010095 pdf = dp_op & MXC_PLL_DP_OP_PDF_MASK;
96 mfi = (dp_op & MXC_PLL_DP_OP_MFI_MASK) >> MXC_PLL_DP_OP_MFI_OFFSET;
97 mfi = (mfi <= 5) ? 5 : mfi;
98 mfd = dp_mfd & MXC_PLL_DP_MFD_MASK;
99 mfn = mfn_abs = dp_mfn & MXC_PLL_DP_MFN_MASK;
100 /* Sign extend to 32-bits */
101 if (mfn >= 0x04000000) {
102 mfn |= 0xFC000000;
103 mfn_abs = -mfn;
104 }
105
106 ref_clk = 2 * parent_rate;
107 if (dbl != 0)
108 ref_clk *= 2;
109
110 ref_clk /= (pdf + 1);
111 temp = (u64) ref_clk * mfn_abs;
112 do_div(temp, mfd + 1);
113 if (mfn < 0)
114 temp = -temp;
115 temp = (ref_clk * mfi) + temp;
116
117 return temp;
118}
119
120static int clk_pllv2_set_rate(struct clk_hw *hw, unsigned long rate,
121 unsigned long parent_rate)
122{
123 struct clk_pllv2 *pll = to_clk_pllv2(hw);
124 u32 reg;
125 void __iomem *pllbase;
126 long mfi, pdf, mfn, mfd = 999999;
127 s64 temp64;
128 unsigned long quad_parent_rate;
Sascha Hauer6cc90d62012-06-04 12:21:21 +0200129 unsigned long dp_ctl;
Sascha Hauera547b812012-03-19 12:36:10 +0100130
131 pllbase = pll->base;
132
133 quad_parent_rate = 4 * parent_rate;
134 pdf = mfi = -1;
135 while (++pdf < 16 && mfi < 5)
136 mfi = rate * (pdf+1) / quad_parent_rate;
137 if (mfi > 15)
138 return -EINVAL;
139 pdf--;
140
141 temp64 = rate * (pdf+1) - quad_parent_rate * mfi;
142 do_div(temp64, quad_parent_rate/1000000);
143 mfn = (long)temp64;
144
145 dp_ctl = __raw_readl(pllbase + MXC_PLL_DP_CTL);
146 /* use dpdck0_2 */
147 __raw_writel(dp_ctl | 0x1000L, pllbase + MXC_PLL_DP_CTL);
Sascha Hauer6cc90d62012-06-04 12:21:21 +0200148
149 reg = mfi << 4 | pdf;
150 __raw_writel(reg, pllbase + MXC_PLL_DP_OP);
151 __raw_writel(mfd, pllbase + MXC_PLL_DP_MFD);
152 __raw_writel(mfn, pllbase + MXC_PLL_DP_MFN);
Sascha Hauera547b812012-03-19 12:36:10 +0100153
154 return 0;
155}
156
157static long clk_pllv2_round_rate(struct clk_hw *hw, unsigned long rate,
158 unsigned long *prate)
159{
160 return rate;
161}
162
163static int clk_pllv2_prepare(struct clk_hw *hw)
164{
165 struct clk_pllv2 *pll = to_clk_pllv2(hw);
166 u32 reg;
167 void __iomem *pllbase;
168 int i = 0;
169
170 pllbase = pll->base;
171 reg = __raw_readl(pllbase + MXC_PLL_DP_CTL) | MXC_PLL_DP_CTL_UPEN;
172 __raw_writel(reg, pllbase + MXC_PLL_DP_CTL);
173
174 /* Wait for lock */
175 do {
176 reg = __raw_readl(pllbase + MXC_PLL_DP_CTL);
177 if (reg & MXC_PLL_DP_CTL_LRF)
178 break;
179
180 udelay(1);
181 } while (++i < MAX_DPLL_WAIT_TRIES);
182
183 if (i == MAX_DPLL_WAIT_TRIES) {
184 pr_err("MX5: pll locking failed\n");
185 return -EINVAL;
186 }
187
188 return 0;
189}
190
191static void clk_pllv2_unprepare(struct clk_hw *hw)
192{
193 struct clk_pllv2 *pll = to_clk_pllv2(hw);
194 u32 reg;
195 void __iomem *pllbase;
196
197 pllbase = pll->base;
198 reg = __raw_readl(pllbase + MXC_PLL_DP_CTL) & ~MXC_PLL_DP_CTL_UPEN;
199 __raw_writel(reg, pllbase + MXC_PLL_DP_CTL);
200}
201
202struct clk_ops clk_pllv2_ops = {
203 .prepare = clk_pllv2_prepare,
204 .unprepare = clk_pllv2_unprepare,
205 .recalc_rate = clk_pllv2_recalc_rate,
206 .round_rate = clk_pllv2_round_rate,
207 .set_rate = clk_pllv2_set_rate,
208};
209
210struct clk *imx_clk_pllv2(const char *name, const char *parent,
211 void __iomem *base)
212{
213 struct clk_pllv2 *pll;
214 struct clk *clk;
215 struct clk_init_data init;
216
217 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
218 if (!pll)
219 return ERR_PTR(-ENOMEM);
220
221 pll->base = base;
222
223 init.name = name;
224 init.ops = &clk_pllv2_ops;
225 init.flags = 0;
226 init.parent_names = &parent;
227 init.num_parents = 1;
228
229 pll->hw.init = &init;
230
231 clk = clk_register(NULL, &pll->hw);
232 if (IS_ERR(clk))
233 kfree(pll);
234
235 return clk;
236}