cpu-pxa.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * linux/arch/arm/mach-pxa/cpu-pxa.c
  3. *
  4. * Copyright (C) 2002,2003 Intrinsyc Software
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * History:
  21. * 31-Jul-2002 : Initial version [FB]
  22. * 29-Jan-2003 : added PXA255 support [FB]
  23. * 20-Apr-2003 : ported to v2.5 (Dustin McIntire, Sensoria Corp.)
  24. *
  25. * Note:
  26. * This driver may change the memory bus clock rate, but will not do any
  27. * platform specific access timing changes... for example if you have flash
  28. * memory connected to CS0, you will need to register a platform specific
  29. * notifier which will adjust the memory access strobes to maintain a
  30. * minimum strobe width.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/sched.h>
  36. #include <linux/init.h>
  37. #include <linux/cpufreq.h>
  38. #include <asm/hardware.h>
  39. #include <asm/arch/pxa-regs.h>
  40. #include <asm/arch/pxa2xx-regs.h>
  41. #ifdef DEBUG
  42. static unsigned int freq_debug;
  43. module_param(freq_debug, uint, 0);
  44. MODULE_PARM_DESC(freq_debug, "Set the debug messages to on=1/off=0");
  45. #else
  46. #define freq_debug 0
  47. #endif
  48. typedef struct {
  49. unsigned int khz;
  50. unsigned int membus;
  51. unsigned int cccr;
  52. unsigned int div2;
  53. } pxa_freqs_t;
  54. /* Define the refresh period in mSec for the SDRAM and the number of rows */
  55. #define SDRAM_TREF 64 /* standard 64ms SDRAM */
  56. #define SDRAM_ROWS 4096 /* 64MB=8192 32MB=4096 */
  57. #define MDREFR_DRI(x) (((x) * SDRAM_TREF) / (SDRAM_ROWS * 32))
  58. #define CCLKCFG_TURBO 0x1
  59. #define CCLKCFG_FCS 0x2
  60. #define PXA25x_MIN_FREQ 99500
  61. #define PXA25x_MAX_FREQ 398100
  62. #define MDREFR_DB2_MASK (MDREFR_K2DB2 | MDREFR_K1DB2)
  63. #define MDREFR_DRI_MASK 0xFFF
  64. /* Use the run mode frequencies for the CPUFREQ_POLICY_PERFORMANCE policy */
  65. static pxa_freqs_t pxa255_run_freqs[] =
  66. {
  67. /* CPU MEMBUS CCCR DIV2*/
  68. { 99500, 99500, 0x121, 1}, /* run= 99, turbo= 99, PXbus=50, SDRAM=50 */
  69. {132700, 132700, 0x123, 1}, /* run=133, turbo=133, PXbus=66, SDRAM=66 */
  70. {199100, 99500, 0x141, 0}, /* run=199, turbo=199, PXbus=99, SDRAM=99 */
  71. {265400, 132700, 0x143, 1}, /* run=265, turbo=265, PXbus=133, SDRAM=66 */
  72. {331800, 165900, 0x145, 1}, /* run=331, turbo=331, PXbus=166, SDRAM=83 */
  73. {398100, 99500, 0x161, 0}, /* run=398, turbo=398, PXbus=196, SDRAM=99 */
  74. {0,}
  75. };
  76. #define NUM_RUN_FREQS ARRAY_SIZE(pxa255_run_freqs)
  77. static struct cpufreq_frequency_table pxa255_run_freq_table[NUM_RUN_FREQS+1];
  78. /* Use the turbo mode frequencies for the CPUFREQ_POLICY_POWERSAVE policy */
  79. static pxa_freqs_t pxa255_turbo_freqs[] =
  80. {
  81. /* CPU MEMBUS CCCR DIV2*/
  82. { 99500, 99500, 0x121, 1}, /* run=99, turbo= 99, PXbus=50, SDRAM=50 */
  83. {199100, 99500, 0x221, 0}, /* run=99, turbo=199, PXbus=50, SDRAM=99 */
  84. {298500, 99500, 0x321, 0}, /* run=99, turbo=287, PXbus=50, SDRAM=99 */
  85. {298600, 99500, 0x1c1, 0}, /* run=199, turbo=287, PXbus=99, SDRAM=99 */
  86. {398100, 99500, 0x241, 0}, /* run=199, turbo=398, PXbus=99, SDRAM=99 */
  87. {0,}
  88. };
  89. #define NUM_TURBO_FREQS ARRAY_SIZE(pxa255_turbo_freqs)
  90. static struct cpufreq_frequency_table pxa255_turbo_freq_table[NUM_TURBO_FREQS+1];
  91. extern unsigned get_clk_frequency_khz(int info);
  92. /* find a valid frequency point */
  93. static int pxa_verify_policy(struct cpufreq_policy *policy)
  94. {
  95. struct cpufreq_frequency_table *pxa_freqs_table;
  96. int ret;
  97. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
  98. pxa_freqs_table = pxa255_run_freq_table;
  99. } else if (policy->policy == CPUFREQ_POLICY_POWERSAVE) {
  100. pxa_freqs_table = pxa255_turbo_freq_table;
  101. } else {
  102. printk("CPU PXA: Unknown policy found. "
  103. "Using CPUFREQ_POLICY_PERFORMANCE\n");
  104. pxa_freqs_table = pxa255_run_freq_table;
  105. }
  106. ret = cpufreq_frequency_table_verify(policy, pxa_freqs_table);
  107. if (freq_debug)
  108. pr_debug("Verified CPU policy: %dKhz min to %dKhz max\n",
  109. policy->min, policy->max);
  110. return ret;
  111. }
  112. static int pxa_set_target(struct cpufreq_policy *policy,
  113. unsigned int target_freq,
  114. unsigned int relation)
  115. {
  116. struct cpufreq_frequency_table *pxa_freqs_table;
  117. pxa_freqs_t *pxa_freq_settings;
  118. struct cpufreq_freqs freqs;
  119. unsigned int idx;
  120. unsigned long flags;
  121. unsigned int unused, preset_mdrefr, postset_mdrefr;
  122. void *ramstart = phys_to_virt(0xa0000000);
  123. /* Get the current policy */
  124. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
  125. pxa_freq_settings = pxa255_run_freqs;
  126. pxa_freqs_table = pxa255_run_freq_table;
  127. } else if (policy->policy == CPUFREQ_POLICY_POWERSAVE) {
  128. pxa_freq_settings = pxa255_turbo_freqs;
  129. pxa_freqs_table = pxa255_turbo_freq_table;
  130. } else {
  131. printk("CPU PXA: Unknown policy found. "
  132. "Using CPUFREQ_POLICY_PERFORMANCE\n");
  133. pxa_freq_settings = pxa255_run_freqs;
  134. pxa_freqs_table = pxa255_run_freq_table;
  135. }
  136. /* Lookup the next frequency */
  137. if (cpufreq_frequency_table_target(policy, pxa_freqs_table,
  138. target_freq, relation, &idx)) {
  139. return -EINVAL;
  140. }
  141. freqs.old = policy->cur;
  142. freqs.new = pxa_freq_settings[idx].khz;
  143. freqs.cpu = policy->cpu;
  144. if (freq_debug)
  145. pr_debug(KERN_INFO "Changing CPU frequency to %d Mhz, (SDRAM %d Mhz)\n",
  146. freqs.new / 1000, (pxa_freq_settings[idx].div2) ?
  147. (pxa_freq_settings[idx].membus / 2000) :
  148. (pxa_freq_settings[idx].membus / 1000));
  149. /*
  150. * Tell everyone what we're about to do...
  151. * you should add a notify client with any platform specific
  152. * Vcc changing capability
  153. */
  154. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  155. /* Calculate the next MDREFR. If we're slowing down the SDRAM clock
  156. * we need to preset the smaller DRI before the change. If we're speeding
  157. * up we need to set the larger DRI value after the change.
  158. */
  159. preset_mdrefr = postset_mdrefr = MDREFR;
  160. if ((MDREFR & MDREFR_DRI_MASK) > MDREFR_DRI(pxa_freq_settings[idx].membus)) {
  161. preset_mdrefr = (preset_mdrefr & ~MDREFR_DRI_MASK) |
  162. MDREFR_DRI(pxa_freq_settings[idx].membus);
  163. }
  164. postset_mdrefr = (postset_mdrefr & ~MDREFR_DRI_MASK) |
  165. MDREFR_DRI(pxa_freq_settings[idx].membus);
  166. /* If we're dividing the memory clock by two for the SDRAM clock, this
  167. * must be set prior to the change. Clearing the divide must be done
  168. * after the change.
  169. */
  170. if (pxa_freq_settings[idx].div2) {
  171. preset_mdrefr |= MDREFR_DB2_MASK;
  172. postset_mdrefr |= MDREFR_DB2_MASK;
  173. } else {
  174. postset_mdrefr &= ~MDREFR_DB2_MASK;
  175. }
  176. local_irq_save(flags);
  177. /* Set new the CCCR */
  178. CCCR = pxa_freq_settings[idx].cccr;
  179. asm volatile(" \n\
  180. ldr r4, [%1] /* load MDREFR */ \n\
  181. b 2f \n\
  182. .align 5 \n\
  183. 1: \n\
  184. str %4, [%1] /* preset the MDREFR */ \n\
  185. mcr p14, 0, %2, c6, c0, 0 /* set CCLKCFG[FCS] */ \n\
  186. str %5, [%1] /* postset the MDREFR */ \n\
  187. \n\
  188. b 3f \n\
  189. 2: b 1b \n\
  190. 3: nop \n\
  191. "
  192. : "=&r" (unused)
  193. : "r" (&MDREFR), "r" (CCLKCFG_TURBO|CCLKCFG_FCS), "r" (ramstart),
  194. "r" (preset_mdrefr), "r" (postset_mdrefr)
  195. : "r4", "r5");
  196. local_irq_restore(flags);
  197. /*
  198. * Tell everyone what we've just done...
  199. * you should add a notify client with any platform specific
  200. * SDRAM refresh timer adjustments
  201. */
  202. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  203. return 0;
  204. }
  205. static unsigned int pxa_cpufreq_get(unsigned int cpu)
  206. {
  207. return get_clk_frequency_khz(0);
  208. }
  209. static int pxa_cpufreq_init(struct cpufreq_policy *policy)
  210. {
  211. int i;
  212. /* set default policy and cpuinfo */
  213. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  214. policy->policy = CPUFREQ_POLICY_PERFORMANCE;
  215. policy->cpuinfo.max_freq = PXA25x_MAX_FREQ;
  216. policy->cpuinfo.min_freq = PXA25x_MIN_FREQ;
  217. policy->cpuinfo.transition_latency = 1000; /* FIXME: 1 ms, assumed */
  218. policy->cur = get_clk_frequency_khz(0); /* current freq */
  219. policy->min = policy->max = policy->cur;
  220. /* Generate the run cpufreq_frequency_table struct */
  221. for (i = 0; i < NUM_RUN_FREQS; i++) {
  222. pxa255_run_freq_table[i].frequency = pxa255_run_freqs[i].khz;
  223. pxa255_run_freq_table[i].index = i;
  224. }
  225. pxa255_run_freq_table[i].frequency = CPUFREQ_TABLE_END;
  226. /* Generate the turbo cpufreq_frequency_table struct */
  227. for (i = 0; i < NUM_TURBO_FREQS; i++) {
  228. pxa255_turbo_freq_table[i].frequency = pxa255_turbo_freqs[i].khz;
  229. pxa255_turbo_freq_table[i].index = i;
  230. }
  231. pxa255_turbo_freq_table[i].frequency = CPUFREQ_TABLE_END;
  232. printk(KERN_INFO "PXA CPU frequency change support initialized\n");
  233. return 0;
  234. }
  235. static struct cpufreq_driver pxa_cpufreq_driver = {
  236. .verify = pxa_verify_policy,
  237. .target = pxa_set_target,
  238. .init = pxa_cpufreq_init,
  239. .get = pxa_cpufreq_get,
  240. .name = "PXA25x",
  241. };
  242. static int __init pxa_cpu_init(void)
  243. {
  244. int ret = -ENODEV;
  245. if (cpu_is_pxa25x())
  246. ret = cpufreq_register_driver(&pxa_cpufreq_driver);
  247. return ret;
  248. }
  249. static void __exit pxa_cpu_exit(void)
  250. {
  251. if (cpu_is_pxa25x())
  252. cpufreq_unregister_driver(&pxa_cpufreq_driver);
  253. }
  254. MODULE_AUTHOR ("Intrinsyc Software Inc.");
  255. MODULE_DESCRIPTION ("CPU frequency changing driver for the PXA architecture");
  256. MODULE_LICENSE("GPL");
  257. module_init(pxa_cpu_init);
  258. module_exit(pxa_cpu_exit);