blob: cde5a144ad16face71a178c45b0eb5b7b9cd5f8d [file] [log] [blame]
Matt Wagantall6d9ebee2011-08-26 12:15:24 -07001/* Copyright (c) 2008-2011, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/io.h>
17#include <linux/delay.h>
18#include <linux/mutex.h>
19#include <linux/errno.h>
20#include <linux/cpufreq.h>
21#include <linux/clk.h>
Matt Wagantallec57f062011-08-16 23:54:46 -070022#include <linux/mfd/tps65023.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070023
24#include <mach/board.h>
25#include <mach/msm_iomap.h>
26
27#include "acpuclock.h"
28#include "avs.h"
29
30#define SHOT_SWITCH 4
31#define HOP_SWITCH 5
32#define SIMPLE_SLEW 6
33#define COMPLEX_SLEW 7
34
35#define SPSS_CLK_CNTL_ADDR (MSM_CSR_BASE + 0x100)
36#define SPSS_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)
37
38/* Scorpion PLL registers */
39#define SCPLL_CTL_ADDR (MSM_SCPLL_BASE + 0x4)
40#define SCPLL_STATUS_ADDR (MSM_SCPLL_BASE + 0x18)
41#define SCPLL_FSM_CTL_EXT_ADDR (MSM_SCPLL_BASE + 0x10)
42
Matt Wagantallec57f062011-08-16 23:54:46 -070043#ifdef CONFIG_QSD_SVS
44#define TPS65023_MAX_DCDC1 1600
45#else
46#define TPS65023_MAX_DCDC1 CONFIG_QSD_PMIC_DEFAULT_DCDC1
47#endif
48
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049enum {
50 ACPU_PLL_TCXO = -1,
51 ACPU_PLL_0 = 0,
52 ACPU_PLL_1,
53 ACPU_PLL_2,
54 ACPU_PLL_3,
55 ACPU_PLL_END,
56};
57
58struct clkctl_acpu_speed {
59 unsigned int use_for_scaling;
60 unsigned int acpuclk_khz;
61 int pll;
62 unsigned int acpuclk_src_sel;
63 unsigned int acpuclk_src_div;
64 unsigned int ahbclk_khz;
65 unsigned int ahbclk_div;
66 unsigned int axiclk_khz;
67 unsigned int sc_core_src_sel_mask;
68 unsigned int sc_l_value;
69 int vdd;
70 unsigned long lpj; /* loops_per_jiffy */
71};
72
73struct clkctl_acpu_speed acpu_freq_tbl_998[] = {
74 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 0, 0, 14000, 0, 0, 1000},
75 { 0, 128000, ACPU_PLL_1, 1, 5, 0, 0, 14000, 2, 0, 1000},
76 { 1, 245760, ACPU_PLL_0, 4, 0, 0, 0, 29000, 0, 0, 1000},
77 /* Update AXI_S and PLL0_S macros if above row numbers change. */
78 { 1, 384000, ACPU_PLL_3, 0, 0, 0, 0, 58000, 1, 0xA, 1000},
79 { 0, 422400, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xB, 1000},
80 { 0, 460800, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xC, 1000},
81 { 0, 499200, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xD, 1050},
82 { 0, 537600, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xE, 1050},
83 { 1, 576000, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xF, 1050},
84 { 0, 614400, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x10, 1075},
85 { 0, 652800, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x11, 1100},
86 { 0, 691200, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x12, 1125},
87 { 0, 729600, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x13, 1150},
88 { 1, 768000, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x14, 1150},
89 { 0, 806400, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x15, 1175},
90 { 0, 844800, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x16, 1225},
91 { 0, 883200, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x17, 1250},
92 { 0, 921600, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x18, 1300},
93 { 0, 960000, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x19, 1300},
94 { 1, 998400, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x1A, 1300},
95 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
96};
97
98struct clkctl_acpu_speed acpu_freq_tbl_768[] = {
99 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 0, 0, 14000, 0, 0, 1000},
100 { 0, 128000, ACPU_PLL_1, 1, 5, 0, 0, 14000, 2, 0, 1000},
101 { 1, 245760, ACPU_PLL_0, 4, 0, 0, 0, 29000, 0, 0, 1000},
102 /* Update AXI_S and PLL0_S macros if above row numbers change. */
103 { 1, 384000, ACPU_PLL_3, 0, 0, 0, 0, 58000, 1, 0xA, 1075},
104 { 0, 422400, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xB, 1100},
105 { 0, 460800, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xC, 1125},
106 { 0, 499200, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xD, 1150},
107 { 0, 537600, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xE, 1150},
108 { 1, 576000, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xF, 1150},
109 { 0, 614400, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x10, 1175},
110 { 0, 652800, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x11, 1200},
111 { 0, 691200, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x12, 1225},
112 { 0, 729600, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x13, 1250},
113 { 1, 768000, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x14, 1250},
114 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
115};
116
117static struct clkctl_acpu_speed *acpu_freq_tbl = acpu_freq_tbl_998;
118#define AXI_S (&acpu_freq_tbl[1])
119#define PLL0_S (&acpu_freq_tbl[2])
120
121/* Use 128MHz for PC since ACPU will auto-switch to AXI (128MHz) before
122 * coming back up. This allows detection of return-from-PC, since 128MHz
123 * is only used for power collapse. */
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700124#define POWER_COLLAPSE_KHZ 128000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125/* Use 245MHz (not 128MHz) for SWFI to avoid unnecessary steps between
126 * 128MHz<->245MHz. Jumping to high frequencies from 128MHz directly
127 * is not allowed. */
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700128#define WAIT_FOR_IRQ_KHZ 245760
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129
130#ifdef CONFIG_CPU_FREQ_MSM
131static struct cpufreq_frequency_table freq_table[20];
132
133static void __init cpufreq_table_init(void)
134{
135 unsigned int i;
136 unsigned int freq_cnt = 0;
137
138 /* Construct the freq_table table from acpu_freq_tbl since the
139 * freq_table values need to match frequencies specified in
140 * acpu_freq_tbl and acpu_freq_tbl needs to be fixed up during init.
141 */
142 for (i = 0; acpu_freq_tbl[i].acpuclk_khz != 0
143 && freq_cnt < ARRAY_SIZE(freq_table)-1; i++) {
144 if (acpu_freq_tbl[i].use_for_scaling) {
145 freq_table[freq_cnt].index = freq_cnt;
146 freq_table[freq_cnt].frequency
147 = acpu_freq_tbl[i].acpuclk_khz;
148 freq_cnt++;
149 }
150 }
151
152 /* freq_table not big enough to store all usable freqs. */
153 BUG_ON(acpu_freq_tbl[i].acpuclk_khz != 0);
154
155 freq_table[freq_cnt].index = freq_cnt;
156 freq_table[freq_cnt].frequency = CPUFREQ_TABLE_END;
157
158 pr_info("%d scaling frequencies supported.\n", freq_cnt);
159}
160#endif
161
162struct clock_state {
163 struct clkctl_acpu_speed *current_speed;
164 struct mutex lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165 struct clk *ebi1_clk;
166 int (*acpu_set_vdd) (int mvolts);
167};
168
169static struct clock_state drv_state = { 0 };
170
171static void scpll_set_freq(uint32_t lval, unsigned freq_switch)
172{
173 uint32_t regval;
174
175 if (lval > 33)
176 lval = 33;
177 if (lval < 10)
178 lval = 10;
179
180 /* wait for any calibrations or frequency switches to finish */
181 while (readl(SCPLL_STATUS_ADDR) & 0x3)
182 ;
183
184 /* write the new L val and switch mode */
185 regval = readl(SCPLL_FSM_CTL_EXT_ADDR);
186 regval &= ~(0x3f << 3);
187 regval |= (lval << 3);
188 if (freq_switch == SIMPLE_SLEW)
189 regval |= (0x1 << 9);
190
191 regval &= ~(0x3 << 0);
192 regval |= (freq_switch << 0);
193 writel(regval, SCPLL_FSM_CTL_EXT_ADDR);
194
195 dmb();
196
197 /* put in normal mode */
198 regval = readl(SCPLL_CTL_ADDR);
199 regval |= 0x7;
200 writel(regval, SCPLL_CTL_ADDR);
201
202 dmb();
203
204 /* wait for frequency switch to finish */
205 while (readl(SCPLL_STATUS_ADDR) & 0x1)
206 ;
207
208 /* status bit seems to clear early, using
209 * 100us to handle the worst case. */
210 udelay(100);
211}
212
213static void scpll_apps_enable(bool state)
214{
215 uint32_t regval;
216
217 if (state)
218 pr_debug("Enabling PLL 3\n");
219 else
220 pr_debug("Disabling PLL 3\n");
221
222 /* Wait for any frequency switches to finish. */
223 while (readl(SCPLL_STATUS_ADDR) & 0x1)
224 ;
225
226 /* put the pll in standby mode */
227 regval = readl(SCPLL_CTL_ADDR);
228 regval &= ~(0x7);
229 regval |= (0x2);
230 writel(regval, SCPLL_CTL_ADDR);
231
232 dmb();
233
234 if (state) {
235 /* put the pll in normal mode */
236 regval = readl(SCPLL_CTL_ADDR);
237 regval |= (0x7);
238 writel(regval, SCPLL_CTL_ADDR);
239 udelay(200);
240 } else {
241 /* put the pll in power down mode */
242 regval = readl(SCPLL_CTL_ADDR);
243 regval &= ~(0x7);
244 writel(regval, SCPLL_CTL_ADDR);
245 }
Matt Wagantallec57f062011-08-16 23:54:46 -0700246 udelay(62);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700247
248 if (state)
249 pr_debug("PLL 3 Enabled\n");
250 else
251 pr_debug("PLL 3 Disabled\n");
252}
253
254static void scpll_init(void)
255{
256 uint32_t regval;
257#define L_VAL_384MHZ 0xA
258#define L_VAL_768MHZ 0x14
259
260 pr_debug("Initializing PLL 3\n");
261
262 /* power down scpll */
263 writel(0x0, SCPLL_CTL_ADDR);
264
265 dmb();
266
267 /* set bypassnl, put into standby */
268 writel(0x00400002, SCPLL_CTL_ADDR);
269
270 /* set bypassnl, reset_n, full calibration */
271 writel(0x00600004, SCPLL_CTL_ADDR);
272
273 /* Ensure register write to initiate calibration has taken
274 effect before reading status flag */
275 dmb();
276
277 /* wait for cal_all_done */
278 while (readl(SCPLL_STATUS_ADDR) & 0x2)
279 ;
280
281 /* Start: Set of experimentally derived steps
282 * to work around a h/w bug. */
283
284 /* Put the pll in normal mode */
285 scpll_apps_enable(1);
286
287 /* SHOT switch to 384 MHz */
288 regval = readl(SCPLL_FSM_CTL_EXT_ADDR);
289 regval &= ~(0x3f << 3);
290 regval |= (L_VAL_384MHZ << 3);
291
292 regval &= ~0x7;
293 regval |= SHOT_SWITCH;
294 writel(regval, SCPLL_FSM_CTL_EXT_ADDR);
295
296 /* Trigger the freq switch by putting pll in normal mode. */
297 regval = readl(SCPLL_CTL_ADDR);
298 regval |= (0x7);
299 writel(regval, SCPLL_CTL_ADDR);
300
301 /* Wait for frequency switch to finish */
302 while (readl(SCPLL_STATUS_ADDR) & 0x1)
303 ;
304
305 /* Status bit seems to clear early, using
306 * 800 microseconds for the worst case. */
307 udelay(800);
308
309 /* HOP switch to 768 MHz. */
310 regval = readl(SCPLL_FSM_CTL_EXT_ADDR);
311 regval &= ~(0x3f << 3);
312 regval |= (L_VAL_768MHZ << 3);
313
314 regval &= ~0x7;
315 regval |= HOP_SWITCH;
316 writel(regval, SCPLL_FSM_CTL_EXT_ADDR);
317
318 /* Trigger the freq switch by putting pll in normal mode. */
319 regval = readl(SCPLL_CTL_ADDR);
320 regval |= (0x7);
321 writel(regval, SCPLL_CTL_ADDR);
322
323 /* Wait for frequency switch to finish */
324 while (readl(SCPLL_STATUS_ADDR) & 0x1)
325 ;
326
327 /* Status bit seems to clear early, using
328 * 100 microseconds for the worst case. */
329 udelay(100);
330
331 /* End: Work around for h/w bug */
332
333 /* Power down scpll */
334 scpll_apps_enable(0);
335}
336
337static void config_pll(struct clkctl_acpu_speed *s)
338{
339 uint32_t regval;
340
341 if (s->pll == ACPU_PLL_3)
342 scpll_set_freq(s->sc_l_value, HOP_SWITCH);
343 /* Configure the PLL divider mux if we plan to use it. */
344 else if (s->sc_core_src_sel_mask == 0) {
345 /* get the current clock source selection */
346 regval = readl(SPSS_CLK_SEL_ADDR) & 0x1;
347
348 /* configure the other clock source, then switch to it,
349 * using the glitch free mux */
350 switch (regval) {
351 case 0x0:
352 regval = readl(SPSS_CLK_CNTL_ADDR);
353 regval &= ~(0x7 << 4 | 0xf);
354 regval |= (s->acpuclk_src_sel << 4);
355 regval |= (s->acpuclk_src_div << 0);
356 writel(regval, SPSS_CLK_CNTL_ADDR);
357
358 regval = readl(SPSS_CLK_SEL_ADDR);
359 regval |= 0x1;
360 writel(regval, SPSS_CLK_SEL_ADDR);
361 break;
362
363 case 0x1:
364 regval = readl(SPSS_CLK_CNTL_ADDR);
365 regval &= ~(0x7 << 12 | 0xf << 8);
366 regval |= (s->acpuclk_src_sel << 12);
367 regval |= (s->acpuclk_src_div << 8);
368 writel(regval, SPSS_CLK_CNTL_ADDR);
369
370 regval = readl(SPSS_CLK_SEL_ADDR);
371 regval &= ~0x1;
372 writel(regval, SPSS_CLK_SEL_ADDR);
373 break;
374 }
375 dmb();
376 }
377
378 regval = readl(SPSS_CLK_SEL_ADDR);
379 regval &= ~(0x3 << 1);
380 regval |= (s->sc_core_src_sel_mask << 1);
381 writel(regval, SPSS_CLK_SEL_ADDR);
382}
383
384static int acpuclk_set_vdd_level(int vdd)
385{
386 if (drv_state.acpu_set_vdd) {
387 pr_debug("Switching VDD to %d mV\n", vdd);
388 return drv_state.acpu_set_vdd(vdd);
389 } else {
390 /* Assume that the PMIC supports scaling the processor
391 * to its maximum frequency at its default voltage.
392 */
393 return 0;
394 }
395}
396
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700397static int acpuclk_8x50_set_rate(int cpu, unsigned long rate,
398 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399{
400 struct clkctl_acpu_speed *tgt_s, *strt_s;
401 int res, rc = 0;
402 int freq_index = 0;
403
404 if (reason == SETRATE_CPUFREQ)
405 mutex_lock(&drv_state.lock);
406
407 strt_s = drv_state.current_speed;
408
409 if (rate == strt_s->acpuclk_khz)
410 goto out;
411
412 for (tgt_s = acpu_freq_tbl; tgt_s->acpuclk_khz != 0; tgt_s++) {
413 if (tgt_s->acpuclk_khz == rate)
414 break;
415 freq_index++;
416 }
417
418 if (tgt_s->acpuclk_khz == 0) {
419 rc = -EINVAL;
420 goto out;
421 }
422
423 if (reason == SETRATE_CPUFREQ) {
424#ifdef CONFIG_MSM_CPU_AVS
425 /* Notify avs before changing frequency */
426 rc = avs_adjust_freq(freq_index, 1);
427 if (rc) {
428 pr_err("Unable to increase ACPU vdd (%d)\n", rc);
429 goto out;
430 }
431#endif
432 /* Increase VDD if needed. */
433 if (tgt_s->vdd > strt_s->vdd) {
434 rc = acpuclk_set_vdd_level(tgt_s->vdd);
435 if (rc) {
436 pr_err("Unable to increase ACPU vdd (%d)\n",
437 rc);
438 goto out;
439 }
440 }
441 } else if (reason == SETRATE_PC
442 && rate != POWER_COLLAPSE_KHZ) {
443 /* Returning from PC. ACPU is running on AXI source.
444 * Step up to PLL0 before ramping up higher. */
445 config_pll(PLL0_S);
446 }
447
448 pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
449 strt_s->acpuclk_khz, tgt_s->acpuclk_khz);
450
451 if (strt_s->pll != ACPU_PLL_3 && tgt_s->pll != ACPU_PLL_3) {
452 config_pll(tgt_s);
453 } else if (strt_s->pll != ACPU_PLL_3 && tgt_s->pll == ACPU_PLL_3) {
454 scpll_apps_enable(1);
455 config_pll(tgt_s);
456 } else if (strt_s->pll == ACPU_PLL_3 && tgt_s->pll != ACPU_PLL_3) {
457 config_pll(tgt_s);
458 scpll_apps_enable(0);
459 } else {
460 /* Temporarily switch to PLL0 while reconfiguring PLL3. */
461 config_pll(PLL0_S);
462 config_pll(tgt_s);
463 }
464
465 /* Update the driver state with the new clock freq */
466 drv_state.current_speed = tgt_s;
467
468 /* Re-adjust lpj for the new clock speed. */
469 loops_per_jiffy = tgt_s->lpj;
470
471 /* Nothing else to do for SWFI. */
472 if (reason == SETRATE_SWFI)
473 goto out;
474
475 if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {
476 res = clk_set_rate(drv_state.ebi1_clk,
477 tgt_s->axiclk_khz * 1000);
478 if (res < 0)
479 pr_warning("Setting AXI min rate failed (%d)\n", res);
480 }
481
482 /* Nothing else to do for power collapse */
483 if (reason == SETRATE_PC)
484 goto out;
485
486#ifdef CONFIG_MSM_CPU_AVS
487 /* notify avs after changing frequency */
488 res = avs_adjust_freq(freq_index, 0);
489 if (res)
490 pr_warning("Unable to drop ACPU vdd (%d)\n", res);
491#endif
492
493 /* Drop VDD level if we can. */
494 if (tgt_s->vdd < strt_s->vdd) {
495 res = acpuclk_set_vdd_level(tgt_s->vdd);
496 if (res)
497 pr_warning("Unable to drop ACPU vdd (%d)\n", res);
498 }
499
500 pr_debug("ACPU speed change complete\n");
501out:
502 if (reason == SETRATE_CPUFREQ)
503 mutex_unlock(&drv_state.lock);
504 return rc;
505}
506
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700507static void __init acpuclk_hw_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700508{
509 struct clkctl_acpu_speed *speed;
510 uint32_t div, sel, regval;
511 int res;
512
513 /* Determine the source of the Scorpion clock. */
514 regval = readl(SPSS_CLK_SEL_ADDR);
515 switch ((regval & 0x6) >> 1) {
516 case 0: /* raw source clock */
517 case 3: /* low jitter PLL1 (768Mhz) */
518 if (regval & 0x1) {
519 sel = ((readl(SPSS_CLK_CNTL_ADDR) >> 4) & 0x7);
520 div = ((readl(SPSS_CLK_CNTL_ADDR) >> 0) & 0xf);
521 } else {
522 sel = ((readl(SPSS_CLK_CNTL_ADDR) >> 12) & 0x7);
523 div = ((readl(SPSS_CLK_CNTL_ADDR) >> 8) & 0xf);
524 }
525
526 /* Find the matching clock rate. */
527 for (speed = acpu_freq_tbl; speed->acpuclk_khz != 0; speed++) {
528 if (speed->acpuclk_src_sel == sel &&
529 speed->acpuclk_src_div == div)
530 break;
531 }
532 break;
533
534 case 1: /* unbuffered scorpion pll (384Mhz to 998.4Mhz) */
535 sel = ((readl(SCPLL_FSM_CTL_EXT_ADDR) >> 3) & 0x3f);
536
537 /* Find the matching clock rate. */
538 for (speed = acpu_freq_tbl; speed->acpuclk_khz != 0; speed++) {
539 if (speed->sc_l_value == sel &&
540 speed->sc_core_src_sel_mask == 1)
541 break;
542 }
543 break;
544
545 case 2: /* AXI bus clock (128Mhz) */
546 speed = AXI_S;
547 break;
548 default:
549 BUG();
550 }
551
552 /* Initialize scpll only if it wasn't already initialized by the boot
553 * loader. If the CPU is already running on scpll, then the scpll was
554 * initialized by the boot loader. */
555 if (speed->pll != ACPU_PLL_3)
556 scpll_init();
557
558 if (speed->acpuclk_khz == 0) {
559 pr_err("Error - ACPU clock reports invalid speed\n");
560 return;
561 }
562
563 drv_state.current_speed = speed;
564 res = clk_set_rate(drv_state.ebi1_clk, speed->axiclk_khz * 1000);
565 if (res < 0)
566 pr_warning("Setting AXI min rate failed (%d)\n", res);
567 res = clk_enable(drv_state.ebi1_clk);
568 if (res < 0)
569 pr_warning("Enabling AXI clock failed (%d)\n", res);
570
571 pr_info("ACPU running at %d KHz\n", speed->acpuclk_khz);
572}
573
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700574static unsigned long acpuclk_8x50_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575{
576 return drv_state.current_speed->acpuclk_khz;
577}
578
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700579/* Spare register populated with efuse data on max ACPU freq. */
580#define CT_CSR_PHYS 0xA8700000
581#define TCSR_SPARE2_ADDR (ct_csr_base + 0x60)
582
583#define PLL0_M_VAL_ADDR (MSM_CLK_CTL_BASE + 0x308)
584
585static void __init acpu_freq_tbl_fixup(void)
586{
587 void __iomem *ct_csr_base;
588 uint32_t tcsr_spare2, pll0_m_val;
589 unsigned int max_acpu_khz;
590 unsigned int i;
591
592 ct_csr_base = ioremap(CT_CSR_PHYS, PAGE_SIZE);
593 BUG_ON(ct_csr_base == NULL);
594
595 tcsr_spare2 = readl(TCSR_SPARE2_ADDR);
596
597 /* Check if the register is supported and meaningful. */
598 if ((tcsr_spare2 & 0xF000) != 0xA000) {
599 pr_info("Efuse data on Max ACPU freq not present.\n");
600 goto skip_efuse_fixup;
601 }
602
603 switch (tcsr_spare2 & 0xF0) {
604 case 0x70:
605 acpu_freq_tbl = acpu_freq_tbl_768;
606 max_acpu_khz = 768000;
607 break;
608 case 0x30:
609 case 0x00:
610 max_acpu_khz = 998400;
611 break;
612 case 0x10:
613 max_acpu_khz = 1267200;
614 break;
615 default:
616 pr_warning("Invalid efuse data (%x) on Max ACPU freq!\n",
617 tcsr_spare2);
618 goto skip_efuse_fixup;
619 }
620
621 pr_info("Max ACPU freq from efuse data is %d KHz\n", max_acpu_khz);
622
623 for (i = 0; acpu_freq_tbl[i].acpuclk_khz != 0; i++) {
624 if (acpu_freq_tbl[i].acpuclk_khz > max_acpu_khz) {
625 acpu_freq_tbl[i].acpuclk_khz = 0;
626 break;
627 }
628 }
629
630skip_efuse_fixup:
631 iounmap(ct_csr_base);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632
633 /* pll0_m_val will be 36 when PLL0 is run at 235MHz
634 * instead of the usual 245MHz. */
635 pll0_m_val = readl(PLL0_M_VAL_ADDR) & 0x7FFFF;
636 if (pll0_m_val == 36)
637 PLL0_S->acpuclk_khz = 235930;
638
639 for (i = 0; acpu_freq_tbl[i].acpuclk_khz != 0; i++) {
Matt Wagantallec57f062011-08-16 23:54:46 -0700640 if (acpu_freq_tbl[i].vdd > TPS65023_MAX_DCDC1) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641 acpu_freq_tbl[i].acpuclk_khz = 0;
642 break;
643 }
644 }
645}
646
647/* Initalize the lpj field in the acpu_freq_tbl. */
648static void __init lpj_init(void)
649{
650 int i;
651 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
652 for (i = 0; acpu_freq_tbl[i].acpuclk_khz; i++) {
653 acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
654 base_clk->acpuclk_khz,
655 acpu_freq_tbl[i].acpuclk_khz);
656 }
657}
658
659#ifdef CONFIG_MSM_CPU_AVS
660static int __init acpu_avs_init(int (*set_vdd) (int), int khz)
661{
662 int i;
663 int freq_count = 0;
664 int freq_index = -1;
665
666 for (i = 0; acpu_freq_tbl[i].acpuclk_khz; i++) {
667 freq_count++;
668 if (acpu_freq_tbl[i].acpuclk_khz == khz)
669 freq_index = i;
670 }
671
672 return avs_init(set_vdd, freq_count, freq_index);
673}
674#endif
675
Matt Wagantallec57f062011-08-16 23:54:46 -0700676static int qsd8x50_tps65023_set_dcdc1(int mVolts)
677{
678 int rc = 0;
679#ifdef CONFIG_QSD_SVS
680 rc = tps65023_set_dcdc1_level(mVolts);
681 /*
682 * By default the TPS65023 will be initialized to 1.225V.
683 * So we can safely switch to any frequency within this
684 * voltage even if the device is not probed/ready.
685 */
686 if (rc == -ENODEV && mVolts <= CONFIG_QSD_PMIC_DEFAULT_DCDC1)
687 rc = 0;
688#else
689 /*
690 * Disallow frequencies not supported in the default PMIC
691 * output voltage.
692 */
693 if (mVolts > CONFIG_QSD_PMIC_DEFAULT_DCDC1)
694 rc = -EFAULT;
695#endif
696 return rc;
697}
698
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700699static struct acpuclk_data acpuclk_8x50_data = {
700 .set_rate = acpuclk_8x50_set_rate,
701 .get_rate = acpuclk_8x50_get_rate,
702 .power_collapse_khz = POWER_COLLAPSE_KHZ,
703 .wait_for_irq_khz = WAIT_FOR_IRQ_KHZ,
Matt Wagantallec57f062011-08-16 23:54:46 -0700704 .switch_time_us = 20,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700705};
706
Matt Wagantallec57f062011-08-16 23:54:46 -0700707static int __init acpuclk_8x50_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708{
709 mutex_init(&drv_state.lock);
Matt Wagantallec57f062011-08-16 23:54:46 -0700710 drv_state.acpu_set_vdd = qsd8x50_tps65023_set_dcdc1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711
712 drv_state.ebi1_clk = clk_get(NULL, "ebi1_acpu_clk");
713 BUG_ON(IS_ERR(drv_state.ebi1_clk));
714
715 acpu_freq_tbl_fixup();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700716 acpuclk_hw_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717 lpj_init();
718 /* Set a lower bound for ACPU rate for boot. This limits the
719 * maximum frequency hop caused by the first CPUFREQ switch. */
720 if (drv_state.current_speed->acpuclk_khz < PLL0_S->acpuclk_khz)
721 acpuclk_set_rate(0, PLL0_S->acpuclk_khz, SETRATE_CPUFREQ);
722
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700723 acpuclk_register(&acpuclk_8x50_data);
724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725#ifdef CONFIG_CPU_FREQ_MSM
726 cpufreq_table_init();
727 cpufreq_frequency_table_get_attr(freq_table, smp_processor_id());
728#endif
729#ifdef CONFIG_MSM_CPU_AVS
730 if (!acpu_avs_init(drv_state.acpu_set_vdd,
731 drv_state.current_speed->acpuclk_khz)) {
732 /* avs init successful. avs will handle voltage changes */
733 drv_state.acpu_set_vdd = NULL;
734 }
735#endif
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700736 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737}
Matt Wagantallec57f062011-08-16 23:54:46 -0700738
739struct acpuclk_soc_data acpuclk_8x50_soc_data __initdata = {
740 .init = acpuclk_8x50_init,
741};