clock.c 8.4 KB

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