clk-mmc-phase.c 6.6 KB

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