clk-prcc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * PRCC clock implementation for ux500 platform.
  3. *
  4. * Copyright (C) 2012 ST-Ericsson SA
  5. * Author: Ulf Hansson <ulf.hansson@linaro.org>
  6. *
  7. * License terms: GNU General Public License (GPL) version 2
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/slab.h>
  11. #include <linux/io.h>
  12. #include <linux/err.h>
  13. #include <linux/types.h>
  14. #include "clk.h"
  15. #define PRCC_PCKEN 0x000
  16. #define PRCC_PCKDIS 0x004
  17. #define PRCC_KCKEN 0x008
  18. #define PRCC_KCKDIS 0x00C
  19. #define PRCC_PCKSR 0x010
  20. #define PRCC_KCKSR 0x014
  21. #define to_clk_prcc(_hw) container_of(_hw, struct clk_prcc, hw)
  22. struct clk_prcc {
  23. struct clk_hw hw;
  24. void __iomem *base;
  25. u32 cg_sel;
  26. int is_enabled;
  27. };
  28. /* PRCC clock operations. */
  29. static int clk_prcc_pclk_enable(struct clk_hw *hw)
  30. {
  31. struct clk_prcc *clk = to_clk_prcc(hw);
  32. writel(clk->cg_sel, (clk->base + PRCC_PCKEN));
  33. while (!(readl(clk->base + PRCC_PCKSR) & clk->cg_sel))
  34. cpu_relax();
  35. clk->is_enabled = 1;
  36. return 0;
  37. }
  38. static void clk_prcc_pclk_disable(struct clk_hw *hw)
  39. {
  40. struct clk_prcc *clk = to_clk_prcc(hw);
  41. writel(clk->cg_sel, (clk->base + PRCC_PCKDIS));
  42. clk->is_enabled = 0;
  43. }
  44. static int clk_prcc_kclk_enable(struct clk_hw *hw)
  45. {
  46. struct clk_prcc *clk = to_clk_prcc(hw);
  47. writel(clk->cg_sel, (clk->base + PRCC_KCKEN));
  48. while (!(readl(clk->base + PRCC_KCKSR) & clk->cg_sel))
  49. cpu_relax();
  50. clk->is_enabled = 1;
  51. return 0;
  52. }
  53. static void clk_prcc_kclk_disable(struct clk_hw *hw)
  54. {
  55. struct clk_prcc *clk = to_clk_prcc(hw);
  56. writel(clk->cg_sel, (clk->base + PRCC_KCKDIS));
  57. clk->is_enabled = 0;
  58. }
  59. static int clk_prcc_is_enabled(struct clk_hw *hw)
  60. {
  61. struct clk_prcc *clk = to_clk_prcc(hw);
  62. return clk->is_enabled;
  63. }
  64. static const struct clk_ops clk_prcc_pclk_ops = {
  65. .enable = clk_prcc_pclk_enable,
  66. .disable = clk_prcc_pclk_disable,
  67. .is_enabled = clk_prcc_is_enabled,
  68. };
  69. static const struct clk_ops clk_prcc_kclk_ops = {
  70. .enable = clk_prcc_kclk_enable,
  71. .disable = clk_prcc_kclk_disable,
  72. .is_enabled = clk_prcc_is_enabled,
  73. };
  74. static struct clk *clk_reg_prcc(const char *name,
  75. const char *parent_name,
  76. resource_size_t phy_base,
  77. u32 cg_sel,
  78. unsigned long flags,
  79. const struct clk_ops *clk_prcc_ops)
  80. {
  81. struct clk_prcc *clk;
  82. struct clk_init_data clk_prcc_init;
  83. struct clk *clk_reg;
  84. if (!name) {
  85. pr_err("clk_prcc: %s invalid arguments passed\n", __func__);
  86. return ERR_PTR(-EINVAL);
  87. }
  88. clk = kzalloc(sizeof(*clk), GFP_KERNEL);
  89. if (!clk)
  90. return ERR_PTR(-ENOMEM);
  91. clk->base = ioremap(phy_base, SZ_4K);
  92. if (!clk->base)
  93. goto free_clk;
  94. clk->cg_sel = cg_sel;
  95. clk->is_enabled = 1;
  96. clk_prcc_init.name = name;
  97. clk_prcc_init.ops = clk_prcc_ops;
  98. clk_prcc_init.flags = flags;
  99. clk_prcc_init.parent_names = (parent_name ? &parent_name : NULL);
  100. clk_prcc_init.num_parents = (parent_name ? 1 : 0);
  101. clk->hw.init = &clk_prcc_init;
  102. clk_reg = clk_register(NULL, &clk->hw);
  103. if (IS_ERR_OR_NULL(clk_reg))
  104. goto unmap_clk;
  105. return clk_reg;
  106. unmap_clk:
  107. iounmap(clk->base);
  108. free_clk:
  109. kfree(clk);
  110. pr_err("clk_prcc: %s failed to register clk\n", __func__);
  111. return ERR_PTR(-ENOMEM);
  112. }
  113. struct clk *clk_reg_prcc_pclk(const char *name,
  114. const char *parent_name,
  115. resource_size_t phy_base,
  116. u32 cg_sel,
  117. unsigned long flags)
  118. {
  119. return clk_reg_prcc(name, parent_name, phy_base, cg_sel, flags,
  120. &clk_prcc_pclk_ops);
  121. }
  122. struct clk *clk_reg_prcc_kclk(const char *name,
  123. const char *parent_name,
  124. resource_size_t phy_base,
  125. u32 cg_sel,
  126. unsigned long flags)
  127. {
  128. return clk_reg_prcc(name, parent_name, phy_base, cg_sel, flags,
  129. &clk_prcc_kclk_ops);
  130. }