clk-gate-s10.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017, Intel Corporation
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/io.h>
  7. #include <linux/slab.h>
  8. #include "stratix10-clk.h"
  9. #include "clk.h"
  10. #define SOCFPGA_CS_PDBG_CLK "cs_pdbg_clk"
  11. #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)
  12. #define SOCFPGA_EMAC0_CLK "emac0_clk"
  13. #define SOCFPGA_EMAC1_CLK "emac1_clk"
  14. #define SOCFPGA_EMAC2_CLK "emac2_clk"
  15. #define AGILEX_BYPASS_OFFSET 0xC
  16. #define STRATIX10_BYPASS_OFFSET 0x2C
  17. #define BOOTCLK_BYPASS 2
  18. static unsigned long socfpga_gate_clk_recalc_rate(struct clk_hw *hwclk,
  19. unsigned long parent_rate)
  20. {
  21. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  22. u32 div = 1, val;
  23. if (socfpgaclk->fixed_div) {
  24. div = socfpgaclk->fixed_div;
  25. } else if (socfpgaclk->div_reg) {
  26. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  27. val &= GENMASK(socfpgaclk->width - 1, 0);
  28. div = (1 << val);
  29. }
  30. return parent_rate / div;
  31. }
  32. static unsigned long socfpga_dbg_clk_recalc_rate(struct clk_hw *hwclk,
  33. unsigned long parent_rate)
  34. {
  35. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  36. u32 div, val;
  37. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  38. val &= GENMASK(socfpgaclk->width - 1, 0);
  39. div = (1 << val);
  40. div = div ? 4 : 1;
  41. return parent_rate / div;
  42. }
  43. static u8 socfpga_gate_get_parent(struct clk_hw *hwclk)
  44. {
  45. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  46. u32 mask, second_bypass;
  47. u8 parent = 0;
  48. const char *name = clk_hw_get_name(hwclk);
  49. if (socfpgaclk->bypass_reg) {
  50. mask = (0x1 << socfpgaclk->bypass_shift);
  51. parent = ((readl(socfpgaclk->bypass_reg) & mask) >>
  52. socfpgaclk->bypass_shift);
  53. }
  54. if (streq(name, SOCFPGA_EMAC0_CLK) ||
  55. streq(name, SOCFPGA_EMAC1_CLK) ||
  56. streq(name, SOCFPGA_EMAC2_CLK)) {
  57. second_bypass = readl(socfpgaclk->bypass_reg -
  58. STRATIX10_BYPASS_OFFSET);
  59. /* EMACA bypass to bootclk @0xB0 offset */
  60. if (second_bypass & 0x1)
  61. if (parent == 0) /* only applicable if parent is maca */
  62. parent = BOOTCLK_BYPASS;
  63. if (second_bypass & 0x2)
  64. if (parent == 1) /* only applicable if parent is macb */
  65. parent = BOOTCLK_BYPASS;
  66. }
  67. return parent;
  68. }
  69. static u8 socfpga_agilex_gate_get_parent(struct clk_hw *hwclk)
  70. {
  71. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  72. u32 mask, second_bypass;
  73. u8 parent = 0;
  74. const char *name = clk_hw_get_name(hwclk);
  75. if (socfpgaclk->bypass_reg) {
  76. mask = (0x1 << socfpgaclk->bypass_shift);
  77. parent = ((readl(socfpgaclk->bypass_reg) & mask) >>
  78. socfpgaclk->bypass_shift);
  79. }
  80. if (streq(name, SOCFPGA_EMAC0_CLK) ||
  81. streq(name, SOCFPGA_EMAC1_CLK) ||
  82. streq(name, SOCFPGA_EMAC2_CLK)) {
  83. second_bypass = readl(socfpgaclk->bypass_reg -
  84. AGILEX_BYPASS_OFFSET);
  85. /* EMACA bypass to bootclk @0x88 offset */
  86. if (second_bypass & 0x1)
  87. if (parent == 0) /* only applicable if parent is maca */
  88. parent = BOOTCLK_BYPASS;
  89. if (second_bypass & 0x2)
  90. if (parent == 1) /* only applicable if parent is macb */
  91. parent = BOOTCLK_BYPASS;
  92. }
  93. return parent;
  94. }
  95. static struct clk_ops gateclk_ops = {
  96. .recalc_rate = socfpga_gate_clk_recalc_rate,
  97. .get_parent = socfpga_gate_get_parent,
  98. };
  99. static const struct clk_ops agilex_gateclk_ops = {
  100. .recalc_rate = socfpga_gate_clk_recalc_rate,
  101. .get_parent = socfpga_agilex_gate_get_parent,
  102. };
  103. static const struct clk_ops dbgclk_ops = {
  104. .recalc_rate = socfpga_dbg_clk_recalc_rate,
  105. .get_parent = socfpga_gate_get_parent,
  106. };
  107. struct clk_hw *s10_register_gate(const struct stratix10_gate_clock *clks, void __iomem *regbase)
  108. {
  109. struct clk_hw *hw_clk;
  110. struct socfpga_gate_clk *socfpga_clk;
  111. struct clk_init_data init;
  112. const char *parent_name = clks->parent_name;
  113. int ret;
  114. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  115. if (!socfpga_clk)
  116. return NULL;
  117. socfpga_clk->hw.reg = regbase + clks->gate_reg;
  118. socfpga_clk->hw.bit_idx = clks->gate_idx;
  119. gateclk_ops.enable = clk_gate_ops.enable;
  120. gateclk_ops.disable = clk_gate_ops.disable;
  121. socfpga_clk->fixed_div = clks->fixed_div;
  122. if (clks->div_reg)
  123. socfpga_clk->div_reg = regbase + clks->div_reg;
  124. else
  125. socfpga_clk->div_reg = NULL;
  126. socfpga_clk->width = clks->div_width;
  127. socfpga_clk->shift = clks->div_offset;
  128. if (clks->bypass_reg)
  129. socfpga_clk->bypass_reg = regbase + clks->bypass_reg;
  130. else
  131. socfpga_clk->bypass_reg = NULL;
  132. socfpga_clk->bypass_shift = clks->bypass_shift;
  133. if (streq(clks->name, "cs_pdbg_clk"))
  134. init.ops = &dbgclk_ops;
  135. else
  136. init.ops = &gateclk_ops;
  137. init.name = clks->name;
  138. init.flags = clks->flags;
  139. init.num_parents = clks->num_parents;
  140. init.parent_names = parent_name ? &parent_name : NULL;
  141. if (init.parent_names == NULL)
  142. init.parent_data = clks->parent_data;
  143. socfpga_clk->hw.hw.init = &init;
  144. hw_clk = &socfpga_clk->hw.hw;
  145. ret = clk_hw_register(NULL, &socfpga_clk->hw.hw);
  146. if (ret) {
  147. kfree(socfpga_clk);
  148. return ERR_PTR(ret);
  149. }
  150. return hw_clk;
  151. }
  152. struct clk_hw *agilex_register_gate(const struct stratix10_gate_clock *clks, void __iomem *regbase)
  153. {
  154. struct clk_hw *hw_clk;
  155. struct socfpga_gate_clk *socfpga_clk;
  156. struct clk_init_data init;
  157. const char *parent_name = clks->parent_name;
  158. int ret;
  159. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  160. if (!socfpga_clk)
  161. return NULL;
  162. socfpga_clk->hw.reg = regbase + clks->gate_reg;
  163. socfpga_clk->hw.bit_idx = clks->gate_idx;
  164. gateclk_ops.enable = clk_gate_ops.enable;
  165. gateclk_ops.disable = clk_gate_ops.disable;
  166. socfpga_clk->fixed_div = clks->fixed_div;
  167. if (clks->div_reg)
  168. socfpga_clk->div_reg = regbase + clks->div_reg;
  169. else
  170. socfpga_clk->div_reg = NULL;
  171. socfpga_clk->width = clks->div_width;
  172. socfpga_clk->shift = clks->div_offset;
  173. if (clks->bypass_reg)
  174. socfpga_clk->bypass_reg = regbase + clks->bypass_reg;
  175. else
  176. socfpga_clk->bypass_reg = NULL;
  177. socfpga_clk->bypass_shift = clks->bypass_shift;
  178. if (streq(clks->name, "cs_pdbg_clk"))
  179. init.ops = &dbgclk_ops;
  180. else
  181. init.ops = &agilex_gateclk_ops;
  182. init.name = clks->name;
  183. init.flags = clks->flags;
  184. init.num_parents = clks->num_parents;
  185. init.parent_names = parent_name ? &parent_name : NULL;
  186. if (init.parent_names == NULL)
  187. init.parent_data = clks->parent_data;
  188. socfpga_clk->hw.hw.init = &init;
  189. hw_clk = &socfpga_clk->hw.hw;
  190. ret = clk_hw_register(NULL, &socfpga_clk->hw.hw);
  191. if (ret) {
  192. kfree(socfpga_clk);
  193. return ERR_PTR(ret);
  194. }
  195. return hw_clk;
  196. }