nouveau_dp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * Copyright 2009 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. #include <drm/drmP.h>
  25. #include <drm/drm_dp_helper.h>
  26. #include "nouveau_drm.h"
  27. #include "nouveau_connector.h"
  28. #include "nouveau_encoder.h"
  29. #include "nouveau_crtc.h"
  30. #include <core/class.h>
  31. #include <subdev/gpio.h>
  32. #include <subdev/i2c.h>
  33. /******************************************************************************
  34. * link training
  35. *****************************************************************************/
  36. struct dp_state {
  37. struct nouveau_i2c_port *auxch;
  38. struct nouveau_object *core;
  39. struct dcb_output *dcb;
  40. int crtc;
  41. u8 *dpcd;
  42. int link_nr;
  43. u32 link_bw;
  44. u8 stat[6];
  45. u8 conf[4];
  46. };
  47. static void
  48. dp_set_link_config(struct drm_device *dev, struct dp_state *dp)
  49. {
  50. struct nouveau_drm *drm = nouveau_drm(dev);
  51. struct dcb_output *dcb = dp->dcb;
  52. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  53. const u32 moff = (dp->crtc << 3) | (link << 2) | or;
  54. u8 sink[2];
  55. u32 data;
  56. NV_DEBUG(drm, "%d lanes at %d KB/s\n", dp->link_nr, dp->link_bw);
  57. /* set desired link configuration on the source */
  58. data = ((dp->link_bw / 27000) << 8) | dp->link_nr;
  59. if (dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)
  60. data |= NV94_DISP_SOR_DP_LNKCTL_FRAME_ENH;
  61. nv_call(dp->core, NV94_DISP_SOR_DP_LNKCTL + moff, data);
  62. /* inform the sink of the new configuration */
  63. sink[0] = dp->link_bw / 27000;
  64. sink[1] = dp->link_nr;
  65. if (dp->dpcd[2] & DP_ENHANCED_FRAME_CAP)
  66. sink[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  67. nv_wraux(dp->auxch, DP_LINK_BW_SET, sink, 2);
  68. }
  69. static void
  70. dp_set_training_pattern(struct drm_device *dev, struct dp_state *dp, u8 pattern)
  71. {
  72. struct nouveau_drm *drm = nouveau_drm(dev);
  73. struct dcb_output *dcb = dp->dcb;
  74. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  75. const u32 moff = (dp->crtc << 3) | (link << 2) | or;
  76. u8 sink_tp;
  77. NV_DEBUG(drm, "training pattern %d\n", pattern);
  78. nv_call(dp->core, NV94_DISP_SOR_DP_TRAIN + moff, pattern);
  79. nv_rdaux(dp->auxch, DP_TRAINING_PATTERN_SET, &sink_tp, 1);
  80. sink_tp &= ~DP_TRAINING_PATTERN_MASK;
  81. sink_tp |= pattern;
  82. nv_wraux(dp->auxch, DP_TRAINING_PATTERN_SET, &sink_tp, 1);
  83. }
  84. static int
  85. dp_link_train_commit(struct drm_device *dev, struct dp_state *dp)
  86. {
  87. struct nouveau_drm *drm = nouveau_drm(dev);
  88. struct dcb_output *dcb = dp->dcb;
  89. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  90. const u32 moff = (dp->crtc << 3) | (link << 2) | or;
  91. int i;
  92. for (i = 0; i < dp->link_nr; i++) {
  93. u8 lane = (dp->stat[4 + (i >> 1)] >> ((i & 1) * 4)) & 0xf;
  94. u8 lpre = (lane & 0x0c) >> 2;
  95. u8 lvsw = (lane & 0x03) >> 0;
  96. dp->conf[i] = (lpre << 3) | lvsw;
  97. if (lvsw == DP_TRAIN_VOLTAGE_SWING_1200)
  98. dp->conf[i] |= DP_TRAIN_MAX_SWING_REACHED;
  99. if ((lpre << 3) == DP_TRAIN_PRE_EMPHASIS_9_5)
  100. dp->conf[i] |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  101. NV_DEBUG(drm, "config lane %d %02x\n", i, dp->conf[i]);
  102. nv_call(dp->core, NV94_DISP_SOR_DP_DRVCTL(i) + moff, (lvsw << 8) | lpre);
  103. }
  104. return nv_wraux(dp->auxch, DP_TRAINING_LANE0_SET, dp->conf, 4);
  105. }
  106. static int
  107. dp_link_train_update(struct drm_device *dev, struct dp_state *dp, u32 delay)
  108. {
  109. struct nouveau_drm *drm = nouveau_drm(dev);
  110. int ret;
  111. udelay(delay);
  112. ret = nv_rdaux(dp->auxch, DP_LANE0_1_STATUS, dp->stat, 6);
  113. if (ret)
  114. return ret;
  115. NV_DEBUG(drm, "status %*ph\n", 6, dp->stat);
  116. return 0;
  117. }
  118. static int
  119. dp_link_train_cr(struct drm_device *dev, struct dp_state *dp)
  120. {
  121. bool cr_done = false, abort = false;
  122. int voltage = dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  123. int tries = 0, i;
  124. dp_set_training_pattern(dev, dp, DP_TRAINING_PATTERN_1);
  125. do {
  126. if (dp_link_train_commit(dev, dp) ||
  127. dp_link_train_update(dev, dp, 100))
  128. break;
  129. cr_done = true;
  130. for (i = 0; i < dp->link_nr; i++) {
  131. u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
  132. if (!(lane & DP_LANE_CR_DONE)) {
  133. cr_done = false;
  134. if (dp->conf[i] & DP_TRAIN_MAX_SWING_REACHED)
  135. abort = true;
  136. break;
  137. }
  138. }
  139. if ((dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK) != voltage) {
  140. voltage = dp->conf[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  141. tries = 0;
  142. }
  143. } while (!cr_done && !abort && ++tries < 5);
  144. return cr_done ? 0 : -1;
  145. }
  146. static int
  147. dp_link_train_eq(struct drm_device *dev, struct dp_state *dp)
  148. {
  149. bool eq_done, cr_done = true;
  150. int tries = 0, i;
  151. dp_set_training_pattern(dev, dp, DP_TRAINING_PATTERN_2);
  152. do {
  153. if (dp_link_train_update(dev, dp, 400))
  154. break;
  155. eq_done = !!(dp->stat[2] & DP_INTERLANE_ALIGN_DONE);
  156. for (i = 0; i < dp->link_nr && eq_done; i++) {
  157. u8 lane = (dp->stat[i >> 1] >> ((i & 1) * 4)) & 0xf;
  158. if (!(lane & DP_LANE_CR_DONE))
  159. cr_done = false;
  160. if (!(lane & DP_LANE_CHANNEL_EQ_DONE) ||
  161. !(lane & DP_LANE_SYMBOL_LOCKED))
  162. eq_done = false;
  163. }
  164. if (dp_link_train_commit(dev, dp))
  165. break;
  166. } while (!eq_done && cr_done && ++tries <= 5);
  167. return eq_done ? 0 : -1;
  168. }
  169. static void
  170. dp_link_train_init(struct drm_device *dev, struct dp_state *dp, bool spread)
  171. {
  172. struct dcb_output *dcb = dp->dcb;
  173. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  174. const u32 moff = (dp->crtc << 3) | (link << 2) | or;
  175. nv_call(dp->core, NV94_DISP_SOR_DP_TRAIN + moff, (spread ?
  176. NV94_DISP_SOR_DP_TRAIN_INIT_SPREAD_ON :
  177. NV94_DISP_SOR_DP_TRAIN_INIT_SPREAD_OFF) |
  178. NV94_DISP_SOR_DP_TRAIN_OP_INIT);
  179. }
  180. static void
  181. dp_link_train_fini(struct drm_device *dev, struct dp_state *dp)
  182. {
  183. struct dcb_output *dcb = dp->dcb;
  184. const u32 or = ffs(dcb->or) - 1, link = !(dcb->sorconf.link & 1);
  185. const u32 moff = (dp->crtc << 3) | (link << 2) | or;
  186. nv_call(dp->core, NV94_DISP_SOR_DP_TRAIN + moff,
  187. NV94_DISP_SOR_DP_TRAIN_OP_FINI);
  188. }
  189. static bool
  190. nouveau_dp_link_train(struct drm_encoder *encoder, u32 datarate,
  191. struct nouveau_object *core)
  192. {
  193. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  194. struct nouveau_crtc *nv_crtc = nouveau_crtc(encoder->crtc);
  195. struct nouveau_connector *nv_connector =
  196. nouveau_encoder_connector_get(nv_encoder);
  197. struct drm_device *dev = encoder->dev;
  198. struct nouveau_drm *drm = nouveau_drm(dev);
  199. struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
  200. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  201. const u32 bw_list[] = { 270000, 162000, 0 };
  202. const u32 *link_bw = bw_list;
  203. struct dp_state dp;
  204. dp.auxch = i2c->find(i2c, nv_encoder->dcb->i2c_index);
  205. if (!dp.auxch)
  206. return false;
  207. dp.core = core;
  208. dp.dcb = nv_encoder->dcb;
  209. dp.crtc = nv_crtc->index;
  210. dp.dpcd = nv_encoder->dp.dpcd;
  211. /* adjust required bandwidth for 8B/10B coding overhead */
  212. datarate = (datarate / 8) * 10;
  213. /* some sinks toggle hotplug in response to some of the actions
  214. * we take during link training (DP_SET_POWER is one), we need
  215. * to ignore them for the moment to avoid races.
  216. */
  217. gpio->irq(gpio, 0, nv_connector->hpd, 0xff, false);
  218. /* enable down-spreading and execute pre-train script from vbios */
  219. dp_link_train_init(dev, &dp, nv_encoder->dp.dpcd[3] & 1);
  220. /* start off at highest link rate supported by encoder and display */
  221. while (*link_bw > nv_encoder->dp.link_bw)
  222. link_bw++;
  223. while (link_bw[0]) {
  224. /* find minimum required lane count at this link rate */
  225. dp.link_nr = nv_encoder->dp.link_nr;
  226. while ((dp.link_nr >> 1) * link_bw[0] > datarate)
  227. dp.link_nr >>= 1;
  228. /* drop link rate to minimum with this lane count */
  229. while ((link_bw[1] * dp.link_nr) > datarate)
  230. link_bw++;
  231. dp.link_bw = link_bw[0];
  232. /* program selected link configuration */
  233. dp_set_link_config(dev, &dp);
  234. /* attempt to train the link at this configuration */
  235. memset(dp.stat, 0x00, sizeof(dp.stat));
  236. if (!dp_link_train_cr(dev, &dp) &&
  237. !dp_link_train_eq(dev, &dp))
  238. break;
  239. /* retry at lower rate */
  240. link_bw++;
  241. }
  242. /* finish link training */
  243. dp_set_training_pattern(dev, &dp, DP_TRAINING_PATTERN_DISABLE);
  244. /* execute post-train script from vbios */
  245. dp_link_train_fini(dev, &dp);
  246. /* re-enable hotplug detect */
  247. gpio->irq(gpio, 0, nv_connector->hpd, 0xff, true);
  248. return true;
  249. }
  250. void
  251. nouveau_dp_dpms(struct drm_encoder *encoder, int mode, u32 datarate,
  252. struct nouveau_object *core)
  253. {
  254. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  255. struct nouveau_drm *drm = nouveau_drm(encoder->dev);
  256. struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
  257. struct nouveau_i2c_port *auxch;
  258. u8 status;
  259. auxch = i2c->find(i2c, nv_encoder->dcb->i2c_index);
  260. if (!auxch)
  261. return;
  262. if (mode == DRM_MODE_DPMS_ON)
  263. status = DP_SET_POWER_D0;
  264. else
  265. status = DP_SET_POWER_D3;
  266. nv_wraux(auxch, DP_SET_POWER, &status, 1);
  267. if (mode == DRM_MODE_DPMS_ON)
  268. nouveau_dp_link_train(encoder, datarate, core);
  269. }
  270. static void
  271. nouveau_dp_probe_oui(struct drm_device *dev, struct nouveau_i2c_port *auxch,
  272. u8 *dpcd)
  273. {
  274. struct nouveau_drm *drm = nouveau_drm(dev);
  275. u8 buf[3];
  276. if (!(dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
  277. return;
  278. if (!nv_rdaux(auxch, DP_SINK_OUI, buf, 3))
  279. NV_DEBUG(drm, "Sink OUI: %02hx%02hx%02hx\n",
  280. buf[0], buf[1], buf[2]);
  281. if (!nv_rdaux(auxch, DP_BRANCH_OUI, buf, 3))
  282. NV_DEBUG(drm, "Branch OUI: %02hx%02hx%02hx\n",
  283. buf[0], buf[1], buf[2]);
  284. }
  285. bool
  286. nouveau_dp_detect(struct drm_encoder *encoder)
  287. {
  288. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  289. struct drm_device *dev = encoder->dev;
  290. struct nouveau_drm *drm = nouveau_drm(dev);
  291. struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
  292. struct nouveau_i2c_port *auxch;
  293. u8 *dpcd = nv_encoder->dp.dpcd;
  294. int ret;
  295. auxch = i2c->find(i2c, nv_encoder->dcb->i2c_index);
  296. if (!auxch)
  297. return false;
  298. ret = nv_rdaux(auxch, DP_DPCD_REV, dpcd, 8);
  299. if (ret)
  300. return false;
  301. nv_encoder->dp.link_bw = 27000 * dpcd[1];
  302. nv_encoder->dp.link_nr = dpcd[2] & DP_MAX_LANE_COUNT_MASK;
  303. NV_DEBUG(drm, "display: %dx%d dpcd 0x%02x\n",
  304. nv_encoder->dp.link_nr, nv_encoder->dp.link_bw, dpcd[0]);
  305. NV_DEBUG(drm, "encoder: %dx%d\n",
  306. nv_encoder->dcb->dpconf.link_nr,
  307. nv_encoder->dcb->dpconf.link_bw);
  308. if (nv_encoder->dcb->dpconf.link_nr < nv_encoder->dp.link_nr)
  309. nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
  310. if (nv_encoder->dcb->dpconf.link_bw < nv_encoder->dp.link_bw)
  311. nv_encoder->dp.link_bw = nv_encoder->dcb->dpconf.link_bw;
  312. NV_DEBUG(drm, "maximum: %dx%d\n",
  313. nv_encoder->dp.link_nr, nv_encoder->dp.link_bw);
  314. nouveau_dp_probe_oui(dev, auxch, dpcd);
  315. return true;
  316. }