clock.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * arch/sh/kernel/cpu/clock.c - SuperH clock framework
  3. *
  4. * Copyright (C) 2005 Paul Mundt
  5. *
  6. * This clock framework is derived from the OMAP version by:
  7. *
  8. * Copyright (C) 2004 Nokia Corporation
  9. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  10. *
  11. * This file is subject to the terms and conditions of the GNU General Public
  12. * License. See the file "COPYING" in the main directory of this archive
  13. * for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/list.h>
  19. #include <linux/kref.h>
  20. #include <linux/seq_file.h>
  21. #include <linux/err.h>
  22. #include <asm/clock.h>
  23. #include <asm/timer.h>
  24. static LIST_HEAD(clock_list);
  25. static DEFINE_SPINLOCK(clock_lock);
  26. static DECLARE_MUTEX(clock_list_sem);
  27. /*
  28. * Each subtype is expected to define the init routines for these clocks,
  29. * as each subtype (or processor family) will have these clocks at the
  30. * very least. These are all provided through the CPG, which even some of
  31. * the more quirky parts (such as ST40, SH4-202, etc.) still have.
  32. *
  33. * The processor-specific code is expected to register any additional
  34. * clock sources that are of interest.
  35. */
  36. static struct clk master_clk = {
  37. .name = "master_clk",
  38. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  39. .rate = CONFIG_SH_PCLK_FREQ,
  40. };
  41. static struct clk module_clk = {
  42. .name = "module_clk",
  43. .parent = &master_clk,
  44. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  45. };
  46. static struct clk bus_clk = {
  47. .name = "bus_clk",
  48. .parent = &master_clk,
  49. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  50. };
  51. static struct clk cpu_clk = {
  52. .name = "cpu_clk",
  53. .parent = &master_clk,
  54. .flags = CLK_ALWAYS_ENABLED,
  55. };
  56. /*
  57. * The ordering of these clocks matters, do not change it.
  58. */
  59. static struct clk *onchip_clocks[] = {
  60. &master_clk,
  61. &module_clk,
  62. &bus_clk,
  63. &cpu_clk,
  64. };
  65. static void propagate_rate(struct clk *clk)
  66. {
  67. struct clk *clkp;
  68. list_for_each_entry(clkp, &clock_list, node) {
  69. if (likely(clkp->parent != clk))
  70. continue;
  71. if (likely(clkp->ops && clkp->ops->recalc))
  72. clkp->ops->recalc(clkp);
  73. }
  74. }
  75. int __clk_enable(struct clk *clk)
  76. {
  77. /*
  78. * See if this is the first time we're enabling the clock, some
  79. * clocks that are always enabled still require "special"
  80. * initialization. This is especially true if the clock mode
  81. * changes and the clock needs to hunt for the proper set of
  82. * divisors to use before it can effectively recalc.
  83. */
  84. if (unlikely(atomic_read(&clk->kref.refcount) == 1))
  85. if (clk->ops && clk->ops->init)
  86. clk->ops->init(clk);
  87. if (clk->flags & CLK_ALWAYS_ENABLED)
  88. return 0;
  89. if (likely(clk->ops && clk->ops->enable))
  90. clk->ops->enable(clk);
  91. kref_get(&clk->kref);
  92. return 0;
  93. }
  94. int clk_enable(struct clk *clk)
  95. {
  96. unsigned long flags;
  97. int ret;
  98. spin_lock_irqsave(&clock_lock, flags);
  99. ret = __clk_enable(clk);
  100. spin_unlock_irqrestore(&clock_lock, flags);
  101. return ret;
  102. }
  103. static void clk_kref_release(struct kref *kref)
  104. {
  105. /* Nothing to do */
  106. }
  107. void __clk_disable(struct clk *clk)
  108. {
  109. if (clk->flags & CLK_ALWAYS_ENABLED)
  110. return;
  111. kref_put(&clk->kref, clk_kref_release);
  112. }
  113. void clk_disable(struct clk *clk)
  114. {
  115. unsigned long flags;
  116. spin_lock_irqsave(&clock_lock, flags);
  117. __clk_disable(clk);
  118. spin_unlock_irqrestore(&clock_lock, flags);
  119. }
  120. int clk_register(struct clk *clk)
  121. {
  122. down(&clock_list_sem);
  123. list_add(&clk->node, &clock_list);
  124. kref_init(&clk->kref);
  125. up(&clock_list_sem);
  126. return 0;
  127. }
  128. void clk_unregister(struct clk *clk)
  129. {
  130. down(&clock_list_sem);
  131. list_del(&clk->node);
  132. up(&clock_list_sem);
  133. }
  134. inline unsigned long clk_get_rate(struct clk *clk)
  135. {
  136. return clk->rate;
  137. }
  138. int clk_set_rate(struct clk *clk, unsigned long rate)
  139. {
  140. int ret = -EOPNOTSUPP;
  141. if (likely(clk->ops && clk->ops->set_rate)) {
  142. unsigned long flags;
  143. spin_lock_irqsave(&clock_lock, flags);
  144. ret = clk->ops->set_rate(clk, rate);
  145. spin_unlock_irqrestore(&clock_lock, flags);
  146. }
  147. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  148. propagate_rate(clk);
  149. return ret;
  150. }
  151. void clk_recalc_rate(struct clk *clk)
  152. {
  153. if (likely(clk->ops && clk->ops->recalc)) {
  154. unsigned long flags;
  155. spin_lock_irqsave(&clock_lock, flags);
  156. clk->ops->recalc(clk);
  157. spin_unlock_irqrestore(&clock_lock, flags);
  158. }
  159. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  160. propagate_rate(clk);
  161. }
  162. struct clk *clk_get(const char *id)
  163. {
  164. struct clk *p, *clk = ERR_PTR(-ENOENT);
  165. down(&clock_list_sem);
  166. list_for_each_entry(p, &clock_list, node) {
  167. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  168. clk = p;
  169. break;
  170. }
  171. }
  172. up(&clock_list_sem);
  173. return clk;
  174. }
  175. void clk_put(struct clk *clk)
  176. {
  177. if (clk && !IS_ERR(clk))
  178. module_put(clk->owner);
  179. }
  180. void __init __attribute__ ((weak))
  181. arch_init_clk_ops(struct clk_ops **ops, int type)
  182. {
  183. }
  184. int __init clk_init(void)
  185. {
  186. int i, ret = 0;
  187. BUG_ON(unlikely(!master_clk.rate));
  188. for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
  189. struct clk *clk = onchip_clocks[i];
  190. arch_init_clk_ops(&clk->ops, i);
  191. ret |= clk_register(clk);
  192. clk_enable(clk);
  193. }
  194. /* Kick the child clocks.. */
  195. propagate_rate(&master_clk);
  196. propagate_rate(&bus_clk);
  197. return ret;
  198. }
  199. int show_clocks(struct seq_file *m)
  200. {
  201. struct clk *clk;
  202. list_for_each_entry_reverse(clk, &clock_list, node) {
  203. unsigned long rate = clk_get_rate(clk);
  204. /*
  205. * Don't bother listing dummy clocks with no ancestry
  206. * that only support enable and disable ops.
  207. */
  208. if (unlikely(!rate && !clk->parent))
  209. continue;
  210. seq_printf(m, "%-12s\t: %ld.%02ldMHz\n", clk->name,
  211. rate / 1000000, (rate % 1000000) / 10000);
  212. }
  213. return 0;
  214. }
  215. EXPORT_SYMBOL_GPL(clk_register);
  216. EXPORT_SYMBOL_GPL(clk_unregister);
  217. EXPORT_SYMBOL_GPL(clk_get);
  218. EXPORT_SYMBOL_GPL(clk_put);
  219. EXPORT_SYMBOL_GPL(clk_enable);
  220. EXPORT_SYMBOL_GPL(clk_disable);
  221. EXPORT_SYMBOL_GPL(__clk_enable);
  222. EXPORT_SYMBOL_GPL(__clk_disable);
  223. EXPORT_SYMBOL_GPL(clk_get_rate);
  224. EXPORT_SYMBOL_GPL(clk_set_rate);
  225. EXPORT_SYMBOL_GPL(clk_recalc_rate);