1 /* linux/arch/arm/mach-s3c2443/clock.c
3 * Copyright (c) 2007,2010 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2443 Clock control support
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/errno.h>
28 #include <linux/err.h>
29 #include <linux/sysdev.h>
30 #include <linux/clk.h>
31 #include <linux/mutex.h>
32 #include <linux/serial_core.h>
35 #include <asm/mach/map.h>
37 #include <mach/hardware.h>
39 #include <mach/regs-s3c2443-clock.h>
41 #include <plat/cpu-freq.h>
43 #include <plat/s3c2443.h>
44 #include <plat/clock.h>
45 #include <plat/clock-clksrc.h>
48 /* We currently have to assume that the system is running
49 * from the XTPll input, and that all ***REFCLKs are being
50 * fed from it, as we cannot read the state of OM[4] from
53 * It would be possible for each board initialisation to
54 * set the correct muxing at initialisation
57 static int s3c2443_gate(void __iomem *reg, struct clk *clk, int enable)
59 u32 ctrlbit = clk->ctrlbit;
60 u32 con = __raw_readl(reg);
67 __raw_writel(con, reg);
71 static int s3c2443_clkcon_enable_h(struct clk *clk, int enable)
73 return s3c2443_gate(S3C2443_HCLKCON, clk, enable);
76 static int s3c2443_clkcon_enable_p(struct clk *clk, int enable)
78 return s3c2443_gate(S3C2443_PCLKCON, clk, enable);
81 static int s3c2443_clkcon_enable_s(struct clk *clk, int enable)
83 return s3c2443_gate(S3C2443_SCLKCON, clk, enable);
86 /* s3c2443_roundate_clksrc is close enough to s3c_roundate_clksrc */
88 /* clock selections */
90 static struct clk clk_mpllref = {
97 static struct clk clk_mpll = {
99 .parent = &clk_mpllref,
104 static struct clk clk_i2s_ext = {
109 static struct clk *clk_epllref_sources[] = {
116 static struct clksrc_clk clk_epllref = {
121 .sources = &(struct clksrc_sources) {
122 .sources = clk_epllref_sources,
123 .nr_sources = ARRAY_SIZE(clk_epllref_sources),
125 .reg_src = { .reg = S3C2443_CLKSRC, .size = 2, .shift = 7 },
128 static unsigned long s3c2443_getrate_mdivclk(struct clk *clk)
130 unsigned long parent_rate = clk_get_rate(clk->parent);
131 unsigned long div = __raw_readl(S3C2443_CLKDIV0);
133 div &= S3C2443_CLKDIV0_EXTDIV_MASK;
134 div >>= (S3C2443_CLKDIV0_EXTDIV_SHIFT-1); /* x2 */
136 return parent_rate / (div + 1);
139 static struct clk clk_mdivclk = {
141 .parent = &clk_mpllref,
143 .ops = &(struct clk_ops) {
144 .get_rate = s3c2443_getrate_mdivclk,
148 static int s3c2443_setparent_msysclk(struct clk *clk, struct clk *parent)
150 unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
152 clksrc &= ~(S3C2443_CLKSRC_MSYSCLK_MPLL |
153 S3C2443_CLKSRC_EXTCLK_DIV);
155 if (parent == &clk_mpll)
156 clksrc |= S3C2443_CLKSRC_MSYSCLK_MPLL;
157 else if (parent == &clk_mdivclk)
158 clksrc |= S3C2443_CLKSRC_EXTCLK_DIV;
159 else if (parent != &clk_mpllref)
162 __raw_writel(clksrc, S3C2443_CLKSRC);
163 clk->parent = parent;
168 static struct clk clk_msysclk = {
172 .ops = &(struct clk_ops) {
173 .set_parent = s3c2443_setparent_msysclk,
179 * this clock is sourced from msysclk and can have a number of
180 * divider values applied to it to then be fed into armclk.
183 static struct clk clk_armdiv = {
186 .parent = &clk_msysclk,
191 * this is the clock fed into the ARM core itself, either from
192 * armdiv or from hclk.
195 static int s3c2443_setparent_armclk(struct clk *clk, struct clk *parent)
197 unsigned long clkdiv0;
199 clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
201 if (parent == &clk_armdiv)
202 clkdiv0 &= ~S3C2443_CLKDIV0_DVS;
203 else if (parent == &clk_h)
204 clkdiv0 |= S3C2443_CLKDIV0_DVS;
208 __raw_writel(clkdiv0, S3C2443_CLKDIV0);
212 static struct clk clk_arm = {
215 .ops = &(struct clk_ops) {
216 .set_parent = s3c2443_setparent_armclk,
222 * this is sourced from either the EPLL or the EPLLref clock
225 static int s3c2443_setparent_esysclk(struct clk *clk, struct clk *parent)
227 unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
229 if (parent == &clk_epll)
230 clksrc |= S3C2443_CLKSRC_ESYSCLK_EPLL;
231 else if (parent == &clk_epllref.clk)
232 clksrc &= ~S3C2443_CLKSRC_ESYSCLK_EPLL;
236 __raw_writel(clksrc, S3C2443_CLKSRC);
237 clk->parent = parent;
242 static struct clk clk_esysclk = {
246 .ops = &(struct clk_ops) {
247 .set_parent = s3c2443_setparent_esysclk,
253 * UART baud-rate clock sourced from esysclk via a divisor
256 static struct clksrc_clk clk_uart = {
260 .parent = &clk_esysclk,
262 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 8 },
268 * high-speed spi clock, sourced from esysclk
271 static struct clksrc_clk clk_hsspi = {
275 .parent = &clk_esysclk,
276 .ctrlbit = S3C2443_SCLKCON_HSSPICLK,
277 .enable = s3c2443_clkcon_enable_s,
279 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 4 },
284 * usb host bus-clock, usually 48MHz to provide USB bus clock timing
287 static struct clksrc_clk clk_usb_bus_host = {
289 .name = "usb-bus-host-parent",
291 .parent = &clk_esysclk,
292 .ctrlbit = S3C2443_SCLKCON_USBHOST,
293 .enable = s3c2443_clkcon_enable_s,
295 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 4 },
300 * this clock is sourced from epll, and is fed through a divider,
301 * to a mux controlled by sclkcon where either it or a extclk can
302 * be fed to the hsmmc block
305 static struct clksrc_clk clk_hsmmc_div = {
309 .parent = &clk_esysclk,
311 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 6 },
314 static int s3c2443_setparent_hsmmc(struct clk *clk, struct clk *parent)
316 unsigned long clksrc = __raw_readl(S3C2443_SCLKCON);
318 clksrc &= ~(S3C2443_SCLKCON_HSMMCCLK_EXT |
319 S3C2443_SCLKCON_HSMMCCLK_EPLL);
321 if (parent == &clk_epll)
322 clksrc |= S3C2443_SCLKCON_HSMMCCLK_EPLL;
323 else if (parent == &clk_ext)
324 clksrc |= S3C2443_SCLKCON_HSMMCCLK_EXT;
328 if (clk->usage > 0) {
329 __raw_writel(clksrc, S3C2443_SCLKCON);
332 clk->parent = parent;
336 static int s3c2443_enable_hsmmc(struct clk *clk, int enable)
338 return s3c2443_setparent_hsmmc(clk, clk->parent);
341 static struct clk clk_hsmmc = {
344 .parent = &clk_hsmmc_div.clk,
345 .enable = s3c2443_enable_hsmmc,
346 .ops = &(struct clk_ops) {
347 .set_parent = s3c2443_setparent_hsmmc,
353 * This clock is the output from the I2S divisor of ESYSCLK, and is seperate
354 * from the mux that comes after it (cannot merge into one single clock)
357 static struct clksrc_clk clk_i2s_eplldiv = {
359 .name = "i2s-eplldiv",
361 .parent = &clk_esysclk,
363 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 12, },
368 * i2s bus reference clock, selectable from external, esysclk or epllref
370 * Note, this used to be two clocks, but was compressed into one.
373 struct clk *clk_i2s_srclist[] = {
374 [0] = &clk_i2s_eplldiv.clk,
376 [2] = &clk_epllref.clk,
377 [3] = &clk_epllref.clk,
380 static struct clksrc_clk clk_i2s = {
384 .ctrlbit = S3C2443_SCLKCON_I2SCLK,
385 .enable = s3c2443_clkcon_enable_s,
388 .sources = &(struct clksrc_sources) {
389 .sources = clk_i2s_srclist,
390 .nr_sources = ARRAY_SIZE(clk_i2s_srclist),
392 .reg_src = { .reg = S3C2443_CLKSRC, .size = 2, .shift = 14 },
397 * camera interface bus-clock, divided down from esysclk
400 static struct clksrc_clk clk_cam = {
402 .name = "camif-upll", /* same as 2440 name */
404 .parent = &clk_esysclk,
405 .ctrlbit = S3C2443_SCLKCON_CAMCLK,
406 .enable = s3c2443_clkcon_enable_s,
408 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 4, .shift = 26 },
413 * display interface clock, divided from esysclk
416 static struct clksrc_clk clk_display = {
418 .name = "display-if",
420 .parent = &clk_esysclk,
421 .ctrlbit = S3C2443_SCLKCON_DISPCLK,
422 .enable = s3c2443_clkcon_enable_s,
424 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 8, .shift = 16 },
429 * this divides the msysclk down to pass to h/p/etc.
432 static unsigned long s3c2443_prediv_getrate(struct clk *clk)
434 unsigned long rate = clk_get_rate(clk->parent);
435 unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
437 clkdiv0 &= S3C2443_CLKDIV0_PREDIV_MASK;
438 clkdiv0 >>= S3C2443_CLKDIV0_PREDIV_SHIFT;
440 return rate / (clkdiv0 + 1);
443 static struct clk clk_prediv = {
446 .parent = &clk_msysclk,
447 .ops = &(struct clk_ops) {
448 .get_rate = s3c2443_prediv_getrate,
452 /* standard clock definitions */
454 static struct clk init_clocks_disable[] = {
463 .enable = s3c2443_clkcon_enable_p,
464 .ctrlbit = S3C2443_PCLKCON_SDI,
469 .enable = s3c2443_clkcon_enable_p,
470 .ctrlbit = S3C2443_PCLKCON_ADC,
475 .enable = s3c2443_clkcon_enable_p,
476 .ctrlbit = S3C2443_PCLKCON_IIC,
481 .enable = s3c2443_clkcon_enable_p,
482 .ctrlbit = S3C2443_PCLKCON_IIS,
487 .enable = s3c2443_clkcon_enable_p,
488 .ctrlbit = S3C2443_PCLKCON_SPI0,
493 .enable = s3c2443_clkcon_enable_p,
494 .ctrlbit = S3C2443_PCLKCON_SPI1,
498 static struct clk init_clocks[] = {
503 .enable = s3c2443_clkcon_enable_h,
504 .ctrlbit = S3C2443_HCLKCON_DMA0,
509 .enable = s3c2443_clkcon_enable_h,
510 .ctrlbit = S3C2443_HCLKCON_DMA1,
515 .enable = s3c2443_clkcon_enable_h,
516 .ctrlbit = S3C2443_HCLKCON_DMA2,
521 .enable = s3c2443_clkcon_enable_h,
522 .ctrlbit = S3C2443_HCLKCON_DMA3,
527 .enable = s3c2443_clkcon_enable_h,
528 .ctrlbit = S3C2443_HCLKCON_DMA4,
533 .enable = s3c2443_clkcon_enable_h,
534 .ctrlbit = S3C2443_HCLKCON_DMA5,
539 .enable = s3c2443_clkcon_enable_h,
540 .ctrlbit = S3C2443_HCLKCON_LCDC,
545 .enable = s3c2443_clkcon_enable_p,
546 .ctrlbit = S3C2443_PCLKCON_GPIO,
551 .enable = s3c2443_clkcon_enable_h,
552 .ctrlbit = S3C2443_HCLKCON_USBH,
554 .name = "usb-device",
557 .enable = s3c2443_clkcon_enable_h,
558 .ctrlbit = S3C2443_HCLKCON_USBD,
563 .enable = s3c2443_clkcon_enable_h,
564 .ctrlbit = S3C2443_HCLKCON_HSMMC,
569 .enable = s3c2443_clkcon_enable_h,
570 .ctrlbit = S3C2443_HCLKCON_CFC,
575 .enable = s3c2443_clkcon_enable_h,
576 .ctrlbit = S3C2443_HCLKCON_SSMC,
581 .enable = s3c2443_clkcon_enable_p,
582 .ctrlbit = S3C2443_PCLKCON_PWMT,
587 .enable = s3c2443_clkcon_enable_p,
588 .ctrlbit = S3C2443_PCLKCON_UART0,
593 .enable = s3c2443_clkcon_enable_p,
594 .ctrlbit = S3C2443_PCLKCON_UART1,
599 .enable = s3c2443_clkcon_enable_p,
600 .ctrlbit = S3C2443_PCLKCON_UART2,
605 .enable = s3c2443_clkcon_enable_p,
606 .ctrlbit = S3C2443_PCLKCON_UART3,
611 .enable = s3c2443_clkcon_enable_p,
612 .ctrlbit = S3C2443_PCLKCON_RTC,
617 .ctrlbit = S3C2443_PCLKCON_WDT,
619 .name = "usb-bus-host",
621 .parent = &clk_usb_bus_host.clk,
626 .ctrlbit = S3C2443_PCLKCON_AC97,
630 /* clocks to add where we need to check their parentage */
632 /* s3c2443_clk_initparents
634 * Initialise the parents for the clocks that we get at start-time
637 static int __init clk_init_set_parent(struct clk *clk, struct clk *parent)
639 printk(KERN_DEBUG "clock %s: parent %s\n", clk->name, parent->name);
640 return clk_set_parent(clk, parent);
643 static struct clksrc_clk __initdata *init_list[] = {
644 &clk_epllref, /* should be first */
654 static void __init s3c2443_clk_initparents(void)
656 unsigned long clksrc = __raw_readl(S3C2443_CLKSRC);
662 parent = (clksrc & S3C2443_CLKSRC_ESYSCLK_EPLL) ?
663 &clk_epll : &clk_epllref.clk;
665 clk_init_set_parent(&clk_esysclk, parent);
669 if (clksrc & S3C2443_CLKSRC_MSYSCLK_MPLL) {
672 parent = (clksrc & S3C2443_CLKSRC_EXTCLK_DIV) ?
673 &clk_mdivclk : &clk_mpllref;
676 clk_init_set_parent(&clk_msysclk, parent);
680 if (__raw_readl(S3C2443_CLKDIV0) & S3C2443_CLKDIV0_DVS)
683 parent = &clk_armdiv;
685 clk_init_set_parent(&clk_arm, parent);
687 for (ptr = 0; ptr < ARRAY_SIZE(init_list); ptr++)
688 s3c_set_clksrc(init_list[ptr], false);
691 /* armdiv divisor table */
693 static unsigned int armdiv[16] = {
694 [S3C2443_CLKDIV0_ARMDIV_1 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 1,
695 [S3C2443_CLKDIV0_ARMDIV_2 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 2,
696 [S3C2443_CLKDIV0_ARMDIV_3 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 3,
697 [S3C2443_CLKDIV0_ARMDIV_4 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 4,
698 [S3C2443_CLKDIV0_ARMDIV_6 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 6,
699 [S3C2443_CLKDIV0_ARMDIV_8 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 8,
700 [S3C2443_CLKDIV0_ARMDIV_12 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 12,
701 [S3C2443_CLKDIV0_ARMDIV_16 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 16,
704 static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0)
706 clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
708 return armdiv[clkcon0 >> S3C2443_CLKDIV0_ARMDIV_SHIFT];
711 static inline unsigned long s3c2443_get_hdiv(unsigned long clkcon0)
713 clkcon0 &= S3C2443_CLKDIV0_HCLKDIV_MASK;
718 /* clocks to add straight away */
720 static struct clksrc_clk *clksrcs[] __initdata = {
732 static struct clk *clks[] __initdata = {
745 void __init_or_cpufreq s3c2443_setup_clocks(void)
747 unsigned long mpllcon = __raw_readl(S3C2443_MPLLCON);
748 unsigned long clkdiv0 = __raw_readl(S3C2443_CLKDIV0);
749 struct clk *xtal_clk;
756 xtal_clk = clk_get(NULL, "xtal");
757 xtal = clk_get_rate(xtal_clk);
760 pll = s3c2443_get_mpll(mpllcon, xtal);
761 clk_msysclk.rate = pll;
763 fclk = pll / s3c2443_fclk_div(clkdiv0);
764 hclk = s3c2443_prediv_getrate(&clk_prediv);
765 hclk /= s3c2443_get_hdiv(clkdiv0);
766 pclk = hclk / ((clkdiv0 & S3C2443_CLKDIV0_HALF_PCLK) ? 2 : 1);
768 s3c24xx_setup_clocks(fclk, hclk, pclk);
770 printk("S3C2443: mpll %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
771 (mpllcon & S3C2443_PLLCON_OFF) ? "off":"on",
772 print_mhz(pll), print_mhz(fclk),
773 print_mhz(hclk), print_mhz(pclk));
775 s3c24xx_setup_clocks(fclk, hclk, pclk);
778 void __init s3c2443_init_clocks(int xtal)
781 unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
785 /* s3c2443 parents h and p clocks from prediv */
786 clk_h.parent = &clk_prediv;
787 clk_p.parent = &clk_prediv;
789 s3c24xx_register_baseclocks(xtal);
790 s3c2443_setup_clocks();
791 s3c2443_clk_initparents();
793 for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
796 ret = s3c24xx_register_clock(clkp);
798 printk(KERN_ERR "Failed to register clock %s (%d)\n",
803 for (ptr = 0; ptr < ARRAY_SIZE(clksrcs); ptr++)
804 s3c_register_clksrc(clksrcs[ptr], 1);
806 clk_epll.rate = s3c2443_get_epll(epllcon, xtal);
807 clk_epll.parent = &clk_epllref.clk;
808 clk_usb_bus.parent = &clk_usb_bus_host.clk;
810 /* ensure usb bus clock is within correct rate of 48MHz */
812 if (clk_get_rate(&clk_usb_bus_host.clk) != (48 * 1000 * 1000)) {
813 printk(KERN_INFO "Warning: USB host bus not at 48MHz\n");
814 clk_set_rate(&clk_usb_bus_host.clk, 48*1000*1000);
817 printk("S3C2443: epll %s %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
818 (epllcon & S3C2443_PLLCON_OFF) ? "off":"on",
819 print_mhz(clk_get_rate(&clk_epll)),
820 print_mhz(clk_get_rate(&clk_usb_bus)));
822 /* register clocks from clock array */
824 s3c_register_clocks(init_clocks, ARRAY_SIZE(init_clocks));
826 /* We must be careful disabling the clocks we are not intending to
827 * be using at boot time, as subsystems such as the LCD which do
828 * their own DMA requests to the bus can cause the system to lockup
829 * if they where in the middle of requesting bus access.
831 * Disabling the LCD clock if the LCD is active is very dangerous,
832 * and therefore the bootloader should be careful to not enable
833 * the LCD clock if it is not needed.
836 /* install (and disable) the clocks we do not need immediately */
838 clkp = init_clocks_disable;
839 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
841 ret = s3c24xx_register_clock(clkp);
843 printk(KERN_ERR "Failed to register clock %s (%d)\n",
847 (clkp->enable)(clkp, 0);