clk-ddr.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2016 Rockchip Electronics Co. Ltd.
  3. * Author: Lin Huang <hl@rock-chips.com>
  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/arm-smccc.h>
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/io.h>
  19. #include <linux/slab.h>
  20. #include <soc/rockchip/rockchip_sip.h>
  21. #include "clk.h"
  22. struct rockchip_ddrclk {
  23. struct clk_hw hw;
  24. void __iomem *reg_base;
  25. int mux_offset;
  26. int mux_shift;
  27. int mux_width;
  28. int div_shift;
  29. int div_width;
  30. int ddr_flag;
  31. spinlock_t *lock;
  32. };
  33. #define to_rockchip_ddrclk_hw(hw) container_of(hw, struct rockchip_ddrclk, hw)
  34. static int rockchip_ddrclk_sip_set_rate(struct clk_hw *hw, unsigned long drate,
  35. unsigned long prate)
  36. {
  37. struct rockchip_ddrclk *ddrclk = to_rockchip_ddrclk_hw(hw);
  38. unsigned long flags;
  39. struct arm_smccc_res res;
  40. spin_lock_irqsave(ddrclk->lock, flags);
  41. arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, drate, 0,
  42. ROCKCHIP_SIP_CONFIG_DRAM_SET_RATE,
  43. 0, 0, 0, 0, &res);
  44. spin_unlock_irqrestore(ddrclk->lock, flags);
  45. return res.a0;
  46. }
  47. static unsigned long
  48. rockchip_ddrclk_sip_recalc_rate(struct clk_hw *hw,
  49. unsigned long parent_rate)
  50. {
  51. struct arm_smccc_res res;
  52. arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, 0, 0,
  53. ROCKCHIP_SIP_CONFIG_DRAM_GET_RATE,
  54. 0, 0, 0, 0, &res);
  55. return res.a0;
  56. }
  57. static long rockchip_ddrclk_sip_round_rate(struct clk_hw *hw,
  58. unsigned long rate,
  59. unsigned long *prate)
  60. {
  61. struct arm_smccc_res res;
  62. arm_smccc_smc(ROCKCHIP_SIP_DRAM_FREQ, rate, 0,
  63. ROCKCHIP_SIP_CONFIG_DRAM_ROUND_RATE,
  64. 0, 0, 0, 0, &res);
  65. return res.a0;
  66. }
  67. static u8 rockchip_ddrclk_get_parent(struct clk_hw *hw)
  68. {
  69. struct rockchip_ddrclk *ddrclk = to_rockchip_ddrclk_hw(hw);
  70. int num_parents = clk_hw_get_num_parents(hw);
  71. u32 val;
  72. val = clk_readl(ddrclk->reg_base +
  73. ddrclk->mux_offset) >> ddrclk->mux_shift;
  74. val &= GENMASK(ddrclk->mux_width - 1, 0);
  75. if (val >= num_parents)
  76. return -EINVAL;
  77. return val;
  78. }
  79. static const struct clk_ops rockchip_ddrclk_sip_ops = {
  80. .recalc_rate = rockchip_ddrclk_sip_recalc_rate,
  81. .set_rate = rockchip_ddrclk_sip_set_rate,
  82. .round_rate = rockchip_ddrclk_sip_round_rate,
  83. .get_parent = rockchip_ddrclk_get_parent,
  84. };
  85. struct clk *rockchip_clk_register_ddrclk(const char *name, int flags,
  86. const char *const *parent_names,
  87. u8 num_parents, int mux_offset,
  88. int mux_shift, int mux_width,
  89. int div_shift, int div_width,
  90. int ddr_flag, void __iomem *reg_base,
  91. spinlock_t *lock)
  92. {
  93. struct rockchip_ddrclk *ddrclk;
  94. struct clk_init_data init;
  95. struct clk *clk;
  96. ddrclk = kzalloc(sizeof(*ddrclk), GFP_KERNEL);
  97. if (!ddrclk)
  98. return ERR_PTR(-ENOMEM);
  99. init.name = name;
  100. init.parent_names = parent_names;
  101. init.num_parents = num_parents;
  102. init.flags = flags;
  103. init.flags |= CLK_SET_RATE_NO_REPARENT;
  104. switch (ddr_flag) {
  105. case ROCKCHIP_DDRCLK_SIP:
  106. init.ops = &rockchip_ddrclk_sip_ops;
  107. break;
  108. default:
  109. pr_err("%s: unsupported ddrclk type %d\n", __func__, ddr_flag);
  110. kfree(ddrclk);
  111. return ERR_PTR(-EINVAL);
  112. }
  113. ddrclk->reg_base = reg_base;
  114. ddrclk->lock = lock;
  115. ddrclk->hw.init = &init;
  116. ddrclk->mux_offset = mux_offset;
  117. ddrclk->mux_shift = mux_shift;
  118. ddrclk->mux_width = mux_width;
  119. ddrclk->div_shift = div_shift;
  120. ddrclk->div_width = div_width;
  121. ddrclk->ddr_flag = ddr_flag;
  122. clk = clk_register(NULL, &ddrclk->hw);
  123. if (IS_ERR(clk))
  124. kfree(ddrclk);
  125. return clk;
  126. }