clk-ti-sci.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Texas Instruments System Control Interface (TI SCI) clock driver
  4. *
  5. * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
  6. * Andreas Dannenberg <dannenberg@ti.com>
  7. *
  8. * Loosely based on Linux kernel sci-clk.c...
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <clk-uclass.h>
  14. #include <linux/soc/ti/ti_sci_protocol.h>
  15. /**
  16. * struct ti_sci_clk_data - clock controller information structure
  17. * @sci: TI SCI handle used for communication with system controller
  18. */
  19. struct ti_sci_clk_data {
  20. const struct ti_sci_handle *sci;
  21. };
  22. static int ti_sci_clk_probe(struct udevice *dev)
  23. {
  24. struct ti_sci_clk_data *data = dev_get_priv(dev);
  25. debug("%s(dev=%p)\n", __func__, dev);
  26. if (!data)
  27. return -ENOMEM;
  28. /* Store handle for communication with the system controller */
  29. data->sci = ti_sci_get_handle(dev);
  30. if (IS_ERR(data->sci))
  31. return PTR_ERR(data->sci);
  32. return 0;
  33. }
  34. static int ti_sci_clk_of_xlate(struct clk *clk,
  35. struct ofnode_phandle_args *args)
  36. {
  37. debug("%s(clk=%p, args_count=%d)\n", __func__, clk, args->args_count);
  38. if (args->args_count != 2) {
  39. debug("Invalid args_count: %d\n", args->args_count);
  40. return -EINVAL;
  41. }
  42. /*
  43. * On TI SCI-based devices, the clock provider id field is used as a
  44. * device ID, and the data field is used as the associated sub-ID.
  45. */
  46. clk->id = args->args[0];
  47. clk->data = args->args[1];
  48. return 0;
  49. }
  50. static int ti_sci_clk_request(struct clk *clk)
  51. {
  52. debug("%s(clk=%p)\n", __func__, clk);
  53. return 0;
  54. }
  55. static int ti_sci_clk_free(struct clk *clk)
  56. {
  57. debug("%s(clk=%p)\n", __func__, clk);
  58. return 0;
  59. }
  60. static ulong ti_sci_clk_get_rate(struct clk *clk)
  61. {
  62. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  63. const struct ti_sci_handle *sci = data->sci;
  64. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  65. u64 current_freq;
  66. int ret;
  67. debug("%s(clk=%p)\n", __func__, clk);
  68. ret = cops->get_freq(sci, clk->id, clk->data, &current_freq);
  69. if (ret) {
  70. dev_err(clk->dev, "%s: get_freq failed (%d)\n", __func__, ret);
  71. return ret;
  72. }
  73. debug("%s(current_freq=%llu)\n", __func__, current_freq);
  74. return current_freq;
  75. }
  76. static ulong ti_sci_clk_set_rate(struct clk *clk, ulong rate)
  77. {
  78. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  79. const struct ti_sci_handle *sci = data->sci;
  80. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  81. int ret;
  82. debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
  83. /* Ask for exact frequency by using same value for min/target/max */
  84. ret = cops->set_freq(sci, clk->id, clk->data, rate, rate, rate);
  85. if (ret)
  86. dev_err(clk->dev, "%s: set_freq failed (%d)\n", __func__, ret);
  87. return ret;
  88. }
  89. static int ti_sci_clk_set_parent(struct clk *clk, struct clk *parent)
  90. {
  91. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  92. const struct ti_sci_handle *sci = data->sci;
  93. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  94. u8 num_parents;
  95. u8 parent_cid;
  96. int ret;
  97. debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
  98. /* Make sure the clock parent is valid for a given device ID */
  99. if (clk->id != parent->id)
  100. return -EINVAL;
  101. /* Make sure clock has parents that can be set */
  102. ret = cops->get_num_parents(sci, clk->id, clk->data, &num_parents);
  103. if (ret) {
  104. dev_err(clk->dev, "%s: get_num_parents failed (%d)\n",
  105. __func__, ret);
  106. return ret;
  107. }
  108. if (num_parents < 2) {
  109. dev_err(clk->dev, "%s: clock has no settable parents!\n",
  110. __func__);
  111. return -EINVAL;
  112. }
  113. /* Make sure parent clock ID is valid */
  114. parent_cid = parent->data - clk->data - 1;
  115. if (parent_cid >= num_parents) {
  116. dev_err(clk->dev, "%s: invalid parent clock!\n", __func__);
  117. return -EINVAL;
  118. }
  119. /* Ready to proceed to configure the new clock parent */
  120. ret = cops->set_parent(sci, clk->id, clk->data, parent->data);
  121. if (ret)
  122. dev_err(clk->dev, "%s: set_parent failed (%d)\n", __func__,
  123. ret);
  124. return ret;
  125. }
  126. static int ti_sci_clk_enable(struct clk *clk)
  127. {
  128. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  129. const struct ti_sci_handle *sci = data->sci;
  130. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  131. int ret;
  132. debug("%s(clk=%p)\n", __func__, clk);
  133. /*
  134. * Allow the System Controller to automatically manage the state of
  135. * this clock. If the device is enabled, then the clock is enabled.
  136. */
  137. ret = cops->put_clock(sci, clk->id, clk->data);
  138. if (ret)
  139. dev_err(clk->dev, "%s: put_clock failed (%d)\n", __func__, ret);
  140. return ret;
  141. }
  142. static int ti_sci_clk_disable(struct clk *clk)
  143. {
  144. struct ti_sci_clk_data *data = dev_get_priv(clk->dev);
  145. const struct ti_sci_handle *sci = data->sci;
  146. const struct ti_sci_clk_ops *cops = &sci->ops.clk_ops;
  147. int ret;
  148. debug("%s(clk=%p)\n", __func__, clk);
  149. /* Unconditionally disable clock, regardless of state of the device */
  150. ret = cops->idle_clock(sci, clk->id, clk->data);
  151. if (ret)
  152. dev_err(clk->dev, "%s: idle_clock failed (%d)\n", __func__,
  153. ret);
  154. return ret;
  155. }
  156. static const struct udevice_id ti_sci_clk_of_match[] = {
  157. { .compatible = "ti,k2g-sci-clk" },
  158. { /* sentinel */ },
  159. };
  160. static struct clk_ops ti_sci_clk_ops = {
  161. .of_xlate = ti_sci_clk_of_xlate,
  162. .request = ti_sci_clk_request,
  163. .free = ti_sci_clk_free,
  164. .get_rate = ti_sci_clk_get_rate,
  165. .set_rate = ti_sci_clk_set_rate,
  166. .set_parent = ti_sci_clk_set_parent,
  167. .enable = ti_sci_clk_enable,
  168. .disable = ti_sci_clk_disable,
  169. };
  170. U_BOOT_DRIVER(ti_sci_clk) = {
  171. .name = "ti-sci-clk",
  172. .id = UCLASS_CLK,
  173. .of_match = ti_sci_clk_of_match,
  174. .probe = ti_sci_clk_probe,
  175. .priv_auto_alloc_size = sizeof(struct ti_sci_clk_data),
  176. .ops = &ti_sci_clk_ops,
  177. };