intel_dp.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  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. printk(KERN_ERR "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. printk(KERN_ERR "dp_aux_ch receive error status 0x%08x\n", status);
  229. return -EIO;
  230. }
  231. if (status & DP_AUX_CH_CTL_TIME_OUT_ERROR) {
  232. printk(KERN_ERR "dp_aux_ch timeout status 0x%08x\n", status);
  233. return -ETIMEDOUT;
  234. }
  235. /* Unload any bytes sent back from the other side */
  236. recv_bytes = ((status & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >>
  237. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
  238. if (recv_bytes > recv_size)
  239. recv_bytes = recv_size;
  240. for (i = 0; i < recv_bytes; i += 4) {
  241. uint32_t d = I915_READ(ch_data + i);
  242. unpack_aux(d, recv + i, recv_bytes - i);
  243. }
  244. return recv_bytes;
  245. }
  246. /* Write data to the aux channel in native mode */
  247. static int
  248. intel_dp_aux_native_write(struct intel_output *intel_output,
  249. uint16_t address, uint8_t *send, int send_bytes)
  250. {
  251. int ret;
  252. uint8_t msg[20];
  253. int msg_bytes;
  254. uint8_t ack;
  255. if (send_bytes > 16)
  256. return -1;
  257. msg[0] = AUX_NATIVE_WRITE << 4;
  258. msg[1] = address >> 8;
  259. msg[2] = address;
  260. msg[3] = send_bytes - 1;
  261. memcpy(&msg[4], send, send_bytes);
  262. msg_bytes = send_bytes + 4;
  263. for (;;) {
  264. ret = intel_dp_aux_ch(intel_output, msg, msg_bytes, &ack, 1);
  265. if (ret < 0)
  266. return ret;
  267. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK)
  268. break;
  269. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  270. udelay(100);
  271. else
  272. return -EIO;
  273. }
  274. return send_bytes;
  275. }
  276. /* Write a single byte to the aux channel in native mode */
  277. static int
  278. intel_dp_aux_native_write_1(struct intel_output *intel_output,
  279. uint16_t address, uint8_t byte)
  280. {
  281. return intel_dp_aux_native_write(intel_output, address, &byte, 1);
  282. }
  283. /* read bytes from a native aux channel */
  284. static int
  285. intel_dp_aux_native_read(struct intel_output *intel_output,
  286. uint16_t address, uint8_t *recv, int recv_bytes)
  287. {
  288. uint8_t msg[4];
  289. int msg_bytes;
  290. uint8_t reply[20];
  291. int reply_bytes;
  292. uint8_t ack;
  293. int ret;
  294. msg[0] = AUX_NATIVE_READ << 4;
  295. msg[1] = address >> 8;
  296. msg[2] = address & 0xff;
  297. msg[3] = recv_bytes - 1;
  298. msg_bytes = 4;
  299. reply_bytes = recv_bytes + 1;
  300. for (;;) {
  301. ret = intel_dp_aux_ch(intel_output, msg, msg_bytes,
  302. reply, reply_bytes);
  303. if (ret == 0)
  304. return -EPROTO;
  305. if (ret < 0)
  306. return ret;
  307. ack = reply[0];
  308. if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_ACK) {
  309. memcpy(recv, reply + 1, ret - 1);
  310. return ret - 1;
  311. }
  312. else if ((ack & AUX_NATIVE_REPLY_MASK) == AUX_NATIVE_REPLY_DEFER)
  313. udelay(100);
  314. else
  315. return -EIO;
  316. }
  317. }
  318. static int
  319. intel_dp_i2c_aux_ch(struct i2c_adapter *adapter,
  320. uint8_t *send, int send_bytes,
  321. uint8_t *recv, int recv_bytes)
  322. {
  323. struct intel_dp_priv *dp_priv = container_of(adapter,
  324. struct intel_dp_priv,
  325. adapter);
  326. struct intel_output *intel_output = dp_priv->intel_output;
  327. return intel_dp_aux_ch(intel_output,
  328. send, send_bytes, recv, recv_bytes);
  329. }
  330. static int
  331. intel_dp_i2c_init(struct intel_output *intel_output, const char *name)
  332. {
  333. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  334. DRM_ERROR("i2c_init %s\n", name);
  335. dp_priv->algo.running = false;
  336. dp_priv->algo.address = 0;
  337. dp_priv->algo.aux_ch = intel_dp_i2c_aux_ch;
  338. memset(&dp_priv->adapter, '\0', sizeof (dp_priv->adapter));
  339. dp_priv->adapter.owner = THIS_MODULE;
  340. dp_priv->adapter.class = I2C_CLASS_DDC;
  341. strncpy (dp_priv->adapter.name, name, sizeof dp_priv->adapter.name - 1);
  342. dp_priv->adapter.name[sizeof dp_priv->adapter.name - 1] = '\0';
  343. dp_priv->adapter.algo_data = &dp_priv->algo;
  344. dp_priv->adapter.dev.parent = &intel_output->base.kdev;
  345. return i2c_dp_aux_add_bus(&dp_priv->adapter);
  346. }
  347. static bool
  348. intel_dp_mode_fixup(struct drm_encoder *encoder, struct drm_display_mode *mode,
  349. struct drm_display_mode *adjusted_mode)
  350. {
  351. struct intel_output *intel_output = enc_to_intel_output(encoder);
  352. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  353. int lane_count, clock;
  354. int max_lane_count = intel_dp_max_lane_count(intel_output);
  355. int max_clock = intel_dp_max_link_bw(intel_output) == DP_LINK_BW_2_7 ? 1 : 0;
  356. static int bws[2] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7 };
  357. for (lane_count = 1; lane_count <= max_lane_count; lane_count <<= 1) {
  358. for (clock = 0; clock <= max_clock; clock++) {
  359. int link_avail = intel_dp_link_clock(bws[clock]) * lane_count;
  360. if (intel_dp_link_required(mode->clock) <= link_avail) {
  361. dp_priv->link_bw = bws[clock];
  362. dp_priv->lane_count = lane_count;
  363. adjusted_mode->clock = intel_dp_link_clock(dp_priv->link_bw);
  364. printk(KERN_ERR "link bw %02x lane count %d clock %d\n",
  365. dp_priv->link_bw, dp_priv->lane_count,
  366. adjusted_mode->clock);
  367. return true;
  368. }
  369. }
  370. }
  371. return false;
  372. }
  373. struct intel_dp_m_n {
  374. uint32_t tu;
  375. uint32_t gmch_m;
  376. uint32_t gmch_n;
  377. uint32_t link_m;
  378. uint32_t link_n;
  379. };
  380. static void
  381. intel_reduce_ratio(uint32_t *num, uint32_t *den)
  382. {
  383. while (*num > 0xffffff || *den > 0xffffff) {
  384. *num >>= 1;
  385. *den >>= 1;
  386. }
  387. }
  388. static void
  389. intel_dp_compute_m_n(int bytes_per_pixel,
  390. int nlanes,
  391. int pixel_clock,
  392. int link_clock,
  393. struct intel_dp_m_n *m_n)
  394. {
  395. m_n->tu = 64;
  396. m_n->gmch_m = pixel_clock * bytes_per_pixel;
  397. m_n->gmch_n = link_clock * nlanes;
  398. intel_reduce_ratio(&m_n->gmch_m, &m_n->gmch_n);
  399. m_n->link_m = pixel_clock;
  400. m_n->link_n = link_clock;
  401. intel_reduce_ratio(&m_n->link_m, &m_n->link_n);
  402. }
  403. void
  404. intel_dp_set_m_n(struct drm_crtc *crtc, struct drm_display_mode *mode,
  405. struct drm_display_mode *adjusted_mode)
  406. {
  407. struct drm_device *dev = crtc->dev;
  408. struct drm_mode_config *mode_config = &dev->mode_config;
  409. struct drm_connector *connector;
  410. struct drm_i915_private *dev_priv = dev->dev_private;
  411. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  412. int lane_count = 4;
  413. struct intel_dp_m_n m_n;
  414. /*
  415. * Find the lane count in the intel_output private
  416. */
  417. list_for_each_entry(connector, &mode_config->connector_list, head) {
  418. struct intel_output *intel_output = to_intel_output(connector);
  419. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  420. if (!connector->encoder || connector->encoder->crtc != crtc)
  421. continue;
  422. if (intel_output->type == INTEL_OUTPUT_DISPLAYPORT) {
  423. lane_count = dp_priv->lane_count;
  424. break;
  425. }
  426. }
  427. /*
  428. * Compute the GMCH and Link ratios. The '3' here is
  429. * the number of bytes_per_pixel post-LUT, which we always
  430. * set up for 8-bits of R/G/B, or 3 bytes total.
  431. */
  432. intel_dp_compute_m_n(3, lane_count,
  433. mode->clock, adjusted_mode->clock, &m_n);
  434. if (intel_crtc->pipe == 0) {
  435. I915_WRITE(PIPEA_GMCH_DATA_M,
  436. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  437. m_n.gmch_m);
  438. I915_WRITE(PIPEA_GMCH_DATA_N,
  439. m_n.gmch_n);
  440. I915_WRITE(PIPEA_DP_LINK_M, m_n.link_m);
  441. I915_WRITE(PIPEA_DP_LINK_N, m_n.link_n);
  442. } else {
  443. I915_WRITE(PIPEB_GMCH_DATA_M,
  444. ((m_n.tu - 1) << PIPE_GMCH_DATA_M_TU_SIZE_SHIFT) |
  445. m_n.gmch_m);
  446. I915_WRITE(PIPEB_GMCH_DATA_N,
  447. m_n.gmch_n);
  448. I915_WRITE(PIPEB_DP_LINK_M, m_n.link_m);
  449. I915_WRITE(PIPEB_DP_LINK_N, m_n.link_n);
  450. }
  451. }
  452. static void
  453. intel_dp_mode_set(struct drm_encoder *encoder, struct drm_display_mode *mode,
  454. struct drm_display_mode *adjusted_mode)
  455. {
  456. struct intel_output *intel_output = enc_to_intel_output(encoder);
  457. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  458. struct drm_crtc *crtc = intel_output->enc.crtc;
  459. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  460. dp_priv->DP = (DP_LINK_TRAIN_OFF |
  461. DP_VOLTAGE_0_4 |
  462. DP_PRE_EMPHASIS_0 |
  463. DP_SYNC_VS_HIGH |
  464. DP_SYNC_HS_HIGH);
  465. switch (dp_priv->lane_count) {
  466. case 1:
  467. dp_priv->DP |= DP_PORT_WIDTH_1;
  468. break;
  469. case 2:
  470. dp_priv->DP |= DP_PORT_WIDTH_2;
  471. break;
  472. case 4:
  473. dp_priv->DP |= DP_PORT_WIDTH_4;
  474. break;
  475. }
  476. if (dp_priv->has_audio)
  477. dp_priv->DP |= DP_AUDIO_OUTPUT_ENABLE;
  478. memset(dp_priv->link_configuration, 0, DP_LINK_CONFIGURATION_SIZE);
  479. dp_priv->link_configuration[0] = dp_priv->link_bw;
  480. dp_priv->link_configuration[1] = dp_priv->lane_count;
  481. /*
  482. * Check for DPCD version > 1.1,
  483. * enable enahanced frame stuff in that case
  484. */
  485. if (dp_priv->dpcd[0] >= 0x11) {
  486. dp_priv->link_configuration[1] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
  487. dp_priv->DP |= DP_ENHANCED_FRAMING;
  488. }
  489. if (intel_crtc->pipe == 1)
  490. dp_priv->DP |= DP_PIPEB_SELECT;
  491. }
  492. static void
  493. intel_dp_dpms(struct drm_encoder *encoder, int mode)
  494. {
  495. struct intel_output *intel_output = enc_to_intel_output(encoder);
  496. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  497. struct drm_device *dev = intel_output->base.dev;
  498. struct drm_i915_private *dev_priv = dev->dev_private;
  499. uint32_t dp_reg = I915_READ(dp_priv->output_reg);
  500. if (mode != DRM_MODE_DPMS_ON) {
  501. if (dp_reg & DP_PORT_EN)
  502. intel_dp_link_down(intel_output, dp_priv->DP);
  503. } else {
  504. if (!(dp_reg & DP_PORT_EN))
  505. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  506. }
  507. dp_priv->dpms_mode = mode;
  508. }
  509. /*
  510. * Fetch AUX CH registers 0x202 - 0x207 which contain
  511. * link status information
  512. */
  513. static bool
  514. intel_dp_get_link_status(struct intel_output *intel_output,
  515. uint8_t link_status[DP_LINK_STATUS_SIZE])
  516. {
  517. int ret;
  518. ret = intel_dp_aux_native_read(intel_output,
  519. DP_LANE0_1_STATUS,
  520. link_status, DP_LINK_STATUS_SIZE);
  521. if (ret != DP_LINK_STATUS_SIZE)
  522. return false;
  523. return true;
  524. }
  525. static uint8_t
  526. intel_dp_link_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  527. int r)
  528. {
  529. return link_status[r - DP_LANE0_1_STATUS];
  530. }
  531. static void
  532. intel_dp_save(struct drm_connector *connector)
  533. {
  534. struct intel_output *intel_output = to_intel_output(connector);
  535. struct drm_device *dev = intel_output->base.dev;
  536. struct drm_i915_private *dev_priv = dev->dev_private;
  537. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  538. dp_priv->save_DP = I915_READ(dp_priv->output_reg);
  539. intel_dp_aux_native_read(intel_output, DP_LINK_BW_SET,
  540. dp_priv->save_link_configuration,
  541. sizeof (dp_priv->save_link_configuration));
  542. }
  543. static uint8_t
  544. intel_get_adjust_request_voltage(uint8_t link_status[DP_LINK_STATUS_SIZE],
  545. int lane)
  546. {
  547. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  548. int s = ((lane & 1) ?
  549. DP_ADJUST_VOLTAGE_SWING_LANE1_SHIFT :
  550. DP_ADJUST_VOLTAGE_SWING_LANE0_SHIFT);
  551. uint8_t l = intel_dp_link_status(link_status, i);
  552. return ((l >> s) & 3) << DP_TRAIN_VOLTAGE_SWING_SHIFT;
  553. }
  554. static uint8_t
  555. intel_get_adjust_request_pre_emphasis(uint8_t link_status[DP_LINK_STATUS_SIZE],
  556. int lane)
  557. {
  558. int i = DP_ADJUST_REQUEST_LANE0_1 + (lane >> 1);
  559. int s = ((lane & 1) ?
  560. DP_ADJUST_PRE_EMPHASIS_LANE1_SHIFT :
  561. DP_ADJUST_PRE_EMPHASIS_LANE0_SHIFT);
  562. uint8_t l = intel_dp_link_status(link_status, i);
  563. return ((l >> s) & 3) << DP_TRAIN_PRE_EMPHASIS_SHIFT;
  564. }
  565. #if 0
  566. static char *voltage_names[] = {
  567. "0.4V", "0.6V", "0.8V", "1.2V"
  568. };
  569. static char *pre_emph_names[] = {
  570. "0dB", "3.5dB", "6dB", "9.5dB"
  571. };
  572. static char *link_train_names[] = {
  573. "pattern 1", "pattern 2", "idle", "off"
  574. };
  575. #endif
  576. /*
  577. * These are source-specific values; current Intel hardware supports
  578. * a maximum voltage of 800mV and a maximum pre-emphasis of 6dB
  579. */
  580. #define I830_DP_VOLTAGE_MAX DP_TRAIN_VOLTAGE_SWING_800
  581. static uint8_t
  582. intel_dp_pre_emphasis_max(uint8_t voltage_swing)
  583. {
  584. switch (voltage_swing & DP_TRAIN_VOLTAGE_SWING_MASK) {
  585. case DP_TRAIN_VOLTAGE_SWING_400:
  586. return DP_TRAIN_PRE_EMPHASIS_6;
  587. case DP_TRAIN_VOLTAGE_SWING_600:
  588. return DP_TRAIN_PRE_EMPHASIS_6;
  589. case DP_TRAIN_VOLTAGE_SWING_800:
  590. return DP_TRAIN_PRE_EMPHASIS_3_5;
  591. case DP_TRAIN_VOLTAGE_SWING_1200:
  592. default:
  593. return DP_TRAIN_PRE_EMPHASIS_0;
  594. }
  595. }
  596. static void
  597. intel_get_adjust_train(struct intel_output *intel_output,
  598. uint8_t link_status[DP_LINK_STATUS_SIZE],
  599. int lane_count,
  600. uint8_t train_set[4])
  601. {
  602. uint8_t v = 0;
  603. uint8_t p = 0;
  604. int lane;
  605. for (lane = 0; lane < lane_count; lane++) {
  606. uint8_t this_v = intel_get_adjust_request_voltage(link_status, lane);
  607. uint8_t this_p = intel_get_adjust_request_pre_emphasis(link_status, lane);
  608. if (this_v > v)
  609. v = this_v;
  610. if (this_p > p)
  611. p = this_p;
  612. }
  613. if (v >= I830_DP_VOLTAGE_MAX)
  614. v = I830_DP_VOLTAGE_MAX | DP_TRAIN_MAX_SWING_REACHED;
  615. if (p >= intel_dp_pre_emphasis_max(v))
  616. p = intel_dp_pre_emphasis_max(v) | DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
  617. for (lane = 0; lane < 4; lane++)
  618. train_set[lane] = v | p;
  619. }
  620. static uint32_t
  621. intel_dp_signal_levels(uint8_t train_set, int lane_count)
  622. {
  623. uint32_t signal_levels = 0;
  624. switch (train_set & DP_TRAIN_VOLTAGE_SWING_MASK) {
  625. case DP_TRAIN_VOLTAGE_SWING_400:
  626. default:
  627. signal_levels |= DP_VOLTAGE_0_4;
  628. break;
  629. case DP_TRAIN_VOLTAGE_SWING_600:
  630. signal_levels |= DP_VOLTAGE_0_6;
  631. break;
  632. case DP_TRAIN_VOLTAGE_SWING_800:
  633. signal_levels |= DP_VOLTAGE_0_8;
  634. break;
  635. case DP_TRAIN_VOLTAGE_SWING_1200:
  636. signal_levels |= DP_VOLTAGE_1_2;
  637. break;
  638. }
  639. switch (train_set & DP_TRAIN_PRE_EMPHASIS_MASK) {
  640. case DP_TRAIN_PRE_EMPHASIS_0:
  641. default:
  642. signal_levels |= DP_PRE_EMPHASIS_0;
  643. break;
  644. case DP_TRAIN_PRE_EMPHASIS_3_5:
  645. signal_levels |= DP_PRE_EMPHASIS_3_5;
  646. break;
  647. case DP_TRAIN_PRE_EMPHASIS_6:
  648. signal_levels |= DP_PRE_EMPHASIS_6;
  649. break;
  650. case DP_TRAIN_PRE_EMPHASIS_9_5:
  651. signal_levels |= DP_PRE_EMPHASIS_9_5;
  652. break;
  653. }
  654. return signal_levels;
  655. }
  656. static uint8_t
  657. intel_get_lane_status(uint8_t link_status[DP_LINK_STATUS_SIZE],
  658. int lane)
  659. {
  660. int i = DP_LANE0_1_STATUS + (lane >> 1);
  661. int s = (lane & 1) * 4;
  662. uint8_t l = intel_dp_link_status(link_status, i);
  663. return (l >> s) & 0xf;
  664. }
  665. /* Check for clock recovery is done on all channels */
  666. static bool
  667. intel_clock_recovery_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  668. {
  669. int lane;
  670. uint8_t lane_status;
  671. for (lane = 0; lane < lane_count; lane++) {
  672. lane_status = intel_get_lane_status(link_status, lane);
  673. if ((lane_status & DP_LANE_CR_DONE) == 0)
  674. return false;
  675. }
  676. return true;
  677. }
  678. /* Check to see if channel eq is done on all channels */
  679. #define CHANNEL_EQ_BITS (DP_LANE_CR_DONE|\
  680. DP_LANE_CHANNEL_EQ_DONE|\
  681. DP_LANE_SYMBOL_LOCKED)
  682. static bool
  683. intel_channel_eq_ok(uint8_t link_status[DP_LINK_STATUS_SIZE], int lane_count)
  684. {
  685. uint8_t lane_align;
  686. uint8_t lane_status;
  687. int lane;
  688. lane_align = intel_dp_link_status(link_status,
  689. DP_LANE_ALIGN_STATUS_UPDATED);
  690. if ((lane_align & DP_INTERLANE_ALIGN_DONE) == 0)
  691. return false;
  692. for (lane = 0; lane < lane_count; lane++) {
  693. lane_status = intel_get_lane_status(link_status, lane);
  694. if ((lane_status & CHANNEL_EQ_BITS) != CHANNEL_EQ_BITS)
  695. return false;
  696. }
  697. return true;
  698. }
  699. static bool
  700. intel_dp_set_link_train(struct intel_output *intel_output,
  701. uint32_t dp_reg_value,
  702. uint8_t dp_train_pat,
  703. uint8_t train_set[4],
  704. bool first)
  705. {
  706. struct drm_device *dev = intel_output->base.dev;
  707. struct drm_i915_private *dev_priv = dev->dev_private;
  708. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  709. int ret;
  710. I915_WRITE(dp_priv->output_reg, dp_reg_value);
  711. POSTING_READ(dp_priv->output_reg);
  712. if (first)
  713. intel_wait_for_vblank(dev);
  714. intel_dp_aux_native_write_1(intel_output,
  715. DP_TRAINING_PATTERN_SET,
  716. dp_train_pat);
  717. ret = intel_dp_aux_native_write(intel_output,
  718. DP_TRAINING_LANE0_SET, train_set, 4);
  719. if (ret != 4)
  720. return false;
  721. return true;
  722. }
  723. static void
  724. intel_dp_link_train(struct intel_output *intel_output, uint32_t DP,
  725. uint8_t link_configuration[DP_LINK_CONFIGURATION_SIZE])
  726. {
  727. struct drm_device *dev = intel_output->base.dev;
  728. struct drm_i915_private *dev_priv = dev->dev_private;
  729. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  730. uint8_t train_set[4];
  731. uint8_t link_status[DP_LINK_STATUS_SIZE];
  732. int i;
  733. uint8_t voltage;
  734. bool clock_recovery = false;
  735. bool channel_eq = false;
  736. bool first = true;
  737. int tries;
  738. /* Write the link configuration data */
  739. intel_dp_aux_native_write(intel_output, 0x100,
  740. link_configuration, DP_LINK_CONFIGURATION_SIZE);
  741. DP |= DP_PORT_EN;
  742. DP &= ~DP_LINK_TRAIN_MASK;
  743. memset(train_set, 0, 4);
  744. voltage = 0xff;
  745. tries = 0;
  746. clock_recovery = false;
  747. for (;;) {
  748. /* Use train_set[0] to set the voltage and pre emphasis values */
  749. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  750. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  751. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_1,
  752. DP_TRAINING_PATTERN_1, train_set, first))
  753. break;
  754. first = false;
  755. /* Set training pattern 1 */
  756. udelay(100);
  757. if (!intel_dp_get_link_status(intel_output, link_status))
  758. break;
  759. if (intel_clock_recovery_ok(link_status, dp_priv->lane_count)) {
  760. clock_recovery = true;
  761. break;
  762. }
  763. /* Check to see if we've tried the max voltage */
  764. for (i = 0; i < dp_priv->lane_count; i++)
  765. if ((train_set[i] & DP_TRAIN_MAX_SWING_REACHED) == 0)
  766. break;
  767. if (i == dp_priv->lane_count)
  768. break;
  769. /* Check to see if we've tried the same voltage 5 times */
  770. if ((train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == voltage) {
  771. ++tries;
  772. if (tries == 5)
  773. break;
  774. } else
  775. tries = 0;
  776. voltage = train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
  777. /* Compute new train_set as requested by target */
  778. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  779. }
  780. /* channel equalization */
  781. tries = 0;
  782. channel_eq = false;
  783. for (;;) {
  784. /* Use train_set[0] to set the voltage and pre emphasis values */
  785. uint32_t signal_levels = intel_dp_signal_levels(train_set[0], dp_priv->lane_count);
  786. DP = (DP & ~(DP_VOLTAGE_MASK|DP_PRE_EMPHASIS_MASK)) | signal_levels;
  787. /* channel eq pattern */
  788. if (!intel_dp_set_link_train(intel_output, DP | DP_LINK_TRAIN_PAT_2,
  789. DP_TRAINING_PATTERN_2, train_set,
  790. false))
  791. break;
  792. udelay(400);
  793. if (!intel_dp_get_link_status(intel_output, link_status))
  794. break;
  795. if (intel_channel_eq_ok(link_status, dp_priv->lane_count)) {
  796. channel_eq = true;
  797. break;
  798. }
  799. /* Try 5 times */
  800. if (tries > 5)
  801. break;
  802. /* Compute new train_set as requested by target */
  803. intel_get_adjust_train(intel_output, link_status, dp_priv->lane_count, train_set);
  804. ++tries;
  805. }
  806. I915_WRITE(dp_priv->output_reg, DP | DP_LINK_TRAIN_OFF);
  807. POSTING_READ(dp_priv->output_reg);
  808. intel_dp_aux_native_write_1(intel_output,
  809. DP_TRAINING_PATTERN_SET, DP_TRAINING_PATTERN_DISABLE);
  810. }
  811. static void
  812. intel_dp_link_down(struct intel_output *intel_output, uint32_t DP)
  813. {
  814. struct drm_device *dev = intel_output->base.dev;
  815. struct drm_i915_private *dev_priv = dev->dev_private;
  816. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  817. I915_WRITE(dp_priv->output_reg, DP & ~DP_PORT_EN);
  818. POSTING_READ(dp_priv->output_reg);
  819. }
  820. static void
  821. intel_dp_restore(struct drm_connector *connector)
  822. {
  823. struct intel_output *intel_output = to_intel_output(connector);
  824. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  825. if (dp_priv->save_DP & DP_PORT_EN)
  826. intel_dp_link_train(intel_output, dp_priv->save_DP, dp_priv->save_link_configuration);
  827. else
  828. intel_dp_link_down(intel_output, dp_priv->save_DP);
  829. }
  830. /*
  831. * According to DP spec
  832. * 5.1.2:
  833. * 1. Read DPCD
  834. * 2. Configure link according to Receiver Capabilities
  835. * 3. Use Link Training from 2.5.3.3 and 3.5.1.3
  836. * 4. Check link status on receipt of hot-plug interrupt
  837. */
  838. static void
  839. intel_dp_check_link_status(struct intel_output *intel_output)
  840. {
  841. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  842. uint8_t link_status[DP_LINK_STATUS_SIZE];
  843. if (!intel_output->enc.crtc)
  844. return;
  845. if (!intel_dp_get_link_status(intel_output, link_status)) {
  846. intel_dp_link_down(intel_output, dp_priv->DP);
  847. return;
  848. }
  849. if (!intel_channel_eq_ok(link_status, dp_priv->lane_count))
  850. intel_dp_link_train(intel_output, dp_priv->DP, dp_priv->link_configuration);
  851. }
  852. /**
  853. * Uses CRT_HOTPLUG_EN and CRT_HOTPLUG_STAT to detect DP connection.
  854. *
  855. * \return true if DP port is connected.
  856. * \return false if DP port is disconnected.
  857. */
  858. static enum drm_connector_status
  859. intel_dp_detect(struct drm_connector *connector)
  860. {
  861. struct intel_output *intel_output = to_intel_output(connector);
  862. struct drm_device *dev = intel_output->base.dev;
  863. struct drm_i915_private *dev_priv = dev->dev_private;
  864. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  865. uint32_t temp, bit;
  866. enum drm_connector_status status;
  867. dp_priv->has_audio = false;
  868. temp = I915_READ(PORT_HOTPLUG_EN);
  869. I915_WRITE(PORT_HOTPLUG_EN,
  870. temp |
  871. DPB_HOTPLUG_INT_EN |
  872. DPC_HOTPLUG_INT_EN |
  873. DPD_HOTPLUG_INT_EN);
  874. POSTING_READ(PORT_HOTPLUG_EN);
  875. switch (dp_priv->output_reg) {
  876. case DP_B:
  877. bit = DPB_HOTPLUG_INT_STATUS;
  878. break;
  879. case DP_C:
  880. bit = DPC_HOTPLUG_INT_STATUS;
  881. break;
  882. case DP_D:
  883. bit = DPD_HOTPLUG_INT_STATUS;
  884. break;
  885. default:
  886. return connector_status_unknown;
  887. }
  888. temp = I915_READ(PORT_HOTPLUG_STAT);
  889. if ((temp & bit) == 0)
  890. return connector_status_disconnected;
  891. status = connector_status_disconnected;
  892. if (intel_dp_aux_native_read(intel_output,
  893. 0x000, dp_priv->dpcd,
  894. sizeof (dp_priv->dpcd)) == sizeof (dp_priv->dpcd))
  895. {
  896. if (dp_priv->dpcd[0] != 0)
  897. status = connector_status_connected;
  898. }
  899. return status;
  900. }
  901. static int intel_dp_get_modes(struct drm_connector *connector)
  902. {
  903. struct intel_output *intel_output = to_intel_output(connector);
  904. /* We should parse the EDID data and find out if it has an audio sink
  905. */
  906. return intel_ddc_get_modes(intel_output);
  907. }
  908. static void
  909. intel_dp_destroy (struct drm_connector *connector)
  910. {
  911. struct intel_output *intel_output = to_intel_output(connector);
  912. if (intel_output->i2c_bus)
  913. intel_i2c_destroy(intel_output->i2c_bus);
  914. drm_sysfs_connector_remove(connector);
  915. drm_connector_cleanup(connector);
  916. kfree(intel_output);
  917. }
  918. static const struct drm_encoder_helper_funcs intel_dp_helper_funcs = {
  919. .dpms = intel_dp_dpms,
  920. .mode_fixup = intel_dp_mode_fixup,
  921. .prepare = intel_encoder_prepare,
  922. .mode_set = intel_dp_mode_set,
  923. .commit = intel_encoder_commit,
  924. };
  925. static const struct drm_connector_funcs intel_dp_connector_funcs = {
  926. .dpms = drm_helper_connector_dpms,
  927. .save = intel_dp_save,
  928. .restore = intel_dp_restore,
  929. .detect = intel_dp_detect,
  930. .fill_modes = drm_helper_probe_single_connector_modes,
  931. .destroy = intel_dp_destroy,
  932. };
  933. static const struct drm_connector_helper_funcs intel_dp_connector_helper_funcs = {
  934. .get_modes = intel_dp_get_modes,
  935. .mode_valid = intel_dp_mode_valid,
  936. .best_encoder = intel_best_encoder,
  937. };
  938. static void intel_dp_enc_destroy(struct drm_encoder *encoder)
  939. {
  940. drm_encoder_cleanup(encoder);
  941. }
  942. static const struct drm_encoder_funcs intel_dp_enc_funcs = {
  943. .destroy = intel_dp_enc_destroy,
  944. };
  945. void
  946. intel_dp_hot_plug(struct intel_output *intel_output)
  947. {
  948. struct intel_dp_priv *dp_priv = intel_output->dev_priv;
  949. if (dp_priv->dpms_mode == DRM_MODE_DPMS_ON)
  950. intel_dp_check_link_status(intel_output);
  951. }
  952. void
  953. intel_dp_init(struct drm_device *dev, int output_reg)
  954. {
  955. struct drm_i915_private *dev_priv = dev->dev_private;
  956. struct drm_connector *connector;
  957. struct intel_output *intel_output;
  958. struct intel_dp_priv *dp_priv;
  959. intel_output = kcalloc(sizeof(struct intel_output) +
  960. sizeof(struct intel_dp_priv), 1, GFP_KERNEL);
  961. if (!intel_output)
  962. return;
  963. dp_priv = (struct intel_dp_priv *)(intel_output + 1);
  964. connector = &intel_output->base;
  965. drm_connector_init(dev, connector, &intel_dp_connector_funcs,
  966. DRM_MODE_CONNECTOR_DisplayPort);
  967. drm_connector_helper_add(connector, &intel_dp_connector_helper_funcs);
  968. intel_output->type = INTEL_OUTPUT_DISPLAYPORT;
  969. connector->interlace_allowed = true;
  970. connector->doublescan_allowed = 0;
  971. dp_priv->intel_output = intel_output;
  972. dp_priv->output_reg = output_reg;
  973. dp_priv->has_audio = false;
  974. dp_priv->dpms_mode = DRM_MODE_DPMS_ON;
  975. intel_output->dev_priv = dp_priv;
  976. drm_encoder_init(dev, &intel_output->enc, &intel_dp_enc_funcs,
  977. DRM_MODE_ENCODER_TMDS);
  978. drm_encoder_helper_add(&intel_output->enc, &intel_dp_helper_funcs);
  979. drm_mode_connector_attach_encoder(&intel_output->base,
  980. &intel_output->enc);
  981. drm_sysfs_connector_add(connector);
  982. /* Set up the DDC bus. */
  983. intel_dp_i2c_init(intel_output,
  984. (output_reg == DP_B) ? "DPDDC-B" :
  985. (output_reg == DP_C) ? "DPDDC-C" : "DPDDC-D");
  986. intel_output->ddc_bus = &dp_priv->adapter;
  987. intel_output->hot_plug = intel_dp_hot_plug;
  988. /* For G4X desktop chip, PEG_BAND_GAP_DATA 3:0 must first be written
  989. * 0xd. Failure to do so will result in spurious interrupts being
  990. * generated on the port when a cable is not attached.
  991. */
  992. if (IS_G4X(dev) && !IS_GM45(dev)) {
  993. u32 temp = I915_READ(PEG_BAND_GAP_DATA);
  994. I915_WRITE(PEG_BAND_GAP_DATA, (temp & ~0xf) | 0xd);
  995. }
  996. }