clk-mmc-phase.c 7.0 KB

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