clk-pll.c 8.2 KB

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