Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Texas Instruments K3 SoC PLL clock driver |
| 4 | * |
Nishanth Menon | eaa39c6 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 5 | * Copyright (C) 2020-2021 Texas Instruments Incorporated - https://www.ti.com/ |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 6 | * Tero Kristo <t-kristo@ti.com> |
| 7 | */ |
| 8 | |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <dm.h> |
| 11 | #include <div64.h> |
| 12 | #include <errno.h> |
| 13 | #include <clk-uclass.h> |
| 14 | #include <linux/clk-provider.h> |
| 15 | #include "k3-clk.h" |
| 16 | #include <linux/rational.h> |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 17 | #include <linux/delay.h> |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 18 | |
| 19 | /* 16FFT register offsets */ |
| 20 | #define PLL_16FFT_CFG 0x08 |
| 21 | #define PLL_KICK0 0x10 |
| 22 | #define PLL_KICK1 0x14 |
| 23 | #define PLL_16FFT_CTRL 0x20 |
| 24 | #define PLL_16FFT_STAT 0x24 |
| 25 | #define PLL_16FFT_FREQ_CTRL0 0x30 |
| 26 | #define PLL_16FFT_FREQ_CTRL1 0x34 |
| 27 | #define PLL_16FFT_DIV_CTRL 0x38 |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 28 | #define PLL_16FFT_CAL_CTRL 0x60 |
| 29 | #define PLL_16FFT_CAL_STAT 0x64 |
| 30 | |
| 31 | /* CAL STAT register bits */ |
| 32 | #define PLL_16FFT_CAL_STAT_CAL_LOCK BIT(31) |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 33 | #define PLL_16FFT_CAL_STAT_CAL_LOCK_TIMEOUT (4350U * 100U) |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 34 | |
| 35 | /* CFG register bits */ |
| 36 | #define PLL_16FFT_CFG_PLL_TYPE_SHIFT (0) |
| 37 | #define PLL_16FFT_CFG_PLL_TYPE_MASK (0x3 << 0) |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 38 | #define PLL_16FFT_CFG_PLL_TYPE_FRAC2 0 |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 39 | #define PLL_16FFT_CFG_PLL_TYPE_FRACF 1 |
| 40 | |
| 41 | /* CAL CTRL register bits */ |
| 42 | #define PLL_16FFT_CAL_CTRL_CAL_EN BIT(31) |
| 43 | #define PLL_16FFT_CAL_CTRL_FAST_CAL BIT(20) |
| 44 | #define PLL_16FFT_CAL_CTRL_CAL_BYP BIT(15) |
| 45 | #define PLL_16FFT_CAL_CTRL_CAL_CNT_SHIFT 16 |
| 46 | #define PLL_16FFT_CAL_CTRL_CAL_CNT_MASK (0x7 << 16) |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 47 | #define PLL_16FFT_CAL_CTRL_CAL_IN_MASK (0xFFFU) |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 48 | |
| 49 | /* CTRL register bits */ |
| 50 | #define PLL_16FFT_CTRL_BYPASS_EN BIT(31) |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 51 | #define PLL_16FFT_CTRL_BYP_ON_LOCKLOSS BIT(16) |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 52 | #define PLL_16FFT_CTRL_PLL_EN BIT(15) |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 53 | #define PLL_16FFT_CTRL_INTL_BYP_EN BIT(8) |
| 54 | #define PLL_16FFT_CTRL_CLK_4PH_EN BIT(5) |
| 55 | #define PLL_16FFT_CTRL_CLK_POSTDIV_EN BIT(4) |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 56 | #define PLL_16FFT_CTRL_DSM_EN BIT(1) |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 57 | #define PLL_16FFT_CTRL_DAC_EN BIT(0) |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 58 | |
| 59 | /* STAT register bits */ |
| 60 | #define PLL_16FFT_STAT_LOCK BIT(0) |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 61 | #define PLL_16FFT_STAT_LOCK_TIMEOUT (150U * 100U) |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 62 | |
| 63 | /* FREQ_CTRL0 bits */ |
| 64 | #define PLL_16FFT_FREQ_CTRL0_FB_DIV_INT_MASK 0xfff |
| 65 | |
| 66 | /* DIV CTRL register bits */ |
| 67 | #define PLL_16FFT_DIV_CTRL_REF_DIV_MASK 0x3f |
| 68 | |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 69 | /* HSDIV register bits*/ |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 70 | #define PLL_16FFT_HSDIV_CTRL_CLKOUT_EN BIT(15) |
| 71 | |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 72 | /* FREQ_CTRL1 bits */ |
| 73 | #define PLL_16FFT_FREQ_CTRL1_FB_DIV_FRAC_BITS 24 |
| 74 | #define PLL_16FFT_FREQ_CTRL1_FB_DIV_FRAC_MASK 0xffffff |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 75 | |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 76 | /* KICK register magic values */ |
| 77 | #define PLL_KICK0_VALUE 0x68ef3490 |
| 78 | #define PLL_KICK1_VALUE 0xd172bc5a |
| 79 | |
| 80 | /** |
| 81 | * struct ti_pll_clk - TI PLL clock data info structure |
| 82 | * @clk: core clock structure |
| 83 | * @reg: memory address of the PLL controller |
| 84 | */ |
| 85 | struct ti_pll_clk { |
| 86 | struct clk clk; |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 87 | void __iomem *base; |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | #define to_clk_pll(_clk) container_of(_clk, struct ti_pll_clk, clk) |
| 91 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 92 | static int ti_pll_clk_disable(struct clk *clk) |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 93 | { |
| 94 | struct ti_pll_clk *pll = to_clk_pll(clk); |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 95 | u32 ctrl; |
| 96 | |
| 97 | ctrl = readl(pll->base + PLL_16FFT_CTRL); |
| 98 | |
| 99 | if ((ctrl & PLL_16FFT_CTRL_PLL_EN)) { |
| 100 | ctrl &= ~PLL_16FFT_CTRL_PLL_EN; |
| 101 | writel(ctrl, pll->base + PLL_16FFT_CTRL); |
| 102 | |
| 103 | /* wait 1us */ |
| 104 | udelay(1); |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int ti_pll_clk_enable(struct clk *clk) |
| 111 | { |
| 112 | struct ti_pll_clk *pll = to_clk_pll(clk); |
| 113 | u32 ctrl; |
| 114 | |
| 115 | ctrl = readl(pll->base + PLL_16FFT_CTRL); |
| 116 | ctrl |= PLL_16FFT_CTRL_PLL_EN; |
| 117 | writel(ctrl, pll->base + PLL_16FFT_CTRL); |
| 118 | |
| 119 | /* Wait 1us */ |
| 120 | udelay(1); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static bool clk_pll_16fft_check_lock(const struct ti_pll_clk *pll) |
| 126 | { |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 127 | u32 stat; |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 128 | |
| 129 | stat = readl(pll->base + PLL_16FFT_STAT); |
| 130 | return (stat & PLL_16FFT_STAT_LOCK); |
| 131 | } |
| 132 | |
| 133 | static bool clk_pll_16fft_check_cal_lock(const struct ti_pll_clk *pll) |
| 134 | { |
| 135 | u32 stat; |
| 136 | |
| 137 | stat = readl(pll->base + PLL_16FFT_CAL_STAT); |
| 138 | return (stat & PLL_16FFT_CAL_STAT_CAL_LOCK); |
| 139 | } |
| 140 | |
| 141 | static void clk_pll_16fft_cal_int(const struct ti_pll_clk *pll) |
| 142 | { |
| 143 | u32 cal; |
| 144 | |
| 145 | cal = readl(pll->base + PLL_16FFT_CAL_CTRL); |
| 146 | |
| 147 | /* Enable fast cal mode */ |
| 148 | cal |= PLL_16FFT_CAL_CTRL_FAST_CAL; |
| 149 | |
| 150 | /* Disable calibration bypass */ |
| 151 | cal &= ~PLL_16FFT_CAL_CTRL_CAL_BYP; |
| 152 | |
| 153 | /* Set CALCNT to 2 */ |
| 154 | cal &= ~PLL_16FFT_CAL_CTRL_CAL_CNT_MASK; |
| 155 | cal |= 2 << PLL_16FFT_CAL_CTRL_CAL_CNT_SHIFT; |
| 156 | |
| 157 | /* Set CAL_IN to 0 */ |
| 158 | cal &= ~PLL_16FFT_CAL_CTRL_CAL_IN_MASK; |
| 159 | |
| 160 | /* Note this register does not readback the written value. */ |
| 161 | writel(cal, pll->base + PLL_16FFT_CAL_CTRL); |
| 162 | |
| 163 | /* Wait 1us before enabling the CAL_EN field */ |
| 164 | udelay(1); |
| 165 | |
| 166 | cal = readl(pll->base + PLL_16FFT_CAL_CTRL); |
| 167 | |
| 168 | /* Enable calibration for FRACF */ |
| 169 | cal |= PLL_16FFT_CAL_CTRL_CAL_EN; |
| 170 | |
| 171 | /* Note this register does not readback the written value. */ |
| 172 | writel(cal, pll->base + PLL_16FFT_CAL_CTRL); |
| 173 | } |
| 174 | |
| 175 | static void clk_pll_16fft_disable_cal(const struct ti_pll_clk *pll) |
| 176 | { |
| 177 | u32 cal, stat; |
| 178 | |
| 179 | cal = readl(pll->base + PLL_16FFT_CAL_CTRL); |
| 180 | cal &= ~PLL_16FFT_CAL_CTRL_CAL_EN; |
| 181 | /* Note this register does not readback the written value. */ |
| 182 | writel(cal, pll->base + PLL_16FFT_CAL_CTRL); |
| 183 | do { |
| 184 | stat = readl(pll->base + PLL_16FFT_CAL_STAT); |
| 185 | } while (stat & PLL_16FFT_CAL_STAT_CAL_LOCK); |
| 186 | } |
| 187 | |
| 188 | static int ti_pll_wait_for_lock(struct clk *clk) |
| 189 | { |
| 190 | struct ti_pll_clk *pll = to_clk_pll(clk); |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 191 | u32 cfg; |
| 192 | u32 cal; |
| 193 | u32 freq_ctrl1; |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 194 | unsigned int i; |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 195 | u32 pllfm; |
| 196 | u32 pll_type; |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 197 | u32 cal_en = 0; |
| 198 | bool success; |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 199 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 200 | /* |
| 201 | * Minimum VCO input freq is 5MHz, and the longest a lock should |
| 202 | * be consider to be timed out after 750 cycles. Be conservative |
| 203 | * and assume each loop takes 10 cycles and we run at a |
| 204 | * max of 1GHz. That gives 15000 loop cycles. We may end up waiting |
| 205 | * longer than necessary for timeout, but that should be ok. |
| 206 | */ |
| 207 | success = false; |
| 208 | for (i = 0; i < PLL_16FFT_STAT_LOCK_TIMEOUT; i++) { |
| 209 | if (clk_pll_16fft_check_lock(pll)) { |
| 210 | success = true; |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 211 | break; |
| 212 | } |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 213 | } |
| 214 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 215 | /* Disable calibration in the fractional mode of the FRACF PLL based on data |
| 216 | * from silicon and simulation data. |
| 217 | */ |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 218 | freq_ctrl1 = readl(pll->base + PLL_16FFT_FREQ_CTRL1); |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 219 | pllfm = freq_ctrl1 & PLL_16FFT_FREQ_CTRL1_FB_DIV_FRAC_MASK; |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 220 | |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 221 | cfg = readl(pll->base + PLL_16FFT_CFG); |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 222 | pll_type = (cfg & PLL_16FFT_CFG_PLL_TYPE_MASK) >> PLL_16FFT_CFG_PLL_TYPE_SHIFT; |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 223 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 224 | if (success && pll_type == PLL_16FFT_CFG_PLL_TYPE_FRACF) { |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 225 | cal = readl(pll->base + PLL_16FFT_CAL_CTRL); |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 226 | cal_en = (cal & PLL_16FFT_CAL_CTRL_CAL_EN); |
| 227 | } |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 228 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 229 | if (success && pll_type == PLL_16FFT_CFG_PLL_TYPE_FRACF && |
| 230 | pllfm == 0 && cal_en == 1) { |
| 231 | /* |
| 232 | * Wait for calibration lock. |
| 233 | * |
| 234 | * Lock should occur within: |
| 235 | * |
| 236 | * 170 * 2^(5+CALCNT) / PFD |
| 237 | * 21760 / PFD |
| 238 | * |
| 239 | * CALCNT = 2, PFD = 5-50MHz. This gives a range of 0.435mS to |
| 240 | * 4.35mS depending on PFD frequency. |
| 241 | * |
| 242 | * Be conservative and assume each loop takes 10 cycles and we run at a |
| 243 | * max of 1GHz. That gives 435000 loop cycles. We may end up waiting |
| 244 | * longer than necessary for timeout, but that should be ok. |
| 245 | * |
| 246 | * The recommend timeout for CALLOCK to go high is 4.35 ms |
| 247 | */ |
| 248 | success = false; |
| 249 | for (i = 0; i < PLL_16FFT_CAL_STAT_CAL_LOCK_TIMEOUT; i++) { |
| 250 | if (clk_pll_16fft_check_cal_lock(pll)) { |
| 251 | success = true; |
| 252 | break; |
| 253 | } |
| 254 | } |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 255 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 256 | /* In case of cal lock failure, operate without calibration */ |
| 257 | if (!success) { |
| 258 | debug("Failure for calibration, falling back without calibration\n"); |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 259 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 260 | /* Disable PLL */ |
| 261 | ti_pll_clk_disable(clk); |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 262 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 263 | /* Disable Calibration */ |
| 264 | clk_pll_16fft_disable_cal(pll); |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 265 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 266 | /* Enable PLL */ |
| 267 | ti_pll_clk_enable(clk); |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 268 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 269 | /* Wait for PLL Lock */ |
| 270 | for (i = 0; i < PLL_16FFT_STAT_LOCK_TIMEOUT; i++) { |
| 271 | if (clk_pll_16fft_check_lock(pll)) { |
| 272 | success = true; |
| 273 | break; |
| 274 | } |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 279 | if (!success) { |
Vishal Mahaveer | a5135b8 | 2023-10-23 08:35:46 -0500 | [diff] [blame] | 280 | printf("%s: pll (%s) failed to lock\n", __func__, |
| 281 | clk->dev->name); |
| 282 | return -EBUSY; |
| 283 | } else { |
| 284 | return 0; |
| 285 | } |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static ulong ti_pll_clk_get_rate(struct clk *clk) |
| 289 | { |
| 290 | struct ti_pll_clk *pll = to_clk_pll(clk); |
| 291 | u64 current_freq; |
| 292 | u64 parent_freq = clk_get_parent_rate(clk); |
| 293 | u32 pllm; |
| 294 | u32 plld; |
| 295 | u32 pllfm; |
| 296 | u32 ctrl; |
| 297 | |
| 298 | /* Check if we are in bypass */ |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 299 | ctrl = readl(pll->base + PLL_16FFT_CTRL); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 300 | if (ctrl & PLL_16FFT_CTRL_BYPASS_EN) |
| 301 | return parent_freq; |
| 302 | |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 303 | pllm = readl(pll->base + PLL_16FFT_FREQ_CTRL0); |
| 304 | pllfm = readl(pll->base + PLL_16FFT_FREQ_CTRL1); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 305 | |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 306 | plld = readl(pll->base + PLL_16FFT_DIV_CTRL) & |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 307 | PLL_16FFT_DIV_CTRL_REF_DIV_MASK; |
| 308 | |
| 309 | current_freq = parent_freq * pllm / plld; |
| 310 | |
| 311 | if (pllfm) { |
| 312 | u64 tmp; |
| 313 | |
| 314 | tmp = parent_freq * pllfm; |
| 315 | do_div(tmp, plld); |
| 316 | tmp >>= PLL_16FFT_FREQ_CTRL1_FB_DIV_FRAC_BITS; |
| 317 | current_freq += tmp; |
| 318 | } |
| 319 | |
| 320 | return current_freq; |
| 321 | } |
| 322 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 323 | static bool ti_pll_clk_is_bypass(struct ti_pll_clk *pll) |
| 324 | { |
| 325 | u32 ctrl; |
| 326 | bool ret; |
| 327 | |
| 328 | ctrl = readl(pll->base + PLL_16FFT_CTRL); |
| 329 | ret = (ctrl & PLL_16FFT_CTRL_BYPASS_EN) != 0; |
| 330 | |
| 331 | return ret; |
| 332 | } |
| 333 | |
| 334 | static void ti_pll_clk_bypass(struct ti_pll_clk *pll, bool bypass) |
| 335 | { |
| 336 | u32 ctrl; |
| 337 | |
| 338 | ctrl = readl(pll->base + PLL_16FFT_CTRL); |
| 339 | if (bypass) |
| 340 | ctrl |= PLL_16FFT_CTRL_BYPASS_EN; |
| 341 | else |
| 342 | ctrl &= ~PLL_16FFT_CTRL_BYPASS_EN; |
| 343 | |
| 344 | writel(ctrl, pll->base + PLL_16FFT_CTRL); |
| 345 | } |
| 346 | |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 347 | static ulong ti_pll_clk_set_rate(struct clk *clk, ulong rate) |
| 348 | { |
| 349 | struct ti_pll_clk *pll = to_clk_pll(clk); |
| 350 | u64 current_freq; |
| 351 | u64 parent_freq = clk_get_parent_rate(clk); |
| 352 | int ret; |
| 353 | u32 ctrl; |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 354 | u32 cfg; |
| 355 | u32 pll_type; |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 356 | unsigned long pllm; |
| 357 | u32 pllfm = 0; |
| 358 | unsigned long plld; |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 359 | u32 freq_ctrl0; |
| 360 | u32 freq_ctrl1; |
Dave Gerlach | 6bc722e | 2021-09-07 17:16:57 -0500 | [diff] [blame] | 361 | u32 div_ctrl; |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 362 | u32 rem; |
| 363 | int shift; |
| 364 | |
| 365 | debug("%s(clk=%p, rate=%u)\n", __func__, clk, (u32)rate); |
| 366 | |
| 367 | if (ti_pll_clk_get_rate(clk) == rate) |
| 368 | return rate; |
| 369 | |
| 370 | if (rate != parent_freq) |
| 371 | /* |
| 372 | * Attempt with higher max multiplier value first to give |
| 373 | * some space for fractional divider to kick in. |
| 374 | */ |
| 375 | for (shift = 8; shift >= 0; shift -= 8) { |
| 376 | rational_best_approximation(rate, parent_freq, |
| 377 | ((PLL_16FFT_FREQ_CTRL0_FB_DIV_INT_MASK + 1) << shift) - 1, |
| 378 | PLL_16FFT_DIV_CTRL_REF_DIV_MASK, &pllm, &plld); |
| 379 | if (pllm / plld <= PLL_16FFT_FREQ_CTRL0_FB_DIV_INT_MASK) |
| 380 | break; |
| 381 | } |
| 382 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 383 | if (!ti_pll_clk_is_bypass(pll)) { |
| 384 | /* Put the PLL into bypass */ |
| 385 | ti_pll_clk_bypass(pll, true); |
| 386 | } |
| 387 | |
| 388 | /* Disable the PLL */ |
| 389 | ti_pll_clk_disable(clk); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 390 | |
| 391 | if (rate == parent_freq) { |
| 392 | debug("%s: put %s to bypass\n", __func__, clk->dev->name); |
| 393 | return rate; |
| 394 | } |
| 395 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 396 | cfg = readl(pll->base + PLL_16FFT_CFG); |
| 397 | pll_type = (cfg & PLL_16FFT_CFG_PLL_TYPE_MASK) >> PLL_16FFT_CFG_PLL_TYPE_SHIFT; |
| 398 | |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 399 | debug("%s: pre-frac-calc: rate=%u, parent_freq=%u, plld=%u, pllm=%u\n", |
| 400 | __func__, (u32)rate, (u32)parent_freq, (u32)plld, (u32)pllm); |
| 401 | |
| 402 | /* Check if we need fractional config */ |
| 403 | if (plld > 1) { |
| 404 | pllfm = pllm % plld; |
| 405 | pllfm <<= PLL_16FFT_FREQ_CTRL1_FB_DIV_FRAC_BITS; |
| 406 | rem = pllfm % plld; |
| 407 | pllfm /= plld; |
| 408 | if (rem) |
| 409 | pllfm++; |
| 410 | pllm /= plld; |
| 411 | plld = 1; |
| 412 | } |
| 413 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 414 | /* Program the new rate */ |
| 415 | freq_ctrl0 = readl(pll->base + PLL_16FFT_FREQ_CTRL0); |
| 416 | freq_ctrl1 = readl(pll->base + PLL_16FFT_FREQ_CTRL1); |
| 417 | div_ctrl = readl(pll->base + PLL_16FFT_DIV_CTRL); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 418 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 419 | freq_ctrl0 &= ~PLL_16FFT_FREQ_CTRL0_FB_DIV_INT_MASK; |
| 420 | freq_ctrl0 |= pllm; |
| 421 | |
| 422 | freq_ctrl1 &= ~PLL_16FFT_FREQ_CTRL1_FB_DIV_FRAC_MASK; |
| 423 | freq_ctrl1 |= pllfm; |
Dave Gerlach | 6bc722e | 2021-09-07 17:16:57 -0500 | [diff] [blame] | 424 | |
| 425 | /* |
| 426 | * div_ctrl register contains other divider values, so rmw |
| 427 | * only plld and leave existing values alone |
| 428 | */ |
Dave Gerlach | 6bc722e | 2021-09-07 17:16:57 -0500 | [diff] [blame] | 429 | div_ctrl &= ~PLL_16FFT_DIV_CTRL_REF_DIV_MASK; |
| 430 | div_ctrl |= plld; |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 431 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 432 | /* Make sure we have fractional support if required */ |
| 433 | ctrl = readl(pll->base + PLL_16FFT_CTRL); |
| 434 | |
| 435 | /* Don't use internal bypass,it is not glitch free. Always prefer glitchless bypass */ |
| 436 | ctrl &= ~(PLL_16FFT_CTRL_INTL_BYP_EN | PLL_16FFT_CTRL_CLK_4PH_EN); |
| 437 | |
| 438 | /* Always enable output if PLL, Always bypass if we lose lock */ |
| 439 | ctrl |= (PLL_16FFT_CTRL_CLK_POSTDIV_EN | PLL_16FFT_CTRL_BYP_ON_LOCKLOSS); |
| 440 | |
| 441 | /* Enable fractional support if required */ |
| 442 | if (pll_type == PLL_16FFT_CFG_PLL_TYPE_FRACF) { |
| 443 | if (pllfm != 0) |
| 444 | ctrl |= (PLL_16FFT_CTRL_DSM_EN | PLL_16FFT_CTRL_DAC_EN); |
| 445 | else |
| 446 | ctrl &= ~(PLL_16FFT_CTRL_DSM_EN | PLL_16FFT_CTRL_DAC_EN); |
| 447 | } |
| 448 | |
| 449 | /* Enable Fractional by default for PLL_16FFT_CFG_PLL_TYPE_FRAC2 */ |
| 450 | if (pll_type == PLL_16FFT_CFG_PLL_TYPE_FRAC2) |
| 451 | ctrl |= (PLL_16FFT_CTRL_DSM_EN | PLL_16FFT_CTRL_DAC_EN); |
| 452 | |
| 453 | writel(freq_ctrl0, pll->base + PLL_16FFT_FREQ_CTRL0); |
| 454 | writel(freq_ctrl1, pll->base + PLL_16FFT_FREQ_CTRL1); |
| 455 | writel(div_ctrl, pll->base + PLL_16FFT_DIV_CTRL); |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 456 | writel(ctrl, pll->base + PLL_16FFT_CTRL); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 457 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 458 | /* Configure PLL calibration*/ |
| 459 | if (pll_type == PLL_16FFT_CFG_PLL_TYPE_FRACF) { |
| 460 | if (pllfm != 0) { |
| 461 | /* Disable Calibration in Fractional mode */ |
| 462 | clk_pll_16fft_disable_cal(pll); |
| 463 | } else { |
| 464 | /* Enable Calibration in Integer mode */ |
| 465 | clk_pll_16fft_cal_int(pll); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Wait at least 1 ref cycle before enabling PLL. |
| 471 | * Minimum VCO input frequency is 5MHz, therefore maximum |
| 472 | * wait time for 1 ref clock is 0.2us. |
| 473 | */ |
| 474 | udelay(1); |
| 475 | ti_pll_clk_enable(clk); |
| 476 | |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 477 | ret = ti_pll_wait_for_lock(clk); |
| 478 | if (ret) |
| 479 | return ret; |
| 480 | |
Manorit Chawdhry | 02fb7bf | 2024-11-21 17:32:53 +0530 | [diff] [blame] | 481 | ti_pll_clk_bypass(pll, false); |
| 482 | |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 483 | debug("%s: pllm=%u, plld=%u, pllfm=%u, parent_freq=%u\n", |
| 484 | __func__, (u32)pllm, (u32)plld, (u32)pllfm, (u32)parent_freq); |
| 485 | |
| 486 | current_freq = parent_freq * pllm / plld; |
| 487 | |
| 488 | if (pllfm) { |
| 489 | u64 tmp; |
| 490 | |
| 491 | tmp = parent_freq * pllfm; |
| 492 | do_div(tmp, plld); |
| 493 | tmp >>= PLL_16FFT_FREQ_CTRL1_FB_DIV_FRAC_BITS; |
| 494 | current_freq += tmp; |
| 495 | } |
| 496 | |
| 497 | return current_freq; |
| 498 | } |
| 499 | |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 500 | |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 501 | |
| 502 | static const struct clk_ops ti_pll_clk_ops = { |
| 503 | .get_rate = ti_pll_clk_get_rate, |
| 504 | .set_rate = ti_pll_clk_set_rate, |
| 505 | .enable = ti_pll_clk_enable, |
| 506 | .disable = ti_pll_clk_disable, |
| 507 | }; |
| 508 | |
| 509 | struct clk *clk_register_ti_pll(const char *name, const char *parent_name, |
| 510 | void __iomem *reg) |
| 511 | { |
| 512 | struct ti_pll_clk *pll; |
| 513 | int ret; |
| 514 | int i; |
| 515 | u32 cfg, ctrl, hsdiv_presence_bit, hsdiv_ctrl_offs; |
| 516 | |
| 517 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
| 518 | if (!pll) |
| 519 | return ERR_PTR(-ENOMEM); |
| 520 | |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 521 | pll->base = reg; |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 522 | |
| 523 | ret = clk_register(&pll->clk, "ti-pll-clk", name, parent_name); |
| 524 | if (ret) { |
| 525 | printf("%s: failed to register: %d\n", __func__, ret); |
| 526 | kfree(pll); |
| 527 | return ERR_PTR(ret); |
| 528 | } |
| 529 | |
| 530 | /* Unlock the PLL registers */ |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 531 | writel(PLL_KICK0_VALUE, pll->base + PLL_KICK0); |
| 532 | writel(PLL_KICK1_VALUE, pll->base + PLL_KICK1); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 533 | |
| 534 | /* Enable all HSDIV outputs */ |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 535 | cfg = readl(pll->base + PLL_16FFT_CFG); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 536 | for (i = 0; i < 16; i++) { |
| 537 | hsdiv_presence_bit = BIT(16 + i); |
| 538 | hsdiv_ctrl_offs = 0x80 + (i * 4); |
| 539 | /* Enable HSDIV output if present */ |
| 540 | if ((hsdiv_presence_bit & cfg) != 0UL) { |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 541 | ctrl = readl(pll->base + hsdiv_ctrl_offs); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 542 | ctrl |= PLL_16FFT_HSDIV_CTRL_CLKOUT_EN; |
Manorit Chawdhry | 65caba5 | 2024-11-21 17:32:52 +0530 | [diff] [blame] | 543 | writel(ctrl, pll->base + hsdiv_ctrl_offs); |
Tero Kristo | 81744b7 | 2021-06-11 11:45:13 +0300 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | return &pll->clk; |
| 548 | } |
| 549 | |
| 550 | U_BOOT_DRIVER(ti_pll_clk) = { |
| 551 | .name = "ti-pll-clk", |
| 552 | .id = UCLASS_CLK, |
| 553 | .ops = &ti_pll_clk_ops, |
| 554 | .flags = DM_FLAG_PRE_RELOC, |
| 555 | }; |