atombios_dp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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_DPCD_SIZE DP_RECEIVER_CAP_SIZE
  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. /* Atom needs data in little endian format so swap as appropriate when copying
  44. * data to or from atom. Note that atom operates on dw units.
  45. *
  46. * Use to_le=true when sending data to atom and provide at least
  47. * ALIGN(num_bytes,4) bytes in the dst buffer.
  48. *
  49. * Use to_le=false when receiving data from atom and provide ALIGN(num_bytes,4)
  50. * byes in the src buffer.
  51. */
  52. void radeon_atom_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
  53. {
  54. #ifdef __BIG_ENDIAN
  55. u32 src_tmp[5], dst_tmp[5];
  56. int i;
  57. u8 align_num_bytes = ALIGN(num_bytes, 4);
  58. if (to_le) {
  59. memcpy(src_tmp, src, num_bytes);
  60. for (i = 0; i < align_num_bytes / 4; i++)
  61. dst_tmp[i] = cpu_to_le32(src_tmp[i]);
  62. memcpy(dst, dst_tmp, align_num_bytes);
  63. } else {
  64. memcpy(src_tmp, src, align_num_bytes);
  65. for (i = 0; i < align_num_bytes / 4; i++)
  66. dst_tmp[i] = le32_to_cpu(src_tmp[i]);
  67. memcpy(dst, dst_tmp, num_bytes);
  68. }
  69. #else
  70. memcpy(dst, src, num_bytes);
  71. #endif
  72. }
  73. union aux_channel_transaction {
  74. PROCESS_AUX_CHANNEL_TRANSACTION_PS_ALLOCATION v1;
  75. PROCESS_AUX_CHANNEL_TRANSACTION_PARAMETERS_V2 v2;
  76. };
  77. static int radeon_process_aux_ch(struct radeon_i2c_chan *chan,
  78. u8 *send, int send_bytes,
  79. u8 *recv, int recv_size,
  80. u8 delay, u8 *ack)
  81. {
  82. struct drm_device *dev = chan->dev;
  83. struct radeon_device *rdev = dev->dev_private;
  84. union aux_channel_transaction args;
  85. int index = GetIndexIntoMasterTable(COMMAND, ProcessAuxChannelTransaction);
  86. unsigned char *base;
  87. int recv_bytes;
  88. int r = 0;
  89. memset(&args, 0, sizeof(args));
  90. mutex_lock(&chan->mutex);
  91. mutex_lock(&rdev->mode_info.atom_context->scratch_mutex);
  92. base = (unsigned char *)(rdev->mode_info.atom_context->scratch + 1);
  93. radeon_atom_copy_swap(base, send, send_bytes, true);
  94. args.v1.lpAuxRequest = cpu_to_le16((u16)(0 + 4));
  95. args.v1.lpDataOut = cpu_to_le16((u16)(16 + 4));
  96. args.v1.ucDataOutLen = 0;
  97. args.v1.ucChannelID = chan->rec.i2c_id;
  98. args.v1.ucDelay = delay / 10;
  99. if (ASIC_IS_DCE4(rdev))
  100. args.v2.ucHPD_ID = chan->rec.hpd;
  101. atom_execute_table_scratch_unlocked(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  102. *ack = args.v1.ucReplyStatus;
  103. /* timeout */
  104. if (args.v1.ucReplyStatus == 1) {
  105. DRM_DEBUG_KMS("dp_aux_ch timeout\n");
  106. r = -ETIMEDOUT;
  107. goto done;
  108. }
  109. /* flags not zero */
  110. if (args.v1.ucReplyStatus == 2) {
  111. DRM_DEBUG_KMS("dp_aux_ch flags not zero\n");
  112. r = -EIO;
  113. goto done;
  114. }
  115. /* error */
  116. if (args.v1.ucReplyStatus == 3) {
  117. DRM_DEBUG_KMS("dp_aux_ch error\n");
  118. r = -EIO;
  119. goto done;
  120. }
  121. recv_bytes = args.v1.ucDataOutLen;
  122. if (recv_bytes > recv_size)
  123. recv_bytes = recv_size;
  124. if (recv && recv_size)
  125. radeon_atom_copy_swap(recv, base + 16, recv_bytes, false);
  126. r = recv_bytes;
  127. done:
  128. mutex_unlock(&rdev->mode_info.atom_context->scratch_mutex);
  129. mutex_unlock(&chan->mutex);
  130. return r;
  131. }
  132. #define BARE_ADDRESS_SIZE 3
  133. #define HEADER_SIZE (BARE_ADDRESS_SIZE + 1)
  134. static ssize_t
  135. radeon_dp_aux_transfer_atom(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
  136. {
  137. struct radeon_i2c_chan *chan =
  138. container_of(aux, struct radeon_i2c_chan, aux);
  139. int ret;
  140. u8 tx_buf[20];
  141. size_t tx_size;
  142. u8 ack, delay = 0;
  143. if (WARN_ON(msg->size > 16))
  144. return -E2BIG;
  145. tx_buf[0] = msg->address & 0xff;
  146. tx_buf[1] = (msg->address >> 8) & 0xff;
  147. tx_buf[2] = (msg->request << 4) |
  148. ((msg->address >> 16) & 0xf);
  149. tx_buf[3] = msg->size ? (msg->size - 1) : 0;
  150. switch (msg->request & ~DP_AUX_I2C_MOT) {
  151. case DP_AUX_NATIVE_WRITE:
  152. case DP_AUX_I2C_WRITE:
  153. case DP_AUX_I2C_WRITE_STATUS_UPDATE:
  154. /* The atom implementation only supports writes with a max payload of
  155. * 12 bytes since it uses 4 bits for the total count (header + payload)
  156. * in the parameter space. The atom interface supports 16 byte
  157. * payloads for reads. The hw itself supports up to 16 bytes of payload.
  158. */
  159. if (WARN_ON_ONCE(msg->size > 12))
  160. return -E2BIG;
  161. /* tx_size needs to be 4 even for bare address packets since the atom
  162. * table needs the info in tx_buf[3].
  163. */
  164. tx_size = HEADER_SIZE + msg->size;
  165. if (msg->size == 0)
  166. tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
  167. else
  168. tx_buf[3] |= tx_size << 4;
  169. memcpy(tx_buf + HEADER_SIZE, msg->buffer, msg->size);
  170. ret = radeon_process_aux_ch(chan,
  171. tx_buf, tx_size, NULL, 0, delay, &ack);
  172. if (ret >= 0)
  173. /* Return payload size. */
  174. ret = msg->size;
  175. break;
  176. case DP_AUX_NATIVE_READ:
  177. case DP_AUX_I2C_READ:
  178. /* tx_size needs to be 4 even for bare address packets since the atom
  179. * table needs the info in tx_buf[3].
  180. */
  181. tx_size = HEADER_SIZE;
  182. if (msg->size == 0)
  183. tx_buf[3] |= BARE_ADDRESS_SIZE << 4;
  184. else
  185. tx_buf[3] |= tx_size << 4;
  186. ret = radeon_process_aux_ch(chan,
  187. tx_buf, tx_size, msg->buffer, msg->size, delay, &ack);
  188. break;
  189. default:
  190. ret = -EINVAL;
  191. break;
  192. }
  193. if (ret >= 0)
  194. msg->reply = ack >> 4;
  195. return ret;
  196. }
  197. void radeon_dp_aux_init(struct radeon_connector *radeon_connector)
  198. {
  199. struct drm_device *dev = radeon_connector->base.dev;
  200. struct radeon_device *rdev = dev->dev_private;
  201. int ret;
  202. radeon_connector->ddc_bus->rec.hpd = radeon_connector->hpd.hpd;
  203. radeon_connector->ddc_bus->aux.dev = radeon_connector->base.kdev;
  204. if (ASIC_IS_DCE5(rdev)) {
  205. if (radeon_auxch)
  206. radeon_connector->ddc_bus->aux.transfer = radeon_dp_aux_transfer_native;
  207. else
  208. radeon_connector->ddc_bus->aux.transfer = radeon_dp_aux_transfer_atom;
  209. } else {
  210. radeon_connector->ddc_bus->aux.transfer = radeon_dp_aux_transfer_atom;
  211. }
  212. ret = drm_dp_aux_register(&radeon_connector->ddc_bus->aux);
  213. if (!ret)
  214. radeon_connector->ddc_bus->has_aux = true;
  215. WARN(ret, "drm_dp_aux_register() failed with error %d\n", ret);
  216. }
  217. /***** general DP utility functions *****/
  218. #define DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_LEVEL_3
  219. #define DP_PRE_EMPHASIS_MAX DP_TRAIN_PRE_EMPH_LEVEL_3
  220. static void dp_get_adjust_train(const u8 link_status[DP_LINK_STATUS_SIZE],
  221. int lane_count,
  222. u8 train_set[4])
  223. {
  224. u8 v = 0;
  225. u8 p = 0;
  226. int lane;
  227. for (lane = 0; lane < lane_count; lane++) {
  228. u8 this_v = drm_dp_get_adjust_request_voltage(link_status, lane);
  229. u8 this_p = drm_dp_get_adjust_request_pre_emphasis(link_status, lane);
  230. DRM_DEBUG_KMS("requested signal parameters: lane %d voltage %s pre_emph %s\n",
  231. lane,
  232. voltage_names[this_v >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
  233. pre_emph_names[this_p >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
  234. if (this_v > v)
  235. v = this_v;
  236. if (this_p > p)
  237. p = this_p;
  238. }
  239. if (v >= DP_VOLTAGE_MAX)
  240. v |= DP_TRAIN_MAX_SWING_REACHED;
  241. if (p >= DP_PRE_EMPHASIS_MAX)
  242. p |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  243. DRM_DEBUG_KMS("using signal parameters: voltage %s pre_emph %s\n",
  244. voltage_names[(v & DP_TRAIN_VOLTAGE_SWING_MASK) >> DP_TRAIN_VOLTAGE_SWING_SHIFT],
  245. pre_emph_names[(p & DP_TRAIN_PRE_EMPHASIS_MASK) >> DP_TRAIN_PRE_EMPHASIS_SHIFT]);
  246. for (lane = 0; lane < 4; lane++)
  247. train_set[lane] = v | p;
  248. }
  249. /* convert bits per color to bits per pixel */
  250. /* get bpc from the EDID */
  251. static int convert_bpc_to_bpp(int bpc)
  252. {
  253. if (bpc == 0)
  254. return 24;
  255. else
  256. return bpc * 3;
  257. }
  258. /***** radeon specific DP functions *****/
  259. int radeon_dp_get_dp_link_config(struct drm_connector *connector,
  260. const u8 dpcd[DP_DPCD_SIZE],
  261. unsigned pix_clock,
  262. unsigned *dp_lanes, unsigned *dp_rate)
  263. {
  264. int bpp = convert_bpc_to_bpp(radeon_get_monitor_bpc(connector));
  265. static const unsigned link_rates[3] = { 162000, 270000, 540000 };
  266. unsigned max_link_rate = drm_dp_max_link_rate(dpcd);
  267. unsigned max_lane_num = drm_dp_max_lane_count(dpcd);
  268. unsigned lane_num, i, max_pix_clock;
  269. if (radeon_connector_encoder_get_dp_bridge_encoder_id(connector) ==
  270. ENCODER_OBJECT_ID_NUTMEG) {
  271. for (lane_num = 1; lane_num <= max_lane_num; lane_num <<= 1) {
  272. max_pix_clock = (lane_num * 270000 * 8) / bpp;
  273. if (max_pix_clock >= pix_clock) {
  274. *dp_lanes = lane_num;
  275. *dp_rate = 270000;
  276. return 0;
  277. }
  278. }
  279. } else {
  280. for (i = 0; i < ARRAY_SIZE(link_rates) && link_rates[i] <= max_link_rate; i++) {
  281. for (lane_num = 1; lane_num <= max_lane_num; lane_num <<= 1) {
  282. max_pix_clock = (lane_num * link_rates[i] * 8) / bpp;
  283. if (max_pix_clock >= pix_clock) {
  284. *dp_lanes = lane_num;
  285. *dp_rate = link_rates[i];
  286. return 0;
  287. }
  288. }
  289. }
  290. }
  291. return -EINVAL;
  292. }
  293. static u8 radeon_dp_encoder_service(struct radeon_device *rdev,
  294. int action, int dp_clock,
  295. u8 ucconfig, u8 lane_num)
  296. {
  297. DP_ENCODER_SERVICE_PARAMETERS args;
  298. int index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
  299. memset(&args, 0, sizeof(args));
  300. args.ucLinkClock = dp_clock / 10;
  301. args.ucConfig = ucconfig;
  302. args.ucAction = action;
  303. args.ucLaneNum = lane_num;
  304. args.ucStatus = 0;
  305. atom_execute_table(rdev->mode_info.atom_context, index, (uint32_t *)&args);
  306. return args.ucStatus;
  307. }
  308. u8 radeon_dp_getsinktype(struct radeon_connector *radeon_connector)
  309. {
  310. struct drm_device *dev = radeon_connector->base.dev;
  311. struct radeon_device *rdev = dev->dev_private;
  312. return radeon_dp_encoder_service(rdev, ATOM_DP_ACTION_GET_SINK_TYPE, 0,
  313. radeon_connector->ddc_bus->rec.i2c_id, 0);
  314. }
  315. static void radeon_dp_probe_oui(struct radeon_connector *radeon_connector)
  316. {
  317. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  318. u8 buf[3];
  319. if (!(dig_connector->dpcd[DP_DOWN_STREAM_PORT_COUNT] & DP_OUI_SUPPORT))
  320. return;
  321. if (drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_SINK_OUI, buf, 3) == 3)
  322. DRM_DEBUG_KMS("Sink OUI: %02hx%02hx%02hx\n",
  323. buf[0], buf[1], buf[2]);
  324. if (drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_BRANCH_OUI, buf, 3) == 3)
  325. DRM_DEBUG_KMS("Branch OUI: %02hx%02hx%02hx\n",
  326. buf[0], buf[1], buf[2]);
  327. }
  328. bool radeon_dp_getdpcd(struct radeon_connector *radeon_connector)
  329. {
  330. struct radeon_connector_atom_dig *dig_connector = radeon_connector->con_priv;
  331. u8 msg[DP_DPCD_SIZE];
  332. int ret, i;
  333. for (i = 0; i < 7; i++) {
  334. ret = drm_dp_dpcd_read(&radeon_connector->ddc_bus->aux, DP_DPCD_REV, msg,
  335. DP_DPCD_SIZE);
  336. if (ret == DP_DPCD_SIZE) {
  337. memcpy(dig_connector->dpcd, msg, DP_DPCD_SIZE);
  338. DRM_DEBUG_KMS("DPCD: %*ph\n", (int)sizeof(dig_connector->dpcd),
  339. dig_connector->dpcd);
  340. radeon_dp_probe_oui(radeon_connector);
  341. return true;
  342. }
  343. }
  344. dig_connector->dpcd[0] = 0;
  345. return false;
  346. }
  347. int radeon_dp_get_panel_mode(struct drm_encoder *encoder,
  348. struct drm_connector *connector)
  349. {
  350. struct drm_device *dev = encoder->dev;
  351. struct radeon_device *rdev = dev->dev_private;
  352. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  353. struct radeon_connector_atom_dig *dig_connector;
  354. int panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
  355. u16 dp_bridge = radeon_connector_encoder_get_dp_bridge_encoder_id(connector);
  356. u8 tmp;
  357. if (!ASIC_IS_DCE4(rdev))
  358. return panel_mode;
  359. if (!radeon_connector->con_priv)
  360. return panel_mode;
  361. dig_connector = radeon_connector->con_priv;
  362. if (dp_bridge != ENCODER_OBJECT_ID_NONE) {
  363. /* DP bridge chips */
  364. if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux,
  365. DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
  366. if (tmp & 1)
  367. panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
  368. else if ((dp_bridge == ENCODER_OBJECT_ID_NUTMEG) ||
  369. (dp_bridge == ENCODER_OBJECT_ID_TRAVIS))
  370. panel_mode = DP_PANEL_MODE_INTERNAL_DP1_MODE;
  371. else
  372. panel_mode = DP_PANEL_MODE_EXTERNAL_DP_MODE;
  373. }
  374. } else if (connector->connector_type == DRM_MODE_CONNECTOR_eDP) {
  375. /* eDP */
  376. if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux,
  377. DP_EDP_CONFIGURATION_CAP, &tmp) == 1) {
  378. if (tmp & 1)
  379. panel_mode = DP_PANEL_MODE_INTERNAL_DP2_MODE;
  380. }
  381. }
  382. return panel_mode;
  383. }
  384. void radeon_dp_set_link_config(struct drm_connector *connector,
  385. const struct drm_display_mode *mode)
  386. {
  387. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  388. struct radeon_connector_atom_dig *dig_connector;
  389. int ret;
  390. if (!radeon_connector->con_priv)
  391. return;
  392. dig_connector = radeon_connector->con_priv;
  393. if ((dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT) ||
  394. (dig_connector->dp_sink_type == CONNECTOR_OBJECT_ID_eDP)) {
  395. ret = radeon_dp_get_dp_link_config(connector, dig_connector->dpcd,
  396. mode->clock,
  397. &dig_connector->dp_lane_count,
  398. &dig_connector->dp_clock);
  399. if (ret) {
  400. dig_connector->dp_clock = 0;
  401. dig_connector->dp_lane_count = 0;
  402. }
  403. }
  404. }
  405. int radeon_dp_mode_valid_helper(struct drm_connector *connector,
  406. struct drm_display_mode *mode)
  407. {
  408. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  409. struct radeon_connector_atom_dig *dig_connector;
  410. unsigned dp_clock, dp_lanes;
  411. int ret;
  412. if ((mode->clock > 340000) &&
  413. (!radeon_connector_is_dp12_capable(connector)))
  414. return MODE_CLOCK_HIGH;
  415. if (!radeon_connector->con_priv)
  416. return MODE_CLOCK_HIGH;
  417. dig_connector = radeon_connector->con_priv;
  418. ret = radeon_dp_get_dp_link_config(connector, dig_connector->dpcd,
  419. mode->clock,
  420. &dp_lanes,
  421. &dp_clock);
  422. if (ret)
  423. return MODE_CLOCK_HIGH;
  424. if ((dp_clock == 540000) &&
  425. (!radeon_connector_is_dp12_capable(connector)))
  426. return MODE_CLOCK_HIGH;
  427. return MODE_OK;
  428. }
  429. bool radeon_dp_needs_link_train(struct radeon_connector *radeon_connector)
  430. {
  431. u8 link_status[DP_LINK_STATUS_SIZE];
  432. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  433. if (drm_dp_dpcd_read_link_status(&radeon_connector->ddc_bus->aux, link_status)
  434. <= 0)
  435. return false;
  436. if (drm_dp_channel_eq_ok(link_status, dig->dp_lane_count))
  437. return false;
  438. return true;
  439. }
  440. void radeon_dp_set_rx_power_state(struct drm_connector *connector,
  441. u8 power_state)
  442. {
  443. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  444. struct radeon_connector_atom_dig *dig_connector;
  445. if (!radeon_connector->con_priv)
  446. return;
  447. dig_connector = radeon_connector->con_priv;
  448. /* power up/down the sink */
  449. if (dig_connector->dpcd[0] >= 0x11) {
  450. drm_dp_dpcd_writeb(&radeon_connector->ddc_bus->aux,
  451. DP_SET_POWER, power_state);
  452. usleep_range(1000, 2000);
  453. }
  454. }
  455. struct radeon_dp_link_train_info {
  456. struct radeon_device *rdev;
  457. struct drm_encoder *encoder;
  458. struct drm_connector *connector;
  459. int enc_id;
  460. int dp_clock;
  461. int dp_lane_count;
  462. bool tp3_supported;
  463. u8 dpcd[DP_RECEIVER_CAP_SIZE];
  464. u8 train_set[4];
  465. u8 link_status[DP_LINK_STATUS_SIZE];
  466. u8 tries;
  467. bool use_dpencoder;
  468. struct drm_dp_aux *aux;
  469. };
  470. static void radeon_dp_update_vs_emph(struct radeon_dp_link_train_info *dp_info)
  471. {
  472. /* set the initial vs/emph on the source */
  473. atombios_dig_transmitter_setup(dp_info->encoder,
  474. ATOM_TRANSMITTER_ACTION_SETUP_VSEMPH,
  475. 0, dp_info->train_set[0]); /* sets all lanes at once */
  476. /* set the vs/emph on the sink */
  477. drm_dp_dpcd_write(dp_info->aux, DP_TRAINING_LANE0_SET,
  478. dp_info->train_set, dp_info->dp_lane_count);
  479. }
  480. static void radeon_dp_set_tp(struct radeon_dp_link_train_info *dp_info, int tp)
  481. {
  482. int rtp = 0;
  483. /* set training pattern on the source */
  484. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder) {
  485. switch (tp) {
  486. case DP_TRAINING_PATTERN_1:
  487. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN1;
  488. break;
  489. case DP_TRAINING_PATTERN_2:
  490. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN2;
  491. break;
  492. case DP_TRAINING_PATTERN_3:
  493. rtp = ATOM_ENCODER_CMD_DP_LINK_TRAINING_PATTERN3;
  494. break;
  495. }
  496. atombios_dig_encoder_setup(dp_info->encoder, rtp, 0);
  497. } else {
  498. switch (tp) {
  499. case DP_TRAINING_PATTERN_1:
  500. rtp = 0;
  501. break;
  502. case DP_TRAINING_PATTERN_2:
  503. rtp = 1;
  504. break;
  505. }
  506. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_PATTERN_SEL,
  507. dp_info->dp_clock, dp_info->enc_id, rtp);
  508. }
  509. /* enable training pattern on the sink */
  510. drm_dp_dpcd_writeb(dp_info->aux, DP_TRAINING_PATTERN_SET, tp);
  511. }
  512. static int radeon_dp_link_train_init(struct radeon_dp_link_train_info *dp_info)
  513. {
  514. struct radeon_encoder *radeon_encoder = to_radeon_encoder(dp_info->encoder);
  515. struct radeon_encoder_atom_dig *dig = radeon_encoder->enc_priv;
  516. u8 tmp;
  517. /* power up the sink */
  518. radeon_dp_set_rx_power_state(dp_info->connector, DP_SET_POWER_D0);
  519. /* possibly enable downspread on the sink */
  520. if (dp_info->dpcd[3] & 0x1)
  521. drm_dp_dpcd_writeb(dp_info->aux,
  522. DP_DOWNSPREAD_CTRL, DP_SPREAD_AMP_0_5);
  523. else
  524. drm_dp_dpcd_writeb(dp_info->aux,
  525. DP_DOWNSPREAD_CTRL, 0);
  526. if (dig->panel_mode == DP_PANEL_MODE_INTERNAL_DP2_MODE)
  527. drm_dp_dpcd_writeb(dp_info->aux, DP_EDP_CONFIGURATION_SET, 1);
  528. /* set the lane count on the sink */
  529. tmp = dp_info->dp_lane_count;
  530. if (drm_dp_enhanced_frame_cap(dp_info->dpcd))
  531. tmp |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  532. drm_dp_dpcd_writeb(dp_info->aux, DP_LANE_COUNT_SET, tmp);
  533. /* set the link rate on the sink */
  534. tmp = drm_dp_link_rate_to_bw_code(dp_info->dp_clock);
  535. drm_dp_dpcd_writeb(dp_info->aux, DP_LINK_BW_SET, tmp);
  536. /* start training on the source */
  537. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
  538. atombios_dig_encoder_setup(dp_info->encoder,
  539. ATOM_ENCODER_CMD_DP_LINK_TRAINING_START, 0);
  540. else
  541. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_START,
  542. dp_info->dp_clock, dp_info->enc_id, 0);
  543. /* disable the training pattern on the sink */
  544. drm_dp_dpcd_writeb(dp_info->aux,
  545. DP_TRAINING_PATTERN_SET,
  546. DP_TRAINING_PATTERN_DISABLE);
  547. return 0;
  548. }
  549. static int radeon_dp_link_train_finish(struct radeon_dp_link_train_info *dp_info)
  550. {
  551. udelay(400);
  552. /* disable the training pattern on the sink */
  553. drm_dp_dpcd_writeb(dp_info->aux,
  554. DP_TRAINING_PATTERN_SET,
  555. DP_TRAINING_PATTERN_DISABLE);
  556. /* disable the training pattern on the source */
  557. if (ASIC_IS_DCE4(dp_info->rdev) || !dp_info->use_dpencoder)
  558. atombios_dig_encoder_setup(dp_info->encoder,
  559. ATOM_ENCODER_CMD_DP_LINK_TRAINING_COMPLETE, 0);
  560. else
  561. radeon_dp_encoder_service(dp_info->rdev, ATOM_DP_ACTION_TRAINING_COMPLETE,
  562. dp_info->dp_clock, dp_info->enc_id, 0);
  563. return 0;
  564. }
  565. static int radeon_dp_link_train_cr(struct radeon_dp_link_train_info *dp_info)
  566. {
  567. bool clock_recovery;
  568. u8 voltage;
  569. int i;
  570. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_1);
  571. memset(dp_info->train_set, 0, 4);
  572. radeon_dp_update_vs_emph(dp_info);
  573. udelay(400);
  574. /* clock recovery loop */
  575. clock_recovery = false;
  576. dp_info->tries = 0;
  577. voltage = 0xff;
  578. while (1) {
  579. drm_dp_link_train_clock_recovery_delay(dp_info->dpcd);
  580. if (drm_dp_dpcd_read_link_status(dp_info->aux,
  581. dp_info->link_status) <= 0) {
  582. DRM_ERROR("displayport link status failed\n");
  583. break;
  584. }
  585. if (drm_dp_clock_recovery_ok(dp_info->link_status, dp_info->dp_lane_count)) {
  586. clock_recovery = true;
  587. break;
  588. }
  589. for (i = 0; i < dp_info->dp_lane_count; i++) {
  590. if ((dp_info->train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  591. break;
  592. }
  593. if (i == dp_info->dp_lane_count) {
  594. DRM_ERROR("clock recovery reached max voltage\n");
  595. break;
  596. }
  597. if ((dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  598. ++dp_info->tries;
  599. if (dp_info->tries == 5) {
  600. DRM_ERROR("clock recovery tried 5 times\n");
  601. break;
  602. }
  603. } else
  604. dp_info->tries = 0;
  605. voltage = dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  606. /* Compute new train_set as requested by sink */
  607. dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
  608. radeon_dp_update_vs_emph(dp_info);
  609. }
  610. if (!clock_recovery) {
  611. DRM_ERROR("clock recovery failed\n");
  612. return -1;
  613. } else {
  614. DRM_DEBUG_KMS("clock recovery at voltage %d pre-emphasis %d\n",
  615. dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  616. (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
  617. DP_TRAIN_PRE_EMPHASIS_SHIFT);
  618. return 0;
  619. }
  620. }
  621. static int radeon_dp_link_train_ce(struct radeon_dp_link_train_info *dp_info)
  622. {
  623. bool channel_eq;
  624. if (dp_info->tp3_supported)
  625. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_3);
  626. else
  627. radeon_dp_set_tp(dp_info, DP_TRAINING_PATTERN_2);
  628. /* channel equalization loop */
  629. dp_info->tries = 0;
  630. channel_eq = false;
  631. while (1) {
  632. drm_dp_link_train_channel_eq_delay(dp_info->dpcd);
  633. if (drm_dp_dpcd_read_link_status(dp_info->aux,
  634. dp_info->link_status) <= 0) {
  635. DRM_ERROR("displayport link status failed\n");
  636. break;
  637. }
  638. if (drm_dp_channel_eq_ok(dp_info->link_status, dp_info->dp_lane_count)) {
  639. channel_eq = true;
  640. break;
  641. }
  642. /* Try 5 times */
  643. if (dp_info->tries > 5) {
  644. DRM_ERROR("channel eq failed: 5 tries\n");
  645. break;
  646. }
  647. /* Compute new train_set as requested by sink */
  648. dp_get_adjust_train(dp_info->link_status, dp_info->dp_lane_count, dp_info->train_set);
  649. radeon_dp_update_vs_emph(dp_info);
  650. dp_info->tries++;
  651. }
  652. if (!channel_eq) {
  653. DRM_ERROR("channel eq failed\n");
  654. return -1;
  655. } else {
  656. DRM_DEBUG_KMS("channel eq at voltage %d pre-emphasis %d\n",
  657. dp_info->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK,
  658. (dp_info->train_set[0] & DP_TRAIN_PRE_EMPHASIS_MASK)
  659. >> DP_TRAIN_PRE_EMPHASIS_SHIFT);
  660. return 0;
  661. }
  662. }
  663. void radeon_dp_link_train(struct drm_encoder *encoder,
  664. struct drm_connector *connector)
  665. {
  666. struct drm_device *dev = encoder->dev;
  667. struct radeon_device *rdev = dev->dev_private;
  668. struct radeon_encoder *radeon_encoder = to_radeon_encoder(encoder);
  669. struct radeon_encoder_atom_dig *dig;
  670. struct radeon_connector *radeon_connector;
  671. struct radeon_connector_atom_dig *dig_connector;
  672. struct radeon_dp_link_train_info dp_info;
  673. int index;
  674. u8 tmp, frev, crev;
  675. if (!radeon_encoder->enc_priv)
  676. return;
  677. dig = radeon_encoder->enc_priv;
  678. radeon_connector = to_radeon_connector(connector);
  679. if (!radeon_connector->con_priv)
  680. return;
  681. dig_connector = radeon_connector->con_priv;
  682. if ((dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_DISPLAYPORT) &&
  683. (dig_connector->dp_sink_type != CONNECTOR_OBJECT_ID_eDP))
  684. return;
  685. /* DPEncoderService newer than 1.1 can't program properly the
  686. * training pattern. When facing such version use the
  687. * DIGXEncoderControl (X== 1 | 2)
  688. */
  689. dp_info.use_dpencoder = true;
  690. index = GetIndexIntoMasterTable(COMMAND, DPEncoderService);
  691. if (atom_parse_cmd_header(rdev->mode_info.atom_context, index, &frev, &crev)) {
  692. if (crev > 1) {
  693. dp_info.use_dpencoder = false;
  694. }
  695. }
  696. dp_info.enc_id = 0;
  697. if (dig->dig_encoder)
  698. dp_info.enc_id |= ATOM_DP_CONFIG_DIG2_ENCODER;
  699. else
  700. dp_info.enc_id |= ATOM_DP_CONFIG_DIG1_ENCODER;
  701. if (dig->linkb)
  702. dp_info.enc_id |= ATOM_DP_CONFIG_LINK_B;
  703. else
  704. dp_info.enc_id |= ATOM_DP_CONFIG_LINK_A;
  705. if (drm_dp_dpcd_readb(&radeon_connector->ddc_bus->aux, DP_MAX_LANE_COUNT, &tmp)
  706. == 1) {
  707. if (ASIC_IS_DCE5(rdev) && (tmp & DP_TPS3_SUPPORTED))
  708. dp_info.tp3_supported = true;
  709. else
  710. dp_info.tp3_supported = false;
  711. } else {
  712. dp_info.tp3_supported = false;
  713. }
  714. memcpy(dp_info.dpcd, dig_connector->dpcd, DP_RECEIVER_CAP_SIZE);
  715. dp_info.rdev = rdev;
  716. dp_info.encoder = encoder;
  717. dp_info.connector = connector;
  718. dp_info.dp_lane_count = dig_connector->dp_lane_count;
  719. dp_info.dp_clock = dig_connector->dp_clock;
  720. dp_info.aux = &radeon_connector->ddc_bus->aux;
  721. if (radeon_dp_link_train_init(&dp_info))
  722. goto done;
  723. if (radeon_dp_link_train_cr(&dp_info))
  724. goto done;
  725. if (radeon_dp_link_train_ce(&dp_info))
  726. goto done;
  727. done:
  728. if (radeon_dp_link_train_finish(&dp_info))
  729. return;
  730. }