clk-periph-s10.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017, Intel Corporation
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/clk-provider.h>
  7. #include <linux/io.h>
  8. #include "stratix10-clk.h"
  9. #include "clk.h"
  10. #define CLK_MGR_FREE_SHIFT 16
  11. #define CLK_MGR_FREE_MASK 0x7
  12. #define SWCTRLBTCLKSEN_SHIFT 8
  13. #define to_periph_clk(p) container_of(p, struct socfpga_periph_clk, hw.hw)
  14. static unsigned long n5x_clk_peri_c_clk_recalc_rate(struct clk_hw *hwclk,
  15. unsigned long parent_rate)
  16. {
  17. struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk);
  18. unsigned long div;
  19. unsigned long shift = socfpgaclk->shift;
  20. u32 val;
  21. val = readl(socfpgaclk->hw.reg);
  22. val &= (0x1f << shift);
  23. div = (val >> shift) + 1;
  24. return parent_rate / div;
  25. }
  26. static unsigned long clk_peri_c_clk_recalc_rate(struct clk_hw *hwclk,
  27. unsigned long parent_rate)
  28. {
  29. struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk);
  30. unsigned long div = 1;
  31. u32 val;
  32. val = readl(socfpgaclk->hw.reg);
  33. val &= GENMASK(SWCTRLBTCLKSEN_SHIFT - 1, 0);
  34. parent_rate /= val;
  35. return parent_rate / div;
  36. }
  37. static unsigned long clk_peri_cnt_clk_recalc_rate(struct clk_hw *hwclk,
  38. unsigned long parent_rate)
  39. {
  40. struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk);
  41. unsigned long div = 1;
  42. if (socfpgaclk->fixed_div) {
  43. div = socfpgaclk->fixed_div;
  44. } else {
  45. if (socfpgaclk->hw.reg)
  46. div = ((readl(socfpgaclk->hw.reg) & 0x7ff) + 1);
  47. }
  48. return parent_rate / div;
  49. }
  50. static u8 clk_periclk_get_parent(struct clk_hw *hwclk)
  51. {
  52. struct socfpga_periph_clk *socfpgaclk = to_periph_clk(hwclk);
  53. u32 clk_src, mask;
  54. u8 parent = 0;
  55. /* handle the bypass first */
  56. if (socfpgaclk->bypass_reg) {
  57. mask = (0x1 << socfpgaclk->bypass_shift);
  58. parent = ((readl(socfpgaclk->bypass_reg) & mask) >>
  59. socfpgaclk->bypass_shift);
  60. if (parent)
  61. return parent;
  62. }
  63. if (socfpgaclk->hw.reg) {
  64. clk_src = readl(socfpgaclk->hw.reg);
  65. parent = (clk_src >> CLK_MGR_FREE_SHIFT) &
  66. CLK_MGR_FREE_MASK;
  67. }
  68. return parent;
  69. }
  70. static const struct clk_ops n5x_peri_c_clk_ops = {
  71. .recalc_rate = n5x_clk_peri_c_clk_recalc_rate,
  72. .get_parent = clk_periclk_get_parent,
  73. };
  74. static const struct clk_ops peri_c_clk_ops = {
  75. .recalc_rate = clk_peri_c_clk_recalc_rate,
  76. .get_parent = clk_periclk_get_parent,
  77. };
  78. static const struct clk_ops peri_cnt_clk_ops = {
  79. .recalc_rate = clk_peri_cnt_clk_recalc_rate,
  80. .get_parent = clk_periclk_get_parent,
  81. };
  82. struct clk_hw *s10_register_periph(const struct stratix10_perip_c_clock *clks,
  83. void __iomem *reg)
  84. {
  85. struct clk_hw *hw_clk;
  86. struct socfpga_periph_clk *periph_clk;
  87. struct clk_init_data init;
  88. const char *name = clks->name;
  89. const char *parent_name = clks->parent_name;
  90. int ret;
  91. periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
  92. if (WARN_ON(!periph_clk))
  93. return NULL;
  94. periph_clk->hw.reg = reg + clks->offset;
  95. init.name = name;
  96. init.ops = &peri_c_clk_ops;
  97. init.flags = clks->flags;
  98. init.num_parents = clks->num_parents;
  99. init.parent_names = parent_name ? &parent_name : NULL;
  100. if (init.parent_names == NULL)
  101. init.parent_data = clks->parent_data;
  102. periph_clk->hw.hw.init = &init;
  103. hw_clk = &periph_clk->hw.hw;
  104. ret = clk_hw_register(NULL, hw_clk);
  105. if (ret) {
  106. kfree(periph_clk);
  107. return ERR_PTR(ret);
  108. }
  109. return hw_clk;
  110. }
  111. struct clk_hw *n5x_register_periph(const struct n5x_perip_c_clock *clks,
  112. void __iomem *regbase)
  113. {
  114. struct clk_hw *hw_clk;
  115. struct socfpga_periph_clk *periph_clk;
  116. struct clk_init_data init;
  117. const char *name = clks->name;
  118. const char *parent_name = clks->parent_name;
  119. int ret;
  120. periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
  121. if (WARN_ON(!periph_clk))
  122. return NULL;
  123. periph_clk->hw.reg = regbase + clks->offset;
  124. periph_clk->shift = clks->shift;
  125. init.name = name;
  126. init.ops = &n5x_peri_c_clk_ops;
  127. init.flags = clks->flags;
  128. init.num_parents = clks->num_parents;
  129. init.parent_names = parent_name ? &parent_name : NULL;
  130. periph_clk->hw.hw.init = &init;
  131. hw_clk = &periph_clk->hw.hw;
  132. ret = clk_hw_register(NULL, hw_clk);
  133. if (ret) {
  134. kfree(periph_clk);
  135. return ERR_PTR(ret);
  136. }
  137. return hw_clk;
  138. }
  139. struct clk_hw *s10_register_cnt_periph(const struct stratix10_perip_cnt_clock *clks,
  140. void __iomem *regbase)
  141. {
  142. struct clk_hw *hw_clk;
  143. struct socfpga_periph_clk *periph_clk;
  144. struct clk_init_data init;
  145. const char *name = clks->name;
  146. const char *parent_name = clks->parent_name;
  147. int ret;
  148. periph_clk = kzalloc(sizeof(*periph_clk), GFP_KERNEL);
  149. if (WARN_ON(!periph_clk))
  150. return NULL;
  151. if (clks->offset)
  152. periph_clk->hw.reg = regbase + clks->offset;
  153. else
  154. periph_clk->hw.reg = NULL;
  155. if (clks->bypass_reg)
  156. periph_clk->bypass_reg = regbase + clks->bypass_reg;
  157. else
  158. periph_clk->bypass_reg = NULL;
  159. periph_clk->bypass_shift = clks->bypass_shift;
  160. periph_clk->fixed_div = clks->fixed_divider;
  161. init.name = name;
  162. init.ops = &peri_cnt_clk_ops;
  163. init.flags = clks->flags;
  164. init.num_parents = clks->num_parents;
  165. init.parent_names = parent_name ? &parent_name : NULL;
  166. if (init.parent_names == NULL)
  167. init.parent_data = clks->parent_data;
  168. periph_clk->hw.hw.init = &init;
  169. hw_clk = &periph_clk->hw.hw;
  170. ret = clk_hw_register(NULL, hw_clk);
  171. if (ret) {
  172. kfree(periph_clk);
  173. return ERR_PTR(ret);
  174. }
  175. return hw_clk;
  176. }