blob: 24c910e4f2b5fbe68db7c14575f9a2a8a8cb688a [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 *
3 * Copyright (C) 2007 Google, Inc.
4 * Copyright (c) 2007-2011, Code Aurora Forum. All rights reserved.
5 *
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
37#define SCSS_CLK_CTL_ADDR (MSM_ACC_BASE + 0x04)
38#define SCSS_CLK_SEL_ADDR (MSM_ACC_BASE + 0x08)
39
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;
79 unsigned long lpj; /* loops_per_jiffy */
80};
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) {
234 rc = clk_set_min_rate(drv_state.ebi1_clk,
235 tgt_s->axi_clk_hz);
236 if (rc < 0) {
237 pr_err("Setting AXI min rate failed (%d)\n", rc);
238 goto out;
239 }
240 }
241
242 /* Move off of PLL2 if we're reprogramming it */
243 if (tgt_s->src == PLL_2 && strt_s->src == PLL_2) {
244 clk_enable(acpuclk_sources[backup_s->src]);
245 acpuclk_set_src(backup_s);
246 clk_disable(acpuclk_sources[strt_s->src]);
247 }
248
249 /* Reconfigure PLL2 if we're moving to it */
250 if (tgt_s->src == PLL_2)
251 acpuclk_config_pll2(tgt_s->pll_rate);
252
253 /* Make sure target PLL is on. */
254 if ((strt_s->src != tgt_s->src && tgt_s->src >= 0) ||
255 (tgt_s->src == PLL_2 && strt_s->src == PLL_2)) {
256 pr_debug("Enabling PLL %d\n", tgt_s->src);
257 clk_enable(acpuclk_sources[tgt_s->src]);
258 }
259
260 /* Perform the frequency switch */
261 acpuclk_set_src(tgt_s);
262 drv_state.current_speed = tgt_s;
263 loops_per_jiffy = tgt_s->lpj;
264
265 if (tgt_s->src == PLL_2 && strt_s->src == PLL_2)
266 clk_disable(acpuclk_sources[backup_s->src]);
267
268 /* Nothing else to do for SWFI. */
269 if (reason == SETRATE_SWFI)
270 goto out;
271
272 /* Turn off previous PLL if not used. */
273 if (strt_s->src != tgt_s->src && strt_s->src >= 0) {
274 pr_debug("Disabling PLL %d\n", strt_s->src);
275 clk_disable(acpuclk_sources[strt_s->src]);
276 }
277
278 /* Decrease the AXI bus frequency if we can. */
279 if (tgt_s->axi_clk_hz < strt_s->axi_clk_hz) {
280 res = clk_set_min_rate(drv_state.ebi1_clk,
281 tgt_s->axi_clk_hz);
282 if (res < 0)
283 pr_warning("Setting AXI min rate failed (%d)\n", res);
284 }
285
286 /* Nothing else to do for power collapse. */
287 if (reason == SETRATE_PC)
288 goto out;
289
290 /* Drop VDD level if we can. */
291 if (tgt_s->vdd_mv < strt_s->vdd_mv) {
292 res = acpuclk_set_acpu_vdd(tgt_s);
293 if (res)
294 pr_warning("ACPU VDD decrease to %d mV failed (%d)\n",
295 tgt_s->vdd_mv, res);
296 }
297
298 pr_debug("ACPU speed change complete\n");
299out:
300 if (reason == SETRATE_CPUFREQ)
301 mutex_unlock(&drv_state.lock);
302
303 return rc;
304}
305
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700306static unsigned long acpuclk_7x30_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307{
308 WARN_ONCE(drv_state.current_speed == NULL,
309 "acpuclk_get_rate: not initialized\n");
310 if (drv_state.current_speed)
311 return drv_state.current_speed->acpu_clk_khz;
312 else
313 return 0;
314}
315
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700316/*----------------------------------------------------------------------------
317 * Clock driver initialization
318 *---------------------------------------------------------------------------*/
319
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700320static void __init acpuclk_hw_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700321{
322 struct clkctl_acpu_speed *s;
323 uint32_t div, sel, src_num;
324 uint32_t reg_clksel, reg_clkctl;
325 int res;
326 u8 pll2_l = readl_relaxed(PLL2_L_VAL_ADDR) & 0xFF;
327
328 drv_state.ebi1_clk = clk_get(NULL, "ebi1_clk");
329 BUG_ON(IS_ERR(drv_state.ebi1_clk));
330
331 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
332
333 /* Determine the ACPU clock rate. */
334 switch ((reg_clksel >> 1) & 0x3) {
335 case 0: /* Running off the output of the raw clock source mux. */
336 reg_clkctl = readl_relaxed(SCSS_CLK_CTL_ADDR);
337 src_num = reg_clksel & 0x1;
338 sel = (reg_clkctl >> (12 - (8 * src_num))) & 0x7;
339 div = (reg_clkctl >> (8 - (8 * src_num))) & 0xF;
340
341 /* Check frequency table for matching sel/div pair. */
342 for (s = acpu_freq_tbl; s->acpu_clk_khz != 0; s++) {
343 if (s->acpu_src_sel == sel && s->acpu_src_div == div)
344 break;
345 }
346 if (s->acpu_clk_khz == 0) {
347 pr_err("Error - ACPU clock reports invalid speed\n");
348 return;
349 }
350 break;
351 case 2: /* Running off of the SCPLL selected through the core mux. */
352 /* Switch to run off of the SCPLL selected through the raw
353 * clock source mux. */
354 for (s = acpu_freq_tbl; s->acpu_clk_khz != 0
355 && s->src != PLL_2 && s->acpu_src_div == 0; s++)
356 ;
357 if (s->acpu_clk_khz != 0) {
358 /* Program raw clock source mux. */
359 acpuclk_set_src(s);
360
361 /* Switch to raw clock source input of the core mux. */
362 reg_clksel = readl_relaxed(SCSS_CLK_SEL_ADDR);
363 reg_clksel &= ~(0x3 << 1);
364 writel_relaxed(reg_clksel, SCSS_CLK_SEL_ADDR);
365 break;
366 }
367 /* else fall through */
368 default:
369 pr_err("Error - ACPU clock reports invalid source\n");
370 return;
371 }
372
373 /* Look at PLL2's L val to determine what speed PLL2 is running at */
374 if (s->src == PLL_2)
375 for ( ; s->acpu_clk_khz; s++)
376 if (s->pll_rate && s->pll_rate->l == pll2_l)
377 break;
378
379 /* Set initial ACPU VDD. */
380 acpuclk_set_acpu_vdd(s);
381
382 drv_state.current_speed = s;
383
384 /* Initialize current PLL's reference count. */
385 if (s->src >= 0)
386 clk_enable(acpuclk_sources[s->src]);
387
388 res = clk_set_min_rate(drv_state.ebi1_clk, s->axi_clk_hz);
389 if (res < 0)
390 pr_warning("Setting AXI min rate failed!\n");
391
392 pr_info("ACPU running at %d KHz\n", s->acpu_clk_khz);
393
394 return;
395}
396
397/* Initalize the lpj field in the acpu_freq_tbl. */
398static void __init lpj_init(void)
399{
400 int i;
401 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
402
403 for (i = 0; acpu_freq_tbl[i].acpu_clk_khz; i++) {
404 acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
405 base_clk->acpu_clk_khz,
406 acpu_freq_tbl[i].acpu_clk_khz);
407 }
408}
409
410#ifdef CONFIG_CPU_FREQ_MSM
411static struct cpufreq_frequency_table cpufreq_tbl[ARRAY_SIZE(acpu_freq_tbl)];
412
413static void setup_cpufreq_table(void)
414{
415 unsigned i = 0;
416 const struct clkctl_acpu_speed *speed;
417
418 for (speed = acpu_freq_tbl; speed->acpu_clk_khz; speed++)
419 if (speed->use_for_scaling) {
420 cpufreq_tbl[i].index = i;
421 cpufreq_tbl[i].frequency = speed->acpu_clk_khz;
422 i++;
423 }
424 cpufreq_tbl[i].frequency = CPUFREQ_TABLE_END;
425
426 cpufreq_frequency_table_get_attr(cpufreq_tbl, smp_processor_id());
427}
428#else
429static inline void setup_cpufreq_table(void) { }
430#endif
431
432/*
433 * Truncate the frequency table at the current PLL2 rate and determine the
434 * backup PLL to use when scaling PLL2.
435 */
436void __init pll2_fixup(void)
437{
438 struct clkctl_acpu_speed *speed = acpu_freq_tbl;
439 u8 pll2_l = readl_relaxed(PLL2_L_VAL_ADDR) & 0xFF;
440
441 for ( ; speed->acpu_clk_khz; speed++) {
442 if (speed->src != PLL_2)
443 backup_s = speed;
444 if (speed->pll_rate && speed->pll_rate->l == pll2_l) {
445 speed++;
446 speed->acpu_clk_khz = 0;
447 return;
448 }
449 }
450
451 pr_err("Unknown PLL2 lval %d\n", pll2_l);
452 BUG();
453}
454
455#define RPM_BYPASS_MASK (1 << 3)
456#define PMIC_MODE_MASK (1 << 4)
457
458static void __init populate_plls(void)
459{
460 acpuclk_sources[PLL_1] = clk_get_sys("acpu", "pll1_clk");
461 BUG_ON(IS_ERR(acpuclk_sources[PLL_1]));
462 acpuclk_sources[PLL_2] = clk_get_sys("acpu", "pll2_clk");
463 BUG_ON(IS_ERR(acpuclk_sources[PLL_2]));
464 acpuclk_sources[PLL_3] = clk_get_sys("acpu", "pll3_clk");
465 BUG_ON(IS_ERR(acpuclk_sources[PLL_3]));
466}
467
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700468static struct acpuclk_data acpuclk_7x30_data = {
469 .set_rate = acpuclk_7x30_set_rate,
470 .get_rate = acpuclk_7x30_get_rate,
471 .power_collapse_khz = MAX_AXI_KHZ,
472 .wait_for_irq_khz = MAX_AXI_KHZ,
Matt Wagantallec57f062011-08-16 23:54:46 -0700473 .switch_time_us = 50,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700474};
475
Matt Wagantallec57f062011-08-16 23:54:46 -0700476static int __init acpuclk_7x30_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477{
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700478 pr_info("%s()\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700479
480 mutex_init(&drv_state.lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481 pll2_fixup();
482 populate_plls();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700483 acpuclk_hw_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484 lpj_init();
485 setup_cpufreq_table();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700486 acpuclk_register(&acpuclk_7x30_data);
487
488 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700489}
Matt Wagantallec57f062011-08-16 23:54:46 -0700490
491struct acpuclk_soc_data acpuclk_7x30_soc_data __initdata = {
492 .init = acpuclk_7x30_init,
493};