pxa2xx-cpufreq.c 8.7 KB

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