cpu-freq.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* linux/arch/arm/mach-s3c2412/cpu-freq.c
  2. *
  3. * Copyright 2008 Simtec Electronics
  4. * http://armlinux.simtec.co.uk/
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * S3C2412 CPU Frequency scalling
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/ioport.h>
  17. #include <linux/cpufreq.h>
  18. #include <linux/sysdev.h>
  19. #include <linux/delay.h>
  20. #include <linux/clk.h>
  21. #include <linux/err.h>
  22. #include <linux/io.h>
  23. #include <asm/mach/arch.h>
  24. #include <asm/mach/map.h>
  25. #include <mach/regs-clock.h>
  26. #include <mach/regs-s3c2412-mem.h>
  27. #include <plat/cpu.h>
  28. #include <plat/clock.h>
  29. #include <plat/cpu-freq-core.h>
  30. /* our clock resources. */
  31. static struct clk *xtal;
  32. static struct clk *fclk;
  33. static struct clk *hclk;
  34. static struct clk *armclk;
  35. /* HDIV: 1, 2, 3, 4, 6, 8 */
  36. static int s3c2412_cpufreq_calcdivs(struct s3c_cpufreq_config *cfg)
  37. {
  38. unsigned int hdiv, pdiv, armdiv, dvs;
  39. unsigned long hclk, fclk, armclk, armdiv_clk;
  40. unsigned long hclk_max;
  41. fclk = cfg->freq.fclk;
  42. armclk = cfg->freq.armclk;
  43. hclk_max = cfg->max.hclk;
  44. /* We can't run hclk above armclk as at the best we have to
  45. * have armclk and hclk in dvs mode. */
  46. if (hclk_max > armclk)
  47. hclk_max = armclk;
  48. s3c_freq_dbg("%s: fclk=%lu, armclk=%lu, hclk_max=%lu\n",
  49. __func__, fclk, armclk, hclk_max);
  50. s3c_freq_dbg("%s: want f=%lu, arm=%lu, h=%lu, p=%lu\n",
  51. __func__, cfg->freq.fclk, cfg->freq.armclk,
  52. cfg->freq.hclk, cfg->freq.pclk);
  53. armdiv = fclk / armclk;
  54. if (armdiv < 1)
  55. armdiv = 1;
  56. if (armdiv > 2)
  57. armdiv = 2;
  58. cfg->divs.arm_divisor = armdiv;
  59. armdiv_clk = fclk / armdiv;
  60. hdiv = armdiv_clk / hclk_max;
  61. if (hdiv < 1)
  62. hdiv = 1;
  63. cfg->freq.hclk = hclk = armdiv_clk / hdiv;
  64. /* set dvs depending on whether we reached armclk or not. */
  65. cfg->divs.dvs = dvs = armclk < armdiv_clk;
  66. /* update the actual armclk we achieved. */
  67. cfg->freq.armclk = dvs ? hclk : armdiv_clk;
  68. s3c_freq_dbg("%s: armclk %lu, hclk %lu, armdiv %d, hdiv %d, dvs %d\n",
  69. __func__, armclk, hclk, armdiv, hdiv, cfg->divs.dvs);
  70. if (hdiv > 4)
  71. goto invalid;
  72. pdiv = (hclk > cfg->max.pclk) ? 2 : 1;
  73. if ((hclk / pdiv) > cfg->max.pclk)
  74. pdiv++;
  75. cfg->freq.pclk = hclk / pdiv;
  76. s3c_freq_dbg("%s: pdiv %d\n", __func__, pdiv);
  77. if (pdiv > 2)
  78. goto invalid;
  79. pdiv *= hdiv;
  80. /* store the result, and then return */
  81. cfg->divs.h_divisor = hdiv * armdiv;
  82. cfg->divs.p_divisor = pdiv * armdiv;
  83. return 0;
  84. invalid:
  85. return -EINVAL;
  86. }
  87. static void s3c2412_cpufreq_setdivs(struct s3c_cpufreq_config *cfg)
  88. {
  89. unsigned long clkdiv;
  90. unsigned long olddiv;
  91. olddiv = clkdiv = __raw_readl(S3C2410_CLKDIVN);
  92. /* clear off current clock info */
  93. clkdiv &= ~S3C2412_CLKDIVN_ARMDIVN;
  94. clkdiv &= ~S3C2412_CLKDIVN_HDIVN_MASK;
  95. clkdiv &= ~S3C2412_CLKDIVN_PDIVN;
  96. if (cfg->divs.arm_divisor == 2)
  97. clkdiv |= S3C2412_CLKDIVN_ARMDIVN;
  98. clkdiv |= ((cfg->divs.h_divisor / cfg->divs.arm_divisor) - 1);
  99. if (cfg->divs.p_divisor != cfg->divs.h_divisor)
  100. clkdiv |= S3C2412_CLKDIVN_PDIVN;
  101. s3c_freq_dbg("%s: div %08lx => %08lx\n", __func__, olddiv, clkdiv);
  102. __raw_writel(clkdiv, S3C2410_CLKDIVN);
  103. clk_set_parent(armclk, cfg->divs.dvs ? hclk : fclk);
  104. }
  105. static void s3c2412_cpufreq_setrefresh(struct s3c_cpufreq_config *cfg)
  106. {
  107. struct s3c_cpufreq_board *board = cfg->board;
  108. unsigned long refresh;
  109. s3c_freq_dbg("%s: refresh %u ns, hclk %lu\n", __func__,
  110. board->refresh, cfg->freq.hclk);
  111. /* Reduce both the refresh time (in ns) and the frequency (in MHz)
  112. * by 10 each to ensure that we do not overflow 32 bit numbers. This
  113. * should work for HCLK up to 133MHz and refresh period up to 30usec.
  114. */
  115. refresh = (board->refresh / 10);
  116. refresh *= (cfg->freq.hclk / 100);
  117. refresh /= (1 * 1000 * 1000); /* 10^6 */
  118. s3c_freq_dbg("%s: setting refresh 0x%08lx\n", __func__, refresh);
  119. __raw_writel(refresh, S3C2412_REFRESH);
  120. }
  121. /* set the default cpu frequency information, based on an 200MHz part
  122. * as we have no other way of detecting the speed rating in software.
  123. */
  124. static struct s3c_cpufreq_info s3c2412_cpufreq_info = {
  125. .max = {
  126. .fclk = 200000000,
  127. .hclk = 100000000,
  128. .pclk = 50000000,
  129. },
  130. .latency = 5000000, /* 5ms */
  131. .locktime_m = 150,
  132. .locktime_u = 150,
  133. .locktime_bits = 16,
  134. .name = "s3c2412",
  135. .set_refresh = s3c2412_cpufreq_setrefresh,
  136. .set_divs = s3c2412_cpufreq_setdivs,
  137. .calc_divs = s3c2412_cpufreq_calcdivs,
  138. .calc_iotiming = s3c2412_iotiming_calc,
  139. .set_iotiming = s3c2412_iotiming_set,
  140. .get_iotiming = s3c2412_iotiming_get,
  141. .resume_clocks = s3c2412_setup_clocks,
  142. .debug_io_show = s3c_cpufreq_debugfs_call(s3c2412_iotiming_debugfs),
  143. };
  144. static int s3c2412_cpufreq_add(struct sys_device *sysdev)
  145. {
  146. unsigned long fclk_rate;
  147. hclk = clk_get(NULL, "hclk");
  148. if (IS_ERR(hclk)) {
  149. printk(KERN_ERR "%s: cannot find hclk clock\n", __func__);
  150. return -ENOENT;
  151. }
  152. fclk = clk_get(NULL, "fclk");
  153. if (IS_ERR(fclk)) {
  154. printk(KERN_ERR "%s: cannot find fclk clock\n", __func__);
  155. goto err_fclk;
  156. }
  157. fclk_rate = clk_get_rate(fclk);
  158. if (fclk_rate > 200000000) {
  159. printk(KERN_INFO
  160. "%s: fclk %ld MHz, assuming 266MHz capable part\n",
  161. __func__, fclk_rate / 1000000);
  162. s3c2412_cpufreq_info.max.fclk = 266000000;
  163. s3c2412_cpufreq_info.max.hclk = 133000000;
  164. s3c2412_cpufreq_info.max.pclk = 66000000;
  165. }
  166. armclk = clk_get(NULL, "armclk");
  167. if (IS_ERR(armclk)) {
  168. printk(KERN_ERR "%s: cannot find arm clock\n", __func__);
  169. goto err_armclk;
  170. }
  171. xtal = clk_get(NULL, "xtal");
  172. if (IS_ERR(xtal)) {
  173. printk(KERN_ERR "%s: cannot find xtal clock\n", __func__);
  174. goto err_xtal;
  175. }
  176. return s3c_cpufreq_register(&s3c2412_cpufreq_info);
  177. err_xtal:
  178. clk_put(armclk);
  179. err_armclk:
  180. clk_put(fclk);
  181. err_fclk:
  182. clk_put(hclk);
  183. return -ENOENT;
  184. }
  185. static struct sysdev_driver s3c2412_cpufreq_driver = {
  186. .add = s3c2412_cpufreq_add,
  187. };
  188. static int s3c2412_cpufreq_init(void)
  189. {
  190. return sysdev_driver_register(&s3c2412_sysclass,
  191. &s3c2412_cpufreq_driver);
  192. }
  193. arch_initcall(s3c2412_cpufreq_init);