clk-prcc.c 3.6 KB

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