exynos4210-cpufreq.c 7.4 KB

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