cpu.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * linux/arch/arm/mach-integrator/cpu.c
  3. *
  4. * Copyright (C) 2001-2002 Deep Blue Solutions Ltd.
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * CPU support functions
  11. */
  12. #include <linux/module.h>
  13. #include <linux/types.h>
  14. #include <linux/kernel.h>
  15. #include <linux/cpufreq.h>
  16. #include <linux/slab.h>
  17. #include <linux/sched.h>
  18. #include <linux/smp.h>
  19. #include <linux/init.h>
  20. #include <linux/io.h>
  21. #include <mach/hardware.h>
  22. #include <mach/platform.h>
  23. #include <asm/mach-types.h>
  24. #include <asm/hardware/icst525.h>
  25. static struct cpufreq_driver integrator_driver;
  26. #define CM_ID IO_ADDRESS(INTEGRATOR_HDR_ID)
  27. #define CM_OSC IO_ADDRESS(INTEGRATOR_HDR_OSC)
  28. #define CM_STAT IO_ADDRESS(INTEGRATOR_HDR_STAT)
  29. #define CM_LOCK IO_ADDRESS(INTEGRATOR_HDR_LOCK)
  30. static const struct icst_params lclk_params = {
  31. .ref = 24000000,
  32. .vco_max = ICST525_VCO_MAX_5V,
  33. .vco_min = ICST525_VCO_MIN,
  34. .vd_min = 8,
  35. .vd_max = 132,
  36. .rd_min = 24,
  37. .rd_max = 24,
  38. .s2div = icst525_s2div,
  39. .idx2s = icst525_idx2s,
  40. };
  41. static const struct icst_params cclk_params = {
  42. .ref = 24000000,
  43. .vco_max = ICST525_VCO_MAX_5V,
  44. .vco_min = ICST525_VCO_MIN,
  45. .vd_min = 12,
  46. .vd_max = 160,
  47. .rd_min = 24,
  48. .rd_max = 24,
  49. .s2div = icst525_s2div,
  50. .idx2s = icst525_idx2s,
  51. };
  52. /*
  53. * Validate the speed policy.
  54. */
  55. static int integrator_verify_policy(struct cpufreq_policy *policy)
  56. {
  57. struct icst_vco vco;
  58. cpufreq_verify_within_limits(policy,
  59. policy->cpuinfo.min_freq,
  60. policy->cpuinfo.max_freq);
  61. vco = icst525_hz_to_vco(&cclk_params, policy->max * 1000);
  62. policy->max = icst525_hz(&cclk_params, vco) / 1000;
  63. vco = icst525_hz_to_vco(&cclk_params, policy->min * 1000);
  64. policy->min = icst525_hz(&cclk_params, vco) / 1000;
  65. cpufreq_verify_within_limits(policy,
  66. policy->cpuinfo.min_freq,
  67. policy->cpuinfo.max_freq);
  68. return 0;
  69. }
  70. static int integrator_set_target(struct cpufreq_policy *policy,
  71. unsigned int target_freq,
  72. unsigned int relation)
  73. {
  74. cpumask_t cpus_allowed;
  75. int cpu = policy->cpu;
  76. struct icst_vco vco;
  77. struct cpufreq_freqs freqs;
  78. u_int cm_osc;
  79. /*
  80. * Save this threads cpus_allowed mask.
  81. */
  82. cpus_allowed = current->cpus_allowed;
  83. /*
  84. * Bind to the specified CPU. When this call returns,
  85. * we should be running on the right CPU.
  86. */
  87. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  88. BUG_ON(cpu != smp_processor_id());
  89. /* get current setting */
  90. cm_osc = __raw_readl(CM_OSC);
  91. if (machine_is_integrator()) {
  92. vco.s = (cm_osc >> 8) & 7;
  93. } else if (machine_is_cintegrator()) {
  94. vco.s = 1;
  95. }
  96. vco.v = cm_osc & 255;
  97. vco.r = 22;
  98. freqs.old = icst525_hz(&cclk_params, vco) / 1000;
  99. /* icst525_hz_to_vco rounds down -- so we need the next
  100. * larger freq in case of CPUFREQ_RELATION_L.
  101. */
  102. if (relation == CPUFREQ_RELATION_L)
  103. target_freq += 999;
  104. if (target_freq > policy->max)
  105. target_freq = policy->max;
  106. vco = icst525_hz_to_vco(&cclk_params, target_freq * 1000);
  107. freqs.new = icst525_hz(&cclk_params, vco) / 1000;
  108. freqs.cpu = policy->cpu;
  109. if (freqs.old == freqs.new) {
  110. set_cpus_allowed(current, cpus_allowed);
  111. return 0;
  112. }
  113. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  114. cm_osc = __raw_readl(CM_OSC);
  115. if (machine_is_integrator()) {
  116. cm_osc &= 0xfffff800;
  117. cm_osc |= vco.s << 8;
  118. } else if (machine_is_cintegrator()) {
  119. cm_osc &= 0xffffff00;
  120. }
  121. cm_osc |= vco.v;
  122. __raw_writel(0xa05f, CM_LOCK);
  123. __raw_writel(cm_osc, CM_OSC);
  124. __raw_writel(0, CM_LOCK);
  125. /*
  126. * Restore the CPUs allowed mask.
  127. */
  128. set_cpus_allowed(current, cpus_allowed);
  129. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  130. return 0;
  131. }
  132. static unsigned int integrator_get(unsigned int cpu)
  133. {
  134. cpumask_t cpus_allowed;
  135. unsigned int current_freq;
  136. u_int cm_osc;
  137. struct icst_vco vco;
  138. cpus_allowed = current->cpus_allowed;
  139. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  140. BUG_ON(cpu != smp_processor_id());
  141. /* detect memory etc. */
  142. cm_osc = __raw_readl(CM_OSC);
  143. if (machine_is_integrator()) {
  144. vco.s = (cm_osc >> 8) & 7;
  145. } else if (machine_is_cintegrator()) {
  146. vco.s = 1;
  147. }
  148. vco.v = cm_osc & 255;
  149. vco.r = 22;
  150. current_freq = icst525_hz(&cclk_params, vco) / 1000; /* current freq */
  151. set_cpus_allowed(current, cpus_allowed);
  152. return current_freq;
  153. }
  154. static int integrator_cpufreq_init(struct cpufreq_policy *policy)
  155. {
  156. /* set default policy and cpuinfo */
  157. policy->cpuinfo.max_freq = 160000;
  158. policy->cpuinfo.min_freq = 12000;
  159. policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
  160. policy->cur = policy->min = policy->max = integrator_get(policy->cpu);
  161. return 0;
  162. }
  163. static struct cpufreq_driver integrator_driver = {
  164. .verify = integrator_verify_policy,
  165. .target = integrator_set_target,
  166. .get = integrator_get,
  167. .init = integrator_cpufreq_init,
  168. .name = "integrator",
  169. };
  170. static int __init integrator_cpu_init(void)
  171. {
  172. return cpufreq_register_driver(&integrator_driver);
  173. }
  174. static void __exit integrator_cpu_exit(void)
  175. {
  176. cpufreq_unregister_driver(&integrator_driver);
  177. }
  178. MODULE_AUTHOR ("Russell M. King");
  179. MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
  180. MODULE_LICENSE ("GPL");
  181. module_init(integrator_cpu_init);
  182. module_exit(integrator_cpu_exit);