clk-mmc-phase.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. };
  27. #define to_mmc_clock(_hw) container_of(_hw, struct rockchip_mmc_clock, hw)
  28. #define RK3288_MMC_CLKGEN_DIV 2
  29. static unsigned long rockchip_mmc_recalc(struct clk_hw *hw,
  30. unsigned long parent_rate)
  31. {
  32. return parent_rate / RK3288_MMC_CLKGEN_DIV;
  33. }
  34. #define ROCKCHIP_MMC_DELAY_SEL BIT(10)
  35. #define ROCKCHIP_MMC_DEGREE_MASK 0x3
  36. #define ROCKCHIP_MMC_DELAYNUM_OFFSET 2
  37. #define ROCKCHIP_MMC_DELAYNUM_MASK (0xff << ROCKCHIP_MMC_DELAYNUM_OFFSET)
  38. #define PSECS_PER_SEC 1000000000000LL
  39. /*
  40. * Each fine delay is between 44ps-77ps. Assume each fine delay is 60ps to
  41. * simplify calculations. So 45degs could be anywhere between 33deg and 57.8deg.
  42. */
  43. #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
  44. static int rockchip_mmc_get_phase(struct clk_hw *hw)
  45. {
  46. struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
  47. unsigned long rate = clk_get_rate(hw->clk);
  48. u32 raw_value;
  49. u16 degrees;
  50. u32 delay_num = 0;
  51. /* See the comment for rockchip_mmc_set_phase below */
  52. if (!rate) {
  53. pr_err("%s: invalid clk rate\n", __func__);
  54. return -EINVAL;
  55. }
  56. raw_value = readl(mmc_clock->reg) >> (mmc_clock->shift);
  57. degrees = (raw_value & ROCKCHIP_MMC_DEGREE_MASK) * 90;
  58. if (raw_value & ROCKCHIP_MMC_DELAY_SEL) {
  59. /* degrees/delaynum * 10000 */
  60. unsigned long factor = (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10) *
  61. 36 * (rate / 1000000);
  62. delay_num = (raw_value & ROCKCHIP_MMC_DELAYNUM_MASK);
  63. delay_num >>= ROCKCHIP_MMC_DELAYNUM_OFFSET;
  64. degrees += DIV_ROUND_CLOSEST(delay_num * factor, 10000);
  65. }
  66. return degrees % 360;
  67. }
  68. static int rockchip_mmc_set_phase(struct clk_hw *hw, int degrees)
  69. {
  70. struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
  71. unsigned long rate = clk_get_rate(hw->clk);
  72. u8 nineties, remainder;
  73. u8 delay_num;
  74. u32 raw_value;
  75. u32 delay;
  76. /*
  77. * The below calculation is based on the output clock from
  78. * MMC host to the card, which expects the phase clock inherits
  79. * the clock rate from its parent, namely the output clock
  80. * provider of MMC host. However, things may go wrong if
  81. * (1) It is orphan.
  82. * (2) It is assigned to the wrong parent.
  83. *
  84. * This check help debug the case (1), which seems to be the
  85. * most likely problem we often face and which makes it difficult
  86. * for people to debug unstable mmc tuning results.
  87. */
  88. if (!rate) {
  89. pr_err("%s: invalid clk rate\n", __func__);
  90. return -EINVAL;
  91. }
  92. nineties = degrees / 90;
  93. remainder = (degrees % 90);
  94. /*
  95. * Due to the inexact nature of the "fine" delay, we might
  96. * actually go non-monotonic. We don't go _too_ monotonic
  97. * though, so we should be OK. Here are options of how we may
  98. * work:
  99. *
  100. * Ideally we end up with:
  101. * 1.0, 2.0, ..., 69.0, 70.0, ..., 89.0, 90.0
  102. *
  103. * On one extreme (if delay is actually 44ps):
  104. * .73, 1.5, ..., 50.6, 51.3, ..., 65.3, 90.0
  105. * The other (if delay is actually 77ps):
  106. * 1.3, 2.6, ..., 88.6. 89.8, ..., 114.0, 90
  107. *
  108. * It's possible we might make a delay that is up to 25
  109. * degrees off from what we think we're making. That's OK
  110. * though because we should be REALLY far from any bad range.
  111. */
  112. /*
  113. * Convert to delay; do a little extra work to make sure we
  114. * don't overflow 32-bit / 64-bit numbers.
  115. */
  116. delay = 10000000; /* PSECS_PER_SEC / 10000 / 10 */
  117. delay *= remainder;
  118. delay = DIV_ROUND_CLOSEST(delay,
  119. (rate / 1000) * 36 *
  120. (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10));
  121. delay_num = (u8) min_t(u32, delay, 255);
  122. raw_value = delay_num ? ROCKCHIP_MMC_DELAY_SEL : 0;
  123. raw_value |= delay_num << ROCKCHIP_MMC_DELAYNUM_OFFSET;
  124. raw_value |= nineties;
  125. writel(HIWORD_UPDATE(raw_value, 0x07ff, mmc_clock->shift),
  126. 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. struct clk *rockchip_clk_register_mmc(const char *name,
  140. const char *const *parent_names, u8 num_parents,
  141. void __iomem *reg, int shift)
  142. {
  143. struct clk_init_data init;
  144. struct rockchip_mmc_clock *mmc_clock;
  145. struct clk *clk;
  146. mmc_clock = kmalloc(sizeof(*mmc_clock), GFP_KERNEL);
  147. if (!mmc_clock)
  148. return ERR_PTR(-ENOMEM);
  149. init.name = name;
  150. init.flags = 0;
  151. init.num_parents = num_parents;
  152. init.parent_names = parent_names;
  153. init.ops = &rockchip_mmc_clk_ops;
  154. mmc_clock->hw.init = &init;
  155. mmc_clock->reg = reg;
  156. mmc_clock->shift = shift;
  157. clk = clk_register(NULL, &mmc_clock->hw);
  158. if (IS_ERR(clk))
  159. kfree(mmc_clock);
  160. return clk;
  161. }