Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / arm / plat-s3c24xx / clock.c
blob99a44746f8f27f89fb4943197a1d6b55b1a259c8
1 /* linux/arch/arm/plat-s3c24xx/clock.c
3 * Copyright (c) 2004-2005 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C24XX Core clock control support
8 * Based on, and code from linux/arch/arm/mach-versatile/clock.c
9 **
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>
44 #include <asm/irq.h>
45 #include <asm/io.h>
47 #include <asm/arch/regs-clock.h>
48 #include <asm/arch/regs-gpio.h>
50 #include <asm/plat-s3c24xx/clock.h>
51 #include <asm/plat-s3c24xx/cpu.h>
53 /* clock information */
55 static LIST_HEAD(clocks);
57 DEFINE_MUTEX(clocks_mutex);
59 /* enable and disable calls for use with the clk struct */
61 static int clk_null_enable(struct clk *clk, int enable)
63 return 0;
66 /* Clock API calls */
68 struct clk *clk_get(struct device *dev, const char *id)
70 struct clk *p;
71 struct clk *clk = ERR_PTR(-ENOENT);
72 int idno;
74 if (dev == NULL || dev->bus != &platform_bus_type)
75 idno = -1;
76 else
77 idno = to_platform_device(dev)->id;
79 mutex_lock(&clocks_mutex);
81 list_for_each_entry(p, &clocks, list) {
82 if (p->id == idno &&
83 strcmp(id, p->name) == 0 &&
84 try_module_get(p->owner)) {
85 clk = p;
86 break;
90 /* check for the case where a device was supplied, but the
91 * clock that was being searched for is not device specific */
93 if (IS_ERR(clk)) {
94 list_for_each_entry(p, &clocks, list) {
95 if (p->id == -1 && strcmp(id, p->name) == 0 &&
96 try_module_get(p->owner)) {
97 clk = p;
98 break;
103 mutex_unlock(&clocks_mutex);
104 return clk;
107 void clk_put(struct clk *clk)
109 module_put(clk->owner);
112 int clk_enable(struct clk *clk)
114 if (IS_ERR(clk) || clk == NULL)
115 return -EINVAL;
117 clk_enable(clk->parent);
119 mutex_lock(&clocks_mutex);
121 if ((clk->usage++) == 0)
122 (clk->enable)(clk, 1);
124 mutex_unlock(&clocks_mutex);
125 return 0;
128 void clk_disable(struct clk *clk)
130 if (IS_ERR(clk) || clk == NULL)
131 return;
133 mutex_lock(&clocks_mutex);
135 if ((--clk->usage) == 0)
136 (clk->enable)(clk, 0);
138 mutex_unlock(&clocks_mutex);
139 clk_disable(clk->parent);
143 unsigned long clk_get_rate(struct clk *clk)
145 if (IS_ERR(clk))
146 return 0;
148 if (clk->rate != 0)
149 return clk->rate;
151 if (clk->get_rate != NULL)
152 return (clk->get_rate)(clk);
154 if (clk->parent != NULL)
155 return clk_get_rate(clk->parent);
157 return clk->rate;
160 long clk_round_rate(struct clk *clk, unsigned long rate)
162 if (!IS_ERR(clk) && clk->round_rate)
163 return (clk->round_rate)(clk, rate);
165 return rate;
168 int clk_set_rate(struct clk *clk, unsigned long rate)
170 int ret;
172 if (IS_ERR(clk))
173 return -EINVAL;
175 /* We do not default just do a clk->rate = rate as
176 * the clock may have been made this way by choice.
179 WARN_ON(clk->set_rate == NULL);
181 if (clk->set_rate == NULL)
182 return -EINVAL;
184 mutex_lock(&clocks_mutex);
185 ret = (clk->set_rate)(clk, rate);
186 mutex_unlock(&clocks_mutex);
188 return ret;
191 struct clk *clk_get_parent(struct clk *clk)
193 return clk->parent;
196 int clk_set_parent(struct clk *clk, struct clk *parent)
198 int ret = 0;
200 if (IS_ERR(clk))
201 return -EINVAL;
203 mutex_lock(&clocks_mutex);
205 if (clk->set_parent)
206 ret = (clk->set_parent)(clk, parent);
208 mutex_unlock(&clocks_mutex);
210 return ret;
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 clocks */
225 static int clk_default_setrate(struct clk *clk, unsigned long rate)
227 clk->rate = rate;
228 return 0;
231 struct clk clk_xtal = {
232 .name = "xtal",
233 .id = -1,
234 .rate = 0,
235 .parent = NULL,
236 .ctrlbit = 0,
239 struct clk clk_mpll = {
240 .name = "mpll",
241 .id = -1,
242 .set_rate = clk_default_setrate,
245 struct clk clk_upll = {
246 .name = "upll",
247 .id = -1,
248 .parent = NULL,
249 .ctrlbit = 0,
252 struct clk clk_f = {
253 .name = "fclk",
254 .id = -1,
255 .rate = 0,
256 .parent = &clk_mpll,
257 .ctrlbit = 0,
258 .set_rate = clk_default_setrate,
261 struct clk clk_h = {
262 .name = "hclk",
263 .id = -1,
264 .rate = 0,
265 .parent = NULL,
266 .ctrlbit = 0,
267 .set_rate = clk_default_setrate,
270 struct clk clk_p = {
271 .name = "pclk",
272 .id = -1,
273 .rate = 0,
274 .parent = NULL,
275 .ctrlbit = 0,
276 .set_rate = clk_default_setrate,
279 struct clk clk_usb_bus = {
280 .name = "usb-bus",
281 .id = -1,
282 .rate = 0,
283 .parent = &clk_upll,
286 /* clocks that could be registered by external code */
288 static int s3c24xx_dclk_enable(struct clk *clk, int enable)
290 unsigned long dclkcon = __raw_readl(S3C24XX_DCLKCON);
292 if (enable)
293 dclkcon |= clk->ctrlbit;
294 else
295 dclkcon &= ~clk->ctrlbit;
297 __raw_writel(dclkcon, S3C24XX_DCLKCON);
299 return 0;
302 static int s3c24xx_dclk_setparent(struct clk *clk, struct clk *parent)
304 unsigned long dclkcon;
305 unsigned int uclk;
307 if (parent == &clk_upll)
308 uclk = 1;
309 else if (parent == &clk_p)
310 uclk = 0;
311 else
312 return -EINVAL;
314 clk->parent = parent;
316 dclkcon = __raw_readl(S3C24XX_DCLKCON);
318 if (clk->ctrlbit == S3C2410_DCLKCON_DCLK0EN) {
319 if (uclk)
320 dclkcon |= S3C2410_DCLKCON_DCLK0_UCLK;
321 else
322 dclkcon &= ~S3C2410_DCLKCON_DCLK0_UCLK;
323 } else {
324 if (uclk)
325 dclkcon |= S3C2410_DCLKCON_DCLK1_UCLK;
326 else
327 dclkcon &= ~S3C2410_DCLKCON_DCLK1_UCLK;
330 __raw_writel(dclkcon, S3C24XX_DCLKCON);
332 return 0;
336 static int s3c24xx_clkout_setparent(struct clk *clk, struct clk *parent)
338 unsigned long mask;
339 unsigned long source;
341 /* calculate the MISCCR setting for the clock */
343 if (parent == &clk_xtal)
344 source = S3C2410_MISCCR_CLK0_MPLL;
345 else if (parent == &clk_upll)
346 source = S3C2410_MISCCR_CLK0_UPLL;
347 else if (parent == &clk_f)
348 source = S3C2410_MISCCR_CLK0_FCLK;
349 else if (parent == &clk_h)
350 source = S3C2410_MISCCR_CLK0_HCLK;
351 else if (parent == &clk_p)
352 source = S3C2410_MISCCR_CLK0_PCLK;
353 else if (clk == &s3c24xx_clkout0 && parent == &s3c24xx_dclk0)
354 source = S3C2410_MISCCR_CLK0_DCLK0;
355 else if (clk == &s3c24xx_clkout1 && parent == &s3c24xx_dclk1)
356 source = S3C2410_MISCCR_CLK0_DCLK0;
357 else
358 return -EINVAL;
360 clk->parent = parent;
362 if (clk == &s3c24xx_dclk0)
363 mask = S3C2410_MISCCR_CLK0_MASK;
364 else {
365 source <<= 4;
366 mask = S3C2410_MISCCR_CLK1_MASK;
369 s3c2410_modify_misccr(mask, source);
370 return 0;
373 /* external clock definitions */
375 struct clk s3c24xx_dclk0 = {
376 .name = "dclk0",
377 .id = -1,
378 .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
379 .enable = s3c24xx_dclk_enable,
380 .set_parent = s3c24xx_dclk_setparent,
383 struct clk s3c24xx_dclk1 = {
384 .name = "dclk1",
385 .id = -1,
386 .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
387 .enable = s3c24xx_dclk_enable,
388 .set_parent = s3c24xx_dclk_setparent,
391 struct clk s3c24xx_clkout0 = {
392 .name = "clkout0",
393 .id = -1,
394 .set_parent = s3c24xx_clkout_setparent,
397 struct clk s3c24xx_clkout1 = {
398 .name = "clkout1",
399 .id = -1,
400 .set_parent = s3c24xx_clkout_setparent,
403 struct clk s3c24xx_uclk = {
404 .name = "uclk",
405 .id = -1,
408 /* initialise the clock system */
410 int s3c24xx_register_clock(struct clk *clk)
412 clk->owner = THIS_MODULE;
414 if (clk->enable == NULL)
415 clk->enable = clk_null_enable;
417 /* add to the list of available clocks */
419 mutex_lock(&clocks_mutex);
420 list_add(&clk->list, &clocks);
421 mutex_unlock(&clocks_mutex);
423 return 0;
426 int s3c24xx_register_clocks(struct clk **clks, int nr_clks)
428 int fails = 0;
430 for (; nr_clks > 0; nr_clks--, clks++) {
431 if (s3c24xx_register_clock(*clks) < 0)
432 fails++;
435 return fails;
438 /* initalise all the clocks */
440 int __init s3c24xx_setup_clocks(unsigned long xtal,
441 unsigned long fclk,
442 unsigned long hclk,
443 unsigned long pclk)
445 printk(KERN_INFO "S3C24XX Clocks, (c) 2004 Simtec Electronics\n");
447 /* initialise the main system clocks */
449 clk_xtal.rate = xtal;
450 clk_upll.rate = s3c2410_get_pll(__raw_readl(S3C2410_UPLLCON), xtal);
452 clk_mpll.rate = fclk;
453 clk_h.rate = hclk;
454 clk_p.rate = pclk;
455 clk_f.rate = fclk;
457 /* assume uart clocks are correctly setup */
459 /* register our clocks */
461 if (s3c24xx_register_clock(&clk_xtal) < 0)
462 printk(KERN_ERR "failed to register master xtal\n");
464 if (s3c24xx_register_clock(&clk_mpll) < 0)
465 printk(KERN_ERR "failed to register mpll clock\n");
467 if (s3c24xx_register_clock(&clk_upll) < 0)
468 printk(KERN_ERR "failed to register upll clock\n");
470 if (s3c24xx_register_clock(&clk_f) < 0)
471 printk(KERN_ERR "failed to register cpu fclk\n");
473 if (s3c24xx_register_clock(&clk_h) < 0)
474 printk(KERN_ERR "failed to register cpu hclk\n");
476 if (s3c24xx_register_clock(&clk_p) < 0)
477 printk(KERN_ERR "failed to register cpu pclk\n");
479 return 0;