s3c2412-cpufreq.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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/device.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/s3c2412.h>
  27. #include <plat/cpu.h>
  28. #include <plat/cpu-freq-core.h>
  29. /* our clock resources. */
  30. static struct clk *xtal;
  31. static struct clk *fclk;
  32. static struct clk *hclk;
  33. static struct clk *armclk;
  34. /* HDIV: 1, 2, 3, 4, 6, 8 */
  35. static int s3c2412_cpufreq_calcdivs(struct s3c_cpufreq_config *cfg)
  36. {
  37. unsigned int hdiv, pdiv, armdiv, dvs;
  38. unsigned long hclk, fclk, armclk, armdiv_clk;
  39. unsigned long hclk_max;
  40. fclk = cfg->freq.fclk;
  41. armclk = cfg->freq.armclk;
  42. hclk_max = cfg->max.hclk;
  43. /* We can't run hclk above armclk as at the best we have to
  44. * have armclk and hclk in dvs mode. */
  45. if (hclk_max > armclk)
  46. hclk_max = armclk;
  47. s3c_freq_dbg("%s: fclk=%lu, armclk=%lu, hclk_max=%lu\n",
  48. __func__, fclk, armclk, hclk_max);
  49. s3c_freq_dbg("%s: want f=%lu, arm=%lu, h=%lu, p=%lu\n",
  50. __func__, cfg->freq.fclk, cfg->freq.armclk,
  51. cfg->freq.hclk, cfg->freq.pclk);
  52. armdiv = fclk / armclk;
  53. if (armdiv < 1)
  54. armdiv = 1;
  55. if (armdiv > 2)
  56. armdiv = 2;
  57. cfg->divs.arm_divisor = armdiv;
  58. armdiv_clk = fclk / armdiv;
  59. hdiv = armdiv_clk / hclk_max;
  60. if (hdiv < 1)
  61. hdiv = 1;
  62. cfg->freq.hclk = hclk = armdiv_clk / hdiv;
  63. /* set dvs depending on whether we reached armclk or not. */
  64. cfg->divs.dvs = dvs = armclk < armdiv_clk;
  65. /* update the actual armclk we achieved. */
  66. cfg->freq.armclk = dvs ? hclk : armdiv_clk;
  67. s3c_freq_dbg("%s: armclk %lu, hclk %lu, armdiv %d, hdiv %d, dvs %d\n",
  68. __func__, armclk, hclk, armdiv, hdiv, cfg->divs.dvs);
  69. if (hdiv > 4)
  70. goto invalid;
  71. pdiv = (hclk > cfg->max.pclk) ? 2 : 1;
  72. if ((hclk / pdiv) > cfg->max.pclk)
  73. pdiv++;
  74. cfg->freq.pclk = hclk / pdiv;
  75. s3c_freq_dbg("%s: pdiv %d\n", __func__, pdiv);
  76. if (pdiv > 2)
  77. goto invalid;
  78. pdiv *= hdiv;
  79. /* store the result, and then return */
  80. cfg->divs.h_divisor = hdiv * armdiv;
  81. cfg->divs.p_divisor = pdiv * armdiv;
  82. return 0;
  83. invalid:
  84. return -EINVAL;
  85. }
  86. static void s3c2412_cpufreq_setdivs(struct s3c_cpufreq_config *cfg)
  87. {
  88. unsigned long clkdiv;
  89. unsigned long olddiv;
  90. olddiv = clkdiv = __raw_readl(S3C2410_CLKDIVN);
  91. /* clear off current clock info */
  92. clkdiv &= ~S3C2412_CLKDIVN_ARMDIVN;
  93. clkdiv &= ~S3C2412_CLKDIVN_HDIVN_MASK;
  94. clkdiv &= ~S3C2412_CLKDIVN_PDIVN;
  95. if (cfg->divs.arm_divisor == 2)
  96. clkdiv |= S3C2412_CLKDIVN_ARMDIVN;
  97. clkdiv |= ((cfg->divs.h_divisor / cfg->divs.arm_divisor) - 1);
  98. if (cfg->divs.p_divisor != cfg->divs.h_divisor)
  99. clkdiv |= S3C2412_CLKDIVN_PDIVN;
  100. s3c_freq_dbg("%s: div %08lx => %08lx\n", __func__, olddiv, clkdiv);
  101. __raw_writel(clkdiv, S3C2410_CLKDIVN);
  102. clk_set_parent(armclk, cfg->divs.dvs ? hclk : fclk);
  103. }
  104. static void s3c2412_cpufreq_setrefresh(struct s3c_cpufreq_config *cfg)
  105. {
  106. struct s3c_cpufreq_board *board = cfg->board;
  107. unsigned long refresh;
  108. s3c_freq_dbg("%s: refresh %u ns, hclk %lu\n", __func__,
  109. board->refresh, cfg->freq.hclk);
  110. /* Reduce both the refresh time (in ns) and the frequency (in MHz)
  111. * by 10 each to ensure that we do not overflow 32 bit numbers. This
  112. * should work for HCLK up to 133MHz and refresh period up to 30usec.
  113. */
  114. refresh = (board->refresh / 10);
  115. refresh *= (cfg->freq.hclk / 100);
  116. refresh /= (1 * 1000 * 1000); /* 10^6 */
  117. s3c_freq_dbg("%s: setting refresh 0x%08lx\n", __func__, refresh);
  118. __raw_writel(refresh, S3C2412_REFRESH);
  119. }
  120. /* set the default cpu frequency information, based on an 200MHz part
  121. * as we have no other way of detecting the speed rating in software.
  122. */
  123. static struct s3c_cpufreq_info s3c2412_cpufreq_info = {
  124. .max = {
  125. .fclk = 200000000,
  126. .hclk = 100000000,
  127. .pclk = 50000000,
  128. },
  129. .latency = 5000000, /* 5ms */
  130. .locktime_m = 150,
  131. .locktime_u = 150,
  132. .locktime_bits = 16,
  133. .name = "s3c2412",
  134. .set_refresh = s3c2412_cpufreq_setrefresh,
  135. .set_divs = s3c2412_cpufreq_setdivs,
  136. .calc_divs = s3c2412_cpufreq_calcdivs,
  137. .calc_iotiming = s3c2412_iotiming_calc,
  138. .set_iotiming = s3c2412_iotiming_set,
  139. .get_iotiming = s3c2412_iotiming_get,
  140. .debug_io_show = s3c_cpufreq_debugfs_call(s3c2412_iotiming_debugfs),
  141. };
  142. static int s3c2412_cpufreq_add(struct device *dev,
  143. struct subsys_interface *sif)
  144. {
  145. unsigned long fclk_rate;
  146. hclk = clk_get(NULL, "hclk");
  147. if (IS_ERR(hclk)) {
  148. pr_err("cannot find hclk clock\n");
  149. return -ENOENT;
  150. }
  151. fclk = clk_get(NULL, "fclk");
  152. if (IS_ERR(fclk)) {
  153. pr_err("cannot find fclk clock\n");
  154. goto err_fclk;
  155. }
  156. fclk_rate = clk_get_rate(fclk);
  157. if (fclk_rate > 200000000) {
  158. pr_info("fclk %ld MHz, assuming 266MHz capable part\n",
  159. 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. pr_err("cannot find arm clock\n");
  167. goto err_armclk;
  168. }
  169. xtal = clk_get(NULL, "xtal");
  170. if (IS_ERR(xtal)) {
  171. pr_err("cannot find xtal clock\n");
  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);