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.
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;
39 unsigned long min_rate;
40 unsigned long max_rate;
43 struct clk_hw *pllb_arm;
44 struct clk_lookup *pllb_arm_lookup;
48 * Structure of the message passed to Raspberry Pi's firmware in order to
49 * change clock rates. The 'disable_turbo' option is only available to the ARM
50 * clock (pllb) which we enable by default as turbo mode will alter multiple
53 * Even though we're able to access the clock registers directly we're bound to
54 * use the firmware interface as the firmware ultimately takes care of
55 * mitigating overheating/undervoltage situations and we would be changing
56 * frequencies behind his back.
58 * For more information on the firmware interface check:
59 * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
61 struct raspberrypi_firmware_prop {
67 static int raspberrypi_clock_property(struct rpi_firmware *firmware, u32 tag,
70 struct raspberrypi_firmware_prop msg = {
71 .id = cpu_to_le32(clk),
72 .val = cpu_to_le32(*val),
73 .disable_turbo = cpu_to_le32(1),
77 ret = rpi_firmware_property(firmware, tag, &msg, sizeof(msg));
81 *val = le32_to_cpu(msg.val);
86 static int raspberrypi_fw_pll_is_on(struct clk_hw *hw)
88 struct raspberrypi_clk *rpi = container_of(hw, struct raspberrypi_clk,
93 ret = raspberrypi_clock_property(rpi->firmware,
94 RPI_FIRMWARE_GET_CLOCK_STATE,
95 RPI_FIRMWARE_ARM_CLK_ID, &val);
99 return !!(val & RPI_FIRMWARE_STATE_ENABLE_BIT);
103 static unsigned long raspberrypi_fw_pll_get_rate(struct clk_hw *hw,
104 unsigned long parent_rate)
106 struct raspberrypi_clk *rpi = container_of(hw, struct raspberrypi_clk,
111 ret = raspberrypi_clock_property(rpi->firmware,
112 RPI_FIRMWARE_GET_CLOCK_RATE,
113 RPI_FIRMWARE_ARM_CLK_ID,
118 return val * RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
121 static int raspberrypi_fw_pll_set_rate(struct clk_hw *hw, unsigned long rate,
122 unsigned long parent_rate)
124 struct raspberrypi_clk *rpi = container_of(hw, struct raspberrypi_clk,
126 u32 new_rate = rate / RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
129 ret = raspberrypi_clock_property(rpi->firmware,
130 RPI_FIRMWARE_SET_CLOCK_RATE,
131 RPI_FIRMWARE_ARM_CLK_ID,
134 dev_err_ratelimited(rpi->dev, "Failed to change %s frequency: %d",
135 clk_hw_get_name(hw), ret);
141 * Sadly there is no firmware rate rounding interface. We borrowed it from
144 static int raspberrypi_pll_determine_rate(struct clk_hw *hw,
145 struct clk_rate_request *req)
147 struct raspberrypi_clk *rpi = container_of(hw, struct raspberrypi_clk,
152 /* We can't use req->rate directly as it would overflow */
153 final_rate = clamp(req->rate, rpi->min_rate, rpi->max_rate);
155 div = (u64)final_rate << A2W_PLL_FRAC_BITS;
156 do_div(div, req->best_parent_rate);
158 ndiv = div >> A2W_PLL_FRAC_BITS;
159 fdiv = div & ((1 << A2W_PLL_FRAC_BITS) - 1);
161 final_rate = ((u64)req->best_parent_rate *
162 ((ndiv << A2W_PLL_FRAC_BITS) + fdiv));
164 req->rate = final_rate >> A2W_PLL_FRAC_BITS;
169 static const struct clk_ops raspberrypi_firmware_pll_clk_ops = {
170 .is_prepared = raspberrypi_fw_pll_is_on,
171 .recalc_rate = raspberrypi_fw_pll_get_rate,
172 .set_rate = raspberrypi_fw_pll_set_rate,
173 .determine_rate = raspberrypi_pll_determine_rate,
176 static int raspberrypi_register_pllb(struct raspberrypi_clk *rpi)
178 u32 min_rate = 0, max_rate = 0;
179 struct clk_init_data init;
182 memset(&init, 0, sizeof(init));
184 /* All of the PLLs derive from the external oscillator. */
185 init.parent_names = (const char *[]){ "osc" };
186 init.num_parents = 1;
188 init.ops = &raspberrypi_firmware_pll_clk_ops;
189 init.flags = CLK_GET_RATE_NOCACHE | CLK_IGNORE_UNUSED;
191 /* Get min & max rates set by the firmware */
192 ret = raspberrypi_clock_property(rpi->firmware,
193 RPI_FIRMWARE_GET_MIN_CLOCK_RATE,
194 RPI_FIRMWARE_ARM_CLK_ID,
197 dev_err(rpi->dev, "Failed to get %s min freq: %d\n",
202 ret = raspberrypi_clock_property(rpi->firmware,
203 RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
204 RPI_FIRMWARE_ARM_CLK_ID,
207 dev_err(rpi->dev, "Failed to get %s max freq: %d\n",
212 if (!min_rate || !max_rate) {
213 dev_err(rpi->dev, "Unexpected frequency range: min %u, max %u\n",
218 dev_info(rpi->dev, "CPU frequency range: min %u, max %u\n",
221 rpi->min_rate = min_rate * RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
222 rpi->max_rate = max_rate * RPI_FIRMWARE_PLLB_ARM_DIV_RATE;
224 rpi->pllb.init = &init;
226 return devm_clk_hw_register(rpi->dev, &rpi->pllb);
229 static int raspberrypi_register_pllb_arm(struct raspberrypi_clk *rpi)
231 rpi->pllb_arm = clk_hw_register_fixed_factor(rpi->dev,
233 CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE,
235 if (IS_ERR(rpi->pllb_arm)) {
236 dev_err(rpi->dev, "Failed to initialize pllb_arm\n");
237 return PTR_ERR(rpi->pllb_arm);
240 rpi->pllb_arm_lookup = clkdev_hw_create(rpi->pllb_arm, NULL, "cpu0");
241 if (!rpi->pllb_arm_lookup) {
242 dev_err(rpi->dev, "Failed to initialize pllb_arm_lookup\n");
243 clk_hw_unregister_fixed_factor(rpi->pllb_arm);
250 static int raspberrypi_clk_probe(struct platform_device *pdev)
252 struct device_node *firmware_node;
253 struct device *dev = &pdev->dev;
254 struct rpi_firmware *firmware;
255 struct raspberrypi_clk *rpi;
258 firmware_node = of_find_compatible_node(NULL, NULL,
259 "raspberrypi,bcm2835-firmware");
260 if (!firmware_node) {
261 dev_err(dev, "Missing firmware node\n");
265 firmware = rpi_firmware_get(firmware_node);
266 of_node_put(firmware_node);
268 return -EPROBE_DEFER;
270 rpi = devm_kzalloc(dev, sizeof(*rpi), GFP_KERNEL);
275 rpi->firmware = firmware;
276 platform_set_drvdata(pdev, rpi);
278 ret = raspberrypi_register_pllb(rpi);
280 dev_err(dev, "Failed to initialize pllb, %d\n", ret);
284 ret = raspberrypi_register_pllb_arm(rpi);
288 rpi->cpufreq = platform_device_register_data(dev, "raspberrypi-cpufreq",
294 static int raspberrypi_clk_remove(struct platform_device *pdev)
296 struct raspberrypi_clk *rpi = platform_get_drvdata(pdev);
298 platform_device_unregister(rpi->cpufreq);
303 static struct platform_driver raspberrypi_clk_driver = {
305 .name = "raspberrypi-clk",
307 .probe = raspberrypi_clk_probe,
308 .remove = raspberrypi_clk_remove,
310 module_platform_driver(raspberrypi_clk_driver);
313 MODULE_DESCRIPTION("Raspberry Pi firmware clock driver");
314 MODULE_LICENSE("GPL");
315 MODULE_ALIAS("platform:raspberrypi-clk");