blob: 14920060e13efaa4632bb89ce2fa09c86b4b9a95 [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. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700177static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800178{
Matt Wagantall600ea502012-06-08 18:49:53 -0700179 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800180 int cpu;
181
182 /* Find max L2 speed vote. */
183 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800184 for_each_present_cpu(cpu)
185 new_l = max(new_l, drv.scalable[cpu].l2_vote);
186
187 return new_l;
188}
189
190/* Update the bus bandwidth request. */
191static void set_bus_bw(unsigned int bw)
192{
193 int ret;
194
195 /* Update bandwidth if request has changed. This may sleep. */
196 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
197 if (ret)
198 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
199}
200
201/* Set the CPU or L2 clock speed. */
202static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
203{
204 const struct core_speed *strt_s = sc->cur_speed;
205
206 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
207 /*
208 * Move to an always-on source running at a frequency
209 * that does not require an elevated CPU voltage.
210 */
211 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
212 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
213
214 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700215 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800216 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700217 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800218
219 /* Move to HFPLL. */
220 set_pri_clk_src(sc, tgt_s->pri_src_sel);
221 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
222 set_sec_clk_src(sc, tgt_s->sec_src_sel);
223 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700224 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800225 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
226 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700227 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800228 set_pri_clk_src(sc, tgt_s->pri_src_sel);
229 } else {
230 set_sec_clk_src(sc, tgt_s->sec_src_sel);
231 }
232
233 sc->cur_speed = tgt_s;
234}
235
236/* Apply any per-cpu voltage increases. */
237static int increase_vdd(int cpu, int vdd_core, int vdd_mem, int vdd_dig,
238 enum setrate_reason reason)
239{
240 struct scalable *sc = &drv.scalable[cpu];
241 int rc = 0;
242
243 /*
244 * Increase vdd_mem active-set before vdd_dig.
245 * vdd_mem should be >= vdd_dig.
246 */
247 if (vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700248 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
249 vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800250 if (rc) {
251 dev_err(drv.dev,
252 "vdd_mem (cpu%d) increase failed (%d)\n",
253 cpu, rc);
254 return rc;
255 }
256 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
257 }
258
259 /* Increase vdd_dig active-set vote. */
260 if (vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700261 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
262 vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800263 if (rc) {
264 dev_err(drv.dev,
265 "vdd_dig (cpu%d) increase failed (%d)\n",
266 cpu, rc);
267 return rc;
268 }
269 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
270 }
271
272 /*
273 * Update per-CPU core voltage. Don't do this for the hotplug path for
274 * which it should already be correct. Attempting to set it is bad
275 * because we don't know what CPU we are running on at this point, but
276 * the CPU regulator API requires we call it from the affected CPU.
277 */
278 if (vdd_core > sc->vreg[VREG_CORE].cur_vdd
279 && reason != SETRATE_HOTPLUG) {
280 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
281 sc->vreg[VREG_CORE].max_vdd);
282 if (rc) {
283 dev_err(drv.dev,
284 "vdd_core (cpu%d) increase failed (%d)\n",
285 cpu, rc);
286 return rc;
287 }
288 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
289 }
290
291 return rc;
292}
293
294/* Apply any per-cpu voltage decreases. */
295static void decrease_vdd(int cpu, int vdd_core, int vdd_mem, int vdd_dig,
296 enum setrate_reason reason)
297{
298 struct scalable *sc = &drv.scalable[cpu];
299 int ret;
300
301 /*
302 * Update per-CPU core voltage. This must be called on the CPU
303 * that's being affected. Don't do this in the hotplug remove path,
304 * where the rail is off and we're executing on the other CPU.
305 */
306 if (vdd_core < sc->vreg[VREG_CORE].cur_vdd
307 && reason != SETRATE_HOTPLUG) {
308 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
309 sc->vreg[VREG_CORE].max_vdd);
310 if (ret) {
311 dev_err(drv.dev,
312 "vdd_core (cpu%d) decrease failed (%d)\n",
313 cpu, ret);
314 return;
315 }
316 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
317 }
318
319 /* Decrease vdd_dig active-set vote. */
320 if (vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700321 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
322 vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800323 if (ret) {
324 dev_err(drv.dev,
325 "vdd_dig (cpu%d) decrease failed (%d)\n",
326 cpu, ret);
327 return;
328 }
329 sc->vreg[VREG_DIG].cur_vdd = vdd_dig;
330 }
331
332 /*
333 * Decrease vdd_mem active-set after vdd_dig.
334 * vdd_mem should be >= vdd_dig.
335 */
336 if (vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700337 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
338 vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800339 if (ret) {
340 dev_err(drv.dev,
341 "vdd_mem (cpu%d) decrease failed (%d)\n",
342 cpu, ret);
343 return;
344 }
345 sc->vreg[VREG_MEM].cur_vdd = vdd_mem;
346 }
347}
348
349static int calculate_vdd_mem(const struct acpu_level *tgt)
350{
Matt Wagantall600ea502012-06-08 18:49:53 -0700351 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800352}
353
354static int calculate_vdd_dig(const struct acpu_level *tgt)
355{
356 int pll_vdd_dig;
357 const int *hfpll_vdd = drv.scalable[L2].hfpll_data->vdd;
358 const u32 low_vdd_l_max = drv.scalable[L2].hfpll_data->low_vdd_l_max;
359
Matt Wagantall600ea502012-06-08 18:49:53 -0700360 if (drv.l2_freq_tbl[tgt->l2_level].speed.src != HFPLL)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800361 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall600ea502012-06-08 18:49:53 -0700362 else if (drv.l2_freq_tbl[tgt->l2_level].speed.pll_l_val > low_vdd_l_max)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800363 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_NOM];
364 else
365 pll_vdd_dig = hfpll_vdd[HFPLL_VDD_LOW];
366
Matt Wagantall600ea502012-06-08 18:49:53 -0700367 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig, pll_vdd_dig);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800368}
369
370static int calculate_vdd_core(const struct acpu_level *tgt)
371{
372 return tgt->vdd_core;
373}
374
375/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
376static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
377 enum setrate_reason reason)
378{
379 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800380 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700381 int tgt_l2_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800382 int vdd_mem, vdd_dig, vdd_core;
383 unsigned long flags;
384 int rc = 0;
385
Matt Wagantall5941a332012-07-10 23:20:44 -0700386 if (cpu > num_possible_cpus())
387 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800388
389 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
390 mutex_lock(&driver_lock);
391
392 strt_acpu_s = drv.scalable[cpu].cur_speed;
393
394 /* Return early if rate didn't change. */
395 if (rate == strt_acpu_s->khz)
396 goto out;
397
398 /* Find target frequency. */
399 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
400 if (tgt->speed.khz == rate) {
401 tgt_acpu_s = &tgt->speed;
402 break;
403 }
404 }
405 if (tgt->speed.khz == 0) {
406 rc = -EINVAL;
407 goto out;
408 }
409
410 /* Calculate voltage requirements for the current CPU. */
411 vdd_mem = calculate_vdd_mem(tgt);
412 vdd_dig = calculate_vdd_dig(tgt);
413 vdd_core = calculate_vdd_core(tgt);
414
415 /* Increase VDD levels if needed. */
416 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
417 rc = increase_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
418 if (rc)
419 goto out;
420 }
421
422 pr_debug("Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
423 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
424
425 /* Set the new CPU speed. */
426 set_speed(&drv.scalable[cpu], tgt_acpu_s);
427
428 /*
429 * Update the L2 vote and apply the rate change. A spinlock is
430 * necessary to ensure L2 rate is calculated and set atomically
431 * with the CPU frequency, even if acpuclk_krait_set_rate() is
432 * called from an atomic context and the driver_lock mutex is not
433 * acquired.
434 */
435 spin_lock_irqsave(&l2_lock, flags);
436 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700437 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800438 spin_unlock_irqrestore(&l2_lock, flags);
439
440 /* Nothing else to do for power collapse or SWFI. */
441 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
442 goto out;
443
444 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700445 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800446
447 /* Drop VDD levels if we can. */
448 decrease_vdd(cpu, vdd_core, vdd_mem, vdd_dig, reason);
449
450 pr_debug("ACPU%d speed change complete\n", cpu);
451
452out:
453 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
454 mutex_unlock(&driver_lock);
455 return rc;
456}
457
458/* Initialize a HFPLL at a given rate and enable it. */
459static void __init hfpll_init(struct scalable *sc,
460 const struct core_speed *tgt_s)
461{
462 pr_debug("Initializing HFPLL%d\n", sc - drv.scalable);
463
464 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700465 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800466
467 /* Configure PLL parameters for integer mode. */
468 writel_relaxed(sc->hfpll_data->config_val,
469 sc->hfpll_base + sc->hfpll_data->config_offset);
470 writel_relaxed(0, sc->hfpll_base + sc->hfpll_data->m_offset);
471 writel_relaxed(1, sc->hfpll_base + sc->hfpll_data->n_offset);
472
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700473 /* Program droop controller, if supported */
474 if (sc->hfpll_data->has_droop_ctl)
475 writel_relaxed(sc->hfpll_data->droop_val,
476 sc->hfpll_base + sc->hfpll_data->droop_offset);
477
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800478 /* Set an initial rate and enable the PLL. */
479 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700480 hfpll_enable(sc, false);
481}
482
Matt Wagantall302d9a32012-07-03 13:37:29 -0700483static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700484 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700485{
486 int ret;
487
488 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700489 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700490
491 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
492 sc->vreg[vreg].name);
493 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700494 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
495 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
496 sc->vreg[vreg].name, ret);
497 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700498 }
499
500 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
501 sc->vreg[vreg].max_vdd);
502 if (ret) {
503 dev_err(drv.dev, "%s initialization failed (%d)\n",
504 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700505 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700506 }
507 sc->vreg[vreg].cur_vdd = vdd;
508
Matt Wagantall302d9a32012-07-03 13:37:29 -0700509 if (enable) {
510 ret = enable_rpm_vreg(&sc->vreg[vreg]);
511 if (ret)
512 goto err_conf;
513 }
514
515 return 0;
516
517err_conf:
518 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
519err_get:
520 return ret;
521}
522
523static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
524 enum vregs vreg)
525{
526 if (!sc->vreg[vreg].rpm_reg)
527 return;
528
529 disable_rpm_vreg(&sc->vreg[vreg]);
530 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800531}
532
533/* Voltage regulator initialization. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700534static int __cpuinit regulator_init(struct scalable *sc)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800535{
Matt Wagantall754ee272012-06-18 13:40:26 -0700536 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800537
Matt Wagantall754ee272012-06-18 13:40:26 -0700538 vdd_mem = calculate_vdd_mem(drv.max_acpu_lvl);
539 vdd_dig = calculate_vdd_dig(drv.max_acpu_lvl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800540
Matt Wagantall302d9a32012-07-03 13:37:29 -0700541 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
542 if (ret)
543 goto err_mem;
544 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
545 if (ret)
546 goto err_dig;
547 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700548 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700549 if (ret)
550 goto err_hfpll_a;
551 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700552 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700553 if (ret)
554 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700555
Matt Wagantall754ee272012-06-18 13:40:26 -0700556 /* Setup Krait CPU regulators and initial core voltage. */
557 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
558 sc->vreg[VREG_CORE].name);
559 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700560 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
561 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
562 sc->vreg[VREG_CORE].name, ret);
563 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700564 }
565 vdd_core = calculate_vdd_core(drv.max_acpu_lvl);
566 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
567 sc->vreg[VREG_CORE].max_vdd);
568 if (ret) {
569 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
570 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700571 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700572 }
573 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
574 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
575 sc->vreg[VREG_CORE].peak_ua);
576 if (ret < 0) {
577 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
578 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700579 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700580 }
581 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
582 if (ret) {
583 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
584 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700585 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800586 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700587
588 return 0;
589
590err_core_conf:
591 regulator_put(sc->vreg[VREG_CORE].reg);
592err_core_get:
593 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
594err_hfpll_b:
595 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
596err_hfpll_a:
597 rpm_regulator_cleanup(sc, VREG_DIG);
598err_dig:
599 rpm_regulator_cleanup(sc, VREG_MEM);
600err_mem:
601 return ret;
602}
603
604static void __cpuinit regulator_cleanup(struct scalable *sc)
605{
606 regulator_disable(sc->vreg[VREG_CORE].reg);
607 regulator_put(sc->vreg[VREG_CORE].reg);
608 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
609 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
610 rpm_regulator_cleanup(sc, VREG_DIG);
611 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800612}
613
614/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700615static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700616 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800617{
618 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700619 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800620
621 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700622 if (sc->aux_clk_sel_phys) {
623 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700624 if (!aux_reg)
625 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700626 writel_relaxed(sc->aux_clk_sel, aux_reg);
627 iounmap(aux_reg);
628 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800629
630 /* Switch away from the HFPLL while it's re-initialized. */
631 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
632 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
633 hfpll_init(sc, tgt_s);
634
635 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
636 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
637 regval &= ~(0x3 << 6);
638 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
639
640 /* Switch to the target clock source. */
641 set_sec_clk_src(sc, tgt_s->sec_src_sel);
642 set_pri_clk_src(sc, tgt_s->pri_src_sel);
643 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700644
645 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800646}
647
Matt Wagantall302d9a32012-07-03 13:37:29 -0700648static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800649{
Matt Wagantall754ee272012-06-18 13:40:26 -0700650 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall302d9a32012-07-03 13:37:29 -0700651 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800652
Matt Wagantall754ee272012-06-18 13:40:26 -0700653 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700654 if (!sc->hfpll_base) {
655 ret = -ENOMEM;
656 goto err_ioremap;
657 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700658
Matt Wagantall302d9a32012-07-03 13:37:29 -0700659 ret = regulator_init(sc);
660 if (ret)
661 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700662
Matt Wagantall302d9a32012-07-03 13:37:29 -0700663 ret = init_clock_sources(sc, &drv.max_acpu_lvl->speed);
664 if (ret)
665 goto err_clocks;
Matt Wagantall754ee272012-06-18 13:40:26 -0700666 sc->l2_vote = drv.max_acpu_lvl->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700667 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700668
669 return 0;
670
671err_clocks:
672 regulator_cleanup(sc);
673err_regulators:
674 iounmap(sc->hfpll_base);
675err_ioremap:
676 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800677}
678
679/* Register with bus driver. */
Matt Wagantall754ee272012-06-18 13:40:26 -0700680static void __init bus_init(struct msm_bus_scale_pdata *bus_scale_data)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800681{
682 int ret;
683
684 drv.bus_perf_client = msm_bus_scale_register_client(bus_scale_data);
685 if (!drv.bus_perf_client) {
686 dev_err(drv.dev, "unable to register bus client\n");
687 BUG();
688 }
689
Matt Wagantall754ee272012-06-18 13:40:26 -0700690 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall600ea502012-06-08 18:49:53 -0700691 drv.l2_freq_tbl[drv.max_acpu_lvl->l2_level].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800692 if (ret)
693 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
694}
695
696#ifdef CONFIG_CPU_FREQ_MSM
697static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
698
699static void __init cpufreq_table_init(void)
700{
701 int cpu;
702
703 for_each_possible_cpu(cpu) {
704 int i, freq_cnt = 0;
705 /* Construct the freq_table tables from acpu_freq_tbl. */
706 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
707 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
708 if (drv.acpu_freq_tbl[i].use_for_scaling) {
709 freq_table[cpu][freq_cnt].index = freq_cnt;
710 freq_table[cpu][freq_cnt].frequency
711 = drv.acpu_freq_tbl[i].speed.khz;
712 freq_cnt++;
713 }
714 }
715 /* freq_table not big enough to store all usable freqs. */
716 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
717
718 freq_table[cpu][freq_cnt].index = freq_cnt;
719 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
720
721 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
722 cpu, freq_cnt);
723
724 /* Register table with CPUFreq. */
725 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
726 }
727}
728#else
729static void __init cpufreq_table_init(void) {}
730#endif
731
732#define HOT_UNPLUG_KHZ STBY_KHZ
733static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
734 unsigned long action, void *hcpu)
735{
736 static int prev_khz[NR_CPUS];
737 int rc, cpu = (int)hcpu;
738 struct scalable *sc = &drv.scalable[cpu];
739
740 switch (action & ~CPU_TASKS_FROZEN) {
741 case CPU_DEAD:
742 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
743 /* Fall through. */
744 case CPU_UP_CANCELED:
745 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
746 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
747 break;
748 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700749 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700750 rc = per_cpu_init(cpu);
751 if (rc)
752 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700753 break;
754 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800755 if (WARN_ON(!prev_khz[cpu]))
756 return NOTIFY_BAD;
757 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
758 sc->vreg[VREG_CORE].peak_ua);
759 if (rc < 0)
760 return NOTIFY_BAD;
761 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
762 break;
763 default:
764 break;
765 }
766
767 return NOTIFY_OK;
768}
769
770static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
771 .notifier_call = acpuclk_cpu_callback,
772};
773
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700774static const int krait_needs_vmin(void)
775{
776 switch (read_cpuid_id()) {
777 case 0x511F04D0: /* KR28M2A20 */
778 case 0x511F04D1: /* KR28M2A21 */
779 case 0x510F06F0: /* KR28M4A10 */
780 return 1;
781 default:
782 return 0;
783 };
784}
785
786static void krait_apply_vmin(struct acpu_level *tbl)
787{
788 for (; tbl->speed.khz != 0; tbl++)
789 if (tbl->vdd_core < 1150000)
790 tbl->vdd_core = 1150000;
791}
792
Matt Wagantall754ee272012-06-18 13:40:26 -0700793static void __init select_freq_plan(struct acpu_level *const *pvs_tbl,
794 u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800795{
Matt Wagantall754ee272012-06-18 13:40:26 -0700796 const struct acpu_level *l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800797 void __iomem *qfprom_base;
798 u32 pte_efuse, pvs, tbl_idx;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700799 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Faster", "Unknown" };
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800800
801 qfprom_base = ioremap(qfprom_phys, SZ_256);
802 /* Select frequency tables. */
803 if (qfprom_base) {
804 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
805 pvs = (pte_efuse >> 10) & 0x7;
806 iounmap(qfprom_base);
807 if (pvs == 0x7)
808 pvs = (pte_efuse >> 13) & 0x7;
809
810 switch (pvs) {
811 case 0x0:
812 case 0x7:
813 tbl_idx = PVS_SLOW;
814 break;
815 case 0x1:
816 tbl_idx = PVS_NOMINAL;
817 break;
818 case 0x3:
819 tbl_idx = PVS_FAST;
820 break;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700821 case 0x4:
822 tbl_idx = PVS_FASTER;
823 break;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800824 default:
825 tbl_idx = PVS_UNKNOWN;
826 break;
827 }
828 } else {
829 tbl_idx = PVS_UNKNOWN;
830 dev_err(drv.dev, "Unable to map QFPROM base\n");
831 }
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700832 if (tbl_idx == PVS_UNKNOWN || !pvs_tbl[tbl_idx]) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800833 tbl_idx = PVS_SLOW;
834 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
835 pvs_names[tbl_idx]);
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700836 } else {
837 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800838 }
839 drv.acpu_freq_tbl = pvs_tbl[tbl_idx];
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700840 BUG_ON(!drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800841
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700842 if (krait_needs_vmin())
843 krait_apply_vmin(drv.acpu_freq_tbl);
844
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800845 /* Find the max supported scaling frequency. */
846 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
847 if (l->use_for_scaling)
Matt Wagantall754ee272012-06-18 13:40:26 -0700848 drv.max_acpu_lvl = l;
849 BUG_ON(!drv.max_acpu_lvl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800850 dev_info(drv.dev, "Max ACPU freq: %lu KHz\n",
Matt Wagantall754ee272012-06-18 13:40:26 -0700851 drv.max_acpu_lvl->speed.khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800852}
853
854static struct acpuclk_data acpuclk_krait_data = {
855 .set_rate = acpuclk_krait_set_rate,
856 .get_rate = acpuclk_krait_get_rate,
857 .power_collapse_khz = STBY_KHZ,
858 .wait_for_irq_khz = STBY_KHZ,
859};
860
861int __init acpuclk_krait_init(struct device *dev,
862 const struct acpuclk_krait_params *params)
863{
Matt Wagantall754ee272012-06-18 13:40:26 -0700864 struct scalable *l2;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700865 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800866
867 drv.scalable = params->scalable;
868 drv.l2_freq_tbl = params->l2_freq_tbl;
869 drv.dev = dev;
870
Matt Wagantall754ee272012-06-18 13:40:26 -0700871 select_freq_plan(params->pvs_acpu_freq_tbl, params->qfprom_phys_base);
872 bus_init(params->bus_scale_data);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800873
Matt Wagantall754ee272012-06-18 13:40:26 -0700874 l2 = &drv.scalable[L2];
875 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
876 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -0700877
Matt Wagantall302d9a32012-07-03 13:37:29 -0700878 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
879 l2->vreg[VREG_HFPLL_A].max_vdd, false);
880 BUG_ON(rc);
881 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
882 l2->vreg[VREG_HFPLL_B].max_vdd, false);
883 BUG_ON(rc);
Matt Wagantall600ea502012-06-08 18:49:53 -0700884 rc = init_clock_sources(l2,
885 &drv.l2_freq_tbl[drv.max_acpu_lvl->l2_level].speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700886 BUG_ON(rc);
887
888 for_each_online_cpu(cpu) {
889 rc = per_cpu_init(cpu);
890 BUG_ON(rc);
891 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800892
893 cpufreq_table_init();
894
895 acpuclk_register(&acpuclk_krait_data);
896 register_hotcpu_notifier(&acpuclk_cpu_notifier);
897
898 return 0;
899}