clk-gate-93.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2022 NXP
  4. *
  5. * Peng Fan <[email protected]>
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/errno.h>
  9. #include <linux/export.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/slab.h>
  13. #include "clk.h"
  14. #define DIRECT_OFFSET 0x0
  15. /*
  16. * 0b000 - LPCG will be OFF in any CPU mode.
  17. * 0b100 - LPCG will be ON in any CPU mode.
  18. */
  19. #define LPM_SETTING_OFF 0x0
  20. #define LPM_SETTING_ON 0x4
  21. #define LPM_CUR_OFFSET 0x1c
  22. #define AUTHEN_OFFSET 0x30
  23. #define CPULPM_EN BIT(2)
  24. #define TZ_NS_SHIFT 9
  25. #define TZ_NS_MASK BIT(9)
  26. #define WHITE_LIST_SHIFT 16
  27. struct imx93_clk_gate {
  28. struct clk_hw hw;
  29. void __iomem *reg;
  30. u32 bit_idx;
  31. u32 val;
  32. u32 mask;
  33. spinlock_t *lock;
  34. unsigned int *share_count;
  35. };
  36. #define to_imx93_clk_gate(_hw) container_of(_hw, struct imx93_clk_gate, hw)
  37. static void imx93_clk_gate_do_hardware(struct clk_hw *hw, bool enable)
  38. {
  39. struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
  40. u32 val;
  41. val = readl(gate->reg + AUTHEN_OFFSET);
  42. if (val & CPULPM_EN) {
  43. val = enable ? LPM_SETTING_ON : LPM_SETTING_OFF;
  44. writel(val, gate->reg + LPM_CUR_OFFSET);
  45. } else {
  46. val = readl(gate->reg + DIRECT_OFFSET);
  47. val &= ~(gate->mask << gate->bit_idx);
  48. if (enable)
  49. val |= (gate->val & gate->mask) << gate->bit_idx;
  50. writel(val, gate->reg + DIRECT_OFFSET);
  51. }
  52. }
  53. static int imx93_clk_gate_enable(struct clk_hw *hw)
  54. {
  55. struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
  56. unsigned long flags;
  57. spin_lock_irqsave(gate->lock, flags);
  58. if (gate->share_count && (*gate->share_count)++ > 0)
  59. goto out;
  60. imx93_clk_gate_do_hardware(hw, true);
  61. out:
  62. spin_unlock_irqrestore(gate->lock, flags);
  63. return 0;
  64. }
  65. static void imx93_clk_gate_disable(struct clk_hw *hw)
  66. {
  67. struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
  68. unsigned long flags;
  69. spin_lock_irqsave(gate->lock, flags);
  70. if (gate->share_count) {
  71. if (WARN_ON(*gate->share_count == 0))
  72. goto out;
  73. else if (--(*gate->share_count) > 0)
  74. goto out;
  75. }
  76. imx93_clk_gate_do_hardware(hw, false);
  77. out:
  78. spin_unlock_irqrestore(gate->lock, flags);
  79. }
  80. static int imx93_clk_gate_reg_is_enabled(struct imx93_clk_gate *gate)
  81. {
  82. u32 val = readl(gate->reg + AUTHEN_OFFSET);
  83. if (val & CPULPM_EN) {
  84. val = readl(gate->reg + LPM_CUR_OFFSET);
  85. if (val == LPM_SETTING_ON)
  86. return 1;
  87. } else {
  88. val = readl(gate->reg);
  89. if (((val >> gate->bit_idx) & gate->mask) == gate->val)
  90. return 1;
  91. }
  92. return 0;
  93. }
  94. static int imx93_clk_gate_is_enabled(struct clk_hw *hw)
  95. {
  96. struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
  97. unsigned long flags;
  98. int ret;
  99. spin_lock_irqsave(gate->lock, flags);
  100. ret = imx93_clk_gate_reg_is_enabled(gate);
  101. spin_unlock_irqrestore(gate->lock, flags);
  102. return ret;
  103. }
  104. static void imx93_clk_gate_disable_unused(struct clk_hw *hw)
  105. {
  106. struct imx93_clk_gate *gate = to_imx93_clk_gate(hw);
  107. unsigned long flags;
  108. spin_lock_irqsave(gate->lock, flags);
  109. if (!gate->share_count || *gate->share_count == 0)
  110. imx93_clk_gate_do_hardware(hw, false);
  111. spin_unlock_irqrestore(gate->lock, flags);
  112. }
  113. static const struct clk_ops imx93_clk_gate_ops = {
  114. .enable = imx93_clk_gate_enable,
  115. .disable = imx93_clk_gate_disable,
  116. .disable_unused = imx93_clk_gate_disable_unused,
  117. .is_enabled = imx93_clk_gate_is_enabled,
  118. };
  119. static const struct clk_ops imx93_clk_gate_ro_ops = {
  120. .is_enabled = imx93_clk_gate_is_enabled,
  121. };
  122. struct clk_hw *imx93_clk_gate(struct device *dev, const char *name, const char *parent_name,
  123. unsigned long flags, void __iomem *reg, u32 bit_idx, u32 val,
  124. u32 mask, u32 domain_id, unsigned int *share_count)
  125. {
  126. struct imx93_clk_gate *gate;
  127. struct clk_hw *hw;
  128. struct clk_init_data init;
  129. int ret;
  130. u32 authen;
  131. gate = kzalloc(sizeof(struct imx93_clk_gate), GFP_KERNEL);
  132. if (!gate)
  133. return ERR_PTR(-ENOMEM);
  134. gate->reg = reg;
  135. gate->lock = &imx_ccm_lock;
  136. gate->bit_idx = bit_idx;
  137. gate->val = val;
  138. gate->mask = mask;
  139. gate->share_count = share_count;
  140. init.name = name;
  141. init.ops = &imx93_clk_gate_ops;
  142. init.flags = flags | CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE;
  143. init.parent_names = parent_name ? &parent_name : NULL;
  144. init.num_parents = parent_name ? 1 : 0;
  145. gate->hw.init = &init;
  146. hw = &gate->hw;
  147. authen = readl(reg + AUTHEN_OFFSET);
  148. if (!(authen & TZ_NS_MASK) || !(authen & BIT(WHITE_LIST_SHIFT + domain_id)))
  149. init.ops = &imx93_clk_gate_ro_ops;
  150. ret = clk_hw_register(dev, hw);
  151. if (ret) {
  152. kfree(gate);
  153. return ERR_PTR(ret);
  154. }
  155. return hw;
  156. }
  157. EXPORT_SYMBOL_GPL(imx93_clk_gate);