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 #define NUM_FW_CLKS 16
36 struct raspberrypi_clk {
38 struct rpi_firmware *firmware;
39 struct platform_device *cpufreq;
42 struct raspberrypi_clk_data {
46 unsigned long min_rate;
47 unsigned long max_rate;
49 struct raspberrypi_clk *rpi;
53 * Structure of the message passed to Raspberry Pi's firmware in order to
54 * change clock rates. The 'disable_turbo' option is only available to the ARM
55 * clock (pllb) which we enable by default as turbo mode will alter multiple
58 * Even though we're able to access the clock registers directly we're bound to
59 * use the firmware interface as the firmware ultimately takes care of
60 * mitigating overheating/undervoltage situations and we would be changing
61 * frequencies behind his back.
63 * For more information on the firmware interface check:
64 * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
66 struct raspberrypi_firmware_prop {
72 static int raspberrypi_clock_property(struct rpi_firmware *firmware,
73 const struct raspberrypi_clk_data *data,
76 struct raspberrypi_firmware_prop msg = {
77 .id = cpu_to_le32(data->id),
78 .val = cpu_to_le32(*val),
79 .disable_turbo = cpu_to_le32(1),
83 ret = rpi_firmware_property(firmware, tag, &msg, sizeof(msg));
87 *val = le32_to_cpu(msg.val);
92 static int raspberrypi_fw_is_prepared(struct clk_hw *hw)
94 struct raspberrypi_clk_data *data =
95 container_of(hw, struct raspberrypi_clk_data, hw);
96 struct raspberrypi_clk *rpi = data->rpi;
100 ret = raspberrypi_clock_property(rpi->firmware, data,
101 RPI_FIRMWARE_GET_CLOCK_STATE, &val);
105 return !!(val & RPI_FIRMWARE_STATE_ENABLE_BIT);
109 static unsigned long raspberrypi_fw_get_rate(struct clk_hw *hw,
110 unsigned long parent_rate)
112 struct raspberrypi_clk_data *data =
113 container_of(hw, struct raspberrypi_clk_data, hw);
114 struct raspberrypi_clk *rpi = data->rpi;
118 ret = raspberrypi_clock_property(rpi->firmware, data,
119 RPI_FIRMWARE_GET_CLOCK_RATE, &val);
126 static unsigned long raspberrypi_fw_pll_get_rate(struct clk_hw *hw,
127 unsigned long parent_rate)
129 return raspberrypi_fw_get_rate(hw, parent_rate) *
130 RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
133 static int raspberrypi_fw_set_rate(struct clk_hw *hw, unsigned long rate,
134 unsigned long parent_rate)
136 struct raspberrypi_clk_data *data =
137 container_of(hw, struct raspberrypi_clk_data, hw);
138 struct raspberrypi_clk *rpi = data->rpi;
142 ret = raspberrypi_clock_property(rpi->firmware, data,
143 RPI_FIRMWARE_SET_CLOCK_RATE, &_rate);
145 dev_err_ratelimited(rpi->dev, "Failed to change %s frequency: %d",
146 clk_hw_get_name(hw), ret);
151 static int raspberrypi_fw_pll_set_rate(struct clk_hw *hw, unsigned long rate,
152 unsigned long parent_rate)
154 u32 new_rate = rate / RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
156 return raspberrypi_fw_set_rate(hw, new_rate, parent_rate);
160 * Sadly there is no firmware rate rounding interface. We borrowed it from
163 static int raspberrypi_pll_determine_rate(struct clk_hw *hw,
164 struct clk_rate_request *req)
166 struct raspberrypi_clk_data *data =
167 container_of(hw, struct raspberrypi_clk_data, hw);
171 /* We can't use req->rate directly as it would overflow */
172 final_rate = clamp(req->rate, data->min_rate, data->max_rate);
174 div = (u64)final_rate << A2W_PLL_FRAC_BITS;
175 do_div(div, req->best_parent_rate);
177 ndiv = div >> A2W_PLL_FRAC_BITS;
178 fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
180 final_rate = ((u64)req->best_parent_rate *
181 ((ndiv << A2W_PLL_FRAC_BITS) + fdiv));
183 req->rate = final_rate >> A2W_PLL_FRAC_BITS;
188 static const struct clk_ops raspberrypi_firmware_pll_clk_ops = {
189 .is_prepared = raspberrypi_fw_is_prepared,
190 .recalc_rate = raspberrypi_fw_pll_get_rate,
191 .set_rate = raspberrypi_fw_pll_set_rate,
192 .determine_rate = raspberrypi_pll_determine_rate,
195 static struct clk_hw *raspberrypi_register_pllb(struct raspberrypi_clk *rpi)
197 struct raspberrypi_clk_data *data;
198 struct clk_init_data init = {};
199 u32 min_rate = 0, max_rate = 0;
202 data = devm_kzalloc(rpi->dev, sizeof(*data), GFP_KERNEL);
204 return ERR_PTR(-ENOMEM);
206 data->id = RPI_FIRMWARE_ARM_CLK_ID;
208 /* All of the PLLs derive from the external oscillator. */
209 init.parent_names = (const char *[]){ "osc" };
210 init.num_parents = 1;
212 init.ops = &raspberrypi_firmware_pll_clk_ops;
213 init.flags = CLK_GET_RATE_NOCACHE | CLK_IGNORE_UNUSED;
215 /* Get min & max rates set by the firmware */
216 ret = raspberrypi_clock_property(rpi->firmware, data,
217 RPI_FIRMWARE_GET_MIN_CLOCK_RATE,
220 dev_err(rpi->dev, "Failed to get %s min freq: %d\n",
225 ret = raspberrypi_clock_property(rpi->firmware, data,
226 RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
229 dev_err(rpi->dev, "Failed to get %s max freq: %d\n",
234 if (!min_rate || !max_rate) {
235 dev_err(rpi->dev, "Unexpected frequency range: min %u, max %u\n",
237 return ERR_PTR(-EINVAL);
240 dev_info(rpi->dev, "CPU frequency range: min %u, max %u\n",
243 data->min_rate = min_rate * RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
244 data->max_rate = max_rate * RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
246 data->hw.init = &init;
248 ret = devm_clk_hw_register(rpi->dev, &data->hw);
255 static struct clk_fixed_factor raspberrypi_clk_pllb_arm = {
258 .hw.init = &(struct clk_init_data) {
260 .parent_names = (const char *[]){ "pllb" },
262 .ops = &clk_fixed_factor_ops,
263 .flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE,
267 static struct clk_hw *raspberrypi_register_pllb_arm(struct raspberrypi_clk *rpi)
271 ret = devm_clk_hw_register(rpi->dev, &raspberrypi_clk_pllb_arm.hw);
273 dev_err(rpi->dev, "Failed to initialize pllb_arm\n");
277 ret = devm_clk_hw_register_clkdev(rpi->dev,
278 &raspberrypi_clk_pllb_arm.hw,
281 dev_err(rpi->dev, "Failed to initialize clkdev\n");
285 return &raspberrypi_clk_pllb_arm.hw;
288 static long raspberrypi_fw_dumb_round_rate(struct clk_hw *hw,
290 unsigned long *parent_rate)
293 * The firmware will do the rounding but that isn't part of
294 * the interface with the firmware, so we just do our best
300 static const struct clk_ops raspberrypi_firmware_clk_ops = {
301 .is_prepared = raspberrypi_fw_is_prepared,
302 .recalc_rate = raspberrypi_fw_get_rate,
303 .round_rate = raspberrypi_fw_dumb_round_rate,
304 .set_rate = raspberrypi_fw_set_rate,
307 static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
311 struct raspberrypi_clk_data *data;
312 struct clk_init_data init = {};
315 if (id == RPI_FIRMWARE_ARM_CLK_ID) {
318 hw = raspberrypi_register_pllb(rpi);
320 dev_err(rpi->dev, "Failed to initialize pllb, %ld\n",
325 return raspberrypi_register_pllb_arm(rpi);
328 data = devm_kzalloc(rpi->dev, sizeof(*data), GFP_KERNEL);
330 return ERR_PTR(-ENOMEM);
334 init.name = devm_kasprintf(rpi->dev, GFP_KERNEL, "fw-clk-%u", id);
335 init.ops = &raspberrypi_firmware_clk_ops;
336 init.flags = CLK_GET_RATE_NOCACHE;
338 data->hw.init = &init;
340 ret = devm_clk_hw_register(rpi->dev, &data->hw);
347 static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
348 struct clk_hw_onecell_data *data)
350 struct rpi_firmware_get_clocks_response *clks;
353 clks = devm_kcalloc(rpi->dev, sizeof(*clks), NUM_FW_CLKS, GFP_KERNEL);
357 ret = rpi_firmware_property(rpi->firmware, RPI_FIRMWARE_GET_CLOCKS,
358 clks, sizeof(*clks) * NUM_FW_CLKS);
365 hw = raspberrypi_clk_register(rpi, clks->parent, clks->id);
369 data->hws[clks->id] = hw;
370 data->num = clks->id + 1;
377 static int raspberrypi_clk_probe(struct platform_device *pdev)
379 struct clk_hw_onecell_data *clk_data;
380 struct device_node *firmware_node;
381 struct device *dev = &pdev->dev;
382 struct rpi_firmware *firmware;
383 struct raspberrypi_clk *rpi;
386 firmware_node = of_parse_phandle(dev->of_node, "raspberrypi,firmware", 0);
387 if (!firmware_node) {
388 dev_err(dev, "Missing firmware node\n");
392 firmware = rpi_firmware_get(firmware_node);
394 return -EPROBE_DEFER;
396 rpi = devm_kzalloc(dev, sizeof(*rpi), GFP_KERNEL);
401 rpi->firmware = firmware;
402 platform_set_drvdata(pdev, rpi);
404 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, NUM_FW_CLKS),
409 ret = raspberrypi_discover_clocks(rpi, clk_data);
413 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
418 rpi->cpufreq = platform_device_register_data(dev, "raspberrypi-cpufreq",
424 static int raspberrypi_clk_remove(struct platform_device *pdev)
426 struct raspberrypi_clk *rpi = platform_get_drvdata(pdev);
428 platform_device_unregister(rpi->cpufreq);
433 static const struct of_device_id raspberrypi_clk_match[] = {
434 { .compatible = "raspberrypi,firmware-clocks" },
437 MODULE_DEVICE_TABLE(of, raspberrypi_clk_match);
439 static struct platform_driver raspberrypi_clk_driver = {
441 .name = "raspberrypi-clk",
442 .of_match_table = raspberrypi_clk_match,
444 .probe = raspberrypi_clk_probe,
445 .remove = raspberrypi_clk_remove,
447 module_platform_driver(raspberrypi_clk_driver);
449 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
450 MODULE_DESCRIPTION("Raspberry Pi firmware clock driver");
451 MODULE_LICENSE("GPL");
452 MODULE_ALIAS("platform:raspberrypi-clk");