mgag200_mode.c 41 KB

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