cdv_intel_display.c 46 KB

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