cdv_intel_display.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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. bool (*find_pll)(const struct cdv_intel_limit_t *, struct drm_crtc *,
  53. int, int, struct cdv_intel_clock_t *);
  54. };
  55. static bool cdv_intel_find_best_PLL(const struct cdv_intel_limit_t *limit,
  56. struct drm_crtc *crtc, int target, int refclk,
  57. struct cdv_intel_clock_t *best_clock);
  58. #define CDV_LIMIT_SINGLE_LVDS_96 0
  59. #define CDV_LIMIT_SINGLE_LVDS_100 1
  60. #define CDV_LIMIT_DAC_HDMI_27 2
  61. #define CDV_LIMIT_DAC_HDMI_96 3
  62. static const struct cdv_intel_limit_t cdv_intel_limits[] = {
  63. { /* CDV_SIGNLE_LVDS_96MHz */
  64. .dot = {.min = 20000, .max = 115500},
  65. .vco = {.min = 1800000, .max = 3600000},
  66. .n = {.min = 2, .max = 6},
  67. .m = {.min = 60, .max = 160},
  68. .m1 = {.min = 0, .max = 0},
  69. .m2 = {.min = 58, .max = 158},
  70. .p = {.min = 28, .max = 140},
  71. .p1 = {.min = 2, .max = 10},
  72. .p2 = {.dot_limit = 200000,
  73. .p2_slow = 14, .p2_fast = 14},
  74. .find_pll = cdv_intel_find_best_PLL,
  75. },
  76. { /* CDV_SINGLE_LVDS_100MHz */
  77. .dot = {.min = 20000, .max = 115500},
  78. .vco = {.min = 1800000, .max = 3600000},
  79. .n = {.min = 2, .max = 6},
  80. .m = {.min = 60, .max = 160},
  81. .m1 = {.min = 0, .max = 0},
  82. .m2 = {.min = 58, .max = 158},
  83. .p = {.min = 28, .max = 140},
  84. .p1 = {.min = 2, .max = 10},
  85. /* The single-channel range is 25-112Mhz, and dual-channel
  86. * is 80-224Mhz. Prefer single channel as much as possible.
  87. */
  88. .p2 = {.dot_limit = 200000, .p2_slow = 14, .p2_fast = 14},
  89. .find_pll = cdv_intel_find_best_PLL,
  90. },
  91. { /* CDV_DAC_HDMI_27MHz */
  92. .dot = {.min = 20000, .max = 400000},
  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 = 5},
  101. .find_pll = cdv_intel_find_best_PLL,
  102. },
  103. { /* CDV_DAC_HDMI_96MHz */
  104. .dot = {.min = 20000, .max = 400000},
  105. .vco = {.min = 1800000, .max = 3600000},
  106. .n = {.min = 2, .max = 6},
  107. .m = {.min = 60, .max = 160},
  108. .m1 = {.min = 0, .max = 0},
  109. .m2 = {.min = 58, .max = 158},
  110. .p = {.min = 5, .max = 100},
  111. .p1 = {.min = 1, .max = 10},
  112. .p2 = {.dot_limit = 225000, .p2_slow = 10, .p2_fast = 5},
  113. .find_pll = cdv_intel_find_best_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. static 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. static 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. static 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 cdv_intel_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) {
  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. /*
  332. * Returns whether any encoder on the specified pipe is of the specified type
  333. */
  334. static bool cdv_intel_pipe_has_type(struct drm_crtc *crtc, int type)
  335. {
  336. struct drm_device *dev = crtc->dev;
  337. struct drm_mode_config *mode_config = &dev->mode_config;
  338. struct drm_connector *l_entry;
  339. list_for_each_entry(l_entry, &mode_config->connector_list, head) {
  340. if (l_entry->encoder && l_entry->encoder->crtc == crtc) {
  341. struct psb_intel_encoder *psb_intel_encoder =
  342. psb_intel_attached_encoder(l_entry);
  343. if (psb_intel_encoder->type == type)
  344. return true;
  345. }
  346. }
  347. return false;
  348. }
  349. static const struct cdv_intel_limit_t *cdv_intel_limit(struct drm_crtc *crtc,
  350. int refclk)
  351. {
  352. const struct cdv_intel_limit_t *limit;
  353. if (cdv_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
  354. /*
  355. * Now only single-channel LVDS is supported on CDV. If it is
  356. * incorrect, please add the dual-channel LVDS.
  357. */
  358. if (refclk == 96000)
  359. limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_96];
  360. else
  361. limit = &cdv_intel_limits[CDV_LIMIT_SINGLE_LVDS_100];
  362. } else {
  363. if (refclk == 27000)
  364. limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_27];
  365. else
  366. limit = &cdv_intel_limits[CDV_LIMIT_DAC_HDMI_96];
  367. }
  368. return limit;
  369. }
  370. /* m1 is reserved as 0 in CDV, n is a ring counter */
  371. static void cdv_intel_clock(struct drm_device *dev,
  372. int refclk, struct cdv_intel_clock_t *clock)
  373. {
  374. clock->m = clock->m2 + 2;
  375. clock->p = clock->p1 * clock->p2;
  376. clock->vco = (refclk * clock->m) / clock->n;
  377. clock->dot = clock->vco / clock->p;
  378. }
  379. #define INTELPllInvalid(s) { /* ErrorF (s) */; return false; }
  380. static bool cdv_intel_PLL_is_valid(struct drm_crtc *crtc,
  381. const struct cdv_intel_limit_t *limit,
  382. struct cdv_intel_clock_t *clock)
  383. {
  384. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  385. INTELPllInvalid("p1 out of range\n");
  386. if (clock->p < limit->p.min || limit->p.max < clock->p)
  387. INTELPllInvalid("p out of range\n");
  388. /* unnecessary to check the range of m(m1/M2)/n again */
  389. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  390. INTELPllInvalid("vco out of range\n");
  391. /* XXX: We may need to be checking "Dot clock"
  392. * depending on the multiplier, connector, etc.,
  393. * rather than just a single range.
  394. */
  395. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  396. INTELPllInvalid("dot out of range\n");
  397. return true;
  398. }
  399. static bool cdv_intel_find_best_PLL(const struct cdv_intel_limit_t *limit,
  400. struct drm_crtc *crtc, int target, int refclk,
  401. struct cdv_intel_clock_t *best_clock)
  402. {
  403. struct drm_device *dev = crtc->dev;
  404. struct cdv_intel_clock_t clock;
  405. int err = target;
  406. if (cdv_intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
  407. (REG_READ(LVDS) & LVDS_PORT_EN) != 0) {
  408. /*
  409. * For LVDS, if the panel is on, just rely on its current
  410. * settings for dual-channel. We haven't figured out how to
  411. * reliably set up different single/dual channel state, if we
  412. * even can.
  413. */
  414. if ((REG_READ(LVDS) & LVDS_CLKB_POWER_MASK) ==
  415. LVDS_CLKB_POWER_UP)
  416. clock.p2 = limit->p2.p2_fast;
  417. else
  418. clock.p2 = limit->p2.p2_slow;
  419. } else {
  420. if (target < limit->p2.dot_limit)
  421. clock.p2 = limit->p2.p2_slow;
  422. else
  423. clock.p2 = limit->p2.p2_fast;
  424. }
  425. memset(best_clock, 0, sizeof(*best_clock));
  426. clock.m1 = 0;
  427. /* m1 is reserved as 0 in CDV, n is a ring counter.
  428. So skip the m1 loop */
  429. for (clock.n = limit->n.min; clock.n <= limit->n.max; clock.n++) {
  430. for (clock.m2 = limit->m2.min; clock.m2 <= limit->m2.max;
  431. clock.m2++) {
  432. for (clock.p1 = limit->p1.min;
  433. clock.p1 <= limit->p1.max;
  434. clock.p1++) {
  435. int this_err;
  436. cdv_intel_clock(dev, refclk, &clock);
  437. if (!cdv_intel_PLL_is_valid(crtc,
  438. limit, &clock))
  439. continue;
  440. this_err = abs(clock.dot - target);
  441. if (this_err < err) {
  442. *best_clock = clock;
  443. err = this_err;
  444. }
  445. }
  446. }
  447. }
  448. return err != target;
  449. }
  450. static int cdv_intel_pipe_set_base(struct drm_crtc *crtc,
  451. int x, int y, struct drm_framebuffer *old_fb)
  452. {
  453. struct drm_device *dev = crtc->dev;
  454. struct drm_psb_private *dev_priv = dev->dev_private;
  455. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  456. struct psb_framebuffer *psbfb = to_psb_fb(crtc->fb);
  457. int pipe = psb_intel_crtc->pipe;
  458. const struct psb_offset *map = &dev_priv->regmap[pipe];
  459. unsigned long start, offset;
  460. u32 dspcntr;
  461. int ret = 0;
  462. if (!gma_power_begin(dev, true))
  463. return 0;
  464. /* no fb bound */
  465. if (!crtc->fb) {
  466. dev_err(dev->dev, "No FB bound\n");
  467. goto psb_intel_pipe_cleaner;
  468. }
  469. /* We are displaying this buffer, make sure it is actually loaded
  470. into the GTT */
  471. ret = psb_gtt_pin(psbfb->gtt);
  472. if (ret < 0)
  473. goto psb_intel_pipe_set_base_exit;
  474. start = psbfb->gtt->offset;
  475. offset = y * crtc->fb->pitches[0] + x * (crtc->fb->bits_per_pixel / 8);
  476. REG_WRITE(map->stride, crtc->fb->pitches[0]);
  477. dspcntr = REG_READ(map->cntr);
  478. dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
  479. switch (crtc->fb->bits_per_pixel) {
  480. case 8:
  481. dspcntr |= DISPPLANE_8BPP;
  482. break;
  483. case 16:
  484. if (crtc->fb->depth == 15)
  485. dspcntr |= DISPPLANE_15_16BPP;
  486. else
  487. dspcntr |= DISPPLANE_16BPP;
  488. break;
  489. case 24:
  490. case 32:
  491. dspcntr |= DISPPLANE_32BPP_NO_ALPHA;
  492. break;
  493. default:
  494. dev_err(dev->dev, "Unknown color depth\n");
  495. ret = -EINVAL;
  496. goto psb_intel_pipe_set_base_exit;
  497. }
  498. REG_WRITE(map->cntr, dspcntr);
  499. dev_dbg(dev->dev,
  500. "Writing base %08lX %08lX %d %d\n", start, offset, x, y);
  501. REG_WRITE(map->base, offset);
  502. REG_READ(map->base);
  503. REG_WRITE(map->surf, start);
  504. REG_READ(map->surf);
  505. psb_intel_pipe_cleaner:
  506. /* If there was a previous display we can now unpin it */
  507. if (old_fb)
  508. psb_gtt_unpin(to_psb_fb(old_fb)->gtt);
  509. psb_intel_pipe_set_base_exit:
  510. gma_power_end(dev);
  511. return ret;
  512. }
  513. #define FIFO_PIPEA (1 << 0)
  514. #define FIFO_PIPEB (1 << 1)
  515. static bool cdv_intel_pipe_enabled(struct drm_device *dev, int pipe)
  516. {
  517. struct drm_crtc *crtc;
  518. struct drm_psb_private *dev_priv = dev->dev_private;
  519. struct psb_intel_crtc *psb_intel_crtc = NULL;
  520. crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  521. psb_intel_crtc = to_psb_intel_crtc(crtc);
  522. if (crtc->fb == NULL || !psb_intel_crtc->active)
  523. return false;
  524. return true;
  525. }
  526. static bool cdv_intel_single_pipe_active (struct drm_device *dev)
  527. {
  528. uint32_t pipe_enabled = 0;
  529. if (cdv_intel_pipe_enabled(dev, 0))
  530. pipe_enabled |= FIFO_PIPEA;
  531. if (cdv_intel_pipe_enabled(dev, 1))
  532. pipe_enabled |= FIFO_PIPEB;
  533. DRM_DEBUG_KMS("pipe enabled %x\n", pipe_enabled);
  534. if (pipe_enabled == FIFO_PIPEA || pipe_enabled == FIFO_PIPEB)
  535. return true;
  536. else
  537. return false;
  538. }
  539. static bool is_pipeb_lvds(struct drm_device *dev, struct drm_crtc *crtc)
  540. {
  541. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  542. struct drm_mode_config *mode_config = &dev->mode_config;
  543. struct drm_connector *connector;
  544. if (psb_intel_crtc->pipe != 1)
  545. return false;
  546. list_for_each_entry(connector, &mode_config->connector_list, head) {
  547. struct psb_intel_encoder *psb_intel_encoder =
  548. psb_intel_attached_encoder(connector);
  549. if (!connector->encoder
  550. || connector->encoder->crtc != crtc)
  551. continue;
  552. if (psb_intel_encoder->type == INTEL_OUTPUT_LVDS)
  553. return true;
  554. }
  555. return false;
  556. }
  557. static void cdv_intel_disable_self_refresh (struct drm_device *dev)
  558. {
  559. if (REG_READ(FW_BLC_SELF) & FW_BLC_SELF_EN) {
  560. /* Disable self-refresh before adjust WM */
  561. REG_WRITE(FW_BLC_SELF, (REG_READ(FW_BLC_SELF) & ~FW_BLC_SELF_EN));
  562. REG_READ(FW_BLC_SELF);
  563. cdv_intel_wait_for_vblank(dev);
  564. /* Cedarview workaround to write ovelay plane, which force to leave
  565. * MAX_FIFO state.
  566. */
  567. REG_WRITE(OV_OVADD, 0/*dev_priv->ovl_offset*/);
  568. REG_READ(OV_OVADD);
  569. cdv_intel_wait_for_vblank(dev);
  570. }
  571. }
  572. static void cdv_intel_update_watermark (struct drm_device *dev, struct drm_crtc *crtc)
  573. {
  574. if (cdv_intel_single_pipe_active(dev)) {
  575. u32 fw;
  576. fw = REG_READ(DSPFW1);
  577. fw &= ~DSP_FIFO_SR_WM_MASK;
  578. fw |= (0x7e << DSP_FIFO_SR_WM_SHIFT);
  579. fw &= ~CURSOR_B_FIFO_WM_MASK;
  580. fw |= (0x4 << CURSOR_B_FIFO_WM_SHIFT);
  581. REG_WRITE(DSPFW1, fw);
  582. fw = REG_READ(DSPFW2);
  583. fw &= ~CURSOR_A_FIFO_WM_MASK;
  584. fw |= (0x6 << CURSOR_A_FIFO_WM_SHIFT);
  585. fw &= ~DSP_PLANE_C_FIFO_WM_MASK;
  586. fw |= (0x8 << DSP_PLANE_C_FIFO_WM_SHIFT);
  587. REG_WRITE(DSPFW2, fw);
  588. REG_WRITE(DSPFW3, 0x36000000);
  589. /* ignore FW4 */
  590. if (is_pipeb_lvds(dev, crtc)) {
  591. REG_WRITE(DSPFW5, 0x00040330);
  592. } else {
  593. fw = (3 << DSP_PLANE_B_FIFO_WM1_SHIFT) |
  594. (4 << DSP_PLANE_A_FIFO_WM1_SHIFT) |
  595. (3 << CURSOR_B_FIFO_WM1_SHIFT) |
  596. (4 << CURSOR_FIFO_SR_WM1_SHIFT);
  597. REG_WRITE(DSPFW5, fw);
  598. }
  599. REG_WRITE(DSPFW6, 0x10);
  600. cdv_intel_wait_for_vblank(dev);
  601. /* enable self-refresh for single pipe active */
  602. REG_WRITE(FW_BLC_SELF, FW_BLC_SELF_EN);
  603. REG_READ(FW_BLC_SELF);
  604. cdv_intel_wait_for_vblank(dev);
  605. } else {
  606. /* HW team suggested values... */
  607. REG_WRITE(DSPFW1, 0x3f880808);
  608. REG_WRITE(DSPFW2, 0x0b020202);
  609. REG_WRITE(DSPFW3, 0x24000000);
  610. REG_WRITE(DSPFW4, 0x08030202);
  611. REG_WRITE(DSPFW5, 0x01010101);
  612. REG_WRITE(DSPFW6, 0x1d0);
  613. cdv_intel_wait_for_vblank(dev);
  614. cdv_intel_disable_self_refresh(dev);
  615. }
  616. }
  617. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  618. static void cdv_intel_crtc_load_lut(struct drm_crtc *crtc)
  619. {
  620. struct drm_device *dev = crtc->dev;
  621. struct drm_psb_private *dev_priv = dev->dev_private;
  622. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  623. int palreg = PALETTE_A;
  624. int i;
  625. /* The clocks have to be on to load the palette. */
  626. if (!crtc->enabled)
  627. return;
  628. switch (psb_intel_crtc->pipe) {
  629. case 0:
  630. break;
  631. case 1:
  632. palreg = PALETTE_B;
  633. break;
  634. case 2:
  635. palreg = PALETTE_C;
  636. break;
  637. default:
  638. dev_err(dev->dev, "Illegal Pipe Number.\n");
  639. return;
  640. }
  641. if (gma_power_begin(dev, false)) {
  642. for (i = 0; i < 256; i++) {
  643. REG_WRITE(palreg + 4 * i,
  644. ((psb_intel_crtc->lut_r[i] +
  645. psb_intel_crtc->lut_adj[i]) << 16) |
  646. ((psb_intel_crtc->lut_g[i] +
  647. psb_intel_crtc->lut_adj[i]) << 8) |
  648. (psb_intel_crtc->lut_b[i] +
  649. psb_intel_crtc->lut_adj[i]));
  650. }
  651. gma_power_end(dev);
  652. } else {
  653. for (i = 0; i < 256; i++) {
  654. dev_priv->regs.pipe[0].palette[i] =
  655. ((psb_intel_crtc->lut_r[i] +
  656. psb_intel_crtc->lut_adj[i]) << 16) |
  657. ((psb_intel_crtc->lut_g[i] +
  658. psb_intel_crtc->lut_adj[i]) << 8) |
  659. (psb_intel_crtc->lut_b[i] +
  660. psb_intel_crtc->lut_adj[i]);
  661. }
  662. }
  663. }
  664. /**
  665. * Sets the power management mode of the pipe and plane.
  666. *
  667. * This code should probably grow support for turning the cursor off and back
  668. * on appropriately at the same time as we're turning the pipe off/on.
  669. */
  670. static void cdv_intel_crtc_dpms(struct drm_crtc *crtc, int mode)
  671. {
  672. struct drm_device *dev = crtc->dev;
  673. struct drm_psb_private *dev_priv = dev->dev_private;
  674. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  675. int pipe = psb_intel_crtc->pipe;
  676. const struct psb_offset *map = &dev_priv->regmap[pipe];
  677. u32 temp;
  678. /* XXX: When our outputs are all unaware of DPMS modes other than off
  679. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  680. */
  681. cdv_intel_disable_self_refresh(dev);
  682. switch (mode) {
  683. case DRM_MODE_DPMS_ON:
  684. case DRM_MODE_DPMS_STANDBY:
  685. case DRM_MODE_DPMS_SUSPEND:
  686. if (psb_intel_crtc->active)
  687. break;
  688. psb_intel_crtc->active = true;
  689. /* Enable the DPLL */
  690. temp = REG_READ(map->dpll);
  691. if ((temp & DPLL_VCO_ENABLE) == 0) {
  692. REG_WRITE(map->dpll, temp);
  693. REG_READ(map->dpll);
  694. /* Wait for the clocks to stabilize. */
  695. udelay(150);
  696. REG_WRITE(map->dpll, temp | DPLL_VCO_ENABLE);
  697. REG_READ(map->dpll);
  698. /* Wait for the clocks to stabilize. */
  699. udelay(150);
  700. REG_WRITE(map->dpll, temp | DPLL_VCO_ENABLE);
  701. REG_READ(map->dpll);
  702. /* Wait for the clocks to stabilize. */
  703. udelay(150);
  704. }
  705. /* Jim Bish - switch plan and pipe per scott */
  706. /* Enable the plane */
  707. temp = REG_READ(map->cntr);
  708. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  709. REG_WRITE(map->cntr,
  710. temp | DISPLAY_PLANE_ENABLE);
  711. /* Flush the plane changes */
  712. REG_WRITE(map->base, REG_READ(map->base));
  713. }
  714. udelay(150);
  715. /* Enable the pipe */
  716. temp = REG_READ(map->conf);
  717. if ((temp & PIPEACONF_ENABLE) == 0)
  718. REG_WRITE(map->conf, temp | PIPEACONF_ENABLE);
  719. temp = REG_READ(map->status);
  720. temp &= ~(0xFFFF);
  721. temp |= PIPE_FIFO_UNDERRUN;
  722. REG_WRITE(map->status, temp);
  723. REG_READ(map->status);
  724. cdv_intel_crtc_load_lut(crtc);
  725. /* Give the overlay scaler a chance to enable
  726. * if it's on this pipe */
  727. /* psb_intel_crtc_dpms_video(crtc, true); TODO */
  728. psb_intel_crtc->crtc_enable = true;
  729. break;
  730. case DRM_MODE_DPMS_OFF:
  731. if (!psb_intel_crtc->active)
  732. break;
  733. psb_intel_crtc->active = false;
  734. /* Give the overlay scaler a chance to disable
  735. * if it's on this pipe */
  736. /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
  737. /* Disable the VGA plane that we never use */
  738. REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  739. /* Jim Bish - changed pipe/plane here as well. */
  740. drm_vblank_off(dev, pipe);
  741. /* Wait for vblank for the disable to take effect */
  742. cdv_intel_wait_for_vblank(dev);
  743. /* Next, disable display pipes */
  744. temp = REG_READ(map->conf);
  745. if ((temp & PIPEACONF_ENABLE) != 0) {
  746. REG_WRITE(map->conf, temp & ~PIPEACONF_ENABLE);
  747. REG_READ(map->conf);
  748. }
  749. /* Wait for vblank for the disable to take effect. */
  750. cdv_intel_wait_for_vblank(dev);
  751. udelay(150);
  752. /* Disable display plane */
  753. temp = REG_READ(map->cntr);
  754. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  755. REG_WRITE(map->cntr,
  756. temp & ~DISPLAY_PLANE_ENABLE);
  757. /* Flush the plane changes */
  758. REG_WRITE(map->base, REG_READ(map->base));
  759. REG_READ(map->base);
  760. }
  761. temp = REG_READ(map->dpll);
  762. if ((temp & DPLL_VCO_ENABLE) != 0) {
  763. REG_WRITE(map->dpll, temp & ~DPLL_VCO_ENABLE);
  764. REG_READ(map->dpll);
  765. }
  766. /* Wait for the clocks to turn off. */
  767. udelay(150);
  768. psb_intel_crtc->crtc_enable = false;
  769. break;
  770. }
  771. cdv_intel_update_watermark(dev, crtc);
  772. /*Set FIFO Watermarks*/
  773. REG_WRITE(DSPARB, 0x3F3E);
  774. }
  775. static void cdv_intel_crtc_prepare(struct drm_crtc *crtc)
  776. {
  777. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  778. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  779. }
  780. static void cdv_intel_crtc_commit(struct drm_crtc *crtc)
  781. {
  782. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  783. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  784. }
  785. static bool cdv_intel_crtc_mode_fixup(struct drm_crtc *crtc,
  786. const struct drm_display_mode *mode,
  787. struct drm_display_mode *adjusted_mode)
  788. {
  789. return true;
  790. }
  791. /**
  792. * Return the pipe currently connected to the panel fitter,
  793. * or -1 if the panel fitter is not present or not in use
  794. */
  795. static int cdv_intel_panel_fitter_pipe(struct drm_device *dev)
  796. {
  797. u32 pfit_control;
  798. pfit_control = REG_READ(PFIT_CONTROL);
  799. /* See if the panel fitter is in use */
  800. if ((pfit_control & PFIT_ENABLE) == 0)
  801. return -1;
  802. return (pfit_control >> 29) & 0x3;
  803. }
  804. static int cdv_intel_crtc_mode_set(struct drm_crtc *crtc,
  805. struct drm_display_mode *mode,
  806. struct drm_display_mode *adjusted_mode,
  807. int x, int y,
  808. struct drm_framebuffer *old_fb)
  809. {
  810. struct drm_device *dev = crtc->dev;
  811. struct drm_psb_private *dev_priv = dev->dev_private;
  812. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  813. int pipe = psb_intel_crtc->pipe;
  814. const struct psb_offset *map = &dev_priv->regmap[pipe];
  815. int refclk;
  816. struct cdv_intel_clock_t clock;
  817. u32 dpll = 0, dspcntr, pipeconf;
  818. bool ok;
  819. bool is_crt = false, is_lvds = false, is_tv = false;
  820. bool is_hdmi = false;
  821. struct drm_mode_config *mode_config = &dev->mode_config;
  822. struct drm_connector *connector;
  823. const struct cdv_intel_limit_t *limit;
  824. u32 ddi_select = 0;
  825. list_for_each_entry(connector, &mode_config->connector_list, head) {
  826. struct psb_intel_encoder *psb_intel_encoder =
  827. psb_intel_attached_encoder(connector);
  828. if (!connector->encoder
  829. || connector->encoder->crtc != crtc)
  830. continue;
  831. ddi_select = psb_intel_encoder->ddi_select;
  832. switch (psb_intel_encoder->type) {
  833. case INTEL_OUTPUT_LVDS:
  834. is_lvds = true;
  835. break;
  836. case INTEL_OUTPUT_TVOUT:
  837. is_tv = true;
  838. break;
  839. case INTEL_OUTPUT_ANALOG:
  840. is_crt = true;
  841. break;
  842. case INTEL_OUTPUT_HDMI:
  843. is_hdmi = true;
  844. break;
  845. default:
  846. DRM_ERROR("invalid output type.\n");
  847. return 0;
  848. }
  849. }
  850. if (dev_priv->dplla_96mhz)
  851. /* low-end sku, 96/100 mhz */
  852. refclk = 96000;
  853. else
  854. /* high-end sku, 27/100 mhz */
  855. refclk = 27000;
  856. if (is_lvds && dev_priv->lvds_use_ssc) {
  857. refclk = dev_priv->lvds_ssc_freq * 1000;
  858. DRM_DEBUG_KMS("Use SSC reference clock %d Mhz\n", dev_priv->lvds_ssc_freq);
  859. }
  860. drm_mode_debug_printmodeline(adjusted_mode);
  861. limit = cdv_intel_limit(crtc, refclk);
  862. ok = limit->find_pll(limit, crtc, adjusted_mode->clock, refclk,
  863. &clock);
  864. if (!ok) {
  865. dev_err(dev->dev, "Couldn't find PLL settings for mode!\n");
  866. return 0;
  867. }
  868. dpll = DPLL_VGA_MODE_DIS;
  869. if (is_tv) {
  870. /* XXX: just matching BIOS for now */
  871. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  872. dpll |= 3;
  873. }
  874. /* dpll |= PLL_REF_INPUT_DREFCLK; */
  875. dpll |= DPLL_SYNCLOCK_ENABLE;
  876. /* if (is_lvds)
  877. dpll |= DPLLB_MODE_LVDS;
  878. else
  879. dpll |= DPLLB_MODE_DAC_SERIAL; */
  880. /* dpll |= (2 << 11); */
  881. /* setup pipeconf */
  882. pipeconf = REG_READ(map->conf);
  883. /* Set up the display plane register */
  884. dspcntr = DISPPLANE_GAMMA_ENABLE;
  885. if (pipe == 0)
  886. dspcntr |= DISPPLANE_SEL_PIPE_A;
  887. else
  888. dspcntr |= DISPPLANE_SEL_PIPE_B;
  889. dspcntr |= DISPLAY_PLANE_ENABLE;
  890. pipeconf |= PIPEACONF_ENABLE;
  891. REG_WRITE(map->dpll, dpll | DPLL_VGA_MODE_DIS | DPLL_SYNCLOCK_ENABLE);
  892. REG_READ(map->dpll);
  893. cdv_dpll_set_clock_cdv(dev, crtc, &clock, is_lvds, ddi_select);
  894. udelay(150);
  895. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  896. * This is an exception to the general rule that mode_set doesn't turn
  897. * things on.
  898. */
  899. if (is_lvds) {
  900. u32 lvds = REG_READ(LVDS);
  901. lvds |=
  902. LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP |
  903. LVDS_PIPEB_SELECT;
  904. /* Set the B0-B3 data pairs corresponding to
  905. * whether we're going to
  906. * set the DPLLs for dual-channel mode or not.
  907. */
  908. if (clock.p2 == 7)
  909. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  910. else
  911. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  912. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  913. * appropriately here, but we need to look more
  914. * thoroughly into how panels behave in the two modes.
  915. */
  916. REG_WRITE(LVDS, lvds);
  917. REG_READ(LVDS);
  918. }
  919. dpll |= DPLL_VCO_ENABLE;
  920. /* Disable the panel fitter if it was on our pipe */
  921. if (cdv_intel_panel_fitter_pipe(dev) == pipe)
  922. REG_WRITE(PFIT_CONTROL, 0);
  923. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  924. drm_mode_debug_printmodeline(mode);
  925. REG_WRITE(map->dpll,
  926. (REG_READ(map->dpll) & ~DPLL_LOCK) | DPLL_VCO_ENABLE);
  927. REG_READ(map->dpll);
  928. /* Wait for the clocks to stabilize. */
  929. udelay(150); /* 42 usec w/o calibration, 110 with. rounded up. */
  930. if (!(REG_READ(map->dpll) & DPLL_LOCK)) {
  931. dev_err(dev->dev, "Failed to get DPLL lock\n");
  932. return -EBUSY;
  933. }
  934. {
  935. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  936. REG_WRITE(map->dpll_md, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) | ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  937. }
  938. REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
  939. ((adjusted_mode->crtc_htotal - 1) << 16));
  940. REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
  941. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  942. REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
  943. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  944. REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
  945. ((adjusted_mode->crtc_vtotal - 1) << 16));
  946. REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
  947. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  948. REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
  949. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  950. /* pipesrc and dspsize control the size that is scaled from,
  951. * which should always be the user's requested size.
  952. */
  953. REG_WRITE(map->size,
  954. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  955. REG_WRITE(map->pos, 0);
  956. REG_WRITE(map->src,
  957. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  958. REG_WRITE(map->conf, pipeconf);
  959. REG_READ(map->conf);
  960. cdv_intel_wait_for_vblank(dev);
  961. REG_WRITE(map->cntr, dspcntr);
  962. /* Flush the plane changes */
  963. {
  964. struct drm_crtc_helper_funcs *crtc_funcs =
  965. crtc->helper_private;
  966. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  967. }
  968. cdv_intel_wait_for_vblank(dev);
  969. return 0;
  970. }
  971. /**
  972. * Save HW states of giving crtc
  973. */
  974. static void cdv_intel_crtc_save(struct drm_crtc *crtc)
  975. {
  976. struct drm_device *dev = crtc->dev;
  977. struct drm_psb_private *dev_priv = dev->dev_private;
  978. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  979. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  980. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  981. uint32_t paletteReg;
  982. int i;
  983. if (!crtc_state) {
  984. dev_dbg(dev->dev, "No CRTC state found\n");
  985. return;
  986. }
  987. crtc_state->saveDSPCNTR = REG_READ(map->cntr);
  988. crtc_state->savePIPECONF = REG_READ(map->conf);
  989. crtc_state->savePIPESRC = REG_READ(map->src);
  990. crtc_state->saveFP0 = REG_READ(map->fp0);
  991. crtc_state->saveFP1 = REG_READ(map->fp1);
  992. crtc_state->saveDPLL = REG_READ(map->dpll);
  993. crtc_state->saveHTOTAL = REG_READ(map->htotal);
  994. crtc_state->saveHBLANK = REG_READ(map->hblank);
  995. crtc_state->saveHSYNC = REG_READ(map->hsync);
  996. crtc_state->saveVTOTAL = REG_READ(map->vtotal);
  997. crtc_state->saveVBLANK = REG_READ(map->vblank);
  998. crtc_state->saveVSYNC = REG_READ(map->vsync);
  999. crtc_state->saveDSPSTRIDE = REG_READ(map->stride);
  1000. /*NOTE: DSPSIZE DSPPOS only for psb*/
  1001. crtc_state->saveDSPSIZE = REG_READ(map->size);
  1002. crtc_state->saveDSPPOS = REG_READ(map->pos);
  1003. crtc_state->saveDSPBASE = REG_READ(map->base);
  1004. DRM_DEBUG("(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  1005. crtc_state->saveDSPCNTR,
  1006. crtc_state->savePIPECONF,
  1007. crtc_state->savePIPESRC,
  1008. crtc_state->saveFP0,
  1009. crtc_state->saveFP1,
  1010. crtc_state->saveDPLL,
  1011. crtc_state->saveHTOTAL,
  1012. crtc_state->saveHBLANK,
  1013. crtc_state->saveHSYNC,
  1014. crtc_state->saveVTOTAL,
  1015. crtc_state->saveVBLANK,
  1016. crtc_state->saveVSYNC,
  1017. crtc_state->saveDSPSTRIDE,
  1018. crtc_state->saveDSPSIZE,
  1019. crtc_state->saveDSPPOS,
  1020. crtc_state->saveDSPBASE
  1021. );
  1022. paletteReg = map->palette;
  1023. for (i = 0; i < 256; ++i)
  1024. crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
  1025. }
  1026. /**
  1027. * Restore HW states of giving crtc
  1028. */
  1029. static void cdv_intel_crtc_restore(struct drm_crtc *crtc)
  1030. {
  1031. struct drm_device *dev = crtc->dev;
  1032. struct drm_psb_private *dev_priv = dev->dev_private;
  1033. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1034. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  1035. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  1036. uint32_t paletteReg;
  1037. int i;
  1038. if (!crtc_state) {
  1039. dev_dbg(dev->dev, "No crtc state\n");
  1040. return;
  1041. }
  1042. DRM_DEBUG(
  1043. "current:(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  1044. REG_READ(map->cntr),
  1045. REG_READ(map->conf),
  1046. REG_READ(map->src),
  1047. REG_READ(map->fp0),
  1048. REG_READ(map->fp1),
  1049. REG_READ(map->dpll),
  1050. REG_READ(map->htotal),
  1051. REG_READ(map->hblank),
  1052. REG_READ(map->hsync),
  1053. REG_READ(map->vtotal),
  1054. REG_READ(map->vblank),
  1055. REG_READ(map->vsync),
  1056. REG_READ(map->stride),
  1057. REG_READ(map->size),
  1058. REG_READ(map->pos),
  1059. REG_READ(map->base)
  1060. );
  1061. DRM_DEBUG(
  1062. "saved: (%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  1063. crtc_state->saveDSPCNTR,
  1064. crtc_state->savePIPECONF,
  1065. crtc_state->savePIPESRC,
  1066. crtc_state->saveFP0,
  1067. crtc_state->saveFP1,
  1068. crtc_state->saveDPLL,
  1069. crtc_state->saveHTOTAL,
  1070. crtc_state->saveHBLANK,
  1071. crtc_state->saveHSYNC,
  1072. crtc_state->saveVTOTAL,
  1073. crtc_state->saveVBLANK,
  1074. crtc_state->saveVSYNC,
  1075. crtc_state->saveDSPSTRIDE,
  1076. crtc_state->saveDSPSIZE,
  1077. crtc_state->saveDSPPOS,
  1078. crtc_state->saveDSPBASE
  1079. );
  1080. if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
  1081. REG_WRITE(map->dpll,
  1082. crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
  1083. REG_READ(map->dpll);
  1084. DRM_DEBUG("write dpll: %x\n",
  1085. REG_READ(map->dpll));
  1086. udelay(150);
  1087. }
  1088. REG_WRITE(map->fp0, crtc_state->saveFP0);
  1089. REG_READ(map->fp0);
  1090. REG_WRITE(map->fp1, crtc_state->saveFP1);
  1091. REG_READ(map->fp1);
  1092. REG_WRITE(map->dpll, crtc_state->saveDPLL);
  1093. REG_READ(map->dpll);
  1094. udelay(150);
  1095. REG_WRITE(map->htotal, crtc_state->saveHTOTAL);
  1096. REG_WRITE(map->hblank, crtc_state->saveHBLANK);
  1097. REG_WRITE(map->hsync, crtc_state->saveHSYNC);
  1098. REG_WRITE(map->vtotal, crtc_state->saveVTOTAL);
  1099. REG_WRITE(map->vblank, crtc_state->saveVBLANK);
  1100. REG_WRITE(map->vsync, crtc_state->saveVSYNC);
  1101. REG_WRITE(map->stride, crtc_state->saveDSPSTRIDE);
  1102. REG_WRITE(map->size, crtc_state->saveDSPSIZE);
  1103. REG_WRITE(map->pos, crtc_state->saveDSPPOS);
  1104. REG_WRITE(map->src, crtc_state->savePIPESRC);
  1105. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  1106. REG_WRITE(map->conf, crtc_state->savePIPECONF);
  1107. cdv_intel_wait_for_vblank(dev);
  1108. REG_WRITE(map->cntr, crtc_state->saveDSPCNTR);
  1109. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  1110. cdv_intel_wait_for_vblank(dev);
  1111. paletteReg = map->palette;
  1112. for (i = 0; i < 256; ++i)
  1113. REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
  1114. }
  1115. static int cdv_intel_crtc_cursor_set(struct drm_crtc *crtc,
  1116. struct drm_file *file_priv,
  1117. uint32_t handle,
  1118. uint32_t width, uint32_t height)
  1119. {
  1120. struct drm_device *dev = crtc->dev;
  1121. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1122. int pipe = psb_intel_crtc->pipe;
  1123. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  1124. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  1125. uint32_t temp;
  1126. size_t addr = 0;
  1127. struct gtt_range *gt;
  1128. struct drm_gem_object *obj;
  1129. int ret;
  1130. /* if we want to turn of the cursor ignore width and height */
  1131. if (!handle) {
  1132. /* turn off the cursor */
  1133. temp = CURSOR_MODE_DISABLE;
  1134. if (gma_power_begin(dev, false)) {
  1135. REG_WRITE(control, temp);
  1136. REG_WRITE(base, 0);
  1137. gma_power_end(dev);
  1138. }
  1139. /* unpin the old GEM object */
  1140. if (psb_intel_crtc->cursor_obj) {
  1141. gt = container_of(psb_intel_crtc->cursor_obj,
  1142. struct gtt_range, gem);
  1143. psb_gtt_unpin(gt);
  1144. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1145. psb_intel_crtc->cursor_obj = NULL;
  1146. }
  1147. return 0;
  1148. }
  1149. /* Currently we only support 64x64 cursors */
  1150. if (width != 64 || height != 64) {
  1151. dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
  1152. return -EINVAL;
  1153. }
  1154. obj = drm_gem_object_lookup(dev, file_priv, handle);
  1155. if (!obj)
  1156. return -ENOENT;
  1157. if (obj->size < width * height * 4) {
  1158. dev_dbg(dev->dev, "buffer is to small\n");
  1159. return -ENOMEM;
  1160. }
  1161. gt = container_of(obj, struct gtt_range, gem);
  1162. /* Pin the memory into the GTT */
  1163. ret = psb_gtt_pin(gt);
  1164. if (ret) {
  1165. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  1166. return ret;
  1167. }
  1168. addr = gt->offset; /* Or resource.start ??? */
  1169. psb_intel_crtc->cursor_addr = addr;
  1170. temp = 0;
  1171. /* set the pipe for the cursor */
  1172. temp |= (pipe << 28);
  1173. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  1174. if (gma_power_begin(dev, false)) {
  1175. REG_WRITE(control, temp);
  1176. REG_WRITE(base, addr);
  1177. gma_power_end(dev);
  1178. }
  1179. /* unpin the old GEM object */
  1180. if (psb_intel_crtc->cursor_obj) {
  1181. gt = container_of(psb_intel_crtc->cursor_obj,
  1182. struct gtt_range, gem);
  1183. psb_gtt_unpin(gt);
  1184. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1185. psb_intel_crtc->cursor_obj = obj;
  1186. }
  1187. return 0;
  1188. }
  1189. static int cdv_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1190. {
  1191. struct drm_device *dev = crtc->dev;
  1192. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1193. int pipe = psb_intel_crtc->pipe;
  1194. uint32_t temp = 0;
  1195. uint32_t adder;
  1196. if (x < 0) {
  1197. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  1198. x = -x;
  1199. }
  1200. if (y < 0) {
  1201. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  1202. y = -y;
  1203. }
  1204. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  1205. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  1206. adder = psb_intel_crtc->cursor_addr;
  1207. if (gma_power_begin(dev, false)) {
  1208. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  1209. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  1210. gma_power_end(dev);
  1211. }
  1212. return 0;
  1213. }
  1214. static void cdv_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
  1215. u16 *green, u16 *blue, uint32_t start, uint32_t size)
  1216. {
  1217. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1218. int i;
  1219. int end = (start + size > 256) ? 256 : start + size;
  1220. for (i = start; i < end; i++) {
  1221. psb_intel_crtc->lut_r[i] = red[i] >> 8;
  1222. psb_intel_crtc->lut_g[i] = green[i] >> 8;
  1223. psb_intel_crtc->lut_b[i] = blue[i] >> 8;
  1224. }
  1225. cdv_intel_crtc_load_lut(crtc);
  1226. }
  1227. static int cdv_crtc_set_config(struct drm_mode_set *set)
  1228. {
  1229. int ret = 0;
  1230. struct drm_device *dev = set->crtc->dev;
  1231. struct drm_psb_private *dev_priv = dev->dev_private;
  1232. if (!dev_priv->rpm_enabled)
  1233. return drm_crtc_helper_set_config(set);
  1234. pm_runtime_forbid(&dev->pdev->dev);
  1235. ret = drm_crtc_helper_set_config(set);
  1236. pm_runtime_allow(&dev->pdev->dev);
  1237. return ret;
  1238. }
  1239. /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
  1240. /* FIXME: why are we using this, should it be cdv_ in this tree ? */
  1241. static void i8xx_clock(int refclk, struct cdv_intel_clock_t *clock)
  1242. {
  1243. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  1244. clock->p = clock->p1 * clock->p2;
  1245. clock->vco = refclk * clock->m / (clock->n + 2);
  1246. clock->dot = clock->vco / clock->p;
  1247. }
  1248. /* Returns the clock of the currently programmed mode of the given pipe. */
  1249. static int cdv_intel_crtc_clock_get(struct drm_device *dev,
  1250. struct drm_crtc *crtc)
  1251. {
  1252. struct drm_psb_private *dev_priv = dev->dev_private;
  1253. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1254. int pipe = psb_intel_crtc->pipe;
  1255. const struct psb_offset *map = &dev_priv->regmap[pipe];
  1256. u32 dpll;
  1257. u32 fp;
  1258. struct cdv_intel_clock_t clock;
  1259. bool is_lvds;
  1260. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  1261. if (gma_power_begin(dev, false)) {
  1262. dpll = REG_READ(map->dpll);
  1263. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1264. fp = REG_READ(map->fp0);
  1265. else
  1266. fp = REG_READ(map->fp1);
  1267. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  1268. gma_power_end(dev);
  1269. } else {
  1270. dpll = p->dpll;
  1271. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1272. fp = p->fp0;
  1273. else
  1274. fp = p->fp1;
  1275. is_lvds = (pipe == 1) &&
  1276. (dev_priv->regs.psb.saveLVDS & LVDS_PORT_EN);
  1277. }
  1278. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1279. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1280. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1281. if (is_lvds) {
  1282. clock.p1 =
  1283. ffs((dpll &
  1284. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1285. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1286. if (clock.p1 == 0) {
  1287. clock.p1 = 4;
  1288. dev_err(dev->dev, "PLL %d\n", dpll);
  1289. }
  1290. clock.p2 = 14;
  1291. if ((dpll & PLL_REF_INPUT_MASK) ==
  1292. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1293. /* XXX: might not be 66MHz */
  1294. i8xx_clock(66000, &clock);
  1295. } else
  1296. i8xx_clock(48000, &clock);
  1297. } else {
  1298. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1299. clock.p1 = 2;
  1300. else {
  1301. clock.p1 =
  1302. ((dpll &
  1303. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1304. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1305. }
  1306. if (dpll & PLL_P2_DIVIDE_BY_4)
  1307. clock.p2 = 4;
  1308. else
  1309. clock.p2 = 2;
  1310. i8xx_clock(48000, &clock);
  1311. }
  1312. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1313. * i830PllIsValid() because it relies on the xf86_config connector
  1314. * configuration being accurate, which it isn't necessarily.
  1315. */
  1316. return clock.dot;
  1317. }
  1318. /** Returns the currently programmed mode of the given pipe. */
  1319. struct drm_display_mode *cdv_intel_crtc_mode_get(struct drm_device *dev,
  1320. struct drm_crtc *crtc)
  1321. {
  1322. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1323. int pipe = psb_intel_crtc->pipe;
  1324. struct drm_psb_private *dev_priv = dev->dev_private;
  1325. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  1326. const struct psb_offset *map = &dev_priv->regmap[pipe];
  1327. struct drm_display_mode *mode;
  1328. int htot;
  1329. int hsync;
  1330. int vtot;
  1331. int vsync;
  1332. if (gma_power_begin(dev, false)) {
  1333. htot = REG_READ(map->htotal);
  1334. hsync = REG_READ(map->hsync);
  1335. vtot = REG_READ(map->vtotal);
  1336. vsync = REG_READ(map->vsync);
  1337. gma_power_end(dev);
  1338. } else {
  1339. htot = p->htotal;
  1340. hsync = p->hsync;
  1341. vtot = p->vtotal;
  1342. vsync = p->vsync;
  1343. }
  1344. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1345. if (!mode)
  1346. return NULL;
  1347. mode->clock = cdv_intel_crtc_clock_get(dev, crtc);
  1348. mode->hdisplay = (htot & 0xffff) + 1;
  1349. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1350. mode->hsync_start = (hsync & 0xffff) + 1;
  1351. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1352. mode->vdisplay = (vtot & 0xffff) + 1;
  1353. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1354. mode->vsync_start = (vsync & 0xffff) + 1;
  1355. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1356. drm_mode_set_name(mode);
  1357. drm_mode_set_crtcinfo(mode, 0);
  1358. return mode;
  1359. }
  1360. static void cdv_intel_crtc_destroy(struct drm_crtc *crtc)
  1361. {
  1362. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1363. kfree(psb_intel_crtc->crtc_state);
  1364. drm_crtc_cleanup(crtc);
  1365. kfree(psb_intel_crtc);
  1366. }
  1367. const struct drm_crtc_helper_funcs cdv_intel_helper_funcs = {
  1368. .dpms = cdv_intel_crtc_dpms,
  1369. .mode_fixup = cdv_intel_crtc_mode_fixup,
  1370. .mode_set = cdv_intel_crtc_mode_set,
  1371. .mode_set_base = cdv_intel_pipe_set_base,
  1372. .prepare = cdv_intel_crtc_prepare,
  1373. .commit = cdv_intel_crtc_commit,
  1374. };
  1375. const struct drm_crtc_funcs cdv_intel_crtc_funcs = {
  1376. .save = cdv_intel_crtc_save,
  1377. .restore = cdv_intel_crtc_restore,
  1378. .cursor_set = cdv_intel_crtc_cursor_set,
  1379. .cursor_move = cdv_intel_crtc_cursor_move,
  1380. .gamma_set = cdv_intel_crtc_gamma_set,
  1381. .set_config = cdv_crtc_set_config,
  1382. .destroy = cdv_intel_crtc_destroy,
  1383. };