acpuclock-arm11.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* arch/arm/mach-msm/acpuclock.c
  2. *
  3. * MSM architecture clock driver
  4. *
  5. * Copyright (C) 2007 Google, Inc.
  6. * Copyright (c) 2007 QUALCOMM Incorporated
  7. * Author: San Mehat <san@android.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/version.h>
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/list.h>
  23. #include <linux/errno.h>
  24. #include <linux/string.h>
  25. #include <linux/delay.h>
  26. #include <linux/clk.h>
  27. #include <linux/cpufreq.h>
  28. #include <linux/mutex.h>
  29. #include <linux/io.h>
  30. #include <mach/board.h>
  31. #include <mach/msm_iomap.h>
  32. #include "proc_comm.h"
  33. #include "acpuclock.h"
  34. #define A11S_CLK_CNTL_ADDR (MSM_CSR_BASE + 0x100)
  35. #define A11S_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)
  36. #define A11S_VDD_SVS_PLEVEL_ADDR (MSM_CSR_BASE + 0x124)
  37. /*
  38. * ARM11 clock configuration for specific ACPU speeds
  39. */
  40. #define ACPU_PLL_TCXO -1
  41. #define ACPU_PLL_0 0
  42. #define ACPU_PLL_1 1
  43. #define ACPU_PLL_2 2
  44. #define ACPU_PLL_3 3
  45. #define PERF_SWITCH_DEBUG 0
  46. #define PERF_SWITCH_STEP_DEBUG 0
  47. struct clock_state
  48. {
  49. struct clkctl_acpu_speed *current_speed;
  50. struct mutex lock;
  51. uint32_t acpu_switch_time_us;
  52. uint32_t max_speed_delta_khz;
  53. uint32_t vdd_switch_time_us;
  54. unsigned long power_collapse_khz;
  55. unsigned long wait_for_irq_khz;
  56. };
  57. static struct clk *ebi1_clk;
  58. static struct clock_state drv_state = { 0 };
  59. static void __init acpuclk_init(void);
  60. /* MSM7201A Levels 3-6 all correspond to 1.2V, level 7 corresponds to 1.325V. */
  61. enum {
  62. VDD_0 = 0,
  63. VDD_1 = 1,
  64. VDD_2 = 2,
  65. VDD_3 = 3,
  66. VDD_4 = 3,
  67. VDD_5 = 3,
  68. VDD_6 = 3,
  69. VDD_7 = 7,
  70. VDD_END
  71. };
  72. struct clkctl_acpu_speed {
  73. unsigned int a11clk_khz;
  74. int pll;
  75. unsigned int a11clk_src_sel;
  76. unsigned int a11clk_src_div;
  77. unsigned int ahbclk_khz;
  78. unsigned int ahbclk_div;
  79. int vdd;
  80. unsigned int axiclk_khz;
  81. unsigned long lpj; /* loops_per_jiffy */
  82. /* Index in acpu_freq_tbl[] for steppings. */
  83. short down;
  84. short up;
  85. };
  86. /*
  87. * ACPU speed table. Complete table is shown but certain speeds are commented
  88. * out to optimized speed switching. Initialize loops_per_jiffy to 0.
  89. *
  90. * Table stepping up/down is optimized for 256mhz jumps while staying on the
  91. * same PLL.
  92. */
  93. #if (0)
  94. static struct clkctl_acpu_speed acpu_freq_tbl[] = {
  95. { 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, VDD_0, 30720, 0, 0, 8 },
  96. { 61440, ACPU_PLL_0, 4, 3, 61440, 0, VDD_0, 30720, 0, 0, 8 },
  97. { 81920, ACPU_PLL_0, 4, 2, 40960, 1, VDD_0, 61440, 0, 0, 8 },
  98. { 96000, ACPU_PLL_1, 1, 7, 48000, 1, VDD_0, 61440, 0, 0, 9 },
  99. { 122880, ACPU_PLL_0, 4, 1, 61440, 1, VDD_3, 61440, 0, 0, 8 },
  100. { 128000, ACPU_PLL_1, 1, 5, 64000, 1, VDD_3, 61440, 0, 0, 12 },
  101. { 176000, ACPU_PLL_2, 2, 5, 88000, 1, VDD_3, 61440, 0, 0, 11 },
  102. { 192000, ACPU_PLL_1, 1, 3, 64000, 2, VDD_3, 61440, 0, 0, 12 },
  103. { 245760, ACPU_PLL_0, 4, 0, 81920, 2, VDD_4, 61440, 0, 0, 12 },
  104. { 256000, ACPU_PLL_1, 1, 2, 128000, 2, VDD_5, 128000, 0, 0, 12 },
  105. { 264000, ACPU_PLL_2, 2, 3, 88000, 2, VDD_5, 128000, 0, 6, 13 },
  106. { 352000, ACPU_PLL_2, 2, 2, 88000, 3, VDD_5, 128000, 0, 6, 13 },
  107. { 384000, ACPU_PLL_1, 1, 1, 128000, 2, VDD_6, 128000, 0, 5, -1 },
  108. { 528000, ACPU_PLL_2, 2, 1, 132000, 3, VDD_7, 128000, 0, 11, -1 },
  109. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  110. };
  111. #else /* Table of freq we currently use. */
  112. static struct clkctl_acpu_speed acpu_freq_tbl[] = {
  113. { 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, VDD_0, 30720, 0, 0, 4 },
  114. { 122880, ACPU_PLL_0, 4, 1, 61440, 1, VDD_3, 61440, 0, 0, 4 },
  115. { 128000, ACPU_PLL_1, 1, 5, 64000, 1, VDD_3, 61440, 0, 0, 6 },
  116. { 176000, ACPU_PLL_2, 2, 5, 88000, 1, VDD_3, 61440, 0, 0, 5 },
  117. { 245760, ACPU_PLL_0, 4, 0, 81920, 2, VDD_4, 61440, 0, 0, 5 },
  118. { 352000, ACPU_PLL_2, 2, 2, 88000, 3, VDD_5, 128000, 0, 3, 7 },
  119. { 384000, ACPU_PLL_1, 1, 1, 128000, 2, VDD_6, 128000, 0, 2, -1 },
  120. { 528000, ACPU_PLL_2, 2, 1, 132000, 3, VDD_7, 128000, 0, 5, -1 },
  121. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  122. };
  123. #endif
  124. #ifdef CONFIG_CPU_FREQ_TABLE
  125. static struct cpufreq_frequency_table freq_table[] = {
  126. { 0, 122880 },
  127. { 1, 128000 },
  128. { 2, 245760 },
  129. { 3, 384000 },
  130. { 4, 528000 },
  131. { 5, CPUFREQ_TABLE_END },
  132. };
  133. #endif
  134. static int pc_pll_request(unsigned id, unsigned on)
  135. {
  136. int res;
  137. on = !!on;
  138. #if PERF_SWITCH_DEBUG
  139. if (on)
  140. printk(KERN_DEBUG "Enabling PLL %d\n", id);
  141. else
  142. printk(KERN_DEBUG "Disabling PLL %d\n", id);
  143. #endif
  144. res = msm_proc_comm(PCOM_CLKCTL_RPC_PLL_REQUEST, &id, &on);
  145. if (res < 0)
  146. return res;
  147. #if PERF_SWITCH_DEBUG
  148. if (on)
  149. printk(KERN_DEBUG "PLL %d enabled\n", id);
  150. else
  151. printk(KERN_DEBUG "PLL %d disabled\n", id);
  152. #endif
  153. return res;
  154. }
  155. /*----------------------------------------------------------------------------
  156. * ARM11 'owned' clock control
  157. *---------------------------------------------------------------------------*/
  158. unsigned long acpuclk_power_collapse(void) {
  159. int ret = acpuclk_get_rate();
  160. ret *= 1000;
  161. if (ret > drv_state.power_collapse_khz)
  162. acpuclk_set_rate(drv_state.power_collapse_khz, 1);
  163. return ret;
  164. }
  165. unsigned long acpuclk_get_wfi_rate(void)
  166. {
  167. return drv_state.wait_for_irq_khz;
  168. }
  169. unsigned long acpuclk_wait_for_irq(void) {
  170. int ret = acpuclk_get_rate();
  171. ret *= 1000;
  172. if (ret > drv_state.wait_for_irq_khz)
  173. acpuclk_set_rate(drv_state.wait_for_irq_khz, 1);
  174. return ret;
  175. }
  176. static int acpuclk_set_vdd_level(int vdd)
  177. {
  178. uint32_t current_vdd;
  179. current_vdd = readl(A11S_VDD_SVS_PLEVEL_ADDR) & 0x07;
  180. #if PERF_SWITCH_DEBUG
  181. printk(KERN_DEBUG "acpuclock: Switching VDD from %u -> %d\n",
  182. current_vdd, vdd);
  183. #endif
  184. writel((1 << 7) | (vdd << 3), A11S_VDD_SVS_PLEVEL_ADDR);
  185. udelay(drv_state.vdd_switch_time_us);
  186. if ((readl(A11S_VDD_SVS_PLEVEL_ADDR) & 0x7) != vdd) {
  187. #if PERF_SWITCH_DEBUG
  188. printk(KERN_ERR "acpuclock: VDD set failed\n");
  189. #endif
  190. return -EIO;
  191. }
  192. #if PERF_SWITCH_DEBUG
  193. printk(KERN_DEBUG "acpuclock: VDD switched\n");
  194. #endif
  195. return 0;
  196. }
  197. /* Set proper dividers for the given clock speed. */
  198. static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s) {
  199. uint32_t reg_clkctl, reg_clksel, clk_div;
  200. /* AHB_CLK_DIV */
  201. clk_div = (readl(A11S_CLK_SEL_ADDR) >> 1) & 0x03;
  202. /*
  203. * If the new clock divider is higher than the previous, then
  204. * program the divider before switching the clock
  205. */
  206. if (hunt_s->ahbclk_div > clk_div) {
  207. reg_clksel = readl(A11S_CLK_SEL_ADDR);
  208. reg_clksel &= ~(0x3 << 1);
  209. reg_clksel |= (hunt_s->ahbclk_div << 1);
  210. writel(reg_clksel, A11S_CLK_SEL_ADDR);
  211. }
  212. if ((readl(A11S_CLK_SEL_ADDR) & 0x01) == 0) {
  213. /* SRC0 */
  214. /* Program clock source */
  215. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  216. reg_clkctl &= ~(0x07 << 4);
  217. reg_clkctl |= (hunt_s->a11clk_src_sel << 4);
  218. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  219. /* Program clock divider */
  220. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  221. reg_clkctl &= ~0xf;
  222. reg_clkctl |= hunt_s->a11clk_src_div;
  223. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  224. /* Program clock source selection */
  225. reg_clksel = readl(A11S_CLK_SEL_ADDR);
  226. reg_clksel |= 1; /* CLK_SEL_SRC1NO == SRC1 */
  227. writel(reg_clksel, A11S_CLK_SEL_ADDR);
  228. } else {
  229. /* SRC1 */
  230. /* Program clock source */
  231. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  232. reg_clkctl &= ~(0x07 << 12);
  233. reg_clkctl |= (hunt_s->a11clk_src_sel << 12);
  234. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  235. /* Program clock divider */
  236. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  237. reg_clkctl &= ~(0xf << 8);
  238. reg_clkctl |= (hunt_s->a11clk_src_div << 8);
  239. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  240. /* Program clock source selection */
  241. reg_clksel = readl(A11S_CLK_SEL_ADDR);
  242. reg_clksel &= ~1; /* CLK_SEL_SRC1NO == SRC0 */
  243. writel(reg_clksel, A11S_CLK_SEL_ADDR);
  244. }
  245. /*
  246. * If the new clock divider is lower than the previous, then
  247. * program the divider after switching the clock
  248. */
  249. if (hunt_s->ahbclk_div < clk_div) {
  250. reg_clksel = readl(A11S_CLK_SEL_ADDR);
  251. reg_clksel &= ~(0x3 << 1);
  252. reg_clksel |= (hunt_s->ahbclk_div << 1);
  253. writel(reg_clksel, A11S_CLK_SEL_ADDR);
  254. }
  255. }
  256. int acpuclk_set_rate(unsigned long rate, int for_power_collapse)
  257. {
  258. uint32_t reg_clkctl;
  259. struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;
  260. int rc = 0;
  261. unsigned int plls_enabled = 0, pll;
  262. strt_s = cur_s = drv_state.current_speed;
  263. WARN_ONCE(cur_s == NULL, "acpuclk_set_rate: not initialized\n");
  264. if (cur_s == NULL)
  265. return -ENOENT;
  266. if (rate == (cur_s->a11clk_khz * 1000))
  267. return 0;
  268. for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {
  269. if (tgt_s->a11clk_khz == (rate / 1000))
  270. break;
  271. }
  272. if (tgt_s->a11clk_khz == 0)
  273. return -EINVAL;
  274. /* Choose the highest speed speed at or below 'rate' with same PLL. */
  275. if (for_power_collapse && tgt_s->a11clk_khz < cur_s->a11clk_khz) {
  276. while (tgt_s->pll != ACPU_PLL_TCXO && tgt_s->pll != cur_s->pll)
  277. tgt_s--;
  278. }
  279. if (strt_s->pll != ACPU_PLL_TCXO)
  280. plls_enabled |= 1 << strt_s->pll;
  281. if (!for_power_collapse) {
  282. mutex_lock(&drv_state.lock);
  283. if (strt_s->pll != tgt_s->pll && tgt_s->pll != ACPU_PLL_TCXO) {
  284. rc = pc_pll_request(tgt_s->pll, 1);
  285. if (rc < 0) {
  286. pr_err("PLL%d enable failed (%d)\n",
  287. tgt_s->pll, rc);
  288. goto out;
  289. }
  290. plls_enabled |= 1 << tgt_s->pll;
  291. }
  292. /* Increase VDD if needed. */
  293. if (tgt_s->vdd > cur_s->vdd) {
  294. if ((rc = acpuclk_set_vdd_level(tgt_s->vdd)) < 0) {
  295. printk(KERN_ERR "Unable to switch ACPU vdd\n");
  296. goto out;
  297. }
  298. }
  299. }
  300. /* Set wait states for CPU inbetween frequency changes */
  301. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  302. reg_clkctl |= (100 << 16); /* set WT_ST_CNT */
  303. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  304. #if PERF_SWITCH_DEBUG
  305. printk(KERN_INFO "acpuclock: Switching from ACPU rate %u -> %u\n",
  306. strt_s->a11clk_khz * 1000, tgt_s->a11clk_khz * 1000);
  307. #endif
  308. while (cur_s != tgt_s) {
  309. /*
  310. * Always jump to target freq if within 256mhz, regulardless of
  311. * PLL. If differnece is greater, use the predefinied
  312. * steppings in the table.
  313. */
  314. int d = abs((int)(cur_s->a11clk_khz - tgt_s->a11clk_khz));
  315. if (d > drv_state.max_speed_delta_khz) {
  316. /* Step up or down depending on target vs current. */
  317. int clk_index = tgt_s->a11clk_khz > cur_s->a11clk_khz ?
  318. cur_s->up : cur_s->down;
  319. if (clk_index < 0) { /* This should not happen. */
  320. printk(KERN_ERR "cur:%u target: %u\n",
  321. cur_s->a11clk_khz, tgt_s->a11clk_khz);
  322. rc = -EINVAL;
  323. goto out;
  324. }
  325. cur_s = &acpu_freq_tbl[clk_index];
  326. } else {
  327. cur_s = tgt_s;
  328. }
  329. #if PERF_SWITCH_STEP_DEBUG
  330. printk(KERN_DEBUG "%s: STEP khz = %u, pll = %d\n",
  331. __FUNCTION__, cur_s->a11clk_khz, cur_s->pll);
  332. #endif
  333. if (!for_power_collapse&& cur_s->pll != ACPU_PLL_TCXO
  334. && !(plls_enabled & (1 << cur_s->pll))) {
  335. rc = pc_pll_request(cur_s->pll, 1);
  336. if (rc < 0) {
  337. pr_err("PLL%d enable failed (%d)\n",
  338. cur_s->pll, rc);
  339. goto out;
  340. }
  341. plls_enabled |= 1 << cur_s->pll;
  342. }
  343. acpuclk_set_div(cur_s);
  344. drv_state.current_speed = cur_s;
  345. /* Re-adjust lpj for the new clock speed. */
  346. loops_per_jiffy = cur_s->lpj;
  347. udelay(drv_state.acpu_switch_time_us);
  348. }
  349. /* Nothing else to do for power collapse. */
  350. if (for_power_collapse)
  351. return 0;
  352. /* Disable PLLs we are not using anymore. */
  353. plls_enabled &= ~(1 << tgt_s->pll);
  354. for (pll = ACPU_PLL_0; pll <= ACPU_PLL_2; pll++)
  355. if (plls_enabled & (1 << pll)) {
  356. rc = pc_pll_request(pll, 0);
  357. if (rc < 0) {
  358. pr_err("PLL%d disable failed (%d)\n", pll, rc);
  359. goto out;
  360. }
  361. }
  362. /* Change the AXI bus frequency if we can. */
  363. if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {
  364. rc = clk_set_rate(ebi1_clk, tgt_s->axiclk_khz * 1000);
  365. if (rc < 0)
  366. pr_err("Setting AXI min rate failed!\n");
  367. }
  368. /* Drop VDD level if we can. */
  369. if (tgt_s->vdd < strt_s->vdd) {
  370. if (acpuclk_set_vdd_level(tgt_s->vdd) < 0)
  371. printk(KERN_ERR "acpuclock: Unable to drop ACPU vdd\n");
  372. }
  373. #if PERF_SWITCH_DEBUG
  374. printk(KERN_DEBUG "%s: ACPU speed change complete\n", __FUNCTION__);
  375. #endif
  376. out:
  377. if (!for_power_collapse)
  378. mutex_unlock(&drv_state.lock);
  379. return rc;
  380. }
  381. static void __init acpuclk_init(void)
  382. {
  383. struct clkctl_acpu_speed *speed;
  384. uint32_t div, sel;
  385. int rc;
  386. /*
  387. * Determine the rate of ACPU clock
  388. */
  389. if (!(readl(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */
  390. /* CLK_SRC0_SEL */
  391. sel = (readl(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;
  392. /* CLK_SRC0_DIV */
  393. div = (readl(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;
  394. } else {
  395. /* CLK_SRC1_SEL */
  396. sel = (readl(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;
  397. /* CLK_SRC1_DIV */
  398. div = readl(A11S_CLK_CNTL_ADDR) & 0x0f;
  399. }
  400. for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {
  401. if (speed->a11clk_src_sel == sel
  402. && (speed->a11clk_src_div == div))
  403. break;
  404. }
  405. if (speed->a11clk_khz == 0) {
  406. printk(KERN_WARNING "Warning - ACPU clock reports invalid speed\n");
  407. return;
  408. }
  409. drv_state.current_speed = speed;
  410. rc = clk_set_rate(ebi1_clk, speed->axiclk_khz * 1000);
  411. if (rc < 0)
  412. pr_err("Setting AXI min rate failed!\n");
  413. printk(KERN_INFO "ACPU running at %d KHz\n", speed->a11clk_khz);
  414. }
  415. unsigned long acpuclk_get_rate(void)
  416. {
  417. WARN_ONCE(drv_state.current_speed == NULL,
  418. "acpuclk_get_rate: not initialized\n");
  419. if (drv_state.current_speed)
  420. return drv_state.current_speed->a11clk_khz;
  421. else
  422. return 0;
  423. }
  424. uint32_t acpuclk_get_switch_time(void)
  425. {
  426. return drv_state.acpu_switch_time_us;
  427. }
  428. /*----------------------------------------------------------------------------
  429. * Clock driver initialization
  430. *---------------------------------------------------------------------------*/
  431. /* Initialize the lpj field in the acpu_freq_tbl. */
  432. static void __init lpj_init(void)
  433. {
  434. int i;
  435. const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
  436. for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
  437. acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
  438. base_clk->a11clk_khz,
  439. acpu_freq_tbl[i].a11clk_khz);
  440. }
  441. }
  442. void __init msm_acpu_clock_init(struct msm_acpu_clock_platform_data *clkdata)
  443. {
  444. pr_info("acpu_clock_init()\n");
  445. ebi1_clk = clk_get(NULL, "ebi1_clk");
  446. mutex_init(&drv_state.lock);
  447. drv_state.acpu_switch_time_us = clkdata->acpu_switch_time_us;
  448. drv_state.max_speed_delta_khz = clkdata->max_speed_delta_khz;
  449. drv_state.vdd_switch_time_us = clkdata->vdd_switch_time_us;
  450. drv_state.power_collapse_khz = clkdata->power_collapse_khz;
  451. drv_state.wait_for_irq_khz = clkdata->wait_for_irq_khz;
  452. acpuclk_init();
  453. lpj_init();
  454. #ifdef CONFIG_CPU_FREQ_TABLE
  455. cpufreq_frequency_table_get_attr(freq_table, smp_processor_id());
  456. #endif
  457. }