blob: 250dcf0c0b228fa9b17d8fa8c18dedc5e853f9f0 [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
Jaecheol Lee6c523c62012-01-07 20:18:39 +090012#include <linux/module.h>
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090013#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 Kangf40f91f2010-09-16 17:59:21 +090018#include <linux/cpufreq.h>
19
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090020#include <mach/regs-clock.h>
Jaecheol Leea125a172012-01-07 20:18:35 +090021#include <mach/cpufreq.h>
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090022
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090023static struct clk *cpu_clk;
24static struct clk *moutcore;
25static struct clk *mout_mpll;
26static struct clk *mout_apll;
27
Jonghwan Choi9d0554f2012-12-23 15:57:42 -080028static unsigned int exynos4210_volt_table[] = {
Jaecheol Leea125a172012-01-07 20:18:35 +090029 1250000, 1150000, 1050000, 975000, 950000,
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090030};
31
Jaecheol Leea125a172012-01-07 20:18:35 +090032static struct cpufreq_frequency_table exynos4210_freq_table[] = {
Jonghwan Choi9d0554f2012-12-23 15:57:42 -080033 {L0, 1200 * 1000},
34 {L1, 1000 * 1000},
35 {L2, 800 * 1000},
36 {L3, 500 * 1000},
37 {L4, 200 * 1000},
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090038 {0, CPUFREQ_TABLE_END},
39};
40
Jonghwan Choi9d0554f2012-12-23 15:57:42 -080041static struct apll_freq apll_freq_4210[] = {
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090042 /*
Jonghwan Choi9d0554f2012-12-23 15:57:42 -080043 * 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 Kangf40f91f2010-09-16 17:59:21 +090048 */
Jonghwan Choi9d0554f2012-12-23 15:57:42 -080049 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 Jubf5ce052010-12-22 16:49:32 +090054};
55
Jaecheol Leea125a172012-01-07 20:18:35 +090056static void exynos4210_set_clkdiv(unsigned int div_index)
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090057{
58 unsigned int tmp;
59
60 /* Change Divider - CPU0 */
61
Jonghwan Choi9d0554f2012-12-23 15:57:42 -080062 tmp = apll_freq_4210[div_index].clk_div_cpu0;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090063
Kukjin Kim09cee1a2012-01-31 13:49:24 +090064 __raw_writel(tmp, EXYNOS4_CLKDIV_CPU);
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090065
66 do {
Kukjin Kim09cee1a2012-01-31 13:49:24 +090067 tmp = __raw_readl(EXYNOS4_CLKDIV_STATCPU);
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090068 } while (tmp & 0x1111111);
69
Sangwook Jubf5ce052010-12-22 16:49:32 +090070 /* Change Divider - CPU1 */
71
Jonghwan Choi9d0554f2012-12-23 15:57:42 -080072 tmp = apll_freq_4210[div_index].clk_div_cpu1;
Sangwook Jubf5ce052010-12-22 16:49:32 +090073
Kukjin Kim09cee1a2012-01-31 13:49:24 +090074 __raw_writel(tmp, EXYNOS4_CLKDIV_CPU1);
Sangwook Jubf5ce052010-12-22 16:49:32 +090075
76 do {
Kukjin Kim09cee1a2012-01-31 13:49:24 +090077 tmp = __raw_readl(EXYNOS4_CLKDIV_STATCPU1);
Sangwook Jubf5ce052010-12-22 16:49:32 +090078 } while (tmp & 0x11);
Sunyoung Kangf40f91f2010-09-16 17:59:21 +090079}
80
Jaecheol Leea125a172012-01-07 20:18:35 +090081static void exynos4210_set_apll(unsigned int index)
Sangwook Jubf5ce052010-12-22 16:49:32 +090082{
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 Kim09cee1a2012-01-31 13:49:24 +090089 tmp = (__raw_readl(EXYNOS4_CLKMUX_STATCPU)
90 >> EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT);
Sangwook Jubf5ce052010-12-22 16:49:32 +090091 tmp &= 0x7;
92 } while (tmp != 0x2);
93
94 /* 2. Set APLL Lock time */
Kukjin Kim09cee1a2012-01-31 13:49:24 +090095 __raw_writel(EXYNOS4_APLL_LOCKTIME, EXYNOS4_APLL_LOCK);
Sangwook Jubf5ce052010-12-22 16:49:32 +090096
97 /* 3. Change PLL PMS values */
Kukjin Kim09cee1a2012-01-31 13:49:24 +090098 tmp = __raw_readl(EXYNOS4_APLL_CON0);
Sangwook Jubf5ce052010-12-22 16:49:32 +090099 tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0));
Jonghwan Choi9d0554f2012-12-23 15:57:42 -0800100 tmp |= apll_freq_4210[index].mps;
Kukjin Kim09cee1a2012-01-31 13:49:24 +0900101 __raw_writel(tmp, EXYNOS4_APLL_CON0);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900102
103 /* 4. wait_lock_time */
104 do {
Kukjin Kim09cee1a2012-01-31 13:49:24 +0900105 tmp = __raw_readl(EXYNOS4_APLL_CON0);
106 } while (!(tmp & (0x1 << EXYNOS4_APLLCON0_LOCKED_SHIFT)));
Sangwook Jubf5ce052010-12-22 16:49:32 +0900107
108 /* 5. MUX_CORE_SEL = APLL */
109 clk_set_parent(moutcore, mout_apll);
110
111 do {
Kukjin Kim09cee1a2012-01-31 13:49:24 +0900112 tmp = __raw_readl(EXYNOS4_CLKMUX_STATCPU);
113 tmp &= EXYNOS4_CLKMUX_STATCPU_MUXCORE_MASK;
114 } while (tmp != (0x1 << EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT));
Sangwook Jubf5ce052010-12-22 16:49:32 +0900115}
116
Jaecheol Leea125a172012-01-07 20:18:35 +0900117bool exynos4210_pms_change(unsigned int old_index, unsigned int new_index)
118{
Jonghwan Choi9d0554f2012-12-23 15:57:42 -0800119 unsigned int old_pm = apll_freq_4210[old_index].mps >> 8;
120 unsigned int new_pm = apll_freq_4210[new_index].mps >> 8;
Jaecheol Leea125a172012-01-07 20:18:35 +0900121
122 return (old_pm == new_pm) ? 0 : 1;
123}
124
125static void exynos4210_set_frequency(unsigned int old_index,
126 unsigned int new_index)
Sangwook Jubf5ce052010-12-22 16:49:32 +0900127{
128 unsigned int tmp;
129
130 if (old_index > new_index) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900131 if (!exynos4210_pms_change(old_index, new_index)) {
Sangwook Jubf5ce052010-12-22 16:49:32 +0900132 /* 1. Change the system clock divider values */
Jaecheol Leea125a172012-01-07 20:18:35 +0900133 exynos4210_set_clkdiv(new_index);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900134
135 /* 2. Change just s value in apll m,p,s value */
Kukjin Kim09cee1a2012-01-31 13:49:24 +0900136 tmp = __raw_readl(EXYNOS4_APLL_CON0);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900137 tmp &= ~(0x7 << 0);
Jonghwan Choi9d0554f2012-12-23 15:57:42 -0800138 tmp |= apll_freq_4210[new_index].mps & 0x7;
Kukjin Kim09cee1a2012-01-31 13:49:24 +0900139 __raw_writel(tmp, EXYNOS4_APLL_CON0);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900140 } else {
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900141 /* Clock Configuration Procedure */
142 /* 1. Change the system clock divider values */
Jaecheol Leea125a172012-01-07 20:18:35 +0900143 exynos4210_set_clkdiv(new_index);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900144 /* 2. Change the apll m,p,s value */
Jaecheol Leea125a172012-01-07 20:18:35 +0900145 exynos4210_set_apll(new_index);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900146 }
147 } else if (old_index < new_index) {
Jaecheol Leea125a172012-01-07 20:18:35 +0900148 if (!exynos4210_pms_change(old_index, new_index)) {
Sangwook Jubf5ce052010-12-22 16:49:32 +0900149 /* 1. Change just s value in apll m,p,s value */
Kukjin Kim09cee1a2012-01-31 13:49:24 +0900150 tmp = __raw_readl(EXYNOS4_APLL_CON0);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900151 tmp &= ~(0x7 << 0);
Jonghwan Choi9d0554f2012-12-23 15:57:42 -0800152 tmp |= apll_freq_4210[new_index].mps & 0x7;
Kukjin Kim09cee1a2012-01-31 13:49:24 +0900153 __raw_writel(tmp, EXYNOS4_APLL_CON0);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900154
155 /* 2. Change the system clock divider values */
Jaecheol Leea125a172012-01-07 20:18:35 +0900156 exynos4210_set_clkdiv(new_index);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900157 } else {
158 /* Clock Configuration Procedure */
159 /* 1. Change the apll m,p,s value */
Jaecheol Leea125a172012-01-07 20:18:35 +0900160 exynos4210_set_apll(new_index);
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900161 /* 2. Change the system clock divider values */
Jaecheol Leea125a172012-01-07 20:18:35 +0900162 exynos4210_set_clkdiv(new_index);
Sangwook Jubf5ce052010-12-22 16:49:32 +0900163 }
164 }
165}
166
Jaecheol Leea125a172012-01-07 20:18:35 +0900167int exynos4210_cpufreq_init(struct exynos_dvfs_info *info)
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900168{
Jaecheol Leea125a172012-01-07 20:18:35 +0900169 unsigned long rate;
Jaecheol Lee27f805d2011-12-07 11:44:09 +0900170
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900171 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 Leea125a172012-01-07 20:18:35 +0900177 goto err_moutcore;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900178
179 mout_mpll = clk_get(NULL, "mout_mpll");
180 if (IS_ERR(mout_mpll))
Jaecheol Leea125a172012-01-07 20:18:35 +0900181 goto err_mout_mpll;
182
183 rate = clk_get_rate(mout_mpll) / 1000;
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900184
185 mout_apll = clk_get(NULL, "mout_apll");
186 if (IS_ERR(mout_apll))
Jaecheol Leea125a172012-01-07 20:18:35 +0900187 goto err_mout_apll;
MyungJoo Ham0073f532011-08-18 19:45:16 +0900188
Jaecheol Leea125a172012-01-07 20:18:35 +0900189 info->mpll_freq_khz = rate;
Jonghwan Choi9d0554f2012-12-23 15:57:42 -0800190 /* 800Mhz */
Jaecheol Leea125a172012-01-07 20:18:35 +0900191 info->pll_safe_idx = L2;
Jaecheol Leea125a172012-01-07 20:18:35 +0900192 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 Kangf40f91f2010-09-16 17:59:21 +0900197
Jaecheol Leea125a172012-01-07 20:18:35 +0900198 return 0;
199
200err_mout_apll:
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800201 clk_put(mout_mpll);
Jaecheol Leea125a172012-01-07 20:18:35 +0900202err_mout_mpll:
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800203 clk_put(moutcore);
Jaecheol Leea125a172012-01-07 20:18:35 +0900204err_moutcore:
Jonghwan Choi184cddd2012-12-23 15:51:40 -0800205 clk_put(cpu_clk);
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900206
Jaecheol Leea125a172012-01-07 20:18:35 +0900207 pr_debug("%s: failed initialization\n", __func__);
Sunyoung Kangf40f91f2010-09-16 17:59:21 +0900208 return -EINVAL;
209}
Jaecheol Leea125a172012-01-07 20:18:35 +0900210EXPORT_SYMBOL(exynos4210_cpufreq_init);