s3c2440-cpufreq.c 7.0 KB

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