blob: 15448b9abac89f49bc5814d74228f9eda95df900 [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];
73
74 DBG("%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
75
76 /* set desired link configuration on the sink */
77 sink[0] = dp->link_bw / 27000;
78 sink[1] = dp->link_nr;
79 if (dp->dpcd[DPCD_RC02] & DPCD_RC02_ENHANCED_FRAME_CAP)
80 sink[1] |= DPCD_LC01_ENHANCED_FRAME_EN;
81
82 nv_wraux(dp->aux, DPCD_LC00, sink, 2);
83
84 /* set desired link configuration on the source */
85 if ((lnkcmp = dp->info.lnkcmp)) {
86 if (dp->version < 0x30) {
87 while ((dp->link_bw / 10) < nv_ro16(bios, lnkcmp))
88 lnkcmp += 4;
89 init.offset = nv_ro16(bios, lnkcmp + 2);
90 } else {
91 while ((dp->link_bw / 27000) < nv_ro08(bios, lnkcmp))
92 lnkcmp += 3;
93 init.offset = nv_ro16(bios, lnkcmp + 1);
94 }
95
96 nvbios_exec(&init);
97 }
98
99 return dp->func->lnk_ctl(dp->disp, dp->outp, dp->head,
100 dp->link_nr, dp->link_bw / 27000,
101 dp->dpcd[DPCD_RC02] &
102 DPCD_RC02_ENHANCED_FRAME_CAP);
103}
104
105static void
106dp_set_training_pattern(struct dp_state *dp, u8 pattern)
107{
108 u8 sink_tp;
109
110 DBG("training pattern %d\n", pattern);
111 dp->func->pattern(dp->disp, dp->outp, dp->head, pattern);
112
113 nv_rdaux(dp->aux, DPCD_LC02, &sink_tp, 1);
114 sink_tp &= ~DPCD_LC02_TRAINING_PATTERN_SET;
115 sink_tp |= pattern;
116 nv_wraux(dp->aux, DPCD_LC02, &sink_tp, 1);
117}
118
119static int
120dp_link_train_commit(struct dp_state *dp)
121{
122 int i;
123
124 for (i = 0; i < dp->link_nr; i++) {
125 u8 lane = (dp->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
126 u8 lpre = (lane & 0x0c) >> 2;
127 u8 lvsw = (lane & 0x03) >> 0;
128
129 dp->conf[i] = (lpre << 3) | lvsw;
130 if (lvsw == 3)
131 dp->conf[i] |= DPCD_LC03_MAX_SWING_REACHED;
132 if (lpre == 3)
133 dp->conf[i] |= DPCD_LC03_MAX_PRE_EMPHASIS_REACHED;
134
135 DBG("config lane %d %02x\n", i, dp->conf[i]);
136 dp->func->drv_ctl(dp->disp, dp->outp, dp->head, i, lvsw, lpre);
137 }
138
139 return nv_wraux(dp->aux, DPCD_LC03(0), dp->conf, 4);
140}
141
142static int
143dp_link_train_update(struct dp_state *dp, u32 delay)
144{
145 int ret;
146
147 udelay(delay);
148
149 ret = nv_rdaux(dp->aux, DPCD_LS02, dp->stat, 6);
150 if (ret)
151 return ret;
152
Andy Shevchenko08fcd722013-08-02 14:09:24 +0300153 DBG("status %6ph\n", dp->stat);
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500154 return 0;
155}
156
157static int
158dp_link_train_cr(struct dp_state *dp)
159{
160 bool cr_done = false, abort = false;
161 int voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
162 int tries = 0, i;
163
164 dp_set_training_pattern(dp, 1);
165
166 do {
167 if (dp_link_train_commit(dp) ||
168 dp_link_train_update(dp, 100))
169 break;
170
171 cr_done = true;
172 for (i = 0; i < dp->link_nr; i++) {
173 u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
174 if (!(lane & DPCD_LS02_LANE0_CR_DONE)) {
175 cr_done = false;
176 if (dp->conf[i] & DPCD_LC03_MAX_SWING_REACHED)
177 abort = true;
178 break;
179 }
180 }
181
182 if ((dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET) != voltage) {
183 voltage = dp->conf[0] & DPCD_LC03_VOLTAGE_SWING_SET;
184 tries = 0;
185 }
186 } while (!cr_done && !abort && ++tries < 5);
187
188 return cr_done ? 0 : -1;
189}
190
191static int
192dp_link_train_eq(struct dp_state *dp)
193{
Ben Skeggsc5bd0282013-04-11 10:12:48 +1000194 bool eq_done = false, cr_done = true;
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500195 int tries = 0, i;
196
197 dp_set_training_pattern(dp, 2);
198
199 do {
200 if (dp_link_train_update(dp, 400))
201 break;
202
203 eq_done = !!(dp->stat[2] & DPCD_LS04_INTERLANE_ALIGN_DONE);
204 for (i = 0; i < dp->link_nr && eq_done; i++) {
205 u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
206 if (!(lane & DPCD_LS02_LANE0_CR_DONE))
207 cr_done = false;
208 if (!(lane & DPCD_LS02_LANE0_CHANNEL_EQ_DONE) ||
209 !(lane & DPCD_LS02_LANE0_SYMBOL_LOCKED))
210 eq_done = false;
211 }
212
213 if (dp_link_train_commit(dp))
214 break;
215 } while (!eq_done && cr_done && ++tries <= 5);
216
217 return eq_done ? 0 : -1;
218}
219
220static void
221dp_link_train_init(struct dp_state *dp, bool spread)
222{
223 struct nvbios_init init = {
224 .subdev = nv_subdev(dp->disp),
225 .bios = nouveau_bios(dp->disp),
226 .outp = dp->outp,
227 .crtc = dp->head,
228 .execute = 1,
229 };
230
231 /* set desired spread */
232 if (spread)
233 init.offset = dp->info.script[2];
234 else
235 init.offset = dp->info.script[3];
236 nvbios_exec(&init);
237
238 /* pre-train script */
239 init.offset = dp->info.script[0];
240 nvbios_exec(&init);
241}
242
243static void
244dp_link_train_fini(struct dp_state *dp)
245{
246 struct nvbios_init init = {
247 .subdev = nv_subdev(dp->disp),
248 .bios = nouveau_bios(dp->disp),
249 .outp = dp->outp,
250 .crtc = dp->head,
251 .execute = 1,
252 };
253
254 /* post-train script */
255 init.offset = dp->info.script[1],
256 nvbios_exec(&init);
257}
258
259int
260nouveau_dp_train(struct nouveau_disp *disp, const struct nouveau_dp_func *func,
261 struct dcb_output *outp, int head, u32 datarate)
262{
263 struct nouveau_bios *bios = nouveau_bios(disp);
264 struct nouveau_i2c *i2c = nouveau_i2c(disp);
265 struct dp_state _dp = {
266 .disp = disp,
267 .func = func,
268 .outp = outp,
269 .head = head,
270 }, *dp = &_dp;
271 const u32 bw_list[] = { 270000, 162000, 0 };
272 const u32 *link_bw = bw_list;
273 u8 hdr, cnt, len;
274 u32 data;
275 int ret;
276
277 /* find the bios displayport data relevant to this output */
278 data = nvbios_dpout_match(bios, outp->hasht, outp->hashm, &dp->version,
279 &hdr, &cnt, &len, &dp->info);
280 if (!data) {
281 ERR("bios data not found\n");
282 return -EINVAL;
283 }
284
285 /* acquire the aux channel and fetch some info about the display */
286 if (outp->location)
287 dp->aux = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(outp->extdev));
288 else
289 dp->aux = i2c->find(i2c, NV_I2C_TYPE_DCBI2C(outp->i2c_index));
290 if (!dp->aux) {
291 ERR("no aux channel?!\n");
292 return -ENODEV;
293 }
294
295 ret = nv_rdaux(dp->aux, 0x00000, dp->dpcd, sizeof(dp->dpcd));
296 if (ret) {
297 ERR("failed to read DPCD\n");
298 return ret;
299 }
300
301 /* adjust required bandwidth for 8B/10B coding overhead */
302 datarate = (datarate / 8) * 10;
303
304 /* enable down-spreading and execute pre-train script from vbios */
305 dp_link_train_init(dp, dp->dpcd[3] & 0x01);
306
307 /* start off at highest link rate supported by encoder and display */
308 while (*link_bw > (dp->dpcd[1] * 27000))
309 link_bw++;
310
Ben Skeggs687d8f62013-11-01 09:36:42 +1000311 while ((ret = -EIO) && link_bw[0]) {
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500312 /* find minimum required lane count at this link rate */
313 dp->link_nr = dp->dpcd[2] & DPCD_RC02_MAX_LANE_COUNT;
314 while ((dp->link_nr >> 1) * link_bw[0] > datarate)
315 dp->link_nr >>= 1;
316
317 /* drop link rate to minimum with this lane count */
318 while ((link_bw[1] * dp->link_nr) > datarate)
319 link_bw++;
320 dp->link_bw = link_bw[0];
321
322 /* program selected link configuration */
323 ret = dp_set_link_config(dp);
324 if (ret == 0) {
325 /* attempt to train the link at this configuration */
326 memset(dp->stat, 0x00, sizeof(dp->stat));
327 if (!dp_link_train_cr(dp) &&
328 !dp_link_train_eq(dp))
329 break;
330 } else
331 if (ret >= 1) {
332 /* dp_set_link_config() handled training */
333 break;
334 }
335
336 /* retry at lower rate */
337 link_bw++;
338 }
339
340 /* finish link training */
341 dp_set_training_pattern(dp, 0);
Ben Skeggs687d8f62013-11-01 09:36:42 +1000342 if (ret < 0)
343 ERR("link training failed\n");
Ben Skeggs0a0afd22013-02-18 23:17:53 -0500344
345 /* execute post-train script from vbios */
346 dp_link_train_fini(dp);
347 return true;
348}