blob: fc478554f9821a764b7ab539b459d8b0a6f33fb3 [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>
22#include <linux/bitops.h>
Konrad Dybciod9935732023-11-07 12:41:01 +000023#include <reset-uclass.h>
24
Caleb Connolly5bb0df62023-11-07 12:40:59 +000025#include "clock-qcom.h"
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010026
27/* CBCR register fields */
28#define CBCR_BRANCH_ENABLE_BIT BIT(0)
29#define CBCR_BRANCH_OFF_BIT BIT(31)
30
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010031/* Enable clock controlled by CBC soft macro */
32void clk_enable_cbc(phys_addr_t cbcr)
33{
34 setbits_le32(cbcr, CBCR_BRANCH_ENABLE_BIT);
35
36 while (readl(cbcr) & CBCR_BRANCH_OFF_BIT)
37 ;
38}
39
Ramon Fried640dc342018-05-16 12:13:39 +030040void clk_enable_gpll0(phys_addr_t base, const struct pll_vote_clk *gpll0)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010041{
42 if (readl(base + gpll0->status) & gpll0->status_bit)
43 return; /* clock already enabled */
44
45 setbits_le32(base + gpll0->ena_vote, gpll0->vote_bit);
46
47 while ((readl(base + gpll0->status) & gpll0->status_bit) == 0)
48 ;
49}
50
Ramon Fried640dc342018-05-16 12:13:39 +030051#define BRANCH_ON_VAL (0)
52#define BRANCH_NOC_FSM_ON_VAL BIT(29)
53#define BRANCH_CHECK_MASK GENMASK(31, 28)
54
55void clk_enable_vote_clk(phys_addr_t base, const struct vote_clk *vclk)
56{
57 u32 val;
58
59 setbits_le32(base + vclk->ena_vote, vclk->vote_bit);
60 do {
61 val = readl(base + vclk->cbcr_reg);
62 val &= BRANCH_CHECK_MASK;
63 } while ((val != BRANCH_ON_VAL) && (val != BRANCH_NOC_FSM_ON_VAL));
64}
65
Sheep Sun6d430e12021-06-20 10:34:35 +080066#define APPS_CMD_RCGR_UPDATE BIT(0)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010067
Sheep Sun6d430e12021-06-20 10:34:35 +080068/* Update clock command via CMD_RCGR */
69void clk_bcr_update(phys_addr_t apps_cmd_rcgr)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010070{
Sheep Sun6d430e12021-06-20 10:34:35 +080071 setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010072
73 /* Wait for frequency to be updated. */
Sheep Sun6d430e12021-06-20 10:34:35 +080074 while (readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010075 ;
76}
77
78#define CFG_MODE_DUAL_EDGE (0x2 << 12) /* Counter mode */
79
80#define CFG_MASK 0x3FFF
81
82#define CFG_DIVIDER_MASK 0x1F
83
84/* root set rate for clocks with half integer and MND divider */
85void clk_rcg_set_rate_mnd(phys_addr_t base, const struct bcr_regs *regs,
Caleb Connolly97d7ed32023-11-07 12:41:04 +000086 int div, int m, int n, int source, u8 mnd_width)
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +010087{
88 u32 cfg;
89 /* M value for MND divider. */
90 u32 m_val = m;
91 /* NOT(N-M) value for MND divider. */
92 u32 n_val = ~((n) - (m)) * !!(n);
93 /* NOT 2D value for MND divider. */
94 u32 d_val = ~(n);
Caleb Connolly97d7ed32023-11-07 12:41:04 +000095 u32 mask = BIT(mnd_width) - 1;
96
97 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 +010098
99 /* Program MND values */
Caleb Connolly97d7ed32023-11-07 12:41:04 +0000100 writel(m_val & mask, base + regs->M);
101 writel(n_val & mask, base + regs->N);
102 writel(d_val & mask, base + regs->D);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100103
104 /* setup src select and divider */
105 cfg = readl(base + regs->cfg_rcgr);
106 cfg &= ~CFG_MASK;
107 cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */
108
109 /* Set the divider; HW permits fraction dividers (+0.5), but
110 for simplicity, we will support integers only */
111 if (div)
112 cfg |= (2 * div - 1) & CFG_DIVIDER_MASK;
113
114 if (n_val)
115 cfg |= CFG_MODE_DUAL_EDGE;
116
117 writel(cfg, base + regs->cfg_rcgr); /* Write new clock configuration */
118
119 /* Inform h/w to start using the new config. */
120 clk_bcr_update(base + regs->cmd_rcgr);
121}
122
Sumit Garg22d3fcd2023-02-01 19:28:57 +0530123/* root set rate for clocks with half integer and mnd_width=0 */
124void clk_rcg_set_rate(phys_addr_t base, const struct bcr_regs *regs, int div,
125 int source)
126{
127 u32 cfg;
128
129 /* setup src select and divider */
130 cfg = readl(base + regs->cfg_rcgr);
131 cfg &= ~CFG_MASK;
132 cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */
133
134 /*
135 * Set the divider; HW permits fraction dividers (+0.5), but
136 * for simplicity, we will support integers only
137 */
138 if (div)
139 cfg |= (2 * div - 1) & CFG_DIVIDER_MASK;
140
141 writel(cfg, base + regs->cfg_rcgr); /* Write new clock configuration */
142
143 /* Inform h/w to start using the new config. */
144 clk_bcr_update(base + regs->cmd_rcgr);
145}
146
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100147static int msm_clk_probe(struct udevice *dev)
148{
Konrad Dybciod9935732023-11-07 12:41:01 +0000149 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100150 struct msm_clk_priv *priv = dev_get_priv(dev);
151
Masahiro Yamada25484932020-07-17 14:36:48 +0900152 priv->base = dev_read_addr(dev);
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100153 if (priv->base == FDT_ADDR_T_NONE)
154 return -EINVAL;
155
Konrad Dybciod9935732023-11-07 12:41:01 +0000156 priv->data = data;
157
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100158 return 0;
159}
160
161static ulong msm_clk_set_rate(struct clk *clk, ulong rate)
162{
Caleb Connollyc94f9e92023-11-07 12:41:03 +0000163 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev);
164
165 if (data->set_rate)
166 return data->set_rate(clk, rate);
167
168 return 0;
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100169}
170
Sumit Gargc9e384e2022-08-04 19:57:14 +0530171static int msm_clk_enable(struct clk *clk)
172{
Caleb Connollyc94f9e92023-11-07 12:41:03 +0000173 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev);
174
175 if (data->enable)
176 return data->enable(clk);
177
178 return 0;
Sumit Gargc9e384e2022-08-04 19:57:14 +0530179}
180
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100181static struct clk_ops msm_clk_ops = {
182 .set_rate = msm_clk_set_rate,
Sumit Gargc9e384e2022-08-04 19:57:14 +0530183 .enable = msm_clk_enable,
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100184};
185
Konrad Dybciod9935732023-11-07 12:41:01 +0000186U_BOOT_DRIVER(qcom_clk) = {
187 .name = "qcom_clk",
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100188 .id = UCLASS_CLK,
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100189 .ops = &msm_clk_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700190 .priv_auto = sizeof(struct msm_clk_priv),
Jorge Ramirez-Ortiz7c75f7f2018-01-10 11:33:49 +0100191 .probe = msm_clk_probe,
192};
Konrad Dybciod9935732023-11-07 12:41:01 +0000193
194int qcom_cc_bind(struct udevice *parent)
195{
196 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(parent);
197 struct udevice *clkdev, *rstdev;
198 struct driver *drv;
199 int ret;
200
201 /* Get a handle to the common clk handler */
202 drv = lists_driver_lookup_name("qcom_clk");
203 if (!drv)
204 return -ENOENT;
205
206 /* Register the clock controller */
207 ret = device_bind_with_driver_data(parent, drv, "qcom_clk", (ulong)data,
208 dev_ofnode(parent), &clkdev);
209 if (ret)
210 return ret;
211
212 /* Bail out early if resets are not specified for this platform */
213 if (!data->resets)
214 return ret;
215
216 /* Get a handle to the common reset handler */
217 drv = lists_driver_lookup_name("qcom_reset");
218 if (!drv)
219 return -ENOENT;
220
221 /* Register the reset controller */
222 ret = device_bind_with_driver_data(parent, drv, "qcom_reset", (ulong)data,
223 dev_ofnode(parent), &rstdev);
224 if (ret)
225 device_unbind(clkdev);
226
227 return ret;
228}
229
230static int qcom_reset_set(struct reset_ctl *rst, bool assert)
231{
232 struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(rst->dev);
233 void __iomem *base = dev_get_priv(rst->dev);
234 const struct qcom_reset_map *map;
235 u32 value;
236
237 map = &data->resets[rst->id];
238
239 value = readl(base + map->reg);
240
241 if (assert)
242 value |= BIT(map->bit);
243 else
244 value &= ~BIT(map->bit);
245
246 writel(value, base + map->reg);
247
248 return 0;
249}
250
251static int qcom_reset_assert(struct reset_ctl *rst)
252{
253 return qcom_reset_set(rst, true);
254}
255
256static int qcom_reset_deassert(struct reset_ctl *rst)
257{
258 return qcom_reset_set(rst, false);
259}
260
261static const struct reset_ops qcom_reset_ops = {
262 .rst_assert = qcom_reset_assert,
263 .rst_deassert = qcom_reset_deassert,
264};
265
266static int qcom_reset_probe(struct udevice *dev)
267{
268 /* Set our priv pointer to the base address */
269 dev_set_priv(dev, (void *)dev_read_addr(dev));
270
271 return 0;
272}
273
274U_BOOT_DRIVER(qcom_reset) = {
275 .name = "qcom_reset",
276 .id = UCLASS_RESET,
277 .ops = &qcom_reset_ops,
278 .probe = qcom_reset_probe,
279};