clk-pll.c 7.8 KB

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