clk-sun9i-cpus.c 6.2 KB

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