clk-periph-gate.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/slab.h>
  19. #include <linux/io.h>
  20. #include <linux/delay.h>
  21. #include <linux/err.h>
  22. #include <linux/tegra-soc.h>
  23. #include "clk.h"
  24. static DEFINE_SPINLOCK(periph_ref_lock);
  25. /* Macros to assist peripheral gate clock */
  26. #define read_enb(gate) \
  27. readl_relaxed(gate->clk_base + (gate->regs->enb_reg))
  28. #define write_enb_set(val, gate) \
  29. writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg))
  30. #define write_enb_clr(val, gate) \
  31. writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg))
  32. #define read_rst(gate) \
  33. readl_relaxed(gate->clk_base + (gate->regs->rst_reg))
  34. #define write_rst_set(val, gate) \
  35. writel_relaxed(val, gate->clk_base + (gate->regs->rst_set_reg))
  36. #define write_rst_clr(val, gate) \
  37. writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
  38. #define periph_clk_to_bit(periph) (1 << (gate->clk_num % 32))
  39. /* Peripheral gate clock ops */
  40. static int clk_periph_is_enabled(struct clk_hw *hw)
  41. {
  42. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  43. int state = 1;
  44. if (!(read_enb(gate) & periph_clk_to_bit(gate)))
  45. state = 0;
  46. if (!(gate->flags & TEGRA_PERIPH_NO_RESET))
  47. if (read_rst(gate) & periph_clk_to_bit(gate))
  48. state = 0;
  49. return state;
  50. }
  51. static int clk_periph_enable(struct clk_hw *hw)
  52. {
  53. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  54. unsigned long flags = 0;
  55. spin_lock_irqsave(&periph_ref_lock, flags);
  56. gate->enable_refcnt[gate->clk_num]++;
  57. if (gate->enable_refcnt[gate->clk_num] > 1) {
  58. spin_unlock_irqrestore(&periph_ref_lock, flags);
  59. return 0;
  60. }
  61. write_enb_set(periph_clk_to_bit(gate), gate);
  62. udelay(2);
  63. if (!(gate->flags & TEGRA_PERIPH_NO_RESET) &&
  64. !(gate->flags & TEGRA_PERIPH_MANUAL_RESET)) {
  65. if (read_rst(gate) & periph_clk_to_bit(gate)) {
  66. udelay(5); /* reset propogation delay */
  67. write_rst_clr(periph_clk_to_bit(gate), gate);
  68. }
  69. }
  70. spin_unlock_irqrestore(&periph_ref_lock, flags);
  71. return 0;
  72. }
  73. static void clk_periph_disable(struct clk_hw *hw)
  74. {
  75. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  76. unsigned long flags = 0;
  77. spin_lock_irqsave(&periph_ref_lock, flags);
  78. gate->enable_refcnt[gate->clk_num]--;
  79. if (gate->enable_refcnt[gate->clk_num] > 0) {
  80. spin_unlock_irqrestore(&periph_ref_lock, flags);
  81. return;
  82. }
  83. /*
  84. * If peripheral is in the APB bus then read the APB bus to
  85. * flush the write operation in apb bus. This will avoid the
  86. * peripheral access after disabling clock
  87. */
  88. if (gate->flags & TEGRA_PERIPH_ON_APB)
  89. tegra_read_chipid();
  90. write_enb_clr(periph_clk_to_bit(gate), gate);
  91. spin_unlock_irqrestore(&periph_ref_lock, flags);
  92. }
  93. void tegra_periph_reset(struct tegra_clk_periph_gate *gate, bool assert)
  94. {
  95. if (gate->flags & TEGRA_PERIPH_NO_RESET)
  96. return;
  97. if (assert) {
  98. /*
  99. * If peripheral is in the APB bus then read the APB bus to
  100. * flush the write operation in apb bus. This will avoid the
  101. * peripheral access after disabling clock
  102. */
  103. if (gate->flags & TEGRA_PERIPH_ON_APB)
  104. tegra_read_chipid();
  105. write_rst_set(periph_clk_to_bit(gate), gate);
  106. } else {
  107. write_rst_clr(periph_clk_to_bit(gate), gate);
  108. }
  109. }
  110. const struct clk_ops tegra_clk_periph_gate_ops = {
  111. .is_enabled = clk_periph_is_enabled,
  112. .enable = clk_periph_enable,
  113. .disable = clk_periph_disable,
  114. };
  115. struct clk *tegra_clk_register_periph_gate(const char *name,
  116. const char *parent_name, u8 gate_flags, void __iomem *clk_base,
  117. unsigned long flags, int clk_num,
  118. struct tegra_clk_periph_regs *pregs, int *enable_refcnt)
  119. {
  120. struct tegra_clk_periph_gate *gate;
  121. struct clk *clk;
  122. struct clk_init_data init;
  123. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  124. if (!gate) {
  125. pr_err("%s: could not allocate periph gate clk\n", __func__);
  126. return ERR_PTR(-ENOMEM);
  127. }
  128. init.name = name;
  129. init.flags = flags;
  130. init.parent_names = parent_name ? &parent_name : NULL;
  131. init.num_parents = parent_name ? 1 : 0;
  132. init.ops = &tegra_clk_periph_gate_ops;
  133. gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC;
  134. gate->clk_base = clk_base;
  135. gate->clk_num = clk_num;
  136. gate->flags = gate_flags;
  137. gate->enable_refcnt = enable_refcnt;
  138. gate->regs = pregs;
  139. /* Data in .init is copied by clk_register(), so stack variable OK */
  140. gate->hw.init = &init;
  141. clk = clk_register(NULL, &gate->hw);
  142. if (IS_ERR(clk))
  143. kfree(gate);
  144. return clk;
  145. }