pxa2xx-cpufreq.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2002,2003 Intrinsyc Software
  4. *
  5. * History:
  6. * 31-Jul-2002 : Initial version [FB]
  7. * 29-Jan-2003 : added PXA255 support [FB]
  8. * 20-Apr-2003 : ported to v2.5 (Dustin McIntire, Sensoria Corp.)
  9. *
  10. * Note:
  11. * This driver may change the memory bus clock rate, but will not do any
  12. * platform specific access timing changes... for example if you have flash
  13. * memory connected to CS0, you will need to register a platform specific
  14. * notifier which will adjust the memory access strobes to maintain a
  15. * minimum strobe width.
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/sched.h>
  21. #include <linux/init.h>
  22. #include <linux/cpufreq.h>
  23. #include <linux/err.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/soc/pxa/cpu.h>
  26. #include <linux/io.h>
  27. #ifdef DEBUG
  28. static unsigned int freq_debug;
  29. module_param(freq_debug, uint, 0);
  30. MODULE_PARM_DESC(freq_debug, "Set the debug messages to on=1/off=0");
  31. #else
  32. #define freq_debug 0
  33. #endif
  34. static struct regulator *vcc_core;
  35. static unsigned int pxa27x_maxfreq;
  36. module_param(pxa27x_maxfreq, uint, 0);
  37. MODULE_PARM_DESC(pxa27x_maxfreq, "Set the pxa27x maxfreq in MHz"
  38. "(typically 624=>pxa270, 416=>pxa271, 520=>pxa272)");
  39. struct pxa_cpufreq_data {
  40. struct clk *clk_core;
  41. };
  42. static struct pxa_cpufreq_data pxa_cpufreq_data;
  43. struct pxa_freqs {
  44. unsigned int khz;
  45. int vmin;
  46. int vmax;
  47. };
  48. /*
  49. * PXA255 definitions
  50. */
  51. static const struct pxa_freqs pxa255_run_freqs[] =
  52. {
  53. /* CPU MEMBUS run turbo PXbus SDRAM */
  54. { 99500, -1, -1}, /* 99, 99, 50, 50 */
  55. {132700, -1, -1}, /* 133, 133, 66, 66 */
  56. {199100, -1, -1}, /* 199, 199, 99, 99 */
  57. {265400, -1, -1}, /* 265, 265, 133, 66 */
  58. {331800, -1, -1}, /* 331, 331, 166, 83 */
  59. {398100, -1, -1}, /* 398, 398, 196, 99 */
  60. };
  61. /* Use the turbo mode frequencies for the CPUFREQ_POLICY_POWERSAVE policy */
  62. static const struct pxa_freqs pxa255_turbo_freqs[] =
  63. {
  64. /* CPU run turbo PXbus SDRAM */
  65. { 99500, -1, -1}, /* 99, 99, 50, 50 */
  66. {199100, -1, -1}, /* 99, 199, 50, 99 */
  67. {298500, -1, -1}, /* 99, 287, 50, 99 */
  68. {298600, -1, -1}, /* 199, 287, 99, 99 */
  69. {398100, -1, -1}, /* 199, 398, 99, 99 */
  70. };
  71. #define NUM_PXA25x_RUN_FREQS ARRAY_SIZE(pxa255_run_freqs)
  72. #define NUM_PXA25x_TURBO_FREQS ARRAY_SIZE(pxa255_turbo_freqs)
  73. static struct cpufreq_frequency_table
  74. pxa255_run_freq_table[NUM_PXA25x_RUN_FREQS+1];
  75. static struct cpufreq_frequency_table
  76. pxa255_turbo_freq_table[NUM_PXA25x_TURBO_FREQS+1];
  77. static unsigned int pxa255_turbo_table;
  78. module_param(pxa255_turbo_table, uint, 0);
  79. MODULE_PARM_DESC(pxa255_turbo_table, "Selects the frequency table (0 = run table, !0 = turbo table)");
  80. static struct pxa_freqs pxa27x_freqs[] = {
  81. {104000, 900000, 1705000 },
  82. {156000, 1000000, 1705000 },
  83. {208000, 1180000, 1705000 },
  84. {312000, 1250000, 1705000 },
  85. {416000, 1350000, 1705000 },
  86. {520000, 1450000, 1705000 },
  87. {624000, 1550000, 1705000 }
  88. };
  89. #define NUM_PXA27x_FREQS ARRAY_SIZE(pxa27x_freqs)
  90. static struct cpufreq_frequency_table
  91. pxa27x_freq_table[NUM_PXA27x_FREQS+1];
  92. #ifdef CONFIG_REGULATOR
  93. static int pxa_cpufreq_change_voltage(const struct pxa_freqs *pxa_freq)
  94. {
  95. int ret = 0;
  96. int vmin, vmax;
  97. if (!cpu_is_pxa27x())
  98. return 0;
  99. vmin = pxa_freq->vmin;
  100. vmax = pxa_freq->vmax;
  101. if ((vmin == -1) || (vmax == -1))
  102. return 0;
  103. ret = regulator_set_voltage(vcc_core, vmin, vmax);
  104. if (ret)
  105. pr_err("Failed to set vcc_core in [%dmV..%dmV]\n", vmin, vmax);
  106. return ret;
  107. }
  108. static void pxa_cpufreq_init_voltages(void)
  109. {
  110. vcc_core = regulator_get(NULL, "vcc_core");
  111. if (IS_ERR(vcc_core)) {
  112. pr_info("Didn't find vcc_core regulator\n");
  113. vcc_core = NULL;
  114. } else {
  115. pr_info("Found vcc_core regulator\n");
  116. }
  117. }
  118. #else
  119. static int pxa_cpufreq_change_voltage(const struct pxa_freqs *pxa_freq)
  120. {
  121. return 0;
  122. }
  123. static void pxa_cpufreq_init_voltages(void) { }
  124. #endif
  125. static void find_freq_tables(struct cpufreq_frequency_table **freq_table,
  126. const struct pxa_freqs **pxa_freqs)
  127. {
  128. if (cpu_is_pxa25x()) {
  129. if (!pxa255_turbo_table) {
  130. *pxa_freqs = pxa255_run_freqs;
  131. *freq_table = pxa255_run_freq_table;
  132. } else {
  133. *pxa_freqs = pxa255_turbo_freqs;
  134. *freq_table = pxa255_turbo_freq_table;
  135. }
  136. } else if (cpu_is_pxa27x()) {
  137. *pxa_freqs = pxa27x_freqs;
  138. *freq_table = pxa27x_freq_table;
  139. } else {
  140. BUG();
  141. }
  142. }
  143. static void pxa27x_guess_max_freq(void)
  144. {
  145. if (!pxa27x_maxfreq) {
  146. pxa27x_maxfreq = 416000;
  147. pr_info("PXA CPU 27x max frequency not defined (pxa27x_maxfreq), assuming pxa271 with %dkHz maxfreq\n",
  148. pxa27x_maxfreq);
  149. } else {
  150. pxa27x_maxfreq *= 1000;
  151. }
  152. }
  153. static unsigned int pxa_cpufreq_get(unsigned int cpu)
  154. {
  155. struct pxa_cpufreq_data *data = cpufreq_get_driver_data();
  156. return (unsigned int) clk_get_rate(data->clk_core) / 1000;
  157. }
  158. static int pxa_set_target(struct cpufreq_policy *policy, unsigned int idx)
  159. {
  160. struct cpufreq_frequency_table *pxa_freqs_table;
  161. const struct pxa_freqs *pxa_freq_settings;
  162. struct pxa_cpufreq_data *data = cpufreq_get_driver_data();
  163. unsigned int new_freq_cpu;
  164. int ret = 0;
  165. /* Get the current policy */
  166. find_freq_tables(&pxa_freqs_table, &pxa_freq_settings);
  167. new_freq_cpu = pxa_freq_settings[idx].khz;
  168. if (freq_debug)
  169. pr_debug("Changing CPU frequency from %d Mhz to %d Mhz\n",
  170. policy->cur / 1000, new_freq_cpu / 1000);
  171. if (vcc_core && new_freq_cpu > policy->cur) {
  172. ret = pxa_cpufreq_change_voltage(&pxa_freq_settings[idx]);
  173. if (ret)
  174. return ret;
  175. }
  176. clk_set_rate(data->clk_core, new_freq_cpu * 1000);
  177. /*
  178. * Even if voltage setting fails, we don't report it, as the frequency
  179. * change succeeded. The voltage reduction is not a critical failure,
  180. * only power savings will suffer from this.
  181. *
  182. * Note: if the voltage change fails, and a return value is returned, a
  183. * bug is triggered (seems a deadlock). Should anybody find out where,
  184. * the "return 0" should become a "return ret".
  185. */
  186. if (vcc_core && new_freq_cpu < policy->cur)
  187. ret = pxa_cpufreq_change_voltage(&pxa_freq_settings[idx]);
  188. return 0;
  189. }
  190. static int pxa_cpufreq_init(struct cpufreq_policy *policy)
  191. {
  192. int i;
  193. unsigned int freq;
  194. struct cpufreq_frequency_table *pxa255_freq_table;
  195. const struct pxa_freqs *pxa255_freqs;
  196. /* try to guess pxa27x cpu */
  197. if (cpu_is_pxa27x())
  198. pxa27x_guess_max_freq();
  199. pxa_cpufreq_init_voltages();
  200. /* set default policy and cpuinfo */
  201. policy->cpuinfo.transition_latency = 1000; /* FIXME: 1 ms, assumed */
  202. /* Generate pxa25x the run cpufreq_frequency_table struct */
  203. for (i = 0; i < NUM_PXA25x_RUN_FREQS; i++) {
  204. pxa255_run_freq_table[i].frequency = pxa255_run_freqs[i].khz;
  205. pxa255_run_freq_table[i].driver_data = i;
  206. }
  207. pxa255_run_freq_table[i].frequency = CPUFREQ_TABLE_END;
  208. /* Generate pxa25x the turbo cpufreq_frequency_table struct */
  209. for (i = 0; i < NUM_PXA25x_TURBO_FREQS; i++) {
  210. pxa255_turbo_freq_table[i].frequency =
  211. pxa255_turbo_freqs[i].khz;
  212. pxa255_turbo_freq_table[i].driver_data = i;
  213. }
  214. pxa255_turbo_freq_table[i].frequency = CPUFREQ_TABLE_END;
  215. pxa255_turbo_table = !!pxa255_turbo_table;
  216. /* Generate the pxa27x cpufreq_frequency_table struct */
  217. for (i = 0; i < NUM_PXA27x_FREQS; i++) {
  218. freq = pxa27x_freqs[i].khz;
  219. if (freq > pxa27x_maxfreq)
  220. break;
  221. pxa27x_freq_table[i].frequency = freq;
  222. pxa27x_freq_table[i].driver_data = i;
  223. }
  224. pxa27x_freq_table[i].driver_data = i;
  225. pxa27x_freq_table[i].frequency = CPUFREQ_TABLE_END;
  226. /*
  227. * Set the policy's minimum and maximum frequencies from the tables
  228. * just constructed. This sets cpuinfo.mxx_freq, min and max.
  229. */
  230. if (cpu_is_pxa25x()) {
  231. find_freq_tables(&pxa255_freq_table, &pxa255_freqs);
  232. pr_info("using %s frequency table\n",
  233. pxa255_turbo_table ? "turbo" : "run");
  234. policy->freq_table = pxa255_freq_table;
  235. }
  236. else if (cpu_is_pxa27x()) {
  237. policy->freq_table = pxa27x_freq_table;
  238. }
  239. pr_info("frequency change support initialized\n");
  240. return 0;
  241. }
  242. static struct cpufreq_driver pxa_cpufreq_driver = {
  243. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  244. .verify = cpufreq_generic_frequency_table_verify,
  245. .target_index = pxa_set_target,
  246. .init = pxa_cpufreq_init,
  247. .get = pxa_cpufreq_get,
  248. .name = "PXA2xx",
  249. .driver_data = &pxa_cpufreq_data,
  250. };
  251. static int __init pxa_cpu_init(void)
  252. {
  253. int ret = -ENODEV;
  254. pxa_cpufreq_data.clk_core = clk_get_sys(NULL, "core");
  255. if (IS_ERR(pxa_cpufreq_data.clk_core))
  256. return PTR_ERR(pxa_cpufreq_data.clk_core);
  257. if (cpu_is_pxa25x() || cpu_is_pxa27x())
  258. ret = cpufreq_register_driver(&pxa_cpufreq_driver);
  259. return ret;
  260. }
  261. static void __exit pxa_cpu_exit(void)
  262. {
  263. cpufreq_unregister_driver(&pxa_cpufreq_driver);
  264. }
  265. MODULE_AUTHOR("Intrinsyc Software Inc.");
  266. MODULE_DESCRIPTION("CPU frequency changing driver for the PXA architecture");
  267. MODULE_LICENSE("GPL");
  268. module_init(pxa_cpu_init);
  269. module_exit(pxa_cpu_exit);