clk-periph-gate.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <soc/tegra/fuse.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_clr(val, gate) \
  35. writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
  36. #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32))
  37. #define LVL2_CLK_GATE_OVRE 0x554
  38. /* Peripheral gate clock ops */
  39. static int clk_periph_is_enabled(struct clk_hw *hw)
  40. {
  41. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  42. int state = 1;
  43. if (!(read_enb(gate) & periph_clk_to_bit(gate)))
  44. state = 0;
  45. if (!(gate->flags & TEGRA_PERIPH_NO_RESET))
  46. if (read_rst(gate) & periph_clk_to_bit(gate))
  47. state = 0;
  48. return state;
  49. }
  50. static int clk_periph_enable(struct clk_hw *hw)
  51. {
  52. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  53. unsigned long flags = 0;
  54. spin_lock_irqsave(&periph_ref_lock, flags);
  55. gate->enable_refcnt[gate->clk_num]++;
  56. if (gate->enable_refcnt[gate->clk_num] > 1) {
  57. spin_unlock_irqrestore(&periph_ref_lock, flags);
  58. return 0;
  59. }
  60. write_enb_set(periph_clk_to_bit(gate), gate);
  61. udelay(2);
  62. if (!(gate->flags & TEGRA_PERIPH_NO_RESET) &&
  63. !(gate->flags & TEGRA_PERIPH_MANUAL_RESET)) {
  64. if (read_rst(gate) & periph_clk_to_bit(gate)) {
  65. udelay(5); /* reset propogation delay */
  66. write_rst_clr(periph_clk_to_bit(gate), gate);
  67. }
  68. }
  69. if (gate->flags & TEGRA_PERIPH_WAR_1005168) {
  70. writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
  71. writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE);
  72. udelay(1);
  73. writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
  74. }
  75. spin_unlock_irqrestore(&periph_ref_lock, flags);
  76. return 0;
  77. }
  78. static void clk_periph_disable(struct clk_hw *hw)
  79. {
  80. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  81. unsigned long flags = 0;
  82. spin_lock_irqsave(&periph_ref_lock, flags);
  83. gate->enable_refcnt[gate->clk_num]--;
  84. if (gate->enable_refcnt[gate->clk_num] > 0) {
  85. spin_unlock_irqrestore(&periph_ref_lock, flags);
  86. return;
  87. }
  88. /*
  89. * If peripheral is in the APB bus then read the APB bus to
  90. * flush the write operation in apb bus. This will avoid the
  91. * peripheral access after disabling clock
  92. */
  93. if (gate->flags & TEGRA_PERIPH_ON_APB)
  94. tegra_read_chipid();
  95. write_enb_clr(periph_clk_to_bit(gate), gate);
  96. spin_unlock_irqrestore(&periph_ref_lock, flags);
  97. }
  98. const struct clk_ops tegra_clk_periph_gate_ops = {
  99. .is_enabled = clk_periph_is_enabled,
  100. .enable = clk_periph_enable,
  101. .disable = clk_periph_disable,
  102. };
  103. struct clk *tegra_clk_register_periph_gate(const char *name,
  104. const char *parent_name, u8 gate_flags, void __iomem *clk_base,
  105. unsigned long flags, int clk_num, int *enable_refcnt)
  106. {
  107. struct tegra_clk_periph_gate *gate;
  108. struct clk *clk;
  109. struct clk_init_data init;
  110. struct tegra_clk_periph_regs *pregs;
  111. pregs = get_reg_bank(clk_num);
  112. if (!pregs)
  113. return ERR_PTR(-EINVAL);
  114. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  115. if (!gate) {
  116. pr_err("%s: could not allocate periph gate clk\n", __func__);
  117. return ERR_PTR(-ENOMEM);
  118. }
  119. init.name = name;
  120. init.flags = flags;
  121. init.parent_names = parent_name ? &parent_name : NULL;
  122. init.num_parents = parent_name ? 1 : 0;
  123. init.ops = &tegra_clk_periph_gate_ops;
  124. gate->magic = TEGRA_CLK_PERIPH_GATE_MAGIC;
  125. gate->clk_base = clk_base;
  126. gate->clk_num = clk_num;
  127. gate->flags = gate_flags;
  128. gate->enable_refcnt = enable_refcnt;
  129. gate->regs = pregs;
  130. /* Data in .init is copied by clk_register(), so stack variable OK */
  131. gate->hw.init = &init;
  132. clk = clk_register(NULL, &gate->hw);
  133. if (IS_ERR(clk))
  134. kfree(gate);
  135. return clk;
  136. }