mgag200_mode.c 40 KB

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