blob: 1469beeb9583b1e19aa92829099df337228c0240 [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
Matt Wagantalle9b715a2012-01-04 18:16:14 -080014#include <linux/kernel.h>
Matt Wagantall9515bc22012-07-19 18:13:40 -070015#include <linux/module.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080016#include <linux/init.h>
17#include <linux/io.h>
18#include <linux/delay.h>
19#include <linux/mutex.h>
20#include <linux/err.h>
21#include <linux/errno.h>
22#include <linux/cpufreq.h>
23#include <linux/cpu.h>
24#include <linux/regulator/consumer.h>
25
26#include <asm/mach-types.h>
27#include <asm/cpu.h>
28
29#include <mach/board.h>
30#include <mach/msm_iomap.h>
31#include <mach/socinfo.h>
32#include <mach/msm-krait-l2-accessors.h>
33#include <mach/rpm-regulator.h>
Matt Wagantall75473eb2012-05-31 15:23:22 -070034#include <mach/rpm-regulator-smd.h>
Matt Wagantalle9b715a2012-01-04 18:16:14 -080035#include <mach/msm_bus.h>
36
37#include "acpuclock.h"
38#include "acpuclock-krait.h"
39
40/* MUX source selects. */
41#define PRI_SRC_SEL_SEC_SRC 0
42#define PRI_SRC_SEL_HFPLL 1
43#define PRI_SRC_SEL_HFPLL_DIV2 2
44#define SEC_SRC_SEL_QSB 0
45#define SEC_SRC_SEL_L2PLL 1
46#define SEC_SRC_SEL_AUX 2
47
48/* PTE EFUSE register offset. */
49#define PTE_EFUSE 0xC0
50
51static DEFINE_MUTEX(driver_lock);
52static DEFINE_SPINLOCK(l2_lock);
53
54static struct drv_data {
Matt Wagantall06e4a1f2012-06-07 18:38:13 -070055 struct acpu_level *acpu_freq_tbl;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080056 const struct l2_level *l2_freq_tbl;
57 struct scalable *scalable;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070058 struct hfpll_data *hfpll_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -080059 u32 bus_perf_client;
Matt Wagantall1f3762d2012-06-08 19:08:48 -070060 struct msm_bus_scale_pdata *bus_scale;
Matt Wagantall9515bc22012-07-19 18:13:40 -070061 int boost_uv;
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{
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700173 void __iomem *base = sc->hfpll_base;
174 u32 regval;
175
176 writel_relaxed(tgt_s->pll_l_val, base + drv.hfpll_data->l_offset);
177
178 if (drv.hfpll_data->has_user_reg) {
179 regval = readl_relaxed(base + drv.hfpll_data->user_offset);
180 if (tgt_s->pll_l_val <= drv.hfpll_data->low_vco_l_max)
181 regval &= ~drv.hfpll_data->user_vco_mask;
182 else
183 regval |= drv.hfpll_data->user_vco_mask;
184 writel_relaxed(regval, base + drv.hfpll_data->user_offset);
185 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800186}
187
188/* Return the L2 speed that should be applied. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700189static unsigned int compute_l2_level(struct scalable *sc, unsigned int vote_l)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800190{
Matt Wagantall600ea502012-06-08 18:49:53 -0700191 unsigned int new_l = 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800192 int cpu;
193
194 /* Find max L2 speed vote. */
195 sc->l2_vote = vote_l;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800196 for_each_present_cpu(cpu)
197 new_l = max(new_l, drv.scalable[cpu].l2_vote);
198
199 return new_l;
200}
201
202/* Update the bus bandwidth request. */
203static void set_bus_bw(unsigned int bw)
204{
205 int ret;
206
207 /* Update bandwidth if request has changed. This may sleep. */
208 ret = msm_bus_scale_client_update_request(drv.bus_perf_client, bw);
209 if (ret)
210 dev_err(drv.dev, "bandwidth request failed (%d)\n", ret);
211}
212
213/* Set the CPU or L2 clock speed. */
214static void set_speed(struct scalable *sc, const struct core_speed *tgt_s)
215{
216 const struct core_speed *strt_s = sc->cur_speed;
217
218 if (strt_s->src == HFPLL && tgt_s->src == HFPLL) {
219 /*
220 * Move to an always-on source running at a frequency
221 * that does not require an elevated CPU voltage.
222 */
223 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
224 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
225
226 /* Re-program HFPLL. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700227 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800228 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700229 hfpll_enable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800230
231 /* Move to HFPLL. */
232 set_pri_clk_src(sc, tgt_s->pri_src_sel);
233 } else if (strt_s->src == HFPLL && tgt_s->src != HFPLL) {
234 set_sec_clk_src(sc, tgt_s->sec_src_sel);
235 set_pri_clk_src(sc, tgt_s->pri_src_sel);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700236 hfpll_disable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800237 } else if (strt_s->src != HFPLL && tgt_s->src == HFPLL) {
238 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700239 hfpll_enable(sc, false);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800240 set_pri_clk_src(sc, tgt_s->pri_src_sel);
241 } else {
242 set_sec_clk_src(sc, tgt_s->sec_src_sel);
243 }
244
245 sc->cur_speed = tgt_s;
246}
247
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700248struct vdd_data {
249 int vdd_mem;
250 int vdd_dig;
251 int vdd_core;
252 int ua_core;
253};
254
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800255/* Apply any per-cpu voltage increases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700256static int increase_vdd(int cpu, struct vdd_data *data,
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800257 enum setrate_reason reason)
258{
259 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700260 int rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800261
262 /*
263 * Increase vdd_mem active-set before vdd_dig.
264 * vdd_mem should be >= vdd_dig.
265 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700266 if (data->vdd_mem > sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700267 rc = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700268 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800269 if (rc) {
270 dev_err(drv.dev,
271 "vdd_mem (cpu%d) increase failed (%d)\n",
272 cpu, rc);
273 return rc;
274 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700275 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800276 }
277
278 /* Increase vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700279 if (data->vdd_dig > sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700280 rc = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700281 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800282 if (rc) {
283 dev_err(drv.dev,
284 "vdd_dig (cpu%d) increase failed (%d)\n",
285 cpu, rc);
286 return rc;
287 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700288 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
289 }
290
291 /* Increase current request. */
292 if (data->ua_core > sc->vreg[VREG_CORE].cur_ua) {
293 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
294 data->ua_core);
295 if (rc < 0) {
296 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
297 sc->vreg[VREG_CORE].name, rc);
298 return rc;
299 }
300 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800301 }
302
303 /*
304 * Update per-CPU core voltage. Don't do this for the hotplug path for
305 * which it should already be correct. Attempting to set it is bad
306 * because we don't know what CPU we are running on at this point, but
307 * the CPU regulator API requires we call it from the affected CPU.
308 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700309 if (data->vdd_core > sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800310 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700311 rc = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
312 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800313 if (rc) {
314 dev_err(drv.dev,
315 "vdd_core (cpu%d) increase failed (%d)\n",
316 cpu, rc);
317 return rc;
318 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700319 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800320 }
321
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700322 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800323}
324
325/* Apply any per-cpu voltage decreases. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700326static void decrease_vdd(int cpu, struct vdd_data *data,
327 enum setrate_reason reason)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800328{
329 struct scalable *sc = &drv.scalable[cpu];
330 int ret;
331
332 /*
333 * Update per-CPU core voltage. This must be called on the CPU
334 * that's being affected. Don't do this in the hotplug remove path,
335 * where the rail is off and we're executing on the other CPU.
336 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700337 if (data->vdd_core < sc->vreg[VREG_CORE].cur_vdd
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800338 && reason != SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700339 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg,
340 data->vdd_core, sc->vreg[VREG_CORE].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800341 if (ret) {
342 dev_err(drv.dev,
343 "vdd_core (cpu%d) decrease failed (%d)\n",
344 cpu, ret);
345 return;
346 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700347 sc->vreg[VREG_CORE].cur_vdd = data->vdd_core;
348 }
349
350 /* Decrease current request. */
351 if (data->ua_core < sc->vreg[VREG_CORE].cur_ua) {
352 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
353 data->ua_core);
354 if (ret < 0) {
355 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
356 sc->vreg[VREG_CORE].name, ret);
357 return;
358 }
359 sc->vreg[VREG_CORE].cur_ua = data->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800360 }
361
362 /* Decrease vdd_dig active-set vote. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700363 if (data->vdd_dig < sc->vreg[VREG_DIG].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700364 ret = rpm_regulator_set_voltage(sc->vreg[VREG_DIG].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700365 data->vdd_dig, sc->vreg[VREG_DIG].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800366 if (ret) {
367 dev_err(drv.dev,
368 "vdd_dig (cpu%d) decrease failed (%d)\n",
369 cpu, ret);
370 return;
371 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700372 sc->vreg[VREG_DIG].cur_vdd = data->vdd_dig;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800373 }
374
375 /*
376 * Decrease vdd_mem active-set after vdd_dig.
377 * vdd_mem should be >= vdd_dig.
378 */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700379 if (data->vdd_mem < sc->vreg[VREG_MEM].cur_vdd) {
Matt Wagantall75473eb2012-05-31 15:23:22 -0700380 ret = rpm_regulator_set_voltage(sc->vreg[VREG_MEM].rpm_reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700381 data->vdd_mem, sc->vreg[VREG_MEM].max_vdd);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800382 if (ret) {
383 dev_err(drv.dev,
384 "vdd_mem (cpu%d) decrease failed (%d)\n",
385 cpu, ret);
386 return;
387 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700388 sc->vreg[VREG_MEM].cur_vdd = data->vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800389 }
390}
391
392static int calculate_vdd_mem(const struct acpu_level *tgt)
393{
Matt Wagantall600ea502012-06-08 18:49:53 -0700394 return drv.l2_freq_tbl[tgt->l2_level].vdd_mem;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800395}
396
Matt Wagantall72a38002012-07-18 13:42:55 -0700397static int get_src_dig(const struct core_speed *s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800398{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700399 const int *hfpll_vdd = drv.hfpll_data->vdd;
400 const u32 low_vdd_l_max = drv.hfpll_data->low_vdd_l_max;
Matt Wagantall87465f52012-07-23 22:03:06 -0700401 const u32 nom_vdd_l_max = drv.hfpll_data->nom_vdd_l_max;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800402
Matt Wagantall72a38002012-07-18 13:42:55 -0700403 if (s->src != HFPLL)
404 return hfpll_vdd[HFPLL_VDD_NONE];
Matt Wagantall87465f52012-07-23 22:03:06 -0700405 else if (s->pll_l_val > nom_vdd_l_max)
406 return hfpll_vdd[HFPLL_VDD_HIGH];
Matt Wagantall72a38002012-07-18 13:42:55 -0700407 else if (s->pll_l_val > low_vdd_l_max)
408 return hfpll_vdd[HFPLL_VDD_NOM];
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800409 else
Matt Wagantall72a38002012-07-18 13:42:55 -0700410 return hfpll_vdd[HFPLL_VDD_LOW];
411}
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800412
Matt Wagantall72a38002012-07-18 13:42:55 -0700413static int calculate_vdd_dig(const struct acpu_level *tgt)
414{
415 int l2_pll_vdd_dig, cpu_pll_vdd_dig;
416
417 l2_pll_vdd_dig = get_src_dig(&drv.l2_freq_tbl[tgt->l2_level].speed);
418 cpu_pll_vdd_dig = get_src_dig(&tgt->speed);
419
420 return max(drv.l2_freq_tbl[tgt->l2_level].vdd_dig,
421 max(l2_pll_vdd_dig, cpu_pll_vdd_dig));
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800422}
423
Matt Wagantall9515bc22012-07-19 18:13:40 -0700424static bool enable_boost = true;
425module_param_named(boost, enable_boost, bool, S_IRUGO | S_IWUSR);
426
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800427static int calculate_vdd_core(const struct acpu_level *tgt)
428{
Matt Wagantall9515bc22012-07-19 18:13:40 -0700429 return tgt->vdd_core + (enable_boost ? drv.boost_uv : 0);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800430}
431
432/* Set the CPU's clock rate and adjust the L2 rate, voltage and BW requests. */
433static int acpuclk_krait_set_rate(int cpu, unsigned long rate,
434 enum setrate_reason reason)
435{
436 const struct core_speed *strt_acpu_s, *tgt_acpu_s;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800437 const struct acpu_level *tgt;
Matt Wagantall600ea502012-06-08 18:49:53 -0700438 int tgt_l2_l;
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700439 struct vdd_data vdd_data;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800440 unsigned long flags;
441 int rc = 0;
442
Matt Wagantall5941a332012-07-10 23:20:44 -0700443 if (cpu > num_possible_cpus())
444 return -EINVAL;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800445
446 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
447 mutex_lock(&driver_lock);
448
449 strt_acpu_s = drv.scalable[cpu].cur_speed;
450
451 /* Return early if rate didn't change. */
452 if (rate == strt_acpu_s->khz)
453 goto out;
454
455 /* Find target frequency. */
456 for (tgt = drv.acpu_freq_tbl; tgt->speed.khz != 0; tgt++) {
457 if (tgt->speed.khz == rate) {
458 tgt_acpu_s = &tgt->speed;
459 break;
460 }
461 }
462 if (tgt->speed.khz == 0) {
463 rc = -EINVAL;
464 goto out;
465 }
466
467 /* Calculate voltage requirements for the current CPU. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700468 vdd_data.vdd_mem = calculate_vdd_mem(tgt);
469 vdd_data.vdd_dig = calculate_vdd_dig(tgt);
470 vdd_data.vdd_core = calculate_vdd_core(tgt);
471 vdd_data.ua_core = tgt->ua_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800472
473 /* Increase VDD levels if needed. */
474 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG) {
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700475 rc = increase_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800476 if (rc)
477 goto out;
478 }
479
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700480 dev_dbg(drv.dev, "Switching from ACPU%d rate %lu KHz -> %lu KHz\n",
481 cpu, strt_acpu_s->khz, tgt_acpu_s->khz);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800482
483 /* Set the new CPU speed. */
484 set_speed(&drv.scalable[cpu], tgt_acpu_s);
485
486 /*
487 * Update the L2 vote and apply the rate change. A spinlock is
488 * necessary to ensure L2 rate is calculated and set atomically
489 * with the CPU frequency, even if acpuclk_krait_set_rate() is
490 * called from an atomic context and the driver_lock mutex is not
491 * acquired.
492 */
493 spin_lock_irqsave(&l2_lock, flags);
494 tgt_l2_l = compute_l2_level(&drv.scalable[cpu], tgt->l2_level);
Matt Wagantall600ea502012-06-08 18:49:53 -0700495 set_speed(&drv.scalable[L2], &drv.l2_freq_tbl[tgt_l2_l].speed);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800496 spin_unlock_irqrestore(&l2_lock, flags);
497
498 /* Nothing else to do for power collapse or SWFI. */
499 if (reason == SETRATE_PC || reason == SETRATE_SWFI)
500 goto out;
501
502 /* Update bus bandwith request. */
Matt Wagantall600ea502012-06-08 18:49:53 -0700503 set_bus_bw(drv.l2_freq_tbl[tgt_l2_l].bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800504
505 /* Drop VDD levels if we can. */
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700506 decrease_vdd(cpu, &vdd_data, reason);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800507
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700508 dev_dbg(drv.dev, "ACPU%d speed change complete\n", cpu);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800509
510out:
511 if (reason == SETRATE_CPUFREQ || reason == SETRATE_HOTPLUG)
512 mutex_unlock(&driver_lock);
513 return rc;
514}
515
516/* Initialize a HFPLL at a given rate and enable it. */
517static void __init hfpll_init(struct scalable *sc,
518 const struct core_speed *tgt_s)
519{
Matt Wagantallbd1b4042012-07-24 11:20:03 -0700520 dev_dbg(drv.dev, "Initializing HFPLL%d\n", sc - drv.scalable);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800521
522 /* Disable the PLL for re-programming. */
Matt Wagantall75473eb2012-05-31 15:23:22 -0700523 hfpll_disable(sc, true);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800524
525 /* Configure PLL parameters for integer mode. */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700526 writel_relaxed(drv.hfpll_data->config_val,
527 sc->hfpll_base + drv.hfpll_data->config_offset);
528 writel_relaxed(0, sc->hfpll_base + drv.hfpll_data->m_offset);
529 writel_relaxed(1, sc->hfpll_base + drv.hfpll_data->n_offset);
Matt Wagantalla77b7f32012-07-18 16:32:01 -0700530 if (drv.hfpll_data->has_user_reg)
531 writel_relaxed(drv.hfpll_data->user_val,
532 sc->hfpll_base + drv.hfpll_data->user_offset);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800533
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700534 /* Program droop controller, if supported */
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700535 if (drv.hfpll_data->has_droop_ctl)
536 writel_relaxed(drv.hfpll_data->droop_val,
537 sc->hfpll_base + drv.hfpll_data->droop_offset);
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700538
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800539 /* Set an initial rate and enable the PLL. */
540 hfpll_set_rate(sc, tgt_s);
Matt Wagantall75473eb2012-05-31 15:23:22 -0700541 hfpll_enable(sc, false);
542}
543
Matt Wagantall302d9a32012-07-03 13:37:29 -0700544static int __cpuinit rpm_regulator_init(struct scalable *sc, enum vregs vreg,
Matt Wagantall754ee272012-06-18 13:40:26 -0700545 int vdd, bool enable)
Matt Wagantall75473eb2012-05-31 15:23:22 -0700546{
547 int ret;
548
549 if (!sc->vreg[vreg].name)
Matt Wagantall302d9a32012-07-03 13:37:29 -0700550 return 0;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700551
552 sc->vreg[vreg].rpm_reg = rpm_regulator_get(drv.dev,
553 sc->vreg[vreg].name);
554 if (IS_ERR(sc->vreg[vreg].rpm_reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700555 ret = PTR_ERR(sc->vreg[vreg].rpm_reg);
556 dev_err(drv.dev, "rpm_regulator_get(%s) failed (%d)\n",
557 sc->vreg[vreg].name, ret);
558 goto err_get;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700559 }
560
561 ret = rpm_regulator_set_voltage(sc->vreg[vreg].rpm_reg, vdd,
562 sc->vreg[vreg].max_vdd);
563 if (ret) {
564 dev_err(drv.dev, "%s initialization failed (%d)\n",
565 sc->vreg[vreg].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700566 goto err_conf;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700567 }
568 sc->vreg[vreg].cur_vdd = vdd;
569
Matt Wagantall302d9a32012-07-03 13:37:29 -0700570 if (enable) {
571 ret = enable_rpm_vreg(&sc->vreg[vreg]);
572 if (ret)
573 goto err_conf;
574 }
575
576 return 0;
577
578err_conf:
579 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
580err_get:
581 return ret;
582}
583
584static void __cpuinit rpm_regulator_cleanup(struct scalable *sc,
585 enum vregs vreg)
586{
587 if (!sc->vreg[vreg].rpm_reg)
588 return;
589
590 disable_rpm_vreg(&sc->vreg[vreg]);
591 rpm_regulator_put(sc->vreg[vreg].rpm_reg);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800592}
593
594/* Voltage regulator initialization. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700595static int __cpuinit regulator_init(struct scalable *sc,
596 const struct acpu_level *acpu_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800597{
Matt Wagantall754ee272012-06-18 13:40:26 -0700598 int ret, vdd_mem, vdd_dig, vdd_core;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800599
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700600 vdd_mem = calculate_vdd_mem(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700601 ret = rpm_regulator_init(sc, VREG_MEM, vdd_mem, true);
602 if (ret)
603 goto err_mem;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700604
605 vdd_dig = calculate_vdd_dig(acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700606 ret = rpm_regulator_init(sc, VREG_DIG, vdd_dig, true);
607 if (ret)
608 goto err_dig;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700609
Matt Wagantall302d9a32012-07-03 13:37:29 -0700610 ret = rpm_regulator_init(sc, VREG_HFPLL_A,
Matt Wagantall754ee272012-06-18 13:40:26 -0700611 sc->vreg[VREG_HFPLL_A].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700612 if (ret)
613 goto err_hfpll_a;
614 ret = rpm_regulator_init(sc, VREG_HFPLL_B,
Matt Wagantall754ee272012-06-18 13:40:26 -0700615 sc->vreg[VREG_HFPLL_B].max_vdd, false);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700616 if (ret)
617 goto err_hfpll_b;
Matt Wagantall75473eb2012-05-31 15:23:22 -0700618
Matt Wagantall754ee272012-06-18 13:40:26 -0700619 /* Setup Krait CPU regulators and initial core voltage. */
620 sc->vreg[VREG_CORE].reg = regulator_get(drv.dev,
621 sc->vreg[VREG_CORE].name);
622 if (IS_ERR(sc->vreg[VREG_CORE].reg)) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700623 ret = PTR_ERR(sc->vreg[VREG_CORE].reg);
624 dev_err(drv.dev, "regulator_get(%s) failed (%d)\n",
625 sc->vreg[VREG_CORE].name, ret);
626 goto err_core_get;
Matt Wagantall754ee272012-06-18 13:40:26 -0700627 }
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700628 ret = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
629 acpu_level->ua_core);
630 if (ret < 0) {
631 dev_err(drv.dev, "regulator_set_optimum_mode(%s) failed (%d)\n",
632 sc->vreg[VREG_CORE].name, ret);
633 goto err_core_conf;
634 }
635 sc->vreg[VREG_CORE].cur_ua = acpu_level->ua_core;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700636 vdd_core = calculate_vdd_core(acpu_level);
Matt Wagantall754ee272012-06-18 13:40:26 -0700637 ret = regulator_set_voltage(sc->vreg[VREG_CORE].reg, vdd_core,
638 sc->vreg[VREG_CORE].max_vdd);
639 if (ret) {
640 dev_err(drv.dev, "regulator_set_voltage(%s) (%d)\n",
641 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700642 goto err_core_conf;
Matt Wagantall754ee272012-06-18 13:40:26 -0700643 }
644 sc->vreg[VREG_CORE].cur_vdd = vdd_core;
Matt Wagantall754ee272012-06-18 13:40:26 -0700645 ret = regulator_enable(sc->vreg[VREG_CORE].reg);
646 if (ret) {
647 dev_err(drv.dev, "regulator_enable(%s) failed (%d)\n",
648 sc->vreg[VREG_CORE].name, ret);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700649 goto err_core_conf;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800650 }
Matt Wagantall302d9a32012-07-03 13:37:29 -0700651
652 return 0;
653
654err_core_conf:
655 regulator_put(sc->vreg[VREG_CORE].reg);
656err_core_get:
657 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
658err_hfpll_b:
659 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
660err_hfpll_a:
661 rpm_regulator_cleanup(sc, VREG_DIG);
662err_dig:
663 rpm_regulator_cleanup(sc, VREG_MEM);
664err_mem:
665 return ret;
666}
667
668static void __cpuinit regulator_cleanup(struct scalable *sc)
669{
670 regulator_disable(sc->vreg[VREG_CORE].reg);
671 regulator_put(sc->vreg[VREG_CORE].reg);
672 rpm_regulator_cleanup(sc, VREG_HFPLL_B);
673 rpm_regulator_cleanup(sc, VREG_HFPLL_A);
674 rpm_regulator_cleanup(sc, VREG_DIG);
675 rpm_regulator_cleanup(sc, VREG_MEM);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800676}
677
678/* Set initial rate for a given core. */
Matt Wagantall302d9a32012-07-03 13:37:29 -0700679static int __cpuinit init_clock_sources(struct scalable *sc,
Matt Wagantall754ee272012-06-18 13:40:26 -0700680 const struct core_speed *tgt_s)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800681{
682 u32 regval;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700683 void __iomem *aux_reg;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800684
685 /* Program AUX source input to the secondary MUX. */
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700686 if (sc->aux_clk_sel_phys) {
687 aux_reg = ioremap(sc->aux_clk_sel_phys, 4);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700688 if (!aux_reg)
689 return -ENOMEM;
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700690 writel_relaxed(sc->aux_clk_sel, aux_reg);
691 iounmap(aux_reg);
692 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800693
694 /* Switch away from the HFPLL while it's re-initialized. */
695 set_sec_clk_src(sc, SEC_SRC_SEL_AUX);
696 set_pri_clk_src(sc, PRI_SRC_SEL_SEC_SRC);
697 hfpll_init(sc, tgt_s);
698
699 /* Set PRI_SRC_SEL_HFPLL_DIV2 divider to div-2. */
700 regval = get_l2_indirect_reg(sc->l2cpmr_iaddr);
701 regval &= ~(0x3 << 6);
702 set_l2_indirect_reg(sc->l2cpmr_iaddr, regval);
703
704 /* Switch to the target clock source. */
705 set_sec_clk_src(sc, tgt_s->sec_src_sel);
706 set_pri_clk_src(sc, tgt_s->pri_src_sel);
707 sc->cur_speed = tgt_s;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700708
709 return 0;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800710}
711
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700712static void __cpuinit fill_cur_core_speed(struct core_speed *s,
713 struct scalable *sc)
714{
715 s->pri_src_sel = get_l2_indirect_reg(sc->l2cpmr_iaddr) & 0x3;
716 s->sec_src_sel = (get_l2_indirect_reg(sc->l2cpmr_iaddr) >> 2) & 0x3;
717 s->pll_l_val = readl_relaxed(sc->hfpll_base + drv.hfpll_data->l_offset);
718}
719
720static bool __cpuinit speed_equal(const struct core_speed *s1,
721 const struct core_speed *s2)
722{
723 return (s1->pri_src_sel == s2->pri_src_sel &&
724 s1->sec_src_sel == s2->sec_src_sel &&
725 s1->pll_l_val == s2->pll_l_val);
726}
727
728static const struct acpu_level __cpuinit *find_cur_acpu_level(int cpu)
729{
730 struct scalable *sc = &drv.scalable[cpu];
731 const struct acpu_level *l;
732 struct core_speed cur_speed;
733
734 fill_cur_core_speed(&cur_speed, sc);
735 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
736 if (speed_equal(&l->speed, &cur_speed))
737 return l;
738 return NULL;
739}
740
741static const struct l2_level __init *find_cur_l2_level(void)
742{
743 struct scalable *sc = &drv.scalable[L2];
744 const struct l2_level *l;
745 struct core_speed cur_speed;
746
747 fill_cur_core_speed(&cur_speed, sc);
748 for (l = drv.l2_freq_tbl; l->speed.khz != 0; l++)
749 if (speed_equal(&l->speed, &cur_speed))
750 return l;
751 return NULL;
752}
753
754static const struct acpu_level __cpuinit *find_min_acpu_level(void)
755{
756 struct acpu_level *l;
757
758 for (l = drv.acpu_freq_tbl; l->speed.khz != 0; l++)
759 if (l->use_for_scaling)
760 return l;
761
762 return NULL;
763}
764
Matt Wagantall302d9a32012-07-03 13:37:29 -0700765static int __cpuinit per_cpu_init(int cpu)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800766{
Matt Wagantall754ee272012-06-18 13:40:26 -0700767 struct scalable *sc = &drv.scalable[cpu];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700768 const struct acpu_level *acpu_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700769 int ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800770
Matt Wagantall754ee272012-06-18 13:40:26 -0700771 sc->hfpll_base = ioremap(sc->hfpll_phys_base, SZ_32);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700772 if (!sc->hfpll_base) {
773 ret = -ENOMEM;
774 goto err_ioremap;
775 }
Matt Wagantall754ee272012-06-18 13:40:26 -0700776
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700777 acpu_level = find_cur_acpu_level(cpu);
778 if (!acpu_level || acpu_level->speed.src == QSB) {
779 acpu_level = find_min_acpu_level();
780 if (!acpu_level) {
781 ret = -ENODEV;
782 goto err_table;
783 }
784 dev_dbg(drv.dev, "CPU%d is running at an unknown rate. Defaulting to %lu KHz.\n",
785 cpu, acpu_level->speed.khz);
786 } else {
787 dev_dbg(drv.dev, "CPU%d is running at %lu KHz\n", cpu,
788 acpu_level->speed.khz);
789 }
790
791 ret = regulator_init(sc, acpu_level);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700792 if (ret)
793 goto err_regulators;
Matt Wagantall754ee272012-06-18 13:40:26 -0700794
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700795 ret = init_clock_sources(sc, &acpu_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -0700796 if (ret)
797 goto err_clocks;
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700798
799 sc->l2_vote = acpu_level->l2_level;
Matt Wagantall754ee272012-06-18 13:40:26 -0700800 sc->initialized = true;
Matt Wagantall302d9a32012-07-03 13:37:29 -0700801
802 return 0;
803
804err_clocks:
805 regulator_cleanup(sc);
806err_regulators:
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700807err_table:
Matt Wagantall302d9a32012-07-03 13:37:29 -0700808 iounmap(sc->hfpll_base);
809err_ioremap:
810 return ret;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800811}
812
813/* Register with bus driver. */
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700814static void __init bus_init(const struct l2_level *l2_level)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800815{
816 int ret;
817
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700818 drv.bus_perf_client = msm_bus_scale_register_client(drv.bus_scale);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800819 if (!drv.bus_perf_client) {
820 dev_err(drv.dev, "unable to register bus client\n");
821 BUG();
822 }
823
Matt Wagantall754ee272012-06-18 13:40:26 -0700824 ret = msm_bus_scale_client_update_request(drv.bus_perf_client,
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -0700825 l2_level->bw_level);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800826 if (ret)
827 dev_err(drv.dev, "initial bandwidth req failed (%d)\n", ret);
828}
829
830#ifdef CONFIG_CPU_FREQ_MSM
831static struct cpufreq_frequency_table freq_table[NR_CPUS][35];
832
833static void __init cpufreq_table_init(void)
834{
835 int cpu;
836
837 for_each_possible_cpu(cpu) {
838 int i, freq_cnt = 0;
839 /* Construct the freq_table tables from acpu_freq_tbl. */
840 for (i = 0; drv.acpu_freq_tbl[i].speed.khz != 0
841 && freq_cnt < ARRAY_SIZE(*freq_table); i++) {
842 if (drv.acpu_freq_tbl[i].use_for_scaling) {
843 freq_table[cpu][freq_cnt].index = freq_cnt;
844 freq_table[cpu][freq_cnt].frequency
845 = drv.acpu_freq_tbl[i].speed.khz;
846 freq_cnt++;
847 }
848 }
849 /* freq_table not big enough to store all usable freqs. */
850 BUG_ON(drv.acpu_freq_tbl[i].speed.khz != 0);
851
852 freq_table[cpu][freq_cnt].index = freq_cnt;
853 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
854
855 dev_info(drv.dev, "CPU%d: %d frequencies supported\n",
856 cpu, freq_cnt);
857
858 /* Register table with CPUFreq. */
859 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
860 }
861}
862#else
863static void __init cpufreq_table_init(void) {}
864#endif
865
866#define HOT_UNPLUG_KHZ STBY_KHZ
867static int __cpuinit acpuclk_cpu_callback(struct notifier_block *nfb,
868 unsigned long action, void *hcpu)
869{
870 static int prev_khz[NR_CPUS];
871 int rc, cpu = (int)hcpu;
872 struct scalable *sc = &drv.scalable[cpu];
873
874 switch (action & ~CPU_TASKS_FROZEN) {
875 case CPU_DEAD:
876 prev_khz[cpu] = acpuclk_krait_get_rate(cpu);
877 /* Fall through. */
878 case CPU_UP_CANCELED:
879 acpuclk_krait_set_rate(cpu, HOT_UNPLUG_KHZ, SETRATE_HOTPLUG);
880 regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg, 0);
881 break;
882 case CPU_UP_PREPARE:
Matt Wagantall754ee272012-06-18 13:40:26 -0700883 if (!sc->initialized) {
Matt Wagantall302d9a32012-07-03 13:37:29 -0700884 rc = per_cpu_init(cpu);
885 if (rc)
886 return NOTIFY_BAD;
Matt Wagantall754ee272012-06-18 13:40:26 -0700887 break;
888 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800889 if (WARN_ON(!prev_khz[cpu]))
890 return NOTIFY_BAD;
891 rc = regulator_set_optimum_mode(sc->vreg[VREG_CORE].reg,
Matt Wagantall6d9c4162012-07-16 18:58:16 -0700892 sc->vreg[VREG_CORE].cur_ua);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800893 if (rc < 0)
894 return NOTIFY_BAD;
895 acpuclk_krait_set_rate(cpu, prev_khz[cpu], SETRATE_HOTPLUG);
896 break;
897 default:
898 break;
899 }
900
901 return NOTIFY_OK;
902}
903
904static struct notifier_block __cpuinitdata acpuclk_cpu_notifier = {
905 .notifier_call = acpuclk_cpu_callback,
906};
907
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700908static const int krait_needs_vmin(void)
909{
910 switch (read_cpuid_id()) {
911 case 0x511F04D0: /* KR28M2A20 */
912 case 0x511F04D1: /* KR28M2A21 */
913 case 0x510F06F0: /* KR28M4A10 */
914 return 1;
915 default:
916 return 0;
917 };
918}
919
920static void krait_apply_vmin(struct acpu_level *tbl)
921{
922 for (; tbl->speed.khz != 0; tbl++)
923 if (tbl->vdd_core < 1150000)
924 tbl->vdd_core = 1150000;
925}
926
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700927static int __init select_freq_plan(u32 qfprom_phys)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800928{
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800929 void __iomem *qfprom_base;
930 u32 pte_efuse, pvs, tbl_idx;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700931 char *pvs_names[] = { "Slow", "Nominal", "Fast", "Faster", "Unknown" };
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800932
933 qfprom_base = ioremap(qfprom_phys, SZ_256);
934 /* Select frequency tables. */
935 if (qfprom_base) {
936 pte_efuse = readl_relaxed(qfprom_base + PTE_EFUSE);
937 pvs = (pte_efuse >> 10) & 0x7;
938 iounmap(qfprom_base);
939 if (pvs == 0x7)
940 pvs = (pte_efuse >> 13) & 0x7;
941
942 switch (pvs) {
943 case 0x0:
944 case 0x7:
945 tbl_idx = PVS_SLOW;
946 break;
947 case 0x1:
948 tbl_idx = PVS_NOMINAL;
949 break;
950 case 0x3:
951 tbl_idx = PVS_FAST;
952 break;
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700953 case 0x4:
954 tbl_idx = PVS_FASTER;
955 break;
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800956 default:
957 tbl_idx = PVS_UNKNOWN;
958 break;
959 }
960 } else {
961 tbl_idx = PVS_UNKNOWN;
962 dev_err(drv.dev, "Unable to map QFPROM base\n");
963 }
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700964 if (tbl_idx == PVS_UNKNOWN) {
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800965 tbl_idx = PVS_SLOW;
966 dev_warn(drv.dev, "ACPU PVS: Defaulting to %s\n",
967 pvs_names[tbl_idx]);
Matt Wagantallf5cc3892012-06-07 19:47:02 -0700968 } else {
969 dev_info(drv.dev, "ACPU PVS: %s\n", pvs_names[tbl_idx]);
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800970 }
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800971
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700972 return tbl_idx;
973}
Matt Wagantall06e4a1f2012-06-07 18:38:13 -0700974
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800975static struct acpuclk_data acpuclk_krait_data = {
976 .set_rate = acpuclk_krait_set_rate,
977 .get_rate = acpuclk_krait_get_rate,
978 .power_collapse_khz = STBY_KHZ,
979 .wait_for_irq_khz = STBY_KHZ,
980};
981
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700982static void __init drv_data_init(struct device *dev,
983 const struct acpuclk_krait_params *params)
Matt Wagantalle9b715a2012-01-04 18:16:14 -0800984{
Matt Wagantall1f3762d2012-06-08 19:08:48 -0700985 int tbl_idx;
986
987 drv.dev = dev;
988 drv.scalable = kmemdup(params->scalable, params->scalable_size,
989 GFP_KERNEL);
990 BUG_ON(!drv.scalable);
991
992 drv.hfpll_data = kmemdup(params->hfpll_data, sizeof(*drv.hfpll_data),
993 GFP_KERNEL);
994 BUG_ON(!drv.hfpll_data);
995
996 drv.l2_freq_tbl = kmemdup(params->l2_freq_tbl, params->l2_freq_tbl_size,
997 GFP_KERNEL);
998 BUG_ON(!drv.l2_freq_tbl);
999
1000 drv.bus_scale = kmemdup(params->bus_scale, sizeof(*drv.bus_scale),
1001 GFP_KERNEL);
1002 BUG_ON(!drv.bus_scale);
1003 drv.bus_scale->usecase = kmemdup(drv.bus_scale->usecase,
1004 drv.bus_scale->num_usecases * sizeof(*drv.bus_scale->usecase),
1005 GFP_KERNEL);
1006 BUG_ON(!drv.bus_scale->usecase);
1007
1008 tbl_idx = select_freq_plan(params->qfprom_phys_base);
1009 drv.acpu_freq_tbl = kmemdup(params->pvs_tables[tbl_idx].table,
1010 params->pvs_tables[tbl_idx].size,
1011 GFP_KERNEL);
1012 BUG_ON(!drv.acpu_freq_tbl);
Matt Wagantall9515bc22012-07-19 18:13:40 -07001013 drv.boost_uv = params->pvs_tables[tbl_idx].boost_uv;
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001014}
1015
1016static void __init hw_init(void)
1017{
1018 struct scalable *l2 = &drv.scalable[L2];
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001019 const struct l2_level *l2_level;
Matt Wagantall302d9a32012-07-03 13:37:29 -07001020 int cpu, rc;
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001021
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001022 if (krait_needs_vmin())
1023 krait_apply_vmin(drv.acpu_freq_tbl);
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001024
Matt Wagantall754ee272012-06-18 13:40:26 -07001025 l2->hfpll_base = ioremap(l2->hfpll_phys_base, SZ_32);
1026 BUG_ON(!l2->hfpll_base);
Matt Wagantall754ee272012-06-18 13:40:26 -07001027
Matt Wagantall302d9a32012-07-03 13:37:29 -07001028 rc = rpm_regulator_init(l2, VREG_HFPLL_A,
1029 l2->vreg[VREG_HFPLL_A].max_vdd, false);
1030 BUG_ON(rc);
1031 rc = rpm_regulator_init(l2, VREG_HFPLL_B,
1032 l2->vreg[VREG_HFPLL_B].max_vdd, false);
1033 BUG_ON(rc);
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001034
1035 l2_level = find_cur_l2_level();
1036 if (!l2_level || l2_level->speed.src == QSB) {
1037 l2_level = drv.l2_freq_tbl;
1038 dev_dbg(drv.dev, "L2 is running at an unknown rate. Defaulting to QSB.\n");
1039 } else {
1040 dev_dbg(drv.dev, "L2 is running at %lu KHz\n",
1041 l2_level->speed.khz);
1042 }
1043
1044 rc = init_clock_sources(l2, &l2_level->speed);
Matt Wagantall302d9a32012-07-03 13:37:29 -07001045 BUG_ON(rc);
1046
1047 for_each_online_cpu(cpu) {
1048 rc = per_cpu_init(cpu);
1049 BUG_ON(rc);
1050 }
Matt Wagantall9c8cb6e2012-07-13 19:39:15 -07001051
1052 bus_init(l2_level);
Matt Wagantall1f3762d2012-06-08 19:08:48 -07001053}
1054
1055int __init acpuclk_krait_init(struct device *dev,
1056 const struct acpuclk_krait_params *params)
1057{
1058 drv_data_init(dev, params);
1059 hw_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001060
1061 cpufreq_table_init();
Matt Wagantalle9b715a2012-01-04 18:16:14 -08001062 acpuclk_register(&acpuclk_krait_data);
1063 register_hotcpu_notifier(&acpuclk_cpu_notifier);
1064
1065 return 0;
1066}