pasemi-cpufreq.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <linux/module.h>
  30. #include <linux/of_address.h>
  31. #include <asm/hw_irq.h>
  32. #include <asm/io.h>
  33. #include <asm/prom.h>
  34. #include <asm/time.h>
  35. #include <asm/smp.h>
  36. #define SDCASR_REG 0x0100
  37. #define SDCASR_REG_STRIDE 0x1000
  38. #define SDCPWR_CFGA0_REG 0x0100
  39. #define SDCPWR_PWST0_REG 0x0000
  40. #define SDCPWR_GIZTIME_REG 0x0440
  41. /* SDCPWR_GIZTIME_REG fields */
  42. #define SDCPWR_GIZTIME_GR 0x80000000
  43. #define SDCPWR_GIZTIME_LONGLOCK 0x000000ff
  44. /* Offset of ASR registers from SDC base */
  45. #define SDCASR_OFFSET 0x120000
  46. static void __iomem *sdcpwr_mapbase;
  47. static void __iomem *sdcasr_mapbase;
  48. /* Current astate, is used when waking up from power savings on
  49. * one core, in case the other core has switched states during
  50. * the idle time.
  51. */
  52. static int current_astate;
  53. /* We support 5(A0-A4) power states excluding turbo(A5-A6) modes */
  54. static struct cpufreq_frequency_table pas_freqs[] = {
  55. {0, 0, 0},
  56. {0, 1, 0},
  57. {0, 2, 0},
  58. {0, 3, 0},
  59. {0, 4, 0},
  60. {0, 0, CPUFREQ_TABLE_END},
  61. };
  62. /*
  63. * hardware specific functions
  64. */
  65. static int get_astate_freq(int astate)
  66. {
  67. u32 ret;
  68. ret = in_le32(sdcpwr_mapbase + SDCPWR_CFGA0_REG + (astate * 0x10));
  69. return ret & 0x3f;
  70. }
  71. static int get_cur_astate(int cpu)
  72. {
  73. u32 ret;
  74. ret = in_le32(sdcpwr_mapbase + SDCPWR_PWST0_REG);
  75. ret = (ret >> (cpu * 4)) & 0x7;
  76. return ret;
  77. }
  78. static int get_gizmo_latency(void)
  79. {
  80. u32 giztime, ret;
  81. giztime = in_le32(sdcpwr_mapbase + SDCPWR_GIZTIME_REG);
  82. /* just provide the upper bound */
  83. if (giztime & SDCPWR_GIZTIME_GR)
  84. ret = (giztime & SDCPWR_GIZTIME_LONGLOCK) * 128000;
  85. else
  86. ret = (giztime & SDCPWR_GIZTIME_LONGLOCK) * 1000;
  87. return ret;
  88. }
  89. static void set_astate(int cpu, unsigned int astate)
  90. {
  91. unsigned long flags;
  92. /* Return if called before init has run */
  93. if (unlikely(!sdcasr_mapbase))
  94. return;
  95. local_irq_save(flags);
  96. out_le32(sdcasr_mapbase + SDCASR_REG + SDCASR_REG_STRIDE*cpu, astate);
  97. local_irq_restore(flags);
  98. }
  99. int check_astate(void)
  100. {
  101. return get_cur_astate(hard_smp_processor_id());
  102. }
  103. void restore_astate(int cpu)
  104. {
  105. set_astate(cpu, current_astate);
  106. }
  107. /*
  108. * cpufreq functions
  109. */
  110. static int pas_cpufreq_cpu_init(struct cpufreq_policy *policy)
  111. {
  112. struct cpufreq_frequency_table *pos;
  113. const u32 *max_freqp;
  114. u32 max_freq;
  115. int cur_astate;
  116. struct resource res;
  117. struct device_node *cpu, *dn;
  118. int err = -ENODEV;
  119. cpu = of_get_cpu_node(policy->cpu, NULL);
  120. if (!cpu)
  121. goto out;
  122. dn = of_find_compatible_node(NULL, NULL, "1682m-sdc");
  123. if (!dn)
  124. dn = of_find_compatible_node(NULL, NULL,
  125. "pasemi,pwrficient-sdc");
  126. if (!dn)
  127. goto out;
  128. err = of_address_to_resource(dn, 0, &res);
  129. of_node_put(dn);
  130. if (err)
  131. goto out;
  132. sdcasr_mapbase = ioremap(res.start + SDCASR_OFFSET, 0x2000);
  133. if (!sdcasr_mapbase) {
  134. err = -EINVAL;
  135. goto out;
  136. }
  137. dn = of_find_compatible_node(NULL, NULL, "1682m-gizmo");
  138. if (!dn)
  139. dn = of_find_compatible_node(NULL, NULL,
  140. "pasemi,pwrficient-gizmo");
  141. if (!dn) {
  142. err = -ENODEV;
  143. goto out_unmap_sdcasr;
  144. }
  145. err = of_address_to_resource(dn, 0, &res);
  146. of_node_put(dn);
  147. if (err)
  148. goto out_unmap_sdcasr;
  149. sdcpwr_mapbase = ioremap(res.start, 0x1000);
  150. if (!sdcpwr_mapbase) {
  151. err = -EINVAL;
  152. goto out_unmap_sdcasr;
  153. }
  154. pr_debug("init cpufreq on CPU %d\n", policy->cpu);
  155. max_freqp = of_get_property(cpu, "clock-frequency", NULL);
  156. if (!max_freqp) {
  157. err = -EINVAL;
  158. goto out_unmap_sdcpwr;
  159. }
  160. /* we need the freq in kHz */
  161. max_freq = *max_freqp / 1000;
  162. pr_debug("max clock-frequency is at %u kHz\n", max_freq);
  163. pr_debug("initializing frequency table\n");
  164. /* initialize frequency table */
  165. cpufreq_for_each_entry(pos, pas_freqs) {
  166. pos->frequency = get_astate_freq(pos->driver_data) * 100000;
  167. pr_debug("%d: %d\n", (int)(pos - pas_freqs), pos->frequency);
  168. }
  169. cur_astate = get_cur_astate(policy->cpu);
  170. pr_debug("current astate is at %d\n",cur_astate);
  171. policy->cur = pas_freqs[cur_astate].frequency;
  172. ppc_proc_freq = policy->cur * 1000ul;
  173. return cpufreq_generic_init(policy, pas_freqs, get_gizmo_latency());
  174. out_unmap_sdcpwr:
  175. iounmap(sdcpwr_mapbase);
  176. out_unmap_sdcasr:
  177. iounmap(sdcasr_mapbase);
  178. out:
  179. return err;
  180. }
  181. static int pas_cpufreq_cpu_exit(struct cpufreq_policy *policy)
  182. {
  183. /*
  184. * We don't support CPU hotplug. Don't unmap after the system
  185. * has already made it to a running state.
  186. */
  187. if (system_state != SYSTEM_BOOTING)
  188. return 0;
  189. if (sdcasr_mapbase)
  190. iounmap(sdcasr_mapbase);
  191. if (sdcpwr_mapbase)
  192. iounmap(sdcpwr_mapbase);
  193. return 0;
  194. }
  195. static int pas_cpufreq_target(struct cpufreq_policy *policy,
  196. unsigned int pas_astate_new)
  197. {
  198. int i;
  199. pr_debug("setting frequency for cpu %d to %d kHz, 1/%d of max frequency\n",
  200. policy->cpu,
  201. pas_freqs[pas_astate_new].frequency,
  202. pas_freqs[pas_astate_new].driver_data);
  203. current_astate = pas_astate_new;
  204. for_each_online_cpu(i)
  205. set_astate(i, pas_astate_new);
  206. ppc_proc_freq = pas_freqs[pas_astate_new].frequency * 1000ul;
  207. return 0;
  208. }
  209. static struct cpufreq_driver pas_cpufreq_driver = {
  210. .name = "pas-cpufreq",
  211. .flags = CPUFREQ_CONST_LOOPS,
  212. .init = pas_cpufreq_cpu_init,
  213. .exit = pas_cpufreq_cpu_exit,
  214. .verify = cpufreq_generic_frequency_table_verify,
  215. .target_index = pas_cpufreq_target,
  216. .attr = cpufreq_generic_attr,
  217. };
  218. /*
  219. * module init and destoy
  220. */
  221. static int __init pas_cpufreq_init(void)
  222. {
  223. if (!of_machine_is_compatible("PA6T-1682M") &&
  224. !of_machine_is_compatible("pasemi,pwrficient"))
  225. return -ENODEV;
  226. return cpufreq_register_driver(&pas_cpufreq_driver);
  227. }
  228. static void __exit pas_cpufreq_exit(void)
  229. {
  230. cpufreq_unregister_driver(&pas_cpufreq_driver);
  231. }
  232. module_init(pas_cpufreq_init);
  233. module_exit(pas_cpufreq_exit);
  234. MODULE_LICENSE("GPL");
  235. MODULE_AUTHOR("Egor Martovetsky <egor@pasemi.com>, Olof Johansson <olof@lixom.net>");