brcm2708: update to latest patches from RPi foundation
[openwrt/staging/wigyori.git] / target / linux / brcm2708 / patches-4.19 / 950-0052-Add-cpufreq-driver.patch
1 From 0a248af6e18d7f1ad57fffa7f588bc8a5851832e 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/806] Add cpufreq driver
5
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
7
8 bcm2835-cpufreq: Change licence to GPLv2
9
10 Signed-off-by: Eben Upton <eben.upton@broadcom.com>
11 Signed-off-by: Dom Cobley <dom@raspberrypi.com>
12 ---
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
18
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
23 default y
24
25 +config ARM_BCM2835_CPUFREQ
26 + depends on RASPBERRYPI_FIRMWARE
27 + bool "BCM2835 Driver"
28 + default y
29 + help
30 + This adds the CPUFreq driver for BCM2835
31 +
32 + If in doubt, say N.
33 +
34 config ARM_TEGRA20_CPUFREQ
35 tristate "Tegra20 CPUFreq support"
36 depends on ARCH_TEGRA
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
47 --- /dev/null
48 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
49 @@ -0,0 +1,210 @@
50 +/*
51 + * Copyright 2011 Broadcom Corporation.
52 + *
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
56 + * of the License.
57 + *
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.
63 + */
64 +
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>
71 +
72 +/* ---------- DEFINES ---------- */
73 +/*#define CPUFREQ_DEBUG_ENABLE*/ /* enable debugging */
74 +#define MODULE_NAME "bcm2835-cpufreq"
75 +
76 +#define VCMSG_ID_ARM_CLOCK 0x000000003 /* Clock/Voltage ID's */
77 +
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__)
81 +#else
82 +#define print_debug(fmt,...)
83 +#endif
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__)
86 +
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];
91 +
92 +/*
93 + ===============================================
94 + clk_rate either gets or sets the clock rates.
95 + ===============================================
96 +*/
97 +
98 +static int bcm2835_cpufreq_clock_property(u32 tag, u32 id, u32 *val)
99 +{
100 + struct rpi_firmware *fw = rpi_firmware_get(NULL);
101 + struct {
102 + u32 id;
103 + u32 val;
104 + } packet;
105 + int ret;
106 +
107 + packet.id = id;
108 + packet.val = *val;
109 + ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
110 + if (ret)
111 + return ret;
112 +
113 + *val = packet.val;
114 +
115 + return 0;
116 +}
117 +
118 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
119 +{
120 + u32 rate = arm_rate * 1000;
121 + int ret;
122 +
123 + ret = bcm2835_cpufreq_clock_property(RPI_FIRMWARE_SET_CLOCK_RATE, VCMSG_ID_ARM_CLOCK, &rate);
124 + if (ret) {
125 + print_err("Failed to set clock: %d (%d)\n", arm_rate, ret);
126 + return 0;
127 + }
128 +
129 + rate /= 1000;
130 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, rate);
131 +
132 + return rate;
133 +}
134 +
135 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
136 +{
137 + u32 rate;
138 + int ret;
139 +
140 + ret = bcm2835_cpufreq_clock_property(tag, VCMSG_ID_ARM_CLOCK, &rate);
141 + if (ret) {
142 + print_err("Failed to get clock (%d)\n", ret);
143 + return 0;
144 + }
145 +
146 + rate /= 1000;
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);
152 +
153 + return rate;
154 +}
155 +
156 +/*
157 + ====================================================
158 + Module Initialisation registers the cpufreq driver
159 + ====================================================
160 +*/
161 +static int __init bcm2835_cpufreq_module_init(void)
162 +{
163 + print_debug("IN\n");
164 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
165 +}
166 +
167 +/*
168 + =============
169 + Module exit
170 + =============
171 +*/
172 +static void __exit bcm2835_cpufreq_module_exit(void)
173 +{
174 + print_debug("IN\n");
175 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
176 + return;
177 +}
178 +
179 +/*
180 + ==============================================================
181 + Initialisation function sets up the CPU policy for first use
182 + ==============================================================
183 +*/
184 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
185 +{
186 + /* measured value of how long it takes to change frequency */
187 + const unsigned int transition_latency = 355000; /* ns */
188 +
189 + if (!rpi_firmware_get(NULL)) {
190 + print_err("Firmware is not available\n");
191 + return -ENODEV;
192 + }
193 +
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);
197 +
198 + if (min_frequency == max_frequency) {
199 + bcm2835_freq_table[0].frequency = min_frequency;
200 + bcm2835_freq_table[1].frequency = CPUFREQ_TABLE_END;
201 + } else {
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;
205 + }
206 +
207 + print_info("min=%d max=%d\n", min_frequency, max_frequency);
208 + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
209 +}
210 +
211 +/*
212 + =====================================================================
213 + Target index function chooses the requested frequency from the table
214 + =====================================================================
215 +*/
216 +
217 +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
218 +{
219 + unsigned int target_freq = state == 0 ? min_frequency : max_frequency;
220 + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
221 +
222 + if (!cur)
223 + {
224 + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
225 + return -EINVAL;
226 + }
227 + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
228 + return 0;
229 +}
230 +
231 +/*
232 + ======================================================
233 + Get function returns the current frequency from table
234 + ======================================================
235 +*/
236 +
237 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
238 +{
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;
242 +}
243 +
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,
252 +};
253 +
254 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
255 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
256 +MODULE_LICENSE("GPL");
257 +
258 +module_init(bcm2835_cpufreq_module_init);
259 +module_exit(bcm2835_cpufreq_module_exit);