intel_dp.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Keith Packard <keithp@keithp.com>
  25. *
  26. */
  27. #include <linux/i2c.h>
  28. #include "drmP.h"
  29. #include "drm.h"
  30. #include "drm_crtc.h"
  31. #include "drm_crtc_helper.h"
  32. #include "intel_drv.h"
  33. #include "i915_drm.h"
  34. #include "i915_drv.h"
  35. #include "intel_dp.h"
  36. #define DP_LINK_STATUS_SIZE 6
  37. #define DP_LINK_CHECK_TIMEOUT (10 * 1000)
  38. #define DP_LINK_CONFIGURATION_SIZE 9
  39. struct intel_dp_priv {
  40. uint32_t output_reg;
  41. uint32_t DP;
  42. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE];
  43. uint32_t save_DP;
  44. uint8_t save_link_configuration[DP_LINK_CONFIGURATION_SIZE];
  45. bool has_audio;
  46. int dpms_mode;
  47. uint8_t link_bw;
  48. uint8_t lane_count;
  49. uint8_t dpcd[4];
  50. struct intel_output *intel_output;
  51. struct i2c_adapter adapter;
  52. struct i2c_algo_dp_aux_data algo;
  53. };
  54. static void
  55. intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
  56. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE]);
  57. static void
  58. intel_dp_link_down(struct intel_output *intel_output, uint32_t DP);
  59. static int
  60. intel_dp_max_lane_count(struct intel_output *intel_output)
  61. {
  62. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  63. int max_lane_count = 4;
  64. if (dp_priv->dpcd[0] >= 0x11) {
  65. max_lane_count = dp_priv->dpcd[2] & 0x1f;
  66. switch (max_lane_count) {
  67. case 1: case 2: case 4:
  68. break;
  69. default:
  70. max_lane_count = 4;
  71. }
  72. }
  73. return max_lane_count;
  74. }
  75. static int
  76. intel_dp_max_link_bw(struct intel_output *intel_output)
  77. {
  78. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  79. int max_link_bw = dp_priv->dpcd[1];
  80. switch (max_link_bw) {
  81. case DP_LINK_BW_1_62:
  82. case DP_LINK_BW_2_7:
  83. break;
  84. default:
  85. max_link_bw = DP_LINK_BW_1_62;
  86. break;
  87. }
  88. return max_link_bw;
  89. }
  90. static int
  91. intel_dp_link_clock(uint8_t link_bw)
  92. {
  93. if (link_bw == DP_LINK_BW_2_7)
  94. return 270000;
  95. else
  96. return 162000;
  97. }
  98. /* I think this is a fiction */
  99. static int
  100. intel_dp_link_required(int pixel_clock)
  101. {
  102. return pixel_clock * 3;
  103. }
  104. static int
  105. intel_dp_mode_valid(struct drm_connector *connector,
  106. struct drm_display_mode *mode)
  107. {
  108. struct intel_output *intel_output = to_intel_output(connector);
  109. int max_link_clock = intel_dp_link_clock(intel_dp_max_link_bw(intel_output));
  110. int max_lanes = intel_dp_max_lane_count(intel_output);
  111. if (intel_dp_link_required(mode->clock) > max_link_clock * max_lanes)
  112. return MODE_CLOCK_HIGH;
  113. if (mode->clock < 10000)
  114. return MODE_CLOCK_LOW;
  115. return MODE_OK;
  116. }
  117. static uint32_t
  118. pack_aux(uint8_t *src, int src_bytes)
  119. {
  120. int i;
  121. uint32_t v = 0;
  122. if (src_bytes > 4)
  123. src_bytes = 4;
  124. for (i = 0; i < src_bytes; i++)
  125. v |= ((uint32_t) src[i]) << ((3-i) * 8);
  126. return v;
  127. }
  128. static void
  129. unpack_aux(uint32_t src, uint8_t *dst, int dst_bytes)
  130. {
  131. int i;
  132. if (dst_bytes > 4)
  133. dst_bytes = 4;
  134. for (i = 0; i < dst_bytes; i++)
  135. dst[i] = src >> ((3-i) * 8);
  136. }
  137. /* hrawclock is 1/4 the FSB frequency */
  138. static int
  139. intel_hrawclk(struct drm_device *dev)
  140. {
  141. struct drm_i915_private *dev_priv = dev->dev_private;
  142. uint32_t clkcfg;
  143. clkcfg = I915_READ(CLKCFG);
  144. switch (clkcfg & CLKCFG_FSB_MASK) {
  145. case CLKCFG_FSB_400:
  146. return 100;
  147. case CLKCFG_FSB_533:
  148. return 133;
  149. case CLKCFG_FSB_667:
  150. return 166;
  151. case CLKCFG_FSB_800:
  152. return 200;
  153. case CLKCFG_FSB_1067:
  154. return 266;
  155. case CLKCFG_FSB_1333:
  156. return 333;
  157. /* these two are just a guess; one of them might be right */
  158. case CLKCFG_FSB_1600:
  159. case CLKCFG_FSB_1600_ALT:
  160. return 400;
  161. default:
  162. return 133;
  163. }
  164. }
  165. static int
  166. intel_dp_aux_ch(struct intel_output *intel_output,
  167. uint8_t *send, int send_bytes,
  168. uint8_t *recv, int recv_size)
  169. {
  170. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  171. uint32_t output_reg = dp_priv->output_reg;
  172. struct drm_device *dev = intel_output->base.dev;
  173. struct drm_i915_private *dev_priv = dev->dev_private;
  174. uint32_t ch_ctl = output_reg + 0x10;
  175. uint32_t ch_data = ch_ctl + 4;
  176. int i;
  177. int recv_bytes;
  178. uint32_t ctl;
  179. uint32_t status;
  180. uint32_t aux_clock_divider;
  181. int try;
  182. /* The clock divider is based off the hrawclk,
  183. * and would like to run at 2MHz. So, take the
  184. * hrawclk value and divide by 2 and use that
  185. */
  186. aux_clock_divider = intel_hrawclk(dev) / 2;
  187. /* Must try at least 3 times according to DP spec */
  188. for (try = 0; try < 5; try++) {
  189. /* Load the send data into the aux channel data registers */
  190. for (i = 0; i < send_bytes; i += 4) {
  191. uint32_t d = pack_aux(send + i, send_bytes - i);;
  192. I915_WRITE(ch_data + i, d);
  193. }
  194. ctl = (DP_AUX_CH_CTL_SEND_BUSY |
  195. DP_AUX_CH_CTL_TIME_OUT_400us |
  196. (send_bytes << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
  197. (5 << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
  198. (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT) |
  199. DP_AUX_CH_CTL_DONE |
  200. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  201. DP_AUX_CH_CTL_RECEIVE_ERROR);
  202. /* Send the command and wait for it to complete */
  203. I915_WRITE(ch_ctl, ctl);
  204. (void) I915_READ(ch_ctl);
  205. for (;;) {
  206. udelay(100);
  207. status = I915_READ(ch_ctl);
  208. if ((status & DP_AUX_CH_CTL_SEND_BUSY) == 0)
  209. break;
  210. }
  211. /* Clear done status and any errors */
  212. I915_WRITE(ch_ctl, (ctl |
  213. DP_AUX_CH_CTL_DONE |
  214. DP_AUX_CH_CTL_TIME_OUT_ERROR |
  215. DP_AUX_CH_CTL_RECEIVE_ERROR));
  216. (void) I915_READ(ch_ctl);
  217. if ((status & DP_AUX_CH_CTL_TIME_OUT_ERROR) == 0)
  218. break;
  219. }
  220. if ((status & DP_AUX_CH_CTL_DONE) == 0) {
  221. DRM_ERROR("dp_aux_ch not done status 0x%08x\n", status);
  222. return -EBUSY;
  223. }
  224. /* Check for timeout or receive error.
  225. * Timeouts occur when the sink is not connected
  226. */
  227. if (status & DP_AUX_CH_CTL_RECEIVE_ERROR) {
  228. DRM_ERROR("dp_aux_ch receive error status 0x%08x\n", status);
  229. return -EIO;
  230. }
  231. /* Timeouts occur when the device isn't connected, so they're
  232. * "normal" -- don't fill the kernel log with these */
  233. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  234. DRM_DEBUG("dp_aux_ch timeout status 0x%08x\n", status);
  235. return -ETIMEDOUT;
  236. }
  237. /* Unload any bytes sent back from the other side */
  238. recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  239. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  240. if (recv_bytes > recv_size)
  241. recv_bytes = recv_size;
  242. for (i = 0; i < recv_bytes; i += 4) {
  243. uint32_t d = I915_READ(ch_data + i);
  244. unpack_aux(d, recv + i, recv_bytes - i);
  245. }
  246. return recv_bytes;
  247. }
  248. /* Write data to the aux channel in native mode */
  249. static int
  250. intel_dp_aux_native_write(struct intel_output *intel_output,
  251. uint16_t address, uint8_t *send, int send_bytes)
  252. {
  253. int ret;
  254. uint8_t msg[20];
  255. int msg_bytes;
  256. uint8_t ack;
  257. if (send_bytes > 16)
  258. return -1;
  259. msg[0] = AUX_NATIVE_WRITE << 4;
  260. msg[1] = address >> 8;
  261. msg[2] = address;
  262. msg[3] = send_bytes - 1;
  263. memcpy(&msg[4], send, send_bytes);
  264. msg_bytes = send_bytes + 4;
  265. for (;;) {
  266. ret = intel_dp_aux_ch(intel_output, msg, msg_bytes, &ack, 1);
  267. if (ret < 0)
  268. return ret;
  269. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
  270. break;
  271. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  272. udelay(100);
  273. else
  274. return -EIO;
  275. }
  276. return send_bytes;
  277. }
  278. /* Write a single byte to the aux channel in native mode */
  279. static int
  280. intel_dp_aux_native_write_1(struct intel_output *intel_output,
  281. uint16_t address, uint8_t byte)
  282. {
  283. return intel_dp_aux_native_write(intel_output, address, &byte, 1);
  284. }
  285. /* read bytes from a native aux channel */
  286. static int
  287. intel_dp_aux_native_read(struct intel_output *intel_output,
  288. uint16_t address, uint8_t *recv, int recv_bytes)
  289. {
  290. uint8_t msg[4];
  291. int msg_bytes;
  292. uint8_t reply[20];
  293. int reply_bytes;
  294. uint8_t ack;
  295. int ret;
  296. msg[0] = AUX_NATIVE_READ << 4;
  297. msg[1] = address >> 8;
  298. msg[2] = address & 0xff;
  299. msg[3] = recv_bytes - 1;
  300. msg_bytes = 4;
  301. reply_bytes = recv_bytes + 1;
  302. for (;;) {
  303. ret = intel_dp_aux_ch(intel_output, msg, msg_bytes,
  304. reply, reply_bytes);
  305. if (ret == 0)
  306. return -EPROTO;
  307. if (ret < 0)
  308. return ret;
  309. ack = reply[0];
  310. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
  311. memcpy(recv, reply + 1, ret - 1);
  312. return ret - 1;
  313. }
  314. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  315. udelay(100);
  316. else
  317. return -EIO;
  318. }
  319. }
  320. static int
  321. intel_dp_i2c_aux_ch(struct i2c_adapter *adapter,
  322. uint8_t *send, int send_bytes,
  323. uint8_t *recv, int recv_bytes)
  324. {
  325. struct intel_dp_priv *dp_priv = container_of(adapter,
  326. struct intel_dp_priv,
  327. adapter);
  328. struct intel_output *intel_output = dp_priv->intel_output;
  329. return intel_dp_aux_ch(intel_output,
  330. send, send_bytes, recv, recv_bytes);
  331. }
  332. static int
  333. intel_dp_i2c_init(struct intel_output *intel_output, const char *name)
  334. {
  335. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  336. DRM_ERROR("i2c_init %s\n", name);
  337. dp_priv->algo.running = false;
  338. dp_priv->algo.address = 0;
  339. dp_priv->algo.aux_ch = intel_dp_i2c_aux_ch;
  340. memset(&dp_priv->adapter, '\0', sizeof (dp_priv->adapter));
  341. dp_priv->adapter.owner = THIS_MODULE;
  342. dp_priv->adapter.class = I2C_CLASS_DDC;
  343. strncpy (dp_priv->adapter.name, name, sizeof dp_priv->adapter.name - 1);
  344. dp_priv->adapter.name[sizeof dp_priv->adapter.name - 1] = '\0';
  345. dp_priv->adapter.algo_data = &dp_priv->algo;
  346. dp_priv->adapter.dev.parent = &intel_output->base.kdev;
  347. return i2c_dp_aux_add_bus(&dp_priv->adapter);
  348. }
  349. static bool
  350. intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  351. struct drm_display_mode *adjusted_mode)
  352. {
  353. struct intel_output *intel_output = enc_to_intel_output(encoder);
  354. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  355. int lane_count, clock;
  356. int max_lane_count = intel_dp_max_lane_count(intel_output);
  357. int max_clock = intel_dp_max_link_bw(intel_output) == DP_LINK_BW_2_7 ? 1 : 0;
  358. static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
  359. for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
  360. for (clock = 0; clock <= max_clock; clock++) {
  361. int link_avail = intel_dp_link_clock(bws[clock]) * lane_count;
  362. if (intel_dp_link_required(mode->clock) <= link_avail) {
  363. dp_priv->link_bw = bws[clock];
  364. dp_priv->lane_count = lane_count;
  365. adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
  366. DRM_DEBUG("Display port link bw %02x lane count %d clock %d\n",
  367. dp_priv->link_bw, dp_priv->lane_count,
  368. adjusted_mode->clock);
  369. return true;
  370. }
  371. }
  372. }
  373. return false;
  374. }
  375. struct intel_dp_m_n {
  376. uint32_t tu;
  377. uint32_t gmch_m;
  378. uint32_t gmch_n;
  379. uint32_t link_m;
  380. uint32_t link_n;
  381. };
  382. static void
  383. intel_reduce_ratio(uint32_t *num, uint32_t *den)
  384. {
  385. while (*num > 0xffffff || *den > 0xffffff) {
  386. *num >>= 1;
  387. *den >>= 1;
  388. }
  389. }
  390. static void
  391. intel_dp_compute_m_n(int bytes_per_pixel,
  392. int nlanes,
  393. int pixel_clock,
  394. int link_clock,
  395. struct intel_dp_m_n *m_n)
  396. {
  397. m_n->tu = 64;
  398. m_n->gmch_m = pixel_clock * bytes_per_pixel;
  399. m_n->gmch_n = link_clock * nlanes;
  400. intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  401. m_n->link_m = pixel_clock;
  402. m_n->link_n = link_clock;
  403. intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  404. }
  405. void
  406. intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
  407. struct drm_display_mode *adjusted_mode)
  408. {
  409. struct drm_device *dev = crtc->dev;
  410. struct drm_mode_config *mode_config = &dev->mode_config;
  411. struct drm_connector *connector;
  412. struct drm_i915_private *dev_priv = dev->dev_private;
  413. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  414. int lane_count = 4;
  415. struct intel_dp_m_n m_n;
  416. /*
  417. * Find the lane count in the intel_output private
  418. */
  419. list_for_each_entry(connector, &mode_config->connector_list, head) {
  420. struct intel_output *intel_output = to_intel_output(connector);
  421. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  422. if (!connector->encoder || connector->encoder->crtc != crtc)
  423. continue;
  424. if (intel_output->type == INTEL_OUTPUT_DISPLAYPORT) {
  425. lane_count = dp_priv->lane_count;
  426. break;
  427. }
  428. }
  429. /*
  430. * Compute the GMCH and Link ratios. The '3' here is
  431. * the number of bytes_per_pixel post-LUT, which we always
  432. * set up for 8-bits of R/G/B, or 3 bytes total.
  433. */
  434. intel_dp_compute_m_n(3, lane_count,
  435. mode->clock, adjusted_mode->clock, &m_n);
  436. if (intel_crtc->pipe == 0) {
  437. I915_WRITE(PIPEA_GMCH_DATA_M,
  438. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  439. m_n.gmch_m);
  440. I915_WRITE(PIPEA_GMCH_DATA_N,
  441. m_n.gmch_n);
  442. I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
  443. I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
  444. } else {
  445. I915_WRITE(PIPEB_GMCH_DATA_M,
  446. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  447. m_n.gmch_m);
  448. I915_WRITE(PIPEB_GMCH_DATA_N,
  449. m_n.gmch_n);
  450. I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
  451. I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
  452. }
  453. }
  454. static void
  455. intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  456. struct drm_display_mode *adjusted_mode)
  457. {
  458. struct intel_output *intel_output = enc_to_intel_output(encoder);
  459. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  460. struct drm_crtc *crtc = intel_output->enc.crtc;
  461. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  462. dp_priv->DP = (DP_LINK_TRAIN_OFF |
  463. DP_VOLTAGE_0_4 |
  464. DP_PRE_EMPHASIS_0 |
  465. DP_SYNC_VS_HIGH |
  466. DP_SYNC_HS_HIGH);
  467. switch (dp_priv->lane_count) {
  468. case 1:
  469. dp_priv->DP |= DP_PORT_WIDTH_1;
  470. break;
  471. case 2:
  472. dp_priv->DP |= DP_PORT_WIDTH_2;
  473. break;
  474. case 4:
  475. dp_priv->DP |= DP_PORT_WIDTH_4;
  476. break;
  477. }
  478. if (dp_priv->has_audio)
  479. dp_priv->DP |= DP_AUDIO_OUTPUT_ENABLE;
  480. memset(dp_priv->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  481. dp_priv->link_configuration[0] = dp_priv->link_bw;
  482. dp_priv->link_configuration[1] = dp_priv->lane_count;
  483. /*
  484. * Check for DPCD version > 1.1,
  485. * enable enahanced frame stuff in that case
  486. */
  487. if (dp_priv->dpcd[0] >= 0x11) {
  488. dp_priv->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  489. dp_priv->DP |= DP_ENHANCED_FRAMING;
  490. }
  491. if (intel_crtc->pipe == 1)
  492. dp_priv->DP |= DP_PIPEB_SELECT;
  493. }
  494. static void
  495. intel_dp_dpms(struct drm_encoder *encoder, int mode)
  496. {
  497. struct intel_output *intel_output = enc_to_intel_output(encoder);
  498. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  499. struct drm_device *dev = intel_output->base.dev;
  500. struct drm_i915_private *dev_priv = dev->dev_private;
  501. uint32_t dp_reg = I915_READ(dp_priv->output_reg);
  502. if (mode != DRM_MODE_DPMS_ON) {
  503. if (dp_reg & DP_PORT_EN)
  504. intel_dp_link_down(intel_output, dp_priv->DP);
  505. } else {
  506. if (!(dp_reg & DP_PORT_EN))
  507. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  508. }
  509. dp_priv->dpms_mode = mode;
  510. }
  511. /*
  512. * Fetch AUX CH registers 0x202 - 0x207 which contain
  513. * link status information
  514. */
  515. static bool
  516. intel_dp_get_link_status(struct intel_output *intel_output,
  517. uint8_t link_status[DP_LINK_STATUS_SIZE])
  518. {
  519. int ret;
  520. ret = intel_dp_aux_native_read(intel_output,
  521. DP_LANE0_1_STATUS,
  522. link_status, DP_LINK_STATUS_SIZE);
  523. if (ret != DP_LINK_STATUS_SIZE)
  524. return false;
  525. return true;
  526. }
  527. static uint8_t
  528. intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  529. int r)
  530. {
  531. return link_status[r - DP_LANE0_1_STATUS];
  532. }
  533. static void
  534. intel_dp_save(struct drm_connector *connector)
  535. {
  536. struct intel_output *intel_output = to_intel_output(connector);
  537. struct drm_device *dev = intel_output->base.dev;
  538. struct drm_i915_private *dev_priv = dev->dev_private;
  539. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  540. dp_priv->save_DP = I915_READ(dp_priv->output_reg);
  541. intel_dp_aux_native_read(intel_output, DP_LINK_BW_SET,
  542. dp_priv->save_link_configuration,
  543. sizeof (dp_priv->save_link_configuration));
  544. }
  545. static uint8_t
  546. intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
  547. int lane)
  548. {
  549. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  550. int s = ((lane & 1) ?
  551. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  552. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  553. uint8_t l = intel_dp_link_status(link_status, i);
  554. return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  555. }
  556. static uint8_t
  557. intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
  558. int lane)
  559. {
  560. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  561. int s = ((lane & 1) ?
  562. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  563. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  564. uint8_t l = intel_dp_link_status(link_status, i);
  565. return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  566. }
  567. #if 0
  568. static char *voltage_names[] = {
  569. "0.4V", "0.6V", "0.8V", "1.2V"
  570. };
  571. static char *pre_emph_names[] = {
  572. "0dB", "3.5dB", "6dB", "9.5dB"
  573. };
  574. static char *link_train_names[] = {
  575. "pattern 1", "pattern 2", "idle", "off"
  576. };
  577. #endif
  578. /*
  579. * These are source-specific values; current Intel hardware supports
  580. * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
  581. */
  582. #define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
  583. static uint8_t
  584. intel_dp_pre_emphasis_max(uint8_t voltage_swing)
  585. {
  586. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  587. case DP_TRAIN_VOLTAGE_SWING_400:
  588. return DP_TRAIN_PRE_EMPHASIS_6;
  589. case DP_TRAIN_VOLTAGE_SWING_600:
  590. return DP_TRAIN_PRE_EMPHASIS_6;
  591. case DP_TRAIN_VOLTAGE_SWING_800:
  592. return DP_TRAIN_PRE_EMPHASIS_3_5;
  593. case DP_TRAIN_VOLTAGE_SWING_1200:
  594. default:
  595. return DP_TRAIN_PRE_EMPHASIS_0;
  596. }
  597. }
  598. static void
  599. intel_get_adjust_train(struct intel_output *intel_output,
  600. uint8_t link_status[DP_LINK_STATUS_SIZE],
  601. int lane_count,
  602. uint8_t train_set[4])
  603. {
  604. uint8_t v = 0;
  605. uint8_t p = 0;
  606. int lane;
  607. for (lane = 0; lane < lane_count; lane++) {
  608. uint8_t this_v = intel_get_adjust_request_voltage(link_status, lane);
  609. uint8_t this_p = intel_get_adjust_request_pre_emphasis(link_status, lane);
  610. if (this_v > v)
  611. v = this_v;
  612. if (this_p > p)
  613. p = this_p;
  614. }
  615. if (v >= I830_DP_VOLTAGE_MAX)
  616. v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
  617. if (p >= intel_dp_pre_emphasis_max(v))
  618. p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  619. for (lane = 0; lane < 4; lane++)
  620. train_set[lane] = v | p;
  621. }
  622. static uint32_t
  623. intel_dp_signal_levels(uint8_t train_set, int lane_count)
  624. {
  625. uint32_t signal_levels = 0;
  626. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  627. case DP_TRAIN_VOLTAGE_SWING_400:
  628. default:
  629. signal_levels |= DP_VOLTAGE_0_4;
  630. break;
  631. case DP_TRAIN_VOLTAGE_SWING_600:
  632. signal_levels |= DP_VOLTAGE_0_6;
  633. break;
  634. case DP_TRAIN_VOLTAGE_SWING_800:
  635. signal_levels |= DP_VOLTAGE_0_8;
  636. break;
  637. case DP_TRAIN_VOLTAGE_SWING_1200:
  638. signal_levels |= DP_VOLTAGE_1_2;
  639. break;
  640. }
  641. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  642. case DP_TRAIN_PRE_EMPHASIS_0:
  643. default:
  644. signal_levels |= DP_PRE_EMPHASIS_0;
  645. break;
  646. case DP_TRAIN_PRE_EMPHASIS_3_5:
  647. signal_levels |= DP_PRE_EMPHASIS_3_5;
  648. break;
  649. case DP_TRAIN_PRE_EMPHASIS_6:
  650. signal_levels |= DP_PRE_EMPHASIS_6;
  651. break;
  652. case DP_TRAIN_PRE_EMPHASIS_9_5:
  653. signal_levels |= DP_PRE_EMPHASIS_9_5;
  654. break;
  655. }
  656. return signal_levels;
  657. }
  658. static uint8_t
  659. intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  660. int lane)
  661. {
  662. int i = DP_LANE0_1_STATUS + (lane >> 1);
  663. int s = (lane & 1) * 4;
  664. uint8_t l = intel_dp_link_status(link_status, i);
  665. return (l >> s) & 0xf;
  666. }
  667. /* Check for clock recovery is done on all channels */
  668. static bool
  669. intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  670. {
  671. int lane;
  672. uint8_t lane_status;
  673. for (lane = 0; lane < lane_count; lane++) {
  674. lane_status = intel_get_lane_status(link_status, lane);
  675. if ((lane_status & DP_LANE_CR_DONE) == 0)
  676. return false;
  677. }
  678. return true;
  679. }
  680. /* Check to see if channel eq is done on all channels */
  681. #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
  682. DP_LANE_CHANNEL_EQ_DONE|\
  683. DP_LANE_SYMBOL_LOCKED)
  684. static bool
  685. intel_channel_eq_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  686. {
  687. uint8_t lane_align;
  688. uint8_t lane_status;
  689. int lane;
  690. lane_align = intel_dp_link_status(link_status,
  691. DP_LANE_ALIGN_STATUS_UPDATED);
  692. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  693. return false;
  694. for (lane = 0; lane < lane_count; lane++) {
  695. lane_status = intel_get_lane_status(link_status, lane);
  696. if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
  697. return false;
  698. }
  699. return true;
  700. }
  701. static bool
  702. intel_dp_set_link_train(struct intel_output *intel_output,
  703. uint32_t dp_reg_value,
  704. uint8_t dp_train_pat,
  705. uint8_t train_set[4],
  706. bool first)
  707. {
  708. struct drm_device *dev = intel_output->base.dev;
  709. struct drm_i915_private *dev_priv = dev->dev_private;
  710. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  711. int ret;
  712. I915_WRITE(dp_priv->output_reg, dp_reg_value);
  713. POSTING_READ(dp_priv->output_reg);
  714. if (first)
  715. intel_wait_for_vblank(dev);
  716. intel_dp_aux_native_write_1(intel_output,
  717. DP_TRAINING_PATTERN_SET,
  718. dp_train_pat);
  719. ret = intel_dp_aux_native_write(intel_output,
  720. DP_TRAINING_LANE0_SET, train_set, 4);
  721. if (ret != 4)
  722. return false;
  723. return true;
  724. }
  725. static void
  726. intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
  727. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE])
  728. {
  729. struct drm_device *dev = intel_output->base.dev;
  730. struct drm_i915_private *dev_priv = dev->dev_private;
  731. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  732. uint8_t train_set[4];
  733. uint8_t link_status[DP_LINK_STATUS_SIZE];
  734. int i;
  735. uint8_t voltage;
  736. bool clock_recovery = false;
  737. bool channel_eq = false;
  738. bool first = true;
  739. int tries;
  740. /* Write the link configuration data */
  741. intel_dp_aux_native_write(intel_output, 0x100,
  742. link_configuration, DP_LINK_CONFIGURATION_SIZE);
  743. DP |= DP_PORT_EN;
  744. DP &= ~DP_LINK_TRAIN_MASK;
  745. memset(train_set, 0, 4);
  746. voltage = 0xff;
  747. tries = 0;
  748. clock_recovery = false;
  749. for (;;) {
  750. /* Use train_set[0] to set the voltage and pre emphasis values */
  751. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  752. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  753. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_1,
  754. DP_TRAINING_PATTERN_1, train_set, first))
  755. break;
  756. first = false;
  757. /* Set training pattern 1 */
  758. udelay(100);
  759. if (!intel_dp_get_link_status(intel_output, link_status))
  760. break;
  761. if (intel_clock_recovery_ok(link_status, dp_priv->lane_count)) {
  762. clock_recovery = true;
  763. break;
  764. }
  765. /* Check to see if we've tried the max voltage */
  766. for (i = 0; i < dp_priv->lane_count; i++)
  767. if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  768. break;
  769. if (i == dp_priv->lane_count)
  770. break;
  771. /* Check to see if we've tried the same voltage 5 times */
  772. if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  773. ++tries;
  774. if (tries == 5)
  775. break;
  776. } else
  777. tries = 0;
  778. voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  779. /* Compute new train_set as requested by target */
  780. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  781. }
  782. /* channel equalization */
  783. tries = 0;
  784. channel_eq = false;
  785. for (;;) {
  786. /* Use train_set[0] to set the voltage and pre emphasis values */
  787. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  788. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  789. /* channel eq pattern */
  790. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_2,
  791. DP_TRAINING_PATTERN_2, train_set,
  792. false))
  793. break;
  794. udelay(400);
  795. if (!intel_dp_get_link_status(intel_output, link_status))
  796. break;
  797. if (intel_channel_eq_ok(link_status, dp_priv->lane_count)) {
  798. channel_eq = true;
  799. break;
  800. }
  801. /* Try 5 times */
  802. if (tries > 5)
  803. break;
  804. /* Compute new train_set as requested by target */
  805. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  806. ++tries;
  807. }
  808. I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_OFF);
  809. POSTING_READ(dp_priv->output_reg);
  810. intel_dp_aux_native_write_1(intel_output,
  811. DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
  812. }
  813. static void
  814. intel_dp_link_down(struct intel_output *intel_output, uint32_t DP)
  815. {
  816. struct drm_device *dev = intel_output->base.dev;
  817. struct drm_i915_private *dev_priv = dev->dev_private;
  818. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  819. I915_WRITE(dp_priv->output_reg, DP & ~DP_PORT_EN);
  820. POSTING_READ(dp_priv->output_reg);
  821. }
  822. static void
  823. intel_dp_restore(struct drm_connector *connector)
  824. {
  825. struct intel_output *intel_output = to_intel_output(connector);
  826. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  827. if (dp_priv->save_DP & DP_PORT_EN)
  828. intel_dp_link_train(intel_output, dp_priv->save_DP, dp_priv->save_link_configuration);
  829. else
  830. intel_dp_link_down(intel_output, dp_priv->save_DP);
  831. }
  832. /*
  833. * According to DP spec
  834. * 5.1.2:
  835. * 1. Read DPCD
  836. * 2. Configure link according to Receiver Capabilities
  837. * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
  838. * 4. Check link status on receipt of hot-plug interrupt
  839. */
  840. static void
  841. intel_dp_check_link_status(struct intel_output *intel_output)
  842. {
  843. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  844. uint8_t link_status[DP_LINK_STATUS_SIZE];
  845. if (!intel_output->enc.crtc)
  846. return;
  847. if (!intel_dp_get_link_status(intel_output, link_status)) {
  848. intel_dp_link_down(intel_output, dp_priv->DP);
  849. return;
  850. }
  851. if (!intel_channel_eq_ok(link_status, dp_priv->lane_count))
  852. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  853. }
  854. /**
  855. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
  856. *
  857. * \return true if DP port is connected.
  858. * \return false if DP port is disconnected.
  859. */
  860. static enum drm_connector_status
  861. intel_dp_detect(struct drm_connector *connector)
  862. {
  863. struct intel_output *intel_output = to_intel_output(connector);
  864. struct drm_device *dev = intel_output->base.dev;
  865. struct drm_i915_private *dev_priv = dev->dev_private;
  866. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  867. uint32_t temp, bit;
  868. enum drm_connector_status status;
  869. dp_priv->has_audio = false;
  870. temp = I915_READ(PORT_HOTPLUG_EN);
  871. I915_WRITE(PORT_HOTPLUG_EN,
  872. temp |
  873. DPB_HOTPLUG_INT_EN |
  874. DPC_HOTPLUG_INT_EN |
  875. DPD_HOTPLUG_INT_EN);
  876. POSTING_READ(PORT_HOTPLUG_EN);
  877. switch (dp_priv->output_reg) {
  878. case DP_B:
  879. bit = DPB_HOTPLUG_INT_STATUS;
  880. break;
  881. case DP_C:
  882. bit = DPC_HOTPLUG_INT_STATUS;
  883. break;
  884. case DP_D:
  885. bit = DPD_HOTPLUG_INT_STATUS;
  886. break;
  887. default:
  888. return connector_status_unknown;
  889. }
  890. temp = I915_READ(PORT_HOTPLUG_STAT);
  891. if ((temp & bit) == 0)
  892. return connector_status_disconnected;
  893. status = connector_status_disconnected;
  894. if (intel_dp_aux_native_read(intel_output,
  895. 0x000, dp_priv->dpcd,
  896. sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
  897. {
  898. if (dp_priv->dpcd[0] != 0)
  899. status = connector_status_connected;
  900. }
  901. return status;
  902. }
  903. static int intel_dp_get_modes(struct drm_connector *connector)
  904. {
  905. struct intel_output *intel_output = to_intel_output(connector);
  906. /* We should parse the EDID data and find out if it has an audio sink
  907. */
  908. return intel_ddc_get_modes(intel_output);
  909. }
  910. static void
  911. intel_dp_destroy (struct drm_connector *connector)
  912. {
  913. struct intel_output *intel_output = to_intel_output(connector);
  914. if (intel_output->i2c_bus)
  915. intel_i2c_destroy(intel_output->i2c_bus);
  916. drm_sysfs_connector_remove(connector);
  917. drm_connector_cleanup(connector);
  918. kfree(intel_output);
  919. }
  920. static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
  921. .dpms = intel_dp_dpms,
  922. .mode_fixup = intel_dp_mode_fixup,
  923. .prepare = intel_encoder_prepare,
  924. .mode_set = intel_dp_mode_set,
  925. .commit = intel_encoder_commit,
  926. };
  927. static const struct drm_connector_funcs intel_dp_connector_funcs = {
  928. .dpms = drm_helper_connector_dpms,
  929. .save = intel_dp_save,
  930. .restore = intel_dp_restore,
  931. .detect = intel_dp_detect,
  932. .fill_modes = drm_helper_probe_single_connector_modes,
  933. .destroy = intel_dp_destroy,
  934. };
  935. static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
  936. .get_modes = intel_dp_get_modes,
  937. .mode_valid = intel_dp_mode_valid,
  938. .best_encoder = intel_best_encoder,
  939. };
  940. static void intel_dp_enc_destroy(struct drm_encoder *encoder)
  941. {
  942. drm_encoder_cleanup(encoder);
  943. }
  944. static const struct drm_encoder_funcs intel_dp_enc_funcs = {
  945. .destroy = intel_dp_enc_destroy,
  946. };
  947. void
  948. intel_dp_hot_plug(struct intel_output *intel_output)
  949. {
  950. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  951. if (dp_priv->dpms_mode == DRM_MODE_DPMS_ON)
  952. intel_dp_check_link_status(intel_output);
  953. }
  954. void
  955. intel_dp_init(struct drm_device *dev, int output_reg)
  956. {
  957. struct drm_i915_private *dev_priv = dev->dev_private;
  958. struct drm_connector *connector;
  959. struct intel_output *intel_output;
  960. struct intel_dp_priv *dp_priv;
  961. intel_output = kcalloc(sizeof(struct intel_output) +
  962. sizeof(struct intel_dp_priv), 1, GFP_KERNEL);
  963. if (!intel_output)
  964. return;
  965. dp_priv = (struct intel_dp_priv *)(intel_output + 1);
  966. connector = &intel_output->base;
  967. drm_connector_init(dev, connector, &intel_dp_connector_funcs,
  968. DRM_MODE_CONNECTOR_DisplayPort);
  969. drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
  970. intel_output->type = INTEL_OUTPUT_DISPLAYPORT;
  971. connector->interlace_allowed = true;
  972. connector->doublescan_allowed = 0;
  973. dp_priv->intel_output = intel_output;
  974. dp_priv->output_reg = output_reg;
  975. dp_priv->has_audio = false;
  976. dp_priv->dpms_mode = DRM_MODE_DPMS_ON;
  977. intel_output->dev_priv = dp_priv;
  978. drm_encoder_init(dev, &intel_output->enc, &intel_dp_enc_funcs,
  979. DRM_MODE_ENCODER_TMDS);
  980. drm_encoder_helper_add(&intel_output->enc, &intel_dp_helper_funcs);
  981. drm_mode_connector_attach_encoder(&intel_output->base,
  982. &intel_output->enc);
  983. drm_sysfs_connector_add(connector);
  984. /* Set up the DDC bus. */
  985. intel_dp_i2c_init(intel_output,
  986. (output_reg == DP_B) ? "DPDDC-B" :
  987. (output_reg == DP_C) ? "DPDDC-C" : "DPDDC-D");
  988. intel_output->ddc_bus = &dp_priv->adapter;
  989. intel_output->hot_plug = intel_dp_hot_plug;
  990. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  991. * 0xd. Failure to do so will result in spurious interrupts being
  992. * generated on the port when a cable is not attached.
  993. */
  994. if (IS_G4X(dev) && !IS_GM45(dev)) {
  995. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  996. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  997. }
  998. }