atombios_dp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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_KMS("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_KMS("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. union aux_channel_transaction {
  286. PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION v1;
  287. PROCESS_AUX_CHANNEL_TRANSACTION_PARAMETERS_V2 v2;
  288. };
  289. /* radeon aux chan functions */
  290. bool radeon_process_aux_ch(struct radeon_i2c_chan *chan, u8 *req_bytes,
  291. int num_bytes, u8 *read_byte,
  292. u8 read_buf_len, u8 delay)
  293. {
  294. struct drm_device *dev = chan->dev;
  295. struct radeon_device *rdev = dev->dev_private;
  296. union aux_channel_transaction args;
  297. int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
  298. unsigned char *base;
  299. int retry_count = 0;
  300. memset(&args, 0, sizeof(args));
  301. base = (unsigned char *)rdev->mode_info.atom_context->scratch;
  302. retry:
  303. memcpy(base, req_bytes, num_bytes);
  304. args.v1.lpAuxRequest = 0;
  305. args.v1.lpDataOut = 16;
  306. args.v1.ucDataOutLen = 0;
  307. args.v1.ucChannelID = chan->rec.i2c_id;
  308. args.v1.ucDelay = delay / 10;
  309. if (ASIC_IS_DCE4(rdev))
  310. args.v2.ucHPD_ID = chan->rec.hpd;
  311. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  312. if (args.v1.ucReplyStatus && !args.v1.ucDataOutLen) {
  313. if (args.v1.ucReplyStatus == 0x20 && retry_count++ < 10)
  314. goto retry;
  315. DRM_DEBUG_KMS("failed to get auxch %02x%02x %02x %02x 0x%02x %02x after %d retries\n",
  316. req_bytes[1], req_bytes[0], req_bytes[2], req_bytes[3],
  317. chan->rec.i2c_id, args.v1.ucReplyStatus, retry_count);
  318. return false;
  319. }
  320. if (args.v1.ucDataOutLen && read_byte && read_buf_len) {
  321. if (read_buf_len < args.v1.ucDataOutLen) {
  322. DRM_ERROR("Buffer to small for return answer %d %d\n",
  323. read_buf_len, args.v1.ucDataOutLen);
  324. return false;
  325. }
  326. {
  327. int len = min(read_buf_len, args.v1.ucDataOutLen);
  328. memcpy(read_byte, base + 16, len);
  329. }
  330. }
  331. return true;
  332. }
  333. bool radeon_dp_aux_native_write(struct radeon_connector *radeon_connector, uint16_t address,
  334. uint8_t send_bytes, uint8_t *send)
  335. {
  336. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  337. u8 msg[20];
  338. u8 msg_len, dp_msg_len;
  339. bool ret;
  340. dp_msg_len = 4;
  341. msg[0] = address;
  342. msg[1] = address >> 8;
  343. msg[2] = AUX_NATIVE_WRITE << 4;
  344. dp_msg_len += send_bytes;
  345. msg[3] = (dp_msg_len << 4) | (send_bytes - 1);
  346. if (send_bytes > 16)
  347. return false;
  348. memcpy(&msg[4], send, send_bytes);
  349. msg_len = 4 + send_bytes;
  350. ret = radeon_process_aux_ch(dig_connector->dp_i2c_bus, msg, msg_len, NULL, 0, 0);
  351. return ret;
  352. }
  353. bool radeon_dp_aux_native_read(struct radeon_connector *radeon_connector, uint16_t address,
  354. uint8_t delay, uint8_t expected_bytes,
  355. uint8_t *read_p)
  356. {
  357. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  358. u8 msg[20];
  359. u8 msg_len, dp_msg_len;
  360. bool ret = false;
  361. msg_len = 4;
  362. dp_msg_len = 4;
  363. msg[0] = address;
  364. msg[1] = address >> 8;
  365. msg[2] = AUX_NATIVE_READ << 4;
  366. msg[3] = (dp_msg_len) << 4;
  367. msg[3] |= expected_bytes - 1;
  368. ret = radeon_process_aux_ch(dig_connector->dp_i2c_bus, msg, msg_len, read_p, expected_bytes, delay);
  369. return ret;
  370. }
  371. /* radeon dp functions */
  372. static u8 radeon_dp_encoder_service(struct radeon_device *rdev, int action, int dp_clock,
  373. uint8_t ucconfig, uint8_t lane_num)
  374. {
  375. DP_ENCODER_SERVICE_PARAMETERS args;
  376. int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
  377. memset(&args, 0, sizeof(args));
  378. args.ucLinkClock = dp_clock / 10;
  379. args.ucConfig = ucconfig;
  380. args.ucAction = action;
  381. args.ucLaneNum = lane_num;
  382. args.ucStatus = 0;
  383. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  384. return args.ucStatus;
  385. }
  386. u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
  387. {
  388. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  389. struct drm_device *dev = radeon_connector->base.dev;
  390. struct radeon_device *rdev = dev->dev_private;
  391. return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
  392. dig_connector->dp_i2c_bus->rec.i2c_id, 0);
  393. }
  394. bool radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
  395. {
  396. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  397. u8 msg[25];
  398. int ret;
  399. ret = radeon_dp_aux_native_read(radeon_connector, DP_DPCD_REV, 0, 8, msg);
  400. if (ret) {
  401. memcpy(dig_connector->dpcd, msg, 8);
  402. {
  403. int i;
  404. DRM_DEBUG_KMS("DPCD: ");
  405. for (i = 0; i < 8; i++)
  406. DRM_DEBUG_KMS("%02x ", msg[i]);
  407. DRM_DEBUG_KMS("\n");
  408. }
  409. return true;
  410. }
  411. dig_connector->dpcd[0] = 0;
  412. return false;
  413. }
  414. void radeon_dp_set_link_config(struct drm_connector *connector,
  415. struct drm_display_mode *mode)
  416. {
  417. struct radeon_connector *radeon_connector;
  418. struct radeon_connector_atom_dig *dig_connector;
  419. if ((connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort) &&
  420. (connector->connector_type != DRM_MODE_CONNECTOR_eDP))
  421. return;
  422. radeon_connector = to_radeon_connector(connector);
  423. if (!radeon_connector->con_priv)
  424. return;
  425. dig_connector = radeon_connector->con_priv;
  426. dig_connector->dp_clock =
  427. dp_link_clock_for_mode_clock(dig_connector->dpcd, mode->clock);
  428. dig_connector->dp_lane_count =
  429. dp_lanes_for_mode_clock(dig_connector->dpcd, mode->clock);
  430. }
  431. int radeon_dp_mode_valid_helper(struct radeon_connector *radeon_connector,
  432. struct drm_display_mode *mode)
  433. {
  434. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  435. return dp_mode_valid(dig_connector->dpcd, mode->clock);
  436. }
  437. static bool atom_dp_get_link_status(struct radeon_connector *radeon_connector,
  438. u8 link_status[DP_LINK_STATUS_SIZE])
  439. {
  440. int ret;
  441. ret = radeon_dp_aux_native_read(radeon_connector, DP_LANE0_1_STATUS, 100,
  442. DP_LINK_STATUS_SIZE, link_status);
  443. if (!ret) {
  444. DRM_ERROR("displayport link status failed\n");
  445. return false;
  446. }
  447. DRM_DEBUG_KMS("link status %02x %02x %02x %02x %02x %02x\n",
  448. link_status[0], link_status[1], link_status[2],
  449. link_status[3], link_status[4], link_status[5]);
  450. return true;
  451. }
  452. bool radeon_dp_needs_link_train(struct radeon_connector *radeon_connector)
  453. {
  454. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  455. u8 link_status[DP_LINK_STATUS_SIZE];
  456. if (!atom_dp_get_link_status(radeon_connector, link_status))
  457. return false;
  458. if (dp_channel_eq_ok(link_status, dig_connector->dp_lane_count))
  459. return false;
  460. return true;
  461. }
  462. static void dp_set_power(struct radeon_connector *radeon_connector, u8 power_state)
  463. {
  464. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  465. if (dig_connector->dpcd[0] >= 0x11) {
  466. radeon_dp_aux_native_write(radeon_connector, DP_SET_POWER, 1,
  467. &power_state);
  468. }
  469. }
  470. static void dp_set_downspread(struct radeon_connector *radeon_connector, u8 downspread)
  471. {
  472. radeon_dp_aux_native_write(radeon_connector, DP_DOWNSPREAD_CTRL, 1,
  473. &downspread);
  474. }
  475. static void dp_set_link_bw_lanes(struct radeon_connector *radeon_connector,
  476. u8 link_configuration[DP_LINK_CONFIGURATION_SIZE])
  477. {
  478. radeon_dp_aux_native_write(radeon_connector, DP_LINK_BW_SET, 2,
  479. link_configuration);
  480. }
  481. static void dp_update_dpvs_emph(struct radeon_connector *radeon_connector,
  482. struct drm_encoder *encoder,
  483. u8 train_set[4])
  484. {
  485. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  486. int i;
  487. for (i = 0; i < dig_connector->dp_lane_count; i++)
  488. atombios_dig_transmitter_setup(encoder,
  489. ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
  490. i, train_set[i]);
  491. radeon_dp_aux_native_write(radeon_connector, DP_TRAINING_LANE0_SET,
  492. dig_connector->dp_lane_count, train_set);
  493. }
  494. static void dp_set_training(struct radeon_connector *radeon_connector,
  495. u8 training)
  496. {
  497. radeon_dp_aux_native_write(radeon_connector, DP_TRAINING_PATTERN_SET,
  498. 1, &training);
  499. }
  500. void dp_link_train(struct drm_encoder *encoder,
  501. struct drm_connector *connector)
  502. {
  503. struct drm_device *dev = encoder->dev;
  504. struct radeon_device *rdev = dev->dev_private;
  505. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  506. struct radeon_encoder_atom_dig *dig;
  507. struct radeon_connector *radeon_connector;
  508. struct radeon_connector_atom_dig *dig_connector;
  509. int enc_id = 0;
  510. bool clock_recovery, channel_eq;
  511. u8 link_status[DP_LINK_STATUS_SIZE];
  512. u8 link_configuration[DP_LINK_CONFIGURATION_SIZE];
  513. u8 tries, voltage;
  514. u8 train_set[4];
  515. int i;
  516. if ((connector->connector_type != DRM_MODE_CONNECTOR_DisplayPort) &&
  517. (connector->connector_type != DRM_MODE_CONNECTOR_eDP))
  518. return;
  519. if (!radeon_encoder->enc_priv)
  520. return;
  521. dig = radeon_encoder->enc_priv;
  522. radeon_connector = to_radeon_connector(connector);
  523. if (!radeon_connector->con_priv)
  524. return;
  525. dig_connector = radeon_connector->con_priv;
  526. if (dig->dig_encoder)
  527. enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
  528. else
  529. enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
  530. if (dig->linkb)
  531. enc_id |= ATOM_DP_CONFIG_LINK_B;
  532. else
  533. enc_id |= ATOM_DP_CONFIG_LINK_A;
  534. memset(link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  535. if (dig_connector->dp_clock == 270000)
  536. link_configuration[0] = DP_LINK_BW_2_7;
  537. else
  538. link_configuration[0] = DP_LINK_BW_1_62;
  539. link_configuration[1] = dig_connector->dp_lane_count;
  540. if (dig_connector->dpcd[0] >= 0x11)
  541. link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  542. /* power up the sink */
  543. dp_set_power(radeon_connector, DP_SET_POWER_D0);
  544. /* disable the training pattern on the sink */
  545. dp_set_training(radeon_connector, DP_TRAINING_PATTERN_DISABLE);
  546. /* set link bw and lanes on the sink */
  547. dp_set_link_bw_lanes(radeon_connector, link_configuration);
  548. /* disable downspread on the sink */
  549. dp_set_downspread(radeon_connector, 0);
  550. if (ASIC_IS_DCE4(rdev)) {
  551. /* start training on the source */
  552. atombios_dig_encoder_setup(encoder, ATOM_ENCODER_CMD_DP_LINK_TRAINING_START);
  553. /* set training pattern 1 on the source */
  554. atombios_dig_encoder_setup(encoder, ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1);
  555. } else {
  556. /* start training on the source */
  557. radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_START,
  558. dig_connector->dp_clock, enc_id, 0);
  559. /* set training pattern 1 on the source */
  560. radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
  561. dig_connector->dp_clock, enc_id, 0);
  562. }
  563. /* set initial vs/emph */
  564. memset(train_set, 0, 4);
  565. udelay(400);
  566. /* set training pattern 1 on the sink */
  567. dp_set_training(radeon_connector, DP_TRAINING_PATTERN_1);
  568. dp_update_dpvs_emph(radeon_connector, encoder, train_set);
  569. /* clock recovery loop */
  570. clock_recovery = false;
  571. tries = 0;
  572. voltage = 0xff;
  573. for (;;) {
  574. udelay(100);
  575. if (!atom_dp_get_link_status(radeon_connector, link_status))
  576. break;
  577. if (dp_clock_recovery_ok(link_status, dig_connector->dp_lane_count)) {
  578. clock_recovery = true;
  579. break;
  580. }
  581. for (i = 0; i < dig_connector->dp_lane_count; i++) {
  582. if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  583. break;
  584. }
  585. if (i == dig_connector->dp_lane_count) {
  586. DRM_ERROR("clock recovery reached max voltage\n");
  587. break;
  588. }
  589. if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  590. ++tries;
  591. if (tries == 5) {
  592. DRM_ERROR("clock recovery tried 5 times\n");
  593. break;
  594. }
  595. } else
  596. tries = 0;
  597. voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  598. /* Compute new train_set as requested by sink */
  599. dp_get_adjust_train(link_status, dig_connector->dp_lane_count, train_set);
  600. dp_update_dpvs_emph(radeon_connector, encoder, train_set);
  601. }
  602. if (!clock_recovery)
  603. DRM_ERROR("clock recovery failed\n");
  604. else
  605. DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
  606. train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  607. (train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
  608. DP_TRAIN_PRE_EMPHASIS_SHIFT);
  609. /* set training pattern 2 on the sink */
  610. dp_set_training(radeon_connector, DP_TRAINING_PATTERN_2);
  611. /* set training pattern 2 on the source */
  612. if (ASIC_IS_DCE4(rdev))
  613. atombios_dig_encoder_setup(encoder, ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2);
  614. else
  615. radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
  616. dig_connector->dp_clock, enc_id, 1);
  617. /* channel equalization loop */
  618. tries = 0;
  619. channel_eq = false;
  620. for (;;) {
  621. udelay(400);
  622. if (!atom_dp_get_link_status(radeon_connector, link_status))
  623. break;
  624. if (dp_channel_eq_ok(link_status, dig_connector->dp_lane_count)) {
  625. channel_eq = true;
  626. break;
  627. }
  628. /* Try 5 times */
  629. if (tries > 5) {
  630. DRM_ERROR("channel eq failed: 5 tries\n");
  631. break;
  632. }
  633. /* Compute new train_set as requested by sink */
  634. dp_get_adjust_train(link_status, dig_connector->dp_lane_count, train_set);
  635. dp_update_dpvs_emph(radeon_connector, encoder, train_set);
  636. tries++;
  637. }
  638. if (!channel_eq)
  639. DRM_ERROR("channel eq failed\n");
  640. else
  641. DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
  642. train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  643. (train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
  644. >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
  645. /* disable the training pattern on the sink */
  646. dp_set_training(radeon_connector, DP_TRAINING_PATTERN_DISABLE);
  647. /* disable the training pattern on the source */
  648. if (ASIC_IS_DCE4(rdev))
  649. atombios_dig_encoder_setup(encoder, ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE);
  650. else
  651. radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
  652. dig_connector->dp_clock, enc_id, 0);
  653. }
  654. int radeon_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  655. uint8_t write_byte, uint8_t *read_byte)
  656. {
  657. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  658. struct radeon_i2c_chan *auxch = (struct radeon_i2c_chan *)adapter;
  659. int ret = 0;
  660. uint16_t address = algo_data->address;
  661. uint8_t msg[5];
  662. uint8_t reply[2];
  663. int msg_len, dp_msg_len;
  664. int reply_bytes;
  665. /* Set up the command byte */
  666. if (mode & MODE_I2C_READ)
  667. msg[2] = AUX_I2C_READ << 4;
  668. else
  669. msg[2] = AUX_I2C_WRITE << 4;
  670. if (!(mode & MODE_I2C_STOP))
  671. msg[2] |= AUX_I2C_MOT << 4;
  672. msg[0] = address;
  673. msg[1] = address >> 8;
  674. reply_bytes = 1;
  675. msg_len = 4;
  676. dp_msg_len = 3;
  677. switch (mode) {
  678. case MODE_I2C_WRITE:
  679. msg[4] = write_byte;
  680. msg_len++;
  681. dp_msg_len += 2;
  682. break;
  683. case MODE_I2C_READ:
  684. dp_msg_len += 1;
  685. break;
  686. default:
  687. break;
  688. }
  689. msg[3] = (dp_msg_len) << 4;
  690. ret = radeon_process_aux_ch(auxch, msg, msg_len, reply, reply_bytes, 0);
  691. if (ret) {
  692. if (read_byte)
  693. *read_byte = reply[0];
  694. return reply_bytes;
  695. }
  696. return -EREMOTEIO;
  697. }