pasemi-cpufreq.c 6.0 KB

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