integrator-cpufreq.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C) 2001-2002 Deep Blue Solutions Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * CPU support functions
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/cpufreq.h>
  14. #include <linux/sched.h>
  15. #include <linux/smp.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <asm/mach-types.h>
  22. #include <asm/hardware/icst.h>
  23. static void __iomem *cm_base;
  24. /* The cpufreq driver only use the OSC register */
  25. #define INTEGRATOR_HDR_OSC_OFFSET 0x08
  26. #define INTEGRATOR_HDR_LOCK_OFFSET 0x14
  27. static struct cpufreq_driver integrator_driver;
  28. static const struct icst_params lclk_params = {
  29. .ref = 24000000,
  30. .vco_max = ICST525_VCO_MAX_5V,
  31. .vco_min = ICST525_VCO_MIN,
  32. .vd_min = 8,
  33. .vd_max = 132,
  34. .rd_min = 24,
  35. .rd_max = 24,
  36. .s2div = icst525_s2div,
  37. .idx2s = icst525_idx2s,
  38. };
  39. static const struct icst_params cclk_params = {
  40. .ref = 24000000,
  41. .vco_max = ICST525_VCO_MAX_5V,
  42. .vco_min = ICST525_VCO_MIN,
  43. .vd_min = 12,
  44. .vd_max = 160,
  45. .rd_min = 24,
  46. .rd_max = 24,
  47. .s2div = icst525_s2div,
  48. .idx2s = icst525_idx2s,
  49. };
  50. /*
  51. * Validate the speed policy.
  52. */
  53. static int integrator_verify_policy(struct cpufreq_policy *policy)
  54. {
  55. struct icst_vco vco;
  56. cpufreq_verify_within_limits(policy,
  57. policy->cpuinfo.min_freq,
  58. policy->cpuinfo.max_freq);
  59. vco = icst_hz_to_vco(&cclk_params, policy->max * 1000);
  60. policy->max = icst_hz(&cclk_params, vco) / 1000;
  61. vco = icst_hz_to_vco(&cclk_params, policy->min * 1000);
  62. policy->min = icst_hz(&cclk_params, vco) / 1000;
  63. cpufreq_verify_within_limits(policy,
  64. policy->cpuinfo.min_freq,
  65. policy->cpuinfo.max_freq);
  66. return 0;
  67. }
  68. static int integrator_set_target(struct cpufreq_policy *policy,
  69. unsigned int target_freq,
  70. unsigned int relation)
  71. {
  72. cpumask_t cpus_allowed;
  73. int cpu = policy->cpu;
  74. struct icst_vco vco;
  75. struct cpufreq_freqs freqs;
  76. u_int cm_osc;
  77. /*
  78. * Save this threads cpus_allowed mask.
  79. */
  80. cpus_allowed = current->cpus_allowed;
  81. /*
  82. * Bind to the specified CPU. When this call returns,
  83. * we should be running on the right CPU.
  84. */
  85. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  86. BUG_ON(cpu != smp_processor_id());
  87. /* get current setting */
  88. cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
  89. if (machine_is_integrator()) {
  90. vco.s = (cm_osc >> 8) & 7;
  91. } else if (machine_is_cintegrator()) {
  92. vco.s = 1;
  93. }
  94. vco.v = cm_osc & 255;
  95. vco.r = 22;
  96. freqs.old = icst_hz(&cclk_params, vco) / 1000;
  97. /* icst_hz_to_vco rounds down -- so we need the next
  98. * larger freq in case of CPUFREQ_RELATION_L.
  99. */
  100. if (relation == CPUFREQ_RELATION_L)
  101. target_freq += 999;
  102. if (target_freq > policy->max)
  103. target_freq = policy->max;
  104. vco = icst_hz_to_vco(&cclk_params, target_freq * 1000);
  105. freqs.new = icst_hz(&cclk_params, vco) / 1000;
  106. if (freqs.old == freqs.new) {
  107. set_cpus_allowed(current, cpus_allowed);
  108. return 0;
  109. }
  110. cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
  111. cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
  112. if (machine_is_integrator()) {
  113. cm_osc &= 0xfffff800;
  114. cm_osc |= vco.s << 8;
  115. } else if (machine_is_cintegrator()) {
  116. cm_osc &= 0xffffff00;
  117. }
  118. cm_osc |= vco.v;
  119. __raw_writel(0xa05f, cm_base + INTEGRATOR_HDR_LOCK_OFFSET);
  120. __raw_writel(cm_osc, cm_base + INTEGRATOR_HDR_OSC_OFFSET);
  121. __raw_writel(0, cm_base + INTEGRATOR_HDR_LOCK_OFFSET);
  122. /*
  123. * Restore the CPUs allowed mask.
  124. */
  125. set_cpus_allowed(current, cpus_allowed);
  126. cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
  127. return 0;
  128. }
  129. static unsigned int integrator_get(unsigned int cpu)
  130. {
  131. cpumask_t cpus_allowed;
  132. unsigned int current_freq;
  133. u_int cm_osc;
  134. struct icst_vco vco;
  135. cpus_allowed = current->cpus_allowed;
  136. set_cpus_allowed(current, cpumask_of_cpu(cpu));
  137. BUG_ON(cpu != smp_processor_id());
  138. /* detect memory etc. */
  139. cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
  140. if (machine_is_integrator()) {
  141. vco.s = (cm_osc >> 8) & 7;
  142. } else {
  143. vco.s = 1;
  144. }
  145. vco.v = cm_osc & 255;
  146. vco.r = 22;
  147. current_freq = icst_hz(&cclk_params, vco) / 1000; /* current freq */
  148. set_cpus_allowed(current, cpus_allowed);
  149. return current_freq;
  150. }
  151. static int integrator_cpufreq_init(struct cpufreq_policy *policy)
  152. {
  153. /* set default policy and cpuinfo */
  154. policy->cpuinfo.max_freq = 160000;
  155. policy->cpuinfo.min_freq = 12000;
  156. policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
  157. policy->cur = policy->min = policy->max = integrator_get(policy->cpu);
  158. return 0;
  159. }
  160. static struct cpufreq_driver integrator_driver = {
  161. .verify = integrator_verify_policy,
  162. .target = integrator_set_target,
  163. .get = integrator_get,
  164. .init = integrator_cpufreq_init,
  165. .name = "integrator",
  166. };
  167. static int __init integrator_cpufreq_probe(struct platform_device *pdev)
  168. {
  169. struct resource *res;
  170. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  171. if (!res)
  172. return -ENODEV;
  173. cm_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  174. if (!cm_base)
  175. return -ENODEV;
  176. return cpufreq_register_driver(&integrator_driver);
  177. }
  178. static void __exit integrator_cpufreq_remove(struct platform_device *pdev)
  179. {
  180. cpufreq_unregister_driver(&integrator_driver);
  181. }
  182. static const struct of_device_id integrator_cpufreq_match[] = {
  183. { .compatible = "arm,core-module-integrator"},
  184. { },
  185. };
  186. static struct platform_driver integrator_cpufreq_driver = {
  187. .driver = {
  188. .name = "integrator-cpufreq",
  189. .owner = THIS_MODULE,
  190. .of_match_table = integrator_cpufreq_match,
  191. },
  192. .remove = __exit_p(integrator_cpufreq_remove),
  193. };
  194. module_platform_driver_probe(integrator_cpufreq_driver,
  195. integrator_cpufreq_probe);
  196. MODULE_AUTHOR ("Russell M. King");
  197. MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
  198. MODULE_LICENSE ("GPL");