1 /* linux/arch/arm/mach-s5pv310/cpufreq.c
3 * Copyright (c) 2010 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com/
6 * S5PV310 - CPU frequency scaling support
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This is for S5PC210 EVT1. EVT0 support is removed.
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/err.h>
18 #include <linux/clk.h>
20 #include <linux/suspend.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/cpufreq.h>
25 #include <linux/suspend.h>
26 #include <linux/reboot.h>
27 #include <linux/pm_qos_params.h>
30 #include <plat/s5pv310.h>
32 #include <mach/regs-iem.h>
33 #include <mach/regs-clock.h>
34 #include <mach/cpu_revision.h>
36 #include <mach/pm-core.h>
37 #include <mach/cpufreq.h>
42 static struct clk *arm_clk;
43 static struct clk *moutcore;
44 static struct clk *mout_mpll;
45 static struct clk *mout_apll;
47 #ifdef CONFIG_REGULATOR
48 static struct regulator *arm_regulator;
51 static struct cpufreq_freqs freqs;
52 static int s5pv310_dvs_locking;
53 static bool s5pv310_cpufreq_init_done;
54 static DEFINE_MUTEX(set_cpu_freq_change);
55 static DEFINE_MUTEX(set_cpu_freq_lock);
57 #define LOCK_CPUFREQ(x) (x | 0x8000)
58 #define UNLOCK_CPUFREQ(x) (x | 0x4000)
59 #define SLEEP_FREQ 800000 /* L1. The frequency set by IPL */
61 /* temperary define for additional symantics for relation */
62 #define DISABLE_FURTHER_CPUFREQ 0x10
63 #define ENABLE_FURTHER_CPUFREQ 0x20
64 #define MASK_FURTHER_CPUFREQ 0x30
65 #define MASK_ONLY_SET_CPUFREQ 0x40
66 #define SET_CPU_FREQ_SAMPLING_RATE 100000
68 #define ARMCLOCK_1200MHZ 1200000
69 #define ARMCLOCK_1000MHZ 1000000
70 #define ARMCLOCK_500MHZ 500000
72 static int s5pv310_max_armclk;
74 enum s5pv310_memory_type{
80 struct cpufreq_clkdiv {
85 enum cpufreq_level_index{
86 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
89 L1, L2, L3, L4, CPUFREQ_LEVEL_END,
92 /* Using lookup table to support 1200MHz/1000MHz by reading chip id */
93 static struct cpufreq_frequency_table s5pv310_lookup_freq_table[]= {
94 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
101 {0, CPUFREQ_TABLE_END},
104 static struct cpufreq_frequency_table s5pv310_freq_table[] = {
105 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
112 {0, CPUFREQ_TABLE_END},
115 static unsigned long cpufreq_lpj_table[CPUFREQ_LEVEL_END] = {};
117 static struct cpufreq_clkdiv s5pv310_clkdiv_table[] = {
118 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
127 /* --- Implementation for PM QOS --- */
128 static enum cpufreq_level_index pm_qos_min_guarantee = UINT_MAX;
129 static enum cpufreq_level_index pm_qos_filter(enum cpufreq_level_index index)
131 /* Copy the value to avoid R/W locking for pm_qos_min_guarantee. */
132 enum cpufreq_level_index ret = pm_qos_min_guarantee;
134 ret = (index > ret) ? ret : index;
139 struct pm_qos_dma2cpufreq {
140 unsigned int dma_latency;
141 unsigned int minimum_frequency;
142 enum cpufreq_level_index level;
146 * Sorted from the slowest frequency / greatest latency.
147 * Based on DW_MMC Unmap time (512kiB)
148 * Unmapping 512kiB with DW_MMC takes
153 * Unmapping 512kiB takes up to 128 times of unmap tasks.
154 * As DW_MMC driver has been showing that unmap taks more time than
155 * other DMA-related operations and we want to use the worse-case
156 * scenario, we assume that the values represent the latency with
157 * 128 operations. Thus, we device the latency values with 128.
158 * Note that the metric in the following table is us, not ms.
159 * Also, the values are rounded-up.
161 static struct pm_qos_dma2cpufreq pm_qos_requirement[] = {
162 { 118, 200000, UINT_MAX }, /* UINT_MAX = to be defined in runtime */
163 { 40, 500000, UINT_MAX },
164 { 24, 800000, UINT_MAX },
165 { 16, 1000000, UINT_MAX },
168 static int pm_qos_notify(struct notifier_block *nb, unsigned long value,
171 int index = 0, i = 0, chosen = -EINVAL;
173 while (pm_qos_requirement[index].dma_latency) {
174 if (value >= pm_qos_requirement[index].dma_latency)
182 if (pm_qos_requirement[index].level >= CPUFREQ_LEVEL_END) {
184 s5pv310_freq_table[i].frequency != CPUFREQ_TABLE_END &&
185 s5pv310_freq_table[i].frequency >=
186 pm_qos_requirement[index].minimum_frequency;
190 pm_qos_requirement[index].level = chosen;
193 if (WARN(pm_qos_requirement[index].level >= CPUFREQ_LEVEL_END,
194 "Cannot find the proper frequency.\n"))
197 pm_qos_min_guarantee = pm_qos_requirement[index].level;
202 static struct notifier_block pm_qos_notifier = {
203 .notifier_call = pm_qos_notify,
208 /* This defines are for cpufreq lock */
209 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
210 #define CPUFREQ_LIMIT_LEVEL L0
212 #define CPUFREQ_LIMIT_LEVEL L1
215 unsigned int g_cpufreq_limit_id;
216 unsigned int g_cpufreq_limit_val[DVFS_LOCK_ID_END];
217 unsigned int g_cpufreq_limit_level = CPUFREQ_LIMIT_LEVEL;
219 #define CPUFREQ_MIN_LEVEL (CPUFREQ_LEVEL_END - 1)
221 unsigned int g_cpufreq_lock_id;
222 unsigned int g_cpufreq_lock_val[DVFS_LOCK_ID_END];
223 unsigned int g_cpufreq_lock_level = CPUFREQ_MIN_LEVEL;
225 static unsigned int clkdiv_cpu0[CPUFREQ_LEVEL_END][7] = {
227 * Clock divider value for following
228 * { DIVCORE, DIVCOREM0, DIVCOREM1, DIVPERIPH,
229 * DIVATB, DIVPCLK_DBG, DIVAPLL }
230 * CORE / COREM0 / COREM1 / PERI/ ATB / PDBG / SCLK_APLL
232 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
233 /* ARM L0: 1200MHz / 300MHz / 150MHz / 300 / 200 / 100 / 100 */
234 { 0, 3, 7, 3, 4, 1, 7 },
236 /* ARM L1: 1000MHz / 250MHz / 125MHz / 250 / 200 / 100 / 100 */
237 { 0, 3, 7, 3, 4, 1, 7 },
238 /* ARM L2: 800MHz / 200MHz / 100MHz / 200 / 200 / 100 / 100 */
239 { 0, 3, 7, 3, 3, 1, 7 },
240 /* ARM L3: 500MHz / 125MHz / 63MHz / 125 / 125 / 63 / 100 */
241 { 0, 3, 7, 3, 3, 1, 7 },
242 /* ARM L4: 200MHz / 100MHz / 50MHz / 100 / 50 / 25 / 100 */
243 { 0, 1, 3, 1, 3, 1, 7 },
246 static unsigned int clkdiv_cpu1[CPUFREQ_LEVEL_END][2] = {
247 /* Clock divider value for following
248 * { DIVCOPY, DIVHPM }
250 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
251 /* ARM L0: 1200MHz */
254 /* ARM L1: 1000MHz */
264 static unsigned int clkdiv_cpu0_lookup[][7] = {
266 * Clock divider value for following
267 * { DIVCORE, DIVCOREM0, DIVCOREM1, DIVPERIPH,
268 * DIVATB, DIVPCLK_DBG, DIVAPLL }
270 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
271 /* ARM L0: 1200MHz */
272 { 0, 3, 7, 3, 4, 1, 7 },
274 /* ARM L1: 1000MHz */
275 { 0, 3, 7, 3, 4, 1, 7 },
278 { 0, 3, 7, 3, 3, 1, 7 },
281 { 0, 3, 7, 3, 3, 1, 7 },
284 { 0, 1, 3, 1, 3, 1, 7 },
287 static unsigned int clkdiv_cpu1_lookup[][2] = {
288 /* Clock divider value for following
289 * { DIVCOPY, DIVHPM }
291 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
292 /* ARM L0: 1200MHz */
295 /* ARM L1: 1000MHz */
308 struct cpufreq_voltage_table {
309 unsigned int index; /* any */
310 unsigned int arm_volt; /* uV */
313 /* Using lookup table to support 1200MHz/1000MHz by reading chip id */
314 static struct cpufreq_voltage_table s5pv310_lookup_volt_table[] = {
316 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
335 static const struct cpufreq_voltage_table s5pv310_volt_table_SS[CPUFREQ_LEVEL_END] = {
337 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
356 static const struct cpufreq_voltage_table s5pv310_volt_table_A1[CPUFREQ_LEVEL_END] = {
358 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
377 static const struct cpufreq_voltage_table s5pv310_volt_table_A2[CPUFREQ_LEVEL_END] = {
379 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
398 static const struct cpufreq_voltage_table s5pv310_volt_table_B1[CPUFREQ_LEVEL_END] = {
400 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
419 static const struct cpufreq_voltage_table s5pv310_volt_table_B2[CPUFREQ_LEVEL_END] = {
421 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
440 static const struct cpufreq_voltage_table s5pv310_volt_table_C1[CPUFREQ_LEVEL_END] = {
442 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
461 static const struct cpufreq_voltage_table s5pv310_volt_table_C2[CPUFREQ_LEVEL_END] = {
463 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
482 static const struct cpufreq_voltage_table s5pv310_volt_table_D[CPUFREQ_LEVEL_END] = {
484 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
503 static struct cpufreq_voltage_table s5pv310_volt_table[CPUFREQ_LEVEL_END];
505 static unsigned int s5pv310_lookup_apll_pms_table[CPUFREQ_LEVEL_END] = {
506 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
507 /* APLL FOUT L0: 1200MHz */
508 ((150<<16)|(3<<8)|(0x1)),
510 /* APLL FOUT L1: 1000MHz */
511 ((250<<16)|(6<<8)|(0x1)),
513 /* APLL FOUT L2: 800MHz */
514 ((200<<16)|(6<<8)|(0x1)),
516 /* APLL FOUT L3: 500MHz */
517 ((250<<16)|(6<<8)|(0x2)),
519 /* APLL FOUT L4: 200MHz */
520 ((200<<16)|(6<<8)|(0x3)),
523 static unsigned int s5pv310_apll_pms_table[CPUFREQ_LEVEL_END] = {
524 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
525 /* APLL FOUT L0: 1200MHz */
526 ((150<<16)|(3<<8)|(0x1)),
528 /* APLL FOUT L1: 1000MHz */
529 ((250<<16)|(6<<8)|(0x1)),
531 /* APLL FOUT L2: 800MHz */
532 ((200<<16)|(6<<8)|(0x1)),
534 /* APLL FOUT L3: 500MHz */
535 ((250<<16)|(6<<8)|(0x2)),
537 /* APLL FOUT L4: 200MHz */
538 ((200<<16)|(6<<8)|(0x3)),
541 static int s5pv310_verify_policy(struct cpufreq_policy *policy)
543 return cpufreq_frequency_table_verify(policy, s5pv310_freq_table);
546 static unsigned int s5pv310_getspeed(unsigned int cpu)
550 rate = clk_get_rate(arm_clk) / 1000;
555 static void s5pv310_set_clkdiv(unsigned int div_index)
558 /* Change Divider - CPU0 */
559 tmp = __raw_readl(S5P_CLKDIV_CPU);
561 tmp &= ~(S5P_CLKDIV_CPU0_CORE_MASK | S5P_CLKDIV_CPU0_COREM0_MASK |
562 S5P_CLKDIV_CPU0_COREM1_MASK | S5P_CLKDIV_CPU0_PERIPH_MASK |
563 S5P_CLKDIV_CPU0_ATB_MASK | S5P_CLKDIV_CPU0_PCLKDBG_MASK |
564 S5P_CLKDIV_CPU0_APLL_MASK);
566 tmp |= ((clkdiv_cpu0[div_index][0] << S5P_CLKDIV_CPU0_CORE_SHIFT) |
567 (clkdiv_cpu0[div_index][1] << S5P_CLKDIV_CPU0_COREM0_SHIFT) |
568 (clkdiv_cpu0[div_index][2] << S5P_CLKDIV_CPU0_COREM1_SHIFT) |
569 (clkdiv_cpu0[div_index][3] << S5P_CLKDIV_CPU0_PERIPH_SHIFT) |
570 (clkdiv_cpu0[div_index][4] << S5P_CLKDIV_CPU0_ATB_SHIFT) |
571 (clkdiv_cpu0[div_index][5] << S5P_CLKDIV_CPU0_PCLKDBG_SHIFT) |
572 (clkdiv_cpu0[div_index][6] << S5P_CLKDIV_CPU0_APLL_SHIFT));
574 __raw_writel(tmp, S5P_CLKDIV_CPU);
577 tmp = __raw_readl(S5P_CLKDIV_STATCPU);
578 } while (tmp & 0x1111111);
580 /* Change Divider - CPU1 */
581 tmp = __raw_readl(S5P_CLKDIV_CPU1);
583 tmp &= ~((0x7 << 4) | (0x7));
585 tmp |= ((clkdiv_cpu1[div_index][0] << 4) |
586 (clkdiv_cpu1[div_index][1] << 0));
588 __raw_writel(tmp, S5P_CLKDIV_CPU1);
591 tmp = __raw_readl(S5P_CLKDIV_STATCPU1);
592 } while (tmp & 0x11);
595 static void s5pv310_set_apll(unsigned int index)
598 unsigned int save_val;
600 /* 1. MUX_CORE_SEL = MPLL,
601 * Reduce the CLKDIVCPU value for using MPLL */
602 save_val = __raw_readl(S5P_CLKDIV_CPU);
604 tmp &= ~S5P_CLKDIV_CPU0_CORE_MASK;
605 tmp |= (((save_val & 0xf) + 1) << S5P_CLKDIV_CPU0_CORE_SHIFT);
606 __raw_writel(tmp, S5P_CLKDIV_CPU);
608 tmp = __raw_readl(S5P_CLKDIV_STATCPU);
611 /* ARMCLK uses MPLL for lock time */
612 clk_set_parent(moutcore, mout_mpll);
615 tmp = (__raw_readl(S5P_CLKMUX_STATCPU)
616 >> S5P_CLKSRC_CPU_MUXCORE_SHIFT);
618 } while (tmp != 0x2);
620 /* 2. Set APLL Lock time */
621 __raw_writel(S5P_APLL_LOCKTIME, S5P_APLL_LOCK);
623 /* 3. Change PLL PMS values */
624 tmp = __raw_readl(S5P_APLL_CON0);
625 tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0));
626 tmp |= s5pv310_apll_pms_table[index];
627 __raw_writel(tmp, S5P_APLL_CON0);
629 /* 4. wait_lock_time */
631 tmp = __raw_readl(S5P_APLL_CON0);
632 } while (!(tmp & (0x1 << S5P_APLLCON0_LOCKED_SHIFT)));
634 /* 5. MUX_CORE_SEL = APLL */
635 clk_set_parent(moutcore, mout_apll);
638 tmp = __raw_readl(S5P_CLKMUX_STATCPU);
639 tmp &= S5P_CLKMUX_STATCPU_MUXCORE_MASK;
640 } while (tmp != (0x1 << S5P_CLKSRC_CPU_MUXCORE_SHIFT));
642 /* Restore the CLKDIVCPU value for APLL */
643 __raw_writel(save_val, S5P_CLKDIV_CPU);
645 tmp = __raw_readl(S5P_CLKDIV_STATCPU);
650 static void s5pv310_set_frequency(unsigned int old_index,
651 unsigned int new_index)
654 unsigned int is_curfreq_table = 0;
655 unsigned int change_s_value = 0;
657 if (freqs.old == s5pv310_freq_table[old_index].frequency)
658 is_curfreq_table = 1;
660 if (freqs.old < freqs.new) {
661 if (s5pv310_max_armclk == ARMCLOCK_1200MHZ) {
662 /* L1/L3, L2/L4 Level change require to only change s value */
663 if (is_curfreq_table &&
664 (((old_index == L3) && (new_index == L1)) ||
665 ((old_index == L4) && (new_index == L2))))
669 /* L0/L2, L1/L3 Level change require to only change s value */
670 if (is_curfreq_table &&
672 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
673 ((old_index == L2) && (new_index == L0)) ||
675 ((old_index == L3) && (new_index == L1))))
679 if (change_s_value) {
680 /* 1. Change the system clock divider values */
681 s5pv310_set_clkdiv(new_index);
683 /* 2. Change just s value in apll m,p,s value */
684 tmp = __raw_readl(S5P_APLL_CON0);
686 tmp |= (s5pv310_apll_pms_table[new_index] & 0x7);
687 __raw_writel(tmp, S5P_APLL_CON0);
690 /* Clock Configuration Procedure */
692 /* 1. Change the system clock divider values */
693 s5pv310_set_clkdiv(new_index);
695 /* 2. Change the apll m,p,s value */
696 if (freqs.new == ARMCLOCK_500MHZ) {
697 regulator_set_voltage(arm_regulator,
698 s5pv310_volt_table[new_index - 1].arm_volt,
699 s5pv310_volt_table[new_index - 1].arm_volt);
701 s5pv310_set_apll(new_index);
703 } else if (freqs.old > freqs.new) {
704 if (s5pv310_max_armclk == ARMCLOCK_1200MHZ) {
705 /* L1/L3, L2/L4 Level change require to only change s value */
706 if (is_curfreq_table &&
707 (((old_index == L1) && (new_index == L3)) ||
708 ((old_index == L2) && (new_index == L4))))
711 /* L0/L2, L1/L3 Level change require to only change s value */
712 if (is_curfreq_table &&
714 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
715 ((old_index == L0) && (new_index == L2)) ||
717 ((old_index == L1) && (new_index == L3))))
721 if (change_s_value) {
722 /* 1. Change just s value in apll m,p,s value */
723 tmp = __raw_readl(S5P_APLL_CON0);
725 tmp |= (s5pv310_apll_pms_table[new_index] & 0x7);
726 __raw_writel(tmp, S5P_APLL_CON0);
728 /* 2. Change the system clock divider values */
729 s5pv310_set_clkdiv(new_index);
731 /* Clock Configuration Procedure */
733 /* 1. Change the apll m,p,s value */
734 s5pv310_set_apll(new_index);
736 /* 2. Change the system clock divider values */
737 s5pv310_set_clkdiv(new_index);
743 * In S5PC210/S5PV310, every core has the same frequency so, we
744 * don't need to distinguish cores when we set jiffies or frequencies.
746 static void adjust_jiffies(unsigned int index)
752 loops_per_jiffy = cpufreq_lpj_table[index];
755 for_each_online_cpu(cpu)
756 smp_store_cpu_info(cpu);
760 static int s5pv310_target(struct cpufreq_policy *policy,
761 unsigned int target_freq, unsigned int relation)
765 unsigned int index, old_index;
766 unsigned int arm_volt;
768 unsigned int check_gov = 0;
770 mutex_lock(&set_cpu_freq_change);
772 if ((relation & ENABLE_FURTHER_CPUFREQ) &&
773 (relation & DISABLE_FURTHER_CPUFREQ)) {
774 /* Invalidate both if both marked */
775 relation &= ~ENABLE_FURTHER_CPUFREQ;
776 relation &= ~DISABLE_FURTHER_CPUFREQ;
777 printk(KERN_ERR "%s:%d denied marking \"FURTHER_CPUFREQ\""
778 " as both marked.\n",
782 if (!strncmp(policy->governor->name, "ondemand", CPUFREQ_NAME_LEN)
783 || !strncmp(policy->governor->name, "conservative", CPUFREQ_NAME_LEN)) {
785 if (relation & ENABLE_FURTHER_CPUFREQ)
786 s5pv310_dvs_locking = 0;
788 if (s5pv310_dvs_locking == 1)
791 if (relation & DISABLE_FURTHER_CPUFREQ)
792 s5pv310_dvs_locking = 1;
794 relation &= ~(MASK_FURTHER_CPUFREQ | MASK_ONLY_SET_CPUFREQ);
796 if ((relation & ENABLE_FURTHER_CPUFREQ) ||
797 (relation & DISABLE_FURTHER_CPUFREQ) ||
798 (relation & MASK_ONLY_SET_CPUFREQ))
802 freqs.old = s5pv310_getspeed(policy->cpu);
804 if (cpufreq_frequency_table_target(policy, s5pv310_freq_table,
805 freqs.old, relation, &old_index)) {
810 if (cpufreq_frequency_table_target(policy, s5pv310_freq_table,
811 target_freq, relation, &index)) {
816 if (s5pv310_max_armclk == ARMCLOCK_1200MHZ) {
817 #ifdef CONFIG_FREQ_STEP_UP_L2_L0
818 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
819 /* change L2 -> L0 */
820 if ((index == L0) && (old_index > L2)) {
826 /* Prevent from jumping to 1GHz directly */
827 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
828 if((index == L0) && (old_index > L1))
838 if ((index > g_cpufreq_lock_level) && check_gov)
839 index = g_cpufreq_lock_level;
840 if ((index < g_cpufreq_limit_level) && check_gov)
841 index = g_cpufreq_limit_level;
843 /* The filter with highest priority: PM QOS */
844 index = pm_qos_filter(index);
846 freqs.new = s5pv310_freq_table[index].frequency;
847 freqs.cpu = policy->cpu;
849 /* If the new frequency is same with previous frequency, skip */
850 if (freqs.new == freqs.old)
854 printk(KERN_ERR "CPUMON F %d\n", freqs.new);
857 /* get the voltage value */
858 arm_volt = s5pv310_volt_table[index].arm_volt;
859 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
861 /* When the new frequency is higher than current frequency */
862 if (freqs.new > freqs.old) {
863 /* Firstly, voltage up to increase frequency */
864 #if defined(CONFIG_REGULATOR)
865 regulator_set_voltage(arm_regulator, arm_volt, arm_volt + 150000);
869 s5pv310_set_frequency(old_index, index);
871 adjust_jiffies(index);
872 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
874 /* When the new frequency is lower than current frequency */
875 if (freqs.new < freqs.old) {
876 /* down the voltage after frequency change */
877 #if defined(CONFIG_REGULATOR)
878 regulator_set_voltage(arm_regulator, arm_volt, arm_volt + 150000);
883 mutex_unlock(&set_cpu_freq_change);
887 mutex_unlock(&set_cpu_freq_change);
888 s5pv310_busfreq_force_notify(&freqs);
893 atomic_t s5pv310_cpufreq_lock_count;
895 int s5pv310_cpufreq_lock(unsigned int nId,
896 enum cpufreq_level_request cpufreq_level)
898 int ret = 0, cpu = 0;
899 unsigned int cur_freq;
901 if (!s5pv310_cpufreq_init_done)
904 if (s5pv310_max_armclk != ARMCLOCK_1200MHZ) {
905 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
906 if (cpufreq_level != CPU_L0) {
909 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
912 "[CPUFREQ]cpufreq lock to 1GHz in place of 1.2GHz\n");
917 if (g_cpufreq_lock_id & (1 << nId)) {
919 "[CPUFREQ]This device [%d] already locked cpufreq\n", nId);
922 mutex_lock(&set_cpu_freq_lock);
923 g_cpufreq_lock_id |= (1 << nId);
924 g_cpufreq_lock_val[nId] = cpufreq_level;
926 /* If the requested cpufreq is higher than current min frequency */
927 if (cpufreq_level < g_cpufreq_lock_level)
928 g_cpufreq_lock_level = cpufreq_level;
929 mutex_unlock(&set_cpu_freq_lock);
931 /* If current frequency is lower than requested freq, need to update */
932 cur_freq = s5pv310_getspeed(cpu);
933 if (cur_freq < s5pv310_freq_table[cpufreq_level].frequency) {
934 ret = cpufreq_driver_target(cpufreq_cpu_get(cpu),
935 s5pv310_freq_table[cpufreq_level].frequency,
936 MASK_ONLY_SET_CPUFREQ);
941 EXPORT_SYMBOL_GPL(s5pv310_cpufreq_lock);
943 void s5pv310_cpufreq_lock_free(unsigned int nId)
947 if (!s5pv310_cpufreq_init_done)
950 mutex_lock(&set_cpu_freq_lock);
952 g_cpufreq_lock_id &= ~(1 << nId);
953 g_cpufreq_lock_val[nId] = CPUFREQ_MIN_LEVEL;
954 g_cpufreq_lock_level = CPUFREQ_MIN_LEVEL;
955 if (g_cpufreq_lock_id) {
956 for (i = 0; i < DVFS_LOCK_ID_END; i++) {
957 if (g_cpufreq_lock_val[i] < g_cpufreq_lock_level)
958 g_cpufreq_lock_level = g_cpufreq_lock_val[i];
961 mutex_unlock(&set_cpu_freq_lock);
963 EXPORT_SYMBOL_GPL(s5pv310_cpufreq_lock_free);
965 int s5pv310_cpufreq_upper_limit(unsigned int nId, enum cpufreq_level_request cpufreq_level)
967 int ret = 0, cpu = 0;
968 unsigned int cur_freq;
970 if (!s5pv310_cpufreq_init_done)
973 if (s5pv310_max_armclk != ARMCLOCK_1200MHZ) {
974 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
975 if (cpufreq_level != CPU_L0) {
978 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
981 "[CPUFREQ]cpufreq lock to 1GHz in place of 1.2GHz\n");
986 if (g_cpufreq_limit_id & (1 << nId)) {
987 printk(KERN_ERR "[CPUFREQ]This device [%d] already limited cpufreq\n", nId);
991 mutex_lock(&set_cpu_freq_lock);
992 g_cpufreq_limit_id |= (1 << nId);
993 g_cpufreq_limit_val[nId] = cpufreq_level;
995 /* If the requested limit level is lower than current value */
996 if (cpufreq_level > g_cpufreq_limit_level)
997 g_cpufreq_limit_level = cpufreq_level;
999 mutex_unlock(&set_cpu_freq_lock);
1001 /* If cur frequency is higher than limit freq, it needs to update */
1002 cur_freq = s5pv310_getspeed(cpu);
1003 if (cur_freq > s5pv310_freq_table[cpufreq_level].frequency) {
1004 ret = cpufreq_driver_target(cpufreq_cpu_get(cpu),
1005 s5pv310_freq_table[cpufreq_level].frequency,
1006 MASK_ONLY_SET_CPUFREQ);
1012 void s5pv310_cpufreq_upper_limit_free(unsigned int nId)
1016 if (!s5pv310_cpufreq_init_done)
1019 mutex_lock(&set_cpu_freq_lock);
1020 g_cpufreq_limit_id &= ~(1 << nId);
1021 g_cpufreq_limit_val[nId] = CPUFREQ_LIMIT_LEVEL;
1022 g_cpufreq_limit_level = CPUFREQ_LIMIT_LEVEL;
1023 if (g_cpufreq_limit_id) {
1024 for (i = 0; i < DVFS_LOCK_ID_END; i++) {
1025 if (g_cpufreq_limit_val[i] > g_cpufreq_limit_level)
1026 g_cpufreq_limit_level = g_cpufreq_limit_val[i];
1029 mutex_unlock(&set_cpu_freq_lock);
1033 static int s5pv310_cpufreq_suspend(struct cpufreq_policy *policy,
1039 static int s5pv310_cpufreq_resume(struct cpufreq_policy *policy)
1045 static int s5pv310_cpufreq_notifier_event(struct notifier_block *this,
1046 unsigned long event, void *ptr)
1048 unsigned int cpu = 0;
1052 case PM_RESTORE_PREPARE:
1053 case PM_HIBERNATION_PREPARE:
1054 case PM_SUSPEND_PREPARE:
1055 ret = cpufreq_driver_target(cpufreq_cpu_get(cpu),
1056 s5pv310_freq_table[L1].frequency, DISABLE_FURTHER_CPUFREQ);
1057 if (WARN_ON(ret < 0))
1059 printk(KERN_DEBUG "PM_SUSPEND_PREPARE for CPUFREQ\n");
1061 case PM_POST_HIBERNATION:
1062 case PM_POST_RESTORE:
1063 case PM_POST_SUSPEND:
1064 printk(KERN_DEBUG "PM_POST_SUSPEND for CPUFREQ: %d\n", ret);
1065 ret = cpufreq_driver_target(cpufreq_cpu_get(cpu),
1066 s5pv310_freq_table[L1].frequency, ENABLE_FURTHER_CPUFREQ);
1072 static struct notifier_block s5pv310_cpufreq_notifier = {
1073 .notifier_call = s5pv310_cpufreq_notifier_event,
1076 static int s5pv310_cpufreq_reboot_notifier_call(struct notifier_block *this,
1077 unsigned long code, void *_cmd)
1079 unsigned int cpu = 0;
1081 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1083 if (strncmp(policy->governor->name, "powersave", CPUFREQ_NAME_LEN)) {
1084 ret = cpufreq_driver_target(policy,
1085 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
1086 s5pv310_freq_table[L0].frequency, DISABLE_FURTHER_CPUFREQ);
1088 s5pv310_freq_table[L1].frequency, DISABLE_FURTHER_CPUFREQ);
1092 s5pv310_busfreq_lock(DVFS_LOCK_ID_PM, BUS_L0);
1094 #if defined(CONFIG_REGULATOR)
1095 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
1096 regulator_set_voltage(arm_regulator, s5pv310_volt_table[L0].arm_volt,
1097 s5pv310_volt_table[L0].arm_volt);
1099 regulator_set_voltage(arm_regulator, s5pv310_volt_table[L1].arm_volt,
1100 s5pv310_volt_table[L1].arm_volt);
1105 printk(KERN_INFO "U1 REBOOT Notifier for CPUFREQ\n");
1111 static struct notifier_block s5pv310_cpufreq_reboot_notifier = {
1112 .notifier_call = s5pv310_cpufreq_reboot_notifier_call,
1115 static int s5pv310_cpufreq_cpu_init(struct cpufreq_policy *policy)
1117 policy->cur = policy->min = policy->max = s5pv310_getspeed(policy->cpu);
1119 cpufreq_frequency_table_get_attr(s5pv310_freq_table, policy->cpu);
1121 policy->cpuinfo.transition_latency = 100000;
1123 if (!cpu_online(1)) {
1124 cpumask_copy(policy->related_cpus, cpu_possible_mask);
1125 cpumask_copy(policy->cpus, cpu_online_mask);
1127 cpumask_setall(policy->cpus);
1130 return cpufreq_frequency_table_cpuinfo(policy, s5pv310_freq_table);
1133 static struct cpufreq_driver s5pv310_driver = {
1134 .flags = CPUFREQ_STICKY,
1135 .verify = s5pv310_verify_policy,
1136 .target = s5pv310_target,
1137 .get = s5pv310_getspeed,
1138 .init = s5pv310_cpufreq_cpu_init,
1139 .name = "s5pv310_cpufreq",
1141 .suspend = s5pv310_cpufreq_suspend,
1142 .resume = s5pv310_cpufreq_resume,
1146 #define HPM_OFFSET 12
1147 #define HPM_MASK 0x1F
1148 #define IDS_OFFSET 24
1149 #define IDS_MASK 0xFF
1151 #define HPM_MINIMUM_B 7
1152 #define HPM_MINIMUM_C 14
1153 #define HPM_MINIMUM_D 21
1155 #define IDS_MINIMUM_B 8
1156 #define IDS_MINIMUM_C 20
1157 #define IDS_MINIMUM_D 60
1159 static void __init s5pv310_set_voltage(void)
1161 unsigned int asv_group;
1163 asv_group = exynos4_asv_group;
1165 printk(KERN_INFO "DVFS : VDD_ARM Voltage table set with %d Group\n",asv_group);
1167 switch (asv_group) {
1169 memcpy(s5pv310_volt_table, s5pv310_volt_table_SS,
1170 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1173 memcpy(s5pv310_volt_table, s5pv310_volt_table_A1,
1174 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1177 memcpy(s5pv310_volt_table, s5pv310_volt_table_A2,
1178 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1181 memcpy(s5pv310_volt_table, s5pv310_volt_table_B1,
1182 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1185 memcpy(s5pv310_volt_table, s5pv310_volt_table_B2,
1186 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1189 memcpy(s5pv310_volt_table, s5pv310_volt_table_C1,
1190 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1193 memcpy(s5pv310_volt_table, s5pv310_volt_table_C2,
1194 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1197 memcpy(s5pv310_volt_table, s5pv310_volt_table_D,
1198 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1201 WARN(true, "Incorrect ASV Value.\n");
1204 memcpy(s5pv310_lookup_volt_table, s5pv310_volt_table,
1205 sizeof(struct cpufreq_voltage_table) * CPUFREQ_LEVEL_END);
1208 static int s5pv310_update_dvfs_table(void)
1214 printk(KERN_INFO "@@@@@ original dvfs table data @@@@@\n");
1215 for (i = 0; i < CPUFREQ_LEVEL_END; i++)
1216 printk(KERN_ERR "index = %d, frequecy = %d\n",
1217 s5pv310_freq_table[i].index, s5pv310_freq_table[i].frequency);
1219 for (i = 0; i < CPUFREQ_LEVEL_END; i++)
1220 printk(KERN_ERR "index = %d, arm_volt = %d\n",
1221 s5pv310_volt_table[i].index, s5pv310_volt_table[i].arm_volt);
1223 for (i = 0; i < CPUFREQ_LEVEL_END; i++)
1224 printk(KERN_ERR "apll pms_table = 0x%08x\n", s5pv310_apll_pms_table[i]);
1226 for (i = 0; i < CPUFREQ_LEVEL_END; i++) {
1227 for (j = 0; j < 7; j++)
1228 printk("%d, ", clkdiv_cpu0[i][j]);
1232 for (i = 0; i < CPUFREQ_LEVEL_END; i++) {
1233 for (j = 0; j < 2; j++)
1234 printk("%d, ", clkdiv_cpu1[i][j]);
1239 // Get the maximum arm clock */
1240 s5pv310_max_armclk = s5pv310_get_max_speed();
1241 printk(KERN_INFO "armclk set max %d \n", s5pv310_max_armclk);
1243 if (s5pv310_max_armclk < 0) {
1244 printk(KERN_ERR "Fail to get max armclk infomatioin.\n");
1245 s5pv310_max_armclk = 0; /* 1000MHz as default value */
1249 switch (s5pv310_max_armclk) {
1251 printk(KERN_INFO "armclk set max 1200MHz as default@@@@@\n");
1257 /* TODO: This could be buggy. Fortunately we have 1.2G for now */
1258 s5pv310_max_armclk = ARMCLOCK_1000MHZ;
1259 printk(KERN_INFO "@@@@@ armclk set max 1000MHz @@@@@\n");
1261 * Prepare to dvfs table to work maximum 1000MHz
1263 * Copy freq_table, volt_table, apll_pms_table, clk_div0_table,
1264 * and clk_div1_table from lists of lookup table.
1266 for (i = 1; i < CPUFREQ_LEVEL_END; i++) {
1267 s5pv310_freq_table[i-1].index = s5pv310_lookup_freq_table[i].index - 1;
1268 s5pv310_freq_table[i-1].frequency = s5pv310_lookup_freq_table[i].frequency;
1269 printk(KERN_INFO "index = %d, frequency = %d\n",
1270 s5pv310_freq_table[i-1].index, s5pv310_freq_table[i-1].frequency);
1273 for (i = 1; i < CPUFREQ_LEVEL_END; i++) {
1274 s5pv310_volt_table[i-1].index = s5pv310_lookup_volt_table[i].index - 1;
1275 s5pv310_volt_table[i-1].arm_volt = s5pv310_lookup_volt_table[i].arm_volt;
1276 printk(KERN_INFO "index = %d, arm_volt = %d\n",
1277 s5pv310_volt_table[i-1].index, s5pv310_volt_table[i-1].arm_volt);
1280 for (i = 1; i < CPUFREQ_LEVEL_END; i++) {
1281 s5pv310_apll_pms_table[i-1] = s5pv310_lookup_apll_pms_table[i];
1282 printk(KERN_INFO "apll pms_table = 0x%08x\n", s5pv310_apll_pms_table[i-1]);
1285 for (i = 1; i < CPUFREQ_LEVEL_END; i++) {
1286 for (j = 0; j < 7; j++) {
1287 clkdiv_cpu0[i-1][j] = clkdiv_cpu0_lookup[i][j];
1288 printk("%d, ", clkdiv_cpu0[i-1][j]);
1293 for (i = 1; i < CPUFREQ_LEVEL_END; i++) {
1294 for (j = 0; j < 2; j++) {
1295 clkdiv_cpu1[i-1][j] = clkdiv_cpu1_lookup[i][j];
1296 printk("%d, ", clkdiv_cpu1[i-1][j]);
1300 printk(KERN_INFO "@@@@@ updated dvfs table @@@@@@\n");
1306 static int __init s5pv310_cpufreq_init(void)
1310 unsigned long def_freq;
1312 if (cpu_revision < 1) {
1313 pr_emerg("This driver supports S5PC210 EVT1 or later only.\n");
1317 s5pv310_set_voltage();
1319 arm_clk = clk_get(NULL, "armclk");
1320 if (IS_ERR(arm_clk))
1321 return PTR_ERR(arm_clk);
1323 moutcore = clk_get(NULL, "moutcore");
1324 if (IS_ERR(moutcore))
1327 mout_mpll = clk_get(NULL, "mout_mpll");
1328 if (IS_ERR(mout_mpll))
1331 mout_apll = clk_get(NULL, "mout_apll");
1332 if (IS_ERR(mout_apll))
1335 #if defined(CONFIG_REGULATOR)
1336 arm_regulator = regulator_get(NULL, "vdd_arm");
1337 if (IS_ERR(arm_regulator)) {
1338 pr_err("failed to get resource %s\n", "vdd_arm");
1341 s5pv310_dvs_locking = 0;
1345 * By getting chip information from pkg_id & pro_id register,
1346 * adujst dvfs level, update clk divider, voltage table,
1347 * apll pms value of dvfs table.
1349 if (s5pv310_update_dvfs_table() < 0)
1350 printk(KERN_INFO "arm clock limited to maximum 1000MHz.\n");
1352 register_pm_notifier(&s5pv310_cpufreq_notifier);
1353 register_reboot_notifier(&s5pv310_cpufreq_reboot_notifier);
1355 tmp = __raw_readl(S5P_CLKDIV_CPU);
1357 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
1358 for (i = L0; i < CPUFREQ_LEVEL_END; i++) {
1360 for (i = L1; i < CPUFREQ_LEVEL_END; i++) {
1362 tmp &= ~(S5P_CLKDIV_CPU0_CORE_MASK |
1363 S5P_CLKDIV_CPU0_COREM0_MASK |
1364 S5P_CLKDIV_CPU0_COREM1_MASK |
1365 S5P_CLKDIV_CPU0_PERIPH_MASK |
1366 S5P_CLKDIV_CPU0_ATB_MASK |
1367 S5P_CLKDIV_CPU0_PCLKDBG_MASK |
1368 S5P_CLKDIV_CPU0_APLL_MASK);
1370 tmp |= ((clkdiv_cpu0[i][0] << S5P_CLKDIV_CPU0_CORE_SHIFT) |
1371 (clkdiv_cpu0[i][1] << S5P_CLKDIV_CPU0_COREM0_SHIFT) |
1372 (clkdiv_cpu0[i][2] << S5P_CLKDIV_CPU0_COREM1_SHIFT) |
1373 (clkdiv_cpu0[i][3] << S5P_CLKDIV_CPU0_PERIPH_SHIFT) |
1374 (clkdiv_cpu0[i][4] << S5P_CLKDIV_CPU0_ATB_SHIFT) |
1375 (clkdiv_cpu0[i][5] << S5P_CLKDIV_CPU0_PCLKDBG_SHIFT) |
1376 (clkdiv_cpu0[i][6] << S5P_CLKDIV_CPU0_APLL_SHIFT));
1378 s5pv310_clkdiv_table[i].clkdiv = tmp;
1381 def_freq = s5pv310_getspeed(0) / 100000; /* by 100MHz */
1382 #ifdef CONFIG_S5PV310_ENABLE_1200MHZ
1383 for (i = L0; i < CPUFREQ_LEVEL_END; i++) {
1385 for (i = L1; i < CPUFREQ_LEVEL_END; i++) {
1387 cpufreq_lpj_table[i] = loops_per_jiffy *
1388 (s5pv310_freq_table[i].frequency /
1389 100000) /* by 100MHz */
1393 if (cpufreq_register_driver(&s5pv310_driver)) {
1394 pr_err("failed to register cpufreq driver\n");
1398 atomic_set(&s5pv310_cpufreq_lock_count, 0);
1399 s5pv310_cpufreq_init_done = true;
1401 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, &pm_qos_notifier);
1405 #ifdef CONFIG_REGULATOR
1406 if (!IS_ERR(arm_regulator))
1407 regulator_put(arm_regulator);
1410 if (!IS_ERR(mout_apll))
1413 if (!IS_ERR(mout_mpll))
1416 if (!IS_ERR(moutcore))
1419 if (!IS_ERR(arm_clk))
1422 pr_debug("%s: failed initialization\n", __func__);
1426 late_initcall(s5pv310_cpufreq_init);