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> |
| 16 | #include <dm.h> |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
| 18 | #include <dm/lists.h> |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <asm/io.h> |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 21 | #include <linux/bug.h> |
| 22 | #include <linux/delay.h> |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 23 | #include <linux/bitops.h> |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 24 | #include <linux/iopoll.h> |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 25 | #include <reset-uclass.h> |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 26 | #include <power-domain-uclass.h> |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 27 | |
Caleb Connolly | 878b26a | 2023-11-07 12:40:59 +0000 | [diff] [blame] | 28 | #include "clock-qcom.h" |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 29 | |
| 30 | /* CBCR register fields */ |
| 31 | #define CBCR_BRANCH_ENABLE_BIT BIT(0) |
| 32 | #define CBCR_BRANCH_OFF_BIT BIT(31) |
| 33 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 34 | #define GDSC_SW_COLLAPSE_MASK BIT(0) |
| 35 | #define GDSC_POWER_DOWN_COMPLETE BIT(15) |
| 36 | #define GDSC_POWER_UP_COMPLETE BIT(16) |
| 37 | #define GDSC_PWR_ON_MASK BIT(31) |
| 38 | #define CFG_GDSCR_OFFSET 0x4 |
| 39 | #define GDSC_STATUS_POLL_TIMEOUT_US 1500 |
| 40 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 41 | /* Enable clock controlled by CBC soft macro */ |
| 42 | void clk_enable_cbc(phys_addr_t cbcr) |
| 43 | { |
| 44 | setbits_le32(cbcr, CBCR_BRANCH_ENABLE_BIT); |
| 45 | |
| 46 | while (readl(cbcr) & CBCR_BRANCH_OFF_BIT) |
| 47 | ; |
| 48 | } |
| 49 | |
Ramon Fried | ae29977 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 50 | 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] | 51 | { |
| 52 | if (readl(base + gpll0->status) & gpll0->status_bit) |
| 53 | return; /* clock already enabled */ |
| 54 | |
| 55 | setbits_le32(base + gpll0->ena_vote, gpll0->vote_bit); |
| 56 | |
| 57 | while ((readl(base + gpll0->status) & gpll0->status_bit) == 0) |
| 58 | ; |
| 59 | } |
| 60 | |
Ramon Fried | ae29977 | 2018-05-16 12:13:39 +0300 | [diff] [blame] | 61 | #define BRANCH_ON_VAL (0) |
| 62 | #define BRANCH_NOC_FSM_ON_VAL BIT(29) |
| 63 | #define BRANCH_CHECK_MASK GENMASK(31, 28) |
| 64 | |
| 65 | void clk_enable_vote_clk(phys_addr_t base, const struct vote_clk *vclk) |
| 66 | { |
| 67 | u32 val; |
| 68 | |
| 69 | setbits_le32(base + vclk->ena_vote, vclk->vote_bit); |
| 70 | do { |
| 71 | val = readl(base + vclk->cbcr_reg); |
| 72 | val &= BRANCH_CHECK_MASK; |
| 73 | } while ((val != BRANCH_ON_VAL) && (val != BRANCH_NOC_FSM_ON_VAL)); |
| 74 | } |
| 75 | |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 76 | #define APPS_CMD_RCGR_UPDATE BIT(0) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 77 | |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 78 | /* Update clock command via CMD_RCGR */ |
| 79 | void clk_bcr_update(phys_addr_t apps_cmd_rcgr) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 80 | { |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 81 | u32 count; |
Sheep Sun | 49fee7b | 2021-06-20 10:34:35 +0800 | [diff] [blame] | 82 | setbits_le32(apps_cmd_rcgr, APPS_CMD_RCGR_UPDATE); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 83 | |
| 84 | /* Wait for frequency to be updated. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 85 | for (count = 0; count < 50000; count++) { |
| 86 | if (!(readl(apps_cmd_rcgr) & APPS_CMD_RCGR_UPDATE)) |
| 87 | break; |
| 88 | udelay(1); |
| 89 | } |
| 90 | WARN(count == 50000, "WARNING: RCG @ %#llx [%#010x] stuck at off\n", |
| 91 | apps_cmd_rcgr, readl(apps_cmd_rcgr)); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 92 | } |
| 93 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 94 | #define CFG_SRC_DIV_MASK 0b11111 |
| 95 | #define CFG_SRC_SEL_SHIFT 8 |
| 96 | #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT) |
| 97 | #define CFG_MODE_SHIFT 12 |
| 98 | #define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT) |
| 99 | #define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT) |
| 100 | #define CFG_HW_CLK_CTRL_MASK BIT(20) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 101 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 102 | /* |
| 103 | * root set rate for clocks with half integer and MND divider |
| 104 | * div should be pre-calculated ((div * 2) - 1) |
| 105 | */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 106 | 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] | 107 | int div, int m, int n, int source, u8 mnd_width) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 108 | { |
| 109 | u32 cfg; |
| 110 | /* M value for MND divider. */ |
| 111 | u32 m_val = m; |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 112 | u32 n_minus_m = n - m; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 113 | /* NOT(N-M) value for MND divider. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 114 | u32 n_val = ~n_minus_m * !!(n); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 115 | /* NOT 2D value for MND divider. */ |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 116 | u32 d_val = ~(clamp_t(u32, n, m, n_minus_m)); |
Caleb Connolly | fbacc67 | 2023-11-07 12:41:04 +0000 | [diff] [blame] | 117 | u32 mask = BIT(mnd_width) - 1; |
| 118 | |
| 119 | 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] | 120 | |
| 121 | /* Program MND values */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 122 | writel(m_val & mask, base + cmd_rcgr + RCG_M_REG); |
| 123 | writel(n_val & mask, base + cmd_rcgr + RCG_N_REG); |
| 124 | writel(d_val & mask, base + cmd_rcgr + RCG_D_REG); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 125 | |
| 126 | /* setup src select and divider */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 127 | cfg = readl(base + cmd_rcgr + RCG_CFG_REG); |
Volodymyr Babchuk | 8eca261 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 128 | cfg &= ~(CFG_SRC_SEL_MASK | CFG_MODE_MASK | CFG_HW_CLK_CTRL_MASK | |
| 129 | CFG_SRC_DIV_MASK); |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 130 | cfg |= source & CFG_SRC_SEL_MASK; /* Select clock source */ |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 131 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 132 | if (div) |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 133 | cfg |= div & CFG_SRC_DIV_MASK; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 134 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 135 | if (n && n != m) |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 136 | cfg |= CFG_MODE_DUAL_EDGE; |
| 137 | |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 138 | 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] | 139 | |
| 140 | /* Inform h/w to start using the new config. */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 141 | clk_bcr_update(base + cmd_rcgr); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 144 | /* root set rate for clocks with half integer and mnd_width=0 */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 145 | 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] | 146 | int source) |
| 147 | { |
| 148 | u32 cfg; |
| 149 | |
| 150 | /* setup src select and divider */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 151 | cfg = readl(base + cmd_rcgr + RCG_CFG_REG); |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 152 | 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] | 153 | cfg |= source & CFG_CLK_SRC_MASK; /* Select clock source */ |
| 154 | |
| 155 | /* |
| 156 | * Set the divider; HW permits fraction dividers (+0.5), but |
| 157 | * for simplicity, we will support integers only |
| 158 | */ |
| 159 | if (div) |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 160 | cfg |= (2 * div - 1) & CFG_SRC_DIV_MASK; |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 161 | |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 162 | writel(cfg, base + cmd_rcgr + RCG_CFG_REG); /* Write new clock configuration */ |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 163 | |
| 164 | /* Inform h/w to start using the new config. */ |
Caleb Connolly | cbdad44 | 2024-04-03 14:07:40 +0200 | [diff] [blame] | 165 | clk_bcr_update(base + cmd_rcgr); |
Sumit Garg | a3e804d | 2023-02-01 19:28:57 +0530 | [diff] [blame] | 166 | } |
| 167 | |
Caleb Connolly | 397c84f | 2023-11-07 12:41:05 +0000 | [diff] [blame] | 168 | const struct freq_tbl *qcom_find_freq(const struct freq_tbl *f, uint rate) |
| 169 | { |
| 170 | if (!f) |
| 171 | return NULL; |
| 172 | |
| 173 | if (!f->freq) |
| 174 | return f; |
| 175 | |
| 176 | for (; f->freq; f++) |
| 177 | if (rate <= f->freq) |
| 178 | return f; |
| 179 | |
| 180 | /* Default to our fastest rate */ |
| 181 | return f - 1; |
| 182 | } |
| 183 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 184 | static int msm_clk_probe(struct udevice *dev) |
| 185 | { |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 186 | 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] | 187 | struct msm_clk_priv *priv = dev_get_priv(dev); |
| 188 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 189 | priv->base = dev_read_addr(dev); |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 190 | if (priv->base == FDT_ADDR_T_NONE) |
| 191 | return -EINVAL; |
| 192 | |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 193 | priv->data = data; |
| 194 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static ulong msm_clk_set_rate(struct clk *clk, ulong rate) |
| 199 | { |
Caleb Connolly | 10a0abb | 2023-11-07 12:41:03 +0000 | [diff] [blame] | 200 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev); |
| 201 | |
| 202 | if (data->set_rate) |
| 203 | return data->set_rate(clk, rate); |
| 204 | |
| 205 | return 0; |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 208 | static int msm_clk_enable(struct clk *clk) |
| 209 | { |
Caleb Connolly | 10a0abb | 2023-11-07 12:41:03 +0000 | [diff] [blame] | 210 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(clk->dev); |
| 211 | |
| 212 | if (data->enable) |
| 213 | return data->enable(clk); |
| 214 | |
| 215 | return 0; |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 216 | } |
| 217 | |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 218 | static struct clk_ops msm_clk_ops = { |
| 219 | .set_rate = msm_clk_set_rate, |
Sumit Garg | 1d1ca6e | 2022-08-04 19:57:14 +0530 | [diff] [blame] | 220 | .enable = msm_clk_enable, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 221 | }; |
| 222 | |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 223 | U_BOOT_DRIVER(qcom_clk) = { |
| 224 | .name = "qcom_clk", |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 225 | .id = UCLASS_CLK, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 226 | .ops = &msm_clk_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 227 | .priv_auto = sizeof(struct msm_clk_priv), |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 228 | .probe = msm_clk_probe, |
Caleb Connolly | e07ce56 | 2024-04-03 14:07:39 +0200 | [diff] [blame] | 229 | .flags = DM_FLAG_PRE_RELOC | DM_FLAG_DEFAULT_PD_CTRL_OFF, |
Jorge Ramirez-Ortiz | 92c1eff | 2018-01-10 11:33:49 +0100 | [diff] [blame] | 230 | }; |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 231 | |
| 232 | int qcom_cc_bind(struct udevice *parent) |
| 233 | { |
| 234 | 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] | 235 | struct udevice *clkdev = NULL, *rstdev = NULL, *pwrdev; |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 236 | struct driver *drv; |
| 237 | int ret; |
| 238 | |
| 239 | /* Get a handle to the common clk handler */ |
| 240 | drv = lists_driver_lookup_name("qcom_clk"); |
| 241 | if (!drv) |
| 242 | return -ENOENT; |
| 243 | |
| 244 | /* Register the clock controller */ |
| 245 | ret = device_bind_with_driver_data(parent, drv, "qcom_clk", (ulong)data, |
| 246 | dev_ofnode(parent), &clkdev); |
| 247 | if (ret) |
| 248 | return ret; |
| 249 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 250 | if (data->resets) { |
| 251 | /* Get a handle to the common reset handler */ |
| 252 | drv = lists_driver_lookup_name("qcom_reset"); |
| 253 | if (!drv) { |
| 254 | ret = -ENOENT; |
| 255 | goto unbind_clkdev; |
| 256 | } |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 257 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 258 | /* Register the reset controller */ |
| 259 | ret = device_bind_with_driver_data(parent, drv, "qcom_reset", (ulong)data, |
| 260 | dev_ofnode(parent), &rstdev); |
| 261 | if (ret) |
| 262 | goto unbind_clkdev; |
| 263 | } |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 264 | |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 265 | if (data->power_domains) { |
| 266 | /* Get a handle to the common power domain handler */ |
| 267 | drv = lists_driver_lookup_name("qcom_power"); |
| 268 | if (!drv) { |
| 269 | ret = -ENOENT; |
| 270 | goto unbind_rstdev; |
| 271 | } |
| 272 | /* Register the power domain controller */ |
| 273 | ret = device_bind_with_driver_data(parent, drv, "qcom_power", (ulong)data, |
| 274 | dev_ofnode(parent), &pwrdev); |
| 275 | if (ret) |
| 276 | goto unbind_rstdev; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | |
| 281 | unbind_rstdev: |
| 282 | device_unbind(rstdev); |
| 283 | unbind_clkdev: |
| 284 | device_unbind(clkdev); |
Konrad Dybcio | 6c0b844 | 2023-11-07 12:41:01 +0000 | [diff] [blame] | 285 | |
| 286 | return ret; |
| 287 | } |
| 288 | |
| 289 | static int qcom_reset_set(struct reset_ctl *rst, bool assert) |
| 290 | { |
| 291 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(rst->dev); |
| 292 | void __iomem *base = dev_get_priv(rst->dev); |
| 293 | const struct qcom_reset_map *map; |
| 294 | u32 value; |
| 295 | |
| 296 | map = &data->resets[rst->id]; |
| 297 | |
| 298 | value = readl(base + map->reg); |
| 299 | |
| 300 | if (assert) |
| 301 | value |= BIT(map->bit); |
| 302 | else |
| 303 | value &= ~BIT(map->bit); |
| 304 | |
| 305 | writel(value, base + map->reg); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int qcom_reset_assert(struct reset_ctl *rst) |
| 311 | { |
| 312 | return qcom_reset_set(rst, true); |
| 313 | } |
| 314 | |
| 315 | static int qcom_reset_deassert(struct reset_ctl *rst) |
| 316 | { |
| 317 | return qcom_reset_set(rst, false); |
| 318 | } |
| 319 | |
| 320 | static const struct reset_ops qcom_reset_ops = { |
| 321 | .rst_assert = qcom_reset_assert, |
| 322 | .rst_deassert = qcom_reset_deassert, |
| 323 | }; |
| 324 | |
| 325 | static int qcom_reset_probe(struct udevice *dev) |
| 326 | { |
| 327 | /* Set our priv pointer to the base address */ |
| 328 | dev_set_priv(dev, (void *)dev_read_addr(dev)); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | U_BOOT_DRIVER(qcom_reset) = { |
| 334 | .name = "qcom_reset", |
| 335 | .id = UCLASS_RESET, |
| 336 | .ops = &qcom_reset_ops, |
| 337 | .probe = qcom_reset_probe, |
| 338 | }; |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 339 | |
| 340 | static int qcom_power_set(struct power_domain *pwr, bool on) |
| 341 | { |
| 342 | struct msm_clk_data *data = (struct msm_clk_data *)dev_get_driver_data(pwr->dev); |
| 343 | void __iomem *base = dev_get_priv(pwr->dev); |
| 344 | const struct qcom_power_map *map; |
| 345 | u32 value; |
| 346 | int ret; |
| 347 | |
| 348 | if (pwr->id >= data->num_power_domains) |
| 349 | return -ENODEV; |
| 350 | |
| 351 | map = &data->power_domains[pwr->id]; |
| 352 | |
| 353 | if (!map->reg) |
| 354 | return -ENODEV; |
| 355 | |
| 356 | value = readl(base + map->reg); |
| 357 | |
| 358 | if (on) |
| 359 | value &= ~GDSC_SW_COLLAPSE_MASK; |
| 360 | else |
| 361 | value |= GDSC_SW_COLLAPSE_MASK; |
| 362 | |
| 363 | writel(value, base + map->reg); |
| 364 | |
| 365 | if (on) |
| 366 | ret = readl_poll_timeout(base + map->reg + CFG_GDSCR_OFFSET, |
| 367 | value, |
| 368 | (value & GDSC_POWER_UP_COMPLETE) || |
| 369 | (value & GDSC_PWR_ON_MASK), |
| 370 | GDSC_STATUS_POLL_TIMEOUT_US); |
| 371 | |
| 372 | else |
| 373 | ret = readl_poll_timeout(base + map->reg + CFG_GDSCR_OFFSET, |
| 374 | value, |
| 375 | (value & GDSC_POWER_DOWN_COMPLETE) || |
| 376 | !(value & GDSC_PWR_ON_MASK), |
| 377 | GDSC_STATUS_POLL_TIMEOUT_US); |
| 378 | |
| 379 | |
| 380 | if (ret == -ETIMEDOUT) |
| 381 | printf("WARNING: GDSC %lu is stuck during power on/off\n", |
| 382 | pwr->id); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | static int qcom_power_on(struct power_domain *pwr) |
| 387 | { |
| 388 | return qcom_power_set(pwr, true); |
| 389 | } |
| 390 | |
| 391 | static int qcom_power_off(struct power_domain *pwr) |
| 392 | { |
| 393 | return qcom_power_set(pwr, false); |
| 394 | } |
| 395 | |
| 396 | static const struct power_domain_ops qcom_power_ops = { |
| 397 | .on = qcom_power_on, |
| 398 | .off = qcom_power_off, |
| 399 | }; |
| 400 | |
| 401 | static int qcom_power_probe(struct udevice *dev) |
| 402 | { |
| 403 | /* Set our priv pointer to the base address */ |
| 404 | dev_set_priv(dev, (void *)dev_read_addr(dev)); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | U_BOOT_DRIVER(qcom_power) = { |
| 410 | .name = "qcom_power", |
| 411 | .id = UCLASS_POWER_DOMAIN, |
| 412 | .ops = &qcom_power_ops, |
| 413 | .probe = qcom_power_probe, |
Caleb Connolly | e07ce56 | 2024-04-03 14:07:39 +0200 | [diff] [blame] | 414 | .flags = DM_FLAG_PRE_RELOC, |
Volodymyr Babchuk | aae4649 | 2024-03-11 21:33:45 +0000 | [diff] [blame] | 415 | }; |