WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / display / intel_dp_link_training.c
blob91d3979902d03cb891474ad587a79c760118797c
1 /*
2 * Copyright © 2008-2015 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
13 * Software.
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 DEALINGS
21 * IN THE SOFTWARE.
24 #include "intel_display_types.h"
25 #include "intel_dp.h"
26 #include "intel_dp_link_training.h"
28 static void
29 intel_dp_dump_link_status(const u8 link_status[DP_LINK_STATUS_SIZE])
32 DRM_DEBUG_KMS("ln0_1:0x%x ln2_3:0x%x align:0x%x sink:0x%x adj_req0_1:0x%x adj_req2_3:0x%x",
33 link_status[0], link_status[1], link_status[2],
34 link_status[3], link_status[4], link_status[5]);
37 static int intel_dp_lttpr_count(struct intel_dp *intel_dp)
39 int count = drm_dp_lttpr_count(intel_dp->lttpr_common_caps);
42 * Pretend no LTTPRs in case of LTTPR detection error, or
43 * if too many (>8) LTTPRs are detected. This translates to link
44 * training in transparent mode.
46 return count <= 0 ? 0 : count;
49 static void intel_dp_reset_lttpr_count(struct intel_dp *intel_dp)
51 intel_dp->lttpr_common_caps[DP_PHY_REPEATER_CNT -
52 DP_LT_TUNABLE_PHY_REPEATER_FIELD_DATA_STRUCTURE_REV] = 0;
55 static const char *intel_dp_phy_name(enum drm_dp_phy dp_phy,
56 char *buf, size_t buf_size)
58 if (dp_phy == DP_PHY_DPRX)
59 snprintf(buf, buf_size, "DPRX");
60 else
61 snprintf(buf, buf_size, "LTTPR %d", dp_phy - DP_PHY_LTTPR1 + 1);
63 return buf;
66 static u8 *intel_dp_lttpr_phy_caps(struct intel_dp *intel_dp,
67 enum drm_dp_phy dp_phy)
69 return intel_dp->lttpr_phy_caps[dp_phy - DP_PHY_LTTPR1];
72 static void intel_dp_read_lttpr_phy_caps(struct intel_dp *intel_dp,
73 enum drm_dp_phy dp_phy)
75 u8 *phy_caps = intel_dp_lttpr_phy_caps(intel_dp, dp_phy);
76 char phy_name[10];
78 intel_dp_phy_name(dp_phy, phy_name, sizeof(phy_name));
80 if (drm_dp_read_lttpr_phy_caps(&intel_dp->aux, dp_phy, phy_caps) < 0) {
81 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
82 "failed to read the PHY caps for %s\n",
83 phy_name);
84 return;
87 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
88 "%s PHY capabilities: %*ph\n",
89 phy_name,
90 (int)sizeof(intel_dp->lttpr_phy_caps[0]),
91 phy_caps);
94 static bool intel_dp_read_lttpr_common_caps(struct intel_dp *intel_dp)
96 if (drm_dp_read_lttpr_common_caps(&intel_dp->aux,
97 intel_dp->lttpr_common_caps) < 0) {
98 memset(intel_dp->lttpr_common_caps, 0,
99 sizeof(intel_dp->lttpr_common_caps));
100 return false;
103 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
104 "LTTPR common capabilities: %*ph\n",
105 (int)sizeof(intel_dp->lttpr_common_caps),
106 intel_dp->lttpr_common_caps);
108 return true;
111 static bool
112 intel_dp_set_lttpr_transparent_mode(struct intel_dp *intel_dp, bool enable)
114 u8 val = enable ? DP_PHY_REPEATER_MODE_TRANSPARENT :
115 DP_PHY_REPEATER_MODE_NON_TRANSPARENT;
117 return drm_dp_dpcd_write(&intel_dp->aux, DP_PHY_REPEATER_MODE, &val, 1) == 1;
121 * intel_dp_lttpr_init - detect LTTPRs and init the LTTPR link training mode
122 * @intel_dp: Intel DP struct
124 * Read the LTTPR common capabilities, switch to non-transparent link training
125 * mode if any is detected and read the PHY capabilities for all detected
126 * LTTPRs. In case of an LTTPR detection error or if the number of
127 * LTTPRs is more than is supported (8), fall back to the no-LTTPR,
128 * transparent mode link training mode.
130 * Returns:
131 * >0 if LTTPRs were detected and the non-transparent LT mode was set
132 * 0 if no LTTPRs or more than 8 LTTPRs were detected or in case of a
133 * detection failure and the transparent LT mode was set
135 int intel_dp_lttpr_init(struct intel_dp *intel_dp)
137 int lttpr_count;
138 bool ret;
139 int i;
141 if (intel_dp_is_edp(intel_dp))
142 return 0;
144 ret = intel_dp_read_lttpr_common_caps(intel_dp);
147 * See DP Standard v2.0 3.6.6.1. about the explicit disabling of
148 * non-transparent mode and the disable->enable non-transparent mode
149 * sequence.
151 intel_dp_set_lttpr_transparent_mode(intel_dp, true);
153 if (!ret)
154 return 0;
156 lttpr_count = intel_dp_lttpr_count(intel_dp);
159 * In case of unsupported number of LTTPRs or failing to switch to
160 * non-transparent mode fall-back to transparent link training mode,
161 * still taking into account any LTTPR common lane- rate/count limits.
163 if (lttpr_count == 0)
164 return 0;
166 if (!intel_dp_set_lttpr_transparent_mode(intel_dp, false)) {
167 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
168 "Switching to LTTPR non-transparent LT mode failed, fall-back to transparent mode\n");
170 intel_dp_set_lttpr_transparent_mode(intel_dp, true);
171 intel_dp_reset_lttpr_count(intel_dp);
173 return 0;
176 for (i = 0; i < lttpr_count; i++)
177 intel_dp_read_lttpr_phy_caps(intel_dp, DP_PHY_LTTPR(i));
179 return lttpr_count;
181 EXPORT_SYMBOL(intel_dp_lttpr_init);
183 static u8 dp_voltage_max(u8 preemph)
185 switch (preemph & DP_TRAIN_PRE_EMPHASIS_MASK) {
186 case DP_TRAIN_PRE_EMPH_LEVEL_0:
187 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
188 case DP_TRAIN_PRE_EMPH_LEVEL_1:
189 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
190 case DP_TRAIN_PRE_EMPH_LEVEL_2:
191 return DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
192 case DP_TRAIN_PRE_EMPH_LEVEL_3:
193 default:
194 return DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
198 static u8 intel_dp_lttpr_voltage_max(struct intel_dp *intel_dp,
199 enum drm_dp_phy dp_phy)
201 const u8 *phy_caps = intel_dp_lttpr_phy_caps(intel_dp, dp_phy);
203 if (drm_dp_lttpr_voltage_swing_level_3_supported(phy_caps))
204 return DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
205 else
206 return DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
209 static u8 intel_dp_lttpr_preemph_max(struct intel_dp *intel_dp,
210 enum drm_dp_phy dp_phy)
212 const u8 *phy_caps = intel_dp_lttpr_phy_caps(intel_dp, dp_phy);
214 if (drm_dp_lttpr_pre_emphasis_level_3_supported(phy_caps))
215 return DP_TRAIN_PRE_EMPH_LEVEL_3;
216 else
217 return DP_TRAIN_PRE_EMPH_LEVEL_2;
220 static bool
221 intel_dp_phy_is_downstream_of_source(struct intel_dp *intel_dp,
222 enum drm_dp_phy dp_phy)
224 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
225 int lttpr_count = intel_dp_lttpr_count(intel_dp);
227 drm_WARN_ON_ONCE(&i915->drm, lttpr_count == 0 && dp_phy != DP_PHY_DPRX);
229 return lttpr_count == 0 || dp_phy == DP_PHY_LTTPR(lttpr_count - 1);
232 static u8 intel_dp_phy_voltage_max(struct intel_dp *intel_dp,
233 const struct intel_crtc_state *crtc_state,
234 enum drm_dp_phy dp_phy)
236 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
237 u8 voltage_max;
240 * Get voltage_max from the DPTX_PHY (source or LTTPR) upstream from
241 * the DPRX_PHY we train.
243 if (intel_dp_phy_is_downstream_of_source(intel_dp, dp_phy))
244 voltage_max = intel_dp->voltage_max(intel_dp, crtc_state);
245 else
246 voltage_max = intel_dp_lttpr_voltage_max(intel_dp, dp_phy + 1);
248 drm_WARN_ON_ONCE(&i915->drm,
249 voltage_max != DP_TRAIN_VOLTAGE_SWING_LEVEL_2 &&
250 voltage_max != DP_TRAIN_VOLTAGE_SWING_LEVEL_3);
252 return voltage_max;
255 static u8 intel_dp_phy_preemph_max(struct intel_dp *intel_dp,
256 enum drm_dp_phy dp_phy)
258 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
259 u8 preemph_max;
262 * Get preemph_max from the DPTX_PHY (source or LTTPR) upstream from
263 * the DPRX_PHY we train.
265 if (intel_dp_phy_is_downstream_of_source(intel_dp, dp_phy))
266 preemph_max = intel_dp->preemph_max(intel_dp);
267 else
268 preemph_max = intel_dp_lttpr_preemph_max(intel_dp, dp_phy + 1);
270 drm_WARN_ON_ONCE(&i915->drm,
271 preemph_max != DP_TRAIN_PRE_EMPH_LEVEL_2 &&
272 preemph_max != DP_TRAIN_PRE_EMPH_LEVEL_3);
274 return preemph_max;
277 void
278 intel_dp_get_adjust_train(struct intel_dp *intel_dp,
279 const struct intel_crtc_state *crtc_state,
280 enum drm_dp_phy dp_phy,
281 const u8 link_status[DP_LINK_STATUS_SIZE])
283 u8 v = 0;
284 u8 p = 0;
285 int lane;
286 u8 voltage_max;
287 u8 preemph_max;
289 for (lane = 0; lane < crtc_state->lane_count; lane++) {
290 v = max(v, drm_dp_get_adjust_request_voltage(link_status, lane));
291 p = max(p, drm_dp_get_adjust_request_pre_emphasis(link_status, lane));
294 preemph_max = intel_dp_phy_preemph_max(intel_dp, dp_phy);
295 if (p >= preemph_max)
296 p = preemph_max | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
298 v = min(v, dp_voltage_max(p));
300 voltage_max = intel_dp_phy_voltage_max(intel_dp, crtc_state, dp_phy);
301 if (v >= voltage_max)
302 v = voltage_max | DP_TRAIN_MAX_SWING_REACHED;
304 for (lane = 0; lane < 4; lane++)
305 intel_dp->train_set[lane] = v | p;
308 static int intel_dp_training_pattern_set_reg(struct intel_dp *intel_dp,
309 enum drm_dp_phy dp_phy)
311 return dp_phy == DP_PHY_DPRX ?
312 DP_TRAINING_PATTERN_SET :
313 DP_TRAINING_PATTERN_SET_PHY_REPEATER(dp_phy);
316 static bool
317 intel_dp_set_link_train(struct intel_dp *intel_dp,
318 const struct intel_crtc_state *crtc_state,
319 enum drm_dp_phy dp_phy,
320 u8 dp_train_pat)
322 int reg = intel_dp_training_pattern_set_reg(intel_dp, dp_phy);
323 u8 buf[sizeof(intel_dp->train_set) + 1];
324 int len;
326 intel_dp_program_link_training_pattern(intel_dp, crtc_state,
327 dp_train_pat);
329 buf[0] = dp_train_pat;
330 /* DP_TRAINING_LANEx_SET follow DP_TRAINING_PATTERN_SET */
331 memcpy(buf + 1, intel_dp->train_set, crtc_state->lane_count);
332 len = crtc_state->lane_count + 1;
334 return drm_dp_dpcd_write(&intel_dp->aux, reg, buf, len) == len;
337 static bool
338 intel_dp_reset_link_train(struct intel_dp *intel_dp,
339 const struct intel_crtc_state *crtc_state,
340 enum drm_dp_phy dp_phy,
341 u8 dp_train_pat)
343 memset(intel_dp->train_set, 0, sizeof(intel_dp->train_set));
344 intel_dp_set_signal_levels(intel_dp, crtc_state);
345 return intel_dp_set_link_train(intel_dp, crtc_state, dp_phy, dp_train_pat);
348 static bool
349 intel_dp_update_link_train(struct intel_dp *intel_dp,
350 const struct intel_crtc_state *crtc_state,
351 enum drm_dp_phy dp_phy)
353 int reg = dp_phy == DP_PHY_DPRX ?
354 DP_TRAINING_LANE0_SET :
355 DP_TRAINING_LANE0_SET_PHY_REPEATER(dp_phy);
356 int ret;
358 intel_dp_set_signal_levels(intel_dp, crtc_state);
360 ret = drm_dp_dpcd_write(&intel_dp->aux, reg,
361 intel_dp->train_set, crtc_state->lane_count);
363 return ret == crtc_state->lane_count;
366 static bool intel_dp_link_max_vswing_reached(struct intel_dp *intel_dp,
367 const struct intel_crtc_state *crtc_state)
369 int lane;
371 for (lane = 0; lane < crtc_state->lane_count; lane++)
372 if ((intel_dp->train_set[lane] &
373 DP_TRAIN_MAX_SWING_REACHED) == 0)
374 return false;
376 return true;
380 * Prepare link training by configuring the link parameters. On DDI platforms
381 * also enable the port here.
383 static bool
384 intel_dp_prepare_link_train(struct intel_dp *intel_dp,
385 const struct intel_crtc_state *crtc_state)
387 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
388 u8 link_config[2];
389 u8 link_bw, rate_select;
391 if (intel_dp->prepare_link_retrain)
392 intel_dp->prepare_link_retrain(intel_dp, crtc_state);
394 intel_dp_compute_rate(intel_dp, crtc_state->port_clock,
395 &link_bw, &rate_select);
397 if (link_bw)
398 drm_dbg_kms(&i915->drm,
399 "Using LINK_BW_SET value %02x\n", link_bw);
400 else
401 drm_dbg_kms(&i915->drm,
402 "Using LINK_RATE_SET value %02x\n", rate_select);
404 /* Write the link configuration data */
405 link_config[0] = link_bw;
406 link_config[1] = crtc_state->lane_count;
407 if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
408 link_config[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
409 drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_BW_SET, link_config, 2);
411 /* eDP 1.4 rate select method. */
412 if (!link_bw)
413 drm_dp_dpcd_write(&intel_dp->aux, DP_LINK_RATE_SET,
414 &rate_select, 1);
416 link_config[0] = 0;
417 link_config[1] = DP_SET_ANSI_8B10B;
418 drm_dp_dpcd_write(&intel_dp->aux, DP_DOWNSPREAD_CTRL, link_config, 2);
420 intel_dp->DP |= DP_PORT_EN;
422 return true;
425 static void intel_dp_link_training_clock_recovery_delay(struct intel_dp *intel_dp,
426 enum drm_dp_phy dp_phy)
428 if (dp_phy == DP_PHY_DPRX)
429 drm_dp_link_train_clock_recovery_delay(intel_dp->dpcd);
430 else
431 drm_dp_lttpr_link_train_clock_recovery_delay();
435 * Perform the link training clock recovery phase on the given DP PHY using
436 * training pattern 1.
438 static bool
439 intel_dp_link_training_clock_recovery(struct intel_dp *intel_dp,
440 const struct intel_crtc_state *crtc_state,
441 enum drm_dp_phy dp_phy)
443 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
444 u8 voltage;
445 int voltage_tries, cr_tries, max_cr_tries;
446 bool max_vswing_reached = false;
448 /* clock recovery */
449 if (!intel_dp_reset_link_train(intel_dp, crtc_state, dp_phy,
450 DP_TRAINING_PATTERN_1 |
451 DP_LINK_SCRAMBLING_DISABLE)) {
452 drm_err(&i915->drm, "failed to enable link training\n");
453 return false;
457 * The DP 1.4 spec defines the max clock recovery retries value
458 * as 10 but for pre-DP 1.4 devices we set a very tolerant
459 * retry limit of 80 (4 voltage levels x 4 preemphasis levels x
460 * x 5 identical voltage retries). Since the previous specs didn't
461 * define a limit and created the possibility of an infinite loop
462 * we want to prevent any sync from triggering that corner case.
464 if (intel_dp->dpcd[DP_DPCD_REV] >= DP_DPCD_REV_14)
465 max_cr_tries = 10;
466 else
467 max_cr_tries = 80;
469 voltage_tries = 1;
470 for (cr_tries = 0; cr_tries < max_cr_tries; ++cr_tries) {
471 u8 link_status[DP_LINK_STATUS_SIZE];
473 intel_dp_link_training_clock_recovery_delay(intel_dp, dp_phy);
475 if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, dp_phy,
476 link_status) < 0) {
477 drm_err(&i915->drm, "failed to get link status\n");
478 return false;
481 if (drm_dp_clock_recovery_ok(link_status, crtc_state->lane_count)) {
482 drm_dbg_kms(&i915->drm, "clock recovery OK\n");
483 return true;
486 if (voltage_tries == 5) {
487 drm_dbg_kms(&i915->drm,
488 "Same voltage tried 5 times\n");
489 return false;
492 if (max_vswing_reached) {
493 drm_dbg_kms(&i915->drm, "Max Voltage Swing reached\n");
494 return false;
497 voltage = intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
499 /* Update training set as requested by target */
500 intel_dp_get_adjust_train(intel_dp, crtc_state, dp_phy,
501 link_status);
502 if (!intel_dp_update_link_train(intel_dp, crtc_state, dp_phy)) {
503 drm_err(&i915->drm,
504 "failed to update link training\n");
505 return false;
508 if ((intel_dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) ==
509 voltage)
510 ++voltage_tries;
511 else
512 voltage_tries = 1;
514 if (intel_dp_link_max_vswing_reached(intel_dp, crtc_state))
515 max_vswing_reached = true;
518 drm_err(&i915->drm,
519 "Failed clock recovery %d times, giving up!\n", max_cr_tries);
520 return false;
524 * Pick training pattern for channel equalization. Training pattern 4 for HBR3
525 * or for 1.4 devices that support it, training Pattern 3 for HBR2
526 * or 1.2 devices that support it, Training Pattern 2 otherwise.
528 static u32 intel_dp_training_pattern(struct intel_dp *intel_dp,
529 const struct intel_crtc_state *crtc_state,
530 enum drm_dp_phy dp_phy)
532 bool source_tps3, sink_tps3, source_tps4, sink_tps4;
535 * Intel platforms that support HBR3 also support TPS4. It is mandatory
536 * for all downstream devices that support HBR3. There are no known eDP
537 * panels that support TPS4 as of Feb 2018 as per VESA eDP_v1.4b_E1
538 * specification.
539 * LTTPRs must support TPS4.
541 source_tps4 = intel_dp_source_supports_hbr3(intel_dp);
542 sink_tps4 = dp_phy != DP_PHY_DPRX ||
543 drm_dp_tps4_supported(intel_dp->dpcd);
544 if (source_tps4 && sink_tps4) {
545 return DP_TRAINING_PATTERN_4;
546 } else if (crtc_state->port_clock == 810000) {
547 if (!source_tps4)
548 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
549 "8.1 Gbps link rate without source HBR3/TPS4 support\n");
550 if (!sink_tps4)
551 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
552 "8.1 Gbps link rate without sink TPS4 support\n");
555 * Intel platforms that support HBR2 also support TPS3. TPS3 support is
556 * also mandatory for downstream devices that support HBR2. However, not
557 * all sinks follow the spec.
559 source_tps3 = intel_dp_source_supports_hbr2(intel_dp);
560 sink_tps3 = dp_phy != DP_PHY_DPRX ||
561 drm_dp_tps3_supported(intel_dp->dpcd);
562 if (source_tps3 && sink_tps3) {
563 return DP_TRAINING_PATTERN_3;
564 } else if (crtc_state->port_clock >= 540000) {
565 if (!source_tps3)
566 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
567 ">=5.4/6.48 Gbps link rate without source HBR2/TPS3 support\n");
568 if (!sink_tps3)
569 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
570 ">=5.4/6.48 Gbps link rate without sink TPS3 support\n");
573 return DP_TRAINING_PATTERN_2;
576 static void
577 intel_dp_link_training_channel_equalization_delay(struct intel_dp *intel_dp,
578 enum drm_dp_phy dp_phy)
580 if (dp_phy == DP_PHY_DPRX) {
581 drm_dp_link_train_channel_eq_delay(intel_dp->dpcd);
582 } else {
583 const u8 *phy_caps = intel_dp_lttpr_phy_caps(intel_dp, dp_phy);
585 drm_dp_lttpr_link_train_channel_eq_delay(phy_caps);
590 * Perform the link training channel equalization phase on the given DP PHY
591 * using one of training pattern 2, 3 or 4 depending on the source and
592 * sink capabilities.
594 static bool
595 intel_dp_link_training_channel_equalization(struct intel_dp *intel_dp,
596 const struct intel_crtc_state *crtc_state,
597 enum drm_dp_phy dp_phy)
599 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
600 int tries;
601 u32 training_pattern;
602 u8 link_status[DP_LINK_STATUS_SIZE];
603 bool channel_eq = false;
605 training_pattern = intel_dp_training_pattern(intel_dp, crtc_state, dp_phy);
606 /* Scrambling is disabled for TPS2/3 and enabled for TPS4 */
607 if (training_pattern != DP_TRAINING_PATTERN_4)
608 training_pattern |= DP_LINK_SCRAMBLING_DISABLE;
610 /* channel equalization */
611 if (!intel_dp_set_link_train(intel_dp, crtc_state, dp_phy,
612 training_pattern)) {
613 drm_err(&i915->drm, "failed to start channel equalization\n");
614 return false;
617 for (tries = 0; tries < 5; tries++) {
618 intel_dp_link_training_channel_equalization_delay(intel_dp,
619 dp_phy);
620 if (drm_dp_dpcd_read_phy_link_status(&intel_dp->aux, dp_phy,
621 link_status) < 0) {
622 drm_err(&i915->drm,
623 "failed to get link status\n");
624 break;
627 /* Make sure clock is still ok */
628 if (!drm_dp_clock_recovery_ok(link_status,
629 crtc_state->lane_count)) {
630 intel_dp_dump_link_status(link_status);
631 drm_dbg_kms(&i915->drm,
632 "Clock recovery check failed, cannot "
633 "continue channel equalization\n");
634 break;
637 if (drm_dp_channel_eq_ok(link_status,
638 crtc_state->lane_count)) {
639 channel_eq = true;
640 drm_dbg_kms(&i915->drm, "Channel EQ done. DP Training "
641 "successful\n");
642 break;
645 /* Update training set as requested by target */
646 intel_dp_get_adjust_train(intel_dp, crtc_state, dp_phy,
647 link_status);
648 if (!intel_dp_update_link_train(intel_dp, crtc_state, dp_phy)) {
649 drm_err(&i915->drm,
650 "failed to update link training\n");
651 break;
655 /* Try 5 times, else fail and try at lower BW */
656 if (tries == 5) {
657 intel_dp_dump_link_status(link_status);
658 drm_dbg_kms(&i915->drm,
659 "Channel equalization failed 5 times\n");
662 return channel_eq;
665 static bool intel_dp_disable_dpcd_training_pattern(struct intel_dp *intel_dp,
666 enum drm_dp_phy dp_phy)
668 int reg = intel_dp_training_pattern_set_reg(intel_dp, dp_phy);
669 u8 val = DP_TRAINING_PATTERN_DISABLE;
671 return drm_dp_dpcd_write(&intel_dp->aux, reg, &val, 1) == 1;
675 * intel_dp_stop_link_train - stop link training
676 * @intel_dp: DP struct
677 * @crtc_state: state for CRTC attached to the encoder
679 * Stop the link training of the @intel_dp port, disabling the test pattern
680 * symbol generation on the port and disabling the training pattern in
681 * the sink's DPCD.
683 * What symbols are output on the port after this point is
684 * platform specific: On DDI/VLV/CHV platforms it will be the idle pattern
685 * with the pipe being disabled, on older platforms it's HW specific if/how an
686 * idle pattern is generated, as the pipe is already enabled here for those.
688 * This function must be called after intel_dp_start_link_train().
690 void intel_dp_stop_link_train(struct intel_dp *intel_dp,
691 const struct intel_crtc_state *crtc_state)
693 intel_dp->link_trained = true;
695 intel_dp_program_link_training_pattern(intel_dp,
696 crtc_state,
697 DP_TRAINING_PATTERN_DISABLE);
698 intel_dp_disable_dpcd_training_pattern(intel_dp, DP_PHY_DPRX);
701 static bool
702 intel_dp_link_train_phy(struct intel_dp *intel_dp,
703 const struct intel_crtc_state *crtc_state,
704 enum drm_dp_phy dp_phy)
706 struct intel_connector *intel_connector = intel_dp->attached_connector;
707 char phy_name[10];
708 bool ret = false;
710 if (!intel_dp_link_training_clock_recovery(intel_dp, crtc_state, dp_phy))
711 goto out;
713 if (!intel_dp_link_training_channel_equalization(intel_dp, crtc_state, dp_phy))
714 goto out;
716 ret = true;
718 out:
719 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
720 "[CONNECTOR:%d:%s] Link Training %s at link rate = %d, lane count = %d, at %s",
721 intel_connector->base.base.id,
722 intel_connector->base.name,
723 ret ? "passed" : "failed",
724 crtc_state->port_clock, crtc_state->lane_count,
725 intel_dp_phy_name(dp_phy, phy_name, sizeof(phy_name)));
727 return ret;
730 static void intel_dp_schedule_fallback_link_training(struct intel_dp *intel_dp,
731 const struct intel_crtc_state *crtc_state)
733 struct intel_connector *intel_connector = intel_dp->attached_connector;
735 if (intel_dp->hobl_active) {
736 drm_dbg_kms(&dp_to_i915(intel_dp)->drm,
737 "Link Training failed with HOBL active, not enabling it from now on");
738 intel_dp->hobl_failed = true;
739 } else if (intel_dp_get_link_train_fallback_values(intel_dp,
740 crtc_state->port_clock,
741 crtc_state->lane_count)) {
742 return;
745 /* Schedule a Hotplug Uevent to userspace to start modeset */
746 schedule_work(&intel_connector->modeset_retry_work);
749 /* Perform the link training on all LTTPRs and the DPRX on a link. */
750 static bool
751 intel_dp_link_train_all_phys(struct intel_dp *intel_dp,
752 const struct intel_crtc_state *crtc_state,
753 int lttpr_count)
755 bool ret = true;
756 int i;
758 intel_dp_prepare_link_train(intel_dp, crtc_state);
760 for (i = lttpr_count - 1; i >= 0; i--) {
761 enum drm_dp_phy dp_phy = DP_PHY_LTTPR(i);
763 ret = intel_dp_link_train_phy(intel_dp, crtc_state, dp_phy);
764 intel_dp_disable_dpcd_training_pattern(intel_dp, dp_phy);
766 if (!ret)
767 break;
770 if (ret)
771 intel_dp_link_train_phy(intel_dp, crtc_state, DP_PHY_DPRX);
773 if (intel_dp->set_idle_link_train)
774 intel_dp->set_idle_link_train(intel_dp, crtc_state);
776 return ret;
780 * intel_dp_start_link_train - start link training
781 * @intel_dp: DP struct
782 * @crtc_state: state for CRTC attached to the encoder
784 * Start the link training of the @intel_dp port, scheduling a fallback
785 * retraining with reduced link rate/lane parameters if the link training
786 * fails.
787 * After calling this function intel_dp_stop_link_train() must be called.
789 void intel_dp_start_link_train(struct intel_dp *intel_dp,
790 const struct intel_crtc_state *crtc_state)
793 * TODO: Reiniting LTTPRs here won't be needed once proper connector
794 * HW state readout is added.
796 int lttpr_count = intel_dp_lttpr_init(intel_dp);
798 if (!intel_dp_link_train_all_phys(intel_dp, crtc_state, lttpr_count))
799 intel_dp_schedule_fallback_link_training(intel_dp, crtc_state);