s3c2440-cpufreq.c 7.5 KB

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