blob: aaada9e3d67f7d39512b581b96b74ef94e682a22 [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 *
6 * S3C2410 Clock control support
7 *
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>
48
49#include "clock.h"
50#include "cpu.h"
51
52/* clock information */
53
54static LIST_HEAD(clocks);
Arjan van de Ven00431702006-01-12 18:42:23 +000055static DEFINE_MUTEX(clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/* old functions */
58
59void inline s3c24xx_clk_enable(unsigned int clocks, unsigned int enable)
60{
61 unsigned long clkcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 clkcon = __raw_readl(S3C2410_CLKCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (enable)
66 clkcon |= clocks;
Ben Dooks2a513ce2006-02-08 21:09:05 +000067 else
68 clkcon &= ~clocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 /* ensure none of the special function bits set */
71 clkcon &= ~(S3C2410_CLKCON_IDLE|S3C2410_CLKCON_POWER);
72
73 __raw_writel(clkcon, S3C2410_CLKCON);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76/* enable and disable calls for use with the clk struct */
77
78static int clk_null_enable(struct clk *clk, int enable)
79{
80 return 0;
81}
82
83int s3c24xx_clkcon_enable(struct clk *clk, int enable)
84{
85 s3c24xx_clk_enable(clk->ctrlbit, enable);
86 return 0;
87}
88
89/* Clock API calls */
90
91struct clk *clk_get(struct device *dev, const char *id)
92{
93 struct clk *p;
94 struct clk *clk = ERR_PTR(-ENOENT);
95 int idno;
96
Ben Dooksc086f282005-10-18 07:51:34 +010097 if (dev == NULL || dev->bus != &platform_bus_type)
98 idno = -1;
99 else
100 idno = to_platform_device(dev)->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Arjan van de Ven00431702006-01-12 18:42:23 +0000102 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 list_for_each_entry(p, &clocks, list) {
105 if (p->id == idno &&
106 strcmp(id, p->name) == 0 &&
107 try_module_get(p->owner)) {
108 clk = p;
109 break;
110 }
111 }
112
113 /* check for the case where a device was supplied, but the
114 * clock that was being searched for is not device specific */
115
116 if (IS_ERR(clk)) {
117 list_for_each_entry(p, &clocks, list) {
118 if (p->id == -1 && strcmp(id, p->name) == 0 &&
119 try_module_get(p->owner)) {
120 clk = p;
121 break;
122 }
123 }
124 }
125
Arjan van de Ven00431702006-01-12 18:42:23 +0000126 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 return clk;
128}
129
130void clk_put(struct clk *clk)
131{
132 module_put(clk->owner);
133}
134
135int clk_enable(struct clk *clk)
136{
Ben Dooks2a513ce2006-02-08 21:09:05 +0000137 if (IS_ERR(clk) || clk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -EINVAL;
139
Ben Dooks2a513ce2006-02-08 21:09:05 +0000140 clk_enable(clk->parent);
141
142 mutex_lock(&clocks_mutex);
143
144 if ((clk->usage++) == 0)
145 (clk->enable)(clk, 1);
146
147 mutex_unlock(&clocks_mutex);
148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151void clk_disable(struct clk *clk)
152{
Ben Dooks2a513ce2006-02-08 21:09:05 +0000153 if (IS_ERR(clk) || clk == NULL)
154 return;
155
156 mutex_lock(&clocks_mutex);
157
158 if ((--clk->usage) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 (clk->enable)(clk, 0);
Ben Dooks2a513ce2006-02-08 21:09:05 +0000160
161 mutex_unlock(&clocks_mutex);
162 clk_disable(clk->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166unsigned long clk_get_rate(struct clk *clk)
167{
168 if (IS_ERR(clk))
169 return 0;
170
171 if (clk->rate != 0)
172 return clk->rate;
173
174 while (clk->parent != NULL && clk->rate == 0)
175 clk = clk->parent;
176
177 return clk->rate;
178}
179
180long clk_round_rate(struct clk *clk, unsigned long rate)
181{
182 return rate;
183}
184
185int clk_set_rate(struct clk *clk, unsigned long rate)
186{
187 return -EINVAL;
188}
189
190struct clk *clk_get_parent(struct clk *clk)
191{
192 return clk->parent;
193}
194
195EXPORT_SYMBOL(clk_get);
196EXPORT_SYMBOL(clk_put);
197EXPORT_SYMBOL(clk_enable);
198EXPORT_SYMBOL(clk_disable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199EXPORT_SYMBOL(clk_get_rate);
200EXPORT_SYMBOL(clk_round_rate);
201EXPORT_SYMBOL(clk_set_rate);
202EXPORT_SYMBOL(clk_get_parent);
203
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000204/* base clock enable */
205
206static int s3c24xx_upll_enable(struct clk *clk, int enable)
207{
208 unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
209 unsigned long orig = clkslow;
210
211 if (enable)
212 clkslow &= ~S3C2410_CLKSLOW_UCLK_OFF;
213 else
214 clkslow |= S3C2410_CLKSLOW_UCLK_OFF;
215
216 __raw_writel(clkslow, S3C2410_CLKSLOW);
217
218 /* if we started the UPLL, then allow to settle */
219
220 if (enable && !(orig & S3C2410_CLKSLOW_UCLK_OFF))
221 udelay(200);
222
223 return 0;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* base clocks */
227
228static struct clk clk_xtal = {
229 .name = "xtal",
230 .id = -1,
231 .rate = 0,
232 .parent = NULL,
233 .ctrlbit = 0,
234};
235
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000236static struct clk clk_upll = {
237 .name = "upll",
238 .id = -1,
239 .parent = NULL,
240 .enable = s3c24xx_upll_enable,
241 .ctrlbit = 0,
242};
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244static struct clk clk_f = {
245 .name = "fclk",
246 .id = -1,
247 .rate = 0,
248 .parent = NULL,
249 .ctrlbit = 0,
250};
251
252static struct clk clk_h = {
253 .name = "hclk",
254 .id = -1,
255 .rate = 0,
256 .parent = NULL,
257 .ctrlbit = 0,
258};
259
260static struct clk clk_p = {
261 .name = "pclk",
262 .id = -1,
263 .rate = 0,
264 .parent = NULL,
265 .ctrlbit = 0,
266};
267
268/* clocks that could be registered by external code */
269
270struct clk s3c24xx_dclk0 = {
271 .name = "dclk0",
272 .id = -1,
273};
274
275struct clk s3c24xx_dclk1 = {
276 .name = "dclk1",
277 .id = -1,
278};
279
280struct clk s3c24xx_clkout0 = {
281 .name = "clkout0",
282 .id = -1,
283};
284
285struct clk s3c24xx_clkout1 = {
286 .name = "clkout1",
287 .id = -1,
288};
289
290struct clk s3c24xx_uclk = {
291 .name = "uclk",
292 .id = -1,
293};
294
295
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000296/* standard clock definitions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298static struct clk init_clocks[] = {
Ben Dooksfe38ea52006-01-09 21:16:18 +0000299 {
300 .name = "nand",
301 .id = -1,
302 .parent = &clk_h,
303 .enable = s3c24xx_clkcon_enable,
304 .ctrlbit = S3C2410_CLKCON_NAND,
305 }, {
306 .name = "lcd",
307 .id = -1,
308 .parent = &clk_h,
309 .enable = s3c24xx_clkcon_enable,
310 .ctrlbit = S3C2410_CLKCON_LCDC,
311 }, {
312 .name = "usb-host",
313 .id = -1,
314 .parent = &clk_h,
315 .enable = s3c24xx_clkcon_enable,
316 .ctrlbit = S3C2410_CLKCON_USBH,
317 }, {
318 .name = "usb-device",
319 .id = -1,
320 .parent = &clk_h,
321 .enable = s3c24xx_clkcon_enable,
322 .ctrlbit = S3C2410_CLKCON_USBD,
323 }, {
324 .name = "timers",
325 .id = -1,
326 .parent = &clk_p,
327 .enable = s3c24xx_clkcon_enable,
328 .ctrlbit = S3C2410_CLKCON_PWMT,
329 }, {
330 .name = "sdi",
331 .id = -1,
332 .parent = &clk_p,
333 .enable = s3c24xx_clkcon_enable,
334 .ctrlbit = S3C2410_CLKCON_SDI,
335 }, {
336 .name = "uart",
337 .id = 0,
338 .parent = &clk_p,
339 .enable = s3c24xx_clkcon_enable,
340 .ctrlbit = S3C2410_CLKCON_UART0,
341 }, {
342 .name = "uart",
343 .id = 1,
344 .parent = &clk_p,
345 .enable = s3c24xx_clkcon_enable,
346 .ctrlbit = S3C2410_CLKCON_UART1,
347 }, {
348 .name = "uart",
349 .id = 2,
350 .parent = &clk_p,
351 .enable = s3c24xx_clkcon_enable,
352 .ctrlbit = S3C2410_CLKCON_UART2,
353 }, {
354 .name = "gpio",
355 .id = -1,
356 .parent = &clk_p,
357 .enable = s3c24xx_clkcon_enable,
358 .ctrlbit = S3C2410_CLKCON_GPIO,
359 }, {
360 .name = "rtc",
361 .id = -1,
362 .parent = &clk_p,
363 .enable = s3c24xx_clkcon_enable,
364 .ctrlbit = S3C2410_CLKCON_RTC,
365 }, {
366 .name = "adc",
367 .id = -1,
368 .parent = &clk_p,
369 .enable = s3c24xx_clkcon_enable,
370 .ctrlbit = S3C2410_CLKCON_ADC,
371 }, {
372 .name = "i2c",
373 .id = -1,
374 .parent = &clk_p,
375 .enable = s3c24xx_clkcon_enable,
376 .ctrlbit = S3C2410_CLKCON_IIC,
377 }, {
378 .name = "iis",
379 .id = -1,
380 .parent = &clk_p,
381 .enable = s3c24xx_clkcon_enable,
382 .ctrlbit = S3C2410_CLKCON_IIS,
383 }, {
384 .name = "spi",
385 .id = -1,
386 .parent = &clk_p,
387 .enable = s3c24xx_clkcon_enable,
388 .ctrlbit = S3C2410_CLKCON_SPI,
389 }, {
390 .name = "watchdog",
391 .id = -1,
392 .parent = &clk_p,
393 .ctrlbit = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395};
396
397/* initialise the clock system */
398
399int s3c24xx_register_clock(struct clk *clk)
400{
401 clk->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (clk->enable == NULL)
404 clk->enable = clk_null_enable;
405
Ben Dooks2a513ce2006-02-08 21:09:05 +0000406 /* if this is a standard clock, set the usage state */
407
408 if (clk->ctrlbit) {
409 unsigned long clkcon = __raw_readl(S3C2410_CLKCON);
410
411 clk->usage = (clkcon & clk->ctrlbit) ? 1 : 0;
412 }
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* add to the list of available clocks */
415
Arjan van de Ven00431702006-01-12 18:42:23 +0000416 mutex_lock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 list_add(&clk->list, &clocks);
Arjan van de Ven00431702006-01-12 18:42:23 +0000418 mutex_unlock(&clocks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 return 0;
421}
422
423/* initalise all the clocks */
424
425int __init s3c24xx_setup_clocks(unsigned long xtal,
426 unsigned long fclk,
427 unsigned long hclk,
428 unsigned long pclk)
429{
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000430 unsigned long upllcon = __raw_readl(S3C2410_UPLLCON);
Ben Dooksd6b0bf22005-08-29 22:46:30 +0100431 unsigned long clkslow = __raw_readl(S3C2410_CLKSLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 struct clk *clkp = init_clocks;
433 int ptr;
434 int ret;
435
436 printk(KERN_INFO "S3C2410 Clocks, (c) 2004 Simtec Electronics\n");
437
438 /* initialise the main system clocks */
439
440 clk_xtal.rate = xtal;
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000441 clk_upll.rate = s3c2410_get_pll(upllcon, xtal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 clk_h.rate = hclk;
444 clk_p.rate = pclk;
445 clk_f.rate = fclk;
446
Ben Dooksfe38ea52006-01-09 21:16:18 +0000447 /* We must be careful disabling the clocks we are not intending to
448 * be using at boot time, as subsytems such as the LCD which do
449 * their own DMA requests to the bus can cause the system to lockup
450 * if they where in the middle of requesting bus access.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
Ben Dooksfe38ea52006-01-09 21:16:18 +0000452 * Disabling the LCD clock if the LCD is active is very dangerous,
453 * and therefore the bootloader should be careful to not enable
454 * the LCD clock if it is not needed.
455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Ben Dooks2a513ce2006-02-08 21:09:05 +0000457 mutex_lock(&clocks_mutex);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 s3c24xx_clk_enable(S3C2410_CLKCON_NAND, 0);
460 s3c24xx_clk_enable(S3C2410_CLKCON_USBH, 0);
461 s3c24xx_clk_enable(S3C2410_CLKCON_USBD, 0);
462 s3c24xx_clk_enable(S3C2410_CLKCON_ADC, 0);
463 s3c24xx_clk_enable(S3C2410_CLKCON_IIC, 0);
464 s3c24xx_clk_enable(S3C2410_CLKCON_SPI, 0);
465
Ben Dooks2a513ce2006-02-08 21:09:05 +0000466 mutex_unlock(&clocks_mutex);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* assume uart clocks are correctly setup */
469
470 /* register our clocks */
471
472 if (s3c24xx_register_clock(&clk_xtal) < 0)
473 printk(KERN_ERR "failed to register master xtal\n");
474
Ben Dooks8e40a2f2006-03-20 17:10:04 +0000475 if (s3c24xx_register_clock(&clk_upll) < 0)
476 printk(KERN_ERR "failed to register upll clock\n");
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (s3c24xx_register_clock(&clk_f) < 0)
479 printk(KERN_ERR "failed to register cpu fclk\n");
480
481 if (s3c24xx_register_clock(&clk_h) < 0)
482 printk(KERN_ERR "failed to register cpu hclk\n");
483
484 if (s3c24xx_register_clock(&clk_p) < 0)
485 printk(KERN_ERR "failed to register cpu pclk\n");
486
487 /* register clocks from clock array */
488
489 for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
490 ret = s3c24xx_register_clock(clkp);
491 if (ret < 0) {
492 printk(KERN_ERR "Failed to register clock %s (%d)\n",
493 clkp->name, ret);
494 }
495 }
496
Ben Dooksd6b0bf22005-08-29 22:46:30 +0100497 /* show the clock-slow value */
498
499 printk("CLOCK: Slow mode (%ld.%ld MHz), %s, MPLL %s, UPLL %s\n",
500 print_mhz(xtal / ( 2 * S3C2410_CLKSLOW_GET_SLOWVAL(clkslow))),
501 (clkslow & S3C2410_CLKSLOW_SLOW) ? "slow" : "fast",
502 (clkslow & S3C2410_CLKSLOW_MPLL_OFF) ? "off" : "on",
503 (clkslow & S3C2410_CLKSLOW_UCLK_OFF) ? "off" : "on");
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return 0;
506}