cpufreq-pxa2xx.c 13 KB

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