2 * Copyright © 2006-2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #include "intel_drv.h"
26 struct intel_shared_dpll *
27 intel_get_shared_dpll_by_id(struct drm_i915_private *dev_priv,
28 enum intel_dpll_id id)
30 return &dev_priv->shared_dplls[id];
34 intel_get_shared_dpll_id(struct drm_i915_private *dev_priv,
35 struct intel_shared_dpll *pll)
37 if (WARN_ON(pll < dev_priv->shared_dplls||
38 pll > &dev_priv->shared_dplls[dev_priv->num_shared_dpll]))
41 return (enum intel_dpll_id) (pll - dev_priv->shared_dplls);
45 intel_shared_dpll_config_get(struct intel_shared_dpll_config *config,
46 struct intel_shared_dpll *pll,
47 struct intel_crtc *crtc)
49 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
50 enum intel_dpll_id id = intel_get_shared_dpll_id(dev_priv, pll);
52 config[id].crtc_mask |= 1 << crtc->pipe;
56 intel_shared_dpll_config_put(struct intel_shared_dpll_config *config,
57 struct intel_shared_dpll *pll,
58 struct intel_crtc *crtc)
60 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
61 enum intel_dpll_id id = intel_get_shared_dpll_id(dev_priv, pll);
63 config[id].crtc_mask &= ~(1 << crtc->pipe);
67 void assert_shared_dpll(struct drm_i915_private *dev_priv,
68 struct intel_shared_dpll *pll,
72 struct intel_dpll_hw_state hw_state;
74 if (WARN(!pll, "asserting DPLL %s with no DPLL\n", onoff(state)))
77 cur_state = pll->funcs.get_hw_state(dev_priv, pll, &hw_state);
78 I915_STATE_WARN(cur_state != state,
79 "%s assertion failure (expected %s, current %s)\n",
80 pll->name, onoff(state), onoff(cur_state));
83 void intel_prepare_shared_dpll(struct intel_crtc *crtc)
85 struct drm_device *dev = crtc->base.dev;
86 struct drm_i915_private *dev_priv = dev->dev_private;
87 struct intel_shared_dpll *pll = crtc->config->shared_dpll;
89 if (WARN_ON(pll == NULL))
92 WARN_ON(!pll->config.crtc_mask);
93 if (pll->active_mask == 0) {
94 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
96 assert_shared_dpll_disabled(dev_priv, pll);
98 pll->funcs.mode_set(dev_priv, pll);
103 * intel_enable_shared_dpll - enable PCH PLL
104 * @dev_priv: i915 private structure
105 * @pipe: pipe PLL to enable
107 * The PCH PLL needs to be enabled before the PCH transcoder, since it
108 * drives the transcoder clock.
110 void intel_enable_shared_dpll(struct intel_crtc *crtc)
112 struct drm_device *dev = crtc->base.dev;
113 struct drm_i915_private *dev_priv = dev->dev_private;
114 struct intel_shared_dpll *pll = crtc->config->shared_dpll;
115 unsigned crtc_mask = 1 << drm_crtc_index(&crtc->base);
116 unsigned old_mask = pll->active_mask;
118 if (WARN_ON(pll == NULL))
121 if (WARN_ON(!(pll->config.crtc_mask & crtc_mask)) ||
122 WARN_ON(pll->active_mask & crtc_mask))
125 pll->active_mask |= crtc_mask;
127 DRM_DEBUG_KMS("enable %s (active %x, on? %d) for crtc %d\n",
128 pll->name, pll->active_mask, pll->on,
133 assert_shared_dpll_enabled(dev_priv, pll);
138 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
140 DRM_DEBUG_KMS("enabling %s\n", pll->name);
141 pll->funcs.enable(dev_priv, pll);
145 void intel_disable_shared_dpll(struct intel_crtc *crtc)
147 struct drm_device *dev = crtc->base.dev;
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 struct intel_shared_dpll *pll = crtc->config->shared_dpll;
150 unsigned crtc_mask = 1 << drm_crtc_index(&crtc->base);
152 /* PCH only available on ILK+ */
153 if (INTEL_INFO(dev)->gen < 5)
159 if (WARN_ON(!(pll->config.crtc_mask & crtc_mask)))
162 DRM_DEBUG_KMS("disable %s (active %x, on? %d) for crtc %d\n",
163 pll->name, pll->active_mask, pll->on,
166 assert_shared_dpll_enabled(dev_priv, pll);
169 pll->active_mask &= ~crtc_mask;
170 if (pll->active_mask)
173 DRM_DEBUG_KMS("disabling %s\n", pll->name);
174 pll->funcs.disable(dev_priv, pll);
177 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
180 static struct intel_shared_dpll *
181 intel_find_shared_dpll(struct intel_crtc *crtc,
182 struct intel_crtc_state *crtc_state,
183 enum intel_dpll_id range_min,
184 enum intel_dpll_id range_max)
186 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
187 struct intel_shared_dpll *pll;
188 struct intel_shared_dpll_config *shared_dpll;
189 enum intel_dpll_id i;
191 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
193 for (i = range_min; i <= range_max; i++) {
194 pll = &dev_priv->shared_dplls[i];
196 /* Only want to check enabled timings first */
197 if (shared_dpll[i].crtc_mask == 0)
200 if (memcmp(&crtc_state->dpll_hw_state,
201 &shared_dpll[i].hw_state,
202 sizeof(crtc_state->dpll_hw_state)) == 0) {
203 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, active %x)\n",
204 crtc->base.base.id, pll->name,
205 shared_dpll[i].crtc_mask,
211 /* Ok no matching timings, maybe there's a free one? */
212 for (i = range_min; i <= range_max; i++) {
213 pll = &dev_priv->shared_dplls[i];
214 if (shared_dpll[i].crtc_mask == 0) {
215 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
216 crtc->base.base.id, pll->name);
225 intel_reference_shared_dpll(struct intel_shared_dpll *pll,
226 struct intel_crtc_state *crtc_state)
228 struct intel_shared_dpll_config *shared_dpll;
229 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
230 enum intel_dpll_id i = pll->id;
232 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
234 if (shared_dpll[i].crtc_mask == 0)
235 shared_dpll[i].hw_state =
236 crtc_state->dpll_hw_state;
238 crtc_state->shared_dpll = pll;
239 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
240 pipe_name(crtc->pipe));
242 intel_shared_dpll_config_get(shared_dpll, pll, crtc);
245 void intel_shared_dpll_commit(struct drm_atomic_state *state)
247 struct drm_i915_private *dev_priv = to_i915(state->dev);
248 struct intel_shared_dpll_config *shared_dpll;
249 struct intel_shared_dpll *pll;
250 enum intel_dpll_id i;
252 if (!to_intel_atomic_state(state)->dpll_set)
255 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
256 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
257 pll = &dev_priv->shared_dplls[i];
258 pll->config = shared_dpll[i];
262 static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
263 struct intel_shared_dpll *pll,
264 struct intel_dpll_hw_state *hw_state)
268 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
271 val = I915_READ(PCH_DPLL(pll->id));
272 hw_state->dpll = val;
273 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
274 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
276 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
278 return val & DPLL_VCO_ENABLE;
281 static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
282 struct intel_shared_dpll *pll)
284 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
285 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
288 static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
293 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
295 val = I915_READ(PCH_DREF_CONTROL);
296 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
297 DREF_SUPERSPREAD_SOURCE_MASK));
298 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
301 static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
302 struct intel_shared_dpll *pll)
304 /* PCH refclock must be enabled first */
305 ibx_assert_pch_refclk_enabled(dev_priv);
307 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
309 /* Wait for the clocks to stabilize. */
310 POSTING_READ(PCH_DPLL(pll->id));
313 /* The pixel multiplier can only be updated once the
314 * DPLL is enabled and the clocks are stable.
318 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
319 POSTING_READ(PCH_DPLL(pll->id));
323 static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
324 struct intel_shared_dpll *pll)
326 struct drm_device *dev = dev_priv->dev;
327 struct intel_crtc *crtc;
329 /* Make sure no transcoder isn't still depending on us. */
330 for_each_intel_crtc(dev, crtc) {
331 if (crtc->config->shared_dpll == pll)
332 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
335 I915_WRITE(PCH_DPLL(pll->id), 0);
336 POSTING_READ(PCH_DPLL(pll->id));
340 static struct intel_shared_dpll *
341 ibx_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
342 struct intel_encoder *encoder)
344 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
345 struct intel_shared_dpll *pll;
346 enum intel_dpll_id i;
348 if (HAS_PCH_IBX(dev_priv)) {
349 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
350 i = (enum intel_dpll_id) crtc->pipe;
351 pll = &dev_priv->shared_dplls[i];
353 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
354 crtc->base.base.id, pll->name);
356 pll = intel_find_shared_dpll(crtc, crtc_state,
361 /* reference the pll */
362 intel_reference_shared_dpll(pll, crtc_state);
367 static const struct intel_shared_dpll_funcs ibx_pch_dpll_funcs = {
368 .mode_set = ibx_pch_dpll_mode_set,
369 .enable = ibx_pch_dpll_enable,
370 .disable = ibx_pch_dpll_disable,
371 .get_hw_state = ibx_pch_dpll_get_hw_state,
374 static void hsw_ddi_wrpll_enable(struct drm_i915_private *dev_priv,
375 struct intel_shared_dpll *pll)
377 I915_WRITE(WRPLL_CTL(pll->id), pll->config.hw_state.wrpll);
378 POSTING_READ(WRPLL_CTL(pll->id));
382 static void hsw_ddi_spll_enable(struct drm_i915_private *dev_priv,
383 struct intel_shared_dpll *pll)
385 I915_WRITE(SPLL_CTL, pll->config.hw_state.spll);
386 POSTING_READ(SPLL_CTL);
390 static void hsw_ddi_wrpll_disable(struct drm_i915_private *dev_priv,
391 struct intel_shared_dpll *pll)
395 val = I915_READ(WRPLL_CTL(pll->id));
396 I915_WRITE(WRPLL_CTL(pll->id), val & ~WRPLL_PLL_ENABLE);
397 POSTING_READ(WRPLL_CTL(pll->id));
400 static void hsw_ddi_spll_disable(struct drm_i915_private *dev_priv,
401 struct intel_shared_dpll *pll)
405 val = I915_READ(SPLL_CTL);
406 I915_WRITE(SPLL_CTL, val & ~SPLL_PLL_ENABLE);
407 POSTING_READ(SPLL_CTL);
410 static bool hsw_ddi_wrpll_get_hw_state(struct drm_i915_private *dev_priv,
411 struct intel_shared_dpll *pll,
412 struct intel_dpll_hw_state *hw_state)
416 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
419 val = I915_READ(WRPLL_CTL(pll->id));
420 hw_state->wrpll = val;
422 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
424 return val & WRPLL_PLL_ENABLE;
427 static bool hsw_ddi_spll_get_hw_state(struct drm_i915_private *dev_priv,
428 struct intel_shared_dpll *pll,
429 struct intel_dpll_hw_state *hw_state)
433 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
436 val = I915_READ(SPLL_CTL);
437 hw_state->spll = val;
439 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
441 return val & SPLL_PLL_ENABLE;
444 static uint32_t hsw_pll_to_ddi_pll_sel(struct intel_shared_dpll *pll)
448 return PORT_CLK_SEL_WRPLL1;
450 return PORT_CLK_SEL_WRPLL2;
452 return PORT_CLK_SEL_SPLL;
453 case DPLL_ID_LCPLL_810:
454 return PORT_CLK_SEL_LCPLL_810;
455 case DPLL_ID_LCPLL_1350:
456 return PORT_CLK_SEL_LCPLL_1350;
457 case DPLL_ID_LCPLL_2700:
458 return PORT_CLK_SEL_LCPLL_2700;
460 return PORT_CLK_SEL_NONE;
465 #define LC_FREQ_2K U64_C(LC_FREQ * 2000)
471 /* Constraints for PLL good behavior */
477 struct hsw_wrpll_rnp {
481 static unsigned hsw_wrpll_get_budget_for_freq(int clock)
555 static void hsw_wrpll_update_rnp(uint64_t freq2k, unsigned budget,
556 unsigned r2, unsigned n2, unsigned p,
557 struct hsw_wrpll_rnp *best)
559 uint64_t a, b, c, d, diff, diff_best;
561 /* No best (r,n,p) yet */
570 * Output clock is (LC_FREQ_2K / 2000) * N / (P * R), which compares to
574 * abs(freq2k - (LC_FREQ_2K * n2/(p * r2))) /
577 * and we would like delta <= budget.
579 * If the discrepancy is above the PPM-based budget, always prefer to
580 * improve upon the previous solution. However, if you're within the
581 * budget, try to maximize Ref * VCO, that is N / (P * R^2).
583 a = freq2k * budget * p * r2;
584 b = freq2k * budget * best->p * best->r2;
585 diff = abs_diff(freq2k * p * r2, LC_FREQ_2K * n2);
586 diff_best = abs_diff(freq2k * best->p * best->r2,
587 LC_FREQ_2K * best->n2);
589 d = 1000000 * diff_best;
591 if (a < c && b < d) {
592 /* If both are above the budget, pick the closer */
593 if (best->p * best->r2 * diff < p * r2 * diff_best) {
598 } else if (a >= c && b < d) {
599 /* If A is below the threshold but B is above it? Update. */
603 } else if (a >= c && b >= d) {
604 /* Both are below the limit, so pick the higher n2/(r2*r2) */
605 if (n2 * best->r2 * best->r2 > best->n2 * r2 * r2) {
611 /* Otherwise a < c && b >= d, do nothing */
615 hsw_ddi_calculate_wrpll(int clock /* in Hz */,
616 unsigned *r2_out, unsigned *n2_out, unsigned *p_out)
620 struct hsw_wrpll_rnp best = { 0, 0, 0 };
623 freq2k = clock / 100;
625 budget = hsw_wrpll_get_budget_for_freq(clock);
627 /* Special case handling for 540 pixel clock: bypass WR PLL entirely
628 * and directly pass the LC PLL to it. */
629 if (freq2k == 5400000) {
637 * Ref = LC_FREQ / R, where Ref is the actual reference input seen by
640 * We want R so that REF_MIN <= Ref <= REF_MAX.
641 * Injecting R2 = 2 * R gives:
642 * REF_MAX * r2 > LC_FREQ * 2 and
643 * REF_MIN * r2 < LC_FREQ * 2
645 * Which means the desired boundaries for r2 are:
646 * LC_FREQ * 2 / REF_MAX < r2 < LC_FREQ * 2 / REF_MIN
649 for (r2 = LC_FREQ * 2 / REF_MAX + 1;
650 r2 <= LC_FREQ * 2 / REF_MIN;
654 * VCO = N * Ref, that is: VCO = N * LC_FREQ / R
656 * Once again we want VCO_MIN <= VCO <= VCO_MAX.
657 * Injecting R2 = 2 * R and N2 = 2 * N, we get:
658 * VCO_MAX * r2 > n2 * LC_FREQ and
659 * VCO_MIN * r2 < n2 * LC_FREQ)
661 * Which means the desired boundaries for n2 are:
662 * VCO_MIN * r2 / LC_FREQ < n2 < VCO_MAX * r2 / LC_FREQ
664 for (n2 = VCO_MIN * r2 / LC_FREQ + 1;
665 n2 <= VCO_MAX * r2 / LC_FREQ;
668 for (p = P_MIN; p <= P_MAX; p += P_INC)
669 hsw_wrpll_update_rnp(freq2k, budget,
679 static struct intel_shared_dpll *
680 hsw_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
681 struct intel_encoder *encoder)
683 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
684 struct intel_shared_dpll *pll;
685 int clock = crtc_state->port_clock;
687 memset(&crtc_state->dpll_hw_state, 0,
688 sizeof(crtc_state->dpll_hw_state));
690 if (encoder->type == INTEL_OUTPUT_HDMI) {
694 hsw_ddi_calculate_wrpll(clock * 1000, &r2, &n2, &p);
696 val = WRPLL_PLL_ENABLE | WRPLL_PLL_LCPLL |
697 WRPLL_DIVIDER_REFERENCE(r2) | WRPLL_DIVIDER_FEEDBACK(n2) |
698 WRPLL_DIVIDER_POST(p);
700 crtc_state->dpll_hw_state.wrpll = val;
702 pll = intel_find_shared_dpll(crtc, crtc_state,
703 DPLL_ID_WRPLL1, DPLL_ID_WRPLL2);
705 } else if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
706 encoder->type == INTEL_OUTPUT_DP_MST ||
707 encoder->type == INTEL_OUTPUT_EDP) {
708 enum intel_dpll_id pll_id;
712 pll_id = DPLL_ID_LCPLL_810;
715 pll_id = DPLL_ID_LCPLL_1350;
718 pll_id = DPLL_ID_LCPLL_2700;
721 DRM_DEBUG_KMS("Invalid clock for DP: %d\n", clock);
725 pll = intel_get_shared_dpll_by_id(dev_priv, pll_id);
727 } else if (encoder->type == INTEL_OUTPUT_ANALOG) {
728 if (WARN_ON(crtc_state->port_clock / 2 != 135000))
731 crtc_state->dpll_hw_state.spll =
732 SPLL_PLL_ENABLE | SPLL_PLL_FREQ_1350MHz | SPLL_PLL_SSC;
734 pll = intel_find_shared_dpll(crtc, crtc_state,
735 DPLL_ID_SPLL, DPLL_ID_SPLL);
743 crtc_state->ddi_pll_sel = hsw_pll_to_ddi_pll_sel(pll);
745 intel_reference_shared_dpll(pll, crtc_state);
751 static const struct intel_shared_dpll_funcs hsw_ddi_wrpll_funcs = {
752 .enable = hsw_ddi_wrpll_enable,
753 .disable = hsw_ddi_wrpll_disable,
754 .get_hw_state = hsw_ddi_wrpll_get_hw_state,
757 static const struct intel_shared_dpll_funcs hsw_ddi_spll_funcs = {
758 .enable = hsw_ddi_spll_enable,
759 .disable = hsw_ddi_spll_disable,
760 .get_hw_state = hsw_ddi_spll_get_hw_state,
763 static void hsw_ddi_lcpll_enable(struct drm_i915_private *dev_priv,
764 struct intel_shared_dpll *pll)
768 static void hsw_ddi_lcpll_disable(struct drm_i915_private *dev_priv,
769 struct intel_shared_dpll *pll)
773 static bool hsw_ddi_lcpll_get_hw_state(struct drm_i915_private *dev_priv,
774 struct intel_shared_dpll *pll,
775 struct intel_dpll_hw_state *hw_state)
780 static const struct intel_shared_dpll_funcs hsw_ddi_lcpll_funcs = {
781 .enable = hsw_ddi_lcpll_enable,
782 .disable = hsw_ddi_lcpll_disable,
783 .get_hw_state = hsw_ddi_lcpll_get_hw_state,
786 struct skl_dpll_regs {
787 i915_reg_t ctl, cfgcr1, cfgcr2;
790 /* this array is indexed by the *shared* pll id */
791 static const struct skl_dpll_regs skl_dpll_regs[4] = {
795 /* DPLL 0 doesn't support HDMI mode */
800 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL1),
801 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL1),
806 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL2),
807 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL2),
812 .cfgcr1 = DPLL_CFGCR1(SKL_DPLL3),
813 .cfgcr2 = DPLL_CFGCR2(SKL_DPLL3),
817 static void skl_ddi_pll_write_ctrl1(struct drm_i915_private *dev_priv,
818 struct intel_shared_dpll *pll)
822 val = I915_READ(DPLL_CTRL1);
824 val &= ~(DPLL_CTRL1_HDMI_MODE(pll->id) | DPLL_CTRL1_SSC(pll->id) |
825 DPLL_CTRL1_LINK_RATE_MASK(pll->id));
826 val |= pll->config.hw_state.ctrl1 << (pll->id * 6);
828 I915_WRITE(DPLL_CTRL1, val);
829 POSTING_READ(DPLL_CTRL1);
832 static void skl_ddi_pll_enable(struct drm_i915_private *dev_priv,
833 struct intel_shared_dpll *pll)
835 const struct skl_dpll_regs *regs = skl_dpll_regs;
837 skl_ddi_pll_write_ctrl1(dev_priv, pll);
839 I915_WRITE(regs[pll->id].cfgcr1, pll->config.hw_state.cfgcr1);
840 I915_WRITE(regs[pll->id].cfgcr2, pll->config.hw_state.cfgcr2);
841 POSTING_READ(regs[pll->id].cfgcr1);
842 POSTING_READ(regs[pll->id].cfgcr2);
844 /* the enable bit is always bit 31 */
845 I915_WRITE(regs[pll->id].ctl,
846 I915_READ(regs[pll->id].ctl) | LCPLL_PLL_ENABLE);
848 if (wait_for(I915_READ(DPLL_STATUS) & DPLL_LOCK(pll->id), 5))
849 DRM_ERROR("DPLL %d not locked\n", pll->id);
852 static void skl_ddi_dpll0_enable(struct drm_i915_private *dev_priv,
853 struct intel_shared_dpll *pll)
855 skl_ddi_pll_write_ctrl1(dev_priv, pll);
858 static void skl_ddi_pll_disable(struct drm_i915_private *dev_priv,
859 struct intel_shared_dpll *pll)
861 const struct skl_dpll_regs *regs = skl_dpll_regs;
863 /* the enable bit is always bit 31 */
864 I915_WRITE(regs[pll->id].ctl,
865 I915_READ(regs[pll->id].ctl) & ~LCPLL_PLL_ENABLE);
866 POSTING_READ(regs[pll->id].ctl);
869 static void skl_ddi_dpll0_disable(struct drm_i915_private *dev_priv,
870 struct intel_shared_dpll *pll)
874 static bool skl_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
875 struct intel_shared_dpll *pll,
876 struct intel_dpll_hw_state *hw_state)
879 const struct skl_dpll_regs *regs = skl_dpll_regs;
882 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
887 val = I915_READ(regs[pll->id].ctl);
888 if (!(val & LCPLL_PLL_ENABLE))
891 val = I915_READ(DPLL_CTRL1);
892 hw_state->ctrl1 = (val >> (pll->id * 6)) & 0x3f;
894 /* avoid reading back stale values if HDMI mode is not enabled */
895 if (val & DPLL_CTRL1_HDMI_MODE(pll->id)) {
896 hw_state->cfgcr1 = I915_READ(regs[pll->id].cfgcr1);
897 hw_state->cfgcr2 = I915_READ(regs[pll->id].cfgcr2);
902 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
907 static bool skl_ddi_dpll0_get_hw_state(struct drm_i915_private *dev_priv,
908 struct intel_shared_dpll *pll,
909 struct intel_dpll_hw_state *hw_state)
912 const struct skl_dpll_regs *regs = skl_dpll_regs;
915 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
920 /* DPLL0 is always enabled since it drives CDCLK */
921 val = I915_READ(regs[pll->id].ctl);
922 if (WARN_ON(!(val & LCPLL_PLL_ENABLE)))
925 val = I915_READ(DPLL_CTRL1);
926 hw_state->ctrl1 = (val >> (pll->id * 6)) & 0x3f;
931 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
936 struct skl_wrpll_context {
937 uint64_t min_deviation; /* current minimal deviation */
938 uint64_t central_freq; /* chosen central freq */
939 uint64_t dco_freq; /* chosen dco freq */
940 unsigned int p; /* chosen divider */
943 static void skl_wrpll_context_init(struct skl_wrpll_context *ctx)
945 memset(ctx, 0, sizeof(*ctx));
947 ctx->min_deviation = U64_MAX;
950 /* DCO freq must be within +1%/-6% of the DCO central freq */
951 #define SKL_DCO_MAX_PDEVIATION 100
952 #define SKL_DCO_MAX_NDEVIATION 600
954 static void skl_wrpll_try_divider(struct skl_wrpll_context *ctx,
955 uint64_t central_freq,
957 unsigned int divider)
961 deviation = div64_u64(10000 * abs_diff(dco_freq, central_freq),
964 /* positive deviation */
965 if (dco_freq >= central_freq) {
966 if (deviation < SKL_DCO_MAX_PDEVIATION &&
967 deviation < ctx->min_deviation) {
968 ctx->min_deviation = deviation;
969 ctx->central_freq = central_freq;
970 ctx->dco_freq = dco_freq;
973 /* negative deviation */
974 } else if (deviation < SKL_DCO_MAX_NDEVIATION &&
975 deviation < ctx->min_deviation) {
976 ctx->min_deviation = deviation;
977 ctx->central_freq = central_freq;
978 ctx->dco_freq = dco_freq;
983 static void skl_wrpll_get_multipliers(unsigned int p,
984 unsigned int *p0 /* out */,
985 unsigned int *p1 /* out */,
986 unsigned int *p2 /* out */)
990 unsigned int half = p / 2;
992 if (half == 1 || half == 2 || half == 3 || half == 5) {
996 } else if (half % 2 == 0) {
1000 } else if (half % 3 == 0) {
1004 } else if (half % 7 == 0) {
1009 } else if (p == 3 || p == 9) { /* 3, 5, 7, 9, 15, 21, 35 */
1013 } else if (p == 5 || p == 7) {
1017 } else if (p == 15) {
1021 } else if (p == 21) {
1025 } else if (p == 35) {
1032 struct skl_wrpll_params {
1033 uint32_t dco_fraction;
1034 uint32_t dco_integer;
1035 uint32_t qdiv_ratio;
1039 uint32_t central_freq;
1042 static void skl_wrpll_params_populate(struct skl_wrpll_params *params,
1044 uint64_t central_freq,
1045 uint32_t p0, uint32_t p1, uint32_t p2)
1049 switch (central_freq) {
1051 params->central_freq = 0;
1054 params->central_freq = 1;
1057 params->central_freq = 3;
1074 WARN(1, "Incorrect PDiv\n");
1091 WARN(1, "Incorrect KDiv\n");
1094 params->qdiv_ratio = p1;
1095 params->qdiv_mode = (params->qdiv_ratio == 1) ? 0 : 1;
1097 dco_freq = p0 * p1 * p2 * afe_clock;
1100 * Intermediate values are in Hz.
1101 * Divide by MHz to match bsepc
1103 params->dco_integer = div_u64(dco_freq, 24 * MHz(1));
1104 params->dco_fraction =
1105 div_u64((div_u64(dco_freq, 24) -
1106 params->dco_integer * MHz(1)) * 0x8000, MHz(1));
1110 skl_ddi_calculate_wrpll(int clock /* in Hz */,
1111 struct skl_wrpll_params *wrpll_params)
1113 uint64_t afe_clock = clock * 5; /* AFE Clock is 5x Pixel clock */
1114 uint64_t dco_central_freq[3] = {8400000000ULL,
1117 static const int even_dividers[] = { 4, 6, 8, 10, 12, 14, 16, 18, 20,
1118 24, 28, 30, 32, 36, 40, 42, 44,
1119 48, 52, 54, 56, 60, 64, 66, 68,
1120 70, 72, 76, 78, 80, 84, 88, 90,
1122 static const int odd_dividers[] = { 3, 5, 7, 9, 15, 21, 35 };
1123 static const struct {
1127 { even_dividers, ARRAY_SIZE(even_dividers) },
1128 { odd_dividers, ARRAY_SIZE(odd_dividers) },
1130 struct skl_wrpll_context ctx;
1131 unsigned int dco, d, i;
1132 unsigned int p0, p1, p2;
1134 skl_wrpll_context_init(&ctx);
1136 for (d = 0; d < ARRAY_SIZE(dividers); d++) {
1137 for (dco = 0; dco < ARRAY_SIZE(dco_central_freq); dco++) {
1138 for (i = 0; i < dividers[d].n_dividers; i++) {
1139 unsigned int p = dividers[d].list[i];
1140 uint64_t dco_freq = p * afe_clock;
1142 skl_wrpll_try_divider(&ctx,
1143 dco_central_freq[dco],
1147 * Skip the remaining dividers if we're sure to
1148 * have found the definitive divider, we can't
1149 * improve a 0 deviation.
1151 if (ctx.min_deviation == 0)
1152 goto skip_remaining_dividers;
1156 skip_remaining_dividers:
1158 * If a solution is found with an even divider, prefer
1161 if (d == 0 && ctx.p)
1166 DRM_DEBUG_DRIVER("No valid divider found for %dHz\n", clock);
1171 * gcc incorrectly analyses that these can be used without being
1172 * initialized. To be fair, it's hard to guess.
1175 skl_wrpll_get_multipliers(ctx.p, &p0, &p1, &p2);
1176 skl_wrpll_params_populate(wrpll_params, afe_clock, ctx.central_freq,
1182 static struct intel_shared_dpll *
1183 skl_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
1184 struct intel_encoder *encoder)
1186 struct intel_shared_dpll *pll;
1187 uint32_t ctrl1, cfgcr1, cfgcr2;
1188 int clock = crtc_state->port_clock;
1191 * See comment in intel_dpll_hw_state to understand why we always use 0
1192 * as the DPLL id in this function.
1195 ctrl1 = DPLL_CTRL1_OVERRIDE(0);
1197 if (encoder->type == INTEL_OUTPUT_HDMI) {
1198 struct skl_wrpll_params wrpll_params = { 0, };
1200 ctrl1 |= DPLL_CTRL1_HDMI_MODE(0);
1202 if (!skl_ddi_calculate_wrpll(clock * 1000, &wrpll_params))
1205 cfgcr1 = DPLL_CFGCR1_FREQ_ENABLE |
1206 DPLL_CFGCR1_DCO_FRACTION(wrpll_params.dco_fraction) |
1207 wrpll_params.dco_integer;
1209 cfgcr2 = DPLL_CFGCR2_QDIV_RATIO(wrpll_params.qdiv_ratio) |
1210 DPLL_CFGCR2_QDIV_MODE(wrpll_params.qdiv_mode) |
1211 DPLL_CFGCR2_KDIV(wrpll_params.kdiv) |
1212 DPLL_CFGCR2_PDIV(wrpll_params.pdiv) |
1213 wrpll_params.central_freq;
1214 } else if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
1215 encoder->type == INTEL_OUTPUT_DP_MST ||
1216 encoder->type == INTEL_OUTPUT_EDP) {
1217 switch (crtc_state->port_clock / 2) {
1219 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, 0);
1222 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, 0);
1225 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, 0);
1229 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, 0);
1231 /* TBD: For DP link rates 2.16 GHz and 4.32 GHz, VCO is 8640 which
1232 results in CDCLK change. Need to handle the change of CDCLK by
1233 disabling pipes and re-enabling them */
1235 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, 0);
1238 ctrl1 |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, 0);
1242 cfgcr1 = cfgcr2 = 0;
1247 memset(&crtc_state->dpll_hw_state, 0,
1248 sizeof(crtc_state->dpll_hw_state));
1250 crtc_state->dpll_hw_state.ctrl1 = ctrl1;
1251 crtc_state->dpll_hw_state.cfgcr1 = cfgcr1;
1252 crtc_state->dpll_hw_state.cfgcr2 = cfgcr2;
1254 if (encoder->type == INTEL_OUTPUT_EDP)
1255 pll = intel_find_shared_dpll(crtc, crtc_state,
1259 pll = intel_find_shared_dpll(crtc, crtc_state,
1265 crtc_state->ddi_pll_sel = pll->id;
1267 intel_reference_shared_dpll(pll, crtc_state);
1272 static const struct intel_shared_dpll_funcs skl_ddi_pll_funcs = {
1273 .enable = skl_ddi_pll_enable,
1274 .disable = skl_ddi_pll_disable,
1275 .get_hw_state = skl_ddi_pll_get_hw_state,
1278 static const struct intel_shared_dpll_funcs skl_ddi_dpll0_funcs = {
1279 .enable = skl_ddi_dpll0_enable,
1280 .disable = skl_ddi_dpll0_disable,
1281 .get_hw_state = skl_ddi_dpll0_get_hw_state,
1284 static void bxt_ddi_pll_enable(struct drm_i915_private *dev_priv,
1285 struct intel_shared_dpll *pll)
1288 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
1290 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1291 temp &= ~PORT_PLL_REF_SEL;
1292 /* Non-SSC reference */
1293 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1295 /* Disable 10 bit clock */
1296 temp = I915_READ(BXT_PORT_PLL_EBB_4(port));
1297 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
1298 I915_WRITE(BXT_PORT_PLL_EBB_4(port), temp);
1301 temp = I915_READ(BXT_PORT_PLL_EBB_0(port));
1302 temp &= ~(PORT_PLL_P1_MASK | PORT_PLL_P2_MASK);
1303 temp |= pll->config.hw_state.ebb0;
1304 I915_WRITE(BXT_PORT_PLL_EBB_0(port), temp);
1306 /* Write M2 integer */
1307 temp = I915_READ(BXT_PORT_PLL(port, 0));
1308 temp &= ~PORT_PLL_M2_MASK;
1309 temp |= pll->config.hw_state.pll0;
1310 I915_WRITE(BXT_PORT_PLL(port, 0), temp);
1313 temp = I915_READ(BXT_PORT_PLL(port, 1));
1314 temp &= ~PORT_PLL_N_MASK;
1315 temp |= pll->config.hw_state.pll1;
1316 I915_WRITE(BXT_PORT_PLL(port, 1), temp);
1318 /* Write M2 fraction */
1319 temp = I915_READ(BXT_PORT_PLL(port, 2));
1320 temp &= ~PORT_PLL_M2_FRAC_MASK;
1321 temp |= pll->config.hw_state.pll2;
1322 I915_WRITE(BXT_PORT_PLL(port, 2), temp);
1324 /* Write M2 fraction enable */
1325 temp = I915_READ(BXT_PORT_PLL(port, 3));
1326 temp &= ~PORT_PLL_M2_FRAC_ENABLE;
1327 temp |= pll->config.hw_state.pll3;
1328 I915_WRITE(BXT_PORT_PLL(port, 3), temp);
1331 temp = I915_READ(BXT_PORT_PLL(port, 6));
1332 temp &= ~PORT_PLL_PROP_COEFF_MASK;
1333 temp &= ~PORT_PLL_INT_COEFF_MASK;
1334 temp &= ~PORT_PLL_GAIN_CTL_MASK;
1335 temp |= pll->config.hw_state.pll6;
1336 I915_WRITE(BXT_PORT_PLL(port, 6), temp);
1338 /* Write calibration val */
1339 temp = I915_READ(BXT_PORT_PLL(port, 8));
1340 temp &= ~PORT_PLL_TARGET_CNT_MASK;
1341 temp |= pll->config.hw_state.pll8;
1342 I915_WRITE(BXT_PORT_PLL(port, 8), temp);
1344 temp = I915_READ(BXT_PORT_PLL(port, 9));
1345 temp &= ~PORT_PLL_LOCK_THRESHOLD_MASK;
1346 temp |= pll->config.hw_state.pll9;
1347 I915_WRITE(BXT_PORT_PLL(port, 9), temp);
1349 temp = I915_READ(BXT_PORT_PLL(port, 10));
1350 temp &= ~PORT_PLL_DCO_AMP_OVR_EN_H;
1351 temp &= ~PORT_PLL_DCO_AMP_MASK;
1352 temp |= pll->config.hw_state.pll10;
1353 I915_WRITE(BXT_PORT_PLL(port, 10), temp);
1355 /* Recalibrate with new settings */
1356 temp = I915_READ(BXT_PORT_PLL_EBB_4(port));
1357 temp |= PORT_PLL_RECALIBRATE;
1358 I915_WRITE(BXT_PORT_PLL_EBB_4(port), temp);
1359 temp &= ~PORT_PLL_10BIT_CLK_ENABLE;
1360 temp |= pll->config.hw_state.ebb4;
1361 I915_WRITE(BXT_PORT_PLL_EBB_4(port), temp);
1364 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1365 temp |= PORT_PLL_ENABLE;
1366 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1367 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
1369 if (wait_for_atomic_us((I915_READ(BXT_PORT_PLL_ENABLE(port)) &
1370 PORT_PLL_LOCK), 200))
1371 DRM_ERROR("PLL %d not locked\n", port);
1374 * While we write to the group register to program all lanes at once we
1375 * can read only lane registers and we pick lanes 0/1 for that.
1377 temp = I915_READ(BXT_PORT_PCS_DW12_LN01(port));
1378 temp &= ~LANE_STAGGER_MASK;
1379 temp &= ~LANESTAGGER_STRAP_OVRD;
1380 temp |= pll->config.hw_state.pcsdw12;
1381 I915_WRITE(BXT_PORT_PCS_DW12_GRP(port), temp);
1384 static void bxt_ddi_pll_disable(struct drm_i915_private *dev_priv,
1385 struct intel_shared_dpll *pll)
1387 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
1390 temp = I915_READ(BXT_PORT_PLL_ENABLE(port));
1391 temp &= ~PORT_PLL_ENABLE;
1392 I915_WRITE(BXT_PORT_PLL_ENABLE(port), temp);
1393 POSTING_READ(BXT_PORT_PLL_ENABLE(port));
1396 static bool bxt_ddi_pll_get_hw_state(struct drm_i915_private *dev_priv,
1397 struct intel_shared_dpll *pll,
1398 struct intel_dpll_hw_state *hw_state)
1400 enum port port = (enum port)pll->id; /* 1:1 port->PLL mapping */
1404 if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_PLLS))
1409 val = I915_READ(BXT_PORT_PLL_ENABLE(port));
1410 if (!(val & PORT_PLL_ENABLE))
1413 hw_state->ebb0 = I915_READ(BXT_PORT_PLL_EBB_0(port));
1414 hw_state->ebb0 &= PORT_PLL_P1_MASK | PORT_PLL_P2_MASK;
1416 hw_state->ebb4 = I915_READ(BXT_PORT_PLL_EBB_4(port));
1417 hw_state->ebb4 &= PORT_PLL_10BIT_CLK_ENABLE;
1419 hw_state->pll0 = I915_READ(BXT_PORT_PLL(port, 0));
1420 hw_state->pll0 &= PORT_PLL_M2_MASK;
1422 hw_state->pll1 = I915_READ(BXT_PORT_PLL(port, 1));
1423 hw_state->pll1 &= PORT_PLL_N_MASK;
1425 hw_state->pll2 = I915_READ(BXT_PORT_PLL(port, 2));
1426 hw_state->pll2 &= PORT_PLL_M2_FRAC_MASK;
1428 hw_state->pll3 = I915_READ(BXT_PORT_PLL(port, 3));
1429 hw_state->pll3 &= PORT_PLL_M2_FRAC_ENABLE;
1431 hw_state->pll6 = I915_READ(BXT_PORT_PLL(port, 6));
1432 hw_state->pll6 &= PORT_PLL_PROP_COEFF_MASK |
1433 PORT_PLL_INT_COEFF_MASK |
1434 PORT_PLL_GAIN_CTL_MASK;
1436 hw_state->pll8 = I915_READ(BXT_PORT_PLL(port, 8));
1437 hw_state->pll8 &= PORT_PLL_TARGET_CNT_MASK;
1439 hw_state->pll9 = I915_READ(BXT_PORT_PLL(port, 9));
1440 hw_state->pll9 &= PORT_PLL_LOCK_THRESHOLD_MASK;
1442 hw_state->pll10 = I915_READ(BXT_PORT_PLL(port, 10));
1443 hw_state->pll10 &= PORT_PLL_DCO_AMP_OVR_EN_H |
1444 PORT_PLL_DCO_AMP_MASK;
1447 * While we write to the group register to program all lanes at once we
1448 * can read only lane registers. We configure all lanes the same way, so
1449 * here just read out lanes 0/1 and output a note if lanes 2/3 differ.
1451 hw_state->pcsdw12 = I915_READ(BXT_PORT_PCS_DW12_LN01(port));
1452 if (I915_READ(BXT_PORT_PCS_DW12_LN23(port)) != hw_state->pcsdw12)
1453 DRM_DEBUG_DRIVER("lane stagger config different for lane 01 (%08x) and 23 (%08x)\n",
1455 I915_READ(BXT_PORT_PCS_DW12_LN23(port)));
1456 hw_state->pcsdw12 &= LANE_STAGGER_MASK | LANESTAGGER_STRAP_OVRD;
1461 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
1466 /* bxt clock parameters */
1467 struct bxt_clk_div {
1477 /* pre-calculated values for DP linkrates */
1478 static const struct bxt_clk_div bxt_dp_clk_val[] = {
1479 {162000, 4, 2, 32, 1677722, 1, 1},
1480 {270000, 4, 1, 27, 0, 0, 1},
1481 {540000, 2, 1, 27, 0, 0, 1},
1482 {216000, 3, 2, 32, 1677722, 1, 1},
1483 {243000, 4, 1, 24, 1258291, 1, 1},
1484 {324000, 4, 1, 32, 1677722, 1, 1},
1485 {432000, 3, 1, 32, 1677722, 1, 1}
1488 static struct intel_shared_dpll *
1489 bxt_get_dpll(struct intel_crtc *crtc, struct intel_crtc_state *crtc_state,
1490 struct intel_encoder *encoder)
1492 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1493 struct intel_shared_dpll *pll;
1494 enum intel_dpll_id i;
1495 struct intel_digital_port *intel_dig_port;
1496 struct bxt_clk_div clk_div = {0};
1498 uint32_t prop_coef, int_coef, gain_ctl, targ_cnt;
1499 uint32_t lanestagger;
1500 int clock = crtc_state->port_clock;
1502 if (encoder->type == INTEL_OUTPUT_HDMI) {
1503 intel_clock_t best_clock;
1505 /* Calculate HDMI div */
1507 * FIXME: tie the following calculation into
1508 * i9xx_crtc_compute_clock
1510 if (!bxt_find_best_dpll(crtc_state, clock, &best_clock)) {
1511 DRM_DEBUG_DRIVER("no PLL dividers found for clock %d pipe %c\n",
1512 clock, pipe_name(crtc->pipe));
1516 clk_div.p1 = best_clock.p1;
1517 clk_div.p2 = best_clock.p2;
1518 WARN_ON(best_clock.m1 != 2);
1519 clk_div.n = best_clock.n;
1520 clk_div.m2_int = best_clock.m2 >> 22;
1521 clk_div.m2_frac = best_clock.m2 & ((1 << 22) - 1);
1522 clk_div.m2_frac_en = clk_div.m2_frac != 0;
1524 vco = best_clock.vco;
1525 } else if (encoder->type == INTEL_OUTPUT_DISPLAYPORT ||
1526 encoder->type == INTEL_OUTPUT_EDP) {
1529 clk_div = bxt_dp_clk_val[0];
1530 for (i = 0; i < ARRAY_SIZE(bxt_dp_clk_val); ++i) {
1531 if (bxt_dp_clk_val[i].clock == clock) {
1532 clk_div = bxt_dp_clk_val[i];
1536 vco = clock * 10 / 2 * clk_div.p1 * clk_div.p2;
1539 if (vco >= 6200000 && vco <= 6700000) {
1544 } else if ((vco > 5400000 && vco < 6200000) ||
1545 (vco >= 4800000 && vco < 5400000)) {
1550 } else if (vco == 5400000) {
1556 DRM_ERROR("Invalid VCO\n");
1560 memset(&crtc_state->dpll_hw_state, 0,
1561 sizeof(crtc_state->dpll_hw_state));
1565 else if (clock > 135000)
1567 else if (clock > 67000)
1569 else if (clock > 33000)
1574 crtc_state->dpll_hw_state.ebb0 =
1575 PORT_PLL_P1(clk_div.p1) | PORT_PLL_P2(clk_div.p2);
1576 crtc_state->dpll_hw_state.pll0 = clk_div.m2_int;
1577 crtc_state->dpll_hw_state.pll1 = PORT_PLL_N(clk_div.n);
1578 crtc_state->dpll_hw_state.pll2 = clk_div.m2_frac;
1580 if (clk_div.m2_frac_en)
1581 crtc_state->dpll_hw_state.pll3 =
1582 PORT_PLL_M2_FRAC_ENABLE;
1584 crtc_state->dpll_hw_state.pll6 =
1585 prop_coef | PORT_PLL_INT_COEFF(int_coef);
1586 crtc_state->dpll_hw_state.pll6 |=
1587 PORT_PLL_GAIN_CTL(gain_ctl);
1589 crtc_state->dpll_hw_state.pll8 = targ_cnt;
1591 crtc_state->dpll_hw_state.pll9 = 5 << PORT_PLL_LOCK_THRESHOLD_SHIFT;
1593 crtc_state->dpll_hw_state.pll10 =
1594 PORT_PLL_DCO_AMP(PORT_PLL_DCO_AMP_DEFAULT)
1595 | PORT_PLL_DCO_AMP_OVR_EN_H;
1597 crtc_state->dpll_hw_state.ebb4 = PORT_PLL_10BIT_CLK_ENABLE;
1599 crtc_state->dpll_hw_state.pcsdw12 =
1600 LANESTAGGER_STRAP_OVRD | lanestagger;
1602 intel_dig_port = enc_to_dig_port(&encoder->base);
1604 /* 1:1 mapping between ports and PLLs */
1605 i = (enum intel_dpll_id) intel_dig_port->port;
1606 pll = intel_get_shared_dpll_by_id(dev_priv, i);
1608 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
1609 crtc->base.base.id, pll->name);
1611 intel_reference_shared_dpll(pll, crtc_state);
1613 /* shared DPLL id 0 is DPLL A */
1614 crtc_state->ddi_pll_sel = pll->id;
1619 static const struct intel_shared_dpll_funcs bxt_ddi_pll_funcs = {
1620 .enable = bxt_ddi_pll_enable,
1621 .disable = bxt_ddi_pll_disable,
1622 .get_hw_state = bxt_ddi_pll_get_hw_state,
1625 static void intel_ddi_pll_init(struct drm_device *dev)
1627 struct drm_i915_private *dev_priv = dev->dev_private;
1628 uint32_t val = I915_READ(LCPLL_CTL);
1630 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
1633 cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
1634 dev_priv->skl_boot_cdclk = cdclk_freq;
1635 if (skl_sanitize_cdclk(dev_priv))
1636 DRM_DEBUG_KMS("Sanitized cdclk programmed by pre-os\n");
1637 if (!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE))
1638 DRM_ERROR("LCPLL1 is disabled\n");
1639 } else if (IS_BROXTON(dev)) {
1640 broxton_init_cdclk(dev);
1641 broxton_ddi_phy_init(dev);
1644 * The LCPLL register should be turned on by the BIOS. For now
1645 * let's just check its state and print errors in case
1646 * something is wrong. Don't even try to turn it on.
1649 if (val & LCPLL_CD_SOURCE_FCLK)
1650 DRM_ERROR("CDCLK source is not LCPLL\n");
1652 if (val & LCPLL_PLL_DISABLE)
1653 DRM_ERROR("LCPLL is disabled\n");
1660 const struct intel_shared_dpll_funcs *funcs;
1664 struct intel_dpll_mgr {
1665 const struct dpll_info *dpll_info;
1667 struct intel_shared_dpll *(*get_dpll)(struct intel_crtc *crtc,
1668 struct intel_crtc_state *crtc_state,
1669 struct intel_encoder *encoder);
1672 static const struct dpll_info pch_plls[] = {
1673 { "PCH DPLL A", DPLL_ID_PCH_PLL_A, &ibx_pch_dpll_funcs, 0 },
1674 { "PCH DPLL B", DPLL_ID_PCH_PLL_B, &ibx_pch_dpll_funcs, 0 },
1675 { NULL, -1, NULL, 0 },
1678 static const struct intel_dpll_mgr pch_pll_mgr = {
1679 .dpll_info = pch_plls,
1680 .get_dpll = ibx_get_dpll,
1683 static const struct dpll_info hsw_plls[] = {
1684 { "WRPLL 1", DPLL_ID_WRPLL1, &hsw_ddi_wrpll_funcs, 0 },
1685 { "WRPLL 2", DPLL_ID_WRPLL2, &hsw_ddi_wrpll_funcs, 0 },
1686 { "SPLL", DPLL_ID_SPLL, &hsw_ddi_spll_funcs, 0 },
1687 { "LCPLL 810", DPLL_ID_LCPLL_810, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
1688 { "LCPLL 1350", DPLL_ID_LCPLL_1350, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
1689 { "LCPLL 2700", DPLL_ID_LCPLL_2700, &hsw_ddi_lcpll_funcs, INTEL_DPLL_ALWAYS_ON },
1690 { NULL, -1, NULL, },
1693 static const struct intel_dpll_mgr hsw_pll_mgr = {
1694 .dpll_info = hsw_plls,
1695 .get_dpll = hsw_get_dpll,
1698 static const struct dpll_info skl_plls[] = {
1699 { "DPLL 0", DPLL_ID_SKL_DPLL0, &skl_ddi_dpll0_funcs, INTEL_DPLL_ALWAYS_ON },
1700 { "DPPL 1", DPLL_ID_SKL_DPLL1, &skl_ddi_pll_funcs, 0 },
1701 { "DPPL 2", DPLL_ID_SKL_DPLL2, &skl_ddi_pll_funcs, 0 },
1702 { "DPPL 3", DPLL_ID_SKL_DPLL3, &skl_ddi_pll_funcs, 0 },
1703 { NULL, -1, NULL, },
1706 static const struct intel_dpll_mgr skl_pll_mgr = {
1707 .dpll_info = skl_plls,
1708 .get_dpll = skl_get_dpll,
1711 static const struct dpll_info bxt_plls[] = {
1712 { "PORT PLL A", DPLL_ID_SKL_DPLL0, &bxt_ddi_pll_funcs, 0 },
1713 { "PORT PLL B", DPLL_ID_SKL_DPLL1, &bxt_ddi_pll_funcs, 0 },
1714 { "PORT PLL C", DPLL_ID_SKL_DPLL2, &bxt_ddi_pll_funcs, 0 },
1715 { NULL, -1, NULL, },
1718 static const struct intel_dpll_mgr bxt_pll_mgr = {
1719 .dpll_info = bxt_plls,
1720 .get_dpll = bxt_get_dpll,
1723 void intel_shared_dpll_init(struct drm_device *dev)
1725 struct drm_i915_private *dev_priv = dev->dev_private;
1726 const struct intel_dpll_mgr *dpll_mgr = NULL;
1727 const struct dpll_info *dpll_info;
1730 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
1731 dpll_mgr = &skl_pll_mgr;
1732 else if (IS_BROXTON(dev))
1733 dpll_mgr = &bxt_pll_mgr;
1734 else if (HAS_DDI(dev))
1735 dpll_mgr = &hsw_pll_mgr;
1736 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
1737 dpll_mgr = &pch_pll_mgr;
1740 dev_priv->num_shared_dpll = 0;
1744 dpll_info = dpll_mgr->dpll_info;
1746 for (i = 0; dpll_info[i].id >= 0; i++) {
1747 WARN_ON(i != dpll_info[i].id);
1749 dev_priv->shared_dplls[i].id = dpll_info[i].id;
1750 dev_priv->shared_dplls[i].name = dpll_info[i].name;
1751 dev_priv->shared_dplls[i].funcs = *dpll_info[i].funcs;
1752 dev_priv->shared_dplls[i].flags = dpll_info[i].flags;
1755 dev_priv->dpll_mgr = dpll_mgr;
1756 dev_priv->num_shared_dpll = i;
1758 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
1760 /* FIXME: Move this to a more suitable place */
1762 intel_ddi_pll_init(dev);
1765 struct intel_shared_dpll *
1766 intel_get_shared_dpll(struct intel_crtc *crtc,
1767 struct intel_crtc_state *crtc_state,
1768 struct intel_encoder *encoder)
1770 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1771 const struct intel_dpll_mgr *dpll_mgr = dev_priv->dpll_mgr;
1773 if (WARN_ON(!dpll_mgr))
1776 return dpll_mgr->get_dpll(crtc, crtc_state, encoder);