blob: 2aedb4e47d0335f9c7b5ffab1876848e5aeb499b [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;
51 u8 dpcd[4];
52 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
152 udelay(delay);
153
154 ret = nv_rdaux(dp->aux, DPCD_LS02, dp->stat, 6);
155 if (ret)
156 return ret;
157
Andy Shevchenko08fcd722013-08-02 14:09:24 +0300158 DBG("status %6ph\n", dp->stat);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500159 return 0;
160}
161
162static int
163dp_link_train_cr(struct dp_state *dp)
164{
165 bool cr_done = false, abort = false;
166 int voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
167 int tries = 0, i;
168
169 dp_set_training_pattern(dp, 1);
170
171 do {
172 if (dp_link_train_commit(dp) ||
173 dp_link_train_update(dp, 100))
174 break;
175
176 cr_done = true;
177 for (i = 0; i < dp->link_nr; i++) {
178 u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
179 if (!(lane & DPCD_LS02_LANE0_CR_DONE)) {
180 cr_done = false;
181 if (dp->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
182 abort = true;
183 break;
184 }
185 }
186
187 if ((dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
188 voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
189 tries = 0;
190 }
191 } while (!cr_done && !abort && ++tries < 5);
192
193 return cr_done ? 0 : -1;
194}
195
196static int
197dp_link_train_eq(struct dp_state *dp)
198{
Ben Skeggsc5bd0282013-04-11 10:12:48 +1000199 bool eq_done = false, cr_done = true;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500200 int tries = 0, i;
201
202 dp_set_training_pattern(dp, 2);
203
204 do {
205 if (dp_link_train_update(dp, 400))
206 break;
207
208 eq_done = !!(dp->stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE);
209 for (i = 0; i < dp->link_nr && eq_done; i++) {
210 u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
211 if (!(lane & DPCD_LS02_LANE0_CR_DONE))
212 cr_done = false;
213 if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
214 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
215 eq_done = false;
216 }
217
218 if (dp_link_train_commit(dp))
219 break;
220 } while (!eq_done && cr_done && ++tries <= 5);
221
222 return eq_done ? 0 : -1;
223}
224
225static void
226dp_link_train_init(struct dp_state *dp, bool spread)
227{
228 struct nvbios_init init = {
229 .subdev = nv_subdev(dp->disp),
230 .bios = nouveau_bios(dp->disp),
231 .outp = dp->outp,
232 .crtc = dp->head,
233 .execute = 1,
234 };
235
236 /* set desired spread */
237 if (spread)
238 init.offset = dp->info.script[2];
239 else
240 init.offset = dp->info.script[3];
241 nvbios_exec(&init);
242
243 /* pre-train script */
244 init.offset = dp->info.script[0];
245 nvbios_exec(&init);
246}
247
248static void
249dp_link_train_fini(struct dp_state *dp)
250{
251 struct nvbios_init init = {
252 .subdev = nv_subdev(dp->disp),
253 .bios = nouveau_bios(dp->disp),
254 .outp = dp->outp,
255 .crtc = dp->head,
256 .execute = 1,
257 };
258
259 /* post-train script */
260 init.offset = dp->info.script[1],
261 nvbios_exec(&init);
262}
263
264int
265nouveau_dp_train(struct nouveau_disp *disp, const struct nouveau_dp_func *func,
266 struct dcb_output *outp, int head, u32 datarate)
267{
268 struct nouveau_bios *bios = nouveau_bios(disp);
269 struct nouveau_i2c *i2c = nouveau_i2c(disp);
270 struct dp_state _dp = {
271 .disp = disp,
272 .func = func,
273 .outp = outp,
274 .head = head,
275 }, *dp = &_dp;
276 const u32 bw_list[] = { 270000, 162000, 0 };
277 const u32 *link_bw = bw_list;
278 u8 hdr, cnt, len;
279 u32 data;
280 int ret;
281
282 /* find the bios displayport data relevant to this output */
283 data = nvbios_dpout_match(bios, outp->hasht, outp->hashm, &dp->version,
284 &hdr, &cnt, &len, &dp->info);
285 if (!data) {
286 ERR("bios data not found\n");
287 return -EINVAL;
288 }
289
290 /* acquire the aux channel and fetch some info about the display */
291 if (outp->location)
292 dp->aux = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(outp->extdev));
293 else
294 dp->aux = i2c->find(i2c, NV_I2C_TYPE_DCBI2C(outp->i2c_index));
295 if (!dp->aux) {
296 ERR("no aux channel?!\n");
297 return -ENODEV;
298 }
299
300 ret = nv_rdaux(dp->aux, 0x00000, dp->dpcd, sizeof(dp->dpcd));
301 if (ret) {
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000302 /* it's possible the display has been unplugged before we
303 * get here. we still need to execute the full set of
304 * vbios scripts, and program the OR at a high enough
305 * frequency to satisfy the target mode. failure to do
306 * so results at best in an UPDATE hanging, and at worst
307 * with PDISP running away to join the circus.
308 */
309 dp->dpcd[1] = link_bw[0] / 27000;
310 dp->dpcd[2] = 4;
311 dp->dpcd[3] = 0x00;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500312 ERR("failed to read DPCD\n");
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500313 }
314
Ben Skeggsfc243d72014-03-20 09:28:00 +1000315 /* bring capabilities within encoder limits */
316 if ((dp->dpcd[2] & 0x1f) > dp->outp->dpconf.link_nr) {
317 dp->dpcd[2] &= ~0x1f;
318 dp->dpcd[2] |= dp->outp->dpconf.link_nr;
319 }
320 if (dp->dpcd[1] > dp->outp->dpconf.link_bw)
321 dp->dpcd[1] = dp->outp->dpconf.link_bw;
322
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500323 /* adjust required bandwidth for 8B/10B coding overhead */
324 datarate = (datarate / 8) * 10;
325
326 /* enable down-spreading and execute pre-train script from vbios */
327 dp_link_train_init(dp, dp->dpcd[3] & 0x01);
328
329 /* start off at highest link rate supported by encoder and display */
330 while (*link_bw > (dp->dpcd[1] * 27000))
331 link_bw++;
332
Ben Skeggs687d8f62013-11-01 09:36:42 +1000333 while ((ret = -EIO) && link_bw[0]) {
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500334 /* find minimum required lane count at this link rate */
335 dp->link_nr = dp->dpcd[2] & DPCD_RC02_MAX_LANE_COUNT;
336 while ((dp->link_nr >> 1) * link_bw[0] > datarate)
337 dp->link_nr >>= 1;
338
339 /* drop link rate to minimum with this lane count */
340 while ((link_bw[1] * dp->link_nr) > datarate)
341 link_bw++;
342 dp->link_bw = link_bw[0];
343
344 /* program selected link configuration */
345 ret = dp_set_link_config(dp);
346 if (ret == 0) {
347 /* attempt to train the link at this configuration */
348 memset(dp->stat, 0x00, sizeof(dp->stat));
349 if (!dp_link_train_cr(dp) &&
350 !dp_link_train_eq(dp))
351 break;
352 } else
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000353 if (ret) {
354 /* dp_set_link_config() handled training, or
355 * we failed to communicate with the sink.
356 */
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500357 break;
358 }
359
360 /* retry at lower rate */
361 link_bw++;
362 }
363
364 /* finish link training */
365 dp_set_training_pattern(dp, 0);
Ben Skeggs687d8f62013-11-01 09:36:42 +1000366 if (ret < 0)
367 ERR("link training failed\n");
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500368
369 /* execute post-train script from vbios */
370 dp_link_train_fini(dp);
Ben Skeggs8df1d0c2013-11-04 13:40:36 +1000371 return (ret < 0) ? false : true;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500372}