blob: f553e5ee747718c3388b2eb65dbd16b70fc82ff6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* linux/arch/arm/mach-s3c2410/clock.c
2 *
3 * Copyright (c) 2004-2005 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 *
Ben Dooks99c13852006-06-22 22:18:20 +01006 * S3C24XX Core clock control support
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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.
12 *
13 *
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.
18 *
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.
23 *
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
27*/
28
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>
Russell Kingd052d1b2005-10-29 19:07:23 +010035#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/interrupt.h>
38#include <linux/ioport.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000039#include <linux/clk.h>
Arjan van de Ven00431702006-01-12 18:42:23 +000040#include <linux/mutex.h>
Ben Dooks8e40a2f2006-03-20 17:10:04 +000041#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/irq.h>
45#include <asm/io.h>
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/arch/regs-clock.h>
Ben Dooks3fc3e1c2006-03-20 17:10:07 +000048#include <asm/arch/regs-gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#include "clock.h"
51#include "cpu.h"
52
53/* clock information */
54
55static LIST_HEAD(clocks);
Ben Dooks36c64af2006-03-20 21:00:48 +000056
57DEFINE_MUTEX(clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/* enable and disable calls for use with the clk struct */
60
61static int clk_null_enable(struct clk *clk, int enable)
62{
63 return 0;
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/* Clock API calls */
67
68struct clk *clk_get(struct device *dev, const char *id)
69{
70 struct clk *p;
71 struct clk *clk = ERR_PTR(-ENOENT);
72 int idno;
73
Ben Dooksc086f282005-10-18 07:51:34 +010074 if (dev == NULL || dev->bus != &platform_bus_type)
75 idno = -1;
76 else
77 idno = to_platform_device(dev)->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Arjan van de Ven00431702006-01-12 18:42:23 +000079 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
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;
87 }
88 }
89
90 /* check for the case where a device was supplied, but the
91 * clock that was being searched for is not device specific */
92
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;
99 }
100 }
101 }
102
Arjan van de Ven00431702006-01-12 18:42:23 +0000103 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return clk;
105}
106
107void clk_put(struct clk *clk)
108{
109 module_put(clk->owner);
110}
111
112int clk_enable(struct clk *clk)
113{
Ben Dooks2a513ce2006-02-08 21:09:05 +0000114 if (IS_ERR(clk) || clk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -EINVAL;
116
Ben Dooks2a513ce2006-02-08 21:09:05 +0000117 clk_enable(clk->parent);
118
119 mutex_lock(&clocks_mutex);
120
121 if ((clk->usage++) == 0)
122 (clk->enable)(clk, 1);
123
124 mutex_unlock(&clocks_mutex);
125 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128void clk_disable(struct clk *clk)
129{
Ben Dooks2a513ce2006-02-08 21:09:05 +0000130 if (IS_ERR(clk) || clk == NULL)
131 return;
132
133 mutex_lock(&clocks_mutex);
134
135 if ((--clk->usage) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 (clk->enable)(clk, 0);
Ben Dooks2a513ce2006-02-08 21:09:05 +0000137
138 mutex_unlock(&clocks_mutex);
139 clk_disable(clk->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143unsigned long clk_get_rate(struct clk *clk)
144{
145 if (IS_ERR(clk))
146 return 0;
147
148 if (clk->rate != 0)
149 return clk->rate;
150
151 while (clk->parent != NULL && clk->rate == 0)
152 clk = clk->parent;
153
154 return clk->rate;
155}
156
157long clk_round_rate(struct clk *clk, unsigned long rate)
158{
Ben Dooks6e8908e2006-03-20 21:00:08 +0000159 if (!IS_ERR(clk) && clk->round_rate)
160 return (clk->round_rate)(clk, rate);
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return rate;
163}
164
165int clk_set_rate(struct clk *clk, unsigned long rate)
166{
Ben Dooks6e8908e2006-03-20 21:00:08 +0000167 int ret;
168
169 if (IS_ERR(clk))
170 return -EINVAL;
171
172 mutex_lock(&clocks_mutex);
173 ret = (clk->set_rate)(clk, rate);
174 mutex_unlock(&clocks_mutex);
175
176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179struct clk *clk_get_parent(struct clk *clk)
180{
181 return clk->parent;
182}
183
Ben Dooksd3468da2006-03-20 17:10:04 +0000184int clk_set_parent(struct clk *clk, struct clk *parent)
185{
186 int ret = 0;
187
188 if (IS_ERR(clk))
189 return -EINVAL;
190
191 mutex_lock(&clocks_mutex);
192
193 if (clk->set_parent)
194 ret = (clk->set_parent)(clk, parent);
195
196 mutex_unlock(&clocks_mutex);
197
198 return ret;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201EXPORT_SYMBOL(clk_get);
202EXPORT_SYMBOL(clk_put);
203EXPORT_SYMBOL(clk_enable);
204EXPORT_SYMBOL(clk_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205EXPORT_SYMBOL(clk_get_rate);
206EXPORT_SYMBOL(clk_round_rate);
207EXPORT_SYMBOL(clk_set_rate);
208EXPORT_SYMBOL(clk_get_parent);
Ben Dooksd3468da2006-03-20 17:10:04 +0000209EXPORT_SYMBOL(clk_set_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/* base clocks */
212
213static struct clk clk_xtal = {
214 .name = "xtal",
215 .id = -1,
216 .rate = 0,
217 .parent = NULL,
218 .ctrlbit = 0,
219};
220
Ben Dooks99c13852006-06-22 22:18:20 +0100221struct clk clk_upll = {
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000222 .name = "upll",
223 .id = -1,
224 .parent = NULL,
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000225 .ctrlbit = 0,
226};
227
Ben Dooks99c13852006-06-22 22:18:20 +0100228struct clk clk_f = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 .name = "fclk",
230 .id = -1,
231 .rate = 0,
232 .parent = NULL,
233 .ctrlbit = 0,
234};
235
Ben Dooks99c13852006-06-22 22:18:20 +0100236struct clk clk_h = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .name = "hclk",
238 .id = -1,
239 .rate = 0,
240 .parent = NULL,
241 .ctrlbit = 0,
242};
243
Ben Dooks99c13852006-06-22 22:18:20 +0100244struct clk clk_p = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 .name = "pclk",
246 .id = -1,
247 .rate = 0,
248 .parent = NULL,
249 .ctrlbit = 0,
250};
251
Ben Dooks36c64af2006-03-20 21:00:48 +0000252struct clk clk_usb_bus = {
253 .name = "usb-bus",
254 .id = -1,
255 .rate = 0,
256 .parent = &clk_upll,
257};
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/* clocks that could be registered by external code */
260
Ben Dooks3fc3e1c2006-03-20 17:10:07 +0000261static int s3c24xx_dclk_enable(struct clk *clk, int enable)
262{
263 unsigned long dclkcon = __raw_readl(S3C2410_DCLKCON);
264
265 if (enable)
266 dclkcon |= clk->ctrlbit;
267 else
268 dclkcon &= ~clk->ctrlbit;
269
270 __raw_writel(dclkcon, S3C2410_DCLKCON);
271
272 return 0;
273}
274
275static int s3c24xx_dclk_setparent(struct clk *clk, struct clk *parent)
276{
277 unsigned long dclkcon;
278 unsigned int uclk;
279
280 if (parent == &clk_upll)
281 uclk = 1;
282 else if (parent == &clk_p)
283 uclk = 0;
284 else
285 return -EINVAL;
286
287 clk->parent = parent;
288
289 dclkcon = __raw_readl(S3C2410_DCLKCON);
290
291 if (clk->ctrlbit == S3C2410_DCLKCON_DCLK0EN) {
292 if (uclk)
293 dclkcon |= S3C2410_DCLKCON_DCLK0_UCLK;
294 else
295 dclkcon &= ~S3C2410_DCLKCON_DCLK0_UCLK;
296 } else {
297 if (uclk)
298 dclkcon |= S3C2410_DCLKCON_DCLK1_UCLK;
299 else
300 dclkcon &= ~S3C2410_DCLKCON_DCLK1_UCLK;
301 }
302
303 __raw_writel(dclkcon, S3C2410_DCLKCON);
304
305 return 0;
306}
307
308
309static int s3c24xx_clkout_setparent(struct clk *clk, struct clk *parent)
310{
311 unsigned long mask;
312 unsigned long source;
313
314 /* calculate the MISCCR setting for the clock */
315
316 if (parent == &clk_xtal)
317 source = S3C2410_MISCCR_CLK0_MPLL;
318 else if (parent == &clk_upll)
319 source = S3C2410_MISCCR_CLK0_UPLL;
320 else if (parent == &clk_f)
321 source = S3C2410_MISCCR_CLK0_FCLK;
Ben Dooks73590362006-04-09 22:21:10 +0100322 else if (parent == &clk_h)
323 source = S3C2410_MISCCR_CLK0_HCLK;
Ben Dooks3fc3e1c2006-03-20 17:10:07 +0000324 else if (parent == &clk_p)
325 source = S3C2410_MISCCR_CLK0_PCLK;
326 else if (clk == &s3c24xx_clkout0 && parent == &s3c24xx_dclk0)
327 source = S3C2410_MISCCR_CLK0_DCLK0;
328 else if (clk == &s3c24xx_clkout1 && parent == &s3c24xx_dclk1)
329 source = S3C2410_MISCCR_CLK0_DCLK0;
330 else
331 return -EINVAL;
332
Ben Dooks73590362006-04-09 22:21:10 +0100333 clk->parent = parent;
334
Ben Dooks3fc3e1c2006-03-20 17:10:07 +0000335 if (clk == &s3c24xx_dclk0)
336 mask = S3C2410_MISCCR_CLK0_MASK;
337 else {
338 source <<= 4;
339 mask = S3C2410_MISCCR_CLK1_MASK;
340 }
341
342 s3c2410_modify_misccr(mask, source);
343 return 0;
344}
345
346/* external clock definitions */
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348struct clk s3c24xx_dclk0 = {
349 .name = "dclk0",
350 .id = -1,
Ben Dooks3fc3e1c2006-03-20 17:10:07 +0000351 .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
352 .enable = s3c24xx_dclk_enable,
353 .set_parent = s3c24xx_dclk_setparent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354};
355
356struct clk s3c24xx_dclk1 = {
357 .name = "dclk1",
358 .id = -1,
Ben Dooks3fc3e1c2006-03-20 17:10:07 +0000359 .ctrlbit = S3C2410_DCLKCON_DCLK0EN,
360 .enable = s3c24xx_dclk_enable,
361 .set_parent = s3c24xx_dclk_setparent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
364struct clk s3c24xx_clkout0 = {
365 .name = "clkout0",
366 .id = -1,
Ben Dooks3fc3e1c2006-03-20 17:10:07 +0000367 .set_parent = s3c24xx_clkout_setparent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368};
369
370struct clk s3c24xx_clkout1 = {
371 .name = "clkout1",
372 .id = -1,
Ben Dooks3fc3e1c2006-03-20 17:10:07 +0000373 .set_parent = s3c24xx_clkout_setparent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374};
375
376struct clk s3c24xx_uclk = {
377 .name = "uclk",
378 .id = -1,
379};
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/* initialise the clock system */
382
383int s3c24xx_register_clock(struct clk *clk)
384{
385 clk->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 if (clk->enable == NULL)
388 clk->enable = clk_null_enable;
389
390 /* add to the list of available clocks */
391
Arjan van de Ven00431702006-01-12 18:42:23 +0000392 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 list_add(&clk->list, &clocks);
Arjan van de Ven00431702006-01-12 18:42:23 +0000394 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 return 0;
397}
398
399/* initalise all the clocks */
400
401int __init s3c24xx_setup_clocks(unsigned long xtal,
402 unsigned long fclk,
403 unsigned long hclk,
404 unsigned long pclk)
405{
Ben Dooks99c13852006-06-22 22:18:20 +0100406 printk(KERN_INFO "S3C24XX Clocks, (c) 2004 Simtec Electronics\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* initialise the main system clocks */
409
410 clk_xtal.rate = xtal;
Ben Dooks99c13852006-06-22 22:18:20 +0100411 clk_upll.rate = s3c2410_get_pll(__raw_readl(S3C2410_UPLLCON), xtal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 clk_h.rate = hclk;
414 clk_p.rate = pclk;
415 clk_f.rate = fclk;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* assume uart clocks are correctly setup */
418
419 /* register our clocks */
420
421 if (s3c24xx_register_clock(&clk_xtal) < 0)
422 printk(KERN_ERR "failed to register master xtal\n");
423
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000424 if (s3c24xx_register_clock(&clk_upll) < 0)
425 printk(KERN_ERR "failed to register upll clock\n");
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (s3c24xx_register_clock(&clk_f) < 0)
428 printk(KERN_ERR "failed to register cpu fclk\n");
429
430 if (s3c24xx_register_clock(&clk_h) < 0)
431 printk(KERN_ERR "failed to register cpu hclk\n");
432
433 if (s3c24xx_register_clock(&clk_p) < 0)
434 printk(KERN_ERR "failed to register cpu pclk\n");
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437}