blob: 84253b89b5d5b89fe066b759ed5a6f1e8edd796b [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 Wagantall302d9a32012-07-03 13:37:29 -070097static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080098{
Matt Wagantall302d9a32012-07-03 13:37:29 -070099 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800100
Matt Wagantall75473eb2012-05-31 15:23:22 -0700101 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700102 ret = rpm_regulator_enable(vreg->rpm_reg);
103 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700104 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700105 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700106 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700107
108 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700109}
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 Wagantall302d9a32012-07-03 13:37:29 -0700487static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700488 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700489{
490 int ret;
491
492 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700493 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700494
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)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700498 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
499 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
500 sc->vreg[vreg].name, ret);
501 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700502 }
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);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700509 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700510 }
511 sc->vreg[vreg].cur_vdd = vdd;
512
Matt Wagantall302d9a32012-07-03 13:37:29 -0700513 if (enable) {
514 ret = enable_rpm_vreg(&sc->vreg[vreg]);
515 if (ret)
516 goto err_conf;
517 }
518
519 return 0;
520
521err_conf:
522 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
523err_get:
524 return ret;
525}
526
527static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
528 enum vregs vreg)
529{
530 if (!sc->vreg[vreg].rpm_reg)
531 return;
532
533 disable_rpm_vreg(&sc->vreg[vreg]);
534 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800535}
536
537/* Voltage regulator initialization. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700538static int __cpuinit regulator_init(struct scalable *sc)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800539{
Matt Wagantall754ee272012-06-18 13:40:26 -0700540 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800541
Matt Wagantall754ee272012-06-18 13:40:26 -0700542 vdd_mem = calculate_vdd_mem(drv.max_acpu_lvl);
543 vdd_dig = calculate_vdd_dig(drv.max_acpu_lvl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800544
Matt Wagantall302d9a32012-07-03 13:37:29 -0700545 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
546 if (ret)
547 goto err_mem;
548 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
549 if (ret)
550 goto err_dig;
551 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700552 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700553 if (ret)
554 goto err_hfpll_a;
555 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700556 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700557 if (ret)
558 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700559
Matt Wagantall754ee272012-06-18 13:40:26 -0700560 /* Setup Krait CPU regulators and initial core voltage. */
561 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
562 sc->vreg[VREG_CORE].name);
563 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700564 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
565 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
566 sc->vreg[VREG_CORE].name, ret);
567 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700568 }
569 vdd_core = calculate_vdd_core(drv.max_acpu_lvl);
570 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
571 sc->vreg[VREG_CORE].max_vdd);
572 if (ret) {
573 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
574 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700575 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700576 }
577 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
578 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
579 sc->vreg[VREG_CORE].peak_ua);
580 if (ret < 0) {
581 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
582 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700583 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700584 }
585 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
586 if (ret) {
587 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
588 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700589 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800590 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700591
592 return 0;
593
594err_core_conf:
595 regulator_put(sc->vreg[VREG_CORE].reg);
596err_core_get:
597 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
598err_hfpll_b:
599 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
600err_hfpll_a:
601 rpm_regulator_cleanup(sc, VREG_DIG);
602err_dig:
603 rpm_regulator_cleanup(sc, VREG_MEM);
604err_mem:
605 return ret;
606}
607
608static void __cpuinit regulator_cleanup(struct scalable *sc)
609{
610 regulator_disable(sc->vreg[VREG_CORE].reg);
611 regulator_put(sc->vreg[VREG_CORE].reg);
612 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
613 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
614 rpm_regulator_cleanup(sc, VREG_DIG);
615 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800616}
617
618/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700619static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700620 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800621{
622 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700623 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800624
625 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700626 if (sc->aux_clk_sel_phys) {
627 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700628 if (!aux_reg)
629 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700630 writel_relaxed(sc->aux_clk_sel, aux_reg);
631 iounmap(aux_reg);
632 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800633
634 /* Switch away from the HFPLL while it's re-initialized. */
635 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
636 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
637 hfpll_init(sc, tgt_s);
638
639 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
640 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
641 regval &= ~(0x3 << 6);
642 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
643
644 /* Switch to the target clock source. */
645 set_sec_clk_src(sc, tgt_s->sec_src_sel);
646 set_pri_clk_src(sc, tgt_s->pri_src_sel);
647 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700648
649 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800650}
651
Matt Wagantall302d9a32012-07-03 13:37:29 -0700652static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800653{
Matt Wagantall754ee272012-06-18 13:40:26 -0700654 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall302d9a32012-07-03 13:37:29 -0700655 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800656
Matt Wagantall754ee272012-06-18 13:40:26 -0700657 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700658 if (!sc->hfpll_base) {
659 ret = -ENOMEM;
660 goto err_ioremap;
661 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700662
Matt Wagantall302d9a32012-07-03 13:37:29 -0700663 ret = regulator_init(sc);
664 if (ret)
665 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700666
Matt Wagantall302d9a32012-07-03 13:37:29 -0700667 ret = init_clock_sources(sc, &drv.max_acpu_lvl->speed);
668 if (ret)
669 goto err_clocks;
Matt Wagantall754ee272012-06-18 13:40:26 -0700670 sc->l2_vote = drv.max_acpu_lvl->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700671 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700672
673 return 0;
674
675err_clocks:
676 regulator_cleanup(sc);
677err_regulators:
678 iounmap(sc->hfpll_base);
679err_ioremap:
680 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800681}
682
683/* Register with bus driver. */
Matt Wagantall754ee272012-06-18 13:40:26 -0700684static void __init bus_init(struct msm_bus_scale_pdata *bus_scale_data)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800685{
686 int ret;
687
688 drv.bus_perf_client = msm_bus_scale_register_client(bus_scale_data);
689 if (!drv.bus_perf_client) {
690 dev_err(drv.dev, "unable to register bus client\n");
691 BUG();
692 }
693
Matt Wagantall754ee272012-06-18 13:40:26 -0700694 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
695 drv.max_acpu_lvl->l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800696 if (ret)
697 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
698}
699
700#ifdef CONFIG_CPU_FREQ_MSM
701static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
702
703static void __init cpufreq_table_init(void)
704{
705 int cpu;
706
707 for_each_possible_cpu(cpu) {
708 int i, freq_cnt = 0;
709 /* Construct the freq_table tables from acpu_freq_tbl. */
710 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
711 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
712 if (drv.acpu_freq_tbl[i].use_for_scaling) {
713 freq_table[cpu][freq_cnt].index = freq_cnt;
714 freq_table[cpu][freq_cnt].frequency
715 = drv.acpu_freq_tbl[i].speed.khz;
716 freq_cnt++;
717 }
718 }
719 /* freq_table not big enough to store all usable freqs. */
720 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
721
722 freq_table[cpu][freq_cnt].index = freq_cnt;
723 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
724
725 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
726 cpu, freq_cnt);
727
728 /* Register table with CPUFreq. */
729 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
730 }
731}
732#else
733static void __init cpufreq_table_init(void) {}
734#endif
735
736#define HOT_UNPLUG_KHZ STBY_KHZ
737static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
738 unsigned long action, void *hcpu)
739{
740 static int prev_khz[NR_CPUS];
741 int rc, cpu = (int)hcpu;
742 struct scalable *sc = &drv.scalable[cpu];
743
744 switch (action & ~CPU_TASKS_FROZEN) {
745 case CPU_DEAD:
746 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
747 /* Fall through. */
748 case CPU_UP_CANCELED:
749 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
750 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
751 break;
752 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700753 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700754 rc = per_cpu_init(cpu);
755 if (rc)
756 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700757 break;
758 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800759 if (WARN_ON(!prev_khz[cpu]))
760 return NOTIFY_BAD;
761 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
762 sc->vreg[VREG_CORE].peak_ua);
763 if (rc < 0)
764 return NOTIFY_BAD;
765 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
766 break;
767 default:
768 break;
769 }
770
771 return NOTIFY_OK;
772}
773
774static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
775 .notifier_call = acpuclk_cpu_callback,
776};
777
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700778static const int krait_needs_vmin(void)
779{
780 switch (read_cpuid_id()) {
781 case 0x511F04D0: /* KR28M2A20 */
782 case 0x511F04D1: /* KR28M2A21 */
783 case 0x510F06F0: /* KR28M4A10 */
784 return 1;
785 default:
786 return 0;
787 };
788}
789
790static void krait_apply_vmin(struct acpu_level *tbl)
791{
792 for (; tbl->speed.khz != 0; tbl++)
793 if (tbl->vdd_core < 1150000)
794 tbl->vdd_core = 1150000;
795}
796
Matt Wagantall754ee272012-06-18 13:40:26 -0700797static void __init select_freq_plan(struct acpu_level *const *pvs_tbl,
798 u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800799{
Matt Wagantall754ee272012-06-18 13:40:26 -0700800 const struct acpu_level *l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800801 void __iomem *qfprom_base;
802 u32 pte_efuse, pvs, tbl_idx;
803 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Unknown" };
804
805 qfprom_base = ioremap(qfprom_phys, SZ_256);
806 /* Select frequency tables. */
807 if (qfprom_base) {
808 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
809 pvs = (pte_efuse >> 10) & 0x7;
810 iounmap(qfprom_base);
811 if (pvs == 0x7)
812 pvs = (pte_efuse >> 13) & 0x7;
813
814 switch (pvs) {
815 case 0x0:
816 case 0x7:
817 tbl_idx = PVS_SLOW;
818 break;
819 case 0x1:
820 tbl_idx = PVS_NOMINAL;
821 break;
822 case 0x3:
823 tbl_idx = PVS_FAST;
824 break;
825 default:
826 tbl_idx = PVS_UNKNOWN;
827 break;
828 }
829 } else {
830 tbl_idx = PVS_UNKNOWN;
831 dev_err(drv.dev, "Unable to map QFPROM base\n");
832 }
833 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
834 if (tbl_idx == PVS_UNKNOWN) {
835 tbl_idx = PVS_SLOW;
836 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
837 pvs_names[tbl_idx]);
838 }
839 drv.acpu_freq_tbl = pvs_tbl[tbl_idx];
840
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700841 if (krait_needs_vmin())
842 krait_apply_vmin(drv.acpu_freq_tbl);
843
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800844 /* Find the max supported scaling frequency. */
845 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
846 if (l->use_for_scaling)
Matt Wagantall754ee272012-06-18 13:40:26 -0700847 drv.max_acpu_lvl = l;
848 BUG_ON(!drv.max_acpu_lvl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800849 dev_info(drv.dev, "Max ACPU freq: %lu KHz\n",
Matt Wagantall754ee272012-06-18 13:40:26 -0700850 drv.max_acpu_lvl->speed.khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800851}
852
853static struct acpuclk_data acpuclk_krait_data = {
854 .set_rate = acpuclk_krait_set_rate,
855 .get_rate = acpuclk_krait_get_rate,
856 .power_collapse_khz = STBY_KHZ,
857 .wait_for_irq_khz = STBY_KHZ,
858};
859
860int __init acpuclk_krait_init(struct device *dev,
861 const struct acpuclk_krait_params *params)
862{
Matt Wagantall754ee272012-06-18 13:40:26 -0700863 struct scalable *l2;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700864 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800865
866 drv.scalable = params->scalable;
867 drv.l2_freq_tbl = params->l2_freq_tbl;
868 drv.dev = dev;
869
Matt Wagantall754ee272012-06-18 13:40:26 -0700870 select_freq_plan(params->pvs_acpu_freq_tbl, params->qfprom_phys_base);
871 bus_init(params->bus_scale_data);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800872
Matt Wagantall754ee272012-06-18 13:40:26 -0700873 l2 = &drv.scalable[L2];
874 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
875 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -0700876
Matt Wagantall302d9a32012-07-03 13:37:29 -0700877 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
878 l2->vreg[VREG_HFPLL_A].max_vdd, false);
879 BUG_ON(rc);
880 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
881 l2->vreg[VREG_HFPLL_B].max_vdd, false);
882 BUG_ON(rc);
883
884 rc = init_clock_sources(l2, &drv.max_acpu_lvl->l2_level->speed);
885 BUG_ON(rc);
886
887 for_each_online_cpu(cpu) {
888 rc = per_cpu_init(cpu);
889 BUG_ON(rc);
890 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800891
892 cpufreq_table_init();
893
894 acpuclk_register(&acpuclk_krait_data);
895 register_hotcpu_notifier(&acpuclk_cpu_notifier);
896
897 return 0;
898}