clk-pll.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 CON0_PCW_CHG 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 *pcw_addr;
  43. const struct mtk_pll_data *data;
  44. };
  45. static inline struct mtk_clk_pll *to_mtk_clk_pll(struct clk_hw *hw)
  46. {
  47. return container_of(hw, struct mtk_clk_pll, hw);
  48. }
  49. static int mtk_pll_is_prepared(struct clk_hw *hw)
  50. {
  51. struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
  52. return (readl(pll->base_addr + REG_CON0) & CON0_BASE_EN) != 0;
  53. }
  54. static unsigned long __mtk_pll_recalc_rate(struct mtk_clk_pll *pll, u32 fin,
  55. u32 pcw, int postdiv)
  56. {
  57. int pcwbits = pll->data->pcwbits;
  58. int pcwfbits;
  59. u64 vco;
  60. u8 c = 0;
  61. /* The fractional part of the PLL divider. */
  62. pcwfbits = pcwbits > INTEGER_BITS ? pcwbits - INTEGER_BITS : 0;
  63. vco = (u64)fin * pcw;
  64. if (pcwfbits && (vco & GENMASK(pcwfbits - 1, 0)))
  65. c = 1;
  66. vco >>= pcwfbits;
  67. if (c)
  68. vco++;
  69. return ((unsigned long)vco + postdiv - 1) / postdiv;
  70. }
  71. static void mtk_pll_set_rate_regs(struct mtk_clk_pll *pll, u32 pcw,
  72. int postdiv)
  73. {
  74. u32 con1, pd, val;
  75. int pll_en;
  76. /* set postdiv */
  77. pd = readl(pll->pd_addr);
  78. pd &= ~(POSTDIV_MASK << pll->data->pd_shift);
  79. pd |= (ffs(postdiv) - 1) << pll->data->pd_shift;
  80. writel(pd, pll->pd_addr);
  81. pll_en = readl(pll->base_addr + REG_CON0) & CON0_BASE_EN;
  82. /* set pcw */
  83. val = readl(pll->pcw_addr);
  84. val &= ~GENMASK(pll->data->pcw_shift + pll->data->pcwbits - 1,
  85. pll->data->pcw_shift);
  86. val |= pcw << pll->data->pcw_shift;
  87. writel(val, pll->pcw_addr);
  88. con1 = readl(pll->base_addr + REG_CON1);
  89. if (pll_en)
  90. con1 |= CON0_PCW_CHG;
  91. writel(con1, pll->base_addr + REG_CON1);
  92. if (pll->tuner_addr)
  93. writel(con1 + 1, pll->tuner_addr);
  94. if (pll_en)
  95. udelay(20);
  96. }
  97. /*
  98. * mtk_pll_calc_values - calculate good values for a given input frequency.
  99. * @pll: The pll
  100. * @pcw: The pcw value (output)
  101. * @postdiv: The post divider (output)
  102. * @freq: The desired target frequency
  103. * @fin: The input frequency
  104. *
  105. */
  106. static void mtk_pll_calc_values(struct mtk_clk_pll *pll, u32 *pcw, u32 *postdiv,
  107. u32 freq, u32 fin)
  108. {
  109. unsigned long fmin = 1000 * MHZ;
  110. u64 _pcw;
  111. u32 val;
  112. if (freq > pll->data->fmax)
  113. freq = pll->data->fmax;
  114. for (val = 0; val < 4; val++) {
  115. *postdiv = 1 << val;
  116. if (freq * *postdiv >= fmin)
  117. break;
  118. }
  119. /* _pcw = freq * postdiv / fin * 2^pcwfbits */
  120. _pcw = ((u64)freq << val) << (pll->data->pcwbits - INTEGER_BITS);
  121. do_div(_pcw, fin);
  122. *pcw = (u32)_pcw;
  123. }
  124. static int mtk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  125. unsigned long parent_rate)
  126. {
  127. struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
  128. u32 pcw = 0;
  129. u32 postdiv;
  130. mtk_pll_calc_values(pll, &pcw, &postdiv, rate, parent_rate);
  131. mtk_pll_set_rate_regs(pll, pcw, postdiv);
  132. return 0;
  133. }
  134. static unsigned long mtk_pll_recalc_rate(struct clk_hw *hw,
  135. unsigned long parent_rate)
  136. {
  137. struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
  138. u32 postdiv;
  139. u32 pcw;
  140. postdiv = (readl(pll->pd_addr) >> pll->data->pd_shift) & POSTDIV_MASK;
  141. postdiv = 1 << postdiv;
  142. pcw = readl(pll->pcw_addr) >> pll->data->pcw_shift;
  143. pcw &= GENMASK(pll->data->pcwbits - 1, 0);
  144. return __mtk_pll_recalc_rate(pll, parent_rate, pcw, postdiv);
  145. }
  146. static long mtk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  147. unsigned long *prate)
  148. {
  149. struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
  150. u32 pcw = 0;
  151. int postdiv;
  152. mtk_pll_calc_values(pll, &pcw, &postdiv, rate, *prate);
  153. return __mtk_pll_recalc_rate(pll, *prate, pcw, postdiv);
  154. }
  155. static int mtk_pll_prepare(struct clk_hw *hw)
  156. {
  157. struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
  158. u32 r;
  159. r = readl(pll->pwr_addr) | CON0_PWR_ON;
  160. writel(r, pll->pwr_addr);
  161. udelay(1);
  162. r = readl(pll->pwr_addr) & ~CON0_ISO_EN;
  163. writel(r, pll->pwr_addr);
  164. udelay(1);
  165. r = readl(pll->base_addr + REG_CON0);
  166. r |= pll->data->en_mask;
  167. writel(r, pll->base_addr + REG_CON0);
  168. if (pll->tuner_addr) {
  169. r = readl(pll->tuner_addr) | AUDPLL_TUNER_EN;
  170. writel(r, pll->tuner_addr);
  171. }
  172. udelay(20);
  173. if (pll->data->flags & HAVE_RST_BAR) {
  174. r = readl(pll->base_addr + REG_CON0);
  175. r |= pll->data->rst_bar_mask;
  176. writel(r, pll->base_addr + REG_CON0);
  177. }
  178. return 0;
  179. }
  180. static void mtk_pll_unprepare(struct clk_hw *hw)
  181. {
  182. struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
  183. u32 r;
  184. if (pll->data->flags & HAVE_RST_BAR) {
  185. r = readl(pll->base_addr + REG_CON0);
  186. r &= ~pll->data->rst_bar_mask;
  187. writel(r, pll->base_addr + REG_CON0);
  188. }
  189. if (pll->tuner_addr) {
  190. r = readl(pll->tuner_addr) & ~AUDPLL_TUNER_EN;
  191. writel(r, pll->tuner_addr);
  192. }
  193. r = readl(pll->base_addr + REG_CON0);
  194. r &= ~CON0_BASE_EN;
  195. writel(r, pll->base_addr + REG_CON0);
  196. r = readl(pll->pwr_addr) | CON0_ISO_EN;
  197. writel(r, pll->pwr_addr);
  198. r = readl(pll->pwr_addr) & ~CON0_PWR_ON;
  199. writel(r, pll->pwr_addr);
  200. }
  201. static const struct clk_ops mtk_pll_ops = {
  202. .is_prepared = mtk_pll_is_prepared,
  203. .prepare = mtk_pll_prepare,
  204. .unprepare = mtk_pll_unprepare,
  205. .recalc_rate = mtk_pll_recalc_rate,
  206. .round_rate = mtk_pll_round_rate,
  207. .set_rate = mtk_pll_set_rate,
  208. };
  209. static struct clk *mtk_clk_register_pll(const struct mtk_pll_data *data,
  210. void __iomem *base)
  211. {
  212. struct mtk_clk_pll *pll;
  213. struct clk_init_data init = {};
  214. struct clk *clk;
  215. const char *parent_name = "clk26m";
  216. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  217. if (!pll)
  218. return ERR_PTR(-ENOMEM);
  219. pll->base_addr = base + data->reg;
  220. pll->pwr_addr = base + data->pwr_reg;
  221. pll->pd_addr = base + data->pd_reg;
  222. pll->pcw_addr = base + data->pcw_reg;
  223. if (data->tuner_reg)
  224. pll->tuner_addr = base + data->tuner_reg;
  225. pll->hw.init = &init;
  226. pll->data = data;
  227. init.name = data->name;
  228. init.ops = &mtk_pll_ops;
  229. init.parent_names = &parent_name;
  230. init.num_parents = 1;
  231. clk = clk_register(NULL, &pll->hw);
  232. if (IS_ERR(clk))
  233. kfree(pll);
  234. return clk;
  235. }
  236. void __init mtk_clk_register_plls(struct device_node *node,
  237. const struct mtk_pll_data *plls, int num_plls, struct clk_onecell_data *clk_data)
  238. {
  239. void __iomem *base;
  240. int i;
  241. struct clk *clk;
  242. base = of_iomap(node, 0);
  243. if (!base) {
  244. pr_err("%s(): ioremap failed\n", __func__);
  245. return;
  246. }
  247. for (i = 0; i < num_plls; i++) {
  248. const struct mtk_pll_data *pll = &plls[i];
  249. clk = mtk_clk_register_pll(pll, base);
  250. if (IS_ERR(clk)) {
  251. pr_err("Failed to register clk %s: %ld\n",
  252. pll->name, PTR_ERR(clk));
  253. continue;
  254. }
  255. clk_data->clks[pll->id] = clk;
  256. }
  257. }