clock34xx.c 13 KB

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