pll.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Clock and PLL control for C64x+ devices
  3. *
  4. * Copyright (C) 2010, 2011 Texas Instruments.
  5. * Contributed by: Mark Salter <msalter@redhat.com>
  6. *
  7. * Copied heavily from arm/mach-davinci/clock.c, so:
  8. *
  9. * Copyright (C) 2006-2007 Texas Instruments.
  10. * Copyright (C) 2008-2009 Deep Root Systems, LLC
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/clk.h>
  20. #include <linux/io.h>
  21. #include <linux/err.h>
  22. #include <asm/clock.h>
  23. #include <asm/soc.h>
  24. static LIST_HEAD(clocks);
  25. static DEFINE_MUTEX(clocks_mutex);
  26. static DEFINE_SPINLOCK(clockfw_lock);
  27. static void __clk_enable(struct clk *clk)
  28. {
  29. if (clk->parent)
  30. __clk_enable(clk->parent);
  31. clk->usecount++;
  32. }
  33. static void __clk_disable(struct clk *clk)
  34. {
  35. if (WARN_ON(clk->usecount == 0))
  36. return;
  37. --clk->usecount;
  38. if (clk->parent)
  39. __clk_disable(clk->parent);
  40. }
  41. int clk_enable(struct clk *clk)
  42. {
  43. unsigned long flags;
  44. if (clk == NULL || IS_ERR(clk))
  45. return -EINVAL;
  46. spin_lock_irqsave(&clockfw_lock, flags);
  47. __clk_enable(clk);
  48. spin_unlock_irqrestore(&clockfw_lock, flags);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(clk_enable);
  52. void clk_disable(struct clk *clk)
  53. {
  54. unsigned long flags;
  55. if (clk == NULL || IS_ERR(clk))
  56. return;
  57. spin_lock_irqsave(&clockfw_lock, flags);
  58. __clk_disable(clk);
  59. spin_unlock_irqrestore(&clockfw_lock, flags);
  60. }
  61. EXPORT_SYMBOL(clk_disable);
  62. unsigned long clk_get_rate(struct clk *clk)
  63. {
  64. if (clk == NULL || IS_ERR(clk))
  65. return -EINVAL;
  66. return clk->rate;
  67. }
  68. EXPORT_SYMBOL(clk_get_rate);
  69. long clk_round_rate(struct clk *clk, unsigned long rate)
  70. {
  71. if (clk == NULL || IS_ERR(clk))
  72. return -EINVAL;
  73. if (clk->round_rate)
  74. return clk->round_rate(clk, rate);
  75. return clk->rate;
  76. }
  77. EXPORT_SYMBOL(clk_round_rate);
  78. /* Propagate rate to children */
  79. static void propagate_rate(struct clk *root)
  80. {
  81. struct clk *clk;
  82. list_for_each_entry(clk, &root->children, childnode) {
  83. if (clk->recalc)
  84. clk->rate = clk->recalc(clk);
  85. propagate_rate(clk);
  86. }
  87. }
  88. int clk_set_rate(struct clk *clk, unsigned long rate)
  89. {
  90. unsigned long flags;
  91. int ret = -EINVAL;
  92. if (clk == NULL || IS_ERR(clk))
  93. return ret;
  94. if (clk->set_rate)
  95. ret = clk->set_rate(clk, rate);
  96. spin_lock_irqsave(&clockfw_lock, flags);
  97. if (ret == 0) {
  98. if (clk->recalc)
  99. clk->rate = clk->recalc(clk);
  100. propagate_rate(clk);
  101. }
  102. spin_unlock_irqrestore(&clockfw_lock, flags);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL(clk_set_rate);
  106. int clk_set_parent(struct clk *clk, struct clk *parent)
  107. {
  108. unsigned long flags;
  109. if (clk == NULL || IS_ERR(clk))
  110. return -EINVAL;
  111. /* Cannot change parent on enabled clock */
  112. if (WARN_ON(clk->usecount))
  113. return -EINVAL;
  114. mutex_lock(&clocks_mutex);
  115. clk->parent = parent;
  116. list_del_init(&clk->childnode);
  117. list_add(&clk->childnode, &clk->parent->children);
  118. mutex_unlock(&clocks_mutex);
  119. spin_lock_irqsave(&clockfw_lock, flags);
  120. if (clk->recalc)
  121. clk->rate = clk->recalc(clk);
  122. propagate_rate(clk);
  123. spin_unlock_irqrestore(&clockfw_lock, flags);
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(clk_set_parent);
  127. int clk_register(struct clk *clk)
  128. {
  129. if (clk == NULL || IS_ERR(clk))
  130. return -EINVAL;
  131. if (WARN(clk->parent && !clk->parent->rate,
  132. "CLK: %s parent %s has no rate!\n",
  133. clk->name, clk->parent->name))
  134. return -EINVAL;
  135. mutex_lock(&clocks_mutex);
  136. list_add_tail(&clk->node, &clocks);
  137. if (clk->parent)
  138. list_add_tail(&clk->childnode, &clk->parent->children);
  139. mutex_unlock(&clocks_mutex);
  140. /* If rate is already set, use it */
  141. if (clk->rate)
  142. return 0;
  143. /* Else, see if there is a way to calculate it */
  144. if (clk->recalc)
  145. clk->rate = clk->recalc(clk);
  146. /* Otherwise, default to parent rate */
  147. else if (clk->parent)
  148. clk->rate = clk->parent->rate;
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(clk_register);
  152. void clk_unregister(struct clk *clk)
  153. {
  154. if (clk == NULL || IS_ERR(clk))
  155. return;
  156. mutex_lock(&clocks_mutex);
  157. list_del(&clk->node);
  158. list_del(&clk->childnode);
  159. mutex_unlock(&clocks_mutex);
  160. }
  161. EXPORT_SYMBOL(clk_unregister);
  162. static u32 pll_read(struct pll_data *pll, int reg)
  163. {
  164. return soc_readl(pll->base + reg);
  165. }
  166. static unsigned long clk_sysclk_recalc(struct clk *clk)
  167. {
  168. u32 v, plldiv = 0;
  169. struct pll_data *pll;
  170. unsigned long rate = clk->rate;
  171. if (WARN_ON(!clk->parent))
  172. return rate;
  173. rate = clk->parent->rate;
  174. /* the parent must be a PLL */
  175. if (WARN_ON(!clk->parent->pll_data))
  176. return rate;
  177. pll = clk->parent->pll_data;
  178. /* If pre-PLL, source clock is before the multiplier and divider(s) */
  179. if (clk->flags & PRE_PLL)
  180. rate = pll->input_rate;
  181. if (!clk->div) {
  182. pr_debug("%s: (no divider) rate = %lu KHz\n",
  183. clk->name, rate / 1000);
  184. return rate;
  185. }
  186. if (clk->flags & FIXED_DIV_PLL) {
  187. rate /= clk->div;
  188. pr_debug("%s: (fixed divide by %d) rate = %lu KHz\n",
  189. clk->name, clk->div, rate / 1000);
  190. return rate;
  191. }
  192. v = pll_read(pll, clk->div);
  193. if (v & PLLDIV_EN)
  194. plldiv = (v & PLLDIV_RATIO_MASK) + 1;
  195. if (plldiv == 0)
  196. plldiv = 1;
  197. rate /= plldiv;
  198. pr_debug("%s: (divide by %d) rate = %lu KHz\n",
  199. clk->name, plldiv, rate / 1000);
  200. return rate;
  201. }
  202. static unsigned long clk_leafclk_recalc(struct clk *clk)
  203. {
  204. if (WARN_ON(!clk->parent))
  205. return clk->rate;
  206. pr_debug("%s: (parent %s) rate = %lu KHz\n",
  207. clk->name, clk->parent->name, clk->parent->rate / 1000);
  208. return clk->parent->rate;
  209. }
  210. static unsigned long clk_pllclk_recalc(struct clk *clk)
  211. {
  212. u32 ctrl, mult = 0, prediv = 0, postdiv = 0;
  213. u8 bypass;
  214. struct pll_data *pll = clk->pll_data;
  215. unsigned long rate = clk->rate;
  216. if (clk->flags & FIXED_RATE_PLL)
  217. return rate;
  218. ctrl = pll_read(pll, PLLCTL);
  219. rate = pll->input_rate = clk->parent->rate;
  220. if (ctrl & PLLCTL_PLLEN)
  221. bypass = 0;
  222. else
  223. bypass = 1;
  224. if (pll->flags & PLL_HAS_MUL) {
  225. mult = pll_read(pll, PLLM);
  226. mult = (mult & PLLM_PLLM_MASK) + 1;
  227. }
  228. if (pll->flags & PLL_HAS_PRE) {
  229. prediv = pll_read(pll, PLLPRE);
  230. if (prediv & PLLDIV_EN)
  231. prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
  232. else
  233. prediv = 0;
  234. }
  235. if (pll->flags & PLL_HAS_POST) {
  236. postdiv = pll_read(pll, PLLPOST);
  237. if (postdiv & PLLDIV_EN)
  238. postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
  239. else
  240. postdiv = 1;
  241. }
  242. if (!bypass) {
  243. if (prediv)
  244. rate /= prediv;
  245. if (mult)
  246. rate *= mult;
  247. if (postdiv)
  248. rate /= postdiv;
  249. pr_debug("PLL%d: input = %luMHz, pre[%d] mul[%d] post[%d] "
  250. "--> %luMHz output.\n",
  251. pll->num, clk->parent->rate / 1000000,
  252. prediv, mult, postdiv, rate / 1000000);
  253. } else
  254. pr_debug("PLL%d: input = %luMHz, bypass mode.\n",
  255. pll->num, clk->parent->rate / 1000000);
  256. return rate;
  257. }
  258. static void __init __init_clk(struct clk *clk)
  259. {
  260. INIT_LIST_HEAD(&clk->node);
  261. INIT_LIST_HEAD(&clk->children);
  262. INIT_LIST_HEAD(&clk->childnode);
  263. if (!clk->recalc) {
  264. /* Check if clock is a PLL */
  265. if (clk->pll_data)
  266. clk->recalc = clk_pllclk_recalc;
  267. /* Else, if it is a PLL-derived clock */
  268. else if (clk->flags & CLK_PLL)
  269. clk->recalc = clk_sysclk_recalc;
  270. /* Otherwise, it is a leaf clock (PSC clock) */
  271. else if (clk->parent)
  272. clk->recalc = clk_leafclk_recalc;
  273. }
  274. }
  275. void __init c6x_clks_init(struct clk_lookup *clocks)
  276. {
  277. struct clk_lookup *c;
  278. struct clk *clk;
  279. size_t num_clocks = 0;
  280. for (c = clocks; c->clk; c++) {
  281. clk = c->clk;
  282. __init_clk(clk);
  283. clk_register(clk);
  284. num_clocks++;
  285. /* Turn on clocks that Linux doesn't otherwise manage */
  286. if (clk->flags & ALWAYS_ENABLED)
  287. clk_enable(clk);
  288. }
  289. clkdev_add_table(clocks, num_clocks);
  290. }
  291. #ifdef CONFIG_DEBUG_FS
  292. #include <linux/debugfs.h>
  293. #include <linux/seq_file.h>
  294. #define CLKNAME_MAX 10 /* longest clock name */
  295. #define NEST_DELTA 2
  296. #define NEST_MAX 4
  297. static void
  298. dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
  299. {
  300. char *state;
  301. char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
  302. struct clk *clk;
  303. unsigned i;
  304. if (parent->flags & CLK_PLL)
  305. state = "pll";
  306. else
  307. state = "";
  308. /* <nest spaces> name <pad to end> */
  309. memset(buf, ' ', sizeof(buf) - 1);
  310. buf[sizeof(buf) - 1] = 0;
  311. i = strlen(parent->name);
  312. memcpy(buf + nest, parent->name,
  313. min(i, (unsigned)(sizeof(buf) - 1 - nest)));
  314. seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
  315. buf, parent->usecount, state, clk_get_rate(parent));
  316. /* REVISIT show device associations too */
  317. /* cost is now small, but not linear... */
  318. list_for_each_entry(clk, &parent->children, childnode) {
  319. dump_clock(s, nest + NEST_DELTA, clk);
  320. }
  321. }
  322. static int c6x_ck_show(struct seq_file *m, void *v)
  323. {
  324. struct clk *clk;
  325. /*
  326. * Show clock tree; We trust nonzero usecounts equate to PSC enables...
  327. */
  328. mutex_lock(&clocks_mutex);
  329. list_for_each_entry(clk, &clocks, node)
  330. if (!clk->parent)
  331. dump_clock(m, 0, clk);
  332. mutex_unlock(&clocks_mutex);
  333. return 0;
  334. }
  335. static int c6x_ck_open(struct inode *inode, struct file *file)
  336. {
  337. return single_open(file, c6x_ck_show, NULL);
  338. }
  339. static const struct file_operations c6x_ck_operations = {
  340. .open = c6x_ck_open,
  341. .read = seq_read,
  342. .llseek = seq_lseek,
  343. .release = single_release,
  344. };
  345. static int __init c6x_clk_debugfs_init(void)
  346. {
  347. debugfs_create_file("c6x_clocks", S_IFREG | S_IRUGO, NULL, NULL,
  348. &c6x_ck_operations);
  349. return 0;
  350. }
  351. device_initcall(c6x_clk_debugfs_init);
  352. #endif /* CONFIG_DEBUG_FS */