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