nouveau_dp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. uint8_t config[4];
  230. uint8_t status[3];
  231. bool cr_done, cr_max_vs, eq_done;
  232. int ret = 0, i, tries, voltage;
  233. NV_DEBUG_KMS(dev, "link training!!\n");
  234. train:
  235. cr_done = eq_done = false;
  236. /* set link configuration */
  237. NV_DEBUG_KMS(dev, "\tbegin train: bw %d, lanes %d\n",
  238. nv_encoder->dp.link_bw, nv_encoder->dp.link_nr);
  239. ret = nouveau_dp_link_bw_set(encoder, nv_encoder->dp.link_bw);
  240. if (ret)
  241. return false;
  242. config[0] = nv_encoder->dp.link_nr;
  243. if (nv_encoder->dp.dpcd_version >= 0x11)
  244. config[0] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  245. ret = nouveau_dp_lane_count_set(encoder, config[0]);
  246. if (ret)
  247. return false;
  248. /* clock recovery */
  249. NV_DEBUG_KMS(dev, "\tbegin cr\n");
  250. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_1);
  251. if (ret)
  252. goto stop;
  253. tries = 0;
  254. voltage = -1;
  255. memset(config, 0x00, sizeof(config));
  256. for (;;) {
  257. if (!nouveau_dp_link_train_commit(encoder, config))
  258. break;
  259. udelay(100);
  260. ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 2);
  261. if (ret)
  262. break;
  263. NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",
  264. status[0], status[1]);
  265. cr_done = true;
  266. cr_max_vs = false;
  267. for (i = 0; i < nv_encoder->dp.link_nr; i++) {
  268. int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;
  269. if (!(lane & DP_LANE_CR_DONE)) {
  270. cr_done = false;
  271. if (config[i] & DP_TRAIN_MAX_PRE_EMPHASIS_REACHED)
  272. cr_max_vs = true;
  273. break;
  274. }
  275. }
  276. if ((config[0] & DP_TRAIN_VOLTAGE_SWING_MASK) != voltage) {
  277. voltage = config[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  278. tries = 0;
  279. }
  280. if (cr_done || cr_max_vs || (++tries == 5))
  281. break;
  282. if (!nouveau_dp_link_train_adjust(encoder, config))
  283. break;
  284. }
  285. if (!cr_done)
  286. goto stop;
  287. /* channel equalisation */
  288. NV_DEBUG_KMS(dev, "\tbegin eq\n");
  289. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_2);
  290. if (ret)
  291. goto stop;
  292. for (tries = 0; tries <= 5; tries++) {
  293. udelay(400);
  294. ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 3);
  295. if (ret)
  296. break;
  297. NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",
  298. status[0], status[1]);
  299. eq_done = true;
  300. if (!(status[2] & DP_INTERLANE_ALIGN_DONE))
  301. eq_done = false;
  302. for (i = 0; eq_done && i < nv_encoder->dp.link_nr; i++) {
  303. int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;
  304. if (!(lane & DP_LANE_CR_DONE)) {
  305. cr_done = false;
  306. break;
  307. }
  308. if (!(lane & DP_LANE_CHANNEL_EQ_DONE) ||
  309. !(lane & DP_LANE_SYMBOL_LOCKED)) {
  310. eq_done = false;
  311. break;
  312. }
  313. }
  314. if (eq_done || !cr_done)
  315. break;
  316. if (!nouveau_dp_link_train_adjust(encoder, config) ||
  317. !nouveau_dp_link_train_commit(encoder, config))
  318. break;
  319. }
  320. stop:
  321. /* end link training */
  322. ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_DISABLE);
  323. if (ret)
  324. return false;
  325. /* retry at a lower setting, if possible */
  326. if (!ret && !(eq_done && cr_done)) {
  327. NV_DEBUG_KMS(dev, "\twe failed\n");
  328. if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62) {
  329. NV_DEBUG_KMS(dev, "retry link training at low rate\n");
  330. nv_encoder->dp.link_bw = DP_LINK_BW_1_62;
  331. goto train;
  332. }
  333. }
  334. return eq_done;
  335. }
  336. bool
  337. nouveau_dp_detect(struct drm_encoder *encoder)
  338. {
  339. struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);
  340. struct drm_device *dev = encoder->dev;
  341. uint8_t dpcd[4];
  342. int ret;
  343. ret = auxch_rd(encoder, 0x0000, dpcd, 4);
  344. if (ret)
  345. return false;
  346. NV_DEBUG_KMS(dev, "encoder: link_bw %d, link_nr %d\n"
  347. "display: link_bw %d, link_nr %d version 0x%02x\n",
  348. nv_encoder->dcb->dpconf.link_bw,
  349. nv_encoder->dcb->dpconf.link_nr,
  350. dpcd[1], dpcd[2] & 0x0f, dpcd[0]);
  351. nv_encoder->dp.dpcd_version = dpcd[0];
  352. nv_encoder->dp.link_bw = dpcd[1];
  353. if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62 &&
  354. !nv_encoder->dcb->dpconf.link_bw)
  355. nv_encoder->dp.link_bw = DP_LINK_BW_1_62;
  356. nv_encoder->dp.link_nr = dpcd[2] & 0xf;
  357. if (nv_encoder->dp.link_nr > nv_encoder->dcb->dpconf.link_nr)
  358. nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;
  359. return true;
  360. }
  361. int
  362. nouveau_dp_auxch(struct nouveau_i2c_chan *auxch, int cmd, int addr,
  363. uint8_t *data, int data_nr)
  364. {
  365. struct drm_device *dev = auxch->dev;
  366. uint32_t tmp, ctrl, stat = 0, data32[4] = {};
  367. int ret = 0, i, index = auxch->rd;
  368. NV_DEBUG_KMS(dev, "ch %d cmd %d addr 0x%x len %d\n", index, cmd, addr, data_nr);
  369. tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));
  370. nv_wr32(dev, NV50_AUXCH_CTRL(auxch->rd), tmp | 0x00100000);
  371. tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));
  372. if (!(tmp & 0x01000000)) {
  373. NV_ERROR(dev, "expected bit 24 == 1, got 0x%08x\n", tmp);
  374. ret = -EIO;
  375. goto out;
  376. }
  377. for (i = 0; i < 3; i++) {
  378. tmp = nv_rd32(dev, NV50_AUXCH_STAT(auxch->rd));
  379. if (tmp & NV50_AUXCH_STAT_STATE_READY)
  380. break;
  381. udelay(100);
  382. }
  383. if (i == 3) {
  384. ret = -EBUSY;
  385. goto out;
  386. }
  387. if (!(cmd & 1)) {
  388. memcpy(data32, data, data_nr);
  389. for (i = 0; i < 4; i++) {
  390. NV_DEBUG_KMS(dev, "wr %d: 0x%08x\n", i, data32[i]);
  391. nv_wr32(dev, NV50_AUXCH_DATA_OUT(index, i), data32[i]);
  392. }
  393. }
  394. nv_wr32(dev, NV50_AUXCH_ADDR(index), addr);
  395. ctrl = nv_rd32(dev, NV50_AUXCH_CTRL(index));
  396. ctrl &= ~(NV50_AUXCH_CTRL_CMD | NV50_AUXCH_CTRL_LEN);
  397. ctrl |= (cmd << NV50_AUXCH_CTRL_CMD_SHIFT);
  398. ctrl |= ((data_nr - 1) << NV50_AUXCH_CTRL_LEN_SHIFT);
  399. for (;;) {
  400. nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl | 0x80000000);
  401. nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl);
  402. nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl | 0x00010000);
  403. if (!nv_wait(NV50_AUXCH_CTRL(index), 0x00010000, 0x00000000)) {
  404. NV_ERROR(dev, "expected bit 16 == 0, got 0x%08x\n",
  405. nv_rd32(dev, NV50_AUXCH_CTRL(index)));
  406. return -EBUSY;
  407. }
  408. udelay(400);
  409. stat = nv_rd32(dev, NV50_AUXCH_STAT(index));
  410. if ((stat & NV50_AUXCH_STAT_REPLY_AUX) !=
  411. NV50_AUXCH_STAT_REPLY_AUX_DEFER)
  412. break;
  413. }
  414. if (cmd & 1) {
  415. for (i = 0; i < 4; i++) {
  416. data32[i] = nv_rd32(dev, NV50_AUXCH_DATA_IN(index, i));
  417. NV_DEBUG_KMS(dev, "rd %d: 0x%08x\n", i, data32[i]);
  418. }
  419. memcpy(data, data32, data_nr);
  420. }
  421. out:
  422. tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));
  423. nv_wr32(dev, NV50_AUXCH_CTRL(auxch->rd), tmp & ~0x00100000);
  424. tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));
  425. if (tmp & 0x01000000) {
  426. NV_ERROR(dev, "expected bit 24 == 0, got 0x%08x\n", tmp);
  427. ret = -EIO;
  428. }
  429. udelay(400);
  430. return ret ? ret : (stat & NV50_AUXCH_STAT_REPLY);
  431. }
  432. int
  433. nouveau_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  434. uint8_t write_byte, uint8_t *read_byte)
  435. {
  436. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  437. struct nouveau_i2c_chan *auxch = (struct nouveau_i2c_chan *)adapter;
  438. struct drm_device *dev = auxch->dev;
  439. int ret = 0, cmd, addr = algo_data->address;
  440. uint8_t *buf;
  441. if (mode == MODE_I2C_READ) {
  442. cmd = AUX_I2C_READ;
  443. buf = read_byte;
  444. } else {
  445. cmd = (mode & MODE_I2C_READ) ? AUX_I2C_READ : AUX_I2C_WRITE;
  446. buf = &write_byte;
  447. }
  448. if (!(mode & MODE_I2C_STOP))
  449. cmd |= AUX_I2C_MOT;
  450. if (mode & MODE_I2C_START)
  451. return 1;
  452. for (;;) {
  453. ret = nouveau_dp_auxch(auxch, cmd, addr, buf, 1);
  454. if (ret < 0)
  455. return ret;
  456. switch (ret & NV50_AUXCH_STAT_REPLY_I2C) {
  457. case NV50_AUXCH_STAT_REPLY_I2C_ACK:
  458. return 1;
  459. case NV50_AUXCH_STAT_REPLY_I2C_NACK:
  460. return -EREMOTEIO;
  461. case NV50_AUXCH_STAT_REPLY_I2C_DEFER:
  462. udelay(100);
  463. break;
  464. default:
  465. NV_ERROR(dev, "invalid auxch status: 0x%08x\n", ret);
  466. return -EREMOTEIO;
  467. }
  468. }
  469. }