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 int raspberrypi_clk_probe(struct platform_device *pdev)
290 struct clk_hw_onecell_data *clk_data;
291 struct device_node *firmware_node;
292 struct device *dev = &pdev->dev;
293 struct rpi_firmware *firmware;
294 struct raspberrypi_clk *rpi;
298 firmware_node = of_parse_phandle(dev->of_node, "raspberrypi,firmware", 0);
299 if (!firmware_node) {
300 dev_err(dev, "Missing firmware node\n");
304 firmware = rpi_firmware_get(firmware_node);
306 return -EPROBE_DEFER;
308 rpi = devm_kzalloc(dev, sizeof(*rpi), GFP_KERNEL);
313 rpi->firmware = firmware;
314 platform_set_drvdata(pdev, rpi);
316 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws, NUM_FW_CLKS),
321 hw = raspberrypi_register_pllb(rpi);
323 dev_err(dev, "Failed to initialize pllb, %ld\n", PTR_ERR(hw));
327 hw = raspberrypi_register_pllb_arm(rpi);
330 clk_data->hws[RPI_FIRMWARE_ARM_CLK_ID] = hw;
331 clk_data->num = RPI_FIRMWARE_ARM_CLK_ID + 1;
333 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
338 rpi->cpufreq = platform_device_register_data(dev, "raspberrypi-cpufreq",
344 static int raspberrypi_clk_remove(struct platform_device *pdev)
346 struct raspberrypi_clk *rpi = platform_get_drvdata(pdev);
348 platform_device_unregister(rpi->cpufreq);
353 static const struct of_device_id raspberrypi_clk_match[] = {
354 { .compatible = "raspberrypi,firmware-clocks" },
357 MODULE_DEVICE_TABLE(of, raspberrypi_clk_match);
359 static struct platform_driver raspberrypi_clk_driver = {
361 .name = "raspberrypi-clk",
362 .of_match_table = raspberrypi_clk_match,
364 .probe = raspberrypi_clk_probe,
365 .remove = raspberrypi_clk_remove,
367 module_platform_driver(raspberrypi_clk_driver);
369 MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
370 MODULE_DESCRIPTION("Raspberry Pi firmware clock driver");
371 MODULE_LICENSE("GPL");
372 MODULE_ALIAS("platform:raspberrypi-clk");