brcm2708: update linux 4.4 patches to latest version
[openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0044-Add-cpufreq-driver.patch
1 From 3c7ea37319a4974cbf6b89a84c109a53f4d6b408 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 044/170] 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 | 213 ++++++++++++++++++++++++++++++++++++++
11  3 files changed, 223 insertions(+)
12  create mode 100644 drivers/cpufreq/bcm2835-cpufreq.c
13
14 --- a/drivers/cpufreq/Kconfig.arm
15 +++ b/drivers/cpufreq/Kconfig.arm
16 @@ -217,6 +217,15 @@ config ARM_SPEAR_CPUFREQ
17         help
18           This adds the CPUFreq driver support for SPEAr 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 @@ -73,6 +73,7 @@ obj-$(CONFIG_ARM_SA1100_CPUFREQ)      += sa11
35  obj-$(CONFIG_ARM_SA1110_CPUFREQ)       += sa1110-cpufreq.o
36  obj-$(CONFIG_ARM_SCPI_CPUFREQ)         += scpi-cpufreq.o
37  obj-$(CONFIG_ARM_SPEAR_CPUFREQ)                += spear-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,213 @@
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 +
93 +static struct cpufreq_frequency_table bcm2835_freq_table[] = {
94 +       {0, 0, 0},
95 +       {0, 0, 0},
96 +       {0, 0, CPUFREQ_TABLE_END},
97 +};
98 +
99 +/*
100 + ===============================================
101 +  clk_rate either gets or sets the clock rates.
102 + ===============================================
103 +*/
104 +
105 +static int bcm2835_cpufreq_clock_property(u32 tag, u32 id, u32 *val)
106 +{
107 +       struct rpi_firmware *fw = rpi_firmware_get(NULL);
108 +       struct {
109 +               u32 id;
110 +               u32 val;
111 +       } packet;
112 +       int ret;
113 +
114 +       packet.id = id;
115 +       packet.val = *val;
116 +       ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
117 +       if (ret)
118 +               return ret;
119 +
120 +       *val = packet.val;
121 +
122 +       return 0;
123 +}
124 +
125 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
126 +{
127 +       u32 rate = arm_rate * 1000;
128 +       int ret;
129 +
130 +       ret = bcm2835_cpufreq_clock_property(RPI_FIRMWARE_SET_CLOCK_RATE, VCMSG_ID_ARM_CLOCK, &rate);
131 +       if (ret) {
132 +               print_err("Failed to set clock: %d (%d)\n", arm_rate, ret);
133 +               return 0;
134 +       }
135 +
136 +       rate /= 1000;
137 +       print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, rate);
138 +
139 +       return rate;
140 +}
141 +
142 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
143 +{
144 +       u32 rate;
145 +       int ret;
146 +
147 +       ret = bcm2835_cpufreq_clock_property(tag, VCMSG_ID_ARM_CLOCK, &rate);
148 +       if (ret) {
149 +               print_err("Failed to get clock (%d)\n", ret);
150 +               return 0;
151 +       }
152 +
153 +       rate /= 1000;
154 +       print_debug("%s frequency = %u\n",
155 +               tag == RPI_FIRMWARE_GET_CLOCK_RATE ? "Current":
156 +               tag == RPI_FIRMWARE_GET_MIN_CLOCK_RATE ? "Min":
157 +               tag == RPI_FIRMWARE_GET_MAX_CLOCK_RATE ? "Max":
158 +               "Unexpected", rate);
159 +
160 +       return rate;
161 +}
162 +
163 +/*
164 + ====================================================
165 +  Module Initialisation registers the cpufreq driver
166 + ====================================================
167 +*/
168 +static int __init bcm2835_cpufreq_module_init(void)
169 +{
170 +       print_debug("IN\n");
171 +       return cpufreq_register_driver(&bcm2835_cpufreq_driver);
172 +}
173 +
174 +/*
175 + =============
176 +  Module exit
177 + =============
178 +*/
179 +static void __exit bcm2835_cpufreq_module_exit(void)
180 +{
181 +       print_debug("IN\n");
182 +       cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
183 +       return;
184 +}
185 +
186 +/*
187 + ==============================================================
188 +  Initialisation function sets up the CPU policy for first use
189 + ==============================================================
190 +*/
191 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
192 +{
193 +       /* measured value of how long it takes to change frequency */
194 +       const unsigned int transition_latency = 355000; /* ns */
195 +
196 +       if (!rpi_firmware_get(NULL)) {
197 +               print_err("Firmware is not available\n");
198 +               return -ENODEV;
199 +       }
200 +
201 +       /* now find out what the maximum and minimum frequencies are */
202 +       bcm2835_freq_table[0].frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MIN_CLOCK_RATE);
203 +       bcm2835_freq_table[1].frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MAX_CLOCK_RATE);
204 +
205 +       print_info("min=%d max=%d\n", bcm2835_freq_table[0].frequency, bcm2835_freq_table[1].frequency);
206 +       return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
207 +}
208 +
209 +/*
210 + =====================================================================
211 +  Target index function chooses the requested frequency from the table
212 + =====================================================================
213 +*/
214 +
215 +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
216 +{
217 +       unsigned int target_freq = bcm2835_freq_table[state].frequency;
218 +       unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
219 +
220 +       if (!cur)
221 +       {
222 +               print_err("Error occurred setting a new frequency (%d)\n", target_freq);
223 +               return -EINVAL;
224 +       }
225 +       print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
226 +       return 0;
227 +}
228 +
229 +/*
230 + ======================================================
231 +  Get function returns the current frequency from table
232 + ======================================================
233 +*/
234 +
235 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
236 +{
237 +       unsigned int actual_rate = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_CLOCK_RATE);
238 +       print_debug("cpu%d: freq=%d\n", cpu, actual_rate);
239 +       return actual_rate <= bcm2835_freq_table[0].frequency ? bcm2835_freq_table[0].frequency : bcm2835_freq_table[1].frequency;
240 +}
241 +
242 +/* the CPUFreq driver */
243 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
244 +       .name         = "BCM2835 CPUFreq",
245 +       .init         = bcm2835_cpufreq_driver_init,
246 +       .verify       = cpufreq_generic_frequency_table_verify,
247 +       .target_index = bcm2835_cpufreq_driver_target_index,
248 +       .get          = bcm2835_cpufreq_driver_get,
249 +       .attr         = cpufreq_generic_attr,
250 +};
251 +
252 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
253 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
254 +MODULE_LICENSE("GPL");
255 +
256 +module_init(bcm2835_cpufreq_module_init);
257 +module_exit(bcm2835_cpufreq_module_exit);