atombios_dp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include "drmP.h"
  27. #include "radeon_drm.h"
  28. #include "radeon.h"
  29. #include "atom.h"
  30. #include "atom-bits.h"
  31. #include "drm_dp_helper.h"
  32. /* move these to drm_dp_helper.c/h */
  33. #define DP_LINK_CONFIGURATION_SIZE 9
  34. #define DP_LINK_STATUS_SIZE 6
  35. #define DP_DPCD_SIZE 8
  36. static char *voltage_names[] = {
  37. "0.4V", "0.6V", "0.8V", "1.2V"
  38. };
  39. static char *pre_emph_names[] = {
  40. "0dB", "3.5dB", "6dB", "9.5dB"
  41. };
  42. static char *link_train_names[] = {
  43. "pattern 1", "pattern 2", "idle", "off"
  44. };
  45. static const int dp_clocks[] = {
  46. 54000, // 1 lane, 1.62 Ghz
  47. 90000, // 1 lane, 2.70 Ghz
  48. 108000, // 2 lane, 1.62 Ghz
  49. 180000, // 2 lane, 2.70 Ghz
  50. 216000, // 4 lane, 1.62 Ghz
  51. 360000, // 4 lane, 2.70 Ghz
  52. };
  53. static const int num_dp_clocks = sizeof(dp_clocks) / sizeof(int);
  54. /* common helper functions */
  55. static int dp_lanes_for_mode_clock(u8 dpcd[DP_DPCD_SIZE], int mode_clock)
  56. {
  57. int i;
  58. u8 max_link_bw;
  59. u8 max_lane_count;
  60. if (!dpcd)
  61. return 0;
  62. max_link_bw = dpcd[DP_MAX_LINK_RATE];
  63. max_lane_count = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
  64. switch (max_link_bw) {
  65. case DP_LINK_BW_1_62:
  66. default:
  67. for (i = 0; i < num_dp_clocks; i++) {
  68. if (i % 2)
  69. continue;
  70. switch (max_lane_count) {
  71. case 1:
  72. if (i > 1)
  73. return 0;
  74. break;
  75. case 2:
  76. if (i > 3)
  77. return 0;
  78. break;
  79. case 4:
  80. default:
  81. break;
  82. }
  83. if (dp_clocks[i] > mode_clock) {
  84. if (i < 2)
  85. return 1;
  86. else if (i < 4)
  87. return 2;
  88. else
  89. return 4;
  90. }
  91. }
  92. break;
  93. case DP_LINK_BW_2_7:
  94. for (i = 0; i < num_dp_clocks; i++) {
  95. switch (max_lane_count) {
  96. case 1:
  97. if (i > 1)
  98. return 0;
  99. break;
  100. case 2:
  101. if (i > 3)
  102. return 0;
  103. break;
  104. case 4:
  105. default:
  106. break;
  107. }
  108. if (dp_clocks[i] > mode_clock) {
  109. if (i < 2)
  110. return 1;
  111. else if (i < 4)
  112. return 2;
  113. else
  114. return 4;
  115. }
  116. }
  117. break;
  118. }
  119. return 0;
  120. }
  121. static int dp_link_clock_for_mode_clock(u8 dpcd[DP_DPCD_SIZE], int mode_clock)
  122. {
  123. int i;
  124. u8 max_link_bw;
  125. u8 max_lane_count;
  126. if (!dpcd)
  127. return 0;
  128. max_link_bw = dpcd[DP_MAX_LINK_RATE];
  129. max_lane_count = dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
  130. switch (max_link_bw) {
  131. case DP_LINK_BW_1_62:
  132. default:
  133. for (i = 0; i < num_dp_clocks; i++) {
  134. if (i % 2)
  135. continue;
  136. switch (max_lane_count) {
  137. case 1:
  138. if (i > 1)
  139. return 0;
  140. break;
  141. case 2:
  142. if (i > 3)
  143. return 0;
  144. break;
  145. case 4:
  146. default:
  147. break;
  148. }
  149. if (dp_clocks[i] > mode_clock)
  150. return 162000;
  151. }
  152. break;
  153. case DP_LINK_BW_2_7:
  154. for (i = 0; i < num_dp_clocks; i++) {
  155. switch (max_lane_count) {
  156. case 1:
  157. if (i > 1)
  158. return 0;
  159. break;
  160. case 2:
  161. if (i > 3)
  162. return 0;
  163. break;
  164. case 4:
  165. default:
  166. break;
  167. }
  168. if (dp_clocks[i] > mode_clock)
  169. return (i % 2) ? 270000 : 162000;
  170. }
  171. }
  172. return 0;
  173. }
  174. int dp_mode_valid(u8 dpcd[DP_DPCD_SIZE], int mode_clock)
  175. {
  176. int lanes = dp_lanes_for_mode_clock(dpcd, mode_clock);
  177. int bw = dp_lanes_for_mode_clock(dpcd, mode_clock);
  178. if ((lanes == 0) || (bw == 0))
  179. return MODE_CLOCK_HIGH;
  180. return MODE_OK;
  181. }
  182. static u8 dp_link_status(u8 link_status[DP_LINK_STATUS_SIZE], int r)
  183. {
  184. return link_status[r - DP_LANE0_1_STATUS];
  185. }
  186. static u8 dp_get_lane_status(u8 link_status[DP_LINK_STATUS_SIZE],
  187. int lane)
  188. {
  189. int i = DP_LANE0_1_STATUS + (lane >> 1);
  190. int s = (lane & 1) * 4;
  191. u8 l = dp_link_status(link_status, i);
  192. return (l >> s) & 0xf;
  193. }
  194. static bool dp_clock_recovery_ok(u8 link_status[DP_LINK_STATUS_SIZE],
  195. int lane_count)
  196. {
  197. int lane;
  198. u8 lane_status;
  199. for (lane = 0; lane < lane_count; lane++) {
  200. lane_status = dp_get_lane_status(link_status, lane);
  201. if ((lane_status & DP_LANE_CR_DONE) == 0)
  202. return false;
  203. }
  204. return true;
  205. }
  206. static bool dp_channel_eq_ok(u8 link_status[DP_LINK_STATUS_SIZE],
  207. int lane_count)
  208. {
  209. u8 lane_align;
  210. u8 lane_status;
  211. int lane;
  212. lane_align = dp_link_status(link_status,
  213. DP_LANE_ALIGN_STATUS_UPDATED);
  214. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  215. return false;
  216. for (lane = 0; lane < lane_count; lane++) {
  217. lane_status = dp_get_lane_status(link_status, lane);
  218. if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
  219. return false;
  220. }
  221. return true;
  222. }
  223. static u8 dp_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
  224. int lane)
  225. {
  226. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  227. int s = ((lane & 1) ?
  228. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  229. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  230. u8 l = dp_link_status(link_status, i);
  231. return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  232. }
  233. static u8 dp_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
  234. int lane)
  235. {
  236. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  237. int s = ((lane & 1) ?
  238. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  239. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  240. u8 l = dp_link_status(link_status, i);
  241. return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  242. }
  243. /* XXX fix me -- chip specific */
  244. #define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_1200
  245. static u8 dp_pre_emphasis_max(u8 voltage_swing)
  246. {
  247. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  248. case DP_TRAIN_VOLTAGE_SWING_400:
  249. return DP_TRAIN_PRE_EMPHASIS_6;
  250. case DP_TRAIN_VOLTAGE_SWING_600:
  251. return DP_TRAIN_PRE_EMPHASIS_6;
  252. case DP_TRAIN_VOLTAGE_SWING_800:
  253. return DP_TRAIN_PRE_EMPHASIS_3_5;
  254. case DP_TRAIN_VOLTAGE_SWING_1200:
  255. default:
  256. return DP_TRAIN_PRE_EMPHASIS_0;
  257. }
  258. }
  259. static void dp_get_adjust_train(u8 link_status[DP_LINK_STATUS_SIZE],
  260. int lane_count,
  261. u8 train_set[4])
  262. {
  263. u8 v = 0;
  264. u8 p = 0;
  265. int lane;
  266. for (lane = 0; lane < lane_count; lane++) {
  267. u8 this_v = dp_get_adjust_request_voltage(link_status, lane);
  268. u8 this_p = dp_get_adjust_request_pre_emphasis(link_status, lane);
  269. DRM_INFO("requested signal parameters: lane %d voltage %s pre_emph %s\n",
  270. lane,
  271. voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
  272. pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
  273. if (this_v > v)
  274. v = this_v;
  275. if (this_p > p)
  276. p = this_p;
  277. }
  278. if (v >= DP_VOLTAGE_MAX)
  279. v = DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
  280. if (p >= dp_pre_emphasis_max(v))
  281. p = dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  282. DRM_INFO("using signal parameters: voltage %s pre_emph %s\n",
  283. voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
  284. pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
  285. for (lane = 0; lane < 4; lane++)
  286. train_set[lane] = v | p;
  287. }
  288. /* radeon aux chan functions */
  289. bool radeon_process_aux_ch(struct radeon_i2c_chan *chan, u8 *req_bytes,
  290. int num_bytes, u8 *read_byte,
  291. u8 read_buf_len, u8 delay)
  292. {
  293. struct drm_device *dev = chan->dev;
  294. struct radeon_device *rdev = dev->dev_private;
  295. PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION args;
  296. int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
  297. unsigned char *base;
  298. memset(&args, 0, sizeof(args));
  299. base = (unsigned char *)rdev->mode_info.atom_context->scratch;
  300. memcpy(base, req_bytes, num_bytes);
  301. args.lpAuxRequest = 0;
  302. args.lpDataOut = 16;
  303. args.ucDataOutLen = 0;
  304. args.ucChannelID = chan->rec.i2c_id;
  305. args.ucDelay = delay / 10;
  306. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  307. if (args.ucReplyStatus) {
  308. DRM_ERROR("failed to get auxch %02x%02x %02x %02x 0x%02x %02x\n",
  309. req_bytes[1], req_bytes[0], req_bytes[2], req_bytes[3],
  310. chan->rec.i2c_id, args.ucReplyStatus);
  311. return false;
  312. }
  313. if (args.ucDataOutLen && read_byte && read_buf_len) {
  314. if (read_buf_len < args.ucDataOutLen) {
  315. DRM_ERROR("Buffer to small for return answer %d %d\n",
  316. read_buf_len, args.ucDataOutLen);
  317. return false;
  318. }
  319. {
  320. int len = min(read_buf_len, args.ucDataOutLen);
  321. memcpy(read_byte, base + 16, len);
  322. }
  323. }
  324. return true;
  325. }
  326. bool radeon_dp_aux_native_write(struct radeon_connector *radeon_connector, uint16_t address,
  327. uint8_t send_bytes, uint8_t *send)
  328. {
  329. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  330. u8 msg[20];
  331. u8 msg_len, dp_msg_len;
  332. bool ret;
  333. dp_msg_len = 4;
  334. msg[0] = address;
  335. msg[1] = address >> 8;
  336. msg[2] = AUX_NATIVE_WRITE << 4;
  337. dp_msg_len += send_bytes;
  338. msg[3] = (dp_msg_len << 4) | (send_bytes - 1);
  339. if (send_bytes > 16)
  340. return false;
  341. memcpy(&msg[4], send, send_bytes);
  342. msg_len = 4 + send_bytes;
  343. ret = radeon_process_aux_ch(dig_connector->dp_i2c_bus, msg, msg_len, NULL, 0, 0);
  344. return ret;
  345. }
  346. bool radeon_dp_aux_native_read(struct radeon_connector *radeon_connector, uint16_t address,
  347. uint8_t delay, uint8_t expected_bytes,
  348. uint8_t *read_p)
  349. {
  350. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  351. u8 msg[20];
  352. u8 msg_len, dp_msg_len;
  353. bool ret = false;
  354. msg_len = 4;
  355. dp_msg_len = 4;
  356. msg[0] = address;
  357. msg[1] = address >> 8;
  358. msg[2] = AUX_NATIVE_READ << 4;
  359. msg[3] = (dp_msg_len) << 4;
  360. msg[3] |= expected_bytes - 1;
  361. ret = radeon_process_aux_ch(dig_connector->dp_i2c_bus, msg, msg_len, read_p, expected_bytes, delay);
  362. return ret;
  363. }
  364. /* radeon dp functions */
  365. static u8 radeon_dp_encoder_service(struct radeon_device *rdev, int action, int dp_clock,
  366. uint8_t ucconfig, uint8_t lane_num)
  367. {
  368. DP_ENCODER_SERVICE_PARAMETERS args;
  369. int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
  370. memset(&args, 0, sizeof(args));
  371. args.ucLinkClock = dp_clock / 10;
  372. args.ucConfig = ucconfig;
  373. args.ucAction = action;
  374. args.ucLaneNum = lane_num;
  375. args.ucStatus = 0;
  376. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  377. return args.ucStatus;
  378. }
  379. u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
  380. {
  381. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  382. struct drm_device *dev = radeon_connector->base.dev;
  383. struct radeon_device *rdev = dev->dev_private;
  384. return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
  385. dig_connector->dp_i2c_bus->rec.i2c_id, 0);
  386. }
  387. void radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
  388. {
  389. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  390. u8 msg[25];
  391. int ret;
  392. ret = radeon_dp_aux_native_read(radeon_connector, DP_DPCD_REV, 0, 8, msg);
  393. if (ret) {
  394. memcpy(dig_connector->dpcd, msg, 8);
  395. {
  396. int i;
  397. printk("DPCD: ");
  398. for (i = 0; i < 8; i++)
  399. printk("%02x ", msg[i]);
  400. printk("\n");
  401. }
  402. }
  403. dig_connector->dpcd[0] = 0;
  404. return;
  405. }
  406. void radeon_dp_set_link_config(struct drm_connector *connector,
  407. struct drm_display_mode *mode)
  408. {
  409. struct radeon_connector *radeon_connector;
  410. struct radeon_connector_atom_dig *dig_connector;
  411. if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
  412. return;
  413. radeon_connector = to_radeon_connector(connector);
  414. if (!radeon_connector->con_priv)
  415. return;
  416. dig_connector = radeon_connector->con_priv;
  417. dig_connector->dp_clock =
  418. dp_link_clock_for_mode_clock(dig_connector->dpcd, mode->clock);
  419. dig_connector->dp_lane_count =
  420. dp_lanes_for_mode_clock(dig_connector->dpcd, mode->clock);
  421. }
  422. int radeon_dp_mode_valid_helper(struct radeon_connector *radeon_connector,
  423. struct drm_display_mode *mode)
  424. {
  425. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  426. return dp_mode_valid(dig_connector->dpcd, mode->clock);
  427. }
  428. static bool atom_dp_get_link_status(struct radeon_connector *radeon_connector,
  429. u8 link_status[DP_LINK_STATUS_SIZE])
  430. {
  431. int ret;
  432. ret = radeon_dp_aux_native_read(radeon_connector, DP_LANE0_1_STATUS, 100,
  433. DP_LINK_STATUS_SIZE, link_status);
  434. if (!ret) {
  435. DRM_ERROR("displayport link status failed\n");
  436. return false;
  437. }
  438. DRM_INFO("link status %02x %02x %02x %02x %02x %02x\n",
  439. link_status[0], link_status[1], link_status[2],
  440. link_status[3], link_status[4], link_status[5]);
  441. return true;
  442. }
  443. static void dp_set_power(struct radeon_connector *radeon_connector, u8 power_state)
  444. {
  445. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  446. if (dig_connector->dpcd[0] >= 0x11) {
  447. radeon_dp_aux_native_write(radeon_connector, DP_SET_POWER, 1,
  448. &power_state);
  449. }
  450. }
  451. static void dp_set_downspread(struct radeon_connector *radeon_connector, u8 downspread)
  452. {
  453. radeon_dp_aux_native_write(radeon_connector, DP_DOWNSPREAD_CTRL, 1,
  454. &downspread);
  455. }
  456. static void dp_set_link_bw_lanes(struct radeon_connector *radeon_connector,
  457. u8 link_configuration[DP_LINK_CONFIGURATION_SIZE])
  458. {
  459. radeon_dp_aux_native_write(radeon_connector, DP_LINK_BW_SET, 2,
  460. link_configuration);
  461. }
  462. static void dp_update_dpvs_emph(struct radeon_connector *radeon_connector,
  463. struct drm_encoder *encoder,
  464. u8 train_set[4])
  465. {
  466. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  467. int i;
  468. for (i = 0; i < dig_connector->dp_lane_count; i++)
  469. atombios_dig_transmitter_setup(encoder,
  470. ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
  471. i, train_set[i]);
  472. radeon_dp_aux_native_write(radeon_connector, DP_TRAINING_LANE0_SET,
  473. dig_connector->dp_lane_count, train_set);
  474. }
  475. static void dp_set_training(struct radeon_connector *radeon_connector,
  476. u8 training)
  477. {
  478. radeon_dp_aux_native_write(radeon_connector, DP_TRAINING_PATTERN_SET,
  479. 1, &training);
  480. }
  481. void dp_link_train(struct drm_encoder *encoder,
  482. struct drm_connector *connector)
  483. {
  484. struct drm_device *dev = encoder->dev;
  485. struct radeon_device *rdev = dev->dev_private;
  486. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  487. struct radeon_encoder_atom_dig *dig;
  488. struct radeon_connector *radeon_connector;
  489. struct radeon_connector_atom_dig *dig_connector;
  490. int enc_id = 0;
  491. bool clock_recovery, channel_eq;
  492. u8 link_status[DP_LINK_STATUS_SIZE];
  493. u8 link_configuration[DP_LINK_CONFIGURATION_SIZE];
  494. u8 tries, voltage;
  495. u8 train_set[4];
  496. int i;
  497. if (connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort)
  498. return;
  499. if (!radeon_encoder->enc_priv)
  500. return;
  501. dig = radeon_encoder->enc_priv;
  502. radeon_connector = to_radeon_connector(connector);
  503. if (!radeon_connector->con_priv)
  504. return;
  505. dig_connector = radeon_connector->con_priv;
  506. if (ASIC_IS_DCE32(rdev)) {
  507. if (dig->dig_block)
  508. enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
  509. else
  510. enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
  511. if (dig_connector->linkb)
  512. enc_id |= ATOM_DP_CONFIG_LINK_B;
  513. else
  514. enc_id |= ATOM_DP_CONFIG_LINK_A;
  515. } else {
  516. if (dig_connector->linkb)
  517. enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER | ATOM_DP_CONFIG_LINK_B;
  518. else
  519. enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER | ATOM_DP_CONFIG_LINK_A;
  520. }
  521. memset(link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  522. if (dig_connector->dp_clock == 270000)
  523. link_configuration[0] = DP_LINK_BW_2_7;
  524. else
  525. link_configuration[0] = DP_LINK_BW_1_62;
  526. link_configuration[1] = dig_connector->dp_lane_count;
  527. if (dig_connector->dpcd[0] >= 0x11)
  528. link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  529. /* power up the sink */
  530. dp_set_power(radeon_connector, DP_SET_POWER_D0);
  531. /* disable the training pattern on the sink */
  532. dp_set_training(radeon_connector, DP_TRAINING_PATTERN_DISABLE);
  533. /* set link bw and lanes on the sink */
  534. dp_set_link_bw_lanes(radeon_connector, link_configuration);
  535. /* disable downspread on the sink */
  536. dp_set_downspread(radeon_connector, 0);
  537. /* start training on the source */
  538. radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_START,
  539. dig_connector->dp_clock, enc_id, 0);
  540. /* set training pattern 1 on the source */
  541. radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
  542. dig_connector->dp_clock, enc_id, 0);
  543. /* set initial vs/emph */
  544. memset(train_set, 0, 4);
  545. dp_update_dpvs_emph(radeon_connector, encoder, train_set);
  546. udelay(400);
  547. /* set training pattern 1 on the sink */
  548. dp_set_training(radeon_connector, DP_TRAINING_PATTERN_1);
  549. /* clock recovery loop */
  550. clock_recovery = false;
  551. tries = 0;
  552. voltage = 0xff;
  553. for (;;) {
  554. udelay(100);
  555. if (!atom_dp_get_link_status(radeon_connector, link_status))
  556. break;
  557. if (dp_clock_recovery_ok(link_status, dig_connector->dp_lane_count)) {
  558. clock_recovery = true;
  559. break;
  560. }
  561. for (i = 0; i < dig_connector->dp_lane_count; i++) {
  562. if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  563. break;
  564. }
  565. if (i == dig_connector->dp_lane_count) {
  566. DRM_ERROR("clock recovery reached max voltage\n");
  567. break;
  568. }
  569. if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  570. ++tries;
  571. if (tries == 5) {
  572. DRM_ERROR("clock recovery tried 5 times\n");
  573. break;
  574. }
  575. } else
  576. tries = 0;
  577. voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  578. /* Compute new train_set as requested by sink */
  579. dp_get_adjust_train(link_status, dig_connector->dp_lane_count, train_set);
  580. dp_update_dpvs_emph(radeon_connector, encoder, train_set);
  581. }
  582. if (!clock_recovery)
  583. DRM_ERROR("clock recovery failed\n");
  584. else
  585. DRM_INFO("clock recovery at voltage %d pre-emphasis %d\n",
  586. train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  587. (train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
  588. DP_TRAIN_PRE_EMPHASIS_SHIFT);
  589. /* set training pattern 2 on the sink */
  590. dp_set_training(radeon_connector, DP_TRAINING_PATTERN_2);
  591. /* set training pattern 2 on the source */
  592. radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
  593. dig_connector->dp_clock, enc_id, 1);
  594. /* channel equalization loop */
  595. tries = 0;
  596. channel_eq = false;
  597. for (;;) {
  598. udelay(400);
  599. if (!atom_dp_get_link_status(radeon_connector, link_status))
  600. break;
  601. if (dp_channel_eq_ok(link_status, dig_connector->dp_lane_count)) {
  602. channel_eq = true;
  603. break;
  604. }
  605. /* Try 5 times */
  606. if (tries > 5) {
  607. DRM_ERROR("channel eq failed: 5 tries\n");
  608. break;
  609. }
  610. /* Compute new train_set as requested by sink */
  611. dp_get_adjust_train(link_status, dig_connector->dp_lane_count, train_set);
  612. dp_update_dpvs_emph(radeon_connector, encoder, train_set);
  613. tries++;
  614. }
  615. if (!channel_eq)
  616. DRM_ERROR("channel eq failed\n");
  617. else
  618. DRM_INFO("channel eq at voltage %d pre-emphasis %d\n",
  619. train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  620. (train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
  621. >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
  622. /* disable the training pattern on the sink */
  623. dp_set_training(radeon_connector, DP_TRAINING_PATTERN_DISABLE);
  624. radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
  625. dig_connector->dp_clock, enc_id, 0);
  626. }
  627. int radeon_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  628. uint8_t write_byte, uint8_t *read_byte)
  629. {
  630. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  631. struct radeon_i2c_chan *auxch = (struct radeon_i2c_chan *)adapter;
  632. int ret = 0;
  633. uint16_t address = algo_data->address;
  634. uint8_t msg[5];
  635. uint8_t reply[2];
  636. int msg_len, dp_msg_len;
  637. int reply_bytes;
  638. /* Set up the command byte */
  639. if (mode & MODE_I2C_READ)
  640. msg[2] = AUX_I2C_READ << 4;
  641. else
  642. msg[2] = AUX_I2C_WRITE << 4;
  643. if (!(mode & MODE_I2C_STOP))
  644. msg[2] |= AUX_I2C_MOT << 4;
  645. msg[0] = address;
  646. msg[1] = address >> 8;
  647. reply_bytes = 1;
  648. msg_len = 4;
  649. dp_msg_len = 3;
  650. switch (mode) {
  651. case MODE_I2C_WRITE:
  652. msg[4] = write_byte;
  653. msg_len++;
  654. dp_msg_len += 2;
  655. break;
  656. case MODE_I2C_READ:
  657. dp_msg_len += 1;
  658. break;
  659. default:
  660. break;
  661. }
  662. msg[3] = (dp_msg_len) << 4;
  663. ret = radeon_process_aux_ch(auxch, msg, msg_len, reply, reply_bytes, 0);
  664. if (ret) {
  665. if (read_byte)
  666. *read_byte = reply[0];
  667. return reply_bytes;
  668. }
  669. return -EREMOTEIO;
  670. }