nouveau_dp.c 12 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_drm.h"
29
#include "nouveau_connector.h"
30
#include "nouveau_encoder.h"
31
#include "nouveau_crtc.h"
32

33
34
35
#include <subdev/gpio.h>
#include <subdev/i2c.h>

36
u8 *
37
nouveau_dp_bios_data(struct drm_device *dev, struct dcb_output *dcb, u8 **entry)
38
{
39
	struct nouveau_drm *drm = nouveau_drm(dev);
40
41
42
43
44
	struct bit_entry d;
	u8 *table;
	int i;

	if (bit_table(dev, 'd', &d)) {
45
		NV_ERROR(drm, "BIT 'd' table not found\n");
46
47
48
49
		return NULL;
	}

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

54
	table = ROMPTR(dev, d.data[0]);
55
	if (!table) {
56
		NV_ERROR(drm, "displayport table pointer invalid\n");
57
58
59
60
61
62
		return NULL;
	}

	switch (table[0]) {
	case 0x20:
	case 0x21:
63
	case 0x30:
64
	case 0x40:
65
66
		break;
	default:
67
		NV_ERROR(drm, "displayport table 0x%02x unknown\n", table[0]);
68
69
70
71
		return NULL;
	}

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

77
	NV_ERROR(drm, "displayport encoder table not found\n");
78
79
80
	return NULL;
}

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

96
97
static void
dp_set_link_config(struct drm_device *dev, struct dp_state *dp)
98
{
99
	struct nouveau_drm *drm = nouveau_drm(dev);
100
	u8 sink[2];
101

102
	NV_DEBUG(drm, "%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
103

104
105
106
	/* 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);
107

108
109
	/* inform the sink of the new configuration */
	sink[0] = dp->link_bw / 27000;
110
	sink[1] = dp->link_nr;
111
	if (dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)
112
		sink[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
113

114
	nv_wraux(dp->auxch, DP_LINK_BW_SET, sink, 2);
115
116
}

117
static void
118
dp_set_training_pattern(struct drm_device *dev, struct dp_state *dp, u8 pattern)
119
{
120
	struct nouveau_drm *drm = nouveau_drm(dev);
121
122
	u8 sink_tp;

123
	NV_DEBUG(drm, "training pattern %d\n", pattern);
124

125
	dp->func->train_set(dev, dp->dcb, pattern);
126

127
	nv_rdaux(dp->auxch, DP_TRAINING_PATTERN_SET, &sink_tp, 1);
128
	sink_tp &= ~DP_TRAINING_PATTERN_MASK;
129
	sink_tp |= pattern;
130
	nv_wraux(dp->auxch, DP_TRAINING_PATTERN_SET, &sink_tp, 1);
131
132
133
}

static int
134
dp_link_train_commit(struct drm_device *dev, struct dp_state *dp)
135
{
136
	struct nouveau_drm *drm = nouveau_drm(dev);
137
138
139
	int i;

	for (i = 0; i < dp->link_nr; i++) {
140
141
142
		u8 lane = (dp->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
		u8 lpre = (lane & 0x0c) >> 2;
		u8 lvsw = (lane & 0x03) >> 0;
143

144
145
		dp->conf[i] = (lpre << 3) | lvsw;
		if (lvsw == DP_TRAIN_VOLTAGE_SWING_1200)
146
			dp->conf[i] |= DP_TRAIN_MAX_SWING_REACHED;
147
		if ((lpre << 3) == DP_TRAIN_PRE_EMPHASIS_9_5)
148
			dp->conf[i] |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
149

150
		NV_DEBUG(drm, "config lane %d %02x\n", i, dp->conf[i]);
151
		dp->func->train_adj(dev, dp->dcb, i, lvsw, lpre);
152
153
	}

154
	return nv_wraux(dp->auxch, DP_TRAINING_LANE0_SET, dp->conf, 4);
155
156
}

157
158
static int
dp_link_train_update(struct drm_device *dev, struct dp_state *dp, u32 delay)
159
{
160
	struct nouveau_drm *drm = nouveau_drm(dev);
161
	int ret;
162

163
	udelay(delay);
164

165
	ret = nv_rdaux(dp->auxch, DP_LANE0_1_STATUS, dp->stat, 6);
166
	if (ret)
167
		return ret;
168

169
	NV_DEBUG(drm, "status %02x %02x %02x %02x %02x %02x\n",
170
171
172
173
		     dp->stat[0], dp->stat[1], dp->stat[2], dp->stat[3],
		     dp->stat[4], dp->stat[5]);
	return 0;
}
174

175
176
177
178
179
180
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;
181

182
	dp_set_training_pattern(dev, dp, DP_TRAINING_PATTERN_1);
183

184
185
186
187
	do {
		if (dp_link_train_commit(dev, dp) ||
		    dp_link_train_update(dev, dp, 100))
			break;
188

189
190
191
192
193
194
195
196
197
198
		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;
			}
		}
199

200
201
202
203
204
		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);
205

206
	return cr_done ? 0 : -1;
207
208
}

209
210
static int
dp_link_train_eq(struct drm_device *dev, struct dp_state *dp)
211
{
212
213
	bool eq_done, cr_done = true;
	int tries = 0, i;
214

215
	dp_set_training_pattern(dev, dp, DP_TRAINING_PATTERN_2);
216

217
218
	do {
		if (dp_link_train_update(dev, dp, 400))
219
220
			break;

221
222
223
224
225
226
227
228
229
		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;
		}
230

231
232
233
234
235
		if (dp_link_train_commit(dev, dp))
			break;
	} while (!eq_done && cr_done && ++tries <= 5);

	return eq_done ? 0 : -1;
236
237
}

238
239
240
241
242
243
244
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) {
245
246
			if (enable) script = ROM16(entry[12]);
			else        script = ROM16(entry[14]);
247
248
249
250
		} else
		if (table[0] == 0x40) {
			if (enable) script = ROM16(entry[11]);
			else        script = ROM16(entry[13]);
251
252
253
254
255
256
257
258
259
260
261
262
263
264
		}
	}

	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]);
265
266
267
		else
		if (table[0] == 0x40)
			script = ROM16(entry[5]);
268
269
270
271
272
273
274
275
276
277
278
279
280
	}

	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]);
281
282
283
		else
		if (table[0] == 0x40)
			script = ROM16(entry[7]);
284
285
286
287
288
	}

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

289
bool
290
291
nouveau_dp_link_train(struct drm_encoder *encoder, u32 datarate,
		      struct dp_train_func *func)
292
293
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
294
295
296
297
	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;
298
299
300
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
	struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
301
302
303
	const u32 bw_list[] = { 270000, 162000, 0 };
	const u32 *link_bw = bw_list;
	struct dp_state dp;
304

305
	dp.auxch = i2c->find(i2c, nv_encoder->dcb->i2c_index);
306
	if (!dp.auxch)
307
308
		return false;

309
	dp.func = func;
310
311
	dp.dcb = nv_encoder->dcb;
	dp.crtc = nv_crtc->index;
312
	dp.dpcd = nv_encoder->dp.dpcd;
313

314
315
316
	/* adjust required bandwidth for 8B/10B coding overhead */
	datarate = (datarate / 8) * 10;

317
318
319
320
	/* 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.
	 */
321
	gpio->irq(gpio, 0, nv_connector->hpd, 0xff, false);
322

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

326
	/* execute pre-train script from vbios */
327
	dp_link_train_init(dev, &dp);
328

329
	/* start off at highest link rate supported by encoder and display */
330
	while (*link_bw > nv_encoder->dp.link_bw)
331
		link_bw++;
332

333
334
335
336
337
	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;
338

339
340
341
342
		/* 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];
343

344
345
		/* program selected link configuration */
		dp_set_link_config(dev, &dp);
346

347
348
349
350
		/* 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))
351
352
			break;

353
354
		/* retry at lower rate */
		link_bw++;
355
356
	}

357
358
	/* finish link training */
	dp_set_training_pattern(dev, &dp, DP_TRAINING_PATTERN_DISABLE);
359

360
	/* execute post-train script from vbios */
361
	dp_link_train_fini(dev, &dp);
362

363
	/* re-enable hotplug detect */
364
	gpio->irq(gpio, 0, nv_connector->hpd, 0xff, true);
365
	return true;
366
367
}

368
369
370
371
372
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);
373
374
	struct nouveau_drm *drm = nouveau_drm(encoder->dev);
	struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
375
	struct nouveau_i2c_port *auxch;
376
377
	u8 status;

378
	auxch = i2c->find(i2c, nv_encoder->dcb->i2c_index);
379
380
381
382
383
384
385
386
	if (!auxch)
		return;

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

387
	nv_wraux(auxch, DP_SET_POWER, &status, 1);
388
389
390
391
392

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

393
static void
394
nouveau_dp_probe_oui(struct drm_device *dev, struct nouveau_i2c_port *auxch,
395
396
		     u8 *dpcd)
{
397
	struct nouveau_drm *drm = nouveau_drm(dev);
398
399
400
401
402
	u8 buf[3];

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

403
404
	if (!nv_rdaux(auxch, DP_SINK_OUI, buf, 3))
		NV_DEBUG(drm, "Sink OUI: %02hx%02hx%02hx\n",
405
406
			     buf[0], buf[1], buf[2]);

407
408
	if (!nv_rdaux(auxch, DP_BRANCH_OUI, buf, 3))
		NV_DEBUG(drm, "Branch OUI: %02hx%02hx%02hx\n",
409
410
411
412
			     buf[0], buf[1], buf[2]);

}

413
414
415
416
417
bool
nouveau_dp_detect(struct drm_encoder *encoder)
{
	struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
	struct drm_device *dev = encoder->dev;
418
419
	struct nouveau_drm *drm = nouveau_drm(dev);
	struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
420
	struct nouveau_i2c_port *auxch;
421
	u8 *dpcd = nv_encoder->dp.dpcd;
422
423
	int ret;

424
	auxch = i2c->find(i2c, nv_encoder->dcb->i2c_index);
425
426
427
	if (!auxch)
		return false;

428
	ret = nv_rdaux(auxch, DP_DPCD_REV, dpcd, 8);
429
430
431
	if (ret)
		return false;

432
433
	nv_encoder->dp.link_bw = 27000 * dpcd[1];
	nv_encoder->dp.link_nr = dpcd[2] & DP_MAX_LANE_COUNT_MASK;
434

435
	NV_DEBUG(drm, "display: %dx%d dpcd 0x%02x\n",
436
		     nv_encoder->dp.link_nr, nv_encoder->dp.link_bw, dpcd[0]);
437
	NV_DEBUG(drm, "encoder: %dx%d\n",
438
439
		     nv_encoder->dcb->dpconf.link_nr,
		     nv_encoder->dcb->dpconf.link_bw);
440

441
	if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
442
		nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
443
444
	if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
		nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
445

446
	NV_DEBUG(drm, "maximum: %dx%d\n",
447
		     nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
448

449
450
	nouveau_dp_probe_oui(dev, auxch, dpcd);

451
452
	return true;
}