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>
52 /* clock information */
54 static LIST_HEAD(clocks);
55 static DEFINE_MUTEX(clocks_mutex);
59 void inline s3c24xx_clk_enable(unsigned int clocks, unsigned int enable)
63 clkcon = __raw_readl(S3C2410_CLKCON);
70 /* ensure none of the special function bits set */
71 clkcon &= ~(S3C2410_CLKCON_IDLE|S3C2410_CLKCON_POWER);
73 __raw_writel(clkcon, S3C2410_CLKCON);
76 /* enable and disable calls for use with the clk struct */
78 static int clk_null_enable(struct clk *clk, int enable)
83 int s3c24xx_clkcon_enable(struct clk *clk, int enable)
85 s3c24xx_clk_enable(clk->ctrlbit, enable);
91 struct clk *clk_get(struct device *dev, const char *id)
94 struct clk *clk = ERR_PTR(-ENOENT);
97 if (dev == NULL || dev->bus != &platform_bus_type)
100 idno = to_platform_device(dev)->id;
102 mutex_lock(&clocks_mutex);
104 list_for_each_entry(p, &clocks, list) {
106 strcmp(id, p->name) == 0 &&
107 try_module_get(p->owner)) {
113 /* check for the case where a device was supplied, but the
114 * clock that was being searched for is not device specific */
117 list_for_each_entry(p, &clocks, list) {
118 if (p->id == -1 && strcmp(id, p->name) == 0 &&
119 try_module_get(p->owner)) {
126 mutex_unlock(&clocks_mutex);
130 void clk_put(struct clk *clk)
132 module_put(clk->owner);
135 int clk_enable(struct clk *clk)
137 if (IS_ERR(clk) || clk == NULL)
140 clk_enable(clk->parent);
142 mutex_lock(&clocks_mutex);
144 if ((clk->usage++) == 0)
145 (clk->enable)(clk, 1);
147 mutex_unlock(&clocks_mutex);
151 void clk_disable(struct clk *clk)
153 if (IS_ERR(clk) || clk == NULL)
156 mutex_lock(&clocks_mutex);
158 if ((--clk->usage) == 0)
159 (clk->enable)(clk, 0);
161 mutex_unlock(&clocks_mutex);
162 clk_disable(clk->parent);
166 unsigned long clk_get_rate(struct clk *clk)
174 while (clk->parent != NULL && clk->rate == 0)
180 long clk_round_rate(struct clk *clk, unsigned long rate)
185 int clk_set_rate(struct clk *clk, unsigned long rate)
190 struct clk *clk_get_parent(struct clk *clk)
195 int clk_set_parent(struct clk *clk, struct clk *parent)
202 mutex_lock(&clocks_mutex);
205 ret = (clk->set_parent)(clk, parent);
207 mutex_unlock(&clocks_mutex);
212 EXPORT_SYMBOL(clk_get);
213 EXPORT_SYMBOL(clk_put);
214 EXPORT_SYMBOL(clk_enable);
215 EXPORT_SYMBOL(clk_disable);
216 EXPORT_SYMBOL(clk_get_rate);
217 EXPORT_SYMBOL(clk_round_rate);
218 EXPORT_SYMBOL(clk_set_rate);
219 EXPORT_SYMBOL(clk_get_parent);
220 EXPORT_SYMBOL(clk_set_parent);
222 /* base clock enable */
224 static int s3c24xx_upll_enable(struct clk *clk, int enable)
226 unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
227 unsigned long orig = clkslow;
230 clkslow &= ~S3C2410_CLKSLOW_UCLK_OFF;
232 clkslow |= S3C2410_CLKSLOW_UCLK_OFF;
234 __raw_writel(clkslow, S3C2410_CLKSLOW);
236 /* if we started the UPLL, then allow to settle */
238 if (enable && !(orig & S3C2410_CLKSLOW_UCLK_OFF))
246 static struct clk clk_xtal = {
254 static struct clk clk_upll = {
258 .enable = s3c24xx_upll_enable,
262 static struct clk clk_f = {
270 static struct clk clk_h = {
278 static struct clk clk_p = {
286 /* clocks that could be registered by external code */
288 struct clk s3c24xx_dclk0 = {
293 struct clk s3c24xx_dclk1 = {
298 struct clk s3c24xx_clkout0 = {
303 struct clk s3c24xx_clkout1 = {
308 struct clk s3c24xx_uclk = {
314 /* standard clock definitions */
316 static struct clk init_clocks[] = {
321 .enable = s3c24xx_clkcon_enable,
322 .ctrlbit = S3C2410_CLKCON_NAND,
327 .enable = s3c24xx_clkcon_enable,
328 .ctrlbit = S3C2410_CLKCON_LCDC,
333 .enable = s3c24xx_clkcon_enable,
334 .ctrlbit = S3C2410_CLKCON_USBH,
336 .name = "usb-device",
339 .enable = s3c24xx_clkcon_enable,
340 .ctrlbit = S3C2410_CLKCON_USBD,
345 .enable = s3c24xx_clkcon_enable,
346 .ctrlbit = S3C2410_CLKCON_PWMT,
351 .enable = s3c24xx_clkcon_enable,
352 .ctrlbit = S3C2410_CLKCON_SDI,
357 .enable = s3c24xx_clkcon_enable,
358 .ctrlbit = S3C2410_CLKCON_UART0,
363 .enable = s3c24xx_clkcon_enable,
364 .ctrlbit = S3C2410_CLKCON_UART1,
369 .enable = s3c24xx_clkcon_enable,
370 .ctrlbit = S3C2410_CLKCON_UART2,
375 .enable = s3c24xx_clkcon_enable,
376 .ctrlbit = S3C2410_CLKCON_GPIO,
381 .enable = s3c24xx_clkcon_enable,
382 .ctrlbit = S3C2410_CLKCON_RTC,
387 .enable = s3c24xx_clkcon_enable,
388 .ctrlbit = S3C2410_CLKCON_ADC,
393 .enable = s3c24xx_clkcon_enable,
394 .ctrlbit = S3C2410_CLKCON_IIC,
399 .enable = s3c24xx_clkcon_enable,
400 .ctrlbit = S3C2410_CLKCON_IIS,
405 .enable = s3c24xx_clkcon_enable,
406 .ctrlbit = S3C2410_CLKCON_SPI,
415 /* initialise the clock system */
417 int s3c24xx_register_clock(struct clk *clk)
419 clk->owner = THIS_MODULE;
421 if (clk->enable == NULL)
422 clk->enable = clk_null_enable;
424 /* if this is a standard clock, set the usage state */
427 unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
429 clk->usage = (clkcon & clk->ctrlbit) ? 1 : 0;
432 /* add to the list of available clocks */
434 mutex_lock(&clocks_mutex);
435 list_add(&clk->list, &clocks);
436 mutex_unlock(&clocks_mutex);
441 /* initalise all the clocks */
443 int __init s3c24xx_setup_clocks(unsigned long xtal,
448 unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
449 unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
450 struct clk *clkp = init_clocks;
454 printk(KERN_INFO "S3C2410 Clocks, (c) 2004 Simtec Electronics\n");
456 /* initialise the main system clocks */
458 clk_xtal.rate = xtal;
459 clk_upll.rate = s3c2410_get_pll(upllcon, xtal);
465 /* We must be careful disabling the clocks we are not intending to
466 * be using at boot time, as subsytems such as the LCD which do
467 * their own DMA requests to the bus can cause the system to lockup
468 * if they where in the middle of requesting bus access.
470 * Disabling the LCD clock if the LCD is active is very dangerous,
471 * and therefore the bootloader should be careful to not enable
472 * the LCD clock if it is not needed.
475 mutex_lock(&clocks_mutex);
477 s3c24xx_clk_enable(S3C2410_CLKCON_NAND, 0);
478 s3c24xx_clk_enable(S3C2410_CLKCON_USBH, 0);
479 s3c24xx_clk_enable(S3C2410_CLKCON_USBD, 0);
480 s3c24xx_clk_enable(S3C2410_CLKCON_ADC, 0);
481 s3c24xx_clk_enable(S3C2410_CLKCON_IIC, 0);
482 s3c24xx_clk_enable(S3C2410_CLKCON_SPI, 0);
484 mutex_unlock(&clocks_mutex);
486 /* assume uart clocks are correctly setup */
488 /* register our clocks */
490 if (s3c24xx_register_clock(&clk_xtal) < 0)
491 printk(KERN_ERR "failed to register master xtal\n");
493 if (s3c24xx_register_clock(&clk_upll) < 0)
494 printk(KERN_ERR "failed to register upll clock\n");
496 if (s3c24xx_register_clock(&clk_f) < 0)
497 printk(KERN_ERR "failed to register cpu fclk\n");
499 if (s3c24xx_register_clock(&clk_h) < 0)
500 printk(KERN_ERR "failed to register cpu hclk\n");
502 if (s3c24xx_register_clock(&clk_p) < 0)
503 printk(KERN_ERR "failed to register cpu pclk\n");
505 /* register clocks from clock array */
507 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
508 ret = s3c24xx_register_clock(clkp);
510 printk(KERN_ERR "Failed to register clock %s (%d)\n",
515 /* show the clock-slow value */
517 printk("CLOCK: Slow mode (%ld.%ld MHz), %s, MPLL %s, UPLL %s\n",
518 print_mhz(xtal / ( 2 * S3C2410_CLKSLOW_GET_SLOWVAL(clkslow))),
519 (clkslow & S3C2410_CLKSLOW_SLOW) ? "slow" : "fast",
520 (clkslow & S3C2410_CLKSLOW_MPLL_OFF) ? "off" : "on",
521 (clkslow & S3C2410_CLKSLOW_UCLK_OFF) ? "off" : "on");