cpufreq-s3c2440.c 7.1 KB

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