ixp4xx: remove linux 3.10 support
[openwrt.git] / target / linux / brcm2708 / patches-3.10 / 0031-Add-cpufreq-driver.patch
1 From 083183f7770ae4fb6f23c99a64a3c5058b9b3b40 Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Sat, 2 Nov 2013 22:44:41 +0000
4 Subject: [PATCH 031/196] Add cpufreq driver
5
6 ---
7  arch/arm/Kconfig                  |   1 +
8  drivers/cpufreq/Kconfig.arm       |   8 ++
9  drivers/cpufreq/Makefile          |   1 +
10  drivers/cpufreq/bcm2835-cpufreq.c | 239 ++++++++++++++++++++++++++++++++++++++
11  4 files changed, 249 insertions(+)
12  create mode 100755 drivers/cpufreq/bcm2835-cpufreq.c
13
14 diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
15 index 5468f1f..4e36ccd 100644
16 --- a/arch/arm/Kconfig
17 +++ b/arch/arm/Kconfig
18 @@ -369,6 +369,7 @@ config ARCH_BCM2708
19         select HAVE_SCHED_CLOCK
20         select NEED_MACH_MEMORY_H
21         select CLKDEV_LOOKUP
22 +       select ARCH_HAS_CPUFREQ
23         select GENERIC_CLOCKEVENTS
24         select ARM_ERRATA_411920
25         select MACH_BCM2708
26 diff --git a/drivers/cpufreq/Kconfig.arm b/drivers/cpufreq/Kconfig.arm
27 index 6e57543..bf40783 100644
28 --- a/drivers/cpufreq/Kconfig.arm
29 +++ b/drivers/cpufreq/Kconfig.arm
30 @@ -150,3 +150,11 @@ config ARM_SPEAR_CPUFREQ
31         default y
32         help
33           This adds the CPUFreq driver support for SPEAr SOCs.
34 +
35 +config ARM_BCM2835_CPUFREQ
36 +       bool "BCM2835 Driver"
37 +       default y
38 +       help
39 +         This adds the CPUFreq driver for BCM2835
40 +
41 +         If in doubt, say N.
42 diff --git a/drivers/cpufreq/Makefile b/drivers/cpufreq/Makefile
43 index 3b95322..3a30bab 100644
44 --- a/drivers/cpufreq/Makefile
45 +++ b/drivers/cpufreq/Makefile
46 @@ -72,6 +72,7 @@ obj-$(CONFIG_ARM_SA1100_CPUFREQ)      += sa1100-cpufreq.o
47  obj-$(CONFIG_ARM_SA1110_CPUFREQ)       += sa1110-cpufreq.o
48  obj-$(CONFIG_ARM_SPEAR_CPUFREQ)                += spear-cpufreq.o
49  obj-$(CONFIG_ARCH_TEGRA)               += tegra-cpufreq.o
50 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ)      += bcm2835-cpufreq.o
51  
52  ##################################################################################
53  # PowerPC platform drivers
54 diff --git a/drivers/cpufreq/bcm2835-cpufreq.c b/drivers/cpufreq/bcm2835-cpufreq.c
55 new file mode 100755
56 index 0000000..822139a
57 --- /dev/null
58 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
59 @@ -0,0 +1,239 @@
60 +/*****************************************************************************
61 +* Copyright 2011 Broadcom Corporation.  All rights reserved.
62 +*
63 +* Unless you and Broadcom execute a separate written software license
64 +* agreement governing use of this software, this software is licensed to you
65 +* under the terms of the GNU General Public License version 2, available at
66 +* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
67 +*      
68 +* Notwithstanding the above, under no circumstances may you combine this
69 +* software in any way with any other Broadcom software provided under a
70 +* license other than the GPL, without Broadcom's express prior written
71 +* consent.
72 +*****************************************************************************/
73 +
74 +/*****************************************************************************
75 +* FILENAME: bcm2835-cpufreq.h
76 +* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM
77 +* processor. Messages are sent to Videocore either setting or requesting the
78 +* frequency of the ARM in order to match an appropiate frequency to the current
79 +* usage of the processor. The policy which selects the frequency to use is
80 +* defined in the kernel .config file, but can be changed during runtime.
81 +*****************************************************************************/
82 +
83 +/* ---------- INCLUDES ---------- */
84 +#include <linux/kernel.h>
85 +#include <linux/init.h>
86 +#include <linux/module.h>
87 +#include <linux/cpufreq.h>
88 +#include <mach/vcio.h>
89 +
90 +/* ---------- DEFINES ---------- */
91 +/*#define CPUFREQ_DEBUG_ENABLE*/               /* enable debugging */
92 +#define MODULE_NAME "bcm2835-cpufreq"
93 +
94 +#define VCMSG_ID_ARM_CLOCK 0x000000003         /* Clock/Voltage ID's */
95 +
96 +/* debug printk macros */
97 +#ifdef CPUFREQ_DEBUG_ENABLE
98 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
99 +#else
100 +#define print_debug(fmt,...)
101 +#endif
102 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
103 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
104 +
105 +/* tag part of the message */
106 +struct vc_msg_tag {
107 +       uint32_t tag_id;                /* the message id */
108 +       uint32_t buffer_size;           /* size of the buffer (which in this case is always 8 bytes) */
109 +       uint32_t data_size;             /* amount of data being sent or received */
110 +       uint32_t dev_id;                /* the ID of the clock/voltage to get or set */
111 +       uint32_t val;                   /* the value (e.g. rate (in Hz)) to set */
112 +};
113 +
114 +/* message structure to be sent to videocore */
115 +struct vc_msg {
116 +       uint32_t msg_size;              /* simply, sizeof(struct vc_msg) */
117 +       uint32_t request_code;          /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
118 +       struct vc_msg_tag tag;          /* the tag structure above to make */
119 +       uint32_t end_tag;               /* an end identifier, should be set to NULL */
120 +};
121 +
122 +/* ---------- GLOBALS ---------- */
123 +static struct cpufreq_driver bcm2835_cpufreq_driver;   /* the cpufreq driver global */
124 +
125 +/*
126 + ===============================================
127 +  clk_rate either gets or sets the clock rates.
128 + ===============================================
129 +*/
130 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
131 +{
132 +       int s, actual_rate=0;
133 +       struct vc_msg msg;
134 +       
135 +       /* wipe all previous message data */
136 +       memset(&msg, 0, sizeof msg);
137 +       
138 +       msg.msg_size = sizeof msg;
139 +                       
140 +       msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
141 +       msg.tag.buffer_size = 8;
142 +       msg.tag.data_size = 8;   /* we're sending the clock ID and the new rates which is a total of 2 words */
143 +       msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
144 +       msg.tag.val = arm_rate * 1000;
145 +                       
146 +       /* send the message */
147 +       s = bcm_mailbox_property(&msg, sizeof msg);
148 +       
149 +       /* check if it was all ok and return the rate in KHz */
150 +       if (s == 0 && (msg.request_code & 0x80000000))
151 +               actual_rate = msg.tag.val/1000;
152 +
153 +       print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate); 
154 +       return actual_rate;
155 +}
156 +
157 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
158 +{
159 +       int s;
160 +       int arm_rate = 0;
161 +       struct vc_msg msg;
162 +       
163 +       /* wipe all previous message data */
164 +       memset(&msg, 0, sizeof msg);
165 +       
166 +       msg.msg_size = sizeof msg;
167 +       msg.tag.tag_id = tag;
168 +       msg.tag.buffer_size = 8;
169 +       msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
170 +       msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
171 +       
172 +       /* send the message */
173 +       s = bcm_mailbox_property(&msg, sizeof msg);
174 +
175 +       /* check if it was all ok and return the rate in KHz */
176 +       if (s == 0 && (msg.request_code & 0x80000000))
177 +               arm_rate = msg.tag.val/1000;
178 +
179 +       print_debug("%s frequency = %d\n",
180 +               tag == VCMSG_GET_CLOCK_RATE ? "Current":
181 +               tag == VCMSG_GET_MIN_CLOCK ? "Min":
182 +               tag == VCMSG_GET_MAX_CLOCK ? "Max":
183 +               "Unexpected", arm_rate);
184 +       
185 +       return arm_rate;
186 +}
187 +
188 +/*
189 + ====================================================
190 +  Module Initialisation registers the cpufreq driver
191 + ====================================================
192 +*/
193 +static int __init bcm2835_cpufreq_module_init(void)
194 +{      
195 +       print_debug("IN\n");
196 +       return cpufreq_register_driver(&bcm2835_cpufreq_driver);
197 +}
198 +
199 +/*
200 + =============
201 +  Module exit
202 + =============
203 +*/
204 +static void __exit bcm2835_cpufreq_module_exit(void)
205 +{
206 +       print_debug("IN\n");
207 +       cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
208 +       return;
209 +}
210 +
211 +/*
212 + ==============================================================
213 +  Initialisation function sets up the CPU policy for first use
214 + ==============================================================
215 +*/
216 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
217 +{
218 +       /* measured value of how long it takes to change frequency */   
219 +       policy->cpuinfo.transition_latency = 355000; /* ns */
220 +
221 +       /* now find out what the maximum and minimum frequencies are */
222 +       policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
223 +       policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
224 +       policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
225 +       
226 +       print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);
227 +       return 0;
228 +}
229 +
230 +/*
231 + =================================================================================
232 +  Target function chooses the most appropriate frequency from the table to enable
233 + =================================================================================
234 +*/
235 +
236 +static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)
237 +{
238 +       unsigned int target = target_freq;
239 +       unsigned int cur = policy->cur;
240 +       print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);
241 +       
242 +       /* if we are above min and using ondemand, then just use max */
243 +       if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)
244 +               target = policy->max;
245 +       /* if the frequency is the same, just quit */
246 +       if (target == policy->cur)
247 +               return 0;
248 +
249 +       /* otherwise were good to set the clock frequency */
250 +       policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);
251 +       
252 +       if (!policy->cur)
253 +       {
254 +               print_err("Error occurred setting a new frequency (%d)!\n", target);
255 +               policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
256 +               return -EINVAL;
257 +       }
258 +       print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);
259 +       return 0;
260 +}
261 +
262 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
263 +{
264 +       unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
265 +       print_debug("cpu=%d\n", actual_rate);
266 +       return actual_rate;
267 +}
268 +
269 +/*
270 + =================================================================================
271 +  Verify ensures that when a policy is changed, it is suitable for the CPU to use
272 + =================================================================================
273 +*/
274 +
275 +static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)
276 +{
277 +       print_info("switching to governor %s\n", policy->governor->name);
278 +       return 0;
279 +}
280 +
281 +
282 +/* the CPUFreq driver */
283 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
284 +               .name   = "BCM2835 CPUFreq",
285 +               .owner  = THIS_MODULE,
286 +               .init   = bcm2835_cpufreq_driver_init,
287 +               .verify = bcm2835_cpufreq_driver_verify,
288 +               .target = bcm2835_cpufreq_driver_target,
289 +               .get    = bcm2835_cpufreq_driver_get
290 +};
291 +
292 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
293 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
294 +MODULE_LICENSE("GPL");
295 +
296 +module_init(bcm2835_cpufreq_module_init);
297 +module_exit(bcm2835_cpufreq_module_exit);
298 +
299 -- 
300 1.9.1
301