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