cpu-omap.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * linux/arch/arm/plat-omap/cpu-omap.c
  3. *
  4. * CPU frequency scaling for OMAP
  5. *
  6. * Copyright (C) 2005 Nokia Corporation
  7. * Written by Tony Lindgren <tony@atomide.com>
  8. *
  9. * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/cpufreq.h>
  19. #include <linux/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <mach/hardware.h>
  25. #include <mach/clock.h>
  26. #include <asm/system.h>
  27. #define VERY_HI_RATE 900000000
  28. static struct cpufreq_frequency_table *freq_table;
  29. #ifdef CONFIG_ARCH_OMAP1
  30. #define MPU_CLK "mpu"
  31. #else
  32. #define MPU_CLK "virt_prcm_set"
  33. #endif
  34. static struct clk *mpu_clk;
  35. /* TODO: Add support for SDRAM timing changes */
  36. int omap_verify_speed(struct cpufreq_policy *policy)
  37. {
  38. if (freq_table)
  39. return cpufreq_frequency_table_verify(policy, freq_table);
  40. if (policy->cpu)
  41. return -EINVAL;
  42. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  43. policy->cpuinfo.max_freq);
  44. policy->min = clk_round_rate(mpu_clk, policy->min * 1000) / 1000;
  45. policy->max = clk_round_rate(mpu_clk, policy->max * 1000) / 1000;
  46. cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  47. policy->cpuinfo.max_freq);
  48. return 0;
  49. }
  50. unsigned int omap_getspeed(unsigned int cpu)
  51. {
  52. unsigned long rate;
  53. if (cpu)
  54. return 0;
  55. rate = clk_get_rate(mpu_clk) / 1000;
  56. return rate;
  57. }
  58. static int omap_target(struct cpufreq_policy *policy,
  59. unsigned int target_freq,
  60. unsigned int relation)
  61. {
  62. struct cpufreq_freqs freqs;
  63. int ret = 0;
  64. /* Ensure desired rate is within allowed range. Some govenors
  65. * (ondemand) will just pass target_freq=0 to get the minimum. */
  66. if (target_freq < policy->cpuinfo.min_freq)
  67. target_freq = policy->cpuinfo.min_freq;
  68. if (target_freq > policy->cpuinfo.max_freq)
  69. target_freq = policy->cpuinfo.max_freq;
  70. freqs.old = omap_getspeed(0);
  71. freqs.new = clk_round_rate(mpu_clk, target_freq * 1000) / 1000;
  72. freqs.cpu = 0;
  73. if (freqs.old == freqs.new)
  74. return ret;
  75. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  76. #ifdef CONFIG_CPU_FREQ_DEBUG
  77. printk(KERN_DEBUG "cpufreq-omap: transition: %u --> %u\n",
  78. freqs.old, freqs.new);
  79. #endif
  80. ret = clk_set_rate(mpu_clk, freqs.new * 1000);
  81. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  82. return ret;
  83. }
  84. static int __init omap_cpu_init(struct cpufreq_policy *policy)
  85. {
  86. int result = 0;
  87. mpu_clk = clk_get(NULL, MPU_CLK);
  88. if (IS_ERR(mpu_clk))
  89. return PTR_ERR(mpu_clk);
  90. if (policy->cpu != 0)
  91. return -EINVAL;
  92. policy->cur = policy->min = policy->max = omap_getspeed(0);
  93. clk_init_cpufreq_table(&freq_table);
  94. if (freq_table) {
  95. result = cpufreq_frequency_table_cpuinfo(policy, freq_table);
  96. if (!result)
  97. cpufreq_frequency_table_get_attr(freq_table,
  98. policy->cpu);
  99. } else {
  100. policy->cpuinfo.min_freq = clk_round_rate(mpu_clk, 0) / 1000;
  101. policy->cpuinfo.max_freq = clk_round_rate(mpu_clk,
  102. VERY_HI_RATE) / 1000;
  103. }
  104. /* FIXME: what's the actual transition time? */
  105. policy->cpuinfo.transition_latency = 10 * 1000 * 1000;
  106. return 0;
  107. }
  108. static int omap_cpu_exit(struct cpufreq_policy *policy)
  109. {
  110. clk_put(mpu_clk);
  111. return 0;
  112. }
  113. static struct freq_attr *omap_cpufreq_attr[] = {
  114. &cpufreq_freq_attr_scaling_available_freqs,
  115. NULL,
  116. };
  117. static struct cpufreq_driver omap_driver = {
  118. .flags = CPUFREQ_STICKY,
  119. .verify = omap_verify_speed,
  120. .target = omap_target,
  121. .get = omap_getspeed,
  122. .init = omap_cpu_init,
  123. .exit = omap_cpu_exit,
  124. .name = "omap",
  125. .attr = omap_cpufreq_attr,
  126. };
  127. static int __init omap_cpufreq_init(void)
  128. {
  129. return cpufreq_register_driver(&omap_driver);
  130. }
  131. arch_initcall(omap_cpufreq_init);
  132. /*
  133. * if ever we want to remove this, upon cleanup call:
  134. *
  135. * cpufreq_unregister_driver()
  136. * cpufreq_frequency_table_put_attr()
  137. */