mgag200_mode.c 40 KB

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