pxa3xx-cpufreq.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2008 Marvell International Ltd.
  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. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/sched.h>
  12. #include <linux/init.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <mach/generic.h>
  17. #include <mach/pxa3xx-regs.h>
  18. #define HSS_104M (0)
  19. #define HSS_156M (1)
  20. #define HSS_208M (2)
  21. #define HSS_312M (3)
  22. #define SMCFS_78M (0)
  23. #define SMCFS_104M (2)
  24. #define SMCFS_208M (5)
  25. #define SFLFS_104M (0)
  26. #define SFLFS_156M (1)
  27. #define SFLFS_208M (2)
  28. #define SFLFS_312M (3)
  29. #define XSPCLK_156M (0)
  30. #define XSPCLK_NONE (3)
  31. #define DMCFS_26M (0)
  32. #define DMCFS_260M (3)
  33. struct pxa3xx_freq_info {
  34. unsigned int cpufreq_mhz;
  35. unsigned int core_xl : 5;
  36. unsigned int core_xn : 3;
  37. unsigned int hss : 2;
  38. unsigned int dmcfs : 2;
  39. unsigned int smcfs : 3;
  40. unsigned int sflfs : 2;
  41. unsigned int df_clkdiv : 3;
  42. int vcc_core; /* in mV */
  43. int vcc_sram; /* in mV */
  44. };
  45. #define OP(cpufreq, _xl, _xn, _hss, _dmc, _smc, _sfl, _dfi, vcore, vsram) \
  46. { \
  47. .cpufreq_mhz = cpufreq, \
  48. .core_xl = _xl, \
  49. .core_xn = _xn, \
  50. .hss = HSS_##_hss##M, \
  51. .dmcfs = DMCFS_##_dmc##M, \
  52. .smcfs = SMCFS_##_smc##M, \
  53. .sflfs = SFLFS_##_sfl##M, \
  54. .df_clkdiv = _dfi, \
  55. .vcc_core = vcore, \
  56. .vcc_sram = vsram, \
  57. }
  58. static struct pxa3xx_freq_info pxa300_freqs[] = {
  59. /* CPU XL XN HSS DMEM SMEM SRAM DFI VCC_CORE VCC_SRAM */
  60. OP(104, 8, 1, 104, 260, 78, 104, 3, 1000, 1100), /* 104MHz */
  61. OP(208, 16, 1, 104, 260, 104, 156, 2, 1000, 1100), /* 208MHz */
  62. OP(416, 16, 2, 156, 260, 104, 208, 2, 1100, 1200), /* 416MHz */
  63. OP(624, 24, 2, 208, 260, 208, 312, 3, 1375, 1400), /* 624MHz */
  64. };
  65. static struct pxa3xx_freq_info pxa320_freqs[] = {
  66. /* CPU XL XN HSS DMEM SMEM SRAM DFI VCC_CORE VCC_SRAM */
  67. OP(104, 8, 1, 104, 260, 78, 104, 3, 1000, 1100), /* 104MHz */
  68. OP(208, 16, 1, 104, 260, 104, 156, 2, 1000, 1100), /* 208MHz */
  69. OP(416, 16, 2, 156, 260, 104, 208, 2, 1100, 1200), /* 416MHz */
  70. OP(624, 24, 2, 208, 260, 208, 312, 3, 1375, 1400), /* 624MHz */
  71. OP(806, 31, 2, 208, 260, 208, 312, 3, 1400, 1400), /* 806MHz */
  72. };
  73. static unsigned int pxa3xx_freqs_num;
  74. static struct pxa3xx_freq_info *pxa3xx_freqs;
  75. static struct cpufreq_frequency_table *pxa3xx_freqs_table;
  76. static int setup_freqs_table(struct cpufreq_policy *policy,
  77. struct pxa3xx_freq_info *freqs, int num)
  78. {
  79. struct cpufreq_frequency_table *table;
  80. int i;
  81. table = kzalloc((num + 1) * sizeof(*table), GFP_KERNEL);
  82. if (table == NULL)
  83. return -ENOMEM;
  84. for (i = 0; i < num; i++) {
  85. table[i].driver_data = i;
  86. table[i].frequency = freqs[i].cpufreq_mhz * 1000;
  87. }
  88. table[num].driver_data = i;
  89. table[num].frequency = CPUFREQ_TABLE_END;
  90. pxa3xx_freqs = freqs;
  91. pxa3xx_freqs_num = num;
  92. pxa3xx_freqs_table = table;
  93. return cpufreq_table_validate_and_show(policy, table);
  94. }
  95. static void __update_core_freq(struct pxa3xx_freq_info *info)
  96. {
  97. uint32_t mask = ACCR_XN_MASK | ACCR_XL_MASK;
  98. uint32_t accr = ACCR;
  99. uint32_t xclkcfg;
  100. accr &= ~(ACCR_XN_MASK | ACCR_XL_MASK | ACCR_XSPCLK_MASK);
  101. accr |= ACCR_XN(info->core_xn) | ACCR_XL(info->core_xl);
  102. /* No clock until core PLL is re-locked */
  103. accr |= ACCR_XSPCLK(XSPCLK_NONE);
  104. xclkcfg = (info->core_xn == 2) ? 0x3 : 0x2; /* turbo bit */
  105. ACCR = accr;
  106. __asm__("mcr p14, 0, %0, c6, c0, 0\n" : : "r"(xclkcfg));
  107. while ((ACSR & mask) != (accr & mask))
  108. cpu_relax();
  109. }
  110. static void __update_bus_freq(struct pxa3xx_freq_info *info)
  111. {
  112. uint32_t mask;
  113. uint32_t accr = ACCR;
  114. mask = ACCR_SMCFS_MASK | ACCR_SFLFS_MASK | ACCR_HSS_MASK |
  115. ACCR_DMCFS_MASK;
  116. accr &= ~mask;
  117. accr |= ACCR_SMCFS(info->smcfs) | ACCR_SFLFS(info->sflfs) |
  118. ACCR_HSS(info->hss) | ACCR_DMCFS(info->dmcfs);
  119. ACCR = accr;
  120. while ((ACSR & mask) != (accr & mask))
  121. cpu_relax();
  122. }
  123. static unsigned int pxa3xx_cpufreq_get(unsigned int cpu)
  124. {
  125. return pxa3xx_get_clk_frequency_khz(0);
  126. }
  127. static int pxa3xx_cpufreq_set(struct cpufreq_policy *policy, unsigned int index)
  128. {
  129. struct pxa3xx_freq_info *next;
  130. unsigned long flags;
  131. if (policy->cpu != 0)
  132. return -EINVAL;
  133. next = &pxa3xx_freqs[index];
  134. local_irq_save(flags);
  135. __update_core_freq(next);
  136. __update_bus_freq(next);
  137. local_irq_restore(flags);
  138. return 0;
  139. }
  140. static int pxa3xx_cpufreq_init(struct cpufreq_policy *policy)
  141. {
  142. int ret = -EINVAL;
  143. /* set default policy and cpuinfo */
  144. policy->min = policy->cpuinfo.min_freq = 104000;
  145. policy->max = policy->cpuinfo.max_freq =
  146. (cpu_is_pxa320()) ? 806000 : 624000;
  147. policy->cpuinfo.transition_latency = 1000; /* FIXME: 1 ms, assumed */
  148. if (cpu_is_pxa300() || cpu_is_pxa310())
  149. ret = setup_freqs_table(policy, pxa300_freqs,
  150. ARRAY_SIZE(pxa300_freqs));
  151. if (cpu_is_pxa320())
  152. ret = setup_freqs_table(policy, pxa320_freqs,
  153. ARRAY_SIZE(pxa320_freqs));
  154. if (ret) {
  155. pr_err("failed to setup frequency table\n");
  156. return ret;
  157. }
  158. pr_info("CPUFREQ support for PXA3xx initialized\n");
  159. return 0;
  160. }
  161. static struct cpufreq_driver pxa3xx_cpufreq_driver = {
  162. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  163. .verify = cpufreq_generic_frequency_table_verify,
  164. .target_index = pxa3xx_cpufreq_set,
  165. .init = pxa3xx_cpufreq_init,
  166. .get = pxa3xx_cpufreq_get,
  167. .name = "pxa3xx-cpufreq",
  168. };
  169. static int __init cpufreq_init(void)
  170. {
  171. if (cpu_is_pxa3xx())
  172. return cpufreq_register_driver(&pxa3xx_cpufreq_driver);
  173. return 0;
  174. }
  175. module_init(cpufreq_init);
  176. static void __exit cpufreq_exit(void)
  177. {
  178. cpufreq_unregister_driver(&pxa3xx_cpufreq_driver);
  179. }
  180. module_exit(cpufreq_exit);
  181. MODULE_DESCRIPTION("CPU frequency scaling driver for PXA3xx");
  182. MODULE_LICENSE("GPL");