mgag200_mode.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /*
  2. * Copyright 2010 Matt Turner.
  3. * Copyright 2012 Red Hat
  4. *
  5. * This file is subject to the terms and conditions of the GNU General
  6. * Public License version 2. See the file COPYING in the main
  7. * directory of this archive for more details.
  8. *
  9. * Authors: Matthew Garrett
  10. * Matt Turner
  11. * Dave Airlie
  12. */
  13. #include <linux/delay.h>
  14. #include "drmP.h"
  15. #include "drm.h"
  16. #include "drm_crtc_helper.h"
  17. #include "mgag200_drv.h"
  18. #define MGAG200_LUT_SIZE 256
  19. /*
  20. * This file contains setup code for the CRTC.
  21. */
  22. static void mga_crtc_load_lut(struct drm_crtc *crtc)
  23. {
  24. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  25. struct drm_device *dev = crtc->dev;
  26. struct mga_device *mdev = dev->dev_private;
  27. int i;
  28. if (!crtc->enabled)
  29. return;
  30. WREG8(DAC_INDEX + MGA1064_INDEX, 0);
  31. for (i = 0; i < MGAG200_LUT_SIZE; i++) {
  32. /* VGA registers */
  33. WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_r[i]);
  34. WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_g[i]);
  35. WREG8(DAC_INDEX + MGA1064_COL_PAL, mga_crtc->lut_b[i]);
  36. }
  37. }
  38. static inline void mga_wait_vsync(struct mga_device *mdev)
  39. {
  40. unsigned int count = 0;
  41. unsigned int status = 0;
  42. do {
  43. status = RREG32(MGAREG_Status);
  44. count++;
  45. } while ((status & 0x08) && (count < 250000));
  46. count = 0;
  47. status = 0;
  48. do {
  49. status = RREG32(MGAREG_Status);
  50. count++;
  51. } while (!(status & 0x08) && (count < 250000));
  52. }
  53. static inline void mga_wait_busy(struct mga_device *mdev)
  54. {
  55. unsigned int count = 0;
  56. unsigned int status = 0;
  57. do {
  58. status = RREG8(MGAREG_Status + 2);
  59. count++;
  60. } while ((status & 0x01) && (count < 500000));
  61. }
  62. /*
  63. * The core passes the desired mode to the CRTC code to see whether any
  64. * CRTC-specific modifications need to be made to it. We're in a position
  65. * to just pass that straight through, so this does nothing
  66. */
  67. static bool mga_crtc_mode_fixup(struct drm_crtc *crtc,
  68. struct drm_display_mode *mode,
  69. struct drm_display_mode *adjusted_mode)
  70. {
  71. return true;
  72. }
  73. static int mga_g200se_set_plls(struct mga_device *mdev, long clock)
  74. {
  75. unsigned int vcomax, vcomin, pllreffreq;
  76. unsigned int delta, tmpdelta, permitteddelta;
  77. unsigned int testp, testm, testn;
  78. unsigned int p, m, n;
  79. unsigned int computed;
  80. m = n = p = 0;
  81. vcomax = 320000;
  82. vcomin = 160000;
  83. pllreffreq = 25000;
  84. delta = 0xffffffff;
  85. permitteddelta = clock * 5 / 1000;
  86. for (testp = 8; testp > 0; testp /= 2) {
  87. if (clock * testp > vcomax)
  88. continue;
  89. if (clock * testp < vcomin)
  90. continue;
  91. for (testn = 17; testn < 256; testn++) {
  92. for (testm = 1; testm < 32; testm++) {
  93. computed = (pllreffreq * testn) /
  94. (testm * testp);
  95. if (computed > clock)
  96. tmpdelta = computed - clock;
  97. else
  98. tmpdelta = clock - computed;
  99. if (tmpdelta < delta) {
  100. delta = tmpdelta;
  101. m = testm - 1;
  102. n = testn - 1;
  103. p = testp - 1;
  104. }
  105. }
  106. }
  107. }
  108. if (delta > permitteddelta) {
  109. printk(KERN_WARNING "PLL delta too large\n");
  110. return 1;
  111. }
  112. WREG_DAC(MGA1064_PIX_PLLC_M, m);
  113. WREG_DAC(MGA1064_PIX_PLLC_N, n);
  114. WREG_DAC(MGA1064_PIX_PLLC_P, p);
  115. return 0;
  116. }
  117. static int mga_g200wb_set_plls(struct mga_device *mdev, long clock)
  118. {
  119. unsigned int vcomax, vcomin, pllreffreq;
  120. unsigned int delta, tmpdelta, permitteddelta;
  121. unsigned int testp, testm, testn;
  122. unsigned int p, m, n;
  123. unsigned int computed;
  124. int i, j, tmpcount, vcount;
  125. bool pll_locked = false;
  126. u8 tmp;
  127. m = n = p = 0;
  128. vcomax = 550000;
  129. vcomin = 150000;
  130. pllreffreq = 48000;
  131. delta = 0xffffffff;
  132. permitteddelta = clock * 5 / 1000;
  133. for (testp = 1; testp < 9; testp++) {
  134. if (clock * testp > vcomax)
  135. continue;
  136. if (clock * testp < vcomin)
  137. continue;
  138. for (testm = 1; testm < 17; testm++) {
  139. for (testn = 1; testn < 151; testn++) {
  140. computed = (pllreffreq * testn) /
  141. (testm * testp);
  142. if (computed > clock)
  143. tmpdelta = computed - clock;
  144. else
  145. tmpdelta = clock - computed;
  146. if (tmpdelta < delta) {
  147. delta = tmpdelta;
  148. n = testn - 1;
  149. m = (testm - 1) | ((n >> 1) & 0x80);
  150. p = testp - 1;
  151. }
  152. }
  153. }
  154. }
  155. for (i = 0; i <= 32 && pll_locked == false; i++) {
  156. if (i > 0) {
  157. WREG8(MGAREG_CRTC_INDEX, 0x1e);
  158. tmp = RREG8(MGAREG_CRTC_DATA);
  159. if (tmp < 0xff)
  160. WREG8(MGAREG_CRTC_DATA, tmp+1);
  161. }
  162. /* set pixclkdis to 1 */
  163. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  164. tmp = RREG8(DAC_DATA);
  165. tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
  166. WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
  167. WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
  168. tmp = RREG8(DAC_DATA);
  169. tmp |= MGA1064_REMHEADCTL_CLKDIS;
  170. WREG_DAC(MGA1064_REMHEADCTL, tmp);
  171. /* select PLL Set C */
  172. tmp = RREG8(MGAREG_MEM_MISC_READ);
  173. tmp |= 0x3 << 2;
  174. WREG8(MGAREG_MEM_MISC_WRITE, tmp);
  175. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  176. tmp = RREG8(DAC_DATA);
  177. tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN | 0x80;
  178. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  179. udelay(500);
  180. /* reset the PLL */
  181. WREG8(DAC_INDEX, MGA1064_VREF_CTL);
  182. tmp = RREG8(DAC_DATA);
  183. tmp &= ~0x04;
  184. WREG_DAC(MGA1064_VREF_CTL, tmp);
  185. udelay(50);
  186. /* program pixel pll register */
  187. WREG_DAC(MGA1064_WB_PIX_PLLC_N, n);
  188. WREG_DAC(MGA1064_WB_PIX_PLLC_M, m);
  189. WREG_DAC(MGA1064_WB_PIX_PLLC_P, p);
  190. udelay(50);
  191. /* turn pll on */
  192. WREG8(DAC_INDEX, MGA1064_VREF_CTL);
  193. tmp = RREG8(DAC_DATA);
  194. tmp |= 0x04;
  195. WREG_DAC(MGA1064_VREF_CTL, tmp);
  196. udelay(500);
  197. /* select the pixel pll */
  198. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  199. tmp = RREG8(DAC_DATA);
  200. tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
  201. tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
  202. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  203. WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
  204. tmp = RREG8(DAC_DATA);
  205. tmp &= ~MGA1064_REMHEADCTL_CLKSL_MSK;
  206. tmp |= MGA1064_REMHEADCTL_CLKSL_PLL;
  207. WREG_DAC(MGA1064_REMHEADCTL, tmp);
  208. /* reset dotclock rate bit */
  209. WREG8(MGAREG_SEQ_INDEX, 1);
  210. tmp = RREG8(MGAREG_SEQ_DATA);
  211. tmp &= ~0x8;
  212. WREG8(MGAREG_SEQ_DATA, tmp);
  213. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  214. tmp = RREG8(DAC_DATA);
  215. tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
  216. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  217. vcount = RREG8(MGAREG_VCOUNT);
  218. for (j = 0; j < 30 && pll_locked == false; j++) {
  219. tmpcount = RREG8(MGAREG_VCOUNT);
  220. if (tmpcount < vcount)
  221. vcount = 0;
  222. if ((tmpcount - vcount) > 2)
  223. pll_locked = true;
  224. else
  225. udelay(5);
  226. }
  227. }
  228. WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
  229. tmp = RREG8(DAC_DATA);
  230. tmp &= ~MGA1064_REMHEADCTL_CLKDIS;
  231. WREG_DAC(MGA1064_REMHEADCTL, tmp);
  232. return 0;
  233. }
  234. static int mga_g200ev_set_plls(struct mga_device *mdev, long clock)
  235. {
  236. unsigned int vcomax, vcomin, pllreffreq;
  237. unsigned int delta, tmpdelta, permitteddelta;
  238. unsigned int testp, testm, testn;
  239. unsigned int p, m, n;
  240. unsigned int computed;
  241. u8 tmp;
  242. m = n = p = 0;
  243. vcomax = 550000;
  244. vcomin = 150000;
  245. pllreffreq = 50000;
  246. delta = 0xffffffff;
  247. permitteddelta = clock * 5 / 1000;
  248. for (testp = 16; testp > 0; testp--) {
  249. if (clock * testp > vcomax)
  250. continue;
  251. if (clock * testp < vcomin)
  252. continue;
  253. for (testn = 1; testn < 257; testn++) {
  254. for (testm = 1; testm < 17; testm++) {
  255. computed = (pllreffreq * testn) /
  256. (testm * testp);
  257. if (computed > clock)
  258. tmpdelta = computed - clock;
  259. else
  260. tmpdelta = clock - computed;
  261. if (tmpdelta < delta) {
  262. delta = tmpdelta;
  263. n = testn - 1;
  264. m = testm - 1;
  265. p = testp - 1;
  266. }
  267. }
  268. }
  269. }
  270. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  271. tmp = RREG8(DAC_DATA);
  272. tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
  273. WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
  274. tmp = RREG8(MGAREG_MEM_MISC_READ);
  275. tmp |= 0x3 << 2;
  276. WREG8(MGAREG_MEM_MISC_WRITE, tmp);
  277. WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
  278. tmp = RREG8(DAC_DATA);
  279. WREG_DAC(MGA1064_PIX_PLL_STAT, tmp & ~0x40);
  280. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  281. tmp = RREG8(DAC_DATA);
  282. tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
  283. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  284. WREG_DAC(MGA1064_EV_PIX_PLLC_M, m);
  285. WREG_DAC(MGA1064_EV_PIX_PLLC_N, n);
  286. WREG_DAC(MGA1064_EV_PIX_PLLC_P, p);
  287. udelay(50);
  288. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  289. tmp = RREG8(DAC_DATA);
  290. tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
  291. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  292. udelay(500);
  293. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  294. tmp = RREG8(DAC_DATA);
  295. tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
  296. tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
  297. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  298. WREG8(DAC_INDEX, MGA1064_PIX_PLL_STAT);
  299. tmp = RREG8(DAC_DATA);
  300. WREG_DAC(MGA1064_PIX_PLL_STAT, tmp | 0x40);
  301. tmp = RREG8(MGAREG_MEM_MISC_READ);
  302. tmp |= (0x3 << 2);
  303. WREG8(MGAREG_MEM_MISC_WRITE, tmp);
  304. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  305. tmp = RREG8(DAC_DATA);
  306. tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
  307. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  308. return 0;
  309. }
  310. static int mga_g200eh_set_plls(struct mga_device *mdev, long clock)
  311. {
  312. unsigned int vcomax, vcomin, pllreffreq;
  313. unsigned int delta, tmpdelta, permitteddelta;
  314. unsigned int testp, testm, testn;
  315. unsigned int p, m, n;
  316. unsigned int computed;
  317. int i, j, tmpcount, vcount;
  318. u8 tmp;
  319. bool pll_locked = false;
  320. m = n = p = 0;
  321. vcomax = 800000;
  322. vcomin = 400000;
  323. pllreffreq = 3333;
  324. delta = 0xffffffff;
  325. permitteddelta = clock * 5 / 1000;
  326. for (testp = 16; testp > 0; testp--) {
  327. if (clock * testp > vcomax)
  328. continue;
  329. if (clock * testp < vcomin)
  330. continue;
  331. for (testm = 1; testm < 33; testm++) {
  332. for (testn = 1; testn < 257; testn++) {
  333. computed = (pllreffreq * testn) /
  334. (testm * testp);
  335. if (computed > clock)
  336. tmpdelta = computed - clock;
  337. else
  338. tmpdelta = clock - computed;
  339. if (tmpdelta < delta) {
  340. delta = tmpdelta;
  341. n = testn - 1;
  342. m = (testm - 1) | ((n >> 1) & 0x80);
  343. p = testp - 1;
  344. }
  345. if ((clock * testp) >= 600000)
  346. p |= 80;
  347. }
  348. }
  349. }
  350. for (i = 0; i <= 32 && pll_locked == false; i++) {
  351. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  352. tmp = RREG8(DAC_DATA);
  353. tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
  354. WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
  355. tmp = RREG8(MGAREG_MEM_MISC_READ);
  356. tmp |= 0x3 << 2;
  357. WREG8(MGAREG_MEM_MISC_WRITE, tmp);
  358. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  359. tmp = RREG8(DAC_DATA);
  360. tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
  361. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  362. udelay(500);
  363. WREG_DAC(MGA1064_EH_PIX_PLLC_M, m);
  364. WREG_DAC(MGA1064_EH_PIX_PLLC_N, n);
  365. WREG_DAC(MGA1064_EH_PIX_PLLC_P, p);
  366. udelay(500);
  367. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  368. tmp = RREG8(DAC_DATA);
  369. tmp &= ~MGA1064_PIX_CLK_CTL_SEL_MSK;
  370. tmp |= MGA1064_PIX_CLK_CTL_SEL_PLL;
  371. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  372. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  373. tmp = RREG8(DAC_DATA);
  374. tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
  375. tmp &= ~MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
  376. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  377. vcount = RREG8(MGAREG_VCOUNT);
  378. for (j = 0; j < 30 && pll_locked == false; j++) {
  379. tmpcount = RREG8(MGAREG_VCOUNT);
  380. if (tmpcount < vcount)
  381. vcount = 0;
  382. if ((tmpcount - vcount) > 2)
  383. pll_locked = true;
  384. else
  385. udelay(5);
  386. }
  387. }
  388. return 0;
  389. }
  390. static int mga_g200er_set_plls(struct mga_device *mdev, long clock)
  391. {
  392. unsigned int vcomax, vcomin, pllreffreq;
  393. unsigned int delta, tmpdelta;
  394. unsigned int testr, testn, testm, testo;
  395. unsigned int p, m, n;
  396. unsigned int computed;
  397. int tmp;
  398. m = n = p = 0;
  399. vcomax = 1488000;
  400. vcomin = 1056000;
  401. pllreffreq = 48000;
  402. delta = 0xffffffff;
  403. for (testr = 0; testr < 4; testr++) {
  404. if (delta == 0)
  405. break;
  406. for (testn = 5; testn < 129; testn++) {
  407. if (delta == 0)
  408. break;
  409. for (testm = 3; testm >= 0; testm--) {
  410. if (delta == 0)
  411. break;
  412. for (testo = 5; testo < 33; testo++) {
  413. computed = pllreffreq * (testn + 1) /
  414. (testr + 1);
  415. if (computed < vcomin)
  416. continue;
  417. if (computed > vcomax)
  418. continue;
  419. if (computed > clock)
  420. tmpdelta = computed - clock;
  421. else
  422. tmpdelta = clock - computed;
  423. if (tmpdelta < delta) {
  424. delta = tmpdelta;
  425. m = testm | (testo << 3);
  426. n = testn;
  427. p = testr | (testr << 3);
  428. }
  429. }
  430. }
  431. }
  432. }
  433. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  434. tmp = RREG8(DAC_DATA);
  435. tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
  436. WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
  437. WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
  438. tmp = RREG8(DAC_DATA);
  439. tmp |= MGA1064_REMHEADCTL_CLKDIS;
  440. WREG_DAC(MGA1064_REMHEADCTL, tmp);
  441. tmp = RREG8(MGAREG_MEM_MISC_READ);
  442. tmp |= (0x3<<2) | 0xc0;
  443. WREG8(MGAREG_MEM_MISC_WRITE, tmp);
  444. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  445. tmp = RREG8(DAC_DATA);
  446. tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
  447. tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
  448. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  449. udelay(500);
  450. WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
  451. WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
  452. WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
  453. udelay(50);
  454. return 0;
  455. }
  456. static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
  457. {
  458. switch(mdev->type) {
  459. case G200_SE_A:
  460. case G200_SE_B:
  461. return mga_g200se_set_plls(mdev, clock);
  462. break;
  463. case G200_WB:
  464. return mga_g200wb_set_plls(mdev, clock);
  465. break;
  466. case G200_EV:
  467. return mga_g200ev_set_plls(mdev, clock);
  468. break;
  469. case G200_EH:
  470. return mga_g200eh_set_plls(mdev, clock);
  471. break;
  472. case G200_ER:
  473. return mga_g200er_set_plls(mdev, clock);
  474. break;
  475. }
  476. return 0;
  477. }
  478. static void mga_g200wb_prepare(struct drm_crtc *crtc)
  479. {
  480. struct mga_device *mdev = crtc->dev->dev_private;
  481. u8 tmp;
  482. int iter_max;
  483. /* 1- The first step is to warn the BMC of an upcoming mode change.
  484. * We are putting the misc<0> to output.*/
  485. WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
  486. tmp = RREG8(DAC_DATA);
  487. tmp |= 0x10;
  488. WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
  489. /* we are putting a 1 on the misc<0> line */
  490. WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
  491. tmp = RREG8(DAC_DATA);
  492. tmp |= 0x10;
  493. WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
  494. /* 2- Second step to mask and further scan request
  495. * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
  496. */
  497. WREG8(DAC_INDEX, MGA1064_SPAREREG);
  498. tmp = RREG8(DAC_DATA);
  499. tmp |= 0x80;
  500. WREG_DAC(MGA1064_SPAREREG, tmp);
  501. /* 3a- the third step is to verifu if there is an active scan
  502. * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
  503. */
  504. iter_max = 300;
  505. while (!(tmp & 0x1) && iter_max) {
  506. WREG8(DAC_INDEX, MGA1064_SPAREREG);
  507. tmp = RREG8(DAC_DATA);
  508. udelay(1000);
  509. iter_max--;
  510. }
  511. /* 3b- this step occurs only if the remove is actually scanning
  512. * we are waiting for the end of the frame which is a 1 on
  513. * remvsyncsts (XSPAREREG<1>)
  514. */
  515. if (iter_max) {
  516. iter_max = 300;
  517. while ((tmp & 0x2) && iter_max) {
  518. WREG8(DAC_INDEX, MGA1064_SPAREREG);
  519. tmp = RREG8(DAC_DATA);
  520. udelay(1000);
  521. iter_max--;
  522. }
  523. }
  524. }
  525. static void mga_g200wb_commit(struct drm_crtc *crtc)
  526. {
  527. u8 tmp;
  528. struct mga_device *mdev = crtc->dev->dev_private;
  529. /* 1- The first step is to ensure that the vrsten and hrsten are set */
  530. WREG8(MGAREG_CRTCEXT_INDEX, 1);
  531. tmp = RREG8(MGAREG_CRTCEXT_DATA);
  532. WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
  533. /* 2- second step is to assert the rstlvl2 */
  534. WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
  535. tmp = RREG8(DAC_DATA);
  536. tmp |= 0x8;
  537. WREG8(DAC_DATA, tmp);
  538. /* wait 10 us */
  539. udelay(10);
  540. /* 3- deassert rstlvl2 */
  541. tmp &= ~0x08;
  542. WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
  543. WREG8(DAC_DATA, tmp);
  544. /* 4- remove mask of scan request */
  545. WREG8(DAC_INDEX, MGA1064_SPAREREG);
  546. tmp = RREG8(DAC_DATA);
  547. tmp &= ~0x80;
  548. WREG8(DAC_DATA, tmp);
  549. /* 5- put back a 0 on the misc<0> line */
  550. WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
  551. tmp = RREG8(DAC_DATA);
  552. tmp &= ~0x10;
  553. WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
  554. }
  555. void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
  556. {
  557. struct mga_device *mdev = crtc->dev->dev_private;
  558. u32 addr;
  559. int count;
  560. while (RREG8(0x1fda) & 0x08);
  561. while (!(RREG8(0x1fda) & 0x08));
  562. count = RREG8(MGAREG_VCOUNT) + 2;
  563. while (RREG8(MGAREG_VCOUNT) < count);
  564. addr = offset >> 2;
  565. WREG_CRT(0x0d, (u8)(addr & 0xff));
  566. WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
  567. WREG_CRT(0xaf, (u8)(addr >> 16) & 0xf);
  568. }
  569. /* ast is different - we will force move buffers out of VRAM */
  570. static int mga_crtc_do_set_base(struct drm_crtc *crtc,
  571. struct drm_framebuffer *fb,
  572. int x, int y, int atomic)
  573. {
  574. struct mga_device *mdev = crtc->dev->dev_private;
  575. struct drm_gem_object *obj;
  576. struct mga_framebuffer *mga_fb;
  577. struct mgag200_bo *bo;
  578. int ret;
  579. u64 gpu_addr;
  580. /* push the previous fb to system ram */
  581. if (!atomic && fb) {
  582. mga_fb = to_mga_framebuffer(fb);
  583. obj = mga_fb->obj;
  584. bo = gem_to_mga_bo(obj);
  585. ret = mgag200_bo_reserve(bo, false);
  586. if (ret)
  587. return ret;
  588. mgag200_bo_push_sysram(bo);
  589. mgag200_bo_unreserve(bo);
  590. }
  591. mga_fb = to_mga_framebuffer(crtc->fb);
  592. obj = mga_fb->obj;
  593. bo = gem_to_mga_bo(obj);
  594. ret = mgag200_bo_reserve(bo, false);
  595. if (ret)
  596. return ret;
  597. ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  598. if (ret) {
  599. mgag200_bo_unreserve(bo);
  600. return ret;
  601. }
  602. if (&mdev->mfbdev->mfb == mga_fb) {
  603. /* if pushing console in kmap it */
  604. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  605. if (ret)
  606. DRM_ERROR("failed to kmap fbcon\n");
  607. }
  608. mgag200_bo_unreserve(bo);
  609. DRM_INFO("mga base %llx\n", gpu_addr);
  610. mga_set_start_address(crtc, (u32)gpu_addr);
  611. return 0;
  612. }
  613. static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  614. struct drm_framebuffer *old_fb)
  615. {
  616. return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
  617. }
  618. static int mga_crtc_mode_set(struct drm_crtc *crtc,
  619. struct drm_display_mode *mode,
  620. struct drm_display_mode *adjusted_mode,
  621. int x, int y, struct drm_framebuffer *old_fb)
  622. {
  623. struct drm_device *dev = crtc->dev;
  624. struct mga_device *mdev = dev->dev_private;
  625. int hdisplay, hsyncstart, hsyncend, htotal;
  626. int vdisplay, vsyncstart, vsyncend, vtotal;
  627. int pitch;
  628. int option = 0, option2 = 0;
  629. int i;
  630. unsigned char misc = 0;
  631. unsigned char ext_vga[6];
  632. unsigned char ext_vga_index24;
  633. unsigned char dac_index90 = 0;
  634. u8 bppshift;
  635. static unsigned char dacvalue[] = {
  636. /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
  637. /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
  638. /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
  639. /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
  640. /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  641. /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
  642. /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
  643. /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
  644. /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
  645. /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
  646. };
  647. bppshift = mdev->bpp_shifts[(crtc->fb->bits_per_pixel >> 3) - 1];
  648. switch (mdev->type) {
  649. case G200_SE_A:
  650. case G200_SE_B:
  651. dacvalue[MGA1064_VREF_CTL] = 0x03;
  652. dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
  653. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
  654. MGA1064_MISC_CTL_VGA8 |
  655. MGA1064_MISC_CTL_DAC_RAM_CS;
  656. if (mdev->has_sdram)
  657. option = 0x40049120;
  658. else
  659. option = 0x4004d120;
  660. option2 = 0x00008000;
  661. break;
  662. case G200_WB:
  663. dacvalue[MGA1064_VREF_CTL] = 0x07;
  664. option = 0x41049120;
  665. option2 = 0x0000b000;
  666. break;
  667. case G200_EV:
  668. dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
  669. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
  670. MGA1064_MISC_CTL_DAC_RAM_CS;
  671. option = 0x00000120;
  672. option2 = 0x0000b000;
  673. break;
  674. case G200_EH:
  675. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
  676. MGA1064_MISC_CTL_DAC_RAM_CS;
  677. option = 0x00000120;
  678. option2 = 0x0000b000;
  679. break;
  680. case G200_ER:
  681. dac_index90 = 0;
  682. break;
  683. }
  684. switch (crtc->fb->bits_per_pixel) {
  685. case 8:
  686. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
  687. break;
  688. case 16:
  689. if (crtc->fb->depth == 15)
  690. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
  691. else
  692. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
  693. break;
  694. case 24:
  695. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
  696. break;
  697. case 32:
  698. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
  699. break;
  700. }
  701. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  702. misc |= 0x40;
  703. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  704. misc |= 0x80;
  705. for (i = 0; i < sizeof(dacvalue); i++) {
  706. if ((i <= 0x03) ||
  707. (i == 0x07) ||
  708. (i == 0x0b) ||
  709. (i == 0x0f) ||
  710. ((i >= 0x13) && (i <= 0x17)) ||
  711. (i == 0x1b) ||
  712. (i == 0x1c) ||
  713. ((i >= 0x1f) && (i <= 0x29)) ||
  714. ((i >= 0x30) && (i <= 0x37)))
  715. continue;
  716. if (IS_G200_SE(mdev) &&
  717. ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
  718. continue;
  719. if ((mdev->type == G200_EV || mdev->type == G200_WB || mdev->type == G200_EH) &&
  720. (i >= 0x44) && (i <= 0x4e))
  721. continue;
  722. WREG_DAC(i, dacvalue[i]);
  723. }
  724. if (mdev->type == G200_ER) {
  725. WREG_DAC(0x90, dac_index90);
  726. }
  727. if (option)
  728. pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
  729. if (option2)
  730. pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
  731. WREG_SEQ(2, 0xf);
  732. WREG_SEQ(3, 0);
  733. WREG_SEQ(4, 0xe);
  734. pitch = crtc->fb->pitches[0] / (crtc->fb->bits_per_pixel / 8);
  735. if (crtc->fb->bits_per_pixel == 24)
  736. pitch = pitch >> (4 - bppshift);
  737. else
  738. pitch = pitch >> (4 - bppshift);
  739. hdisplay = mode->hdisplay / 8 - 1;
  740. hsyncstart = mode->hsync_start / 8 - 1;
  741. hsyncend = mode->hsync_end / 8 - 1;
  742. htotal = mode->htotal / 8 - 1;
  743. /* Work around hardware quirk */
  744. if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
  745. htotal++;
  746. vdisplay = mode->vdisplay - 1;
  747. vsyncstart = mode->vsync_start - 1;
  748. vsyncend = mode->vsync_end - 1;
  749. vtotal = mode->vtotal - 2;
  750. WREG_GFX(0, 0);
  751. WREG_GFX(1, 0);
  752. WREG_GFX(2, 0);
  753. WREG_GFX(3, 0);
  754. WREG_GFX(4, 0);
  755. WREG_GFX(5, 0x40);
  756. WREG_GFX(6, 0x5);
  757. WREG_GFX(7, 0xf);
  758. WREG_GFX(8, 0xf);
  759. WREG_CRT(0, htotal - 4);
  760. WREG_CRT(1, hdisplay);
  761. WREG_CRT(2, hdisplay);
  762. WREG_CRT(3, (htotal & 0x1F) | 0x80);
  763. WREG_CRT(4, hsyncstart);
  764. WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
  765. WREG_CRT(6, vtotal & 0xFF);
  766. WREG_CRT(7, ((vtotal & 0x100) >> 8) |
  767. ((vdisplay & 0x100) >> 7) |
  768. ((vsyncstart & 0x100) >> 6) |
  769. ((vdisplay & 0x100) >> 5) |
  770. ((vdisplay & 0x100) >> 4) | /* linecomp */
  771. ((vtotal & 0x200) >> 4)|
  772. ((vdisplay & 0x200) >> 3) |
  773. ((vsyncstart & 0x200) >> 2));
  774. WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
  775. ((vdisplay & 0x200) >> 3));
  776. WREG_CRT(10, 0);
  777. WREG_CRT(11, 0);
  778. WREG_CRT(12, 0);
  779. WREG_CRT(13, 0);
  780. WREG_CRT(14, 0);
  781. WREG_CRT(15, 0);
  782. WREG_CRT(16, vsyncstart & 0xFF);
  783. WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
  784. WREG_CRT(18, vdisplay & 0xFF);
  785. WREG_CRT(19, pitch & 0xFF);
  786. WREG_CRT(20, 0);
  787. WREG_CRT(21, vdisplay & 0xFF);
  788. WREG_CRT(22, (vtotal + 1) & 0xFF);
  789. WREG_CRT(23, 0xc3);
  790. WREG_CRT(24, vdisplay & 0xFF);
  791. ext_vga[0] = 0;
  792. ext_vga[5] = 0;
  793. /* TODO interlace */
  794. ext_vga[0] |= (pitch & 0x300) >> 4;
  795. ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
  796. ((hdisplay & 0x100) >> 7) |
  797. ((hsyncstart & 0x100) >> 6) |
  798. (htotal & 0x40);
  799. ext_vga[2] = ((vtotal & 0xc00) >> 10) |
  800. ((vdisplay & 0x400) >> 8) |
  801. ((vdisplay & 0xc00) >> 7) |
  802. ((vsyncstart & 0xc00) >> 5) |
  803. ((vdisplay & 0x400) >> 3);
  804. if (crtc->fb->bits_per_pixel == 24)
  805. ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
  806. else
  807. ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
  808. ext_vga[4] = 0;
  809. if (mdev->type == G200_WB)
  810. ext_vga[1] |= 0x88;
  811. ext_vga_index24 = 0x05;
  812. /* Set pixel clocks */
  813. misc = 0x2d;
  814. WREG8(MGA_MISC_OUT, misc);
  815. mga_crtc_set_plls(mdev, mode->clock);
  816. for (i = 0; i < 6; i++) {
  817. WREG_ECRT(i, ext_vga[i]);
  818. }
  819. if (mdev->type == G200_ER)
  820. WREG_ECRT(24, ext_vga_index24);
  821. if (mdev->type == G200_EV) {
  822. WREG_ECRT(6, 0);
  823. }
  824. WREG_ECRT(0, ext_vga[0]);
  825. /* Enable mga pixel clock */
  826. misc = 0x2d;
  827. WREG8(MGA_MISC_OUT, misc);
  828. if (adjusted_mode)
  829. memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
  830. mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
  831. /* reset tagfifo */
  832. if (mdev->type == G200_ER) {
  833. u32 mem_ctl = RREG32(MGAREG_MEMCTL);
  834. u8 seq1;
  835. /* screen off */
  836. WREG8(MGAREG_SEQ_INDEX, 0x01);
  837. seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
  838. WREG8(MGAREG_SEQ_DATA, seq1);
  839. WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
  840. udelay(1000);
  841. WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
  842. WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
  843. }
  844. if (IS_G200_SE(mdev)) {
  845. if (mdev->reg_1e24 >= 0x02) {
  846. u8 hi_pri_lvl;
  847. u32 bpp;
  848. u32 mb;
  849. if (crtc->fb->bits_per_pixel > 16)
  850. bpp = 32;
  851. else if (crtc->fb->bits_per_pixel > 8)
  852. bpp = 16;
  853. else
  854. bpp = 8;
  855. mb = (mode->clock * bpp) / 1000;
  856. if (mb > 3100)
  857. hi_pri_lvl = 0;
  858. else if (mb > 2600)
  859. hi_pri_lvl = 1;
  860. else if (mb > 1900)
  861. hi_pri_lvl = 2;
  862. else if (mb > 1160)
  863. hi_pri_lvl = 3;
  864. else if (mb > 440)
  865. hi_pri_lvl = 4;
  866. else
  867. hi_pri_lvl = 5;
  868. WREG8(0x1fde, 0x06);
  869. WREG8(0x1fdf, hi_pri_lvl);
  870. } else {
  871. if (mdev->reg_1e24 >= 0x01)
  872. WREG8(0x1fdf, 0x03);
  873. else
  874. WREG8(0x1fdf, 0x04);
  875. }
  876. }
  877. return 0;
  878. }
  879. #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
  880. static int mga_suspend(struct drm_crtc *crtc)
  881. {
  882. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  883. struct drm_device *dev = crtc->dev;
  884. struct mga_device *mdev = dev->dev_private;
  885. struct pci_dev *pdev = dev->pdev;
  886. int option;
  887. if (mdev->suspended)
  888. return 0;
  889. WREG_SEQ(1, 0x20);
  890. WREG_ECRT(1, 0x30);
  891. /* Disable the pixel clock */
  892. WREG_DAC(0x1a, 0x05);
  893. /* Power down the DAC */
  894. WREG_DAC(0x1e, 0x18);
  895. /* Power down the pixel PLL */
  896. WREG_DAC(0x1a, 0x0d);
  897. /* Disable PLLs and clocks */
  898. pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
  899. option &= ~(0x1F8024);
  900. pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
  901. pci_set_power_state(pdev, PCI_D3hot);
  902. pci_disable_device(pdev);
  903. mdev->suspended = true;
  904. return 0;
  905. }
  906. static int mga_resume(struct drm_crtc *crtc)
  907. {
  908. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  909. struct drm_device *dev = crtc->dev;
  910. struct mga_device *mdev = dev->dev_private;
  911. struct pci_dev *pdev = dev->pdev;
  912. int option;
  913. if (!mdev->suspended)
  914. return 0;
  915. pci_set_power_state(pdev, PCI_D0);
  916. pci_enable_device(pdev);
  917. /* Disable sysclk */
  918. pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
  919. option &= ~(0x4);
  920. pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
  921. mdev->suspended = false;
  922. return 0;
  923. }
  924. #endif
  925. static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
  926. {
  927. struct drm_device *dev = crtc->dev;
  928. struct mga_device *mdev = dev->dev_private;
  929. u8 seq1 = 0, crtcext1 = 0;
  930. switch (mode) {
  931. case DRM_MODE_DPMS_ON:
  932. seq1 = 0;
  933. crtcext1 = 0;
  934. mga_crtc_load_lut(crtc);
  935. break;
  936. case DRM_MODE_DPMS_STANDBY:
  937. seq1 = 0x20;
  938. crtcext1 = 0x10;
  939. break;
  940. case DRM_MODE_DPMS_SUSPEND:
  941. seq1 = 0x20;
  942. crtcext1 = 0x20;
  943. break;
  944. case DRM_MODE_DPMS_OFF:
  945. seq1 = 0x20;
  946. crtcext1 = 0x30;
  947. break;
  948. }
  949. #if 0
  950. if (mode == DRM_MODE_DPMS_OFF) {
  951. mga_suspend(crtc);
  952. }
  953. #endif
  954. WREG8(MGAREG_SEQ_INDEX, 0x01);
  955. seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
  956. mga_wait_vsync(mdev);
  957. mga_wait_busy(mdev);
  958. WREG8(MGAREG_SEQ_DATA, seq1);
  959. msleep(20);
  960. WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
  961. crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
  962. WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
  963. #if 0
  964. if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
  965. mga_resume(crtc);
  966. drm_helper_resume_force_mode(dev);
  967. }
  968. #endif
  969. }
  970. /*
  971. * This is called before a mode is programmed. A typical use might be to
  972. * enable DPMS during the programming to avoid seeing intermediate stages,
  973. * but that's not relevant to us
  974. */
  975. static void mga_crtc_prepare(struct drm_crtc *crtc)
  976. {
  977. struct drm_device *dev = crtc->dev;
  978. struct mga_device *mdev = dev->dev_private;
  979. u8 tmp;
  980. /* mga_resume(crtc);*/
  981. WREG8(MGAREG_CRTC_INDEX, 0x11);
  982. tmp = RREG8(MGAREG_CRTC_DATA);
  983. WREG_CRT(0x11, tmp | 0x80);
  984. if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
  985. WREG_SEQ(0, 1);
  986. msleep(50);
  987. WREG_SEQ(1, 0x20);
  988. msleep(20);
  989. } else {
  990. WREG8(MGAREG_SEQ_INDEX, 0x1);
  991. tmp = RREG8(MGAREG_SEQ_DATA);
  992. /* start sync reset */
  993. WREG_SEQ(0, 1);
  994. WREG_SEQ(1, tmp | 0x20);
  995. }
  996. if (mdev->type == G200_WB)
  997. mga_g200wb_prepare(crtc);
  998. WREG_CRT(17, 0);
  999. }
  1000. /*
  1001. * This is called after a mode is programmed. It should reverse anything done
  1002. * by the prepare function
  1003. */
  1004. static void mga_crtc_commit(struct drm_crtc *crtc)
  1005. {
  1006. struct drm_device *dev = crtc->dev;
  1007. struct mga_device *mdev = dev->dev_private;
  1008. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1009. u8 tmp;
  1010. if (mdev->type == G200_WB)
  1011. mga_g200wb_commit(crtc);
  1012. if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
  1013. msleep(50);
  1014. WREG_SEQ(1, 0x0);
  1015. msleep(20);
  1016. WREG_SEQ(0, 0x3);
  1017. } else {
  1018. WREG8(MGAREG_SEQ_INDEX, 0x1);
  1019. tmp = RREG8(MGAREG_SEQ_DATA);
  1020. tmp &= ~0x20;
  1021. WREG_SEQ(0x1, tmp);
  1022. WREG_SEQ(0, 3);
  1023. }
  1024. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1025. }
  1026. /*
  1027. * The core can pass us a set of gamma values to program. We actually only
  1028. * use this for 8-bit mode so can't perform smooth fades on deeper modes,
  1029. * but it's a requirement that we provide the function
  1030. */
  1031. static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  1032. u16 *blue, uint32_t start, uint32_t size)
  1033. {
  1034. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1035. int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
  1036. int i;
  1037. for (i = start; i < end; i++) {
  1038. mga_crtc->lut_r[i] = red[i] >> 8;
  1039. mga_crtc->lut_g[i] = green[i] >> 8;
  1040. mga_crtc->lut_b[i] = blue[i] >> 8;
  1041. }
  1042. mga_crtc_load_lut(crtc);
  1043. }
  1044. /* Simple cleanup function */
  1045. static void mga_crtc_destroy(struct drm_crtc *crtc)
  1046. {
  1047. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1048. drm_crtc_cleanup(crtc);
  1049. kfree(mga_crtc);
  1050. }
  1051. /* These provide the minimum set of functions required to handle a CRTC */
  1052. static const struct drm_crtc_funcs mga_crtc_funcs = {
  1053. .gamma_set = mga_crtc_gamma_set,
  1054. .set_config = drm_crtc_helper_set_config,
  1055. .destroy = mga_crtc_destroy,
  1056. };
  1057. static const struct drm_crtc_helper_funcs mga_helper_funcs = {
  1058. .dpms = mga_crtc_dpms,
  1059. .mode_fixup = mga_crtc_mode_fixup,
  1060. .mode_set = mga_crtc_mode_set,
  1061. .mode_set_base = mga_crtc_mode_set_base,
  1062. .prepare = mga_crtc_prepare,
  1063. .commit = mga_crtc_commit,
  1064. .load_lut = mga_crtc_load_lut,
  1065. };
  1066. /* CRTC setup */
  1067. static void mga_crtc_init(struct drm_device *dev)
  1068. {
  1069. struct mga_device *mdev = dev->dev_private;
  1070. struct mga_crtc *mga_crtc;
  1071. int i;
  1072. mga_crtc = kzalloc(sizeof(struct mga_crtc) +
  1073. (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
  1074. GFP_KERNEL);
  1075. if (mga_crtc == NULL)
  1076. return;
  1077. drm_crtc_init(dev, &mga_crtc->base, &mga_crtc_funcs);
  1078. drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
  1079. mdev->mode_info.crtc = mga_crtc;
  1080. for (i = 0; i < MGAG200_LUT_SIZE; i++) {
  1081. mga_crtc->lut_r[i] = i;
  1082. mga_crtc->lut_g[i] = i;
  1083. mga_crtc->lut_b[i] = i;
  1084. }
  1085. drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
  1086. }
  1087. /** Sets the color ramps on behalf of fbcon */
  1088. void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  1089. u16 blue, int regno)
  1090. {
  1091. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1092. mga_crtc->lut_r[regno] = red >> 8;
  1093. mga_crtc->lut_g[regno] = green >> 8;
  1094. mga_crtc->lut_b[regno] = blue >> 8;
  1095. }
  1096. /** Gets the color ramps on behalf of fbcon */
  1097. void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  1098. u16 *blue, int regno)
  1099. {
  1100. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1101. *red = (u16)mga_crtc->lut_r[regno] << 8;
  1102. *green = (u16)mga_crtc->lut_g[regno] << 8;
  1103. *blue = (u16)mga_crtc->lut_b[regno] << 8;
  1104. }
  1105. /*
  1106. * The encoder comes after the CRTC in the output pipeline, but before
  1107. * the connector. It's responsible for ensuring that the digital
  1108. * stream is appropriately converted into the output format. Setup is
  1109. * very simple in this case - all we have to do is inform qemu of the
  1110. * colour depth in order to ensure that it displays appropriately
  1111. */
  1112. /*
  1113. * These functions are analagous to those in the CRTC code, but are intended
  1114. * to handle any encoder-specific limitations
  1115. */
  1116. static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
  1117. struct drm_display_mode *mode,
  1118. struct drm_display_mode *adjusted_mode)
  1119. {
  1120. return true;
  1121. }
  1122. static void mga_encoder_mode_set(struct drm_encoder *encoder,
  1123. struct drm_display_mode *mode,
  1124. struct drm_display_mode *adjusted_mode)
  1125. {
  1126. }
  1127. static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
  1128. {
  1129. return;
  1130. }
  1131. static void mga_encoder_prepare(struct drm_encoder *encoder)
  1132. {
  1133. }
  1134. static void mga_encoder_commit(struct drm_encoder *encoder)
  1135. {
  1136. }
  1137. void mga_encoder_destroy(struct drm_encoder *encoder)
  1138. {
  1139. struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
  1140. drm_encoder_cleanup(encoder);
  1141. kfree(mga_encoder);
  1142. }
  1143. static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
  1144. .dpms = mga_encoder_dpms,
  1145. .mode_fixup = mga_encoder_mode_fixup,
  1146. .mode_set = mga_encoder_mode_set,
  1147. .prepare = mga_encoder_prepare,
  1148. .commit = mga_encoder_commit,
  1149. };
  1150. static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
  1151. .destroy = mga_encoder_destroy,
  1152. };
  1153. static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
  1154. {
  1155. struct drm_encoder *encoder;
  1156. struct mga_encoder *mga_encoder;
  1157. mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
  1158. if (!mga_encoder)
  1159. return NULL;
  1160. encoder = &mga_encoder->base;
  1161. encoder->possible_crtcs = 0x1;
  1162. drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
  1163. DRM_MODE_ENCODER_DAC);
  1164. drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
  1165. return encoder;
  1166. }
  1167. static int mga_vga_get_modes(struct drm_connector *connector)
  1168. {
  1169. struct mga_connector *mga_connector = to_mga_connector(connector);
  1170. struct edid *edid;
  1171. int ret = 0;
  1172. edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
  1173. if (edid) {
  1174. drm_mode_connector_update_edid_property(connector, edid);
  1175. ret = drm_add_edid_modes(connector, edid);
  1176. connector->display_info.raw_edid = NULL;
  1177. kfree(edid);
  1178. }
  1179. return ret;
  1180. }
  1181. static int mga_vga_mode_valid(struct drm_connector *connector,
  1182. struct drm_display_mode *mode)
  1183. {
  1184. /* FIXME: Add bandwidth and g200se limitations */
  1185. if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
  1186. mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
  1187. mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
  1188. mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
  1189. return MODE_BAD;
  1190. }
  1191. return MODE_OK;
  1192. }
  1193. struct drm_encoder *mga_connector_best_encoder(struct drm_connector
  1194. *connector)
  1195. {
  1196. int enc_id = connector->encoder_ids[0];
  1197. struct drm_mode_object *obj;
  1198. struct drm_encoder *encoder;
  1199. /* pick the encoder ids */
  1200. if (enc_id) {
  1201. obj =
  1202. drm_mode_object_find(connector->dev, enc_id,
  1203. DRM_MODE_OBJECT_ENCODER);
  1204. if (!obj)
  1205. return NULL;
  1206. encoder = obj_to_encoder(obj);
  1207. return encoder;
  1208. }
  1209. return NULL;
  1210. }
  1211. static enum drm_connector_status mga_vga_detect(struct drm_connector
  1212. *connector, bool force)
  1213. {
  1214. return connector_status_connected;
  1215. }
  1216. static void mga_connector_destroy(struct drm_connector *connector)
  1217. {
  1218. struct mga_connector *mga_connector = to_mga_connector(connector);
  1219. mgag200_i2c_destroy(mga_connector->i2c);
  1220. drm_connector_cleanup(connector);
  1221. kfree(connector);
  1222. }
  1223. struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
  1224. .get_modes = mga_vga_get_modes,
  1225. .mode_valid = mga_vga_mode_valid,
  1226. .best_encoder = mga_connector_best_encoder,
  1227. };
  1228. struct drm_connector_funcs mga_vga_connector_funcs = {
  1229. .dpms = drm_helper_connector_dpms,
  1230. .detect = mga_vga_detect,
  1231. .fill_modes = drm_helper_probe_single_connector_modes,
  1232. .destroy = mga_connector_destroy,
  1233. };
  1234. static struct drm_connector *mga_vga_init(struct drm_device *dev)
  1235. {
  1236. struct drm_connector *connector;
  1237. struct mga_connector *mga_connector;
  1238. mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
  1239. if (!mga_connector)
  1240. return NULL;
  1241. connector = &mga_connector->base;
  1242. drm_connector_init(dev, connector,
  1243. &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  1244. drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
  1245. mga_connector->i2c = mgag200_i2c_create(dev);
  1246. if (!mga_connector->i2c)
  1247. DRM_ERROR("failed to add ddc bus\n");
  1248. return connector;
  1249. }
  1250. int mgag200_modeset_init(struct mga_device *mdev)
  1251. {
  1252. struct drm_encoder *encoder;
  1253. struct drm_connector *connector;
  1254. int ret;
  1255. mdev->mode_info.mode_config_initialized = true;
  1256. mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
  1257. mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
  1258. mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
  1259. mga_crtc_init(mdev->dev);
  1260. encoder = mga_encoder_init(mdev->dev);
  1261. if (!encoder) {
  1262. DRM_ERROR("mga_encoder_init failed\n");
  1263. return -1;
  1264. }
  1265. connector = mga_vga_init(mdev->dev);
  1266. if (!connector) {
  1267. DRM_ERROR("mga_vga_init failed\n");
  1268. return -1;
  1269. }
  1270. drm_mode_connector_attach_encoder(connector, encoder);
  1271. ret = mgag200_fbdev_init(mdev);
  1272. if (ret) {
  1273. DRM_ERROR("mga_fbdev_init failed\n");
  1274. return ret;
  1275. }
  1276. return 0;
  1277. }
  1278. void mgag200_modeset_fini(struct mga_device *mdev)
  1279. {
  1280. }