ccu_mp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright (C) 2016 Maxime Ripard
  3. * Maxime Ripard <maxime.ripard@free-electrons.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. */
  10. #include <linux/clk-provider.h>
  11. #include "ccu_gate.h"
  12. #include "ccu_mp.h"
  13. static void ccu_mp_find_best(unsigned long parent, unsigned long rate,
  14. unsigned int max_m, unsigned int max_p,
  15. unsigned int *m, unsigned int *p)
  16. {
  17. unsigned long best_rate = 0;
  18. unsigned int best_m = 0, best_p = 0;
  19. unsigned int _m, _p;
  20. for (_p = 1; _p <= max_p; _p <<= 1) {
  21. for (_m = 1; _m <= max_m; _m++) {
  22. unsigned long tmp_rate = parent / _p / _m;
  23. if (tmp_rate > rate)
  24. continue;
  25. if ((rate - tmp_rate) < (rate - best_rate)) {
  26. best_rate = tmp_rate;
  27. best_m = _m;
  28. best_p = _p;
  29. }
  30. }
  31. }
  32. *m = best_m;
  33. *p = best_p;
  34. }
  35. static unsigned long ccu_mp_round_rate(struct ccu_mux_internal *mux,
  36. struct clk_hw *hw,
  37. unsigned long *parent_rate,
  38. unsigned long rate,
  39. void *data)
  40. {
  41. struct ccu_mp *cmp = data;
  42. unsigned int max_m, max_p;
  43. unsigned int m, p;
  44. if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
  45. rate *= cmp->fixed_post_div;
  46. max_m = cmp->m.max ?: 1 << cmp->m.width;
  47. max_p = cmp->p.max ?: 1 << ((1 << cmp->p.width) - 1);
  48. ccu_mp_find_best(*parent_rate, rate, max_m, max_p, &m, &p);
  49. rate = *parent_rate / p / m;
  50. if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
  51. rate /= cmp->fixed_post_div;
  52. return rate;
  53. }
  54. static void ccu_mp_disable(struct clk_hw *hw)
  55. {
  56. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  57. return ccu_gate_helper_disable(&cmp->common, cmp->enable);
  58. }
  59. static int ccu_mp_enable(struct clk_hw *hw)
  60. {
  61. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  62. return ccu_gate_helper_enable(&cmp->common, cmp->enable);
  63. }
  64. static int ccu_mp_is_enabled(struct clk_hw *hw)
  65. {
  66. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  67. return ccu_gate_helper_is_enabled(&cmp->common, cmp->enable);
  68. }
  69. static unsigned long ccu_mp_recalc_rate(struct clk_hw *hw,
  70. unsigned long parent_rate)
  71. {
  72. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  73. unsigned long rate;
  74. unsigned int m, p;
  75. u32 reg;
  76. /* Adjust parent_rate according to pre-dividers */
  77. parent_rate = ccu_mux_helper_apply_prediv(&cmp->common, &cmp->mux, -1,
  78. parent_rate);
  79. reg = readl(cmp->common.base + cmp->common.reg);
  80. m = reg >> cmp->m.shift;
  81. m &= (1 << cmp->m.width) - 1;
  82. m += cmp->m.offset;
  83. if (!m)
  84. m++;
  85. p = reg >> cmp->p.shift;
  86. p &= (1 << cmp->p.width) - 1;
  87. rate = (parent_rate >> p) / m;
  88. if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
  89. rate /= cmp->fixed_post_div;
  90. return rate;
  91. }
  92. static int ccu_mp_determine_rate(struct clk_hw *hw,
  93. struct clk_rate_request *req)
  94. {
  95. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  96. return ccu_mux_helper_determine_rate(&cmp->common, &cmp->mux,
  97. req, ccu_mp_round_rate, cmp);
  98. }
  99. static int ccu_mp_set_rate(struct clk_hw *hw, unsigned long rate,
  100. unsigned long parent_rate)
  101. {
  102. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  103. unsigned long flags;
  104. unsigned int max_m, max_p;
  105. unsigned int m, p;
  106. u32 reg;
  107. /* Adjust parent_rate according to pre-dividers */
  108. parent_rate = ccu_mux_helper_apply_prediv(&cmp->common, &cmp->mux, -1,
  109. parent_rate);
  110. max_m = cmp->m.max ?: 1 << cmp->m.width;
  111. max_p = cmp->p.max ?: 1 << ((1 << cmp->p.width) - 1);
  112. /* Adjust target rate according to post-dividers */
  113. if (cmp->common.features & CCU_FEATURE_FIXED_POSTDIV)
  114. rate = rate * cmp->fixed_post_div;
  115. ccu_mp_find_best(parent_rate, rate, max_m, max_p, &m, &p);
  116. spin_lock_irqsave(cmp->common.lock, flags);
  117. reg = readl(cmp->common.base + cmp->common.reg);
  118. reg &= ~GENMASK(cmp->m.width + cmp->m.shift - 1, cmp->m.shift);
  119. reg &= ~GENMASK(cmp->p.width + cmp->p.shift - 1, cmp->p.shift);
  120. reg |= (m - cmp->m.offset) << cmp->m.shift;
  121. reg |= ilog2(p) << cmp->p.shift;
  122. writel(reg, cmp->common.base + cmp->common.reg);
  123. spin_unlock_irqrestore(cmp->common.lock, flags);
  124. return 0;
  125. }
  126. static u8 ccu_mp_get_parent(struct clk_hw *hw)
  127. {
  128. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  129. return ccu_mux_helper_get_parent(&cmp->common, &cmp->mux);
  130. }
  131. static int ccu_mp_set_parent(struct clk_hw *hw, u8 index)
  132. {
  133. struct ccu_mp *cmp = hw_to_ccu_mp(hw);
  134. return ccu_mux_helper_set_parent(&cmp->common, &cmp->mux, index);
  135. }
  136. const struct clk_ops ccu_mp_ops = {
  137. .disable = ccu_mp_disable,
  138. .enable = ccu_mp_enable,
  139. .is_enabled = ccu_mp_is_enabled,
  140. .get_parent = ccu_mp_get_parent,
  141. .set_parent = ccu_mp_set_parent,
  142. .determine_rate = ccu_mp_determine_rate,
  143. .recalc_rate = ccu_mp_recalc_rate,
  144. .set_rate = ccu_mp_set_rate,
  145. };
  146. /*
  147. * Support for MMC timing mode switching
  148. *
  149. * The MMC clocks on some SoCs support switching between old and
  150. * new timing modes. A platform specific API is provided to query
  151. * and set the timing mode on supported SoCs.
  152. *
  153. * In addition, a special class of ccu_mp_ops is provided, which
  154. * takes in to account the timing mode switch. When the new timing
  155. * mode is active, the clock output rate is halved. This new class
  156. * is a wrapper around the generic ccu_mp_ops. When clock rates
  157. * are passed through to ccu_mp_ops callbacks, they are doubled
  158. * if the new timing mode bit is set, to account for the post
  159. * divider. Conversely, when clock rates are passed back, they
  160. * are halved if the mode bit is set.
  161. */
  162. static unsigned long ccu_mp_mmc_recalc_rate(struct clk_hw *hw,
  163. unsigned long parent_rate)
  164. {
  165. unsigned long rate = ccu_mp_recalc_rate(hw, parent_rate);
  166. struct ccu_common *cm = hw_to_ccu_common(hw);
  167. u32 val = readl(cm->base + cm->reg);
  168. if (val & CCU_MMC_NEW_TIMING_MODE)
  169. return rate / 2;
  170. return rate;
  171. }
  172. static int ccu_mp_mmc_determine_rate(struct clk_hw *hw,
  173. struct clk_rate_request *req)
  174. {
  175. struct ccu_common *cm = hw_to_ccu_common(hw);
  176. u32 val = readl(cm->base + cm->reg);
  177. int ret;
  178. /* adjust the requested clock rate */
  179. if (val & CCU_MMC_NEW_TIMING_MODE) {
  180. req->rate *= 2;
  181. req->min_rate *= 2;
  182. req->max_rate *= 2;
  183. }
  184. ret = ccu_mp_determine_rate(hw, req);
  185. /* re-adjust the requested clock rate back */
  186. if (val & CCU_MMC_NEW_TIMING_MODE) {
  187. req->rate /= 2;
  188. req->min_rate /= 2;
  189. req->max_rate /= 2;
  190. }
  191. return ret;
  192. }
  193. static int ccu_mp_mmc_set_rate(struct clk_hw *hw, unsigned long rate,
  194. unsigned long parent_rate)
  195. {
  196. struct ccu_common *cm = hw_to_ccu_common(hw);
  197. u32 val = readl(cm->base + cm->reg);
  198. if (val & CCU_MMC_NEW_TIMING_MODE)
  199. rate *= 2;
  200. return ccu_mp_set_rate(hw, rate, parent_rate);
  201. }
  202. const struct clk_ops ccu_mp_mmc_ops = {
  203. .disable = ccu_mp_disable,
  204. .enable = ccu_mp_enable,
  205. .is_enabled = ccu_mp_is_enabled,
  206. .get_parent = ccu_mp_get_parent,
  207. .set_parent = ccu_mp_set_parent,
  208. .determine_rate = ccu_mp_mmc_determine_rate,
  209. .recalc_rate = ccu_mp_mmc_recalc_rate,
  210. .set_rate = ccu_mp_mmc_set_rate,
  211. };