clock.c 14 KB

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