clk-gate.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
  4. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  5. * Copyright 2019 NXP
  6. *
  7. * Gated clock implementation
  8. */
  9. #include <common.h>
  10. #include <asm/io.h>
  11. #include <malloc.h>
  12. #include <clk-uclass.h>
  13. #include <dm/device.h>
  14. #include <dm/devres.h>
  15. #include <linux/bitops.h>
  16. #include <linux/clk-provider.h>
  17. #include <clk.h>
  18. #include "clk.h"
  19. #include <linux/err.h>
  20. #define UBOOT_DM_CLK_GATE "clk_gate"
  21. /**
  22. * DOC: basic gatable clock which can gate and ungate it's output
  23. *
  24. * Traits of this clock:
  25. * prepare - clk_(un)prepare only ensures parent is (un)prepared
  26. * enable - clk_enable and clk_disable are functional & control gating
  27. * rate - inherits rate from parent. No clk_set_rate support
  28. * parent - fixed parent. No clk_set_parent support
  29. */
  30. /*
  31. * It works on following logic:
  32. *
  33. * For enabling clock, enable = 1
  34. * set2dis = 1 -> clear bit -> set = 0
  35. * set2dis = 0 -> set bit -> set = 1
  36. *
  37. * For disabling clock, enable = 0
  38. * set2dis = 1 -> set bit -> set = 1
  39. * set2dis = 0 -> clear bit -> set = 0
  40. *
  41. * So, result is always: enable xor set2dis.
  42. */
  43. static void clk_gate_endisable(struct clk *clk, int enable)
  44. {
  45. struct clk_gate *gate = to_clk_gate(clk);
  46. int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
  47. u32 reg;
  48. set ^= enable;
  49. if (gate->flags & CLK_GATE_HIWORD_MASK) {
  50. reg = BIT(gate->bit_idx + 16);
  51. if (set)
  52. reg |= BIT(gate->bit_idx);
  53. } else {
  54. #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
  55. reg = gate->io_gate_val;
  56. #else
  57. reg = readl(gate->reg);
  58. #endif
  59. if (set)
  60. reg |= BIT(gate->bit_idx);
  61. else
  62. reg &= ~BIT(gate->bit_idx);
  63. }
  64. writel(reg, gate->reg);
  65. }
  66. static int clk_gate_enable(struct clk *clk)
  67. {
  68. clk_gate_endisable(clk, 1);
  69. return 0;
  70. }
  71. static int clk_gate_disable(struct clk *clk)
  72. {
  73. clk_gate_endisable(clk, 0);
  74. return 0;
  75. }
  76. int clk_gate_is_enabled(struct clk *clk)
  77. {
  78. struct clk_gate *gate = to_clk_gate(clk);
  79. u32 reg;
  80. #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
  81. reg = gate->io_gate_val;
  82. #else
  83. reg = readl(gate->reg);
  84. #endif
  85. /* if a set bit disables this clk, flip it before masking */
  86. if (gate->flags & CLK_GATE_SET_TO_DISABLE)
  87. reg ^= BIT(gate->bit_idx);
  88. reg &= BIT(gate->bit_idx);
  89. return reg ? 1 : 0;
  90. }
  91. const struct clk_ops clk_gate_ops = {
  92. .enable = clk_gate_enable,
  93. .disable = clk_gate_disable,
  94. .get_rate = clk_generic_get_rate,
  95. };
  96. struct clk *clk_register_gate(struct device *dev, const char *name,
  97. const char *parent_name, unsigned long flags,
  98. void __iomem *reg, u8 bit_idx,
  99. u8 clk_gate_flags, spinlock_t *lock)
  100. {
  101. struct clk_gate *gate;
  102. struct clk *clk;
  103. int ret;
  104. if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
  105. if (bit_idx > 15) {
  106. pr_err("gate bit exceeds LOWORD field\n");
  107. return ERR_PTR(-EINVAL);
  108. }
  109. }
  110. /* allocate the gate */
  111. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  112. if (!gate)
  113. return ERR_PTR(-ENOMEM);
  114. /* struct clk_gate assignments */
  115. gate->reg = reg;
  116. gate->bit_idx = bit_idx;
  117. gate->flags = clk_gate_flags;
  118. #if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
  119. gate->io_gate_val = *(u32 *)reg;
  120. #endif
  121. clk = &gate->clk;
  122. clk->flags = flags;
  123. ret = clk_register(clk, UBOOT_DM_CLK_GATE, name, parent_name);
  124. if (ret) {
  125. kfree(gate);
  126. return ERR_PTR(ret);
  127. }
  128. return clk;
  129. }
  130. U_BOOT_DRIVER(clk_gate) = {
  131. .name = UBOOT_DM_CLK_GATE,
  132. .id = UCLASS_CLK,
  133. .ops = &clk_gate_ops,
  134. .flags = DM_FLAG_PRE_RELOC,
  135. };