exynos4210-cpufreq.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "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 exynos4210_volt_table[] = {
  25. 1250000, 1150000, 1050000, 975000, 950000,
  26. };
  27. static struct cpufreq_frequency_table exynos4210_freq_table[] = {
  28. {L0, 1200 * 1000},
  29. {L1, 1000 * 1000},
  30. {L2, 800 * 1000},
  31. {L3, 500 * 1000},
  32. {L4, 200 * 1000},
  33. {0, CPUFREQ_TABLE_END},
  34. };
  35. static struct apll_freq apll_freq_4210[] = {
  36. /*
  37. * values:
  38. * freq
  39. * clock divider for CORE, COREM0, COREM1, PERIPH, ATB, PCLK_DBG, APLL, RESERVED
  40. * clock divider for COPY, HPM, RESERVED
  41. * PLL M, P, S
  42. */
  43. APLL_FREQ(1200, 0, 3, 7, 3, 4, 1, 7, 0, 5, 0, 0, 150, 3, 1),
  44. APLL_FREQ(1000, 0, 3, 7, 3, 4, 1, 7, 0, 4, 0, 0, 250, 6, 1),
  45. APLL_FREQ(800, 0, 3, 7, 3, 3, 1, 7, 0, 3, 0, 0, 200, 6, 1),
  46. APLL_FREQ(500, 0, 3, 7, 3, 3, 1, 7, 0, 3, 0, 0, 250, 6, 2),
  47. APLL_FREQ(200, 0, 1, 3, 1, 3, 1, 0, 0, 3, 0, 0, 200, 6, 3),
  48. };
  49. static void exynos4210_set_clkdiv(unsigned int div_index)
  50. {
  51. unsigned int tmp;
  52. /* Change Divider - CPU0 */
  53. tmp = apll_freq_4210[div_index].clk_div_cpu0;
  54. __raw_writel(tmp, EXYNOS4_CLKDIV_CPU);
  55. do {
  56. tmp = __raw_readl(EXYNOS4_CLKDIV_STATCPU);
  57. } while (tmp & 0x1111111);
  58. /* Change Divider - CPU1 */
  59. tmp = apll_freq_4210[div_index].clk_div_cpu1;
  60. __raw_writel(tmp, EXYNOS4_CLKDIV_CPU1);
  61. do {
  62. tmp = __raw_readl(EXYNOS4_CLKDIV_STATCPU1);
  63. } while (tmp & 0x11);
  64. }
  65. static void exynos4210_set_apll(unsigned int index)
  66. {
  67. unsigned int tmp;
  68. /* 1. MUX_CORE_SEL = MPLL, ARMCLK uses MPLL for lock time */
  69. clk_set_parent(moutcore, mout_mpll);
  70. do {
  71. tmp = (__raw_readl(EXYNOS4_CLKMUX_STATCPU)
  72. >> EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT);
  73. tmp &= 0x7;
  74. } while (tmp != 0x2);
  75. /* 2. Set APLL Lock time */
  76. __raw_writel(EXYNOS4_APLL_LOCKTIME, EXYNOS4_APLL_LOCK);
  77. /* 3. Change PLL PMS values */
  78. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  79. tmp &= ~((0x3ff << 16) | (0x3f << 8) | (0x7 << 0));
  80. tmp |= apll_freq_4210[index].mps;
  81. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  82. /* 4. wait_lock_time */
  83. do {
  84. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  85. } while (!(tmp & (0x1 << EXYNOS4_APLLCON0_LOCKED_SHIFT)));
  86. /* 5. MUX_CORE_SEL = APLL */
  87. clk_set_parent(moutcore, mout_apll);
  88. do {
  89. tmp = __raw_readl(EXYNOS4_CLKMUX_STATCPU);
  90. tmp &= EXYNOS4_CLKMUX_STATCPU_MUXCORE_MASK;
  91. } while (tmp != (0x1 << EXYNOS4_CLKSRC_CPU_MUXCORE_SHIFT));
  92. }
  93. static bool exynos4210_pms_change(unsigned int old_index, unsigned int new_index)
  94. {
  95. unsigned int old_pm = apll_freq_4210[old_index].mps >> 8;
  96. unsigned int new_pm = apll_freq_4210[new_index].mps >> 8;
  97. return (old_pm == new_pm) ? 0 : 1;
  98. }
  99. static void exynos4210_set_frequency(unsigned int old_index,
  100. unsigned int new_index)
  101. {
  102. unsigned int tmp;
  103. if (old_index > new_index) {
  104. if (!exynos4210_pms_change(old_index, new_index)) {
  105. /* 1. Change the system clock divider values */
  106. exynos4210_set_clkdiv(new_index);
  107. /* 2. Change just s value in apll m,p,s value */
  108. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  109. tmp &= ~(0x7 << 0);
  110. tmp |= apll_freq_4210[new_index].mps & 0x7;
  111. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  112. } else {
  113. /* Clock Configuration Procedure */
  114. /* 1. Change the system clock divider values */
  115. exynos4210_set_clkdiv(new_index);
  116. /* 2. Change the apll m,p,s value */
  117. exynos4210_set_apll(new_index);
  118. }
  119. } else if (old_index < new_index) {
  120. if (!exynos4210_pms_change(old_index, new_index)) {
  121. /* 1. Change just s value in apll m,p,s value */
  122. tmp = __raw_readl(EXYNOS4_APLL_CON0);
  123. tmp &= ~(0x7 << 0);
  124. tmp |= apll_freq_4210[new_index].mps & 0x7;
  125. __raw_writel(tmp, EXYNOS4_APLL_CON0);
  126. /* 2. Change the system clock divider values */
  127. exynos4210_set_clkdiv(new_index);
  128. } else {
  129. /* Clock Configuration Procedure */
  130. /* 1. Change the apll m,p,s value */
  131. exynos4210_set_apll(new_index);
  132. /* 2. Change the system clock divider values */
  133. exynos4210_set_clkdiv(new_index);
  134. }
  135. }
  136. }
  137. int exynos4210_cpufreq_init(struct exynos_dvfs_info *info)
  138. {
  139. unsigned long rate;
  140. cpu_clk = clk_get(NULL, "armclk");
  141. if (IS_ERR(cpu_clk))
  142. return PTR_ERR(cpu_clk);
  143. moutcore = clk_get(NULL, "moutcore");
  144. if (IS_ERR(moutcore))
  145. goto err_moutcore;
  146. mout_mpll = clk_get(NULL, "mout_mpll");
  147. if (IS_ERR(mout_mpll))
  148. goto err_mout_mpll;
  149. rate = clk_get_rate(mout_mpll) / 1000;
  150. mout_apll = clk_get(NULL, "mout_apll");
  151. if (IS_ERR(mout_apll))
  152. goto err_mout_apll;
  153. info->mpll_freq_khz = rate;
  154. /* 800Mhz */
  155. info->pll_safe_idx = L2;
  156. info->cpu_clk = cpu_clk;
  157. info->volt_table = exynos4210_volt_table;
  158. info->freq_table = exynos4210_freq_table;
  159. info->set_freq = exynos4210_set_frequency;
  160. info->need_apll_change = exynos4210_pms_change;
  161. return 0;
  162. err_mout_apll:
  163. clk_put(mout_mpll);
  164. err_mout_mpll:
  165. clk_put(moutcore);
  166. err_moutcore:
  167. clk_put(cpu_clk);
  168. pr_debug("%s: failed initialization\n", __func__);
  169. return -EINVAL;
  170. }
  171. EXPORT_SYMBOL(exynos4210_cpufreq_init);