clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/io.h>
  20. #include <linux/delay.h>
  21. #include <mach/hardware.h>
  22. #include <mach/clock.h>
  23. #include <mach/psc.h>
  24. #include <mach/cputype.h>
  25. #include "clock.h"
  26. static LIST_HEAD(clocks);
  27. static DEFINE_MUTEX(clocks_mutex);
  28. static DEFINE_SPINLOCK(clockfw_lock);
  29. static unsigned psc_domain(struct clk *clk)
  30. {
  31. return (clk->flags & PSC_DSP)
  32. ? DAVINCI_GPSC_DSPDOMAIN
  33. : DAVINCI_GPSC_ARMDOMAIN;
  34. }
  35. static void __clk_enable(struct clk *clk)
  36. {
  37. if (clk->parent)
  38. __clk_enable(clk->parent);
  39. if (clk->usecount++ == 0 && (clk->flags & CLK_PSC))
  40. davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc,
  41. PSC_STATE_ENABLE);
  42. }
  43. static void __clk_disable(struct clk *clk)
  44. {
  45. if (WARN_ON(clk->usecount == 0))
  46. return;
  47. if (--clk->usecount == 0 && !(clk->flags & CLK_PLL) &&
  48. (clk->flags & CLK_PSC))
  49. davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc,
  50. (clk->flags & PSC_SWRSTDISABLE) ?
  51. PSC_STATE_SWRSTDISABLE : PSC_STATE_DISABLE);
  52. if (clk->parent)
  53. __clk_disable(clk->parent);
  54. }
  55. int clk_enable(struct clk *clk)
  56. {
  57. unsigned long flags;
  58. if (clk == NULL || IS_ERR(clk))
  59. return -EINVAL;
  60. spin_lock_irqsave(&clockfw_lock, flags);
  61. __clk_enable(clk);
  62. spin_unlock_irqrestore(&clockfw_lock, flags);
  63. return 0;
  64. }
  65. EXPORT_SYMBOL(clk_enable);
  66. void clk_disable(struct clk *clk)
  67. {
  68. unsigned long flags;
  69. if (clk == NULL || IS_ERR(clk))
  70. return;
  71. spin_lock_irqsave(&clockfw_lock, flags);
  72. __clk_disable(clk);
  73. spin_unlock_irqrestore(&clockfw_lock, flags);
  74. }
  75. EXPORT_SYMBOL(clk_disable);
  76. unsigned long clk_get_rate(struct clk *clk)
  77. {
  78. if (clk == NULL || IS_ERR(clk))
  79. return -EINVAL;
  80. return clk->rate;
  81. }
  82. EXPORT_SYMBOL(clk_get_rate);
  83. long clk_round_rate(struct clk *clk, unsigned long rate)
  84. {
  85. if (clk == NULL || IS_ERR(clk))
  86. return -EINVAL;
  87. if (clk->round_rate)
  88. return clk->round_rate(clk, rate);
  89. return clk->rate;
  90. }
  91. EXPORT_SYMBOL(clk_round_rate);
  92. /* Propagate rate to children */
  93. static void propagate_rate(struct clk *root)
  94. {
  95. struct clk *clk;
  96. list_for_each_entry(clk, &root->children, childnode) {
  97. if (clk->recalc)
  98. clk->rate = clk->recalc(clk);
  99. propagate_rate(clk);
  100. }
  101. }
  102. int clk_set_rate(struct clk *clk, unsigned long rate)
  103. {
  104. unsigned long flags;
  105. int ret = -EINVAL;
  106. if (clk == NULL || IS_ERR(clk))
  107. return ret;
  108. if (clk->set_rate)
  109. ret = clk->set_rate(clk, rate);
  110. spin_lock_irqsave(&clockfw_lock, flags);
  111. if (ret == 0) {
  112. if (clk->recalc)
  113. clk->rate = clk->recalc(clk);
  114. propagate_rate(clk);
  115. }
  116. spin_unlock_irqrestore(&clockfw_lock, flags);
  117. return ret;
  118. }
  119. EXPORT_SYMBOL(clk_set_rate);
  120. int clk_set_parent(struct clk *clk, struct clk *parent)
  121. {
  122. unsigned long flags;
  123. if (clk == NULL || IS_ERR(clk))
  124. return -EINVAL;
  125. /* Cannot change parent on enabled clock */
  126. if (WARN_ON(clk->usecount))
  127. return -EINVAL;
  128. mutex_lock(&clocks_mutex);
  129. clk->parent = parent;
  130. list_del_init(&clk->childnode);
  131. list_add(&clk->childnode, &clk->parent->children);
  132. mutex_unlock(&clocks_mutex);
  133. spin_lock_irqsave(&clockfw_lock, flags);
  134. if (clk->recalc)
  135. clk->rate = clk->recalc(clk);
  136. propagate_rate(clk);
  137. spin_unlock_irqrestore(&clockfw_lock, flags);
  138. return 0;
  139. }
  140. EXPORT_SYMBOL(clk_set_parent);
  141. int clk_register(struct clk *clk)
  142. {
  143. if (clk == NULL || IS_ERR(clk))
  144. return -EINVAL;
  145. if (WARN(clk->parent && !clk->parent->rate,
  146. "CLK: %s parent %s has no rate!\n",
  147. clk->name, clk->parent->name))
  148. return -EINVAL;
  149. INIT_LIST_HEAD(&clk->children);
  150. mutex_lock(&clocks_mutex);
  151. list_add_tail(&clk->node, &clocks);
  152. if (clk->parent)
  153. list_add_tail(&clk->childnode, &clk->parent->children);
  154. mutex_unlock(&clocks_mutex);
  155. /* If rate is already set, use it */
  156. if (clk->rate)
  157. return 0;
  158. /* Else, see if there is a way to calculate it */
  159. if (clk->recalc)
  160. clk->rate = clk->recalc(clk);
  161. /* Otherwise, default to parent rate */
  162. else if (clk->parent)
  163. clk->rate = clk->parent->rate;
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(clk_register);
  167. void clk_unregister(struct clk *clk)
  168. {
  169. if (clk == NULL || IS_ERR(clk))
  170. return;
  171. mutex_lock(&clocks_mutex);
  172. list_del(&clk->node);
  173. list_del(&clk->childnode);
  174. mutex_unlock(&clocks_mutex);
  175. }
  176. EXPORT_SYMBOL(clk_unregister);
  177. #ifdef CONFIG_DAVINCI_RESET_CLOCKS
  178. /*
  179. * Disable any unused clocks left on by the bootloader
  180. */
  181. static int __init clk_disable_unused(void)
  182. {
  183. struct clk *ck;
  184. spin_lock_irq(&clockfw_lock);
  185. list_for_each_entry(ck, &clocks, node) {
  186. if (ck->usecount > 0)
  187. continue;
  188. if (!(ck->flags & CLK_PSC))
  189. continue;
  190. /* ignore if in Disabled or SwRstDisable states */
  191. if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
  192. continue;
  193. pr_info("Clocks: disable unused %s\n", ck->name);
  194. davinci_psc_config(psc_domain(ck), ck->gpsc, ck->lpsc,
  195. (ck->flags & PSC_SWRSTDISABLE) ?
  196. PSC_STATE_SWRSTDISABLE : PSC_STATE_DISABLE);
  197. }
  198. spin_unlock_irq(&clockfw_lock);
  199. return 0;
  200. }
  201. late_initcall(clk_disable_unused);
  202. #endif
  203. static unsigned long clk_sysclk_recalc(struct clk *clk)
  204. {
  205. u32 v, plldiv;
  206. struct pll_data *pll;
  207. unsigned long rate = clk->rate;
  208. /* If this is the PLL base clock, no more calculations needed */
  209. if (clk->pll_data)
  210. return rate;
  211. if (WARN_ON(!clk->parent))
  212. return rate;
  213. rate = clk->parent->rate;
  214. /* Otherwise, the parent must be a PLL */
  215. if (WARN_ON(!clk->parent->pll_data))
  216. return rate;
  217. pll = clk->parent->pll_data;
  218. /* If pre-PLL, source clock is before the multiplier and divider(s) */
  219. if (clk->flags & PRE_PLL)
  220. rate = pll->input_rate;
  221. if (!clk->div_reg)
  222. return rate;
  223. v = __raw_readl(pll->base + clk->div_reg);
  224. if (v & PLLDIV_EN) {
  225. plldiv = (v & pll->div_ratio_mask) + 1;
  226. if (plldiv)
  227. rate /= plldiv;
  228. }
  229. return rate;
  230. }
  231. static unsigned long clk_leafclk_recalc(struct clk *clk)
  232. {
  233. if (WARN_ON(!clk->parent))
  234. return clk->rate;
  235. return clk->parent->rate;
  236. }
  237. static unsigned long clk_pllclk_recalc(struct clk *clk)
  238. {
  239. u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
  240. u8 bypass;
  241. struct pll_data *pll = clk->pll_data;
  242. unsigned long rate = clk->rate;
  243. ctrl = __raw_readl(pll->base + PLLCTL);
  244. rate = pll->input_rate = clk->parent->rate;
  245. if (ctrl & PLLCTL_PLLEN) {
  246. bypass = 0;
  247. mult = __raw_readl(pll->base + PLLM);
  248. if (cpu_is_davinci_dm365())
  249. mult = 2 * (mult & PLLM_PLLM_MASK);
  250. else
  251. mult = (mult & PLLM_PLLM_MASK) + 1;
  252. } else
  253. bypass = 1;
  254. if (pll->flags & PLL_HAS_PREDIV) {
  255. prediv = __raw_readl(pll->base + PREDIV);
  256. if (prediv & PLLDIV_EN)
  257. prediv = (prediv & pll->div_ratio_mask) + 1;
  258. else
  259. prediv = 1;
  260. }
  261. /* pre-divider is fixed, but (some?) chips won't report that */
  262. if (cpu_is_davinci_dm355() && pll->num == 1)
  263. prediv = 8;
  264. if (pll->flags & PLL_HAS_POSTDIV) {
  265. postdiv = __raw_readl(pll->base + POSTDIV);
  266. if (postdiv & PLLDIV_EN)
  267. postdiv = (postdiv & pll->div_ratio_mask) + 1;
  268. else
  269. postdiv = 1;
  270. }
  271. if (!bypass) {
  272. rate /= prediv;
  273. rate *= mult;
  274. rate /= postdiv;
  275. }
  276. pr_debug("PLL%d: input = %lu MHz [ ",
  277. pll->num, clk->parent->rate / 1000000);
  278. if (bypass)
  279. pr_debug("bypass ");
  280. if (prediv > 1)
  281. pr_debug("/ %d ", prediv);
  282. if (mult > 1)
  283. pr_debug("* %d ", mult);
  284. if (postdiv > 1)
  285. pr_debug("/ %d ", postdiv);
  286. pr_debug("] --> %lu MHz output.\n", rate / 1000000);
  287. return rate;
  288. }
  289. /**
  290. * davinci_set_pllrate - set the output rate of a given PLL.
  291. *
  292. * Note: Currently tested to work with OMAP-L138 only.
  293. *
  294. * @pll: pll whose rate needs to be changed.
  295. * @prediv: The pre divider value. Passing 0 disables the pre-divider.
  296. * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
  297. * @postdiv: The post divider value. Passing 0 disables the post-divider.
  298. */
  299. int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
  300. unsigned int mult, unsigned int postdiv)
  301. {
  302. u32 ctrl;
  303. unsigned int locktime;
  304. unsigned long flags;
  305. if (pll->base == NULL)
  306. return -EINVAL;
  307. /*
  308. * PLL lock time required per OMAP-L138 datasheet is
  309. * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
  310. * as 4 and OSCIN cycle as 25 MHz.
  311. */
  312. if (prediv) {
  313. locktime = ((2000 * prediv) / 100);
  314. prediv = (prediv - 1) | PLLDIV_EN;
  315. } else {
  316. locktime = PLL_LOCK_TIME;
  317. }
  318. if (postdiv)
  319. postdiv = (postdiv - 1) | PLLDIV_EN;
  320. if (mult)
  321. mult = mult - 1;
  322. /* Protect against simultaneous calls to PLL setting seqeunce */
  323. spin_lock_irqsave(&clockfw_lock, flags);
  324. ctrl = __raw_readl(pll->base + PLLCTL);
  325. /* Switch the PLL to bypass mode */
  326. ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
  327. __raw_writel(ctrl, pll->base + PLLCTL);
  328. udelay(PLL_BYPASS_TIME);
  329. /* Reset and enable PLL */
  330. ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
  331. __raw_writel(ctrl, pll->base + PLLCTL);
  332. if (pll->flags & PLL_HAS_PREDIV)
  333. __raw_writel(prediv, pll->base + PREDIV);
  334. __raw_writel(mult, pll->base + PLLM);
  335. if (pll->flags & PLL_HAS_POSTDIV)
  336. __raw_writel(postdiv, pll->base + POSTDIV);
  337. udelay(PLL_RESET_TIME);
  338. /* Bring PLL out of reset */
  339. ctrl |= PLLCTL_PLLRST;
  340. __raw_writel(ctrl, pll->base + PLLCTL);
  341. udelay(locktime);
  342. /* Remove PLL from bypass mode */
  343. ctrl |= PLLCTL_PLLEN;
  344. __raw_writel(ctrl, pll->base + PLLCTL);
  345. spin_unlock_irqrestore(&clockfw_lock, flags);
  346. return 0;
  347. }
  348. EXPORT_SYMBOL(davinci_set_pllrate);
  349. int __init davinci_clk_init(struct clk_lookup *clocks)
  350. {
  351. struct clk_lookup *c;
  352. struct clk *clk;
  353. size_t num_clocks = 0;
  354. for (c = clocks; c->clk; c++) {
  355. clk = c->clk;
  356. if (!clk->recalc) {
  357. /* Check if clock is a PLL */
  358. if (clk->pll_data)
  359. clk->recalc = clk_pllclk_recalc;
  360. /* Else, if it is a PLL-derived clock */
  361. else if (clk->flags & CLK_PLL)
  362. clk->recalc = clk_sysclk_recalc;
  363. /* Otherwise, it is a leaf clock (PSC clock) */
  364. else if (clk->parent)
  365. clk->recalc = clk_leafclk_recalc;
  366. }
  367. if (clk->pll_data) {
  368. struct pll_data *pll = clk->pll_data;
  369. if (!pll->div_ratio_mask)
  370. pll->div_ratio_mask = PLLDIV_RATIO_MASK;
  371. if (pll->phys_base && !pll->base) {
  372. pll->base = ioremap(pll->phys_base, SZ_4K);
  373. WARN_ON(!pll->base);
  374. }
  375. }
  376. if (clk->recalc)
  377. clk->rate = clk->recalc(clk);
  378. if (clk->lpsc)
  379. clk->flags |= CLK_PSC;
  380. clk_register(clk);
  381. num_clocks++;
  382. /* Turn on clocks that Linux doesn't otherwise manage */
  383. if (clk->flags & ALWAYS_ENABLED)
  384. clk_enable(clk);
  385. }
  386. clkdev_add_table(clocks, num_clocks);
  387. return 0;
  388. }
  389. #ifdef CONFIG_DEBUG_FS
  390. #include <linux/debugfs.h>
  391. #include <linux/seq_file.h>
  392. #define CLKNAME_MAX 10 /* longest clock name */
  393. #define NEST_DELTA 2
  394. #define NEST_MAX 4
  395. static void
  396. dump_clock(struct seq_file *s, unsigned nest, struct clk *parent)
  397. {
  398. char *state;
  399. char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX];
  400. struct clk *clk;
  401. unsigned i;
  402. if (parent->flags & CLK_PLL)
  403. state = "pll";
  404. else if (parent->flags & CLK_PSC)
  405. state = "psc";
  406. else
  407. state = "";
  408. /* <nest spaces> name <pad to end> */
  409. memset(buf, ' ', sizeof(buf) - 1);
  410. buf[sizeof(buf) - 1] = 0;
  411. i = strlen(parent->name);
  412. memcpy(buf + nest, parent->name,
  413. min(i, (unsigned)(sizeof(buf) - 1 - nest)));
  414. seq_printf(s, "%s users=%2d %-3s %9ld Hz\n",
  415. buf, parent->usecount, state, clk_get_rate(parent));
  416. /* REVISIT show device associations too */
  417. /* cost is now small, but not linear... */
  418. list_for_each_entry(clk, &parent->children, childnode) {
  419. dump_clock(s, nest + NEST_DELTA, clk);
  420. }
  421. }
  422. static int davinci_ck_show(struct seq_file *m, void *v)
  423. {
  424. struct clk *clk;
  425. /*
  426. * Show clock tree; We trust nonzero usecounts equate to PSC enables...
  427. */
  428. mutex_lock(&clocks_mutex);
  429. list_for_each_entry(clk, &clocks, node)
  430. if (!clk->parent)
  431. dump_clock(m, 0, clk);
  432. mutex_unlock(&clocks_mutex);
  433. return 0;
  434. }
  435. static int davinci_ck_open(struct inode *inode, struct file *file)
  436. {
  437. return single_open(file, davinci_ck_show, NULL);
  438. }
  439. static const struct file_operations davinci_ck_operations = {
  440. .open = davinci_ck_open,
  441. .read = seq_read,
  442. .llseek = seq_lseek,
  443. .release = single_release,
  444. };
  445. static int __init davinci_clk_debugfs_init(void)
  446. {
  447. debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL,
  448. &davinci_ck_operations);
  449. return 0;
  450. }
  451. device_initcall(davinci_clk_debugfs_init);
  452. #endif /* CONFIG_DEBUG_FS */