xref: /openbmc/linux/drivers/gpu/drm/nouveau/nvkm/engine/disp/dp.c (revision 692f5510159c79bfa312a4e27a15e266232bfb4c)
1af85389cSBen Skeggs /*
2af85389cSBen Skeggs  * Copyright 2014 Red Hat Inc.
3af85389cSBen Skeggs  *
4af85389cSBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
5af85389cSBen Skeggs  * copy of this software and associated documentation files (the "Software"),
6af85389cSBen Skeggs  * to deal in the Software without restriction, including without limitation
7af85389cSBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8af85389cSBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
9af85389cSBen Skeggs  * Software is furnished to do so, subject to the following conditions:
10af85389cSBen Skeggs  *
11af85389cSBen Skeggs  * The above copyright notice and this permission notice shall be included in
12af85389cSBen Skeggs  * all copies or substantial portions of the Software.
13af85389cSBen Skeggs  *
14af85389cSBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15af85389cSBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16af85389cSBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17af85389cSBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18af85389cSBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19af85389cSBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20af85389cSBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
21af85389cSBen Skeggs  *
22af85389cSBen Skeggs  * Authors: Ben Skeggs
23af85389cSBen Skeggs  */
24af85389cSBen Skeggs #include "dp.h"
25af85389cSBen Skeggs #include "conn.h"
269648da5aSBen Skeggs #include "head.h"
2775eefe95SBen Skeggs #include "ior.h"
28af85389cSBen Skeggs 
29*e4060dadSLyude Paul #include <drm/display/drm_dp.h>
30*e4060dadSLyude Paul 
31af85389cSBen Skeggs #include <subdev/bios.h>
32af85389cSBen Skeggs #include <subdev/bios/init.h>
33f6d52b21SBen Skeggs #include <subdev/gpio.h>
34af85389cSBen Skeggs #include <subdev/i2c.h>
35af85389cSBen Skeggs 
36af85389cSBen Skeggs #include <nvif/event.h>
37af85389cSBen Skeggs 
388ef23b6fSBen Skeggs /* IED scripts are no longer used by UEFI/RM from Ampere, but have been updated for
398ef23b6fSBen Skeggs  * the x86 option ROM.  However, the relevant VBIOS table versions weren't modified,
408ef23b6fSBen Skeggs  * so we're unable to detect this in a nice way.
418ef23b6fSBen Skeggs  */
428ef23b6fSBen Skeggs #define AMPERE_IED_HACK(disp) ((disp)->engine.subdev.device->card_type >= GA100)
438ef23b6fSBen Skeggs 
44af85389cSBen Skeggs struct lt_state {
457786fb36SBen Skeggs 	struct nvkm_outp *outp;
46f21e5fa1SBen Skeggs 
47f21e5fa1SBen Skeggs 	int repeaters;
48f21e5fa1SBen Skeggs 	int repeater;
49f21e5fa1SBen Skeggs 
50af85389cSBen Skeggs 	u8  stat[6];
51af85389cSBen Skeggs 	u8  conf[4];
52af85389cSBen Skeggs 	bool pc2;
53af85389cSBen Skeggs 	u8  pc2stat;
54af85389cSBen Skeggs 	u8  pc2conf[2];
55af85389cSBen Skeggs };
56af85389cSBen Skeggs 
57af85389cSBen Skeggs static int
nvkm_dp_train_sense(struct lt_state * lt,bool pc,u32 delay)58af85389cSBen Skeggs nvkm_dp_train_sense(struct lt_state *lt, bool pc, u32 delay)
59af85389cSBen Skeggs {
607786fb36SBen Skeggs 	struct nvkm_outp *outp = lt->outp;
61f21e5fa1SBen Skeggs 	u32 addr;
62af85389cSBen Skeggs 	int ret;
63af85389cSBen Skeggs 
649543e3c0SBen Skeggs 	usleep_range(delay, delay * 2);
65af85389cSBen Skeggs 
66f21e5fa1SBen Skeggs 	if (lt->repeater)
67f21e5fa1SBen Skeggs 		addr = DPCD_LTTPR_LANE0_1_STATUS(lt->repeater);
68f21e5fa1SBen Skeggs 	else
69f21e5fa1SBen Skeggs 		addr = DPCD_LS02;
70f21e5fa1SBen Skeggs 
717786fb36SBen Skeggs 	ret = nvkm_rdaux(outp->dp.aux, addr, &lt->stat[0], 3);
72f21e5fa1SBen Skeggs 	if (ret)
73f21e5fa1SBen Skeggs 		return ret;
74f21e5fa1SBen Skeggs 
75f21e5fa1SBen Skeggs 	if (lt->repeater)
76f21e5fa1SBen Skeggs 		addr = DPCD_LTTPR_LANE0_1_ADJUST(lt->repeater);
77f21e5fa1SBen Skeggs 	else
78f21e5fa1SBen Skeggs 		addr = DPCD_LS06;
79f21e5fa1SBen Skeggs 
807786fb36SBen Skeggs 	ret = nvkm_rdaux(outp->dp.aux, addr, &lt->stat[4], 2);
81af85389cSBen Skeggs 	if (ret)
82af85389cSBen Skeggs 		return ret;
83af85389cSBen Skeggs 
84af85389cSBen Skeggs 	if (pc) {
857786fb36SBen Skeggs 		ret = nvkm_rdaux(outp->dp.aux, DPCD_LS0C, &lt->pc2stat, 1);
86af85389cSBen Skeggs 		if (ret)
87af85389cSBen Skeggs 			lt->pc2stat = 0x00;
887786fb36SBen Skeggs 
897786fb36SBen Skeggs 		OUTP_TRACE(outp, "status %6ph pc2 %02x", lt->stat, lt->pc2stat);
90af85389cSBen Skeggs 	} else {
917786fb36SBen Skeggs 		OUTP_TRACE(outp, "status %6ph", lt->stat);
92af85389cSBen Skeggs 	}
93af85389cSBen Skeggs 
94af85389cSBen Skeggs 	return 0;
95af85389cSBen Skeggs }
96af85389cSBen Skeggs 
97af85389cSBen Skeggs static int
nvkm_dp_train_drive(struct lt_state * lt,bool pc)98af85389cSBen Skeggs nvkm_dp_train_drive(struct lt_state *lt, bool pc)
99af85389cSBen Skeggs {
1007786fb36SBen Skeggs 	struct nvkm_outp *outp = lt->outp;
1017786fb36SBen Skeggs 	struct nvkm_ior *ior = outp->ior;
1027d1fede0SBen Skeggs 	struct nvkm_bios *bios = ior->disp->engine.subdev.device->bios;
1037d1fede0SBen Skeggs 	struct nvbios_dpout info;
1047d1fede0SBen Skeggs 	struct nvbios_dpcfg ocfg;
1057d1fede0SBen Skeggs 	u8  ver, hdr, cnt, len;
106f21e5fa1SBen Skeggs 	u32 addr;
1077d1fede0SBen Skeggs 	u32 data;
108af85389cSBen Skeggs 	int ret, i;
109af85389cSBen Skeggs 
1107d1fede0SBen Skeggs 	for (i = 0; i < ior->dp.nr; i++) {
111af85389cSBen Skeggs 		u8 lane = (lt->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
112af85389cSBen Skeggs 		u8 lpc2 = (lt->pc2stat >> (i * 2)) & 0x3;
113af85389cSBen Skeggs 		u8 lpre = (lane & 0x0c) >> 2;
114af85389cSBen Skeggs 		u8 lvsw = (lane & 0x03) >> 0;
115af85389cSBen Skeggs 		u8 hivs = 3 - lpre;
116af85389cSBen Skeggs 		u8 hipe = 3;
117af85389cSBen Skeggs 		u8 hipc = 3;
118af85389cSBen Skeggs 
119af85389cSBen Skeggs 		if (lpc2 >= hipc)
120af85389cSBen Skeggs 			lpc2 = hipc | DPCD_LC0F_LANE0_MAX_POST_CURSOR2_REACHED;
121af85389cSBen Skeggs 		if (lpre >= hipe) {
122af85389cSBen Skeggs 			lpre = hipe | DPCD_LC03_MAX_SWING_REACHED; /* yes. */
123af85389cSBen Skeggs 			lvsw = hivs = 3 - (lpre & 3);
124af85389cSBen Skeggs 		} else
125af85389cSBen Skeggs 		if (lvsw >= hivs) {
126af85389cSBen Skeggs 			lvsw = hivs | DPCD_LC03_MAX_SWING_REACHED;
127af85389cSBen Skeggs 		}
128af85389cSBen Skeggs 
129af85389cSBen Skeggs 		lt->conf[i] = (lpre << 3) | lvsw;
130af85389cSBen Skeggs 		lt->pc2conf[i >> 1] |= lpc2 << ((i & 1) * 4);
131af85389cSBen Skeggs 
1327786fb36SBen Skeggs 		OUTP_TRACE(outp, "config lane %d %02x %02x", i, lt->conf[i], lpc2);
1337d1fede0SBen Skeggs 
134f21e5fa1SBen Skeggs 		if (lt->repeater != lt->repeaters)
135f21e5fa1SBen Skeggs 			continue;
136f21e5fa1SBen Skeggs 
1377786fb36SBen Skeggs 		data = nvbios_dpout_match(bios, outp->info.hasht, outp->info.hashm,
1387d1fede0SBen Skeggs 					  &ver, &hdr, &cnt, &len, &info);
1397d1fede0SBen Skeggs 		if (!data)
1407d1fede0SBen Skeggs 			continue;
1417d1fede0SBen Skeggs 
1427786fb36SBen Skeggs 		data = nvbios_dpcfg_match(bios, data, lpc2 & 3, lvsw & 3, lpre & 3,
1437786fb36SBen Skeggs 					  &ver, &hdr, &cnt, &len, &ocfg);
1447d1fede0SBen Skeggs 		if (!data)
1457d1fede0SBen Skeggs 			continue;
1467d1fede0SBen Skeggs 
1479a4514fbSBen Skeggs 		ior->func->dp->drive(ior, i, ocfg.pc, ocfg.dc, ocfg.pe, ocfg.tx_pu);
148af85389cSBen Skeggs 	}
149af85389cSBen Skeggs 
150f21e5fa1SBen Skeggs 	if (lt->repeater)
151f21e5fa1SBen Skeggs 		addr = DPCD_LTTPR_LANE0_SET(lt->repeater);
152f21e5fa1SBen Skeggs 	else
153f21e5fa1SBen Skeggs 		addr = DPCD_LC03(0);
154f21e5fa1SBen Skeggs 
1557786fb36SBen Skeggs 	ret = nvkm_wraux(outp->dp.aux, addr, lt->conf, 4);
156af85389cSBen Skeggs 	if (ret)
157af85389cSBen Skeggs 		return ret;
158af85389cSBen Skeggs 
159af85389cSBen Skeggs 	if (pc) {
1607786fb36SBen Skeggs 		ret = nvkm_wraux(outp->dp.aux, DPCD_LC0F, lt->pc2conf, 2);
161af85389cSBen Skeggs 		if (ret)
162af85389cSBen Skeggs 			return ret;
163af85389cSBen Skeggs 	}
164af85389cSBen Skeggs 
165af85389cSBen Skeggs 	return 0;
166af85389cSBen Skeggs }
167af85389cSBen Skeggs 
168af85389cSBen Skeggs static void
nvkm_dp_train_pattern(struct lt_state * lt,u8 pattern)169af85389cSBen Skeggs nvkm_dp_train_pattern(struct lt_state *lt, u8 pattern)
170af85389cSBen Skeggs {
1717786fb36SBen Skeggs 	struct nvkm_outp *outp = lt->outp;
172f21e5fa1SBen Skeggs 	u32 addr;
173af85389cSBen Skeggs 	u8 sink_tp;
174af85389cSBen Skeggs 
1757786fb36SBen Skeggs 	OUTP_TRACE(outp, "training pattern %d", pattern);
1769a4514fbSBen Skeggs 	outp->ior->func->dp->pattern(outp->ior, pattern);
177af85389cSBen Skeggs 
178f21e5fa1SBen Skeggs 	if (lt->repeater)
179f21e5fa1SBen Skeggs 		addr = DPCD_LTTPR_PATTERN_SET(lt->repeater);
180f21e5fa1SBen Skeggs 	else
181f21e5fa1SBen Skeggs 		addr = DPCD_LC02;
182f21e5fa1SBen Skeggs 
1837786fb36SBen Skeggs 	nvkm_rdaux(outp->dp.aux, addr, &sink_tp, 1);
184af85389cSBen Skeggs 	sink_tp &= ~DPCD_LC02_TRAINING_PATTERN_SET;
1853edcd504SBen Skeggs 	sink_tp |= (pattern != 4) ? pattern : 7;
186be5b6985SBen Skeggs 
187be5b6985SBen Skeggs 	if (pattern != 0)
188be5b6985SBen Skeggs 		sink_tp |=  DPCD_LC02_SCRAMBLING_DISABLE;
189be5b6985SBen Skeggs 	else
190be5b6985SBen Skeggs 		sink_tp &= ~DPCD_LC02_SCRAMBLING_DISABLE;
1917786fb36SBen Skeggs 	nvkm_wraux(outp->dp.aux, addr, &sink_tp, 1);
192af85389cSBen Skeggs }
193af85389cSBen Skeggs 
194af85389cSBen Skeggs static int
nvkm_dp_train_eq(struct lt_state * lt)195af85389cSBen Skeggs nvkm_dp_train_eq(struct lt_state *lt)
196af85389cSBen Skeggs {
1977786fb36SBen Skeggs 	struct nvkm_i2c_aux *aux = lt->outp->dp.aux;
198af85389cSBen Skeggs 	bool eq_done = false, cr_done = true;
1999543e3c0SBen Skeggs 	int tries = 0, usec = 0, i;
200f21e5fa1SBen Skeggs 	u8 data;
201af85389cSBen Skeggs 
202f21e5fa1SBen Skeggs 	if (lt->repeater) {
203f21e5fa1SBen Skeggs 		if (!nvkm_rdaux(aux, DPCD_LTTPR_AUX_RD_INTERVAL(lt->repeater), &data, sizeof(data)))
204f21e5fa1SBen Skeggs 			usec = (data & DPCD_RC0E_AUX_RD_INTERVAL) * 4000;
205f21e5fa1SBen Skeggs 
206f21e5fa1SBen Skeggs 		nvkm_dp_train_pattern(lt, 4);
207f21e5fa1SBen Skeggs 	} else {
2087786fb36SBen Skeggs 		if (lt->outp->dp.dpcd[DPCD_RC00_DPCD_REV] >= 0x14 &&
2097786fb36SBen Skeggs 		    lt->outp->dp.dpcd[DPCD_RC03] & DPCD_RC03_TPS4_SUPPORTED)
2103edcd504SBen Skeggs 			nvkm_dp_train_pattern(lt, 4);
2113edcd504SBen Skeggs 		else
2127786fb36SBen Skeggs 		if (lt->outp->dp.dpcd[DPCD_RC00_DPCD_REV] >= 0x12 &&
2137786fb36SBen Skeggs 		    lt->outp->dp.dpcd[DPCD_RC02] & DPCD_RC02_TPS3_SUPPORTED)
214af85389cSBen Skeggs 			nvkm_dp_train_pattern(lt, 3);
215af85389cSBen Skeggs 		else
216af85389cSBen Skeggs 			nvkm_dp_train_pattern(lt, 2);
2179543e3c0SBen Skeggs 
2187786fb36SBen Skeggs 		usec = (lt->outp->dp.dpcd[DPCD_RC0E] & DPCD_RC0E_AUX_RD_INTERVAL) * 4000;
2193edcd504SBen Skeggs 	}
220af85389cSBen Skeggs 
221af85389cSBen Skeggs 	do {
222af85389cSBen Skeggs 		if ((tries &&
223af85389cSBen Skeggs 		    nvkm_dp_train_drive(lt, lt->pc2)) ||
2249543e3c0SBen Skeggs 		    nvkm_dp_train_sense(lt, lt->pc2, usec ? usec : 400))
225af85389cSBen Skeggs 			break;
226af85389cSBen Skeggs 
227af85389cSBen Skeggs 		eq_done = !!(lt->stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE);
2287786fb36SBen Skeggs 		for (i = 0; i < lt->outp->ior->dp.nr && eq_done; i++) {
229af85389cSBen Skeggs 			u8 lane = (lt->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
230af85389cSBen Skeggs 			if (!(lane & DPCD_LS02_LANE0_CR_DONE))
231af85389cSBen Skeggs 				cr_done = false;
232af85389cSBen Skeggs 			if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
233af85389cSBen Skeggs 			    !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
234af85389cSBen Skeggs 				eq_done = false;
235af85389cSBen Skeggs 		}
236af85389cSBen Skeggs 	} while (!eq_done && cr_done && ++tries <= 5);
237af85389cSBen Skeggs 
238af85389cSBen Skeggs 	return eq_done ? 0 : -1;
239af85389cSBen Skeggs }
240af85389cSBen Skeggs 
241af85389cSBen Skeggs static int
nvkm_dp_train_cr(struct lt_state * lt)242af85389cSBen Skeggs nvkm_dp_train_cr(struct lt_state *lt)
243af85389cSBen Skeggs {
244af85389cSBen Skeggs 	bool cr_done = false, abort = false;
245af85389cSBen Skeggs 	int voltage = lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
2469543e3c0SBen Skeggs 	int tries = 0, usec = 0, i;
247af85389cSBen Skeggs 
248af85389cSBen Skeggs 	nvkm_dp_train_pattern(lt, 1);
249af85389cSBen Skeggs 
2507786fb36SBen Skeggs 	if (lt->outp->dp.dpcd[DPCD_RC00_DPCD_REV] < 0x14 && !lt->repeater)
2517786fb36SBen Skeggs 		usec = (lt->outp->dp.dpcd[DPCD_RC0E] & DPCD_RC0E_AUX_RD_INTERVAL) * 4000;
2529543e3c0SBen Skeggs 
253af85389cSBen Skeggs 	do {
254af85389cSBen Skeggs 		if (nvkm_dp_train_drive(lt, false) ||
2559543e3c0SBen Skeggs 		    nvkm_dp_train_sense(lt, false, usec ? usec : 100))
256af85389cSBen Skeggs 			break;
257af85389cSBen Skeggs 
258af85389cSBen Skeggs 		cr_done = true;
2597786fb36SBen Skeggs 		for (i = 0; i < lt->outp->ior->dp.nr; i++) {
260af85389cSBen Skeggs 			u8 lane = (lt->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
261af85389cSBen Skeggs 			if (!(lane & DPCD_LS02_LANE0_CR_DONE)) {
262af85389cSBen Skeggs 				cr_done = false;
263af85389cSBen Skeggs 				if (lt->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
264af85389cSBen Skeggs 					abort = true;
265af85389cSBen Skeggs 				break;
266af85389cSBen Skeggs 			}
267af85389cSBen Skeggs 		}
268af85389cSBen Skeggs 
269af85389cSBen Skeggs 		if ((lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
270af85389cSBen Skeggs 			voltage = lt->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
271af85389cSBen Skeggs 			tries = 0;
272af85389cSBen Skeggs 		}
273af85389cSBen Skeggs 	} while (!cr_done && !abort && ++tries < 5);
274af85389cSBen Skeggs 
275af85389cSBen Skeggs 	return cr_done ? 0 : -1;
276af85389cSBen Skeggs }
277af85389cSBen Skeggs 
278af85389cSBen Skeggs static int
nvkm_dp_train_link(struct nvkm_outp * outp,int rate)2798bb30c88SBen Skeggs nvkm_dp_train_link(struct nvkm_outp *outp, int rate)
280af85389cSBen Skeggs {
2817786fb36SBen Skeggs 	struct nvkm_ior *ior = outp->ior;
2824423c743SBen Skeggs 	struct lt_state lt = {
2837786fb36SBen Skeggs 		.outp = outp,
2848bb30c88SBen Skeggs 		.pc2 = outp->dp.dpcd[DPCD_RC02] & DPCD_RC02_TPS3_SUPPORTED,
2854423c743SBen Skeggs 	};
286f21e5fa1SBen Skeggs 	u8 sink[2], data;
287af85389cSBen Skeggs 	int ret;
288af85389cSBen Skeggs 
28981344372SBen Skeggs 	OUTP_DBG(outp, "training %dx%02x", ior->dp.nr, ior->dp.bw);
290af85389cSBen Skeggs 
291f21e5fa1SBen Skeggs 	/* Select LTTPR non-transparent mode if we have a valid configuration,
292f21e5fa1SBen Skeggs 	 * use transparent mode otherwise.
293f21e5fa1SBen Skeggs 	 */
2947786fb36SBen Skeggs 	if (outp->dp.lttpr[0] >= 0x14) {
295f21e5fa1SBen Skeggs 		data = DPCD_LTTPR_MODE_TRANSPARENT;
2967786fb36SBen Skeggs 		nvkm_wraux(outp->dp.aux, DPCD_LTTPR_MODE, &data, sizeof(data));
297f21e5fa1SBen Skeggs 
2987786fb36SBen Skeggs 		if (outp->dp.lttprs) {
299f21e5fa1SBen Skeggs 			data = DPCD_LTTPR_MODE_NON_TRANSPARENT;
3007786fb36SBen Skeggs 			nvkm_wraux(outp->dp.aux, DPCD_LTTPR_MODE, &data, sizeof(data));
3017786fb36SBen Skeggs 			lt.repeaters = outp->dp.lttprs;
302f21e5fa1SBen Skeggs 		}
303f21e5fa1SBen Skeggs 	}
304f21e5fa1SBen Skeggs 
305af85389cSBen Skeggs 	/* Set desired link configuration on the sink. */
3067786fb36SBen Skeggs 	sink[0] = (outp->dp.rate[rate].dpcd < 0) ? ior->dp.bw : 0;
30775eefe95SBen Skeggs 	sink[1] = ior->dp.nr;
30875eefe95SBen Skeggs 	if (ior->dp.ef)
309af85389cSBen Skeggs 		sink[1] |= DPCD_LC01_ENHANCED_FRAME_EN;
310af85389cSBen Skeggs 
3117786fb36SBen Skeggs 	ret = nvkm_wraux(outp->dp.aux, DPCD_LC00_LINK_BW_SET, sink, 2);
3124423c743SBen Skeggs 	if (ret)
3134423c743SBen Skeggs 		return ret;
3144423c743SBen Skeggs 
3157786fb36SBen Skeggs 	if (outp->dp.rate[rate].dpcd >= 0) {
3167786fb36SBen Skeggs 		ret = nvkm_rdaux(outp->dp.aux, DPCD_LC15_LINK_RATE_SET, &sink[0], sizeof(sink[0]));
31770704fbfSBen Skeggs 		if (ret)
31870704fbfSBen Skeggs 			return ret;
31970704fbfSBen Skeggs 
32070704fbfSBen Skeggs 		sink[0] &= ~DPCD_LC15_LINK_RATE_SET_MASK;
3217786fb36SBen Skeggs 		sink[0] |= outp->dp.rate[rate].dpcd;
32270704fbfSBen Skeggs 
3237786fb36SBen Skeggs 		ret = nvkm_wraux(outp->dp.aux, DPCD_LC15_LINK_RATE_SET, &sink[0], sizeof(sink[0]));
32470704fbfSBen Skeggs 		if (ret)
32570704fbfSBen Skeggs 			return ret;
32670704fbfSBen Skeggs 	}
32770704fbfSBen Skeggs 
3284423c743SBen Skeggs 	/* Attempt to train the link in this configuration. */
329f21e5fa1SBen Skeggs 	for (lt.repeater = lt.repeaters; lt.repeater >= 0; lt.repeater--) {
330f21e5fa1SBen Skeggs 		if (lt.repeater)
3317786fb36SBen Skeggs 			OUTP_DBG(outp, "training LTTPR%d", lt.repeater);
332f21e5fa1SBen Skeggs 		else
3337786fb36SBen Skeggs 			OUTP_DBG(outp, "training sink");
334f21e5fa1SBen Skeggs 
3354423c743SBen Skeggs 		memset(lt.stat, 0x00, sizeof(lt.stat));
3364423c743SBen Skeggs 		ret = nvkm_dp_train_cr(&lt);
3374423c743SBen Skeggs 		if (ret == 0)
3384423c743SBen Skeggs 			ret = nvkm_dp_train_eq(&lt);
3394423c743SBen Skeggs 		nvkm_dp_train_pattern(&lt, 0);
340f21e5fa1SBen Skeggs 	}
341f21e5fa1SBen Skeggs 
3424423c743SBen Skeggs 	return ret;
343af85389cSBen Skeggs }
344af85389cSBen Skeggs 
3458bb30c88SBen Skeggs static int
nvkm_dp_train_links(struct nvkm_outp * outp,int rate)3468bb30c88SBen Skeggs nvkm_dp_train_links(struct nvkm_outp *outp, int rate)
3478bb30c88SBen Skeggs {
3488bb30c88SBen Skeggs 	struct nvkm_ior *ior = outp->ior;
3498bb30c88SBen Skeggs 	struct nvkm_disp *disp = outp->disp;
3508bb30c88SBen Skeggs 	struct nvkm_subdev *subdev = &disp->engine.subdev;
3518bb30c88SBen Skeggs 	struct nvkm_bios *bios = subdev->device->bios;
3528bb30c88SBen Skeggs 	u32 lnkcmp;
3538bb30c88SBen Skeggs 	int ret;
3548bb30c88SBen Skeggs 
3558bb30c88SBen Skeggs 	OUTP_DBG(outp, "programming link for %dx%02x", ior->dp.nr, ior->dp.bw);
3568bb30c88SBen Skeggs 
3578bb30c88SBen Skeggs 	/* Intersect misc. capabilities of the OR and sink. */
3588bb30c88SBen Skeggs 	if (disp->engine.subdev.device->chipset < 0x110)
3598bb30c88SBen Skeggs 		outp->dp.dpcd[DPCD_RC03] &= ~DPCD_RC03_TPS4_SUPPORTED;
3608bb30c88SBen Skeggs 	if (disp->engine.subdev.device->chipset < 0xd0)
3618bb30c88SBen Skeggs 		outp->dp.dpcd[DPCD_RC02] &= ~DPCD_RC02_TPS3_SUPPORTED;
3628bb30c88SBen Skeggs 
3638bb30c88SBen Skeggs 	if (AMPERE_IED_HACK(disp) && (lnkcmp = outp->dp.info.script[0])) {
3648bb30c88SBen Skeggs 		/* Execute BeforeLinkTraining script from DP Info table. */
3658bb30c88SBen Skeggs 		while (ior->dp.bw < nvbios_rd08(bios, lnkcmp))
3668bb30c88SBen Skeggs 			lnkcmp += 3;
3678bb30c88SBen Skeggs 		lnkcmp = nvbios_rd16(bios, lnkcmp + 1);
3688bb30c88SBen Skeggs 
3698bb30c88SBen Skeggs 		nvbios_init(&outp->disp->engine.subdev, lnkcmp,
3708bb30c88SBen Skeggs 			init.outp = &outp->info;
3718bb30c88SBen Skeggs 			init.or   = ior->id;
3728bb30c88SBen Skeggs 			init.link = ior->asy.link;
3738bb30c88SBen Skeggs 		);
3748bb30c88SBen Skeggs 	}
3758bb30c88SBen Skeggs 
3768bb30c88SBen Skeggs 	/* Set desired link configuration on the source. */
3778bb30c88SBen Skeggs 	if ((lnkcmp = outp->dp.info.lnkcmp)) {
3788bb30c88SBen Skeggs 		if (outp->dp.version < 0x30) {
3798bb30c88SBen Skeggs 			while ((ior->dp.bw * 2700) < nvbios_rd16(bios, lnkcmp))
3808bb30c88SBen Skeggs 				lnkcmp += 4;
3818bb30c88SBen Skeggs 			lnkcmp = nvbios_rd16(bios, lnkcmp + 2);
3828bb30c88SBen Skeggs 		} else {
3838bb30c88SBen Skeggs 			while (ior->dp.bw < nvbios_rd08(bios, lnkcmp))
3848bb30c88SBen Skeggs 				lnkcmp += 3;
3858bb30c88SBen Skeggs 			lnkcmp = nvbios_rd16(bios, lnkcmp + 1);
3868bb30c88SBen Skeggs 		}
3878bb30c88SBen Skeggs 
3888bb30c88SBen Skeggs 		nvbios_init(subdev, lnkcmp,
3898bb30c88SBen Skeggs 			init.outp = &outp->info;
3908bb30c88SBen Skeggs 			init.or   = ior->id;
3918bb30c88SBen Skeggs 			init.link = ior->asy.link;
3928bb30c88SBen Skeggs 		);
3938bb30c88SBen Skeggs 	}
3948bb30c88SBen Skeggs 
3958bb30c88SBen Skeggs 	ret = ior->func->dp->links(ior, outp->dp.aux);
3968bb30c88SBen Skeggs 	if (ret) {
3978bb30c88SBen Skeggs 		if (ret < 0) {
3988bb30c88SBen Skeggs 			OUTP_ERR(outp, "train failed with %d", ret);
3998bb30c88SBen Skeggs 			return ret;
4008bb30c88SBen Skeggs 		}
4018bb30c88SBen Skeggs 		return 0;
4028bb30c88SBen Skeggs 	}
4038bb30c88SBen Skeggs 
4048bb30c88SBen Skeggs 	ior->func->dp->power(ior, ior->dp.nr);
4058bb30c88SBen Skeggs 
4068bb30c88SBen Skeggs 	/* Attempt to train the link in this configuration. */
4078bb30c88SBen Skeggs 	return nvkm_dp_train_link(outp, rate);
4088bb30c88SBen Skeggs }
4098bb30c88SBen Skeggs 
410af85389cSBen Skeggs static void
nvkm_dp_train_fini(struct nvkm_outp * outp)4117786fb36SBen Skeggs nvkm_dp_train_fini(struct nvkm_outp *outp)
412af85389cSBen Skeggs {
413af85389cSBen Skeggs 	/* Execute AfterLinkTraining script from DP Info table. */
4147786fb36SBen Skeggs 	nvbios_init(&outp->disp->engine.subdev, outp->dp.info.script[1],
4157786fb36SBen Skeggs 		init.outp = &outp->info;
4167786fb36SBen Skeggs 		init.or   = outp->ior->id;
4177786fb36SBen Skeggs 		init.link = outp->ior->asy.link;
41832a232c5SBen Skeggs 	);
419af85389cSBen Skeggs }
420af85389cSBen Skeggs 
421af85389cSBen Skeggs static void
nvkm_dp_train_init(struct nvkm_outp * outp)4227786fb36SBen Skeggs nvkm_dp_train_init(struct nvkm_outp *outp)
423af85389cSBen Skeggs {
424af85389cSBen Skeggs 	/* Execute EnableSpread/DisableSpread script from DP Info table. */
4257786fb36SBen Skeggs 	if (outp->dp.dpcd[DPCD_RC03] & DPCD_RC03_MAX_DOWNSPREAD) {
4267786fb36SBen Skeggs 		nvbios_init(&outp->disp->engine.subdev, outp->dp.info.script[2],
4277786fb36SBen Skeggs 			init.outp = &outp->info;
4287786fb36SBen Skeggs 			init.or   = outp->ior->id;
4297786fb36SBen Skeggs 			init.link = outp->ior->asy.link;
43032a232c5SBen Skeggs 		);
43132a232c5SBen Skeggs 	} else {
4327786fb36SBen Skeggs 		nvbios_init(&outp->disp->engine.subdev, outp->dp.info.script[3],
4337786fb36SBen Skeggs 			init.outp = &outp->info;
4347786fb36SBen Skeggs 			init.or   = outp->ior->id;
4357786fb36SBen Skeggs 			init.link = outp->ior->asy.link;
43632a232c5SBen Skeggs 		);
43732a232c5SBen Skeggs 	}
438af85389cSBen Skeggs 
4397786fb36SBen Skeggs 	if (!AMPERE_IED_HACK(outp->disp)) {
440f3e70d29SBen Skeggs 		/* Execute BeforeLinkTraining script from DP Info table. */
4417786fb36SBen Skeggs 		nvbios_init(&outp->disp->engine.subdev, outp->dp.info.script[0],
4427786fb36SBen Skeggs 			init.outp = &outp->info;
4437786fb36SBen Skeggs 			init.or   = outp->ior->id;
4447786fb36SBen Skeggs 			init.link = outp->ior->asy.link;
44532a232c5SBen Skeggs 		);
446af85389cSBen Skeggs 	}
4478ef23b6fSBen Skeggs }
448af85389cSBen Skeggs 
449fafa8b5cSBen Skeggs static int
nvkm_dp_train(struct nvkm_outp * outp,u32 dataKBps)4507786fb36SBen Skeggs nvkm_dp_train(struct nvkm_outp *outp, u32 dataKBps)
451af85389cSBen Skeggs {
4527786fb36SBen Skeggs 	struct nvkm_ior *ior = outp->ior;
453b96a1d8cSBen Skeggs 	int ret = -EINVAL, nr, rate;
454af85389cSBen Skeggs 	u8  pwr;
455af85389cSBen Skeggs 
4568bb30c88SBen Skeggs 	/* Retraining link?  Skip source configuration, it can mess up the active modeset. */
4578bb30c88SBen Skeggs 	if (atomic_read(&outp->dp.lt.done)) {
4588bb30c88SBen Skeggs 		for (rate = 0; rate < outp->dp.rates; rate++) {
4598bb30c88SBen Skeggs 			if (outp->dp.rate[rate].rate == ior->dp.bw * 27000)
4608bb30c88SBen Skeggs 				return nvkm_dp_train_link(outp, ret);
4618bb30c88SBen Skeggs 		}
4628bb30c88SBen Skeggs 		WARN_ON(1);
4638bb30c88SBen Skeggs 		return -EINVAL;
4648bb30c88SBen Skeggs 	}
4658bb30c88SBen Skeggs 
466af85389cSBen Skeggs 	/* Ensure sink is not in a low-power state. */
4677786fb36SBen Skeggs 	if (!nvkm_rdaux(outp->dp.aux, DPCD_SC00, &pwr, 1)) {
468af85389cSBen Skeggs 		if ((pwr & DPCD_SC00_SET_POWER) != DPCD_SC00_SET_POWER_D0) {
469af85389cSBen Skeggs 			pwr &= ~DPCD_SC00_SET_POWER;
470af85389cSBen Skeggs 			pwr |=  DPCD_SC00_SET_POWER_D0;
4717786fb36SBen Skeggs 			nvkm_wraux(outp->dp.aux, DPCD_SC00, &pwr, 1);
472af85389cSBen Skeggs 		}
473af85389cSBen Skeggs 	}
474af85389cSBen Skeggs 
4757786fb36SBen Skeggs 	ior->dp.mst = outp->dp.lt.mst;
4767786fb36SBen Skeggs 	ior->dp.ef = outp->dp.dpcd[DPCD_RC02] & DPCD_RC02_ENHANCED_FRAME_CAP;
477b96a1d8cSBen Skeggs 	ior->dp.nr = 0;
478af85389cSBen Skeggs 
479b96a1d8cSBen Skeggs 	/* Link training. */
4807786fb36SBen Skeggs 	OUTP_DBG(outp, "training");
4817786fb36SBen Skeggs 	nvkm_dp_train_init(outp);
48281344372SBen Skeggs 
48381344372SBen Skeggs 	/* Validate and train at configuration requested (if any) on ACQUIRE. */
48481344372SBen Skeggs 	if (outp->dp.lt.nr) {
48581344372SBen Skeggs 		for (nr = outp->dp.links; ret < 0 && nr; nr >>= 1) {
48681344372SBen Skeggs 			for (rate = 0; nr == outp->dp.lt.nr && rate < outp->dp.rates; rate++) {
48781344372SBen Skeggs 				if (outp->dp.rate[rate].rate / 27000 == outp->dp.lt.bw) {
48881344372SBen Skeggs 					ior->dp.bw = outp->dp.rate[rate].rate / 27000;
48981344372SBen Skeggs 					ior->dp.nr = nr;
49081344372SBen Skeggs 					ret = nvkm_dp_train_links(outp, rate);
49181344372SBen Skeggs 				}
49281344372SBen Skeggs 			}
49381344372SBen Skeggs 		}
49481344372SBen Skeggs 	}
49581344372SBen Skeggs 
49681344372SBen Skeggs 	/* Otherwise, loop through all valid link configurations that support the data rate. */
4977786fb36SBen Skeggs 	for (nr = outp->dp.links; ret < 0 && nr; nr >>= 1) {
4987786fb36SBen Skeggs 		for (rate = 0; ret < 0 && rate < outp->dp.rates; rate++) {
4997786fb36SBen Skeggs 			if (outp->dp.rate[rate].rate * nr >= dataKBps || WARN_ON(!ior->dp.nr)) {
500af85389cSBen Skeggs 				/* Program selected link configuration. */
5017786fb36SBen Skeggs 				ior->dp.bw = outp->dp.rate[rate].rate / 27000;
502b96a1d8cSBen Skeggs 				ior->dp.nr = nr;
5037786fb36SBen Skeggs 				ret = nvkm_dp_train_links(outp, rate);
504af85389cSBen Skeggs 			}
505b96a1d8cSBen Skeggs 		}
506b96a1d8cSBen Skeggs 	}
50781344372SBen Skeggs 
50881344372SBen Skeggs 	/* Finish up. */
5097786fb36SBen Skeggs 	nvkm_dp_train_fini(outp);
510af85389cSBen Skeggs 	if (ret < 0)
5117786fb36SBen Skeggs 		OUTP_ERR(outp, "training failed");
512fafa8b5cSBen Skeggs 	else
5137786fb36SBen Skeggs 		OUTP_DBG(outp, "training done");
5147786fb36SBen Skeggs 	atomic_set(&outp->dp.lt.done, 1);
515fafa8b5cSBen Skeggs 	return ret;
516af85389cSBen Skeggs }
517af85389cSBen Skeggs 
5186eaa1f3cSBen Skeggs void
nvkm_dp_disable(struct nvkm_outp * outp,struct nvkm_ior * ior)519e04cfdc9SBen Skeggs nvkm_dp_disable(struct nvkm_outp *outp, struct nvkm_ior *ior)
520d52e948cSBen Skeggs {
521d52e948cSBen Skeggs 	/* Execute DisableLT script from DP Info Table. */
5227786fb36SBen Skeggs 	nvbios_init(&ior->disp->engine.subdev, outp->dp.info.script[4],
5237786fb36SBen Skeggs 		init.outp = &outp->info;
524d52e948cSBen Skeggs 		init.or   = ior->id;
525d52e948cSBen Skeggs 		init.link = ior->arm.link;
526d52e948cSBen Skeggs 	);
527d52e948cSBen Skeggs }
528d52e948cSBen Skeggs 
529e04cfdc9SBen Skeggs static void
nvkm_dp_release(struct nvkm_outp * outp)530e04cfdc9SBen Skeggs nvkm_dp_release(struct nvkm_outp *outp)
531e04cfdc9SBen Skeggs {
532e04cfdc9SBen Skeggs 	/* Prevent link from being retrained if sink sends an IRQ. */
5337786fb36SBen Skeggs 	atomic_set(&outp->dp.lt.done, 0);
5347786fb36SBen Skeggs 	outp->ior->dp.nr = 0;
535e04cfdc9SBen Skeggs }
536e04cfdc9SBen Skeggs 
5378d7ef84dSBen Skeggs static int
nvkm_dp_acquire(struct nvkm_outp * outp)5388d7ef84dSBen Skeggs nvkm_dp_acquire(struct nvkm_outp *outp)
539af85389cSBen Skeggs {
5407786fb36SBen Skeggs 	struct nvkm_ior *ior = outp->ior;
5419648da5aSBen Skeggs 	struct nvkm_head *head;
542af85389cSBen Skeggs 	bool retrain = true;
5439648da5aSBen Skeggs 	u32 datakbps = 0;
544fafa8b5cSBen Skeggs 	u32 dataKBps;
5459648da5aSBen Skeggs 	u32 linkKBps;
546fafa8b5cSBen Skeggs 	u8  stat[3];
547af85389cSBen Skeggs 	int ret, i;
548af85389cSBen Skeggs 
5497786fb36SBen Skeggs 	mutex_lock(&outp->dp.mutex);
550af85389cSBen Skeggs 
551fafa8b5cSBen Skeggs 	/* Check that link configuration meets current requirements. */
55292fba5d3SBen Skeggs 	list_for_each_entry(head, &outp->disp->heads, head) {
5539648da5aSBen Skeggs 		if (ior->asy.head & (1 << head->id)) {
5549648da5aSBen Skeggs 			u32 khz = (head->asy.hz >> ior->asy.rgdiv) / 1000;
5559648da5aSBen Skeggs 			datakbps += khz * head->asy.or.depth;
5569648da5aSBen Skeggs 		}
5579648da5aSBen Skeggs 	}
5589648da5aSBen Skeggs 
559fafa8b5cSBen Skeggs 	linkKBps = ior->dp.bw * 27000 * ior->dp.nr;
560fafa8b5cSBen Skeggs 	dataKBps = DIV_ROUND_UP(datakbps, 8);
5617786fb36SBen Skeggs 	OUTP_DBG(outp, "data %d KB/s link %d KB/s mst %d->%d",
5627786fb36SBen Skeggs 		 dataKBps, linkKBps, ior->dp.mst, outp->dp.lt.mst);
5637786fb36SBen Skeggs 	if (linkKBps < dataKBps || ior->dp.mst != outp->dp.lt.mst) {
5647786fb36SBen Skeggs 		OUTP_DBG(outp, "link requirements changed");
565af85389cSBen Skeggs 		goto done;
566af85389cSBen Skeggs 	}
567af85389cSBen Skeggs 
568fafa8b5cSBen Skeggs 	/* Check that link is still trained. */
5697786fb36SBen Skeggs 	ret = nvkm_rdaux(outp->dp.aux, DPCD_LS02, stat, 3);
570af85389cSBen Skeggs 	if (ret) {
5717786fb36SBen Skeggs 		OUTP_DBG(outp, "failed to read link status, assuming no sink");
572af85389cSBen Skeggs 		goto done;
573af85389cSBen Skeggs 	}
574af85389cSBen Skeggs 
575af85389cSBen Skeggs 	if (stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE) {
576fafa8b5cSBen Skeggs 		for (i = 0; i < ior->dp.nr; i++) {
577af85389cSBen Skeggs 			u8 lane = (stat[i >> 1] >> ((i & 1) * 4)) & 0x0f;
578af85389cSBen Skeggs 			if (!(lane & DPCD_LS02_LANE0_CR_DONE) ||
579af85389cSBen Skeggs 			    !(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
580af85389cSBen Skeggs 			    !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED)) {
5817786fb36SBen Skeggs 				OUTP_DBG(outp, "lane %d not equalised", lane);
582af85389cSBen Skeggs 				goto done;
583af85389cSBen Skeggs 			}
584af85389cSBen Skeggs 		}
585af85389cSBen Skeggs 		retrain = false;
586af85389cSBen Skeggs 	} else {
5877786fb36SBen Skeggs 		OUTP_DBG(outp, "no inter-lane alignment");
588af85389cSBen Skeggs 	}
589af85389cSBen Skeggs 
590af85389cSBen Skeggs done:
5917786fb36SBen Skeggs 	if (retrain || !atomic_read(&outp->dp.lt.done))
5927786fb36SBen Skeggs 		ret = nvkm_dp_train(outp, dataKBps);
5937786fb36SBen Skeggs 	mutex_unlock(&outp->dp.mutex);
594af85389cSBen Skeggs 	return ret;
595af85389cSBen Skeggs }
596af85389cSBen Skeggs 
597f6d52b21SBen Skeggs static bool
nvkm_dp_enable_supported_link_rates(struct nvkm_outp * outp)5987786fb36SBen Skeggs nvkm_dp_enable_supported_link_rates(struct nvkm_outp *outp)
59970704fbfSBen Skeggs {
60070704fbfSBen Skeggs 	u8 sink_rates[DPCD_RC10_SUPPORTED_LINK_RATES__SIZE];
60170704fbfSBen Skeggs 	int i, j, k;
60270704fbfSBen Skeggs 
6037786fb36SBen Skeggs 	if (outp->conn->info.type != DCB_CONNECTOR_eDP ||
6047786fb36SBen Skeggs 	    outp->dp.dpcd[DPCD_RC00_DPCD_REV] < 0x13 ||
6057786fb36SBen Skeggs 	    nvkm_rdaux(outp->dp.aux, DPCD_RC10_SUPPORTED_LINK_RATES(0),
6067786fb36SBen Skeggs 		       sink_rates, sizeof(sink_rates)))
60770704fbfSBen Skeggs 		return false;
60870704fbfSBen Skeggs 
60970704fbfSBen Skeggs 	for (i = 0; i < ARRAY_SIZE(sink_rates); i += 2) {
61070704fbfSBen Skeggs 		const u32 rate = ((sink_rates[i + 1] << 8) | sink_rates[i]) * 200 / 10;
61170704fbfSBen Skeggs 
6127786fb36SBen Skeggs 		if (!rate || WARN_ON(outp->dp.rates == ARRAY_SIZE(outp->dp.rate)))
61370704fbfSBen Skeggs 			break;
61470704fbfSBen Skeggs 
6157786fb36SBen Skeggs 		if (rate > outp->info.dpconf.link_bw * 27000) {
6167786fb36SBen Skeggs 			OUTP_DBG(outp, "rate %d !outp", rate);
61770704fbfSBen Skeggs 			continue;
61870704fbfSBen Skeggs 		}
61970704fbfSBen Skeggs 
6207786fb36SBen Skeggs 		for (j = 0; j < outp->dp.rates; j++) {
6217786fb36SBen Skeggs 			if (rate > outp->dp.rate[j].rate) {
6227786fb36SBen Skeggs 				for (k = outp->dp.rates; k > j; k--)
6237786fb36SBen Skeggs 					outp->dp.rate[k] = outp->dp.rate[k - 1];
62470704fbfSBen Skeggs 				break;
62570704fbfSBen Skeggs 			}
62670704fbfSBen Skeggs 		}
62770704fbfSBen Skeggs 
6287786fb36SBen Skeggs 		outp->dp.rate[j].dpcd = i / 2;
6297786fb36SBen Skeggs 		outp->dp.rate[j].rate = rate;
6307786fb36SBen Skeggs 		outp->dp.rates++;
63170704fbfSBen Skeggs 	}
63270704fbfSBen Skeggs 
6337786fb36SBen Skeggs 	for (i = 0; i < outp->dp.rates; i++)
6347786fb36SBen Skeggs 		OUTP_DBG(outp, "link_rate[%d] = %d", outp->dp.rate[i].dpcd, outp->dp.rate[i].rate);
63570704fbfSBen Skeggs 
6367786fb36SBen Skeggs 	return outp->dp.rates != 0;
63770704fbfSBen Skeggs }
63870704fbfSBen Skeggs 
639*e4060dadSLyude Paul /* XXX: This is a big fat hack, and this is just drm_dp_read_dpcd_caps()
640*e4060dadSLyude Paul  * converted to work inside nvkm. This is a temporary holdover until we start
641*e4060dadSLyude Paul  * passing the drm_dp_aux device through NVKM
642*e4060dadSLyude Paul  */
643*e4060dadSLyude Paul static int
nvkm_dp_read_dpcd_caps(struct nvkm_outp * outp)644*e4060dadSLyude Paul nvkm_dp_read_dpcd_caps(struct nvkm_outp *outp)
645*e4060dadSLyude Paul {
646*e4060dadSLyude Paul 	struct nvkm_i2c_aux *aux = outp->dp.aux;
647*e4060dadSLyude Paul 	u8 dpcd_ext[DP_RECEIVER_CAP_SIZE];
648*e4060dadSLyude Paul 	int ret;
649*e4060dadSLyude Paul 
650*e4060dadSLyude Paul 	ret = nvkm_rdaux(aux, DPCD_RC00_DPCD_REV, outp->dp.dpcd, DP_RECEIVER_CAP_SIZE);
651*e4060dadSLyude Paul 	if (ret < 0)
652*e4060dadSLyude Paul 		return ret;
653*e4060dadSLyude Paul 
654*e4060dadSLyude Paul 	/*
655*e4060dadSLyude Paul 	 * Prior to DP1.3 the bit represented by
656*e4060dadSLyude Paul 	 * DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT was reserved.
657*e4060dadSLyude Paul 	 * If it is set DP_DPCD_REV at 0000h could be at a value less than
658*e4060dadSLyude Paul 	 * the true capability of the panel. The only way to check is to
659*e4060dadSLyude Paul 	 * then compare 0000h and 2200h.
660*e4060dadSLyude Paul 	 */
661*e4060dadSLyude Paul 	if (!(outp->dp.dpcd[DP_TRAINING_AUX_RD_INTERVAL] &
662*e4060dadSLyude Paul 	      DP_EXTENDED_RECEIVER_CAP_FIELD_PRESENT))
663*e4060dadSLyude Paul 		return 0;
664*e4060dadSLyude Paul 
665*e4060dadSLyude Paul 	ret = nvkm_rdaux(aux, DP_DP13_DPCD_REV, dpcd_ext, sizeof(dpcd_ext));
666*e4060dadSLyude Paul 	if (ret < 0)
667*e4060dadSLyude Paul 		return ret;
668*e4060dadSLyude Paul 
669*e4060dadSLyude Paul 	if (outp->dp.dpcd[DP_DPCD_REV] > dpcd_ext[DP_DPCD_REV]) {
670*e4060dadSLyude Paul 		OUTP_DBG(outp, "Extended DPCD rev less than base DPCD rev (%d > %d)\n",
671*e4060dadSLyude Paul 			 outp->dp.dpcd[DP_DPCD_REV], dpcd_ext[DP_DPCD_REV]);
672*e4060dadSLyude Paul 		return 0;
673*e4060dadSLyude Paul 	}
674*e4060dadSLyude Paul 
675*e4060dadSLyude Paul 	if (!memcmp(outp->dp.dpcd, dpcd_ext, sizeof(dpcd_ext)))
676*e4060dadSLyude Paul 		return 0;
677*e4060dadSLyude Paul 
678*e4060dadSLyude Paul 	memcpy(outp->dp.dpcd, dpcd_ext, sizeof(dpcd_ext));
679*e4060dadSLyude Paul 
680*e4060dadSLyude Paul 	return 0;
681*e4060dadSLyude Paul }
682*e4060dadSLyude Paul 
683a62b7493SBen Skeggs void
nvkm_dp_enable(struct nvkm_outp * outp,bool auxpwr)684a62b7493SBen Skeggs nvkm_dp_enable(struct nvkm_outp *outp, bool auxpwr)
685af85389cSBen Skeggs {
686a62b7493SBen Skeggs 	struct nvkm_gpio *gpio = outp->disp->engine.subdev.device->gpio;
6877786fb36SBen Skeggs 	struct nvkm_i2c_aux *aux = outp->dp.aux;
688af85389cSBen Skeggs 
689a62b7493SBen Skeggs 	if (auxpwr && !outp->dp.aux_pwr) {
690a62b7493SBen Skeggs 		/* eDP panels need powering on by us (if the VBIOS doesn't default it
691a62b7493SBen Skeggs 		 * to on) before doing any AUX channel transactions.  LVDS panel power
692a62b7493SBen Skeggs 		 * is handled by the SOR itself, and not required for LVDS DDC.
693a62b7493SBen Skeggs 		 */
694a62b7493SBen Skeggs 		if (outp->conn->info.type == DCB_CONNECTOR_eDP) {
695a62b7493SBen Skeggs 			int power = nvkm_gpio_get(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff);
696a62b7493SBen Skeggs 			if (power == 0) {
697a62b7493SBen Skeggs 				nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 1);
698a62b7493SBen Skeggs 				outp->dp.aux_pwr_pu = true;
699a62b7493SBen Skeggs 			}
700a62b7493SBen Skeggs 
701a62b7493SBen Skeggs 			/* We delay here unconditionally, even if already powered,
702a62b7493SBen Skeggs 			 * because some laptop panels having a significant resume
703a62b7493SBen Skeggs 			 * delay before the panel begins responding.
704a62b7493SBen Skeggs 			 *
705a62b7493SBen Skeggs 			 * This is likely a bit of a hack, but no better idea for
706a62b7493SBen Skeggs 			 * handling this at the moment.
707a62b7493SBen Skeggs 			 */
708a62b7493SBen Skeggs 			msleep(300);
709a62b7493SBen Skeggs 		}
710a62b7493SBen Skeggs 
7117786fb36SBen Skeggs 		OUTP_DBG(outp, "aux power -> always");
712af85389cSBen Skeggs 		nvkm_i2c_aux_monitor(aux, true);
713a62b7493SBen Skeggs 		outp->dp.aux_pwr = true;
714af85389cSBen Skeggs 
715f21e5fa1SBen Skeggs 		/* Detect any LTTPRs before reading DPCD receiver caps. */
7167786fb36SBen Skeggs 		if (!nvkm_rdaux(aux, DPCD_LTTPR_REV, outp->dp.lttpr, sizeof(outp->dp.lttpr)) &&
7177786fb36SBen Skeggs 		    outp->dp.lttpr[0] >= 0x14 && outp->dp.lttpr[2]) {
7187786fb36SBen Skeggs 			switch (outp->dp.lttpr[2]) {
7197786fb36SBen Skeggs 			case 0x80: outp->dp.lttprs = 1; break;
7207786fb36SBen Skeggs 			case 0x40: outp->dp.lttprs = 2; break;
7217786fb36SBen Skeggs 			case 0x20: outp->dp.lttprs = 3; break;
7227786fb36SBen Skeggs 			case 0x10: outp->dp.lttprs = 4; break;
7237786fb36SBen Skeggs 			case 0x08: outp->dp.lttprs = 5; break;
7247786fb36SBen Skeggs 			case 0x04: outp->dp.lttprs = 6; break;
7257786fb36SBen Skeggs 			case 0x02: outp->dp.lttprs = 7; break;
7267786fb36SBen Skeggs 			case 0x01: outp->dp.lttprs = 8; break;
727f21e5fa1SBen Skeggs 			default:
728f21e5fa1SBen Skeggs 				/* Unknown LTTPR count, we'll switch to transparent mode. */
729f21e5fa1SBen Skeggs 				WARN_ON(1);
7307786fb36SBen Skeggs 				outp->dp.lttprs = 0;
731f21e5fa1SBen Skeggs 				break;
732f21e5fa1SBen Skeggs 			}
733f21e5fa1SBen Skeggs 		} else {
734f21e5fa1SBen Skeggs 			/* No LTTPR support, or zero LTTPR count - don't touch it at all. */
7357786fb36SBen Skeggs 			memset(outp->dp.lttpr, 0x00, sizeof(outp->dp.lttpr));
736f21e5fa1SBen Skeggs 		}
737f21e5fa1SBen Skeggs 
738*e4060dadSLyude Paul 		if (!nvkm_dp_read_dpcd_caps(outp)) {
739405d5382SBen Skeggs 			const u8 rates[] = { 0x1e, 0x14, 0x0a, 0x06, 0 };
740b96a1d8cSBen Skeggs 			const u8 *rate;
741b96a1d8cSBen Skeggs 			int rate_max;
742b96a1d8cSBen Skeggs 
7437786fb36SBen Skeggs 			outp->dp.rates = 0;
7447786fb36SBen Skeggs 			outp->dp.links = outp->dp.dpcd[DPCD_RC02] & DPCD_RC02_MAX_LANE_COUNT;
7457786fb36SBen Skeggs 			outp->dp.links = min(outp->dp.links, outp->info.dpconf.link_nr);
7467786fb36SBen Skeggs 			if (outp->dp.lttprs && outp->dp.lttpr[4])
7477786fb36SBen Skeggs 				outp->dp.links = min_t(int, outp->dp.links, outp->dp.lttpr[4]);
748b96a1d8cSBen Skeggs 
7497786fb36SBen Skeggs 			rate_max = outp->dp.dpcd[DPCD_RC01_MAX_LINK_RATE];
7507786fb36SBen Skeggs 			rate_max = min(rate_max, outp->info.dpconf.link_bw);
7517786fb36SBen Skeggs 			if (outp->dp.lttprs && outp->dp.lttpr[1])
7527786fb36SBen Skeggs 				rate_max = min_t(int, rate_max, outp->dp.lttpr[1]);
753b96a1d8cSBen Skeggs 
7547786fb36SBen Skeggs 			if (!nvkm_dp_enable_supported_link_rates(outp)) {
755b96a1d8cSBen Skeggs 				for (rate = rates; *rate; rate++) {
7567786fb36SBen Skeggs 					if (*rate > rate_max)
7577786fb36SBen Skeggs 						continue;
7587786fb36SBen Skeggs 
7597786fb36SBen Skeggs 					if (WARN_ON(outp->dp.rates == ARRAY_SIZE(outp->dp.rate)))
760b96a1d8cSBen Skeggs 						break;
761b96a1d8cSBen Skeggs 
7627786fb36SBen Skeggs 					outp->dp.rate[outp->dp.rates].dpcd = -1;
7637786fb36SBen Skeggs 					outp->dp.rate[outp->dp.rates].rate = *rate * 27000;
7647786fb36SBen Skeggs 					outp->dp.rates++;
765b96a1d8cSBen Skeggs 				}
766b96a1d8cSBen Skeggs 			}
767af85389cSBen Skeggs 		}
768a62b7493SBen Skeggs 	} else
769a62b7493SBen Skeggs 	if (!auxpwr && outp->dp.aux_pwr) {
7707786fb36SBen Skeggs 		OUTP_DBG(outp, "aux power -> demand");
771af85389cSBen Skeggs 		nvkm_i2c_aux_monitor(aux, false);
772a62b7493SBen Skeggs 		outp->dp.aux_pwr = false;
7737786fb36SBen Skeggs 		atomic_set(&outp->dp.lt.done, 0);
774a62b7493SBen Skeggs 
775a62b7493SBen Skeggs 		/* Restore eDP panel GPIO to its prior state if we changed it, as
776a62b7493SBen Skeggs 		 * it could potentially interfere with other outputs.
777a62b7493SBen Skeggs 		 */
778a62b7493SBen Skeggs 		if (outp->conn->info.type == DCB_CONNECTOR_eDP) {
779a62b7493SBen Skeggs 			if (outp->dp.aux_pwr_pu) {
780a62b7493SBen Skeggs 				nvkm_gpio_set(gpio, 0, DCB_GPIO_PANEL_POWER, 0xff, 0);
781a62b7493SBen Skeggs 				outp->dp.aux_pwr_pu = false;
782a62b7493SBen Skeggs 			}
783a62b7493SBen Skeggs 		}
784a62b7493SBen Skeggs 	}
785af85389cSBen Skeggs }
786af85389cSBen Skeggs 
787af85389cSBen Skeggs static void
nvkm_dp_fini(struct nvkm_outp * outp)788f3e70d29SBen Skeggs nvkm_dp_fini(struct nvkm_outp *outp)
789af85389cSBen Skeggs {
7907786fb36SBen Skeggs 	nvkm_dp_enable(outp, false);
791af85389cSBen Skeggs }
792af85389cSBen Skeggs 
793af85389cSBen Skeggs static void
nvkm_dp_init(struct nvkm_outp * outp)794f3e70d29SBen Skeggs nvkm_dp_init(struct nvkm_outp *outp)
795af85389cSBen Skeggs {
796a62b7493SBen Skeggs 	nvkm_dp_enable(outp, outp->dp.enabled);
797af85389cSBen Skeggs }
798af85389cSBen Skeggs 
799af85389cSBen Skeggs static void *
nvkm_dp_dtor(struct nvkm_outp * outp)800f3e70d29SBen Skeggs nvkm_dp_dtor(struct nvkm_outp *outp)
801af85389cSBen Skeggs {
8027786fb36SBen Skeggs 	return outp;
803af85389cSBen Skeggs }
804af85389cSBen Skeggs 
805f3e70d29SBen Skeggs static const struct nvkm_outp_func
806f3e70d29SBen Skeggs nvkm_dp_func = {
807f3e70d29SBen Skeggs 	.dtor = nvkm_dp_dtor,
808f3e70d29SBen Skeggs 	.init = nvkm_dp_init,
809f3e70d29SBen Skeggs 	.fini = nvkm_dp_fini,
8108d7ef84dSBen Skeggs 	.acquire = nvkm_dp_acquire,
811d52e948cSBen Skeggs 	.release = nvkm_dp_release,
812e04cfdc9SBen Skeggs 	.disable = nvkm_dp_disable,
813af85389cSBen Skeggs };
814af85389cSBen Skeggs 
815412dfcf3SBen Skeggs int
nvkm_dp_new(struct nvkm_disp * disp,int index,struct dcb_output * dcbE,struct nvkm_outp ** poutp)816412dfcf3SBen Skeggs nvkm_dp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE, struct nvkm_outp **poutp)
817af85389cSBen Skeggs {
818af85389cSBen Skeggs 	struct nvkm_device *device = disp->engine.subdev.device;
819af85389cSBen Skeggs 	struct nvkm_bios *bios = device->bios;
820af85389cSBen Skeggs 	struct nvkm_i2c *i2c = device->i2c;
821412dfcf3SBen Skeggs 	struct nvkm_outp *outp;
822af85389cSBen Skeggs 	u8  hdr, cnt, len;
823af85389cSBen Skeggs 	u32 data;
824af85389cSBen Skeggs 	int ret;
825af85389cSBen Skeggs 
826412dfcf3SBen Skeggs 	ret = nvkm_outp_new_(&nvkm_dp_func, disp, index, dcbE, poutp);
827412dfcf3SBen Skeggs 	outp = *poutp;
82801a97637SBen Skeggs 	if (ret)
82901a97637SBen Skeggs 		return ret;
83001a97637SBen Skeggs 
831412dfcf3SBen Skeggs 	if (dcbE->location == 0)
832412dfcf3SBen Skeggs 		outp->dp.aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_CCB(dcbE->i2c_index));
833412dfcf3SBen Skeggs 	else
834412dfcf3SBen Skeggs 		outp->dp.aux = nvkm_i2c_aux_find(i2c, NVKM_I2C_AUX_EXT(dcbE->extdev));
8357786fb36SBen Skeggs 	if (!outp->dp.aux) {
8367786fb36SBen Skeggs 		OUTP_ERR(outp, "no aux");
8373c66c87dSBen Skeggs 		return -EINVAL;
838af85389cSBen Skeggs 	}
839af85389cSBen Skeggs 
840af85389cSBen Skeggs 	/* bios data is not optional */
8417786fb36SBen Skeggs 	data = nvbios_dpout_match(bios, outp->info.hasht, outp->info.hashm,
8427786fb36SBen Skeggs 				  &outp->dp.version, &hdr, &cnt, &len, &outp->dp.info);
843af85389cSBen Skeggs 	if (!data) {
8447786fb36SBen Skeggs 		OUTP_ERR(outp, "no bios dp data");
8453c66c87dSBen Skeggs 		return -EINVAL;
846af85389cSBen Skeggs 	}
847af85389cSBen Skeggs 
8487786fb36SBen Skeggs 	OUTP_DBG(outp, "bios dp %02x %02x %02x %02x", outp->dp.version, hdr, cnt, len);
849af85389cSBen Skeggs 
8507786fb36SBen Skeggs 	mutex_init(&outp->dp.mutex);
8517786fb36SBen Skeggs 	atomic_set(&outp->dp.lt.done, 0);
852af85389cSBen Skeggs 	return 0;
853af85389cSBen Skeggs }
854