cpufreq-pxa2xx.c 13 KB

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