blob: 79922e6fc24fde4e58763956896bc88d5a62f757 [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;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070059 struct hfpll_data *hfpll_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080060 u32 bus_perf_client;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070061 struct msm_bus_scale_pdata *bus_scale;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080062 struct device *dev;
63} drv;
64
65static unsigned long acpuclk_krait_get_rate(int cpu)
66{
67 return drv.scalable[cpu].cur_speed->khz;
68}
69
70/* Select a source on the primary MUX. */
71static void set_pri_clk_src(struct scalable *sc, u32 pri_src_sel)
72{
73 u32 regval;
74
75 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
76 regval &= ~0x3;
77 regval |= (pri_src_sel & 0x3);
78 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
79 /* Wait for switch to complete. */
80 mb();
81 udelay(1);
82}
83
84/* Select a source on the secondary MUX. */
85static void set_sec_clk_src(struct scalable *sc, u32 sec_src_sel)
86{
87 u32 regval;
88
89 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
90 regval &= ~(0x3 << 2);
91 regval |= ((sec_src_sel & 0x3) << 2);
92 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
93 /* Wait for switch to complete. */
94 mb();
95 udelay(1);
96}
97
Matt Wagantall302d9a32012-07-03 13:37:29 -070098static int enable_rpm_vreg(struct vreg *vreg)
Matt Wagantalle9b715a2012-01-04 18:16:14 -080099{
Matt Wagantall302d9a32012-07-03 13:37:29 -0700100 int ret = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800101
Matt Wagantall75473eb2012-05-31 15:23:22 -0700102 if (vreg->rpm_reg) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700103 ret = rpm_regulator_enable(vreg->rpm_reg);
104 if (ret)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700105 dev_err(drv.dev, "%s regulator enable failed (%d)\n",
Matt Wagantall302d9a32012-07-03 13:37:29 -0700106 vreg->name, ret);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700107 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700108
109 return ret;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700110}
111
112static void disable_rpm_vreg(struct vreg *vreg)
113{
114 int rc;
115
116 if (vreg->rpm_reg) {
117 rc = rpm_regulator_disable(vreg->rpm_reg);
118 if (rc)
119 dev_err(drv.dev, "%s regulator disable failed (%d)\n",
120 vreg->name, rc);
121 }
122}
123
124/* Enable an already-configured HFPLL. */
125static void hfpll_enable(struct scalable *sc, bool skip_regulators)
126{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800127 if (!skip_regulators) {
128 /* Enable regulators required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700129 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
130 enable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800131 }
132
133 /* Disable PLL bypass mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700134 writel_relaxed(0x2, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800135
136 /*
137 * H/W requires a 5us delay between disabling the bypass and
138 * de-asserting the reset. Delay 10us just to be safe.
139 */
140 mb();
141 udelay(10);
142
143 /* De-assert active-low PLL reset. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700144 writel_relaxed(0x6, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800145
146 /* Wait for PLL to lock. */
147 mb();
148 udelay(60);
149
150 /* Enable PLL output. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700151 writel_relaxed(0x7, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800152}
153
154/* Disable a HFPLL for power-savings or while it's being reprogrammed. */
155static void hfpll_disable(struct scalable *sc, bool skip_regulators)
156{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800157 /*
158 * Disable the PLL output, disable test mode, enable the bypass mode,
159 * and assert the reset.
160 */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700161 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->mode_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800162
163 if (!skip_regulators) {
164 /* Remove voltage votes required by the HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700165 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_B]);
166 disable_rpm_vreg(&sc->vreg[VREG_HFPLL_A]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800167 }
168}
169
170/* Program the HFPLL rate. Assumes HFPLL is already disabled. */
171static void hfpll_set_rate(struct scalable *sc, const struct core_speed *tgt_s)
172{
173 writel_relaxed(tgt_s->pll_l_val,
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700174 sc->hfpll_base + drv.hfpll_data->l_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800175}
176
177/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700178static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800179{
Matt Wagantall600ea502012-06-08 18:49:53 -0700180 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800181 int cpu;
182
183 /* Find max L2 speed vote. */
184 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800185 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
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700237struct vdd_data {
238 int vdd_mem;
239 int vdd_dig;
240 int vdd_core;
241 int ua_core;
242};
243
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800244/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700245static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800246 enum setrate_reason reason)
247{
248 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700249 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800250
251 /*
252 * Increase vdd_mem active-set before vdd_dig.
253 * vdd_mem should be >= vdd_dig.
254 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700255 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700256 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700257 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800258 if (rc) {
259 dev_err(drv.dev,
260 "vdd_mem (cpu%d) increase failed (%d)\n",
261 cpu, rc);
262 return rc;
263 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700264 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800265 }
266
267 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700268 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700269 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700270 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800271 if (rc) {
272 dev_err(drv.dev,
273 "vdd_dig (cpu%d) increase failed (%d)\n",
274 cpu, rc);
275 return rc;
276 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700277 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
278 }
279
280 /* Increase current request. */
281 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
282 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
283 data->ua_core);
284 if (rc < 0) {
285 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
286 sc->vreg[VREG_CORE].name, rc);
287 return rc;
288 }
289 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800290 }
291
292 /*
293 * Update per-CPU core voltage. Don't do this for the hotplug path for
294 * which it should already be correct. Attempting to set it is bad
295 * because we don't know what CPU we are running on at this point, but
296 * the CPU regulator API requires we call it from the affected CPU.
297 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700298 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800299 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700300 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
301 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800302 if (rc) {
303 dev_err(drv.dev,
304 "vdd_core (cpu%d) increase failed (%d)\n",
305 cpu, rc);
306 return rc;
307 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700308 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800309 }
310
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700311 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800312}
313
314/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700315static void decrease_vdd(int cpu, struct vdd_data *data,
316 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800317{
318 struct scalable *sc = &drv.scalable[cpu];
319 int ret;
320
321 /*
322 * Update per-CPU core voltage. This must be called on the CPU
323 * that's being affected. Don't do this in the hotplug remove path,
324 * where the rail is off and we're executing on the other CPU.
325 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700326 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800327 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700328 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
329 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800330 if (ret) {
331 dev_err(drv.dev,
332 "vdd_core (cpu%d) decrease failed (%d)\n",
333 cpu, ret);
334 return;
335 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700336 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
337 }
338
339 /* Decrease current request. */
340 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
341 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
342 data->ua_core);
343 if (ret < 0) {
344 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
345 sc->vreg[VREG_CORE].name, ret);
346 return;
347 }
348 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800349 }
350
351 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700352 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700353 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700354 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800355 if (ret) {
356 dev_err(drv.dev,
357 "vdd_dig (cpu%d) decrease failed (%d)\n",
358 cpu, ret);
359 return;
360 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700361 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800362 }
363
364 /*
365 * Decrease vdd_mem active-set after vdd_dig.
366 * vdd_mem should be >= vdd_dig.
367 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700368 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700369 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700370 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800371 if (ret) {
372 dev_err(drv.dev,
373 "vdd_mem (cpu%d) decrease failed (%d)\n",
374 cpu, ret);
375 return;
376 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700377 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800378 }
379}
380
381static int calculate_vdd_mem(const struct acpu_level *tgt)
382{
Matt Wagantall600ea502012-06-08 18:49:53 -0700383 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800384}
385
Matt Wagantall72a38002012-07-18 13:42:55 -0700386static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800387{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700388 const int *hfpll_vdd = drv.hfpll_data->vdd;
389 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700390 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800391
Matt Wagantall72a38002012-07-18 13:42:55 -0700392 if (s->src != HFPLL)
393 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700394 else if (s->pll_l_val > nom_vdd_l_max)
395 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700396 else if (s->pll_l_val > low_vdd_l_max)
397 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800398 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700399 return hfpll_vdd[HFPLL_VDD_LOW];
400}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800401
Matt Wagantall72a38002012-07-18 13:42:55 -0700402static int calculate_vdd_dig(const struct acpu_level *tgt)
403{
404 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
405
406 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
407 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
408
409 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
410 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800411}
412
413static int calculate_vdd_core(const struct acpu_level *tgt)
414{
415 return tgt->vdd_core;
416}
417
418/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
419static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
420 enum setrate_reason reason)
421{
422 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800423 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700424 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700425 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800426 unsigned long flags;
427 int rc = 0;
428
Matt Wagantall5941a332012-07-10 23:20:44 -0700429 if (cpu > num_possible_cpus())
430 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800431
432 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
433 mutex_lock(&driver_lock);
434
435 strt_acpu_s = drv.scalable[cpu].cur_speed;
436
437 /* Return early if rate didn't change. */
438 if (rate == strt_acpu_s->khz)
439 goto out;
440
441 /* Find target frequency. */
442 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
443 if (tgt->speed.khz == rate) {
444 tgt_acpu_s = &tgt->speed;
445 break;
446 }
447 }
448 if (tgt->speed.khz == 0) {
449 rc = -EINVAL;
450 goto out;
451 }
452
453 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700454 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
455 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
456 vdd_data.vdd_core = calculate_vdd_core(tgt);
457 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800458
459 /* Increase VDD levels if needed. */
460 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700461 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800462 if (rc)
463 goto out;
464 }
465
466 pr_debug("Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
467 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
468
469 /* Set the new CPU speed. */
470 set_speed(&drv.scalable[cpu], tgt_acpu_s);
471
472 /*
473 * Update the L2 vote and apply the rate change. A spinlock is
474 * necessary to ensure L2 rate is calculated and set atomically
475 * with the CPU frequency, even if acpuclk_krait_set_rate() is
476 * called from an atomic context and the driver_lock mutex is not
477 * acquired.
478 */
479 spin_lock_irqsave(&l2_lock, flags);
480 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700481 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800482 spin_unlock_irqrestore(&l2_lock, flags);
483
484 /* Nothing else to do for power collapse or SWFI. */
485 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
486 goto out;
487
488 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700489 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800490
491 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700492 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800493
494 pr_debug("ACPU%d speed change complete\n", cpu);
495
496out:
497 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
498 mutex_unlock(&driver_lock);
499 return rc;
500}
501
502/* Initialize a HFPLL at a given rate and enable it. */
503static void __init hfpll_init(struct scalable *sc,
504 const struct core_speed *tgt_s)
505{
506 pr_debug("Initializing HFPLL%d\n", sc - drv.scalable);
507
508 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700509 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800510
511 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700512 writel_relaxed(drv.hfpll_data->config_val,
513 sc->hfpll_base + drv.hfpll_data->config_offset);
514 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
515 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800516
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700517 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700518 if (drv.hfpll_data->has_droop_ctl)
519 writel_relaxed(drv.hfpll_data->droop_val,
520 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700521
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800522 /* Set an initial rate and enable the PLL. */
523 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700524 hfpll_enable(sc, false);
525}
526
Matt Wagantall302d9a32012-07-03 13:37:29 -0700527static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700528 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700529{
530 int ret;
531
532 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700533 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700534
535 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
536 sc->vreg[vreg].name);
537 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700538 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
539 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
540 sc->vreg[vreg].name, ret);
541 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700542 }
543
544 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
545 sc->vreg[vreg].max_vdd);
546 if (ret) {
547 dev_err(drv.dev, "%s initialization failed (%d)\n",
548 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700549 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700550 }
551 sc->vreg[vreg].cur_vdd = vdd;
552
Matt Wagantall302d9a32012-07-03 13:37:29 -0700553 if (enable) {
554 ret = enable_rpm_vreg(&sc->vreg[vreg]);
555 if (ret)
556 goto err_conf;
557 }
558
559 return 0;
560
561err_conf:
562 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
563err_get:
564 return ret;
565}
566
567static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
568 enum vregs vreg)
569{
570 if (!sc->vreg[vreg].rpm_reg)
571 return;
572
573 disable_rpm_vreg(&sc->vreg[vreg]);
574 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800575}
576
577/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700578static int __cpuinit regulator_init(struct scalable *sc,
579 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800580{
Matt Wagantall754ee272012-06-18 13:40:26 -0700581 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800582
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700583 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700584 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
585 if (ret)
586 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700587
588 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700589 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
590 if (ret)
591 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700592
Matt Wagantall302d9a32012-07-03 13:37:29 -0700593 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700594 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700595 if (ret)
596 goto err_hfpll_a;
597 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700598 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700599 if (ret)
600 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700601
Matt Wagantall754ee272012-06-18 13:40:26 -0700602 /* Setup Krait CPU regulators and initial core voltage. */
603 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
604 sc->vreg[VREG_CORE].name);
605 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700606 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
607 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
608 sc->vreg[VREG_CORE].name, ret);
609 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700610 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700611 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
612 acpu_level->ua_core);
613 if (ret < 0) {
614 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
615 sc->vreg[VREG_CORE].name, ret);
616 goto err_core_conf;
617 }
618 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700619 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700620 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
621 sc->vreg[VREG_CORE].max_vdd);
622 if (ret) {
623 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
624 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700625 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700626 }
627 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700628 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
629 if (ret) {
630 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
631 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700632 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800633 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700634
635 return 0;
636
637err_core_conf:
638 regulator_put(sc->vreg[VREG_CORE].reg);
639err_core_get:
640 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
641err_hfpll_b:
642 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
643err_hfpll_a:
644 rpm_regulator_cleanup(sc, VREG_DIG);
645err_dig:
646 rpm_regulator_cleanup(sc, VREG_MEM);
647err_mem:
648 return ret;
649}
650
651static void __cpuinit regulator_cleanup(struct scalable *sc)
652{
653 regulator_disable(sc->vreg[VREG_CORE].reg);
654 regulator_put(sc->vreg[VREG_CORE].reg);
655 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
656 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
657 rpm_regulator_cleanup(sc, VREG_DIG);
658 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800659}
660
661/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700662static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700663 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800664{
665 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700666 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800667
668 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700669 if (sc->aux_clk_sel_phys) {
670 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700671 if (!aux_reg)
672 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700673 writel_relaxed(sc->aux_clk_sel, aux_reg);
674 iounmap(aux_reg);
675 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800676
677 /* Switch away from the HFPLL while it's re-initialized. */
678 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
679 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
680 hfpll_init(sc, tgt_s);
681
682 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
683 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
684 regval &= ~(0x3 << 6);
685 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
686
687 /* Switch to the target clock source. */
688 set_sec_clk_src(sc, tgt_s->sec_src_sel);
689 set_pri_clk_src(sc, tgt_s->pri_src_sel);
690 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700691
692 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800693}
694
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700695static void __cpuinit fill_cur_core_speed(struct core_speed *s,
696 struct scalable *sc)
697{
698 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
699 s->sec_src_sel = (get_l2_indirect_reg(sc->l2cpmr_iaddr) >> 2) & 0x3;
700 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
701}
702
703static bool __cpuinit speed_equal(const struct core_speed *s1,
704 const struct core_speed *s2)
705{
706 return (s1->pri_src_sel == s2->pri_src_sel &&
707 s1->sec_src_sel == s2->sec_src_sel &&
708 s1->pll_l_val == s2->pll_l_val);
709}
710
711static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
712{
713 struct scalable *sc = &drv.scalable[cpu];
714 const struct acpu_level *l;
715 struct core_speed cur_speed;
716
717 fill_cur_core_speed(&cur_speed, sc);
718 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
719 if (speed_equal(&l->speed, &cur_speed))
720 return l;
721 return NULL;
722}
723
724static const struct l2_level __init *find_cur_l2_level(void)
725{
726 struct scalable *sc = &drv.scalable[L2];
727 const struct l2_level *l;
728 struct core_speed cur_speed;
729
730 fill_cur_core_speed(&cur_speed, sc);
731 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
732 if (speed_equal(&l->speed, &cur_speed))
733 return l;
734 return NULL;
735}
736
737static const struct acpu_level __cpuinit *find_min_acpu_level(void)
738{
739 struct acpu_level *l;
740
741 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
742 if (l->use_for_scaling)
743 return l;
744
745 return NULL;
746}
747
Matt Wagantall302d9a32012-07-03 13:37:29 -0700748static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800749{
Matt Wagantall754ee272012-06-18 13:40:26 -0700750 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700751 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700752 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800753
Matt Wagantall754ee272012-06-18 13:40:26 -0700754 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700755 if (!sc->hfpll_base) {
756 ret = -ENOMEM;
757 goto err_ioremap;
758 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700759
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700760 acpu_level = find_cur_acpu_level(cpu);
761 if (!acpu_level || acpu_level->speed.src == QSB) {
762 acpu_level = find_min_acpu_level();
763 if (!acpu_level) {
764 ret = -ENODEV;
765 goto err_table;
766 }
767 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
768 cpu, acpu_level->speed.khz);
769 } else {
770 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
771 acpu_level->speed.khz);
772 }
773
774 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700775 if (ret)
776 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700777
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700778 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700779 if (ret)
780 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700781
782 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700783 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700784
785 return 0;
786
787err_clocks:
788 regulator_cleanup(sc);
789err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700790err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700791 iounmap(sc->hfpll_base);
792err_ioremap:
793 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800794}
795
796/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700797static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800798{
799 int ret;
800
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700801 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800802 if (!drv.bus_perf_client) {
803 dev_err(drv.dev, "unable to register bus client\n");
804 BUG();
805 }
806
Matt Wagantall754ee272012-06-18 13:40:26 -0700807 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700808 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800809 if (ret)
810 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
811}
812
813#ifdef CONFIG_CPU_FREQ_MSM
814static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
815
816static void __init cpufreq_table_init(void)
817{
818 int cpu;
819
820 for_each_possible_cpu(cpu) {
821 int i, freq_cnt = 0;
822 /* Construct the freq_table tables from acpu_freq_tbl. */
823 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
824 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
825 if (drv.acpu_freq_tbl[i].use_for_scaling) {
826 freq_table[cpu][freq_cnt].index = freq_cnt;
827 freq_table[cpu][freq_cnt].frequency
828 = drv.acpu_freq_tbl[i].speed.khz;
829 freq_cnt++;
830 }
831 }
832 /* freq_table not big enough to store all usable freqs. */
833 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
834
835 freq_table[cpu][freq_cnt].index = freq_cnt;
836 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
837
838 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
839 cpu, freq_cnt);
840
841 /* Register table with CPUFreq. */
842 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
843 }
844}
845#else
846static void __init cpufreq_table_init(void) {}
847#endif
848
849#define HOT_UNPLUG_KHZ STBY_KHZ
850static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
851 unsigned long action, void *hcpu)
852{
853 static int prev_khz[NR_CPUS];
854 int rc, cpu = (int)hcpu;
855 struct scalable *sc = &drv.scalable[cpu];
856
857 switch (action & ~CPU_TASKS_FROZEN) {
858 case CPU_DEAD:
859 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
860 /* Fall through. */
861 case CPU_UP_CANCELED:
862 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
863 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
864 break;
865 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700866 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700867 rc = per_cpu_init(cpu);
868 if (rc)
869 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700870 break;
871 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800872 if (WARN_ON(!prev_khz[cpu]))
873 return NOTIFY_BAD;
874 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700875 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800876 if (rc < 0)
877 return NOTIFY_BAD;
878 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
879 break;
880 default:
881 break;
882 }
883
884 return NOTIFY_OK;
885}
886
887static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
888 .notifier_call = acpuclk_cpu_callback,
889};
890
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700891static const int krait_needs_vmin(void)
892{
893 switch (read_cpuid_id()) {
894 case 0x511F04D0: /* KR28M2A20 */
895 case 0x511F04D1: /* KR28M2A21 */
896 case 0x510F06F0: /* KR28M4A10 */
897 return 1;
898 default:
899 return 0;
900 };
901}
902
903static void krait_apply_vmin(struct acpu_level *tbl)
904{
905 for (; tbl->speed.khz != 0; tbl++)
906 if (tbl->vdd_core < 1150000)
907 tbl->vdd_core = 1150000;
908}
909
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700910static int __init select_freq_plan(u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800911{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800912 void __iomem *qfprom_base;
913 u32 pte_efuse, pvs, tbl_idx;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700914 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Faster", "Unknown" };
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800915
916 qfprom_base = ioremap(qfprom_phys, SZ_256);
917 /* Select frequency tables. */
918 if (qfprom_base) {
919 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
920 pvs = (pte_efuse >> 10) & 0x7;
921 iounmap(qfprom_base);
922 if (pvs == 0x7)
923 pvs = (pte_efuse >> 13) & 0x7;
924
925 switch (pvs) {
926 case 0x0:
927 case 0x7:
928 tbl_idx = PVS_SLOW;
929 break;
930 case 0x1:
931 tbl_idx = PVS_NOMINAL;
932 break;
933 case 0x3:
934 tbl_idx = PVS_FAST;
935 break;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700936 case 0x4:
937 tbl_idx = PVS_FASTER;
938 break;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800939 default:
940 tbl_idx = PVS_UNKNOWN;
941 break;
942 }
943 } else {
944 tbl_idx = PVS_UNKNOWN;
945 dev_err(drv.dev, "Unable to map QFPROM base\n");
946 }
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700947 if (tbl_idx == PVS_UNKNOWN) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800948 tbl_idx = PVS_SLOW;
949 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
950 pvs_names[tbl_idx]);
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700951 } else {
952 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800953 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800954
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700955 return tbl_idx;
956}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700957
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800958static struct acpuclk_data acpuclk_krait_data = {
959 .set_rate = acpuclk_krait_set_rate,
960 .get_rate = acpuclk_krait_get_rate,
961 .power_collapse_khz = STBY_KHZ,
962 .wait_for_irq_khz = STBY_KHZ,
963};
964
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700965static void __init drv_data_init(struct device *dev,
966 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800967{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700968 int tbl_idx;
969
970 drv.dev = dev;
971 drv.scalable = kmemdup(params->scalable, params->scalable_size,
972 GFP_KERNEL);
973 BUG_ON(!drv.scalable);
974
975 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
976 GFP_KERNEL);
977 BUG_ON(!drv.hfpll_data);
978
979 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
980 GFP_KERNEL);
981 BUG_ON(!drv.l2_freq_tbl);
982
983 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
984 GFP_KERNEL);
985 BUG_ON(!drv.bus_scale);
986 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
987 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
988 GFP_KERNEL);
989 BUG_ON(!drv.bus_scale->usecase);
990
991 tbl_idx = select_freq_plan(params->qfprom_phys_base);
992 drv.acpu_freq_tbl = kmemdup(params->pvs_tables[tbl_idx].table,
993 params->pvs_tables[tbl_idx].size,
994 GFP_KERNEL);
995 BUG_ON(!drv.acpu_freq_tbl);
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700996}
997
998static void __init hw_init(void)
999{
1000 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001001 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001002 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001003
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001004 if (krait_needs_vmin())
1005 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001006
Matt Wagantall754ee272012-06-18 13:40:26 -07001007 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1008 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001009
Matt Wagantall302d9a32012-07-03 13:37:29 -07001010 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1011 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1012 BUG_ON(rc);
1013 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1014 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1015 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001016
1017 l2_level = find_cur_l2_level();
1018 if (!l2_level || l2_level->speed.src == QSB) {
1019 l2_level = drv.l2_freq_tbl;
1020 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to QSB.\n");
1021 } else {
1022 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1023 l2_level->speed.khz);
1024 }
1025
1026 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001027 BUG_ON(rc);
1028
1029 for_each_online_cpu(cpu) {
1030 rc = per_cpu_init(cpu);
1031 BUG_ON(rc);
1032 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001033
1034 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001035}
1036
1037int __init acpuclk_krait_init(struct device *dev,
1038 const struct acpuclk_krait_params *params)
1039{
1040 drv_data_init(dev, params);
1041 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001042
1043 cpufreq_table_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001044 acpuclk_register(&acpuclk_krait_data);
1045 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1046
1047 return 0;
1048}