1 From 7d4719c360b755637c2e68f9855be282cd0065b6 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] Add cpufreq driver
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 drivers/cpufreq/Kconfig.arm | 9 ++
9 drivers/cpufreq/Makefile | 1 +
10 drivers/cpufreq/bcm2835-cpufreq.c | 218 ++++++++++++++++++++++++++++++++++++++
11 3 files changed, 228 insertions(+)
12 create mode 100644 drivers/cpufreq/bcm2835-cpufreq.c
14 --- a/drivers/cpufreq/Kconfig.arm
15 +++ b/drivers/cpufreq/Kconfig.arm
16 @@ -220,6 +220,15 @@ config ARM_STI_CPUFREQ
17 this config option if you wish to add CPUFreq support for STi based
20 +config ARM_BCM2835_CPUFREQ
21 + depends on RASPBERRYPI_FIRMWARE
22 + bool "BCM2835 Driver"
25 + This adds the CPUFreq driver for BCM2835
29 config ARM_TEGRA20_CPUFREQ
30 bool "Tegra20 CPUFreq support"
32 --- a/drivers/cpufreq/Makefile
33 +++ b/drivers/cpufreq/Makefile
34 @@ -75,6 +75,7 @@ obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa11
35 obj-$(CONFIG_ARM_SCPI_CPUFREQ) += scpi-cpufreq.o
36 obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
37 obj-$(CONFIG_ARM_STI_CPUFREQ) += sti-cpufreq.o
38 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
39 obj-$(CONFIG_ARM_TEGRA20_CPUFREQ) += tegra20-cpufreq.o
40 obj-$(CONFIG_ARM_TEGRA124_CPUFREQ) += tegra124-cpufreq.o
41 obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
43 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
45 +/*****************************************************************************
46 +* Copyright 2011 Broadcom Corporation. All rights reserved.
48 +* Unless you and Broadcom execute a separate written software license
49 +* agreement governing use of this software, this software is licensed to you
50 +* under the terms of the GNU General Public License version 2, available at
51 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
53 +* Notwithstanding the above, under no circumstances may you combine this
54 +* software in any way with any other Broadcom software provided under a
55 +* license other than the GPL, without Broadcom's express prior written
57 +*****************************************************************************/
59 +/*****************************************************************************
60 +* FILENAME: bcm2835-cpufreq.h
61 +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
62 +* processor. Messages are sent to Videocore either setting or requesting the
63 +* frequency of the ARM in order to match an appropiate frequency to the current
64 +* usage of the processor. The policy which selects the frequency to use is
65 +* defined in the kernel .config file, but can be changed during runtime.
66 +*****************************************************************************/
68 +/* ---------- INCLUDES ---------- */
69 +#include <linux/kernel.h>
70 +#include <linux/init.h>
71 +#include <linux/module.h>
72 +#include <linux/cpufreq.h>
73 +#include <soc/bcm2835/raspberrypi-firmware.h>
75 +/* ---------- DEFINES ---------- */
76 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
77 +#define MODULE_NAME "bcm2835-cpufreq"
79 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
81 +/* debug printk macros */
82 +#ifdef CPUFREQ_DEBUG_ENABLE
83 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
85 +#define print_debug(fmt,...)
87 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
88 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
90 +/* ---------- GLOBALS ---------- */
91 +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
92 +static unsigned int min_frequency, max_frequency;
93 +static struct cpufreq_frequency_table bcm2835_freq_table[3];
96 + ===============================================
97 + clk_rate either gets or sets the clock rates.
98 + ===============================================
101 +static int bcm2835_cpufreq_clock_property(u32 tag, u32 id, u32 *val)
103 + struct rpi_firmware *fw = rpi_firmware_get(NULL);
112 + ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
121 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
123 + u32 rate = arm_rate * 1000;
126 + ret = bcm2835_cpufreq_clock_property(RPI_FIRMWARE_SET_CLOCK_RATE, VCMSG_ID_ARM_CLOCK, &rate);
128 + print_err("Failed to set clock: %d (%d)\n", arm_rate, ret);
133 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, rate);
138 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
143 + ret = bcm2835_cpufreq_clock_property(tag, VCMSG_ID_ARM_CLOCK, &rate);
145 + print_err("Failed to get clock (%d)\n", ret);
150 + print_debug("%s frequency = %u\n",
151 + tag == RPI_FIRMWARE_GET_CLOCK_RATE ? "Current":
152 + tag == RPI_FIRMWARE_GET_MIN_CLOCK_RATE ? "Min":
153 + tag == RPI_FIRMWARE_GET_MAX_CLOCK_RATE ? "Max":
154 + "Unexpected", rate);
160 + ====================================================
161 + Module Initialisation registers the cpufreq driver
162 + ====================================================
164 +static int __init bcm2835_cpufreq_module_init(void)
166 + print_debug("IN\n");
167 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
175 +static void __exit bcm2835_cpufreq_module_exit(void)
177 + print_debug("IN\n");
178 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
183 + ==============================================================
184 + Initialisation function sets up the CPU policy for first use
185 + ==============================================================
187 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
189 + /* measured value of how long it takes to change frequency */
190 + const unsigned int transition_latency = 355000; /* ns */
192 + if (!rpi_firmware_get(NULL)) {
193 + print_err("Firmware is not available\n");
197 + /* now find out what the maximum and minimum frequencies are */
198 + min_frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MIN_CLOCK_RATE);
199 + max_frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MAX_CLOCK_RATE);
201 + if (min_frequency == max_frequency) {
202 + bcm2835_freq_table[0].frequency = min_frequency;
203 + bcm2835_freq_table[1].frequency = CPUFREQ_TABLE_END;
205 + bcm2835_freq_table[0].frequency = min_frequency;
206 + bcm2835_freq_table[1].frequency = max_frequency;
207 + bcm2835_freq_table[2].frequency = CPUFREQ_TABLE_END;
210 + print_info("min=%d max=%d\n", min_frequency, max_frequency);
211 + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
215 + =====================================================================
216 + Target index function chooses the requested frequency from the table
217 + =====================================================================
220 +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
222 + unsigned int target_freq = state == 0 ? min_frequency : max_frequency;
223 + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
227 + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
230 + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
235 + ======================================================
236 + Get function returns the current frequency from table
237 + ======================================================
240 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
242 + unsigned int actual_rate = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_CLOCK_RATE);
243 + print_debug("cpu%d: freq=%d\n", cpu, actual_rate);
244 + return actual_rate <= min_frequency ? min_frequency : max_frequency;
247 +/* the CPUFreq driver */
248 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
249 + .name = "BCM2835 CPUFreq",
250 + .init = bcm2835_cpufreq_driver_init,
251 + .verify = cpufreq_generic_frequency_table_verify,
252 + .target_index = bcm2835_cpufreq_driver_target_index,
253 + .get = bcm2835_cpufreq_driver_get,
254 + .attr = cpufreq_generic_attr,
257 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
258 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
259 +MODULE_LICENSE("GPL");
261 +module_init(bcm2835_cpufreq_module_init);
262 +module_exit(bcm2835_cpufreq_module_exit);