clk-gate-s10.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017, Intel Corporation
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/slab.h>
  7. #include "stratix10-clk.h"
  8. #include "clk.h"
  9. #define SOCFPGA_CS_PDBG_CLK "cs_pdbg_clk"
  10. #define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)
  11. static unsigned long socfpga_gate_clk_recalc_rate(struct clk_hw *hwclk,
  12. unsigned long parent_rate)
  13. {
  14. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  15. u32 div = 1, val;
  16. if (socfpgaclk->fixed_div) {
  17. div = socfpgaclk->fixed_div;
  18. } else if (socfpgaclk->div_reg) {
  19. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  20. val &= GENMASK(socfpgaclk->width - 1, 0);
  21. div = (1 << val);
  22. }
  23. return parent_rate / div;
  24. }
  25. static unsigned long socfpga_dbg_clk_recalc_rate(struct clk_hw *hwclk,
  26. unsigned long parent_rate)
  27. {
  28. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  29. u32 div = 1, val;
  30. val = readl(socfpgaclk->div_reg) >> socfpgaclk->shift;
  31. val &= GENMASK(socfpgaclk->width - 1, 0);
  32. div = (1 << val);
  33. div = div ? 4 : 1;
  34. return parent_rate / div;
  35. }
  36. static u8 socfpga_gate_get_parent(struct clk_hw *hwclk)
  37. {
  38. struct socfpga_gate_clk *socfpgaclk = to_socfpga_gate_clk(hwclk);
  39. u32 mask;
  40. u8 parent = 0;
  41. if (socfpgaclk->bypass_reg) {
  42. mask = (0x1 << socfpgaclk->bypass_shift);
  43. parent = ((readl(socfpgaclk->bypass_reg) & mask) >>
  44. socfpgaclk->bypass_shift);
  45. }
  46. return parent;
  47. }
  48. static struct clk_ops gateclk_ops = {
  49. .recalc_rate = socfpga_gate_clk_recalc_rate,
  50. .get_parent = socfpga_gate_get_parent,
  51. };
  52. static const struct clk_ops dbgclk_ops = {
  53. .recalc_rate = socfpga_dbg_clk_recalc_rate,
  54. .get_parent = socfpga_gate_get_parent,
  55. };
  56. struct clk *s10_register_gate(const char *name, const char *parent_name,
  57. const char * const *parent_names,
  58. u8 num_parents, unsigned long flags,
  59. void __iomem *regbase, unsigned long gate_reg,
  60. unsigned long gate_idx, unsigned long div_reg,
  61. unsigned long div_offset, u8 div_width,
  62. unsigned long bypass_reg, u8 bypass_shift,
  63. u8 fixed_div)
  64. {
  65. struct clk *clk;
  66. struct socfpga_gate_clk *socfpga_clk;
  67. struct clk_init_data init;
  68. socfpga_clk = kzalloc(sizeof(*socfpga_clk), GFP_KERNEL);
  69. if (!socfpga_clk)
  70. return NULL;
  71. socfpga_clk->hw.reg = regbase + gate_reg;
  72. socfpga_clk->hw.bit_idx = gate_idx;
  73. gateclk_ops.enable = clk_gate_ops.enable;
  74. gateclk_ops.disable = clk_gate_ops.disable;
  75. socfpga_clk->fixed_div = fixed_div;
  76. if (div_reg)
  77. socfpga_clk->div_reg = regbase + div_reg;
  78. else
  79. socfpga_clk->div_reg = NULL;
  80. socfpga_clk->width = div_width;
  81. socfpga_clk->shift = div_offset;
  82. if (bypass_reg)
  83. socfpga_clk->bypass_reg = regbase + bypass_reg;
  84. else
  85. socfpga_clk->bypass_reg = NULL;
  86. socfpga_clk->bypass_shift = bypass_shift;
  87. if (streq(name, "cs_pdbg_clk"))
  88. init.ops = &dbgclk_ops;
  89. else
  90. init.ops = &gateclk_ops;
  91. init.name = name;
  92. init.flags = flags;
  93. init.num_parents = num_parents;
  94. init.parent_names = parent_names ? parent_names : &parent_name;
  95. socfpga_clk->hw.hw.init = &init;
  96. clk = clk_register(NULL, &socfpga_clk->hw.hw);
  97. if (WARN_ON(IS_ERR(clk))) {
  98. kfree(socfpga_clk);
  99. return NULL;
  100. }
  101. return clk;
  102. }