1 /* linux/arch/arm/mach-s3c2410/clock.c
3 * Copyright (c) 2004-2005 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2410 Clock control support
8 * Based on, and code from linux/arch/arm/mach-versatile/clock.c
10 ** Copyright (C) 2004 ARM Limited.
11 ** Written by Deep Blue Solutions Limited.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/list.h>
33 #include <linux/errno.h>
34 #include <linux/err.h>
35 #include <linux/platform_device.h>
36 #include <linux/sysdev.h>
37 #include <linux/interrupt.h>
38 #include <linux/ioport.h>
39 #include <linux/clk.h>
40 #include <linux/mutex.h>
41 #include <linux/delay.h>
43 #include <asm/hardware.h>
47 #include <asm/arch/regs-clock.h>
48 #include <asm/arch/regs-gpio.h>
53 /* clock information */
55 static LIST_HEAD(clocks);
56 static DEFINE_MUTEX(clocks_mutex);
60 void inline s3c24xx_clk_enable(unsigned int clocks, unsigned int enable)
64 clkcon = __raw_readl(S3C2410_CLKCON);
71 /* ensure none of the special function bits set */
72 clkcon &= ~(S3C2410_CLKCON_IDLE|S3C2410_CLKCON_POWER);
74 __raw_writel(clkcon, S3C2410_CLKCON);
77 /* enable and disable calls for use with the clk struct */
79 static int clk_null_enable(struct clk *clk, int enable)
84 int s3c24xx_clkcon_enable(struct clk *clk, int enable)
86 s3c24xx_clk_enable(clk->ctrlbit, enable);
92 struct clk *clk_get(struct device *dev, const char *id)
95 struct clk *clk = ERR_PTR(-ENOENT);
98 if (dev == NULL || dev->bus != &platform_bus_type)
101 idno = to_platform_device(dev)->id;
103 mutex_lock(&clocks_mutex);
105 list_for_each_entry(p, &clocks, list) {
107 strcmp(id, p->name) == 0 &&
108 try_module_get(p->owner)) {
114 /* check for the case where a device was supplied, but the
115 * clock that was being searched for is not device specific */
118 list_for_each_entry(p, &clocks, list) {
119 if (p->id == -1 && strcmp(id, p->name) == 0 &&
120 try_module_get(p->owner)) {
127 mutex_unlock(&clocks_mutex);
131 void clk_put(struct clk *clk)
133 module_put(clk->owner);
136 int clk_enable(struct clk *clk)
138 if (IS_ERR(clk) || clk == NULL)
141 clk_enable(clk->parent);
143 mutex_lock(&clocks_mutex);
145 if ((clk->usage++) == 0)
146 (clk->enable)(clk, 1);
148 mutex_unlock(&clocks_mutex);
152 void clk_disable(struct clk *clk)
154 if (IS_ERR(clk) || clk == NULL)
157 mutex_lock(&clocks_mutex);
159 if ((--clk->usage) == 0)
160 (clk->enable)(clk, 0);
162 mutex_unlock(&clocks_mutex);
163 clk_disable(clk->parent);
167 unsigned long clk_get_rate(struct clk *clk)
175 while (clk->parent != NULL && clk->rate == 0)
181 long clk_round_rate(struct clk *clk, unsigned long rate)
186 int clk_set_rate(struct clk *clk, unsigned long rate)
191 struct clk *clk_get_parent(struct clk *clk)
196 int clk_set_parent(struct clk *clk, struct clk *parent)
203 mutex_lock(&clocks_mutex);
206 ret = (clk->set_parent)(clk, parent);
208 mutex_unlock(&clocks_mutex);
213 EXPORT_SYMBOL(clk_get);
214 EXPORT_SYMBOL(clk_put);
215 EXPORT_SYMBOL(clk_enable);
216 EXPORT_SYMBOL(clk_disable);
217 EXPORT_SYMBOL(clk_get_rate);
218 EXPORT_SYMBOL(clk_round_rate);
219 EXPORT_SYMBOL(clk_set_rate);
220 EXPORT_SYMBOL(clk_get_parent);
221 EXPORT_SYMBOL(clk_set_parent);
223 /* base clock enable */
225 static int s3c24xx_upll_enable(struct clk *clk, int enable)
227 unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
228 unsigned long orig = clkslow;
231 clkslow &= ~S3C2410_CLKSLOW_UCLK_OFF;
233 clkslow |= S3C2410_CLKSLOW_UCLK_OFF;
235 __raw_writel(clkslow, S3C2410_CLKSLOW);
237 /* if we started the UPLL, then allow to settle */
239 if (enable && !(orig & S3C2410_CLKSLOW_UCLK_OFF))
247 static struct clk clk_xtal = {
255 static struct clk clk_upll = {
259 .enable = s3c24xx_upll_enable,
263 static struct clk clk_f = {
271 static struct clk clk_h = {
279 static struct clk clk_p = {
287 /* clocks that could be registered by external code */
289 static int s3c24xx_dclk_enable(struct clk *clk, int enable)
291 unsigned long dclkcon = __raw_readl(S3C2410_DCLKCON);
294 dclkcon |= clk->ctrlbit;
296 dclkcon &= ~clk->ctrlbit;
298 __raw_writel(dclkcon, S3C2410_DCLKCON);
303 static int s3c24xx_dclk_setparent(struct clk *clk, struct clk *parent)
305 unsigned long dclkcon;
308 if (parent == &clk_upll)
310 else if (parent == &clk_p)
315 clk->parent = parent;
317 dclkcon = __raw_readl(S3C2410_DCLKCON);
319 if (clk->ctrlbit == S3C2410_DCLKCON_DCLK0EN) {
321 dclkcon |= S3C2410_DCLKCON_DCLK0_UCLK;
323 dclkcon &= ~S3C2410_DCLKCON_DCLK0_UCLK;
326 dclkcon |= S3C2410_DCLKCON_DCLK1_UCLK;
328 dclkcon &= ~S3C2410_DCLKCON_DCLK1_UCLK;
331 __raw_writel(dclkcon, S3C2410_DCLKCON);
337 static int s3c24xx_clkout_setparent(struct clk *clk, struct clk *parent)
340 unsigned long source;
342 /* calculate the MISCCR setting for the clock */
344 if (parent == &clk_xtal)
345 source = S3C2410_MISCCR_CLK0_MPLL;
346 else if (parent == &clk_upll)
347 source = S3C2410_MISCCR_CLK0_UPLL;
348 else if (parent == &clk_f)
349 source = S3C2410_MISCCR_CLK0_FCLK;
350 else if (parent == &clk_p)
351 source = S3C2410_MISCCR_CLK0_PCLK;
352 else if (clk == &s3c24xx_clkout0 && parent == &s3c24xx_dclk0)
353 source = S3C2410_MISCCR_CLK0_DCLK0;
354 else if (clk == &s3c24xx_clkout1 && parent == &s3c24xx_dclk1)
355 source = S3C2410_MISCCR_CLK0_DCLK0;
359 if (clk == &s3c24xx_dclk0)
360 mask = S3C2410_MISCCR_CLK0_MASK;
363 mask = S3C2410_MISCCR_CLK1_MASK;
366 s3c2410_modify_misccr(mask, source);
370 /* external clock definitions */
372 struct clk s3c24xx_dclk0 = {
375 .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
376 .enable = s3c24xx_dclk_enable,
377 .set_parent = s3c24xx_dclk_setparent,
380 struct clk s3c24xx_dclk1 = {
383 .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
384 .enable = s3c24xx_dclk_enable,
385 .set_parent = s3c24xx_dclk_setparent,
388 struct clk s3c24xx_clkout0 = {
391 .set_parent = s3c24xx_clkout_setparent,
394 struct clk s3c24xx_clkout1 = {
397 .set_parent = s3c24xx_clkout_setparent,
400 struct clk s3c24xx_uclk = {
406 /* standard clock definitions */
408 static struct clk init_clocks[] = {
413 .enable = s3c24xx_clkcon_enable,
414 .ctrlbit = S3C2410_CLKCON_NAND,
419 .enable = s3c24xx_clkcon_enable,
420 .ctrlbit = S3C2410_CLKCON_LCDC,
425 .enable = s3c24xx_clkcon_enable,
426 .ctrlbit = S3C2410_CLKCON_USBH,
428 .name = "usb-device",
431 .enable = s3c24xx_clkcon_enable,
432 .ctrlbit = S3C2410_CLKCON_USBD,
437 .enable = s3c24xx_clkcon_enable,
438 .ctrlbit = S3C2410_CLKCON_PWMT,
443 .enable = s3c24xx_clkcon_enable,
444 .ctrlbit = S3C2410_CLKCON_SDI,
449 .enable = s3c24xx_clkcon_enable,
450 .ctrlbit = S3C2410_CLKCON_UART0,
455 .enable = s3c24xx_clkcon_enable,
456 .ctrlbit = S3C2410_CLKCON_UART1,
461 .enable = s3c24xx_clkcon_enable,
462 .ctrlbit = S3C2410_CLKCON_UART2,
467 .enable = s3c24xx_clkcon_enable,
468 .ctrlbit = S3C2410_CLKCON_GPIO,
473 .enable = s3c24xx_clkcon_enable,
474 .ctrlbit = S3C2410_CLKCON_RTC,
479 .enable = s3c24xx_clkcon_enable,
480 .ctrlbit = S3C2410_CLKCON_ADC,
485 .enable = s3c24xx_clkcon_enable,
486 .ctrlbit = S3C2410_CLKCON_IIC,
491 .enable = s3c24xx_clkcon_enable,
492 .ctrlbit = S3C2410_CLKCON_IIS,
497 .enable = s3c24xx_clkcon_enable,
498 .ctrlbit = S3C2410_CLKCON_SPI,
507 /* initialise the clock system */
509 int s3c24xx_register_clock(struct clk *clk)
511 clk->owner = THIS_MODULE;
513 if (clk->enable == NULL)
514 clk->enable = clk_null_enable;
516 /* if this is a standard clock, set the usage state */
518 if (clk->ctrlbit && clk->enable == s3c24xx_clkcon_enable) {
519 unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
521 clk->usage = (clkcon & clk->ctrlbit) ? 1 : 0;
524 /* add to the list of available clocks */
526 mutex_lock(&clocks_mutex);
527 list_add(&clk->list, &clocks);
528 mutex_unlock(&clocks_mutex);
533 /* initalise all the clocks */
535 int __init s3c24xx_setup_clocks(unsigned long xtal,
540 unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
541 unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
542 struct clk *clkp = init_clocks;
546 printk(KERN_INFO "S3C2410 Clocks, (c) 2004 Simtec Electronics\n");
548 /* initialise the main system clocks */
550 clk_xtal.rate = xtal;
551 clk_upll.rate = s3c2410_get_pll(upllcon, xtal);
557 /* We must be careful disabling the clocks we are not intending to
558 * be using at boot time, as subsytems such as the LCD which do
559 * their own DMA requests to the bus can cause the system to lockup
560 * if they where in the middle of requesting bus access.
562 * Disabling the LCD clock if the LCD is active is very dangerous,
563 * and therefore the bootloader should be careful to not enable
564 * the LCD clock if it is not needed.
567 mutex_lock(&clocks_mutex);
569 s3c24xx_clk_enable(S3C2410_CLKCON_NAND, 0);
570 s3c24xx_clk_enable(S3C2410_CLKCON_USBH, 0);
571 s3c24xx_clk_enable(S3C2410_CLKCON_USBD, 0);
572 s3c24xx_clk_enable(S3C2410_CLKCON_ADC, 0);
573 s3c24xx_clk_enable(S3C2410_CLKCON_IIC, 0);
574 s3c24xx_clk_enable(S3C2410_CLKCON_SPI, 0);
576 mutex_unlock(&clocks_mutex);
578 /* assume uart clocks are correctly setup */
580 /* register our clocks */
582 if (s3c24xx_register_clock(&clk_xtal) < 0)
583 printk(KERN_ERR "failed to register master xtal\n");
585 if (s3c24xx_register_clock(&clk_upll) < 0)
586 printk(KERN_ERR "failed to register upll clock\n");
588 if (s3c24xx_register_clock(&clk_f) < 0)
589 printk(KERN_ERR "failed to register cpu fclk\n");
591 if (s3c24xx_register_clock(&clk_h) < 0)
592 printk(KERN_ERR "failed to register cpu hclk\n");
594 if (s3c24xx_register_clock(&clk_p) < 0)
595 printk(KERN_ERR "failed to register cpu pclk\n");
597 /* register clocks from clock array */
599 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
600 ret = s3c24xx_register_clock(clkp);
602 printk(KERN_ERR "Failed to register clock %s (%d)\n",
607 /* show the clock-slow value */
609 printk("CLOCK: Slow mode (%ld.%ld MHz), %s, MPLL %s, UPLL %s\n",
610 print_mhz(xtal / ( 2 * S3C2410_CLKSLOW_GET_SLOWVAL(clkslow))),
611 (clkslow & S3C2410_CLKSLOW_SLOW) ? "slow" : "fast",
612 (clkslow & S3C2410_CLKSLOW_MPLL_OFF) ? "off" : "on",
613 (clkslow & S3C2410_CLKSLOW_UCLK_OFF) ? "off" : "on");