Konrad Dybcio | d993573 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause AND GPL-2.0 |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 2 | /* |
Konrad Dybcio | d993573 | 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 | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 5 | * |
| 6 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
Konrad Dybcio | d993573 | 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 | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 11 | * |
| 12 | * Based on Little Kernel driver, simplified |
Jorge Ramirez-Ortiz | 7c75f7f | 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 | d993573 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 18 | #include <dm/device-internal.h> |
| 19 | #include <dm/lists.h> |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <asm/io.h> |
| 22 | #include <linux/bitops.h> |
Konrad Dybcio | d993573 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 23 | #include <reset-uclass.h> |
| 24 | |
Caleb Connolly | 5bb0df6 | 2023-11-07 12:40:59 +0000 | [diff] [blame] | 25 | #include "clock-qcom.h" |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 26 | |
| 27 | /* CBCR register fields */ |
| 28 | #define CBCR_BRANCH_ENABLE_BIT BIT(0) |
| 29 | #define CBCR_BRANCH_OFF_BIT BIT(31) |
| 30 | |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 31 | /* Enable clock controlled by CBC soft macro */ |
| 32 | void 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 Fried | 640dc34 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 40 | void clk_enable_gpll0(phys_addr_t base, const struct pll_vote_clk *gpll0) |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 41 | { |
| 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 Fried | 640dc34 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 51 | #define BRANCH_ON_VAL (0) |
| 52 | #define BRANCH_NOC_FSM_ON_VAL BIT(29) |
| 53 | #define BRANCH_CHECK_MASK GENMASK(31, 28) |
| 54 | |
| 55 | void 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 Sun | 6d430e1 | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 66 | #define APPS_CMD_RCGR_UPDATE BIT(0) |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 67 | |
Sheep Sun | 6d430e1 | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 68 | /* Update clock command via CMD_RCGR */ |
| 69 | void clk_bcr_update(phys_addr_t apps_cmd_rcgr) |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 70 | { |
Sheep Sun | 6d430e1 | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 71 | setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE); |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 72 | |
| 73 | /* Wait for frequency to be updated. */ |
Sheep Sun | 6d430e1 | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 74 | while (readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE) |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 75 | ; |
| 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 */ |
| 85 | void clk_rcg_set_rate_mnd(phys_addr_t base, const struct bcr_regs *regs, |
Caleb Connolly | 97d7ed3 | 2023-11-07 12:41:04 +0000 | [diff] [blame^] | 86 | int div, int m, int n, int source, u8 mnd_width) |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 87 | { |
| 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 Connolly | 97d7ed3 | 2023-11-07 12:41:04 +0000 | [diff] [blame^] | 95 | 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-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 98 | |
| 99 | /* Program MND values */ |
Caleb Connolly | 97d7ed3 | 2023-11-07 12:41:04 +0000 | [diff] [blame^] | 100 | 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-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 103 | |
| 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 Garg | 22d3fcd | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 123 | /* root set rate for clocks with half integer and mnd_width=0 */ |
| 124 | void 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-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 147 | static int msm_clk_probe(struct udevice *dev) |
| 148 | { |
Konrad Dybcio | d993573 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 149 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev); |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 150 | struct msm_clk_priv *priv = dev_get_priv(dev); |
| 151 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 152 | priv->base = dev_read_addr(dev); |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 153 | if (priv->base == FDT_ADDR_T_NONE) |
| 154 | return -EINVAL; |
| 155 | |
Konrad Dybcio | d993573 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 156 | priv->data = data; |
| 157 | |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static ulong msm_clk_set_rate(struct clk *clk, ulong rate) |
| 162 | { |
Caleb Connolly | c94f9e9 | 2023-11-07 12:41:03 +0000 | [diff] [blame] | 163 | 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-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 169 | } |
| 170 | |
Sumit Garg | c9e384e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 171 | static int msm_clk_enable(struct clk *clk) |
| 172 | { |
Caleb Connolly | c94f9e9 | 2023-11-07 12:41:03 +0000 | [diff] [blame] | 173 | 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 Garg | c9e384e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 179 | } |
| 180 | |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 181 | static struct clk_ops msm_clk_ops = { |
| 182 | .set_rate = msm_clk_set_rate, |
Sumit Garg | c9e384e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 183 | .enable = msm_clk_enable, |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 184 | }; |
| 185 | |
Konrad Dybcio | d993573 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 186 | U_BOOT_DRIVER(qcom_clk) = { |
| 187 | .name = "qcom_clk", |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 188 | .id = UCLASS_CLK, |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 189 | .ops = &msm_clk_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 190 | .priv_auto = sizeof(struct msm_clk_priv), |
Jorge Ramirez-Ortiz | 7c75f7f | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 191 | .probe = msm_clk_probe, |
| 192 | }; |
Konrad Dybcio | d993573 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 193 | |
| 194 | int 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 | |
| 230 | static 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 | |
| 251 | static int qcom_reset_assert(struct reset_ctl *rst) |
| 252 | { |
| 253 | return qcom_reset_set(rst, true); |
| 254 | } |
| 255 | |
| 256 | static int qcom_reset_deassert(struct reset_ctl *rst) |
| 257 | { |
| 258 | return qcom_reset_set(rst, false); |
| 259 | } |
| 260 | |
| 261 | static const struct reset_ops qcom_reset_ops = { |
| 262 | .rst_assert = qcom_reset_assert, |
| 263 | .rst_deassert = qcom_reset_deassert, |
| 264 | }; |
| 265 | |
| 266 | static 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 | |
| 274 | U_BOOT_DRIVER(qcom_reset) = { |
| 275 | .name = "qcom_reset", |
| 276 | .id = UCLASS_RESET, |
| 277 | .ops = &qcom_reset_ops, |
| 278 | .probe = qcom_reset_probe, |
| 279 | }; |