]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/core/engine/disp/dport.c
drm/nouveau/disp/dp: support postcursor in link training
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / core / engine / disp / dport.c
1 /*
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 <core/class.h>
34
35 #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  *****************************************************************************/
45 struct 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;
53         u8  dpcd[16];
54         int link_nr;
55         u32 link_bw;
56         u8  stat[6];
57         u8  conf[4];
58         bool pc2;
59         u8  pc2stat;
60         u8  pc2conf[2];
61 };
62
63 static int
64 dp_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];
78         int ret;
79
80         DBG("%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
81
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))
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
97         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);
113 }
114
115 static void
116 dp_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
129 static int
130 dp_link_train_commit(struct dp_state *dp, bool pc)
131 {
132         const struct nouveau_dp_func *func = dp->func;
133         struct nouveau_disp *disp = dp->disp;
134         int ret, i;
135
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;
146                 dp->pc2conf[i >> 1] |= 4 << ((i & 1) * 4);
147
148                 DBG("config lane %d %02x\n", i, dp->conf[i]);
149                 func->drv_ctl(disp, dp->outp, dp->head, i, lvsw, lpre);
150         }
151
152         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;
163 }
164
165 static int
166 dp_link_train_update(struct dp_state *dp, bool pc, u32 delay)
167 {
168         int ret;
169
170         if (dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL])
171                 mdelay(dp->dpcd[DPCD_RC0E_AUX_RD_INTERVAL] * 4);
172         else
173                 udelay(delay);
174
175         ret = nv_rdaux(dp->aux, DPCD_LS02, dp->stat, 6);
176         if (ret)
177                 return ret;
178
179         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
188         return 0;
189 }
190
191 static int
192 dp_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 {
201                 if (dp_link_train_commit(dp, false) ||
202                     dp_link_train_update(dp, false, 100))
203                         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
225 static int
226 dp_link_train_eq(struct dp_state *dp)
227 {
228         bool eq_done = false, cr_done = true;
229         int tries = 0, i;
230
231         if (dp->dpcd[2] & DPCD_RC02_TPS3_SUPPORTED)
232                 dp_set_training_pattern(dp, 3);
233         else
234                 dp_set_training_pattern(dp, 2);
235
236         do {
237                 if (dp_link_train_update(dp, dp->pc2, 400))
238                         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
250                 if (dp_link_train_commit(dp, dp->pc2))
251                         break;
252         } while (!eq_done && cr_done && ++tries <= 5);
253
254         return eq_done ? 0 : -1;
255 }
256
257 static void
258 dp_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
280 static void
281 dp_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
296 int
297 nouveau_dp_train(struct nouveau_disp *disp, const struct nouveau_dp_func *func,
298                  struct dcb_output *outp, int head, u32 datarate)
299 {
300         struct nouveau_bios *bios = nouveau_bios(disp);
301         struct nouveau_i2c *i2c = nouveau_i2c(disp);
302         struct dp_state _dp = {
303                 .disp = disp,
304                 .func = func,
305                 .outp = outp,
306                 .head = head,
307         }, *dp = &_dp;
308         const u32 bw_list[] = { 540000, 270000, 162000, 0 };
309         const u32 *link_bw = bw_list;
310         u8  hdr, cnt, len;
311         u32 data;
312         int ret;
313
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);
317         if (!data) {
318                 ERR("bios data not found\n");
319                 return -EINVAL;
320         }
321
322         /* acquire the aux channel and fetch some info about the display */
323         if (outp->location)
324                 dp->aux = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(outp->extdev));
325         else
326                 dp->aux = i2c->find(i2c, NV_I2C_TYPE_DCBI2C(outp->i2c_index));
327         if (!dp->aux) {
328                 ERR("no aux channel?!\n");
329                 return -ENODEV;
330         }
331
332         ret = nv_rdaux(dp->aux, 0x00000, dp->dpcd, sizeof(dp->dpcd));
333         if (ret) {
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.
340                  */
341                 dp->dpcd[1] = link_bw[0] / 27000;
342                 dp->dpcd[2] = 4;
343                 dp->dpcd[3] = 0x00;
344                 ERR("failed to read DPCD\n");
345         }
346
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;
353         }
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;
357
358         /* adjust required bandwidth for 8B/10B coding overhead */
359         datarate = (datarate / 8) * 10;
360
361         /* enable down-spreading and execute pre-train script from vbios */
362         dp_link_train_init(dp, dp->dpcd[3] & 0x01);
363
364         /* start off at highest link rate supported by encoder and display */
365         while (*link_bw > (dp->dpcd[1] * 27000))
366                 link_bw++;
367
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)
372                         dp->link_nr >>= 1;
373
374                 /* drop link rate to minimum with this lane count */
375                 while ((link_bw[1] * dp->link_nr) > datarate)
376                         link_bw++;
377                 dp->link_bw = link_bw[0];
378
379                 /* program selected link configuration */
380                 ret = dp_set_link_config(dp);
381                 if (ret == 0) {
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))
386                                 break;
387                 } else
388                 if (ret) {
389                         /* dp_set_link_config() handled training, or
390                          * we failed to communicate with the sink.
391                          */
392                         break;
393                 }
394
395                 /* retry at lower rate */
396                 link_bw++;
397         }
398
399         /* finish link training */
400         dp_set_training_pattern(dp, 0);
401         if (ret < 0)
402                 ERR("link training failed\n");
403
404         /* execute post-train script from vbios */
405         dp_link_train_fini(dp);
406         return (ret < 0) ? false : true;
407 }