clk-sun9i-cpus.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2015 Chen-Yu Tsai
  4. *
  5. * Chen-Yu Tsai <wens@csie.org>
  6. *
  7. * Allwinner A80 CPUS clock driver
  8. *
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. static DEFINE_SPINLOCK(sun9i_a80_cpus_lock);
  17. /**
  18. * sun9i_a80_cpus_clk_setup() - Setup function for a80 cpus composite clk
  19. */
  20. #define SUN9I_CPUS_MAX_PARENTS 4
  21. #define SUN9I_CPUS_MUX_PARENT_PLL4 3
  22. #define SUN9I_CPUS_MUX_SHIFT 16
  23. #define SUN9I_CPUS_MUX_MASK GENMASK(17, 16)
  24. #define SUN9I_CPUS_MUX_GET_PARENT(reg) ((reg & SUN9I_CPUS_MUX_MASK) >> \
  25. SUN9I_CPUS_MUX_SHIFT)
  26. #define SUN9I_CPUS_DIV_SHIFT 4
  27. #define SUN9I_CPUS_DIV_MASK GENMASK(5, 4)
  28. #define SUN9I_CPUS_DIV_GET(reg) ((reg & SUN9I_CPUS_DIV_MASK) >> \
  29. SUN9I_CPUS_DIV_SHIFT)
  30. #define SUN9I_CPUS_DIV_SET(reg, div) ((reg & ~SUN9I_CPUS_DIV_MASK) | \
  31. (div << SUN9I_CPUS_DIV_SHIFT))
  32. #define SUN9I_CPUS_PLL4_DIV_SHIFT 8
  33. #define SUN9I_CPUS_PLL4_DIV_MASK GENMASK(12, 8)
  34. #define SUN9I_CPUS_PLL4_DIV_GET(reg) ((reg & SUN9I_CPUS_PLL4_DIV_MASK) >> \
  35. SUN9I_CPUS_PLL4_DIV_SHIFT)
  36. #define SUN9I_CPUS_PLL4_DIV_SET(reg, div) ((reg & ~SUN9I_CPUS_PLL4_DIV_MASK) | \
  37. (div << SUN9I_CPUS_PLL4_DIV_SHIFT))
  38. struct sun9i_a80_cpus_clk {
  39. struct clk_hw hw;
  40. void __iomem *reg;
  41. };
  42. #define to_sun9i_a80_cpus_clk(_hw) container_of(_hw, struct sun9i_a80_cpus_clk, hw)
  43. static unsigned long sun9i_a80_cpus_clk_recalc_rate(struct clk_hw *hw,
  44. unsigned long parent_rate)
  45. {
  46. struct sun9i_a80_cpus_clk *cpus = to_sun9i_a80_cpus_clk(hw);
  47. unsigned long rate;
  48. u32 reg;
  49. /* Fetch the register value */
  50. reg = readl(cpus->reg);
  51. /* apply pre-divider first if parent is pll4 */
  52. if (SUN9I_CPUS_MUX_GET_PARENT(reg) == SUN9I_CPUS_MUX_PARENT_PLL4)
  53. parent_rate /= SUN9I_CPUS_PLL4_DIV_GET(reg) + 1;
  54. /* clk divider */
  55. rate = parent_rate / (SUN9I_CPUS_DIV_GET(reg) + 1);
  56. return rate;
  57. }
  58. static long sun9i_a80_cpus_clk_round(unsigned long rate, u8 *divp, u8 *pre_divp,
  59. u8 parent, unsigned long parent_rate)
  60. {
  61. u8 div, pre_div = 1;
  62. /*
  63. * clock can only divide, so we will never be able to achieve
  64. * frequencies higher than the parent frequency
  65. */
  66. if (parent_rate && rate > parent_rate)
  67. rate = parent_rate;
  68. div = DIV_ROUND_UP(parent_rate, rate);
  69. /* calculate pre-divider if parent is pll4 */
  70. if (parent == SUN9I_CPUS_MUX_PARENT_PLL4 && div > 4) {
  71. /* pre-divider is 1 ~ 32 */
  72. if (div < 32) {
  73. pre_div = div;
  74. div = 1;
  75. } else if (div < 64) {
  76. pre_div = DIV_ROUND_UP(div, 2);
  77. div = 2;
  78. } else if (div < 96) {
  79. pre_div = DIV_ROUND_UP(div, 3);
  80. div = 3;
  81. } else {
  82. pre_div = DIV_ROUND_UP(div, 4);
  83. div = 4;
  84. }
  85. }
  86. /* we were asked to pass back divider values */
  87. if (divp) {
  88. *divp = div - 1;
  89. *pre_divp = pre_div - 1;
  90. }
  91. return parent_rate / pre_div / div;
  92. }
  93. static int sun9i_a80_cpus_clk_determine_rate(struct clk_hw *clk,
  94. struct clk_rate_request *req)
  95. {
  96. struct clk_hw *parent, *best_parent = NULL;
  97. int i, num_parents;
  98. unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
  99. unsigned long rate = req->rate;
  100. /* find the parent that can help provide the fastest rate <= rate */
  101. num_parents = clk_hw_get_num_parents(clk);
  102. for (i = 0; i < num_parents; i++) {
  103. parent = clk_hw_get_parent_by_index(clk, i);
  104. if (!parent)
  105. continue;
  106. if (clk_hw_get_flags(clk) & CLK_SET_RATE_PARENT)
  107. parent_rate = clk_hw_round_rate(parent, rate);
  108. else
  109. parent_rate = clk_hw_get_rate(parent);
  110. child_rate = sun9i_a80_cpus_clk_round(rate, NULL, NULL, i,
  111. parent_rate);
  112. if (child_rate <= rate && child_rate > best_child_rate) {
  113. best_parent = parent;
  114. best = parent_rate;
  115. best_child_rate = child_rate;
  116. }
  117. }
  118. if (!best_parent)
  119. return -EINVAL;
  120. req->best_parent_hw = best_parent;
  121. req->best_parent_rate = best;
  122. req->rate = best_child_rate;
  123. return 0;
  124. }
  125. static int sun9i_a80_cpus_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  126. unsigned long parent_rate)
  127. {
  128. struct sun9i_a80_cpus_clk *cpus = to_sun9i_a80_cpus_clk(hw);
  129. unsigned long flags;
  130. u8 div, pre_div, parent;
  131. u32 reg;
  132. spin_lock_irqsave(&sun9i_a80_cpus_lock, flags);
  133. reg = readl(cpus->reg);
  134. /* need to know which parent is used to apply pre-divider */
  135. parent = SUN9I_CPUS_MUX_GET_PARENT(reg);
  136. sun9i_a80_cpus_clk_round(rate, &div, &pre_div, parent, parent_rate);
  137. reg = SUN9I_CPUS_DIV_SET(reg, div);
  138. reg = SUN9I_CPUS_PLL4_DIV_SET(reg, pre_div);
  139. writel(reg, cpus->reg);
  140. spin_unlock_irqrestore(&sun9i_a80_cpus_lock, flags);
  141. return 0;
  142. }
  143. static const struct clk_ops sun9i_a80_cpus_clk_ops = {
  144. .determine_rate = sun9i_a80_cpus_clk_determine_rate,
  145. .recalc_rate = sun9i_a80_cpus_clk_recalc_rate,
  146. .set_rate = sun9i_a80_cpus_clk_set_rate,
  147. };
  148. static void sun9i_a80_cpus_setup(struct device_node *node)
  149. {
  150. const char *clk_name = node->name;
  151. const char *parents[SUN9I_CPUS_MAX_PARENTS];
  152. struct resource res;
  153. struct sun9i_a80_cpus_clk *cpus;
  154. struct clk_mux *mux;
  155. struct clk *clk;
  156. int ret;
  157. cpus = kzalloc(sizeof(*cpus), GFP_KERNEL);
  158. if (!cpus)
  159. return;
  160. cpus->reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  161. if (IS_ERR(cpus->reg))
  162. goto err_free_cpus;
  163. of_property_read_string(node, "clock-output-names", &clk_name);
  164. /* we have a mux, we will have >1 parents */
  165. ret = of_clk_parent_fill(node, parents, SUN9I_CPUS_MAX_PARENTS);
  166. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  167. if (!mux)
  168. goto err_unmap;
  169. /* set up clock properties */
  170. mux->reg = cpus->reg;
  171. mux->shift = SUN9I_CPUS_MUX_SHIFT;
  172. /* un-shifted mask is what mux_clk expects */
  173. mux->mask = SUN9I_CPUS_MUX_MASK >> SUN9I_CPUS_MUX_SHIFT;
  174. mux->lock = &sun9i_a80_cpus_lock;
  175. clk = clk_register_composite(NULL, clk_name, parents, ret,
  176. &mux->hw, &clk_mux_ops,
  177. &cpus->hw, &sun9i_a80_cpus_clk_ops,
  178. NULL, NULL, 0);
  179. if (IS_ERR(clk))
  180. goto err_free_mux;
  181. ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  182. if (ret)
  183. goto err_unregister;
  184. return;
  185. err_unregister:
  186. clk_unregister(clk);
  187. err_free_mux:
  188. kfree(mux);
  189. err_unmap:
  190. iounmap(cpus->reg);
  191. of_address_to_resource(node, 0, &res);
  192. release_mem_region(res.start, resource_size(&res));
  193. err_free_cpus:
  194. kfree(cpus);
  195. }
  196. CLK_OF_DECLARE(sun9i_a80_cpus, "allwinner,sun9i-a80-cpus-clk",
  197. sun9i_a80_cpus_setup);