acpuclock-arm11.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. Initalize 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. 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. static int pc_pll_request(unsigned id, unsigned on)
  133. {
  134. int res;
  135. on = !!on;
  136. #if PERF_SWITCH_DEBUG
  137. if (on)
  138. printk(KERN_DEBUG "Enabling PLL %d\n", id);
  139. else
  140. printk(KERN_DEBUG "Disabling PLL %d\n", id);
  141. #endif
  142. res = msm_proc_comm(PCOM_CLKCTL_RPC_PLL_REQUEST, &id, &on);
  143. if (res < 0)
  144. return res;
  145. #if PERF_SWITCH_DEBUG
  146. if (on)
  147. printk(KERN_DEBUG "PLL %d enabled\n", id);
  148. else
  149. printk(KERN_DEBUG "PLL %d disabled\n", id);
  150. #endif
  151. return res;
  152. }
  153. /*----------------------------------------------------------------------------
  154. * ARM11 'owned' clock control
  155. *---------------------------------------------------------------------------*/
  156. unsigned long acpuclk_power_collapse(void) {
  157. int ret = acpuclk_get_rate();
  158. ret *= 1000;
  159. if (ret > drv_state.power_collapse_khz)
  160. acpuclk_set_rate(drv_state.power_collapse_khz, 1);
  161. return ret;
  162. }
  163. unsigned long acpuclk_get_wfi_rate(void)
  164. {
  165. return drv_state.wait_for_irq_khz;
  166. }
  167. unsigned long acpuclk_wait_for_irq(void) {
  168. int ret = acpuclk_get_rate();
  169. ret *= 1000;
  170. if (ret > drv_state.wait_for_irq_khz)
  171. acpuclk_set_rate(drv_state.wait_for_irq_khz, 1);
  172. return ret;
  173. }
  174. static int acpuclk_set_vdd_level(int vdd)
  175. {
  176. uint32_t current_vdd;
  177. current_vdd = readl(A11S_VDD_SVS_PLEVEL_ADDR) & 0x07;
  178. #if PERF_SWITCH_DEBUG
  179. printk(KERN_DEBUG "acpuclock: Switching VDD from %u -> %d\n",
  180. current_vdd, vdd);
  181. #endif
  182. writel((1 << 7) | (vdd << 3), A11S_VDD_SVS_PLEVEL_ADDR);
  183. udelay(drv_state.vdd_switch_time_us);
  184. if ((readl(A11S_VDD_SVS_PLEVEL_ADDR) & 0x7) != vdd) {
  185. #if PERF_SWITCH_DEBUG
  186. printk(KERN_ERR "acpuclock: VDD set failed\n");
  187. #endif
  188. return -EIO;
  189. }
  190. #if PERF_SWITCH_DEBUG
  191. printk(KERN_DEBUG "acpuclock: VDD switched\n");
  192. #endif
  193. return 0;
  194. }
  195. /* Set proper dividers for the given clock speed. */
  196. static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s) {
  197. uint32_t reg_clkctl, reg_clksel, clk_div;
  198. /* AHB_CLK_DIV */
  199. clk_div = (readl(A11S_CLK_SEL_ADDR) >> 1) & 0x03;
  200. /*
  201. * If the new clock divider is higher than the previous, then
  202. * program the divider before switching the clock
  203. */
  204. if (hunt_s->ahbclk_div > clk_div) {
  205. reg_clksel = readl(A11S_CLK_SEL_ADDR);
  206. reg_clksel &= ~(0x3 << 1);
  207. reg_clksel |= (hunt_s->ahbclk_div << 1);
  208. writel(reg_clksel, A11S_CLK_SEL_ADDR);
  209. }
  210. if ((readl(A11S_CLK_SEL_ADDR) & 0x01) == 0) {
  211. /* SRC0 */
  212. /* Program clock source */
  213. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  214. reg_clkctl &= ~(0x07 << 4);
  215. reg_clkctl |= (hunt_s->a11clk_src_sel << 4);
  216. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  217. /* Program clock divider */
  218. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  219. reg_clkctl &= ~0xf;
  220. reg_clkctl |= hunt_s->a11clk_src_div;
  221. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  222. /* Program clock source selection */
  223. reg_clksel = readl(A11S_CLK_SEL_ADDR);
  224. reg_clksel |= 1; /* CLK_SEL_SRC1NO == SRC1 */
  225. writel(reg_clksel, A11S_CLK_SEL_ADDR);
  226. } else {
  227. /* SRC1 */
  228. /* Program clock source */
  229. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  230. reg_clkctl &= ~(0x07 << 12);
  231. reg_clkctl |= (hunt_s->a11clk_src_sel << 12);
  232. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  233. /* Program clock divider */
  234. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  235. reg_clkctl &= ~(0xf << 8);
  236. reg_clkctl |= (hunt_s->a11clk_src_div << 8);
  237. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  238. /* Program clock source selection */
  239. reg_clksel = readl(A11S_CLK_SEL_ADDR);
  240. reg_clksel &= ~1; /* CLK_SEL_SRC1NO == SRC0 */
  241. writel(reg_clksel, A11S_CLK_SEL_ADDR);
  242. }
  243. /*
  244. * If the new clock divider is lower than the previous, then
  245. * program the divider after switching the clock
  246. */
  247. if (hunt_s->ahbclk_div < clk_div) {
  248. reg_clksel = readl(A11S_CLK_SEL_ADDR);
  249. reg_clksel &= ~(0x3 << 1);
  250. reg_clksel |= (hunt_s->ahbclk_div << 1);
  251. writel(reg_clksel, A11S_CLK_SEL_ADDR);
  252. }
  253. }
  254. int acpuclk_set_rate(unsigned long rate, int for_power_collapse)
  255. {
  256. uint32_t reg_clkctl;
  257. struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;
  258. int rc = 0;
  259. unsigned int plls_enabled = 0, pll;
  260. strt_s = cur_s = drv_state.current_speed;
  261. WARN_ONCE(cur_s == NULL, "acpuclk_set_rate: not initialized\n");
  262. if (cur_s == NULL)
  263. return -ENOENT;
  264. if (rate == (cur_s->a11clk_khz * 1000))
  265. return 0;
  266. for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {
  267. if (tgt_s->a11clk_khz == (rate / 1000))
  268. break;
  269. }
  270. if (tgt_s->a11clk_khz == 0)
  271. return -EINVAL;
  272. /* Choose the highest speed speed at or below 'rate' with same PLL. */
  273. if (for_power_collapse && tgt_s->a11clk_khz < cur_s->a11clk_khz) {
  274. while (tgt_s->pll != ACPU_PLL_TCXO && tgt_s->pll != cur_s->pll)
  275. tgt_s--;
  276. }
  277. if (strt_s->pll != ACPU_PLL_TCXO)
  278. plls_enabled |= 1 << strt_s->pll;
  279. if (!for_power_collapse) {
  280. mutex_lock(&drv_state.lock);
  281. if (strt_s->pll != tgt_s->pll && tgt_s->pll != ACPU_PLL_TCXO) {
  282. rc = pc_pll_request(tgt_s->pll, 1);
  283. if (rc < 0) {
  284. pr_err("PLL%d enable failed (%d)\n",
  285. tgt_s->pll, rc);
  286. goto out;
  287. }
  288. plls_enabled |= 1 << tgt_s->pll;
  289. }
  290. /* Increase VDD if needed. */
  291. if (tgt_s->vdd > cur_s->vdd) {
  292. if ((rc = acpuclk_set_vdd_level(tgt_s->vdd)) < 0) {
  293. printk(KERN_ERR "Unable to switch ACPU vdd\n");
  294. goto out;
  295. }
  296. }
  297. }
  298. /* Set wait states for CPU inbetween frequency changes */
  299. reg_clkctl = readl(A11S_CLK_CNTL_ADDR);
  300. reg_clkctl |= (100 << 16); /* set WT_ST_CNT */
  301. writel(reg_clkctl, A11S_CLK_CNTL_ADDR);
  302. #if PERF_SWITCH_DEBUG
  303. printk(KERN_INFO "acpuclock: Switching from ACPU rate %u -> %u\n",
  304. strt_s->a11clk_khz * 1000, tgt_s->a11clk_khz * 1000);
  305. #endif
  306. while (cur_s != tgt_s) {
  307. /*
  308. * Always jump to target freq if within 256mhz, regulardless of
  309. * PLL. If differnece is greater, use the predefinied
  310. * steppings in the table.
  311. */
  312. int d = abs((int)(cur_s->a11clk_khz - tgt_s->a11clk_khz));
  313. if (d > drv_state.max_speed_delta_khz) {
  314. /* Step up or down depending on target vs current. */
  315. int clk_index = tgt_s->a11clk_khz > cur_s->a11clk_khz ?
  316. cur_s->up : cur_s->down;
  317. if (clk_index < 0) { /* This should not happen. */
  318. printk(KERN_ERR "cur:%u target: %u\n",
  319. cur_s->a11clk_khz, tgt_s->a11clk_khz);
  320. rc = -EINVAL;
  321. goto out;
  322. }
  323. cur_s = &acpu_freq_tbl[clk_index];
  324. } else {
  325. cur_s = tgt_s;
  326. }
  327. #if PERF_SWITCH_STEP_DEBUG
  328. printk(KERN_DEBUG "%s: STEP khz = %u, pll = %d\n",
  329. __FUNCTION__, cur_s->a11clk_khz, cur_s->pll);
  330. #endif
  331. if (!for_power_collapse&& cur_s->pll != ACPU_PLL_TCXO
  332. && !(plls_enabled & (1 << cur_s->pll))) {
  333. rc = pc_pll_request(cur_s->pll, 1);
  334. if (rc < 0) {
  335. pr_err("PLL%d enable failed (%d)\n",
  336. cur_s->pll, rc);
  337. goto out;
  338. }
  339. plls_enabled |= 1 << cur_s->pll;
  340. }
  341. acpuclk_set_div(cur_s);
  342. drv_state.current_speed = cur_s;
  343. /* Re-adjust lpj for the new clock speed. */
  344. loops_per_jiffy = cur_s->lpj;
  345. udelay(drv_state.acpu_switch_time_us);
  346. }
  347. /* Nothing else to do for power collapse. */
  348. if (for_power_collapse)
  349. return 0;
  350. /* Disable PLLs we are not using anymore. */
  351. plls_enabled &= ~(1 << tgt_s->pll);
  352. for (pll = ACPU_PLL_0; pll <= ACPU_PLL_2; pll++)
  353. if (plls_enabled & (1 << pll)) {
  354. rc = pc_pll_request(pll, 0);
  355. if (rc < 0) {
  356. pr_err("PLL%d disable failed (%d)\n", pll, rc);
  357. goto out;
  358. }
  359. }
  360. /* Change the AXI bus frequency if we can. */
  361. if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {
  362. rc = clk_set_rate(ebi1_clk, tgt_s->axiclk_khz * 1000);
  363. if (rc < 0)
  364. pr_err("Setting AXI min rate failed!\n");
  365. }
  366. /* Drop VDD level if we can. */
  367. if (tgt_s->vdd < strt_s->vdd) {
  368. if (acpuclk_set_vdd_level(tgt_s->vdd) < 0)
  369. printk(KERN_ERR "acpuclock: Unable to drop ACPU vdd\n");
  370. }
  371. #if PERF_SWITCH_DEBUG
  372. printk(KERN_DEBUG "%s: ACPU speed change complete\n", __FUNCTION__);
  373. #endif
  374. out:
  375. if (!for_power_collapse)
  376. mutex_unlock(&drv_state.lock);
  377. return rc;
  378. }
  379. static void __init acpuclk_init(void)
  380. {
  381. struct clkctl_acpu_speed *speed;
  382. uint32_t div, sel;
  383. int rc;
  384. /*
  385. * Determine the rate of ACPU clock
  386. */
  387. if (!(readl(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */
  388. /* CLK_SRC0_SEL */
  389. sel = (readl(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;
  390. /* CLK_SRC0_DIV */
  391. div = (readl(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;
  392. } else {
  393. /* CLK_SRC1_SEL */
  394. sel = (readl(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;
  395. /* CLK_SRC1_DIV */
  396. div = readl(A11S_CLK_CNTL_ADDR) & 0x0f;
  397. }
  398. for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {
  399. if (speed->a11clk_src_sel == sel
  400. && (speed->a11clk_src_div == div))
  401. break;
  402. }
  403. if (speed->a11clk_khz == 0) {
  404. printk(KERN_WARNING "Warning - ACPU clock reports invalid speed\n");
  405. return;
  406. }
  407. drv_state.current_speed = speed;
  408. rc = clk_set_rate(ebi1_clk, speed->axiclk_khz * 1000);
  409. if (rc < 0)
  410. pr_err("Setting AXI min rate failed!\n");
  411. printk(KERN_INFO "ACPU running at %d KHz\n", speed->a11clk_khz);
  412. }
  413. unsigned long acpuclk_get_rate(void)
  414. {
  415. WARN_ONCE(drv_state.current_speed == NULL,
  416. "acpuclk_get_rate: not initialized\n");
  417. if (drv_state.current_speed)
  418. return drv_state.current_speed->a11clk_khz;
  419. else
  420. return 0;
  421. }
  422. uint32_t acpuclk_get_switch_time(void)
  423. {
  424. return drv_state.acpu_switch_time_us;
  425. }
  426. /*----------------------------------------------------------------------------
  427. * Clock driver initialization
  428. *---------------------------------------------------------------------------*/
  429. /* Initalize the lpj field in the acpu_freq_tbl. */
  430. static void __init lpj_init(void)
  431. {
  432. int i;
  433. const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
  434. for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
  435. acpu_freq_tbl[i].lpj = cpufreq_scale(loops_per_jiffy,
  436. base_clk->a11clk_khz,
  437. acpu_freq_tbl[i].a11clk_khz);
  438. }
  439. }
  440. void __init msm_acpu_clock_init(struct msm_acpu_clock_platform_data *clkdata)
  441. {
  442. pr_info("acpu_clock_init()\n");
  443. ebi1_clk = clk_get(NULL, "ebi1_clk");
  444. mutex_init(&drv_state.lock);
  445. drv_state.acpu_switch_time_us = clkdata->acpu_switch_time_us;
  446. drv_state.max_speed_delta_khz = clkdata->max_speed_delta_khz;
  447. drv_state.vdd_switch_time_us = clkdata->vdd_switch_time_us;
  448. drv_state.power_collapse_khz = clkdata->power_collapse_khz;
  449. drv_state.wait_for_irq_khz = clkdata->wait_for_irq_khz;
  450. acpuclk_init();
  451. lpj_init();
  452. #ifdef CONFIG_CPU_FREQ_TABLE
  453. cpufreq_frequency_table_get_attr(freq_table, smp_processor_id());
  454. #endif
  455. }