blob: e216b78f7c0b5f8e87a8e2b6c6b43be21776409f [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>
35
36#include "acpuclock.h"
37
38/*
39 * Source IDs.
40 * These must be negative to not overlap with the source IDs
41 * used by the 8x60 local clock driver.
42 */
43#define PLL_8 0
44#define HFPLL -1
45#define QSB -2
46
47/* Mux source selects. */
48#define PRI_SRC_SEL_SEC_SRC 0
49#define PRI_SRC_SEL_HFPLL 1
50#define PRI_SRC_SEL_HFPLL_DIV2 2
51#define SEC_SRC_SEL_QSB 0
52
53/* HFPLL registers offsets. */
54#define HFPLL_MODE 0x00
55#define HFPLL_CONFIG_CTL 0x04
56#define HFPLL_L_VAL 0x08
57#define HFPLL_M_VAL 0x0C
58#define HFPLL_N_VAL 0x10
59#define HFPLL_DROOP_CTL 0x14
60
61/* CP15 L2 indirect addresses. */
62#define L2CPMR_IADDR 0x500
63#define L2CPUCPMR_IADDR 0x501
64
65#define STBY_KHZ 1
66
67#define HFPLL_NOMINAL_VDD 1050000
68#define HFPLL_LOW_VDD 1050000
69#define HFPLL_LOW_VDD_PLL_L_MAX 0x28
70
71#define SECCLKAGD BIT(4)
72
73enum scalables {
74 CPU0 = 0,
75 CPU1,
76 L2,
77 NUM_SCALABLES
78};
79
80enum vregs {
81 VREG_CORE,
82 VREG_MEM,
83 VREG_DIG,
84 NUM_VREG
85};
86
87struct vreg {
88 const char name[15];
89 const unsigned int max_vdd;
90 const int rpm_vreg_voter;
91 const int rpm_vreg_id;
92 struct regulator *reg;
93 unsigned int cur_vdd;
94};
95
96struct core_speed {
97 unsigned int khz;
98 int src;
99 unsigned int pri_src_sel;
100 unsigned int sec_src_sel;
101 unsigned int pll_l_val;
102};
103
104struct l2_level {
105 struct core_speed speed;
106 unsigned int vdd_dig;
107 unsigned int vdd_mem;
108 unsigned int bw_level;
109};
110
111struct acpu_level {
112 unsigned int use_for_scaling;
113 struct core_speed speed;
114 struct l2_level *l2_level;
115 unsigned int vdd_core;
116};
117
118struct scalable {
119 void * __iomem const hfpll_base;
120 void * __iomem const aux_clk_sel;
121 const uint32_t l2cpmr_iaddr;
122 struct core_speed *current_speed;
123 struct l2_level *l2_vote;
124 struct vreg vreg[NUM_VREG];
125 bool first_set_call;
126};
127
128static struct scalable scalable[] = {
129 [CPU0] = {
130 .hfpll_base = MSM_HFPLL_BASE + 0x200,
131 .aux_clk_sel = MSM_ACC0_BASE + 0x014,
132 .l2cpmr_iaddr = L2CPUCPMR_IADDR,
133 .vreg[VREG_CORE] = { "krait0", 1150000 },
134 .vreg[VREG_MEM] = { "krait0_mem", 1150000,
135 RPM_VREG_VOTER1,
136 RPM_VREG_ID_PM8921_L24 },
137 .vreg[VREG_DIG] = { "krait0_dig", 1150000,
138 RPM_VREG_VOTER1,
139 RPM_VREG_ID_PM8921_S3 },
140 },
141 [CPU1] = {
142 .hfpll_base = MSM_HFPLL_BASE + 0x300,
143 .aux_clk_sel = MSM_ACC1_BASE + 0x014,
144 .l2cpmr_iaddr = L2CPUCPMR_IADDR,
145 .vreg[VREG_CORE] = { "krait1", 1150000 },
146 .vreg[VREG_MEM] = { "krait0_mem", 1150000,
147 RPM_VREG_VOTER2,
148 RPM_VREG_ID_PM8921_L24 },
149 .vreg[VREG_DIG] = { "krait0_dig", 1150000,
150 RPM_VREG_VOTER2,
151 RPM_VREG_ID_PM8921_S3 },
152 },
153 [L2] = {
154 .hfpll_base = MSM_HFPLL_BASE + 0x400,
155 .aux_clk_sel = MSM_APCS_GCC_BASE + 0x028,
156 .l2cpmr_iaddr = L2CPMR_IADDR,
157 },
158};
159
160struct mutex driver_lock;
161static spinlock_t l2_lock;
162
163/* Instantaneous bandwidth requests in MB/s. */
164#define BW_MBPS(_bw) \
165 { \
166 .vectors = (struct msm_bus_vectors[]){ \
167 {\
168 .src = MSM_BUS_MASTER_AMPSS_M0, \
169 .dst = MSM_BUS_SLAVE_EBI_CH0, \
170 .ib = (_bw) * 1000000UL, \
171 .ab = (_bw) * 100000UL, \
172 }, \
173 { \
174 .src = MSM_BUS_MASTER_AMPSS_M1, \
175 .dst = MSM_BUS_SLAVE_EBI_CH0, \
176 .ib = (_bw) * 1000000UL, \
177 .ab = (_bw) * 100000UL, \
178 }, \
179 }, \
180 .num_paths = 2, \
181 }
182static struct msm_bus_paths bw_level_tbl[] = {
183 [0] = BW_MBPS(616), /* At least 77 MHz on bus. */
184 [1] = BW_MBPS(1024), /* At least 128 MHz on bus. */
185 [2] = BW_MBPS(1536), /* At least 192 MHz on bus. */
186 [3] = BW_MBPS(2048), /* At least 256 MHz on bus. */
187 [4] = BW_MBPS(3080), /* At least 385 MHz on bus. */
188 [5] = BW_MBPS(3968), /* At least 496 MHz on bus. */
189};
190
191static struct msm_bus_scale_pdata bus_client_pdata = {
192 .usecase = bw_level_tbl,
193 .num_usecases = ARRAY_SIZE(bw_level_tbl),
194 .active_only = 1,
195 .name = "acpuclock",
196};
197
198static uint32_t bus_perf_client;
199
200/* TODO: Update vdd_dig and vdd_mem when voltage data is available. */
201#define L2(x) (&l2_freq_tbl[(x)])
202#define L2_BOOT_IDX 11
203static struct l2_level l2_freq_tbl[] = {
204 [0] = { {STBY_KHZ, QSB, 0, 0, 0x00 }, 1050000, 1050000, 0 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700205 [1] = { { 384000, PLL_8, 0, 2, 0x00 }, 1050000, 1050000, 1 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 [2] = { { 432000, HFPLL, 2, 0, 0x20 }, 1050000, 1050000, 1 },
207 [3] = { { 486000, HFPLL, 2, 0, 0x24 }, 1050000, 1050000, 1 },
208 [4] = { { 540000, HFPLL, 2, 0, 0x28 }, 1050000, 1050000, 1 },
209 [5] = { { 594000, HFPLL, 1, 0, 0x16 }, 1050000, 1050000, 2 },
210 [6] = { { 648000, HFPLL, 1, 0, 0x18 }, 1050000, 1050000, 2 },
211 [7] = { { 702000, HFPLL, 1, 0, 0x1A }, 1050000, 1050000, 2 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700212 [8] = { { 756000, HFPLL, 1, 0, 0x1C }, 1150000, 1150000, 2 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 [9] = { { 810000, HFPLL, 1, 0, 0x1E }, 1150000, 1150000, 3 },
214 [10] = { { 864000, HFPLL, 1, 0, 0x20 }, 1150000, 1150000, 3 },
215 [11] = { { 918000, HFPLL, 1, 0, 0x22 }, 1150000, 1150000, 3 },
216 [12] = { { 972000, HFPLL, 1, 0, 0x24 }, 1150000, 1150000, 3 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700217 [13] = { { 1026000, HFPLL, 1, 0, 0x26 }, 1150000, 1150000, 4 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 [14] = { { 1080000, HFPLL, 1, 0, 0x28 }, 1150000, 1150000, 4 },
219 [15] = { { 1134000, HFPLL, 1, 0, 0x2A }, 1150000, 1150000, 4 },
220 [16] = { { 1188000, HFPLL, 1, 0, 0x2C }, 1150000, 1150000, 4 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700221 [17] = { { 1242000, HFPLL, 1, 0, 0x2E }, 1150000, 1150000, 5 },
222 [18] = { { 1296000, HFPLL, 1, 0, 0x30 }, 1150000, 1150000, 5 },
223 [19] = { { 1350000, HFPLL, 1, 0, 0x32 }, 1150000, 1150000, 5 },
224 [20] = { { 1404000, HFPLL, 1, 0, 0x34 }, 1150000, 1150000, 5 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 [21] = { { 1458000, HFPLL, 1, 0, 0x36 }, 1150000, 1150000, 5 },
226 [22] = { { 1512000, HFPLL, 1, 0, 0x38 }, 1150000, 1150000, 5 },
227 [23] = { { 1566000, HFPLL, 1, 0, 0x3A }, 1150000, 1150000, 5 },
228 [24] = { { 1620000, HFPLL, 1, 0, 0x3C }, 1150000, 1150000, 5 },
229 [25] = { { 1674000, HFPLL, 1, 0, 0x3E }, 1150000, 1150000, 5 },
230};
231
232/* TODO: Update core voltages when data is available. */
233#define CPU_BOOT_IDX 11
234static struct acpu_level acpu_freq_tbl[] = {
235 { 0, {STBY_KHZ, QSB, 0, 0, 0x00 }, L2(0), 1050000 },
236 { 1, { 384000, PLL_8, 0, 2, 0x00 }, L2(1), 1050000 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700237 { 1, { 432000, HFPLL, 2, 0, 0x20 }, L2(6), 1050000 },
238 { 1, { 486000, HFPLL, 2, 0, 0x24 }, L2(6), 1050000 },
239 { 1, { 540000, HFPLL, 2, 0, 0x28 }, L2(6), 1050000 },
240 { 1, { 594000, HFPLL, 1, 0, 0x16 }, L2(6), 1050000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 { 1, { 648000, HFPLL, 1, 0, 0x18 }, L2(6), 1050000 },
Matt Wagantalle64d56a2011-07-14 19:35:27 -0700242 { 1, { 702000, HFPLL, 1, 0, 0x1A }, L2(6), 1050000 },
243 { 1, { 756000, HFPLL, 1, 0, 0x1C }, L2(13), 1150000 },
244 { 1, { 810000, HFPLL, 1, 0, 0x1E }, L2(13), 1150000 },
245 { 1, { 864000, HFPLL, 1, 0, 0x20 }, L2(13), 1150000 },
246 { 1, { 918000, HFPLL, 1, 0, 0x22 }, L2(13), 1150000 },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247 { 0, { 0 } }
248};
249
250unsigned long acpuclk_get_rate(int cpu)
251{
252 return scalable[cpu].current_speed->khz;
253}
254
255uint32_t acpuclk_get_switch_time(void)
256{
257 return 0;
258}
259
260unsigned long acpuclk_power_collapse(void)
261{
262 int ret = acpuclk_get_rate(smp_processor_id());
263 acpuclk_set_rate(smp_processor_id(), STBY_KHZ, SETRATE_PC);
264 return ret;
265}
266
267unsigned long acpuclk_wait_for_irq(void)
268{
269 int ret = acpuclk_get_rate(smp_processor_id());
270 acpuclk_set_rate(smp_processor_id(), STBY_KHZ, SETRATE_SWFI);
271 return ret;
272}
273
274/* Read an 'indirectly' addressed L2 CP15 register. */
275static uint32_t readl_cp15_l2ind(uint32_t addr)
276{
277 uint32_t regval;
278
279 /*
280 * TODO: CP15 registers are not emulated on RUMI3.
281 * Remove this check if/when they are.
282 */
283 if (machine_is_msm8960_rumi3())
284 return 0;
285
286 asm volatile ("mcr p15, 3, %[l2cpsler], c15, c0, 6\n\t"
287 "mrc p15, 3, %[l2cpdr], c15, c0, 7\n\t"
288 : [l2cpdr]"=r" (regval)
289 : [l2cpsler]"r" (addr)
290 : "cc"
291 );
292 return regval;
293}
294
295/* Write an 'indirectly' addressed L2 CP15 register. */
296static void writel_cp15_l2ind(uint32_t regval, uint32_t addr)
297{
298 /*
299 * TODO: CP15 registers are not emulated on RUMI3.
300 * Remove this check if/when they are.
301 */
302 if (machine_is_msm8960_rumi3())
303 return;
304
305 mb();
306 asm volatile ("mcr p15, 3, %[l2cpsler], c15, c0, 6\n\t"
307 "mcr p15, 3, %[l2cpdr], c15, c0, 7\n\t"
308 :
309 : [l2cpsler]"r" (addr), [l2cpdr]"r" (regval)
310 : "cc"
311 );
312 isb();
313}
314
315/* Get the selected source on primary MUX. */
316static int get_pri_clk_src(struct scalable *sc)
317{
318 uint32_t regval;
319
320 regval = readl_cp15_l2ind(sc->l2cpmr_iaddr);
321 return regval & 0x3;
322}
323
324/* Set the selected source on primary MUX. */
325static void set_pri_clk_src(struct scalable *sc, uint32_t pri_src_sel)
326{
327 uint32_t regval;
328
329 regval = readl_cp15_l2ind(sc->l2cpmr_iaddr);
330 regval &= ~0x3;
331 regval |= (pri_src_sel & 0x3);
332 writel_cp15_l2ind(regval, sc->l2cpmr_iaddr);
333 /* Wait for switch to complete. */
334 mb();
335 udelay(1);
336}
337
338/* Get the selected source on secondary MUX. */
339static int get_sec_clk_src(struct scalable *sc)
340{
341 uint32_t regval;
342
343 regval = readl_cp15_l2ind(sc->l2cpmr_iaddr);
344 return (regval >> 2) & 0x3;
345}
346
347/* Set the selected source on secondary MUX. */
348static void set_sec_clk_src(struct scalable *sc, uint32_t sec_src_sel)
349{
350 uint32_t regval;
351
352 /* Disable secondary source clock gating during switch. */
353 regval = readl_cp15_l2ind(sc->l2cpmr_iaddr);
354 regval |= SECCLKAGD;
355 writel_cp15_l2ind(regval, sc->l2cpmr_iaddr);
356
357 /* Program the MUX. */
358 regval &= ~(0x3 << 2);
359 regval |= ((sec_src_sel & 0x3) << 2);
360 writel_cp15_l2ind(regval, sc->l2cpmr_iaddr);
361
362 /* Wait for switch to complete. */
363 mb();
364 udelay(1);
365
366 /* Re-enable secondary source clock gating. */
367 regval &= ~SECCLKAGD;
368 writel_cp15_l2ind(regval, sc->l2cpmr_iaddr);
369}
370
371/* Enable an already-configured HFPLL. */
372static void hfpll_enable(struct scalable *sc)
373{
374 /* Disable PLL bypass mode. */
375 writel_relaxed(0x2, sc->hfpll_base + HFPLL_MODE);
376
377 /*
378 * H/W requires a 5us delay between disabling the bypass and
379 * de-asserting the reset. Delay 10us just to be safe.
380 */
381 mb();
382 udelay(10);
383
384 /* De-assert active-low PLL reset. */
385 writel_relaxed(0x6, sc->hfpll_base + HFPLL_MODE);
386
387 /* Wait for PLL to lock. */
388 mb();
389 udelay(60);
390
391 /* Enable PLL output. */
392 writel_relaxed(0x7, sc->hfpll_base + HFPLL_MODE);
393}
394
395/* Disable a HFPLL for power-savings or while its being reprogrammed. */
396static void hfpll_disable(struct scalable *sc)
397{
398 /*
399 * Disable the PLL output, disable test mode, enable
400 * the bypass mode, and assert the reset.
401 */
402 writel_relaxed(0, sc->hfpll_base + HFPLL_MODE);
403}
404
405/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
406static void hfpll_set_rate(struct scalable *sc, struct core_speed *tgt_s)
407{
408 writel_relaxed(tgt_s->pll_l_val, sc->hfpll_base + HFPLL_L_VAL);
409}
410
411/* Return the L2 speed that should be applied. */
412static struct l2_level *compute_l2_level(struct scalable *sc,
413 struct l2_level *vote_l)
414{
415 struct l2_level *new_l;
416 int cpu;
417
418 /* Bounds check. */
419 BUG_ON(vote_l >= (l2_freq_tbl + ARRAY_SIZE(l2_freq_tbl)));
420
421 /* Find max L2 speed vote. */
422 sc->l2_vote = vote_l;
423 new_l = l2_freq_tbl;
424 for_each_present_cpu(cpu)
425 new_l = max(new_l, scalable[cpu].l2_vote);
426
427 return new_l;
428}
429
430/* Update the bus bandwidth request. */
431static void set_bus_bw(unsigned int bw)
432{
433 int ret;
434
435 /* Bounds check. */
436 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
437 pr_err("invalid bandwidth request (%d)\n", bw);
438 return;
439 }
440
441 /* Update bandwidth if request has changed. This may sleep. */
442 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
443 if (ret)
444 pr_err("bandwidth request failed (%d)\n", ret);
445}
446
447/* Set the CPU or L2 clock speed. */
448static void set_speed(struct scalable *sc, struct core_speed *tgt_s,
449 enum setrate_reason reason)
450{
451 struct core_speed *strt_s = sc->current_speed;
452
453 if (tgt_s == strt_s)
454 return;
455
456 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
457 /* Move CPU to QSB source. */
458 /*
459 * TODO: If using QSB here requires elevating voltages,
460 * consider using PLL8 instead.
461 */
462 set_sec_clk_src(sc, SEC_SRC_SEL_QSB);
463 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
464
465 /* Program CPU HFPLL. */
466 hfpll_disable(sc);
467 hfpll_set_rate(sc, tgt_s);
468 hfpll_enable(sc);
469
470 /* Move CPU to HFPLL source. */
471 set_pri_clk_src(sc, tgt_s->pri_src_sel);
472 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
473 /* TODO: Enable source. */
474 /*
475 * If responding to CPU_DEAD we must be running on another
476 * CPU. Therefore, we can't access the downed CPU's CP15
477 * clock MUX registers from here and can't change clock sources.
478 * Just turn off the PLL- since the CPU is down already, halting
479 * its clock should be safe.
480 */
481 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2]) {
482 set_sec_clk_src(sc, tgt_s->sec_src_sel);
483 set_pri_clk_src(sc, tgt_s->pri_src_sel);
484 }
485 hfpll_disable(sc);
486 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
487 hfpll_set_rate(sc, tgt_s);
488 hfpll_enable(sc);
489 /*
490 * If responding to CPU_UP_PREPARE, we can't change CP15
491 * registers for the CPU that's coming up since we're not
492 * running on that CPU. That's okay though, since the MUX
493 * source was not changed on the way down, either.
494 */
495 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2])
496 set_pri_clk_src(sc, tgt_s->pri_src_sel);
497 /* TODO: Disable source. */
498 } else {
499 /* TODO: Enable source. */
500 if (reason != SETRATE_HOTPLUG || sc == &scalable[L2])
501 set_sec_clk_src(sc, tgt_s->sec_src_sel);
502 /* TODO: Disable source. */
503 }
504
505 sc->current_speed = tgt_s;
506}
507
508/* Apply any per-cpu voltage increases. */
509static int increase_vdd(int cpu, unsigned int vdd_core, unsigned int vdd_mem,
510 unsigned int vdd_dig, enum setrate_reason reason)
511{
512 struct scalable *sc = &scalable[cpu];
513 int rc;
514
515 /*
516 * Increase vdd_mem active-set before vdd_dig and vdd_core.
517 * vdd_mem should be >= both vdd_core and vdd_dig.
518 */
519 if (vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
520 rc = rpm_vreg_set_voltage(sc->vreg[VREG_MEM].rpm_vreg_id,
521 sc->vreg[VREG_MEM].rpm_vreg_voter, vdd_mem,
522 sc->vreg[VREG_MEM].max_vdd, 0);
523 if (rc) {
524 pr_err("%s: vdd_mem (cpu%d) increase failed (%d)\n",
525 __func__, cpu, rc);
526 return rc;
527 }
528 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
529 }
530
531 /* Increase vdd_dig active-set vote. */
532 if (vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
533 rc = rpm_vreg_set_voltage(sc->vreg[VREG_DIG].rpm_vreg_id,
534 sc->vreg[VREG_DIG].rpm_vreg_voter, vdd_dig,
535 sc->vreg[VREG_DIG].max_vdd, 0);
536 if (rc) {
537 pr_err("%s: vdd_dig (cpu%d) increase failed (%d)\n",
538 __func__, cpu, rc);
539 return rc;
540 }
541 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
542 }
543
544 /*
545 * Update per-CPU core voltage. Don't do this for the hotplug path for
546 * which it should already be correct. Attempting to set it is bad
547 * because we don't know what CPU we are running on at this point, but
548 * the CPU regulator API requires we call it from the affected CPU.
549 */
550 if (vdd_core > sc->vreg[VREG_CORE].cur_vdd
551 && reason != SETRATE_HOTPLUG) {
552 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
553 sc->vreg[VREG_CORE].max_vdd);
554 if (rc) {
555 pr_err("%s: vdd_core (cpu%d) increase failed (%d)\n",
556 __func__, cpu, rc);
557 return rc;
558 }
559 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
560 }
561
562 return rc;
563}
564
565/* Apply any per-cpu voltage decreases. */
566static void decrease_vdd(int cpu, unsigned int vdd_core, unsigned int vdd_mem,
567 unsigned int vdd_dig, enum setrate_reason reason)
568{
569 struct scalable *sc = &scalable[cpu];
570 int ret;
571
572 /*
573 * Update per-CPU core voltage. This must be called on the CPU
574 * that's being affected. Don't do this in the hotplug remove path,
575 * where the rail is off and we're executing on the other CPU.
576 */
577 if (vdd_core < sc->vreg[VREG_CORE].cur_vdd
578 && reason != SETRATE_HOTPLUG) {
579 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
580 sc->vreg[VREG_CORE].max_vdd);
581 if (ret) {
582 pr_err("%s: vdd_core (cpu%d) decrease failed (%d)\n",
583 __func__, cpu, ret);
584 return;
585 }
586 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
587 }
588
589 /* Decrease vdd_dig active-set vote. */
590 if (vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
591 ret = rpm_vreg_set_voltage(sc->vreg[VREG_DIG].rpm_vreg_id,
592 sc->vreg[VREG_DIG].rpm_vreg_voter, vdd_dig,
593 sc->vreg[VREG_DIG].max_vdd, 0);
594 if (ret) {
595 pr_err("%s: vdd_dig (cpu%d) decrease failed (%d)\n",
596 __func__, cpu, ret);
597 return;
598 }
599 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
600 }
601
602 /*
603 * Decrease vdd_mem active-set after vdd_dig and vdd_core.
604 * vdd_mem should be >= both vdd_core and vdd_dig.
605 */
606 if (vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
607 ret = rpm_vreg_set_voltage(sc->vreg[VREG_MEM].rpm_vreg_id,
608 sc->vreg[VREG_MEM].rpm_vreg_voter, vdd_mem,
609 sc->vreg[VREG_MEM].max_vdd, 0);
610 if (ret) {
611 pr_err("%s: vdd_mem (cpu%d) decrease failed (%d)\n",
612 __func__, cpu, ret);
613 return;
614 }
615 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
616 }
617}
618
619static unsigned int calculate_vdd_mem(struct acpu_level *tgt)
620{
621 return max(tgt->vdd_core, tgt->l2_level->vdd_mem);
622}
623
624static unsigned int calculate_vdd_dig(struct acpu_level *tgt)
625{
626 unsigned int pll_vdd_dig;
627
628 if (tgt->l2_level->speed.pll_l_val > HFPLL_LOW_VDD_PLL_L_MAX)
629 pll_vdd_dig = HFPLL_NOMINAL_VDD;
630 else
631 pll_vdd_dig = HFPLL_LOW_VDD;
632
633 return max(tgt->l2_level->vdd_dig, pll_vdd_dig);
634}
635
636static unsigned int calculate_vdd_core(struct acpu_level *tgt)
637{
638 unsigned int pll_vdd_core;
639
640 if (tgt->speed.pll_l_val > HFPLL_LOW_VDD_PLL_L_MAX)
641 pll_vdd_core = HFPLL_NOMINAL_VDD;
642 else
643 pll_vdd_core = HFPLL_LOW_VDD;
644
645 return max(tgt->vdd_core, pll_vdd_core);
646}
647
648/* Set the CPU's clock rate and adjust the L2 rate, if appropriate. */
649int acpuclk_set_rate(int cpu, unsigned long rate, enum setrate_reason reason)
650{
651 struct core_speed *strt_acpu_s, *tgt_acpu_s;
652 struct l2_level *tgt_l2_l;
653 struct acpu_level *tgt;
654 unsigned int vdd_mem, vdd_dig, vdd_core;
655 unsigned long flags;
656 int rc = 0;
657
658 if (cpu > num_possible_cpus()) {
659 rc = -EINVAL;
660 goto out;
661 }
662
663 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
664 mutex_lock(&driver_lock);
665
666 strt_acpu_s = scalable[cpu].current_speed;
667
668 /* Return early if rate didn't change. */
669 if (rate == strt_acpu_s->khz && scalable[cpu].first_set_call == false)
670 goto out;
671
672 /* Find target frequency. */
673 for (tgt = acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
674 if (tgt->speed.khz == rate) {
675 tgt_acpu_s = &tgt->speed;
676 break;
677 }
678 }
679 if (tgt->speed.khz == 0) {
680 rc = -EINVAL;
681 goto out;
682 }
683
684 /* Calculate voltage requirements for the current CPU. */
685 vdd_mem = calculate_vdd_mem(tgt);
686 vdd_dig = calculate_vdd_dig(tgt);
687 vdd_core = calculate_vdd_core(tgt);
688
689 /* Increase VDD levels if needed. */
690 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
691 rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
692 if (rc)
693 goto out;
694 }
695
696 pr_debug("Switching from ACPU%d rate %u KHz -> %u KHz\n",
697 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
698
699 /* Set the CPU speed. */
700 set_speed(&scalable[cpu], tgt_acpu_s, reason);
701
702 /*
703 * Update the L2 vote and apply the rate change. A spinlock is
704 * necessary to ensure L2 rate is calulated and set atomically,
705 * even if acpuclk_set_rate() is called from an atomic context
706 * and the driver_lock mutex is not acquired.
707 */
708 spin_lock_irqsave(&l2_lock, flags);
709 tgt_l2_l = compute_l2_level(&scalable[cpu], tgt->l2_level);
710 set_speed(&scalable[L2], &tgt_l2_l->speed, reason);
711 spin_unlock_irqrestore(&l2_lock, flags);
712
713 /* Nothing else to do for power collapse or SWFI. */
714 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
715 goto out;
716
717 /* Update bus bandwith request. */
718 set_bus_bw(tgt_l2_l->bw_level);
719
720 /* Drop VDD levels if we can. */
721 decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
722
723 scalable[cpu].first_set_call = false;
724 pr_debug("ACPU%d speed change complete\n", cpu);
725
726out:
727 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
728 mutex_unlock(&driver_lock);
729 return rc;
730}
731
732/* Initialize a HFPLL at a given rate and enable it. */
733static void __init hfpll_init(struct scalable *sc, struct core_speed *tgt_s)
734{
735 pr_debug("Initializing HFPLL%d\n", sc - scalable);
736
737 /* Disable the PLL for re-programming. */
738 hfpll_disable(sc);
739
740 /* Configure PLL parameters for integer mode. */
741 writel_relaxed(0x7845C665, sc->hfpll_base + HFPLL_CONFIG_CTL);
742 writel_relaxed(0, sc->hfpll_base + HFPLL_M_VAL);
743 writel_relaxed(1, sc->hfpll_base + HFPLL_N_VAL);
744
745 /* Program droop controller. */
746 writel_relaxed(0x0108C000, sc->hfpll_base + HFPLL_DROOP_CTL);
747
748 /* Set an initial rate and enable the PLL. */
749 hfpll_set_rate(sc, tgt_s);
750 hfpll_enable(sc);
751}
752
753/* Voltage regulator initialization. */
754static void __init regulator_init(void)
755{
756 int cpu, ret;
757 struct scalable *sc;
758
759 for_each_possible_cpu(cpu) {
760 sc = &scalable[cpu];
761 sc->vreg[VREG_CORE].reg = regulator_get(NULL,
762 sc->vreg[VREG_CORE].name);
763 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
764 pr_err("regulator_get(%s) failed (%ld)\n",
765 sc->vreg[VREG_CORE].name,
766 PTR_ERR(sc->vreg[VREG_CORE].reg));
767 BUG();
768 }
769
770 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
771 sc->vreg[VREG_CORE].max_vdd,
772 sc->vreg[VREG_CORE].max_vdd);
773 if (ret)
774 pr_err("regulator_set_voltage(%s) failed"
775 " (%d)\n", sc->vreg[VREG_CORE].name, ret);
776
777 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
778 if (ret)
779 pr_err("regulator_enable(%s) failed (%d)\n",
780 sc->vreg[VREG_CORE].name, ret);
781 }
782}
783
784#define INIT_QSB_ID 0
785#define INIT_HFPLL_ID 1
786/* Set initial rate for a given core. */
787static void __init init_clock_sources(struct scalable *sc,
788 struct core_speed *tgt_s)
789{
790 uint32_t pri_src, regval;
791
792 /*
793 * If the HFPLL is in use, program AUX source for QSB, switch to it,
794 * re-initialize the HFPLL, and switch back to the HFPLL. Otherwise,
795 * the HFPLL is not in use, so we can switch directly to it.
796 */
797 pri_src = get_pri_clk_src(scalable);
798 if (pri_src == PRI_SRC_SEL_HFPLL || pri_src == PRI_SRC_SEL_HFPLL_DIV2) {
799 set_sec_clk_src(sc, SEC_SRC_SEL_QSB);
800 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
801 }
802 hfpll_init(sc, tgt_s);
803
804 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
805 regval = readl_cp15_l2ind(sc->l2cpmr_iaddr);
806 regval &= ~(0x3 << 6);
807 writel_cp15_l2ind(regval, sc->l2cpmr_iaddr);
808
809 /* Select PLL8 as AUX source input to the secondary MUX. */
810 writel_relaxed(0x3, sc->aux_clk_sel);
811
812 set_pri_clk_src(sc, tgt_s->pri_src_sel);
813 sc->current_speed = tgt_s;
814
815 /*
816 * Set this flag so that the first call to acpuclk_set_rate() can drop
817 * voltages and set initial bus bandwidth requests.
818 */
819 sc->first_set_call = true;
820}
821
822/* Perform CPU0-specific setup. */
823int __init msm_acpu_clock_early_init(void)
824{
825 init_clock_sources(&scalable[L2], &l2_freq_tbl[L2_BOOT_IDX].speed);
826 init_clock_sources(&scalable[CPU0], &acpu_freq_tbl[CPU_BOOT_IDX].speed);
827 scalable[CPU0].l2_vote = &l2_freq_tbl[L2_BOOT_IDX];
828
829 return 0;
830}
831early_initcall(msm_acpu_clock_early_init);
832
833/* Perform CPU1-specific setup. */
834void __cpuinit acpuclock_secondary_init(void)
835{
836 static bool warm_boot;
837
838 if (warm_boot)
839 return;
840
841 init_clock_sources(&scalable[CPU1], &acpu_freq_tbl[CPU_BOOT_IDX].speed);
842 scalable[CPU1].l2_vote = &l2_freq_tbl[L2_BOOT_IDX];
843
844 /* Secondary CPU has booted, don't repeat for subsequent warm boots. */
845 warm_boot = true;
846}
847
848/* Register with bus driver. */
849static void __init bus_init(void)
850{
851 int ret;
852
853 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
854 if (!bus_perf_client) {
855 pr_err("unable to register bus client\n");
856 BUG();
857 }
858
859 ret = msm_bus_scale_client_update_request(bus_perf_client,
860 (ARRAY_SIZE(bw_level_tbl)-1));
861 if (ret)
862 pr_err("initial bandwidth request failed (%d)\n", ret);
863}
864
865#ifdef CONFIG_CPU_FREQ_MSM
866static struct cpufreq_frequency_table freq_table[NR_CPUS][30];
867
868static void __init cpufreq_table_init(void)
869{
870 int cpu;
871
872 for_each_possible_cpu(cpu) {
873 int i, freq_cnt = 0;
874 /* Construct the freq_table tables from acpu_freq_tbl. */
875 for (i = 0; acpu_freq_tbl[i].speed.khz != 0
876 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
877 if (acpu_freq_tbl[i].use_for_scaling) {
878 freq_table[cpu][freq_cnt].index = freq_cnt;
879 freq_table[cpu][freq_cnt].frequency
880 = acpu_freq_tbl[i].speed.khz;
881 freq_cnt++;
882 }
883 }
884 /* freq_table not big enough to store all usable freqs. */
885 BUG_ON(acpu_freq_tbl[i].speed.khz != 0);
886
887 freq_table[cpu][freq_cnt].index = freq_cnt;
888 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
889
890 pr_info("CPU%d: %d scaling frequencies supported.\n",
891 cpu, freq_cnt);
892
893 /* Register table with CPUFreq. */
894 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
895 }
896}
897#else
898static void __init cpufreq_table_init(void) {}
899#endif
900
901#define HOT_UNPLUG_KHZ STBY_KHZ
902static int __cpuinit acpuclock_cpu_callback(struct notifier_block *nfb,
903 unsigned long action, void *hcpu)
904{
905 static int prev_khz[NR_CPUS];
906 static int prev_pri_src[NR_CPUS];
907 static int prev_sec_src[NR_CPUS];
908 int cpu = (int)hcpu;
909 uint32_t soc_platform_version = socinfo_get_platform_version();
910
911 switch (action) {
912 case CPU_DYING:
913 case CPU_DYING_FROZEN:
914 /*
915 * 8960 HW versions < 2.1 must set their primary and secondary
916 * mux source selections to QSB before L2 power collapse and
917 * restore it after.
918 */
919 if (SOCINFO_VERSION_MAJOR(soc_platform_version) < 2 ||
920 (SOCINFO_VERSION_MAJOR(soc_platform_version) == 2 &&
921 SOCINFO_VERSION_MINOR(soc_platform_version) < 1)) {
922 prev_sec_src[cpu] = get_sec_clk_src(&scalable[cpu]);
923 prev_pri_src[cpu] = get_pri_clk_src(&scalable[cpu]);
924 set_sec_clk_src(&scalable[cpu], SEC_SRC_SEL_QSB);
925 set_pri_clk_src(&scalable[cpu], PRI_SRC_SEL_SEC_SRC);
926 }
927 break;
928 case CPU_DEAD:
929 case CPU_DEAD_FROZEN:
930 prev_khz[cpu] = acpuclk_get_rate(cpu);
931 /* Fall through. */
932 case CPU_UP_CANCELED:
933 case CPU_UP_CANCELED_FROZEN:
934 acpuclk_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
935 break;
936 case CPU_UP_PREPARE:
937 case CPU_UP_PREPARE_FROZEN:
938 if (WARN_ON(!prev_khz[cpu]))
939 prev_khz[cpu] = acpu_freq_tbl->speed.khz;
940 acpuclk_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
941 break;
942 case CPU_STARTING:
943 case CPU_STARTING_FROZEN:
944 if (SOCINFO_VERSION_MAJOR(soc_platform_version) < 2 ||
945 (SOCINFO_VERSION_MAJOR(soc_platform_version) == 2 &&
946 SOCINFO_VERSION_MINOR(soc_platform_version) < 1)) {
947 set_sec_clk_src(&scalable[cpu], prev_sec_src[cpu]);
948 set_pri_clk_src(&scalable[cpu], prev_pri_src[cpu]);
949 }
950 break;
951 default:
952 break;
953 }
954
955 return NOTIFY_OK;
956}
957
958static struct notifier_block __cpuinitdata acpuclock_cpu_notifier = {
959 .notifier_call = acpuclock_cpu_callback,
960};
961
962void __init msm_acpu_clock_init(struct msm_acpu_clock_platform_data *clkdata)
963{
964 mutex_init(&driver_lock);
965 regulator_init();
966 bus_init();
967 cpufreq_table_init();
968 register_hotcpu_notifier(&acpuclock_cpu_notifier);
969}