clkt2xxx_dpllcore.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * DPLL + CORE_CLK composite clock functions
  3. *
  4. * Copyright (C) 2005-2008 Texas Instruments, Inc.
  5. * Copyright (C) 2004-2010 Nokia Corporation
  6. *
  7. * Contacts:
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Paul Walmsley
  10. *
  11. * Based on earlier work by Tuukka Tikkanen, Tony Lindgren,
  12. * Gordon McNutt and RidgeRun, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. *
  18. * XXX The DPLL and CORE clocks should be split into two separate clock
  19. * types.
  20. */
  21. #undef DEBUG
  22. #include <linux/kernel.h>
  23. #include <linux/errno.h>
  24. #include <linux/clk.h>
  25. #include <linux/io.h>
  26. #include "../plat-omap/sram.h"
  27. #include "clock.h"
  28. #include "clock2xxx.h"
  29. #include "opp2xxx.h"
  30. #include "cm2xxx.h"
  31. #include "cm-regbits-24xx.h"
  32. #include "sdrc.h"
  33. /* #define DOWN_VARIABLE_DPLL 1 */ /* Experimental */
  34. /*
  35. * dpll_core_ck: pointer to the combined dpll_ck + core_ck on OMAP2xxx
  36. * (currently defined as "dpll_ck" in the OMAP2xxx clock tree). Set
  37. * during dpll_ck init and used later by omap2xxx_clk_get_core_rate().
  38. */
  39. #ifdef CONFIG_COMMON_CLK
  40. static struct clk_hw_omap *dpll_core_ck;
  41. #else
  42. static struct clk *dpll_core_ck;
  43. #endif
  44. /**
  45. * omap2xxx_clk_get_core_rate - return the CORE_CLK rate
  46. *
  47. * Returns the CORE_CLK rate. CORE_CLK can have one of three rate
  48. * sources on OMAP2xxx: the DPLL CLKOUT rate, DPLL CLKOUTX2, or 32KHz
  49. * (the latter is unusual). This currently should be called with
  50. * struct clk *dpll_ck, which is a composite clock of dpll_ck and
  51. * core_ck.
  52. */
  53. unsigned long omap2xxx_clk_get_core_rate(void)
  54. {
  55. long long core_clk;
  56. u32 v;
  57. WARN_ON(!dpll_core_ck);
  58. core_clk = omap2_get_dpll_rate(dpll_core_ck);
  59. v = omap2_cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  60. v &= OMAP24XX_CORE_CLK_SRC_MASK;
  61. if (v == CORE_CLK_SRC_32K)
  62. core_clk = 32768;
  63. else
  64. core_clk *= v;
  65. return core_clk;
  66. }
  67. /*
  68. * Uses the current prcm set to tell if a rate is valid.
  69. * You can go slower, but not faster within a given rate set.
  70. */
  71. static long omap2_dpllcore_round_rate(unsigned long target_rate)
  72. {
  73. u32 high, low, core_clk_src;
  74. core_clk_src = omap2_cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  75. core_clk_src &= OMAP24XX_CORE_CLK_SRC_MASK;
  76. if (core_clk_src == CORE_CLK_SRC_DPLL) { /* DPLL clockout */
  77. high = curr_prcm_set->dpll_speed * 2;
  78. low = curr_prcm_set->dpll_speed;
  79. } else { /* DPLL clockout x 2 */
  80. high = curr_prcm_set->dpll_speed;
  81. low = curr_prcm_set->dpll_speed / 2;
  82. }
  83. #ifdef DOWN_VARIABLE_DPLL
  84. if (target_rate > high)
  85. return high;
  86. else
  87. return target_rate;
  88. #else
  89. if (target_rate > low)
  90. return high;
  91. else
  92. return low;
  93. #endif
  94. }
  95. #ifdef CONFIG_COMMON_CLK
  96. unsigned long omap2_dpllcore_recalc(struct clk_hw *hw,
  97. unsigned long parent_rate)
  98. #else
  99. unsigned long omap2_dpllcore_recalc(struct clk *clk)
  100. #endif
  101. {
  102. return omap2xxx_clk_get_core_rate();
  103. }
  104. #ifdef CONFIG_COMMON_CLK
  105. int omap2_reprogram_dpllcore(struct clk_hw *hw, unsigned long rate,
  106. unsigned long parent_rate)
  107. {
  108. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  109. #else
  110. int omap2_reprogram_dpllcore(struct clk *clk, unsigned long rate)
  111. {
  112. #endif
  113. u32 cur_rate, low, mult, div, valid_rate, done_rate;
  114. u32 bypass = 0;
  115. struct prcm_config tmpset;
  116. const struct dpll_data *dd;
  117. cur_rate = omap2xxx_clk_get_core_rate();
  118. mult = omap2_cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  119. mult &= OMAP24XX_CORE_CLK_SRC_MASK;
  120. if ((rate == (cur_rate / 2)) && (mult == 2)) {
  121. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
  122. } else if ((rate == (cur_rate * 2)) && (mult == 1)) {
  123. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
  124. } else if (rate != cur_rate) {
  125. valid_rate = omap2_dpllcore_round_rate(rate);
  126. if (valid_rate != rate)
  127. return -EINVAL;
  128. if (mult == 1)
  129. low = curr_prcm_set->dpll_speed;
  130. else
  131. low = curr_prcm_set->dpll_speed / 2;
  132. dd = clk->dpll_data;
  133. if (!dd)
  134. return -EINVAL;
  135. tmpset.cm_clksel1_pll = __raw_readl(dd->mult_div1_reg);
  136. tmpset.cm_clksel1_pll &= ~(dd->mult_mask |
  137. dd->div1_mask);
  138. div = ((curr_prcm_set->xtal_speed / 1000000) - 1);
  139. tmpset.cm_clksel2_pll = omap2_cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  140. tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK;
  141. if (rate > low) {
  142. tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2;
  143. mult = ((rate / 2) / 1000000);
  144. done_rate = CORE_CLK_SRC_DPLL_X2;
  145. } else {
  146. tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL;
  147. mult = (rate / 1000000);
  148. done_rate = CORE_CLK_SRC_DPLL;
  149. }
  150. tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask));
  151. tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask));
  152. /* Worst case */
  153. tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS;
  154. if (rate == curr_prcm_set->xtal_speed) /* If asking for 1-1 */
  155. bypass = 1;
  156. /* For omap2xxx_sdrc_init_params() */
  157. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
  158. /* Force dll lock mode */
  159. omap2_set_prcm(tmpset.cm_clksel1_pll, tmpset.base_sdrc_rfr,
  160. bypass);
  161. /* Errata: ret dll entry state */
  162. omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
  163. omap2xxx_sdrc_reprogram(done_rate, 0);
  164. }
  165. return 0;
  166. }
  167. /**
  168. * omap2xxx_clkt_dpllcore_init - clk init function for dpll_ck
  169. * @clk: struct clk *dpll_ck
  170. *
  171. * Store a local copy of @clk in dpll_core_ck so other code can query
  172. * the core rate without having to clk_get(), which can sleep. Must
  173. * only be called once. No return value. XXX If the clock
  174. * registration process is ever changed such that dpll_ck is no longer
  175. * statically defined, this code may need to change to increment some
  176. * kind of use count on dpll_ck.
  177. */
  178. #ifdef CONFIG_COMMON_CLK
  179. void omap2xxx_clkt_dpllcore_init(struct clk_hw *hw)
  180. #else
  181. void omap2xxx_clkt_dpllcore_init(struct clk *clk)
  182. #endif
  183. {
  184. WARN(dpll_core_ck, "dpll_core_ck already set - should never happen");
  185. #ifdef CONFIG_COMMON_CLK
  186. dpll_core_ck = to_clk_hw_omap(hw);
  187. #else
  188. dpll_core_ck = clk;
  189. #endif
  190. }