clock.c 6.4 KB

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