cdv_intel_display.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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. psb_intel_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. psb_intel_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. /**
  748. * Save HW states of giving crtc
  749. */
  750. static void cdv_intel_crtc_save(struct drm_crtc *crtc)
  751. {
  752. struct drm_device *dev = crtc->dev;
  753. struct drm_psb_private *dev_priv = dev->dev_private;
  754. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  755. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  756. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  757. uint32_t paletteReg;
  758. int i;
  759. if (!crtc_state) {
  760. dev_dbg(dev->dev, "No CRTC state found\n");
  761. return;
  762. }
  763. crtc_state->saveDSPCNTR = REG_READ(map->cntr);
  764. crtc_state->savePIPECONF = REG_READ(map->conf);
  765. crtc_state->savePIPESRC = REG_READ(map->src);
  766. crtc_state->saveFP0 = REG_READ(map->fp0);
  767. crtc_state->saveFP1 = REG_READ(map->fp1);
  768. crtc_state->saveDPLL = REG_READ(map->dpll);
  769. crtc_state->saveHTOTAL = REG_READ(map->htotal);
  770. crtc_state->saveHBLANK = REG_READ(map->hblank);
  771. crtc_state->saveHSYNC = REG_READ(map->hsync);
  772. crtc_state->saveVTOTAL = REG_READ(map->vtotal);
  773. crtc_state->saveVBLANK = REG_READ(map->vblank);
  774. crtc_state->saveVSYNC = REG_READ(map->vsync);
  775. crtc_state->saveDSPSTRIDE = REG_READ(map->stride);
  776. /*NOTE: DSPSIZE DSPPOS only for psb*/
  777. crtc_state->saveDSPSIZE = REG_READ(map->size);
  778. crtc_state->saveDSPPOS = REG_READ(map->pos);
  779. crtc_state->saveDSPBASE = REG_READ(map->base);
  780. DRM_DEBUG("(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  781. crtc_state->saveDSPCNTR,
  782. crtc_state->savePIPECONF,
  783. crtc_state->savePIPESRC,
  784. crtc_state->saveFP0,
  785. crtc_state->saveFP1,
  786. crtc_state->saveDPLL,
  787. crtc_state->saveHTOTAL,
  788. crtc_state->saveHBLANK,
  789. crtc_state->saveHSYNC,
  790. crtc_state->saveVTOTAL,
  791. crtc_state->saveVBLANK,
  792. crtc_state->saveVSYNC,
  793. crtc_state->saveDSPSTRIDE,
  794. crtc_state->saveDSPSIZE,
  795. crtc_state->saveDSPPOS,
  796. crtc_state->saveDSPBASE
  797. );
  798. paletteReg = map->palette;
  799. for (i = 0; i < 256; ++i)
  800. crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
  801. }
  802. /**
  803. * Restore HW states of giving crtc
  804. */
  805. static void cdv_intel_crtc_restore(struct drm_crtc *crtc)
  806. {
  807. struct drm_device *dev = crtc->dev;
  808. struct drm_psb_private *dev_priv = dev->dev_private;
  809. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  810. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  811. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  812. uint32_t paletteReg;
  813. int i;
  814. if (!crtc_state) {
  815. dev_dbg(dev->dev, "No crtc state\n");
  816. return;
  817. }
  818. DRM_DEBUG(
  819. "current:(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  820. REG_READ(map->cntr),
  821. REG_READ(map->conf),
  822. REG_READ(map->src),
  823. REG_READ(map->fp0),
  824. REG_READ(map->fp1),
  825. REG_READ(map->dpll),
  826. REG_READ(map->htotal),
  827. REG_READ(map->hblank),
  828. REG_READ(map->hsync),
  829. REG_READ(map->vtotal),
  830. REG_READ(map->vblank),
  831. REG_READ(map->vsync),
  832. REG_READ(map->stride),
  833. REG_READ(map->size),
  834. REG_READ(map->pos),
  835. REG_READ(map->base)
  836. );
  837. DRM_DEBUG(
  838. "saved: (%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  839. crtc_state->saveDSPCNTR,
  840. crtc_state->savePIPECONF,
  841. crtc_state->savePIPESRC,
  842. crtc_state->saveFP0,
  843. crtc_state->saveFP1,
  844. crtc_state->saveDPLL,
  845. crtc_state->saveHTOTAL,
  846. crtc_state->saveHBLANK,
  847. crtc_state->saveHSYNC,
  848. crtc_state->saveVTOTAL,
  849. crtc_state->saveVBLANK,
  850. crtc_state->saveVSYNC,
  851. crtc_state->saveDSPSTRIDE,
  852. crtc_state->saveDSPSIZE,
  853. crtc_state->saveDSPPOS,
  854. crtc_state->saveDSPBASE
  855. );
  856. if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
  857. REG_WRITE(map->dpll,
  858. crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
  859. REG_READ(map->dpll);
  860. DRM_DEBUG("write dpll: %x\n",
  861. REG_READ(map->dpll));
  862. udelay(150);
  863. }
  864. REG_WRITE(map->fp0, crtc_state->saveFP0);
  865. REG_READ(map->fp0);
  866. REG_WRITE(map->fp1, crtc_state->saveFP1);
  867. REG_READ(map->fp1);
  868. REG_WRITE(map->dpll, crtc_state->saveDPLL);
  869. REG_READ(map->dpll);
  870. udelay(150);
  871. REG_WRITE(map->htotal, crtc_state->saveHTOTAL);
  872. REG_WRITE(map->hblank, crtc_state->saveHBLANK);
  873. REG_WRITE(map->hsync, crtc_state->saveHSYNC);
  874. REG_WRITE(map->vtotal, crtc_state->saveVTOTAL);
  875. REG_WRITE(map->vblank, crtc_state->saveVBLANK);
  876. REG_WRITE(map->vsync, crtc_state->saveVSYNC);
  877. REG_WRITE(map->stride, crtc_state->saveDSPSTRIDE);
  878. REG_WRITE(map->size, crtc_state->saveDSPSIZE);
  879. REG_WRITE(map->pos, crtc_state->saveDSPPOS);
  880. REG_WRITE(map->src, crtc_state->savePIPESRC);
  881. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  882. REG_WRITE(map->conf, crtc_state->savePIPECONF);
  883. gma_wait_for_vblank(dev);
  884. REG_WRITE(map->cntr, crtc_state->saveDSPCNTR);
  885. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  886. gma_wait_for_vblank(dev);
  887. paletteReg = map->palette;
  888. for (i = 0; i < 256; ++i)
  889. REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
  890. }
  891. static int cdv_intel_crtc_cursor_set(struct drm_crtc *crtc,
  892. struct drm_file *file_priv,
  893. uint32_t handle,
  894. uint32_t width, uint32_t height)
  895. {
  896. struct drm_device *dev = crtc->dev;
  897. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  898. int pipe = psb_intel_crtc->pipe;
  899. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  900. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  901. uint32_t temp;
  902. size_t addr = 0;
  903. struct gtt_range *gt;
  904. struct drm_gem_object *obj;
  905. int ret = 0;
  906. /* if we want to turn of the cursor ignore width and height */
  907. if (!handle) {
  908. /* turn off the cursor */
  909. temp = CURSOR_MODE_DISABLE;
  910. if (gma_power_begin(dev, false)) {
  911. REG_WRITE(control, temp);
  912. REG_WRITE(base, 0);
  913. gma_power_end(dev);
  914. }
  915. /* unpin the old GEM object */
  916. if (psb_intel_crtc->cursor_obj) {
  917. gt = container_of(psb_intel_crtc->cursor_obj,
  918. struct gtt_range, gem);
  919. psb_gtt_unpin(gt);
  920. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  921. psb_intel_crtc->cursor_obj = NULL;
  922. }
  923. return 0;
  924. }
  925. /* Currently we only support 64x64 cursors */
  926. if (width != 64 || height != 64) {
  927. dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
  928. return -EINVAL;
  929. }
  930. obj = drm_gem_object_lookup(dev, file_priv, handle);
  931. if (!obj)
  932. return -ENOENT;
  933. if (obj->size < width * height * 4) {
  934. dev_dbg(dev->dev, "buffer is to small\n");
  935. ret = -ENOMEM;
  936. goto unref_cursor;
  937. }
  938. gt = container_of(obj, struct gtt_range, gem);
  939. /* Pin the memory into the GTT */
  940. ret = psb_gtt_pin(gt);
  941. if (ret) {
  942. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  943. goto unref_cursor;
  944. }
  945. addr = gt->offset; /* Or resource.start ??? */
  946. psb_intel_crtc->cursor_addr = addr;
  947. temp = 0;
  948. /* set the pipe for the cursor */
  949. temp |= (pipe << 28);
  950. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  951. if (gma_power_begin(dev, false)) {
  952. REG_WRITE(control, temp);
  953. REG_WRITE(base, addr);
  954. gma_power_end(dev);
  955. }
  956. /* unpin the old GEM object */
  957. if (psb_intel_crtc->cursor_obj) {
  958. gt = container_of(psb_intel_crtc->cursor_obj,
  959. struct gtt_range, gem);
  960. psb_gtt_unpin(gt);
  961. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  962. }
  963. psb_intel_crtc->cursor_obj = obj;
  964. return ret;
  965. unref_cursor:
  966. drm_gem_object_unreference(obj);
  967. return ret;
  968. }
  969. static int cdv_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  970. {
  971. struct drm_device *dev = crtc->dev;
  972. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  973. int pipe = psb_intel_crtc->pipe;
  974. uint32_t temp = 0;
  975. uint32_t adder;
  976. if (x < 0) {
  977. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  978. x = -x;
  979. }
  980. if (y < 0) {
  981. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  982. y = -y;
  983. }
  984. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  985. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  986. adder = psb_intel_crtc->cursor_addr;
  987. if (gma_power_begin(dev, false)) {
  988. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  989. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  990. gma_power_end(dev);
  991. }
  992. return 0;
  993. }
  994. static int cdv_crtc_set_config(struct drm_mode_set *set)
  995. {
  996. int ret = 0;
  997. struct drm_device *dev = set->crtc->dev;
  998. struct drm_psb_private *dev_priv = dev->dev_private;
  999. if (!dev_priv->rpm_enabled)
  1000. return drm_crtc_helper_set_config(set);
  1001. pm_runtime_forbid(&dev->pdev->dev);
  1002. ret = drm_crtc_helper_set_config(set);
  1003. pm_runtime_allow(&dev->pdev->dev);
  1004. return ret;
  1005. }
  1006. /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
  1007. /* FIXME: why are we using this, should it be cdv_ in this tree ? */
  1008. static void i8xx_clock(int refclk, struct gma_clock_t *clock)
  1009. {
  1010. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  1011. clock->p = clock->p1 * clock->p2;
  1012. clock->vco = refclk * clock->m / (clock->n + 2);
  1013. clock->dot = clock->vco / clock->p;
  1014. }
  1015. /* Returns the clock of the currently programmed mode of the given pipe. */
  1016. static int cdv_intel_crtc_clock_get(struct drm_device *dev,
  1017. struct drm_crtc *crtc)
  1018. {
  1019. struct drm_psb_private *dev_priv = dev->dev_private;
  1020. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1021. int pipe = psb_intel_crtc->pipe;
  1022. const struct psb_offset *map = &dev_priv->regmap[pipe];
  1023. u32 dpll;
  1024. u32 fp;
  1025. struct gma_clock_t clock;
  1026. bool is_lvds;
  1027. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  1028. if (gma_power_begin(dev, false)) {
  1029. dpll = REG_READ(map->dpll);
  1030. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1031. fp = REG_READ(map->fp0);
  1032. else
  1033. fp = REG_READ(map->fp1);
  1034. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  1035. gma_power_end(dev);
  1036. } else {
  1037. dpll = p->dpll;
  1038. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1039. fp = p->fp0;
  1040. else
  1041. fp = p->fp1;
  1042. is_lvds = (pipe == 1) &&
  1043. (dev_priv->regs.psb.saveLVDS & LVDS_PORT_EN);
  1044. }
  1045. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1046. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1047. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1048. if (is_lvds) {
  1049. clock.p1 =
  1050. ffs((dpll &
  1051. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1052. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1053. if (clock.p1 == 0) {
  1054. clock.p1 = 4;
  1055. dev_err(dev->dev, "PLL %d\n", dpll);
  1056. }
  1057. clock.p2 = 14;
  1058. if ((dpll & PLL_REF_INPUT_MASK) ==
  1059. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1060. /* XXX: might not be 66MHz */
  1061. i8xx_clock(66000, &clock);
  1062. } else
  1063. i8xx_clock(48000, &clock);
  1064. } else {
  1065. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1066. clock.p1 = 2;
  1067. else {
  1068. clock.p1 =
  1069. ((dpll &
  1070. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1071. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1072. }
  1073. if (dpll & PLL_P2_DIVIDE_BY_4)
  1074. clock.p2 = 4;
  1075. else
  1076. clock.p2 = 2;
  1077. i8xx_clock(48000, &clock);
  1078. }
  1079. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1080. * i830PllIsValid() because it relies on the xf86_config connector
  1081. * configuration being accurate, which it isn't necessarily.
  1082. */
  1083. return clock.dot;
  1084. }
  1085. /** Returns the currently programmed mode of the given pipe. */
  1086. struct drm_display_mode *cdv_intel_crtc_mode_get(struct drm_device *dev,
  1087. struct drm_crtc *crtc)
  1088. {
  1089. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1090. int pipe = psb_intel_crtc->pipe;
  1091. struct drm_psb_private *dev_priv = dev->dev_private;
  1092. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  1093. const struct psb_offset *map = &dev_priv->regmap[pipe];
  1094. struct drm_display_mode *mode;
  1095. int htot;
  1096. int hsync;
  1097. int vtot;
  1098. int vsync;
  1099. if (gma_power_begin(dev, false)) {
  1100. htot = REG_READ(map->htotal);
  1101. hsync = REG_READ(map->hsync);
  1102. vtot = REG_READ(map->vtotal);
  1103. vsync = REG_READ(map->vsync);
  1104. gma_power_end(dev);
  1105. } else {
  1106. htot = p->htotal;
  1107. hsync = p->hsync;
  1108. vtot = p->vtotal;
  1109. vsync = p->vsync;
  1110. }
  1111. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1112. if (!mode)
  1113. return NULL;
  1114. mode->clock = cdv_intel_crtc_clock_get(dev, crtc);
  1115. mode->hdisplay = (htot & 0xffff) + 1;
  1116. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1117. mode->hsync_start = (hsync & 0xffff) + 1;
  1118. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1119. mode->vdisplay = (vtot & 0xffff) + 1;
  1120. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1121. mode->vsync_start = (vsync & 0xffff) + 1;
  1122. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1123. drm_mode_set_name(mode);
  1124. drm_mode_set_crtcinfo(mode, 0);
  1125. return mode;
  1126. }
  1127. const struct drm_crtc_helper_funcs cdv_intel_helper_funcs = {
  1128. .dpms = gma_crtc_dpms,
  1129. .mode_fixup = gma_crtc_mode_fixup,
  1130. .mode_set = cdv_intel_crtc_mode_set,
  1131. .mode_set_base = gma_pipe_set_base,
  1132. .prepare = gma_crtc_prepare,
  1133. .commit = gma_crtc_commit,
  1134. .disable = gma_crtc_disable,
  1135. };
  1136. const struct drm_crtc_funcs cdv_intel_crtc_funcs = {
  1137. .save = cdv_intel_crtc_save,
  1138. .restore = cdv_intel_crtc_restore,
  1139. .cursor_set = cdv_intel_crtc_cursor_set,
  1140. .cursor_move = cdv_intel_crtc_cursor_move,
  1141. .gamma_set = gma_crtc_gamma_set,
  1142. .set_config = cdv_crtc_set_config,
  1143. .destroy = gma_crtc_destroy,
  1144. };
  1145. const struct gma_clock_funcs cdv_clock_funcs = {
  1146. .clock = cdv_intel_clock,
  1147. .limit = cdv_intel_limit,
  1148. .pll_is_valid = gma_pll_is_valid,
  1149. };