1 /* linux/arch/arm/mach-s3c2410/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>
34 #include <asm/mach/map.h>
36 #include <asm/hardware.h>
39 #include <asm/arch/regs-serial.h>
40 #include <asm/arch/regs-clock.h>
41 #include <asm/arch/regs-gpio.h>
47 /* We currently have to assume that the system is running
48 * from the XTPll input, and that all ***REFCLKs are being
49 * fed from it, as we cannot read the state of OM[4] from
52 * It would be possible for each board initialisation to
53 * set the correct muxing at initialisation
56 static int s3c2412_clkcon_enable(struct clk *clk, int enable)
58 unsigned int clocks = clk->ctrlbit;
61 clkcon = __raw_readl(S3C2410_CLKCON);
68 __raw_writel(clkcon, S3C2410_CLKCON);
73 static int s3c2412_upll_enable(struct clk *clk, int enable)
75 unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
76 unsigned long orig = upllcon;
79 upllcon |= S3C2412_PLLCON_OFF;
81 upllcon &= ~S3C2412_PLLCON_OFF;
83 __raw_writel(upllcon, S3C2410_UPLLCON);
85 /* allow ~150uS for the PLL to settle and lock */
87 if (enable && (orig & S3C2412_PLLCON_OFF))
93 /* clock selections */
95 /* CPU EXTCLK input */
96 static struct clk clk_ext = {
101 static struct clk clk_erefclk = {
106 static struct clk clk_urefclk = {
111 static int s3c2412_setparent_usysclk(struct clk *clk, struct clk *parent)
113 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
115 if (parent == &clk_urefclk)
116 clksrc &= ~S3C2412_CLKSRC_USYSCLK_UPLL;
117 else if (parent == &clk_upll)
118 clksrc |= S3C2412_CLKSRC_USYSCLK_UPLL;
122 clk->parent = parent;
124 __raw_writel(clksrc, S3C2412_CLKSRC);
128 static struct clk clk_usysclk = {
132 .set_parent = s3c2412_setparent_usysclk,
135 static struct clk clk_mrefclk = {
141 static struct clk clk_mdivclk = {
147 static int s3c2412_setparent_usbsrc(struct clk *clk, struct clk *parent)
149 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
151 if (parent == &clk_usysclk)
152 clksrc &= ~S3C2412_CLKSRC_USBCLK_HCLK;
153 else if (parent == &clk_h)
154 clksrc |= S3C2412_CLKSRC_USBCLK_HCLK;
158 clk->parent = parent;
160 __raw_writel(clksrc, S3C2412_CLKSRC);
164 static unsigned long s3c2412_roundrate_usbsrc(struct clk *clk,
167 unsigned long parent_rate = clk_get_rate(clk->parent);
170 if (rate > parent_rate)
173 div = parent_rate / rate;
177 return parent_rate / div;
180 static unsigned long s3c2412_getrate_usbsrc(struct clk *clk)
182 unsigned long parent_rate = clk_get_rate(clk->parent);
183 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
185 return parent_rate / ((div & S3C2412_CLKDIVN_USB48DIV) ? 2 : 1);
188 static int s3c2412_setrate_usbsrc(struct clk *clk, unsigned long rate)
190 unsigned long parent_rate = clk_get_rate(clk->parent);
191 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
193 rate = s3c2412_roundrate_usbsrc(clk, rate);
195 if ((parent_rate / rate) == 2)
196 clkdivn |= S3C2412_CLKDIVN_USB48DIV;
198 clkdivn &= ~S3C2412_CLKDIVN_USB48DIV;
200 __raw_writel(clkdivn, S3C2410_CLKDIVN);
204 static struct clk clk_usbsrc = {
207 .get_rate = s3c2412_getrate_usbsrc,
208 .set_rate = s3c2412_setrate_usbsrc,
209 .round_rate = s3c2412_roundrate_usbsrc,
210 .set_parent = s3c2412_setparent_usbsrc,
213 static int s3c2412_setparent_msysclk(struct clk *clk, struct clk *parent)
215 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
217 if (parent == &clk_mdivclk)
218 clksrc &= ~S3C2412_CLKSRC_MSYSCLK_MPLL;
219 else if (parent == &clk_upll)
220 clksrc |= S3C2412_CLKSRC_MSYSCLK_MPLL;
224 clk->parent = parent;
226 __raw_writel(clksrc, S3C2412_CLKSRC);
230 static struct clk clk_msysclk = {
233 .set_parent = s3c2412_setparent_msysclk,
236 /* these next clocks have an divider immediately after them,
237 * so we can register them with their divider and leave out the
238 * intermediate clock stage
240 static unsigned long s3c2412_roundrate_clksrc(struct clk *clk,
243 unsigned long parent_rate = clk_get_rate(clk->parent);
246 if (rate > parent_rate)
249 /* note, we remove the +/- 1 calculations as they cancel out */
251 div = (rate / parent_rate);
258 return parent_rate / div;
261 static int s3c2412_setparent_uart(struct clk *clk, struct clk *parent)
263 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
265 if (parent == &clk_erefclk)
266 clksrc &= ~S3C2412_CLKSRC_UARTCLK_MPLL;
267 else if (parent == &clk_mpll)
268 clksrc |= S3C2412_CLKSRC_UARTCLK_MPLL;
272 clk->parent = parent;
274 __raw_writel(clksrc, S3C2412_CLKSRC);
278 static unsigned long s3c2412_getrate_uart(struct clk *clk)
280 unsigned long parent_rate = clk_get_rate(clk->parent);
281 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
283 div &= S3C2412_CLKDIVN_UARTDIV_MASK;
284 div >>= S3C2412_CLKDIVN_UARTDIV_SHIFT;
286 return parent_rate / (div + 1);
289 static int s3c2412_setrate_uart(struct clk *clk, unsigned long rate)
291 unsigned long parent_rate = clk_get_rate(clk->parent);
292 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
294 rate = s3c2412_roundrate_clksrc(clk, rate);
296 clkdivn &= ~S3C2412_CLKDIVN_UARTDIV_MASK;
297 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_UARTDIV_SHIFT;
299 __raw_writel(clkdivn, S3C2410_CLKDIVN);
303 static struct clk clk_uart = {
306 .get_rate = s3c2412_getrate_uart,
307 .set_rate = s3c2412_setrate_uart,
308 .set_parent = s3c2412_setparent_uart,
309 .round_rate = s3c2412_roundrate_clksrc,
312 static int s3c2412_setparent_i2s(struct clk *clk, struct clk *parent)
314 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
316 if (parent == &clk_erefclk)
317 clksrc &= ~S3C2412_CLKSRC_I2SCLK_MPLL;
318 else if (parent == &clk_mpll)
319 clksrc |= S3C2412_CLKSRC_I2SCLK_MPLL;
323 clk->parent = parent;
325 __raw_writel(clksrc, S3C2412_CLKSRC);
329 static unsigned long s3c2412_getrate_i2s(struct clk *clk)
331 unsigned long parent_rate = clk_get_rate(clk->parent);
332 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
334 div &= S3C2412_CLKDIVN_I2SDIV_MASK;
335 div >>= S3C2412_CLKDIVN_I2SDIV_SHIFT;
337 return parent_rate / (div + 1);
340 static int s3c2412_setrate_i2s(struct clk *clk, unsigned long rate)
342 unsigned long parent_rate = clk_get_rate(clk->parent);
343 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
345 rate = s3c2412_roundrate_clksrc(clk, rate);
347 clkdivn &= ~S3C2412_CLKDIVN_I2SDIV_MASK;
348 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_I2SDIV_SHIFT;
350 __raw_writel(clkdivn, S3C2410_CLKDIVN);
354 static struct clk clk_i2s = {
357 .get_rate = s3c2412_getrate_i2s,
358 .set_rate = s3c2412_setrate_i2s,
359 .set_parent = s3c2412_setparent_i2s,
360 .round_rate = s3c2412_roundrate_clksrc,
363 static int s3c2412_setparent_cam(struct clk *clk, struct clk *parent)
365 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
367 if (parent == &clk_usysclk)
368 clksrc &= ~S3C2412_CLKSRC_CAMCLK_HCLK;
369 else if (parent == &clk_h)
370 clksrc |= S3C2412_CLKSRC_CAMCLK_HCLK;
374 clk->parent = parent;
376 __raw_writel(clksrc, S3C2412_CLKSRC);
379 static unsigned long s3c2412_getrate_cam(struct clk *clk)
381 unsigned long parent_rate = clk_get_rate(clk->parent);
382 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
384 div &= S3C2412_CLKDIVN_CAMDIV_MASK;
385 div >>= S3C2412_CLKDIVN_CAMDIV_SHIFT;
387 return parent_rate / (div + 1);
390 static int s3c2412_setrate_cam(struct clk *clk, unsigned long rate)
392 unsigned long parent_rate = clk_get_rate(clk->parent);
393 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
395 rate = s3c2412_roundrate_clksrc(clk, rate);
397 clkdivn &= ~S3C2412_CLKDIVN_CAMDIV_MASK;
398 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_CAMDIV_SHIFT;
400 __raw_writel(clkdivn, S3C2410_CLKDIVN);
404 static struct clk clk_cam = {
405 .name = "camif-upll", /* same as 2440 name */
407 .get_rate = s3c2412_getrate_cam,
408 .set_rate = s3c2412_setrate_cam,
409 .set_parent = s3c2412_setparent_cam,
410 .round_rate = s3c2412_roundrate_clksrc,
413 /* standard clock definitions */
415 static struct clk init_clocks_disable[] = {
420 .enable = s3c2412_clkcon_enable,
421 .ctrlbit = S3C2412_CLKCON_NAND,
426 .enable = s3c2412_clkcon_enable,
427 .ctrlbit = S3C2412_CLKCON_SDI,
432 .enable = s3c2412_clkcon_enable,
433 .ctrlbit = S3C2412_CLKCON_ADC,
438 .enable = s3c2412_clkcon_enable,
439 .ctrlbit = S3C2412_CLKCON_IIC,
444 .enable = s3c2412_clkcon_enable,
445 .ctrlbit = S3C2412_CLKCON_IIS,
450 .enable = s3c2412_clkcon_enable,
451 .ctrlbit = S3C2412_CLKCON_SPI,
455 static struct clk init_clocks[] = {
460 .enable = s3c2412_clkcon_enable,
461 .ctrlbit = S3C2412_CLKCON_DMA0,
466 .enable = s3c2412_clkcon_enable,
467 .ctrlbit = S3C2412_CLKCON_DMA1,
472 .enable = s3c2412_clkcon_enable,
473 .ctrlbit = S3C2412_CLKCON_DMA2,
478 .enable = s3c2412_clkcon_enable,
479 .ctrlbit = S3C2412_CLKCON_DMA3,
484 .enable = s3c2412_clkcon_enable,
485 .ctrlbit = S3C2412_CLKCON_LCDC,
490 .enable = s3c2412_clkcon_enable,
491 .ctrlbit = S3C2412_CLKCON_GPIO,
496 .enable = s3c2412_clkcon_enable,
497 .ctrlbit = S3C2412_CLKCON_USBH,
499 .name = "usb-device",
502 .enable = s3c2412_clkcon_enable,
503 .ctrlbit = S3C2412_CLKCON_USBD,
508 .enable = s3c2412_clkcon_enable,
509 .ctrlbit = S3C2412_CLKCON_PWMT,
514 .enable = s3c2412_clkcon_enable,
515 .ctrlbit = S3C2412_CLKCON_UART0,
520 .enable = s3c2412_clkcon_enable,
521 .ctrlbit = S3C2412_CLKCON_UART1,
526 .enable = s3c2412_clkcon_enable,
527 .ctrlbit = S3C2412_CLKCON_UART2,
532 .enable = s3c2412_clkcon_enable,
533 .ctrlbit = S3C2412_CLKCON_RTC,
540 .name = "usb-bus-gadget",
542 .parent = &clk_usb_bus,
543 .enable = s3c2412_clkcon_enable,
544 .ctrlbit = S3C2412_CLKCON_USB_DEV48,
546 .name = "usb-bus-host",
548 .parent = &clk_usb_bus,
549 .enable = s3c2412_clkcon_enable,
550 .ctrlbit = S3C2412_CLKCON_USB_HOST48,
554 /* clocks to add where we need to check their parentage */
563 static struct clk_init clks_src[] __initdata = {
566 .bit = S3C2412_CLKSRC_USBCLK_HCLK,
567 .src_0 = &clk_urefclk,
571 .bit = S3C2412_CLKSRC_I2SCLK_MPLL,
572 .src_0 = &clk_erefclk,
576 .bit = S3C2412_CLKSRC_CAMCLK_HCLK,
577 .src_0 = &clk_usysclk,
581 .bit = S3C2412_CLKSRC_MSYSCLK_MPLL,
582 .src_0 = &clk_mdivclk,
586 .bit = S3C2412_CLKSRC_UARTCLK_MPLL,
587 .src_0 = &clk_erefclk,
591 .bit = S3C2412_CLKSRC_USBCLK_HCLK,
592 .src_0 = &clk_usysclk,
597 /* s3c2412_clk_initparents
599 * Initialise the parents for the clocks that we get at start-time
602 static void __init s3c2412_clk_initparents(void)
604 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
605 struct clk_init *cip = clks_src;
610 for (ptr = 0; ptr < ARRAY_SIZE(clks_src); ptr++, cip++) {
611 ret = s3c24xx_register_clock(cip->clk);
613 printk(KERN_ERR "Failed to register clock %s (%d)\n",
614 cip->clk->name, ret);
617 src = (clksrc & cip->bit) ? cip->src_1 : cip->src_0;
619 printk(KERN_INFO "%s: parent %s\n", cip->clk->name, src->name);
620 clk_set_parent(cip->clk, src);
624 /* clocks to add straight away */
626 static struct clk *clks[] __initdata = {
634 int __init s3c2412_baseclk_add(void)
636 unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
641 clk_upll.enable = s3c2412_upll_enable;
642 clk_usb_bus.parent = &clk_usbsrc;
643 clk_usb_bus.rate = 0x0;
645 s3c2412_clk_initparents();
647 for (ptr = 0; ptr < ARRAY_SIZE(clks); ptr++) {
650 ret = s3c24xx_register_clock(clkp);
652 printk(KERN_ERR "Failed to register clock %s (%d)\n",
657 /* ensure usb bus clock is within correct rate of 48MHz */
659 if (clk_get_rate(&clk_usb_bus) != (48 * 1000 * 1000)) {
660 printk(KERN_INFO "Warning: USB bus clock not at 48MHz\n");
662 /* for the moment, let's use the UPLL, and see if we can
665 clk_set_parent(&clk_usysclk, &clk_upll);
666 clk_set_parent(&clk_usbsrc, &clk_usysclk);
667 clk_set_rate(&clk_usbsrc, 48*1000*1000);
670 printk("S3C2412: upll %s, %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
671 (__raw_readl(S3C2410_UPLLCON) & S3C2412_PLLCON_OFF) ? "off":"on",
672 print_mhz(clk_get_rate(&clk_upll)),
673 print_mhz(clk_get_rate(&clk_usb_bus)));
675 /* register clocks from clock array */
678 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
679 /* ensure that we note the clock state */
681 clkp->usage = clkcon & clkp->ctrlbit ? 1 : 0;
683 ret = s3c24xx_register_clock(clkp);
685 printk(KERN_ERR "Failed to register clock %s (%d)\n",
690 /* We must be careful disabling the clocks we are not intending to
691 * be using at boot time, as subsytems such as the LCD which do
692 * their own DMA requests to the bus can cause the system to lockup
693 * if they where in the middle of requesting bus access.
695 * Disabling the LCD clock if the LCD is active is very dangerous,
696 * and therefore the bootloader should be careful to not enable
697 * the LCD clock if it is not needed.
700 /* install (and disable) the clocks we do not need immediately */
702 clkp = init_clocks_disable;
703 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
705 ret = s3c24xx_register_clock(clkp);
707 printk(KERN_ERR "Failed to register clock %s (%d)\n",
711 s3c2412_clkcon_enable(clkp, 0);