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.
27 #include "nouveau_drv.h"
28 #include "nouveau_i2c.h"
29 #include "nouveau_connector.h"
30 #include "nouveau_encoder.h"
31 #include "nouveau_crtc.h"
32 #include "nouveau_gpio.h"
34 /******************************************************************************
35 * aux channel util functions
36 *****************************************************************************/
37 #define AUX_DBG(fmt, args...) do { \
38 if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_AUXCH) { \
39 NV_PRINTK(KERN_DEBUG, dev, "AUXCH(%d): " fmt, ch, ##args); \
42 #define AUX_ERR(fmt, args...) NV_ERROR(dev, "AUXCH(%d): " fmt, ch, ##args)
45 auxch_fini(struct drm_device
*dev
, int ch
)
47 nv_mask(dev
, 0x00e4e4 + (ch
* 0x50), 0x00310000, 0x00000000);
51 auxch_init(struct drm_device
*dev
, int ch
)
53 const u32 unksel
= 1; /* nfi which to use, or if it matters.. */
54 const u32 ureq
= unksel
? 0x00100000 : 0x00200000;
55 const u32 urep
= unksel
? 0x01000000 : 0x02000000;
58 /* wait up to 1ms for any previous transaction to be done... */
61 ctrl
= nv_rd32(dev
, 0x00e4e4 + (ch
* 0x50));
64 AUX_ERR("begin idle timeout 0x%08x", ctrl
);
67 } while (ctrl
& 0x03010000);
69 /* set some magic, and wait up to 1ms for it to appear */
70 nv_mask(dev
, 0x00e4e4 + (ch
* 0x50), 0x00300000, ureq
);
73 ctrl
= nv_rd32(dev
, 0x00e4e4 + (ch
* 0x50));
76 AUX_ERR("magic wait 0x%08x\n", ctrl
);
80 } while ((ctrl
& 0x03000000) != urep
);
86 auxch_tx(struct drm_device
*dev
, int ch
, u8 type
, u32 addr
, u8
*data
, u8 size
)
88 u32 ctrl
, stat
, timeout
, retries
;
92 AUX_DBG("%d: 0x%08x %d\n", type
, addr
, size
);
94 ret
= auxch_init(dev
, ch
);
98 stat
= nv_rd32(dev
, 0x00e4e8 + (ch
* 0x50));
99 if (!(stat
& 0x10000000)) {
100 AUX_DBG("sink not detected\n");
106 memcpy(xbuf
, data
, size
);
107 for (i
= 0; i
< 16; i
+= 4) {
108 AUX_DBG("wr 0x%08x\n", xbuf
[i
/ 4]);
109 nv_wr32(dev
, 0x00e4c0 + (ch
* 0x50) + i
, xbuf
[i
/ 4]);
113 ctrl
= nv_rd32(dev
, 0x00e4e4 + (ch
* 0x50));
117 nv_wr32(dev
, 0x00e4e0 + (ch
* 0x50), addr
);
119 /* retry transaction a number of times on failure... */
121 for (retries
= 0; retries
< 32; retries
++) {
122 /* reset, and delay a while if this is a retry */
123 nv_wr32(dev
, 0x00e4e4 + (ch
* 0x50), 0x80000000 | ctrl
);
124 nv_wr32(dev
, 0x00e4e4 + (ch
* 0x50), 0x00000000 | ctrl
);
128 /* transaction request, wait up to 1ms for it to complete */
129 nv_wr32(dev
, 0x00e4e4 + (ch
* 0x50), 0x00010000 | ctrl
);
133 ctrl
= nv_rd32(dev
, 0x00e4e4 + (ch
* 0x50));
136 AUX_ERR("tx req timeout 0x%08x\n", ctrl
);
139 } while (ctrl
& 0x00010000);
141 /* read status, and check if transaction completed ok */
142 stat
= nv_mask(dev
, 0x00e4e8 + (ch
* 0x50), 0, 0);
143 if (!(stat
& 0x000f0f00)) {
148 AUX_DBG("%02d 0x%08x 0x%08x\n", retries
, ctrl
, stat
);
152 for (i
= 0; i
< 16; i
+= 4) {
153 xbuf
[i
/ 4] = nv_rd32(dev
, 0x00e4d0 + (ch
* 0x50) + i
);
154 AUX_DBG("rd 0x%08x\n", xbuf
[i
/ 4]);
156 memcpy(data
, xbuf
, size
);
165 dp_link_bw_get(struct drm_device
*dev
, int or, int link
)
167 u32 ctrl
= nv_rd32(dev
, 0x614300 + (or * 0x800));
168 if (!(ctrl
& 0x000c0000))
174 dp_lane_count_get(struct drm_device
*dev
, int or, int link
)
176 u32 ctrl
= nv_rd32(dev
, NV50_SOR_DP_CTRL(or, link
));
177 switch (ctrl
& 0x000f0000) {
178 case 0x00010000: return 1;
179 case 0x00030000: return 2;
186 nouveau_dp_tu_update(struct drm_device
*dev
, int or, int link
, u32 clk
, u32 bpp
)
188 const u32 symbol
= 100000;
189 int bestTU
= 0, bestVTUi
= 0, bestVTUf
= 0, bestVTUa
= 0;
190 int TU
, VTUi
, VTUf
, VTUa
;
191 u64 link_data_rate
, link_ratio
, unk
;
192 u32 best_diff
= 64 * symbol
;
193 u32 link_nr
, link_bw
, r
;
195 /* calculate packed data rate for each lane */
196 link_nr
= dp_lane_count_get(dev
, or, link
);
197 link_data_rate
= (clk
* bpp
/ 8) / link_nr
;
199 /* calculate ratio of packed data rate to link symbol rate */
200 link_bw
= dp_link_bw_get(dev
, or, link
);
201 link_ratio
= link_data_rate
* symbol
;
202 r
= do_div(link_ratio
, link_bw
);
204 for (TU
= 64; TU
>= 32; TU
--) {
205 /* calculate average number of valid symbols in each TU */
206 u32 tu_valid
= link_ratio
* TU
;
209 /* find a hw representation for the fraction.. */
210 VTUi
= tu_valid
/ symbol
;
211 calc
= VTUi
* symbol
;
212 diff
= tu_valid
- calc
;
214 if (diff
>= (symbol
/ 2)) {
215 VTUf
= symbol
/ (symbol
- diff
);
216 if (symbol
- (VTUf
* diff
))
221 calc
+= symbol
- (symbol
/ VTUf
);
229 VTUf
= min((int)(symbol
/ diff
), 15);
230 calc
+= symbol
/ VTUf
;
233 diff
= calc
- tu_valid
;
235 /* no remainder, but the hw doesn't like the fractional
236 * part to be zero. decrement the integer part and
237 * have the fraction add a whole symbol back
244 if (diff
< best_diff
) {
256 NV_ERROR(dev
, "DP: unable to find suitable config\n");
260 /* XXX close to vbios numbers, but not right */
261 unk
= (symbol
- link_ratio
) * bestTU
;
263 r
= do_div(unk
, symbol
);
264 r
= do_div(unk
, symbol
);
267 nv_mask(dev
, NV50_SOR_DP_CTRL(or, link
), 0x000001fc, bestTU
<< 2);
268 nv_mask(dev
, NV50_SOR_DP_SCFG(or, link
), 0x010f7f3f, bestVTUa
<< 24 |
275 nouveau_dp_bios_data(struct drm_device
*dev
, struct dcb_entry
*dcb
, u8
**entry
)
281 if (bit_table(dev
, 'd', &d
)) {
282 NV_ERROR(dev
, "BIT 'd' table not found\n");
286 if (d
.version
!= 1) {
287 NV_ERROR(dev
, "BIT 'd' table version %d unknown\n", d
.version
);
291 table
= ROMPTR(dev
, d
.data
[0]);
293 NV_ERROR(dev
, "displayport table pointer invalid\n");
303 NV_ERROR(dev
, "displayport table 0x%02x unknown\n", table
[0]);
307 for (i
= 0; i
< table
[3]; i
++) {
308 *entry
= ROMPTR(dev
, table
[table
[1] + (i
* table
[2])]);
309 if (*entry
&& bios_encoder_match(dcb
, ROM32((*entry
)[0])))
313 NV_ERROR(dev
, "displayport encoder table not found\n");
317 /******************************************************************************
319 *****************************************************************************/
321 struct dcb_entry
*dcb
;
336 dp_set_link_config(struct drm_device
*dev
, struct dp_state
*dp
)
338 int or = dp
->or, link
= dp
->link
;
343 NV_DEBUG_KMS(dev
, "%d lanes at %d KB/s\n", dp
->link_nr
, dp
->link_bw
);
345 /* set selected link rate on source */
346 switch (dp
->link_bw
) {
348 nv_mask(dev
, 0x614300 + (or * 0x800), 0x000c0000, 0x00040000);
349 sink
[0] = DP_LINK_BW_2_7
;
352 nv_mask(dev
, 0x614300 + (or * 0x800), 0x000c0000, 0x00000000);
353 sink
[0] = DP_LINK_BW_1_62
;
357 /* offset +0x0a of each dp encoder table entry is a pointer to another
358 * table, that has (among other things) pointers to more scripts that
359 * need to be executed, this time depending on link speed.
361 entry
= ROMPTR(dev
, dp
->entry
[10]);
363 if (dp
->table
[0] < 0x30) {
364 while (dp
->link_bw
< (ROM16(entry
[0]) * 10))
366 script
= ROM16(entry
[2]);
368 while (dp
->link_bw
< (entry
[0] * 27000))
370 script
= ROM16(entry
[1]);
373 nouveau_bios_run_init_table(dev
, script
, dp
->dcb
, dp
->crtc
);
376 /* configure lane count on the source */
377 dp_ctrl
= ((1 << dp
->link_nr
) - 1) << 16;
378 sink
[1] = dp
->link_nr
;
379 if (dp
->dpcd
[2] & DP_ENHANCED_FRAME_CAP
) {
380 dp_ctrl
|= 0x00004000;
381 sink
[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN
;
384 nv_mask(dev
, NV50_SOR_DP_CTRL(or, link
), 0x001f4000, dp_ctrl
);
386 /* inform the sink of the new configuration */
387 auxch_tx(dev
, dp
->auxch
, 8, DP_LINK_BW_SET
, sink
, 2);
391 dp_set_training_pattern(struct drm_device
*dev
, struct dp_state
*dp
, u8 tp
)
395 NV_DEBUG_KMS(dev
, "training pattern %d\n", tp
);
397 nv_mask(dev
, NV50_SOR_DP_CTRL(dp
->or, dp
->link
), 0x0f000000, tp
<< 24);
399 auxch_tx(dev
, dp
->auxch
, 9, DP_TRAINING_PATTERN_SET
, &sink_tp
, 1);
400 sink_tp
&= ~DP_TRAINING_PATTERN_MASK
;
402 auxch_tx(dev
, dp
->auxch
, 8, DP_TRAINING_PATTERN_SET
, &sink_tp
, 1);
405 static const u8 nv50_lane_map
[] = { 16, 8, 0, 24 };
406 static const u8 nvaf_lane_map
[] = { 24, 16, 8, 0 };
409 dp_link_train_commit(struct drm_device
*dev
, struct dp_state
*dp
)
411 struct drm_nouveau_private
*dev_priv
= dev
->dev_private
;
412 u32 mask
= 0, drv
= 0, pre
= 0, unk
= 0;
418 if (dev_priv
->chipset
!= 0xaf)
419 shifts
= nv50_lane_map
;
421 shifts
= nvaf_lane_map
;
423 for (i
= 0; i
< dp
->link_nr
; i
++) {
424 u8
*conf
= dp
->entry
+ dp
->table
[4];
425 u8 lane
= (dp
->stat
[4 + (i
>> 1)] >> ((i
& 1) * 4)) & 0xf;
426 u8 lpre
= (lane
& 0x0c) >> 2;
427 u8 lvsw
= (lane
& 0x03) >> 0;
429 mask
|= 0xff << shifts
[i
];
430 unk
|= 1 << (shifts
[i
] >> 3);
432 dp
->conf
[i
] = (lpre
<< 3) | lvsw
;
433 if (lvsw
== DP_TRAIN_VOLTAGE_SWING_1200
)
434 dp
->conf
[i
] |= DP_TRAIN_MAX_SWING_REACHED
;
435 if (lpre
== DP_TRAIN_PRE_EMPHASIS_9_5
)
436 dp
->conf
[i
] |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED
;
438 NV_DEBUG_KMS(dev
, "config lane %d %02x\n", i
, dp
->conf
[i
]);
440 if (dp
->table
[0] < 0x30) {
441 u8
*last
= conf
+ (dp
->entry
[4] * dp
->table
[5]);
442 while (lvsw
!= conf
[0] || lpre
!= conf
[1]) {
443 conf
+= dp
->table
[5];
450 /* no lookup table anymore, set entries for each
451 * combination of voltage swing and pre-emphasis
452 * level allowed by the DP spec.
455 case 0: lpre
+= 0; break;
456 case 1: lpre
+= 4; break;
457 case 2: lpre
+= 7; break;
458 case 3: lpre
+= 9; break;
461 conf
= conf
+ (lpre
* dp
->table
[5]);
465 drv
|= conf
[0] << shifts
[i
];
466 pre
|= conf
[1] << shifts
[i
];
467 unk
= (unk
& ~0x0000ff00) | (conf
[2] << 8);
470 nv_mask(dev
, NV50_SOR_DP_UNK118(or, link
), mask
, drv
);
471 nv_mask(dev
, NV50_SOR_DP_UNK120(or, link
), mask
, pre
);
472 nv_mask(dev
, NV50_SOR_DP_UNK130(or, link
), 0x0000ff0f, unk
);
474 return auxch_tx(dev
, dp
->auxch
, 8, DP_TRAINING_LANE0_SET
, dp
->conf
, 4);
478 dp_link_train_update(struct drm_device
*dev
, struct dp_state
*dp
, u32 delay
)
484 ret
= auxch_tx(dev
, dp
->auxch
, 9, DP_LANE0_1_STATUS
, dp
->stat
, 6);
488 NV_DEBUG_KMS(dev
, "status %02x %02x %02x %02x %02x %02x\n",
489 dp
->stat
[0], dp
->stat
[1], dp
->stat
[2], dp
->stat
[3],
490 dp
->stat
[4], dp
->stat
[5]);
495 dp_link_train_cr(struct drm_device
*dev
, struct dp_state
*dp
)
497 bool cr_done
= false, abort
= false;
498 int voltage
= dp
->conf
[0] & DP_TRAIN_VOLTAGE_SWING_MASK
;
501 dp_set_training_pattern(dev
, dp
, DP_TRAINING_PATTERN_1
);
504 if (dp_link_train_commit(dev
, dp
) ||
505 dp_link_train_update(dev
, dp
, 100))
509 for (i
= 0; i
< dp
->link_nr
; i
++) {
510 u8 lane
= (dp
->stat
[i
>> 1] >> ((i
& 1) * 4)) & 0xf;
511 if (!(lane
& DP_LANE_CR_DONE
)) {
513 if (dp
->conf
[i
] & DP_TRAIN_MAX_SWING_REACHED
)
519 if ((dp
->conf
[0] & DP_TRAIN_VOLTAGE_SWING_MASK
) != voltage
) {
520 voltage
= dp
->conf
[0] & DP_TRAIN_VOLTAGE_SWING_MASK
;
523 } while (!cr_done
&& !abort
&& ++tries
< 5);
525 return cr_done
? 0 : -1;
529 dp_link_train_eq(struct drm_device
*dev
, struct dp_state
*dp
)
531 bool eq_done
, cr_done
= true;
534 dp_set_training_pattern(dev
, dp
, DP_TRAINING_PATTERN_2
);
537 if (dp_link_train_update(dev
, dp
, 400))
540 eq_done
= !!(dp
->stat
[2] & DP_INTERLANE_ALIGN_DONE
);
541 for (i
= 0; i
< dp
->link_nr
&& eq_done
; i
++) {
542 u8 lane
= (dp
->stat
[i
>> 1] >> ((i
& 1) * 4)) & 0xf;
543 if (!(lane
& DP_LANE_CR_DONE
))
545 if (!(lane
& DP_LANE_CHANNEL_EQ_DONE
) ||
546 !(lane
& DP_LANE_SYMBOL_LOCKED
))
550 if (dp_link_train_commit(dev
, dp
))
552 } while (!eq_done
&& cr_done
&& ++tries
<= 5);
554 return eq_done
? 0 : -1;
558 nouveau_dp_link_train(struct drm_encoder
*encoder
, u32 datarate
)
560 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
561 struct nouveau_crtc
*nv_crtc
= nouveau_crtc(encoder
->crtc
);
562 struct nouveau_connector
*nv_connector
=
563 nouveau_encoder_connector_get(nv_encoder
);
564 struct drm_device
*dev
= encoder
->dev
;
565 struct nouveau_i2c_chan
*auxch
;
566 const u32 bw_list
[] = { 270000, 162000, 0 };
567 const u32
*link_bw
= bw_list
;
570 auxch
= nouveau_i2c_find(dev
, nv_encoder
->dcb
->i2c_index
);
574 dp
.table
= nouveau_dp_bios_data(dev
, nv_encoder
->dcb
, &dp
.entry
);
578 dp
.dcb
= nv_encoder
->dcb
;
579 dp
.crtc
= nv_crtc
->index
;
580 dp
.auxch
= auxch
->drive
;
581 dp
.or = nv_encoder
->or;
582 dp
.link
= !(nv_encoder
->dcb
->sorconf
.link
& 1);
583 dp
.dpcd
= nv_encoder
->dp
.dpcd
;
585 /* some sinks toggle hotplug in response to some of the actions
586 * we take during link training (DP_SET_POWER is one), we need
587 * to ignore them for the moment to avoid races.
589 nouveau_gpio_irq(dev
, 0, nv_connector
->hpd
, 0xff, false);
591 /* enable down-spreading, if possible */
592 if (dp
.table
[1] >= 16) {
593 u16 script
= ROM16(dp
.entry
[14]);
594 if (nv_encoder
->dp
.dpcd
[3] & 1)
595 script
= ROM16(dp
.entry
[12]);
597 nouveau_bios_run_init_table(dev
, script
, dp
.dcb
, dp
.crtc
);
600 /* execute pre-train script from vbios */
601 nouveau_bios_run_init_table(dev
, ROM16(dp
.entry
[6]), dp
.dcb
, dp
.crtc
);
603 /* start off at highest link rate supported by encoder and display */
604 while (*link_bw
> nv_encoder
->dp
.link_bw
)
608 /* find minimum required lane count at this link rate */
609 dp
.link_nr
= nv_encoder
->dp
.link_nr
;
610 while ((dp
.link_nr
>> 1) * link_bw
[0] > datarate
)
613 /* drop link rate to minimum with this lane count */
614 while ((link_bw
[1] * dp
.link_nr
) > datarate
)
616 dp
.link_bw
= link_bw
[0];
618 /* program selected link configuration */
619 dp_set_link_config(dev
, &dp
);
621 /* attempt to train the link at this configuration */
622 memset(dp
.stat
, 0x00, sizeof(dp
.stat
));
623 if (!dp_link_train_cr(dev
, &dp
) &&
624 !dp_link_train_eq(dev
, &dp
))
627 /* retry at lower rate */
631 /* finish link training */
632 dp_set_training_pattern(dev
, &dp
, DP_TRAINING_PATTERN_DISABLE
);
634 /* execute post-train script from vbios */
635 nouveau_bios_run_init_table(dev
, ROM16(dp
.entry
[8]), dp
.dcb
, dp
.crtc
);
637 /* re-enable hotplug detect */
638 nouveau_gpio_irq(dev
, 0, nv_connector
->hpd
, 0xff, true);
643 nouveau_dp_detect(struct drm_encoder
*encoder
)
645 struct nouveau_encoder
*nv_encoder
= nouveau_encoder(encoder
);
646 struct drm_device
*dev
= encoder
->dev
;
647 struct nouveau_i2c_chan
*auxch
;
648 u8
*dpcd
= nv_encoder
->dp
.dpcd
;
651 auxch
= nouveau_i2c_find(dev
, nv_encoder
->dcb
->i2c_index
);
655 ret
= auxch_tx(dev
, auxch
->drive
, 9, DP_DPCD_REV
, dpcd
, 8);
659 nv_encoder
->dp
.link_bw
= 27000 * dpcd
[1];
660 nv_encoder
->dp
.link_nr
= dpcd
[2] & DP_MAX_LANE_COUNT_MASK
;
662 NV_DEBUG_KMS(dev
, "display: %dx%d dpcd 0x%02x\n",
663 nv_encoder
->dp
.link_nr
, nv_encoder
->dp
.link_bw
, dpcd
[0]);
664 NV_DEBUG_KMS(dev
, "encoder: %dx%d\n",
665 nv_encoder
->dcb
->dpconf
.link_nr
,
666 nv_encoder
->dcb
->dpconf
.link_bw
);
668 if (nv_encoder
->dcb
->dpconf
.link_nr
< nv_encoder
->dp
.link_nr
)
669 nv_encoder
->dp
.link_nr
= nv_encoder
->dcb
->dpconf
.link_nr
;
670 if (nv_encoder
->dcb
->dpconf
.link_bw
< nv_encoder
->dp
.link_bw
)
671 nv_encoder
->dp
.link_bw
= nv_encoder
->dcb
->dpconf
.link_bw
;
673 NV_DEBUG_KMS(dev
, "maximum: %dx%d\n",
674 nv_encoder
->dp
.link_nr
, nv_encoder
->dp
.link_bw
);
680 nouveau_dp_auxch(struct nouveau_i2c_chan
*auxch
, int cmd
, int addr
,
681 uint8_t *data
, int data_nr
)
683 return auxch_tx(auxch
->dev
, auxch
->drive
, cmd
, addr
, data
, data_nr
);
687 nouveau_dp_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
689 struct nouveau_i2c_chan
*auxch
= (struct nouveau_i2c_chan
*)adap
;
690 struct i2c_msg
*msg
= msgs
;
694 u8 remaining
= msg
->len
;
698 u8 cnt
= (remaining
> 16) ? 16 : remaining
;
701 if (msg
->flags
& I2C_M_RD
)
706 if (mcnt
|| remaining
> 16)
709 ret
= nouveau_dp_auxch(auxch
, cmd
, msg
->addr
, ptr
, cnt
);
724 nouveau_dp_i2c_func(struct i2c_adapter
*adap
)
726 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
729 const struct i2c_algorithm nouveau_dp_i2c_algo
= {
730 .master_xfer
= nouveau_dp_i2c_xfer
,
731 .functionality
= nouveau_dp_i2c_func