s3c2440-cpufreq.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2006-2009 Simtec Electronics
  3. * http://armlinux.simtec.co.uk/
  4. * Ben Dooks <ben@simtec.co.uk>
  5. * Vincent Sanders <vince@simtec.co.uk>
  6. *
  7. * S3C2440/S3C2442 CPU Frequency scaling
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/device.h>
  20. #include <linux/delay.h>
  21. #include <linux/clk.h>
  22. #include <linux/err.h>
  23. #include <linux/io.h>
  24. #include <asm/mach/arch.h>
  25. #include <asm/mach/map.h>
  26. #include <mach/regs-clock.h>
  27. #include <plat/cpu.h>
  28. #include <plat/cpu-freq-core.h>
  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 inline int within_khz(unsigned long a, unsigned long b)
  35. {
  36. long diff = a - b;
  37. return (diff >= -1000 && diff <= 1000);
  38. }
  39. /**
  40. * s3c2440_cpufreq_calcdivs - calculate divider settings
  41. * @cfg: The cpu frequency settings.
  42. *
  43. * Calcualte the divider values for the given frequency settings
  44. * specified in @cfg. The values are stored in @cfg for later use
  45. * by the relevant set routine if the request settings can be reached.
  46. */
  47. static int s3c2440_cpufreq_calcdivs(struct s3c_cpufreq_config *cfg)
  48. {
  49. unsigned int hdiv, pdiv;
  50. unsigned long hclk, fclk, armclk;
  51. unsigned long hclk_max;
  52. fclk = cfg->freq.fclk;
  53. armclk = cfg->freq.armclk;
  54. hclk_max = cfg->max.hclk;
  55. s3c_freq_dbg("%s: fclk is %lu, armclk %lu, max hclk %lu\n",
  56. __func__, fclk, armclk, hclk_max);
  57. if (armclk > fclk) {
  58. pr_warn("%s: armclk > fclk\n", __func__);
  59. armclk = fclk;
  60. }
  61. /* if we are in DVS, we need HCLK to be <= ARMCLK */
  62. if (armclk < fclk && armclk < hclk_max)
  63. hclk_max = armclk;
  64. for (hdiv = 1; hdiv < 9; hdiv++) {
  65. if (hdiv == 5 || hdiv == 7)
  66. hdiv++;
  67. hclk = (fclk / hdiv);
  68. if (hclk <= hclk_max || within_khz(hclk, hclk_max))
  69. break;
  70. }
  71. s3c_freq_dbg("%s: hclk %lu, div %d\n", __func__, hclk, hdiv);
  72. if (hdiv > 8)
  73. goto invalid;
  74. pdiv = (hclk > cfg->max.pclk) ? 2 : 1;
  75. if ((hclk / pdiv) > cfg->max.pclk)
  76. pdiv++;
  77. s3c_freq_dbg("%s: pdiv %d\n", __func__, pdiv);
  78. if (pdiv > 2)
  79. goto invalid;
  80. pdiv *= hdiv;
  81. /* calculate a valid armclk */
  82. if (armclk < hclk)
  83. armclk = hclk;
  84. /* if we're running armclk lower than fclk, this really means
  85. * that the system should go into dvs mode, which means that
  86. * armclk is connected to hclk. */
  87. if (armclk < fclk) {
  88. cfg->divs.dvs = 1;
  89. armclk = hclk;
  90. } else
  91. cfg->divs.dvs = 0;
  92. cfg->freq.armclk = armclk;
  93. /* store the result, and then return */
  94. cfg->divs.h_divisor = hdiv;
  95. cfg->divs.p_divisor = pdiv;
  96. return 0;
  97. invalid:
  98. return -EINVAL;
  99. }
  100. #define CAMDIVN_HCLK_HALF (S3C2440_CAMDIVN_HCLK3_HALF | \
  101. S3C2440_CAMDIVN_HCLK4_HALF)
  102. /**
  103. * s3c2440_cpufreq_setdivs - set the cpu frequency divider settings
  104. * @cfg: The cpu frequency settings.
  105. *
  106. * Set the divisors from the settings in @cfg, which where generated
  107. * during the calculation phase by s3c2440_cpufreq_calcdivs().
  108. */
  109. static void s3c2440_cpufreq_setdivs(struct s3c_cpufreq_config *cfg)
  110. {
  111. unsigned long clkdiv, camdiv;
  112. s3c_freq_dbg("%s: divsiors: h=%d, p=%d\n", __func__,
  113. cfg->divs.h_divisor, cfg->divs.p_divisor);
  114. clkdiv = __raw_readl(S3C2410_CLKDIVN);
  115. camdiv = __raw_readl(S3C2440_CAMDIVN);
  116. clkdiv &= ~(S3C2440_CLKDIVN_HDIVN_MASK | S3C2440_CLKDIVN_PDIVN);
  117. camdiv &= ~CAMDIVN_HCLK_HALF;
  118. switch (cfg->divs.h_divisor) {
  119. case 1:
  120. clkdiv |= S3C2440_CLKDIVN_HDIVN_1;
  121. break;
  122. case 2:
  123. clkdiv |= S3C2440_CLKDIVN_HDIVN_2;
  124. break;
  125. case 6:
  126. camdiv |= S3C2440_CAMDIVN_HCLK3_HALF;
  127. case 3:
  128. clkdiv |= S3C2440_CLKDIVN_HDIVN_3_6;
  129. break;
  130. case 8:
  131. camdiv |= S3C2440_CAMDIVN_HCLK4_HALF;
  132. case 4:
  133. clkdiv |= S3C2440_CLKDIVN_HDIVN_4_8;
  134. break;
  135. default:
  136. BUG(); /* we don't expect to get here. */
  137. }
  138. if (cfg->divs.p_divisor != cfg->divs.h_divisor)
  139. clkdiv |= S3C2440_CLKDIVN_PDIVN;
  140. /* todo - set pclk. */
  141. /* Write the divisors first with hclk intentionally halved so that
  142. * when we write clkdiv we will under-frequency instead of over. We
  143. * then make a short delay and remove the hclk halving if necessary.
  144. */
  145. __raw_writel(camdiv | CAMDIVN_HCLK_HALF, S3C2440_CAMDIVN);
  146. __raw_writel(clkdiv, S3C2410_CLKDIVN);
  147. ndelay(20);
  148. __raw_writel(camdiv, S3C2440_CAMDIVN);
  149. clk_set_parent(armclk, cfg->divs.dvs ? hclk : fclk);
  150. }
  151. static int run_freq_for(unsigned long max_hclk, unsigned long fclk,
  152. int *divs,
  153. struct cpufreq_frequency_table *table,
  154. size_t table_size)
  155. {
  156. unsigned long freq;
  157. int index = 0;
  158. int div;
  159. for (div = *divs; div > 0; div = *divs++) {
  160. freq = fclk / div;
  161. if (freq > max_hclk && div != 1)
  162. continue;
  163. freq /= 1000; /* table is in kHz */
  164. index = s3c_cpufreq_addfreq(table, index, table_size, freq);
  165. if (index < 0)
  166. break;
  167. }
  168. return index;
  169. }
  170. static int hclk_divs[] = { 1, 2, 3, 4, 6, 8, -1 };
  171. static int s3c2440_cpufreq_calctable(struct s3c_cpufreq_config *cfg,
  172. struct cpufreq_frequency_table *table,
  173. size_t table_size)
  174. {
  175. int ret;
  176. WARN_ON(cfg->info == NULL);
  177. WARN_ON(cfg->board == NULL);
  178. ret = run_freq_for(cfg->info->max.hclk,
  179. cfg->info->max.fclk,
  180. hclk_divs,
  181. table, table_size);
  182. s3c_freq_dbg("%s: returning %d\n", __func__, ret);
  183. return ret;
  184. }
  185. static struct s3c_cpufreq_info s3c2440_cpufreq_info = {
  186. .max = {
  187. .fclk = 400000000,
  188. .hclk = 133333333,
  189. .pclk = 66666666,
  190. },
  191. .locktime_m = 300,
  192. .locktime_u = 300,
  193. .locktime_bits = 16,
  194. .name = "s3c244x",
  195. .calc_iotiming = s3c2410_iotiming_calc,
  196. .set_iotiming = s3c2410_iotiming_set,
  197. .get_iotiming = s3c2410_iotiming_get,
  198. .set_fvco = s3c2410_set_fvco,
  199. .set_refresh = s3c2410_cpufreq_setrefresh,
  200. .set_divs = s3c2440_cpufreq_setdivs,
  201. .calc_divs = s3c2440_cpufreq_calcdivs,
  202. .calc_freqtable = s3c2440_cpufreq_calctable,
  203. .debug_io_show = s3c_cpufreq_debugfs_call(s3c2410_iotiming_debugfs),
  204. };
  205. static int s3c2440_cpufreq_add(struct device *dev,
  206. struct subsys_interface *sif)
  207. {
  208. xtal = s3c_cpufreq_clk_get(NULL, "xtal");
  209. hclk = s3c_cpufreq_clk_get(NULL, "hclk");
  210. fclk = s3c_cpufreq_clk_get(NULL, "fclk");
  211. armclk = s3c_cpufreq_clk_get(NULL, "armclk");
  212. if (IS_ERR(xtal) || IS_ERR(hclk) || IS_ERR(fclk) || IS_ERR(armclk)) {
  213. pr_err("%s: failed to get clocks\n", __func__);
  214. return -ENOENT;
  215. }
  216. return s3c_cpufreq_register(&s3c2440_cpufreq_info);
  217. }
  218. static struct subsys_interface s3c2440_cpufreq_interface = {
  219. .name = "s3c2440_cpufreq",
  220. .subsys = &s3c2440_subsys,
  221. .add_dev = s3c2440_cpufreq_add,
  222. };
  223. static int s3c2440_cpufreq_init(void)
  224. {
  225. return subsys_interface_register(&s3c2440_cpufreq_interface);
  226. }
  227. /* arch_initcall adds the clocks we need, so use subsys_initcall. */
  228. subsys_initcall(s3c2440_cpufreq_init);
  229. static struct subsys_interface s3c2442_cpufreq_interface = {
  230. .name = "s3c2442_cpufreq",
  231. .subsys = &s3c2442_subsys,
  232. .add_dev = s3c2440_cpufreq_add,
  233. };
  234. static int s3c2442_cpufreq_init(void)
  235. {
  236. return subsys_interface_register(&s3c2442_cpufreq_interface);
  237. }
  238. subsys_initcall(s3c2442_cpufreq_init);