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