atombios_dp.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. if (!ASIC_IS_DCE4(rdev))
  494. return panel_mode;
  495. if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
  496. ENCODER_OBJECT_ID_NUTMEG)
  497. panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
  498. else if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
  499. ENCODER_OBJECT_ID_TRAVIS) {
  500. u8 id[6];
  501. int i;
  502. for (i = 0; i < 6; i++)
  503. id[i] = radeon_read_dpcd_reg(radeon_connector, 0x503 + i);
  504. if (id[0] == 0x73 &&
  505. id[1] == 0x69 &&
  506. id[2] == 0x76 &&
  507. id[3] == 0x61 &&
  508. id[4] == 0x72 &&
  509. id[5] == 0x54)
  510. panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
  511. else
  512. panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
  513. } else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
  514. u8 tmp = radeon_read_dpcd_reg(radeon_connector, DP_EDP_CONFIGURATION_CAP);
  515. if (tmp & 1)
  516. panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
  517. }
  518. return panel_mode;
  519. }
  520. void radeon_dp_set_link_config(struct drm_connector *connector,
  521. const struct drm_display_mode *mode)
  522. {
  523. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  524. struct radeon_connector_atom_dig *dig_connector;
  525. if (!radeon_connector->con_priv)
  526. return;
  527. dig_connector = radeon_connector->con_priv;
  528. if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
  529. (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
  530. dig_connector->dp_clock =
  531. radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
  532. dig_connector->dp_lane_count =
  533. radeon_dp_get_dp_lane_number(connector, dig_connector->dpcd, mode->clock);
  534. }
  535. }
  536. int radeon_dp_mode_valid_helper(struct drm_connector *connector,
  537. struct drm_display_mode *mode)
  538. {
  539. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  540. struct radeon_connector_atom_dig *dig_connector;
  541. int dp_clock;
  542. if (!radeon_connector->con_priv)
  543. return MODE_CLOCK_HIGH;
  544. dig_connector = radeon_connector->con_priv;
  545. dp_clock =
  546. radeon_dp_get_dp_link_clock(connector, dig_connector->dpcd, mode->clock);
  547. if ((dp_clock == 540000) &&
  548. (!radeon_connector_is_dp12_capable(connector)))
  549. return MODE_CLOCK_HIGH;
  550. return MODE_OK;
  551. }
  552. static bool radeon_dp_get_link_status(struct radeon_connector *radeon_connector,
  553. u8 link_status[DP_LINK_STATUS_SIZE])
  554. {
  555. int ret;
  556. ret = radeon_dp_aux_native_read(radeon_connector, DP_LANE0_1_STATUS,
  557. link_status, DP_LINK_STATUS_SIZE, 100);
  558. if (ret <= 0) {
  559. return false;
  560. }
  561. DRM_DEBUG_KMS("link status %*ph\n", 6, link_status);
  562. return true;
  563. }
  564. bool radeon_dp_needs_link_train(struct radeon_connector *radeon_connector)
  565. {
  566. u8 link_status[DP_LINK_STATUS_SIZE];
  567. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  568. if (!radeon_dp_get_link_status(radeon_connector, link_status))
  569. return false;
  570. if (dp_channel_eq_ok(link_status, dig->dp_lane_count))
  571. return false;
  572. return true;
  573. }
  574. struct radeon_dp_link_train_info {
  575. struct radeon_device *rdev;
  576. struct drm_encoder *encoder;
  577. struct drm_connector *connector;
  578. struct radeon_connector *radeon_connector;
  579. int enc_id;
  580. int dp_clock;
  581. int dp_lane_count;
  582. int rd_interval;
  583. bool tp3_supported;
  584. u8 dpcd[8];
  585. u8 train_set[4];
  586. u8 link_status[DP_LINK_STATUS_SIZE];
  587. u8 tries;
  588. bool use_dpencoder;
  589. };
  590. static void radeon_dp_update_vs_emph(struct radeon_dp_link_train_info *dp_info)
  591. {
  592. /* set the initial vs/emph on the source */
  593. atombios_dig_transmitter_setup(dp_info->encoder,
  594. ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
  595. 0, dp_info->train_set[0]); /* sets all lanes at once */
  596. /* set the vs/emph on the sink */
  597. radeon_dp_aux_native_write(dp_info->radeon_connector, DP_TRAINING_LANE0_SET,
  598. dp_info->train_set, dp_info->dp_lane_count, 0);
  599. }
  600. static void radeon_dp_set_tp(struct radeon_dp_link_train_info *dp_info, int tp)
  601. {
  602. int rtp = 0;
  603. /* set training pattern on the source */
  604. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder) {
  605. switch (tp) {
  606. case DP_TRAINING_PATTERN_1:
  607. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
  608. break;
  609. case DP_TRAINING_PATTERN_2:
  610. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
  611. break;
  612. case DP_TRAINING_PATTERN_3:
  613. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
  614. break;
  615. }
  616. atombios_dig_encoder_setup(dp_info->encoder, rtp, 0);
  617. } else {
  618. switch (tp) {
  619. case DP_TRAINING_PATTERN_1:
  620. rtp = 0;
  621. break;
  622. case DP_TRAINING_PATTERN_2:
  623. rtp = 1;
  624. break;
  625. }
  626. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
  627. dp_info->dp_clock, dp_info->enc_id, rtp);
  628. }
  629. /* enable training pattern on the sink */
  630. radeon_write_dpcd_reg(dp_info->radeon_connector, DP_TRAINING_PATTERN_SET, tp);
  631. }
  632. static int radeon_dp_link_train_init(struct radeon_dp_link_train_info *dp_info)
  633. {
  634. struct radeon_encoder *radeon_encoder = to_radeon_encoder(dp_info->encoder);
  635. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  636. u8 tmp;
  637. /* power up the sink */
  638. if (dp_info->dpcd[0] >= 0x11)
  639. radeon_write_dpcd_reg(dp_info->radeon_connector,
  640. DP_SET_POWER, DP_SET_POWER_D0);
  641. /* possibly enable downspread on the sink */
  642. if (dp_info->dpcd[3] & 0x1)
  643. radeon_write_dpcd_reg(dp_info->radeon_connector,
  644. DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
  645. else
  646. radeon_write_dpcd_reg(dp_info->radeon_connector,
  647. DP_DOWNSPREAD_CTRL, 0);
  648. if ((dp_info->connector->connector_type == DRM_MODE_CONNECTOR_eDP) &&
  649. (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)) {
  650. radeon_write_dpcd_reg(dp_info->radeon_connector, DP_EDP_CONFIGURATION_SET, 1);
  651. }
  652. /* set the lane count on the sink */
  653. tmp = dp_info->dp_lane_count;
  654. if (dp_info->dpcd[DP_DPCD_REV] >= 0x11 &&
  655. dp_info->dpcd[DP_MAX_LANE_COUNT] & DP_ENHANCED_FRAME_CAP)
  656. tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  657. radeon_write_dpcd_reg(dp_info->radeon_connector, DP_LANE_COUNT_SET, tmp);
  658. /* set the link rate on the sink */
  659. tmp = dp_get_dp_link_rate_coded(dp_info->dp_clock);
  660. radeon_write_dpcd_reg(dp_info->radeon_connector, DP_LINK_BW_SET, tmp);
  661. /* start training on the source */
  662. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
  663. atombios_dig_encoder_setup(dp_info->encoder,
  664. ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
  665. else
  666. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_START,
  667. dp_info->dp_clock, dp_info->enc_id, 0);
  668. /* disable the training pattern on the sink */
  669. radeon_write_dpcd_reg(dp_info->radeon_connector,
  670. DP_TRAINING_PATTERN_SET,
  671. DP_TRAINING_PATTERN_DISABLE);
  672. return 0;
  673. }
  674. static int radeon_dp_link_train_finish(struct radeon_dp_link_train_info *dp_info)
  675. {
  676. udelay(400);
  677. /* disable the training pattern on the sink */
  678. radeon_write_dpcd_reg(dp_info->radeon_connector,
  679. DP_TRAINING_PATTERN_SET,
  680. DP_TRAINING_PATTERN_DISABLE);
  681. /* disable the training pattern on the source */
  682. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
  683. atombios_dig_encoder_setup(dp_info->encoder,
  684. ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
  685. else
  686. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
  687. dp_info->dp_clock, dp_info->enc_id, 0);
  688. return 0;
  689. }
  690. static int radeon_dp_link_train_cr(struct radeon_dp_link_train_info *dp_info)
  691. {
  692. bool clock_recovery;
  693. u8 voltage;
  694. int i;
  695. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
  696. memset(dp_info->train_set, 0, 4);
  697. radeon_dp_update_vs_emph(dp_info);
  698. udelay(400);
  699. /* clock recovery loop */
  700. clock_recovery = false;
  701. dp_info->tries = 0;
  702. voltage = 0xff;
  703. while (1) {
  704. if (dp_info->rd_interval == 0)
  705. udelay(100);
  706. else
  707. mdelay(dp_info->rd_interval * 4);
  708. if (!radeon_dp_get_link_status(dp_info->radeon_connector, dp_info->link_status)) {
  709. DRM_ERROR("displayport link status failed\n");
  710. break;
  711. }
  712. if (dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
  713. clock_recovery = true;
  714. break;
  715. }
  716. for (i = 0; i < dp_info->dp_lane_count; i++) {
  717. if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  718. break;
  719. }
  720. if (i == dp_info->dp_lane_count) {
  721. DRM_ERROR("clock recovery reached max voltage\n");
  722. break;
  723. }
  724. if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  725. ++dp_info->tries;
  726. if (dp_info->tries == 5) {
  727. DRM_ERROR("clock recovery tried 5 times\n");
  728. break;
  729. }
  730. } else
  731. dp_info->tries = 0;
  732. voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  733. /* Compute new train_set as requested by sink */
  734. dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
  735. radeon_dp_update_vs_emph(dp_info);
  736. }
  737. if (!clock_recovery) {
  738. DRM_ERROR("clock recovery failed\n");
  739. return -1;
  740. } else {
  741. DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
  742. dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  743. (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
  744. DP_TRAIN_PRE_EMPHASIS_SHIFT);
  745. return 0;
  746. }
  747. }
  748. static int radeon_dp_link_train_ce(struct radeon_dp_link_train_info *dp_info)
  749. {
  750. bool channel_eq;
  751. if (dp_info->tp3_supported)
  752. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
  753. else
  754. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
  755. /* channel equalization loop */
  756. dp_info->tries = 0;
  757. channel_eq = false;
  758. while (1) {
  759. if (dp_info->rd_interval == 0)
  760. udelay(400);
  761. else
  762. mdelay(dp_info->rd_interval * 4);
  763. if (!radeon_dp_get_link_status(dp_info->radeon_connector, dp_info->link_status)) {
  764. DRM_ERROR("displayport link status failed\n");
  765. break;
  766. }
  767. if (dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
  768. channel_eq = true;
  769. break;
  770. }
  771. /* Try 5 times */
  772. if (dp_info->tries > 5) {
  773. DRM_ERROR("channel eq failed: 5 tries\n");
  774. break;
  775. }
  776. /* Compute new train_set as requested by sink */
  777. dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
  778. radeon_dp_update_vs_emph(dp_info);
  779. dp_info->tries++;
  780. }
  781. if (!channel_eq) {
  782. DRM_ERROR("channel eq failed\n");
  783. return -1;
  784. } else {
  785. DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
  786. dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  787. (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
  788. >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
  789. return 0;
  790. }
  791. }
  792. void radeon_dp_link_train(struct drm_encoder *encoder,
  793. struct drm_connector *connector)
  794. {
  795. struct drm_device *dev = encoder->dev;
  796. struct radeon_device *rdev = dev->dev_private;
  797. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  798. struct radeon_encoder_atom_dig *dig;
  799. struct radeon_connector *radeon_connector;
  800. struct radeon_connector_atom_dig *dig_connector;
  801. struct radeon_dp_link_train_info dp_info;
  802. int index;
  803. u8 tmp, frev, crev;
  804. if (!radeon_encoder->enc_priv)
  805. return;
  806. dig = radeon_encoder->enc_priv;
  807. radeon_connector = to_radeon_connector(connector);
  808. if (!radeon_connector->con_priv)
  809. return;
  810. dig_connector = radeon_connector->con_priv;
  811. if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
  812. (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
  813. return;
  814. /* DPEncoderService newer than 1.1 can't program properly the
  815. * training pattern. When facing such version use the
  816. * DIGXEncoderControl (X== 1 | 2)
  817. */
  818. dp_info.use_dpencoder = true;
  819. index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
  820. if (atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev)) {
  821. if (crev > 1) {
  822. dp_info.use_dpencoder = false;
  823. }
  824. }
  825. dp_info.enc_id = 0;
  826. if (dig->dig_encoder)
  827. dp_info.enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
  828. else
  829. dp_info.enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
  830. if (dig->linkb)
  831. dp_info.enc_id |= ATOM_DP_CONFIG_LINK_B;
  832. else
  833. dp_info.enc_id |= ATOM_DP_CONFIG_LINK_A;
  834. dp_info.rd_interval = radeon_read_dpcd_reg(radeon_connector, DP_TRAINING_AUX_RD_INTERVAL);
  835. tmp = radeon_read_dpcd_reg(radeon_connector, DP_MAX_LANE_COUNT);
  836. if (ASIC_IS_DCE5(rdev) && (tmp & DP_TPS3_SUPPORTED))
  837. dp_info.tp3_supported = true;
  838. else
  839. dp_info.tp3_supported = false;
  840. memcpy(dp_info.dpcd, dig_connector->dpcd, 8);
  841. dp_info.rdev = rdev;
  842. dp_info.encoder = encoder;
  843. dp_info.connector = connector;
  844. dp_info.radeon_connector = radeon_connector;
  845. dp_info.dp_lane_count = dig_connector->dp_lane_count;
  846. dp_info.dp_clock = dig_connector->dp_clock;
  847. if (radeon_dp_link_train_init(&dp_info))
  848. goto done;
  849. if (radeon_dp_link_train_cr(&dp_info))
  850. goto done;
  851. if (radeon_dp_link_train_ce(&dp_info))
  852. goto done;
  853. done:
  854. if (radeon_dp_link_train_finish(&dp_info))
  855. return;
  856. }