mgag200_mode.c 37 KB

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