clk-sp810.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * Copyright (C) 2013 ARM Limited
  12. */
  13. #include <linux/amba/sp810.h>
  14. #include <linux/clkdev.h>
  15. #include <linux/clk-provider.h>
  16. #include <linux/err.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #define to_clk_sp810_timerclken(_hw) \
  20. container_of(_hw, struct clk_sp810_timerclken, hw)
  21. struct clk_sp810;
  22. struct clk_sp810_timerclken {
  23. struct clk_hw hw;
  24. struct clk *clk;
  25. struct clk_sp810 *sp810;
  26. int channel;
  27. };
  28. struct clk_sp810 {
  29. struct device_node *node;
  30. int refclk_index, timclk_index;
  31. void __iomem *base;
  32. spinlock_t lock;
  33. struct clk_sp810_timerclken timerclken[4];
  34. struct clk *refclk;
  35. struct clk *timclk;
  36. };
  37. static u8 clk_sp810_timerclken_get_parent(struct clk_hw *hw)
  38. {
  39. struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
  40. u32 val = readl(timerclken->sp810->base + SCCTRL);
  41. return !!(val & (1 << SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel)));
  42. }
  43. static int clk_sp810_timerclken_set_parent(struct clk_hw *hw, u8 index)
  44. {
  45. struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
  46. struct clk_sp810 *sp810 = timerclken->sp810;
  47. u32 val, shift = SCCTRL_TIMERENnSEL_SHIFT(timerclken->channel);
  48. unsigned long flags = 0;
  49. if (WARN_ON(index > 1))
  50. return -EINVAL;
  51. spin_lock_irqsave(&sp810->lock, flags);
  52. val = readl(sp810->base + SCCTRL);
  53. val &= ~(1 << shift);
  54. val |= index << shift;
  55. writel(val, sp810->base + SCCTRL);
  56. spin_unlock_irqrestore(&sp810->lock, flags);
  57. return 0;
  58. }
  59. /*
  60. * FIXME - setting the parent every time .prepare is invoked is inefficient.
  61. * This is better handled by a dedicated clock tree configuration mechanism at
  62. * init-time. Revisit this later when such a mechanism exists
  63. */
  64. static int clk_sp810_timerclken_prepare(struct clk_hw *hw)
  65. {
  66. struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
  67. struct clk_sp810 *sp810 = timerclken->sp810;
  68. struct clk *old_parent = __clk_get_parent(hw->clk);
  69. struct clk *new_parent;
  70. if (!sp810->refclk)
  71. sp810->refclk = of_clk_get(sp810->node, sp810->refclk_index);
  72. if (!sp810->timclk)
  73. sp810->timclk = of_clk_get(sp810->node, sp810->timclk_index);
  74. if (WARN_ON(IS_ERR(sp810->refclk) || IS_ERR(sp810->timclk)))
  75. return -ENOENT;
  76. /* Select fastest parent */
  77. if (clk_get_rate(sp810->refclk) > clk_get_rate(sp810->timclk))
  78. new_parent = sp810->refclk;
  79. else
  80. new_parent = sp810->timclk;
  81. /* Switch the parent if necessary */
  82. if (old_parent != new_parent) {
  83. clk_prepare(new_parent);
  84. clk_set_parent(hw->clk, new_parent);
  85. clk_unprepare(old_parent);
  86. }
  87. return 0;
  88. }
  89. static void clk_sp810_timerclken_unprepare(struct clk_hw *hw)
  90. {
  91. struct clk_sp810_timerclken *timerclken = to_clk_sp810_timerclken(hw);
  92. struct clk_sp810 *sp810 = timerclken->sp810;
  93. clk_put(sp810->timclk);
  94. clk_put(sp810->refclk);
  95. }
  96. static const struct clk_ops clk_sp810_timerclken_ops = {
  97. .prepare = clk_sp810_timerclken_prepare,
  98. .unprepare = clk_sp810_timerclken_unprepare,
  99. .get_parent = clk_sp810_timerclken_get_parent,
  100. .set_parent = clk_sp810_timerclken_set_parent,
  101. };
  102. struct clk *clk_sp810_timerclken_of_get(struct of_phandle_args *clkspec,
  103. void *data)
  104. {
  105. struct clk_sp810 *sp810 = data;
  106. if (WARN_ON(clkspec->args_count != 1 || clkspec->args[0] >
  107. ARRAY_SIZE(sp810->timerclken)))
  108. return NULL;
  109. return sp810->timerclken[clkspec->args[0]].clk;
  110. }
  111. void __init clk_sp810_of_setup(struct device_node *node)
  112. {
  113. struct clk_sp810 *sp810 = kzalloc(sizeof(*sp810), GFP_KERNEL);
  114. const char *parent_names[2];
  115. char name[12];
  116. struct clk_init_data init;
  117. int i;
  118. if (!sp810) {
  119. pr_err("Failed to allocate memory for SP810!\n");
  120. return;
  121. }
  122. sp810->refclk_index = of_property_match_string(node, "clock-names",
  123. "refclk");
  124. parent_names[0] = of_clk_get_parent_name(node, sp810->refclk_index);
  125. sp810->timclk_index = of_property_match_string(node, "clock-names",
  126. "timclk");
  127. parent_names[1] = of_clk_get_parent_name(node, sp810->timclk_index);
  128. if (parent_names[0] <= 0 || parent_names[1] <= 0) {
  129. pr_warn("Failed to obtain parent clocks for SP810!\n");
  130. return;
  131. }
  132. sp810->node = node;
  133. sp810->base = of_iomap(node, 0);
  134. spin_lock_init(&sp810->lock);
  135. init.name = name;
  136. init.ops = &clk_sp810_timerclken_ops;
  137. init.flags = CLK_IS_BASIC;
  138. init.parent_names = parent_names;
  139. init.num_parents = ARRAY_SIZE(parent_names);
  140. for (i = 0; i < ARRAY_SIZE(sp810->timerclken); i++) {
  141. snprintf(name, ARRAY_SIZE(name), "timerclken%d", i);
  142. sp810->timerclken[i].sp810 = sp810;
  143. sp810->timerclken[i].channel = i;
  144. sp810->timerclken[i].hw.init = &init;
  145. sp810->timerclken[i].clk = clk_register(NULL,
  146. &sp810->timerclken[i].hw);
  147. WARN_ON(IS_ERR(sp810->timerclken[i].clk));
  148. }
  149. of_clk_add_provider(node, clk_sp810_timerclken_of_get, sp810);
  150. }
  151. CLK_OF_DECLARE(sp810, "arm,sp810", clk_sp810_of_setup);