clk-prcc.c 3.6 KB

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