1 From 6ae7a56fdff2578805c6f17f03b5bc1b1f9d45a8 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 052/703] Add cpufreq driver
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 bcm2835-cpufreq: Change licence to GPLv2
10 Signed-off-by: Eben Upton <eben.upton@broadcom.com>
11 Signed-off-by: Dom Cobley <dom@raspberrypi.com>
13 drivers/cpufreq/Kconfig.arm | 9 ++
14 drivers/cpufreq/Makefile | 1 +
15 drivers/cpufreq/bcm2835-cpufreq.c | 210 ++++++++++++++++++++++++++++++
16 3 files changed, 220 insertions(+)
17 create mode 100644 drivers/cpufreq/bcm2835-cpufreq.c
19 --- a/drivers/cpufreq/Kconfig.arm
20 +++ b/drivers/cpufreq/Kconfig.arm
21 @@ -260,6 +260,15 @@ config ARM_TANGO_CPUFREQ
22 depends on CPUFREQ_DT && ARCH_TANGO
25 +config ARM_BCM2835_CPUFREQ
26 + depends on RASPBERRYPI_FIRMWARE
27 + bool "BCM2835 Driver"
30 + This adds the CPUFreq driver for BCM2835
34 config ARM_TEGRA20_CPUFREQ
35 tristate "Tegra20 CPUFreq support"
37 --- a/drivers/cpufreq/Makefile
38 +++ b/drivers/cpufreq/Makefile
39 @@ -80,6 +80,7 @@ obj-$(CONFIG_ARM_SCPI_CPUFREQ) += scpi-
40 obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
41 obj-$(CONFIG_ARM_STI_CPUFREQ) += sti-cpufreq.o
42 obj-$(CONFIG_ARM_TANGO_CPUFREQ) += tango-cpufreq.o
43 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
44 obj-$(CONFIG_ARM_TEGRA20_CPUFREQ) += tegra20-cpufreq.o
45 obj-$(CONFIG_ARM_TEGRA124_CPUFREQ) += tegra124-cpufreq.o
46 obj-$(CONFIG_ARM_TEGRA186_CPUFREQ) += tegra186-cpufreq.o
48 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
51 + * Copyright 2011 Broadcom Corporation.
53 + * This program is free software; you can redistribute it and/or
54 + * modify it under the terms of the GNU General Public License
55 + * as published by the Free Software Foundation; version 2
58 + * This driver dynamically manages the CPU Frequency of the ARM
59 + * processor. Messages are sent to Videocore either setting or requesting the
60 + * frequency of the ARM in order to match an appropiate frequency to the current
61 + * usage of the processor. The policy which selects the frequency to use is
62 + * defined in the kernel .config file, but can be changed during runtime.
65 +/* ---------- INCLUDES ---------- */
66 +#include <linux/kernel.h>
67 +#include <linux/init.h>
68 +#include <linux/module.h>
69 +#include <linux/cpufreq.h>
70 +#include <soc/bcm2835/raspberrypi-firmware.h>
72 +/* ---------- DEFINES ---------- */
73 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
74 +#define MODULE_NAME "bcm2835-cpufreq"
76 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
78 +/* debug printk macros */
79 +#ifdef CPUFREQ_DEBUG_ENABLE
80 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
82 +#define print_debug(fmt,...)
84 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
85 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
87 +/* ---------- GLOBALS ---------- */
88 +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
89 +static unsigned int min_frequency, max_frequency;
90 +static struct cpufreq_frequency_table bcm2835_freq_table[3];
93 + ===============================================
94 + clk_rate either gets or sets the clock rates.
95 + ===============================================
98 +static int bcm2835_cpufreq_clock_property(u32 tag, u32 id, u32 *val)
100 + struct rpi_firmware *fw = rpi_firmware_get(NULL);
109 + ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
118 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
120 + u32 rate = arm_rate * 1000;
123 + ret = bcm2835_cpufreq_clock_property(RPI_FIRMWARE_SET_CLOCK_RATE, VCMSG_ID_ARM_CLOCK, &rate);
125 + print_err("Failed to set clock: %d (%d)\n", arm_rate, ret);
130 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, rate);
135 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
140 + ret = bcm2835_cpufreq_clock_property(tag, VCMSG_ID_ARM_CLOCK, &rate);
142 + print_err("Failed to get clock (%d)\n", ret);
147 + print_debug("%s frequency = %u\n",
148 + tag == RPI_FIRMWARE_GET_CLOCK_RATE ? "Current":
149 + tag == RPI_FIRMWARE_GET_MIN_CLOCK_RATE ? "Min":
150 + tag == RPI_FIRMWARE_GET_MAX_CLOCK_RATE ? "Max":
151 + "Unexpected", rate);
157 + ====================================================
158 + Module Initialisation registers the cpufreq driver
159 + ====================================================
161 +static int __init bcm2835_cpufreq_module_init(void)
163 + print_debug("IN\n");
164 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
172 +static void __exit bcm2835_cpufreq_module_exit(void)
174 + print_debug("IN\n");
175 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
180 + ==============================================================
181 + Initialisation function sets up the CPU policy for first use
182 + ==============================================================
184 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
186 + /* measured value of how long it takes to change frequency */
187 + const unsigned int transition_latency = 355000; /* ns */
189 + if (!rpi_firmware_get(NULL)) {
190 + print_err("Firmware is not available\n");
194 + /* now find out what the maximum and minimum frequencies are */
195 + min_frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MIN_CLOCK_RATE);
196 + max_frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MAX_CLOCK_RATE);
198 + if (min_frequency == max_frequency) {
199 + bcm2835_freq_table[0].frequency = min_frequency;
200 + bcm2835_freq_table[1].frequency = CPUFREQ_TABLE_END;
202 + bcm2835_freq_table[0].frequency = min_frequency;
203 + bcm2835_freq_table[1].frequency = max_frequency;
204 + bcm2835_freq_table[2].frequency = CPUFREQ_TABLE_END;
207 + print_info("min=%d max=%d\n", min_frequency, max_frequency);
208 + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
212 + =====================================================================
213 + Target index function chooses the requested frequency from the table
214 + =====================================================================
217 +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
219 + unsigned int target_freq = state == 0 ? min_frequency : max_frequency;
220 + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
224 + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
227 + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
232 + ======================================================
233 + Get function returns the current frequency from table
234 + ======================================================
237 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
239 + unsigned int actual_rate = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_CLOCK_RATE);
240 + print_debug("cpu%d: freq=%d\n", cpu, actual_rate);
241 + return actual_rate <= min_frequency ? min_frequency : max_frequency;
244 +/* the CPUFreq driver */
245 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
246 + .name = "BCM2835 CPUFreq",
247 + .init = bcm2835_cpufreq_driver_init,
248 + .verify = cpufreq_generic_frequency_table_verify,
249 + .target_index = bcm2835_cpufreq_driver_target_index,
250 + .get = bcm2835_cpufreq_driver_get,
251 + .attr = cpufreq_generic_attr,
254 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
255 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
256 +MODULE_LICENSE("GPL");
258 +module_init(bcm2835_cpufreq_module_init);
259 +module_exit(bcm2835_cpufreq_module_exit);