PageRenderTime 94ms CodeModel.GetById 45ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/arm/mach-msm/acpuclock-8x50a.c

https://github.com/JoeyJiao/android-huawei-kernel-common
C | 494 lines | 355 code | 79 blank | 60 comment | 74 complexity | e4fda917a815dc19437f5cde755b6930 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* Copyright (c) 2009-2010, Code Aurora Forum. All rights reserved.
  2. *
  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. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. * 02110-1301, USA.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/io.h>
  20. #include <linux/delay.h>
  21. #include <linux/mutex.h>
  22. #include <linux/errno.h>
  23. #include <linux/cpufreq.h>
  24. #include <mach/board.h>
  25. #include <mach/msm_iomap.h>
  26. #include "acpuclock.h"
  27. #include "clock.h"
  28. /* Frequency switch modes. */
  29. #define SHOT_SWITCH 4
  30. #define HOP_SWITCH 5
  31. #define SIMPLE_SLEW 6
  32. #define COMPLEX_SLEW 7
  33. #define L_VAL_384MHZ 0xA
  34. #define L_VAL_1497MHZ 0x27
  35. #define L_VAL_SCPLL_HW_MAX L_VAL_1497MHZ
  36. /* SCPLL Modes. */
  37. #define SCPLL_POWER_DOWN 0
  38. #define SCPLL_BYPASS 1
  39. #define SCPLL_STANDBY 2
  40. #define SCPLL_FULL_CAL 4
  41. #define SCPLL_HALF_CAL 5
  42. #define SCPLL_STEP_CAL 6
  43. #define SCPLL_NORMAL 7
  44. /* Scorpion PLL registers. */
  45. #define SCPLL_CTL_ADDR (MSM_SCPLL_BASE + 0x4)
  46. #define SCPLL_CAL_ADDR (MSM_SCPLL_BASE + 0x8)
  47. #define SCPLL_STATUS_ADDR (MSM_SCPLL_BASE + 0x10)
  48. #define SCPLL_FSM_CTL_EXT_ADDR (MSM_SCPLL_BASE + 0x24)
  49. #define SPSS_CLK_CTL_ADDR (MSM_CSR_BASE + 0x100)
  50. #define SPSS_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)
  51. #define dprintk(msg...) \
  52. cpufreq_debug_printk(CPUFREQ_DEBUG_DRIVER, "cpufreq-msm", msg)
  53. enum {
  54. ACPU_PLL_TCXO = -1,
  55. ACPU_PLL_0 = 0,
  56. ACPU_PLL_1,
  57. ACPU_PLL_2,
  58. ACPU_PLL_3,
  59. ACPU_PLL_END,
  60. };
  61. struct clkctl_acpu_speed {
  62. unsigned int use_for_scaling;
  63. unsigned int acpuclk_khz;
  64. int pll;
  65. unsigned int acpuclk_src_sel;
  66. unsigned int acpuclk_src_div;
  67. unsigned int ahbclk_khz;
  68. unsigned int ahbclk_div;
  69. unsigned int ebi1clk_khz;
  70. unsigned int core_src_sel;
  71. unsigned int l_value;
  72. int vdd;
  73. unsigned long lpj; /* loops_per_jiffy */
  74. };
  75. struct clkctl_acpu_speed acpu_freq_tbl[] = {
  76. { 0, 19200, ACPU_PLL_TCXO, 0, 0, 0, 0, 14000, 0, 0, 1225 },
  77. /* Use AXI source. Row number in acpuclk_init() must match this. */
  78. { 0, 192000, ACPU_PLL_1, 1, 5, 0, 0, 14000, 2, 0, 1225 },
  79. { 1, 245760, ACPU_PLL_0, 4, 0, 0, 0, 29000, 0, 0, 1225 },
  80. { 1, 384000, ACPU_PLL_3, 0, 0, 0, 0, 58000, 1, 0xA, 1225 },
  81. { 0, 422400, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xB, 1225 },
  82. { 0, 460800, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xC, 1225 },
  83. { 0, 499200, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xD, 1225 },
  84. { 0, 537600, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xE, 1225 },
  85. { 1, 576000, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0xF, 1225 },
  86. { 0, 614400, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x10, 1225 },
  87. { 0, 652800, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x11, 1225 },
  88. { 0, 691200, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x12, 1225 },
  89. { 0, 729600, ACPU_PLL_3, 0, 0, 0, 0, 117000, 1, 0x13, 1225 },
  90. { 1, 768000, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x14, 1225 },
  91. { 0, 806400, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x15, 1225 },
  92. { 0, 844800, ACPU_PLL_3, 0, 0, 0, 0, 128000, 1, 0x16, 1225 },
  93. { 0, 883200, ACPU_PLL_3, 0, 0, 0, 0, 160000, 1, 0x17, 1225 },
  94. { 0, 921600, ACPU_PLL_3, 0, 0, 0, 0, 160000, 1, 0x18, 1225 },
  95. { 0, 960000, ACPU_PLL_3, 0, 0, 0, 0, 192000, 1, 0x19, 1225 },
  96. { 1, 998400, ACPU_PLL_3, 0, 0, 0, 0, 192000, 1, 0x1A, 1225 },
  97. { 1, 1190400, ACPU_PLL_3, 0, 0, 0, 0, 259200, 1, 0x1F, 1225 },
  98. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  99. };
  100. struct clock_state {
  101. struct clkctl_acpu_speed *current_speed;
  102. struct mutex lock;
  103. uint32_t acpu_switch_time_us;
  104. uint32_t max_speed_delta_khz;
  105. unsigned int max_vdd;
  106. int (*acpu_set_vdd) (int mvolts);
  107. };
  108. static struct clock_state drv_state = { 0 };
  109. unsigned long clk_get_max_axi_khz(void)
  110. {
  111. return 192000;
  112. }
  113. EXPORT_SYMBOL(clk_get_max_axi_khz);
  114. unsigned long acpuclk_get_rate(int cpu)
  115. {
  116. return drv_state.current_speed->acpuclk_khz;
  117. }
  118. uint32_t acpuclk_get_switch_time(void)
  119. {
  120. return drv_state.acpu_switch_time_us;
  121. }
  122. #define POWER_COLLAPSE_KHZ 128000
  123. unsigned long acpuclk_power_collapse(void)
  124. {
  125. int ret = acpuclk_get_rate(smp_processor_id());
  126. acpuclk_set_rate(smp_processor_id(), POWER_COLLAPSE_KHZ, SETRATE_PC);
  127. return ret;
  128. }
  129. #define WAIT_FOR_IRQ_KHZ 128000
  130. unsigned long acpuclk_wait_for_irq(void)
  131. {
  132. int ret = acpuclk_get_rate(smp_processor_id());
  133. acpuclk_set_rate(smp_processor_id(), WAIT_FOR_IRQ_KHZ, SETRATE_SWFI);
  134. return ret;
  135. }
  136. static void select_core_source(unsigned int src)
  137. {
  138. uint32_t regval;
  139. regval = readl(SPSS_CLK_SEL_ADDR);
  140. regval &= ~(0x3 << 1);
  141. regval |= (src << 1);
  142. writel(regval, SPSS_CLK_SEL_ADDR);
  143. }
  144. static void select_clk_source(struct clkctl_acpu_speed *s)
  145. {
  146. uint32_t reg_clksel, reg_clkctl, src_sel;
  147. /* Configure the PLL divider mux if we plan to use it. */
  148. if (s->core_src_sel == 0) {
  149. reg_clksel = readl(SPSS_CLK_SEL_ADDR);
  150. /* CLK_SEL_SRC1NO */
  151. src_sel = reg_clksel & 1;
  152. /* Program clock source and divider. */
  153. reg_clkctl = readl(SPSS_CLK_CTL_ADDR);
  154. reg_clkctl &= ~(0xFF << (8 * src_sel));
  155. reg_clkctl |= s->acpuclk_src_sel << (4 + 8 * src_sel);
  156. reg_clkctl |= s->acpuclk_src_div << (0 + 8 * src_sel);
  157. writel(reg_clkctl, SPSS_CLK_CTL_ADDR);
  158. /* Toggle clock source. */
  159. reg_clksel ^= 1;
  160. /* Program clock source selection. */
  161. writel(reg_clksel, SPSS_CLK_SEL_ADDR);
  162. }
  163. }
  164. static void scpll_enable(bool state, struct clkctl_acpu_speed *tgt_s)
  165. {
  166. uint32_t regval;
  167. if (state)
  168. dprintk("Enabling PLL 3\n");
  169. else
  170. dprintk("Disabling PLL 3\n");
  171. if (state) {
  172. /* Power-up SCPLL into standby mode. */
  173. writel(SCPLL_STANDBY, SCPLL_CTL_ADDR);
  174. udelay(10);
  175. /* Shot-switch to target frequency. */
  176. regval = (tgt_s->l_value << 3) | SHOT_SWITCH;
  177. writel(regval, SCPLL_FSM_CTL_EXT_ADDR);
  178. writel(SCPLL_NORMAL, SCPLL_CTL_ADDR);
  179. udelay(20);
  180. } else {
  181. /* Power down SCPLL. */
  182. writel(SCPLL_POWER_DOWN, SCPLL_CTL_ADDR);
  183. }
  184. if (state)
  185. dprintk("PLL 3 Enabled\n");
  186. else
  187. dprintk("PLL 3 Disabled\n");
  188. }
  189. static void scpll_change_freq(uint32_t lval)
  190. {
  191. uint32_t regval;
  192. /* Complex-slew switch to target frequency. */
  193. regval = (lval << 3) | COMPLEX_SLEW;
  194. writel(regval, SCPLL_FSM_CTL_EXT_ADDR);
  195. writel(SCPLL_NORMAL, SCPLL_CTL_ADDR);
  196. /* Wait for frequency switch to finish. */
  197. while (readl(SCPLL_STATUS_ADDR) & 0x1)
  198. ;
  199. }
  200. static int acpuclk_set_vdd_level(int vdd)
  201. {
  202. if (drv_state.acpu_set_vdd) {
  203. dprintk("Switching VDD to %d mV\n", vdd);
  204. return drv_state.acpu_set_vdd(vdd);
  205. } else {
  206. /* Assume that the PMIC supports scaling the processor
  207. * to its maximum frequency at its default voltage.
  208. */
  209. return 0;
  210. }
  211. }
  212. int acpuclk_set_rate(int cpu, unsigned long rate, enum setrate_reason reason)
  213. {
  214. struct clkctl_acpu_speed *tgt_s, *strt_s;
  215. int res, rc = 0;
  216. int freq_index = 0;
  217. if (reason == SETRATE_CPUFREQ)
  218. mutex_lock(&drv_state.lock);
  219. strt_s = drv_state.current_speed;
  220. if (rate == strt_s->acpuclk_khz)
  221. goto out;
  222. for (tgt_s = acpu_freq_tbl; tgt_s->acpuclk_khz != 0; tgt_s++) {
  223. if (tgt_s->acpuclk_khz == rate)
  224. break;
  225. freq_index++;
  226. }
  227. if (tgt_s->acpuclk_khz == 0) {
  228. rc = -EINVAL;
  229. goto out;
  230. }
  231. if (reason == SETRATE_CPUFREQ) {
  232. /* Increase VDD if needed. */
  233. if (tgt_s->vdd > strt_s->vdd) {
  234. rc = acpuclk_set_vdd_level(tgt_s->vdd);
  235. if (rc) {
  236. pr_err("Unable to increase ACPU vdd (%d)\n",
  237. rc);
  238. goto out;
  239. }
  240. }
  241. }
  242. dprintk("Switching from ACPU rate %u KHz -> %u KHz\n",
  243. strt_s->acpuclk_khz, tgt_s->acpuclk_khz);
  244. if (strt_s->pll != ACPU_PLL_3 && tgt_s->pll != ACPU_PLL_3) {
  245. select_clk_source(tgt_s);
  246. /* Select core source because target may be AXI. */
  247. select_core_source(tgt_s->core_src_sel);
  248. } else if (strt_s->pll != ACPU_PLL_3 && tgt_s->pll == ACPU_PLL_3) {
  249. scpll_enable(1, tgt_s);
  250. mb();
  251. select_core_source(tgt_s->core_src_sel);
  252. } else if (strt_s->pll == ACPU_PLL_3 && tgt_s->pll != ACPU_PLL_3) {
  253. select_clk_source(tgt_s);
  254. select_core_source(tgt_s->core_src_sel);
  255. mb();
  256. scpll_enable(0, NULL);
  257. } else {
  258. scpll_change_freq(tgt_s->l_value);
  259. }
  260. /* Update the driver state with the new clock freq */
  261. drv_state.current_speed = tgt_s;
  262. /* Re-adjust lpj for the new clock speed. */
  263. loops_per_jiffy = tgt_s->lpj;
  264. /* Nothing else to do for SWFI. */
  265. if (reason == SETRATE_SWFI)
  266. goto out;
  267. if (strt_s->ebi1clk_khz != tgt_s->ebi1clk_khz) {
  268. res = ebi1_clk_set_min_rate(CLKVOTE_ACPUCLK,
  269. tgt_s->ebi1clk_khz * 1000);
  270. if (res < 0)
  271. pr_warning("Setting EBI1/AXI min rate failed (%d)\n",
  272. res);
  273. }
  274. /* Nothing else to do for power collapse */
  275. if (reason == SETRATE_PC)
  276. goto out;
  277. /* Drop VDD level if we can. */
  278. if (tgt_s->vdd < strt_s->vdd) {
  279. res = acpuclk_set_vdd_level(tgt_s->vdd);
  280. if (res)
  281. pr_warning("Unable to drop ACPU vdd (%d)\n", res);
  282. }
  283. dprintk("ACPU speed change complete\n");
  284. out:
  285. if (reason == SETRATE_CPUFREQ)
  286. mutex_unlock(&drv_state.lock);
  287. return rc;
  288. }
  289. static void __init scpll_init(void)
  290. {
  291. uint32_t regval;
  292. dprintk("Initializing PLL 3\n");
  293. /* Power-up SCPLL into standby mode. */
  294. writel(SCPLL_STANDBY, SCPLL_CTL_ADDR);
  295. udelay(10);
  296. /* Calibrate the SCPLL to the maximum range supported by the h/w. We
  297. * might not use the full range of calibrated frequencies, but this
  298. * simplifies changes required for future increases in max CPU freq.
  299. */
  300. regval = (L_VAL_SCPLL_HW_MAX << 24) | (L_VAL_384MHZ << 16);
  301. writel(regval, SCPLL_CAL_ADDR);
  302. writel(SCPLL_FULL_CAL, SCPLL_CTL_ADDR);
  303. /* Wait for calibration to compelte. */
  304. while (readl(SCPLL_STATUS_ADDR) & 0x2)
  305. ;
  306. /* Power-down SCPLL */
  307. scpll_enable(0, NULL);
  308. }
  309. static void __init acpuclk_init(void)
  310. {
  311. struct clkctl_acpu_speed *speed;
  312. uint32_t div, sel, regval;
  313. int res;
  314. /* Determine the source of the Scorpion clock. */
  315. regval = readl(SPSS_CLK_SEL_ADDR);
  316. switch ((regval & 0x6) >> 1) {
  317. case 0: /* raw source clock */
  318. case 3: /* low jitter PLL1 (768Mhz) */
  319. if (regval & 0x1) {
  320. sel = ((readl(SPSS_CLK_CTL_ADDR) >> 4) & 0x7);
  321. div = ((readl(SPSS_CLK_CTL_ADDR) >> 0) & 0xf);
  322. } else {
  323. sel = ((readl(SPSS_CLK_CTL_ADDR) >> 12) & 0x7);
  324. div = ((readl(SPSS_CLK_CTL_ADDR) >> 8) & 0xf);
  325. }
  326. /* Find the matching clock rate. */
  327. for (speed = acpu_freq_tbl; speed->acpuclk_khz != 0; speed++) {
  328. if (speed->acpuclk_src_sel == sel &&
  329. speed->acpuclk_src_div == div)
  330. break;
  331. }
  332. break;
  333. case 1: /* unbuffered scorpion pll (384Mhz to 998.4Mhz) */
  334. sel = ((readl(SCPLL_FSM_CTL_EXT_ADDR) >> 3) & 0x3f);
  335. /* Find the matching clock rate. */
  336. for (speed = acpu_freq_tbl; speed->acpuclk_khz != 0; speed++) {
  337. if (speed->l_value == sel &&
  338. speed->core_src_sel == 1)
  339. break;
  340. }
  341. break;
  342. case 2: /* AXI bus clock (128Mhz) */
  343. speed = &acpu_freq_tbl[1];
  344. break;
  345. default:
  346. BUG();
  347. }
  348. /* Initialize scpll only if it wasn't already initialized by the boot
  349. * loader. If the CPU is already running on scpll, then the scpll was
  350. * initialized by the boot loader. */
  351. if (speed->pll != ACPU_PLL_3)
  352. scpll_init();
  353. if (speed->acpuclk_khz == 0) {
  354. pr_err("Error - ACPU clock reports invalid speed\n");
  355. return;
  356. }
  357. /* Set initial ACPU VDD. */
  358. acpuclk_set_vdd_level(speed->vdd);
  359. drv_state.current_speed = speed;
  360. res = ebi1_clk_set_min_rate(CLKVOTE_ACPUCLK, speed->ebi1clk_khz * 1000);
  361. if (res < 0)
  362. pr_warning("Setting EBI1/AXI min rate failed (%d)\n", res);
  363. pr_info("ACPU running at %d KHz\n", speed->acpuclk_khz);
  364. }
  365. /* Initalize the lpj field in the acpu_freq_tbl. */
  366. static void __init lpj_init(void)
  367. {
  368. int i;
  369. const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
  370. for (i = 0; acpu_freq_tbl[i].acpuclk_khz; i++) {
  371. acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
  372. base_clk->acpuclk_khz,
  373. acpu_freq_tbl[i].acpuclk_khz);
  374. }
  375. }
  376. #ifdef CONFIG_CPU_FREQ_MSM
  377. static struct cpufreq_frequency_table freq_table[20];
  378. static void __init cpufreq_table_init(void)
  379. {
  380. unsigned int i;
  381. unsigned int freq_cnt = 0;
  382. /* Construct the freq_table table from acpu_freq_tbl since the
  383. * freq_table values need to match frequencies specified in
  384. * acpu_freq_tbl and acpu_freq_tbl needs to be fixed up during init.
  385. */
  386. for (i = 0; acpu_freq_tbl[i].acpuclk_khz != 0
  387. && freq_cnt < ARRAY_SIZE(freq_table)-1; i++) {
  388. if (acpu_freq_tbl[i].use_for_scaling) {
  389. freq_table[freq_cnt].index = freq_cnt;
  390. freq_table[freq_cnt].frequency
  391. = acpu_freq_tbl[i].acpuclk_khz;
  392. freq_cnt++;
  393. }
  394. }
  395. /* freq_table not big enough to store all usable freqs. */
  396. BUG_ON(acpu_freq_tbl[i].acpuclk_khz != 0);
  397. freq_table[freq_cnt].index = freq_cnt;
  398. freq_table[freq_cnt].frequency = CPUFREQ_TABLE_END;
  399. pr_info("%d scaling frequencies supported.\n", freq_cnt);
  400. }
  401. #endif
  402. void __init msm_acpu_clock_init(struct msm_acpu_clock_platform_data *clkdata)
  403. {
  404. mutex_init(&drv_state.lock);
  405. drv_state.acpu_switch_time_us = clkdata->acpu_switch_time_us;
  406. drv_state.max_speed_delta_khz = clkdata->max_speed_delta_khz;
  407. drv_state.max_vdd = clkdata->max_vdd;
  408. drv_state.acpu_set_vdd = clkdata->acpu_set_vdd;
  409. acpuclk_init();
  410. lpj_init();
  411. #ifdef CONFIG_CPU_FREQ_MSM
  412. cpufreq_table_init();
  413. cpufreq_frequency_table_get_attr(freq_table, smp_processor_id());
  414. #endif
  415. }