clk-super.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/kernel.h>
  17. #include <linux/io.h>
  18. #include <linux/delay.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/clk-provider.h>
  22. #include <linux/clk.h>
  23. #include "clk.h"
  24. #define SUPER_STATE_IDLE 0
  25. #define SUPER_STATE_RUN 1
  26. #define SUPER_STATE_IRQ 2
  27. #define SUPER_STATE_FIQ 3
  28. #define SUPER_STATE_SHIFT 28
  29. #define SUPER_STATE_MASK ((BIT(SUPER_STATE_IDLE) | BIT(SUPER_STATE_RUN) | \
  30. BIT(SUPER_STATE_IRQ) | BIT(SUPER_STATE_FIQ)) \
  31. << SUPER_STATE_SHIFT)
  32. #define SUPER_LP_DIV2_BYPASS (1 << 16)
  33. #define super_state(s) (BIT(s) << SUPER_STATE_SHIFT)
  34. #define super_state_to_src_shift(m, s) ((m->width * s))
  35. #define super_state_to_src_mask(m) (((1 << m->width) - 1))
  36. static u8 clk_super_get_parent(struct clk_hw *hw)
  37. {
  38. struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
  39. u32 val, state;
  40. u8 source, shift;
  41. val = readl_relaxed(mux->reg);
  42. state = val & SUPER_STATE_MASK;
  43. BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
  44. (state != super_state(SUPER_STATE_IDLE)));
  45. shift = (state == super_state(SUPER_STATE_IDLE)) ?
  46. super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
  47. super_state_to_src_shift(mux, SUPER_STATE_RUN);
  48. source = (val >> shift) & super_state_to_src_mask(mux);
  49. /*
  50. * If LP_DIV2_BYPASS is not set and PLLX is current parent then
  51. * PLLX/2 is the input source to CCLKLP.
  52. */
  53. if ((mux->flags & TEGRA_DIVIDER_2) && !(val & SUPER_LP_DIV2_BYPASS) &&
  54. (source == mux->pllx_index))
  55. source = mux->div2_index;
  56. return source;
  57. }
  58. static int clk_super_set_parent(struct clk_hw *hw, u8 index)
  59. {
  60. struct tegra_clk_super_mux *mux = to_clk_super_mux(hw);
  61. u32 val, state;
  62. int err = 0;
  63. u8 parent_index, shift;
  64. unsigned long flags = 0;
  65. if (mux->lock)
  66. spin_lock_irqsave(mux->lock, flags);
  67. val = readl_relaxed(mux->reg);
  68. state = val & SUPER_STATE_MASK;
  69. BUG_ON((state != super_state(SUPER_STATE_RUN)) &&
  70. (state != super_state(SUPER_STATE_IDLE)));
  71. shift = (state == super_state(SUPER_STATE_IDLE)) ?
  72. super_state_to_src_shift(mux, SUPER_STATE_IDLE) :
  73. super_state_to_src_shift(mux, SUPER_STATE_RUN);
  74. /*
  75. * For LP mode super-clock switch between PLLX direct
  76. * and divided-by-2 outputs is allowed only when other
  77. * than PLLX clock source is current parent.
  78. */
  79. if ((mux->flags & TEGRA_DIVIDER_2) && ((index == mux->div2_index) ||
  80. (index == mux->pllx_index))) {
  81. parent_index = clk_super_get_parent(hw);
  82. if ((parent_index == mux->div2_index) ||
  83. (parent_index == mux->pllx_index)) {
  84. err = -EINVAL;
  85. goto out;
  86. }
  87. val ^= SUPER_LP_DIV2_BYPASS;
  88. writel_relaxed(val, mux->reg);
  89. udelay(2);
  90. if (index == mux->div2_index)
  91. index = mux->pllx_index;
  92. }
  93. val &= ~((super_state_to_src_mask(mux)) << shift);
  94. val |= (index & (super_state_to_src_mask(mux))) << shift;
  95. writel_relaxed(val, mux->reg);
  96. udelay(2);
  97. out:
  98. if (mux->lock)
  99. spin_unlock_irqrestore(mux->lock, flags);
  100. return err;
  101. }
  102. const struct clk_ops tegra_clk_super_ops = {
  103. .get_parent = clk_super_get_parent,
  104. .set_parent = clk_super_set_parent,
  105. };
  106. struct clk *tegra_clk_register_super_mux(const char *name,
  107. const char **parent_names, u8 num_parents,
  108. unsigned long flags, void __iomem *reg, u8 clk_super_flags,
  109. u8 width, u8 pllx_index, u8 div2_index, spinlock_t *lock)
  110. {
  111. struct tegra_clk_super_mux *super;
  112. struct clk *clk;
  113. struct clk_init_data init;
  114. super = kzalloc(sizeof(*super), GFP_KERNEL);
  115. if (!super) {
  116. pr_err("%s: could not allocate super clk\n", __func__);
  117. return ERR_PTR(-ENOMEM);
  118. }
  119. init.name = name;
  120. init.ops = &tegra_clk_super_ops;
  121. init.flags = flags;
  122. init.parent_names = parent_names;
  123. init.num_parents = num_parents;
  124. super->reg = reg;
  125. super->pllx_index = pllx_index;
  126. super->div2_index = div2_index;
  127. super->lock = lock;
  128. super->width = width;
  129. super->flags = clk_super_flags;
  130. /* Data in .init is copied by clk_register(), so stack variable OK */
  131. super->hw.init = &init;
  132. clk = clk_register(NULL, &super->hw);
  133. if (IS_ERR(clk))
  134. kfree(super);
  135. return clk;
  136. }