acpuclock-arm11.c 14 KB

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