Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause AND GPL-2.0 |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 2 | /* |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 3 | * Clock and reset drivers for Qualcomm platforms Global Clock |
| 4 | * Controller (GCC). |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 5 | * |
| 6 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 7 | * (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-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 11 | * |
| 12 | * Based on Little Kernel driver, simplified |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <clk-uclass.h> |
| 17 | #include <dm.h> |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/lists.h> |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <asm/io.h> |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 22 | #include <linux/bug.h> |
| 23 | #include <linux/delay.h> |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 25 | #include <reset-uclass.h> |
| 26 | |
Caleb Connolly | 878b26a | 2023-11-07 12:40:59 +0000 | [diff] [blame] | 27 | #include "clock-qcom.h" |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 28 | |
| 29 | /* CBCR register fields */ |
| 30 | #define CBCR_BRANCH_ENABLE_BIT BIT(0) |
| 31 | #define CBCR_BRANCH_OFF_BIT BIT(31) |
| 32 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 33 | /* Enable clock controlled by CBC soft macro */ |
| 34 | void 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 | |
Ramon Fried | ae29977 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 42 | void clk_enable_gpll0(phys_addr_t base, const struct pll_vote_clk *gpll0) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 43 | { |
| 44 | if (readl(base + gpll0->status) & gpll0->status_bit) |
| 45 | return; /* clock already enabled */ |
| 46 | |
| 47 | setbits_le32(base + gpll0->ena_vote, gpll0->vote_bit); |
| 48 | |
| 49 | while ((readl(base + gpll0->status) & gpll0->status_bit) == 0) |
| 50 | ; |
| 51 | } |
| 52 | |
Ramon Fried | ae29977 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 53 | #define BRANCH_ON_VAL (0) |
| 54 | #define BRANCH_NOC_FSM_ON_VAL BIT(29) |
| 55 | #define BRANCH_CHECK_MASK GENMASK(31, 28) |
| 56 | |
| 57 | void clk_enable_vote_clk(phys_addr_t base, const struct vote_clk *vclk) |
| 58 | { |
| 59 | u32 val; |
| 60 | |
| 61 | setbits_le32(base + vclk->ena_vote, vclk->vote_bit); |
| 62 | do { |
| 63 | val = readl(base + vclk->cbcr_reg); |
| 64 | val &= BRANCH_CHECK_MASK; |
| 65 | } while ((val != BRANCH_ON_VAL) && (val != BRANCH_NOC_FSM_ON_VAL)); |
| 66 | } |
| 67 | |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 68 | #define APPS_CMD_RCGR_UPDATE BIT(0) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 69 | |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 70 | /* Update clock command via CMD_RCGR */ |
| 71 | void clk_bcr_update(phys_addr_t apps_cmd_rcgr) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 72 | { |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 73 | u32 count; |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 74 | setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 75 | |
| 76 | /* Wait for frequency to be updated. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 77 | for (count = 0; count < 50000; count++) { |
| 78 | if (!(readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE)) |
| 79 | break; |
| 80 | udelay(1); |
| 81 | } |
| 82 | WARN(count == 50000, "WARNING: RCG @ %#llx [%#010x] stuck at off\n", |
| 83 | apps_cmd_rcgr, readl(apps_cmd_rcgr)); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 86 | #define CFG_SRC_DIV_MASK 0b11111 |
| 87 | #define CFG_SRC_SEL_SHIFT 8 |
| 88 | #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT) |
| 89 | #define CFG_MODE_SHIFT 12 |
| 90 | #define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT) |
| 91 | #define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT) |
| 92 | #define CFG_HW_CLK_CTRL_MASK BIT(20) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 93 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 94 | /* |
| 95 | * root set rate for clocks with half integer and MND divider |
| 96 | * div should be pre-calculated ((div * 2) - 1) |
| 97 | */ |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 98 | void clk_rcg_set_rate_mnd(phys_addr_t base, const struct bcr_regs *regs, |
Caleb Connolly | fbacc67 | 2023-11-07 12:41:04 +0000 | [diff] [blame] | 99 | int div, int m, int n, int source, u8 mnd_width) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 100 | { |
| 101 | u32 cfg; |
| 102 | /* M value for MND divider. */ |
| 103 | u32 m_val = m; |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 104 | u32 n_minus_m = n - m; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 105 | /* NOT(N-M) value for MND divider. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 106 | u32 n_val = ~n_minus_m * !!(n); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 107 | /* NOT 2D value for MND divider. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 108 | u32 d_val = ~(clamp_t(u32, n, m, n_minus_m)); |
Caleb Connolly | fbacc67 | 2023-11-07 12:41:04 +0000 | [diff] [blame] | 109 | u32 mask = BIT(mnd_width) - 1; |
| 110 | |
| 111 | debug("m %#x n %#x d %#x div %#x mask %#x\n", m_val, n_val, d_val, div, mask); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 112 | |
| 113 | /* Program MND values */ |
Caleb Connolly | fbacc67 | 2023-11-07 12:41:04 +0000 | [diff] [blame] | 114 | writel(m_val & mask, base + regs->M); |
| 115 | writel(n_val & mask, base + regs->N); |
| 116 | writel(d_val & mask, base + regs->D); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 117 | |
| 118 | /* setup src select and divider */ |
| 119 | cfg = readl(base + regs->cfg_rcgr); |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 120 | cfg &= ~(CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK); |
| 121 | cfg |= source & CFG_SRC_SEL_MASK; /* Select clock source */ |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 122 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 123 | if (div) |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 124 | cfg |= div & CFG_SRC_DIV_MASK; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 125 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 126 | if (n && n != m) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 127 | cfg |= CFG_MODE_DUAL_EDGE; |
| 128 | |
| 129 | writel(cfg, base + regs->cfg_rcgr); /* Write new clock configuration */ |
| 130 | |
| 131 | /* Inform h/w to start using the new config. */ |
| 132 | clk_bcr_update(base + regs->cmd_rcgr); |
| 133 | } |
| 134 | |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 135 | /* root set rate for clocks with half integer and mnd_width=0 */ |
| 136 | void clk_rcg_set_rate(phys_addr_t base, const struct bcr_regs *regs, int div, |
| 137 | int source) |
| 138 | { |
| 139 | u32 cfg; |
| 140 | |
| 141 | /* setup src select and divider */ |
| 142 | cfg = readl(base + regs->cfg_rcgr); |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 143 | cfg &= ~(CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK); |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 144 | cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */ |
| 145 | |
| 146 | /* |
| 147 | * Set the divider; HW permits fraction dividers (+0.5), but |
| 148 | * for simplicity, we will support integers only |
| 149 | */ |
| 150 | if (div) |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 151 | cfg |= (2 * div - 1) & CFG_SRC_DIV_MASK; |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 152 | |
| 153 | writel(cfg, base + regs->cfg_rcgr); /* Write new clock configuration */ |
| 154 | |
| 155 | /* Inform h/w to start using the new config. */ |
| 156 | clk_bcr_update(base + regs->cmd_rcgr); |
| 157 | } |
| 158 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 159 | const struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, uint rate) |
| 160 | { |
| 161 | if (!f) |
| 162 | return NULL; |
| 163 | |
| 164 | if (!f->freq) |
| 165 | return f; |
| 166 | |
| 167 | for (; f->freq; f++) |
| 168 | if (rate <= f->freq) |
| 169 | return f; |
| 170 | |
| 171 | /* Default to our fastest rate */ |
| 172 | return f - 1; |
| 173 | } |
| 174 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 175 | static int msm_clk_probe(struct udevice *dev) |
| 176 | { |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 177 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 178 | struct msm_clk_priv *priv = dev_get_priv(dev); |
| 179 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 180 | priv->base = dev_read_addr(dev); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 181 | if (priv->base == FDT_ADDR_T_NONE) |
| 182 | return -EINVAL; |
| 183 | |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 184 | priv->data = data; |
| 185 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static ulong msm_clk_set_rate(struct clk *clk, ulong rate) |
| 190 | { |
Caleb Connolly | 10a0abb | 2023-11-07 12:41:03 +0000 | [diff] [blame] | 191 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev); |
| 192 | |
| 193 | if (data->set_rate) |
| 194 | return data->set_rate(clk, rate); |
| 195 | |
| 196 | return 0; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 197 | } |
| 198 | |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 199 | static int msm_clk_enable(struct clk *clk) |
| 200 | { |
Caleb Connolly | 10a0abb | 2023-11-07 12:41:03 +0000 | [diff] [blame] | 201 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev); |
| 202 | |
| 203 | if (data->enable) |
| 204 | return data->enable(clk); |
| 205 | |
| 206 | return 0; |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 207 | } |
| 208 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 209 | static struct clk_ops msm_clk_ops = { |
| 210 | .set_rate = msm_clk_set_rate, |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 211 | .enable = msm_clk_enable, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 212 | }; |
| 213 | |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 214 | U_BOOT_DRIVER(qcom_clk) = { |
| 215 | .name = "qcom_clk", |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 216 | .id = UCLASS_CLK, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 217 | .ops = &msm_clk_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 218 | .priv_auto = sizeof(struct msm_clk_priv), |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 219 | .probe = msm_clk_probe, |
| 220 | }; |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 221 | |
| 222 | int qcom_cc_bind(struct udevice *parent) |
| 223 | { |
| 224 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(parent); |
| 225 | struct udevice *clkdev, *rstdev; |
| 226 | struct driver *drv; |
| 227 | int ret; |
| 228 | |
| 229 | /* Get a handle to the common clk handler */ |
| 230 | drv = lists_driver_lookup_name("qcom_clk"); |
| 231 | if (!drv) |
| 232 | return -ENOENT; |
| 233 | |
| 234 | /* Register the clock controller */ |
| 235 | ret = device_bind_with_driver_data(parent, drv, "qcom_clk", (ulong)data, |
| 236 | dev_ofnode(parent), &clkdev); |
| 237 | if (ret) |
| 238 | return ret; |
| 239 | |
| 240 | /* Bail out early if resets are not specified for this platform */ |
| 241 | if (!data->resets) |
| 242 | return ret; |
| 243 | |
| 244 | /* Get a handle to the common reset handler */ |
| 245 | drv = lists_driver_lookup_name("qcom_reset"); |
| 246 | if (!drv) |
| 247 | return -ENOENT; |
| 248 | |
| 249 | /* Register the reset controller */ |
| 250 | ret = device_bind_with_driver_data(parent, drv, "qcom_reset", (ulong)data, |
| 251 | dev_ofnode(parent), &rstdev); |
| 252 | if (ret) |
| 253 | device_unbind(clkdev); |
| 254 | |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | static int qcom_reset_set(struct reset_ctl *rst, bool assert) |
| 259 | { |
| 260 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(rst->dev); |
| 261 | void __iomem *base = dev_get_priv(rst->dev); |
| 262 | const struct qcom_reset_map *map; |
| 263 | u32 value; |
| 264 | |
| 265 | map = &data->resets[rst->id]; |
| 266 | |
| 267 | value = readl(base + map->reg); |
| 268 | |
| 269 | if (assert) |
| 270 | value |= BIT(map->bit); |
| 271 | else |
| 272 | value &= ~BIT(map->bit); |
| 273 | |
| 274 | writel(value, base + map->reg); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int qcom_reset_assert(struct reset_ctl *rst) |
| 280 | { |
| 281 | return qcom_reset_set(rst, true); |
| 282 | } |
| 283 | |
| 284 | static int qcom_reset_deassert(struct reset_ctl *rst) |
| 285 | { |
| 286 | return qcom_reset_set(rst, false); |
| 287 | } |
| 288 | |
| 289 | static const struct reset_ops qcom_reset_ops = { |
| 290 | .rst_assert = qcom_reset_assert, |
| 291 | .rst_deassert = qcom_reset_deassert, |
| 292 | }; |
| 293 | |
| 294 | static int qcom_reset_probe(struct udevice *dev) |
| 295 | { |
| 296 | /* Set our priv pointer to the base address */ |
| 297 | dev_set_priv(dev, (void *)dev_read_addr(dev)); |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | U_BOOT_DRIVER(qcom_reset) = { |
| 303 | .name = "qcom_reset", |
| 304 | .id = UCLASS_RESET, |
| 305 | .ops = &qcom_reset_ops, |
| 306 | .probe = qcom_reset_probe, |
| 307 | }; |