cdv_intel_display.c 41 KB

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