0016-Add-cpufreq-driver.patch 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. From f8628f418651bcb52604f943c00c45d932ff3572 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 016/114] Add cpufreq driver
  5. ---
  6. arch/arm/Kconfig | 1 +
  7. drivers/cpufreq/Kconfig.arm | 8 ++
  8. drivers/cpufreq/Makefile | 1 +
  9. drivers/cpufreq/bcm2835-cpufreq.c | 224 ++++++++++++++++++++++++++++++++++++++
  10. 4 files changed, 234 insertions(+)
  11. create mode 100755 drivers/cpufreq/bcm2835-cpufreq.c
  12. --- a/arch/arm/Kconfig
  13. +++ b/arch/arm/Kconfig
  14. @@ -390,6 +390,7 @@ config ARCH_BCM2708
  15. select NEED_MACH_GPIO_H
  16. select NEED_MACH_MEMORY_H
  17. select CLKDEV_LOOKUP
  18. + select ARCH_HAS_CPUFREQ
  19. select GENERIC_CLOCKEVENTS
  20. select ARM_ERRATA_411920
  21. select MACH_BCM2708
  22. --- a/drivers/cpufreq/Kconfig.arm
  23. +++ b/drivers/cpufreq/Kconfig.arm
  24. @@ -241,6 +241,14 @@ config ARM_SPEAR_CPUFREQ
  25. help
  26. This adds the CPUFreq driver support for SPEAr SOCs.
  27. +config ARM_BCM2835_CPUFREQ
  28. + bool "BCM2835 Driver"
  29. + default y
  30. + help
  31. + This adds the CPUFreq driver for BCM2835
  32. +
  33. + If in doubt, say N.
  34. +
  35. config ARM_TEGRA_CPUFREQ
  36. bool "TEGRA CPUFreq support"
  37. depends on ARCH_TEGRA
  38. --- a/drivers/cpufreq/Makefile
  39. +++ b/drivers/cpufreq/Makefile
  40. @@ -75,6 +75,7 @@ obj-$(CONFIG_ARM_S5PV210_CPUFREQ) += s5p
  41. obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
  42. obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
  43. obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
  44. +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
  45. obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
  46. obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
  47. --- /dev/null
  48. +++ b/drivers/cpufreq/bcm2835-cpufreq.c
  49. @@ -0,0 +1,224 @@
  50. +/*****************************************************************************
  51. +* Copyright 2011 Broadcom Corporation. All rights reserved.
  52. +*
  53. +* Unless you and Broadcom execute a separate written software license
  54. +* agreement governing use of this software, this software is licensed to you
  55. +* under the terms of the GNU General Public License version 2, available at
  56. +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
  57. +*
  58. +* Notwithstanding the above, under no circumstances may you combine this
  59. +* software in any way with any other Broadcom software provided under a
  60. +* license other than the GPL, without Broadcom's express prior written
  61. +* consent.
  62. +*****************************************************************************/
  63. +
  64. +/*****************************************************************************
  65. +* FILENAME: bcm2835-cpufreq.h
  66. +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
  67. +* processor. Messages are sent to Videocore either setting or requesting the
  68. +* frequency of the ARM in order to match an appropiate frequency to the current
  69. +* usage of the processor. The policy which selects the frequency to use is
  70. +* defined in the kernel .config file, but can be changed during runtime.
  71. +*****************************************************************************/
  72. +
  73. +/* ---------- INCLUDES ---------- */
  74. +#include <linux/kernel.h>
  75. +#include <linux/init.h>
  76. +#include <linux/module.h>
  77. +#include <linux/cpufreq.h>
  78. +#include <mach/vcio.h>
  79. +
  80. +/* ---------- DEFINES ---------- */
  81. +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
  82. +#define MODULE_NAME "bcm2835-cpufreq"
  83. +
  84. +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
  85. +
  86. +/* debug printk macros */
  87. +#ifdef CPUFREQ_DEBUG_ENABLE
  88. +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
  89. +#else
  90. +#define print_debug(fmt,...)
  91. +#endif
  92. +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
  93. +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
  94. +
  95. +/* tag part of the message */
  96. +struct vc_msg_tag {
  97. + uint32_t tag_id; /* the message id */
  98. + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
  99. + uint32_t data_size; /* amount of data being sent or received */
  100. + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
  101. + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
  102. +};
  103. +
  104. +/* message structure to be sent to videocore */
  105. +struct vc_msg {
  106. + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
  107. + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
  108. + struct vc_msg_tag tag; /* the tag structure above to make */
  109. + uint32_t end_tag; /* an end identifier, should be set to NULL */
  110. +};
  111. +
  112. +/* ---------- GLOBALS ---------- */
  113. +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
  114. +
  115. +static struct cpufreq_frequency_table bcm2835_freq_table[] = {
  116. + {0, 0, 0},
  117. + {0, 0, 0},
  118. + {0, 0, CPUFREQ_TABLE_END},
  119. +};
  120. +
  121. +/*
  122. + ===============================================
  123. + clk_rate either gets or sets the clock rates.
  124. + ===============================================
  125. +*/
  126. +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
  127. +{
  128. + int s, actual_rate=0;
  129. + struct vc_msg msg;
  130. +
  131. + /* wipe all previous message data */
  132. + memset(&msg, 0, sizeof msg);
  133. +
  134. + msg.msg_size = sizeof msg;
  135. +
  136. + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
  137. + msg.tag.buffer_size = 8;
  138. + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
  139. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  140. + msg.tag.val = arm_rate * 1000;
  141. +
  142. + /* send the message */
  143. + s = bcm_mailbox_property(&msg, sizeof msg);
  144. +
  145. + /* check if it was all ok and return the rate in KHz */
  146. + if (s == 0 && (msg.request_code & 0x80000000))
  147. + actual_rate = msg.tag.val/1000;
  148. +
  149. + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
  150. + return actual_rate;
  151. +}
  152. +
  153. +static uint32_t bcm2835_cpufreq_get_clock(int tag)
  154. +{
  155. + int s;
  156. + int arm_rate = 0;
  157. + struct vc_msg msg;
  158. +
  159. + /* wipe all previous message data */
  160. + memset(&msg, 0, sizeof msg);
  161. +
  162. + msg.msg_size = sizeof msg;
  163. + msg.tag.tag_id = tag;
  164. + msg.tag.buffer_size = 8;
  165. + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
  166. + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
  167. +
  168. + /* send the message */
  169. + s = bcm_mailbox_property(&msg, sizeof msg);
  170. +
  171. + /* check if it was all ok and return the rate in KHz */
  172. + if (s == 0 && (msg.request_code & 0x80000000))
  173. + arm_rate = msg.tag.val/1000;
  174. +
  175. + print_debug("%s frequency = %d\n",
  176. + tag == VCMSG_GET_CLOCK_RATE ? "Current":
  177. + tag == VCMSG_GET_MIN_CLOCK ? "Min":
  178. + tag == VCMSG_GET_MAX_CLOCK ? "Max":
  179. + "Unexpected", arm_rate);
  180. +
  181. + return arm_rate;
  182. +}
  183. +
  184. +/*
  185. + ====================================================
  186. + Module Initialisation registers the cpufreq driver
  187. + ====================================================
  188. +*/
  189. +static int __init bcm2835_cpufreq_module_init(void)
  190. +{
  191. + print_debug("IN\n");
  192. + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
  193. +}
  194. +
  195. +/*
  196. + =============
  197. + Module exit
  198. + =============
  199. +*/
  200. +static void __exit bcm2835_cpufreq_module_exit(void)
  201. +{
  202. + print_debug("IN\n");
  203. + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
  204. + return;
  205. +}
  206. +
  207. +/*
  208. + ==============================================================
  209. + Initialisation function sets up the CPU policy for first use
  210. + ==============================================================
  211. +*/
  212. +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
  213. +{
  214. + /* measured value of how long it takes to change frequency */
  215. + const unsigned int transition_latency = 355000; /* ns */
  216. +
  217. + /* now find out what the maximum and minimum frequencies are */
  218. + bcm2835_freq_table[0].frequency = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
  219. + bcm2835_freq_table[1].frequency = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
  220. +
  221. + print_info("min=%d max=%d\n", bcm2835_freq_table[0].frequency, bcm2835_freq_table[1].frequency);
  222. + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
  223. +}
  224. +
  225. +/*
  226. + =====================================================================
  227. + Target index function chooses the requested frequency from the table
  228. + =====================================================================
  229. +*/
  230. +
  231. +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
  232. +{
  233. + unsigned int target_freq = bcm2835_freq_table[state].frequency;
  234. + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
  235. +
  236. + if (!cur)
  237. + {
  238. + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
  239. + return -EINVAL;
  240. + }
  241. + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
  242. + return 0;
  243. +}
  244. +
  245. +/*
  246. + ======================================================
  247. + Get function returns the current frequency from table
  248. + ======================================================
  249. +*/
  250. +
  251. +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
  252. +{
  253. + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
  254. + print_debug("%d: freq=%d\n", cpu, actual_rate);
  255. + return actual_rate <= bcm2835_freq_table[0].frequency ? bcm2835_freq_table[0].frequency : bcm2835_freq_table[1].frequency;
  256. +}
  257. +
  258. +/* the CPUFreq driver */
  259. +static struct cpufreq_driver bcm2835_cpufreq_driver = {
  260. + .name = "BCM2835 CPUFreq",
  261. + .init = bcm2835_cpufreq_driver_init,
  262. + .verify = cpufreq_generic_frequency_table_verify,
  263. + .target_index = bcm2835_cpufreq_driver_target_index,
  264. + .get = bcm2835_cpufreq_driver_get,
  265. + .attr = cpufreq_generic_attr,
  266. +};
  267. +
  268. +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
  269. +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
  270. +MODULE_LICENSE("GPL");
  271. +
  272. +module_init(bcm2835_cpufreq_module_init);
  273. +module_exit(bcm2835_cpufreq_module_exit);