atombios_dp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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/amdgpu_drm.h>
  29. #include "amdgpu.h"
  30. #include "atom.h"
  31. #include "atom-bits.h"
  32. #include "atombios_encoders.h"
  33. #include "atombios_dp.h"
  34. #include "amdgpu_connectors.h"
  35. #include "amdgpu_atombios.h"
  36. #include <drm/drm_dp_helper.h>
  37. /* move these to drm_dp_helper.c/h */
  38. #define DP_LINK_CONFIGURATION_SIZE 9
  39. #define DP_DPCD_SIZE DP_RECEIVER_CAP_SIZE
  40. static char *voltage_names[] = {
  41. "0.4V", "0.6V", "0.8V", "1.2V"
  42. };
  43. static char *pre_emph_names[] = {
  44. "0dB", "3.5dB", "6dB", "9.5dB"
  45. };
  46. /***** amdgpu AUX functions *****/
  47. union aux_channel_transaction {
  48. PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION v1;
  49. PROCESS_AUX_CHANNEL_TRANSACTION_PARAMETERS_V2 v2;
  50. };
  51. static int amdgpu_atombios_dp_process_aux_ch(struct amdgpu_i2c_chan *chan,
  52. u8 *send, int send_bytes,
  53. u8 *recv, int recv_size,
  54. u8 delay, u8 *ack)
  55. {
  56. struct drm_device *dev = chan->dev;
  57. struct amdgpu_device *adev = dev->dev_private;
  58. union aux_channel_transaction args;
  59. int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
  60. unsigned char *base;
  61. int recv_bytes;
  62. int r = 0;
  63. memset(&args, 0, sizeof(args));
  64. mutex_lock(&chan->mutex);
  65. base = (unsigned char *)(adev->mode_info.atom_context->scratch + 1);
  66. amdgpu_atombios_copy_swap(base, send, send_bytes, true);
  67. args.v2.lpAuxRequest = cpu_to_le16((u16)(0 + 4));
  68. args.v2.lpDataOut = cpu_to_le16((u16)(16 + 4));
  69. args.v2.ucDataOutLen = 0;
  70. args.v2.ucChannelID = chan->rec.i2c_id;
  71. args.v2.ucDelay = delay / 10;
  72. args.v2.ucHPD_ID = chan->rec.hpd;
  73. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  74. *ack = args.v2.ucReplyStatus;
  75. /* timeout */
  76. if (args.v2.ucReplyStatus == 1) {
  77. DRM_DEBUG_KMS("dp_aux_ch timeout\n");
  78. r = -ETIMEDOUT;
  79. goto done;
  80. }
  81. /* flags not zero */
  82. if (args.v2.ucReplyStatus == 2) {
  83. DRM_DEBUG_KMS("dp_aux_ch flags not zero\n");
  84. r = -EIO;
  85. goto done;
  86. }
  87. /* error */
  88. if (args.v2.ucReplyStatus == 3) {
  89. DRM_DEBUG_KMS("dp_aux_ch error\n");
  90. r = -EIO;
  91. goto done;
  92. }
  93. recv_bytes = args.v1.ucDataOutLen;
  94. if (recv_bytes > recv_size)
  95. recv_bytes = recv_size;
  96. if (recv && recv_size)
  97. amdgpu_atombios_copy_swap(recv, base + 16, recv_bytes, false);
  98. r = recv_bytes;
  99. done:
  100. mutex_unlock(&chan->mutex);
  101. return r;
  102. }
  103. #define BARE_ADDRESS_SIZE 3
  104. #define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
  105. static ssize_t
  106. amdgpu_atombios_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  107. {
  108. struct amdgpu_i2c_chan *chan =
  109. container_of(aux, struct amdgpu_i2c_chan, aux);
  110. int ret;
  111. u8 tx_buf[20];
  112. size_t tx_size;
  113. u8 ack, delay = 0;
  114. if (WARN_ON(msg->size > 16))
  115. return -E2BIG;
  116. tx_buf[0] = msg->address & 0xff;
  117. tx_buf[1] = msg->address >> 8;
  118. tx_buf[2] = (msg->request << 4) |
  119. ((msg->address >> 16) & 0xf);
  120. tx_buf[3] = msg->size ? (msg->size - 1) : 0;
  121. switch (msg->request & ~DP_AUX_I2C_MOT) {
  122. case DP_AUX_NATIVE_WRITE:
  123. case DP_AUX_I2C_WRITE:
  124. /* tx_size needs to be 4 even for bare address packets since the atom
  125. * table needs the info in tx_buf[3].
  126. */
  127. tx_size = HEADER_SIZE + msg->size;
  128. if (msg->size == 0)
  129. tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
  130. else
  131. tx_buf[3] |= tx_size << 4;
  132. memcpy(tx_buf + HEADER_SIZE, msg->buffer, msg->size);
  133. ret = amdgpu_atombios_dp_process_aux_ch(chan,
  134. tx_buf, tx_size, NULL, 0, delay, &ack);
  135. if (ret >= 0)
  136. /* Return payload size. */
  137. ret = msg->size;
  138. break;
  139. case DP_AUX_NATIVE_READ:
  140. case DP_AUX_I2C_READ:
  141. /* tx_size needs to be 4 even for bare address packets since the atom
  142. * table needs the info in tx_buf[3].
  143. */
  144. tx_size = HEADER_SIZE;
  145. if (msg->size == 0)
  146. tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
  147. else
  148. tx_buf[3] |= tx_size << 4;
  149. ret = amdgpu_atombios_dp_process_aux_ch(chan,
  150. tx_buf, tx_size, msg->buffer, msg->size, delay, &ack);
  151. break;
  152. default:
  153. ret = -EINVAL;
  154. break;
  155. }
  156. if (ret >= 0)
  157. msg->reply = ack >> 4;
  158. return ret;
  159. }
  160. void amdgpu_atombios_dp_aux_init(struct amdgpu_connector *amdgpu_connector)
  161. {
  162. int ret;
  163. amdgpu_connector->ddc_bus->rec.hpd = amdgpu_connector->hpd.hpd;
  164. amdgpu_connector->ddc_bus->aux.dev = amdgpu_connector->base.kdev;
  165. amdgpu_connector->ddc_bus->aux.transfer = amdgpu_atombios_dp_aux_transfer;
  166. ret = drm_dp_aux_register(&amdgpu_connector->ddc_bus->aux);
  167. if (!ret)
  168. amdgpu_connector->ddc_bus->has_aux = true;
  169. WARN(ret, "drm_dp_aux_register_i2c_bus() failed with error %d\n", ret);
  170. }
  171. /***** general DP utility functions *****/
  172. #define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_LEVEL_3
  173. #define DP_PRE_EMPHASIS_MAX DP_TRAIN_PRE_EMPH_LEVEL_3
  174. static void amdgpu_atombios_dp_get_adjust_train(const u8 link_status[DP_LINK_STATUS_SIZE],
  175. int lane_count,
  176. u8 train_set[4])
  177. {
  178. u8 v = 0;
  179. u8 p = 0;
  180. int lane;
  181. for (lane = 0; lane < lane_count; lane++) {
  182. u8 this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
  183. u8 this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
  184. DRM_DEBUG_KMS("requested signal parameters: lane %d voltage %s pre_emph %s\n",
  185. lane,
  186. voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
  187. pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
  188. if (this_v > v)
  189. v = this_v;
  190. if (this_p > p)
  191. p = this_p;
  192. }
  193. if (v >= DP_VOLTAGE_MAX)
  194. v |= DP_TRAIN_MAX_SWING_REACHED;
  195. if (p >= DP_PRE_EMPHASIS_MAX)
  196. p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  197. DRM_DEBUG_KMS("using signal parameters: voltage %s pre_emph %s\n",
  198. voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
  199. pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
  200. for (lane = 0; lane < 4; lane++)
  201. train_set[lane] = v | p;
  202. }
  203. /* convert bits per color to bits per pixel */
  204. /* get bpc from the EDID */
  205. static unsigned amdgpu_atombios_dp_convert_bpc_to_bpp(int bpc)
  206. {
  207. if (bpc == 0)
  208. return 24;
  209. else
  210. return bpc * 3;
  211. }
  212. /***** amdgpu specific DP functions *****/
  213. static int amdgpu_atombios_dp_get_dp_link_config(struct drm_connector *connector,
  214. const u8 dpcd[DP_DPCD_SIZE],
  215. unsigned pix_clock,
  216. unsigned *dp_lanes, unsigned *dp_rate)
  217. {
  218. unsigned bpp =
  219. amdgpu_atombios_dp_convert_bpc_to_bpp(amdgpu_connector_get_monitor_bpc(connector));
  220. static const unsigned link_rates[3] = { 162000, 270000, 540000 };
  221. unsigned max_link_rate = drm_dp_max_link_rate(dpcd);
  222. unsigned max_lane_num = drm_dp_max_lane_count(dpcd);
  223. unsigned lane_num, i, max_pix_clock;
  224. if (amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector) ==
  225. ENCODER_OBJECT_ID_NUTMEG) {
  226. for (lane_num = 1; lane_num <= max_lane_num; lane_num <<= 1) {
  227. max_pix_clock = (lane_num * 270000 * 8) / bpp;
  228. if (max_pix_clock >= pix_clock) {
  229. *dp_lanes = lane_num;
  230. *dp_rate = 270000;
  231. return 0;
  232. }
  233. }
  234. } else {
  235. for (i = 0; i < ARRAY_SIZE(link_rates) && link_rates[i] <= max_link_rate; i++) {
  236. for (lane_num = 1; lane_num <= max_lane_num; lane_num <<= 1) {
  237. max_pix_clock = (lane_num * link_rates[i] * 8) / bpp;
  238. if (max_pix_clock >= pix_clock) {
  239. *dp_lanes = lane_num;
  240. *dp_rate = link_rates[i];
  241. return 0;
  242. }
  243. }
  244. }
  245. }
  246. return -EINVAL;
  247. }
  248. static u8 amdgpu_atombios_dp_encoder_service(struct amdgpu_device *adev,
  249. int action, int dp_clock,
  250. u8 ucconfig, u8 lane_num)
  251. {
  252. DP_ENCODER_SERVICE_PARAMETERS args;
  253. int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
  254. memset(&args, 0, sizeof(args));
  255. args.ucLinkClock = dp_clock / 10;
  256. args.ucConfig = ucconfig;
  257. args.ucAction = action;
  258. args.ucLaneNum = lane_num;
  259. args.ucStatus = 0;
  260. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  261. return args.ucStatus;
  262. }
  263. u8 amdgpu_atombios_dp_get_sinktype(struct amdgpu_connector *amdgpu_connector)
  264. {
  265. struct drm_device *dev = amdgpu_connector->base.dev;
  266. struct amdgpu_device *adev = dev->dev_private;
  267. return amdgpu_atombios_dp_encoder_service(adev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
  268. amdgpu_connector->ddc_bus->rec.i2c_id, 0);
  269. }
  270. static void amdgpu_atombios_dp_probe_oui(struct amdgpu_connector *amdgpu_connector)
  271. {
  272. struct amdgpu_connector_atom_dig *dig_connector = amdgpu_connector->con_priv;
  273. u8 buf[3];
  274. if (!(dig_connector->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
  275. return;
  276. if (drm_dp_dpcd_read(&amdgpu_connector->ddc_bus->aux, DP_SINK_OUI, buf, 3) == 3)
  277. DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
  278. buf[0], buf[1], buf[2]);
  279. if (drm_dp_dpcd_read(&amdgpu_connector->ddc_bus->aux, DP_BRANCH_OUI, buf, 3) == 3)
  280. DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
  281. buf[0], buf[1], buf[2]);
  282. }
  283. int amdgpu_atombios_dp_get_dpcd(struct amdgpu_connector *amdgpu_connector)
  284. {
  285. struct amdgpu_connector_atom_dig *dig_connector = amdgpu_connector->con_priv;
  286. u8 msg[DP_DPCD_SIZE];
  287. int ret, i;
  288. for (i = 0; i < 7; i++) {
  289. ret = drm_dp_dpcd_read(&amdgpu_connector->ddc_bus->aux, DP_DPCD_REV, msg,
  290. DP_DPCD_SIZE);
  291. if (ret == DP_DPCD_SIZE) {
  292. memcpy(dig_connector->dpcd, msg, DP_DPCD_SIZE);
  293. DRM_DEBUG_KMS("DPCD: %*ph\n", (int)sizeof(dig_connector->dpcd),
  294. dig_connector->dpcd);
  295. amdgpu_atombios_dp_probe_oui(amdgpu_connector);
  296. return 0;
  297. }
  298. }
  299. dig_connector->dpcd[0] = 0;
  300. return -EINVAL;
  301. }
  302. int amdgpu_atombios_dp_get_panel_mode(struct drm_encoder *encoder,
  303. struct drm_connector *connector)
  304. {
  305. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  306. struct amdgpu_connector_atom_dig *dig_connector;
  307. int panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
  308. u16 dp_bridge = amdgpu_connector_encoder_get_dp_bridge_encoder_id(connector);
  309. u8 tmp;
  310. if (!amdgpu_connector->con_priv)
  311. return panel_mode;
  312. dig_connector = amdgpu_connector->con_priv;
  313. if (dp_bridge != ENCODER_OBJECT_ID_NONE) {
  314. /* DP bridge chips */
  315. if (drm_dp_dpcd_readb(&amdgpu_connector->ddc_bus->aux,
  316. DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
  317. if (tmp & 1)
  318. panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
  319. else if ((dp_bridge == ENCODER_OBJECT_ID_NUTMEG) ||
  320. (dp_bridge == ENCODER_OBJECT_ID_TRAVIS))
  321. panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
  322. else
  323. panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
  324. }
  325. } else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
  326. /* eDP */
  327. if (drm_dp_dpcd_readb(&amdgpu_connector->ddc_bus->aux,
  328. DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
  329. if (tmp & 1)
  330. panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
  331. }
  332. }
  333. return panel_mode;
  334. }
  335. void amdgpu_atombios_dp_set_link_config(struct drm_connector *connector,
  336. const struct drm_display_mode *mode)
  337. {
  338. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  339. struct amdgpu_connector_atom_dig *dig_connector;
  340. int ret;
  341. if (!amdgpu_connector->con_priv)
  342. return;
  343. dig_connector = amdgpu_connector->con_priv;
  344. if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
  345. (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
  346. ret = amdgpu_atombios_dp_get_dp_link_config(connector, dig_connector->dpcd,
  347. mode->clock,
  348. &dig_connector->dp_lane_count,
  349. &dig_connector->dp_clock);
  350. if (ret) {
  351. dig_connector->dp_clock = 0;
  352. dig_connector->dp_lane_count = 0;
  353. }
  354. }
  355. }
  356. int amdgpu_atombios_dp_mode_valid_helper(struct drm_connector *connector,
  357. struct drm_display_mode *mode)
  358. {
  359. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  360. struct amdgpu_connector_atom_dig *dig_connector;
  361. unsigned dp_lanes, dp_clock;
  362. int ret;
  363. if (!amdgpu_connector->con_priv)
  364. return MODE_CLOCK_HIGH;
  365. dig_connector = amdgpu_connector->con_priv;
  366. ret = amdgpu_atombios_dp_get_dp_link_config(connector, dig_connector->dpcd,
  367. mode->clock, &dp_lanes, &dp_clock);
  368. if (ret)
  369. return MODE_CLOCK_HIGH;
  370. if ((dp_clock == 540000) &&
  371. (!amdgpu_connector_is_dp12_capable(connector)))
  372. return MODE_CLOCK_HIGH;
  373. return MODE_OK;
  374. }
  375. bool amdgpu_atombios_dp_needs_link_train(struct amdgpu_connector *amdgpu_connector)
  376. {
  377. u8 link_status[DP_LINK_STATUS_SIZE];
  378. struct amdgpu_connector_atom_dig *dig = amdgpu_connector->con_priv;
  379. if (drm_dp_dpcd_read_link_status(&amdgpu_connector->ddc_bus->aux, link_status)
  380. <= 0)
  381. return false;
  382. if (drm_dp_channel_eq_ok(link_status, dig->dp_lane_count))
  383. return false;
  384. return true;
  385. }
  386. void amdgpu_atombios_dp_set_rx_power_state(struct drm_connector *connector,
  387. u8 power_state)
  388. {
  389. struct amdgpu_connector *amdgpu_connector = to_amdgpu_connector(connector);
  390. struct amdgpu_connector_atom_dig *dig_connector;
  391. if (!amdgpu_connector->con_priv)
  392. return;
  393. dig_connector = amdgpu_connector->con_priv;
  394. /* power up/down the sink */
  395. if (dig_connector->dpcd[0] >= 0x11) {
  396. drm_dp_dpcd_writeb(&amdgpu_connector->ddc_bus->aux,
  397. DP_SET_POWER, power_state);
  398. usleep_range(1000, 2000);
  399. }
  400. }
  401. struct amdgpu_atombios_dp_link_train_info {
  402. struct amdgpu_device *adev;
  403. struct drm_encoder *encoder;
  404. struct drm_connector *connector;
  405. int dp_clock;
  406. int dp_lane_count;
  407. bool tp3_supported;
  408. u8 dpcd[DP_RECEIVER_CAP_SIZE];
  409. u8 train_set[4];
  410. u8 link_status[DP_LINK_STATUS_SIZE];
  411. u8 tries;
  412. struct drm_dp_aux *aux;
  413. };
  414. static void
  415. amdgpu_atombios_dp_update_vs_emph(struct amdgpu_atombios_dp_link_train_info *dp_info)
  416. {
  417. /* set the initial vs/emph on the source */
  418. amdgpu_atombios_encoder_setup_dig_transmitter(dp_info->encoder,
  419. ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
  420. 0, dp_info->train_set[0]); /* sets all lanes at once */
  421. /* set the vs/emph on the sink */
  422. drm_dp_dpcd_write(dp_info->aux, DP_TRAINING_LANE0_SET,
  423. dp_info->train_set, dp_info->dp_lane_count);
  424. }
  425. static void
  426. amdgpu_atombios_dp_set_tp(struct amdgpu_atombios_dp_link_train_info *dp_info, int tp)
  427. {
  428. int rtp = 0;
  429. /* set training pattern on the source */
  430. switch (tp) {
  431. case DP_TRAINING_PATTERN_1:
  432. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
  433. break;
  434. case DP_TRAINING_PATTERN_2:
  435. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
  436. break;
  437. case DP_TRAINING_PATTERN_3:
  438. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
  439. break;
  440. }
  441. amdgpu_atombios_encoder_setup_dig_encoder(dp_info->encoder, rtp, 0);
  442. /* enable training pattern on the sink */
  443. drm_dp_dpcd_writeb(dp_info->aux, DP_TRAINING_PATTERN_SET, tp);
  444. }
  445. static int
  446. amdgpu_atombios_dp_link_train_init(struct amdgpu_atombios_dp_link_train_info *dp_info)
  447. {
  448. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(dp_info->encoder);
  449. struct amdgpu_encoder_atom_dig *dig = amdgpu_encoder->enc_priv;
  450. u8 tmp;
  451. /* power up the sink */
  452. amdgpu_atombios_dp_set_rx_power_state(dp_info->connector, DP_SET_POWER_D0);
  453. /* possibly enable downspread on the sink */
  454. if (dp_info->dpcd[3] & 0x1)
  455. drm_dp_dpcd_writeb(dp_info->aux,
  456. DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
  457. else
  458. drm_dp_dpcd_writeb(dp_info->aux,
  459. DP_DOWNSPREAD_CTRL, 0);
  460. if (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)
  461. drm_dp_dpcd_writeb(dp_info->aux, DP_EDP_CONFIGURATION_SET, 1);
  462. /* set the lane count on the sink */
  463. tmp = dp_info->dp_lane_count;
  464. if (drm_dp_enhanced_frame_cap(dp_info->dpcd))
  465. tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  466. drm_dp_dpcd_writeb(dp_info->aux, DP_LANE_COUNT_SET, tmp);
  467. /* set the link rate on the sink */
  468. tmp = drm_dp_link_rate_to_bw_code(dp_info->dp_clock);
  469. drm_dp_dpcd_writeb(dp_info->aux, DP_LINK_BW_SET, tmp);
  470. /* start training on the source */
  471. amdgpu_atombios_encoder_setup_dig_encoder(dp_info->encoder,
  472. ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
  473. /* disable the training pattern on the sink */
  474. drm_dp_dpcd_writeb(dp_info->aux,
  475. DP_TRAINING_PATTERN_SET,
  476. DP_TRAINING_PATTERN_DISABLE);
  477. return 0;
  478. }
  479. static int
  480. amdgpu_atombios_dp_link_train_finish(struct amdgpu_atombios_dp_link_train_info *dp_info)
  481. {
  482. udelay(400);
  483. /* disable the training pattern on the sink */
  484. drm_dp_dpcd_writeb(dp_info->aux,
  485. DP_TRAINING_PATTERN_SET,
  486. DP_TRAINING_PATTERN_DISABLE);
  487. /* disable the training pattern on the source */
  488. amdgpu_atombios_encoder_setup_dig_encoder(dp_info->encoder,
  489. ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
  490. return 0;
  491. }
  492. static int
  493. amdgpu_atombios_dp_link_train_cr(struct amdgpu_atombios_dp_link_train_info *dp_info)
  494. {
  495. bool clock_recovery;
  496. u8 voltage;
  497. int i;
  498. amdgpu_atombios_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
  499. memset(dp_info->train_set, 0, 4);
  500. amdgpu_atombios_dp_update_vs_emph(dp_info);
  501. udelay(400);
  502. /* clock recovery loop */
  503. clock_recovery = false;
  504. dp_info->tries = 0;
  505. voltage = 0xff;
  506. while (1) {
  507. drm_dp_link_train_clock_recovery_delay(dp_info->dpcd);
  508. if (drm_dp_dpcd_read_link_status(dp_info->aux,
  509. dp_info->link_status) <= 0) {
  510. DRM_ERROR("displayport link status failed\n");
  511. break;
  512. }
  513. if (drm_dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
  514. clock_recovery = true;
  515. break;
  516. }
  517. for (i = 0; i < dp_info->dp_lane_count; i++) {
  518. if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  519. break;
  520. }
  521. if (i == dp_info->dp_lane_count) {
  522. DRM_ERROR("clock recovery reached max voltage\n");
  523. break;
  524. }
  525. if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  526. ++dp_info->tries;
  527. if (dp_info->tries == 5) {
  528. DRM_ERROR("clock recovery tried 5 times\n");
  529. break;
  530. }
  531. } else
  532. dp_info->tries = 0;
  533. voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  534. /* Compute new train_set as requested by sink */
  535. amdgpu_atombios_dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count,
  536. dp_info->train_set);
  537. amdgpu_atombios_dp_update_vs_emph(dp_info);
  538. }
  539. if (!clock_recovery) {
  540. DRM_ERROR("clock recovery failed\n");
  541. return -1;
  542. } else {
  543. DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
  544. dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  545. (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
  546. DP_TRAIN_PRE_EMPHASIS_SHIFT);
  547. return 0;
  548. }
  549. }
  550. static int
  551. amdgpu_atombios_dp_link_train_ce(struct amdgpu_atombios_dp_link_train_info *dp_info)
  552. {
  553. bool channel_eq;
  554. if (dp_info->tp3_supported)
  555. amdgpu_atombios_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
  556. else
  557. amdgpu_atombios_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
  558. /* channel equalization loop */
  559. dp_info->tries = 0;
  560. channel_eq = false;
  561. while (1) {
  562. drm_dp_link_train_channel_eq_delay(dp_info->dpcd);
  563. if (drm_dp_dpcd_read_link_status(dp_info->aux,
  564. dp_info->link_status) <= 0) {
  565. DRM_ERROR("displayport link status failed\n");
  566. break;
  567. }
  568. if (drm_dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
  569. channel_eq = true;
  570. break;
  571. }
  572. /* Try 5 times */
  573. if (dp_info->tries > 5) {
  574. DRM_ERROR("channel eq failed: 5 tries\n");
  575. break;
  576. }
  577. /* Compute new train_set as requested by sink */
  578. amdgpu_atombios_dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count,
  579. dp_info->train_set);
  580. amdgpu_atombios_dp_update_vs_emph(dp_info);
  581. dp_info->tries++;
  582. }
  583. if (!channel_eq) {
  584. DRM_ERROR("channel eq failed\n");
  585. return -1;
  586. } else {
  587. DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
  588. dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  589. (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
  590. >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
  591. return 0;
  592. }
  593. }
  594. void amdgpu_atombios_dp_link_train(struct drm_encoder *encoder,
  595. struct drm_connector *connector)
  596. {
  597. struct drm_device *dev = encoder->dev;
  598. struct amdgpu_device *adev = dev->dev_private;
  599. struct amdgpu_encoder *amdgpu_encoder = to_amdgpu_encoder(encoder);
  600. struct amdgpu_encoder_atom_dig *dig;
  601. struct amdgpu_connector *amdgpu_connector;
  602. struct amdgpu_connector_atom_dig *dig_connector;
  603. struct amdgpu_atombios_dp_link_train_info dp_info;
  604. u8 tmp;
  605. if (!amdgpu_encoder->enc_priv)
  606. return;
  607. dig = amdgpu_encoder->enc_priv;
  608. amdgpu_connector = to_amdgpu_connector(connector);
  609. if (!amdgpu_connector->con_priv)
  610. return;
  611. dig_connector = amdgpu_connector->con_priv;
  612. if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
  613. (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
  614. return;
  615. if (drm_dp_dpcd_readb(&amdgpu_connector->ddc_bus->aux, DP_MAX_LANE_COUNT, &tmp)
  616. == 1) {
  617. if (tmp & DP_TPS3_SUPPORTED)
  618. dp_info.tp3_supported = true;
  619. else
  620. dp_info.tp3_supported = false;
  621. } else {
  622. dp_info.tp3_supported = false;
  623. }
  624. memcpy(dp_info.dpcd, dig_connector->dpcd, DP_RECEIVER_CAP_SIZE);
  625. dp_info.adev = adev;
  626. dp_info.encoder = encoder;
  627. dp_info.connector = connector;
  628. dp_info.dp_lane_count = dig_connector->dp_lane_count;
  629. dp_info.dp_clock = dig_connector->dp_clock;
  630. dp_info.aux = &amdgpu_connector->ddc_bus->aux;
  631. if (amdgpu_atombios_dp_link_train_init(&dp_info))
  632. goto done;
  633. if (amdgpu_atombios_dp_link_train_cr(&dp_info))
  634. goto done;
  635. if (amdgpu_atombios_dp_link_train_ce(&dp_info))
  636. goto done;
  637. done:
  638. if (amdgpu_atombios_dp_link_train_finish(&dp_info))
  639. return;
  640. }