blob: 9fbe364d7b8e53da6734ec52b8dde9efeb8e6ad0 [file] [log] [blame]
Matt Wagantalld1af38e2011-08-06 01:38:02 -07001/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 * MSM architecture clock driver
3 *
4 * Copyright (C) 2007 Google, Inc.
5 * Copyright (c) 2007-2011, Code Aurora Forum. All rights reserved.
6 * Author: San Mehat <san@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/version.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/delay.h>
25#include <linux/clk.h>
26#include <linux/cpufreq.h>
27#include <linux/mutex.h>
28#include <linux/io.h>
29#include <linux/sort.h>
30#include <linux/remote_spinlock.h>
31#include <mach/board.h>
32#include <mach/msm_iomap.h>
33#include <asm/mach-types.h>
34#include <mach/socinfo.h>
35
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include "smd_private.h"
37#include "acpuclock.h"
38
39#define A11S_CLK_CNTL_ADDR (MSM_CSR_BASE + 0x100)
40#define A11S_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)
41#define A11S_VDD_SVS_PLEVEL_ADDR (MSM_CSR_BASE + 0x124)
42#define PLLn_MODE(n) (MSM_CLK_CTL_BASE + 0x300 + 28 * (n))
43#define PLLn_L_VAL(n) (MSM_CLK_CTL_BASE + 0x304 + 28 * (n))
44
45#define PLL4_MODE (MSM_CLK_CTL_BASE + 0x374)
46#define PLL4_L_VAL (MSM_CLK_CTL_BASE + 0x378)
47
Matt Wagantall6d9ebee2011-08-26 12:15:24 -070048#define POWER_COLLAPSE_KHZ 19200
49
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050/* Max CPU frequency allowed by hardware while in standby waiting for an irq. */
51#define MAX_WAIT_FOR_IRQ_KHZ 128000
52
53enum {
54 ACPU_PLL_TCXO = -1,
55 ACPU_PLL_0 = 0,
56 ACPU_PLL_1,
57 ACPU_PLL_2,
58 ACPU_PLL_3,
59 ACPU_PLL_4,
60 ACPU_PLL_END,
61};
62
63static const struct pll {
64 void __iomem *mod_reg;
65 const uint32_t l_val_mask;
66} soc_pll[ACPU_PLL_END] = {
67 [ACPU_PLL_0] = {PLLn_MODE(ACPU_PLL_0), 0x3f},
68 [ACPU_PLL_1] = {PLLn_MODE(ACPU_PLL_1), 0x3f},
69 [ACPU_PLL_2] = {PLLn_MODE(ACPU_PLL_2), 0x3f},
70 [ACPU_PLL_3] = {PLLn_MODE(ACPU_PLL_3), 0x3f},
71 [ACPU_PLL_4] = {PLL4_MODE, 0x3ff},
72};
73
74struct clock_state {
75 struct clkctl_acpu_speed *current_speed;
76 struct mutex lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077 uint32_t max_speed_delta_khz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078 struct clk *ebi1_clk;
79};
80
81#define PLL_BASE 7
82
83struct shared_pll_control {
84 uint32_t version;
85 struct {
86 /* Denotes if the PLL is ON. Technically, this can be read
87 * directly from the PLL registers, but this feild is here,
88 * so let's use it.
89 */
90 uint32_t on;
91 /* One bit for each processor core. The application processor
92 * is allocated bit position 1. All other bits should be
93 * considered as votes from other processors.
94 */
95 uint32_t votes;
96 } pll[PLL_BASE + ACPU_PLL_END];
97};
98
99struct clkctl_acpu_speed {
100 unsigned int use_for_scaling;
101 unsigned int a11clk_khz;
102 int pll;
103 unsigned int a11clk_src_sel;
104 unsigned int a11clk_src_div;
105 unsigned int ahbclk_khz;
106 unsigned int ahbclk_div;
107 int vdd;
108 unsigned int axiclk_khz;
109 unsigned long lpj; /* loops_per_jiffy */
110 /* Pointers in acpu_freq_tbl[] for max up/down steppings. */
111 struct clkctl_acpu_speed *down[ACPU_PLL_END];
112 struct clkctl_acpu_speed *up[ACPU_PLL_END];
113};
114
115static remote_spinlock_t pll_lock;
116static struct shared_pll_control *pll_control;
117static struct clock_state drv_state = { 0 };
118static struct clkctl_acpu_speed *acpu_freq_tbl;
119
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120/*
121 * ACPU freq tables used for different PLLs frequency combinations. The
122 * correct table is selected during init.
123 *
124 * Table stepping up/down entries are calculated during boot to choose the
125 * largest frequency jump that's less than max_speed_delta_khz on each PLL.
126 */
127
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128/* 7x27 normal with GSM capable modem */
129static struct clkctl_acpu_speed pll0_245_pll1_960_pll2_1200_pll4_0[] = {
130 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 30720 },
131 { 0, 120000, ACPU_PLL_1, 1, 7, 60000, 1, 3, 61440 },
132 { 1, 122880, ACPU_PLL_0, 4, 1, 61440, 1, 3, 61440 },
133 { 0, 200000, ACPU_PLL_2, 2, 5, 66667, 2, 4, 61440 },
134 { 1, 245760, ACPU_PLL_0, 4, 0, 122880, 1, 4, 61440 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530135 { 1, 320000, ACPU_PLL_1, 1, 2, 160000, 1, 5, 160000 },
136 { 0, 400000, ACPU_PLL_2, 2, 2, 133333, 2, 5, 160000 },
137 { 1, 480000, ACPU_PLL_1, 1, 1, 160000, 2, 6, 160000 },
138 { 1, 600000, ACPU_PLL_2, 2, 1, 200000, 2, 7, 200000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
140};
141
142/* 7x27 normal with CDMA-only modem */
143static struct clkctl_acpu_speed pll0_196_pll1_960_pll2_1200_pll4_0[] = {
144 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 24576 },
145 { 1, 98304, ACPU_PLL_0, 4, 1, 98304, 0, 3, 49152 },
146 { 0, 120000, ACPU_PLL_1, 1, 7, 60000, 1, 3, 49152 },
147 { 1, 196608, ACPU_PLL_0, 4, 0, 65536, 2, 4, 98304 },
148 { 0, 200000, ACPU_PLL_2, 2, 5, 66667, 2, 4, 98304 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530149 { 1, 320000, ACPU_PLL_1, 1, 2, 160000, 1, 5, 160000 },
150 { 0, 400000, ACPU_PLL_2, 2, 2, 133333, 2, 5, 160000 },
151 { 1, 480000, ACPU_PLL_1, 1, 1, 160000, 2, 6, 160000 },
152 { 1, 600000, ACPU_PLL_2, 2, 1, 200000, 2, 7, 200000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
154};
155
156/* 7x27 normal with GSM capable modem - PLL0 and PLL1 swapped */
157static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_0[] = {
158 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 30720 },
159 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 61440 },
160 { 1, 122880, ACPU_PLL_1, 1, 1, 61440, 1, 3, 61440 },
161 { 0, 200000, ACPU_PLL_2, 2, 5, 66667, 2, 4, 61440 },
162 { 1, 245760, ACPU_PLL_1, 1, 0, 122880, 1, 4, 61440 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530163 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
164 { 0, 400000, ACPU_PLL_2, 2, 2, 133333, 2, 5, 160000 },
165 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
166 { 1, 600000, ACPU_PLL_2, 2, 1, 200000, 2, 7, 200000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
168};
169
170/* 7x27 normal with CDMA-only modem - PLL0 and PLL1 swapped */
171static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_0[] = {
172 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 24576 },
173 { 1, 98304, ACPU_PLL_1, 1, 1, 98304, 0, 3, 49152 },
174 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 49152 },
175 { 1, 196608, ACPU_PLL_1, 1, 0, 65536, 2, 4, 98304 },
176 { 0, 200000, ACPU_PLL_2, 2, 5, 66667, 2, 4, 98304 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530177 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
178 { 0, 400000, ACPU_PLL_2, 2, 2, 133333, 2, 5, 160000 },
179 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
180 { 1, 600000, ACPU_PLL_2, 2, 1, 200000, 2, 7, 200000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
182};
183
184/* 7x27 normal with GSM capable modem - PLL0 and PLL1 swapped and pll2 @ 800 */
185static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_800_pll4_0[] = {
186 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 30720 },
187 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 61440 },
188 { 1, 122880, ACPU_PLL_1, 1, 1, 61440, 1, 3, 61440 },
189 { 0, 200000, ACPU_PLL_2, 2, 3, 66667, 2, 4, 61440 },
190 { 1, 245760, ACPU_PLL_1, 1, 0, 122880, 1, 4, 61440 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530191 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
192 { 0, 400000, ACPU_PLL_2, 2, 1, 133333, 2, 5, 160000 },
193 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
194 { 1, 800000, ACPU_PLL_2, 2, 0, 200000, 3, 7, 200000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
196};
197
198/* 7x27 normal with CDMA-only modem - PLL0 and PLL1 swapped and pll2 @ 800 */
199static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_800_pll4_0[] = {
200 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 24576 },
201 { 1, 98304, ACPU_PLL_1, 1, 1, 98304, 0, 3, 49152 },
202 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 49152 },
203 { 1, 196608, ACPU_PLL_1, 1, 0, 65536, 2, 4, 98304 },
204 { 0, 200000, ACPU_PLL_2, 2, 3, 66667, 2, 4, 98304 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530205 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
206 { 0, 400000, ACPU_PLL_2, 2, 1, 133333, 2, 5, 160000 },
207 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
208 { 1, 800000, ACPU_PLL_2, 2, 0, 200000, 3, 7, 200000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
210};
211
212/* 7x27a pll2 at 1200mhz with GSM capable modem */
213static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_800[] = {
Trilok Soni7d6c8652011-07-14 15:35:07 +0530214 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
215 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
216 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
217 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
218 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 150000 },
219 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
220 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
221 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
222 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
223 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
225};
226
227/* 7x27a pll2 at 1200mhz with CDMA only modem */
228static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_800[] = {
Trilok Soni7d6c8652011-07-14 15:35:07 +0530229 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
230 { 0, 65536, ACPU_PLL_1, 1, 3, 8192, 3, 1, 49152 },
231 { 1, 98304, ACPU_PLL_1, 1, 1, 12288, 3, 2, 49152 },
232 { 1, 196608, ACPU_PLL_1, 1, 0, 24576, 3, 3, 98304 },
Trilok Soniabb750b2011-07-13 16:47:18 +0530233 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 120000 },
234 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 120000 },
235 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 120000 },
236 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 120000 },
Trilok Soni7d6c8652011-07-14 15:35:07 +0530237 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
238 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
240};
241
Trilok Sonif597e242011-06-06 12:37:16 +0530242/* 7x27aa pll4 at 1008mhz with GSM capable modem */
243static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_1008[] = {
244 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
245 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
246 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
247 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
248 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 150000 },
249 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
250 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
251 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 200000 },
252 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
253 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
254 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
255};
256
Trilok Sonid7b05e52011-08-17 18:09:08 +0530257/* 7x27aa pll4 at 1008mhz with CDMA capable modem */
258static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_1008[] = {
259 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
260 { 0, 65536, ACPU_PLL_1, 1, 3, 8192, 3, 1, 49152 },
261 { 1, 98304, ACPU_PLL_1, 1, 1, 12288, 3, 2, 49152 },
262 { 1, 196608, ACPU_PLL_1, 1, 0, 24576, 3, 3, 98304 },
263 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 150000 },
264 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
265 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
266 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 200000 },
267 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
268 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
269 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
270};
271
Trilok Soni54d35c42011-07-14 17:47:50 +0530272/* 7x25a pll2 at 1200mhz with GSM capable modem */
273static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_800_25a[] = {
274 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
275 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
276 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
277 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
278 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 150000 },
279 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
280 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
281 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
282 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
283 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
284};
285
Trilok Soni9bb022c2011-10-31 18:25:19 +0530286/* 7x27a pll2 at 1200mhz with GSM capable modem */
287static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_800[] = {
288 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
289 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
290 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
291 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
292 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 150000 },
293 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
294 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
295 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
296 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
297 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
298 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
299};
300
301/* 7x27a pll2 at 1200mhz with CDMA only modem */
302static struct clkctl_acpu_speed pll0_960_pll1_589_pll2_1200_pll4_800[] = {
303 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
304 { 0, 65536, ACPU_PLL_1, 1, 8, 8192, 3, 1, 49152 },
305 { 1, 98304, ACPU_PLL_1, 1, 5, 12288, 3, 2, 49152 },
306 { 1, 196608, ACPU_PLL_1, 1, 2, 24576, 3, 3, 98304 },
307 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 120000 },
308 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 120000 },
309 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 120000 },
310 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 120000 },
311 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
312 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
313 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
314};
315
316/* 7x27aa pll4 at 1008mhz with GSM capable modem */
317static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_1008[] = {
318 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
319 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
320 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
321 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
322 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 150000 },
323 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
324 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
325 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 200000 },
326 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
327 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
328 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
329};
330
331/* 7x27aa pll4 at 1008mhz with CDMA capable modem */
332static struct clkctl_acpu_speed pll0_960_pll1_589_pll2_1200_pll4_1008[] = {
333 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
334 { 0, 65536, ACPU_PLL_1, 1, 8, 8192, 3, 1, 49152 },
335 { 1, 98304, ACPU_PLL_1, 1, 5, 12288, 3, 2, 49152 },
336 { 1, 196608, ACPU_PLL_1, 1, 2, 24576, 3, 3, 98304 },
337 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 150000 },
338 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
339 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
340 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 200000 },
341 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
342 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
343 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
344};
345
346/* 7x25a pll2 at 1200mhz with GSM capable modem */
347static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_800_25a[] = {
348 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
349 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
350 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
351 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
352 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 150000 },
353 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
354 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
355 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
356 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
357 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
358};
359
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700360#define PLL_0_MHZ 0
361#define PLL_196_MHZ 10
362#define PLL_245_MHZ 12
Trilok Soni9bb022c2011-10-31 18:25:19 +0530363#define PLL_589_MHZ 30
364#define PLL_737_MHZ 38
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700365#define PLL_800_MHZ 41
366#define PLL_960_MHZ 50
Trilok Sonif597e242011-06-06 12:37:16 +0530367#define PLL_1008_MHZ 52
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368#define PLL_1200_MHZ 62
369
370#define PLL_CONFIG(m0, m1, m2, m4) { \
371 PLL_##m0##_MHZ, PLL_##m1##_MHZ, PLL_##m2##_MHZ, PLL_##m4##_MHZ, \
372 pll0_##m0##_pll1_##m1##_pll2_##m2##_pll4_##m4 \
373}
374
375struct pll_freq_tbl_map {
376 unsigned int pll0_l;
377 unsigned int pll1_l;
378 unsigned int pll2_l;
379 unsigned int pll4_l;
380 struct clkctl_acpu_speed *tbl;
381};
382
383static struct pll_freq_tbl_map acpu_freq_tbl_list[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384 PLL_CONFIG(196, 960, 1200, 0),
385 PLL_CONFIG(245, 960, 1200, 0),
386 PLL_CONFIG(960, 196, 1200, 0),
387 PLL_CONFIG(960, 245, 1200, 0),
388 PLL_CONFIG(960, 196, 800, 0),
389 PLL_CONFIG(960, 245, 800, 0),
390 PLL_CONFIG(960, 245, 1200, 800),
391 PLL_CONFIG(960, 196, 1200, 800),
Trilok Sonif597e242011-06-06 12:37:16 +0530392 PLL_CONFIG(960, 245, 1200, 1008),
Trilok Sonid7b05e52011-08-17 18:09:08 +0530393 PLL_CONFIG(960, 196, 1200, 1008),
Trilok Soni9bb022c2011-10-31 18:25:19 +0530394 PLL_CONFIG(960, 737, 1200, 800),
395 PLL_CONFIG(960, 589, 1200, 800),
396 PLL_CONFIG(960, 737, 1200, 1008),
397 PLL_CONFIG(960, 589, 1200, 1008),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700398 { 0, 0, 0, 0, 0 }
399};
400
401#ifdef CONFIG_CPU_FREQ_MSM
402static struct cpufreq_frequency_table freq_table[20];
403
404static void __init cpufreq_table_init(void)
405{
406 unsigned int i;
407 unsigned int freq_cnt = 0;
408
409 /* Construct the freq_table table from acpu_freq_tbl since the
410 * freq_table values need to match frequencies specified in
411 * acpu_freq_tbl and acpu_freq_tbl needs to be fixed up during init.
412 */
413 for (i = 0; acpu_freq_tbl[i].a11clk_khz != 0
414 && freq_cnt < ARRAY_SIZE(freq_table)-1; i++) {
415 if (acpu_freq_tbl[i].use_for_scaling) {
416 freq_table[freq_cnt].index = freq_cnt;
417 freq_table[freq_cnt].frequency
418 = acpu_freq_tbl[i].a11clk_khz;
419 freq_cnt++;
420 }
421 }
422
423 /* freq_table not big enough to store all usable freqs. */
424 BUG_ON(acpu_freq_tbl[i].a11clk_khz != 0);
425
426 freq_table[freq_cnt].index = freq_cnt;
427 freq_table[freq_cnt].frequency = CPUFREQ_TABLE_END;
428
429 pr_info("%d scaling frequencies supported.\n", freq_cnt);
430}
431#endif
432
433static void pll_enable(void __iomem *addr, unsigned on)
434{
435 if (on) {
436 writel_relaxed(2, addr);
437 mb();
438 udelay(5);
439 writel_relaxed(6, addr);
440 mb();
441 udelay(50);
442 writel_relaxed(7, addr);
443 } else {
444 writel_relaxed(0, addr);
445 }
446}
447
448static int pc_pll_request(unsigned id, unsigned on)
449{
450 int res = 0;
451 on = !!on;
452
453 if (on)
454 pr_debug("Enabling PLL %d\n", id);
455 else
456 pr_debug("Disabling PLL %d\n", id);
457
458 if (id >= ACPU_PLL_END)
459 return -EINVAL;
460
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530461 remote_spin_lock(&pll_lock);
462 if (on) {
463 pll_control->pll[PLL_BASE + id].votes |= 2;
464 if (!pll_control->pll[PLL_BASE + id].on) {
465 pll_enable(soc_pll[id].mod_reg, 1);
466 pll_control->pll[PLL_BASE + id].on = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700467 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468 } else {
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530469 pll_control->pll[PLL_BASE + id].votes &= ~2;
470 if (pll_control->pll[PLL_BASE + id].on
471 && !pll_control->pll[PLL_BASE + id].votes) {
472 pll_enable(soc_pll[id].mod_reg, 0);
473 pll_control->pll[PLL_BASE + id].on = 0;
474 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 }
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530476 remote_spin_unlock(&pll_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477
478 if (on)
479 pr_debug("PLL enabled\n");
480 else
481 pr_debug("PLL disabled\n");
482
483 return res;
484}
485
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486static int acpuclk_set_vdd_level(int vdd)
487{
488 uint32_t current_vdd;
489
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 current_vdd = readl_relaxed(A11S_VDD_SVS_PLEVEL_ADDR) & 0x07;
491
492 pr_debug("Switching VDD from %u mV -> %d mV\n",
493 current_vdd, vdd);
494
495 writel_relaxed((1 << 7) | (vdd << 3), A11S_VDD_SVS_PLEVEL_ADDR);
496 mb();
Matt Wagantallec57f062011-08-16 23:54:46 -0700497 udelay(62);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 if ((readl_relaxed(A11S_VDD_SVS_PLEVEL_ADDR) & 0x7) != vdd) {
499 pr_err("VDD set failed\n");
500 return -EIO;
501 }
502
503 pr_debug("VDD switched\n");
504
505 return 0;
506}
507
508/* Set proper dividers for the given clock speed. */
509static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s)
510{
511 uint32_t reg_clkctl, reg_clksel, clk_div, src_sel;
512
513 reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
514
515 /* AHB_CLK_DIV */
516 clk_div = (reg_clksel >> 1) & 0x03;
517 /* CLK_SEL_SRC1NO */
518 src_sel = reg_clksel & 1;
519
520 /*
521 * If the new clock divider is higher than the previous, then
522 * program the divider before switching the clock
523 */
524 if (hunt_s->ahbclk_div > clk_div) {
525 reg_clksel &= ~(0x3 << 1);
526 reg_clksel |= (hunt_s->ahbclk_div << 1);
527 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
528 }
529
530 /* Program clock source and divider */
531 reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
532 reg_clkctl &= ~(0xFF << (8 * src_sel));
533 reg_clkctl |= hunt_s->a11clk_src_sel << (4 + 8 * src_sel);
534 reg_clkctl |= hunt_s->a11clk_src_div << (0 + 8 * src_sel);
535 writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
536
537 /* Program clock source selection */
538 reg_clksel ^= 1;
539 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
540
541 /*
542 * If the new clock divider is lower than the previous, then
543 * program the divider after switching the clock
544 */
545 if (hunt_s->ahbclk_div < clk_div) {
546 reg_clksel &= ~(0x3 << 1);
547 reg_clksel |= (hunt_s->ahbclk_div << 1);
548 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
549 }
550}
551
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530552static int acpuclk_7627_set_rate(int cpu, unsigned long rate,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700553 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554{
555 uint32_t reg_clkctl;
556 struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;
557 int res, rc = 0;
558 unsigned int plls_enabled = 0, pll;
559
560 if (reason == SETRATE_CPUFREQ)
561 mutex_lock(&drv_state.lock);
562
563 strt_s = cur_s = drv_state.current_speed;
564
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700565 WARN_ONCE(cur_s == NULL, "%s: not initialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 if (cur_s == NULL) {
567 rc = -ENOENT;
568 goto out;
569 }
570
571 if (rate == cur_s->a11clk_khz)
572 goto out;
573
574 for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {
575 if (tgt_s->a11clk_khz == rate)
576 break;
577 }
578
579 if (tgt_s->a11clk_khz == 0) {
580 rc = -EINVAL;
581 goto out;
582 }
583
584 /* Choose the highest speed at or below 'rate' with same PLL. */
585 if (reason != SETRATE_CPUFREQ
586 && tgt_s->a11clk_khz < cur_s->a11clk_khz) {
587 while (tgt_s->pll != ACPU_PLL_TCXO && tgt_s->pll != cur_s->pll)
588 tgt_s--;
589 }
590
591 if (strt_s->pll != ACPU_PLL_TCXO)
592 plls_enabled |= 1 << strt_s->pll;
593
594 if (reason == SETRATE_CPUFREQ) {
595 if (strt_s->pll != tgt_s->pll && tgt_s->pll != ACPU_PLL_TCXO) {
596 rc = pc_pll_request(tgt_s->pll, 1);
597 if (rc < 0) {
598 pr_err("PLL%d enable failed (%d)\n",
599 tgt_s->pll, rc);
600 goto out;
601 }
602 plls_enabled |= 1 << tgt_s->pll;
603 }
604 }
605 /* Need to do this when coming out of power collapse since some modem
606 * firmwares reset the VDD when the application processor enters power
607 * collapse. */
608 if (reason == SETRATE_CPUFREQ || reason == SETRATE_PC) {
609 /* Increase VDD if needed. */
610 if (tgt_s->vdd > cur_s->vdd) {
611 rc = acpuclk_set_vdd_level(tgt_s->vdd);
612 if (rc < 0) {
613 pr_err("Unable to switch ACPU vdd (%d)\n", rc);
614 goto out;
615 }
616 }
617 }
618
619 /* Set wait states for CPU inbetween frequency changes */
620 reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
621 reg_clkctl |= (100 << 16); /* set WT_ST_CNT */
622 writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
623
624 pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
625 strt_s->a11clk_khz, tgt_s->a11clk_khz);
626
627 while (cur_s != tgt_s) {
628 /*
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530629 * Always jump to target freq if within max_speed_delta_khz,
630 * regardless of PLL. If differnece is greater, use the
631 * predefined steppings in the table.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 */
633 int d = abs((int)(cur_s->a11clk_khz - tgt_s->a11clk_khz));
634 if (d > drv_state.max_speed_delta_khz) {
635
636 if (tgt_s->a11clk_khz > cur_s->a11clk_khz) {
637 /* Step up: jump to target PLL as early as
638 * possible so indexing using TCXO (up[-1])
639 * never occurs. */
640 if (likely(cur_s->up[tgt_s->pll]))
641 cur_s = cur_s->up[tgt_s->pll];
642 else
643 cur_s = cur_s->up[cur_s->pll];
644 } else {
645 /* Step down: stay on current PLL as long as
646 * possible so indexing using TCXO (down[-1])
647 * never occurs. */
648 if (likely(cur_s->down[cur_s->pll]))
649 cur_s = cur_s->down[cur_s->pll];
650 else
651 cur_s = cur_s->down[tgt_s->pll];
652 }
653
654 if (cur_s == NULL) { /* This should not happen. */
655 pr_err("No stepping frequencies found. "
656 "strt_s:%u tgt_s:%u\n",
657 strt_s->a11clk_khz, tgt_s->a11clk_khz);
658 rc = -EINVAL;
659 goto out;
660 }
661
662 } else {
663 cur_s = tgt_s;
664 }
665
666 pr_debug("STEP khz = %u, pll = %d\n",
667 cur_s->a11clk_khz, cur_s->pll);
668
669 if (cur_s->pll != ACPU_PLL_TCXO
670 && !(plls_enabled & (1 << cur_s->pll))) {
671 rc = pc_pll_request(cur_s->pll, 1);
672 if (rc < 0) {
673 pr_err("PLL%d enable failed (%d)\n",
674 cur_s->pll, rc);
675 goto out;
676 }
677 plls_enabled |= 1 << cur_s->pll;
678 }
679
680 acpuclk_set_div(cur_s);
681 drv_state.current_speed = cur_s;
682 /* Re-adjust lpj for the new clock speed. */
683 loops_per_jiffy = cur_s->lpj;
684 mb();
Matt Wagantallec57f062011-08-16 23:54:46 -0700685 udelay(50);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686 }
687
688 /* Nothing else to do for SWFI. */
689 if (reason == SETRATE_SWFI)
690 goto out;
691
692 /* Change the AXI bus frequency if we can. */
693 if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {
694 res = clk_set_rate(drv_state.ebi1_clk,
695 tgt_s->axiclk_khz * 1000);
696 if (res < 0)
697 pr_warning("Setting AXI min rate failed (%d)\n", res);
698 }
699
700 /* Disable PLLs we are not using anymore. */
701 if (tgt_s->pll != ACPU_PLL_TCXO)
702 plls_enabled &= ~(1 << tgt_s->pll);
703 for (pll = ACPU_PLL_0; pll < ACPU_PLL_END; pll++)
704 if (plls_enabled & (1 << pll)) {
705 res = pc_pll_request(pll, 0);
706 if (res < 0)
707 pr_warning("PLL%d disable failed (%d)\n",
708 pll, res);
709 }
710
711 /* Nothing else to do for power collapse. */
712 if (reason == SETRATE_PC)
713 goto out;
714
715 /* Drop VDD level if we can. */
716 if (tgt_s->vdd < strt_s->vdd) {
717 res = acpuclk_set_vdd_level(tgt_s->vdd);
718 if (res < 0)
719 pr_warning("Unable to drop ACPU vdd (%d)\n", res);
720 }
721
722 pr_debug("ACPU speed change complete\n");
723out:
724 if (reason == SETRATE_CPUFREQ)
725 mutex_unlock(&drv_state.lock);
726 return rc;
727}
728
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700729static void __init acpuclk_hw_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730{
731 struct clkctl_acpu_speed *speed;
Trilok Soni7d6c8652011-07-14 15:35:07 +0530732 uint32_t div, sel, reg_clksel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733 int res;
734
735 /*
736 * Determine the rate of ACPU clock
737 */
738
739 if (!(readl_relaxed(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */
740 /* CLK_SRC0_SEL */
741 sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;
742 /* CLK_SRC0_DIV */
743 div = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;
744 } else {
745 /* CLK_SRC1_SEL */
746 sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;
747 /* CLK_SRC1_DIV */
748 div = readl_relaxed(A11S_CLK_CNTL_ADDR) & 0x0f;
749 }
750
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {
752 if (speed->a11clk_src_sel == sel
753 && (speed->a11clk_src_div == div))
754 break;
755 }
756 if (speed->a11clk_khz == 0) {
757 pr_err("Error - ACPU clock reports invalid speed\n");
758 return;
759 }
760
761 drv_state.current_speed = speed;
762 if (speed->pll != ACPU_PLL_TCXO)
763 if (pc_pll_request(speed->pll, 1))
764 pr_warning("Failed to vote for boot PLL\n");
765
Trilok Soni7d6c8652011-07-14 15:35:07 +0530766 /* Fix div2 to 2 for 7x27/5a(aa) targets */
767 if (!cpu_is_msm7x27()) {
768 reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
769 reg_clksel &= ~(0x3 << 14);
770 reg_clksel |= (0x1 << 14);
771 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
772 }
773
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774 res = clk_set_rate(drv_state.ebi1_clk, speed->axiclk_khz * 1000);
775 if (res < 0)
776 pr_warning("Setting AXI min rate failed (%d)\n", res);
777 res = clk_enable(drv_state.ebi1_clk);
778 if (res < 0)
779 pr_warning("Enabling AXI clock failed (%d)\n", res);
780
781 pr_info("ACPU running at %d KHz\n", speed->a11clk_khz);
782}
783
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530784static unsigned long acpuclk_7627_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700785{
786 WARN_ONCE(drv_state.current_speed == NULL,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700787 "%s: not initialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 if (drv_state.current_speed)
789 return drv_state.current_speed->a11clk_khz;
790 else
791 return 0;
792}
793
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794/*----------------------------------------------------------------------------
795 * Clock driver initialization
796 *---------------------------------------------------------------------------*/
797
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798static void __init acpu_freq_tbl_fixup(void)
799{
800 unsigned long pll0_l, pll1_l, pll2_l, pll4_l;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801 struct pll_freq_tbl_map *lst;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700802
803 /* Wait for the PLLs to be initialized and then read their frequency.
804 */
805 do {
806 pll0_l = readl_relaxed(PLLn_L_VAL(0)) &
807 soc_pll[ACPU_PLL_0].l_val_mask;
808 cpu_relax();
809 udelay(50);
810 } while (pll0_l == 0);
811 do {
812 pll1_l = readl_relaxed(PLLn_L_VAL(1)) &
813 soc_pll[ACPU_PLL_1].l_val_mask;
814 cpu_relax();
815 udelay(50);
816 } while (pll1_l == 0);
817 do {
818 pll2_l = readl_relaxed(PLLn_L_VAL(2)) &
819 soc_pll[ACPU_PLL_2].l_val_mask;
820 cpu_relax();
821 udelay(50);
822 } while (pll2_l == 0);
823
824 pr_info("L val: PLL0: %d, PLL1: %d, PLL2: %d\n",
825 (int)pll0_l, (int)pll1_l, (int)pll2_l);
826
827 if (!cpu_is_msm7x27() && !cpu_is_msm7x25a()) {
828 do {
829 pll4_l = readl_relaxed(PLL4_L_VAL) &
830 soc_pll[ACPU_PLL_4].l_val_mask;
831 cpu_relax();
832 udelay(50);
833 } while (pll4_l == 0);
834 pr_info("L val: PLL4: %d\n", (int)pll4_l);
835 } else {
836 pll4_l = 0;
837 }
838
Trilok Soni54d35c42011-07-14 17:47:50 +0530839 /* Fix the tables for 7x25a variant to not conflict with 7x27 ones */
840 if (cpu_is_msm7x25a()) {
841 if (pll1_l == PLL_245_MHZ) {
842 acpu_freq_tbl =
843 pll0_960_pll1_245_pll2_1200_pll4_800_25a;
Trilok Soni9bb022c2011-10-31 18:25:19 +0530844 } else if (pll1_l == PLL_737_MHZ) {
845 acpu_freq_tbl =
846 pll0_960_pll1_737_pll2_1200_pll4_800_25a;
Trilok Soni54d35c42011-07-14 17:47:50 +0530847 }
848 } else {
849 /* Select the right table to use. */
850 for (lst = acpu_freq_tbl_list; lst->tbl != 0; lst++) {
851 if (lst->pll0_l == pll0_l && lst->pll1_l == pll1_l
852 && lst->pll2_l == pll2_l
853 && lst->pll4_l == pll4_l) {
854 acpu_freq_tbl = lst->tbl;
855 break;
856 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 }
858 }
859
860 if (acpu_freq_tbl == NULL) {
861 pr_crit("Unknown PLL configuration!\n");
862 BUG();
863 }
864
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865 /* The default 7x27 ACPU clock plan supports running the AXI bus at
866 * 200 MHz. So we don't classify it as Turbo mode.
867 */
868 if (cpu_is_msm7x27())
869 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870}
871
872/*
873 * Hardware requires the CPU to be dropped to less than MAX_WAIT_FOR_IRQ_KHZ
874 * before entering a wait for irq low-power mode. Find a suitable rate.
875 */
876static unsigned long __init find_wait_for_irq_khz(void)
877{
878 unsigned long found_khz = 0;
879 int i;
880
881 for (i = 0; acpu_freq_tbl[i].a11clk_khz &&
882 acpu_freq_tbl[i].a11clk_khz <= MAX_WAIT_FOR_IRQ_KHZ; i++)
883 found_khz = acpu_freq_tbl[i].a11clk_khz;
884
885 return found_khz;
886}
887
888/* Initalize the lpj field in the acpu_freq_tbl. */
889static void __init lpj_init(void)
890{
891 int i;
892 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
893 for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
894 acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
895 base_clk->a11clk_khz,
896 acpu_freq_tbl[i].a11clk_khz);
897 }
898}
899
900static void __init precompute_stepping(void)
901{
902 int i, step_idx;
903
904#define cur_freq acpu_freq_tbl[i].a11clk_khz
905#define step_freq acpu_freq_tbl[step_idx].a11clk_khz
906#define cur_pll acpu_freq_tbl[i].pll
907#define step_pll acpu_freq_tbl[step_idx].pll
908
909 for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
910
911 /* Calculate max "up" step for each destination PLL */
912 step_idx = i + 1;
913 while (step_freq && (step_freq - cur_freq)
914 <= drv_state.max_speed_delta_khz) {
915 acpu_freq_tbl[i].up[step_pll] =
916 &acpu_freq_tbl[step_idx];
917 step_idx++;
918 }
919 if (step_idx == (i + 1) && step_freq) {
920 pr_crit("Delta between freqs %u KHz and %u KHz is"
921 " too high!\n", cur_freq, step_freq);
922 BUG();
923 }
924
925 /* Calculate max "down" step for each destination PLL */
926 step_idx = i - 1;
927 while (step_idx >= 0 && (cur_freq - step_freq)
928 <= drv_state.max_speed_delta_khz) {
929 acpu_freq_tbl[i].down[step_pll] =
930 &acpu_freq_tbl[step_idx];
931 step_idx--;
932 }
933 if (step_idx == (i - 1) && i > 0) {
934 pr_crit("Delta between freqs %u KHz and %u KHz is"
935 " too high!\n", cur_freq, step_freq);
936 BUG();
937 }
938 }
939}
940
941static void __init print_acpu_freq_tbl(void)
942{
943 struct clkctl_acpu_speed *t;
944 short down_idx[ACPU_PLL_END];
945 short up_idx[ACPU_PLL_END];
946 int i, j;
947
948#define FREQ_IDX(freq_ptr) (freq_ptr - acpu_freq_tbl)
949 pr_info("Id CPU-KHz PLL DIV AHB-KHz ADIV AXI-KHz "
950 "D0 D1 D2 D4 U0 U1 U2 U4\n");
951
952 t = &acpu_freq_tbl[0];
953 for (i = 0; t->a11clk_khz != 0; i++) {
954
955 for (j = 0; j < ACPU_PLL_END; j++) {
956 down_idx[j] = t->down[j] ? FREQ_IDX(t->down[j]) : -1;
957 up_idx[j] = t->up[j] ? FREQ_IDX(t->up[j]) : -1;
958 }
959
960 pr_info("%2d %7d %3d %3d %7d %4d %7d "
961 "%2d %2d %2d %2d %2d %2d %2d %2d\n",
962 i, t->a11clk_khz, t->pll, t->a11clk_src_div + 1,
963 t->ahbclk_khz, t->ahbclk_div + 1, t->axiclk_khz,
964 down_idx[0], down_idx[1], down_idx[2], down_idx[4],
965 up_idx[0], up_idx[1], up_idx[2], up_idx[4]);
966
967 t++;
968 }
969}
970
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971static void shared_pll_control_init(void)
972{
973#define PLL_REMOTE_SPINLOCK_ID "S:7"
974 unsigned smem_size;
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530975
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 remote_spin_lock_init(&pll_lock, PLL_REMOTE_SPINLOCK_ID);
977 pll_control = smem_get_entry(SMEM_CLKREGIM_SOURCES, &smem_size);
978
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530979 if (!pll_control) {
980 pr_err("Can't find shared PLL control data structure!\n");
981 BUG();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982 /* There might be more PLLs than what the application processor knows
983 * about. But the index used for each PLL is guaranteed to remain the
984 * same. */
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530985 } else if (smem_size < sizeof(struct shared_pll_control)) {
986 pr_err("Shared PLL control data"
987 "structure too small!\n");
988 BUG();
989 } else if (pll_control->version != 0xCCEE0001) {
990 pr_err("Shared PLL control version mismatch!\n");
991 BUG();
992 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 pr_info("Shared PLL control available.\n");
994 return;
995 }
996
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997}
998
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530999static struct acpuclk_data acpuclk_7627_data = {
1000 .set_rate = acpuclk_7627_set_rate,
1001 .get_rate = acpuclk_7627_get_rate,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001002 .power_collapse_khz = POWER_COLLAPSE_KHZ,
Matt Wagantallec57f062011-08-16 23:54:46 -07001003 .switch_time_us = 50,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001004};
1005
Pankaj Kumar6e66f372011-12-05 14:41:58 +05301006static int __init acpuclk_7627_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001007{
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001008 pr_info("%s()\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009
1010 drv_state.ebi1_clk = clk_get(NULL, "ebi1_acpu_clk");
1011 BUG_ON(IS_ERR(drv_state.ebi1_clk));
1012
1013 mutex_init(&drv_state.lock);
1014 shared_pll_control_init();
Matt Wagantallec57f062011-08-16 23:54:46 -07001015 drv_state.max_speed_delta_khz = soc_data->max_speed_delta_khz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 acpu_freq_tbl_fixup();
Pankaj Kumar6e66f372011-12-05 14:41:58 +05301017 acpuclk_7627_data.wait_for_irq_khz = find_wait_for_irq_khz();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001018 precompute_stepping();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001019 acpuclk_hw_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 lpj_init();
1021 print_acpu_freq_tbl();
Pankaj Kumar6e66f372011-12-05 14:41:58 +05301022 acpuclk_register(&acpuclk_7627_data);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001023
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024#ifdef CONFIG_CPU_FREQ_MSM
1025 cpufreq_table_init();
1026 cpufreq_frequency_table_get_attr(freq_table, smp_processor_id());
1027#endif
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001028 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029}
Matt Wagantallec57f062011-08-16 23:54:46 -07001030
Matt Wagantallec57f062011-08-16 23:54:46 -07001031struct acpuclk_soc_data acpuclk_7x27_soc_data __initdata = {
1032 .max_speed_delta_khz = 400000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +05301033 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -07001034};
1035
1036struct acpuclk_soc_data acpuclk_7x27a_soc_data __initdata = {
1037 .max_speed_delta_khz = 400000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +05301038 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -07001039};
1040
1041struct acpuclk_soc_data acpuclk_7x27aa_soc_data __initdata = {
1042 .max_speed_delta_khz = 504000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +05301043 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -07001044};