clk-periph-gate.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/slab.h>
  7. #include <linux/io.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <soc/tegra/fuse.h>
  11. #include "clk.h"
  12. static DEFINE_SPINLOCK(periph_ref_lock);
  13. /* Macros to assist peripheral gate clock */
  14. #define read_enb(gate) \
  15. readl_relaxed(gate->clk_base + (gate->regs->enb_reg))
  16. #define write_enb_set(val, gate) \
  17. writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg))
  18. #define write_enb_clr(val, gate) \
  19. writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg))
  20. #define read_rst(gate) \
  21. readl_relaxed(gate->clk_base + (gate->regs->rst_reg))
  22. #define write_rst_clr(val, gate) \
  23. writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
  24. #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32))
  25. #define LVL2_CLK_GATE_OVRE 0x554
  26. /* Peripheral gate clock ops */
  27. static int clk_periph_is_enabled(struct clk_hw *hw)
  28. {
  29. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  30. int state = 1;
  31. if (!(read_enb(gate) & periph_clk_to_bit(gate)))
  32. state = 0;
  33. if (!(gate->flags & TEGRA_PERIPH_NO_RESET))
  34. if (read_rst(gate) & periph_clk_to_bit(gate))
  35. state = 0;
  36. return state;
  37. }
  38. static void clk_periph_enable_locked(struct clk_hw *hw)
  39. {
  40. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  41. write_enb_set(periph_clk_to_bit(gate), gate);
  42. udelay(2);
  43. if (gate->flags & TEGRA_PERIPH_WAR_1005168) {
  44. writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
  45. writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE);
  46. udelay(1);
  47. writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
  48. }
  49. }
  50. static void clk_periph_disable_locked(struct clk_hw *hw)
  51. {
  52. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  53. /*
  54. * If peripheral is in the APB bus then read the APB bus to
  55. * flush the write operation in apb bus. This will avoid the
  56. * peripheral access after disabling clock
  57. */
  58. if (gate->flags & TEGRA_PERIPH_ON_APB)
  59. tegra_read_chipid();
  60. write_enb_clr(periph_clk_to_bit(gate), gate);
  61. }
  62. static int clk_periph_enable(struct clk_hw *hw)
  63. {
  64. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  65. unsigned long flags = 0;
  66. spin_lock_irqsave(&periph_ref_lock, flags);
  67. if (!gate->enable_refcnt[gate->clk_num]++)
  68. clk_periph_enable_locked(hw);
  69. spin_unlock_irqrestore(&periph_ref_lock, flags);
  70. return 0;
  71. }
  72. static void clk_periph_disable(struct clk_hw *hw)
  73. {
  74. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  75. unsigned long flags = 0;
  76. spin_lock_irqsave(&periph_ref_lock, flags);
  77. WARN_ON(!gate->enable_refcnt[gate->clk_num]);
  78. if (--gate->enable_refcnt[gate->clk_num] == 0)
  79. clk_periph_disable_locked(hw);
  80. spin_unlock_irqrestore(&periph_ref_lock, flags);
  81. }
  82. static void clk_periph_disable_unused(struct clk_hw *hw)
  83. {
  84. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  85. unsigned long flags = 0;
  86. spin_lock_irqsave(&periph_ref_lock, flags);
  87. /*
  88. * Some clocks are duplicated and some of them are marked as critical,
  89. * like fuse and fuse_burn for example, thus the enable_refcnt will
  90. * be non-zero here if the "unused" duplicate is disabled by CCF.
  91. */
  92. if (!gate->enable_refcnt[gate->clk_num])
  93. clk_periph_disable_locked(hw);
  94. spin_unlock_irqrestore(&periph_ref_lock, flags);
  95. }
  96. const struct clk_ops tegra_clk_periph_gate_ops = {
  97. .is_enabled = clk_periph_is_enabled,
  98. .enable = clk_periph_enable,
  99. .disable = clk_periph_disable,
  100. .disable_unused = clk_periph_disable_unused,
  101. };
  102. struct clk *tegra_clk_register_periph_gate(const char *name,
  103. const char *parent_name, u8 gate_flags, void __iomem *clk_base,
  104. unsigned long flags, int clk_num, int *enable_refcnt)
  105. {
  106. struct tegra_clk_periph_gate *gate;
  107. struct clk *clk;
  108. struct clk_init_data init;
  109. const struct tegra_clk_periph_regs *pregs;
  110. pregs = get_reg_bank(clk_num);
  111. if (!pregs)
  112. return ERR_PTR(-EINVAL);
  113. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  114. if (!gate) {
  115. pr_err("%s: could not allocate periph gate clk\n", __func__);
  116. return ERR_PTR(-ENOMEM);
  117. }
  118. init.name = name;
  119. init.flags = flags;
  120. init.parent_names = parent_name ? &parent_name : NULL;
  121. init.num_parents = parent_name ? 1 : 0;
  122. init.ops = &tegra_clk_periph_gate_ops;
  123. gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC;
  124. gate->clk_base = clk_base;
  125. gate->clk_num = clk_num;
  126. gate->flags = gate_flags;
  127. gate->enable_refcnt = enable_refcnt;
  128. gate->regs = pregs;
  129. /* Data in .init is copied by clk_register(), so stack variable OK */
  130. gate->hw.init = &init;
  131. clk = clk_register(NULL, &gate->hw);
  132. if (IS_ERR(clk))
  133. kfree(gate);
  134. return clk;
  135. }