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 /* clock selections */
88 /* mpllref is a direct descendant of clk_xtal by default, but it is not
89 * elided as the EPLL can be either sourced by the XTAL or EXTCLK and as
90 * such directly equating the two source clocks is impossible.
92 static struct clk clk_mpllref
= {
98 static struct clk clk_i2s_ext
= {
103 static struct clk
*clk_epllref_sources
[] = {
110 static struct clksrc_clk clk_epllref
= {
115 .sources
= &(struct clksrc_sources
) {
116 .sources
= clk_epllref_sources
,
117 .nr_sources
= ARRAY_SIZE(clk_epllref_sources
),
119 .reg_src
= { .reg
= S3C2443_CLKSRC
, .size
= 2, .shift
= 7 },
122 static unsigned long s3c2443_getrate_mdivclk(struct clk
*clk
)
124 unsigned long parent_rate
= clk_get_rate(clk
->parent
);
125 unsigned long div
= __raw_readl(S3C2443_CLKDIV0
);
127 div
&= S3C2443_CLKDIV0_EXTDIV_MASK
;
128 div
>>= (S3C2443_CLKDIV0_EXTDIV_SHIFT
-1); /* x2 */
130 return parent_rate
/ (div
+ 1);
133 static struct clk clk_mdivclk
= {
135 .parent
= &clk_mpllref
,
137 .ops
= &(struct clk_ops
) {
138 .get_rate
= s3c2443_getrate_mdivclk
,
142 static struct clk
*clk_msysclk_sources
[] = {
149 static struct clksrc_clk clk_msysclk
= {
155 .sources
= &(struct clksrc_sources
) {
156 .sources
= clk_msysclk_sources
,
157 .nr_sources
= ARRAY_SIZE(clk_msysclk_sources
),
159 .reg_src
= { .reg
= S3C2443_CLKSRC
, .size
= 2, .shift
= 3 },
164 * this clock is sourced from msysclk and can have a number of
165 * divider values applied to it to then be fed into armclk.
168 /* armdiv divisor table */
170 static unsigned int armdiv
[16] = {
171 [S3C2443_CLKDIV0_ARMDIV_1
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
] = 1,
172 [S3C2443_CLKDIV0_ARMDIV_2
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
] = 2,
173 [S3C2443_CLKDIV0_ARMDIV_3
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
] = 3,
174 [S3C2443_CLKDIV0_ARMDIV_4
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
] = 4,
175 [S3C2443_CLKDIV0_ARMDIV_6
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
] = 6,
176 [S3C2443_CLKDIV0_ARMDIV_8
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
] = 8,
177 [S3C2443_CLKDIV0_ARMDIV_12
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
] = 12,
178 [S3C2443_CLKDIV0_ARMDIV_16
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
] = 16,
181 static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0
)
183 clkcon0
&= S3C2443_CLKDIV0_ARMDIV_MASK
;
185 return armdiv
[clkcon0
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
];
188 static unsigned long s3c2443_armclk_roundrate(struct clk
*clk
,
191 unsigned long parent
= clk_get_rate(clk
->parent
);
193 unsigned best
= 256; /* bigger than any value */
197 for (ptr
= 0; ptr
< ARRAY_SIZE(armdiv
); ptr
++) {
200 if (calc
<= rate
&& div
< best
)
204 return parent
/ best
;
207 static int s3c2443_armclk_setrate(struct clk
*clk
, unsigned long rate
)
209 unsigned long parent
= clk_get_rate(clk
->parent
);
212 unsigned best
= 256; /* bigger than any value */
216 for (ptr
= 0; ptr
< ARRAY_SIZE(armdiv
); ptr
++) {
219 if (calc
<= rate
&& div
< best
) {
226 unsigned long clkcon0
;
228 clkcon0
= __raw_readl(S3C2443_CLKDIV0
);
229 clkcon0
&= S3C2443_CLKDIV0_ARMDIV_MASK
;
230 clkcon0
|= val
<< S3C2443_CLKDIV0_ARMDIV_SHIFT
;
231 __raw_writel(clkcon0
, S3C2443_CLKDIV0
);
234 return (val
== -1) ? -EINVAL
: 0;
237 static struct clk clk_armdiv
= {
240 .parent
= &clk_msysclk
.clk
,
241 .ops
= &(struct clk_ops
) {
242 .round_rate
= s3c2443_armclk_roundrate
,
243 .set_rate
= s3c2443_armclk_setrate
,
249 * this is the clock fed into the ARM core itself, from armdiv or from hclk.
252 static struct clk
*clk_arm_sources
[] = {
257 static struct clksrc_clk clk_arm
= {
262 .sources
= &(struct clksrc_sources
) {
263 .sources
= clk_arm_sources
,
264 .nr_sources
= ARRAY_SIZE(clk_arm_sources
),
266 .reg_src
= { .reg
= S3C2443_CLKDIV0
, .size
= 1, .shift
= 13 },
271 * this is sourced from either the EPLL or the EPLLref clock
274 static struct clk
*clk_sysclk_sources
[] = {
275 [0] = &clk_epllref
.clk
,
279 static struct clksrc_clk clk_esysclk
= {
285 .sources
= &(struct clksrc_sources
) {
286 .sources
= clk_sysclk_sources
,
287 .nr_sources
= ARRAY_SIZE(clk_sysclk_sources
),
289 .reg_src
= { .reg
= S3C2443_CLKSRC
, .size
= 1, .shift
= 6 },
294 * UART baud-rate clock sourced from esysclk via a divisor
297 static struct clksrc_clk clk_uart
= {
301 .parent
= &clk_esysclk
.clk
,
303 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 4, .shift
= 8 },
309 * high-speed spi clock, sourced from esysclk
312 static struct clksrc_clk clk_hsspi
= {
316 .parent
= &clk_esysclk
.clk
,
317 .ctrlbit
= S3C2443_SCLKCON_HSSPICLK
,
318 .enable
= s3c2443_clkcon_enable_s
,
320 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 2, .shift
= 4 },
325 * usb host bus-clock, usually 48MHz to provide USB bus clock timing
328 static struct clksrc_clk clk_usb_bus_host
= {
330 .name
= "usb-bus-host-parent",
332 .parent
= &clk_esysclk
.clk
,
333 .ctrlbit
= S3C2443_SCLKCON_USBHOST
,
334 .enable
= s3c2443_clkcon_enable_s
,
336 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 2, .shift
= 4 },
341 * this clock is sourced from epll, and is fed through a divider,
342 * to a mux controlled by sclkcon where either it or a extclk can
343 * be fed to the hsmmc block
346 static struct clksrc_clk clk_hsmmc_div
= {
350 .parent
= &clk_esysclk
.clk
,
352 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 2, .shift
= 6 },
355 static int s3c2443_setparent_hsmmc(struct clk
*clk
, struct clk
*parent
)
357 unsigned long clksrc
= __raw_readl(S3C2443_SCLKCON
);
359 clksrc
&= ~(S3C2443_SCLKCON_HSMMCCLK_EXT
|
360 S3C2443_SCLKCON_HSMMCCLK_EPLL
);
362 if (parent
== &clk_epll
)
363 clksrc
|= S3C2443_SCLKCON_HSMMCCLK_EPLL
;
364 else if (parent
== &clk_ext
)
365 clksrc
|= S3C2443_SCLKCON_HSMMCCLK_EXT
;
369 if (clk
->usage
> 0) {
370 __raw_writel(clksrc
, S3C2443_SCLKCON
);
373 clk
->parent
= parent
;
377 static int s3c2443_enable_hsmmc(struct clk
*clk
, int enable
)
379 return s3c2443_setparent_hsmmc(clk
, clk
->parent
);
382 static struct clk clk_hsmmc
= {
385 .parent
= &clk_hsmmc_div
.clk
,
386 .enable
= s3c2443_enable_hsmmc
,
387 .ops
= &(struct clk_ops
) {
388 .set_parent
= s3c2443_setparent_hsmmc
,
394 * This clock is the output from the I2S divisor of ESYSCLK, and is seperate
395 * from the mux that comes after it (cannot merge into one single clock)
398 static struct clksrc_clk clk_i2s_eplldiv
= {
400 .name
= "i2s-eplldiv",
402 .parent
= &clk_esysclk
.clk
,
404 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 4, .shift
= 12, },
409 * i2s bus reference clock, selectable from external, esysclk or epllref
411 * Note, this used to be two clocks, but was compressed into one.
414 struct clk
*clk_i2s_srclist
[] = {
415 [0] = &clk_i2s_eplldiv
.clk
,
417 [2] = &clk_epllref
.clk
,
418 [3] = &clk_epllref
.clk
,
421 static struct clksrc_clk clk_i2s
= {
425 .ctrlbit
= S3C2443_SCLKCON_I2SCLK
,
426 .enable
= s3c2443_clkcon_enable_s
,
429 .sources
= &(struct clksrc_sources
) {
430 .sources
= clk_i2s_srclist
,
431 .nr_sources
= ARRAY_SIZE(clk_i2s_srclist
),
433 .reg_src
= { .reg
= S3C2443_CLKSRC
, .size
= 2, .shift
= 14 },
438 * camera interface bus-clock, divided down from esysclk
441 static struct clksrc_clk clk_cam
= {
443 .name
= "camif-upll", /* same as 2440 name */
445 .parent
= &clk_esysclk
.clk
,
446 .ctrlbit
= S3C2443_SCLKCON_CAMCLK
,
447 .enable
= s3c2443_clkcon_enable_s
,
449 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 4, .shift
= 26 },
454 * display interface clock, divided from esysclk
457 static struct clksrc_clk clk_display
= {
459 .name
= "display-if",
461 .parent
= &clk_esysclk
.clk
,
462 .ctrlbit
= S3C2443_SCLKCON_DISPCLK
,
463 .enable
= s3c2443_clkcon_enable_s
,
465 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 8, .shift
= 16 },
470 * this divides the msysclk down to pass to h/p/etc.
473 static unsigned long s3c2443_prediv_getrate(struct clk
*clk
)
475 unsigned long rate
= clk_get_rate(clk
->parent
);
476 unsigned long clkdiv0
= __raw_readl(S3C2443_CLKDIV0
);
478 clkdiv0
&= S3C2443_CLKDIV0_PREDIV_MASK
;
479 clkdiv0
>>= S3C2443_CLKDIV0_PREDIV_SHIFT
;
481 return rate
/ (clkdiv0
+ 1);
484 static struct clk clk_prediv
= {
487 .parent
= &clk_msysclk
.clk
,
488 .ops
= &(struct clk_ops
) {
489 .get_rate
= s3c2443_prediv_getrate
,
493 /* standard clock definitions */
495 static struct clk init_clocks_disable
[] = {
504 .enable
= s3c2443_clkcon_enable_p
,
505 .ctrlbit
= S3C2443_PCLKCON_SDI
,
510 .enable
= s3c2443_clkcon_enable_p
,
511 .ctrlbit
= S3C2443_PCLKCON_ADC
,
516 .enable
= s3c2443_clkcon_enable_p
,
517 .ctrlbit
= S3C2443_PCLKCON_IIC
,
522 .enable
= s3c2443_clkcon_enable_p
,
523 .ctrlbit
= S3C2443_PCLKCON_IIS
,
528 .enable
= s3c2443_clkcon_enable_p
,
529 .ctrlbit
= S3C2443_PCLKCON_SPI0
,
534 .enable
= s3c2443_clkcon_enable_p
,
535 .ctrlbit
= S3C2443_PCLKCON_SPI1
,
539 static struct clk init_clocks
[] = {
544 .enable
= s3c2443_clkcon_enable_h
,
545 .ctrlbit
= S3C2443_HCLKCON_DMA0
,
550 .enable
= s3c2443_clkcon_enable_h
,
551 .ctrlbit
= S3C2443_HCLKCON_DMA1
,
556 .enable
= s3c2443_clkcon_enable_h
,
557 .ctrlbit
= S3C2443_HCLKCON_DMA2
,
562 .enable
= s3c2443_clkcon_enable_h
,
563 .ctrlbit
= S3C2443_HCLKCON_DMA3
,
568 .enable
= s3c2443_clkcon_enable_h
,
569 .ctrlbit
= S3C2443_HCLKCON_DMA4
,
574 .enable
= s3c2443_clkcon_enable_h
,
575 .ctrlbit
= S3C2443_HCLKCON_DMA5
,
580 .enable
= s3c2443_clkcon_enable_h
,
581 .ctrlbit
= S3C2443_HCLKCON_LCDC
,
586 .enable
= s3c2443_clkcon_enable_p
,
587 .ctrlbit
= S3C2443_PCLKCON_GPIO
,
592 .enable
= s3c2443_clkcon_enable_h
,
593 .ctrlbit
= S3C2443_HCLKCON_USBH
,
595 .name
= "usb-device",
598 .enable
= s3c2443_clkcon_enable_h
,
599 .ctrlbit
= S3C2443_HCLKCON_USBD
,
604 .enable
= s3c2443_clkcon_enable_h
,
605 .ctrlbit
= S3C2443_HCLKCON_HSMMC
,
610 .enable
= s3c2443_clkcon_enable_h
,
611 .ctrlbit
= S3C2443_HCLKCON_CFC
,
616 .enable
= s3c2443_clkcon_enable_h
,
617 .ctrlbit
= S3C2443_HCLKCON_SSMC
,
622 .enable
= s3c2443_clkcon_enable_p
,
623 .ctrlbit
= S3C2443_PCLKCON_PWMT
,
628 .enable
= s3c2443_clkcon_enable_p
,
629 .ctrlbit
= S3C2443_PCLKCON_UART0
,
634 .enable
= s3c2443_clkcon_enable_p
,
635 .ctrlbit
= S3C2443_PCLKCON_UART1
,
640 .enable
= s3c2443_clkcon_enable_p
,
641 .ctrlbit
= S3C2443_PCLKCON_UART2
,
646 .enable
= s3c2443_clkcon_enable_p
,
647 .ctrlbit
= S3C2443_PCLKCON_UART3
,
652 .enable
= s3c2443_clkcon_enable_p
,
653 .ctrlbit
= S3C2443_PCLKCON_RTC
,
658 .ctrlbit
= S3C2443_PCLKCON_WDT
,
660 .name
= "usb-bus-host",
662 .parent
= &clk_usb_bus_host
.clk
,
667 .ctrlbit
= S3C2443_PCLKCON_AC97
,
671 /* clocks to add where we need to check their parentage */
673 static struct clksrc_clk __initdata
*init_list
[] = {
674 &clk_epllref
, /* should be first */
687 static void __init
s3c2443_clk_initparents(void)
691 for (ptr
= 0; ptr
< ARRAY_SIZE(init_list
); ptr
++)
692 s3c_set_clksrc(init_list
[ptr
], true);
695 static inline unsigned long s3c2443_get_hdiv(unsigned long clkcon0
)
697 clkcon0
&= S3C2443_CLKDIV0_HCLKDIV_MASK
;
702 /* clocks to add straight away */
704 static struct clksrc_clk
*clksrcs
[] __initdata
= {
719 static struct clk
*clks
[] __initdata
= {
729 void __init_or_cpufreq
s3c2443_setup_clocks(void)
731 unsigned long mpllcon
= __raw_readl(S3C2443_MPLLCON
);
732 unsigned long clkdiv0
= __raw_readl(S3C2443_CLKDIV0
);
733 struct clk
*xtal_clk
;
740 xtal_clk
= clk_get(NULL
, "xtal");
741 xtal
= clk_get_rate(xtal_clk
);
744 pll
= s3c2443_get_mpll(mpllcon
, xtal
);
745 clk_msysclk
.clk
.rate
= pll
;
747 fclk
= pll
/ s3c2443_fclk_div(clkdiv0
);
748 hclk
= s3c2443_prediv_getrate(&clk_prediv
);
749 hclk
/= s3c2443_get_hdiv(clkdiv0
);
750 pclk
= hclk
/ ((clkdiv0
& S3C2443_CLKDIV0_HALF_PCLK
) ? 2 : 1);
752 s3c24xx_setup_clocks(fclk
, hclk
, pclk
);
754 printk("S3C2443: mpll %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
755 (mpllcon
& S3C2443_PLLCON_OFF
) ? "off":"on",
756 print_mhz(pll
), print_mhz(fclk
),
757 print_mhz(hclk
), print_mhz(pclk
));
759 s3c24xx_setup_clocks(fclk
, hclk
, pclk
);
762 void __init
s3c2443_init_clocks(int xtal
)
765 unsigned long epllcon
= __raw_readl(S3C2443_EPLLCON
);
769 /* s3c2443 parents h and p clocks from prediv */
770 clk_h
.parent
= &clk_prediv
;
771 clk_p
.parent
= &clk_prediv
;
773 s3c24xx_register_baseclocks(xtal
);
774 s3c2443_setup_clocks();
775 s3c2443_clk_initparents();
777 for (ptr
= 0; ptr
< ARRAY_SIZE(clks
); ptr
++) {
780 ret
= s3c24xx_register_clock(clkp
);
782 printk(KERN_ERR
"Failed to register clock %s (%d)\n",
787 for (ptr
= 0; ptr
< ARRAY_SIZE(clksrcs
); ptr
++)
788 s3c_register_clksrc(clksrcs
[ptr
], 1);
790 clk_epll
.rate
= s3c2443_get_epll(epllcon
, xtal
);
791 clk_epll
.parent
= &clk_epllref
.clk
;
792 clk_usb_bus
.parent
= &clk_usb_bus_host
.clk
;
794 /* ensure usb bus clock is within correct rate of 48MHz */
796 if (clk_get_rate(&clk_usb_bus_host
.clk
) != (48 * 1000 * 1000)) {
797 printk(KERN_INFO
"Warning: USB host bus not at 48MHz\n");
798 clk_set_rate(&clk_usb_bus_host
.clk
, 48*1000*1000);
801 printk("S3C2443: epll %s %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
802 (epllcon
& S3C2443_PLLCON_OFF
) ? "off":"on",
803 print_mhz(clk_get_rate(&clk_epll
)),
804 print_mhz(clk_get_rate(&clk_usb_bus
)));
806 /* register clocks from clock array */
808 s3c_register_clocks(init_clocks
, ARRAY_SIZE(init_clocks
));
810 /* We must be careful disabling the clocks we are not intending to
811 * be using at boot time, as subsystems such as the LCD which do
812 * their own DMA requests to the bus can cause the system to lockup
813 * if they where in the middle of requesting bus access.
815 * Disabling the LCD clock if the LCD is active is very dangerous,
816 * and therefore the bootloader should be careful to not enable
817 * the LCD clock if it is not needed.
820 /* install (and disable) the clocks we do not need immediately */
822 clkp
= init_clocks_disable
;
823 for (ptr
= 0; ptr
< ARRAY_SIZE(init_clocks_disable
); ptr
++, clkp
++) {
825 ret
= s3c24xx_register_clock(clkp
);
827 printk(KERN_ERR
"Failed to register clock %s (%d)\n",
831 (clkp
->enable
)(clkp
, 0);