nouveau_dp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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_encoder.h"
  28. static int
  29. auxch_rd(struct drm_encoder *encoder, int address, uint8_t *buf, int size)
  30. {
  31. struct drm_device *dev = encoder->dev;
  32. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  33. struct nouveau_i2c_chan *auxch;
  34. int ret;
  35. auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  36. if (!auxch)
  37. return -ENODEV;
  38. ret = nouveau_dp_auxch(auxch, 9, address, buf, size);
  39. if (ret)
  40. return ret;
  41. return 0;
  42. }
  43. static int
  44. auxch_wr(struct drm_encoder *encoder, int address, uint8_t *buf, int size)
  45. {
  46. struct drm_device *dev = encoder->dev;
  47. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  48. struct nouveau_i2c_chan *auxch;
  49. int ret;
  50. auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);
  51. if (!auxch)
  52. return -ENODEV;
  53. ret = nouveau_dp_auxch(auxch, 8, address, buf, size);
  54. return ret;
  55. }
  56. static int
  57. nouveau_dp_lane_count_set(struct drm_encoder *encoder, uint8_t cmd)
  58. {
  59. struct drm_device *dev = encoder->dev;
  60. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  61. uint32_t tmp;
  62. int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
  63. tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  64. tmp &= ~(NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED |
  65. NV50_SOR_DP_CTRL_LANE_MASK);
  66. tmp |= ((1 << (cmd & DP_LANE_COUNT_MASK)) - 1) << 16;
  67. if (cmd & DP_LANE_COUNT_ENHANCED_FRAME_EN)
  68. tmp |= NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED;
  69. nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
  70. return auxch_wr(encoder, DP_LANE_COUNT_SET, &cmd, 1);
  71. }
  72. static int
  73. nouveau_dp_link_bw_set(struct drm_encoder *encoder, uint8_t cmd)
  74. {
  75. struct drm_device *dev = encoder->dev;
  76. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  77. uint32_t tmp;
  78. int reg = 0x614300 + (nv_encoder->or * 0x800);
  79. tmp = nv_rd32(dev, reg);
  80. tmp &= 0xfff3ffff;
  81. if (cmd == DP_LINK_BW_2_7)
  82. tmp |= 0x00040000;
  83. nv_wr32(dev, reg, tmp);
  84. return auxch_wr(encoder, DP_LINK_BW_SET, &cmd, 1);
  85. }
  86. static int
  87. nouveau_dp_link_train_set(struct drm_encoder *encoder, int pattern)
  88. {
  89. struct drm_device *dev = encoder->dev;
  90. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  91. uint32_t tmp;
  92. uint8_t cmd;
  93. int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
  94. int ret;
  95. tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));
  96. tmp &= ~NV50_SOR_DP_CTRL_TRAINING_PATTERN;
  97. tmp |= (pattern << 24);
  98. nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);
  99. ret = auxch_rd(encoder, DP_TRAINING_PATTERN_SET, &cmd, 1);
  100. if (ret)
  101. return ret;
  102. cmd &= ~DP_TRAINING_PATTERN_MASK;
  103. cmd |= (pattern & DP_TRAINING_PATTERN_MASK);
  104. return auxch_wr(encoder, DP_TRAINING_PATTERN_SET, &cmd, 1);
  105. }
  106. static int
  107. nouveau_dp_max_voltage_swing(struct drm_encoder *encoder)
  108. {
  109. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  110. struct drm_device *dev = encoder->dev;
  111. struct bit_displayport_encoder_table_entry *dpse;
  112. struct bit_displayport_encoder_table *dpe;
  113. int i, dpe_headerlen, max_vs = 0;
  114. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  115. if (!dpe)
  116. return false;
  117. dpse = (void *)((char *)dpe + dpe_headerlen);
  118. for (i = 0; i < dpe_headerlen; i++, dpse++) {
  119. if (dpse->vs_level > max_vs)
  120. max_vs = dpse->vs_level;
  121. }
  122. return max_vs;
  123. }
  124. static int
  125. nouveau_dp_max_pre_emphasis(struct drm_encoder *encoder, int vs)
  126. {
  127. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  128. struct drm_device *dev = encoder->dev;
  129. struct bit_displayport_encoder_table_entry *dpse;
  130. struct bit_displayport_encoder_table *dpe;
  131. int i, dpe_headerlen, max_pre = 0;
  132. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  133. if (!dpe)
  134. return false;
  135. dpse = (void *)((char *)dpe + dpe_headerlen);
  136. for (i = 0; i < dpe_headerlen; i++, dpse++) {
  137. if (dpse->vs_level != vs)
  138. continue;
  139. if (dpse->pre_level > max_pre)
  140. max_pre = dpse->pre_level;
  141. }
  142. return max_pre;
  143. }
  144. static bool
  145. nouveau_dp_link_train_adjust(struct drm_encoder *encoder, uint8_t *config)
  146. {
  147. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  148. struct drm_device *dev = encoder->dev;
  149. struct bit_displayport_encoder_table_entry *dpse;
  150. struct bit_displayport_encoder_table *dpe;
  151. int ret, i, dpe_headerlen, vs = 0, pre = 0;
  152. uint8_t request[2];
  153. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  154. if (!dpe)
  155. return false;
  156. dpse = (void *)((char *)dpe + dpe_headerlen);
  157. ret = auxch_rd(encoder, DP_ADJUST_REQUEST_LANE0_1, request, 2);
  158. if (ret)
  159. return false;
  160. NV_DEBUG_KMS(dev, "\t\tadjust 0x%02x 0x%02x\n", request[0], request[1]);
  161. /* Keep all lanes at the same level.. */
  162. for (i = 0; i < nv_encoder->dp.link_nr; i++) {
  163. int lane_req = (request[i >> 1] >> ((i & 1) << 2)) & 0xf;
  164. int lane_vs = lane_req & 3;
  165. int lane_pre = (lane_req >> 2) & 3;
  166. if (lane_vs > vs)
  167. vs = lane_vs;
  168. if (lane_pre > pre)
  169. pre = lane_pre;
  170. }
  171. if (vs >= nouveau_dp_max_voltage_swing(encoder)) {
  172. vs = nouveau_dp_max_voltage_swing(encoder);
  173. vs |= 4;
  174. }
  175. if (pre >= nouveau_dp_max_pre_emphasis(encoder, vs & 3)) {
  176. pre = nouveau_dp_max_pre_emphasis(encoder, vs & 3);
  177. pre |= 4;
  178. }
  179. /* Update the configuration for all lanes.. */
  180. for (i = 0; i < nv_encoder->dp.link_nr; i++)
  181. config[i] = (pre << 3) | vs;
  182. return true;
  183. }
  184. static bool
  185. nouveau_dp_link_train_commit(struct drm_encoder *encoder, uint8_t *config)
  186. {
  187. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  188. struct drm_device *dev = encoder->dev;
  189. struct bit_displayport_encoder_table_entry *dpse;
  190. struct bit_displayport_encoder_table *dpe;
  191. int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);
  192. int dpe_headerlen, ret, i;
  193. NV_DEBUG_KMS(dev, "\t\tconfig 0x%02x 0x%02x 0x%02x 0x%02x\n",
  194. config[0], config[1], config[2], config[3]);
  195. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  196. if (!dpe)
  197. return false;
  198. dpse = (void *)((char *)dpe + dpe_headerlen);
  199. for (i = 0; i < dpe->record_nr; i++, dpse++) {
  200. if (dpse->vs_level == (config[0] & 3) &&
  201. dpse->pre_level == ((config[0] >> 3) & 3))
  202. break;
  203. }
  204. BUG_ON(i == dpe->record_nr);
  205. for (i = 0; i < nv_encoder->dp.link_nr; i++) {
  206. const int shift[4] = { 16, 8, 0, 24 };
  207. uint32_t mask = 0xff << shift[i];
  208. uint32_t reg0, reg1, reg2;
  209. reg0 = nv_rd32(dev, NV50_SOR_DP_UNK118(or, link)) & ~mask;
  210. reg0 |= (dpse->reg0 << shift[i]);
  211. reg1 = nv_rd32(dev, NV50_SOR_DP_UNK120(or, link)) & ~mask;
  212. reg1 |= (dpse->reg1 << shift[i]);
  213. reg2 = nv_rd32(dev, NV50_SOR_DP_UNK130(or, link)) & 0xffff00ff;
  214. reg2 |= (dpse->reg2 << 8);
  215. nv_wr32(dev, NV50_SOR_DP_UNK118(or, link), reg0);
  216. nv_wr32(dev, NV50_SOR_DP_UNK120(or, link), reg1);
  217. nv_wr32(dev, NV50_SOR_DP_UNK130(or, link), reg2);
  218. }
  219. ret = auxch_wr(encoder, DP_TRAINING_LANE0_SET, config, 4);
  220. if (ret)
  221. return false;
  222. return true;
  223. }
  224. bool
  225. nouveau_dp_link_train(struct drm_encoder *encoder)
  226. {
  227. struct drm_device *dev = encoder->dev;
  228. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  229. struct bit_displayport_encoder_table *dpe;
  230. int dpe_headerlen;
  231. uint8_t config[4], status[3];
  232. bool cr_done, cr_max_vs, eq_done;
  233. int ret = 0, i, tries, voltage;
  234. NV_DEBUG_KMS(dev, "link training!!\n");
  235. dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);
  236. if (!dpe) {
  237. NV_ERROR(dev, "SOR-%d: no DP encoder table!\n", nv_encoder->or);
  238. return false;
  239. }
  240. if (dpe->script0) {
  241. NV_DEBUG_KMS(dev, "SOR-%d: running DP script 0\n", nv_encoder->or);
  242. nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script0),
  243. nv_encoder->dcb);
  244. }
  245. train:
  246. cr_done = eq_done = false;
  247. /* set link configuration */
  248. NV_DEBUG_KMS(dev, "\tbegin train: bw %d, lanes %d\n",
  249. nv_encoder->dp.link_bw, nv_encoder->dp.link_nr);
  250. ret = nouveau_dp_link_bw_set(encoder, nv_encoder->dp.link_bw);
  251. if (ret)
  252. return false;
  253. config[0] = nv_encoder->dp.link_nr;
  254. if (nv_encoder->dp.dpcd_version >= 0x11)
  255. config[0] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  256. ret = nouveau_dp_lane_count_set(encoder, config[0]);
  257. if (ret)
  258. return false;
  259. /* clock recovery */
  260. NV_DEBUG_KMS(dev, "\tbegin cr\n");
  261. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_1);
  262. if (ret)
  263. goto stop;
  264. tries = 0;
  265. voltage = -1;
  266. memset(config, 0x00, sizeof(config));
  267. for (;;) {
  268. if (!nouveau_dp_link_train_commit(encoder, config))
  269. break;
  270. udelay(100);
  271. ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 2);
  272. if (ret)
  273. break;
  274. NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",
  275. status[0], status[1]);
  276. cr_done = true;
  277. cr_max_vs = false;
  278. for (i = 0; i < nv_encoder->dp.link_nr; i++) {
  279. int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;
  280. if (!(lane & DP_LANE_CR_DONE)) {
  281. cr_done = false;
  282. if (config[i] & DP_TRAIN_MAX_PRE_EMPHASIS_REACHED)
  283. cr_max_vs = true;
  284. break;
  285. }
  286. }
  287. if ((config[0] & DP_TRAIN_VOLTAGE_SWING_MASK) != voltage) {
  288. voltage = config[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  289. tries = 0;
  290. }
  291. if (cr_done || cr_max_vs || (++tries == 5))
  292. break;
  293. if (!nouveau_dp_link_train_adjust(encoder, config))
  294. break;
  295. }
  296. if (!cr_done)
  297. goto stop;
  298. /* channel equalisation */
  299. NV_DEBUG_KMS(dev, "\tbegin eq\n");
  300. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_2);
  301. if (ret)
  302. goto stop;
  303. for (tries = 0; tries <= 5; tries++) {
  304. udelay(400);
  305. ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 3);
  306. if (ret)
  307. break;
  308. NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",
  309. status[0], status[1]);
  310. eq_done = true;
  311. if (!(status[2] & DP_INTERLANE_ALIGN_DONE))
  312. eq_done = false;
  313. for (i = 0; eq_done && i < nv_encoder->dp.link_nr; i++) {
  314. int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;
  315. if (!(lane & DP_LANE_CR_DONE)) {
  316. cr_done = false;
  317. break;
  318. }
  319. if (!(lane & DP_LANE_CHANNEL_EQ_DONE) ||
  320. !(lane & DP_LANE_SYMBOL_LOCKED)) {
  321. eq_done = false;
  322. break;
  323. }
  324. }
  325. if (eq_done || !cr_done)
  326. break;
  327. if (!nouveau_dp_link_train_adjust(encoder, config) ||
  328. !nouveau_dp_link_train_commit(encoder, config))
  329. break;
  330. }
  331. stop:
  332. /* end link training */
  333. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_DISABLE);
  334. if (ret)
  335. return false;
  336. /* retry at a lower setting, if possible */
  337. if (!ret && !(eq_done && cr_done)) {
  338. NV_DEBUG_KMS(dev, "\twe failed\n");
  339. if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62) {
  340. NV_DEBUG_KMS(dev, "retry link training at low rate\n");
  341. nv_encoder->dp.link_bw = DP_LINK_BW_1_62;
  342. goto train;
  343. }
  344. }
  345. if (dpe->script1) {
  346. NV_DEBUG_KMS(dev, "SOR-%d: running DP script 1\n", nv_encoder->or);
  347. nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script1),
  348. nv_encoder->dcb);
  349. }
  350. return eq_done;
  351. }
  352. bool
  353. nouveau_dp_detect(struct drm_encoder *encoder)
  354. {
  355. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  356. struct drm_device *dev = encoder->dev;
  357. uint8_t dpcd[4];
  358. int ret;
  359. ret = auxch_rd(encoder, 0x0000, dpcd, 4);
  360. if (ret)
  361. return false;
  362. NV_DEBUG_KMS(dev, "encoder: link_bw %d, link_nr %d\n"
  363. "display: link_bw %d, link_nr %d version 0x%02x\n",
  364. nv_encoder->dcb->dpconf.link_bw,
  365. nv_encoder->dcb->dpconf.link_nr,
  366. dpcd[1], dpcd[2] & 0x0f, dpcd[0]);
  367. nv_encoder->dp.dpcd_version = dpcd[0];
  368. nv_encoder->dp.link_bw = dpcd[1];
  369. if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62 &&
  370. !nv_encoder->dcb->dpconf.link_bw)
  371. nv_encoder->dp.link_bw = DP_LINK_BW_1_62;
  372. nv_encoder->dp.link_nr = dpcd[2] & 0xf;
  373. if (nv_encoder->dp.link_nr > nv_encoder->dcb->dpconf.link_nr)
  374. nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
  375. return true;
  376. }
  377. int
  378. nouveau_dp_auxch(struct nouveau_i2c_chan *auxch, int cmd, int addr,
  379. uint8_t *data, int data_nr)
  380. {
  381. struct drm_device *dev = auxch->dev;
  382. uint32_t tmp, ctrl, stat = 0, data32[4] = {};
  383. int ret = 0, i, index = auxch->rd;
  384. NV_DEBUG_KMS(dev, "ch %d cmd %d addr 0x%x len %d\n", index, cmd, addr, data_nr);
  385. tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));
  386. nv_wr32(dev, NV50_AUXCH_CTRL(auxch->rd), tmp | 0x00100000);
  387. tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));
  388. if (!(tmp & 0x01000000)) {
  389. NV_ERROR(dev, "expected bit 24 == 1, got 0x%08x\n", tmp);
  390. ret = -EIO;
  391. goto out;
  392. }
  393. for (i = 0; i < 3; i++) {
  394. tmp = nv_rd32(dev, NV50_AUXCH_STAT(auxch->rd));
  395. if (tmp & NV50_AUXCH_STAT_STATE_READY)
  396. break;
  397. udelay(100);
  398. }
  399. if (i == 3) {
  400. ret = -EBUSY;
  401. goto out;
  402. }
  403. if (!(cmd & 1)) {
  404. memcpy(data32, data, data_nr);
  405. for (i = 0; i < 4; i++) {
  406. NV_DEBUG_KMS(dev, "wr %d: 0x%08x\n", i, data32[i]);
  407. nv_wr32(dev, NV50_AUXCH_DATA_OUT(index, i), data32[i]);
  408. }
  409. }
  410. nv_wr32(dev, NV50_AUXCH_ADDR(index), addr);
  411. ctrl = nv_rd32(dev, NV50_AUXCH_CTRL(index));
  412. ctrl &= ~(NV50_AUXCH_CTRL_CMD | NV50_AUXCH_CTRL_LEN);
  413. ctrl |= (cmd << NV50_AUXCH_CTRL_CMD_SHIFT);
  414. ctrl |= ((data_nr - 1) << NV50_AUXCH_CTRL_LEN_SHIFT);
  415. for (i = 0; i < 16; i++) {
  416. nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl | 0x80000000);
  417. nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl);
  418. nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl | 0x00010000);
  419. if (!nv_wait(NV50_AUXCH_CTRL(index), 0x00010000, 0x00000000)) {
  420. NV_ERROR(dev, "expected bit 16 == 0, got 0x%08x\n",
  421. nv_rd32(dev, NV50_AUXCH_CTRL(index)));
  422. ret = -EBUSY;
  423. goto out;
  424. }
  425. udelay(400);
  426. stat = nv_rd32(dev, NV50_AUXCH_STAT(index));
  427. if ((stat & NV50_AUXCH_STAT_REPLY_AUX) !=
  428. NV50_AUXCH_STAT_REPLY_AUX_DEFER)
  429. break;
  430. }
  431. if (i == 16) {
  432. NV_ERROR(dev, "auxch DEFER too many times, bailing\n");
  433. ret = -EREMOTEIO;
  434. goto out;
  435. }
  436. if (cmd & 1) {
  437. if ((stat & NV50_AUXCH_STAT_COUNT) != data_nr) {
  438. ret = -EREMOTEIO;
  439. goto out;
  440. }
  441. for (i = 0; i < 4; i++) {
  442. data32[i] = nv_rd32(dev, NV50_AUXCH_DATA_IN(index, i));
  443. NV_DEBUG_KMS(dev, "rd %d: 0x%08x\n", i, data32[i]);
  444. }
  445. memcpy(data, data32, data_nr);
  446. }
  447. out:
  448. tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));
  449. nv_wr32(dev, NV50_AUXCH_CTRL(auxch->rd), tmp & ~0x00100000);
  450. tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));
  451. if (tmp & 0x01000000) {
  452. NV_ERROR(dev, "expected bit 24 == 0, got 0x%08x\n", tmp);
  453. ret = -EIO;
  454. }
  455. udelay(400);
  456. return ret ? ret : (stat & NV50_AUXCH_STAT_REPLY);
  457. }
  458. int
  459. nouveau_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  460. uint8_t write_byte, uint8_t *read_byte)
  461. {
  462. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  463. struct nouveau_i2c_chan *auxch = (struct nouveau_i2c_chan *)adapter;
  464. struct drm_device *dev = auxch->dev;
  465. int ret = 0, cmd, addr = algo_data->address;
  466. uint8_t *buf;
  467. if (mode == MODE_I2C_READ) {
  468. cmd = AUX_I2C_READ;
  469. buf = read_byte;
  470. } else {
  471. cmd = (mode & MODE_I2C_READ) ? AUX_I2C_READ : AUX_I2C_WRITE;
  472. buf = &write_byte;
  473. }
  474. if (!(mode & MODE_I2C_STOP))
  475. cmd |= AUX_I2C_MOT;
  476. if (mode & MODE_I2C_START)
  477. return 1;
  478. for (;;) {
  479. ret = nouveau_dp_auxch(auxch, cmd, addr, buf, 1);
  480. if (ret < 0)
  481. return ret;
  482. switch (ret & NV50_AUXCH_STAT_REPLY_I2C) {
  483. case NV50_AUXCH_STAT_REPLY_I2C_ACK:
  484. return 1;
  485. case NV50_AUXCH_STAT_REPLY_I2C_NACK:
  486. return -EREMOTEIO;
  487. case NV50_AUXCH_STAT_REPLY_I2C_DEFER:
  488. udelay(100);
  489. break;
  490. default:
  491. NV_ERROR(dev, "invalid auxch status: 0x%08x\n", ret);
  492. return -EREMOTEIO;
  493. }
  494. }
  495. }