exynos4210-cpufreq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2010-2011 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * EXYNOS4210 - CPU frequency scaling support
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/err.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/slab.h>
  17. #include <linux/cpufreq.h>
  18. #include <mach/regs-clock.h>
  19. #include <mach/cpufreq.h>
  20. #define CPUFREQ_LEVEL_END L5
  21. static struct clk *cpu_clk;
  22. static struct clk *moutcore;
  23. static struct clk *mout_mpll;
  24. static struct clk *mout_apll;
  25. struct cpufreq_clkdiv {
  26. unsigned int index;
  27. unsigned int clkdiv;
  28. };
  29. static unsigned int exynos4210_volt_table[CPUFREQ_LEVEL_END] = {
  30. 1250000, 1150000, 1050000, 975000, 950000,
  31. };
  32. static struct cpufreq_clkdiv exynos4210_clkdiv_table[CPUFREQ_LEVEL_END];
  33. static struct cpufreq_frequency_table exynos4210_freq_table[] = {
  34. {L0, 1200*1000},
  35. {L1, 1000*1000},
  36. {L2, 800*1000},
  37. {L3, 500*1000},
  38. {L4, 200*1000},
  39. {0, CPUFREQ_TABLE_END},
  40. };
  41. static unsigned int clkdiv_cpu0[CPUFREQ_LEVEL_END][7] = {
  42. /*
  43. * Clock divider value for following
  44. * { DIVCORE, DIVCOREM0, DIVCOREM1, DIVPERIPH,
  45. * DIVATB, DIVPCLK_DBG, DIVAPLL }
  46. */
  47. /* ARM L0: 1200MHz */
  48. { 0, 3, 7, 3, 4, 1, 7 },
  49. /* ARM L1: 1000MHz */
  50. { 0, 3, 7, 3, 4, 1, 7 },
  51. /* ARM L2: 800MHz */
  52. { 0, 3, 7, 3, 3, 1, 7 },
  53. /* ARM L3: 500MHz */
  54. { 0, 3, 7, 3, 3, 1, 7 },
  55. /* ARM L4: 200MHz */
  56. { 0, 1, 3, 1, 3, 1, 0 },
  57. };
  58. static unsigned int clkdiv_cpu1[CPUFREQ_LEVEL_END][2] = {
  59. /*
  60. * Clock divider value for following
  61. * { DIVCOPY, DIVHPM }
  62. */
  63. /* ARM L0: 1200MHz */
  64. { 5, 0 },
  65. /* ARM L1: 1000MHz */
  66. { 4, 0 },
  67. /* ARM L2: 800MHz */
  68. { 3, 0 },
  69. /* ARM L3: 500MHz */
  70. { 3, 0 },
  71. /* ARM L4: 200MHz */
  72. { 3, 0 },
  73. };
  74. static unsigned int exynos4210_apll_pms_table[CPUFREQ_LEVEL_END] = {
  75. /* APLL FOUT L0: 1200MHz */
  76. ((150 << 16) | (3 << 8) | 1),
  77. /* APLL FOUT L1: 1000MHz */
  78. ((250 << 16) | (6 << 8) | 1),
  79. /* APLL FOUT L2: 800MHz */
  80. ((200 << 16) | (6 << 8) | 1),
  81. /* APLL FOUT L3: 500MHz */
  82. ((250 << 16) | (6 << 8) | 2),
  83. /* APLL FOUT L4: 200MHz */
  84. ((200 << 16) | (6 << 8) | 3),
  85. };
  86. static void exynos4210_set_clkdiv(unsigned int div_index)
  87. {
  88. unsigned int tmp;
  89. /* Change Divider - CPU0 */
  90. tmp = exynos4210_clkdiv_table[div_index].clkdiv;
  91. __raw_writel(tmp, EXYNOS4_CLKDIV_CPU);
  92. do {
  93. tmp = __raw_readl(EXYNOS4_CLKDIV_STATCPU);
  94. } while (tmp & 0x1111111);
  95. /* Change Divider - CPU1 */
  96. tmp = __raw_readl(EXYNOS4_CLKDIV_CPU1);
  97. tmp &= ~((0x7 << 4) | 0x7);
  98. tmp |= ((clkdiv_cpu1[div_index][0] << 4) |
  99. (clkdiv_cpu1[div_index][1] << 0));
  100. __raw_writel(tmp, EXYNOS4_CLKDIV_CPU1);
  101. do {
  102. tmp = __raw_readl(EXYNOS4_CLKDIV_STATCPU1);
  103. } while (tmp & 0x11);
  104. }
  105. static void exynos4210_set_apll(unsigned int index)
  106. {
  107. unsigned int tmp;
  108. /* 1. MUX_CORE_SEL = MPLL, ARMCLK uses MPLL for lock time */
  109. clk_set_parent(moutcore, mout_mpll);
  110. do {
  111. tmp = (__raw_readl(EXYNOS4_CLKMUX_STATCPU)
  112. >> EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT);
  113. tmp &= 0x7;
  114. } while (tmp != 0x2);
  115. /* 2. Set APLL Lock time */
  116. __raw_writel(EXYNOS4_APLL_LOCKTIME, EXYNOS4_APLL_LOCK);
  117. /* 3. Change PLL PMS values */
  118. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  119. tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0));
  120. tmp |= exynos4210_apll_pms_table[index];
  121. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  122. /* 4. wait_lock_time */
  123. do {
  124. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  125. } while (!(tmp & (0x1 << EXYNOS4_APLLCON0_LOCKED_SHIFT)));
  126. /* 5. MUX_CORE_SEL = APLL */
  127. clk_set_parent(moutcore, mout_apll);
  128. do {
  129. tmp = __raw_readl(EXYNOS4_CLKMUX_STATCPU);
  130. tmp &= EXYNOS4_CLKMUX_STATCPU_MUXCORE_MASK;
  131. } while (tmp != (0x1 << EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT));
  132. }
  133. bool exynos4210_pms_change(unsigned int old_index, unsigned int new_index)
  134. {
  135. unsigned int old_pm = (exynos4210_apll_pms_table[old_index] >> 8);
  136. unsigned int new_pm = (exynos4210_apll_pms_table[new_index] >> 8);
  137. return (old_pm == new_pm) ? 0 : 1;
  138. }
  139. static void exynos4210_set_frequency(unsigned int old_index,
  140. unsigned int new_index)
  141. {
  142. unsigned int tmp;
  143. if (old_index > new_index) {
  144. if (!exynos4210_pms_change(old_index, new_index)) {
  145. /* 1. Change the system clock divider values */
  146. exynos4210_set_clkdiv(new_index);
  147. /* 2. Change just s value in apll m,p,s value */
  148. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  149. tmp &= ~(0x7 << 0);
  150. tmp |= (exynos4210_apll_pms_table[new_index] & 0x7);
  151. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  152. } else {
  153. /* Clock Configuration Procedure */
  154. /* 1. Change the system clock divider values */
  155. exynos4210_set_clkdiv(new_index);
  156. /* 2. Change the apll m,p,s value */
  157. exynos4210_set_apll(new_index);
  158. }
  159. } else if (old_index < new_index) {
  160. if (!exynos4210_pms_change(old_index, new_index)) {
  161. /* 1. Change just s value in apll m,p,s value */
  162. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  163. tmp &= ~(0x7 << 0);
  164. tmp |= (exynos4210_apll_pms_table[new_index] & 0x7);
  165. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  166. /* 2. Change the system clock divider values */
  167. exynos4210_set_clkdiv(new_index);
  168. } else {
  169. /* Clock Configuration Procedure */
  170. /* 1. Change the apll m,p,s value */
  171. exynos4210_set_apll(new_index);
  172. /* 2. Change the system clock divider values */
  173. exynos4210_set_clkdiv(new_index);
  174. }
  175. }
  176. }
  177. int exynos4210_cpufreq_init(struct exynos_dvfs_info *info)
  178. {
  179. int i;
  180. unsigned int tmp;
  181. unsigned long rate;
  182. cpu_clk = clk_get(NULL, "armclk");
  183. if (IS_ERR(cpu_clk))
  184. return PTR_ERR(cpu_clk);
  185. moutcore = clk_get(NULL, "moutcore");
  186. if (IS_ERR(moutcore))
  187. goto err_moutcore;
  188. mout_mpll = clk_get(NULL, "mout_mpll");
  189. if (IS_ERR(mout_mpll))
  190. goto err_mout_mpll;
  191. rate = clk_get_rate(mout_mpll) / 1000;
  192. mout_apll = clk_get(NULL, "mout_apll");
  193. if (IS_ERR(mout_apll))
  194. goto err_mout_apll;
  195. tmp = __raw_readl(EXYNOS4_CLKDIV_CPU);
  196. for (i = L0; i < CPUFREQ_LEVEL_END; i++) {
  197. tmp &= ~(EXYNOS4_CLKDIV_CPU0_CORE_MASK |
  198. EXYNOS4_CLKDIV_CPU0_COREM0_MASK |
  199. EXYNOS4_CLKDIV_CPU0_COREM1_MASK |
  200. EXYNOS4_CLKDIV_CPU0_PERIPH_MASK |
  201. EXYNOS4_CLKDIV_CPU0_ATB_MASK |
  202. EXYNOS4_CLKDIV_CPU0_PCLKDBG_MASK |
  203. EXYNOS4_CLKDIV_CPU0_APLL_MASK);
  204. tmp |= ((clkdiv_cpu0[i][0] << EXYNOS4_CLKDIV_CPU0_CORE_SHIFT) |
  205. (clkdiv_cpu0[i][1] << EXYNOS4_CLKDIV_CPU0_COREM0_SHIFT) |
  206. (clkdiv_cpu0[i][2] << EXYNOS4_CLKDIV_CPU0_COREM1_SHIFT) |
  207. (clkdiv_cpu0[i][3] << EXYNOS4_CLKDIV_CPU0_PERIPH_SHIFT) |
  208. (clkdiv_cpu0[i][4] << EXYNOS4_CLKDIV_CPU0_ATB_SHIFT) |
  209. (clkdiv_cpu0[i][5] << EXYNOS4_CLKDIV_CPU0_PCLKDBG_SHIFT) |
  210. (clkdiv_cpu0[i][6] << EXYNOS4_CLKDIV_CPU0_APLL_SHIFT));
  211. exynos4210_clkdiv_table[i].clkdiv = tmp;
  212. }
  213. info->mpll_freq_khz = rate;
  214. info->pll_safe_idx = L2;
  215. info->cpu_clk = cpu_clk;
  216. info->volt_table = exynos4210_volt_table;
  217. info->freq_table = exynos4210_freq_table;
  218. info->set_freq = exynos4210_set_frequency;
  219. info->need_apll_change = exynos4210_pms_change;
  220. return 0;
  221. err_mout_apll:
  222. clk_put(mout_mpll);
  223. err_mout_mpll:
  224. clk_put(moutcore);
  225. err_moutcore:
  226. clk_put(cpu_clk);
  227. pr_debug("%s: failed initialization\n", __func__);
  228. return -EINVAL;
  229. }
  230. EXPORT_SYMBOL(exynos4210_cpufreq_init);