blob: 4dc47d2bfedae33cd9b8606f66ef8425019e7339 [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 Wagantalle9b715a2012-01-04 18:16:14 -080057 const struct l2_level *l2_freq_tbl;
58 struct scalable *scalable;
59 u32 bus_perf_client;
60 struct device *dev;
61} drv;
62
63static unsigned long acpuclk_krait_get_rate(int cpu)
64{
65 return drv.scalable[cpu].cur_speed->khz;
66}
67
68/* Select a source on the primary MUX. */
69static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
70{
71 u32 regval;
72
73 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
74 regval &= ~0x3;
75 regval |= (pri_src_sel & 0x3);
76 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
77 /* Wait for switch to complete. */
78 mb();
79 udelay(1);
80}
81
82/* Select a source on the secondary MUX. */
83static void set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
84{
85 u32 regval;
86
87 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
88 regval &= ~(0x3 << 2);
89 regval |= ((sec_src_sel & 0x3) << 2);
90 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
91 /* Wait for switch to complete. */
92 mb();
93 udelay(1);
94}
95
Matt Wagantall75473eb2012-05-31 15:23:22 -070096static void enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080097{
98 int rc;
99
Matt Wagantall75473eb2012-05-31 15:23:22 -0700100 if (vreg->rpm_reg) {
101 rc = rpm_regulator_enable(vreg->rpm_reg);
102 if (rc) {
103 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
104 vreg->name, rc);
105 BUG();
106 }
107 }
108}
109
110static void disable_rpm_vreg(struct vreg *vreg)
111{
112 int rc;
113
114 if (vreg->rpm_reg) {
115 rc = rpm_regulator_disable(vreg->rpm_reg);
116 if (rc)
117 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
118 vreg->name, rc);
119 }
120}
121
122/* Enable an already-configured HFPLL. */
123static void hfpll_enable(struct scalable *sc, bool skip_regulators)
124{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800125 if (!skip_regulators) {
126 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700127 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
128 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800129 }
130
131 /* Disable PLL bypass mode. */
132 writel_relaxed(0x2, sc->hfpll_base + sc->hfpll_data->mode_offset);
133
134 /*
135 * H/W requires a 5us delay between disabling the bypass and
136 * de-asserting the reset. Delay 10us just to be safe.
137 */
138 mb();
139 udelay(10);
140
141 /* De-assert active-low PLL reset. */
142 writel_relaxed(0x6, sc->hfpll_base + sc->hfpll_data->mode_offset);
143
144 /* Wait for PLL to lock. */
145 mb();
146 udelay(60);
147
148 /* Enable PLL output. */
149 writel_relaxed(0x7, sc->hfpll_base + sc->hfpll_data->mode_offset);
150}
151
152/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
153static void hfpll_disable(struct scalable *sc, bool skip_regulators)
154{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800155 /*
156 * Disable the PLL output, disable test mode, enable the bypass mode,
157 * and assert the reset.
158 */
159 writel_relaxed(0, sc->hfpll_base + sc->hfpll_data->mode_offset);
160
161 if (!skip_regulators) {
162 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700163 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
164 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800165 }
166}
167
168/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
169static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
170{
171 writel_relaxed(tgt_s->pll_l_val,
172 sc->hfpll_base + sc->hfpll_data->l_offset);
173}
174
175/* Return the L2 speed that should be applied. */
176static const struct l2_level *compute_l2_level(struct scalable *sc,
177 const struct l2_level *vote_l)
178{
179 const struct l2_level *new_l;
180 int cpu;
181
182 /* Find max L2 speed vote. */
183 sc->l2_vote = vote_l;
184 new_l = drv.l2_freq_tbl;
185 for_each_present_cpu(cpu)
186 new_l = max(new_l, drv.scalable[cpu].l2_vote);
187
188 return new_l;
189}
190
191/* Update the bus bandwidth request. */
192static void set_bus_bw(unsigned int bw)
193{
194 int ret;
195
196 /* Update bandwidth if request has changed. This may sleep. */
197 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
198 if (ret)
199 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
200}
201
202/* Set the CPU or L2 clock speed. */
203static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
204{
205 const struct core_speed *strt_s = sc->cur_speed;
206
207 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
208 /*
209 * Move to an always-on source running at a frequency
210 * that does not require an elevated CPU voltage.
211 */
212 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
213 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
214
215 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700216 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800217 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700218 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800219
220 /* Move to HFPLL. */
221 set_pri_clk_src(sc, tgt_s->pri_src_sel);
222 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
223 set_sec_clk_src(sc, tgt_s->sec_src_sel);
224 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700225 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800226 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
227 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700228 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800229 set_pri_clk_src(sc, tgt_s->pri_src_sel);
230 } else {
231 set_sec_clk_src(sc, tgt_s->sec_src_sel);
232 }
233
234 sc->cur_speed = tgt_s;
235}
236
237/* Apply any per-cpu voltage increases. */
238static int increase_vdd(int cpu, int vdd_core, int vdd_mem, int vdd_dig,
239 enum setrate_reason reason)
240{
241 struct scalable *sc = &drv.scalable[cpu];
242 int rc = 0;
243
244 /*
245 * Increase vdd_mem active-set before vdd_dig.
246 * vdd_mem should be >= vdd_dig.
247 */
248 if (vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700249 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
250 vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800251 if (rc) {
252 dev_err(drv.dev,
253 "vdd_mem (cpu%d) increase failed (%d)\n",
254 cpu, rc);
255 return rc;
256 }
257 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
258 }
259
260 /* Increase vdd_dig active-set vote. */
261 if (vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700262 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
263 vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800264 if (rc) {
265 dev_err(drv.dev,
266 "vdd_dig (cpu%d) increase failed (%d)\n",
267 cpu, rc);
268 return rc;
269 }
270 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
271 }
272
273 /*
274 * Update per-CPU core voltage. Don't do this for the hotplug path for
275 * which it should already be correct. Attempting to set it is bad
276 * because we don't know what CPU we are running on at this point, but
277 * the CPU regulator API requires we call it from the affected CPU.
278 */
279 if (vdd_core > sc->vreg[VREG_CORE].cur_vdd
280 && reason != SETRATE_HOTPLUG) {
281 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
282 sc->vreg[VREG_CORE].max_vdd);
283 if (rc) {
284 dev_err(drv.dev,
285 "vdd_core (cpu%d) increase failed (%d)\n",
286 cpu, rc);
287 return rc;
288 }
289 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
290 }
291
292 return rc;
293}
294
295/* Apply any per-cpu voltage decreases. */
296static void decrease_vdd(int cpu, int vdd_core, int vdd_mem, int vdd_dig,
297 enum setrate_reason reason)
298{
299 struct scalable *sc = &drv.scalable[cpu];
300 int ret;
301
302 /*
303 * Update per-CPU core voltage. This must be called on the CPU
304 * that's being affected. Don't do this in the hotplug remove path,
305 * where the rail is off and we're executing on the other CPU.
306 */
307 if (vdd_core < sc->vreg[VREG_CORE].cur_vdd
308 && reason != SETRATE_HOTPLUG) {
309 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
310 sc->vreg[VREG_CORE].max_vdd);
311 if (ret) {
312 dev_err(drv.dev,
313 "vdd_core (cpu%d) decrease failed (%d)\n",
314 cpu, ret);
315 return;
316 }
317 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
318 }
319
320 /* Decrease vdd_dig active-set vote. */
321 if (vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700322 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
323 vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800324 if (ret) {
325 dev_err(drv.dev,
326 "vdd_dig (cpu%d) decrease failed (%d)\n",
327 cpu, ret);
328 return;
329 }
330 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
331 }
332
333 /*
334 * Decrease vdd_mem active-set after vdd_dig.
335 * vdd_mem should be >= vdd_dig.
336 */
337 if (vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700338 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
339 vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800340 if (ret) {
341 dev_err(drv.dev,
342 "vdd_mem (cpu%d) decrease failed (%d)\n",
343 cpu, ret);
344 return;
345 }
346 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
347 }
348}
349
350static int calculate_vdd_mem(const struct acpu_level *tgt)
351{
352 return tgt->l2_level->vdd_mem;
353}
354
355static int calculate_vdd_dig(const struct acpu_level *tgt)
356{
357 int pll_vdd_dig;
358 const int *hfpll_vdd = drv.scalable[L2].hfpll_data->vdd;
359 const u32 low_vdd_l_max = drv.scalable[L2].hfpll_data->low_vdd_l_max;
360
361 if (tgt->l2_level->speed.src != HFPLL)
362 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_NONE];
363 else if (tgt->l2_level->speed.pll_l_val > low_vdd_l_max)
364 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_NOM];
365 else
366 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_LOW];
367
368 return max(tgt->l2_level->vdd_dig, pll_vdd_dig);
369}
370
371static int calculate_vdd_core(const struct acpu_level *tgt)
372{
373 return tgt->vdd_core;
374}
375
376/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
377static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
378 enum setrate_reason reason)
379{
380 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
381 const struct l2_level *tgt_l2_l;
382 const struct acpu_level *tgt;
383 int vdd_mem, vdd_dig, vdd_core;
384 unsigned long flags;
385 int rc = 0;
386
387 if (cpu > num_possible_cpus()) {
388 rc = -EINVAL;
389 goto out;
390 }
391
392 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
393 mutex_lock(&driver_lock);
394
395 strt_acpu_s = drv.scalable[cpu].cur_speed;
396
397 /* Return early if rate didn't change. */
398 if (rate == strt_acpu_s->khz)
399 goto out;
400
401 /* Find target frequency. */
402 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
403 if (tgt->speed.khz == rate) {
404 tgt_acpu_s = &tgt->speed;
405 break;
406 }
407 }
408 if (tgt->speed.khz == 0) {
409 rc = -EINVAL;
410 goto out;
411 }
412
413 /* Calculate voltage requirements for the current CPU. */
414 vdd_mem = calculate_vdd_mem(tgt);
415 vdd_dig = calculate_vdd_dig(tgt);
416 vdd_core = calculate_vdd_core(tgt);
417
418 /* Increase VDD levels if needed. */
419 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
420 rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
421 if (rc)
422 goto out;
423 }
424
425 pr_debug("Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
426 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
427
428 /* Set the new CPU speed. */
429 set_speed(&drv.scalable[cpu], tgt_acpu_s);
430
431 /*
432 * Update the L2 vote and apply the rate change. A spinlock is
433 * necessary to ensure L2 rate is calculated and set atomically
434 * with the CPU frequency, even if acpuclk_krait_set_rate() is
435 * called from an atomic context and the driver_lock mutex is not
436 * acquired.
437 */
438 spin_lock_irqsave(&l2_lock, flags);
439 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
440 set_speed(&drv.scalable[L2], &tgt_l2_l->speed);
441 spin_unlock_irqrestore(&l2_lock, flags);
442
443 /* Nothing else to do for power collapse or SWFI. */
444 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
445 goto out;
446
447 /* Update bus bandwith request. */
448 set_bus_bw(tgt_l2_l->bw_level);
449
450 /* Drop VDD levels if we can. */
451 decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
452
453 pr_debug("ACPU%d speed change complete\n", cpu);
454
455out:
456 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
457 mutex_unlock(&driver_lock);
458 return rc;
459}
460
461/* Initialize a HFPLL at a given rate and enable it. */
462static void __init hfpll_init(struct scalable *sc,
463 const struct core_speed *tgt_s)
464{
465 pr_debug("Initializing HFPLL%d\n", sc - drv.scalable);
466
467 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700468 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800469
470 /* Configure PLL parameters for integer mode. */
471 writel_relaxed(sc->hfpll_data->config_val,
472 sc->hfpll_base + sc->hfpll_data->config_offset);
473 writel_relaxed(0, sc->hfpll_base + sc->hfpll_data->m_offset);
474 writel_relaxed(1, sc->hfpll_base + sc->hfpll_data->n_offset);
475
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700476 /* Program droop controller, if supported */
477 if (sc->hfpll_data->has_droop_ctl)
478 writel_relaxed(sc->hfpll_data->droop_val,
479 sc->hfpll_base + sc->hfpll_data->droop_offset);
480
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800481 /* Set an initial rate and enable the PLL. */
482 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700483 hfpll_enable(sc, false);
484}
485
486static void __init rpm_regulator_init(struct scalable *sc, enum vregs vreg,
487 int vdd, bool enable)
488{
489 int ret;
490
491 if (!sc->vreg[vreg].name)
492 return;
493
494 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
495 sc->vreg[vreg].name);
496 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
497 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%ld)\n",
498 sc->vreg[vreg].name,
499 PTR_ERR(sc->vreg[vreg].rpm_reg));
500 BUG();
501 }
502
503 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
504 sc->vreg[vreg].max_vdd);
505 if (ret) {
506 dev_err(drv.dev, "%s initialization failed (%d)\n",
507 sc->vreg[vreg].name, ret);
508 BUG();
509 }
510 sc->vreg[vreg].cur_vdd = vdd;
511
512 if (enable)
513 enable_rpm_vreg(&sc->vreg[vreg]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800514}
515
516/* Voltage regulator initialization. */
Matt Wagantallbf9eb2c2012-05-31 09:44:22 -0700517static void __init regulator_init(struct device *dev,
518 const struct acpu_level *lvl)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800519{
520 int cpu, ret;
521 struct scalable *sc;
522 int vdd_mem, vdd_dig, vdd_core;
523
524 vdd_mem = calculate_vdd_mem(lvl);
525 vdd_dig = calculate_vdd_dig(lvl);
526
Matt Wagantall75473eb2012-05-31 15:23:22 -0700527 rpm_regulator_init(&drv.scalable[L2], VREG_HFPLL_A,
528 drv.scalable[L2].vreg[VREG_HFPLL_A].max_vdd, false);
529 rpm_regulator_init(&drv.scalable[L2], VREG_HFPLL_B,
530 drv.scalable[L2].vreg[VREG_HFPLL_B].max_vdd, false);
531
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800532 for_each_possible_cpu(cpu) {
533 sc = &drv.scalable[cpu];
534
Matt Wagantall75473eb2012-05-31 15:23:22 -0700535 rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
536 rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
537 rpm_regulator_init(sc, VREG_HFPLL_A,
538 sc->vreg[VREG_HFPLL_A].max_vdd, false);
539 rpm_regulator_init(sc, VREG_HFPLL_B,
540 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800541
542 /* Setup Krait CPU regulators and initial core voltage. */
Matt Wagantallbf9eb2c2012-05-31 09:44:22 -0700543 sc->vreg[VREG_CORE].reg = regulator_get(dev,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800544 sc->vreg[VREG_CORE].name);
545 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
546 dev_err(drv.dev, "regulator_get(%s) failed (%ld)\n",
547 sc->vreg[VREG_CORE].name,
548 PTR_ERR(sc->vreg[VREG_CORE].reg));
549 BUG();
550 }
551 vdd_core = calculate_vdd_core(lvl);
552 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
553 sc->vreg[VREG_CORE].max_vdd);
554 if (ret) {
555 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
556 sc->vreg[VREG_CORE].name, ret);
557 BUG();
558 }
559 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
560 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
561 sc->vreg[VREG_CORE].peak_ua);
562 if (ret < 0) {
563 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed"
564 " (%d)\n", sc->vreg[VREG_CORE].name, ret);
565 BUG();
566 }
567 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
568 if (ret) {
569 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
570 sc->vreg[VREG_CORE].name, ret);
571 BUG();
572 }
573 }
574}
575
576/* Set initial rate for a given core. */
577static void __init init_clock_sources(struct scalable *sc,
578 const struct core_speed *tgt_s)
579{
580 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700581 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800582
583 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700584 if (sc->aux_clk_sel_phys) {
585 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
586 BUG_ON(!aux_reg);
587 writel_relaxed(sc->aux_clk_sel, aux_reg);
588 iounmap(aux_reg);
589 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800590
591 /* Switch away from the HFPLL while it's re-initialized. */
592 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
593 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
594 hfpll_init(sc, tgt_s);
595
596 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
597 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
598 regval &= ~(0x3 << 6);
599 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
600
601 /* Switch to the target clock source. */
602 set_sec_clk_src(sc, tgt_s->sec_src_sel);
603 set_pri_clk_src(sc, tgt_s->pri_src_sel);
604 sc->cur_speed = tgt_s;
605}
606
607static void __init per_cpu_init(int cpu, const struct acpu_level *max_level)
608{
609 drv.scalable[cpu].hfpll_base =
610 ioremap(drv.scalable[cpu].hfpll_phys_base, SZ_32);
611 BUG_ON(!drv.scalable[cpu].hfpll_base);
612
613 init_clock_sources(&drv.scalable[cpu], &max_level->speed);
614 drv.scalable[cpu].l2_vote = max_level->l2_level;
615}
616
617/* Register with bus driver. */
618static void __init bus_init(struct msm_bus_scale_pdata *bus_scale_data,
619 unsigned int init_bw)
620{
621 int ret;
622
623 drv.bus_perf_client = msm_bus_scale_register_client(bus_scale_data);
624 if (!drv.bus_perf_client) {
625 dev_err(drv.dev, "unable to register bus client\n");
626 BUG();
627 }
628
629 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, init_bw);
630 if (ret)
631 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
632}
633
634#ifdef CONFIG_CPU_FREQ_MSM
635static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
636
637static void __init cpufreq_table_init(void)
638{
639 int cpu;
640
641 for_each_possible_cpu(cpu) {
642 int i, freq_cnt = 0;
643 /* Construct the freq_table tables from acpu_freq_tbl. */
644 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
645 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
646 if (drv.acpu_freq_tbl[i].use_for_scaling) {
647 freq_table[cpu][freq_cnt].index = freq_cnt;
648 freq_table[cpu][freq_cnt].frequency
649 = drv.acpu_freq_tbl[i].speed.khz;
650 freq_cnt++;
651 }
652 }
653 /* freq_table not big enough to store all usable freqs. */
654 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
655
656 freq_table[cpu][freq_cnt].index = freq_cnt;
657 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
658
659 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
660 cpu, freq_cnt);
661
662 /* Register table with CPUFreq. */
663 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
664 }
665}
666#else
667static void __init cpufreq_table_init(void) {}
668#endif
669
670#define HOT_UNPLUG_KHZ STBY_KHZ
671static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
672 unsigned long action, void *hcpu)
673{
674 static int prev_khz[NR_CPUS];
675 int rc, cpu = (int)hcpu;
676 struct scalable *sc = &drv.scalable[cpu];
677
678 switch (action & ~CPU_TASKS_FROZEN) {
679 case CPU_DEAD:
680 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
681 /* Fall through. */
682 case CPU_UP_CANCELED:
683 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
684 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
685 break;
686 case CPU_UP_PREPARE:
687 if (WARN_ON(!prev_khz[cpu]))
688 return NOTIFY_BAD;
689 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
690 sc->vreg[VREG_CORE].peak_ua);
691 if (rc < 0)
692 return NOTIFY_BAD;
693 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
694 break;
695 default:
696 break;
697 }
698
699 return NOTIFY_OK;
700}
701
702static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
703 .notifier_call = acpuclk_cpu_callback,
704};
705
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700706static const int krait_needs_vmin(void)
707{
708 switch (read_cpuid_id()) {
709 case 0x511F04D0: /* KR28M2A20 */
710 case 0x511F04D1: /* KR28M2A21 */
711 case 0x510F06F0: /* KR28M4A10 */
712 return 1;
713 default:
714 return 0;
715 };
716}
717
718static void krait_apply_vmin(struct acpu_level *tbl)
719{
720 for (; tbl->speed.khz != 0; tbl++)
721 if (tbl->vdd_core < 1150000)
722 tbl->vdd_core = 1150000;
723}
724
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800725static const struct acpu_level __init *select_freq_plan(
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700726 struct acpu_level *const *pvs_tbl, u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800727{
728 const struct acpu_level *l, *max_acpu_level = NULL;
729 void __iomem *qfprom_base;
730 u32 pte_efuse, pvs, tbl_idx;
731 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Unknown" };
732
733 qfprom_base = ioremap(qfprom_phys, SZ_256);
734 /* Select frequency tables. */
735 if (qfprom_base) {
736 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
737 pvs = (pte_efuse >> 10) & 0x7;
738 iounmap(qfprom_base);
739 if (pvs == 0x7)
740 pvs = (pte_efuse >> 13) & 0x7;
741
742 switch (pvs) {
743 case 0x0:
744 case 0x7:
745 tbl_idx = PVS_SLOW;
746 break;
747 case 0x1:
748 tbl_idx = PVS_NOMINAL;
749 break;
750 case 0x3:
751 tbl_idx = PVS_FAST;
752 break;
753 default:
754 tbl_idx = PVS_UNKNOWN;
755 break;
756 }
757 } else {
758 tbl_idx = PVS_UNKNOWN;
759 dev_err(drv.dev, "Unable to map QFPROM base\n");
760 }
761 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
762 if (tbl_idx == PVS_UNKNOWN) {
763 tbl_idx = PVS_SLOW;
764 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
765 pvs_names[tbl_idx]);
766 }
767 drv.acpu_freq_tbl = pvs_tbl[tbl_idx];
768
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700769 if (krait_needs_vmin())
770 krait_apply_vmin(drv.acpu_freq_tbl);
771
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800772 /* Find the max supported scaling frequency. */
773 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
774 if (l->use_for_scaling)
775 max_acpu_level = l;
776 BUG_ON(!max_acpu_level);
777 dev_info(drv.dev, "Max ACPU freq: %lu KHz\n",
778 max_acpu_level->speed.khz);
779
780 return max_acpu_level;
781}
782
783static struct acpuclk_data acpuclk_krait_data = {
784 .set_rate = acpuclk_krait_set_rate,
785 .get_rate = acpuclk_krait_get_rate,
786 .power_collapse_khz = STBY_KHZ,
787 .wait_for_irq_khz = STBY_KHZ,
788};
789
790int __init acpuclk_krait_init(struct device *dev,
791 const struct acpuclk_krait_params *params)
792{
793 const struct acpu_level *max_acpu_level;
794 int cpu;
795
796 drv.scalable = params->scalable;
797 drv.l2_freq_tbl = params->l2_freq_tbl;
798 drv.dev = dev;
799
800 drv.scalable[L2].hfpll_base =
801 ioremap(drv.scalable[L2].hfpll_phys_base, SZ_32);
802 BUG_ON(!drv.scalable[L2].hfpll_base);
803
804 max_acpu_level = select_freq_plan(params->pvs_acpu_freq_tbl,
805 params->qfprom_phys_base);
Matt Wagantallbf9eb2c2012-05-31 09:44:22 -0700806 regulator_init(dev, max_acpu_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800807 bus_init(params->bus_scale_data, max_acpu_level->l2_level->bw_level);
808 init_clock_sources(&drv.scalable[L2], &max_acpu_level->l2_level->speed);
809 for_each_online_cpu(cpu)
810 per_cpu_init(cpu, max_acpu_level);
811
812 cpufreq_table_init();
813
814 acpuclk_register(&acpuclk_krait_data);
815 register_hotcpu_notifier(&acpuclk_cpu_notifier);
816
817 return 0;
818}