Kukjin Kim | f7d7707 | 2011-06-01 14:18:22 -0700 | [diff] [blame] | 1 | /* |
Kukjin Kim | 7d30e8b | 2011-02-14 16:33:10 +0900 | [diff] [blame] | 2 | * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd. |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 3 | * http://www.samsung.com |
| 4 | * |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 5 | * EXYNOS4210 - CPU frequency scaling support |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 6 | * |
| 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. |
| 10 | */ |
| 11 | |
Jaecheol Lee | 6c523c6 | 2012-01-07 20:18:39 +0900 | [diff] [blame] | 12 | #include <linux/module.h> |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/clk.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/slab.h> |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 18 | #include <linux/cpufreq.h> |
| 19 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 20 | #include <mach/regs-clock.h> |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 21 | #include <mach/cpufreq.h> |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 22 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 23 | static struct clk *cpu_clk; |
| 24 | static struct clk *moutcore; |
| 25 | static struct clk *mout_mpll; |
| 26 | static struct clk *mout_apll; |
| 27 | |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 28 | static unsigned int exynos4210_volt_table[] = { |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 29 | 1250000, 1150000, 1050000, 975000, 950000, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 30 | }; |
| 31 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 32 | static struct cpufreq_frequency_table exynos4210_freq_table[] = { |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 33 | {L0, 1200 * 1000}, |
| 34 | {L1, 1000 * 1000}, |
| 35 | {L2, 800 * 1000}, |
| 36 | {L3, 500 * 1000}, |
| 37 | {L4, 200 * 1000}, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 38 | {0, CPUFREQ_TABLE_END}, |
| 39 | }; |
| 40 | |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 41 | static struct apll_freq apll_freq_4210[] = { |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 42 | /* |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 43 | * values: |
| 44 | * freq |
| 45 | * clock divider for CORE, COREM0, COREM1, PERIPH, ATB, PCLK_DBG, APLL, RESERVED |
| 46 | * clock divider for COPY, HPM, RESERVED |
| 47 | * PLL M, P, S |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 48 | */ |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 49 | APLL_FREQ(1200, 0, 3, 7, 3, 4, 1, 7, 0, 5, 0, 0, 150, 3, 1), |
| 50 | APLL_FREQ(1000, 0, 3, 7, 3, 4, 1, 7, 0, 4, 0, 0, 250, 6, 1), |
| 51 | APLL_FREQ(800, 0, 3, 7, 3, 3, 1, 7, 0, 3, 0, 0, 200, 6, 1), |
| 52 | APLL_FREQ(500, 0, 3, 7, 3, 3, 1, 7, 0, 3, 0, 0, 250, 6, 2), |
| 53 | APLL_FREQ(200, 0, 1, 3, 1, 3, 1, 0, 0, 3, 0, 0, 200, 6, 3), |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 54 | }; |
| 55 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 56 | static void exynos4210_set_clkdiv(unsigned int div_index) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 57 | { |
| 58 | unsigned int tmp; |
| 59 | |
| 60 | /* Change Divider - CPU0 */ |
| 61 | |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 62 | tmp = apll_freq_4210[div_index].clk_div_cpu0; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 63 | |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 64 | __raw_writel(tmp, EXYNOS4_CLKDIV_CPU); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 65 | |
| 66 | do { |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 67 | tmp = __raw_readl(EXYNOS4_CLKDIV_STATCPU); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 68 | } while (tmp & 0x1111111); |
| 69 | |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 70 | /* Change Divider - CPU1 */ |
| 71 | |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 72 | tmp = apll_freq_4210[div_index].clk_div_cpu1; |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 73 | |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 74 | __raw_writel(tmp, EXYNOS4_CLKDIV_CPU1); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 75 | |
| 76 | do { |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 77 | tmp = __raw_readl(EXYNOS4_CLKDIV_STATCPU1); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 78 | } while (tmp & 0x11); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 79 | } |
| 80 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 81 | static void exynos4210_set_apll(unsigned int index) |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 82 | { |
| 83 | unsigned int tmp; |
| 84 | |
| 85 | /* 1. MUX_CORE_SEL = MPLL, ARMCLK uses MPLL for lock time */ |
| 86 | clk_set_parent(moutcore, mout_mpll); |
| 87 | |
| 88 | do { |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 89 | tmp = (__raw_readl(EXYNOS4_CLKMUX_STATCPU) |
| 90 | >> EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 91 | tmp &= 0x7; |
| 92 | } while (tmp != 0x2); |
| 93 | |
| 94 | /* 2. Set APLL Lock time */ |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 95 | __raw_writel(EXYNOS4_APLL_LOCKTIME, EXYNOS4_APLL_LOCK); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 96 | |
| 97 | /* 3. Change PLL PMS values */ |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 98 | tmp = __raw_readl(EXYNOS4_APLL_CON0); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 99 | tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0)); |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 100 | tmp |= apll_freq_4210[index].mps; |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 101 | __raw_writel(tmp, EXYNOS4_APLL_CON0); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 102 | |
| 103 | /* 4. wait_lock_time */ |
| 104 | do { |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 105 | tmp = __raw_readl(EXYNOS4_APLL_CON0); |
| 106 | } while (!(tmp & (0x1 << EXYNOS4_APLLCON0_LOCKED_SHIFT))); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 107 | |
| 108 | /* 5. MUX_CORE_SEL = APLL */ |
| 109 | clk_set_parent(moutcore, mout_apll); |
| 110 | |
| 111 | do { |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 112 | tmp = __raw_readl(EXYNOS4_CLKMUX_STATCPU); |
| 113 | tmp &= EXYNOS4_CLKMUX_STATCPU_MUXCORE_MASK; |
| 114 | } while (tmp != (0x1 << EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT)); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Jonghwan Choi | 94aa440 | 2012-12-23 15:59:06 -0800 | [diff] [blame] | 117 | static bool exynos4210_pms_change(unsigned int old_index, unsigned int new_index) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 118 | { |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 119 | unsigned int old_pm = apll_freq_4210[old_index].mps >> 8; |
| 120 | unsigned int new_pm = apll_freq_4210[new_index].mps >> 8; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 121 | |
| 122 | return (old_pm == new_pm) ? 0 : 1; |
| 123 | } |
| 124 | |
| 125 | static void exynos4210_set_frequency(unsigned int old_index, |
| 126 | unsigned int new_index) |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 127 | { |
| 128 | unsigned int tmp; |
| 129 | |
| 130 | if (old_index > new_index) { |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 131 | if (!exynos4210_pms_change(old_index, new_index)) { |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 132 | /* 1. Change the system clock divider values */ |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 133 | exynos4210_set_clkdiv(new_index); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 134 | |
| 135 | /* 2. Change just s value in apll m,p,s value */ |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 136 | tmp = __raw_readl(EXYNOS4_APLL_CON0); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 137 | tmp &= ~(0x7 << 0); |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 138 | tmp |= apll_freq_4210[new_index].mps & 0x7; |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 139 | __raw_writel(tmp, EXYNOS4_APLL_CON0); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 140 | } else { |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame] | 141 | /* Clock Configuration Procedure */ |
| 142 | /* 1. Change the system clock divider values */ |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 143 | exynos4210_set_clkdiv(new_index); |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame] | 144 | /* 2. Change the apll m,p,s value */ |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 145 | exynos4210_set_apll(new_index); |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame] | 146 | } |
| 147 | } else if (old_index < new_index) { |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 148 | if (!exynos4210_pms_change(old_index, new_index)) { |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 149 | /* 1. Change just s value in apll m,p,s value */ |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 150 | tmp = __raw_readl(EXYNOS4_APLL_CON0); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 151 | tmp &= ~(0x7 << 0); |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 152 | tmp |= apll_freq_4210[new_index].mps & 0x7; |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 153 | __raw_writel(tmp, EXYNOS4_APLL_CON0); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 154 | |
| 155 | /* 2. Change the system clock divider values */ |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 156 | exynos4210_set_clkdiv(new_index); |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame] | 157 | } else { |
| 158 | /* Clock Configuration Procedure */ |
| 159 | /* 1. Change the apll m,p,s value */ |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 160 | exynos4210_set_apll(new_index); |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame] | 161 | /* 2. Change the system clock divider values */ |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 162 | exynos4210_set_clkdiv(new_index); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 167 | int exynos4210_cpufreq_init(struct exynos_dvfs_info *info) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 168 | { |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 169 | unsigned long rate; |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame] | 170 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 171 | cpu_clk = clk_get(NULL, "armclk"); |
| 172 | if (IS_ERR(cpu_clk)) |
| 173 | return PTR_ERR(cpu_clk); |
| 174 | |
| 175 | moutcore = clk_get(NULL, "moutcore"); |
| 176 | if (IS_ERR(moutcore)) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 177 | goto err_moutcore; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 178 | |
| 179 | mout_mpll = clk_get(NULL, "mout_mpll"); |
| 180 | if (IS_ERR(mout_mpll)) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 181 | goto err_mout_mpll; |
| 182 | |
| 183 | rate = clk_get_rate(mout_mpll) / 1000; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 184 | |
| 185 | mout_apll = clk_get(NULL, "mout_apll"); |
| 186 | if (IS_ERR(mout_apll)) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 187 | goto err_mout_apll; |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 188 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 189 | info->mpll_freq_khz = rate; |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 190 | /* 800Mhz */ |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 191 | info->pll_safe_idx = L2; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 192 | info->cpu_clk = cpu_clk; |
| 193 | info->volt_table = exynos4210_volt_table; |
| 194 | info->freq_table = exynos4210_freq_table; |
| 195 | info->set_freq = exynos4210_set_frequency; |
| 196 | info->need_apll_change = exynos4210_pms_change; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 197 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 198 | return 0; |
| 199 | |
| 200 | err_mout_apll: |
Jonghwan Choi | 184cddd | 2012-12-23 15:51:40 -0800 | [diff] [blame] | 201 | clk_put(mout_mpll); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 202 | err_mout_mpll: |
Jonghwan Choi | 184cddd | 2012-12-23 15:51:40 -0800 | [diff] [blame] | 203 | clk_put(moutcore); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 204 | err_moutcore: |
Jonghwan Choi | 184cddd | 2012-12-23 15:51:40 -0800 | [diff] [blame] | 205 | clk_put(cpu_clk); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 206 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 207 | pr_debug("%s: failed initialization\n", __func__); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 208 | return -EINVAL; |
| 209 | } |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 210 | EXPORT_SYMBOL(exynos4210_cpufreq_init); |