blob: e7e328f4188ee610f4d7bebc54b5cc8be030fccf [file] [log] [blame]
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001/*
2 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/io.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/err.h>
22#include <linux/errno.h>
23#include <linux/cpufreq.h>
24#include <linux/cpu.h>
25#include <linux/regulator/consumer.h>
26
27#include <asm/mach-types.h>
28#include <asm/cpu.h>
29
30#include <mach/board.h>
31#include <mach/msm_iomap.h>
32#include <mach/socinfo.h>
33#include <mach/msm-krait-l2-accessors.h>
34#include <mach/rpm-regulator.h>
Matt Wagantall75473eb2012-05-31 15:23:22 -070035#include <mach/rpm-regulator-smd.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080036#include <mach/msm_bus.h>
37
38#include "acpuclock.h"
39#include "acpuclock-krait.h"
40
41/* MUX source selects. */
42#define PRI_SRC_SEL_SEC_SRC 0
43#define PRI_SRC_SEL_HFPLL 1
44#define PRI_SRC_SEL_HFPLL_DIV2 2
45#define SEC_SRC_SEL_QSB 0
46#define SEC_SRC_SEL_L2PLL 1
47#define SEC_SRC_SEL_AUX 2
48
49/* PTE EFUSE register offset. */
50#define PTE_EFUSE 0xC0
51
52static DEFINE_MUTEX(driver_lock);
53static DEFINE_SPINLOCK(l2_lock);
54
55static struct drv_data {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -070056 struct acpu_level *acpu_freq_tbl;
Matt Wagantall754ee272012-06-18 13:40:26 -070057 const struct acpu_level *max_acpu_lvl;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080058 const struct l2_level *l2_freq_tbl;
59 struct scalable *scalable;
60 u32 bus_perf_client;
61 struct device *dev;
62} drv;
63
64static unsigned long acpuclk_krait_get_rate(int cpu)
65{
66 return drv.scalable[cpu].cur_speed->khz;
67}
68
69/* Select a source on the primary MUX. */
70static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
71{
72 u32 regval;
73
74 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
75 regval &= ~0x3;
76 regval |= (pri_src_sel & 0x3);
77 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
78 /* Wait for switch to complete. */
79 mb();
80 udelay(1);
81}
82
83/* Select a source on the secondary MUX. */
84static void set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
85{
86 u32 regval;
87
88 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
89 regval &= ~(0x3 << 2);
90 regval |= ((sec_src_sel & 0x3) << 2);
91 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
92 /* Wait for switch to complete. */
93 mb();
94 udelay(1);
95}
96
Matt Wagantall75473eb2012-05-31 15:23:22 -070097static void enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080098{
99 int rc;
100
Matt Wagantall75473eb2012-05-31 15:23:22 -0700101 if (vreg->rpm_reg) {
102 rc = rpm_regulator_enable(vreg->rpm_reg);
103 if (rc) {
104 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
105 vreg->name, rc);
106 BUG();
107 }
108 }
109}
110
111static void disable_rpm_vreg(struct vreg *vreg)
112{
113 int rc;
114
115 if (vreg->rpm_reg) {
116 rc = rpm_regulator_disable(vreg->rpm_reg);
117 if (rc)
118 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
119 vreg->name, rc);
120 }
121}
122
123/* Enable an already-configured HFPLL. */
124static void hfpll_enable(struct scalable *sc, bool skip_regulators)
125{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800126 if (!skip_regulators) {
127 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700128 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
129 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800130 }
131
132 /* Disable PLL bypass mode. */
133 writel_relaxed(0x2, sc->hfpll_base + sc->hfpll_data->mode_offset);
134
135 /*
136 * H/W requires a 5us delay between disabling the bypass and
137 * de-asserting the reset. Delay 10us just to be safe.
138 */
139 mb();
140 udelay(10);
141
142 /* De-assert active-low PLL reset. */
143 writel_relaxed(0x6, sc->hfpll_base + sc->hfpll_data->mode_offset);
144
145 /* Wait for PLL to lock. */
146 mb();
147 udelay(60);
148
149 /* Enable PLL output. */
150 writel_relaxed(0x7, sc->hfpll_base + sc->hfpll_data->mode_offset);
151}
152
153/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
154static void hfpll_disable(struct scalable *sc, bool skip_regulators)
155{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800156 /*
157 * Disable the PLL output, disable test mode, enable the bypass mode,
158 * and assert the reset.
159 */
160 writel_relaxed(0, sc->hfpll_base + sc->hfpll_data->mode_offset);
161
162 if (!skip_regulators) {
163 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700164 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
165 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800166 }
167}
168
169/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
170static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
171{
172 writel_relaxed(tgt_s->pll_l_val,
173 sc->hfpll_base + sc->hfpll_data->l_offset);
174}
175
176/* Return the L2 speed that should be applied. */
177static const struct l2_level *compute_l2_level(struct scalable *sc,
178 const struct l2_level *vote_l)
179{
180 const struct l2_level *new_l;
181 int cpu;
182
183 /* Find max L2 speed vote. */
184 sc->l2_vote = vote_l;
185 new_l = drv.l2_freq_tbl;
186 for_each_present_cpu(cpu)
187 new_l = max(new_l, drv.scalable[cpu].l2_vote);
188
189 return new_l;
190}
191
192/* Update the bus bandwidth request. */
193static void set_bus_bw(unsigned int bw)
194{
195 int ret;
196
197 /* Update bandwidth if request has changed. This may sleep. */
198 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
199 if (ret)
200 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
201}
202
203/* Set the CPU or L2 clock speed. */
204static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
205{
206 const struct core_speed *strt_s = sc->cur_speed;
207
208 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
209 /*
210 * Move to an always-on source running at a frequency
211 * that does not require an elevated CPU voltage.
212 */
213 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
214 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
215
216 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700217 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800218 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700219 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800220
221 /* Move to HFPLL. */
222 set_pri_clk_src(sc, tgt_s->pri_src_sel);
223 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
224 set_sec_clk_src(sc, tgt_s->sec_src_sel);
225 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700226 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800227 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
228 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700229 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800230 set_pri_clk_src(sc, tgt_s->pri_src_sel);
231 } else {
232 set_sec_clk_src(sc, tgt_s->sec_src_sel);
233 }
234
235 sc->cur_speed = tgt_s;
236}
237
238/* Apply any per-cpu voltage increases. */
239static int increase_vdd(int cpu, int vdd_core, int vdd_mem, int vdd_dig,
240 enum setrate_reason reason)
241{
242 struct scalable *sc = &drv.scalable[cpu];
243 int rc = 0;
244
245 /*
246 * Increase vdd_mem active-set before vdd_dig.
247 * vdd_mem should be >= vdd_dig.
248 */
249 if (vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700250 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
251 vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800252 if (rc) {
253 dev_err(drv.dev,
254 "vdd_mem (cpu%d) increase failed (%d)\n",
255 cpu, rc);
256 return rc;
257 }
258 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
259 }
260
261 /* Increase vdd_dig active-set vote. */
262 if (vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700263 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
264 vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800265 if (rc) {
266 dev_err(drv.dev,
267 "vdd_dig (cpu%d) increase failed (%d)\n",
268 cpu, rc);
269 return rc;
270 }
271 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
272 }
273
274 /*
275 * Update per-CPU core voltage. Don't do this for the hotplug path for
276 * which it should already be correct. Attempting to set it is bad
277 * because we don't know what CPU we are running on at this point, but
278 * the CPU regulator API requires we call it from the affected CPU.
279 */
280 if (vdd_core > sc->vreg[VREG_CORE].cur_vdd
281 && reason != SETRATE_HOTPLUG) {
282 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
283 sc->vreg[VREG_CORE].max_vdd);
284 if (rc) {
285 dev_err(drv.dev,
286 "vdd_core (cpu%d) increase failed (%d)\n",
287 cpu, rc);
288 return rc;
289 }
290 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
291 }
292
293 return rc;
294}
295
296/* Apply any per-cpu voltage decreases. */
297static void decrease_vdd(int cpu, int vdd_core, int vdd_mem, int vdd_dig,
298 enum setrate_reason reason)
299{
300 struct scalable *sc = &drv.scalable[cpu];
301 int ret;
302
303 /*
304 * Update per-CPU core voltage. This must be called on the CPU
305 * that's being affected. Don't do this in the hotplug remove path,
306 * where the rail is off and we're executing on the other CPU.
307 */
308 if (vdd_core < sc->vreg[VREG_CORE].cur_vdd
309 && reason != SETRATE_HOTPLUG) {
310 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
311 sc->vreg[VREG_CORE].max_vdd);
312 if (ret) {
313 dev_err(drv.dev,
314 "vdd_core (cpu%d) decrease failed (%d)\n",
315 cpu, ret);
316 return;
317 }
318 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
319 }
320
321 /* Decrease vdd_dig active-set vote. */
322 if (vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700323 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
324 vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800325 if (ret) {
326 dev_err(drv.dev,
327 "vdd_dig (cpu%d) decrease failed (%d)\n",
328 cpu, ret);
329 return;
330 }
331 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
332 }
333
334 /*
335 * Decrease vdd_mem active-set after vdd_dig.
336 * vdd_mem should be >= vdd_dig.
337 */
338 if (vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700339 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
340 vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800341 if (ret) {
342 dev_err(drv.dev,
343 "vdd_mem (cpu%d) decrease failed (%d)\n",
344 cpu, ret);
345 return;
346 }
347 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
348 }
349}
350
351static int calculate_vdd_mem(const struct acpu_level *tgt)
352{
353 return tgt->l2_level->vdd_mem;
354}
355
356static int calculate_vdd_dig(const struct acpu_level *tgt)
357{
358 int pll_vdd_dig;
359 const int *hfpll_vdd = drv.scalable[L2].hfpll_data->vdd;
360 const u32 low_vdd_l_max = drv.scalable[L2].hfpll_data->low_vdd_l_max;
361
362 if (tgt->l2_level->speed.src != HFPLL)
363 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_NONE];
364 else if (tgt->l2_level->speed.pll_l_val > low_vdd_l_max)
365 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_NOM];
366 else
367 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_LOW];
368
369 return max(tgt->l2_level->vdd_dig, pll_vdd_dig);
370}
371
372static int calculate_vdd_core(const struct acpu_level *tgt)
373{
374 return tgt->vdd_core;
375}
376
377/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
378static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
379 enum setrate_reason reason)
380{
381 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
382 const struct l2_level *tgt_l2_l;
383 const struct acpu_level *tgt;
384 int vdd_mem, vdd_dig, vdd_core;
385 unsigned long flags;
386 int rc = 0;
387
388 if (cpu > num_possible_cpus()) {
389 rc = -EINVAL;
390 goto out;
391 }
392
393 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
394 mutex_lock(&driver_lock);
395
396 strt_acpu_s = drv.scalable[cpu].cur_speed;
397
398 /* Return early if rate didn't change. */
399 if (rate == strt_acpu_s->khz)
400 goto out;
401
402 /* Find target frequency. */
403 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
404 if (tgt->speed.khz == rate) {
405 tgt_acpu_s = &tgt->speed;
406 break;
407 }
408 }
409 if (tgt->speed.khz == 0) {
410 rc = -EINVAL;
411 goto out;
412 }
413
414 /* Calculate voltage requirements for the current CPU. */
415 vdd_mem = calculate_vdd_mem(tgt);
416 vdd_dig = calculate_vdd_dig(tgt);
417 vdd_core = calculate_vdd_core(tgt);
418
419 /* Increase VDD levels if needed. */
420 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
421 rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
422 if (rc)
423 goto out;
424 }
425
426 pr_debug("Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
427 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
428
429 /* Set the new CPU speed. */
430 set_speed(&drv.scalable[cpu], tgt_acpu_s);
431
432 /*
433 * Update the L2 vote and apply the rate change. A spinlock is
434 * necessary to ensure L2 rate is calculated and set atomically
435 * with the CPU frequency, even if acpuclk_krait_set_rate() is
436 * called from an atomic context and the driver_lock mutex is not
437 * acquired.
438 */
439 spin_lock_irqsave(&l2_lock, flags);
440 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
441 set_speed(&drv.scalable[L2], &tgt_l2_l->speed);
442 spin_unlock_irqrestore(&l2_lock, flags);
443
444 /* Nothing else to do for power collapse or SWFI. */
445 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
446 goto out;
447
448 /* Update bus bandwith request. */
449 set_bus_bw(tgt_l2_l->bw_level);
450
451 /* Drop VDD levels if we can. */
452 decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
453
454 pr_debug("ACPU%d speed change complete\n", cpu);
455
456out:
457 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
458 mutex_unlock(&driver_lock);
459 return rc;
460}
461
462/* Initialize a HFPLL at a given rate and enable it. */
463static void __init hfpll_init(struct scalable *sc,
464 const struct core_speed *tgt_s)
465{
466 pr_debug("Initializing HFPLL%d\n", sc - drv.scalable);
467
468 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700469 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800470
471 /* Configure PLL parameters for integer mode. */
472 writel_relaxed(sc->hfpll_data->config_val,
473 sc->hfpll_base + sc->hfpll_data->config_offset);
474 writel_relaxed(0, sc->hfpll_base + sc->hfpll_data->m_offset);
475 writel_relaxed(1, sc->hfpll_base + sc->hfpll_data->n_offset);
476
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700477 /* Program droop controller, if supported */
478 if (sc->hfpll_data->has_droop_ctl)
479 writel_relaxed(sc->hfpll_data->droop_val,
480 sc->hfpll_base + sc->hfpll_data->droop_offset);
481
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800482 /* Set an initial rate and enable the PLL. */
483 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700484 hfpll_enable(sc, false);
485}
486
Matt Wagantall754ee272012-06-18 13:40:26 -0700487static void __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
488 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700489{
490 int ret;
491
492 if (!sc->vreg[vreg].name)
493 return;
494
495 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
496 sc->vreg[vreg].name);
497 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
498 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%ld)\n",
499 sc->vreg[vreg].name,
500 PTR_ERR(sc->vreg[vreg].rpm_reg));
501 BUG();
502 }
503
504 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
505 sc->vreg[vreg].max_vdd);
506 if (ret) {
507 dev_err(drv.dev, "%s initialization failed (%d)\n",
508 sc->vreg[vreg].name, ret);
509 BUG();
510 }
511 sc->vreg[vreg].cur_vdd = vdd;
512
513 if (enable)
514 enable_rpm_vreg(&sc->vreg[vreg]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800515}
516
517/* Voltage regulator initialization. */
Matt Wagantall754ee272012-06-18 13:40:26 -0700518static void __cpuinit regulator_init(struct scalable *sc)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800519{
Matt Wagantall754ee272012-06-18 13:40:26 -0700520 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800521
Matt Wagantall754ee272012-06-18 13:40:26 -0700522 vdd_mem = calculate_vdd_mem(drv.max_acpu_lvl);
523 vdd_dig = calculate_vdd_dig(drv.max_acpu_lvl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800524
Matt Wagantall754ee272012-06-18 13:40:26 -0700525 rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
526 rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
527 rpm_regulator_init(sc, VREG_HFPLL_A,
528 sc->vreg[VREG_HFPLL_A].max_vdd, false);
529 rpm_regulator_init(sc, VREG_HFPLL_B,
530 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700531
Matt Wagantall754ee272012-06-18 13:40:26 -0700532 /* Setup Krait CPU regulators and initial core voltage. */
533 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
534 sc->vreg[VREG_CORE].name);
535 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
536 dev_err(drv.dev, "regulator_get(%s) failed (%ld)\n",
537 sc->vreg[VREG_CORE].name,
538 PTR_ERR(sc->vreg[VREG_CORE].reg));
539 BUG();
540 }
541 vdd_core = calculate_vdd_core(drv.max_acpu_lvl);
542 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
543 sc->vreg[VREG_CORE].max_vdd);
544 if (ret) {
545 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
546 sc->vreg[VREG_CORE].name, ret);
547 BUG();
548 }
549 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
550 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
551 sc->vreg[VREG_CORE].peak_ua);
552 if (ret < 0) {
553 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
554 sc->vreg[VREG_CORE].name, ret);
555 BUG();
556 }
557 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
558 if (ret) {
559 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
560 sc->vreg[VREG_CORE].name, ret);
561 BUG();
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800562 }
563}
564
565/* Set initial rate for a given core. */
Matt Wagantall754ee272012-06-18 13:40:26 -0700566static void __cpuinit init_clock_sources(struct scalable *sc,
567 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800568{
569 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700570 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800571
572 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700573 if (sc->aux_clk_sel_phys) {
574 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
575 BUG_ON(!aux_reg);
576 writel_relaxed(sc->aux_clk_sel, aux_reg);
577 iounmap(aux_reg);
578 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800579
580 /* Switch away from the HFPLL while it's re-initialized. */
581 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
582 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
583 hfpll_init(sc, tgt_s);
584
585 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
586 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
587 regval &= ~(0x3 << 6);
588 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
589
590 /* Switch to the target clock source. */
591 set_sec_clk_src(sc, tgt_s->sec_src_sel);
592 set_pri_clk_src(sc, tgt_s->pri_src_sel);
593 sc->cur_speed = tgt_s;
594}
595
Matt Wagantall754ee272012-06-18 13:40:26 -0700596static void __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800597{
Matt Wagantall754ee272012-06-18 13:40:26 -0700598 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800599
Matt Wagantall754ee272012-06-18 13:40:26 -0700600 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
601 BUG_ON(!sc->hfpll_base);
602
603 regulator_init(sc);
604
605 init_clock_sources(sc, &drv.max_acpu_lvl->speed);
606 sc->l2_vote = drv.max_acpu_lvl->l2_level;
607
608 sc->initialized = true;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800609}
610
611/* Register with bus driver. */
Matt Wagantall754ee272012-06-18 13:40:26 -0700612static void __init bus_init(struct msm_bus_scale_pdata *bus_scale_data)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800613{
614 int ret;
615
616 drv.bus_perf_client = msm_bus_scale_register_client(bus_scale_data);
617 if (!drv.bus_perf_client) {
618 dev_err(drv.dev, "unable to register bus client\n");
619 BUG();
620 }
621
Matt Wagantall754ee272012-06-18 13:40:26 -0700622 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
623 drv.max_acpu_lvl->l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800624 if (ret)
625 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
626}
627
628#ifdef CONFIG_CPU_FREQ_MSM
629static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
630
631static void __init cpufreq_table_init(void)
632{
633 int cpu;
634
635 for_each_possible_cpu(cpu) {
636 int i, freq_cnt = 0;
637 /* Construct the freq_table tables from acpu_freq_tbl. */
638 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
639 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
640 if (drv.acpu_freq_tbl[i].use_for_scaling) {
641 freq_table[cpu][freq_cnt].index = freq_cnt;
642 freq_table[cpu][freq_cnt].frequency
643 = drv.acpu_freq_tbl[i].speed.khz;
644 freq_cnt++;
645 }
646 }
647 /* freq_table not big enough to store all usable freqs. */
648 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
649
650 freq_table[cpu][freq_cnt].index = freq_cnt;
651 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
652
653 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
654 cpu, freq_cnt);
655
656 /* Register table with CPUFreq. */
657 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
658 }
659}
660#else
661static void __init cpufreq_table_init(void) {}
662#endif
663
664#define HOT_UNPLUG_KHZ STBY_KHZ
665static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
666 unsigned long action, void *hcpu)
667{
668 static int prev_khz[NR_CPUS];
669 int rc, cpu = (int)hcpu;
670 struct scalable *sc = &drv.scalable[cpu];
671
672 switch (action & ~CPU_TASKS_FROZEN) {
673 case CPU_DEAD:
674 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
675 /* Fall through. */
676 case CPU_UP_CANCELED:
677 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
678 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
679 break;
680 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700681 if (!sc->initialized) {
682 per_cpu_init(cpu);
683 break;
684 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800685 if (WARN_ON(!prev_khz[cpu]))
686 return NOTIFY_BAD;
687 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
688 sc->vreg[VREG_CORE].peak_ua);
689 if (rc < 0)
690 return NOTIFY_BAD;
691 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
692 break;
693 default:
694 break;
695 }
696
697 return NOTIFY_OK;
698}
699
700static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
701 .notifier_call = acpuclk_cpu_callback,
702};
703
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700704static const int krait_needs_vmin(void)
705{
706 switch (read_cpuid_id()) {
707 case 0x511F04D0: /* KR28M2A20 */
708 case 0x511F04D1: /* KR28M2A21 */
709 case 0x510F06F0: /* KR28M4A10 */
710 return 1;
711 default:
712 return 0;
713 };
714}
715
716static void krait_apply_vmin(struct acpu_level *tbl)
717{
718 for (; tbl->speed.khz != 0; tbl++)
719 if (tbl->vdd_core < 1150000)
720 tbl->vdd_core = 1150000;
721}
722
Matt Wagantall754ee272012-06-18 13:40:26 -0700723static void __init select_freq_plan(struct acpu_level *const *pvs_tbl,
724 u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800725{
Matt Wagantall754ee272012-06-18 13:40:26 -0700726 const struct acpu_level *l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800727 void __iomem *qfprom_base;
728 u32 pte_efuse, pvs, tbl_idx;
729 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Unknown" };
730
731 qfprom_base = ioremap(qfprom_phys, SZ_256);
732 /* Select frequency tables. */
733 if (qfprom_base) {
734 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
735 pvs = (pte_efuse >> 10) & 0x7;
736 iounmap(qfprom_base);
737 if (pvs == 0x7)
738 pvs = (pte_efuse >> 13) & 0x7;
739
740 switch (pvs) {
741 case 0x0:
742 case 0x7:
743 tbl_idx = PVS_SLOW;
744 break;
745 case 0x1:
746 tbl_idx = PVS_NOMINAL;
747 break;
748 case 0x3:
749 tbl_idx = PVS_FAST;
750 break;
751 default:
752 tbl_idx = PVS_UNKNOWN;
753 break;
754 }
755 } else {
756 tbl_idx = PVS_UNKNOWN;
757 dev_err(drv.dev, "Unable to map QFPROM base\n");
758 }
759 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
760 if (tbl_idx == PVS_UNKNOWN) {
761 tbl_idx = PVS_SLOW;
762 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
763 pvs_names[tbl_idx]);
764 }
765 drv.acpu_freq_tbl = pvs_tbl[tbl_idx];
766
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700767 if (krait_needs_vmin())
768 krait_apply_vmin(drv.acpu_freq_tbl);
769
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800770 /* Find the max supported scaling frequency. */
771 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
772 if (l->use_for_scaling)
Matt Wagantall754ee272012-06-18 13:40:26 -0700773 drv.max_acpu_lvl = l;
774 BUG_ON(!drv.max_acpu_lvl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800775 dev_info(drv.dev, "Max ACPU freq: %lu KHz\n",
Matt Wagantall754ee272012-06-18 13:40:26 -0700776 drv.max_acpu_lvl->speed.khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800777}
778
779static struct acpuclk_data acpuclk_krait_data = {
780 .set_rate = acpuclk_krait_set_rate,
781 .get_rate = acpuclk_krait_get_rate,
782 .power_collapse_khz = STBY_KHZ,
783 .wait_for_irq_khz = STBY_KHZ,
784};
785
786int __init acpuclk_krait_init(struct device *dev,
787 const struct acpuclk_krait_params *params)
788{
Matt Wagantall754ee272012-06-18 13:40:26 -0700789 struct scalable *l2;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800790 int cpu;
791
792 drv.scalable = params->scalable;
793 drv.l2_freq_tbl = params->l2_freq_tbl;
794 drv.dev = dev;
795
Matt Wagantall754ee272012-06-18 13:40:26 -0700796 select_freq_plan(params->pvs_acpu_freq_tbl, params->qfprom_phys_base);
797 bus_init(params->bus_scale_data);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800798
Matt Wagantall754ee272012-06-18 13:40:26 -0700799 l2 = &drv.scalable[L2];
800 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
801 BUG_ON(!l2->hfpll_base);
802 rpm_regulator_init(l2, VREG_HFPLL_A, l2->vreg[VREG_HFPLL_A].max_vdd,
803 false);
804 rpm_regulator_init(l2, VREG_HFPLL_B, l2->vreg[VREG_HFPLL_B].max_vdd,
805 false);
806 init_clock_sources(l2, &drv.max_acpu_lvl->l2_level->speed);
807
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800808 for_each_online_cpu(cpu)
Matt Wagantall754ee272012-06-18 13:40:26 -0700809 per_cpu_init(cpu);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800810
811 cpufreq_table_init();
812
813 acpuclk_register(&acpuclk_krait_data);
814 register_hotcpu_notifier(&acpuclk_cpu_notifier);
815
816 return 0;
817}