blob: 13903533d7a246c3a8aab8ce5c95cf47ddf1d70e [file] [log] [blame]
Ben Skeggs0a0afd22013-02-18 23:17:53 -05001/*
2 * Copyright 2013 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Ben Skeggs
23 */
24
25#include <subdev/bios.h>
26#include <subdev/bios/dcb.h>
27#include <subdev/bios/dp.h>
28#include <subdev/bios/init.h>
29#include <subdev/i2c.h>
30
31#include <engine/disp.h>
32
33#include "dport.h"
34
35#define DBG(fmt, args...) nv_debug(dp->disp, "DP:%04x:%04x: " fmt, \
36 dp->outp->hasht, dp->outp->hashm, ##args)
37#define ERR(fmt, args...) nv_error(dp->disp, "DP:%04x:%04x: " fmt, \
38 dp->outp->hasht, dp->outp->hashm, ##args)
39
40/******************************************************************************
41 * link training
42 *****************************************************************************/
43struct dp_state {
44 const struct nouveau_dp_func *func;
45 struct nouveau_disp *disp;
46 struct dcb_output *outp;
47 struct nvbios_dpout info;
48 u8 version;
49 struct nouveau_i2c_port *aux;
50 int head;
Ben Skeggsfb7c2a72014-05-15 21:50:07 +100051 u8 dpcd[16];
Ben Skeggs0a0afd22013-02-18 23:17:53 -050052 int link_nr;
53 u32 link_bw;
54 u8 stat[6];
55 u8 conf[4];
56};
57
58static int
59dp_set_link_config(struct dp_state *dp)
60{
61 struct nouveau_disp *disp = dp->disp;
62 struct nouveau_bios *bios = nouveau_bios(disp);
63 struct nvbios_init init = {
64 .subdev = nv_subdev(dp->disp),
65 .bios = bios,
66 .offset = 0x0000,
67 .outp = dp->outp,
68 .crtc = dp->head,
69 .execute = 1,
70 };
71 u32 lnkcmp;
72 u8 sink[2];
Ben Skeggs8df1d0c2013-11-04 13:40:36 +100073 int ret;
Ben Skeggs0a0afd22013-02-18 23:17:53 -050074
75 DBG("%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
76
Ben Skeggs0a0afd22013-02-18 23:17:53 -050077 /* set desired link configuration on the source */
78 if ((lnkcmp = dp->info.lnkcmp)) {
79 if (dp->version < 0x30) {
80 while ((dp->link_bw / 10) < nv_ro16(bios, lnkcmp))
81 lnkcmp += 4;
82 init.offset = nv_ro16(bios, lnkcmp + 2);
83 } else {
84 while ((dp->link_bw / 27000) < nv_ro08(bios, lnkcmp))
85 lnkcmp += 3;
86 init.offset = nv_ro16(bios, lnkcmp + 1);
87 }
88
89 nvbios_exec(&init);
90 }
91
Ben Skeggs8df1d0c2013-11-04 13:40:36 +100092 ret = dp->func->lnk_ctl(dp->disp, dp->outp, dp->head,
93 dp->link_nr, dp->link_bw / 27000,
94 dp->dpcd[DPCD_RC02] &
95 DPCD_RC02_ENHANCED_FRAME_CAP);
96 if (ret) {
97 ERR("lnk_ctl failed with %d\n", ret);
98 return ret;
99 }
100
101 /* set desired link configuration on the sink */
102 sink[0] = dp->link_bw / 27000;
103 sink[1] = dp->link_nr;
104 if (dp->dpcd[DPCD_RC02] & DPCD_RC02_ENHANCED_FRAME_CAP)
105 sink[1] |= DPCD_LC01_ENHANCED_FRAME_EN;
106
107 return nv_wraux(dp->aux, DPCD_LC00, sink, 2);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500108}
109
110static void
111dp_set_training_pattern(struct dp_state *dp, u8 pattern)
112{
113 u8 sink_tp;
114
115 DBG("training pattern %d\n", pattern);
116 dp->func->pattern(dp->disp, dp->outp, dp->head, pattern);
117
118 nv_rdaux(dp->aux, DPCD_LC02, &sink_tp, 1);
119 sink_tp &= ~DPCD_LC02_TRAINING_PATTERN_SET;
120 sink_tp |= pattern;
121 nv_wraux(dp->aux, DPCD_LC02, &sink_tp, 1);
122}
123
124static int
125dp_link_train_commit(struct dp_state *dp)
126{
127 int i;
128
129 for (i = 0; i < dp->link_nr; i++) {
130 u8 lane = (dp->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
131 u8 lpre = (lane & 0x0c) >> 2;
132 u8 lvsw = (lane & 0x03) >> 0;
133
134 dp->conf[i] = (lpre << 3) | lvsw;
135 if (lvsw == 3)
136 dp->conf[i] |= DPCD_LC03_MAX_SWING_REACHED;
137 if (lpre == 3)
138 dp->conf[i] |= DPCD_LC03_MAX_PRE_EMPHASIS_REACHED;
139
140 DBG("config lane %d %02x\n", i, dp->conf[i]);
141 dp->func->drv_ctl(dp->disp, dp->outp, dp->head, i, lvsw, lpre);
142 }
143
144 return nv_wraux(dp->aux, DPCD_LC03(0), dp->conf, 4);
145}
146
147static int
148dp_link_train_update(struct dp_state *dp, u32 delay)
149{
150 int ret;
151
Ben Skeggsfb7c2a72014-05-15 21:50:07 +1000152 if (dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL])
153 mdelay(dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL] * 4);
154 else
155 udelay(delay);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500156
157 ret = nv_rdaux(dp->aux, DPCD_LS02, dp->stat, 6);
158 if (ret)
159 return ret;
160
Andy Shevchenko08fcd722013-08-02 14:09:24 +0300161 DBG("status %6ph\n", dp->stat);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500162 return 0;
163}
164
165static int
166dp_link_train_cr(struct dp_state *dp)
167{
168 bool cr_done = false, abort = false;
169 int voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
170 int tries = 0, i;
171
172 dp_set_training_pattern(dp, 1);
173
174 do {
175 if (dp_link_train_commit(dp) ||
176 dp_link_train_update(dp, 100))
177 break;
178
179 cr_done = true;
180 for (i = 0; i < dp->link_nr; i++) {
181 u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
182 if (!(lane & DPCD_LS02_LANE0_CR_DONE)) {
183 cr_done = false;
184 if (dp->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
185 abort = true;
186 break;
187 }
188 }
189
190 if ((dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
191 voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
192 tries = 0;
193 }
194 } while (!cr_done && !abort && ++tries < 5);
195
196 return cr_done ? 0 : -1;
197}
198
199static int
200dp_link_train_eq(struct dp_state *dp)
201{
Ben Skeggsc5bd0282013-04-11 10:12:48 +1000202 bool eq_done = false, cr_done = true;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500203 int tries = 0, i;
204
Ben Skeggs6e8e2682014-05-15 22:00:06 +1000205 if (dp->dpcd[2] & DPCD_RC02_TPS3_SUPPORTED)
206 dp_set_training_pattern(dp, 3);
207 else
208 dp_set_training_pattern(dp, 2);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500209
210 do {
211 if (dp_link_train_update(dp, 400))
212 break;
213
214 eq_done = !!(dp->stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE);
215 for (i = 0; i < dp->link_nr && eq_done; i++) {
216 u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
217 if (!(lane & DPCD_LS02_LANE0_CR_DONE))
218 cr_done = false;
219 if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
220 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
221 eq_done = false;
222 }
223
224 if (dp_link_train_commit(dp))
225 break;
226 } while (!eq_done && cr_done && ++tries <= 5);
227
228 return eq_done ? 0 : -1;
229}
230
231static void
232dp_link_train_init(struct dp_state *dp, bool spread)
233{
234 struct nvbios_init init = {
235 .subdev = nv_subdev(dp->disp),
236 .bios = nouveau_bios(dp->disp),
237 .outp = dp->outp,
238 .crtc = dp->head,
239 .execute = 1,
240 };
241
242 /* set desired spread */
243 if (spread)
244 init.offset = dp->info.script[2];
245 else
246 init.offset = dp->info.script[3];
247 nvbios_exec(&init);
248
249 /* pre-train script */
250 init.offset = dp->info.script[0];
251 nvbios_exec(&init);
252}
253
254static void
255dp_link_train_fini(struct dp_state *dp)
256{
257 struct nvbios_init init = {
258 .subdev = nv_subdev(dp->disp),
259 .bios = nouveau_bios(dp->disp),
260 .outp = dp->outp,
261 .crtc = dp->head,
262 .execute = 1,
263 };
264
265 /* post-train script */
266 init.offset = dp->info.script[1],
267 nvbios_exec(&init);
268}
269
270int
271nouveau_dp_train(struct nouveau_disp *disp, const struct nouveau_dp_func *func,
272 struct dcb_output *outp, int head, u32 datarate)
273{
274 struct nouveau_bios *bios = nouveau_bios(disp);
275 struct nouveau_i2c *i2c = nouveau_i2c(disp);
276 struct dp_state _dp = {
277 .disp = disp,
278 .func = func,
279 .outp = outp,
280 .head = head,
281 }, *dp = &_dp;
Ilia Mirkincbc53c12014-03-19 10:45:55 -0400282 const u32 bw_list[] = { 540000, 270000, 162000, 0 };
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500283 const u32 *link_bw = bw_list;
284 u8 hdr, cnt, len;
285 u32 data;
286 int ret;
287
288 /* find the bios displayport data relevant to this output */
289 data = nvbios_dpout_match(bios, outp->hasht, outp->hashm, &dp->version,
290 &hdr, &cnt, &len, &dp->info);
291 if (!data) {
292 ERR("bios data not found\n");
293 return -EINVAL;
294 }
295
296 /* acquire the aux channel and fetch some info about the display */
297 if (outp->location)
298 dp->aux = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(outp->extdev));
299 else
300 dp->aux = i2c->find(i2c, NV_I2C_TYPE_DCBI2C(outp->i2c_index));
301 if (!dp->aux) {
302 ERR("no aux channel?!\n");
303 return -ENODEV;
304 }
305
306 ret = nv_rdaux(dp->aux, 0x00000, dp->dpcd, sizeof(dp->dpcd));
307 if (ret) {
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000308 /* it's possible the display has been unplugged before we
309 * get here. we still need to execute the full set of
310 * vbios scripts, and program the OR at a high enough
311 * frequency to satisfy the target mode. failure to do
312 * so results at best in an UPDATE hanging, and at worst
313 * with PDISP running away to join the circus.
314 */
315 dp->dpcd[1] = link_bw[0] / 27000;
316 dp->dpcd[2] = 4;
317 dp->dpcd[3] = 0x00;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500318 ERR("failed to read DPCD\n");
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500319 }
320
Ben Skeggsfc243d72014-03-20 09:28:00 +1000321 /* bring capabilities within encoder limits */
Ben Skeggs6e8e2682014-05-15 22:00:06 +1000322 if (nv_oclass(disp)->handle < NV_ENGINE(DISP, 0x90))
323 dp->dpcd[2] &= ~DPCD_RC02_TPS3_SUPPORTED;
Ben Skeggsfc243d72014-03-20 09:28:00 +1000324 if ((dp->dpcd[2] & 0x1f) > dp->outp->dpconf.link_nr) {
Ben Skeggs6e8e2682014-05-15 22:00:06 +1000325 dp->dpcd[2] &= ~DPCD_RC02_MAX_LANE_COUNT;
Ben Skeggsfc243d72014-03-20 09:28:00 +1000326 dp->dpcd[2] |= dp->outp->dpconf.link_nr;
327 }
328 if (dp->dpcd[1] > dp->outp->dpconf.link_bw)
329 dp->dpcd[1] = dp->outp->dpconf.link_bw;
330
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500331 /* adjust required bandwidth for 8B/10B coding overhead */
332 datarate = (datarate / 8) * 10;
333
334 /* enable down-spreading and execute pre-train script from vbios */
335 dp_link_train_init(dp, dp->dpcd[3] & 0x01);
336
337 /* start off at highest link rate supported by encoder and display */
338 while (*link_bw > (dp->dpcd[1] * 27000))
339 link_bw++;
340
Ben Skeggs687d8f62013-11-01 09:36:42 +1000341 while ((ret = -EIO) && link_bw[0]) {
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500342 /* find minimum required lane count at this link rate */
343 dp->link_nr = dp->dpcd[2] & DPCD_RC02_MAX_LANE_COUNT;
344 while ((dp->link_nr >> 1) * link_bw[0] > datarate)
345 dp->link_nr >>= 1;
346
347 /* drop link rate to minimum with this lane count */
348 while ((link_bw[1] * dp->link_nr) > datarate)
349 link_bw++;
350 dp->link_bw = link_bw[0];
351
352 /* program selected link configuration */
353 ret = dp_set_link_config(dp);
354 if (ret == 0) {
355 /* attempt to train the link at this configuration */
356 memset(dp->stat, 0x00, sizeof(dp->stat));
357 if (!dp_link_train_cr(dp) &&
358 !dp_link_train_eq(dp))
359 break;
360 } else
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000361 if (ret) {
362 /* dp_set_link_config() handled training, or
363 * we failed to communicate with the sink.
364 */
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500365 break;
366 }
367
368 /* retry at lower rate */
369 link_bw++;
370 }
371
372 /* finish link training */
373 dp_set_training_pattern(dp, 0);
Ben Skeggs687d8f62013-11-01 09:36:42 +1000374 if (ret < 0)
375 ERR("link training failed\n");
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500376
377 /* execute post-train script from vbios */
378 dp_link_train_fini(dp);
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000379 return (ret < 0) ? false : true;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500380}