mgag200_mode.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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->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. 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->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. DRM_INFO("mga base %llx\n", gpu_addr);
  653. mga_set_start_address(crtc, (u32)gpu_addr);
  654. return 0;
  655. }
  656. static int mga_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  657. struct drm_framebuffer *old_fb)
  658. {
  659. return mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
  660. }
  661. static int mga_crtc_mode_set(struct drm_crtc *crtc,
  662. struct drm_display_mode *mode,
  663. struct drm_display_mode *adjusted_mode,
  664. int x, int y, struct drm_framebuffer *old_fb)
  665. {
  666. struct drm_device *dev = crtc->dev;
  667. struct mga_device *mdev = dev->dev_private;
  668. int hdisplay, hsyncstart, hsyncend, htotal;
  669. int vdisplay, vsyncstart, vsyncend, vtotal;
  670. int pitch;
  671. int option = 0, option2 = 0;
  672. int i;
  673. unsigned char misc = 0;
  674. unsigned char ext_vga[6];
  675. u8 bppshift;
  676. static unsigned char dacvalue[] = {
  677. /* 0x00: */ 0, 0, 0, 0, 0, 0, 0x00, 0,
  678. /* 0x08: */ 0, 0, 0, 0, 0, 0, 0, 0,
  679. /* 0x10: */ 0, 0, 0, 0, 0, 0, 0, 0,
  680. /* 0x18: */ 0x00, 0, 0xC9, 0xFF, 0xBF, 0x20, 0x1F, 0x20,
  681. /* 0x20: */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  682. /* 0x28: */ 0x00, 0x00, 0x00, 0x00, 0, 0, 0, 0x40,
  683. /* 0x30: */ 0x00, 0xB0, 0x00, 0xC2, 0x34, 0x14, 0x02, 0x83,
  684. /* 0x38: */ 0x00, 0x93, 0x00, 0x77, 0x00, 0x00, 0x00, 0x3A,
  685. /* 0x40: */ 0, 0, 0, 0, 0, 0, 0, 0,
  686. /* 0x48: */ 0, 0, 0, 0, 0, 0, 0, 0
  687. };
  688. bppshift = mdev->bpp_shifts[(crtc->fb->bits_per_pixel >> 3) - 1];
  689. switch (mdev->type) {
  690. case G200_SE_A:
  691. case G200_SE_B:
  692. dacvalue[MGA1064_VREF_CTL] = 0x03;
  693. dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
  694. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_DAC_EN |
  695. MGA1064_MISC_CTL_VGA8 |
  696. MGA1064_MISC_CTL_DAC_RAM_CS;
  697. if (mdev->has_sdram)
  698. option = 0x40049120;
  699. else
  700. option = 0x4004d120;
  701. option2 = 0x00008000;
  702. break;
  703. case G200_WB:
  704. dacvalue[MGA1064_VREF_CTL] = 0x07;
  705. option = 0x41049120;
  706. option2 = 0x0000b000;
  707. break;
  708. case G200_EV:
  709. dacvalue[MGA1064_PIX_CLK_CTL] = MGA1064_PIX_CLK_CTL_SEL_PLL;
  710. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
  711. MGA1064_MISC_CTL_DAC_RAM_CS;
  712. option = 0x00000120;
  713. option2 = 0x0000b000;
  714. break;
  715. case G200_EH:
  716. dacvalue[MGA1064_MISC_CTL] = MGA1064_MISC_CTL_VGA8 |
  717. MGA1064_MISC_CTL_DAC_RAM_CS;
  718. option = 0x00000120;
  719. option2 = 0x0000b000;
  720. break;
  721. case G200_ER:
  722. break;
  723. }
  724. switch (crtc->fb->bits_per_pixel) {
  725. case 8:
  726. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_8bits;
  727. break;
  728. case 16:
  729. if (crtc->fb->depth == 15)
  730. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_15bits;
  731. else
  732. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_16bits;
  733. break;
  734. case 24:
  735. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_24bits;
  736. break;
  737. case 32:
  738. dacvalue[MGA1064_MUL_CTL] = MGA1064_MUL_CTL_32_24bits;
  739. break;
  740. }
  741. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  742. misc |= 0x40;
  743. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  744. misc |= 0x80;
  745. for (i = 0; i < sizeof(dacvalue); i++) {
  746. if ((i <= 0x17) ||
  747. (i == 0x1b) ||
  748. (i == 0x1c) ||
  749. ((i >= 0x1f) && (i <= 0x29)) ||
  750. ((i >= 0x30) && (i <= 0x37)))
  751. continue;
  752. if (IS_G200_SE(mdev) &&
  753. ((i == 0x2c) || (i == 0x2d) || (i == 0x2e)))
  754. continue;
  755. if ((mdev->type == G200_EV || mdev->type == G200_WB || mdev->type == G200_EH) &&
  756. (i >= 0x44) && (i <= 0x4e))
  757. continue;
  758. WREG_DAC(i, dacvalue[i]);
  759. }
  760. if (mdev->type == G200_ER)
  761. WREG_DAC(0x90, 0);
  762. if (option)
  763. pci_write_config_dword(dev->pdev, PCI_MGA_OPTION, option);
  764. if (option2)
  765. pci_write_config_dword(dev->pdev, PCI_MGA_OPTION2, option2);
  766. WREG_SEQ(2, 0xf);
  767. WREG_SEQ(3, 0);
  768. WREG_SEQ(4, 0xe);
  769. pitch = crtc->fb->pitches[0] / (crtc->fb->bits_per_pixel / 8);
  770. if (crtc->fb->bits_per_pixel == 24)
  771. pitch = (pitch * 3) >> (4 - bppshift);
  772. else
  773. pitch = pitch >> (4 - bppshift);
  774. hdisplay = mode->hdisplay / 8 - 1;
  775. hsyncstart = mode->hsync_start / 8 - 1;
  776. hsyncend = mode->hsync_end / 8 - 1;
  777. htotal = mode->htotal / 8 - 1;
  778. /* Work around hardware quirk */
  779. if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
  780. htotal++;
  781. vdisplay = mode->vdisplay - 1;
  782. vsyncstart = mode->vsync_start - 1;
  783. vsyncend = mode->vsync_end - 1;
  784. vtotal = mode->vtotal - 2;
  785. WREG_GFX(0, 0);
  786. WREG_GFX(1, 0);
  787. WREG_GFX(2, 0);
  788. WREG_GFX(3, 0);
  789. WREG_GFX(4, 0);
  790. WREG_GFX(5, 0x40);
  791. WREG_GFX(6, 0x5);
  792. WREG_GFX(7, 0xf);
  793. WREG_GFX(8, 0xf);
  794. WREG_CRT(0, htotal - 4);
  795. WREG_CRT(1, hdisplay);
  796. WREG_CRT(2, hdisplay);
  797. WREG_CRT(3, (htotal & 0x1F) | 0x80);
  798. WREG_CRT(4, hsyncstart);
  799. WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
  800. WREG_CRT(6, vtotal & 0xFF);
  801. WREG_CRT(7, ((vtotal & 0x100) >> 8) |
  802. ((vdisplay & 0x100) >> 7) |
  803. ((vsyncstart & 0x100) >> 6) |
  804. ((vdisplay & 0x100) >> 5) |
  805. ((vdisplay & 0x100) >> 4) | /* linecomp */
  806. ((vtotal & 0x200) >> 4)|
  807. ((vdisplay & 0x200) >> 3) |
  808. ((vsyncstart & 0x200) >> 2));
  809. WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
  810. ((vdisplay & 0x200) >> 3));
  811. WREG_CRT(10, 0);
  812. WREG_CRT(11, 0);
  813. WREG_CRT(12, 0);
  814. WREG_CRT(13, 0);
  815. WREG_CRT(14, 0);
  816. WREG_CRT(15, 0);
  817. WREG_CRT(16, vsyncstart & 0xFF);
  818. WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
  819. WREG_CRT(18, vdisplay & 0xFF);
  820. WREG_CRT(19, pitch & 0xFF);
  821. WREG_CRT(20, 0);
  822. WREG_CRT(21, vdisplay & 0xFF);
  823. WREG_CRT(22, (vtotal + 1) & 0xFF);
  824. WREG_CRT(23, 0xc3);
  825. WREG_CRT(24, vdisplay & 0xFF);
  826. ext_vga[0] = 0;
  827. ext_vga[5] = 0;
  828. /* TODO interlace */
  829. ext_vga[0] |= (pitch & 0x300) >> 4;
  830. ext_vga[1] = (((htotal - 4) & 0x100) >> 8) |
  831. ((hdisplay & 0x100) >> 7) |
  832. ((hsyncstart & 0x100) >> 6) |
  833. (htotal & 0x40);
  834. ext_vga[2] = ((vtotal & 0xc00) >> 10) |
  835. ((vdisplay & 0x400) >> 8) |
  836. ((vdisplay & 0xc00) >> 7) |
  837. ((vsyncstart & 0xc00) >> 5) |
  838. ((vdisplay & 0x400) >> 3);
  839. if (crtc->fb->bits_per_pixel == 24)
  840. ext_vga[3] = (((1 << bppshift) * 3) - 1) | 0x80;
  841. else
  842. ext_vga[3] = ((1 << bppshift) - 1) | 0x80;
  843. ext_vga[4] = 0;
  844. if (mdev->type == G200_WB)
  845. ext_vga[1] |= 0x88;
  846. /* Set pixel clocks */
  847. misc = 0x2d;
  848. WREG8(MGA_MISC_OUT, misc);
  849. mga_crtc_set_plls(mdev, mode->clock);
  850. for (i = 0; i < 6; i++) {
  851. WREG_ECRT(i, ext_vga[i]);
  852. }
  853. if (mdev->type == G200_ER)
  854. WREG_ECRT(0x24, 0x5);
  855. if (mdev->type == G200_EV) {
  856. WREG_ECRT(6, 0);
  857. }
  858. WREG_ECRT(0, ext_vga[0]);
  859. /* Enable mga pixel clock */
  860. misc = 0x2d;
  861. WREG8(MGA_MISC_OUT, misc);
  862. if (adjusted_mode)
  863. memcpy(&mdev->mode, mode, sizeof(struct drm_display_mode));
  864. mga_crtc_do_set_base(crtc, old_fb, x, y, 0);
  865. /* reset tagfifo */
  866. if (mdev->type == G200_ER) {
  867. u32 mem_ctl = RREG32(MGAREG_MEMCTL);
  868. u8 seq1;
  869. /* screen off */
  870. WREG8(MGAREG_SEQ_INDEX, 0x01);
  871. seq1 = RREG8(MGAREG_SEQ_DATA) | 0x20;
  872. WREG8(MGAREG_SEQ_DATA, seq1);
  873. WREG32(MGAREG_MEMCTL, mem_ctl | 0x00200000);
  874. udelay(1000);
  875. WREG32(MGAREG_MEMCTL, mem_ctl & ~0x00200000);
  876. WREG8(MGAREG_SEQ_DATA, seq1 & ~0x20);
  877. }
  878. if (IS_G200_SE(mdev)) {
  879. if (mdev->unique_rev_id >= 0x02) {
  880. u8 hi_pri_lvl;
  881. u32 bpp;
  882. u32 mb;
  883. if (crtc->fb->bits_per_pixel > 16)
  884. bpp = 32;
  885. else if (crtc->fb->bits_per_pixel > 8)
  886. bpp = 16;
  887. else
  888. bpp = 8;
  889. mb = (mode->clock * bpp) / 1000;
  890. if (mb > 3100)
  891. hi_pri_lvl = 0;
  892. else if (mb > 2600)
  893. hi_pri_lvl = 1;
  894. else if (mb > 1900)
  895. hi_pri_lvl = 2;
  896. else if (mb > 1160)
  897. hi_pri_lvl = 3;
  898. else if (mb > 440)
  899. hi_pri_lvl = 4;
  900. else
  901. hi_pri_lvl = 5;
  902. WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
  903. WREG8(MGAREG_CRTCEXT_DATA, hi_pri_lvl);
  904. } else {
  905. WREG8(MGAREG_CRTCEXT_INDEX, 0x06);
  906. if (mdev->unique_rev_id >= 0x01)
  907. WREG8(MGAREG_CRTCEXT_DATA, 0x03);
  908. else
  909. WREG8(MGAREG_CRTCEXT_DATA, 0x04);
  910. }
  911. }
  912. return 0;
  913. }
  914. #if 0 /* code from mjg to attempt D3 on crtc dpms off - revisit later */
  915. static int mga_suspend(struct drm_crtc *crtc)
  916. {
  917. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  918. struct drm_device *dev = crtc->dev;
  919. struct mga_device *mdev = dev->dev_private;
  920. struct pci_dev *pdev = dev->pdev;
  921. int option;
  922. if (mdev->suspended)
  923. return 0;
  924. WREG_SEQ(1, 0x20);
  925. WREG_ECRT(1, 0x30);
  926. /* Disable the pixel clock */
  927. WREG_DAC(0x1a, 0x05);
  928. /* Power down the DAC */
  929. WREG_DAC(0x1e, 0x18);
  930. /* Power down the pixel PLL */
  931. WREG_DAC(0x1a, 0x0d);
  932. /* Disable PLLs and clocks */
  933. pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
  934. option &= ~(0x1F8024);
  935. pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
  936. pci_set_power_state(pdev, PCI_D3hot);
  937. pci_disable_device(pdev);
  938. mdev->suspended = true;
  939. return 0;
  940. }
  941. static int mga_resume(struct drm_crtc *crtc)
  942. {
  943. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  944. struct drm_device *dev = crtc->dev;
  945. struct mga_device *mdev = dev->dev_private;
  946. struct pci_dev *pdev = dev->pdev;
  947. int option;
  948. if (!mdev->suspended)
  949. return 0;
  950. pci_set_power_state(pdev, PCI_D0);
  951. pci_enable_device(pdev);
  952. /* Disable sysclk */
  953. pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
  954. option &= ~(0x4);
  955. pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
  956. mdev->suspended = false;
  957. return 0;
  958. }
  959. #endif
  960. static void mga_crtc_dpms(struct drm_crtc *crtc, int mode)
  961. {
  962. struct drm_device *dev = crtc->dev;
  963. struct mga_device *mdev = dev->dev_private;
  964. u8 seq1 = 0, crtcext1 = 0;
  965. switch (mode) {
  966. case DRM_MODE_DPMS_ON:
  967. seq1 = 0;
  968. crtcext1 = 0;
  969. mga_crtc_load_lut(crtc);
  970. break;
  971. case DRM_MODE_DPMS_STANDBY:
  972. seq1 = 0x20;
  973. crtcext1 = 0x10;
  974. break;
  975. case DRM_MODE_DPMS_SUSPEND:
  976. seq1 = 0x20;
  977. crtcext1 = 0x20;
  978. break;
  979. case DRM_MODE_DPMS_OFF:
  980. seq1 = 0x20;
  981. crtcext1 = 0x30;
  982. break;
  983. }
  984. #if 0
  985. if (mode == DRM_MODE_DPMS_OFF) {
  986. mga_suspend(crtc);
  987. }
  988. #endif
  989. WREG8(MGAREG_SEQ_INDEX, 0x01);
  990. seq1 |= RREG8(MGAREG_SEQ_DATA) & ~0x20;
  991. mga_wait_vsync(mdev);
  992. mga_wait_busy(mdev);
  993. WREG8(MGAREG_SEQ_DATA, seq1);
  994. msleep(20);
  995. WREG8(MGAREG_CRTCEXT_INDEX, 0x01);
  996. crtcext1 |= RREG8(MGAREG_CRTCEXT_DATA) & ~0x30;
  997. WREG8(MGAREG_CRTCEXT_DATA, crtcext1);
  998. #if 0
  999. if (mode == DRM_MODE_DPMS_ON && mdev->suspended == true) {
  1000. mga_resume(crtc);
  1001. drm_helper_resume_force_mode(dev);
  1002. }
  1003. #endif
  1004. }
  1005. /*
  1006. * This is called before a mode is programmed. A typical use might be to
  1007. * enable DPMS during the programming to avoid seeing intermediate stages,
  1008. * but that's not relevant to us
  1009. */
  1010. static void mga_crtc_prepare(struct drm_crtc *crtc)
  1011. {
  1012. struct drm_device *dev = crtc->dev;
  1013. struct mga_device *mdev = dev->dev_private;
  1014. u8 tmp;
  1015. /* mga_resume(crtc);*/
  1016. WREG8(MGAREG_CRTC_INDEX, 0x11);
  1017. tmp = RREG8(MGAREG_CRTC_DATA);
  1018. WREG_CRT(0x11, tmp | 0x80);
  1019. if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
  1020. WREG_SEQ(0, 1);
  1021. msleep(50);
  1022. WREG_SEQ(1, 0x20);
  1023. msleep(20);
  1024. } else {
  1025. WREG8(MGAREG_SEQ_INDEX, 0x1);
  1026. tmp = RREG8(MGAREG_SEQ_DATA);
  1027. /* start sync reset */
  1028. WREG_SEQ(0, 1);
  1029. WREG_SEQ(1, tmp | 0x20);
  1030. }
  1031. if (mdev->type == G200_WB)
  1032. mga_g200wb_prepare(crtc);
  1033. WREG_CRT(17, 0);
  1034. }
  1035. /*
  1036. * This is called after a mode is programmed. It should reverse anything done
  1037. * by the prepare function
  1038. */
  1039. static void mga_crtc_commit(struct drm_crtc *crtc)
  1040. {
  1041. struct drm_device *dev = crtc->dev;
  1042. struct mga_device *mdev = dev->dev_private;
  1043. struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
  1044. u8 tmp;
  1045. if (mdev->type == G200_WB)
  1046. mga_g200wb_commit(crtc);
  1047. if (mdev->type == G200_SE_A || mdev->type == G200_SE_B) {
  1048. msleep(50);
  1049. WREG_SEQ(1, 0x0);
  1050. msleep(20);
  1051. WREG_SEQ(0, 0x3);
  1052. } else {
  1053. WREG8(MGAREG_SEQ_INDEX, 0x1);
  1054. tmp = RREG8(MGAREG_SEQ_DATA);
  1055. tmp &= ~0x20;
  1056. WREG_SEQ(0x1, tmp);
  1057. WREG_SEQ(0, 3);
  1058. }
  1059. crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
  1060. }
  1061. /*
  1062. * The core can pass us a set of gamma values to program. We actually only
  1063. * use this for 8-bit mode so can't perform smooth fades on deeper modes,
  1064. * but it's a requirement that we provide the function
  1065. */
  1066. static void mga_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  1067. u16 *blue, uint32_t start, uint32_t size)
  1068. {
  1069. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1070. int end = (start + size > MGAG200_LUT_SIZE) ? MGAG200_LUT_SIZE : start + size;
  1071. int i;
  1072. for (i = start; i < end; i++) {
  1073. mga_crtc->lut_r[i] = red[i] >> 8;
  1074. mga_crtc->lut_g[i] = green[i] >> 8;
  1075. mga_crtc->lut_b[i] = blue[i] >> 8;
  1076. }
  1077. mga_crtc_load_lut(crtc);
  1078. }
  1079. /* Simple cleanup function */
  1080. static void mga_crtc_destroy(struct drm_crtc *crtc)
  1081. {
  1082. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1083. drm_crtc_cleanup(crtc);
  1084. kfree(mga_crtc);
  1085. }
  1086. static void mga_crtc_disable(struct drm_crtc *crtc)
  1087. {
  1088. int ret;
  1089. DRM_DEBUG_KMS("\n");
  1090. mga_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  1091. if (crtc->fb) {
  1092. struct mga_framebuffer *mga_fb = to_mga_framebuffer(crtc->fb);
  1093. struct drm_gem_object *obj = mga_fb->obj;
  1094. struct mgag200_bo *bo = gem_to_mga_bo(obj);
  1095. ret = mgag200_bo_reserve(bo, false);
  1096. if (ret)
  1097. return;
  1098. mgag200_bo_push_sysram(bo);
  1099. mgag200_bo_unreserve(bo);
  1100. }
  1101. crtc->fb = NULL;
  1102. }
  1103. /* These provide the minimum set of functions required to handle a CRTC */
  1104. static const struct drm_crtc_funcs mga_crtc_funcs = {
  1105. .cursor_set = mga_crtc_cursor_set,
  1106. .cursor_move = mga_crtc_cursor_move,
  1107. .gamma_set = mga_crtc_gamma_set,
  1108. .set_config = drm_crtc_helper_set_config,
  1109. .destroy = mga_crtc_destroy,
  1110. };
  1111. static const struct drm_crtc_helper_funcs mga_helper_funcs = {
  1112. .disable = mga_crtc_disable,
  1113. .dpms = mga_crtc_dpms,
  1114. .mode_fixup = mga_crtc_mode_fixup,
  1115. .mode_set = mga_crtc_mode_set,
  1116. .mode_set_base = mga_crtc_mode_set_base,
  1117. .prepare = mga_crtc_prepare,
  1118. .commit = mga_crtc_commit,
  1119. .load_lut = mga_crtc_load_lut,
  1120. };
  1121. /* CRTC setup */
  1122. static void mga_crtc_init(struct mga_device *mdev)
  1123. {
  1124. struct mga_crtc *mga_crtc;
  1125. int i;
  1126. mga_crtc = kzalloc(sizeof(struct mga_crtc) +
  1127. (MGAG200FB_CONN_LIMIT * sizeof(struct drm_connector *)),
  1128. GFP_KERNEL);
  1129. if (mga_crtc == NULL)
  1130. return;
  1131. drm_crtc_init(mdev->dev, &mga_crtc->base, &mga_crtc_funcs);
  1132. drm_mode_crtc_set_gamma_size(&mga_crtc->base, MGAG200_LUT_SIZE);
  1133. mdev->mode_info.crtc = mga_crtc;
  1134. for (i = 0; i < MGAG200_LUT_SIZE; i++) {
  1135. mga_crtc->lut_r[i] = i;
  1136. mga_crtc->lut_g[i] = i;
  1137. mga_crtc->lut_b[i] = i;
  1138. }
  1139. drm_crtc_helper_add(&mga_crtc->base, &mga_helper_funcs);
  1140. }
  1141. /** Sets the color ramps on behalf of fbcon */
  1142. void mga_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  1143. u16 blue, int regno)
  1144. {
  1145. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1146. mga_crtc->lut_r[regno] = red >> 8;
  1147. mga_crtc->lut_g[regno] = green >> 8;
  1148. mga_crtc->lut_b[regno] = blue >> 8;
  1149. }
  1150. /** Gets the color ramps on behalf of fbcon */
  1151. void mga_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  1152. u16 *blue, int regno)
  1153. {
  1154. struct mga_crtc *mga_crtc = to_mga_crtc(crtc);
  1155. *red = (u16)mga_crtc->lut_r[regno] << 8;
  1156. *green = (u16)mga_crtc->lut_g[regno] << 8;
  1157. *blue = (u16)mga_crtc->lut_b[regno] << 8;
  1158. }
  1159. /*
  1160. * The encoder comes after the CRTC in the output pipeline, but before
  1161. * the connector. It's responsible for ensuring that the digital
  1162. * stream is appropriately converted into the output format. Setup is
  1163. * very simple in this case - all we have to do is inform qemu of the
  1164. * colour depth in order to ensure that it displays appropriately
  1165. */
  1166. /*
  1167. * These functions are analagous to those in the CRTC code, but are intended
  1168. * to handle any encoder-specific limitations
  1169. */
  1170. static bool mga_encoder_mode_fixup(struct drm_encoder *encoder,
  1171. const struct drm_display_mode *mode,
  1172. struct drm_display_mode *adjusted_mode)
  1173. {
  1174. return true;
  1175. }
  1176. static void mga_encoder_mode_set(struct drm_encoder *encoder,
  1177. struct drm_display_mode *mode,
  1178. struct drm_display_mode *adjusted_mode)
  1179. {
  1180. }
  1181. static void mga_encoder_dpms(struct drm_encoder *encoder, int state)
  1182. {
  1183. return;
  1184. }
  1185. static void mga_encoder_prepare(struct drm_encoder *encoder)
  1186. {
  1187. }
  1188. static void mga_encoder_commit(struct drm_encoder *encoder)
  1189. {
  1190. }
  1191. void mga_encoder_destroy(struct drm_encoder *encoder)
  1192. {
  1193. struct mga_encoder *mga_encoder = to_mga_encoder(encoder);
  1194. drm_encoder_cleanup(encoder);
  1195. kfree(mga_encoder);
  1196. }
  1197. static const struct drm_encoder_helper_funcs mga_encoder_helper_funcs = {
  1198. .dpms = mga_encoder_dpms,
  1199. .mode_fixup = mga_encoder_mode_fixup,
  1200. .mode_set = mga_encoder_mode_set,
  1201. .prepare = mga_encoder_prepare,
  1202. .commit = mga_encoder_commit,
  1203. };
  1204. static const struct drm_encoder_funcs mga_encoder_encoder_funcs = {
  1205. .destroy = mga_encoder_destroy,
  1206. };
  1207. static struct drm_encoder *mga_encoder_init(struct drm_device *dev)
  1208. {
  1209. struct drm_encoder *encoder;
  1210. struct mga_encoder *mga_encoder;
  1211. mga_encoder = kzalloc(sizeof(struct mga_encoder), GFP_KERNEL);
  1212. if (!mga_encoder)
  1213. return NULL;
  1214. encoder = &mga_encoder->base;
  1215. encoder->possible_crtcs = 0x1;
  1216. drm_encoder_init(dev, encoder, &mga_encoder_encoder_funcs,
  1217. DRM_MODE_ENCODER_DAC);
  1218. drm_encoder_helper_add(encoder, &mga_encoder_helper_funcs);
  1219. return encoder;
  1220. }
  1221. static int mga_vga_get_modes(struct drm_connector *connector)
  1222. {
  1223. struct mga_connector *mga_connector = to_mga_connector(connector);
  1224. struct edid *edid;
  1225. int ret = 0;
  1226. edid = drm_get_edid(connector, &mga_connector->i2c->adapter);
  1227. if (edid) {
  1228. drm_mode_connector_update_edid_property(connector, edid);
  1229. ret = drm_add_edid_modes(connector, edid);
  1230. kfree(edid);
  1231. }
  1232. return ret;
  1233. }
  1234. static uint32_t mga_vga_calculate_mode_bandwidth(struct drm_display_mode *mode,
  1235. int bits_per_pixel)
  1236. {
  1237. uint32_t total_area, divisor;
  1238. int64_t active_area, pixels_per_second, bandwidth;
  1239. uint64_t bytes_per_pixel = (bits_per_pixel + 7) / 8;
  1240. divisor = 1024;
  1241. if (!mode->htotal || !mode->vtotal || !mode->clock)
  1242. return 0;
  1243. active_area = mode->hdisplay * mode->vdisplay;
  1244. total_area = mode->htotal * mode->vtotal;
  1245. pixels_per_second = active_area * mode->clock * 1000;
  1246. do_div(pixels_per_second, total_area);
  1247. bandwidth = pixels_per_second * bytes_per_pixel * 100;
  1248. do_div(bandwidth, divisor);
  1249. return (uint32_t)(bandwidth);
  1250. }
  1251. #define MODE_BANDWIDTH MODE_BAD
  1252. static int mga_vga_mode_valid(struct drm_connector *connector,
  1253. struct drm_display_mode *mode)
  1254. {
  1255. struct drm_device *dev = connector->dev;
  1256. struct mga_device *mdev = (struct mga_device*)dev->dev_private;
  1257. struct mga_fbdev *mfbdev = mdev->mfbdev;
  1258. struct drm_fb_helper *fb_helper = &mfbdev->helper;
  1259. struct drm_fb_helper_connector *fb_helper_conn = NULL;
  1260. int bpp = 32;
  1261. int i = 0;
  1262. if (IS_G200_SE(mdev)) {
  1263. if (mdev->unique_rev_id == 0x01) {
  1264. if (mode->hdisplay > 1600)
  1265. return MODE_VIRTUAL_X;
  1266. if (mode->vdisplay > 1200)
  1267. return MODE_VIRTUAL_Y;
  1268. if (mga_vga_calculate_mode_bandwidth(mode, bpp)
  1269. > (24400 * 1024))
  1270. return MODE_BANDWIDTH;
  1271. } else if (mdev->unique_rev_id >= 0x02) {
  1272. if (mode->hdisplay > 1920)
  1273. return MODE_VIRTUAL_X;
  1274. if (mode->vdisplay > 1200)
  1275. return MODE_VIRTUAL_Y;
  1276. if (mga_vga_calculate_mode_bandwidth(mode, bpp)
  1277. > (30100 * 1024))
  1278. return MODE_BANDWIDTH;
  1279. }
  1280. } else if (mdev->type == G200_WB) {
  1281. if (mode->hdisplay > 1280)
  1282. return MODE_VIRTUAL_X;
  1283. if (mode->vdisplay > 1024)
  1284. return MODE_VIRTUAL_Y;
  1285. if (mga_vga_calculate_mode_bandwidth(mode,
  1286. bpp > (31877 * 1024)))
  1287. return MODE_BANDWIDTH;
  1288. } else if (mdev->type == G200_EV &&
  1289. (mga_vga_calculate_mode_bandwidth(mode, bpp)
  1290. > (32700 * 1024))) {
  1291. return MODE_BANDWIDTH;
  1292. } else if (mode->type == G200_EH &&
  1293. (mga_vga_calculate_mode_bandwidth(mode, bpp)
  1294. > (37500 * 1024))) {
  1295. return MODE_BANDWIDTH;
  1296. } else if (mode->type == G200_ER &&
  1297. (mga_vga_calculate_mode_bandwidth(mode,
  1298. bpp) > (55000 * 1024))) {
  1299. return MODE_BANDWIDTH;
  1300. }
  1301. if (mode->crtc_hdisplay > 2048 || mode->crtc_hsync_start > 4096 ||
  1302. mode->crtc_hsync_end > 4096 || mode->crtc_htotal > 4096 ||
  1303. mode->crtc_vdisplay > 2048 || mode->crtc_vsync_start > 4096 ||
  1304. mode->crtc_vsync_end > 4096 || mode->crtc_vtotal > 4096) {
  1305. return MODE_BAD;
  1306. }
  1307. /* Validate the mode input by the user */
  1308. for (i = 0; i < fb_helper->connector_count; i++) {
  1309. if (fb_helper->connector_info[i]->connector == connector) {
  1310. /* Found the helper for this connector */
  1311. fb_helper_conn = fb_helper->connector_info[i];
  1312. if (fb_helper_conn->cmdline_mode.specified) {
  1313. if (fb_helper_conn->cmdline_mode.bpp_specified) {
  1314. bpp = fb_helper_conn->cmdline_mode.bpp;
  1315. }
  1316. }
  1317. }
  1318. }
  1319. if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
  1320. if (fb_helper_conn)
  1321. fb_helper_conn->cmdline_mode.specified = false;
  1322. return MODE_BAD;
  1323. }
  1324. return MODE_OK;
  1325. }
  1326. struct drm_encoder *mga_connector_best_encoder(struct drm_connector
  1327. *connector)
  1328. {
  1329. int enc_id = connector->encoder_ids[0];
  1330. struct drm_mode_object *obj;
  1331. struct drm_encoder *encoder;
  1332. /* pick the encoder ids */
  1333. if (enc_id) {
  1334. obj =
  1335. drm_mode_object_find(connector->dev, enc_id,
  1336. DRM_MODE_OBJECT_ENCODER);
  1337. if (!obj)
  1338. return NULL;
  1339. encoder = obj_to_encoder(obj);
  1340. return encoder;
  1341. }
  1342. return NULL;
  1343. }
  1344. static enum drm_connector_status mga_vga_detect(struct drm_connector
  1345. *connector, bool force)
  1346. {
  1347. return connector_status_connected;
  1348. }
  1349. static void mga_connector_destroy(struct drm_connector *connector)
  1350. {
  1351. struct mga_connector *mga_connector = to_mga_connector(connector);
  1352. mgag200_i2c_destroy(mga_connector->i2c);
  1353. drm_connector_cleanup(connector);
  1354. kfree(connector);
  1355. }
  1356. struct drm_connector_helper_funcs mga_vga_connector_helper_funcs = {
  1357. .get_modes = mga_vga_get_modes,
  1358. .mode_valid = mga_vga_mode_valid,
  1359. .best_encoder = mga_connector_best_encoder,
  1360. };
  1361. struct drm_connector_funcs mga_vga_connector_funcs = {
  1362. .dpms = drm_helper_connector_dpms,
  1363. .detect = mga_vga_detect,
  1364. .fill_modes = drm_helper_probe_single_connector_modes,
  1365. .destroy = mga_connector_destroy,
  1366. };
  1367. static struct drm_connector *mga_vga_init(struct drm_device *dev)
  1368. {
  1369. struct drm_connector *connector;
  1370. struct mga_connector *mga_connector;
  1371. mga_connector = kzalloc(sizeof(struct mga_connector), GFP_KERNEL);
  1372. if (!mga_connector)
  1373. return NULL;
  1374. connector = &mga_connector->base;
  1375. drm_connector_init(dev, connector,
  1376. &mga_vga_connector_funcs, DRM_MODE_CONNECTOR_VGA);
  1377. drm_connector_helper_add(connector, &mga_vga_connector_helper_funcs);
  1378. drm_sysfs_connector_add(connector);
  1379. mga_connector->i2c = mgag200_i2c_create(dev);
  1380. if (!mga_connector->i2c)
  1381. DRM_ERROR("failed to add ddc bus\n");
  1382. return connector;
  1383. }
  1384. int mgag200_modeset_init(struct mga_device *mdev)
  1385. {
  1386. struct drm_encoder *encoder;
  1387. struct drm_connector *connector;
  1388. int ret;
  1389. mdev->mode_info.mode_config_initialized = true;
  1390. mdev->dev->mode_config.max_width = MGAG200_MAX_FB_WIDTH;
  1391. mdev->dev->mode_config.max_height = MGAG200_MAX_FB_HEIGHT;
  1392. mdev->dev->mode_config.fb_base = mdev->mc.vram_base;
  1393. mga_crtc_init(mdev);
  1394. encoder = mga_encoder_init(mdev->dev);
  1395. if (!encoder) {
  1396. DRM_ERROR("mga_encoder_init failed\n");
  1397. return -1;
  1398. }
  1399. connector = mga_vga_init(mdev->dev);
  1400. if (!connector) {
  1401. DRM_ERROR("mga_vga_init failed\n");
  1402. return -1;
  1403. }
  1404. drm_mode_connector_attach_encoder(connector, encoder);
  1405. ret = mgag200_fbdev_init(mdev);
  1406. if (ret) {
  1407. DRM_ERROR("mga_fbdev_init failed\n");
  1408. return ret;
  1409. }
  1410. return 0;
  1411. }
  1412. void mgag200_modeset_fini(struct mga_device *mdev)
  1413. {
  1414. }