clk-mmc-phase.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2014 Google, Inc
  4. * Author: Alexandru M Stan <amstan@chromium.org>
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/io.h>
  10. #include <linux/kernel.h>
  11. #include "clk.h"
  12. struct rockchip_mmc_clock {
  13. struct clk_hw hw;
  14. void __iomem *reg;
  15. int shift;
  16. int cached_phase;
  17. struct notifier_block clk_rate_change_nb;
  18. };
  19. #define to_mmc_clock(_hw) container_of(_hw, struct rockchip_mmc_clock, hw)
  20. #define RK3288_MMC_CLKGEN_DIV 2
  21. static unsigned long rockchip_mmc_recalc(struct clk_hw *hw,
  22. unsigned long parent_rate)
  23. {
  24. return parent_rate / RK3288_MMC_CLKGEN_DIV;
  25. }
  26. #define ROCKCHIP_MMC_DELAY_SEL BIT(10)
  27. #define ROCKCHIP_MMC_DEGREE_MASK 0x3
  28. #define ROCKCHIP_MMC_DELAYNUM_OFFSET 2
  29. #define ROCKCHIP_MMC_DELAYNUM_MASK (0xff << ROCKCHIP_MMC_DELAYNUM_OFFSET)
  30. #define PSECS_PER_SEC 1000000000000LL
  31. /*
  32. * Each fine delay is between 44ps-77ps. Assume each fine delay is 60ps to
  33. * simplify calculations. So 45degs could be anywhere between 33deg and 57.8deg.
  34. */
  35. #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
  36. static int rockchip_mmc_get_phase(struct clk_hw *hw)
  37. {
  38. struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
  39. unsigned long rate = clk_hw_get_rate(hw);
  40. u32 raw_value;
  41. u16 degrees;
  42. u32 delay_num = 0;
  43. /* Constant signal, no measurable phase shift */
  44. if (!rate)
  45. return 0;
  46. raw_value = readl(mmc_clock->reg) >> (mmc_clock->shift);
  47. degrees = (raw_value & ROCKCHIP_MMC_DEGREE_MASK) * 90;
  48. if (raw_value & ROCKCHIP_MMC_DELAY_SEL) {
  49. /* degrees/delaynum * 1000000 */
  50. unsigned long factor = (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10) *
  51. 36 * (rate / 10000);
  52. delay_num = (raw_value & ROCKCHIP_MMC_DELAYNUM_MASK);
  53. delay_num >>= ROCKCHIP_MMC_DELAYNUM_OFFSET;
  54. degrees += DIV_ROUND_CLOSEST(delay_num * factor, 1000000);
  55. }
  56. return degrees % 360;
  57. }
  58. static int rockchip_mmc_set_phase(struct clk_hw *hw, int degrees)
  59. {
  60. struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
  61. unsigned long rate = clk_hw_get_rate(hw);
  62. u8 nineties, remainder;
  63. u8 delay_num;
  64. u32 raw_value;
  65. u32 delay;
  66. /*
  67. * The below calculation is based on the output clock from
  68. * MMC host to the card, which expects the phase clock inherits
  69. * the clock rate from its parent, namely the output clock
  70. * provider of MMC host. However, things may go wrong if
  71. * (1) It is orphan.
  72. * (2) It is assigned to the wrong parent.
  73. *
  74. * This check help debug the case (1), which seems to be the
  75. * most likely problem we often face and which makes it difficult
  76. * for people to debug unstable mmc tuning results.
  77. */
  78. if (!rate) {
  79. pr_err("%s: invalid clk rate\n", __func__);
  80. return -EINVAL;
  81. }
  82. nineties = degrees / 90;
  83. remainder = (degrees % 90);
  84. /*
  85. * Due to the inexact nature of the "fine" delay, we might
  86. * actually go non-monotonic. We don't go _too_ monotonic
  87. * though, so we should be OK. Here are options of how we may
  88. * work:
  89. *
  90. * Ideally we end up with:
  91. * 1.0, 2.0, ..., 69.0, 70.0, ..., 89.0, 90.0
  92. *
  93. * On one extreme (if delay is actually 44ps):
  94. * .73, 1.5, ..., 50.6, 51.3, ..., 65.3, 90.0
  95. * The other (if delay is actually 77ps):
  96. * 1.3, 2.6, ..., 88.6. 89.8, ..., 114.0, 90
  97. *
  98. * It's possible we might make a delay that is up to 25
  99. * degrees off from what we think we're making. That's OK
  100. * though because we should be REALLY far from any bad range.
  101. */
  102. /*
  103. * Convert to delay; do a little extra work to make sure we
  104. * don't overflow 32-bit / 64-bit numbers.
  105. */
  106. delay = 10000000; /* PSECS_PER_SEC / 10000 / 10 */
  107. delay *= remainder;
  108. delay = DIV_ROUND_CLOSEST(delay,
  109. (rate / 1000) * 36 *
  110. (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10));
  111. delay_num = (u8) min_t(u32, delay, 255);
  112. raw_value = delay_num ? ROCKCHIP_MMC_DELAY_SEL : 0;
  113. raw_value |= delay_num << ROCKCHIP_MMC_DELAYNUM_OFFSET;
  114. raw_value |= nineties;
  115. writel(HIWORD_UPDATE(raw_value, 0x07ff, mmc_clock->shift),
  116. mmc_clock->reg);
  117. pr_debug("%s->set_phase(%d) delay_nums=%u reg[0x%p]=0x%03x actual_degrees=%d\n",
  118. clk_hw_get_name(hw), degrees, delay_num,
  119. mmc_clock->reg, raw_value>>(mmc_clock->shift),
  120. rockchip_mmc_get_phase(hw)
  121. );
  122. return 0;
  123. }
  124. static const struct clk_ops rockchip_mmc_clk_ops = {
  125. .recalc_rate = rockchip_mmc_recalc,
  126. .get_phase = rockchip_mmc_get_phase,
  127. .set_phase = rockchip_mmc_set_phase,
  128. };
  129. #define to_rockchip_mmc_clock(x) \
  130. container_of(x, struct rockchip_mmc_clock, clk_rate_change_nb)
  131. static int rockchip_mmc_clk_rate_notify(struct notifier_block *nb,
  132. unsigned long event, void *data)
  133. {
  134. struct rockchip_mmc_clock *mmc_clock = to_rockchip_mmc_clock(nb);
  135. struct clk_notifier_data *ndata = data;
  136. /*
  137. * rockchip_mmc_clk is mostly used by mmc controllers to sample
  138. * the intput data, which expects the fixed phase after the tuning
  139. * process. However if the clock rate is changed, the phase is stale
  140. * and may break the data sampling. So here we try to restore the phase
  141. * for that case, except that
  142. * (1) cached_phase is invaild since we inevitably cached it when the
  143. * clock provider be reparented from orphan to its real parent in the
  144. * first place. Otherwise we may mess up the initialization of MMC cards
  145. * since we only set the default sample phase and drive phase later on.
  146. * (2) the new coming rate is higher than the older one since mmc driver
  147. * set the max-frequency to match the boards' ability but we can't go
  148. * over the heads of that, otherwise the tests smoke out the issue.
  149. */
  150. if (ndata->old_rate <= ndata->new_rate)
  151. return NOTIFY_DONE;
  152. if (event == PRE_RATE_CHANGE)
  153. mmc_clock->cached_phase =
  154. rockchip_mmc_get_phase(&mmc_clock->hw);
  155. else if (mmc_clock->cached_phase != -EINVAL &&
  156. event == POST_RATE_CHANGE)
  157. rockchip_mmc_set_phase(&mmc_clock->hw, mmc_clock->cached_phase);
  158. return NOTIFY_DONE;
  159. }
  160. struct clk *rockchip_clk_register_mmc(const char *name,
  161. const char *const *parent_names, u8 num_parents,
  162. void __iomem *reg, int shift)
  163. {
  164. struct clk_init_data init;
  165. struct rockchip_mmc_clock *mmc_clock;
  166. struct clk *clk;
  167. int ret;
  168. mmc_clock = kmalloc(sizeof(*mmc_clock), GFP_KERNEL);
  169. if (!mmc_clock)
  170. return ERR_PTR(-ENOMEM);
  171. init.name = name;
  172. init.flags = 0;
  173. init.num_parents = num_parents;
  174. init.parent_names = parent_names;
  175. init.ops = &rockchip_mmc_clk_ops;
  176. mmc_clock->hw.init = &init;
  177. mmc_clock->reg = reg;
  178. mmc_clock->shift = shift;
  179. clk = clk_register(NULL, &mmc_clock->hw);
  180. if (IS_ERR(clk)) {
  181. ret = PTR_ERR(clk);
  182. goto err_register;
  183. }
  184. mmc_clock->clk_rate_change_nb.notifier_call =
  185. &rockchip_mmc_clk_rate_notify;
  186. ret = clk_notifier_register(clk, &mmc_clock->clk_rate_change_nb);
  187. if (ret)
  188. goto err_notifier;
  189. return clk;
  190. err_notifier:
  191. clk_unregister(clk);
  192. err_register:
  193. kfree(mmc_clock);
  194. return ERR_PTR(ret);
  195. }