nouveau_dp.c 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright 2009 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs
 */

#include "drmP.h"
26
#include "drm_dp_helper.h"
27

28
#include "nouveau_drv.h"
29
#include "nouveau_connector.h"
30
#include "nouveau_encoder.h"
31
#include "nouveau_crtc.h"
32

33
u8 *
34
nouveau_dp_bios_data(struct drm_device *dev, struct dcb_output *dcb, u8 **entry)
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{
	struct bit_entry d;
	u8 *table;
	int i;

	if (bit_table(dev, 'd', &d)) {
		NV_ERROR(dev, "BIT 'd' table not found\n");
		return NULL;
	}

	if (d.version != 1) {
		NV_ERROR(dev, "BIT 'd' table version %d unknown\n", d.version);
		return NULL;
	}

50
	table = ROMPTR(dev, d.data[0]);
51
52
53
54
55
56
57
58
	if (!table) {
		NV_ERROR(dev, "displayport table pointer invalid\n");
		return NULL;
	}

	switch (table[0]) {
	case 0x20:
	case 0x21:
59
	case 0x30:
60
	case 0x40:
61
62
63
64
65
66
67
		break;
	default:
		NV_ERROR(dev, "displayport table 0x%02x unknown\n", table[0]);
		return NULL;
	}

	for (i = 0; i < table[3]; i++) {
68
		*entry = ROMPTR(dev, table[table[1] + (i * table[2])]);
69
70
71
72
73
74
75
76
		if (*entry && bios_encoder_match(dcb, ROM32((*entry)[0])))
			return table;
	}

	NV_ERROR(dev, "displayport encoder table not found\n");
	return NULL;
}

77
78
79
80
/******************************************************************************
 * link training
 *****************************************************************************/
struct dp_state {
81
	struct nouveau_i2c_port *auxch;
82
	struct dp_train_func *func;
83
	struct dcb_output *dcb;
84
	int crtc;
85
	u8 *dpcd;
86
87
88
89
90
	int link_nr;
	u32 link_bw;
	u8  stat[6];
	u8  conf[4];
};
91

92
93
static void
dp_set_link_config(struct drm_device *dev, struct dp_state *dp)
94
{
95
	u8 sink[2];
96

97
	NV_DEBUG_KMS(dev, "%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
98

99
100
101
	/* set desired link configuration on the source */
	dp->func->link_set(dev, dp->dcb, dp->crtc, dp->link_nr, dp->link_bw,
			   dp->dpcd[2] & DP_ENHANCED_FRAME_CAP);
102

103
104
	/* inform the sink of the new configuration */
	sink[0] = dp->link_bw / 27000;
105
	sink[1] = dp->link_nr;
106
	if (dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)
107
		sink[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
108

109
	auxch_wr(dev, dp->auxch, DP_LINK_BW_SET, sink, 2);
110
111
}

112
static void
113
dp_set_training_pattern(struct drm_device *dev, struct dp_state *dp, u8 pattern)
114
{
115
116
	u8 sink_tp;

117
	NV_DEBUG_KMS(dev, "training pattern %d\n", pattern);
118

119
	dp->func->train_set(dev, dp->dcb, pattern);
120

121
	auxch_rd(dev, dp->auxch, DP_TRAINING_PATTERN_SET, &sink_tp, 1);
122
	sink_tp &= ~DP_TRAINING_PATTERN_MASK;
123
	sink_tp |= pattern;
124
	auxch_wr(dev, dp->auxch, DP_TRAINING_PATTERN_SET, &sink_tp, 1);
125
126
127
}

static int
128
dp_link_train_commit(struct drm_device *dev, struct dp_state *dp)
129
{
130
131
132
	int i;

	for (i = 0; i < dp->link_nr; i++) {
133
134
135
		u8 lane = (dp->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
		u8 lpre = (lane & 0x0c) >> 2;
		u8 lvsw = (lane & 0x03) >> 0;
136

137
138
		dp->conf[i] = (lpre << 3) | lvsw;
		if (lvsw == DP_TRAIN_VOLTAGE_SWING_1200)
139
			dp->conf[i] |= DP_TRAIN_MAX_SWING_REACHED;
140
		if ((lpre << 3) == DP_TRAIN_PRE_EMPHASIS_9_5)
141
			dp->conf[i] |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
142

143
		NV_DEBUG_KMS(dev, "config lane %d %02x\n", i, dp->conf[i]);
144
		dp->func->train_adj(dev, dp->dcb, i, lvsw, lpre);
145
146
	}

147
	return auxch_wr(dev, dp->auxch, DP_TRAINING_LANE0_SET, dp->conf, 4);
148
149
}

150
151
static int
dp_link_train_update(struct drm_device *dev, struct dp_state *dp, u32 delay)
152
{
153
	int ret;
154

155
	udelay(delay);
156

157
	ret = auxch_rd(dev, dp->auxch, DP_LANE0_1_STATUS, dp->stat, 6);
158
	if (ret)
159
		return ret;
160

161
162
163
164
165
	NV_DEBUG_KMS(dev, "status %02x %02x %02x %02x %02x %02x\n",
		     dp->stat[0], dp->stat[1], dp->stat[2], dp->stat[3],
		     dp->stat[4], dp->stat[5]);
	return 0;
}
166

167
168
169
170
171
172
static int
dp_link_train_cr(struct drm_device *dev, struct dp_state *dp)
{
	bool cr_done = false, abort = false;
	int voltage = dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
	int tries = 0, i;
173

174
	dp_set_training_pattern(dev, dp, DP_TRAINING_PATTERN_1);
175

176
177
178
179
	do {
		if (dp_link_train_commit(dev, dp) ||
		    dp_link_train_update(dev, dp, 100))
			break;
180

181
182
183
184
185
186
187
188
189
190
		cr_done = true;
		for (i = 0; i < dp->link_nr; i++) {
			u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
			if (!(lane & DP_LANE_CR_DONE)) {
				cr_done = false;
				if (dp->conf[i] & DP_TRAIN_MAX_SWING_REACHED)
					abort = true;
				break;
			}
		}
191

192
193
194
195
196
		if ((dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK) != voltage) {
			voltage = dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
			tries = 0;
		}
	} while (!cr_done && !abort && ++tries < 5);
197

198
	return cr_done ? 0 : -1;
199
200
}

201
202
static int
dp_link_train_eq(struct drm_device *dev, struct dp_state *dp)
203
{
204
205
	bool eq_done, cr_done = true;
	int tries = 0, i;
206

207
	dp_set_training_pattern(dev, dp, DP_TRAINING_PATTERN_2);
208

209
210
	do {
		if (dp_link_train_update(dev, dp, 400))
211
212
			break;

213
214
215
216
217
218
219
220
221
		eq_done = !!(dp->stat[2] & DP_INTERLANE_ALIGN_DONE);
		for (i = 0; i < dp->link_nr && eq_done; i++) {
			u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
			if (!(lane & DP_LANE_CR_DONE))
				cr_done = false;
			if (!(lane & DP_LANE_CHANNEL_EQ_DONE) ||
			    !(lane & DP_LANE_SYMBOL_LOCKED))
				eq_done = false;
		}
222

223
224
225
226
227
		if (dp_link_train_commit(dev, dp))
			break;
	} while (!eq_done && cr_done && ++tries <= 5);

	return eq_done ? 0 : -1;
228
229
}

230
231
232
233
234
235
236
static void
dp_set_downspread(struct drm_device *dev, struct dp_state *dp, bool enable)
{
	u16 script = 0x0000;
	u8 *entry, *table = nouveau_dp_bios_data(dev, dp->dcb, &entry);
	if (table) {
		if (table[0] >= 0x20 && table[0] <= 0x30) {
237
238
			if (enable) script = ROM16(entry[12]);
			else        script = ROM16(entry[14]);
239
240
241
242
		} else
		if (table[0] == 0x40) {
			if (enable) script = ROM16(entry[11]);
			else        script = ROM16(entry[13]);
243
244
245
246
247
248
249
250
251
252
253
254
255
256
		}
	}

	nouveau_bios_run_init_table(dev, script, dp->dcb, dp->crtc);
}

static void
dp_link_train_init(struct drm_device *dev, struct dp_state *dp)
{
	u16 script = 0x0000;
	u8 *entry, *table = nouveau_dp_bios_data(dev, dp->dcb, &entry);
	if (table) {
		if (table[0] >= 0x20 && table[0] <= 0x30)
			script = ROM16(entry[6]);
257
258
259
		else
		if (table[0] == 0x40)
			script = ROM16(entry[5]);
260
261
262
263
264
265
266
267
268
269
270
271
272
	}

	nouveau_bios_run_init_table(dev, script, dp->dcb, dp->crtc);
}

static void
dp_link_train_fini(struct drm_device *dev, struct dp_state *dp)
{
	u16 script = 0x0000;
	u8 *entry, *table = nouveau_dp_bios_data(dev, dp->dcb, &entry);
	if (table) {
		if (table[0] >= 0x20 && table[0] <= 0x30)
			script = ROM16(entry[8]);
273
274
275
		else
		if (table[0] == 0x40)
			script = ROM16(entry[7]);
276
277
278
279
280
	}

	nouveau_bios_run_init_table(dev, script, dp->dcb, dp->crtc);
}

281
bool
282
283
nouveau_dp_link_train(struct drm_encoder *encoder, u32 datarate,
		      struct dp_train_func *func)
284
285
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
286
287
288
289
290
291
292
	struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
	struct nouveau_connector *nv_connector =
		nouveau_encoder_connector_get(nv_encoder);
	struct drm_device *dev = encoder->dev;
	const u32 bw_list[] = { 270000, 162000, 0 };
	const u32 *link_bw = bw_list;
	struct dp_state dp;
293

294
295
	dp.auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
	if (!dp.auxch)
296
297
		return false;

298
	dp.func = func;
299
300
	dp.dcb = nv_encoder->dcb;
	dp.crtc = nv_crtc->index;
301
	dp.dpcd = nv_encoder->dp.dpcd;
302

303
304
305
	/* adjust required bandwidth for 8B/10B coding overhead */
	datarate = (datarate / 8) * 10;

306
307
308
309
	/* some sinks toggle hotplug in response to some of the actions
	 * we take during link training (DP_SET_POWER is one), we need
	 * to ignore them for the moment to avoid races.
	 */
310
	nouveau_gpio_irq(dev, 0, nv_connector->hpd, 0xff, false);
311

312
	/* enable down-spreading, if possible */
313
	dp_set_downspread(dev, &dp, nv_encoder->dp.dpcd[3] & 1);
314

315
	/* execute pre-train script from vbios */
316
	dp_link_train_init(dev, &dp);
317

318
	/* start off at highest link rate supported by encoder and display */
319
	while (*link_bw > nv_encoder->dp.link_bw)
320
		link_bw++;
321

322
323
324
325
326
	while (link_bw[0]) {
		/* find minimum required lane count at this link rate */
		dp.link_nr = nv_encoder->dp.link_nr;
		while ((dp.link_nr >> 1) * link_bw[0] > datarate)
			dp.link_nr >>= 1;
327

328
329
330
331
		/* drop link rate to minimum with this lane count */
		while ((link_bw[1] * dp.link_nr) > datarate)
			link_bw++;
		dp.link_bw = link_bw[0];
332

333
334
		/* program selected link configuration */
		dp_set_link_config(dev, &dp);
335

336
337
338
339
		/* attempt to train the link at this configuration */
		memset(dp.stat, 0x00, sizeof(dp.stat));
		if (!dp_link_train_cr(dev, &dp) &&
		    !dp_link_train_eq(dev, &dp))
340
341
			break;

342
343
		/* retry at lower rate */
		link_bw++;
344
345
	}

346
347
	/* finish link training */
	dp_set_training_pattern(dev, &dp, DP_TRAINING_PATTERN_DISABLE);
348

349
	/* execute post-train script from vbios */
350
	dp_link_train_fini(dev, &dp);
351

352
	/* re-enable hotplug detect */
353
	nouveau_gpio_irq(dev, 0, nv_connector->hpd, 0xff, true);
354
	return true;
355
356
}

357
358
359
360
361
void
nouveau_dp_dpms(struct drm_encoder *encoder, int mode, u32 datarate,
		struct dp_train_func *func)
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
362
	struct nouveau_i2c_port *auxch;
363
364
365
366
367
368
369
370
371
372
373
	u8 status;

	auxch = nouveau_i2c_find(encoder->dev, nv_encoder->dcb->i2c_index);
	if (!auxch)
		return;

	if (mode == DRM_MODE_DPMS_ON)
		status = DP_SET_POWER_D0;
	else
		status = DP_SET_POWER_D3;

374
	auxch_wr(encoder->dev, auxch, DP_SET_POWER, &status, 1);
375
376
377
378
379

	if (mode == DRM_MODE_DPMS_ON)
		nouveau_dp_link_train(encoder, datarate, func);
}

380
static void
381
nouveau_dp_probe_oui(struct drm_device *dev, struct nouveau_i2c_port *auxch,
382
383
384
385
386
387
388
		     u8 *dpcd)
{
	u8 buf[3];

	if (!(dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
		return;

389
	if (!auxch_rd(dev, auxch, DP_SINK_OUI, buf, 3))
390
391
392
		NV_DEBUG_KMS(dev, "Sink OUI: %02hx%02hx%02hx\n",
			     buf[0], buf[1], buf[2]);

393
	if (!auxch_rd(dev, auxch, DP_BRANCH_OUI, buf, 3))
394
395
396
397
398
		NV_DEBUG_KMS(dev, "Branch OUI: %02hx%02hx%02hx\n",
			     buf[0], buf[1], buf[2]);

}

399
400
401
402
403
bool
nouveau_dp_detect(struct drm_encoder *encoder)
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
	struct drm_device *dev = encoder->dev;
404
	struct nouveau_i2c_port *auxch;
405
	u8 *dpcd = nv_encoder->dp.dpcd;
406
407
	int ret;

408
409
410
411
	auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
	if (!auxch)
		return false;

412
	ret = auxch_rd(dev, auxch, DP_DPCD_REV, dpcd, 8);
413
414
415
	if (ret)
		return false;

416
417
	nv_encoder->dp.link_bw = 27000 * dpcd[1];
	nv_encoder->dp.link_nr = dpcd[2] & DP_MAX_LANE_COUNT_MASK;
418

419
420
421
422
423
	NV_DEBUG_KMS(dev, "display: %dx%d dpcd 0x%02x\n",
		     nv_encoder->dp.link_nr, nv_encoder->dp.link_bw, dpcd[0]);
	NV_DEBUG_KMS(dev, "encoder: %dx%d\n",
		     nv_encoder->dcb->dpconf.link_nr,
		     nv_encoder->dcb->dpconf.link_bw);
424

425
	if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
426
		nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
427
428
	if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
		nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
429

430
431
	NV_DEBUG_KMS(dev, "maximum: %dx%d\n",
		     nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
432

433
434
	nouveau_dp_probe_oui(dev, auxch, dpcd);

435
436
	return true;
}