cpu.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 = 24000,
  32. .vco_max = 320000,
  33. .vd_min = 8,
  34. .vd_max = 132,
  35. .rd_min = 24,
  36. .rd_max = 24,
  37. };
  38. static const struct icst_params cclk_params = {
  39. .ref = 24000,
  40. .vco_max = 320000,
  41. .vd_min = 12,
  42. .vd_max = 160,
  43. .rd_min = 24,
  44. .rd_max = 24,
  45. };
  46. /*
  47. * Validate the speed policy.
  48. */
  49. static int integrator_verify_policy(struct cpufreq_policy *policy)
  50. {
  51. struct icst_vco vco;
  52. cpufreq_verify_within_limits(policy,
  53. policy->cpuinfo.min_freq,
  54. policy->cpuinfo.max_freq);
  55. vco = icst525_khz_to_vco(&cclk_params, policy->max);
  56. policy->max = icst525_khz(&cclk_params, vco);
  57. vco = icst525_khz_to_vco(&cclk_params, policy->min);
  58. policy->min = icst525_khz(&cclk_params, vco);
  59. cpufreq_verify_within_limits(policy,
  60. policy->cpuinfo.min_freq,
  61. policy->cpuinfo.max_freq);
  62. return 0;
  63. }
  64. static int integrator_set_target(struct cpufreq_policy *policy,
  65. unsigned int target_freq,
  66. unsigned int relation)
  67. {
  68. cpumask_t cpus_allowed;
  69. int cpu = policy->cpu;
  70. struct icst_vco vco;
  71. struct cpufreq_freqs freqs;
  72. u_int cm_osc;
  73. /*
  74. * Save this threads cpus_allowed mask.
  75. */
  76. cpus_allowed = current->cpus_allowed;
  77. /*
  78. * Bind to the specified CPU. When this call returns,
  79. * we should be running on the right CPU.
  80. */
  81. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  82. BUG_ON(cpu != smp_processor_id());
  83. /* get current setting */
  84. cm_osc = __raw_readl(CM_OSC);
  85. if (machine_is_integrator()) {
  86. vco.s = (cm_osc >> 8) & 7;
  87. } else if (machine_is_cintegrator()) {
  88. vco.s = 1;
  89. }
  90. vco.v = cm_osc & 255;
  91. vco.r = 22;
  92. freqs.old = icst525_khz(&cclk_params, vco);
  93. /* icst525_khz_to_vco rounds down -- so we need the next
  94. * larger freq in case of CPUFREQ_RELATION_L.
  95. */
  96. if (relation == CPUFREQ_RELATION_L)
  97. target_freq += 999;
  98. if (target_freq > policy->max)
  99. target_freq = policy->max;
  100. vco = icst525_khz_to_vco(&cclk_params, target_freq);
  101. freqs.new = icst525_khz(&cclk_params, vco);
  102. freqs.cpu = policy->cpu;
  103. if (freqs.old == freqs.new) {
  104. set_cpus_allowed(current, cpus_allowed);
  105. return 0;
  106. }
  107. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  108. cm_osc = __raw_readl(CM_OSC);
  109. if (machine_is_integrator()) {
  110. cm_osc &= 0xfffff800;
  111. cm_osc |= vco.s << 8;
  112. } else if (machine_is_cintegrator()) {
  113. cm_osc &= 0xffffff00;
  114. }
  115. cm_osc |= vco.v;
  116. __raw_writel(0xa05f, CM_LOCK);
  117. __raw_writel(cm_osc, CM_OSC);
  118. __raw_writel(0, CM_LOCK);
  119. /*
  120. * Restore the CPUs allowed mask.
  121. */
  122. set_cpus_allowed(current, cpus_allowed);
  123. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  124. return 0;
  125. }
  126. static unsigned int integrator_get(unsigned int cpu)
  127. {
  128. cpumask_t cpus_allowed;
  129. unsigned int current_freq;
  130. u_int cm_osc;
  131. struct icst_vco vco;
  132. cpus_allowed = current->cpus_allowed;
  133. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  134. BUG_ON(cpu != smp_processor_id());
  135. /* detect memory etc. */
  136. cm_osc = __raw_readl(CM_OSC);
  137. if (machine_is_integrator()) {
  138. vco.s = (cm_osc >> 8) & 7;
  139. } else if (machine_is_cintegrator()) {
  140. vco.s = 1;
  141. }
  142. vco.v = cm_osc & 255;
  143. vco.r = 22;
  144. current_freq = icst525_khz(&cclk_params, vco); /* current freq */
  145. set_cpus_allowed(current, cpus_allowed);
  146. return current_freq;
  147. }
  148. static int integrator_cpufreq_init(struct cpufreq_policy *policy)
  149. {
  150. /* set default policy and cpuinfo */
  151. policy->cpuinfo.max_freq = 160000;
  152. policy->cpuinfo.min_freq = 12000;
  153. policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
  154. policy->cur = policy->min = policy->max = integrator_get(policy->cpu);
  155. return 0;
  156. }
  157. static struct cpufreq_driver integrator_driver = {
  158. .verify = integrator_verify_policy,
  159. .target = integrator_set_target,
  160. .get = integrator_get,
  161. .init = integrator_cpufreq_init,
  162. .name = "integrator",
  163. };
  164. static int __init integrator_cpu_init(void)
  165. {
  166. return cpufreq_register_driver(&integrator_driver);
  167. }
  168. static void __exit integrator_cpu_exit(void)
  169. {
  170. cpufreq_unregister_driver(&integrator_driver);
  171. }
  172. MODULE_AUTHOR ("Russell M. King");
  173. MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
  174. MODULE_LICENSE ("GPL");
  175. module_init(integrator_cpu_init);
  176. module_exit(integrator_cpu_exit);