s3c2440-cpufreq.c 7.2 KB

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