dpll44xx.c 5.2 KB

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