nouveau_dp.c 12 KB

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