clock.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #ifdef CONFIG_SH_PCLK_FREQ_BOOL
  40. .rate = CONFIG_SH_PCLK_FREQ,
  41. #endif
  42. };
  43. static struct clk module_clk = {
  44. .name = "module_clk",
  45. .parent = &master_clk,
  46. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  47. };
  48. static struct clk bus_clk = {
  49. .name = "bus_clk",
  50. .parent = &master_clk,
  51. .flags = CLK_ALWAYS_ENABLED | CLK_RATE_PROPAGATES,
  52. };
  53. static struct clk cpu_clk = {
  54. .name = "cpu_clk",
  55. .parent = &master_clk,
  56. .flags = CLK_ALWAYS_ENABLED,
  57. };
  58. /*
  59. * The ordering of these clocks matters, do not change it.
  60. */
  61. static struct clk *onchip_clocks[] = {
  62. &master_clk,
  63. &module_clk,
  64. &bus_clk,
  65. &cpu_clk,
  66. };
  67. static void propagate_rate(struct clk *clk)
  68. {
  69. struct clk *clkp;
  70. list_for_each_entry(clkp, &clock_list, node) {
  71. if (likely(clkp->parent != clk))
  72. continue;
  73. if (likely(clkp->ops && clkp->ops->recalc))
  74. clkp->ops->recalc(clkp);
  75. }
  76. }
  77. int __clk_enable(struct clk *clk)
  78. {
  79. /*
  80. * See if this is the first time we're enabling the clock, some
  81. * clocks that are always enabled still require "special"
  82. * initialization. This is especially true if the clock mode
  83. * changes and the clock needs to hunt for the proper set of
  84. * divisors to use before it can effectively recalc.
  85. */
  86. if (unlikely(atomic_read(&clk->kref.refcount) == 1))
  87. if (clk->ops && clk->ops->init)
  88. clk->ops->init(clk);
  89. if (clk->flags & CLK_ALWAYS_ENABLED)
  90. return 0;
  91. if (likely(clk->ops && clk->ops->enable))
  92. clk->ops->enable(clk);
  93. kref_get(&clk->kref);
  94. return 0;
  95. }
  96. int clk_enable(struct clk *clk)
  97. {
  98. unsigned long flags;
  99. int ret;
  100. spin_lock_irqsave(&clock_lock, flags);
  101. ret = __clk_enable(clk);
  102. spin_unlock_irqrestore(&clock_lock, flags);
  103. return ret;
  104. }
  105. static void clk_kref_release(struct kref *kref)
  106. {
  107. /* Nothing to do */
  108. }
  109. void __clk_disable(struct clk *clk)
  110. {
  111. if (clk->flags & CLK_ALWAYS_ENABLED)
  112. return;
  113. kref_put(&clk->kref, clk_kref_release);
  114. }
  115. void clk_disable(struct clk *clk)
  116. {
  117. unsigned long flags;
  118. spin_lock_irqsave(&clock_lock, flags);
  119. __clk_disable(clk);
  120. spin_unlock_irqrestore(&clock_lock, flags);
  121. }
  122. int clk_register(struct clk *clk)
  123. {
  124. down(&clock_list_sem);
  125. list_add(&clk->node, &clock_list);
  126. kref_init(&clk->kref);
  127. up(&clock_list_sem);
  128. return 0;
  129. }
  130. void clk_unregister(struct clk *clk)
  131. {
  132. down(&clock_list_sem);
  133. list_del(&clk->node);
  134. up(&clock_list_sem);
  135. }
  136. inline unsigned long clk_get_rate(struct clk *clk)
  137. {
  138. return clk->rate;
  139. }
  140. int clk_set_rate(struct clk *clk, unsigned long rate)
  141. {
  142. int ret = -EOPNOTSUPP;
  143. if (likely(clk->ops && clk->ops->set_rate)) {
  144. unsigned long flags;
  145. spin_lock_irqsave(&clock_lock, flags);
  146. ret = clk->ops->set_rate(clk, rate);
  147. spin_unlock_irqrestore(&clock_lock, flags);
  148. }
  149. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  150. propagate_rate(clk);
  151. return ret;
  152. }
  153. void clk_recalc_rate(struct clk *clk)
  154. {
  155. if (likely(clk->ops && clk->ops->recalc)) {
  156. unsigned long flags;
  157. spin_lock_irqsave(&clock_lock, flags);
  158. clk->ops->recalc(clk);
  159. spin_unlock_irqrestore(&clock_lock, flags);
  160. }
  161. if (unlikely(clk->flags & CLK_RATE_PROPAGATES))
  162. propagate_rate(clk);
  163. }
  164. struct clk *clk_get(const char *id)
  165. {
  166. struct clk *p, *clk = ERR_PTR(-ENOENT);
  167. down(&clock_list_sem);
  168. list_for_each_entry(p, &clock_list, node) {
  169. if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
  170. clk = p;
  171. break;
  172. }
  173. }
  174. up(&clock_list_sem);
  175. return clk;
  176. }
  177. void clk_put(struct clk *clk)
  178. {
  179. if (clk && !IS_ERR(clk))
  180. module_put(clk->owner);
  181. }
  182. void __init __attribute__ ((weak))
  183. arch_init_clk_ops(struct clk_ops **ops, int type)
  184. {
  185. }
  186. int __init clk_init(void)
  187. {
  188. int i, ret = 0;
  189. if (unlikely(!master_clk.rate))
  190. /*
  191. * NOTE: This will break if the default divisor has been
  192. * changed.
  193. *
  194. * No one should be changing the default on us however,
  195. * expect that a sane value for CONFIG_SH_PCLK_FREQ will
  196. * be defined in the event of a different divisor.
  197. */
  198. master_clk.rate = get_timer_frequency() * 4;
  199. for (i = 0; i < ARRAY_SIZE(onchip_clocks); i++) {
  200. struct clk *clk = onchip_clocks[i];
  201. arch_init_clk_ops(&clk->ops, i);
  202. ret |= clk_register(clk);
  203. clk_enable(clk);
  204. }
  205. /* Kick the child clocks.. */
  206. propagate_rate(&master_clk);
  207. propagate_rate(&bus_clk);
  208. return ret;
  209. }
  210. int show_clocks(struct seq_file *m)
  211. {
  212. struct clk *clk;
  213. list_for_each_entry_reverse(clk, &clock_list, node) {
  214. unsigned long rate = clk_get_rate(clk);
  215. /*
  216. * Don't bother listing dummy clocks with no ancestry
  217. * that only support enable and disable ops.
  218. */
  219. if (unlikely(!rate && !clk->parent))
  220. continue;
  221. seq_printf(m, "%-12s\t: %ld.%02ldMHz\n", clk->name,
  222. rate / 1000000, (rate % 1000000) / 10000);
  223. }
  224. return 0;
  225. }
  226. EXPORT_SYMBOL_GPL(clk_register);
  227. EXPORT_SYMBOL_GPL(clk_unregister);
  228. EXPORT_SYMBOL_GPL(clk_get);
  229. EXPORT_SYMBOL_GPL(clk_put);
  230. EXPORT_SYMBOL_GPL(clk_enable);
  231. EXPORT_SYMBOL_GPL(clk_disable);
  232. EXPORT_SYMBOL_GPL(__clk_enable);
  233. EXPORT_SYMBOL_GPL(__clk_disable);
  234. EXPORT_SYMBOL_GPL(clk_get_rate);
  235. EXPORT_SYMBOL_GPL(clk_set_rate);
  236. EXPORT_SYMBOL_GPL(clk_recalc_rate);