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