1 From 4882beaf73302c162f08620d770f020211bf0acb 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 14/54] Add cpufreq driver
8 drivers/cpufreq/Kconfig.arm | 8 ++
9 drivers/cpufreq/Makefile | 1 +
10 drivers/cpufreq/bcm2835-cpufreq.c | 239 ++++++++++++++++++++++++++++++++++++++
11 4 files changed, 249 insertions(+)
12 create mode 100755 drivers/cpufreq/bcm2835-cpufreq.c
14 diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
15 index 9e23417..be8a752 100644
16 --- a/arch/arm/Kconfig
17 +++ b/arch/arm/Kconfig
18 @@ -389,6 +389,7 @@ config ARCH_BCM2708
19 select NEED_MACH_GPIO_H
20 select NEED_MACH_MEMORY_H
22 + select ARCH_HAS_CPUFREQ
23 select GENERIC_CLOCKEVENTS
24 select ARM_ERRATA_411920
26 diff --git a/drivers/cpufreq/Kconfig.arm b/drivers/cpufreq/Kconfig.arm
27 index 3129749..d12c80d 100644
28 --- a/drivers/cpufreq/Kconfig.arm
29 +++ b/drivers/cpufreq/Kconfig.arm
30 @@ -235,6 +235,14 @@ config ARM_SPEAR_CPUFREQ
32 This adds the CPUFreq driver support for SPEAr SOCs.
34 +config ARM_BCM2835_CPUFREQ
35 + bool "BCM2835 Driver"
38 + This adds the CPUFreq driver for BCM2835
42 config ARM_TEGRA_CPUFREQ
43 bool "TEGRA CPUFreq support"
45 diff --git a/drivers/cpufreq/Makefile b/drivers/cpufreq/Makefile
46 index dac58f6..d8ded24 100644
47 --- a/drivers/cpufreq/Makefile
48 +++ b/drivers/cpufreq/Makefile
49 @@ -73,6 +73,7 @@ obj-$(CONFIG_ARM_S5PV210_CPUFREQ) += s5pv210-cpufreq.o
50 obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
51 obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
52 obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
53 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
54 obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
55 obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
57 diff --git a/drivers/cpufreq/bcm2835-cpufreq.c b/drivers/cpufreq/bcm2835-cpufreq.c
59 index 0000000..7bc55bd
61 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
63 +/*****************************************************************************
64 +* Copyright 2011 Broadcom Corporation. All rights reserved.
66 +* Unless you and Broadcom execute a separate written software license
67 +* agreement governing use of this software, this software is licensed to you
68 +* under the terms of the GNU General Public License version 2, available at
69 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
71 +* Notwithstanding the above, under no circumstances may you combine this
72 +* software in any way with any other Broadcom software provided under a
73 +* license other than the GPL, without Broadcom's express prior written
75 +*****************************************************************************/
77 +/*****************************************************************************
78 +* FILENAME: bcm2835-cpufreq.h
79 +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
80 +* processor. Messages are sent to Videocore either setting or requesting the
81 +* frequency of the ARM in order to match an appropiate frequency to the current
82 +* usage of the processor. The policy which selects the frequency to use is
83 +* defined in the kernel .config file, but can be changed during runtime.
84 +*****************************************************************************/
86 +/* ---------- INCLUDES ---------- */
87 +#include <linux/kernel.h>
88 +#include <linux/init.h>
89 +#include <linux/module.h>
90 +#include <linux/cpufreq.h>
91 +#include <mach/vcio.h>
93 +/* ---------- DEFINES ---------- */
94 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
95 +#define MODULE_NAME "bcm2835-cpufreq"
97 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
99 +/* debug printk macros */
100 +#ifdef CPUFREQ_DEBUG_ENABLE
101 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
103 +#define print_debug(fmt,...)
105 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
106 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
108 +/* tag part of the message */
110 + uint32_t tag_id; /* the message id */
111 + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
112 + uint32_t data_size; /* amount of data being sent or received */
113 + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
114 + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
117 +/* message structure to be sent to videocore */
119 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
120 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
121 + struct vc_msg_tag tag; /* the tag structure above to make */
122 + uint32_t end_tag; /* an end identifier, should be set to NULL */
125 +/* ---------- GLOBALS ---------- */
126 +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
129 + ===============================================
130 + clk_rate either gets or sets the clock rates.
131 + ===============================================
133 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
135 + int s, actual_rate=0;
138 + /* wipe all previous message data */
139 + memset(&msg, 0, sizeof msg);
141 + msg.msg_size = sizeof msg;
143 + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
144 + msg.tag.buffer_size = 8;
145 + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
146 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
147 + msg.tag.val = arm_rate * 1000;
149 + /* send the message */
150 + s = bcm_mailbox_property(&msg, sizeof msg);
152 + /* check if it was all ok and return the rate in KHz */
153 + if (s == 0 && (msg.request_code & 0x80000000))
154 + actual_rate = msg.tag.val/1000;
156 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
157 + return actual_rate;
160 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
166 + /* wipe all previous message data */
167 + memset(&msg, 0, sizeof msg);
169 + msg.msg_size = sizeof msg;
170 + msg.tag.tag_id = tag;
171 + msg.tag.buffer_size = 8;
172 + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
173 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
175 + /* send the message */
176 + s = bcm_mailbox_property(&msg, sizeof msg);
178 + /* check if it was all ok and return the rate in KHz */
179 + if (s == 0 && (msg.request_code & 0x80000000))
180 + arm_rate = msg.tag.val/1000;
182 + print_debug("%s frequency = %d\n",
183 + tag == VCMSG_GET_CLOCK_RATE ? "Current":
184 + tag == VCMSG_GET_MIN_CLOCK ? "Min":
185 + tag == VCMSG_GET_MAX_CLOCK ? "Max":
186 + "Unexpected", arm_rate);
192 + ====================================================
193 + Module Initialisation registers the cpufreq driver
194 + ====================================================
196 +static int __init bcm2835_cpufreq_module_init(void)
198 + print_debug("IN\n");
199 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
207 +static void __exit bcm2835_cpufreq_module_exit(void)
209 + print_debug("IN\n");
210 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
215 + ==============================================================
216 + Initialisation function sets up the CPU policy for first use
217 + ==============================================================
219 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
221 + /* measured value of how long it takes to change frequency */
222 + policy->cpuinfo.transition_latency = 355000; /* ns */
224 + /* now find out what the maximum and minimum frequencies are */
225 + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
226 + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
227 + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
229 + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
234 + =================================================================================
235 + Target function chooses the most appropriate frequency from the table to enable
236 + =================================================================================
239 +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
241 + unsigned int target = target_freq;
242 +#ifdef CPUFREQ_DEBUG_ENABLE
243 + unsigned int cur = policy->cur;
245 + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
247 + /* if we are above min and using ondemand, then just use max */
248 + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
249 + target = policy->max;
250 + /* if the frequency is the same, just quit */
251 + if (target == policy->cur)
254 + /* otherwise were good to set the clock frequency */
255 + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
259 + print_err("Error occurred setting a new frequency (%d)!\n", target);
260 + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
263 + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
267 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
269 + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
270 + print_debug("cpu=%d\n", actual_rate);
271 + return actual_rate;
275 + =================================================================================
276 + Verify ensures that when a policy is changed, it is suitable for the CPU to use
277 + =================================================================================
280 +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
282 + print_info("switching to governor %s\n", policy->governor->name);
287 +/* the CPUFreq driver */
288 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
289 + .name = "BCM2835 CPUFreq",
290 + .init = bcm2835_cpufreq_driver_init,
291 + .verify = bcm2835_cpufreq_driver_verify,
292 + .target = bcm2835_cpufreq_driver_target,
293 + .get = bcm2835_cpufreq_driver_get
296 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
297 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
298 +MODULE_LICENSE("GPL");
300 +module_init(bcm2835_cpufreq_module_init);
301 +module_exit(bcm2835_cpufreq_module_exit);