cpufreq-pxa2xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * linux/arch/arm/mach-pxa/cpufreq-pxa2xx.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 <mach/hardware.h>
  39. #include <mach/pxa-regs.h>
  40. #include <mach/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. static unsigned int pxa27x_maxfreq;
  49. module_param(pxa27x_maxfreq, uint, 0);
  50. MODULE_PARM_DESC(pxa27x_maxfreq, "Set the pxa27x maxfreq in MHz"
  51. "(typically 624=>pxa270, 416=>pxa271, 520=>pxa272)");
  52. typedef struct {
  53. unsigned int khz;
  54. unsigned int membus;
  55. unsigned int cccr;
  56. unsigned int div2;
  57. unsigned int cclkcfg;
  58. } pxa_freqs_t;
  59. /* Define the refresh period in mSec for the SDRAM and the number of rows */
  60. #define SDRAM_TREF 64 /* standard 64ms SDRAM */
  61. #define SDRAM_ROWS 4096 /* 64MB=8192 32MB=4096 */
  62. #define CCLKCFG_TURBO 0x1
  63. #define CCLKCFG_FCS 0x2
  64. #define CCLKCFG_HALFTURBO 0x4
  65. #define CCLKCFG_FASTBUS 0x8
  66. #define MDREFR_DB2_MASK (MDREFR_K2DB2 | MDREFR_K1DB2)
  67. #define MDREFR_DRI_MASK 0xFFF
  68. /*
  69. * PXA255 definitions
  70. */
  71. /* Use the run mode frequencies for the CPUFREQ_POLICY_PERFORMANCE policy */
  72. #define CCLKCFG CCLKCFG_TURBO | CCLKCFG_FCS
  73. static pxa_freqs_t pxa255_run_freqs[] =
  74. {
  75. /* CPU MEMBUS CCCR DIV2 CCLKCFG run turbo PXbus SDRAM */
  76. { 99500, 99500, 0x121, 1, CCLKCFG}, /* 99, 99, 50, 50 */
  77. {132700, 132700, 0x123, 1, CCLKCFG}, /* 133, 133, 66, 66 */
  78. {199100, 99500, 0x141, 0, CCLKCFG}, /* 199, 199, 99, 99 */
  79. {265400, 132700, 0x143, 1, CCLKCFG}, /* 265, 265, 133, 66 */
  80. {331800, 165900, 0x145, 1, CCLKCFG}, /* 331, 331, 166, 83 */
  81. {398100, 99500, 0x161, 0, CCLKCFG}, /* 398, 398, 196, 99 */
  82. };
  83. /* Use the turbo mode frequencies for the CPUFREQ_POLICY_POWERSAVE policy */
  84. static pxa_freqs_t pxa255_turbo_freqs[] =
  85. {
  86. /* CPU MEMBUS CCCR DIV2 CCLKCFG run turbo PXbus SDRAM */
  87. { 99500, 99500, 0x121, 1, CCLKCFG}, /* 99, 99, 50, 50 */
  88. {199100, 99500, 0x221, 0, CCLKCFG}, /* 99, 199, 50, 99 */
  89. {298500, 99500, 0x321, 0, CCLKCFG}, /* 99, 287, 50, 99 */
  90. {298600, 99500, 0x1c1, 0, CCLKCFG}, /* 199, 287, 99, 99 */
  91. {398100, 99500, 0x241, 0, CCLKCFG}, /* 199, 398, 99, 99 */
  92. };
  93. #define NUM_PXA25x_RUN_FREQS ARRAY_SIZE(pxa255_run_freqs)
  94. #define NUM_PXA25x_TURBO_FREQS ARRAY_SIZE(pxa255_turbo_freqs)
  95. static struct cpufreq_frequency_table
  96. pxa255_run_freq_table[NUM_PXA25x_RUN_FREQS+1];
  97. static struct cpufreq_frequency_table
  98. pxa255_turbo_freq_table[NUM_PXA25x_TURBO_FREQS+1];
  99. /*
  100. * PXA270 definitions
  101. *
  102. * For the PXA27x:
  103. * Control variables are A, L, 2N for CCCR; B, HT, T for CLKCFG.
  104. *
  105. * A = 0 => memory controller clock from table 3-7,
  106. * A = 1 => memory controller clock = system bus clock
  107. * Run mode frequency = 13 MHz * L
  108. * Turbo mode frequency = 13 MHz * L * N
  109. * System bus frequency = 13 MHz * L / (B + 1)
  110. *
  111. * In CCCR:
  112. * A = 1
  113. * L = 16 oscillator to run mode ratio
  114. * 2N = 6 2 * (turbo mode to run mode ratio)
  115. *
  116. * In CCLKCFG:
  117. * B = 1 Fast bus mode
  118. * HT = 0 Half-Turbo mode
  119. * T = 1 Turbo mode
  120. *
  121. * For now, just support some of the combinations in table 3-7 of
  122. * PXA27x Processor Family Developer's Manual to simplify frequency
  123. * change sequences.
  124. */
  125. #define PXA27x_CCCR(A, L, N2) (A << 25 | N2 << 7 | L)
  126. #define CCLKCFG2(B, HT, T) \
  127. (CCLKCFG_FCS | \
  128. ((B) ? CCLKCFG_FASTBUS : 0) | \
  129. ((HT) ? CCLKCFG_HALFTURBO : 0) | \
  130. ((T) ? CCLKCFG_TURBO : 0))
  131. static pxa_freqs_t pxa27x_freqs[] = {
  132. {104000, 104000, PXA27x_CCCR(1, 8, 2), 0, CCLKCFG2(1, 0, 1)},
  133. {156000, 104000, PXA27x_CCCR(1, 8, 6), 0, CCLKCFG2(1, 1, 1)},
  134. {208000, 208000, PXA27x_CCCR(0, 16, 2), 1, CCLKCFG2(0, 0, 1)},
  135. {312000, 208000, PXA27x_CCCR(1, 16, 3), 1, CCLKCFG2(1, 0, 1)},
  136. {416000, 208000, PXA27x_CCCR(1, 16, 4), 1, CCLKCFG2(1, 0, 1)},
  137. {520000, 208000, PXA27x_CCCR(1, 16, 5), 1, CCLKCFG2(1, 0, 1)},
  138. {624000, 208000, PXA27x_CCCR(1, 16, 6), 1, CCLKCFG2(1, 0, 1)}
  139. };
  140. #define NUM_PXA27x_FREQS ARRAY_SIZE(pxa27x_freqs)
  141. static struct cpufreq_frequency_table
  142. pxa27x_freq_table[NUM_PXA27x_FREQS+1];
  143. extern unsigned get_clk_frequency_khz(int info);
  144. static void find_freq_tables(struct cpufreq_policy *policy,
  145. struct cpufreq_frequency_table **freq_table,
  146. pxa_freqs_t **pxa_freqs)
  147. {
  148. if (cpu_is_pxa25x()) {
  149. if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) {
  150. *pxa_freqs = pxa255_run_freqs;
  151. *freq_table = pxa255_run_freq_table;
  152. } else if (policy->policy == CPUFREQ_POLICY_POWERSAVE) {
  153. *pxa_freqs = pxa255_turbo_freqs;
  154. *freq_table = pxa255_turbo_freq_table;
  155. } else {
  156. printk("CPU PXA: Unknown policy found. "
  157. "Using CPUFREQ_POLICY_PERFORMANCE\n");
  158. *pxa_freqs = pxa255_run_freqs;
  159. *freq_table = pxa255_run_freq_table;
  160. }
  161. }
  162. if (cpu_is_pxa27x()) {
  163. *pxa_freqs = pxa27x_freqs;
  164. *freq_table = pxa27x_freq_table;
  165. }
  166. }
  167. static void pxa27x_guess_max_freq(void)
  168. {
  169. if (!pxa27x_maxfreq) {
  170. pxa27x_maxfreq = 416000;
  171. printk(KERN_INFO "PXA CPU 27x max frequency not defined "
  172. "(pxa27x_maxfreq), assuming pxa271 with %dkHz maxfreq\n",
  173. pxa27x_maxfreq);
  174. } else {
  175. pxa27x_maxfreq *= 1000;
  176. }
  177. }
  178. static u32 mdrefr_dri(unsigned int freq)
  179. {
  180. u32 dri = 0;
  181. if (cpu_is_pxa25x())
  182. dri = ((freq * SDRAM_TREF) / (SDRAM_ROWS * 32));
  183. if (cpu_is_pxa27x())
  184. dri = ((freq * SDRAM_TREF) / (SDRAM_ROWS - 31)) / 32;
  185. return dri;
  186. }
  187. /* find a valid frequency point */
  188. static int pxa_verify_policy(struct cpufreq_policy *policy)
  189. {
  190. struct cpufreq_frequency_table *pxa_freqs_table;
  191. pxa_freqs_t *pxa_freqs;
  192. int ret;
  193. find_freq_tables(policy, &pxa_freqs_table, &pxa_freqs);
  194. ret = cpufreq_frequency_table_verify(policy, pxa_freqs_table);
  195. if (freq_debug)
  196. pr_debug("Verified CPU policy: %dKhz min to %dKhz max\n",
  197. policy->min, policy->max);
  198. return ret;
  199. }
  200. static unsigned int pxa_cpufreq_get(unsigned int cpu)
  201. {
  202. return get_clk_frequency_khz(0);
  203. }
  204. static int pxa_set_target(struct cpufreq_policy *policy,
  205. unsigned int target_freq,
  206. unsigned int relation)
  207. {
  208. struct cpufreq_frequency_table *pxa_freqs_table;
  209. pxa_freqs_t *pxa_freq_settings;
  210. struct cpufreq_freqs freqs;
  211. unsigned int idx;
  212. unsigned long flags;
  213. unsigned int new_freq_cpu, new_freq_mem;
  214. unsigned int unused, preset_mdrefr, postset_mdrefr, cclkcfg;
  215. /* Get the current policy */
  216. find_freq_tables(policy, &pxa_freqs_table, &pxa_freq_settings);
  217. /* Lookup the next frequency */
  218. if (cpufreq_frequency_table_target(policy, pxa_freqs_table,
  219. target_freq, relation, &idx)) {
  220. return -EINVAL;
  221. }
  222. new_freq_cpu = pxa_freq_settings[idx].khz;
  223. new_freq_mem = pxa_freq_settings[idx].membus;
  224. freqs.old = policy->cur;
  225. freqs.new = new_freq_cpu;
  226. freqs.cpu = policy->cpu;
  227. if (freq_debug)
  228. pr_debug(KERN_INFO "Changing CPU frequency to %d Mhz, "
  229. "(SDRAM %d Mhz)\n",
  230. freqs.new / 1000, (pxa_freq_settings[idx].div2) ?
  231. (new_freq_mem / 2000) : (new_freq_mem / 1000));
  232. /*
  233. * Tell everyone what we're about to do...
  234. * you should add a notify client with any platform specific
  235. * Vcc changing capability
  236. */
  237. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  238. /* Calculate the next MDREFR. If we're slowing down the SDRAM clock
  239. * we need to preset the smaller DRI before the change. If we're
  240. * speeding up we need to set the larger DRI value after the change.
  241. */
  242. preset_mdrefr = postset_mdrefr = MDREFR;
  243. if ((MDREFR & MDREFR_DRI_MASK) > mdrefr_dri(new_freq_mem)) {
  244. preset_mdrefr = (preset_mdrefr & ~MDREFR_DRI_MASK);
  245. preset_mdrefr |= mdrefr_dri(new_freq_mem);
  246. }
  247. postset_mdrefr =
  248. (postset_mdrefr & ~MDREFR_DRI_MASK) | mdrefr_dri(new_freq_mem);
  249. /* If we're dividing the memory clock by two for the SDRAM clock, this
  250. * must be set prior to the change. Clearing the divide must be done
  251. * after the change.
  252. */
  253. if (pxa_freq_settings[idx].div2) {
  254. preset_mdrefr |= MDREFR_DB2_MASK;
  255. postset_mdrefr |= MDREFR_DB2_MASK;
  256. } else {
  257. postset_mdrefr &= ~MDREFR_DB2_MASK;
  258. }
  259. local_irq_save(flags);
  260. /* Set new the CCCR and prepare CCLKCFG */
  261. CCCR = pxa_freq_settings[idx].cccr;
  262. cclkcfg = pxa_freq_settings[idx].cclkcfg;
  263. asm volatile(" \n\
  264. ldr r4, [%1] /* load MDREFR */ \n\
  265. b 2f \n\
  266. .align 5 \n\
  267. 1: \n\
  268. str %3, [%1] /* preset the MDREFR */ \n\
  269. mcr p14, 0, %2, c6, c0, 0 /* set CCLKCFG[FCS] */ \n\
  270. str %4, [%1] /* postset the MDREFR */ \n\
  271. \n\
  272. b 3f \n\
  273. 2: b 1b \n\
  274. 3: nop \n\
  275. "
  276. : "=&r" (unused)
  277. : "r" (&MDREFR), "r" (cclkcfg),
  278. "r" (preset_mdrefr), "r" (postset_mdrefr)
  279. : "r4", "r5");
  280. local_irq_restore(flags);
  281. /*
  282. * Tell everyone what we've just done...
  283. * you should add a notify client with any platform specific
  284. * SDRAM refresh timer adjustments
  285. */
  286. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  287. return 0;
  288. }
  289. static __init int pxa_cpufreq_init(struct cpufreq_policy *policy)
  290. {
  291. int i;
  292. unsigned int freq;
  293. /* try to guess pxa27x cpu */
  294. if (cpu_is_pxa27x())
  295. pxa27x_guess_max_freq();
  296. /* set default policy and cpuinfo */
  297. policy->cpuinfo.transition_latency = 1000; /* FIXME: 1 ms, assumed */
  298. policy->cur = get_clk_frequency_khz(0); /* current freq */
  299. policy->min = policy->max = policy->cur;
  300. /* Generate pxa25x the run cpufreq_frequency_table struct */
  301. for (i = 0; i < NUM_PXA25x_RUN_FREQS; i++) {
  302. pxa255_run_freq_table[i].frequency = pxa255_run_freqs[i].khz;
  303. pxa255_run_freq_table[i].index = i;
  304. }
  305. pxa255_run_freq_table[i].frequency = CPUFREQ_TABLE_END;
  306. /* Generate pxa25x the turbo cpufreq_frequency_table struct */
  307. for (i = 0; i < NUM_PXA25x_TURBO_FREQS; i++) {
  308. pxa255_turbo_freq_table[i].frequency =
  309. pxa255_turbo_freqs[i].khz;
  310. pxa255_turbo_freq_table[i].index = i;
  311. }
  312. pxa255_turbo_freq_table[i].frequency = CPUFREQ_TABLE_END;
  313. /* Generate the pxa27x cpufreq_frequency_table struct */
  314. for (i = 0; i < NUM_PXA27x_FREQS; i++) {
  315. freq = pxa27x_freqs[i].khz;
  316. if (freq > pxa27x_maxfreq)
  317. break;
  318. pxa27x_freq_table[i].frequency = freq;
  319. pxa27x_freq_table[i].index = i;
  320. }
  321. pxa27x_freq_table[i].frequency = CPUFREQ_TABLE_END;
  322. /*
  323. * Set the policy's minimum and maximum frequencies from the tables
  324. * just constructed. This sets cpuinfo.mxx_freq, min and max.
  325. */
  326. if (cpu_is_pxa25x())
  327. cpufreq_frequency_table_cpuinfo(policy, pxa255_run_freq_table);
  328. else if (cpu_is_pxa27x())
  329. cpufreq_frequency_table_cpuinfo(policy, pxa27x_freq_table);
  330. printk(KERN_INFO "PXA CPU frequency change support initialized\n");
  331. return 0;
  332. }
  333. static struct cpufreq_driver pxa_cpufreq_driver = {
  334. .verify = pxa_verify_policy,
  335. .target = pxa_set_target,
  336. .init = pxa_cpufreq_init,
  337. .get = pxa_cpufreq_get,
  338. .name = "PXA2xx",
  339. };
  340. static int __init pxa_cpu_init(void)
  341. {
  342. int ret = -ENODEV;
  343. if (cpu_is_pxa25x() || cpu_is_pxa27x())
  344. ret = cpufreq_register_driver(&pxa_cpufreq_driver);
  345. return ret;
  346. }
  347. static void __exit pxa_cpu_exit(void)
  348. {
  349. cpufreq_unregister_driver(&pxa_cpufreq_driver);
  350. }
  351. MODULE_AUTHOR("Intrinsyc Software Inc.");
  352. MODULE_DESCRIPTION("CPU frequency changing driver for the PXA architecture");
  353. MODULE_LICENSE("GPL");
  354. module_init(pxa_cpu_init);
  355. module_exit(pxa_cpu_exit);