blob: 29b00655c68b61cdafb2865a01132c3e3883fd32 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 *
3 * Copyright (C) 2007 Google, Inc.
Trilok Sonief18f6c2012-03-07 12:33:40 +05304 * Copyright (c) 2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07005 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 */
16
17#include <linux/version.h>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/errno.h>
21#include <linux/string.h>
22#include <linux/delay.h>
23#include <linux/clk.h>
24#include <linux/cpufreq.h>
25#include <linux/mutex.h>
26#include <linux/io.h>
27#include <linux/sort.h>
28#include <mach/board.h>
29#include <mach/msm_iomap.h>
30#include <asm/mach-types.h>
31
32#include "smd_private.h"
33#include "clock.h"
34#include "acpuclock.h"
35#include "spm.h"
36
Taniya Das298de8c2012-02-16 11:45:31 +053037#define SCSS_CLK_CTL_ADDR (MSM_ACC0_BASE + 0x04)
38#define SCSS_CLK_SEL_ADDR (MSM_ACC0_BASE + 0x08)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
40#define PLL2_L_VAL_ADDR (MSM_CLK_CTL_BASE + 0x33C)
41#define PLL2_M_VAL_ADDR (MSM_CLK_CTL_BASE + 0x340)
42#define PLL2_N_VAL_ADDR (MSM_CLK_CTL_BASE + 0x344)
43#define PLL2_CONFIG_ADDR (MSM_CLK_CTL_BASE + 0x34C)
44
45#define VREF_SEL 1 /* 0: 0.625V (50mV step), 1: 0.3125V (25mV step). */
46#define V_STEP (25 * (2 - VREF_SEL)) /* Minimum voltage step size. */
47#define VREG_DATA (VREG_CONFIG | (VREF_SEL << 5))
48#define VREG_CONFIG (BIT(7) | BIT(6)) /* Enable VREG, pull-down if disabled. */
49/* Cause a compile error if the voltage is not a multiple of the step size. */
50#define MV(mv) ((mv) / (!((mv) % V_STEP)))
51/* mv = (750mV + (raw * 25mV)) * (2 - VREF_SEL) */
52#define VDD_RAW(mv) (((MV(mv) / V_STEP) - 30) | VREG_DATA)
53
54#define MAX_AXI_KHZ 192000
55
56struct clock_state {
57 struct clkctl_acpu_speed *current_speed;
58 struct mutex lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059 struct clk *ebi1_clk;
60};
61
62struct pll {
63 unsigned int l;
64 unsigned int m;
65 unsigned int n;
66 unsigned int pre_div;
67};
68
69struct clkctl_acpu_speed {
70 unsigned int use_for_scaling;
71 unsigned int acpu_clk_khz;
72 int src;
73 unsigned int acpu_src_sel;
74 unsigned int acpu_src_div;
75 unsigned int axi_clk_hz;
76 unsigned int vdd_mv;
77 unsigned int vdd_raw;
78 struct pll *pll_rate;
Taniya Dasbb0b6db2012-03-19 14:09:55 +053079 unsigned long lpj; /* loops_per_jiffy */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080};
81
82static struct clock_state drv_state = { 0 };
83
84/* Switch to this when reprogramming PLL2 */
85static struct clkctl_acpu_speed *backup_s;
86
87static struct pll pll2_tbl[] = {
88 { 42, 0, 1, 0 }, /* 806 MHz */
89 { 53, 1, 3, 0 }, /* 1024 MHz */
90 { 125, 0, 1, 1 }, /* 1200 MHz */
91 { 73, 0, 1, 0 }, /* 1401 MHz */
92};
93
94/* Use negative numbers for sources that can't be enabled/disabled */
95
96enum acpuclk_source {
97 LPXO = -2,
98 AXI = -1,
99 PLL_0 = 0,
100 PLL_1,
101 PLL_2,
102 PLL_3,
103 MAX_SOURCE
104};
105
106static struct clk *acpuclk_sources[MAX_SOURCE];
107
108/*
109 * Each ACPU frequency has a certain minimum MSMC1 voltage requirement
110 * that is implicitly met by voting for a specific minimum AXI frequency.
111 * Do NOT change the AXI frequency unless you are _absoulutely_ sure you
112 * know all the h/w requirements.
113 */
114static struct clkctl_acpu_speed acpu_freq_tbl[] = {
115 { 0, 24576, LPXO, 0, 0, 30720000, 900, VDD_RAW(900) },
116 { 0, 61440, PLL_3, 5, 11, 61440000, 900, VDD_RAW(900) },
117 { 1, 122880, PLL_3, 5, 5, 61440000, 900, VDD_RAW(900) },
118 { 0, 184320, PLL_3, 5, 4, 61440000, 900, VDD_RAW(900) },
119 { 0, MAX_AXI_KHZ, AXI, 1, 0, 61440000, 900, VDD_RAW(900) },
120 { 1, 245760, PLL_3, 5, 2, 61440000, 900, VDD_RAW(900) },
121 { 1, 368640, PLL_3, 5, 1, 122800000, 900, VDD_RAW(900) },
122 /* AXI has MSMC1 implications. See above. */
123 { 1, 768000, PLL_1, 2, 0, 153600000, 1050, VDD_RAW(1050) },
124 /*
125 * AXI has MSMC1 implications. See above.
126 */
127 { 1, 806400, PLL_2, 3, 0, UINT_MAX, 1100, VDD_RAW(1100), &pll2_tbl[0]},
128 { 1, 1024000, PLL_2, 3, 0, UINT_MAX, 1200, VDD_RAW(1200), &pll2_tbl[1]},
129 { 1, 1200000, PLL_2, 3, 0, UINT_MAX, 1200, VDD_RAW(1200), &pll2_tbl[2]},
130 { 1, 1401600, PLL_2, 3, 0, UINT_MAX, 1250, VDD_RAW(1250), &pll2_tbl[3]},
131 { 0 }
132};
133
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134static int acpuclk_set_acpu_vdd(struct clkctl_acpu_speed *s)
135{
136 int ret = msm_spm_set_vdd(0, s->vdd_raw);
137 if (ret)
138 return ret;
139
140 /* Wait for voltage to stabilize. */
Matt Wagantallec57f062011-08-16 23:54:46 -0700141 udelay(62);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 return 0;
143}
144
145/* Assumes PLL2 is off and the acpuclock isn't sourced from PLL2 */
146static void acpuclk_config_pll2(struct pll *pll)
147{
148 uint32_t config = readl_relaxed(PLL2_CONFIG_ADDR);
149
150 /* Make sure write to disable PLL_2 has completed
151 * before reconfiguring that PLL. */
152 mb();
153 writel_relaxed(pll->l, PLL2_L_VAL_ADDR);
154 writel_relaxed(pll->m, PLL2_M_VAL_ADDR);
155 writel_relaxed(pll->n, PLL2_N_VAL_ADDR);
156 if (pll->pre_div)
157 config |= BIT(15);
158 else
159 config &= ~BIT(15);
160 writel_relaxed(config, PLL2_CONFIG_ADDR);
161 /* Make sure PLL is programmed before returning. */
162 mb();
163}
164
165/* Set clock source and divider given a clock speed */
166static void acpuclk_set_src(const struct clkctl_acpu_speed *s)
167{
168 uint32_t reg_clksel, reg_clkctl, src_sel;
169
170 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
171
172 /* CLK_SEL_SRC1NO */
173 src_sel = reg_clksel & 1;
174
175 /* Program clock source and divider. */
176 reg_clkctl = readl_relaxed(SCSS_CLK_CTL_ADDR);
177 reg_clkctl &= ~(0xFF << (8 * src_sel));
178 reg_clkctl |= s->acpu_src_sel << (4 + 8 * src_sel);
179 reg_clkctl |= s->acpu_src_div << (0 + 8 * src_sel);
180 writel_relaxed(reg_clkctl, SCSS_CLK_CTL_ADDR);
181
182 /* Toggle clock source. */
183 reg_clksel ^= 1;
184
185 /* Program clock source selection. */
186 writel_relaxed(reg_clksel, SCSS_CLK_SEL_ADDR);
187
188 /* Make sure switch to new source is complete. */
189 mb();
190}
191
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700192static int acpuclk_7x30_set_rate(int cpu, unsigned long rate,
193 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194{
195 struct clkctl_acpu_speed *tgt_s, *strt_s;
196 int res, rc = 0;
197
198 if (reason == SETRATE_CPUFREQ)
199 mutex_lock(&drv_state.lock);
200
201 strt_s = drv_state.current_speed;
202
203 if (rate == strt_s->acpu_clk_khz)
204 goto out;
205
206 for (tgt_s = acpu_freq_tbl; tgt_s->acpu_clk_khz != 0; tgt_s++) {
207 if (tgt_s->acpu_clk_khz == rate)
208 break;
209 }
210 if (tgt_s->acpu_clk_khz == 0) {
211 rc = -EINVAL;
212 goto out;
213 }
214
215 if (reason == SETRATE_CPUFREQ) {
216 /* Increase VDD if needed. */
217 if (tgt_s->vdd_mv > strt_s->vdd_mv) {
218 rc = acpuclk_set_acpu_vdd(tgt_s);
219 if (rc < 0) {
220 pr_err("ACPU VDD increase to %d mV failed "
221 "(%d)\n", tgt_s->vdd_mv, rc);
222 goto out;
223 }
224 }
225 }
226
227 pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
228 strt_s->acpu_clk_khz, tgt_s->acpu_clk_khz);
229
230 /* Increase the AXI bus frequency if needed. This must be done before
231 * increasing the ACPU frequency, since voting for high AXI rates
232 * implicitly takes care of increasing the MSMC1 voltage, as needed. */
233 if (tgt_s->axi_clk_hz > strt_s->axi_clk_hz) {
Matt Wagantalle0eecf02011-11-08 14:07:54 -0800234 rc = clk_set_rate(drv_state.ebi1_clk, tgt_s->axi_clk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 if (rc < 0) {
236 pr_err("Setting AXI min rate failed (%d)\n", rc);
237 goto out;
238 }
239 }
240
241 /* Move off of PLL2 if we're reprogramming it */
242 if (tgt_s->src == PLL_2 && strt_s->src == PLL_2) {
243 clk_enable(acpuclk_sources[backup_s->src]);
244 acpuclk_set_src(backup_s);
245 clk_disable(acpuclk_sources[strt_s->src]);
246 }
247
248 /* Reconfigure PLL2 if we're moving to it */
249 if (tgt_s->src == PLL_2)
250 acpuclk_config_pll2(tgt_s->pll_rate);
251
252 /* Make sure target PLL is on. */
253 if ((strt_s->src != tgt_s->src && tgt_s->src >= 0) ||
254 (tgt_s->src == PLL_2 && strt_s->src == PLL_2)) {
255 pr_debug("Enabling PLL %d\n", tgt_s->src);
256 clk_enable(acpuclk_sources[tgt_s->src]);
257 }
258
259 /* Perform the frequency switch */
260 acpuclk_set_src(tgt_s);
261 drv_state.current_speed = tgt_s;
Taniya Dasbb0b6db2012-03-19 14:09:55 +0530262 loops_per_jiffy = tgt_s->lpj;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263
264 if (tgt_s->src == PLL_2 && strt_s->src == PLL_2)
265 clk_disable(acpuclk_sources[backup_s->src]);
266
267 /* Nothing else to do for SWFI. */
268 if (reason == SETRATE_SWFI)
269 goto out;
270
271 /* Turn off previous PLL if not used. */
272 if (strt_s->src != tgt_s->src && strt_s->src >= 0) {
273 pr_debug("Disabling PLL %d\n", strt_s->src);
274 clk_disable(acpuclk_sources[strt_s->src]);
275 }
276
277 /* Decrease the AXI bus frequency if we can. */
278 if (tgt_s->axi_clk_hz < strt_s->axi_clk_hz) {
Matt Wagantalle0eecf02011-11-08 14:07:54 -0800279 res = clk_set_rate(drv_state.ebi1_clk, tgt_s->axi_clk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 if (res < 0)
281 pr_warning("Setting AXI min rate failed (%d)\n", res);
282 }
283
284 /* Nothing else to do for power collapse. */
285 if (reason == SETRATE_PC)
286 goto out;
287
288 /* Drop VDD level if we can. */
289 if (tgt_s->vdd_mv < strt_s->vdd_mv) {
290 res = acpuclk_set_acpu_vdd(tgt_s);
291 if (res)
292 pr_warning("ACPU VDD decrease to %d mV failed (%d)\n",
293 tgt_s->vdd_mv, res);
294 }
295
296 pr_debug("ACPU speed change complete\n");
297out:
298 if (reason == SETRATE_CPUFREQ)
299 mutex_unlock(&drv_state.lock);
300
301 return rc;
302}
303
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700304static unsigned long acpuclk_7x30_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700305{
306 WARN_ONCE(drv_state.current_speed == NULL,
307 "acpuclk_get_rate: not initialized\n");
308 if (drv_state.current_speed)
309 return drv_state.current_speed->acpu_clk_khz;
310 else
311 return 0;
312}
313
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700314/*----------------------------------------------------------------------------
315 * Clock driver initialization
316 *---------------------------------------------------------------------------*/
317
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700318static void __init acpuclk_hw_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319{
320 struct clkctl_acpu_speed *s;
321 uint32_t div, sel, src_num;
322 uint32_t reg_clksel, reg_clkctl;
323 int res;
324 u8 pll2_l = readl_relaxed(PLL2_L_VAL_ADDR) & 0xFF;
325
326 drv_state.ebi1_clk = clk_get(NULL, "ebi1_clk");
327 BUG_ON(IS_ERR(drv_state.ebi1_clk));
328
329 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
330
331 /* Determine the ACPU clock rate. */
332 switch ((reg_clksel >> 1) & 0x3) {
333 case 0: /* Running off the output of the raw clock source mux. */
334 reg_clkctl = readl_relaxed(SCSS_CLK_CTL_ADDR);
335 src_num = reg_clksel & 0x1;
336 sel = (reg_clkctl >> (12 - (8 * src_num))) & 0x7;
337 div = (reg_clkctl >> (8 - (8 * src_num))) & 0xF;
338
339 /* Check frequency table for matching sel/div pair. */
340 for (s = acpu_freq_tbl; s->acpu_clk_khz != 0; s++) {
341 if (s->acpu_src_sel == sel && s->acpu_src_div == div)
342 break;
343 }
344 if (s->acpu_clk_khz == 0) {
345 pr_err("Error - ACPU clock reports invalid speed\n");
346 return;
347 }
348 break;
349 case 2: /* Running off of the SCPLL selected through the core mux. */
350 /* Switch to run off of the SCPLL selected through the raw
351 * clock source mux. */
352 for (s = acpu_freq_tbl; s->acpu_clk_khz != 0
353 && s->src != PLL_2 && s->acpu_src_div == 0; s++)
354 ;
355 if (s->acpu_clk_khz != 0) {
356 /* Program raw clock source mux. */
357 acpuclk_set_src(s);
358
359 /* Switch to raw clock source input of the core mux. */
360 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
361 reg_clksel &= ~(0x3 << 1);
362 writel_relaxed(reg_clksel, SCSS_CLK_SEL_ADDR);
363 break;
364 }
365 /* else fall through */
366 default:
367 pr_err("Error - ACPU clock reports invalid source\n");
368 return;
369 }
370
371 /* Look at PLL2's L val to determine what speed PLL2 is running at */
372 if (s->src == PLL_2)
373 for ( ; s->acpu_clk_khz; s++)
374 if (s->pll_rate && s->pll_rate->l == pll2_l)
375 break;
376
377 /* Set initial ACPU VDD. */
378 acpuclk_set_acpu_vdd(s);
379
380 drv_state.current_speed = s;
381
382 /* Initialize current PLL's reference count. */
383 if (s->src >= 0)
384 clk_enable(acpuclk_sources[s->src]);
385
Matt Wagantalle0eecf02011-11-08 14:07:54 -0800386 res = clk_set_rate(drv_state.ebi1_clk, s->axi_clk_hz);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 if (res < 0)
388 pr_warning("Setting AXI min rate failed!\n");
389
390 pr_info("ACPU running at %d KHz\n", s->acpu_clk_khz);
391
392 return;
393}
394
Taniya Dasbb0b6db2012-03-19 14:09:55 +0530395/* Initalize the lpj field in the acpu_freq_tbl. */
396static void __init lpj_init(void)
397{
398 int i;
399 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
400
401 for (i = 0; acpu_freq_tbl[i].acpu_clk_khz; i++) {
402 acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
403 base_clk->acpu_clk_khz,
404 acpu_freq_tbl[i].acpu_clk_khz);
405 }
406}
407
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408#ifdef CONFIG_CPU_FREQ_MSM
409static struct cpufreq_frequency_table cpufreq_tbl[ARRAY_SIZE(acpu_freq_tbl)];
410
411static void setup_cpufreq_table(void)
412{
413 unsigned i = 0;
414 const struct clkctl_acpu_speed *speed;
415
416 for (speed = acpu_freq_tbl; speed->acpu_clk_khz; speed++)
417 if (speed->use_for_scaling) {
418 cpufreq_tbl[i].index = i;
419 cpufreq_tbl[i].frequency = speed->acpu_clk_khz;
420 i++;
421 }
422 cpufreq_tbl[i].frequency = CPUFREQ_TABLE_END;
423
424 cpufreq_frequency_table_get_attr(cpufreq_tbl, smp_processor_id());
425}
426#else
427static inline void setup_cpufreq_table(void) { }
428#endif
429
430/*
431 * Truncate the frequency table at the current PLL2 rate and determine the
432 * backup PLL to use when scaling PLL2.
433 */
434void __init pll2_fixup(void)
435{
436 struct clkctl_acpu_speed *speed = acpu_freq_tbl;
437 u8 pll2_l = readl_relaxed(PLL2_L_VAL_ADDR) & 0xFF;
438
439 for ( ; speed->acpu_clk_khz; speed++) {
440 if (speed->src != PLL_2)
441 backup_s = speed;
442 if (speed->pll_rate && speed->pll_rate->l == pll2_l) {
443 speed++;
444 speed->acpu_clk_khz = 0;
445 return;
446 }
447 }
448
449 pr_err("Unknown PLL2 lval %d\n", pll2_l);
450 BUG();
451}
452
453#define RPM_BYPASS_MASK (1 << 3)
454#define PMIC_MODE_MASK (1 << 4)
455
456static void __init populate_plls(void)
457{
458 acpuclk_sources[PLL_1] = clk_get_sys("acpu", "pll1_clk");
459 BUG_ON(IS_ERR(acpuclk_sources[PLL_1]));
460 acpuclk_sources[PLL_2] = clk_get_sys("acpu", "pll2_clk");
461 BUG_ON(IS_ERR(acpuclk_sources[PLL_2]));
462 acpuclk_sources[PLL_3] = clk_get_sys("acpu", "pll3_clk");
463 BUG_ON(IS_ERR(acpuclk_sources[PLL_3]));
Stephen Boydbeb23f52012-01-25 10:09:30 -0800464 /*
465 * Prepare all the PLLs because we enable/disable them
466 * from atomic context and can't always ensure they're
467 * all prepared in non-atomic context.
468 */
469 BUG_ON(clk_prepare(acpuclk_sources[PLL_1]));
470 BUG_ON(clk_prepare(acpuclk_sources[PLL_2]));
471 BUG_ON(clk_prepare(acpuclk_sources[PLL_3]));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472}
473
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700474static struct acpuclk_data acpuclk_7x30_data = {
475 .set_rate = acpuclk_7x30_set_rate,
476 .get_rate = acpuclk_7x30_get_rate,
477 .power_collapse_khz = MAX_AXI_KHZ,
478 .wait_for_irq_khz = MAX_AXI_KHZ,
Matt Wagantallec57f062011-08-16 23:54:46 -0700479 .switch_time_us = 50,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700480};
481
Matt Wagantallec57f062011-08-16 23:54:46 -0700482static int __init acpuclk_7x30_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483{
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700484 pr_info("%s()\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700485
486 mutex_init(&drv_state.lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 pll2_fixup();
488 populate_plls();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700489 acpuclk_hw_init();
Taniya Dasbb0b6db2012-03-19 14:09:55 +0530490 lpj_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 setup_cpufreq_table();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700492 acpuclk_register(&acpuclk_7x30_data);
493
494 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495}
Matt Wagantallec57f062011-08-16 23:54:46 -0700496
497struct acpuclk_soc_data acpuclk_7x30_soc_data __initdata = {
498 .init = acpuclk_7x30_init,
499};