blob: 6bc4ada56df1dfbf5b14e17e899c7f46e726b477 [file] [log] [blame]
Kukjin Kimf7d77072011-06-01 14:18:22 -07001/*
Kukjin Kim7d30e8b2011-02-14 16:33:10 +09002 * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
Sunyoung Kangf40f91f2010-09-16 17:59:21 +09003 * http://www.samsung.com
4 *
Jaecheol Leea125a172012-01-07 20:18:35 +09005 * EXYNOS4210 - CPU frequency scaling support
Sunyoung Kangf40f91f2010-09-16 17:59:21 +09006 *
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
12#include <linux/types.h>
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>
18#include <linux/regulator/consumer.h>
19#include <linux/cpufreq.h>
MyungJoo Ham0073f532011-08-18 19:45:16 +090020#include <linux/notifier.h>
21#include <linux/suspend.h>
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090022
23#include <mach/map.h>
24#include <mach/regs-clock.h>
25#include <mach/regs-mem.h>
Jaecheol Leea125a172012-01-07 20:18:35 +090026#include <mach/cpufreq.h>
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090027
28#include <plat/clock.h>
Sangwook Jubf5ce052010-12-22 16:49:32 +090029#include <plat/pm.h>
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090030
Jaecheol Leea125a172012-01-07 20:18:35 +090031#define CPUFREQ_LEVEL_END L5
32
33static int max_support_idx = L0;
34static int min_support_idx = (CPUFREQ_LEVEL_END - 1);
35
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090036static struct clk *cpu_clk;
37static struct clk *moutcore;
38static struct clk *mout_mpll;
39static struct clk *mout_apll;
40
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090041static struct regulator *arm_regulator;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090042
43static struct cpufreq_freqs freqs;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090044
Jaecheol Lee27f805d2011-12-07 11:44:09 +090045struct cpufreq_clkdiv {
Jaecheol Leea125a172012-01-07 20:18:35 +090046 unsigned int index;
Jaecheol Lee27f805d2011-12-07 11:44:09 +090047 unsigned int clkdiv;
48};
49
Jaecheol Leea125a172012-01-07 20:18:35 +090050static unsigned int exynos4210_volt_table[CPUFREQ_LEVEL_END] = {
51 1250000, 1150000, 1050000, 975000, 950000,
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090052};
53
Jaecheol Lee27f805d2011-12-07 11:44:09 +090054
Jaecheol Leea125a172012-01-07 20:18:35 +090055static struct cpufreq_clkdiv exynos4210_clkdiv_table[CPUFREQ_LEVEL_END];
56
57static struct cpufreq_frequency_table exynos4210_freq_table[] = {
Jaecheol Leeba9d7802011-12-07 11:43:56 +090058 {L0, 1200*1000},
59 {L1, 1000*1000},
60 {L2, 800*1000},
61 {L3, 500*1000},
62 {L4, 200*1000},
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090063 {0, CPUFREQ_TABLE_END},
64};
65
Sangwook Jubf5ce052010-12-22 16:49:32 +090066static unsigned int clkdiv_cpu0[CPUFREQ_LEVEL_END][7] = {
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090067 /*
68 * Clock divider value for following
69 * { DIVCORE, DIVCOREM0, DIVCOREM1, DIVPERIPH,
70 * DIVATB, DIVPCLK_DBG, DIVAPLL }
71 */
72
Jaecheol Leeba9d7802011-12-07 11:43:56 +090073 /* ARM L0: 1200MHz */
74 { 0, 3, 7, 3, 4, 1, 7 },
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090075
Jaecheol Leeba9d7802011-12-07 11:43:56 +090076 /* ARM L1: 1000MHz */
77 { 0, 3, 7, 3, 4, 1, 7 },
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090078
Jaecheol Leeba9d7802011-12-07 11:43:56 +090079 /* ARM L2: 800MHz */
80 { 0, 3, 7, 3, 3, 1, 7 },
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090081
Jaecheol Leeba9d7802011-12-07 11:43:56 +090082 /* ARM L3: 500MHz */
83 { 0, 3, 7, 3, 3, 1, 7 },
84
85 /* ARM L4: 200MHz */
86 { 0, 1, 3, 1, 3, 1, 0 },
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090087};
88
Sangwook Jubf5ce052010-12-22 16:49:32 +090089static unsigned int clkdiv_cpu1[CPUFREQ_LEVEL_END][2] = {
90 /*
91 * Clock divider value for following
92 * { DIVCOPY, DIVHPM }
93 */
94
Jaecheol Leeba9d7802011-12-07 11:43:56 +090095 /* ARM L0: 1200MHz */
96 { 5, 0 },
97
98 /* ARM L1: 1000MHz */
99 { 4, 0 },
100
101 /* ARM L2: 800MHz */
Sangwook Jubf5ce052010-12-22 16:49:32 +0900102 { 3, 0 },
103
Jaecheol Leeba9d7802011-12-07 11:43:56 +0900104 /* ARM L3: 500MHz */
Sangwook Jubf5ce052010-12-22 16:49:32 +0900105 { 3, 0 },
106
Jaecheol Leeba9d7802011-12-07 11:43:56 +0900107 /* ARM L4: 200MHz */
Sangwook Jubf5ce052010-12-22 16:49:32 +0900108 { 3, 0 },
109};
110
Jaecheol Leea125a172012-01-07 20:18:35 +0900111static unsigned int exynos4210_apll_pms_table[CPUFREQ_LEVEL_END] = {
Jaecheol Leeba9d7802011-12-07 11:43:56 +0900112 /* APLL FOUT L0: 1200MHz */
113 ((150 << 16) | (3 << 8) | 1),
114
115 /* APLL FOUT L1: 1000MHz */
Sangwook Jubf5ce052010-12-22 16:49:32 +0900116 ((250 << 16) | (6 << 8) | 1),
117
Jaecheol Leeba9d7802011-12-07 11:43:56 +0900118 /* APLL FOUT L2: 800MHz */
Sangwook Jubf5ce052010-12-22 16:49:32 +0900119 ((200 << 16) | (6 << 8) | 1),
120
Jaecheol Leeba9d7802011-12-07 11:43:56 +0900121 /* APLL FOUT L3: 500MHz */
122 ((250 << 16) | (6 << 8) | 2),
Sangwook Jubf5ce052010-12-22 16:49:32 +0900123
Jaecheol Leeba9d7802011-12-07 11:43:56 +0900124 /* APLL FOUT L4: 200MHz */
125 ((200 << 16) | (6 << 8) | 3),
Sangwook Jubf5ce052010-12-22 16:49:32 +0900126};
127
Jaecheol Leea125a172012-01-07 20:18:35 +0900128static void exynos4210_set_clkdiv(unsigned int div_index)
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900129{
130 unsigned int tmp;
131
132 /* Change Divider - CPU0 */
133
Jaecheol Leea125a172012-01-07 20:18:35 +0900134 tmp = exynos4210_clkdiv_table[div_index].clkdiv;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900135
136 __raw_writel(tmp, S5P_CLKDIV_CPU);
137
138 do {
139 tmp = __raw_readl(S5P_CLKDIV_STATCPU);
140 } while (tmp & 0x1111111);
141
Sangwook Jubf5ce052010-12-22 16:49:32 +0900142 /* Change Divider - CPU1 */
143
144 tmp = __raw_readl(S5P_CLKDIV_CPU1);
145
146 tmp &= ~((0x7 << 4) | 0x7);
147
148 tmp |= ((clkdiv_cpu1[div_index][0] << 4) |
149 (clkdiv_cpu1[div_index][1] << 0));
150
151 __raw_writel(tmp, S5P_CLKDIV_CPU1);
152
153 do {
154 tmp = __raw_readl(S5P_CLKDIV_STATCPU1);
155 } while (tmp & 0x11);
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900156}
157
Jaecheol Leea125a172012-01-07 20:18:35 +0900158static void exynos4210_set_apll(unsigned int index)
Sangwook Jubf5ce052010-12-22 16:49:32 +0900159{
160 unsigned int tmp;
161
162 /* 1. MUX_CORE_SEL = MPLL, ARMCLK uses MPLL for lock time */
163 clk_set_parent(moutcore, mout_mpll);
164
165 do {
166 tmp = (__raw_readl(S5P_CLKMUX_STATCPU)
167 >> S5P_CLKSRC_CPU_MUXCORE_SHIFT);
168 tmp &= 0x7;
169 } while (tmp != 0x2);
170
171 /* 2. Set APLL Lock time */
172 __raw_writel(S5P_APLL_LOCKTIME, S5P_APLL_LOCK);
173
174 /* 3. Change PLL PMS values */
175 tmp = __raw_readl(S5P_APLL_CON0);
176 tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0));
Jaecheol Leea125a172012-01-07 20:18:35 +0900177 tmp |= exynos4210_apll_pms_table[index];
Sangwook Jubf5ce052010-12-22 16:49:32 +0900178 __raw_writel(tmp, S5P_APLL_CON0);
179
180 /* 4. wait_lock_time */
181 do {
182 tmp = __raw_readl(S5P_APLL_CON0);
183 } while (!(tmp & (0x1 << S5P_APLLCON0_LOCKED_SHIFT)));
184
185 /* 5. MUX_CORE_SEL = APLL */
186 clk_set_parent(moutcore, mout_apll);
187
188 do {
189 tmp = __raw_readl(S5P_CLKMUX_STATCPU);
190 tmp &= S5P_CLKMUX_STATCPU_MUXCORE_MASK;
191 } while (tmp != (0x1 << S5P_CLKSRC_CPU_MUXCORE_SHIFT));
192}
193
Jaecheol Leea125a172012-01-07 20:18:35 +0900194bool exynos4210_pms_change(unsigned int old_index, unsigned int new_index)
195{
196 unsigned int old_pm = (exynos4210_apll_pms_table[old_index] >> 8);
197 unsigned int new_pm = (exynos4210_apll_pms_table[new_index] >> 8);
198
199 return (old_pm == new_pm) ? 0 : 1;
200}
201
202static void exynos4210_set_frequency(unsigned int old_index,
203 unsigned int new_index)
Sangwook Jubf5ce052010-12-22 16:49:32 +0900204{
205 unsigned int tmp;
206
207 if (old_index > new_index) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900208 if (!exynos4210_pms_change(old_index, new_index)) {
Sangwook Jubf5ce052010-12-22 16:49:32 +0900209 /* 1. Change the system clock divider values */
Jaecheol Leea125a172012-01-07 20:18:35 +0900210 exynos4210_set_clkdiv(new_index);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900211
212 /* 2. Change just s value in apll m,p,s value */
213 tmp = __raw_readl(S5P_APLL_CON0);
214 tmp &= ~(0x7 << 0);
Jaecheol Leea125a172012-01-07 20:18:35 +0900215 tmp |= (exynos4210_apll_pms_table[new_index] & 0x7);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900216 __raw_writel(tmp, S5P_APLL_CON0);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900217 } else {
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900218 /* Clock Configuration Procedure */
219 /* 1. Change the system clock divider values */
Jaecheol Leea125a172012-01-07 20:18:35 +0900220 exynos4210_set_clkdiv(new_index);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900221 /* 2. Change the apll m,p,s value */
Jaecheol Leea125a172012-01-07 20:18:35 +0900222 exynos4210_set_apll(new_index);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900223 }
224 } else if (old_index < new_index) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900225 if (!exynos4210_pms_change(old_index, new_index)) {
Sangwook Jubf5ce052010-12-22 16:49:32 +0900226 /* 1. Change just s value in apll m,p,s value */
227 tmp = __raw_readl(S5P_APLL_CON0);
228 tmp &= ~(0x7 << 0);
Jaecheol Leea125a172012-01-07 20:18:35 +0900229 tmp |= (exynos4210_apll_pms_table[new_index] & 0x7);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900230 __raw_writel(tmp, S5P_APLL_CON0);
231
232 /* 2. Change the system clock divider values */
Jaecheol Leea125a172012-01-07 20:18:35 +0900233 exynos4210_set_clkdiv(new_index);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900234 } else {
235 /* Clock Configuration Procedure */
236 /* 1. Change the apll m,p,s value */
Jaecheol Leea125a172012-01-07 20:18:35 +0900237 exynos4210_set_apll(new_index);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900238 /* 2. Change the system clock divider values */
Jaecheol Leea125a172012-01-07 20:18:35 +0900239 exynos4210_set_clkdiv(new_index);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900240 }
241 }
242}
243
Jaecheol Leea125a172012-01-07 20:18:35 +0900244int exynos4210_cpufreq_init(struct exynos_dvfs_info *info)
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900245{
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900246 int i;
247 unsigned int tmp;
Jaecheol Leea125a172012-01-07 20:18:35 +0900248 unsigned long rate;
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900249
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900250 cpu_clk = clk_get(NULL, "armclk");
251 if (IS_ERR(cpu_clk))
252 return PTR_ERR(cpu_clk);
253
254 moutcore = clk_get(NULL, "moutcore");
255 if (IS_ERR(moutcore))
Jaecheol Leea125a172012-01-07 20:18:35 +0900256 goto err_moutcore;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900257
258 mout_mpll = clk_get(NULL, "mout_mpll");
259 if (IS_ERR(mout_mpll))
Jaecheol Leea125a172012-01-07 20:18:35 +0900260 goto err_mout_mpll;
261
262 rate = clk_get_rate(mout_mpll) / 1000;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900263
264 mout_apll = clk_get(NULL, "mout_apll");
265 if (IS_ERR(mout_apll))
Jaecheol Leea125a172012-01-07 20:18:35 +0900266 goto err_mout_apll;
MyungJoo Ham0073f532011-08-18 19:45:16 +0900267
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900268 tmp = __raw_readl(S5P_CLKDIV_CPU);
269
270 for (i = L0; i < CPUFREQ_LEVEL_END; i++) {
271 tmp &= ~(S5P_CLKDIV_CPU0_CORE_MASK |
Jaecheol Leea125a172012-01-07 20:18:35 +0900272 S5P_CLKDIV_CPU0_COREM0_MASK |
273 S5P_CLKDIV_CPU0_COREM1_MASK |
274 S5P_CLKDIV_CPU0_PERIPH_MASK |
275 S5P_CLKDIV_CPU0_ATB_MASK |
276 S5P_CLKDIV_CPU0_PCLKDBG_MASK |
277 S5P_CLKDIV_CPU0_APLL_MASK);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900278
279 tmp |= ((clkdiv_cpu0[i][0] << S5P_CLKDIV_CPU0_CORE_SHIFT) |
280 (clkdiv_cpu0[i][1] << S5P_CLKDIV_CPU0_COREM0_SHIFT) |
281 (clkdiv_cpu0[i][2] << S5P_CLKDIV_CPU0_COREM1_SHIFT) |
282 (clkdiv_cpu0[i][3] << S5P_CLKDIV_CPU0_PERIPH_SHIFT) |
283 (clkdiv_cpu0[i][4] << S5P_CLKDIV_CPU0_ATB_SHIFT) |
284 (clkdiv_cpu0[i][5] << S5P_CLKDIV_CPU0_PCLKDBG_SHIFT) |
285 (clkdiv_cpu0[i][6] << S5P_CLKDIV_CPU0_APLL_SHIFT));
286
Jaecheol Leea125a172012-01-07 20:18:35 +0900287 exynos4210_clkdiv_table[i].clkdiv = tmp;
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900288 }
289
Jaecheol Leea125a172012-01-07 20:18:35 +0900290 info->mpll_freq_khz = rate;
291 info->pm_lock_idx = L2;
292 info->pll_safe_idx = L2;
293 info->max_support_idx = max_support_idx;
294 info->min_support_idx = min_support_idx;
295 info->cpu_clk = cpu_clk;
296 info->volt_table = exynos4210_volt_table;
297 info->freq_table = exynos4210_freq_table;
298 info->set_freq = exynos4210_set_frequency;
299 info->need_apll_change = exynos4210_pms_change;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900300
Jaecheol Leea125a172012-01-07 20:18:35 +0900301 return 0;
302
303err_mout_apll:
304 if (!IS_ERR(mout_mpll))
305 clk_put(mout_mpll);
306err_mout_mpll:
307 if (!IS_ERR(moutcore))
308 clk_put(moutcore);
309err_moutcore:
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900310 if (!IS_ERR(cpu_clk))
311 clk_put(cpu_clk);
312
Jaecheol Leea125a172012-01-07 20:18:35 +0900313 pr_debug("%s: failed initialization\n", __func__);
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900314 return -EINVAL;
315}
Jaecheol Leea125a172012-01-07 20:18:35 +0900316EXPORT_SYMBOL(exynos4210_cpufreq_init);