1 /* linux/arch/arm/plat-s3c24xx/s3c2440-cpufreq.c
3 * Copyright (c) 2006-2009 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
6 * Vincent Sanders <vince@simtec.co.uk>
8 * S3C2440/S3C2442 CPU Frequency scaling
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/ioport.h>
19 #include <linux/cpufreq.h>
20 #include <linux/sysdev.h>
21 #include <linux/delay.h>
22 #include <linux/clk.h>
23 #include <linux/err.h>
26 #include <mach/hardware.h>
28 #include <asm/mach/arch.h>
29 #include <asm/mach/map.h>
31 #include <mach/regs-clock.h>
34 #include <plat/cpu-freq-core.h>
35 #include <plat/clock.h>
37 static struct clk
*xtal
;
38 static struct clk
*fclk
;
39 static struct clk
*hclk
;
40 static struct clk
*armclk
;
42 /* HDIV: 1, 2, 3, 4, 6, 8 */
44 static inline int within_khz(unsigned long a
, unsigned long b
)
48 return (diff
>= -1000 && diff
<= 1000);
52 * s3c2440_cpufreq_calcdivs - calculate divider settings
53 * @cfg: The cpu frequency settings.
55 * Calcualte the divider values for the given frequency settings
56 * specified in @cfg. The values are stored in @cfg for later use
57 * by the relevant set routine if the request settings can be reached.
59 int s3c2440_cpufreq_calcdivs(struct s3c_cpufreq_config
*cfg
)
61 unsigned int hdiv
, pdiv
;
62 unsigned long hclk
, fclk
, armclk
;
63 unsigned long hclk_max
;
65 fclk
= cfg
->freq
.fclk
;
66 armclk
= cfg
->freq
.armclk
;
67 hclk_max
= cfg
->max
.hclk
;
69 s3c_freq_dbg("%s: fclk is %lu, armclk %lu, max hclk %lu\n",
70 __func__
, fclk
, armclk
, hclk_max
);
73 printk(KERN_WARNING
"%s: armclk > fclk\n", __func__
);
77 /* if we are in DVS, we need HCLK to be <= ARMCLK */
78 if (armclk
< fclk
&& armclk
< hclk_max
)
81 for (hdiv
= 1; hdiv
< 9; hdiv
++) {
82 if (hdiv
== 5 || hdiv
== 7)
86 if (hclk
<= hclk_max
|| within_khz(hclk
, hclk_max
))
90 s3c_freq_dbg("%s: hclk %lu, div %d\n", __func__
, hclk
, hdiv
);
95 pdiv
= (hclk
> cfg
->max
.pclk
) ? 2 : 1;
97 if ((hclk
/ pdiv
) > cfg
->max
.pclk
)
100 s3c_freq_dbg("%s: pdiv %d\n", __func__
, pdiv
);
107 /* calculate a valid armclk */
112 /* if we're running armclk lower than fclk, this really means
113 * that the system should go into dvs mode, which means that
114 * armclk is connected to hclk. */
121 cfg
->freq
.armclk
= armclk
;
123 /* store the result, and then return */
125 cfg
->divs
.h_divisor
= hdiv
;
126 cfg
->divs
.p_divisor
= pdiv
;
134 #define CAMDIVN_HCLK_HALF (S3C2440_CAMDIVN_HCLK3_HALF | \
135 S3C2440_CAMDIVN_HCLK4_HALF)
138 * s3c2440_cpufreq_setdivs - set the cpu frequency divider settings
139 * @cfg: The cpu frequency settings.
141 * Set the divisors from the settings in @cfg, which where generated
142 * during the calculation phase by s3c2440_cpufreq_calcdivs().
144 static void s3c2440_cpufreq_setdivs(struct s3c_cpufreq_config
*cfg
)
146 unsigned long clkdiv
, camdiv
;
148 s3c_freq_dbg("%s: divsiors: h=%d, p=%d\n", __func__
,
149 cfg
->divs
.h_divisor
, cfg
->divs
.p_divisor
);
151 clkdiv
= __raw_readl(S3C2410_CLKDIVN
);
152 camdiv
= __raw_readl(S3C2440_CAMDIVN
);
154 clkdiv
&= ~(S3C2440_CLKDIVN_HDIVN_MASK
| S3C2440_CLKDIVN_PDIVN
);
155 camdiv
&= ~CAMDIVN_HCLK_HALF
;
157 switch (cfg
->divs
.h_divisor
) {
159 clkdiv
|= S3C2440_CLKDIVN_HDIVN_1
;
163 clkdiv
|= S3C2440_CLKDIVN_HDIVN_2
;
167 camdiv
|= S3C2440_CAMDIVN_HCLK3_HALF
;
169 clkdiv
|= S3C2440_CLKDIVN_HDIVN_3_6
;
173 camdiv
|= S3C2440_CAMDIVN_HCLK4_HALF
;
175 clkdiv
|= S3C2440_CLKDIVN_HDIVN_4_8
;
179 BUG(); /* we don't expect to get here. */
182 if (cfg
->divs
.p_divisor
!= cfg
->divs
.h_divisor
)
183 clkdiv
|= S3C2440_CLKDIVN_PDIVN
;
185 /* todo - set pclk. */
187 /* Write the divisors first with hclk intentionally halved so that
188 * when we write clkdiv we will under-frequency instead of over. We
189 * then make a short delay and remove the hclk halving if necessary.
192 __raw_writel(camdiv
| CAMDIVN_HCLK_HALF
, S3C2440_CAMDIVN
);
193 __raw_writel(clkdiv
, S3C2410_CLKDIVN
);
196 __raw_writel(camdiv
, S3C2440_CAMDIVN
);
198 clk_set_parent(armclk
, cfg
->divs
.dvs
? hclk
: fclk
);
201 static int run_freq_for(unsigned long max_hclk
, unsigned long fclk
,
203 struct cpufreq_frequency_table
*table
,
210 for (div
= *divs
; div
> 0; div
= *divs
++) {
213 if (freq
> max_hclk
&& div
!= 1)
216 freq
/= 1000; /* table is in kHz */
217 index
= s3c_cpufreq_addfreq(table
, index
, table_size
, freq
);
225 static int hclk_divs
[] = { 1, 2, 3, 4, 6, 8, -1 };
227 static int s3c2440_cpufreq_calctable(struct s3c_cpufreq_config
*cfg
,
228 struct cpufreq_frequency_table
*table
,
233 WARN_ON(cfg
->info
== NULL
);
234 WARN_ON(cfg
->board
== NULL
);
236 ret
= run_freq_for(cfg
->info
->max
.hclk
,
241 s3c_freq_dbg("%s: returning %d\n", __func__
, ret
);
246 struct s3c_cpufreq_info s3c2440_cpufreq_info
= {
258 .calc_iotiming
= s3c2410_iotiming_calc
,
259 .set_iotiming
= s3c2410_iotiming_set
,
260 .get_iotiming
= s3c2410_iotiming_get
,
261 .set_fvco
= s3c2410_set_fvco
,
263 .set_refresh
= s3c2410_cpufreq_setrefresh
,
264 .set_divs
= s3c2440_cpufreq_setdivs
,
265 .calc_divs
= s3c2440_cpufreq_calcdivs
,
266 .calc_freqtable
= s3c2440_cpufreq_calctable
,
268 .resume_clocks
= s3c244x_setup_clocks
,
270 .debug_io_show
= s3c_cpufreq_debugfs_call(s3c2410_iotiming_debugfs
),
273 static int s3c2440_cpufreq_add(struct sys_device
*sysdev
)
275 xtal
= s3c_cpufreq_clk_get(NULL
, "xtal");
276 hclk
= s3c_cpufreq_clk_get(NULL
, "hclk");
277 fclk
= s3c_cpufreq_clk_get(NULL
, "fclk");
278 armclk
= s3c_cpufreq_clk_get(NULL
, "armclk");
280 if (IS_ERR(xtal
) || IS_ERR(hclk
) || IS_ERR(fclk
) || IS_ERR(armclk
)) {
281 printk(KERN_ERR
"%s: failed to get clocks\n", __func__
);
285 return s3c_cpufreq_register(&s3c2440_cpufreq_info
);
288 static struct sysdev_driver s3c2440_cpufreq_driver
= {
289 .add
= s3c2440_cpufreq_add
,
292 static int s3c2440_cpufreq_init(void)
294 return sysdev_driver_register(&s3c2440_sysclass
,
295 &s3c2440_cpufreq_driver
);
298 /* arch_initcall adds the clocks we need, so use subsys_initcall. */
299 subsys_initcall(s3c2440_cpufreq_init
);
301 static struct sysdev_driver s3c2442_cpufreq_driver
= {
302 .add
= s3c2440_cpufreq_add
,
305 static int s3c2442_cpufreq_init(void)
307 return sysdev_driver_register(&s3c2442_sysclass
,
308 &s3c2442_cpufreq_driver
);
311 subsys_initcall(s3c2442_cpufreq_init
);