blob: f85853c5d5eb8e7086db653b1ff53b37f7e2fb8e [file] [log] [blame]
Ben Dooksa21765a2007-02-11 18:31:01 +01001/* linux/arch/arm/mach-s3c2440/clock.c
Ben Dooksa8d11e32005-07-26 22:39:14 +01002 *
3 * Copyright (c) 2004-2005 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
6 *
7 * S3C2440 Clock support
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22*/
23
24#include <linux/init.h>
25#include <linux/module.h>
26#include <linux/kernel.h>
27#include <linux/list.h>
28#include <linux/errno.h>
29#include <linux/err.h>
30#include <linux/device.h>
31#include <linux/sysdev.h>
Ben Dooksa8d11e32005-07-26 22:39:14 +010032#include <linux/interrupt.h>
33#include <linux/ioport.h>
Ben Dooks36c64af2006-03-20 21:00:48 +000034#include <linux/mutex.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000035#include <linux/clk.h>
Russell Kingfced80c2008-09-06 12:10:45 +010036#include <linux/io.h>
Thomas Abraham046c2172011-10-24 11:47:40 +020037#include <linux/serial_core.h>
Ben Dooksa8d11e32005-07-26 22:39:14 +010038
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/hardware.h>
Arun Sharma600634972011-07-26 16:09:06 -070040#include <linux/atomic.h>
Ben Dooksa8d11e32005-07-26 22:39:14 +010041#include <asm/irq.h>
Ben Dooksa8d11e32005-07-26 22:39:14 +010042
Russell Kinga09e64f2008-08-05 16:14:15 +010043#include <mach/regs-clock.h>
Ben Dooksa8d11e32005-07-26 22:39:14 +010044
Ben Dooksd5120ae2008-10-07 23:09:51 +010045#include <plat/clock.h>
Ben Dooksa2b7ba92008-10-07 22:26:09 +010046#include <plat/cpu.h>
Thomas Abraham046c2172011-10-24 11:47:40 +020047#include <plat/regs-serial.h>
Ben Dooksa8d11e32005-07-26 22:39:14 +010048
49/* S3C2440 extended clock support */
50
Ben Dookse44c0392006-03-20 21:00:12 +000051static unsigned long s3c2440_camif_upll_round(struct clk *clk,
52 unsigned long rate)
53{
54 unsigned long parent_rate = clk_get_rate(clk->parent);
55 int div;
56
57 if (rate > parent_rate)
58 return parent_rate;
59
60 /* note, we remove the +/- 1 calculations for the divisor */
61
62 div = (parent_rate / rate) / 2;
63
64 if (div < 1)
65 div = 1;
66 else if (div > 16)
67 div = 16;
68
69 return parent_rate / (div * 2);
70}
71
72static int s3c2440_camif_upll_setrate(struct clk *clk, unsigned long rate)
73{
74 unsigned long parent_rate = clk_get_rate(clk->parent);
75 unsigned long camdivn = __raw_readl(S3C2440_CAMDIVN);
76
77 rate = s3c2440_camif_upll_round(clk, rate);
78
79 camdivn &= ~(S3C2440_CAMDIVN_CAMCLK_SEL | S3C2440_CAMDIVN_CAMCLK_MASK);
80
81 if (rate != parent_rate) {
82 camdivn |= S3C2440_CAMDIVN_CAMCLK_SEL;
83 camdivn |= (((parent_rate / rate) / 2) - 1);
84 }
85
86 __raw_writel(camdivn, S3C2440_CAMDIVN);
87
88 return 0;
89}
90
91/* Extra S3C2440 clocks */
92
Ben Dooksa8d11e32005-07-26 22:39:14 +010093static struct clk s3c2440_clk_cam = {
94 .name = "camif",
Ben Dooks99c13852006-06-22 22:18:20 +010095 .enable = s3c2410_clkcon_enable,
Ben Dooksa8d11e32005-07-26 22:39:14 +010096 .ctrlbit = S3C2440_CLKCON_CAMERA,
97};
98
Ben Dookse44c0392006-03-20 21:00:12 +000099static struct clk s3c2440_clk_cam_upll = {
100 .name = "camif-upll",
Ben Dooksb3bf41b2009-12-01 01:24:37 +0000101 .ops = &(struct clk_ops) {
102 .set_rate = s3c2440_camif_upll_setrate,
103 .round_rate = s3c2440_camif_upll_round,
104 },
Ben Dookse44c0392006-03-20 21:00:12 +0000105};
106
Ben Dooksa8d11e32005-07-26 22:39:14 +0100107static struct clk s3c2440_clk_ac97 = {
108 .name = "ac97",
Ben Dooks99c13852006-06-22 22:18:20 +0100109 .enable = s3c2410_clkcon_enable,
Ben Dooksa8d11e32005-07-26 22:39:14 +0100110 .ctrlbit = S3C2440_CLKCON_CAMERA,
111};
112
Thomas Abraham046c2172011-10-24 11:47:40 +0200113static unsigned long s3c2440_fclk_n_getrate(struct clk *clk)
114{
115 unsigned long ucon0, ucon1, ucon2, divisor;
116
117 /* the fun of calculating the uart divisors on the s3c2440 */
118 ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
119 ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
120 ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
121
122 ucon0 &= S3C2440_UCON0_DIVMASK;
123 ucon1 &= S3C2440_UCON1_DIVMASK;
124 ucon2 &= S3C2440_UCON2_DIVMASK;
125
126 if (ucon0 != 0)
127 divisor = (ucon0 >> S3C2440_UCON_DIVSHIFT) + 6;
128 else if (ucon1 != 0)
129 divisor = (ucon1 >> S3C2440_UCON_DIVSHIFT) + 21;
130 else if (ucon2 != 0)
131 divisor = (ucon2 >> S3C2440_UCON_DIVSHIFT) + 36;
132 else
133 /* manual calims 44, seems to be 9 */
134 divisor = 9;
135
136 return clk_get_rate(clk->parent) / divisor;
137}
138
139static struct clk s3c2440_clk_fclk_n = {
140 .name = "fclk_n",
141 .parent = &clk_f,
142 .ops = &(struct clk_ops) {
143 .get_rate = s3c2440_fclk_n_getrate,
144 },
145};
146
Ben Dooksa8d11e32005-07-26 22:39:14 +0100147static int s3c2440_clk_add(struct sys_device *sysdev)
148{
Ben Dooks3a38e4b2008-01-28 13:01:34 +0100149 struct clk *clock_upll;
Ben Dookse546e8a2006-12-17 20:38:14 +0100150 struct clk *clock_h;
151 struct clk *clock_p;
Ben Dooksa8d11e32005-07-26 22:39:14 +0100152
Ben Dookse546e8a2006-12-17 20:38:14 +0100153 clock_p = clk_get(NULL, "pclk");
154 clock_h = clk_get(NULL, "hclk");
155 clock_upll = clk_get(NULL, "upll");
Ben Dooksa8d11e32005-07-26 22:39:14 +0100156
Ben Dookse546e8a2006-12-17 20:38:14 +0100157 if (IS_ERR(clock_p) || IS_ERR(clock_h) || IS_ERR(clock_upll)) {
Ben Dooksa8d11e32005-07-26 22:39:14 +0100158 printk(KERN_ERR "S3C2440: Failed to get parent clocks\n");
159 return -EINVAL;
160 }
161
Ben Dookse546e8a2006-12-17 20:38:14 +0100162 s3c2440_clk_cam.parent = clock_h;
163 s3c2440_clk_ac97.parent = clock_p;
164 s3c2440_clk_cam_upll.parent = clock_upll;
Thomas Abraham046c2172011-10-24 11:47:40 +0200165 s3c24xx_register_clock(&s3c2440_clk_fclk_n);
Ben Dooksa8d11e32005-07-26 22:39:14 +0100166
167 s3c24xx_register_clock(&s3c2440_clk_ac97);
168 s3c24xx_register_clock(&s3c2440_clk_cam);
Ben Dookse44c0392006-03-20 21:00:12 +0000169 s3c24xx_register_clock(&s3c2440_clk_cam_upll);
Ben Dooksa8d11e32005-07-26 22:39:14 +0100170
171 clk_disable(&s3c2440_clk_ac97);
172 clk_disable(&s3c2440_clk_cam);
173
174 return 0;
175}
176
177static struct sysdev_driver s3c2440_clk_driver = {
178 .add = s3c2440_clk_add,
179};
180
181static __init int s3c24xx_clk_driver(void)
182{
183 return sysdev_driver_register(&s3c2440_sysclass, &s3c2440_clk_driver);
184}
185
186arch_initcall(s3c24xx_clk_driver);