clk-sun9i-cpus.c 6.2 KB

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