This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / arm / mach-s3c2410 / clock.c
1 /* linux/arch/arm/mach-s3c2410/clock.c
2  *
3  * Copyright (c) 2004 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>
35
36 #include <linux/interrupt.h>
37 #include <linux/ioport.h>
38
39 #include <asm/hardware.h>
40 #include <asm/atomic.h>
41 #include <asm/irq.h>
42 #include <asm/io.h>
43
44 #include <asm/hardware/clock.h>
45 #include <asm/arch/regs-clock.h>
46
47 #include "clock.h"
48
49
50 static LIST_HEAD(clocks);
51 static DECLARE_MUTEX(clocks_sem);
52
53
54 /* old functions */
55
56 void s3c2410_clk_enable(unsigned int clocks, unsigned int enable)
57 {
58         unsigned long clkcon;
59         unsigned long flags;
60
61         local_irq_save(flags);
62
63         clkcon = __raw_readl(S3C2410_CLKCON);
64         clkcon &= ~clocks;
65
66         if (enable)
67                 clkcon |= clocks;
68
69         __raw_writel(clkcon, S3C2410_CLKCON);
70
71         local_irq_restore(flags);
72 }
73
74
75 /* Clock API calls */
76
77 struct clk *clk_get(struct device *dev, const char *id)
78 {
79         struct clk *p;
80         struct clk *clk = ERR_PTR(-ENOENT);
81
82         down(&clocks_sem);
83         list_for_each_entry(p, &clocks, list) {
84                 if (strcmp(id, p->name) == 0 &&
85                     try_module_get(p->owner)) {
86                         clk = p;
87                         break;
88                 }
89         }
90         up(&clocks_sem);
91
92         return clk;
93 }
94
95 void clk_put(struct clk *clk)
96 {
97         module_put(clk->owner);
98 }
99
100 int clk_enable(struct clk *clk)
101 {
102         if (clk->ctrlbit != 0)
103                 s3c2410_clk_enable(clk->ctrlbit, 1);
104
105         return 0;
106 }
107
108 void clk_disable(struct clk *clk)
109 {
110         s3c2410_clk_enable(clk->ctrlbit, 0);
111 }
112
113
114 int clk_use(struct clk *clk)
115 {
116         atomic_inc(&clk->used);
117         return 0;
118 }
119
120
121 void clk_unuse(struct clk *clk)
122 {
123         atomic_dec(&clk->used);
124 }
125
126 unsigned long clk_get_rate(struct clk *clk)
127 {
128         if (clk->parent != NULL)
129                 return clk->parent->rate;
130
131         return clk->rate;
132 }
133
134 long clk_round_rate(struct clk *clk, unsigned long rate)
135 {
136         return rate;
137 }
138
139 int clk_set_rate(struct clk *clk, unsigned long rate)
140 {
141         return -EINVAL;
142 }
143
144 struct clk *clk_get_parent(struct clk *clk)
145 {
146         return clk->parent;
147 }
148
149 EXPORT_SYMBOL(clk_get);
150 EXPORT_SYMBOL(clk_put);
151 EXPORT_SYMBOL(clk_enable);
152 EXPORT_SYMBOL(clk_disable);
153 EXPORT_SYMBOL(clk_use);
154 EXPORT_SYMBOL(clk_unuse);
155 EXPORT_SYMBOL(clk_get_rate);
156 EXPORT_SYMBOL(clk_round_rate);
157 EXPORT_SYMBOL(clk_set_rate);
158 EXPORT_SYMBOL(clk_get_parent);
159
160 /* base clocks */
161
162 static struct clk clk_f = {
163         .name          = "fclk",
164         .rate          = 0,
165         .parent        = NULL,
166         .ctrlbit       = 0
167 };
168
169 static struct clk clk_h = {
170         .name          = "hclk",
171         .rate          = 0,
172         .parent        = NULL,
173         .ctrlbit       = 0
174 };
175
176 static struct clk clk_p = {
177         .name          = "pclk",
178         .rate          = 0,
179         .parent        = NULL,
180         .ctrlbit       = 0
181 };
182
183 /* clock definitions */
184
185 static struct clk init_clocks[] = {
186         { .name    = "nand",
187           .parent  = &clk_h,
188           .ctrlbit = S3C2410_CLKCON_NAND
189         },
190         { .name    = "lcd",
191           .parent  = &clk_h,
192           .ctrlbit = S3C2410_CLKCON_LCDC
193         },
194         { .name    = "usb-host",
195           .parent  = &clk_h,
196           .ctrlbit =   S3C2410_CLKCON_USBH
197         },
198         { .name    = "usb-device",
199           .parent  = &clk_h,
200           .ctrlbit = S3C2410_CLKCON_USBD
201         },
202         { .name    = "timers",
203           .parent  = &clk_p,
204           .ctrlbit = S3C2410_CLKCON_PWMT
205         },
206         { .name    = "sdi",
207           .parent  = &clk_p,
208           .ctrlbit = S3C2410_CLKCON_SDI
209         },
210         { .name    = "uart0",
211           .parent  = &clk_p,
212           .ctrlbit = S3C2410_CLKCON_UART0
213         },
214         { .name    = "uart1",
215           .parent  = &clk_p,
216           .ctrlbit = S3C2410_CLKCON_UART1
217         },
218         { .name    = "uart2",
219           .parent  = &clk_p,
220           .ctrlbit = S3C2410_CLKCON_UART2
221         },
222         { .name    = "gpio",
223           .parent  = &clk_p,
224           .ctrlbit = S3C2410_CLKCON_GPIO
225         },
226         { .name    = "rtc",
227           .parent  = &clk_p,
228           .ctrlbit = S3C2410_CLKCON_RTC
229         },
230         { .name    = "adc",
231           .parent  = &clk_p,
232           .ctrlbit = S3C2410_CLKCON_ADC
233         },
234         { .name    = "i2c",
235           .parent  = &clk_p,
236           .ctrlbit = S3C2410_CLKCON_IIC
237         },
238         { .name    = "iis",
239           .parent  = &clk_p,
240           .ctrlbit = S3C2410_CLKCON_IIS
241         },
242         { .name    = "spi",
243           .parent  = &clk_p,
244           .ctrlbit = S3C2410_CLKCON_SPI
245         },
246         { .name    = "watchdog",
247           .parent  = &clk_p,
248           .ctrlbit = 0
249         }
250 };
251
252 /* initialise the clock system */
253
254 int s3c2410_register_clock(struct clk *clk)
255 {
256         clk->owner = THIS_MODULE;
257         atomic_set(&clk->used, 0);
258
259         /* add to the list of available clocks */
260
261         down(&clocks_sem);
262         list_add(&clk->list, &clocks);
263         up(&clocks_sem);
264
265         return 0;
266 }
267
268 /* initalise all the clocks */
269
270 static int __init s3c2410_init_clocks(void)
271 {
272         struct clk *clkp = init_clocks;
273         int ptr;
274         int ret;
275
276         printk(KERN_INFO "S3C2410 Clock control, (c) 2004 Simtec Electronics\n");
277
278         /* initialise the main system clocks */
279
280         clk_h.rate = s3c2410_hclk;
281         clk_p.rate = s3c2410_pclk;
282         clk_f.rate = s3c2410_fclk;
283
284         /* set the enabled clocks to a minimal (known) state */
285         __raw_writel(S3C2410_CLKCON_PWMT | S3C2410_CLKCON_UART0 | S3C2410_CLKCON_UART1 | S3C2410_CLKCON_UART2 | S3C2410_CLKCON_GPIO | S3C2410_CLKCON_RTC, S3C2410_CLKCON);
286
287         /* register our clocks */
288
289         if (s3c2410_register_clock(&clk_f) < 0)
290                 printk(KERN_ERR "failed to register cpu fclk\n");
291
292         if (s3c2410_register_clock(&clk_h) < 0)
293                 printk(KERN_ERR "failed to register cpu hclk\n");
294
295         if (s3c2410_register_clock(&clk_p) < 0)
296                 printk(KERN_ERR "failed to register cpu pclk\n");
297
298         for (ptr = 0; ptr < ARRAY_SIZE(init_clocks); ptr++, clkp++) {
299                 ret = s3c2410_register_clock(clkp);
300                 if (ret < 0) {
301                         printk(KERN_ERR "Failed to register clock %s (%d)\n",
302                                clkp->name, ret);
303                 }
304         }
305
306         return 0;
307 }
308
309 arch_initcall(s3c2410_init_clocks);
310