162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * linux/drivers/video/s3fb.c -- Frame buffer device driver for S3 Trio/Virge
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (c) 2006-2007 Ondrej Zajicek <santiago@crfreenet.org>
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
762306a36Sopenharmony_ci * License.  See the file COPYING in the main directory of this archive for
862306a36Sopenharmony_ci * more details.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Code is based on David Boucher's viafb (http://davesdomain.org.uk/viafb/)
1162306a36Sopenharmony_ci * which is based on the code of neofb.
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <linux/aperture.h>
1562306a36Sopenharmony_ci#include <linux/module.h>
1662306a36Sopenharmony_ci#include <linux/kernel.h>
1762306a36Sopenharmony_ci#include <linux/errno.h>
1862306a36Sopenharmony_ci#include <linux/string.h>
1962306a36Sopenharmony_ci#include <linux/mm.h>
2062306a36Sopenharmony_ci#include <linux/tty.h>
2162306a36Sopenharmony_ci#include <linux/delay.h>
2262306a36Sopenharmony_ci#include <linux/fb.h>
2362306a36Sopenharmony_ci#include <linux/svga.h>
2462306a36Sopenharmony_ci#include <linux/init.h>
2562306a36Sopenharmony_ci#include <linux/pci.h>
2662306a36Sopenharmony_ci#include <linux/console.h> /* Why should fb driver call console functions? because console_lock() */
2762306a36Sopenharmony_ci#include <video/vga.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <linux/i2c.h>
3062306a36Sopenharmony_ci#include <linux/i2c-algo-bit.h>
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_cistruct s3fb_info {
3362306a36Sopenharmony_ci	int chip, rev, mclk_freq;
3462306a36Sopenharmony_ci	int wc_cookie;
3562306a36Sopenharmony_ci	struct vgastate state;
3662306a36Sopenharmony_ci	struct mutex open_lock;
3762306a36Sopenharmony_ci	unsigned int ref_count;
3862306a36Sopenharmony_ci	u32 pseudo_palette[16];
3962306a36Sopenharmony_ci#ifdef CONFIG_FB_S3_DDC
4062306a36Sopenharmony_ci	u8 __iomem *mmio;
4162306a36Sopenharmony_ci	bool ddc_registered;
4262306a36Sopenharmony_ci	struct i2c_adapter ddc_adapter;
4362306a36Sopenharmony_ci	struct i2c_algo_bit_data ddc_algo;
4462306a36Sopenharmony_ci#endif
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic const struct svga_fb_format s3fb_formats[] = {
5162306a36Sopenharmony_ci	{ 0,  {0, 6, 0},  {0, 6, 0},  {0, 6, 0}, {0, 0, 0}, 0,
5262306a36Sopenharmony_ci		FB_TYPE_TEXT, FB_AUX_TEXT_SVGA_STEP4,	FB_VISUAL_PSEUDOCOLOR, 8, 16},
5362306a36Sopenharmony_ci	{ 4,  {0, 4, 0},  {0, 4, 0},  {0, 4, 0}, {0, 0, 0}, 0,
5462306a36Sopenharmony_ci		FB_TYPE_PACKED_PIXELS, 0,		FB_VISUAL_PSEUDOCOLOR, 8, 16},
5562306a36Sopenharmony_ci	{ 4,  {0, 4, 0},  {0, 4, 0},  {0, 4, 0}, {0, 0, 0}, 1,
5662306a36Sopenharmony_ci		FB_TYPE_INTERLEAVED_PLANES, 1,		FB_VISUAL_PSEUDOCOLOR, 8, 16},
5762306a36Sopenharmony_ci	{ 8,  {0, 8, 0},  {0, 8, 0},  {0, 8, 0}, {0, 0, 0}, 0,
5862306a36Sopenharmony_ci		FB_TYPE_PACKED_PIXELS, 0,		FB_VISUAL_PSEUDOCOLOR, 4, 8},
5962306a36Sopenharmony_ci	{16,  {10, 5, 0}, {5, 5, 0},  {0, 5, 0}, {0, 0, 0}, 0,
6062306a36Sopenharmony_ci		FB_TYPE_PACKED_PIXELS, 0,		FB_VISUAL_TRUECOLOR, 2, 4},
6162306a36Sopenharmony_ci	{16,  {11, 5, 0}, {5, 6, 0},  {0, 5, 0}, {0, 0, 0}, 0,
6262306a36Sopenharmony_ci		FB_TYPE_PACKED_PIXELS, 0,		FB_VISUAL_TRUECOLOR, 2, 4},
6362306a36Sopenharmony_ci	{24,  {16, 8, 0}, {8, 8, 0},  {0, 8, 0}, {0, 0, 0}, 0,
6462306a36Sopenharmony_ci		FB_TYPE_PACKED_PIXELS, 0,		FB_VISUAL_TRUECOLOR, 1, 2},
6562306a36Sopenharmony_ci	{32,  {16, 8, 0}, {8, 8, 0},  {0, 8, 0}, {0, 0, 0}, 0,
6662306a36Sopenharmony_ci		FB_TYPE_PACKED_PIXELS, 0,		FB_VISUAL_TRUECOLOR, 1, 2},
6762306a36Sopenharmony_ci	SVGA_FORMAT_END
6862306a36Sopenharmony_ci};
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic const struct svga_pll s3_pll = {3, 129, 3, 33, 0, 3,
7262306a36Sopenharmony_ci	35000, 240000, 14318};
7362306a36Sopenharmony_cistatic const struct svga_pll s3_trio3d_pll = {3, 129, 3, 31, 0, 4,
7462306a36Sopenharmony_ci	230000, 460000, 14318};
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_cistatic const int s3_memsizes[] = {4096, 0, 3072, 8192, 2048, 6144, 1024, 512};
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic const char * const s3_names[] = {"S3 Unknown", "S3 Trio32", "S3 Trio64", "S3 Trio64V+",
7962306a36Sopenharmony_ci			"S3 Trio64UV+", "S3 Trio64V2/DX", "S3 Trio64V2/GX",
8062306a36Sopenharmony_ci			"S3 Plato/PX", "S3 Aurora64V+", "S3 Virge",
8162306a36Sopenharmony_ci			"S3 Virge/VX", "S3 Virge/DX", "S3 Virge/GX",
8262306a36Sopenharmony_ci			"S3 Virge/GX2", "S3 Virge/GX2+", "",
8362306a36Sopenharmony_ci			"S3 Trio3D/1X", "S3 Trio3D/2X", "S3 Trio3D/2X",
8462306a36Sopenharmony_ci			"S3 Trio3D", "S3 Virge/MX"};
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci#define CHIP_UNKNOWN		0x00
8762306a36Sopenharmony_ci#define CHIP_732_TRIO32		0x01
8862306a36Sopenharmony_ci#define CHIP_764_TRIO64		0x02
8962306a36Sopenharmony_ci#define CHIP_765_TRIO64VP	0x03
9062306a36Sopenharmony_ci#define CHIP_767_TRIO64UVP	0x04
9162306a36Sopenharmony_ci#define CHIP_775_TRIO64V2_DX	0x05
9262306a36Sopenharmony_ci#define CHIP_785_TRIO64V2_GX	0x06
9362306a36Sopenharmony_ci#define CHIP_551_PLATO_PX	0x07
9462306a36Sopenharmony_ci#define CHIP_M65_AURORA64VP	0x08
9562306a36Sopenharmony_ci#define CHIP_325_VIRGE		0x09
9662306a36Sopenharmony_ci#define CHIP_988_VIRGE_VX	0x0A
9762306a36Sopenharmony_ci#define CHIP_375_VIRGE_DX	0x0B
9862306a36Sopenharmony_ci#define CHIP_385_VIRGE_GX	0x0C
9962306a36Sopenharmony_ci#define CHIP_357_VIRGE_GX2	0x0D
10062306a36Sopenharmony_ci#define CHIP_359_VIRGE_GX2P	0x0E
10162306a36Sopenharmony_ci#define CHIP_360_TRIO3D_1X	0x10
10262306a36Sopenharmony_ci#define CHIP_362_TRIO3D_2X	0x11
10362306a36Sopenharmony_ci#define CHIP_368_TRIO3D_2X	0x12
10462306a36Sopenharmony_ci#define CHIP_365_TRIO3D		0x13
10562306a36Sopenharmony_ci#define CHIP_260_VIRGE_MX	0x14
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci#define CHIP_XXX_TRIO		0x80
10862306a36Sopenharmony_ci#define CHIP_XXX_TRIO64V2_DXGX	0x81
10962306a36Sopenharmony_ci#define CHIP_XXX_VIRGE_DXGX	0x82
11062306a36Sopenharmony_ci#define CHIP_36X_TRIO3D_1X_2X	0x83
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#define CHIP_UNDECIDED_FLAG	0x80
11362306a36Sopenharmony_ci#define CHIP_MASK		0xFF
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define MMIO_OFFSET		0x1000000
11662306a36Sopenharmony_ci#define MMIO_SIZE		0x10000
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/* CRT timing register sets */
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistatic const struct vga_regset s3_h_total_regs[]        = {{0x00, 0, 7}, {0x5D, 0, 0}, VGA_REGSET_END};
12162306a36Sopenharmony_cistatic const struct vga_regset s3_h_display_regs[]      = {{0x01, 0, 7}, {0x5D, 1, 1}, VGA_REGSET_END};
12262306a36Sopenharmony_cistatic const struct vga_regset s3_h_blank_start_regs[]  = {{0x02, 0, 7}, {0x5D, 2, 2}, VGA_REGSET_END};
12362306a36Sopenharmony_cistatic const struct vga_regset s3_h_blank_end_regs[]    = {{0x03, 0, 4}, {0x05, 7, 7}, VGA_REGSET_END};
12462306a36Sopenharmony_cistatic const struct vga_regset s3_h_sync_start_regs[]   = {{0x04, 0, 7}, {0x5D, 4, 4}, VGA_REGSET_END};
12562306a36Sopenharmony_cistatic const struct vga_regset s3_h_sync_end_regs[]     = {{0x05, 0, 4}, VGA_REGSET_END};
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic const struct vga_regset s3_v_total_regs[]        = {{0x06, 0, 7}, {0x07, 0, 0}, {0x07, 5, 5}, {0x5E, 0, 0}, VGA_REGSET_END};
12862306a36Sopenharmony_cistatic const struct vga_regset s3_v_display_regs[]      = {{0x12, 0, 7}, {0x07, 1, 1}, {0x07, 6, 6}, {0x5E, 1, 1}, VGA_REGSET_END};
12962306a36Sopenharmony_cistatic const struct vga_regset s3_v_blank_start_regs[]  = {{0x15, 0, 7}, {0x07, 3, 3}, {0x09, 5, 5}, {0x5E, 2, 2}, VGA_REGSET_END};
13062306a36Sopenharmony_cistatic const struct vga_regset s3_v_blank_end_regs[]    = {{0x16, 0, 7}, VGA_REGSET_END};
13162306a36Sopenharmony_cistatic const struct vga_regset s3_v_sync_start_regs[]   = {{0x10, 0, 7}, {0x07, 2, 2}, {0x07, 7, 7}, {0x5E, 4, 4}, VGA_REGSET_END};
13262306a36Sopenharmony_cistatic const struct vga_regset s3_v_sync_end_regs[]     = {{0x11, 0, 3}, VGA_REGSET_END};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic const struct vga_regset s3_line_compare_regs[]   = {{0x18, 0, 7}, {0x07, 4, 4}, {0x09, 6, 6}, {0x5E, 6, 6}, VGA_REGSET_END};
13562306a36Sopenharmony_cistatic const struct vga_regset s3_start_address_regs[]  = {{0x0d, 0, 7}, {0x0c, 0, 7}, {0x69, 0, 4}, VGA_REGSET_END};
13662306a36Sopenharmony_cistatic const struct vga_regset s3_offset_regs[]         = {{0x13, 0, 7}, {0x51, 4, 5}, VGA_REGSET_END}; /* set 0x43 bit 2 to 0 */
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cistatic const struct vga_regset s3_dtpc_regs[]		= {{0x3B, 0, 7}, {0x5D, 6, 6}, VGA_REGSET_END};
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_cistatic const struct svga_timing_regs s3_timing_regs     = {
14162306a36Sopenharmony_ci	s3_h_total_regs, s3_h_display_regs, s3_h_blank_start_regs,
14262306a36Sopenharmony_ci	s3_h_blank_end_regs, s3_h_sync_start_regs, s3_h_sync_end_regs,
14362306a36Sopenharmony_ci	s3_v_total_regs, s3_v_display_regs, s3_v_blank_start_regs,
14462306a36Sopenharmony_ci	s3_v_blank_end_regs, s3_v_sync_start_regs, s3_v_sync_end_regs,
14562306a36Sopenharmony_ci};
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci/* Module parameters */
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistatic char *mode_option;
15462306a36Sopenharmony_cistatic int mtrr = 1;
15562306a36Sopenharmony_cistatic int fasttext = 1;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciMODULE_AUTHOR("(c) 2006-2007 Ondrej Zajicek <santiago@crfreenet.org>");
15962306a36Sopenharmony_ciMODULE_LICENSE("GPL");
16062306a36Sopenharmony_ciMODULE_DESCRIPTION("fbdev driver for S3 Trio/Virge");
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cimodule_param(mode_option, charp, 0444);
16362306a36Sopenharmony_ciMODULE_PARM_DESC(mode_option, "Default video mode ('640x480-8@60', etc)");
16462306a36Sopenharmony_cimodule_param_named(mode, mode_option, charp, 0444);
16562306a36Sopenharmony_ciMODULE_PARM_DESC(mode, "Default video mode ('640x480-8@60', etc) (deprecated)");
16662306a36Sopenharmony_cimodule_param(mtrr, int, 0444);
16762306a36Sopenharmony_ciMODULE_PARM_DESC(mtrr, "Enable write-combining with MTRR (1=enable, 0=disable, default=1)");
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_cimodule_param(fasttext, int, 0644);
17062306a36Sopenharmony_ciMODULE_PARM_DESC(fasttext, "Enable S3 fast text mode (1=enable, 0=disable, default=1)");
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci#ifdef CONFIG_FB_S3_DDC
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci#define DDC_REG		0xaa		/* Trio 3D/1X/2X */
17862306a36Sopenharmony_ci#define DDC_MMIO_REG	0xff20		/* all other chips */
17962306a36Sopenharmony_ci#define DDC_SCL_OUT	(1 << 0)
18062306a36Sopenharmony_ci#define DDC_SDA_OUT	(1 << 1)
18162306a36Sopenharmony_ci#define DDC_SCL_IN	(1 << 2)
18262306a36Sopenharmony_ci#define DDC_SDA_IN	(1 << 3)
18362306a36Sopenharmony_ci#define DDC_DRIVE_EN	(1 << 4)
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistatic bool s3fb_ddc_needs_mmio(int chip)
18662306a36Sopenharmony_ci{
18762306a36Sopenharmony_ci	return !(chip == CHIP_360_TRIO3D_1X  ||
18862306a36Sopenharmony_ci		 chip == CHIP_362_TRIO3D_2X  ||
18962306a36Sopenharmony_ci		 chip == CHIP_368_TRIO3D_2X);
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_cistatic u8 s3fb_ddc_read(struct s3fb_info *par)
19362306a36Sopenharmony_ci{
19462306a36Sopenharmony_ci	if (s3fb_ddc_needs_mmio(par->chip))
19562306a36Sopenharmony_ci		return readb(par->mmio + DDC_MMIO_REG);
19662306a36Sopenharmony_ci	else
19762306a36Sopenharmony_ci		return vga_rcrt(par->state.vgabase, DDC_REG);
19862306a36Sopenharmony_ci}
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_cistatic void s3fb_ddc_write(struct s3fb_info *par, u8 val)
20162306a36Sopenharmony_ci{
20262306a36Sopenharmony_ci	if (s3fb_ddc_needs_mmio(par->chip))
20362306a36Sopenharmony_ci		writeb(val, par->mmio + DDC_MMIO_REG);
20462306a36Sopenharmony_ci	else
20562306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, DDC_REG, val);
20662306a36Sopenharmony_ci}
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_cistatic void s3fb_ddc_setscl(void *data, int val)
20962306a36Sopenharmony_ci{
21062306a36Sopenharmony_ci	struct s3fb_info *par = data;
21162306a36Sopenharmony_ci	unsigned char reg;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	reg = s3fb_ddc_read(par) | DDC_DRIVE_EN;
21462306a36Sopenharmony_ci	if (val)
21562306a36Sopenharmony_ci		reg |= DDC_SCL_OUT;
21662306a36Sopenharmony_ci	else
21762306a36Sopenharmony_ci		reg &= ~DDC_SCL_OUT;
21862306a36Sopenharmony_ci	s3fb_ddc_write(par, reg);
21962306a36Sopenharmony_ci}
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_cistatic void s3fb_ddc_setsda(void *data, int val)
22262306a36Sopenharmony_ci{
22362306a36Sopenharmony_ci	struct s3fb_info *par = data;
22462306a36Sopenharmony_ci	unsigned char reg;
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	reg = s3fb_ddc_read(par) | DDC_DRIVE_EN;
22762306a36Sopenharmony_ci	if (val)
22862306a36Sopenharmony_ci		reg |= DDC_SDA_OUT;
22962306a36Sopenharmony_ci	else
23062306a36Sopenharmony_ci		reg &= ~DDC_SDA_OUT;
23162306a36Sopenharmony_ci	s3fb_ddc_write(par, reg);
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cistatic int s3fb_ddc_getscl(void *data)
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	struct s3fb_info *par = data;
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	return !!(s3fb_ddc_read(par) & DDC_SCL_IN);
23962306a36Sopenharmony_ci}
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_cistatic int s3fb_ddc_getsda(void *data)
24262306a36Sopenharmony_ci{
24362306a36Sopenharmony_ci	struct s3fb_info *par = data;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	return !!(s3fb_ddc_read(par) & DDC_SDA_IN);
24662306a36Sopenharmony_ci}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_cistatic int s3fb_setup_ddc_bus(struct fb_info *info)
24962306a36Sopenharmony_ci{
25062306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	strscpy(par->ddc_adapter.name, info->fix.id,
25362306a36Sopenharmony_ci		sizeof(par->ddc_adapter.name));
25462306a36Sopenharmony_ci	par->ddc_adapter.owner		= THIS_MODULE;
25562306a36Sopenharmony_ci	par->ddc_adapter.class		= I2C_CLASS_DDC;
25662306a36Sopenharmony_ci	par->ddc_adapter.algo_data	= &par->ddc_algo;
25762306a36Sopenharmony_ci	par->ddc_adapter.dev.parent	= info->device;
25862306a36Sopenharmony_ci	par->ddc_algo.setsda		= s3fb_ddc_setsda;
25962306a36Sopenharmony_ci	par->ddc_algo.setscl		= s3fb_ddc_setscl;
26062306a36Sopenharmony_ci	par->ddc_algo.getsda		= s3fb_ddc_getsda;
26162306a36Sopenharmony_ci	par->ddc_algo.getscl		= s3fb_ddc_getscl;
26262306a36Sopenharmony_ci	par->ddc_algo.udelay		= 10;
26362306a36Sopenharmony_ci	par->ddc_algo.timeout		= 20;
26462306a36Sopenharmony_ci	par->ddc_algo.data		= par;
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	i2c_set_adapdata(&par->ddc_adapter, par);
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	/*
26962306a36Sopenharmony_ci	 * some Virge cards have external MUX to switch chip I2C bus between
27062306a36Sopenharmony_ci	 * DDC and extension pins - switch it do DDC
27162306a36Sopenharmony_ci	 */
27262306a36Sopenharmony_ci/*	vga_wseq(par->state.vgabase, 0x08, 0x06); - not needed, already unlocked */
27362306a36Sopenharmony_ci	if (par->chip == CHIP_357_VIRGE_GX2 ||
27462306a36Sopenharmony_ci	    par->chip == CHIP_359_VIRGE_GX2P ||
27562306a36Sopenharmony_ci	    par->chip == CHIP_260_VIRGE_MX)
27662306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x0d, 0x01, 0x03);
27762306a36Sopenharmony_ci	else
27862306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x0d, 0x00, 0x03);
27962306a36Sopenharmony_ci	/* some Virge need this or the DDC is ignored */
28062306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x5c, 0x03, 0x03);
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	return i2c_bit_add_bus(&par->ddc_adapter);
28362306a36Sopenharmony_ci}
28462306a36Sopenharmony_ci#endif /* CONFIG_FB_S3_DDC */
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci/* Set font in S3 fast text mode */
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_cistatic void s3fb_settile_fast(struct fb_info *info, struct fb_tilemap *map)
29262306a36Sopenharmony_ci{
29362306a36Sopenharmony_ci	const u8 *font = map->data;
29462306a36Sopenharmony_ci	u8 __iomem *fb = (u8 __iomem *) info->screen_base;
29562306a36Sopenharmony_ci	int i, c;
29662306a36Sopenharmony_ci
29762306a36Sopenharmony_ci	if ((map->width != 8) || (map->height != 16) ||
29862306a36Sopenharmony_ci	    (map->depth != 1) || (map->length != 256)) {
29962306a36Sopenharmony_ci		fb_err(info, "unsupported font parameters: width %d, height %d, depth %d, length %d\n",
30062306a36Sopenharmony_ci		       map->width, map->height, map->depth, map->length);
30162306a36Sopenharmony_ci		return;
30262306a36Sopenharmony_ci	}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	fb += 2;
30562306a36Sopenharmony_ci	for (i = 0; i < map->height; i++) {
30662306a36Sopenharmony_ci		for (c = 0; c < map->length; c++) {
30762306a36Sopenharmony_ci			fb_writeb(font[c * map->height + i], fb + c * 4);
30862306a36Sopenharmony_ci		}
30962306a36Sopenharmony_ci		fb += 1024;
31062306a36Sopenharmony_ci	}
31162306a36Sopenharmony_ci}
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_cistatic void s3fb_tilecursor(struct fb_info *info, struct fb_tilecursor *cursor)
31462306a36Sopenharmony_ci{
31562306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	svga_tilecursor(par->state.vgabase, info, cursor);
31862306a36Sopenharmony_ci}
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_cistatic struct fb_tile_ops s3fb_tile_ops = {
32162306a36Sopenharmony_ci	.fb_settile	= svga_settile,
32262306a36Sopenharmony_ci	.fb_tilecopy	= svga_tilecopy,
32362306a36Sopenharmony_ci	.fb_tilefill    = svga_tilefill,
32462306a36Sopenharmony_ci	.fb_tileblit    = svga_tileblit,
32562306a36Sopenharmony_ci	.fb_tilecursor  = s3fb_tilecursor,
32662306a36Sopenharmony_ci	.fb_get_tilemax = svga_get_tilemax,
32762306a36Sopenharmony_ci};
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_cistatic struct fb_tile_ops s3fb_fast_tile_ops = {
33062306a36Sopenharmony_ci	.fb_settile	= s3fb_settile_fast,
33162306a36Sopenharmony_ci	.fb_tilecopy	= svga_tilecopy,
33262306a36Sopenharmony_ci	.fb_tilefill    = svga_tilefill,
33362306a36Sopenharmony_ci	.fb_tileblit    = svga_tileblit,
33462306a36Sopenharmony_ci	.fb_tilecursor  = s3fb_tilecursor,
33562306a36Sopenharmony_ci	.fb_get_tilemax = svga_get_tilemax,
33662306a36Sopenharmony_ci};
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci/* image data is MSB-first, fb structure is MSB-first too */
34262306a36Sopenharmony_cistatic inline u32 expand_color(u32 c)
34362306a36Sopenharmony_ci{
34462306a36Sopenharmony_ci	return ((c & 1) | ((c & 2) << 7) | ((c & 4) << 14) | ((c & 8) << 21)) * 0xFF;
34562306a36Sopenharmony_ci}
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci/* s3fb_iplan_imageblit silently assumes that almost everything is 8-pixel aligned */
34862306a36Sopenharmony_cistatic void s3fb_iplan_imageblit(struct fb_info *info, const struct fb_image *image)
34962306a36Sopenharmony_ci{
35062306a36Sopenharmony_ci	u32 fg = expand_color(image->fg_color);
35162306a36Sopenharmony_ci	u32 bg = expand_color(image->bg_color);
35262306a36Sopenharmony_ci	const u8 *src1, *src;
35362306a36Sopenharmony_ci	u8 __iomem *dst1;
35462306a36Sopenharmony_ci	u32 __iomem *dst;
35562306a36Sopenharmony_ci	u32 val;
35662306a36Sopenharmony_ci	int x, y;
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	src1 = image->data;
35962306a36Sopenharmony_ci	dst1 = info->screen_base + (image->dy * info->fix.line_length)
36062306a36Sopenharmony_ci		 + ((image->dx / 8) * 4);
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	for (y = 0; y < image->height; y++) {
36362306a36Sopenharmony_ci		src = src1;
36462306a36Sopenharmony_ci		dst = (u32 __iomem *) dst1;
36562306a36Sopenharmony_ci		for (x = 0; x < image->width; x += 8) {
36662306a36Sopenharmony_ci			val = *(src++) * 0x01010101;
36762306a36Sopenharmony_ci			val = (val & fg) | (~val & bg);
36862306a36Sopenharmony_ci			fb_writel(val, dst++);
36962306a36Sopenharmony_ci		}
37062306a36Sopenharmony_ci		src1 += image->width / 8;
37162306a36Sopenharmony_ci		dst1 += info->fix.line_length;
37262306a36Sopenharmony_ci	}
37362306a36Sopenharmony_ci
37462306a36Sopenharmony_ci}
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci/* s3fb_iplan_fillrect silently assumes that almost everything is 8-pixel aligned */
37762306a36Sopenharmony_cistatic void s3fb_iplan_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
37862306a36Sopenharmony_ci{
37962306a36Sopenharmony_ci	u32 fg = expand_color(rect->color);
38062306a36Sopenharmony_ci	u8 __iomem *dst1;
38162306a36Sopenharmony_ci	u32 __iomem *dst;
38262306a36Sopenharmony_ci	int x, y;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	dst1 = info->screen_base + (rect->dy * info->fix.line_length)
38562306a36Sopenharmony_ci		 + ((rect->dx / 8) * 4);
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	for (y = 0; y < rect->height; y++) {
38862306a36Sopenharmony_ci		dst = (u32 __iomem *) dst1;
38962306a36Sopenharmony_ci		for (x = 0; x < rect->width; x += 8) {
39062306a36Sopenharmony_ci			fb_writel(fg, dst++);
39162306a36Sopenharmony_ci		}
39262306a36Sopenharmony_ci		dst1 += info->fix.line_length;
39362306a36Sopenharmony_ci	}
39462306a36Sopenharmony_ci}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_ci/* image data is MSB-first, fb structure is high-nibble-in-low-byte-first */
39862306a36Sopenharmony_cistatic inline u32 expand_pixel(u32 c)
39962306a36Sopenharmony_ci{
40062306a36Sopenharmony_ci	return (((c &  1) << 24) | ((c &  2) << 27) | ((c &  4) << 14) | ((c &   8) << 17) |
40162306a36Sopenharmony_ci		((c & 16) <<  4) | ((c & 32) <<  7) | ((c & 64) >>  6) | ((c & 128) >>  3)) * 0xF;
40262306a36Sopenharmony_ci}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci/* s3fb_cfb4_imageblit silently assumes that almost everything is 8-pixel aligned */
40562306a36Sopenharmony_cistatic void s3fb_cfb4_imageblit(struct fb_info *info, const struct fb_image *image)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	u32 fg = image->fg_color * 0x11111111;
40862306a36Sopenharmony_ci	u32 bg = image->bg_color * 0x11111111;
40962306a36Sopenharmony_ci	const u8 *src1, *src;
41062306a36Sopenharmony_ci	u8 __iomem *dst1;
41162306a36Sopenharmony_ci	u32 __iomem *dst;
41262306a36Sopenharmony_ci	u32 val;
41362306a36Sopenharmony_ci	int x, y;
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	src1 = image->data;
41662306a36Sopenharmony_ci	dst1 = info->screen_base + (image->dy * info->fix.line_length)
41762306a36Sopenharmony_ci		 + ((image->dx / 8) * 4);
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	for (y = 0; y < image->height; y++) {
42062306a36Sopenharmony_ci		src = src1;
42162306a36Sopenharmony_ci		dst = (u32 __iomem *) dst1;
42262306a36Sopenharmony_ci		for (x = 0; x < image->width; x += 8) {
42362306a36Sopenharmony_ci			val = expand_pixel(*(src++));
42462306a36Sopenharmony_ci			val = (val & fg) | (~val & bg);
42562306a36Sopenharmony_ci			fb_writel(val, dst++);
42662306a36Sopenharmony_ci		}
42762306a36Sopenharmony_ci		src1 += image->width / 8;
42862306a36Sopenharmony_ci		dst1 += info->fix.line_length;
42962306a36Sopenharmony_ci	}
43062306a36Sopenharmony_ci}
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_cistatic void s3fb_imageblit(struct fb_info *info, const struct fb_image *image)
43362306a36Sopenharmony_ci{
43462306a36Sopenharmony_ci	if ((info->var.bits_per_pixel == 4) && (image->depth == 1)
43562306a36Sopenharmony_ci	    && ((image->width % 8) == 0) && ((image->dx % 8) == 0)) {
43662306a36Sopenharmony_ci		if (info->fix.type == FB_TYPE_INTERLEAVED_PLANES)
43762306a36Sopenharmony_ci			s3fb_iplan_imageblit(info, image);
43862306a36Sopenharmony_ci		else
43962306a36Sopenharmony_ci			s3fb_cfb4_imageblit(info, image);
44062306a36Sopenharmony_ci	} else
44162306a36Sopenharmony_ci		cfb_imageblit(info, image);
44262306a36Sopenharmony_ci}
44362306a36Sopenharmony_ci
44462306a36Sopenharmony_cistatic void s3fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
44562306a36Sopenharmony_ci{
44662306a36Sopenharmony_ci	if ((info->var.bits_per_pixel == 4)
44762306a36Sopenharmony_ci	    && ((rect->width % 8) == 0) && ((rect->dx % 8) == 0)
44862306a36Sopenharmony_ci	    && (info->fix.type == FB_TYPE_INTERLEAVED_PLANES))
44962306a36Sopenharmony_ci		s3fb_iplan_fillrect(info, rect);
45062306a36Sopenharmony_ci	 else
45162306a36Sopenharmony_ci		cfb_fillrect(info, rect);
45262306a36Sopenharmony_ci}
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_cistatic void s3_set_pixclock(struct fb_info *info, u32 pixclock)
46062306a36Sopenharmony_ci{
46162306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
46262306a36Sopenharmony_ci	u16 m, n, r;
46362306a36Sopenharmony_ci	u8 regval;
46462306a36Sopenharmony_ci	int rv;
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	rv = svga_compute_pll((par->chip == CHIP_365_TRIO3D) ? &s3_trio3d_pll : &s3_pll,
46762306a36Sopenharmony_ci			      1000000000 / pixclock, &m, &n, &r, info->node);
46862306a36Sopenharmony_ci	if (rv < 0) {
46962306a36Sopenharmony_ci		fb_err(info, "cannot set requested pixclock, keeping old value\n");
47062306a36Sopenharmony_ci		return;
47162306a36Sopenharmony_ci	}
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	/* Set VGA misc register  */
47462306a36Sopenharmony_ci	regval = vga_r(par->state.vgabase, VGA_MIS_R);
47562306a36Sopenharmony_ci	vga_w(par->state.vgabase, VGA_MIS_W, regval | VGA_MIS_ENB_PLL_LOAD);
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ci	/* Set S3 clock registers */
47862306a36Sopenharmony_ci	if (par->chip == CHIP_357_VIRGE_GX2 ||
47962306a36Sopenharmony_ci	    par->chip == CHIP_359_VIRGE_GX2P ||
48062306a36Sopenharmony_ci	    par->chip == CHIP_360_TRIO3D_1X ||
48162306a36Sopenharmony_ci	    par->chip == CHIP_362_TRIO3D_2X ||
48262306a36Sopenharmony_ci	    par->chip == CHIP_368_TRIO3D_2X ||
48362306a36Sopenharmony_ci	    par->chip == CHIP_260_VIRGE_MX) {
48462306a36Sopenharmony_ci		vga_wseq(par->state.vgabase, 0x12, (n - 2) | ((r & 3) << 6));	/* n and two bits of r */
48562306a36Sopenharmony_ci		vga_wseq(par->state.vgabase, 0x29, r >> 2); /* remaining highest bit of r */
48662306a36Sopenharmony_ci	} else
48762306a36Sopenharmony_ci		vga_wseq(par->state.vgabase, 0x12, (n - 2) | (r << 5));
48862306a36Sopenharmony_ci	vga_wseq(par->state.vgabase, 0x13, m - 2);
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci	udelay(1000);
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci	/* Activate clock - write 0, 1, 0 to seq/15 bit 5 */
49362306a36Sopenharmony_ci	regval = vga_rseq (par->state.vgabase, 0x15); /* | 0x80; */
49462306a36Sopenharmony_ci	vga_wseq(par->state.vgabase, 0x15, regval & ~(1<<5));
49562306a36Sopenharmony_ci	vga_wseq(par->state.vgabase, 0x15, regval |  (1<<5));
49662306a36Sopenharmony_ci	vga_wseq(par->state.vgabase, 0x15, regval & ~(1<<5));
49762306a36Sopenharmony_ci}
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci/* Open framebuffer */
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_cistatic int s3fb_open(struct fb_info *info, int user)
50362306a36Sopenharmony_ci{
50462306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci	mutex_lock(&(par->open_lock));
50762306a36Sopenharmony_ci	if (par->ref_count == 0) {
50862306a36Sopenharmony_ci		void __iomem *vgabase = par->state.vgabase;
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci		memset(&(par->state), 0, sizeof(struct vgastate));
51162306a36Sopenharmony_ci		par->state.vgabase = vgabase;
51262306a36Sopenharmony_ci		par->state.flags = VGA_SAVE_MODE | VGA_SAVE_FONTS | VGA_SAVE_CMAP;
51362306a36Sopenharmony_ci		par->state.num_crtc = 0x70;
51462306a36Sopenharmony_ci		par->state.num_seq = 0x20;
51562306a36Sopenharmony_ci		save_vga(&(par->state));
51662306a36Sopenharmony_ci	}
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci	par->ref_count++;
51962306a36Sopenharmony_ci	mutex_unlock(&(par->open_lock));
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	return 0;
52262306a36Sopenharmony_ci}
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci/* Close framebuffer */
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_cistatic int s3fb_release(struct fb_info *info, int user)
52762306a36Sopenharmony_ci{
52862306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	mutex_lock(&(par->open_lock));
53162306a36Sopenharmony_ci	if (par->ref_count == 0) {
53262306a36Sopenharmony_ci		mutex_unlock(&(par->open_lock));
53362306a36Sopenharmony_ci		return -EINVAL;
53462306a36Sopenharmony_ci	}
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ci	if (par->ref_count == 1)
53762306a36Sopenharmony_ci		restore_vga(&(par->state));
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci	par->ref_count--;
54062306a36Sopenharmony_ci	mutex_unlock(&(par->open_lock));
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci	return 0;
54362306a36Sopenharmony_ci}
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ci/* Validate passed in var */
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_cistatic int s3fb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
54862306a36Sopenharmony_ci{
54962306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
55062306a36Sopenharmony_ci	int rv, mem, step;
55162306a36Sopenharmony_ci	u16 m, n, r;
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci	if (!var->pixclock)
55462306a36Sopenharmony_ci		return -EINVAL;
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	/* Find appropriate format */
55762306a36Sopenharmony_ci	rv = svga_match_format (s3fb_formats, var, NULL);
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci	/* 32bpp mode is not supported on VIRGE VX,
56062306a36Sopenharmony_ci	   24bpp is not supported on others */
56162306a36Sopenharmony_ci	if ((par->chip == CHIP_988_VIRGE_VX) ? (rv == 7) : (rv == 6))
56262306a36Sopenharmony_ci		rv = -EINVAL;
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci	if (rv < 0) {
56562306a36Sopenharmony_ci		fb_err(info, "unsupported mode requested\n");
56662306a36Sopenharmony_ci		return rv;
56762306a36Sopenharmony_ci	}
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	/* Do not allow to have real resoulution larger than virtual */
57062306a36Sopenharmony_ci	if (var->xres > var->xres_virtual)
57162306a36Sopenharmony_ci		var->xres_virtual = var->xres;
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	if (var->yres > var->yres_virtual)
57462306a36Sopenharmony_ci		var->yres_virtual = var->yres;
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci	/* Round up xres_virtual to have proper alignment of lines */
57762306a36Sopenharmony_ci	step = s3fb_formats[rv].xresstep - 1;
57862306a36Sopenharmony_ci	var->xres_virtual = (var->xres_virtual+step) & ~step;
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci	/* Check whether have enough memory */
58162306a36Sopenharmony_ci	mem = ((var->bits_per_pixel * var->xres_virtual) >> 3) * var->yres_virtual;
58262306a36Sopenharmony_ci	if (mem > info->screen_size) {
58362306a36Sopenharmony_ci		fb_err(info, "not enough framebuffer memory (%d kB requested , %u kB available)\n",
58462306a36Sopenharmony_ci		       mem >> 10, (unsigned int) (info->screen_size >> 10));
58562306a36Sopenharmony_ci		return -EINVAL;
58662306a36Sopenharmony_ci	}
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci	rv = svga_check_timings (&s3_timing_regs, var, info->node);
58962306a36Sopenharmony_ci	if (rv < 0) {
59062306a36Sopenharmony_ci		fb_err(info, "invalid timings requested\n");
59162306a36Sopenharmony_ci		return rv;
59262306a36Sopenharmony_ci	}
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci	rv = svga_compute_pll(&s3_pll, PICOS2KHZ(var->pixclock), &m, &n, &r,
59562306a36Sopenharmony_ci				info->node);
59662306a36Sopenharmony_ci	if (rv < 0) {
59762306a36Sopenharmony_ci		fb_err(info, "invalid pixclock value requested\n");
59862306a36Sopenharmony_ci		return rv;
59962306a36Sopenharmony_ci	}
60062306a36Sopenharmony_ci
60162306a36Sopenharmony_ci	return 0;
60262306a36Sopenharmony_ci}
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_ci/* Set video mode from par */
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_cistatic int s3fb_set_par(struct fb_info *info)
60762306a36Sopenharmony_ci{
60862306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
60962306a36Sopenharmony_ci	u32 value, mode, hmul, offset_value, screen_size, multiplex, dbytes;
61062306a36Sopenharmony_ci	u32 bpp = info->var.bits_per_pixel;
61162306a36Sopenharmony_ci	u32 htotal, hsstart;
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci	if (bpp != 0) {
61462306a36Sopenharmony_ci		info->fix.ypanstep = 1;
61562306a36Sopenharmony_ci		info->fix.line_length = (info->var.xres_virtual * bpp) / 8;
61662306a36Sopenharmony_ci
61762306a36Sopenharmony_ci		info->flags &= ~FBINFO_MISC_TILEBLITTING;
61862306a36Sopenharmony_ci		info->tileops = NULL;
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci		/* in 4bpp supports 8p wide tiles only, any tiles otherwise */
62162306a36Sopenharmony_ci		info->pixmap.blit_x = (bpp == 4) ? (1 << (8 - 1)) : (~(u32)0);
62262306a36Sopenharmony_ci		info->pixmap.blit_y = ~(u32)0;
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ci		offset_value = (info->var.xres_virtual * bpp) / 64;
62562306a36Sopenharmony_ci		screen_size = info->var.yres_virtual * info->fix.line_length;
62662306a36Sopenharmony_ci	} else {
62762306a36Sopenharmony_ci		info->fix.ypanstep = 16;
62862306a36Sopenharmony_ci		info->fix.line_length = 0;
62962306a36Sopenharmony_ci
63062306a36Sopenharmony_ci		info->flags |= FBINFO_MISC_TILEBLITTING;
63162306a36Sopenharmony_ci		info->tileops = fasttext ? &s3fb_fast_tile_ops : &s3fb_tile_ops;
63262306a36Sopenharmony_ci
63362306a36Sopenharmony_ci		/* supports 8x16 tiles only */
63462306a36Sopenharmony_ci		info->pixmap.blit_x = 1 << (8 - 1);
63562306a36Sopenharmony_ci		info->pixmap.blit_y = 1 << (16 - 1);
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci		offset_value = info->var.xres_virtual / 16;
63862306a36Sopenharmony_ci		screen_size = (info->var.xres_virtual * info->var.yres_virtual) / 64;
63962306a36Sopenharmony_ci	}
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci	info->var.xoffset = 0;
64262306a36Sopenharmony_ci	info->var.yoffset = 0;
64362306a36Sopenharmony_ci	info->var.activate = FB_ACTIVATE_NOW;
64462306a36Sopenharmony_ci
64562306a36Sopenharmony_ci	/* Unlock registers */
64662306a36Sopenharmony_ci	vga_wcrt(par->state.vgabase, 0x38, 0x48);
64762306a36Sopenharmony_ci	vga_wcrt(par->state.vgabase, 0x39, 0xA5);
64862306a36Sopenharmony_ci	vga_wseq(par->state.vgabase, 0x08, 0x06);
64962306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x11, 0x00, 0x80);
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci	/* Blank screen and turn off sync */
65262306a36Sopenharmony_ci	svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
65362306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x17, 0x00, 0x80);
65462306a36Sopenharmony_ci
65562306a36Sopenharmony_ci	/* Set default values */
65662306a36Sopenharmony_ci	svga_set_default_gfx_regs(par->state.vgabase);
65762306a36Sopenharmony_ci	svga_set_default_atc_regs(par->state.vgabase);
65862306a36Sopenharmony_ci	svga_set_default_seq_regs(par->state.vgabase);
65962306a36Sopenharmony_ci	svga_set_default_crt_regs(par->state.vgabase);
66062306a36Sopenharmony_ci	svga_wcrt_multi(par->state.vgabase, s3_line_compare_regs, 0xFFFFFFFF);
66162306a36Sopenharmony_ci	svga_wcrt_multi(par->state.vgabase, s3_start_address_regs, 0);
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci	/* S3 specific initialization */
66462306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x58, 0x10, 0x10); /* enable linear framebuffer */
66562306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x31, 0x08, 0x08); /* enable sequencer access to framebuffer above 256 kB */
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_ci/*	svga_wcrt_mask(par->state.vgabase, 0x33, 0x08, 0x08); */ /* DDR ?	*/
66862306a36Sopenharmony_ci/*	svga_wcrt_mask(par->state.vgabase, 0x43, 0x01, 0x01); */ /* DDR ?	*/
66962306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x33, 0x00, 0x08); /* no DDR ?	*/
67062306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x43, 0x00, 0x01); /* no DDR ?	*/
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x5D, 0x00, 0x28); /* Clear strange HSlen bits */
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_ci/*	svga_wcrt_mask(par->state.vgabase, 0x58, 0x03, 0x03); */
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_ci/*	svga_wcrt_mask(par->state.vgabase, 0x53, 0x12, 0x13); */ /* enable MMIO */
67762306a36Sopenharmony_ci/*	svga_wcrt_mask(par->state.vgabase, 0x40, 0x08, 0x08); */ /* enable write buffer */
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci	/* Set the offset register */
68162306a36Sopenharmony_ci	fb_dbg(info, "offset register       : %d\n", offset_value);
68262306a36Sopenharmony_ci	svga_wcrt_multi(par->state.vgabase, s3_offset_regs, offset_value);
68362306a36Sopenharmony_ci
68462306a36Sopenharmony_ci	if (par->chip != CHIP_357_VIRGE_GX2 &&
68562306a36Sopenharmony_ci	    par->chip != CHIP_359_VIRGE_GX2P &&
68662306a36Sopenharmony_ci	    par->chip != CHIP_360_TRIO3D_1X &&
68762306a36Sopenharmony_ci	    par->chip != CHIP_362_TRIO3D_2X &&
68862306a36Sopenharmony_ci	    par->chip != CHIP_368_TRIO3D_2X &&
68962306a36Sopenharmony_ci	    par->chip != CHIP_260_VIRGE_MX) {
69062306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x54, 0x18); /* M parameter */
69162306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x60, 0xff); /* N parameter */
69262306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x61, 0xff); /* L parameter */
69362306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x62, 0xff); /* L parameter */
69462306a36Sopenharmony_ci	}
69562306a36Sopenharmony_ci
69662306a36Sopenharmony_ci	vga_wcrt(par->state.vgabase, 0x3A, 0x35);
69762306a36Sopenharmony_ci	svga_wattr(par->state.vgabase, 0x33, 0x00);
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci	if (info->var.vmode & FB_VMODE_DOUBLE)
70062306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x09, 0x80, 0x80);
70162306a36Sopenharmony_ci	else
70262306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x09, 0x00, 0x80);
70362306a36Sopenharmony_ci
70462306a36Sopenharmony_ci	if (info->var.vmode & FB_VMODE_INTERLACED)
70562306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x42, 0x20, 0x20);
70662306a36Sopenharmony_ci	else
70762306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x42, 0x00, 0x20);
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	/* Disable hardware graphics cursor */
71062306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x45, 0x00, 0x01);
71162306a36Sopenharmony_ci	/* Disable Streams engine */
71262306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0x0C);
71362306a36Sopenharmony_ci
71462306a36Sopenharmony_ci	mode = svga_match_format(s3fb_formats, &(info->var), &(info->fix));
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci	/* S3 virge DX hack */
71762306a36Sopenharmony_ci	if (par->chip == CHIP_375_VIRGE_DX) {
71862306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x86, 0x80);
71962306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x90, 0x00);
72062306a36Sopenharmony_ci	}
72162306a36Sopenharmony_ci
72262306a36Sopenharmony_ci	/* S3 virge VX hack */
72362306a36Sopenharmony_ci	if (par->chip == CHIP_988_VIRGE_VX) {
72462306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x50, 0x00);
72562306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x67, 0x50);
72662306a36Sopenharmony_ci		msleep(10); /* screen remains blank sometimes without this */
72762306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x63, (mode <= 2) ? 0x90 : 0x09);
72862306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x66, 0x90);
72962306a36Sopenharmony_ci	}
73062306a36Sopenharmony_ci
73162306a36Sopenharmony_ci	if (par->chip == CHIP_357_VIRGE_GX2 ||
73262306a36Sopenharmony_ci	    par->chip == CHIP_359_VIRGE_GX2P ||
73362306a36Sopenharmony_ci	    par->chip == CHIP_360_TRIO3D_1X ||
73462306a36Sopenharmony_ci	    par->chip == CHIP_362_TRIO3D_2X ||
73562306a36Sopenharmony_ci	    par->chip == CHIP_368_TRIO3D_2X ||
73662306a36Sopenharmony_ci	    par->chip == CHIP_365_TRIO3D    ||
73762306a36Sopenharmony_ci	    par->chip == CHIP_375_VIRGE_DX  ||
73862306a36Sopenharmony_ci	    par->chip == CHIP_385_VIRGE_GX  ||
73962306a36Sopenharmony_ci	    par->chip == CHIP_260_VIRGE_MX) {
74062306a36Sopenharmony_ci		dbytes = info->var.xres * ((bpp+7)/8);
74162306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x91, (dbytes + 7) / 8);
74262306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x90, (((dbytes + 7) / 8) >> 8) | 0x80);
74362306a36Sopenharmony_ci
74462306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x66, 0x81);
74562306a36Sopenharmony_ci	}
74662306a36Sopenharmony_ci
74762306a36Sopenharmony_ci	if (par->chip == CHIP_357_VIRGE_GX2  ||
74862306a36Sopenharmony_ci	    par->chip == CHIP_359_VIRGE_GX2P ||
74962306a36Sopenharmony_ci	    par->chip == CHIP_360_TRIO3D_1X ||
75062306a36Sopenharmony_ci	    par->chip == CHIP_362_TRIO3D_2X ||
75162306a36Sopenharmony_ci	    par->chip == CHIP_368_TRIO3D_2X ||
75262306a36Sopenharmony_ci	    par->chip == CHIP_260_VIRGE_MX)
75362306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x34, 0x00);
75462306a36Sopenharmony_ci	else	/* enable Data Transfer Position Control (DTPC) */
75562306a36Sopenharmony_ci		vga_wcrt(par->state.vgabase, 0x34, 0x10);
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x31, 0x00, 0x40);
75862306a36Sopenharmony_ci	multiplex = 0;
75962306a36Sopenharmony_ci	hmul = 1;
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci	/* Set mode-specific register values */
76262306a36Sopenharmony_ci	switch (mode) {
76362306a36Sopenharmony_ci	case 0:
76462306a36Sopenharmony_ci		fb_dbg(info, "text mode\n");
76562306a36Sopenharmony_ci		svga_set_textmode_vga_regs(par->state.vgabase);
76662306a36Sopenharmony_ci
76762306a36Sopenharmony_ci		/* Set additional registers like in 8-bit mode */
76862306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x50, 0x00, 0x30);
76962306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0xF0);
77062306a36Sopenharmony_ci
77162306a36Sopenharmony_ci		/* Disable enhanced mode */
77262306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x3A, 0x00, 0x30);
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci		if (fasttext) {
77562306a36Sopenharmony_ci			fb_dbg(info, "high speed text mode set\n");
77662306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x31, 0x40, 0x40);
77762306a36Sopenharmony_ci		}
77862306a36Sopenharmony_ci		break;
77962306a36Sopenharmony_ci	case 1:
78062306a36Sopenharmony_ci		fb_dbg(info, "4 bit pseudocolor\n");
78162306a36Sopenharmony_ci		vga_wgfx(par->state.vgabase, VGA_GFX_MODE, 0x40);
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci		/* Set additional registers like in 8-bit mode */
78462306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x50, 0x00, 0x30);
78562306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0xF0);
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci		/* disable enhanced mode */
78862306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x3A, 0x00, 0x30);
78962306a36Sopenharmony_ci		break;
79062306a36Sopenharmony_ci	case 2:
79162306a36Sopenharmony_ci		fb_dbg(info, "4 bit pseudocolor, planar\n");
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci		/* Set additional registers like in 8-bit mode */
79462306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x50, 0x00, 0x30);
79562306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0xF0);
79662306a36Sopenharmony_ci
79762306a36Sopenharmony_ci		/* disable enhanced mode */
79862306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x3A, 0x00, 0x30);
79962306a36Sopenharmony_ci		break;
80062306a36Sopenharmony_ci	case 3:
80162306a36Sopenharmony_ci		fb_dbg(info, "8 bit pseudocolor\n");
80262306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x50, 0x00, 0x30);
80362306a36Sopenharmony_ci		if (info->var.pixclock > 20000 ||
80462306a36Sopenharmony_ci		    par->chip == CHIP_357_VIRGE_GX2 ||
80562306a36Sopenharmony_ci		    par->chip == CHIP_359_VIRGE_GX2P ||
80662306a36Sopenharmony_ci		    par->chip == CHIP_360_TRIO3D_1X ||
80762306a36Sopenharmony_ci		    par->chip == CHIP_362_TRIO3D_2X ||
80862306a36Sopenharmony_ci		    par->chip == CHIP_368_TRIO3D_2X ||
80962306a36Sopenharmony_ci		    par->chip == CHIP_260_VIRGE_MX)
81062306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x67, 0x00, 0xF0);
81162306a36Sopenharmony_ci		else {
81262306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x67, 0x10, 0xF0);
81362306a36Sopenharmony_ci			multiplex = 1;
81462306a36Sopenharmony_ci		}
81562306a36Sopenharmony_ci		break;
81662306a36Sopenharmony_ci	case 4:
81762306a36Sopenharmony_ci		fb_dbg(info, "5/5/5 truecolor\n");
81862306a36Sopenharmony_ci		if (par->chip == CHIP_988_VIRGE_VX) {
81962306a36Sopenharmony_ci			if (info->var.pixclock > 20000)
82062306a36Sopenharmony_ci				svga_wcrt_mask(par->state.vgabase, 0x67, 0x20, 0xF0);
82162306a36Sopenharmony_ci			else
82262306a36Sopenharmony_ci				svga_wcrt_mask(par->state.vgabase, 0x67, 0x30, 0xF0);
82362306a36Sopenharmony_ci		} else if (par->chip == CHIP_365_TRIO3D) {
82462306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x50, 0x10, 0x30);
82562306a36Sopenharmony_ci			if (info->var.pixclock > 8695) {
82662306a36Sopenharmony_ci				svga_wcrt_mask(par->state.vgabase, 0x67, 0x30, 0xF0);
82762306a36Sopenharmony_ci				hmul = 2;
82862306a36Sopenharmony_ci			} else {
82962306a36Sopenharmony_ci				svga_wcrt_mask(par->state.vgabase, 0x67, 0x20, 0xF0);
83062306a36Sopenharmony_ci				multiplex = 1;
83162306a36Sopenharmony_ci			}
83262306a36Sopenharmony_ci		} else {
83362306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x50, 0x10, 0x30);
83462306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x67, 0x30, 0xF0);
83562306a36Sopenharmony_ci			if (par->chip != CHIP_357_VIRGE_GX2 &&
83662306a36Sopenharmony_ci			    par->chip != CHIP_359_VIRGE_GX2P &&
83762306a36Sopenharmony_ci			    par->chip != CHIP_360_TRIO3D_1X &&
83862306a36Sopenharmony_ci			    par->chip != CHIP_362_TRIO3D_2X &&
83962306a36Sopenharmony_ci			    par->chip != CHIP_368_TRIO3D_2X &&
84062306a36Sopenharmony_ci			    par->chip != CHIP_260_VIRGE_MX)
84162306a36Sopenharmony_ci				hmul = 2;
84262306a36Sopenharmony_ci		}
84362306a36Sopenharmony_ci		break;
84462306a36Sopenharmony_ci	case 5:
84562306a36Sopenharmony_ci		fb_dbg(info, "5/6/5 truecolor\n");
84662306a36Sopenharmony_ci		if (par->chip == CHIP_988_VIRGE_VX) {
84762306a36Sopenharmony_ci			if (info->var.pixclock > 20000)
84862306a36Sopenharmony_ci				svga_wcrt_mask(par->state.vgabase, 0x67, 0x40, 0xF0);
84962306a36Sopenharmony_ci			else
85062306a36Sopenharmony_ci				svga_wcrt_mask(par->state.vgabase, 0x67, 0x50, 0xF0);
85162306a36Sopenharmony_ci		} else if (par->chip == CHIP_365_TRIO3D) {
85262306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x50, 0x10, 0x30);
85362306a36Sopenharmony_ci			if (info->var.pixclock > 8695) {
85462306a36Sopenharmony_ci				svga_wcrt_mask(par->state.vgabase, 0x67, 0x50, 0xF0);
85562306a36Sopenharmony_ci				hmul = 2;
85662306a36Sopenharmony_ci			} else {
85762306a36Sopenharmony_ci				svga_wcrt_mask(par->state.vgabase, 0x67, 0x40, 0xF0);
85862306a36Sopenharmony_ci				multiplex = 1;
85962306a36Sopenharmony_ci			}
86062306a36Sopenharmony_ci		} else {
86162306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x50, 0x10, 0x30);
86262306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x67, 0x50, 0xF0);
86362306a36Sopenharmony_ci			if (par->chip != CHIP_357_VIRGE_GX2 &&
86462306a36Sopenharmony_ci			    par->chip != CHIP_359_VIRGE_GX2P &&
86562306a36Sopenharmony_ci			    par->chip != CHIP_360_TRIO3D_1X &&
86662306a36Sopenharmony_ci			    par->chip != CHIP_362_TRIO3D_2X &&
86762306a36Sopenharmony_ci			    par->chip != CHIP_368_TRIO3D_2X &&
86862306a36Sopenharmony_ci			    par->chip != CHIP_260_VIRGE_MX)
86962306a36Sopenharmony_ci				hmul = 2;
87062306a36Sopenharmony_ci		}
87162306a36Sopenharmony_ci		break;
87262306a36Sopenharmony_ci	case 6:
87362306a36Sopenharmony_ci		/* VIRGE VX case */
87462306a36Sopenharmony_ci		fb_dbg(info, "8/8/8 truecolor\n");
87562306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x67, 0xD0, 0xF0);
87662306a36Sopenharmony_ci		break;
87762306a36Sopenharmony_ci	case 7:
87862306a36Sopenharmony_ci		fb_dbg(info, "8/8/8/8 truecolor\n");
87962306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x50, 0x30, 0x30);
88062306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x67, 0xD0, 0xF0);
88162306a36Sopenharmony_ci		break;
88262306a36Sopenharmony_ci	default:
88362306a36Sopenharmony_ci		fb_err(info, "unsupported mode - bug\n");
88462306a36Sopenharmony_ci		return -EINVAL;
88562306a36Sopenharmony_ci	}
88662306a36Sopenharmony_ci
88762306a36Sopenharmony_ci	if (par->chip != CHIP_988_VIRGE_VX) {
88862306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x15, multiplex ? 0x10 : 0x00, 0x10);
88962306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x18, multiplex ? 0x80 : 0x00, 0x80);
89062306a36Sopenharmony_ci	}
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ci	s3_set_pixclock(info, info->var.pixclock);
89362306a36Sopenharmony_ci	svga_set_timings(par->state.vgabase, &s3_timing_regs, &(info->var), hmul, 1,
89462306a36Sopenharmony_ci			 (info->var.vmode & FB_VMODE_DOUBLE)     ? 2 : 1,
89562306a36Sopenharmony_ci			 (info->var.vmode & FB_VMODE_INTERLACED) ? 2 : 1,
89662306a36Sopenharmony_ci			 hmul, info->node);
89762306a36Sopenharmony_ci
89862306a36Sopenharmony_ci	/* Set interlaced mode start/end register */
89962306a36Sopenharmony_ci	htotal = info->var.xres + info->var.left_margin + info->var.right_margin + info->var.hsync_len;
90062306a36Sopenharmony_ci	htotal = ((htotal * hmul) / 8) - 5;
90162306a36Sopenharmony_ci	vga_wcrt(par->state.vgabase, 0x3C, (htotal + 1) / 2);
90262306a36Sopenharmony_ci
90362306a36Sopenharmony_ci	/* Set Data Transfer Position */
90462306a36Sopenharmony_ci	hsstart = ((info->var.xres + info->var.right_margin) * hmul) / 8;
90562306a36Sopenharmony_ci	/* + 2 is needed for Virge/VX, does no harm on other cards */
90662306a36Sopenharmony_ci	value = clamp((htotal + hsstart + 1) / 2 + 2, hsstart + 4, htotal + 1);
90762306a36Sopenharmony_ci	svga_wcrt_multi(par->state.vgabase, s3_dtpc_regs, value);
90862306a36Sopenharmony_ci
90962306a36Sopenharmony_ci	if (screen_size > info->screen_size)
91062306a36Sopenharmony_ci		screen_size = info->screen_size;
91162306a36Sopenharmony_ci	memset_io(info->screen_base, 0x00, screen_size);
91262306a36Sopenharmony_ci	/* Device and screen back on */
91362306a36Sopenharmony_ci	svga_wcrt_mask(par->state.vgabase, 0x17, 0x80, 0x80);
91462306a36Sopenharmony_ci	svga_wseq_mask(par->state.vgabase, 0x01, 0x00, 0x20);
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ci	return 0;
91762306a36Sopenharmony_ci}
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_ci/* Set a colour register */
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_cistatic int s3fb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
92262306a36Sopenharmony_ci				u_int transp, struct fb_info *fb)
92362306a36Sopenharmony_ci{
92462306a36Sopenharmony_ci	switch (fb->var.bits_per_pixel) {
92562306a36Sopenharmony_ci	case 0:
92662306a36Sopenharmony_ci	case 4:
92762306a36Sopenharmony_ci		if (regno >= 16)
92862306a36Sopenharmony_ci			return -EINVAL;
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci		if ((fb->var.bits_per_pixel == 4) &&
93162306a36Sopenharmony_ci		    (fb->var.nonstd == 0)) {
93262306a36Sopenharmony_ci			outb(0xF0, VGA_PEL_MSK);
93362306a36Sopenharmony_ci			outb(regno*16, VGA_PEL_IW);
93462306a36Sopenharmony_ci		} else {
93562306a36Sopenharmony_ci			outb(0x0F, VGA_PEL_MSK);
93662306a36Sopenharmony_ci			outb(regno, VGA_PEL_IW);
93762306a36Sopenharmony_ci		}
93862306a36Sopenharmony_ci		outb(red >> 10, VGA_PEL_D);
93962306a36Sopenharmony_ci		outb(green >> 10, VGA_PEL_D);
94062306a36Sopenharmony_ci		outb(blue >> 10, VGA_PEL_D);
94162306a36Sopenharmony_ci		break;
94262306a36Sopenharmony_ci	case 8:
94362306a36Sopenharmony_ci		if (regno >= 256)
94462306a36Sopenharmony_ci			return -EINVAL;
94562306a36Sopenharmony_ci
94662306a36Sopenharmony_ci		outb(0xFF, VGA_PEL_MSK);
94762306a36Sopenharmony_ci		outb(regno, VGA_PEL_IW);
94862306a36Sopenharmony_ci		outb(red >> 10, VGA_PEL_D);
94962306a36Sopenharmony_ci		outb(green >> 10, VGA_PEL_D);
95062306a36Sopenharmony_ci		outb(blue >> 10, VGA_PEL_D);
95162306a36Sopenharmony_ci		break;
95262306a36Sopenharmony_ci	case 16:
95362306a36Sopenharmony_ci		if (regno >= 16)
95462306a36Sopenharmony_ci			return 0;
95562306a36Sopenharmony_ci
95662306a36Sopenharmony_ci		if (fb->var.green.length == 5)
95762306a36Sopenharmony_ci			((u32*)fb->pseudo_palette)[regno] = ((red & 0xF800) >> 1) |
95862306a36Sopenharmony_ci				((green & 0xF800) >> 6) | ((blue & 0xF800) >> 11);
95962306a36Sopenharmony_ci		else if (fb->var.green.length == 6)
96062306a36Sopenharmony_ci			((u32*)fb->pseudo_palette)[regno] = (red & 0xF800) |
96162306a36Sopenharmony_ci				((green & 0xFC00) >> 5) | ((blue & 0xF800) >> 11);
96262306a36Sopenharmony_ci		else return -EINVAL;
96362306a36Sopenharmony_ci		break;
96462306a36Sopenharmony_ci	case 24:
96562306a36Sopenharmony_ci	case 32:
96662306a36Sopenharmony_ci		if (regno >= 16)
96762306a36Sopenharmony_ci			return 0;
96862306a36Sopenharmony_ci
96962306a36Sopenharmony_ci		((u32*)fb->pseudo_palette)[regno] = ((red & 0xFF00) << 8) |
97062306a36Sopenharmony_ci			(green & 0xFF00) | ((blue & 0xFF00) >> 8);
97162306a36Sopenharmony_ci		break;
97262306a36Sopenharmony_ci	default:
97362306a36Sopenharmony_ci		return -EINVAL;
97462306a36Sopenharmony_ci	}
97562306a36Sopenharmony_ci
97662306a36Sopenharmony_ci	return 0;
97762306a36Sopenharmony_ci}
97862306a36Sopenharmony_ci
97962306a36Sopenharmony_ci
98062306a36Sopenharmony_ci/* Set the display blanking state */
98162306a36Sopenharmony_ci
98262306a36Sopenharmony_cistatic int s3fb_blank(int blank_mode, struct fb_info *info)
98362306a36Sopenharmony_ci{
98462306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
98562306a36Sopenharmony_ci
98662306a36Sopenharmony_ci	switch (blank_mode) {
98762306a36Sopenharmony_ci	case FB_BLANK_UNBLANK:
98862306a36Sopenharmony_ci		fb_dbg(info, "unblank\n");
98962306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x56, 0x00, 0x06);
99062306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x01, 0x00, 0x20);
99162306a36Sopenharmony_ci		break;
99262306a36Sopenharmony_ci	case FB_BLANK_NORMAL:
99362306a36Sopenharmony_ci		fb_dbg(info, "blank\n");
99462306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x56, 0x00, 0x06);
99562306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
99662306a36Sopenharmony_ci		break;
99762306a36Sopenharmony_ci	case FB_BLANK_HSYNC_SUSPEND:
99862306a36Sopenharmony_ci		fb_dbg(info, "hsync\n");
99962306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x56, 0x02, 0x06);
100062306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
100162306a36Sopenharmony_ci		break;
100262306a36Sopenharmony_ci	case FB_BLANK_VSYNC_SUSPEND:
100362306a36Sopenharmony_ci		fb_dbg(info, "vsync\n");
100462306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x56, 0x04, 0x06);
100562306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
100662306a36Sopenharmony_ci		break;
100762306a36Sopenharmony_ci	case FB_BLANK_POWERDOWN:
100862306a36Sopenharmony_ci		fb_dbg(info, "sync down\n");
100962306a36Sopenharmony_ci		svga_wcrt_mask(par->state.vgabase, 0x56, 0x06, 0x06);
101062306a36Sopenharmony_ci		svga_wseq_mask(par->state.vgabase, 0x01, 0x20, 0x20);
101162306a36Sopenharmony_ci		break;
101262306a36Sopenharmony_ci	}
101362306a36Sopenharmony_ci
101462306a36Sopenharmony_ci	return 0;
101562306a36Sopenharmony_ci}
101662306a36Sopenharmony_ci
101762306a36Sopenharmony_ci
101862306a36Sopenharmony_ci/* Pan the display */
101962306a36Sopenharmony_ci
102062306a36Sopenharmony_cistatic int s3fb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
102162306a36Sopenharmony_ci{
102262306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
102362306a36Sopenharmony_ci	unsigned int offset;
102462306a36Sopenharmony_ci
102562306a36Sopenharmony_ci	/* Calculate the offset */
102662306a36Sopenharmony_ci	if (info->var.bits_per_pixel == 0) {
102762306a36Sopenharmony_ci		offset = (var->yoffset / 16) * (info->var.xres_virtual / 2)
102862306a36Sopenharmony_ci		       + (var->xoffset / 2);
102962306a36Sopenharmony_ci		offset = offset >> 2;
103062306a36Sopenharmony_ci	} else {
103162306a36Sopenharmony_ci		offset = (var->yoffset * info->fix.line_length) +
103262306a36Sopenharmony_ci			 (var->xoffset * info->var.bits_per_pixel / 8);
103362306a36Sopenharmony_ci		offset = offset >> 2;
103462306a36Sopenharmony_ci	}
103562306a36Sopenharmony_ci
103662306a36Sopenharmony_ci	/* Set the offset */
103762306a36Sopenharmony_ci	svga_wcrt_multi(par->state.vgabase, s3_start_address_regs, offset);
103862306a36Sopenharmony_ci
103962306a36Sopenharmony_ci	return 0;
104062306a36Sopenharmony_ci}
104162306a36Sopenharmony_ci
104262306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci/* Frame buffer operations */
104562306a36Sopenharmony_ci
104662306a36Sopenharmony_cistatic const struct fb_ops s3fb_ops = {
104762306a36Sopenharmony_ci	.owner		= THIS_MODULE,
104862306a36Sopenharmony_ci	.fb_open	= s3fb_open,
104962306a36Sopenharmony_ci	.fb_release	= s3fb_release,
105062306a36Sopenharmony_ci	.fb_check_var	= s3fb_check_var,
105162306a36Sopenharmony_ci	.fb_set_par	= s3fb_set_par,
105262306a36Sopenharmony_ci	.fb_setcolreg	= s3fb_setcolreg,
105362306a36Sopenharmony_ci	.fb_blank	= s3fb_blank,
105462306a36Sopenharmony_ci	.fb_pan_display	= s3fb_pan_display,
105562306a36Sopenharmony_ci	.fb_fillrect	= s3fb_fillrect,
105662306a36Sopenharmony_ci	.fb_copyarea	= cfb_copyarea,
105762306a36Sopenharmony_ci	.fb_imageblit	= s3fb_imageblit,
105862306a36Sopenharmony_ci	.fb_get_caps    = svga_get_caps,
105962306a36Sopenharmony_ci};
106062306a36Sopenharmony_ci
106162306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
106262306a36Sopenharmony_ci
106362306a36Sopenharmony_cistatic int s3_identification(struct s3fb_info *par)
106462306a36Sopenharmony_ci{
106562306a36Sopenharmony_ci	int chip = par->chip;
106662306a36Sopenharmony_ci
106762306a36Sopenharmony_ci	if (chip == CHIP_XXX_TRIO) {
106862306a36Sopenharmony_ci		u8 cr30 = vga_rcrt(par->state.vgabase, 0x30);
106962306a36Sopenharmony_ci		u8 cr2e = vga_rcrt(par->state.vgabase, 0x2e);
107062306a36Sopenharmony_ci		u8 cr2f = vga_rcrt(par->state.vgabase, 0x2f);
107162306a36Sopenharmony_ci
107262306a36Sopenharmony_ci		if ((cr30 == 0xE0) || (cr30 == 0xE1)) {
107362306a36Sopenharmony_ci			if (cr2e == 0x10)
107462306a36Sopenharmony_ci				return CHIP_732_TRIO32;
107562306a36Sopenharmony_ci			if (cr2e == 0x11) {
107662306a36Sopenharmony_ci				if (! (cr2f & 0x40))
107762306a36Sopenharmony_ci					return CHIP_764_TRIO64;
107862306a36Sopenharmony_ci				else
107962306a36Sopenharmony_ci					return CHIP_765_TRIO64VP;
108062306a36Sopenharmony_ci			}
108162306a36Sopenharmony_ci		}
108262306a36Sopenharmony_ci	}
108362306a36Sopenharmony_ci
108462306a36Sopenharmony_ci	if (chip == CHIP_XXX_TRIO64V2_DXGX) {
108562306a36Sopenharmony_ci		u8 cr6f = vga_rcrt(par->state.vgabase, 0x6f);
108662306a36Sopenharmony_ci
108762306a36Sopenharmony_ci		if (! (cr6f & 0x01))
108862306a36Sopenharmony_ci			return CHIP_775_TRIO64V2_DX;
108962306a36Sopenharmony_ci		else
109062306a36Sopenharmony_ci			return CHIP_785_TRIO64V2_GX;
109162306a36Sopenharmony_ci	}
109262306a36Sopenharmony_ci
109362306a36Sopenharmony_ci	if (chip == CHIP_XXX_VIRGE_DXGX) {
109462306a36Sopenharmony_ci		u8 cr6f = vga_rcrt(par->state.vgabase, 0x6f);
109562306a36Sopenharmony_ci
109662306a36Sopenharmony_ci		if (! (cr6f & 0x01))
109762306a36Sopenharmony_ci			return CHIP_375_VIRGE_DX;
109862306a36Sopenharmony_ci		else
109962306a36Sopenharmony_ci			return CHIP_385_VIRGE_GX;
110062306a36Sopenharmony_ci	}
110162306a36Sopenharmony_ci
110262306a36Sopenharmony_ci	if (chip == CHIP_36X_TRIO3D_1X_2X) {
110362306a36Sopenharmony_ci		switch (vga_rcrt(par->state.vgabase, 0x2f)) {
110462306a36Sopenharmony_ci		case 0x00:
110562306a36Sopenharmony_ci			return CHIP_360_TRIO3D_1X;
110662306a36Sopenharmony_ci		case 0x01:
110762306a36Sopenharmony_ci			return CHIP_362_TRIO3D_2X;
110862306a36Sopenharmony_ci		case 0x02:
110962306a36Sopenharmony_ci			return CHIP_368_TRIO3D_2X;
111062306a36Sopenharmony_ci		}
111162306a36Sopenharmony_ci	}
111262306a36Sopenharmony_ci
111362306a36Sopenharmony_ci	return CHIP_UNKNOWN;
111462306a36Sopenharmony_ci}
111562306a36Sopenharmony_ci
111662306a36Sopenharmony_ci
111762306a36Sopenharmony_ci/* PCI probe */
111862306a36Sopenharmony_ci
111962306a36Sopenharmony_cistatic int s3_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
112062306a36Sopenharmony_ci{
112162306a36Sopenharmony_ci	struct pci_bus_region bus_reg;
112262306a36Sopenharmony_ci	struct resource vga_res;
112362306a36Sopenharmony_ci	struct fb_info *info;
112462306a36Sopenharmony_ci	struct s3fb_info *par;
112562306a36Sopenharmony_ci	int rc;
112662306a36Sopenharmony_ci	u8 regval, cr38, cr39;
112762306a36Sopenharmony_ci	bool found = false;
112862306a36Sopenharmony_ci
112962306a36Sopenharmony_ci	/* Ignore secondary VGA device because there is no VGA arbitration */
113062306a36Sopenharmony_ci	if (! svga_primary_device(dev)) {
113162306a36Sopenharmony_ci		dev_info(&(dev->dev), "ignoring secondary device\n");
113262306a36Sopenharmony_ci		return -ENODEV;
113362306a36Sopenharmony_ci	}
113462306a36Sopenharmony_ci
113562306a36Sopenharmony_ci	rc = aperture_remove_conflicting_pci_devices(dev, "s3fb");
113662306a36Sopenharmony_ci	if (rc)
113762306a36Sopenharmony_ci		return rc;
113862306a36Sopenharmony_ci
113962306a36Sopenharmony_ci	/* Allocate and fill driver data structure */
114062306a36Sopenharmony_ci	info = framebuffer_alloc(sizeof(struct s3fb_info), &(dev->dev));
114162306a36Sopenharmony_ci	if (!info)
114262306a36Sopenharmony_ci		return -ENOMEM;
114362306a36Sopenharmony_ci
114462306a36Sopenharmony_ci	par = info->par;
114562306a36Sopenharmony_ci	mutex_init(&par->open_lock);
114662306a36Sopenharmony_ci
114762306a36Sopenharmony_ci	info->flags = FBINFO_PARTIAL_PAN_OK | FBINFO_HWACCEL_YPAN;
114862306a36Sopenharmony_ci	info->fbops = &s3fb_ops;
114962306a36Sopenharmony_ci
115062306a36Sopenharmony_ci	/* Prepare PCI device */
115162306a36Sopenharmony_ci	rc = pci_enable_device(dev);
115262306a36Sopenharmony_ci	if (rc < 0) {
115362306a36Sopenharmony_ci		dev_err(info->device, "cannot enable PCI device\n");
115462306a36Sopenharmony_ci		goto err_enable_device;
115562306a36Sopenharmony_ci	}
115662306a36Sopenharmony_ci
115762306a36Sopenharmony_ci	rc = pci_request_regions(dev, "s3fb");
115862306a36Sopenharmony_ci	if (rc < 0) {
115962306a36Sopenharmony_ci		dev_err(info->device, "cannot reserve framebuffer region\n");
116062306a36Sopenharmony_ci		goto err_request_regions;
116162306a36Sopenharmony_ci	}
116262306a36Sopenharmony_ci
116362306a36Sopenharmony_ci
116462306a36Sopenharmony_ci	info->fix.smem_start = pci_resource_start(dev, 0);
116562306a36Sopenharmony_ci	info->fix.smem_len = pci_resource_len(dev, 0);
116662306a36Sopenharmony_ci
116762306a36Sopenharmony_ci	/* Map physical IO memory address into kernel space */
116862306a36Sopenharmony_ci	info->screen_base = pci_iomap_wc(dev, 0, 0);
116962306a36Sopenharmony_ci	if (! info->screen_base) {
117062306a36Sopenharmony_ci		rc = -ENOMEM;
117162306a36Sopenharmony_ci		dev_err(info->device, "iomap for framebuffer failed\n");
117262306a36Sopenharmony_ci		goto err_iomap;
117362306a36Sopenharmony_ci	}
117462306a36Sopenharmony_ci
117562306a36Sopenharmony_ci	bus_reg.start = 0;
117662306a36Sopenharmony_ci	bus_reg.end = 64 * 1024;
117762306a36Sopenharmony_ci
117862306a36Sopenharmony_ci	vga_res.flags = IORESOURCE_IO;
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci	pcibios_bus_to_resource(dev->bus, &vga_res, &bus_reg);
118162306a36Sopenharmony_ci
118262306a36Sopenharmony_ci	par->state.vgabase = (void __iomem *) (unsigned long) vga_res.start;
118362306a36Sopenharmony_ci
118462306a36Sopenharmony_ci	/* Unlock regs */
118562306a36Sopenharmony_ci	cr38 = vga_rcrt(par->state.vgabase, 0x38);
118662306a36Sopenharmony_ci	cr39 = vga_rcrt(par->state.vgabase, 0x39);
118762306a36Sopenharmony_ci	vga_wseq(par->state.vgabase, 0x08, 0x06);
118862306a36Sopenharmony_ci	vga_wcrt(par->state.vgabase, 0x38, 0x48);
118962306a36Sopenharmony_ci	vga_wcrt(par->state.vgabase, 0x39, 0xA5);
119062306a36Sopenharmony_ci
119162306a36Sopenharmony_ci	/* Identify chip type */
119262306a36Sopenharmony_ci	par->chip = id->driver_data & CHIP_MASK;
119362306a36Sopenharmony_ci	par->rev = vga_rcrt(par->state.vgabase, 0x2f);
119462306a36Sopenharmony_ci	if (par->chip & CHIP_UNDECIDED_FLAG)
119562306a36Sopenharmony_ci		par->chip = s3_identification(par);
119662306a36Sopenharmony_ci
119762306a36Sopenharmony_ci	/* Find how many physical memory there is on card */
119862306a36Sopenharmony_ci	/* 0x36 register is accessible even if other registers are locked */
119962306a36Sopenharmony_ci	regval = vga_rcrt(par->state.vgabase, 0x36);
120062306a36Sopenharmony_ci	if (par->chip == CHIP_360_TRIO3D_1X ||
120162306a36Sopenharmony_ci	    par->chip == CHIP_362_TRIO3D_2X ||
120262306a36Sopenharmony_ci	    par->chip == CHIP_368_TRIO3D_2X ||
120362306a36Sopenharmony_ci	    par->chip == CHIP_365_TRIO3D) {
120462306a36Sopenharmony_ci		switch ((regval & 0xE0) >> 5) {
120562306a36Sopenharmony_ci		case 0: /* 8MB -- only 4MB usable for display */
120662306a36Sopenharmony_ci		case 1: /* 4MB with 32-bit bus */
120762306a36Sopenharmony_ci		case 2:	/* 4MB */
120862306a36Sopenharmony_ci			info->screen_size = 4 << 20;
120962306a36Sopenharmony_ci			break;
121062306a36Sopenharmony_ci		case 4: /* 2MB on 365 Trio3D */
121162306a36Sopenharmony_ci		case 6: /* 2MB */
121262306a36Sopenharmony_ci			info->screen_size = 2 << 20;
121362306a36Sopenharmony_ci			break;
121462306a36Sopenharmony_ci		}
121562306a36Sopenharmony_ci	} else if (par->chip == CHIP_357_VIRGE_GX2 ||
121662306a36Sopenharmony_ci		   par->chip == CHIP_359_VIRGE_GX2P ||
121762306a36Sopenharmony_ci		   par->chip == CHIP_260_VIRGE_MX) {
121862306a36Sopenharmony_ci		switch ((regval & 0xC0) >> 6) {
121962306a36Sopenharmony_ci		case 1: /* 4MB */
122062306a36Sopenharmony_ci			info->screen_size = 4 << 20;
122162306a36Sopenharmony_ci			break;
122262306a36Sopenharmony_ci		case 3: /* 2MB */
122362306a36Sopenharmony_ci			info->screen_size = 2 << 20;
122462306a36Sopenharmony_ci			break;
122562306a36Sopenharmony_ci		}
122662306a36Sopenharmony_ci	} else if (par->chip == CHIP_988_VIRGE_VX) {
122762306a36Sopenharmony_ci		switch ((regval & 0x60) >> 5) {
122862306a36Sopenharmony_ci		case 0: /* 2MB */
122962306a36Sopenharmony_ci			info->screen_size = 2 << 20;
123062306a36Sopenharmony_ci			break;
123162306a36Sopenharmony_ci		case 1: /* 4MB */
123262306a36Sopenharmony_ci			info->screen_size = 4 << 20;
123362306a36Sopenharmony_ci			break;
123462306a36Sopenharmony_ci		case 2: /* 6MB */
123562306a36Sopenharmony_ci			info->screen_size = 6 << 20;
123662306a36Sopenharmony_ci			break;
123762306a36Sopenharmony_ci		case 3: /* 8MB */
123862306a36Sopenharmony_ci			info->screen_size = 8 << 20;
123962306a36Sopenharmony_ci			break;
124062306a36Sopenharmony_ci		}
124162306a36Sopenharmony_ci		/* off-screen memory */
124262306a36Sopenharmony_ci		regval = vga_rcrt(par->state.vgabase, 0x37);
124362306a36Sopenharmony_ci		switch ((regval & 0x60) >> 5) {
124462306a36Sopenharmony_ci		case 1: /* 4MB */
124562306a36Sopenharmony_ci			info->screen_size -= 4 << 20;
124662306a36Sopenharmony_ci			break;
124762306a36Sopenharmony_ci		case 2: /* 2MB */
124862306a36Sopenharmony_ci			info->screen_size -= 2 << 20;
124962306a36Sopenharmony_ci			break;
125062306a36Sopenharmony_ci		}
125162306a36Sopenharmony_ci	} else
125262306a36Sopenharmony_ci		info->screen_size = s3_memsizes[regval >> 5] << 10;
125362306a36Sopenharmony_ci	info->fix.smem_len = info->screen_size;
125462306a36Sopenharmony_ci
125562306a36Sopenharmony_ci	/* Find MCLK frequency */
125662306a36Sopenharmony_ci	regval = vga_rseq(par->state.vgabase, 0x10);
125762306a36Sopenharmony_ci	par->mclk_freq = ((vga_rseq(par->state.vgabase, 0x11) + 2) * 14318) / ((regval & 0x1F)  + 2);
125862306a36Sopenharmony_ci	par->mclk_freq = par->mclk_freq >> (regval >> 5);
125962306a36Sopenharmony_ci
126062306a36Sopenharmony_ci	/* Restore locks */
126162306a36Sopenharmony_ci	vga_wcrt(par->state.vgabase, 0x38, cr38);
126262306a36Sopenharmony_ci	vga_wcrt(par->state.vgabase, 0x39, cr39);
126362306a36Sopenharmony_ci
126462306a36Sopenharmony_ci	strcpy(info->fix.id, s3_names [par->chip]);
126562306a36Sopenharmony_ci	info->fix.mmio_start = 0;
126662306a36Sopenharmony_ci	info->fix.mmio_len = 0;
126762306a36Sopenharmony_ci	info->fix.type = FB_TYPE_PACKED_PIXELS;
126862306a36Sopenharmony_ci	info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
126962306a36Sopenharmony_ci	info->fix.ypanstep = 0;
127062306a36Sopenharmony_ci	info->fix.accel = FB_ACCEL_NONE;
127162306a36Sopenharmony_ci	info->pseudo_palette = (void*) (par->pseudo_palette);
127262306a36Sopenharmony_ci	info->var.bits_per_pixel = 8;
127362306a36Sopenharmony_ci
127462306a36Sopenharmony_ci#ifdef CONFIG_FB_S3_DDC
127562306a36Sopenharmony_ci	/* Enable MMIO if needed */
127662306a36Sopenharmony_ci	if (s3fb_ddc_needs_mmio(par->chip)) {
127762306a36Sopenharmony_ci		par->mmio = ioremap(info->fix.smem_start + MMIO_OFFSET, MMIO_SIZE);
127862306a36Sopenharmony_ci		if (par->mmio)
127962306a36Sopenharmony_ci			svga_wcrt_mask(par->state.vgabase, 0x53, 0x08, 0x08);	/* enable MMIO */
128062306a36Sopenharmony_ci		else
128162306a36Sopenharmony_ci			dev_err(info->device, "unable to map MMIO at 0x%lx, disabling DDC",
128262306a36Sopenharmony_ci				info->fix.smem_start + MMIO_OFFSET);
128362306a36Sopenharmony_ci	}
128462306a36Sopenharmony_ci	if (!s3fb_ddc_needs_mmio(par->chip) || par->mmio)
128562306a36Sopenharmony_ci		if (s3fb_setup_ddc_bus(info) == 0) {
128662306a36Sopenharmony_ci			u8 *edid = fb_ddc_read(&par->ddc_adapter);
128762306a36Sopenharmony_ci			par->ddc_registered = true;
128862306a36Sopenharmony_ci			if (edid) {
128962306a36Sopenharmony_ci				fb_edid_to_monspecs(edid, &info->monspecs);
129062306a36Sopenharmony_ci				kfree(edid);
129162306a36Sopenharmony_ci				if (!info->monspecs.modedb)
129262306a36Sopenharmony_ci					dev_err(info->device, "error getting mode database\n");
129362306a36Sopenharmony_ci				else {
129462306a36Sopenharmony_ci					const struct fb_videomode *m;
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci					fb_videomode_to_modelist(info->monspecs.modedb,
129762306a36Sopenharmony_ci								 info->monspecs.modedb_len,
129862306a36Sopenharmony_ci								 &info->modelist);
129962306a36Sopenharmony_ci					m = fb_find_best_display(&info->monspecs, &info->modelist);
130062306a36Sopenharmony_ci					if (m) {
130162306a36Sopenharmony_ci						fb_videomode_to_var(&info->var, m);
130262306a36Sopenharmony_ci						/* fill all other info->var's fields */
130362306a36Sopenharmony_ci						if (s3fb_check_var(&info->var, info) == 0)
130462306a36Sopenharmony_ci							found = true;
130562306a36Sopenharmony_ci					}
130662306a36Sopenharmony_ci				}
130762306a36Sopenharmony_ci			}
130862306a36Sopenharmony_ci		}
130962306a36Sopenharmony_ci#endif
131062306a36Sopenharmony_ci	if (!mode_option && !found)
131162306a36Sopenharmony_ci		mode_option = "640x480-8@60";
131262306a36Sopenharmony_ci
131362306a36Sopenharmony_ci	/* Prepare startup mode */
131462306a36Sopenharmony_ci	if (mode_option) {
131562306a36Sopenharmony_ci		rc = fb_find_mode(&info->var, info, mode_option,
131662306a36Sopenharmony_ci				   info->monspecs.modedb, info->monspecs.modedb_len,
131762306a36Sopenharmony_ci				   NULL, info->var.bits_per_pixel);
131862306a36Sopenharmony_ci		if (!rc || rc == 4) {
131962306a36Sopenharmony_ci			rc = -EINVAL;
132062306a36Sopenharmony_ci			dev_err(info->device, "mode %s not found\n", mode_option);
132162306a36Sopenharmony_ci			fb_destroy_modedb(info->monspecs.modedb);
132262306a36Sopenharmony_ci			info->monspecs.modedb = NULL;
132362306a36Sopenharmony_ci			goto err_find_mode;
132462306a36Sopenharmony_ci		}
132562306a36Sopenharmony_ci	}
132662306a36Sopenharmony_ci
132762306a36Sopenharmony_ci	fb_destroy_modedb(info->monspecs.modedb);
132862306a36Sopenharmony_ci	info->monspecs.modedb = NULL;
132962306a36Sopenharmony_ci
133062306a36Sopenharmony_ci	/* maximize virtual vertical size for fast scrolling */
133162306a36Sopenharmony_ci	info->var.yres_virtual = info->fix.smem_len * 8 /
133262306a36Sopenharmony_ci			(info->var.bits_per_pixel * info->var.xres_virtual);
133362306a36Sopenharmony_ci	if (info->var.yres_virtual < info->var.yres) {
133462306a36Sopenharmony_ci		dev_err(info->device, "virtual vertical size smaller than real\n");
133562306a36Sopenharmony_ci		rc = -EINVAL;
133662306a36Sopenharmony_ci		goto err_find_mode;
133762306a36Sopenharmony_ci	}
133862306a36Sopenharmony_ci
133962306a36Sopenharmony_ci	rc = fb_alloc_cmap(&info->cmap, 256, 0);
134062306a36Sopenharmony_ci	if (rc < 0) {
134162306a36Sopenharmony_ci		dev_err(info->device, "cannot allocate colormap\n");
134262306a36Sopenharmony_ci		goto err_alloc_cmap;
134362306a36Sopenharmony_ci	}
134462306a36Sopenharmony_ci
134562306a36Sopenharmony_ci	rc = register_framebuffer(info);
134662306a36Sopenharmony_ci	if (rc < 0) {
134762306a36Sopenharmony_ci		dev_err(info->device, "cannot register framebuffer\n");
134862306a36Sopenharmony_ci		goto err_reg_fb;
134962306a36Sopenharmony_ci	}
135062306a36Sopenharmony_ci
135162306a36Sopenharmony_ci	fb_info(info, "%s on %s, %d MB RAM, %d MHz MCLK\n",
135262306a36Sopenharmony_ci		info->fix.id, pci_name(dev),
135362306a36Sopenharmony_ci		info->fix.smem_len >> 20, (par->mclk_freq + 500) / 1000);
135462306a36Sopenharmony_ci
135562306a36Sopenharmony_ci	if (par->chip == CHIP_UNKNOWN)
135662306a36Sopenharmony_ci		fb_info(info, "unknown chip, CR2D=%x, CR2E=%x, CRT2F=%x, CRT30=%x\n",
135762306a36Sopenharmony_ci			vga_rcrt(par->state.vgabase, 0x2d),
135862306a36Sopenharmony_ci			vga_rcrt(par->state.vgabase, 0x2e),
135962306a36Sopenharmony_ci			vga_rcrt(par->state.vgabase, 0x2f),
136062306a36Sopenharmony_ci			vga_rcrt(par->state.vgabase, 0x30));
136162306a36Sopenharmony_ci
136262306a36Sopenharmony_ci	/* Record a reference to the driver data */
136362306a36Sopenharmony_ci	pci_set_drvdata(dev, info);
136462306a36Sopenharmony_ci
136562306a36Sopenharmony_ci	if (mtrr)
136662306a36Sopenharmony_ci		par->wc_cookie = arch_phys_wc_add(info->fix.smem_start,
136762306a36Sopenharmony_ci						  info->fix.smem_len);
136862306a36Sopenharmony_ci
136962306a36Sopenharmony_ci	return 0;
137062306a36Sopenharmony_ci
137162306a36Sopenharmony_ci	/* Error handling */
137262306a36Sopenharmony_cierr_reg_fb:
137362306a36Sopenharmony_ci	fb_dealloc_cmap(&info->cmap);
137462306a36Sopenharmony_cierr_alloc_cmap:
137562306a36Sopenharmony_cierr_find_mode:
137662306a36Sopenharmony_ci#ifdef CONFIG_FB_S3_DDC
137762306a36Sopenharmony_ci	if (par->ddc_registered)
137862306a36Sopenharmony_ci		i2c_del_adapter(&par->ddc_adapter);
137962306a36Sopenharmony_ci	if (par->mmio)
138062306a36Sopenharmony_ci		iounmap(par->mmio);
138162306a36Sopenharmony_ci#endif
138262306a36Sopenharmony_ci	pci_iounmap(dev, info->screen_base);
138362306a36Sopenharmony_cierr_iomap:
138462306a36Sopenharmony_ci	pci_release_regions(dev);
138562306a36Sopenharmony_cierr_request_regions:
138662306a36Sopenharmony_ci/*	pci_disable_device(dev); */
138762306a36Sopenharmony_cierr_enable_device:
138862306a36Sopenharmony_ci	framebuffer_release(info);
138962306a36Sopenharmony_ci	return rc;
139062306a36Sopenharmony_ci}
139162306a36Sopenharmony_ci
139262306a36Sopenharmony_ci
139362306a36Sopenharmony_ci/* PCI remove */
139462306a36Sopenharmony_ci
139562306a36Sopenharmony_cistatic void s3_pci_remove(struct pci_dev *dev)
139662306a36Sopenharmony_ci{
139762306a36Sopenharmony_ci	struct fb_info *info = pci_get_drvdata(dev);
139862306a36Sopenharmony_ci	struct s3fb_info __maybe_unused *par;
139962306a36Sopenharmony_ci
140062306a36Sopenharmony_ci	if (info) {
140162306a36Sopenharmony_ci		par = info->par;
140262306a36Sopenharmony_ci		arch_phys_wc_del(par->wc_cookie);
140362306a36Sopenharmony_ci		unregister_framebuffer(info);
140462306a36Sopenharmony_ci		fb_dealloc_cmap(&info->cmap);
140562306a36Sopenharmony_ci
140662306a36Sopenharmony_ci#ifdef CONFIG_FB_S3_DDC
140762306a36Sopenharmony_ci		if (par->ddc_registered)
140862306a36Sopenharmony_ci			i2c_del_adapter(&par->ddc_adapter);
140962306a36Sopenharmony_ci		if (par->mmio)
141062306a36Sopenharmony_ci			iounmap(par->mmio);
141162306a36Sopenharmony_ci#endif
141262306a36Sopenharmony_ci
141362306a36Sopenharmony_ci		pci_iounmap(dev, info->screen_base);
141462306a36Sopenharmony_ci		pci_release_regions(dev);
141562306a36Sopenharmony_ci/*		pci_disable_device(dev); */
141662306a36Sopenharmony_ci
141762306a36Sopenharmony_ci		framebuffer_release(info);
141862306a36Sopenharmony_ci	}
141962306a36Sopenharmony_ci}
142062306a36Sopenharmony_ci
142162306a36Sopenharmony_ci/* PCI suspend */
142262306a36Sopenharmony_ci
142362306a36Sopenharmony_cistatic int __maybe_unused s3_pci_suspend(struct device *dev)
142462306a36Sopenharmony_ci{
142562306a36Sopenharmony_ci	struct fb_info *info = dev_get_drvdata(dev);
142662306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
142762306a36Sopenharmony_ci
142862306a36Sopenharmony_ci	dev_info(info->device, "suspend\n");
142962306a36Sopenharmony_ci
143062306a36Sopenharmony_ci	console_lock();
143162306a36Sopenharmony_ci	mutex_lock(&(par->open_lock));
143262306a36Sopenharmony_ci
143362306a36Sopenharmony_ci	if (par->ref_count == 0) {
143462306a36Sopenharmony_ci		mutex_unlock(&(par->open_lock));
143562306a36Sopenharmony_ci		console_unlock();
143662306a36Sopenharmony_ci		return 0;
143762306a36Sopenharmony_ci	}
143862306a36Sopenharmony_ci
143962306a36Sopenharmony_ci	fb_set_suspend(info, 1);
144062306a36Sopenharmony_ci
144162306a36Sopenharmony_ci	mutex_unlock(&(par->open_lock));
144262306a36Sopenharmony_ci	console_unlock();
144362306a36Sopenharmony_ci
144462306a36Sopenharmony_ci	return 0;
144562306a36Sopenharmony_ci}
144662306a36Sopenharmony_ci
144762306a36Sopenharmony_ci
144862306a36Sopenharmony_ci/* PCI resume */
144962306a36Sopenharmony_ci
145062306a36Sopenharmony_cistatic int __maybe_unused s3_pci_resume(struct device *dev)
145162306a36Sopenharmony_ci{
145262306a36Sopenharmony_ci	struct fb_info *info = dev_get_drvdata(dev);
145362306a36Sopenharmony_ci	struct s3fb_info *par = info->par;
145462306a36Sopenharmony_ci
145562306a36Sopenharmony_ci	dev_info(info->device, "resume\n");
145662306a36Sopenharmony_ci
145762306a36Sopenharmony_ci	console_lock();
145862306a36Sopenharmony_ci	mutex_lock(&(par->open_lock));
145962306a36Sopenharmony_ci
146062306a36Sopenharmony_ci	if (par->ref_count == 0) {
146162306a36Sopenharmony_ci		mutex_unlock(&(par->open_lock));
146262306a36Sopenharmony_ci		console_unlock();
146362306a36Sopenharmony_ci		return 0;
146462306a36Sopenharmony_ci	}
146562306a36Sopenharmony_ci
146662306a36Sopenharmony_ci	s3fb_set_par(info);
146762306a36Sopenharmony_ci	fb_set_suspend(info, 0);
146862306a36Sopenharmony_ci
146962306a36Sopenharmony_ci	mutex_unlock(&(par->open_lock));
147062306a36Sopenharmony_ci	console_unlock();
147162306a36Sopenharmony_ci
147262306a36Sopenharmony_ci	return 0;
147362306a36Sopenharmony_ci}
147462306a36Sopenharmony_ci
147562306a36Sopenharmony_cistatic const struct dev_pm_ops s3_pci_pm_ops = {
147662306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
147762306a36Sopenharmony_ci	.suspend	= s3_pci_suspend,
147862306a36Sopenharmony_ci	.resume		= s3_pci_resume,
147962306a36Sopenharmony_ci	.freeze		= NULL,
148062306a36Sopenharmony_ci	.thaw		= s3_pci_resume,
148162306a36Sopenharmony_ci	.poweroff	= s3_pci_suspend,
148262306a36Sopenharmony_ci	.restore	= s3_pci_resume,
148362306a36Sopenharmony_ci#endif
148462306a36Sopenharmony_ci};
148562306a36Sopenharmony_ci
148662306a36Sopenharmony_ci/* List of boards that we are trying to support */
148762306a36Sopenharmony_ci
148862306a36Sopenharmony_cistatic const struct pci_device_id s3_devices[] = {
148962306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8810), .driver_data = CHIP_XXX_TRIO},
149062306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8811), .driver_data = CHIP_XXX_TRIO},
149162306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8812), .driver_data = CHIP_M65_AURORA64VP},
149262306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8814), .driver_data = CHIP_767_TRIO64UVP},
149362306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8901), .driver_data = CHIP_XXX_TRIO64V2_DXGX},
149462306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8902), .driver_data = CHIP_551_PLATO_PX},
149562306a36Sopenharmony_ci
149662306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x5631), .driver_data = CHIP_325_VIRGE},
149762306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x883D), .driver_data = CHIP_988_VIRGE_VX},
149862306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A01), .driver_data = CHIP_XXX_VIRGE_DXGX},
149962306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A10), .driver_data = CHIP_357_VIRGE_GX2},
150062306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A11), .driver_data = CHIP_359_VIRGE_GX2P},
150162306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A12), .driver_data = CHIP_359_VIRGE_GX2P},
150262306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8A13), .driver_data = CHIP_36X_TRIO3D_1X_2X},
150362306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8904), .driver_data = CHIP_365_TRIO3D},
150462306a36Sopenharmony_ci	{PCI_DEVICE(PCI_VENDOR_ID_S3, 0x8C01), .driver_data = CHIP_260_VIRGE_MX},
150562306a36Sopenharmony_ci
150662306a36Sopenharmony_ci	{0, 0, 0, 0, 0, 0, 0}
150762306a36Sopenharmony_ci};
150862306a36Sopenharmony_ci
150962306a36Sopenharmony_ci
151062306a36Sopenharmony_ciMODULE_DEVICE_TABLE(pci, s3_devices);
151162306a36Sopenharmony_ci
151262306a36Sopenharmony_cistatic struct pci_driver s3fb_pci_driver = {
151362306a36Sopenharmony_ci	.name		= "s3fb",
151462306a36Sopenharmony_ci	.id_table	= s3_devices,
151562306a36Sopenharmony_ci	.probe		= s3_pci_probe,
151662306a36Sopenharmony_ci	.remove		= s3_pci_remove,
151762306a36Sopenharmony_ci	.driver.pm	= &s3_pci_pm_ops,
151862306a36Sopenharmony_ci};
151962306a36Sopenharmony_ci
152062306a36Sopenharmony_ci/* Parse user specified options */
152162306a36Sopenharmony_ci
152262306a36Sopenharmony_ci#ifndef MODULE
152362306a36Sopenharmony_cistatic int  __init s3fb_setup(char *options)
152462306a36Sopenharmony_ci{
152562306a36Sopenharmony_ci	char *opt;
152662306a36Sopenharmony_ci
152762306a36Sopenharmony_ci	if (!options || !*options)
152862306a36Sopenharmony_ci		return 0;
152962306a36Sopenharmony_ci
153062306a36Sopenharmony_ci	while ((opt = strsep(&options, ",")) != NULL) {
153162306a36Sopenharmony_ci
153262306a36Sopenharmony_ci		if (!*opt)
153362306a36Sopenharmony_ci			continue;
153462306a36Sopenharmony_ci		else if (!strncmp(opt, "mtrr:", 5))
153562306a36Sopenharmony_ci			mtrr = simple_strtoul(opt + 5, NULL, 0);
153662306a36Sopenharmony_ci		else if (!strncmp(opt, "fasttext:", 9))
153762306a36Sopenharmony_ci			fasttext = simple_strtoul(opt + 9, NULL, 0);
153862306a36Sopenharmony_ci		else
153962306a36Sopenharmony_ci			mode_option = opt;
154062306a36Sopenharmony_ci	}
154162306a36Sopenharmony_ci
154262306a36Sopenharmony_ci	return 0;
154362306a36Sopenharmony_ci}
154462306a36Sopenharmony_ci#endif
154562306a36Sopenharmony_ci
154662306a36Sopenharmony_ci/* Cleanup */
154762306a36Sopenharmony_ci
154862306a36Sopenharmony_cistatic void __exit s3fb_cleanup(void)
154962306a36Sopenharmony_ci{
155062306a36Sopenharmony_ci	pr_debug("s3fb: cleaning up\n");
155162306a36Sopenharmony_ci	pci_unregister_driver(&s3fb_pci_driver);
155262306a36Sopenharmony_ci}
155362306a36Sopenharmony_ci
155462306a36Sopenharmony_ci/* Driver Initialisation */
155562306a36Sopenharmony_ci
155662306a36Sopenharmony_cistatic int __init s3fb_init(void)
155762306a36Sopenharmony_ci{
155862306a36Sopenharmony_ci
155962306a36Sopenharmony_ci#ifndef MODULE
156062306a36Sopenharmony_ci	char *option = NULL;
156162306a36Sopenharmony_ci#endif
156262306a36Sopenharmony_ci
156362306a36Sopenharmony_ci	if (fb_modesetting_disabled("s3fb"))
156462306a36Sopenharmony_ci		return -ENODEV;
156562306a36Sopenharmony_ci
156662306a36Sopenharmony_ci#ifndef MODULE
156762306a36Sopenharmony_ci	if (fb_get_options("s3fb", &option))
156862306a36Sopenharmony_ci		return -ENODEV;
156962306a36Sopenharmony_ci	s3fb_setup(option);
157062306a36Sopenharmony_ci#endif
157162306a36Sopenharmony_ci
157262306a36Sopenharmony_ci	pr_debug("s3fb: initializing\n");
157362306a36Sopenharmony_ci	return pci_register_driver(&s3fb_pci_driver);
157462306a36Sopenharmony_ci}
157562306a36Sopenharmony_ci
157662306a36Sopenharmony_ci/* ------------------------------------------------------------------------- */
157762306a36Sopenharmony_ci
157862306a36Sopenharmony_ci/* Modularization */
157962306a36Sopenharmony_ci
158062306a36Sopenharmony_cimodule_init(s3fb_init);
158162306a36Sopenharmony_cimodule_exit(s3fb_cleanup);
1582