blob: 3a9cf2a231f2ca19d767a305edb832fa544a01f8 [file] [log] [blame]
Konrad Dybcio6c0b8442023-11-07 12:41:01 +00001// SPDX-License-Identifier: BSD-3-Clause AND GPL-2.0
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +01002/*
Konrad Dybcio6c0b8442023-11-07 12:41:01 +00003 * Clock and reset drivers for Qualcomm platforms Global Clock
4 * Controller (GCC).
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +01005 *
6 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
Konrad Dybcio6c0b8442023-11-07 12:41:01 +00007 * (C) Copyright 2020 Sartura Ltd. (reset driver)
8 * Author: Robert Marko <robert.marko@sartura.hr>
9 * (C) Copyright 2022 Linaro Ltd. (reset driver)
10 * Author: Sumit Garg <sumit.garg@linaro.org>
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010011 *
12 * Based on Little Kernel driver, simplified
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010013 */
14
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010015#include <clk-uclass.h>
16#include <dm.h>
Konrad Dybcio6c0b8442023-11-07 12:41:01 +000017#include <dm/device-internal.h>
18#include <dm/lists.h>
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010019#include <errno.h>
20#include <asm/io.h>
Caleb Connolly397c84f2023-11-07 12:41:05 +000021#include <linux/bug.h>
22#include <linux/delay.h>
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010023#include <linux/bitops.h>
Volodymyr Babchukaae46492024-03-11 21:33:45 +000024#include <linux/iopoll.h>
Konrad Dybcio6c0b8442023-11-07 12:41:01 +000025#include <reset-uclass.h>
Volodymyr Babchukaae46492024-03-11 21:33:45 +000026#include <power-domain-uclass.h>
Konrad Dybcio6c0b8442023-11-07 12:41:01 +000027
Caleb Connolly878b26a2023-11-07 12:40:59 +000028#include "clock-qcom.h"
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010029
30/* CBCR register fields */
31#define CBCR_BRANCH_ENABLE_BIT BIT(0)
32#define CBCR_BRANCH_OFF_BIT BIT(31)
33
Volodymyr Babchukaae46492024-03-11 21:33:45 +000034#define GDSC_SW_COLLAPSE_MASK BIT(0)
35#define GDSC_POWER_DOWN_COMPLETE BIT(15)
36#define GDSC_POWER_UP_COMPLETE BIT(16)
37#define GDSC_PWR_ON_MASK BIT(31)
38#define CFG_GDSCR_OFFSET 0x4
39#define GDSC_STATUS_POLL_TIMEOUT_US 1500
40
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010041/* Enable clock controlled by CBC soft macro */
42void clk_enable_cbc(phys_addr_t cbcr)
43{
44 setbits_le32(cbcr, CBCR_BRANCH_ENABLE_BIT);
45
46 while (readl(cbcr) & CBCR_BRANCH_OFF_BIT)
47 ;
48}
49
Ramon Friedae299772018-05-16 12:13:39 +030050void clk_enable_gpll0(phys_addr_t base, const struct pll_vote_clk *gpll0)
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010051{
52 if (readl(base + gpll0->status) & gpll0->status_bit)
53 return; /* clock already enabled */
54
55 setbits_le32(base + gpll0->ena_vote, gpll0->vote_bit);
56
57 while ((readl(base + gpll0->status) & gpll0->status_bit) == 0)
58 ;
59}
60
Ramon Friedae299772018-05-16 12:13:39 +030061#define BRANCH_ON_VAL (0)
62#define BRANCH_NOC_FSM_ON_VAL BIT(29)
63#define BRANCH_CHECK_MASK GENMASK(31, 28)
64
65void clk_enable_vote_clk(phys_addr_t base, const struct vote_clk *vclk)
66{
67 u32 val;
68
69 setbits_le32(base + vclk->ena_vote, vclk->vote_bit);
70 do {
71 val = readl(base + vclk->cbcr_reg);
72 val &= BRANCH_CHECK_MASK;
73 } while ((val != BRANCH_ON_VAL) && (val != BRANCH_NOC_FSM_ON_VAL));
74}
75
Sheep Sun49fee7b2021-06-20 10:34:35 +080076#define APPS_CMD_RCGR_UPDATE BIT(0)
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010077
Sheep Sun49fee7b2021-06-20 10:34:35 +080078/* Update clock command via CMD_RCGR */
79void clk_bcr_update(phys_addr_t apps_cmd_rcgr)
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010080{
Caleb Connolly397c84f2023-11-07 12:41:05 +000081 u32 count;
Sheep Sun49fee7b2021-06-20 10:34:35 +080082 setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE);
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010083
84 /* Wait for frequency to be updated. */
Caleb Connolly397c84f2023-11-07 12:41:05 +000085 for (count = 0; count < 50000; count++) {
86 if (!(readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE))
87 break;
88 udelay(1);
89 }
90 WARN(count == 50000, "WARNING: RCG @ %#llx [%#010x] stuck at off\n",
91 apps_cmd_rcgr, readl(apps_cmd_rcgr));
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +010092}
93
Caleb Connolly397c84f2023-11-07 12:41:05 +000094#define CFG_SRC_DIV_MASK 0b11111
95#define CFG_SRC_SEL_SHIFT 8
96#define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT)
97#define CFG_MODE_SHIFT 12
98#define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT)
99#define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT)
100#define CFG_HW_CLK_CTRL_MASK BIT(20)
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100101
Caleb Connolly397c84f2023-11-07 12:41:05 +0000102/*
103 * root set rate for clocks with half integer and MND divider
104 * div should be pre-calculated ((div * 2) - 1)
105 */
Caleb Connollycbdad442024-04-03 14:07:40 +0200106void clk_rcg_set_rate_mnd(phys_addr_t base, uint32_t cmd_rcgr,
Caleb Connollyfbacc672023-11-07 12:41:04 +0000107 int div, int m, int n, int source, u8 mnd_width)
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100108{
109 u32 cfg;
110 /* M value for MND divider. */
111 u32 m_val = m;
Caleb Connolly397c84f2023-11-07 12:41:05 +0000112 u32 n_minus_m = n - m;
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100113 /* NOT(N-M) value for MND divider. */
Caleb Connolly397c84f2023-11-07 12:41:05 +0000114 u32 n_val = ~n_minus_m * !!(n);
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100115 /* NOT 2D value for MND divider. */
Caleb Connolly397c84f2023-11-07 12:41:05 +0000116 u32 d_val = ~(clamp_t(u32, n, m, n_minus_m));
Caleb Connollyfbacc672023-11-07 12:41:04 +0000117 u32 mask = BIT(mnd_width) - 1;
118
119 debug("m %#x n %#x d %#x div %#x mask %#x\n", m_val, n_val, d_val, div, mask);
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100120
121 /* Program MND values */
Caleb Connollycbdad442024-04-03 14:07:40 +0200122 writel(m_val & mask, base + cmd_rcgr + RCG_M_REG);
123 writel(n_val & mask, base + cmd_rcgr + RCG_N_REG);
124 writel(d_val & mask, base + cmd_rcgr + RCG_D_REG);
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100125
126 /* setup src select and divider */
Caleb Connollycbdad442024-04-03 14:07:40 +0200127 cfg = readl(base + cmd_rcgr + RCG_CFG_REG);
Volodymyr Babchuk8eca2612024-03-11 21:33:45 +0000128 cfg &= ~(CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK |
129 CFG_SRC_DIV_MASK);
Caleb Connolly397c84f2023-11-07 12:41:05 +0000130 cfg |= source & CFG_SRC_SEL_MASK; /* Select clock source */
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100131
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100132 if (div)
Caleb Connolly397c84f2023-11-07 12:41:05 +0000133 cfg |= div & CFG_SRC_DIV_MASK;
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100134
Caleb Connolly397c84f2023-11-07 12:41:05 +0000135 if (n && n != m)
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100136 cfg |= CFG_MODE_DUAL_EDGE;
137
Caleb Connollycbdad442024-04-03 14:07:40 +0200138 writel(cfg, base + cmd_rcgr + RCG_CFG_REG); /* Write new clock configuration */
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100139
140 /* Inform h/w to start using the new config. */
Caleb Connollycbdad442024-04-03 14:07:40 +0200141 clk_bcr_update(base + cmd_rcgr);
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100142}
143
Sumit Garga3e804d2023-02-01 19:28:57 +0530144/* root set rate for clocks with half integer and mnd_width=0 */
Caleb Connollycbdad442024-04-03 14:07:40 +0200145void clk_rcg_set_rate(phys_addr_t base, uint32_t cmd_rcgr, int div,
Sumit Garga3e804d2023-02-01 19:28:57 +0530146 int source)
147{
148 u32 cfg;
149
150 /* setup src select and divider */
Caleb Connollycbdad442024-04-03 14:07:40 +0200151 cfg = readl(base + cmd_rcgr + RCG_CFG_REG);
Caleb Connolly397c84f2023-11-07 12:41:05 +0000152 cfg &= ~(CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK);
Sumit Garga3e804d2023-02-01 19:28:57 +0530153 cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */
154
155 /*
156 * Set the divider; HW permits fraction dividers (+0.5), but
157 * for simplicity, we will support integers only
158 */
159 if (div)
Caleb Connolly397c84f2023-11-07 12:41:05 +0000160 cfg |= (2 * div - 1) & CFG_SRC_DIV_MASK;
Sumit Garga3e804d2023-02-01 19:28:57 +0530161
Caleb Connollycbdad442024-04-03 14:07:40 +0200162 writel(cfg, base + cmd_rcgr + RCG_CFG_REG); /* Write new clock configuration */
Sumit Garga3e804d2023-02-01 19:28:57 +0530163
164 /* Inform h/w to start using the new config. */
Caleb Connollycbdad442024-04-03 14:07:40 +0200165 clk_bcr_update(base + cmd_rcgr);
Sumit Garga3e804d2023-02-01 19:28:57 +0530166}
167
Caleb Connolly397c84f2023-11-07 12:41:05 +0000168const struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, uint rate)
169{
170 if (!f)
171 return NULL;
172
173 if (!f->freq)
174 return f;
175
176 for (; f->freq; f++)
177 if (rate <= f->freq)
178 return f;
179
180 /* Default to our fastest rate */
181 return f - 1;
182}
183
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100184static int msm_clk_probe(struct udevice *dev)
185{
Konrad Dybcio6c0b8442023-11-07 12:41:01 +0000186 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev);
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100187 struct msm_clk_priv *priv = dev_get_priv(dev);
188
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900189 priv->base = dev_read_addr(dev);
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100190 if (priv->base == FDT_ADDR_T_NONE)
191 return -EINVAL;
192
Konrad Dybcio6c0b8442023-11-07 12:41:01 +0000193 priv->data = data;
194
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100195 return 0;
196}
197
198static ulong msm_clk_set_rate(struct clk *clk, ulong rate)
199{
Caleb Connolly10a0abb2023-11-07 12:41:03 +0000200 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev);
201
202 if (data->set_rate)
203 return data->set_rate(clk, rate);
204
205 return 0;
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100206}
207
Sumit Garg1d1ca6e2022-08-04 19:57:14 +0530208static int msm_clk_enable(struct clk *clk)
209{
Caleb Connolly10a0abb2023-11-07 12:41:03 +0000210 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev);
211
212 if (data->enable)
213 return data->enable(clk);
214
215 return 0;
Sumit Garg1d1ca6e2022-08-04 19:57:14 +0530216}
217
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100218static struct clk_ops msm_clk_ops = {
219 .set_rate = msm_clk_set_rate,
Sumit Garg1d1ca6e2022-08-04 19:57:14 +0530220 .enable = msm_clk_enable,
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100221};
222
Konrad Dybcio6c0b8442023-11-07 12:41:01 +0000223U_BOOT_DRIVER(qcom_clk) = {
224 .name = "qcom_clk",
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100225 .id = UCLASS_CLK,
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100226 .ops = &msm_clk_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700227 .priv_auto = sizeof(struct msm_clk_priv),
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100228 .probe = msm_clk_probe,
Caleb Connollye07ce562024-04-03 14:07:39 +0200229 .flags = DM_FLAG_PRE_RELOC | DM_FLAG_DEFAULT_PD_CTRL_OFF,
Jorge Ramirez-Ortiz92c1eff2018-01-10 11:33:49 +0100230};
Konrad Dybcio6c0b8442023-11-07 12:41:01 +0000231
232int qcom_cc_bind(struct udevice *parent)
233{
234 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(parent);
Volodymyr Babchukaae46492024-03-11 21:33:45 +0000235 struct udevice *clkdev = NULL, *rstdev = NULL, *pwrdev;
Konrad Dybcio6c0b8442023-11-07 12:41:01 +0000236 struct driver *drv;
237 int ret;
238
239 /* Get a handle to the common clk handler */
240 drv = lists_driver_lookup_name("qcom_clk");
241 if (!drv)
242 return -ENOENT;
243
244 /* Register the clock controller */
245 ret = device_bind_with_driver_data(parent, drv, "qcom_clk", (ulong)data,
246 dev_ofnode(parent), &clkdev);
247 if (ret)
248 return ret;
249
Volodymyr Babchukaae46492024-03-11 21:33:45 +0000250 if (data->resets) {
251 /* Get a handle to the common reset handler */
252 drv = lists_driver_lookup_name("qcom_reset");
253 if (!drv) {
254 ret = -ENOENT;
255 goto unbind_clkdev;
256 }
Konrad Dybcio6c0b8442023-11-07 12:41:01 +0000257
Volodymyr Babchukaae46492024-03-11 21:33:45 +0000258 /* Register the reset controller */
259 ret = device_bind_with_driver_data(parent, drv, "qcom_reset", (ulong)data,
260 dev_ofnode(parent), &rstdev);
261 if (ret)
262 goto unbind_clkdev;
263 }
Konrad Dybcio6c0b8442023-11-07 12:41:01 +0000264
Volodymyr Babchukaae46492024-03-11 21:33:45 +0000265 if (data->power_domains) {
266 /* Get a handle to the common power domain handler */
267 drv = lists_driver_lookup_name("qcom_power");
268 if (!drv) {
269 ret = -ENOENT;
270 goto unbind_rstdev;
271 }
272 /* Register the power domain controller */
273 ret = device_bind_with_driver_data(parent, drv, "qcom_power", (ulong)data,
274 dev_ofnode(parent), &pwrdev);
275 if (ret)
276 goto unbind_rstdev;
277 }
278
279 return 0;
280
281unbind_rstdev:
282 device_unbind(rstdev);
283unbind_clkdev:
284 device_unbind(clkdev);
Konrad Dybcio6c0b8442023-11-07 12:41:01 +0000285
286 return ret;
287}
288
289static int qcom_reset_set(struct reset_ctl *rst, bool assert)
290{
291 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(rst->dev);
292 void __iomem *base = dev_get_priv(rst->dev);
293 const struct qcom_reset_map *map;
294 u32 value;
295
296 map = &data->resets[rst->id];
297
298 value = readl(base + map->reg);
299
300 if (assert)
301 value |= BIT(map->bit);
302 else
303 value &= ~BIT(map->bit);
304
305 writel(value, base + map->reg);
306
307 return 0;
308}
309
310static int qcom_reset_assert(struct reset_ctl *rst)
311{
312 return qcom_reset_set(rst, true);
313}
314
315static int qcom_reset_deassert(struct reset_ctl *rst)
316{
317 return qcom_reset_set(rst, false);
318}
319
320static const struct reset_ops qcom_reset_ops = {
321 .rst_assert = qcom_reset_assert,
322 .rst_deassert = qcom_reset_deassert,
323};
324
325static int qcom_reset_probe(struct udevice *dev)
326{
327 /* Set our priv pointer to the base address */
328 dev_set_priv(dev, (void *)dev_read_addr(dev));
329
330 return 0;
331}
332
333U_BOOT_DRIVER(qcom_reset) = {
334 .name = "qcom_reset",
335 .id = UCLASS_RESET,
336 .ops = &qcom_reset_ops,
337 .probe = qcom_reset_probe,
338};
Volodymyr Babchukaae46492024-03-11 21:33:45 +0000339
340static int qcom_power_set(struct power_domain *pwr, bool on)
341{
342 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(pwr->dev);
343 void __iomem *base = dev_get_priv(pwr->dev);
344 const struct qcom_power_map *map;
345 u32 value;
346 int ret;
347
348 if (pwr->id >= data->num_power_domains)
349 return -ENODEV;
350
351 map = &data->power_domains[pwr->id];
352
353 if (!map->reg)
354 return -ENODEV;
355
356 value = readl(base + map->reg);
357
358 if (on)
359 value &= ~GDSC_SW_COLLAPSE_MASK;
360 else
361 value |= GDSC_SW_COLLAPSE_MASK;
362
363 writel(value, base + map->reg);
364
365 if (on)
366 ret = readl_poll_timeout(base + map->reg + CFG_GDSCR_OFFSET,
367 value,
368 (value & GDSC_POWER_UP_COMPLETE) ||
369 (value & GDSC_PWR_ON_MASK),
370 GDSC_STATUS_POLL_TIMEOUT_US);
371
372 else
373 ret = readl_poll_timeout(base + map->reg + CFG_GDSCR_OFFSET,
374 value,
375 (value & GDSC_POWER_DOWN_COMPLETE) ||
376 !(value & GDSC_PWR_ON_MASK),
377 GDSC_STATUS_POLL_TIMEOUT_US);
378
379
380 if (ret == -ETIMEDOUT)
381 printf("WARNING: GDSC %lu is stuck during power on/off\n",
382 pwr->id);
383 return ret;
384}
385
386static int qcom_power_on(struct power_domain *pwr)
387{
388 return qcom_power_set(pwr, true);
389}
390
391static int qcom_power_off(struct power_domain *pwr)
392{
393 return qcom_power_set(pwr, false);
394}
395
396static const struct power_domain_ops qcom_power_ops = {
397 .on = qcom_power_on,
398 .off = qcom_power_off,
399};
400
401static int qcom_power_probe(struct udevice *dev)
402{
403 /* Set our priv pointer to the base address */
404 dev_set_priv(dev, (void *)dev_read_addr(dev));
405
406 return 0;
407}
408
409U_BOOT_DRIVER(qcom_power) = {
410 .name = "qcom_power",
411 .id = UCLASS_POWER_DOMAIN,
412 .ops = &qcom_power_ops,
413 .probe = qcom_power_probe,
Caleb Connollye07ce562024-04-03 14:07:39 +0200414 .flags = DM_FLAG_PRE_RELOC,
Volodymyr Babchukaae46492024-03-11 21:33:45 +0000415};