exynos4x12-cpufreq.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <linux/of.h>
  19. #include <linux/of_address.h>
  20. #include "exynos-cpufreq.h"
  21. static struct clk *cpu_clk;
  22. static struct clk *moutcore;
  23. static struct clk *mout_mpll;
  24. static struct clk *mout_apll;
  25. static struct exynos_dvfs_info *cpufreq;
  26. static unsigned int exynos4x12_volt_table[] = {
  27. 1350000, 1287500, 1250000, 1187500, 1137500, 1087500, 1037500,
  28. 1000000, 987500, 975000, 950000, 925000, 900000, 900000
  29. };
  30. static struct cpufreq_frequency_table exynos4x12_freq_table[] = {
  31. {CPUFREQ_BOOST_FREQ, L0, 1500 * 1000},
  32. {0, L1, 1400 * 1000},
  33. {0, L2, 1300 * 1000},
  34. {0, L3, 1200 * 1000},
  35. {0, L4, 1100 * 1000},
  36. {0, L5, 1000 * 1000},
  37. {0, L6, 900 * 1000},
  38. {0, L7, 800 * 1000},
  39. {0, L8, 700 * 1000},
  40. {0, L9, 600 * 1000},
  41. {0, L10, 500 * 1000},
  42. {0, L11, 400 * 1000},
  43. {0, L12, 300 * 1000},
  44. {0, L13, 200 * 1000},
  45. {0, 0, CPUFREQ_TABLE_END},
  46. };
  47. static struct apll_freq *apll_freq_4x12;
  48. static struct apll_freq apll_freq_4212[] = {
  49. /*
  50. * values:
  51. * freq
  52. * clock divider for CORE, COREM0, COREM1, PERIPH, ATB, PCLK_DBG, APLL, CORE2
  53. * clock divider for COPY, HPM, RESERVED
  54. * PLL M, P, S
  55. */
  56. APLL_FREQ(1500, 0, 3, 7, 0, 6, 1, 2, 0, 6, 2, 0, 250, 4, 0),
  57. APLL_FREQ(1400, 0, 3, 7, 0, 6, 1, 2, 0, 6, 2, 0, 175, 3, 0),
  58. APLL_FREQ(1300, 0, 3, 7, 0, 5, 1, 2, 0, 5, 2, 0, 325, 6, 0),
  59. APLL_FREQ(1200, 0, 3, 7, 0, 5, 1, 2, 0, 5, 2, 0, 200, 4, 0),
  60. APLL_FREQ(1100, 0, 3, 6, 0, 4, 1, 2, 0, 4, 2, 0, 275, 6, 0),
  61. APLL_FREQ(1000, 0, 2, 5, 0, 4, 1, 1, 0, 4, 2, 0, 125, 3, 0),
  62. APLL_FREQ(900, 0, 2, 5, 0, 3, 1, 1, 0, 3, 2, 0, 150, 4, 0),
  63. APLL_FREQ(800, 0, 2, 5, 0, 3, 1, 1, 0, 3, 2, 0, 100, 3, 0),
  64. APLL_FREQ(700, 0, 2, 4, 0, 3, 1, 1, 0, 3, 2, 0, 175, 3, 1),
  65. APLL_FREQ(600, 0, 2, 4, 0, 3, 1, 1, 0, 3, 2, 0, 200, 4, 1),
  66. APLL_FREQ(500, 0, 2, 4, 0, 3, 1, 1, 0, 3, 2, 0, 125, 3, 1),
  67. APLL_FREQ(400, 0, 2, 4, 0, 3, 1, 1, 0, 3, 2, 0, 100, 3, 1),
  68. APLL_FREQ(300, 0, 2, 4, 0, 2, 1, 1, 0, 3, 2, 0, 200, 4, 2),
  69. APLL_FREQ(200, 0, 1, 3, 0, 1, 1, 1, 0, 3, 2, 0, 100, 3, 2),
  70. };
  71. static struct apll_freq apll_freq_4412[] = {
  72. /*
  73. * values:
  74. * freq
  75. * clock divider for CORE, COREM0, COREM1, PERIPH, ATB, PCLK_DBG, APLL, CORE2
  76. * clock divider for COPY, HPM, CORES
  77. * PLL M, P, S
  78. */
  79. APLL_FREQ(1500, 0, 3, 7, 0, 6, 1, 2, 0, 6, 0, 7, 250, 4, 0),
  80. APLL_FREQ(1400, 0, 3, 7, 0, 6, 1, 2, 0, 6, 0, 6, 175, 3, 0),
  81. APLL_FREQ(1300, 0, 3, 7, 0, 5, 1, 2, 0, 5, 0, 6, 325, 6, 0),
  82. APLL_FREQ(1200, 0, 3, 7, 0, 5, 1, 2, 0, 5, 0, 5, 200, 4, 0),
  83. APLL_FREQ(1100, 0, 3, 6, 0, 4, 1, 2, 0, 4, 0, 5, 275, 6, 0),
  84. APLL_FREQ(1000, 0, 2, 5, 0, 4, 1, 1, 0, 4, 0, 4, 125, 3, 0),
  85. APLL_FREQ(900, 0, 2, 5, 0, 3, 1, 1, 0, 3, 0, 4, 150, 4, 0),
  86. APLL_FREQ(800, 0, 2, 5, 0, 3, 1, 1, 0, 3, 0, 3, 100, 3, 0),
  87. APLL_FREQ(700, 0, 2, 4, 0, 3, 1, 1, 0, 3, 0, 3, 175, 3, 1),
  88. APLL_FREQ(600, 0, 2, 4, 0, 3, 1, 1, 0, 3, 0, 2, 200, 4, 1),
  89. APLL_FREQ(500, 0, 2, 4, 0, 3, 1, 1, 0, 3, 0, 2, 125, 3, 1),
  90. APLL_FREQ(400, 0, 2, 4, 0, 3, 1, 1, 0, 3, 0, 1, 100, 3, 1),
  91. APLL_FREQ(300, 0, 2, 4, 0, 2, 1, 1, 0, 3, 0, 1, 200, 4, 2),
  92. APLL_FREQ(200, 0, 1, 3, 0, 1, 1, 1, 0, 3, 0, 0, 100, 3, 2),
  93. };
  94. static void exynos4x12_set_clkdiv(unsigned int div_index)
  95. {
  96. unsigned int tmp;
  97. /* Change Divider - CPU0 */
  98. tmp = apll_freq_4x12[div_index].clk_div_cpu0;
  99. __raw_writel(tmp, cpufreq->cmu_regs + EXYNOS4_CLKDIV_CPU);
  100. while (__raw_readl(cpufreq->cmu_regs + EXYNOS4_CLKDIV_STATCPU)
  101. & 0x11111111)
  102. cpu_relax();
  103. /* Change Divider - CPU1 */
  104. tmp = apll_freq_4x12[div_index].clk_div_cpu1;
  105. __raw_writel(tmp, cpufreq->cmu_regs + EXYNOS4_CLKDIV_CPU1);
  106. do {
  107. cpu_relax();
  108. tmp = __raw_readl(cpufreq->cmu_regs + EXYNOS4_CLKDIV_STATCPU1);
  109. } while (tmp != 0x0);
  110. }
  111. static void exynos4x12_set_apll(unsigned int index)
  112. {
  113. unsigned int tmp, freq = apll_freq_4x12[index].freq;
  114. /* 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(cpufreq->cmu_regs + EXYNOS4_CLKMUX_STATCPU)
  119. >> EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT);
  120. tmp &= 0x7;
  121. } while (tmp != 0x2);
  122. clk_set_rate(mout_apll, freq * 1000);
  123. /* MUX_CORE_SEL = APLL */
  124. clk_set_parent(moutcore, mout_apll);
  125. do {
  126. cpu_relax();
  127. tmp = __raw_readl(cpufreq->cmu_regs + EXYNOS4_CLKMUX_STATCPU);
  128. tmp &= EXYNOS4_CLKMUX_STATCPU_MUXCORE_MASK;
  129. } while (tmp != (0x1 << EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT));
  130. }
  131. static void exynos4x12_set_frequency(unsigned int old_index,
  132. unsigned int new_index)
  133. {
  134. if (old_index > new_index) {
  135. exynos4x12_set_clkdiv(new_index);
  136. exynos4x12_set_apll(new_index);
  137. } else if (old_index < new_index) {
  138. exynos4x12_set_apll(new_index);
  139. exynos4x12_set_clkdiv(new_index);
  140. }
  141. }
  142. int exynos4x12_cpufreq_init(struct exynos_dvfs_info *info)
  143. {
  144. struct device_node *np;
  145. unsigned long rate;
  146. /*
  147. * HACK: This is a temporary workaround to get access to clock
  148. * controller registers directly and remove static mappings and
  149. * dependencies on platform headers. It is necessary to enable
  150. * Exynos multi-platform support and will be removed together with
  151. * this whole driver as soon as Exynos gets migrated to use
  152. * cpufreq-dt driver.
  153. */
  154. np = of_find_compatible_node(NULL, NULL, "samsung,exynos4412-clock");
  155. if (!np) {
  156. pr_err("%s: failed to find clock controller DT node\n",
  157. __func__);
  158. return -ENODEV;
  159. }
  160. info->cmu_regs = of_iomap(np, 0);
  161. if (!info->cmu_regs) {
  162. pr_err("%s: failed to map CMU registers\n", __func__);
  163. return -EFAULT;
  164. }
  165. cpu_clk = clk_get(NULL, "armclk");
  166. if (IS_ERR(cpu_clk))
  167. return PTR_ERR(cpu_clk);
  168. moutcore = clk_get(NULL, "moutcore");
  169. if (IS_ERR(moutcore))
  170. goto err_moutcore;
  171. mout_mpll = clk_get(NULL, "mout_mpll");
  172. if (IS_ERR(mout_mpll))
  173. goto err_mout_mpll;
  174. rate = clk_get_rate(mout_mpll) / 1000;
  175. mout_apll = clk_get(NULL, "mout_apll");
  176. if (IS_ERR(mout_apll))
  177. goto err_mout_apll;
  178. if (info->type == EXYNOS_SOC_4212)
  179. apll_freq_4x12 = apll_freq_4212;
  180. else
  181. apll_freq_4x12 = apll_freq_4412;
  182. info->mpll_freq_khz = rate;
  183. /* 800Mhz */
  184. info->pll_safe_idx = L7;
  185. info->cpu_clk = cpu_clk;
  186. info->volt_table = exynos4x12_volt_table;
  187. info->freq_table = exynos4x12_freq_table;
  188. info->set_freq = exynos4x12_set_frequency;
  189. cpufreq = info;
  190. return 0;
  191. err_mout_apll:
  192. clk_put(mout_mpll);
  193. err_mout_mpll:
  194. clk_put(moutcore);
  195. err_moutcore:
  196. clk_put(cpu_clk);
  197. pr_debug("%s: failed initialization\n", __func__);
  198. return -EINVAL;
  199. }