exynos4x12-cpufreq.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2010-2012 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * EXYNOS4X12 - 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 "exynos-cpufreq.h"
  20. static struct clk *cpu_clk;
  21. static struct clk *moutcore;
  22. static struct clk *mout_mpll;
  23. static struct clk *mout_apll;
  24. static unsigned int exynos4x12_volt_table[] = {
  25. 1350000, 1287500, 1250000, 1187500, 1137500, 1087500, 1037500,
  26. 1000000, 987500, 975000, 950000, 925000, 900000, 900000
  27. };
  28. static struct cpufreq_frequency_table exynos4x12_freq_table[] = {
  29. {L0, CPUFREQ_ENTRY_INVALID},
  30. {L1, 1400 * 1000},
  31. {L2, 1300 * 1000},
  32. {L3, 1200 * 1000},
  33. {L4, 1100 * 1000},
  34. {L5, 1000 * 1000},
  35. {L6, 900 * 1000},
  36. {L7, 800 * 1000},
  37. {L8, 700 * 1000},
  38. {L9, 600 * 1000},
  39. {L10, 500 * 1000},
  40. {L11, 400 * 1000},
  41. {L12, 300 * 1000},
  42. {L13, 200 * 1000},
  43. {0, CPUFREQ_TABLE_END},
  44. };
  45. static struct apll_freq *apll_freq_4x12;
  46. static struct apll_freq apll_freq_4212[] = {
  47. /*
  48. * values:
  49. * freq
  50. * clock divider for CORE, COREM0, COREM1, PERIPH, ATB, PCLK_DBG, APLL, CORE2
  51. * clock divider for COPY, HPM, RESERVED
  52. * PLL M, P, S
  53. */
  54. APLL_FREQ(1500, 0, 3, 7, 0, 6, 1, 2, 0, 6, 2, 0, 250, 4, 0),
  55. APLL_FREQ(1400, 0, 3, 7, 0, 6, 1, 2, 0, 6, 2, 0, 175, 3, 0),
  56. APLL_FREQ(1300, 0, 3, 7, 0, 5, 1, 2, 0, 5, 2, 0, 325, 6, 0),
  57. APLL_FREQ(1200, 0, 3, 7, 0, 5, 1, 2, 0, 5, 2, 0, 200, 4, 0),
  58. APLL_FREQ(1100, 0, 3, 6, 0, 4, 1, 2, 0, 4, 2, 0, 275, 6, 0),
  59. APLL_FREQ(1000, 0, 2, 5, 0, 4, 1, 1, 0, 4, 2, 0, 125, 3, 0),
  60. APLL_FREQ(900, 0, 2, 5, 0, 3, 1, 1, 0, 3, 2, 0, 150, 4, 0),
  61. APLL_FREQ(800, 0, 2, 5, 0, 3, 1, 1, 0, 3, 2, 0, 100, 3, 0),
  62. APLL_FREQ(700, 0, 2, 4, 0, 3, 1, 1, 0, 3, 2, 0, 175, 3, 1),
  63. APLL_FREQ(600, 0, 2, 4, 0, 3, 1, 1, 0, 3, 2, 0, 200, 4, 1),
  64. APLL_FREQ(500, 0, 2, 4, 0, 3, 1, 1, 0, 3, 2, 0, 125, 3, 1),
  65. APLL_FREQ(400, 0, 2, 4, 0, 3, 1, 1, 0, 3, 2, 0, 100, 3, 1),
  66. APLL_FREQ(300, 0, 2, 4, 0, 2, 1, 1, 0, 3, 2, 0, 200, 4, 2),
  67. APLL_FREQ(200, 0, 1, 3, 0, 1, 1, 1, 0, 3, 2, 0, 100, 3, 2),
  68. };
  69. static struct apll_freq apll_freq_4412[] = {
  70. /*
  71. * values:
  72. * freq
  73. * clock divider for CORE, COREM0, COREM1, PERIPH, ATB, PCLK_DBG, APLL, CORE2
  74. * clock divider for COPY, HPM, CORES
  75. * PLL M, P, S
  76. */
  77. APLL_FREQ(1500, 0, 3, 7, 0, 6, 1, 2, 0, 6, 0, 7, 250, 4, 0),
  78. APLL_FREQ(1400, 0, 3, 7, 0, 6, 1, 2, 0, 6, 0, 6, 175, 3, 0),
  79. APLL_FREQ(1300, 0, 3, 7, 0, 5, 1, 2, 0, 5, 0, 6, 325, 6, 0),
  80. APLL_FREQ(1200, 0, 3, 7, 0, 5, 1, 2, 0, 5, 0, 5, 200, 4, 0),
  81. APLL_FREQ(1100, 0, 3, 6, 0, 4, 1, 2, 0, 4, 0, 5, 275, 6, 0),
  82. APLL_FREQ(1000, 0, 2, 5, 0, 4, 1, 1, 0, 4, 0, 4, 125, 3, 0),
  83. APLL_FREQ(900, 0, 2, 5, 0, 3, 1, 1, 0, 3, 0, 4, 150, 4, 0),
  84. APLL_FREQ(800, 0, 2, 5, 0, 3, 1, 1, 0, 3, 0, 3, 100, 3, 0),
  85. APLL_FREQ(700, 0, 2, 4, 0, 3, 1, 1, 0, 3, 0, 3, 175, 3, 1),
  86. APLL_FREQ(600, 0, 2, 4, 0, 3, 1, 1, 0, 3, 0, 2, 200, 4, 1),
  87. APLL_FREQ(500, 0, 2, 4, 0, 3, 1, 1, 0, 3, 0, 2, 125, 3, 1),
  88. APLL_FREQ(400, 0, 2, 4, 0, 3, 1, 1, 0, 3, 0, 1, 100, 3, 1),
  89. APLL_FREQ(300, 0, 2, 4, 0, 2, 1, 1, 0, 3, 0, 1, 200, 4, 2),
  90. APLL_FREQ(200, 0, 1, 3, 0, 1, 1, 1, 0, 3, 0, 0, 100, 3, 2),
  91. };
  92. static void exynos4x12_set_clkdiv(unsigned int div_index)
  93. {
  94. unsigned int tmp;
  95. unsigned int stat_cpu1;
  96. /* Change Divider - CPU0 */
  97. tmp = apll_freq_4x12[div_index].clk_div_cpu0;
  98. __raw_writel(tmp, EXYNOS4_CLKDIV_CPU);
  99. while (__raw_readl(EXYNOS4_CLKDIV_STATCPU) & 0x11111111)
  100. cpu_relax();
  101. /* Change Divider - CPU1 */
  102. tmp = apll_freq_4x12[div_index].clk_div_cpu1;
  103. __raw_writel(tmp, EXYNOS4_CLKDIV_CPU1);
  104. if (soc_is_exynos4212())
  105. stat_cpu1 = 0x11;
  106. else
  107. stat_cpu1 = 0x111;
  108. while (__raw_readl(EXYNOS4_CLKDIV_STATCPU1) & stat_cpu1)
  109. cpu_relax();
  110. }
  111. static void exynos4x12_set_apll(unsigned int index)
  112. {
  113. unsigned int tmp, pdiv;
  114. /* 1. MUX_CORE_SEL = MPLL, ARMCLK uses MPLL for lock time */
  115. clk_set_parent(moutcore, mout_mpll);
  116. do {
  117. cpu_relax();
  118. tmp = (__raw_readl(EXYNOS4_CLKMUX_STATCPU)
  119. >> EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT);
  120. tmp &= 0x7;
  121. } while (tmp != 0x2);
  122. /* 2. Set APLL Lock time */
  123. pdiv = ((apll_freq_4x12[index].mps >> 8) & 0x3f);
  124. __raw_writel((pdiv * 250), EXYNOS4_APLL_LOCK);
  125. /* 3. Change PLL PMS values */
  126. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  127. tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0));
  128. tmp |= apll_freq_4x12[index].mps;
  129. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  130. /* 4. wait_lock_time */
  131. do {
  132. cpu_relax();
  133. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  134. } while (!(tmp & (0x1 << EXYNOS4_APLLCON0_LOCKED_SHIFT)));
  135. /* 5. MUX_CORE_SEL = APLL */
  136. clk_set_parent(moutcore, mout_apll);
  137. do {
  138. cpu_relax();
  139. tmp = __raw_readl(EXYNOS4_CLKMUX_STATCPU);
  140. tmp &= EXYNOS4_CLKMUX_STATCPU_MUXCORE_MASK;
  141. } while (tmp != (0x1 << EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT));
  142. }
  143. static bool exynos4x12_pms_change(unsigned int old_index, unsigned int new_index)
  144. {
  145. unsigned int old_pm = apll_freq_4x12[old_index].mps >> 8;
  146. unsigned int new_pm = apll_freq_4x12[new_index].mps >> 8;
  147. return (old_pm == new_pm) ? 0 : 1;
  148. }
  149. static void exynos4x12_set_frequency(unsigned int old_index,
  150. unsigned int new_index)
  151. {
  152. unsigned int tmp;
  153. if (old_index > new_index) {
  154. if (!exynos4x12_pms_change(old_index, new_index)) {
  155. /* 1. Change the system clock divider values */
  156. exynos4x12_set_clkdiv(new_index);
  157. /* 2. Change just s value in apll m,p,s value */
  158. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  159. tmp &= ~(0x7 << 0);
  160. tmp |= apll_freq_4x12[new_index].mps & 0x7;
  161. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  162. } else {
  163. /* Clock Configuration Procedure */
  164. /* 1. Change the system clock divider values */
  165. exynos4x12_set_clkdiv(new_index);
  166. /* 2. Change the apll m,p,s value */
  167. exynos4x12_set_apll(new_index);
  168. }
  169. } else if (old_index < new_index) {
  170. if (!exynos4x12_pms_change(old_index, new_index)) {
  171. /* 1. Change just s value in apll m,p,s value */
  172. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  173. tmp &= ~(0x7 << 0);
  174. tmp |= apll_freq_4x12[new_index].mps & 0x7;
  175. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  176. /* 2. Change the system clock divider values */
  177. exynos4x12_set_clkdiv(new_index);
  178. } else {
  179. /* Clock Configuration Procedure */
  180. /* 1. Change the apll m,p,s value */
  181. exynos4x12_set_apll(new_index);
  182. /* 2. Change the system clock divider values */
  183. exynos4x12_set_clkdiv(new_index);
  184. }
  185. }
  186. }
  187. int exynos4x12_cpufreq_init(struct exynos_dvfs_info *info)
  188. {
  189. unsigned long rate;
  190. cpu_clk = clk_get(NULL, "armclk");
  191. if (IS_ERR(cpu_clk))
  192. return PTR_ERR(cpu_clk);
  193. moutcore = clk_get(NULL, "moutcore");
  194. if (IS_ERR(moutcore))
  195. goto err_moutcore;
  196. mout_mpll = clk_get(NULL, "mout_mpll");
  197. if (IS_ERR(mout_mpll))
  198. goto err_mout_mpll;
  199. rate = clk_get_rate(mout_mpll) / 1000;
  200. mout_apll = clk_get(NULL, "mout_apll");
  201. if (IS_ERR(mout_apll))
  202. goto err_mout_apll;
  203. if (soc_is_exynos4212())
  204. apll_freq_4x12 = apll_freq_4212;
  205. else
  206. apll_freq_4x12 = apll_freq_4412;
  207. info->mpll_freq_khz = rate;
  208. /* 800Mhz */
  209. info->pll_safe_idx = L7;
  210. info->cpu_clk = cpu_clk;
  211. info->volt_table = exynos4x12_volt_table;
  212. info->freq_table = exynos4x12_freq_table;
  213. info->set_freq = exynos4x12_set_frequency;
  214. info->need_apll_change = exynos4x12_pms_change;
  215. return 0;
  216. err_mout_apll:
  217. clk_put(mout_mpll);
  218. err_mout_mpll:
  219. clk_put(moutcore);
  220. err_moutcore:
  221. clk_put(cpu_clk);
  222. pr_debug("%s: failed initialization\n", __func__);
  223. return -EINVAL;
  224. }
  225. EXPORT_SYMBOL(exynos4x12_cpufreq_init);