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