clock.c 15 KB

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