clk-periph-gate.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-provider.h>
  17. #include <linux/slab.h>
  18. #include <linux/io.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <soc/tegra/fuse.h>
  22. #include "clk.h"
  23. static DEFINE_SPINLOCK(periph_ref_lock);
  24. /* Macros to assist peripheral gate clock */
  25. #define read_enb(gate) \
  26. readl_relaxed(gate->clk_base + (gate->regs->enb_reg))
  27. #define write_enb_set(val, gate) \
  28. writel_relaxed(val, gate->clk_base + (gate->regs->enb_set_reg))
  29. #define write_enb_clr(val, gate) \
  30. writel_relaxed(val, gate->clk_base + (gate->regs->enb_clr_reg))
  31. #define read_rst(gate) \
  32. readl_relaxed(gate->clk_base + (gate->regs->rst_reg))
  33. #define write_rst_clr(val, gate) \
  34. writel_relaxed(val, gate->clk_base + (gate->regs->rst_clr_reg))
  35. #define periph_clk_to_bit(gate) (1 << (gate->clk_num % 32))
  36. #define LVL2_CLK_GATE_OVRE 0x554
  37. /* Peripheral gate clock ops */
  38. static int clk_periph_is_enabled(struct clk_hw *hw)
  39. {
  40. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  41. int state = 1;
  42. if (!(read_enb(gate) & periph_clk_to_bit(gate)))
  43. state = 0;
  44. if (!(gate->flags & TEGRA_PERIPH_NO_RESET))
  45. if (read_rst(gate) & periph_clk_to_bit(gate))
  46. state = 0;
  47. return state;
  48. }
  49. static int clk_periph_enable(struct clk_hw *hw)
  50. {
  51. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  52. unsigned long flags = 0;
  53. spin_lock_irqsave(&periph_ref_lock, flags);
  54. gate->enable_refcnt[gate->clk_num]++;
  55. if (gate->enable_refcnt[gate->clk_num] > 1) {
  56. spin_unlock_irqrestore(&periph_ref_lock, flags);
  57. return 0;
  58. }
  59. write_enb_set(periph_clk_to_bit(gate), gate);
  60. udelay(2);
  61. if (!(gate->flags & TEGRA_PERIPH_NO_RESET) &&
  62. !(gate->flags & TEGRA_PERIPH_MANUAL_RESET)) {
  63. if (read_rst(gate) & periph_clk_to_bit(gate)) {
  64. udelay(5); /* reset propogation delay */
  65. write_rst_clr(periph_clk_to_bit(gate), gate);
  66. }
  67. }
  68. if (gate->flags & TEGRA_PERIPH_WAR_1005168) {
  69. writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
  70. writel_relaxed(BIT(22), gate->clk_base + LVL2_CLK_GATE_OVRE);
  71. udelay(1);
  72. writel_relaxed(0, gate->clk_base + LVL2_CLK_GATE_OVRE);
  73. }
  74. spin_unlock_irqrestore(&periph_ref_lock, flags);
  75. return 0;
  76. }
  77. static void clk_periph_disable(struct clk_hw *hw)
  78. {
  79. struct tegra_clk_periph_gate *gate = to_clk_periph_gate(hw);
  80. unsigned long flags = 0;
  81. spin_lock_irqsave(&periph_ref_lock, flags);
  82. gate->enable_refcnt[gate->clk_num]--;
  83. if (gate->enable_refcnt[gate->clk_num] > 0) {
  84. spin_unlock_irqrestore(&periph_ref_lock, flags);
  85. return;
  86. }
  87. /*
  88. * If peripheral is in the APB bus then read the APB bus to
  89. * flush the write operation in apb bus. This will avoid the
  90. * peripheral access after disabling clock
  91. */
  92. if (gate->flags & TEGRA_PERIPH_ON_APB)
  93. tegra_read_chipid();
  94. write_enb_clr(periph_clk_to_bit(gate), gate);
  95. spin_unlock_irqrestore(&periph_ref_lock, flags);
  96. }
  97. const struct clk_ops tegra_clk_periph_gate_ops = {
  98. .is_enabled = clk_periph_is_enabled,
  99. .enable = clk_periph_enable,
  100. .disable = clk_periph_disable,
  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. 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. }