6da1856d92f0f6d9d4d18476d7841126480e82f4
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0017-Add-cpufreq-driver.patch
1 From ccdc49f30be0caec97b9b7e4352b8c63eb45acdd 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 017/121] 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 | 224 ++++++++++++++++++++++++++++++++++++++
11 3 files changed, 234 insertions(+)
12 create mode 100644 drivers/cpufreq/bcm2835-cpufreq.c
13
14 --- a/drivers/cpufreq/Kconfig.arm
15 +++ b/drivers/cpufreq/Kconfig.arm
16 @@ -258,6 +258,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 BCM2708_MBOX
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_TEGRA_CPUFREQ
30 bool "TEGRA CPUFreq support"
31 depends on ARCH_TEGRA
32 --- a/drivers/cpufreq/Makefile
33 +++ b/drivers/cpufreq/Makefile
34 @@ -77,6 +77,7 @@ obj-$(CONFIG_ARM_S5PV210_CPUFREQ) += s5p
35 obj-$(CONFIG_ARM_SA1100_CPUFREQ) += sa1100-cpufreq.o
36 obj-$(CONFIG_ARM_SA1110_CPUFREQ) += sa1110-cpufreq.o
37 obj-$(CONFIG_ARM_SPEAR_CPUFREQ) += spear-cpufreq.o
38 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ) += bcm2835-cpufreq.o
39 obj-$(CONFIG_ARM_TEGRA_CPUFREQ) += tegra-cpufreq.o
40 obj-$(CONFIG_ARM_VEXPRESS_SPC_CPUFREQ) += vexpress-spc-cpufreq.o
41
42 --- /dev/null
43 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
44 @@ -0,0 +1,224 @@
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 <linux/platform_data/mailbox-bcm2708.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 +/* tag part of the message */
91 +struct vc_msg_tag {
92 + uint32_t tag_id; /* the message id */
93 + uint32_t buffer_size; /* size of the buffer (which in this case is always 8 bytes) */
94 + uint32_t data_size; /* amount of data being sent or received */
95 + uint32_t dev_id; /* the ID of the clock/voltage to get or set */
96 + uint32_t val; /* the value (e.g. rate (in Hz)) to set */
97 +};
98 +
99 +/* message structure to be sent to videocore */
100 +struct vc_msg {
101 + uint32_t msg_size; /* simply, sizeof(struct vc_msg) */
102 + uint32_t request_code; /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */
103 + struct vc_msg_tag tag; /* the tag structure above to make */
104 + uint32_t end_tag; /* an end identifier, should be set to NULL */
105 +};
106 +
107 +/* ---------- GLOBALS ---------- */
108 +static struct cpufreq_driver bcm2835_cpufreq_driver; /* the cpufreq driver global */
109 +
110 +static struct cpufreq_frequency_table bcm2835_freq_table[] = {
111 + {0, 0, 0},
112 + {0, 0, 0},
113 + {0, 0, CPUFREQ_TABLE_END},
114 +};
115 +
116 +/*
117 + ===============================================
118 + clk_rate either gets or sets the clock rates.
119 + ===============================================
120 +*/
121 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
122 +{
123 + int s, actual_rate=0;
124 + struct vc_msg msg;
125 +
126 + /* wipe all previous message data */
127 + memset(&msg, 0, sizeof msg);
128 +
129 + msg.msg_size = sizeof msg;
130 +
131 + msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;
132 + msg.tag.buffer_size = 8;
133 + msg.tag.data_size = 8; /* we're sending the clock ID and the new rates which is a total of 2 words */
134 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
135 + msg.tag.val = arm_rate * 1000;
136 +
137 + /* send the message */
138 + s = bcm_mailbox_property(&msg, sizeof msg);
139 +
140 + /* check if it was all ok and return the rate in KHz */
141 + if (s == 0 && (msg.request_code & 0x80000000))
142 + actual_rate = msg.tag.val/1000;
143 +
144 + print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate);
145 + return actual_rate;
146 +}
147 +
148 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
149 +{
150 + int s;
151 + int arm_rate = 0;
152 + struct vc_msg msg;
153 +
154 + /* wipe all previous message data */
155 + memset(&msg, 0, sizeof msg);
156 +
157 + msg.msg_size = sizeof msg;
158 + msg.tag.tag_id = tag;
159 + msg.tag.buffer_size = 8;
160 + msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */
161 + msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;
162 +
163 + /* send the message */
164 + s = bcm_mailbox_property(&msg, sizeof msg);
165 +
166 + /* check if it was all ok and return the rate in KHz */
167 + if (s == 0 && (msg.request_code & 0x80000000))
168 + arm_rate = msg.tag.val/1000;
169 +
170 + print_debug("%s frequency = %d\n",
171 + tag == VCMSG_GET_CLOCK_RATE ? "Current":
172 + tag == VCMSG_GET_MIN_CLOCK ? "Min":
173 + tag == VCMSG_GET_MAX_CLOCK ? "Max":
174 + "Unexpected", arm_rate);
175 +
176 + return arm_rate;
177 +}
178 +
179 +/*
180 + ====================================================
181 + Module Initialisation registers the cpufreq driver
182 + ====================================================
183 +*/
184 +static int __init bcm2835_cpufreq_module_init(void)
185 +{
186 + print_debug("IN\n");
187 + return cpufreq_register_driver(&bcm2835_cpufreq_driver);
188 +}
189 +
190 +/*
191 + =============
192 + Module exit
193 + =============
194 +*/
195 +static void __exit bcm2835_cpufreq_module_exit(void)
196 +{
197 + print_debug("IN\n");
198 + cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
199 + return;
200 +}
201 +
202 +/*
203 + ==============================================================
204 + Initialisation function sets up the CPU policy for first use
205 + ==============================================================
206 +*/
207 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
208 +{
209 + /* measured value of how long it takes to change frequency */
210 + const unsigned int transition_latency = 355000; /* ns */
211 +
212 + /* now find out what the maximum and minimum frequencies are */
213 + bcm2835_freq_table[0].frequency = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);
214 + bcm2835_freq_table[1].frequency = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);
215 +
216 + print_info("min=%d max=%d\n", bcm2835_freq_table[0].frequency, bcm2835_freq_table[1].frequency);
217 + return cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
218 +}
219 +
220 +/*
221 + =====================================================================
222 + Target index function chooses the requested frequency from the table
223 + =====================================================================
224 +*/
225 +
226 +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
227 +{
228 + unsigned int target_freq = bcm2835_freq_table[state].frequency;
229 + unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
230 +
231 + if (!cur)
232 + {
233 + print_err("Error occurred setting a new frequency (%d)\n", target_freq);
234 + return -EINVAL;
235 + }
236 + print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
237 + return 0;
238 +}
239 +
240 +/*
241 + ======================================================
242 + Get function returns the current frequency from table
243 + ======================================================
244 +*/
245 +
246 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
247 +{
248 + unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);
249 + print_debug("%d: freq=%d\n", cpu, actual_rate);
250 + return actual_rate <= bcm2835_freq_table[0].frequency ? bcm2835_freq_table[0].frequency : bcm2835_freq_table[1].frequency;
251 +}
252 +
253 +/* the CPUFreq driver */
254 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
255 + .name = "BCM2835 CPUFreq",
256 + .init = bcm2835_cpufreq_driver_init,
257 + .verify = cpufreq_generic_frequency_table_verify,
258 + .target_index = bcm2835_cpufreq_driver_target_index,
259 + .get = bcm2835_cpufreq_driver_get,
260 + .attr = cpufreq_generic_attr,
261 +};
262 +
263 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
264 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
265 +MODULE_LICENSE("GPL");
266 +
267 +module_init(bcm2835_cpufreq_module_init);
268 +module_exit(bcm2835_cpufreq_module_exit);