clock.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Clock and PLL control for DaVinci devices
  3. *
  4. * Copyright (C) 2006-2007 Texas Instruments.
  5. * Copyright (C) 2008-2009 Deep Root Systems, LLC
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/mutex.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/io.h>
  21. #include <mach/hardware.h>
  22. #include <mach/psc.h>
  23. #include <mach/cputype.h>
  24. #include "clock.h"
  25. static LIST_HEAD(clocks);
  26. static DEFINE_MUTEX(clocks_mutex);
  27. static DEFINE_SPINLOCK(clockfw_lock);
  28. static unsigned psc_domain(struct clk *clk)
  29. {
  30. return (clk->flags & PSC_DSP)
  31. ? DAVINCI_GPSC_DSPDOMAIN
  32. : DAVINCI_GPSC_ARMDOMAIN;
  33. }
  34. static void __clk_enable(struct clk *clk)
  35. {
  36. if (clk->parent)
  37. __clk_enable(clk->parent);
  38. if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
  39. davinci_psc_config(psc_domain(clk), clk->psc_ctlr,
  40. clk->lpsc, 1);
  41. }
  42. static void __clk_disable(struct clk *clk)
  43. {
  44. if (WARN_ON(clk->usecount == 0))
  45. return;
  46. if (--clk->usecount == 0 && !(clk->flags & CLK_PLL))
  47. davinci_psc_config(psc_domain(clk), clk->psc_ctlr,
  48. clk->lpsc, 0);
  49. if (clk->parent)
  50. __clk_disable(clk->parent);
  51. }
  52. int clk_enable(struct clk *clk)
  53. {
  54. unsigned long flags;
  55. if (clk == NULL || IS_ERR(clk))
  56. return -EINVAL;
  57. spin_lock_irqsave(&clockfw_lock, flags);
  58. __clk_enable(clk);
  59. spin_unlock_irqrestore(&clockfw_lock, flags);
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(clk_enable);
  63. void clk_disable(struct clk *clk)
  64. {
  65. unsigned long flags;
  66. if (clk == NULL || IS_ERR(clk))
  67. return;
  68. spin_lock_irqsave(&clockfw_lock, flags);
  69. __clk_disable(clk);
  70. spin_unlock_irqrestore(&clockfw_lock, flags);
  71. }
  72. EXPORT_SYMBOL(clk_disable);
  73. unsigned long clk_get_rate(struct clk *clk)
  74. {
  75. if (clk == NULL || IS_ERR(clk))
  76. return -EINVAL;
  77. return clk->rate;
  78. }
  79. EXPORT_SYMBOL(clk_get_rate);
  80. long clk_round_rate(struct clk *clk, unsigned long rate)
  81. {
  82. if (clk == NULL || IS_ERR(clk))
  83. return -EINVAL;
  84. return clk->rate;
  85. }
  86. EXPORT_SYMBOL(clk_round_rate);
  87. int clk_set_rate(struct clk *clk, unsigned long rate)
  88. {
  89. if (clk == NULL || IS_ERR(clk))
  90. return -EINVAL;
  91. /* changing the clk rate is not supported */
  92. return -EINVAL;
  93. }
  94. EXPORT_SYMBOL(clk_set_rate);
  95. int clk_register(struct clk *clk)
  96. {
  97. if (clk == NULL || IS_ERR(clk))
  98. return -EINVAL;
  99. if (WARN(clk->parent && !clk->parent->rate,
  100. "CLK: %s parent %s has no rate!\n",
  101. clk->name, clk->parent->name))
  102. return -EINVAL;
  103. mutex_lock(&clocks_mutex);
  104. list_add_tail(&clk->node, &clocks);
  105. mutex_unlock(&clocks_mutex);
  106. /* If rate is already set, use it */
  107. if (clk->rate)
  108. return 0;
  109. /* Otherwise, default to parent rate */
  110. if (clk->parent)
  111. clk->rate = clk->parent->rate;
  112. return 0;
  113. }
  114. EXPORT_SYMBOL(clk_register);
  115. void clk_unregister(struct clk *clk)
  116. {
  117. if (clk == NULL || IS_ERR(clk))
  118. return;
  119. mutex_lock(&clocks_mutex);
  120. list_del(&clk->node);
  121. mutex_unlock(&clocks_mutex);
  122. }
  123. EXPORT_SYMBOL(clk_unregister);
  124. #ifdef CONFIG_DAVINCI_RESET_CLOCKS
  125. /*
  126. * Disable any unused clocks left on by the bootloader
  127. */
  128. static int __init clk_disable_unused(void)
  129. {
  130. struct clk *ck;
  131. spin_lock_irq(&clockfw_lock);
  132. list_for_each_entry(ck, &clocks, node) {
  133. if (ck->usecount > 0)
  134. continue;
  135. if (!(ck->flags & CLK_PSC))
  136. continue;
  137. /* ignore if in Disabled or SwRstDisable states */
  138. if (!davinci_psc_is_clk_active(ck->psc_ctlr, ck->lpsc))
  139. continue;
  140. pr_info("Clocks: disable unused %s\n", ck->name);
  141. davinci_psc_config(psc_domain(ck), ck->psc_ctlr, ck->lpsc, 0);
  142. }
  143. spin_unlock_irq(&clockfw_lock);
  144. return 0;
  145. }
  146. late_initcall(clk_disable_unused);
  147. #endif
  148. static void clk_sysclk_recalc(struct clk *clk)
  149. {
  150. u32 v, plldiv;
  151. struct pll_data *pll;
  152. /* If this is the PLL base clock, no more calculations needed */
  153. if (clk->pll_data)
  154. return;
  155. if (WARN_ON(!clk->parent))
  156. return;
  157. clk->rate = clk->parent->rate;
  158. /* Otherwise, the parent must be a PLL */
  159. if (WARN_ON(!clk->parent->pll_data))
  160. return;
  161. pll = clk->parent->pll_data;
  162. /* If pre-PLL, source clock is before the multiplier and divider(s) */
  163. if (clk->flags & PRE_PLL)
  164. clk->rate = pll->input_rate;
  165. if (!clk->div_reg)
  166. return;
  167. v = __raw_readl(pll->base + clk->div_reg);
  168. if (v & PLLDIV_EN) {
  169. plldiv = (v & PLLDIV_RATIO_MASK) + 1;
  170. if (plldiv)
  171. clk->rate /= plldiv;
  172. }
  173. }
  174. static void __init clk_pll_init(struct clk *clk)
  175. {
  176. u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
  177. u8 bypass;
  178. struct pll_data *pll = clk->pll_data;
  179. pll->base = IO_ADDRESS(pll->phys_base);
  180. ctrl = __raw_readl(pll->base + PLLCTL);
  181. clk->rate = pll->input_rate = clk->parent->rate;
  182. if (ctrl & PLLCTL_PLLEN) {
  183. bypass = 0;
  184. mult = __raw_readl(pll->base + PLLM);
  185. mult = (mult & PLLM_PLLM_MASK) + 1;
  186. } else
  187. bypass = 1;
  188. if (pll->flags & PLL_HAS_PREDIV) {
  189. prediv = __raw_readl(pll->base + PREDIV);
  190. if (prediv & PLLDIV_EN)
  191. prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
  192. else
  193. prediv = 1;
  194. }
  195. /* pre-divider is fixed, but (some?) chips won't report that */
  196. if (cpu_is_davinci_dm355() && pll->num == 1)
  197. prediv = 8;
  198. if (pll->flags & PLL_HAS_POSTDIV) {
  199. postdiv = __raw_readl(pll->base + POSTDIV);
  200. if (postdiv & PLLDIV_EN)
  201. postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
  202. else
  203. postdiv = 1;
  204. }
  205. if (!bypass) {
  206. clk->rate /= prediv;
  207. clk->rate *= mult;
  208. clk->rate /= postdiv;
  209. }
  210. pr_debug("PLL%d: input = %lu MHz [ ",
  211. pll->num, clk->parent->rate / 1000000);
  212. if (bypass)
  213. pr_debug("bypass ");
  214. if (prediv > 1)
  215. pr_debug("/ %d ", prediv);
  216. if (mult > 1)
  217. pr_debug("* %d ", mult);
  218. if (postdiv > 1)
  219. pr_debug("/ %d ", postdiv);
  220. pr_debug("] --> %lu MHz output.\n", clk->rate / 1000000);
  221. }
  222. int __init davinci_clk_init(struct davinci_clk *clocks)
  223. {
  224. struct davinci_clk *c;
  225. struct clk *clk;
  226. for (c = clocks; c->lk.clk; c++) {
  227. clk = c->lk.clk;
  228. if (clk->pll_data)
  229. clk_pll_init(clk);
  230. /* Calculate rates for PLL-derived clocks */
  231. else if (clk->flags & CLK_PLL)
  232. clk_sysclk_recalc(clk);
  233. if (clk->lpsc)
  234. clk->flags |= CLK_PSC;
  235. clkdev_add(&c->lk);
  236. clk_register(clk);
  237. /* Turn on clocks that Linux doesn't otherwise manage */
  238. if (clk->flags & ALWAYS_ENABLED)
  239. clk_enable(clk);
  240. }
  241. return 0;
  242. }
  243. #ifdef CONFIG_PROC_FS
  244. #include <linux/proc_fs.h>
  245. #include <linux/seq_file.h>
  246. static void *davinci_ck_start(struct seq_file *m, loff_t *pos)
  247. {
  248. return *pos < 1 ? (void *)1 : NULL;
  249. }
  250. static void *davinci_ck_next(struct seq_file *m, void *v, loff_t *pos)
  251. {
  252. ++*pos;
  253. return NULL;
  254. }
  255. static void davinci_ck_stop(struct seq_file *m, void *v)
  256. {
  257. }
  258. #define CLKNAME_MAX 10 /* longest clock name */
  259. #define NEST_DELTA 2
  260. #define NEST_MAX 4
  261. static void
  262. dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
  263. {
  264. char *state;
  265. char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
  266. struct clk *clk;
  267. unsigned i;
  268. if (parent->flags & CLK_PLL)
  269. state = "pll";
  270. else if (parent->flags & CLK_PSC)
  271. state = "psc";
  272. else
  273. state = "";
  274. /* <nest spaces> name <pad to end> */
  275. memset(buf, ' ', sizeof(buf) - 1);
  276. buf[sizeof(buf) - 1] = 0;
  277. i = strlen(parent->name);
  278. memcpy(buf + nest, parent->name,
  279. min(i, (unsigned)(sizeof(buf) - 1 - nest)));
  280. seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
  281. buf, parent->usecount, state, clk_get_rate(parent));
  282. /* REVISIT show device associations too */
  283. /* cost is now small, but not linear... */
  284. list_for_each_entry(clk, &clocks, node) {
  285. if (clk->parent == parent)
  286. dump_clock(s, nest + NEST_DELTA, clk);
  287. }
  288. }
  289. static int davinci_ck_show(struct seq_file *m, void *v)
  290. {
  291. /* Show clock tree; we know the main oscillator is first.
  292. * We trust nonzero usecounts equate to PSC enables...
  293. */
  294. mutex_lock(&clocks_mutex);
  295. if (!list_empty(&clocks))
  296. dump_clock(m, 0, list_first_entry(&clocks, struct clk, node));
  297. mutex_unlock(&clocks_mutex);
  298. return 0;
  299. }
  300. static const struct seq_operations davinci_ck_op = {
  301. .start = davinci_ck_start,
  302. .next = davinci_ck_next,
  303. .stop = davinci_ck_stop,
  304. .show = davinci_ck_show
  305. };
  306. static int davinci_ck_open(struct inode *inode, struct file *file)
  307. {
  308. return seq_open(file, &davinci_ck_op);
  309. }
  310. static const struct file_operations proc_davinci_ck_operations = {
  311. .open = davinci_ck_open,
  312. .read = seq_read,
  313. .llseek = seq_lseek,
  314. .release = seq_release,
  315. };
  316. static int __init davinci_ck_proc_init(void)
  317. {
  318. proc_create("davinci_clocks", 0, NULL, &proc_davinci_ck_operations);
  319. return 0;
  320. }
  321. __initcall(davinci_ck_proc_init);
  322. #endif /* CONFIG_DEBUG_PROC_FS */