blob: f89b9cfc43099992b920940bb633ca2fb270fe3c [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Raspberry Pi driver for firmware controlled clocks
4 *
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.
9 *
10 * Copyright (C) 2019 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
11 */
12
13#include <linux/clkdev.h>
14#include <linux/clk-provider.h>
15#include <linux/io.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18
19#include <soc/bcm2835/raspberrypi-firmware.h>
20
Olivier Deprez157378f2022-04-04 15:47:50 +020021enum rpi_firmware_clk_id {
22 RPI_FIRMWARE_EMMC_CLK_ID = 1,
23 RPI_FIRMWARE_UART_CLK_ID,
24 RPI_FIRMWARE_ARM_CLK_ID,
25 RPI_FIRMWARE_CORE_CLK_ID,
26 RPI_FIRMWARE_V3D_CLK_ID,
27 RPI_FIRMWARE_H264_CLK_ID,
28 RPI_FIRMWARE_ISP_CLK_ID,
29 RPI_FIRMWARE_SDRAM_CLK_ID,
30 RPI_FIRMWARE_PIXEL_CLK_ID,
31 RPI_FIRMWARE_PWM_CLK_ID,
32 RPI_FIRMWARE_HEVC_CLK_ID,
33 RPI_FIRMWARE_EMMC2_CLK_ID,
34 RPI_FIRMWARE_M2MC_CLK_ID,
35 RPI_FIRMWARE_PIXEL_BVB_CLK_ID,
36 RPI_FIRMWARE_NUM_CLK_ID,
37};
38
39static char *rpi_firmware_clk_names[] = {
40 [RPI_FIRMWARE_EMMC_CLK_ID] = "emmc",
41 [RPI_FIRMWARE_UART_CLK_ID] = "uart",
42 [RPI_FIRMWARE_ARM_CLK_ID] = "arm",
43 [RPI_FIRMWARE_CORE_CLK_ID] = "core",
44 [RPI_FIRMWARE_V3D_CLK_ID] = "v3d",
45 [RPI_FIRMWARE_H264_CLK_ID] = "h264",
46 [RPI_FIRMWARE_ISP_CLK_ID] = "isp",
47 [RPI_FIRMWARE_SDRAM_CLK_ID] = "sdram",
48 [RPI_FIRMWARE_PIXEL_CLK_ID] = "pixel",
49 [RPI_FIRMWARE_PWM_CLK_ID] = "pwm",
50 [RPI_FIRMWARE_HEVC_CLK_ID] = "hevc",
51 [RPI_FIRMWARE_EMMC2_CLK_ID] = "emmc2",
52 [RPI_FIRMWARE_M2MC_CLK_ID] = "m2mc",
53 [RPI_FIRMWARE_PIXEL_BVB_CLK_ID] = "pixel-bvb",
54};
David Brazdil0f672f62019-12-10 10:32:29 +000055
56#define RPI_FIRMWARE_STATE_ENABLE_BIT BIT(0)
57#define RPI_FIRMWARE_STATE_WAIT_BIT BIT(1)
58
David Brazdil0f672f62019-12-10 10:32:29 +000059struct raspberrypi_clk {
60 struct device *dev;
61 struct rpi_firmware *firmware;
62 struct platform_device *cpufreq;
Olivier Deprez157378f2022-04-04 15:47:50 +020063};
David Brazdil0f672f62019-12-10 10:32:29 +000064
Olivier Deprez157378f2022-04-04 15:47:50 +020065struct raspberrypi_clk_data {
66 struct clk_hw hw;
David Brazdil0f672f62019-12-10 10:32:29 +000067
Olivier Deprez157378f2022-04-04 15:47:50 +020068 unsigned int id;
69
70 struct raspberrypi_clk *rpi;
David Brazdil0f672f62019-12-10 10:32:29 +000071};
72
73/*
74 * Structure of the message passed to Raspberry Pi's firmware in order to
75 * change clock rates. The 'disable_turbo' option is only available to the ARM
76 * clock (pllb) which we enable by default as turbo mode will alter multiple
77 * clocks at once.
78 *
79 * Even though we're able to access the clock registers directly we're bound to
80 * use the firmware interface as the firmware ultimately takes care of
81 * mitigating overheating/undervoltage situations and we would be changing
82 * frequencies behind his back.
83 *
84 * For more information on the firmware interface check:
85 * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
86 */
87struct raspberrypi_firmware_prop {
88 __le32 id;
89 __le32 val;
90 __le32 disable_turbo;
91} __packed;
92
Olivier Deprez157378f2022-04-04 15:47:50 +020093static int raspberrypi_clock_property(struct rpi_firmware *firmware,
94 const struct raspberrypi_clk_data *data,
95 u32 tag, u32 *val)
David Brazdil0f672f62019-12-10 10:32:29 +000096{
97 struct raspberrypi_firmware_prop msg = {
Olivier Deprez157378f2022-04-04 15:47:50 +020098 .id = cpu_to_le32(data->id),
David Brazdil0f672f62019-12-10 10:32:29 +000099 .val = cpu_to_le32(*val),
100 .disable_turbo = cpu_to_le32(1),
101 };
102 int ret;
103
104 ret = rpi_firmware_property(firmware, tag, &msg, sizeof(msg));
105 if (ret)
106 return ret;
107
108 *val = le32_to_cpu(msg.val);
109
110 return 0;
111}
112
Olivier Deprez157378f2022-04-04 15:47:50 +0200113static int raspberrypi_fw_is_prepared(struct clk_hw *hw)
David Brazdil0f672f62019-12-10 10:32:29 +0000114{
Olivier Deprez157378f2022-04-04 15:47:50 +0200115 struct raspberrypi_clk_data *data =
116 container_of(hw, struct raspberrypi_clk_data, hw);
117 struct raspberrypi_clk *rpi = data->rpi;
David Brazdil0f672f62019-12-10 10:32:29 +0000118 u32 val = 0;
119 int ret;
120
Olivier Deprez157378f2022-04-04 15:47:50 +0200121 ret = raspberrypi_clock_property(rpi->firmware, data,
122 RPI_FIRMWARE_GET_CLOCK_STATE, &val);
David Brazdil0f672f62019-12-10 10:32:29 +0000123 if (ret)
124 return 0;
125
126 return !!(val & RPI_FIRMWARE_STATE_ENABLE_BIT);
127}
128
129
Olivier Deprez157378f2022-04-04 15:47:50 +0200130static unsigned long raspberrypi_fw_get_rate(struct clk_hw *hw,
131 unsigned long parent_rate)
David Brazdil0f672f62019-12-10 10:32:29 +0000132{
Olivier Deprez157378f2022-04-04 15:47:50 +0200133 struct raspberrypi_clk_data *data =
134 container_of(hw, struct raspberrypi_clk_data, hw);
135 struct raspberrypi_clk *rpi = data->rpi;
David Brazdil0f672f62019-12-10 10:32:29 +0000136 u32 val = 0;
137 int ret;
138
Olivier Deprez157378f2022-04-04 15:47:50 +0200139 ret = raspberrypi_clock_property(rpi->firmware, data,
140 RPI_FIRMWARE_GET_CLOCK_RATE, &val);
David Brazdil0f672f62019-12-10 10:32:29 +0000141 if (ret)
142 return ret;
143
Olivier Deprez157378f2022-04-04 15:47:50 +0200144 return val;
David Brazdil0f672f62019-12-10 10:32:29 +0000145}
146
Olivier Deprez157378f2022-04-04 15:47:50 +0200147static int raspberrypi_fw_set_rate(struct clk_hw *hw, unsigned long rate,
148 unsigned long parent_rate)
David Brazdil0f672f62019-12-10 10:32:29 +0000149{
Olivier Deprez157378f2022-04-04 15:47:50 +0200150 struct raspberrypi_clk_data *data =
151 container_of(hw, struct raspberrypi_clk_data, hw);
152 struct raspberrypi_clk *rpi = data->rpi;
153 u32 _rate = rate;
David Brazdil0f672f62019-12-10 10:32:29 +0000154 int ret;
155
Olivier Deprez157378f2022-04-04 15:47:50 +0200156 ret = raspberrypi_clock_property(rpi->firmware, data,
157 RPI_FIRMWARE_SET_CLOCK_RATE, &_rate);
David Brazdil0f672f62019-12-10 10:32:29 +0000158 if (ret)
159 dev_err_ratelimited(rpi->dev, "Failed to change %s frequency: %d",
160 clk_hw_get_name(hw), ret);
161
162 return ret;
163}
164
Olivier Deprez157378f2022-04-04 15:47:50 +0200165static int raspberrypi_fw_dumb_determine_rate(struct clk_hw *hw,
166 struct clk_rate_request *req)
David Brazdil0f672f62019-12-10 10:32:29 +0000167{
Olivier Deprez157378f2022-04-04 15:47:50 +0200168 /*
169 * The firmware will do the rounding but that isn't part of
170 * the interface with the firmware, so we just do our best
171 * here.
172 */
173 req->rate = clamp(req->rate, req->min_rate, req->max_rate);
David Brazdil0f672f62019-12-10 10:32:29 +0000174 return 0;
175}
176
Olivier Deprez157378f2022-04-04 15:47:50 +0200177static const struct clk_ops raspberrypi_firmware_clk_ops = {
178 .is_prepared = raspberrypi_fw_is_prepared,
179 .recalc_rate = raspberrypi_fw_get_rate,
180 .determine_rate = raspberrypi_fw_dumb_determine_rate,
181 .set_rate = raspberrypi_fw_set_rate,
David Brazdil0f672f62019-12-10 10:32:29 +0000182};
183
Olivier Deprez157378f2022-04-04 15:47:50 +0200184static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
185 unsigned int parent,
186 unsigned int id)
David Brazdil0f672f62019-12-10 10:32:29 +0000187{
Olivier Deprez157378f2022-04-04 15:47:50 +0200188 struct raspberrypi_clk_data *data;
189 struct clk_init_data init = {};
190 u32 min_rate, max_rate;
David Brazdil0f672f62019-12-10 10:32:29 +0000191 int ret;
192
Olivier Deprez157378f2022-04-04 15:47:50 +0200193 data = devm_kzalloc(rpi->dev, sizeof(*data), GFP_KERNEL);
194 if (!data)
195 return ERR_PTR(-ENOMEM);
196 data->rpi = rpi;
197 data->id = id;
David Brazdil0f672f62019-12-10 10:32:29 +0000198
Olivier Deprez157378f2022-04-04 15:47:50 +0200199 init.name = devm_kasprintf(rpi->dev, GFP_KERNEL,
200 "fw-clk-%s",
201 rpi_firmware_clk_names[id]);
202 init.ops = &raspberrypi_firmware_clk_ops;
203 init.flags = CLK_GET_RATE_NOCACHE;
David Brazdil0f672f62019-12-10 10:32:29 +0000204
Olivier Deprez157378f2022-04-04 15:47:50 +0200205 data->hw.init = &init;
206
207 ret = raspberrypi_clock_property(rpi->firmware, data,
David Brazdil0f672f62019-12-10 10:32:29 +0000208 RPI_FIRMWARE_GET_MIN_CLOCK_RATE,
David Brazdil0f672f62019-12-10 10:32:29 +0000209 &min_rate);
210 if (ret) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200211 dev_err(rpi->dev, "Failed to get clock %d min freq: %d",
212 id, ret);
213 return ERR_PTR(ret);
David Brazdil0f672f62019-12-10 10:32:29 +0000214 }
215
Olivier Deprez157378f2022-04-04 15:47:50 +0200216 ret = raspberrypi_clock_property(rpi->firmware, data,
David Brazdil0f672f62019-12-10 10:32:29 +0000217 RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
David Brazdil0f672f62019-12-10 10:32:29 +0000218 &max_rate);
219 if (ret) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200220 dev_err(rpi->dev, "Failed to get clock %d max freq: %d\n",
221 id, ret);
222 return ERR_PTR(ret);
David Brazdil0f672f62019-12-10 10:32:29 +0000223 }
224
Olivier Deprez157378f2022-04-04 15:47:50 +0200225 ret = devm_clk_hw_register(rpi->dev, &data->hw);
226 if (ret)
227 return ERR_PTR(ret);
228
229 clk_hw_set_rate_range(&data->hw, min_rate, max_rate);
230
231 if (id == RPI_FIRMWARE_ARM_CLK_ID) {
232 ret = devm_clk_hw_register_clkdev(rpi->dev, &data->hw,
233 NULL, "cpu0");
234 if (ret) {
235 dev_err(rpi->dev, "Failed to initialize clkdev\n");
236 return ERR_PTR(ret);
237 }
David Brazdil0f672f62019-12-10 10:32:29 +0000238 }
239
Olivier Deprez157378f2022-04-04 15:47:50 +0200240 return &data->hw;
David Brazdil0f672f62019-12-10 10:32:29 +0000241}
242
Olivier Deprez157378f2022-04-04 15:47:50 +0200243struct rpi_firmware_get_clocks_response {
244 u32 parent;
245 u32 id;
246};
David Brazdil0f672f62019-12-10 10:32:29 +0000247
Olivier Deprez157378f2022-04-04 15:47:50 +0200248static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
249 struct clk_hw_onecell_data *data)
250{
251 struct rpi_firmware_get_clocks_response *clks;
252 int ret;
253
254 clks = devm_kcalloc(rpi->dev,
255 sizeof(*clks), RPI_FIRMWARE_NUM_CLK_ID,
256 GFP_KERNEL);
257 if (!clks)
David Brazdil0f672f62019-12-10 10:32:29 +0000258 return -ENOMEM;
Olivier Deprez157378f2022-04-04 15:47:50 +0200259
260 ret = rpi_firmware_property(rpi->firmware, RPI_FIRMWARE_GET_CLOCKS,
261 clks,
262 sizeof(*clks) * RPI_FIRMWARE_NUM_CLK_ID);
263 if (ret)
264 return ret;
265
266 while (clks->id) {
267 struct clk_hw *hw;
268
269 switch (clks->id) {
270 case RPI_FIRMWARE_ARM_CLK_ID:
271 case RPI_FIRMWARE_CORE_CLK_ID:
272 case RPI_FIRMWARE_M2MC_CLK_ID:
273 case RPI_FIRMWARE_V3D_CLK_ID:
274 case RPI_FIRMWARE_PIXEL_BVB_CLK_ID:
275 hw = raspberrypi_clk_register(rpi, clks->parent,
276 clks->id);
277 if (IS_ERR(hw))
278 return PTR_ERR(hw);
279
280 data->hws[clks->id] = hw;
281 data->num = clks->id + 1;
282 fallthrough;
283
284 default:
285 clks++;
286 break;
287 }
David Brazdil0f672f62019-12-10 10:32:29 +0000288 }
289
290 return 0;
291}
292
293static int raspberrypi_clk_probe(struct platform_device *pdev)
294{
Olivier Deprez157378f2022-04-04 15:47:50 +0200295 struct clk_hw_onecell_data *clk_data;
David Brazdil0f672f62019-12-10 10:32:29 +0000296 struct device_node *firmware_node;
297 struct device *dev = &pdev->dev;
298 struct rpi_firmware *firmware;
299 struct raspberrypi_clk *rpi;
300 int ret;
301
Olivier Deprez157378f2022-04-04 15:47:50 +0200302 /*
303 * We can be probed either through the an old-fashioned
304 * platform device registration or through a DT node that is a
305 * child of the firmware node. Handle both cases.
306 */
307 if (dev->of_node)
308 firmware_node = of_get_parent(dev->of_node);
309 else
310 firmware_node = of_find_compatible_node(NULL, NULL,
311 "raspberrypi,bcm2835-firmware");
David Brazdil0f672f62019-12-10 10:32:29 +0000312 if (!firmware_node) {
313 dev_err(dev, "Missing firmware node\n");
314 return -ENOENT;
315 }
316
317 firmware = rpi_firmware_get(firmware_node);
318 of_node_put(firmware_node);
319 if (!firmware)
320 return -EPROBE_DEFER;
321
322 rpi = devm_kzalloc(dev, sizeof(*rpi), GFP_KERNEL);
323 if (!rpi)
324 return -ENOMEM;
325
326 rpi->dev = dev;
327 rpi->firmware = firmware;
328 platform_set_drvdata(pdev, rpi);
329
Olivier Deprez157378f2022-04-04 15:47:50 +0200330 clk_data = devm_kzalloc(dev, struct_size(clk_data, hws,
331 RPI_FIRMWARE_NUM_CLK_ID),
332 GFP_KERNEL);
333 if (!clk_data)
334 return -ENOMEM;
David Brazdil0f672f62019-12-10 10:32:29 +0000335
Olivier Deprez157378f2022-04-04 15:47:50 +0200336 ret = raspberrypi_discover_clocks(rpi, clk_data);
337 if (ret)
338 return ret;
339
340 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
341 clk_data);
David Brazdil0f672f62019-12-10 10:32:29 +0000342 if (ret)
343 return ret;
344
345 rpi->cpufreq = platform_device_register_data(dev, "raspberrypi-cpufreq",
346 -1, NULL, 0);
347
348 return 0;
349}
350
351static int raspberrypi_clk_remove(struct platform_device *pdev)
352{
353 struct raspberrypi_clk *rpi = platform_get_drvdata(pdev);
354
355 platform_device_unregister(rpi->cpufreq);
356
357 return 0;
358}
359
Olivier Deprez157378f2022-04-04 15:47:50 +0200360static const struct of_device_id raspberrypi_clk_match[] = {
361 { .compatible = "raspberrypi,firmware-clocks" },
362 { },
363};
364MODULE_DEVICE_TABLE(of, raspberrypi_clk_match);
365
David Brazdil0f672f62019-12-10 10:32:29 +0000366static struct platform_driver raspberrypi_clk_driver = {
367 .driver = {
368 .name = "raspberrypi-clk",
Olivier Deprez157378f2022-04-04 15:47:50 +0200369 .of_match_table = raspberrypi_clk_match,
David Brazdil0f672f62019-12-10 10:32:29 +0000370 },
371 .probe = raspberrypi_clk_probe,
372 .remove = raspberrypi_clk_remove,
373};
374module_platform_driver(raspberrypi_clk_driver);
375
376MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
377MODULE_DESCRIPTION("Raspberry Pi firmware clock driver");
378MODULE_LICENSE("GPL");
379MODULE_ALIAS("platform:raspberrypi-clk");