brcm2708: add linux 4.9 support
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.9 / 0046-Add-cpufreq-driver.patch
1 From 91eeb6693671b5ae0220f4a276b84e6597266cb9 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
5
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
7 ---
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
13
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
18 SoCs.
19
20 +config ARM_BCM2835_CPUFREQ
21 + depends on RASPBERRYPI_FIRMWARE
22 + bool "BCM2835 Driver"
23 + default y
24 + help
25 + This adds the CPUFreq driver for BCM2835
26 +
27 + If in doubt, say N.
28 +
29 config ARM_TEGRA20_CPUFREQ
30 bool "Tegra20 CPUFreq support"
31 depends on ARCH_TEGRA
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
42 --- /dev/null
43 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
44 @@ -0,0 +1,218 @@
45 +/*****************************************************************************
46 +* Copyright 2011 Broadcom Corporation. All rights reserved.
47 +*
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").
52 +*
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
56 +* consent.
57 +*****************************************************************************/
58 +
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 +*****************************************************************************/
67 +
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>
74 +
75 +/* ---------- DEFINES ---------- */
76 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
77 +#define MODULE_NAME "bcm2835-cpufreq"
78 +
79 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
80 +
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__)
84 +#else
85 +#define print_debug(fmt,...)
86 +#endif
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__)
89 +
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];
94 +
95 +/*
96 + ===============================================
97 + clk_rate either gets or sets the clock rates.
98 + ===============================================
99 +*/
100 +
101 +static int bcm2835_cpufreq_clock_property(u32 tag, u32 id, u32 *val)
102 +{
103 + struct rpi_firmware *fw = rpi_firmware_get(NULL);
104 + struct {
105 + u32 id;
106 + u32 val;
107 + } packet;
108 + int ret;
109 +
110 + packet.id = id;
111 + packet.val = *val;
112 + ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
113 + if (ret)
114 + return ret;
115 +
116 + *val = packet.val;
117 +
118 + return 0;
119 +}
120 +
121 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
122 +{
123 + u32 rate = arm_rate * 1000;
124 + int ret;
125 +
126 + ret = bcm2835_cpufreq_clock_property(RPI_FIRMWARE_SET_CLOCK_RATE, VCMSG_ID_ARM_CLOCK, &rate);
127 + if (ret) {
128 + print_err("Failed to set clock: %d (%d)\n", arm_rate, ret);
129 + return 0;
130 + }
131 +
132 + rate /= 1000;
133 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, rate);
134 +
135 + return rate;
136 +}
137 +
138 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
139 +{
140 + u32 rate;
141 + int ret;
142 +
143 + ret = bcm2835_cpufreq_clock_property(tag, VCMSG_ID_ARM_CLOCK, &rate);
144 + if (ret) {
145 + print_err("Failed to get clock (%d)\n", ret);
146 + return 0;
147 + }
148 +
149 + rate /= 1000;
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);
155 +
156 + return rate;
157 +}
158 +
159 +/*
160 + ====================================================
161 + Module Initialisation registers the cpufreq driver
162 + ====================================================
163 +*/
164 +static int __init bcm2835_cpufreq_module_init(void)
165 +{
166 + print_debug("IN\n");
167 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
168 +}
169 +
170 +/*
171 + =============
172 + Module exit
173 + =============
174 +*/
175 +static void __exit bcm2835_cpufreq_module_exit(void)
176 +{
177 + print_debug("IN\n");
178 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
179 + return;
180 +}
181 +
182 +/*
183 + ==============================================================
184 + Initialisation function sets up the CPU policy for first use
185 + ==============================================================
186 +*/
187 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
188 +{
189 + /* measured value of how long it takes to change frequency */
190 + const unsigned int transition_latency = 355000; /* ns */
191 +
192 + if (!rpi_firmware_get(NULL)) {
193 + print_err("Firmware is not available\n");
194 + return -ENODEV;
195 + }
196 +
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);
200 +
201 + if (min_frequency == max_frequency) {
202 + bcm2835_freq_table[0].frequency = min_frequency;
203 + bcm2835_freq_table[1].frequency = CPUFREQ_TABLE_END;
204 + } else {
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;
208 + }
209 +
210 + print_info("min=%d max=%d\n", min_frequency, max_frequency);
211 + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
212 +}
213 +
214 +/*
215 + =====================================================================
216 + Target index function chooses the requested frequency from the table
217 + =====================================================================
218 +*/
219 +
220 +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
221 +{
222 + unsigned int target_freq = state == 0 ? min_frequency : max_frequency;
223 + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
224 +
225 + if (!cur)
226 + {
227 + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
228 + return -EINVAL;
229 + }
230 + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
231 + return 0;
232 +}
233 +
234 +/*
235 + ======================================================
236 + Get function returns the current frequency from table
237 + ======================================================
238 +*/
239 +
240 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
241 +{
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;
245 +}
246 +
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,
255 +};
256 +
257 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
258 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
259 +MODULE_LICENSE("GPL");
260 +
261 +module_init(bcm2835_cpufreq_module_init);
262 +module_exit(bcm2835_cpufreq_module_exit);