psb_intel_display.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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. static 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. /* XXX: When our outputs are all unaware of DPMS modes other than off
  391. * and on, we should map those modes to DRM_MODE_DPMS_OFF in the CRTC.
  392. */
  393. switch (mode) {
  394. case DRM_MODE_DPMS_ON:
  395. case DRM_MODE_DPMS_STANDBY:
  396. case DRM_MODE_DPMS_SUSPEND:
  397. /* Enable the DPLL */
  398. temp = REG_READ(dpll_reg);
  399. if ((temp & DPLL_VCO_ENABLE) == 0) {
  400. REG_WRITE(dpll_reg, temp);
  401. REG_READ(dpll_reg);
  402. /* Wait for the clocks to stabilize. */
  403. udelay(150);
  404. REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  405. REG_READ(dpll_reg);
  406. /* Wait for the clocks to stabilize. */
  407. udelay(150);
  408. REG_WRITE(dpll_reg, temp | DPLL_VCO_ENABLE);
  409. REG_READ(dpll_reg);
  410. /* Wait for the clocks to stabilize. */
  411. udelay(150);
  412. }
  413. /* Enable the pipe */
  414. temp = REG_READ(pipeconf_reg);
  415. if ((temp & PIPEACONF_ENABLE) == 0)
  416. REG_WRITE(pipeconf_reg, temp | PIPEACONF_ENABLE);
  417. /* Enable the plane */
  418. temp = REG_READ(dspcntr_reg);
  419. if ((temp & DISPLAY_PLANE_ENABLE) == 0) {
  420. REG_WRITE(dspcntr_reg,
  421. temp | DISPLAY_PLANE_ENABLE);
  422. /* Flush the plane changes */
  423. REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
  424. }
  425. psb_intel_crtc_load_lut(crtc);
  426. /* Give the overlay scaler a chance to enable
  427. * if it's on this pipe */
  428. /* psb_intel_crtc_dpms_video(crtc, true); TODO */
  429. break;
  430. case DRM_MODE_DPMS_OFF:
  431. /* Give the overlay scaler a chance to disable
  432. * if it's on this pipe */
  433. /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
  434. /* Disable the VGA plane that we never use */
  435. REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  436. /* Disable display plane */
  437. temp = REG_READ(dspcntr_reg);
  438. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  439. REG_WRITE(dspcntr_reg,
  440. temp & ~DISPLAY_PLANE_ENABLE);
  441. /* Flush the plane changes */
  442. REG_WRITE(dspbase_reg, REG_READ(dspbase_reg));
  443. REG_READ(dspbase_reg);
  444. }
  445. /* Next, disable display pipes */
  446. temp = REG_READ(pipeconf_reg);
  447. if ((temp & PIPEACONF_ENABLE) != 0) {
  448. REG_WRITE(pipeconf_reg, temp & ~PIPEACONF_ENABLE);
  449. REG_READ(pipeconf_reg);
  450. }
  451. /* Wait for vblank for the disable to take effect. */
  452. psb_intel_wait_for_vblank(dev);
  453. temp = REG_READ(dpll_reg);
  454. if ((temp & DPLL_VCO_ENABLE) != 0) {
  455. REG_WRITE(dpll_reg, temp & ~DPLL_VCO_ENABLE);
  456. REG_READ(dpll_reg);
  457. }
  458. /* Wait for the clocks to turn off. */
  459. udelay(150);
  460. break;
  461. }
  462. /*Set FIFO Watermarks*/
  463. REG_WRITE(DSPARB, 0x3F3E);
  464. }
  465. static void psb_intel_crtc_prepare(struct drm_crtc *crtc)
  466. {
  467. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  468. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  469. }
  470. static void psb_intel_crtc_commit(struct drm_crtc *crtc)
  471. {
  472. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  473. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  474. }
  475. void psb_intel_encoder_prepare(struct drm_encoder *encoder)
  476. {
  477. struct drm_encoder_helper_funcs *encoder_funcs =
  478. encoder->helper_private;
  479. /* lvds has its own version of prepare see psb_intel_lvds_prepare */
  480. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_OFF);
  481. }
  482. void psb_intel_encoder_commit(struct drm_encoder *encoder)
  483. {
  484. struct drm_encoder_helper_funcs *encoder_funcs =
  485. encoder->helper_private;
  486. /* lvds has its own version of commit see psb_intel_lvds_commit */
  487. encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
  488. }
  489. void psb_intel_encoder_destroy(struct drm_encoder *encoder)
  490. {
  491. struct psb_intel_encoder *intel_encoder = to_psb_intel_encoder(encoder);
  492. drm_encoder_cleanup(encoder);
  493. kfree(intel_encoder);
  494. }
  495. static bool psb_intel_crtc_mode_fixup(struct drm_crtc *crtc,
  496. struct drm_display_mode *mode,
  497. struct drm_display_mode *adjusted_mode)
  498. {
  499. return true;
  500. }
  501. /**
  502. * Return the pipe currently connected to the panel fitter,
  503. * or -1 if the panel fitter is not present or not in use
  504. */
  505. static int psb_intel_panel_fitter_pipe(struct drm_device *dev)
  506. {
  507. u32 pfit_control;
  508. pfit_control = REG_READ(PFIT_CONTROL);
  509. /* See if the panel fitter is in use */
  510. if ((pfit_control & PFIT_ENABLE) == 0)
  511. return -1;
  512. /* Must be on PIPE 1 for PSB */
  513. return 1;
  514. }
  515. static int psb_intel_crtc_mode_set(struct drm_crtc *crtc,
  516. struct drm_display_mode *mode,
  517. struct drm_display_mode *adjusted_mode,
  518. int x, int y,
  519. struct drm_framebuffer *old_fb)
  520. {
  521. struct drm_device *dev = crtc->dev;
  522. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  523. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  524. int pipe = psb_intel_crtc->pipe;
  525. int fp_reg = (pipe == 0) ? FPA0 : FPB0;
  526. int dpll_reg = (pipe == 0) ? DPLL_A : DPLL_B;
  527. int dspcntr_reg = (pipe == 0) ? DSPACNTR : DSPBCNTR;
  528. int pipeconf_reg = (pipe == 0) ? PIPEACONF : PIPEBCONF;
  529. int htot_reg = (pipe == 0) ? HTOTAL_A : HTOTAL_B;
  530. int hblank_reg = (pipe == 0) ? HBLANK_A : HBLANK_B;
  531. int hsync_reg = (pipe == 0) ? HSYNC_A : HSYNC_B;
  532. int vtot_reg = (pipe == 0) ? VTOTAL_A : VTOTAL_B;
  533. int vblank_reg = (pipe == 0) ? VBLANK_A : VBLANK_B;
  534. int vsync_reg = (pipe == 0) ? VSYNC_A : VSYNC_B;
  535. int dspsize_reg = (pipe == 0) ? DSPASIZE : DSPBSIZE;
  536. int dsppos_reg = (pipe == 0) ? DSPAPOS : DSPBPOS;
  537. int pipesrc_reg = (pipe == 0) ? PIPEASRC : PIPEBSRC;
  538. int refclk;
  539. struct psb_intel_clock_t clock;
  540. u32 dpll = 0, fp = 0, dspcntr, pipeconf;
  541. bool ok, is_sdvo = false;
  542. bool is_lvds = false, is_tv = false;
  543. struct drm_mode_config *mode_config = &dev->mode_config;
  544. struct drm_connector *connector;
  545. /* No scan out no play */
  546. if (crtc->fb == NULL) {
  547. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  548. return 0;
  549. }
  550. list_for_each_entry(connector, &mode_config->connector_list, head) {
  551. struct psb_intel_encoder *psb_intel_encoder =
  552. psb_intel_attached_encoder(connector);
  553. if (!connector->encoder
  554. || connector->encoder->crtc != crtc)
  555. continue;
  556. switch (psb_intel_encoder->type) {
  557. case INTEL_OUTPUT_LVDS:
  558. is_lvds = true;
  559. break;
  560. case INTEL_OUTPUT_SDVO:
  561. is_sdvo = true;
  562. break;
  563. case INTEL_OUTPUT_TVOUT:
  564. is_tv = true;
  565. break;
  566. }
  567. }
  568. refclk = 96000;
  569. ok = psb_intel_find_best_PLL(crtc, adjusted_mode->clock, refclk,
  570. &clock);
  571. if (!ok) {
  572. dev_err(dev->dev, "Couldn't find PLL settings for mode!\n");
  573. return 0;
  574. }
  575. fp = clock.n << 16 | clock.m1 << 8 | clock.m2;
  576. dpll = DPLL_VGA_MODE_DIS;
  577. if (is_lvds) {
  578. dpll |= DPLLB_MODE_LVDS;
  579. dpll |= DPLL_DVO_HIGH_SPEED;
  580. } else
  581. dpll |= DPLLB_MODE_DAC_SERIAL;
  582. if (is_sdvo) {
  583. int sdvo_pixel_multiply =
  584. adjusted_mode->clock / mode->clock;
  585. dpll |= DPLL_DVO_HIGH_SPEED;
  586. dpll |=
  587. (sdvo_pixel_multiply - 1) << SDVO_MULTIPLIER_SHIFT_HIRES;
  588. }
  589. /* compute bitmask from p1 value */
  590. dpll |= (1 << (clock.p1 - 1)) << 16;
  591. switch (clock.p2) {
  592. case 5:
  593. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  594. break;
  595. case 7:
  596. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  597. break;
  598. case 10:
  599. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  600. break;
  601. case 14:
  602. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  603. break;
  604. }
  605. if (is_tv) {
  606. /* XXX: just matching BIOS for now */
  607. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  608. dpll |= 3;
  609. }
  610. dpll |= PLL_REF_INPUT_DREFCLK;
  611. /* setup pipeconf */
  612. pipeconf = REG_READ(pipeconf_reg);
  613. /* Set up the display plane register */
  614. dspcntr = DISPPLANE_GAMMA_ENABLE;
  615. if (pipe == 0)
  616. dspcntr |= DISPPLANE_SEL_PIPE_A;
  617. else
  618. dspcntr |= DISPPLANE_SEL_PIPE_B;
  619. dspcntr |= DISPLAY_PLANE_ENABLE;
  620. pipeconf |= PIPEACONF_ENABLE;
  621. dpll |= DPLL_VCO_ENABLE;
  622. /* Disable the panel fitter if it was on our pipe */
  623. if (psb_intel_panel_fitter_pipe(dev) == pipe)
  624. REG_WRITE(PFIT_CONTROL, 0);
  625. drm_mode_debug_printmodeline(mode);
  626. if (dpll & DPLL_VCO_ENABLE) {
  627. REG_WRITE(fp_reg, fp);
  628. REG_WRITE(dpll_reg, dpll & ~DPLL_VCO_ENABLE);
  629. REG_READ(dpll_reg);
  630. udelay(150);
  631. }
  632. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  633. * This is an exception to the general rule that mode_set doesn't turn
  634. * things on.
  635. */
  636. if (is_lvds) {
  637. u32 lvds = REG_READ(LVDS);
  638. lvds &= ~LVDS_PIPEB_SELECT;
  639. if (pipe == 1)
  640. lvds |= LVDS_PIPEB_SELECT;
  641. lvds |= LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP;
  642. /* Set the B0-B3 data pairs corresponding to
  643. * whether we're going to
  644. * set the DPLLs for dual-channel mode or not.
  645. */
  646. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  647. if (clock.p2 == 7)
  648. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  649. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  650. * appropriately here, but we need to look more
  651. * thoroughly into how panels behave in the two modes.
  652. */
  653. REG_WRITE(LVDS, lvds);
  654. REG_READ(LVDS);
  655. }
  656. REG_WRITE(fp_reg, fp);
  657. REG_WRITE(dpll_reg, dpll);
  658. REG_READ(dpll_reg);
  659. /* Wait for the clocks to stabilize. */
  660. udelay(150);
  661. /* write it again -- the BIOS does, after all */
  662. REG_WRITE(dpll_reg, dpll);
  663. REG_READ(dpll_reg);
  664. /* Wait for the clocks to stabilize. */
  665. udelay(150);
  666. REG_WRITE(htot_reg, (adjusted_mode->crtc_hdisplay - 1) |
  667. ((adjusted_mode->crtc_htotal - 1) << 16));
  668. REG_WRITE(hblank_reg, (adjusted_mode->crtc_hblank_start - 1) |
  669. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  670. REG_WRITE(hsync_reg, (adjusted_mode->crtc_hsync_start - 1) |
  671. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  672. REG_WRITE(vtot_reg, (adjusted_mode->crtc_vdisplay - 1) |
  673. ((adjusted_mode->crtc_vtotal - 1) << 16));
  674. REG_WRITE(vblank_reg, (adjusted_mode->crtc_vblank_start - 1) |
  675. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  676. REG_WRITE(vsync_reg, (adjusted_mode->crtc_vsync_start - 1) |
  677. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  678. /* pipesrc and dspsize control the size that is scaled from,
  679. * which should always be the user's requested size.
  680. */
  681. REG_WRITE(dspsize_reg,
  682. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  683. REG_WRITE(dsppos_reg, 0);
  684. REG_WRITE(pipesrc_reg,
  685. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  686. REG_WRITE(pipeconf_reg, pipeconf);
  687. REG_READ(pipeconf_reg);
  688. psb_intel_wait_for_vblank(dev);
  689. REG_WRITE(dspcntr_reg, dspcntr);
  690. /* Flush the plane changes */
  691. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  692. psb_intel_wait_for_vblank(dev);
  693. return 0;
  694. }
  695. /** Loads the palette/gamma unit for the CRTC with the prepared values */
  696. void psb_intel_crtc_load_lut(struct drm_crtc *crtc)
  697. {
  698. struct drm_device *dev = crtc->dev;
  699. struct drm_psb_private *dev_priv =
  700. (struct drm_psb_private *)dev->dev_private;
  701. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  702. int palreg = PALETTE_A;
  703. int i;
  704. /* The clocks have to be on to load the palette. */
  705. if (!crtc->enabled)
  706. return;
  707. switch (psb_intel_crtc->pipe) {
  708. case 0:
  709. break;
  710. case 1:
  711. palreg = PALETTE_B;
  712. break;
  713. case 2:
  714. palreg = PALETTE_C;
  715. break;
  716. default:
  717. dev_err(dev->dev, "Illegal Pipe Number.\n");
  718. return;
  719. }
  720. if (gma_power_begin(dev, false)) {
  721. for (i = 0; i < 256; i++) {
  722. REG_WRITE(palreg + 4 * i,
  723. ((psb_intel_crtc->lut_r[i] +
  724. psb_intel_crtc->lut_adj[i]) << 16) |
  725. ((psb_intel_crtc->lut_g[i] +
  726. psb_intel_crtc->lut_adj[i]) << 8) |
  727. (psb_intel_crtc->lut_b[i] +
  728. psb_intel_crtc->lut_adj[i]));
  729. }
  730. gma_power_end(dev);
  731. } else {
  732. for (i = 0; i < 256; i++) {
  733. dev_priv->regs.psb.save_palette_a[i] =
  734. ((psb_intel_crtc->lut_r[i] +
  735. psb_intel_crtc->lut_adj[i]) << 16) |
  736. ((psb_intel_crtc->lut_g[i] +
  737. psb_intel_crtc->lut_adj[i]) << 8) |
  738. (psb_intel_crtc->lut_b[i] +
  739. psb_intel_crtc->lut_adj[i]);
  740. }
  741. }
  742. }
  743. /**
  744. * Save HW states of giving crtc
  745. */
  746. static void psb_intel_crtc_save(struct drm_crtc *crtc)
  747. {
  748. struct drm_device *dev = crtc->dev;
  749. /* struct drm_psb_private *dev_priv =
  750. (struct drm_psb_private *)dev->dev_private; */
  751. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  752. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  753. int pipeA = (psb_intel_crtc->pipe == 0);
  754. uint32_t paletteReg;
  755. int i;
  756. if (!crtc_state) {
  757. dev_err(dev->dev, "No CRTC state found\n");
  758. return;
  759. }
  760. crtc_state->saveDSPCNTR = REG_READ(pipeA ? DSPACNTR : DSPBCNTR);
  761. crtc_state->savePIPECONF = REG_READ(pipeA ? PIPEACONF : PIPEBCONF);
  762. crtc_state->savePIPESRC = REG_READ(pipeA ? PIPEASRC : PIPEBSRC);
  763. crtc_state->saveFP0 = REG_READ(pipeA ? FPA0 : FPB0);
  764. crtc_state->saveFP1 = REG_READ(pipeA ? FPA1 : FPB1);
  765. crtc_state->saveDPLL = REG_READ(pipeA ? DPLL_A : DPLL_B);
  766. crtc_state->saveHTOTAL = REG_READ(pipeA ? HTOTAL_A : HTOTAL_B);
  767. crtc_state->saveHBLANK = REG_READ(pipeA ? HBLANK_A : HBLANK_B);
  768. crtc_state->saveHSYNC = REG_READ(pipeA ? HSYNC_A : HSYNC_B);
  769. crtc_state->saveVTOTAL = REG_READ(pipeA ? VTOTAL_A : VTOTAL_B);
  770. crtc_state->saveVBLANK = REG_READ(pipeA ? VBLANK_A : VBLANK_B);
  771. crtc_state->saveVSYNC = REG_READ(pipeA ? VSYNC_A : VSYNC_B);
  772. crtc_state->saveDSPSTRIDE = REG_READ(pipeA ? DSPASTRIDE : DSPBSTRIDE);
  773. /*NOTE: DSPSIZE DSPPOS only for psb*/
  774. crtc_state->saveDSPSIZE = REG_READ(pipeA ? DSPASIZE : DSPBSIZE);
  775. crtc_state->saveDSPPOS = REG_READ(pipeA ? DSPAPOS : DSPBPOS);
  776. crtc_state->saveDSPBASE = REG_READ(pipeA ? DSPABASE : DSPBBASE);
  777. paletteReg = pipeA ? PALETTE_A : PALETTE_B;
  778. for (i = 0; i < 256; ++i)
  779. crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
  780. }
  781. /**
  782. * Restore HW states of giving crtc
  783. */
  784. static void psb_intel_crtc_restore(struct drm_crtc *crtc)
  785. {
  786. struct drm_device *dev = crtc->dev;
  787. /* struct drm_psb_private * dev_priv =
  788. (struct drm_psb_private *)dev->dev_private; */
  789. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  790. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  791. /* struct drm_crtc_helper_funcs * crtc_funcs = crtc->helper_private; */
  792. int pipeA = (psb_intel_crtc->pipe == 0);
  793. uint32_t paletteReg;
  794. int i;
  795. if (!crtc_state) {
  796. dev_err(dev->dev, "No crtc state\n");
  797. return;
  798. }
  799. if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
  800. REG_WRITE(pipeA ? DPLL_A : DPLL_B,
  801. crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
  802. REG_READ(pipeA ? DPLL_A : DPLL_B);
  803. udelay(150);
  804. }
  805. REG_WRITE(pipeA ? FPA0 : FPB0, crtc_state->saveFP0);
  806. REG_READ(pipeA ? FPA0 : FPB0);
  807. REG_WRITE(pipeA ? FPA1 : FPB1, crtc_state->saveFP1);
  808. REG_READ(pipeA ? FPA1 : FPB1);
  809. REG_WRITE(pipeA ? DPLL_A : DPLL_B, crtc_state->saveDPLL);
  810. REG_READ(pipeA ? DPLL_A : DPLL_B);
  811. udelay(150);
  812. REG_WRITE(pipeA ? HTOTAL_A : HTOTAL_B, crtc_state->saveHTOTAL);
  813. REG_WRITE(pipeA ? HBLANK_A : HBLANK_B, crtc_state->saveHBLANK);
  814. REG_WRITE(pipeA ? HSYNC_A : HSYNC_B, crtc_state->saveHSYNC);
  815. REG_WRITE(pipeA ? VTOTAL_A : VTOTAL_B, crtc_state->saveVTOTAL);
  816. REG_WRITE(pipeA ? VBLANK_A : VBLANK_B, crtc_state->saveVBLANK);
  817. REG_WRITE(pipeA ? VSYNC_A : VSYNC_B, crtc_state->saveVSYNC);
  818. REG_WRITE(pipeA ? DSPASTRIDE : DSPBSTRIDE, crtc_state->saveDSPSTRIDE);
  819. REG_WRITE(pipeA ? DSPASIZE : DSPBSIZE, crtc_state->saveDSPSIZE);
  820. REG_WRITE(pipeA ? DSPAPOS : DSPBPOS, crtc_state->saveDSPPOS);
  821. REG_WRITE(pipeA ? PIPEASRC : PIPEBSRC, crtc_state->savePIPESRC);
  822. REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
  823. REG_WRITE(pipeA ? PIPEACONF : PIPEBCONF, crtc_state->savePIPECONF);
  824. psb_intel_wait_for_vblank(dev);
  825. REG_WRITE(pipeA ? DSPACNTR : DSPBCNTR, crtc_state->saveDSPCNTR);
  826. REG_WRITE(pipeA ? DSPABASE : DSPBBASE, crtc_state->saveDSPBASE);
  827. psb_intel_wait_for_vblank(dev);
  828. paletteReg = pipeA ? PALETTE_A : PALETTE_B;
  829. for (i = 0; i < 256; ++i)
  830. REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
  831. }
  832. static int psb_intel_crtc_cursor_set(struct drm_crtc *crtc,
  833. struct drm_file *file_priv,
  834. uint32_t handle,
  835. uint32_t width, uint32_t height)
  836. {
  837. struct drm_device *dev = crtc->dev;
  838. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  839. int pipe = psb_intel_crtc->pipe;
  840. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  841. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  842. uint32_t temp;
  843. size_t addr = 0;
  844. struct gtt_range *gt;
  845. struct drm_gem_object *obj;
  846. int ret;
  847. /* if we want to turn of the cursor ignore width and height */
  848. if (!handle) {
  849. /* turn off the cursor */
  850. temp = CURSOR_MODE_DISABLE;
  851. if (gma_power_begin(dev, false)) {
  852. REG_WRITE(control, temp);
  853. REG_WRITE(base, 0);
  854. gma_power_end(dev);
  855. }
  856. /* Unpin the old GEM object */
  857. if (psb_intel_crtc->cursor_obj) {
  858. gt = container_of(psb_intel_crtc->cursor_obj,
  859. struct gtt_range, gem);
  860. psb_gtt_unpin(gt);
  861. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  862. psb_intel_crtc->cursor_obj = NULL;
  863. }
  864. return 0;
  865. }
  866. /* Currently we only support 64x64 cursors */
  867. if (width != 64 || height != 64) {
  868. dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
  869. return -EINVAL;
  870. }
  871. obj = drm_gem_object_lookup(dev, file_priv, handle);
  872. if (!obj)
  873. return -ENOENT;
  874. if (obj->size < width * height * 4) {
  875. dev_dbg(dev->dev, "buffer is to small\n");
  876. return -ENOMEM;
  877. }
  878. gt = container_of(obj, struct gtt_range, gem);
  879. /* Pin the memory into the GTT */
  880. ret = psb_gtt_pin(gt);
  881. if (ret) {
  882. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  883. return ret;
  884. }
  885. addr = gt->offset; /* Or resource.start ??? */
  886. psb_intel_crtc->cursor_addr = addr;
  887. temp = 0;
  888. /* set the pipe for the cursor */
  889. temp |= (pipe << 28);
  890. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  891. if (gma_power_begin(dev, false)) {
  892. REG_WRITE(control, temp);
  893. REG_WRITE(base, addr);
  894. gma_power_end(dev);
  895. }
  896. /* unpin the old bo */
  897. if (psb_intel_crtc->cursor_obj) {
  898. gt = container_of(psb_intel_crtc->cursor_obj,
  899. struct gtt_range, gem);
  900. psb_gtt_unpin(gt);
  901. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  902. psb_intel_crtc->cursor_obj = obj;
  903. }
  904. return 0;
  905. }
  906. static int psb_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  907. {
  908. struct drm_device *dev = crtc->dev;
  909. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  910. int pipe = psb_intel_crtc->pipe;
  911. uint32_t temp = 0;
  912. uint32_t addr;
  913. if (x < 0) {
  914. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  915. x = -x;
  916. }
  917. if (y < 0) {
  918. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  919. y = -y;
  920. }
  921. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  922. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  923. addr = psb_intel_crtc->cursor_addr;
  924. if (gma_power_begin(dev, false)) {
  925. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  926. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, addr);
  927. gma_power_end(dev);
  928. }
  929. return 0;
  930. }
  931. void psb_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
  932. u16 *green, u16 *blue, uint32_t type, uint32_t size)
  933. {
  934. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  935. int i;
  936. if (size != 256)
  937. return;
  938. for (i = 0; i < 256; i++) {
  939. psb_intel_crtc->lut_r[i] = red[i] >> 8;
  940. psb_intel_crtc->lut_g[i] = green[i] >> 8;
  941. psb_intel_crtc->lut_b[i] = blue[i] >> 8;
  942. }
  943. psb_intel_crtc_load_lut(crtc);
  944. }
  945. static int psb_crtc_set_config(struct drm_mode_set *set)
  946. {
  947. int ret;
  948. struct drm_device *dev = set->crtc->dev;
  949. struct drm_psb_private *dev_priv = dev->dev_private;
  950. if (!dev_priv->rpm_enabled)
  951. return drm_crtc_helper_set_config(set);
  952. pm_runtime_forbid(&dev->pdev->dev);
  953. ret = drm_crtc_helper_set_config(set);
  954. pm_runtime_allow(&dev->pdev->dev);
  955. return ret;
  956. }
  957. /* Returns the clock of the currently programmed mode of the given pipe. */
  958. static int psb_intel_crtc_clock_get(struct drm_device *dev,
  959. struct drm_crtc *crtc)
  960. {
  961. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  962. int pipe = psb_intel_crtc->pipe;
  963. u32 dpll;
  964. u32 fp;
  965. struct psb_intel_clock_t clock;
  966. bool is_lvds;
  967. struct drm_psb_private *dev_priv = dev->dev_private;
  968. if (gma_power_begin(dev, false)) {
  969. dpll = REG_READ((pipe == 0) ? DPLL_A : DPLL_B);
  970. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  971. fp = REG_READ((pipe == 0) ? FPA0 : FPB0);
  972. else
  973. fp = REG_READ((pipe == 0) ? FPA1 : FPB1);
  974. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  975. gma_power_end(dev);
  976. } else {
  977. dpll = (pipe == 0) ?
  978. dev_priv->regs.psb.saveDPLL_A :
  979. dev_priv->regs.psb.saveDPLL_B;
  980. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  981. fp = (pipe == 0) ?
  982. dev_priv->regs.psb.saveFPA0 :
  983. dev_priv->regs.psb.saveFPB0;
  984. else
  985. fp = (pipe == 0) ?
  986. dev_priv->regs.psb.saveFPA1 :
  987. dev_priv->regs.psb.saveFPB1;
  988. is_lvds = (pipe == 1) && (dev_priv->regs.psb.saveLVDS &
  989. LVDS_PORT_EN);
  990. }
  991. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  992. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  993. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  994. if (is_lvds) {
  995. clock.p1 =
  996. ffs((dpll &
  997. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  998. DPLL_FPA01_P1_POST_DIV_SHIFT);
  999. clock.p2 = 14;
  1000. if ((dpll & PLL_REF_INPUT_MASK) ==
  1001. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1002. /* XXX: might not be 66MHz */
  1003. i8xx_clock(66000, &clock);
  1004. } else
  1005. i8xx_clock(48000, &clock);
  1006. } else {
  1007. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1008. clock.p1 = 2;
  1009. else {
  1010. clock.p1 =
  1011. ((dpll &
  1012. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1013. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1014. }
  1015. if (dpll & PLL_P2_DIVIDE_BY_4)
  1016. clock.p2 = 4;
  1017. else
  1018. clock.p2 = 2;
  1019. i8xx_clock(48000, &clock);
  1020. }
  1021. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1022. * i830PllIsValid() because it relies on the xf86_config connector
  1023. * configuration being accurate, which it isn't necessarily.
  1024. */
  1025. return clock.dot;
  1026. }
  1027. /** Returns the currently programmed mode of the given pipe. */
  1028. struct drm_display_mode *psb_intel_crtc_mode_get(struct drm_device *dev,
  1029. struct drm_crtc *crtc)
  1030. {
  1031. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1032. int pipe = psb_intel_crtc->pipe;
  1033. struct drm_display_mode *mode;
  1034. int htot;
  1035. int hsync;
  1036. int vtot;
  1037. int vsync;
  1038. struct drm_psb_private *dev_priv = dev->dev_private;
  1039. if (gma_power_begin(dev, false)) {
  1040. htot = REG_READ((pipe == 0) ? HTOTAL_A : HTOTAL_B);
  1041. hsync = REG_READ((pipe == 0) ? HSYNC_A : HSYNC_B);
  1042. vtot = REG_READ((pipe == 0) ? VTOTAL_A : VTOTAL_B);
  1043. vsync = REG_READ((pipe == 0) ? VSYNC_A : VSYNC_B);
  1044. gma_power_end(dev);
  1045. } else {
  1046. htot = (pipe == 0) ?
  1047. dev_priv->regs.psb.saveHTOTAL_A :
  1048. dev_priv->regs.psb.saveHTOTAL_B;
  1049. hsync = (pipe == 0) ?
  1050. dev_priv->regs.psb.saveHSYNC_A :
  1051. dev_priv->regs.psb.saveHSYNC_B;
  1052. vtot = (pipe == 0) ?
  1053. dev_priv->regs.psb.saveVTOTAL_A :
  1054. dev_priv->regs.psb.saveVTOTAL_B;
  1055. vsync = (pipe == 0) ?
  1056. dev_priv->regs.psb.saveVSYNC_A :
  1057. dev_priv->regs.psb.saveVSYNC_B;
  1058. }
  1059. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1060. if (!mode)
  1061. return NULL;
  1062. mode->clock = psb_intel_crtc_clock_get(dev, crtc);
  1063. mode->hdisplay = (htot & 0xffff) + 1;
  1064. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1065. mode->hsync_start = (hsync & 0xffff) + 1;
  1066. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1067. mode->vdisplay = (vtot & 0xffff) + 1;
  1068. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1069. mode->vsync_start = (vsync & 0xffff) + 1;
  1070. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1071. drm_mode_set_name(mode);
  1072. drm_mode_set_crtcinfo(mode, 0);
  1073. return mode;
  1074. }
  1075. void psb_intel_crtc_destroy(struct drm_crtc *crtc)
  1076. {
  1077. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1078. struct gtt_range *gt;
  1079. /* Unpin the old GEM object */
  1080. if (psb_intel_crtc->cursor_obj) {
  1081. gt = container_of(psb_intel_crtc->cursor_obj,
  1082. struct gtt_range, gem);
  1083. psb_gtt_unpin(gt);
  1084. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1085. psb_intel_crtc->cursor_obj = NULL;
  1086. }
  1087. kfree(psb_intel_crtc->crtc_state);
  1088. drm_crtc_cleanup(crtc);
  1089. kfree(psb_intel_crtc);
  1090. }
  1091. static void psb_intel_crtc_disable(struct drm_crtc *crtc)
  1092. {
  1093. struct gtt_range *gt;
  1094. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1095. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  1096. if (crtc->fb) {
  1097. gt = to_psb_fb(crtc->fb)->gtt;
  1098. psb_gtt_unpin(gt);
  1099. }
  1100. }
  1101. const struct drm_crtc_helper_funcs psb_intel_helper_funcs = {
  1102. .dpms = psb_intel_crtc_dpms,
  1103. .mode_fixup = psb_intel_crtc_mode_fixup,
  1104. .mode_set = psb_intel_crtc_mode_set,
  1105. .mode_set_base = psb_intel_pipe_set_base,
  1106. .prepare = psb_intel_crtc_prepare,
  1107. .commit = psb_intel_crtc_commit,
  1108. .disable = psb_intel_crtc_disable,
  1109. };
  1110. const struct drm_crtc_funcs psb_intel_crtc_funcs = {
  1111. .save = psb_intel_crtc_save,
  1112. .restore = psb_intel_crtc_restore,
  1113. .cursor_set = psb_intel_crtc_cursor_set,
  1114. .cursor_move = psb_intel_crtc_cursor_move,
  1115. .gamma_set = psb_intel_crtc_gamma_set,
  1116. .set_config = psb_crtc_set_config,
  1117. .destroy = psb_intel_crtc_destroy,
  1118. };
  1119. /*
  1120. * Set the default value of cursor control and base register
  1121. * to zero. This is a workaround for h/w defect on Oaktrail
  1122. */
  1123. static void psb_intel_cursor_init(struct drm_device *dev, int pipe)
  1124. {
  1125. u32 control[3] = { CURACNTR, CURBCNTR, CURCCNTR };
  1126. u32 base[3] = { CURABASE, CURBBASE, CURCBASE };
  1127. REG_WRITE(control[pipe], 0);
  1128. REG_WRITE(base[pipe], 0);
  1129. }
  1130. void psb_intel_crtc_init(struct drm_device *dev, int pipe,
  1131. struct psb_intel_mode_device *mode_dev)
  1132. {
  1133. struct drm_psb_private *dev_priv = dev->dev_private;
  1134. struct psb_intel_crtc *psb_intel_crtc;
  1135. int i;
  1136. uint16_t *r_base, *g_base, *b_base;
  1137. /* We allocate a extra array of drm_connector pointers
  1138. * for fbdev after the crtc */
  1139. psb_intel_crtc =
  1140. kzalloc(sizeof(struct psb_intel_crtc) +
  1141. (INTELFB_CONN_LIMIT * sizeof(struct drm_connector *)),
  1142. GFP_KERNEL);
  1143. if (psb_intel_crtc == NULL)
  1144. return;
  1145. psb_intel_crtc->crtc_state =
  1146. kzalloc(sizeof(struct psb_intel_crtc_state), GFP_KERNEL);
  1147. if (!psb_intel_crtc->crtc_state) {
  1148. dev_err(dev->dev, "Crtc state error: No memory\n");
  1149. kfree(psb_intel_crtc);
  1150. return;
  1151. }
  1152. /* Set the CRTC operations from the chip specific data */
  1153. drm_crtc_init(dev, &psb_intel_crtc->base, dev_priv->ops->crtc_funcs);
  1154. drm_mode_crtc_set_gamma_size(&psb_intel_crtc->base, 256);
  1155. psb_intel_crtc->pipe = pipe;
  1156. psb_intel_crtc->plane = pipe;
  1157. r_base = psb_intel_crtc->base.gamma_store;
  1158. g_base = r_base + 256;
  1159. b_base = g_base + 256;
  1160. for (i = 0; i < 256; i++) {
  1161. psb_intel_crtc->lut_r[i] = i;
  1162. psb_intel_crtc->lut_g[i] = i;
  1163. psb_intel_crtc->lut_b[i] = i;
  1164. r_base[i] = i << 8;
  1165. g_base[i] = i << 8;
  1166. b_base[i] = i << 8;
  1167. psb_intel_crtc->lut_adj[i] = 0;
  1168. }
  1169. psb_intel_crtc->mode_dev = mode_dev;
  1170. psb_intel_crtc->cursor_addr = 0;
  1171. drm_crtc_helper_add(&psb_intel_crtc->base,
  1172. dev_priv->ops->crtc_helper);
  1173. /* Setup the array of drm_connector pointer array */
  1174. psb_intel_crtc->mode_set.crtc = &psb_intel_crtc->base;
  1175. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  1176. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] != NULL);
  1177. dev_priv->plane_to_crtc_mapping[psb_intel_crtc->plane] =
  1178. &psb_intel_crtc->base;
  1179. dev_priv->pipe_to_crtc_mapping[psb_intel_crtc->pipe] =
  1180. &psb_intel_crtc->base;
  1181. psb_intel_crtc->mode_set.connectors =
  1182. (struct drm_connector **) (psb_intel_crtc + 1);
  1183. psb_intel_crtc->mode_set.num_connectors = 0;
  1184. psb_intel_cursor_init(dev, pipe);
  1185. }
  1186. int psb_intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  1187. struct drm_file *file_priv)
  1188. {
  1189. struct drm_psb_private *dev_priv = dev->dev_private;
  1190. struct drm_psb_get_pipe_from_crtc_id_arg *pipe_from_crtc_id = data;
  1191. struct drm_mode_object *drmmode_obj;
  1192. struct psb_intel_crtc *crtc;
  1193. if (!dev_priv) {
  1194. dev_err(dev->dev, "called with no initialization\n");
  1195. return -EINVAL;
  1196. }
  1197. drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
  1198. DRM_MODE_OBJECT_CRTC);
  1199. if (!drmmode_obj) {
  1200. dev_err(dev->dev, "no such CRTC id\n");
  1201. return -EINVAL;
  1202. }
  1203. crtc = to_psb_intel_crtc(obj_to_crtc(drmmode_obj));
  1204. pipe_from_crtc_id->pipe = crtc->pipe;
  1205. return 0;
  1206. }
  1207. struct drm_crtc *psb_intel_get_crtc_from_pipe(struct drm_device *dev, int pipe)
  1208. {
  1209. struct drm_crtc *crtc = NULL;
  1210. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1211. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1212. if (psb_intel_crtc->pipe == pipe)
  1213. break;
  1214. }
  1215. return crtc;
  1216. }
  1217. int psb_intel_connector_clones(struct drm_device *dev, int type_mask)
  1218. {
  1219. int index_mask = 0;
  1220. struct drm_connector *connector;
  1221. int entry = 0;
  1222. list_for_each_entry(connector, &dev->mode_config.connector_list,
  1223. head) {
  1224. struct psb_intel_encoder *psb_intel_encoder =
  1225. psb_intel_attached_encoder(connector);
  1226. if (type_mask & (1 << psb_intel_encoder->type))
  1227. index_mask |= (1 << entry);
  1228. entry++;
  1229. }
  1230. return index_mask;
  1231. }
  1232. /* current intel driver doesn't take advantage of encoders
  1233. always give back the encoder for the connector
  1234. */
  1235. struct drm_encoder *psb_intel_best_encoder(struct drm_connector *connector)
  1236. {
  1237. struct psb_intel_encoder *psb_intel_encoder =
  1238. psb_intel_attached_encoder(connector);
  1239. return &psb_intel_encoder->base;
  1240. }
  1241. void psb_intel_connector_attach_encoder(struct psb_intel_connector *connector,
  1242. struct psb_intel_encoder *encoder)
  1243. {
  1244. connector->encoder = encoder;
  1245. drm_mode_connector_attach_encoder(&connector->base,
  1246. &encoder->base);
  1247. }