1 /* linux/arch/arm/plat-s3c24xx/s3c2443-clock.c
3 * Copyright (c) 2007, 2010 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2443 Clock control suport - common code
9 #include <linux/init.h>
10 #include <linux/clk.h>
13 #include <mach/regs-s3c2443-clock.h>
15 #include <plat/s3c2443.h>
16 #include <plat/clock.h>
17 #include <plat/clock-clksrc.h>
20 #include <plat/cpu-freq.h>
23 static int s3c2443_gate(void __iomem
*reg
, struct clk
*clk
, int enable
)
25 u32 ctrlbit
= clk
->ctrlbit
;
26 u32 con
= __raw_readl(reg
);
33 __raw_writel(con
, reg
);
37 int s3c2443_clkcon_enable_h(struct clk
*clk
, int enable
)
39 return s3c2443_gate(S3C2443_HCLKCON
, clk
, enable
);
42 int s3c2443_clkcon_enable_p(struct clk
*clk
, int enable
)
44 return s3c2443_gate(S3C2443_PCLKCON
, clk
, enable
);
47 int s3c2443_clkcon_enable_s(struct clk
*clk
, int enable
)
49 return s3c2443_gate(S3C2443_SCLKCON
, clk
, enable
);
52 /* mpllref is a direct descendant of clk_xtal by default, but it is not
53 * elided as the EPLL can be either sourced by the XTAL or EXTCLK and as
54 * such directly equating the two source clocks is impossible.
56 struct clk clk_mpllref
= {
61 static struct clk
*clk_epllref_sources
[] = {
68 struct clksrc_clk clk_epllref
= {
72 .sources
= &(struct clksrc_sources
) {
73 .sources
= clk_epllref_sources
,
74 .nr_sources
= ARRAY_SIZE(clk_epllref_sources
),
76 .reg_src
= { .reg
= S3C2443_CLKSRC
, .size
= 2, .shift
= 7 },
81 * this is sourced from either the EPLL or the EPLLref clock
84 static struct clk
*clk_sysclk_sources
[] = {
85 [0] = &clk_epllref
.clk
,
89 struct clksrc_clk clk_esysclk
= {
94 .sources
= &(struct clksrc_sources
) {
95 .sources
= clk_sysclk_sources
,
96 .nr_sources
= ARRAY_SIZE(clk_sysclk_sources
),
98 .reg_src
= { .reg
= S3C2443_CLKSRC
, .size
= 1, .shift
= 6 },
101 static unsigned long s3c2443_getrate_mdivclk(struct clk
*clk
)
103 unsigned long parent_rate
= clk_get_rate(clk
->parent
);
104 unsigned long div
= __raw_readl(S3C2443_CLKDIV0
);
106 div
&= S3C2443_CLKDIV0_EXTDIV_MASK
;
107 div
>>= (S3C2443_CLKDIV0_EXTDIV_SHIFT
-1); /* x2 */
109 return parent_rate
/ (div
+ 1);
112 static struct clk clk_mdivclk
= {
114 .parent
= &clk_mpllref
,
115 .ops
= &(struct clk_ops
) {
116 .get_rate
= s3c2443_getrate_mdivclk
,
120 static struct clk
*clk_msysclk_sources
[] = {
127 struct clksrc_clk clk_msysclk
= {
132 .sources
= &(struct clksrc_sources
) {
133 .sources
= clk_msysclk_sources
,
134 .nr_sources
= ARRAY_SIZE(clk_msysclk_sources
),
136 .reg_src
= { .reg
= S3C2443_CLKSRC
, .size
= 2, .shift
= 3 },
141 * this divides the msysclk down to pass to h/p/etc.
144 static unsigned long s3c2443_prediv_getrate(struct clk
*clk
)
146 unsigned long rate
= clk_get_rate(clk
->parent
);
147 unsigned long clkdiv0
= __raw_readl(S3C2443_CLKDIV0
);
149 clkdiv0
&= S3C2443_CLKDIV0_PREDIV_MASK
;
150 clkdiv0
>>= S3C2443_CLKDIV0_PREDIV_SHIFT
;
152 return rate
/ (clkdiv0
+ 1);
155 static struct clk clk_prediv
= {
157 .parent
= &clk_msysclk
.clk
,
158 .ops
= &(struct clk_ops
) {
159 .get_rate
= s3c2443_prediv_getrate
,
165 * this clock is sourced from msysclk and can have a number of
166 * divider values applied to it to then be fed into armclk.
169 static unsigned int *armdiv
;
170 static int nr_armdiv
;
171 static int armdivmask
;
173 static unsigned long s3c2443_armclk_roundrate(struct clk
*clk
,
176 unsigned long parent
= clk_get_rate(clk
->parent
);
178 unsigned best
= 256; /* bigger than any value */
185 for (ptr
= 0; ptr
< nr_armdiv
; ptr
++) {
188 /* cpufreq provides 266mhz as 266666000 not 266666666 */
189 calc
= (parent
/ div
/ 1000) * 1000;
190 if (calc
<= rate
&& div
< best
)
195 return parent
/ best
;
198 static unsigned long s3c2443_armclk_getrate(struct clk
*clk
)
200 unsigned long rate
= clk_get_rate(clk
->parent
);
201 unsigned long clkcon0
;
204 if (!nr_armdiv
|| !armdivmask
)
207 clkcon0
= __raw_readl(S3C2443_CLKDIV0
);
208 clkcon0
&= armdivmask
;
209 val
= clkcon0
>> S3C2443_CLKDIV0_ARMDIV_SHIFT
;
211 return rate
/ armdiv
[val
];
214 static int s3c2443_armclk_setrate(struct clk
*clk
, unsigned long rate
)
216 unsigned long parent
= clk_get_rate(clk
->parent
);
219 unsigned best
= 256; /* bigger than any value */
223 if (!nr_armdiv
|| !armdivmask
)
226 for (ptr
= 0; ptr
< nr_armdiv
; ptr
++) {
229 /* cpufreq provides 266mhz as 266666000 not 266666666 */
230 calc
= (parent
/ div
/ 1000) * 1000;
231 if (calc
<= rate
&& div
< best
) {
239 unsigned long clkcon0
;
241 clkcon0
= __raw_readl(S3C2443_CLKDIV0
);
242 clkcon0
&= ~armdivmask
;
243 clkcon0
|= val
<< S3C2443_CLKDIV0_ARMDIV_SHIFT
;
244 __raw_writel(clkcon0
, S3C2443_CLKDIV0
);
247 return (val
== -1) ? -EINVAL
: 0;
250 static struct clk clk_armdiv
= {
252 .parent
= &clk_msysclk
.clk
,
253 .ops
= &(struct clk_ops
) {
254 .round_rate
= s3c2443_armclk_roundrate
,
255 .get_rate
= s3c2443_armclk_getrate
,
256 .set_rate
= s3c2443_armclk_setrate
,
262 * this is the clock fed into the ARM core itself, from armdiv or from hclk.
265 static struct clk
*clk_arm_sources
[] = {
270 static struct clksrc_clk clk_arm
= {
274 .sources
= &(struct clksrc_sources
) {
275 .sources
= clk_arm_sources
,
276 .nr_sources
= ARRAY_SIZE(clk_arm_sources
),
278 .reg_src
= { .reg
= S3C2443_CLKDIV0
, .size
= 1, .shift
= 13 },
283 * usb host bus-clock, usually 48MHz to provide USB bus clock timing
286 static struct clksrc_clk clk_usb_bus_host
= {
288 .name
= "usb-bus-host-parent",
289 .parent
= &clk_esysclk
.clk
,
290 .ctrlbit
= S3C2443_SCLKCON_USBHOST
,
291 .enable
= s3c2443_clkcon_enable_s
,
293 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 2, .shift
= 4 },
296 /* common clksrc clocks */
298 static struct clksrc_clk clksrc_clks
[] = {
300 /* ART baud-rate clock sourced from esysclk via a divisor */
303 .parent
= &clk_esysclk
.clk
,
305 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 4, .shift
= 8 },
307 /* camera interface bus-clock, divided down from esysclk */
309 .name
= "camif-upll", /* same as 2440 name */
310 .parent
= &clk_esysclk
.clk
,
311 .ctrlbit
= S3C2443_SCLKCON_CAMCLK
,
312 .enable
= s3c2443_clkcon_enable_s
,
314 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 4, .shift
= 26 },
317 .name
= "display-if",
318 .parent
= &clk_esysclk
.clk
,
319 .ctrlbit
= S3C2443_SCLKCON_DISPCLK
,
320 .enable
= s3c2443_clkcon_enable_s
,
322 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 8, .shift
= 16 },
326 static struct clk clk_i2s_ext
= {
332 * This clock is the output from the I2S divisor of ESYSCLK, and is separate
333 * from the mux that comes after it (cannot merge into one single clock)
336 static struct clksrc_clk clk_i2s_eplldiv
= {
338 .name
= "i2s-eplldiv",
339 .parent
= &clk_esysclk
.clk
,
341 .reg_div
= { .reg
= S3C2443_CLKDIV1
, .size
= 4, .shift
= 12, },
346 * i2s bus reference clock, selectable from external, esysclk or epllref
348 * Note, this used to be two clocks, but was compressed into one.
351 static struct clk
*clk_i2s_srclist
[] = {
352 [0] = &clk_i2s_eplldiv
.clk
,
354 [2] = &clk_epllref
.clk
,
355 [3] = &clk_epllref
.clk
,
358 static struct clksrc_clk clk_i2s
= {
361 .ctrlbit
= S3C2443_SCLKCON_I2SCLK
,
362 .enable
= s3c2443_clkcon_enable_s
,
365 .sources
= &(struct clksrc_sources
) {
366 .sources
= clk_i2s_srclist
,
367 .nr_sources
= ARRAY_SIZE(clk_i2s_srclist
),
369 .reg_src
= { .reg
= S3C2443_CLKSRC
, .size
= 2, .shift
= 14 },
372 static struct clk init_clocks_off
[] = {
376 .enable
= s3c2443_clkcon_enable_p
,
377 .ctrlbit
= S3C2443_PCLKCON_IIS
,
381 .enable
= s3c2443_clkcon_enable_p
,
382 .ctrlbit
= S3C2443_PCLKCON_HSSPI
,
386 .enable
= s3c2443_clkcon_enable_p
,
387 .ctrlbit
= S3C2443_PCLKCON_ADC
,
391 .enable
= s3c2443_clkcon_enable_p
,
392 .ctrlbit
= S3C2443_PCLKCON_IIC
,
396 static struct clk init_clocks
[] = {
400 .enable
= s3c2443_clkcon_enable_h
,
401 .ctrlbit
= S3C2443_HCLKCON_DMA0
,
405 .enable
= s3c2443_clkcon_enable_h
,
406 .ctrlbit
= S3C2443_HCLKCON_DMA1
,
410 .enable
= s3c2443_clkcon_enable_h
,
411 .ctrlbit
= S3C2443_HCLKCON_DMA2
,
415 .enable
= s3c2443_clkcon_enable_h
,
416 .ctrlbit
= S3C2443_HCLKCON_DMA3
,
420 .enable
= s3c2443_clkcon_enable_h
,
421 .ctrlbit
= S3C2443_HCLKCON_DMA4
,
425 .enable
= s3c2443_clkcon_enable_h
,
426 .ctrlbit
= S3C2443_HCLKCON_DMA5
,
429 .devname
= "s3c-sdhci.1",
431 .enable
= s3c2443_clkcon_enable_h
,
432 .ctrlbit
= S3C2443_HCLKCON_HSMMC
,
436 .enable
= s3c2443_clkcon_enable_p
,
437 .ctrlbit
= S3C2443_PCLKCON_GPIO
,
441 .enable
= s3c2443_clkcon_enable_h
,
442 .ctrlbit
= S3C2443_HCLKCON_USBH
,
444 .name
= "usb-device",
446 .enable
= s3c2443_clkcon_enable_h
,
447 .ctrlbit
= S3C2443_HCLKCON_USBD
,
451 .enable
= s3c2443_clkcon_enable_h
,
452 .ctrlbit
= S3C2443_HCLKCON_LCDC
,
457 .enable
= s3c2443_clkcon_enable_p
,
458 .ctrlbit
= S3C2443_PCLKCON_PWMT
,
462 .enable
= s3c2443_clkcon_enable_h
,
463 .ctrlbit
= S3C2443_HCLKCON_CFC
,
467 .enable
= s3c2443_clkcon_enable_h
,
468 .ctrlbit
= S3C2443_HCLKCON_SSMC
,
471 .devname
= "s3c2440-uart.0",
473 .enable
= s3c2443_clkcon_enable_p
,
474 .ctrlbit
= S3C2443_PCLKCON_UART0
,
477 .devname
= "s3c2440-uart.1",
479 .enable
= s3c2443_clkcon_enable_p
,
480 .ctrlbit
= S3C2443_PCLKCON_UART1
,
483 .devname
= "s3c2440-uart.2",
485 .enable
= s3c2443_clkcon_enable_p
,
486 .ctrlbit
= S3C2443_PCLKCON_UART2
,
489 .devname
= "s3c2440-uart.3",
491 .enable
= s3c2443_clkcon_enable_p
,
492 .ctrlbit
= S3C2443_PCLKCON_UART3
,
496 .enable
= s3c2443_clkcon_enable_p
,
497 .ctrlbit
= S3C2443_PCLKCON_RTC
,
501 .ctrlbit
= S3C2443_PCLKCON_WDT
,
505 .ctrlbit
= S3C2443_PCLKCON_AC97
,
510 .name
= "usb-bus-host",
511 .parent
= &clk_usb_bus_host
.clk
,
515 static inline unsigned long s3c2443_get_hdiv(unsigned long clkcon0
)
517 clkcon0
&= S3C2443_CLKDIV0_HCLKDIV_MASK
;
522 /* EPLLCON compatible enough to get on/off information */
524 void __init_or_cpufreq
s3c2443_common_setup_clocks(pll_fn get_mpll
)
526 unsigned long epllcon
= __raw_readl(S3C2443_EPLLCON
);
527 unsigned long mpllcon
= __raw_readl(S3C2443_MPLLCON
);
528 unsigned long clkdiv0
= __raw_readl(S3C2443_CLKDIV0
);
529 struct clk
*xtal_clk
;
537 xtal_clk
= clk_get(NULL
, "xtal");
538 xtal
= clk_get_rate(xtal_clk
);
541 pll
= get_mpll(mpllcon
, xtal
);
542 clk_msysclk
.clk
.rate
= pll
;
544 fclk
= clk_get_rate(&clk_armdiv
);
545 hclk
= s3c2443_prediv_getrate(&clk_prediv
);
546 hclk
/= s3c2443_get_hdiv(clkdiv0
);
547 pclk
= hclk
/ ((clkdiv0
& S3C2443_CLKDIV0_HALF_PCLK
) ? 2 : 1);
549 s3c24xx_setup_clocks(fclk
, hclk
, pclk
);
551 printk("CPU: MPLL %s %ld.%03ld MHz, cpu %ld.%03ld MHz, mem %ld.%03ld MHz, pclk %ld.%03ld MHz\n",
552 (mpllcon
& S3C2443_PLLCON_OFF
) ? "off":"on",
553 print_mhz(pll
), print_mhz(fclk
),
554 print_mhz(hclk
), print_mhz(pclk
));
556 for (ptr
= 0; ptr
< ARRAY_SIZE(clksrc_clks
); ptr
++)
557 s3c_set_clksrc(&clksrc_clks
[ptr
], true);
559 /* ensure usb bus clock is within correct rate of 48MHz */
561 if (clk_get_rate(&clk_usb_bus_host
.clk
) != (48 * 1000 * 1000)) {
562 printk(KERN_INFO
"Warning: USB host bus not at 48MHz\n");
563 clk_set_rate(&clk_usb_bus_host
.clk
, 48*1000*1000);
566 printk("CPU: EPLL %s %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
567 (epllcon
& S3C2443_PLLCON_OFF
) ? "off":"on",
568 print_mhz(clk_get_rate(&clk_epll
)),
569 print_mhz(clk_get_rate(&clk_usb_bus
)));
572 static struct clk
*clks
[] __initdata
= {
582 static struct clksrc_clk
*clksrcs
[] __initdata
= {
592 void __init
s3c2443_common_init_clocks(int xtal
, pll_fn get_mpll
,
593 unsigned int *divs
, int nr_divs
,
600 armdivmask
= divmask
;
602 /* s3c2443 parents h and p clocks from prediv */
603 clk_h
.parent
= &clk_prediv
;
604 clk_p
.parent
= &clk_prediv
;
606 clk_usb_bus
.parent
= &clk_usb_bus_host
.clk
;
607 clk_epll
.parent
= &clk_epllref
.clk
;
609 s3c24xx_register_baseclocks(xtal
);
610 s3c24xx_register_clocks(clks
, ARRAY_SIZE(clks
));
612 for (ptr
= 0; ptr
< ARRAY_SIZE(clksrcs
); ptr
++)
613 s3c_register_clksrc(clksrcs
[ptr
], 1);
615 s3c_register_clksrc(clksrc_clks
, ARRAY_SIZE(clksrc_clks
));
616 s3c_register_clocks(init_clocks
, ARRAY_SIZE(init_clocks
));
618 /* See s3c2443/etc notes on disabling clocks at init time */
619 s3c_register_clocks(init_clocks_off
, ARRAY_SIZE(init_clocks_off
));
620 s3c_disable_clocks(init_clocks_off
, ARRAY_SIZE(init_clocks_off
));
622 s3c2443_common_setup_clocks(get_mpll
);