dpll44xx.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * OMAP4-specific DPLL control functions
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Rajendra Nayak
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/clk.h>
  14. #include <linux/io.h>
  15. #include <linux/bitops.h>
  16. #include "soc.h"
  17. #include "clock.h"
  18. #include "clock44xx.h"
  19. #include "cm-regbits-44xx.h"
  20. /*
  21. * Maximum DPLL input frequency (FINT) and output frequency (FOUT) that
  22. * can supported when using the DPLL low-power mode. Frequencies are
  23. * defined in OMAP4430/60 Public TRM section 3.6.3.3.2 "Enable Control,
  24. * Status, and Low-Power Operation Mode".
  25. */
  26. #define OMAP4_DPLL_LP_FINT_MAX 1000000
  27. #define OMAP4_DPLL_LP_FOUT_MAX 100000000
  28. /* Supported only on OMAP4 */
  29. int omap4_dpllmx_gatectrl_read(struct clk_hw_omap *clk)
  30. {
  31. u32 v;
  32. u32 mask;
  33. if (!clk || !clk->clksel_reg || !cpu_is_omap44xx())
  34. return -EINVAL;
  35. mask = clk->flags & CLOCK_CLKOUTX2 ?
  36. OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
  37. OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
  38. v = __raw_readl(clk->clksel_reg);
  39. v &= mask;
  40. v >>= __ffs(mask);
  41. return v;
  42. }
  43. void omap4_dpllmx_allow_gatectrl(struct clk_hw_omap *clk)
  44. {
  45. u32 v;
  46. u32 mask;
  47. if (!clk || !clk->clksel_reg || !cpu_is_omap44xx())
  48. return;
  49. mask = clk->flags & CLOCK_CLKOUTX2 ?
  50. OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
  51. OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
  52. v = __raw_readl(clk->clksel_reg);
  53. /* Clear the bit to allow gatectrl */
  54. v &= ~mask;
  55. __raw_writel(v, clk->clksel_reg);
  56. }
  57. void omap4_dpllmx_deny_gatectrl(struct clk_hw_omap *clk)
  58. {
  59. u32 v;
  60. u32 mask;
  61. if (!clk || !clk->clksel_reg || !cpu_is_omap44xx())
  62. return;
  63. mask = clk->flags & CLOCK_CLKOUTX2 ?
  64. OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
  65. OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
  66. v = __raw_readl(clk->clksel_reg);
  67. /* Set the bit to deny gatectrl */
  68. v |= mask;
  69. __raw_writel(v, clk->clksel_reg);
  70. }
  71. const struct clk_hw_omap_ops clkhwops_omap4_dpllmx = {
  72. .allow_idle = omap4_dpllmx_allow_gatectrl,
  73. .deny_idle = omap4_dpllmx_deny_gatectrl,
  74. };
  75. /**
  76. * omap4_dpll_lpmode_recalc - compute DPLL low-power setting
  77. * @dd: pointer to the dpll data structure
  78. *
  79. * Calculates if low-power mode can be enabled based upon the last
  80. * multiplier and divider values calculated. If low-power mode can be
  81. * enabled, then the bit to enable low-power mode is stored in the
  82. * last_rounded_lpmode variable. This implementation is based upon the
  83. * criteria for enabling low-power mode as described in the OMAP4430/60
  84. * Public TRM section 3.6.3.3.2 "Enable Control, Status, and Low-Power
  85. * Operation Mode".
  86. */
  87. static void omap4_dpll_lpmode_recalc(struct dpll_data *dd)
  88. {
  89. long fint, fout;
  90. fint = __clk_get_rate(dd->clk_ref) / (dd->last_rounded_n + 1);
  91. fout = fint * dd->last_rounded_m;
  92. if ((fint < OMAP4_DPLL_LP_FINT_MAX) && (fout < OMAP4_DPLL_LP_FOUT_MAX))
  93. dd->last_rounded_lpmode = 1;
  94. else
  95. dd->last_rounded_lpmode = 0;
  96. }
  97. /**
  98. * omap4_dpll_regm4xen_recalc - compute DPLL rate, considering REGM4XEN bit
  99. * @clk: struct clk * of the DPLL to compute the rate for
  100. *
  101. * Compute the output rate for the OMAP4 DPLL represented by @clk.
  102. * Takes the REGM4XEN bit into consideration, which is needed for the
  103. * OMAP4 ABE DPLL. Returns the DPLL's output rate (before M-dividers)
  104. * upon success, or 0 upon error.
  105. */
  106. unsigned long omap4_dpll_regm4xen_recalc(struct clk_hw *hw,
  107. unsigned long parent_rate)
  108. {
  109. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  110. u32 v;
  111. unsigned long rate;
  112. struct dpll_data *dd;
  113. if (!clk || !clk->dpll_data)
  114. return 0;
  115. dd = clk->dpll_data;
  116. rate = omap2_get_dpll_rate(clk);
  117. /* regm4xen adds a multiplier of 4 to DPLL calculations */
  118. v = __raw_readl(dd->control_reg);
  119. if (v & OMAP4430_DPLL_REGM4XEN_MASK)
  120. rate *= OMAP4430_REGM4XEN_MULT;
  121. return rate;
  122. }
  123. /**
  124. * omap4_dpll_regm4xen_round_rate - round DPLL rate, considering REGM4XEN bit
  125. * @clk: struct clk * of the DPLL to round a rate for
  126. * @target_rate: the desired rate of the DPLL
  127. *
  128. * Compute the rate that would be programmed into the DPLL hardware
  129. * for @clk if set_rate() were to be provided with the rate
  130. * @target_rate. Takes the REGM4XEN bit into consideration, which is
  131. * needed for the OMAP4 ABE DPLL. Returns the rounded rate (before
  132. * M-dividers) upon success, -EINVAL if @clk is null or not a DPLL, or
  133. * ~0 if an error occurred in omap2_dpll_round_rate().
  134. */
  135. long omap4_dpll_regm4xen_round_rate(struct clk_hw *hw,
  136. unsigned long target_rate,
  137. unsigned long *parent_rate)
  138. {
  139. struct clk_hw_omap *clk = to_clk_hw_omap(hw);
  140. struct dpll_data *dd;
  141. long r;
  142. if (!clk || !clk->dpll_data)
  143. return -EINVAL;
  144. dd = clk->dpll_data;
  145. dd->last_rounded_m4xen = 0;
  146. /*
  147. * First try to compute the DPLL configuration for
  148. * target rate without using the 4X multiplier.
  149. */
  150. r = omap2_dpll_round_rate(hw, target_rate, NULL);
  151. if (r != ~0)
  152. goto out;
  153. /*
  154. * If we did not find a valid DPLL configuration, try again, but
  155. * this time see if using the 4X multiplier can help. Enabling the
  156. * 4X multiplier is equivalent to dividing the target rate by 4.
  157. */
  158. r = omap2_dpll_round_rate(hw, target_rate / OMAP4430_REGM4XEN_MULT,
  159. NULL);
  160. if (r == ~0)
  161. return r;
  162. dd->last_rounded_rate *= OMAP4430_REGM4XEN_MULT;
  163. dd->last_rounded_m4xen = 1;
  164. out:
  165. omap4_dpll_lpmode_recalc(dd);
  166. return dd->last_rounded_rate;
  167. }