| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 MediaTek Inc. |
| 3 | * Author: James Liao <jamesjj.liao@mediatek.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | #include <linux/of.h> |
| 15 | #include <linux/of_address.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/clkdev.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include "clk-mtk.h" |
| 21 | #define REG_CON0 0 |
| 22 | #define REG_CON1 4 |
| 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 | #define AUDPLL_TUNER_EN BIT(31) |
| 28 | #define POSTDIV_MASK 0x7 |
| 29 | #define INTEGER_BITS 7 |
| 30 | /* |
| 31 | * MediaTek PLLs are configured through their pcw value. The pcw value describes |
| 32 | * a divider in the PLL feedback loop which consists of 7 bits for the integer |
| 33 | * part and the remaining bits (if present) for the fractional part. Also they |
| 34 | * have a 3 bit power-of-two post divider. |
| 35 | */ |
| 36 | struct mtk_clk_pll { |
| 37 | struct clk_hw hw; |
| 38 | void __iomem *base_addr; |
| 39 | void __iomem *pd_addr; |
| 40 | void __iomem *pwr_addr; |
| 41 | void __iomem *tuner_addr; |
| 42 | void __iomem *tuner_en_addr; |
| 43 | void __iomem *pcw_addr; |
| 44 | void __iomem *pcw_chg_addr; |
| 45 | void __iomem *en_addr; |
| 46 | void __iomem *rst_bar_addr; |
| 47 | const struct mtk_pll_data *data; |
| 48 | uint32_t en_mask; |
| 49 | uint32_t iso_mask; |
| 50 | uint32_t pwron_mask; |
| 51 | }; |
| 52 | |
| 53 | bool (*mtk_fh_set_rate)(int pll_id, unsigned long dds, int postdiv) = NULL; |
| 54 | EXPORT_SYMBOL(mtk_fh_set_rate); |
| 55 | |
| 56 | static inline struct mtk_clk_pll *to_mtk_clk_pll(struct clk_hw *hw) |
| 57 | { |
| 58 | return container_of(hw, struct mtk_clk_pll, hw); |
| 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 | return (readl(pll->en_addr) & pll->data->en_mask) != 0; |
| 64 | } |
| 65 | static unsigned long __mtk_pll_recalc_rate(struct mtk_clk_pll *pll, u32 fin, |
| 66 | u32 pcw, int postdiv) |
| 67 | { |
| 68 | int pcwbits = pll->data->pcwbits; |
| 69 | int pcwfbits; |
| 70 | int ibits; |
| 71 | u64 vco; |
| 72 | u8 c = 0; |
| 73 | /* The fractional part of the PLL divider. */ |
| 74 | ibits = pll->data->pcwibits ? pll->data->pcwibits : INTEGER_BITS; |
| 75 | pcwfbits = pcwbits > ibits ? pcwbits - ibits : 0; |
| 76 | vco = (u64)fin * pcw; |
| 77 | if (pcwfbits && (vco & GENMASK(pcwfbits - 1, 0))) |
| 78 | c = 1; |
| 79 | vco >>= pcwfbits; |
| 80 | if (c) |
| 81 | vco++; |
| 82 | return ((unsigned long)vco + postdiv - 1) / postdiv; |
| 83 | } |
| 84 | static void mtk_pll_set_rate_regs(struct mtk_clk_pll *pll, u32 pcw, |
| 85 | int postdiv) |
| 86 | { |
| 87 | u32 val, chg; |
| 88 | u32 tuner_en = 0; |
| 89 | u32 tuner_en_mask; |
| 90 | void __iomem *tuner_en_addr = NULL; |
| 91 | /* disable tuner */ |
| 92 | if (pll->tuner_en_addr) { |
| 93 | tuner_en_addr = pll->tuner_en_addr; |
| 94 | tuner_en_mask = BIT(pll->data->tuner_en_bit); |
| 95 | } else if (pll->tuner_addr) { |
| 96 | tuner_en_addr = pll->tuner_addr; |
| 97 | tuner_en_mask = AUDPLL_TUNER_EN; |
| 98 | } |
| 99 | if (tuner_en_addr) { |
| 100 | val = readl(tuner_en_addr); |
| 101 | tuner_en = val & tuner_en_mask; |
| 102 | if (tuner_en) { |
| 103 | val &= ~tuner_en_mask; |
| 104 | writel(val, tuner_en_addr); |
| 105 | } |
| 106 | } |
| 107 | /* set postdiv */ |
| 108 | val = readl(pll->pd_addr); |
| 109 | val &= ~(POSTDIV_MASK << pll->data->pd_shift); |
| 110 | val |= (ffs(postdiv) - 1) << pll->data->pd_shift; |
| 111 | /* postdiv and pcw need to set at the same time if on same register */ |
| 112 | if (pll->pd_addr != pll->pcw_addr) { |
| 113 | writel(val, pll->pd_addr); |
| 114 | val = readl(pll->pcw_addr); |
| 115 | } |
| 116 | /* set pcw */ |
| 117 | val &= ~GENMASK(pll->data->pcw_shift + pll->data->pcwbits - 1, |
| 118 | pll->data->pcw_shift); |
| 119 | val |= pcw << pll->data->pcw_shift; |
| 120 | writel(val, pll->pcw_addr); |
| 121 | if (pll->tuner_addr) |
| 122 | writel(val + 1, pll->tuner_addr); |
| 123 | chg = readl(pll->pcw_chg_addr) | PCW_CHG_MASK; |
| 124 | writel(chg, pll->pcw_chg_addr); |
| 125 | /* restore tuner_en */ |
| 126 | if (tuner_en_addr && tuner_en) { |
| 127 | val = readl(tuner_en_addr); |
| 128 | val |= tuner_en_mask; |
| 129 | writel(val, tuner_en_addr); |
| 130 | } |
| 131 | udelay(20); |
| 132 | } |
| 133 | /* |
| 134 | * mtk_pll_calc_values - calculate good values for a given input frequency. |
| 135 | * @pll: The pll |
| 136 | * @pcw: The pcw value (output) |
| 137 | * @postdiv: The post divider (output) |
| 138 | * @freq: The desired target frequency |
| 139 | * @fin: The input frequency |
| 140 | * |
| 141 | */ |
| 142 | static void mtk_pll_calc_values(struct mtk_clk_pll *pll, u32 *pcw, u32 *postdiv, |
| 143 | u32 freq, u32 fin) |
| 144 | { |
| 145 | unsigned long fmin = pll->data->fmin ? pll->data->fmin : 1000 * MHZ; |
| 146 | const struct mtk_pll_div_table *div_table = pll->data->div_table; |
| 147 | u64 _pcw; |
| 148 | int ibits; |
| 149 | u32 val; |
| 150 | if (freq > pll->data->fmax) |
| 151 | freq = pll->data->fmax; |
| 152 | if (div_table) { |
| 153 | if (freq > div_table[0].freq) |
| 154 | freq = div_table[0].freq; |
| 155 | for (val = 0; div_table[val + 1].freq != 0; val++) { |
| 156 | if (freq > div_table[val + 1].freq) |
| 157 | break; |
| 158 | } |
| 159 | *postdiv = 1 << val; |
| 160 | } else { |
| 161 | for (val = 0; val < 5; val++) { |
| 162 | *postdiv = 1 << val; |
| 163 | if ((u64)freq * *postdiv >= fmin) |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | /* _pcw = freq * postdiv / fin * 2^pcwfbits */ |
| 168 | ibits = pll->data->pcwibits ? pll->data->pcwibits : INTEGER_BITS; |
| 169 | _pcw = ((u64)freq << val) << (pll->data->pcwbits - ibits); |
| 170 | do_div(_pcw, fin); |
| 171 | *pcw = (u32)_pcw; |
| 172 | } |
| 173 | static int mtk_pll_set_rate(struct clk_hw *hw, unsigned long rate, |
| 174 | unsigned long parent_rate) |
| 175 | { |
| 176 | struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); |
| 177 | u32 pcw = 0; |
| 178 | u32 postdiv; |
| 179 | mtk_pll_calc_values(pll, &pcw, &postdiv, rate, parent_rate); |
| 180 | if (!mtk_fh_set_rate || !mtk_fh_set_rate(pll->data->id, pcw, postdiv)) |
| 181 | mtk_pll_set_rate_regs(pll, pcw, postdiv); |
| 182 | return 0; |
| 183 | } |
| 184 | static unsigned long mtk_pll_recalc_rate(struct clk_hw *hw, |
| 185 | unsigned long parent_rate) |
| 186 | { |
| 187 | struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); |
| 188 | u32 postdiv; |
| 189 | u32 pcw; |
| 190 | postdiv = (readl(pll->pd_addr) >> pll->data->pd_shift) & POSTDIV_MASK; |
| 191 | postdiv = 1 << postdiv; |
| 192 | pcw = readl(pll->pcw_addr) >> pll->data->pcw_shift; |
| 193 | pcw &= GENMASK(pll->data->pcwbits - 1, 0); |
| 194 | return __mtk_pll_recalc_rate(pll, parent_rate, pcw, postdiv); |
| 195 | } |
| 196 | static long mtk_pll_round_rate(struct clk_hw *hw, unsigned long rate, |
| 197 | unsigned long *prate) |
| 198 | { |
| 199 | struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); |
| 200 | u32 pcw = 0; |
| 201 | int postdiv; |
| 202 | mtk_pll_calc_values(pll, &pcw, &postdiv, rate, *prate); |
| 203 | return __mtk_pll_recalc_rate(pll, *prate, pcw, postdiv); |
| 204 | } |
| 205 | static int mtk_pll_prepare(struct clk_hw *hw) |
| 206 | { |
| 207 | struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); |
| 208 | u32 r; |
| 209 | r = readl(pll->pwr_addr) | pll->pwron_mask; |
| 210 | writel(r, pll->pwr_addr); |
| 211 | udelay(1); |
| 212 | r = readl(pll->pwr_addr) & ~pll->iso_mask; |
| 213 | writel(r, pll->pwr_addr); |
| 214 | udelay(1); |
| 215 | r = readl(pll->en_addr) | pll->en_mask; |
| 216 | writel(r, pll->en_addr); |
| 217 | if (pll->tuner_en_addr) { |
| 218 | r = readl(pll->tuner_en_addr) | BIT(pll->data->tuner_en_bit); |
| 219 | writel(r, pll->tuner_en_addr); |
| 220 | } else if (pll->tuner_addr) { |
| 221 | r = readl(pll->tuner_addr) | AUDPLL_TUNER_EN; |
| 222 | writel(r, pll->tuner_addr); |
| 223 | } |
| 224 | udelay(20); |
| 225 | if (pll->data->flags & HAVE_RST_BAR) { |
| 226 | r = readl(pll->rst_bar_addr); |
| 227 | r |= pll->data->rst_bar_mask; |
| 228 | writel(r, pll->rst_bar_addr); |
| 229 | } |
| 230 | return 0; |
| 231 | } |
| 232 | static void mtk_pll_unprepare(struct clk_hw *hw) |
| 233 | { |
| 234 | struct mtk_clk_pll *pll = to_mtk_clk_pll(hw); |
| 235 | u32 r; |
| 236 | u32 i; |
| 237 | if (pll->data->flags & HAVE_RST_BAR_4_TIMES) { |
| 238 | for (i = 0; i < 3; i++) { |
| 239 | r = readl(pll->rst_bar_addr); |
| 240 | r &= ~pll->data->rst_bar_mask; |
| 241 | writel(r, pll->rst_bar_addr); |
| 242 | udelay(1); |
| 243 | r = readl(pll->rst_bar_addr); |
| 244 | r |= pll->data->rst_bar_mask; |
| 245 | writel(r, pll->rst_bar_addr); |
| 246 | udelay(1); |
| 247 | } |
| 248 | } |
| 249 | if (pll->data->flags & HAVE_RST_BAR) { |
| 250 | r = readl(pll->rst_bar_addr); |
| 251 | r &= ~pll->data->rst_bar_mask; |
| 252 | writel(r, pll->rst_bar_addr); |
| 253 | } |
| 254 | if (pll->tuner_en_addr) { |
| 255 | r = readl(pll->tuner_en_addr) & ~BIT(pll->data->tuner_en_bit); |
| 256 | writel(r, pll->tuner_en_addr); |
| 257 | } else if (pll->tuner_addr) { |
| 258 | r = readl(pll->tuner_addr) & ~AUDPLL_TUNER_EN; |
| 259 | writel(r, pll->tuner_addr); |
| 260 | } |
| 261 | r = readl(pll->en_addr) & ~pll->en_mask; |
| 262 | writel(r, pll->en_addr); |
| 263 | r = readl(pll->pwr_addr) | pll->iso_mask; |
| 264 | writel(r, pll->pwr_addr); |
| 265 | r = readl(pll->pwr_addr) & ~pll->pwron_mask; |
| 266 | writel(r, pll->pwr_addr); |
| 267 | } |
| 268 | static const struct clk_ops mtk_pll_ops = { |
| 269 | .is_prepared = mtk_pll_is_prepared, |
| 270 | .prepare = mtk_pll_prepare, |
| 271 | .unprepare = mtk_pll_unprepare, |
| 272 | .recalc_rate = mtk_pll_recalc_rate, |
| 273 | .round_rate = mtk_pll_round_rate, |
| 274 | .set_rate = mtk_pll_set_rate, |
| 275 | }; |
| 276 | static struct clk *mtk_clk_register_pll(const struct mtk_pll_data *data, |
| 277 | void __iomem *base) |
| 278 | { |
| 279 | struct mtk_clk_pll *pll; |
| 280 | struct clk_init_data init = {}; |
| 281 | struct clk *clk; |
| 282 | const char *parent_name = "clk26m"; |
| 283 | pll = kzalloc(sizeof(*pll), GFP_KERNEL); |
| 284 | if (!pll) |
| 285 | return ERR_PTR(-ENOMEM); |
| 286 | pll->base_addr = base + data->reg; |
| 287 | pll->pwr_addr = base + data->pwr_reg; |
| 288 | if (data->en_reg) |
| 289 | pll->en_addr = base + data->en_reg; |
| 290 | else |
| 291 | pll->en_addr = pll->base_addr + REG_CON0; |
| 292 | pll->pd_addr = base + data->pd_reg; |
| 293 | pll->pcw_addr = base + data->pcw_reg; |
| 294 | if (data->pcw_chg_reg) |
| 295 | pll->pcw_chg_addr = base + data->pcw_chg_reg; |
| 296 | else |
| 297 | pll->pcw_chg_addr = pll->base_addr + REG_CON1; |
| 298 | |
| 299 | if (data->rst_bar_reg) |
| 300 | pll->rst_bar_addr = base + data->rst_bar_reg; |
| 301 | else |
| 302 | pll->rst_bar_addr = pll->base_addr + REG_CON0; |
| 303 | if (data->tuner_reg) |
| 304 | pll->tuner_addr = base + data->tuner_reg; |
| 305 | if (data->tuner_en_reg) |
| 306 | pll->tuner_en_addr = base + data->tuner_en_reg; |
| 307 | if (data->en_mask) |
| 308 | pll->en_mask = data->en_mask; |
| 309 | else |
| 310 | pll->en_mask = CON0_BASE_EN; |
| 311 | if (data->iso_mask) |
| 312 | pll->iso_mask = data->iso_mask; |
| 313 | else |
| 314 | pll->iso_mask = CON0_ISO_EN; |
| 315 | if (data->pwron_mask) |
| 316 | pll->pwron_mask = data->pwron_mask; |
| 317 | else |
| 318 | pll->pwron_mask = CON0_PWR_ON; |
| 319 | pll->hw.init = &init; |
| 320 | pll->data = data; |
| 321 | init.name = data->name; |
| 322 | init.flags = (data->flags & PLL_AO) ? CLK_IS_CRITICAL : 0; |
| 323 | init.ops = &mtk_pll_ops; |
| 324 | if (data->parent_name) |
| 325 | init.parent_names = &data->parent_name; |
| 326 | else |
| 327 | init.parent_names = &parent_name; |
| 328 | init.num_parents = 1; |
| 329 | clk = clk_register(NULL, &pll->hw); |
| 330 | if (IS_ERR(clk)) |
| 331 | kfree(pll); |
| 332 | return clk; |
| 333 | } |
| 334 | void mtk_clk_register_plls(struct device_node *node, |
| 335 | const struct mtk_pll_data *plls, int num_plls, |
| 336 | struct clk_onecell_data *clk_data) |
| 337 | { |
| 338 | void __iomem *base; |
| 339 | int i; |
| 340 | struct clk *clk; |
| 341 | base = of_iomap(node, 0); |
| 342 | if (!base) { |
| 343 | pr_err("%s(): ioremap failed\n", __func__); |
| 344 | return; |
| 345 | } |
| 346 | for (i = 0; i < num_plls; i++) { |
| 347 | const struct mtk_pll_data *pll = &plls[i]; |
| 348 | clk = mtk_clk_register_pll(pll, base); |
| 349 | if (IS_ERR(clk)) { |
| 350 | pr_err("Failed to register clk %s: %ld\n", |
| 351 | pll->name, PTR_ERR(clk)); |
| 352 | continue; |
| 353 | } |
| 354 | clk_data->clks[pll->id] = clk; |
| 355 | } |
| 356 | } |