1 // SPDX-License-Identifier: GPL-2.0-only
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
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
30 #include <plat/cpu-freq-core.h>
32 static struct clk
*xtal
;
33 static struct clk
*fclk
;
34 static struct clk
*hclk
;
35 static struct clk
*armclk
;
37 /* HDIV: 1, 2, 3, 4, 6, 8 */
39 static inline int within_khz(unsigned long a
, unsigned long b
)
43 return (diff
>= -1000 && diff
<= 1000);
47 * s3c2440_cpufreq_calcdivs - calculate divider settings
48 * @cfg: The cpu frequency settings.
50 * Calcualte the divider values for the given frequency settings
51 * specified in @cfg. The values are stored in @cfg for later use
52 * by the relevant set routine if the request settings can be reached.
54 static int s3c2440_cpufreq_calcdivs(struct s3c_cpufreq_config
*cfg
)
56 unsigned int hdiv
, pdiv
;
57 unsigned long hclk
, fclk
, armclk
;
58 unsigned long hclk_max
;
60 fclk
= cfg
->freq
.fclk
;
61 armclk
= cfg
->freq
.armclk
;
62 hclk_max
= cfg
->max
.hclk
;
64 s3c_freq_dbg("%s: fclk is %lu, armclk %lu, max hclk %lu\n",
65 __func__
, fclk
, armclk
, hclk_max
);
68 pr_warn("%s: armclk > fclk\n", __func__
);
72 /* if we are in DVS, we need HCLK to be <= ARMCLK */
73 if (armclk
< fclk
&& armclk
< hclk_max
)
76 for (hdiv
= 1; hdiv
< 9; hdiv
++) {
77 if (hdiv
== 5 || hdiv
== 7)
81 if (hclk
<= hclk_max
|| within_khz(hclk
, hclk_max
))
85 s3c_freq_dbg("%s: hclk %lu, div %d\n", __func__
, hclk
, hdiv
);
90 pdiv
= (hclk
> cfg
->max
.pclk
) ? 2 : 1;
92 if ((hclk
/ pdiv
) > cfg
->max
.pclk
)
95 s3c_freq_dbg("%s: pdiv %d\n", __func__
, pdiv
);
102 /* calculate a valid armclk */
107 /* if we're running armclk lower than fclk, this really means
108 * that the system should go into dvs mode, which means that
109 * armclk is connected to hclk. */
116 cfg
->freq
.armclk
= armclk
;
118 /* store the result, and then return */
120 cfg
->divs
.h_divisor
= hdiv
;
121 cfg
->divs
.p_divisor
= pdiv
;
129 #define CAMDIVN_HCLK_HALF (S3C2440_CAMDIVN_HCLK3_HALF | \
130 S3C2440_CAMDIVN_HCLK4_HALF)
133 * s3c2440_cpufreq_setdivs - set the cpu frequency divider settings
134 * @cfg: The cpu frequency settings.
136 * Set the divisors from the settings in @cfg, which where generated
137 * during the calculation phase by s3c2440_cpufreq_calcdivs().
139 static void s3c2440_cpufreq_setdivs(struct s3c_cpufreq_config
*cfg
)
141 unsigned long clkdiv
, camdiv
;
143 s3c_freq_dbg("%s: divisors: h=%d, p=%d\n", __func__
,
144 cfg
->divs
.h_divisor
, cfg
->divs
.p_divisor
);
146 clkdiv
= __raw_readl(S3C2410_CLKDIVN
);
147 camdiv
= __raw_readl(S3C2440_CAMDIVN
);
149 clkdiv
&= ~(S3C2440_CLKDIVN_HDIVN_MASK
| S3C2440_CLKDIVN_PDIVN
);
150 camdiv
&= ~CAMDIVN_HCLK_HALF
;
152 switch (cfg
->divs
.h_divisor
) {
154 clkdiv
|= S3C2440_CLKDIVN_HDIVN_1
;
158 clkdiv
|= S3C2440_CLKDIVN_HDIVN_2
;
162 camdiv
|= S3C2440_CAMDIVN_HCLK3_HALF
;
164 clkdiv
|= S3C2440_CLKDIVN_HDIVN_3_6
;
168 camdiv
|= S3C2440_CAMDIVN_HCLK4_HALF
;
170 clkdiv
|= S3C2440_CLKDIVN_HDIVN_4_8
;
174 BUG(); /* we don't expect to get here. */
177 if (cfg
->divs
.p_divisor
!= cfg
->divs
.h_divisor
)
178 clkdiv
|= S3C2440_CLKDIVN_PDIVN
;
180 /* todo - set pclk. */
182 /* Write the divisors first with hclk intentionally halved so that
183 * when we write clkdiv we will under-frequency instead of over. We
184 * then make a short delay and remove the hclk halving if necessary.
187 __raw_writel(camdiv
| CAMDIVN_HCLK_HALF
, S3C2440_CAMDIVN
);
188 __raw_writel(clkdiv
, S3C2410_CLKDIVN
);
191 __raw_writel(camdiv
, S3C2440_CAMDIVN
);
193 clk_set_parent(armclk
, cfg
->divs
.dvs
? hclk
: fclk
);
196 static int run_freq_for(unsigned long max_hclk
, unsigned long fclk
,
198 struct cpufreq_frequency_table
*table
,
205 for (div
= *divs
; div
> 0; div
= *divs
++) {
208 if (freq
> max_hclk
&& div
!= 1)
211 freq
/= 1000; /* table is in kHz */
212 index
= s3c_cpufreq_addfreq(table
, index
, table_size
, freq
);
220 static int hclk_divs
[] = { 1, 2, 3, 4, 6, 8, -1 };
222 static int s3c2440_cpufreq_calctable(struct s3c_cpufreq_config
*cfg
,
223 struct cpufreq_frequency_table
*table
,
228 WARN_ON(cfg
->info
== NULL
);
229 WARN_ON(cfg
->board
== NULL
);
231 ret
= run_freq_for(cfg
->info
->max
.hclk
,
236 s3c_freq_dbg("%s: returning %d\n", __func__
, ret
);
241 static struct s3c_cpufreq_info s3c2440_cpufreq_info
= {
253 .calc_iotiming
= s3c2410_iotiming_calc
,
254 .set_iotiming
= s3c2410_iotiming_set
,
255 .get_iotiming
= s3c2410_iotiming_get
,
256 .set_fvco
= s3c2410_set_fvco
,
258 .set_refresh
= s3c2410_cpufreq_setrefresh
,
259 .set_divs
= s3c2440_cpufreq_setdivs
,
260 .calc_divs
= s3c2440_cpufreq_calcdivs
,
261 .calc_freqtable
= s3c2440_cpufreq_calctable
,
263 .debug_io_show
= s3c_cpufreq_debugfs_call(s3c2410_iotiming_debugfs
),
266 static int s3c2440_cpufreq_add(struct device
*dev
,
267 struct subsys_interface
*sif
)
269 xtal
= s3c_cpufreq_clk_get(NULL
, "xtal");
270 hclk
= s3c_cpufreq_clk_get(NULL
, "hclk");
271 fclk
= s3c_cpufreq_clk_get(NULL
, "fclk");
272 armclk
= s3c_cpufreq_clk_get(NULL
, "armclk");
274 if (IS_ERR(xtal
) || IS_ERR(hclk
) || IS_ERR(fclk
) || IS_ERR(armclk
)) {
275 pr_err("%s: failed to get clocks\n", __func__
);
279 return s3c_cpufreq_register(&s3c2440_cpufreq_info
);
282 static struct subsys_interface s3c2440_cpufreq_interface
= {
283 .name
= "s3c2440_cpufreq",
284 .subsys
= &s3c2440_subsys
,
285 .add_dev
= s3c2440_cpufreq_add
,
288 static int s3c2440_cpufreq_init(void)
290 return subsys_interface_register(&s3c2440_cpufreq_interface
);
293 /* arch_initcall adds the clocks we need, so use subsys_initcall. */
294 subsys_initcall(s3c2440_cpufreq_init
);
296 static struct subsys_interface s3c2442_cpufreq_interface
= {
297 .name
= "s3c2442_cpufreq",
298 .subsys
= &s3c2442_subsys
,
299 .add_dev
= s3c2440_cpufreq_add
,
302 static int s3c2442_cpufreq_init(void)
304 return subsys_interface_register(&s3c2442_cpufreq_interface
);
306 subsys_initcall(s3c2442_cpufreq_init
);