clock34xx.c 14 KB

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