blob: 82d8173ba0f448afb933469ecbaf471992773f9a [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
Stephen Boyd9d0fab12011-12-08 10:56:06 -080071#define HFPLL_LOW_VDD 850000
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 Wagantall627f4312011-12-13 13:33:47 -0800150 .vreg[VREG_HFPLL_A] = { "hfpll", 2100000,
Matt Wagantallcb12c392011-10-19 10:32:07 -0700151 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 Wagantall627f4312011-12-13 13:33:47 -0800168 .vreg[VREG_HFPLL_A] = { "hfpll", 2100000,
Matt Wagantallcb12c392011-10-19 10:32:07 -0700169 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 Wagantall627f4312011-12-13 13:33:47 -0800179 .vreg[VREG_HFPLL_A] = { "hfpll", 2100000,
Matt Wagantallcb12c392011-10-19 10:32:07 -0700180 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[] = {
Stephen Boydf2770c32011-12-07 18:52:30 -0800272 [0] = BW_MBPS(640), /* At least 80 MHz on bus. */
273 [1] = BW_MBPS(1064), /* At least 133 MHz on bus. */
274 [2] = BW_MBPS(1600), /* At least 200 MHz on bus. */
275 [3] = BW_MBPS(2128), /* At least 266 MHz on bus. */
276 [4] = BW_MBPS(3200), /* At least 400 MHz on bus. */
277 [5] = BW_MBPS(3600), /* At least 450 MHz on bus. */
278 [6] = BW_MBPS(3936), /* At least 492 MHz on bus. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279};
280
281static struct msm_bus_scale_pdata bus_client_pdata = {
282 .usecase = bw_level_tbl,
283 .num_usecases = ARRAY_SIZE(bw_level_tbl),
284 .active_only = 1,
285 .name = "acpuclock",
286};
287
288static uint32_t bus_perf_client;
289
290/* TODO: Update vdd_dig and vdd_mem when voltage data is available. */
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800291#define L2(x) (&l2_freq_tbl_8960_kraitv1[(x)])
292static struct l2_level l2_freq_tbl_8960_kraitv1[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 [0] = { {STBY_KHZ, QSB, 0, 0, 0x00 }, 1050000, 1050000, 0 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700294 [1] = { { 384000, PLL_8, 0, 2, 0x00 }, 1050000, 1050000, 1 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 [2] = { { 432000, HFPLL, 2, 0, 0x20 }, 1050000, 1050000, 1 },
296 [3] = { { 486000, HFPLL, 2, 0, 0x24 }, 1050000, 1050000, 1 },
297 [4] = { { 540000, HFPLL, 2, 0, 0x28 }, 1050000, 1050000, 1 },
298 [5] = { { 594000, HFPLL, 1, 0, 0x16 }, 1050000, 1050000, 2 },
299 [6] = { { 648000, HFPLL, 1, 0, 0x18 }, 1050000, 1050000, 2 },
300 [7] = { { 702000, HFPLL, 1, 0, 0x1A }, 1050000, 1050000, 2 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700301 [8] = { { 756000, HFPLL, 1, 0, 0x1C }, 1150000, 1150000, 2 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 [9] = { { 810000, HFPLL, 1, 0, 0x1E }, 1150000, 1150000, 3 },
303 [10] = { { 864000, HFPLL, 1, 0, 0x20 }, 1150000, 1150000, 3 },
304 [11] = { { 918000, HFPLL, 1, 0, 0x22 }, 1150000, 1150000, 3 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305};
306
Stephen Boyd9d0fab12011-12-08 10:56:06 -0800307static struct acpu_level acpu_freq_tbl_8960_kraitv1_slow[] = {
308 { 0, {STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 900000 },
309 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 900000 },
310 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(6), 925000 },
311 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(6), 925000 },
312 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(6), 937500 },
313 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(6), 962500 },
314 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 987500 },
315 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(6), 1000000 },
316 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(11), 1025000 },
317 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(11), 1062500 },
318 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(11), 1062500 },
319 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1087500 },
320 { 0, { 0 } }
321};
322
323static struct acpu_level acpu_freq_tbl_8960_kraitv1_nom_fast[] = {
324 { 0, {STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 862500 },
325 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 862500 },
326 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(6), 862500 },
327 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(6), 887500 },
328 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(6), 900000 },
329 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(6), 925000 },
330 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 925000 },
331 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(6), 937500 },
332 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(11), 962500 },
333 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(11), 1012500 },
334 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(11), 1025000 },
335 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1025000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 { 0, { 0 } }
337};
338
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800339#undef L2
340#define L2(x) (&l2_freq_tbl_8960_kraitv2[(x)])
341static struct l2_level l2_freq_tbl_8960_kraitv2[] = {
342 [0] = { {STBY_KHZ, QSB, 0, 0, 0x00 }, 1050000, 1050000, 0 },
343 [1] = { { 384000, PLL_8, 0, 2, 0x00 }, 1050000, 1050000, 1 },
Stephen Boydf2770c32011-12-07 18:52:30 -0800344 [2] = { { 432000, HFPLL, 2, 0, 0x20 }, 1050000, 1050000, 2 },
345 [3] = { { 486000, HFPLL, 2, 0, 0x24 }, 1050000, 1050000, 2 },
346 [4] = { { 540000, HFPLL, 2, 0, 0x28 }, 1050000, 1050000, 2 },
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800347 [5] = { { 594000, HFPLL, 1, 0, 0x16 }, 1050000, 1050000, 2 },
Stephen Boydf2770c32011-12-07 18:52:30 -0800348 [6] = { { 648000, HFPLL, 1, 0, 0x18 }, 1050000, 1050000, 4 },
349 [7] = { { 702000, HFPLL, 1, 0, 0x1A }, 1050000, 1050000, 4 },
350 [8] = { { 756000, HFPLL, 1, 0, 0x1C }, 1150000, 1150000, 4 },
351 [9] = { { 810000, HFPLL, 1, 0, 0x1E }, 1150000, 1150000, 4 },
352 [10] = { { 864000, HFPLL, 1, 0, 0x20 }, 1150000, 1150000, 4 },
353 [11] = { { 918000, HFPLL, 1, 0, 0x22 }, 1150000, 1150000, 6 },
354 [12] = { { 972000, HFPLL, 1, 0, 0x24 }, 1150000, 1150000, 6 },
355 [13] = { { 1026000, HFPLL, 1, 0, 0x26 }, 1150000, 1150000, 6 },
356 [14] = { { 1080000, HFPLL, 1, 0, 0x28 }, 1150000, 1150000, 6 },
357 [15] = { { 1134000, HFPLL, 1, 0, 0x2A }, 1150000, 1150000, 6 },
358 [16] = { { 1188000, HFPLL, 1, 0, 0x2C }, 1150000, 1150000, 6 },
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800359};
360
Stephen Boyd9d0fab12011-12-08 10:56:06 -0800361static struct acpu_level acpu_freq_tbl_8960_kraitv2_slow[] = {
362 { 0, { STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 900000 },
363 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 900000 },
364 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(6), 925000 },
365 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(6), 925000 },
366 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(6), 937500 },
367 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(6), 962500 },
368 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 987500 },
369 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(6), 1000000 },
370 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(11), 1025000 },
371 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(11), 1062500 },
372 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(11), 1062500 },
373 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1087500 },
374 { 1, { 972000, HFPLL, 1, 0, 0x24 }, L2(16), 1300000 },
375 { 1, { 1026000, HFPLL, 1, 0, 0x26 }, L2(16), 1300000 },
376 { 1, { 1080000, HFPLL, 1, 0, 0x28 }, L2(16), 1300000 },
377 { 1, { 1134000, HFPLL, 1, 0, 0x2A }, L2(16), 1300000 },
378 { 1, { 1188000, HFPLL, 1, 0, 0x2C }, L2(16), 1300000 },
379 { 1, { 1242000, HFPLL, 1, 0, 0x2E }, L2(16), 1300000 },
380 { 1, { 1296000, HFPLL, 1, 0, 0x30 }, L2(16), 1300000 },
381 { 1, { 1350000, HFPLL, 1, 0, 0x32 }, L2(16), 1300000 },
382 { 1, { 1404000, HFPLL, 1, 0, 0x34 }, L2(16), 1300000 },
383 { 1, { 1458000, HFPLL, 1, 0, 0x36 }, L2(16), 1300000 },
384 { 1, { 1512000, HFPLL, 1, 0, 0x38 }, L2(16), 1300000 },
385 { 0, { 0 } }
386};
387
388static struct acpu_level acpu_freq_tbl_8960_kraitv2_nom_fast[] = {
389 { 0, { STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 862500 },
390 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 862500 },
391 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(6), 862500 },
392 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(6), 887500 },
393 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(6), 900000 },
394 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(6), 925000 },
395 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 925000 },
396 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(6), 937500 },
397 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(11), 962500 },
398 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(11), 1012500 },
399 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(11), 1025000 },
400 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1025000 },
Stephen Boyd1be9bf62011-11-21 10:51:46 -0800401 { 1, { 972000, HFPLL, 1, 0, 0x24 }, L2(16), 1300000 },
402 { 1, { 1026000, HFPLL, 1, 0, 0x26 }, L2(16), 1300000 },
403 { 1, { 1080000, HFPLL, 1, 0, 0x28 }, L2(16), 1300000 },
404 { 1, { 1134000, HFPLL, 1, 0, 0x2A }, L2(16), 1300000 },
405 { 1, { 1188000, HFPLL, 1, 0, 0x2C }, L2(16), 1300000 },
406 { 1, { 1242000, HFPLL, 1, 0, 0x2E }, L2(16), 1300000 },
407 { 1, { 1296000, HFPLL, 1, 0, 0x30 }, L2(16), 1300000 },
408 { 1, { 1350000, HFPLL, 1, 0, 0x32 }, L2(16), 1300000 },
409 { 1, { 1404000, HFPLL, 1, 0, 0x34 }, L2(16), 1300000 },
410 { 1, { 1458000, HFPLL, 1, 0, 0x36 }, L2(16), 1300000 },
411 { 1, { 1512000, HFPLL, 1, 0, 0x38 }, L2(16), 1300000 },
412 { 0, { 0 } }
413};
414
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700415/* TODO: Update vdd_dig and vdd_mem when voltage data is available. */
416#undef L2
417#define L2(x) (&l2_freq_tbl_8064[(x)])
418static struct l2_level l2_freq_tbl_8064[] = {
419 [0] = { {STBY_KHZ, QSB, 0, 0, 0x00 }, 1050000, 1050000, 0 },
420 [1] = { { 384000, PLL_8, 0, 2, 0x00 }, 1050000, 1050000, 0 },
421 [2] = { { 432000, HFPLL, 2, 0, 0x20 }, 1050000, 1050000, 1 },
422 [3] = { { 486000, HFPLL, 2, 0, 0x24 }, 1050000, 1050000, 1 },
423 [4] = { { 540000, HFPLL, 2, 0, 0x28 }, 1050000, 1050000, 1 },
424 [5] = { { 594000, HFPLL, 1, 0, 0x16 }, 1050000, 1050000, 2 },
425 [6] = { { 648000, HFPLL, 1, 0, 0x18 }, 1050000, 1050000, 2 },
426 [7] = { { 702000, HFPLL, 1, 0, 0x1A }, 1050000, 1050000, 2 },
427 [8] = { { 756000, HFPLL, 1, 0, 0x1C }, 1150000, 1150000, 3 },
428 [9] = { { 810000, HFPLL, 1, 0, 0x1E }, 1150000, 1150000, 3 },
429 [10] = { { 864000, HFPLL, 1, 0, 0x20 }, 1150000, 1150000, 3 },
430 [11] = { { 918000, HFPLL, 1, 0, 0x22 }, 1150000, 1150000, 3 },
431 [12] = { { 972000, HFPLL, 1, 0, 0x24 }, 1150000, 1150000, 3 },
432 [13] = { { 1026000, HFPLL, 1, 0, 0x26 }, 1150000, 1150000, 3 },
433 [14] = { { 1080000, HFPLL, 1, 0, 0x28 }, 1150000, 1150000, 4 },
434 [15] = { { 1134000, HFPLL, 1, 0, 0x2A }, 1150000, 1150000, 4 },
435 [16] = { { 1188000, HFPLL, 1, 0, 0x2C }, 1150000, 1150000, 4 },
436 [17] = { { 1242000, HFPLL, 1, 0, 0x2E }, 1150000, 1150000, 4 },
437 [18] = { { 1296000, HFPLL, 1, 0, 0x30 }, 1150000, 1150000, 4 },
438 [19] = { { 1350000, HFPLL, 1, 0, 0x32 }, 1150000, 1150000, 4 },
439 [20] = { { 1404000, HFPLL, 1, 0, 0x34 }, 1150000, 1150000, 4 },
440 [21] = { { 1458000, HFPLL, 1, 0, 0x36 }, 1150000, 1150000, 5 },
441 [22] = { { 1512000, HFPLL, 1, 0, 0x38 }, 1150000, 1150000, 5 },
442 [23] = { { 1566000, HFPLL, 1, 0, 0x3A }, 1150000, 1150000, 5 },
443 [24] = { { 1620000, HFPLL, 1, 0, 0x3C }, 1150000, 1150000, 5 },
444 [25] = { { 1674000, HFPLL, 1, 0, 0x3E }, 1150000, 1150000, 5 },
445};
446
447/* TODO: Update core voltages when data is available. */
448static struct acpu_level acpu_freq_tbl_8064[] = {
449 { 0, {STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 1050000 },
450 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 1050000 },
451 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(2), 1050000 },
452 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(3), 1050000 },
453 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(4), 1050000 },
454 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(5), 1050000 },
455 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 1050000 },
456 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(7), 1050000 },
457 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(8), 1150000 },
458 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(9), 1150000 },
459 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(10), 1150000 },
460 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1150000 },
461 { 0, { 0 } }
462};
463
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700464static unsigned long acpuclk_8960_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465{
466 return scalable[cpu].current_speed->khz;
467}
468
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469/* Get the selected source on primary MUX. */
470static int get_pri_clk_src(struct scalable *sc)
471{
472 uint32_t regval;
473
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700474 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 return regval & 0x3;
476}
477
478/* Set the selected source on primary MUX. */
479static void set_pri_clk_src(struct scalable *sc, uint32_t pri_src_sel)
480{
481 uint32_t regval;
482
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700483 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 regval &= ~0x3;
485 regval |= (pri_src_sel & 0x3);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700486 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 /* Wait for switch to complete. */
488 mb();
489 udelay(1);
490}
491
492/* Get the selected source on secondary MUX. */
493static int get_sec_clk_src(struct scalable *sc)
494{
495 uint32_t regval;
496
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700497 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498 return (regval >> 2) & 0x3;
499}
500
501/* Set the selected source on secondary MUX. */
502static void set_sec_clk_src(struct scalable *sc, uint32_t sec_src_sel)
503{
504 uint32_t regval;
505
506 /* Disable secondary source clock gating during switch. */
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700507 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508 regval |= SECCLKAGD;
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700509 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700510
511 /* Program the MUX. */
512 regval &= ~(0x3 << 2);
513 regval |= ((sec_src_sel & 0x3) << 2);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700514 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515
516 /* Wait for switch to complete. */
517 mb();
518 udelay(1);
Stephen Boyd753b5092011-10-17 19:14:12 -0700519
520 /* Re-enable secondary source clock gating. */
521 regval &= ~SECCLKAGD;
522 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523}
524
525/* Enable an already-configured HFPLL. */
526static void hfpll_enable(struct scalable *sc)
527{
Matt Wagantallcb12c392011-10-19 10:32:07 -0700528 int rc;
529
530 if (cpu_is_msm8960()) {
531 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_A].rpm_vreg_id,
Matt Wagantall627f4312011-12-13 13:33:47 -0800532 sc->vreg[VREG_HFPLL_A].rpm_vreg_voter, 2100000,
533 sc->vreg[VREG_HFPLL_A].max_vdd, 0);
Matt Wagantallcb12c392011-10-19 10:32:07 -0700534 if (rc)
535 pr_err("%s regulator enable failed (%d)\n",
536 sc->vreg[VREG_HFPLL_A].name, rc);
537 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_B].rpm_vreg_id,
538 sc->vreg[VREG_HFPLL_B].rpm_vreg_voter, 1800000,
Matt Wagantall627f4312011-12-13 13:33:47 -0800539 sc->vreg[VREG_HFPLL_B].max_vdd, 0);
Matt Wagantallcb12c392011-10-19 10:32:07 -0700540 if (rc)
541 pr_err("%s regulator enable failed (%d)\n",
542 sc->vreg[VREG_HFPLL_B].name, rc);
543 }
544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 /* Disable PLL bypass mode. */
546 writel_relaxed(0x2, sc->hfpll_base + HFPLL_MODE);
547
548 /*
549 * H/W requires a 5us delay between disabling the bypass and
550 * de-asserting the reset. Delay 10us just to be safe.
551 */
552 mb();
553 udelay(10);
554
555 /* De-assert active-low PLL reset. */
556 writel_relaxed(0x6, sc->hfpll_base + HFPLL_MODE);
557
558 /* Wait for PLL to lock. */
559 mb();
560 udelay(60);
561
562 /* Enable PLL output. */
563 writel_relaxed(0x7, sc->hfpll_base + HFPLL_MODE);
564}
565
566/* Disable a HFPLL for power-savings or while its being reprogrammed. */
567static void hfpll_disable(struct scalable *sc)
568{
Matt Wagantallcb12c392011-10-19 10:32:07 -0700569 int rc;
570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571 /*
572 * Disable the PLL output, disable test mode, enable
573 * the bypass mode, and assert the reset.
574 */
575 writel_relaxed(0, sc->hfpll_base + HFPLL_MODE);
Matt Wagantallcb12c392011-10-19 10:32:07 -0700576
577 if (cpu_is_msm8960()) {
578 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_B].rpm_vreg_id,
579 sc->vreg[VREG_HFPLL_B].rpm_vreg_voter, 0,
580 0, 0);
581 if (rc)
582 pr_err("%s regulator enable failed (%d)\n",
583 sc->vreg[VREG_HFPLL_B].name, rc);
584 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_A].rpm_vreg_id,
585 sc->vreg[VREG_HFPLL_A].rpm_vreg_voter, 0,
586 0, 0);
587 if (rc)
588 pr_err("%s regulator enable failed (%d)\n",
589 sc->vreg[VREG_HFPLL_A].name, rc);
590 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591}
592
593/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
594static void hfpll_set_rate(struct scalable *sc, struct core_speed *tgt_s)
595{
596 writel_relaxed(tgt_s->pll_l_val, sc->hfpll_base + HFPLL_L_VAL);
597}
598
599/* Return the L2 speed that should be applied. */
600static struct l2_level *compute_l2_level(struct scalable *sc,
601 struct l2_level *vote_l)
602{
603 struct l2_level *new_l;
604 int cpu;
605
606 /* Bounds check. */
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700607 BUG_ON(vote_l >= (l2_freq_tbl + l2_freq_tbl_size));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608
609 /* Find max L2 speed vote. */
610 sc->l2_vote = vote_l;
611 new_l = l2_freq_tbl;
612 for_each_present_cpu(cpu)
613 new_l = max(new_l, scalable[cpu].l2_vote);
614
615 return new_l;
616}
617
618/* Update the bus bandwidth request. */
619static void set_bus_bw(unsigned int bw)
620{
621 int ret;
622
623 /* Bounds check. */
624 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
625 pr_err("invalid bandwidth request (%d)\n", bw);
626 return;
627 }
628
629 /* Update bandwidth if request has changed. This may sleep. */
630 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
631 if (ret)
632 pr_err("bandwidth request failed (%d)\n", ret);
633}
634
635/* Set the CPU or L2 clock speed. */
636static void set_speed(struct scalable *sc, struct core_speed *tgt_s,
637 enum setrate_reason reason)
638{
639 struct core_speed *strt_s = sc->current_speed;
640
641 if (tgt_s == strt_s)
642 return;
643
644 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 /*
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700646 * Move to an always-on source running at a frequency that does
647 * not require an elevated CPU voltage. PLL8 is used here.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 */
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700649 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
651
652 /* Program CPU HFPLL. */
653 hfpll_disable(sc);
654 hfpll_set_rate(sc, tgt_s);
655 hfpll_enable(sc);
656
657 /* Move CPU to HFPLL source. */
658 set_pri_clk_src(sc, tgt_s->pri_src_sel);
659 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 /*
661 * If responding to CPU_DEAD we must be running on another
662 * CPU. Therefore, we can't access the downed CPU's CP15
663 * clock MUX registers from here and can't change clock sources.
664 * Just turn off the PLL- since the CPU is down already, halting
665 * its clock should be safe.
666 */
667 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2]) {
668 set_sec_clk_src(sc, tgt_s->sec_src_sel);
669 set_pri_clk_src(sc, tgt_s->pri_src_sel);
670 }
671 hfpll_disable(sc);
672 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
673 hfpll_set_rate(sc, tgt_s);
674 hfpll_enable(sc);
675 /*
676 * If responding to CPU_UP_PREPARE, we can't change CP15
677 * registers for the CPU that's coming up since we're not
678 * running on that CPU. That's okay though, since the MUX
679 * source was not changed on the way down, either.
680 */
681 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2])
682 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700683 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2])
685 set_sec_clk_src(sc, tgt_s->sec_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686 }
687
688 sc->current_speed = tgt_s;
689}
690
691/* Apply any per-cpu voltage increases. */
692static int increase_vdd(int cpu, unsigned int vdd_core, unsigned int vdd_mem,
693 unsigned int vdd_dig, enum setrate_reason reason)
694{
695 struct scalable *sc = &scalable[cpu];
Saravana Kannan9dcb89f2011-09-26 19:02:22 -0700696 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697
698 /*
Matt Wagantallabd55f02011-09-12 11:45:54 -0700699 * Increase vdd_mem active-set before vdd_dig.
700 * vdd_mem should be >= vdd_dig.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701 */
702 if (vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
703 rc = rpm_vreg_set_voltage(sc->vreg[VREG_MEM].rpm_vreg_id,
704 sc->vreg[VREG_MEM].rpm_vreg_voter, vdd_mem,
705 sc->vreg[VREG_MEM].max_vdd, 0);
706 if (rc) {
707 pr_err("%s: vdd_mem (cpu%d) increase failed (%d)\n",
708 __func__, cpu, rc);
709 return rc;
710 }
711 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
712 }
713
714 /* Increase vdd_dig active-set vote. */
715 if (vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
716 rc = rpm_vreg_set_voltage(sc->vreg[VREG_DIG].rpm_vreg_id,
717 sc->vreg[VREG_DIG].rpm_vreg_voter, vdd_dig,
718 sc->vreg[VREG_DIG].max_vdd, 0);
719 if (rc) {
720 pr_err("%s: vdd_dig (cpu%d) increase failed (%d)\n",
721 __func__, cpu, rc);
722 return rc;
723 }
724 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
725 }
726
727 /*
728 * Update per-CPU core voltage. Don't do this for the hotplug path for
729 * which it should already be correct. Attempting to set it is bad
730 * because we don't know what CPU we are running on at this point, but
731 * the CPU regulator API requires we call it from the affected CPU.
732 */
733 if (vdd_core > sc->vreg[VREG_CORE].cur_vdd
734 && reason != SETRATE_HOTPLUG) {
735 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
736 sc->vreg[VREG_CORE].max_vdd);
737 if (rc) {
738 pr_err("%s: vdd_core (cpu%d) increase failed (%d)\n",
739 __func__, cpu, rc);
740 return rc;
741 }
742 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
743 }
744
745 return rc;
746}
747
748/* Apply any per-cpu voltage decreases. */
749static void decrease_vdd(int cpu, unsigned int vdd_core, unsigned int vdd_mem,
750 unsigned int vdd_dig, enum setrate_reason reason)
751{
752 struct scalable *sc = &scalable[cpu];
753 int ret;
754
755 /*
756 * Update per-CPU core voltage. This must be called on the CPU
757 * that's being affected. Don't do this in the hotplug remove path,
758 * where the rail is off and we're executing on the other CPU.
759 */
760 if (vdd_core < sc->vreg[VREG_CORE].cur_vdd
761 && reason != SETRATE_HOTPLUG) {
762 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
763 sc->vreg[VREG_CORE].max_vdd);
764 if (ret) {
765 pr_err("%s: vdd_core (cpu%d) decrease failed (%d)\n",
766 __func__, cpu, ret);
767 return;
768 }
769 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
770 }
771
772 /* Decrease vdd_dig active-set vote. */
773 if (vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
774 ret = rpm_vreg_set_voltage(sc->vreg[VREG_DIG].rpm_vreg_id,
775 sc->vreg[VREG_DIG].rpm_vreg_voter, vdd_dig,
776 sc->vreg[VREG_DIG].max_vdd, 0);
777 if (ret) {
778 pr_err("%s: vdd_dig (cpu%d) decrease failed (%d)\n",
779 __func__, cpu, ret);
780 return;
781 }
782 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
783 }
784
785 /*
Matt Wagantallabd55f02011-09-12 11:45:54 -0700786 * Decrease vdd_mem active-set after vdd_dig.
787 * vdd_mem should be >= vdd_dig.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 */
789 if (vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
790 ret = rpm_vreg_set_voltage(sc->vreg[VREG_MEM].rpm_vreg_id,
791 sc->vreg[VREG_MEM].rpm_vreg_voter, vdd_mem,
792 sc->vreg[VREG_MEM].max_vdd, 0);
793 if (ret) {
794 pr_err("%s: vdd_mem (cpu%d) decrease failed (%d)\n",
795 __func__, cpu, ret);
796 return;
797 }
798 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
799 }
800}
801
802static unsigned int calculate_vdd_mem(struct acpu_level *tgt)
803{
Matt Wagantallabd55f02011-09-12 11:45:54 -0700804 return tgt->l2_level->vdd_mem;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805}
806
807static unsigned int calculate_vdd_dig(struct acpu_level *tgt)
808{
809 unsigned int pll_vdd_dig;
810
Stephen Boydc76158f2011-12-08 12:42:40 -0800811 if (tgt->l2_level->speed.src != HFPLL)
812 pll_vdd_dig = 0;
813 else if (tgt->l2_level->speed.pll_l_val > HFPLL_LOW_VDD_PLL_L_MAX)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 pll_vdd_dig = HFPLL_NOMINAL_VDD;
815 else
816 pll_vdd_dig = HFPLL_LOW_VDD;
817
818 return max(tgt->l2_level->vdd_dig, pll_vdd_dig);
819}
820
821static unsigned int calculate_vdd_core(struct acpu_level *tgt)
822{
823 unsigned int pll_vdd_core;
824
Stephen Boydc76158f2011-12-08 12:42:40 -0800825 if (tgt->speed.src != HFPLL)
826 pll_vdd_core = 0;
827 else if (tgt->speed.pll_l_val > HFPLL_LOW_VDD_PLL_L_MAX)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 pll_vdd_core = HFPLL_NOMINAL_VDD;
829 else
830 pll_vdd_core = HFPLL_LOW_VDD;
831
832 return max(tgt->vdd_core, pll_vdd_core);
833}
834
835/* Set the CPU's clock rate and adjust the L2 rate, if appropriate. */
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700836static int acpuclk_8960_set_rate(int cpu, unsigned long rate,
837 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700838{
839 struct core_speed *strt_acpu_s, *tgt_acpu_s;
840 struct l2_level *tgt_l2_l;
841 struct acpu_level *tgt;
842 unsigned int vdd_mem, vdd_dig, vdd_core;
843 unsigned long flags;
844 int rc = 0;
845
846 if (cpu > num_possible_cpus()) {
847 rc = -EINVAL;
848 goto out;
849 }
850
851 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
852 mutex_lock(&driver_lock);
853
854 strt_acpu_s = scalable[cpu].current_speed;
855
856 /* Return early if rate didn't change. */
857 if (rate == strt_acpu_s->khz && scalable[cpu].first_set_call == false)
858 goto out;
859
860 /* Find target frequency. */
861 for (tgt = acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
862 if (tgt->speed.khz == rate) {
863 tgt_acpu_s = &tgt->speed;
864 break;
865 }
866 }
867 if (tgt->speed.khz == 0) {
868 rc = -EINVAL;
869 goto out;
870 }
871
872 /* Calculate voltage requirements for the current CPU. */
873 vdd_mem = calculate_vdd_mem(tgt);
874 vdd_dig = calculate_vdd_dig(tgt);
875 vdd_core = calculate_vdd_core(tgt);
876
877 /* Increase VDD levels if needed. */
878 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
879 rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
880 if (rc)
881 goto out;
882 }
883
884 pr_debug("Switching from ACPU%d rate %u KHz -> %u KHz\n",
885 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
886
887 /* Set the CPU speed. */
888 set_speed(&scalable[cpu], tgt_acpu_s, reason);
889
890 /*
891 * Update the L2 vote and apply the rate change. A spinlock is
892 * necessary to ensure L2 rate is calulated and set atomically,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700893 * even if acpuclk_8960_set_rate() is called from an atomic context
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 * and the driver_lock mutex is not acquired.
895 */
896 spin_lock_irqsave(&l2_lock, flags);
897 tgt_l2_l = compute_l2_level(&scalable[cpu], tgt->l2_level);
898 set_speed(&scalable[L2], &tgt_l2_l->speed, reason);
899 spin_unlock_irqrestore(&l2_lock, flags);
900
901 /* Nothing else to do for power collapse or SWFI. */
902 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
903 goto out;
904
905 /* Update bus bandwith request. */
906 set_bus_bw(tgt_l2_l->bw_level);
907
908 /* Drop VDD levels if we can. */
909 decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
910
911 scalable[cpu].first_set_call = false;
912 pr_debug("ACPU%d speed change complete\n", cpu);
913
914out:
915 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
916 mutex_unlock(&driver_lock);
917 return rc;
918}
919
920/* Initialize a HFPLL at a given rate and enable it. */
921static void __init hfpll_init(struct scalable *sc, struct core_speed *tgt_s)
922{
923 pr_debug("Initializing HFPLL%d\n", sc - scalable);
924
925 /* Disable the PLL for re-programming. */
926 hfpll_disable(sc);
927
928 /* Configure PLL parameters for integer mode. */
929 writel_relaxed(0x7845C665, sc->hfpll_base + HFPLL_CONFIG_CTL);
930 writel_relaxed(0, sc->hfpll_base + HFPLL_M_VAL);
931 writel_relaxed(1, sc->hfpll_base + HFPLL_N_VAL);
932
933 /* Program droop controller. */
934 writel_relaxed(0x0108C000, sc->hfpll_base + HFPLL_DROOP_CTL);
935
936 /* Set an initial rate and enable the PLL. */
937 hfpll_set_rate(sc, tgt_s);
938 hfpll_enable(sc);
939}
940
941/* Voltage regulator initialization. */
942static void __init regulator_init(void)
943{
944 int cpu, ret;
945 struct scalable *sc;
946
947 for_each_possible_cpu(cpu) {
948 sc = &scalable[cpu];
949 sc->vreg[VREG_CORE].reg = regulator_get(NULL,
950 sc->vreg[VREG_CORE].name);
951 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
952 pr_err("regulator_get(%s) failed (%ld)\n",
953 sc->vreg[VREG_CORE].name,
954 PTR_ERR(sc->vreg[VREG_CORE].reg));
955 BUG();
956 }
957
958 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
959 sc->vreg[VREG_CORE].max_vdd,
960 sc->vreg[VREG_CORE].max_vdd);
961 if (ret)
962 pr_err("regulator_set_voltage(%s) failed"
963 " (%d)\n", sc->vreg[VREG_CORE].name, ret);
964
965 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
966 if (ret)
967 pr_err("regulator_enable(%s) failed (%d)\n",
968 sc->vreg[VREG_CORE].name, ret);
969 }
970}
971
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972/* Set initial rate for a given core. */
973static void __init init_clock_sources(struct scalable *sc,
974 struct core_speed *tgt_s)
975{
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700976 uint32_t regval;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700978 /* Select PLL8 as AUX source input to the secondary MUX. */
979 writel_relaxed(0x3, sc->aux_clk_sel);
980
981 /* Switch away from the HFPLL while it's re-initialized. */
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700982 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700983 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700984 hfpll_init(sc, tgt_s);
985
986 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700987 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988 regval &= ~(0x3 << 6);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700989 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700991 /* Switch to the target clock source. */
992 set_sec_clk_src(sc, tgt_s->sec_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 set_pri_clk_src(sc, tgt_s->pri_src_sel);
994 sc->current_speed = tgt_s;
995
996 /*
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700997 * Set this flag so that the first call to acpuclk_8960_set_rate() can
998 * drop voltages and set initial bus bandwidth requests.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999 */
1000 sc->first_set_call = true;
1001}
1002
Matt Wagantall8e726c72011-08-06 00:49:28 -07001003static void __init per_cpu_init(void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004{
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001005 struct acpu_level *max_acpu_level = data;
Matt Wagantall8e726c72011-08-06 00:49:28 -07001006 int cpu = smp_processor_id();
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001007
1008 init_clock_sources(&scalable[cpu], &max_acpu_level->speed);
1009 scalable[cpu].l2_vote = max_acpu_level->l2_level;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010}
1011
1012/* Register with bus driver. */
1013static void __init bus_init(void)
1014{
1015 int ret;
1016
1017 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
1018 if (!bus_perf_client) {
1019 pr_err("unable to register bus client\n");
1020 BUG();
1021 }
1022
1023 ret = msm_bus_scale_client_update_request(bus_perf_client,
1024 (ARRAY_SIZE(bw_level_tbl)-1));
1025 if (ret)
1026 pr_err("initial bandwidth request failed (%d)\n", ret);
1027}
1028
1029#ifdef CONFIG_CPU_FREQ_MSM
1030static struct cpufreq_frequency_table freq_table[NR_CPUS][30];
1031
1032static void __init cpufreq_table_init(void)
1033{
1034 int cpu;
1035
1036 for_each_possible_cpu(cpu) {
1037 int i, freq_cnt = 0;
1038 /* Construct the freq_table tables from acpu_freq_tbl. */
1039 for (i = 0; acpu_freq_tbl[i].speed.khz != 0
1040 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
1041 if (acpu_freq_tbl[i].use_for_scaling) {
1042 freq_table[cpu][freq_cnt].index = freq_cnt;
1043 freq_table[cpu][freq_cnt].frequency
1044 = acpu_freq_tbl[i].speed.khz;
1045 freq_cnt++;
1046 }
1047 }
1048 /* freq_table not big enough to store all usable freqs. */
1049 BUG_ON(acpu_freq_tbl[i].speed.khz != 0);
1050
1051 freq_table[cpu][freq_cnt].index = freq_cnt;
1052 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
1053
1054 pr_info("CPU%d: %d scaling frequencies supported.\n",
1055 cpu, freq_cnt);
1056
1057 /* Register table with CPUFreq. */
1058 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
1059 }
1060}
1061#else
1062static void __init cpufreq_table_init(void) {}
1063#endif
1064
1065#define HOT_UNPLUG_KHZ STBY_KHZ
1066static int __cpuinit acpuclock_cpu_callback(struct notifier_block *nfb,
1067 unsigned long action, void *hcpu)
1068{
1069 static int prev_khz[NR_CPUS];
1070 static int prev_pri_src[NR_CPUS];
1071 static int prev_sec_src[NR_CPUS];
1072 int cpu = (int)hcpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001073
1074 switch (action) {
1075 case CPU_DYING:
1076 case CPU_DYING_FROZEN:
1077 /*
Matt Wagantall27663842011-08-25 15:11:48 -07001078 * On Krait v1, the primary and secondary muxes must be set
1079 * to QSB before L2 power collapse and restored after.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 */
Matt Wagantall27663842011-08-25 15:11:48 -07001081 if (cpu_is_krait_v1()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 prev_sec_src[cpu] = get_sec_clk_src(&scalable[cpu]);
1083 prev_pri_src[cpu] = get_pri_clk_src(&scalable[cpu]);
1084 set_sec_clk_src(&scalable[cpu], SEC_SRC_SEL_QSB);
1085 set_pri_clk_src(&scalable[cpu], PRI_SRC_SEL_SEC_SRC);
1086 }
1087 break;
1088 case CPU_DEAD:
1089 case CPU_DEAD_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001090 prev_khz[cpu] = acpuclk_8960_get_rate(cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 /* Fall through. */
1092 case CPU_UP_CANCELED:
1093 case CPU_UP_CANCELED_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001094 acpuclk_8960_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001095 break;
1096 case CPU_UP_PREPARE:
1097 case CPU_UP_PREPARE_FROZEN:
1098 if (WARN_ON(!prev_khz[cpu]))
1099 prev_khz[cpu] = acpu_freq_tbl->speed.khz;
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001100 acpuclk_8960_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001101 break;
1102 case CPU_STARTING:
1103 case CPU_STARTING_FROZEN:
Matt Wagantall27663842011-08-25 15:11:48 -07001104 if (cpu_is_krait_v1()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 set_sec_clk_src(&scalable[cpu], prev_sec_src[cpu]);
1106 set_pri_clk_src(&scalable[cpu], prev_pri_src[cpu]);
1107 }
1108 break;
1109 default:
1110 break;
1111 }
1112
1113 return NOTIFY_OK;
1114}
1115
1116static struct notifier_block __cpuinitdata acpuclock_cpu_notifier = {
1117 .notifier_call = acpuclock_cpu_callback,
1118};
1119
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001120static struct acpu_level * __init select_freq_plan(void)
1121{
1122 struct acpu_level *l, *max_acpu_level = NULL;
1123
1124 /* Select frequency tables. */
Stepan Moskovchenko532008c2011-10-25 14:43:49 -07001125 if (cpu_is_msm8960() || cpu_is_msm8930()) {
Matt Wagantalla518f8f2011-10-17 13:24:53 -07001126 uint32_t pte_efuse, pvs;
Stephen Boyd9d0fab12011-12-08 10:56:06 -08001127 struct acpu_level *v1, *v2;
Matt Wagantalla518f8f2011-10-17 13:24:53 -07001128
1129 pte_efuse = readl_relaxed(QFPROM_PTE_EFUSE_ADDR);
1130 pvs = (pte_efuse >> 10) & 0x7;
1131 if (pvs == 0x7)
1132 pvs = (pte_efuse >> 13) & 0x7;
1133
1134 switch (pvs) {
1135 case 0x0:
1136 case 0x7:
1137 pr_info("ACPU PVS: Slow\n");
Stephen Boyd9d0fab12011-12-08 10:56:06 -08001138 v1 = acpu_freq_tbl_8960_kraitv1_slow;
1139 v2 = acpu_freq_tbl_8960_kraitv2_slow;
Matt Wagantalla518f8f2011-10-17 13:24:53 -07001140 break;
1141 case 0x1:
1142 pr_info("ACPU PVS: Nominal\n");
Stephen Boyd9d0fab12011-12-08 10:56:06 -08001143 v1 = acpu_freq_tbl_8960_kraitv1_nom_fast;
1144 v2 = acpu_freq_tbl_8960_kraitv2_nom_fast;
Matt Wagantalla518f8f2011-10-17 13:24:53 -07001145 break;
1146 case 0x3:
1147 pr_info("ACPU PVS: Fast\n");
Stephen Boyd9d0fab12011-12-08 10:56:06 -08001148 v1 = acpu_freq_tbl_8960_kraitv1_nom_fast;
1149 v2 = acpu_freq_tbl_8960_kraitv2_nom_fast;
Matt Wagantalla518f8f2011-10-17 13:24:53 -07001150 break;
1151 default:
1152 pr_warn("ACPU PVS: Unknown. Defaulting to slow.\n");
Stephen Boyd9d0fab12011-12-08 10:56:06 -08001153 v1 = acpu_freq_tbl_8960_kraitv1_slow;
1154 v2 = acpu_freq_tbl_8960_kraitv2_slow;
Matt Wagantalla518f8f2011-10-17 13:24:53 -07001155 break;
1156 }
1157
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001158 scalable = scalable_8960;
Stephen Boyd1be9bf62011-11-21 10:51:46 -08001159 if (cpu_is_krait_v1()) {
Stephen Boyd9d0fab12011-12-08 10:56:06 -08001160 acpu_freq_tbl = v1;
Stephen Boyd1be9bf62011-11-21 10:51:46 -08001161 l2_freq_tbl = l2_freq_tbl_8960_kraitv1;
1162 l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_8960_kraitv1);
1163 } else {
Stephen Boyd9d0fab12011-12-08 10:56:06 -08001164 acpu_freq_tbl = v2;
Stephen Boyd1be9bf62011-11-21 10:51:46 -08001165 l2_freq_tbl = l2_freq_tbl_8960_kraitv2;
1166 l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_8960_kraitv2);
1167 }
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001168 } else if (cpu_is_apq8064()) {
1169 scalable = scalable_8064;
1170 acpu_freq_tbl = acpu_freq_tbl_8064;
1171 l2_freq_tbl = l2_freq_tbl_8064;
1172 l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_8064);
1173 } else {
1174 BUG();
1175 }
1176
1177 /* Find the max supported scaling frequency. */
1178 for (l = acpu_freq_tbl; l->speed.khz != 0; l++)
1179 if (l->use_for_scaling)
1180 max_acpu_level = l;
1181 BUG_ON(!max_acpu_level);
1182 pr_info("Max ACPU freq: %u KHz\n", max_acpu_level->speed.khz);
1183
1184 return max_acpu_level;
1185}
1186
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001187static struct acpuclk_data acpuclk_8960_data = {
1188 .set_rate = acpuclk_8960_set_rate,
1189 .get_rate = acpuclk_8960_get_rate,
1190 .power_collapse_khz = STBY_KHZ,
1191 .wait_for_irq_khz = STBY_KHZ,
1192};
1193
Matt Wagantallec57f062011-08-16 23:54:46 -07001194static int __init acpuclk_8960_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195{
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001196 struct acpu_level *max_acpu_level = select_freq_plan();
1197 init_clock_sources(&scalable[L2], &max_acpu_level->l2_level->speed);
1198 on_each_cpu(per_cpu_init, max_acpu_level, true);
Matt Wagantall8e726c72011-08-06 00:49:28 -07001199
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 regulator_init();
1201 bus_init();
1202 cpufreq_table_init();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001203
1204 acpuclk_register(&acpuclk_8960_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205 register_hotcpu_notifier(&acpuclock_cpu_notifier);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001206
1207 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208}
Matt Wagantallec57f062011-08-16 23:54:46 -07001209
1210struct acpuclk_soc_data acpuclk_8960_soc_data __initdata = {
1211 .init = acpuclk_8960_init,
1212};