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/hardware.h>
37 #include <asm/arch/regs-clock.h>
38 #include <asm/arch/regs-gpio.h>
43 /* We currently have to assume that the system is running
44 * from the XTPll input, and that all ***REFCLKs are being
45 * fed from it, as we cannot read the state of OM[4] from
48 * It would be possible for each board initialisation to
49 * set the correct muxing at initialisation
52 int s3c2412_clkcon_enable(struct clk *clk, int enable)
54 unsigned int clocks = clk->ctrlbit;
57 clkcon = __raw_readl(S3C2410_CLKCON);
64 __raw_writel(clkcon, S3C2410_CLKCON);
69 static int s3c2412_upll_enable(struct clk *clk, int enable)
71 unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
72 unsigned long orig = upllcon;
75 upllcon |= S3C2412_PLLCON_OFF;
77 upllcon &= ~S3C2412_PLLCON_OFF;
79 __raw_writel(upllcon, S3C2410_UPLLCON);
81 /* allow ~150uS for the PLL to settle and lock */
83 if (enable && (orig & S3C2412_PLLCON_OFF))
89 /* clock selections */
91 /* CPU EXTCLK input */
92 static struct clk clk_ext = {
97 static struct clk clk_erefclk = {
102 static struct clk clk_urefclk = {
107 static int s3c2412_setparent_usysclk(struct clk *clk, struct clk *parent)
109 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
111 if (parent == &clk_urefclk)
112 clksrc &= ~S3C2412_CLKSRC_USYSCLK_UPLL;
113 else if (parent == &clk_upll)
114 clksrc |= S3C2412_CLKSRC_USYSCLK_UPLL;
118 clk->parent = parent;
120 __raw_writel(clksrc, S3C2412_CLKSRC);
124 static struct clk clk_usysclk = {
128 .set_parent = s3c2412_setparent_usysclk,
131 static struct clk clk_mrefclk = {
137 static struct clk clk_mdivclk = {
143 static int s3c2412_setparent_usbsrc(struct clk *clk, struct clk *parent)
145 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
147 if (parent == &clk_usysclk)
148 clksrc &= ~S3C2412_CLKSRC_USBCLK_HCLK;
149 else if (parent == &clk_h)
150 clksrc |= S3C2412_CLKSRC_USBCLK_HCLK;
154 clk->parent = parent;
156 __raw_writel(clksrc, S3C2412_CLKSRC);
160 static unsigned long s3c2412_roundrate_usbsrc(struct clk *clk,
163 unsigned long parent_rate = clk_get_rate(clk->parent);
166 if (rate > parent_rate)
169 div = parent_rate / rate;
173 return parent_rate / div;
176 static unsigned long s3c2412_getrate_usbsrc(struct clk *clk)
178 unsigned long parent_rate = clk_get_rate(clk->parent);
179 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
181 return parent_rate / ((div & S3C2412_CLKDIVN_USB48DIV) ? 2 : 1);
184 static int s3c2412_setrate_usbsrc(struct clk *clk, unsigned long rate)
186 unsigned long parent_rate = clk_get_rate(clk->parent);
187 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
189 rate = s3c2412_roundrate_usbsrc(clk, rate);
191 if ((parent_rate / rate) == 2)
192 clkdivn |= S3C2412_CLKDIVN_USB48DIV;
194 clkdivn &= ~S3C2412_CLKDIVN_USB48DIV;
196 __raw_writel(clkdivn, S3C2410_CLKDIVN);
200 static struct clk clk_usbsrc = {
203 .get_rate = s3c2412_getrate_usbsrc,
204 .set_rate = s3c2412_setrate_usbsrc,
205 .round_rate = s3c2412_roundrate_usbsrc,
206 .set_parent = s3c2412_setparent_usbsrc,
209 static int s3c2412_setparent_msysclk(struct clk *clk, struct clk *parent)
211 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
213 if (parent == &clk_mdivclk)
214 clksrc &= ~S3C2412_CLKSRC_MSYSCLK_MPLL;
215 else if (parent == &clk_upll)
216 clksrc |= S3C2412_CLKSRC_MSYSCLK_MPLL;
220 clk->parent = parent;
222 __raw_writel(clksrc, S3C2412_CLKSRC);
226 static struct clk clk_msysclk = {
229 .set_parent = s3c2412_setparent_msysclk,
232 /* these next clocks have an divider immediately after them,
233 * so we can register them with their divider and leave out the
234 * intermediate clock stage
236 static unsigned long s3c2412_roundrate_clksrc(struct clk *clk,
239 unsigned long parent_rate = clk_get_rate(clk->parent);
242 if (rate > parent_rate)
245 /* note, we remove the +/- 1 calculations as they cancel out */
247 div = (rate / parent_rate);
254 return parent_rate / div;
257 static int s3c2412_setparent_uart(struct clk *clk, struct clk *parent)
259 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
261 if (parent == &clk_erefclk)
262 clksrc &= ~S3C2412_CLKSRC_UARTCLK_MPLL;
263 else if (parent == &clk_mpll)
264 clksrc |= S3C2412_CLKSRC_UARTCLK_MPLL;
268 clk->parent = parent;
270 __raw_writel(clksrc, S3C2412_CLKSRC);
274 static unsigned long s3c2412_getrate_uart(struct clk *clk)
276 unsigned long parent_rate = clk_get_rate(clk->parent);
277 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
279 div &= S3C2412_CLKDIVN_UARTDIV_MASK;
280 div >>= S3C2412_CLKDIVN_UARTDIV_SHIFT;
282 return parent_rate / (div + 1);
285 static int s3c2412_setrate_uart(struct clk *clk, unsigned long rate)
287 unsigned long parent_rate = clk_get_rate(clk->parent);
288 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
290 rate = s3c2412_roundrate_clksrc(clk, rate);
292 clkdivn &= ~S3C2412_CLKDIVN_UARTDIV_MASK;
293 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_UARTDIV_SHIFT;
295 __raw_writel(clkdivn, S3C2410_CLKDIVN);
299 static struct clk clk_uart = {
302 .get_rate = s3c2412_getrate_uart,
303 .set_rate = s3c2412_setrate_uart,
304 .set_parent = s3c2412_setparent_uart,
305 .round_rate = s3c2412_roundrate_clksrc,
308 static int s3c2412_setparent_i2s(struct clk *clk, struct clk *parent)
310 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
312 if (parent == &clk_erefclk)
313 clksrc &= ~S3C2412_CLKSRC_I2SCLK_MPLL;
314 else if (parent == &clk_mpll)
315 clksrc |= S3C2412_CLKSRC_I2SCLK_MPLL;
319 clk->parent = parent;
321 __raw_writel(clksrc, S3C2412_CLKSRC);
325 static unsigned long s3c2412_getrate_i2s(struct clk *clk)
327 unsigned long parent_rate = clk_get_rate(clk->parent);
328 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
330 div &= S3C2412_CLKDIVN_I2SDIV_MASK;
331 div >>= S3C2412_CLKDIVN_I2SDIV_SHIFT;
333 return parent_rate / (div + 1);
336 static int s3c2412_setrate_i2s(struct clk *clk, unsigned long rate)
338 unsigned long parent_rate = clk_get_rate(clk->parent);
339 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
341 rate = s3c2412_roundrate_clksrc(clk, rate);
343 clkdivn &= ~S3C2412_CLKDIVN_I2SDIV_MASK;
344 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_I2SDIV_SHIFT;
346 __raw_writel(clkdivn, S3C2410_CLKDIVN);
350 static struct clk clk_i2s = {
353 .get_rate = s3c2412_getrate_i2s,
354 .set_rate = s3c2412_setrate_i2s,
355 .set_parent = s3c2412_setparent_i2s,
356 .round_rate = s3c2412_roundrate_clksrc,
359 static int s3c2412_setparent_cam(struct clk *clk, struct clk *parent)
361 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
363 if (parent == &clk_usysclk)
364 clksrc &= ~S3C2412_CLKSRC_CAMCLK_HCLK;
365 else if (parent == &clk_h)
366 clksrc |= S3C2412_CLKSRC_CAMCLK_HCLK;
370 clk->parent = parent;
372 __raw_writel(clksrc, S3C2412_CLKSRC);
375 static unsigned long s3c2412_getrate_cam(struct clk *clk)
377 unsigned long parent_rate = clk_get_rate(clk->parent);
378 unsigned long div = __raw_readl(S3C2410_CLKDIVN);
380 div &= S3C2412_CLKDIVN_CAMDIV_MASK;
381 div >>= S3C2412_CLKDIVN_CAMDIV_SHIFT;
383 return parent_rate / (div + 1);
386 static int s3c2412_setrate_cam(struct clk *clk, unsigned long rate)
388 unsigned long parent_rate = clk_get_rate(clk->parent);
389 unsigned long clkdivn = __raw_readl(S3C2410_CLKDIVN);
391 rate = s3c2412_roundrate_clksrc(clk, rate);
393 clkdivn &= ~S3C2412_CLKDIVN_CAMDIV_MASK;
394 clkdivn |= ((parent_rate / rate) - 1) << S3C2412_CLKDIVN_CAMDIV_SHIFT;
396 __raw_writel(clkdivn, S3C2410_CLKDIVN);
400 static struct clk clk_cam = {
401 .name = "camif-upll", /* same as 2440 name */
403 .get_rate = s3c2412_getrate_cam,
404 .set_rate = s3c2412_setrate_cam,
405 .set_parent = s3c2412_setparent_cam,
406 .round_rate = s3c2412_roundrate_clksrc,
409 /* standard clock definitions */
411 static struct clk init_clocks_disable[] = {
416 .enable = s3c2412_clkcon_enable,
417 .ctrlbit = S3C2412_CLKCON_NAND,
422 .enable = s3c2412_clkcon_enable,
423 .ctrlbit = S3C2412_CLKCON_SDI,
428 .enable = s3c2412_clkcon_enable,
429 .ctrlbit = S3C2412_CLKCON_ADC,
434 .enable = s3c2412_clkcon_enable,
435 .ctrlbit = S3C2412_CLKCON_IIC,
440 .enable = s3c2412_clkcon_enable,
441 .ctrlbit = S3C2412_CLKCON_IIS,
446 .enable = s3c2412_clkcon_enable,
447 .ctrlbit = S3C2412_CLKCON_SPI,
451 static struct clk init_clocks[] = {
456 .enable = s3c2412_clkcon_enable,
457 .ctrlbit = S3C2412_CLKCON_DMA0,
462 .enable = s3c2412_clkcon_enable,
463 .ctrlbit = S3C2412_CLKCON_DMA1,
468 .enable = s3c2412_clkcon_enable,
469 .ctrlbit = S3C2412_CLKCON_DMA2,
474 .enable = s3c2412_clkcon_enable,
475 .ctrlbit = S3C2412_CLKCON_DMA3,
480 .enable = s3c2412_clkcon_enable,
481 .ctrlbit = S3C2412_CLKCON_LCDC,
486 .enable = s3c2412_clkcon_enable,
487 .ctrlbit = S3C2412_CLKCON_GPIO,
492 .enable = s3c2412_clkcon_enable,
493 .ctrlbit = S3C2412_CLKCON_USBH,
495 .name = "usb-device",
498 .enable = s3c2412_clkcon_enable,
499 .ctrlbit = S3C2412_CLKCON_USBD,
504 .enable = s3c2412_clkcon_enable,
505 .ctrlbit = S3C2412_CLKCON_PWMT,
510 .enable = s3c2412_clkcon_enable,
511 .ctrlbit = S3C2412_CLKCON_UART0,
516 .enable = s3c2412_clkcon_enable,
517 .ctrlbit = S3C2412_CLKCON_UART1,
522 .enable = s3c2412_clkcon_enable,
523 .ctrlbit = S3C2412_CLKCON_UART2,
528 .enable = s3c2412_clkcon_enable,
529 .ctrlbit = S3C2412_CLKCON_RTC,
536 .name = "usb-bus-gadget",
538 .parent = &clk_usb_bus,
539 .enable = s3c2412_clkcon_enable,
540 .ctrlbit = S3C2412_CLKCON_USB_DEV48,
542 .name = "usb-bus-host",
544 .parent = &clk_usb_bus,
545 .enable = s3c2412_clkcon_enable,
546 .ctrlbit = S3C2412_CLKCON_USB_HOST48,
550 /* clocks to add where we need to check their parentage */
559 struct clk_init clks_src[] __initdata = {
562 .bit = S3C2412_CLKSRC_USBCLK_HCLK,
563 .src_0 = &clk_urefclk,
567 .bit = S3C2412_CLKSRC_I2SCLK_MPLL,
568 .src_0 = &clk_erefclk,
572 .bit = S3C2412_CLKSRC_CAMCLK_HCLK,
573 .src_0 = &clk_usysclk,
577 .bit = S3C2412_CLKSRC_MSYSCLK_MPLL,
578 .src_0 = &clk_mdivclk,
582 .bit = S3C2412_CLKSRC_UARTCLK_MPLL,
583 .src_0 = &clk_erefclk,
587 .bit = S3C2412_CLKSRC_USBCLK_HCLK,
588 .src_0 = &clk_usysclk,
593 /* s3c2412_clk_initparents
595 * Initialise the parents for the clocks that we get at start-time
598 static void __init s3c2412_clk_initparents(void)
600 unsigned long clksrc = __raw_readl(S3C2412_CLKSRC);
601 struct clk_init *cip = clks_src;
606 for (ptr = 0; ptr < ARRAY_SIZE(clks_src); ptr++, cip++) {
607 ret = s3c24xx_register_clock(cip->clk);
609 printk(KERN_ERR "Failed to register clock %s (%d)\n",
610 cip->clk->name, ret);
613 src = (clksrc & cip->bit) ? cip->src_1 : cip->src_0;
615 printk(KERN_INFO "%s: parent %s\n", cip->clk->name, src->name);
616 clk_set_parent(cip->clk, src);
620 /* clocks to add straight away */
622 struct clk *clks[] __initdata = {
630 int __init s3c2412_baseclk_add(void)
632 unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
637 clk_upll.enable = s3c2412_upll_enable;
638 clk_usb_bus.parent = &clk_usbsrc;
639 clk_usb_bus.rate = 0x0;
641 s3c2412_clk_initparents();
643 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 /* ensure usb bus clock is within correct rate of 48MHz */
655 if (clk_get_rate(&clk_usb_bus) != (48 * 1000 * 1000)) {
656 printk(KERN_INFO "Warning: USB bus clock not at 48MHz\n");
658 /* for the moment, let's use the UPLL, and see if we can
661 clk_set_parent(&clk_usysclk, &clk_upll);
662 clk_set_parent(&clk_usbsrc, &clk_usysclk);
663 clk_set_rate(&clk_usbsrc, 48*1000*1000);
666 printk("S3C2412: upll %s, %ld.%03ld MHz, usb-bus %ld.%03ld MHz\n",
667 (__raw_readl(S3C2410_UPLLCON) & S3C2412_PLLCON_OFF) ? "off":"on",
668 print_mhz(clk_get_rate(&clk_upll)),
669 print_mhz(clk_get_rate(&clk_usb_bus)));
671 /* register clocks from clock array */
674 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
675 /* ensure that we note the clock state */
677 clkp->usage = clkcon & clkp->ctrlbit ? 1 : 0;
679 ret = s3c24xx_register_clock(clkp);
681 printk(KERN_ERR "Failed to register clock %s (%d)\n",
686 /* We must be careful disabling the clocks we are not intending to
687 * be using at boot time, as subsytems such as the LCD which do
688 * their own DMA requests to the bus can cause the system to lockup
689 * if they where in the middle of requesting bus access.
691 * Disabling the LCD clock if the LCD is active is very dangerous,
692 * and therefore the bootloader should be careful to not enable
693 * the LCD clock if it is not needed.
696 /* install (and disable) the clocks we do not need immediately */
698 clkp = init_clocks_disable;
699 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks_disable); ptr++, clkp++) {
701 ret = s3c24xx_register_clock(clkp);
703 printk(KERN_ERR "Failed to register clock %s (%d)\n",
707 s3c2412_clkcon_enable(clkp, 0);