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