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