18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200 and G400
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Portions Copyright (c) 2001 Matrox Graphics Inc.
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Version: 1.65 2002/08/14
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * MTRR stuff: 1998 Tom Rini <trini@kernel.crashing.org>
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * Contributors: "menion?" <menion@mindless.com>
158c2ecf20Sopenharmony_ci *                     Betatesting, fixes, ideas
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci *               "Kurt Garloff" <garloff@suse.de>
188c2ecf20Sopenharmony_ci *                     Betatesting, fixes, ideas, videomodes, videomodes timmings
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *               "Tom Rini" <trini@kernel.crashing.org>
218c2ecf20Sopenharmony_ci *                     MTRR stuff, PPC cleanups, betatesting, fixes, ideas
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci *               "Bibek Sahu" <scorpio@dodds.net>
248c2ecf20Sopenharmony_ci *                     Access device through readb|w|l and write b|w|l
258c2ecf20Sopenharmony_ci *                     Extensive debugging stuff
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci *               "Daniel Haun" <haund@usa.net>
288c2ecf20Sopenharmony_ci *                     Testing, hardware cursor fixes
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci *               "Scott Wood" <sawst46+@pitt.edu>
318c2ecf20Sopenharmony_ci *                     Fixes
328c2ecf20Sopenharmony_ci *
338c2ecf20Sopenharmony_ci *               "Gerd Knorr" <kraxel@goldbach.isdn.cs.tu-berlin.de>
348c2ecf20Sopenharmony_ci *                     Betatesting
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci *               "Kelly French" <targon@hazmat.com>
378c2ecf20Sopenharmony_ci *               "Fernando Herrera" <fherrera@eurielec.etsit.upm.es>
388c2ecf20Sopenharmony_ci *                     Betatesting, bug reporting
398c2ecf20Sopenharmony_ci *
408c2ecf20Sopenharmony_ci *               "Pablo Bianucci" <pbian@pccp.com.ar>
418c2ecf20Sopenharmony_ci *                     Fixes, ideas, betatesting
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci *               "Inaky Perez Gonzalez" <inaky@peloncho.fis.ucm.es>
448c2ecf20Sopenharmony_ci *                     Fixes, enhandcements, ideas, betatesting
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci *               "Ryuichi Oikawa" <roikawa@rr.iiij4u.or.jp>
478c2ecf20Sopenharmony_ci *                     PPC betatesting, PPC support, backward compatibility
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci *               "Paul Womar" <Paul@pwomar.demon.co.uk>
508c2ecf20Sopenharmony_ci *               "Owen Waller" <O.Waller@ee.qub.ac.uk>
518c2ecf20Sopenharmony_ci *                     PPC betatesting
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci *               "Thomas Pornin" <pornin@bolet.ens.fr>
548c2ecf20Sopenharmony_ci *                     Alpha betatesting
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci *               "Pieter van Leuven" <pvl@iae.nl>
578c2ecf20Sopenharmony_ci *               "Ulf Jaenicke-Roessler" <ujr@physik.phy.tu-dresden.de>
588c2ecf20Sopenharmony_ci *                     G100 testing
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci *               "H. Peter Arvin" <hpa@transmeta.com>
618c2ecf20Sopenharmony_ci *                     Ideas
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci *               "Cort Dougan" <cort@cs.nmt.edu>
648c2ecf20Sopenharmony_ci *                     CHRP fixes and PReP cleanup
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci *               "Mark Vojkovich" <mvojkovi@ucsd.edu>
678c2ecf20Sopenharmony_ci *                     G400 support
688c2ecf20Sopenharmony_ci *
698c2ecf20Sopenharmony_ci *               "David C. Hansen" <haveblue@us.ibm.com>
708c2ecf20Sopenharmony_ci *                     Fixes
718c2ecf20Sopenharmony_ci *
728c2ecf20Sopenharmony_ci *               "Ian Romanick" <idr@us.ibm.com>
738c2ecf20Sopenharmony_ci *                     Find PInS data in BIOS on PowerPC systems.
748c2ecf20Sopenharmony_ci *
758c2ecf20Sopenharmony_ci * (following author is not in any relation with this code, but his code
768c2ecf20Sopenharmony_ci *  is included in this driver)
778c2ecf20Sopenharmony_ci *
788c2ecf20Sopenharmony_ci * Based on framebuffer driver for VBE 2.0 compliant graphic boards
798c2ecf20Sopenharmony_ci *     (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
808c2ecf20Sopenharmony_ci *
818c2ecf20Sopenharmony_ci * (following author is not in any relation with this code, but his ideas
828c2ecf20Sopenharmony_ci *  were used when writing this driver)
838c2ecf20Sopenharmony_ci *
848c2ecf20Sopenharmony_ci *		 FreeVBE/AF (Matrox), "Shawn Hargreaves" <shawn@talula.demon.co.uk>
858c2ecf20Sopenharmony_ci *
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#include "matroxfb_misc.h"
908c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
918c2ecf20Sopenharmony_ci#include <linux/matroxfb.h>
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_civoid matroxfb_DAC_out(const struct matrox_fb_info *minfo, int reg, int val)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	DBG_REG(__func__)
968c2ecf20Sopenharmony_ci	mga_outb(M_RAMDAC_BASE+M_X_INDEX, reg);
978c2ecf20Sopenharmony_ci	mga_outb(M_RAMDAC_BASE+M_X_DATAREG, val);
988c2ecf20Sopenharmony_ci}
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ciint matroxfb_DAC_in(const struct matrox_fb_info *minfo, int reg)
1018c2ecf20Sopenharmony_ci{
1028c2ecf20Sopenharmony_ci	DBG_REG(__func__)
1038c2ecf20Sopenharmony_ci	mga_outb(M_RAMDAC_BASE+M_X_INDEX, reg);
1048c2ecf20Sopenharmony_ci	return mga_inb(M_RAMDAC_BASE+M_X_DATAREG);
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_civoid matroxfb_var2my(struct fb_var_screeninfo* var, struct my_timming* mt) {
1088c2ecf20Sopenharmony_ci	unsigned int pixclock = var->pixclock;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	DBG(__func__)
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (!pixclock) pixclock = 10000;	/* 10ns = 100MHz */
1138c2ecf20Sopenharmony_ci	mt->pixclock = 1000000000 / pixclock;
1148c2ecf20Sopenharmony_ci	if (mt->pixclock < 1) mt->pixclock = 1;
1158c2ecf20Sopenharmony_ci	mt->mnp = -1;
1168c2ecf20Sopenharmony_ci	mt->dblscan = var->vmode & FB_VMODE_DOUBLE;
1178c2ecf20Sopenharmony_ci	mt->interlaced = var->vmode & FB_VMODE_INTERLACED;
1188c2ecf20Sopenharmony_ci	mt->HDisplay = var->xres;
1198c2ecf20Sopenharmony_ci	mt->HSyncStart = mt->HDisplay + var->right_margin;
1208c2ecf20Sopenharmony_ci	mt->HSyncEnd = mt->HSyncStart + var->hsync_len;
1218c2ecf20Sopenharmony_ci	mt->HTotal = mt->HSyncEnd + var->left_margin;
1228c2ecf20Sopenharmony_ci	mt->VDisplay = var->yres;
1238c2ecf20Sopenharmony_ci	mt->VSyncStart = mt->VDisplay + var->lower_margin;
1248c2ecf20Sopenharmony_ci	mt->VSyncEnd = mt->VSyncStart + var->vsync_len;
1258c2ecf20Sopenharmony_ci	mt->VTotal = mt->VSyncEnd + var->upper_margin;
1268c2ecf20Sopenharmony_ci	mt->sync = var->sync;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciint matroxfb_PLL_calcclock(const struct matrox_pll_features* pll, unsigned int freq, unsigned int fmax,
1308c2ecf20Sopenharmony_ci		unsigned int* in, unsigned int* feed, unsigned int* post) {
1318c2ecf20Sopenharmony_ci	unsigned int bestdiff = ~0;
1328c2ecf20Sopenharmony_ci	unsigned int bestvco = 0;
1338c2ecf20Sopenharmony_ci	unsigned int fxtal = pll->ref_freq;
1348c2ecf20Sopenharmony_ci	unsigned int fwant;
1358c2ecf20Sopenharmony_ci	unsigned int p;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	DBG(__func__)
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	fwant = freq;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci#ifdef DEBUG
1428c2ecf20Sopenharmony_ci	printk(KERN_ERR "post_shift_max: %d\n", pll->post_shift_max);
1438c2ecf20Sopenharmony_ci	printk(KERN_ERR "ref_freq: %d\n", pll->ref_freq);
1448c2ecf20Sopenharmony_ci	printk(KERN_ERR "freq: %d\n", freq);
1458c2ecf20Sopenharmony_ci	printk(KERN_ERR "vco_freq_min: %d\n", pll->vco_freq_min);
1468c2ecf20Sopenharmony_ci	printk(KERN_ERR "in_div_min: %d\n", pll->in_div_min);
1478c2ecf20Sopenharmony_ci	printk(KERN_ERR "in_div_max: %d\n", pll->in_div_max);
1488c2ecf20Sopenharmony_ci	printk(KERN_ERR "feed_div_min: %d\n", pll->feed_div_min);
1498c2ecf20Sopenharmony_ci	printk(KERN_ERR "feed_div_max: %d\n", pll->feed_div_max);
1508c2ecf20Sopenharmony_ci	printk(KERN_ERR "fmax: %d\n", fmax);
1518c2ecf20Sopenharmony_ci#endif
1528c2ecf20Sopenharmony_ci	for (p = 1; p <= pll->post_shift_max; p++) {
1538c2ecf20Sopenharmony_ci		if (fwant * 2 > fmax)
1548c2ecf20Sopenharmony_ci			break;
1558c2ecf20Sopenharmony_ci		fwant *= 2;
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci	if (fwant < pll->vco_freq_min) fwant = pll->vco_freq_min;
1588c2ecf20Sopenharmony_ci	if (fwant > fmax) fwant = fmax;
1598c2ecf20Sopenharmony_ci	for (; p-- > 0; fwant >>= 1, bestdiff >>= 1) {
1608c2ecf20Sopenharmony_ci		unsigned int m;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		if (fwant < pll->vco_freq_min) break;
1638c2ecf20Sopenharmony_ci		for (m = pll->in_div_min; m <= pll->in_div_max; m++) {
1648c2ecf20Sopenharmony_ci			unsigned int diff, fvco;
1658c2ecf20Sopenharmony_ci			unsigned int n;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci			n = (fwant * (m + 1) + (fxtal >> 1)) / fxtal - 1;
1688c2ecf20Sopenharmony_ci			if (n > pll->feed_div_max)
1698c2ecf20Sopenharmony_ci				break;
1708c2ecf20Sopenharmony_ci			if (n < pll->feed_div_min)
1718c2ecf20Sopenharmony_ci				n = pll->feed_div_min;
1728c2ecf20Sopenharmony_ci			fvco = (fxtal * (n + 1)) / (m + 1);
1738c2ecf20Sopenharmony_ci			if (fvco < fwant)
1748c2ecf20Sopenharmony_ci				diff = fwant - fvco;
1758c2ecf20Sopenharmony_ci			else
1768c2ecf20Sopenharmony_ci				diff = fvco - fwant;
1778c2ecf20Sopenharmony_ci			if (diff < bestdiff) {
1788c2ecf20Sopenharmony_ci				bestdiff = diff;
1798c2ecf20Sopenharmony_ci				*post = p;
1808c2ecf20Sopenharmony_ci				*in = m;
1818c2ecf20Sopenharmony_ci				*feed = n;
1828c2ecf20Sopenharmony_ci				bestvco = fvco;
1838c2ecf20Sopenharmony_ci			}
1848c2ecf20Sopenharmony_ci		}
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci	dprintk(KERN_ERR "clk: %02X %02X %02X %d %d %d\n", *in, *feed, *post, fxtal, bestvco, fwant);
1878c2ecf20Sopenharmony_ci	return bestvco;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ciint matroxfb_vgaHWinit(struct matrox_fb_info *minfo, struct my_timming *m)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	unsigned int hd, hs, he, hbe, ht;
1938c2ecf20Sopenharmony_ci	unsigned int vd, vs, ve, vt, lc;
1948c2ecf20Sopenharmony_ci	unsigned int wd;
1958c2ecf20Sopenharmony_ci	unsigned int divider;
1968c2ecf20Sopenharmony_ci	int i;
1978c2ecf20Sopenharmony_ci	struct matrox_hw_state * const hw = &minfo->hw;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	DBG(__func__)
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	hw->SEQ[0] = 0x00;
2028c2ecf20Sopenharmony_ci	hw->SEQ[1] = 0x01;	/* or 0x09 */
2038c2ecf20Sopenharmony_ci	hw->SEQ[2] = 0x0F;	/* bitplanes */
2048c2ecf20Sopenharmony_ci	hw->SEQ[3] = 0x00;
2058c2ecf20Sopenharmony_ci	hw->SEQ[4] = 0x0E;
2068c2ecf20Sopenharmony_ci	/* CRTC 0..7, 9, 16..19, 21, 22 are reprogrammed by Matrox Millennium code... Hope that by MGA1064 too */
2078c2ecf20Sopenharmony_ci	if (m->dblscan) {
2088c2ecf20Sopenharmony_ci		m->VTotal <<= 1;
2098c2ecf20Sopenharmony_ci		m->VDisplay <<= 1;
2108c2ecf20Sopenharmony_ci		m->VSyncStart <<= 1;
2118c2ecf20Sopenharmony_ci		m->VSyncEnd <<= 1;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci	if (m->interlaced) {
2148c2ecf20Sopenharmony_ci		m->VTotal >>= 1;
2158c2ecf20Sopenharmony_ci		m->VDisplay >>= 1;
2168c2ecf20Sopenharmony_ci		m->VSyncStart >>= 1;
2178c2ecf20Sopenharmony_ci		m->VSyncEnd >>= 1;
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/* GCTL is ignored when not using 0xA0000 aperture */
2218c2ecf20Sopenharmony_ci	hw->GCTL[0] = 0x00;
2228c2ecf20Sopenharmony_ci	hw->GCTL[1] = 0x00;
2238c2ecf20Sopenharmony_ci	hw->GCTL[2] = 0x00;
2248c2ecf20Sopenharmony_ci	hw->GCTL[3] = 0x00;
2258c2ecf20Sopenharmony_ci	hw->GCTL[4] = 0x00;
2268c2ecf20Sopenharmony_ci	hw->GCTL[5] = 0x40;
2278c2ecf20Sopenharmony_ci	hw->GCTL[6] = 0x05;
2288c2ecf20Sopenharmony_ci	hw->GCTL[7] = 0x0F;
2298c2ecf20Sopenharmony_ci	hw->GCTL[8] = 0xFF;
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/* Whole ATTR is ignored in PowerGraphics mode */
2328c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++)
2338c2ecf20Sopenharmony_ci		hw->ATTR[i] = i;
2348c2ecf20Sopenharmony_ci	hw->ATTR[16] = 0x41;
2358c2ecf20Sopenharmony_ci	hw->ATTR[17] = 0xFF;
2368c2ecf20Sopenharmony_ci	hw->ATTR[18] = 0x0F;
2378c2ecf20Sopenharmony_ci	hw->ATTR[19] = 0x00;
2388c2ecf20Sopenharmony_ci	hw->ATTR[20] = 0x00;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	hd = m->HDisplay >> 3;
2418c2ecf20Sopenharmony_ci	hs = m->HSyncStart >> 3;
2428c2ecf20Sopenharmony_ci	he = m->HSyncEnd >> 3;
2438c2ecf20Sopenharmony_ci	ht = m->HTotal >> 3;
2448c2ecf20Sopenharmony_ci	/* standard timmings are in 8pixels, but for interleaved we cannot */
2458c2ecf20Sopenharmony_ci	/* do it for 4bpp (because of (4bpp >> 1(interleaved))/4 == 0) */
2468c2ecf20Sopenharmony_ci	/* using 16 or more pixels per unit can save us */
2478c2ecf20Sopenharmony_ci	divider = minfo->curr.final_bppShift;
2488c2ecf20Sopenharmony_ci	while (divider & 3) {
2498c2ecf20Sopenharmony_ci		hd >>= 1;
2508c2ecf20Sopenharmony_ci		hs >>= 1;
2518c2ecf20Sopenharmony_ci		he >>= 1;
2528c2ecf20Sopenharmony_ci		ht >>= 1;
2538c2ecf20Sopenharmony_ci		divider <<= 1;
2548c2ecf20Sopenharmony_ci	}
2558c2ecf20Sopenharmony_ci	divider = divider / 4;
2568c2ecf20Sopenharmony_ci	/* divider can be from 1 to 8 */
2578c2ecf20Sopenharmony_ci	while (divider > 8) {
2588c2ecf20Sopenharmony_ci		hd <<= 1;
2598c2ecf20Sopenharmony_ci		hs <<= 1;
2608c2ecf20Sopenharmony_ci		he <<= 1;
2618c2ecf20Sopenharmony_ci		ht <<= 1;
2628c2ecf20Sopenharmony_ci		divider >>= 1;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci	hd = hd - 1;
2658c2ecf20Sopenharmony_ci	hs = hs - 1;
2668c2ecf20Sopenharmony_ci	he = he - 1;
2678c2ecf20Sopenharmony_ci	ht = ht - 1;
2688c2ecf20Sopenharmony_ci	vd = m->VDisplay - 1;
2698c2ecf20Sopenharmony_ci	vs = m->VSyncStart - 1;
2708c2ecf20Sopenharmony_ci	ve = m->VSyncEnd - 1;
2718c2ecf20Sopenharmony_ci	vt = m->VTotal - 2;
2728c2ecf20Sopenharmony_ci	lc = vd;
2738c2ecf20Sopenharmony_ci	/* G200 cannot work with (ht & 7) == 6 */
2748c2ecf20Sopenharmony_ci	if (((ht & 0x07) == 0x06) || ((ht & 0x0F) == 0x04))
2758c2ecf20Sopenharmony_ci		ht++;
2768c2ecf20Sopenharmony_ci	hbe = ht;
2778c2ecf20Sopenharmony_ci	wd = minfo->fbcon.var.xres_virtual * minfo->curr.final_bppShift / 64;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	hw->CRTCEXT[0] = 0;
2808c2ecf20Sopenharmony_ci	hw->CRTCEXT[5] = 0;
2818c2ecf20Sopenharmony_ci	if (m->interlaced) {
2828c2ecf20Sopenharmony_ci		hw->CRTCEXT[0] = 0x80;
2838c2ecf20Sopenharmony_ci		hw->CRTCEXT[5] = (hs + he - ht) >> 1;
2848c2ecf20Sopenharmony_ci		if (!m->dblscan)
2858c2ecf20Sopenharmony_ci			wd <<= 1;
2868c2ecf20Sopenharmony_ci		vt &= ~1;
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci	hw->CRTCEXT[0] |=  (wd & 0x300) >> 4;
2898c2ecf20Sopenharmony_ci	hw->CRTCEXT[1] = (((ht - 4) & 0x100) >> 8) |
2908c2ecf20Sopenharmony_ci			  ((hd      & 0x100) >> 7) | /* blanking */
2918c2ecf20Sopenharmony_ci			  ((hs      & 0x100) >> 6) | /* sync start */
2928c2ecf20Sopenharmony_ci			   (hbe     & 0x040);	 /* end hor. blanking */
2938c2ecf20Sopenharmony_ci	/* FIXME: Enable vidrst only on G400, and only if TV-out is used */
2948c2ecf20Sopenharmony_ci	if (minfo->outputs[1].src == MATROXFB_SRC_CRTC1)
2958c2ecf20Sopenharmony_ci		hw->CRTCEXT[1] |= 0x88;		/* enable horizontal and vertical vidrst */
2968c2ecf20Sopenharmony_ci	hw->CRTCEXT[2] =  ((vt & 0xC00) >> 10) |
2978c2ecf20Sopenharmony_ci			  ((vd & 0x400) >>  8) |	/* disp end */
2988c2ecf20Sopenharmony_ci			  ((vd & 0xC00) >>  7) |	/* vblanking start */
2998c2ecf20Sopenharmony_ci			  ((vs & 0xC00) >>  5) |
3008c2ecf20Sopenharmony_ci			  ((lc & 0x400) >>  3);
3018c2ecf20Sopenharmony_ci	hw->CRTCEXT[3] = (divider - 1) | 0x80;
3028c2ecf20Sopenharmony_ci	hw->CRTCEXT[4] = 0;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	hw->CRTC[0] = ht-4;
3058c2ecf20Sopenharmony_ci	hw->CRTC[1] = hd;
3068c2ecf20Sopenharmony_ci	hw->CRTC[2] = hd;
3078c2ecf20Sopenharmony_ci	hw->CRTC[3] = (hbe & 0x1F) | 0x80;
3088c2ecf20Sopenharmony_ci	hw->CRTC[4] = hs;
3098c2ecf20Sopenharmony_ci	hw->CRTC[5] = ((hbe & 0x20) << 2) | (he & 0x1F);
3108c2ecf20Sopenharmony_ci	hw->CRTC[6] = vt & 0xFF;
3118c2ecf20Sopenharmony_ci	hw->CRTC[7] = ((vt & 0x100) >> 8) |
3128c2ecf20Sopenharmony_ci		      ((vd & 0x100) >> 7) |
3138c2ecf20Sopenharmony_ci		      ((vs & 0x100) >> 6) |
3148c2ecf20Sopenharmony_ci		      ((vd & 0x100) >> 5) |
3158c2ecf20Sopenharmony_ci		      ((lc & 0x100) >> 4) |
3168c2ecf20Sopenharmony_ci		      ((vt & 0x200) >> 4) |
3178c2ecf20Sopenharmony_ci		      ((vd & 0x200) >> 3) |
3188c2ecf20Sopenharmony_ci		      ((vs & 0x200) >> 2);
3198c2ecf20Sopenharmony_ci	hw->CRTC[8] = 0x00;
3208c2ecf20Sopenharmony_ci	hw->CRTC[9] = ((vd & 0x200) >> 4) |
3218c2ecf20Sopenharmony_ci		      ((lc & 0x200) >> 3);
3228c2ecf20Sopenharmony_ci	if (m->dblscan && !m->interlaced)
3238c2ecf20Sopenharmony_ci		hw->CRTC[9] |= 0x80;
3248c2ecf20Sopenharmony_ci	for (i = 10; i < 16; i++)
3258c2ecf20Sopenharmony_ci		hw->CRTC[i] = 0x00;
3268c2ecf20Sopenharmony_ci	hw->CRTC[16] = vs /* & 0xFF */;
3278c2ecf20Sopenharmony_ci	hw->CRTC[17] = (ve & 0x0F) | 0x20;
3288c2ecf20Sopenharmony_ci	hw->CRTC[18] = vd /* & 0xFF */;
3298c2ecf20Sopenharmony_ci	hw->CRTC[19] = wd /* & 0xFF */;
3308c2ecf20Sopenharmony_ci	hw->CRTC[20] = 0x00;
3318c2ecf20Sopenharmony_ci	hw->CRTC[21] = vd /* & 0xFF */;
3328c2ecf20Sopenharmony_ci	hw->CRTC[22] = (vt + 1) /* & 0xFF */;
3338c2ecf20Sopenharmony_ci	hw->CRTC[23] = 0xC3;
3348c2ecf20Sopenharmony_ci	hw->CRTC[24] = lc;
3358c2ecf20Sopenharmony_ci	return 0;
3368c2ecf20Sopenharmony_ci};
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_civoid matroxfb_vgaHWrestore(struct matrox_fb_info *minfo)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	int i;
3418c2ecf20Sopenharmony_ci	struct matrox_hw_state * const hw = &minfo->hw;
3428c2ecf20Sopenharmony_ci	CRITFLAGS
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	DBG(__func__)
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	dprintk(KERN_INFO "MiscOutReg: %02X\n", hw->MiscOutReg);
3478c2ecf20Sopenharmony_ci	dprintk(KERN_INFO "SEQ regs:   ");
3488c2ecf20Sopenharmony_ci	for (i = 0; i < 5; i++)
3498c2ecf20Sopenharmony_ci		dprintk("%02X:", hw->SEQ[i]);
3508c2ecf20Sopenharmony_ci	dprintk("\n");
3518c2ecf20Sopenharmony_ci	dprintk(KERN_INFO "GDC regs:   ");
3528c2ecf20Sopenharmony_ci	for (i = 0; i < 9; i++)
3538c2ecf20Sopenharmony_ci		dprintk("%02X:", hw->GCTL[i]);
3548c2ecf20Sopenharmony_ci	dprintk("\n");
3558c2ecf20Sopenharmony_ci	dprintk(KERN_INFO "CRTC regs: ");
3568c2ecf20Sopenharmony_ci	for (i = 0; i < 25; i++)
3578c2ecf20Sopenharmony_ci		dprintk("%02X:", hw->CRTC[i]);
3588c2ecf20Sopenharmony_ci	dprintk("\n");
3598c2ecf20Sopenharmony_ci	dprintk(KERN_INFO "ATTR regs: ");
3608c2ecf20Sopenharmony_ci	for (i = 0; i < 21; i++)
3618c2ecf20Sopenharmony_ci		dprintk("%02X:", hw->ATTR[i]);
3628c2ecf20Sopenharmony_ci	dprintk("\n");
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	CRITBEGIN
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	mga_inb(M_ATTR_RESET);
3678c2ecf20Sopenharmony_ci	mga_outb(M_ATTR_INDEX, 0);
3688c2ecf20Sopenharmony_ci	mga_outb(M_MISC_REG, hw->MiscOutReg);
3698c2ecf20Sopenharmony_ci	for (i = 1; i < 5; i++)
3708c2ecf20Sopenharmony_ci		mga_setr(M_SEQ_INDEX, i, hw->SEQ[i]);
3718c2ecf20Sopenharmony_ci	mga_setr(M_CRTC_INDEX, 17, hw->CRTC[17] & 0x7F);
3728c2ecf20Sopenharmony_ci	for (i = 0; i < 25; i++)
3738c2ecf20Sopenharmony_ci		mga_setr(M_CRTC_INDEX, i, hw->CRTC[i]);
3748c2ecf20Sopenharmony_ci	for (i = 0; i < 9; i++)
3758c2ecf20Sopenharmony_ci		mga_setr(M_GRAPHICS_INDEX, i, hw->GCTL[i]);
3768c2ecf20Sopenharmony_ci	for (i = 0; i < 21; i++) {
3778c2ecf20Sopenharmony_ci		mga_inb(M_ATTR_RESET);
3788c2ecf20Sopenharmony_ci		mga_outb(M_ATTR_INDEX, i);
3798c2ecf20Sopenharmony_ci		mga_outb(M_ATTR_INDEX, hw->ATTR[i]);
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci	mga_outb(M_PALETTE_MASK, 0xFF);
3828c2ecf20Sopenharmony_ci	mga_outb(M_DAC_REG, 0x00);
3838c2ecf20Sopenharmony_ci	for (i = 0; i < 768; i++)
3848c2ecf20Sopenharmony_ci		mga_outb(M_DAC_VAL, hw->DACpal[i]);
3858c2ecf20Sopenharmony_ci	mga_inb(M_ATTR_RESET);
3868c2ecf20Sopenharmony_ci	mga_outb(M_ATTR_INDEX, 0x20);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	CRITEND
3898c2ecf20Sopenharmony_ci}
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic void get_pins(unsigned char __iomem* pins, struct matrox_bios* bd) {
3928c2ecf20Sopenharmony_ci	unsigned int b0 = readb(pins);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	if (b0 == 0x2E && readb(pins+1) == 0x41) {
3958c2ecf20Sopenharmony_ci		unsigned int pins_len = readb(pins+2);
3968c2ecf20Sopenharmony_ci		unsigned int i;
3978c2ecf20Sopenharmony_ci		unsigned char cksum;
3988c2ecf20Sopenharmony_ci		unsigned char* dst = bd->pins;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci		if (pins_len < 3 || pins_len > 128) {
4018c2ecf20Sopenharmony_ci			return;
4028c2ecf20Sopenharmony_ci		}
4038c2ecf20Sopenharmony_ci		*dst++ = 0x2E;
4048c2ecf20Sopenharmony_ci		*dst++ = 0x41;
4058c2ecf20Sopenharmony_ci		*dst++ = pins_len;
4068c2ecf20Sopenharmony_ci		cksum = 0x2E + 0x41 + pins_len;
4078c2ecf20Sopenharmony_ci		for (i = 3; i < pins_len; i++) {
4088c2ecf20Sopenharmony_ci			cksum += *dst++ = readb(pins+i);
4098c2ecf20Sopenharmony_ci		}
4108c2ecf20Sopenharmony_ci		if (cksum) {
4118c2ecf20Sopenharmony_ci			return;
4128c2ecf20Sopenharmony_ci		}
4138c2ecf20Sopenharmony_ci		bd->pins_len = pins_len;
4148c2ecf20Sopenharmony_ci	} else if (b0 == 0x40 && readb(pins+1) == 0x00) {
4158c2ecf20Sopenharmony_ci		unsigned int i;
4168c2ecf20Sopenharmony_ci		unsigned char* dst = bd->pins;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		*dst++ = 0x40;
4198c2ecf20Sopenharmony_ci		*dst++ = 0;
4208c2ecf20Sopenharmony_ci		for (i = 2; i < 0x40; i++) {
4218c2ecf20Sopenharmony_ci			*dst++ = readb(pins+i);
4228c2ecf20Sopenharmony_ci		}
4238c2ecf20Sopenharmony_ci		bd->pins_len = 0x40;
4248c2ecf20Sopenharmony_ci	}
4258c2ecf20Sopenharmony_ci}
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic void get_bios_version(unsigned char __iomem * vbios, struct matrox_bios* bd) {
4288c2ecf20Sopenharmony_ci	unsigned int pcir_offset;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	pcir_offset = readb(vbios + 24) | (readb(vbios + 25) << 8);
4318c2ecf20Sopenharmony_ci	if (pcir_offset >= 26 && pcir_offset < 0xFFE0 &&
4328c2ecf20Sopenharmony_ci	    readb(vbios + pcir_offset    ) == 'P' &&
4338c2ecf20Sopenharmony_ci	    readb(vbios + pcir_offset + 1) == 'C' &&
4348c2ecf20Sopenharmony_ci	    readb(vbios + pcir_offset + 2) == 'I' &&
4358c2ecf20Sopenharmony_ci	    readb(vbios + pcir_offset + 3) == 'R') {
4368c2ecf20Sopenharmony_ci		unsigned char h;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci		h = readb(vbios + pcir_offset + 0x12);
4398c2ecf20Sopenharmony_ci		bd->version.vMaj = (h >> 4) & 0xF;
4408c2ecf20Sopenharmony_ci		bd->version.vMin = h & 0xF;
4418c2ecf20Sopenharmony_ci		bd->version.vRev = readb(vbios + pcir_offset + 0x13);
4428c2ecf20Sopenharmony_ci	} else {
4438c2ecf20Sopenharmony_ci		unsigned char h;
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci		h = readb(vbios + 5);
4468c2ecf20Sopenharmony_ci		bd->version.vMaj = (h >> 4) & 0xF;
4478c2ecf20Sopenharmony_ci		bd->version.vMin = h & 0xF;
4488c2ecf20Sopenharmony_ci		bd->version.vRev = 0;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_cistatic void get_bios_output(unsigned char __iomem* vbios, struct matrox_bios* bd) {
4538c2ecf20Sopenharmony_ci	unsigned char b;
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ci	b = readb(vbios + 0x7FF1);
4568c2ecf20Sopenharmony_ci	if (b == 0xFF) {
4578c2ecf20Sopenharmony_ci		b = 0;
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci	bd->output.state = b;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic void get_bios_tvout(unsigned char __iomem* vbios, struct matrox_bios* bd) {
4638c2ecf20Sopenharmony_ci	unsigned int i;
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	/* Check for 'IBM .*(V....TVO' string - it means TVO BIOS */
4668c2ecf20Sopenharmony_ci	bd->output.tvout = 0;
4678c2ecf20Sopenharmony_ci	if (readb(vbios + 0x1D) != 'I' ||
4688c2ecf20Sopenharmony_ci	    readb(vbios + 0x1E) != 'B' ||
4698c2ecf20Sopenharmony_ci	    readb(vbios + 0x1F) != 'M' ||
4708c2ecf20Sopenharmony_ci	    readb(vbios + 0x20) != ' ') {
4718c2ecf20Sopenharmony_ci	    	return;
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci	for (i = 0x2D; i < 0x2D + 128; i++) {
4748c2ecf20Sopenharmony_ci		unsigned char b = readb(vbios + i);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci		if (b == '(' && readb(vbios + i + 1) == 'V') {
4778c2ecf20Sopenharmony_ci			if (readb(vbios + i + 6) == 'T' &&
4788c2ecf20Sopenharmony_ci			    readb(vbios + i + 7) == 'V' &&
4798c2ecf20Sopenharmony_ci			    readb(vbios + i + 8) == 'O') {
4808c2ecf20Sopenharmony_ci				bd->output.tvout = 1;
4818c2ecf20Sopenharmony_ci			}
4828c2ecf20Sopenharmony_ci			return;
4838c2ecf20Sopenharmony_ci		}
4848c2ecf20Sopenharmony_ci		if (b == 0)
4858c2ecf20Sopenharmony_ci			break;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci}
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_cistatic void parse_bios(unsigned char __iomem* vbios, struct matrox_bios* bd) {
4908c2ecf20Sopenharmony_ci	unsigned int pins_offset;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	if (readb(vbios) != 0x55 || readb(vbios + 1) != 0xAA) {
4938c2ecf20Sopenharmony_ci		return;
4948c2ecf20Sopenharmony_ci	}
4958c2ecf20Sopenharmony_ci	bd->bios_valid = 1;
4968c2ecf20Sopenharmony_ci	get_bios_version(vbios, bd);
4978c2ecf20Sopenharmony_ci	get_bios_output(vbios, bd);
4988c2ecf20Sopenharmony_ci	get_bios_tvout(vbios, bd);
4998c2ecf20Sopenharmony_ci#if defined(__powerpc__)
5008c2ecf20Sopenharmony_ci	/* On PowerPC cards, the PInS offset isn't stored at the end of the
5018c2ecf20Sopenharmony_ci	 * BIOS image.  Instead, you must search the entire BIOS image for
5028c2ecf20Sopenharmony_ci	 * the magic PInS signature.
5038c2ecf20Sopenharmony_ci	 *
5048c2ecf20Sopenharmony_ci	 * This actually applies to all OpenFirmware base cards.  Since these
5058c2ecf20Sopenharmony_ci	 * cards could be put in a MIPS or SPARC system, should the condition
5068c2ecf20Sopenharmony_ci	 * be something different?
5078c2ecf20Sopenharmony_ci	 */
5088c2ecf20Sopenharmony_ci	for ( pins_offset = 0 ; pins_offset <= 0xFF80 ; pins_offset++ ) {
5098c2ecf20Sopenharmony_ci		unsigned char header[3];
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci		header[0] = readb(vbios + pins_offset);
5128c2ecf20Sopenharmony_ci		header[1] = readb(vbios + pins_offset + 1);
5138c2ecf20Sopenharmony_ci		header[2] = readb(vbios + pins_offset + 2);
5148c2ecf20Sopenharmony_ci		if ( (header[0] == 0x2E) && (header[1] == 0x41)
5158c2ecf20Sopenharmony_ci		     && ((header[2] == 0x40) || (header[2] == 0x80)) ) {
5168c2ecf20Sopenharmony_ci			printk(KERN_INFO "PInS data found at offset %u\n",
5178c2ecf20Sopenharmony_ci			       pins_offset);
5188c2ecf20Sopenharmony_ci			get_pins(vbios + pins_offset, bd);
5198c2ecf20Sopenharmony_ci			break;
5208c2ecf20Sopenharmony_ci		}
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci#else
5238c2ecf20Sopenharmony_ci	pins_offset = readb(vbios + 0x7FFC) | (readb(vbios + 0x7FFD) << 8);
5248c2ecf20Sopenharmony_ci	if (pins_offset <= 0xFF80) {
5258c2ecf20Sopenharmony_ci		get_pins(vbios + pins_offset, bd);
5268c2ecf20Sopenharmony_ci	}
5278c2ecf20Sopenharmony_ci#endif
5288c2ecf20Sopenharmony_ci}
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_cistatic int parse_pins1(struct matrox_fb_info *minfo,
5318c2ecf20Sopenharmony_ci		       const struct matrox_bios *bd)
5328c2ecf20Sopenharmony_ci{
5338c2ecf20Sopenharmony_ci	unsigned int maxdac;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	switch (bd->pins[22]) {
5368c2ecf20Sopenharmony_ci		case 0:		maxdac = 175000; break;
5378c2ecf20Sopenharmony_ci		case 1:		maxdac = 220000; break;
5388c2ecf20Sopenharmony_ci		default:	maxdac = 240000; break;
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci	if (get_unaligned_le16(bd->pins + 24)) {
5418c2ecf20Sopenharmony_ci		maxdac = get_unaligned_le16(bd->pins + 24) * 10;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax = maxdac;
5448c2ecf20Sopenharmony_ci	minfo->values.pll.system = get_unaligned_le16(bd->pins + 28) ?
5458c2ecf20Sopenharmony_ci		get_unaligned_le16(bd->pins + 28) * 10 : 50000;
5468c2ecf20Sopenharmony_ci	/* ignore 4MB, 8MB, module clocks */
5478c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq = 14318;
5488c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= 0x00030101;
5498c2ecf20Sopenharmony_ci	return 0;
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic void default_pins1(struct matrox_fb_info *minfo)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	/* Millennium */
5558c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	= 220000;
5568c2ecf20Sopenharmony_ci	minfo->values.pll.system	=  50000;
5578c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	=  14318;
5588c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= 0x00030101;
5598c2ecf20Sopenharmony_ci}
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic int parse_pins2(struct matrox_fb_info *minfo,
5628c2ecf20Sopenharmony_ci		       const struct matrox_bios *bd)
5638c2ecf20Sopenharmony_ci{
5648c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	=
5658c2ecf20Sopenharmony_ci	minfo->limits.system.vcomax	= (bd->pins[41] == 0xFF) ? 230000 : ((bd->pins[41] + 100) * 1000);
5668c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= ((bd->pins[51] & 0x01) ? 0x00000001 : 0) |
5678c2ecf20Sopenharmony_ci					  ((bd->pins[51] & 0x02) ? 0x00000100 : 0) |
5688c2ecf20Sopenharmony_ci					  ((bd->pins[51] & 0x04) ? 0x00010000 : 0) |
5698c2ecf20Sopenharmony_ci					  ((bd->pins[51] & 0x08) ? 0x00020000 : 0);
5708c2ecf20Sopenharmony_ci	minfo->values.pll.system	= (bd->pins[43] == 0xFF) ? 50000 : ((bd->pins[43] + 100) * 1000);
5718c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	= 14318;
5728c2ecf20Sopenharmony_ci	return 0;
5738c2ecf20Sopenharmony_ci}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_cistatic void default_pins2(struct matrox_fb_info *minfo)
5768c2ecf20Sopenharmony_ci{
5778c2ecf20Sopenharmony_ci	/* Millennium II, Mystique */
5788c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	=
5798c2ecf20Sopenharmony_ci	minfo->limits.system.vcomax	= 230000;
5808c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= 0x00030101;
5818c2ecf20Sopenharmony_ci	minfo->values.pll.system	=  50000;
5828c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	=  14318;
5838c2ecf20Sopenharmony_ci}
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_cistatic int parse_pins3(struct matrox_fb_info *minfo,
5868c2ecf20Sopenharmony_ci		       const struct matrox_bios *bd)
5878c2ecf20Sopenharmony_ci{
5888c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	=
5898c2ecf20Sopenharmony_ci	minfo->limits.system.vcomax	= (bd->pins[36] == 0xFF) ? 230000			: ((bd->pins[36] + 100) * 1000);
5908c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= get_unaligned_le32(bd->pins + 48) == 0xFFFFFFFF ?
5918c2ecf20Sopenharmony_ci		0x01250A21 : get_unaligned_le32(bd->pins + 48);
5928c2ecf20Sopenharmony_ci	/* memory config */
5938c2ecf20Sopenharmony_ci	minfo->values.reg.memrdbk	= ((bd->pins[57] << 21) & 0x1E000000) |
5948c2ecf20Sopenharmony_ci					  ((bd->pins[57] << 22) & 0x00C00000) |
5958c2ecf20Sopenharmony_ci					  ((bd->pins[56] <<  1) & 0x000001E0) |
5968c2ecf20Sopenharmony_ci					  ( bd->pins[56]        & 0x0000000F);
5978c2ecf20Sopenharmony_ci	minfo->values.reg.opt		= (bd->pins[54] & 7) << 10;
5988c2ecf20Sopenharmony_ci	minfo->values.reg.opt2		= bd->pins[58] << 12;
5998c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	= (bd->pins[52] & 0x20) ? 14318 : 27000;
6008c2ecf20Sopenharmony_ci	return 0;
6018c2ecf20Sopenharmony_ci}
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_cistatic void default_pins3(struct matrox_fb_info *minfo)
6048c2ecf20Sopenharmony_ci{
6058c2ecf20Sopenharmony_ci	/* G100, G200 */
6068c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	=
6078c2ecf20Sopenharmony_ci	minfo->limits.system.vcomax	= 230000;
6088c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= 0x01250A21;
6098c2ecf20Sopenharmony_ci	minfo->values.reg.memrdbk	= 0x00000000;
6108c2ecf20Sopenharmony_ci	minfo->values.reg.opt		= 0x00000C00;
6118c2ecf20Sopenharmony_ci	minfo->values.reg.opt2		= 0x00000000;
6128c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	=  27000;
6138c2ecf20Sopenharmony_ci}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_cistatic int parse_pins4(struct matrox_fb_info *minfo,
6168c2ecf20Sopenharmony_ci		       const struct matrox_bios *bd)
6178c2ecf20Sopenharmony_ci{
6188c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	= (bd->pins[ 39] == 0xFF) ? 230000			: bd->pins[ 39] * 4000;
6198c2ecf20Sopenharmony_ci	minfo->limits.system.vcomax	= (bd->pins[ 38] == 0xFF) ? minfo->limits.pixel.vcomax	: bd->pins[ 38] * 4000;
6208c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= get_unaligned_le32(bd->pins + 71);
6218c2ecf20Sopenharmony_ci	minfo->values.reg.memrdbk	= ((bd->pins[87] << 21) & 0x1E000000) |
6228c2ecf20Sopenharmony_ci					  ((bd->pins[87] << 22) & 0x00C00000) |
6238c2ecf20Sopenharmony_ci					  ((bd->pins[86] <<  1) & 0x000001E0) |
6248c2ecf20Sopenharmony_ci					  ( bd->pins[86]        & 0x0000000F);
6258c2ecf20Sopenharmony_ci	minfo->values.reg.opt		= ((bd->pins[53] << 15) & 0x00400000) |
6268c2ecf20Sopenharmony_ci					  ((bd->pins[53] << 22) & 0x10000000) |
6278c2ecf20Sopenharmony_ci					  ((bd->pins[53] <<  7) & 0x00001C00);
6288c2ecf20Sopenharmony_ci	minfo->values.reg.opt3		= get_unaligned_le32(bd->pins + 67);
6298c2ecf20Sopenharmony_ci	minfo->values.pll.system	= (bd->pins[ 65] == 0xFF) ? 200000 			: bd->pins[ 65] * 4000;
6308c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	= (bd->pins[ 92] & 0x01) ? 14318 : 27000;
6318c2ecf20Sopenharmony_ci	return 0;
6328c2ecf20Sopenharmony_ci}
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_cistatic void default_pins4(struct matrox_fb_info *minfo)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	/* G400 */
6378c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	=
6388c2ecf20Sopenharmony_ci	minfo->limits.system.vcomax	= 252000;
6398c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= 0x04A450A1;
6408c2ecf20Sopenharmony_ci	minfo->values.reg.memrdbk	= 0x000000E7;
6418c2ecf20Sopenharmony_ci	minfo->values.reg.opt		= 0x10000400;
6428c2ecf20Sopenharmony_ci	minfo->values.reg.opt3		= 0x0190A419;
6438c2ecf20Sopenharmony_ci	minfo->values.pll.system	= 200000;
6448c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	= 27000;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic int parse_pins5(struct matrox_fb_info *minfo,
6488c2ecf20Sopenharmony_ci		       const struct matrox_bios *bd)
6498c2ecf20Sopenharmony_ci{
6508c2ecf20Sopenharmony_ci	unsigned int mult;
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	mult = bd->pins[4]?8000:6000;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	= (bd->pins[ 38] == 0xFF) ? 600000			: bd->pins[ 38] * mult;
6558c2ecf20Sopenharmony_ci	minfo->limits.system.vcomax	= (bd->pins[ 36] == 0xFF) ? minfo->limits.pixel.vcomax	: bd->pins[ 36] * mult;
6568c2ecf20Sopenharmony_ci	minfo->limits.video.vcomax	= (bd->pins[ 37] == 0xFF) ? minfo->limits.system.vcomax	: bd->pins[ 37] * mult;
6578c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomin	= (bd->pins[123] == 0xFF) ? 256000			: bd->pins[123] * mult;
6588c2ecf20Sopenharmony_ci	minfo->limits.system.vcomin	= (bd->pins[121] == 0xFF) ? minfo->limits.pixel.vcomin	: bd->pins[121] * mult;
6598c2ecf20Sopenharmony_ci	minfo->limits.video.vcomin	= (bd->pins[122] == 0xFF) ? minfo->limits.system.vcomin	: bd->pins[122] * mult;
6608c2ecf20Sopenharmony_ci	minfo->values.pll.system	=
6618c2ecf20Sopenharmony_ci	minfo->values.pll.video		= (bd->pins[ 92] == 0xFF) ? 284000			: bd->pins[ 92] * 4000;
6628c2ecf20Sopenharmony_ci	minfo->values.reg.opt		= get_unaligned_le32(bd->pins + 48);
6638c2ecf20Sopenharmony_ci	minfo->values.reg.opt2		= get_unaligned_le32(bd->pins + 52);
6648c2ecf20Sopenharmony_ci	minfo->values.reg.opt3		= get_unaligned_le32(bd->pins + 94);
6658c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= get_unaligned_le32(bd->pins + 98);
6668c2ecf20Sopenharmony_ci	minfo->values.reg.memmisc	= get_unaligned_le32(bd->pins + 102);
6678c2ecf20Sopenharmony_ci	minfo->values.reg.memrdbk	= get_unaligned_le32(bd->pins + 106);
6688c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	= (bd->pins[110] & 0x01) ? 14318 : 27000;
6698c2ecf20Sopenharmony_ci	minfo->values.memory.ddr	= (bd->pins[114] & 0x60) == 0x20;
6708c2ecf20Sopenharmony_ci	minfo->values.memory.dll	= (bd->pins[115] & 0x02) != 0;
6718c2ecf20Sopenharmony_ci	minfo->values.memory.emrswen	= (bd->pins[115] & 0x01) != 0;
6728c2ecf20Sopenharmony_ci	minfo->values.reg.maccess	= minfo->values.memory.emrswen ? 0x00004000 : 0x00000000;
6738c2ecf20Sopenharmony_ci	if (bd->pins[115] & 4) {
6748c2ecf20Sopenharmony_ci		minfo->values.reg.mctlwtst_core = minfo->values.reg.mctlwtst;
6758c2ecf20Sopenharmony_ci	} else {
6768c2ecf20Sopenharmony_ci		static const u8 wtst_xlat[] = {
6778c2ecf20Sopenharmony_ci			0, 1, 5, 6, 7, 5, 2, 3
6788c2ecf20Sopenharmony_ci		};
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci		minfo->values.reg.mctlwtst_core = (minfo->values.reg.mctlwtst & ~7) |
6818c2ecf20Sopenharmony_ci						  wtst_xlat[minfo->values.reg.mctlwtst & 7];
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci	minfo->max_pixel_clock_panellink = bd->pins[47] * 4000;
6848c2ecf20Sopenharmony_ci	return 0;
6858c2ecf20Sopenharmony_ci}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_cistatic void default_pins5(struct matrox_fb_info *minfo)
6888c2ecf20Sopenharmony_ci{
6898c2ecf20Sopenharmony_ci	/* Mine 16MB G450 with SDRAM DDR */
6908c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomax	=
6918c2ecf20Sopenharmony_ci	minfo->limits.system.vcomax	=
6928c2ecf20Sopenharmony_ci	minfo->limits.video.vcomax	= 600000;
6938c2ecf20Sopenharmony_ci	minfo->limits.pixel.vcomin	=
6948c2ecf20Sopenharmony_ci	minfo->limits.system.vcomin	=
6958c2ecf20Sopenharmony_ci	minfo->limits.video.vcomin	= 256000;
6968c2ecf20Sopenharmony_ci	minfo->values.pll.system	=
6978c2ecf20Sopenharmony_ci	minfo->values.pll.video		= 284000;
6988c2ecf20Sopenharmony_ci	minfo->values.reg.opt		= 0x404A1160;
6998c2ecf20Sopenharmony_ci	minfo->values.reg.opt2		= 0x0000AC00;
7008c2ecf20Sopenharmony_ci	minfo->values.reg.opt3		= 0x0090A409;
7018c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst_core	=
7028c2ecf20Sopenharmony_ci	minfo->values.reg.mctlwtst	= 0x0C81462B;
7038c2ecf20Sopenharmony_ci	minfo->values.reg.memmisc	= 0x80000004;
7048c2ecf20Sopenharmony_ci	minfo->values.reg.memrdbk	= 0x01001103;
7058c2ecf20Sopenharmony_ci	minfo->features.pll.ref_freq	= 27000;
7068c2ecf20Sopenharmony_ci	minfo->values.memory.ddr	= 1;
7078c2ecf20Sopenharmony_ci	minfo->values.memory.dll	= 1;
7088c2ecf20Sopenharmony_ci	minfo->values.memory.emrswen	= 1;
7098c2ecf20Sopenharmony_ci	minfo->values.reg.maccess	= 0x00004000;
7108c2ecf20Sopenharmony_ci}
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_cistatic int matroxfb_set_limits(struct matrox_fb_info *minfo,
7138c2ecf20Sopenharmony_ci			       const struct matrox_bios *bd)
7148c2ecf20Sopenharmony_ci{
7158c2ecf20Sopenharmony_ci	unsigned int pins_version;
7168c2ecf20Sopenharmony_ci	static const unsigned int pinslen[] = { 64, 64, 64, 128, 128 };
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	switch (minfo->chip) {
7198c2ecf20Sopenharmony_ci		case MGA_2064:	default_pins1(minfo); break;
7208c2ecf20Sopenharmony_ci		case MGA_2164:
7218c2ecf20Sopenharmony_ci		case MGA_1064:
7228c2ecf20Sopenharmony_ci		case MGA_1164:	default_pins2(minfo); break;
7238c2ecf20Sopenharmony_ci		case MGA_G100:
7248c2ecf20Sopenharmony_ci		case MGA_G200:	default_pins3(minfo); break;
7258c2ecf20Sopenharmony_ci		case MGA_G400:	default_pins4(minfo); break;
7268c2ecf20Sopenharmony_ci		case MGA_G450:
7278c2ecf20Sopenharmony_ci		case MGA_G550:	default_pins5(minfo); break;
7288c2ecf20Sopenharmony_ci	}
7298c2ecf20Sopenharmony_ci	if (!bd->bios_valid) {
7308c2ecf20Sopenharmony_ci		printk(KERN_INFO "matroxfb: Your Matrox device does not have BIOS\n");
7318c2ecf20Sopenharmony_ci		return -1;
7328c2ecf20Sopenharmony_ci	}
7338c2ecf20Sopenharmony_ci	if (bd->pins_len < 64) {
7348c2ecf20Sopenharmony_ci		printk(KERN_INFO "matroxfb: BIOS on your Matrox device does not contain powerup info\n");
7358c2ecf20Sopenharmony_ci		return -1;
7368c2ecf20Sopenharmony_ci	}
7378c2ecf20Sopenharmony_ci	if (bd->pins[0] == 0x2E && bd->pins[1] == 0x41) {
7388c2ecf20Sopenharmony_ci		pins_version = bd->pins[5];
7398c2ecf20Sopenharmony_ci		if (pins_version < 2 || pins_version > 5) {
7408c2ecf20Sopenharmony_ci			printk(KERN_INFO "matroxfb: Unknown version (%u) of powerup info\n", pins_version);
7418c2ecf20Sopenharmony_ci			return -1;
7428c2ecf20Sopenharmony_ci		}
7438c2ecf20Sopenharmony_ci	} else {
7448c2ecf20Sopenharmony_ci		pins_version = 1;
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci	if (bd->pins_len != pinslen[pins_version - 1]) {
7478c2ecf20Sopenharmony_ci		printk(KERN_INFO "matroxfb: Invalid powerup info\n");
7488c2ecf20Sopenharmony_ci		return -1;
7498c2ecf20Sopenharmony_ci	}
7508c2ecf20Sopenharmony_ci	switch (pins_version) {
7518c2ecf20Sopenharmony_ci		case 1:
7528c2ecf20Sopenharmony_ci			return parse_pins1(minfo, bd);
7538c2ecf20Sopenharmony_ci		case 2:
7548c2ecf20Sopenharmony_ci			return parse_pins2(minfo, bd);
7558c2ecf20Sopenharmony_ci		case 3:
7568c2ecf20Sopenharmony_ci			return parse_pins3(minfo, bd);
7578c2ecf20Sopenharmony_ci		case 4:
7588c2ecf20Sopenharmony_ci			return parse_pins4(minfo, bd);
7598c2ecf20Sopenharmony_ci		case 5:
7608c2ecf20Sopenharmony_ci			return parse_pins5(minfo, bd);
7618c2ecf20Sopenharmony_ci		default:
7628c2ecf20Sopenharmony_ci			printk(KERN_DEBUG "matroxfb: Powerup info version %u is not yet supported\n", pins_version);
7638c2ecf20Sopenharmony_ci			return -1;
7648c2ecf20Sopenharmony_ci	}
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_civoid matroxfb_read_pins(struct matrox_fb_info *minfo)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	u32 opt;
7708c2ecf20Sopenharmony_ci	u32 biosbase;
7718c2ecf20Sopenharmony_ci	u32 fbbase;
7728c2ecf20Sopenharmony_ci	struct pci_dev *pdev = minfo->pcidev;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	memset(&minfo->bios, 0, sizeof(minfo->bios));
7758c2ecf20Sopenharmony_ci	pci_read_config_dword(pdev, PCI_OPTION_REG, &opt);
7768c2ecf20Sopenharmony_ci	pci_write_config_dword(pdev, PCI_OPTION_REG, opt | PCI_OPTION_ENABLE_ROM);
7778c2ecf20Sopenharmony_ci	pci_read_config_dword(pdev, PCI_ROM_ADDRESS, &biosbase);
7788c2ecf20Sopenharmony_ci	pci_read_config_dword(pdev, minfo->devflags.fbResource, &fbbase);
7798c2ecf20Sopenharmony_ci	pci_write_config_dword(pdev, PCI_ROM_ADDRESS, (fbbase & PCI_ROM_ADDRESS_MASK) | PCI_ROM_ADDRESS_ENABLE);
7808c2ecf20Sopenharmony_ci	parse_bios(vaddr_va(minfo->video.vbase), &minfo->bios);
7818c2ecf20Sopenharmony_ci	pci_write_config_dword(pdev, PCI_ROM_ADDRESS, biosbase);
7828c2ecf20Sopenharmony_ci	pci_write_config_dword(pdev, PCI_OPTION_REG, opt);
7838c2ecf20Sopenharmony_ci#ifdef CONFIG_X86
7848c2ecf20Sopenharmony_ci	if (!minfo->bios.bios_valid) {
7858c2ecf20Sopenharmony_ci		unsigned char __iomem* b;
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci		b = ioremap(0x000C0000, 65536);
7888c2ecf20Sopenharmony_ci		if (!b) {
7898c2ecf20Sopenharmony_ci			printk(KERN_INFO "matroxfb: Unable to map legacy BIOS\n");
7908c2ecf20Sopenharmony_ci		} else {
7918c2ecf20Sopenharmony_ci			unsigned int ven = readb(b+0x64+0) | (readb(b+0x64+1) << 8);
7928c2ecf20Sopenharmony_ci			unsigned int dev = readb(b+0x64+2) | (readb(b+0x64+3) << 8);
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci			if (ven != pdev->vendor || dev != pdev->device) {
7958c2ecf20Sopenharmony_ci				printk(KERN_INFO "matroxfb: Legacy BIOS is for %04X:%04X, while this device is %04X:%04X\n",
7968c2ecf20Sopenharmony_ci					ven, dev, pdev->vendor, pdev->device);
7978c2ecf20Sopenharmony_ci			} else {
7988c2ecf20Sopenharmony_ci				parse_bios(b, &minfo->bios);
7998c2ecf20Sopenharmony_ci			}
8008c2ecf20Sopenharmony_ci			iounmap(b);
8018c2ecf20Sopenharmony_ci		}
8028c2ecf20Sopenharmony_ci	}
8038c2ecf20Sopenharmony_ci#endif
8048c2ecf20Sopenharmony_ci	matroxfb_set_limits(minfo, &minfo->bios);
8058c2ecf20Sopenharmony_ci	printk(KERN_INFO "PInS memtype = %u\n",
8068c2ecf20Sopenharmony_ci	       (minfo->values.reg.opt & 0x1C00) >> 10);
8078c2ecf20Sopenharmony_ci}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(matroxfb_DAC_in);
8108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(matroxfb_DAC_out);
8118c2ecf20Sopenharmony_ciEXPORT_SYMBOL(matroxfb_var2my);
8128c2ecf20Sopenharmony_ciEXPORT_SYMBOL(matroxfb_PLL_calcclock);
8138c2ecf20Sopenharmony_ciEXPORT_SYMBOL(matroxfb_vgaHWinit);		/* DAC1064, Ti3026 */
8148c2ecf20Sopenharmony_ciEXPORT_SYMBOL(matroxfb_vgaHWrestore);		/* DAC1064, Ti3026 */
8158c2ecf20Sopenharmony_ciEXPORT_SYMBOL(matroxfb_read_pins);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ciMODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
8188c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Miscellaneous support for Matrox video cards");
8198c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
820