clk-hi3660-stub.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Hisilicon clock driver
  4. *
  5. * Copyright (c) 2013-2017 Hisilicon Limited.
  6. * Copyright (c) 2017 Linaro Limited.
  7. *
  8. * Author: Kai Zhao <[email protected]>
  9. * Tao Wang <[email protected]>
  10. * Leo Yan <[email protected]>
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/mailbox_client.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/platform_device.h>
  21. #include <dt-bindings/clock/hi3660-clock.h>
  22. #define HI3660_STUB_CLOCK_DATA (0x70)
  23. #define MHZ (1000 * 1000)
  24. #define DEFINE_CLK_STUB(_id, _cmd, _name) \
  25. { \
  26. .id = (_id), \
  27. .cmd = (_cmd), \
  28. .hw.init = &(struct clk_init_data) { \
  29. .name = #_name, \
  30. .ops = &hi3660_stub_clk_ops, \
  31. .num_parents = 0, \
  32. .flags = CLK_GET_RATE_NOCACHE, \
  33. }, \
  34. },
  35. #define to_stub_clk(_hw) container_of(_hw, struct hi3660_stub_clk, hw)
  36. struct hi3660_stub_clk_chan {
  37. struct mbox_client cl;
  38. struct mbox_chan *mbox;
  39. };
  40. struct hi3660_stub_clk {
  41. unsigned int id;
  42. struct clk_hw hw;
  43. unsigned int cmd;
  44. unsigned int msg[8];
  45. unsigned int rate;
  46. };
  47. static void __iomem *freq_reg;
  48. static struct hi3660_stub_clk_chan stub_clk_chan;
  49. static unsigned long hi3660_stub_clk_recalc_rate(struct clk_hw *hw,
  50. unsigned long parent_rate)
  51. {
  52. struct hi3660_stub_clk *stub_clk = to_stub_clk(hw);
  53. /*
  54. * LPM3 writes back the CPU frequency in shared SRAM so read
  55. * back the frequency.
  56. */
  57. stub_clk->rate = readl(freq_reg + (stub_clk->id << 2)) * MHZ;
  58. return stub_clk->rate;
  59. }
  60. static long hi3660_stub_clk_round_rate(struct clk_hw *hw, unsigned long rate,
  61. unsigned long *prate)
  62. {
  63. /*
  64. * LPM3 handles rate rounding so just return whatever
  65. * rate is requested.
  66. */
  67. return rate;
  68. }
  69. static int hi3660_stub_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  70. unsigned long parent_rate)
  71. {
  72. struct hi3660_stub_clk *stub_clk = to_stub_clk(hw);
  73. stub_clk->msg[0] = stub_clk->cmd;
  74. stub_clk->msg[1] = rate / MHZ;
  75. dev_dbg(stub_clk_chan.cl.dev, "set rate msg[0]=0x%x msg[1]=0x%x\n",
  76. stub_clk->msg[0], stub_clk->msg[1]);
  77. mbox_send_message(stub_clk_chan.mbox, stub_clk->msg);
  78. mbox_client_txdone(stub_clk_chan.mbox, 0);
  79. stub_clk->rate = rate;
  80. return 0;
  81. }
  82. static const struct clk_ops hi3660_stub_clk_ops = {
  83. .recalc_rate = hi3660_stub_clk_recalc_rate,
  84. .round_rate = hi3660_stub_clk_round_rate,
  85. .set_rate = hi3660_stub_clk_set_rate,
  86. };
  87. static struct hi3660_stub_clk hi3660_stub_clks[HI3660_CLK_STUB_NUM] = {
  88. DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER0, 0x0001030A, "cpu-cluster.0")
  89. DEFINE_CLK_STUB(HI3660_CLK_STUB_CLUSTER1, 0x0002030A, "cpu-cluster.1")
  90. DEFINE_CLK_STUB(HI3660_CLK_STUB_GPU, 0x0003030A, "clk-g3d")
  91. DEFINE_CLK_STUB(HI3660_CLK_STUB_DDR, 0x00040309, "clk-ddrc")
  92. };
  93. static struct clk_hw *hi3660_stub_clk_hw_get(struct of_phandle_args *clkspec,
  94. void *data)
  95. {
  96. unsigned int idx = clkspec->args[0];
  97. if (idx >= HI3660_CLK_STUB_NUM) {
  98. pr_err("%s: invalid index %u\n", __func__, idx);
  99. return ERR_PTR(-EINVAL);
  100. }
  101. return &hi3660_stub_clks[idx].hw;
  102. }
  103. static int hi3660_stub_clk_probe(struct platform_device *pdev)
  104. {
  105. struct device *dev = &pdev->dev;
  106. struct resource *res;
  107. unsigned int i;
  108. int ret;
  109. /* Use mailbox client without blocking */
  110. stub_clk_chan.cl.dev = dev;
  111. stub_clk_chan.cl.tx_done = NULL;
  112. stub_clk_chan.cl.tx_block = false;
  113. stub_clk_chan.cl.knows_txdone = false;
  114. /* Allocate mailbox channel */
  115. stub_clk_chan.mbox = mbox_request_channel(&stub_clk_chan.cl, 0);
  116. if (IS_ERR(stub_clk_chan.mbox))
  117. return PTR_ERR(stub_clk_chan.mbox);
  118. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  119. if (!res)
  120. return -EINVAL;
  121. freq_reg = devm_ioremap(dev, res->start, resource_size(res));
  122. if (!freq_reg)
  123. return -ENOMEM;
  124. freq_reg += HI3660_STUB_CLOCK_DATA;
  125. for (i = 0; i < HI3660_CLK_STUB_NUM; i++) {
  126. ret = devm_clk_hw_register(&pdev->dev, &hi3660_stub_clks[i].hw);
  127. if (ret)
  128. return ret;
  129. }
  130. return devm_of_clk_add_hw_provider(&pdev->dev, hi3660_stub_clk_hw_get,
  131. hi3660_stub_clks);
  132. }
  133. static const struct of_device_id hi3660_stub_clk_of_match[] = {
  134. { .compatible = "hisilicon,hi3660-stub-clk", },
  135. {}
  136. };
  137. static struct platform_driver hi3660_stub_clk_driver = {
  138. .probe = hi3660_stub_clk_probe,
  139. .driver = {
  140. .name = "hi3660-stub-clk",
  141. .of_match_table = hi3660_stub_clk_of_match,
  142. },
  143. };
  144. static int __init hi3660_stub_clk_init(void)
  145. {
  146. return platform_driver_register(&hi3660_stub_clk_driver);
  147. }
  148. subsys_initcall(hi3660_stub_clk_init);