atombios_dp.c 21 KB

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