blob: 8bd54e30b9f51edb140686ebf841f5702f73e4ce [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
Matt Wagantall5941a332012-07-10 23:20:44 -0700388 if (cpu > num_possible_cpus())
389 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800390
391 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
392 mutex_lock(&driver_lock);
393
394 strt_acpu_s = drv.scalable[cpu].cur_speed;
395
396 /* Return early if rate didn't change. */
397 if (rate == strt_acpu_s->khz)
398 goto out;
399
400 /* Find target frequency. */
401 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
402 if (tgt->speed.khz == rate) {
403 tgt_acpu_s = &tgt->speed;
404 break;
405 }
406 }
407 if (tgt->speed.khz == 0) {
408 rc = -EINVAL;
409 goto out;
410 }
411
412 /* Calculate voltage requirements for the current CPU. */
413 vdd_mem = calculate_vdd_mem(tgt);
414 vdd_dig = calculate_vdd_dig(tgt);
415 vdd_core = calculate_vdd_core(tgt);
416
417 /* Increase VDD levels if needed. */
418 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
419 rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
420 if (rc)
421 goto out;
422 }
423
424 pr_debug("Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
425 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
426
427 /* Set the new CPU speed. */
428 set_speed(&drv.scalable[cpu], tgt_acpu_s);
429
430 /*
431 * Update the L2 vote and apply the rate change. A spinlock is
432 * necessary to ensure L2 rate is calculated and set atomically
433 * with the CPU frequency, even if acpuclk_krait_set_rate() is
434 * called from an atomic context and the driver_lock mutex is not
435 * acquired.
436 */
437 spin_lock_irqsave(&l2_lock, flags);
438 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
439 set_speed(&drv.scalable[L2], &tgt_l2_l->speed);
440 spin_unlock_irqrestore(&l2_lock, flags);
441
442 /* Nothing else to do for power collapse or SWFI. */
443 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
444 goto out;
445
446 /* Update bus bandwith request. */
447 set_bus_bw(tgt_l2_l->bw_level);
448
449 /* Drop VDD levels if we can. */
450 decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
451
452 pr_debug("ACPU%d speed change complete\n", cpu);
453
454out:
455 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
456 mutex_unlock(&driver_lock);
457 return rc;
458}
459
460/* Initialize a HFPLL at a given rate and enable it. */
461static void __init hfpll_init(struct scalable *sc,
462 const struct core_speed *tgt_s)
463{
464 pr_debug("Initializing HFPLL%d\n", sc - drv.scalable);
465
466 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700467 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800468
469 /* Configure PLL parameters for integer mode. */
470 writel_relaxed(sc->hfpll_data->config_val,
471 sc->hfpll_base + sc->hfpll_data->config_offset);
472 writel_relaxed(0, sc->hfpll_base + sc->hfpll_data->m_offset);
473 writel_relaxed(1, sc->hfpll_base + sc->hfpll_data->n_offset);
474
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700475 /* Program droop controller, if supported */
476 if (sc->hfpll_data->has_droop_ctl)
477 writel_relaxed(sc->hfpll_data->droop_val,
478 sc->hfpll_base + sc->hfpll_data->droop_offset);
479
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800480 /* Set an initial rate and enable the PLL. */
481 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700482 hfpll_enable(sc, false);
483}
484
Matt Wagantall302d9a32012-07-03 13:37:29 -0700485static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700486 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700487{
488 int ret;
489
490 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700491 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700492
493 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
494 sc->vreg[vreg].name);
495 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700496 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
497 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
498 sc->vreg[vreg].name, ret);
499 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700500 }
501
502 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
503 sc->vreg[vreg].max_vdd);
504 if (ret) {
505 dev_err(drv.dev, "%s initialization failed (%d)\n",
506 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700507 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700508 }
509 sc->vreg[vreg].cur_vdd = vdd;
510
Matt Wagantall302d9a32012-07-03 13:37:29 -0700511 if (enable) {
512 ret = enable_rpm_vreg(&sc->vreg[vreg]);
513 if (ret)
514 goto err_conf;
515 }
516
517 return 0;
518
519err_conf:
520 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
521err_get:
522 return ret;
523}
524
525static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
526 enum vregs vreg)
527{
528 if (!sc->vreg[vreg].rpm_reg)
529 return;
530
531 disable_rpm_vreg(&sc->vreg[vreg]);
532 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800533}
534
535/* Voltage regulator initialization. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700536static int __cpuinit regulator_init(struct scalable *sc)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800537{
Matt Wagantall754ee272012-06-18 13:40:26 -0700538 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800539
Matt Wagantall754ee272012-06-18 13:40:26 -0700540 vdd_mem = calculate_vdd_mem(drv.max_acpu_lvl);
541 vdd_dig = calculate_vdd_dig(drv.max_acpu_lvl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800542
Matt Wagantall302d9a32012-07-03 13:37:29 -0700543 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
544 if (ret)
545 goto err_mem;
546 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
547 if (ret)
548 goto err_dig;
549 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700550 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700551 if (ret)
552 goto err_hfpll_a;
553 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700554 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700555 if (ret)
556 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700557
Matt Wagantall754ee272012-06-18 13:40:26 -0700558 /* Setup Krait CPU regulators and initial core voltage. */
559 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
560 sc->vreg[VREG_CORE].name);
561 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700562 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
563 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
564 sc->vreg[VREG_CORE].name, ret);
565 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700566 }
567 vdd_core = calculate_vdd_core(drv.max_acpu_lvl);
568 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
569 sc->vreg[VREG_CORE].max_vdd);
570 if (ret) {
571 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
572 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700573 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700574 }
575 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
576 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
577 sc->vreg[VREG_CORE].peak_ua);
578 if (ret < 0) {
579 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
580 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700581 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700582 }
583 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
584 if (ret) {
585 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
586 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700587 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800588 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700589
590 return 0;
591
592err_core_conf:
593 regulator_put(sc->vreg[VREG_CORE].reg);
594err_core_get:
595 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
596err_hfpll_b:
597 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
598err_hfpll_a:
599 rpm_regulator_cleanup(sc, VREG_DIG);
600err_dig:
601 rpm_regulator_cleanup(sc, VREG_MEM);
602err_mem:
603 return ret;
604}
605
606static void __cpuinit regulator_cleanup(struct scalable *sc)
607{
608 regulator_disable(sc->vreg[VREG_CORE].reg);
609 regulator_put(sc->vreg[VREG_CORE].reg);
610 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
611 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
612 rpm_regulator_cleanup(sc, VREG_DIG);
613 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800614}
615
616/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700617static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700618 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800619{
620 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700621 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800622
623 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700624 if (sc->aux_clk_sel_phys) {
625 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700626 if (!aux_reg)
627 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700628 writel_relaxed(sc->aux_clk_sel, aux_reg);
629 iounmap(aux_reg);
630 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800631
632 /* Switch away from the HFPLL while it's re-initialized. */
633 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
634 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
635 hfpll_init(sc, tgt_s);
636
637 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
638 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
639 regval &= ~(0x3 << 6);
640 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
641
642 /* Switch to the target clock source. */
643 set_sec_clk_src(sc, tgt_s->sec_src_sel);
644 set_pri_clk_src(sc, tgt_s->pri_src_sel);
645 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700646
647 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800648}
649
Matt Wagantall302d9a32012-07-03 13:37:29 -0700650static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800651{
Matt Wagantall754ee272012-06-18 13:40:26 -0700652 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall302d9a32012-07-03 13:37:29 -0700653 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800654
Matt Wagantall754ee272012-06-18 13:40:26 -0700655 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700656 if (!sc->hfpll_base) {
657 ret = -ENOMEM;
658 goto err_ioremap;
659 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700660
Matt Wagantall302d9a32012-07-03 13:37:29 -0700661 ret = regulator_init(sc);
662 if (ret)
663 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700664
Matt Wagantall302d9a32012-07-03 13:37:29 -0700665 ret = init_clock_sources(sc, &drv.max_acpu_lvl->speed);
666 if (ret)
667 goto err_clocks;
Matt Wagantall754ee272012-06-18 13:40:26 -0700668 sc->l2_vote = drv.max_acpu_lvl->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700669 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700670
671 return 0;
672
673err_clocks:
674 regulator_cleanup(sc);
675err_regulators:
676 iounmap(sc->hfpll_base);
677err_ioremap:
678 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800679}
680
681/* Register with bus driver. */
Matt Wagantall754ee272012-06-18 13:40:26 -0700682static void __init bus_init(struct msm_bus_scale_pdata *bus_scale_data)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800683{
684 int ret;
685
686 drv.bus_perf_client = msm_bus_scale_register_client(bus_scale_data);
687 if (!drv.bus_perf_client) {
688 dev_err(drv.dev, "unable to register bus client\n");
689 BUG();
690 }
691
Matt Wagantall754ee272012-06-18 13:40:26 -0700692 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
693 drv.max_acpu_lvl->l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800694 if (ret)
695 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
696}
697
698#ifdef CONFIG_CPU_FREQ_MSM
699static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
700
701static void __init cpufreq_table_init(void)
702{
703 int cpu;
704
705 for_each_possible_cpu(cpu) {
706 int i, freq_cnt = 0;
707 /* Construct the freq_table tables from acpu_freq_tbl. */
708 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
709 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
710 if (drv.acpu_freq_tbl[i].use_for_scaling) {
711 freq_table[cpu][freq_cnt].index = freq_cnt;
712 freq_table[cpu][freq_cnt].frequency
713 = drv.acpu_freq_tbl[i].speed.khz;
714 freq_cnt++;
715 }
716 }
717 /* freq_table not big enough to store all usable freqs. */
718 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
719
720 freq_table[cpu][freq_cnt].index = freq_cnt;
721 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
722
723 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
724 cpu, freq_cnt);
725
726 /* Register table with CPUFreq. */
727 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
728 }
729}
730#else
731static void __init cpufreq_table_init(void) {}
732#endif
733
734#define HOT_UNPLUG_KHZ STBY_KHZ
735static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
736 unsigned long action, void *hcpu)
737{
738 static int prev_khz[NR_CPUS];
739 int rc, cpu = (int)hcpu;
740 struct scalable *sc = &drv.scalable[cpu];
741
742 switch (action & ~CPU_TASKS_FROZEN) {
743 case CPU_DEAD:
744 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
745 /* Fall through. */
746 case CPU_UP_CANCELED:
747 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
748 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
749 break;
750 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700751 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700752 rc = per_cpu_init(cpu);
753 if (rc)
754 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700755 break;
756 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800757 if (WARN_ON(!prev_khz[cpu]))
758 return NOTIFY_BAD;
759 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
760 sc->vreg[VREG_CORE].peak_ua);
761 if (rc < 0)
762 return NOTIFY_BAD;
763 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
764 break;
765 default:
766 break;
767 }
768
769 return NOTIFY_OK;
770}
771
772static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
773 .notifier_call = acpuclk_cpu_callback,
774};
775
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700776static const int krait_needs_vmin(void)
777{
778 switch (read_cpuid_id()) {
779 case 0x511F04D0: /* KR28M2A20 */
780 case 0x511F04D1: /* KR28M2A21 */
781 case 0x510F06F0: /* KR28M4A10 */
782 return 1;
783 default:
784 return 0;
785 };
786}
787
788static void krait_apply_vmin(struct acpu_level *tbl)
789{
790 for (; tbl->speed.khz != 0; tbl++)
791 if (tbl->vdd_core < 1150000)
792 tbl->vdd_core = 1150000;
793}
794
Matt Wagantall754ee272012-06-18 13:40:26 -0700795static void __init select_freq_plan(struct acpu_level *const *pvs_tbl,
796 u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800797{
Matt Wagantall754ee272012-06-18 13:40:26 -0700798 const struct acpu_level *l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800799 void __iomem *qfprom_base;
800 u32 pte_efuse, pvs, tbl_idx;
801 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Unknown" };
802
803 qfprom_base = ioremap(qfprom_phys, SZ_256);
804 /* Select frequency tables. */
805 if (qfprom_base) {
806 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
807 pvs = (pte_efuse >> 10) & 0x7;
808 iounmap(qfprom_base);
809 if (pvs == 0x7)
810 pvs = (pte_efuse >> 13) & 0x7;
811
812 switch (pvs) {
813 case 0x0:
814 case 0x7:
815 tbl_idx = PVS_SLOW;
816 break;
817 case 0x1:
818 tbl_idx = PVS_NOMINAL;
819 break;
820 case 0x3:
821 tbl_idx = PVS_FAST;
822 break;
823 default:
824 tbl_idx = PVS_UNKNOWN;
825 break;
826 }
827 } else {
828 tbl_idx = PVS_UNKNOWN;
829 dev_err(drv.dev, "Unable to map QFPROM base\n");
830 }
831 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
832 if (tbl_idx == PVS_UNKNOWN) {
833 tbl_idx = PVS_SLOW;
834 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
835 pvs_names[tbl_idx]);
836 }
837 drv.acpu_freq_tbl = pvs_tbl[tbl_idx];
838
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700839 if (krait_needs_vmin())
840 krait_apply_vmin(drv.acpu_freq_tbl);
841
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800842 /* Find the max supported scaling frequency. */
843 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
844 if (l->use_for_scaling)
Matt Wagantall754ee272012-06-18 13:40:26 -0700845 drv.max_acpu_lvl = l;
846 BUG_ON(!drv.max_acpu_lvl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800847 dev_info(drv.dev, "Max ACPU freq: %lu KHz\n",
Matt Wagantall754ee272012-06-18 13:40:26 -0700848 drv.max_acpu_lvl->speed.khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800849}
850
851static struct acpuclk_data acpuclk_krait_data = {
852 .set_rate = acpuclk_krait_set_rate,
853 .get_rate = acpuclk_krait_get_rate,
854 .power_collapse_khz = STBY_KHZ,
855 .wait_for_irq_khz = STBY_KHZ,
856};
857
858int __init acpuclk_krait_init(struct device *dev,
859 const struct acpuclk_krait_params *params)
860{
Matt Wagantall754ee272012-06-18 13:40:26 -0700861 struct scalable *l2;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700862 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800863
864 drv.scalable = params->scalable;
865 drv.l2_freq_tbl = params->l2_freq_tbl;
866 drv.dev = dev;
867
Matt Wagantall754ee272012-06-18 13:40:26 -0700868 select_freq_plan(params->pvs_acpu_freq_tbl, params->qfprom_phys_base);
869 bus_init(params->bus_scale_data);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800870
Matt Wagantall754ee272012-06-18 13:40:26 -0700871 l2 = &drv.scalable[L2];
872 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
873 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -0700874
Matt Wagantall302d9a32012-07-03 13:37:29 -0700875 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
876 l2->vreg[VREG_HFPLL_A].max_vdd, false);
877 BUG_ON(rc);
878 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
879 l2->vreg[VREG_HFPLL_B].max_vdd, false);
880 BUG_ON(rc);
881
882 rc = init_clock_sources(l2, &drv.max_acpu_lvl->l2_level->speed);
883 BUG_ON(rc);
884
885 for_each_online_cpu(cpu) {
886 rc = per_cpu_init(cpu);
887 BUG_ON(rc);
888 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800889
890 cpufreq_table_init();
891
892 acpuclk_register(&acpuclk_krait_data);
893 register_hotcpu_notifier(&acpuclk_cpu_notifier);
894
895 return 0;
896}