blob: 78fe5ca1a7d87fa26fc953b95cd4fb43a8f0efdd [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>
Matt Wagantall9515bc22012-07-19 18:13:40 -070017#include <linux/module.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080018#include <linux/init.h>
19#include <linux/io.h>
20#include <linux/delay.h>
21#include <linux/mutex.h>
22#include <linux/err.h>
23#include <linux/errno.h>
24#include <linux/cpufreq.h>
25#include <linux/cpu.h>
26#include <linux/regulator/consumer.h>
27
28#include <asm/mach-types.h>
29#include <asm/cpu.h>
30
31#include <mach/board.h>
32#include <mach/msm_iomap.h>
33#include <mach/socinfo.h>
34#include <mach/msm-krait-l2-accessors.h>
35#include <mach/rpm-regulator.h>
Matt Wagantall75473eb2012-05-31 15:23:22 -070036#include <mach/rpm-regulator-smd.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080037#include <mach/msm_bus.h>
38
39#include "acpuclock.h"
40#include "acpuclock-krait.h"
41
42/* MUX source selects. */
43#define PRI_SRC_SEL_SEC_SRC 0
44#define PRI_SRC_SEL_HFPLL 1
45#define PRI_SRC_SEL_HFPLL_DIV2 2
46#define SEC_SRC_SEL_QSB 0
47#define SEC_SRC_SEL_L2PLL 1
48#define SEC_SRC_SEL_AUX 2
49
50/* PTE EFUSE register offset. */
51#define PTE_EFUSE 0xC0
52
53static DEFINE_MUTEX(driver_lock);
54static DEFINE_SPINLOCK(l2_lock);
55
56static struct drv_data {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -070057 struct acpu_level *acpu_freq_tbl;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080058 const struct l2_level *l2_freq_tbl;
59 struct scalable *scalable;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070060 struct hfpll_data *hfpll_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080061 u32 bus_perf_client;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070062 struct msm_bus_scale_pdata *bus_scale;
Matt Wagantall9515bc22012-07-19 18:13:40 -070063 int boost_uv;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080064 struct device *dev;
65} drv;
66
67static unsigned long acpuclk_krait_get_rate(int cpu)
68{
69 return drv.scalable[cpu].cur_speed->khz;
70}
71
72/* Select a source on the primary MUX. */
73static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
74{
75 u32 regval;
76
77 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
78 regval &= ~0x3;
79 regval |= (pri_src_sel & 0x3);
80 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
81 /* Wait for switch to complete. */
82 mb();
83 udelay(1);
84}
85
86/* Select a source on the secondary MUX. */
87static void set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
88{
89 u32 regval;
90
91 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
92 regval &= ~(0x3 << 2);
93 regval |= ((sec_src_sel & 0x3) << 2);
94 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
95 /* Wait for switch to complete. */
96 mb();
97 udelay(1);
98}
99
Matt Wagantall302d9a32012-07-03 13:37:29 -0700100static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800101{
Matt Wagantall302d9a32012-07-03 13:37:29 -0700102 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800103
Matt Wagantall75473eb2012-05-31 15:23:22 -0700104 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700105 ret = rpm_regulator_enable(vreg->rpm_reg);
106 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700107 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700108 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700109 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700110
111 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700112}
113
114static void disable_rpm_vreg(struct vreg *vreg)
115{
116 int rc;
117
118 if (vreg->rpm_reg) {
119 rc = rpm_regulator_disable(vreg->rpm_reg);
120 if (rc)
121 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
122 vreg->name, rc);
123 }
124}
125
126/* Enable an already-configured HFPLL. */
127static void hfpll_enable(struct scalable *sc, bool skip_regulators)
128{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800129 if (!skip_regulators) {
130 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700131 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
132 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800133 }
134
135 /* Disable PLL bypass mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700136 writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800137
138 /*
139 * H/W requires a 5us delay between disabling the bypass and
140 * de-asserting the reset. Delay 10us just to be safe.
141 */
142 mb();
143 udelay(10);
144
145 /* De-assert active-low PLL reset. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700146 writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800147
148 /* Wait for PLL to lock. */
149 mb();
150 udelay(60);
151
152 /* Enable PLL output. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700153 writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800154}
155
156/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
157static void hfpll_disable(struct scalable *sc, bool skip_regulators)
158{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800159 /*
160 * Disable the PLL output, disable test mode, enable the bypass mode,
161 * and assert the reset.
162 */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700163 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800164
165 if (!skip_regulators) {
166 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700167 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
168 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800169 }
170}
171
172/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
173static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
174{
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700175 void __iomem *base = sc->hfpll_base;
176 u32 regval;
177
178 writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset);
179
180 if (drv.hfpll_data->has_user_reg) {
181 regval = readl_relaxed(base + drv.hfpll_data->user_offset);
182 if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max)
183 regval &= ~drv.hfpll_data->user_vco_mask;
184 else
185 regval |= drv.hfpll_data->user_vco_mask;
186 writel_relaxed(regval, base + drv.hfpll_data->user_offset);
187 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800188}
189
190/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700191static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800192{
Matt Wagantall600ea502012-06-08 18:49:53 -0700193 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800194 int cpu;
195
196 /* Find max L2 speed vote. */
197 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800198 for_each_present_cpu(cpu)
199 new_l = max(new_l, drv.scalable[cpu].l2_vote);
200
201 return new_l;
202}
203
204/* Update the bus bandwidth request. */
205static void set_bus_bw(unsigned int bw)
206{
207 int ret;
208
209 /* Update bandwidth if request has changed. This may sleep. */
210 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
211 if (ret)
212 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
213}
214
215/* Set the CPU or L2 clock speed. */
216static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
217{
218 const struct core_speed *strt_s = sc->cur_speed;
219
220 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
221 /*
222 * Move to an always-on source running at a frequency
223 * that does not require an elevated CPU voltage.
224 */
225 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
226 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
227
228 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700229 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800230 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700231 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800232
233 /* Move to HFPLL. */
234 set_pri_clk_src(sc, tgt_s->pri_src_sel);
235 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
236 set_sec_clk_src(sc, tgt_s->sec_src_sel);
237 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700238 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800239 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
240 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700241 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800242 set_pri_clk_src(sc, tgt_s->pri_src_sel);
243 } else {
244 set_sec_clk_src(sc, tgt_s->sec_src_sel);
245 }
246
247 sc->cur_speed = tgt_s;
248}
249
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700250struct vdd_data {
251 int vdd_mem;
252 int vdd_dig;
253 int vdd_core;
254 int ua_core;
255};
256
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800257/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700258static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800259 enum setrate_reason reason)
260{
261 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700262 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800263
264 /*
265 * Increase vdd_mem active-set before vdd_dig.
266 * vdd_mem should be >= vdd_dig.
267 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700268 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700269 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700270 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800271 if (rc) {
272 dev_err(drv.dev,
273 "vdd_mem (cpu%d) increase failed (%d)\n",
274 cpu, rc);
275 return rc;
276 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700277 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800278 }
279
280 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700281 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700282 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700283 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800284 if (rc) {
285 dev_err(drv.dev,
286 "vdd_dig (cpu%d) increase failed (%d)\n",
287 cpu, rc);
288 return rc;
289 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700290 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
291 }
292
293 /* Increase current request. */
294 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
295 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
296 data->ua_core);
297 if (rc < 0) {
298 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
299 sc->vreg[VREG_CORE].name, rc);
300 return rc;
301 }
302 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800303 }
304
305 /*
306 * Update per-CPU core voltage. Don't do this for the hotplug path for
307 * which it should already be correct. Attempting to set it is bad
308 * because we don't know what CPU we are running on at this point, but
309 * the CPU regulator API requires we call it from the affected CPU.
310 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700311 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800312 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700313 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
314 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800315 if (rc) {
316 dev_err(drv.dev,
317 "vdd_core (cpu%d) increase failed (%d)\n",
318 cpu, rc);
319 return rc;
320 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700321 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800322 }
323
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700324 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800325}
326
327/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700328static void decrease_vdd(int cpu, struct vdd_data *data,
329 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800330{
331 struct scalable *sc = &drv.scalable[cpu];
332 int ret;
333
334 /*
335 * Update per-CPU core voltage. This must be called on the CPU
336 * that's being affected. Don't do this in the hotplug remove path,
337 * where the rail is off and we're executing on the other CPU.
338 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700339 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800340 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700341 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
342 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800343 if (ret) {
344 dev_err(drv.dev,
345 "vdd_core (cpu%d) decrease failed (%d)\n",
346 cpu, ret);
347 return;
348 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700349 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
350 }
351
352 /* Decrease current request. */
353 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
354 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
355 data->ua_core);
356 if (ret < 0) {
357 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
358 sc->vreg[VREG_CORE].name, ret);
359 return;
360 }
361 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800362 }
363
364 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700365 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700366 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700367 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800368 if (ret) {
369 dev_err(drv.dev,
370 "vdd_dig (cpu%d) decrease failed (%d)\n",
371 cpu, ret);
372 return;
373 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700374 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800375 }
376
377 /*
378 * Decrease vdd_mem active-set after vdd_dig.
379 * vdd_mem should be >= vdd_dig.
380 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700381 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700382 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700383 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800384 if (ret) {
385 dev_err(drv.dev,
386 "vdd_mem (cpu%d) decrease failed (%d)\n",
387 cpu, ret);
388 return;
389 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700390 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800391 }
392}
393
394static int calculate_vdd_mem(const struct acpu_level *tgt)
395{
Matt Wagantall600ea502012-06-08 18:49:53 -0700396 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800397}
398
Matt Wagantall72a38002012-07-18 13:42:55 -0700399static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800400{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700401 const int *hfpll_vdd = drv.hfpll_data->vdd;
402 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700403 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800404
Matt Wagantall72a38002012-07-18 13:42:55 -0700405 if (s->src != HFPLL)
406 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700407 else if (s->pll_l_val > nom_vdd_l_max)
408 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700409 else if (s->pll_l_val > low_vdd_l_max)
410 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800411 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700412 return hfpll_vdd[HFPLL_VDD_LOW];
413}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800414
Matt Wagantall72a38002012-07-18 13:42:55 -0700415static int calculate_vdd_dig(const struct acpu_level *tgt)
416{
417 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
418
419 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
420 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
421
422 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
423 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800424}
425
Matt Wagantall9515bc22012-07-19 18:13:40 -0700426static bool enable_boost = true;
427module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR);
428
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800429static int calculate_vdd_core(const struct acpu_level *tgt)
430{
Matt Wagantall9515bc22012-07-19 18:13:40 -0700431 return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800432}
433
434/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
435static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
436 enum setrate_reason reason)
437{
438 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800439 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700440 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700441 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800442 unsigned long flags;
443 int rc = 0;
444
Matt Wagantall5941a332012-07-10 23:20:44 -0700445 if (cpu > num_possible_cpus())
446 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800447
448 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
449 mutex_lock(&driver_lock);
450
451 strt_acpu_s = drv.scalable[cpu].cur_speed;
452
453 /* Return early if rate didn't change. */
454 if (rate == strt_acpu_s->khz)
455 goto out;
456
457 /* Find target frequency. */
458 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
459 if (tgt->speed.khz == rate) {
460 tgt_acpu_s = &tgt->speed;
461 break;
462 }
463 }
464 if (tgt->speed.khz == 0) {
465 rc = -EINVAL;
466 goto out;
467 }
468
469 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700470 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
471 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
472 vdd_data.vdd_core = calculate_vdd_core(tgt);
473 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800474
475 /* Increase VDD levels if needed. */
476 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700477 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800478 if (rc)
479 goto out;
480 }
481
482 pr_debug("Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
483 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
484
485 /* Set the new CPU speed. */
486 set_speed(&drv.scalable[cpu], tgt_acpu_s);
487
488 /*
489 * Update the L2 vote and apply the rate change. A spinlock is
490 * necessary to ensure L2 rate is calculated and set atomically
491 * with the CPU frequency, even if acpuclk_krait_set_rate() is
492 * called from an atomic context and the driver_lock mutex is not
493 * acquired.
494 */
495 spin_lock_irqsave(&l2_lock, flags);
496 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700497 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800498 spin_unlock_irqrestore(&l2_lock, flags);
499
500 /* Nothing else to do for power collapse or SWFI. */
501 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
502 goto out;
503
504 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700505 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800506
507 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700508 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800509
510 pr_debug("ACPU%d speed change complete\n", cpu);
511
512out:
513 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
514 mutex_unlock(&driver_lock);
515 return rc;
516}
517
518/* Initialize a HFPLL at a given rate and enable it. */
519static void __init hfpll_init(struct scalable *sc,
520 const struct core_speed *tgt_s)
521{
522 pr_debug("Initializing HFPLL%d\n", sc - drv.scalable);
523
524 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700525 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800526
527 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700528 writel_relaxed(drv.hfpll_data->config_val,
529 sc->hfpll_base + drv.hfpll_data->config_offset);
530 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
531 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700532 if (drv.hfpll_data->has_user_reg)
533 writel_relaxed(drv.hfpll_data->user_val,
534 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800535
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700536 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700537 if (drv.hfpll_data->has_droop_ctl)
538 writel_relaxed(drv.hfpll_data->droop_val,
539 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700540
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800541 /* Set an initial rate and enable the PLL. */
542 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700543 hfpll_enable(sc, false);
544}
545
Matt Wagantall302d9a32012-07-03 13:37:29 -0700546static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700547 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700548{
549 int ret;
550
551 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700552 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700553
554 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
555 sc->vreg[vreg].name);
556 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700557 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
558 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
559 sc->vreg[vreg].name, ret);
560 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700561 }
562
563 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
564 sc->vreg[vreg].max_vdd);
565 if (ret) {
566 dev_err(drv.dev, "%s initialization failed (%d)\n",
567 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700568 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700569 }
570 sc->vreg[vreg].cur_vdd = vdd;
571
Matt Wagantall302d9a32012-07-03 13:37:29 -0700572 if (enable) {
573 ret = enable_rpm_vreg(&sc->vreg[vreg]);
574 if (ret)
575 goto err_conf;
576 }
577
578 return 0;
579
580err_conf:
581 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
582err_get:
583 return ret;
584}
585
586static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
587 enum vregs vreg)
588{
589 if (!sc->vreg[vreg].rpm_reg)
590 return;
591
592 disable_rpm_vreg(&sc->vreg[vreg]);
593 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800594}
595
596/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700597static int __cpuinit regulator_init(struct scalable *sc,
598 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800599{
Matt Wagantall754ee272012-06-18 13:40:26 -0700600 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800601
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700602 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700603 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
604 if (ret)
605 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700606
607 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700608 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
609 if (ret)
610 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700611
Matt Wagantall302d9a32012-07-03 13:37:29 -0700612 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700613 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700614 if (ret)
615 goto err_hfpll_a;
616 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700617 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700618 if (ret)
619 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700620
Matt Wagantall754ee272012-06-18 13:40:26 -0700621 /* Setup Krait CPU regulators and initial core voltage. */
622 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
623 sc->vreg[VREG_CORE].name);
624 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700625 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
626 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
627 sc->vreg[VREG_CORE].name, ret);
628 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700629 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700630 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
631 acpu_level->ua_core);
632 if (ret < 0) {
633 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
634 sc->vreg[VREG_CORE].name, ret);
635 goto err_core_conf;
636 }
637 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700638 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700639 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
640 sc->vreg[VREG_CORE].max_vdd);
641 if (ret) {
642 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
643 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700644 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700645 }
646 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700647 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
648 if (ret) {
649 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
650 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700651 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800652 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700653
654 return 0;
655
656err_core_conf:
657 regulator_put(sc->vreg[VREG_CORE].reg);
658err_core_get:
659 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
660err_hfpll_b:
661 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
662err_hfpll_a:
663 rpm_regulator_cleanup(sc, VREG_DIG);
664err_dig:
665 rpm_regulator_cleanup(sc, VREG_MEM);
666err_mem:
667 return ret;
668}
669
670static void __cpuinit regulator_cleanup(struct scalable *sc)
671{
672 regulator_disable(sc->vreg[VREG_CORE].reg);
673 regulator_put(sc->vreg[VREG_CORE].reg);
674 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
675 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
676 rpm_regulator_cleanup(sc, VREG_DIG);
677 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800678}
679
680/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700681static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700682 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800683{
684 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700685 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800686
687 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700688 if (sc->aux_clk_sel_phys) {
689 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700690 if (!aux_reg)
691 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700692 writel_relaxed(sc->aux_clk_sel, aux_reg);
693 iounmap(aux_reg);
694 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800695
696 /* Switch away from the HFPLL while it's re-initialized. */
697 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
698 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
699 hfpll_init(sc, tgt_s);
700
701 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
702 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
703 regval &= ~(0x3 << 6);
704 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
705
706 /* Switch to the target clock source. */
707 set_sec_clk_src(sc, tgt_s->sec_src_sel);
708 set_pri_clk_src(sc, tgt_s->pri_src_sel);
709 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700710
711 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800712}
713
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700714static void __cpuinit fill_cur_core_speed(struct core_speed *s,
715 struct scalable *sc)
716{
717 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
718 s->sec_src_sel = (get_l2_indirect_reg(sc->l2cpmr_iaddr) >> 2) & 0x3;
719 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
720}
721
722static bool __cpuinit speed_equal(const struct core_speed *s1,
723 const struct core_speed *s2)
724{
725 return (s1->pri_src_sel == s2->pri_src_sel &&
726 s1->sec_src_sel == s2->sec_src_sel &&
727 s1->pll_l_val == s2->pll_l_val);
728}
729
730static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
731{
732 struct scalable *sc = &drv.scalable[cpu];
733 const struct acpu_level *l;
734 struct core_speed cur_speed;
735
736 fill_cur_core_speed(&cur_speed, sc);
737 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
738 if (speed_equal(&l->speed, &cur_speed))
739 return l;
740 return NULL;
741}
742
743static const struct l2_level __init *find_cur_l2_level(void)
744{
745 struct scalable *sc = &drv.scalable[L2];
746 const struct l2_level *l;
747 struct core_speed cur_speed;
748
749 fill_cur_core_speed(&cur_speed, sc);
750 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
751 if (speed_equal(&l->speed, &cur_speed))
752 return l;
753 return NULL;
754}
755
756static const struct acpu_level __cpuinit *find_min_acpu_level(void)
757{
758 struct acpu_level *l;
759
760 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
761 if (l->use_for_scaling)
762 return l;
763
764 return NULL;
765}
766
Matt Wagantall302d9a32012-07-03 13:37:29 -0700767static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800768{
Matt Wagantall754ee272012-06-18 13:40:26 -0700769 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700770 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700771 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800772
Matt Wagantall754ee272012-06-18 13:40:26 -0700773 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700774 if (!sc->hfpll_base) {
775 ret = -ENOMEM;
776 goto err_ioremap;
777 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700778
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700779 acpu_level = find_cur_acpu_level(cpu);
780 if (!acpu_level || acpu_level->speed.src == QSB) {
781 acpu_level = find_min_acpu_level();
782 if (!acpu_level) {
783 ret = -ENODEV;
784 goto err_table;
785 }
786 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
787 cpu, acpu_level->speed.khz);
788 } else {
789 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
790 acpu_level->speed.khz);
791 }
792
793 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700794 if (ret)
795 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700796
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700797 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700798 if (ret)
799 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700800
801 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700802 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700803
804 return 0;
805
806err_clocks:
807 regulator_cleanup(sc);
808err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700809err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700810 iounmap(sc->hfpll_base);
811err_ioremap:
812 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800813}
814
815/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700816static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800817{
818 int ret;
819
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700820 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800821 if (!drv.bus_perf_client) {
822 dev_err(drv.dev, "unable to register bus client\n");
823 BUG();
824 }
825
Matt Wagantall754ee272012-06-18 13:40:26 -0700826 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700827 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800828 if (ret)
829 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
830}
831
832#ifdef CONFIG_CPU_FREQ_MSM
833static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
834
835static void __init cpufreq_table_init(void)
836{
837 int cpu;
838
839 for_each_possible_cpu(cpu) {
840 int i, freq_cnt = 0;
841 /* Construct the freq_table tables from acpu_freq_tbl. */
842 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
843 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
844 if (drv.acpu_freq_tbl[i].use_for_scaling) {
845 freq_table[cpu][freq_cnt].index = freq_cnt;
846 freq_table[cpu][freq_cnt].frequency
847 = drv.acpu_freq_tbl[i].speed.khz;
848 freq_cnt++;
849 }
850 }
851 /* freq_table not big enough to store all usable freqs. */
852 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
853
854 freq_table[cpu][freq_cnt].index = freq_cnt;
855 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
856
857 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
858 cpu, freq_cnt);
859
860 /* Register table with CPUFreq. */
861 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
862 }
863}
864#else
865static void __init cpufreq_table_init(void) {}
866#endif
867
868#define HOT_UNPLUG_KHZ STBY_KHZ
869static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
870 unsigned long action, void *hcpu)
871{
872 static int prev_khz[NR_CPUS];
873 int rc, cpu = (int)hcpu;
874 struct scalable *sc = &drv.scalable[cpu];
875
876 switch (action & ~CPU_TASKS_FROZEN) {
877 case CPU_DEAD:
878 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
879 /* Fall through. */
880 case CPU_UP_CANCELED:
881 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
882 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
883 break;
884 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700885 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700886 rc = per_cpu_init(cpu);
887 if (rc)
888 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700889 break;
890 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800891 if (WARN_ON(!prev_khz[cpu]))
892 return NOTIFY_BAD;
893 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700894 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800895 if (rc < 0)
896 return NOTIFY_BAD;
897 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
898 break;
899 default:
900 break;
901 }
902
903 return NOTIFY_OK;
904}
905
906static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
907 .notifier_call = acpuclk_cpu_callback,
908};
909
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700910static const int krait_needs_vmin(void)
911{
912 switch (read_cpuid_id()) {
913 case 0x511F04D0: /* KR28M2A20 */
914 case 0x511F04D1: /* KR28M2A21 */
915 case 0x510F06F0: /* KR28M4A10 */
916 return 1;
917 default:
918 return 0;
919 };
920}
921
922static void krait_apply_vmin(struct acpu_level *tbl)
923{
924 for (; tbl->speed.khz != 0; tbl++)
925 if (tbl->vdd_core < 1150000)
926 tbl->vdd_core = 1150000;
927}
928
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700929static int __init select_freq_plan(u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800930{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800931 void __iomem *qfprom_base;
932 u32 pte_efuse, pvs, tbl_idx;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700933 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Faster", "Unknown" };
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800934
935 qfprom_base = ioremap(qfprom_phys, SZ_256);
936 /* Select frequency tables. */
937 if (qfprom_base) {
938 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
939 pvs = (pte_efuse >> 10) & 0x7;
940 iounmap(qfprom_base);
941 if (pvs == 0x7)
942 pvs = (pte_efuse >> 13) & 0x7;
943
944 switch (pvs) {
945 case 0x0:
946 case 0x7:
947 tbl_idx = PVS_SLOW;
948 break;
949 case 0x1:
950 tbl_idx = PVS_NOMINAL;
951 break;
952 case 0x3:
953 tbl_idx = PVS_FAST;
954 break;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700955 case 0x4:
956 tbl_idx = PVS_FASTER;
957 break;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800958 default:
959 tbl_idx = PVS_UNKNOWN;
960 break;
961 }
962 } else {
963 tbl_idx = PVS_UNKNOWN;
964 dev_err(drv.dev, "Unable to map QFPROM base\n");
965 }
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700966 if (tbl_idx == PVS_UNKNOWN) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800967 tbl_idx = PVS_SLOW;
968 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
969 pvs_names[tbl_idx]);
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700970 } else {
971 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800972 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800973
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700974 return tbl_idx;
975}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700976
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800977static struct acpuclk_data acpuclk_krait_data = {
978 .set_rate = acpuclk_krait_set_rate,
979 .get_rate = acpuclk_krait_get_rate,
980 .power_collapse_khz = STBY_KHZ,
981 .wait_for_irq_khz = STBY_KHZ,
982};
983
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700984static void __init drv_data_init(struct device *dev,
985 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800986{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700987 int tbl_idx;
988
989 drv.dev = dev;
990 drv.scalable = kmemdup(params->scalable, params->scalable_size,
991 GFP_KERNEL);
992 BUG_ON(!drv.scalable);
993
994 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
995 GFP_KERNEL);
996 BUG_ON(!drv.hfpll_data);
997
998 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
999 GFP_KERNEL);
1000 BUG_ON(!drv.l2_freq_tbl);
1001
1002 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
1003 GFP_KERNEL);
1004 BUG_ON(!drv.bus_scale);
1005 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1006 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1007 GFP_KERNEL);
1008 BUG_ON(!drv.bus_scale->usecase);
1009
1010 tbl_idx = select_freq_plan(params->qfprom_phys_base);
1011 drv.acpu_freq_tbl = kmemdup(params->pvs_tables[tbl_idx].table,
1012 params->pvs_tables[tbl_idx].size,
1013 GFP_KERNEL);
1014 BUG_ON(!drv.acpu_freq_tbl);
Matt Wagantall9515bc22012-07-19 18:13:40 -07001015 drv.boost_uv = params->pvs_tables[tbl_idx].boost_uv;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001016}
1017
1018static void __init hw_init(void)
1019{
1020 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001021 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001022 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001023
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001024 if (krait_needs_vmin())
1025 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001026
Matt Wagantall754ee272012-06-18 13:40:26 -07001027 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1028 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001029
Matt Wagantall302d9a32012-07-03 13:37:29 -07001030 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1031 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1032 BUG_ON(rc);
1033 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1034 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1035 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001036
1037 l2_level = find_cur_l2_level();
1038 if (!l2_level || l2_level->speed.src == QSB) {
1039 l2_level = drv.l2_freq_tbl;
1040 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to QSB.\n");
1041 } else {
1042 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1043 l2_level->speed.khz);
1044 }
1045
1046 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001047 BUG_ON(rc);
1048
1049 for_each_online_cpu(cpu) {
1050 rc = per_cpu_init(cpu);
1051 BUG_ON(rc);
1052 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001053
1054 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001055}
1056
1057int __init acpuclk_krait_init(struct device *dev,
1058 const struct acpuclk_krait_params *params)
1059{
1060 drv_data_init(dev, params);
1061 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001062
1063 cpufreq_table_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001064 acpuclk_register(&acpuclk_krait_data);
1065 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1066
1067 return 0;
1068}