1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2014 MediaTek Inc. 4 * Author: James Liao <jamesjj.liao@mediatek.com> 5 */ 6 7 #include <linux/clk-provider.h> 8 #include <linux/container_of.h> 9 #include <linux/delay.h> 10 #include <linux/err.h> 11 #include <linux/io.h> 12 #include <linux/module.h> 13 #include <linux/of_address.h> 14 #include <linux/slab.h> 15 16 #include "clk-pll.h" 17 18 #define MHZ (1000 * 1000) 19 20 #define REG_CON0 0 21 #define REG_CON1 4 22 23 #define CON0_BASE_EN BIT(0) 24 #define CON0_PWR_ON BIT(0) 25 #define CON0_ISO_EN BIT(1) 26 #define PCW_CHG_MASK BIT(31) 27 28 #define AUDPLL_TUNER_EN BIT(31) 29 30 #define POSTDIV_MASK 0x7 31 32 /* default 7 bits integer, can be overridden with pcwibits. */ 33 #define INTEGER_BITS 7 34 35 /* 36 * MediaTek PLLs are configured through their pcw value. The pcw value describes 37 * a divider in the PLL feedback loop which consists of 7 bits for the integer 38 * part and the remaining bits (if present) for the fractional part. Also they 39 * have a 3 bit power-of-two post divider. 40 */ 41 42 struct mtk_clk_pll { 43 struct clk_hw hw; 44 void __iomem *base_addr; 45 void __iomem *pd_addr; 46 void __iomem *pwr_addr; 47 void __iomem *tuner_addr; 48 void __iomem *tuner_en_addr; 49 void __iomem *pcw_addr; 50 void __iomem *pcw_chg_addr; 51 void __iomem *en_addr; 52 const struct mtk_pll_data *data; 53 }; 54 55 static inline struct mtk_clk_pll *to_mtk_clk_pll(struct clk_hw *hw) 56 { 57 return container_of(hw, struct mtk_clk_pll, hw); 58 } 59 60 static int mtk_pll_is_prepared(struct clk_hw *hw) 61 { 62 struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); 63 64 return (readl(pll->en_addr) & BIT(pll->data->pll_en_bit)) != 0; 65 } 66 67 static unsigned long __mtk_pll_recalc_rate(struct mtk_clk_pll *pll, u32 fin, 68 u32 pcw, int postdiv) 69 { 70 int pcwbits = pll->data->pcwbits; 71 int pcwfbits = 0; 72 int ibits; 73 u64 vco; 74 u8 c = 0; 75 76 /* The fractional part of the PLL divider. */ 77 ibits = pll->data->pcwibits ? pll->data->pcwibits : INTEGER_BITS; 78 if (pcwbits > ibits) 79 pcwfbits = pcwbits - ibits; 80 81 vco = (u64)fin * pcw; 82 83 if (pcwfbits && (vco & GENMASK(pcwfbits - 1, 0))) 84 c = 1; 85 86 vco >>= pcwfbits; 87 88 if (c) 89 vco++; 90 91 return ((unsigned long)vco + postdiv - 1) / postdiv; 92 } 93 94 static void __mtk_pll_tuner_enable(struct mtk_clk_pll *pll) 95 { 96 u32 r; 97 98 if (pll->tuner_en_addr) { 99 r = readl(pll->tuner_en_addr) | BIT(pll->data->tuner_en_bit); 100 writel(r, pll->tuner_en_addr); 101 } else if (pll->tuner_addr) { 102 r = readl(pll->tuner_addr) | AUDPLL_TUNER_EN; 103 writel(r, pll->tuner_addr); 104 } 105 } 106 107 static void __mtk_pll_tuner_disable(struct mtk_clk_pll *pll) 108 { 109 u32 r; 110 111 if (pll->tuner_en_addr) { 112 r = readl(pll->tuner_en_addr) & ~BIT(pll->data->tuner_en_bit); 113 writel(r, pll->tuner_en_addr); 114 } else if (pll->tuner_addr) { 115 r = readl(pll->tuner_addr) & ~AUDPLL_TUNER_EN; 116 writel(r, pll->tuner_addr); 117 } 118 } 119 120 static void mtk_pll_set_rate_regs(struct mtk_clk_pll *pll, u32 pcw, 121 int postdiv) 122 { 123 u32 chg, val; 124 125 /* disable tuner */ 126 __mtk_pll_tuner_disable(pll); 127 128 /* set postdiv */ 129 val = readl(pll->pd_addr); 130 val &= ~(POSTDIV_MASK << pll->data->pd_shift); 131 val |= (ffs(postdiv) - 1) << pll->data->pd_shift; 132 133 /* postdiv and pcw need to set at the same time if on same register */ 134 if (pll->pd_addr != pll->pcw_addr) { 135 writel(val, pll->pd_addr); 136 val = readl(pll->pcw_addr); 137 } 138 139 /* set pcw */ 140 val &= ~GENMASK(pll->data->pcw_shift + pll->data->pcwbits - 1, 141 pll->data->pcw_shift); 142 val |= pcw << pll->data->pcw_shift; 143 writel(val, pll->pcw_addr); 144 chg = readl(pll->pcw_chg_addr) | PCW_CHG_MASK; 145 writel(chg, pll->pcw_chg_addr); 146 if (pll->tuner_addr) 147 writel(val + 1, pll->tuner_addr); 148 149 /* restore tuner_en */ 150 __mtk_pll_tuner_enable(pll); 151 152 udelay(20); 153 } 154 155 /* 156 * mtk_pll_calc_values - calculate good values for a given input frequency. 157 * @pll: The pll 158 * @pcw: The pcw value (output) 159 * @postdiv: The post divider (output) 160 * @freq: The desired target frequency 161 * @fin: The input frequency 162 * 163 */ 164 static void mtk_pll_calc_values(struct mtk_clk_pll *pll, u32 *pcw, u32 *postdiv, 165 u32 freq, u32 fin) 166 { 167 unsigned long fmin = pll->data->fmin ? pll->data->fmin : (1000 * MHZ); 168 const struct mtk_pll_div_table *div_table = pll->data->div_table; 169 u64 _pcw; 170 int ibits; 171 u32 val; 172 173 if (freq > pll->data->fmax) 174 freq = pll->data->fmax; 175 176 if (div_table) { 177 if (freq > div_table[0].freq) 178 freq = div_table[0].freq; 179 180 for (val = 0; div_table[val + 1].freq != 0; val++) { 181 if (freq > div_table[val + 1].freq) 182 break; 183 } 184 *postdiv = 1 << val; 185 } else { 186 for (val = 0; val < 5; val++) { 187 *postdiv = 1 << val; 188 if ((u64)freq * *postdiv >= fmin) 189 break; 190 } 191 } 192 193 /* _pcw = freq * postdiv / fin * 2^pcwfbits */ 194 ibits = pll->data->pcwibits ? pll->data->pcwibits : INTEGER_BITS; 195 _pcw = ((u64)freq << val) << (pll->data->pcwbits - ibits); 196 do_div(_pcw, fin); 197 198 *pcw = (u32)_pcw; 199 } 200 201 static int mtk_pll_set_rate(struct clk_hw *hw, unsigned long rate, 202 unsigned long parent_rate) 203 { 204 struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); 205 u32 pcw = 0; 206 u32 postdiv; 207 208 mtk_pll_calc_values(pll, &pcw, &postdiv, rate, parent_rate); 209 mtk_pll_set_rate_regs(pll, pcw, postdiv); 210 211 return 0; 212 } 213 214 static unsigned long mtk_pll_recalc_rate(struct clk_hw *hw, 215 unsigned long parent_rate) 216 { 217 struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); 218 u32 postdiv; 219 u32 pcw; 220 221 postdiv = (readl(pll->pd_addr) >> pll->data->pd_shift) & POSTDIV_MASK; 222 postdiv = 1 << postdiv; 223 224 pcw = readl(pll->pcw_addr) >> pll->data->pcw_shift; 225 pcw &= GENMASK(pll->data->pcwbits - 1, 0); 226 227 return __mtk_pll_recalc_rate(pll, parent_rate, pcw, postdiv); 228 } 229 230 static long mtk_pll_round_rate(struct clk_hw *hw, unsigned long rate, 231 unsigned long *prate) 232 { 233 struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); 234 u32 pcw = 0; 235 int postdiv; 236 237 mtk_pll_calc_values(pll, &pcw, &postdiv, rate, *prate); 238 239 return __mtk_pll_recalc_rate(pll, *prate, pcw, postdiv); 240 } 241 242 static int mtk_pll_prepare(struct clk_hw *hw) 243 { 244 struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); 245 u32 r; 246 u32 div_en_mask; 247 248 r = readl(pll->pwr_addr) | CON0_PWR_ON; 249 writel(r, pll->pwr_addr); 250 udelay(1); 251 252 r = readl(pll->pwr_addr) & ~CON0_ISO_EN; 253 writel(r, pll->pwr_addr); 254 udelay(1); 255 256 r = readl(pll->en_addr) | BIT(pll->data->pll_en_bit); 257 writel(r, pll->en_addr); 258 259 div_en_mask = pll->data->en_mask & ~CON0_BASE_EN; 260 if (div_en_mask) { 261 r = readl(pll->base_addr + REG_CON0) | div_en_mask; 262 writel(r, pll->base_addr + REG_CON0); 263 } 264 265 __mtk_pll_tuner_enable(pll); 266 267 udelay(20); 268 269 if (pll->data->flags & HAVE_RST_BAR) { 270 r = readl(pll->base_addr + REG_CON0); 271 r |= pll->data->rst_bar_mask; 272 writel(r, pll->base_addr + REG_CON0); 273 } 274 275 return 0; 276 } 277 278 static void mtk_pll_unprepare(struct clk_hw *hw) 279 { 280 struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); 281 u32 r; 282 u32 div_en_mask; 283 284 if (pll->data->flags & HAVE_RST_BAR) { 285 r = readl(pll->base_addr + REG_CON0); 286 r &= ~pll->data->rst_bar_mask; 287 writel(r, pll->base_addr + REG_CON0); 288 } 289 290 __mtk_pll_tuner_disable(pll); 291 292 div_en_mask = pll->data->en_mask & ~CON0_BASE_EN; 293 if (div_en_mask) { 294 r = readl(pll->base_addr + REG_CON0) & ~div_en_mask; 295 writel(r, pll->base_addr + REG_CON0); 296 } 297 298 r = readl(pll->en_addr) & ~BIT(pll->data->pll_en_bit); 299 writel(r, pll->en_addr); 300 301 r = readl(pll->pwr_addr) | CON0_ISO_EN; 302 writel(r, pll->pwr_addr); 303 304 r = readl(pll->pwr_addr) & ~CON0_PWR_ON; 305 writel(r, pll->pwr_addr); 306 } 307 308 static const struct clk_ops mtk_pll_ops = { 309 .is_prepared = mtk_pll_is_prepared, 310 .prepare = mtk_pll_prepare, 311 .unprepare = mtk_pll_unprepare, 312 .recalc_rate = mtk_pll_recalc_rate, 313 .round_rate = mtk_pll_round_rate, 314 .set_rate = mtk_pll_set_rate, 315 }; 316 317 static struct clk *mtk_clk_register_pll(const struct mtk_pll_data *data, 318 void __iomem *base) 319 { 320 struct mtk_clk_pll *pll; 321 struct clk_init_data init = {}; 322 struct clk *clk; 323 const char *parent_name = "clk26m"; 324 325 pll = kzalloc(sizeof(*pll), GFP_KERNEL); 326 if (!pll) 327 return ERR_PTR(-ENOMEM); 328 329 pll->base_addr = base + data->reg; 330 pll->pwr_addr = base + data->pwr_reg; 331 pll->pd_addr = base + data->pd_reg; 332 pll->pcw_addr = base + data->pcw_reg; 333 if (data->pcw_chg_reg) 334 pll->pcw_chg_addr = base + data->pcw_chg_reg; 335 else 336 pll->pcw_chg_addr = pll->base_addr + REG_CON1; 337 if (data->tuner_reg) 338 pll->tuner_addr = base + data->tuner_reg; 339 if (data->tuner_en_reg || data->tuner_en_bit) 340 pll->tuner_en_addr = base + data->tuner_en_reg; 341 if (data->en_reg) 342 pll->en_addr = base + data->en_reg; 343 else 344 pll->en_addr = pll->base_addr + REG_CON0; 345 pll->hw.init = &init; 346 pll->data = data; 347 348 init.name = data->name; 349 init.flags = (data->flags & PLL_AO) ? CLK_IS_CRITICAL : 0; 350 init.ops = &mtk_pll_ops; 351 if (data->parent_name) 352 init.parent_names = &data->parent_name; 353 else 354 init.parent_names = &parent_name; 355 init.num_parents = 1; 356 357 clk = clk_register(NULL, &pll->hw); 358 359 if (IS_ERR(clk)) 360 kfree(pll); 361 362 return clk; 363 } 364 365 static void mtk_clk_unregister_pll(struct clk *clk) 366 { 367 struct clk_hw *hw; 368 struct mtk_clk_pll *pll; 369 370 hw = __clk_get_hw(clk); 371 if (!hw) 372 return; 373 374 pll = to_mtk_clk_pll(hw); 375 376 clk_unregister(clk); 377 kfree(pll); 378 } 379 380 int mtk_clk_register_plls(struct device_node *node, 381 const struct mtk_pll_data *plls, int num_plls, 382 struct clk_onecell_data *clk_data) 383 { 384 void __iomem *base; 385 int i; 386 struct clk *clk; 387 388 base = of_iomap(node, 0); 389 if (!base) { 390 pr_err("%s(): ioremap failed\n", __func__); 391 return -EINVAL; 392 } 393 394 for (i = 0; i < num_plls; i++) { 395 const struct mtk_pll_data *pll = &plls[i]; 396 397 if (!IS_ERR_OR_NULL(clk_data->clks[pll->id])) { 398 pr_warn("%pOF: Trying to register duplicate clock ID: %d\n", 399 node, pll->id); 400 continue; 401 } 402 403 clk = mtk_clk_register_pll(pll, base); 404 405 if (IS_ERR(clk)) { 406 pr_err("Failed to register clk %s: %pe\n", pll->name, clk); 407 goto err; 408 } 409 410 clk_data->clks[pll->id] = clk; 411 } 412 413 return 0; 414 415 err: 416 while (--i >= 0) { 417 const struct mtk_pll_data *pll = &plls[i]; 418 419 mtk_clk_unregister_pll(clk_data->clks[pll->id]); 420 clk_data->clks[pll->id] = ERR_PTR(-ENOENT); 421 } 422 423 iounmap(base); 424 425 return PTR_ERR(clk); 426 } 427 EXPORT_SYMBOL_GPL(mtk_clk_register_plls); 428 429 static __iomem void *mtk_clk_pll_get_base(struct clk *clk, 430 const struct mtk_pll_data *data) 431 { 432 struct clk_hw *hw = __clk_get_hw(clk); 433 struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); 434 435 return pll->base_addr - data->reg; 436 } 437 438 void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls, 439 struct clk_onecell_data *clk_data) 440 { 441 __iomem void *base = NULL; 442 int i; 443 444 if (!clk_data) 445 return; 446 447 for (i = num_plls; i > 0; i--) { 448 const struct mtk_pll_data *pll = &plls[i - 1]; 449 450 if (IS_ERR_OR_NULL(clk_data->clks[pll->id])) 451 continue; 452 453 /* 454 * This is quite ugly but unfortunately the clks don't have 455 * any device tied to them, so there's no place to store the 456 * pointer to the I/O region base address. We have to fetch 457 * it from one of the registered clks. 458 */ 459 base = mtk_clk_pll_get_base(clk_data->clks[pll->id], pll); 460 461 mtk_clk_unregister_pll(clk_data->clks[pll->id]); 462 clk_data->clks[pll->id] = ERR_PTR(-ENOENT); 463 } 464 465 iounmap(base); 466 } 467 EXPORT_SYMBOL_GPL(mtk_clk_unregister_plls); 468 469 MODULE_LICENSE("GPL"); 470