s3c2412-cpufreq.c 6.0 KB

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