1 /* linux/arch/arm/plat-s3c64xx/clock.c
3 * Copyright 2008 Openmoko, Inc.
4 * Copyright 2008 Simtec Electronics
5 * Ben Dooks <ben@simtec.co.uk>
6 * http://armlinux.simtec.co.uk/
8 * S3C64XX Base clock support
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
23 #include <mach/hardware.h>
26 #include <mach/regs-sys.h>
27 #include <mach/regs-clock.h>
31 #include <plat/devs.h>
32 #include <plat/cpu-freq.h>
33 #include <plat/clock.h>
34 #include <plat/clock-clksrc.h>
36 /* fin_apll, fin_mpll and fin_epll are all the same clock, which we call
37 * ext_xtal_mux for want of an actual name from the manual.
40 static struct clk clk_ext_xtal_mux
= {
44 #define clk_fin_apll clk_ext_xtal_mux
45 #define clk_fin_mpll clk_ext_xtal_mux
46 #define clk_fin_epll clk_ext_xtal_mux
48 #define clk_fout_mpll clk_mpll
49 #define clk_fout_epll clk_epll
56 struct clk clk_27m
= {
61 static int clk_48m_ctrl(struct clk
*clk
, int enable
)
66 /* can't rely on clock lock, this register has other usages */
67 local_irq_save(flags
);
69 val
= __raw_readl(S3C64XX_OTHERS
);
71 val
|= S3C64XX_OTHERS_USBMASK
;
73 val
&= ~S3C64XX_OTHERS_USBMASK
;
75 __raw_writel(val
, S3C64XX_OTHERS
);
76 local_irq_restore(flags
);
81 struct clk clk_48m
= {
84 .enable
= clk_48m_ctrl
,
87 struct clk clk_xusbxti
= {
92 static int inline s3c64xx_gate(void __iomem
*reg
,
96 unsigned int ctrlbit
= clk
->ctrlbit
;
99 con
= __raw_readl(reg
);
106 __raw_writel(con
, reg
);
110 static int s3c64xx_pclk_ctrl(struct clk
*clk
, int enable
)
112 return s3c64xx_gate(S3C_PCLK_GATE
, clk
, enable
);
115 static int s3c64xx_hclk_ctrl(struct clk
*clk
, int enable
)
117 return s3c64xx_gate(S3C_HCLK_GATE
, clk
, enable
);
120 int s3c64xx_sclk_ctrl(struct clk
*clk
, int enable
)
122 return s3c64xx_gate(S3C_SCLK_GATE
, clk
, enable
);
125 static struct clk init_clocks_off
[] = {
132 .enable
= s3c64xx_pclk_ctrl
,
133 .ctrlbit
= S3C_CLKCON_PCLK_RTC
,
137 .enable
= s3c64xx_pclk_ctrl
,
138 .ctrlbit
= S3C_CLKCON_PCLK_TSADC
,
142 .enable
= s3c64xx_pclk_ctrl
,
143 .ctrlbit
= S3C_CLKCON_PCLK_IIC
,
146 .devname
= "s3c2440-i2c.1",
148 .enable
= s3c64xx_pclk_ctrl
,
149 .ctrlbit
= S3C6410_CLKCON_PCLK_I2C1
,
152 .devname
= "samsung-i2s.0",
154 .enable
= s3c64xx_pclk_ctrl
,
155 .ctrlbit
= S3C_CLKCON_PCLK_IIS0
,
158 .devname
= "samsung-i2s.1",
160 .enable
= s3c64xx_pclk_ctrl
,
161 .ctrlbit
= S3C_CLKCON_PCLK_IIS1
,
163 #ifdef CONFIG_CPU_S3C6410
166 .enable
= s3c64xx_pclk_ctrl
,
167 .ctrlbit
= S3C6410_CLKCON_PCLK_IIS2
,
172 .enable
= s3c64xx_pclk_ctrl
,
173 .ctrlbit
= S3C_CLKCON_PCLK_KEYPAD
,
176 .devname
= "s3c64xx-spi.0",
178 .enable
= s3c64xx_pclk_ctrl
,
179 .ctrlbit
= S3C_CLKCON_PCLK_SPI0
,
182 .devname
= "s3c64xx-spi.1",
184 .enable
= s3c64xx_pclk_ctrl
,
185 .ctrlbit
= S3C_CLKCON_PCLK_SPI1
,
188 .devname
= "s3c64xx-spi.0",
190 .enable
= s3c64xx_sclk_ctrl
,
191 .ctrlbit
= S3C_CLKCON_SCLK_SPI0_48
,
194 .devname
= "s3c64xx-spi.1",
196 .enable
= s3c64xx_sclk_ctrl
,
197 .ctrlbit
= S3C_CLKCON_SCLK_SPI1_48
,
200 .devname
= "s3c-sdhci.0",
202 .enable
= s3c64xx_sclk_ctrl
,
203 .ctrlbit
= S3C_CLKCON_SCLK_MMC0_48
,
206 .devname
= "s3c-sdhci.1",
208 .enable
= s3c64xx_sclk_ctrl
,
209 .ctrlbit
= S3C_CLKCON_SCLK_MMC1_48
,
212 .devname
= "s3c-sdhci.2",
214 .enable
= s3c64xx_sclk_ctrl
,
215 .ctrlbit
= S3C_CLKCON_SCLK_MMC2_48
,
219 .enable
= s3c64xx_hclk_ctrl
,
220 .ctrlbit
= S3C_CLKCON_HCLK_DMA0
,
224 .enable
= s3c64xx_hclk_ctrl
,
225 .ctrlbit
= S3C_CLKCON_HCLK_DMA1
,
229 static struct clk init_clocks
[] = {
233 .enable
= s3c64xx_hclk_ctrl
,
234 .ctrlbit
= S3C_CLKCON_HCLK_LCD
,
238 .enable
= s3c64xx_pclk_ctrl
,
239 .ctrlbit
= S3C_CLKCON_PCLK_GPIO
,
243 .enable
= s3c64xx_hclk_ctrl
,
244 .ctrlbit
= S3C_CLKCON_HCLK_UHOST
,
247 .devname
= "s3c-sdhci.0",
249 .enable
= s3c64xx_hclk_ctrl
,
250 .ctrlbit
= S3C_CLKCON_HCLK_HSMMC0
,
253 .devname
= "s3c-sdhci.1",
255 .enable
= s3c64xx_hclk_ctrl
,
256 .ctrlbit
= S3C_CLKCON_HCLK_HSMMC1
,
259 .devname
= "s3c-sdhci.2",
261 .enable
= s3c64xx_hclk_ctrl
,
262 .ctrlbit
= S3C_CLKCON_HCLK_HSMMC2
,
266 .enable
= s3c64xx_hclk_ctrl
,
267 .ctrlbit
= S3C_CLKCON_HCLK_USB
,
271 .enable
= s3c64xx_pclk_ctrl
,
272 .ctrlbit
= S3C_CLKCON_PCLK_PWM
,
275 .devname
= "s3c6400-uart.0",
277 .enable
= s3c64xx_pclk_ctrl
,
278 .ctrlbit
= S3C_CLKCON_PCLK_UART0
,
281 .devname
= "s3c6400-uart.1",
283 .enable
= s3c64xx_pclk_ctrl
,
284 .ctrlbit
= S3C_CLKCON_PCLK_UART1
,
287 .devname
= "s3c6400-uart.2",
289 .enable
= s3c64xx_pclk_ctrl
,
290 .ctrlbit
= S3C_CLKCON_PCLK_UART2
,
293 .devname
= "s3c6400-uart.3",
295 .enable
= s3c64xx_pclk_ctrl
,
296 .ctrlbit
= S3C_CLKCON_PCLK_UART3
,
300 .ctrlbit
= S3C_CLKCON_PCLK_WDT
,
304 .ctrlbit
= S3C_CLKCON_PCLK_AC97
,
308 .enable
= s3c64xx_hclk_ctrl
,
309 .ctrlbit
= S3C_CLKCON_HCLK_IHOST
,
314 static struct clk clk_fout_apll
= {
318 static struct clk
*clk_src_apll_list
[] = {
320 [1] = &clk_fout_apll
,
323 static struct clksrc_sources clk_src_apll
= {
324 .sources
= clk_src_apll_list
,
325 .nr_sources
= ARRAY_SIZE(clk_src_apll_list
),
328 static struct clksrc_clk clk_mout_apll
= {
332 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 0, .size
= 1 },
333 .sources
= &clk_src_apll
,
336 static struct clk
*clk_src_epll_list
[] = {
338 [1] = &clk_fout_epll
,
341 static struct clksrc_sources clk_src_epll
= {
342 .sources
= clk_src_epll_list
,
343 .nr_sources
= ARRAY_SIZE(clk_src_epll_list
),
346 static struct clksrc_clk clk_mout_epll
= {
350 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 2, .size
= 1 },
351 .sources
= &clk_src_epll
,
354 static struct clk
*clk_src_mpll_list
[] = {
356 [1] = &clk_fout_mpll
,
359 static struct clksrc_sources clk_src_mpll
= {
360 .sources
= clk_src_mpll_list
,
361 .nr_sources
= ARRAY_SIZE(clk_src_mpll_list
),
364 static struct clksrc_clk clk_mout_mpll
= {
368 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 1, .size
= 1 },
369 .sources
= &clk_src_mpll
,
372 static unsigned int armclk_mask
;
374 static unsigned long s3c64xx_clk_arm_get_rate(struct clk
*clk
)
376 unsigned long rate
= clk_get_rate(clk
->parent
);
379 /* divisor mask starts at bit0, so no need to shift */
380 clkdiv
= __raw_readl(S3C_CLK_DIV0
) & armclk_mask
;
382 return rate
/ (clkdiv
+ 1);
385 static unsigned long s3c64xx_clk_arm_round_rate(struct clk
*clk
,
388 unsigned long parent
= clk_get_rate(clk
->parent
);
394 div
= (parent
/ rate
) - 1;
395 if (div
> armclk_mask
)
398 return parent
/ (div
+ 1);
401 static int s3c64xx_clk_arm_set_rate(struct clk
*clk
, unsigned long rate
)
403 unsigned long parent
= clk_get_rate(clk
->parent
);
407 if (rate
< parent
/ (armclk_mask
+ 1))
410 rate
= clk_round_rate(clk
, rate
);
411 div
= clk_get_rate(clk
->parent
) / rate
;
413 val
= __raw_readl(S3C_CLK_DIV0
);
416 __raw_writel(val
, S3C_CLK_DIV0
);
422 static struct clk clk_arm
= {
424 .parent
= &clk_mout_apll
.clk
,
425 .ops
= &(struct clk_ops
) {
426 .get_rate
= s3c64xx_clk_arm_get_rate
,
427 .set_rate
= s3c64xx_clk_arm_set_rate
,
428 .round_rate
= s3c64xx_clk_arm_round_rate
,
432 static unsigned long s3c64xx_clk_doutmpll_get_rate(struct clk
*clk
)
434 unsigned long rate
= clk_get_rate(clk
->parent
);
436 printk(KERN_DEBUG
"%s: parent is %ld\n", __func__
, rate
);
438 if (__raw_readl(S3C_CLK_DIV0
) & S3C6400_CLKDIV0_MPLL_MASK
)
444 static struct clk_ops clk_dout_ops
= {
445 .get_rate
= s3c64xx_clk_doutmpll_get_rate
,
448 static struct clk clk_dout_mpll
= {
450 .parent
= &clk_mout_mpll
.clk
,
451 .ops
= &clk_dout_ops
,
454 static struct clk
*clkset_spi_mmc_list
[] = {
461 static struct clksrc_sources clkset_spi_mmc
= {
462 .sources
= clkset_spi_mmc_list
,
463 .nr_sources
= ARRAY_SIZE(clkset_spi_mmc_list
),
466 static struct clk
*clkset_irda_list
[] = {
473 static struct clksrc_sources clkset_irda
= {
474 .sources
= clkset_irda_list
,
475 .nr_sources
= ARRAY_SIZE(clkset_irda_list
),
478 static struct clk
*clkset_uart_list
[] = {
485 static struct clksrc_sources clkset_uart
= {
486 .sources
= clkset_uart_list
,
487 .nr_sources
= ARRAY_SIZE(clkset_uart_list
),
490 static struct clk
*clkset_uhost_list
[] = {
497 static struct clksrc_sources clkset_uhost
= {
498 .sources
= clkset_uhost_list
,
499 .nr_sources
= ARRAY_SIZE(clkset_uhost_list
),
502 /* The peripheral clocks are all controlled via clocksource followed
503 * by an optional divider and gate stage. We currently roll this into
504 * one clock which hides the intermediate clock from the mux.
506 * Note, the JPEG clock can only be an even divider...
508 * The scaler and LCD clocks depend on the S3C64XX version, and also
509 * have a common parent divisor so are not included here.
512 /* clocks that feed other parts of the clock source tree */
514 static struct clk clk_iis_cd0
= {
515 .name
= "iis_cdclk0",
518 static struct clk clk_iis_cd1
= {
519 .name
= "iis_cdclk1",
522 static struct clk clk_iisv4_cd
= {
523 .name
= "iis_cdclk_v4",
526 static struct clk clk_pcm_cd
= {
530 static struct clk
*clkset_audio0_list
[] = {
531 [0] = &clk_mout_epll
.clk
,
532 [1] = &clk_dout_mpll
,
538 static struct clksrc_sources clkset_audio0
= {
539 .sources
= clkset_audio0_list
,
540 .nr_sources
= ARRAY_SIZE(clkset_audio0_list
),
543 static struct clk
*clkset_audio1_list
[] = {
544 [0] = &clk_mout_epll
.clk
,
545 [1] = &clk_dout_mpll
,
551 static struct clksrc_sources clkset_audio1
= {
552 .sources
= clkset_audio1_list
,
553 .nr_sources
= ARRAY_SIZE(clkset_audio1_list
),
556 static struct clk
*clkset_audio2_list
[] = {
557 [0] = &clk_mout_epll
.clk
,
558 [1] = &clk_dout_mpll
,
564 static struct clksrc_sources clkset_audio2
= {
565 .sources
= clkset_audio2_list
,
566 .nr_sources
= ARRAY_SIZE(clkset_audio2_list
),
569 static struct clk
*clkset_camif_list
[] = {
573 static struct clksrc_sources clkset_camif
= {
574 .sources
= clkset_camif_list
,
575 .nr_sources
= ARRAY_SIZE(clkset_camif_list
),
578 static struct clksrc_clk clksrcs
[] = {
582 .devname
= "s3c-sdhci.0",
583 .ctrlbit
= S3C_CLKCON_SCLK_MMC0
,
584 .enable
= s3c64xx_sclk_ctrl
,
586 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 18, .size
= 2 },
587 .reg_div
= { .reg
= S3C_CLK_DIV1
, .shift
= 0, .size
= 4 },
588 .sources
= &clkset_spi_mmc
,
592 .devname
= "s3c-sdhci.1",
593 .ctrlbit
= S3C_CLKCON_SCLK_MMC1
,
594 .enable
= s3c64xx_sclk_ctrl
,
596 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 20, .size
= 2 },
597 .reg_div
= { .reg
= S3C_CLK_DIV1
, .shift
= 4, .size
= 4 },
598 .sources
= &clkset_spi_mmc
,
602 .devname
= "s3c-sdhci.2",
603 .ctrlbit
= S3C_CLKCON_SCLK_MMC2
,
604 .enable
= s3c64xx_sclk_ctrl
,
606 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 22, .size
= 2 },
607 .reg_div
= { .reg
= S3C_CLK_DIV1
, .shift
= 8, .size
= 4 },
608 .sources
= &clkset_spi_mmc
,
611 .name
= "usb-bus-host",
612 .ctrlbit
= S3C_CLKCON_SCLK_UHOST
,
613 .enable
= s3c64xx_sclk_ctrl
,
615 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 5, .size
= 2 },
616 .reg_div
= { .reg
= S3C_CLK_DIV1
, .shift
= 20, .size
= 4 },
617 .sources
= &clkset_uhost
,
621 .ctrlbit
= S3C_CLKCON_SCLK_UART
,
622 .enable
= s3c64xx_sclk_ctrl
,
624 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 13, .size
= 1 },
625 .reg_div
= { .reg
= S3C_CLK_DIV2
, .shift
= 16, .size
= 4 },
626 .sources
= &clkset_uart
,
628 /* Where does UCLK0 come from? */
631 .devname
= "s3c64xx-spi.0",
632 .ctrlbit
= S3C_CLKCON_SCLK_SPI0
,
633 .enable
= s3c64xx_sclk_ctrl
,
635 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 14, .size
= 2 },
636 .reg_div
= { .reg
= S3C_CLK_DIV2
, .shift
= 0, .size
= 4 },
637 .sources
= &clkset_spi_mmc
,
641 .devname
= "s3c64xx-spi.1",
642 .enable
= s3c64xx_sclk_ctrl
,
644 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 16, .size
= 2 },
645 .reg_div
= { .reg
= S3C_CLK_DIV2
, .shift
= 4, .size
= 4 },
646 .sources
= &clkset_spi_mmc
,
650 .devname
= "samsung-i2s.0",
651 .ctrlbit
= S3C_CLKCON_SCLK_AUDIO0
,
652 .enable
= s3c64xx_sclk_ctrl
,
654 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 7, .size
= 3 },
655 .reg_div
= { .reg
= S3C_CLK_DIV2
, .shift
= 8, .size
= 4 },
656 .sources
= &clkset_audio0
,
660 .devname
= "samsung-i2s.1",
661 .ctrlbit
= S3C_CLKCON_SCLK_AUDIO1
,
662 .enable
= s3c64xx_sclk_ctrl
,
664 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 10, .size
= 3 },
665 .reg_div
= { .reg
= S3C_CLK_DIV2
, .shift
= 12, .size
= 4 },
666 .sources
= &clkset_audio1
,
670 .devname
= "samsung-i2s.2",
671 .ctrlbit
= S3C6410_CLKCON_SCLK_AUDIO2
,
672 .enable
= s3c64xx_sclk_ctrl
,
674 .reg_src
= { .reg
= S3C6410_CLK_SRC2
, .shift
= 0, .size
= 3 },
675 .reg_div
= { .reg
= S3C_CLK_DIV2
, .shift
= 24, .size
= 4 },
676 .sources
= &clkset_audio2
,
680 .ctrlbit
= S3C_CLKCON_SCLK_IRDA
,
681 .enable
= s3c64xx_sclk_ctrl
,
683 .reg_src
= { .reg
= S3C_CLK_SRC
, .shift
= 24, .size
= 2 },
684 .reg_div
= { .reg
= S3C_CLK_DIV2
, .shift
= 20, .size
= 4 },
685 .sources
= &clkset_irda
,
689 .ctrlbit
= S3C_CLKCON_SCLK_CAM
,
690 .enable
= s3c64xx_sclk_ctrl
,
692 .reg_div
= { .reg
= S3C_CLK_DIV0
, .shift
= 20, .size
= 4 },
693 .reg_src
= { .reg
= NULL
, .shift
= 0, .size
= 0 },
694 .sources
= &clkset_camif
,
698 /* Clock initialisation code */
700 static struct clksrc_clk
*init_parents
[] = {
706 #define GET_DIV(clk, field) ((((clk) & field##_MASK) >> field##_SHIFT) + 1)
708 void __init_or_cpufreq
s3c6400_setup_clocks(void)
710 struct clk
*xtal_clk
;
722 printk(KERN_DEBUG
"%s: registering clocks\n", __func__
);
724 clkdiv0
= __raw_readl(S3C_CLK_DIV0
);
725 printk(KERN_DEBUG
"%s: clkdiv0 = %08x\n", __func__
, clkdiv0
);
727 xtal_clk
= clk_get(NULL
, "xtal");
728 BUG_ON(IS_ERR(xtal_clk
));
730 xtal
= clk_get_rate(xtal_clk
);
733 printk(KERN_DEBUG
"%s: xtal is %ld\n", __func__
, xtal
);
735 /* For now assume the mux always selects the crystal */
736 clk_ext_xtal_mux
.parent
= xtal_clk
;
738 epll
= s3c6400_get_epll(xtal
);
739 mpll
= s3c6400_get_pll(xtal
, __raw_readl(S3C_MPLL_CON
));
740 apll
= s3c6400_get_pll(xtal
, __raw_readl(S3C_APLL_CON
));
744 printk(KERN_INFO
"S3C64XX: PLL settings, A=%ld, M=%ld, E=%ld\n",
747 hclk2
= mpll
/ GET_DIV(clkdiv0
, S3C6400_CLKDIV0_HCLK2
);
748 hclk
= hclk2
/ GET_DIV(clkdiv0
, S3C6400_CLKDIV0_HCLK
);
749 pclk
= hclk2
/ GET_DIV(clkdiv0
, S3C6400_CLKDIV0_PCLK
);
751 printk(KERN_INFO
"S3C64XX: HCLK2=%ld, HCLK=%ld, PCLK=%ld\n",
754 clk_fout_mpll
.rate
= mpll
;
755 clk_fout_epll
.rate
= epll
;
756 clk_fout_apll
.rate
= apll
;
763 for (ptr
= 0; ptr
< ARRAY_SIZE(init_parents
); ptr
++)
764 s3c_set_clksrc(init_parents
[ptr
], true);
766 for (ptr
= 0; ptr
< ARRAY_SIZE(clksrcs
); ptr
++)
767 s3c_set_clksrc(&clksrcs
[ptr
], true);
770 static struct clk
*clks1
[] __initdata
= {
782 static struct clk
*clks
[] __initdata
= {
792 * s3c64xx_register_clocks - register clocks for s3c6400 and s3c6410
793 * @xtal: The rate for the clock crystal feeding the PLLs.
794 * @armclk_divlimit: Divisor mask for ARMCLK.
796 * Register the clocks for the S3C6400 and S3C6410 SoC range, such
797 * as ARMCLK as well as the necessary parent clocks.
799 * This call does not setup the clocks, which is left to the
800 * s3c6400_setup_clocks() call which may be needed by the cpufreq
801 * or resume code to re-set the clocks if the bootloader has changed
804 void __init
s3c64xx_register_clocks(unsigned long xtal
,
805 unsigned armclk_divlimit
)
807 armclk_mask
= armclk_divlimit
;
809 s3c24xx_register_baseclocks(xtal
);
810 s3c24xx_register_clocks(clks
, ARRAY_SIZE(clks
));
812 s3c_register_clocks(init_clocks
, ARRAY_SIZE(init_clocks
));
814 s3c_register_clocks(init_clocks_off
, ARRAY_SIZE(init_clocks_off
));
815 s3c_disable_clocks(init_clocks_off
, ARRAY_SIZE(init_clocks_off
));
817 s3c24xx_register_clocks(clks1
, ARRAY_SIZE(clks1
));
818 s3c_register_clksrc(clksrcs
, ARRAY_SIZE(clksrcs
));