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 --- a/arch/arm/Kconfig
15 +++ b/arch/arm/Kconfig
16 @@ -389,6 +389,7 @@ config ARCH_BCM2708
17 select NEED_MACH_GPIO_H
18 select NEED_MACH_MEMORY_H
20 + select ARCH_HAS_CPUFREQ
21 select GENERIC_CLOCKEVENTS
22 select ARM_ERRATA_411920
24 --- a/drivers/cpufreq/Kconfig.arm
25 +++ b/drivers/cpufreq/Kconfig.arm
26 @@ -235,6 +235,14 @@ config ARM_SPEAR_CPUFREQ
28 This adds the CPUFreq driver support for SPEAr SOCs.
30 +config ARM_BCM2835_CPUFREQ
31 + bool "BCM2835 Driver"
34 + This adds the CPUFreq driver for BCM2835
38 config ARM_TEGRA_CPUFREQ
39 bool "TEGRA CPUFreq support"
41 --- a/drivers/cpufreq/Makefile
42 +++ b/drivers/cpufreq/Makefile
43 @@ -73,6 +73,7 @@ obj-$(CONFIG_ARM_S5PV210_CPUFREQ) += s5p
44 obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
45 obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
46 obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
47 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
48 obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
49 obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
52 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
54 +/*****************************************************************************
55 +* Copyright 2011 Broadcom Corporation. All rights reserved.
57 +* Unless you and Broadcom execute a separate written software license
58 +* agreement governing use of this software, this software is licensed to you
59 +* under the terms of the GNU General Public License version 2, available at
60 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
62 +* Notwithstanding the above, under no circumstances may you combine this
63 +* software in any way with any other Broadcom software provided under a
64 +* license other than the GPL, without Broadcom's express prior written
66 +*****************************************************************************/
68 +/*****************************************************************************
69 +* FILENAME: bcm2835-cpufreq.h
70 +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
71 +* processor. Messages are sent to Videocore either setting or requesting the
72 +* frequency of the ARM in order to match an appropiate frequency to the current
73 +* usage of the processor. The policy which selects the frequency to use is
74 +* defined in the kernel .config file, but can be changed during runtime.
75 +*****************************************************************************/
77 +/* ---------- INCLUDES ---------- */
78 +#include <linux/kernel.h>
79 +#include <linux/init.h>
80 +#include <linux/module.h>
81 +#include <linux/cpufreq.h>
82 +#include <mach/vcio.h>
84 +/* ---------- DEFINES ---------- */
85 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
86 +#define MODULE_NAME "bcm2835-cpufreq"
88 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
90 +/* debug printk macros */
91 +#ifdef CPUFREQ_DEBUG_ENABLE
92 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
94 +#define print_debug(fmt,...)
96 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
97 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
99 +/* tag part of the message */
101 + uint32_t tag_id; /* the message id */
102 + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
103 + uint32_t data_size; /* amount of data being sent or received */
104 + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
105 + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
108 +/* message structure to be sent to videocore */
110 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
111 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
112 + struct vc_msg_tag tag; /* the tag structure above to make */
113 + uint32_t end_tag; /* an end identifier, should be set to NULL */
116 +/* ---------- GLOBALS ---------- */
117 +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
120 + ===============================================
121 + clk_rate either gets or sets the clock rates.
122 + ===============================================
124 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
126 + int s, actual_rate=0;
129 + /* wipe all previous message data */
130 + memset(&msg, 0, sizeof msg);
132 + msg.msg_size = sizeof msg;
134 + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
135 + msg.tag.buffer_size = 8;
136 + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
137 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
138 + msg.tag.val = arm_rate * 1000;
140 + /* send the message */
141 + s = bcm_mailbox_property(&msg, sizeof msg);
143 + /* check if it was all ok and return the rate in KHz */
144 + if (s == 0 && (msg.request_code & 0x80000000))
145 + actual_rate = msg.tag.val/1000;
147 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
148 + return actual_rate;
151 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
157 + /* wipe all previous message data */
158 + memset(&msg, 0, sizeof msg);
160 + msg.msg_size = sizeof msg;
161 + msg.tag.tag_id = tag;
162 + msg.tag.buffer_size = 8;
163 + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
164 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
166 + /* send the message */
167 + s = bcm_mailbox_property(&msg, sizeof msg);
169 + /* check if it was all ok and return the rate in KHz */
170 + if (s == 0 && (msg.request_code & 0x80000000))
171 + arm_rate = msg.tag.val/1000;
173 + print_debug("%s frequency = %d\n",
174 + tag == VCMSG_GET_CLOCK_RATE ? "Current":
175 + tag == VCMSG_GET_MIN_CLOCK ? "Min":
176 + tag == VCMSG_GET_MAX_CLOCK ? "Max":
177 + "Unexpected", arm_rate);
183 + ====================================================
184 + Module Initialisation registers the cpufreq driver
185 + ====================================================
187 +static int __init bcm2835_cpufreq_module_init(void)
189 + print_debug("IN\n");
190 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
198 +static void __exit bcm2835_cpufreq_module_exit(void)
200 + print_debug("IN\n");
201 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
206 + ==============================================================
207 + Initialisation function sets up the CPU policy for first use
208 + ==============================================================
210 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
212 + /* measured value of how long it takes to change frequency */
213 + policy->cpuinfo.transition_latency = 355000; /* ns */
215 + /* now find out what the maximum and minimum frequencies are */
216 + policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
217 + policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
218 + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
220 + print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
225 + =================================================================================
226 + Target function chooses the most appropriate frequency from the table to enable
227 + =================================================================================
230 +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
232 + unsigned int target = target_freq;
233 +#ifdef CPUFREQ_DEBUG_ENABLE
234 + unsigned int cur = policy->cur;
236 + print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
238 + /* if we are above min and using ondemand, then just use max */
239 + if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
240 + target = policy->max;
241 + /* if the frequency is the same, just quit */
242 + if (target == policy->cur)
245 + /* otherwise were good to set the clock frequency */
246 + policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
250 + print_err("Error occurred setting a new frequency (%d)!\n", target);
251 + policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
254 + print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
258 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
260 + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
261 + print_debug("cpu=%d\n", actual_rate);
262 + return actual_rate;
266 + =================================================================================
267 + Verify ensures that when a policy is changed, it is suitable for the CPU to use
268 + =================================================================================
271 +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
273 + print_info("switching to governor %s\n", policy->governor->name);
278 +/* the CPUFreq driver */
279 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
280 + .name = "BCM2835 CPUFreq",
281 + .init = bcm2835_cpufreq_driver_init,
282 + .verify = bcm2835_cpufreq_driver_verify,
283 + .target = bcm2835_cpufreq_driver_target,
284 + .get = bcm2835_cpufreq_driver_get
287 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
288 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
289 +MODULE_LICENSE("GPL");
291 +module_init(bcm2835_cpufreq_module_init);
292 +module_exit(bcm2835_cpufreq_module_exit);