blob: 3814c3d33812797afdcc2f7de09a67dcbe32a855 [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
Ben Skeggs04e7e922014-05-15 22:20:40 +100033#include <core/class.h>
34
Ben Skeggs0a0afd22013-02-18 23:17:53 -050035#include "dport.h"
36
37#define DBG(fmt, args...) nv_debug(dp->disp, "DP:%04x:%04x: " fmt, \
38 dp->outp->hasht, dp->outp->hashm, ##args)
39#define ERR(fmt, args...) nv_error(dp->disp, "DP:%04x:%04x: " fmt, \
40 dp->outp->hasht, dp->outp->hashm, ##args)
41
42/******************************************************************************
43 * link training
44 *****************************************************************************/
45struct dp_state {
46 const struct nouveau_dp_func *func;
47 struct nouveau_disp *disp;
48 struct dcb_output *outp;
49 struct nvbios_dpout info;
50 u8 version;
51 struct nouveau_i2c_port *aux;
52 int head;
Ben Skeggsfb7c2a72014-05-15 21:50:07 +100053 u8 dpcd[16];
Ben Skeggs0a0afd22013-02-18 23:17:53 -050054 int link_nr;
55 u32 link_bw;
56 u8 stat[6];
57 u8 conf[4];
Ben Skeggs04e7e922014-05-15 22:20:40 +100058 bool pc2;
59 u8 pc2stat;
60 u8 pc2conf[2];
Ben Skeggs0a0afd22013-02-18 23:17:53 -050061};
62
63static int
64dp_set_link_config(struct dp_state *dp)
65{
66 struct nouveau_disp *disp = dp->disp;
67 struct nouveau_bios *bios = nouveau_bios(disp);
68 struct nvbios_init init = {
69 .subdev = nv_subdev(dp->disp),
70 .bios = bios,
71 .offset = 0x0000,
72 .outp = dp->outp,
73 .crtc = dp->head,
74 .execute = 1,
75 };
76 u32 lnkcmp;
77 u8 sink[2];
Ben Skeggs8df1d0c2013-11-04 13:40:36 +100078 int ret;
Ben Skeggs0a0afd22013-02-18 23:17:53 -050079
80 DBG("%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
81
Ben Skeggs0a0afd22013-02-18 23:17:53 -050082 /* set desired link configuration on the source */
83 if ((lnkcmp = dp->info.lnkcmp)) {
84 if (dp->version < 0x30) {
85 while ((dp->link_bw / 10) < nv_ro16(bios, lnkcmp))
86 lnkcmp += 4;
87 init.offset = nv_ro16(bios, lnkcmp + 2);
88 } else {
89 while ((dp->link_bw / 27000) < nv_ro08(bios, lnkcmp))
90 lnkcmp += 3;
91 init.offset = nv_ro16(bios, lnkcmp + 1);
92 }
93
94 nvbios_exec(&init);
95 }
96
Ben Skeggs8df1d0c2013-11-04 13:40:36 +100097 ret = dp->func->lnk_ctl(dp->disp, dp->outp, dp->head,
98 dp->link_nr, dp->link_bw / 27000,
99 dp->dpcd[DPCD_RC02] &
100 DPCD_RC02_ENHANCED_FRAME_CAP);
101 if (ret) {
102 ERR("lnk_ctl failed with %d\n", ret);
103 return ret;
104 }
105
106 /* set desired link configuration on the sink */
107 sink[0] = dp->link_bw / 27000;
108 sink[1] = dp->link_nr;
109 if (dp->dpcd[DPCD_RC02] & DPCD_RC02_ENHANCED_FRAME_CAP)
110 sink[1] |= DPCD_LC01_ENHANCED_FRAME_EN;
111
112 return nv_wraux(dp->aux, DPCD_LC00, sink, 2);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500113}
114
115static void
116dp_set_training_pattern(struct dp_state *dp, u8 pattern)
117{
118 u8 sink_tp;
119
120 DBG("training pattern %d\n", pattern);
121 dp->func->pattern(dp->disp, dp->outp, dp->head, pattern);
122
123 nv_rdaux(dp->aux, DPCD_LC02, &sink_tp, 1);
124 sink_tp &= ~DPCD_LC02_TRAINING_PATTERN_SET;
125 sink_tp |= pattern;
126 nv_wraux(dp->aux, DPCD_LC02, &sink_tp, 1);
127}
128
129static int
Ben Skeggs04e7e922014-05-15 22:20:40 +1000130dp_link_train_commit(struct dp_state *dp, bool pc)
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500131{
Ben Skeggs04e7e922014-05-15 22:20:40 +1000132 const struct nouveau_dp_func *func = dp->func;
133 struct nouveau_disp *disp = dp->disp;
134 int ret, i;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500135
136 for (i = 0; i < dp->link_nr; i++) {
137 u8 lane = (dp->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
138 u8 lpre = (lane & 0x0c) >> 2;
139 u8 lvsw = (lane & 0x03) >> 0;
140
141 dp->conf[i] = (lpre << 3) | lvsw;
142 if (lvsw == 3)
143 dp->conf[i] |= DPCD_LC03_MAX_SWING_REACHED;
144 if (lpre == 3)
145 dp->conf[i] |= DPCD_LC03_MAX_PRE_EMPHASIS_REACHED;
Ben Skeggs04e7e922014-05-15 22:20:40 +1000146 dp->pc2conf[i >> 1] |= 4 << ((i & 1) * 4);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500147
148 DBG("config lane %d %02x\n", i, dp->conf[i]);
Ben Skeggs04e7e922014-05-15 22:20:40 +1000149 func->drv_ctl(disp, dp->outp, dp->head, i, lvsw, lpre);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500150 }
151
Ben Skeggs04e7e922014-05-15 22:20:40 +1000152 ret = nv_wraux(dp->aux, DPCD_LC03(0), dp->conf, 4);
153 if (ret)
154 return ret;
155
156 if (pc) {
157 ret = nv_wraux(dp->aux, DPCD_LC0F, dp->pc2conf, 2);
158 if (ret)
159 return ret;
160 }
161
162 return 0;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500163}
164
165static int
Ben Skeggs04e7e922014-05-15 22:20:40 +1000166dp_link_train_update(struct dp_state *dp, bool pc, u32 delay)
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500167{
168 int ret;
169
Ben Skeggsfb7c2a72014-05-15 21:50:07 +1000170 if (dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL])
171 mdelay(dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL] * 4);
172 else
173 udelay(delay);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500174
175 ret = nv_rdaux(dp->aux, DPCD_LS02, dp->stat, 6);
176 if (ret)
177 return ret;
178
Ben Skeggs04e7e922014-05-15 22:20:40 +1000179 if (pc) {
180 ret = nv_rdaux(dp->aux, DPCD_LS0C, &dp->pc2stat, 1);
181 if (ret)
182 dp->pc2stat = 0x00;
183 DBG("status %6ph pc2 %02x\n", dp->stat, dp->pc2stat);
184 } else {
185 DBG("status %6ph\n", dp->stat);
186 }
187
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500188 return 0;
189}
190
191static int
192dp_link_train_cr(struct dp_state *dp)
193{
194 bool cr_done = false, abort = false;
195 int voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
196 int tries = 0, i;
197
198 dp_set_training_pattern(dp, 1);
199
200 do {
Ben Skeggs04e7e922014-05-15 22:20:40 +1000201 if (dp_link_train_commit(dp, false) ||
202 dp_link_train_update(dp, false, 100))
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500203 break;
204
205 cr_done = true;
206 for (i = 0; i < dp->link_nr; i++) {
207 u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
208 if (!(lane & DPCD_LS02_LANE0_CR_DONE)) {
209 cr_done = false;
210 if (dp->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
211 abort = true;
212 break;
213 }
214 }
215
216 if ((dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
217 voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
218 tries = 0;
219 }
220 } while (!cr_done && !abort && ++tries < 5);
221
222 return cr_done ? 0 : -1;
223}
224
225static int
226dp_link_train_eq(struct dp_state *dp)
227{
Ben Skeggsc5bd0282013-04-11 10:12:48 +1000228 bool eq_done = false, cr_done = true;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500229 int tries = 0, i;
230
Ben Skeggs6e8e2682014-05-15 22:00:06 +1000231 if (dp->dpcd[2] & DPCD_RC02_TPS3_SUPPORTED)
232 dp_set_training_pattern(dp, 3);
233 else
234 dp_set_training_pattern(dp, 2);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500235
236 do {
Ben Skeggs04e7e922014-05-15 22:20:40 +1000237 if (dp_link_train_update(dp, dp->pc2, 400))
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500238 break;
239
240 eq_done = !!(dp->stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE);
241 for (i = 0; i < dp->link_nr && eq_done; i++) {
242 u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
243 if (!(lane & DPCD_LS02_LANE0_CR_DONE))
244 cr_done = false;
245 if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
246 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
247 eq_done = false;
248 }
249
Ben Skeggs04e7e922014-05-15 22:20:40 +1000250 if (dp_link_train_commit(dp, dp->pc2))
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500251 break;
252 } while (!eq_done && cr_done && ++tries <= 5);
253
254 return eq_done ? 0 : -1;
255}
256
257static void
258dp_link_train_init(struct dp_state *dp, bool spread)
259{
260 struct nvbios_init init = {
261 .subdev = nv_subdev(dp->disp),
262 .bios = nouveau_bios(dp->disp),
263 .outp = dp->outp,
264 .crtc = dp->head,
265 .execute = 1,
266 };
267
268 /* set desired spread */
269 if (spread)
270 init.offset = dp->info.script[2];
271 else
272 init.offset = dp->info.script[3];
273 nvbios_exec(&init);
274
275 /* pre-train script */
276 init.offset = dp->info.script[0];
277 nvbios_exec(&init);
278}
279
280static void
281dp_link_train_fini(struct dp_state *dp)
282{
283 struct nvbios_init init = {
284 .subdev = nv_subdev(dp->disp),
285 .bios = nouveau_bios(dp->disp),
286 .outp = dp->outp,
287 .crtc = dp->head,
288 .execute = 1,
289 };
290
291 /* post-train script */
292 init.offset = dp->info.script[1],
293 nvbios_exec(&init);
294}
295
Ben Skeggs1f86ca12014-05-16 10:49:28 +1000296static const struct dp_rates {
297 u32 rate;
298 u8 bw;
299 u8 nr;
300} nouveau_dp_rates[] = {
301 { 2160000, 0x14, 4 },
302 { 1080000, 0x0a, 4 },
303 { 1080000, 0x14, 2 },
304 { 648000, 0x06, 4 },
305 { 540000, 0x0a, 2 },
306 { 540000, 0x14, 1 },
307 { 324000, 0x06, 2 },
308 { 270000, 0x0a, 1 },
309 { 162000, 0x06, 1 },
310 {}
311};
312
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500313int
314nouveau_dp_train(struct nouveau_disp *disp, const struct nouveau_dp_func *func,
315 struct dcb_output *outp, int head, u32 datarate)
316{
317 struct nouveau_bios *bios = nouveau_bios(disp);
318 struct nouveau_i2c *i2c = nouveau_i2c(disp);
Ben Skeggs1f86ca12014-05-16 10:49:28 +1000319 const struct dp_rates *cfg = nouveau_dp_rates;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500320 struct dp_state _dp = {
321 .disp = disp,
322 .func = func,
323 .outp = outp,
324 .head = head,
325 }, *dp = &_dp;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500326 u8 hdr, cnt, len;
327 u32 data;
328 int ret;
329
330 /* find the bios displayport data relevant to this output */
331 data = nvbios_dpout_match(bios, outp->hasht, outp->hashm, &dp->version,
332 &hdr, &cnt, &len, &dp->info);
333 if (!data) {
334 ERR("bios data not found\n");
335 return -EINVAL;
336 }
337
338 /* acquire the aux channel and fetch some info about the display */
339 if (outp->location)
340 dp->aux = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(outp->extdev));
341 else
342 dp->aux = i2c->find(i2c, NV_I2C_TYPE_DCBI2C(outp->i2c_index));
343 if (!dp->aux) {
344 ERR("no aux channel?!\n");
345 return -ENODEV;
346 }
347
348 ret = nv_rdaux(dp->aux, 0x00000, dp->dpcd, sizeof(dp->dpcd));
349 if (ret) {
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000350 /* it's possible the display has been unplugged before we
351 * get here. we still need to execute the full set of
352 * vbios scripts, and program the OR at a high enough
353 * frequency to satisfy the target mode. failure to do
354 * so results at best in an UPDATE hanging, and at worst
355 * with PDISP running away to join the circus.
356 */
Ben Skeggs1f86ca12014-05-16 10:49:28 +1000357 dp->dpcd[1] = dp->outp->dpconf.link_bw;
358 dp->dpcd[2] = dp->outp->dpconf.link_nr;
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000359 dp->dpcd[3] = 0x00;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500360 ERR("failed to read DPCD\n");
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500361 }
362
Ben Skeggsfc243d72014-03-20 09:28:00 +1000363 /* bring capabilities within encoder limits */
Ben Skeggs04e7e922014-05-15 22:20:40 +1000364 if (nv_mclass(disp) < NVD0_DISP_CLASS)
Ben Skeggs6e8e2682014-05-15 22:00:06 +1000365 dp->dpcd[2] &= ~DPCD_RC02_TPS3_SUPPORTED;
Ben Skeggsfc243d72014-03-20 09:28:00 +1000366 if ((dp->dpcd[2] & 0x1f) > dp->outp->dpconf.link_nr) {
Ben Skeggs6e8e2682014-05-15 22:00:06 +1000367 dp->dpcd[2] &= ~DPCD_RC02_MAX_LANE_COUNT;
Ben Skeggsfc243d72014-03-20 09:28:00 +1000368 dp->dpcd[2] |= dp->outp->dpconf.link_nr;
369 }
370 if (dp->dpcd[1] > dp->outp->dpconf.link_bw)
371 dp->dpcd[1] = dp->outp->dpconf.link_bw;
Ben Skeggs04e7e922014-05-15 22:20:40 +1000372 dp->pc2 = dp->dpcd[2] & DPCD_RC02_TPS3_SUPPORTED;
Ben Skeggsfc243d72014-03-20 09:28:00 +1000373
Ben Skeggs1f86ca12014-05-16 10:49:28 +1000374 /* restrict link config to the lowest required rate, if requested */
375 if (datarate) {
376 datarate = (datarate / 8) * 10; /* 8B/10B coding overhead */
377 while (cfg[1].rate >= datarate)
378 cfg++;
379 }
380 cfg--;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500381
382 /* enable down-spreading and execute pre-train script from vbios */
383 dp_link_train_init(dp, dp->dpcd[3] & 0x01);
384
Ben Skeggs1f86ca12014-05-16 10:49:28 +1000385 while (ret = -EIO, (++cfg)->rate) {
386 /* select next configuration supported by encoder and sink */
387 while (cfg->nr > (dp->dpcd[2] & DPCD_RC02_MAX_LANE_COUNT) ||
388 cfg->bw > (dp->dpcd[DPCD_RC01_MAX_LINK_RATE]))
389 cfg++;
390 dp->link_bw = cfg->bw * 27000;
391 dp->link_nr = cfg->nr;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500392
393 /* program selected link configuration */
394 ret = dp_set_link_config(dp);
395 if (ret == 0) {
396 /* attempt to train the link at this configuration */
397 memset(dp->stat, 0x00, sizeof(dp->stat));
398 if (!dp_link_train_cr(dp) &&
399 !dp_link_train_eq(dp))
400 break;
401 } else
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000402 if (ret) {
403 /* dp_set_link_config() handled training, or
404 * we failed to communicate with the sink.
405 */
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500406 break;
407 }
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500408 }
409
410 /* finish link training */
411 dp_set_training_pattern(dp, 0);
Ben Skeggs687d8f62013-11-01 09:36:42 +1000412 if (ret < 0)
413 ERR("link training failed\n");
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500414
415 /* execute post-train script from vbios */
416 dp_link_train_fini(dp);
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000417 return (ret < 0) ? false : true;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500418}