psb_intel_display.c 41 KB

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