bcm27xx: update patches from RPi foundation
[oweals/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0049-Add-cpufreq-driver.patch
1 From 0ab080ba0ac496efa285450a555d0a06a1e166d8 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] Add cpufreq driver
5
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
7
8 bcm2835-cpufreq: Change licence to GPLv2
9
10 Signed-off-by: Eben Upton <eben.upton@broadcom.com>
11 Signed-off-by: Dom Cobley <dom@raspberrypi.com>
12 ---
13  drivers/cpufreq/Kconfig.arm       |   9 ++
14  drivers/cpufreq/Makefile          |   1 +
15  drivers/cpufreq/bcm2835-cpufreq.c | 211 ++++++++++++++++++++++++++++++
16  3 files changed, 221 insertions(+)
17  create mode 100644 drivers/cpufreq/bcm2835-cpufreq.c
18
19 --- a/drivers/cpufreq/Kconfig.arm
20 +++ b/drivers/cpufreq/Kconfig.arm
21 @@ -300,6 +300,15 @@ config ARM_TANGO_CPUFREQ
22         depends on CPUFREQ_DT && ARCH_TANGO
23         default y
24  
25 +config ARM_BCM2835_CPUFREQ
26 +       depends on RASPBERRYPI_FIRMWARE
27 +       bool "BCM2835 Driver"
28 +       default y
29 +       help
30 +         This adds the CPUFreq driver for BCM2835
31 +
32 +         If in doubt, say N.
33 +
34  config ARM_TEGRA20_CPUFREQ
35         tristate "Tegra20 CPUFreq support"
36         depends on ARCH_TEGRA
37 --- a/drivers/cpufreq/Makefile
38 +++ b/drivers/cpufreq/Makefile
39 @@ -82,6 +82,7 @@ obj-$(CONFIG_ARM_SPEAR_CPUFREQ)               += spea
40  obj-$(CONFIG_ARM_STI_CPUFREQ)          += sti-cpufreq.o
41  obj-$(CONFIG_ARM_ALLWINNER_SUN50I_CPUFREQ_NVMEM) += sun50i-cpufreq-nvmem.o
42  obj-$(CONFIG_ARM_TANGO_CPUFREQ)                += tango-cpufreq.o
43 +obj-$(CONFIG_ARM_BCM2835_CPUFREQ)      += bcm2835-cpufreq.o
44  obj-$(CONFIG_ARM_TEGRA20_CPUFREQ)      += tegra20-cpufreq.o
45  obj-$(CONFIG_ARM_TEGRA124_CPUFREQ)     += tegra124-cpufreq.o
46  obj-$(CONFIG_ARM_TEGRA186_CPUFREQ)     += tegra186-cpufreq.o
47 --- /dev/null
48 +++ b/drivers/cpufreq/bcm2835-cpufreq.c
49 @@ -0,0 +1,211 @@
50 +/*
51 + * Copyright 2011 Broadcom Corporation.
52 + *
53 + * This program is free software; you can redistribute it and/or
54 + * modify it under the terms of the GNU General Public License
55 + * as published by the Free Software Foundation; version 2
56 + * of the License.
57 + *
58 + * This driver dynamically manages the CPU Frequency of the ARM
59 + * processor. Messages are sent to Videocore either setting or requesting the
60 + * frequency of the ARM in order to match an appropiate frequency to the current
61 + * usage of the processor. The policy which selects the frequency to use is
62 + * defined in the kernel .config file, but can be changed during runtime.
63 + */
64 +
65 +/* ---------- INCLUDES ---------- */
66 +#include <linux/kernel.h>
67 +#include <linux/init.h>
68 +#include <linux/module.h>
69 +#include <linux/cpufreq.h>
70 +#include <soc/bcm2835/raspberrypi-firmware.h>
71 +
72 +/* ---------- DEFINES ---------- */
73 +/*#define CPUFREQ_DEBUG_ENABLE*/               /* enable debugging */
74 +#define MODULE_NAME "bcm2835-cpufreq"
75 +
76 +#define VCMSG_ID_ARM_CLOCK 0x000000003         /* Clock/Voltage ID's */
77 +
78 +/* debug printk macros */
79 +#ifdef CPUFREQ_DEBUG_ENABLE
80 +#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)
81 +#else
82 +#define print_debug(fmt,...)
83 +#endif
84 +#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)
85 +#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)
86 +
87 +/* ---------- GLOBALS ---------- */
88 +static struct cpufreq_driver bcm2835_cpufreq_driver;   /* the cpufreq driver global */
89 +static unsigned int min_frequency, max_frequency;
90 +static struct cpufreq_frequency_table bcm2835_freq_table[3];
91 +
92 +/*
93 + ===============================================
94 +  clk_rate either gets or sets the clock rates.
95 + ===============================================
96 +*/
97 +
98 +static int bcm2835_cpufreq_clock_property(u32 tag, u32 id, u32 *val)
99 +{
100 +       struct rpi_firmware *fw = rpi_firmware_get(NULL);
101 +       struct {
102 +               u32 id;
103 +               u32 val;
104 +       } packet;
105 +       int ret;
106 +
107 +       packet.id = id;
108 +       packet.val = *val;
109 +       ret = rpi_firmware_property(fw, tag, &packet, sizeof(packet));
110 +       if (ret)
111 +               return ret;
112 +
113 +       *val = packet.val;
114 +
115 +       return 0;
116 +}
117 +
118 +static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)
119 +{
120 +       u32 rate = arm_rate * 1000;
121 +       int ret;
122 +
123 +       ret = bcm2835_cpufreq_clock_property(RPI_FIRMWARE_SET_CLOCK_RATE, VCMSG_ID_ARM_CLOCK, &rate);
124 +       if (ret) {
125 +               print_err("Failed to set clock: %d (%d)\n", arm_rate, ret);
126 +               return 0;
127 +       }
128 +
129 +       rate /= 1000;
130 +       print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, rate);
131 +
132 +       return rate;
133 +}
134 +
135 +static uint32_t bcm2835_cpufreq_get_clock(int tag)
136 +{
137 +       u32 rate;
138 +       int ret;
139 +
140 +       ret = bcm2835_cpufreq_clock_property(tag, VCMSG_ID_ARM_CLOCK, &rate);
141 +       if (ret) {
142 +               print_err("Failed to get clock (%d)\n", ret);
143 +               return 0;
144 +       }
145 +
146 +       rate /= 1000;
147 +       print_debug("%s frequency = %u\n",
148 +               tag == RPI_FIRMWARE_GET_CLOCK_RATE ? "Current":
149 +               tag == RPI_FIRMWARE_GET_MIN_CLOCK_RATE ? "Min":
150 +               tag == RPI_FIRMWARE_GET_MAX_CLOCK_RATE ? "Max":
151 +               "Unexpected", rate);
152 +
153 +       return rate;
154 +}
155 +
156 +/*
157 + ====================================================
158 +  Module Initialisation registers the cpufreq driver
159 + ====================================================
160 +*/
161 +static int __init bcm2835_cpufreq_module_init(void)
162 +{
163 +       print_debug("IN\n");
164 +       return cpufreq_register_driver(&bcm2835_cpufreq_driver);
165 +}
166 +
167 +/*
168 + =============
169 +  Module exit
170 + =============
171 +*/
172 +static void __exit bcm2835_cpufreq_module_exit(void)
173 +{
174 +       print_debug("IN\n");
175 +       cpufreq_unregister_driver(&bcm2835_cpufreq_driver);
176 +       return;
177 +}
178 +
179 +/*
180 + ==============================================================
181 +  Initialisation function sets up the CPU policy for first use
182 + ==============================================================
183 +*/
184 +static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)
185 +{
186 +       /* measured value of how long it takes to change frequency */
187 +       const unsigned int transition_latency = 355000; /* ns */
188 +
189 +       if (!rpi_firmware_get(NULL)) {
190 +               print_err("Firmware is not available\n");
191 +               return -ENODEV;
192 +       }
193 +
194 +       /* now find out what the maximum and minimum frequencies are */
195 +       min_frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MIN_CLOCK_RATE);
196 +       max_frequency = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_MAX_CLOCK_RATE);
197 +
198 +       if (min_frequency == max_frequency) {
199 +               bcm2835_freq_table[0].frequency = min_frequency;
200 +               bcm2835_freq_table[1].frequency = CPUFREQ_TABLE_END;
201 +       } else {
202 +               bcm2835_freq_table[0].frequency = min_frequency;
203 +               bcm2835_freq_table[1].frequency = max_frequency;
204 +               bcm2835_freq_table[2].frequency = CPUFREQ_TABLE_END;
205 +       }
206 +
207 +       print_info("min=%d max=%d\n", min_frequency, max_frequency);
208 +       cpufreq_generic_init(policy, bcm2835_freq_table, transition_latency);
209 +       return 0;
210 +}
211 +
212 +/*
213 + =====================================================================
214 +  Target index function chooses the requested frequency from the table
215 + =====================================================================
216 +*/
217 +
218 +static int bcm2835_cpufreq_driver_target_index(struct cpufreq_policy *policy, unsigned int state)
219 +{
220 +       unsigned int target_freq = state == 0 ? min_frequency : max_frequency;
221 +       unsigned int cur = bcm2835_cpufreq_set_clock(policy->cur, target_freq);
222 +
223 +       if (!cur)
224 +       {
225 +               print_err("Error occurred setting a new frequency (%d)\n", target_freq);
226 +               return -EINVAL;
227 +       }
228 +       print_debug("%s: %i: freq %d->%d\n", policy->governor->name, state, policy->cur, cur);
229 +       return 0;
230 +}
231 +
232 +/*
233 + ======================================================
234 +  Get function returns the current frequency from table
235 + ======================================================
236 +*/
237 +
238 +static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)
239 +{
240 +       unsigned int actual_rate = bcm2835_cpufreq_get_clock(RPI_FIRMWARE_GET_CLOCK_RATE);
241 +       print_debug("cpu%d: freq=%d\n", cpu, actual_rate);
242 +       return actual_rate <= min_frequency ? min_frequency : max_frequency;
243 +}
244 +
245 +/* the CPUFreq driver */
246 +static struct cpufreq_driver bcm2835_cpufreq_driver = {
247 +       .name         = "BCM2835 CPUFreq",
248 +       .init         = bcm2835_cpufreq_driver_init,
249 +       .verify       = cpufreq_generic_frequency_table_verify,
250 +       .target_index = bcm2835_cpufreq_driver_target_index,
251 +       .get          = bcm2835_cpufreq_driver_get,
252 +       .attr         = cpufreq_generic_attr,
253 +};
254 +
255 +MODULE_AUTHOR("Dorian Peake and Dom Cobley");
256 +MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");
257 +MODULE_LICENSE("GPL");
258 +
259 +module_init(bcm2835_cpufreq_module_init);
260 +module_exit(bcm2835_cpufreq_module_exit);