1 diff -urN linux-3.10/drivers/cpufreq/bcm2835-cpufreq.c linux-rpi-3.10.y/drivers/cpufreq/bcm2835-cpufreq.c
2 --- linux-3.10/drivers/cpufreq/bcm2835-cpufreq.c 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-rpi-3.10.y/drivers/cpufreq/bcm2835-cpufreq.c 2013-07-06 15:25:50.000000000 +0100
5 +/*****************************************************************************
6 +* Copyright 2011 Broadcom Corporation. All rights reserved.
8 +* Unless you and Broadcom execute a separate written software license
9 +* agreement governing use of this software, this software is licensed to you
10 +* under the terms of the GNU General Public License version 2, available at
11 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
13 +* Notwithstanding the above, under no circumstances may you combine this
14 +* software in any way with any other Broadcom software provided under a
15 +* license other than the GPL, without Broadcom's express prior written
17 +*****************************************************************************/
19 +/*****************************************************************************
20 +* FILENAME: bcm2835-cpufreq.h
21 +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
22 +* processor. Messages are sent to Videocore either setting or requesting the
23 +* frequency of the ARM in order to match an appropiate frequency to the current
24 +* usage of the processor. The policy which selects the frequency to use is
25 +* defined in the kernel .config file, but can be changed during runtime.
26 +*****************************************************************************/
28 +/* ---------- INCLUDES ---------- */
29 +#include <linux/kernel.h>
30 +#include <linux/init.h>
31 +#include <linux/module.h>
32 +#include <linux/cpufreq.h>
33 +#include <mach/vcio.h>
35 +/* ---------- DEFINES ---------- */
36 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
37 +#define MODULE_NAME "bcm2835-cpufreq"
39 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
41 +/* debug printk macros */
42 +#ifdef CPUFREQ_DEBUG_ENABLE
43 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
45 +#define print_debug(fmt,...)
47 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
48 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
50 +/* tag part of the message */
52 + uint32_t tag_id; /* the message id */
53 + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
54 + uint32_t data_size; /* amount of data being sent or received */
55 + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
56 + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
59 +/* message structure to be sent to videocore */
61 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
62 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
63 + struct vc_msg_tag tag; /* the tag structure above to make */
64 + uint32_t end_tag; /* an end identifier, should be set to NULL */
67 +/* ---------- GLOBALS ---------- */
68 +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
71 + ===============================================
72 + clk_rate either gets or sets the clock rates.
73 + ===============================================
75 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
77 + int s, actual_rate=0;
80 + /* wipe all previous message data */
81 + memset(&msg, 0, sizeof msg);
83 + msg.msg_size = sizeof msg;
85 + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
86 + msg.tag.buffer_size = 8;
87 + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
88 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
89 + msg.tag.val = arm_rate * 1000;
91 + /* send the message */
92 + s = bcm_mailbox_property(&msg, sizeof msg);
94 + /* check if it was all ok and return the rate in KHz */
95 + if (s == 0 && (msg.request_code & 0x80000000))
96 + actual_rate = msg.tag.val/1000;
98 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
102 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
108 + /* wipe all previous message data */
109 + memset(&msg, 0, sizeof msg);
111 + msg.msg_size = sizeof msg;
112 + msg.tag.tag_id = tag;
113 + msg.tag.buffer_size = 8;
114 + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
115 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
117 + /* send the message */
118 + s = bcm_mailbox_property(&msg, sizeof msg);
120 + /* check if it was all ok and return the rate in KHz */
121 + if (s == 0 && (msg.request_code & 0x80000000))
122 + arm_rate = msg.tag.val/1000;
124 + print_debug("%s frequency = %d\n",
125 + tag == VCMSG_GET_CLOCK_RATE ? "Current":
126 + tag == VCMSG_GET_MIN_CLOCK ? "Min":
127 + tag == VCMSG_GET_MAX_CLOCK ? "Max":
128 + "Unexpected", arm_rate);
134 + ====================================================
135 + Module Initialisation registers the cpufreq driver
136 + ====================================================
138 +static int __init bcm2835_cpufreq_module_init(void)
140 + print_debug("IN\n");
141 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
149 +static void __exit bcm2835_cpufreq_module_exit(void)
151 + print_debug("IN\n");
152 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
157 + ==============================================================
158 + Initialisation function sets up the CPU policy for first use
159 + ==============================================================
161 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
163 + /* measured value of how long it takes to change frequency */
164 + policy->cpuinfo.transition_latency = 355000; /* ns */
166 + /* now find out what the maximum and minimum frequencies are */
167 + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
168 + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
169 + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
171 + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
176 + =================================================================================
177 + Target function chooses the most appropriate frequency from the table to enable
178 + =================================================================================
181 +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
183 + unsigned int target = target_freq;
184 + unsigned int cur = policy->cur;
185 + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
187 + /* if we are above min and using ondemand, then just use max */
188 + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
189 + target = policy->max;
190 + /* if the frequency is the same, just quit */
191 + if (target == policy->cur)
194 + /* otherwise were good to set the clock frequency */
195 + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
199 + print_err("Error occurred setting a new frequency (%d)!\n", target);
200 + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
203 + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
207 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
209 + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
210 + print_debug("cpu=%d\n", actual_rate);
211 + return actual_rate;
215 + =================================================================================
216 + Verify ensures that when a policy is changed, it is suitable for the CPU to use
217 + =================================================================================
220 +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
222 + print_info("switching to governor %s\n", policy->governor->name);
227 +/* the CPUFreq driver */
228 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
229 + .name = "BCM2835 CPUFreq",
230 + .owner = THIS_MODULE,
231 + .init = bcm2835_cpufreq_driver_init,
232 + .verify = bcm2835_cpufreq_driver_verify,
233 + .target = bcm2835_cpufreq_driver_target,
234 + .get = bcm2835_cpufreq_driver_get
237 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
238 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
239 +MODULE_LICENSE("GPL");
241 +module_init(bcm2835_cpufreq_module_init);
242 +module_exit(bcm2835_cpufreq_module_exit);
244 diff -urN linux-3.10/drivers/cpufreq/Kconfig.arm linux-rpi-3.10.y/drivers/cpufreq/Kconfig.arm
245 --- linux-3.10/drivers/cpufreq/Kconfig.arm 2013-06-30 23:13:29.000000000 +0100
246 +++ linux-rpi-3.10.y/drivers/cpufreq/Kconfig.arm 2013-07-06 15:25:50.000000000 +0100
250 This adds the CPUFreq driver support for SPEAr SOCs.
252 +config ARM_BCM2835_CPUFREQ
253 + bool "BCM2835 Driver"
256 + This adds the CPUFreq driver for BCM2835
258 + If in doubt, say N.
259 diff -urN linux-3.10/drivers/cpufreq/Makefile linux-rpi-3.10.y/drivers/cpufreq/Makefile
260 --- linux-3.10/drivers/cpufreq/Makefile 2013-06-30 23:13:29.000000000 +0100
261 +++ linux-rpi-3.10.y/drivers/cpufreq/Makefile 2013-07-06 15:25:50.000000000 +0100
263 obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
264 obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
265 obj-$(CONFIG_ARCH_TEGRA) += tegra-cpufreq.o
266 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
268 ##################################################################################
269 # PowerPC platform drivers