nouveau_dp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 "nouveau_drv.h"
  26. #include "nouveau_i2c.h"
  27. #include "nouveau_connector.h"
  28. #include "nouveau_encoder.h"
  29. /******************************************************************************
  30. * aux channel util functions
  31. *****************************************************************************/
  32. #define AUX_DBG(fmt, args...) do { \
  33. if (nouveau_reg_debug & NOUVEAU_REG_DEBUG_AUXCH) { \
  34. NV_PRINTK(KERN_DEBUG, dev, "AUXCH(%d): " fmt, ch, ##args); \
  35. } \
  36. } while (0)
  37. #define AUX_ERR(fmt, args...) NV_ERROR(dev, "AUXCH(%d): " fmt, ch, ##args)
  38. static void
  39. auxch_fini(struct drm_device *dev, int ch)
  40. {
  41. nv_mask(dev, 0x00e4e4 + (ch * 0x50), 0x00310000, 0x00000000);
  42. }
  43. static int
  44. auxch_init(struct drm_device *dev, int ch)
  45. {
  46. const u32 unksel = 1; /* nfi which to use, or if it matters.. */
  47. const u32 ureq = unksel ? 0x00100000 : 0x00200000;
  48. const u32 urep = unksel ? 0x01000000 : 0x02000000;
  49. u32 ctrl, timeout;
  50. /* wait up to 1ms for any previous transaction to be done... */
  51. timeout = 1000;
  52. do {
  53. ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
  54. udelay(1);
  55. if (!timeout--) {
  56. AUX_ERR("begin idle timeout 0x%08x", ctrl);
  57. return -EBUSY;
  58. }
  59. } while (ctrl & 0x03010000);
  60. /* set some magic, and wait up to 1ms for it to appear */
  61. nv_mask(dev, 0x00e4e4 + (ch * 0x50), 0x00300000, ureq);
  62. timeout = 1000;
  63. do {
  64. ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
  65. udelay(1);
  66. if (!timeout--) {
  67. AUX_ERR("magic wait 0x%08x\n", ctrl);
  68. auxch_fini(dev, ch);
  69. return -EBUSY;
  70. }
  71. } while ((ctrl & 0x03000000) != urep);
  72. return 0;
  73. }
  74. static int
  75. auxch_tx(struct drm_device *dev, int ch, u8 type, u32 addr, u8 *data, u8 size)
  76. {
  77. u32 ctrl, stat, timeout, retries;
  78. u32 xbuf[4] = {};
  79. int ret, i;
  80. AUX_DBG("%d: 0x%08x %d\n", type, addr, size);
  81. ret = auxch_init(dev, ch);
  82. if (ret)
  83. goto out;
  84. stat = nv_rd32(dev, 0x00e4e8 + (ch * 0x50));
  85. if (!(stat & 0x10000000)) {
  86. AUX_DBG("sink not detected\n");
  87. ret = -ENXIO;
  88. goto out;
  89. }
  90. if (!(type & 1)) {
  91. memcpy(xbuf, data, size);
  92. for (i = 0; i < 16; i += 4) {
  93. AUX_DBG("wr 0x%08x\n", xbuf[i / 4]);
  94. nv_wr32(dev, 0x00e4c0 + (ch * 0x50) + i, xbuf[i / 4]);
  95. }
  96. }
  97. ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
  98. ctrl &= ~0x0001f0ff;
  99. ctrl |= type << 12;
  100. ctrl |= size - 1;
  101. nv_wr32(dev, 0x00e4e0 + (ch * 0x50), addr);
  102. /* retry transaction a number of times on failure... */
  103. ret = -EREMOTEIO;
  104. for (retries = 0; retries < 32; retries++) {
  105. /* reset, and delay a while if this is a retry */
  106. nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x80000000 | ctrl);
  107. nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x00000000 | ctrl);
  108. if (retries)
  109. udelay(400);
  110. /* transaction request, wait up to 1ms for it to complete */
  111. nv_wr32(dev, 0x00e4e4 + (ch * 0x50), 0x00010000 | ctrl);
  112. timeout = 1000;
  113. do {
  114. ctrl = nv_rd32(dev, 0x00e4e4 + (ch * 0x50));
  115. udelay(1);
  116. if (!timeout--) {
  117. AUX_ERR("tx req timeout 0x%08x\n", ctrl);
  118. goto out;
  119. }
  120. } while (ctrl & 0x00010000);
  121. /* read status, and check if transaction completed ok */
  122. stat = nv_mask(dev, 0x00e4e8 + (ch * 0x50), 0, 0);
  123. if (!(stat & 0x000f0f00)) {
  124. ret = 0;
  125. break;
  126. }
  127. AUX_DBG("%02d 0x%08x 0x%08x\n", retries, ctrl, stat);
  128. }
  129. if (type & 1) {
  130. for (i = 0; i < 16; i += 4) {
  131. xbuf[i / 4] = nv_rd32(dev, 0x00e4d0 + (ch * 0x50) + i);
  132. AUX_DBG("rd 0x%08x\n", xbuf[i / 4]);
  133. }
  134. memcpy(data, xbuf, size);
  135. }
  136. out:
  137. auxch_fini(dev, ch);
  138. return ret;
  139. }
  140. static int
  141. auxch_rd(struct drm_encoder *encoder, int address, uint8_t *buf, int size)
  142. {
  143. struct drm_device *dev = encoder->dev;
  144. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  145. struct nouveau_i2c_chan *auxch;
  146. int ret;
  147. auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  148. if (!auxch)
  149. return -ENODEV;
  150. ret = nouveau_dp_auxch(auxch, 9, address, buf, size);
  151. if (ret)
  152. return ret;
  153. return 0;
  154. }
  155. static int
  156. auxch_wr(struct drm_encoder *encoder, int address, uint8_t *buf, int size)
  157. {
  158. struct drm_device *dev = encoder->dev;
  159. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  160. struct nouveau_i2c_chan *auxch;
  161. int ret;
  162. auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  163. if (!auxch)
  164. return -ENODEV;
  165. ret = nouveau_dp_auxch(auxch, 8, address, buf, size);
  166. return ret;
  167. }
  168. static u32
  169. dp_link_bw_get(struct drm_device *dev, int or, int link)
  170. {
  171. u32 ctrl = nv_rd32(dev, 0x614300 + (or * 0x800));
  172. if (!(ctrl & 0x000c0000))
  173. return 162000;
  174. return 270000;
  175. }
  176. static int
  177. dp_lane_count_get(struct drm_device *dev, int or, int link)
  178. {
  179. u32 ctrl = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  180. switch (ctrl & 0x000f0000) {
  181. case 0x00010000: return 1;
  182. case 0x00030000: return 2;
  183. default:
  184. return 4;
  185. }
  186. }
  187. void
  188. nouveau_dp_tu_update(struct drm_device *dev, int or, int link, u32 clk, u32 bpp)
  189. {
  190. const u32 symbol = 100000;
  191. int bestTU = 0, bestVTUi = 0, bestVTUf = 0, bestVTUa = 0;
  192. int TU, VTUi, VTUf, VTUa;
  193. u64 link_data_rate, link_ratio, unk;
  194. u32 best_diff = 64 * symbol;
  195. u32 link_nr, link_bw, r;
  196. /* calculate packed data rate for each lane */
  197. link_nr = dp_lane_count_get(dev, or, link);
  198. link_data_rate = (clk * bpp / 8) / link_nr;
  199. /* calculate ratio of packed data rate to link symbol rate */
  200. link_bw = dp_link_bw_get(dev, or, link);
  201. link_ratio = link_data_rate * symbol;
  202. r = do_div(link_ratio, link_bw);
  203. for (TU = 64; TU >= 32; TU--) {
  204. /* calculate average number of valid symbols in each TU */
  205. u32 tu_valid = link_ratio * TU;
  206. u32 calc, diff;
  207. /* find a hw representation for the fraction.. */
  208. VTUi = tu_valid / symbol;
  209. calc = VTUi * symbol;
  210. diff = tu_valid - calc;
  211. if (diff) {
  212. if (diff >= (symbol / 2)) {
  213. VTUf = symbol / (symbol - diff);
  214. if (symbol - (VTUf * diff))
  215. VTUf++;
  216. if (VTUf <= 15) {
  217. VTUa = 1;
  218. calc += symbol - (symbol / VTUf);
  219. } else {
  220. VTUa = 0;
  221. VTUf = 1;
  222. calc += symbol;
  223. }
  224. } else {
  225. VTUa = 0;
  226. VTUf = min((int)(symbol / diff), 15);
  227. calc += symbol / VTUf;
  228. }
  229. diff = calc - tu_valid;
  230. } else {
  231. /* no remainder, but the hw doesn't like the fractional
  232. * part to be zero. decrement the integer part and
  233. * have the fraction add a whole symbol back
  234. */
  235. VTUa = 0;
  236. VTUf = 1;
  237. VTUi--;
  238. }
  239. if (diff < best_diff) {
  240. best_diff = diff;
  241. bestTU = TU;
  242. bestVTUa = VTUa;
  243. bestVTUf = VTUf;
  244. bestVTUi = VTUi;
  245. if (diff == 0)
  246. break;
  247. }
  248. }
  249. if (!bestTU) {
  250. NV_ERROR(dev, "DP: unable to find suitable config\n");
  251. return;
  252. }
  253. /* XXX close to vbios numbers, but not right */
  254. unk = (symbol - link_ratio) * bestTU;
  255. unk *= link_ratio;
  256. r = do_div(unk, symbol);
  257. r = do_div(unk, symbol);
  258. unk += 6;
  259. nv_mask(dev, NV50_SOR_DP_CTRL(or, link), 0x000001fc, bestTU << 2);
  260. nv_mask(dev, NV50_SOR_DP_SCFG(or, link), 0x010f7f3f, bestVTUa << 24 |
  261. bestVTUf << 16 |
  262. bestVTUi << 8 |
  263. unk);
  264. }
  265. static int
  266. nouveau_dp_lane_count_set(struct drm_encoder *encoder, uint8_t cmd)
  267. {
  268. struct drm_device *dev = encoder->dev;
  269. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  270. uint32_t tmp;
  271. int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
  272. tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  273. tmp &= ~(NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED |
  274. NV50_SOR_DP_CTRL_LANE_MASK);
  275. tmp |= ((1 << (cmd & DP_LANE_COUNT_MASK)) - 1) << 16;
  276. if (cmd & DP_LANE_COUNT_ENHANCED_FRAME_EN)
  277. tmp |= NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED;
  278. nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
  279. return auxch_wr(encoder, DP_LANE_COUNT_SET, &cmd, 1);
  280. }
  281. static int
  282. nouveau_dp_link_bw_set(struct drm_encoder *encoder, uint8_t cmd)
  283. {
  284. struct drm_device *dev = encoder->dev;
  285. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  286. uint32_t tmp;
  287. int reg = 0x614300 + (nv_encoder->or * 0x800);
  288. tmp = nv_rd32(dev, reg);
  289. tmp &= 0xfff3ffff;
  290. if (cmd == DP_LINK_BW_2_7)
  291. tmp |= 0x00040000;
  292. nv_wr32(dev, reg, tmp);
  293. return auxch_wr(encoder, DP_LINK_BW_SET, &cmd, 1);
  294. }
  295. static int
  296. nouveau_dp_link_train_set(struct drm_encoder *encoder, int pattern)
  297. {
  298. struct drm_device *dev = encoder->dev;
  299. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  300. uint32_t tmp;
  301. uint8_t cmd;
  302. int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
  303. int ret;
  304. tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  305. tmp &= ~NV50_SOR_DP_CTRL_TRAINING_PATTERN;
  306. tmp |= (pattern << 24);
  307. nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
  308. ret = auxch_rd(encoder, DP_TRAINING_PATTERN_SET, &cmd, 1);
  309. if (ret)
  310. return ret;
  311. cmd &= ~DP_TRAINING_PATTERN_MASK;
  312. cmd |= (pattern & DP_TRAINING_PATTERN_MASK);
  313. return auxch_wr(encoder, DP_TRAINING_PATTERN_SET, &cmd, 1);
  314. }
  315. static int
  316. nouveau_dp_max_voltage_swing(struct drm_encoder *encoder)
  317. {
  318. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  319. struct drm_device *dev = encoder->dev;
  320. struct bit_displayport_encoder_table_entry *dpse;
  321. struct bit_displayport_encoder_table *dpe;
  322. int i, dpe_headerlen, max_vs = 0;
  323. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  324. if (!dpe)
  325. return false;
  326. dpse = (void *)((char *)dpe + dpe_headerlen);
  327. for (i = 0; i < dpe_headerlen; i++, dpse++) {
  328. if (dpse->vs_level > max_vs)
  329. max_vs = dpse->vs_level;
  330. }
  331. return max_vs;
  332. }
  333. static int
  334. nouveau_dp_max_pre_emphasis(struct drm_encoder *encoder, int vs)
  335. {
  336. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  337. struct drm_device *dev = encoder->dev;
  338. struct bit_displayport_encoder_table_entry *dpse;
  339. struct bit_displayport_encoder_table *dpe;
  340. int i, dpe_headerlen, max_pre = 0;
  341. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  342. if (!dpe)
  343. return false;
  344. dpse = (void *)((char *)dpe + dpe_headerlen);
  345. for (i = 0; i < dpe_headerlen; i++, dpse++) {
  346. if (dpse->vs_level != vs)
  347. continue;
  348. if (dpse->pre_level > max_pre)
  349. max_pre = dpse->pre_level;
  350. }
  351. return max_pre;
  352. }
  353. static bool
  354. nouveau_dp_link_train_adjust(struct drm_encoder *encoder, uint8_t *config)
  355. {
  356. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  357. struct drm_device *dev = encoder->dev;
  358. struct bit_displayport_encoder_table *dpe;
  359. int ret, i, dpe_headerlen, vs = 0, pre = 0;
  360. uint8_t request[2];
  361. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  362. if (!dpe)
  363. return false;
  364. ret = auxch_rd(encoder, DP_ADJUST_REQUEST_LANE0_1, request, 2);
  365. if (ret)
  366. return false;
  367. NV_DEBUG_KMS(dev, "\t\tadjust 0x%02x 0x%02x\n", request[0], request[1]);
  368. /* Keep all lanes at the same level.. */
  369. for (i = 0; i < nv_encoder->dp.link_nr; i++) {
  370. int lane_req = (request[i >> 1] >> ((i & 1) << 2)) & 0xf;
  371. int lane_vs = lane_req & 3;
  372. int lane_pre = (lane_req >> 2) & 3;
  373. if (lane_vs > vs)
  374. vs = lane_vs;
  375. if (lane_pre > pre)
  376. pre = lane_pre;
  377. }
  378. if (vs >= nouveau_dp_max_voltage_swing(encoder)) {
  379. vs = nouveau_dp_max_voltage_swing(encoder);
  380. vs |= 4;
  381. }
  382. if (pre >= nouveau_dp_max_pre_emphasis(encoder, vs & 3)) {
  383. pre = nouveau_dp_max_pre_emphasis(encoder, vs & 3);
  384. pre |= 4;
  385. }
  386. /* Update the configuration for all lanes.. */
  387. for (i = 0; i < nv_encoder->dp.link_nr; i++)
  388. config[i] = (pre << 3) | vs;
  389. return true;
  390. }
  391. static bool
  392. nouveau_dp_link_train_commit(struct drm_encoder *encoder, uint8_t *config)
  393. {
  394. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  395. struct drm_device *dev = encoder->dev;
  396. struct bit_displayport_encoder_table_entry *dpse;
  397. struct bit_displayport_encoder_table *dpe;
  398. int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
  399. int dpe_headerlen, ret, i;
  400. NV_DEBUG_KMS(dev, "\t\tconfig 0x%02x 0x%02x 0x%02x 0x%02x\n",
  401. config[0], config[1], config[2], config[3]);
  402. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  403. if (!dpe)
  404. return false;
  405. dpse = (void *)((char *)dpe + dpe_headerlen);
  406. for (i = 0; i < dpe->record_nr; i++, dpse++) {
  407. if (dpse->vs_level == (config[0] & 3) &&
  408. dpse->pre_level == ((config[0] >> 3) & 3))
  409. break;
  410. }
  411. BUG_ON(i == dpe->record_nr);
  412. for (i = 0; i < nv_encoder->dp.link_nr; i++) {
  413. const int shift[4] = { 16, 8, 0, 24 };
  414. uint32_t mask = 0xff << shift[i];
  415. uint32_t reg0, reg1, reg2;
  416. reg0 = nv_rd32(dev, NV50_SOR_DP_UNK118(or, link)) & ~mask;
  417. reg0 |= (dpse->reg0 << shift[i]);
  418. reg1 = nv_rd32(dev, NV50_SOR_DP_UNK120(or, link)) & ~mask;
  419. reg1 |= (dpse->reg1 << shift[i]);
  420. reg2 = nv_rd32(dev, NV50_SOR_DP_UNK130(or, link)) & 0xffff00ff;
  421. reg2 |= (dpse->reg2 << 8);
  422. nv_wr32(dev, NV50_SOR_DP_UNK118(or, link), reg0);
  423. nv_wr32(dev, NV50_SOR_DP_UNK120(or, link), reg1);
  424. nv_wr32(dev, NV50_SOR_DP_UNK130(or, link), reg2);
  425. }
  426. ret = auxch_wr(encoder, DP_TRAINING_LANE0_SET, config, 4);
  427. if (ret)
  428. return false;
  429. return true;
  430. }
  431. bool
  432. nouveau_dp_link_train(struct drm_encoder *encoder)
  433. {
  434. struct drm_device *dev = encoder->dev;
  435. struct drm_nouveau_private *dev_priv = dev->dev_private;
  436. struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;
  437. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  438. struct nouveau_connector *nv_connector;
  439. struct bit_displayport_encoder_table *dpe;
  440. int dpe_headerlen;
  441. uint8_t config[4], status[3];
  442. bool cr_done, cr_max_vs, eq_done, hpd_state;
  443. int ret = 0, i, tries, voltage;
  444. NV_DEBUG_KMS(dev, "link training!!\n");
  445. nv_connector = nouveau_encoder_connector_get(nv_encoder);
  446. if (!nv_connector)
  447. return false;
  448. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  449. if (!dpe) {
  450. NV_ERROR(dev, "SOR-%d: no DP encoder table!\n", nv_encoder->or);
  451. return false;
  452. }
  453. /* disable hotplug detect, this flips around on some panels during
  454. * link training.
  455. */
  456. hpd_state = pgpio->irq_enable(dev, nv_connector->dcb->gpio_tag, false);
  457. if (dpe->script0) {
  458. NV_DEBUG_KMS(dev, "SOR-%d: running DP script 0\n", nv_encoder->or);
  459. nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script0),
  460. nv_encoder->dcb, -1);
  461. }
  462. train:
  463. cr_done = eq_done = false;
  464. /* set link configuration */
  465. NV_DEBUG_KMS(dev, "\tbegin train: bw %d, lanes %d\n",
  466. nv_encoder->dp.link_bw, nv_encoder->dp.link_nr);
  467. ret = nouveau_dp_link_bw_set(encoder, nv_encoder->dp.link_bw);
  468. if (ret)
  469. return false;
  470. config[0] = nv_encoder->dp.link_nr;
  471. if (nv_encoder->dp.dpcd_version >= 0x11 &&
  472. nv_encoder->dp.enhanced_frame)
  473. config[0] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  474. ret = nouveau_dp_lane_count_set(encoder, config[0]);
  475. if (ret)
  476. return false;
  477. /* clock recovery */
  478. NV_DEBUG_KMS(dev, "\tbegin cr\n");
  479. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_1);
  480. if (ret)
  481. goto stop;
  482. tries = 0;
  483. voltage = -1;
  484. memset(config, 0x00, sizeof(config));
  485. for (;;) {
  486. if (!nouveau_dp_link_train_commit(encoder, config))
  487. break;
  488. udelay(100);
  489. ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 2);
  490. if (ret)
  491. break;
  492. NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",
  493. status[0], status[1]);
  494. cr_done = true;
  495. cr_max_vs = false;
  496. for (i = 0; i < nv_encoder->dp.link_nr; i++) {
  497. int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;
  498. if (!(lane & DP_LANE_CR_DONE)) {
  499. cr_done = false;
  500. if (config[i] & DP_TRAIN_MAX_PRE_EMPHASIS_REACHED)
  501. cr_max_vs = true;
  502. break;
  503. }
  504. }
  505. if ((config[0] & DP_TRAIN_VOLTAGE_SWING_MASK) != voltage) {
  506. voltage = config[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  507. tries = 0;
  508. }
  509. if (cr_done || cr_max_vs || (++tries == 5))
  510. break;
  511. if (!nouveau_dp_link_train_adjust(encoder, config))
  512. break;
  513. }
  514. if (!cr_done)
  515. goto stop;
  516. /* channel equalisation */
  517. NV_DEBUG_KMS(dev, "\tbegin eq\n");
  518. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_2);
  519. if (ret)
  520. goto stop;
  521. for (tries = 0; tries <= 5; tries++) {
  522. udelay(400);
  523. ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 3);
  524. if (ret)
  525. break;
  526. NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",
  527. status[0], status[1]);
  528. eq_done = true;
  529. if (!(status[2] & DP_INTERLANE_ALIGN_DONE))
  530. eq_done = false;
  531. for (i = 0; eq_done && i < nv_encoder->dp.link_nr; i++) {
  532. int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;
  533. if (!(lane & DP_LANE_CR_DONE)) {
  534. cr_done = false;
  535. break;
  536. }
  537. if (!(lane & DP_LANE_CHANNEL_EQ_DONE) ||
  538. !(lane & DP_LANE_SYMBOL_LOCKED)) {
  539. eq_done = false;
  540. break;
  541. }
  542. }
  543. if (eq_done || !cr_done)
  544. break;
  545. if (!nouveau_dp_link_train_adjust(encoder, config) ||
  546. !nouveau_dp_link_train_commit(encoder, config))
  547. break;
  548. }
  549. stop:
  550. /* end link training */
  551. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_DISABLE);
  552. if (ret)
  553. return false;
  554. /* retry at a lower setting, if possible */
  555. if (!ret && !(eq_done && cr_done)) {
  556. NV_DEBUG_KMS(dev, "\twe failed\n");
  557. if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62) {
  558. NV_DEBUG_KMS(dev, "retry link training at low rate\n");
  559. nv_encoder->dp.link_bw = DP_LINK_BW_1_62;
  560. goto train;
  561. }
  562. }
  563. if (dpe->script1) {
  564. NV_DEBUG_KMS(dev, "SOR-%d: running DP script 1\n", nv_encoder->or);
  565. nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script1),
  566. nv_encoder->dcb, -1);
  567. }
  568. /* re-enable hotplug detect */
  569. pgpio->irq_enable(dev, nv_connector->dcb->gpio_tag, hpd_state);
  570. return eq_done;
  571. }
  572. bool
  573. nouveau_dp_detect(struct drm_encoder *encoder)
  574. {
  575. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  576. struct drm_device *dev = encoder->dev;
  577. uint8_t dpcd[4];
  578. int ret;
  579. ret = auxch_rd(encoder, 0x0000, dpcd, 4);
  580. if (ret)
  581. return false;
  582. NV_DEBUG_KMS(dev, "encoder: link_bw %d, link_nr %d\n"
  583. "display: link_bw %d, link_nr %d version 0x%02x\n",
  584. nv_encoder->dcb->dpconf.link_bw,
  585. nv_encoder->dcb->dpconf.link_nr,
  586. dpcd[1], dpcd[2] & 0x0f, dpcd[0]);
  587. nv_encoder->dp.dpcd_version = dpcd[0];
  588. nv_encoder->dp.link_bw = dpcd[1];
  589. if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62 &&
  590. !nv_encoder->dcb->dpconf.link_bw)
  591. nv_encoder->dp.link_bw = DP_LINK_BW_1_62;
  592. nv_encoder->dp.link_nr = dpcd[2] & DP_MAX_LANE_COUNT_MASK;
  593. if (nv_encoder->dp.link_nr > nv_encoder->dcb->dpconf.link_nr)
  594. nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
  595. nv_encoder->dp.enhanced_frame = (dpcd[2] & DP_ENHANCED_FRAME_CAP);
  596. return true;
  597. }
  598. int
  599. nouveau_dp_auxch(struct nouveau_i2c_chan *auxch, int cmd, int addr,
  600. uint8_t *data, int data_nr)
  601. {
  602. return auxch_tx(auxch->dev, auxch->rd, cmd, addr, data, data_nr);
  603. }
  604. static int
  605. nouveau_dp_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
  606. {
  607. struct nouveau_i2c_chan *auxch = (struct nouveau_i2c_chan *)adap;
  608. struct i2c_msg *msg = msgs;
  609. int ret, mcnt = num;
  610. while (mcnt--) {
  611. u8 remaining = msg->len;
  612. u8 *ptr = msg->buf;
  613. while (remaining) {
  614. u8 cnt = (remaining > 16) ? 16 : remaining;
  615. u8 cmd;
  616. if (msg->flags & I2C_M_RD)
  617. cmd = AUX_I2C_READ;
  618. else
  619. cmd = AUX_I2C_WRITE;
  620. if (mcnt || remaining > 16)
  621. cmd |= AUX_I2C_MOT;
  622. ret = nouveau_dp_auxch(auxch, cmd, msg->addr, ptr, cnt);
  623. if (ret < 0)
  624. return ret;
  625. ptr += cnt;
  626. remaining -= cnt;
  627. }
  628. msg++;
  629. }
  630. return num;
  631. }
  632. static u32
  633. nouveau_dp_i2c_func(struct i2c_adapter *adap)
  634. {
  635. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  636. }
  637. const struct i2c_algorithm nouveau_dp_i2c_algo = {
  638. .master_xfer = nouveau_dp_i2c_xfer,
  639. .functionality = nouveau_dp_i2c_func
  640. };