integrator-cpufreq.c 5.7 KB

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