atombios_dp.c 27 KB

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