xref: /linux/drivers/gpu/drm/i915/display/intel_dpio_phy.c (revision b3e328dcedc11f1a17dfbc9baedebf6938b5c878)
1 /*
2  * Copyright © 2014-2016 Intel Corporation
3  *
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:
10  *
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.
14  *
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.
22  */
23 
24 #include "bxt_dpio_phy_regs.h"
25 #include "i915_reg.h"
26 #include "intel_ddi.h"
27 #include "intel_ddi_buf_trans.h"
28 #include "intel_de.h"
29 #include "intel_display_power_well.h"
30 #include "intel_display_types.h"
31 #include "intel_dp.h"
32 #include "intel_dpio_phy.h"
33 #include "vlv_sideband.h"
34 
35 /**
36  * DOC: DPIO
37  *
38  * VLV, CHV and BXT have slightly peculiar display PHYs for driving DP/HDMI
39  * ports. DPIO is the name given to such a display PHY. These PHYs
40  * don't follow the standard programming model using direct MMIO
41  * registers, and instead their registers must be accessed trough IOSF
42  * sideband. VLV has one such PHY for driving ports B and C, and CHV
43  * adds another PHY for driving port D. Each PHY responds to specific
44  * IOSF-SB port.
45  *
46  * Each display PHY is made up of one or two channels. Each channel
47  * houses a common lane part which contains the PLL and other common
48  * logic. CH0 common lane also contains the IOSF-SB logic for the
49  * Common Register Interface (CRI) ie. the DPIO registers. CRI clock
50  * must be running when any DPIO registers are accessed.
51  *
52  * In addition to having their own registers, the PHYs are also
53  * controlled through some dedicated signals from the display
54  * controller. These include PLL reference clock enable, PLL enable,
55  * and CRI clock selection, for example.
56  *
57  * Eeach channel also has two splines (also called data lanes), and
58  * each spline is made up of one Physical Access Coding Sub-Layer
59  * (PCS) block and two TX lanes. So each channel has two PCS blocks
60  * and four TX lanes. The TX lanes are used as DP lanes or TMDS
61  * data/clock pairs depending on the output type.
62  *
63  * Additionally the PHY also contains an AUX lane with AUX blocks
64  * for each channel. This is used for DP AUX communication, but
65  * this fact isn't really relevant for the driver since AUX is
66  * controlled from the display controller side. No DPIO registers
67  * need to be accessed during AUX communication,
68  *
69  * Generally on VLV/CHV the common lane corresponds to the pipe and
70  * the spline (PCS/TX) corresponds to the port.
71  *
72  * For dual channel PHY (VLV/CHV):
73  *
74  *  pipe A == CMN/PLL/REF CH0
75  *
76  *  pipe B == CMN/PLL/REF CH1
77  *
78  *  port B == PCS/TX CH0
79  *
80  *  port C == PCS/TX CH1
81  *
82  * This is especially important when we cross the streams
83  * ie. drive port B with pipe B, or port C with pipe A.
84  *
85  * For single channel PHY (CHV):
86  *
87  *  pipe C == CMN/PLL/REF CH0
88  *
89  *  port D == PCS/TX CH0
90  *
91  * On BXT the entire PHY channel corresponds to the port. That means
92  * the PLL is also now associated with the port rather than the pipe,
93  * and so the clock needs to be routed to the appropriate transcoder.
94  * Port A PLL is directly connected to transcoder EDP and port B/C
95  * PLLs can be routed to any transcoder A/B/C.
96  *
97  * Note: DDI0 is digital port B, DD1 is digital port C, and DDI2 is
98  * digital port D (CHV) or port A (BXT). ::
99  *
100  *
101  *     Dual channel PHY (VLV/CHV/BXT)
102  *     ---------------------------------
103  *     |      CH0      |      CH1      |
104  *     |  CMN/PLL/REF  |  CMN/PLL/REF  |
105  *     |---------------|---------------| Display PHY
106  *     | PCS01 | PCS23 | PCS01 | PCS23 |
107  *     |-------|-------|-------|-------|
108  *     |TX0|TX1|TX2|TX3|TX0|TX1|TX2|TX3|
109  *     ---------------------------------
110  *     |     DDI0      |     DDI1      | DP/HDMI ports
111  *     ---------------------------------
112  *
113  *     Single channel PHY (CHV/BXT)
114  *     -----------------
115  *     |      CH0      |
116  *     |  CMN/PLL/REF  |
117  *     |---------------| Display PHY
118  *     | PCS01 | PCS23 |
119  *     |-------|-------|
120  *     |TX0|TX1|TX2|TX3|
121  *     -----------------
122  *     |     DDI2      | DP/HDMI port
123  *     -----------------
124  */
125 
126 /**
127  * struct bxt_dpio_phy_info - Hold info for a broxton DDI phy
128  */
129 struct bxt_dpio_phy_info {
130 	/**
131 	 * @dual_channel: true if this phy has a second channel.
132 	 */
133 	bool dual_channel;
134 
135 	/**
136 	 * @rcomp_phy: If -1, indicates this phy has its own rcomp resistor.
137 	 * Otherwise the GRC value will be copied from the phy indicated by
138 	 * this field.
139 	 */
140 	enum dpio_phy rcomp_phy;
141 
142 	/**
143 	 * @reset_delay: delay in us to wait before setting the common reset
144 	 * bit in BXT_PHY_CTL_FAMILY, which effectively enables the phy.
145 	 */
146 	int reset_delay;
147 
148 	/**
149 	 * @pwron_mask: Mask with the appropriate bit set that would cause the
150 	 * punit to power this phy if written to BXT_P_CR_GT_DISP_PWRON.
151 	 */
152 	u32 pwron_mask;
153 
154 	/**
155 	 * @channel: struct containing per channel information.
156 	 */
157 	struct {
158 		/**
159 		 * @channel.port: which port maps to this channel.
160 		 */
161 		enum port port;
162 	} channel[2];
163 };
164 
165 static const struct bxt_dpio_phy_info bxt_dpio_phy_info[] = {
166 	[DPIO_PHY0] = {
167 		.dual_channel = true,
168 		.rcomp_phy = DPIO_PHY1,
169 		.pwron_mask = BIT(0),
170 
171 		.channel = {
172 			[DPIO_CH0] = { .port = PORT_B },
173 			[DPIO_CH1] = { .port = PORT_C },
174 		}
175 	},
176 	[DPIO_PHY1] = {
177 		.dual_channel = false,
178 		.rcomp_phy = -1,
179 		.pwron_mask = BIT(1),
180 
181 		.channel = {
182 			[DPIO_CH0] = { .port = PORT_A },
183 		}
184 	},
185 };
186 
187 static const struct bxt_dpio_phy_info glk_dpio_phy_info[] = {
188 	[DPIO_PHY0] = {
189 		.dual_channel = false,
190 		.rcomp_phy = DPIO_PHY1,
191 		.pwron_mask = BIT(0),
192 		.reset_delay = 20,
193 
194 		.channel = {
195 			[DPIO_CH0] = { .port = PORT_B },
196 		}
197 	},
198 	[DPIO_PHY1] = {
199 		.dual_channel = false,
200 		.rcomp_phy = -1,
201 		.pwron_mask = BIT(3),
202 		.reset_delay = 20,
203 
204 		.channel = {
205 			[DPIO_CH0] = { .port = PORT_A },
206 		}
207 	},
208 	[DPIO_PHY2] = {
209 		.dual_channel = false,
210 		.rcomp_phy = DPIO_PHY1,
211 		.pwron_mask = BIT(1),
212 		.reset_delay = 20,
213 
214 		.channel = {
215 			[DPIO_CH0] = { .port = PORT_C },
216 		}
217 	},
218 };
219 
220 static const struct bxt_dpio_phy_info *
221 bxt_get_phy_list(struct drm_i915_private *dev_priv, int *count)
222 {
223 	if (IS_GEMINILAKE(dev_priv)) {
224 		*count =  ARRAY_SIZE(glk_dpio_phy_info);
225 		return glk_dpio_phy_info;
226 	} else {
227 		*count =  ARRAY_SIZE(bxt_dpio_phy_info);
228 		return bxt_dpio_phy_info;
229 	}
230 }
231 
232 static const struct bxt_dpio_phy_info *
233 bxt_get_phy_info(struct drm_i915_private *dev_priv, enum dpio_phy phy)
234 {
235 	int count;
236 	const struct bxt_dpio_phy_info *phy_list =
237 		bxt_get_phy_list(dev_priv, &count);
238 
239 	return &phy_list[phy];
240 }
241 
242 void bxt_port_to_phy_channel(struct drm_i915_private *dev_priv, enum port port,
243 			     enum dpio_phy *phy, enum dpio_channel *ch)
244 {
245 	const struct bxt_dpio_phy_info *phy_info, *phys;
246 	int i, count;
247 
248 	phys = bxt_get_phy_list(dev_priv, &count);
249 
250 	for (i = 0; i < count; i++) {
251 		phy_info = &phys[i];
252 
253 		if (port == phy_info->channel[DPIO_CH0].port) {
254 			*phy = i;
255 			*ch = DPIO_CH0;
256 			return;
257 		}
258 
259 		if (phy_info->dual_channel &&
260 		    port == phy_info->channel[DPIO_CH1].port) {
261 			*phy = i;
262 			*ch = DPIO_CH1;
263 			return;
264 		}
265 	}
266 
267 	drm_WARN(&dev_priv->drm, 1, "PHY not found for PORT %c",
268 		 port_name(port));
269 	*phy = DPIO_PHY0;
270 	*ch = DPIO_CH0;
271 }
272 
273 /*
274  * Like intel_de_rmw() but reads from a single per-lane register and
275  * writes to the group register to write the same value to all the lanes.
276  */
277 static u32 bxt_dpio_phy_rmw_grp(struct drm_i915_private *i915,
278 				i915_reg_t reg_single,
279 				i915_reg_t reg_group,
280 				u32 clear, u32 set)
281 {
282 	u32 old, val;
283 
284 	old = intel_de_read(i915, reg_single);
285 	val = (old & ~clear) | set;
286 	intel_de_write(i915, reg_group, val);
287 
288 	return old;
289 }
290 
291 void bxt_dpio_phy_set_signal_levels(struct intel_encoder *encoder,
292 				    const struct intel_crtc_state *crtc_state)
293 {
294 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
295 	const struct intel_ddi_buf_trans *trans;
296 	enum dpio_channel ch;
297 	enum dpio_phy phy;
298 	int lane, n_entries;
299 
300 	trans = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
301 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !trans))
302 		return;
303 
304 	bxt_port_to_phy_channel(dev_priv, encoder->port, &phy, &ch);
305 
306 	/*
307 	 * While we write to the group register to program all lanes at once we
308 	 * can read only lane registers and we pick lanes 0/1 for that.
309 	 */
310 	bxt_dpio_phy_rmw_grp(dev_priv, BXT_PORT_PCS_DW10_LN01(phy, ch),
311 			     BXT_PORT_PCS_DW10_GRP(phy, ch),
312 			     TX2_SWING_CALC_INIT | TX1_SWING_CALC_INIT, 0);
313 
314 	for (lane = 0; lane < crtc_state->lane_count; lane++) {
315 		int level = intel_ddi_level(encoder, crtc_state, lane);
316 
317 		intel_de_rmw(dev_priv, BXT_PORT_TX_DW2_LN(phy, ch, lane),
318 			     MARGIN_000_MASK | UNIQ_TRANS_SCALE_MASK,
319 			     MARGIN_000(trans->entries[level].bxt.margin) |
320 			     UNIQ_TRANS_SCALE(trans->entries[level].bxt.scale));
321 	}
322 
323 	for (lane = 0; lane < crtc_state->lane_count; lane++) {
324 		int level = intel_ddi_level(encoder, crtc_state, lane);
325 		u32 val;
326 
327 		intel_de_rmw(dev_priv, BXT_PORT_TX_DW3_LN(phy, ch, lane),
328 			     SCALE_DCOMP_METHOD,
329 			     trans->entries[level].bxt.enable ?
330 			     SCALE_DCOMP_METHOD : 0);
331 
332 		val = intel_de_read(dev_priv, BXT_PORT_TX_DW3_LN(phy, ch, lane));
333 		if ((val & UNIQUE_TRANGE_EN_METHOD) && !(val & SCALE_DCOMP_METHOD))
334 			drm_err(&dev_priv->drm,
335 				"Disabled scaling while ouniqetrangenmethod was set");
336 	}
337 
338 	for (lane = 0; lane < crtc_state->lane_count; lane++) {
339 		int level = intel_ddi_level(encoder, crtc_state, lane);
340 
341 		intel_de_rmw(dev_priv, BXT_PORT_TX_DW4_LN(phy, ch, lane),
342 			     DE_EMPHASIS_MASK,
343 			     DE_EMPHASIS(trans->entries[level].bxt.deemphasis));
344 	}
345 
346 	bxt_dpio_phy_rmw_grp(dev_priv, BXT_PORT_PCS_DW10_LN01(phy, ch),
347 			     BXT_PORT_PCS_DW10_GRP(phy, ch),
348 			     0, TX2_SWING_CALC_INIT | TX1_SWING_CALC_INIT);
349 }
350 
351 bool bxt_dpio_phy_is_enabled(struct drm_i915_private *dev_priv,
352 			     enum dpio_phy phy)
353 {
354 	const struct bxt_dpio_phy_info *phy_info;
355 
356 	phy_info = bxt_get_phy_info(dev_priv, phy);
357 
358 	if (!(intel_de_read(dev_priv, BXT_P_CR_GT_DISP_PWRON) & phy_info->pwron_mask))
359 		return false;
360 
361 	if ((intel_de_read(dev_priv, BXT_PORT_CL1CM_DW0(phy)) &
362 	     (PHY_POWER_GOOD | PHY_RESERVED)) != PHY_POWER_GOOD) {
363 		drm_dbg(&dev_priv->drm,
364 			"DDI PHY %d powered, but power hasn't settled\n", phy);
365 
366 		return false;
367 	}
368 
369 	if (!(intel_de_read(dev_priv, BXT_PHY_CTL_FAMILY(phy)) & COMMON_RESET_DIS)) {
370 		drm_dbg(&dev_priv->drm,
371 			"DDI PHY %d powered, but still in reset\n", phy);
372 
373 		return false;
374 	}
375 
376 	return true;
377 }
378 
379 static u32 bxt_get_grc(struct drm_i915_private *dev_priv, enum dpio_phy phy)
380 {
381 	u32 val = intel_de_read(dev_priv, BXT_PORT_REF_DW6(phy));
382 
383 	return REG_FIELD_GET(GRC_CODE_MASK, val);
384 }
385 
386 static void bxt_phy_wait_grc_done(struct drm_i915_private *dev_priv,
387 				  enum dpio_phy phy)
388 {
389 	if (intel_de_wait_for_set(dev_priv, BXT_PORT_REF_DW3(phy),
390 				  GRC_DONE, 10))
391 		drm_err(&dev_priv->drm, "timeout waiting for PHY%d GRC\n",
392 			phy);
393 }
394 
395 static void _bxt_dpio_phy_init(struct drm_i915_private *dev_priv,
396 			       enum dpio_phy phy)
397 {
398 	const struct bxt_dpio_phy_info *phy_info;
399 	u32 val;
400 
401 	phy_info = bxt_get_phy_info(dev_priv, phy);
402 
403 	if (bxt_dpio_phy_is_enabled(dev_priv, phy)) {
404 		/* Still read out the GRC value for state verification */
405 		if (phy_info->rcomp_phy != -1)
406 			dev_priv->display.state.bxt_phy_grc = bxt_get_grc(dev_priv, phy);
407 
408 		if (bxt_dpio_phy_verify_state(dev_priv, phy)) {
409 			drm_dbg(&dev_priv->drm, "DDI PHY %d already enabled, "
410 				"won't reprogram it\n", phy);
411 			return;
412 		}
413 
414 		drm_dbg(&dev_priv->drm,
415 			"DDI PHY %d enabled with invalid state, "
416 			"force reprogramming it\n", phy);
417 	}
418 
419 	intel_de_rmw(dev_priv, BXT_P_CR_GT_DISP_PWRON, 0, phy_info->pwron_mask);
420 
421 	/*
422 	 * The PHY registers start out inaccessible and respond to reads with
423 	 * all 1s.  Eventually they become accessible as they power up, then
424 	 * the reserved bit will give the default 0.  Poll on the reserved bit
425 	 * becoming 0 to find when the PHY is accessible.
426 	 * The flag should get set in 100us according to the HW team, but
427 	 * use 1ms due to occasional timeouts observed with that.
428 	 */
429 	if (intel_de_wait_fw(dev_priv, BXT_PORT_CL1CM_DW0(phy),
430 			     PHY_RESERVED | PHY_POWER_GOOD, PHY_POWER_GOOD, 1))
431 		drm_err(&dev_priv->drm, "timeout during PHY%d power on\n",
432 			phy);
433 
434 	/* Program PLL Rcomp code offset */
435 	intel_de_rmw(dev_priv, BXT_PORT_CL1CM_DW9(phy),
436 		     IREF0RC_OFFSET_MASK, IREF0RC_OFFSET(0xE4));
437 
438 	intel_de_rmw(dev_priv, BXT_PORT_CL1CM_DW10(phy),
439 		     IREF1RC_OFFSET_MASK, IREF1RC_OFFSET(0xE4));
440 
441 	/* Program power gating */
442 	intel_de_rmw(dev_priv, BXT_PORT_CL1CM_DW28(phy), 0,
443 		     OCL1_POWER_DOWN_EN | DW28_OLDO_DYN_PWR_DOWN_EN | SUS_CLK_CONFIG);
444 
445 	if (phy_info->dual_channel)
446 		intel_de_rmw(dev_priv, BXT_PORT_CL2CM_DW6(phy), 0,
447 			     DW6_OLDO_DYN_PWR_DOWN_EN);
448 
449 	if (phy_info->rcomp_phy != -1) {
450 		u32 grc_code;
451 
452 		bxt_phy_wait_grc_done(dev_priv, phy_info->rcomp_phy);
453 
454 		/*
455 		 * PHY0 isn't connected to an RCOMP resistor so copy over
456 		 * the corresponding calibrated value from PHY1, and disable
457 		 * the automatic calibration on PHY0.
458 		 */
459 		val = bxt_get_grc(dev_priv, phy_info->rcomp_phy);
460 		dev_priv->display.state.bxt_phy_grc = val;
461 
462 		grc_code = GRC_CODE_FAST(val) |
463 			GRC_CODE_SLOW(val) |
464 			GRC_CODE_NOM(val);
465 		intel_de_write(dev_priv, BXT_PORT_REF_DW6(phy), grc_code);
466 		intel_de_rmw(dev_priv, BXT_PORT_REF_DW8(phy),
467 			     0, GRC_DIS | GRC_RDY_OVRD);
468 	}
469 
470 	if (phy_info->reset_delay)
471 		udelay(phy_info->reset_delay);
472 
473 	intel_de_rmw(dev_priv, BXT_PHY_CTL_FAMILY(phy), 0, COMMON_RESET_DIS);
474 }
475 
476 void bxt_dpio_phy_uninit(struct drm_i915_private *dev_priv, enum dpio_phy phy)
477 {
478 	const struct bxt_dpio_phy_info *phy_info;
479 
480 	phy_info = bxt_get_phy_info(dev_priv, phy);
481 
482 	intel_de_rmw(dev_priv, BXT_PHY_CTL_FAMILY(phy), COMMON_RESET_DIS, 0);
483 
484 	intel_de_rmw(dev_priv, BXT_P_CR_GT_DISP_PWRON, phy_info->pwron_mask, 0);
485 }
486 
487 void bxt_dpio_phy_init(struct drm_i915_private *dev_priv, enum dpio_phy phy)
488 {
489 	const struct bxt_dpio_phy_info *phy_info =
490 		bxt_get_phy_info(dev_priv, phy);
491 	enum dpio_phy rcomp_phy = phy_info->rcomp_phy;
492 	bool was_enabled;
493 
494 	lockdep_assert_held(&dev_priv->display.power.domains.lock);
495 
496 	was_enabled = true;
497 	if (rcomp_phy != -1)
498 		was_enabled = bxt_dpio_phy_is_enabled(dev_priv, rcomp_phy);
499 
500 	/*
501 	 * We need to copy the GRC calibration value from rcomp_phy,
502 	 * so make sure it's powered up.
503 	 */
504 	if (!was_enabled)
505 		_bxt_dpio_phy_init(dev_priv, rcomp_phy);
506 
507 	_bxt_dpio_phy_init(dev_priv, phy);
508 
509 	if (!was_enabled)
510 		bxt_dpio_phy_uninit(dev_priv, rcomp_phy);
511 }
512 
513 static bool __printf(6, 7)
514 __phy_reg_verify_state(struct drm_i915_private *dev_priv, enum dpio_phy phy,
515 		       i915_reg_t reg, u32 mask, u32 expected,
516 		       const char *reg_fmt, ...)
517 {
518 	struct va_format vaf;
519 	va_list args;
520 	u32 val;
521 
522 	val = intel_de_read(dev_priv, reg);
523 	if ((val & mask) == expected)
524 		return true;
525 
526 	va_start(args, reg_fmt);
527 	vaf.fmt = reg_fmt;
528 	vaf.va = &args;
529 
530 	drm_dbg(&dev_priv->drm, "DDI PHY %d reg %pV [%08x] state mismatch: "
531 			 "current %08x, expected %08x (mask %08x)\n",
532 			 phy, &vaf, reg.reg, val, (val & ~mask) | expected,
533 			 mask);
534 
535 	va_end(args);
536 
537 	return false;
538 }
539 
540 bool bxt_dpio_phy_verify_state(struct drm_i915_private *dev_priv,
541 			       enum dpio_phy phy)
542 {
543 	const struct bxt_dpio_phy_info *phy_info;
544 	u32 mask;
545 	bool ok;
546 
547 	phy_info = bxt_get_phy_info(dev_priv, phy);
548 
549 #define _CHK(reg, mask, exp, fmt, ...)					\
550 	__phy_reg_verify_state(dev_priv, phy, reg, mask, exp, fmt,	\
551 			       ## __VA_ARGS__)
552 
553 	if (!bxt_dpio_phy_is_enabled(dev_priv, phy))
554 		return false;
555 
556 	ok = true;
557 
558 	/* PLL Rcomp code offset */
559 	ok &= _CHK(BXT_PORT_CL1CM_DW9(phy),
560 		   IREF0RC_OFFSET_MASK, IREF0RC_OFFSET(0xe4),
561 		   "BXT_PORT_CL1CM_DW9(%d)", phy);
562 	ok &= _CHK(BXT_PORT_CL1CM_DW10(phy),
563 		   IREF1RC_OFFSET_MASK, IREF1RC_OFFSET(0xe4),
564 		   "BXT_PORT_CL1CM_DW10(%d)", phy);
565 
566 	/* Power gating */
567 	mask = OCL1_POWER_DOWN_EN | DW28_OLDO_DYN_PWR_DOWN_EN | SUS_CLK_CONFIG;
568 	ok &= _CHK(BXT_PORT_CL1CM_DW28(phy), mask, mask,
569 		   "BXT_PORT_CL1CM_DW28(%d)", phy);
570 
571 	if (phy_info->dual_channel)
572 		ok &= _CHK(BXT_PORT_CL2CM_DW6(phy),
573 			   DW6_OLDO_DYN_PWR_DOWN_EN, DW6_OLDO_DYN_PWR_DOWN_EN,
574 			   "BXT_PORT_CL2CM_DW6(%d)", phy);
575 
576 	if (phy_info->rcomp_phy != -1) {
577 		u32 grc_code = dev_priv->display.state.bxt_phy_grc;
578 
579 		grc_code = GRC_CODE_FAST(grc_code) |
580 			GRC_CODE_SLOW(grc_code) |
581 			GRC_CODE_NOM(grc_code);
582 		mask = GRC_CODE_FAST_MASK | GRC_CODE_SLOW_MASK |
583 		       GRC_CODE_NOM_MASK;
584 		ok &= _CHK(BXT_PORT_REF_DW6(phy), mask, grc_code,
585 			   "BXT_PORT_REF_DW6(%d)", phy);
586 
587 		mask = GRC_DIS | GRC_RDY_OVRD;
588 		ok &= _CHK(BXT_PORT_REF_DW8(phy), mask, mask,
589 			   "BXT_PORT_REF_DW8(%d)", phy);
590 	}
591 
592 	return ok;
593 #undef _CHK
594 }
595 
596 u8
597 bxt_dpio_phy_calc_lane_lat_optim_mask(u8 lane_count)
598 {
599 	switch (lane_count) {
600 	case 1:
601 		return 0;
602 	case 2:
603 		return BIT(2) | BIT(0);
604 	case 4:
605 		return BIT(3) | BIT(2) | BIT(0);
606 	default:
607 		MISSING_CASE(lane_count);
608 
609 		return 0;
610 	}
611 }
612 
613 void bxt_dpio_phy_set_lane_optim_mask(struct intel_encoder *encoder,
614 				      u8 lane_lat_optim_mask)
615 {
616 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
617 	enum port port = encoder->port;
618 	enum dpio_phy phy;
619 	enum dpio_channel ch;
620 	int lane;
621 
622 	bxt_port_to_phy_channel(dev_priv, port, &phy, &ch);
623 
624 	for (lane = 0; lane < 4; lane++) {
625 		/*
626 		 * Note that on CHV this flag is called UPAR, but has
627 		 * the same function.
628 		 */
629 		intel_de_rmw(dev_priv, BXT_PORT_TX_DW14_LN(phy, ch, lane),
630 			     LATENCY_OPTIM,
631 			     lane_lat_optim_mask & BIT(lane) ? LATENCY_OPTIM : 0);
632 	}
633 }
634 
635 u8
636 bxt_dpio_phy_get_lane_lat_optim_mask(struct intel_encoder *encoder)
637 {
638 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
639 	enum port port = encoder->port;
640 	enum dpio_phy phy;
641 	enum dpio_channel ch;
642 	int lane;
643 	u8 mask;
644 
645 	bxt_port_to_phy_channel(dev_priv, port, &phy, &ch);
646 
647 	mask = 0;
648 	for (lane = 0; lane < 4; lane++) {
649 		u32 val = intel_de_read(dev_priv,
650 					BXT_PORT_TX_DW14_LN(phy, ch, lane));
651 
652 		if (val & LATENCY_OPTIM)
653 			mask |= BIT(lane);
654 	}
655 
656 	return mask;
657 }
658 
659 enum dpio_channel vlv_dig_port_to_channel(struct intel_digital_port *dig_port)
660 {
661 	switch (dig_port->base.port) {
662 	default:
663 		MISSING_CASE(dig_port->base.port);
664 		fallthrough;
665 	case PORT_B:
666 	case PORT_D:
667 		return DPIO_CH0;
668 	case PORT_C:
669 		return DPIO_CH1;
670 	}
671 }
672 
673 enum dpio_phy vlv_dig_port_to_phy(struct intel_digital_port *dig_port)
674 {
675 	switch (dig_port->base.port) {
676 	default:
677 		MISSING_CASE(dig_port->base.port);
678 		fallthrough;
679 	case PORT_B:
680 	case PORT_C:
681 		return DPIO_PHY0;
682 	case PORT_D:
683 		return DPIO_PHY1;
684 	}
685 }
686 
687 enum dpio_phy vlv_pipe_to_phy(enum pipe pipe)
688 {
689 	switch (pipe) {
690 	default:
691 		MISSING_CASE(pipe);
692 		fallthrough;
693 	case PIPE_A:
694 	case PIPE_B:
695 		return DPIO_PHY0;
696 	case PIPE_C:
697 		return DPIO_PHY1;
698 	}
699 }
700 
701 enum dpio_channel vlv_pipe_to_channel(enum pipe pipe)
702 {
703 	switch (pipe) {
704 	default:
705 		MISSING_CASE(pipe);
706 		fallthrough;
707 	case PIPE_A:
708 	case PIPE_C:
709 		return DPIO_CH0;
710 	case PIPE_B:
711 		return DPIO_CH1;
712 	}
713 }
714 
715 void chv_set_phy_signal_level(struct intel_encoder *encoder,
716 			      const struct intel_crtc_state *crtc_state,
717 			      u32 deemph_reg_value, u32 margin_reg_value,
718 			      bool uniq_trans_scale)
719 {
720 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
721 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
722 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
723 	enum dpio_channel ch = vlv_dig_port_to_channel(dig_port);
724 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
725 	u32 val;
726 	int i;
727 
728 	vlv_dpio_get(dev_priv);
729 
730 	/* Clear calc init */
731 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW10(ch));
732 	val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
733 	val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
734 	val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
735 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW10(ch), val);
736 
737 	if (crtc_state->lane_count > 2) {
738 		val = vlv_dpio_read(dev_priv, phy, VLV_PCS23_DW10(ch));
739 		val &= ~(DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3);
740 		val &= ~(DPIO_PCS_TX1DEEMP_MASK | DPIO_PCS_TX2DEEMP_MASK);
741 		val |= DPIO_PCS_TX1DEEMP_9P5 | DPIO_PCS_TX2DEEMP_9P5;
742 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW10(ch), val);
743 	}
744 
745 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW9(ch));
746 	val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
747 	val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
748 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW9(ch), val);
749 
750 	if (crtc_state->lane_count > 2) {
751 		val = vlv_dpio_read(dev_priv, phy, VLV_PCS23_DW9(ch));
752 		val &= ~(DPIO_PCS_TX1MARGIN_MASK | DPIO_PCS_TX2MARGIN_MASK);
753 		val |= DPIO_PCS_TX1MARGIN_000 | DPIO_PCS_TX2MARGIN_000;
754 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW9(ch), val);
755 	}
756 
757 	/* Program swing deemph */
758 	for (i = 0; i < crtc_state->lane_count; i++) {
759 		val = vlv_dpio_read(dev_priv, phy, CHV_TX_DW4(ch, i));
760 		val &= ~DPIO_SWING_DEEMPH9P5_MASK;
761 		val |= deemph_reg_value << DPIO_SWING_DEEMPH9P5_SHIFT;
762 		vlv_dpio_write(dev_priv, phy, CHV_TX_DW4(ch, i), val);
763 	}
764 
765 	/* Program swing margin */
766 	for (i = 0; i < crtc_state->lane_count; i++) {
767 		val = vlv_dpio_read(dev_priv, phy, CHV_TX_DW2(ch, i));
768 
769 		val &= ~DPIO_SWING_MARGIN000_MASK;
770 		val |= margin_reg_value << DPIO_SWING_MARGIN000_SHIFT;
771 
772 		/*
773 		 * Supposedly this value shouldn't matter when unique transition
774 		 * scale is disabled, but in fact it does matter. Let's just
775 		 * always program the same value and hope it's OK.
776 		 */
777 		val &= ~(0xff << DPIO_UNIQ_TRANS_SCALE_SHIFT);
778 		val |= 0x9a << DPIO_UNIQ_TRANS_SCALE_SHIFT;
779 
780 		vlv_dpio_write(dev_priv, phy, CHV_TX_DW2(ch, i), val);
781 	}
782 
783 	/*
784 	 * The document said it needs to set bit 27 for ch0 and bit 26
785 	 * for ch1. Might be a typo in the doc.
786 	 * For now, for this unique transition scale selection, set bit
787 	 * 27 for ch0 and ch1.
788 	 */
789 	for (i = 0; i < crtc_state->lane_count; i++) {
790 		val = vlv_dpio_read(dev_priv, phy, CHV_TX_DW3(ch, i));
791 		if (uniq_trans_scale)
792 			val |= DPIO_TX_UNIQ_TRANS_SCALE_EN;
793 		else
794 			val &= ~DPIO_TX_UNIQ_TRANS_SCALE_EN;
795 		vlv_dpio_write(dev_priv, phy, CHV_TX_DW3(ch, i), val);
796 	}
797 
798 	/* Start swing calculation */
799 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW10(ch));
800 	val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
801 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW10(ch), val);
802 
803 	if (crtc_state->lane_count > 2) {
804 		val = vlv_dpio_read(dev_priv, phy, VLV_PCS23_DW10(ch));
805 		val |= DPIO_PCS_SWING_CALC_TX0_TX2 | DPIO_PCS_SWING_CALC_TX1_TX3;
806 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW10(ch), val);
807 	}
808 
809 	vlv_dpio_put(dev_priv);
810 }
811 
812 void chv_data_lane_soft_reset(struct intel_encoder *encoder,
813 			      const struct intel_crtc_state *crtc_state,
814 			      bool reset)
815 {
816 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
817 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
818 	enum dpio_channel ch = vlv_dig_port_to_channel(enc_to_dig_port(encoder));
819 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
820 	u32 val;
821 
822 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW0(ch));
823 	if (reset)
824 		val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
825 	else
826 		val |= DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET;
827 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW0(ch), val);
828 
829 	if (crtc_state->lane_count > 2) {
830 		val = vlv_dpio_read(dev_priv, phy, VLV_PCS23_DW0(ch));
831 		if (reset)
832 			val &= ~(DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET);
833 		else
834 			val |= DPIO_PCS_TX_LANE2_RESET | DPIO_PCS_TX_LANE1_RESET;
835 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW0(ch), val);
836 	}
837 
838 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW1(ch));
839 	val |= CHV_PCS_REQ_SOFTRESET_EN;
840 	if (reset)
841 		val &= ~DPIO_PCS_CLK_SOFT_RESET;
842 	else
843 		val |= DPIO_PCS_CLK_SOFT_RESET;
844 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW1(ch), val);
845 
846 	if (crtc_state->lane_count > 2) {
847 		val = vlv_dpio_read(dev_priv, phy, VLV_PCS23_DW1(ch));
848 		val |= CHV_PCS_REQ_SOFTRESET_EN;
849 		if (reset)
850 			val &= ~DPIO_PCS_CLK_SOFT_RESET;
851 		else
852 			val |= DPIO_PCS_CLK_SOFT_RESET;
853 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW1(ch), val);
854 	}
855 }
856 
857 void chv_phy_pre_pll_enable(struct intel_encoder *encoder,
858 			    const struct intel_crtc_state *crtc_state)
859 {
860 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
861 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
862 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
863 	enum dpio_channel ch = vlv_dig_port_to_channel(dig_port);
864 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
865 	enum pipe pipe = crtc->pipe;
866 	unsigned int lane_mask =
867 		intel_dp_unused_lane_mask(crtc_state->lane_count);
868 	u32 val;
869 
870 	/*
871 	 * Must trick the second common lane into life.
872 	 * Otherwise we can't even access the PLL.
873 	 */
874 	if (ch == DPIO_CH0 && pipe == PIPE_B)
875 		dig_port->release_cl2_override =
876 			!chv_phy_powergate_ch(dev_priv, DPIO_PHY0, DPIO_CH1, true);
877 
878 	chv_phy_powergate_lanes(encoder, true, lane_mask);
879 
880 	vlv_dpio_get(dev_priv);
881 
882 	/* Assert data lane reset */
883 	chv_data_lane_soft_reset(encoder, crtc_state, true);
884 
885 	/* program left/right clock distribution */
886 	if (pipe != PIPE_B) {
887 		val = vlv_dpio_read(dev_priv, phy, _CHV_CMN_DW5_CH0);
888 		val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
889 		if (ch == DPIO_CH0)
890 			val |= CHV_BUFLEFTENA1_FORCE;
891 		if (ch == DPIO_CH1)
892 			val |= CHV_BUFRIGHTENA1_FORCE;
893 		vlv_dpio_write(dev_priv, phy, _CHV_CMN_DW5_CH0, val);
894 	} else {
895 		val = vlv_dpio_read(dev_priv, phy, _CHV_CMN_DW1_CH1);
896 		val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
897 		if (ch == DPIO_CH0)
898 			val |= CHV_BUFLEFTENA2_FORCE;
899 		if (ch == DPIO_CH1)
900 			val |= CHV_BUFRIGHTENA2_FORCE;
901 		vlv_dpio_write(dev_priv, phy, _CHV_CMN_DW1_CH1, val);
902 	}
903 
904 	/* program clock channel usage */
905 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW8(ch));
906 	val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
907 	if (pipe != PIPE_B)
908 		val &= ~CHV_PCS_USEDCLKCHANNEL;
909 	else
910 		val |= CHV_PCS_USEDCLKCHANNEL;
911 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW8(ch), val);
912 
913 	if (crtc_state->lane_count > 2) {
914 		val = vlv_dpio_read(dev_priv, phy, VLV_PCS23_DW8(ch));
915 		val |= CHV_PCS_USEDCLKCHANNEL_OVRRIDE;
916 		if (pipe != PIPE_B)
917 			val &= ~CHV_PCS_USEDCLKCHANNEL;
918 		else
919 			val |= CHV_PCS_USEDCLKCHANNEL;
920 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW8(ch), val);
921 	}
922 
923 	/*
924 	 * This a a bit weird since generally CL
925 	 * matches the pipe, but here we need to
926 	 * pick the CL based on the port.
927 	 */
928 	val = vlv_dpio_read(dev_priv, phy, CHV_CMN_DW19(ch));
929 	if (pipe != PIPE_B)
930 		val &= ~CHV_CMN_USEDCLKCHANNEL;
931 	else
932 		val |= CHV_CMN_USEDCLKCHANNEL;
933 	vlv_dpio_write(dev_priv, phy, CHV_CMN_DW19(ch), val);
934 
935 	vlv_dpio_put(dev_priv);
936 }
937 
938 void chv_phy_pre_encoder_enable(struct intel_encoder *encoder,
939 				const struct intel_crtc_state *crtc_state)
940 {
941 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
942 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
943 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
944 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
945 	enum dpio_channel ch = vlv_dig_port_to_channel(dig_port);
946 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
947 	int data, i, stagger;
948 	u32 val;
949 
950 	vlv_dpio_get(dev_priv);
951 
952 	/* allow hardware to manage TX FIFO reset source */
953 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW11(ch));
954 	val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
955 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW11(ch), val);
956 
957 	if (crtc_state->lane_count > 2) {
958 		val = vlv_dpio_read(dev_priv, phy, VLV_PCS23_DW11(ch));
959 		val &= ~DPIO_LANEDESKEW_STRAP_OVRD;
960 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW11(ch), val);
961 	}
962 
963 	/* Program Tx lane latency optimal setting*/
964 	for (i = 0; i < crtc_state->lane_count; i++) {
965 		/* Set the upar bit */
966 		if (crtc_state->lane_count == 1)
967 			data = 0x0;
968 		else
969 			data = (i == 1) ? 0x0 : 0x1;
970 		vlv_dpio_write(dev_priv, phy, CHV_TX_DW14(ch, i),
971 				data << DPIO_UPAR_SHIFT);
972 	}
973 
974 	/* Data lane stagger programming */
975 	if (crtc_state->port_clock > 270000)
976 		stagger = 0x18;
977 	else if (crtc_state->port_clock > 135000)
978 		stagger = 0xd;
979 	else if (crtc_state->port_clock > 67500)
980 		stagger = 0x7;
981 	else if (crtc_state->port_clock > 33750)
982 		stagger = 0x4;
983 	else
984 		stagger = 0x2;
985 
986 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW11(ch));
987 	val |= DPIO_TX2_STAGGER_MASK(0x1f);
988 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW11(ch), val);
989 
990 	if (crtc_state->lane_count > 2) {
991 		val = vlv_dpio_read(dev_priv, phy, VLV_PCS23_DW11(ch));
992 		val |= DPIO_TX2_STAGGER_MASK(0x1f);
993 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW11(ch), val);
994 	}
995 
996 	vlv_dpio_write(dev_priv, phy, VLV_PCS01_DW12(ch),
997 		       DPIO_LANESTAGGER_STRAP(stagger) |
998 		       DPIO_LANESTAGGER_STRAP_OVRD |
999 		       DPIO_TX1_STAGGER_MASK(0x1f) |
1000 		       DPIO_TX1_STAGGER_MULT(6) |
1001 		       DPIO_TX2_STAGGER_MULT(0));
1002 
1003 	if (crtc_state->lane_count > 2) {
1004 		vlv_dpio_write(dev_priv, phy, VLV_PCS23_DW12(ch),
1005 			       DPIO_LANESTAGGER_STRAP(stagger) |
1006 			       DPIO_LANESTAGGER_STRAP_OVRD |
1007 			       DPIO_TX1_STAGGER_MASK(0x1f) |
1008 			       DPIO_TX1_STAGGER_MULT(7) |
1009 			       DPIO_TX2_STAGGER_MULT(5));
1010 	}
1011 
1012 	/* Deassert data lane reset */
1013 	chv_data_lane_soft_reset(encoder, crtc_state, false);
1014 
1015 	vlv_dpio_put(dev_priv);
1016 }
1017 
1018 void chv_phy_release_cl2_override(struct intel_encoder *encoder)
1019 {
1020 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1021 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1022 
1023 	if (dig_port->release_cl2_override) {
1024 		chv_phy_powergate_ch(dev_priv, DPIO_PHY0, DPIO_CH1, false);
1025 		dig_port->release_cl2_override = false;
1026 	}
1027 }
1028 
1029 void chv_phy_post_pll_disable(struct intel_encoder *encoder,
1030 			      const struct intel_crtc_state *old_crtc_state)
1031 {
1032 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1033 	enum pipe pipe = to_intel_crtc(old_crtc_state->uapi.crtc)->pipe;
1034 	enum dpio_phy phy = vlv_pipe_to_phy(pipe);
1035 	u32 val;
1036 
1037 	vlv_dpio_get(dev_priv);
1038 
1039 	/* disable left/right clock distribution */
1040 	if (pipe != PIPE_B) {
1041 		val = vlv_dpio_read(dev_priv, phy, _CHV_CMN_DW5_CH0);
1042 		val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1043 		vlv_dpio_write(dev_priv, phy, _CHV_CMN_DW5_CH0, val);
1044 	} else {
1045 		val = vlv_dpio_read(dev_priv, phy, _CHV_CMN_DW1_CH1);
1046 		val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1047 		vlv_dpio_write(dev_priv, phy, _CHV_CMN_DW1_CH1, val);
1048 	}
1049 
1050 	vlv_dpio_put(dev_priv);
1051 
1052 	/*
1053 	 * Leave the power down bit cleared for at least one
1054 	 * lane so that chv_powergate_phy_ch() will power
1055 	 * on something when the channel is otherwise unused.
1056 	 * When the port is off and the override is removed
1057 	 * the lanes power down anyway, so otherwise it doesn't
1058 	 * really matter what the state of power down bits is
1059 	 * after this.
1060 	 */
1061 	chv_phy_powergate_lanes(encoder, false, 0x0);
1062 }
1063 
1064 void vlv_set_phy_signal_level(struct intel_encoder *encoder,
1065 			      const struct intel_crtc_state *crtc_state,
1066 			      u32 demph_reg_value, u32 preemph_reg_value,
1067 			      u32 uniqtranscale_reg_value, u32 tx3_demph)
1068 {
1069 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1070 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1071 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1072 	enum dpio_channel port = vlv_dig_port_to_channel(dig_port);
1073 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
1074 
1075 	vlv_dpio_get(dev_priv);
1076 
1077 	vlv_dpio_write(dev_priv, phy, VLV_TX_DW5(port), 0x00000000);
1078 	vlv_dpio_write(dev_priv, phy, VLV_TX_DW4(port), demph_reg_value);
1079 	vlv_dpio_write(dev_priv, phy, VLV_TX_DW2(port),
1080 			 uniqtranscale_reg_value);
1081 	vlv_dpio_write(dev_priv, phy, VLV_TX_DW3(port), 0x0C782040);
1082 
1083 	if (tx3_demph)
1084 		vlv_dpio_write(dev_priv, phy, VLV_TX3_DW4(port), tx3_demph);
1085 
1086 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW11(port), 0x00030000);
1087 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW9(port), preemph_reg_value);
1088 	vlv_dpio_write(dev_priv, phy, VLV_TX_DW5(port), DPIO_TX_OCALINIT_EN);
1089 
1090 	vlv_dpio_put(dev_priv);
1091 }
1092 
1093 void vlv_phy_pre_pll_enable(struct intel_encoder *encoder,
1094 			    const struct intel_crtc_state *crtc_state)
1095 {
1096 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1097 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1098 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1099 	enum dpio_channel port = vlv_dig_port_to_channel(dig_port);
1100 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
1101 
1102 	/* Program Tx lane resets to default */
1103 	vlv_dpio_get(dev_priv);
1104 
1105 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW0(port),
1106 			 DPIO_PCS_TX_LANE2_RESET |
1107 			 DPIO_PCS_TX_LANE1_RESET);
1108 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW1(port),
1109 			 DPIO_PCS_CLK_CRI_RXEB_EIOS_EN |
1110 			 DPIO_PCS_CLK_CRI_RXDIGFILTSG_EN |
1111 			 (1<<DPIO_PCS_CLK_DATAWIDTH_SHIFT) |
1112 				 DPIO_PCS_CLK_SOFT_RESET);
1113 
1114 	/* Fix up inter-pair skew failure */
1115 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW12(port), 0x00750f00);
1116 	vlv_dpio_write(dev_priv, phy, VLV_TX_DW11(port), 0x00001500);
1117 	vlv_dpio_write(dev_priv, phy, VLV_TX_DW14(port), 0x40400000);
1118 
1119 	vlv_dpio_put(dev_priv);
1120 }
1121 
1122 void vlv_phy_pre_encoder_enable(struct intel_encoder *encoder,
1123 				const struct intel_crtc_state *crtc_state)
1124 {
1125 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1126 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1127 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1128 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1129 	enum dpio_channel port = vlv_dig_port_to_channel(dig_port);
1130 	enum pipe pipe = crtc->pipe;
1131 	enum dpio_phy phy = vlv_pipe_to_phy(pipe);
1132 	u32 val;
1133 
1134 	vlv_dpio_get(dev_priv);
1135 
1136 	/* Enable clock channels for this port */
1137 	val = vlv_dpio_read(dev_priv, phy, VLV_PCS01_DW8(port));
1138 	val = 0;
1139 	if (pipe)
1140 		val |= (1<<21);
1141 	else
1142 		val &= ~(1<<21);
1143 	val |= 0x001000c4;
1144 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW8(port), val);
1145 
1146 	/* Program lane clock */
1147 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW14(port), 0x00760018);
1148 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW23(port), 0x00400888);
1149 
1150 	vlv_dpio_put(dev_priv);
1151 }
1152 
1153 void vlv_phy_reset_lanes(struct intel_encoder *encoder,
1154 			 const struct intel_crtc_state *old_crtc_state)
1155 {
1156 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1157 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1158 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1159 	enum dpio_channel port = vlv_dig_port_to_channel(dig_port);
1160 	enum dpio_phy phy = vlv_pipe_to_phy(crtc->pipe);
1161 
1162 	vlv_dpio_get(dev_priv);
1163 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW0(port), 0x00000000);
1164 	vlv_dpio_write(dev_priv, phy, VLV_PCS_DW1(port), 0x00e00060);
1165 	vlv_dpio_put(dev_priv);
1166 }
1167