]> Pileus Git - ~andy/linux/blob - drivers/gpu/drm/nouveau/core/engine/disp/dport.c
Merge tag 'drm-intel-fixes-2013-11-07' of git://people.freedesktop.org/~danvet/drm...
[~andy/linux] / 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 "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  *****************************************************************************/
43 struct 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
58 static int
59 dp_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         int ret;
74
75         DBG("%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
76
77         /* 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
92         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);
108 }
109
110 static void
111 dp_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
124 static int
125 dp_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
147 static int
148 dp_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
158         DBG("status %6ph\n", dp->stat);
159         return 0;
160 }
161
162 static int
163 dp_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
196 static int
197 dp_link_train_eq(struct dp_state *dp)
198 {
199         bool eq_done = false, cr_done = true;
200         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
225 static void
226 dp_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
248 static void
249 dp_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
264 int
265 nouveau_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) {
302                 /* 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;
312                 ERR("failed to read DPCD\n");
313         }
314
315         /* adjust required bandwidth for 8B/10B coding overhead */
316         datarate = (datarate / 8) * 10;
317
318         /* enable down-spreading and execute pre-train script from vbios */
319         dp_link_train_init(dp, dp->dpcd[3] & 0x01);
320
321         /* start off at highest link rate supported by encoder and display */
322         while (*link_bw > (dp->dpcd[1] * 27000))
323                 link_bw++;
324
325         while ((ret = -EIO) && link_bw[0]) {
326                 /* find minimum required lane count at this link rate */
327                 dp->link_nr = dp->dpcd[2] & DPCD_RC02_MAX_LANE_COUNT;
328                 while ((dp->link_nr >> 1) * link_bw[0] > datarate)
329                         dp->link_nr >>= 1;
330
331                 /* drop link rate to minimum with this lane count */
332                 while ((link_bw[1] * dp->link_nr) > datarate)
333                         link_bw++;
334                 dp->link_bw = link_bw[0];
335
336                 /* program selected link configuration */
337                 ret = dp_set_link_config(dp);
338                 if (ret == 0) {
339                         /* attempt to train the link at this configuration */
340                         memset(dp->stat, 0x00, sizeof(dp->stat));
341                         if (!dp_link_train_cr(dp) &&
342                             !dp_link_train_eq(dp))
343                                 break;
344                 } else
345                 if (ret) {
346                         /* dp_set_link_config() handled training, or
347                          * we failed to communicate with the sink.
348                          */
349                         break;
350                 }
351
352                 /* retry at lower rate */
353                 link_bw++;
354         }
355
356         /* finish link training */
357         dp_set_training_pattern(dp, 0);
358         if (ret < 0)
359                 ERR("link training failed\n");
360
361         /* execute post-train script from vbios */
362         dp_link_train_fini(dp);
363         return (ret < 0) ? false : true;
364 }