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> |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 19 | #include <linux/of.h> |
| 20 | #include <linux/of_address.h> |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 21 | |
Kukjin Kim | c4aaa29 | 2012-12-28 16:29:10 -0800 | [diff] [blame] | 22 | #include "exynos-cpufreq.h" |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 23 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 24 | static struct clk *cpu_clk; |
| 25 | static struct clk *moutcore; |
| 26 | static struct clk *mout_mpll; |
| 27 | static struct clk *mout_apll; |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 28 | static struct exynos_dvfs_info *cpufreq; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 29 | |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 30 | static unsigned int exynos4210_volt_table[] = { |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 31 | 1250000, 1150000, 1050000, 975000, 950000, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 32 | }; |
| 33 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 34 | static struct cpufreq_frequency_table exynos4210_freq_table[] = { |
Viresh Kumar | 7f4b046 | 2014-03-28 19:11:47 +0530 | [diff] [blame] | 35 | {0, L0, 1200 * 1000}, |
| 36 | {0, L1, 1000 * 1000}, |
| 37 | {0, L2, 800 * 1000}, |
| 38 | {0, L3, 500 * 1000}, |
| 39 | {0, L4, 200 * 1000}, |
| 40 | {0, 0, CPUFREQ_TABLE_END}, |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 41 | }; |
| 42 | |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 43 | static struct apll_freq apll_freq_4210[] = { |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 44 | /* |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 45 | * values: |
| 46 | * freq |
| 47 | * clock divider for CORE, COREM0, COREM1, PERIPH, ATB, PCLK_DBG, APLL, RESERVED |
| 48 | * clock divider for COPY, HPM, RESERVED |
| 49 | * PLL M, P, S |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 50 | */ |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 51 | APLL_FREQ(1200, 0, 3, 7, 3, 4, 1, 7, 0, 5, 0, 0, 150, 3, 1), |
| 52 | APLL_FREQ(1000, 0, 3, 7, 3, 4, 1, 7, 0, 4, 0, 0, 250, 6, 1), |
| 53 | APLL_FREQ(800, 0, 3, 7, 3, 3, 1, 7, 0, 3, 0, 0, 200, 6, 1), |
| 54 | APLL_FREQ(500, 0, 3, 7, 3, 3, 1, 7, 0, 3, 0, 0, 250, 6, 2), |
| 55 | 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] | 56 | }; |
| 57 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 58 | static void exynos4210_set_clkdiv(unsigned int div_index) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 59 | { |
| 60 | unsigned int tmp; |
| 61 | |
| 62 | /* Change Divider - CPU0 */ |
| 63 | |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 64 | tmp = apll_freq_4210[div_index].clk_div_cpu0; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 65 | |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 66 | __raw_writel(tmp, cpufreq->cmu_regs + EXYNOS4_CLKDIV_CPU); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 67 | |
| 68 | do { |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 69 | tmp = __raw_readl(cpufreq->cmu_regs + EXYNOS4_CLKDIV_STATCPU); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 70 | } while (tmp & 0x1111111); |
| 71 | |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 72 | /* Change Divider - CPU1 */ |
| 73 | |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 74 | tmp = apll_freq_4210[div_index].clk_div_cpu1; |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 75 | |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 76 | __raw_writel(tmp, cpufreq->cmu_regs + EXYNOS4_CLKDIV_CPU1); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 77 | |
| 78 | do { |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 79 | tmp = __raw_readl(cpufreq->cmu_regs + EXYNOS4_CLKDIV_STATCPU1); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 80 | } while (tmp & 0x11); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 83 | static void exynos4210_set_apll(unsigned int index) |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 84 | { |
Lukasz Majewski | 7ad65d5 | 2013-10-09 14:08:43 +0200 | [diff] [blame] | 85 | unsigned int tmp, freq = apll_freq_4210[index].freq; |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 86 | |
Lukasz Majewski | 7ad65d5 | 2013-10-09 14:08:43 +0200 | [diff] [blame] | 87 | /* MUX_CORE_SEL = MPLL, ARMCLK uses MPLL for lock time */ |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 88 | clk_set_parent(moutcore, mout_mpll); |
| 89 | |
| 90 | do { |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 91 | tmp = (__raw_readl(cpufreq->cmu_regs + EXYNOS4_CLKMUX_STATCPU) |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 92 | >> EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 93 | tmp &= 0x7; |
| 94 | } while (tmp != 0x2); |
| 95 | |
Lukasz Majewski | 7ad65d5 | 2013-10-09 14:08:43 +0200 | [diff] [blame] | 96 | clk_set_rate(mout_apll, freq * 1000); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 97 | |
Lukasz Majewski | 7ad65d5 | 2013-10-09 14:08:43 +0200 | [diff] [blame] | 98 | /* MUX_CORE_SEL = APLL */ |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 99 | clk_set_parent(moutcore, mout_apll); |
| 100 | |
| 101 | do { |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 102 | tmp = __raw_readl(cpufreq->cmu_regs + EXYNOS4_CLKMUX_STATCPU); |
Kukjin Kim | 09cee1a | 2012-01-31 13:49:24 +0900 | [diff] [blame] | 103 | tmp &= EXYNOS4_CLKMUX_STATCPU_MUXCORE_MASK; |
| 104 | } while (tmp != (0x1 << EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT)); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 105 | } |
| 106 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 107 | static void exynos4210_set_frequency(unsigned int old_index, |
| 108 | unsigned int new_index) |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 109 | { |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 110 | if (old_index > new_index) { |
Lukasz Majewski | 7ad65d5 | 2013-10-09 14:08:43 +0200 | [diff] [blame] | 111 | exynos4210_set_clkdiv(new_index); |
| 112 | exynos4210_set_apll(new_index); |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame] | 113 | } else if (old_index < new_index) { |
Lukasz Majewski | 7ad65d5 | 2013-10-09 14:08:43 +0200 | [diff] [blame] | 114 | exynos4210_set_apll(new_index); |
| 115 | exynos4210_set_clkdiv(new_index); |
Sangwook Ju | bf5ce05 | 2010-12-22 16:49:32 +0900 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 119 | int exynos4210_cpufreq_init(struct exynos_dvfs_info *info) |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 120 | { |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 121 | struct device_node *np; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 122 | unsigned long rate; |
Jaecheol Lee | 27f805d | 2011-12-07 11:44:09 +0900 | [diff] [blame] | 123 | |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 124 | /* |
| 125 | * HACK: This is a temporary workaround to get access to clock |
| 126 | * controller registers directly and remove static mappings and |
| 127 | * dependencies on platform headers. It is necessary to enable |
| 128 | * Exynos multi-platform support and will be removed together with |
| 129 | * this whole driver as soon as Exynos gets migrated to use |
Viresh Kumar | bbcf071 | 2014-09-09 19:58:03 +0530 | [diff] [blame] | 130 | * cpufreq-dt driver. |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 131 | */ |
| 132 | np = of_find_compatible_node(NULL, NULL, "samsung,exynos4210-clock"); |
| 133 | if (!np) { |
| 134 | pr_err("%s: failed to find clock controller DT node\n", |
| 135 | __func__); |
| 136 | return -ENODEV; |
| 137 | } |
| 138 | |
| 139 | info->cmu_regs = of_iomap(np, 0); |
| 140 | if (!info->cmu_regs) { |
| 141 | pr_err("%s: failed to map CMU registers\n", __func__); |
| 142 | return -EFAULT; |
| 143 | } |
| 144 | |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 145 | cpu_clk = clk_get(NULL, "armclk"); |
| 146 | if (IS_ERR(cpu_clk)) |
| 147 | return PTR_ERR(cpu_clk); |
| 148 | |
| 149 | moutcore = clk_get(NULL, "moutcore"); |
| 150 | if (IS_ERR(moutcore)) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 151 | goto err_moutcore; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 152 | |
| 153 | mout_mpll = clk_get(NULL, "mout_mpll"); |
| 154 | if (IS_ERR(mout_mpll)) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 155 | goto err_mout_mpll; |
| 156 | |
| 157 | rate = clk_get_rate(mout_mpll) / 1000; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 158 | |
| 159 | mout_apll = clk_get(NULL, "mout_apll"); |
| 160 | if (IS_ERR(mout_apll)) |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 161 | goto err_mout_apll; |
MyungJoo Ham | 0073f53 | 2011-08-18 19:45:16 +0900 | [diff] [blame] | 162 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 163 | info->mpll_freq_khz = rate; |
Jonghwan Choi | 9d0554f | 2012-12-23 15:57:42 -0800 | [diff] [blame] | 164 | /* 800Mhz */ |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 165 | info->pll_safe_idx = L2; |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 166 | info->cpu_clk = cpu_clk; |
| 167 | info->volt_table = exynos4210_volt_table; |
| 168 | info->freq_table = exynos4210_freq_table; |
| 169 | info->set_freq = exynos4210_set_frequency; |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 170 | |
Tomasz Figa | 4c8d819 | 2014-05-26 06:26:03 +0900 | [diff] [blame] | 171 | cpufreq = info; |
| 172 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 173 | return 0; |
| 174 | |
| 175 | err_mout_apll: |
Jonghwan Choi | 184cddd | 2012-12-23 15:51:40 -0800 | [diff] [blame] | 176 | clk_put(mout_mpll); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 177 | err_mout_mpll: |
Jonghwan Choi | 184cddd | 2012-12-23 15:51:40 -0800 | [diff] [blame] | 178 | clk_put(moutcore); |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 179 | err_moutcore: |
Jonghwan Choi | 184cddd | 2012-12-23 15:51:40 -0800 | [diff] [blame] | 180 | clk_put(cpu_clk); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 181 | |
Jaecheol Lee | a125a17 | 2012-01-07 20:18:35 +0900 | [diff] [blame] | 182 | pr_debug("%s: failed initialization\n", __func__); |
Sunyoung Kang | f40f91f | 2010-09-16 17:59:21 +0900 | [diff] [blame] | 183 | return -EINVAL; |
| 184 | } |