2 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
4 +/*****************************************************************************
5 +* Copyright 2011 Broadcom Corporation. All rights reserved.
7 +* Unless you and Broadcom execute a separate written software license
8 +* agreement governing use of this software, this software is licensed to you
9 +* under the terms of the GNU General Public License version 2, available at
10 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
12 +* Notwithstanding the above, under no circumstances may you combine this
13 +* software in any way with any other Broadcom software provided under a
14 +* license other than the GPL, without Broadcom's express prior written
16 +*****************************************************************************/
18 +/*****************************************************************************
19 +* FILENAME: bcm2835-cpufreq.h
20 +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
21 +* processor. Messages are sent to Videocore either setting or requesting the
22 +* frequency of the ARM in order to match an appropiate frequency to the current
23 +* usage of the processor. The policy which selects the frequency to use is
24 +* defined in the kernel .config file, but can be changed during runtime.
25 +*****************************************************************************/
27 +/* ---------- INCLUDES ---------- */
28 +#include <linux/kernel.h>
29 +#include <linux/init.h>
30 +#include <linux/module.h>
31 +#include <linux/cpufreq.h>
32 +#include <mach/vcio.h>
34 +/* ---------- DEFINES ---------- */
35 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
36 +#define MODULE_NAME "bcm2835-cpufreq"
38 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
40 +/* debug printk macros */
41 +#ifdef CPUFREQ_DEBUG_ENABLE
42 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
44 +#define print_debug(fmt,...)
46 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
47 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
49 +/* tag part of the message */
51 + uint32_t tag_id; /* the message id */
52 + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
53 + uint32_t data_size; /* amount of data being sent or received */
54 + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
55 + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
58 +/* message structure to be sent to videocore */
60 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
61 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
62 + struct vc_msg_tag tag; /* the tag structure above to make */
63 + uint32_t end_tag; /* an end identifier, should be set to NULL */
66 +/* ---------- GLOBALS ---------- */
67 +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
70 + ===============================================
71 + clk_rate either gets or sets the clock rates.
72 + ===============================================
74 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
76 + int s, actual_rate=0;
79 + /* wipe all previous message data */
80 + memset(&msg, 0, sizeof msg);
82 + msg.msg_size = sizeof msg;
84 + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
85 + msg.tag.buffer_size = 8;
86 + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
87 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
88 + msg.tag.val = arm_rate * 1000;
90 + /* send the message */
91 + s = bcm_mailbox_property(&msg, sizeof msg);
93 + /* check if it was all ok and return the rate in KHz */
94 + if (s == 0 && (msg.request_code & 0x80000000))
95 + actual_rate = msg.tag.val/1000;
97 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
101 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
107 + /* wipe all previous message data */
108 + memset(&msg, 0, sizeof msg);
110 + msg.msg_size = sizeof msg;
111 + msg.tag.tag_id = tag;
112 + msg.tag.buffer_size = 8;
113 + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
114 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
116 + /* send the message */
117 + s = bcm_mailbox_property(&msg, sizeof msg);
119 + /* check if it was all ok and return the rate in KHz */
120 + if (s == 0 && (msg.request_code & 0x80000000))
121 + arm_rate = msg.tag.val/1000;
123 + print_debug("%s frequency = %d\n",
124 + tag == VCMSG_GET_CLOCK_RATE ? "Current":
125 + tag == VCMSG_GET_MIN_CLOCK ? "Min":
126 + tag == VCMSG_GET_MAX_CLOCK ? "Max":
127 + "Unexpected", arm_rate);
133 + ====================================================
134 + Module Initialisation registers the cpufreq driver
135 + ====================================================
137 +static int __init bcm2835_cpufreq_module_init(void)
139 + print_debug("IN\n");
140 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
148 +static void __exit bcm2835_cpufreq_module_exit(void)
150 + print_debug("IN\n");
151 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
156 + ==============================================================
157 + Initialisation function sets up the CPU policy for first use
158 + ==============================================================
160 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
162 + /* measured value of how long it takes to change frequency */
163 + policy->cpuinfo.transition_latency = 355000; /* ns */
165 + /* now find out what the maximum and minimum frequencies are */
166 + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
167 + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
168 + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
170 + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
175 + =================================================================================
176 + Target function chooses the most appropriate frequency from the table to enable
177 + =================================================================================
180 +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
182 + unsigned int target = target_freq;
183 + unsigned int cur = policy->cur;
184 + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
186 + /* if we are above min and using ondemand, then just use max */
187 + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
188 + target = policy->max;
189 + /* if the frequency is the same, just quit */
190 + if (target == policy->cur)
193 + /* otherwise were good to set the clock frequency */
194 + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
198 + print_err("Error occurred setting a new frequency (%d)!\n", target);
199 + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
202 + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
206 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
208 + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
209 + print_debug("cpu=%d\n", actual_rate);
210 + return actual_rate;
214 + =================================================================================
215 + Verify ensures that when a policy is changed, it is suitable for the CPU to use
216 + =================================================================================
219 +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
221 + print_info("switching to governor %s\n", policy->governor->name);
226 +/* the CPUFreq driver */
227 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
228 + .name = "BCM2835 CPUFreq",
229 + .owner = THIS_MODULE,
230 + .init = bcm2835_cpufreq_driver_init,
231 + .verify = bcm2835_cpufreq_driver_verify,
232 + .target = bcm2835_cpufreq_driver_target,
233 + .get = bcm2835_cpufreq_driver_get
236 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
237 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
238 +MODULE_LICENSE("GPL");
240 +module_init(bcm2835_cpufreq_module_init);
241 +module_exit(bcm2835_cpufreq_module_exit);
243 --- a/drivers/cpufreq/Kconfig.arm
244 +++ b/drivers/cpufreq/Kconfig.arm
245 @@ -150,3 +150,11 @@ config ARM_SPEAR_CPUFREQ
248 This adds the CPUFreq driver support for SPEAr SOCs.
250 +config ARM_BCM2835_CPUFREQ
251 + bool "BCM2835 Driver"
254 + This adds the CPUFreq driver for BCM2835
256 + If in doubt, say N.
257 --- a/drivers/cpufreq/Makefile
258 +++ b/drivers/cpufreq/Makefile
259 @@ -72,6 +72,7 @@ obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa11
260 obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
261 obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
262 obj-$(CONFIG_ARCH_TEGRA) += tegra-cpufreq.o
263 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
265 ##################################################################################
266 # PowerPC platform drivers