matroxfb_misc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. *
  3. * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200 and G400
  4. *
  5. * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
  6. *
  7. * Portions Copyright (c) 2001 Matrox Graphics Inc.
  8. *
  9. * Version: 1.65 2002/08/14
  10. *
  11. * MTRR stuff: 1998 Tom Rini <trini@kernel.crashing.org>
  12. *
  13. * Contributors: "menion?" <menion@mindless.com>
  14. * Betatesting, fixes, ideas
  15. *
  16. * "Kurt Garloff" <garloff@suse.de>
  17. * Betatesting, fixes, ideas, videomodes, videomodes timmings
  18. *
  19. * "Tom Rini" <trini@kernel.crashing.org>
  20. * MTRR stuff, PPC cleanups, betatesting, fixes, ideas
  21. *
  22. * "Bibek Sahu" <scorpio@dodds.net>
  23. * Access device through readb|w|l and write b|w|l
  24. * Extensive debugging stuff
  25. *
  26. * "Daniel Haun" <haund@usa.net>
  27. * Testing, hardware cursor fixes
  28. *
  29. * "Scott Wood" <sawst46+@pitt.edu>
  30. * Fixes
  31. *
  32. * "Gerd Knorr" <kraxel@goldbach.isdn.cs.tu-berlin.de>
  33. * Betatesting
  34. *
  35. * "Kelly French" <targon@hazmat.com>
  36. * "Fernando Herrera" <fherrera@eurielec.etsit.upm.es>
  37. * Betatesting, bug reporting
  38. *
  39. * "Pablo Bianucci" <pbian@pccp.com.ar>
  40. * Fixes, ideas, betatesting
  41. *
  42. * "Inaky Perez Gonzalez" <inaky@peloncho.fis.ucm.es>
  43. * Fixes, enhandcements, ideas, betatesting
  44. *
  45. * "Ryuichi Oikawa" <roikawa@rr.iiij4u.or.jp>
  46. * PPC betatesting, PPC support, backward compatibility
  47. *
  48. * "Paul Womar" <Paul@pwomar.demon.co.uk>
  49. * "Owen Waller" <O.Waller@ee.qub.ac.uk>
  50. * PPC betatesting
  51. *
  52. * "Thomas Pornin" <pornin@bolet.ens.fr>
  53. * Alpha betatesting
  54. *
  55. * "Pieter van Leuven" <pvl@iae.nl>
  56. * "Ulf Jaenicke-Roessler" <ujr@physik.phy.tu-dresden.de>
  57. * G100 testing
  58. *
  59. * "H. Peter Arvin" <hpa@transmeta.com>
  60. * Ideas
  61. *
  62. * "Cort Dougan" <cort@cs.nmt.edu>
  63. * CHRP fixes and PReP cleanup
  64. *
  65. * "Mark Vojkovich" <mvojkovi@ucsd.edu>
  66. * G400 support
  67. *
  68. * "David C. Hansen" <haveblue@us.ibm.com>
  69. * Fixes
  70. *
  71. * "Ian Romanick" <idr@us.ibm.com>
  72. * Find PInS data in BIOS on PowerPC systems.
  73. *
  74. * (following author is not in any relation with this code, but his code
  75. * is included in this driver)
  76. *
  77. * Based on framebuffer driver for VBE 2.0 compliant graphic boards
  78. * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
  79. *
  80. * (following author is not in any relation with this code, but his ideas
  81. * were used when writing this driver)
  82. *
  83. * FreeVBE/AF (Matrox), "Shawn Hargreaves" <shawn@talula.demon.co.uk>
  84. *
  85. */
  86. #include "matroxfb_misc.h"
  87. #include <linux/interrupt.h>
  88. #include <linux/matroxfb.h>
  89. void matroxfb_DAC_out(CPMINFO int reg, int val) {
  90. DBG_REG(__func__)
  91. mga_outb(M_RAMDAC_BASE+M_X_INDEX, reg);
  92. mga_outb(M_RAMDAC_BASE+M_X_DATAREG, val);
  93. }
  94. int matroxfb_DAC_in(CPMINFO int reg) {
  95. DBG_REG(__func__)
  96. mga_outb(M_RAMDAC_BASE+M_X_INDEX, reg);
  97. return mga_inb(M_RAMDAC_BASE+M_X_DATAREG);
  98. }
  99. void matroxfb_var2my(struct fb_var_screeninfo* var, struct my_timming* mt) {
  100. unsigned int pixclock = var->pixclock;
  101. DBG(__func__)
  102. if (!pixclock) pixclock = 10000; /* 10ns = 100MHz */
  103. mt->pixclock = 1000000000 / pixclock;
  104. if (mt->pixclock < 1) mt->pixclock = 1;
  105. mt->mnp = -1;
  106. mt->dblscan = var->vmode & FB_VMODE_DOUBLE;
  107. mt->interlaced = var->vmode & FB_VMODE_INTERLACED;
  108. mt->HDisplay = var->xres;
  109. mt->HSyncStart = mt->HDisplay + var->right_margin;
  110. mt->HSyncEnd = mt->HSyncStart + var->hsync_len;
  111. mt->HTotal = mt->HSyncEnd + var->left_margin;
  112. mt->VDisplay = var->yres;
  113. mt->VSyncStart = mt->VDisplay + var->lower_margin;
  114. mt->VSyncEnd = mt->VSyncStart + var->vsync_len;
  115. mt->VTotal = mt->VSyncEnd + var->upper_margin;
  116. mt->sync = var->sync;
  117. }
  118. int matroxfb_PLL_calcclock(const struct matrox_pll_features* pll, unsigned int freq, unsigned int fmax,
  119. unsigned int* in, unsigned int* feed, unsigned int* post) {
  120. unsigned int bestdiff = ~0;
  121. unsigned int bestvco = 0;
  122. unsigned int fxtal = pll->ref_freq;
  123. unsigned int fwant;
  124. unsigned int p;
  125. DBG(__func__)
  126. fwant = freq;
  127. #ifdef DEBUG
  128. printk(KERN_ERR "post_shift_max: %d\n", pll->post_shift_max);
  129. printk(KERN_ERR "ref_freq: %d\n", pll->ref_freq);
  130. printk(KERN_ERR "freq: %d\n", freq);
  131. printk(KERN_ERR "vco_freq_min: %d\n", pll->vco_freq_min);
  132. printk(KERN_ERR "in_div_min: %d\n", pll->in_div_min);
  133. printk(KERN_ERR "in_div_max: %d\n", pll->in_div_max);
  134. printk(KERN_ERR "feed_div_min: %d\n", pll->feed_div_min);
  135. printk(KERN_ERR "feed_div_max: %d\n", pll->feed_div_max);
  136. printk(KERN_ERR "fmax: %d\n", fmax);
  137. #endif
  138. for (p = 1; p <= pll->post_shift_max; p++) {
  139. if (fwant * 2 > fmax)
  140. break;
  141. fwant *= 2;
  142. }
  143. if (fwant < pll->vco_freq_min) fwant = pll->vco_freq_min;
  144. if (fwant > fmax) fwant = fmax;
  145. for (; p-- > 0; fwant >>= 1, bestdiff >>= 1) {
  146. unsigned int m;
  147. if (fwant < pll->vco_freq_min) break;
  148. for (m = pll->in_div_min; m <= pll->in_div_max; m++) {
  149. unsigned int diff, fvco;
  150. unsigned int n;
  151. n = (fwant * (m + 1) + (fxtal >> 1)) / fxtal - 1;
  152. if (n > pll->feed_div_max)
  153. break;
  154. if (n < pll->feed_div_min)
  155. n = pll->feed_div_min;
  156. fvco = (fxtal * (n + 1)) / (m + 1);
  157. if (fvco < fwant)
  158. diff = fwant - fvco;
  159. else
  160. diff = fvco - fwant;
  161. if (diff < bestdiff) {
  162. bestdiff = diff;
  163. *post = p;
  164. *in = m;
  165. *feed = n;
  166. bestvco = fvco;
  167. }
  168. }
  169. }
  170. dprintk(KERN_ERR "clk: %02X %02X %02X %d %d %d\n", *in, *feed, *post, fxtal, bestvco, fwant);
  171. return bestvco;
  172. }
  173. int matroxfb_vgaHWinit(WPMINFO struct my_timming* m) {
  174. unsigned int hd, hs, he, hbe, ht;
  175. unsigned int vd, vs, ve, vt, lc;
  176. unsigned int wd;
  177. unsigned int divider;
  178. int i;
  179. struct matrox_hw_state * const hw = &ACCESS_FBINFO(hw);
  180. DBG(__func__)
  181. hw->SEQ[0] = 0x00;
  182. hw->SEQ[1] = 0x01; /* or 0x09 */
  183. hw->SEQ[2] = 0x0F; /* bitplanes */
  184. hw->SEQ[3] = 0x00;
  185. hw->SEQ[4] = 0x0E;
  186. /* CRTC 0..7, 9, 16..19, 21, 22 are reprogrammed by Matrox Millennium code... Hope that by MGA1064 too */
  187. if (m->dblscan) {
  188. m->VTotal <<= 1;
  189. m->VDisplay <<= 1;
  190. m->VSyncStart <<= 1;
  191. m->VSyncEnd <<= 1;
  192. }
  193. if (m->interlaced) {
  194. m->VTotal >>= 1;
  195. m->VDisplay >>= 1;
  196. m->VSyncStart >>= 1;
  197. m->VSyncEnd >>= 1;
  198. }
  199. /* GCTL is ignored when not using 0xA0000 aperture */
  200. hw->GCTL[0] = 0x00;
  201. hw->GCTL[1] = 0x00;
  202. hw->GCTL[2] = 0x00;
  203. hw->GCTL[3] = 0x00;
  204. hw->GCTL[4] = 0x00;
  205. hw->GCTL[5] = 0x40;
  206. hw->GCTL[6] = 0x05;
  207. hw->GCTL[7] = 0x0F;
  208. hw->GCTL[8] = 0xFF;
  209. /* Whole ATTR is ignored in PowerGraphics mode */
  210. for (i = 0; i < 16; i++)
  211. hw->ATTR[i] = i;
  212. hw->ATTR[16] = 0x41;
  213. hw->ATTR[17] = 0xFF;
  214. hw->ATTR[18] = 0x0F;
  215. hw->ATTR[19] = 0x00;
  216. hw->ATTR[20] = 0x00;
  217. hd = m->HDisplay >> 3;
  218. hs = m->HSyncStart >> 3;
  219. he = m->HSyncEnd >> 3;
  220. ht = m->HTotal >> 3;
  221. /* standard timmings are in 8pixels, but for interleaved we cannot */
  222. /* do it for 4bpp (because of (4bpp >> 1(interleaved))/4 == 0) */
  223. /* using 16 or more pixels per unit can save us */
  224. divider = ACCESS_FBINFO(curr.final_bppShift);
  225. while (divider & 3) {
  226. hd >>= 1;
  227. hs >>= 1;
  228. he >>= 1;
  229. ht >>= 1;
  230. divider <<= 1;
  231. }
  232. divider = divider / 4;
  233. /* divider can be from 1 to 8 */
  234. while (divider > 8) {
  235. hd <<= 1;
  236. hs <<= 1;
  237. he <<= 1;
  238. ht <<= 1;
  239. divider >>= 1;
  240. }
  241. hd = hd - 1;
  242. hs = hs - 1;
  243. he = he - 1;
  244. ht = ht - 1;
  245. vd = m->VDisplay - 1;
  246. vs = m->VSyncStart - 1;
  247. ve = m->VSyncEnd - 1;
  248. vt = m->VTotal - 2;
  249. lc = vd;
  250. /* G200 cannot work with (ht & 7) == 6 */
  251. if (((ht & 0x07) == 0x06) || ((ht & 0x0F) == 0x04))
  252. ht++;
  253. hbe = ht;
  254. wd = ACCESS_FBINFO(fbcon).var.xres_virtual * ACCESS_FBINFO(curr.final_bppShift) / 64;
  255. hw->CRTCEXT[0] = 0;
  256. hw->CRTCEXT[5] = 0;
  257. if (m->interlaced) {
  258. hw->CRTCEXT[0] = 0x80;
  259. hw->CRTCEXT[5] = (hs + he - ht) >> 1;
  260. if (!m->dblscan)
  261. wd <<= 1;
  262. vt &= ~1;
  263. }
  264. hw->CRTCEXT[0] |= (wd & 0x300) >> 4;
  265. hw->CRTCEXT[1] = (((ht - 4) & 0x100) >> 8) |
  266. ((hd & 0x100) >> 7) | /* blanking */
  267. ((hs & 0x100) >> 6) | /* sync start */
  268. (hbe & 0x040); /* end hor. blanking */
  269. /* FIXME: Enable vidrst only on G400, and only if TV-out is used */
  270. if (ACCESS_FBINFO(outputs[1]).src == MATROXFB_SRC_CRTC1)
  271. hw->CRTCEXT[1] |= 0x88; /* enable horizontal and vertical vidrst */
  272. hw->CRTCEXT[2] = ((vt & 0xC00) >> 10) |
  273. ((vd & 0x400) >> 8) | /* disp end */
  274. ((vd & 0xC00) >> 7) | /* vblanking start */
  275. ((vs & 0xC00) >> 5) |
  276. ((lc & 0x400) >> 3);
  277. hw->CRTCEXT[3] = (divider - 1) | 0x80;
  278. hw->CRTCEXT[4] = 0;
  279. hw->CRTC[0] = ht-4;
  280. hw->CRTC[1] = hd;
  281. hw->CRTC[2] = hd;
  282. hw->CRTC[3] = (hbe & 0x1F) | 0x80;
  283. hw->CRTC[4] = hs;
  284. hw->CRTC[5] = ((hbe & 0x20) << 2) | (he & 0x1F);
  285. hw->CRTC[6] = vt & 0xFF;
  286. hw->CRTC[7] = ((vt & 0x100) >> 8) |
  287. ((vd & 0x100) >> 7) |
  288. ((vs & 0x100) >> 6) |
  289. ((vd & 0x100) >> 5) |
  290. ((lc & 0x100) >> 4) |
  291. ((vt & 0x200) >> 4) |
  292. ((vd & 0x200) >> 3) |
  293. ((vs & 0x200) >> 2);
  294. hw->CRTC[8] = 0x00;
  295. hw->CRTC[9] = ((vd & 0x200) >> 4) |
  296. ((lc & 0x200) >> 3);
  297. if (m->dblscan && !m->interlaced)
  298. hw->CRTC[9] |= 0x80;
  299. for (i = 10; i < 16; i++)
  300. hw->CRTC[i] = 0x00;
  301. hw->CRTC[16] = vs /* & 0xFF */;
  302. hw->CRTC[17] = (ve & 0x0F) | 0x20;
  303. hw->CRTC[18] = vd /* & 0xFF */;
  304. hw->CRTC[19] = wd /* & 0xFF */;
  305. hw->CRTC[20] = 0x00;
  306. hw->CRTC[21] = vd /* & 0xFF */;
  307. hw->CRTC[22] = (vt + 1) /* & 0xFF */;
  308. hw->CRTC[23] = 0xC3;
  309. hw->CRTC[24] = lc;
  310. return 0;
  311. };
  312. void matroxfb_vgaHWrestore(WPMINFO2) {
  313. int i;
  314. struct matrox_hw_state * const hw = &ACCESS_FBINFO(hw);
  315. CRITFLAGS
  316. DBG(__func__)
  317. dprintk(KERN_INFO "MiscOutReg: %02X\n", hw->MiscOutReg);
  318. dprintk(KERN_INFO "SEQ regs: ");
  319. for (i = 0; i < 5; i++)
  320. dprintk("%02X:", hw->SEQ[i]);
  321. dprintk("\n");
  322. dprintk(KERN_INFO "GDC regs: ");
  323. for (i = 0; i < 9; i++)
  324. dprintk("%02X:", hw->GCTL[i]);
  325. dprintk("\n");
  326. dprintk(KERN_INFO "CRTC regs: ");
  327. for (i = 0; i < 25; i++)
  328. dprintk("%02X:", hw->CRTC[i]);
  329. dprintk("\n");
  330. dprintk(KERN_INFO "ATTR regs: ");
  331. for (i = 0; i < 21; i++)
  332. dprintk("%02X:", hw->ATTR[i]);
  333. dprintk("\n");
  334. CRITBEGIN
  335. mga_inb(M_ATTR_RESET);
  336. mga_outb(M_ATTR_INDEX, 0);
  337. mga_outb(M_MISC_REG, hw->MiscOutReg);
  338. for (i = 1; i < 5; i++)
  339. mga_setr(M_SEQ_INDEX, i, hw->SEQ[i]);
  340. mga_setr(M_CRTC_INDEX, 17, hw->CRTC[17] & 0x7F);
  341. for (i = 0; i < 25; i++)
  342. mga_setr(M_CRTC_INDEX, i, hw->CRTC[i]);
  343. for (i = 0; i < 9; i++)
  344. mga_setr(M_GRAPHICS_INDEX, i, hw->GCTL[i]);
  345. for (i = 0; i < 21; i++) {
  346. mga_inb(M_ATTR_RESET);
  347. mga_outb(M_ATTR_INDEX, i);
  348. mga_outb(M_ATTR_INDEX, hw->ATTR[i]);
  349. }
  350. mga_outb(M_PALETTE_MASK, 0xFF);
  351. mga_outb(M_DAC_REG, 0x00);
  352. for (i = 0; i < 768; i++)
  353. mga_outb(M_DAC_VAL, hw->DACpal[i]);
  354. mga_inb(M_ATTR_RESET);
  355. mga_outb(M_ATTR_INDEX, 0x20);
  356. CRITEND
  357. }
  358. static void get_pins(unsigned char __iomem* pins, struct matrox_bios* bd) {
  359. unsigned int b0 = readb(pins);
  360. if (b0 == 0x2E && readb(pins+1) == 0x41) {
  361. unsigned int pins_len = readb(pins+2);
  362. unsigned int i;
  363. unsigned char cksum;
  364. unsigned char* dst = bd->pins;
  365. if (pins_len < 3 || pins_len > 128) {
  366. return;
  367. }
  368. *dst++ = 0x2E;
  369. *dst++ = 0x41;
  370. *dst++ = pins_len;
  371. cksum = 0x2E + 0x41 + pins_len;
  372. for (i = 3; i < pins_len; i++) {
  373. cksum += *dst++ = readb(pins+i);
  374. }
  375. if (cksum) {
  376. return;
  377. }
  378. bd->pins_len = pins_len;
  379. } else if (b0 == 0x40 && readb(pins+1) == 0x00) {
  380. unsigned int i;
  381. unsigned char* dst = bd->pins;
  382. *dst++ = 0x40;
  383. *dst++ = 0;
  384. for (i = 2; i < 0x40; i++) {
  385. *dst++ = readb(pins+i);
  386. }
  387. bd->pins_len = 0x40;
  388. }
  389. }
  390. static void get_bios_version(unsigned char __iomem * vbios, struct matrox_bios* bd) {
  391. unsigned int pcir_offset;
  392. pcir_offset = readb(vbios + 24) | (readb(vbios + 25) << 8);
  393. if (pcir_offset >= 26 && pcir_offset < 0xFFE0 &&
  394. readb(vbios + pcir_offset ) == 'P' &&
  395. readb(vbios + pcir_offset + 1) == 'C' &&
  396. readb(vbios + pcir_offset + 2) == 'I' &&
  397. readb(vbios + pcir_offset + 3) == 'R') {
  398. unsigned char h;
  399. h = readb(vbios + pcir_offset + 0x12);
  400. bd->version.vMaj = (h >> 4) & 0xF;
  401. bd->version.vMin = h & 0xF;
  402. bd->version.vRev = readb(vbios + pcir_offset + 0x13);
  403. } else {
  404. unsigned char h;
  405. h = readb(vbios + 5);
  406. bd->version.vMaj = (h >> 4) & 0xF;
  407. bd->version.vMin = h & 0xF;
  408. bd->version.vRev = 0;
  409. }
  410. }
  411. static void get_bios_output(unsigned char __iomem* vbios, struct matrox_bios* bd) {
  412. unsigned char b;
  413. b = readb(vbios + 0x7FF1);
  414. if (b == 0xFF) {
  415. b = 0;
  416. }
  417. bd->output.state = b;
  418. }
  419. static void get_bios_tvout(unsigned char __iomem* vbios, struct matrox_bios* bd) {
  420. unsigned int i;
  421. /* Check for 'IBM .*(V....TVO' string - it means TVO BIOS */
  422. bd->output.tvout = 0;
  423. if (readb(vbios + 0x1D) != 'I' ||
  424. readb(vbios + 0x1E) != 'B' ||
  425. readb(vbios + 0x1F) != 'M' ||
  426. readb(vbios + 0x20) != ' ') {
  427. return;
  428. }
  429. for (i = 0x2D; i < 0x2D + 128; i++) {
  430. unsigned char b = readb(vbios + i);
  431. if (b == '(' && readb(vbios + i + 1) == 'V') {
  432. if (readb(vbios + i + 6) == 'T' &&
  433. readb(vbios + i + 7) == 'V' &&
  434. readb(vbios + i + 8) == 'O') {
  435. bd->output.tvout = 1;
  436. }
  437. return;
  438. }
  439. if (b == 0)
  440. break;
  441. }
  442. }
  443. static void parse_bios(unsigned char __iomem* vbios, struct matrox_bios* bd) {
  444. unsigned int pins_offset;
  445. if (readb(vbios) != 0x55 || readb(vbios + 1) != 0xAA) {
  446. return;
  447. }
  448. bd->bios_valid = 1;
  449. get_bios_version(vbios, bd);
  450. get_bios_output(vbios, bd);
  451. get_bios_tvout(vbios, bd);
  452. #if defined(__powerpc__)
  453. /* On PowerPC cards, the PInS offset isn't stored at the end of the
  454. * BIOS image. Instead, you must search the entire BIOS image for
  455. * the magic PInS signature.
  456. *
  457. * This actually applies to all OpenFirmware base cards. Since these
  458. * cards could be put in a MIPS or SPARC system, should the condition
  459. * be something different?
  460. */
  461. for ( pins_offset = 0 ; pins_offset <= 0xFF80 ; pins_offset++ ) {
  462. unsigned char header[3];
  463. header[0] = readb(vbios + pins_offset);
  464. header[1] = readb(vbios + pins_offset + 1);
  465. header[2] = readb(vbios + pins_offset + 2);
  466. if ( (header[0] == 0x2E) && (header[1] == 0x41)
  467. && ((header[2] == 0x40) || (header[2] == 0x80)) ) {
  468. printk(KERN_INFO "PInS data found at offset %u\n",
  469. pins_offset);
  470. get_pins(vbios + pins_offset, bd);
  471. break;
  472. }
  473. }
  474. #else
  475. pins_offset = readb(vbios + 0x7FFC) | (readb(vbios + 0x7FFD) << 8);
  476. if (pins_offset <= 0xFF80) {
  477. get_pins(vbios + pins_offset, bd);
  478. }
  479. #endif
  480. }
  481. static int parse_pins1(WPMINFO const struct matrox_bios* bd) {
  482. unsigned int maxdac;
  483. switch (bd->pins[22]) {
  484. case 0: maxdac = 175000; break;
  485. case 1: maxdac = 220000; break;
  486. default: maxdac = 240000; break;
  487. }
  488. if (get_unaligned_le16(bd->pins + 24)) {
  489. maxdac = get_unaligned_le16(bd->pins + 24) * 10;
  490. }
  491. MINFO->limits.pixel.vcomax = maxdac;
  492. MINFO->values.pll.system = get_unaligned_le16(bd->pins + 28) ?
  493. get_unaligned_le16(bd->pins + 28) * 10 : 50000;
  494. /* ignore 4MB, 8MB, module clocks */
  495. MINFO->features.pll.ref_freq = 14318;
  496. MINFO->values.reg.mctlwtst = 0x00030101;
  497. return 0;
  498. }
  499. static void default_pins1(WPMINFO2) {
  500. /* Millennium */
  501. MINFO->limits.pixel.vcomax = 220000;
  502. MINFO->values.pll.system = 50000;
  503. MINFO->features.pll.ref_freq = 14318;
  504. MINFO->values.reg.mctlwtst = 0x00030101;
  505. }
  506. static int parse_pins2(WPMINFO const struct matrox_bios* bd) {
  507. MINFO->limits.pixel.vcomax =
  508. MINFO->limits.system.vcomax = (bd->pins[41] == 0xFF) ? 230000 : ((bd->pins[41] + 100) * 1000);
  509. MINFO->values.reg.mctlwtst = ((bd->pins[51] & 0x01) ? 0x00000001 : 0) |
  510. ((bd->pins[51] & 0x02) ? 0x00000100 : 0) |
  511. ((bd->pins[51] & 0x04) ? 0x00010000 : 0) |
  512. ((bd->pins[51] & 0x08) ? 0x00020000 : 0);
  513. MINFO->values.pll.system = (bd->pins[43] == 0xFF) ? 50000 : ((bd->pins[43] + 100) * 1000);
  514. MINFO->features.pll.ref_freq = 14318;
  515. return 0;
  516. }
  517. static void default_pins2(WPMINFO2) {
  518. /* Millennium II, Mystique */
  519. MINFO->limits.pixel.vcomax =
  520. MINFO->limits.system.vcomax = 230000;
  521. MINFO->values.reg.mctlwtst = 0x00030101;
  522. MINFO->values.pll.system = 50000;
  523. MINFO->features.pll.ref_freq = 14318;
  524. }
  525. static int parse_pins3(WPMINFO const struct matrox_bios* bd) {
  526. MINFO->limits.pixel.vcomax =
  527. MINFO->limits.system.vcomax = (bd->pins[36] == 0xFF) ? 230000 : ((bd->pins[36] + 100) * 1000);
  528. MINFO->values.reg.mctlwtst = get_unaligned_le32(bd->pins + 48) == 0xFFFFFFFF ?
  529. 0x01250A21 : get_unaligned_le32(bd->pins + 48);
  530. /* memory config */
  531. MINFO->values.reg.memrdbk = ((bd->pins[57] << 21) & 0x1E000000) |
  532. ((bd->pins[57] << 22) & 0x00C00000) |
  533. ((bd->pins[56] << 1) & 0x000001E0) |
  534. ( bd->pins[56] & 0x0000000F);
  535. MINFO->values.reg.opt = (bd->pins[54] & 7) << 10;
  536. MINFO->values.reg.opt2 = bd->pins[58] << 12;
  537. MINFO->features.pll.ref_freq = (bd->pins[52] & 0x20) ? 14318 : 27000;
  538. return 0;
  539. }
  540. static void default_pins3(WPMINFO2) {
  541. /* G100, G200 */
  542. MINFO->limits.pixel.vcomax =
  543. MINFO->limits.system.vcomax = 230000;
  544. MINFO->values.reg.mctlwtst = 0x01250A21;
  545. MINFO->values.reg.memrdbk = 0x00000000;
  546. MINFO->values.reg.opt = 0x00000C00;
  547. MINFO->values.reg.opt2 = 0x00000000;
  548. MINFO->features.pll.ref_freq = 27000;
  549. }
  550. static int parse_pins4(WPMINFO const struct matrox_bios* bd) {
  551. MINFO->limits.pixel.vcomax = (bd->pins[ 39] == 0xFF) ? 230000 : bd->pins[ 39] * 4000;
  552. MINFO->limits.system.vcomax = (bd->pins[ 38] == 0xFF) ? MINFO->limits.pixel.vcomax : bd->pins[ 38] * 4000;
  553. MINFO->values.reg.mctlwtst = get_unaligned_le32(bd->pins + 71);
  554. MINFO->values.reg.memrdbk = ((bd->pins[87] << 21) & 0x1E000000) |
  555. ((bd->pins[87] << 22) & 0x00C00000) |
  556. ((bd->pins[86] << 1) & 0x000001E0) |
  557. ( bd->pins[86] & 0x0000000F);
  558. MINFO->values.reg.opt = ((bd->pins[53] << 15) & 0x00400000) |
  559. ((bd->pins[53] << 22) & 0x10000000) |
  560. ((bd->pins[53] << 7) & 0x00001C00);
  561. MINFO->values.reg.opt3 = get_unaligned_le32(bd->pins + 67);
  562. MINFO->values.pll.system = (bd->pins[ 65] == 0xFF) ? 200000 : bd->pins[ 65] * 4000;
  563. MINFO->features.pll.ref_freq = (bd->pins[ 92] & 0x01) ? 14318 : 27000;
  564. return 0;
  565. }
  566. static void default_pins4(WPMINFO2) {
  567. /* G400 */
  568. MINFO->limits.pixel.vcomax =
  569. MINFO->limits.system.vcomax = 252000;
  570. MINFO->values.reg.mctlwtst = 0x04A450A1;
  571. MINFO->values.reg.memrdbk = 0x000000E7;
  572. MINFO->values.reg.opt = 0x10000400;
  573. MINFO->values.reg.opt3 = 0x0190A419;
  574. MINFO->values.pll.system = 200000;
  575. MINFO->features.pll.ref_freq = 27000;
  576. }
  577. static int parse_pins5(WPMINFO const struct matrox_bios* bd) {
  578. unsigned int mult;
  579. mult = bd->pins[4]?8000:6000;
  580. MINFO->limits.pixel.vcomax = (bd->pins[ 38] == 0xFF) ? 600000 : bd->pins[ 38] * mult;
  581. MINFO->limits.system.vcomax = (bd->pins[ 36] == 0xFF) ? MINFO->limits.pixel.vcomax : bd->pins[ 36] * mult;
  582. MINFO->limits.video.vcomax = (bd->pins[ 37] == 0xFF) ? MINFO->limits.system.vcomax : bd->pins[ 37] * mult;
  583. MINFO->limits.pixel.vcomin = (bd->pins[123] == 0xFF) ? 256000 : bd->pins[123] * mult;
  584. MINFO->limits.system.vcomin = (bd->pins[121] == 0xFF) ? MINFO->limits.pixel.vcomin : bd->pins[121] * mult;
  585. MINFO->limits.video.vcomin = (bd->pins[122] == 0xFF) ? MINFO->limits.system.vcomin : bd->pins[122] * mult;
  586. MINFO->values.pll.system =
  587. MINFO->values.pll.video = (bd->pins[ 92] == 0xFF) ? 284000 : bd->pins[ 92] * 4000;
  588. MINFO->values.reg.opt = get_unaligned_le32(bd->pins + 48);
  589. MINFO->values.reg.opt2 = get_unaligned_le32(bd->pins + 52);
  590. MINFO->values.reg.opt3 = get_unaligned_le32(bd->pins + 94);
  591. MINFO->values.reg.mctlwtst = get_unaligned_le32(bd->pins + 98);
  592. MINFO->values.reg.memmisc = get_unaligned_le32(bd->pins + 102);
  593. MINFO->values.reg.memrdbk = get_unaligned_le32(bd->pins + 106);
  594. MINFO->features.pll.ref_freq = (bd->pins[110] & 0x01) ? 14318 : 27000;
  595. MINFO->values.memory.ddr = (bd->pins[114] & 0x60) == 0x20;
  596. MINFO->values.memory.dll = (bd->pins[115] & 0x02) != 0;
  597. MINFO->values.memory.emrswen = (bd->pins[115] & 0x01) != 0;
  598. MINFO->values.reg.maccess = MINFO->values.memory.emrswen ? 0x00004000 : 0x00000000;
  599. if (bd->pins[115] & 4) {
  600. MINFO->values.reg.mctlwtst_core = MINFO->values.reg.mctlwtst;
  601. } else {
  602. u_int32_t wtst_xlat[] = { 0, 1, 5, 6, 7, 5, 2, 3 };
  603. MINFO->values.reg.mctlwtst_core = (MINFO->values.reg.mctlwtst & ~7) |
  604. wtst_xlat[MINFO->values.reg.mctlwtst & 7];
  605. }
  606. MINFO->max_pixel_clock_panellink = bd->pins[47] * 4000;
  607. return 0;
  608. }
  609. static void default_pins5(WPMINFO2) {
  610. /* Mine 16MB G450 with SDRAM DDR */
  611. MINFO->limits.pixel.vcomax =
  612. MINFO->limits.system.vcomax =
  613. MINFO->limits.video.vcomax = 600000;
  614. MINFO->limits.pixel.vcomin =
  615. MINFO->limits.system.vcomin =
  616. MINFO->limits.video.vcomin = 256000;
  617. MINFO->values.pll.system =
  618. MINFO->values.pll.video = 284000;
  619. MINFO->values.reg.opt = 0x404A1160;
  620. MINFO->values.reg.opt2 = 0x0000AC00;
  621. MINFO->values.reg.opt3 = 0x0090A409;
  622. MINFO->values.reg.mctlwtst_core =
  623. MINFO->values.reg.mctlwtst = 0x0C81462B;
  624. MINFO->values.reg.memmisc = 0x80000004;
  625. MINFO->values.reg.memrdbk = 0x01001103;
  626. MINFO->features.pll.ref_freq = 27000;
  627. MINFO->values.memory.ddr = 1;
  628. MINFO->values.memory.dll = 1;
  629. MINFO->values.memory.emrswen = 1;
  630. MINFO->values.reg.maccess = 0x00004000;
  631. }
  632. static int matroxfb_set_limits(WPMINFO const struct matrox_bios* bd) {
  633. unsigned int pins_version;
  634. static const unsigned int pinslen[] = { 64, 64, 64, 128, 128 };
  635. switch (ACCESS_FBINFO(chip)) {
  636. case MGA_2064: default_pins1(PMINFO2); break;
  637. case MGA_2164:
  638. case MGA_1064:
  639. case MGA_1164: default_pins2(PMINFO2); break;
  640. case MGA_G100:
  641. case MGA_G200: default_pins3(PMINFO2); break;
  642. case MGA_G400: default_pins4(PMINFO2); break;
  643. case MGA_G450:
  644. case MGA_G550: default_pins5(PMINFO2); break;
  645. }
  646. if (!bd->bios_valid) {
  647. printk(KERN_INFO "matroxfb: Your Matrox device does not have BIOS\n");
  648. return -1;
  649. }
  650. if (bd->pins_len < 64) {
  651. printk(KERN_INFO "matroxfb: BIOS on your Matrox device does not contain powerup info\n");
  652. return -1;
  653. }
  654. if (bd->pins[0] == 0x2E && bd->pins[1] == 0x41) {
  655. pins_version = bd->pins[5];
  656. if (pins_version < 2 || pins_version > 5) {
  657. printk(KERN_INFO "matroxfb: Unknown version (%u) of powerup info\n", pins_version);
  658. return -1;
  659. }
  660. } else {
  661. pins_version = 1;
  662. }
  663. if (bd->pins_len != pinslen[pins_version - 1]) {
  664. printk(KERN_INFO "matroxfb: Invalid powerup info\n");
  665. return -1;
  666. }
  667. switch (pins_version) {
  668. case 1:
  669. return parse_pins1(PMINFO bd);
  670. case 2:
  671. return parse_pins2(PMINFO bd);
  672. case 3:
  673. return parse_pins3(PMINFO bd);
  674. case 4:
  675. return parse_pins4(PMINFO bd);
  676. case 5:
  677. return parse_pins5(PMINFO bd);
  678. default:
  679. printk(KERN_DEBUG "matroxfb: Powerup info version %u is not yet supported\n", pins_version);
  680. return -1;
  681. }
  682. }
  683. void matroxfb_read_pins(WPMINFO2) {
  684. u32 opt;
  685. u32 biosbase;
  686. u32 fbbase;
  687. struct pci_dev* pdev = ACCESS_FBINFO(pcidev);
  688. memset(&ACCESS_FBINFO(bios), 0, sizeof(ACCESS_FBINFO(bios)));
  689. pci_read_config_dword(pdev, PCI_OPTION_REG, &opt);
  690. pci_write_config_dword(pdev, PCI_OPTION_REG, opt | PCI_OPTION_ENABLE_ROM);
  691. pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &biosbase);
  692. pci_read_config_dword(pdev, ACCESS_FBINFO(devflags.fbResource), &fbbase);
  693. pci_write_config_dword(pdev, PCI_ROM_ADDRESS, (fbbase & PCI_ROM_ADDRESS_MASK) | PCI_ROM_ADDRESS_ENABLE);
  694. parse_bios(vaddr_va(ACCESS_FBINFO(video).vbase), &ACCESS_FBINFO(bios));
  695. pci_write_config_dword(pdev, PCI_ROM_ADDRESS, biosbase);
  696. pci_write_config_dword(pdev, PCI_OPTION_REG, opt);
  697. #ifdef CONFIG_X86
  698. if (!ACCESS_FBINFO(bios).bios_valid) {
  699. unsigned char __iomem* b;
  700. b = ioremap(0x000C0000, 65536);
  701. if (!b) {
  702. printk(KERN_INFO "matroxfb: Unable to map legacy BIOS\n");
  703. } else {
  704. unsigned int ven = readb(b+0x64+0) | (readb(b+0x64+1) << 8);
  705. unsigned int dev = readb(b+0x64+2) | (readb(b+0x64+3) << 8);
  706. if (ven != pdev->vendor || dev != pdev->device) {
  707. printk(KERN_INFO "matroxfb: Legacy BIOS is for %04X:%04X, while this device is %04X:%04X\n",
  708. ven, dev, pdev->vendor, pdev->device);
  709. } else {
  710. parse_bios(b, &ACCESS_FBINFO(bios));
  711. }
  712. iounmap(b);
  713. }
  714. }
  715. #endif
  716. matroxfb_set_limits(PMINFO &ACCESS_FBINFO(bios));
  717. printk(KERN_INFO "PInS memtype = %u\n",
  718. (ACCESS_FBINFO(values).reg.opt & 0x1C00) >> 10);
  719. }
  720. EXPORT_SYMBOL(matroxfb_DAC_in);
  721. EXPORT_SYMBOL(matroxfb_DAC_out);
  722. EXPORT_SYMBOL(matroxfb_var2my);
  723. EXPORT_SYMBOL(matroxfb_PLL_calcclock);
  724. #ifndef CONFIG_FB_MATROX_MULTIHEAD
  725. struct matrox_fb_info matroxfb_global_mxinfo;
  726. EXPORT_SYMBOL(matroxfb_global_mxinfo);
  727. #endif
  728. EXPORT_SYMBOL(matroxfb_vgaHWinit); /* DAC1064, Ti3026 */
  729. EXPORT_SYMBOL(matroxfb_vgaHWrestore); /* DAC1064, Ti3026 */
  730. EXPORT_SYMBOL(matroxfb_read_pins);
  731. MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
  732. MODULE_DESCRIPTION("Miscellaneous support for Matrox video cards");
  733. MODULE_LICENSE("GPL");