mgag200_mode.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. const 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. int testr, testn, testm, testo;
  395. unsigned int p, m, n;
  396. unsigned int computed, vco;
  397. int tmp;
  398. const unsigned int m_div_val[] = { 1, 2, 4, 8 };
  399. m = n = p = 0;
  400. vcomax = 1488000;
  401. vcomin = 1056000;
  402. pllreffreq = 48000;
  403. delta = 0xffffffff;
  404. for (testr = 0; testr < 4; testr++) {
  405. if (delta == 0)
  406. break;
  407. for (testn = 5; testn < 129; testn++) {
  408. if (delta == 0)
  409. break;
  410. for (testm = 3; testm >= 0; testm--) {
  411. if (delta == 0)
  412. break;
  413. for (testo = 5; testo < 33; testo++) {
  414. vco = pllreffreq * (testn + 1) /
  415. (testr + 1);
  416. if (vco < vcomin)
  417. continue;
  418. if (vco > vcomax)
  419. continue;
  420. computed = vco / (m_div_val[testm] * (testo + 1));
  421. if (computed > clock)
  422. tmpdelta = computed - clock;
  423. else
  424. tmpdelta = clock - computed;
  425. if (tmpdelta < delta) {
  426. delta = tmpdelta;
  427. m = testm | (testo << 3);
  428. n = testn;
  429. p = testr | (testr << 3);
  430. }
  431. }
  432. }
  433. }
  434. }
  435. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  436. tmp = RREG8(DAC_DATA);
  437. tmp |= MGA1064_PIX_CLK_CTL_CLK_DIS;
  438. WREG_DAC(MGA1064_PIX_CLK_CTL_CLK_DIS, tmp);
  439. WREG8(DAC_INDEX, MGA1064_REMHEADCTL);
  440. tmp = RREG8(DAC_DATA);
  441. tmp |= MGA1064_REMHEADCTL_CLKDIS;
  442. WREG_DAC(MGA1064_REMHEADCTL, tmp);
  443. tmp = RREG8(MGAREG_MEM_MISC_READ);
  444. tmp |= (0x3<<2) | 0xc0;
  445. WREG8(MGAREG_MEM_MISC_WRITE, tmp);
  446. WREG8(DAC_INDEX, MGA1064_PIX_CLK_CTL);
  447. tmp = RREG8(DAC_DATA);
  448. tmp &= ~MGA1064_PIX_CLK_CTL_CLK_DIS;
  449. tmp |= MGA1064_PIX_CLK_CTL_CLK_POW_DOWN;
  450. WREG_DAC(MGA1064_PIX_CLK_CTL, tmp);
  451. udelay(500);
  452. WREG_DAC(MGA1064_ER_PIX_PLLC_N, n);
  453. WREG_DAC(MGA1064_ER_PIX_PLLC_M, m);
  454. WREG_DAC(MGA1064_ER_PIX_PLLC_P, p);
  455. udelay(50);
  456. return 0;
  457. }
  458. static int mga_crtc_set_plls(struct mga_device *mdev, long clock)
  459. {
  460. switch(mdev->type) {
  461. case G200_SE_A:
  462. case G200_SE_B:
  463. return mga_g200se_set_plls(mdev, clock);
  464. break;
  465. case G200_WB:
  466. return mga_g200wb_set_plls(mdev, clock);
  467. break;
  468. case G200_EV:
  469. return mga_g200ev_set_plls(mdev, clock);
  470. break;
  471. case G200_EH:
  472. return mga_g200eh_set_plls(mdev, clock);
  473. break;
  474. case G200_ER:
  475. return mga_g200er_set_plls(mdev, clock);
  476. break;
  477. }
  478. return 0;
  479. }
  480. static void mga_g200wb_prepare(struct drm_crtc *crtc)
  481. {
  482. struct mga_device *mdev = crtc->dev->dev_private;
  483. u8 tmp;
  484. int iter_max;
  485. /* 1- The first step is to warn the BMC of an upcoming mode change.
  486. * We are putting the misc<0> to output.*/
  487. WREG8(DAC_INDEX, MGA1064_GEN_IO_CTL);
  488. tmp = RREG8(DAC_DATA);
  489. tmp |= 0x10;
  490. WREG_DAC(MGA1064_GEN_IO_CTL, tmp);
  491. /* we are putting a 1 on the misc<0> line */
  492. WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
  493. tmp = RREG8(DAC_DATA);
  494. tmp |= 0x10;
  495. WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
  496. /* 2- Second step to mask and further scan request
  497. * This will be done by asserting the remfreqmsk bit (XSPAREREG<7>)
  498. */
  499. WREG8(DAC_INDEX, MGA1064_SPAREREG);
  500. tmp = RREG8(DAC_DATA);
  501. tmp |= 0x80;
  502. WREG_DAC(MGA1064_SPAREREG, tmp);
  503. /* 3a- the third step is to verifu if there is an active scan
  504. * We are searching for a 0 on remhsyncsts <XSPAREREG<0>)
  505. */
  506. iter_max = 300;
  507. while (!(tmp & 0x1) && iter_max) {
  508. WREG8(DAC_INDEX, MGA1064_SPAREREG);
  509. tmp = RREG8(DAC_DATA);
  510. udelay(1000);
  511. iter_max--;
  512. }
  513. /* 3b- this step occurs only if the remove is actually scanning
  514. * we are waiting for the end of the frame which is a 1 on
  515. * remvsyncsts (XSPAREREG<1>)
  516. */
  517. if (iter_max) {
  518. iter_max = 300;
  519. while ((tmp & 0x2) && iter_max) {
  520. WREG8(DAC_INDEX, MGA1064_SPAREREG);
  521. tmp = RREG8(DAC_DATA);
  522. udelay(1000);
  523. iter_max--;
  524. }
  525. }
  526. }
  527. static void mga_g200wb_commit(struct drm_crtc *crtc)
  528. {
  529. u8 tmp;
  530. struct mga_device *mdev = crtc->dev->dev_private;
  531. /* 1- The first step is to ensure that the vrsten and hrsten are set */
  532. WREG8(MGAREG_CRTCEXT_INDEX, 1);
  533. tmp = RREG8(MGAREG_CRTCEXT_DATA);
  534. WREG8(MGAREG_CRTCEXT_DATA, tmp | 0x88);
  535. /* 2- second step is to assert the rstlvl2 */
  536. WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
  537. tmp = RREG8(DAC_DATA);
  538. tmp |= 0x8;
  539. WREG8(DAC_DATA, tmp);
  540. /* wait 10 us */
  541. udelay(10);
  542. /* 3- deassert rstlvl2 */
  543. tmp &= ~0x08;
  544. WREG8(DAC_INDEX, MGA1064_REMHEADCTL2);
  545. WREG8(DAC_DATA, tmp);
  546. /* 4- remove mask of scan request */
  547. WREG8(DAC_INDEX, MGA1064_SPAREREG);
  548. tmp = RREG8(DAC_DATA);
  549. tmp &= ~0x80;
  550. WREG8(DAC_DATA, tmp);
  551. /* 5- put back a 0 on the misc<0> line */
  552. WREG8(DAC_INDEX, MGA1064_GEN_IO_DATA);
  553. tmp = RREG8(DAC_DATA);
  554. tmp &= ~0x10;
  555. WREG_DAC(MGA1064_GEN_IO_DATA, tmp);
  556. }
  557. void mga_set_start_address(struct drm_crtc *crtc, unsigned offset)
  558. {
  559. struct mga_device *mdev = crtc->dev->dev_private;
  560. u32 addr;
  561. int count;
  562. while (RREG8(0x1fda) & 0x08);
  563. while (!(RREG8(0x1fda) & 0x08));
  564. count = RREG8(MGAREG_VCOUNT) + 2;
  565. while (RREG8(MGAREG_VCOUNT) < count);
  566. addr = offset >> 2;
  567. WREG_CRT(0x0d, (u8)(addr & 0xff));
  568. WREG_CRT(0x0c, (u8)(addr >> 8) & 0xff);
  569. WREG_CRT(0xaf, (u8)(addr >> 16) & 0xf);
  570. }
  571. /* ast is different - we will force move buffers out of VRAM */
  572. static int mga_crtc_do_set_base(struct drm_crtc *crtc,
  573. struct drm_framebuffer *fb,
  574. int x, int y, int atomic)
  575. {
  576. struct mga_device *mdev = crtc->dev->dev_private;
  577. struct drm_gem_object *obj;
  578. struct mga_framebuffer *mga_fb;
  579. struct mgag200_bo *bo;
  580. int ret;
  581. u64 gpu_addr;
  582. /* push the previous fb to system ram */
  583. if (!atomic && fb) {
  584. mga_fb = to_mga_framebuffer(fb);
  585. obj = mga_fb->obj;
  586. bo = gem_to_mga_bo(obj);
  587. ret = mgag200_bo_reserve(bo, false);
  588. if (ret)
  589. return ret;
  590. mgag200_bo_push_sysram(bo);
  591. mgag200_bo_unreserve(bo);
  592. }
  593. mga_fb = to_mga_framebuffer(crtc->fb);
  594. obj = mga_fb->obj;
  595. bo = gem_to_mga_bo(obj);
  596. ret = mgag200_bo_reserve(bo, false);
  597. if (ret)
  598. return ret;
  599. ret = mgag200_bo_pin(bo, TTM_PL_FLAG_VRAM, &gpu_addr);
  600. if (ret) {
  601. mgag200_bo_unreserve(bo);
  602. return ret;
  603. }
  604. if (&mdev->mfbdev->mfb == mga_fb) {
  605. /* if pushing console in kmap it */
  606. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  607. if (ret)
  608. DRM_ERROR("failed to kmap fbcon\n");
  609. }
  610. mgag200_bo_unreserve(bo);
  611. DRM_INFO("mga base %llx\n", gpu_addr);
  612. mga_set_start_address(crtc, (u32)gpu_addr);
  613. return 0;
  614. }
  615. static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  616. struct drm_framebuffer *old_fb)
  617. {
  618. return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
  619. }
  620. static int mga_crtc_mode_set(struct drm_crtc *crtc,
  621. struct drm_display_mode *mode,
  622. struct drm_display_mode *adjusted_mode,
  623. int x, int y, struct drm_framebuffer *old_fb)
  624. {
  625. struct drm_device *dev = crtc->dev;
  626. struct mga_device *mdev = dev->dev_private;
  627. int hdisplay, hsyncstart, hsyncend, htotal;
  628. int vdisplay, vsyncstart, vsyncend, vtotal;
  629. int pitch;
  630. int option = 0, option2 = 0;
  631. int i;
  632. unsigned char misc = 0;
  633. unsigned char ext_vga[6];
  634. unsigned char ext_vga_index24;
  635. unsigned char dac_index90 = 0;
  636. u8 bppshift;
  637. static unsigned char dacvalue[] = {
  638. /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
  639. /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
  640. /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
  641. /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
  642. /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  643. /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
  644. /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
  645. /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
  646. /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
  647. /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
  648. };
  649. bppshift = mdev->bpp_shifts[(crtc->fb->bits_per_pixel >> 3) - 1];
  650. switch (mdev->type) {
  651. case G200_SE_A:
  652. case G200_SE_B:
  653. dacvalue[MGA1064_VREF_CTL] = 0x03;
  654. dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
  655. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
  656. MGA1064_MISC_CTL_VGA8 |
  657. MGA1064_MISC_CTL_DAC_RAM_CS;
  658. if (mdev->has_sdram)
  659. option = 0x40049120;
  660. else
  661. option = 0x4004d120;
  662. option2 = 0x00008000;
  663. break;
  664. case G200_WB:
  665. dacvalue[MGA1064_VREF_CTL] = 0x07;
  666. option = 0x41049120;
  667. option2 = 0x0000b000;
  668. break;
  669. case G200_EV:
  670. dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
  671. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
  672. MGA1064_MISC_CTL_DAC_RAM_CS;
  673. option = 0x00000120;
  674. option2 = 0x0000b000;
  675. break;
  676. case G200_EH:
  677. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
  678. MGA1064_MISC_CTL_DAC_RAM_CS;
  679. option = 0x00000120;
  680. option2 = 0x0000b000;
  681. break;
  682. case G200_ER:
  683. dac_index90 = 0;
  684. break;
  685. }
  686. switch (crtc->fb->bits_per_pixel) {
  687. case 8:
  688. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
  689. break;
  690. case 16:
  691. if (crtc->fb->depth == 15)
  692. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
  693. else
  694. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
  695. break;
  696. case 24:
  697. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
  698. break;
  699. case 32:
  700. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
  701. break;
  702. }
  703. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  704. misc |= 0x40;
  705. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  706. misc |= 0x80;
  707. for (i = 0; i < sizeof(dacvalue); i++) {
  708. if ((i <= 0x03) ||
  709. (i == 0x07) ||
  710. (i == 0x0b) ||
  711. (i == 0x0f) ||
  712. ((i >= 0x13) && (i <= 0x17)) ||
  713. (i == 0x1b) ||
  714. (i == 0x1c) ||
  715. ((i >= 0x1f) && (i <= 0x29)) ||
  716. ((i >= 0x30) && (i <= 0x37)))
  717. continue;
  718. if (IS_G200_SE(mdev) &&
  719. ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
  720. continue;
  721. if ((mdev->type == G200_EV || mdev->type == G200_WB || mdev->type == G200_EH) &&
  722. (i >= 0x44) && (i <= 0x4e))
  723. continue;
  724. WREG_DAC(i, dacvalue[i]);
  725. }
  726. if (mdev->type == G200_ER) {
  727. WREG_DAC(0x90, dac_index90);
  728. }
  729. if (option)
  730. pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
  731. if (option2)
  732. pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
  733. WREG_SEQ(2, 0xf);
  734. WREG_SEQ(3, 0);
  735. WREG_SEQ(4, 0xe);
  736. pitch = crtc->fb->pitches[0] / (crtc->fb->bits_per_pixel / 8);
  737. if (crtc->fb->bits_per_pixel == 24)
  738. pitch = pitch >> (4 - bppshift);
  739. else
  740. pitch = pitch >> (4 - bppshift);
  741. hdisplay = mode->hdisplay / 8 - 1;
  742. hsyncstart = mode->hsync_start / 8 - 1;
  743. hsyncend = mode->hsync_end / 8 - 1;
  744. htotal = mode->htotal / 8 - 1;
  745. /* Work around hardware quirk */
  746. if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
  747. htotal++;
  748. vdisplay = mode->vdisplay - 1;
  749. vsyncstart = mode->vsync_start - 1;
  750. vsyncend = mode->vsync_end - 1;
  751. vtotal = mode->vtotal - 2;
  752. WREG_GFX(0, 0);
  753. WREG_GFX(1, 0);
  754. WREG_GFX(2, 0);
  755. WREG_GFX(3, 0);
  756. WREG_GFX(4, 0);
  757. WREG_GFX(5, 0x40);
  758. WREG_GFX(6, 0x5);
  759. WREG_GFX(7, 0xf);
  760. WREG_GFX(8, 0xf);
  761. WREG_CRT(0, htotal - 4);
  762. WREG_CRT(1, hdisplay);
  763. WREG_CRT(2, hdisplay);
  764. WREG_CRT(3, (htotal & 0x1F) | 0x80);
  765. WREG_CRT(4, hsyncstart);
  766. WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
  767. WREG_CRT(6, vtotal & 0xFF);
  768. WREG_CRT(7, ((vtotal & 0x100) >> 8) |
  769. ((vdisplay & 0x100) >> 7) |
  770. ((vsyncstart & 0x100) >> 6) |
  771. ((vdisplay & 0x100) >> 5) |
  772. ((vdisplay & 0x100) >> 4) | /* linecomp */
  773. ((vtotal & 0x200) >> 4)|
  774. ((vdisplay & 0x200) >> 3) |
  775. ((vsyncstart & 0x200) >> 2));
  776. WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
  777. ((vdisplay & 0x200) >> 3));
  778. WREG_CRT(10, 0);
  779. WREG_CRT(11, 0);
  780. WREG_CRT(12, 0);
  781. WREG_CRT(13, 0);
  782. WREG_CRT(14, 0);
  783. WREG_CRT(15, 0);
  784. WREG_CRT(16, vsyncstart & 0xFF);
  785. WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
  786. WREG_CRT(18, vdisplay & 0xFF);
  787. WREG_CRT(19, pitch & 0xFF);
  788. WREG_CRT(20, 0);
  789. WREG_CRT(21, vdisplay & 0xFF);
  790. WREG_CRT(22, (vtotal + 1) & 0xFF);
  791. WREG_CRT(23, 0xc3);
  792. WREG_CRT(24, vdisplay & 0xFF);
  793. ext_vga[0] = 0;
  794. ext_vga[5] = 0;
  795. /* TODO interlace */
  796. ext_vga[0] |= (pitch & 0x300) >> 4;
  797. ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
  798. ((hdisplay & 0x100) >> 7) |
  799. ((hsyncstart & 0x100) >> 6) |
  800. (htotal & 0x40);
  801. ext_vga[2] = ((vtotal & 0xc00) >> 10) |
  802. ((vdisplay & 0x400) >> 8) |
  803. ((vdisplay & 0xc00) >> 7) |
  804. ((vsyncstart & 0xc00) >> 5) |
  805. ((vdisplay & 0x400) >> 3);
  806. if (crtc->fb->bits_per_pixel == 24)
  807. ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
  808. else
  809. ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
  810. ext_vga[4] = 0;
  811. if (mdev->type == G200_WB)
  812. ext_vga[1] |= 0x88;
  813. ext_vga_index24 = 0x05;
  814. /* Set pixel clocks */
  815. misc = 0x2d;
  816. WREG8(MGA_MISC_OUT, misc);
  817. mga_crtc_set_plls(mdev, mode->clock);
  818. for (i = 0; i < 6; i++) {
  819. WREG_ECRT(i, ext_vga[i]);
  820. }
  821. if (mdev->type == G200_ER)
  822. WREG_ECRT(24, ext_vga_index24);
  823. if (mdev->type == G200_EV) {
  824. WREG_ECRT(6, 0);
  825. }
  826. WREG_ECRT(0, ext_vga[0]);
  827. /* Enable mga pixel clock */
  828. misc = 0x2d;
  829. WREG8(MGA_MISC_OUT, misc);
  830. if (adjusted_mode)
  831. memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
  832. mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
  833. /* reset tagfifo */
  834. if (mdev->type == G200_ER) {
  835. u32 mem_ctl = RREG32(MGAREG_MEMCTL);
  836. u8 seq1;
  837. /* screen off */
  838. WREG8(MGAREG_SEQ_INDEX, 0x01);
  839. seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
  840. WREG8(MGAREG_SEQ_DATA, seq1);
  841. WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
  842. udelay(1000);
  843. WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
  844. WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
  845. }
  846. if (IS_G200_SE(mdev)) {
  847. if (mdev->reg_1e24 >= 0x02) {
  848. u8 hi_pri_lvl;
  849. u32 bpp;
  850. u32 mb;
  851. if (crtc->fb->bits_per_pixel > 16)
  852. bpp = 32;
  853. else if (crtc->fb->bits_per_pixel > 8)
  854. bpp = 16;
  855. else
  856. bpp = 8;
  857. mb = (mode->clock * bpp) / 1000;
  858. if (mb > 3100)
  859. hi_pri_lvl = 0;
  860. else if (mb > 2600)
  861. hi_pri_lvl = 1;
  862. else if (mb > 1900)
  863. hi_pri_lvl = 2;
  864. else if (mb > 1160)
  865. hi_pri_lvl = 3;
  866. else if (mb > 440)
  867. hi_pri_lvl = 4;
  868. else
  869. hi_pri_lvl = 5;
  870. WREG8(0x1fde, 0x06);
  871. WREG8(0x1fdf, hi_pri_lvl);
  872. } else {
  873. if (mdev->reg_1e24 >= 0x01)
  874. WREG8(0x1fdf, 0x03);
  875. else
  876. WREG8(0x1fdf, 0x04);
  877. }
  878. }
  879. return 0;
  880. }
  881. #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
  882. static int mga_suspend(struct drm_crtc *crtc)
  883. {
  884. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  885. struct drm_device *dev = crtc->dev;
  886. struct mga_device *mdev = dev->dev_private;
  887. struct pci_dev *pdev = dev->pdev;
  888. int option;
  889. if (mdev->suspended)
  890. return 0;
  891. WREG_SEQ(1, 0x20);
  892. WREG_ECRT(1, 0x30);
  893. /* Disable the pixel clock */
  894. WREG_DAC(0x1a, 0x05);
  895. /* Power down the DAC */
  896. WREG_DAC(0x1e, 0x18);
  897. /* Power down the pixel PLL */
  898. WREG_DAC(0x1a, 0x0d);
  899. /* Disable PLLs and clocks */
  900. pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
  901. option &= ~(0x1F8024);
  902. pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
  903. pci_set_power_state(pdev, PCI_D3hot);
  904. pci_disable_device(pdev);
  905. mdev->suspended = true;
  906. return 0;
  907. }
  908. static int mga_resume(struct drm_crtc *crtc)
  909. {
  910. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  911. struct drm_device *dev = crtc->dev;
  912. struct mga_device *mdev = dev->dev_private;
  913. struct pci_dev *pdev = dev->pdev;
  914. int option;
  915. if (!mdev->suspended)
  916. return 0;
  917. pci_set_power_state(pdev, PCI_D0);
  918. pci_enable_device(pdev);
  919. /* Disable sysclk */
  920. pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
  921. option &= ~(0x4);
  922. pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
  923. mdev->suspended = false;
  924. return 0;
  925. }
  926. #endif
  927. static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
  928. {
  929. struct drm_device *dev = crtc->dev;
  930. struct mga_device *mdev = dev->dev_private;
  931. u8 seq1 = 0, crtcext1 = 0;
  932. switch (mode) {
  933. case DRM_MODE_DPMS_ON:
  934. seq1 = 0;
  935. crtcext1 = 0;
  936. mga_crtc_load_lut(crtc);
  937. break;
  938. case DRM_MODE_DPMS_STANDBY:
  939. seq1 = 0x20;
  940. crtcext1 = 0x10;
  941. break;
  942. case DRM_MODE_DPMS_SUSPEND:
  943. seq1 = 0x20;
  944. crtcext1 = 0x20;
  945. break;
  946. case DRM_MODE_DPMS_OFF:
  947. seq1 = 0x20;
  948. crtcext1 = 0x30;
  949. break;
  950. }
  951. #if 0
  952. if (mode == DRM_MODE_DPMS_OFF) {
  953. mga_suspend(crtc);
  954. }
  955. #endif
  956. WREG8(MGAREG_SEQ_INDEX, 0x01);
  957. seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
  958. mga_wait_vsync(mdev);
  959. mga_wait_busy(mdev);
  960. WREG8(MGAREG_SEQ_DATA, seq1);
  961. msleep(20);
  962. WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
  963. crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
  964. WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
  965. #if 0
  966. if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
  967. mga_resume(crtc);
  968. drm_helper_resume_force_mode(dev);
  969. }
  970. #endif
  971. }
  972. /*
  973. * This is called before a mode is programmed. A typical use might be to
  974. * enable DPMS during the programming to avoid seeing intermediate stages,
  975. * but that's not relevant to us
  976. */
  977. static void mga_crtc_prepare(struct drm_crtc *crtc)
  978. {
  979. struct drm_device *dev = crtc->dev;
  980. struct mga_device *mdev = dev->dev_private;
  981. u8 tmp;
  982. /* mga_resume(crtc);*/
  983. WREG8(MGAREG_CRTC_INDEX, 0x11);
  984. tmp = RREG8(MGAREG_CRTC_DATA);
  985. WREG_CRT(0x11, tmp | 0x80);
  986. if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
  987. WREG_SEQ(0, 1);
  988. msleep(50);
  989. WREG_SEQ(1, 0x20);
  990. msleep(20);
  991. } else {
  992. WREG8(MGAREG_SEQ_INDEX, 0x1);
  993. tmp = RREG8(MGAREG_SEQ_DATA);
  994. /* start sync reset */
  995. WREG_SEQ(0, 1);
  996. WREG_SEQ(1, tmp | 0x20);
  997. }
  998. if (mdev->type == G200_WB)
  999. mga_g200wb_prepare(crtc);
  1000. WREG_CRT(17, 0);
  1001. }
  1002. /*
  1003. * This is called after a mode is programmed. It should reverse anything done
  1004. * by the prepare function
  1005. */
  1006. static void mga_crtc_commit(struct drm_crtc *crtc)
  1007. {
  1008. struct drm_device *dev = crtc->dev;
  1009. struct mga_device *mdev = dev->dev_private;
  1010. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1011. u8 tmp;
  1012. if (mdev->type == G200_WB)
  1013. mga_g200wb_commit(crtc);
  1014. if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
  1015. msleep(50);
  1016. WREG_SEQ(1, 0x0);
  1017. msleep(20);
  1018. WREG_SEQ(0, 0x3);
  1019. } else {
  1020. WREG8(MGAREG_SEQ_INDEX, 0x1);
  1021. tmp = RREG8(MGAREG_SEQ_DATA);
  1022. tmp &= ~0x20;
  1023. WREG_SEQ(0x1, tmp);
  1024. WREG_SEQ(0, 3);
  1025. }
  1026. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1027. }
  1028. /*
  1029. * The core can pass us a set of gamma values to program. We actually only
  1030. * use this for 8-bit mode so can't perform smooth fades on deeper modes,
  1031. * but it's a requirement that we provide the function
  1032. */
  1033. static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  1034. u16 *blue, uint32_t start, uint32_t size)
  1035. {
  1036. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1037. int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
  1038. int i;
  1039. for (i = start; i < end; i++) {
  1040. mga_crtc->lut_r[i] = red[i] >> 8;
  1041. mga_crtc->lut_g[i] = green[i] >> 8;
  1042. mga_crtc->lut_b[i] = blue[i] >> 8;
  1043. }
  1044. mga_crtc_load_lut(crtc);
  1045. }
  1046. /* Simple cleanup function */
  1047. static void mga_crtc_destroy(struct drm_crtc *crtc)
  1048. {
  1049. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1050. drm_crtc_cleanup(crtc);
  1051. kfree(mga_crtc);
  1052. }
  1053. /* These provide the minimum set of functions required to handle a CRTC */
  1054. static const struct drm_crtc_funcs mga_crtc_funcs = {
  1055. .gamma_set = mga_crtc_gamma_set,
  1056. .set_config = drm_crtc_helper_set_config,
  1057. .destroy = mga_crtc_destroy,
  1058. };
  1059. static const struct drm_crtc_helper_funcs mga_helper_funcs = {
  1060. .dpms = mga_crtc_dpms,
  1061. .mode_fixup = mga_crtc_mode_fixup,
  1062. .mode_set = mga_crtc_mode_set,
  1063. .mode_set_base = mga_crtc_mode_set_base,
  1064. .prepare = mga_crtc_prepare,
  1065. .commit = mga_crtc_commit,
  1066. .load_lut = mga_crtc_load_lut,
  1067. };
  1068. /* CRTC setup */
  1069. static void mga_crtc_init(struct drm_device *dev)
  1070. {
  1071. struct mga_device *mdev = dev->dev_private;
  1072. struct mga_crtc *mga_crtc;
  1073. int i;
  1074. mga_crtc = kzalloc(sizeof(struct mga_crtc) +
  1075. (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
  1076. GFP_KERNEL);
  1077. if (mga_crtc == NULL)
  1078. return;
  1079. drm_crtc_init(dev, &mga_crtc->base, &mga_crtc_funcs);
  1080. drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
  1081. mdev->mode_info.crtc = mga_crtc;
  1082. for (i = 0; i < MGAG200_LUT_SIZE; i++) {
  1083. mga_crtc->lut_r[i] = i;
  1084. mga_crtc->lut_g[i] = i;
  1085. mga_crtc->lut_b[i] = i;
  1086. }
  1087. drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
  1088. }
  1089. /** Sets the color ramps on behalf of fbcon */
  1090. void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  1091. u16 blue, int regno)
  1092. {
  1093. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1094. mga_crtc->lut_r[regno] = red >> 8;
  1095. mga_crtc->lut_g[regno] = green >> 8;
  1096. mga_crtc->lut_b[regno] = blue >> 8;
  1097. }
  1098. /** Gets the color ramps on behalf of fbcon */
  1099. void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  1100. u16 *blue, int regno)
  1101. {
  1102. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1103. *red = (u16)mga_crtc->lut_r[regno] << 8;
  1104. *green = (u16)mga_crtc->lut_g[regno] << 8;
  1105. *blue = (u16)mga_crtc->lut_b[regno] << 8;
  1106. }
  1107. /*
  1108. * The encoder comes after the CRTC in the output pipeline, but before
  1109. * the connector. It's responsible for ensuring that the digital
  1110. * stream is appropriately converted into the output format. Setup is
  1111. * very simple in this case - all we have to do is inform qemu of the
  1112. * colour depth in order to ensure that it displays appropriately
  1113. */
  1114. /*
  1115. * These functions are analagous to those in the CRTC code, but are intended
  1116. * to handle any encoder-specific limitations
  1117. */
  1118. static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
  1119. const struct drm_display_mode *mode,
  1120. struct drm_display_mode *adjusted_mode)
  1121. {
  1122. return true;
  1123. }
  1124. static void mga_encoder_mode_set(struct drm_encoder *encoder,
  1125. struct drm_display_mode *mode,
  1126. struct drm_display_mode *adjusted_mode)
  1127. {
  1128. }
  1129. static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
  1130. {
  1131. return;
  1132. }
  1133. static void mga_encoder_prepare(struct drm_encoder *encoder)
  1134. {
  1135. }
  1136. static void mga_encoder_commit(struct drm_encoder *encoder)
  1137. {
  1138. }
  1139. void mga_encoder_destroy(struct drm_encoder *encoder)
  1140. {
  1141. struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
  1142. drm_encoder_cleanup(encoder);
  1143. kfree(mga_encoder);
  1144. }
  1145. static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
  1146. .dpms = mga_encoder_dpms,
  1147. .mode_fixup = mga_encoder_mode_fixup,
  1148. .mode_set = mga_encoder_mode_set,
  1149. .prepare = mga_encoder_prepare,
  1150. .commit = mga_encoder_commit,
  1151. };
  1152. static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
  1153. .destroy = mga_encoder_destroy,
  1154. };
  1155. static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
  1156. {
  1157. struct drm_encoder *encoder;
  1158. struct mga_encoder *mga_encoder;
  1159. mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
  1160. if (!mga_encoder)
  1161. return NULL;
  1162. encoder = &mga_encoder->base;
  1163. encoder->possible_crtcs = 0x1;
  1164. drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
  1165. DRM_MODE_ENCODER_DAC);
  1166. drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
  1167. return encoder;
  1168. }
  1169. static int mga_vga_get_modes(struct drm_connector *connector)
  1170. {
  1171. struct mga_connector *mga_connector = to_mga_connector(connector);
  1172. struct edid *edid;
  1173. int ret = 0;
  1174. edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
  1175. if (edid) {
  1176. drm_mode_connector_update_edid_property(connector, edid);
  1177. ret = drm_add_edid_modes(connector, edid);
  1178. kfree(edid);
  1179. }
  1180. return ret;
  1181. }
  1182. static int mga_vga_mode_valid(struct drm_connector *connector,
  1183. struct drm_display_mode *mode)
  1184. {
  1185. /* FIXME: Add bandwidth and g200se limitations */
  1186. if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
  1187. mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
  1188. mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
  1189. mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
  1190. return MODE_BAD;
  1191. }
  1192. return MODE_OK;
  1193. }
  1194. struct drm_encoder *mga_connector_best_encoder(struct drm_connector
  1195. *connector)
  1196. {
  1197. int enc_id = connector->encoder_ids[0];
  1198. struct drm_mode_object *obj;
  1199. struct drm_encoder *encoder;
  1200. /* pick the encoder ids */
  1201. if (enc_id) {
  1202. obj =
  1203. drm_mode_object_find(connector->dev, enc_id,
  1204. DRM_MODE_OBJECT_ENCODER);
  1205. if (!obj)
  1206. return NULL;
  1207. encoder = obj_to_encoder(obj);
  1208. return encoder;
  1209. }
  1210. return NULL;
  1211. }
  1212. static enum drm_connector_status mga_vga_detect(struct drm_connector
  1213. *connector, bool force)
  1214. {
  1215. return connector_status_connected;
  1216. }
  1217. static void mga_connector_destroy(struct drm_connector *connector)
  1218. {
  1219. struct mga_connector *mga_connector = to_mga_connector(connector);
  1220. mgag200_i2c_destroy(mga_connector->i2c);
  1221. drm_connector_cleanup(connector);
  1222. kfree(connector);
  1223. }
  1224. struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
  1225. .get_modes = mga_vga_get_modes,
  1226. .mode_valid = mga_vga_mode_valid,
  1227. .best_encoder = mga_connector_best_encoder,
  1228. };
  1229. struct drm_connector_funcs mga_vga_connector_funcs = {
  1230. .dpms = drm_helper_connector_dpms,
  1231. .detect = mga_vga_detect,
  1232. .fill_modes = drm_helper_probe_single_connector_modes,
  1233. .destroy = mga_connector_destroy,
  1234. };
  1235. static struct drm_connector *mga_vga_init(struct drm_device *dev)
  1236. {
  1237. struct drm_connector *connector;
  1238. struct mga_connector *mga_connector;
  1239. mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
  1240. if (!mga_connector)
  1241. return NULL;
  1242. connector = &mga_connector->base;
  1243. drm_connector_init(dev, connector,
  1244. &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  1245. drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
  1246. mga_connector->i2c = mgag200_i2c_create(dev);
  1247. if (!mga_connector->i2c)
  1248. DRM_ERROR("failed to add ddc bus\n");
  1249. return connector;
  1250. }
  1251. int mgag200_modeset_init(struct mga_device *mdev)
  1252. {
  1253. struct drm_encoder *encoder;
  1254. struct drm_connector *connector;
  1255. int ret;
  1256. mdev->mode_info.mode_config_initialized = true;
  1257. mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
  1258. mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
  1259. mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
  1260. mga_crtc_init(mdev->dev);
  1261. encoder = mga_encoder_init(mdev->dev);
  1262. if (!encoder) {
  1263. DRM_ERROR("mga_encoder_init failed\n");
  1264. return -1;
  1265. }
  1266. connector = mga_vga_init(mdev->dev);
  1267. if (!connector) {
  1268. DRM_ERROR("mga_vga_init failed\n");
  1269. return -1;
  1270. }
  1271. drm_mode_connector_attach_encoder(connector, encoder);
  1272. ret = mgag200_fbdev_init(mdev);
  1273. if (ret) {
  1274. DRM_ERROR("mga_fbdev_init failed\n");
  1275. return ret;
  1276. }
  1277. return 0;
  1278. }
  1279. void mgag200_modeset_fini(struct mga_device *mdev)
  1280. {
  1281. }