clock.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. if (cpu_is_davinci_dm365())
  186. mult = 2 * (mult & PLLM_PLLM_MASK);
  187. else
  188. mult = (mult & PLLM_PLLM_MASK) + 1;
  189. } else
  190. bypass = 1;
  191. if (pll->flags & PLL_HAS_PREDIV) {
  192. prediv = __raw_readl(pll->base + PREDIV);
  193. if (prediv & PLLDIV_EN)
  194. prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
  195. else
  196. prediv = 1;
  197. }
  198. /* pre-divider is fixed, but (some?) chips won't report that */
  199. if (cpu_is_davinci_dm355() && pll->num == 1)
  200. prediv = 8;
  201. if (pll->flags & PLL_HAS_POSTDIV) {
  202. postdiv = __raw_readl(pll->base + POSTDIV);
  203. if (postdiv & PLLDIV_EN)
  204. postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
  205. else
  206. postdiv = 1;
  207. }
  208. if (!bypass) {
  209. clk->rate /= prediv;
  210. clk->rate *= mult;
  211. clk->rate /= postdiv;
  212. }
  213. pr_debug("PLL%d: input = %lu MHz [ ",
  214. pll->num, clk->parent->rate / 1000000);
  215. if (bypass)
  216. pr_debug("bypass ");
  217. if (prediv > 1)
  218. pr_debug("/ %d ", prediv);
  219. if (mult > 1)
  220. pr_debug("* %d ", mult);
  221. if (postdiv > 1)
  222. pr_debug("/ %d ", postdiv);
  223. pr_debug("] --> %lu MHz output.\n", clk->rate / 1000000);
  224. }
  225. int __init davinci_clk_init(struct davinci_clk *clocks)
  226. {
  227. struct davinci_clk *c;
  228. struct clk *clk;
  229. for (c = clocks; c->lk.clk; c++) {
  230. clk = c->lk.clk;
  231. if (clk->pll_data)
  232. clk_pll_init(clk);
  233. /* Calculate rates for PLL-derived clocks */
  234. else if (clk->flags & CLK_PLL)
  235. clk_sysclk_recalc(clk);
  236. if (clk->lpsc)
  237. clk->flags |= CLK_PSC;
  238. clkdev_add(&c->lk);
  239. clk_register(clk);
  240. /* Turn on clocks that Linux doesn't otherwise manage */
  241. if (clk->flags & ALWAYS_ENABLED)
  242. clk_enable(clk);
  243. }
  244. return 0;
  245. }
  246. #ifdef CONFIG_PROC_FS
  247. #include <linux/proc_fs.h>
  248. #include <linux/seq_file.h>
  249. static void *davinci_ck_start(struct seq_file *m, loff_t *pos)
  250. {
  251. return *pos < 1 ? (void *)1 : NULL;
  252. }
  253. static void *davinci_ck_next(struct seq_file *m, void *v, loff_t *pos)
  254. {
  255. ++*pos;
  256. return NULL;
  257. }
  258. static void davinci_ck_stop(struct seq_file *m, void *v)
  259. {
  260. }
  261. #define CLKNAME_MAX 10 /* longest clock name */
  262. #define NEST_DELTA 2
  263. #define NEST_MAX 4
  264. static void
  265. dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
  266. {
  267. char *state;
  268. char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
  269. struct clk *clk;
  270. unsigned i;
  271. if (parent->flags & CLK_PLL)
  272. state = "pll";
  273. else if (parent->flags & CLK_PSC)
  274. state = "psc";
  275. else
  276. state = "";
  277. /* <nest spaces> name <pad to end> */
  278. memset(buf, ' ', sizeof(buf) - 1);
  279. buf[sizeof(buf) - 1] = 0;
  280. i = strlen(parent->name);
  281. memcpy(buf + nest, parent->name,
  282. min(i, (unsigned)(sizeof(buf) - 1 - nest)));
  283. seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
  284. buf, parent->usecount, state, clk_get_rate(parent));
  285. /* REVISIT show device associations too */
  286. /* cost is now small, but not linear... */
  287. list_for_each_entry(clk, &clocks, node) {
  288. if (clk->parent == parent)
  289. dump_clock(s, nest + NEST_DELTA, clk);
  290. }
  291. }
  292. static int davinci_ck_show(struct seq_file *m, void *v)
  293. {
  294. /* Show clock tree; we know the main oscillator is first.
  295. * We trust nonzero usecounts equate to PSC enables...
  296. */
  297. mutex_lock(&clocks_mutex);
  298. if (!list_empty(&clocks))
  299. dump_clock(m, 0, list_first_entry(&clocks, struct clk, node));
  300. mutex_unlock(&clocks_mutex);
  301. return 0;
  302. }
  303. static const struct seq_operations davinci_ck_op = {
  304. .start = davinci_ck_start,
  305. .next = davinci_ck_next,
  306. .stop = davinci_ck_stop,
  307. .show = davinci_ck_show
  308. };
  309. static int davinci_ck_open(struct inode *inode, struct file *file)
  310. {
  311. return seq_open(file, &davinci_ck_op);
  312. }
  313. static const struct file_operations proc_davinci_ck_operations = {
  314. .open = davinci_ck_open,
  315. .read = seq_read,
  316. .llseek = seq_lseek,
  317. .release = seq_release,
  318. };
  319. static int __init davinci_ck_proc_init(void)
  320. {
  321. proc_create("davinci_clocks", 0, NULL, &proc_davinci_ck_operations);
  322. return 0;
  323. }
  324. __initcall(davinci_ck_proc_init);
  325. #endif /* CONFIG_DEBUG_PROC_FS */