cdv_intel_display.c 25 KB

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