bypass.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
  4. */
  5. #define LOG_CATEGORY UCLASS_CLK
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <clk-uclass.h>
  9. #include <dm.h>
  10. #include <log.h>
  11. #include <kendryte/bypass.h>
  12. #include <linux/clk-provider.h>
  13. #include <linux/err.h>
  14. #define CLK_K210_BYPASS "k210_clk_bypass"
  15. /*
  16. * This is a small driver to do a software bypass of a clock if hardware bypass
  17. * is not working. I have tried to write this in a generic fashion, so that it
  18. * could be potentially broken out of the kendryte code at some future date.
  19. *
  20. * Say you have the following clock configuration
  21. *
  22. * +---+ +---+
  23. * |osc| |pll|
  24. * +---+ +---+
  25. * ^
  26. * /|
  27. * / |
  28. * / |
  29. * / |
  30. * / |
  31. * +---+ +---+
  32. * |clk| |clk|
  33. * +---+ +---+
  34. *
  35. * But the pll does not have a bypass, so when you configure the pll, the
  36. * configuration needs to change to look like
  37. *
  38. * +---+ +---+
  39. * |osc| |pll|
  40. * +---+ +---+
  41. * ^
  42. * |\
  43. * | \
  44. * | \
  45. * | \
  46. * | \
  47. * +---+ +---+
  48. * |clk| |clk|
  49. * +---+ +---+
  50. *
  51. * To set this up, create a bypass clock with bypassee=pll and alt=osc. When
  52. * creating the child clocks, set their parent to the bypass clock. After
  53. * creating all the children, call k210_bypass_setchildren().
  54. */
  55. static int k210_bypass_dobypass(struct k210_bypass *bypass)
  56. {
  57. int ret, i;
  58. /*
  59. * If we already have saved parents, then the children are already
  60. * bypassed
  61. */
  62. if (bypass->child_count && bypass->saved_parents[0])
  63. return 0;
  64. for (i = 0; i < bypass->child_count; i++) {
  65. struct clk *child = bypass->children[i];
  66. struct clk *parent = clk_get_parent(child);
  67. if (IS_ERR(parent)) {
  68. for (; i; i--)
  69. bypass->saved_parents[i] = NULL;
  70. return PTR_ERR(parent);
  71. }
  72. bypass->saved_parents[i] = parent;
  73. }
  74. for (i = 0; i < bypass->child_count; i++) {
  75. struct clk *child = bypass->children[i];
  76. ret = clk_set_parent(child, bypass->alt);
  77. if (ret) {
  78. for (; i; i--)
  79. clk_set_parent(bypass->children[i],
  80. bypass->saved_parents[i]);
  81. for (i = 0; i < bypass->child_count; i++)
  82. bypass->saved_parents[i] = NULL;
  83. return ret;
  84. }
  85. }
  86. return 0;
  87. }
  88. static int k210_bypass_unbypass(struct k210_bypass *bypass)
  89. {
  90. int err, ret, i;
  91. if (!bypass->child_count && !bypass->saved_parents[0]) {
  92. log_warning("Cannot unbypass children; dobypass not called first\n");
  93. return 0;
  94. }
  95. ret = 0;
  96. for (i = 0; i < bypass->child_count; i++) {
  97. err = clk_set_parent(bypass->children[i],
  98. bypass->saved_parents[i]);
  99. if (err)
  100. ret = err;
  101. bypass->saved_parents[i] = NULL;
  102. }
  103. return ret;
  104. }
  105. static ulong k210_bypass_get_rate(struct clk *clk)
  106. {
  107. struct k210_bypass *bypass = to_k210_bypass(clk);
  108. const struct clk_ops *ops = bypass->bypassee_ops;
  109. if (ops->get_rate)
  110. return ops->get_rate(bypass->bypassee);
  111. else
  112. return clk_get_parent_rate(bypass->bypassee);
  113. }
  114. static ulong k210_bypass_set_rate(struct clk *clk, unsigned long rate)
  115. {
  116. int ret;
  117. struct k210_bypass *bypass = to_k210_bypass(clk);
  118. const struct clk_ops *ops = bypass->bypassee_ops;
  119. /* Don't bother bypassing if we aren't going to set the rate */
  120. if (!ops->set_rate)
  121. return k210_bypass_get_rate(clk);
  122. ret = k210_bypass_dobypass(bypass);
  123. if (ret)
  124. return ret;
  125. ret = ops->set_rate(bypass->bypassee, rate);
  126. if (ret < 0)
  127. return ret;
  128. return k210_bypass_unbypass(bypass);
  129. }
  130. static int k210_bypass_set_parent(struct clk *clk, struct clk *parent)
  131. {
  132. struct k210_bypass *bypass = to_k210_bypass(clk);
  133. const struct clk_ops *ops = bypass->bypassee_ops;
  134. if (ops->set_parent)
  135. return ops->set_parent(bypass->bypassee, parent);
  136. else
  137. return -ENOTSUPP;
  138. }
  139. /*
  140. * For these next two functions, do the bypassing even if there is no
  141. * en-/-disable function, since the bypassing itself can be observed in between
  142. * calls.
  143. */
  144. static int k210_bypass_enable(struct clk *clk)
  145. {
  146. int ret;
  147. struct k210_bypass *bypass = to_k210_bypass(clk);
  148. const struct clk_ops *ops = bypass->bypassee_ops;
  149. ret = k210_bypass_dobypass(bypass);
  150. if (ret)
  151. return ret;
  152. if (ops->enable)
  153. ret = ops->enable(bypass->bypassee);
  154. else
  155. ret = 0;
  156. if (ret)
  157. return ret;
  158. return k210_bypass_unbypass(bypass);
  159. }
  160. static int k210_bypass_disable(struct clk *clk)
  161. {
  162. int ret;
  163. struct k210_bypass *bypass = to_k210_bypass(clk);
  164. const struct clk_ops *ops = bypass->bypassee_ops;
  165. ret = k210_bypass_dobypass(bypass);
  166. if (ret)
  167. return ret;
  168. if (ops->disable)
  169. return ops->disable(bypass->bypassee);
  170. else
  171. return 0;
  172. }
  173. static const struct clk_ops k210_bypass_ops = {
  174. .get_rate = k210_bypass_get_rate,
  175. .set_rate = k210_bypass_set_rate,
  176. .set_parent = k210_bypass_set_parent,
  177. .enable = k210_bypass_enable,
  178. .disable = k210_bypass_disable,
  179. };
  180. int k210_bypass_set_children(struct clk *clk, struct clk **children,
  181. size_t child_count)
  182. {
  183. struct k210_bypass *bypass = to_k210_bypass(clk);
  184. kfree(bypass->saved_parents);
  185. if (child_count) {
  186. bypass->saved_parents =
  187. kcalloc(child_count, sizeof(struct clk *), GFP_KERNEL);
  188. if (!bypass->saved_parents)
  189. return -ENOMEM;
  190. }
  191. bypass->child_count = child_count;
  192. bypass->children = children;
  193. return 0;
  194. }
  195. struct clk *k210_register_bypass_struct(const char *name,
  196. const char *parent_name,
  197. struct k210_bypass *bypass)
  198. {
  199. int ret;
  200. struct clk *clk;
  201. clk = &bypass->clk;
  202. ret = clk_register(clk, CLK_K210_BYPASS, name, parent_name);
  203. if (ret)
  204. return ERR_PTR(ret);
  205. bypass->bypassee->dev = clk->dev;
  206. return clk;
  207. }
  208. struct clk *k210_register_bypass(const char *name, const char *parent_name,
  209. struct clk *bypassee,
  210. const struct clk_ops *bypassee_ops,
  211. struct clk *alt)
  212. {
  213. struct clk *clk;
  214. struct k210_bypass *bypass;
  215. bypass = kzalloc(sizeof(*bypass), GFP_KERNEL);
  216. if (!bypass)
  217. return ERR_PTR(-ENOMEM);
  218. bypass->bypassee = bypassee;
  219. bypass->bypassee_ops = bypassee_ops;
  220. bypass->alt = alt;
  221. clk = k210_register_bypass_struct(name, parent_name, bypass);
  222. if (IS_ERR(clk))
  223. kfree(bypass);
  224. return clk;
  225. }
  226. U_BOOT_DRIVER(k210_bypass) = {
  227. .name = CLK_K210_BYPASS,
  228. .id = UCLASS_CLK,
  229. .ops = &k210_bypass_ops,
  230. };