clkt2xxx_dpllcore.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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/clock.h>
  27. #include <plat/sram.h>
  28. #include <plat/sdrc.h>
  29. #include "clock.h"
  30. #include "clock2xxx.h"
  31. #include "opp2xxx.h"
  32. #include "cm.h"
  33. #include "cm-regbits-24xx.h"
  34. /* #define DOWN_VARIABLE_DPLL 1 */ /* Experimental */
  35. /**
  36. * omap2xxx_clk_get_core_rate - return the CORE_CLK rate
  37. * @clk: pointer to the combined dpll_ck + core_ck (currently "dpll_ck")
  38. *
  39. * Returns the CORE_CLK rate. CORE_CLK can have one of three rate
  40. * sources on OMAP2xxx: the DPLL CLKOUT rate, DPLL CLKOUTX2, or 32KHz
  41. * (the latter is unusual). This currently should be called with
  42. * struct clk *dpll_ck, which is a composite clock of dpll_ck and
  43. * core_ck.
  44. */
  45. unsigned long omap2xxx_clk_get_core_rate(struct clk *clk)
  46. {
  47. long long core_clk;
  48. u32 v;
  49. core_clk = omap2_get_dpll_rate(clk);
  50. v = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  51. v &= OMAP24XX_CORE_CLK_SRC_MASK;
  52. if (v == CORE_CLK_SRC_32K)
  53. core_clk = 32768;
  54. else
  55. core_clk *= v;
  56. return core_clk;
  57. }
  58. /*
  59. * Uses the current prcm set to tell if a rate is valid.
  60. * You can go slower, but not faster within a given rate set.
  61. */
  62. static long omap2_dpllcore_round_rate(unsigned long target_rate)
  63. {
  64. u32 high, low, core_clk_src;
  65. core_clk_src = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  66. core_clk_src &= OMAP24XX_CORE_CLK_SRC_MASK;
  67. if (core_clk_src == CORE_CLK_SRC_DPLL) { /* DPLL clockout */
  68. high = curr_prcm_set->dpll_speed * 2;
  69. low = curr_prcm_set->dpll_speed;
  70. } else { /* DPLL clockout x 2 */
  71. high = curr_prcm_set->dpll_speed;
  72. low = curr_prcm_set->dpll_speed / 2;
  73. }
  74. #ifdef DOWN_VARIABLE_DPLL
  75. if (target_rate > high)
  76. return high;
  77. else
  78. return target_rate;
  79. #else
  80. if (target_rate > low)
  81. return high;
  82. else
  83. return low;
  84. #endif
  85. }
  86. unsigned long omap2_dpllcore_recalc(struct clk *clk)
  87. {
  88. return omap2xxx_clk_get_core_rate(clk);
  89. }
  90. int omap2_reprogram_dpllcore(struct clk *clk, unsigned long rate)
  91. {
  92. u32 cur_rate, low, mult, div, valid_rate, done_rate;
  93. u32 bypass = 0;
  94. struct prcm_config tmpset;
  95. const struct dpll_data *dd;
  96. cur_rate = omap2xxx_clk_get_core_rate(dclk);
  97. mult = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  98. mult &= OMAP24XX_CORE_CLK_SRC_MASK;
  99. if ((rate == (cur_rate / 2)) && (mult == 2)) {
  100. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
  101. } else if ((rate == (cur_rate * 2)) && (mult == 1)) {
  102. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
  103. } else if (rate != cur_rate) {
  104. valid_rate = omap2_dpllcore_round_rate(rate);
  105. if (valid_rate != rate)
  106. return -EINVAL;
  107. if (mult == 1)
  108. low = curr_prcm_set->dpll_speed;
  109. else
  110. low = curr_prcm_set->dpll_speed / 2;
  111. dd = clk->dpll_data;
  112. if (!dd)
  113. return -EINVAL;
  114. tmpset.cm_clksel1_pll = __raw_readl(dd->mult_div1_reg);
  115. tmpset.cm_clksel1_pll &= ~(dd->mult_mask |
  116. dd->div1_mask);
  117. div = ((curr_prcm_set->xtal_speed / 1000000) - 1);
  118. tmpset.cm_clksel2_pll = cm_read_mod_reg(PLL_MOD, CM_CLKSEL2);
  119. tmpset.cm_clksel2_pll &= ~OMAP24XX_CORE_CLK_SRC_MASK;
  120. if (rate > low) {
  121. tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL_X2;
  122. mult = ((rate / 2) / 1000000);
  123. done_rate = CORE_CLK_SRC_DPLL_X2;
  124. } else {
  125. tmpset.cm_clksel2_pll |= CORE_CLK_SRC_DPLL;
  126. mult = (rate / 1000000);
  127. done_rate = CORE_CLK_SRC_DPLL;
  128. }
  129. tmpset.cm_clksel1_pll |= (div << __ffs(dd->mult_mask));
  130. tmpset.cm_clksel1_pll |= (mult << __ffs(dd->div1_mask));
  131. /* Worst case */
  132. tmpset.base_sdrc_rfr = SDRC_RFR_CTRL_BYPASS;
  133. if (rate == curr_prcm_set->xtal_speed) /* If asking for 1-1 */
  134. bypass = 1;
  135. /* For omap2xxx_sdrc_init_params() */
  136. omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
  137. /* Force dll lock mode */
  138. omap2_set_prcm(tmpset.cm_clksel1_pll, tmpset.base_sdrc_rfr,
  139. bypass);
  140. /* Errata: ret dll entry state */
  141. omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
  142. omap2xxx_sdrc_reprogram(done_rate, 0);
  143. }
  144. return 0;
  145. }