blob: 659c29263778882a0e880e226a4e4a19974ba159 [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,
143 .vreg[VREG_CORE] = { "krait0", 1150000 },
144 .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,
161 .vreg[VREG_CORE] = { "krait1", 1150000 },
162 .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. */
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700290#define L2(x) (&l2_freq_tbl_8960[(x)])
291static struct l2_level l2_freq_tbl_8960[] = {
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
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700306static struct acpu_level acpu_freq_tbl_8960[] = {
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
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700322/* TODO: Update vdd_dig and vdd_mem when voltage data is available. */
323#undef L2
324#define L2(x) (&l2_freq_tbl_8064[(x)])
325static struct l2_level l2_freq_tbl_8064[] = {
326 [0] = { {STBY_KHZ, QSB, 0, 0, 0x00 }, 1050000, 1050000, 0 },
327 [1] = { { 384000, PLL_8, 0, 2, 0x00 }, 1050000, 1050000, 0 },
328 [2] = { { 432000, HFPLL, 2, 0, 0x20 }, 1050000, 1050000, 1 },
329 [3] = { { 486000, HFPLL, 2, 0, 0x24 }, 1050000, 1050000, 1 },
330 [4] = { { 540000, HFPLL, 2, 0, 0x28 }, 1050000, 1050000, 1 },
331 [5] = { { 594000, HFPLL, 1, 0, 0x16 }, 1050000, 1050000, 2 },
332 [6] = { { 648000, HFPLL, 1, 0, 0x18 }, 1050000, 1050000, 2 },
333 [7] = { { 702000, HFPLL, 1, 0, 0x1A }, 1050000, 1050000, 2 },
334 [8] = { { 756000, HFPLL, 1, 0, 0x1C }, 1150000, 1150000, 3 },
335 [9] = { { 810000, HFPLL, 1, 0, 0x1E }, 1150000, 1150000, 3 },
336 [10] = { { 864000, HFPLL, 1, 0, 0x20 }, 1150000, 1150000, 3 },
337 [11] = { { 918000, HFPLL, 1, 0, 0x22 }, 1150000, 1150000, 3 },
338 [12] = { { 972000, HFPLL, 1, 0, 0x24 }, 1150000, 1150000, 3 },
339 [13] = { { 1026000, HFPLL, 1, 0, 0x26 }, 1150000, 1150000, 3 },
340 [14] = { { 1080000, HFPLL, 1, 0, 0x28 }, 1150000, 1150000, 4 },
341 [15] = { { 1134000, HFPLL, 1, 0, 0x2A }, 1150000, 1150000, 4 },
342 [16] = { { 1188000, HFPLL, 1, 0, 0x2C }, 1150000, 1150000, 4 },
343 [17] = { { 1242000, HFPLL, 1, 0, 0x2E }, 1150000, 1150000, 4 },
344 [18] = { { 1296000, HFPLL, 1, 0, 0x30 }, 1150000, 1150000, 4 },
345 [19] = { { 1350000, HFPLL, 1, 0, 0x32 }, 1150000, 1150000, 4 },
346 [20] = { { 1404000, HFPLL, 1, 0, 0x34 }, 1150000, 1150000, 4 },
347 [21] = { { 1458000, HFPLL, 1, 0, 0x36 }, 1150000, 1150000, 5 },
348 [22] = { { 1512000, HFPLL, 1, 0, 0x38 }, 1150000, 1150000, 5 },
349 [23] = { { 1566000, HFPLL, 1, 0, 0x3A }, 1150000, 1150000, 5 },
350 [24] = { { 1620000, HFPLL, 1, 0, 0x3C }, 1150000, 1150000, 5 },
351 [25] = { { 1674000, HFPLL, 1, 0, 0x3E }, 1150000, 1150000, 5 },
352};
353
354/* TODO: Update core voltages when data is available. */
355static struct acpu_level acpu_freq_tbl_8064[] = {
356 { 0, {STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 1050000 },
357 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 1050000 },
358 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(2), 1050000 },
359 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(3), 1050000 },
360 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(4), 1050000 },
361 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(5), 1050000 },
362 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 1050000 },
363 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(7), 1050000 },
364 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(8), 1150000 },
365 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(9), 1150000 },
366 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(10), 1150000 },
367 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(11), 1150000 },
368 { 0, { 0 } }
369};
370
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700371static unsigned long acpuclk_8960_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372{
373 return scalable[cpu].current_speed->khz;
374}
375
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376/* Get the selected source on primary MUX. */
377static int get_pri_clk_src(struct scalable *sc)
378{
379 uint32_t regval;
380
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700381 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382 return regval & 0x3;
383}
384
385/* Set the selected source on primary MUX. */
386static void set_pri_clk_src(struct scalable *sc, uint32_t pri_src_sel)
387{
388 uint32_t regval;
389
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700390 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391 regval &= ~0x3;
392 regval |= (pri_src_sel & 0x3);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700393 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700394 /* Wait for switch to complete. */
395 mb();
396 udelay(1);
397}
398
399/* Get the selected source on secondary MUX. */
400static int get_sec_clk_src(struct scalable *sc)
401{
402 uint32_t regval;
403
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700404 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405 return (regval >> 2) & 0x3;
406}
407
408/* Set the selected source on secondary MUX. */
409static void set_sec_clk_src(struct scalable *sc, uint32_t sec_src_sel)
410{
411 uint32_t regval;
412
413 /* Disable secondary source clock gating during switch. */
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700414 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700415 regval |= SECCLKAGD;
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700416 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700417
418 /* Program the MUX. */
419 regval &= ~(0x3 << 2);
420 regval |= ((sec_src_sel & 0x3) << 2);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700421 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700422
423 /* Wait for switch to complete. */
424 mb();
425 udelay(1);
Stephen Boyd753b5092011-10-17 19:14:12 -0700426
427 /* Re-enable secondary source clock gating. */
428 regval &= ~SECCLKAGD;
429 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700430}
431
432/* Enable an already-configured HFPLL. */
433static void hfpll_enable(struct scalable *sc)
434{
Matt Wagantallcb12c392011-10-19 10:32:07 -0700435 int rc;
436
437 if (cpu_is_msm8960()) {
438 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_A].rpm_vreg_id,
439 sc->vreg[VREG_HFPLL_A].rpm_vreg_voter, 2200000,
440 2200000, 0);
441 if (rc)
442 pr_err("%s regulator enable failed (%d)\n",
443 sc->vreg[VREG_HFPLL_A].name, rc);
444 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_B].rpm_vreg_id,
445 sc->vreg[VREG_HFPLL_B].rpm_vreg_voter, 1800000,
446 1800000, 0);
447 if (rc)
448 pr_err("%s regulator enable failed (%d)\n",
449 sc->vreg[VREG_HFPLL_B].name, rc);
450 }
451
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452 /* Disable PLL bypass mode. */
453 writel_relaxed(0x2, sc->hfpll_base + HFPLL_MODE);
454
455 /*
456 * H/W requires a 5us delay between disabling the bypass and
457 * de-asserting the reset. Delay 10us just to be safe.
458 */
459 mb();
460 udelay(10);
461
462 /* De-assert active-low PLL reset. */
463 writel_relaxed(0x6, sc->hfpll_base + HFPLL_MODE);
464
465 /* Wait for PLL to lock. */
466 mb();
467 udelay(60);
468
469 /* Enable PLL output. */
470 writel_relaxed(0x7, sc->hfpll_base + HFPLL_MODE);
471}
472
473/* Disable a HFPLL for power-savings or while its being reprogrammed. */
474static void hfpll_disable(struct scalable *sc)
475{
Matt Wagantallcb12c392011-10-19 10:32:07 -0700476 int rc;
477
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700478 /*
479 * Disable the PLL output, disable test mode, enable
480 * the bypass mode, and assert the reset.
481 */
482 writel_relaxed(0, sc->hfpll_base + HFPLL_MODE);
Matt Wagantallcb12c392011-10-19 10:32:07 -0700483
484 if (cpu_is_msm8960()) {
485 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_B].rpm_vreg_id,
486 sc->vreg[VREG_HFPLL_B].rpm_vreg_voter, 0,
487 0, 0);
488 if (rc)
489 pr_err("%s regulator enable failed (%d)\n",
490 sc->vreg[VREG_HFPLL_B].name, rc);
491 rc = rpm_vreg_set_voltage(sc->vreg[VREG_HFPLL_A].rpm_vreg_id,
492 sc->vreg[VREG_HFPLL_A].rpm_vreg_voter, 0,
493 0, 0);
494 if (rc)
495 pr_err("%s regulator enable failed (%d)\n",
496 sc->vreg[VREG_HFPLL_A].name, rc);
497 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700498}
499
500/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
501static void hfpll_set_rate(struct scalable *sc, struct core_speed *tgt_s)
502{
503 writel_relaxed(tgt_s->pll_l_val, sc->hfpll_base + HFPLL_L_VAL);
504}
505
506/* Return the L2 speed that should be applied. */
507static struct l2_level *compute_l2_level(struct scalable *sc,
508 struct l2_level *vote_l)
509{
510 struct l2_level *new_l;
511 int cpu;
512
513 /* Bounds check. */
Vikram Mulukutlaa00149c2011-07-21 18:43:26 -0700514 BUG_ON(vote_l >= (l2_freq_tbl + l2_freq_tbl_size));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700515
516 /* Find max L2 speed vote. */
517 sc->l2_vote = vote_l;
518 new_l = l2_freq_tbl;
519 for_each_present_cpu(cpu)
520 new_l = max(new_l, scalable[cpu].l2_vote);
521
522 return new_l;
523}
524
525/* Update the bus bandwidth request. */
526static void set_bus_bw(unsigned int bw)
527{
528 int ret;
529
530 /* Bounds check. */
531 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
532 pr_err("invalid bandwidth request (%d)\n", bw);
533 return;
534 }
535
536 /* Update bandwidth if request has changed. This may sleep. */
537 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
538 if (ret)
539 pr_err("bandwidth request failed (%d)\n", ret);
540}
541
542/* Set the CPU or L2 clock speed. */
543static void set_speed(struct scalable *sc, struct core_speed *tgt_s,
544 enum setrate_reason reason)
545{
546 struct core_speed *strt_s = sc->current_speed;
547
548 if (tgt_s == strt_s)
549 return;
550
551 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552 /*
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700553 * Move to an always-on source running at a frequency that does
554 * not require an elevated CPU voltage. PLL8 is used here.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 */
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700556 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
558
559 /* Program CPU HFPLL. */
560 hfpll_disable(sc);
561 hfpll_set_rate(sc, tgt_s);
562 hfpll_enable(sc);
563
564 /* Move CPU to HFPLL source. */
565 set_pri_clk_src(sc, tgt_s->pri_src_sel);
566 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 /*
568 * If responding to CPU_DEAD we must be running on another
569 * CPU. Therefore, we can't access the downed CPU's CP15
570 * clock MUX registers from here and can't change clock sources.
571 * Just turn off the PLL- since the CPU is down already, halting
572 * its clock should be safe.
573 */
574 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2]) {
575 set_sec_clk_src(sc, tgt_s->sec_src_sel);
576 set_pri_clk_src(sc, tgt_s->pri_src_sel);
577 }
578 hfpll_disable(sc);
579 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
580 hfpll_set_rate(sc, tgt_s);
581 hfpll_enable(sc);
582 /*
583 * If responding to CPU_UP_PREPARE, we can't change CP15
584 * registers for the CPU that's coming up since we're not
585 * running on that CPU. That's okay though, since the MUX
586 * source was not changed on the way down, either.
587 */
588 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2])
589 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700590 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2])
592 set_sec_clk_src(sc, tgt_s->sec_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593 }
594
595 sc->current_speed = tgt_s;
596}
597
598/* Apply any per-cpu voltage increases. */
599static int increase_vdd(int cpu, unsigned int vdd_core, unsigned int vdd_mem,
600 unsigned int vdd_dig, enum setrate_reason reason)
601{
602 struct scalable *sc = &scalable[cpu];
Saravana Kannan9dcb89f2011-09-26 19:02:22 -0700603 int rc = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700604
605 /*
Matt Wagantallabd55f02011-09-12 11:45:54 -0700606 * Increase vdd_mem active-set before vdd_dig.
607 * vdd_mem should be >= vdd_dig.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 */
609 if (vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
610 rc = rpm_vreg_set_voltage(sc->vreg[VREG_MEM].rpm_vreg_id,
611 sc->vreg[VREG_MEM].rpm_vreg_voter, vdd_mem,
612 sc->vreg[VREG_MEM].max_vdd, 0);
613 if (rc) {
614 pr_err("%s: vdd_mem (cpu%d) increase failed (%d)\n",
615 __func__, cpu, rc);
616 return rc;
617 }
618 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
619 }
620
621 /* Increase vdd_dig active-set vote. */
622 if (vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
623 rc = rpm_vreg_set_voltage(sc->vreg[VREG_DIG].rpm_vreg_id,
624 sc->vreg[VREG_DIG].rpm_vreg_voter, vdd_dig,
625 sc->vreg[VREG_DIG].max_vdd, 0);
626 if (rc) {
627 pr_err("%s: vdd_dig (cpu%d) increase failed (%d)\n",
628 __func__, cpu, rc);
629 return rc;
630 }
631 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
632 }
633
634 /*
635 * Update per-CPU core voltage. Don't do this for the hotplug path for
636 * which it should already be correct. Attempting to set it is bad
637 * because we don't know what CPU we are running on at this point, but
638 * the CPU regulator API requires we call it from the affected CPU.
639 */
640 if (vdd_core > sc->vreg[VREG_CORE].cur_vdd
641 && reason != SETRATE_HOTPLUG) {
642 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
643 sc->vreg[VREG_CORE].max_vdd);
644 if (rc) {
645 pr_err("%s: vdd_core (cpu%d) increase failed (%d)\n",
646 __func__, cpu, rc);
647 return rc;
648 }
649 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
650 }
651
652 return rc;
653}
654
655/* Apply any per-cpu voltage decreases. */
656static void decrease_vdd(int cpu, unsigned int vdd_core, unsigned int vdd_mem,
657 unsigned int vdd_dig, enum setrate_reason reason)
658{
659 struct scalable *sc = &scalable[cpu];
660 int ret;
661
662 /*
663 * Update per-CPU core voltage. This must be called on the CPU
664 * that's being affected. Don't do this in the hotplug remove path,
665 * where the rail is off and we're executing on the other CPU.
666 */
667 if (vdd_core < sc->vreg[VREG_CORE].cur_vdd
668 && reason != SETRATE_HOTPLUG) {
669 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
670 sc->vreg[VREG_CORE].max_vdd);
671 if (ret) {
672 pr_err("%s: vdd_core (cpu%d) decrease failed (%d)\n",
673 __func__, cpu, ret);
674 return;
675 }
676 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
677 }
678
679 /* Decrease vdd_dig active-set vote. */
680 if (vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
681 ret = rpm_vreg_set_voltage(sc->vreg[VREG_DIG].rpm_vreg_id,
682 sc->vreg[VREG_DIG].rpm_vreg_voter, vdd_dig,
683 sc->vreg[VREG_DIG].max_vdd, 0);
684 if (ret) {
685 pr_err("%s: vdd_dig (cpu%d) decrease failed (%d)\n",
686 __func__, cpu, ret);
687 return;
688 }
689 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
690 }
691
692 /*
Matt Wagantallabd55f02011-09-12 11:45:54 -0700693 * Decrease vdd_mem active-set after vdd_dig.
694 * vdd_mem should be >= vdd_dig.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 */
696 if (vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
697 ret = rpm_vreg_set_voltage(sc->vreg[VREG_MEM].rpm_vreg_id,
698 sc->vreg[VREG_MEM].rpm_vreg_voter, vdd_mem,
699 sc->vreg[VREG_MEM].max_vdd, 0);
700 if (ret) {
701 pr_err("%s: vdd_mem (cpu%d) decrease failed (%d)\n",
702 __func__, cpu, ret);
703 return;
704 }
705 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
706 }
707}
708
709static unsigned int calculate_vdd_mem(struct acpu_level *tgt)
710{
Matt Wagantallabd55f02011-09-12 11:45:54 -0700711 return tgt->l2_level->vdd_mem;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712}
713
714static unsigned int calculate_vdd_dig(struct acpu_level *tgt)
715{
716 unsigned int pll_vdd_dig;
717
718 if (tgt->l2_level->speed.pll_l_val > HFPLL_LOW_VDD_PLL_L_MAX)
719 pll_vdd_dig = HFPLL_NOMINAL_VDD;
720 else
721 pll_vdd_dig = HFPLL_LOW_VDD;
722
723 return max(tgt->l2_level->vdd_dig, pll_vdd_dig);
724}
725
726static unsigned int calculate_vdd_core(struct acpu_level *tgt)
727{
728 unsigned int pll_vdd_core;
729
730 if (tgt->speed.pll_l_val > HFPLL_LOW_VDD_PLL_L_MAX)
731 pll_vdd_core = HFPLL_NOMINAL_VDD;
732 else
733 pll_vdd_core = HFPLL_LOW_VDD;
734
735 return max(tgt->vdd_core, pll_vdd_core);
736}
737
738/* Set the CPU's clock rate and adjust the L2 rate, if appropriate. */
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700739static int acpuclk_8960_set_rate(int cpu, unsigned long rate,
740 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700741{
742 struct core_speed *strt_acpu_s, *tgt_acpu_s;
743 struct l2_level *tgt_l2_l;
744 struct acpu_level *tgt;
745 unsigned int vdd_mem, vdd_dig, vdd_core;
746 unsigned long flags;
747 int rc = 0;
748
749 if (cpu > num_possible_cpus()) {
750 rc = -EINVAL;
751 goto out;
752 }
753
754 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
755 mutex_lock(&driver_lock);
756
757 strt_acpu_s = scalable[cpu].current_speed;
758
759 /* Return early if rate didn't change. */
760 if (rate == strt_acpu_s->khz && scalable[cpu].first_set_call == false)
761 goto out;
762
763 /* Find target frequency. */
764 for (tgt = acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
765 if (tgt->speed.khz == rate) {
766 tgt_acpu_s = &tgt->speed;
767 break;
768 }
769 }
770 if (tgt->speed.khz == 0) {
771 rc = -EINVAL;
772 goto out;
773 }
774
775 /* Calculate voltage requirements for the current CPU. */
776 vdd_mem = calculate_vdd_mem(tgt);
777 vdd_dig = calculate_vdd_dig(tgt);
778 vdd_core = calculate_vdd_core(tgt);
779
780 /* Increase VDD levels if needed. */
781 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
782 rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
783 if (rc)
784 goto out;
785 }
786
787 pr_debug("Switching from ACPU%d rate %u KHz -> %u KHz\n",
788 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
789
790 /* Set the CPU speed. */
791 set_speed(&scalable[cpu], tgt_acpu_s, reason);
792
793 /*
794 * Update the L2 vote and apply the rate change. A spinlock is
795 * necessary to ensure L2 rate is calulated and set atomically,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700796 * even if acpuclk_8960_set_rate() is called from an atomic context
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797 * and the driver_lock mutex is not acquired.
798 */
799 spin_lock_irqsave(&l2_lock, flags);
800 tgt_l2_l = compute_l2_level(&scalable[cpu], tgt->l2_level);
801 set_speed(&scalable[L2], &tgt_l2_l->speed, reason);
802 spin_unlock_irqrestore(&l2_lock, flags);
803
804 /* Nothing else to do for power collapse or SWFI. */
805 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
806 goto out;
807
808 /* Update bus bandwith request. */
809 set_bus_bw(tgt_l2_l->bw_level);
810
811 /* Drop VDD levels if we can. */
812 decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
813
814 scalable[cpu].first_set_call = false;
815 pr_debug("ACPU%d speed change complete\n", cpu);
816
817out:
818 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
819 mutex_unlock(&driver_lock);
820 return rc;
821}
822
823/* Initialize a HFPLL at a given rate and enable it. */
824static void __init hfpll_init(struct scalable *sc, struct core_speed *tgt_s)
825{
826 pr_debug("Initializing HFPLL%d\n", sc - scalable);
827
828 /* Disable the PLL for re-programming. */
829 hfpll_disable(sc);
830
831 /* Configure PLL parameters for integer mode. */
832 writel_relaxed(0x7845C665, sc->hfpll_base + HFPLL_CONFIG_CTL);
833 writel_relaxed(0, sc->hfpll_base + HFPLL_M_VAL);
834 writel_relaxed(1, sc->hfpll_base + HFPLL_N_VAL);
835
836 /* Program droop controller. */
837 writel_relaxed(0x0108C000, sc->hfpll_base + HFPLL_DROOP_CTL);
838
839 /* Set an initial rate and enable the PLL. */
840 hfpll_set_rate(sc, tgt_s);
841 hfpll_enable(sc);
842}
843
844/* Voltage regulator initialization. */
845static void __init regulator_init(void)
846{
847 int cpu, ret;
848 struct scalable *sc;
849
850 for_each_possible_cpu(cpu) {
851 sc = &scalable[cpu];
852 sc->vreg[VREG_CORE].reg = regulator_get(NULL,
853 sc->vreg[VREG_CORE].name);
854 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
855 pr_err("regulator_get(%s) failed (%ld)\n",
856 sc->vreg[VREG_CORE].name,
857 PTR_ERR(sc->vreg[VREG_CORE].reg));
858 BUG();
859 }
860
861 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
862 sc->vreg[VREG_CORE].max_vdd,
863 sc->vreg[VREG_CORE].max_vdd);
864 if (ret)
865 pr_err("regulator_set_voltage(%s) failed"
866 " (%d)\n", sc->vreg[VREG_CORE].name, ret);
867
868 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
869 if (ret)
870 pr_err("regulator_enable(%s) failed (%d)\n",
871 sc->vreg[VREG_CORE].name, ret);
872 }
873}
874
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875/* Set initial rate for a given core. */
876static void __init init_clock_sources(struct scalable *sc,
877 struct core_speed *tgt_s)
878{
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700879 uint32_t regval;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700881 /* Select PLL8 as AUX source input to the secondary MUX. */
882 writel_relaxed(0x3, sc->aux_clk_sel);
883
884 /* Switch away from the HFPLL while it's re-initialized. */
Matt Wagantall65e5e4b2011-10-27 16:52:10 -0700885 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700886 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 hfpll_init(sc, tgt_s);
888
889 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700890 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700891 regval &= ~(0x3 << 6);
Stephen Boyd469ed3e2011-09-29 16:41:19 -0700892 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893
Matt Wagantall6ba92d82011-10-27 16:51:26 -0700894 /* Switch to the target clock source. */
895 set_sec_clk_src(sc, tgt_s->sec_src_sel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896 set_pri_clk_src(sc, tgt_s->pri_src_sel);
897 sc->current_speed = tgt_s;
898
899 /*
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700900 * Set this flag so that the first call to acpuclk_8960_set_rate() can
901 * drop voltages and set initial bus bandwidth requests.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 */
903 sc->first_set_call = true;
904}
905
Matt Wagantall8e726c72011-08-06 00:49:28 -0700906static void __init per_cpu_init(void *data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907{
Matt Wagantall6b013ca2011-10-12 14:15:45 -0700908 struct acpu_level *max_acpu_level = data;
Matt Wagantall8e726c72011-08-06 00:49:28 -0700909 int cpu = smp_processor_id();
Matt Wagantall6b013ca2011-10-12 14:15:45 -0700910
911 init_clock_sources(&scalable[cpu], &max_acpu_level->speed);
912 scalable[cpu].l2_vote = max_acpu_level->l2_level;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913}
914
915/* Register with bus driver. */
916static void __init bus_init(void)
917{
918 int ret;
919
920 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
921 if (!bus_perf_client) {
922 pr_err("unable to register bus client\n");
923 BUG();
924 }
925
926 ret = msm_bus_scale_client_update_request(bus_perf_client,
927 (ARRAY_SIZE(bw_level_tbl)-1));
928 if (ret)
929 pr_err("initial bandwidth request failed (%d)\n", ret);
930}
931
932#ifdef CONFIG_CPU_FREQ_MSM
933static struct cpufreq_frequency_table freq_table[NR_CPUS][30];
934
935static void __init cpufreq_table_init(void)
936{
937 int cpu;
938
939 for_each_possible_cpu(cpu) {
940 int i, freq_cnt = 0;
941 /* Construct the freq_table tables from acpu_freq_tbl. */
942 for (i = 0; acpu_freq_tbl[i].speed.khz != 0
943 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
944 if (acpu_freq_tbl[i].use_for_scaling) {
945 freq_table[cpu][freq_cnt].index = freq_cnt;
946 freq_table[cpu][freq_cnt].frequency
947 = acpu_freq_tbl[i].speed.khz;
948 freq_cnt++;
949 }
950 }
951 /* freq_table not big enough to store all usable freqs. */
952 BUG_ON(acpu_freq_tbl[i].speed.khz != 0);
953
954 freq_table[cpu][freq_cnt].index = freq_cnt;
955 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
956
957 pr_info("CPU%d: %d scaling frequencies supported.\n",
958 cpu, freq_cnt);
959
960 /* Register table with CPUFreq. */
961 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
962 }
963}
964#else
965static void __init cpufreq_table_init(void) {}
966#endif
967
968#define HOT_UNPLUG_KHZ STBY_KHZ
969static int __cpuinit acpuclock_cpu_callback(struct notifier_block *nfb,
970 unsigned long action, void *hcpu)
971{
972 static int prev_khz[NR_CPUS];
973 static int prev_pri_src[NR_CPUS];
974 static int prev_sec_src[NR_CPUS];
975 int cpu = (int)hcpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976
977 switch (action) {
978 case CPU_DYING:
979 case CPU_DYING_FROZEN:
980 /*
Matt Wagantall27663842011-08-25 15:11:48 -0700981 * On Krait v1, the primary and secondary muxes must be set
982 * to QSB before L2 power collapse and restored after.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 */
Matt Wagantall27663842011-08-25 15:11:48 -0700984 if (cpu_is_krait_v1()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 prev_sec_src[cpu] = get_sec_clk_src(&scalable[cpu]);
986 prev_pri_src[cpu] = get_pri_clk_src(&scalable[cpu]);
987 set_sec_clk_src(&scalable[cpu], SEC_SRC_SEL_QSB);
988 set_pri_clk_src(&scalable[cpu], PRI_SRC_SEL_SEC_SRC);
989 }
990 break;
991 case CPU_DEAD:
992 case CPU_DEAD_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700993 prev_khz[cpu] = acpuclk_8960_get_rate(cpu);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 /* Fall through. */
995 case CPU_UP_CANCELED:
996 case CPU_UP_CANCELED_FROZEN:
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700997 acpuclk_8960_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 break;
999 case CPU_UP_PREPARE:
1000 case CPU_UP_PREPARE_FROZEN:
1001 if (WARN_ON(!prev_khz[cpu]))
1002 prev_khz[cpu] = acpu_freq_tbl->speed.khz;
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001003 acpuclk_8960_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001004 break;
1005 case CPU_STARTING:
1006 case CPU_STARTING_FROZEN:
Matt Wagantall27663842011-08-25 15:11:48 -07001007 if (cpu_is_krait_v1()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 set_sec_clk_src(&scalable[cpu], prev_sec_src[cpu]);
1009 set_pri_clk_src(&scalable[cpu], prev_pri_src[cpu]);
1010 }
1011 break;
1012 default:
1013 break;
1014 }
1015
1016 return NOTIFY_OK;
1017}
1018
1019static struct notifier_block __cpuinitdata acpuclock_cpu_notifier = {
1020 .notifier_call = acpuclock_cpu_callback,
1021};
1022
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001023static struct acpu_level * __init select_freq_plan(void)
1024{
1025 struct acpu_level *l, *max_acpu_level = NULL;
1026
1027 /* Select frequency tables. */
Stepan Moskovchenko532008c2011-10-25 14:43:49 -07001028 if (cpu_is_msm8960() || cpu_is_msm8930()) {
Matt Wagantalla518f8f2011-10-17 13:24:53 -07001029 uint32_t pte_efuse, pvs;
1030
1031 pte_efuse = readl_relaxed(QFPROM_PTE_EFUSE_ADDR);
1032 pvs = (pte_efuse >> 10) & 0x7;
1033 if (pvs == 0x7)
1034 pvs = (pte_efuse >> 13) & 0x7;
1035
1036 switch (pvs) {
1037 case 0x0:
1038 case 0x7:
1039 pr_info("ACPU PVS: Slow\n");
1040 break;
1041 case 0x1:
1042 pr_info("ACPU PVS: Nominal\n");
1043 break;
1044 case 0x3:
1045 pr_info("ACPU PVS: Fast\n");
1046 break;
1047 default:
1048 pr_warn("ACPU PVS: Unknown. Defaulting to slow.\n");
1049 break;
1050 }
1051
1052 /* TODO: Select tables based on PVS data. */
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001053 scalable = scalable_8960;
1054 acpu_freq_tbl = acpu_freq_tbl_8960;
1055 l2_freq_tbl = l2_freq_tbl_8960;
1056 l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_8960);
1057 } else if (cpu_is_apq8064()) {
1058 scalable = scalable_8064;
1059 acpu_freq_tbl = acpu_freq_tbl_8064;
1060 l2_freq_tbl = l2_freq_tbl_8064;
1061 l2_freq_tbl_size = ARRAY_SIZE(l2_freq_tbl_8064);
1062 } else {
1063 BUG();
1064 }
1065
1066 /* Find the max supported scaling frequency. */
1067 for (l = acpu_freq_tbl; l->speed.khz != 0; l++)
1068 if (l->use_for_scaling)
1069 max_acpu_level = l;
1070 BUG_ON(!max_acpu_level);
1071 pr_info("Max ACPU freq: %u KHz\n", max_acpu_level->speed.khz);
1072
1073 return max_acpu_level;
1074}
1075
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001076static struct acpuclk_data acpuclk_8960_data = {
1077 .set_rate = acpuclk_8960_set_rate,
1078 .get_rate = acpuclk_8960_get_rate,
1079 .power_collapse_khz = STBY_KHZ,
1080 .wait_for_irq_khz = STBY_KHZ,
1081};
1082
Matt Wagantallec57f062011-08-16 23:54:46 -07001083static int __init acpuclk_8960_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084{
Matt Wagantall6b013ca2011-10-12 14:15:45 -07001085 struct acpu_level *max_acpu_level = select_freq_plan();
1086 init_clock_sources(&scalable[L2], &max_acpu_level->l2_level->speed);
1087 on_each_cpu(per_cpu_init, max_acpu_level, true);
Matt Wagantall8e726c72011-08-06 00:49:28 -07001088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 regulator_init();
1090 bus_init();
1091 cpufreq_table_init();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001092
1093 acpuclk_register(&acpuclk_8960_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001094 register_hotcpu_notifier(&acpuclock_cpu_notifier);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001095
1096 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097}
Matt Wagantallec57f062011-08-16 23:54:46 -07001098
1099struct acpuclk_soc_data acpuclk_8960_soc_data __initdata = {
1100 .init = acpuclk_8960_init,
1101};