mgag200_mode.c 41 KB

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