blob: c880df2126336ca98efdcb569a3b91a194d257bf [file] [log] [blame]
Konrad Dybciod9935732023-11-07 12:41:01 +00001// SPDX-License-Identifier: BSD-3-Clause AND GPL-2.0
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +01002/*
Konrad Dybciod9935732023-11-07 12:41:01 +00003 * Clock and reset drivers for Qualcomm platforms Global Clock
4 * Controller (GCC).
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +01005 *
6 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
Konrad Dybciod9935732023-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-Ortiz7c75f7f2018-01-10 11:33:49 +010011 *
12 * Based on Little Kernel driver, simplified
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010013 */
14
15#include <common.h>
16#include <clk-uclass.h>
17#include <dm.h>
Konrad Dybciod9935732023-11-07 12:41:01 +000018#include <dm/device-internal.h>
19#include <dm/lists.h>
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010020#include <errno.h>
21#include <asm/io.h>
Caleb Connollybe20d622023-11-07 12:41:05 +000022#include <linux/bug.h>
23#include <linux/delay.h>
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010024#include <linux/bitops.h>
Konrad Dybciod9935732023-11-07 12:41:01 +000025#include <reset-uclass.h>
26
Caleb Connolly5bb0df62023-11-07 12:40:59 +000027#include "clock-qcom.h"
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010028
29/* CBCR register fields */
30#define CBCR_BRANCH_ENABLE_BIT BIT(0)
31#define CBCR_BRANCH_OFF_BIT BIT(31)
32
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010033/* Enable clock controlled by CBC soft macro */
34void clk_enable_cbc(phys_addr_t cbcr)
35{
36 setbits_le32(cbcr, CBCR_BRANCH_ENABLE_BIT);
37
38 while (readl(cbcr) & CBCR_BRANCH_OFF_BIT)
39 ;
40}
41
Caleb Connolly4b31c6f2023-11-21 18:09:15 +000042void gdsc_enable(phys_addr_t gdscr)
43{
44 uint32_t count;
45 clrbits_le32(gdscr, GDSC_SW_COLLAPSE);
46 for (count = 0; count < 1500; count++) {
47 if (readl(gdscr) & GDSC_PWR_ON)
48 break;
49 udelay(1);
50 }
51 WARN(count == 1500, "WARNING: GDSC @ %#llx stuck at off\n", gdscr);
52}
53
Ramon Fried640dc342018-05-16 12:13:39 +030054void clk_enable_gpll0(phys_addr_t base, const struct pll_vote_clk *gpll0)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010055{
56 if (readl(base + gpll0->status) & gpll0->status_bit)
57 return; /* clock already enabled */
58
59 setbits_le32(base + gpll0->ena_vote, gpll0->vote_bit);
60
61 while ((readl(base + gpll0->status) & gpll0->status_bit) == 0)
62 ;
63}
64
Ramon Fried640dc342018-05-16 12:13:39 +030065#define BRANCH_ON_VAL (0)
66#define BRANCH_NOC_FSM_ON_VAL BIT(29)
67#define BRANCH_CHECK_MASK GENMASK(31, 28)
68
69void clk_enable_vote_clk(phys_addr_t base, const struct vote_clk *vclk)
70{
71 u32 val;
72
73 setbits_le32(base + vclk->ena_vote, vclk->vote_bit);
74 do {
75 val = readl(base + vclk->cbcr_reg);
76 val &= BRANCH_CHECK_MASK;
77 } while ((val != BRANCH_ON_VAL) && (val != BRANCH_NOC_FSM_ON_VAL));
78}
79
Sheep Sun6d430e12021-06-20 10:34:35 +080080#define APPS_CMD_RCGR_UPDATE BIT(0)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010081
Sheep Sun6d430e12021-06-20 10:34:35 +080082/* Update clock command via CMD_RCGR */
83void clk_bcr_update(phys_addr_t apps_cmd_rcgr)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010084{
Caleb Connollybe20d622023-11-07 12:41:05 +000085 u32 count;
Sheep Sun6d430e12021-06-20 10:34:35 +080086 setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010087
88 /* Wait for frequency to be updated. */
Caleb Connollybe20d622023-11-07 12:41:05 +000089 for (count = 0; count < 50000; count++) {
90 if (!(readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE))
91 break;
92 udelay(1);
93 }
94 WARN(count == 50000, "WARNING: RCG @ %#llx [%#010x] stuck at off\n",
95 apps_cmd_rcgr, readl(apps_cmd_rcgr));
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010096}
97
Caleb Connollybe20d622023-11-07 12:41:05 +000098#define CFG_SRC_DIV_MASK 0b11111
99#define CFG_SRC_SEL_SHIFT 8
100#define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT)
101#define CFG_MODE_SHIFT 12
102#define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT)
103#define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT)
104#define CFG_HW_CLK_CTRL_MASK BIT(20)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100105
Caleb Connollybe20d622023-11-07 12:41:05 +0000106/*
107 * root set rate for clocks with half integer and MND divider
108 * div should be pre-calculated ((div * 2) - 1)
109 */
Caleb Connolly422b74b2023-11-21 17:55:53 +0000110void clk_rcg_set_rate_mnd(phys_addr_t base, uint32_t cmd_rcgr,
Caleb Connolly97d7ed32023-11-07 12:41:04 +0000111 int div, int m, int n, int source, u8 mnd_width)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100112{
113 u32 cfg;
114 /* M value for MND divider. */
115 u32 m_val = m;
Caleb Connollybe20d622023-11-07 12:41:05 +0000116 u32 n_minus_m = n - m;
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100117 /* NOT(N-M) value for MND divider. */
Caleb Connollybe20d622023-11-07 12:41:05 +0000118 u32 n_val = ~n_minus_m * !!(n);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100119 /* NOT 2D value for MND divider. */
Caleb Connollybe20d622023-11-07 12:41:05 +0000120 u32 d_val = ~(clamp_t(u32, n, m, n_minus_m));
Caleb Connolly97d7ed32023-11-07 12:41:04 +0000121 u32 mask = BIT(mnd_width) - 1;
122
123 debug("m %#x n %#x d %#x div %#x mask %#x\n", m_val, n_val, d_val, div, mask);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100124
125 /* Program MND values */
Caleb Connolly422b74b2023-11-21 17:55:53 +0000126 writel(m_val & mask, base + cmd_rcgr + RCG_M_REG);
127 writel(n_val & mask, base + cmd_rcgr + RCG_N_REG);
128 writel(d_val & mask, base + cmd_rcgr + RCG_D_REG);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100129
130 /* setup src select and divider */
Caleb Connolly422b74b2023-11-21 17:55:53 +0000131 cfg = readl(base + cmd_rcgr + RCG_CFG_REG);
Caleb Connollybe20d622023-11-07 12:41:05 +0000132 cfg &= ~(CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK);
133 cfg |= source & CFG_SRC_SEL_MASK; /* Select clock source */
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100134
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100135 if (div)
Caleb Connollybe20d622023-11-07 12:41:05 +0000136 cfg |= div & CFG_SRC_DIV_MASK;
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100137
Caleb Connollybe20d622023-11-07 12:41:05 +0000138 if (n && n != m)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100139 cfg |= CFG_MODE_DUAL_EDGE;
140
Caleb Connolly422b74b2023-11-21 17:55:53 +0000141 writel(cfg, base + cmd_rcgr + RCG_CFG_REG); /* Write new clock configuration */
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100142
143 /* Inform h/w to start using the new config. */
Caleb Connolly422b74b2023-11-21 17:55:53 +0000144 clk_bcr_update(base + cmd_rcgr);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100145}
146
Sumit Garg22d3fcd2023-02-01 19:28:57 +0530147/* root set rate for clocks with half integer and mnd_width=0 */
Caleb Connolly422b74b2023-11-21 17:55:53 +0000148void clk_rcg_set_rate(phys_addr_t base, uint32_t cmd_rcgr, int div,
Sumit Garg22d3fcd2023-02-01 19:28:57 +0530149 int source)
150{
151 u32 cfg;
152
153 /* setup src select and divider */
Caleb Connolly422b74b2023-11-21 17:55:53 +0000154 cfg = readl(base + cmd_rcgr + RCG_CFG_REG);
Caleb Connollybe20d622023-11-07 12:41:05 +0000155 cfg &= ~(CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK);
Sumit Garg22d3fcd2023-02-01 19:28:57 +0530156 cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */
157
158 /*
159 * Set the divider; HW permits fraction dividers (+0.5), but
160 * for simplicity, we will support integers only
161 */
162 if (div)
Caleb Connollybe20d622023-11-07 12:41:05 +0000163 cfg |= (2 * div - 1) & CFG_SRC_DIV_MASK;
Sumit Garg22d3fcd2023-02-01 19:28:57 +0530164
Caleb Connolly422b74b2023-11-21 17:55:53 +0000165 writel(cfg, base + cmd_rcgr + RCG_CFG_REG); /* Write new clock configuration */
Sumit Garg22d3fcd2023-02-01 19:28:57 +0530166
167 /* Inform h/w to start using the new config. */
Caleb Connolly422b74b2023-11-21 17:55:53 +0000168 clk_bcr_update(base + cmd_rcgr);
Sumit Garg22d3fcd2023-02-01 19:28:57 +0530169}
170
Caleb Connollybe20d622023-11-07 12:41:05 +0000171const struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, uint rate)
172{
173 if (!f)
174 return NULL;
175
176 if (!f->freq)
177 return f;
178
179 for (; f->freq; f++)
180 if (rate <= f->freq)
181 return f;
182
183 /* Default to our fastest rate */
184 return f - 1;
185}
186
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100187static int msm_clk_probe(struct udevice *dev)
188{
Konrad Dybciod9935732023-11-07 12:41:01 +0000189 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100190 struct msm_clk_priv *priv = dev_get_priv(dev);
191
Masahiro Yamada25484932020-07-17 14:36:48 +0900192 priv->base = dev_read_addr(dev);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100193 if (priv->base == FDT_ADDR_T_NONE)
194 return -EINVAL;
195
Konrad Dybciod9935732023-11-07 12:41:01 +0000196 priv->data = data;
197
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100198 return 0;
199}
200
201static ulong msm_clk_set_rate(struct clk *clk, ulong rate)
202{
Caleb Connollyc94f9e92023-11-07 12:41:03 +0000203 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev);
204
205 if (data->set_rate)
206 return data->set_rate(clk, rate);
207
208 return 0;
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100209}
210
Sumit Gargc9e384e2022-08-04 19:57:14 +0530211static int msm_clk_enable(struct clk *clk)
212{
Caleb Connollyc94f9e92023-11-07 12:41:03 +0000213 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev);
214
215 if (data->enable)
216 return data->enable(clk);
217
218 return 0;
Sumit Gargc9e384e2022-08-04 19:57:14 +0530219}
220
Caleb Connollyd3888992023-10-02 16:56:21 +0100221static void dump_gplls(struct udevice *dev, phys_addr_t base) {
222 static phys_addr_t gplls[] = {
223 0x00100000,
224 0x00101000,
225 0x00102000,
226 0x00103000,
227 0x00176000,
228 0x00174000,
229 0x00113000,
230 0x0011a000,
231 0x0011b000,
232 0x0011c000,
233 0x0011d000,
234 0x0014a000,
235 };
236 uint32_t i;
237 bool locked;
238 uint64_t l, a, xo_rate = 19200000;
239 struct clk clk;
240 int ret;
241 u32 pll_branch = readl(0x00152018);
242
243 ret = clk_get_by_name(dev, "xo_board", &clk);
244 if (ret < 0) {
245 ret = clk_get_by_name(dev, "xo-board", &clk);
246 if (ret < 0)
247 printf("Can't find XO clock, XO_BOARD rate may be wrong\n");
248 }
249
250 if (ret >= 0)
251 xo_rate = clk_get_rate(&clk);
252
253 printf("| GPLL | LOCKED | GATE | XO_BOARD | PLL_L | ALPHA |\n");
254 printf("+--------+--------+------+-----------+------------+----------------+\n");
255 for (i = 0; i < ARRAY_SIZE(gplls); i++) {
256 locked = !!(readl(gplls[i]) & BIT(31));
257 l = readl(gplls[i] + 4) & (BIT(16)-1);
258 a = readq(gplls[i] + 40) & (BIT(16)-1);
259 printf("| GPLL%-2d | %-6s | %-4s | %9llu * (%#-9llx + %#-13llx * 2 ** -40 ) / 1000000\n",
260 i, locked ? "X" : "", pll_branch & BIT(i) ? "X" : "", xo_rate, l, a);
261 }
262}
263
264static void dump_rcgs(void) {
265 static phys_addr_t rcgs[] = {
266 // 0x0010f018, // RB2
267 // 0x0010f030,
268 // 0x0010f05c,
269
270 // RB5
271 // 0x00175024, // GCC_UFS_CARD_AXI_CMD_RCGR
272 // 0x0017506c, // GCC_UFS_CARD_ICE_CORE_CMD_RCGR
273 // 0x00175084, // GCC_UFS_CARD_UNIPRO_CORE_CMD_RCGR
274 // 0x001750a0, // GCC_UFS_CARD_PHY_AUX_CMD_RCGR
275 // 0x00177024, // GCC_UFS_PHY_AXI_CMD_RCGR
276 // 0x0017706c, // GCC_UFS_PHY_ICE_CORE_CMD_RCGR
277 // 0x00177084, // GCC_UFS_PHY_UNIPRO_CORE_CMD_RCGR
278 // 0x001770a0, // GCC_UFS_PHY_PHY_AUX_CMD_RCGR
279 0x0011400c, // GCC_SDCC2_APPS_CMD_RCGR
280 //0x001184D0
281
282 // RB3
283 //0x00118148
284 };
285 static const char * const rcg_names[] = {
286 // "USB30_PRIM_MASTER", // RB2
287 // "USB30_PRIM_MOCK_UTMI",
288 // "USB3_PRIM_PHY_AUX",
289
290 // RB5
291 // "GCC_UFS_CARD_AXI_CMD_RCGR",
292 // "GCC_UFS_CARD_ICE_CORE_CMD_RCGR",
293 // "GCC_UFS_CARD_UNIPRO_CORE_CMD_RCGR",
294 // "GCC_UFS_CARD_PHY_AUX_CMD_RCGR",
295 // "GCC_UFS_PHY_AXI_CMD_RCGR",
296 // "GCC_UFS_PHY_ICE_CORE_CMD_RCGR",
297 // "GCC_UFS_PHY_UNIPRO_CORE_CMD_RCGR",
298 // "GCC_UFS_PHY_PHY_AUX_CMD_RCGR",
299 "GCC_SDCC2_APPS_CMD_RCGR",
300 //"UART",
301 };
302 int i;
303 uint32_t cmd;
304 uint32_t cfg;
305 uint32_t not_n_minus_m;
306 uint32_t src, m, n, div;
307 bool root_on, d_odd;
308 printf("\nRCGs:\n");
309
310 for (i = 0; i < ARRAY_SIZE(rcgs); i++) {
311 cmd = readl(rcgs[i]);
312 cfg = readl(rcgs[i] + 0x4);
313 m = readl(rcgs[i] + 0x8);
314 not_n_minus_m = readl(rcgs[i] + 0xc);
315
316 root_on = !(cmd & BIT(31)); // ROOT_OFF
317 src = (cfg >> 8) & 7;
318
319 if (not_n_minus_m)
320 n = (~not_n_minus_m & 0xffff) + m;
321 else
322 n = 0;
323
324 div = ((cfg & 0b11111) + 1) / 2;
325 d_odd = ((cfg & 0b11111) + 1) % 2 == 1;
326 printf("%#010x %#010x %#010x %#010x %#010x\n", cmd, cfg, m, not_n_minus_m, readl(rcgs[i] + 0x10));
327 printf("%-32s: %-1s src %d | input_freq * (%#x/%#x) * (1/%d%s)",
328 rcg_names[i], root_on ? "X" : "", src, m ?: 1, n ?: 1, div, d_odd ? ".5" : "");
329 printf(" [%#010X]\n", cmd);
330 }
331
332 printf("\n");
333}
334
335static void msm_dump_clks(struct udevice *dev)
336{
337 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev);
338 struct msm_clk_priv *priv = dev_get_priv(dev);
339 const struct gate_clk *sclk;
340 const struct qcom_reset_map *rst;
341 int val, i;
342
343 if (!data->clks) {
344 printf("No clocks\n");
345 return;
346 }
347
348 for (i = 0; i < data->num_clks; i++) {
349 sclk = &data->clks[i];
350 if (!sclk->name)
351 continue;
352 printf("%-32s: ", sclk->name);
353 val = readl(priv->base + sclk->reg) & sclk->en_val;
354 printf("%s\n", val ? "ON" : "");
355 }
356
357 for (i = 0; i < data->num_resets; i++) {
358 rst = &data->resets[i];
359 printf("%#05x: ", rst->reg);
360 val = readl(priv->base + rst->reg);
361 printf("%s\n", val > 0 ? "ON" : "");
362 }
363
364 dump_gplls(dev, priv->base);
365 dump_rcgs();
366}
367
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100368static struct clk_ops msm_clk_ops = {
369 .set_rate = msm_clk_set_rate,
Sumit Gargc9e384e2022-08-04 19:57:14 +0530370 .enable = msm_clk_enable,
Caleb Connollyd3888992023-10-02 16:56:21 +0100371 .dump_clks = msm_dump_clks,
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100372};
373
Konrad Dybciod9935732023-11-07 12:41:01 +0000374U_BOOT_DRIVER(qcom_clk) = {
375 .name = "qcom_clk",
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100376 .id = UCLASS_CLK,
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100377 .ops = &msm_clk_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700378 .priv_auto = sizeof(struct msm_clk_priv),
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100379 .probe = msm_clk_probe,
380};
Konrad Dybciod9935732023-11-07 12:41:01 +0000381
382int qcom_cc_bind(struct udevice *parent)
383{
384 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(parent);
385 struct udevice *clkdev, *rstdev;
386 struct driver *drv;
387 int ret;
388
389 /* Get a handle to the common clk handler */
390 drv = lists_driver_lookup_name("qcom_clk");
391 if (!drv)
392 return -ENOENT;
393
394 /* Register the clock controller */
395 ret = device_bind_with_driver_data(parent, drv, "qcom_clk", (ulong)data,
396 dev_ofnode(parent), &clkdev);
397 if (ret)
398 return ret;
399
400 /* Bail out early if resets are not specified for this platform */
401 if (!data->resets)
402 return ret;
403
404 /* Get a handle to the common reset handler */
405 drv = lists_driver_lookup_name("qcom_reset");
406 if (!drv)
407 return -ENOENT;
408
409 /* Register the reset controller */
410 ret = device_bind_with_driver_data(parent, drv, "qcom_reset", (ulong)data,
411 dev_ofnode(parent), &rstdev);
412 if (ret)
413 device_unbind(clkdev);
414
415 return ret;
416}
417
418static int qcom_reset_set(struct reset_ctl *rst, bool assert)
419{
420 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(rst->dev);
421 void __iomem *base = dev_get_priv(rst->dev);
422 const struct qcom_reset_map *map;
423 u32 value;
424
425 map = &data->resets[rst->id];
426
427 value = readl(base + map->reg);
428
429 if (assert)
430 value |= BIT(map->bit);
431 else
432 value &= ~BIT(map->bit);
433
434 writel(value, base + map->reg);
435
436 return 0;
437}
438
439static int qcom_reset_assert(struct reset_ctl *rst)
440{
441 return qcom_reset_set(rst, true);
442}
443
444static int qcom_reset_deassert(struct reset_ctl *rst)
445{
446 return qcom_reset_set(rst, false);
447}
448
449static const struct reset_ops qcom_reset_ops = {
450 .rst_assert = qcom_reset_assert,
451 .rst_deassert = qcom_reset_deassert,
452};
453
454static int qcom_reset_probe(struct udevice *dev)
455{
456 /* Set our priv pointer to the base address */
457 dev_set_priv(dev, (void *)dev_read_addr(dev));
458
459 return 0;
460}
461
462U_BOOT_DRIVER(qcom_reset) = {
463 .name = "qcom_reset",
464 .id = UCLASS_RESET,
465 .ops = &qcom_reset_ops,
466 .probe = qcom_reset_probe,
467};