blob: b7cf98f36fd5af012b6729ac2f743c8641d89346 [file] [log] [blame]
Marek Vasutc140e982011-11-08 23:18:08 +00001/*
2 * Freescale i.MX28 clock setup code
3 *
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
6 *
7 * Based on code from LTIB:
8 * Copyright (C) 2010 Freescale Semiconductor, Inc.
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29#include <common.h>
30#include <asm/errno.h>
31#include <asm/io.h>
32#include <asm/arch/clock.h>
33#include <asm/arch/imx-regs.h>
34
35/* The PLL frequency is always 480MHz, see section 10.2 in iMX28 datasheet. */
36#define PLL_FREQ_KHZ 480000
37#define PLL_FREQ_COEF 18
38/* The XTAL frequency is always 24MHz, see section 10.2 in iMX28 datasheet. */
39#define XTAL_FREQ_KHZ 24000
40
41#define PLL_FREQ_MHZ (PLL_FREQ_KHZ / 1000)
42#define XTAL_FREQ_MHZ (XTAL_FREQ_KHZ / 1000)
43
Otavio Salvador2906f942013-01-11 03:19:03 +000044static uint32_t mxs_get_pclk(void)
Marek Vasutc140e982011-11-08 23:18:08 +000045{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000046 struct mxs_clkctrl_regs *clkctrl_regs =
47 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +000048
Robert Deliendb5512c2012-02-26 12:15:07 +000049 uint32_t clkctrl, clkseq, div;
50 uint8_t clkfrac, frac;
Marek Vasutc140e982011-11-08 23:18:08 +000051
52 clkctrl = readl(&clkctrl_regs->hw_clkctrl_cpu);
53
54 /* No support of fractional divider calculation */
55 if (clkctrl &
56 (CLKCTRL_CPU_DIV_XTAL_FRAC_EN | CLKCTRL_CPU_DIV_CPU_FRAC_EN)) {
57 return 0;
58 }
59
60 clkseq = readl(&clkctrl_regs->hw_clkctrl_clkseq);
61
62 /* XTAL Path */
63 if (clkseq & CLKCTRL_CLKSEQ_BYPASS_CPU) {
64 div = (clkctrl & CLKCTRL_CPU_DIV_XTAL_MASK) >>
65 CLKCTRL_CPU_DIV_XTAL_OFFSET;
66 return XTAL_FREQ_MHZ / div;
67 }
68
69 /* REF Path */
Robert Deliendb5512c2012-02-26 12:15:07 +000070 clkfrac = readb(&clkctrl_regs->hw_clkctrl_frac0[CLKCTRL_FRAC0_CPU]);
71 frac = clkfrac & CLKCTRL_FRAC_FRAC_MASK;
Marek Vasutc140e982011-11-08 23:18:08 +000072 div = clkctrl & CLKCTRL_CPU_DIV_CPU_MASK;
73 return (PLL_FREQ_MHZ * PLL_FREQ_COEF / frac) / div;
74}
75
Otavio Salvador2906f942013-01-11 03:19:03 +000076static uint32_t mxs_get_hclk(void)
Marek Vasutc140e982011-11-08 23:18:08 +000077{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000078 struct mxs_clkctrl_regs *clkctrl_regs =
79 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +000080
81 uint32_t div;
82 uint32_t clkctrl;
83
84 clkctrl = readl(&clkctrl_regs->hw_clkctrl_hbus);
85
86 /* No support of fractional divider calculation */
87 if (clkctrl & CLKCTRL_HBUS_DIV_FRAC_EN)
88 return 0;
89
90 div = clkctrl & CLKCTRL_HBUS_DIV_MASK;
Otavio Salvador2906f942013-01-11 03:19:03 +000091 return mxs_get_pclk() / div;
Marek Vasutc140e982011-11-08 23:18:08 +000092}
93
Otavio Salvador2906f942013-01-11 03:19:03 +000094static uint32_t mxs_get_emiclk(void)
Marek Vasutc140e982011-11-08 23:18:08 +000095{
Otavio Salvador22f4ff92012-08-05 09:05:31 +000096 struct mxs_clkctrl_regs *clkctrl_regs =
97 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +000098
Robert Deliendb5512c2012-02-26 12:15:07 +000099 uint32_t clkctrl, clkseq, div;
100 uint8_t clkfrac, frac;
Marek Vasutc140e982011-11-08 23:18:08 +0000101
102 clkseq = readl(&clkctrl_regs->hw_clkctrl_clkseq);
103 clkctrl = readl(&clkctrl_regs->hw_clkctrl_emi);
104
105 /* XTAL Path */
106 if (clkseq & CLKCTRL_CLKSEQ_BYPASS_EMI) {
107 div = (clkctrl & CLKCTRL_EMI_DIV_XTAL_MASK) >>
108 CLKCTRL_EMI_DIV_XTAL_OFFSET;
109 return XTAL_FREQ_MHZ / div;
110 }
111
Marek Vasutc140e982011-11-08 23:18:08 +0000112 /* REF Path */
Robert Deliendb5512c2012-02-26 12:15:07 +0000113 clkfrac = readb(&clkctrl_regs->hw_clkctrl_frac0[CLKCTRL_FRAC0_EMI]);
114 frac = clkfrac & CLKCTRL_FRAC_FRAC_MASK;
Marek Vasutc140e982011-11-08 23:18:08 +0000115 div = clkctrl & CLKCTRL_EMI_DIV_EMI_MASK;
116 return (PLL_FREQ_MHZ * PLL_FREQ_COEF / frac) / div;
117}
118
Otavio Salvador2906f942013-01-11 03:19:03 +0000119static uint32_t mxs_get_gpmiclk(void)
Marek Vasutc140e982011-11-08 23:18:08 +0000120{
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000121 struct mxs_clkctrl_regs *clkctrl_regs =
122 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +0000123
Robert Deliendb5512c2012-02-26 12:15:07 +0000124 uint32_t clkctrl, clkseq, div;
125 uint8_t clkfrac, frac;
Marek Vasutc140e982011-11-08 23:18:08 +0000126
127 clkseq = readl(&clkctrl_regs->hw_clkctrl_clkseq);
128 clkctrl = readl(&clkctrl_regs->hw_clkctrl_gpmi);
129
130 /* XTAL Path */
131 if (clkseq & CLKCTRL_CLKSEQ_BYPASS_GPMI) {
132 div = clkctrl & CLKCTRL_GPMI_DIV_MASK;
133 return XTAL_FREQ_MHZ / div;
134 }
135
Marek Vasutc140e982011-11-08 23:18:08 +0000136 /* REF Path */
Robert Deliendb5512c2012-02-26 12:15:07 +0000137 clkfrac = readb(&clkctrl_regs->hw_clkctrl_frac1[CLKCTRL_FRAC1_GPMI]);
138 frac = clkfrac & CLKCTRL_FRAC_FRAC_MASK;
Marek Vasutc140e982011-11-08 23:18:08 +0000139 div = clkctrl & CLKCTRL_GPMI_DIV_MASK;
140 return (PLL_FREQ_MHZ * PLL_FREQ_COEF / frac) / div;
141}
142
143/*
144 * Set IO clock frequency, in kHz
145 */
Otavio Salvador2906f942013-01-11 03:19:03 +0000146void mxs_set_ioclk(enum mxs_ioclock io, uint32_t freq)
Marek Vasutc140e982011-11-08 23:18:08 +0000147{
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000148 struct mxs_clkctrl_regs *clkctrl_regs =
149 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +0000150 uint32_t div;
Robert Deliendb5512c2012-02-26 12:15:07 +0000151 int io_reg;
Marek Vasutc140e982011-11-08 23:18:08 +0000152
153 if (freq == 0)
154 return;
155
Robert Deliendb5512c2012-02-26 12:15:07 +0000156 if ((io < MXC_IOCLK0) || (io > MXC_IOCLK1))
Marek Vasutc140e982011-11-08 23:18:08 +0000157 return;
158
159 div = (PLL_FREQ_KHZ * PLL_FREQ_COEF) / freq;
160
161 if (div < 18)
162 div = 18;
163
164 if (div > 35)
165 div = 35;
166
Robert Deliendb5512c2012-02-26 12:15:07 +0000167 io_reg = CLKCTRL_FRAC0_IO0 - io; /* Register order is reversed */
168 writeb(CLKCTRL_FRAC_CLKGATE,
169 &clkctrl_regs->hw_clkctrl_frac0_set[io_reg]);
170 writeb(CLKCTRL_FRAC_CLKGATE | (div & CLKCTRL_FRAC_FRAC_MASK),
171 &clkctrl_regs->hw_clkctrl_frac0[io_reg]);
172 writeb(CLKCTRL_FRAC_CLKGATE,
173 &clkctrl_regs->hw_clkctrl_frac0_clr[io_reg]);
Marek Vasutc140e982011-11-08 23:18:08 +0000174}
175
176/*
177 * Get IO clock, returns IO clock in kHz
178 */
Otavio Salvador2906f942013-01-11 03:19:03 +0000179static uint32_t mxs_get_ioclk(enum mxs_ioclock io)
Marek Vasutc140e982011-11-08 23:18:08 +0000180{
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000181 struct mxs_clkctrl_regs *clkctrl_regs =
182 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Robert Deliendb5512c2012-02-26 12:15:07 +0000183 uint8_t ret;
184 int io_reg;
Marek Vasutc140e982011-11-08 23:18:08 +0000185
Robert Deliendb5512c2012-02-26 12:15:07 +0000186 if ((io < MXC_IOCLK0) || (io > MXC_IOCLK1))
Marek Vasutc140e982011-11-08 23:18:08 +0000187 return 0;
188
Robert Deliendb5512c2012-02-26 12:15:07 +0000189 io_reg = CLKCTRL_FRAC0_IO0 - io; /* Register order is reversed */
Marek Vasutc140e982011-11-08 23:18:08 +0000190
Robert Deliendb5512c2012-02-26 12:15:07 +0000191 ret = readb(&clkctrl_regs->hw_clkctrl_frac0[io_reg]) &
192 CLKCTRL_FRAC_FRAC_MASK;
Marek Vasutc140e982011-11-08 23:18:08 +0000193
194 return (PLL_FREQ_KHZ * PLL_FREQ_COEF) / ret;
195}
196
197/*
198 * Configure SSP clock frequency, in kHz
199 */
Otavio Salvador2906f942013-01-11 03:19:03 +0000200void mxs_set_sspclk(enum mxs_sspclock ssp, uint32_t freq, int xtal)
Marek Vasutc140e982011-11-08 23:18:08 +0000201{
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000202 struct mxs_clkctrl_regs *clkctrl_regs =
203 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +0000204 uint32_t clk, clkreg;
205
206 if (ssp > MXC_SSPCLK3)
207 return;
208
209 clkreg = (uint32_t)(&clkctrl_regs->hw_clkctrl_ssp0) +
Otavio Salvador5309b002012-08-05 09:05:30 +0000210 (ssp * sizeof(struct mxs_register_32));
Marek Vasutc140e982011-11-08 23:18:08 +0000211
212 clrbits_le32(clkreg, CLKCTRL_SSP_CLKGATE);
213 while (readl(clkreg) & CLKCTRL_SSP_CLKGATE)
214 ;
215
216 if (xtal)
217 clk = XTAL_FREQ_KHZ;
218 else
Otavio Salvador2906f942013-01-11 03:19:03 +0000219 clk = mxs_get_ioclk(ssp >> 1);
Marek Vasutc140e982011-11-08 23:18:08 +0000220
221 if (freq > clk)
222 return;
223
224 /* Calculate the divider and cap it if necessary */
225 clk /= freq;
226 if (clk > CLKCTRL_SSP_DIV_MASK)
227 clk = CLKCTRL_SSP_DIV_MASK;
228
229 clrsetbits_le32(clkreg, CLKCTRL_SSP_DIV_MASK, clk);
230 while (readl(clkreg) & CLKCTRL_SSP_BUSY)
231 ;
232
233 if (xtal)
234 writel(CLKCTRL_CLKSEQ_BYPASS_SSP0 << ssp,
235 &clkctrl_regs->hw_clkctrl_clkseq_set);
236 else
237 writel(CLKCTRL_CLKSEQ_BYPASS_SSP0 << ssp,
238 &clkctrl_regs->hw_clkctrl_clkseq_clr);
239}
240
241/*
242 * Return SSP frequency, in kHz
243 */
Otavio Salvador2906f942013-01-11 03:19:03 +0000244static uint32_t mxs_get_sspclk(enum mxs_sspclock ssp)
Marek Vasutc140e982011-11-08 23:18:08 +0000245{
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000246 struct mxs_clkctrl_regs *clkctrl_regs =
247 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasutc140e982011-11-08 23:18:08 +0000248 uint32_t clkreg;
249 uint32_t clk, tmp;
250
251 if (ssp > MXC_SSPCLK3)
252 return 0;
253
254 tmp = readl(&clkctrl_regs->hw_clkctrl_clkseq);
255 if (tmp & (CLKCTRL_CLKSEQ_BYPASS_SSP0 << ssp))
256 return XTAL_FREQ_KHZ;
257
258 clkreg = (uint32_t)(&clkctrl_regs->hw_clkctrl_ssp0) +
Otavio Salvador5309b002012-08-05 09:05:30 +0000259 (ssp * sizeof(struct mxs_register_32));
Marek Vasutc140e982011-11-08 23:18:08 +0000260
261 tmp = readl(clkreg) & CLKCTRL_SSP_DIV_MASK;
262
263 if (tmp == 0)
264 return 0;
265
Otavio Salvador2906f942013-01-11 03:19:03 +0000266 clk = mxs_get_ioclk(ssp >> 1);
Marek Vasutc140e982011-11-08 23:18:08 +0000267
268 return clk / tmp;
269}
270
271/*
272 * Set SSP/MMC bus frequency, in kHz)
273 */
Otavio Salvador2906f942013-01-11 03:19:03 +0000274void mxs_set_ssp_busclock(unsigned int bus, uint32_t freq)
Marek Vasutc140e982011-11-08 23:18:08 +0000275{
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000276 struct mxs_ssp_regs *ssp_regs;
Otavio Salvador2906f942013-01-11 03:19:03 +0000277 const uint32_t sspclk = mxs_get_sspclk(bus);
Marek Vasutc140e982011-11-08 23:18:08 +0000278 uint32_t reg;
279 uint32_t divide, rate, tgtclk;
280
Marek Vasut96026612013-01-11 03:19:02 +0000281 ssp_regs = mxs_ssp_regs_by_bus(bus);
Marek Vasutc140e982011-11-08 23:18:08 +0000282
283 /*
284 * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)),
285 * CLOCK_DIVIDE has to be an even value from 2 to 254, and
286 * CLOCK_RATE could be any integer from 0 to 255.
287 */
288 for (divide = 2; divide < 254; divide += 2) {
289 rate = sspclk / freq / divide;
290 if (rate <= 256)
291 break;
292 }
293
294 tgtclk = sspclk / divide / rate;
295 while (tgtclk > freq) {
296 rate++;
297 tgtclk = sspclk / divide / rate;
298 }
299 if (rate > 256)
300 rate = 256;
301
302 /* Always set timeout the maximum */
303 reg = SSP_TIMING_TIMEOUT_MASK |
304 (divide << SSP_TIMING_CLOCK_DIVIDE_OFFSET) |
305 ((rate - 1) << SSP_TIMING_CLOCK_RATE_OFFSET);
306 writel(reg, &ssp_regs->hw_ssp_timing);
307
308 debug("SPI%d: Set freq rate to %d KHz (requested %d KHz)\n",
309 bus, tgtclk, freq);
310}
311
312uint32_t mxc_get_clock(enum mxc_clock clk)
313{
314 switch (clk) {
315 case MXC_ARM_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000316 return mxs_get_pclk() * 1000000;
Marek Vasutc140e982011-11-08 23:18:08 +0000317 case MXC_GPMI_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000318 return mxs_get_gpmiclk() * 1000000;
Marek Vasutc140e982011-11-08 23:18:08 +0000319 case MXC_AHB_CLK:
320 case MXC_IPG_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000321 return mxs_get_hclk() * 1000000;
Marek Vasutc140e982011-11-08 23:18:08 +0000322 case MXC_EMI_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000323 return mxs_get_emiclk();
Marek Vasutc140e982011-11-08 23:18:08 +0000324 case MXC_IO0_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000325 return mxs_get_ioclk(MXC_IOCLK0);
Marek Vasutc140e982011-11-08 23:18:08 +0000326 case MXC_IO1_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000327 return mxs_get_ioclk(MXC_IOCLK1);
Marek Vasutc140e982011-11-08 23:18:08 +0000328 case MXC_SSP0_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000329 return mxs_get_sspclk(MXC_SSPCLK0);
Marek Vasutc140e982011-11-08 23:18:08 +0000330 case MXC_SSP1_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000331 return mxs_get_sspclk(MXC_SSPCLK1);
Marek Vasutc140e982011-11-08 23:18:08 +0000332 case MXC_SSP2_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000333 return mxs_get_sspclk(MXC_SSPCLK2);
Marek Vasutc140e982011-11-08 23:18:08 +0000334 case MXC_SSP3_CLK:
Otavio Salvador2906f942013-01-11 03:19:03 +0000335 return mxs_get_sspclk(MXC_SSPCLK3);
Marek Vasut9fd4f7f2012-11-30 18:17:07 +0000336 case MXC_XTAL_CLK:
337 return XTAL_FREQ_KHZ * 1000;
Marek Vasutc140e982011-11-08 23:18:08 +0000338 }
339
340 return 0;
341}