mgag200_mode.c 39 KB

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