clock34xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * OMAP3-specific clock framework functions
  3. *
  4. * Copyright (C) 2007-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2007-2008 Nokia Corporation
  6. *
  7. * Written by Paul Walmsley
  8. * Testing and integration fixes by Jouni Högander
  9. *
  10. * Parts of this code are based on code written by
  11. * Richard Woodruff, Tony Lindgren, Tuukka Tikkanen, Karthik Dasu
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #undef DEBUG
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/device.h>
  21. #include <linux/list.h>
  22. #include <linux/errno.h>
  23. #include <linux/delay.h>
  24. #include <linux/clk.h>
  25. #include <linux/io.h>
  26. #include <linux/limits.h>
  27. #include <linux/bitops.h>
  28. #include <mach/clock.h>
  29. #include <mach/sram.h>
  30. #include <asm/div64.h>
  31. #include "memory.h"
  32. #include "clock.h"
  33. #include "clock34xx.h"
  34. #include "prm.h"
  35. #include "prm-regbits-34xx.h"
  36. #include "cm.h"
  37. #include "cm-regbits-34xx.h"
  38. /* CM_AUTOIDLE_PLL*.AUTO_* bit values */
  39. #define DPLL_AUTOIDLE_DISABLE 0x0
  40. #define DPLL_AUTOIDLE_LOW_POWER_STOP 0x1
  41. #define MAX_DPLL_WAIT_TRIES 1000000
  42. /**
  43. * omap3_dpll_recalc - recalculate DPLL rate
  44. * @clk: DPLL struct clk
  45. *
  46. * Recalculate and propagate the DPLL rate.
  47. */
  48. static void omap3_dpll_recalc(struct clk *clk)
  49. {
  50. clk->rate = omap2_get_dpll_rate(clk);
  51. propagate_rate(clk);
  52. }
  53. /* _omap3_dpll_write_clken - write clken_bits arg to a DPLL's enable bits */
  54. static void _omap3_dpll_write_clken(struct clk *clk, u8 clken_bits)
  55. {
  56. const struct dpll_data *dd;
  57. u32 v;
  58. dd = clk->dpll_data;
  59. v = __raw_readl(dd->control_reg);
  60. v &= ~dd->enable_mask;
  61. v |= clken_bits << __ffs(dd->enable_mask);
  62. __raw_writel(v, dd->control_reg);
  63. }
  64. /* _omap3_wait_dpll_status: wait for a DPLL to enter a specific state */
  65. static int _omap3_wait_dpll_status(struct clk *clk, u8 state)
  66. {
  67. const struct dpll_data *dd;
  68. int i = 0;
  69. int ret = -EINVAL;
  70. u32 idlest_mask;
  71. dd = clk->dpll_data;
  72. state <<= dd->idlest_bit;
  73. idlest_mask = 1 << dd->idlest_bit;
  74. while (((__raw_readl(dd->idlest_reg) & idlest_mask) != state) &&
  75. i < MAX_DPLL_WAIT_TRIES) {
  76. i++;
  77. udelay(1);
  78. }
  79. if (i == MAX_DPLL_WAIT_TRIES) {
  80. printk(KERN_ERR "clock: %s failed transition to '%s'\n",
  81. clk->name, (state) ? "locked" : "bypassed");
  82. } else {
  83. pr_debug("clock: %s transition to '%s' in %d loops\n",
  84. clk->name, (state) ? "locked" : "bypassed", i);
  85. ret = 0;
  86. }
  87. return ret;
  88. }
  89. /* Non-CORE DPLL (e.g., DPLLs that do not control SDRC) clock functions */
  90. /*
  91. * _omap3_noncore_dpll_lock - instruct a DPLL to lock and wait for readiness
  92. * @clk: pointer to a DPLL struct clk
  93. *
  94. * Instructs a non-CORE DPLL to lock. Waits for the DPLL to report
  95. * readiness before returning. Will save and restore the DPLL's
  96. * autoidle state across the enable, per the CDP code. If the DPLL
  97. * locked successfully, return 0; if the DPLL did not lock in the time
  98. * allotted, or DPLL3 was passed in, return -EINVAL.
  99. */
  100. static int _omap3_noncore_dpll_lock(struct clk *clk)
  101. {
  102. u8 ai;
  103. int r;
  104. if (clk == &dpll3_ck)
  105. return -EINVAL;
  106. pr_debug("clock: locking DPLL %s\n", clk->name);
  107. ai = omap3_dpll_autoidle_read(clk);
  108. _omap3_dpll_write_clken(clk, DPLL_LOCKED);
  109. if (ai) {
  110. /*
  111. * If no downstream clocks are enabled, CM_IDLEST bit
  112. * may never become active, so don't wait for DPLL to lock.
  113. */
  114. r = 0;
  115. omap3_dpll_allow_idle(clk);
  116. } else {
  117. r = _omap3_wait_dpll_status(clk, 1);
  118. omap3_dpll_deny_idle(clk);
  119. };
  120. return r;
  121. }
  122. /*
  123. * omap3_noncore_dpll_bypass - instruct a DPLL to bypass and wait for readiness
  124. * @clk: pointer to a DPLL struct clk
  125. *
  126. * Instructs a non-CORE DPLL to enter low-power bypass mode. In
  127. * bypass mode, the DPLL's rate is set equal to its parent clock's
  128. * rate. Waits for the DPLL to report readiness before returning.
  129. * Will save and restore the DPLL's autoidle state across the enable,
  130. * per the CDP code. If the DPLL entered bypass mode successfully,
  131. * return 0; if the DPLL did not enter bypass in the time allotted, or
  132. * DPLL3 was passed in, or the DPLL does not support low-power bypass,
  133. * return -EINVAL.
  134. */
  135. static int _omap3_noncore_dpll_bypass(struct clk *clk)
  136. {
  137. int r;
  138. u8 ai;
  139. if (clk == &dpll3_ck)
  140. return -EINVAL;
  141. if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_BYPASS)))
  142. return -EINVAL;
  143. pr_debug("clock: configuring DPLL %s for low-power bypass\n",
  144. clk->name);
  145. ai = omap3_dpll_autoidle_read(clk);
  146. _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_BYPASS);
  147. r = _omap3_wait_dpll_status(clk, 0);
  148. if (ai)
  149. omap3_dpll_allow_idle(clk);
  150. else
  151. omap3_dpll_deny_idle(clk);
  152. return r;
  153. }
  154. /*
  155. * _omap3_noncore_dpll_stop - instruct a DPLL to stop
  156. * @clk: pointer to a DPLL struct clk
  157. *
  158. * Instructs a non-CORE DPLL to enter low-power stop. Will save and
  159. * restore the DPLL's autoidle state across the stop, per the CDP
  160. * code. If DPLL3 was passed in, or the DPLL does not support
  161. * low-power stop, return -EINVAL; otherwise, return 0.
  162. */
  163. static int _omap3_noncore_dpll_stop(struct clk *clk)
  164. {
  165. u8 ai;
  166. if (clk == &dpll3_ck)
  167. return -EINVAL;
  168. if (!(clk->dpll_data->modes & (1 << DPLL_LOW_POWER_STOP)))
  169. return -EINVAL;
  170. pr_debug("clock: stopping DPLL %s\n", clk->name);
  171. ai = omap3_dpll_autoidle_read(clk);
  172. _omap3_dpll_write_clken(clk, DPLL_LOW_POWER_STOP);
  173. if (ai)
  174. omap3_dpll_allow_idle(clk);
  175. else
  176. omap3_dpll_deny_idle(clk);
  177. return 0;
  178. }
  179. /**
  180. * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
  181. * @clk: pointer to a DPLL struct clk
  182. *
  183. * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
  184. * The choice of modes depends on the DPLL's programmed rate: if it is
  185. * the same as the DPLL's parent clock, it will enter bypass;
  186. * otherwise, it will enter lock. This code will wait for the DPLL to
  187. * indicate readiness before returning, unless the DPLL takes too long
  188. * to enter the target state. Intended to be used as the struct clk's
  189. * enable function. If DPLL3 was passed in, or the DPLL does not
  190. * support low-power stop, or if the DPLL took too long to enter
  191. * bypass or lock, return -EINVAL; otherwise, return 0.
  192. */
  193. static int omap3_noncore_dpll_enable(struct clk *clk)
  194. {
  195. int r;
  196. if (clk == &dpll3_ck)
  197. return -EINVAL;
  198. if (clk->parent->rate == clk_get_rate(clk))
  199. r = _omap3_noncore_dpll_bypass(clk);
  200. else
  201. r = _omap3_noncore_dpll_lock(clk);
  202. return r;
  203. }
  204. /**
  205. * omap3_noncore_dpll_enable - instruct a DPLL to enter bypass or lock mode
  206. * @clk: pointer to a DPLL struct clk
  207. *
  208. * Instructs a non-CORE DPLL to enable, e.g., to enter bypass or lock.
  209. * The choice of modes depends on the DPLL's programmed rate: if it is
  210. * the same as the DPLL's parent clock, it will enter bypass;
  211. * otherwise, it will enter lock. This code will wait for the DPLL to
  212. * indicate readiness before returning, unless the DPLL takes too long
  213. * to enter the target state. Intended to be used as the struct clk's
  214. * enable function. If DPLL3 was passed in, or the DPLL does not
  215. * support low-power stop, or if the DPLL took too long to enter
  216. * bypass or lock, return -EINVAL; otherwise, return 0.
  217. */
  218. static void omap3_noncore_dpll_disable(struct clk *clk)
  219. {
  220. if (clk == &dpll3_ck)
  221. return;
  222. _omap3_noncore_dpll_stop(clk);
  223. }
  224. /**
  225. * omap3_dpll_autoidle_read - read a DPLL's autoidle bits
  226. * @clk: struct clk * of the DPLL to read
  227. *
  228. * Return the DPLL's autoidle bits, shifted down to bit 0. Returns
  229. * -EINVAL if passed a null pointer or if the struct clk does not
  230. * appear to refer to a DPLL.
  231. */
  232. static u32 omap3_dpll_autoidle_read(struct clk *clk)
  233. {
  234. const struct dpll_data *dd;
  235. u32 v;
  236. if (!clk || !clk->dpll_data)
  237. return -EINVAL;
  238. dd = clk->dpll_data;
  239. v = __raw_readl(dd->autoidle_reg);
  240. v &= dd->autoidle_mask;
  241. v >>= __ffs(dd->autoidle_mask);
  242. return v;
  243. }
  244. /**
  245. * omap3_dpll_allow_idle - enable DPLL autoidle bits
  246. * @clk: struct clk * of the DPLL to operate on
  247. *
  248. * Enable DPLL automatic idle control. This automatic idle mode
  249. * switching takes effect only when the DPLL is locked, at least on
  250. * OMAP3430. The DPLL will enter low-power stop when its downstream
  251. * clocks are gated. No return value.
  252. */
  253. static void omap3_dpll_allow_idle(struct clk *clk)
  254. {
  255. const struct dpll_data *dd;
  256. u32 v;
  257. if (!clk || !clk->dpll_data)
  258. return;
  259. dd = clk->dpll_data;
  260. /*
  261. * REVISIT: CORE DPLL can optionally enter low-power bypass
  262. * by writing 0x5 instead of 0x1. Add some mechanism to
  263. * optionally enter this mode.
  264. */
  265. v = __raw_readl(dd->autoidle_reg);
  266. v &= ~dd->autoidle_mask;
  267. v |= DPLL_AUTOIDLE_LOW_POWER_STOP << __ffs(dd->autoidle_mask);
  268. __raw_writel(v, dd->autoidle_reg);
  269. }
  270. /**
  271. * omap3_dpll_deny_idle - prevent DPLL from automatically idling
  272. * @clk: struct clk * of the DPLL to operate on
  273. *
  274. * Disable DPLL automatic idle control. No return value.
  275. */
  276. static void omap3_dpll_deny_idle(struct clk *clk)
  277. {
  278. const struct dpll_data *dd;
  279. u32 v;
  280. if (!clk || !clk->dpll_data)
  281. return;
  282. dd = clk->dpll_data;
  283. v = __raw_readl(dd->autoidle_reg);
  284. v &= ~dd->autoidle_mask;
  285. v |= DPLL_AUTOIDLE_DISABLE << __ffs(dd->autoidle_mask);
  286. __raw_writel(v, dd->autoidle_reg);
  287. }
  288. /* Clock control for DPLL outputs */
  289. /**
  290. * omap3_clkoutx2_recalc - recalculate DPLL X2 output virtual clock rate
  291. * @clk: DPLL output struct clk
  292. *
  293. * Using parent clock DPLL data, look up DPLL state. If locked, set our
  294. * rate to the dpll_clk * 2; otherwise, just use dpll_clk.
  295. */
  296. static void omap3_clkoutx2_recalc(struct clk *clk)
  297. {
  298. const struct dpll_data *dd;
  299. u32 v;
  300. struct clk *pclk;
  301. /* Walk up the parents of clk, looking for a DPLL */
  302. pclk = clk->parent;
  303. while (pclk && !pclk->dpll_data)
  304. pclk = pclk->parent;
  305. /* clk does not have a DPLL as a parent? */
  306. WARN_ON(!pclk);
  307. dd = pclk->dpll_data;
  308. WARN_ON(!dd->control_reg || !dd->enable_mask);
  309. v = __raw_readl(dd->control_reg) & dd->enable_mask;
  310. v >>= __ffs(dd->enable_mask);
  311. if (v != DPLL_LOCKED)
  312. clk->rate = clk->parent->rate;
  313. else
  314. clk->rate = clk->parent->rate * 2;
  315. if (clk->flags & RATE_PROPAGATES)
  316. propagate_rate(clk);
  317. }
  318. /* Common clock code */
  319. /*
  320. * As it is structured now, this will prevent an OMAP2/3 multiboot
  321. * kernel from compiling. This will need further attention.
  322. */
  323. #if defined(CONFIG_ARCH_OMAP3)
  324. static struct clk_functions omap2_clk_functions = {
  325. .clk_enable = omap2_clk_enable,
  326. .clk_disable = omap2_clk_disable,
  327. .clk_round_rate = omap2_clk_round_rate,
  328. .clk_set_rate = omap2_clk_set_rate,
  329. .clk_set_parent = omap2_clk_set_parent,
  330. .clk_disable_unused = omap2_clk_disable_unused,
  331. };
  332. /*
  333. * Set clocks for bypass mode for reboot to work.
  334. */
  335. void omap2_clk_prepare_for_reboot(void)
  336. {
  337. /* REVISIT: Not ready for 343x */
  338. #if 0
  339. u32 rate;
  340. if (vclk == NULL || sclk == NULL)
  341. return;
  342. rate = clk_get_rate(sclk);
  343. clk_set_rate(vclk, rate);
  344. #endif
  345. }
  346. /* REVISIT: Move this init stuff out into clock.c */
  347. /*
  348. * Switch the MPU rate if specified on cmdline.
  349. * We cannot do this early until cmdline is parsed.
  350. */
  351. static int __init omap2_clk_arch_init(void)
  352. {
  353. if (!mpurate)
  354. return -EINVAL;
  355. /* REVISIT: not yet ready for 343x */
  356. #if 0
  357. if (omap2_select_table_rate(&virt_prcm_set, mpurate))
  358. printk(KERN_ERR "Could not find matching MPU rate\n");
  359. #endif
  360. recalculate_root_clocks();
  361. printk(KERN_INFO "Switched to new clocking rate (Crystal/DPLL3/MPU): "
  362. "%ld.%01ld/%ld/%ld MHz\n",
  363. (osc_sys_ck.rate / 1000000), (osc_sys_ck.rate / 100000) % 10,
  364. (core_ck.rate / 1000000), (dpll1_fck.rate / 1000000)) ;
  365. return 0;
  366. }
  367. arch_initcall(omap2_clk_arch_init);
  368. int __init omap2_clk_init(void)
  369. {
  370. /* struct prcm_config *prcm; */
  371. struct clk **clkp;
  372. /* u32 clkrate; */
  373. u32 cpu_clkflg;
  374. /* REVISIT: Ultimately this will be used for multiboot */
  375. #if 0
  376. if (cpu_is_omap242x()) {
  377. cpu_mask = RATE_IN_242X;
  378. cpu_clkflg = CLOCK_IN_OMAP242X;
  379. clkp = onchip_24xx_clks;
  380. } else if (cpu_is_omap2430()) {
  381. cpu_mask = RATE_IN_243X;
  382. cpu_clkflg = CLOCK_IN_OMAP243X;
  383. clkp = onchip_24xx_clks;
  384. }
  385. #endif
  386. if (cpu_is_omap34xx()) {
  387. cpu_mask = RATE_IN_343X;
  388. cpu_clkflg = CLOCK_IN_OMAP343X;
  389. clkp = onchip_34xx_clks;
  390. /*
  391. * Update this if there are further clock changes between ES2
  392. * and production parts
  393. */
  394. if (is_sil_rev_equal_to(OMAP3430_REV_ES1_0)) {
  395. /* No 3430ES1-only rates exist, so no RATE_IN_3430ES1 */
  396. cpu_clkflg |= CLOCK_IN_OMAP3430ES1;
  397. } else {
  398. cpu_mask |= RATE_IN_3430ES2;
  399. cpu_clkflg |= CLOCK_IN_OMAP3430ES2;
  400. }
  401. }
  402. clk_init(&omap2_clk_functions);
  403. for (clkp = onchip_34xx_clks;
  404. clkp < onchip_34xx_clks + ARRAY_SIZE(onchip_34xx_clks);
  405. clkp++) {
  406. if ((*clkp)->flags & cpu_clkflg) {
  407. clk_register(*clkp);
  408. omap2_init_clk_clkdm(*clkp);
  409. }
  410. }
  411. /* REVISIT: Not yet ready for OMAP3 */
  412. #if 0
  413. /* Check the MPU rate set by bootloader */
  414. clkrate = omap2_get_dpll_rate_24xx(&dpll_ck);
  415. for (prcm = rate_table; prcm->mpu_speed; prcm++) {
  416. if (!(prcm->flags & cpu_mask))
  417. continue;
  418. if (prcm->xtal_speed != sys_ck.rate)
  419. continue;
  420. if (prcm->dpll_speed <= clkrate)
  421. break;
  422. }
  423. curr_prcm_set = prcm;
  424. #endif
  425. recalculate_root_clocks();
  426. printk(KERN_INFO "Clocking rate (Crystal/DPLL/ARM core): "
  427. "%ld.%01ld/%ld/%ld MHz\n",
  428. (osc_sys_ck.rate / 1000000), (osc_sys_ck.rate / 100000) % 10,
  429. (core_ck.rate / 1000000), (arm_fck.rate / 1000000));
  430. /*
  431. * Only enable those clocks we will need, let the drivers
  432. * enable other clocks as necessary
  433. */
  434. clk_enable_init_clocks();
  435. /* Avoid sleeping during omap2_clk_prepare_for_reboot() */
  436. /* REVISIT: not yet ready for 343x */
  437. #if 0
  438. vclk = clk_get(NULL, "virt_prcm_set");
  439. sclk = clk_get(NULL, "sys_ck");
  440. #endif
  441. return 0;
  442. }
  443. #endif