1 /* linux/arch/arm/plat-s3c64xx/s3c6400-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 * S3C6400 based common 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/kernel.h>
18 #include <linux/list.h>
19 #include <linux/errno.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
22 #include <linux/sysdev.h>
25 #include <mach/hardware.h>
28 #include <plat/cpu-freq.h>
30 #include <plat/regs-clock.h>
31 #include <plat/clock.h>
35 /* fin_apll, fin_mpll and fin_epll are all the same clock, which we call
36 * ext_xtal_mux for want of an actual name from the manual.
39 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
51 unsigned int nr_sources
;
60 struct clk_sources
*sources
;
62 unsigned int divider_shift
;
63 void __iomem
*reg_divider
;
66 struct clk clk_fout_apll
= {
71 static struct clk
*clk_src_apll_list
[] = {
76 static struct clk_sources clk_src_apll
= {
77 .sources
= clk_src_apll_list
,
78 .nr_sources
= ARRAY_SIZE(clk_src_apll_list
),
81 struct clksrc_clk clk_mout_apll
= {
86 .shift
= S3C6400_CLKSRC_APLL_MOUT_SHIFT
,
87 .mask
= S3C6400_CLKSRC_APLL_MOUT
,
88 .sources
= &clk_src_apll
,
91 struct clk clk_fout_epll
= {
96 static struct clk
*clk_src_epll_list
[] = {
101 static struct clk_sources clk_src_epll
= {
102 .sources
= clk_src_epll_list
,
103 .nr_sources
= ARRAY_SIZE(clk_src_epll_list
),
106 struct clksrc_clk clk_mout_epll
= {
111 .shift
= S3C6400_CLKSRC_EPLL_MOUT_SHIFT
,
112 .mask
= S3C6400_CLKSRC_EPLL_MOUT
,
113 .sources
= &clk_src_epll
,
116 static struct clk
*clk_src_mpll_list
[] = {
118 [1] = &clk_fout_mpll
,
121 static struct clk_sources clk_src_mpll
= {
122 .sources
= clk_src_mpll_list
,
123 .nr_sources
= ARRAY_SIZE(clk_src_mpll_list
),
126 struct clksrc_clk clk_mout_mpll
= {
131 .shift
= S3C6400_CLKSRC_MPLL_MOUT_SHIFT
,
132 .mask
= S3C6400_CLKSRC_MPLL_MOUT
,
133 .sources
= &clk_src_mpll
,
136 static unsigned long s3c64xx_clk_doutmpll_get_rate(struct clk
*clk
)
138 unsigned long rate
= clk_get_rate(clk
->parent
);
140 printk(KERN_DEBUG
"%s: parent is %ld\n", __func__
, rate
);
142 if (__raw_readl(S3C_CLK_DIV0
) & S3C6400_CLKDIV0_MPLL_MASK
)
148 struct clk clk_dout_mpll
= {
151 .parent
= &clk_mout_mpll
.clk
,
152 .get_rate
= s3c64xx_clk_doutmpll_get_rate
,
155 static struct clk
*clkset_spi_mmc_list
[] = {
162 static struct clk_sources clkset_spi_mmc
= {
163 .sources
= clkset_spi_mmc_list
,
164 .nr_sources
= ARRAY_SIZE(clkset_spi_mmc_list
),
167 static struct clk
*clkset_irda_list
[] = {
174 static struct clk_sources clkset_irda
= {
175 .sources
= clkset_irda_list
,
176 .nr_sources
= ARRAY_SIZE(clkset_irda_list
),
179 static struct clk
*clkset_uart_list
[] = {
186 static struct clk_sources clkset_uart
= {
187 .sources
= clkset_uart_list
,
188 .nr_sources
= ARRAY_SIZE(clkset_uart_list
),
191 static struct clk
*clkset_uhost_list
[] = {
198 static struct clk_sources clkset_uhost
= {
199 .sources
= clkset_uhost_list
,
200 .nr_sources
= ARRAY_SIZE(clkset_uhost_list
),
204 /* The peripheral clocks are all controlled via clocksource followed
205 * by an optional divider and gate stage. We currently roll this into
206 * one clock which hides the intermediate clock from the mux.
208 * Note, the JPEG clock can only be an even divider...
210 * The scaler and LCD clocks depend on the S3C64XX version, and also
211 * have a common parent divisor so are not included here.
214 static inline struct clksrc_clk
*to_clksrc(struct clk
*clk
)
216 return container_of(clk
, struct clksrc_clk
, clk
);
219 static unsigned long s3c64xx_getrate_clksrc(struct clk
*clk
)
221 struct clksrc_clk
*sclk
= to_clksrc(clk
);
222 unsigned long rate
= clk_get_rate(clk
->parent
);
223 u32 clkdiv
= __raw_readl(sclk
->reg_divider
);
225 clkdiv
>>= sclk
->divider_shift
;
233 static int s3c64xx_setrate_clksrc(struct clk
*clk
, unsigned long rate
)
235 struct clksrc_clk
*sclk
= to_clksrc(clk
);
236 void __iomem
*reg
= sclk
->reg_divider
;
240 rate
= clk_round_rate(clk
, rate
);
241 div
= clk_get_rate(clk
->parent
) / rate
;
243 val
= __raw_readl(reg
);
245 val
|= (rate
- 1) << sclk
->shift
;
246 __raw_writel(val
, reg
);
251 static int s3c64xx_setparent_clksrc(struct clk
*clk
, struct clk
*parent
)
253 struct clksrc_clk
*sclk
= to_clksrc(clk
);
254 struct clk_sources
*srcs
= sclk
->sources
;
255 u32 clksrc
= __raw_readl(S3C_CLK_SRC
);
259 for (ptr
= 0; ptr
< srcs
->nr_sources
; ptr
++)
260 if (srcs
->sources
[ptr
] == parent
) {
266 clksrc
&= ~sclk
->mask
;
267 clksrc
|= src_nr
<< sclk
->shift
;
269 __raw_writel(clksrc
, S3C_CLK_SRC
);
276 static unsigned long s3c64xx_roundrate_clksrc(struct clk
*clk
,
279 unsigned long parent_rate
= clk_get_rate(clk
->parent
);
282 if (rate
> parent_rate
)
285 div
= rate
/ parent_rate
;
292 rate
= parent_rate
/ div
;
298 static struct clksrc_clk clk_mmc0
= {
302 .ctrlbit
= S3C_CLKCON_SCLK_MMC0
,
303 .enable
= s3c64xx_sclk_ctrl
,
304 .set_parent
= s3c64xx_setparent_clksrc
,
305 .get_rate
= s3c64xx_getrate_clksrc
,
306 .set_rate
= s3c64xx_setrate_clksrc
,
307 .round_rate
= s3c64xx_roundrate_clksrc
,
309 .shift
= S3C6400_CLKSRC_MMC0_SHIFT
,
310 .mask
= S3C6400_CLKSRC_MMC0_MASK
,
311 .sources
= &clkset_spi_mmc
,
312 .divider_shift
= S3C6400_CLKDIV1_MMC0_SHIFT
,
313 .reg_divider
= S3C_CLK_DIV1
,
316 static struct clksrc_clk clk_mmc1
= {
320 .ctrlbit
= S3C_CLKCON_SCLK_MMC1
,
321 .enable
= s3c64xx_sclk_ctrl
,
322 .get_rate
= s3c64xx_getrate_clksrc
,
323 .set_rate
= s3c64xx_setrate_clksrc
,
324 .set_parent
= s3c64xx_setparent_clksrc
,
325 .round_rate
= s3c64xx_roundrate_clksrc
,
327 .shift
= S3C6400_CLKSRC_MMC1_SHIFT
,
328 .mask
= S3C6400_CLKSRC_MMC1_MASK
,
329 .sources
= &clkset_spi_mmc
,
330 .divider_shift
= S3C6400_CLKDIV1_MMC1_SHIFT
,
331 .reg_divider
= S3C_CLK_DIV1
,
334 static struct clksrc_clk clk_mmc2
= {
338 .ctrlbit
= S3C_CLKCON_SCLK_MMC2
,
339 .enable
= s3c64xx_sclk_ctrl
,
340 .get_rate
= s3c64xx_getrate_clksrc
,
341 .set_rate
= s3c64xx_setrate_clksrc
,
342 .set_parent
= s3c64xx_setparent_clksrc
,
343 .round_rate
= s3c64xx_roundrate_clksrc
,
345 .shift
= S3C6400_CLKSRC_MMC2_SHIFT
,
346 .mask
= S3C6400_CLKSRC_MMC2_MASK
,
347 .sources
= &clkset_spi_mmc
,
348 .divider_shift
= S3C6400_CLKDIV1_MMC2_SHIFT
,
349 .reg_divider
= S3C_CLK_DIV1
,
352 static struct clksrc_clk clk_usbhost
= {
354 .name
= "usb-bus-host",
356 .ctrlbit
= S3C_CLKCON_SCLK_UHOST
,
357 .enable
= s3c64xx_sclk_ctrl
,
358 .set_parent
= s3c64xx_setparent_clksrc
,
359 .get_rate
= s3c64xx_getrate_clksrc
,
360 .set_rate
= s3c64xx_setrate_clksrc
,
361 .round_rate
= s3c64xx_roundrate_clksrc
,
363 .shift
= S3C6400_CLKSRC_UHOST_SHIFT
,
364 .mask
= S3C6400_CLKSRC_UHOST_MASK
,
365 .sources
= &clkset_uhost
,
366 .divider_shift
= S3C6400_CLKDIV1_UHOST_SHIFT
,
367 .reg_divider
= S3C_CLK_DIV1
,
370 static struct clksrc_clk clk_uart_uclk1
= {
374 .ctrlbit
= S3C_CLKCON_SCLK_UART
,
375 .enable
= s3c64xx_sclk_ctrl
,
376 .set_parent
= s3c64xx_setparent_clksrc
,
377 .get_rate
= s3c64xx_getrate_clksrc
,
378 .set_rate
= s3c64xx_setrate_clksrc
,
379 .round_rate
= s3c64xx_roundrate_clksrc
,
381 .shift
= S3C6400_CLKSRC_UART_SHIFT
,
382 .mask
= S3C6400_CLKSRC_UART_MASK
,
383 .sources
= &clkset_uart
,
384 .divider_shift
= S3C6400_CLKDIV2_UART_SHIFT
,
385 .reg_divider
= S3C_CLK_DIV2
,
388 /* Where does UCLK0 come from? */
390 static struct clksrc_clk clk_spi0
= {
394 .ctrlbit
= S3C_CLKCON_SCLK_SPI0
,
395 .enable
= s3c64xx_sclk_ctrl
,
396 .set_parent
= s3c64xx_setparent_clksrc
,
397 .get_rate
= s3c64xx_getrate_clksrc
,
398 .set_rate
= s3c64xx_setrate_clksrc
,
399 .round_rate
= s3c64xx_roundrate_clksrc
,
401 .shift
= S3C6400_CLKSRC_SPI0_SHIFT
,
402 .mask
= S3C6400_CLKSRC_SPI0_MASK
,
403 .sources
= &clkset_spi_mmc
,
404 .divider_shift
= S3C6400_CLKDIV2_SPI0_SHIFT
,
405 .reg_divider
= S3C_CLK_DIV2
,
408 static struct clksrc_clk clk_spi1
= {
412 .ctrlbit
= S3C_CLKCON_SCLK_SPI1
,
413 .enable
= s3c64xx_sclk_ctrl
,
414 .set_parent
= s3c64xx_setparent_clksrc
,
415 .get_rate
= s3c64xx_getrate_clksrc
,
416 .set_rate
= s3c64xx_setrate_clksrc
,
417 .round_rate
= s3c64xx_roundrate_clksrc
,
419 .shift
= S3C6400_CLKSRC_SPI1_SHIFT
,
420 .mask
= S3C6400_CLKSRC_SPI1_MASK
,
421 .sources
= &clkset_spi_mmc
,
422 .divider_shift
= S3C6400_CLKDIV2_SPI1_SHIFT
,
423 .reg_divider
= S3C_CLK_DIV2
,
426 static struct clk clk_iis_cd0
= {
427 .name
= "iis_cdclk0",
431 static struct clk clk_iis_cd1
= {
432 .name
= "iis_cdclk1",
436 static struct clk clk_pcm_cd
= {
441 static struct clk
*clkset_audio0_list
[] = {
442 [0] = &clk_mout_epll
.clk
,
443 [1] = &clk_dout_mpll
,
449 static struct clk_sources clkset_audio0
= {
450 .sources
= clkset_audio0_list
,
451 .nr_sources
= ARRAY_SIZE(clkset_audio0_list
),
454 static struct clksrc_clk clk_audio0
= {
458 .ctrlbit
= S3C_CLKCON_SCLK_AUDIO0
,
459 .enable
= s3c64xx_sclk_ctrl
,
460 .set_parent
= s3c64xx_setparent_clksrc
,
461 .get_rate
= s3c64xx_getrate_clksrc
,
462 .set_rate
= s3c64xx_setrate_clksrc
,
463 .round_rate
= s3c64xx_roundrate_clksrc
,
465 .shift
= S3C6400_CLKSRC_AUDIO0_SHIFT
,
466 .mask
= S3C6400_CLKSRC_AUDIO0_MASK
,
467 .sources
= &clkset_audio0
,
468 .divider_shift
= S3C6400_CLKDIV2_AUDIO0_SHIFT
,
469 .reg_divider
= S3C_CLK_DIV2
,
472 static struct clk
*clkset_audio1_list
[] = {
473 [0] = &clk_mout_epll
.clk
,
474 [1] = &clk_dout_mpll
,
480 static struct clk_sources clkset_audio1
= {
481 .sources
= clkset_audio1_list
,
482 .nr_sources
= ARRAY_SIZE(clkset_audio1_list
),
485 static struct clksrc_clk clk_audio1
= {
489 .ctrlbit
= S3C_CLKCON_SCLK_AUDIO1
,
490 .enable
= s3c64xx_sclk_ctrl
,
491 .set_parent
= s3c64xx_setparent_clksrc
,
492 .get_rate
= s3c64xx_getrate_clksrc
,
493 .set_rate
= s3c64xx_setrate_clksrc
,
494 .round_rate
= s3c64xx_roundrate_clksrc
,
496 .shift
= S3C6400_CLKSRC_AUDIO1_SHIFT
,
497 .mask
= S3C6400_CLKSRC_AUDIO1_MASK
,
498 .sources
= &clkset_audio1
,
499 .divider_shift
= S3C6400_CLKDIV2_AUDIO1_SHIFT
,
500 .reg_divider
= S3C_CLK_DIV2
,
503 static struct clksrc_clk clk_irda
= {
507 .ctrlbit
= S3C_CLKCON_SCLK_IRDA
,
508 .enable
= s3c64xx_sclk_ctrl
,
509 .set_parent
= s3c64xx_setparent_clksrc
,
510 .get_rate
= s3c64xx_getrate_clksrc
,
511 .set_rate
= s3c64xx_setrate_clksrc
,
512 .round_rate
= s3c64xx_roundrate_clksrc
,
514 .shift
= S3C6400_CLKSRC_IRDA_SHIFT
,
515 .mask
= S3C6400_CLKSRC_IRDA_MASK
,
516 .sources
= &clkset_irda
,
517 .divider_shift
= S3C6400_CLKDIV2_IRDA_SHIFT
,
518 .reg_divider
= S3C_CLK_DIV2
,
521 /* Clock initialisation code */
523 static struct clksrc_clk
*init_parents
[] = {
539 static void __init_or_cpufreq
s3c6400_set_clksrc(struct clksrc_clk
*clk
)
541 struct clk_sources
*srcs
= clk
->sources
;
542 u32 clksrc
= __raw_readl(S3C_CLK_SRC
);
545 clksrc
>>= clk
->shift
;
547 if (clksrc
> srcs
->nr_sources
|| !srcs
->sources
[clksrc
]) {
548 printk(KERN_ERR
"%s: bad source %d\n",
549 clk
->clk
.name
, clksrc
);
553 clk
->clk
.parent
= srcs
->sources
[clksrc
];
555 printk(KERN_INFO
"%s: source is %s (%d), rate is %ld\n",
556 clk
->clk
.name
, clk
->clk
.parent
->name
, clksrc
,
557 clk_get_rate(&clk
->clk
));
560 #define GET_DIV(clk, field) ((((clk) & field##_MASK) >> field##_SHIFT) + 1)
562 void __init_or_cpufreq
s3c6400_setup_clocks(void)
564 struct clk
*xtal_clk
;
576 printk(KERN_DEBUG
"%s: registering clocks\n", __func__
);
578 clkdiv0
= __raw_readl(S3C_CLK_DIV0
);
579 printk(KERN_DEBUG
"%s: clkdiv0 = %08x\n", __func__
, clkdiv0
);
581 xtal_clk
= clk_get(NULL
, "xtal");
582 BUG_ON(IS_ERR(xtal_clk
));
584 xtal
= clk_get_rate(xtal_clk
);
587 printk(KERN_DEBUG
"%s: xtal is %ld\n", __func__
, xtal
);
589 epll
= s3c6400_get_epll(xtal
);
590 mpll
= s3c6400_get_pll(xtal
, __raw_readl(S3C_MPLL_CON
));
591 apll
= s3c6400_get_pll(xtal
, __raw_readl(S3C_APLL_CON
));
595 printk(KERN_INFO
"S3C64XX: PLL settings, A=%ld, M=%ld, E=%ld\n",
598 hclk2
= mpll
/ GET_DIV(clkdiv0
, S3C6400_CLKDIV0_HCLK2
);
599 hclk
= hclk2
/ GET_DIV(clkdiv0
, S3C6400_CLKDIV0_HCLK
);
600 pclk
= hclk2
/ GET_DIV(clkdiv0
, S3C6400_CLKDIV0_PCLK
);
602 printk(KERN_INFO
"S3C64XX: HCLK2=%ld, HCLK=%ld, PCLK=%ld\n",
605 clk_fout_mpll
.rate
= mpll
;
606 clk_fout_epll
.rate
= epll
;
607 clk_fout_apll
.rate
= apll
;
613 for (ptr
= 0; ptr
< ARRAY_SIZE(init_parents
); ptr
++)
614 s3c6400_set_clksrc(init_parents
[ptr
]);
617 static struct clk
*clks
[] __initdata
= {
638 void __init
s3c6400_register_clocks(void)
644 for (ptr
= 0; ptr
< ARRAY_SIZE(clks
); ptr
++) {
646 ret
= s3c24xx_register_clock(clkp
);
648 printk(KERN_ERR
"Failed to register clock %s (%d)\n",
653 clk_mpll
.parent
= &clk_mout_mpll
.clk
;
654 clk_epll
.parent
= &clk_mout_epll
.clk
;