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