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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/ioport.h>
20 #include <linux/cpufreq.h>
21 #include <linux/device.h>
22 #include <linux/delay.h>
23 #include <linux/clk.h>
24 #include <linux/err.h>
27 #include <asm/mach/arch.h>
28 #include <asm/mach/map.h>
30 #include <mach/regs-clock.h>
33 #include <plat/cpu-freq-core.h>
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 inline int within_khz(unsigned long a
, unsigned long b
)
46 return (diff
>= -1000 && diff
<= 1000);
50 * s3c2440_cpufreq_calcdivs - calculate divider settings
51 * @cfg: The cpu frequency settings.
53 * Calcualte the divider values for the given frequency settings
54 * specified in @cfg. The values are stored in @cfg for later use
55 * by the relevant set routine if the request settings can be reached.
57 static int s3c2440_cpufreq_calcdivs(struct s3c_cpufreq_config
*cfg
)
59 unsigned int hdiv
, pdiv
;
60 unsigned long hclk
, fclk
, armclk
;
61 unsigned long hclk_max
;
63 fclk
= cfg
->freq
.fclk
;
64 armclk
= cfg
->freq
.armclk
;
65 hclk_max
= cfg
->max
.hclk
;
67 s3c_freq_dbg("%s: fclk is %lu, armclk %lu, max hclk %lu\n",
68 __func__
, fclk
, armclk
, hclk_max
);
71 pr_warn("%s: armclk > fclk\n", __func__
);
75 /* if we are in DVS, we need HCLK to be <= ARMCLK */
76 if (armclk
< fclk
&& armclk
< hclk_max
)
79 for (hdiv
= 1; hdiv
< 9; hdiv
++) {
80 if (hdiv
== 5 || hdiv
== 7)
84 if (hclk
<= hclk_max
|| within_khz(hclk
, hclk_max
))
88 s3c_freq_dbg("%s: hclk %lu, div %d\n", __func__
, hclk
, hdiv
);
93 pdiv
= (hclk
> cfg
->max
.pclk
) ? 2 : 1;
95 if ((hclk
/ pdiv
) > cfg
->max
.pclk
)
98 s3c_freq_dbg("%s: pdiv %d\n", __func__
, pdiv
);
105 /* calculate a valid armclk */
110 /* if we're running armclk lower than fclk, this really means
111 * that the system should go into dvs mode, which means that
112 * armclk is connected to hclk. */
119 cfg
->freq
.armclk
= armclk
;
121 /* store the result, and then return */
123 cfg
->divs
.h_divisor
= hdiv
;
124 cfg
->divs
.p_divisor
= pdiv
;
132 #define CAMDIVN_HCLK_HALF (S3C2440_CAMDIVN_HCLK3_HALF | \
133 S3C2440_CAMDIVN_HCLK4_HALF)
136 * s3c2440_cpufreq_setdivs - set the cpu frequency divider settings
137 * @cfg: The cpu frequency settings.
139 * Set the divisors from the settings in @cfg, which where generated
140 * during the calculation phase by s3c2440_cpufreq_calcdivs().
142 static void s3c2440_cpufreq_setdivs(struct s3c_cpufreq_config
*cfg
)
144 unsigned long clkdiv
, camdiv
;
146 s3c_freq_dbg("%s: divisors: h=%d, p=%d\n", __func__
,
147 cfg
->divs
.h_divisor
, cfg
->divs
.p_divisor
);
149 clkdiv
= __raw_readl(S3C2410_CLKDIVN
);
150 camdiv
= __raw_readl(S3C2440_CAMDIVN
);
152 clkdiv
&= ~(S3C2440_CLKDIVN_HDIVN_MASK
| S3C2440_CLKDIVN_PDIVN
);
153 camdiv
&= ~CAMDIVN_HCLK_HALF
;
155 switch (cfg
->divs
.h_divisor
) {
157 clkdiv
|= S3C2440_CLKDIVN_HDIVN_1
;
161 clkdiv
|= S3C2440_CLKDIVN_HDIVN_2
;
165 camdiv
|= S3C2440_CAMDIVN_HCLK3_HALF
;
167 clkdiv
|= S3C2440_CLKDIVN_HDIVN_3_6
;
171 camdiv
|= S3C2440_CAMDIVN_HCLK4_HALF
;
173 clkdiv
|= S3C2440_CLKDIVN_HDIVN_4_8
;
177 BUG(); /* we don't expect to get here. */
180 if (cfg
->divs
.p_divisor
!= cfg
->divs
.h_divisor
)
181 clkdiv
|= S3C2440_CLKDIVN_PDIVN
;
183 /* todo - set pclk. */
185 /* Write the divisors first with hclk intentionally halved so that
186 * when we write clkdiv we will under-frequency instead of over. We
187 * then make a short delay and remove the hclk halving if necessary.
190 __raw_writel(camdiv
| CAMDIVN_HCLK_HALF
, S3C2440_CAMDIVN
);
191 __raw_writel(clkdiv
, S3C2410_CLKDIVN
);
194 __raw_writel(camdiv
, S3C2440_CAMDIVN
);
196 clk_set_parent(armclk
, cfg
->divs
.dvs
? hclk
: fclk
);
199 static int run_freq_for(unsigned long max_hclk
, unsigned long fclk
,
201 struct cpufreq_frequency_table
*table
,
208 for (div
= *divs
; div
> 0; div
= *divs
++) {
211 if (freq
> max_hclk
&& div
!= 1)
214 freq
/= 1000; /* table is in kHz */
215 index
= s3c_cpufreq_addfreq(table
, index
, table_size
, freq
);
223 static int hclk_divs
[] = { 1, 2, 3, 4, 6, 8, -1 };
225 static int s3c2440_cpufreq_calctable(struct s3c_cpufreq_config
*cfg
,
226 struct cpufreq_frequency_table
*table
,
231 WARN_ON(cfg
->info
== NULL
);
232 WARN_ON(cfg
->board
== NULL
);
234 ret
= run_freq_for(cfg
->info
->max
.hclk
,
239 s3c_freq_dbg("%s: returning %d\n", __func__
, ret
);
244 static struct s3c_cpufreq_info s3c2440_cpufreq_info
= {
256 .calc_iotiming
= s3c2410_iotiming_calc
,
257 .set_iotiming
= s3c2410_iotiming_set
,
258 .get_iotiming
= s3c2410_iotiming_get
,
259 .set_fvco
= s3c2410_set_fvco
,
261 .set_refresh
= s3c2410_cpufreq_setrefresh
,
262 .set_divs
= s3c2440_cpufreq_setdivs
,
263 .calc_divs
= s3c2440_cpufreq_calcdivs
,
264 .calc_freqtable
= s3c2440_cpufreq_calctable
,
266 .debug_io_show
= s3c_cpufreq_debugfs_call(s3c2410_iotiming_debugfs
),
269 static int s3c2440_cpufreq_add(struct device
*dev
,
270 struct subsys_interface
*sif
)
272 xtal
= s3c_cpufreq_clk_get(NULL
, "xtal");
273 hclk
= s3c_cpufreq_clk_get(NULL
, "hclk");
274 fclk
= s3c_cpufreq_clk_get(NULL
, "fclk");
275 armclk
= s3c_cpufreq_clk_get(NULL
, "armclk");
277 if (IS_ERR(xtal
) || IS_ERR(hclk
) || IS_ERR(fclk
) || IS_ERR(armclk
)) {
278 pr_err("%s: failed to get clocks\n", __func__
);
282 return s3c_cpufreq_register(&s3c2440_cpufreq_info
);
285 static struct subsys_interface s3c2440_cpufreq_interface
= {
286 .name
= "s3c2440_cpufreq",
287 .subsys
= &s3c2440_subsys
,
288 .add_dev
= s3c2440_cpufreq_add
,
291 static int s3c2440_cpufreq_init(void)
293 return subsys_interface_register(&s3c2440_cpufreq_interface
);
296 /* arch_initcall adds the clocks we need, so use subsys_initcall. */
297 subsys_initcall(s3c2440_cpufreq_init
);
299 static struct subsys_interface s3c2442_cpufreq_interface
= {
300 .name
= "s3c2442_cpufreq",
301 .subsys
= &s3c2442_subsys
,
302 .add_dev
= s3c2440_cpufreq_add
,
305 static int s3c2442_cpufreq_init(void)
307 return subsys_interface_register(&s3c2442_cpufreq_interface
);
309 subsys_initcall(s3c2442_cpufreq_init
);