atombios_dp.c 27 KB

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