clock.c 5.9 KB

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