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 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 15 | #include <clk-uclass.h> |
Caleb Connolly | 86d2839 | 2024-08-19 21:34:17 +0200 | [diff] [blame] | 16 | #include <linux/clk-provider.h> |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 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> |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 25 | #include <linux/iopoll.h> |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 26 | #include <reset-uclass.h> |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 27 | #include <power-domain-uclass.h> |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 28 | |
Caleb Connolly | 878b26a | 2023-11-07 12:40:59 +0000 | [diff] [blame] | 29 | #include "clock-qcom.h" |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 30 | |
| 31 | /* CBCR register fields */ |
| 32 | #define CBCR_BRANCH_ENABLE_BIT BIT(0) |
| 33 | #define CBCR_BRANCH_OFF_BIT BIT(31) |
| 34 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 35 | #define GDSC_SW_COLLAPSE_MASK BIT(0) |
| 36 | #define GDSC_POWER_DOWN_COMPLETE BIT(15) |
| 37 | #define GDSC_POWER_UP_COMPLETE BIT(16) |
| 38 | #define GDSC_PWR_ON_MASK BIT(31) |
| 39 | #define CFG_GDSCR_OFFSET 0x4 |
| 40 | #define GDSC_STATUS_POLL_TIMEOUT_US 1500 |
| 41 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 42 | /* Enable clock controlled by CBC soft macro */ |
| 43 | void clk_enable_cbc(phys_addr_t cbcr) |
| 44 | { |
| 45 | setbits_le32(cbcr, CBCR_BRANCH_ENABLE_BIT); |
| 46 | |
| 47 | while (readl(cbcr) & CBCR_BRANCH_OFF_BIT) |
| 48 | ; |
| 49 | } |
| 50 | |
Ramon Fried | ae29977 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 51 | 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] | 52 | { |
| 53 | if (readl(base + gpll0->status) & gpll0->status_bit) |
| 54 | return; /* clock already enabled */ |
| 55 | |
| 56 | setbits_le32(base + gpll0->ena_vote, gpll0->vote_bit); |
| 57 | |
| 58 | while ((readl(base + gpll0->status) & gpll0->status_bit) == 0) |
| 59 | ; |
| 60 | } |
| 61 | |
Ramon Fried | ae29977 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 62 | #define BRANCH_ON_VAL (0) |
| 63 | #define BRANCH_NOC_FSM_ON_VAL BIT(29) |
| 64 | #define BRANCH_CHECK_MASK GENMASK(31, 28) |
| 65 | |
| 66 | void clk_enable_vote_clk(phys_addr_t base, const struct vote_clk *vclk) |
| 67 | { |
| 68 | u32 val; |
| 69 | |
| 70 | setbits_le32(base + vclk->ena_vote, vclk->vote_bit); |
| 71 | do { |
| 72 | val = readl(base + vclk->cbcr_reg); |
| 73 | val &= BRANCH_CHECK_MASK; |
| 74 | } while ((val != BRANCH_ON_VAL) && (val != BRANCH_NOC_FSM_ON_VAL)); |
| 75 | } |
| 76 | |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 77 | #define APPS_CMD_RCGR_UPDATE BIT(0) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 78 | |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 79 | /* Update clock command via CMD_RCGR */ |
| 80 | void clk_bcr_update(phys_addr_t apps_cmd_rcgr) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 81 | { |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 82 | u32 count; |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 83 | setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 84 | |
| 85 | /* Wait for frequency to be updated. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 86 | for (count = 0; count < 50000; count++) { |
| 87 | if (!(readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE)) |
| 88 | break; |
| 89 | udelay(1); |
| 90 | } |
| 91 | WARN(count == 50000, "WARNING: RCG @ %#llx [%#010x] stuck at off\n", |
| 92 | apps_cmd_rcgr, readl(apps_cmd_rcgr)); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 95 | #define CFG_SRC_DIV_MASK 0b11111 |
| 96 | #define CFG_SRC_SEL_SHIFT 8 |
| 97 | #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT) |
| 98 | #define CFG_MODE_SHIFT 12 |
| 99 | #define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT) |
| 100 | #define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT) |
| 101 | #define CFG_HW_CLK_CTRL_MASK BIT(20) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 102 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 103 | /* |
| 104 | * root set rate for clocks with half integer and MND divider |
| 105 | * div should be pre-calculated ((div * 2) - 1) |
| 106 | */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 107 | void clk_rcg_set_rate_mnd(phys_addr_t base, uint32_t cmd_rcgr, |
Caleb Connolly | fbacc67 | 2023-11-07 12:41:04 +0000 | [diff] [blame] | 108 | int div, int m, int n, int source, u8 mnd_width) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 109 | { |
| 110 | u32 cfg; |
| 111 | /* M value for MND divider. */ |
| 112 | u32 m_val = m; |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 113 | u32 n_minus_m = n - m; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 114 | /* NOT(N-M) value for MND divider. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 115 | u32 n_val = ~n_minus_m * !!(n); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 116 | /* NOT 2D value for MND divider. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 117 | u32 d_val = ~(clamp_t(u32, n, m, n_minus_m)); |
Caleb Connolly | fbacc67 | 2023-11-07 12:41:04 +0000 | [diff] [blame] | 118 | u32 mask = BIT(mnd_width) - 1; |
| 119 | |
| 120 | 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] | 121 | |
| 122 | /* Program MND values */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 123 | writel(m_val & mask, base + cmd_rcgr + RCG_M_REG); |
| 124 | writel(n_val & mask, base + cmd_rcgr + RCG_N_REG); |
| 125 | writel(d_val & mask, base + cmd_rcgr + RCG_D_REG); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 126 | |
| 127 | /* setup src select and divider */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 128 | cfg = readl(base + cmd_rcgr + RCG_CFG_REG); |
Volodymyr Babchuk | 8eca261 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 129 | cfg &= ~(CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK | |
| 130 | CFG_SRC_DIV_MASK); |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 131 | cfg |= source & CFG_SRC_SEL_MASK; /* Select clock source */ |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 132 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 133 | if (div) |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 134 | cfg |= div & CFG_SRC_DIV_MASK; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 135 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 136 | if (n && n != m) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 137 | cfg |= CFG_MODE_DUAL_EDGE; |
| 138 | |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 139 | writel(cfg, base + cmd_rcgr + RCG_CFG_REG); /* Write new clock configuration */ |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 140 | |
| 141 | /* Inform h/w to start using the new config. */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 142 | clk_bcr_update(base + cmd_rcgr); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 145 | /* root set rate for clocks with half integer and mnd_width=0 */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 146 | void clk_rcg_set_rate(phys_addr_t base, uint32_t cmd_rcgr, int div, |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 147 | int source) |
| 148 | { |
| 149 | u32 cfg; |
| 150 | |
| 151 | /* setup src select and divider */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 152 | cfg = readl(base + cmd_rcgr + RCG_CFG_REG); |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 153 | 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] | 154 | cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */ |
| 155 | |
| 156 | /* |
| 157 | * Set the divider; HW permits fraction dividers (+0.5), but |
| 158 | * for simplicity, we will support integers only |
| 159 | */ |
| 160 | if (div) |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 161 | cfg |= (2 * div - 1) & CFG_SRC_DIV_MASK; |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 162 | |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 163 | writel(cfg, base + cmd_rcgr + RCG_CFG_REG); /* Write new clock configuration */ |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 164 | |
| 165 | /* Inform h/w to start using the new config. */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 166 | clk_bcr_update(base + cmd_rcgr); |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 167 | } |
| 168 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 169 | const struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, uint rate) |
| 170 | { |
| 171 | if (!f) |
| 172 | return NULL; |
| 173 | |
| 174 | if (!f->freq) |
| 175 | return f; |
| 176 | |
| 177 | for (; f->freq; f++) |
| 178 | if (rate <= f->freq) |
| 179 | return f; |
| 180 | |
| 181 | /* Default to our fastest rate */ |
| 182 | return f - 1; |
| 183 | } |
| 184 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 185 | static int msm_clk_probe(struct udevice *dev) |
| 186 | { |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 187 | 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] | 188 | struct msm_clk_priv *priv = dev_get_priv(dev); |
| 189 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 190 | priv->base = dev_read_addr(dev); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 191 | if (priv->base == FDT_ADDR_T_NONE) |
| 192 | return -EINVAL; |
| 193 | |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 194 | priv->data = data; |
| 195 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static ulong msm_clk_set_rate(struct clk *clk, ulong rate) |
| 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->set_rate) |
| 204 | return data->set_rate(clk, rate); |
| 205 | |
| 206 | return 0; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 209 | static int msm_clk_enable(struct clk *clk) |
| 210 | { |
Caleb Connolly | 10a0abb | 2023-11-07 12:41:03 +0000 | [diff] [blame] | 211 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev); |
| 212 | |
| 213 | if (data->enable) |
| 214 | return data->enable(clk); |
| 215 | |
| 216 | return 0; |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 217 | } |
| 218 | |
Caleb Connolly | 86d2839 | 2024-08-19 21:34:17 +0200 | [diff] [blame] | 219 | static void dump_gplls(struct udevice *dev, phys_addr_t base) |
| 220 | { |
| 221 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev); |
| 222 | u32 i; |
| 223 | bool locked; |
| 224 | u64 l, a, xo_rate = 19200000; |
| 225 | struct clk *clk = NULL; |
| 226 | struct udevice *xodev; |
| 227 | const phys_addr_t *gplls = data->dbg_pll_addrs; |
| 228 | |
| 229 | uclass_foreach_dev_probe(UCLASS_CLK, xodev) { |
| 230 | if (!strcmp(xodev->name, "xo-board") || !strcmp(xodev->name, "xo_board")) { |
| 231 | clk = dev_get_clk_ptr(xodev); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (clk) { |
| 237 | xo_rate = clk_get_rate(clk); |
| 238 | |
| 239 | /* On SDM845 this needs to be divided by 2 for some reason */ |
| 240 | if (xo_rate && of_machine_is_compatible("qcom,sdm845")) |
| 241 | xo_rate /= 2; |
| 242 | } else { |
| 243 | printf("Can't find XO clock, XO_BOARD rate may be wrong\n"); |
| 244 | } |
| 245 | |
| 246 | printf("GPLL clocks:\n"); |
| 247 | printf("| GPLL | LOCKED | XO_BOARD | PLL_L | ALPHA |\n"); |
| 248 | printf("+--------+--------+-----------+------------+----------------+\n"); |
| 249 | for (i = 0; i < data->num_plls; i++) { |
| 250 | locked = !!(readl(gplls[i]) & BIT(31)); |
| 251 | l = readl(gplls[i] + 4) & (BIT(16) - 1); |
| 252 | a = readq(gplls[i] + 40) & (BIT(16) - 1); |
| 253 | printf("| GPLL%-2d | %-6s | %9llu * (%#-9llx + %#-13llx * 2 ** -40 ) / 1000000\n", |
| 254 | i, locked ? "X" : "", xo_rate, l, a); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static void dump_rcgs(struct udevice *dev) |
| 259 | { |
| 260 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev); |
| 261 | int i; |
| 262 | u32 cmd; |
| 263 | u32 cfg; |
| 264 | u32 not_n_minus_m; |
| 265 | u32 src, m, n, div; |
| 266 | bool root_on, d_odd; |
| 267 | |
| 268 | printf("\nRCGs:\n"); |
| 269 | |
| 270 | /* |
| 271 | * Which GPLL SRC corresponds to depends on the parent map, see gcc-<soc>.c in Linux |
| 272 | * and find the parent map associated with the clock. Note that often there are multiple |
| 273 | * outputs from a single GPLL where one is actually half the rate of the other (_EVEN). |
| 274 | * intput_freq = associated GPLL output freq (potentially divided depending on SRC). |
| 275 | */ |
| 276 | printf("| NAME | ON | SRC | OUT_FREQ = input_freq * (m/n) * (1/d) | [CMD REG ] |\n"); |
| 277 | printf("+----------------------------------+----+-----+---------------------------------------+--------------+\n"); |
| 278 | for (i = 0; i < data->num_rcgs; i++) { |
| 279 | cmd = readl(data->dbg_rcg_addrs[i]); |
| 280 | cfg = readl(data->dbg_rcg_addrs[i] + 0x4); |
| 281 | m = readl(data->dbg_rcg_addrs[i] + 0x8); |
| 282 | n = 0; |
| 283 | not_n_minus_m = readl(data->dbg_rcg_addrs[i] + 0xc); |
| 284 | |
| 285 | root_on = !(cmd & BIT(31)); // ROOT_OFF |
| 286 | src = (cfg >> 8) & 7; |
| 287 | |
| 288 | if (not_n_minus_m) { |
| 289 | n = (~not_n_minus_m & 0xffff); |
| 290 | |
| 291 | /* A clumsy assumption that this is an 8-bit MND RCG */ |
| 292 | if ((n & 0xff00) == 0xff00) |
| 293 | n = n & 0xff; |
| 294 | |
| 295 | n += m; |
| 296 | } |
| 297 | |
| 298 | div = ((cfg & 0b11111) + 1) / 2; |
| 299 | d_odd = ((cfg & 0b11111) + 1) % 2 == 1; |
| 300 | printf("%-34s | %-2s | %3d | input_freq * (%4d/%5d) * (1/%1d%-2s) | [%#010x]\n", |
| 301 | data->dbg_rcg_names[i], root_on ? "X" : "", src, |
| 302 | m ?: 1, n ?: 1, div, d_odd ? ".5" : "", cmd); |
| 303 | } |
| 304 | |
| 305 | printf("\n"); |
| 306 | } |
| 307 | |
| 308 | static void __maybe_unused msm_dump_clks(struct udevice *dev) |
| 309 | { |
| 310 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(dev); |
| 311 | struct msm_clk_priv *priv = dev_get_priv(dev); |
| 312 | const struct gate_clk *sclk; |
| 313 | int val, i; |
| 314 | |
| 315 | if (!data->clks) { |
| 316 | printf("No clocks\n"); |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | printf("Gate Clocks:\n"); |
| 321 | for (i = 0; i < data->num_clks; i++) { |
| 322 | sclk = &data->clks[i]; |
| 323 | if (!sclk->name) |
| 324 | continue; |
| 325 | printf("%-32s: ", sclk->name); |
| 326 | val = readl(priv->base + sclk->reg) & sclk->en_val; |
| 327 | printf("%s\n", val ? "ON" : ""); |
| 328 | } |
| 329 | |
| 330 | dump_gplls(dev, priv->base); |
| 331 | dump_rcgs(dev); |
| 332 | } |
| 333 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 334 | static struct clk_ops msm_clk_ops = { |
| 335 | .set_rate = msm_clk_set_rate, |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 336 | .enable = msm_clk_enable, |
Caleb Connolly | 86d2839 | 2024-08-19 21:34:17 +0200 | [diff] [blame] | 337 | #if IS_ENABLED(CONFIG_CMD_CLK) |
| 338 | .dump = msm_dump_clks, |
| 339 | #endif |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 340 | }; |
| 341 | |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 342 | U_BOOT_DRIVER(qcom_clk) = { |
| 343 | .name = "qcom_clk", |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 344 | .id = UCLASS_CLK, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 345 | .ops = &msm_clk_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 346 | .priv_auto = sizeof(struct msm_clk_priv), |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 347 | .probe = msm_clk_probe, |
Caleb Connolly | e07ce56 | 2024-04-03 14:07:39 +0200 | [diff] [blame] | 348 | .flags = DM_FLAG_PRE_RELOC | DM_FLAG_DEFAULT_PD_CTRL_OFF, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 349 | }; |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 350 | |
| 351 | int qcom_cc_bind(struct udevice *parent) |
| 352 | { |
| 353 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(parent); |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 354 | struct udevice *clkdev = NULL, *rstdev = NULL, *pwrdev; |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 355 | struct driver *drv; |
| 356 | int ret; |
| 357 | |
| 358 | /* Get a handle to the common clk handler */ |
| 359 | drv = lists_driver_lookup_name("qcom_clk"); |
| 360 | if (!drv) |
| 361 | return -ENOENT; |
| 362 | |
| 363 | /* Register the clock controller */ |
| 364 | ret = device_bind_with_driver_data(parent, drv, "qcom_clk", (ulong)data, |
| 365 | dev_ofnode(parent), &clkdev); |
| 366 | if (ret) |
| 367 | return ret; |
| 368 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 369 | if (data->resets) { |
| 370 | /* Get a handle to the common reset handler */ |
| 371 | drv = lists_driver_lookup_name("qcom_reset"); |
| 372 | if (!drv) { |
| 373 | ret = -ENOENT; |
| 374 | goto unbind_clkdev; |
| 375 | } |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 376 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 377 | /* Register the reset controller */ |
| 378 | ret = device_bind_with_driver_data(parent, drv, "qcom_reset", (ulong)data, |
| 379 | dev_ofnode(parent), &rstdev); |
| 380 | if (ret) |
| 381 | goto unbind_clkdev; |
| 382 | } |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 383 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 384 | if (data->power_domains) { |
| 385 | /* Get a handle to the common power domain handler */ |
| 386 | drv = lists_driver_lookup_name("qcom_power"); |
| 387 | if (!drv) { |
| 388 | ret = -ENOENT; |
| 389 | goto unbind_rstdev; |
| 390 | } |
| 391 | /* Register the power domain controller */ |
| 392 | ret = device_bind_with_driver_data(parent, drv, "qcom_power", (ulong)data, |
| 393 | dev_ofnode(parent), &pwrdev); |
| 394 | if (ret) |
| 395 | goto unbind_rstdev; |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | |
| 400 | unbind_rstdev: |
| 401 | device_unbind(rstdev); |
| 402 | unbind_clkdev: |
| 403 | device_unbind(clkdev); |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 404 | |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | static int qcom_reset_set(struct reset_ctl *rst, bool assert) |
| 409 | { |
| 410 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(rst->dev); |
| 411 | void __iomem *base = dev_get_priv(rst->dev); |
| 412 | const struct qcom_reset_map *map; |
| 413 | u32 value; |
| 414 | |
| 415 | map = &data->resets[rst->id]; |
| 416 | |
| 417 | value = readl(base + map->reg); |
| 418 | |
| 419 | if (assert) |
| 420 | value |= BIT(map->bit); |
| 421 | else |
| 422 | value &= ~BIT(map->bit); |
| 423 | |
| 424 | writel(value, base + map->reg); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int qcom_reset_assert(struct reset_ctl *rst) |
| 430 | { |
| 431 | return qcom_reset_set(rst, true); |
| 432 | } |
| 433 | |
| 434 | static int qcom_reset_deassert(struct reset_ctl *rst) |
| 435 | { |
| 436 | return qcom_reset_set(rst, false); |
| 437 | } |
| 438 | |
| 439 | static const struct reset_ops qcom_reset_ops = { |
| 440 | .rst_assert = qcom_reset_assert, |
| 441 | .rst_deassert = qcom_reset_deassert, |
| 442 | }; |
| 443 | |
| 444 | static int qcom_reset_probe(struct udevice *dev) |
| 445 | { |
| 446 | /* Set our priv pointer to the base address */ |
| 447 | dev_set_priv(dev, (void *)dev_read_addr(dev)); |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | U_BOOT_DRIVER(qcom_reset) = { |
| 453 | .name = "qcom_reset", |
| 454 | .id = UCLASS_RESET, |
| 455 | .ops = &qcom_reset_ops, |
| 456 | .probe = qcom_reset_probe, |
| 457 | }; |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 458 | |
| 459 | static int qcom_power_set(struct power_domain *pwr, bool on) |
| 460 | { |
| 461 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(pwr->dev); |
| 462 | void __iomem *base = dev_get_priv(pwr->dev); |
| 463 | const struct qcom_power_map *map; |
| 464 | u32 value; |
| 465 | int ret; |
| 466 | |
| 467 | if (pwr->id >= data->num_power_domains) |
| 468 | return -ENODEV; |
| 469 | |
| 470 | map = &data->power_domains[pwr->id]; |
| 471 | |
| 472 | if (!map->reg) |
| 473 | return -ENODEV; |
| 474 | |
| 475 | value = readl(base + map->reg); |
| 476 | |
| 477 | if (on) |
| 478 | value &= ~GDSC_SW_COLLAPSE_MASK; |
| 479 | else |
| 480 | value |= GDSC_SW_COLLAPSE_MASK; |
| 481 | |
| 482 | writel(value, base + map->reg); |
| 483 | |
| 484 | if (on) |
| 485 | ret = readl_poll_timeout(base + map->reg + CFG_GDSCR_OFFSET, |
| 486 | value, |
| 487 | (value & GDSC_POWER_UP_COMPLETE) || |
| 488 | (value & GDSC_PWR_ON_MASK), |
| 489 | GDSC_STATUS_POLL_TIMEOUT_US); |
| 490 | |
| 491 | else |
| 492 | ret = readl_poll_timeout(base + map->reg + CFG_GDSCR_OFFSET, |
| 493 | value, |
| 494 | (value & GDSC_POWER_DOWN_COMPLETE) || |
| 495 | !(value & GDSC_PWR_ON_MASK), |
| 496 | GDSC_STATUS_POLL_TIMEOUT_US); |
| 497 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 498 | if (ret == -ETIMEDOUT) |
| 499 | printf("WARNING: GDSC %lu is stuck during power on/off\n", |
| 500 | pwr->id); |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | static int qcom_power_on(struct power_domain *pwr) |
| 505 | { |
| 506 | return qcom_power_set(pwr, true); |
| 507 | } |
| 508 | |
| 509 | static int qcom_power_off(struct power_domain *pwr) |
| 510 | { |
| 511 | return qcom_power_set(pwr, false); |
| 512 | } |
| 513 | |
| 514 | static const struct power_domain_ops qcom_power_ops = { |
| 515 | .on = qcom_power_on, |
| 516 | .off = qcom_power_off, |
| 517 | }; |
| 518 | |
| 519 | static int qcom_power_probe(struct udevice *dev) |
| 520 | { |
| 521 | /* Set our priv pointer to the base address */ |
| 522 | dev_set_priv(dev, (void *)dev_read_addr(dev)); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | U_BOOT_DRIVER(qcom_power) = { |
| 528 | .name = "qcom_power", |
| 529 | .id = UCLASS_POWER_DOMAIN, |
| 530 | .ops = &qcom_power_ops, |
| 531 | .probe = qcom_power_probe, |
Caleb Connolly | e07ce56 | 2024-04-03 14:07:39 +0200 | [diff] [blame] | 532 | .flags = DM_FLAG_PRE_RELOC, |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 533 | }; |