2 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
5 * CPU frequency scaling for S5PC110/S5PV210
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
18 #include <linux/cpufreq.h>
19 #include <linux/reboot.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/suspend.h>
24 #include <mach/regs-clock.h>
26 static struct clk
*dmc0_clk
;
27 static struct clk
*dmc1_clk
;
28 static DEFINE_MUTEX(set_freq_lock
);
30 /* APLL M,P,S values for 1G/800Mhz */
31 #define APLL_VAL_1000 ((1 << 31) | (125 << 16) | (3 << 8) | 1)
32 #define APLL_VAL_800 ((1 << 31) | (100 << 16) | (3 << 8) | 1)
34 /* Use 800MHz when entering sleep mode */
35 #define SLEEP_FREQ (800 * 1000)
37 /* Tracks if cpu freqency can be updated anymore */
38 static bool no_cpufreq_access
;
41 * DRAM configurations to calculate refresh counter for changing
42 * frequency of memory.
45 unsigned long freq
; /* HZ */
46 unsigned long refresh
; /* DRAM refresh counter * 1000 */
49 /* DRAM configuration (DMC0 and DMC1) */
50 static struct dram_conf s5pv210_dram_conf
[2];
56 enum s5pv210_mem_type
{
62 enum s5pv210_dmc_port
{
67 static struct cpufreq_frequency_table s5pv210_freq_table
[] = {
73 {0, CPUFREQ_TABLE_END
},
76 static struct regulator
*arm_regulator
;
77 static struct regulator
*int_regulator
;
79 struct s5pv210_dvs_conf
{
80 int arm_volt
; /* uV */
81 int int_volt
; /* uV */
84 static const int arm_volt_max
= 1350000;
85 static const int int_volt_max
= 1250000;
87 static struct s5pv210_dvs_conf dvs_conf
[] = {
110 static u32 clkdiv_val
[5][11] = {
112 * Clock divider value for following
113 * { APLL, A2M, HCLK_MSYS, PCLK_MSYS,
114 * HCLK_DSYS, PCLK_DSYS, HCLK_PSYS, PCLK_PSYS,
115 * ONEDRAM, MFC, G3D }
118 /* L0 : [1000/200/100][166/83][133/66][200/200] */
119 {0, 4, 4, 1, 3, 1, 4, 1, 3, 0, 0},
121 /* L1 : [800/200/100][166/83][133/66][200/200] */
122 {0, 3, 3, 1, 3, 1, 4, 1, 3, 0, 0},
124 /* L2 : [400/200/100][166/83][133/66][200/200] */
125 {1, 3, 1, 1, 3, 1, 4, 1, 3, 0, 0},
127 /* L3 : [200/200/100][166/83][133/66][200/200] */
128 {3, 3, 1, 1, 3, 1, 4, 1, 3, 0, 0},
130 /* L4 : [100/100/100][83/83][66/66][100/100] */
131 {7, 7, 0, 0, 7, 0, 9, 0, 7, 0, 0},
135 * This function set DRAM refresh counter
136 * accoriding to operating frequency of DRAM
137 * ch: DMC port number 0 or 1
138 * freq: Operating frequency of DRAM(KHz)
140 static void s5pv210_set_refresh(enum s5pv210_dmc_port ch
, unsigned long freq
)
142 unsigned long tmp
, tmp1
;
143 void __iomem
*reg
= NULL
;
146 reg
= (S5P_VA_DMC0
+ 0x30);
147 } else if (ch
== DMC1
) {
148 reg
= (S5P_VA_DMC1
+ 0x30);
150 printk(KERN_ERR
"Cannot find DMC port\n");
154 /* Find current DRAM frequency */
155 tmp
= s5pv210_dram_conf
[ch
].freq
;
159 tmp1
= s5pv210_dram_conf
[ch
].refresh
;
163 __raw_writel(tmp1
, reg
);
166 static int s5pv210_target(struct cpufreq_policy
*policy
, unsigned int index
)
169 unsigned int priv_index
;
170 unsigned int pll_changing
= 0;
171 unsigned int bus_speed_changing
= 0;
172 unsigned int old_freq
, new_freq
;
173 int arm_volt
, int_volt
;
176 mutex_lock(&set_freq_lock
);
178 if (no_cpufreq_access
) {
179 #ifdef CONFIG_PM_VERBOSE
180 pr_err("%s:%d denied access to %s as it is disabled"
181 "temporarily\n", __FILE__
, __LINE__
, __func__
);
187 old_freq
= policy
->cur
;
188 new_freq
= s5pv210_freq_table
[index
].frequency
;
190 /* Finding current running level index */
191 if (cpufreq_frequency_table_target(policy
, s5pv210_freq_table
,
192 old_freq
, CPUFREQ_RELATION_H
,
198 arm_volt
= dvs_conf
[index
].arm_volt
;
199 int_volt
= dvs_conf
[index
].int_volt
;
201 if (new_freq
> old_freq
) {
202 ret
= regulator_set_voltage(arm_regulator
,
203 arm_volt
, arm_volt_max
);
207 ret
= regulator_set_voltage(int_regulator
,
208 int_volt
, int_volt_max
);
213 /* Check if there need to change PLL */
214 if ((index
== L0
) || (priv_index
== L0
))
217 /* Check if there need to change System bus clock */
218 if ((index
== L4
) || (priv_index
== L4
))
219 bus_speed_changing
= 1;
221 if (bus_speed_changing
) {
223 * Reconfigure DRAM refresh counter value for minimum
224 * temporary clock while changing divider.
225 * expected clock is 83Mhz : 7.8usec/(1/83Mhz) = 0x287
228 s5pv210_set_refresh(DMC1
, 83000);
230 s5pv210_set_refresh(DMC1
, 100000);
232 s5pv210_set_refresh(DMC0
, 83000);
236 * APLL should be changed in this level
237 * APLL -> MPLL(for stable transition) -> APLL
238 * Some clock source's clock API are not prepared.
239 * Do not use clock API in below code.
243 * 1. Temporary Change divider for MFC and G3D
244 * SCLKA2M(200/1=200)->(200/4=50)Mhz
246 reg
= __raw_readl(S5P_CLK_DIV2
);
247 reg
&= ~(S5P_CLKDIV2_G3D_MASK
| S5P_CLKDIV2_MFC_MASK
);
248 reg
|= (3 << S5P_CLKDIV2_G3D_SHIFT
) |
249 (3 << S5P_CLKDIV2_MFC_SHIFT
);
250 __raw_writel(reg
, S5P_CLK_DIV2
);
252 /* For MFC, G3D dividing */
254 reg
= __raw_readl(S5P_CLKDIV_STAT0
);
255 } while (reg
& ((1 << 16) | (1 << 17)));
258 * 2. Change SCLKA2M(200Mhz)to SCLKMPLL in MFC_MUX, G3D MUX
259 * (200/4=50)->(667/4=166)Mhz
261 reg
= __raw_readl(S5P_CLK_SRC2
);
262 reg
&= ~(S5P_CLKSRC2_G3D_MASK
| S5P_CLKSRC2_MFC_MASK
);
263 reg
|= (1 << S5P_CLKSRC2_G3D_SHIFT
) |
264 (1 << S5P_CLKSRC2_MFC_SHIFT
);
265 __raw_writel(reg
, S5P_CLK_SRC2
);
268 reg
= __raw_readl(S5P_CLKMUX_STAT1
);
269 } while (reg
& ((1 << 7) | (1 << 3)));
272 * 3. DMC1 refresh count for 133Mhz if (index == L4) is
273 * true refresh counter is already programed in upper
276 if (!bus_speed_changing
)
277 s5pv210_set_refresh(DMC1
, 133000);
279 /* 4. SCLKAPLL -> SCLKMPLL */
280 reg
= __raw_readl(S5P_CLK_SRC0
);
281 reg
&= ~(S5P_CLKSRC0_MUX200_MASK
);
282 reg
|= (0x1 << S5P_CLKSRC0_MUX200_SHIFT
);
283 __raw_writel(reg
, S5P_CLK_SRC0
);
286 reg
= __raw_readl(S5P_CLKMUX_STAT0
);
287 } while (reg
& (0x1 << 18));
292 reg
= __raw_readl(S5P_CLK_DIV0
);
294 reg
&= ~(S5P_CLKDIV0_APLL_MASK
| S5P_CLKDIV0_A2M_MASK
|
295 S5P_CLKDIV0_HCLK200_MASK
| S5P_CLKDIV0_PCLK100_MASK
|
296 S5P_CLKDIV0_HCLK166_MASK
| S5P_CLKDIV0_PCLK83_MASK
|
297 S5P_CLKDIV0_HCLK133_MASK
| S5P_CLKDIV0_PCLK66_MASK
);
299 reg
|= ((clkdiv_val
[index
][0] << S5P_CLKDIV0_APLL_SHIFT
) |
300 (clkdiv_val
[index
][1] << S5P_CLKDIV0_A2M_SHIFT
) |
301 (clkdiv_val
[index
][2] << S5P_CLKDIV0_HCLK200_SHIFT
) |
302 (clkdiv_val
[index
][3] << S5P_CLKDIV0_PCLK100_SHIFT
) |
303 (clkdiv_val
[index
][4] << S5P_CLKDIV0_HCLK166_SHIFT
) |
304 (clkdiv_val
[index
][5] << S5P_CLKDIV0_PCLK83_SHIFT
) |
305 (clkdiv_val
[index
][6] << S5P_CLKDIV0_HCLK133_SHIFT
) |
306 (clkdiv_val
[index
][7] << S5P_CLKDIV0_PCLK66_SHIFT
));
308 __raw_writel(reg
, S5P_CLK_DIV0
);
311 reg
= __raw_readl(S5P_CLKDIV_STAT0
);
312 } while (reg
& 0xff);
314 /* ARM MCS value changed */
315 reg
= __raw_readl(S5P_ARM_MCS_CON
);
322 __raw_writel(reg
, S5P_ARM_MCS_CON
);
325 /* 5. Set Lock time = 30us*24Mhz = 0x2cf */
326 __raw_writel(0x2cf, S5P_APLL_LOCK
);
330 * 6-1. Set PMS values
331 * 6-2. Wait untile the PLL is locked
334 __raw_writel(APLL_VAL_1000
, S5P_APLL_CON
);
336 __raw_writel(APLL_VAL_800
, S5P_APLL_CON
);
339 reg
= __raw_readl(S5P_APLL_CON
);
340 } while (!(reg
& (0x1 << 29)));
343 * 7. Change souce clock from SCLKMPLL(667Mhz)
344 * to SCLKA2M(200Mhz) in MFC_MUX and G3D MUX
345 * (667/4=166)->(200/4=50)Mhz
347 reg
= __raw_readl(S5P_CLK_SRC2
);
348 reg
&= ~(S5P_CLKSRC2_G3D_MASK
| S5P_CLKSRC2_MFC_MASK
);
349 reg
|= (0 << S5P_CLKSRC2_G3D_SHIFT
) |
350 (0 << S5P_CLKSRC2_MFC_SHIFT
);
351 __raw_writel(reg
, S5P_CLK_SRC2
);
354 reg
= __raw_readl(S5P_CLKMUX_STAT1
);
355 } while (reg
& ((1 << 7) | (1 << 3)));
358 * 8. Change divider for MFC and G3D
359 * (200/4=50)->(200/1=200)Mhz
361 reg
= __raw_readl(S5P_CLK_DIV2
);
362 reg
&= ~(S5P_CLKDIV2_G3D_MASK
| S5P_CLKDIV2_MFC_MASK
);
363 reg
|= (clkdiv_val
[index
][10] << S5P_CLKDIV2_G3D_SHIFT
) |
364 (clkdiv_val
[index
][9] << S5P_CLKDIV2_MFC_SHIFT
);
365 __raw_writel(reg
, S5P_CLK_DIV2
);
367 /* For MFC, G3D dividing */
369 reg
= __raw_readl(S5P_CLKDIV_STAT0
);
370 } while (reg
& ((1 << 16) | (1 << 17)));
372 /* 9. Change MPLL to APLL in MSYS_MUX */
373 reg
= __raw_readl(S5P_CLK_SRC0
);
374 reg
&= ~(S5P_CLKSRC0_MUX200_MASK
);
375 reg
|= (0x0 << S5P_CLKSRC0_MUX200_SHIFT
);
376 __raw_writel(reg
, S5P_CLK_SRC0
);
379 reg
= __raw_readl(S5P_CLKMUX_STAT0
);
380 } while (reg
& (0x1 << 18));
383 * 10. DMC1 refresh counter
384 * L4 : DMC1 = 100Mhz 7.8us/(1/100) = 0x30c
385 * Others : DMC1 = 200Mhz 7.8us/(1/200) = 0x618
387 if (!bus_speed_changing
)
388 s5pv210_set_refresh(DMC1
, 200000);
392 * L4 level need to change memory bus speed, hence onedram clock divier
393 * and memory refresh parameter should be changed
395 if (bus_speed_changing
) {
396 reg
= __raw_readl(S5P_CLK_DIV6
);
397 reg
&= ~S5P_CLKDIV6_ONEDRAM_MASK
;
398 reg
|= (clkdiv_val
[index
][8] << S5P_CLKDIV6_ONEDRAM_SHIFT
);
399 __raw_writel(reg
, S5P_CLK_DIV6
);
402 reg
= __raw_readl(S5P_CLKDIV_STAT1
);
403 } while (reg
& (1 << 15));
405 /* Reconfigure DRAM refresh counter value */
411 s5pv210_set_refresh(DMC0
, 166000);
412 s5pv210_set_refresh(DMC1
, 200000);
418 s5pv210_set_refresh(DMC0
, 83000);
419 s5pv210_set_refresh(DMC1
, 100000);
423 if (new_freq
< old_freq
) {
424 regulator_set_voltage(int_regulator
,
425 int_volt
, int_volt_max
);
427 regulator_set_voltage(arm_regulator
,
428 arm_volt
, arm_volt_max
);
431 printk(KERN_DEBUG
"Perf changed[L%d]\n", index
);
434 mutex_unlock(&set_freq_lock
);
439 static int s5pv210_cpufreq_suspend(struct cpufreq_policy
*policy
)
444 static int s5pv210_cpufreq_resume(struct cpufreq_policy
*policy
)
450 static int check_mem_type(void __iomem
*dmc_reg
)
454 val
= __raw_readl(dmc_reg
+ 0x4);
455 val
= (val
& (0xf << 8));
460 static int __init
s5pv210_cpu_init(struct cpufreq_policy
*policy
)
462 unsigned long mem_type
;
465 policy
->clk
= clk_get(NULL
, "armclk");
466 if (IS_ERR(policy
->clk
))
467 return PTR_ERR(policy
->clk
);
469 dmc0_clk
= clk_get(NULL
, "sclk_dmc0");
470 if (IS_ERR(dmc0_clk
)) {
471 ret
= PTR_ERR(dmc0_clk
);
475 dmc1_clk
= clk_get(NULL
, "hclk_msys");
476 if (IS_ERR(dmc1_clk
)) {
477 ret
= PTR_ERR(dmc1_clk
);
481 if (policy
->cpu
!= 0) {
487 * check_mem_type : This driver only support LPDDR & LPDDR2.
488 * other memory type is not supported.
490 mem_type
= check_mem_type(S5P_VA_DMC0
);
492 if ((mem_type
!= LPDDR
) && (mem_type
!= LPDDR2
)) {
493 printk(KERN_ERR
"CPUFreq doesn't support this memory type\n");
498 /* Find current refresh counter and frequency each DMC */
499 s5pv210_dram_conf
[0].refresh
= (__raw_readl(S5P_VA_DMC0
+ 0x30) * 1000);
500 s5pv210_dram_conf
[0].freq
= clk_get_rate(dmc0_clk
);
502 s5pv210_dram_conf
[1].refresh
= (__raw_readl(S5P_VA_DMC1
+ 0x30) * 1000);
503 s5pv210_dram_conf
[1].freq
= clk_get_rate(dmc1_clk
);
505 return cpufreq_generic_init(policy
, s5pv210_freq_table
, 40000);
510 clk_put(policy
->clk
);
514 static int s5pv210_cpufreq_notifier_event(struct notifier_block
*this,
515 unsigned long event
, void *ptr
)
520 case PM_SUSPEND_PREPARE
:
521 ret
= cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ
, 0);
525 /* Disable updation of cpu frequency */
526 no_cpufreq_access
= true;
528 case PM_POST_RESTORE
:
529 case PM_POST_SUSPEND
:
530 /* Enable updation of cpu frequency */
531 no_cpufreq_access
= false;
532 cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ
, 0);
540 static int s5pv210_cpufreq_reboot_notifier_event(struct notifier_block
*this,
541 unsigned long event
, void *ptr
)
545 ret
= cpufreq_driver_target(cpufreq_cpu_get(0), SLEEP_FREQ
, 0);
549 no_cpufreq_access
= true;
553 static struct cpufreq_driver s5pv210_driver
= {
554 .flags
= CPUFREQ_STICKY
| CPUFREQ_NEED_INITIAL_FREQ_CHECK
,
555 .verify
= cpufreq_generic_frequency_table_verify
,
556 .target_index
= s5pv210_target
,
557 .get
= cpufreq_generic_get
,
558 .init
= s5pv210_cpu_init
,
561 .suspend
= s5pv210_cpufreq_suspend
,
562 .resume
= s5pv210_cpufreq_resume
,
566 static struct notifier_block s5pv210_cpufreq_notifier
= {
567 .notifier_call
= s5pv210_cpufreq_notifier_event
,
570 static struct notifier_block s5pv210_cpufreq_reboot_notifier
= {
571 .notifier_call
= s5pv210_cpufreq_reboot_notifier_event
,
574 static int __init
s5pv210_cpufreq_init(void)
576 arm_regulator
= regulator_get(NULL
, "vddarm");
577 if (IS_ERR(arm_regulator
)) {
578 pr_err("failed to get regulator vddarm");
579 return PTR_ERR(arm_regulator
);
582 int_regulator
= regulator_get(NULL
, "vddint");
583 if (IS_ERR(int_regulator
)) {
584 pr_err("failed to get regulator vddint");
585 regulator_put(arm_regulator
);
586 return PTR_ERR(int_regulator
);
589 register_pm_notifier(&s5pv210_cpufreq_notifier
);
590 register_reboot_notifier(&s5pv210_cpufreq_reboot_notifier
);
592 return cpufreq_register_driver(&s5pv210_driver
);
595 late_initcall(s5pv210_cpufreq_init
);