1 /* linux/arch/arm/mach-s3c2412/clock.c
3 * Copyright (c) 2006 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2412,S3C2413 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/delay.h>
33 #include <linux/serial_core.h>
36 #include <asm/mach/map.h>
38 #include <mach/hardware.h>
40 #include <plat/regs-serial.h>
41 #include <mach/regs-clock.h>
42 #include <mach/regs-gpio.h>
44 #include <plat/s3c2412.h>
45 #include <plat/clock.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 s3c2412_clkcon_enable(struct clk *clk, int enable)
59 unsigned int clocks = clk->ctrlbit;
62 clkcon = __raw_readl(S3C2410_CLKCON);
69 __raw_writel(clkcon, S3C2410_CLKCON);
74 static int s3c2412_upll_enable(struct clk *clk, int enable)
76 unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
77 unsigned long orig = upllcon;
80 upllcon |= S3C2412_PLLCON_OFF;
82 upllcon &= ~S3C2412_PLLCON_OFF;
84 __raw_writel(upllcon, S3C2410_UPLLCON);
86 /* allow ~150uS for the PLL to settle and lock */
88 if (enable && (orig & S3C2412_PLLCON_OFF))
94 /* clock selections */
96 /* CPU EXTCLK input */
97 static struct clk clk_ext = {
102 static struct clk clk_erefclk = {
107 static struct clk clk_urefclk = {
112 static int s3c2412_setparent_usysclk(struct clk *clk, struct clk *parent)
114 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
116 if (parent == &clk_urefclk)
117 clksrc &= ~S3C2412_CLKSRC_USYSCLK_UPLL;
118 else if (parent == &clk_upll)
119 clksrc |= S3C2412_CLKSRC_USYSCLK_UPLL;
123 clk->parent = parent;
125 __raw_writel(clksrc, S3C2412_CLKSRC);
129 static struct clk clk_usysclk = {
133 .set_parent = s3c2412_setparent_usysclk,
136 static struct clk clk_mrefclk = {
142 static struct clk clk_mdivclk = {
148 static int s3c2412_setparent_usbsrc(struct clk *clk, struct clk *parent)
150 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
152 if (parent == &clk_usysclk)
153 clksrc &= ~S3C2412_CLKSRC_USBCLK_HCLK;
154 else if (parent == &clk_h)
155 clksrc |= S3C2412_CLKSRC_USBCLK_HCLK;
159 clk->parent = parent;
161 __raw_writel(clksrc, S3C2412_CLKSRC);
165 static unsigned long s3c2412_roundrate_usbsrc(struct clk *clk,
168 unsigned long parent_rate = clk_get_rate(clk->parent);
171 if (rate > parent_rate)
174 div = parent_rate / rate;
178 return parent_rate / div;
181 static unsigned long s3c2412_getrate_usbsrc(struct clk *clk)
183 unsigned long parent_rate = clk_get_rate(clk->parent);
184 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
186 return parent_rate / ((div & S3C2412_CLKDIVN_USB48DIV) ? 2 : 1);
189 static int s3c2412_setrate_usbsrc(struct clk *clk, unsigned long rate)
191 unsigned long parent_rate = clk_get_rate(clk->parent);
192 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
194 rate = s3c2412_roundrate_usbsrc(clk, rate);
196 if ((parent_rate / rate) == 2)
197 clkdivn |= S3C2412_CLKDIVN_USB48DIV;
199 clkdivn &= ~S3C2412_CLKDIVN_USB48DIV;
201 __raw_writel(clkdivn, S3C2410_CLKDIVN);
205 static struct clk clk_usbsrc = {
208 .get_rate = s3c2412_getrate_usbsrc,
209 .set_rate = s3c2412_setrate_usbsrc,
210 .round_rate = s3c2412_roundrate_usbsrc,
211 .set_parent = s3c2412_setparent_usbsrc,
214 static int s3c2412_setparent_msysclk(struct clk *clk, struct clk *parent)
216 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
218 if (parent == &clk_mdivclk)
219 clksrc &= ~S3C2412_CLKSRC_MSYSCLK_MPLL;
220 else if (parent == &clk_mpll)
221 clksrc |= S3C2412_CLKSRC_MSYSCLK_MPLL;
225 clk->parent = parent;
227 __raw_writel(clksrc, S3C2412_CLKSRC);
231 static struct clk clk_msysclk = {
234 .set_parent = s3c2412_setparent_msysclk,
237 static int s3c2412_setparent_armclk(struct clk *clk, struct clk *parent)
240 unsigned long clkdiv;
243 /* Note, we current equate fclk andf msysclk for S3C2412 */
245 if (parent == &clk_msysclk || parent == &clk_f)
247 else if (parent == &clk_h)
248 dvs = S3C2412_CLKDIVN_DVSEN;
252 clk->parent = parent;
254 /* update this under irq lockdown, clkdivn is not protected
255 * by the clock system. */
257 local_irq_save(flags);
259 clkdiv = __raw_readl(S3C2410_CLKDIVN);
260 clkdiv &= ~S3C2412_CLKDIVN_DVSEN;
262 __raw_writel(clkdiv, S3C2410_CLKDIVN);
264 local_irq_restore(flags);
269 static struct clk clk_armclk = {
272 .parent = &clk_msysclk,
273 .set_parent = s3c2412_setparent_armclk,
276 /* these next clocks have an divider immediately after them,
277 * so we can register them with their divider and leave out the
278 * intermediate clock stage
280 static unsigned long s3c2412_roundrate_clksrc(struct clk *clk,
283 unsigned long parent_rate = clk_get_rate(clk->parent);
286 if (rate > parent_rate)
289 /* note, we remove the +/- 1 calculations as they cancel out */
291 div = (rate / parent_rate);
298 return parent_rate / div;
301 static int s3c2412_setparent_uart(struct clk *clk, struct clk *parent)
303 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
305 if (parent == &clk_erefclk)
306 clksrc &= ~S3C2412_CLKSRC_UARTCLK_MPLL;
307 else if (parent == &clk_mpll)
308 clksrc |= S3C2412_CLKSRC_UARTCLK_MPLL;
312 clk->parent = parent;
314 __raw_writel(clksrc, S3C2412_CLKSRC);
318 static unsigned long s3c2412_getrate_uart(struct clk *clk)
320 unsigned long parent_rate = clk_get_rate(clk->parent);
321 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
323 div &= S3C2412_CLKDIVN_UARTDIV_MASK;
324 div >>= S3C2412_CLKDIVN_UARTDIV_SHIFT;
326 return parent_rate / (div + 1);
329 static int s3c2412_setrate_uart(struct clk *clk, unsigned long rate)
331 unsigned long parent_rate = clk_get_rate(clk->parent);
332 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
334 rate = s3c2412_roundrate_clksrc(clk, rate);
336 clkdivn &= ~S3C2412_CLKDIVN_UARTDIV_MASK;
337 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_UARTDIV_SHIFT;
339 __raw_writel(clkdivn, S3C2410_CLKDIVN);
343 static struct clk clk_uart = {
346 .get_rate = s3c2412_getrate_uart,
347 .set_rate = s3c2412_setrate_uart,
348 .set_parent = s3c2412_setparent_uart,
349 .round_rate = s3c2412_roundrate_clksrc,
352 static int s3c2412_setparent_i2s(struct clk *clk, struct clk *parent)
354 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
356 if (parent == &clk_erefclk)
357 clksrc &= ~S3C2412_CLKSRC_I2SCLK_MPLL;
358 else if (parent == &clk_mpll)
359 clksrc |= S3C2412_CLKSRC_I2SCLK_MPLL;
363 clk->parent = parent;
365 __raw_writel(clksrc, S3C2412_CLKSRC);
369 static unsigned long s3c2412_getrate_i2s(struct clk *clk)
371 unsigned long parent_rate = clk_get_rate(clk->parent);
372 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
374 div &= S3C2412_CLKDIVN_I2SDIV_MASK;
375 div >>= S3C2412_CLKDIVN_I2SDIV_SHIFT;
377 return parent_rate / (div + 1);
380 static int s3c2412_setrate_i2s(struct clk *clk, unsigned long rate)
382 unsigned long parent_rate = clk_get_rate(clk->parent);
383 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
385 rate = s3c2412_roundrate_clksrc(clk, rate);
387 clkdivn &= ~S3C2412_CLKDIVN_I2SDIV_MASK;
388 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_I2SDIV_SHIFT;
390 __raw_writel(clkdivn, S3C2410_CLKDIVN);
394 static struct clk clk_i2s = {
397 .get_rate = s3c2412_getrate_i2s,
398 .set_rate = s3c2412_setrate_i2s,
399 .set_parent = s3c2412_setparent_i2s,
400 .round_rate = s3c2412_roundrate_clksrc,
403 static int s3c2412_setparent_cam(struct clk *clk, struct clk *parent)
405 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
407 if (parent == &clk_usysclk)
408 clksrc &= ~S3C2412_CLKSRC_CAMCLK_HCLK;
409 else if (parent == &clk_h)
410 clksrc |= S3C2412_CLKSRC_CAMCLK_HCLK;
414 clk->parent = parent;
416 __raw_writel(clksrc, S3C2412_CLKSRC);
419 static unsigned long s3c2412_getrate_cam(struct clk *clk)
421 unsigned long parent_rate = clk_get_rate(clk->parent);
422 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
424 div &= S3C2412_CLKDIVN_CAMDIV_MASK;
425 div >>= S3C2412_CLKDIVN_CAMDIV_SHIFT;
427 return parent_rate / (div + 1);
430 static int s3c2412_setrate_cam(struct clk *clk, unsigned long rate)
432 unsigned long parent_rate = clk_get_rate(clk->parent);
433 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
435 rate = s3c2412_roundrate_clksrc(clk, rate);
437 clkdivn &= ~S3C2412_CLKDIVN_CAMDIV_MASK;
438 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_CAMDIV_SHIFT;
440 __raw_writel(clkdivn, S3C2410_CLKDIVN);
444 static struct clk clk_cam = {
445 .name = "camif-upll", /* same as 2440 name */
447 .get_rate = s3c2412_getrate_cam,
448 .set_rate = s3c2412_setrate_cam,
449 .set_parent = s3c2412_setparent_cam,
450 .round_rate = s3c2412_roundrate_clksrc,
453 /* standard clock definitions */
455 static struct clk init_clocks_disable[] = {
460 .enable = s3c2412_clkcon_enable,
461 .ctrlbit = S3C2412_CLKCON_NAND,
466 .enable = s3c2412_clkcon_enable,
467 .ctrlbit = S3C2412_CLKCON_SDI,
472 .enable = s3c2412_clkcon_enable,
473 .ctrlbit = S3C2412_CLKCON_ADC,
478 .enable = s3c2412_clkcon_enable,
479 .ctrlbit = S3C2412_CLKCON_IIC,
484 .enable = s3c2412_clkcon_enable,
485 .ctrlbit = S3C2412_CLKCON_IIS,
490 .enable = s3c2412_clkcon_enable,
491 .ctrlbit = S3C2412_CLKCON_SPI,
495 static struct clk init_clocks[] = {
500 .enable = s3c2412_clkcon_enable,
501 .ctrlbit = S3C2412_CLKCON_DMA0,
506 .enable = s3c2412_clkcon_enable,
507 .ctrlbit = S3C2412_CLKCON_DMA1,
512 .enable = s3c2412_clkcon_enable,
513 .ctrlbit = S3C2412_CLKCON_DMA2,
518 .enable = s3c2412_clkcon_enable,
519 .ctrlbit = S3C2412_CLKCON_DMA3,
524 .enable = s3c2412_clkcon_enable,
525 .ctrlbit = S3C2412_CLKCON_LCDC,
530 .enable = s3c2412_clkcon_enable,
531 .ctrlbit = S3C2412_CLKCON_GPIO,
536 .enable = s3c2412_clkcon_enable,
537 .ctrlbit = S3C2412_CLKCON_USBH,
539 .name = "usb-device",
542 .enable = s3c2412_clkcon_enable,
543 .ctrlbit = S3C2412_CLKCON_USBD,
548 .enable = s3c2412_clkcon_enable,
549 .ctrlbit = S3C2412_CLKCON_PWMT,
554 .enable = s3c2412_clkcon_enable,
555 .ctrlbit = S3C2412_CLKCON_UART0,
560 .enable = s3c2412_clkcon_enable,
561 .ctrlbit = S3C2412_CLKCON_UART1,
566 .enable = s3c2412_clkcon_enable,
567 .ctrlbit = S3C2412_CLKCON_UART2,
572 .enable = s3c2412_clkcon_enable,
573 .ctrlbit = S3C2412_CLKCON_RTC,
580 .name = "usb-bus-gadget",
582 .parent = &clk_usb_bus,
583 .enable = s3c2412_clkcon_enable,
584 .ctrlbit = S3C2412_CLKCON_USB_DEV48,
586 .name = "usb-bus-host",
588 .parent = &clk_usb_bus,
589 .enable = s3c2412_clkcon_enable,
590 .ctrlbit = S3C2412_CLKCON_USB_HOST48,
594 /* clocks to add where we need to check their parentage */
603 static struct clk_init clks_src[] __initdata = {
606 .bit = S3C2412_CLKSRC_USBCLK_HCLK,
607 .src_0 = &clk_urefclk,
611 .bit = S3C2412_CLKSRC_I2SCLK_MPLL,
612 .src_0 = &clk_erefclk,
616 .bit = S3C2412_CLKSRC_CAMCLK_HCLK,
617 .src_0 = &clk_usysclk,
621 .bit = S3C2412_CLKSRC_MSYSCLK_MPLL,
622 .src_0 = &clk_mdivclk,
626 .bit = S3C2412_CLKSRC_UARTCLK_MPLL,
627 .src_0 = &clk_erefclk,
631 .bit = S3C2412_CLKSRC_USBCLK_HCLK,
632 .src_0 = &clk_usysclk,
634 /* here we assume OM[4] select xtal */
637 .bit = S3C2412_CLKSRC_EREFCLK_EXTCLK,
642 .bit = S3C2412_CLKSRC_UREFCLK_EXTCLK,
648 /* s3c2412_clk_initparents
650 * Initialise the parents for the clocks that we get at start-time
653 static void __init s3c2412_clk_initparents(void)
655 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
656 struct clk_init *cip = clks_src;
661 for (ptr = 0; ptr < ARRAY_SIZE(clks_src); ptr++, cip++) {
662 ret = s3c24xx_register_clock(cip->clk);
664 printk(KERN_ERR "Failed to register clock %s (%d)\n",
665 cip->clk->name, ret);
668 src = (clksrc & cip->bit) ? cip->src_1 : cip->src_0;
670 printk(KERN_INFO "%s: parent %s\n", cip->clk->name, src->name);
671 clk_set_parent(cip->clk, src);
675 /* clocks to add straight away */
677 static struct clk *clks[] __initdata = {
684 int __init s3c2412_baseclk_add(void)
686 unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
692 clk_upll.enable = s3c2412_upll_enable;
693 clk_usb_bus.parent = &clk_usbsrc;
694 clk_usb_bus.rate = 0x0;
696 clk_f.parent = &clk_msysclk;
698 s3c2412_clk_initparents();
700 for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
703 ret = s3c24xx_register_clock(clkp);
705 printk(KERN_ERR "Failed to register clock %s (%d)\n",
710 /* set the dvs state according to what we got at boot time */
712 dvs = __raw_readl(S3C2410_CLKDIVN) & S3C2412_CLKDIVN_DVSEN;
715 clk_armclk.parent = &clk_h;
717 printk(KERN_INFO "S3C2412: DVS is %s\n", dvs ? "on" : "off");
719 /* ensure usb bus clock is within correct rate of 48MHz */
721 if (clk_get_rate(&clk_usb_bus) != (48 * 1000 * 1000)) {
722 printk(KERN_INFO "Warning: USB bus clock not at 48MHz\n");
724 /* for the moment, let's use the UPLL, and see if we can
727 clk_set_parent(&clk_usysclk, &clk_upll);
728 clk_set_parent(&clk_usbsrc, &clk_usysclk);
729 clk_set_rate(&clk_usbsrc, 48*1000*1000);
732 printk("S3C2412: upll %s, %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
733 (__raw_readl(S3C2410_UPLLCON) & S3C2412_PLLCON_OFF) ? "off":"on",
734 print_mhz(clk_get_rate(&clk_upll)),
735 print_mhz(clk_get_rate(&clk_usb_bus)));
737 /* register clocks from clock array */
740 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
741 /* ensure that we note the clock state */
743 clkp->usage = clkcon & clkp->ctrlbit ? 1 : 0;
745 ret = s3c24xx_register_clock(clkp);
747 printk(KERN_ERR "Failed to register clock %s (%d)\n",
752 /* We must be careful disabling the clocks we are not intending to
753 * be using at boot time, as subsystems such as the LCD which do
754 * their own DMA requests to the bus can cause the system to lockup
755 * if they where in the middle of requesting bus access.
757 * Disabling the LCD clock if the LCD is active is very dangerous,
758 * and therefore the bootloader should be careful to not enable
759 * the LCD clock if it is not needed.
762 /* install (and disable) the clocks we do not need immediately */
764 clkp = init_clocks_disable;
765 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
767 ret = s3c24xx_register_clock(clkp);
769 printk(KERN_ERR "Failed to register clock %s (%d)\n",
773 s3c2412_clkcon_enable(clkp, 0);