mgag200_mode.c 39 KB

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