2 * Copyright (C) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/math64.h>
22 #include <linux/err.h>
23 #include <linux/clk.h>
26 #include <mach/clock.h>
27 #include <mach/hardware.h>
30 static int _clk_enable(struct clk *clk)
34 reg = __raw_readl(clk->enable_reg);
35 reg |= 1 << clk->enable_shift;
36 __raw_writel(reg, clk->enable_reg);
41 static void _clk_disable(struct clk *clk)
45 reg = __raw_readl(clk->enable_reg);
46 reg &= ~(1 << clk->enable_shift);
47 __raw_writel(reg, clk->enable_reg);
50 static int _clk_can_use_parent(const struct clk *clk_arr[], unsigned int size,
55 for (i = 0; i < size; i++)
56 if (parent == clk_arr[i])
63 _clk_simple_round_rate(struct clk *clk, unsigned long rate, unsigned int limit)
66 unsigned long parent_rate;
68 parent_rate = clk_get_rate(clk->parent);
70 div = parent_rate / rate;
71 if (parent_rate % rate)
77 return parent_rate / div;
80 static unsigned long _clk_parent_round_rate(struct clk *clk, unsigned long rate)
82 return clk->parent->round_rate(clk->parent, rate);
85 static int _clk_parent_set_rate(struct clk *clk, unsigned long rate)
87 return clk->parent->set_rate(clk->parent, rate);
91 * get the system pll clock in Hz
93 * mfi + mfn / (mfd +1)
94 * f = 2 * f_ref * --------------------
97 static unsigned long mx1_decode_pll(unsigned int pll, u32 f_ref)
99 unsigned long long ll;
102 u32 mfi = (pll >> 10) & 0xf;
103 u32 mfn = pll & 0x3ff;
104 u32 mfd = (pll >> 16) & 0x3ff;
105 u32 pd = (pll >> 26) & 0xf;
107 mfi = mfi <= 5 ? 5 : mfi;
109 ll = 2 * (unsigned long long)f_ref *
110 ((mfi << 16) + (mfn << 16) / (mfd + 1));
111 quot = (pd + 1) * (1 << 16);
114 return (unsigned long)ll;
117 static unsigned long clk16m_get_rate(struct clk *clk)
122 static struct clk clk16m = {
124 .get_rate = clk16m_get_rate,
125 .enable = _clk_enable,
126 .enable_reg = CCM_CSCR,
127 .enable_shift = CCM_CSCR_OSC_EN_SHIFT,
128 .disable = _clk_disable,
132 static unsigned long clk32_rate;
134 static unsigned long clk32_get_rate(struct clk *clk)
139 static struct clk clk32 = {
141 .get_rate = clk32_get_rate,
144 static unsigned long clk32_premult_get_rate(struct clk *clk)
146 return clk_get_rate(clk->parent) * 512;
149 static struct clk clk32_premult = {
150 .name = "CLK32_premultiplier",
152 .get_rate = clk32_premult_get_rate,
155 static const struct clk *prem_clk_clocks[] = {
160 static int prem_clk_set_parent(struct clk *clk, struct clk *parent)
163 unsigned int reg = __raw_readl(CCM_CSCR);
165 i = _clk_can_use_parent(prem_clk_clocks, ARRAY_SIZE(prem_clk_clocks),
170 reg &= ~CCM_CSCR_SYSTEM_SEL;
173 reg |= CCM_CSCR_SYSTEM_SEL;
179 __raw_writel(reg, CCM_CSCR);
184 static struct clk prem_clk = {
186 .set_parent = prem_clk_set_parent,
189 static unsigned long system_clk_get_rate(struct clk *clk)
191 return mx1_decode_pll(__raw_readl(CCM_SPCTL0),
192 clk_get_rate(clk->parent));
195 static struct clk system_clk = {
196 .name = "system_clk",
198 .get_rate = system_clk_get_rate,
201 static unsigned long mcu_clk_get_rate(struct clk *clk)
203 return mx1_decode_pll(__raw_readl(CCM_MPCTL0),
204 clk_get_rate(clk->parent));
207 static struct clk mcu_clk = {
209 .parent = &clk32_premult,
210 .get_rate = mcu_clk_get_rate,
213 static unsigned long fclk_get_rate(struct clk *clk)
215 unsigned long fclk = clk_get_rate(clk->parent);
217 if (__raw_readl(CCM_CSCR) & CCM_CSCR_PRESC)
223 static struct clk fclk = {
226 .get_rate = fclk_get_rate,
230 * get hclk ( SDRAM, CSI, Memory Stick, I2C, DMA )
232 static unsigned long hclk_get_rate(struct clk *clk)
234 return clk_get_rate(clk->parent) / (((__raw_readl(CCM_CSCR) &
235 CCM_CSCR_BCLK_MASK) >> CCM_CSCR_BCLK_OFFSET) + 1);
238 static unsigned long hclk_round_rate(struct clk *clk, unsigned long rate)
240 return _clk_simple_round_rate(clk, rate, 16);
243 static int hclk_set_rate(struct clk *clk, unsigned long rate)
247 unsigned long parent_rate;
249 parent_rate = clk_get_rate(clk->parent);
251 div = parent_rate / rate;
253 if (div > 16 || div < 1 || ((parent_rate / div) != rate))
258 reg = __raw_readl(CCM_CSCR);
259 reg &= ~CCM_CSCR_BCLK_MASK;
260 reg |= div << CCM_CSCR_BCLK_OFFSET;
261 __raw_writel(reg, CCM_CSCR);
266 static struct clk hclk = {
268 .parent = &system_clk,
269 .get_rate = hclk_get_rate,
270 .round_rate = hclk_round_rate,
271 .set_rate = hclk_set_rate,
274 static unsigned long clk48m_get_rate(struct clk *clk)
276 return clk_get_rate(clk->parent) / (((__raw_readl(CCM_CSCR) &
277 CCM_CSCR_USB_MASK) >> CCM_CSCR_USB_OFFSET) + 1);
280 static unsigned long clk48m_round_rate(struct clk *clk, unsigned long rate)
282 return _clk_simple_round_rate(clk, rate, 8);
285 static int clk48m_set_rate(struct clk *clk, unsigned long rate)
289 unsigned long parent_rate;
291 parent_rate = clk_get_rate(clk->parent);
293 div = parent_rate / rate;
295 if (div > 8 || div < 1 || ((parent_rate / div) != rate))
300 reg = __raw_readl(CCM_CSCR);
301 reg &= ~CCM_CSCR_USB_MASK;
302 reg |= div << CCM_CSCR_USB_OFFSET;
303 __raw_writel(reg, CCM_CSCR);
308 static struct clk clk48m = {
310 .parent = &system_clk,
311 .get_rate = clk48m_get_rate,
312 .round_rate = clk48m_round_rate,
313 .set_rate = clk48m_set_rate,
317 * get peripheral clock 1 ( UART[12], Timer[12], PWM )
319 static unsigned long perclk1_get_rate(struct clk *clk)
321 return clk_get_rate(clk->parent) / (((__raw_readl(CCM_PCDR) &
322 CCM_PCDR_PCLK1_MASK) >> CCM_PCDR_PCLK1_OFFSET) + 1);
325 static unsigned long perclk1_round_rate(struct clk *clk, unsigned long rate)
327 return _clk_simple_round_rate(clk, rate, 16);
330 static int perclk1_set_rate(struct clk *clk, unsigned long rate)
334 unsigned long parent_rate;
336 parent_rate = clk_get_rate(clk->parent);
338 div = parent_rate / rate;
340 if (div > 16 || div < 1 || ((parent_rate / div) != rate))
345 reg = __raw_readl(CCM_PCDR);
346 reg &= ~CCM_PCDR_PCLK1_MASK;
347 reg |= div << CCM_PCDR_PCLK1_OFFSET;
348 __raw_writel(reg, CCM_PCDR);
354 * get peripheral clock 2 ( LCD, SD, SPI[12] )
356 static unsigned long perclk2_get_rate(struct clk *clk)
358 return clk_get_rate(clk->parent) / (((__raw_readl(CCM_PCDR) &
359 CCM_PCDR_PCLK2_MASK) >> CCM_PCDR_PCLK2_OFFSET) + 1);
362 static unsigned long perclk2_round_rate(struct clk *clk, unsigned long rate)
364 return _clk_simple_round_rate(clk, rate, 16);
367 static int perclk2_set_rate(struct clk *clk, unsigned long rate)
371 unsigned long parent_rate;
373 parent_rate = clk_get_rate(clk->parent);
375 div = parent_rate / rate;
377 if (div > 16 || div < 1 || ((parent_rate / div) != rate))
382 reg = __raw_readl(CCM_PCDR);
383 reg &= ~CCM_PCDR_PCLK2_MASK;
384 reg |= div << CCM_PCDR_PCLK2_OFFSET;
385 __raw_writel(reg, CCM_PCDR);
391 * get peripheral clock 3 ( SSI )
393 static unsigned long perclk3_get_rate(struct clk *clk)
395 return clk_get_rate(clk->parent) / (((__raw_readl(CCM_PCDR) &
396 CCM_PCDR_PCLK3_MASK) >> CCM_PCDR_PCLK3_OFFSET) + 1);
399 static unsigned long perclk3_round_rate(struct clk *clk, unsigned long rate)
401 return _clk_simple_round_rate(clk, rate, 128);
404 static int perclk3_set_rate(struct clk *clk, unsigned long rate)
408 unsigned long parent_rate;
410 parent_rate = clk_get_rate(clk->parent);
412 div = parent_rate / rate;
414 if (div > 128 || div < 1 || ((parent_rate / div) != rate))
419 reg = __raw_readl(CCM_PCDR);
420 reg &= ~CCM_PCDR_PCLK3_MASK;
421 reg |= div << CCM_PCDR_PCLK3_OFFSET;
422 __raw_writel(reg, CCM_PCDR);
427 static struct clk perclk[] = {
431 .parent = &system_clk,
432 .get_rate = perclk1_get_rate,
433 .round_rate = perclk1_round_rate,
434 .set_rate = perclk1_set_rate,
438 .parent = &system_clk,
439 .get_rate = perclk2_get_rate,
440 .round_rate = perclk2_round_rate,
441 .set_rate = perclk2_set_rate,
445 .parent = &system_clk,
446 .get_rate = perclk3_get_rate,
447 .round_rate = perclk3_round_rate,
448 .set_rate = perclk3_set_rate,
452 static const struct clk *clko_clocks[] = {
461 static int clko_set_parent(struct clk *clk, struct clk *parent)
466 i = _clk_can_use_parent(clko_clocks, ARRAY_SIZE(clko_clocks), parent);
470 reg = __raw_readl(CCM_CSCR) & ~CCM_CSCR_CLKO_MASK;
471 reg |= i << CCM_CSCR_CLKO_OFFSET;
472 __raw_writel(reg, CCM_CSCR);
474 if (clko_clocks[i]->set_rate && clko_clocks[i]->round_rate) {
475 clk->set_rate = _clk_parent_set_rate;
476 clk->round_rate = _clk_parent_round_rate;
478 clk->set_rate = NULL;
479 clk->round_rate = NULL;
485 static struct clk clko_clk = {
487 .set_parent = clko_set_parent,
490 static struct clk dma_clk = {
493 .round_rate = _clk_parent_round_rate,
494 .set_rate = _clk_parent_set_rate,
495 .enable = _clk_enable,
496 .enable_reg = SCM_GCCR,
497 .enable_shift = SCM_GCCR_DMA_CLK_EN_OFFSET,
498 .disable = _clk_disable,
501 static struct clk csi_clk = {
504 .round_rate = _clk_parent_round_rate,
505 .set_rate = _clk_parent_set_rate,
506 .enable = _clk_enable,
507 .enable_reg = SCM_GCCR,
508 .enable_shift = SCM_GCCR_CSI_CLK_EN_OFFSET,
509 .disable = _clk_disable,
512 static struct clk mma_clk = {
515 .round_rate = _clk_parent_round_rate,
516 .set_rate = _clk_parent_set_rate,
517 .enable = _clk_enable,
518 .enable_reg = SCM_GCCR,
519 .enable_shift = SCM_GCCR_MMA_CLK_EN_OFFSET,
520 .disable = _clk_disable,
523 static struct clk usbd_clk = {
526 .round_rate = _clk_parent_round_rate,
527 .set_rate = _clk_parent_set_rate,
528 .enable = _clk_enable,
529 .enable_reg = SCM_GCCR,
530 .enable_shift = SCM_GCCR_USBD_CLK_EN_OFFSET,
531 .disable = _clk_disable,
534 static struct clk gpt_clk = {
536 .parent = &perclk[0],
537 .round_rate = _clk_parent_round_rate,
538 .set_rate = _clk_parent_set_rate,
541 static struct clk uart_clk = {
543 .parent = &perclk[0],
544 .round_rate = _clk_parent_round_rate,
545 .set_rate = _clk_parent_set_rate,
548 static struct clk i2c_clk = {
551 .round_rate = _clk_parent_round_rate,
552 .set_rate = _clk_parent_set_rate,
555 static struct clk spi_clk = {
557 .parent = &perclk[1],
558 .round_rate = _clk_parent_round_rate,
559 .set_rate = _clk_parent_set_rate,
562 static struct clk sdhc_clk = {
564 .parent = &perclk[1],
565 .round_rate = _clk_parent_round_rate,
566 .set_rate = _clk_parent_set_rate,
569 static struct clk lcdc_clk = {
571 .parent = &perclk[1],
572 .round_rate = _clk_parent_round_rate,
573 .set_rate = _clk_parent_set_rate,
576 static struct clk mshc_clk = {
579 .round_rate = _clk_parent_round_rate,
580 .set_rate = _clk_parent_set_rate,
583 static struct clk ssi_clk = {
585 .parent = &perclk[2],
586 .round_rate = _clk_parent_round_rate,
587 .set_rate = _clk_parent_set_rate,
590 static struct clk rtc_clk = {
595 static struct clk *mxc_clks[] = {
624 int __init mxc_clocks_init(unsigned long fref)
629 /* disable clocks we are able to */
630 __raw_writel(0, SCM_GCCR);
633 reg = __raw_readl(CCM_CSCR);
635 /* detect clock reference for system PLL */
636 if (reg & CCM_CSCR_SYSTEM_SEL) {
637 prem_clk.parent = &clk16m;
639 /* ensure that oscillator is disabled */
640 reg &= ~(1 << CCM_CSCR_OSC_EN_SHIFT);
641 __raw_writel(reg, CCM_CSCR);
642 prem_clk.parent = &clk32_premult;
645 /* detect reference for CLKO */
646 reg = (reg & CCM_CSCR_CLKO_MASK) >> CCM_CSCR_CLKO_OFFSET;
647 clko_clk.parent = (struct clk *)clko_clocks[reg];
649 for (clkp = mxc_clks; clkp < mxc_clks + ARRAY_SIZE(mxc_clks); clkp++)