clk-mod0.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2013 Emilio López
  3. *
  4. * Emilio López <emilio@elopez.com.ar>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk-provider.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/of_address.h>
  19. #include "clk-factors.h"
  20. /**
  21. * sun4i_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
  22. * MOD0 rate is calculated as follows
  23. * rate = (parent_rate >> p) / (m + 1);
  24. */
  25. static void sun4i_a10_get_mod0_factors(u32 *freq, u32 parent_rate,
  26. u8 *n, u8 *k, u8 *m, u8 *p)
  27. {
  28. u8 div, calcm, calcp;
  29. /* These clocks can only divide, so we will never be able to achieve
  30. * frequencies higher than the parent frequency */
  31. if (*freq > parent_rate)
  32. *freq = parent_rate;
  33. div = DIV_ROUND_UP(parent_rate, *freq);
  34. if (div < 16)
  35. calcp = 0;
  36. else if (div / 2 < 16)
  37. calcp = 1;
  38. else if (div / 4 < 16)
  39. calcp = 2;
  40. else
  41. calcp = 3;
  42. calcm = DIV_ROUND_UP(div, 1 << calcp);
  43. *freq = (parent_rate >> calcp) / calcm;
  44. /* we were called to round the frequency, we can now return */
  45. if (n == NULL)
  46. return;
  47. *m = calcm - 1;
  48. *p = calcp;
  49. }
  50. /* user manual says "n" but it's really "p" */
  51. static struct clk_factors_config sun4i_a10_mod0_config = {
  52. .mshift = 0,
  53. .mwidth = 4,
  54. .pshift = 16,
  55. .pwidth = 2,
  56. };
  57. static const struct factors_data sun4i_a10_mod0_data __initconst = {
  58. .enable = 31,
  59. .mux = 24,
  60. .muxmask = BIT(1) | BIT(0),
  61. .table = &sun4i_a10_mod0_config,
  62. .getter = sun4i_a10_get_mod0_factors,
  63. };
  64. static DEFINE_SPINLOCK(sun4i_a10_mod0_lock);
  65. static void __init sun4i_a10_mod0_setup(struct device_node *node)
  66. {
  67. sunxi_factors_register(node, &sun4i_a10_mod0_data, &sun4i_a10_mod0_lock);
  68. }
  69. CLK_OF_DECLARE(sun4i_a10_mod0, "allwinner,sun4i-a10-mod0-clk", sun4i_a10_mod0_setup);
  70. static DEFINE_SPINLOCK(sun5i_a13_mbus_lock);
  71. static void __init sun5i_a13_mbus_setup(struct device_node *node)
  72. {
  73. struct clk *mbus = sunxi_factors_register(node, &sun4i_a10_mod0_data, &sun5i_a13_mbus_lock);
  74. /* The MBUS clocks needs to be always enabled */
  75. __clk_get(mbus);
  76. clk_prepare_enable(mbus);
  77. }
  78. CLK_OF_DECLARE(sun5i_a13_mbus, "allwinner,sun5i-a13-mbus-clk", sun5i_a13_mbus_setup);
  79. struct mmc_phase_data {
  80. u8 offset;
  81. };
  82. struct mmc_phase {
  83. struct clk_hw hw;
  84. void __iomem *reg;
  85. struct mmc_phase_data *data;
  86. spinlock_t *lock;
  87. };
  88. #define to_mmc_phase(_hw) container_of(_hw, struct mmc_phase, hw)
  89. static int mmc_get_phase(struct clk_hw *hw)
  90. {
  91. struct clk *mmc, *mmc_parent, *clk = hw->clk;
  92. struct mmc_phase *phase = to_mmc_phase(hw);
  93. unsigned int mmc_rate, mmc_parent_rate;
  94. u16 step, mmc_div;
  95. u32 value;
  96. u8 delay;
  97. value = readl(phase->reg);
  98. delay = (value >> phase->data->offset) & 0x3;
  99. if (!delay)
  100. return 180;
  101. /* Get the main MMC clock */
  102. mmc = clk_get_parent(clk);
  103. if (!mmc)
  104. return -EINVAL;
  105. /* And its rate */
  106. mmc_rate = clk_get_rate(mmc);
  107. if (!mmc_rate)
  108. return -EINVAL;
  109. /* Now, get the MMC parent (most likely some PLL) */
  110. mmc_parent = clk_get_parent(mmc);
  111. if (!mmc_parent)
  112. return -EINVAL;
  113. /* And its rate */
  114. mmc_parent_rate = clk_get_rate(mmc_parent);
  115. if (!mmc_parent_rate)
  116. return -EINVAL;
  117. /* Get MMC clock divider */
  118. mmc_div = mmc_parent_rate / mmc_rate;
  119. step = DIV_ROUND_CLOSEST(360, mmc_div);
  120. return delay * step;
  121. }
  122. static int mmc_set_phase(struct clk_hw *hw, int degrees)
  123. {
  124. struct clk *mmc, *mmc_parent, *clk = hw->clk;
  125. struct mmc_phase *phase = to_mmc_phase(hw);
  126. unsigned int mmc_rate, mmc_parent_rate;
  127. unsigned long flags;
  128. u32 value;
  129. u8 delay;
  130. /* Get the main MMC clock */
  131. mmc = clk_get_parent(clk);
  132. if (!mmc)
  133. return -EINVAL;
  134. /* And its rate */
  135. mmc_rate = clk_get_rate(mmc);
  136. if (!mmc_rate)
  137. return -EINVAL;
  138. /* Now, get the MMC parent (most likely some PLL) */
  139. mmc_parent = clk_get_parent(mmc);
  140. if (!mmc_parent)
  141. return -EINVAL;
  142. /* And its rate */
  143. mmc_parent_rate = clk_get_rate(mmc_parent);
  144. if (!mmc_parent_rate)
  145. return -EINVAL;
  146. if (degrees != 180) {
  147. u16 step, mmc_div;
  148. /* Get MMC clock divider */
  149. mmc_div = mmc_parent_rate / mmc_rate;
  150. /*
  151. * We can only outphase the clocks by multiple of the
  152. * PLL's period.
  153. *
  154. * Since the MMC clock in only a divider, and the
  155. * formula to get the outphasing in degrees is deg =
  156. * 360 * delta / period
  157. *
  158. * If we simplify this formula, we can see that the
  159. * only thing that we're concerned about is the number
  160. * of period we want to outphase our clock from, and
  161. * the divider set by the MMC clock.
  162. */
  163. step = DIV_ROUND_CLOSEST(360, mmc_div);
  164. delay = DIV_ROUND_CLOSEST(degrees, step);
  165. } else {
  166. delay = 0;
  167. }
  168. spin_lock_irqsave(phase->lock, flags);
  169. value = readl(phase->reg);
  170. value &= ~GENMASK(phase->data->offset + 3, phase->data->offset);
  171. value |= delay << phase->data->offset;
  172. writel(value, phase->reg);
  173. spin_unlock_irqrestore(phase->lock, flags);
  174. return 0;
  175. }
  176. static const struct clk_ops mmc_clk_ops = {
  177. .get_phase = mmc_get_phase,
  178. .set_phase = mmc_set_phase,
  179. };
  180. static void __init sun4i_a10_mmc_phase_setup(struct device_node *node,
  181. struct mmc_phase_data *data)
  182. {
  183. const char *parent_names[1] = { of_clk_get_parent_name(node, 0) };
  184. struct clk_init_data init = {
  185. .num_parents = 1,
  186. .parent_names = parent_names,
  187. .ops = &mmc_clk_ops,
  188. };
  189. struct mmc_phase *phase;
  190. struct clk *clk;
  191. phase = kmalloc(sizeof(*phase), GFP_KERNEL);
  192. if (!phase)
  193. return;
  194. phase->hw.init = &init;
  195. phase->reg = of_iomap(node, 0);
  196. if (!phase->reg)
  197. goto err_free;
  198. phase->data = data;
  199. phase->lock = &sun4i_a10_mod0_lock;
  200. if (of_property_read_string(node, "clock-output-names", &init.name))
  201. init.name = node->name;
  202. clk = clk_register(NULL, &phase->hw);
  203. if (IS_ERR(clk))
  204. goto err_unmap;
  205. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  206. return;
  207. err_unmap:
  208. iounmap(phase->reg);
  209. err_free:
  210. kfree(phase);
  211. }
  212. static struct mmc_phase_data mmc_output_clk = {
  213. .offset = 8,
  214. };
  215. static struct mmc_phase_data mmc_sample_clk = {
  216. .offset = 20,
  217. };
  218. static void __init sun4i_a10_mmc_output_setup(struct device_node *node)
  219. {
  220. sun4i_a10_mmc_phase_setup(node, &mmc_output_clk);
  221. }
  222. CLK_OF_DECLARE(sun4i_a10_mmc_output, "allwinner,sun4i-a10-mmc-output-clk", sun4i_a10_mmc_output_setup);
  223. static void __init sun4i_a10_mmc_sample_setup(struct device_node *node)
  224. {
  225. sun4i_a10_mmc_phase_setup(node, &mmc_sample_clk);
  226. }
  227. CLK_OF_DECLARE(sun4i_a10_mmc_sample, "allwinner,sun4i-a10-mmc-sample-clk", sun4i_a10_mmc_sample_setup);