18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * tdfxfb.c
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Author: Hannu Mallat <hmallat@cc.hut.fi>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright © 1999 Hannu Mallat
98c2ecf20Sopenharmony_ci * All rights reserved
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Created      : Thu Sep 23 18:17:43 1999, hmallat
128c2ecf20Sopenharmony_ci * Last modified: Tue Nov  2 21:19:47 1999, hmallat
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * I2C part copied from the i2c-voodoo3.c driver by:
158c2ecf20Sopenharmony_ci * Frodo Looijaard <frodol@dds.nl>,
168c2ecf20Sopenharmony_ci * Philip Edelbrock <phil@netroedge.com>,
178c2ecf20Sopenharmony_ci * Ralph Metzler <rjkm@thp.uni-koeln.de>, and
188c2ecf20Sopenharmony_ci * Mark D. Studebaker <mdsxyz123@yahoo.com>
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci * Lots of the information here comes from the Daryll Strauss' Banshee
218c2ecf20Sopenharmony_ci * patches to the XF86 server, and the rest comes from the 3dfx
228c2ecf20Sopenharmony_ci * Banshee specification. I'm very much indebted to Daryll for his
238c2ecf20Sopenharmony_ci * work on the X server.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * Voodoo3 support was contributed Harold Oga. Lots of additions
268c2ecf20Sopenharmony_ci * (proper acceleration, 24 bpp, hardware cursor) and bug fixes by Attila
278c2ecf20Sopenharmony_ci * Kesmarki. Thanks guys!
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * Voodoo1 and Voodoo2 support aren't relevant to this driver as they
308c2ecf20Sopenharmony_ci * behave very differently from the Voodoo3/4/5. For anyone wanting to
318c2ecf20Sopenharmony_ci * use frame buffer on the Voodoo1/2, see the sstfb driver (which is
328c2ecf20Sopenharmony_ci * located at http://www.sourceforge.net/projects/sstfb).
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * While I _am_ grateful to 3Dfx for releasing the specs for Banshee,
358c2ecf20Sopenharmony_ci * I do wish the next version is a bit more complete. Without the XF86
368c2ecf20Sopenharmony_ci * patches I couldn't have gotten even this far... for instance, the
378c2ecf20Sopenharmony_ci * extensions to the VGA register set go completely unmentioned in the
388c2ecf20Sopenharmony_ci * spec! Also, lots of references are made to the 'SST core', but no
398c2ecf20Sopenharmony_ci * spec is publicly available, AFAIK.
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * The structure of this driver comes pretty much from the Permedia
428c2ecf20Sopenharmony_ci * driver by Ilario Nardinocchi, which in turn is based on skeletonfb.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * TODO:
458c2ecf20Sopenharmony_ci * - multihead support (basically need to support an array of fb_infos)
468c2ecf20Sopenharmony_ci * - support other architectures (PPC, Alpha); does the fact that the VGA
478c2ecf20Sopenharmony_ci *   core can be accessed only thru I/O (not memory mapped) complicate
488c2ecf20Sopenharmony_ci *   things?
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Version history:
518c2ecf20Sopenharmony_ci *
528c2ecf20Sopenharmony_ci * 0.1.4 (released 2002-05-28)	ported over to new fbdev api by James Simmons
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * 0.1.3 (released 1999-11-02)	added Attila's panning support, code
558c2ecf20Sopenharmony_ci *				reorg, hwcursor address page size alignment
568c2ecf20Sopenharmony_ci *				(for mmapping both frame buffer and regs),
578c2ecf20Sopenharmony_ci *				and my changes to get rid of hardcoded
588c2ecf20Sopenharmony_ci *				VGA i/o register locations (uses PCI
598c2ecf20Sopenharmony_ci *				configuration info now)
608c2ecf20Sopenharmony_ci * 0.1.2 (released 1999-10-19)	added Attila Kesmarki's bug fixes and
618c2ecf20Sopenharmony_ci *				improvements
628c2ecf20Sopenharmony_ci * 0.1.1 (released 1999-10-07)	added Voodoo3 support by Harold Oga.
638c2ecf20Sopenharmony_ci * 0.1.0 (released 1999-10-06)	initial version
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#include <linux/module.h>
688c2ecf20Sopenharmony_ci#include <linux/kernel.h>
698c2ecf20Sopenharmony_ci#include <linux/errno.h>
708c2ecf20Sopenharmony_ci#include <linux/string.h>
718c2ecf20Sopenharmony_ci#include <linux/mm.h>
728c2ecf20Sopenharmony_ci#include <linux/slab.h>
738c2ecf20Sopenharmony_ci#include <linux/fb.h>
748c2ecf20Sopenharmony_ci#include <linux/init.h>
758c2ecf20Sopenharmony_ci#include <linux/pci.h>
768c2ecf20Sopenharmony_ci#include <asm/io.h>
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#include <video/tdfx.h>
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define DPRINTK(a, b...) pr_debug("fb: %s: " a, __func__ , ## b)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define BANSHEE_MAX_PIXCLOCK 270000
838c2ecf20Sopenharmony_ci#define VOODOO3_MAX_PIXCLOCK 300000
848c2ecf20Sopenharmony_ci#define VOODOO5_MAX_PIXCLOCK 350000
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic const struct fb_fix_screeninfo tdfx_fix = {
878c2ecf20Sopenharmony_ci	.type =		FB_TYPE_PACKED_PIXELS,
888c2ecf20Sopenharmony_ci	.visual =	FB_VISUAL_PSEUDOCOLOR,
898c2ecf20Sopenharmony_ci	.ypanstep =	1,
908c2ecf20Sopenharmony_ci	.ywrapstep =	1,
918c2ecf20Sopenharmony_ci	.accel =	FB_ACCEL_3DFX_BANSHEE
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic const struct fb_var_screeninfo tdfx_var = {
958c2ecf20Sopenharmony_ci	/* "640x480, 8 bpp @ 60 Hz */
968c2ecf20Sopenharmony_ci	.xres =		640,
978c2ecf20Sopenharmony_ci	.yres =		480,
988c2ecf20Sopenharmony_ci	.xres_virtual =	640,
998c2ecf20Sopenharmony_ci	.yres_virtual =	1024,
1008c2ecf20Sopenharmony_ci	.bits_per_pixel = 8,
1018c2ecf20Sopenharmony_ci	.red =		{0, 8, 0},
1028c2ecf20Sopenharmony_ci	.blue =		{0, 8, 0},
1038c2ecf20Sopenharmony_ci	.green =	{0, 8, 0},
1048c2ecf20Sopenharmony_ci	.activate =	FB_ACTIVATE_NOW,
1058c2ecf20Sopenharmony_ci	.height =	-1,
1068c2ecf20Sopenharmony_ci	.width =	-1,
1078c2ecf20Sopenharmony_ci	.accel_flags =	FB_ACCELF_TEXT,
1088c2ecf20Sopenharmony_ci	.pixclock =	39722,
1098c2ecf20Sopenharmony_ci	.left_margin =	40,
1108c2ecf20Sopenharmony_ci	.right_margin =	24,
1118c2ecf20Sopenharmony_ci	.upper_margin =	32,
1128c2ecf20Sopenharmony_ci	.lower_margin =	11,
1138c2ecf20Sopenharmony_ci	.hsync_len =	96,
1148c2ecf20Sopenharmony_ci	.vsync_len =	2,
1158c2ecf20Sopenharmony_ci	.vmode =	FB_VMODE_NONINTERLACED
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/*
1198c2ecf20Sopenharmony_ci * PCI driver prototypes
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_cistatic int tdfxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id);
1228c2ecf20Sopenharmony_cistatic void tdfxfb_remove(struct pci_dev *pdev);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic const struct pci_device_id tdfxfb_id_table[] = {
1258c2ecf20Sopenharmony_ci	{ PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_BANSHEE,
1268c2ecf20Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
1278c2ecf20Sopenharmony_ci	  0xff0000, 0 },
1288c2ecf20Sopenharmony_ci	{ PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO3,
1298c2ecf20Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
1308c2ecf20Sopenharmony_ci	  0xff0000, 0 },
1318c2ecf20Sopenharmony_ci	{ PCI_VENDOR_ID_3DFX, PCI_DEVICE_ID_3DFX_VOODOO5,
1328c2ecf20Sopenharmony_ci	  PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
1338c2ecf20Sopenharmony_ci	  0xff0000, 0 },
1348c2ecf20Sopenharmony_ci	{ 0, }
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic struct pci_driver tdfxfb_driver = {
1388c2ecf20Sopenharmony_ci	.name		= "tdfxfb",
1398c2ecf20Sopenharmony_ci	.id_table	= tdfxfb_id_table,
1408c2ecf20Sopenharmony_ci	.probe		= tdfxfb_probe,
1418c2ecf20Sopenharmony_ci	.remove		= tdfxfb_remove,
1428c2ecf20Sopenharmony_ci};
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, tdfxfb_id_table);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/*
1478c2ecf20Sopenharmony_ci * Driver data
1488c2ecf20Sopenharmony_ci */
1498c2ecf20Sopenharmony_cistatic int nopan;
1508c2ecf20Sopenharmony_cistatic int nowrap = 1;      /* not implemented (yet) */
1518c2ecf20Sopenharmony_cistatic int hwcursor = 1;
1528c2ecf20Sopenharmony_cistatic char *mode_option;
1538c2ecf20Sopenharmony_cistatic bool nomtrr;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci/* -------------------------------------------------------------------------
1568c2ecf20Sopenharmony_ci *			Hardware-specific funcions
1578c2ecf20Sopenharmony_ci * ------------------------------------------------------------------------- */
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_cistatic inline u8 vga_inb(struct tdfx_par *par, u32 reg)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	return inb(par->iobase + reg - 0x300);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic inline void vga_outb(struct tdfx_par *par, u32 reg, u8 val)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	outb(val, par->iobase + reg - 0x300);
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic inline void gra_outb(struct tdfx_par *par, u32 idx, u8 val)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	vga_outb(par, GRA_I, idx);
1728c2ecf20Sopenharmony_ci	wmb();
1738c2ecf20Sopenharmony_ci	vga_outb(par, GRA_D, val);
1748c2ecf20Sopenharmony_ci	wmb();
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic inline void seq_outb(struct tdfx_par *par, u32 idx, u8 val)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	vga_outb(par, SEQ_I, idx);
1808c2ecf20Sopenharmony_ci	wmb();
1818c2ecf20Sopenharmony_ci	vga_outb(par, SEQ_D, val);
1828c2ecf20Sopenharmony_ci	wmb();
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic inline u8 seq_inb(struct tdfx_par *par, u32 idx)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	vga_outb(par, SEQ_I, idx);
1888c2ecf20Sopenharmony_ci	mb();
1898c2ecf20Sopenharmony_ci	return vga_inb(par, SEQ_D);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic inline void crt_outb(struct tdfx_par *par, u32 idx, u8 val)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	vga_outb(par, CRT_I, idx);
1958c2ecf20Sopenharmony_ci	wmb();
1968c2ecf20Sopenharmony_ci	vga_outb(par, CRT_D, val);
1978c2ecf20Sopenharmony_ci	wmb();
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic inline u8 crt_inb(struct tdfx_par *par, u32 idx)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	vga_outb(par, CRT_I, idx);
2038c2ecf20Sopenharmony_ci	mb();
2048c2ecf20Sopenharmony_ci	return vga_inb(par, CRT_D);
2058c2ecf20Sopenharmony_ci}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic inline void att_outb(struct tdfx_par *par, u32 idx, u8 val)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	unsigned char tmp;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	tmp = vga_inb(par, IS1_R);
2128c2ecf20Sopenharmony_ci	vga_outb(par, ATT_IW, idx);
2138c2ecf20Sopenharmony_ci	vga_outb(par, ATT_IW, val);
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic inline void vga_disable_video(struct tdfx_par *par)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	unsigned char s;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	s = seq_inb(par, 0x01) | 0x20;
2218c2ecf20Sopenharmony_ci	seq_outb(par, 0x00, 0x01);
2228c2ecf20Sopenharmony_ci	seq_outb(par, 0x01, s);
2238c2ecf20Sopenharmony_ci	seq_outb(par, 0x00, 0x03);
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic inline void vga_enable_video(struct tdfx_par *par)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	unsigned char s;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	s = seq_inb(par, 0x01) & 0xdf;
2318c2ecf20Sopenharmony_ci	seq_outb(par, 0x00, 0x01);
2328c2ecf20Sopenharmony_ci	seq_outb(par, 0x01, s);
2338c2ecf20Sopenharmony_ci	seq_outb(par, 0x00, 0x03);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic inline void vga_enable_palette(struct tdfx_par *par)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	vga_inb(par, IS1_R);
2398c2ecf20Sopenharmony_ci	mb();
2408c2ecf20Sopenharmony_ci	vga_outb(par, ATT_IW, 0x20);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic inline u32 tdfx_inl(struct tdfx_par *par, unsigned int reg)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	return readl(par->regbase_virt + reg);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_cistatic inline void tdfx_outl(struct tdfx_par *par, unsigned int reg, u32 val)
2498c2ecf20Sopenharmony_ci{
2508c2ecf20Sopenharmony_ci	writel(val, par->regbase_virt + reg);
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic inline void banshee_make_room(struct tdfx_par *par, int size)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	/* Note: The Voodoo3's onboard FIFO has 32 slots. This loop
2568c2ecf20Sopenharmony_ci	 * won't quit if you ask for more. */
2578c2ecf20Sopenharmony_ci	while ((tdfx_inl(par, STATUS) & 0x1f) < size - 1)
2588c2ecf20Sopenharmony_ci		cpu_relax();
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_cistatic int banshee_wait_idle(struct fb_info *info)
2628c2ecf20Sopenharmony_ci{
2638c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
2648c2ecf20Sopenharmony_ci	int i = 0;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	banshee_make_room(par, 1);
2678c2ecf20Sopenharmony_ci	tdfx_outl(par, COMMAND_3D, COMMAND_3D_NOP);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	do {
2708c2ecf20Sopenharmony_ci		if ((tdfx_inl(par, STATUS) & STATUS_BUSY) == 0)
2718c2ecf20Sopenharmony_ci			i++;
2728c2ecf20Sopenharmony_ci	} while (i < 3);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci/*
2788c2ecf20Sopenharmony_ci * Set the color of a palette entry in 8bpp mode
2798c2ecf20Sopenharmony_ci */
2808c2ecf20Sopenharmony_cistatic inline void do_setpalentry(struct tdfx_par *par, unsigned regno, u32 c)
2818c2ecf20Sopenharmony_ci{
2828c2ecf20Sopenharmony_ci	banshee_make_room(par, 2);
2838c2ecf20Sopenharmony_ci	tdfx_outl(par, DACADDR, regno);
2848c2ecf20Sopenharmony_ci	/* read after write makes it working */
2858c2ecf20Sopenharmony_ci	tdfx_inl(par, DACADDR);
2868c2ecf20Sopenharmony_ci	tdfx_outl(par, DACDATA, c);
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic u32 do_calc_pll(int freq, int *freq_out)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	int m, n, k, best_m, best_n, best_k, best_error;
2928c2ecf20Sopenharmony_ci	int fref = 14318;
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	best_error = freq;
2958c2ecf20Sopenharmony_ci	best_n = best_m = best_k = 0;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	for (k = 3; k >= 0; k--) {
2988c2ecf20Sopenharmony_ci		for (m = 63; m >= 0; m--) {
2998c2ecf20Sopenharmony_ci			/*
3008c2ecf20Sopenharmony_ci			 * Estimate value of n that produces target frequency
3018c2ecf20Sopenharmony_ci			 * with current m and k
3028c2ecf20Sopenharmony_ci			 */
3038c2ecf20Sopenharmony_ci			int n_estimated = ((freq * (m + 2) << k) / fref) - 2;
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci			/* Search neighborhood of estimated n */
3068c2ecf20Sopenharmony_ci			for (n = max(0, n_estimated);
3078c2ecf20Sopenharmony_ci				n <= min(255, n_estimated + 1);
3088c2ecf20Sopenharmony_ci				n++) {
3098c2ecf20Sopenharmony_ci				/*
3108c2ecf20Sopenharmony_ci				 * Calculate PLL freqency with current m, k and
3118c2ecf20Sopenharmony_ci				 * estimated n
3128c2ecf20Sopenharmony_ci				 */
3138c2ecf20Sopenharmony_ci				int f = (fref * (n + 2) / (m + 2)) >> k;
3148c2ecf20Sopenharmony_ci				int error = abs(f - freq);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci				/*
3178c2ecf20Sopenharmony_ci				 * If this is the closest we've come to the
3188c2ecf20Sopenharmony_ci				 * target frequency then remember n, m and k
3198c2ecf20Sopenharmony_ci				 */
3208c2ecf20Sopenharmony_ci				if (error < best_error) {
3218c2ecf20Sopenharmony_ci					best_error = error;
3228c2ecf20Sopenharmony_ci					best_n = n;
3238c2ecf20Sopenharmony_ci					best_m = m;
3248c2ecf20Sopenharmony_ci					best_k = k;
3258c2ecf20Sopenharmony_ci				}
3268c2ecf20Sopenharmony_ci			}
3278c2ecf20Sopenharmony_ci		}
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	n = best_n;
3318c2ecf20Sopenharmony_ci	m = best_m;
3328c2ecf20Sopenharmony_ci	k = best_k;
3338c2ecf20Sopenharmony_ci	*freq_out = (fref * (n + 2) / (m + 2)) >> k;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	return (n << 8) | (m << 2) | k;
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic void do_write_regs(struct fb_info *info, struct banshee_reg *reg)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
3418c2ecf20Sopenharmony_ci	int i;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	banshee_wait_idle(info);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	tdfx_outl(par, MISCINIT1, tdfx_inl(par, MISCINIT1) | 0x01);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	crt_outb(par, 0x11, crt_inb(par, 0x11) & 0x7f); /* CRT unprotect */
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	banshee_make_room(par, 3);
3508c2ecf20Sopenharmony_ci	tdfx_outl(par, VGAINIT1, reg->vgainit1 & 0x001FFFFF);
3518c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDPROCCFG, reg->vidcfg & ~0x00000001);
3528c2ecf20Sopenharmony_ci#if 0
3538c2ecf20Sopenharmony_ci	tdfx_outl(par, PLLCTRL1, reg->mempll);
3548c2ecf20Sopenharmony_ci	tdfx_outl(par, PLLCTRL2, reg->gfxpll);
3558c2ecf20Sopenharmony_ci#endif
3568c2ecf20Sopenharmony_ci	tdfx_outl(par, PLLCTRL0, reg->vidpll);
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	vga_outb(par, MISC_W, reg->misc[0x00] | 0x01);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	for (i = 0; i < 5; i++)
3618c2ecf20Sopenharmony_ci		seq_outb(par, i, reg->seq[i]);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	for (i = 0; i < 25; i++)
3648c2ecf20Sopenharmony_ci		crt_outb(par, i, reg->crt[i]);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	for (i = 0; i < 9; i++)
3678c2ecf20Sopenharmony_ci		gra_outb(par, i, reg->gra[i]);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	for (i = 0; i < 21; i++)
3708c2ecf20Sopenharmony_ci		att_outb(par, i, reg->att[i]);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	crt_outb(par, 0x1a, reg->ext[0]);
3738c2ecf20Sopenharmony_ci	crt_outb(par, 0x1b, reg->ext[1]);
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	vga_enable_palette(par);
3768c2ecf20Sopenharmony_ci	vga_enable_video(par);
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci	banshee_make_room(par, 9);
3798c2ecf20Sopenharmony_ci	tdfx_outl(par, VGAINIT0, reg->vgainit0);
3808c2ecf20Sopenharmony_ci	tdfx_outl(par, DACMODE, reg->dacmode);
3818c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDDESKSTRIDE, reg->stride);
3828c2ecf20Sopenharmony_ci	tdfx_outl(par, HWCURPATADDR, reg->curspataddr);
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDSCREENSIZE, reg->screensize);
3858c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDDESKSTART, reg->startaddr);
3868c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDPROCCFG, reg->vidcfg);
3878c2ecf20Sopenharmony_ci	tdfx_outl(par, VGAINIT1, reg->vgainit1);
3888c2ecf20Sopenharmony_ci	tdfx_outl(par, MISCINIT0, reg->miscinit0);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	banshee_make_room(par, 8);
3918c2ecf20Sopenharmony_ci	tdfx_outl(par, SRCBASE, reg->startaddr);
3928c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTBASE, reg->startaddr);
3938c2ecf20Sopenharmony_ci	tdfx_outl(par, COMMANDEXTRA_2D, 0);
3948c2ecf20Sopenharmony_ci	tdfx_outl(par, CLIP0MIN, 0);
3958c2ecf20Sopenharmony_ci	tdfx_outl(par, CLIP0MAX, 0x0fff0fff);
3968c2ecf20Sopenharmony_ci	tdfx_outl(par, CLIP1MIN, 0);
3978c2ecf20Sopenharmony_ci	tdfx_outl(par, CLIP1MAX, 0x0fff0fff);
3988c2ecf20Sopenharmony_ci	tdfx_outl(par, SRCXY, 0);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	banshee_wait_idle(info);
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic unsigned long do_lfb_size(struct tdfx_par *par, unsigned short dev_id)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	u32 draminit0 = tdfx_inl(par, DRAMINIT0);
4068c2ecf20Sopenharmony_ci	u32 draminit1 = tdfx_inl(par, DRAMINIT1);
4078c2ecf20Sopenharmony_ci	u32 miscinit1;
4088c2ecf20Sopenharmony_ci	int num_chips = (draminit0 & DRAMINIT0_SGRAM_NUM) ? 8 : 4;
4098c2ecf20Sopenharmony_ci	int chip_size; /* in MB */
4108c2ecf20Sopenharmony_ci	int has_sgram = draminit1 & DRAMINIT1_MEM_SDRAM;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	if (dev_id < PCI_DEVICE_ID_3DFX_VOODOO5) {
4138c2ecf20Sopenharmony_ci		/* Banshee/Voodoo3 */
4148c2ecf20Sopenharmony_ci		chip_size = 2;
4158c2ecf20Sopenharmony_ci		if (has_sgram && !(draminit0 & DRAMINIT0_SGRAM_TYPE))
4168c2ecf20Sopenharmony_ci			chip_size = 1;
4178c2ecf20Sopenharmony_ci	} else {
4188c2ecf20Sopenharmony_ci		/* Voodoo4/5 */
4198c2ecf20Sopenharmony_ci		has_sgram = 0;
4208c2ecf20Sopenharmony_ci		chip_size = draminit0 & DRAMINIT0_SGRAM_TYPE_MASK;
4218c2ecf20Sopenharmony_ci		chip_size = 1 << (chip_size >> DRAMINIT0_SGRAM_TYPE_SHIFT);
4228c2ecf20Sopenharmony_ci	}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci	/* disable block writes for SDRAM */
4258c2ecf20Sopenharmony_ci	miscinit1 = tdfx_inl(par, MISCINIT1);
4268c2ecf20Sopenharmony_ci	miscinit1 |= has_sgram ? 0 : MISCINIT1_2DBLOCK_DIS;
4278c2ecf20Sopenharmony_ci	miscinit1 |= MISCINIT1_CLUT_INV;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	banshee_make_room(par, 1);
4308c2ecf20Sopenharmony_ci	tdfx_outl(par, MISCINIT1, miscinit1);
4318c2ecf20Sopenharmony_ci	return num_chips * chip_size * 1024l * 1024;
4328c2ecf20Sopenharmony_ci}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------- */
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_cistatic int tdfxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
4398c2ecf20Sopenharmony_ci	u32 lpitch;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	if (var->bits_per_pixel != 8  && var->bits_per_pixel != 16 &&
4428c2ecf20Sopenharmony_ci	    var->bits_per_pixel != 24 && var->bits_per_pixel != 32) {
4438c2ecf20Sopenharmony_ci		DPRINTK("depth not supported: %u\n", var->bits_per_pixel);
4448c2ecf20Sopenharmony_ci		return -EINVAL;
4458c2ecf20Sopenharmony_ci	}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci	if (var->xres != var->xres_virtual)
4488c2ecf20Sopenharmony_ci		var->xres_virtual = var->xres;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (var->yres > var->yres_virtual)
4518c2ecf20Sopenharmony_ci		var->yres_virtual = var->yres;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	if (var->xoffset) {
4548c2ecf20Sopenharmony_ci		DPRINTK("xoffset not supported\n");
4558c2ecf20Sopenharmony_ci		return -EINVAL;
4568c2ecf20Sopenharmony_ci	}
4578c2ecf20Sopenharmony_ci	var->yoffset = 0;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	/*
4608c2ecf20Sopenharmony_ci	 * Banshee doesn't support interlace, but Voodoo4/5 and probably
4618c2ecf20Sopenharmony_ci	 * Voodoo3 do.
4628c2ecf20Sopenharmony_ci	 * no direct information about device id now?
4638c2ecf20Sopenharmony_ci	 *  use max_pixclock for this...
4648c2ecf20Sopenharmony_ci	 */
4658c2ecf20Sopenharmony_ci	if (((var->vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED) &&
4668c2ecf20Sopenharmony_ci	    (par->max_pixclock < VOODOO3_MAX_PIXCLOCK)) {
4678c2ecf20Sopenharmony_ci		DPRINTK("interlace not supported\n");
4688c2ecf20Sopenharmony_ci		return -EINVAL;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	if (info->monspecs.hfmax && info->monspecs.vfmax &&
4728c2ecf20Sopenharmony_ci	    info->monspecs.dclkmax && fb_validate_mode(var, info) < 0) {
4738c2ecf20Sopenharmony_ci		DPRINTK("mode outside monitor's specs\n");
4748c2ecf20Sopenharmony_ci		return -EINVAL;
4758c2ecf20Sopenharmony_ci	}
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	var->xres = (var->xres + 15) & ~15; /* could sometimes be 8 */
4788c2ecf20Sopenharmony_ci	lpitch = var->xres * ((var->bits_per_pixel + 7) >> 3);
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	if (var->xres < 320 || var->xres > 2048) {
4818c2ecf20Sopenharmony_ci		DPRINTK("width not supported: %u\n", var->xres);
4828c2ecf20Sopenharmony_ci		return -EINVAL;
4838c2ecf20Sopenharmony_ci	}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	if (var->yres < 200 || var->yres > 2048) {
4868c2ecf20Sopenharmony_ci		DPRINTK("height not supported: %u\n", var->yres);
4878c2ecf20Sopenharmony_ci		return -EINVAL;
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (lpitch * var->yres_virtual > info->fix.smem_len) {
4918c2ecf20Sopenharmony_ci		var->yres_virtual = info->fix.smem_len / lpitch;
4928c2ecf20Sopenharmony_ci		if (var->yres_virtual < var->yres) {
4938c2ecf20Sopenharmony_ci			DPRINTK("no memory for screen (%ux%ux%u)\n",
4948c2ecf20Sopenharmony_ci				var->xres, var->yres_virtual,
4958c2ecf20Sopenharmony_ci				var->bits_per_pixel);
4968c2ecf20Sopenharmony_ci			return -EINVAL;
4978c2ecf20Sopenharmony_ci		}
4988c2ecf20Sopenharmony_ci	}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	if (PICOS2KHZ(var->pixclock) > par->max_pixclock) {
5018c2ecf20Sopenharmony_ci		DPRINTK("pixclock too high (%ldKHz)\n",
5028c2ecf20Sopenharmony_ci			PICOS2KHZ(var->pixclock));
5038c2ecf20Sopenharmony_ci		return -EINVAL;
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	var->transp.offset = 0;
5078c2ecf20Sopenharmony_ci	var->transp.length = 0;
5088c2ecf20Sopenharmony_ci	switch (var->bits_per_pixel) {
5098c2ecf20Sopenharmony_ci	case 8:
5108c2ecf20Sopenharmony_ci		var->red.length = 8;
5118c2ecf20Sopenharmony_ci		var->red.offset = 0;
5128c2ecf20Sopenharmony_ci		var->green = var->red;
5138c2ecf20Sopenharmony_ci		var->blue = var->red;
5148c2ecf20Sopenharmony_ci		break;
5158c2ecf20Sopenharmony_ci	case 16:
5168c2ecf20Sopenharmony_ci		var->red.offset   = 11;
5178c2ecf20Sopenharmony_ci		var->red.length   = 5;
5188c2ecf20Sopenharmony_ci		var->green.offset = 5;
5198c2ecf20Sopenharmony_ci		var->green.length = 6;
5208c2ecf20Sopenharmony_ci		var->blue.offset  = 0;
5218c2ecf20Sopenharmony_ci		var->blue.length  = 5;
5228c2ecf20Sopenharmony_ci		break;
5238c2ecf20Sopenharmony_ci	case 32:
5248c2ecf20Sopenharmony_ci		var->transp.offset = 24;
5258c2ecf20Sopenharmony_ci		var->transp.length = 8;
5268c2ecf20Sopenharmony_ci		fallthrough;
5278c2ecf20Sopenharmony_ci	case 24:
5288c2ecf20Sopenharmony_ci		var->red.offset = 16;
5298c2ecf20Sopenharmony_ci		var->green.offset = 8;
5308c2ecf20Sopenharmony_ci		var->blue.offset = 0;
5318c2ecf20Sopenharmony_ci		var->red.length = var->green.length = var->blue.length = 8;
5328c2ecf20Sopenharmony_ci		break;
5338c2ecf20Sopenharmony_ci	}
5348c2ecf20Sopenharmony_ci	var->width = -1;
5358c2ecf20Sopenharmony_ci	var->height = -1;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	var->accel_flags = FB_ACCELF_TEXT;
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	DPRINTK("Checking graphics mode at %dx%d depth %d\n",
5408c2ecf20Sopenharmony_ci		var->xres, var->yres, var->bits_per_pixel);
5418c2ecf20Sopenharmony_ci	return 0;
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cistatic int tdfxfb_set_par(struct fb_info *info)
5458c2ecf20Sopenharmony_ci{
5468c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
5478c2ecf20Sopenharmony_ci	u32 hdispend = info->var.xres;
5488c2ecf20Sopenharmony_ci	u32 hsyncsta = hdispend + info->var.right_margin;
5498c2ecf20Sopenharmony_ci	u32 hsyncend = hsyncsta + info->var.hsync_len;
5508c2ecf20Sopenharmony_ci	u32 htotal   = hsyncend + info->var.left_margin;
5518c2ecf20Sopenharmony_ci	u32 hd, hs, he, ht, hbs, hbe;
5528c2ecf20Sopenharmony_ci	u32 vd, vs, ve, vt, vbs, vbe;
5538c2ecf20Sopenharmony_ci	struct banshee_reg reg;
5548c2ecf20Sopenharmony_ci	int fout, freq;
5558c2ecf20Sopenharmony_ci	u32 wd;
5568c2ecf20Sopenharmony_ci	u32 cpp = (info->var.bits_per_pixel + 7) >> 3;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	memset(&reg, 0, sizeof(reg));
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	reg.vidcfg = VIDCFG_VIDPROC_ENABLE | VIDCFG_DESK_ENABLE |
5618c2ecf20Sopenharmony_ci		     VIDCFG_CURS_X11 |
5628c2ecf20Sopenharmony_ci		     ((cpp - 1) << VIDCFG_PIXFMT_SHIFT) |
5638c2ecf20Sopenharmony_ci		     (cpp != 1 ? VIDCFG_CLUT_BYPASS : 0);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	/* PLL settings */
5668c2ecf20Sopenharmony_ci	freq = PICOS2KHZ(info->var.pixclock);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	reg.vidcfg &= ~VIDCFG_2X;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	if (freq > par->max_pixclock / 2) {
5718c2ecf20Sopenharmony_ci		freq = freq > par->max_pixclock ? par->max_pixclock : freq;
5728c2ecf20Sopenharmony_ci		reg.dacmode |= DACMODE_2X;
5738c2ecf20Sopenharmony_ci		reg.vidcfg  |= VIDCFG_2X;
5748c2ecf20Sopenharmony_ci		hdispend >>= 1;
5758c2ecf20Sopenharmony_ci		hsyncsta >>= 1;
5768c2ecf20Sopenharmony_ci		hsyncend >>= 1;
5778c2ecf20Sopenharmony_ci		htotal   >>= 1;
5788c2ecf20Sopenharmony_ci	}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci	wd = (hdispend >> 3) - 1;
5818c2ecf20Sopenharmony_ci	hd  = wd;
5828c2ecf20Sopenharmony_ci	hs  = (hsyncsta >> 3) - 1;
5838c2ecf20Sopenharmony_ci	he  = (hsyncend >> 3) - 1;
5848c2ecf20Sopenharmony_ci	ht  = (htotal >> 3) - 1;
5858c2ecf20Sopenharmony_ci	hbs = hd;
5868c2ecf20Sopenharmony_ci	hbe = ht;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_DOUBLE) {
5898c2ecf20Sopenharmony_ci		vd = (info->var.yres << 1) - 1;
5908c2ecf20Sopenharmony_ci		vs  = vd + (info->var.lower_margin << 1);
5918c2ecf20Sopenharmony_ci		ve  = vs + (info->var.vsync_len << 1);
5928c2ecf20Sopenharmony_ci		vt = ve + (info->var.upper_margin << 1) - 1;
5938c2ecf20Sopenharmony_ci		reg.screensize = info->var.xres | (info->var.yres << 13);
5948c2ecf20Sopenharmony_ci		reg.vidcfg |= VIDCFG_HALF_MODE;
5958c2ecf20Sopenharmony_ci		reg.crt[0x09] = 0x80;
5968c2ecf20Sopenharmony_ci	} else {
5978c2ecf20Sopenharmony_ci		vd = info->var.yres - 1;
5988c2ecf20Sopenharmony_ci		vs  = vd + info->var.lower_margin;
5998c2ecf20Sopenharmony_ci		ve  = vs + info->var.vsync_len;
6008c2ecf20Sopenharmony_ci		vt = ve + info->var.upper_margin - 1;
6018c2ecf20Sopenharmony_ci		reg.screensize = info->var.xres | (info->var.yres << 12);
6028c2ecf20Sopenharmony_ci		reg.vidcfg &= ~VIDCFG_HALF_MODE;
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci	vbs = vd;
6058c2ecf20Sopenharmony_ci	vbe = vt;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* this is all pretty standard VGA register stuffing */
6088c2ecf20Sopenharmony_ci	reg.misc[0x00] = 0x0f |
6098c2ecf20Sopenharmony_ci			(info->var.xres < 400 ? 0xa0 :
6108c2ecf20Sopenharmony_ci			 info->var.xres < 480 ? 0x60 :
6118c2ecf20Sopenharmony_ci			 info->var.xres < 768 ? 0xe0 : 0x20);
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	reg.gra[0x05] = 0x40;
6148c2ecf20Sopenharmony_ci	reg.gra[0x06] = 0x05;
6158c2ecf20Sopenharmony_ci	reg.gra[0x07] = 0x0f;
6168c2ecf20Sopenharmony_ci	reg.gra[0x08] = 0xff;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	reg.att[0x00] = 0x00;
6198c2ecf20Sopenharmony_ci	reg.att[0x01] = 0x01;
6208c2ecf20Sopenharmony_ci	reg.att[0x02] = 0x02;
6218c2ecf20Sopenharmony_ci	reg.att[0x03] = 0x03;
6228c2ecf20Sopenharmony_ci	reg.att[0x04] = 0x04;
6238c2ecf20Sopenharmony_ci	reg.att[0x05] = 0x05;
6248c2ecf20Sopenharmony_ci	reg.att[0x06] = 0x06;
6258c2ecf20Sopenharmony_ci	reg.att[0x07] = 0x07;
6268c2ecf20Sopenharmony_ci	reg.att[0x08] = 0x08;
6278c2ecf20Sopenharmony_ci	reg.att[0x09] = 0x09;
6288c2ecf20Sopenharmony_ci	reg.att[0x0a] = 0x0a;
6298c2ecf20Sopenharmony_ci	reg.att[0x0b] = 0x0b;
6308c2ecf20Sopenharmony_ci	reg.att[0x0c] = 0x0c;
6318c2ecf20Sopenharmony_ci	reg.att[0x0d] = 0x0d;
6328c2ecf20Sopenharmony_ci	reg.att[0x0e] = 0x0e;
6338c2ecf20Sopenharmony_ci	reg.att[0x0f] = 0x0f;
6348c2ecf20Sopenharmony_ci	reg.att[0x10] = 0x41;
6358c2ecf20Sopenharmony_ci	reg.att[0x12] = 0x0f;
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	reg.seq[0x00] = 0x03;
6388c2ecf20Sopenharmony_ci	reg.seq[0x01] = 0x01; /* fixme: clkdiv2? */
6398c2ecf20Sopenharmony_ci	reg.seq[0x02] = 0x0f;
6408c2ecf20Sopenharmony_ci	reg.seq[0x03] = 0x00;
6418c2ecf20Sopenharmony_ci	reg.seq[0x04] = 0x0e;
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	reg.crt[0x00] = ht - 4;
6448c2ecf20Sopenharmony_ci	reg.crt[0x01] = hd;
6458c2ecf20Sopenharmony_ci	reg.crt[0x02] = hbs;
6468c2ecf20Sopenharmony_ci	reg.crt[0x03] = 0x80 | (hbe & 0x1f);
6478c2ecf20Sopenharmony_ci	reg.crt[0x04] = hs;
6488c2ecf20Sopenharmony_ci	reg.crt[0x05] = ((hbe & 0x20) << 2) | (he & 0x1f);
6498c2ecf20Sopenharmony_ci	reg.crt[0x06] = vt;
6508c2ecf20Sopenharmony_ci	reg.crt[0x07] = ((vs & 0x200) >> 2) |
6518c2ecf20Sopenharmony_ci			((vd & 0x200) >> 3) |
6528c2ecf20Sopenharmony_ci			((vt & 0x200) >> 4) | 0x10 |
6538c2ecf20Sopenharmony_ci			((vbs & 0x100) >> 5) |
6548c2ecf20Sopenharmony_ci			((vs & 0x100) >> 6) |
6558c2ecf20Sopenharmony_ci			((vd & 0x100) >> 7) |
6568c2ecf20Sopenharmony_ci			((vt & 0x100) >> 8);
6578c2ecf20Sopenharmony_ci	reg.crt[0x09] |= 0x40 | ((vbs & 0x200) >> 4);
6588c2ecf20Sopenharmony_ci	reg.crt[0x10] = vs;
6598c2ecf20Sopenharmony_ci	reg.crt[0x11] = (ve & 0x0f) | 0x20;
6608c2ecf20Sopenharmony_ci	reg.crt[0x12] = vd;
6618c2ecf20Sopenharmony_ci	reg.crt[0x13] = wd;
6628c2ecf20Sopenharmony_ci	reg.crt[0x15] = vbs;
6638c2ecf20Sopenharmony_ci	reg.crt[0x16] = vbe + 1;
6648c2ecf20Sopenharmony_ci	reg.crt[0x17] = 0xc3;
6658c2ecf20Sopenharmony_ci	reg.crt[0x18] = 0xff;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	/* Banshee's nonvga stuff */
6688c2ecf20Sopenharmony_ci	reg.ext[0x00] = (((ht & 0x100) >> 8) |
6698c2ecf20Sopenharmony_ci			((hd & 0x100) >> 6) |
6708c2ecf20Sopenharmony_ci			((hbs & 0x100) >> 4) |
6718c2ecf20Sopenharmony_ci			((hbe & 0x40) >> 1) |
6728c2ecf20Sopenharmony_ci			((hs & 0x100) >> 2) |
6738c2ecf20Sopenharmony_ci			((he & 0x20) << 2));
6748c2ecf20Sopenharmony_ci	reg.ext[0x01] = (((vt & 0x400) >> 10) |
6758c2ecf20Sopenharmony_ci			((vd & 0x400) >> 8) |
6768c2ecf20Sopenharmony_ci			((vbs & 0x400) >> 6) |
6778c2ecf20Sopenharmony_ci			((vbe & 0x400) >> 4));
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	reg.vgainit0 =	VGAINIT0_8BIT_DAC     |
6808c2ecf20Sopenharmony_ci			VGAINIT0_EXT_ENABLE   |
6818c2ecf20Sopenharmony_ci			VGAINIT0_WAKEUP_3C3   |
6828c2ecf20Sopenharmony_ci			VGAINIT0_ALT_READBACK |
6838c2ecf20Sopenharmony_ci			VGAINIT0_EXTSHIFTOUT;
6848c2ecf20Sopenharmony_ci	reg.vgainit1 = tdfx_inl(par, VGAINIT1) & 0x1fffff;
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	if (hwcursor)
6878c2ecf20Sopenharmony_ci		reg.curspataddr = info->fix.smem_len;
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	reg.cursloc   = 0;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	reg.cursc0    = 0;
6928c2ecf20Sopenharmony_ci	reg.cursc1    = 0xffffff;
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	reg.stride    = info->var.xres * cpp;
6958c2ecf20Sopenharmony_ci	reg.startaddr = info->var.yoffset * reg.stride
6968c2ecf20Sopenharmony_ci			+ info->var.xoffset * cpp;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	reg.vidpll = do_calc_pll(freq, &fout);
6998c2ecf20Sopenharmony_ci#if 0
7008c2ecf20Sopenharmony_ci	reg.mempll = do_calc_pll(..., &fout);
7018c2ecf20Sopenharmony_ci	reg.gfxpll = do_calc_pll(..., &fout);
7028c2ecf20Sopenharmony_ci#endif
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	if ((info->var.vmode & FB_VMODE_MASK) == FB_VMODE_INTERLACED)
7058c2ecf20Sopenharmony_ci		reg.vidcfg |= VIDCFG_INTERLACE;
7068c2ecf20Sopenharmony_ci	reg.miscinit0 = tdfx_inl(par, MISCINIT0);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci#if defined(__BIG_ENDIAN)
7098c2ecf20Sopenharmony_ci	switch (info->var.bits_per_pixel) {
7108c2ecf20Sopenharmony_ci	case 8:
7118c2ecf20Sopenharmony_ci	case 24:
7128c2ecf20Sopenharmony_ci		reg.miscinit0 &= ~(1 << 30);
7138c2ecf20Sopenharmony_ci		reg.miscinit0 &= ~(1 << 31);
7148c2ecf20Sopenharmony_ci		break;
7158c2ecf20Sopenharmony_ci	case 16:
7168c2ecf20Sopenharmony_ci		reg.miscinit0 |= (1 << 30);
7178c2ecf20Sopenharmony_ci		reg.miscinit0 |= (1 << 31);
7188c2ecf20Sopenharmony_ci		break;
7198c2ecf20Sopenharmony_ci	case 32:
7208c2ecf20Sopenharmony_ci		reg.miscinit0 |= (1 << 30);
7218c2ecf20Sopenharmony_ci		reg.miscinit0 &= ~(1 << 31);
7228c2ecf20Sopenharmony_ci		break;
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci#endif
7258c2ecf20Sopenharmony_ci	do_write_regs(info, &reg);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	/* Now change fb_fix_screeninfo according to changes in par */
7288c2ecf20Sopenharmony_ci	info->fix.line_length = reg.stride;
7298c2ecf20Sopenharmony_ci	info->fix.visual = (info->var.bits_per_pixel == 8)
7308c2ecf20Sopenharmony_ci				? FB_VISUAL_PSEUDOCOLOR
7318c2ecf20Sopenharmony_ci				: FB_VISUAL_TRUECOLOR;
7328c2ecf20Sopenharmony_ci	DPRINTK("Graphics mode is now set at %dx%d depth %d\n",
7338c2ecf20Sopenharmony_ci		info->var.xres, info->var.yres, info->var.bits_per_pixel);
7348c2ecf20Sopenharmony_ci	return 0;
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci/* A handy macro shamelessly pinched from matroxfb */
7388c2ecf20Sopenharmony_ci#define CNVT_TOHW(val, width) ((((val) << (width)) + 0x7FFF - (val)) >> 16)
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_cistatic int tdfxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
7418c2ecf20Sopenharmony_ci			    unsigned blue, unsigned transp,
7428c2ecf20Sopenharmony_ci			    struct fb_info *info)
7438c2ecf20Sopenharmony_ci{
7448c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
7458c2ecf20Sopenharmony_ci	u32 rgbcol;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	if (regno >= info->cmap.len || regno > 255)
7488c2ecf20Sopenharmony_ci		return 1;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	/* grayscale works only partially under directcolor */
7518c2ecf20Sopenharmony_ci	if (info->var.grayscale) {
7528c2ecf20Sopenharmony_ci		/* grayscale = 0.30*R + 0.59*G + 0.11*B */
7538c2ecf20Sopenharmony_ci		blue = (red * 77 + green * 151 + blue * 28) >> 8;
7548c2ecf20Sopenharmony_ci		green = blue;
7558c2ecf20Sopenharmony_ci		red = blue;
7568c2ecf20Sopenharmony_ci	}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	switch (info->fix.visual) {
7598c2ecf20Sopenharmony_ci	case FB_VISUAL_PSEUDOCOLOR:
7608c2ecf20Sopenharmony_ci		rgbcol = (((u32)red   & 0xff00) << 8) |
7618c2ecf20Sopenharmony_ci			 (((u32)green & 0xff00) << 0) |
7628c2ecf20Sopenharmony_ci			 (((u32)blue  & 0xff00) >> 8);
7638c2ecf20Sopenharmony_ci		do_setpalentry(par, regno, rgbcol);
7648c2ecf20Sopenharmony_ci		break;
7658c2ecf20Sopenharmony_ci	/* Truecolor has no hardware color palettes. */
7668c2ecf20Sopenharmony_ci	case FB_VISUAL_TRUECOLOR:
7678c2ecf20Sopenharmony_ci		if (regno < 16) {
7688c2ecf20Sopenharmony_ci			rgbcol = (CNVT_TOHW(red, info->var.red.length) <<
7698c2ecf20Sopenharmony_ci				  info->var.red.offset) |
7708c2ecf20Sopenharmony_ci				(CNVT_TOHW(green, info->var.green.length) <<
7718c2ecf20Sopenharmony_ci				 info->var.green.offset) |
7728c2ecf20Sopenharmony_ci				(CNVT_TOHW(blue, info->var.blue.length) <<
7738c2ecf20Sopenharmony_ci				 info->var.blue.offset) |
7748c2ecf20Sopenharmony_ci				(CNVT_TOHW(transp, info->var.transp.length) <<
7758c2ecf20Sopenharmony_ci				 info->var.transp.offset);
7768c2ecf20Sopenharmony_ci			par->palette[regno] = rgbcol;
7778c2ecf20Sopenharmony_ci		}
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci		break;
7808c2ecf20Sopenharmony_ci	default:
7818c2ecf20Sopenharmony_ci		DPRINTK("bad depth %u\n", info->var.bits_per_pixel);
7828c2ecf20Sopenharmony_ci		break;
7838c2ecf20Sopenharmony_ci	}
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	return 0;
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
7898c2ecf20Sopenharmony_cistatic int tdfxfb_blank(int blank, struct fb_info *info)
7908c2ecf20Sopenharmony_ci{
7918c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
7928c2ecf20Sopenharmony_ci	int vgablank = 1;
7938c2ecf20Sopenharmony_ci	u32 dacmode = tdfx_inl(par, DACMODE);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	dacmode &= ~(BIT(1) | BIT(3));
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	switch (blank) {
7988c2ecf20Sopenharmony_ci	case FB_BLANK_UNBLANK: /* Screen: On; HSync: On, VSync: On */
7998c2ecf20Sopenharmony_ci		vgablank = 0;
8008c2ecf20Sopenharmony_ci		break;
8018c2ecf20Sopenharmony_ci	case FB_BLANK_NORMAL: /* Screen: Off; HSync: On, VSync: On */
8028c2ecf20Sopenharmony_ci		break;
8038c2ecf20Sopenharmony_ci	case FB_BLANK_VSYNC_SUSPEND: /* Screen: Off; HSync: On, VSync: Off */
8048c2ecf20Sopenharmony_ci		dacmode |= BIT(3);
8058c2ecf20Sopenharmony_ci		break;
8068c2ecf20Sopenharmony_ci	case FB_BLANK_HSYNC_SUSPEND: /* Screen: Off; HSync: Off, VSync: On */
8078c2ecf20Sopenharmony_ci		dacmode |= BIT(1);
8088c2ecf20Sopenharmony_ci		break;
8098c2ecf20Sopenharmony_ci	case FB_BLANK_POWERDOWN: /* Screen: Off; HSync: Off, VSync: Off */
8108c2ecf20Sopenharmony_ci		dacmode |= BIT(1) | BIT(3);
8118c2ecf20Sopenharmony_ci		break;
8128c2ecf20Sopenharmony_ci	}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	banshee_make_room(par, 1);
8158c2ecf20Sopenharmony_ci	tdfx_outl(par, DACMODE, dacmode);
8168c2ecf20Sopenharmony_ci	if (vgablank)
8178c2ecf20Sopenharmony_ci		vga_disable_video(par);
8188c2ecf20Sopenharmony_ci	else
8198c2ecf20Sopenharmony_ci		vga_enable_video(par);
8208c2ecf20Sopenharmony_ci	return 0;
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci/*
8248c2ecf20Sopenharmony_ci * Set the starting position of the visible screen to var->yoffset
8258c2ecf20Sopenharmony_ci */
8268c2ecf20Sopenharmony_cistatic int tdfxfb_pan_display(struct fb_var_screeninfo *var,
8278c2ecf20Sopenharmony_ci			      struct fb_info *info)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
8308c2ecf20Sopenharmony_ci	u32 addr = var->yoffset * info->fix.line_length;
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	if (nopan || var->xoffset)
8338c2ecf20Sopenharmony_ci		return -EINVAL;
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	banshee_make_room(par, 1);
8368c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDDESKSTART, addr);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	return 0;
8398c2ecf20Sopenharmony_ci}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci#ifdef CONFIG_FB_3DFX_ACCEL
8428c2ecf20Sopenharmony_ci/*
8438c2ecf20Sopenharmony_ci * FillRect 2D command (solidfill or invert (via ROP_XOR))
8448c2ecf20Sopenharmony_ci */
8458c2ecf20Sopenharmony_cistatic void tdfxfb_fillrect(struct fb_info *info,
8468c2ecf20Sopenharmony_ci			    const struct fb_fillrect *rect)
8478c2ecf20Sopenharmony_ci{
8488c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
8498c2ecf20Sopenharmony_ci	u32 bpp = info->var.bits_per_pixel;
8508c2ecf20Sopenharmony_ci	u32 stride = info->fix.line_length;
8518c2ecf20Sopenharmony_ci	u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
8528c2ecf20Sopenharmony_ci	int tdfx_rop;
8538c2ecf20Sopenharmony_ci	u32 dx = rect->dx;
8548c2ecf20Sopenharmony_ci	u32 dy = rect->dy;
8558c2ecf20Sopenharmony_ci	u32 dstbase = 0;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	if (rect->rop == ROP_COPY)
8588c2ecf20Sopenharmony_ci		tdfx_rop = TDFX_ROP_COPY;
8598c2ecf20Sopenharmony_ci	else
8608c2ecf20Sopenharmony_ci		tdfx_rop = TDFX_ROP_XOR;
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	/* assume always rect->height < 4096 */
8638c2ecf20Sopenharmony_ci	if (dy + rect->height > 4095) {
8648c2ecf20Sopenharmony_ci		dstbase = stride * dy;
8658c2ecf20Sopenharmony_ci		dy = 0;
8668c2ecf20Sopenharmony_ci	}
8678c2ecf20Sopenharmony_ci	/* assume always rect->width < 4096 */
8688c2ecf20Sopenharmony_ci	if (dx + rect->width > 4095) {
8698c2ecf20Sopenharmony_ci		dstbase += dx * bpp >> 3;
8708c2ecf20Sopenharmony_ci		dx = 0;
8718c2ecf20Sopenharmony_ci	}
8728c2ecf20Sopenharmony_ci	banshee_make_room(par, 6);
8738c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTFORMAT, fmt);
8748c2ecf20Sopenharmony_ci	if (info->fix.visual == FB_VISUAL_PSEUDOCOLOR) {
8758c2ecf20Sopenharmony_ci		tdfx_outl(par, COLORFORE, rect->color);
8768c2ecf20Sopenharmony_ci	} else { /* FB_VISUAL_TRUECOLOR */
8778c2ecf20Sopenharmony_ci		tdfx_outl(par, COLORFORE, par->palette[rect->color]);
8788c2ecf20Sopenharmony_ci	}
8798c2ecf20Sopenharmony_ci	tdfx_outl(par, COMMAND_2D, COMMAND_2D_FILLRECT | (tdfx_rop << 24));
8808c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTBASE, dstbase);
8818c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTSIZE, rect->width | (rect->height << 16));
8828c2ecf20Sopenharmony_ci	tdfx_outl(par, LAUNCH_2D, dx | (dy << 16));
8838c2ecf20Sopenharmony_ci}
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci/*
8868c2ecf20Sopenharmony_ci * Screen-to-Screen BitBlt 2D command (for the bmove fb op.)
8878c2ecf20Sopenharmony_ci */
8888c2ecf20Sopenharmony_cistatic void tdfxfb_copyarea(struct fb_info *info,
8898c2ecf20Sopenharmony_ci			    const struct fb_copyarea *area)
8908c2ecf20Sopenharmony_ci{
8918c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
8928c2ecf20Sopenharmony_ci	u32 sx = area->sx, sy = area->sy, dx = area->dx, dy = area->dy;
8938c2ecf20Sopenharmony_ci	u32 bpp = info->var.bits_per_pixel;
8948c2ecf20Sopenharmony_ci	u32 stride = info->fix.line_length;
8958c2ecf20Sopenharmony_ci	u32 blitcmd = COMMAND_2D_S2S_BITBLT | (TDFX_ROP_COPY << 24);
8968c2ecf20Sopenharmony_ci	u32 fmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
8978c2ecf20Sopenharmony_ci	u32 dstbase = 0;
8988c2ecf20Sopenharmony_ci	u32 srcbase = 0;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	/* assume always area->height < 4096 */
9018c2ecf20Sopenharmony_ci	if (sy + area->height > 4095) {
9028c2ecf20Sopenharmony_ci		srcbase = stride * sy;
9038c2ecf20Sopenharmony_ci		sy = 0;
9048c2ecf20Sopenharmony_ci	}
9058c2ecf20Sopenharmony_ci	/* assume always area->width < 4096 */
9068c2ecf20Sopenharmony_ci	if (sx + area->width > 4095) {
9078c2ecf20Sopenharmony_ci		srcbase += sx * bpp >> 3;
9088c2ecf20Sopenharmony_ci		sx = 0;
9098c2ecf20Sopenharmony_ci	}
9108c2ecf20Sopenharmony_ci	/* assume always area->height < 4096 */
9118c2ecf20Sopenharmony_ci	if (dy + area->height > 4095) {
9128c2ecf20Sopenharmony_ci		dstbase = stride * dy;
9138c2ecf20Sopenharmony_ci		dy = 0;
9148c2ecf20Sopenharmony_ci	}
9158c2ecf20Sopenharmony_ci	/* assume always area->width < 4096 */
9168c2ecf20Sopenharmony_ci	if (dx + area->width > 4095) {
9178c2ecf20Sopenharmony_ci		dstbase += dx * bpp >> 3;
9188c2ecf20Sopenharmony_ci		dx = 0;
9198c2ecf20Sopenharmony_ci	}
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	if (area->sx <= area->dx) {
9228c2ecf20Sopenharmony_ci		/* -X */
9238c2ecf20Sopenharmony_ci		blitcmd |= BIT(14);
9248c2ecf20Sopenharmony_ci		sx += area->width - 1;
9258c2ecf20Sopenharmony_ci		dx += area->width - 1;
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci	if (area->sy <= area->dy) {
9288c2ecf20Sopenharmony_ci		/* -Y */
9298c2ecf20Sopenharmony_ci		blitcmd |= BIT(15);
9308c2ecf20Sopenharmony_ci		sy += area->height - 1;
9318c2ecf20Sopenharmony_ci		dy += area->height - 1;
9328c2ecf20Sopenharmony_ci	}
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	banshee_make_room(par, 8);
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	tdfx_outl(par, SRCFORMAT, fmt);
9378c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTFORMAT, fmt);
9388c2ecf20Sopenharmony_ci	tdfx_outl(par, COMMAND_2D, blitcmd);
9398c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTSIZE, area->width | (area->height << 16));
9408c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTXY, dx | (dy << 16));
9418c2ecf20Sopenharmony_ci	tdfx_outl(par, SRCBASE, srcbase);
9428c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTBASE, dstbase);
9438c2ecf20Sopenharmony_ci	tdfx_outl(par, LAUNCH_2D, sx | (sy << 16));
9448c2ecf20Sopenharmony_ci}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_cistatic void tdfxfb_imageblit(struct fb_info *info, const struct fb_image *image)
9478c2ecf20Sopenharmony_ci{
9488c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
9498c2ecf20Sopenharmony_ci	int size = image->height * ((image->width * image->depth + 7) >> 3);
9508c2ecf20Sopenharmony_ci	int fifo_free;
9518c2ecf20Sopenharmony_ci	int i, stride = info->fix.line_length;
9528c2ecf20Sopenharmony_ci	u32 bpp = info->var.bits_per_pixel;
9538c2ecf20Sopenharmony_ci	u32 dstfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13);
9548c2ecf20Sopenharmony_ci	u8 *chardata = (u8 *) image->data;
9558c2ecf20Sopenharmony_ci	u32 srcfmt;
9568c2ecf20Sopenharmony_ci	u32 dx = image->dx;
9578c2ecf20Sopenharmony_ci	u32 dy = image->dy;
9588c2ecf20Sopenharmony_ci	u32 dstbase = 0;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	if (image->depth != 1) {
9618c2ecf20Sopenharmony_ci#ifdef BROKEN_CODE
9628c2ecf20Sopenharmony_ci		banshee_make_room(par, 6 + ((size + 3) >> 2));
9638c2ecf20Sopenharmony_ci		srcfmt = stride | ((bpp + ((bpp == 8) ? 0 : 8)) << 13) |
9648c2ecf20Sopenharmony_ci			0x400000;
9658c2ecf20Sopenharmony_ci#else
9668c2ecf20Sopenharmony_ci		cfb_imageblit(info, image);
9678c2ecf20Sopenharmony_ci#endif
9688c2ecf20Sopenharmony_ci		return;
9698c2ecf20Sopenharmony_ci	}
9708c2ecf20Sopenharmony_ci	banshee_make_room(par, 9);
9718c2ecf20Sopenharmony_ci	switch (info->fix.visual) {
9728c2ecf20Sopenharmony_ci	case FB_VISUAL_PSEUDOCOLOR:
9738c2ecf20Sopenharmony_ci		tdfx_outl(par, COLORFORE, image->fg_color);
9748c2ecf20Sopenharmony_ci		tdfx_outl(par, COLORBACK, image->bg_color);
9758c2ecf20Sopenharmony_ci		break;
9768c2ecf20Sopenharmony_ci	case FB_VISUAL_TRUECOLOR:
9778c2ecf20Sopenharmony_ci	default:
9788c2ecf20Sopenharmony_ci		tdfx_outl(par, COLORFORE,
9798c2ecf20Sopenharmony_ci			  par->palette[image->fg_color]);
9808c2ecf20Sopenharmony_ci		tdfx_outl(par, COLORBACK,
9818c2ecf20Sopenharmony_ci			  par->palette[image->bg_color]);
9828c2ecf20Sopenharmony_ci	}
9838c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN
9848c2ecf20Sopenharmony_ci	srcfmt = 0x400000 | BIT(20);
9858c2ecf20Sopenharmony_ci#else
9868c2ecf20Sopenharmony_ci	srcfmt = 0x400000;
9878c2ecf20Sopenharmony_ci#endif
9888c2ecf20Sopenharmony_ci	/* assume always image->height < 4096 */
9898c2ecf20Sopenharmony_ci	if (dy + image->height > 4095) {
9908c2ecf20Sopenharmony_ci		dstbase = stride * dy;
9918c2ecf20Sopenharmony_ci		dy = 0;
9928c2ecf20Sopenharmony_ci	}
9938c2ecf20Sopenharmony_ci	/* assume always image->width < 4096 */
9948c2ecf20Sopenharmony_ci	if (dx + image->width > 4095) {
9958c2ecf20Sopenharmony_ci		dstbase += dx * bpp >> 3;
9968c2ecf20Sopenharmony_ci		dx = 0;
9978c2ecf20Sopenharmony_ci	}
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTBASE, dstbase);
10008c2ecf20Sopenharmony_ci	tdfx_outl(par, SRCXY, 0);
10018c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTXY, dx | (dy << 16));
10028c2ecf20Sopenharmony_ci	tdfx_outl(par, COMMAND_2D,
10038c2ecf20Sopenharmony_ci		  COMMAND_2D_H2S_BITBLT | (TDFX_ROP_COPY << 24));
10048c2ecf20Sopenharmony_ci	tdfx_outl(par, SRCFORMAT, srcfmt);
10058c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTFORMAT, dstfmt);
10068c2ecf20Sopenharmony_ci	tdfx_outl(par, DSTSIZE, image->width | (image->height << 16));
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	/* A count of how many free FIFO entries we've requested.
10098c2ecf20Sopenharmony_ci	 * When this goes negative, we need to request more. */
10108c2ecf20Sopenharmony_ci	fifo_free = 0;
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	/* Send four bytes at a time of data */
10138c2ecf20Sopenharmony_ci	for (i = (size >> 2); i > 0; i--) {
10148c2ecf20Sopenharmony_ci		if (--fifo_free < 0) {
10158c2ecf20Sopenharmony_ci			fifo_free = 31;
10168c2ecf20Sopenharmony_ci			banshee_make_room(par, fifo_free);
10178c2ecf20Sopenharmony_ci		}
10188c2ecf20Sopenharmony_ci		tdfx_outl(par, LAUNCH_2D, *(u32 *)chardata);
10198c2ecf20Sopenharmony_ci		chardata += 4;
10208c2ecf20Sopenharmony_ci	}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	/* Send the leftovers now */
10238c2ecf20Sopenharmony_ci	banshee_make_room(par, 3);
10248c2ecf20Sopenharmony_ci	switch (size % 4) {
10258c2ecf20Sopenharmony_ci	case 0:
10268c2ecf20Sopenharmony_ci		break;
10278c2ecf20Sopenharmony_ci	case 1:
10288c2ecf20Sopenharmony_ci		tdfx_outl(par, LAUNCH_2D, *chardata);
10298c2ecf20Sopenharmony_ci		break;
10308c2ecf20Sopenharmony_ci	case 2:
10318c2ecf20Sopenharmony_ci		tdfx_outl(par, LAUNCH_2D, *(u16 *)chardata);
10328c2ecf20Sopenharmony_ci		break;
10338c2ecf20Sopenharmony_ci	case 3:
10348c2ecf20Sopenharmony_ci		tdfx_outl(par, LAUNCH_2D,
10358c2ecf20Sopenharmony_ci			*(u16 *)chardata | (chardata[3] << 24));
10368c2ecf20Sopenharmony_ci		break;
10378c2ecf20Sopenharmony_ci	}
10388c2ecf20Sopenharmony_ci}
10398c2ecf20Sopenharmony_ci#endif /* CONFIG_FB_3DFX_ACCEL */
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_cistatic int tdfxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
10428c2ecf20Sopenharmony_ci{
10438c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
10448c2ecf20Sopenharmony_ci	u32 vidcfg;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	if (!hwcursor)
10478c2ecf20Sopenharmony_ci		return -EINVAL;	/* just to force soft_cursor() call */
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	/* Too large of a cursor or wrong bpp :-( */
10508c2ecf20Sopenharmony_ci	if (cursor->image.width > 64 ||
10518c2ecf20Sopenharmony_ci	    cursor->image.height > 64 ||
10528c2ecf20Sopenharmony_ci	    cursor->image.depth > 1)
10538c2ecf20Sopenharmony_ci		return -EINVAL;
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_ci	vidcfg = tdfx_inl(par, VIDPROCCFG);
10568c2ecf20Sopenharmony_ci	if (cursor->enable)
10578c2ecf20Sopenharmony_ci		tdfx_outl(par, VIDPROCCFG, vidcfg | VIDCFG_HWCURSOR_ENABLE);
10588c2ecf20Sopenharmony_ci	else
10598c2ecf20Sopenharmony_ci		tdfx_outl(par, VIDPROCCFG, vidcfg & ~VIDCFG_HWCURSOR_ENABLE);
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	/*
10628c2ecf20Sopenharmony_ci	 * If the cursor is not be changed this means either we want the
10638c2ecf20Sopenharmony_ci	 * current cursor state (if enable is set) or we want to query what
10648c2ecf20Sopenharmony_ci	 * we can do with the cursor (if enable is not set)
10658c2ecf20Sopenharmony_ci	 */
10668c2ecf20Sopenharmony_ci	if (!cursor->set)
10678c2ecf20Sopenharmony_ci		return 0;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	/* fix cursor color - XFree86 forgets to restore it properly */
10708c2ecf20Sopenharmony_ci	if (cursor->set & FB_CUR_SETCMAP) {
10718c2ecf20Sopenharmony_ci		struct fb_cmap cmap = info->cmap;
10728c2ecf20Sopenharmony_ci		u32 bg_idx = cursor->image.bg_color;
10738c2ecf20Sopenharmony_ci		u32 fg_idx = cursor->image.fg_color;
10748c2ecf20Sopenharmony_ci		unsigned long bg_color, fg_color;
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci		fg_color = (((u32)cmap.red[fg_idx]   & 0xff00) << 8) |
10778c2ecf20Sopenharmony_ci			   (((u32)cmap.green[fg_idx] & 0xff00) << 0) |
10788c2ecf20Sopenharmony_ci			   (((u32)cmap.blue[fg_idx]  & 0xff00) >> 8);
10798c2ecf20Sopenharmony_ci		bg_color = (((u32)cmap.red[bg_idx]   & 0xff00) << 8) |
10808c2ecf20Sopenharmony_ci			   (((u32)cmap.green[bg_idx] & 0xff00) << 0) |
10818c2ecf20Sopenharmony_ci			   (((u32)cmap.blue[bg_idx]  & 0xff00) >> 8);
10828c2ecf20Sopenharmony_ci		banshee_make_room(par, 2);
10838c2ecf20Sopenharmony_ci		tdfx_outl(par, HWCURC0, bg_color);
10848c2ecf20Sopenharmony_ci		tdfx_outl(par, HWCURC1, fg_color);
10858c2ecf20Sopenharmony_ci	}
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	if (cursor->set & FB_CUR_SETPOS) {
10888c2ecf20Sopenharmony_ci		int x = cursor->image.dx;
10898c2ecf20Sopenharmony_ci		int y = cursor->image.dy - info->var.yoffset;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci		x += 63;
10928c2ecf20Sopenharmony_ci		y += 63;
10938c2ecf20Sopenharmony_ci		banshee_make_room(par, 1);
10948c2ecf20Sopenharmony_ci		tdfx_outl(par, HWCURLOC, (y << 16) + x);
10958c2ecf20Sopenharmony_ci	}
10968c2ecf20Sopenharmony_ci	if (cursor->set & (FB_CUR_SETIMAGE | FB_CUR_SETSHAPE)) {
10978c2ecf20Sopenharmony_ci		/*
10988c2ecf20Sopenharmony_ci		 * Voodoo 3 and above cards use 2 monochrome cursor patterns.
10998c2ecf20Sopenharmony_ci		 *    The reason is so the card can fetch 8 words at a time
11008c2ecf20Sopenharmony_ci		 * and are stored on chip for use for the next 8 scanlines.
11018c2ecf20Sopenharmony_ci		 * This reduces the number of times for access to draw the
11028c2ecf20Sopenharmony_ci		 * cursor for each screen refresh.
11038c2ecf20Sopenharmony_ci		 *    Each pattern is a bitmap of 64 bit wide and 64 bit high
11048c2ecf20Sopenharmony_ci		 * (total of 8192 bits or 1024 bytes). The two patterns are
11058c2ecf20Sopenharmony_ci		 * stored in such a way that pattern 0 always resides in the
11068c2ecf20Sopenharmony_ci		 * lower half (least significant 64 bits) of a 128 bit word
11078c2ecf20Sopenharmony_ci		 * and pattern 1 the upper half. If you examine the data of
11088c2ecf20Sopenharmony_ci		 * the cursor image the graphics card uses then from the
11098c2ecf20Sopenharmony_ci		 * beginning you see line one of pattern 0, line one of
11108c2ecf20Sopenharmony_ci		 * pattern 1, line two of pattern 0, line two of pattern 1,
11118c2ecf20Sopenharmony_ci		 * etc etc. The linear stride for the cursor is always 16 bytes
11128c2ecf20Sopenharmony_ci		 * (128 bits) which is the maximum cursor width times two for
11138c2ecf20Sopenharmony_ci		 * the two monochrome patterns.
11148c2ecf20Sopenharmony_ci		 */
11158c2ecf20Sopenharmony_ci		u8 __iomem *cursorbase = info->screen_base + info->fix.smem_len;
11168c2ecf20Sopenharmony_ci		u8 *bitmap = (u8 *)cursor->image.data;
11178c2ecf20Sopenharmony_ci		u8 *mask = (u8 *)cursor->mask;
11188c2ecf20Sopenharmony_ci		int i;
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci		fb_memset(cursorbase, 0, 1024);
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_ci		for (i = 0; i < cursor->image.height; i++) {
11238c2ecf20Sopenharmony_ci			int h = 0;
11248c2ecf20Sopenharmony_ci			int j = (cursor->image.width + 7) >> 3;
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_ci			for (; j > 0; j--) {
11278c2ecf20Sopenharmony_ci				u8 data = *mask ^ *bitmap;
11288c2ecf20Sopenharmony_ci				if (cursor->rop == ROP_COPY)
11298c2ecf20Sopenharmony_ci					data = *mask & *bitmap;
11308c2ecf20Sopenharmony_ci				/* Pattern 0. Copy the cursor mask to it */
11318c2ecf20Sopenharmony_ci				fb_writeb(*mask, cursorbase + h);
11328c2ecf20Sopenharmony_ci				mask++;
11338c2ecf20Sopenharmony_ci				/* Pattern 1. Copy the cursor bitmap to it */
11348c2ecf20Sopenharmony_ci				fb_writeb(data, cursorbase + h + 8);
11358c2ecf20Sopenharmony_ci				bitmap++;
11368c2ecf20Sopenharmony_ci				h++;
11378c2ecf20Sopenharmony_ci			}
11388c2ecf20Sopenharmony_ci			cursorbase += 16;
11398c2ecf20Sopenharmony_ci		}
11408c2ecf20Sopenharmony_ci	}
11418c2ecf20Sopenharmony_ci	return 0;
11428c2ecf20Sopenharmony_ci}
11438c2ecf20Sopenharmony_ci
11448c2ecf20Sopenharmony_cistatic const struct fb_ops tdfxfb_ops = {
11458c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
11468c2ecf20Sopenharmony_ci	.fb_check_var	= tdfxfb_check_var,
11478c2ecf20Sopenharmony_ci	.fb_set_par	= tdfxfb_set_par,
11488c2ecf20Sopenharmony_ci	.fb_setcolreg	= tdfxfb_setcolreg,
11498c2ecf20Sopenharmony_ci	.fb_blank	= tdfxfb_blank,
11508c2ecf20Sopenharmony_ci	.fb_pan_display	= tdfxfb_pan_display,
11518c2ecf20Sopenharmony_ci	.fb_sync	= banshee_wait_idle,
11528c2ecf20Sopenharmony_ci	.fb_cursor	= tdfxfb_cursor,
11538c2ecf20Sopenharmony_ci#ifdef CONFIG_FB_3DFX_ACCEL
11548c2ecf20Sopenharmony_ci	.fb_fillrect	= tdfxfb_fillrect,
11558c2ecf20Sopenharmony_ci	.fb_copyarea	= tdfxfb_copyarea,
11568c2ecf20Sopenharmony_ci	.fb_imageblit	= tdfxfb_imageblit,
11578c2ecf20Sopenharmony_ci#else
11588c2ecf20Sopenharmony_ci	.fb_fillrect	= cfb_fillrect,
11598c2ecf20Sopenharmony_ci	.fb_copyarea	= cfb_copyarea,
11608c2ecf20Sopenharmony_ci	.fb_imageblit	= cfb_imageblit,
11618c2ecf20Sopenharmony_ci#endif
11628c2ecf20Sopenharmony_ci};
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci#ifdef CONFIG_FB_3DFX_I2C
11658c2ecf20Sopenharmony_ci/* The voo GPIO registers don't have individual masks for each bit
11668c2ecf20Sopenharmony_ci   so we always have to read before writing. */
11678c2ecf20Sopenharmony_ci
11688c2ecf20Sopenharmony_cistatic void tdfxfb_i2c_setscl(void *data, int val)
11698c2ecf20Sopenharmony_ci{
11708c2ecf20Sopenharmony_ci	struct tdfxfb_i2c_chan 	*chan = data;
11718c2ecf20Sopenharmony_ci	struct tdfx_par 	*par = chan->par;
11728c2ecf20Sopenharmony_ci	unsigned int r;
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	r = tdfx_inl(par, VIDSERPARPORT);
11758c2ecf20Sopenharmony_ci	if (val)
11768c2ecf20Sopenharmony_ci		r |= I2C_SCL_OUT;
11778c2ecf20Sopenharmony_ci	else
11788c2ecf20Sopenharmony_ci		r &= ~I2C_SCL_OUT;
11798c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDSERPARPORT, r);
11808c2ecf20Sopenharmony_ci	tdfx_inl(par, VIDSERPARPORT);	/* flush posted write */
11818c2ecf20Sopenharmony_ci}
11828c2ecf20Sopenharmony_ci
11838c2ecf20Sopenharmony_cistatic void tdfxfb_i2c_setsda(void *data, int val)
11848c2ecf20Sopenharmony_ci{
11858c2ecf20Sopenharmony_ci	struct tdfxfb_i2c_chan 	*chan = data;
11868c2ecf20Sopenharmony_ci	struct tdfx_par 	*par = chan->par;
11878c2ecf20Sopenharmony_ci	unsigned int r;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	r = tdfx_inl(par, VIDSERPARPORT);
11908c2ecf20Sopenharmony_ci	if (val)
11918c2ecf20Sopenharmony_ci		r |= I2C_SDA_OUT;
11928c2ecf20Sopenharmony_ci	else
11938c2ecf20Sopenharmony_ci		r &= ~I2C_SDA_OUT;
11948c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDSERPARPORT, r);
11958c2ecf20Sopenharmony_ci	tdfx_inl(par, VIDSERPARPORT);	/* flush posted write */
11968c2ecf20Sopenharmony_ci}
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci/* The GPIO pins are open drain, so the pins always remain outputs.
11998c2ecf20Sopenharmony_ci   We rely on the i2c-algo-bit routines to set the pins high before
12008c2ecf20Sopenharmony_ci   reading the input from other chips. */
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_cistatic int tdfxfb_i2c_getscl(void *data)
12038c2ecf20Sopenharmony_ci{
12048c2ecf20Sopenharmony_ci	struct tdfxfb_i2c_chan 	*chan = data;
12058c2ecf20Sopenharmony_ci	struct tdfx_par 	*par = chan->par;
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	return (0 != (tdfx_inl(par, VIDSERPARPORT) & I2C_SCL_IN));
12088c2ecf20Sopenharmony_ci}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_cistatic int tdfxfb_i2c_getsda(void *data)
12118c2ecf20Sopenharmony_ci{
12128c2ecf20Sopenharmony_ci	struct tdfxfb_i2c_chan 	*chan = data;
12138c2ecf20Sopenharmony_ci	struct tdfx_par 	*par = chan->par;
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	return (0 != (tdfx_inl(par, VIDSERPARPORT) & I2C_SDA_IN));
12168c2ecf20Sopenharmony_ci}
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_cistatic void tdfxfb_ddc_setscl(void *data, int val)
12198c2ecf20Sopenharmony_ci{
12208c2ecf20Sopenharmony_ci	struct tdfxfb_i2c_chan 	*chan = data;
12218c2ecf20Sopenharmony_ci	struct tdfx_par 	*par = chan->par;
12228c2ecf20Sopenharmony_ci	unsigned int r;
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	r = tdfx_inl(par, VIDSERPARPORT);
12258c2ecf20Sopenharmony_ci	if (val)
12268c2ecf20Sopenharmony_ci		r |= DDC_SCL_OUT;
12278c2ecf20Sopenharmony_ci	else
12288c2ecf20Sopenharmony_ci		r &= ~DDC_SCL_OUT;
12298c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDSERPARPORT, r);
12308c2ecf20Sopenharmony_ci	tdfx_inl(par, VIDSERPARPORT);	/* flush posted write */
12318c2ecf20Sopenharmony_ci}
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_cistatic void tdfxfb_ddc_setsda(void *data, int val)
12348c2ecf20Sopenharmony_ci{
12358c2ecf20Sopenharmony_ci	struct tdfxfb_i2c_chan 	*chan = data;
12368c2ecf20Sopenharmony_ci	struct tdfx_par 	*par = chan->par;
12378c2ecf20Sopenharmony_ci	unsigned int r;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	r = tdfx_inl(par, VIDSERPARPORT);
12408c2ecf20Sopenharmony_ci	if (val)
12418c2ecf20Sopenharmony_ci		r |= DDC_SDA_OUT;
12428c2ecf20Sopenharmony_ci	else
12438c2ecf20Sopenharmony_ci		r &= ~DDC_SDA_OUT;
12448c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDSERPARPORT, r);
12458c2ecf20Sopenharmony_ci	tdfx_inl(par, VIDSERPARPORT);	/* flush posted write */
12468c2ecf20Sopenharmony_ci}
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_cistatic int tdfxfb_ddc_getscl(void *data)
12498c2ecf20Sopenharmony_ci{
12508c2ecf20Sopenharmony_ci	struct tdfxfb_i2c_chan 	*chan = data;
12518c2ecf20Sopenharmony_ci	struct tdfx_par 	*par = chan->par;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	return (0 != (tdfx_inl(par, VIDSERPARPORT) & DDC_SCL_IN));
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_cistatic int tdfxfb_ddc_getsda(void *data)
12578c2ecf20Sopenharmony_ci{
12588c2ecf20Sopenharmony_ci	struct tdfxfb_i2c_chan 	*chan = data;
12598c2ecf20Sopenharmony_ci	struct tdfx_par 	*par = chan->par;
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	return (0 != (tdfx_inl(par, VIDSERPARPORT) & DDC_SDA_IN));
12628c2ecf20Sopenharmony_ci}
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_cistatic int tdfxfb_setup_ddc_bus(struct tdfxfb_i2c_chan *chan, const char *name,
12658c2ecf20Sopenharmony_ci				struct device *dev)
12668c2ecf20Sopenharmony_ci{
12678c2ecf20Sopenharmony_ci	int rc;
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	strlcpy(chan->adapter.name, name, sizeof(chan->adapter.name));
12708c2ecf20Sopenharmony_ci	chan->adapter.owner		= THIS_MODULE;
12718c2ecf20Sopenharmony_ci	chan->adapter.class		= I2C_CLASS_DDC;
12728c2ecf20Sopenharmony_ci	chan->adapter.algo_data		= &chan->algo;
12738c2ecf20Sopenharmony_ci	chan->adapter.dev.parent	= dev;
12748c2ecf20Sopenharmony_ci	chan->algo.setsda		= tdfxfb_ddc_setsda;
12758c2ecf20Sopenharmony_ci	chan->algo.setscl		= tdfxfb_ddc_setscl;
12768c2ecf20Sopenharmony_ci	chan->algo.getsda		= tdfxfb_ddc_getsda;
12778c2ecf20Sopenharmony_ci	chan->algo.getscl		= tdfxfb_ddc_getscl;
12788c2ecf20Sopenharmony_ci	chan->algo.udelay		= 10;
12798c2ecf20Sopenharmony_ci	chan->algo.timeout		= msecs_to_jiffies(500);
12808c2ecf20Sopenharmony_ci	chan->algo.data 		= chan;
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	i2c_set_adapdata(&chan->adapter, chan);
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	rc = i2c_bit_add_bus(&chan->adapter);
12858c2ecf20Sopenharmony_ci	if (rc == 0)
12868c2ecf20Sopenharmony_ci		DPRINTK("I2C bus %s registered.\n", name);
12878c2ecf20Sopenharmony_ci	else
12888c2ecf20Sopenharmony_ci		chan->par = NULL;
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci	return rc;
12918c2ecf20Sopenharmony_ci}
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_cistatic int tdfxfb_setup_i2c_bus(struct tdfxfb_i2c_chan *chan, const char *name,
12948c2ecf20Sopenharmony_ci				struct device *dev)
12958c2ecf20Sopenharmony_ci{
12968c2ecf20Sopenharmony_ci	int rc;
12978c2ecf20Sopenharmony_ci
12988c2ecf20Sopenharmony_ci	strlcpy(chan->adapter.name, name, sizeof(chan->adapter.name));
12998c2ecf20Sopenharmony_ci	chan->adapter.owner		= THIS_MODULE;
13008c2ecf20Sopenharmony_ci	chan->adapter.algo_data		= &chan->algo;
13018c2ecf20Sopenharmony_ci	chan->adapter.dev.parent	= dev;
13028c2ecf20Sopenharmony_ci	chan->algo.setsda		= tdfxfb_i2c_setsda;
13038c2ecf20Sopenharmony_ci	chan->algo.setscl		= tdfxfb_i2c_setscl;
13048c2ecf20Sopenharmony_ci	chan->algo.getsda		= tdfxfb_i2c_getsda;
13058c2ecf20Sopenharmony_ci	chan->algo.getscl		= tdfxfb_i2c_getscl;
13068c2ecf20Sopenharmony_ci	chan->algo.udelay		= 10;
13078c2ecf20Sopenharmony_ci	chan->algo.timeout		= msecs_to_jiffies(500);
13088c2ecf20Sopenharmony_ci	chan->algo.data 		= chan;
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci	i2c_set_adapdata(&chan->adapter, chan);
13118c2ecf20Sopenharmony_ci
13128c2ecf20Sopenharmony_ci	rc = i2c_bit_add_bus(&chan->adapter);
13138c2ecf20Sopenharmony_ci	if (rc == 0)
13148c2ecf20Sopenharmony_ci		DPRINTK("I2C bus %s registered.\n", name);
13158c2ecf20Sopenharmony_ci	else
13168c2ecf20Sopenharmony_ci		chan->par = NULL;
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_ci	return rc;
13198c2ecf20Sopenharmony_ci}
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_cistatic void tdfxfb_create_i2c_busses(struct fb_info *info)
13228c2ecf20Sopenharmony_ci{
13238c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
13248c2ecf20Sopenharmony_ci
13258c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDINFORMAT, 0x8160);
13268c2ecf20Sopenharmony_ci	tdfx_outl(par, VIDSERPARPORT, 0xcffc0020);
13278c2ecf20Sopenharmony_ci
13288c2ecf20Sopenharmony_ci	par->chan[0].par = par;
13298c2ecf20Sopenharmony_ci	par->chan[1].par = par;
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	tdfxfb_setup_ddc_bus(&par->chan[0], "Voodoo3-DDC", info->dev);
13328c2ecf20Sopenharmony_ci	tdfxfb_setup_i2c_bus(&par->chan[1], "Voodoo3-I2C", info->dev);
13338c2ecf20Sopenharmony_ci}
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_cistatic void tdfxfb_delete_i2c_busses(struct tdfx_par *par)
13368c2ecf20Sopenharmony_ci{
13378c2ecf20Sopenharmony_ci	if (par->chan[0].par)
13388c2ecf20Sopenharmony_ci		i2c_del_adapter(&par->chan[0].adapter);
13398c2ecf20Sopenharmony_ci	par->chan[0].par = NULL;
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	if (par->chan[1].par)
13428c2ecf20Sopenharmony_ci		i2c_del_adapter(&par->chan[1].adapter);
13438c2ecf20Sopenharmony_ci	par->chan[1].par = NULL;
13448c2ecf20Sopenharmony_ci}
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_cistatic int tdfxfb_probe_i2c_connector(struct tdfx_par *par,
13478c2ecf20Sopenharmony_ci				      struct fb_monspecs *specs)
13488c2ecf20Sopenharmony_ci{
13498c2ecf20Sopenharmony_ci	u8 *edid = NULL;
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_ci	DPRINTK("Probe DDC Bus\n");
13528c2ecf20Sopenharmony_ci	if (par->chan[0].par)
13538c2ecf20Sopenharmony_ci		edid = fb_ddc_read(&par->chan[0].adapter);
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	if (edid) {
13568c2ecf20Sopenharmony_ci		fb_edid_to_monspecs(edid, specs);
13578c2ecf20Sopenharmony_ci		kfree(edid);
13588c2ecf20Sopenharmony_ci		return 0;
13598c2ecf20Sopenharmony_ci	}
13608c2ecf20Sopenharmony_ci	return 1;
13618c2ecf20Sopenharmony_ci}
13628c2ecf20Sopenharmony_ci#endif /* CONFIG_FB_3DFX_I2C */
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci/**
13658c2ecf20Sopenharmony_ci *      tdfxfb_probe - Device Initializiation
13668c2ecf20Sopenharmony_ci *
13678c2ecf20Sopenharmony_ci *      @pdev:  PCI Device to initialize
13688c2ecf20Sopenharmony_ci *      @id:    PCI Device ID
13698c2ecf20Sopenharmony_ci *
13708c2ecf20Sopenharmony_ci *      Initializes and allocates resources for PCI device @pdev.
13718c2ecf20Sopenharmony_ci *
13728c2ecf20Sopenharmony_ci */
13738c2ecf20Sopenharmony_cistatic int tdfxfb_probe(struct pci_dev *pdev, const struct pci_device_id *id)
13748c2ecf20Sopenharmony_ci{
13758c2ecf20Sopenharmony_ci	struct tdfx_par *default_par;
13768c2ecf20Sopenharmony_ci	struct fb_info *info;
13778c2ecf20Sopenharmony_ci	int err, lpitch;
13788c2ecf20Sopenharmony_ci	struct fb_monspecs *specs;
13798c2ecf20Sopenharmony_ci	bool found;
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci	err = pci_enable_device(pdev);
13828c2ecf20Sopenharmony_ci	if (err) {
13838c2ecf20Sopenharmony_ci		printk(KERN_ERR "tdfxfb: Can't enable pdev: %d\n", err);
13848c2ecf20Sopenharmony_ci		return err;
13858c2ecf20Sopenharmony_ci	}
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci	info = framebuffer_alloc(sizeof(struct tdfx_par), &pdev->dev);
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	if (!info)
13908c2ecf20Sopenharmony_ci		return -ENOMEM;
13918c2ecf20Sopenharmony_ci
13928c2ecf20Sopenharmony_ci	default_par = info->par;
13938c2ecf20Sopenharmony_ci	info->fix = tdfx_fix;
13948c2ecf20Sopenharmony_ci
13958c2ecf20Sopenharmony_ci	/* Configure the default fb_fix_screeninfo first */
13968c2ecf20Sopenharmony_ci	switch (pdev->device) {
13978c2ecf20Sopenharmony_ci	case PCI_DEVICE_ID_3DFX_BANSHEE:
13988c2ecf20Sopenharmony_ci		strcpy(info->fix.id, "3Dfx Banshee");
13998c2ecf20Sopenharmony_ci		default_par->max_pixclock = BANSHEE_MAX_PIXCLOCK;
14008c2ecf20Sopenharmony_ci		break;
14018c2ecf20Sopenharmony_ci	case PCI_DEVICE_ID_3DFX_VOODOO3:
14028c2ecf20Sopenharmony_ci		strcpy(info->fix.id, "3Dfx Voodoo3");
14038c2ecf20Sopenharmony_ci		default_par->max_pixclock = VOODOO3_MAX_PIXCLOCK;
14048c2ecf20Sopenharmony_ci		break;
14058c2ecf20Sopenharmony_ci	case PCI_DEVICE_ID_3DFX_VOODOO5:
14068c2ecf20Sopenharmony_ci		strcpy(info->fix.id, "3Dfx Voodoo5");
14078c2ecf20Sopenharmony_ci		default_par->max_pixclock = VOODOO5_MAX_PIXCLOCK;
14088c2ecf20Sopenharmony_ci		break;
14098c2ecf20Sopenharmony_ci	}
14108c2ecf20Sopenharmony_ci
14118c2ecf20Sopenharmony_ci	info->fix.mmio_start = pci_resource_start(pdev, 0);
14128c2ecf20Sopenharmony_ci	info->fix.mmio_len = pci_resource_len(pdev, 0);
14138c2ecf20Sopenharmony_ci	if (!request_mem_region(info->fix.mmio_start, info->fix.mmio_len,
14148c2ecf20Sopenharmony_ci				"tdfx regbase")) {
14158c2ecf20Sopenharmony_ci		printk(KERN_ERR "tdfxfb: Can't reserve regbase\n");
14168c2ecf20Sopenharmony_ci		goto out_err;
14178c2ecf20Sopenharmony_ci	}
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	default_par->regbase_virt =
14208c2ecf20Sopenharmony_ci		ioremap(info->fix.mmio_start, info->fix.mmio_len);
14218c2ecf20Sopenharmony_ci	if (!default_par->regbase_virt) {
14228c2ecf20Sopenharmony_ci		printk(KERN_ERR "fb: Can't remap %s register area.\n",
14238c2ecf20Sopenharmony_ci				info->fix.id);
14248c2ecf20Sopenharmony_ci		goto out_err_regbase;
14258c2ecf20Sopenharmony_ci	}
14268c2ecf20Sopenharmony_ci
14278c2ecf20Sopenharmony_ci	info->fix.smem_start = pci_resource_start(pdev, 1);
14288c2ecf20Sopenharmony_ci	info->fix.smem_len = do_lfb_size(default_par, pdev->device);
14298c2ecf20Sopenharmony_ci	if (!info->fix.smem_len) {
14308c2ecf20Sopenharmony_ci		printk(KERN_ERR "fb: Can't count %s memory.\n", info->fix.id);
14318c2ecf20Sopenharmony_ci		goto out_err_regbase;
14328c2ecf20Sopenharmony_ci	}
14338c2ecf20Sopenharmony_ci
14348c2ecf20Sopenharmony_ci	if (!request_mem_region(info->fix.smem_start,
14358c2ecf20Sopenharmony_ci				pci_resource_len(pdev, 1), "tdfx smem")) {
14368c2ecf20Sopenharmony_ci		printk(KERN_ERR "tdfxfb: Can't reserve smem\n");
14378c2ecf20Sopenharmony_ci		goto out_err_regbase;
14388c2ecf20Sopenharmony_ci	}
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	info->screen_base = ioremap_wc(info->fix.smem_start,
14418c2ecf20Sopenharmony_ci				       info->fix.smem_len);
14428c2ecf20Sopenharmony_ci	if (!info->screen_base) {
14438c2ecf20Sopenharmony_ci		printk(KERN_ERR "fb: Can't remap %s framebuffer.\n",
14448c2ecf20Sopenharmony_ci				info->fix.id);
14458c2ecf20Sopenharmony_ci		goto out_err_screenbase;
14468c2ecf20Sopenharmony_ci	}
14478c2ecf20Sopenharmony_ci
14488c2ecf20Sopenharmony_ci	default_par->iobase = pci_resource_start(pdev, 2);
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci	if (!request_region(pci_resource_start(pdev, 2),
14518c2ecf20Sopenharmony_ci			    pci_resource_len(pdev, 2), "tdfx iobase")) {
14528c2ecf20Sopenharmony_ci		printk(KERN_ERR "tdfxfb: Can't reserve iobase\n");
14538c2ecf20Sopenharmony_ci		goto out_err_screenbase;
14548c2ecf20Sopenharmony_ci	}
14558c2ecf20Sopenharmony_ci
14568c2ecf20Sopenharmony_ci	printk(KERN_INFO "fb: %s memory = %dK\n", info->fix.id,
14578c2ecf20Sopenharmony_ci			info->fix.smem_len >> 10);
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	if (!nomtrr)
14608c2ecf20Sopenharmony_ci		default_par->wc_cookie= arch_phys_wc_add(info->fix.smem_start,
14618c2ecf20Sopenharmony_ci							 info->fix.smem_len);
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	info->fix.ypanstep	= nopan ? 0 : 1;
14648c2ecf20Sopenharmony_ci	info->fix.ywrapstep	= nowrap ? 0 : 1;
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci	info->fbops		= &tdfxfb_ops;
14678c2ecf20Sopenharmony_ci	info->pseudo_palette	= default_par->palette;
14688c2ecf20Sopenharmony_ci	info->flags		= FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
14698c2ecf20Sopenharmony_ci#ifdef CONFIG_FB_3DFX_ACCEL
14708c2ecf20Sopenharmony_ci	info->flags		|= FBINFO_HWACCEL_FILLRECT |
14718c2ecf20Sopenharmony_ci				   FBINFO_HWACCEL_COPYAREA |
14728c2ecf20Sopenharmony_ci				   FBINFO_HWACCEL_IMAGEBLIT |
14738c2ecf20Sopenharmony_ci				   FBINFO_READS_FAST;
14748c2ecf20Sopenharmony_ci#endif
14758c2ecf20Sopenharmony_ci	/* reserve 8192 bits for cursor */
14768c2ecf20Sopenharmony_ci	/* the 2.4 driver says PAGE_MASK boundary is not enough for Voodoo4 */
14778c2ecf20Sopenharmony_ci	if (hwcursor)
14788c2ecf20Sopenharmony_ci		info->fix.smem_len = (info->fix.smem_len - 1024) &
14798c2ecf20Sopenharmony_ci					(PAGE_MASK << 1);
14808c2ecf20Sopenharmony_ci	specs = &info->monspecs;
14818c2ecf20Sopenharmony_ci	found = false;
14828c2ecf20Sopenharmony_ci	info->var.bits_per_pixel = 8;
14838c2ecf20Sopenharmony_ci#ifdef CONFIG_FB_3DFX_I2C
14848c2ecf20Sopenharmony_ci	tdfxfb_create_i2c_busses(info);
14858c2ecf20Sopenharmony_ci	err = tdfxfb_probe_i2c_connector(default_par, specs);
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	if (!err) {
14888c2ecf20Sopenharmony_ci		if (specs->modedb == NULL)
14898c2ecf20Sopenharmony_ci			DPRINTK("Unable to get Mode Database\n");
14908c2ecf20Sopenharmony_ci		else {
14918c2ecf20Sopenharmony_ci			const struct fb_videomode *m;
14928c2ecf20Sopenharmony_ci
14938c2ecf20Sopenharmony_ci			fb_videomode_to_modelist(specs->modedb,
14948c2ecf20Sopenharmony_ci						 specs->modedb_len,
14958c2ecf20Sopenharmony_ci						 &info->modelist);
14968c2ecf20Sopenharmony_ci			m = fb_find_best_display(specs, &info->modelist);
14978c2ecf20Sopenharmony_ci			if (m) {
14988c2ecf20Sopenharmony_ci				fb_videomode_to_var(&info->var, m);
14998c2ecf20Sopenharmony_ci				/* fill all other info->var's fields */
15008c2ecf20Sopenharmony_ci				if (tdfxfb_check_var(&info->var, info) < 0)
15018c2ecf20Sopenharmony_ci					info->var = tdfx_var;
15028c2ecf20Sopenharmony_ci				else
15038c2ecf20Sopenharmony_ci					found = true;
15048c2ecf20Sopenharmony_ci			}
15058c2ecf20Sopenharmony_ci		}
15068c2ecf20Sopenharmony_ci	}
15078c2ecf20Sopenharmony_ci#endif
15088c2ecf20Sopenharmony_ci	if (!mode_option && !found)
15098c2ecf20Sopenharmony_ci		mode_option = "640x480@60";
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci	if (mode_option) {
15128c2ecf20Sopenharmony_ci		err = fb_find_mode(&info->var, info, mode_option,
15138c2ecf20Sopenharmony_ci				   specs->modedb, specs->modedb_len,
15148c2ecf20Sopenharmony_ci				   NULL, info->var.bits_per_pixel);
15158c2ecf20Sopenharmony_ci		if (!err || err == 4)
15168c2ecf20Sopenharmony_ci			info->var = tdfx_var;
15178c2ecf20Sopenharmony_ci	}
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_ci	if (found) {
15208c2ecf20Sopenharmony_ci		fb_destroy_modedb(specs->modedb);
15218c2ecf20Sopenharmony_ci		specs->modedb = NULL;
15228c2ecf20Sopenharmony_ci	}
15238c2ecf20Sopenharmony_ci
15248c2ecf20Sopenharmony_ci	/* maximize virtual vertical length */
15258c2ecf20Sopenharmony_ci	lpitch = info->var.xres_virtual * ((info->var.bits_per_pixel + 7) >> 3);
15268c2ecf20Sopenharmony_ci	info->var.yres_virtual = info->fix.smem_len / lpitch;
15278c2ecf20Sopenharmony_ci	if (info->var.yres_virtual < info->var.yres)
15288c2ecf20Sopenharmony_ci		goto out_err_iobase;
15298c2ecf20Sopenharmony_ci
15308c2ecf20Sopenharmony_ci	if (fb_alloc_cmap(&info->cmap, 256, 0) < 0) {
15318c2ecf20Sopenharmony_ci		printk(KERN_ERR "tdfxfb: Can't allocate color map\n");
15328c2ecf20Sopenharmony_ci		goto out_err_iobase;
15338c2ecf20Sopenharmony_ci	}
15348c2ecf20Sopenharmony_ci
15358c2ecf20Sopenharmony_ci	if (register_framebuffer(info) < 0) {
15368c2ecf20Sopenharmony_ci		printk(KERN_ERR "tdfxfb: can't register framebuffer\n");
15378c2ecf20Sopenharmony_ci		fb_dealloc_cmap(&info->cmap);
15388c2ecf20Sopenharmony_ci		goto out_err_iobase;
15398c2ecf20Sopenharmony_ci	}
15408c2ecf20Sopenharmony_ci	/*
15418c2ecf20Sopenharmony_ci	 * Our driver data
15428c2ecf20Sopenharmony_ci	 */
15438c2ecf20Sopenharmony_ci	pci_set_drvdata(pdev, info);
15448c2ecf20Sopenharmony_ci	return 0;
15458c2ecf20Sopenharmony_ci
15468c2ecf20Sopenharmony_ciout_err_iobase:
15478c2ecf20Sopenharmony_ci#ifdef CONFIG_FB_3DFX_I2C
15488c2ecf20Sopenharmony_ci	tdfxfb_delete_i2c_busses(default_par);
15498c2ecf20Sopenharmony_ci#endif
15508c2ecf20Sopenharmony_ci	arch_phys_wc_del(default_par->wc_cookie);
15518c2ecf20Sopenharmony_ci	release_region(pci_resource_start(pdev, 2),
15528c2ecf20Sopenharmony_ci		       pci_resource_len(pdev, 2));
15538c2ecf20Sopenharmony_ciout_err_screenbase:
15548c2ecf20Sopenharmony_ci	if (info->screen_base)
15558c2ecf20Sopenharmony_ci		iounmap(info->screen_base);
15568c2ecf20Sopenharmony_ci	release_mem_region(info->fix.smem_start, pci_resource_len(pdev, 1));
15578c2ecf20Sopenharmony_ciout_err_regbase:
15588c2ecf20Sopenharmony_ci	/*
15598c2ecf20Sopenharmony_ci	 * Cleanup after anything that was remapped/allocated.
15608c2ecf20Sopenharmony_ci	 */
15618c2ecf20Sopenharmony_ci	if (default_par->regbase_virt)
15628c2ecf20Sopenharmony_ci		iounmap(default_par->regbase_virt);
15638c2ecf20Sopenharmony_ci	release_mem_region(info->fix.mmio_start, info->fix.mmio_len);
15648c2ecf20Sopenharmony_ciout_err:
15658c2ecf20Sopenharmony_ci	framebuffer_release(info);
15668c2ecf20Sopenharmony_ci	return -ENXIO;
15678c2ecf20Sopenharmony_ci}
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci#ifndef MODULE
15708c2ecf20Sopenharmony_cistatic void __init tdfxfb_setup(char *options)
15718c2ecf20Sopenharmony_ci{
15728c2ecf20Sopenharmony_ci	char *this_opt;
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	if (!options || !*options)
15758c2ecf20Sopenharmony_ci		return;
15768c2ecf20Sopenharmony_ci
15778c2ecf20Sopenharmony_ci	while ((this_opt = strsep(&options, ",")) != NULL) {
15788c2ecf20Sopenharmony_ci		if (!*this_opt)
15798c2ecf20Sopenharmony_ci			continue;
15808c2ecf20Sopenharmony_ci		if (!strcmp(this_opt, "nopan")) {
15818c2ecf20Sopenharmony_ci			nopan = 1;
15828c2ecf20Sopenharmony_ci		} else if (!strcmp(this_opt, "nowrap")) {
15838c2ecf20Sopenharmony_ci			nowrap = 1;
15848c2ecf20Sopenharmony_ci		} else if (!strncmp(this_opt, "hwcursor=", 9)) {
15858c2ecf20Sopenharmony_ci			hwcursor = simple_strtoul(this_opt + 9, NULL, 0);
15868c2ecf20Sopenharmony_ci		} else if (!strncmp(this_opt, "nomtrr", 6)) {
15878c2ecf20Sopenharmony_ci			nomtrr = 1;
15888c2ecf20Sopenharmony_ci		} else {
15898c2ecf20Sopenharmony_ci			mode_option = this_opt;
15908c2ecf20Sopenharmony_ci		}
15918c2ecf20Sopenharmony_ci	}
15928c2ecf20Sopenharmony_ci}
15938c2ecf20Sopenharmony_ci#endif
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci/**
15968c2ecf20Sopenharmony_ci *      tdfxfb_remove - Device removal
15978c2ecf20Sopenharmony_ci *
15988c2ecf20Sopenharmony_ci *      @pdev:  PCI Device to cleanup
15998c2ecf20Sopenharmony_ci *
16008c2ecf20Sopenharmony_ci *      Releases all resources allocated during the course of the driver's
16018c2ecf20Sopenharmony_ci *      lifetime for the PCI device @pdev.
16028c2ecf20Sopenharmony_ci *
16038c2ecf20Sopenharmony_ci */
16048c2ecf20Sopenharmony_cistatic void tdfxfb_remove(struct pci_dev *pdev)
16058c2ecf20Sopenharmony_ci{
16068c2ecf20Sopenharmony_ci	struct fb_info *info = pci_get_drvdata(pdev);
16078c2ecf20Sopenharmony_ci	struct tdfx_par *par = info->par;
16088c2ecf20Sopenharmony_ci
16098c2ecf20Sopenharmony_ci	unregister_framebuffer(info);
16108c2ecf20Sopenharmony_ci#ifdef CONFIG_FB_3DFX_I2C
16118c2ecf20Sopenharmony_ci	tdfxfb_delete_i2c_busses(par);
16128c2ecf20Sopenharmony_ci#endif
16138c2ecf20Sopenharmony_ci	arch_phys_wc_del(par->wc_cookie);
16148c2ecf20Sopenharmony_ci	iounmap(par->regbase_virt);
16158c2ecf20Sopenharmony_ci	iounmap(info->screen_base);
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_ci	/* Clean up after reserved regions */
16188c2ecf20Sopenharmony_ci	release_region(pci_resource_start(pdev, 2),
16198c2ecf20Sopenharmony_ci		       pci_resource_len(pdev, 2));
16208c2ecf20Sopenharmony_ci	release_mem_region(pci_resource_start(pdev, 1),
16218c2ecf20Sopenharmony_ci			   pci_resource_len(pdev, 1));
16228c2ecf20Sopenharmony_ci	release_mem_region(pci_resource_start(pdev, 0),
16238c2ecf20Sopenharmony_ci			   pci_resource_len(pdev, 0));
16248c2ecf20Sopenharmony_ci	fb_dealloc_cmap(&info->cmap);
16258c2ecf20Sopenharmony_ci	framebuffer_release(info);
16268c2ecf20Sopenharmony_ci}
16278c2ecf20Sopenharmony_ci
16288c2ecf20Sopenharmony_cistatic int __init tdfxfb_init(void)
16298c2ecf20Sopenharmony_ci{
16308c2ecf20Sopenharmony_ci#ifndef MODULE
16318c2ecf20Sopenharmony_ci	char *option = NULL;
16328c2ecf20Sopenharmony_ci
16338c2ecf20Sopenharmony_ci	if (fb_get_options("tdfxfb", &option))
16348c2ecf20Sopenharmony_ci		return -ENODEV;
16358c2ecf20Sopenharmony_ci
16368c2ecf20Sopenharmony_ci	tdfxfb_setup(option);
16378c2ecf20Sopenharmony_ci#endif
16388c2ecf20Sopenharmony_ci	return pci_register_driver(&tdfxfb_driver);
16398c2ecf20Sopenharmony_ci}
16408c2ecf20Sopenharmony_ci
16418c2ecf20Sopenharmony_cistatic void __exit tdfxfb_exit(void)
16428c2ecf20Sopenharmony_ci{
16438c2ecf20Sopenharmony_ci	pci_unregister_driver(&tdfxfb_driver);
16448c2ecf20Sopenharmony_ci}
16458c2ecf20Sopenharmony_ci
16468c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hannu Mallat <hmallat@cc.hut.fi>");
16478c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("3Dfx framebuffer device driver");
16488c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_cimodule_param(hwcursor, int, 0644);
16518c2ecf20Sopenharmony_ciMODULE_PARM_DESC(hwcursor, "Enable hardware cursor "
16528c2ecf20Sopenharmony_ci			"(1=enable, 0=disable, default=1)");
16538c2ecf20Sopenharmony_cimodule_param(mode_option, charp, 0);
16548c2ecf20Sopenharmony_ciMODULE_PARM_DESC(mode_option, "Initial video mode e.g. '648x480-8@60'");
16558c2ecf20Sopenharmony_cimodule_param(nomtrr, bool, 0);
16568c2ecf20Sopenharmony_ciMODULE_PARM_DESC(nomtrr, "Disable MTRR support (default: enabled)");
16578c2ecf20Sopenharmony_ci
16588c2ecf20Sopenharmony_cimodule_init(tdfxfb_init);
16598c2ecf20Sopenharmony_cimodule_exit(tdfxfb_exit);
1660