clock.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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/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->gpsc, 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->gpsc, 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. if (clk->round_rate)
  83. return clk->round_rate(clk, rate);
  84. return clk->rate;
  85. }
  86. EXPORT_SYMBOL(clk_round_rate);
  87. /* Propagate rate to children */
  88. static void propagate_rate(struct clk *root)
  89. {
  90. struct clk *clk;
  91. list_for_each_entry(clk, &root->children, childnode) {
  92. if (clk->recalc)
  93. clk->rate = clk->recalc(clk);
  94. propagate_rate(clk);
  95. }
  96. }
  97. int clk_set_rate(struct clk *clk, unsigned long rate)
  98. {
  99. unsigned long flags;
  100. int ret = -EINVAL;
  101. if (clk == NULL || IS_ERR(clk))
  102. return ret;
  103. spin_lock_irqsave(&clockfw_lock, flags);
  104. if (clk->set_rate)
  105. ret = clk->set_rate(clk, rate);
  106. if (ret == 0) {
  107. if (clk->recalc)
  108. clk->rate = clk->recalc(clk);
  109. propagate_rate(clk);
  110. }
  111. spin_unlock_irqrestore(&clockfw_lock, flags);
  112. return ret;
  113. }
  114. EXPORT_SYMBOL(clk_set_rate);
  115. int clk_set_parent(struct clk *clk, struct clk *parent)
  116. {
  117. unsigned long flags;
  118. if (clk == NULL || IS_ERR(clk))
  119. return -EINVAL;
  120. /* Cannot change parent on enabled clock */
  121. if (WARN_ON(clk->usecount))
  122. return -EINVAL;
  123. mutex_lock(&clocks_mutex);
  124. clk->parent = parent;
  125. list_del_init(&clk->childnode);
  126. list_add(&clk->childnode, &clk->parent->children);
  127. mutex_unlock(&clocks_mutex);
  128. spin_lock_irqsave(&clockfw_lock, flags);
  129. if (clk->recalc)
  130. clk->rate = clk->recalc(clk);
  131. propagate_rate(clk);
  132. spin_unlock_irqrestore(&clockfw_lock, flags);
  133. return 0;
  134. }
  135. EXPORT_SYMBOL(clk_set_parent);
  136. int clk_register(struct clk *clk)
  137. {
  138. if (clk == NULL || IS_ERR(clk))
  139. return -EINVAL;
  140. if (WARN(clk->parent && !clk->parent->rate,
  141. "CLK: %s parent %s has no rate!\n",
  142. clk->name, clk->parent->name))
  143. return -EINVAL;
  144. INIT_LIST_HEAD(&clk->children);
  145. mutex_lock(&clocks_mutex);
  146. list_add_tail(&clk->node, &clocks);
  147. if (clk->parent)
  148. list_add_tail(&clk->childnode, &clk->parent->children);
  149. mutex_unlock(&clocks_mutex);
  150. /* If rate is already set, use it */
  151. if (clk->rate)
  152. return 0;
  153. /* Else, see if there is a way to calculate it */
  154. if (clk->recalc)
  155. clk->rate = clk->recalc(clk);
  156. /* Otherwise, default to parent rate */
  157. else if (clk->parent)
  158. clk->rate = clk->parent->rate;
  159. return 0;
  160. }
  161. EXPORT_SYMBOL(clk_register);
  162. void clk_unregister(struct clk *clk)
  163. {
  164. if (clk == NULL || IS_ERR(clk))
  165. return;
  166. mutex_lock(&clocks_mutex);
  167. list_del(&clk->node);
  168. list_del(&clk->childnode);
  169. mutex_unlock(&clocks_mutex);
  170. }
  171. EXPORT_SYMBOL(clk_unregister);
  172. #ifdef CONFIG_DAVINCI_RESET_CLOCKS
  173. /*
  174. * Disable any unused clocks left on by the bootloader
  175. */
  176. static int __init clk_disable_unused(void)
  177. {
  178. struct clk *ck;
  179. spin_lock_irq(&clockfw_lock);
  180. list_for_each_entry(ck, &clocks, node) {
  181. if (ck->usecount > 0)
  182. continue;
  183. if (!(ck->flags & CLK_PSC))
  184. continue;
  185. /* ignore if in Disabled or SwRstDisable states */
  186. if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc))
  187. continue;
  188. pr_info("Clocks: disable unused %s\n", ck->name);
  189. davinci_psc_config(psc_domain(ck), ck->gpsc, ck->lpsc, 0);
  190. }
  191. spin_unlock_irq(&clockfw_lock);
  192. return 0;
  193. }
  194. late_initcall(clk_disable_unused);
  195. #endif
  196. static unsigned long clk_sysclk_recalc(struct clk *clk)
  197. {
  198. u32 v, plldiv;
  199. struct pll_data *pll;
  200. unsigned long rate = clk->rate;
  201. /* If this is the PLL base clock, no more calculations needed */
  202. if (clk->pll_data)
  203. return rate;
  204. if (WARN_ON(!clk->parent))
  205. return rate;
  206. rate = clk->parent->rate;
  207. /* Otherwise, the parent must be a PLL */
  208. if (WARN_ON(!clk->parent->pll_data))
  209. return rate;
  210. pll = clk->parent->pll_data;
  211. /* If pre-PLL, source clock is before the multiplier and divider(s) */
  212. if (clk->flags & PRE_PLL)
  213. rate = pll->input_rate;
  214. if (!clk->div_reg)
  215. return rate;
  216. v = __raw_readl(pll->base + clk->div_reg);
  217. if (v & PLLDIV_EN) {
  218. plldiv = (v & PLLDIV_RATIO_MASK) + 1;
  219. if (plldiv)
  220. rate /= plldiv;
  221. }
  222. return rate;
  223. }
  224. static unsigned long clk_leafclk_recalc(struct clk *clk)
  225. {
  226. if (WARN_ON(!clk->parent))
  227. return clk->rate;
  228. return clk->parent->rate;
  229. }
  230. static unsigned long clk_pllclk_recalc(struct clk *clk)
  231. {
  232. u32 ctrl, mult = 1, prediv = 1, postdiv = 1;
  233. u8 bypass;
  234. struct pll_data *pll = clk->pll_data;
  235. unsigned long rate = clk->rate;
  236. pll->base = IO_ADDRESS(pll->phys_base);
  237. ctrl = __raw_readl(pll->base + PLLCTL);
  238. rate = pll->input_rate = clk->parent->rate;
  239. if (ctrl & PLLCTL_PLLEN) {
  240. bypass = 0;
  241. mult = __raw_readl(pll->base + PLLM);
  242. if (cpu_is_davinci_dm365())
  243. mult = 2 * (mult & PLLM_PLLM_MASK);
  244. else
  245. mult = (mult & PLLM_PLLM_MASK) + 1;
  246. } else
  247. bypass = 1;
  248. if (pll->flags & PLL_HAS_PREDIV) {
  249. prediv = __raw_readl(pll->base + PREDIV);
  250. if (prediv & PLLDIV_EN)
  251. prediv = (prediv & PLLDIV_RATIO_MASK) + 1;
  252. else
  253. prediv = 1;
  254. }
  255. /* pre-divider is fixed, but (some?) chips won't report that */
  256. if (cpu_is_davinci_dm355() && pll->num == 1)
  257. prediv = 8;
  258. if (pll->flags & PLL_HAS_POSTDIV) {
  259. postdiv = __raw_readl(pll->base + POSTDIV);
  260. if (postdiv & PLLDIV_EN)
  261. postdiv = (postdiv & PLLDIV_RATIO_MASK) + 1;
  262. else
  263. postdiv = 1;
  264. }
  265. if (!bypass) {
  266. rate /= prediv;
  267. rate *= mult;
  268. rate /= postdiv;
  269. }
  270. pr_debug("PLL%d: input = %lu MHz [ ",
  271. pll->num, clk->parent->rate / 1000000);
  272. if (bypass)
  273. pr_debug("bypass ");
  274. if (prediv > 1)
  275. pr_debug("/ %d ", prediv);
  276. if (mult > 1)
  277. pr_debug("* %d ", mult);
  278. if (postdiv > 1)
  279. pr_debug("/ %d ", postdiv);
  280. pr_debug("] --> %lu MHz output.\n", rate / 1000000);
  281. return rate;
  282. }
  283. /**
  284. * davinci_set_pllrate - set the output rate of a given PLL.
  285. *
  286. * Note: Currently tested to work with OMAP-L138 only.
  287. *
  288. * @pll: pll whose rate needs to be changed.
  289. * @prediv: The pre divider value. Passing 0 disables the pre-divider.
  290. * @pllm: The multiplier value. Passing 0 leads to multiply-by-one.
  291. * @postdiv: The post divider value. Passing 0 disables the post-divider.
  292. */
  293. int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv,
  294. unsigned int mult, unsigned int postdiv)
  295. {
  296. u32 ctrl;
  297. unsigned int locktime;
  298. if (pll->base == NULL)
  299. return -EINVAL;
  300. /*
  301. * PLL lock time required per OMAP-L138 datasheet is
  302. * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm)
  303. * as 4 and OSCIN cycle as 25 MHz.
  304. */
  305. if (prediv) {
  306. locktime = ((2000 * prediv) / 100);
  307. prediv = (prediv - 1) | PLLDIV_EN;
  308. } else {
  309. locktime = 20;
  310. }
  311. if (postdiv)
  312. postdiv = (postdiv - 1) | PLLDIV_EN;
  313. if (mult)
  314. mult = mult - 1;
  315. ctrl = __raw_readl(pll->base + PLLCTL);
  316. /* Switch the PLL to bypass mode */
  317. ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN);
  318. __raw_writel(ctrl, pll->base + PLLCTL);
  319. /*
  320. * Wait for 4 OSCIN/CLKIN cycles to ensure that the PLLC has switched
  321. * to bypass mode. Delay of 1us ensures we are good for all > 4MHz
  322. * OSCIN/CLKIN inputs. Typically the input is ~25MHz.
  323. */
  324. udelay(1);
  325. /* Reset and enable PLL */
  326. ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS);
  327. __raw_writel(ctrl, pll->base + PLLCTL);
  328. if (pll->flags & PLL_HAS_PREDIV)
  329. __raw_writel(prediv, pll->base + PREDIV);
  330. __raw_writel(mult, pll->base + PLLM);
  331. if (pll->flags & PLL_HAS_POSTDIV)
  332. __raw_writel(postdiv, pll->base + POSTDIV);
  333. /*
  334. * Wait for PLL to reset properly, OMAP-L138 datasheet says
  335. * 'min' time = 125ns
  336. */
  337. udelay(1);
  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. return 0;
  346. }
  347. EXPORT_SYMBOL(davinci_set_pllrate);
  348. int __init davinci_clk_init(struct davinci_clk *clocks)
  349. {
  350. struct davinci_clk *c;
  351. struct clk *clk;
  352. for (c = clocks; c->lk.clk; c++) {
  353. clk = c->lk.clk;
  354. if (!clk->recalc) {
  355. /* Check if clock is a PLL */
  356. if (clk->pll_data)
  357. clk->recalc = clk_pllclk_recalc;
  358. /* Else, if it is a PLL-derived clock */
  359. else if (clk->flags & CLK_PLL)
  360. clk->recalc = clk_sysclk_recalc;
  361. /* Otherwise, it is a leaf clock (PSC clock) */
  362. else if (clk->parent)
  363. clk->recalc = clk_leafclk_recalc;
  364. }
  365. if (clk->recalc)
  366. clk->rate = clk->recalc(clk);
  367. if (clk->lpsc)
  368. clk->flags |= CLK_PSC;
  369. clkdev_add(&c->lk);
  370. clk_register(clk);
  371. /* Turn on clocks that Linux doesn't otherwise manage */
  372. if (clk->flags & ALWAYS_ENABLED)
  373. clk_enable(clk);
  374. }
  375. return 0;
  376. }
  377. #ifdef CONFIG_PROC_FS
  378. #include <linux/proc_fs.h>
  379. #include <linux/seq_file.h>
  380. static void *davinci_ck_start(struct seq_file *m, loff_t *pos)
  381. {
  382. return *pos < 1 ? (void *)1 : NULL;
  383. }
  384. static void *davinci_ck_next(struct seq_file *m, void *v, loff_t *pos)
  385. {
  386. ++*pos;
  387. return NULL;
  388. }
  389. static void davinci_ck_stop(struct seq_file *m, void *v)
  390. {
  391. }
  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. /* Show clock tree; we know the main oscillator is first.
  425. * We trust nonzero usecounts equate to PSC enables...
  426. */
  427. mutex_lock(&clocks_mutex);
  428. if (!list_empty(&clocks))
  429. dump_clock(m, 0, list_first_entry(&clocks, struct clk, node));
  430. mutex_unlock(&clocks_mutex);
  431. return 0;
  432. }
  433. static const struct seq_operations davinci_ck_op = {
  434. .start = davinci_ck_start,
  435. .next = davinci_ck_next,
  436. .stop = davinci_ck_stop,
  437. .show = davinci_ck_show
  438. };
  439. static int davinci_ck_open(struct inode *inode, struct file *file)
  440. {
  441. return seq_open(file, &davinci_ck_op);
  442. }
  443. static const struct file_operations proc_davinci_ck_operations = {
  444. .open = davinci_ck_open,
  445. .read = seq_read,
  446. .llseek = seq_lseek,
  447. .release = seq_release,
  448. };
  449. static int __init davinci_ck_proc_init(void)
  450. {
  451. proc_create("davinci_clocks", 0, NULL, &proc_davinci_ck_operations);
  452. return 0;
  453. }
  454. __initcall(davinci_ck_proc_init);
  455. #endif /* CONFIG_DEBUG_PROC_FS */