2 * Copyright 2008 Simtec Electronics
3 * http://armlinux.simtec.co.uk/
4 * Ben Dooks <ben@simtec.co.uk>
6 * S3C2412 CPU Frequency scalling
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
17 #include <linux/cpufreq.h>
18 #include <linux/device.h>
19 #include <linux/delay.h>
20 #include <linux/clk.h>
21 #include <linux/err.h>
24 #include <asm/mach/arch.h>
25 #include <asm/mach/map.h>
27 #include <mach/regs-clock.h>
28 #include <mach/s3c2412.h>
31 #include <plat/clock.h>
32 #include <plat/cpu-freq-core.h>
34 /* our clock resources. */
35 static struct clk *xtal;
36 static struct clk *fclk;
37 static struct clk *hclk;
38 static struct clk *armclk;
40 /* HDIV: 1, 2, 3, 4, 6, 8 */
42 static int s3c2412_cpufreq_calcdivs(struct s3c_cpufreq_config *cfg)
44 unsigned int hdiv, pdiv, armdiv, dvs;
45 unsigned long hclk, fclk, armclk, armdiv_clk;
46 unsigned long hclk_max;
48 fclk = cfg->freq.fclk;
49 armclk = cfg->freq.armclk;
50 hclk_max = cfg->max.hclk;
52 /* We can't run hclk above armclk as at the best we have to
53 * have armclk and hclk in dvs mode. */
55 if (hclk_max > armclk)
58 s3c_freq_dbg("%s: fclk=%lu, armclk=%lu, hclk_max=%lu\n",
59 __func__, fclk, armclk, hclk_max);
60 s3c_freq_dbg("%s: want f=%lu, arm=%lu, h=%lu, p=%lu\n",
61 __func__, cfg->freq.fclk, cfg->freq.armclk,
62 cfg->freq.hclk, cfg->freq.pclk);
64 armdiv = fclk / armclk;
71 cfg->divs.arm_divisor = armdiv;
72 armdiv_clk = fclk / armdiv;
74 hdiv = armdiv_clk / hclk_max;
78 cfg->freq.hclk = hclk = armdiv_clk / hdiv;
80 /* set dvs depending on whether we reached armclk or not. */
81 cfg->divs.dvs = dvs = armclk < armdiv_clk;
83 /* update the actual armclk we achieved. */
84 cfg->freq.armclk = dvs ? hclk : armdiv_clk;
86 s3c_freq_dbg("%s: armclk %lu, hclk %lu, armdiv %d, hdiv %d, dvs %d\n",
87 __func__, armclk, hclk, armdiv, hdiv, cfg->divs.dvs);
92 pdiv = (hclk > cfg->max.pclk) ? 2 : 1;
94 if ((hclk / pdiv) > cfg->max.pclk)
97 cfg->freq.pclk = hclk / pdiv;
99 s3c_freq_dbg("%s: pdiv %d\n", __func__, pdiv);
106 /* store the result, and then return */
108 cfg->divs.h_divisor = hdiv * armdiv;
109 cfg->divs.p_divisor = pdiv * armdiv;
117 static void s3c2412_cpufreq_setdivs(struct s3c_cpufreq_config *cfg)
119 unsigned long clkdiv;
120 unsigned long olddiv;
122 olddiv = clkdiv = __raw_readl(S3C2410_CLKDIVN);
124 /* clear off current clock info */
126 clkdiv &= ~S3C2412_CLKDIVN_ARMDIVN;
127 clkdiv &= ~S3C2412_CLKDIVN_HDIVN_MASK;
128 clkdiv &= ~S3C2412_CLKDIVN_PDIVN;
130 if (cfg->divs.arm_divisor == 2)
131 clkdiv |= S3C2412_CLKDIVN_ARMDIVN;
133 clkdiv |= ((cfg->divs.h_divisor / cfg->divs.arm_divisor) - 1);
135 if (cfg->divs.p_divisor != cfg->divs.h_divisor)
136 clkdiv |= S3C2412_CLKDIVN_PDIVN;
138 s3c_freq_dbg("%s: div %08lx => %08lx\n", __func__, olddiv, clkdiv);
139 __raw_writel(clkdiv, S3C2410_CLKDIVN);
141 clk_set_parent(armclk, cfg->divs.dvs ? hclk : fclk);
144 static void s3c2412_cpufreq_setrefresh(struct s3c_cpufreq_config *cfg)
146 struct s3c_cpufreq_board *board = cfg->board;
147 unsigned long refresh;
149 s3c_freq_dbg("%s: refresh %u ns, hclk %lu\n", __func__,
150 board->refresh, cfg->freq.hclk);
152 /* Reduce both the refresh time (in ns) and the frequency (in MHz)
153 * by 10 each to ensure that we do not overflow 32 bit numbers. This
154 * should work for HCLK up to 133MHz and refresh period up to 30usec.
157 refresh = (board->refresh / 10);
158 refresh *= (cfg->freq.hclk / 100);
159 refresh /= (1 * 1000 * 1000); /* 10^6 */
161 s3c_freq_dbg("%s: setting refresh 0x%08lx\n", __func__, refresh);
162 __raw_writel(refresh, S3C2412_REFRESH);
165 /* set the default cpu frequency information, based on an 200MHz part
166 * as we have no other way of detecting the speed rating in software.
169 static struct s3c_cpufreq_info s3c2412_cpufreq_info = {
176 .latency = 5000000, /* 5ms */
183 .set_refresh = s3c2412_cpufreq_setrefresh,
184 .set_divs = s3c2412_cpufreq_setdivs,
185 .calc_divs = s3c2412_cpufreq_calcdivs,
187 .calc_iotiming = s3c2412_iotiming_calc,
188 .set_iotiming = s3c2412_iotiming_set,
189 .get_iotiming = s3c2412_iotiming_get,
191 .resume_clocks = s3c2412_setup_clocks,
193 .debug_io_show = s3c_cpufreq_debugfs_call(s3c2412_iotiming_debugfs),
196 static int s3c2412_cpufreq_add(struct device *dev,
197 struct subsys_interface *sif)
199 unsigned long fclk_rate;
201 hclk = clk_get(NULL, "hclk");
203 printk(KERN_ERR "%s: cannot find hclk clock\n", __func__);
207 fclk = clk_get(NULL, "fclk");
209 printk(KERN_ERR "%s: cannot find fclk clock\n", __func__);
213 fclk_rate = clk_get_rate(fclk);
214 if (fclk_rate > 200000000) {
216 "%s: fclk %ld MHz, assuming 266MHz capable part\n",
217 __func__, fclk_rate / 1000000);
218 s3c2412_cpufreq_info.max.fclk = 266000000;
219 s3c2412_cpufreq_info.max.hclk = 133000000;
220 s3c2412_cpufreq_info.max.pclk = 66000000;
223 armclk = clk_get(NULL, "armclk");
224 if (IS_ERR(armclk)) {
225 printk(KERN_ERR "%s: cannot find arm clock\n", __func__);
229 xtal = clk_get(NULL, "xtal");
231 printk(KERN_ERR "%s: cannot find xtal clock\n", __func__);
235 return s3c_cpufreq_register(&s3c2412_cpufreq_info);
247 static struct subsys_interface s3c2412_cpufreq_interface = {
248 .name = "s3c2412_cpufreq",
249 .subsys = &s3c2412_subsys,
250 .add_dev = s3c2412_cpufreq_add,
253 static int s3c2412_cpufreq_init(void)
255 return subsys_interface_register(&s3c2412_cpufreq_interface);
257 arch_initcall(s3c2412_cpufreq_init);