cpufreq.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2007 PA Semi, Inc
  3. *
  4. * Authors: Egor Martovetsky <egor@pasemi.com>
  5. * Olof Johansson <olof@lixom.net>
  6. *
  7. * Maintained by: Olof Johansson <olof@lixom.net>
  8. *
  9. * Based on arch/powerpc/platforms/cell/cbe_cpufreq.c:
  10. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. */
  27. #include <linux/cpufreq.h>
  28. #include <linux/timer.h>
  29. #include <asm/hw_irq.h>
  30. #include <asm/io.h>
  31. #include <asm/prom.h>
  32. #define SDCASR_REG 0x0100
  33. #define SDCASR_REG_STRIDE 0x1000
  34. #define SDCPWR_CFGA0_REG 0x0100
  35. #define SDCPWR_PWST0_REG 0x0000
  36. #define SDCPWR_GIZTIME_REG 0x0440
  37. /* SDCPWR_GIZTIME_REG fields */
  38. #define SDCPWR_GIZTIME_GR 0x80000000
  39. #define SDCPWR_GIZTIME_LONGLOCK 0x000000ff
  40. /* Offset of ASR registers from SDC base */
  41. #define SDCASR_OFFSET 0x120000
  42. static void __iomem *sdcpwr_mapbase;
  43. static void __iomem *sdcasr_mapbase;
  44. static DEFINE_MUTEX(pas_switch_mutex);
  45. /* Current astate, is used when waking up from power savings on
  46. * one core, in case the other core has switched states during
  47. * the idle time.
  48. */
  49. static int current_astate;
  50. /* We support 5(A0-A4) power states excluding turbo(A5-A6) modes */
  51. static struct cpufreq_frequency_table pas_freqs[] = {
  52. {0, 0},
  53. {1, 0},
  54. {2, 0},
  55. {3, 0},
  56. {4, 0},
  57. {0, CPUFREQ_TABLE_END},
  58. };
  59. static struct freq_attr *pas_cpu_freqs_attr[] = {
  60. &cpufreq_freq_attr_scaling_available_freqs,
  61. NULL,
  62. };
  63. /*
  64. * hardware specific functions
  65. */
  66. static int get_astate_freq(int astate)
  67. {
  68. u32 ret;
  69. ret = in_le32(sdcpwr_mapbase + SDCPWR_CFGA0_REG + (astate * 0x10));
  70. return ret & 0x3f;
  71. }
  72. static int get_cur_astate(int cpu)
  73. {
  74. u32 ret;
  75. ret = in_le32(sdcpwr_mapbase + SDCPWR_PWST0_REG);
  76. ret = (ret >> (cpu * 4)) & 0x7;
  77. return ret;
  78. }
  79. static int get_gizmo_latency(void)
  80. {
  81. u32 giztime, ret;
  82. giztime = in_le32(sdcpwr_mapbase + SDCPWR_GIZTIME_REG);
  83. /* just provide the upper bound */
  84. if (giztime & SDCPWR_GIZTIME_GR)
  85. ret = (giztime & SDCPWR_GIZTIME_LONGLOCK) * 128000;
  86. else
  87. ret = (giztime & SDCPWR_GIZTIME_LONGLOCK) * 1000;
  88. return ret;
  89. }
  90. static void set_astate(int cpu, unsigned int astate)
  91. {
  92. u64 flags;
  93. /* Return if called before init has run */
  94. if (unlikely(!sdcasr_mapbase))
  95. return;
  96. local_irq_save(flags);
  97. out_le32(sdcasr_mapbase + SDCASR_REG + SDCASR_REG_STRIDE*cpu, astate);
  98. local_irq_restore(flags);
  99. }
  100. void restore_astate(int cpu)
  101. {
  102. set_astate(cpu, current_astate);
  103. }
  104. /*
  105. * cpufreq functions
  106. */
  107. static int pas_cpufreq_cpu_init(struct cpufreq_policy *policy)
  108. {
  109. u32 *max_freq;
  110. int i, cur_astate;
  111. struct resource res;
  112. struct device_node *cpu, *dn;
  113. int err = -ENODEV;
  114. cpu = of_get_cpu_node(policy->cpu, NULL);
  115. if (!cpu)
  116. goto out;
  117. dn = of_find_compatible_node(NULL, "sdc", "1682m-sdc");
  118. if (!dn)
  119. goto out;
  120. err = of_address_to_resource(dn, 0, &res);
  121. of_node_put(dn);
  122. if (err)
  123. goto out;
  124. sdcasr_mapbase = ioremap(res.start + SDCASR_OFFSET, 0x2000);
  125. if (!sdcasr_mapbase) {
  126. err = -EINVAL;
  127. goto out;
  128. }
  129. dn = of_find_compatible_node(NULL, "gizmo", "1682m-gizmo");
  130. if (!dn) {
  131. err = -ENODEV;
  132. goto out_unmap_sdcasr;
  133. }
  134. err = of_address_to_resource(dn, 0, &res);
  135. of_node_put(dn);
  136. if (err)
  137. goto out_unmap_sdcasr;
  138. sdcpwr_mapbase = ioremap(res.start, 0x1000);
  139. if (!sdcpwr_mapbase) {
  140. err = -EINVAL;
  141. goto out_unmap_sdcasr;
  142. }
  143. pr_debug("init cpufreq on CPU %d\n", policy->cpu);
  144. max_freq = (u32*) get_property(cpu, "clock-frequency", NULL);
  145. if (!max_freq) {
  146. err = -EINVAL;
  147. goto out_unmap_sdcpwr;
  148. }
  149. /* we need the freq in kHz */
  150. *max_freq /= 1000;
  151. pr_debug("max clock-frequency is at %u kHz\n", *max_freq);
  152. pr_debug("initializing frequency table\n");
  153. /* initialize frequency table */
  154. for (i=0; pas_freqs[i].frequency!=CPUFREQ_TABLE_END; i++) {
  155. pas_freqs[i].frequency = get_astate_freq(pas_freqs[i].index) * 100000;
  156. pr_debug("%d: %d\n", i, pas_freqs[i].frequency);
  157. }
  158. policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
  159. policy->cpuinfo.transition_latency = get_gizmo_latency();
  160. cur_astate = get_cur_astate(policy->cpu);
  161. pr_debug("current astate is at %d\n",cur_astate);
  162. policy->cur = pas_freqs[cur_astate].frequency;
  163. policy->cpus = cpu_online_map;
  164. cpufreq_frequency_table_get_attr(pas_freqs, policy->cpu);
  165. /* this ensures that policy->cpuinfo_min and policy->cpuinfo_max
  166. * are set correctly
  167. */
  168. return cpufreq_frequency_table_cpuinfo(policy, pas_freqs);
  169. out_unmap_sdcpwr:
  170. iounmap(sdcpwr_mapbase);
  171. out_unmap_sdcasr:
  172. iounmap(sdcasr_mapbase);
  173. out:
  174. return err;
  175. }
  176. static int pas_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  177. {
  178. if (sdcasr_mapbase)
  179. iounmap(sdcasr_mapbase);
  180. if (sdcpwr_mapbase)
  181. iounmap(sdcpwr_mapbase);
  182. cpufreq_frequency_table_put_attr(policy->cpu);
  183. return 0;
  184. }
  185. static int pas_cpufreq_verify(struct cpufreq_policy *policy)
  186. {
  187. return cpufreq_frequency_table_verify(policy, pas_freqs);
  188. }
  189. static int pas_cpufreq_target(struct cpufreq_policy *policy,
  190. unsigned int target_freq,
  191. unsigned int relation)
  192. {
  193. struct cpufreq_freqs freqs;
  194. int pas_astate_new;
  195. int i;
  196. cpufreq_frequency_table_target(policy,
  197. pas_freqs,
  198. target_freq,
  199. relation,
  200. &pas_astate_new);
  201. freqs.old = policy->cur;
  202. freqs.new = pas_freqs[pas_astate_new].frequency;
  203. freqs.cpu = policy->cpu;
  204. mutex_lock(&pas_switch_mutex);
  205. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  206. pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
  207. policy->cpu,
  208. pas_freqs[pas_astate_new].frequency,
  209. pas_freqs[pas_astate_new].index);
  210. current_astate = pas_astate_new;
  211. for_each_online_cpu(i)
  212. set_astate(i, pas_astate_new);
  213. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  214. mutex_unlock(&pas_switch_mutex);
  215. return 0;
  216. }
  217. static struct cpufreq_driver pas_cpufreq_driver = {
  218. .name = "pas-cpufreq",
  219. .owner = THIS_MODULE,
  220. .flags = CPUFREQ_CONST_LOOPS,
  221. .init = pas_cpufreq_cpu_init,
  222. .exit = pas_cpufreq_cpu_exit,
  223. .verify = pas_cpufreq_verify,
  224. .target = pas_cpufreq_target,
  225. .attr = pas_cpu_freqs_attr,
  226. };
  227. /*
  228. * module init and destoy
  229. */
  230. static int __init pas_cpufreq_init(void)
  231. {
  232. if (!machine_is_compatible("PA6T-1682M"))
  233. return -ENODEV;
  234. return cpufreq_register_driver(&pas_cpufreq_driver);
  235. }
  236. static void __exit pas_cpufreq_exit(void)
  237. {
  238. cpufreq_unregister_driver(&pas_cpufreq_driver);
  239. }
  240. module_init(pas_cpufreq_init);
  241. module_exit(pas_cpufreq_exit);
  242. MODULE_LICENSE("GPL");
  243. MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>, Olof Johansson <olof@lixom.net>");