cdv_intel_display.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  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. return;
  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_update_watermark(dev, crtc);
  712. cdv_intel_crtc_load_lut(crtc);
  713. /* Give the overlay scaler a chance to enable
  714. * if it's on this pipe */
  715. /* psb_intel_crtc_dpms_video(crtc, true); TODO */
  716. psb_intel_crtc->crtc_enable = true;
  717. break;
  718. case DRM_MODE_DPMS_OFF:
  719. if (!psb_intel_crtc->active)
  720. return;
  721. psb_intel_crtc->active = false;
  722. /* Give the overlay scaler a chance to disable
  723. * if it's on this pipe */
  724. /* psb_intel_crtc_dpms_video(crtc, FALSE); TODO */
  725. /* Disable the VGA plane that we never use */
  726. REG_WRITE(VGACNTRL, VGA_DISP_DISABLE);
  727. /* Jim Bish - changed pipe/plane here as well. */
  728. drm_vblank_off(dev, pipe);
  729. /* Wait for vblank for the disable to take effect */
  730. cdv_intel_wait_for_vblank(dev);
  731. /* Next, disable display pipes */
  732. temp = REG_READ(map->conf);
  733. if ((temp & PIPEACONF_ENABLE) != 0) {
  734. REG_WRITE(map->conf, temp & ~PIPEACONF_ENABLE);
  735. REG_READ(map->conf);
  736. }
  737. /* Wait for vblank for the disable to take effect. */
  738. cdv_intel_wait_for_vblank(dev);
  739. udelay(150);
  740. /* Disable display plane */
  741. temp = REG_READ(map->cntr);
  742. if ((temp & DISPLAY_PLANE_ENABLE) != 0) {
  743. REG_WRITE(map->cntr,
  744. temp & ~DISPLAY_PLANE_ENABLE);
  745. /* Flush the plane changes */
  746. REG_WRITE(map->base, REG_READ(map->base));
  747. REG_READ(map->base);
  748. }
  749. temp = REG_READ(map->dpll);
  750. if ((temp & DPLL_VCO_ENABLE) != 0) {
  751. REG_WRITE(map->dpll, temp & ~DPLL_VCO_ENABLE);
  752. REG_READ(map->dpll);
  753. }
  754. /* Wait for the clocks to turn off. */
  755. udelay(150);
  756. cdv_intel_update_watermark(dev, crtc);
  757. psb_intel_crtc->crtc_enable = false;
  758. break;
  759. }
  760. /*Set FIFO Watermarks*/
  761. REG_WRITE(DSPARB, 0x3F3E);
  762. }
  763. static void cdv_intel_crtc_prepare(struct drm_crtc *crtc)
  764. {
  765. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  766. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
  767. }
  768. static void cdv_intel_crtc_commit(struct drm_crtc *crtc)
  769. {
  770. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  771. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  772. }
  773. static bool cdv_intel_crtc_mode_fixup(struct drm_crtc *crtc,
  774. struct drm_display_mode *mode,
  775. struct drm_display_mode *adjusted_mode)
  776. {
  777. return true;
  778. }
  779. /**
  780. * Return the pipe currently connected to the panel fitter,
  781. * or -1 if the panel fitter is not present or not in use
  782. */
  783. static int cdv_intel_panel_fitter_pipe(struct drm_device *dev)
  784. {
  785. u32 pfit_control;
  786. pfit_control = REG_READ(PFIT_CONTROL);
  787. /* See if the panel fitter is in use */
  788. if ((pfit_control & PFIT_ENABLE) == 0)
  789. return -1;
  790. return (pfit_control >> 29) & 0x3;
  791. }
  792. static int cdv_intel_crtc_mode_set(struct drm_crtc *crtc,
  793. struct drm_display_mode *mode,
  794. struct drm_display_mode *adjusted_mode,
  795. int x, int y,
  796. struct drm_framebuffer *old_fb)
  797. {
  798. struct drm_device *dev = crtc->dev;
  799. struct drm_psb_private *dev_priv = dev->dev_private;
  800. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  801. int pipe = psb_intel_crtc->pipe;
  802. const struct psb_offset *map = &dev_priv->regmap[pipe];
  803. int refclk;
  804. struct cdv_intel_clock_t clock;
  805. u32 dpll = 0, dspcntr, pipeconf;
  806. bool ok;
  807. bool is_crt = false, is_lvds = false, is_tv = false;
  808. bool is_hdmi = false;
  809. struct drm_mode_config *mode_config = &dev->mode_config;
  810. struct drm_connector *connector;
  811. list_for_each_entry(connector, &mode_config->connector_list, head) {
  812. struct psb_intel_encoder *psb_intel_encoder =
  813. psb_intel_attached_encoder(connector);
  814. if (!connector->encoder
  815. || connector->encoder->crtc != crtc)
  816. continue;
  817. switch (psb_intel_encoder->type) {
  818. case INTEL_OUTPUT_LVDS:
  819. is_lvds = true;
  820. break;
  821. case INTEL_OUTPUT_TVOUT:
  822. is_tv = true;
  823. break;
  824. case INTEL_OUTPUT_ANALOG:
  825. is_crt = true;
  826. break;
  827. case INTEL_OUTPUT_HDMI:
  828. is_hdmi = true;
  829. break;
  830. }
  831. }
  832. if (dev_priv->dplla_96mhz)
  833. /* low-end sku, 96/100 mhz */
  834. refclk = 96000;
  835. else
  836. /* high-end sku, 27/100 mhz */
  837. refclk = 27000;
  838. if (is_lvds && dev_priv->lvds_use_ssc) {
  839. refclk = dev_priv->lvds_ssc_freq * 1000;
  840. DRM_DEBUG_KMS("Use SSC reference clock %d Mhz\n", dev_priv->lvds_ssc_freq);
  841. }
  842. drm_mode_debug_printmodeline(adjusted_mode);
  843. ok = cdv_intel_find_best_PLL(crtc, adjusted_mode->clock, refclk,
  844. &clock);
  845. if (!ok) {
  846. dev_err(dev->dev, "Couldn't find PLL settings for mode!\n");
  847. return 0;
  848. }
  849. dpll = DPLL_VGA_MODE_DIS;
  850. if (is_tv) {
  851. /* XXX: just matching BIOS for now */
  852. /* dpll |= PLL_REF_INPUT_TVCLKINBC; */
  853. dpll |= 3;
  854. }
  855. /* dpll |= PLL_REF_INPUT_DREFCLK; */
  856. dpll |= DPLL_SYNCLOCK_ENABLE;
  857. /* if (is_lvds)
  858. dpll |= DPLLB_MODE_LVDS;
  859. else
  860. dpll |= DPLLB_MODE_DAC_SERIAL; */
  861. /* dpll |= (2 << 11); */
  862. /* setup pipeconf */
  863. pipeconf = REG_READ(map->conf);
  864. /* Set up the display plane register */
  865. dspcntr = DISPPLANE_GAMMA_ENABLE;
  866. if (pipe == 0)
  867. dspcntr |= DISPPLANE_SEL_PIPE_A;
  868. else
  869. dspcntr |= DISPPLANE_SEL_PIPE_B;
  870. dspcntr |= DISPLAY_PLANE_ENABLE;
  871. pipeconf |= PIPEACONF_ENABLE;
  872. REG_WRITE(map->dpll, dpll | DPLL_VGA_MODE_DIS | DPLL_SYNCLOCK_ENABLE);
  873. REG_READ(map->dpll);
  874. cdv_dpll_set_clock_cdv(dev, crtc, &clock, is_lvds);
  875. udelay(150);
  876. /* The LVDS pin pair needs to be on before the DPLLs are enabled.
  877. * This is an exception to the general rule that mode_set doesn't turn
  878. * things on.
  879. */
  880. if (is_lvds) {
  881. u32 lvds = REG_READ(LVDS);
  882. lvds |=
  883. LVDS_PORT_EN | LVDS_A0A2_CLKA_POWER_UP |
  884. LVDS_PIPEB_SELECT;
  885. /* Set the B0-B3 data pairs corresponding to
  886. * whether we're going to
  887. * set the DPLLs for dual-channel mode or not.
  888. */
  889. if (clock.p2 == 7)
  890. lvds |= LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP;
  891. else
  892. lvds &= ~(LVDS_B0B3_POWER_UP | LVDS_CLKB_POWER_UP);
  893. /* It would be nice to set 24 vs 18-bit mode (LVDS_A3_POWER_UP)
  894. * appropriately here, but we need to look more
  895. * thoroughly into how panels behave in the two modes.
  896. */
  897. REG_WRITE(LVDS, lvds);
  898. REG_READ(LVDS);
  899. }
  900. dpll |= DPLL_VCO_ENABLE;
  901. /* Disable the panel fitter if it was on our pipe */
  902. if (cdv_intel_panel_fitter_pipe(dev) == pipe)
  903. REG_WRITE(PFIT_CONTROL, 0);
  904. DRM_DEBUG_KMS("Mode for pipe %c:\n", pipe == 0 ? 'A' : 'B');
  905. drm_mode_debug_printmodeline(mode);
  906. REG_WRITE(map->dpll,
  907. (REG_READ(map->dpll) & ~DPLL_LOCK) | DPLL_VCO_ENABLE);
  908. REG_READ(map->dpll);
  909. /* Wait for the clocks to stabilize. */
  910. udelay(150); /* 42 usec w/o calibration, 110 with. rounded up. */
  911. if (!(REG_READ(map->dpll) & DPLL_LOCK)) {
  912. dev_err(dev->dev, "Failed to get DPLL lock\n");
  913. return -EBUSY;
  914. }
  915. {
  916. int sdvo_pixel_multiply = adjusted_mode->clock / mode->clock;
  917. REG_WRITE(map->dpll_md, (0 << DPLL_MD_UDI_DIVIDER_SHIFT) | ((sdvo_pixel_multiply - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT));
  918. }
  919. REG_WRITE(map->htotal, (adjusted_mode->crtc_hdisplay - 1) |
  920. ((adjusted_mode->crtc_htotal - 1) << 16));
  921. REG_WRITE(map->hblank, (adjusted_mode->crtc_hblank_start - 1) |
  922. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  923. REG_WRITE(map->hsync, (adjusted_mode->crtc_hsync_start - 1) |
  924. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  925. REG_WRITE(map->vtotal, (adjusted_mode->crtc_vdisplay - 1) |
  926. ((adjusted_mode->crtc_vtotal - 1) << 16));
  927. REG_WRITE(map->vblank, (adjusted_mode->crtc_vblank_start - 1) |
  928. ((adjusted_mode->crtc_vblank_end - 1) << 16));
  929. REG_WRITE(map->vsync, (adjusted_mode->crtc_vsync_start - 1) |
  930. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  931. /* pipesrc and dspsize control the size that is scaled from,
  932. * which should always be the user's requested size.
  933. */
  934. REG_WRITE(map->size,
  935. ((mode->vdisplay - 1) << 16) | (mode->hdisplay - 1));
  936. REG_WRITE(map->pos, 0);
  937. REG_WRITE(map->src,
  938. ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
  939. REG_WRITE(map->conf, pipeconf);
  940. REG_READ(map->conf);
  941. cdv_intel_wait_for_vblank(dev);
  942. REG_WRITE(map->cntr, dspcntr);
  943. /* Flush the plane changes */
  944. {
  945. struct drm_crtc_helper_funcs *crtc_funcs =
  946. crtc->helper_private;
  947. crtc_funcs->mode_set_base(crtc, x, y, old_fb);
  948. }
  949. cdv_intel_wait_for_vblank(dev);
  950. return 0;
  951. }
  952. /**
  953. * Save HW states of giving crtc
  954. */
  955. static void cdv_intel_crtc_save(struct drm_crtc *crtc)
  956. {
  957. struct drm_device *dev = crtc->dev;
  958. struct drm_psb_private *dev_priv = dev->dev_private;
  959. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  960. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  961. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  962. uint32_t paletteReg;
  963. int i;
  964. if (!crtc_state) {
  965. dev_dbg(dev->dev, "No CRTC state found\n");
  966. return;
  967. }
  968. crtc_state->saveDSPCNTR = REG_READ(map->cntr);
  969. crtc_state->savePIPECONF = REG_READ(map->conf);
  970. crtc_state->savePIPESRC = REG_READ(map->src);
  971. crtc_state->saveFP0 = REG_READ(map->fp0);
  972. crtc_state->saveFP1 = REG_READ(map->fp1);
  973. crtc_state->saveDPLL = REG_READ(map->dpll);
  974. crtc_state->saveHTOTAL = REG_READ(map->htotal);
  975. crtc_state->saveHBLANK = REG_READ(map->hblank);
  976. crtc_state->saveHSYNC = REG_READ(map->hsync);
  977. crtc_state->saveVTOTAL = REG_READ(map->vtotal);
  978. crtc_state->saveVBLANK = REG_READ(map->vblank);
  979. crtc_state->saveVSYNC = REG_READ(map->vsync);
  980. crtc_state->saveDSPSTRIDE = REG_READ(map->stride);
  981. /*NOTE: DSPSIZE DSPPOS only for psb*/
  982. crtc_state->saveDSPSIZE = REG_READ(map->size);
  983. crtc_state->saveDSPPOS = REG_READ(map->pos);
  984. crtc_state->saveDSPBASE = REG_READ(map->base);
  985. DRM_DEBUG("(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  986. crtc_state->saveDSPCNTR,
  987. crtc_state->savePIPECONF,
  988. crtc_state->savePIPESRC,
  989. crtc_state->saveFP0,
  990. crtc_state->saveFP1,
  991. crtc_state->saveDPLL,
  992. crtc_state->saveHTOTAL,
  993. crtc_state->saveHBLANK,
  994. crtc_state->saveHSYNC,
  995. crtc_state->saveVTOTAL,
  996. crtc_state->saveVBLANK,
  997. crtc_state->saveVSYNC,
  998. crtc_state->saveDSPSTRIDE,
  999. crtc_state->saveDSPSIZE,
  1000. crtc_state->saveDSPPOS,
  1001. crtc_state->saveDSPBASE
  1002. );
  1003. paletteReg = map->palette;
  1004. for (i = 0; i < 256; ++i)
  1005. crtc_state->savePalette[i] = REG_READ(paletteReg + (i << 2));
  1006. }
  1007. /**
  1008. * Restore HW states of giving crtc
  1009. */
  1010. static void cdv_intel_crtc_restore(struct drm_crtc *crtc)
  1011. {
  1012. struct drm_device *dev = crtc->dev;
  1013. struct drm_psb_private *dev_priv = dev->dev_private;
  1014. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1015. struct psb_intel_crtc_state *crtc_state = psb_intel_crtc->crtc_state;
  1016. const struct psb_offset *map = &dev_priv->regmap[psb_intel_crtc->pipe];
  1017. uint32_t paletteReg;
  1018. int i;
  1019. if (!crtc_state) {
  1020. dev_dbg(dev->dev, "No crtc state\n");
  1021. return;
  1022. }
  1023. DRM_DEBUG(
  1024. "current:(%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  1025. REG_READ(map->cntr),
  1026. REG_READ(map->conf),
  1027. REG_READ(map->src),
  1028. REG_READ(map->fp0),
  1029. REG_READ(map->fp1),
  1030. REG_READ(map->dpll),
  1031. REG_READ(map->htotal),
  1032. REG_READ(map->hblank),
  1033. REG_READ(map->hsync),
  1034. REG_READ(map->vtotal),
  1035. REG_READ(map->vblank),
  1036. REG_READ(map->vsync),
  1037. REG_READ(map->stride),
  1038. REG_READ(map->size),
  1039. REG_READ(map->pos),
  1040. REG_READ(map->base)
  1041. );
  1042. DRM_DEBUG(
  1043. "saved: (%x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x)\n",
  1044. crtc_state->saveDSPCNTR,
  1045. crtc_state->savePIPECONF,
  1046. crtc_state->savePIPESRC,
  1047. crtc_state->saveFP0,
  1048. crtc_state->saveFP1,
  1049. crtc_state->saveDPLL,
  1050. crtc_state->saveHTOTAL,
  1051. crtc_state->saveHBLANK,
  1052. crtc_state->saveHSYNC,
  1053. crtc_state->saveVTOTAL,
  1054. crtc_state->saveVBLANK,
  1055. crtc_state->saveVSYNC,
  1056. crtc_state->saveDSPSTRIDE,
  1057. crtc_state->saveDSPSIZE,
  1058. crtc_state->saveDSPPOS,
  1059. crtc_state->saveDSPBASE
  1060. );
  1061. if (crtc_state->saveDPLL & DPLL_VCO_ENABLE) {
  1062. REG_WRITE(map->dpll,
  1063. crtc_state->saveDPLL & ~DPLL_VCO_ENABLE);
  1064. REG_READ(map->dpll);
  1065. DRM_DEBUG("write dpll: %x\n",
  1066. REG_READ(map->dpll));
  1067. udelay(150);
  1068. }
  1069. REG_WRITE(map->fp0, crtc_state->saveFP0);
  1070. REG_READ(map->fp0);
  1071. REG_WRITE(map->fp1, crtc_state->saveFP1);
  1072. REG_READ(map->fp1);
  1073. REG_WRITE(map->dpll, crtc_state->saveDPLL);
  1074. REG_READ(map->dpll);
  1075. udelay(150);
  1076. REG_WRITE(map->htotal, crtc_state->saveHTOTAL);
  1077. REG_WRITE(map->hblank, crtc_state->saveHBLANK);
  1078. REG_WRITE(map->hsync, crtc_state->saveHSYNC);
  1079. REG_WRITE(map->vtotal, crtc_state->saveVTOTAL);
  1080. REG_WRITE(map->vblank, crtc_state->saveVBLANK);
  1081. REG_WRITE(map->vsync, crtc_state->saveVSYNC);
  1082. REG_WRITE(map->stride, crtc_state->saveDSPSTRIDE);
  1083. REG_WRITE(map->size, crtc_state->saveDSPSIZE);
  1084. REG_WRITE(map->pos, crtc_state->saveDSPPOS);
  1085. REG_WRITE(map->src, crtc_state->savePIPESRC);
  1086. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  1087. REG_WRITE(map->conf, crtc_state->savePIPECONF);
  1088. cdv_intel_wait_for_vblank(dev);
  1089. REG_WRITE(map->cntr, crtc_state->saveDSPCNTR);
  1090. REG_WRITE(map->base, crtc_state->saveDSPBASE);
  1091. cdv_intel_wait_for_vblank(dev);
  1092. paletteReg = map->palette;
  1093. for (i = 0; i < 256; ++i)
  1094. REG_WRITE(paletteReg + (i << 2), crtc_state->savePalette[i]);
  1095. }
  1096. static int cdv_intel_crtc_cursor_set(struct drm_crtc *crtc,
  1097. struct drm_file *file_priv,
  1098. uint32_t handle,
  1099. uint32_t width, uint32_t height)
  1100. {
  1101. struct drm_device *dev = crtc->dev;
  1102. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1103. int pipe = psb_intel_crtc->pipe;
  1104. uint32_t control = (pipe == 0) ? CURACNTR : CURBCNTR;
  1105. uint32_t base = (pipe == 0) ? CURABASE : CURBBASE;
  1106. uint32_t temp;
  1107. size_t addr = 0;
  1108. struct gtt_range *gt;
  1109. struct drm_gem_object *obj;
  1110. int ret;
  1111. /* if we want to turn of the cursor ignore width and height */
  1112. if (!handle) {
  1113. /* turn off the cursor */
  1114. temp = CURSOR_MODE_DISABLE;
  1115. if (gma_power_begin(dev, false)) {
  1116. REG_WRITE(control, temp);
  1117. REG_WRITE(base, 0);
  1118. gma_power_end(dev);
  1119. }
  1120. /* unpin the old GEM object */
  1121. if (psb_intel_crtc->cursor_obj) {
  1122. gt = container_of(psb_intel_crtc->cursor_obj,
  1123. struct gtt_range, gem);
  1124. psb_gtt_unpin(gt);
  1125. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1126. psb_intel_crtc->cursor_obj = NULL;
  1127. }
  1128. return 0;
  1129. }
  1130. /* Currently we only support 64x64 cursors */
  1131. if (width != 64 || height != 64) {
  1132. dev_dbg(dev->dev, "we currently only support 64x64 cursors\n");
  1133. return -EINVAL;
  1134. }
  1135. obj = drm_gem_object_lookup(dev, file_priv, handle);
  1136. if (!obj)
  1137. return -ENOENT;
  1138. if (obj->size < width * height * 4) {
  1139. dev_dbg(dev->dev, "buffer is to small\n");
  1140. return -ENOMEM;
  1141. }
  1142. gt = container_of(obj, struct gtt_range, gem);
  1143. /* Pin the memory into the GTT */
  1144. ret = psb_gtt_pin(gt);
  1145. if (ret) {
  1146. dev_err(dev->dev, "Can not pin down handle 0x%x\n", handle);
  1147. return ret;
  1148. }
  1149. addr = gt->offset; /* Or resource.start ??? */
  1150. psb_intel_crtc->cursor_addr = addr;
  1151. temp = 0;
  1152. /* set the pipe for the cursor */
  1153. temp |= (pipe << 28);
  1154. temp |= CURSOR_MODE_64_ARGB_AX | MCURSOR_GAMMA_ENABLE;
  1155. if (gma_power_begin(dev, false)) {
  1156. REG_WRITE(control, temp);
  1157. REG_WRITE(base, addr);
  1158. gma_power_end(dev);
  1159. }
  1160. /* unpin the old GEM object */
  1161. if (psb_intel_crtc->cursor_obj) {
  1162. gt = container_of(psb_intel_crtc->cursor_obj,
  1163. struct gtt_range, gem);
  1164. psb_gtt_unpin(gt);
  1165. drm_gem_object_unreference(psb_intel_crtc->cursor_obj);
  1166. psb_intel_crtc->cursor_obj = obj;
  1167. }
  1168. return 0;
  1169. }
  1170. static int cdv_intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  1171. {
  1172. struct drm_device *dev = crtc->dev;
  1173. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1174. int pipe = psb_intel_crtc->pipe;
  1175. uint32_t temp = 0;
  1176. uint32_t adder;
  1177. if (x < 0) {
  1178. temp |= (CURSOR_POS_SIGN << CURSOR_X_SHIFT);
  1179. x = -x;
  1180. }
  1181. if (y < 0) {
  1182. temp |= (CURSOR_POS_SIGN << CURSOR_Y_SHIFT);
  1183. y = -y;
  1184. }
  1185. temp |= ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT);
  1186. temp |= ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
  1187. adder = psb_intel_crtc->cursor_addr;
  1188. if (gma_power_begin(dev, false)) {
  1189. REG_WRITE((pipe == 0) ? CURAPOS : CURBPOS, temp);
  1190. REG_WRITE((pipe == 0) ? CURABASE : CURBBASE, adder);
  1191. gma_power_end(dev);
  1192. }
  1193. return 0;
  1194. }
  1195. static void cdv_intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red,
  1196. u16 *green, u16 *blue, uint32_t start, uint32_t size)
  1197. {
  1198. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1199. int i;
  1200. int end = (start + size > 256) ? 256 : start + size;
  1201. for (i = start; i < end; i++) {
  1202. psb_intel_crtc->lut_r[i] = red[i] >> 8;
  1203. psb_intel_crtc->lut_g[i] = green[i] >> 8;
  1204. psb_intel_crtc->lut_b[i] = blue[i] >> 8;
  1205. }
  1206. cdv_intel_crtc_load_lut(crtc);
  1207. }
  1208. static int cdv_crtc_set_config(struct drm_mode_set *set)
  1209. {
  1210. int ret = 0;
  1211. struct drm_device *dev = set->crtc->dev;
  1212. struct drm_psb_private *dev_priv = dev->dev_private;
  1213. if (!dev_priv->rpm_enabled)
  1214. return drm_crtc_helper_set_config(set);
  1215. pm_runtime_forbid(&dev->pdev->dev);
  1216. ret = drm_crtc_helper_set_config(set);
  1217. pm_runtime_allow(&dev->pdev->dev);
  1218. return ret;
  1219. }
  1220. /** Derive the pixel clock for the given refclk and divisors for 8xx chips. */
  1221. /* FIXME: why are we using this, should it be cdv_ in this tree ? */
  1222. static void i8xx_clock(int refclk, struct cdv_intel_clock_t *clock)
  1223. {
  1224. clock->m = 5 * (clock->m1 + 2) + (clock->m2 + 2);
  1225. clock->p = clock->p1 * clock->p2;
  1226. clock->vco = refclk * clock->m / (clock->n + 2);
  1227. clock->dot = clock->vco / clock->p;
  1228. }
  1229. /* Returns the clock of the currently programmed mode of the given pipe. */
  1230. static int cdv_intel_crtc_clock_get(struct drm_device *dev,
  1231. struct drm_crtc *crtc)
  1232. {
  1233. struct drm_psb_private *dev_priv = dev->dev_private;
  1234. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1235. int pipe = psb_intel_crtc->pipe;
  1236. const struct psb_offset *map = &dev_priv->regmap[pipe];
  1237. u32 dpll;
  1238. u32 fp;
  1239. struct cdv_intel_clock_t clock;
  1240. bool is_lvds;
  1241. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  1242. if (gma_power_begin(dev, false)) {
  1243. dpll = REG_READ(map->dpll);
  1244. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1245. fp = REG_READ(map->fp0);
  1246. else
  1247. fp = REG_READ(map->fp1);
  1248. is_lvds = (pipe == 1) && (REG_READ(LVDS) & LVDS_PORT_EN);
  1249. gma_power_end(dev);
  1250. } else {
  1251. dpll = p->dpll;
  1252. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  1253. fp = p->fp0;
  1254. else
  1255. fp = p->fp1;
  1256. is_lvds = (pipe == 1) &&
  1257. (dev_priv->regs.psb.saveLVDS & LVDS_PORT_EN);
  1258. }
  1259. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  1260. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  1261. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  1262. if (is_lvds) {
  1263. clock.p1 =
  1264. ffs((dpll &
  1265. DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  1266. DPLL_FPA01_P1_POST_DIV_SHIFT);
  1267. if (clock.p1 == 0) {
  1268. clock.p1 = 4;
  1269. dev_err(dev->dev, "PLL %d\n", dpll);
  1270. }
  1271. clock.p2 = 14;
  1272. if ((dpll & PLL_REF_INPUT_MASK) ==
  1273. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  1274. /* XXX: might not be 66MHz */
  1275. i8xx_clock(66000, &clock);
  1276. } else
  1277. i8xx_clock(48000, &clock);
  1278. } else {
  1279. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  1280. clock.p1 = 2;
  1281. else {
  1282. clock.p1 =
  1283. ((dpll &
  1284. DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  1285. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  1286. }
  1287. if (dpll & PLL_P2_DIVIDE_BY_4)
  1288. clock.p2 = 4;
  1289. else
  1290. clock.p2 = 2;
  1291. i8xx_clock(48000, &clock);
  1292. }
  1293. /* XXX: It would be nice to validate the clocks, but we can't reuse
  1294. * i830PllIsValid() because it relies on the xf86_config connector
  1295. * configuration being accurate, which it isn't necessarily.
  1296. */
  1297. return clock.dot;
  1298. }
  1299. /** Returns the currently programmed mode of the given pipe. */
  1300. struct drm_display_mode *cdv_intel_crtc_mode_get(struct drm_device *dev,
  1301. struct drm_crtc *crtc)
  1302. {
  1303. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1304. int pipe = psb_intel_crtc->pipe;
  1305. struct drm_psb_private *dev_priv = dev->dev_private;
  1306. struct psb_pipe *p = &dev_priv->regs.pipe[pipe];
  1307. const struct psb_offset *map = &dev_priv->regmap[pipe];
  1308. struct drm_display_mode *mode;
  1309. int htot;
  1310. int hsync;
  1311. int vtot;
  1312. int vsync;
  1313. if (gma_power_begin(dev, false)) {
  1314. htot = REG_READ(map->htotal);
  1315. hsync = REG_READ(map->hsync);
  1316. vtot = REG_READ(map->vtotal);
  1317. vsync = REG_READ(map->vsync);
  1318. gma_power_end(dev);
  1319. } else {
  1320. htot = p->htotal;
  1321. hsync = p->hsync;
  1322. vtot = p->vtotal;
  1323. vsync = p->vsync;
  1324. }
  1325. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  1326. if (!mode)
  1327. return NULL;
  1328. mode->clock = cdv_intel_crtc_clock_get(dev, crtc);
  1329. mode->hdisplay = (htot & 0xffff) + 1;
  1330. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  1331. mode->hsync_start = (hsync & 0xffff) + 1;
  1332. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  1333. mode->vdisplay = (vtot & 0xffff) + 1;
  1334. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  1335. mode->vsync_start = (vsync & 0xffff) + 1;
  1336. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  1337. drm_mode_set_name(mode);
  1338. drm_mode_set_crtcinfo(mode, 0);
  1339. return mode;
  1340. }
  1341. static void cdv_intel_crtc_destroy(struct drm_crtc *crtc)
  1342. {
  1343. struct psb_intel_crtc *psb_intel_crtc = to_psb_intel_crtc(crtc);
  1344. kfree(psb_intel_crtc->crtc_state);
  1345. drm_crtc_cleanup(crtc);
  1346. kfree(psb_intel_crtc);
  1347. }
  1348. const struct drm_crtc_helper_funcs cdv_intel_helper_funcs = {
  1349. .dpms = cdv_intel_crtc_dpms,
  1350. .mode_fixup = cdv_intel_crtc_mode_fixup,
  1351. .mode_set = cdv_intel_crtc_mode_set,
  1352. .mode_set_base = cdv_intel_pipe_set_base,
  1353. .prepare = cdv_intel_crtc_prepare,
  1354. .commit = cdv_intel_crtc_commit,
  1355. };
  1356. const struct drm_crtc_funcs cdv_intel_crtc_funcs = {
  1357. .save = cdv_intel_crtc_save,
  1358. .restore = cdv_intel_crtc_restore,
  1359. .cursor_set = cdv_intel_crtc_cursor_set,
  1360. .cursor_move = cdv_intel_crtc_cursor_move,
  1361. .gamma_set = cdv_intel_crtc_gamma_set,
  1362. .set_config = cdv_crtc_set_config,
  1363. .destroy = cdv_intel_crtc_destroy,
  1364. };