exynos4210-cpufreq.c 7.3 KB

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