clk-pll.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2013 Boris BREZILLON <[email protected]>
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/clkdev.h>
  7. #include <linux/clk/at91_pmc.h>
  8. #include <linux/of.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/regmap.h>
  11. #include "pmc.h"
  12. #define PLL_STATUS_MASK(id) (1 << (1 + (id)))
  13. #define PLL_REG(id) (AT91_CKGR_PLLAR + ((id) * 4))
  14. #define PLL_DIV_MASK 0xff
  15. #define PLL_DIV_MAX PLL_DIV_MASK
  16. #define PLL_DIV(reg) ((reg) & PLL_DIV_MASK)
  17. #define PLL_MUL(reg, layout) (((reg) >> (layout)->mul_shift) & \
  18. (layout)->mul_mask)
  19. #define PLL_MUL_MIN 2
  20. #define PLL_MUL_MASK(layout) ((layout)->mul_mask)
  21. #define PLL_MUL_MAX(layout) (PLL_MUL_MASK(layout) + 1)
  22. #define PLL_ICPR_SHIFT(id) ((id) * 16)
  23. #define PLL_ICPR_MASK(id) (0xffff << PLL_ICPR_SHIFT(id))
  24. #define PLL_MAX_COUNT 0x3f
  25. #define PLL_COUNT_SHIFT 8
  26. #define PLL_OUT_SHIFT 14
  27. #define PLL_MAX_ID 1
  28. #define to_clk_pll(hw) container_of(hw, struct clk_pll, hw)
  29. struct clk_pll {
  30. struct clk_hw hw;
  31. struct regmap *regmap;
  32. u8 id;
  33. u8 div;
  34. u8 range;
  35. u16 mul;
  36. const struct clk_pll_layout *layout;
  37. const struct clk_pll_characteristics *characteristics;
  38. struct at91_clk_pms pms;
  39. };
  40. static inline bool clk_pll_ready(struct regmap *regmap, int id)
  41. {
  42. unsigned int status;
  43. regmap_read(regmap, AT91_PMC_SR, &status);
  44. return status & PLL_STATUS_MASK(id) ? 1 : 0;
  45. }
  46. static int clk_pll_prepare(struct clk_hw *hw)
  47. {
  48. struct clk_pll *pll = to_clk_pll(hw);
  49. struct regmap *regmap = pll->regmap;
  50. const struct clk_pll_layout *layout = pll->layout;
  51. const struct clk_pll_characteristics *characteristics =
  52. pll->characteristics;
  53. u8 id = pll->id;
  54. u32 mask = PLL_STATUS_MASK(id);
  55. int offset = PLL_REG(id);
  56. u8 out = 0;
  57. unsigned int pllr;
  58. unsigned int status;
  59. u8 div;
  60. u16 mul;
  61. regmap_read(regmap, offset, &pllr);
  62. div = PLL_DIV(pllr);
  63. mul = PLL_MUL(pllr, layout);
  64. regmap_read(regmap, AT91_PMC_SR, &status);
  65. if ((status & mask) &&
  66. (div == pll->div && mul == pll->mul))
  67. return 0;
  68. if (characteristics->out)
  69. out = characteristics->out[pll->range];
  70. if (characteristics->icpll)
  71. regmap_update_bits(regmap, AT91_PMC_PLLICPR, PLL_ICPR_MASK(id),
  72. characteristics->icpll[pll->range] << PLL_ICPR_SHIFT(id));
  73. regmap_update_bits(regmap, offset, layout->pllr_mask,
  74. pll->div | (PLL_MAX_COUNT << PLL_COUNT_SHIFT) |
  75. (out << PLL_OUT_SHIFT) |
  76. ((pll->mul & layout->mul_mask) << layout->mul_shift));
  77. while (!clk_pll_ready(regmap, pll->id))
  78. cpu_relax();
  79. return 0;
  80. }
  81. static int clk_pll_is_prepared(struct clk_hw *hw)
  82. {
  83. struct clk_pll *pll = to_clk_pll(hw);
  84. return clk_pll_ready(pll->regmap, pll->id);
  85. }
  86. static void clk_pll_unprepare(struct clk_hw *hw)
  87. {
  88. struct clk_pll *pll = to_clk_pll(hw);
  89. unsigned int mask = pll->layout->pllr_mask;
  90. regmap_update_bits(pll->regmap, PLL_REG(pll->id), mask, ~mask);
  91. }
  92. static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,
  93. unsigned long parent_rate)
  94. {
  95. struct clk_pll *pll = to_clk_pll(hw);
  96. if (!pll->div || !pll->mul)
  97. return 0;
  98. return (parent_rate / pll->div) * (pll->mul + 1);
  99. }
  100. static long clk_pll_get_best_div_mul(struct clk_pll *pll, unsigned long rate,
  101. unsigned long parent_rate,
  102. u32 *div, u32 *mul,
  103. u32 *index) {
  104. const struct clk_pll_layout *layout = pll->layout;
  105. const struct clk_pll_characteristics *characteristics =
  106. pll->characteristics;
  107. unsigned long bestremainder = ULONG_MAX;
  108. unsigned long maxdiv, mindiv, tmpdiv;
  109. long bestrate = -ERANGE;
  110. unsigned long bestdiv;
  111. unsigned long bestmul;
  112. int i = 0;
  113. /* Check if parent_rate is a valid input rate */
  114. if (parent_rate < characteristics->input.min)
  115. return -ERANGE;
  116. /*
  117. * Calculate minimum divider based on the minimum multiplier, the
  118. * parent_rate and the requested rate.
  119. * Should always be 2 according to the input and output characteristics
  120. * of the PLL blocks.
  121. */
  122. mindiv = (parent_rate * PLL_MUL_MIN) / rate;
  123. if (!mindiv)
  124. mindiv = 1;
  125. if (parent_rate > characteristics->input.max) {
  126. tmpdiv = DIV_ROUND_UP(parent_rate, characteristics->input.max);
  127. if (tmpdiv > PLL_DIV_MAX)
  128. return -ERANGE;
  129. if (tmpdiv > mindiv)
  130. mindiv = tmpdiv;
  131. }
  132. /*
  133. * Calculate the maximum divider which is limited by PLL register
  134. * layout (limited by the MUL or DIV field size).
  135. */
  136. maxdiv = DIV_ROUND_UP(parent_rate * PLL_MUL_MAX(layout), rate);
  137. if (maxdiv > PLL_DIV_MAX)
  138. maxdiv = PLL_DIV_MAX;
  139. /*
  140. * Iterate over the acceptable divider values to find the best
  141. * divider/multiplier pair (the one that generates the closest
  142. * rate to the requested one).
  143. */
  144. for (tmpdiv = mindiv; tmpdiv <= maxdiv; tmpdiv++) {
  145. unsigned long remainder;
  146. unsigned long tmprate;
  147. unsigned long tmpmul;
  148. /*
  149. * Calculate the multiplier associated with the current
  150. * divider that provide the closest rate to the requested one.
  151. */
  152. tmpmul = DIV_ROUND_CLOSEST(rate, parent_rate / tmpdiv);
  153. tmprate = (parent_rate / tmpdiv) * tmpmul;
  154. if (tmprate > rate)
  155. remainder = tmprate - rate;
  156. else
  157. remainder = rate - tmprate;
  158. /*
  159. * Compare the remainder with the best remainder found until
  160. * now and elect a new best multiplier/divider pair if the
  161. * current remainder is smaller than the best one.
  162. */
  163. if (remainder < bestremainder) {
  164. bestremainder = remainder;
  165. bestdiv = tmpdiv;
  166. bestmul = tmpmul;
  167. bestrate = tmprate;
  168. }
  169. /*
  170. * We've found a perfect match!
  171. * Stop searching now and use this multiplier/divider pair.
  172. */
  173. if (!remainder)
  174. break;
  175. }
  176. /* We haven't found any multiplier/divider pair => return -ERANGE */
  177. if (bestrate < 0)
  178. return bestrate;
  179. /* Check if bestrate is a valid output rate */
  180. for (i = 0; i < characteristics->num_output; i++) {
  181. if (bestrate >= characteristics->output[i].min &&
  182. bestrate <= characteristics->output[i].max)
  183. break;
  184. }
  185. if (i >= characteristics->num_output)
  186. return -ERANGE;
  187. if (div)
  188. *div = bestdiv;
  189. if (mul)
  190. *mul = bestmul - 1;
  191. if (index)
  192. *index = i;
  193. return bestrate;
  194. }
  195. static long clk_pll_round_rate(struct clk_hw *hw, unsigned long rate,
  196. unsigned long *parent_rate)
  197. {
  198. struct clk_pll *pll = to_clk_pll(hw);
  199. return clk_pll_get_best_div_mul(pll, rate, *parent_rate,
  200. NULL, NULL, NULL);
  201. }
  202. static int clk_pll_set_rate(struct clk_hw *hw, unsigned long rate,
  203. unsigned long parent_rate)
  204. {
  205. struct clk_pll *pll = to_clk_pll(hw);
  206. long ret;
  207. u32 div;
  208. u32 mul;
  209. u32 index;
  210. ret = clk_pll_get_best_div_mul(pll, rate, parent_rate,
  211. &div, &mul, &index);
  212. if (ret < 0)
  213. return ret;
  214. pll->range = index;
  215. pll->div = div;
  216. pll->mul = mul;
  217. return 0;
  218. }
  219. static int clk_pll_save_context(struct clk_hw *hw)
  220. {
  221. struct clk_pll *pll = to_clk_pll(hw);
  222. struct clk_hw *parent_hw = clk_hw_get_parent(hw);
  223. pll->pms.parent_rate = clk_hw_get_rate(parent_hw);
  224. pll->pms.rate = clk_pll_recalc_rate(&pll->hw, pll->pms.parent_rate);
  225. pll->pms.status = clk_pll_ready(pll->regmap, PLL_REG(pll->id));
  226. return 0;
  227. }
  228. static void clk_pll_restore_context(struct clk_hw *hw)
  229. {
  230. struct clk_pll *pll = to_clk_pll(hw);
  231. unsigned long calc_rate;
  232. unsigned int pllr, pllr_out, pllr_count;
  233. u8 out = 0;
  234. if (pll->characteristics->out)
  235. out = pll->characteristics->out[pll->range];
  236. regmap_read(pll->regmap, PLL_REG(pll->id), &pllr);
  237. calc_rate = (pll->pms.parent_rate / PLL_DIV(pllr)) *
  238. (PLL_MUL(pllr, pll->layout) + 1);
  239. pllr_count = (pllr >> PLL_COUNT_SHIFT) & PLL_MAX_COUNT;
  240. pllr_out = (pllr >> PLL_OUT_SHIFT) & out;
  241. if (pll->pms.rate != calc_rate ||
  242. pll->pms.status != clk_pll_ready(pll->regmap, PLL_REG(pll->id)) ||
  243. pllr_count != PLL_MAX_COUNT ||
  244. (out && pllr_out != out))
  245. pr_warn("PLLAR was not configured properly by firmware\n");
  246. }
  247. static const struct clk_ops pll_ops = {
  248. .prepare = clk_pll_prepare,
  249. .unprepare = clk_pll_unprepare,
  250. .is_prepared = clk_pll_is_prepared,
  251. .recalc_rate = clk_pll_recalc_rate,
  252. .round_rate = clk_pll_round_rate,
  253. .set_rate = clk_pll_set_rate,
  254. .save_context = clk_pll_save_context,
  255. .restore_context = clk_pll_restore_context,
  256. };
  257. struct clk_hw * __init
  258. at91_clk_register_pll(struct regmap *regmap, const char *name,
  259. const char *parent_name, u8 id,
  260. const struct clk_pll_layout *layout,
  261. const struct clk_pll_characteristics *characteristics)
  262. {
  263. struct clk_pll *pll;
  264. struct clk_hw *hw;
  265. struct clk_init_data init;
  266. int offset = PLL_REG(id);
  267. unsigned int pllr;
  268. int ret;
  269. if (id > PLL_MAX_ID)
  270. return ERR_PTR(-EINVAL);
  271. pll = kzalloc(sizeof(*pll), GFP_KERNEL);
  272. if (!pll)
  273. return ERR_PTR(-ENOMEM);
  274. init.name = name;
  275. init.ops = &pll_ops;
  276. init.parent_names = &parent_name;
  277. init.num_parents = 1;
  278. init.flags = CLK_SET_RATE_GATE;
  279. pll->id = id;
  280. pll->hw.init = &init;
  281. pll->layout = layout;
  282. pll->characteristics = characteristics;
  283. pll->regmap = regmap;
  284. regmap_read(regmap, offset, &pllr);
  285. pll->div = PLL_DIV(pllr);
  286. pll->mul = PLL_MUL(pllr, layout);
  287. hw = &pll->hw;
  288. ret = clk_hw_register(NULL, &pll->hw);
  289. if (ret) {
  290. kfree(pll);
  291. hw = ERR_PTR(ret);
  292. }
  293. return hw;
  294. }
  295. const struct clk_pll_layout at91rm9200_pll_layout = {
  296. .pllr_mask = 0x7FFFFFF,
  297. .mul_shift = 16,
  298. .mul_mask = 0x7FF,
  299. };
  300. const struct clk_pll_layout at91sam9g45_pll_layout = {
  301. .pllr_mask = 0xFFFFFF,
  302. .mul_shift = 16,
  303. .mul_mask = 0xFF,
  304. };
  305. const struct clk_pll_layout at91sam9g20_pllb_layout = {
  306. .pllr_mask = 0x3FFFFF,
  307. .mul_shift = 16,
  308. .mul_mask = 0x3F,
  309. };
  310. const struct clk_pll_layout sama5d3_pll_layout = {
  311. .pllr_mask = 0x1FFFFFF,
  312. .mul_shift = 18,
  313. .mul_mask = 0x7F,
  314. };