atombios_dp.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. * Jerome Glisse
  26. */
  27. #include "drmP.h"
  28. #include "radeon_drm.h"
  29. #include "radeon.h"
  30. #include "atom.h"
  31. #include "atom-bits.h"
  32. #include "drm_dp_helper.h"
  33. /* move these to drm_dp_helper.c/h */
  34. #define DP_LINK_CONFIGURATION_SIZE 9
  35. #define DP_LINK_STATUS_SIZE 6
  36. #define DP_DPCD_SIZE 8
  37. static char *voltage_names[] = {
  38. "0.4V", "0.6V", "0.8V", "1.2V"
  39. };
  40. static char *pre_emph_names[] = {
  41. "0dB", "3.5dB", "6dB", "9.5dB"
  42. };
  43. /***** radeon AUX functions *****/
  44. union aux_channel_transaction {
  45. PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION v1;
  46. PROCESS_AUX_CHANNEL_TRANSACTION_PARAMETERS_V2 v2;
  47. };
  48. static int radeon_process_aux_ch(struct radeon_i2c_chan *chan,
  49. u8 *send, int send_bytes,
  50. u8 *recv, int recv_size,
  51. u8 delay, u8 *ack)
  52. {
  53. struct drm_device *dev = chan->dev;
  54. struct radeon_device *rdev = dev->dev_private;
  55. union aux_channel_transaction args;
  56. int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
  57. unsigned char *base;
  58. int recv_bytes;
  59. memset(&args, 0, sizeof(args));
  60. base = (unsigned char *)(rdev->mode_info.atom_context->scratch + 1);
  61. memcpy(base, send, send_bytes);
  62. args.v1.lpAuxRequest = 0 + 4;
  63. args.v1.lpDataOut = 16 + 4;
  64. args.v1.ucDataOutLen = 0;
  65. args.v1.ucChannelID = chan->rec.i2c_id;
  66. args.v1.ucDelay = delay / 10;
  67. if (ASIC_IS_DCE4(rdev))
  68. args.v2.ucHPD_ID = chan->rec.hpd;
  69. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  70. *ack = args.v1.ucReplyStatus;
  71. /* timeout */
  72. if (args.v1.ucReplyStatus == 1) {
  73. DRM_DEBUG_KMS("dp_aux_ch timeout\n");
  74. return -ETIMEDOUT;
  75. }
  76. /* flags not zero */
  77. if (args.v1.ucReplyStatus == 2) {
  78. DRM_DEBUG_KMS("dp_aux_ch flags not zero\n");
  79. return -EBUSY;
  80. }
  81. /* error */
  82. if (args.v1.ucReplyStatus == 3) {
  83. DRM_DEBUG_KMS("dp_aux_ch error\n");
  84. return -EIO;
  85. }
  86. recv_bytes = args.v1.ucDataOutLen;
  87. if (recv_bytes > recv_size)
  88. recv_bytes = recv_size;
  89. if (recv && recv_size)
  90. memcpy(recv, base + 16, recv_bytes);
  91. return recv_bytes;
  92. }
  93. static int radeon_dp_aux_native_write(struct radeon_connector *radeon_connector,
  94. u16 address, u8 *send, u8 send_bytes, u8 delay)
  95. {
  96. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  97. int ret;
  98. u8 msg[20];
  99. int msg_bytes = send_bytes + 4;
  100. u8 ack;
  101. unsigned retry;
  102. if (send_bytes > 16)
  103. return -1;
  104. msg[0] = address;
  105. msg[1] = address >> 8;
  106. msg[2] = AUX_NATIVE_WRITE << 4;
  107. msg[3] = (msg_bytes << 4) | (send_bytes - 1);
  108. memcpy(&msg[4], send, send_bytes);
  109. for (retry = 0; retry < 4; retry++) {
  110. ret = radeon_process_aux_ch(dig_connector->dp_i2c_bus,
  111. msg, msg_bytes, NULL, 0, delay, &ack);
  112. if (ret == -EBUSY)
  113. continue;
  114. else if (ret < 0)
  115. return ret;
  116. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
  117. return send_bytes;
  118. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  119. udelay(400);
  120. else
  121. return -EIO;
  122. }
  123. return -EIO;
  124. }
  125. static int radeon_dp_aux_native_read(struct radeon_connector *radeon_connector,
  126. u16 address, u8 *recv, int recv_bytes, u8 delay)
  127. {
  128. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  129. u8 msg[4];
  130. int msg_bytes = 4;
  131. u8 ack;
  132. int ret;
  133. unsigned retry;
  134. msg[0] = address;
  135. msg[1] = address >> 8;
  136. msg[2] = AUX_NATIVE_READ << 4;
  137. msg[3] = (msg_bytes << 4) | (recv_bytes - 1);
  138. for (retry = 0; retry < 4; retry++) {
  139. ret = radeon_process_aux_ch(dig_connector->dp_i2c_bus,
  140. msg, msg_bytes, recv, recv_bytes, delay, &ack);
  141. if (ret == -EBUSY)
  142. continue;
  143. else if (ret < 0)
  144. return ret;
  145. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
  146. return ret;
  147. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  148. udelay(400);
  149. else if (ret == 0)
  150. return -EPROTO;
  151. else
  152. return -EIO;
  153. }
  154. return -EIO;
  155. }
  156. static void radeon_write_dpcd_reg(struct radeon_connector *radeon_connector,
  157. u16 reg, u8 val)
  158. {
  159. radeon_dp_aux_native_write(radeon_connector, reg, &val, 1, 0);
  160. }
  161. static u8 radeon_read_dpcd_reg(struct radeon_connector *radeon_connector,
  162. u16 reg)
  163. {
  164. u8 val = 0;
  165. radeon_dp_aux_native_read(radeon_connector, reg, &val, 1, 0);
  166. return val;
  167. }
  168. int radeon_dp_i2c_aux_ch(struct i2c_adapter *adapter, int mode,
  169. u8 write_byte, u8 *read_byte)
  170. {
  171. struct i2c_algo_dp_aux_data *algo_data = adapter->algo_data;
  172. struct radeon_i2c_chan *auxch = (struct radeon_i2c_chan *)adapter;
  173. u16 address = algo_data->address;
  174. u8 msg[5];
  175. u8 reply[2];
  176. unsigned retry;
  177. int msg_bytes;
  178. int reply_bytes = 1;
  179. int ret;
  180. u8 ack;
  181. /* Set up the command byte */
  182. if (mode & MODE_I2C_READ)
  183. msg[2] = AUX_I2C_READ << 4;
  184. else
  185. msg[2] = AUX_I2C_WRITE << 4;
  186. if (!(mode & MODE_I2C_STOP))
  187. msg[2] |= AUX_I2C_MOT << 4;
  188. msg[0] = address;
  189. msg[1] = address >> 8;
  190. switch (mode) {
  191. case MODE_I2C_WRITE:
  192. msg_bytes = 5;
  193. msg[3] = msg_bytes << 4;
  194. msg[4] = write_byte;
  195. break;
  196. case MODE_I2C_READ:
  197. msg_bytes = 4;
  198. msg[3] = msg_bytes << 4;
  199. break;
  200. default:
  201. msg_bytes = 4;
  202. msg[3] = 3 << 4;
  203. break;
  204. }
  205. for (retry = 0; retry < 4; retry++) {
  206. ret = radeon_process_aux_ch(auxch,
  207. msg, msg_bytes, reply, reply_bytes, 0, &ack);
  208. if (ret == -EBUSY)
  209. continue;
  210. else if (ret < 0) {
  211. DRM_DEBUG_KMS("aux_ch failed %d\n", ret);
  212. return ret;
  213. }
  214. switch (ack & AUX_NATIVE_REPLY_MASK) {
  215. case AUX_NATIVE_REPLY_ACK:
  216. /* I2C-over-AUX Reply field is only valid
  217. * when paired with AUX ACK.
  218. */
  219. break;
  220. case AUX_NATIVE_REPLY_NACK:
  221. DRM_DEBUG_KMS("aux_ch native nack\n");
  222. return -EREMOTEIO;
  223. case AUX_NATIVE_REPLY_DEFER:
  224. DRM_DEBUG_KMS("aux_ch native defer\n");
  225. udelay(400);
  226. continue;
  227. default:
  228. DRM_ERROR("aux_ch invalid native reply 0x%02x\n", ack);
  229. return -EREMOTEIO;
  230. }
  231. switch (ack & AUX_I2C_REPLY_MASK) {
  232. case AUX_I2C_REPLY_ACK:
  233. if (mode == MODE_I2C_READ)
  234. *read_byte = reply[0];
  235. return ret;
  236. case AUX_I2C_REPLY_NACK:
  237. DRM_DEBUG_KMS("aux_i2c nack\n");
  238. return -EREMOTEIO;
  239. case AUX_I2C_REPLY_DEFER:
  240. DRM_DEBUG_KMS("aux_i2c defer\n");
  241. udelay(400);
  242. break;
  243. default:
  244. DRM_ERROR("aux_i2c invalid reply 0x%02x\n", ack);
  245. return -EREMOTEIO;
  246. }
  247. }
  248. DRM_DEBUG_KMS("aux i2c too many retries, giving up\n");
  249. return -EREMOTEIO;
  250. }
  251. /***** general DP utility functions *****/
  252. static u8 dp_link_status(u8 link_status[DP_LINK_STATUS_SIZE], int r)
  253. {
  254. return link_status[r - DP_LANE0_1_STATUS];
  255. }
  256. static u8 dp_get_lane_status(u8 link_status[DP_LINK_STATUS_SIZE],
  257. int lane)
  258. {
  259. int i = DP_LANE0_1_STATUS + (lane >> 1);
  260. int s = (lane & 1) * 4;
  261. u8 l = dp_link_status(link_status, i);
  262. return (l >> s) & 0xf;
  263. }
  264. static bool dp_clock_recovery_ok(u8 link_status[DP_LINK_STATUS_SIZE],
  265. int lane_count)
  266. {
  267. int lane;
  268. u8 lane_status;
  269. for (lane = 0; lane < lane_count; lane++) {
  270. lane_status = dp_get_lane_status(link_status, lane);
  271. if ((lane_status & DP_LANE_CR_DONE) == 0)
  272. return false;
  273. }
  274. return true;
  275. }
  276. static bool dp_channel_eq_ok(u8 link_status[DP_LINK_STATUS_SIZE],
  277. int lane_count)
  278. {
  279. u8 lane_align;
  280. u8 lane_status;
  281. int lane;
  282. lane_align = dp_link_status(link_status,
  283. DP_LANE_ALIGN_STATUS_UPDATED);
  284. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  285. return false;
  286. for (lane = 0; lane < lane_count; lane++) {
  287. lane_status = dp_get_lane_status(link_status, lane);
  288. if ((lane_status & DP_CHANNEL_EQ_BITS) != DP_CHANNEL_EQ_BITS)
  289. return false;
  290. }
  291. return true;
  292. }
  293. static u8 dp_get_adjust_request_voltage(u8 link_status[DP_LINK_STATUS_SIZE],
  294. int lane)
  295. {
  296. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  297. int s = ((lane & 1) ?
  298. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  299. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  300. u8 l = dp_link_status(link_status, i);
  301. return ((l >> s) & 0x3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  302. }
  303. static u8 dp_get_adjust_request_pre_emphasis(u8 link_status[DP_LINK_STATUS_SIZE],
  304. int lane)
  305. {
  306. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  307. int s = ((lane & 1) ?
  308. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  309. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  310. u8 l = dp_link_status(link_status, i);
  311. return ((l >> s) & 0x3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  312. }
  313. #define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_1200
  314. #define DP_PRE_EMPHASIS_MAX DP_TRAIN_PRE_EMPHASIS_9_5
  315. static void dp_get_adjust_train(u8 link_status[DP_LINK_STATUS_SIZE],
  316. int lane_count,
  317. u8 train_set[4])
  318. {
  319. u8 v = 0;
  320. u8 p = 0;
  321. int lane;
  322. for (lane = 0; lane < lane_count; lane++) {
  323. u8 this_v = dp_get_adjust_request_voltage(link_status, lane);
  324. u8 this_p = dp_get_adjust_request_pre_emphasis(link_status, lane);
  325. DRM_DEBUG_KMS("requested signal parameters: lane %d voltage %s pre_emph %s\n",
  326. lane,
  327. voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
  328. pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
  329. if (this_v > v)
  330. v = this_v;
  331. if (this_p > p)
  332. p = this_p;
  333. }
  334. if (v >= DP_VOLTAGE_MAX)
  335. v |= DP_TRAIN_MAX_SWING_REACHED;
  336. if (p >= DP_PRE_EMPHASIS_MAX)
  337. p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  338. DRM_DEBUG_KMS("using signal parameters: voltage %s pre_emph %s\n",
  339. voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
  340. pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
  341. for (lane = 0; lane < 4; lane++)
  342. train_set[lane] = v | p;
  343. }
  344. /* convert bits per color to bits per pixel */
  345. /* get bpc from the EDID */
  346. static int convert_bpc_to_bpp(int bpc)
  347. {
  348. if (bpc == 0)
  349. return 24;
  350. else
  351. return bpc * 3;
  352. }
  353. /* get the max pix clock supported by the link rate and lane num */
  354. static int dp_get_max_dp_pix_clock(int link_rate,
  355. int lane_num,
  356. int bpp)
  357. {
  358. return (link_rate * lane_num * 8) / bpp;
  359. }
  360. static int dp_get_max_link_rate(u8 dpcd[DP_DPCD_SIZE])
  361. {
  362. switch (dpcd[DP_MAX_LINK_RATE]) {
  363. case DP_LINK_BW_1_62:
  364. default:
  365. return 162000;
  366. case DP_LINK_BW_2_7:
  367. return 270000;
  368. case DP_LINK_BW_5_4:
  369. return 540000;
  370. }
  371. }
  372. static u8 dp_get_max_lane_number(u8 dpcd[DP_DPCD_SIZE])
  373. {
  374. return dpcd[DP_MAX_LANE_COUNT] & DP_MAX_LANE_COUNT_MASK;
  375. }
  376. static u8 dp_get_dp_link_rate_coded(int link_rate)
  377. {
  378. switch (link_rate) {
  379. case 162000:
  380. default:
  381. return DP_LINK_BW_1_62;
  382. case 270000:
  383. return DP_LINK_BW_2_7;
  384. case 540000:
  385. return DP_LINK_BW_5_4;
  386. }
  387. }
  388. /***** radeon specific DP functions *****/
  389. /* First get the min lane# when low rate is used according to pixel clock
  390. * (prefer low rate), second check max lane# supported by DP panel,
  391. * if the max lane# < low rate lane# then use max lane# instead.
  392. */
  393. static int radeon_dp_get_dp_lane_number(struct drm_connector *connector,
  394. u8 dpcd[DP_DPCD_SIZE],
  395. int pix_clock)
  396. {
  397. int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
  398. int max_link_rate = dp_get_max_link_rate(dpcd);
  399. int max_lane_num = dp_get_max_lane_number(dpcd);
  400. int lane_num;
  401. int max_dp_pix_clock;
  402. for (lane_num = 1; lane_num < max_lane_num; lane_num <<= 1) {
  403. max_dp_pix_clock = dp_get_max_dp_pix_clock(max_link_rate, lane_num, bpp);
  404. if (pix_clock <= max_dp_pix_clock)
  405. break;
  406. }
  407. return lane_num;
  408. }
  409. static int radeon_dp_get_dp_link_clock(struct drm_connector *connector,
  410. u8 dpcd[DP_DPCD_SIZE],
  411. int pix_clock)
  412. {
  413. int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
  414. int lane_num, max_pix_clock;
  415. if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
  416. ENCODER_OBJECT_ID_NUTMEG)
  417. return 270000;
  418. lane_num = radeon_dp_get_dp_lane_number(connector, dpcd, pix_clock);
  419. max_pix_clock = dp_get_max_dp_pix_clock(162000, lane_num, bpp);
  420. if (pix_clock <= max_pix_clock)
  421. return 162000;
  422. max_pix_clock = dp_get_max_dp_pix_clock(270000, lane_num, bpp);
  423. if (pix_clock <= max_pix_clock)
  424. return 270000;
  425. if (radeon_connector_is_dp12_capable(connector)) {
  426. max_pix_clock = dp_get_max_dp_pix_clock(540000, lane_num, bpp);
  427. if (pix_clock <= max_pix_clock)
  428. return 540000;
  429. }
  430. return dp_get_max_link_rate(dpcd);
  431. }
  432. static u8 radeon_dp_encoder_service(struct radeon_device *rdev,
  433. int action, int dp_clock,
  434. u8 ucconfig, u8 lane_num)
  435. {
  436. DP_ENCODER_SERVICE_PARAMETERS args;
  437. int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
  438. memset(&args, 0, sizeof(args));
  439. args.ucLinkClock = dp_clock / 10;
  440. args.ucConfig = ucconfig;
  441. args.ucAction = action;
  442. args.ucLaneNum = lane_num;
  443. args.ucStatus = 0;
  444. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  445. return args.ucStatus;
  446. }
  447. u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
  448. {
  449. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  450. struct drm_device *dev = radeon_connector->base.dev;
  451. struct radeon_device *rdev = dev->dev_private;
  452. return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
  453. dig_connector->dp_i2c_bus->rec.i2c_id, 0);
  454. }
  455. static void radeon_dp_probe_oui(struct radeon_connector *radeon_connector)
  456. {
  457. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  458. u8 buf[3];
  459. if (!(dig_connector->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
  460. return;
  461. if (radeon_dp_aux_native_read(radeon_connector, DP_SINK_OUI, buf, 3, 0))
  462. DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
  463. buf[0], buf[1], buf[2]);
  464. if (radeon_dp_aux_native_read(radeon_connector, DP_BRANCH_OUI, buf, 3, 0))
  465. DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
  466. buf[0], buf[1], buf[2]);
  467. }
  468. bool radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
  469. {
  470. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  471. u8 msg[25];
  472. int ret, i;
  473. ret = radeon_dp_aux_native_read(radeon_connector, DP_DPCD_REV, msg, 8, 0);
  474. if (ret > 0) {
  475. memcpy(dig_connector->dpcd, msg, 8);
  476. DRM_DEBUG_KMS("DPCD: ");
  477. for (i = 0; i < 8; i++)
  478. DRM_DEBUG_KMS("%02x ", msg[i]);
  479. DRM_DEBUG_KMS("\n");
  480. radeon_dp_probe_oui(radeon_connector);
  481. return true;
  482. }
  483. dig_connector->dpcd[0] = 0;
  484. return false;
  485. }
  486. int radeon_dp_get_panel_mode(struct drm_encoder *encoder,
  487. struct drm_connector *connector)
  488. {
  489. struct drm_device *dev = encoder->dev;
  490. struct radeon_device *rdev = dev->dev_private;
  491. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  492. int panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
  493. u16 dp_bridge = radeon_connector_encoder_get_dp_bridge_encoder_id(connector);
  494. u8 tmp;
  495. if (!ASIC_IS_DCE4(rdev))
  496. return panel_mode;
  497. if (dp_bridge != ENCODER_OBJECT_ID_NONE) {
  498. /* DP bridge chips */
  499. tmp = radeon_read_dpcd_reg(radeon_connector, DP_EDP_CONFIGURATION_CAP);
  500. if (tmp & 1)
  501. panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
  502. else if ((dp_bridge == ENCODER_OBJECT_ID_NUTMEG) ||
  503. (dp_bridge == ENCODER_OBJECT_ID_TRAVIS))
  504. panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
  505. else
  506. panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
  507. } else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
  508. /* eDP */
  509. tmp = radeon_read_dpcd_reg(radeon_connector, DP_EDP_CONFIGURATION_CAP);
  510. if (tmp & 1)
  511. panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
  512. }
  513. return panel_mode;
  514. }
  515. void radeon_dp_set_link_config(struct drm_connector *connector,
  516. const struct drm_display_mode *mode)
  517. {
  518. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  519. struct radeon_connector_atom_dig *dig_connector;
  520. if (!radeon_connector->con_priv)
  521. return;
  522. dig_connector = radeon_connector->con_priv;
  523. if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
  524. (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
  525. dig_connector->dp_clock =
  526. radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
  527. dig_connector->dp_lane_count =
  528. radeon_dp_get_dp_lane_number(connector, dig_connector->dpcd, mode->clock);
  529. }
  530. }
  531. int radeon_dp_mode_valid_helper(struct drm_connector *connector,
  532. struct drm_display_mode *mode)
  533. {
  534. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  535. struct radeon_connector_atom_dig *dig_connector;
  536. int dp_clock;
  537. if (!radeon_connector->con_priv)
  538. return MODE_CLOCK_HIGH;
  539. dig_connector = radeon_connector->con_priv;
  540. dp_clock =
  541. radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
  542. if ((dp_clock == 540000) &&
  543. (!radeon_connector_is_dp12_capable(connector)))
  544. return MODE_CLOCK_HIGH;
  545. return MODE_OK;
  546. }
  547. static bool radeon_dp_get_link_status(struct radeon_connector *radeon_connector,
  548. u8 link_status[DP_LINK_STATUS_SIZE])
  549. {
  550. int ret;
  551. ret = radeon_dp_aux_native_read(radeon_connector, DP_LANE0_1_STATUS,
  552. link_status, DP_LINK_STATUS_SIZE, 100);
  553. if (ret <= 0) {
  554. return false;
  555. }
  556. DRM_DEBUG_KMS("link status %02x %02x %02x %02x %02x %02x\n",
  557. link_status[0], link_status[1], link_status[2],
  558. link_status[3], link_status[4], link_status[5]);
  559. return true;
  560. }
  561. bool radeon_dp_needs_link_train(struct radeon_connector *radeon_connector)
  562. {
  563. u8 link_status[DP_LINK_STATUS_SIZE];
  564. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  565. if (!radeon_dp_get_link_status(radeon_connector, link_status))
  566. return false;
  567. if (dp_channel_eq_ok(link_status, dig->dp_lane_count))
  568. return false;
  569. return true;
  570. }
  571. struct radeon_dp_link_train_info {
  572. struct radeon_device *rdev;
  573. struct drm_encoder *encoder;
  574. struct drm_connector *connector;
  575. struct radeon_connector *radeon_connector;
  576. int enc_id;
  577. int dp_clock;
  578. int dp_lane_count;
  579. int rd_interval;
  580. bool tp3_supported;
  581. u8 dpcd[8];
  582. u8 train_set[4];
  583. u8 link_status[DP_LINK_STATUS_SIZE];
  584. u8 tries;
  585. bool use_dpencoder;
  586. };
  587. static void radeon_dp_update_vs_emph(struct radeon_dp_link_train_info *dp_info)
  588. {
  589. /* set the initial vs/emph on the source */
  590. atombios_dig_transmitter_setup(dp_info->encoder,
  591. ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
  592. 0, dp_info->train_set[0]); /* sets all lanes at once */
  593. /* set the vs/emph on the sink */
  594. radeon_dp_aux_native_write(dp_info->radeon_connector, DP_TRAINING_LANE0_SET,
  595. dp_info->train_set, dp_info->dp_lane_count, 0);
  596. }
  597. static void radeon_dp_set_tp(struct radeon_dp_link_train_info *dp_info, int tp)
  598. {
  599. int rtp = 0;
  600. /* set training pattern on the source */
  601. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder) {
  602. switch (tp) {
  603. case DP_TRAINING_PATTERN_1:
  604. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
  605. break;
  606. case DP_TRAINING_PATTERN_2:
  607. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
  608. break;
  609. case DP_TRAINING_PATTERN_3:
  610. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
  611. break;
  612. }
  613. atombios_dig_encoder_setup(dp_info->encoder, rtp, 0);
  614. } else {
  615. switch (tp) {
  616. case DP_TRAINING_PATTERN_1:
  617. rtp = 0;
  618. break;
  619. case DP_TRAINING_PATTERN_2:
  620. rtp = 1;
  621. break;
  622. }
  623. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
  624. dp_info->dp_clock, dp_info->enc_id, rtp);
  625. }
  626. /* enable training pattern on the sink */
  627. radeon_write_dpcd_reg(dp_info->radeon_connector, DP_TRAINING_PATTERN_SET, tp);
  628. }
  629. static int radeon_dp_link_train_init(struct radeon_dp_link_train_info *dp_info)
  630. {
  631. struct radeon_encoder *radeon_encoder = to_radeon_encoder(dp_info->encoder);
  632. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  633. u8 tmp;
  634. /* power up the sink */
  635. if (dp_info->dpcd[0] >= 0x11)
  636. radeon_write_dpcd_reg(dp_info->radeon_connector,
  637. DP_SET_POWER, DP_SET_POWER_D0);
  638. /* possibly enable downspread on the sink */
  639. if (dp_info->dpcd[3] & 0x1)
  640. radeon_write_dpcd_reg(dp_info->radeon_connector,
  641. DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
  642. else
  643. radeon_write_dpcd_reg(dp_info->radeon_connector,
  644. DP_DOWNSPREAD_CTRL, 0);
  645. if ((dp_info->connector->connector_type == DRM_MODE_CONNECTOR_eDP) &&
  646. (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)) {
  647. radeon_write_dpcd_reg(dp_info->radeon_connector, DP_EDP_CONFIGURATION_SET, 1);
  648. }
  649. /* set the lane count on the sink */
  650. tmp = dp_info->dp_lane_count;
  651. if (dp_info->dpcd[DP_DPCD_REV] >= 0x11 &&
  652. dp_info->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)
  653. tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  654. radeon_write_dpcd_reg(dp_info->radeon_connector, DP_LANE_COUNT_SET, tmp);
  655. /* set the link rate on the sink */
  656. tmp = dp_get_dp_link_rate_coded(dp_info->dp_clock);
  657. radeon_write_dpcd_reg(dp_info->radeon_connector, DP_LINK_BW_SET, tmp);
  658. /* start training on the source */
  659. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
  660. atombios_dig_encoder_setup(dp_info->encoder,
  661. ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
  662. else
  663. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_START,
  664. dp_info->dp_clock, dp_info->enc_id, 0);
  665. /* disable the training pattern on the sink */
  666. radeon_write_dpcd_reg(dp_info->radeon_connector,
  667. DP_TRAINING_PATTERN_SET,
  668. DP_TRAINING_PATTERN_DISABLE);
  669. return 0;
  670. }
  671. static int radeon_dp_link_train_finish(struct radeon_dp_link_train_info *dp_info)
  672. {
  673. udelay(400);
  674. /* disable the training pattern on the sink */
  675. radeon_write_dpcd_reg(dp_info->radeon_connector,
  676. DP_TRAINING_PATTERN_SET,
  677. DP_TRAINING_PATTERN_DISABLE);
  678. /* disable the training pattern on the source */
  679. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
  680. atombios_dig_encoder_setup(dp_info->encoder,
  681. ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
  682. else
  683. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
  684. dp_info->dp_clock, dp_info->enc_id, 0);
  685. return 0;
  686. }
  687. static int radeon_dp_link_train_cr(struct radeon_dp_link_train_info *dp_info)
  688. {
  689. bool clock_recovery;
  690. u8 voltage;
  691. int i;
  692. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
  693. memset(dp_info->train_set, 0, 4);
  694. radeon_dp_update_vs_emph(dp_info);
  695. udelay(400);
  696. /* clock recovery loop */
  697. clock_recovery = false;
  698. dp_info->tries = 0;
  699. voltage = 0xff;
  700. while (1) {
  701. if (dp_info->rd_interval == 0)
  702. udelay(100);
  703. else
  704. mdelay(dp_info->rd_interval * 4);
  705. if (!radeon_dp_get_link_status(dp_info->radeon_connector, dp_info->link_status)) {
  706. DRM_ERROR("displayport link status failed\n");
  707. break;
  708. }
  709. if (dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
  710. clock_recovery = true;
  711. break;
  712. }
  713. for (i = 0; i < dp_info->dp_lane_count; i++) {
  714. if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  715. break;
  716. }
  717. if (i == dp_info->dp_lane_count) {
  718. DRM_ERROR("clock recovery reached max voltage\n");
  719. break;
  720. }
  721. if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  722. ++dp_info->tries;
  723. if (dp_info->tries == 5) {
  724. DRM_ERROR("clock recovery tried 5 times\n");
  725. break;
  726. }
  727. } else
  728. dp_info->tries = 0;
  729. voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  730. /* Compute new train_set as requested by sink */
  731. dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
  732. radeon_dp_update_vs_emph(dp_info);
  733. }
  734. if (!clock_recovery) {
  735. DRM_ERROR("clock recovery failed\n");
  736. return -1;
  737. } else {
  738. DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
  739. dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  740. (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
  741. DP_TRAIN_PRE_EMPHASIS_SHIFT);
  742. return 0;
  743. }
  744. }
  745. static int radeon_dp_link_train_ce(struct radeon_dp_link_train_info *dp_info)
  746. {
  747. bool channel_eq;
  748. if (dp_info->tp3_supported)
  749. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
  750. else
  751. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
  752. /* channel equalization loop */
  753. dp_info->tries = 0;
  754. channel_eq = false;
  755. while (1) {
  756. if (dp_info->rd_interval == 0)
  757. udelay(400);
  758. else
  759. mdelay(dp_info->rd_interval * 4);
  760. if (!radeon_dp_get_link_status(dp_info->radeon_connector, dp_info->link_status)) {
  761. DRM_ERROR("displayport link status failed\n");
  762. break;
  763. }
  764. if (dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
  765. channel_eq = true;
  766. break;
  767. }
  768. /* Try 5 times */
  769. if (dp_info->tries > 5) {
  770. DRM_ERROR("channel eq failed: 5 tries\n");
  771. break;
  772. }
  773. /* Compute new train_set as requested by sink */
  774. dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
  775. radeon_dp_update_vs_emph(dp_info);
  776. dp_info->tries++;
  777. }
  778. if (!channel_eq) {
  779. DRM_ERROR("channel eq failed\n");
  780. return -1;
  781. } else {
  782. DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
  783. dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  784. (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
  785. >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
  786. return 0;
  787. }
  788. }
  789. void radeon_dp_link_train(struct drm_encoder *encoder,
  790. struct drm_connector *connector)
  791. {
  792. struct drm_device *dev = encoder->dev;
  793. struct radeon_device *rdev = dev->dev_private;
  794. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  795. struct radeon_encoder_atom_dig *dig;
  796. struct radeon_connector *radeon_connector;
  797. struct radeon_connector_atom_dig *dig_connector;
  798. struct radeon_dp_link_train_info dp_info;
  799. int index;
  800. u8 tmp, frev, crev;
  801. if (!radeon_encoder->enc_priv)
  802. return;
  803. dig = radeon_encoder->enc_priv;
  804. radeon_connector = to_radeon_connector(connector);
  805. if (!radeon_connector->con_priv)
  806. return;
  807. dig_connector = radeon_connector->con_priv;
  808. if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
  809. (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
  810. return;
  811. /* DPEncoderService newer than 1.1 can't program properly the
  812. * training pattern. When facing such version use the
  813. * DIGXEncoderControl (X== 1 | 2)
  814. */
  815. dp_info.use_dpencoder = true;
  816. index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
  817. if (atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev)) {
  818. if (crev > 1) {
  819. dp_info.use_dpencoder = false;
  820. }
  821. }
  822. dp_info.enc_id = 0;
  823. if (dig->dig_encoder)
  824. dp_info.enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
  825. else
  826. dp_info.enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
  827. if (dig->linkb)
  828. dp_info.enc_id |= ATOM_DP_CONFIG_LINK_B;
  829. else
  830. dp_info.enc_id |= ATOM_DP_CONFIG_LINK_A;
  831. dp_info.rd_interval = radeon_read_dpcd_reg(radeon_connector, DP_TRAINING_AUX_RD_INTERVAL);
  832. tmp = radeon_read_dpcd_reg(radeon_connector, DP_MAX_LANE_COUNT);
  833. if (ASIC_IS_DCE5(rdev) && (tmp & DP_TPS3_SUPPORTED))
  834. dp_info.tp3_supported = true;
  835. else
  836. dp_info.tp3_supported = false;
  837. memcpy(dp_info.dpcd, dig_connector->dpcd, 8);
  838. dp_info.rdev = rdev;
  839. dp_info.encoder = encoder;
  840. dp_info.connector = connector;
  841. dp_info.radeon_connector = radeon_connector;
  842. dp_info.dp_lane_count = dig_connector->dp_lane_count;
  843. dp_info.dp_clock = dig_connector->dp_clock;
  844. if (radeon_dp_link_train_init(&dp_info))
  845. goto done;
  846. if (radeon_dp_link_train_cr(&dp_info))
  847. goto done;
  848. if (radeon_dp_link_train_ce(&dp_info))
  849. goto done;
  850. done:
  851. if (radeon_dp_link_train_finish(&dp_info))
  852. return;
  853. }