integrator-cpufreq.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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_cpu_limits(policy);
  57. vco = icst_hz_to_vco(&cclk_params, policy->max * 1000);
  58. policy->max = icst_hz(&cclk_params, vco) / 1000;
  59. vco = icst_hz_to_vco(&cclk_params, policy->min * 1000);
  60. policy->min = icst_hz(&cclk_params, vco) / 1000;
  61. cpufreq_verify_within_cpu_limits(policy);
  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_ptr(current, cpumask_of(cpu));
  82. BUG_ON(cpu != smp_processor_id());
  83. /* get current setting */
  84. cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
  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 = icst_hz(&cclk_params, vco) / 1000;
  93. /* icst_hz_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 = icst_hz_to_vco(&cclk_params, target_freq * 1000);
  101. freqs.new = icst_hz(&cclk_params, vco) / 1000;
  102. if (freqs.old == freqs.new) {
  103. set_cpus_allowed_ptr(current, &cpus_allowed);
  104. return 0;
  105. }
  106. cpufreq_freq_transition_begin(policy, &freqs);
  107. cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
  108. if (machine_is_integrator()) {
  109. cm_osc &= 0xfffff800;
  110. cm_osc |= vco.s << 8;
  111. } else if (machine_is_cintegrator()) {
  112. cm_osc &= 0xffffff00;
  113. }
  114. cm_osc |= vco.v;
  115. __raw_writel(0xa05f, cm_base + INTEGRATOR_HDR_LOCK_OFFSET);
  116. __raw_writel(cm_osc, cm_base + INTEGRATOR_HDR_OSC_OFFSET);
  117. __raw_writel(0, cm_base + INTEGRATOR_HDR_LOCK_OFFSET);
  118. /*
  119. * Restore the CPUs allowed mask.
  120. */
  121. set_cpus_allowed_ptr(current, &cpus_allowed);
  122. cpufreq_freq_transition_end(policy, &freqs, 0);
  123. return 0;
  124. }
  125. static unsigned int integrator_get(unsigned int cpu)
  126. {
  127. cpumask_t cpus_allowed;
  128. unsigned int current_freq;
  129. u_int cm_osc;
  130. struct icst_vco vco;
  131. cpus_allowed = current->cpus_allowed;
  132. set_cpus_allowed_ptr(current, cpumask_of(cpu));
  133. BUG_ON(cpu != smp_processor_id());
  134. /* detect memory etc. */
  135. cm_osc = __raw_readl(cm_base + INTEGRATOR_HDR_OSC_OFFSET);
  136. if (machine_is_integrator()) {
  137. vco.s = (cm_osc >> 8) & 7;
  138. } else {
  139. vco.s = 1;
  140. }
  141. vco.v = cm_osc & 255;
  142. vco.r = 22;
  143. current_freq = icst_hz(&cclk_params, vco) / 1000; /* current freq */
  144. set_cpus_allowed_ptr(current, &cpus_allowed);
  145. return current_freq;
  146. }
  147. static int integrator_cpufreq_init(struct cpufreq_policy *policy)
  148. {
  149. /* set default policy and cpuinfo */
  150. policy->max = policy->cpuinfo.max_freq = 160000;
  151. policy->min = policy->cpuinfo.min_freq = 12000;
  152. policy->cpuinfo.transition_latency = 1000000; /* 1 ms, assumed */
  153. return 0;
  154. }
  155. static struct cpufreq_driver integrator_driver = {
  156. .flags = CPUFREQ_NEED_INITIAL_FREQ_CHECK,
  157. .verify = integrator_verify_policy,
  158. .target = integrator_set_target,
  159. .get = integrator_get,
  160. .init = integrator_cpufreq_init,
  161. .name = "integrator",
  162. };
  163. static int __init integrator_cpufreq_probe(struct platform_device *pdev)
  164. {
  165. struct resource *res;
  166. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  167. if (!res)
  168. return -ENODEV;
  169. cm_base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  170. if (!cm_base)
  171. return -ENODEV;
  172. return cpufreq_register_driver(&integrator_driver);
  173. }
  174. static int __exit integrator_cpufreq_remove(struct platform_device *pdev)
  175. {
  176. return cpufreq_unregister_driver(&integrator_driver);
  177. }
  178. static const struct of_device_id integrator_cpufreq_match[] = {
  179. { .compatible = "arm,core-module-integrator"},
  180. { },
  181. };
  182. static struct platform_driver integrator_cpufreq_driver = {
  183. .driver = {
  184. .name = "integrator-cpufreq",
  185. .of_match_table = integrator_cpufreq_match,
  186. },
  187. .remove = __exit_p(integrator_cpufreq_remove),
  188. };
  189. module_platform_driver_probe(integrator_cpufreq_driver,
  190. integrator_cpufreq_probe);
  191. MODULE_AUTHOR ("Russell M. King");
  192. MODULE_DESCRIPTION ("cpufreq driver for ARM Integrator CPUs");
  193. MODULE_LICENSE ("GPL");