1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2008 Simtec Electronics
4 * http://armlinux.simtec.co.uk/
5 * Ben Dooks <ben@simtec.co.uk>
7 * S3C2412 CPU Frequency scalling
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/ioport.h>
16 #include <linux/cpufreq.h>
17 #include <linux/device.h>
18 #include <linux/delay.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
22 #include <linux/soc/samsung/s3c-cpufreq-core.h>
23 #include <linux/soc/samsung/s3c-pm.h>
25 #include <asm/mach/arch.h>
26 #include <asm/mach/map.h>
28 #define S3C2412_CLKDIVN_PDIVN (1<<2)
29 #define S3C2412_CLKDIVN_HDIVN_MASK (3<<0)
30 #define S3C2412_CLKDIVN_ARMDIVN (1<<3)
31 #define S3C2412_CLKDIVN_DVSEN (1<<4)
32 #define S3C2412_CLKDIVN_HALFHCLK (1<<5)
33 #define S3C2412_CLKDIVN_USB48DIV (1<<6)
34 #define S3C2412_CLKDIVN_UARTDIV_MASK (15<<8)
35 #define S3C2412_CLKDIVN_UARTDIV_SHIFT (8)
36 #define S3C2412_CLKDIVN_I2SDIV_MASK (15<<12)
37 #define S3C2412_CLKDIVN_I2SDIV_SHIFT (12)
38 #define S3C2412_CLKDIVN_CAMDIV_MASK (15<<16)
39 #define S3C2412_CLKDIVN_CAMDIV_SHIFT (16)
41 /* our clock resources. */
42 static struct clk
*xtal
;
43 static struct clk
*fclk
;
44 static struct clk
*hclk
;
45 static struct clk
*armclk
;
47 /* HDIV: 1, 2, 3, 4, 6, 8 */
49 static int s3c2412_cpufreq_calcdivs(struct s3c_cpufreq_config
*cfg
)
51 unsigned int hdiv
, pdiv
, armdiv
, dvs
;
52 unsigned long hclk
, fclk
, armclk
, armdiv_clk
;
53 unsigned long hclk_max
;
55 fclk
= cfg
->freq
.fclk
;
56 armclk
= cfg
->freq
.armclk
;
57 hclk_max
= cfg
->max
.hclk
;
59 /* We can't run hclk above armclk as at the best we have to
60 * have armclk and hclk in dvs mode. */
62 if (hclk_max
> armclk
)
65 s3c_freq_dbg("%s: fclk=%lu, armclk=%lu, hclk_max=%lu\n",
66 __func__
, fclk
, armclk
, hclk_max
);
67 s3c_freq_dbg("%s: want f=%lu, arm=%lu, h=%lu, p=%lu\n",
68 __func__
, cfg
->freq
.fclk
, cfg
->freq
.armclk
,
69 cfg
->freq
.hclk
, cfg
->freq
.pclk
);
71 armdiv
= fclk
/ armclk
;
78 cfg
->divs
.arm_divisor
= armdiv
;
79 armdiv_clk
= fclk
/ armdiv
;
81 hdiv
= armdiv_clk
/ hclk_max
;
85 cfg
->freq
.hclk
= hclk
= armdiv_clk
/ hdiv
;
87 /* set dvs depending on whether we reached armclk or not. */
88 cfg
->divs
.dvs
= dvs
= armclk
< armdiv_clk
;
90 /* update the actual armclk we achieved. */
91 cfg
->freq
.armclk
= dvs
? hclk
: armdiv_clk
;
93 s3c_freq_dbg("%s: armclk %lu, hclk %lu, armdiv %d, hdiv %d, dvs %d\n",
94 __func__
, armclk
, hclk
, armdiv
, hdiv
, cfg
->divs
.dvs
);
99 pdiv
= (hclk
> cfg
->max
.pclk
) ? 2 : 1;
101 if ((hclk
/ pdiv
) > cfg
->max
.pclk
)
104 cfg
->freq
.pclk
= hclk
/ pdiv
;
106 s3c_freq_dbg("%s: pdiv %d\n", __func__
, pdiv
);
113 /* store the result, and then return */
115 cfg
->divs
.h_divisor
= hdiv
* armdiv
;
116 cfg
->divs
.p_divisor
= pdiv
* armdiv
;
124 static void s3c2412_cpufreq_setdivs(struct s3c_cpufreq_config
*cfg
)
126 unsigned long clkdiv
;
127 unsigned long olddiv
;
129 olddiv
= clkdiv
= s3c24xx_read_clkdivn();
131 /* clear off current clock info */
133 clkdiv
&= ~S3C2412_CLKDIVN_ARMDIVN
;
134 clkdiv
&= ~S3C2412_CLKDIVN_HDIVN_MASK
;
135 clkdiv
&= ~S3C2412_CLKDIVN_PDIVN
;
137 if (cfg
->divs
.arm_divisor
== 2)
138 clkdiv
|= S3C2412_CLKDIVN_ARMDIVN
;
140 clkdiv
|= ((cfg
->divs
.h_divisor
/ cfg
->divs
.arm_divisor
) - 1);
142 if (cfg
->divs
.p_divisor
!= cfg
->divs
.h_divisor
)
143 clkdiv
|= S3C2412_CLKDIVN_PDIVN
;
145 s3c_freq_dbg("%s: div %08lx => %08lx\n", __func__
, olddiv
, clkdiv
);
146 s3c24xx_write_clkdivn(clkdiv
);
148 clk_set_parent(armclk
, cfg
->divs
.dvs
? hclk
: fclk
);
151 /* set the default cpu frequency information, based on an 200MHz part
152 * as we have no other way of detecting the speed rating in software.
155 static struct s3c_cpufreq_info s3c2412_cpufreq_info
= {
162 .latency
= 5000000, /* 5ms */
169 .set_refresh
= s3c2412_cpufreq_setrefresh
,
170 .set_divs
= s3c2412_cpufreq_setdivs
,
171 .calc_divs
= s3c2412_cpufreq_calcdivs
,
173 .calc_iotiming
= s3c2412_iotiming_calc
,
174 .set_iotiming
= s3c2412_iotiming_set
,
175 .get_iotiming
= s3c2412_iotiming_get
,
177 .debug_io_show
= s3c_cpufreq_debugfs_call(s3c2412_iotiming_debugfs
),
180 static int s3c2412_cpufreq_add(struct device
*dev
,
181 struct subsys_interface
*sif
)
183 unsigned long fclk_rate
;
185 hclk
= clk_get(NULL
, "hclk");
187 pr_err("cannot find hclk clock\n");
191 fclk
= clk_get(NULL
, "fclk");
193 pr_err("cannot find fclk clock\n");
197 fclk_rate
= clk_get_rate(fclk
);
198 if (fclk_rate
> 200000000) {
199 pr_info("fclk %ld MHz, assuming 266MHz capable part\n",
200 fclk_rate
/ 1000000);
201 s3c2412_cpufreq_info
.max
.fclk
= 266000000;
202 s3c2412_cpufreq_info
.max
.hclk
= 133000000;
203 s3c2412_cpufreq_info
.max
.pclk
= 66000000;
206 armclk
= clk_get(NULL
, "armclk");
207 if (IS_ERR(armclk
)) {
208 pr_err("cannot find arm clock\n");
212 xtal
= clk_get(NULL
, "xtal");
214 pr_err("cannot find xtal clock\n");
218 return s3c_cpufreq_register(&s3c2412_cpufreq_info
);
230 static struct subsys_interface s3c2412_cpufreq_interface
= {
231 .name
= "s3c2412_cpufreq",
232 .subsys
= &s3c2412_subsys
,
233 .add_dev
= s3c2412_cpufreq_add
,
236 static int s3c2412_cpufreq_init(void)
238 return subsys_interface_register(&s3c2412_cpufreq_interface
);
240 arch_initcall(s3c2412_cpufreq_init
);