blob: cd51d04e1de7f429a3fce43ff1db4e801a5ed480 [file] [log] [blame]
Ben Dookse4d06e32007-02-16 12:12:31 +01001/* linux/arch/arm/mach-s3c2443/clock.c
2 *
Ben Dooks4bed36b2010-01-30 10:25:49 +02003 * Copyright (c) 2007, 2010 Simtec Electronics
Ben Dookse4d06e32007-02-16 12:12:31 +01004 * Ben Dooks <ben@simtec.co.uk>
5 *
6 * S3C2443 Clock control support
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21*/
22
23#include <linux/init.h>
Ben Dooksaf337f32010-04-28 18:03:57 +090024
Ben Dookse4d06e32007-02-16 12:12:31 +010025#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/sysdev.h>
31#include <linux/clk.h>
32#include <linux/mutex.h>
Ben Dookse4d06e32007-02-16 12:12:31 +010033#include <linux/serial_core.h>
Russell Kingfced80c2008-09-06 12:10:45 +010034#include <linux/io.h>
Ben Dookse4d06e32007-02-16 12:12:31 +010035
36#include <asm/mach/map.h>
37
Russell Kinga09e64f2008-08-05 16:14:15 +010038#include <mach/hardware.h>
Ben Dookse4d06e32007-02-16 12:12:31 +010039
Russell Kinga09e64f2008-08-05 16:14:15 +010040#include <mach/regs-s3c2443-clock.h>
Ben Dookse4d06e32007-02-16 12:12:31 +010041
Ben Dookse4253822008-10-21 14:06:38 +010042#include <plat/cpu-freq.h>
43
Ben Dooksa2b7ba92008-10-07 22:26:09 +010044#include <plat/s3c2443.h>
Ben Dooksd5120ae2008-10-07 23:09:51 +010045#include <plat/clock.h>
Ben Dooks9aa753c2010-01-30 09:19:59 +020046#include <plat/clock-clksrc.h>
Ben Dooksa2b7ba92008-10-07 22:26:09 +010047#include <plat/cpu.h>
Ben Dookse4d06e32007-02-16 12:12:31 +010048
49/* We currently have to assume that the system is running
50 * from the XTPll input, and that all ***REFCLKs are being
51 * fed from it, as we cannot read the state of OM[4] from
52 * software.
53 *
54 * It would be possible for each board initialisation to
55 * set the correct muxing at initialisation
56*/
57
Ben Dookse4d06e32007-02-16 12:12:31 +010058/* clock selections */
59
Ben Dooksba7622a2008-07-07 18:12:39 +010060/* armdiv
61 *
62 * this clock is sourced from msysclk and can have a number of
63 * divider values applied to it to then be fed into armclk.
64*/
65
Ben Dooks41f23a02010-01-30 11:14:14 +020066/* armdiv divisor table */
67
68static unsigned int armdiv[16] = {
69 [S3C2443_CLKDIV0_ARMDIV_1 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 1,
70 [S3C2443_CLKDIV0_ARMDIV_2 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 2,
71 [S3C2443_CLKDIV0_ARMDIV_3 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 3,
72 [S3C2443_CLKDIV0_ARMDIV_4 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 4,
73 [S3C2443_CLKDIV0_ARMDIV_6 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 6,
74 [S3C2443_CLKDIV0_ARMDIV_8 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 8,
75 [S3C2443_CLKDIV0_ARMDIV_12 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 12,
76 [S3C2443_CLKDIV0_ARMDIV_16 >> S3C2443_CLKDIV0_ARMDIV_SHIFT] = 16,
77};
78
79static inline unsigned int s3c2443_fclk_div(unsigned long clkcon0)
80{
81 clkcon0 &= S3C2443_CLKDIV0_ARMDIV_MASK;
82
83 return armdiv[clkcon0 >> S3C2443_CLKDIV0_ARMDIV_SHIFT];
84}
85
86static unsigned long s3c2443_armclk_roundrate(struct clk *clk,
87 unsigned long rate)
88{
89 unsigned long parent = clk_get_rate(clk->parent);
90 unsigned long calc;
91 unsigned best = 256; /* bigger than any value */
92 unsigned div;
93 int ptr;
94
95 for (ptr = 0; ptr < ARRAY_SIZE(armdiv); ptr++) {
96 div = armdiv[ptr];
97 calc = parent / div;
98 if (calc <= rate && div < best)
99 best = div;
100 }
101
102 return parent / best;
103}
104
105static int s3c2443_armclk_setrate(struct clk *clk, unsigned long rate)
106{
107 unsigned long parent = clk_get_rate(clk->parent);
108 unsigned long calc;
109 unsigned div;
110 unsigned best = 256; /* bigger than any value */
111 int ptr;
112 int val = -1;
113
114 for (ptr = 0; ptr < ARRAY_SIZE(armdiv); ptr++) {
115 div = armdiv[ptr];
116 calc = parent / div;
117 if (calc <= rate && div < best) {
118 best = div;
119 val = ptr;
120 }
121 }
122
123 if (val >= 0) {
124 unsigned long clkcon0;
125
126 clkcon0 = __raw_readl(S3C2443_CLKDIV0);
Heiko Stuebner43446462011-09-26 10:30:29 +0900127 clkcon0 &= ~S3C2443_CLKDIV0_ARMDIV_MASK;
Ben Dooks41f23a02010-01-30 11:14:14 +0200128 clkcon0 |= val << S3C2443_CLKDIV0_ARMDIV_SHIFT;
129 __raw_writel(clkcon0, S3C2443_CLKDIV0);
130 }
131
132 return (val == -1) ? -EINVAL : 0;
133}
134
Ben Dooksba7622a2008-07-07 18:12:39 +0100135static struct clk clk_armdiv = {
136 .name = "armdiv",
Ben Dooks4bed36b2010-01-30 10:25:49 +0200137 .parent = &clk_msysclk.clk,
Ben Dooks41f23a02010-01-30 11:14:14 +0200138 .ops = &(struct clk_ops) {
139 .round_rate = s3c2443_armclk_roundrate,
140 .set_rate = s3c2443_armclk_setrate,
141 },
Ben Dooksba7622a2008-07-07 18:12:39 +0100142};
143
144/* armclk
145 *
Ben Dooks4bed36b2010-01-30 10:25:49 +0200146 * this is the clock fed into the ARM core itself, from armdiv or from hclk.
Ben Dooksba7622a2008-07-07 18:12:39 +0100147 */
148
Ben Dooks4bed36b2010-01-30 10:25:49 +0200149static struct clk *clk_arm_sources[] = {
150 [0] = &clk_armdiv,
151 [1] = &clk_h,
152};
Ben Dooksba7622a2008-07-07 18:12:39 +0100153
Ben Dooks4bed36b2010-01-30 10:25:49 +0200154static struct clksrc_clk clk_arm = {
155 .clk = {
156 .name = "armclk",
Ben Dooksb3bf41b2009-12-01 01:24:37 +0000157 },
Ben Dooks4bed36b2010-01-30 10:25:49 +0200158 .sources = &(struct clksrc_sources) {
159 .sources = clk_arm_sources,
160 .nr_sources = ARRAY_SIZE(clk_arm_sources),
161 },
162 .reg_src = { .reg = S3C2443_CLKDIV0, .size = 1, .shift = 13 },
Ben Dooksba7622a2008-07-07 18:12:39 +0100163};
Ben Dookse4d06e32007-02-16 12:12:31 +0100164
Ben Dookse4d06e32007-02-16 12:12:31 +0100165/* hsspi
166 *
167 * high-speed spi clock, sourced from esysclk
168*/
169
Ben Dooks9aa753c2010-01-30 09:19:59 +0200170static struct clksrc_clk clk_hsspi = {
171 .clk = {
Heiko Stuebner8b069b72011-09-27 08:45:23 +0900172 .name = "hsspi-if",
Ben Dooks4bed36b2010-01-30 10:25:49 +0200173 .parent = &clk_esysclk.clk,
Ben Dooks9aa753c2010-01-30 09:19:59 +0200174 .ctrlbit = S3C2443_SCLKCON_HSSPICLK,
175 .enable = s3c2443_clkcon_enable_s,
Ben Dooksb3bf41b2009-12-01 01:24:37 +0000176 },
Ben Dooks9aa753c2010-01-30 09:19:59 +0200177 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 4 },
Ben Dookse4d06e32007-02-16 12:12:31 +0100178};
179
Ben Dookse4d06e32007-02-16 12:12:31 +0100180
181/* clk_hsmcc_div
182 *
183 * this clock is sourced from epll, and is fed through a divider,
184 * to a mux controlled by sclkcon where either it or a extclk can
185 * be fed to the hsmmc block
186*/
187
Ben Dooks9aa753c2010-01-30 09:19:59 +0200188static struct clksrc_clk clk_hsmmc_div = {
189 .clk = {
190 .name = "hsmmc-div",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900191 .devname = "s3c-sdhci.1",
Ben Dooks4bed36b2010-01-30 10:25:49 +0200192 .parent = &clk_esysclk.clk,
Ben Dooksb3bf41b2009-12-01 01:24:37 +0000193 },
Ben Dooks9aa753c2010-01-30 09:19:59 +0200194 .reg_div = { .reg = S3C2443_CLKDIV1, .size = 2, .shift = 6 },
Ben Dookse4d06e32007-02-16 12:12:31 +0100195};
196
197static int s3c2443_setparent_hsmmc(struct clk *clk, struct clk *parent)
198{
199 unsigned long clksrc = __raw_readl(S3C2443_SCLKCON);
200
201 clksrc &= ~(S3C2443_SCLKCON_HSMMCCLK_EXT |
202 S3C2443_SCLKCON_HSMMCCLK_EPLL);
203
204 if (parent == &clk_epll)
205 clksrc |= S3C2443_SCLKCON_HSMMCCLK_EPLL;
206 else if (parent == &clk_ext)
207 clksrc |= S3C2443_SCLKCON_HSMMCCLK_EXT;
208 else
209 return -EINVAL;
210
211 if (clk->usage > 0) {
212 __raw_writel(clksrc, S3C2443_SCLKCON);
213 }
214
215 clk->parent = parent;
216 return 0;
217}
218
219static int s3c2443_enable_hsmmc(struct clk *clk, int enable)
220{
221 return s3c2443_setparent_hsmmc(clk, clk->parent);
222}
223
224static struct clk clk_hsmmc = {
225 .name = "hsmmc-if",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900226 .devname = "s3c-sdhci.1",
Ben Dooks9aa753c2010-01-30 09:19:59 +0200227 .parent = &clk_hsmmc_div.clk,
Ben Dookse4d06e32007-02-16 12:12:31 +0100228 .enable = s3c2443_enable_hsmmc,
Ben Dooksb3bf41b2009-12-01 01:24:37 +0000229 .ops = &(struct clk_ops) {
230 .set_parent = s3c2443_setparent_hsmmc,
231 },
Ben Dookse4d06e32007-02-16 12:12:31 +0100232};
233
Ben Dookse4d06e32007-02-16 12:12:31 +0100234/* standard clock definitions */
235
Ben Dooks4e046912010-04-28 12:58:13 +0900236static struct clk init_clocks_off[] = {
Ben Dookse4d06e32007-02-16 12:12:31 +0100237 {
Ben Dookse4d06e32007-02-16 12:12:31 +0100238 .name = "sdi",
Ben Dookse4d06e32007-02-16 12:12:31 +0100239 .parent = &clk_p,
240 .enable = s3c2443_clkcon_enable_p,
241 .ctrlbit = S3C2443_PCLKCON_SDI,
242 }, {
Ben Dookse4d06e32007-02-16 12:12:31 +0100243 .name = "spi",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900244 .devname = "s3c2410-spi.0",
Ben Dookse4d06e32007-02-16 12:12:31 +0100245 .parent = &clk_p,
246 .enable = s3c2443_clkcon_enable_p,
247 .ctrlbit = S3C2443_PCLKCON_SPI0,
248 }, {
249 .name = "spi",
Thomas Abrahame83626f2011-06-14 19:12:26 +0900250 .devname = "s3c2410-spi.1",
Ben Dookse4d06e32007-02-16 12:12:31 +0100251 .parent = &clk_p,
252 .enable = s3c2443_clkcon_enable_p,
253 .ctrlbit = S3C2443_PCLKCON_SPI1,
254 }
255};
256
257static struct clk init_clocks[] = {
Ben Dookse4d06e32007-02-16 12:12:31 +0100258};
259
Ben Dookse4d06e32007-02-16 12:12:31 +0100260/* clocks to add straight away */
261
Ben Dooks9aa753c2010-01-30 09:19:59 +0200262static struct clksrc_clk *clksrcs[] __initdata = {
Ben Dooks4bed36b2010-01-30 10:25:49 +0200263 &clk_arm,
Ben Dookse4d06e32007-02-16 12:12:31 +0100264 &clk_hsspi,
265 &clk_hsmmc_div,
Ben Dooks9aa753c2010-01-30 09:19:59 +0200266};
267
268static struct clk *clks[] __initdata = {
Ben Dookse4d06e32007-02-16 12:12:31 +0100269 &clk_hsmmc,
Ben Dooksba7622a2008-07-07 18:12:39 +0100270 &clk_armdiv,
Ben Dookse4d06e32007-02-16 12:12:31 +0100271};
272
Ben Dookse4253822008-10-21 14:06:38 +0100273void __init_or_cpufreq s3c2443_setup_clocks(void)
Ben Dookse4d06e32007-02-16 12:12:31 +0100274{
Ben Dooksaf337f32010-04-28 18:03:57 +0900275 s3c2443_common_setup_clocks(s3c2443_get_mpll, s3c2443_fclk_div);
Ben Dookse4253822008-10-21 14:06:38 +0100276}
277
278void __init s3c2443_init_clocks(int xtal)
279{
Ben Dookse4253822008-10-21 14:06:38 +0100280 unsigned long epllcon = __raw_readl(S3C2443_EPLLCON);
Ben Dookse4253822008-10-21 14:06:38 +0100281 int ptr;
282
Ben Dooksaf337f32010-04-28 18:03:57 +0900283 clk_epll.rate = s3c2443_get_epll(epllcon, xtal);
284 clk_epll.parent = &clk_epllref.clk;
Ben Dookse4253822008-10-21 14:06:38 +0100285
Ben Dooksaf337f32010-04-28 18:03:57 +0900286 s3c2443_common_init_clocks(xtal, s3c2443_get_mpll, s3c2443_fclk_div);
287
Ben Dookse4253822008-10-21 14:06:38 +0100288 s3c2443_setup_clocks();
Ben Dookse4d06e32007-02-16 12:12:31 +0100289
Ben Dooks4e046912010-04-28 12:58:13 +0900290 s3c24xx_register_clocks(clks, ARRAY_SIZE(clks));
Ben Dookse4d06e32007-02-16 12:12:31 +0100291
Ben Dooks9aa753c2010-01-30 09:19:59 +0200292 for (ptr = 0; ptr < ARRAY_SIZE(clksrcs); ptr++)
293 s3c_register_clksrc(clksrcs[ptr], 1);
294
Ben Dookse4d06e32007-02-16 12:12:31 +0100295 /* register clocks from clock array */
296
Ben Dooks1d9f13c2010-01-06 01:21:38 +0900297 s3c_register_clocks(init_clocks, ARRAY_SIZE(init_clocks));
Ben Dookse4d06e32007-02-16 12:12:31 +0100298
299 /* We must be careful disabling the clocks we are not intending to
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200300 * be using at boot time, as subsystems such as the LCD which do
Ben Dookse4d06e32007-02-16 12:12:31 +0100301 * their own DMA requests to the bus can cause the system to lockup
302 * if they where in the middle of requesting bus access.
303 *
304 * Disabling the LCD clock if the LCD is active is very dangerous,
305 * and therefore the bootloader should be careful to not enable
306 * the LCD clock if it is not needed.
307 */
308
309 /* install (and disable) the clocks we do not need immediately */
310
Ben Dooks4e046912010-04-28 12:58:13 +0900311 s3c_register_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
312 s3c_disable_clocks(init_clocks_off, ARRAY_SIZE(init_clocks_off));
Ben Dooks9d325f22008-11-21 10:36:05 +0000313
314 s3c_pwmclk_init();
Ben Dookse4d06e32007-02-16 12:12:31 +0100315}