blob: ae4b08458154aaea53d43a120759b66781579467 [file] [log] [blame]
Matt Wagantalld1af38e2011-08-06 01:38:02 -07001/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 * MSM architecture clock driver
3 *
4 * Copyright (C) 2007 Google, Inc.
Pankaj Kumarc9136b32012-01-02 18:46:13 +05305 * Copyright (c) 2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 * Author: San Mehat <san@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/version.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/delay.h>
25#include <linux/clk.h>
26#include <linux/cpufreq.h>
27#include <linux/mutex.h>
28#include <linux/io.h>
29#include <linux/sort.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#include <mach/board.h>
31#include <mach/msm_iomap.h>
32#include <asm/mach-types.h>
33#include <mach/socinfo.h>
34
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035#include "smd_private.h"
36#include "acpuclock.h"
37
38#define A11S_CLK_CNTL_ADDR (MSM_CSR_BASE + 0x100)
39#define A11S_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)
40#define A11S_VDD_SVS_PLEVEL_ADDR (MSM_CSR_BASE + 0x124)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042
Matt Wagantall6d9ebee2011-08-26 12:15:24 -070043#define POWER_COLLAPSE_KHZ 19200
44
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045/* Max CPU frequency allowed by hardware while in standby waiting for an irq. */
46#define MAX_WAIT_FOR_IRQ_KHZ 128000
47
Pankaj Kumar3912c982011-12-07 16:59:03 +053048/**
49 * enum - For acpuclock PLL IDs
50 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051enum {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052 ACPU_PLL_0 = 0,
53 ACPU_PLL_1,
54 ACPU_PLL_2,
55 ACPU_PLL_3,
56 ACPU_PLL_4,
Pankaj Kumar0249bed2012-03-08 15:20:54 +053057 ACPU_PLL_TCXO,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058 ACPU_PLL_END,
59};
60
Pankaj Kumar3912c982011-12-07 16:59:03 +053061struct acpu_clk_src {
62 struct clk *clk;
63 const char *name;
64};
65
66static struct acpu_clk_src pll_clk[ACPU_PLL_END] = {
67 [ACPU_PLL_0] = { .name = "pll0_clk" },
68 [ACPU_PLL_1] = { .name = "pll1_clk" },
69 [ACPU_PLL_2] = { .name = "pll2_clk" },
70 [ACPU_PLL_4] = { .name = "pll4_clk" },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071};
72
73struct clock_state {
74 struct clkctl_acpu_speed *current_speed;
75 struct mutex lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076 uint32_t max_speed_delta_khz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077 struct clk *ebi1_clk;
78};
79
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080struct clkctl_acpu_speed {
81 unsigned int use_for_scaling;
82 unsigned int a11clk_khz;
83 int pll;
84 unsigned int a11clk_src_sel;
85 unsigned int a11clk_src_div;
86 unsigned int ahbclk_khz;
87 unsigned int ahbclk_div;
88 int vdd;
89 unsigned int axiclk_khz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090 /* Pointers in acpu_freq_tbl[] for max up/down steppings. */
91 struct clkctl_acpu_speed *down[ACPU_PLL_END];
92 struct clkctl_acpu_speed *up[ACPU_PLL_END];
93};
94
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095static struct clock_state drv_state = { 0 };
96static struct clkctl_acpu_speed *acpu_freq_tbl;
97
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070098/*
99 * ACPU freq tables used for different PLLs frequency combinations. The
100 * correct table is selected during init.
101 *
102 * Table stepping up/down entries are calculated during boot to choose the
103 * largest frequency jump that's less than max_speed_delta_khz on each PLL.
104 */
105
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530106/* 7627 with GSM capable modem */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_0[] = {
108 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 30720 },
109 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 61440 },
110 { 1, 122880, ACPU_PLL_1, 1, 1, 61440, 1, 3, 61440 },
111 { 0, 200000, ACPU_PLL_2, 2, 5, 66667, 2, 4, 61440 },
112 { 1, 245760, ACPU_PLL_1, 1, 0, 122880, 1, 4, 61440 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530113 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
114 { 0, 400000, ACPU_PLL_2, 2, 2, 133333, 2, 5, 160000 },
115 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
116 { 1, 600000, ACPU_PLL_2, 2, 1, 200000, 2, 7, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530117 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118};
119
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530120/* 7627 with CDMA capable modem */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_0[] = {
122 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 24576 },
123 { 1, 98304, ACPU_PLL_1, 1, 1, 98304, 0, 3, 49152 },
124 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 49152 },
125 { 1, 196608, ACPU_PLL_1, 1, 0, 65536, 2, 4, 98304 },
126 { 0, 200000, ACPU_PLL_2, 2, 5, 66667, 2, 4, 98304 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530127 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
128 { 0, 400000, ACPU_PLL_2, 2, 2, 133333, 2, 5, 160000 },
129 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
130 { 1, 600000, ACPU_PLL_2, 2, 1, 200000, 2, 7, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530131 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132};
133
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530134/* 7627 with GSM capable modem - PLL2 @ 800 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_800_pll4_0[] = {
136 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 30720 },
137 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 61440 },
138 { 1, 122880, ACPU_PLL_1, 1, 1, 61440, 1, 3, 61440 },
139 { 0, 200000, ACPU_PLL_2, 2, 3, 66667, 2, 4, 61440 },
140 { 1, 245760, ACPU_PLL_1, 1, 0, 122880, 1, 4, 61440 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530141 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
142 { 0, 400000, ACPU_PLL_2, 2, 1, 133333, 2, 5, 160000 },
143 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
144 { 1, 800000, ACPU_PLL_2, 2, 0, 200000, 3, 7, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530145 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146};
147
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530148/* 7627 with CDMA capable modem - PLL2 @ 800 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_800_pll4_0[] = {
150 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 24576 },
151 { 1, 98304, ACPU_PLL_1, 1, 1, 98304, 0, 3, 49152 },
152 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 49152 },
153 { 1, 196608, ACPU_PLL_1, 1, 0, 65536, 2, 4, 98304 },
154 { 0, 200000, ACPU_PLL_2, 2, 3, 66667, 2, 4, 98304 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530155 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
156 { 0, 400000, ACPU_PLL_2, 2, 1, 133333, 2, 5, 160000 },
157 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
158 { 1, 800000, ACPU_PLL_2, 2, 0, 200000, 3, 7, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530159 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160};
161
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530162/* 7627a PLL2 @ 1200MHz with GSM capable modem */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_800[] = {
Trilok Soni7d6c8652011-07-14 15:35:07 +0530164 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
165 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
166 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
167 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530168 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni7d6c8652011-07-14 15:35:07 +0530169 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
170 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
171 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
172 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
173 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530174 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175};
176
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530177/* 7627a PLL2 @ 1200MHz with CDMA capable modem */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_800[] = {
Trilok Soni7d6c8652011-07-14 15:35:07 +0530179 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
180 { 0, 65536, ACPU_PLL_1, 1, 3, 8192, 3, 1, 49152 },
181 { 1, 98304, ACPU_PLL_1, 1, 1, 12288, 3, 2, 49152 },
182 { 1, 196608, ACPU_PLL_1, 1, 0, 24576, 3, 3, 98304 },
Trilok Soniabb750b2011-07-13 16:47:18 +0530183 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 120000 },
184 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 120000 },
185 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 120000 },
186 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 120000 },
Trilok Soni7d6c8652011-07-14 15:35:07 +0530187 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
188 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530189 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190};
191
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530192/* 7627aa PLL4 @ 1008MHz with GSM capable modem */
Trilok Sonif597e242011-06-06 12:37:16 +0530193static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_1008[] = {
194 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
195 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
196 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
197 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530198 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Sonif597e242011-06-06 12:37:16 +0530199 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
200 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
201 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 200000 },
202 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
203 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Pankaj Kumar94528702012-03-02 16:00:33 +0530204 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Sonif597e242011-06-06 12:37:16 +0530205};
206
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530207/* 7627aa PLL4 @ 1008MHz with CDMA capable modem */
Trilok Sonid7b05e52011-08-17 18:09:08 +0530208static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_1008[] = {
209 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
210 { 0, 65536, ACPU_PLL_1, 1, 3, 8192, 3, 1, 49152 },
211 { 1, 98304, ACPU_PLL_1, 1, 1, 12288, 3, 2, 49152 },
212 { 1, 196608, ACPU_PLL_1, 1, 0, 24576, 3, 3, 98304 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530213 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Sonid7b05e52011-08-17 18:09:08 +0530214 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
215 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
216 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 200000 },
217 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
218 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Pankaj Kumar94528702012-03-02 16:00:33 +0530219 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Sonid7b05e52011-08-17 18:09:08 +0530220};
221
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530222/* 7625a PLL2 @ 1200MHz with GSM capable modem */
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530223static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_25a[] = {
Trilok Soni54d35c42011-07-14 17:47:50 +0530224 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
225 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
226 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
227 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530228 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni54d35c42011-07-14 17:47:50 +0530229 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530230 { 0, 400000, ACPU_PLL_2, 2, 2, 50000, 3, 4, 122880 },
Trilok Soni54d35c42011-07-14 17:47:50 +0530231 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
232 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530233 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni54d35c42011-07-14 17:47:50 +0530234};
235
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530236/* 7627a PLL2 @ 1200MHz with GSM capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530237static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_800[] = {
238 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
239 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
240 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
241 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530242 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530243 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
244 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
245 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
246 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
247 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530248 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530249};
250
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530251/* 7627a PLL2 @ 1200MHz with CDMA capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530252static struct clkctl_acpu_speed pll0_960_pll1_589_pll2_1200_pll4_800[] = {
253 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
254 { 0, 65536, ACPU_PLL_1, 1, 8, 8192, 3, 1, 49152 },
255 { 1, 98304, ACPU_PLL_1, 1, 5, 12288, 3, 2, 49152 },
256 { 1, 196608, ACPU_PLL_1, 1, 2, 24576, 3, 3, 98304 },
257 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 120000 },
258 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 120000 },
259 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 120000 },
260 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 120000 },
261 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
262 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530263 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530264};
265
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530266/* 7627aa PLL4 @ 1008MHz with GSM capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530267static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_1008[] = {
268 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
269 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
270 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
271 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530272 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530273 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
274 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
275 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 200000 },
276 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
277 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Pankaj Kumar94528702012-03-02 16:00:33 +0530278 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530279};
280
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530281/* 7627aa PLL4 @ 1008MHz with CDMA capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530282static struct clkctl_acpu_speed pll0_960_pll1_589_pll2_1200_pll4_1008[] = {
283 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
284 { 0, 65536, ACPU_PLL_1, 1, 8, 8192, 3, 1, 49152 },
285 { 1, 98304, ACPU_PLL_1, 1, 5, 12288, 3, 2, 49152 },
286 { 1, 196608, ACPU_PLL_1, 1, 2, 24576, 3, 3, 98304 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530287 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530288 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
289 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
290 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 200000 },
291 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
292 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Pankaj Kumar94528702012-03-02 16:00:33 +0530293 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530294};
295
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530296/* 7625a PLL2 @ 1200MHz with GSM capable modem */
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530297static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_25a[] = {
Trilok Soni9bb022c2011-10-31 18:25:19 +0530298 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
299 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
300 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
301 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530302 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530303 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530304 { 0, 400000, ACPU_PLL_2, 2, 2, 50000, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530305 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
306 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
Pankaj Kumar94528702012-03-02 16:00:33 +0530307 { 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530308};
309
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700310#define PLL_CONFIG(m0, m1, m2, m4) { \
Pankaj Kumar3912c982011-12-07 16:59:03 +0530311 m0, m1, m2, m4, \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700312 pll0_##m0##_pll1_##m1##_pll2_##m2##_pll4_##m4 \
313}
314
315struct pll_freq_tbl_map {
Pankaj Kumar3912c982011-12-07 16:59:03 +0530316 unsigned int pll0_rate;
317 unsigned int pll1_rate;
318 unsigned int pll2_rate;
319 unsigned int pll4_rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 struct clkctl_acpu_speed *tbl;
321};
322
323static struct pll_freq_tbl_map acpu_freq_tbl_list[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700324 PLL_CONFIG(960, 196, 1200, 0),
325 PLL_CONFIG(960, 245, 1200, 0),
326 PLL_CONFIG(960, 196, 800, 0),
327 PLL_CONFIG(960, 245, 800, 0),
328 PLL_CONFIG(960, 245, 1200, 800),
329 PLL_CONFIG(960, 196, 1200, 800),
Trilok Sonif597e242011-06-06 12:37:16 +0530330 PLL_CONFIG(960, 245, 1200, 1008),
Trilok Sonid7b05e52011-08-17 18:09:08 +0530331 PLL_CONFIG(960, 196, 1200, 1008),
Trilok Soni9bb022c2011-10-31 18:25:19 +0530332 PLL_CONFIG(960, 737, 1200, 800),
333 PLL_CONFIG(960, 589, 1200, 800),
334 PLL_CONFIG(960, 737, 1200, 1008),
335 PLL_CONFIG(960, 589, 1200, 1008),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700336 { 0, 0, 0, 0, 0 }
337};
338
339#ifdef CONFIG_CPU_FREQ_MSM
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530340static struct cpufreq_frequency_table freq_table[NR_CPUS][20];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700341
342static void __init cpufreq_table_init(void)
343{
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530344 int cpu;
345 for_each_possible_cpu(cpu) {
346 unsigned int i, freq_cnt = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700347
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530348 /* Construct the freq_table table from acpu_freq_tbl since
349 * the freq_table values need to match frequencies specified
350 * in acpu_freq_tbl and acpu_freq_tbl needs to be fixed up
351 * during init.
352 */
353 for (i = 0; acpu_freq_tbl[i].a11clk_khz != 0
354 && freq_cnt < ARRAY_SIZE(*freq_table)-1; i++) {
355 if (acpu_freq_tbl[i].use_for_scaling) {
356 freq_table[cpu][freq_cnt].index = freq_cnt;
357 freq_table[cpu][freq_cnt].frequency
358 = acpu_freq_tbl[i].a11clk_khz;
359 freq_cnt++;
360 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700361 }
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530362
363 /* freq_table not big enough to store all usable freqs. */
364 BUG_ON(acpu_freq_tbl[i].a11clk_khz != 0);
365
366 freq_table[cpu][freq_cnt].index = freq_cnt;
367 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
368 /* Register table with CPUFreq. */
369 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
370 pr_info("CPU%d: %d scaling frequencies supported.\n",
371 cpu, freq_cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373}
374#endif
375
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700376static int acpuclk_set_vdd_level(int vdd)
377{
378 uint32_t current_vdd;
379
Pankaj Kumar9406a3b2011-12-23 18:07:15 +0530380 /*
381 * NOTE: v1.0 of 7x27a/7x25a chip doesn't have working
382 * VDD switching support.
383 */
384 if ((cpu_is_msm7x27a() || cpu_is_msm7x25a()) &&
385 (SOCINFO_VERSION_MINOR(socinfo_get_version()) < 1))
386 return 0;
387
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 current_vdd = readl_relaxed(A11S_VDD_SVS_PLEVEL_ADDR) & 0x07;
389
390 pr_debug("Switching VDD from %u mV -> %d mV\n",
391 current_vdd, vdd);
392
393 writel_relaxed((1 << 7) | (vdd << 3), A11S_VDD_SVS_PLEVEL_ADDR);
394 mb();
Matt Wagantallec57f062011-08-16 23:54:46 -0700395 udelay(62);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700396 if ((readl_relaxed(A11S_VDD_SVS_PLEVEL_ADDR) & 0x7) != vdd) {
397 pr_err("VDD set failed\n");
398 return -EIO;
399 }
400
401 pr_debug("VDD switched\n");
402
403 return 0;
404}
405
406/* Set proper dividers for the given clock speed. */
407static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s)
408{
409 uint32_t reg_clkctl, reg_clksel, clk_div, src_sel;
410
411 reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
412
413 /* AHB_CLK_DIV */
414 clk_div = (reg_clksel >> 1) & 0x03;
415 /* CLK_SEL_SRC1NO */
416 src_sel = reg_clksel & 1;
417
418 /*
419 * If the new clock divider is higher than the previous, then
420 * program the divider before switching the clock
421 */
422 if (hunt_s->ahbclk_div > clk_div) {
423 reg_clksel &= ~(0x3 << 1);
424 reg_clksel |= (hunt_s->ahbclk_div << 1);
425 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
426 }
427
428 /* Program clock source and divider */
429 reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
430 reg_clkctl &= ~(0xFF << (8 * src_sel));
431 reg_clkctl |= hunt_s->a11clk_src_sel << (4 + 8 * src_sel);
432 reg_clkctl |= hunt_s->a11clk_src_div << (0 + 8 * src_sel);
433 writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
434
435 /* Program clock source selection */
436 reg_clksel ^= 1;
437 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
438
439 /*
440 * If the new clock divider is lower than the previous, then
441 * program the divider after switching the clock
442 */
443 if (hunt_s->ahbclk_div < clk_div) {
444 reg_clksel &= ~(0x3 << 1);
445 reg_clksel |= (hunt_s->ahbclk_div << 1);
446 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
447 }
448}
449
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530450static int acpuclk_7627_set_rate(int cpu, unsigned long rate,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700451 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700452{
453 uint32_t reg_clkctl;
454 struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;
455 int res, rc = 0;
456 unsigned int plls_enabled = 0, pll;
457
458 if (reason == SETRATE_CPUFREQ)
459 mutex_lock(&drv_state.lock);
460
461 strt_s = cur_s = drv_state.current_speed;
462
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700463 WARN_ONCE(cur_s == NULL, "%s: not initialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700464 if (cur_s == NULL) {
465 rc = -ENOENT;
466 goto out;
467 }
468
469 if (rate == cur_s->a11clk_khz)
470 goto out;
471
472 for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {
473 if (tgt_s->a11clk_khz == rate)
474 break;
475 }
476
477 if (tgt_s->a11clk_khz == 0) {
478 rc = -EINVAL;
479 goto out;
480 }
481
482 /* Choose the highest speed at or below 'rate' with same PLL. */
483 if (reason != SETRATE_CPUFREQ
484 && tgt_s->a11clk_khz < cur_s->a11clk_khz) {
485 while (tgt_s->pll != ACPU_PLL_TCXO && tgt_s->pll != cur_s->pll)
486 tgt_s--;
487 }
488
489 if (strt_s->pll != ACPU_PLL_TCXO)
490 plls_enabled |= 1 << strt_s->pll;
491
492 if (reason == SETRATE_CPUFREQ) {
493 if (strt_s->pll != tgt_s->pll && tgt_s->pll != ACPU_PLL_TCXO) {
Pankaj Kumar3912c982011-12-07 16:59:03 +0530494 rc = clk_prepare_enable(pll_clk[tgt_s->pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 if (rc < 0) {
496 pr_err("PLL%d enable failed (%d)\n",
497 tgt_s->pll, rc);
498 goto out;
499 }
500 plls_enabled |= 1 << tgt_s->pll;
501 }
502 }
503 /* Need to do this when coming out of power collapse since some modem
504 * firmwares reset the VDD when the application processor enters power
505 * collapse. */
506 if (reason == SETRATE_CPUFREQ || reason == SETRATE_PC) {
507 /* Increase VDD if needed. */
508 if (tgt_s->vdd > cur_s->vdd) {
509 rc = acpuclk_set_vdd_level(tgt_s->vdd);
510 if (rc < 0) {
511 pr_err("Unable to switch ACPU vdd (%d)\n", rc);
512 goto out;
513 }
514 }
515 }
516
517 /* Set wait states for CPU inbetween frequency changes */
518 reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
519 reg_clkctl |= (100 << 16); /* set WT_ST_CNT */
520 writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
521
522 pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
523 strt_s->a11clk_khz, tgt_s->a11clk_khz);
524
525 while (cur_s != tgt_s) {
526 /*
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530527 * Always jump to target freq if within max_speed_delta_khz,
528 * regardless of PLL. If differnece is greater, use the
529 * predefined steppings in the table.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 */
531 int d = abs((int)(cur_s->a11clk_khz - tgt_s->a11clk_khz));
532 if (d > drv_state.max_speed_delta_khz) {
533
534 if (tgt_s->a11clk_khz > cur_s->a11clk_khz) {
535 /* Step up: jump to target PLL as early as
536 * possible so indexing using TCXO (up[-1])
537 * never occurs. */
538 if (likely(cur_s->up[tgt_s->pll]))
539 cur_s = cur_s->up[tgt_s->pll];
540 else
541 cur_s = cur_s->up[cur_s->pll];
542 } else {
543 /* Step down: stay on current PLL as long as
544 * possible so indexing using TCXO (down[-1])
545 * never occurs. */
546 if (likely(cur_s->down[cur_s->pll]))
547 cur_s = cur_s->down[cur_s->pll];
548 else
549 cur_s = cur_s->down[tgt_s->pll];
550 }
551
552 if (cur_s == NULL) { /* This should not happen. */
553 pr_err("No stepping frequencies found. "
554 "strt_s:%u tgt_s:%u\n",
555 strt_s->a11clk_khz, tgt_s->a11clk_khz);
556 rc = -EINVAL;
557 goto out;
558 }
559
560 } else {
561 cur_s = tgt_s;
562 }
563
564 pr_debug("STEP khz = %u, pll = %d\n",
565 cur_s->a11clk_khz, cur_s->pll);
566
567 if (cur_s->pll != ACPU_PLL_TCXO
568 && !(plls_enabled & (1 << cur_s->pll))) {
Pankaj Kumar3912c982011-12-07 16:59:03 +0530569 rc = clk_prepare_enable(pll_clk[cur_s->pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 if (rc < 0) {
571 pr_err("PLL%d enable failed (%d)\n",
572 cur_s->pll, rc);
573 goto out;
574 }
575 plls_enabled |= 1 << cur_s->pll;
576 }
577
578 acpuclk_set_div(cur_s);
579 drv_state.current_speed = cur_s;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 mb();
Matt Wagantallec57f062011-08-16 23:54:46 -0700581 udelay(50);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 }
583
584 /* Nothing else to do for SWFI. */
585 if (reason == SETRATE_SWFI)
586 goto out;
587
588 /* Change the AXI bus frequency if we can. */
589 if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {
590 res = clk_set_rate(drv_state.ebi1_clk,
591 tgt_s->axiclk_khz * 1000);
592 if (res < 0)
593 pr_warning("Setting AXI min rate failed (%d)\n", res);
594 }
595
596 /* Disable PLLs we are not using anymore. */
597 if (tgt_s->pll != ACPU_PLL_TCXO)
598 plls_enabled &= ~(1 << tgt_s->pll);
599 for (pll = ACPU_PLL_0; pll < ACPU_PLL_END; pll++)
Pankaj Kumar3912c982011-12-07 16:59:03 +0530600 if (plls_enabled & (1 << pll))
601 clk_disable_unprepare(pll_clk[pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602
603 /* Nothing else to do for power collapse. */
604 if (reason == SETRATE_PC)
605 goto out;
606
607 /* Drop VDD level if we can. */
608 if (tgt_s->vdd < strt_s->vdd) {
609 res = acpuclk_set_vdd_level(tgt_s->vdd);
610 if (res < 0)
611 pr_warning("Unable to drop ACPU vdd (%d)\n", res);
612 }
613
614 pr_debug("ACPU speed change complete\n");
615out:
616 if (reason == SETRATE_CPUFREQ)
617 mutex_unlock(&drv_state.lock);
618 return rc;
619}
620
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700621static void __init acpuclk_hw_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700622{
623 struct clkctl_acpu_speed *speed;
Trilok Soni7d6c8652011-07-14 15:35:07 +0530624 uint32_t div, sel, reg_clksel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 int res;
626
627 /*
628 * Determine the rate of ACPU clock
629 */
630
631 if (!(readl_relaxed(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */
632 /* CLK_SRC0_SEL */
633 sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;
634 /* CLK_SRC0_DIV */
635 div = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;
636 } else {
637 /* CLK_SRC1_SEL */
638 sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;
639 /* CLK_SRC1_DIV */
640 div = readl_relaxed(A11S_CLK_CNTL_ADDR) & 0x0f;
641 }
642
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700643 for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {
644 if (speed->a11clk_src_sel == sel
645 && (speed->a11clk_src_div == div))
646 break;
647 }
648 if (speed->a11clk_khz == 0) {
649 pr_err("Error - ACPU clock reports invalid speed\n");
650 return;
651 }
652
653 drv_state.current_speed = speed;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530654 if (speed->pll != ACPU_PLL_TCXO) {
655 if (clk_prepare_enable(pll_clk[speed->pll].clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 pr_warning("Failed to vote for boot PLL\n");
Pankaj Kumar3912c982011-12-07 16:59:03 +0530657 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658
Trilok Soni7d6c8652011-07-14 15:35:07 +0530659 /* Fix div2 to 2 for 7x27/5a(aa) targets */
660 if (!cpu_is_msm7x27()) {
661 reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
662 reg_clksel &= ~(0x3 << 14);
663 reg_clksel |= (0x1 << 14);
664 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
665 }
666
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667 res = clk_set_rate(drv_state.ebi1_clk, speed->axiclk_khz * 1000);
668 if (res < 0)
669 pr_warning("Setting AXI min rate failed (%d)\n", res);
Pankaj Kumar19095912012-01-11 18:09:13 +0530670 res = clk_prepare_enable(drv_state.ebi1_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700671 if (res < 0)
672 pr_warning("Enabling AXI clock failed (%d)\n", res);
673
674 pr_info("ACPU running at %d KHz\n", speed->a11clk_khz);
675}
676
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530677static unsigned long acpuclk_7627_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678{
679 WARN_ONCE(drv_state.current_speed == NULL,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700680 "%s: not initialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700681 if (drv_state.current_speed)
682 return drv_state.current_speed->a11clk_khz;
683 else
684 return 0;
685}
686
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687/*----------------------------------------------------------------------------
688 * Clock driver initialization
689 *---------------------------------------------------------------------------*/
Pankaj Kumar3912c982011-12-07 16:59:03 +0530690#define MHZ 1000000
691static void __init select_freq_plan(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692{
Pankaj Kumar3912c982011-12-07 16:59:03 +0530693 unsigned long pll_mhz[ACPU_PLL_END];
694 struct pll_freq_tbl_map *t;
695 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700696
Pankaj Kumar3912c982011-12-07 16:59:03 +0530697 /* Get PLL clocks */
698 for (i = 0; i < ACPU_PLL_END; i++) {
699 if (pll_clk[i].name) {
700 pll_clk[i].clk = clk_get_sys("acpu", pll_clk[i].name);
701 if (IS_ERR(pll_clk[i].clk)) {
702 pll_mhz[i] = 0;
703 continue;
704 }
705 /* Get PLL's Rate */
706 pll_mhz[i] = clk_get_rate(pll_clk[i].clk)/MHZ;
707 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700708 }
709
Pankaj Kumar3912c982011-12-07 16:59:03 +0530710 /*
711 * For the pll configuration used in acpuclock table e.g.
712 * pll0_960_pll1_245_pll2_1200" is same for 7627 and
713 * 7625a (as pll0,pll1,pll2) having same rates, but frequency
714 * table is different for both targets.
715 *
716 * Hence below for loop will not be able to select correct
717 * table based on PLL rates as rates are same. Hence we need
718 * to add this cpu check for selecting the correct acpuclock table.
719 */
Trilok Soni54d35c42011-07-14 17:47:50 +0530720 if (cpu_is_msm7x25a()) {
Pankaj Kumar3912c982011-12-07 16:59:03 +0530721 if (pll_mhz[ACPU_PLL_1] == 245) {
Trilok Soni54d35c42011-07-14 17:47:50 +0530722 acpu_freq_tbl =
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530723 pll0_960_pll1_245_pll2_1200_25a;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530724 } else if (pll_mhz[ACPU_PLL_1] == 737) {
Trilok Soni9bb022c2011-10-31 18:25:19 +0530725 acpu_freq_tbl =
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530726 pll0_960_pll1_737_pll2_1200_25a;
Trilok Soni54d35c42011-07-14 17:47:50 +0530727 }
728 } else {
729 /* Select the right table to use. */
Pankaj Kumar3912c982011-12-07 16:59:03 +0530730 for (t = acpu_freq_tbl_list; t->tbl != 0; t++) {
731 if (t->pll0_rate == pll_mhz[ACPU_PLL_0]
732 && t->pll1_rate == pll_mhz[ACPU_PLL_1]
733 && t->pll2_rate == pll_mhz[ACPU_PLL_2]
734 && t->pll4_rate == pll_mhz[ACPU_PLL_4]) {
735 acpu_freq_tbl = t->tbl;
Trilok Soni54d35c42011-07-14 17:47:50 +0530736 break;
737 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700738 }
739 }
740
741 if (acpu_freq_tbl == NULL) {
742 pr_crit("Unknown PLL configuration!\n");
743 BUG();
744 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745}
746
747/*
748 * Hardware requires the CPU to be dropped to less than MAX_WAIT_FOR_IRQ_KHZ
749 * before entering a wait for irq low-power mode. Find a suitable rate.
750 */
751static unsigned long __init find_wait_for_irq_khz(void)
752{
753 unsigned long found_khz = 0;
754 int i;
755
756 for (i = 0; acpu_freq_tbl[i].a11clk_khz &&
757 acpu_freq_tbl[i].a11clk_khz <= MAX_WAIT_FOR_IRQ_KHZ; i++)
758 found_khz = acpu_freq_tbl[i].a11clk_khz;
759
760 return found_khz;
761}
762
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763static void __init precompute_stepping(void)
764{
765 int i, step_idx;
766
767#define cur_freq acpu_freq_tbl[i].a11clk_khz
768#define step_freq acpu_freq_tbl[step_idx].a11clk_khz
769#define cur_pll acpu_freq_tbl[i].pll
770#define step_pll acpu_freq_tbl[step_idx].pll
771
772 for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
773
774 /* Calculate max "up" step for each destination PLL */
775 step_idx = i + 1;
776 while (step_freq && (step_freq - cur_freq)
777 <= drv_state.max_speed_delta_khz) {
778 acpu_freq_tbl[i].up[step_pll] =
779 &acpu_freq_tbl[step_idx];
780 step_idx++;
781 }
782 if (step_idx == (i + 1) && step_freq) {
783 pr_crit("Delta between freqs %u KHz and %u KHz is"
784 " too high!\n", cur_freq, step_freq);
785 BUG();
786 }
787
788 /* Calculate max "down" step for each destination PLL */
789 step_idx = i - 1;
790 while (step_idx >= 0 && (cur_freq - step_freq)
791 <= drv_state.max_speed_delta_khz) {
792 acpu_freq_tbl[i].down[step_pll] =
793 &acpu_freq_tbl[step_idx];
794 step_idx--;
795 }
796 if (step_idx == (i - 1) && i > 0) {
797 pr_crit("Delta between freqs %u KHz and %u KHz is"
798 " too high!\n", cur_freq, step_freq);
799 BUG();
800 }
801 }
802}
803
804static void __init print_acpu_freq_tbl(void)
805{
806 struct clkctl_acpu_speed *t;
807 short down_idx[ACPU_PLL_END];
808 short up_idx[ACPU_PLL_END];
809 int i, j;
810
811#define FREQ_IDX(freq_ptr) (freq_ptr - acpu_freq_tbl)
812 pr_info("Id CPU-KHz PLL DIV AHB-KHz ADIV AXI-KHz "
813 "D0 D1 D2 D4 U0 U1 U2 U4\n");
814
815 t = &acpu_freq_tbl[0];
816 for (i = 0; t->a11clk_khz != 0; i++) {
817
818 for (j = 0; j < ACPU_PLL_END; j++) {
819 down_idx[j] = t->down[j] ? FREQ_IDX(t->down[j]) : -1;
820 up_idx[j] = t->up[j] ? FREQ_IDX(t->up[j]) : -1;
821 }
822
823 pr_info("%2d %7d %3d %3d %7d %4d %7d "
824 "%2d %2d %2d %2d %2d %2d %2d %2d\n",
825 i, t->a11clk_khz, t->pll, t->a11clk_src_div + 1,
826 t->ahbclk_khz, t->ahbclk_div + 1, t->axiclk_khz,
827 down_idx[0], down_idx[1], down_idx[2], down_idx[4],
828 up_idx[0], up_idx[1], up_idx[2], up_idx[4]);
829
830 t++;
831 }
832}
833
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530835static struct acpuclk_data acpuclk_7627_data = {
836 .set_rate = acpuclk_7627_set_rate,
837 .get_rate = acpuclk_7627_get_rate,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700838 .power_collapse_khz = POWER_COLLAPSE_KHZ,
Matt Wagantallec57f062011-08-16 23:54:46 -0700839 .switch_time_us = 50,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700840};
841
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530842static int __init acpuclk_7627_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843{
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700844 pr_info("%s()\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845
846 drv_state.ebi1_clk = clk_get(NULL, "ebi1_acpu_clk");
847 BUG_ON(IS_ERR(drv_state.ebi1_clk));
848
849 mutex_init(&drv_state.lock);
Matt Wagantallec57f062011-08-16 23:54:46 -0700850 drv_state.max_speed_delta_khz = soc_data->max_speed_delta_khz;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530851 select_freq_plan();
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530852 acpuclk_7627_data.wait_for_irq_khz = find_wait_for_irq_khz();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853 precompute_stepping();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700854 acpuclk_hw_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 print_acpu_freq_tbl();
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530856 acpuclk_register(&acpuclk_7627_data);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700857
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858#ifdef CONFIG_CPU_FREQ_MSM
859 cpufreq_table_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860#endif
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700861 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862}
Matt Wagantallec57f062011-08-16 23:54:46 -0700863
Matt Wagantallec57f062011-08-16 23:54:46 -0700864struct acpuclk_soc_data acpuclk_7x27_soc_data __initdata = {
865 .max_speed_delta_khz = 400000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530866 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700867};
868
869struct acpuclk_soc_data acpuclk_7x27a_soc_data __initdata = {
870 .max_speed_delta_khz = 400000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530871 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700872};
873
874struct acpuclk_soc_data acpuclk_7x27aa_soc_data __initdata = {
875 .max_speed_delta_khz = 504000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530876 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700877};