clk-pll.c 8.6 KB

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