2 * Copyright (c) 2006-2009 Simtec Electronics
3 * http://armlinux.simtec.co.uk/
4 * Ben Dooks <ben@simtec.co.uk>
5 * Vincent Sanders <vince@simtec.co.uk>
7 * S3C2440/S3C2442 CPU Frequency scaling
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/ioport.h>
18 #include <linux/cpufreq.h>
19 #include <linux/device.h>
20 #include <linux/delay.h>
21 #include <linux/clk.h>
22 #include <linux/err.h>
25 #include <asm/mach/arch.h>
26 #include <asm/mach/map.h>
28 #include <mach/regs-clock.h>
31 #include <plat/cpu-freq-core.h>
33 static struct clk
*xtal
;
34 static struct clk
*fclk
;
35 static struct clk
*hclk
;
36 static struct clk
*armclk
;
38 /* HDIV: 1, 2, 3, 4, 6, 8 */
40 static inline int within_khz(unsigned long a
, unsigned long b
)
44 return (diff
>= -1000 && diff
<= 1000);
48 * s3c2440_cpufreq_calcdivs - calculate divider settings
49 * @cfg: The cpu frequency settings.
51 * Calcualte the divider values for the given frequency settings
52 * specified in @cfg. The values are stored in @cfg for later use
53 * by the relevant set routine if the request settings can be reached.
55 static int s3c2440_cpufreq_calcdivs(struct s3c_cpufreq_config
*cfg
)
57 unsigned int hdiv
, pdiv
;
58 unsigned long hclk
, fclk
, armclk
;
59 unsigned long hclk_max
;
61 fclk
= cfg
->freq
.fclk
;
62 armclk
= cfg
->freq
.armclk
;
63 hclk_max
= cfg
->max
.hclk
;
65 s3c_freq_dbg("%s: fclk is %lu, armclk %lu, max hclk %lu\n",
66 __func__
, fclk
, armclk
, hclk_max
);
69 printk(KERN_WARNING
"%s: armclk > fclk\n", __func__
);
73 /* if we are in DVS, we need HCLK to be <= ARMCLK */
74 if (armclk
< fclk
&& armclk
< hclk_max
)
77 for (hdiv
= 1; hdiv
< 9; hdiv
++) {
78 if (hdiv
== 5 || hdiv
== 7)
82 if (hclk
<= hclk_max
|| within_khz(hclk
, hclk_max
))
86 s3c_freq_dbg("%s: hclk %lu, div %d\n", __func__
, hclk
, hdiv
);
91 pdiv
= (hclk
> cfg
->max
.pclk
) ? 2 : 1;
93 if ((hclk
/ pdiv
) > cfg
->max
.pclk
)
96 s3c_freq_dbg("%s: pdiv %d\n", __func__
, pdiv
);
103 /* calculate a valid armclk */
108 /* if we're running armclk lower than fclk, this really means
109 * that the system should go into dvs mode, which means that
110 * armclk is connected to hclk. */
117 cfg
->freq
.armclk
= armclk
;
119 /* store the result, and then return */
121 cfg
->divs
.h_divisor
= hdiv
;
122 cfg
->divs
.p_divisor
= pdiv
;
130 #define CAMDIVN_HCLK_HALF (S3C2440_CAMDIVN_HCLK3_HALF | \
131 S3C2440_CAMDIVN_HCLK4_HALF)
134 * s3c2440_cpufreq_setdivs - set the cpu frequency divider settings
135 * @cfg: The cpu frequency settings.
137 * Set the divisors from the settings in @cfg, which where generated
138 * during the calculation phase by s3c2440_cpufreq_calcdivs().
140 static void s3c2440_cpufreq_setdivs(struct s3c_cpufreq_config
*cfg
)
142 unsigned long clkdiv
, camdiv
;
144 s3c_freq_dbg("%s: divsiors: h=%d, p=%d\n", __func__
,
145 cfg
->divs
.h_divisor
, cfg
->divs
.p_divisor
);
147 clkdiv
= __raw_readl(S3C2410_CLKDIVN
);
148 camdiv
= __raw_readl(S3C2440_CAMDIVN
);
150 clkdiv
&= ~(S3C2440_CLKDIVN_HDIVN_MASK
| S3C2440_CLKDIVN_PDIVN
);
151 camdiv
&= ~CAMDIVN_HCLK_HALF
;
153 switch (cfg
->divs
.h_divisor
) {
155 clkdiv
|= S3C2440_CLKDIVN_HDIVN_1
;
159 clkdiv
|= S3C2440_CLKDIVN_HDIVN_2
;
163 camdiv
|= S3C2440_CAMDIVN_HCLK3_HALF
;
165 clkdiv
|= S3C2440_CLKDIVN_HDIVN_3_6
;
169 camdiv
|= S3C2440_CAMDIVN_HCLK4_HALF
;
171 clkdiv
|= S3C2440_CLKDIVN_HDIVN_4_8
;
175 BUG(); /* we don't expect to get here. */
178 if (cfg
->divs
.p_divisor
!= cfg
->divs
.h_divisor
)
179 clkdiv
|= S3C2440_CLKDIVN_PDIVN
;
181 /* todo - set pclk. */
183 /* Write the divisors first with hclk intentionally halved so that
184 * when we write clkdiv we will under-frequency instead of over. We
185 * then make a short delay and remove the hclk halving if necessary.
188 __raw_writel(camdiv
| CAMDIVN_HCLK_HALF
, S3C2440_CAMDIVN
);
189 __raw_writel(clkdiv
, S3C2410_CLKDIVN
);
192 __raw_writel(camdiv
, S3C2440_CAMDIVN
);
194 clk_set_parent(armclk
, cfg
->divs
.dvs
? hclk
: fclk
);
197 static int run_freq_for(unsigned long max_hclk
, unsigned long fclk
,
199 struct cpufreq_frequency_table
*table
,
206 for (div
= *divs
; div
> 0; div
= *divs
++) {
209 if (freq
> max_hclk
&& div
!= 1)
212 freq
/= 1000; /* table is in kHz */
213 index
= s3c_cpufreq_addfreq(table
, index
, table_size
, freq
);
221 static int hclk_divs
[] = { 1, 2, 3, 4, 6, 8, -1 };
223 static int s3c2440_cpufreq_calctable(struct s3c_cpufreq_config
*cfg
,
224 struct cpufreq_frequency_table
*table
,
229 WARN_ON(cfg
->info
== NULL
);
230 WARN_ON(cfg
->board
== NULL
);
232 ret
= run_freq_for(cfg
->info
->max
.hclk
,
237 s3c_freq_dbg("%s: returning %d\n", __func__
, ret
);
242 static struct s3c_cpufreq_info s3c2440_cpufreq_info
= {
254 .calc_iotiming
= s3c2410_iotiming_calc
,
255 .set_iotiming
= s3c2410_iotiming_set
,
256 .get_iotiming
= s3c2410_iotiming_get
,
257 .set_fvco
= s3c2410_set_fvco
,
259 .set_refresh
= s3c2410_cpufreq_setrefresh
,
260 .set_divs
= s3c2440_cpufreq_setdivs
,
261 .calc_divs
= s3c2440_cpufreq_calcdivs
,
262 .calc_freqtable
= s3c2440_cpufreq_calctable
,
264 .debug_io_show
= s3c_cpufreq_debugfs_call(s3c2410_iotiming_debugfs
),
267 static int s3c2440_cpufreq_add(struct device
*dev
,
268 struct subsys_interface
*sif
)
270 xtal
= s3c_cpufreq_clk_get(NULL
, "xtal");
271 hclk
= s3c_cpufreq_clk_get(NULL
, "hclk");
272 fclk
= s3c_cpufreq_clk_get(NULL
, "fclk");
273 armclk
= s3c_cpufreq_clk_get(NULL
, "armclk");
275 if (IS_ERR(xtal
) || IS_ERR(hclk
) || IS_ERR(fclk
) || IS_ERR(armclk
)) {
276 printk(KERN_ERR
"%s: failed to get clocks\n", __func__
);
280 return s3c_cpufreq_register(&s3c2440_cpufreq_info
);
283 static struct subsys_interface s3c2440_cpufreq_interface
= {
284 .name
= "s3c2440_cpufreq",
285 .subsys
= &s3c2440_subsys
,
286 .add_dev
= s3c2440_cpufreq_add
,
289 static int s3c2440_cpufreq_init(void)
291 return subsys_interface_register(&s3c2440_cpufreq_interface
);
294 /* arch_initcall adds the clocks we need, so use subsys_initcall. */
295 subsys_initcall(s3c2440_cpufreq_init
);
297 static struct subsys_interface s3c2442_cpufreq_interface
= {
298 .name
= "s3c2442_cpufreq",
299 .subsys
= &s3c2442_subsys
,
300 .add_dev
= s3c2440_cpufreq_add
,
303 static int s3c2442_cpufreq_init(void)
305 return subsys_interface_register(&s3c2442_cpufreq_interface
);
307 subsys_initcall(s3c2442_cpufreq_init
);