mgag200_mode.c 36 KB

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