cdv_intel_display.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  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;
  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. return -ENOMEM;
  1288. }
  1289. gt = container_of(obj, struct gtt_range, gem);
  1290. /* Pin the memory into the GTT */
  1291. ret = psb_gtt_pin(gt);
  1292. if (ret) {
  1293. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  1294. return ret;
  1295. }
  1296. addr = gt->offset; /* Or resource.start ??? */
  1297. psb_intel_crtc->cursor_addr = addr;
  1298. temp = 0;
  1299. /* set the pipe for the cursor */
  1300. temp |= (pipe << 28);
  1301. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  1302. if (gma_power_begin(dev, false)) {
  1303. REG_WRITE(control, temp);
  1304. REG_WRITE(base, addr);
  1305. gma_power_end(dev);
  1306. }
  1307. /* unpin the old GEM object */
  1308. if (psb_intel_crtc->cursor_obj) {
  1309. gt = container_of(psb_intel_crtc->cursor_obj,
  1310. struct gtt_range, gem);
  1311. psb_gtt_unpin(gt);
  1312. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1313. psb_intel_crtc->cursor_obj = obj;
  1314. }
  1315. return 0;
  1316. }
  1317. static int cdv_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1318. {
  1319. struct drm_device *dev = crtc->dev;
  1320. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1321. int pipe = psb_intel_crtc->pipe;
  1322. uint32_t temp = 0;
  1323. uint32_t adder;
  1324. if (x < 0) {
  1325. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  1326. x = -x;
  1327. }
  1328. if (y < 0) {
  1329. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  1330. y = -y;
  1331. }
  1332. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  1333. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  1334. adder = psb_intel_crtc->cursor_addr;
  1335. if (gma_power_begin(dev, false)) {
  1336. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  1337. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  1338. gma_power_end(dev);
  1339. }
  1340. return 0;
  1341. }
  1342. static void cdv_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
  1343. u16 *green, u16 *blue, uint32_t start, uint32_t size)
  1344. {
  1345. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1346. int i;
  1347. int end = (start + size > 256) ? 256 : start + size;
  1348. for (i = start; i < end; i++) {
  1349. psb_intel_crtc->lut_r[i] = red[i] >> 8;
  1350. psb_intel_crtc->lut_g[i] = green[i] >> 8;
  1351. psb_intel_crtc->lut_b[i] = blue[i] >> 8;
  1352. }
  1353. cdv_intel_crtc_load_lut(crtc);
  1354. }
  1355. static int cdv_crtc_set_config(struct drm_mode_set *set)
  1356. {
  1357. int ret = 0;
  1358. struct drm_device *dev = set->crtc->dev;
  1359. struct drm_psb_private *dev_priv = dev->dev_private;
  1360. if (!dev_priv->rpm_enabled)
  1361. return drm_crtc_helper_set_config(set);
  1362. pm_runtime_forbid(&dev->pdev->dev);
  1363. ret = drm_crtc_helper_set_config(set);
  1364. pm_runtime_allow(&dev->pdev->dev);
  1365. return ret;
  1366. }
  1367. /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
  1368. /* FIXME: why are we using this, should it be cdv_ in this tree ? */
  1369. static void i8xx_clock(int refclk, struct cdv_intel_clock_t *clock)
  1370. {
  1371. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  1372. clock->p = clock->p1 * clock->p2;
  1373. clock->vco = refclk * clock->m / (clock->n + 2);
  1374. clock->dot = clock->vco / clock->p;
  1375. }
  1376. /* Returns the clock of the currently programmed mode of the given pipe. */
  1377. static int cdv_intel_crtc_clock_get(struct drm_device *dev,
  1378. struct drm_crtc *crtc)
  1379. {
  1380. struct drm_psb_private *dev_priv = dev->dev_private;
  1381. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1382. int pipe = psb_intel_crtc->pipe;
  1383. const struct psb_offset *map = &dev_priv->regmap[pipe];
  1384. u32 dpll;
  1385. u32 fp;
  1386. struct cdv_intel_clock_t clock;
  1387. bool is_lvds;
  1388. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  1389. if (gma_power_begin(dev, false)) {
  1390. dpll = REG_READ(map->dpll);
  1391. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1392. fp = REG_READ(map->fp0);
  1393. else
  1394. fp = REG_READ(map->fp1);
  1395. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  1396. gma_power_end(dev);
  1397. } else {
  1398. dpll = p->dpll;
  1399. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1400. fp = p->fp0;
  1401. else
  1402. fp = p->fp1;
  1403. is_lvds = (pipe == 1) &&
  1404. (dev_priv->regs.psb.saveLVDS & LVDS_PORT_EN);
  1405. }
  1406. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1407. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1408. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1409. if (is_lvds) {
  1410. clock.p1 =
  1411. ffs((dpll &
  1412. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1413. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1414. if (clock.p1 == 0) {
  1415. clock.p1 = 4;
  1416. dev_err(dev->dev, "PLL %d\n", dpll);
  1417. }
  1418. clock.p2 = 14;
  1419. if ((dpll & PLL_REF_INPUT_MASK) ==
  1420. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1421. /* XXX: might not be 66MHz */
  1422. i8xx_clock(66000, &clock);
  1423. } else
  1424. i8xx_clock(48000, &clock);
  1425. } else {
  1426. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1427. clock.p1 = 2;
  1428. else {
  1429. clock.p1 =
  1430. ((dpll &
  1431. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1432. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1433. }
  1434. if (dpll & PLL_P2_DIVIDE_BY_4)
  1435. clock.p2 = 4;
  1436. else
  1437. clock.p2 = 2;
  1438. i8xx_clock(48000, &clock);
  1439. }
  1440. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1441. * i830PllIsValid() because it relies on the xf86_config connector
  1442. * configuration being accurate, which it isn't necessarily.
  1443. */
  1444. return clock.dot;
  1445. }
  1446. /** Returns the currently programmed mode of the given pipe. */
  1447. struct drm_display_mode *cdv_intel_crtc_mode_get(struct drm_device *dev,
  1448. struct drm_crtc *crtc)
  1449. {
  1450. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1451. int pipe = psb_intel_crtc->pipe;
  1452. struct drm_psb_private *dev_priv = dev->dev_private;
  1453. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  1454. const struct psb_offset *map = &dev_priv->regmap[pipe];
  1455. struct drm_display_mode *mode;
  1456. int htot;
  1457. int hsync;
  1458. int vtot;
  1459. int vsync;
  1460. if (gma_power_begin(dev, false)) {
  1461. htot = REG_READ(map->htotal);
  1462. hsync = REG_READ(map->hsync);
  1463. vtot = REG_READ(map->vtotal);
  1464. vsync = REG_READ(map->vsync);
  1465. gma_power_end(dev);
  1466. } else {
  1467. htot = p->htotal;
  1468. hsync = p->hsync;
  1469. vtot = p->vtotal;
  1470. vsync = p->vsync;
  1471. }
  1472. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1473. if (!mode)
  1474. return NULL;
  1475. mode->clock = cdv_intel_crtc_clock_get(dev, crtc);
  1476. mode->hdisplay = (htot & 0xffff) + 1;
  1477. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1478. mode->hsync_start = (hsync & 0xffff) + 1;
  1479. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1480. mode->vdisplay = (vtot & 0xffff) + 1;
  1481. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1482. mode->vsync_start = (vsync & 0xffff) + 1;
  1483. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1484. drm_mode_set_name(mode);
  1485. drm_mode_set_crtcinfo(mode, 0);
  1486. return mode;
  1487. }
  1488. static void cdv_intel_crtc_destroy(struct drm_crtc *crtc)
  1489. {
  1490. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1491. kfree(psb_intel_crtc->crtc_state);
  1492. drm_crtc_cleanup(crtc);
  1493. kfree(psb_intel_crtc);
  1494. }
  1495. const struct drm_crtc_helper_funcs cdv_intel_helper_funcs = {
  1496. .dpms = cdv_intel_crtc_dpms,
  1497. .mode_fixup = cdv_intel_crtc_mode_fixup,
  1498. .mode_set = cdv_intel_crtc_mode_set,
  1499. .mode_set_base = cdv_intel_pipe_set_base,
  1500. .prepare = cdv_intel_crtc_prepare,
  1501. .commit = cdv_intel_crtc_commit,
  1502. };
  1503. const struct drm_crtc_funcs cdv_intel_crtc_funcs = {
  1504. .save = cdv_intel_crtc_save,
  1505. .restore = cdv_intel_crtc_restore,
  1506. .cursor_set = cdv_intel_crtc_cursor_set,
  1507. .cursor_move = cdv_intel_crtc_cursor_move,
  1508. .gamma_set = cdv_intel_crtc_gamma_set,
  1509. .set_config = cdv_crtc_set_config,
  1510. .destroy = cdv_intel_crtc_destroy,
  1511. };