cdv_intel_display.c 42 KB

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