matroxfb_misc.c 24 KB

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