2 * Copyright 2013 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
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>
31 #include <engine/disp.h>
33 #include <core/class.h>
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)
42 /******************************************************************************
44 *****************************************************************************/
46 const struct nouveau_dp_func *func;
47 struct nouveau_disp *disp;
48 struct dcb_output *outp;
49 struct nvbios_dpout info;
51 struct nouveau_i2c_port *aux;
64 dp_set_link_config(struct dp_state *dp)
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),
80 DBG("%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
82 /* 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))
87 init.offset = nv_ro16(bios, lnkcmp + 2);
89 while ((dp->link_bw / 27000) < nv_ro08(bios, lnkcmp))
91 init.offset = nv_ro16(bios, lnkcmp + 1);
97 ret = dp->func->lnk_ctl(dp->disp, dp->outp, dp->head,
98 dp->link_nr, dp->link_bw / 27000,
100 DPCD_RC02_ENHANCED_FRAME_CAP);
102 ERR("lnk_ctl failed with %d\n", ret);
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;
112 return nv_wraux(dp->aux, DPCD_LC00, sink, 2);
116 dp_set_training_pattern(struct dp_state *dp, u8 pattern)
120 DBG("training pattern %d\n", pattern);
121 dp->func->pattern(dp->disp, dp->outp, dp->head, pattern);
123 nv_rdaux(dp->aux, DPCD_LC02, &sink_tp, 1);
124 sink_tp &= ~DPCD_LC02_TRAINING_PATTERN_SET;
126 nv_wraux(dp->aux, DPCD_LC02, &sink_tp, 1);
130 dp_link_train_commit(struct dp_state *dp, bool pc)
132 const struct nouveau_dp_func *func = dp->func;
133 struct nouveau_disp *disp = dp->disp;
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;
141 dp->conf[i] = (lpre << 3) | lvsw;
143 dp->conf[i] |= DPCD_LC03_MAX_SWING_REACHED;
145 dp->conf[i] |= DPCD_LC03_MAX_PRE_EMPHASIS_REACHED;
146 dp->pc2conf[i >> 1] |= 4 << ((i & 1) * 4);
148 DBG("config lane %d %02x\n", i, dp->conf[i]);
149 func->drv_ctl(disp, dp->outp, dp->head, i, lvsw, lpre);
152 ret = nv_wraux(dp->aux, DPCD_LC03(0), dp->conf, 4);
157 ret = nv_wraux(dp->aux, DPCD_LC0F, dp->pc2conf, 2);
166 dp_link_train_update(struct dp_state *dp, bool pc, u32 delay)
170 if (dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL])
171 mdelay(dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL] * 4);
175 ret = nv_rdaux(dp->aux, DPCD_LS02, dp->stat, 6);
180 ret = nv_rdaux(dp->aux, DPCD_LS0C, &dp->pc2stat, 1);
183 DBG("status %6ph pc2 %02x\n", dp->stat, dp->pc2stat);
185 DBG("status %6ph\n", dp->stat);
192 dp_link_train_cr(struct dp_state *dp)
194 bool cr_done = false, abort = false;
195 int voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
198 dp_set_training_pattern(dp, 1);
201 if (dp_link_train_commit(dp, false) ||
202 dp_link_train_update(dp, false, 100))
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)) {
210 if (dp->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
216 if ((dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
217 voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
220 } while (!cr_done && !abort && ++tries < 5);
222 return cr_done ? 0 : -1;
226 dp_link_train_eq(struct dp_state *dp)
228 bool eq_done = false, cr_done = true;
231 if (dp->dpcd[2] & DPCD_RC02_TPS3_SUPPORTED)
232 dp_set_training_pattern(dp, 3);
234 dp_set_training_pattern(dp, 2);
237 if (dp_link_train_update(dp, dp->pc2, 400))
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))
245 if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
246 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
250 if (dp_link_train_commit(dp, dp->pc2))
252 } while (!eq_done && cr_done && ++tries <= 5);
254 return eq_done ? 0 : -1;
258 dp_link_train_init(struct dp_state *dp, bool spread)
260 struct nvbios_init init = {
261 .subdev = nv_subdev(dp->disp),
262 .bios = nouveau_bios(dp->disp),
268 /* set desired spread */
270 init.offset = dp->info.script[2];
272 init.offset = dp->info.script[3];
275 /* pre-train script */
276 init.offset = dp->info.script[0];
281 dp_link_train_fini(struct dp_state *dp)
283 struct nvbios_init init = {
284 .subdev = nv_subdev(dp->disp),
285 .bios = nouveau_bios(dp->disp),
291 /* post-train script */
292 init.offset = dp->info.script[1],
297 nouveau_dp_train(struct nouveau_disp *disp, const struct nouveau_dp_func *func,
298 struct dcb_output *outp, int head, u32 datarate)
300 struct nouveau_bios *bios = nouveau_bios(disp);
301 struct nouveau_i2c *i2c = nouveau_i2c(disp);
302 struct dp_state _dp = {
308 const u32 bw_list[] = { 540000, 270000, 162000, 0 };
309 const u32 *link_bw = bw_list;
314 /* find the bios displayport data relevant to this output */
315 data = nvbios_dpout_match(bios, outp->hasht, outp->hashm, &dp->version,
316 &hdr, &cnt, &len, &dp->info);
318 ERR("bios data not found\n");
322 /* acquire the aux channel and fetch some info about the display */
324 dp->aux = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(outp->extdev));
326 dp->aux = i2c->find(i2c, NV_I2C_TYPE_DCBI2C(outp->i2c_index));
328 ERR("no aux channel?!\n");
332 ret = nv_rdaux(dp->aux, 0x00000, dp->dpcd, sizeof(dp->dpcd));
334 /* it's possible the display has been unplugged before we
335 * get here. we still need to execute the full set of
336 * vbios scripts, and program the OR at a high enough
337 * frequency to satisfy the target mode. failure to do
338 * so results at best in an UPDATE hanging, and at worst
339 * with PDISP running away to join the circus.
341 dp->dpcd[1] = link_bw[0] / 27000;
344 ERR("failed to read DPCD\n");
347 /* bring capabilities within encoder limits */
348 if (nv_mclass(disp) < NVD0_DISP_CLASS)
349 dp->dpcd[2] &= ~DPCD_RC02_TPS3_SUPPORTED;
350 if ((dp->dpcd[2] & 0x1f) > dp->outp->dpconf.link_nr) {
351 dp->dpcd[2] &= ~DPCD_RC02_MAX_LANE_COUNT;
352 dp->dpcd[2] |= dp->outp->dpconf.link_nr;
354 if (dp->dpcd[1] > dp->outp->dpconf.link_bw)
355 dp->dpcd[1] = dp->outp->dpconf.link_bw;
356 dp->pc2 = dp->dpcd[2] & DPCD_RC02_TPS3_SUPPORTED;
358 /* adjust required bandwidth for 8B/10B coding overhead */
359 datarate = (datarate / 8) * 10;
361 /* enable down-spreading and execute pre-train script from vbios */
362 dp_link_train_init(dp, dp->dpcd[3] & 0x01);
364 /* start off at highest link rate supported by encoder and display */
365 while (*link_bw > (dp->dpcd[1] * 27000))
368 while ((ret = -EIO) && link_bw[0]) {
369 /* find minimum required lane count at this link rate */
370 dp->link_nr = dp->dpcd[2] & DPCD_RC02_MAX_LANE_COUNT;
371 while ((dp->link_nr >> 1) * link_bw[0] > datarate)
374 /* drop link rate to minimum with this lane count */
375 while ((link_bw[1] * dp->link_nr) > datarate)
377 dp->link_bw = link_bw[0];
379 /* program selected link configuration */
380 ret = dp_set_link_config(dp);
382 /* attempt to train the link at this configuration */
383 memset(dp->stat, 0x00, sizeof(dp->stat));
384 if (!dp_link_train_cr(dp) &&
385 !dp_link_train_eq(dp))
389 /* dp_set_link_config() handled training, or
390 * we failed to communicate with the sink.
395 /* retry at lower rate */
399 /* finish link training */
400 dp_set_training_pattern(dp, 0);
402 ERR("link training failed\n");
404 /* execute post-train script from vbios */
405 dp_link_train_fini(dp);
406 return (ret < 0) ? false : true;