2 * Copyright 2009 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "nouveau_drv.h"
27 #include "nouveau_reg.h"
28 #include "nouveau_crtc.h"
29 #include "nouveau_i2c.h"
30 #include "nouveau_connector.h"
31 #include "nouveau_encoder.h"
34 auxch_rd(struct drm_encoder
*encoder
, int address
, uint8_t *buf
, int size
)
36 struct drm_device
*dev
= encoder
->dev
;
37 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
38 struct nouveau_i2c_chan
*auxch
;
41 auxch
= nouveau_i2c_find(dev
, nv_encoder
->dcb
->i2c_index
);
45 ret
= nouveau_dp_auxch(auxch
, 9, address
, buf
, size
);
53 auxch_wr(struct drm_encoder
*encoder
, int address
, uint8_t *buf
, int size
)
55 struct drm_device
*dev
= encoder
->dev
;
56 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
57 struct nouveau_i2c_chan
*auxch
;
60 auxch
= nouveau_i2c_find(dev
, nv_encoder
->dcb
->i2c_index
);
64 ret
= nouveau_dp_auxch(auxch
, 8, address
, buf
, size
);
69 nouveau_dp_lane_count_set(struct drm_encoder
*encoder
, uint8_t cmd
)
71 struct drm_device
*dev
= encoder
->dev
;
72 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
74 int or = nv_encoder
->or, link
= !(nv_encoder
->dcb
->sorconf
.link
& 1);
76 tmp
= nv_rd32(dev
, NV50_SOR_DP_CTRL(or, link
));
77 tmp
&= ~(NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED
|
78 NV50_SOR_DP_CTRL_LANE_MASK
);
79 tmp
|= ((1 << (cmd
& DP_LANE_COUNT_MASK
)) - 1) << 16;
80 if (cmd
& DP_LANE_COUNT_ENHANCED_FRAME_EN
)
81 tmp
|= NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED
;
82 nv_wr32(dev
, NV50_SOR_DP_CTRL(or, link
), tmp
);
84 return auxch_wr(encoder
, DP_LANE_COUNT_SET
, &cmd
, 1);
88 nouveau_dp_link_bw_set(struct drm_encoder
*encoder
, uint8_t cmd
)
90 struct drm_device
*dev
= encoder
->dev
;
91 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
93 int reg
= 0x614300 + (nv_encoder
->or * 0x800);
95 tmp
= nv_rd32(dev
, reg
);
97 if (cmd
== DP_LINK_BW_2_7
)
99 nv_wr32(dev
, reg
, tmp
);
101 return auxch_wr(encoder
, DP_LINK_BW_SET
, &cmd
, 1);
105 nouveau_dp_link_train_set(struct drm_encoder
*encoder
, int pattern
)
107 struct drm_device
*dev
= encoder
->dev
;
108 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
111 int or = nv_encoder
->or, link
= !(nv_encoder
->dcb
->sorconf
.link
& 1);
114 tmp
= nv_rd32(dev
, NV50_SOR_DP_CTRL(or, link
));
115 tmp
&= ~NV50_SOR_DP_CTRL_TRAINING_PATTERN
;
116 tmp
|= (pattern
<< 24);
117 nv_wr32(dev
, NV50_SOR_DP_CTRL(or, link
), tmp
);
119 ret
= auxch_rd(encoder
, DP_TRAINING_PATTERN_SET
, &cmd
, 1);
122 cmd
&= ~DP_TRAINING_PATTERN_MASK
;
123 cmd
|= (pattern
& DP_TRAINING_PATTERN_MASK
);
124 return auxch_wr(encoder
, DP_TRAINING_PATTERN_SET
, &cmd
, 1);
128 nouveau_dp_max_voltage_swing(struct drm_encoder
*encoder
)
130 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
131 struct drm_device
*dev
= encoder
->dev
;
132 struct bit_displayport_encoder_table_entry
*dpse
;
133 struct bit_displayport_encoder_table
*dpe
;
134 int i
, dpe_headerlen
, max_vs
= 0;
136 dpe
= nouveau_bios_dp_table(dev
, nv_encoder
->dcb
, &dpe_headerlen
);
139 dpse
= (void *)((char *)dpe
+ dpe_headerlen
);
141 for (i
= 0; i
< dpe_headerlen
; i
++, dpse
++) {
142 if (dpse
->vs_level
> max_vs
)
143 max_vs
= dpse
->vs_level
;
150 nouveau_dp_max_pre_emphasis(struct drm_encoder
*encoder
, int vs
)
152 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
153 struct drm_device
*dev
= encoder
->dev
;
154 struct bit_displayport_encoder_table_entry
*dpse
;
155 struct bit_displayport_encoder_table
*dpe
;
156 int i
, dpe_headerlen
, max_pre
= 0;
158 dpe
= nouveau_bios_dp_table(dev
, nv_encoder
->dcb
, &dpe_headerlen
);
161 dpse
= (void *)((char *)dpe
+ dpe_headerlen
);
163 for (i
= 0; i
< dpe_headerlen
; i
++, dpse
++) {
164 if (dpse
->vs_level
!= vs
)
167 if (dpse
->pre_level
> max_pre
)
168 max_pre
= dpse
->pre_level
;
175 nouveau_dp_link_train_adjust(struct drm_encoder
*encoder
, uint8_t *config
)
177 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
178 struct drm_device
*dev
= encoder
->dev
;
179 struct bit_displayport_encoder_table_entry
*dpse
;
180 struct bit_displayport_encoder_table
*dpe
;
181 int ret
, i
, dpe_headerlen
, vs
= 0, pre
= 0;
184 dpe
= nouveau_bios_dp_table(dev
, nv_encoder
->dcb
, &dpe_headerlen
);
187 dpse
= (void *)((char *)dpe
+ dpe_headerlen
);
189 ret
= auxch_rd(encoder
, DP_ADJUST_REQUEST_LANE0_1
, request
, 2);
193 NV_DEBUG_KMS(dev
, "\t\tadjust 0x%02x 0x%02x\n", request
[0], request
[1]);
195 /* Keep all lanes at the same level.. */
196 for (i
= 0; i
< nv_encoder
->dp
.link_nr
; i
++) {
197 int lane_req
= (request
[i
>> 1] >> ((i
& 1) << 2)) & 0xf;
198 int lane_vs
= lane_req
& 3;
199 int lane_pre
= (lane_req
>> 2) & 3;
207 if (vs
>= nouveau_dp_max_voltage_swing(encoder
)) {
208 vs
= nouveau_dp_max_voltage_swing(encoder
);
212 if (pre
>= nouveau_dp_max_pre_emphasis(encoder
, vs
& 3)) {
213 pre
= nouveau_dp_max_pre_emphasis(encoder
, vs
& 3);
217 /* Update the configuration for all lanes.. */
218 for (i
= 0; i
< nv_encoder
->dp
.link_nr
; i
++)
219 config
[i
] = (pre
<< 3) | vs
;
225 nouveau_dp_link_train_commit(struct drm_encoder
*encoder
, uint8_t *config
)
227 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
228 struct drm_device
*dev
= encoder
->dev
;
229 struct bit_displayport_encoder_table_entry
*dpse
;
230 struct bit_displayport_encoder_table
*dpe
;
231 int or = nv_encoder
->or, link
= !(nv_encoder
->dcb
->sorconf
.link
& 1);
232 int dpe_headerlen
, ret
, i
;
234 NV_DEBUG_KMS(dev
, "\t\tconfig 0x%02x 0x%02x 0x%02x 0x%02x\n",
235 config
[0], config
[1], config
[2], config
[3]);
237 dpe
= nouveau_bios_dp_table(dev
, nv_encoder
->dcb
, &dpe_headerlen
);
240 dpse
= (void *)((char *)dpe
+ dpe_headerlen
);
242 for (i
= 0; i
< dpe
->record_nr
; i
++, dpse
++) {
243 if (dpse
->vs_level
== (config
[0] & 3) &&
244 dpse
->pre_level
== ((config
[0] >> 3) & 3))
247 BUG_ON(i
== dpe
->record_nr
);
249 for (i
= 0; i
< nv_encoder
->dp
.link_nr
; i
++) {
250 const int shift
[4] = { 16, 8, 0, 24 };
251 uint32_t mask
= 0xff << shift
[i
];
252 uint32_t reg0
, reg1
, reg2
;
254 reg0
= nv_rd32(dev
, NV50_SOR_DP_UNK118(or, link
)) & ~mask
;
255 reg0
|= (dpse
->reg0
<< shift
[i
]);
256 reg1
= nv_rd32(dev
, NV50_SOR_DP_UNK120(or, link
)) & ~mask
;
257 reg1
|= (dpse
->reg1
<< shift
[i
]);
258 reg2
= nv_rd32(dev
, NV50_SOR_DP_UNK130(or, link
)) & 0xffff00ff;
259 reg2
|= (dpse
->reg2
<< 8);
260 nv_wr32(dev
, NV50_SOR_DP_UNK118(or, link
), reg0
);
261 nv_wr32(dev
, NV50_SOR_DP_UNK120(or, link
), reg1
);
262 nv_wr32(dev
, NV50_SOR_DP_UNK130(or, link
), reg2
);
265 ret
= auxch_wr(encoder
, DP_TRAINING_LANE0_SET
, config
, 4);
273 nouveau_dp_link_train(struct drm_encoder
*encoder
)
275 struct drm_device
*dev
= encoder
->dev
;
276 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
277 struct nouveau_gpio_engine
*pgpio
= &dev_priv
->engine
.gpio
;
278 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
279 struct nouveau_connector
*nv_connector
;
280 struct bit_displayport_encoder_table
*dpe
;
282 uint8_t config
[4], status
[3];
283 bool cr_done
, cr_max_vs
, eq_done
;
284 int ret
= 0, i
, tries
, voltage
;
286 NV_DEBUG_KMS(dev
, "link training!!\n");
288 nv_connector
= nouveau_encoder_connector_get(nv_encoder
);
292 dpe
= nouveau_bios_dp_table(dev
, nv_encoder
->dcb
, &dpe_headerlen
);
294 NV_ERROR(dev
, "SOR-%d: no DP encoder table!\n", nv_encoder
->or);
298 /* disable hotplug detect, this flips around on some panels during
301 pgpio
->irq_enable(dev
, nv_connector
->dcb
->gpio_tag
, false);
304 NV_DEBUG_KMS(dev
, "SOR-%d: running DP script 0\n", nv_encoder
->or);
305 nouveau_bios_run_init_table(dev
, le16_to_cpu(dpe
->script0
),
307 nouveau_crtc(encoder
->crtc
)->index
);
311 cr_done
= eq_done
= false;
313 /* set link configuration */
314 NV_DEBUG_KMS(dev
, "\tbegin train: bw %d, lanes %d\n",
315 nv_encoder
->dp
.link_bw
, nv_encoder
->dp
.link_nr
);
317 ret
= nouveau_dp_link_bw_set(encoder
, nv_encoder
->dp
.link_bw
);
321 config
[0] = nv_encoder
->dp
.link_nr
;
322 if (nv_encoder
->dp
.dpcd_version
>= 0x11 &&
323 nv_encoder
->dp
.enhanced_frame
)
324 config
[0] |= DP_LANE_COUNT_ENHANCED_FRAME_EN
;
326 ret
= nouveau_dp_lane_count_set(encoder
, config
[0]);
331 NV_DEBUG_KMS(dev
, "\tbegin cr\n");
332 ret
= nouveau_dp_link_train_set(encoder
, DP_TRAINING_PATTERN_1
);
338 memset(config
, 0x00, sizeof(config
));
340 if (!nouveau_dp_link_train_commit(encoder
, config
))
345 ret
= auxch_rd(encoder
, DP_LANE0_1_STATUS
, status
, 2);
348 NV_DEBUG_KMS(dev
, "\t\tstatus: 0x%02x 0x%02x\n",
349 status
[0], status
[1]);
353 for (i
= 0; i
< nv_encoder
->dp
.link_nr
; i
++) {
354 int lane
= (status
[i
>> 1] >> ((i
& 1) * 4)) & 0xf;
356 if (!(lane
& DP_LANE_CR_DONE
)) {
358 if (config
[i
] & DP_TRAIN_MAX_PRE_EMPHASIS_REACHED
)
364 if ((config
[0] & DP_TRAIN_VOLTAGE_SWING_MASK
) != voltage
) {
365 voltage
= config
[0] & DP_TRAIN_VOLTAGE_SWING_MASK
;
369 if (cr_done
|| cr_max_vs
|| (++tries
== 5))
372 if (!nouveau_dp_link_train_adjust(encoder
, config
))
379 /* channel equalisation */
380 NV_DEBUG_KMS(dev
, "\tbegin eq\n");
381 ret
= nouveau_dp_link_train_set(encoder
, DP_TRAINING_PATTERN_2
);
385 for (tries
= 0; tries
<= 5; tries
++) {
388 ret
= auxch_rd(encoder
, DP_LANE0_1_STATUS
, status
, 3);
391 NV_DEBUG_KMS(dev
, "\t\tstatus: 0x%02x 0x%02x\n",
392 status
[0], status
[1]);
395 if (!(status
[2] & DP_INTERLANE_ALIGN_DONE
))
398 for (i
= 0; eq_done
&& i
< nv_encoder
->dp
.link_nr
; i
++) {
399 int lane
= (status
[i
>> 1] >> ((i
& 1) * 4)) & 0xf;
401 if (!(lane
& DP_LANE_CR_DONE
)) {
406 if (!(lane
& DP_LANE_CHANNEL_EQ_DONE
) ||
407 !(lane
& DP_LANE_SYMBOL_LOCKED
)) {
413 if (eq_done
|| !cr_done
)
416 if (!nouveau_dp_link_train_adjust(encoder
, config
) ||
417 !nouveau_dp_link_train_commit(encoder
, config
))
422 /* end link training */
423 ret
= nouveau_dp_link_train_set(encoder
, DP_TRAINING_PATTERN_DISABLE
);
427 /* retry at a lower setting, if possible */
428 if (!ret
&& !(eq_done
&& cr_done
)) {
429 NV_DEBUG_KMS(dev
, "\twe failed\n");
430 if (nv_encoder
->dp
.link_bw
!= DP_LINK_BW_1_62
) {
431 NV_DEBUG_KMS(dev
, "retry link training at low rate\n");
432 nv_encoder
->dp
.link_bw
= DP_LINK_BW_1_62
;
438 NV_DEBUG_KMS(dev
, "SOR-%d: running DP script 1\n", nv_encoder
->or);
439 nouveau_bios_run_init_table(dev
, le16_to_cpu(dpe
->script1
),
441 nouveau_crtc(encoder
->crtc
)->index
);
444 /* re-enable hotplug detect */
445 pgpio
->irq_enable(dev
, nv_connector
->dcb
->gpio_tag
, true);
451 nouveau_dp_detect(struct drm_encoder
*encoder
)
453 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
454 struct drm_device
*dev
= encoder
->dev
;
458 ret
= auxch_rd(encoder
, 0x0000, dpcd
, 4);
462 NV_DEBUG_KMS(dev
, "encoder: link_bw %d, link_nr %d\n"
463 "display: link_bw %d, link_nr %d version 0x%02x\n",
464 nv_encoder
->dcb
->dpconf
.link_bw
,
465 nv_encoder
->dcb
->dpconf
.link_nr
,
466 dpcd
[1], dpcd
[2] & 0x0f, dpcd
[0]);
468 nv_encoder
->dp
.dpcd_version
= dpcd
[0];
470 nv_encoder
->dp
.link_bw
= dpcd
[1];
471 if (nv_encoder
->dp
.link_bw
!= DP_LINK_BW_1_62
&&
472 !nv_encoder
->dcb
->dpconf
.link_bw
)
473 nv_encoder
->dp
.link_bw
= DP_LINK_BW_1_62
;
475 nv_encoder
->dp
.link_nr
= dpcd
[2] & DP_MAX_LANE_COUNT_MASK
;
476 if (nv_encoder
->dp
.link_nr
> nv_encoder
->dcb
->dpconf
.link_nr
)
477 nv_encoder
->dp
.link_nr
= nv_encoder
->dcb
->dpconf
.link_nr
;
479 nv_encoder
->dp
.enhanced_frame
= (dpcd
[2] & DP_ENHANCED_FRAME_CAP
);
485 nouveau_dp_auxch(struct nouveau_i2c_chan
*auxch
, int cmd
, int addr
,
486 uint8_t *data
, int data_nr
)
488 struct drm_device
*dev
= auxch
->dev
;
489 uint32_t tmp
, ctrl
, stat
= 0, data32
[4] = {};
490 int ret
= 0, i
, index
= auxch
->rd
;
492 NV_DEBUG_KMS(dev
, "ch %d cmd %d addr 0x%x len %d\n", index
, cmd
, addr
, data_nr
);
494 tmp
= nv_rd32(dev
, NV50_AUXCH_CTRL(auxch
->rd
));
495 nv_wr32(dev
, NV50_AUXCH_CTRL(auxch
->rd
), tmp
| 0x00100000);
496 tmp
= nv_rd32(dev
, NV50_AUXCH_CTRL(auxch
->rd
));
497 if (!(tmp
& 0x01000000)) {
498 NV_ERROR(dev
, "expected bit 24 == 1, got 0x%08x\n", tmp
);
503 for (i
= 0; i
< 3; i
++) {
504 tmp
= nv_rd32(dev
, NV50_AUXCH_STAT(auxch
->rd
));
505 if (tmp
& NV50_AUXCH_STAT_STATE_READY
)
516 memcpy(data32
, data
, data_nr
);
517 for (i
= 0; i
< 4; i
++) {
518 NV_DEBUG_KMS(dev
, "wr %d: 0x%08x\n", i
, data32
[i
]);
519 nv_wr32(dev
, NV50_AUXCH_DATA_OUT(index
, i
), data32
[i
]);
523 nv_wr32(dev
, NV50_AUXCH_ADDR(index
), addr
);
524 ctrl
= nv_rd32(dev
, NV50_AUXCH_CTRL(index
));
525 ctrl
&= ~(NV50_AUXCH_CTRL_CMD
| NV50_AUXCH_CTRL_LEN
);
526 ctrl
|= (cmd
<< NV50_AUXCH_CTRL_CMD_SHIFT
);
527 ctrl
|= ((data_nr
- 1) << NV50_AUXCH_CTRL_LEN_SHIFT
);
529 for (i
= 0; i
< 16; i
++) {
530 nv_wr32(dev
, NV50_AUXCH_CTRL(index
), ctrl
| 0x80000000);
531 nv_wr32(dev
, NV50_AUXCH_CTRL(index
), ctrl
);
532 nv_wr32(dev
, NV50_AUXCH_CTRL(index
), ctrl
| 0x00010000);
533 if (!nv_wait(dev
, NV50_AUXCH_CTRL(index
),
534 0x00010000, 0x00000000)) {
535 NV_ERROR(dev
, "expected bit 16 == 0, got 0x%08x\n",
536 nv_rd32(dev
, NV50_AUXCH_CTRL(index
)));
543 stat
= nv_rd32(dev
, NV50_AUXCH_STAT(index
));
544 if ((stat
& NV50_AUXCH_STAT_REPLY_AUX
) !=
545 NV50_AUXCH_STAT_REPLY_AUX_DEFER
)
550 NV_ERROR(dev
, "auxch DEFER too many times, bailing\n");
556 if ((stat
& NV50_AUXCH_STAT_COUNT
) != data_nr
) {
561 for (i
= 0; i
< 4; i
++) {
562 data32
[i
] = nv_rd32(dev
, NV50_AUXCH_DATA_IN(index
, i
));
563 NV_DEBUG_KMS(dev
, "rd %d: 0x%08x\n", i
, data32
[i
]);
565 memcpy(data
, data32
, data_nr
);
569 tmp
= nv_rd32(dev
, NV50_AUXCH_CTRL(auxch
->rd
));
570 nv_wr32(dev
, NV50_AUXCH_CTRL(auxch
->rd
), tmp
& ~0x00100000);
571 tmp
= nv_rd32(dev
, NV50_AUXCH_CTRL(auxch
->rd
));
572 if (tmp
& 0x01000000) {
573 NV_ERROR(dev
, "expected bit 24 == 0, got 0x%08x\n", tmp
);
579 return ret
? ret
: (stat
& NV50_AUXCH_STAT_REPLY
);
585 nouveau_dp_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
587 struct nouveau_i2c_chan
*auxch
= (struct nouveau_i2c_chan
*)adap
;
588 struct drm_device
*dev
= auxch
->dev
;
589 struct i2c_msg
*msg
= msgs
;
593 u8 remaining
= msg
->len
;
597 u8 cnt
= (remaining
> 16) ? 16 : remaining
;
600 if (msg
->flags
& I2C_M_RD
)
605 if (mcnt
|| remaining
> 16)
608 ret
= nouveau_dp_auxch(auxch
, cmd
, msg
->addr
, ptr
, cnt
);
612 switch (ret
& NV50_AUXCH_STAT_REPLY_I2C
) {
613 case NV50_AUXCH_STAT_REPLY_I2C_ACK
:
615 case NV50_AUXCH_STAT_REPLY_I2C_NACK
:
617 case NV50_AUXCH_STAT_REPLY_I2C_DEFER
:
621 NV_ERROR(dev
, "bad auxch reply: 0x%08x\n", ret
);
636 nouveau_dp_i2c_func(struct i2c_adapter
*adap
)
638 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
641 const struct i2c_algorithm nouveau_dp_i2c_algo
= {
642 .master_xfer
= nouveau_dp_i2c_xfer
,
643 .functionality
= nouveau_dp_i2c_func