clk-mmc-phase.c 7.2 KB

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