pxa2xx-cpufreq.c 9.4 KB

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