cdv_intel_display.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. /*
  2. * Copyright © 2006-2011 Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Authors:
  18. * Eric Anholt <eric@anholt.net>
  19. */
  20. #include <linux/i2c.h>
  21. #include <linux/pm_runtime.h>
  22. #include <drm/drmP.h>
  23. #include "framebuffer.h"
  24. #include "psb_drv.h"
  25. #include "psb_intel_drv.h"
  26. #include "psb_intel_reg.h"
  27. #include "gma_display.h"
  28. #include "power.h"
  29. #include "cdv_device.h"
  30. static bool cdv_intel_find_dp_pll(const struct gma_limit_t *limit,
  31. struct drm_crtc *crtc, int target,
  32. int refclk, struct gma_clock_t *best_clock);
  33. #define CDV_LIMIT_SINGLE_LVDS_96 0
  34. #define CDV_LIMIT_SINGLE_LVDS_100 1
  35. #define CDV_LIMIT_DAC_HDMI_27 2
  36. #define CDV_LIMIT_DAC_HDMI_96 3
  37. #define CDV_LIMIT_DP_27 4
  38. #define CDV_LIMIT_DP_100 5
  39. static const struct gma_limit_t cdv_intel_limits[] = {
  40. { /* CDV_SINGLE_LVDS_96MHz */
  41. .dot = {.min = 20000, .max = 115500},
  42. .vco = {.min = 1800000, .max = 3600000},
  43. .n = {.min = 2, .max = 6},
  44. .m = {.min = 60, .max = 160},
  45. .m1 = {.min = 0, .max = 0},
  46. .m2 = {.min = 58, .max = 158},
  47. .p = {.min = 28, .max = 140},
  48. .p1 = {.min = 2, .max = 10},
  49. .p2 = {.dot_limit = 200000, .p2_slow = 14, .p2_fast = 14},
  50. .find_pll = gma_find_best_pll,
  51. },
  52. { /* CDV_SINGLE_LVDS_100MHz */
  53. .dot = {.min = 20000, .max = 115500},
  54. .vco = {.min = 1800000, .max = 3600000},
  55. .n = {.min = 2, .max = 6},
  56. .m = {.min = 60, .max = 160},
  57. .m1 = {.min = 0, .max = 0},
  58. .m2 = {.min = 58, .max = 158},
  59. .p = {.min = 28, .max = 140},
  60. .p1 = {.min = 2, .max = 10},
  61. /* The single-channel range is 25-112Mhz, and dual-channel
  62. * is 80-224Mhz. Prefer single channel as much as possible.
  63. */
  64. .p2 = {.dot_limit = 200000, .p2_slow = 14, .p2_fast = 14},
  65. .find_pll = gma_find_best_pll,
  66. },
  67. { /* CDV_DAC_HDMI_27MHz */
  68. .dot = {.min = 20000, .max = 400000},
  69. .vco = {.min = 1809000, .max = 3564000},
  70. .n = {.min = 1, .max = 1},
  71. .m = {.min = 67, .max = 132},
  72. .m1 = {.min = 0, .max = 0},
  73. .m2 = {.min = 65, .max = 130},
  74. .p = {.min = 5, .max = 90},
  75. .p1 = {.min = 1, .max = 9},
  76. .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
  77. .find_pll = gma_find_best_pll,
  78. },
  79. { /* CDV_DAC_HDMI_96MHz */
  80. .dot = {.min = 20000, .max = 400000},
  81. .vco = {.min = 1800000, .max = 3600000},
  82. .n = {.min = 2, .max = 6},
  83. .m = {.min = 60, .max = 160},
  84. .m1 = {.min = 0, .max = 0},
  85. .m2 = {.min = 58, .max = 158},
  86. .p = {.min = 5, .max = 100},
  87. .p1 = {.min = 1, .max = 10},
  88. .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
  89. .find_pll = gma_find_best_pll,
  90. },
  91. { /* CDV_DP_27MHz */
  92. .dot = {.min = 160000, .max = 272000},
  93. .vco = {.min = 1809000, .max = 3564000},
  94. .n = {.min = 1, .max = 1},
  95. .m = {.min = 67, .max = 132},
  96. .m1 = {.min = 0, .max = 0},
  97. .m2 = {.min = 65, .max = 130},
  98. .p = {.min = 5, .max = 90},
  99. .p1 = {.min = 1, .max = 9},
  100. .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 10},
  101. .find_pll = cdv_intel_find_dp_pll,
  102. },
  103. { /* CDV_DP_100MHz */
  104. .dot = {.min = 160000, .max = 272000},
  105. .vco = {.min = 1800000, .max = 3600000},
  106. .n = {.min = 2, .max = 6},
  107. .m = {.min = 60, .max = 164},
  108. .m1 = {.min = 0, .max = 0},
  109. .m2 = {.min = 58, .max = 162},
  110. .p = {.min = 5, .max = 100},
  111. .p1 = {.min = 1, .max = 10},
  112. .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 10},
  113. .find_pll = cdv_intel_find_dp_pll,
  114. }
  115. };
  116. #define _wait_for(COND, MS, W) ({ \
  117. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS); \
  118. int ret__ = 0; \
  119. while (!(COND)) { \
  120. if (time_after(jiffies, timeout__)) { \
  121. ret__ = -ETIMEDOUT; \
  122. break; \
  123. } \
  124. if (W && !in_dbg_master()) \
  125. msleep(W); \
  126. } \
  127. ret__; \
  128. })
  129. #define wait_for(COND, MS) _wait_for(COND, MS, 1)
  130. int cdv_sb_read(struct drm_device *dev, u32 reg, u32 *val)
  131. {
  132. int ret;
  133. ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
  134. if (ret) {
  135. DRM_ERROR("timeout waiting for SB to idle before read\n");
  136. return ret;
  137. }
  138. REG_WRITE(SB_ADDR, reg);
  139. REG_WRITE(SB_PCKT,
  140. SET_FIELD(SB_OPCODE_READ, SB_OPCODE) |
  141. SET_FIELD(SB_DEST_DPLL, SB_DEST) |
  142. SET_FIELD(0xf, SB_BYTE_ENABLE));
  143. ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
  144. if (ret) {
  145. DRM_ERROR("timeout waiting for SB to idle after read\n");
  146. return ret;
  147. }
  148. *val = REG_READ(SB_DATA);
  149. return 0;
  150. }
  151. int cdv_sb_write(struct drm_device *dev, u32 reg, u32 val)
  152. {
  153. int ret;
  154. static bool dpio_debug = true;
  155. u32 temp;
  156. if (dpio_debug) {
  157. if (cdv_sb_read(dev, reg, &temp) == 0)
  158. DRM_DEBUG_KMS("0x%08x: 0x%08x (before)\n", reg, temp);
  159. DRM_DEBUG_KMS("0x%08x: 0x%08x\n", reg, val);
  160. }
  161. ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
  162. if (ret) {
  163. DRM_ERROR("timeout waiting for SB to idle before write\n");
  164. return ret;
  165. }
  166. REG_WRITE(SB_ADDR, reg);
  167. REG_WRITE(SB_DATA, val);
  168. REG_WRITE(SB_PCKT,
  169. SET_FIELD(SB_OPCODE_WRITE, SB_OPCODE) |
  170. SET_FIELD(SB_DEST_DPLL, SB_DEST) |
  171. SET_FIELD(0xf, SB_BYTE_ENABLE));
  172. ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
  173. if (ret) {
  174. DRM_ERROR("timeout waiting for SB to idle after write\n");
  175. return ret;
  176. }
  177. if (dpio_debug) {
  178. if (cdv_sb_read(dev, reg, &temp) == 0)
  179. DRM_DEBUG_KMS("0x%08x: 0x%08x (after)\n", reg, temp);
  180. }
  181. return 0;
  182. }
  183. /* Reset the DPIO configuration register. The BIOS does this at every
  184. * mode set.
  185. */
  186. void cdv_sb_reset(struct drm_device *dev)
  187. {
  188. REG_WRITE(DPIO_CFG, 0);
  189. REG_READ(DPIO_CFG);
  190. REG_WRITE(DPIO_CFG, DPIO_MODE_SELECT_0 | DPIO_CMN_RESET_N);
  191. }
  192. /* Unlike most Intel display engines, on Cedarview the DPLL registers
  193. * are behind this sideband bus. They must be programmed while the
  194. * DPLL reference clock is on in the DPLL control register, but before
  195. * the DPLL is enabled in the DPLL control register.
  196. */
  197. static int
  198. cdv_dpll_set_clock_cdv(struct drm_device *dev, struct drm_crtc *crtc,
  199. struct gma_clock_t *clock, bool is_lvds, u32 ddi_select)
  200. {
  201. struct psb_intel_crtc *psb_crtc = to_psb_intel_crtc(crtc);
  202. int pipe = psb_crtc->pipe;
  203. u32 m, n_vco, p;
  204. int ret = 0;
  205. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  206. int ref_sfr = (pipe == 0) ? SB_REF_DPLLA : SB_REF_DPLLB;
  207. u32 ref_value;
  208. u32 lane_reg, lane_value;
  209. cdv_sb_reset(dev);
  210. REG_WRITE(dpll_reg, DPLL_SYNCLOCK_ENABLE | DPLL_VGA_MODE_DIS);
  211. udelay(100);
  212. /* Follow the BIOS and write the REF/SFR Register. Hardcoded value */
  213. ref_value = 0x68A701;
  214. cdv_sb_write(dev, SB_REF_SFR(pipe), ref_value);
  215. /* We don't know what the other fields of these regs are, so
  216. * leave them in place.
  217. */
  218. /*
  219. * The BIT 14:13 of 0x8010/0x8030 is used to select the ref clk
  220. * for the pipe A/B. Display spec 1.06 has wrong definition.
  221. * Correct definition is like below:
  222. *
  223. * refclka mean use clock from same PLL
  224. *
  225. * if DPLLA sets 01 and DPLLB sets 01, they use clock from their pll
  226. *
  227. * if DPLLA sets 01 and DPLLB sets 02, both use clk from DPLLA
  228. *
  229. */
  230. ret = cdv_sb_read(dev, ref_sfr, &ref_value);
  231. if (ret)
  232. return ret;
  233. ref_value &= ~(REF_CLK_MASK);
  234. /* use DPLL_A for pipeB on CRT/HDMI */
  235. if (pipe == 1 && !is_lvds && !(ddi_select & DP_MASK)) {
  236. DRM_DEBUG_KMS("use DPLLA for pipe B\n");
  237. ref_value |= REF_CLK_DPLLA;
  238. } else {
  239. DRM_DEBUG_KMS("use their DPLL for pipe A/B\n");
  240. ref_value |= REF_CLK_DPLL;
  241. }
  242. ret = cdv_sb_write(dev, ref_sfr, ref_value);
  243. if (ret)
  244. return ret;
  245. ret = cdv_sb_read(dev, SB_M(pipe), &m);
  246. if (ret)
  247. return ret;
  248. m &= ~SB_M_DIVIDER_MASK;
  249. m |= ((clock->m2) << SB_M_DIVIDER_SHIFT);
  250. ret = cdv_sb_write(dev, SB_M(pipe), m);
  251. if (ret)
  252. return ret;
  253. ret = cdv_sb_read(dev, SB_N_VCO(pipe), &n_vco);
  254. if (ret)
  255. return ret;
  256. /* Follow the BIOS to program the N_DIVIDER REG */
  257. n_vco &= 0xFFFF;
  258. n_vco |= 0x107;
  259. n_vco &= ~(SB_N_VCO_SEL_MASK |
  260. SB_N_DIVIDER_MASK |
  261. SB_N_CB_TUNE_MASK);
  262. n_vco |= ((clock->n) << SB_N_DIVIDER_SHIFT);
  263. if (clock->vco < 2250000) {
  264. n_vco |= (2 << SB_N_CB_TUNE_SHIFT);
  265. n_vco |= (0 << SB_N_VCO_SEL_SHIFT);
  266. } else if (clock->vco < 2750000) {
  267. n_vco |= (1 << SB_N_CB_TUNE_SHIFT);
  268. n_vco |= (1 << SB_N_VCO_SEL_SHIFT);
  269. } else if (clock->vco < 3300000) {
  270. n_vco |= (0 << SB_N_CB_TUNE_SHIFT);
  271. n_vco |= (2 << SB_N_VCO_SEL_SHIFT);
  272. } else {
  273. n_vco |= (0 << SB_N_CB_TUNE_SHIFT);
  274. n_vco |= (3 << SB_N_VCO_SEL_SHIFT);
  275. }
  276. ret = cdv_sb_write(dev, SB_N_VCO(pipe), n_vco);
  277. if (ret)
  278. return ret;
  279. ret = cdv_sb_read(dev, SB_P(pipe), &p);
  280. if (ret)
  281. return ret;
  282. p &= ~(SB_P2_DIVIDER_MASK | SB_P1_DIVIDER_MASK);
  283. p |= SET_FIELD(clock->p1, SB_P1_DIVIDER);
  284. switch (clock->p2) {
  285. case 5:
  286. p |= SET_FIELD(SB_P2_5, SB_P2_DIVIDER);
  287. break;
  288. case 10:
  289. p |= SET_FIELD(SB_P2_10, SB_P2_DIVIDER);
  290. break;
  291. case 14:
  292. p |= SET_FIELD(SB_P2_14, SB_P2_DIVIDER);
  293. break;
  294. case 7:
  295. p |= SET_FIELD(SB_P2_7, SB_P2_DIVIDER);
  296. break;
  297. default:
  298. DRM_ERROR("Bad P2 clock: %d\n", clock->p2);
  299. return -EINVAL;
  300. }
  301. ret = cdv_sb_write(dev, SB_P(pipe), p);
  302. if (ret)
  303. return ret;
  304. if (ddi_select) {
  305. if ((ddi_select & DDI_MASK) == DDI0_SELECT) {
  306. lane_reg = PSB_LANE0;
  307. cdv_sb_read(dev, lane_reg, &lane_value);
  308. lane_value &= ~(LANE_PLL_MASK);
  309. lane_value |= LANE_PLL_ENABLE | LANE_PLL_PIPE(pipe);
  310. cdv_sb_write(dev, lane_reg, lane_value);
  311. lane_reg = PSB_LANE1;
  312. cdv_sb_read(dev, lane_reg, &lane_value);
  313. lane_value &= ~(LANE_PLL_MASK);
  314. lane_value |= LANE_PLL_ENABLE | LANE_PLL_PIPE(pipe);
  315. cdv_sb_write(dev, lane_reg, lane_value);
  316. } else {
  317. lane_reg = PSB_LANE2;
  318. cdv_sb_read(dev, lane_reg, &lane_value);
  319. lane_value &= ~(LANE_PLL_MASK);
  320. lane_value |= LANE_PLL_ENABLE | LANE_PLL_PIPE(pipe);
  321. cdv_sb_write(dev, lane_reg, lane_value);
  322. lane_reg = PSB_LANE3;
  323. cdv_sb_read(dev, lane_reg, &lane_value);
  324. lane_value &= ~(LANE_PLL_MASK);
  325. lane_value |= LANE_PLL_ENABLE | LANE_PLL_PIPE(pipe);
  326. cdv_sb_write(dev, lane_reg, lane_value);
  327. }
  328. }
  329. return 0;
  330. }
  331. static const struct gma_limit_t *cdv_intel_limit(struct drm_crtc *crtc,
  332. int refclk)
  333. {
  334. const struct gma_limit_t *limit;
  335. if (gma_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  336. /*
  337. * Now only single-channel LVDS is supported on CDV. If it is
  338. * incorrect, please add the dual-channel LVDS.
  339. */
  340. if (refclk == 96000)
  341. limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_96];
  342. else
  343. limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_100];
  344. } else if (gma_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
  345. gma_pipe_has_type(crtc, INTEL_OUTPUT_EDP)) {
  346. if (refclk == 27000)
  347. limit = &cdv_intel_limits[CDV_LIMIT_DP_27];
  348. else
  349. limit = &cdv_intel_limits[CDV_LIMIT_DP_100];
  350. } else {
  351. if (refclk == 27000)
  352. limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_27];
  353. else
  354. limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_96];
  355. }
  356. return limit;
  357. }
  358. /* m1 is reserved as 0 in CDV, n is a ring counter */
  359. static void cdv_intel_clock(int refclk, struct gma_clock_t *clock)
  360. {
  361. clock->m = clock->m2 + 2;
  362. clock->p = clock->p1 * clock->p2;
  363. clock->vco = (refclk * clock->m) / clock->n;
  364. clock->dot = clock->vco / clock->p;
  365. }
  366. static bool cdv_intel_find_dp_pll(const struct gma_limit_t *limit,
  367. struct drm_crtc *crtc, int target,
  368. int refclk,
  369. struct gma_clock_t *best_clock)
  370. {
  371. struct gma_clock_t clock;
  372. if (refclk == 27000) {
  373. if (target < 200000) {
  374. clock.p1 = 2;
  375. clock.p2 = 10;
  376. clock.n = 1;
  377. clock.m1 = 0;
  378. clock.m2 = 118;
  379. } else {
  380. clock.p1 = 1;
  381. clock.p2 = 10;
  382. clock.n = 1;
  383. clock.m1 = 0;
  384. clock.m2 = 98;
  385. }
  386. } else if (refclk == 100000) {
  387. if (target < 200000) {
  388. clock.p1 = 2;
  389. clock.p2 = 10;
  390. clock.n = 5;
  391. clock.m1 = 0;
  392. clock.m2 = 160;
  393. } else {
  394. clock.p1 = 1;
  395. clock.p2 = 10;
  396. clock.n = 5;
  397. clock.m1 = 0;
  398. clock.m2 = 133;
  399. }
  400. } else
  401. return false;
  402. clock.m = clock.m2 + 2;
  403. clock.p = clock.p1 * clock.p2;
  404. clock.vco = (refclk * clock.m) / clock.n;
  405. clock.dot = clock.vco / clock.p;
  406. memcpy(best_clock, &clock, sizeof(struct gma_clock_t));
  407. return true;
  408. }
  409. #define FIFO_PIPEA (1 << 0)
  410. #define FIFO_PIPEB (1 << 1)
  411. static bool cdv_intel_pipe_enabled(struct drm_device *dev, int pipe)
  412. {
  413. struct drm_crtc *crtc;
  414. struct drm_psb_private *dev_priv = dev->dev_private;
  415. struct psb_intel_crtc *psb_intel_crtc = NULL;
  416. crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  417. psb_intel_crtc = to_psb_intel_crtc(crtc);
  418. if (crtc->fb == NULL || !psb_intel_crtc->active)
  419. return false;
  420. return true;
  421. }
  422. static bool cdv_intel_single_pipe_active (struct drm_device *dev)
  423. {
  424. uint32_t pipe_enabled = 0;
  425. if (cdv_intel_pipe_enabled(dev, 0))
  426. pipe_enabled |= FIFO_PIPEA;
  427. if (cdv_intel_pipe_enabled(dev, 1))
  428. pipe_enabled |= FIFO_PIPEB;
  429. DRM_DEBUG_KMS("pipe enabled %x\n", pipe_enabled);
  430. if (pipe_enabled == FIFO_PIPEA || pipe_enabled == FIFO_PIPEB)
  431. return true;
  432. else
  433. return false;
  434. }
  435. static bool is_pipeb_lvds(struct drm_device *dev, struct drm_crtc *crtc)
  436. {
  437. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  438. struct drm_mode_config *mode_config = &dev->mode_config;
  439. struct drm_connector *connector;
  440. if (psb_intel_crtc->pipe != 1)
  441. return false;
  442. list_for_each_entry(connector, &mode_config->connector_list, head) {
  443. struct psb_intel_encoder *psb_intel_encoder =
  444. gma_attached_encoder(connector);
  445. if (!connector->encoder
  446. || connector->encoder->crtc != crtc)
  447. continue;
  448. if (psb_intel_encoder->type == INTEL_OUTPUT_LVDS)
  449. return true;
  450. }
  451. return false;
  452. }
  453. void cdv_intel_disable_self_refresh(struct drm_device *dev)
  454. {
  455. if (REG_READ(FW_BLC_SELF) & FW_BLC_SELF_EN) {
  456. /* Disable self-refresh before adjust WM */
  457. REG_WRITE(FW_BLC_SELF, (REG_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN));
  458. REG_READ(FW_BLC_SELF);
  459. gma_wait_for_vblank(dev);
  460. /* Cedarview workaround to write ovelay plane, which force to leave
  461. * MAX_FIFO state.
  462. */
  463. REG_WRITE(OV_OVADD, 0/*dev_priv->ovl_offset*/);
  464. REG_READ(OV_OVADD);
  465. gma_wait_for_vblank(dev);
  466. }
  467. }
  468. void cdv_intel_update_watermark(struct drm_device *dev, struct drm_crtc *crtc)
  469. {
  470. if (cdv_intel_single_pipe_active(dev)) {
  471. u32 fw;
  472. fw = REG_READ(DSPFW1);
  473. fw &= ~DSP_FIFO_SR_WM_MASK;
  474. fw |= (0x7e << DSP_FIFO_SR_WM_SHIFT);
  475. fw &= ~CURSOR_B_FIFO_WM_MASK;
  476. fw |= (0x4 << CURSOR_B_FIFO_WM_SHIFT);
  477. REG_WRITE(DSPFW1, fw);
  478. fw = REG_READ(DSPFW2);
  479. fw &= ~CURSOR_A_FIFO_WM_MASK;
  480. fw |= (0x6 << CURSOR_A_FIFO_WM_SHIFT);
  481. fw &= ~DSP_PLANE_C_FIFO_WM_MASK;
  482. fw |= (0x8 << DSP_PLANE_C_FIFO_WM_SHIFT);
  483. REG_WRITE(DSPFW2, fw);
  484. REG_WRITE(DSPFW3, 0x36000000);
  485. /* ignore FW4 */
  486. if (is_pipeb_lvds(dev, crtc)) {
  487. REG_WRITE(DSPFW5, 0x00040330);
  488. } else {
  489. fw = (3 << DSP_PLANE_B_FIFO_WM1_SHIFT) |
  490. (4 << DSP_PLANE_A_FIFO_WM1_SHIFT) |
  491. (3 << CURSOR_B_FIFO_WM1_SHIFT) |
  492. (4 << CURSOR_FIFO_SR_WM1_SHIFT);
  493. REG_WRITE(DSPFW5, fw);
  494. }
  495. REG_WRITE(DSPFW6, 0x10);
  496. gma_wait_for_vblank(dev);
  497. /* enable self-refresh for single pipe active */
  498. REG_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  499. REG_READ(FW_BLC_SELF);
  500. gma_wait_for_vblank(dev);
  501. } else {
  502. /* HW team suggested values... */
  503. REG_WRITE(DSPFW1, 0x3f880808);
  504. REG_WRITE(DSPFW2, 0x0b020202);
  505. REG_WRITE(DSPFW3, 0x24000000);
  506. REG_WRITE(DSPFW4, 0x08030202);
  507. REG_WRITE(DSPFW5, 0x01010101);
  508. REG_WRITE(DSPFW6, 0x1d0);
  509. gma_wait_for_vblank(dev);
  510. cdv_intel_disable_self_refresh(dev);
  511. }
  512. }
  513. /**
  514. * Return the pipe currently connected to the panel fitter,
  515. * or -1 if the panel fitter is not present or not in use
  516. */
  517. static int cdv_intel_panel_fitter_pipe(struct drm_device *dev)
  518. {
  519. u32 pfit_control;
  520. pfit_control = REG_READ(PFIT_CONTROL);
  521. /* See if the panel fitter is in use */
  522. if ((pfit_control & PFIT_ENABLE) == 0)
  523. return -1;
  524. return (pfit_control >> 29) & 0x3;
  525. }
  526. static int cdv_intel_crtc_mode_set(struct drm_crtc *crtc,
  527. struct drm_display_mode *mode,
  528. struct drm_display_mode *adjusted_mode,
  529. int x, int y,
  530. struct drm_framebuffer *old_fb)
  531. {
  532. struct drm_device *dev = crtc->dev;
  533. struct drm_psb_private *dev_priv = dev->dev_private;
  534. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  535. int pipe = psb_intel_crtc->pipe;
  536. const struct psb_offset *map = &dev_priv->regmap[pipe];
  537. int refclk;
  538. struct gma_clock_t clock;
  539. u32 dpll = 0, dspcntr, pipeconf;
  540. bool ok;
  541. bool is_crt = false, is_lvds = false, is_tv = false;
  542. bool is_hdmi = false, is_dp = false;
  543. struct drm_mode_config *mode_config = &dev->mode_config;
  544. struct drm_connector *connector;
  545. const struct gma_limit_t *limit;
  546. u32 ddi_select = 0;
  547. bool is_edp = false;
  548. list_for_each_entry(connector, &mode_config->connector_list, head) {
  549. struct psb_intel_encoder *psb_intel_encoder =
  550. gma_attached_encoder(connector);
  551. if (!connector->encoder
  552. || connector->encoder->crtc != crtc)
  553. continue;
  554. ddi_select = psb_intel_encoder->ddi_select;
  555. switch (psb_intel_encoder->type) {
  556. case INTEL_OUTPUT_LVDS:
  557. is_lvds = true;
  558. break;
  559. case INTEL_OUTPUT_TVOUT:
  560. is_tv = true;
  561. break;
  562. case INTEL_OUTPUT_ANALOG:
  563. is_crt = true;
  564. break;
  565. case INTEL_OUTPUT_HDMI:
  566. is_hdmi = true;
  567. break;
  568. case INTEL_OUTPUT_DISPLAYPORT:
  569. is_dp = true;
  570. break;
  571. case INTEL_OUTPUT_EDP:
  572. is_edp = true;
  573. break;
  574. default:
  575. DRM_ERROR("invalid output type.\n");
  576. return 0;
  577. }
  578. }
  579. if (dev_priv->dplla_96mhz)
  580. /* low-end sku, 96/100 mhz */
  581. refclk = 96000;
  582. else
  583. /* high-end sku, 27/100 mhz */
  584. refclk = 27000;
  585. if (is_dp || is_edp) {
  586. /*
  587. * Based on the spec the low-end SKU has only CRT/LVDS. So it is
  588. * unnecessary to consider it for DP/eDP.
  589. * On the high-end SKU, it will use the 27/100M reference clk
  590. * for DP/eDP. When using SSC clock, the ref clk is 100MHz.Otherwise
  591. * it will be 27MHz. From the VBIOS code it seems that the pipe A choose
  592. * 27MHz for DP/eDP while the Pipe B chooses the 100MHz.
  593. */
  594. if (pipe == 0)
  595. refclk = 27000;
  596. else
  597. refclk = 100000;
  598. }
  599. if (is_lvds && dev_priv->lvds_use_ssc) {
  600. refclk = dev_priv->lvds_ssc_freq * 1000;
  601. DRM_DEBUG_KMS("Use SSC reference clock %d Mhz\n", dev_priv->lvds_ssc_freq);
  602. }
  603. drm_mode_debug_printmodeline(adjusted_mode);
  604. limit = psb_intel_crtc->clock_funcs->limit(crtc, refclk);
  605. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
  606. &clock);
  607. if (!ok) {
  608. DRM_ERROR("Couldn't find PLL settings for mode! target: %d, actual: %d",
  609. adjusted_mode->clock, clock.dot);
  610. return 0;
  611. }
  612. dpll = DPLL_VGA_MODE_DIS;
  613. if (is_tv) {
  614. /* XXX: just matching BIOS for now */
  615. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  616. dpll |= 3;
  617. }
  618. /* dpll |= PLL_REF_INPUT_DREFCLK; */
  619. if (is_dp || is_edp) {
  620. cdv_intel_dp_set_m_n(crtc, mode, adjusted_mode);
  621. } else {
  622. REG_WRITE(PIPE_GMCH_DATA_M(pipe), 0);
  623. REG_WRITE(PIPE_GMCH_DATA_N(pipe), 0);
  624. REG_WRITE(PIPE_DP_LINK_M(pipe), 0);
  625. REG_WRITE(PIPE_DP_LINK_N(pipe), 0);
  626. }
  627. dpll |= DPLL_SYNCLOCK_ENABLE;
  628. /* if (is_lvds)
  629. dpll |= DPLLB_MODE_LVDS;
  630. else
  631. dpll |= DPLLB_MODE_DAC_SERIAL; */
  632. /* dpll |= (2 << 11); */
  633. /* setup pipeconf */
  634. pipeconf = REG_READ(map->conf);
  635. pipeconf &= ~(PIPE_BPC_MASK);
  636. if (is_edp) {
  637. switch (dev_priv->edp.bpp) {
  638. case 24:
  639. pipeconf |= PIPE_8BPC;
  640. break;
  641. case 18:
  642. pipeconf |= PIPE_6BPC;
  643. break;
  644. case 30:
  645. pipeconf |= PIPE_10BPC;
  646. break;
  647. default:
  648. pipeconf |= PIPE_8BPC;
  649. break;
  650. }
  651. } else if (is_lvds) {
  652. /* the BPC will be 6 if it is 18-bit LVDS panel */
  653. if ((REG_READ(LVDS) & LVDS_A3_POWER_MASK) == LVDS_A3_POWER_UP)
  654. pipeconf |= PIPE_8BPC;
  655. else
  656. pipeconf |= PIPE_6BPC;
  657. } else
  658. pipeconf |= PIPE_8BPC;
  659. /* Set up the display plane register */
  660. dspcntr = DISPPLANE_GAMMA_ENABLE;
  661. if (pipe == 0)
  662. dspcntr |= DISPPLANE_SEL_PIPE_A;
  663. else
  664. dspcntr |= DISPPLANE_SEL_PIPE_B;
  665. dspcntr |= DISPLAY_PLANE_ENABLE;
  666. pipeconf |= PIPEACONF_ENABLE;
  667. REG_WRITE(map->dpll, dpll | DPLL_VGA_MODE_DIS | DPLL_SYNCLOCK_ENABLE);
  668. REG_READ(map->dpll);
  669. cdv_dpll_set_clock_cdv(dev, crtc, &clock, is_lvds, ddi_select);
  670. udelay(150);
  671. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  672. * This is an exception to the general rule that mode_set doesn't turn
  673. * things on.
  674. */
  675. if (is_lvds) {
  676. u32 lvds = REG_READ(LVDS);
  677. lvds |=
  678. LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP |
  679. LVDS_PIPEB_SELECT;
  680. /* Set the B0-B3 data pairs corresponding to
  681. * whether we're going to
  682. * set the DPLLs for dual-channel mode or not.
  683. */
  684. if (clock.p2 == 7)
  685. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  686. else
  687. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  688. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  689. * appropriately here, but we need to look more
  690. * thoroughly into how panels behave in the two modes.
  691. */
  692. REG_WRITE(LVDS, lvds);
  693. REG_READ(LVDS);
  694. }
  695. dpll |= DPLL_VCO_ENABLE;
  696. /* Disable the panel fitter if it was on our pipe */
  697. if (cdv_intel_panel_fitter_pipe(dev) == pipe)
  698. REG_WRITE(PFIT_CONTROL, 0);
  699. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  700. drm_mode_debug_printmodeline(mode);
  701. REG_WRITE(map->dpll,
  702. (REG_READ(map->dpll) & ~DPLL_LOCK) | DPLL_VCO_ENABLE);
  703. REG_READ(map->dpll);
  704. /* Wait for the clocks to stabilize. */
  705. udelay(150); /* 42 usec w/o calibration, 110 with. rounded up. */
  706. if (!(REG_READ(map->dpll) & DPLL_LOCK)) {
  707. dev_err(dev->dev, "Failed to get DPLL lock\n");
  708. return -EBUSY;
  709. }
  710. {
  711. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  712. REG_WRITE(map->dpll_md, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) | ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  713. }
  714. REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
  715. ((adjusted_mode->crtc_htotal - 1) << 16));
  716. REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
  717. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  718. REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
  719. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  720. REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
  721. ((adjusted_mode->crtc_vtotal - 1) << 16));
  722. REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
  723. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  724. REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
  725. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  726. /* pipesrc and dspsize control the size that is scaled from,
  727. * which should always be the user's requested size.
  728. */
  729. REG_WRITE(map->size,
  730. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  731. REG_WRITE(map->pos, 0);
  732. REG_WRITE(map->src,
  733. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  734. REG_WRITE(map->conf, pipeconf);
  735. REG_READ(map->conf);
  736. gma_wait_for_vblank(dev);
  737. REG_WRITE(map->cntr, dspcntr);
  738. /* Flush the plane changes */
  739. {
  740. struct drm_crtc_helper_funcs *crtc_funcs =
  741. crtc->helper_private;
  742. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  743. }
  744. gma_wait_for_vblank(dev);
  745. return 0;
  746. }
  747. static int cdv_crtc_set_config(struct drm_mode_set *set)
  748. {
  749. int ret = 0;
  750. struct drm_device *dev = set->crtc->dev;
  751. struct drm_psb_private *dev_priv = dev->dev_private;
  752. if (!dev_priv->rpm_enabled)
  753. return drm_crtc_helper_set_config(set);
  754. pm_runtime_forbid(&dev->pdev->dev);
  755. ret = drm_crtc_helper_set_config(set);
  756. pm_runtime_allow(&dev->pdev->dev);
  757. return ret;
  758. }
  759. /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
  760. /* FIXME: why are we using this, should it be cdv_ in this tree ? */
  761. static void i8xx_clock(int refclk, struct gma_clock_t *clock)
  762. {
  763. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  764. clock->p = clock->p1 * clock->p2;
  765. clock->vco = refclk * clock->m / (clock->n + 2);
  766. clock->dot = clock->vco / clock->p;
  767. }
  768. /* Returns the clock of the currently programmed mode of the given pipe. */
  769. static int cdv_intel_crtc_clock_get(struct drm_device *dev,
  770. struct drm_crtc *crtc)
  771. {
  772. struct drm_psb_private *dev_priv = dev->dev_private;
  773. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  774. int pipe = psb_intel_crtc->pipe;
  775. const struct psb_offset *map = &dev_priv->regmap[pipe];
  776. u32 dpll;
  777. u32 fp;
  778. struct gma_clock_t clock;
  779. bool is_lvds;
  780. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  781. if (gma_power_begin(dev, false)) {
  782. dpll = REG_READ(map->dpll);
  783. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  784. fp = REG_READ(map->fp0);
  785. else
  786. fp = REG_READ(map->fp1);
  787. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  788. gma_power_end(dev);
  789. } else {
  790. dpll = p->dpll;
  791. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  792. fp = p->fp0;
  793. else
  794. fp = p->fp1;
  795. is_lvds = (pipe == 1) &&
  796. (dev_priv->regs.psb.saveLVDS & LVDS_PORT_EN);
  797. }
  798. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  799. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  800. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  801. if (is_lvds) {
  802. clock.p1 =
  803. ffs((dpll &
  804. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  805. DPLL_FPA01_P1_POST_DIV_SHIFT);
  806. if (clock.p1 == 0) {
  807. clock.p1 = 4;
  808. dev_err(dev->dev, "PLL %d\n", dpll);
  809. }
  810. clock.p2 = 14;
  811. if ((dpll & PLL_REF_INPUT_MASK) ==
  812. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  813. /* XXX: might not be 66MHz */
  814. i8xx_clock(66000, &clock);
  815. } else
  816. i8xx_clock(48000, &clock);
  817. } else {
  818. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  819. clock.p1 = 2;
  820. else {
  821. clock.p1 =
  822. ((dpll &
  823. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  824. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  825. }
  826. if (dpll & PLL_P2_DIVIDE_BY_4)
  827. clock.p2 = 4;
  828. else
  829. clock.p2 = 2;
  830. i8xx_clock(48000, &clock);
  831. }
  832. /* XXX: It would be nice to validate the clocks, but we can't reuse
  833. * i830PllIsValid() because it relies on the xf86_config connector
  834. * configuration being accurate, which it isn't necessarily.
  835. */
  836. return clock.dot;
  837. }
  838. /** Returns the currently programmed mode of the given pipe. */
  839. struct drm_display_mode *cdv_intel_crtc_mode_get(struct drm_device *dev,
  840. struct drm_crtc *crtc)
  841. {
  842. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  843. int pipe = psb_intel_crtc->pipe;
  844. struct drm_psb_private *dev_priv = dev->dev_private;
  845. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  846. const struct psb_offset *map = &dev_priv->regmap[pipe];
  847. struct drm_display_mode *mode;
  848. int htot;
  849. int hsync;
  850. int vtot;
  851. int vsync;
  852. if (gma_power_begin(dev, false)) {
  853. htot = REG_READ(map->htotal);
  854. hsync = REG_READ(map->hsync);
  855. vtot = REG_READ(map->vtotal);
  856. vsync = REG_READ(map->vsync);
  857. gma_power_end(dev);
  858. } else {
  859. htot = p->htotal;
  860. hsync = p->hsync;
  861. vtot = p->vtotal;
  862. vsync = p->vsync;
  863. }
  864. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  865. if (!mode)
  866. return NULL;
  867. mode->clock = cdv_intel_crtc_clock_get(dev, crtc);
  868. mode->hdisplay = (htot & 0xffff) + 1;
  869. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  870. mode->hsync_start = (hsync & 0xffff) + 1;
  871. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  872. mode->vdisplay = (vtot & 0xffff) + 1;
  873. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  874. mode->vsync_start = (vsync & 0xffff) + 1;
  875. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  876. drm_mode_set_name(mode);
  877. drm_mode_set_crtcinfo(mode, 0);
  878. return mode;
  879. }
  880. const struct drm_crtc_helper_funcs cdv_intel_helper_funcs = {
  881. .dpms = gma_crtc_dpms,
  882. .mode_fixup = gma_crtc_mode_fixup,
  883. .mode_set = cdv_intel_crtc_mode_set,
  884. .mode_set_base = gma_pipe_set_base,
  885. .prepare = gma_crtc_prepare,
  886. .commit = gma_crtc_commit,
  887. .disable = gma_crtc_disable,
  888. };
  889. const struct drm_crtc_funcs cdv_intel_crtc_funcs = {
  890. .save = gma_crtc_save,
  891. .restore = gma_crtc_restore,
  892. .cursor_set = gma_crtc_cursor_set,
  893. .cursor_move = gma_crtc_cursor_move,
  894. .gamma_set = gma_crtc_gamma_set,
  895. .set_config = cdv_crtc_set_config,
  896. .destroy = gma_crtc_destroy,
  897. };
  898. const struct gma_clock_funcs cdv_clock_funcs = {
  899. .clock = cdv_intel_clock,
  900. .limit = cdv_intel_limit,
  901. .pll_is_valid = gma_pll_is_valid,
  902. };