0043-Add-cpufreq-driver.patch 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. From f2624ce13f5f6ba1121a8447a2a128215a62345a Mon Sep 17 00:00:00 2001
  2. From: popcornmix <popcornmix@gmail.com>
  3. Date: Wed, 3 Jul 2013 00:49:20 +0100
  4. Subject: [PATCH] Add cpufreq driver
  5. Signed-off-by: popcornmix <popcornmix@gmail.com>
  6. ---
  7. drivers/cpufreq/Kconfig.arm | 9 ++
  8. drivers/cpufreq/Makefile | 1 +
  9. drivers/cpufreq/bcm2835-cpufreq.c | 213 ++++++++++++++++++++++++++++++++++++++
  10. 3 files changed, 223 insertions(+)
  11. create mode 100644 drivers/cpufreq/bcm2835-cpufreq.c
  12. --- a/drivers/cpufreq/Kconfig.arm
  13. +++ b/drivers/cpufreq/Kconfig.arm
  14. @@ -217,6 +217,15 @@ config ARM_SPEAR_CPUFREQ
  15. help
  16. This adds the CPUFreq driver support for SPEAr SOCs.
  17. +config ARM_BCM2835_CPUFREQ
  18. + depends on RASPBERRYPI_FIRMWARE
  19. + bool "BCM2835 Driver"
  20. + default y
  21. + help
  22. + This adds the CPUFreq driver for BCM2835
  23. +
  24. + If in doubt, say N.
  25. +
  26. config ARM_TEGRA20_CPUFREQ
  27. bool "Tegra20 CPUFreq support"
  28. depends on ARCH_TEGRA
  29. --- a/drivers/cpufreq/Makefile
  30. +++ b/drivers/cpufreq/Makefile
  31. @@ -73,6 +73,7 @@ obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa11
  32. obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
  33. obj-$(CONFIG_ARM_SCPI_CPUFREQ) += scpi-cpufreq.o
  34. obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
  35. +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
  36. obj-$(CONFIG_ARM_TEGRA20_CPUFREQ) += tegra20-cpufreq.o
  37. obj-$(CONFIG_ARM_TEGRA124_CPUFREQ) += tegra124-cpufreq.o
  38. obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
  39. --- /dev/null
  40. +++ b/drivers/cpufreq/bcm2835-cpufreq.c
  41. @@ -0,0 +1,213 @@
  42. +/*****************************************************************************
  43. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  44. +*
  45. +* Unless you and Broadcom execute a separate written software license
  46. +* agreement governing use of this software, this software is licensed to you
  47. +* under the terms of the GNU General Public License version 2, available at
  48. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  49. +*
  50. +* Notwithstanding the above, under no circumstances may you combine this
  51. +* software in any way with any other Broadcom software provided under a
  52. +* license other than the GPL, without Broadcom's express prior written
  53. +* consent.
  54. +*****************************************************************************/
  55. +
  56. +/*****************************************************************************
  57. +* FILENAME: bcm2835-cpufreq.h
  58. +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
  59. +* processor. Messages are sent to Videocore either setting or requesting the
  60. +* frequency of the ARM in order to match an appropiate frequency to the current
  61. +* usage of the processor. The policy which selects the frequency to use is
  62. +* defined in the kernel .config file, but can be changed during runtime.
  63. +*****************************************************************************/
  64. +
  65. +/* ---------- INCLUDES ---------- */
  66. +#include <linux/kernel.h>
  67. +#include <linux/init.h>
  68. +#include <linux/module.h>
  69. +#include <linux/cpufreq.h>
  70. +#include <soc/bcm2835/raspberrypi-firmware.h>
  71. +
  72. +/* ---------- DEFINES ---------- */
  73. +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
  74. +#define MODULE_NAME "bcm2835-cpufreq"
  75. +
  76. +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
  77. +
  78. +/* debug printk macros */
  79. +#ifdef CPUFREQ_DEBUG_ENABLE
  80. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  81. +#else
  82. +#define print_debug(fmt,...)
  83. +#endif
  84. +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  85. +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
  86. +
  87. +/* ---------- GLOBALS ---------- */
  88. +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
  89. +
  90. +static struct cpufreq_frequency_table bcm2835_freq_table[] = {
  91. + {0, 0, 0},
  92. + {0, 0, 0},
  93. + {0, 0, CPUFREQ_TABLE_END},
  94. +};
  95. +
  96. +/*
  97. + ===============================================
  98. + clk_rate either gets or sets the clock rates.
  99. + ===============================================
  100. +*/
  101. +
  102. +static int bcm2835_cpufreq_clock_property(u32 tag, u32 id, u32 *val)
  103. +{
  104. + struct rpi_firmware *fw = rpi_firmware_get(NULL);
  105. + struct {
  106. + u32 id;
  107. + u32 val;
  108. + } packet;
  109. + int ret;
  110. +
  111. + packet.id = id;
  112. + packet.val = *val;
  113. + ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
  114. + if (ret)
  115. + return ret;
  116. +
  117. + *val = packet.val;
  118. +
  119. + return 0;
  120. +}
  121. +
  122. +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
  123. +{
  124. + u32 rate = arm_rate * 1000;
  125. + int ret;
  126. +
  127. + ret = bcm2835_cpufreq_clock_property(RPI_FIRMWARE_SET_CLOCK_RATE, VCMSG_ID_ARM_CLOCK, &rate);
  128. + if (ret) {
  129. + print_err("Failed to set clock: %d (%d)\n", arm_rate, ret);
  130. + return 0;
  131. + }
  132. +
  133. + rate /= 1000;
  134. + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, rate);
  135. +
  136. + return rate;
  137. +}
  138. +
  139. +static uint32_t bcm2835_cpufreq_get_clock(int tag)
  140. +{
  141. + u32 rate;
  142. + int ret;
  143. +
  144. + ret = bcm2835_cpufreq_clock_property(tag, VCMSG_ID_ARM_CLOCK, &rate);
  145. + if (ret) {
  146. + print_err("Failed to get clock (%d)\n", ret);
  147. + return 0;
  148. + }
  149. +
  150. + rate /= 1000;
  151. + print_debug("%s frequency = %u\n",
  152. + tag == RPI_FIRMWARE_GET_CLOCK_RATE ? "Current":
  153. + tag == RPI_FIRMWARE_GET_MIN_CLOCK_RATE ? "Min":
  154. + tag == RPI_FIRMWARE_GET_MAX_CLOCK_RATE ? "Max":
  155. + "Unexpected", rate);
  156. +
  157. + return rate;
  158. +}
  159. +
  160. +/*
  161. + ====================================================
  162. + Module Initialisation registers the cpufreq driver
  163. + ====================================================
  164. +*/
  165. +static int __init bcm2835_cpufreq_module_init(void)
  166. +{
  167. + print_debug("IN\n");
  168. + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
  169. +}
  170. +
  171. +/*
  172. + =============
  173. + Module exit
  174. + =============
  175. +*/
  176. +static void __exit bcm2835_cpufreq_module_exit(void)
  177. +{
  178. + print_debug("IN\n");
  179. + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
  180. + return;
  181. +}
  182. +
  183. +/*
  184. + ==============================================================
  185. + Initialisation function sets up the CPU policy for first use
  186. + ==============================================================
  187. +*/
  188. +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
  189. +{
  190. + /* measured value of how long it takes to change frequency */
  191. + const unsigned int transition_latency = 355000; /* ns */
  192. +
  193. + if (!rpi_firmware_get(NULL)) {
  194. + print_err("Firmware is not available\n");
  195. + return -ENODEV;
  196. + }
  197. +
  198. + /* now find out what the maximum and minimum frequencies are */
  199. + bcm2835_freq_table[0].frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MIN_CLOCK_RATE);
  200. + bcm2835_freq_table[1].frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MAX_CLOCK_RATE);
  201. +
  202. + print_info("min=%d max=%d\n", bcm2835_freq_table[0].frequency, bcm2835_freq_table[1].frequency);
  203. + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
  204. +}
  205. +
  206. +/*
  207. + =====================================================================
  208. + Target index function chooses the requested frequency from the table
  209. + =====================================================================
  210. +*/
  211. +
  212. +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
  213. +{
  214. + unsigned int target_freq = bcm2835_freq_table[state].frequency;
  215. + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
  216. +
  217. + if (!cur)
  218. + {
  219. + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
  220. + return -EINVAL;
  221. + }
  222. + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
  223. + return 0;
  224. +}
  225. +
  226. +/*
  227. + ======================================================
  228. + Get function returns the current frequency from table
  229. + ======================================================
  230. +*/
  231. +
  232. +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
  233. +{
  234. + unsigned int actual_rate = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_CLOCK_RATE);
  235. + print_debug("cpu%d: freq=%d\n", cpu, actual_rate);
  236. + return actual_rate <= bcm2835_freq_table[0].frequency ? bcm2835_freq_table[0].frequency : bcm2835_freq_table[1].frequency;
  237. +}
  238. +
  239. +/* the CPUFreq driver */
  240. +static struct cpufreq_driver bcm2835_cpufreq_driver = {
  241. + .name = "BCM2835 CPUFreq",
  242. + .init = bcm2835_cpufreq_driver_init,
  243. + .verify = cpufreq_generic_frequency_table_verify,
  244. + .target_index = bcm2835_cpufreq_driver_target_index,
  245. + .get = bcm2835_cpufreq_driver_get,
  246. + .attr = cpufreq_generic_attr,
  247. +};
  248. +
  249. +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
  250. +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
  251. +MODULE_LICENSE("GPL");
  252. +
  253. +module_init(bcm2835_cpufreq_module_init);
  254. +module_exit(bcm2835_cpufreq_module_exit);