cdv_intel_display.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  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 "psb_intel_display.h"
  28. #include "power.h"
  29. #include "cdv_device.h"
  30. struct cdv_intel_range_t {
  31. int min, max;
  32. };
  33. struct cdv_intel_p2_t {
  34. int dot_limit;
  35. int p2_slow, p2_fast;
  36. };
  37. struct cdv_intel_clock_t {
  38. /* given values */
  39. int n;
  40. int m1, m2;
  41. int p1, p2;
  42. /* derived values */
  43. int dot;
  44. int vco;
  45. int m;
  46. int p;
  47. };
  48. #define INTEL_P2_NUM 2
  49. struct cdv_intel_limit_t {
  50. struct cdv_intel_range_t dot, vco, n, m, m1, m2, p, p1;
  51. struct cdv_intel_p2_t p2;
  52. };
  53. #define CDV_LIMIT_SINGLE_LVDS_96 0
  54. #define CDV_LIMIT_SINGLE_LVDS_100 1
  55. #define CDV_LIMIT_DAC_HDMI_27 2
  56. #define CDV_LIMIT_DAC_HDMI_96 3
  57. static const struct cdv_intel_limit_t cdv_intel_limits[] = {
  58. { /* CDV_SIGNLE_LVDS_96MHz */
  59. .dot = {.min = 20000, .max = 115500},
  60. .vco = {.min = 1800000, .max = 3600000},
  61. .n = {.min = 2, .max = 6},
  62. .m = {.min = 60, .max = 160},
  63. .m1 = {.min = 0, .max = 0},
  64. .m2 = {.min = 58, .max = 158},
  65. .p = {.min = 28, .max = 140},
  66. .p1 = {.min = 2, .max = 10},
  67. .p2 = {.dot_limit = 200000,
  68. .p2_slow = 14, .p2_fast = 14},
  69. },
  70. { /* CDV_SINGLE_LVDS_100MHz */
  71. .dot = {.min = 20000, .max = 115500},
  72. .vco = {.min = 1800000, .max = 3600000},
  73. .n = {.min = 2, .max = 6},
  74. .m = {.min = 60, .max = 160},
  75. .m1 = {.min = 0, .max = 0},
  76. .m2 = {.min = 58, .max = 158},
  77. .p = {.min = 28, .max = 140},
  78. .p1 = {.min = 2, .max = 10},
  79. /* The single-channel range is 25-112Mhz, and dual-channel
  80. * is 80-224Mhz. Prefer single channel as much as possible.
  81. */
  82. .p2 = {.dot_limit = 200000, .p2_slow = 14, .p2_fast = 14},
  83. },
  84. { /* CDV_DAC_HDMI_27MHz */
  85. .dot = {.min = 20000, .max = 400000},
  86. .vco = {.min = 1809000, .max = 3564000},
  87. .n = {.min = 1, .max = 1},
  88. .m = {.min = 67, .max = 132},
  89. .m1 = {.min = 0, .max = 0},
  90. .m2 = {.min = 65, .max = 130},
  91. .p = {.min = 5, .max = 90},
  92. .p1 = {.min = 1, .max = 9},
  93. .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
  94. },
  95. { /* CDV_DAC_HDMI_96MHz */
  96. .dot = {.min = 20000, .max = 400000},
  97. .vco = {.min = 1800000, .max = 3600000},
  98. .n = {.min = 2, .max = 6},
  99. .m = {.min = 60, .max = 160},
  100. .m1 = {.min = 0, .max = 0},
  101. .m2 = {.min = 58, .max = 158},
  102. .p = {.min = 5, .max = 100},
  103. .p1 = {.min = 1, .max = 10},
  104. .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
  105. },
  106. };
  107. #define _wait_for(COND, MS, W) ({ \
  108. unsigned long timeout__ = jiffies + msecs_to_jiffies(MS); \
  109. int ret__ = 0; \
  110. while (!(COND)) { \
  111. if (time_after(jiffies, timeout__)) { \
  112. ret__ = -ETIMEDOUT; \
  113. break; \
  114. } \
  115. if (W && !in_dbg_master()) \
  116. msleep(W); \
  117. } \
  118. ret__; \
  119. })
  120. #define wait_for(COND, MS) _wait_for(COND, MS, 1)
  121. static int cdv_sb_read(struct drm_device *dev, u32 reg, u32 *val)
  122. {
  123. int ret;
  124. ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
  125. if (ret) {
  126. DRM_ERROR("timeout waiting for SB to idle before read\n");
  127. return ret;
  128. }
  129. REG_WRITE(SB_ADDR, reg);
  130. REG_WRITE(SB_PCKT,
  131. SET_FIELD(SB_OPCODE_READ, SB_OPCODE) |
  132. SET_FIELD(SB_DEST_DPLL, SB_DEST) |
  133. SET_FIELD(0xf, SB_BYTE_ENABLE));
  134. ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
  135. if (ret) {
  136. DRM_ERROR("timeout waiting for SB to idle after read\n");
  137. return ret;
  138. }
  139. *val = REG_READ(SB_DATA);
  140. return 0;
  141. }
  142. static int cdv_sb_write(struct drm_device *dev, u32 reg, u32 val)
  143. {
  144. int ret;
  145. static bool dpio_debug = true;
  146. u32 temp;
  147. if (dpio_debug) {
  148. if (cdv_sb_read(dev, reg, &temp) == 0)
  149. DRM_DEBUG_KMS("0x%08x: 0x%08x (before)\n", reg, temp);
  150. DRM_DEBUG_KMS("0x%08x: 0x%08x\n", reg, val);
  151. }
  152. ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
  153. if (ret) {
  154. DRM_ERROR("timeout waiting for SB to idle before write\n");
  155. return ret;
  156. }
  157. REG_WRITE(SB_ADDR, reg);
  158. REG_WRITE(SB_DATA, val);
  159. REG_WRITE(SB_PCKT,
  160. SET_FIELD(SB_OPCODE_WRITE, SB_OPCODE) |
  161. SET_FIELD(SB_DEST_DPLL, SB_DEST) |
  162. SET_FIELD(0xf, SB_BYTE_ENABLE));
  163. ret = wait_for((REG_READ(SB_PCKT) & SB_BUSY) == 0, 1000);
  164. if (ret) {
  165. DRM_ERROR("timeout waiting for SB to idle after write\n");
  166. return ret;
  167. }
  168. if (dpio_debug) {
  169. if (cdv_sb_read(dev, reg, &temp) == 0)
  170. DRM_DEBUG_KMS("0x%08x: 0x%08x (after)\n", reg, temp);
  171. }
  172. return 0;
  173. }
  174. /* Reset the DPIO configuration register. The BIOS does this at every
  175. * mode set.
  176. */
  177. static void cdv_sb_reset(struct drm_device *dev)
  178. {
  179. REG_WRITE(DPIO_CFG, 0);
  180. REG_READ(DPIO_CFG);
  181. REG_WRITE(DPIO_CFG, DPIO_MODE_SELECT_0 | DPIO_CMN_RESET_N);
  182. }
  183. /* Unlike most Intel display engines, on Cedarview the DPLL registers
  184. * are behind this sideband bus. They must be programmed while the
  185. * DPLL reference clock is on in the DPLL control register, but before
  186. * the DPLL is enabled in the DPLL control register.
  187. */
  188. static int
  189. cdv_dpll_set_clock_cdv(struct drm_device *dev, struct drm_crtc *crtc,
  190. struct cdv_intel_clock_t *clock)
  191. {
  192. struct psb_intel_crtc *psb_crtc =
  193. to_psb_intel_crtc(crtc);
  194. int pipe = psb_crtc->pipe;
  195. u32 m, n_vco, p;
  196. int ret = 0;
  197. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  198. u32 ref_value;
  199. cdv_sb_reset(dev);
  200. if ((REG_READ(dpll_reg) & DPLL_SYNCLOCK_ENABLE) == 0) {
  201. DRM_ERROR("Attempting to set DPLL with refclk disabled\n");
  202. return -EBUSY;
  203. }
  204. /* Follow the BIOS and write the REF/SFR Register. Hardcoded value */
  205. ref_value = 0x68A701;
  206. cdv_sb_write(dev, SB_REF_SFR(pipe), ref_value);
  207. /* We don't know what the other fields of these regs are, so
  208. * leave them in place.
  209. */
  210. ret = cdv_sb_read(dev, SB_M(pipe), &m);
  211. if (ret)
  212. return ret;
  213. m &= ~SB_M_DIVIDER_MASK;
  214. m |= ((clock->m2) << SB_M_DIVIDER_SHIFT);
  215. ret = cdv_sb_write(dev, SB_M(pipe), m);
  216. if (ret)
  217. return ret;
  218. ret = cdv_sb_read(dev, SB_N_VCO(pipe), &n_vco);
  219. if (ret)
  220. return ret;
  221. /* Follow the BIOS to program the N_DIVIDER REG */
  222. n_vco &= 0xFFFF;
  223. n_vco |= 0x107;
  224. n_vco &= ~(SB_N_VCO_SEL_MASK |
  225. SB_N_DIVIDER_MASK |
  226. SB_N_CB_TUNE_MASK);
  227. n_vco |= ((clock->n) << SB_N_DIVIDER_SHIFT);
  228. if (clock->vco < 2250000) {
  229. n_vco |= (2 << SB_N_CB_TUNE_SHIFT);
  230. n_vco |= (0 << SB_N_VCO_SEL_SHIFT);
  231. } else if (clock->vco < 2750000) {
  232. n_vco |= (1 << SB_N_CB_TUNE_SHIFT);
  233. n_vco |= (1 << SB_N_VCO_SEL_SHIFT);
  234. } else if (clock->vco < 3300000) {
  235. n_vco |= (0 << SB_N_CB_TUNE_SHIFT);
  236. n_vco |= (2 << SB_N_VCO_SEL_SHIFT);
  237. } else {
  238. n_vco |= (0 << SB_N_CB_TUNE_SHIFT);
  239. n_vco |= (3 << SB_N_VCO_SEL_SHIFT);
  240. }
  241. ret = cdv_sb_write(dev, SB_N_VCO(pipe), n_vco);
  242. if (ret)
  243. return ret;
  244. ret = cdv_sb_read(dev, SB_P(pipe), &p);
  245. if (ret)
  246. return ret;
  247. p &= ~(SB_P2_DIVIDER_MASK | SB_P1_DIVIDER_MASK);
  248. p |= SET_FIELD(clock->p1, SB_P1_DIVIDER);
  249. switch (clock->p2) {
  250. case 5:
  251. p |= SET_FIELD(SB_P2_5, SB_P2_DIVIDER);
  252. break;
  253. case 10:
  254. p |= SET_FIELD(SB_P2_10, SB_P2_DIVIDER);
  255. break;
  256. case 14:
  257. p |= SET_FIELD(SB_P2_14, SB_P2_DIVIDER);
  258. break;
  259. case 7:
  260. p |= SET_FIELD(SB_P2_7, SB_P2_DIVIDER);
  261. break;
  262. default:
  263. DRM_ERROR("Bad P2 clock: %d\n", clock->p2);
  264. return -EINVAL;
  265. }
  266. ret = cdv_sb_write(dev, SB_P(pipe), p);
  267. if (ret)
  268. return ret;
  269. /* always Program the Lane Register for the Pipe A*/
  270. if (pipe == 0) {
  271. /* Program the Lane0/1 for HDMI B */
  272. u32 lane_reg, lane_value;
  273. lane_reg = PSB_LANE0;
  274. cdv_sb_read(dev, lane_reg, &lane_value);
  275. lane_value &= ~(LANE_PLL_MASK);
  276. lane_value |= LANE_PLL_ENABLE;
  277. cdv_sb_write(dev, lane_reg, lane_value);
  278. lane_reg = PSB_LANE1;
  279. cdv_sb_read(dev, lane_reg, &lane_value);
  280. lane_value &= ~(LANE_PLL_MASK);
  281. lane_value |= LANE_PLL_ENABLE;
  282. cdv_sb_write(dev, lane_reg, lane_value);
  283. /* Program the Lane2/3 for HDMI C */
  284. lane_reg = PSB_LANE2;
  285. cdv_sb_read(dev, lane_reg, &lane_value);
  286. lane_value &= ~(LANE_PLL_MASK);
  287. lane_value |= LANE_PLL_ENABLE;
  288. cdv_sb_write(dev, lane_reg, lane_value);
  289. lane_reg = PSB_LANE3;
  290. cdv_sb_read(dev, lane_reg, &lane_value);
  291. lane_value &= ~(LANE_PLL_MASK);
  292. lane_value |= LANE_PLL_ENABLE;
  293. cdv_sb_write(dev, lane_reg, lane_value);
  294. }
  295. return 0;
  296. }
  297. /*
  298. * Returns whether any encoder on the specified pipe is of the specified type
  299. */
  300. static bool cdv_intel_pipe_has_type(struct drm_crtc *crtc, int type)
  301. {
  302. struct drm_device *dev = crtc->dev;
  303. struct drm_mode_config *mode_config = &dev->mode_config;
  304. struct drm_connector *l_entry;
  305. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  306. if (l_entry->encoder && l_entry->encoder->crtc == crtc) {
  307. struct psb_intel_encoder *psb_intel_encoder =
  308. psb_intel_attached_encoder(l_entry);
  309. if (psb_intel_encoder->type == type)
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. static const struct cdv_intel_limit_t *cdv_intel_limit(struct drm_crtc *crtc,
  316. int refclk)
  317. {
  318. const struct cdv_intel_limit_t *limit;
  319. if (cdv_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  320. /*
  321. * Now only single-channel LVDS is supported on CDV. If it is
  322. * incorrect, please add the dual-channel LVDS.
  323. */
  324. if (refclk == 96000)
  325. limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_96];
  326. else
  327. limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_100];
  328. } else {
  329. if (refclk == 27000)
  330. limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_27];
  331. else
  332. limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_96];
  333. }
  334. return limit;
  335. }
  336. /* m1 is reserved as 0 in CDV, n is a ring counter */
  337. static void cdv_intel_clock(struct drm_device *dev,
  338. int refclk, struct cdv_intel_clock_t *clock)
  339. {
  340. clock->m = clock->m2 + 2;
  341. clock->p = clock->p1 * clock->p2;
  342. clock->vco = (refclk * clock->m) / clock->n;
  343. clock->dot = clock->vco / clock->p;
  344. }
  345. #define INTELPllInvalid(s) { /* ErrorF (s) */; return false; }
  346. static bool cdv_intel_PLL_is_valid(struct drm_crtc *crtc,
  347. const struct cdv_intel_limit_t *limit,
  348. struct cdv_intel_clock_t *clock)
  349. {
  350. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  351. INTELPllInvalid("p1 out of range\n");
  352. if (clock->p < limit->p.min || limit->p.max < clock->p)
  353. INTELPllInvalid("p out of range\n");
  354. /* unnecessary to check the range of m(m1/M2)/n again */
  355. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  356. INTELPllInvalid("vco out of range\n");
  357. /* XXX: We may need to be checking "Dot clock"
  358. * depending on the multiplier, connector, etc.,
  359. * rather than just a single range.
  360. */
  361. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  362. INTELPllInvalid("dot out of range\n");
  363. return true;
  364. }
  365. static bool cdv_intel_find_best_PLL(struct drm_crtc *crtc, int target,
  366. int refclk,
  367. struct cdv_intel_clock_t *best_clock)
  368. {
  369. struct drm_device *dev = crtc->dev;
  370. struct cdv_intel_clock_t clock;
  371. const struct cdv_intel_limit_t *limit = cdv_intel_limit(crtc, refclk);
  372. int err = target;
  373. if (cdv_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  374. (REG_READ(LVDS) & LVDS_PORT_EN) != 0) {
  375. /*
  376. * For LVDS, if the panel is on, just rely on its current
  377. * settings for dual-channel. We haven't figured out how to
  378. * reliably set up different single/dual channel state, if we
  379. * even can.
  380. */
  381. if ((REG_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  382. LVDS_CLKB_POWER_UP)
  383. clock.p2 = limit->p2.p2_fast;
  384. else
  385. clock.p2 = limit->p2.p2_slow;
  386. } else {
  387. if (target < limit->p2.dot_limit)
  388. clock.p2 = limit->p2.p2_slow;
  389. else
  390. clock.p2 = limit->p2.p2_fast;
  391. }
  392. memset(best_clock, 0, sizeof(*best_clock));
  393. clock.m1 = 0;
  394. /* m1 is reserved as 0 in CDV, n is a ring counter.
  395. So skip the m1 loop */
  396. for (clock.n = limit->n.min; clock.n <= limit->n.max; clock.n++) {
  397. for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max;
  398. clock.m2++) {
  399. for (clock.p1 = limit->p1.min;
  400. clock.p1 <= limit->p1.max;
  401. clock.p1++) {
  402. int this_err;
  403. cdv_intel_clock(dev, refclk, &clock);
  404. if (!cdv_intel_PLL_is_valid(crtc,
  405. limit, &clock))
  406. continue;
  407. this_err = abs(clock.dot - target);
  408. if (this_err < err) {
  409. *best_clock = clock;
  410. err = this_err;
  411. }
  412. }
  413. }
  414. }
  415. return err != target;
  416. }
  417. static int cdv_intel_pipe_set_base(struct drm_crtc *crtc,
  418. int x, int y, struct drm_framebuffer *old_fb)
  419. {
  420. struct drm_device *dev = crtc->dev;
  421. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  422. struct psb_framebuffer *psbfb = to_psb_fb(crtc->fb);
  423. int pipe = psb_intel_crtc->pipe;
  424. unsigned long start, offset;
  425. int dspbase = (pipe == 0 ? DSPABASE : DSPBBASE);
  426. int dspsurf = (pipe == 0 ? DSPASURF : DSPBSURF);
  427. int dspstride = (pipe == 0) ? DSPASTRIDE : DSPBSTRIDE;
  428. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  429. u32 dspcntr;
  430. int ret = 0;
  431. if (!gma_power_begin(dev, true))
  432. return 0;
  433. /* no fb bound */
  434. if (!crtc->fb) {
  435. dev_err(dev->dev, "No FB bound\n");
  436. goto psb_intel_pipe_cleaner;
  437. }
  438. /* We are displaying this buffer, make sure it is actually loaded
  439. into the GTT */
  440. ret = psb_gtt_pin(psbfb->gtt);
  441. if (ret < 0)
  442. goto psb_intel_pipe_set_base_exit;
  443. start = psbfb->gtt->offset;
  444. offset = y * crtc->fb->pitches[0] + x * (crtc->fb->bits_per_pixel / 8);
  445. REG_WRITE(dspstride, crtc->fb->pitches[0]);
  446. dspcntr = REG_READ(dspcntr_reg);
  447. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  448. switch (crtc->fb->bits_per_pixel) {
  449. case 8:
  450. dspcntr |= DISPPLANE_8BPP;
  451. break;
  452. case 16:
  453. if (crtc->fb->depth == 15)
  454. dspcntr |= DISPPLANE_15_16BPP;
  455. else
  456. dspcntr |= DISPPLANE_16BPP;
  457. break;
  458. case 24:
  459. case 32:
  460. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  461. break;
  462. default:
  463. dev_err(dev->dev, "Unknown color depth\n");
  464. ret = -EINVAL;
  465. goto psb_intel_pipe_set_base_exit;
  466. }
  467. REG_WRITE(dspcntr_reg, dspcntr);
  468. dev_dbg(dev->dev,
  469. "Writing base %08lX %08lX %d %d\n", start, offset, x, y);
  470. REG_WRITE(dspbase, offset);
  471. REG_READ(dspbase);
  472. REG_WRITE(dspsurf, start);
  473. REG_READ(dspsurf);
  474. psb_intel_pipe_cleaner:
  475. /* If there was a previous display we can now unpin it */
  476. if (old_fb)
  477. psb_gtt_unpin(to_psb_fb(old_fb)->gtt);
  478. psb_intel_pipe_set_base_exit:
  479. gma_power_end(dev);
  480. return ret;
  481. }
  482. /**
  483. * Sets the power management mode of the pipe and plane.
  484. *
  485. * This code should probably grow support for turning the cursor off and back
  486. * on appropriately at the same time as we're turning the pipe off/on.
  487. */
  488. static void cdv_intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  489. {
  490. struct drm_device *dev = crtc->dev;
  491. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  492. int pipe = psb_intel_crtc->pipe;
  493. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  494. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  495. int dspbase_reg = (pipe == 0) ? DSPABASE : DSPBBASE;
  496. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  497. u32 temp;
  498. /* XXX: When our outputs are all unaware of DPMS modes other than off
  499. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  500. */
  501. switch (mode) {
  502. case DRM_MODE_DPMS_ON:
  503. case DRM_MODE_DPMS_STANDBY:
  504. case DRM_MODE_DPMS_SUSPEND:
  505. /* Enable the DPLL */
  506. temp = REG_READ(dpll_reg);
  507. if ((temp & DPLL_VCO_ENABLE) == 0) {
  508. REG_WRITE(dpll_reg, temp);
  509. REG_READ(dpll_reg);
  510. /* Wait for the clocks to stabilize. */
  511. udelay(150);
  512. REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  513. REG_READ(dpll_reg);
  514. /* Wait for the clocks to stabilize. */
  515. udelay(150);
  516. REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  517. REG_READ(dpll_reg);
  518. /* Wait for the clocks to stabilize. */
  519. udelay(150);
  520. }
  521. /* Jim Bish - switch plan and pipe per scott */
  522. /* Enable the plane */
  523. temp = REG_READ(dspcntr_reg);
  524. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  525. REG_WRITE(dspcntr_reg,
  526. temp | DISPLAY_PLANE_ENABLE);
  527. /* Flush the plane changes */
  528. REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
  529. }
  530. udelay(150);
  531. /* Enable the pipe */
  532. temp = REG_READ(pipeconf_reg);
  533. if ((temp & PIPEACONF_ENABLE) == 0)
  534. REG_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  535. psb_intel_crtc_load_lut(crtc);
  536. /* Give the overlay scaler a chance to enable
  537. * if it's on this pipe */
  538. /* psb_intel_crtc_dpms_video(crtc, true); TODO */
  539. break;
  540. case DRM_MODE_DPMS_OFF:
  541. /* Give the overlay scaler a chance to disable
  542. * if it's on this pipe */
  543. /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
  544. /* Disable the VGA plane that we never use */
  545. REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  546. /* Jim Bish - changed pipe/plane here as well. */
  547. /* Wait for vblank for the disable to take effect */
  548. cdv_intel_wait_for_vblank(dev);
  549. /* Next, disable display pipes */
  550. temp = REG_READ(pipeconf_reg);
  551. if ((temp & PIPEACONF_ENABLE) != 0) {
  552. REG_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  553. REG_READ(pipeconf_reg);
  554. }
  555. /* Wait for vblank for the disable to take effect. */
  556. cdv_intel_wait_for_vblank(dev);
  557. udelay(150);
  558. /* Disable display plane */
  559. temp = REG_READ(dspcntr_reg);
  560. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  561. REG_WRITE(dspcntr_reg,
  562. temp & ~DISPLAY_PLANE_ENABLE);
  563. /* Flush the plane changes */
  564. REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
  565. REG_READ(dspbase_reg);
  566. }
  567. temp = REG_READ(dpll_reg);
  568. if ((temp & DPLL_VCO_ENABLE) != 0) {
  569. REG_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  570. REG_READ(dpll_reg);
  571. }
  572. /* Wait for the clocks to turn off. */
  573. udelay(150);
  574. break;
  575. }
  576. /*Set FIFO Watermarks*/
  577. REG_WRITE(DSPARB, 0x3F3E);
  578. }
  579. static void cdv_intel_crtc_prepare(struct drm_crtc *crtc)
  580. {
  581. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  582. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  583. }
  584. static void cdv_intel_crtc_commit(struct drm_crtc *crtc)
  585. {
  586. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  587. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  588. }
  589. static bool cdv_intel_crtc_mode_fixup(struct drm_crtc *crtc,
  590. struct drm_display_mode *mode,
  591. struct drm_display_mode *adjusted_mode)
  592. {
  593. return true;
  594. }
  595. /**
  596. * Return the pipe currently connected to the panel fitter,
  597. * or -1 if the panel fitter is not present or not in use
  598. */
  599. static int cdv_intel_panel_fitter_pipe(struct drm_device *dev)
  600. {
  601. u32 pfit_control;
  602. pfit_control = REG_READ(PFIT_CONTROL);
  603. /* See if the panel fitter is in use */
  604. if ((pfit_control & PFIT_ENABLE) == 0)
  605. return -1;
  606. return (pfit_control >> 29) & 0x3;
  607. }
  608. static int cdv_intel_crtc_mode_set(struct drm_crtc *crtc,
  609. struct drm_display_mode *mode,
  610. struct drm_display_mode *adjusted_mode,
  611. int x, int y,
  612. struct drm_framebuffer *old_fb)
  613. {
  614. struct drm_device *dev = crtc->dev;
  615. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  616. int pipe = psb_intel_crtc->pipe;
  617. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  618. int dpll_md_reg = (psb_intel_crtc->pipe == 0) ? DPLL_A_MD : DPLL_B_MD;
  619. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  620. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  621. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  622. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  623. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  624. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  625. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  626. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  627. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  628. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  629. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  630. int refclk;
  631. struct cdv_intel_clock_t clock;
  632. u32 dpll = 0, dspcntr, pipeconf;
  633. bool ok;
  634. bool is_crt = false, is_lvds = false, is_tv = false;
  635. bool is_hdmi = false;
  636. struct drm_mode_config *mode_config = &dev->mode_config;
  637. struct drm_connector *connector;
  638. list_for_each_entry(connector, &mode_config->connector_list, head) {
  639. struct psb_intel_encoder *psb_intel_encoder =
  640. psb_intel_attached_encoder(connector);
  641. if (!connector->encoder
  642. || connector->encoder->crtc != crtc)
  643. continue;
  644. switch (psb_intel_encoder->type) {
  645. case INTEL_OUTPUT_LVDS:
  646. is_lvds = true;
  647. break;
  648. case INTEL_OUTPUT_TVOUT:
  649. is_tv = true;
  650. break;
  651. case INTEL_OUTPUT_ANALOG:
  652. is_crt = true;
  653. break;
  654. case INTEL_OUTPUT_HDMI:
  655. is_hdmi = true;
  656. break;
  657. }
  658. }
  659. refclk = 96000;
  660. /* Hack selection about ref clk for CRT */
  661. /* Select 27MHz as the reference clk for HDMI */
  662. if (is_crt || is_hdmi)
  663. refclk = 27000;
  664. drm_mode_debug_printmodeline(adjusted_mode);
  665. ok = cdv_intel_find_best_PLL(crtc, adjusted_mode->clock, refclk,
  666. &clock);
  667. if (!ok) {
  668. dev_err(dev->dev, "Couldn't find PLL settings for mode!\n");
  669. return 0;
  670. }
  671. dpll = DPLL_VGA_MODE_DIS;
  672. if (is_tv) {
  673. /* XXX: just matching BIOS for now */
  674. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  675. dpll |= 3;
  676. }
  677. dpll |= PLL_REF_INPUT_DREFCLK;
  678. dpll |= DPLL_SYNCLOCK_ENABLE;
  679. dpll |= DPLL_VGA_MODE_DIS;
  680. if (is_lvds)
  681. dpll |= DPLLB_MODE_LVDS;
  682. else
  683. dpll |= DPLLB_MODE_DAC_SERIAL;
  684. /* dpll |= (2 << 11); */
  685. /* setup pipeconf */
  686. pipeconf = REG_READ(pipeconf_reg);
  687. /* Set up the display plane register */
  688. dspcntr = DISPPLANE_GAMMA_ENABLE;
  689. if (pipe == 0)
  690. dspcntr |= DISPPLANE_SEL_PIPE_A;
  691. else
  692. dspcntr |= DISPPLANE_SEL_PIPE_B;
  693. dspcntr |= DISPLAY_PLANE_ENABLE;
  694. pipeconf |= PIPEACONF_ENABLE;
  695. REG_WRITE(dpll_reg, dpll | DPLL_VGA_MODE_DIS | DPLL_SYNCLOCK_ENABLE);
  696. REG_READ(dpll_reg);
  697. cdv_dpll_set_clock_cdv(dev, crtc, &clock);
  698. udelay(150);
  699. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  700. * This is an exception to the general rule that mode_set doesn't turn
  701. * things on.
  702. */
  703. if (is_lvds) {
  704. u32 lvds = REG_READ(LVDS);
  705. lvds |=
  706. LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP |
  707. LVDS_PIPEB_SELECT;
  708. /* Set the B0-B3 data pairs corresponding to
  709. * whether we're going to
  710. * set the DPLLs for dual-channel mode or not.
  711. */
  712. if (clock.p2 == 7)
  713. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  714. else
  715. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  716. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  717. * appropriately here, but we need to look more
  718. * thoroughly into how panels behave in the two modes.
  719. */
  720. REG_WRITE(LVDS, lvds);
  721. REG_READ(LVDS);
  722. }
  723. dpll |= DPLL_VCO_ENABLE;
  724. /* Disable the panel fitter if it was on our pipe */
  725. if (cdv_intel_panel_fitter_pipe(dev) == pipe)
  726. REG_WRITE(PFIT_CONTROL, 0);
  727. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  728. drm_mode_debug_printmodeline(mode);
  729. REG_WRITE(dpll_reg,
  730. (REG_READ(dpll_reg) & ~DPLL_LOCK) | DPLL_VCO_ENABLE);
  731. REG_READ(dpll_reg);
  732. /* Wait for the clocks to stabilize. */
  733. udelay(150); /* 42 usec w/o calibration, 110 with. rounded up. */
  734. if (!(REG_READ(dpll_reg) & DPLL_LOCK)) {
  735. dev_err(dev->dev, "Failed to get DPLL lock\n");
  736. return -EBUSY;
  737. }
  738. {
  739. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  740. REG_WRITE(dpll_md_reg, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) | ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  741. }
  742. REG_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  743. ((adjusted_mode->crtc_htotal - 1) << 16));
  744. REG_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  745. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  746. REG_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  747. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  748. REG_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  749. ((adjusted_mode->crtc_vtotal - 1) << 16));
  750. REG_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  751. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  752. REG_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  753. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  754. /* pipesrc and dspsize control the size that is scaled from,
  755. * which should always be the user's requested size.
  756. */
  757. REG_WRITE(dspsize_reg,
  758. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  759. REG_WRITE(dsppos_reg, 0);
  760. REG_WRITE(pipesrc_reg,
  761. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  762. REG_WRITE(pipeconf_reg, pipeconf);
  763. REG_READ(pipeconf_reg);
  764. cdv_intel_wait_for_vblank(dev);
  765. REG_WRITE(dspcntr_reg, dspcntr);
  766. /* Flush the plane changes */
  767. {
  768. struct drm_crtc_helper_funcs *crtc_funcs =
  769. crtc->helper_private;
  770. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  771. }
  772. cdv_intel_wait_for_vblank(dev);
  773. return 0;
  774. }
  775. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  776. static void cdv_intel_crtc_load_lut(struct drm_crtc *crtc)
  777. {
  778. struct drm_device *dev = crtc->dev;
  779. struct drm_psb_private *dev_priv =
  780. (struct drm_psb_private *)dev->dev_private;
  781. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  782. int palreg = PALETTE_A;
  783. int i;
  784. /* The clocks have to be on to load the palette. */
  785. if (!crtc->enabled)
  786. return;
  787. switch (psb_intel_crtc->pipe) {
  788. case 0:
  789. break;
  790. case 1:
  791. palreg = PALETTE_B;
  792. break;
  793. case 2:
  794. palreg = PALETTE_C;
  795. break;
  796. default:
  797. dev_err(dev->dev, "Illegal Pipe Number.\n");
  798. return;
  799. }
  800. if (gma_power_begin(dev, false)) {
  801. for (i = 0; i < 256; i++) {
  802. REG_WRITE(palreg + 4 * i,
  803. ((psb_intel_crtc->lut_r[i] +
  804. psb_intel_crtc->lut_adj[i]) << 16) |
  805. ((psb_intel_crtc->lut_g[i] +
  806. psb_intel_crtc->lut_adj[i]) << 8) |
  807. (psb_intel_crtc->lut_b[i] +
  808. psb_intel_crtc->lut_adj[i]));
  809. }
  810. gma_power_end(dev);
  811. } else {
  812. for (i = 0; i < 256; i++) {
  813. dev_priv->regs.psb.save_palette_a[i] =
  814. ((psb_intel_crtc->lut_r[i] +
  815. psb_intel_crtc->lut_adj[i]) << 16) |
  816. ((psb_intel_crtc->lut_g[i] +
  817. psb_intel_crtc->lut_adj[i]) << 8) |
  818. (psb_intel_crtc->lut_b[i] +
  819. psb_intel_crtc->lut_adj[i]);
  820. }
  821. }
  822. }
  823. /**
  824. * Save HW states of giving crtc
  825. */
  826. static void cdv_intel_crtc_save(struct drm_crtc *crtc)
  827. {
  828. struct drm_device *dev = crtc->dev;
  829. /* struct drm_psb_private *dev_priv =
  830. (struct drm_psb_private *)dev->dev_private; */
  831. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  832. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  833. int pipeA = (psb_intel_crtc->pipe == 0);
  834. uint32_t paletteReg;
  835. int i;
  836. if (!crtc_state) {
  837. dev_dbg(dev->dev, "No CRTC state found\n");
  838. return;
  839. }
  840. crtc_state->saveDSPCNTR = REG_READ(pipeA ? DSPACNTR : DSPBCNTR);
  841. crtc_state->savePIPECONF = REG_READ(pipeA ? PIPEACONF : PIPEBCONF);
  842. crtc_state->savePIPESRC = REG_READ(pipeA ? PIPEASRC : PIPEBSRC);
  843. crtc_state->saveFP0 = REG_READ(pipeA ? FPA0 : FPB0);
  844. crtc_state->saveFP1 = REG_READ(pipeA ? FPA1 : FPB1);
  845. crtc_state->saveDPLL = REG_READ(pipeA ? DPLL_A : DPLL_B);
  846. crtc_state->saveHTOTAL = REG_READ(pipeA ? HTOTAL_A : HTOTAL_B);
  847. crtc_state->saveHBLANK = REG_READ(pipeA ? HBLANK_A : HBLANK_B);
  848. crtc_state->saveHSYNC = REG_READ(pipeA ? HSYNC_A : HSYNC_B);
  849. crtc_state->saveVTOTAL = REG_READ(pipeA ? VTOTAL_A : VTOTAL_B);
  850. crtc_state->saveVBLANK = REG_READ(pipeA ? VBLANK_A : VBLANK_B);
  851. crtc_state->saveVSYNC = REG_READ(pipeA ? VSYNC_A : VSYNC_B);
  852. crtc_state->saveDSPSTRIDE = REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE);
  853. /*NOTE: DSPSIZE DSPPOS only for psb*/
  854. crtc_state->saveDSPSIZE = REG_READ(pipeA ? DSPASIZE : DSPBSIZE);
  855. crtc_state->saveDSPPOS = REG_READ(pipeA ? DSPAPOS : DSPBPOS);
  856. crtc_state->saveDSPBASE = REG_READ(pipeA ? DSPABASE : DSPBBASE);
  857. DRM_DEBUG("(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  858. crtc_state->saveDSPCNTR,
  859. crtc_state->savePIPECONF,
  860. crtc_state->savePIPESRC,
  861. crtc_state->saveFP0,
  862. crtc_state->saveFP1,
  863. crtc_state->saveDPLL,
  864. crtc_state->saveHTOTAL,
  865. crtc_state->saveHBLANK,
  866. crtc_state->saveHSYNC,
  867. crtc_state->saveVTOTAL,
  868. crtc_state->saveVBLANK,
  869. crtc_state->saveVSYNC,
  870. crtc_state->saveDSPSTRIDE,
  871. crtc_state->saveDSPSIZE,
  872. crtc_state->saveDSPPOS,
  873. crtc_state->saveDSPBASE
  874. );
  875. paletteReg = pipeA ? PALETTE_A : PALETTE_B;
  876. for (i = 0; i < 256; ++i)
  877. crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
  878. }
  879. /**
  880. * Restore HW states of giving crtc
  881. */
  882. static void cdv_intel_crtc_restore(struct drm_crtc *crtc)
  883. {
  884. struct drm_device *dev = crtc->dev;
  885. /* struct drm_psb_private * dev_priv =
  886. (struct drm_psb_private *)dev->dev_private; */
  887. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  888. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  889. /* struct drm_crtc_helper_funcs * crtc_funcs = crtc->helper_private; */
  890. int pipeA = (psb_intel_crtc->pipe == 0);
  891. uint32_t paletteReg;
  892. int i;
  893. if (!crtc_state) {
  894. dev_dbg(dev->dev, "No crtc state\n");
  895. return;
  896. }
  897. DRM_DEBUG(
  898. "current:(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  899. REG_READ(pipeA ? DSPACNTR : DSPBCNTR),
  900. REG_READ(pipeA ? PIPEACONF : PIPEBCONF),
  901. REG_READ(pipeA ? PIPEASRC : PIPEBSRC),
  902. REG_READ(pipeA ? FPA0 : FPB0),
  903. REG_READ(pipeA ? FPA1 : FPB1),
  904. REG_READ(pipeA ? DPLL_A : DPLL_B),
  905. REG_READ(pipeA ? HTOTAL_A : HTOTAL_B),
  906. REG_READ(pipeA ? HBLANK_A : HBLANK_B),
  907. REG_READ(pipeA ? HSYNC_A : HSYNC_B),
  908. REG_READ(pipeA ? VTOTAL_A : VTOTAL_B),
  909. REG_READ(pipeA ? VBLANK_A : VBLANK_B),
  910. REG_READ(pipeA ? VSYNC_A : VSYNC_B),
  911. REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE),
  912. REG_READ(pipeA ? DSPASIZE : DSPBSIZE),
  913. REG_READ(pipeA ? DSPAPOS : DSPBPOS),
  914. REG_READ(pipeA ? DSPABASE : DSPBBASE)
  915. );
  916. DRM_DEBUG(
  917. "saved: (%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  918. crtc_state->saveDSPCNTR,
  919. crtc_state->savePIPECONF,
  920. crtc_state->savePIPESRC,
  921. crtc_state->saveFP0,
  922. crtc_state->saveFP1,
  923. crtc_state->saveDPLL,
  924. crtc_state->saveHTOTAL,
  925. crtc_state->saveHBLANK,
  926. crtc_state->saveHSYNC,
  927. crtc_state->saveVTOTAL,
  928. crtc_state->saveVBLANK,
  929. crtc_state->saveVSYNC,
  930. crtc_state->saveDSPSTRIDE,
  931. crtc_state->saveDSPSIZE,
  932. crtc_state->saveDSPPOS,
  933. crtc_state->saveDSPBASE
  934. );
  935. if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
  936. REG_WRITE(pipeA ? DPLL_A : DPLL_B,
  937. crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
  938. REG_READ(pipeA ? DPLL_A : DPLL_B);
  939. DRM_DEBUG("write dpll: %x\n",
  940. REG_READ(pipeA ? DPLL_A : DPLL_B));
  941. udelay(150);
  942. }
  943. REG_WRITE(pipeA ? FPA0 : FPB0, crtc_state->saveFP0);
  944. REG_READ(pipeA ? FPA0 : FPB0);
  945. REG_WRITE(pipeA ? FPA1 : FPB1, crtc_state->saveFP1);
  946. REG_READ(pipeA ? FPA1 : FPB1);
  947. REG_WRITE(pipeA ? DPLL_A : DPLL_B, crtc_state->saveDPLL);
  948. REG_READ(pipeA ? DPLL_A : DPLL_B);
  949. udelay(150);
  950. REG_WRITE(pipeA ? HTOTAL_A : HTOTAL_B, crtc_state->saveHTOTAL);
  951. REG_WRITE(pipeA ? HBLANK_A : HBLANK_B, crtc_state->saveHBLANK);
  952. REG_WRITE(pipeA ? HSYNC_A : HSYNC_B, crtc_state->saveHSYNC);
  953. REG_WRITE(pipeA ? VTOTAL_A : VTOTAL_B, crtc_state->saveVTOTAL);
  954. REG_WRITE(pipeA ? VBLANK_A : VBLANK_B, crtc_state->saveVBLANK);
  955. REG_WRITE(pipeA ? VSYNC_A : VSYNC_B, crtc_state->saveVSYNC);
  956. REG_WRITE(pipeA ? DSPASTRIDE : DSPBSTRIDE, crtc_state->saveDSPSTRIDE);
  957. REG_WRITE(pipeA ? DSPASIZE : DSPBSIZE, crtc_state->saveDSPSIZE);
  958. REG_WRITE(pipeA ? DSPAPOS : DSPBPOS, crtc_state->saveDSPPOS);
  959. REG_WRITE(pipeA ? PIPEASRC : PIPEBSRC, crtc_state->savePIPESRC);
  960. REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
  961. REG_WRITE(pipeA ? PIPEACONF : PIPEBCONF, crtc_state->savePIPECONF);
  962. cdv_intel_wait_for_vblank(dev);
  963. REG_WRITE(pipeA ? DSPACNTR : DSPBCNTR, crtc_state->saveDSPCNTR);
  964. REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
  965. cdv_intel_wait_for_vblank(dev);
  966. paletteReg = pipeA ? PALETTE_A : PALETTE_B;
  967. for (i = 0; i < 256; ++i)
  968. REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
  969. }
  970. static int cdv_intel_crtc_cursor_set(struct drm_crtc *crtc,
  971. struct drm_file *file_priv,
  972. uint32_t handle,
  973. uint32_t width, uint32_t height)
  974. {
  975. struct drm_device *dev = crtc->dev;
  976. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  977. int pipe = psb_intel_crtc->pipe;
  978. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  979. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  980. uint32_t temp;
  981. size_t addr = 0;
  982. struct gtt_range *gt;
  983. struct drm_gem_object *obj;
  984. int ret;
  985. /* if we want to turn of the cursor ignore width and height */
  986. if (!handle) {
  987. /* turn off the cursor */
  988. temp = CURSOR_MODE_DISABLE;
  989. if (gma_power_begin(dev, false)) {
  990. REG_WRITE(control, temp);
  991. REG_WRITE(base, 0);
  992. gma_power_end(dev);
  993. }
  994. /* unpin the old GEM object */
  995. if (psb_intel_crtc->cursor_obj) {
  996. gt = container_of(psb_intel_crtc->cursor_obj,
  997. struct gtt_range, gem);
  998. psb_gtt_unpin(gt);
  999. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1000. psb_intel_crtc->cursor_obj = NULL;
  1001. }
  1002. return 0;
  1003. }
  1004. /* Currently we only support 64x64 cursors */
  1005. if (width != 64 || height != 64) {
  1006. dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
  1007. return -EINVAL;
  1008. }
  1009. obj = drm_gem_object_lookup(dev, file_priv, handle);
  1010. if (!obj)
  1011. return -ENOENT;
  1012. if (obj->size < width * height * 4) {
  1013. dev_dbg(dev->dev, "buffer is to small\n");
  1014. return -ENOMEM;
  1015. }
  1016. gt = container_of(obj, struct gtt_range, gem);
  1017. /* Pin the memory into the GTT */
  1018. ret = psb_gtt_pin(gt);
  1019. if (ret) {
  1020. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  1021. return ret;
  1022. }
  1023. addr = gt->offset; /* Or resource.start ??? */
  1024. psb_intel_crtc->cursor_addr = addr;
  1025. temp = 0;
  1026. /* set the pipe for the cursor */
  1027. temp |= (pipe << 28);
  1028. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  1029. if (gma_power_begin(dev, false)) {
  1030. REG_WRITE(control, temp);
  1031. REG_WRITE(base, addr);
  1032. gma_power_end(dev);
  1033. }
  1034. /* unpin the old GEM object */
  1035. if (psb_intel_crtc->cursor_obj) {
  1036. gt = container_of(psb_intel_crtc->cursor_obj,
  1037. struct gtt_range, gem);
  1038. psb_gtt_unpin(gt);
  1039. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1040. psb_intel_crtc->cursor_obj = obj;
  1041. }
  1042. return 0;
  1043. }
  1044. static int cdv_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1045. {
  1046. struct drm_device *dev = crtc->dev;
  1047. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1048. int pipe = psb_intel_crtc->pipe;
  1049. uint32_t temp = 0;
  1050. uint32_t adder;
  1051. if (x < 0) {
  1052. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  1053. x = -x;
  1054. }
  1055. if (y < 0) {
  1056. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  1057. y = -y;
  1058. }
  1059. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  1060. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  1061. adder = psb_intel_crtc->cursor_addr;
  1062. if (gma_power_begin(dev, false)) {
  1063. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  1064. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  1065. gma_power_end(dev);
  1066. }
  1067. return 0;
  1068. }
  1069. static void cdv_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
  1070. u16 *green, u16 *blue, uint32_t start, uint32_t size)
  1071. {
  1072. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1073. int i;
  1074. int end = (start + size > 256) ? 256 : start + size;
  1075. for (i = start; i < end; i++) {
  1076. psb_intel_crtc->lut_r[i] = red[i] >> 8;
  1077. psb_intel_crtc->lut_g[i] = green[i] >> 8;
  1078. psb_intel_crtc->lut_b[i] = blue[i] >> 8;
  1079. }
  1080. cdv_intel_crtc_load_lut(crtc);
  1081. }
  1082. static int cdv_crtc_set_config(struct drm_mode_set *set)
  1083. {
  1084. int ret = 0;
  1085. struct drm_device *dev = set->crtc->dev;
  1086. struct drm_psb_private *dev_priv = dev->dev_private;
  1087. if (!dev_priv->rpm_enabled)
  1088. return drm_crtc_helper_set_config(set);
  1089. pm_runtime_forbid(&dev->pdev->dev);
  1090. ret = drm_crtc_helper_set_config(set);
  1091. pm_runtime_allow(&dev->pdev->dev);
  1092. return ret;
  1093. }
  1094. /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
  1095. /* FIXME: why are we using this, should it be cdv_ in this tree ? */
  1096. static void i8xx_clock(int refclk, struct cdv_intel_clock_t *clock)
  1097. {
  1098. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  1099. clock->p = clock->p1 * clock->p2;
  1100. clock->vco = refclk * clock->m / (clock->n + 2);
  1101. clock->dot = clock->vco / clock->p;
  1102. }
  1103. /* Returns the clock of the currently programmed mode of the given pipe. */
  1104. static int cdv_intel_crtc_clock_get(struct drm_device *dev,
  1105. struct drm_crtc *crtc)
  1106. {
  1107. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1108. int pipe = psb_intel_crtc->pipe;
  1109. u32 dpll;
  1110. u32 fp;
  1111. struct cdv_intel_clock_t clock;
  1112. bool is_lvds;
  1113. struct drm_psb_private *dev_priv = dev->dev_private;
  1114. if (gma_power_begin(dev, false)) {
  1115. dpll = REG_READ((pipe == 0) ? DPLL_A : DPLL_B);
  1116. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1117. fp = REG_READ((pipe == 0) ? FPA0 : FPB0);
  1118. else
  1119. fp = REG_READ((pipe == 0) ? FPA1 : FPB1);
  1120. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  1121. gma_power_end(dev);
  1122. } else {
  1123. dpll = (pipe == 0) ?
  1124. dev_priv->regs.psb.saveDPLL_A :
  1125. dev_priv->regs.psb.saveDPLL_B;
  1126. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1127. fp = (pipe == 0) ?
  1128. dev_priv->regs.psb.saveFPA0 :
  1129. dev_priv->regs.psb.saveFPB0;
  1130. else
  1131. fp = (pipe == 0) ?
  1132. dev_priv->regs.psb.saveFPA1 :
  1133. dev_priv->regs.psb.saveFPB1;
  1134. is_lvds = (pipe == 1) &&
  1135. (dev_priv->regs.psb.saveLVDS & LVDS_PORT_EN);
  1136. }
  1137. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1138. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1139. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1140. if (is_lvds) {
  1141. clock.p1 =
  1142. ffs((dpll &
  1143. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1144. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1145. if (clock.p1 == 0) {
  1146. clock.p1 = 4;
  1147. dev_err(dev->dev, "PLL %d\n", dpll);
  1148. }
  1149. clock.p2 = 14;
  1150. if ((dpll & PLL_REF_INPUT_MASK) ==
  1151. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1152. /* XXX: might not be 66MHz */
  1153. i8xx_clock(66000, &clock);
  1154. } else
  1155. i8xx_clock(48000, &clock);
  1156. } else {
  1157. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1158. clock.p1 = 2;
  1159. else {
  1160. clock.p1 =
  1161. ((dpll &
  1162. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1163. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1164. }
  1165. if (dpll & PLL_P2_DIVIDE_BY_4)
  1166. clock.p2 = 4;
  1167. else
  1168. clock.p2 = 2;
  1169. i8xx_clock(48000, &clock);
  1170. }
  1171. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1172. * i830PllIsValid() because it relies on the xf86_config connector
  1173. * configuration being accurate, which it isn't necessarily.
  1174. */
  1175. return clock.dot;
  1176. }
  1177. /** Returns the currently programmed mode of the given pipe. */
  1178. struct drm_display_mode *cdv_intel_crtc_mode_get(struct drm_device *dev,
  1179. struct drm_crtc *crtc)
  1180. {
  1181. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1182. int pipe = psb_intel_crtc->pipe;
  1183. struct drm_display_mode *mode;
  1184. int htot;
  1185. int hsync;
  1186. int vtot;
  1187. int vsync;
  1188. struct drm_psb_private *dev_priv = dev->dev_private;
  1189. if (gma_power_begin(dev, false)) {
  1190. htot = REG_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  1191. hsync = REG_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  1192. vtot = REG_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  1193. vsync = REG_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  1194. gma_power_end(dev);
  1195. } else {
  1196. htot = (pipe == 0) ?
  1197. dev_priv->regs.psb.saveHTOTAL_A :
  1198. dev_priv->regs.psb.saveHTOTAL_B;
  1199. hsync = (pipe == 0) ?
  1200. dev_priv->regs.psb.saveHSYNC_A :
  1201. dev_priv->regs.psb.saveHSYNC_B;
  1202. vtot = (pipe == 0) ?
  1203. dev_priv->regs.psb.saveVTOTAL_A :
  1204. dev_priv->regs.psb.saveVTOTAL_B;
  1205. vsync = (pipe == 0) ?
  1206. dev_priv->regs.psb.saveVSYNC_A :
  1207. dev_priv->regs.psb.saveVSYNC_B;
  1208. }
  1209. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1210. if (!mode)
  1211. return NULL;
  1212. mode->clock = cdv_intel_crtc_clock_get(dev, crtc);
  1213. mode->hdisplay = (htot & 0xffff) + 1;
  1214. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1215. mode->hsync_start = (hsync & 0xffff) + 1;
  1216. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1217. mode->vdisplay = (vtot & 0xffff) + 1;
  1218. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1219. mode->vsync_start = (vsync & 0xffff) + 1;
  1220. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1221. drm_mode_set_name(mode);
  1222. drm_mode_set_crtcinfo(mode, 0);
  1223. return mode;
  1224. }
  1225. static void cdv_intel_crtc_destroy(struct drm_crtc *crtc)
  1226. {
  1227. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1228. kfree(psb_intel_crtc->crtc_state);
  1229. drm_crtc_cleanup(crtc);
  1230. kfree(psb_intel_crtc);
  1231. }
  1232. static void cdv_intel_crtc_disable(struct drm_crtc *crtc)
  1233. {
  1234. struct gtt_range *gt;
  1235. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1236. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  1237. if (crtc->fb) {
  1238. gt = to_psb_fb(crtc->fb)->gtt;
  1239. psb_gtt_unpin(gt);
  1240. }
  1241. }
  1242. const struct drm_crtc_helper_funcs cdv_intel_helper_funcs = {
  1243. .dpms = cdv_intel_crtc_dpms,
  1244. .mode_fixup = cdv_intel_crtc_mode_fixup,
  1245. .mode_set = cdv_intel_crtc_mode_set,
  1246. .mode_set_base = cdv_intel_pipe_set_base,
  1247. .prepare = cdv_intel_crtc_prepare,
  1248. .commit = cdv_intel_crtc_commit,
  1249. .disable = cdv_intel_crtc_disable,
  1250. };
  1251. const struct drm_crtc_funcs cdv_intel_crtc_funcs = {
  1252. .save = cdv_intel_crtc_save,
  1253. .restore = cdv_intel_crtc_restore,
  1254. .cursor_set = cdv_intel_crtc_cursor_set,
  1255. .cursor_move = cdv_intel_crtc_cursor_move,
  1256. .gamma_set = cdv_intel_crtc_gamma_set,
  1257. .set_config = cdv_crtc_set_config,
  1258. .destroy = cdv_intel_crtc_destroy,
  1259. };