mgag200_mode.c 41 KB

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