clk-prcc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 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 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. 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(struct clk_prcc), GFP_KERNEL);
  89. if (!clk) {
  90. pr_err("clk_prcc: %s could not allocate clk\n", __func__);
  91. return ERR_PTR(-ENOMEM);
  92. }
  93. clk->base = ioremap(phy_base, SZ_4K);
  94. if (!clk->base)
  95. goto free_clk;
  96. clk->cg_sel = cg_sel;
  97. clk->is_enabled = 1;
  98. clk_prcc_init.name = name;
  99. clk_prcc_init.ops = clk_prcc_ops;
  100. clk_prcc_init.flags = flags;
  101. clk_prcc_init.parent_names = (parent_name ? &parent_name : NULL);
  102. clk_prcc_init.num_parents = (parent_name ? 1 : 0);
  103. clk->hw.init = &clk_prcc_init;
  104. clk_reg = clk_register(NULL, &clk->hw);
  105. if (IS_ERR_OR_NULL(clk_reg))
  106. goto unmap_clk;
  107. return clk_reg;
  108. unmap_clk:
  109. iounmap(clk->base);
  110. free_clk:
  111. kfree(clk);
  112. pr_err("clk_prcc: %s failed to register clk\n", __func__);
  113. return ERR_PTR(-ENOMEM);
  114. }
  115. struct clk *clk_reg_prcc_pclk(const char *name,
  116. const char *parent_name,
  117. resource_size_t phy_base,
  118. u32 cg_sel,
  119. unsigned long flags)
  120. {
  121. return clk_reg_prcc(name, parent_name, phy_base, cg_sel, flags,
  122. &clk_prcc_pclk_ops);
  123. }
  124. struct clk *clk_reg_prcc_kclk(const char *name,
  125. const char *parent_name,
  126. resource_size_t phy_base,
  127. u32 cg_sel,
  128. unsigned long flags)
  129. {
  130. return clk_reg_prcc(name, parent_name, phy_base, cg_sel, flags,
  131. &clk_prcc_kclk_ops);
  132. }