mgag200_mode.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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. u8 bppshift;
  634. static unsigned char dacvalue[] = {
  635. /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
  636. /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
  637. /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
  638. /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
  639. /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  640. /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
  641. /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
  642. /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
  643. /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
  644. /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
  645. };
  646. bppshift = mdev->bpp_shifts[(crtc->fb->bits_per_pixel >> 3) - 1];
  647. switch (mdev->type) {
  648. case G200_SE_A:
  649. case G200_SE_B:
  650. dacvalue[MGA1064_VREF_CTL] = 0x03;
  651. dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
  652. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
  653. MGA1064_MISC_CTL_VGA8 |
  654. MGA1064_MISC_CTL_DAC_RAM_CS;
  655. if (mdev->has_sdram)
  656. option = 0x40049120;
  657. else
  658. option = 0x4004d120;
  659. option2 = 0x00008000;
  660. break;
  661. case G200_WB:
  662. dacvalue[MGA1064_VREF_CTL] = 0x07;
  663. option = 0x41049120;
  664. option2 = 0x0000b000;
  665. break;
  666. case G200_EV:
  667. dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
  668. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
  669. MGA1064_MISC_CTL_DAC_RAM_CS;
  670. option = 0x00000120;
  671. option2 = 0x0000b000;
  672. break;
  673. case G200_EH:
  674. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
  675. MGA1064_MISC_CTL_DAC_RAM_CS;
  676. option = 0x00000120;
  677. option2 = 0x0000b000;
  678. break;
  679. case G200_ER:
  680. break;
  681. }
  682. switch (crtc->fb->bits_per_pixel) {
  683. case 8:
  684. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
  685. break;
  686. case 16:
  687. if (crtc->fb->depth == 15)
  688. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
  689. else
  690. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
  691. break;
  692. case 24:
  693. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
  694. break;
  695. case 32:
  696. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
  697. break;
  698. }
  699. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  700. misc |= 0x40;
  701. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  702. misc |= 0x80;
  703. for (i = 0; i < sizeof(dacvalue); i++) {
  704. if ((i <= 0x03) ||
  705. (i == 0x07) ||
  706. (i == 0x0b) ||
  707. (i == 0x0f) ||
  708. ((i >= 0x13) && (i <= 0x17)) ||
  709. (i == 0x1b) ||
  710. (i == 0x1c) ||
  711. ((i >= 0x1f) && (i <= 0x29)) ||
  712. ((i >= 0x30) && (i <= 0x37)))
  713. continue;
  714. if (IS_G200_SE(mdev) &&
  715. ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
  716. continue;
  717. if ((mdev->type == G200_EV || mdev->type == G200_WB || mdev->type == G200_EH) &&
  718. (i >= 0x44) && (i <= 0x4e))
  719. continue;
  720. WREG_DAC(i, dacvalue[i]);
  721. }
  722. if (mdev->type == G200_ER)
  723. WREG_DAC(0x90, 0);
  724. if (option)
  725. pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
  726. if (option2)
  727. pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
  728. WREG_SEQ(2, 0xf);
  729. WREG_SEQ(3, 0);
  730. WREG_SEQ(4, 0xe);
  731. pitch = crtc->fb->pitches[0] / (crtc->fb->bits_per_pixel / 8);
  732. if (crtc->fb->bits_per_pixel == 24)
  733. pitch = pitch >> (4 - bppshift);
  734. else
  735. pitch = pitch >> (4 - bppshift);
  736. hdisplay = mode->hdisplay / 8 - 1;
  737. hsyncstart = mode->hsync_start / 8 - 1;
  738. hsyncend = mode->hsync_end / 8 - 1;
  739. htotal = mode->htotal / 8 - 1;
  740. /* Work around hardware quirk */
  741. if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
  742. htotal++;
  743. vdisplay = mode->vdisplay - 1;
  744. vsyncstart = mode->vsync_start - 1;
  745. vsyncend = mode->vsync_end - 1;
  746. vtotal = mode->vtotal - 2;
  747. WREG_GFX(0, 0);
  748. WREG_GFX(1, 0);
  749. WREG_GFX(2, 0);
  750. WREG_GFX(3, 0);
  751. WREG_GFX(4, 0);
  752. WREG_GFX(5, 0x40);
  753. WREG_GFX(6, 0x5);
  754. WREG_GFX(7, 0xf);
  755. WREG_GFX(8, 0xf);
  756. WREG_CRT(0, htotal - 4);
  757. WREG_CRT(1, hdisplay);
  758. WREG_CRT(2, hdisplay);
  759. WREG_CRT(3, (htotal & 0x1F) | 0x80);
  760. WREG_CRT(4, hsyncstart);
  761. WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
  762. WREG_CRT(6, vtotal & 0xFF);
  763. WREG_CRT(7, ((vtotal & 0x100) >> 8) |
  764. ((vdisplay & 0x100) >> 7) |
  765. ((vsyncstart & 0x100) >> 6) |
  766. ((vdisplay & 0x100) >> 5) |
  767. ((vdisplay & 0x100) >> 4) | /* linecomp */
  768. ((vtotal & 0x200) >> 4)|
  769. ((vdisplay & 0x200) >> 3) |
  770. ((vsyncstart & 0x200) >> 2));
  771. WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
  772. ((vdisplay & 0x200) >> 3));
  773. WREG_CRT(10, 0);
  774. WREG_CRT(11, 0);
  775. WREG_CRT(12, 0);
  776. WREG_CRT(13, 0);
  777. WREG_CRT(14, 0);
  778. WREG_CRT(15, 0);
  779. WREG_CRT(16, vsyncstart & 0xFF);
  780. WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
  781. WREG_CRT(18, vdisplay & 0xFF);
  782. WREG_CRT(19, pitch & 0xFF);
  783. WREG_CRT(20, 0);
  784. WREG_CRT(21, vdisplay & 0xFF);
  785. WREG_CRT(22, (vtotal + 1) & 0xFF);
  786. WREG_CRT(23, 0xc3);
  787. WREG_CRT(24, vdisplay & 0xFF);
  788. ext_vga[0] = 0;
  789. ext_vga[5] = 0;
  790. /* TODO interlace */
  791. ext_vga[0] |= (pitch & 0x300) >> 4;
  792. ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
  793. ((hdisplay & 0x100) >> 7) |
  794. ((hsyncstart & 0x100) >> 6) |
  795. (htotal & 0x40);
  796. ext_vga[2] = ((vtotal & 0xc00) >> 10) |
  797. ((vdisplay & 0x400) >> 8) |
  798. ((vdisplay & 0xc00) >> 7) |
  799. ((vsyncstart & 0xc00) >> 5) |
  800. ((vdisplay & 0x400) >> 3);
  801. if (crtc->fb->bits_per_pixel == 24)
  802. ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
  803. else
  804. ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
  805. ext_vga[4] = 0;
  806. if (mdev->type == G200_WB)
  807. ext_vga[1] |= 0x88;
  808. /* Set pixel clocks */
  809. misc = 0x2d;
  810. WREG8(MGA_MISC_OUT, misc);
  811. mga_crtc_set_plls(mdev, mode->clock);
  812. for (i = 0; i < 6; i++) {
  813. WREG_ECRT(i, ext_vga[i]);
  814. }
  815. if (mdev->type == G200_ER)
  816. WREG_ECRT(0x24, 0x5);
  817. if (mdev->type == G200_EV) {
  818. WREG_ECRT(6, 0);
  819. }
  820. WREG_ECRT(0, ext_vga[0]);
  821. /* Enable mga pixel clock */
  822. misc = 0x2d;
  823. WREG8(MGA_MISC_OUT, misc);
  824. if (adjusted_mode)
  825. memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
  826. mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
  827. /* reset tagfifo */
  828. if (mdev->type == G200_ER) {
  829. u32 mem_ctl = RREG32(MGAREG_MEMCTL);
  830. u8 seq1;
  831. /* screen off */
  832. WREG8(MGAREG_SEQ_INDEX, 0x01);
  833. seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
  834. WREG8(MGAREG_SEQ_DATA, seq1);
  835. WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
  836. udelay(1000);
  837. WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
  838. WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
  839. }
  840. if (IS_G200_SE(mdev)) {
  841. if (mdev->reg_1e24 >= 0x02) {
  842. u8 hi_pri_lvl;
  843. u32 bpp;
  844. u32 mb;
  845. if (crtc->fb->bits_per_pixel > 16)
  846. bpp = 32;
  847. else if (crtc->fb->bits_per_pixel > 8)
  848. bpp = 16;
  849. else
  850. bpp = 8;
  851. mb = (mode->clock * bpp) / 1000;
  852. if (mb > 3100)
  853. hi_pri_lvl = 0;
  854. else if (mb > 2600)
  855. hi_pri_lvl = 1;
  856. else if (mb > 1900)
  857. hi_pri_lvl = 2;
  858. else if (mb > 1160)
  859. hi_pri_lvl = 3;
  860. else if (mb > 440)
  861. hi_pri_lvl = 4;
  862. else
  863. hi_pri_lvl = 5;
  864. WREG8(0x1fde, 0x06);
  865. WREG8(0x1fdf, hi_pri_lvl);
  866. } else {
  867. if (mdev->reg_1e24 >= 0x01)
  868. WREG8(0x1fdf, 0x03);
  869. else
  870. WREG8(0x1fdf, 0x04);
  871. }
  872. }
  873. return 0;
  874. }
  875. #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
  876. static int mga_suspend(struct drm_crtc *crtc)
  877. {
  878. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  879. struct drm_device *dev = crtc->dev;
  880. struct mga_device *mdev = dev->dev_private;
  881. struct pci_dev *pdev = dev->pdev;
  882. int option;
  883. if (mdev->suspended)
  884. return 0;
  885. WREG_SEQ(1, 0x20);
  886. WREG_ECRT(1, 0x30);
  887. /* Disable the pixel clock */
  888. WREG_DAC(0x1a, 0x05);
  889. /* Power down the DAC */
  890. WREG_DAC(0x1e, 0x18);
  891. /* Power down the pixel PLL */
  892. WREG_DAC(0x1a, 0x0d);
  893. /* Disable PLLs and clocks */
  894. pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
  895. option &= ~(0x1F8024);
  896. pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
  897. pci_set_power_state(pdev, PCI_D3hot);
  898. pci_disable_device(pdev);
  899. mdev->suspended = true;
  900. return 0;
  901. }
  902. static int mga_resume(struct drm_crtc *crtc)
  903. {
  904. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  905. struct drm_device *dev = crtc->dev;
  906. struct mga_device *mdev = dev->dev_private;
  907. struct pci_dev *pdev = dev->pdev;
  908. int option;
  909. if (!mdev->suspended)
  910. return 0;
  911. pci_set_power_state(pdev, PCI_D0);
  912. pci_enable_device(pdev);
  913. /* Disable sysclk */
  914. pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
  915. option &= ~(0x4);
  916. pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
  917. mdev->suspended = false;
  918. return 0;
  919. }
  920. #endif
  921. static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
  922. {
  923. struct drm_device *dev = crtc->dev;
  924. struct mga_device *mdev = dev->dev_private;
  925. u8 seq1 = 0, crtcext1 = 0;
  926. switch (mode) {
  927. case DRM_MODE_DPMS_ON:
  928. seq1 = 0;
  929. crtcext1 = 0;
  930. mga_crtc_load_lut(crtc);
  931. break;
  932. case DRM_MODE_DPMS_STANDBY:
  933. seq1 = 0x20;
  934. crtcext1 = 0x10;
  935. break;
  936. case DRM_MODE_DPMS_SUSPEND:
  937. seq1 = 0x20;
  938. crtcext1 = 0x20;
  939. break;
  940. case DRM_MODE_DPMS_OFF:
  941. seq1 = 0x20;
  942. crtcext1 = 0x30;
  943. break;
  944. }
  945. #if 0
  946. if (mode == DRM_MODE_DPMS_OFF) {
  947. mga_suspend(crtc);
  948. }
  949. #endif
  950. WREG8(MGAREG_SEQ_INDEX, 0x01);
  951. seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
  952. mga_wait_vsync(mdev);
  953. mga_wait_busy(mdev);
  954. WREG8(MGAREG_SEQ_DATA, seq1);
  955. msleep(20);
  956. WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
  957. crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
  958. WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
  959. #if 0
  960. if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
  961. mga_resume(crtc);
  962. drm_helper_resume_force_mode(dev);
  963. }
  964. #endif
  965. }
  966. /*
  967. * This is called before a mode is programmed. A typical use might be to
  968. * enable DPMS during the programming to avoid seeing intermediate stages,
  969. * but that's not relevant to us
  970. */
  971. static void mga_crtc_prepare(struct drm_crtc *crtc)
  972. {
  973. struct drm_device *dev = crtc->dev;
  974. struct mga_device *mdev = dev->dev_private;
  975. u8 tmp;
  976. /* mga_resume(crtc);*/
  977. WREG8(MGAREG_CRTC_INDEX, 0x11);
  978. tmp = RREG8(MGAREG_CRTC_DATA);
  979. WREG_CRT(0x11, tmp | 0x80);
  980. if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
  981. WREG_SEQ(0, 1);
  982. msleep(50);
  983. WREG_SEQ(1, 0x20);
  984. msleep(20);
  985. } else {
  986. WREG8(MGAREG_SEQ_INDEX, 0x1);
  987. tmp = RREG8(MGAREG_SEQ_DATA);
  988. /* start sync reset */
  989. WREG_SEQ(0, 1);
  990. WREG_SEQ(1, tmp | 0x20);
  991. }
  992. if (mdev->type == G200_WB)
  993. mga_g200wb_prepare(crtc);
  994. WREG_CRT(17, 0);
  995. }
  996. /*
  997. * This is called after a mode is programmed. It should reverse anything done
  998. * by the prepare function
  999. */
  1000. static void mga_crtc_commit(struct drm_crtc *crtc)
  1001. {
  1002. struct drm_device *dev = crtc->dev;
  1003. struct mga_device *mdev = dev->dev_private;
  1004. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1005. u8 tmp;
  1006. if (mdev->type == G200_WB)
  1007. mga_g200wb_commit(crtc);
  1008. if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
  1009. msleep(50);
  1010. WREG_SEQ(1, 0x0);
  1011. msleep(20);
  1012. WREG_SEQ(0, 0x3);
  1013. } else {
  1014. WREG8(MGAREG_SEQ_INDEX, 0x1);
  1015. tmp = RREG8(MGAREG_SEQ_DATA);
  1016. tmp &= ~0x20;
  1017. WREG_SEQ(0x1, tmp);
  1018. WREG_SEQ(0, 3);
  1019. }
  1020. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1021. }
  1022. /*
  1023. * The core can pass us a set of gamma values to program. We actually only
  1024. * use this for 8-bit mode so can't perform smooth fades on deeper modes,
  1025. * but it's a requirement that we provide the function
  1026. */
  1027. static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  1028. u16 *blue, uint32_t start, uint32_t size)
  1029. {
  1030. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1031. int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
  1032. int i;
  1033. for (i = start; i < end; i++) {
  1034. mga_crtc->lut_r[i] = red[i] >> 8;
  1035. mga_crtc->lut_g[i] = green[i] >> 8;
  1036. mga_crtc->lut_b[i] = blue[i] >> 8;
  1037. }
  1038. mga_crtc_load_lut(crtc);
  1039. }
  1040. /* Simple cleanup function */
  1041. static void mga_crtc_destroy(struct drm_crtc *crtc)
  1042. {
  1043. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1044. drm_crtc_cleanup(crtc);
  1045. kfree(mga_crtc);
  1046. }
  1047. /* These provide the minimum set of functions required to handle a CRTC */
  1048. static const struct drm_crtc_funcs mga_crtc_funcs = {
  1049. .gamma_set = mga_crtc_gamma_set,
  1050. .set_config = drm_crtc_helper_set_config,
  1051. .destroy = mga_crtc_destroy,
  1052. };
  1053. static const struct drm_crtc_helper_funcs mga_helper_funcs = {
  1054. .dpms = mga_crtc_dpms,
  1055. .mode_fixup = mga_crtc_mode_fixup,
  1056. .mode_set = mga_crtc_mode_set,
  1057. .mode_set_base = mga_crtc_mode_set_base,
  1058. .prepare = mga_crtc_prepare,
  1059. .commit = mga_crtc_commit,
  1060. .load_lut = mga_crtc_load_lut,
  1061. };
  1062. /* CRTC setup */
  1063. static void mga_crtc_init(struct mga_device *mdev)
  1064. {
  1065. struct mga_crtc *mga_crtc;
  1066. int i;
  1067. mga_crtc = kzalloc(sizeof(struct mga_crtc) +
  1068. (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
  1069. GFP_KERNEL);
  1070. if (mga_crtc == NULL)
  1071. return;
  1072. drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
  1073. drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
  1074. mdev->mode_info.crtc = mga_crtc;
  1075. for (i = 0; i < MGAG200_LUT_SIZE; i++) {
  1076. mga_crtc->lut_r[i] = i;
  1077. mga_crtc->lut_g[i] = i;
  1078. mga_crtc->lut_b[i] = i;
  1079. }
  1080. drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
  1081. }
  1082. /** Sets the color ramps on behalf of fbcon */
  1083. void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  1084. u16 blue, int regno)
  1085. {
  1086. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1087. mga_crtc->lut_r[regno] = red >> 8;
  1088. mga_crtc->lut_g[regno] = green >> 8;
  1089. mga_crtc->lut_b[regno] = blue >> 8;
  1090. }
  1091. /** Gets the color ramps on behalf of fbcon */
  1092. void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  1093. u16 *blue, int regno)
  1094. {
  1095. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1096. *red = (u16)mga_crtc->lut_r[regno] << 8;
  1097. *green = (u16)mga_crtc->lut_g[regno] << 8;
  1098. *blue = (u16)mga_crtc->lut_b[regno] << 8;
  1099. }
  1100. /*
  1101. * The encoder comes after the CRTC in the output pipeline, but before
  1102. * the connector. It's responsible for ensuring that the digital
  1103. * stream is appropriately converted into the output format. Setup is
  1104. * very simple in this case - all we have to do is inform qemu of the
  1105. * colour depth in order to ensure that it displays appropriately
  1106. */
  1107. /*
  1108. * These functions are analagous to those in the CRTC code, but are intended
  1109. * to handle any encoder-specific limitations
  1110. */
  1111. static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
  1112. const struct drm_display_mode *mode,
  1113. struct drm_display_mode *adjusted_mode)
  1114. {
  1115. return true;
  1116. }
  1117. static void mga_encoder_mode_set(struct drm_encoder *encoder,
  1118. struct drm_display_mode *mode,
  1119. struct drm_display_mode *adjusted_mode)
  1120. {
  1121. }
  1122. static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
  1123. {
  1124. return;
  1125. }
  1126. static void mga_encoder_prepare(struct drm_encoder *encoder)
  1127. {
  1128. }
  1129. static void mga_encoder_commit(struct drm_encoder *encoder)
  1130. {
  1131. }
  1132. void mga_encoder_destroy(struct drm_encoder *encoder)
  1133. {
  1134. struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
  1135. drm_encoder_cleanup(encoder);
  1136. kfree(mga_encoder);
  1137. }
  1138. static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
  1139. .dpms = mga_encoder_dpms,
  1140. .mode_fixup = mga_encoder_mode_fixup,
  1141. .mode_set = mga_encoder_mode_set,
  1142. .prepare = mga_encoder_prepare,
  1143. .commit = mga_encoder_commit,
  1144. };
  1145. static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
  1146. .destroy = mga_encoder_destroy,
  1147. };
  1148. static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
  1149. {
  1150. struct drm_encoder *encoder;
  1151. struct mga_encoder *mga_encoder;
  1152. mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
  1153. if (!mga_encoder)
  1154. return NULL;
  1155. encoder = &mga_encoder->base;
  1156. encoder->possible_crtcs = 0x1;
  1157. drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
  1158. DRM_MODE_ENCODER_DAC);
  1159. drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
  1160. return encoder;
  1161. }
  1162. static int mga_vga_get_modes(struct drm_connector *connector)
  1163. {
  1164. struct mga_connector *mga_connector = to_mga_connector(connector);
  1165. struct edid *edid;
  1166. int ret = 0;
  1167. edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
  1168. if (edid) {
  1169. drm_mode_connector_update_edid_property(connector, edid);
  1170. ret = drm_add_edid_modes(connector, edid);
  1171. kfree(edid);
  1172. }
  1173. return ret;
  1174. }
  1175. static int mga_vga_mode_valid(struct drm_connector *connector,
  1176. struct drm_display_mode *mode)
  1177. {
  1178. struct drm_device *dev = connector->dev;
  1179. struct mga_device *mdev = (struct mga_device*)dev->dev_private;
  1180. struct mga_fbdev *mfbdev = mdev->mfbdev;
  1181. struct drm_fb_helper *fb_helper = &mfbdev->helper;
  1182. struct drm_fb_helper_connector *fb_helper_conn = NULL;
  1183. int bpp = 32;
  1184. int i = 0;
  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. /* Validate the mode input by the user */
  1193. for (i = 0; i < fb_helper->connector_count; i++) {
  1194. if (fb_helper->connector_info[i]->connector == connector) {
  1195. /* Found the helper for this connector */
  1196. fb_helper_conn = fb_helper->connector_info[i];
  1197. if (fb_helper_conn->cmdline_mode.specified) {
  1198. if (fb_helper_conn->cmdline_mode.bpp_specified) {
  1199. bpp = fb_helper_conn->cmdline_mode.bpp;
  1200. }
  1201. }
  1202. }
  1203. }
  1204. if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
  1205. if (fb_helper_conn)
  1206. fb_helper_conn->cmdline_mode.specified = false;
  1207. return MODE_BAD;
  1208. }
  1209. return MODE_OK;
  1210. }
  1211. struct drm_encoder *mga_connector_best_encoder(struct drm_connector
  1212. *connector)
  1213. {
  1214. int enc_id = connector->encoder_ids[0];
  1215. struct drm_mode_object *obj;
  1216. struct drm_encoder *encoder;
  1217. /* pick the encoder ids */
  1218. if (enc_id) {
  1219. obj =
  1220. drm_mode_object_find(connector->dev, enc_id,
  1221. DRM_MODE_OBJECT_ENCODER);
  1222. if (!obj)
  1223. return NULL;
  1224. encoder = obj_to_encoder(obj);
  1225. return encoder;
  1226. }
  1227. return NULL;
  1228. }
  1229. static enum drm_connector_status mga_vga_detect(struct drm_connector
  1230. *connector, bool force)
  1231. {
  1232. return connector_status_connected;
  1233. }
  1234. static void mga_connector_destroy(struct drm_connector *connector)
  1235. {
  1236. struct mga_connector *mga_connector = to_mga_connector(connector);
  1237. mgag200_i2c_destroy(mga_connector->i2c);
  1238. drm_connector_cleanup(connector);
  1239. kfree(connector);
  1240. }
  1241. struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
  1242. .get_modes = mga_vga_get_modes,
  1243. .mode_valid = mga_vga_mode_valid,
  1244. .best_encoder = mga_connector_best_encoder,
  1245. };
  1246. struct drm_connector_funcs mga_vga_connector_funcs = {
  1247. .dpms = drm_helper_connector_dpms,
  1248. .detect = mga_vga_detect,
  1249. .fill_modes = drm_helper_probe_single_connector_modes,
  1250. .destroy = mga_connector_destroy,
  1251. };
  1252. static struct drm_connector *mga_vga_init(struct drm_device *dev)
  1253. {
  1254. struct drm_connector *connector;
  1255. struct mga_connector *mga_connector;
  1256. mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
  1257. if (!mga_connector)
  1258. return NULL;
  1259. connector = &mga_connector->base;
  1260. drm_connector_init(dev, connector,
  1261. &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  1262. drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
  1263. mga_connector->i2c = mgag200_i2c_create(dev);
  1264. if (!mga_connector->i2c)
  1265. DRM_ERROR("failed to add ddc bus\n");
  1266. return connector;
  1267. }
  1268. int mgag200_modeset_init(struct mga_device *mdev)
  1269. {
  1270. struct drm_encoder *encoder;
  1271. struct drm_connector *connector;
  1272. int ret;
  1273. mdev->mode_info.mode_config_initialized = true;
  1274. mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
  1275. mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
  1276. mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
  1277. mga_crtc_init(mdev);
  1278. encoder = mga_encoder_init(mdev->dev);
  1279. if (!encoder) {
  1280. DRM_ERROR("mga_encoder_init failed\n");
  1281. return -1;
  1282. }
  1283. connector = mga_vga_init(mdev->dev);
  1284. if (!connector) {
  1285. DRM_ERROR("mga_vga_init failed\n");
  1286. return -1;
  1287. }
  1288. drm_mode_connector_attach_encoder(connector, encoder);
  1289. ret = mgag200_fbdev_init(mdev);
  1290. if (ret) {
  1291. DRM_ERROR("mga_fbdev_init failed\n");
  1292. return ret;
  1293. }
  1294. return 0;
  1295. }
  1296. void mgag200_modeset_fini(struct mga_device *mdev)
  1297. {
  1298. }