1 /* linux/arch/arm/mach-s5p6440/clock.c
3 * Copyright (c) 2009 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
6 * S5P6440 - Clock 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 version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/sysdev.h>
23 #include <mach/hardware.h>
26 #include <plat/cpu-freq.h>
27 #include <mach/regs-clock.h>
28 #include <plat/clock.h>
30 #include <plat/clock-clksrc.h>
31 #include <plat/s5p-clock.h>
33 #include <plat/s5p6440.h>
35 /* APLL Mux output clock */
36 static struct clksrc_clk clk_mout_apll = {
41 .sources = &clk_src_apll,
42 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 0, .size = 1 },
45 static int s5p6440_epll_enable(struct clk *clk, int enable)
47 unsigned int ctrlbit = clk->ctrlbit;
48 unsigned int epll_con = __raw_readl(S5P_EPLL_CON) & ~ctrlbit;
51 __raw_writel(epll_con | ctrlbit, S5P_EPLL_CON);
53 __raw_writel(epll_con, S5P_EPLL_CON);
58 static unsigned long s5p6440_epll_get_rate(struct clk *clk)
63 static u32 epll_div[][5] = {
64 { 36000000, 0, 48, 1, 4 },
65 { 48000000, 0, 32, 1, 3 },
66 { 60000000, 0, 40, 1, 3 },
67 { 72000000, 0, 48, 1, 3 },
68 { 84000000, 0, 28, 1, 2 },
69 { 96000000, 0, 32, 1, 2 },
70 { 32768000, 45264, 43, 1, 4 },
71 { 45158000, 6903, 30, 1, 3 },
72 { 49152000, 50332, 32, 1, 3 },
73 { 67738000, 10398, 45, 1, 3 },
74 { 73728000, 9961, 49, 1, 3 }
77 static int s5p6440_epll_set_rate(struct clk *clk, unsigned long rate)
79 unsigned int epll_con, epll_con_k;
82 if (clk->rate == rate) /* Return if nothing changed */
85 epll_con = __raw_readl(S5P_EPLL_CON);
86 epll_con_k = __raw_readl(S5P_EPLL_CON_K);
88 epll_con_k &= ~(PLL90XX_KDIV_MASK);
89 epll_con &= ~(PLL90XX_MDIV_MASK | PLL90XX_PDIV_MASK | PLL90XX_SDIV_MASK);
91 for (i = 0; i < ARRAY_SIZE(epll_div); i++) {
92 if (epll_div[i][0] == rate) {
93 epll_con_k |= (epll_div[i][1] << PLL90XX_KDIV_SHIFT);
94 epll_con |= (epll_div[i][2] << PLL90XX_MDIV_SHIFT) |
95 (epll_div[i][3] << PLL90XX_PDIV_SHIFT) |
96 (epll_div[i][4] << PLL90XX_SDIV_SHIFT);
101 if (i == ARRAY_SIZE(epll_div)) {
102 printk(KERN_ERR "%s: Invalid Clock EPLL Frequency\n", __func__);
106 __raw_writel(epll_con, S5P_EPLL_CON);
107 __raw_writel(epll_con_k, S5P_EPLL_CON_K);
114 static struct clk_ops s5p6440_epll_ops = {
115 .get_rate = s5p6440_epll_get_rate,
116 .set_rate = s5p6440_epll_set_rate,
119 static struct clksrc_clk clk_mout_epll = {
124 .sources = &clk_src_epll,
125 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 2, .size = 1 },
128 static struct clksrc_clk clk_mout_mpll = {
133 .sources = &clk_src_mpll,
134 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 1, .size = 1 },
137 static struct clk clk_h_low = {
143 .ops = &clk_ops_def_setrate,
146 static struct clk clk_p_low = {
152 .ops = &clk_ops_def_setrate,
161 static const u32 clock_table[][3] = {
162 /*{ARM_CLK, DIVarm, DIVhclk}*/
163 {L0 * 1000, (0 << ARM_DIV_RATIO_SHIFT), (3 << S5P_CLKDIV0_HCLK_SHIFT)},
164 {L1 * 1000, (1 << ARM_DIV_RATIO_SHIFT), (1 << S5P_CLKDIV0_HCLK_SHIFT)},
165 {L2 * 1000, (3 << ARM_DIV_RATIO_SHIFT), (0 << S5P_CLKDIV0_HCLK_SHIFT)},
168 static unsigned long s5p6440_armclk_get_rate(struct clk *clk)
170 unsigned long rate = clk_get_rate(clk->parent);
173 /* divisor mask starts at bit0, so no need to shift */
174 clkdiv = __raw_readl(ARM_CLK_DIV) & ARM_DIV_MASK;
176 return rate / (clkdiv + 1);
179 static unsigned long s5p6440_armclk_round_rate(struct clk *clk,
184 for (iter = 1 ; iter < ARRAY_SIZE(clock_table) ; iter++) {
185 if (rate > clock_table[iter][0])
186 return clock_table[iter-1][0];
189 return clock_table[ARRAY_SIZE(clock_table) - 1][0];
192 static int s5p6440_armclk_set_rate(struct clk *clk, unsigned long rate)
197 u32 cur_rate = clk->ops->get_rate(clk);
200 round_tmp = clk->ops->round_rate(clk, rate);
201 if (round_tmp == cur_rate)
205 for (iter = 0 ; iter < ARRAY_SIZE(clock_table) ; iter++) {
206 if (round_tmp == clock_table[iter][0])
210 if (iter >= ARRAY_SIZE(clock_table))
211 iter = ARRAY_SIZE(clock_table) - 1;
213 local_irq_save(flags);
214 if (cur_rate > round_tmp) {
216 clk_div0_tmp = __raw_readl(ARM_CLK_DIV) & ~(ARM_DIV_MASK);
217 clk_div0_tmp |= clock_table[iter][1];
218 __raw_writel(clk_div0_tmp, ARM_CLK_DIV);
220 clk_div0_tmp = __raw_readl(ARM_CLK_DIV) &
221 ~(S5P_CLKDIV0_HCLK_MASK);
222 clk_div0_tmp |= clock_table[iter][2];
223 __raw_writel(clk_div0_tmp, ARM_CLK_DIV);
228 clk_div0_tmp = __raw_readl(ARM_CLK_DIV) &
229 ~(S5P_CLKDIV0_HCLK_MASK);
230 clk_div0_tmp |= clock_table[iter][2];
231 __raw_writel(clk_div0_tmp, ARM_CLK_DIV);
233 clk_div0_tmp = __raw_readl(ARM_CLK_DIV) & ~(ARM_DIV_MASK);
234 clk_div0_tmp |= clock_table[iter][1];
235 __raw_writel(clk_div0_tmp, ARM_CLK_DIV);
237 local_irq_restore(flags);
239 clk->rate = clock_table[iter][0];
244 static struct clk_ops s5p6440_clkarm_ops = {
245 .get_rate = s5p6440_armclk_get_rate,
246 .set_rate = s5p6440_armclk_set_rate,
247 .round_rate = s5p6440_armclk_round_rate,
250 static struct clksrc_clk clk_armclk = {
254 .parent = &clk_mout_apll.clk,
255 .ops = &s5p6440_clkarm_ops,
257 .reg_div = { .reg = S5P_CLK_DIV0, .shift = 0, .size = 4 },
260 static struct clksrc_clk clk_dout_mpll = {
264 .parent = &clk_mout_mpll.clk,
266 .reg_div = { .reg = S5P_CLK_DIV0, .shift = 4, .size = 1 },
269 static struct clksrc_clk clk_hclk = {
273 .parent = &clk_armclk.clk,
275 .reg_div = { .reg = S5P_CLK_DIV0, .shift = 8, .size = 4 },
278 int s5p6440_clk48m_ctrl(struct clk *clk, int enable)
283 /* can't rely on clock lock, this register has other usages */
284 local_irq_save(flags);
286 val = __raw_readl(S5P_OTHERS);
288 val |= S5P_OTHERS_USB_SIG_MASK;
290 val &= ~S5P_OTHERS_USB_SIG_MASK;
292 __raw_writel(val, S5P_OTHERS);
294 local_irq_restore(flags);
299 static int s5p6440_pclk_ctrl(struct clk *clk, int enable)
301 return s5p_gatectrl(S5P_CLK_GATE_PCLK, clk, enable);
304 static int s5p6440_hclk0_ctrl(struct clk *clk, int enable)
306 return s5p_gatectrl(S5P_CLK_GATE_HCLK0, clk, enable);
309 static int s5p6440_hclk1_ctrl(struct clk *clk, int enable)
311 return s5p_gatectrl(S5P_CLK_GATE_HCLK1, clk, enable);
314 static int s5p6440_sclk_ctrl(struct clk *clk, int enable)
316 return s5p_gatectrl(S5P_CLK_GATE_SCLK0, clk, enable);
319 static int s5p6440_mem_ctrl(struct clk *clk, int enable)
321 return s5p_gatectrl(S5P_CLK_GATE_MEM0, clk, enable);
325 * The following clocks will be disabled during clock initialization. It is
326 * recommended to keep the following clocks disabled until the driver requests
327 * for enabling the clock.
329 static struct clk init_clocks_disable[] = {
333 .parent = &clk_hclk.clk,
334 .enable = s5p6440_mem_ctrl,
335 .ctrlbit = S5P_CLKCON_MEM0_HCLK_NFCON,
339 .parent = &clk_p_low,
340 .enable = s5p6440_pclk_ctrl,
341 .ctrlbit = S5P_CLKCON_PCLK_TSADC,
345 .parent = &clk_p_low,
346 .enable = s5p6440_pclk_ctrl,
347 .ctrlbit = S5P_CLKCON_PCLK_IIC0,
351 .parent = &clk_p_low,
352 .enable = s5p6440_pclk_ctrl,
353 .ctrlbit = S5P_CLKCON_PCLK_IIS2,
357 .parent = &clk_p_low,
358 .enable = s5p6440_pclk_ctrl,
359 .ctrlbit = S5P_CLKCON_PCLK_SPI0,
363 .parent = &clk_p_low,
364 .enable = s5p6440_pclk_ctrl,
365 .ctrlbit = S5P_CLKCON_PCLK_SPI1,
367 .name = "sclk_spi_48",
370 .enable = s5p6440_sclk_ctrl,
371 .ctrlbit = S5P_CLKCON_SCLK0_SPI0_48,
373 .name = "sclk_spi_48",
376 .enable = s5p6440_sclk_ctrl,
377 .ctrlbit = S5P_CLKCON_SCLK0_SPI1_48,
382 .enable = s5p6440_sclk_ctrl,
383 .ctrlbit = S5P_CLKCON_SCLK0_MMC0_48,
388 .enable = s5p6440_sclk_ctrl,
389 .ctrlbit = S5P_CLKCON_SCLK0_MMC1_48,
394 .enable = s5p6440_sclk_ctrl,
395 .ctrlbit = S5P_CLKCON_SCLK0_MMC2_48,
399 .parent = &clk_h_low,
400 .enable = s5p6440_hclk0_ctrl,
401 .ctrlbit = S5P_CLKCON_HCLK0_USB
405 .parent = &clk_h_low,
406 .enable = s5p6440_hclk0_ctrl,
407 .ctrlbit = S5P_CLKCON_HCLK0_POST0
411 .parent = &clk_h_low,
412 .enable = s5p6440_hclk1_ctrl,
413 .ctrlbit = S5P_CLKCON_HCLK1_DISPCON,
417 .parent = &clk_h_low,
418 .enable = s5p6440_hclk0_ctrl,
419 .ctrlbit = S5P_CLKCON_HCLK0_HSMMC0,
423 .parent = &clk_h_low,
424 .enable = s5p6440_hclk0_ctrl,
425 .ctrlbit = S5P_CLKCON_HCLK0_HSMMC1,
429 .parent = &clk_h_low,
430 .enable = s5p6440_hclk0_ctrl,
431 .ctrlbit = S5P_CLKCON_HCLK0_HSMMC2,
435 .parent = &clk_p_low,
436 .enable = s5p6440_pclk_ctrl,
437 .ctrlbit = S5P_CLKCON_PCLK_RTC,
441 .parent = &clk_p_low,
442 .enable = s5p6440_pclk_ctrl,
443 .ctrlbit = S5P_CLKCON_PCLK_WDT,
447 .parent = &clk_p_low,
448 .enable = s5p6440_pclk_ctrl,
449 .ctrlbit = S5P_CLKCON_PCLK_PWM,
454 * The following clocks will be enabled during clock initialization.
456 static struct clk init_clocks[] = {
460 .parent = &clk_p_low,
461 .enable = s5p6440_pclk_ctrl,
462 .ctrlbit = S5P_CLKCON_PCLK_GPIO,
466 .parent = &clk_p_low,
467 .enable = s5p6440_pclk_ctrl,
468 .ctrlbit = S5P_CLKCON_PCLK_UART0,
472 .parent = &clk_p_low,
473 .enable = s5p6440_pclk_ctrl,
474 .ctrlbit = S5P_CLKCON_PCLK_UART1,
478 .parent = &clk_p_low,
479 .enable = s5p6440_pclk_ctrl,
480 .ctrlbit = S5P_CLKCON_PCLK_UART2,
484 .parent = &clk_p_low,
485 .enable = s5p6440_pclk_ctrl,
486 .ctrlbit = S5P_CLKCON_PCLK_UART3,
490 static struct clk clk_iis_cd_v40 = {
491 .name = "iis_cdclk_v40",
495 static struct clk clk_pcm_cd = {
500 static struct clk *clkset_spi_mmc_list[] = {
506 static struct clksrc_sources clkset_spi_mmc = {
507 .sources = clkset_spi_mmc_list,
508 .nr_sources = ARRAY_SIZE(clkset_spi_mmc_list),
511 static struct clk *clkset_uart_list[] = {
516 static struct clksrc_sources clkset_uart = {
517 .sources = clkset_uart_list,
518 .nr_sources = ARRAY_SIZE(clkset_uart_list),
521 static struct clksrc_clk clksrcs[] = {
526 .ctrlbit = S5P_CLKCON_SCLK0_MMC0,
527 .enable = s5p6440_sclk_ctrl,
529 .sources = &clkset_spi_mmc,
530 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 18, .size = 2 },
531 .reg_div = { .reg = S5P_CLK_DIV1, .shift = 0, .size = 4 },
536 .ctrlbit = S5P_CLKCON_SCLK0_MMC1,
537 .enable = s5p6440_sclk_ctrl,
539 .sources = &clkset_spi_mmc,
540 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 20, .size = 2 },
541 .reg_div = { .reg = S5P_CLK_DIV1, .shift = 4, .size = 4 },
546 .ctrlbit = S5P_CLKCON_SCLK0_MMC2,
547 .enable = s5p6440_sclk_ctrl,
549 .sources = &clkset_spi_mmc,
550 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 22, .size = 2 },
551 .reg_div = { .reg = S5P_CLK_DIV1, .shift = 8, .size = 4 },
556 .ctrlbit = S5P_CLKCON_SCLK0_UART,
557 .enable = s5p6440_sclk_ctrl,
559 .sources = &clkset_uart,
560 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 13, .size = 1 },
561 .reg_div = { .reg = S5P_CLK_DIV2, .shift = 16, .size = 4 },
566 .ctrlbit = S5P_CLKCON_SCLK0_SPI0,
567 .enable = s5p6440_sclk_ctrl,
569 .sources = &clkset_spi_mmc,
570 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 14, .size = 2 },
571 .reg_div = { .reg = S5P_CLK_DIV2, .shift = 0, .size = 4 },
576 .ctrlbit = S5P_CLKCON_SCLK0_SPI1,
577 .enable = s5p6440_sclk_ctrl,
579 .sources = &clkset_spi_mmc,
580 .reg_src = { .reg = S5P_CLK_SRC0, .shift = 16, .size = 2 },
581 .reg_div = { .reg = S5P_CLK_DIV2, .shift = 4, .size = 4 },
585 /* Clock initialisation code */
586 static struct clksrc_clk *sysclks[] = {
595 void __init_or_cpufreq s5p6440_setup_clocks(void)
597 struct clk *xtal_clk;
601 unsigned long hclk_low;
603 unsigned long pclk_low;
611 /* Set S5P6440 functions for clk_fout_epll */
612 clk_fout_epll.enable = s5p6440_epll_enable;
613 clk_fout_epll.ops = &s5p6440_epll_ops;
615 /* Set S5P6440 functions for arm clock */
616 clk_48m.enable = s5p6440_clk48m_ctrl;
618 clkdiv0 = __raw_readl(S5P_CLK_DIV0);
619 clkdiv3 = __raw_readl(S5P_CLK_DIV3);
621 xtal_clk = clk_get(NULL, "ext_xtal");
622 BUG_ON(IS_ERR(xtal_clk));
624 xtal = clk_get_rate(xtal_clk);
627 epll = s5p_get_pll90xx(xtal, __raw_readl(S5P_EPLL_CON),
628 __raw_readl(S5P_EPLL_CON_K));
629 mpll = s5p_get_pll45xx(xtal, __raw_readl(S5P_MPLL_CON), pll_4502);
630 apll = s5p_get_pll45xx(xtal, __raw_readl(S5P_APLL_CON), pll_4502);
632 clk_fout_mpll.rate = mpll;
633 clk_fout_epll.rate = epll;
634 clk_fout_apll.rate = apll;
636 printk(KERN_INFO "S5P6440: PLL settings, A=%ld.%ldMHz, M=%ld.%ldMHz," \
638 print_mhz(apll), print_mhz(mpll), print_mhz(epll));
640 fclk = clk_get_rate(&clk_armclk.clk);
641 hclk = clk_get_rate(&clk_hclk.clk);
642 pclk = hclk / GET_DIV(clkdiv0, S5P_CLKDIV0_PCLK);
644 if (__raw_readl(S5P_OTHERS) & S5P_OTHERS_HCLK_LOW_SEL_MPLL) {
645 /* Asynchronous mode */
646 hclk_low = mpll / GET_DIV(clkdiv3, S5P_CLKDIV3_HCLK_LOW);
648 /* Synchronous mode */
649 hclk_low = apll / GET_DIV(clkdiv3, S5P_CLKDIV3_HCLK_LOW);
652 pclk_low = hclk_low / GET_DIV(clkdiv3, S5P_CLKDIV3_PCLK_LOW);
654 printk(KERN_INFO "S5P6440: HCLK=%ld.%ldMHz, HCLK_LOW=%ld.%ldMHz," \
655 " PCLK=%ld.%ldMHz, PCLK_LOW=%ld.%ldMHz\n",
656 print_mhz(hclk), print_mhz(hclk_low),
657 print_mhz(pclk), print_mhz(pclk_low));
662 clk_h_low.rate = hclk_low;
663 clk_p_low.rate = pclk_low;
665 for (ptr = 0; ptr < ARRAY_SIZE(clksrcs); ptr++)
666 s3c_set_clksrc(&clksrcs[ptr], true);
669 static struct clk *clks[] __initdata = {
677 void __init s5p6440_register_clocks(void)
683 ret = s3c24xx_register_clocks(clks, ARRAY_SIZE(clks));
685 printk(KERN_ERR "Failed to register %u clocks\n", ret);
687 for (ptr = 0; ptr < ARRAY_SIZE(sysclks); ptr++)
688 s3c_register_clksrc(sysclks[ptr], 1);
690 s3c_register_clksrc(clksrcs, ARRAY_SIZE(clksrcs));
691 s3c_register_clocks(init_clocks, ARRAY_SIZE(init_clocks));
693 clkp = init_clocks_disable;
694 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
696 ret = s3c24xx_register_clock(clkp);
698 printk(KERN_ERR "Failed to register clock %s (%d)\n",
701 (clkp->enable)(clkp, 0);