blob: be8c31c0dfde95a8d4457e2c33734252be162fea [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/kernel.h>
16#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/delay.h>
19#include <linux/mutex.h>
20#include <linux/err.h>
21#include <linux/errno.h>
22#include <linux/cpufreq.h>
23#include <linux/cpu.h>
24#include <linux/regulator/consumer.h>
25
26#include <asm/mach-types.h>
27#include <asm/cpu.h>
28
29#include <mach/board.h>
30#include <mach/msm_iomap.h>
31#include <mach/rpm-regulator.h>
32#include <mach/msm_bus.h>
33#include <mach/msm_bus_board.h>
34#include <mach/socinfo.h>
Stephen Boyd469ed3e2011-09-29 16:41:19 -070035#include <mach/msm-krait-l2-accessors.h>
Matt Wagantallcb12c392011-10-19 10:32:07 -070036#include <mach/rpm-regulator.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
38#include "acpuclock.h"
39
40/*
41 * Source IDs.
42 * These must be negative to not overlap with the source IDs
43 * used by the 8x60 local clock driver.
44 */
45#define PLL_8 0
46#define HFPLL -1
47#define QSB -2
48
49/* Mux source selects. */
50#define PRI_SRC_SEL_SEC_SRC 0
51#define PRI_SRC_SEL_HFPLL 1
52#define PRI_SRC_SEL_HFPLL_DIV2 2
53#define SEC_SRC_SEL_QSB 0
Matt Wagantall65e5e4b2011-10-27 16:52:10 -070054#define SEC_SRC_SEL_AUX 2
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055
56/* HFPLL registers offsets. */
57#define HFPLL_MODE 0x00
58#define HFPLL_CONFIG_CTL 0x04
59#define HFPLL_L_VAL 0x08
60#define HFPLL_M_VAL 0x0C
61#define HFPLL_N_VAL 0x10
62#define HFPLL_DROOP_CTL 0x14
63
64/* CP15 L2 indirect addresses. */
65#define L2CPMR_IADDR 0x500
66#define L2CPUCPMR_IADDR 0x501
67
68#define STBY_KHZ 1
69
70#define HFPLL_NOMINAL_VDD 1050000
Matt Wagantallc5236722011-10-14 17:47:06 -070071#define HFPLL_LOW_VDD 945000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072#define HFPLL_LOW_VDD_PLL_L_MAX 0x28
73
74#define SECCLKAGD BIT(4)
75
Matt Wagantalla518f8f2011-10-17 13:24:53 -070076/* PTE EFUSE register. */
77#define QFPROM_PTE_EFUSE_ADDR (MSM_QFPROM_BASE + 0x00C0)
78
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070079enum scalables {
80 CPU0 = 0,
81 CPU1,
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -070082 CPU2,
83 CPU3,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070084 L2,
85 NUM_SCALABLES
86};
87
88enum vregs {
89 VREG_CORE,
90 VREG_MEM,
91 VREG_DIG,
Matt Wagantallcb12c392011-10-19 10:32:07 -070092 VREG_HFPLL_A,
93 VREG_HFPLL_B,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070094 NUM_VREG
95};
96
97struct vreg {
98 const char name[15];
99 const unsigned int max_vdd;
100 const int rpm_vreg_voter;
101 const int rpm_vreg_id;
102 struct regulator *reg;
103 unsigned int cur_vdd;
104};
105
106struct core_speed {
107 unsigned int khz;
108 int src;
109 unsigned int pri_src_sel;
110 unsigned int sec_src_sel;
111 unsigned int pll_l_val;
112};
113
114struct l2_level {
115 struct core_speed speed;
116 unsigned int vdd_dig;
117 unsigned int vdd_mem;
118 unsigned int bw_level;
119};
120
121struct acpu_level {
122 unsigned int use_for_scaling;
123 struct core_speed speed;
124 struct l2_level *l2_level;
125 unsigned int vdd_core;
126};
127
128struct scalable {
129 void * __iomem const hfpll_base;
130 void * __iomem const aux_clk_sel;
131 const uint32_t l2cpmr_iaddr;
132 struct core_speed *current_speed;
133 struct l2_level *l2_vote;
134 struct vreg vreg[NUM_VREG];
135 bool first_set_call;
136};
137
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700138static struct scalable scalable_8960[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 [CPU0] = {
140 .hfpll_base = MSM_HFPLL_BASE + 0x200,
141 .aux_clk_sel = MSM_ACC0_BASE + 0x014,
142 .l2cpmr_iaddr = L2CPUCPMR_IADDR,
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800143 .vreg[VREG_CORE] = { "krait0", 1300000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144 .vreg[VREG_MEM] = { "krait0_mem", 1150000,
145 RPM_VREG_VOTER1,
146 RPM_VREG_ID_PM8921_L24 },
147 .vreg[VREG_DIG] = { "krait0_dig", 1150000,
148 RPM_VREG_VOTER1,
149 RPM_VREG_ID_PM8921_S3 },
Matt Wagantallcb12c392011-10-19 10:32:07 -0700150 .vreg[VREG_HFPLL_A] = { "hfpll", 2200000,
151 RPM_VREG_VOTER1,
152 RPM_VREG_ID_PM8921_S8 },
153 .vreg[VREG_HFPLL_B] = { "hfpll", 1800000,
154 RPM_VREG_VOTER1,
155 RPM_VREG_ID_PM8921_L23 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156 },
157 [CPU1] = {
158 .hfpll_base = MSM_HFPLL_BASE + 0x300,
159 .aux_clk_sel = MSM_ACC1_BASE + 0x014,
160 .l2cpmr_iaddr = L2CPUCPMR_IADDR,
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800161 .vreg[VREG_CORE] = { "krait1", 1300000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162 .vreg[VREG_MEM] = { "krait0_mem", 1150000,
163 RPM_VREG_VOTER2,
164 RPM_VREG_ID_PM8921_L24 },
165 .vreg[VREG_DIG] = { "krait0_dig", 1150000,
166 RPM_VREG_VOTER2,
167 RPM_VREG_ID_PM8921_S3 },
Matt Wagantallcb12c392011-10-19 10:32:07 -0700168 .vreg[VREG_HFPLL_A] = { "hfpll", 2200000,
169 RPM_VREG_VOTER2,
170 RPM_VREG_ID_PM8921_S8 },
171 .vreg[VREG_HFPLL_B] = { "hfpll", 1800000,
172 RPM_VREG_VOTER2,
173 RPM_VREG_ID_PM8921_L23 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174 },
175 [L2] = {
176 .hfpll_base = MSM_HFPLL_BASE + 0x400,
177 .aux_clk_sel = MSM_APCS_GCC_BASE + 0x028,
178 .l2cpmr_iaddr = L2CPMR_IADDR,
Matt Wagantallcb12c392011-10-19 10:32:07 -0700179 .vreg[VREG_HFPLL_A] = { "hfpll", 2200000,
180 RPM_VREG_VOTER6,
181 RPM_VREG_ID_PM8921_S8 },
182 .vreg[VREG_HFPLL_B] = { "hfpll", 1800000,
183 RPM_VREG_VOTER6,
184 RPM_VREG_ID_PM8921_L23 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185 },
186};
187
Stephen Boyd7ad84752011-08-05 14:04:28 -0700188static DEFINE_MUTEX(driver_lock);
189static DEFINE_SPINLOCK(l2_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700191static struct scalable scalable_8064[] = {
192 [CPU0] = {
193 .hfpll_base = MSM_HFPLL_BASE + 0x200,
194 .aux_clk_sel = MSM_ACC0_BASE + 0x014,
195 .l2cpmr_iaddr = L2CPUCPMR_IADDR,
196 .vreg[VREG_CORE] = { "krait0", 1150000 },
197 .vreg[VREG_MEM] = { "krait0_mem", 1150000,
198 RPM_VREG_VOTER1,
199 RPM_VREG_ID_PM8921_L24 },
200 .vreg[VREG_DIG] = { "krait0_dig", 1150000,
201 RPM_VREG_VOTER1,
202 RPM_VREG_ID_PM8921_S3 },
203 },
204 [CPU1] = {
205 .hfpll_base = MSM_HFPLL_BASE + 0x240,
206 .aux_clk_sel = MSM_ACC1_BASE + 0x014,
207 .l2cpmr_iaddr = L2CPUCPMR_IADDR,
208 .vreg[VREG_CORE] = { "krait1", 1150000 },
209 .vreg[VREG_MEM] = { "krait0_mem", 1150000,
210 RPM_VREG_VOTER2,
211 RPM_VREG_ID_PM8921_L24 },
212 .vreg[VREG_DIG] = { "krait0_dig", 1150000,
213 RPM_VREG_VOTER2,
214 RPM_VREG_ID_PM8921_S3 },
215 },
216 [CPU2] = {
217 .hfpll_base = MSM_HFPLL_BASE + 0x280,
218 .aux_clk_sel = MSM_ACC2_BASE + 0x014,
219 .l2cpmr_iaddr = L2CPUCPMR_IADDR,
220 .vreg[VREG_CORE] = { "krait2", 1150000 },
221 .vreg[VREG_MEM] = { "krait0_mem", 1150000,
222 RPM_VREG_VOTER4,
223 RPM_VREG_ID_PM8921_L24 },
224 .vreg[VREG_DIG] = { "krait0_dig", 1150000,
225 RPM_VREG_VOTER4,
226 RPM_VREG_ID_PM8921_S3 },
227 },
228 [CPU3] = {
229 .hfpll_base = MSM_HFPLL_BASE + 0x2C0,
230 .aux_clk_sel = MSM_ACC3_BASE + 0x014,
231 .l2cpmr_iaddr = L2CPUCPMR_IADDR,
232 .vreg[VREG_CORE] = { "krait3", 1150000 },
233 .vreg[VREG_MEM] = { "krait0_mem", 1150000,
234 RPM_VREG_VOTER5,
235 RPM_VREG_ID_PM8921_L24 },
236 .vreg[VREG_DIG] = { "krait0_dig", 1150000,
237 RPM_VREG_VOTER5,
238 RPM_VREG_ID_PM8921_S3 },
239 },
240 [L2] = {
241 .hfpll_base = MSM_HFPLL_BASE + 0x300,
242 .aux_clk_sel = MSM_APCS_GCC_BASE + 0x028,
243 .l2cpmr_iaddr = L2CPMR_IADDR,
244 },
245};
246
247static struct scalable *scalable;
248static struct l2_level *l2_freq_tbl;
249static struct acpu_level *acpu_freq_tbl;
250static int l2_freq_tbl_size;
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700251
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252/* Instantaneous bandwidth requests in MB/s. */
253#define BW_MBPS(_bw) \
254 { \
255 .vectors = (struct msm_bus_vectors[]){ \
256 {\
257 .src = MSM_BUS_MASTER_AMPSS_M0, \
258 .dst = MSM_BUS_SLAVE_EBI_CH0, \
259 .ib = (_bw) * 1000000UL, \
260 .ab = (_bw) * 100000UL, \
261 }, \
262 { \
263 .src = MSM_BUS_MASTER_AMPSS_M1, \
264 .dst = MSM_BUS_SLAVE_EBI_CH0, \
265 .ib = (_bw) * 1000000UL, \
266 .ab = (_bw) * 100000UL, \
267 }, \
268 }, \
269 .num_paths = 2, \
270 }
271static struct msm_bus_paths bw_level_tbl[] = {
272 [0] = BW_MBPS(616), /* At least 77 MHz on bus. */
273 [1] = BW_MBPS(1024), /* At least 128 MHz on bus. */
274 [2] = BW_MBPS(1536), /* At least 192 MHz on bus. */
275 [3] = BW_MBPS(2048), /* At least 256 MHz on bus. */
276 [4] = BW_MBPS(3080), /* At least 385 MHz on bus. */
277 [5] = BW_MBPS(3968), /* At least 496 MHz on bus. */
278};
279
280static struct msm_bus_scale_pdata bus_client_pdata = {
281 .usecase = bw_level_tbl,
282 .num_usecases = ARRAY_SIZE(bw_level_tbl),
283 .active_only = 1,
284 .name = "acpuclock",
285};
286
287static uint32_t bus_perf_client;
288
289/* TODO: Update vdd_dig and vdd_mem when voltage data is available. */
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800290#define L2(x) (&l2_freq_tbl_8960_kraitv1[(x)])
291static struct l2_level l2_freq_tbl_8960_kraitv1[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 [0] = { {STBY_KHZ, QSB, 0, 0, 0x00 }, 1050000, 1050000, 0 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700293 [1] = { { 384000, PLL_8, 0, 2, 0x00 }, 1050000, 1050000, 1 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294 [2] = { { 432000, HFPLL, 2, 0, 0x20 }, 1050000, 1050000, 1 },
295 [3] = { { 486000, HFPLL, 2, 0, 0x24 }, 1050000, 1050000, 1 },
296 [4] = { { 540000, HFPLL, 2, 0, 0x28 }, 1050000, 1050000, 1 },
297 [5] = { { 594000, HFPLL, 1, 0, 0x16 }, 1050000, 1050000, 2 },
298 [6] = { { 648000, HFPLL, 1, 0, 0x18 }, 1050000, 1050000, 2 },
299 [7] = { { 702000, HFPLL, 1, 0, 0x1A }, 1050000, 1050000, 2 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700300 [8] = { { 756000, HFPLL, 1, 0, 0x1C }, 1150000, 1150000, 2 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700301 [9] = { { 810000, HFPLL, 1, 0, 0x1E }, 1150000, 1150000, 3 },
302 [10] = { { 864000, HFPLL, 1, 0, 0x20 }, 1150000, 1150000, 3 },
303 [11] = { { 918000, HFPLL, 1, 0, 0x22 }, 1150000, 1150000, 3 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304};
305
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800306static struct acpu_level acpu_freq_tbl_8960_kraitv1[] = {
Matt Wagantallc5236722011-10-14 17:47:06 -0700307 { 0, {STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 950000 },
308 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 950000 },
309 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(6), 950000 },
310 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(6), 962500 },
311 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(6), 962500 },
312 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(6), 987500 },
313 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 1000000 },
314 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(6), 1025000 },
315 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(11), 1050000 },
316 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(11), 1087500 },
317 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(11), 1125000 },
318 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1137500 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319 { 0, { 0 } }
320};
321
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800322#undef L2
323#define L2(x) (&l2_freq_tbl_8960_kraitv2[(x)])
324static struct l2_level l2_freq_tbl_8960_kraitv2[] = {
325 [0] = { {STBY_KHZ, QSB, 0, 0, 0x00 }, 1050000, 1050000, 0 },
326 [1] = { { 384000, PLL_8, 0, 2, 0x00 }, 1050000, 1050000, 1 },
327 [2] = { { 432000, HFPLL, 2, 0, 0x20 }, 1050000, 1050000, 1 },
328 [3] = { { 486000, HFPLL, 2, 0, 0x24 }, 1050000, 1050000, 1 },
329 [4] = { { 540000, HFPLL, 2, 0, 0x28 }, 1050000, 1050000, 1 },
330 [5] = { { 594000, HFPLL, 1, 0, 0x16 }, 1050000, 1050000, 2 },
331 [6] = { { 648000, HFPLL, 1, 0, 0x18 }, 1050000, 1050000, 2 },
332 [7] = { { 702000, HFPLL, 1, 0, 0x1A }, 1050000, 1050000, 2 },
333 [8] = { { 756000, HFPLL, 1, 0, 0x1C }, 1150000, 1150000, 2 },
334 [9] = { { 810000, HFPLL, 1, 0, 0x1E }, 1150000, 1150000, 3 },
335 [10] = { { 864000, HFPLL, 1, 0, 0x20 }, 1150000, 1150000, 3 },
336 [11] = { { 918000, HFPLL, 1, 0, 0x22 }, 1150000, 1150000, 3 },
337 [12] = { { 972000, HFPLL, 1, 0, 0x24 }, 1150000, 1150000, 4 },
338 [13] = { { 1026000, HFPLL, 1, 0, 0x26 }, 1150000, 1150000, 4 },
339 [14] = { { 1080000, HFPLL, 1, 0, 0x28 }, 1150000, 1150000, 4 },
340 [15] = { { 1134000, HFPLL, 1, 0, 0x2A }, 1150000, 1150000, 4 },
341 [16] = { { 1188000, HFPLL, 1, 0, 0x2C }, 1150000, 1150000, 4 },
342};
343
344static struct acpu_level acpu_freq_tbl_8960_kraitv2[] = {
345 { 0, { STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 950000 },
346 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 950000 },
347 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(6), 950000 },
348 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(6), 962500 },
349 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(6), 962500 },
350 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(6), 987500 },
351 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 1000000 },
352 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(6), 1025000 },
353 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(11), 1050000 },
354 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(11), 1087500 },
355 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(11), 1125000 },
356 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1137500 },
357 { 1, { 972000, HFPLL, 1, 0, 0x24 }, L2(16), 1300000 },
358 { 1, { 1026000, HFPLL, 1, 0, 0x26 }, L2(16), 1300000 },
359 { 1, { 1080000, HFPLL, 1, 0, 0x28 }, L2(16), 1300000 },
360 { 1, { 1134000, HFPLL, 1, 0, 0x2A }, L2(16), 1300000 },
361 { 1, { 1188000, HFPLL, 1, 0, 0x2C }, L2(16), 1300000 },
362 { 1, { 1242000, HFPLL, 1, 0, 0x2E }, L2(16), 1300000 },
363 { 1, { 1296000, HFPLL, 1, 0, 0x30 }, L2(16), 1300000 },
364 { 1, { 1350000, HFPLL, 1, 0, 0x32 }, L2(16), 1300000 },
365 { 1, { 1404000, HFPLL, 1, 0, 0x34 }, L2(16), 1300000 },
366 { 1, { 1458000, HFPLL, 1, 0, 0x36 }, L2(16), 1300000 },
367 { 1, { 1512000, HFPLL, 1, 0, 0x38 }, L2(16), 1300000 },
368 { 0, { 0 } }
369};
370
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700371/* TODO: Update vdd_dig and vdd_mem when voltage data is available. */
372#undef L2
373#define L2(x) (&l2_freq_tbl_8064[(x)])
374static struct l2_level l2_freq_tbl_8064[] = {
375 [0] = { {STBY_KHZ, QSB, 0, 0, 0x00 }, 1050000, 1050000, 0 },
376 [1] = { { 384000, PLL_8, 0, 2, 0x00 }, 1050000, 1050000, 0 },
377 [2] = { { 432000, HFPLL, 2, 0, 0x20 }, 1050000, 1050000, 1 },
378 [3] = { { 486000, HFPLL, 2, 0, 0x24 }, 1050000, 1050000, 1 },
379 [4] = { { 540000, HFPLL, 2, 0, 0x28 }, 1050000, 1050000, 1 },
380 [5] = { { 594000, HFPLL, 1, 0, 0x16 }, 1050000, 1050000, 2 },
381 [6] = { { 648000, HFPLL, 1, 0, 0x18 }, 1050000, 1050000, 2 },
382 [7] = { { 702000, HFPLL, 1, 0, 0x1A }, 1050000, 1050000, 2 },
383 [8] = { { 756000, HFPLL, 1, 0, 0x1C }, 1150000, 1150000, 3 },
384 [9] = { { 810000, HFPLL, 1, 0, 0x1E }, 1150000, 1150000, 3 },
385 [10] = { { 864000, HFPLL, 1, 0, 0x20 }, 1150000, 1150000, 3 },
386 [11] = { { 918000, HFPLL, 1, 0, 0x22 }, 1150000, 1150000, 3 },
387 [12] = { { 972000, HFPLL, 1, 0, 0x24 }, 1150000, 1150000, 3 },
388 [13] = { { 1026000, HFPLL, 1, 0, 0x26 }, 1150000, 1150000, 3 },
389 [14] = { { 1080000, HFPLL, 1, 0, 0x28 }, 1150000, 1150000, 4 },
390 [15] = { { 1134000, HFPLL, 1, 0, 0x2A }, 1150000, 1150000, 4 },
391 [16] = { { 1188000, HFPLL, 1, 0, 0x2C }, 1150000, 1150000, 4 },
392 [17] = { { 1242000, HFPLL, 1, 0, 0x2E }, 1150000, 1150000, 4 },
393 [18] = { { 1296000, HFPLL, 1, 0, 0x30 }, 1150000, 1150000, 4 },
394 [19] = { { 1350000, HFPLL, 1, 0, 0x32 }, 1150000, 1150000, 4 },
395 [20] = { { 1404000, HFPLL, 1, 0, 0x34 }, 1150000, 1150000, 4 },
396 [21] = { { 1458000, HFPLL, 1, 0, 0x36 }, 1150000, 1150000, 5 },
397 [22] = { { 1512000, HFPLL, 1, 0, 0x38 }, 1150000, 1150000, 5 },
398 [23] = { { 1566000, HFPLL, 1, 0, 0x3A }, 1150000, 1150000, 5 },
399 [24] = { { 1620000, HFPLL, 1, 0, 0x3C }, 1150000, 1150000, 5 },
400 [25] = { { 1674000, HFPLL, 1, 0, 0x3E }, 1150000, 1150000, 5 },
401};
402
403/* TODO: Update core voltages when data is available. */
404static struct acpu_level acpu_freq_tbl_8064[] = {
405 { 0, {STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 1050000 },
406 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 1050000 },
407 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(2), 1050000 },
408 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(3), 1050000 },
409 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(4), 1050000 },
410 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(5), 1050000 },
411 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 1050000 },
412 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(7), 1050000 },
413 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(8), 1150000 },
414 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(9), 1150000 },
415 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(10), 1150000 },
416 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1150000 },
417 { 0, { 0 } }
418};
419
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700420static unsigned long acpuclk_8960_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700421{
422 return scalable[cpu].current_speed->khz;
423}
424
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425/* Get the selected source on primary MUX. */
426static int get_pri_clk_src(struct scalable *sc)
427{
428 uint32_t regval;
429
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700430 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700431 return regval & 0x3;
432}
433
434/* Set the selected source on primary MUX. */
435static void set_pri_clk_src(struct scalable *sc, uint32_t pri_src_sel)
436{
437 uint32_t regval;
438
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700439 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700440 regval &= ~0x3;
441 regval |= (pri_src_sel & 0x3);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700442 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 /* Wait for switch to complete. */
444 mb();
445 udelay(1);
446}
447
448/* Get the selected source on secondary MUX. */
449static int get_sec_clk_src(struct scalable *sc)
450{
451 uint32_t regval;
452
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700453 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700454 return (regval >> 2) & 0x3;
455}
456
457/* Set the selected source on secondary MUX. */
458static void set_sec_clk_src(struct scalable *sc, uint32_t sec_src_sel)
459{
460 uint32_t regval;
461
462 /* Disable secondary source clock gating during switch. */
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700463 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 regval |= SECCLKAGD;
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700465 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700466
467 /* Program the MUX. */
468 regval &= ~(0x3 << 2);
469 regval |= ((sec_src_sel & 0x3) << 2);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700470 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700471
472 /* Wait for switch to complete. */
473 mb();
474 udelay(1);
Stephen Boyd753b5092011-10-17 19:14:12 -0700475
476 /* Re-enable secondary source clock gating. */
477 regval &= ~SECCLKAGD;
478 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479}
480
481/* Enable an already-configured HFPLL. */
482static void hfpll_enable(struct scalable *sc)
483{
Matt Wagantallcb12c392011-10-19 10:32:07 -0700484 int rc;
485
486 if (cpu_is_msm8960()) {
487 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_A].rpm_vreg_id,
488 sc->vreg[VREG_HFPLL_A].rpm_vreg_voter, 2200000,
489 2200000, 0);
490 if (rc)
491 pr_err("%s regulator enable failed (%d)\n",
492 sc->vreg[VREG_HFPLL_A].name, rc);
493 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_B].rpm_vreg_id,
494 sc->vreg[VREG_HFPLL_B].rpm_vreg_voter, 1800000,
495 1800000, 0);
496 if (rc)
497 pr_err("%s regulator enable failed (%d)\n",
498 sc->vreg[VREG_HFPLL_B].name, rc);
499 }
500
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700501 /* Disable PLL bypass mode. */
502 writel_relaxed(0x2, sc->hfpll_base + HFPLL_MODE);
503
504 /*
505 * H/W requires a 5us delay between disabling the bypass and
506 * de-asserting the reset. Delay 10us just to be safe.
507 */
508 mb();
509 udelay(10);
510
511 /* De-assert active-low PLL reset. */
512 writel_relaxed(0x6, sc->hfpll_base + HFPLL_MODE);
513
514 /* Wait for PLL to lock. */
515 mb();
516 udelay(60);
517
518 /* Enable PLL output. */
519 writel_relaxed(0x7, sc->hfpll_base + HFPLL_MODE);
520}
521
522/* Disable a HFPLL for power-savings or while its being reprogrammed. */
523static void hfpll_disable(struct scalable *sc)
524{
Matt Wagantallcb12c392011-10-19 10:32:07 -0700525 int rc;
526
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527 /*
528 * Disable the PLL output, disable test mode, enable
529 * the bypass mode, and assert the reset.
530 */
531 writel_relaxed(0, sc->hfpll_base + HFPLL_MODE);
Matt Wagantallcb12c392011-10-19 10:32:07 -0700532
533 if (cpu_is_msm8960()) {
534 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_B].rpm_vreg_id,
535 sc->vreg[VREG_HFPLL_B].rpm_vreg_voter, 0,
536 0, 0);
537 if (rc)
538 pr_err("%s regulator enable failed (%d)\n",
539 sc->vreg[VREG_HFPLL_B].name, rc);
540 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_A].rpm_vreg_id,
541 sc->vreg[VREG_HFPLL_A].rpm_vreg_voter, 0,
542 0, 0);
543 if (rc)
544 pr_err("%s regulator enable failed (%d)\n",
545 sc->vreg[VREG_HFPLL_A].name, rc);
546 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700547}
548
549/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
550static void hfpll_set_rate(struct scalable *sc, struct core_speed *tgt_s)
551{
552 writel_relaxed(tgt_s->pll_l_val, sc->hfpll_base + HFPLL_L_VAL);
553}
554
555/* Return the L2 speed that should be applied. */
556static struct l2_level *compute_l2_level(struct scalable *sc,
557 struct l2_level *vote_l)
558{
559 struct l2_level *new_l;
560 int cpu;
561
562 /* Bounds check. */
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700563 BUG_ON(vote_l >= (l2_freq_tbl + l2_freq_tbl_size));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
565 /* Find max L2 speed vote. */
566 sc->l2_vote = vote_l;
567 new_l = l2_freq_tbl;
568 for_each_present_cpu(cpu)
569 new_l = max(new_l, scalable[cpu].l2_vote);
570
571 return new_l;
572}
573
574/* Update the bus bandwidth request. */
575static void set_bus_bw(unsigned int bw)
576{
577 int ret;
578
579 /* Bounds check. */
580 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
581 pr_err("invalid bandwidth request (%d)\n", bw);
582 return;
583 }
584
585 /* Update bandwidth if request has changed. This may sleep. */
586 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
587 if (ret)
588 pr_err("bandwidth request failed (%d)\n", ret);
589}
590
591/* Set the CPU or L2 clock speed. */
592static void set_speed(struct scalable *sc, struct core_speed *tgt_s,
593 enum setrate_reason reason)
594{
595 struct core_speed *strt_s = sc->current_speed;
596
597 if (tgt_s == strt_s)
598 return;
599
600 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700601 /*
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700602 * Move to an always-on source running at a frequency that does
603 * not require an elevated CPU voltage. PLL8 is used here.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604 */
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700605 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
607
608 /* Program CPU HFPLL. */
609 hfpll_disable(sc);
610 hfpll_set_rate(sc, tgt_s);
611 hfpll_enable(sc);
612
613 /* Move CPU to HFPLL source. */
614 set_pri_clk_src(sc, tgt_s->pri_src_sel);
615 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700616 /*
617 * If responding to CPU_DEAD we must be running on another
618 * CPU. Therefore, we can't access the downed CPU's CP15
619 * clock MUX registers from here and can't change clock sources.
620 * Just turn off the PLL- since the CPU is down already, halting
621 * its clock should be safe.
622 */
623 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2]) {
624 set_sec_clk_src(sc, tgt_s->sec_src_sel);
625 set_pri_clk_src(sc, tgt_s->pri_src_sel);
626 }
627 hfpll_disable(sc);
628 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
629 hfpll_set_rate(sc, tgt_s);
630 hfpll_enable(sc);
631 /*
632 * If responding to CPU_UP_PREPARE, we can't change CP15
633 * registers for the CPU that's coming up since we're not
634 * running on that CPU. That's okay though, since the MUX
635 * source was not changed on the way down, either.
636 */
637 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2])
638 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2])
641 set_sec_clk_src(sc, tgt_s->sec_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 }
643
644 sc->current_speed = tgt_s;
645}
646
647/* Apply any per-cpu voltage increases. */
648static int increase_vdd(int cpu, unsigned int vdd_core, unsigned int vdd_mem,
649 unsigned int vdd_dig, enum setrate_reason reason)
650{
651 struct scalable *sc = &scalable[cpu];
Saravana Kannan9dcb89f2011-09-26 19:02:22 -0700652 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653
654 /*
Matt Wagantallabd55f02011-09-12 11:45:54 -0700655 * Increase vdd_mem active-set before vdd_dig.
656 * vdd_mem should be >= vdd_dig.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 */
658 if (vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
659 rc = rpm_vreg_set_voltage(sc->vreg[VREG_MEM].rpm_vreg_id,
660 sc->vreg[VREG_MEM].rpm_vreg_voter, vdd_mem,
661 sc->vreg[VREG_MEM].max_vdd, 0);
662 if (rc) {
663 pr_err("%s: vdd_mem (cpu%d) increase failed (%d)\n",
664 __func__, cpu, rc);
665 return rc;
666 }
667 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
668 }
669
670 /* Increase vdd_dig active-set vote. */
671 if (vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
672 rc = rpm_vreg_set_voltage(sc->vreg[VREG_DIG].rpm_vreg_id,
673 sc->vreg[VREG_DIG].rpm_vreg_voter, vdd_dig,
674 sc->vreg[VREG_DIG].max_vdd, 0);
675 if (rc) {
676 pr_err("%s: vdd_dig (cpu%d) increase failed (%d)\n",
677 __func__, cpu, rc);
678 return rc;
679 }
680 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
681 }
682
683 /*
684 * Update per-CPU core voltage. Don't do this for the hotplug path for
685 * which it should already be correct. Attempting to set it is bad
686 * because we don't know what CPU we are running on at this point, but
687 * the CPU regulator API requires we call it from the affected CPU.
688 */
689 if (vdd_core > sc->vreg[VREG_CORE].cur_vdd
690 && reason != SETRATE_HOTPLUG) {
691 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
692 sc->vreg[VREG_CORE].max_vdd);
693 if (rc) {
694 pr_err("%s: vdd_core (cpu%d) increase failed (%d)\n",
695 __func__, cpu, rc);
696 return rc;
697 }
698 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
699 }
700
701 return rc;
702}
703
704/* Apply any per-cpu voltage decreases. */
705static void decrease_vdd(int cpu, unsigned int vdd_core, unsigned int vdd_mem,
706 unsigned int vdd_dig, enum setrate_reason reason)
707{
708 struct scalable *sc = &scalable[cpu];
709 int ret;
710
711 /*
712 * Update per-CPU core voltage. This must be called on the CPU
713 * that's being affected. Don't do this in the hotplug remove path,
714 * where the rail is off and we're executing on the other CPU.
715 */
716 if (vdd_core < sc->vreg[VREG_CORE].cur_vdd
717 && reason != SETRATE_HOTPLUG) {
718 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
719 sc->vreg[VREG_CORE].max_vdd);
720 if (ret) {
721 pr_err("%s: vdd_core (cpu%d) decrease failed (%d)\n",
722 __func__, cpu, ret);
723 return;
724 }
725 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
726 }
727
728 /* Decrease vdd_dig active-set vote. */
729 if (vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
730 ret = rpm_vreg_set_voltage(sc->vreg[VREG_DIG].rpm_vreg_id,
731 sc->vreg[VREG_DIG].rpm_vreg_voter, vdd_dig,
732 sc->vreg[VREG_DIG].max_vdd, 0);
733 if (ret) {
734 pr_err("%s: vdd_dig (cpu%d) decrease failed (%d)\n",
735 __func__, cpu, ret);
736 return;
737 }
738 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
739 }
740
741 /*
Matt Wagantallabd55f02011-09-12 11:45:54 -0700742 * Decrease vdd_mem active-set after vdd_dig.
743 * vdd_mem should be >= vdd_dig.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 */
745 if (vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
746 ret = rpm_vreg_set_voltage(sc->vreg[VREG_MEM].rpm_vreg_id,
747 sc->vreg[VREG_MEM].rpm_vreg_voter, vdd_mem,
748 sc->vreg[VREG_MEM].max_vdd, 0);
749 if (ret) {
750 pr_err("%s: vdd_mem (cpu%d) decrease failed (%d)\n",
751 __func__, cpu, ret);
752 return;
753 }
754 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
755 }
756}
757
758static unsigned int calculate_vdd_mem(struct acpu_level *tgt)
759{
Matt Wagantallabd55f02011-09-12 11:45:54 -0700760 return tgt->l2_level->vdd_mem;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761}
762
763static unsigned int calculate_vdd_dig(struct acpu_level *tgt)
764{
765 unsigned int pll_vdd_dig;
766
Stephen Boydc76158f2011-12-08 12:42:40 -0800767 if (tgt->l2_level->speed.src != HFPLL)
768 pll_vdd_dig = 0;
769 else if (tgt->l2_level->speed.pll_l_val > HFPLL_LOW_VDD_PLL_L_MAX)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 pll_vdd_dig = HFPLL_NOMINAL_VDD;
771 else
772 pll_vdd_dig = HFPLL_LOW_VDD;
773
774 return max(tgt->l2_level->vdd_dig, pll_vdd_dig);
775}
776
777static unsigned int calculate_vdd_core(struct acpu_level *tgt)
778{
779 unsigned int pll_vdd_core;
780
Stephen Boydc76158f2011-12-08 12:42:40 -0800781 if (tgt->speed.src != HFPLL)
782 pll_vdd_core = 0;
783 else if (tgt->speed.pll_l_val > HFPLL_LOW_VDD_PLL_L_MAX)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700784 pll_vdd_core = HFPLL_NOMINAL_VDD;
785 else
786 pll_vdd_core = HFPLL_LOW_VDD;
787
788 return max(tgt->vdd_core, pll_vdd_core);
789}
790
791/* Set the CPU's clock rate and adjust the L2 rate, if appropriate. */
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700792static int acpuclk_8960_set_rate(int cpu, unsigned long rate,
793 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794{
795 struct core_speed *strt_acpu_s, *tgt_acpu_s;
796 struct l2_level *tgt_l2_l;
797 struct acpu_level *tgt;
798 unsigned int vdd_mem, vdd_dig, vdd_core;
799 unsigned long flags;
800 int rc = 0;
801
802 if (cpu > num_possible_cpus()) {
803 rc = -EINVAL;
804 goto out;
805 }
806
807 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
808 mutex_lock(&driver_lock);
809
810 strt_acpu_s = scalable[cpu].current_speed;
811
812 /* Return early if rate didn't change. */
813 if (rate == strt_acpu_s->khz && scalable[cpu].first_set_call == false)
814 goto out;
815
816 /* Find target frequency. */
817 for (tgt = acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
818 if (tgt->speed.khz == rate) {
819 tgt_acpu_s = &tgt->speed;
820 break;
821 }
822 }
823 if (tgt->speed.khz == 0) {
824 rc = -EINVAL;
825 goto out;
826 }
827
828 /* Calculate voltage requirements for the current CPU. */
829 vdd_mem = calculate_vdd_mem(tgt);
830 vdd_dig = calculate_vdd_dig(tgt);
831 vdd_core = calculate_vdd_core(tgt);
832
833 /* Increase VDD levels if needed. */
834 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
835 rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
836 if (rc)
837 goto out;
838 }
839
840 pr_debug("Switching from ACPU%d rate %u KHz -> %u KHz\n",
841 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
842
843 /* Set the CPU speed. */
844 set_speed(&scalable[cpu], tgt_acpu_s, reason);
845
846 /*
847 * Update the L2 vote and apply the rate change. A spinlock is
848 * necessary to ensure L2 rate is calulated and set atomically,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700849 * even if acpuclk_8960_set_rate() is called from an atomic context
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 * and the driver_lock mutex is not acquired.
851 */
852 spin_lock_irqsave(&l2_lock, flags);
853 tgt_l2_l = compute_l2_level(&scalable[cpu], tgt->l2_level);
854 set_speed(&scalable[L2], &tgt_l2_l->speed, reason);
855 spin_unlock_irqrestore(&l2_lock, flags);
856
857 /* Nothing else to do for power collapse or SWFI. */
858 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
859 goto out;
860
861 /* Update bus bandwith request. */
862 set_bus_bw(tgt_l2_l->bw_level);
863
864 /* Drop VDD levels if we can. */
865 decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
866
867 scalable[cpu].first_set_call = false;
868 pr_debug("ACPU%d speed change complete\n", cpu);
869
870out:
871 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
872 mutex_unlock(&driver_lock);
873 return rc;
874}
875
876/* Initialize a HFPLL at a given rate and enable it. */
877static void __init hfpll_init(struct scalable *sc, struct core_speed *tgt_s)
878{
879 pr_debug("Initializing HFPLL%d\n", sc - scalable);
880
881 /* Disable the PLL for re-programming. */
882 hfpll_disable(sc);
883
884 /* Configure PLL parameters for integer mode. */
885 writel_relaxed(0x7845C665, sc->hfpll_base + HFPLL_CONFIG_CTL);
886 writel_relaxed(0, sc->hfpll_base + HFPLL_M_VAL);
887 writel_relaxed(1, sc->hfpll_base + HFPLL_N_VAL);
888
889 /* Program droop controller. */
890 writel_relaxed(0x0108C000, sc->hfpll_base + HFPLL_DROOP_CTL);
891
892 /* Set an initial rate and enable the PLL. */
893 hfpll_set_rate(sc, tgt_s);
894 hfpll_enable(sc);
895}
896
897/* Voltage regulator initialization. */
898static void __init regulator_init(void)
899{
900 int cpu, ret;
901 struct scalable *sc;
902
903 for_each_possible_cpu(cpu) {
904 sc = &scalable[cpu];
905 sc->vreg[VREG_CORE].reg = regulator_get(NULL,
906 sc->vreg[VREG_CORE].name);
907 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
908 pr_err("regulator_get(%s) failed (%ld)\n",
909 sc->vreg[VREG_CORE].name,
910 PTR_ERR(sc->vreg[VREG_CORE].reg));
911 BUG();
912 }
913
914 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
915 sc->vreg[VREG_CORE].max_vdd,
916 sc->vreg[VREG_CORE].max_vdd);
917 if (ret)
918 pr_err("regulator_set_voltage(%s) failed"
919 " (%d)\n", sc->vreg[VREG_CORE].name, ret);
920
921 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
922 if (ret)
923 pr_err("regulator_enable(%s) failed (%d)\n",
924 sc->vreg[VREG_CORE].name, ret);
925 }
926}
927
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928/* Set initial rate for a given core. */
929static void __init init_clock_sources(struct scalable *sc,
930 struct core_speed *tgt_s)
931{
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700932 uint32_t regval;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700934 /* Select PLL8 as AUX source input to the secondary MUX. */
935 writel_relaxed(0x3, sc->aux_clk_sel);
936
937 /* Switch away from the HFPLL while it's re-initialized. */
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700938 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700939 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 hfpll_init(sc, tgt_s);
941
942 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700943 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 regval &= ~(0x3 << 6);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700945 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700947 /* Switch to the target clock source. */
948 set_sec_clk_src(sc, tgt_s->sec_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949 set_pri_clk_src(sc, tgt_s->pri_src_sel);
950 sc->current_speed = tgt_s;
951
952 /*
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700953 * Set this flag so that the first call to acpuclk_8960_set_rate() can
954 * drop voltages and set initial bus bandwidth requests.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955 */
956 sc->first_set_call = true;
957}
958
Matt Wagantall8e726c72011-08-06 00:49:28 -0700959static void __init per_cpu_init(void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960{
Matt Wagantall6b013ca2011-10-12 14:15:45 -0700961 struct acpu_level *max_acpu_level = data;
Matt Wagantall8e726c72011-08-06 00:49:28 -0700962 int cpu = smp_processor_id();
Matt Wagantall6b013ca2011-10-12 14:15:45 -0700963
964 init_clock_sources(&scalable[cpu], &max_acpu_level->speed);
965 scalable[cpu].l2_vote = max_acpu_level->l2_level;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966}
967
968/* Register with bus driver. */
969static void __init bus_init(void)
970{
971 int ret;
972
973 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
974 if (!bus_perf_client) {
975 pr_err("unable to register bus client\n");
976 BUG();
977 }
978
979 ret = msm_bus_scale_client_update_request(bus_perf_client,
980 (ARRAY_SIZE(bw_level_tbl)-1));
981 if (ret)
982 pr_err("initial bandwidth request failed (%d)\n", ret);
983}
984
985#ifdef CONFIG_CPU_FREQ_MSM
986static struct cpufreq_frequency_table freq_table[NR_CPUS][30];
987
988static void __init cpufreq_table_init(void)
989{
990 int cpu;
991
992 for_each_possible_cpu(cpu) {
993 int i, freq_cnt = 0;
994 /* Construct the freq_table tables from acpu_freq_tbl. */
995 for (i = 0; acpu_freq_tbl[i].speed.khz != 0
996 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
997 if (acpu_freq_tbl[i].use_for_scaling) {
998 freq_table[cpu][freq_cnt].index = freq_cnt;
999 freq_table[cpu][freq_cnt].frequency
1000 = acpu_freq_tbl[i].speed.khz;
1001 freq_cnt++;
1002 }
1003 }
1004 /* freq_table not big enough to store all usable freqs. */
1005 BUG_ON(acpu_freq_tbl[i].speed.khz != 0);
1006
1007 freq_table[cpu][freq_cnt].index = freq_cnt;
1008 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
1009
1010 pr_info("CPU%d: %d scaling frequencies supported.\n",
1011 cpu, freq_cnt);
1012
1013 /* Register table with CPUFreq. */
1014 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
1015 }
1016}
1017#else
1018static void __init cpufreq_table_init(void) {}
1019#endif
1020
1021#define HOT_UNPLUG_KHZ STBY_KHZ
1022static int __cpuinit acpuclock_cpu_callback(struct notifier_block *nfb,
1023 unsigned long action, void *hcpu)
1024{
1025 static int prev_khz[NR_CPUS];
1026 static int prev_pri_src[NR_CPUS];
1027 static int prev_sec_src[NR_CPUS];
1028 int cpu = (int)hcpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029
1030 switch (action) {
1031 case CPU_DYING:
1032 case CPU_DYING_FROZEN:
1033 /*
Matt Wagantall27663842011-08-25 15:11:48 -07001034 * On Krait v1, the primary and secondary muxes must be set
1035 * to QSB before L2 power collapse and restored after.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 */
Matt Wagantall27663842011-08-25 15:11:48 -07001037 if (cpu_is_krait_v1()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038 prev_sec_src[cpu] = get_sec_clk_src(&scalable[cpu]);
1039 prev_pri_src[cpu] = get_pri_clk_src(&scalable[cpu]);
1040 set_sec_clk_src(&scalable[cpu], SEC_SRC_SEL_QSB);
1041 set_pri_clk_src(&scalable[cpu], PRI_SRC_SEL_SEC_SRC);
1042 }
1043 break;
1044 case CPU_DEAD:
1045 case CPU_DEAD_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001046 prev_khz[cpu] = acpuclk_8960_get_rate(cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 /* Fall through. */
1048 case CPU_UP_CANCELED:
1049 case CPU_UP_CANCELED_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001050 acpuclk_8960_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051 break;
1052 case CPU_UP_PREPARE:
1053 case CPU_UP_PREPARE_FROZEN:
1054 if (WARN_ON(!prev_khz[cpu]))
1055 prev_khz[cpu] = acpu_freq_tbl->speed.khz;
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001056 acpuclk_8960_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057 break;
1058 case CPU_STARTING:
1059 case CPU_STARTING_FROZEN:
Matt Wagantall27663842011-08-25 15:11:48 -07001060 if (cpu_is_krait_v1()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 set_sec_clk_src(&scalable[cpu], prev_sec_src[cpu]);
1062 set_pri_clk_src(&scalable[cpu], prev_pri_src[cpu]);
1063 }
1064 break;
1065 default:
1066 break;
1067 }
1068
1069 return NOTIFY_OK;
1070}
1071
1072static struct notifier_block __cpuinitdata acpuclock_cpu_notifier = {
1073 .notifier_call = acpuclock_cpu_callback,
1074};
1075
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001076static struct acpu_level * __init select_freq_plan(void)
1077{
1078 struct acpu_level *l, *max_acpu_level = NULL;
1079
1080 /* Select frequency tables. */
Stepan Moskovchenko532008c2011-10-25 14:43:49 -07001081 if (cpu_is_msm8960() || cpu_is_msm8930()) {
Matt Wagantalla518f8f2011-10-17 13:24:53 -07001082 uint32_t pte_efuse, pvs;
1083
1084 pte_efuse = readl_relaxed(QFPROM_PTE_EFUSE_ADDR);
1085 pvs = (pte_efuse >> 10) & 0x7;
1086 if (pvs == 0x7)
1087 pvs = (pte_efuse >> 13) & 0x7;
1088
1089 switch (pvs) {
1090 case 0x0:
1091 case 0x7:
1092 pr_info("ACPU PVS: Slow\n");
1093 break;
1094 case 0x1:
1095 pr_info("ACPU PVS: Nominal\n");
1096 break;
1097 case 0x3:
1098 pr_info("ACPU PVS: Fast\n");
1099 break;
1100 default:
1101 pr_warn("ACPU PVS: Unknown. Defaulting to slow.\n");
1102 break;
1103 }
1104
1105 /* TODO: Select tables based on PVS data. */
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001106 scalable = scalable_8960;
Stephen Boyd1be9bf62011-11-21 10:51:46 -08001107 if (cpu_is_krait_v1()) {
1108 acpu_freq_tbl = acpu_freq_tbl_8960_kraitv1;
1109 l2_freq_tbl = l2_freq_tbl_8960_kraitv1;
1110 l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_8960_kraitv1);
1111 } else {
1112 acpu_freq_tbl = acpu_freq_tbl_8960_kraitv2;
1113 l2_freq_tbl = l2_freq_tbl_8960_kraitv2;
1114 l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_8960_kraitv2);
1115 }
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001116 } else if (cpu_is_apq8064()) {
1117 scalable = scalable_8064;
1118 acpu_freq_tbl = acpu_freq_tbl_8064;
1119 l2_freq_tbl = l2_freq_tbl_8064;
1120 l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_8064);
1121 } else {
1122 BUG();
1123 }
1124
1125 /* Find the max supported scaling frequency. */
1126 for (l = acpu_freq_tbl; l->speed.khz != 0; l++)
1127 if (l->use_for_scaling)
1128 max_acpu_level = l;
1129 BUG_ON(!max_acpu_level);
1130 pr_info("Max ACPU freq: %u KHz\n", max_acpu_level->speed.khz);
1131
1132 return max_acpu_level;
1133}
1134
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001135static struct acpuclk_data acpuclk_8960_data = {
1136 .set_rate = acpuclk_8960_set_rate,
1137 .get_rate = acpuclk_8960_get_rate,
1138 .power_collapse_khz = STBY_KHZ,
1139 .wait_for_irq_khz = STBY_KHZ,
1140};
1141
Matt Wagantallec57f062011-08-16 23:54:46 -07001142static int __init acpuclk_8960_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143{
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001144 struct acpu_level *max_acpu_level = select_freq_plan();
1145 init_clock_sources(&scalable[L2], &max_acpu_level->l2_level->speed);
1146 on_each_cpu(per_cpu_init, max_acpu_level, true);
Matt Wagantall8e726c72011-08-06 00:49:28 -07001147
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 regulator_init();
1149 bus_init();
1150 cpufreq_table_init();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001151
1152 acpuclk_register(&acpuclk_8960_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153 register_hotcpu_notifier(&acpuclock_cpu_notifier);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001154
1155 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156}
Matt Wagantallec57f062011-08-16 23:54:46 -07001157
1158struct acpuclk_soc_data acpuclk_8960_soc_data __initdata = {
1159 .init = acpuclk_8960_init,
1160};