1 // SPDX-License-Identifier: GPL-2.0+
3 * Raspberry Pi driver for firmware controlled clocks
5 * Even though clk-bcm2835 provides an interface to the hardware registers for
6 * the system clocks we've had to factor out 'pllb' as the firmware 'owns' it.
7 * We're not allowed to change it directly as we might race with the
8 * over-temperature and under-voltage protections provided by the firmware.
10 * Copyright (C) 2019 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
13 #include <linux/clkdev.h>
14 #include <linux/clk-provider.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
19 #include <soc/bcm2835/raspberrypi-firmware.h>
21 #define RPI_FIRMWARE_ARM_CLK_ID 0x00000003
23 #define RPI_FIRMWARE_STATE_ENABLE_BIT BIT(0)
24 #define RPI_FIRMWARE_STATE_WAIT_BIT BIT(1)
27 * Even though the firmware interface alters 'pllb' the frequencies are
28 * provided as per 'pllb_arm'. We need to scale before passing them trough.
30 #define RPI_FIRMWARE_PLLB_ARM_DIV_RATE 2
32 #define A2W_PLL_FRAC_BITS 20
34 struct raspberrypi_clk {
36 struct rpi_firmware *firmware;
37 struct platform_device *cpufreq;
40 struct raspberrypi_clk_data {
44 unsigned long min_rate;
45 unsigned long max_rate;
47 struct raspberrypi_clk *rpi;
51 * Structure of the message passed to Raspberry Pi's firmware in order to
52 * change clock rates. The 'disable_turbo' option is only available to the ARM
53 * clock (pllb) which we enable by default as turbo mode will alter multiple
56 * Even though we're able to access the clock registers directly we're bound to
57 * use the firmware interface as the firmware ultimately takes care of
58 * mitigating overheating/undervoltage situations and we would be changing
59 * frequencies behind his back.
61 * For more information on the firmware interface check:
62 * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
64 struct raspberrypi_firmware_prop {
70 static int raspberrypi_clock_property(struct rpi_firmware *firmware, u32 tag,
73 struct raspberrypi_firmware_prop msg = {
74 .id = cpu_to_le32(clk),
75 .val = cpu_to_le32(*val),
76 .disable_turbo = cpu_to_le32(1),
80 ret = rpi_firmware_property(firmware, tag, &msg, sizeof(msg));
84 *val = le32_to_cpu(msg.val);
89 static int raspberrypi_fw_pll_is_on(struct clk_hw *hw)
91 struct raspberrypi_clk_data *data =
92 container_of(hw, struct raspberrypi_clk_data, hw);
93 struct raspberrypi_clk *rpi = data->rpi;
97 ret = raspberrypi_clock_property(rpi->firmware,
98 RPI_FIRMWARE_GET_CLOCK_STATE,
103 return !!(val & RPI_FIRMWARE_STATE_ENABLE_BIT);
107 static unsigned long raspberrypi_fw_pll_get_rate(struct clk_hw *hw,
108 unsigned long parent_rate)
110 struct raspberrypi_clk_data *data =
111 container_of(hw, struct raspberrypi_clk_data, hw);
112 struct raspberrypi_clk *rpi = data->rpi;
116 ret = raspberrypi_clock_property(rpi->firmware,
117 RPI_FIRMWARE_GET_CLOCK_RATE,
122 return val * RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
125 static int raspberrypi_fw_pll_set_rate(struct clk_hw *hw, unsigned long rate,
126 unsigned long parent_rate)
128 struct raspberrypi_clk_data *data =
129 container_of(hw, struct raspberrypi_clk_data, hw);
130 struct raspberrypi_clk *rpi = data->rpi;
131 u32 new_rate = rate / RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
134 ret = raspberrypi_clock_property(rpi->firmware,
135 RPI_FIRMWARE_SET_CLOCK_RATE,
136 data->id, &new_rate);
138 dev_err_ratelimited(rpi->dev, "Failed to change %s frequency: %d",
139 clk_hw_get_name(hw), ret);
145 * Sadly there is no firmware rate rounding interface. We borrowed it from
148 static int raspberrypi_pll_determine_rate(struct clk_hw *hw,
149 struct clk_rate_request *req)
151 struct raspberrypi_clk_data *data =
152 container_of(hw, struct raspberrypi_clk_data, hw);
156 /* We can't use req->rate directly as it would overflow */
157 final_rate = clamp(req->rate, data->min_rate, data->max_rate);
159 div = (u64)final_rate << A2W_PLL_FRAC_BITS;
160 do_div(div, req->best_parent_rate);
162 ndiv = div >> A2W_PLL_FRAC_BITS;
163 fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
165 final_rate = ((u64)req->best_parent_rate *
166 ((ndiv << A2W_PLL_FRAC_BITS) + fdiv));
168 req->rate = final_rate >> A2W_PLL_FRAC_BITS;
173 static const struct clk_ops raspberrypi_firmware_pll_clk_ops = {
174 .is_prepared = raspberrypi_fw_pll_is_on,
175 .recalc_rate = raspberrypi_fw_pll_get_rate,
176 .set_rate = raspberrypi_fw_pll_set_rate,
177 .determine_rate = raspberrypi_pll_determine_rate,
180 static int raspberrypi_register_pllb(struct raspberrypi_clk *rpi)
182 struct raspberrypi_clk_data *data;
183 struct clk_init_data init = {};
184 u32 min_rate = 0, max_rate = 0;
187 data = devm_kzalloc(rpi->dev, sizeof(*data), GFP_KERNEL);
191 data->id = RPI_FIRMWARE_ARM_CLK_ID;
193 /* All of the PLLs derive from the external oscillator. */
194 init.parent_names = (const char *[]){ "osc" };
195 init.num_parents = 1;
197 init.ops = &raspberrypi_firmware_pll_clk_ops;
198 init.flags = CLK_GET_RATE_NOCACHE | CLK_IGNORE_UNUSED;
200 /* Get min & max rates set by the firmware */
201 ret = raspberrypi_clock_property(rpi->firmware,
202 RPI_FIRMWARE_GET_MIN_CLOCK_RATE,
203 data->id, &min_rate);
205 dev_err(rpi->dev, "Failed to get %s min freq: %d\n",
210 ret = raspberrypi_clock_property(rpi->firmware,
211 RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
212 data->id, &max_rate);
214 dev_err(rpi->dev, "Failed to get %s max freq: %d\n",
219 if (!min_rate || !max_rate) {
220 dev_err(rpi->dev, "Unexpected frequency range: min %u, max %u\n",
225 dev_info(rpi->dev, "CPU frequency range: min %u, max %u\n",
228 data->min_rate = min_rate * RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
229 data->max_rate = max_rate * RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
231 data->hw.init = &init;
233 return devm_clk_hw_register(rpi->dev, &data->hw);
236 static struct clk_fixed_factor raspberrypi_clk_pllb_arm = {
239 .hw.init = &(struct clk_init_data) {
241 .parent_names = (const char *[]){ "pllb" },
243 .ops = &clk_fixed_factor_ops,
244 .flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE,
248 static int raspberrypi_register_pllb_arm(struct raspberrypi_clk *rpi)
252 ret = devm_clk_hw_register(rpi->dev, &raspberrypi_clk_pllb_arm.hw);
254 dev_err(rpi->dev, "Failed to initialize pllb_arm\n");
258 ret = devm_clk_hw_register_clkdev(rpi->dev,
259 &raspberrypi_clk_pllb_arm.hw,
262 dev_err(rpi->dev, "Failed to initialize clkdev\n");
269 static int raspberrypi_clk_probe(struct platform_device *pdev)
271 struct device_node *firmware_node;
272 struct device *dev = &pdev->dev;
273 struct rpi_firmware *firmware;
274 struct raspberrypi_clk *rpi;
277 firmware_node = of_parse_phandle(dev->of_node, "raspberrypi,firmware", 0);
278 if (!firmware_node) {
279 dev_err(dev, "Missing firmware node\n");
283 firmware = rpi_firmware_get(firmware_node);
285 return -EPROBE_DEFER;
287 rpi = devm_kzalloc(dev, sizeof(*rpi), GFP_KERNEL);
292 rpi->firmware = firmware;
293 platform_set_drvdata(pdev, rpi);
295 ret = raspberrypi_register_pllb(rpi);
297 dev_err(dev, "Failed to initialize pllb, %d\n", ret);
301 ret = raspberrypi_register_pllb_arm(rpi);
305 rpi->cpufreq = platform_device_register_data(dev, "raspberrypi-cpufreq",
311 static int raspberrypi_clk_remove(struct platform_device *pdev)
313 struct raspberrypi_clk *rpi = platform_get_drvdata(pdev);
315 platform_device_unregister(rpi->cpufreq);
320 static const struct of_device_id raspberrypi_clk_match[] = {
321 { .compatible = "raspberrypi,firmware-clocks" },
324 MODULE_DEVICE_TABLE(of, raspberrypi_clk_match);
326 static struct platform_driver raspberrypi_clk_driver = {
328 .name = "raspberrypi-clk",
329 .of_match_table = raspberrypi_clk_match,
331 .probe = raspberrypi_clk_probe,
332 .remove = raspberrypi_clk_remove,
334 module_platform_driver(raspberrypi_clk_driver);
336 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
337 MODULE_DESCRIPTION("Raspberry Pi firmware clock driver");
338 MODULE_LICENSE("GPL");
339 MODULE_ALIAS("platform:raspberrypi-clk");