clock.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Clock management for AT32AP CPUs
  3. *
  4. * Copyright (C) 2006 Atmel Corporation
  5. *
  6. * Based on arch/arm/mach-at91/clock.c
  7. * Copyright (C) 2005 David Brownell
  8. * Copyright (C) 2005 Ivan Kokshaysky
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/device.h>
  17. #include <linux/string.h>
  18. #include <linux/list.h>
  19. #include <mach/chip.h>
  20. #include "clock.h"
  21. /* at32 clock list */
  22. static LIST_HEAD(at32_clock_list);
  23. static DEFINE_SPINLOCK(clk_lock);
  24. static DEFINE_SPINLOCK(clk_list_lock);
  25. void at32_clk_register(struct clk *clk)
  26. {
  27. spin_lock(&clk_list_lock);
  28. /* add the new item to the end of the list */
  29. list_add_tail(&clk->list, &at32_clock_list);
  30. spin_unlock(&clk_list_lock);
  31. }
  32. static struct clk *__clk_get(struct device *dev, const char *id)
  33. {
  34. struct clk *clk;
  35. list_for_each_entry(clk, &at32_clock_list, list) {
  36. if (clk->dev == dev && strcmp(id, clk->name) == 0) {
  37. return clk;
  38. }
  39. }
  40. return ERR_PTR(-ENOENT);
  41. }
  42. struct clk *clk_get(struct device *dev, const char *id)
  43. {
  44. struct clk *clk;
  45. spin_lock(&clk_list_lock);
  46. clk = __clk_get(dev, id);
  47. spin_unlock(&clk_list_lock);
  48. return clk;
  49. }
  50. EXPORT_SYMBOL(clk_get);
  51. void clk_put(struct clk *clk)
  52. {
  53. /* clocks are static for now, we can't free them */
  54. }
  55. EXPORT_SYMBOL(clk_put);
  56. static void __clk_enable(struct clk *clk)
  57. {
  58. if (clk->parent)
  59. __clk_enable(clk->parent);
  60. if (clk->users++ == 0 && clk->mode)
  61. clk->mode(clk, 1);
  62. }
  63. int clk_enable(struct clk *clk)
  64. {
  65. unsigned long flags;
  66. spin_lock_irqsave(&clk_lock, flags);
  67. __clk_enable(clk);
  68. spin_unlock_irqrestore(&clk_lock, flags);
  69. return 0;
  70. }
  71. EXPORT_SYMBOL(clk_enable);
  72. static void __clk_disable(struct clk *clk)
  73. {
  74. if (clk->users == 0) {
  75. printk(KERN_ERR "%s: mismatched disable\n", clk->name);
  76. WARN_ON(1);
  77. return;
  78. }
  79. if (--clk->users == 0 && clk->mode)
  80. clk->mode(clk, 0);
  81. if (clk->parent)
  82. __clk_disable(clk->parent);
  83. }
  84. void clk_disable(struct clk *clk)
  85. {
  86. unsigned long flags;
  87. spin_lock_irqsave(&clk_lock, flags);
  88. __clk_disable(clk);
  89. spin_unlock_irqrestore(&clk_lock, flags);
  90. }
  91. EXPORT_SYMBOL(clk_disable);
  92. unsigned long clk_get_rate(struct clk *clk)
  93. {
  94. unsigned long flags;
  95. unsigned long rate;
  96. spin_lock_irqsave(&clk_lock, flags);
  97. rate = clk->get_rate(clk);
  98. spin_unlock_irqrestore(&clk_lock, flags);
  99. return rate;
  100. }
  101. EXPORT_SYMBOL(clk_get_rate);
  102. long clk_round_rate(struct clk *clk, unsigned long rate)
  103. {
  104. unsigned long flags, actual_rate;
  105. if (!clk->set_rate)
  106. return -ENOSYS;
  107. spin_lock_irqsave(&clk_lock, flags);
  108. actual_rate = clk->set_rate(clk, rate, 0);
  109. spin_unlock_irqrestore(&clk_lock, flags);
  110. return actual_rate;
  111. }
  112. EXPORT_SYMBOL(clk_round_rate);
  113. int clk_set_rate(struct clk *clk, unsigned long rate)
  114. {
  115. unsigned long flags;
  116. long ret;
  117. if (!clk->set_rate)
  118. return -ENOSYS;
  119. spin_lock_irqsave(&clk_lock, flags);
  120. ret = clk->set_rate(clk, rate, 1);
  121. spin_unlock_irqrestore(&clk_lock, flags);
  122. return (ret < 0) ? ret : 0;
  123. }
  124. EXPORT_SYMBOL(clk_set_rate);
  125. int clk_set_parent(struct clk *clk, struct clk *parent)
  126. {
  127. unsigned long flags;
  128. int ret;
  129. if (!clk->set_parent)
  130. return -ENOSYS;
  131. spin_lock_irqsave(&clk_lock, flags);
  132. ret = clk->set_parent(clk, parent);
  133. spin_unlock_irqrestore(&clk_lock, flags);
  134. return ret;
  135. }
  136. EXPORT_SYMBOL(clk_set_parent);
  137. struct clk *clk_get_parent(struct clk *clk)
  138. {
  139. return clk->parent;
  140. }
  141. EXPORT_SYMBOL(clk_get_parent);
  142. #ifdef CONFIG_DEBUG_FS
  143. /* /sys/kernel/debug/at32ap_clk */
  144. #include <linux/io.h>
  145. #include <linux/debugfs.h>
  146. #include <linux/seq_file.h>
  147. #include "pm.h"
  148. #define NEST_DELTA 2
  149. #define NEST_MAX 6
  150. struct clkinf {
  151. struct seq_file *s;
  152. unsigned nest;
  153. };
  154. static void
  155. dump_clock(struct clk *parent, struct clkinf *r)
  156. {
  157. unsigned nest = r->nest;
  158. char buf[16 + NEST_MAX];
  159. struct clk *clk;
  160. unsigned i;
  161. /* skip clocks coupled to devices that aren't registered */
  162. if (parent->dev && !dev_name(parent->dev) && !parent->users)
  163. return;
  164. /* <nest spaces> name <pad to end> */
  165. memset(buf, ' ', sizeof(buf) - 1);
  166. buf[sizeof(buf) - 1] = 0;
  167. i = strlen(parent->name);
  168. memcpy(buf + nest, parent->name,
  169. min(i, (unsigned)(sizeof(buf) - 1 - nest)));
  170. seq_printf(r->s, "%s%c users=%2d %-3s %9ld Hz",
  171. buf, parent->set_parent ? '*' : ' ',
  172. parent->users,
  173. parent->users ? "on" : "off", /* NOTE: not-paranoid!! */
  174. clk_get_rate(parent));
  175. if (parent->dev)
  176. seq_printf(r->s, ", for %s", dev_name(parent->dev));
  177. seq_printf(r->s, "\n");
  178. /* cost of this scan is small, but not linear... */
  179. r->nest = nest + NEST_DELTA;
  180. list_for_each_entry(clk, &at32_clock_list, list) {
  181. if (clk->parent == parent)
  182. dump_clock(clk, r);
  183. }
  184. r->nest = nest;
  185. }
  186. static int clk_show(struct seq_file *s, void *unused)
  187. {
  188. struct clkinf r;
  189. int i;
  190. struct clk *clk;
  191. /* show all the power manager registers */
  192. seq_printf(s, "MCCTRL = %8x\n", pm_readl(MCCTRL));
  193. seq_printf(s, "CKSEL = %8x\n", pm_readl(CKSEL));
  194. seq_printf(s, "CPUMASK = %8x\n", pm_readl(CPU_MASK));
  195. seq_printf(s, "HSBMASK = %8x\n", pm_readl(HSB_MASK));
  196. seq_printf(s, "PBAMASK = %8x\n", pm_readl(PBA_MASK));
  197. seq_printf(s, "PBBMASK = %8x\n", pm_readl(PBB_MASK));
  198. seq_printf(s, "PLL0 = %8x\n", pm_readl(PLL0));
  199. seq_printf(s, "PLL1 = %8x\n", pm_readl(PLL1));
  200. seq_printf(s, "IMR = %8x\n", pm_readl(IMR));
  201. for (i = 0; i < 8; i++) {
  202. if (i == 5)
  203. continue;
  204. seq_printf(s, "GCCTRL%d = %8x\n", i, pm_readl(GCCTRL(i)));
  205. }
  206. seq_printf(s, "\n");
  207. r.s = s;
  208. r.nest = 0;
  209. /* protected from changes on the list while dumping */
  210. spin_lock(&clk_list_lock);
  211. /* show clock tree as derived from the three oscillators */
  212. clk = __clk_get(NULL, "osc32k");
  213. dump_clock(clk, &r);
  214. clk_put(clk);
  215. clk = __clk_get(NULL, "osc0");
  216. dump_clock(clk, &r);
  217. clk_put(clk);
  218. clk = __clk_get(NULL, "osc1");
  219. dump_clock(clk, &r);
  220. clk_put(clk);
  221. spin_unlock(&clk_list_lock);
  222. return 0;
  223. }
  224. static int clk_open(struct inode *inode, struct file *file)
  225. {
  226. return single_open(file, clk_show, NULL);
  227. }
  228. static const struct file_operations clk_operations = {
  229. .open = clk_open,
  230. .read = seq_read,
  231. .llseek = seq_lseek,
  232. .release = single_release,
  233. };
  234. static int __init clk_debugfs_init(void)
  235. {
  236. (void) debugfs_create_file("at32ap_clk", S_IFREG | S_IRUGO,
  237. NULL, NULL, &clk_operations);
  238. return 0;
  239. }
  240. postcore_initcall(clk_debugfs_init);
  241. #endif