xref: /OK3568_Linux_fs/kernel/drivers/video/fbdev/tgafb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  linux/drivers/video/tgafb.c -- DEC 21030 TGA frame buffer device
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *	Copyright (C) 1995 Jay Estabrook
5*4882a593Smuzhiyun  *	Copyright (C) 1997 Geert Uytterhoeven
6*4882a593Smuzhiyun  *	Copyright (C) 1999,2000 Martin Lucina, Tom Zerucha
7*4882a593Smuzhiyun  *	Copyright (C) 2002 Richard Henderson
8*4882a593Smuzhiyun  *	Copyright (C) 2006, 2007  Maciej W. Rozycki
9*4882a593Smuzhiyun  *
10*4882a593Smuzhiyun  *  This file is subject to the terms and conditions of the GNU General Public
11*4882a593Smuzhiyun  *  License. See the file COPYING in the main directory of this archive for
12*4882a593Smuzhiyun  *  more details.
13*4882a593Smuzhiyun  */
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <linux/bitrev.h>
16*4882a593Smuzhiyun #include <linux/compiler.h>
17*4882a593Smuzhiyun #include <linux/delay.h>
18*4882a593Smuzhiyun #include <linux/device.h>
19*4882a593Smuzhiyun #include <linux/errno.h>
20*4882a593Smuzhiyun #include <linux/fb.h>
21*4882a593Smuzhiyun #include <linux/init.h>
22*4882a593Smuzhiyun #include <linux/ioport.h>
23*4882a593Smuzhiyun #include <linux/kernel.h>
24*4882a593Smuzhiyun #include <linux/mm.h>
25*4882a593Smuzhiyun #include <linux/module.h>
26*4882a593Smuzhiyun #include <linux/pci.h>
27*4882a593Smuzhiyun #include <linux/selection.h>
28*4882a593Smuzhiyun #include <linux/string.h>
29*4882a593Smuzhiyun #include <linux/tc.h>
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <asm/io.h>
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun #include <video/tgafb.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #ifdef CONFIG_TC
36*4882a593Smuzhiyun #define TGA_BUS_TC(dev) (dev->bus == &tc_bus_type)
37*4882a593Smuzhiyun #else
38*4882a593Smuzhiyun #define TGA_BUS_TC(dev) 0
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun  * Local functions.
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun static int tgafb_check_var(struct fb_var_screeninfo *, struct fb_info *);
46*4882a593Smuzhiyun static int tgafb_set_par(struct fb_info *);
47*4882a593Smuzhiyun static void tgafb_set_pll(struct tga_par *, int);
48*4882a593Smuzhiyun static int tgafb_setcolreg(unsigned, unsigned, unsigned, unsigned,
49*4882a593Smuzhiyun 			   unsigned, struct fb_info *);
50*4882a593Smuzhiyun static int tgafb_blank(int, struct fb_info *);
51*4882a593Smuzhiyun static void tgafb_init_fix(struct fb_info *);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static void tgafb_imageblit(struct fb_info *, const struct fb_image *);
54*4882a593Smuzhiyun static void tgafb_fillrect(struct fb_info *, const struct fb_fillrect *);
55*4882a593Smuzhiyun static void tgafb_copyarea(struct fb_info *, const struct fb_copyarea *);
56*4882a593Smuzhiyun static int tgafb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info);
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun static int tgafb_register(struct device *dev);
59*4882a593Smuzhiyun static void tgafb_unregister(struct device *dev);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun static const char *mode_option;
62*4882a593Smuzhiyun static const char *mode_option_pci = "640x480@60";
63*4882a593Smuzhiyun static const char *mode_option_tc = "1280x1024@72";
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun static struct pci_driver tgafb_pci_driver;
67*4882a593Smuzhiyun static struct tc_driver tgafb_tc_driver;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun  *  Frame buffer operations
71*4882a593Smuzhiyun  */
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun static const struct fb_ops tgafb_ops = {
74*4882a593Smuzhiyun 	.owner			= THIS_MODULE,
75*4882a593Smuzhiyun 	.fb_check_var		= tgafb_check_var,
76*4882a593Smuzhiyun 	.fb_set_par		= tgafb_set_par,
77*4882a593Smuzhiyun 	.fb_setcolreg		= tgafb_setcolreg,
78*4882a593Smuzhiyun 	.fb_blank		= tgafb_blank,
79*4882a593Smuzhiyun 	.fb_pan_display		= tgafb_pan_display,
80*4882a593Smuzhiyun 	.fb_fillrect		= tgafb_fillrect,
81*4882a593Smuzhiyun 	.fb_copyarea		= tgafb_copyarea,
82*4882a593Smuzhiyun 	.fb_imageblit		= tgafb_imageblit,
83*4882a593Smuzhiyun };
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun #ifdef CONFIG_PCI
87*4882a593Smuzhiyun /*
88*4882a593Smuzhiyun  *  PCI registration operations
89*4882a593Smuzhiyun  */
90*4882a593Smuzhiyun static int tgafb_pci_register(struct pci_dev *, const struct pci_device_id *);
91*4882a593Smuzhiyun static void tgafb_pci_unregister(struct pci_dev *);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun static struct pci_device_id const tgafb_pci_table[] = {
94*4882a593Smuzhiyun 	{ PCI_DEVICE(PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TGA) },
95*4882a593Smuzhiyun 	{ }
96*4882a593Smuzhiyun };
97*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, tgafb_pci_table);
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun static struct pci_driver tgafb_pci_driver = {
100*4882a593Smuzhiyun 	.name			= "tgafb",
101*4882a593Smuzhiyun 	.id_table		= tgafb_pci_table,
102*4882a593Smuzhiyun 	.probe			= tgafb_pci_register,
103*4882a593Smuzhiyun 	.remove			= tgafb_pci_unregister,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun 
tgafb_pci_register(struct pci_dev * pdev,const struct pci_device_id * ent)106*4882a593Smuzhiyun static int tgafb_pci_register(struct pci_dev *pdev,
107*4882a593Smuzhiyun 			      const struct pci_device_id *ent)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	return tgafb_register(&pdev->dev);
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun 
tgafb_pci_unregister(struct pci_dev * pdev)112*4882a593Smuzhiyun static void tgafb_pci_unregister(struct pci_dev *pdev)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	tgafb_unregister(&pdev->dev);
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun #endif /* CONFIG_PCI */
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #ifdef CONFIG_TC
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun  *  TC registration operations
121*4882a593Smuzhiyun  */
122*4882a593Smuzhiyun static int tgafb_tc_register(struct device *);
123*4882a593Smuzhiyun static int tgafb_tc_unregister(struct device *);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun static struct tc_device_id const tgafb_tc_table[] = {
126*4882a593Smuzhiyun 	{ "DEC     ", "PMAGD-AA" },
127*4882a593Smuzhiyun 	{ "DEC     ", "PMAGD   " },
128*4882a593Smuzhiyun 	{ }
129*4882a593Smuzhiyun };
130*4882a593Smuzhiyun MODULE_DEVICE_TABLE(tc, tgafb_tc_table);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static struct tc_driver tgafb_tc_driver = {
133*4882a593Smuzhiyun 	.id_table		= tgafb_tc_table,
134*4882a593Smuzhiyun 	.driver			= {
135*4882a593Smuzhiyun 		.name		= "tgafb",
136*4882a593Smuzhiyun 		.bus		= &tc_bus_type,
137*4882a593Smuzhiyun 		.probe		= tgafb_tc_register,
138*4882a593Smuzhiyun 		.remove		= tgafb_tc_unregister,
139*4882a593Smuzhiyun 	},
140*4882a593Smuzhiyun };
141*4882a593Smuzhiyun 
tgafb_tc_register(struct device * dev)142*4882a593Smuzhiyun static int tgafb_tc_register(struct device *dev)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	int status = tgafb_register(dev);
145*4882a593Smuzhiyun 	if (!status)
146*4882a593Smuzhiyun 		get_device(dev);
147*4882a593Smuzhiyun 	return status;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
tgafb_tc_unregister(struct device * dev)150*4882a593Smuzhiyun static int tgafb_tc_unregister(struct device *dev)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	put_device(dev);
153*4882a593Smuzhiyun 	tgafb_unregister(dev);
154*4882a593Smuzhiyun 	return 0;
155*4882a593Smuzhiyun }
156*4882a593Smuzhiyun #endif /* CONFIG_TC */
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun /**
160*4882a593Smuzhiyun  *      tgafb_check_var - Optional function.  Validates a var passed in.
161*4882a593Smuzhiyun  *      @var: frame buffer variable screen structure
162*4882a593Smuzhiyun  *      @info: frame buffer structure that represents a single frame buffer
163*4882a593Smuzhiyun  */
164*4882a593Smuzhiyun static int
tgafb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)165*4882a593Smuzhiyun tgafb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *)info->par;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	if (par->tga_type == TGA_TYPE_8PLANE) {
170*4882a593Smuzhiyun 		if (var->bits_per_pixel != 8)
171*4882a593Smuzhiyun 			return -EINVAL;
172*4882a593Smuzhiyun 	} else {
173*4882a593Smuzhiyun 		if (var->bits_per_pixel != 32)
174*4882a593Smuzhiyun 			return -EINVAL;
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun 	var->red.length = var->green.length = var->blue.length = 8;
177*4882a593Smuzhiyun 	if (var->bits_per_pixel == 32) {
178*4882a593Smuzhiyun 		var->red.offset = 16;
179*4882a593Smuzhiyun 		var->green.offset = 8;
180*4882a593Smuzhiyun 		var->blue.offset = 0;
181*4882a593Smuzhiyun 	}
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	if (var->xres_virtual != var->xres || var->yres_virtual != var->yres)
184*4882a593Smuzhiyun 		return -EINVAL;
185*4882a593Smuzhiyun 	if (var->xres * var->yres * (var->bits_per_pixel >> 3) > info->fix.smem_len)
186*4882a593Smuzhiyun 		return -EINVAL;
187*4882a593Smuzhiyun 	if (var->nonstd)
188*4882a593Smuzhiyun 		return -EINVAL;
189*4882a593Smuzhiyun 	if (1000000000 / var->pixclock > TGA_PLL_MAX_FREQ)
190*4882a593Smuzhiyun 		return -EINVAL;
191*4882a593Smuzhiyun 	if ((var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED)
192*4882a593Smuzhiyun 		return -EINVAL;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	/* Some of the acceleration routines assume the line width is
195*4882a593Smuzhiyun 	   a multiple of 8 bytes.  */
196*4882a593Smuzhiyun 	if (var->xres * (par->tga_type == TGA_TYPE_8PLANE ? 1 : 4) % 8)
197*4882a593Smuzhiyun 		return -EINVAL;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	return 0;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun /**
203*4882a593Smuzhiyun  *      tgafb_set_par - Optional function.  Alters the hardware state.
204*4882a593Smuzhiyun  *      @info: frame buffer structure that represents a single frame buffer
205*4882a593Smuzhiyun  */
206*4882a593Smuzhiyun static int
tgafb_set_par(struct fb_info * info)207*4882a593Smuzhiyun tgafb_set_par(struct fb_info *info)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	static unsigned int const deep_presets[4] = {
210*4882a593Smuzhiyun 		0x00004000,
211*4882a593Smuzhiyun 		0x0000440d,
212*4882a593Smuzhiyun 		0xffffffff,
213*4882a593Smuzhiyun 		0x0000441d
214*4882a593Smuzhiyun 	};
215*4882a593Smuzhiyun 	static unsigned int const rasterop_presets[4] = {
216*4882a593Smuzhiyun 		0x00000003,
217*4882a593Smuzhiyun 		0x00000303,
218*4882a593Smuzhiyun 		0xffffffff,
219*4882a593Smuzhiyun 		0x00000303
220*4882a593Smuzhiyun 	};
221*4882a593Smuzhiyun 	static unsigned int const mode_presets[4] = {
222*4882a593Smuzhiyun 		0x00000000,
223*4882a593Smuzhiyun 		0x00000300,
224*4882a593Smuzhiyun 		0xffffffff,
225*4882a593Smuzhiyun 		0x00000300
226*4882a593Smuzhiyun 	};
227*4882a593Smuzhiyun 	static unsigned int const base_addr_presets[4] = {
228*4882a593Smuzhiyun 		0x00000000,
229*4882a593Smuzhiyun 		0x00000001,
230*4882a593Smuzhiyun 		0xffffffff,
231*4882a593Smuzhiyun 		0x00000001
232*4882a593Smuzhiyun 	};
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
235*4882a593Smuzhiyun 	int tga_bus_pci = dev_is_pci(par->dev);
236*4882a593Smuzhiyun 	int tga_bus_tc = TGA_BUS_TC(par->dev);
237*4882a593Smuzhiyun 	u32 htimings, vtimings, pll_freq;
238*4882a593Smuzhiyun 	u8 tga_type;
239*4882a593Smuzhiyun 	int i;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/* Encode video timings.  */
242*4882a593Smuzhiyun 	htimings = (((info->var.xres/4) & TGA_HORIZ_ACT_LSB)
243*4882a593Smuzhiyun 		    | (((info->var.xres/4) & 0x600 << 19) & TGA_HORIZ_ACT_MSB));
244*4882a593Smuzhiyun 	vtimings = (info->var.yres & TGA_VERT_ACTIVE);
245*4882a593Smuzhiyun 	htimings |= ((info->var.right_margin/4) << 9) & TGA_HORIZ_FP;
246*4882a593Smuzhiyun 	vtimings |= (info->var.lower_margin << 11) & TGA_VERT_FP;
247*4882a593Smuzhiyun 	htimings |= ((info->var.hsync_len/4) << 14) & TGA_HORIZ_SYNC;
248*4882a593Smuzhiyun 	vtimings |= (info->var.vsync_len << 16) & TGA_VERT_SYNC;
249*4882a593Smuzhiyun 	htimings |= ((info->var.left_margin/4) << 21) & TGA_HORIZ_BP;
250*4882a593Smuzhiyun 	vtimings |= (info->var.upper_margin << 22) & TGA_VERT_BP;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	if (info->var.sync & FB_SYNC_HOR_HIGH_ACT)
253*4882a593Smuzhiyun 		htimings |= TGA_HORIZ_POLARITY;
254*4882a593Smuzhiyun 	if (info->var.sync & FB_SYNC_VERT_HIGH_ACT)
255*4882a593Smuzhiyun 		vtimings |= TGA_VERT_POLARITY;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	par->htimings = htimings;
258*4882a593Smuzhiyun 	par->vtimings = vtimings;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	par->sync_on_green = !!(info->var.sync & FB_SYNC_ON_GREEN);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* Store other useful values in par.  */
263*4882a593Smuzhiyun 	par->xres = info->var.xres;
264*4882a593Smuzhiyun 	par->yres = info->var.yres;
265*4882a593Smuzhiyun 	par->pll_freq = pll_freq = 1000000000 / info->var.pixclock;
266*4882a593Smuzhiyun 	par->bits_per_pixel = info->var.bits_per_pixel;
267*4882a593Smuzhiyun 	info->fix.line_length = par->xres * (par->bits_per_pixel >> 3);
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	tga_type = par->tga_type;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	/* First, disable video.  */
272*4882a593Smuzhiyun 	TGA_WRITE_REG(par, TGA_VALID_VIDEO | TGA_VALID_BLANK, TGA_VALID_REG);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* Write the DEEP register.  */
275*4882a593Smuzhiyun 	while (TGA_READ_REG(par, TGA_CMD_STAT_REG) & 1) /* wait for not busy */
276*4882a593Smuzhiyun 		continue;
277*4882a593Smuzhiyun 	mb();
278*4882a593Smuzhiyun 	TGA_WRITE_REG(par, deep_presets[tga_type] |
279*4882a593Smuzhiyun 			   (par->sync_on_green ? 0x0 : 0x00010000),
280*4882a593Smuzhiyun 		      TGA_DEEP_REG);
281*4882a593Smuzhiyun 	while (TGA_READ_REG(par, TGA_CMD_STAT_REG) & 1) /* wait for not busy */
282*4882a593Smuzhiyun 		continue;
283*4882a593Smuzhiyun 	mb();
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	/* Write some more registers.  */
286*4882a593Smuzhiyun 	TGA_WRITE_REG(par, rasterop_presets[tga_type], TGA_RASTEROP_REG);
287*4882a593Smuzhiyun 	TGA_WRITE_REG(par, mode_presets[tga_type], TGA_MODE_REG);
288*4882a593Smuzhiyun 	TGA_WRITE_REG(par, base_addr_presets[tga_type], TGA_BASE_ADDR_REG);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/* Calculate & write the PLL.  */
291*4882a593Smuzhiyun 	tgafb_set_pll(par, pll_freq);
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	/* Write some more registers.  */
294*4882a593Smuzhiyun 	TGA_WRITE_REG(par, 0xffffffff, TGA_PLANEMASK_REG);
295*4882a593Smuzhiyun 	TGA_WRITE_REG(par, 0xffffffff, TGA_PIXELMASK_REG);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	/* Init video timing regs.  */
298*4882a593Smuzhiyun 	TGA_WRITE_REG(par, htimings, TGA_HORIZ_REG);
299*4882a593Smuzhiyun 	TGA_WRITE_REG(par, vtimings, TGA_VERT_REG);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/* Initialise RAMDAC. */
302*4882a593Smuzhiyun 	if (tga_type == TGA_TYPE_8PLANE && tga_bus_pci) {
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 		/* Init BT485 RAMDAC registers.  */
305*4882a593Smuzhiyun 		BT485_WRITE(par, 0xa2 | (par->sync_on_green ? 0x8 : 0x0),
306*4882a593Smuzhiyun 			    BT485_CMD_0);
307*4882a593Smuzhiyun 		BT485_WRITE(par, 0x01, BT485_ADDR_PAL_WRITE);
308*4882a593Smuzhiyun 		BT485_WRITE(par, 0x14, BT485_CMD_3); /* cursor 64x64 */
309*4882a593Smuzhiyun 		BT485_WRITE(par, 0x40, BT485_CMD_1);
310*4882a593Smuzhiyun 		BT485_WRITE(par, 0x20, BT485_CMD_2); /* cursor off, for now */
311*4882a593Smuzhiyun 		BT485_WRITE(par, 0xff, BT485_PIXEL_MASK);
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 		/* Fill palette registers.  */
314*4882a593Smuzhiyun 		BT485_WRITE(par, 0x00, BT485_ADDR_PAL_WRITE);
315*4882a593Smuzhiyun 		TGA_WRITE_REG(par, BT485_DATA_PAL, TGA_RAMDAC_SETUP_REG);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 		for (i = 0; i < 256 * 3; i += 4) {
318*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x55 | (BT485_DATA_PAL << 8),
319*4882a593Smuzhiyun 				      TGA_RAMDAC_REG);
320*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00 | (BT485_DATA_PAL << 8),
321*4882a593Smuzhiyun 				      TGA_RAMDAC_REG);
322*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00 | (BT485_DATA_PAL << 8),
323*4882a593Smuzhiyun 				      TGA_RAMDAC_REG);
324*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00 | (BT485_DATA_PAL << 8),
325*4882a593Smuzhiyun 				      TGA_RAMDAC_REG);
326*4882a593Smuzhiyun 		}
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	} else if (tga_type == TGA_TYPE_8PLANE && tga_bus_tc) {
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 		/* Init BT459 RAMDAC registers.  */
331*4882a593Smuzhiyun 		BT459_WRITE(par, BT459_REG_ACC, BT459_CMD_REG_0, 0x40);
332*4882a593Smuzhiyun 		BT459_WRITE(par, BT459_REG_ACC, BT459_CMD_REG_1, 0x00);
333*4882a593Smuzhiyun 		BT459_WRITE(par, BT459_REG_ACC, BT459_CMD_REG_2,
334*4882a593Smuzhiyun 			    (par->sync_on_green ? 0xc0 : 0x40));
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 		BT459_WRITE(par, BT459_REG_ACC, BT459_CUR_CMD_REG, 0x00);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 		/* Fill the palette.  */
339*4882a593Smuzhiyun 		BT459_LOAD_ADDR(par, 0x0000);
340*4882a593Smuzhiyun 		TGA_WRITE_REG(par, BT459_PALETTE << 2, TGA_RAMDAC_SETUP_REG);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 		for (i = 0; i < 256 * 3; i += 4) {
343*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x55, TGA_RAMDAC_REG);
344*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
345*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
346*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
347*4882a593Smuzhiyun 		}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	} else { /* 24-plane or 24plusZ */
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun 		/* Init BT463 RAMDAC registers.  */
352*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_0, 0x40);
353*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_1, 0x08);
354*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_CMD_REG_2,
355*4882a593Smuzhiyun 			    (par->sync_on_green ? 0xc0 : 0x40));
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_0, 0xff);
358*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_1, 0xff);
359*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_2, 0xff);
360*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_READ_MASK_3, 0x0f);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_0, 0x00);
363*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_1, 0x00);
364*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_2, 0x00);
365*4882a593Smuzhiyun 		BT463_WRITE(par, BT463_REG_ACC, BT463_BLINK_MASK_3, 0x00);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 		/* Fill the palette.  */
368*4882a593Smuzhiyun 		BT463_LOAD_ADDR(par, 0x0000);
369*4882a593Smuzhiyun 		TGA_WRITE_REG(par, BT463_PALETTE << 2, TGA_RAMDAC_SETUP_REG);
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun #ifdef CONFIG_HW_CONSOLE
372*4882a593Smuzhiyun 		for (i = 0; i < 16; i++) {
373*4882a593Smuzhiyun 			int j = color_table[i];
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 			TGA_WRITE_REG(par, default_red[j], TGA_RAMDAC_REG);
376*4882a593Smuzhiyun 			TGA_WRITE_REG(par, default_grn[j], TGA_RAMDAC_REG);
377*4882a593Smuzhiyun 			TGA_WRITE_REG(par, default_blu[j], TGA_RAMDAC_REG);
378*4882a593Smuzhiyun 		}
379*4882a593Smuzhiyun 		for (i = 0; i < 512 * 3; i += 4) {
380*4882a593Smuzhiyun #else
381*4882a593Smuzhiyun 		for (i = 0; i < 528 * 3; i += 4) {
382*4882a593Smuzhiyun #endif
383*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x55, TGA_RAMDAC_REG);
384*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
385*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
386*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
387*4882a593Smuzhiyun 		}
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 		/* Fill window type table after start of vertical retrace.  */
390*4882a593Smuzhiyun 		while (!(TGA_READ_REG(par, TGA_INTR_STAT_REG) & 0x01))
391*4882a593Smuzhiyun 			continue;
392*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 0x01, TGA_INTR_STAT_REG);
393*4882a593Smuzhiyun 		mb();
394*4882a593Smuzhiyun 		while (!(TGA_READ_REG(par, TGA_INTR_STAT_REG) & 0x01))
395*4882a593Smuzhiyun 			continue;
396*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 0x01, TGA_INTR_STAT_REG);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 		BT463_LOAD_ADDR(par, BT463_WINDOW_TYPE_BASE);
399*4882a593Smuzhiyun 		TGA_WRITE_REG(par, BT463_REG_ACC << 2, TGA_RAMDAC_SETUP_REG);
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 		for (i = 0; i < 16; i++) {
402*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
403*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x01, TGA_RAMDAC_REG);
404*4882a593Smuzhiyun 			TGA_WRITE_REG(par, 0x00, TGA_RAMDAC_REG);
405*4882a593Smuzhiyun 		}
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	}
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	/* Finally, enable video scan (and pray for the monitor... :-) */
410*4882a593Smuzhiyun 	TGA_WRITE_REG(par, TGA_VALID_VIDEO, TGA_VALID_REG);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	return 0;
413*4882a593Smuzhiyun }
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun #define DIFFCHECK(X)							  \
416*4882a593Smuzhiyun do {									  \
417*4882a593Smuzhiyun 	if (m <= 0x3f) {						  \
418*4882a593Smuzhiyun 		int delta = f - (TGA_PLL_BASE_FREQ * (X)) / (r << shift); \
419*4882a593Smuzhiyun 		if (delta < 0)						  \
420*4882a593Smuzhiyun 			delta = -delta;					  \
421*4882a593Smuzhiyun 		if (delta < min_diff)					  \
422*4882a593Smuzhiyun 			min_diff = delta, vm = m, va = a, vr = r;	  \
423*4882a593Smuzhiyun 	}								  \
424*4882a593Smuzhiyun } while (0)
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun static void
427*4882a593Smuzhiyun tgafb_set_pll(struct tga_par *par, int f)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	int n, shift, base, min_diff, target;
430*4882a593Smuzhiyun 	int r,a,m,vm = 34, va = 1, vr = 30;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	for (r = 0 ; r < 12 ; r++)
433*4882a593Smuzhiyun 		TGA_WRITE_REG(par, !r, TGA_CLOCK_REG);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	if (f > TGA_PLL_MAX_FREQ)
436*4882a593Smuzhiyun 		f = TGA_PLL_MAX_FREQ;
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	if (f >= TGA_PLL_MAX_FREQ / 2)
439*4882a593Smuzhiyun 		shift = 0;
440*4882a593Smuzhiyun 	else if (f >= TGA_PLL_MAX_FREQ / 4)
441*4882a593Smuzhiyun 		shift = 1;
442*4882a593Smuzhiyun 	else
443*4882a593Smuzhiyun 		shift = 2;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	TGA_WRITE_REG(par, shift & 1, TGA_CLOCK_REG);
446*4882a593Smuzhiyun 	TGA_WRITE_REG(par, shift >> 1, TGA_CLOCK_REG);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	for (r = 0 ; r < 10 ; r++)
449*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	if (f <= 120000) {
452*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
453*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
454*4882a593Smuzhiyun 	}
455*4882a593Smuzhiyun 	else if (f <= 200000) {
456*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
457*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
458*4882a593Smuzhiyun 	}
459*4882a593Smuzhiyun 	else {
460*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
461*4882a593Smuzhiyun 		TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
462*4882a593Smuzhiyun 	}
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
465*4882a593Smuzhiyun 	TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
466*4882a593Smuzhiyun 	TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
467*4882a593Smuzhiyun 	TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
468*4882a593Smuzhiyun 	TGA_WRITE_REG(par, 0, TGA_CLOCK_REG);
469*4882a593Smuzhiyun 	TGA_WRITE_REG(par, 1, TGA_CLOCK_REG);
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	target = (f << shift) / TGA_PLL_BASE_FREQ;
472*4882a593Smuzhiyun 	min_diff = TGA_PLL_MAX_FREQ;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	r = 7 / target;
475*4882a593Smuzhiyun 	if (!r) r = 1;
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	base = target * r;
478*4882a593Smuzhiyun 	while (base < 449) {
479*4882a593Smuzhiyun 		for (n = base < 7 ? 7 : base; n < base + target && n < 449; n++) {
480*4882a593Smuzhiyun 			m = ((n + 3) / 7) - 1;
481*4882a593Smuzhiyun 			a = 0;
482*4882a593Smuzhiyun 			DIFFCHECK((m + 1) * 7);
483*4882a593Smuzhiyun 			m++;
484*4882a593Smuzhiyun 			DIFFCHECK((m + 1) * 7);
485*4882a593Smuzhiyun 			m = (n / 6) - 1;
486*4882a593Smuzhiyun 			if ((a = n % 6))
487*4882a593Smuzhiyun 				DIFFCHECK(n);
488*4882a593Smuzhiyun 		}
489*4882a593Smuzhiyun 		r++;
490*4882a593Smuzhiyun 		base += target;
491*4882a593Smuzhiyun 	}
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	vr--;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	for (r = 0; r < 8; r++)
496*4882a593Smuzhiyun 		TGA_WRITE_REG(par, (vm >> r) & 1, TGA_CLOCK_REG);
497*4882a593Smuzhiyun 	for (r = 0; r < 8 ; r++)
498*4882a593Smuzhiyun 		TGA_WRITE_REG(par, (va >> r) & 1, TGA_CLOCK_REG);
499*4882a593Smuzhiyun 	for (r = 0; r < 7 ; r++)
500*4882a593Smuzhiyun 		TGA_WRITE_REG(par, (vr >> r) & 1, TGA_CLOCK_REG);
501*4882a593Smuzhiyun 	TGA_WRITE_REG(par, ((vr >> 7) & 1)|2, TGA_CLOCK_REG);
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun /**
506*4882a593Smuzhiyun  *      tgafb_setcolreg - Optional function. Sets a color register.
507*4882a593Smuzhiyun  *      @regno: boolean, 0 copy local, 1 get_user() function
508*4882a593Smuzhiyun  *      @red: frame buffer colormap structure
509*4882a593Smuzhiyun  *      @green: The green value which can be up to 16 bits wide
510*4882a593Smuzhiyun  *      @blue:  The blue value which can be up to 16 bits wide.
511*4882a593Smuzhiyun  *      @transp: If supported the alpha value which can be up to 16 bits wide.
512*4882a593Smuzhiyun  *      @info: frame buffer info structure
513*4882a593Smuzhiyun  */
514*4882a593Smuzhiyun static int
515*4882a593Smuzhiyun tgafb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue,
516*4882a593Smuzhiyun 		unsigned transp, struct fb_info *info)
517*4882a593Smuzhiyun {
518*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
519*4882a593Smuzhiyun 	int tga_bus_pci = dev_is_pci(par->dev);
520*4882a593Smuzhiyun 	int tga_bus_tc = TGA_BUS_TC(par->dev);
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun 	if (regno > 255)
523*4882a593Smuzhiyun 		return 1;
524*4882a593Smuzhiyun 	red >>= 8;
525*4882a593Smuzhiyun 	green >>= 8;
526*4882a593Smuzhiyun 	blue >>= 8;
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	if (par->tga_type == TGA_TYPE_8PLANE && tga_bus_pci) {
529*4882a593Smuzhiyun 		BT485_WRITE(par, regno, BT485_ADDR_PAL_WRITE);
530*4882a593Smuzhiyun 		TGA_WRITE_REG(par, BT485_DATA_PAL, TGA_RAMDAC_SETUP_REG);
531*4882a593Smuzhiyun 		TGA_WRITE_REG(par, red|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
532*4882a593Smuzhiyun 		TGA_WRITE_REG(par, green|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
533*4882a593Smuzhiyun 		TGA_WRITE_REG(par, blue|(BT485_DATA_PAL<<8),TGA_RAMDAC_REG);
534*4882a593Smuzhiyun 	} else if (par->tga_type == TGA_TYPE_8PLANE && tga_bus_tc) {
535*4882a593Smuzhiyun 		BT459_LOAD_ADDR(par, regno);
536*4882a593Smuzhiyun 		TGA_WRITE_REG(par, BT459_PALETTE << 2, TGA_RAMDAC_SETUP_REG);
537*4882a593Smuzhiyun 		TGA_WRITE_REG(par, red, TGA_RAMDAC_REG);
538*4882a593Smuzhiyun 		TGA_WRITE_REG(par, green, TGA_RAMDAC_REG);
539*4882a593Smuzhiyun 		TGA_WRITE_REG(par, blue, TGA_RAMDAC_REG);
540*4882a593Smuzhiyun 	} else {
541*4882a593Smuzhiyun 		if (regno < 16) {
542*4882a593Smuzhiyun 			u32 value = (regno << 16) | (regno << 8) | regno;
543*4882a593Smuzhiyun 			((u32 *)info->pseudo_palette)[regno] = value;
544*4882a593Smuzhiyun 		}
545*4882a593Smuzhiyun 		BT463_LOAD_ADDR(par, regno);
546*4882a593Smuzhiyun 		TGA_WRITE_REG(par, BT463_PALETTE << 2, TGA_RAMDAC_SETUP_REG);
547*4882a593Smuzhiyun 		TGA_WRITE_REG(par, red, TGA_RAMDAC_REG);
548*4882a593Smuzhiyun 		TGA_WRITE_REG(par, green, TGA_RAMDAC_REG);
549*4882a593Smuzhiyun 		TGA_WRITE_REG(par, blue, TGA_RAMDAC_REG);
550*4882a593Smuzhiyun 	}
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	return 0;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /**
557*4882a593Smuzhiyun  *      tgafb_blank - Optional function.  Blanks the display.
558*4882a593Smuzhiyun  *      @blank_mode: the blank mode we want.
559*4882a593Smuzhiyun  *      @info: frame buffer structure that represents a single frame buffer
560*4882a593Smuzhiyun  */
561*4882a593Smuzhiyun static int
562*4882a593Smuzhiyun tgafb_blank(int blank, struct fb_info *info)
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
565*4882a593Smuzhiyun 	u32 vhcr, vvcr, vvvr;
566*4882a593Smuzhiyun 	unsigned long flags;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	local_irq_save(flags);
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	vhcr = TGA_READ_REG(par, TGA_HORIZ_REG);
571*4882a593Smuzhiyun 	vvcr = TGA_READ_REG(par, TGA_VERT_REG);
572*4882a593Smuzhiyun 	vvvr = TGA_READ_REG(par, TGA_VALID_REG);
573*4882a593Smuzhiyun 	vvvr &= ~(TGA_VALID_VIDEO | TGA_VALID_BLANK);
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun 	switch (blank) {
576*4882a593Smuzhiyun 	case FB_BLANK_UNBLANK: /* Unblanking */
577*4882a593Smuzhiyun 		if (par->vesa_blanked) {
578*4882a593Smuzhiyun 			TGA_WRITE_REG(par, vhcr & 0xbfffffff, TGA_HORIZ_REG);
579*4882a593Smuzhiyun 			TGA_WRITE_REG(par, vvcr & 0xbfffffff, TGA_VERT_REG);
580*4882a593Smuzhiyun 			par->vesa_blanked = 0;
581*4882a593Smuzhiyun 		}
582*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vvvr | TGA_VALID_VIDEO, TGA_VALID_REG);
583*4882a593Smuzhiyun 		break;
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	case FB_BLANK_NORMAL: /* Normal blanking */
586*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vvvr | TGA_VALID_VIDEO | TGA_VALID_BLANK,
587*4882a593Smuzhiyun 			      TGA_VALID_REG);
588*4882a593Smuzhiyun 		break;
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	case FB_BLANK_VSYNC_SUSPEND: /* VESA blank (vsync off) */
591*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vvcr | 0x40000000, TGA_VERT_REG);
592*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
593*4882a593Smuzhiyun 		par->vesa_blanked = 1;
594*4882a593Smuzhiyun 		break;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	case FB_BLANK_HSYNC_SUSPEND: /* VESA blank (hsync off) */
597*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vhcr | 0x40000000, TGA_HORIZ_REG);
598*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
599*4882a593Smuzhiyun 		par->vesa_blanked = 1;
600*4882a593Smuzhiyun 		break;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	case FB_BLANK_POWERDOWN: /* Poweroff */
603*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vhcr | 0x40000000, TGA_HORIZ_REG);
604*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vvcr | 0x40000000, TGA_VERT_REG);
605*4882a593Smuzhiyun 		TGA_WRITE_REG(par, vvvr | TGA_VALID_BLANK, TGA_VALID_REG);
606*4882a593Smuzhiyun 		par->vesa_blanked = 1;
607*4882a593Smuzhiyun 		break;
608*4882a593Smuzhiyun 	}
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	local_irq_restore(flags);
611*4882a593Smuzhiyun 	return 0;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun /*
616*4882a593Smuzhiyun  *  Acceleration.
617*4882a593Smuzhiyun  */
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun static void
620*4882a593Smuzhiyun tgafb_mono_imageblit(struct fb_info *info, const struct fb_image *image)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
623*4882a593Smuzhiyun 	u32 fgcolor, bgcolor, dx, dy, width, height, vxres, vyres, pixelmask;
624*4882a593Smuzhiyun 	unsigned long rincr, line_length, shift, pos, is8bpp;
625*4882a593Smuzhiyun 	unsigned long i, j;
626*4882a593Smuzhiyun 	const unsigned char *data;
627*4882a593Smuzhiyun 	void __iomem *regs_base;
628*4882a593Smuzhiyun 	void __iomem *fb_base;
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun 	is8bpp = info->var.bits_per_pixel == 8;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	dx = image->dx;
633*4882a593Smuzhiyun 	dy = image->dy;
634*4882a593Smuzhiyun 	width = image->width;
635*4882a593Smuzhiyun 	height = image->height;
636*4882a593Smuzhiyun 	vxres = info->var.xres_virtual;
637*4882a593Smuzhiyun 	vyres = info->var.yres_virtual;
638*4882a593Smuzhiyun 	line_length = info->fix.line_length;
639*4882a593Smuzhiyun 	rincr = (width + 7) / 8;
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	/* A shift below cannot cope with.  */
642*4882a593Smuzhiyun 	if (unlikely(width == 0))
643*4882a593Smuzhiyun 		return;
644*4882a593Smuzhiyun 	/* Crop the image to the screen.  */
645*4882a593Smuzhiyun 	if (dx > vxres || dy > vyres)
646*4882a593Smuzhiyun 		return;
647*4882a593Smuzhiyun 	if (dx + width > vxres)
648*4882a593Smuzhiyun 		width = vxres - dx;
649*4882a593Smuzhiyun 	if (dy + height > vyres)
650*4882a593Smuzhiyun 		height = vyres - dy;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	regs_base = par->tga_regs_base;
653*4882a593Smuzhiyun 	fb_base = par->tga_fb_base;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	/* Expand the color values to fill 32-bits.  */
656*4882a593Smuzhiyun 	/* ??? Would be nice to notice colour changes elsewhere, so
657*4882a593Smuzhiyun 	   that we can do this only when necessary.  */
658*4882a593Smuzhiyun 	fgcolor = image->fg_color;
659*4882a593Smuzhiyun 	bgcolor = image->bg_color;
660*4882a593Smuzhiyun 	if (is8bpp) {
661*4882a593Smuzhiyun 		fgcolor |= fgcolor << 8;
662*4882a593Smuzhiyun 		fgcolor |= fgcolor << 16;
663*4882a593Smuzhiyun 		bgcolor |= bgcolor << 8;
664*4882a593Smuzhiyun 		bgcolor |= bgcolor << 16;
665*4882a593Smuzhiyun 	} else {
666*4882a593Smuzhiyun 		if (fgcolor < 16)
667*4882a593Smuzhiyun 			fgcolor = ((u32 *)info->pseudo_palette)[fgcolor];
668*4882a593Smuzhiyun 		if (bgcolor < 16)
669*4882a593Smuzhiyun 			bgcolor = ((u32 *)info->pseudo_palette)[bgcolor];
670*4882a593Smuzhiyun 	}
671*4882a593Smuzhiyun 	__raw_writel(fgcolor, regs_base + TGA_FOREGROUND_REG);
672*4882a593Smuzhiyun 	__raw_writel(bgcolor, regs_base + TGA_BACKGROUND_REG);
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	/* Acquire proper alignment; set up the PIXELMASK register
675*4882a593Smuzhiyun 	   so that we only write the proper character cell.  */
676*4882a593Smuzhiyun 	pos = dy * line_length;
677*4882a593Smuzhiyun 	if (is8bpp) {
678*4882a593Smuzhiyun 		pos += dx;
679*4882a593Smuzhiyun 		shift = pos & 3;
680*4882a593Smuzhiyun 		pos &= -4;
681*4882a593Smuzhiyun 	} else {
682*4882a593Smuzhiyun 		pos += dx * 4;
683*4882a593Smuzhiyun 		shift = (pos & 7) >> 2;
684*4882a593Smuzhiyun 		pos &= -8;
685*4882a593Smuzhiyun 	}
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	data = (const unsigned char *) image->data;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	/* Enable opaque stipple mode.  */
690*4882a593Smuzhiyun 	__raw_writel((is8bpp
691*4882a593Smuzhiyun 		      ? TGA_MODE_SBM_8BPP | TGA_MODE_OPAQUE_STIPPLE
692*4882a593Smuzhiyun 		      : TGA_MODE_SBM_24BPP | TGA_MODE_OPAQUE_STIPPLE),
693*4882a593Smuzhiyun 		     regs_base + TGA_MODE_REG);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (width + shift <= 32) {
696*4882a593Smuzhiyun 		unsigned long bwidth;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 		/* Handle common case of imaging a single character, in
699*4882a593Smuzhiyun 		   a font less than or 32 pixels wide.  */
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 		/* Avoid a shift by 32; width > 0 implied.  */
702*4882a593Smuzhiyun 		pixelmask = (2ul << (width - 1)) - 1;
703*4882a593Smuzhiyun 		pixelmask <<= shift;
704*4882a593Smuzhiyun 		__raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
705*4882a593Smuzhiyun 		wmb();
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 		bwidth = (width + 7) / 8;
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun 		for (i = 0; i < height; ++i) {
710*4882a593Smuzhiyun 			u32 mask = 0;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 			/* The image data is bit big endian; we need
713*4882a593Smuzhiyun 			   little endian.  */
714*4882a593Smuzhiyun 			for (j = 0; j < bwidth; ++j)
715*4882a593Smuzhiyun 				mask |= bitrev8(data[j]) << (j * 8);
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 			__raw_writel(mask << shift, fb_base + pos);
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 			pos += line_length;
720*4882a593Smuzhiyun 			data += rincr;
721*4882a593Smuzhiyun 		}
722*4882a593Smuzhiyun 		wmb();
723*4882a593Smuzhiyun 		__raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
724*4882a593Smuzhiyun 	} else if (shift == 0) {
725*4882a593Smuzhiyun 		unsigned long pos0 = pos;
726*4882a593Smuzhiyun 		const unsigned char *data0 = data;
727*4882a593Smuzhiyun 		unsigned long bincr = (is8bpp ? 8 : 8*4);
728*4882a593Smuzhiyun 		unsigned long bwidth;
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 		/* Handle another common case in which accel_putcs
731*4882a593Smuzhiyun 		   generates a large bitmap, which happens to be aligned.
732*4882a593Smuzhiyun 		   Allow the tail to be misaligned.  This case is
733*4882a593Smuzhiyun 		   interesting because we've not got to hold partial
734*4882a593Smuzhiyun 		   bytes across the words being written.  */
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 		wmb();
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		bwidth = (width / 8) & -4;
739*4882a593Smuzhiyun 		for (i = 0; i < height; ++i) {
740*4882a593Smuzhiyun 			for (j = 0; j < bwidth; j += 4) {
741*4882a593Smuzhiyun 				u32 mask = 0;
742*4882a593Smuzhiyun 				mask |= bitrev8(data[j+0]) << (0 * 8);
743*4882a593Smuzhiyun 				mask |= bitrev8(data[j+1]) << (1 * 8);
744*4882a593Smuzhiyun 				mask |= bitrev8(data[j+2]) << (2 * 8);
745*4882a593Smuzhiyun 				mask |= bitrev8(data[j+3]) << (3 * 8);
746*4882a593Smuzhiyun 				__raw_writel(mask, fb_base + pos + j*bincr);
747*4882a593Smuzhiyun 			}
748*4882a593Smuzhiyun 			pos += line_length;
749*4882a593Smuzhiyun 			data += rincr;
750*4882a593Smuzhiyun 		}
751*4882a593Smuzhiyun 		wmb();
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 		pixelmask = (1ul << (width & 31)) - 1;
754*4882a593Smuzhiyun 		if (pixelmask) {
755*4882a593Smuzhiyun 			__raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
756*4882a593Smuzhiyun 			wmb();
757*4882a593Smuzhiyun 
758*4882a593Smuzhiyun 			pos = pos0 + bwidth*bincr;
759*4882a593Smuzhiyun 			data = data0 + bwidth;
760*4882a593Smuzhiyun 			bwidth = ((width & 31) + 7) / 8;
761*4882a593Smuzhiyun 
762*4882a593Smuzhiyun 			for (i = 0; i < height; ++i) {
763*4882a593Smuzhiyun 				u32 mask = 0;
764*4882a593Smuzhiyun 				for (j = 0; j < bwidth; ++j)
765*4882a593Smuzhiyun 					mask |= bitrev8(data[j]) << (j * 8);
766*4882a593Smuzhiyun 				__raw_writel(mask, fb_base + pos);
767*4882a593Smuzhiyun 				pos += line_length;
768*4882a593Smuzhiyun 				data += rincr;
769*4882a593Smuzhiyun 			}
770*4882a593Smuzhiyun 			wmb();
771*4882a593Smuzhiyun 			__raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
772*4882a593Smuzhiyun 		}
773*4882a593Smuzhiyun 	} else {
774*4882a593Smuzhiyun 		unsigned long pos0 = pos;
775*4882a593Smuzhiyun 		const unsigned char *data0 = data;
776*4882a593Smuzhiyun 		unsigned long bincr = (is8bpp ? 8 : 8*4);
777*4882a593Smuzhiyun 		unsigned long bwidth;
778*4882a593Smuzhiyun 
779*4882a593Smuzhiyun 		/* Finally, handle the generic case of misaligned start.
780*4882a593Smuzhiyun 		   Here we split the write into 16-bit spans.  This allows
781*4882a593Smuzhiyun 		   us to use only one pixel mask, instead of four as would
782*4882a593Smuzhiyun 		   be required by writing 24-bit spans.  */
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 		pixelmask = 0xffff << shift;
785*4882a593Smuzhiyun 		__raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
786*4882a593Smuzhiyun 		wmb();
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 		bwidth = (width / 8) & -2;
789*4882a593Smuzhiyun 		for (i = 0; i < height; ++i) {
790*4882a593Smuzhiyun 			for (j = 0; j < bwidth; j += 2) {
791*4882a593Smuzhiyun 				u32 mask = 0;
792*4882a593Smuzhiyun 				mask |= bitrev8(data[j+0]) << (0 * 8);
793*4882a593Smuzhiyun 				mask |= bitrev8(data[j+1]) << (1 * 8);
794*4882a593Smuzhiyun 				mask <<= shift;
795*4882a593Smuzhiyun 				__raw_writel(mask, fb_base + pos + j*bincr);
796*4882a593Smuzhiyun 			}
797*4882a593Smuzhiyun 			pos += line_length;
798*4882a593Smuzhiyun 			data += rincr;
799*4882a593Smuzhiyun 		}
800*4882a593Smuzhiyun 		wmb();
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 		pixelmask = ((1ul << (width & 15)) - 1) << shift;
803*4882a593Smuzhiyun 		if (pixelmask) {
804*4882a593Smuzhiyun 			__raw_writel(pixelmask, regs_base + TGA_PIXELMASK_REG);
805*4882a593Smuzhiyun 			wmb();
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 			pos = pos0 + bwidth*bincr;
808*4882a593Smuzhiyun 			data = data0 + bwidth;
809*4882a593Smuzhiyun 			bwidth = (width & 15) > 8;
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 			for (i = 0; i < height; ++i) {
812*4882a593Smuzhiyun 				u32 mask = bitrev8(data[0]);
813*4882a593Smuzhiyun 				if (bwidth)
814*4882a593Smuzhiyun 					mask |= bitrev8(data[1]) << 8;
815*4882a593Smuzhiyun 				mask <<= shift;
816*4882a593Smuzhiyun 				__raw_writel(mask, fb_base + pos);
817*4882a593Smuzhiyun 				pos += line_length;
818*4882a593Smuzhiyun 				data += rincr;
819*4882a593Smuzhiyun 			}
820*4882a593Smuzhiyun 			wmb();
821*4882a593Smuzhiyun 		}
822*4882a593Smuzhiyun 		__raw_writel(0xffffffff, regs_base + TGA_PIXELMASK_REG);
823*4882a593Smuzhiyun 	}
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	/* Disable opaque stipple mode.  */
826*4882a593Smuzhiyun 	__raw_writel((is8bpp
827*4882a593Smuzhiyun 		      ? TGA_MODE_SBM_8BPP | TGA_MODE_SIMPLE
828*4882a593Smuzhiyun 		      : TGA_MODE_SBM_24BPP | TGA_MODE_SIMPLE),
829*4882a593Smuzhiyun 		     regs_base + TGA_MODE_REG);
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun static void
833*4882a593Smuzhiyun tgafb_clut_imageblit(struct fb_info *info, const struct fb_image *image)
834*4882a593Smuzhiyun {
835*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
836*4882a593Smuzhiyun 	u32 color, dx, dy, width, height, vxres, vyres;
837*4882a593Smuzhiyun 	u32 *palette = ((u32 *)info->pseudo_palette);
838*4882a593Smuzhiyun 	unsigned long pos, line_length, i, j;
839*4882a593Smuzhiyun 	const unsigned char *data;
840*4882a593Smuzhiyun 	void __iomem *regs_base, *fb_base;
841*4882a593Smuzhiyun 
842*4882a593Smuzhiyun 	dx = image->dx;
843*4882a593Smuzhiyun 	dy = image->dy;
844*4882a593Smuzhiyun 	width = image->width;
845*4882a593Smuzhiyun 	height = image->height;
846*4882a593Smuzhiyun 	vxres = info->var.xres_virtual;
847*4882a593Smuzhiyun 	vyres = info->var.yres_virtual;
848*4882a593Smuzhiyun 	line_length = info->fix.line_length;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun 	/* Crop the image to the screen.  */
851*4882a593Smuzhiyun 	if (dx > vxres || dy > vyres)
852*4882a593Smuzhiyun 		return;
853*4882a593Smuzhiyun 	if (dx + width > vxres)
854*4882a593Smuzhiyun 		width = vxres - dx;
855*4882a593Smuzhiyun 	if (dy + height > vyres)
856*4882a593Smuzhiyun 		height = vyres - dy;
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun 	regs_base = par->tga_regs_base;
859*4882a593Smuzhiyun 	fb_base = par->tga_fb_base;
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	pos = dy * line_length + (dx * 4);
862*4882a593Smuzhiyun 	data = image->data;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	/* Now copy the image, color_expanding via the palette. */
865*4882a593Smuzhiyun 	for (i = 0; i < height; i++) {
866*4882a593Smuzhiyun 		for (j = 0; j < width; j++) {
867*4882a593Smuzhiyun 			color = palette[*data++];
868*4882a593Smuzhiyun 			__raw_writel(color, fb_base + pos + j*4);
869*4882a593Smuzhiyun 		}
870*4882a593Smuzhiyun 		pos += line_length;
871*4882a593Smuzhiyun 	}
872*4882a593Smuzhiyun }
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun /**
875*4882a593Smuzhiyun  *      tgafb_imageblit - REQUIRED function. Can use generic routines if
876*4882a593Smuzhiyun  *                        non acclerated hardware and packed pixel based.
877*4882a593Smuzhiyun  *                        Copies a image from system memory to the screen.
878*4882a593Smuzhiyun  *
879*4882a593Smuzhiyun  *      @info: frame buffer structure that represents a single frame buffer
880*4882a593Smuzhiyun  *      @image: structure defining the image.
881*4882a593Smuzhiyun  */
882*4882a593Smuzhiyun static void
883*4882a593Smuzhiyun tgafb_imageblit(struct fb_info *info, const struct fb_image *image)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun 	unsigned int is8bpp = info->var.bits_per_pixel == 8;
886*4882a593Smuzhiyun 
887*4882a593Smuzhiyun 	/* If a mono image, regardless of FB depth, go do it. */
888*4882a593Smuzhiyun 	if (image->depth == 1) {
889*4882a593Smuzhiyun 		tgafb_mono_imageblit(info, image);
890*4882a593Smuzhiyun 		return;
891*4882a593Smuzhiyun 	}
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	/* For copies that aren't pixel expansion, there's little we
894*4882a593Smuzhiyun 	   can do better than the generic code.  */
895*4882a593Smuzhiyun 	/* ??? There is a DMA write mode; I wonder if that could be
896*4882a593Smuzhiyun 	   made to pull the data from the image buffer...  */
897*4882a593Smuzhiyun 	if (image->depth == info->var.bits_per_pixel) {
898*4882a593Smuzhiyun 		cfb_imageblit(info, image);
899*4882a593Smuzhiyun 		return;
900*4882a593Smuzhiyun 	}
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	/* If 24-plane FB and the image is 8-plane with CLUT, we can do it. */
903*4882a593Smuzhiyun 	if (!is8bpp && image->depth == 8) {
904*4882a593Smuzhiyun 		tgafb_clut_imageblit(info, image);
905*4882a593Smuzhiyun 		return;
906*4882a593Smuzhiyun 	}
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	/* Silently return... */
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun /**
912*4882a593Smuzhiyun  *      tgafb_fillrect - REQUIRED function. Can use generic routines if
913*4882a593Smuzhiyun  *                       non acclerated hardware and packed pixel based.
914*4882a593Smuzhiyun  *                       Draws a rectangle on the screen.
915*4882a593Smuzhiyun  *
916*4882a593Smuzhiyun  *      @info: frame buffer structure that represents a single frame buffer
917*4882a593Smuzhiyun  *      @rect: structure defining the rectagle and operation.
918*4882a593Smuzhiyun  */
919*4882a593Smuzhiyun static void
920*4882a593Smuzhiyun tgafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
921*4882a593Smuzhiyun {
922*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
923*4882a593Smuzhiyun 	int is8bpp = info->var.bits_per_pixel == 8;
924*4882a593Smuzhiyun 	u32 dx, dy, width, height, vxres, vyres, color;
925*4882a593Smuzhiyun 	unsigned long pos, align, line_length, i, j;
926*4882a593Smuzhiyun 	void __iomem *regs_base;
927*4882a593Smuzhiyun 	void __iomem *fb_base;
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	dx = rect->dx;
930*4882a593Smuzhiyun 	dy = rect->dy;
931*4882a593Smuzhiyun 	width = rect->width;
932*4882a593Smuzhiyun 	height = rect->height;
933*4882a593Smuzhiyun 	vxres = info->var.xres_virtual;
934*4882a593Smuzhiyun 	vyres = info->var.yres_virtual;
935*4882a593Smuzhiyun 	line_length = info->fix.line_length;
936*4882a593Smuzhiyun 	regs_base = par->tga_regs_base;
937*4882a593Smuzhiyun 	fb_base = par->tga_fb_base;
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun 	/* Crop the rectangle to the screen.  */
940*4882a593Smuzhiyun 	if (dx > vxres || dy > vyres || !width || !height)
941*4882a593Smuzhiyun 		return;
942*4882a593Smuzhiyun 	if (dx + width > vxres)
943*4882a593Smuzhiyun 		width = vxres - dx;
944*4882a593Smuzhiyun 	if (dy + height > vyres)
945*4882a593Smuzhiyun 		height = vyres - dy;
946*4882a593Smuzhiyun 
947*4882a593Smuzhiyun 	pos = dy * line_length + dx * (is8bpp ? 1 : 4);
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	/* ??? We could implement ROP_XOR with opaque fill mode
950*4882a593Smuzhiyun 	   and a RasterOp setting of GXxor, but as far as I can
951*4882a593Smuzhiyun 	   tell, this mode is not actually used in the kernel.
952*4882a593Smuzhiyun 	   Thus I am ignoring it for now.  */
953*4882a593Smuzhiyun 	if (rect->rop != ROP_COPY) {
954*4882a593Smuzhiyun 		cfb_fillrect(info, rect);
955*4882a593Smuzhiyun 		return;
956*4882a593Smuzhiyun 	}
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	/* Expand the color value to fill 8 pixels.  */
959*4882a593Smuzhiyun 	color = rect->color;
960*4882a593Smuzhiyun 	if (is8bpp) {
961*4882a593Smuzhiyun 		color |= color << 8;
962*4882a593Smuzhiyun 		color |= color << 16;
963*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR0_REG);
964*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR1_REG);
965*4882a593Smuzhiyun 	} else {
966*4882a593Smuzhiyun 		if (color < 16)
967*4882a593Smuzhiyun 			color = ((u32 *)info->pseudo_palette)[color];
968*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR0_REG);
969*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR1_REG);
970*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR2_REG);
971*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR3_REG);
972*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR4_REG);
973*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR5_REG);
974*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR6_REG);
975*4882a593Smuzhiyun 		__raw_writel(color, regs_base + TGA_BLOCK_COLOR7_REG);
976*4882a593Smuzhiyun 	}
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 	/* The DATA register holds the fill mask for block fill mode.
979*4882a593Smuzhiyun 	   Since we're not stippling, this is all ones.  */
980*4882a593Smuzhiyun 	__raw_writel(0xffffffff, regs_base + TGA_DATA_REG);
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun 	/* Enable block fill mode.  */
983*4882a593Smuzhiyun 	__raw_writel((is8bpp
984*4882a593Smuzhiyun 		      ? TGA_MODE_SBM_8BPP | TGA_MODE_BLOCK_FILL
985*4882a593Smuzhiyun 		      : TGA_MODE_SBM_24BPP | TGA_MODE_BLOCK_FILL),
986*4882a593Smuzhiyun 		     regs_base + TGA_MODE_REG);
987*4882a593Smuzhiyun 	wmb();
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	/* We can fill 2k pixels per operation.  Notice blocks that fit
990*4882a593Smuzhiyun 	   the width of the screen so that we can take advantage of this
991*4882a593Smuzhiyun 	   and fill more than one line per write.  */
992*4882a593Smuzhiyun 	if (width == line_length) {
993*4882a593Smuzhiyun 		width *= height;
994*4882a593Smuzhiyun 		height = 1;
995*4882a593Smuzhiyun 	}
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	/* The write into the frame buffer must be aligned to 4 bytes,
998*4882a593Smuzhiyun 	   but we are allowed to encode the offset within the word in
999*4882a593Smuzhiyun 	   the data word written.  */
1000*4882a593Smuzhiyun 	align = (pos & 3) << 16;
1001*4882a593Smuzhiyun 	pos &= -4;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	if (width <= 2048) {
1004*4882a593Smuzhiyun 		u32 data;
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 		data = (width - 1) | align;
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 		for (i = 0; i < height; ++i) {
1009*4882a593Smuzhiyun 			__raw_writel(data, fb_base + pos);
1010*4882a593Smuzhiyun 			pos += line_length;
1011*4882a593Smuzhiyun 		}
1012*4882a593Smuzhiyun 	} else {
1013*4882a593Smuzhiyun 		unsigned long Bpp = (is8bpp ? 1 : 4);
1014*4882a593Smuzhiyun 		unsigned long nwidth = width & -2048;
1015*4882a593Smuzhiyun 		u32 fdata, ldata;
1016*4882a593Smuzhiyun 
1017*4882a593Smuzhiyun 		fdata = (2048 - 1) | align;
1018*4882a593Smuzhiyun 		ldata = ((width & 2047) - 1) | align;
1019*4882a593Smuzhiyun 
1020*4882a593Smuzhiyun 		for (i = 0; i < height; ++i) {
1021*4882a593Smuzhiyun 			for (j = 0; j < nwidth; j += 2048)
1022*4882a593Smuzhiyun 				__raw_writel(fdata, fb_base + pos + j*Bpp);
1023*4882a593Smuzhiyun 			if (j < width)
1024*4882a593Smuzhiyun 				__raw_writel(ldata, fb_base + pos + j*Bpp);
1025*4882a593Smuzhiyun 			pos += line_length;
1026*4882a593Smuzhiyun 		}
1027*4882a593Smuzhiyun 	}
1028*4882a593Smuzhiyun 	wmb();
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun 	/* Disable block fill mode.  */
1031*4882a593Smuzhiyun 	__raw_writel((is8bpp
1032*4882a593Smuzhiyun 		      ? TGA_MODE_SBM_8BPP | TGA_MODE_SIMPLE
1033*4882a593Smuzhiyun 		      : TGA_MODE_SBM_24BPP | TGA_MODE_SIMPLE),
1034*4882a593Smuzhiyun 		     regs_base + TGA_MODE_REG);
1035*4882a593Smuzhiyun }
1036*4882a593Smuzhiyun 
1037*4882a593Smuzhiyun /**
1038*4882a593Smuzhiyun  *      tgafb_copyarea - REQUIRED function. Can use generic routines if
1039*4882a593Smuzhiyun  *                       non acclerated hardware and packed pixel based.
1040*4882a593Smuzhiyun  *                       Copies on area of the screen to another area.
1041*4882a593Smuzhiyun  *
1042*4882a593Smuzhiyun  *      @info: frame buffer structure that represents a single frame buffer
1043*4882a593Smuzhiyun  *      @area: structure defining the source and destination.
1044*4882a593Smuzhiyun  */
1045*4882a593Smuzhiyun 
1046*4882a593Smuzhiyun /* Handle the special case of copying entire lines, e.g. during scrolling.
1047*4882a593Smuzhiyun    We can avoid a lot of needless computation in this case.  In the 8bpp
1048*4882a593Smuzhiyun    case we need to use the COPY64 registers instead of mask writes into
1049*4882a593Smuzhiyun    the frame buffer to achieve maximum performance.  */
1050*4882a593Smuzhiyun 
1051*4882a593Smuzhiyun static inline void
1052*4882a593Smuzhiyun copyarea_line_8bpp(struct fb_info *info, u32 dy, u32 sy,
1053*4882a593Smuzhiyun 		   u32 height, u32 width)
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
1056*4882a593Smuzhiyun 	void __iomem *tga_regs = par->tga_regs_base;
1057*4882a593Smuzhiyun 	unsigned long dpos, spos, i, n64;
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	/* Set up the MODE and PIXELSHIFT registers.  */
1060*4882a593Smuzhiyun 	__raw_writel(TGA_MODE_SBM_8BPP | TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
1061*4882a593Smuzhiyun 	__raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
1062*4882a593Smuzhiyun 	wmb();
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	n64 = (height * width) / 64;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	if (sy < dy) {
1067*4882a593Smuzhiyun 		spos = (sy + height) * width;
1068*4882a593Smuzhiyun 		dpos = (dy + height) * width;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 		for (i = 0; i < n64; ++i) {
1071*4882a593Smuzhiyun 			spos -= 64;
1072*4882a593Smuzhiyun 			dpos -= 64;
1073*4882a593Smuzhiyun 			__raw_writel(spos, tga_regs+TGA_COPY64_SRC);
1074*4882a593Smuzhiyun 			wmb();
1075*4882a593Smuzhiyun 			__raw_writel(dpos, tga_regs+TGA_COPY64_DST);
1076*4882a593Smuzhiyun 			wmb();
1077*4882a593Smuzhiyun 		}
1078*4882a593Smuzhiyun 	} else {
1079*4882a593Smuzhiyun 		spos = sy * width;
1080*4882a593Smuzhiyun 		dpos = dy * width;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 		for (i = 0; i < n64; ++i) {
1083*4882a593Smuzhiyun 			__raw_writel(spos, tga_regs+TGA_COPY64_SRC);
1084*4882a593Smuzhiyun 			wmb();
1085*4882a593Smuzhiyun 			__raw_writel(dpos, tga_regs+TGA_COPY64_DST);
1086*4882a593Smuzhiyun 			wmb();
1087*4882a593Smuzhiyun 			spos += 64;
1088*4882a593Smuzhiyun 			dpos += 64;
1089*4882a593Smuzhiyun 		}
1090*4882a593Smuzhiyun 	}
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	/* Reset the MODE register to normal.  */
1093*4882a593Smuzhiyun 	__raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
1094*4882a593Smuzhiyun }
1095*4882a593Smuzhiyun 
1096*4882a593Smuzhiyun static inline void
1097*4882a593Smuzhiyun copyarea_line_32bpp(struct fb_info *info, u32 dy, u32 sy,
1098*4882a593Smuzhiyun 		    u32 height, u32 width)
1099*4882a593Smuzhiyun {
1100*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
1101*4882a593Smuzhiyun 	void __iomem *tga_regs = par->tga_regs_base;
1102*4882a593Smuzhiyun 	void __iomem *tga_fb = par->tga_fb_base;
1103*4882a593Smuzhiyun 	void __iomem *src;
1104*4882a593Smuzhiyun 	void __iomem *dst;
1105*4882a593Smuzhiyun 	unsigned long i, n16;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	/* Set up the MODE and PIXELSHIFT registers.  */
1108*4882a593Smuzhiyun 	__raw_writel(TGA_MODE_SBM_24BPP | TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
1109*4882a593Smuzhiyun 	__raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
1110*4882a593Smuzhiyun 	wmb();
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	n16 = (height * width) / 16;
1113*4882a593Smuzhiyun 
1114*4882a593Smuzhiyun 	if (sy < dy) {
1115*4882a593Smuzhiyun 		src = tga_fb + (sy + height) * width * 4;
1116*4882a593Smuzhiyun 		dst = tga_fb + (dy + height) * width * 4;
1117*4882a593Smuzhiyun 
1118*4882a593Smuzhiyun 		for (i = 0; i < n16; ++i) {
1119*4882a593Smuzhiyun 			src -= 64;
1120*4882a593Smuzhiyun 			dst -= 64;
1121*4882a593Smuzhiyun 			__raw_writel(0xffff, src);
1122*4882a593Smuzhiyun 			wmb();
1123*4882a593Smuzhiyun 			__raw_writel(0xffff, dst);
1124*4882a593Smuzhiyun 			wmb();
1125*4882a593Smuzhiyun 		}
1126*4882a593Smuzhiyun 	} else {
1127*4882a593Smuzhiyun 		src = tga_fb + sy * width * 4;
1128*4882a593Smuzhiyun 		dst = tga_fb + dy * width * 4;
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun 		for (i = 0; i < n16; ++i) {
1131*4882a593Smuzhiyun 			__raw_writel(0xffff, src);
1132*4882a593Smuzhiyun 			wmb();
1133*4882a593Smuzhiyun 			__raw_writel(0xffff, dst);
1134*4882a593Smuzhiyun 			wmb();
1135*4882a593Smuzhiyun 			src += 64;
1136*4882a593Smuzhiyun 			dst += 64;
1137*4882a593Smuzhiyun 		}
1138*4882a593Smuzhiyun 	}
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	/* Reset the MODE register to normal.  */
1141*4882a593Smuzhiyun 	__raw_writel(TGA_MODE_SBM_24BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun /* The (almost) general case of backward copy in 8bpp mode.  */
1145*4882a593Smuzhiyun static inline void
1146*4882a593Smuzhiyun copyarea_8bpp(struct fb_info *info, u32 dx, u32 dy, u32 sx, u32 sy,
1147*4882a593Smuzhiyun 	      u32 height, u32 width, u32 line_length,
1148*4882a593Smuzhiyun 	      const struct fb_copyarea *area)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *) info->par;
1151*4882a593Smuzhiyun 	unsigned i, yincr;
1152*4882a593Smuzhiyun 	int depos, sepos, backward, last_step, step;
1153*4882a593Smuzhiyun 	u32 mask_last;
1154*4882a593Smuzhiyun 	unsigned n32;
1155*4882a593Smuzhiyun 	void __iomem *tga_regs;
1156*4882a593Smuzhiyun 	void __iomem *tga_fb;
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	/* Do acceleration only if we are aligned on 8 pixels */
1159*4882a593Smuzhiyun 	if ((dx | sx | width) & 7) {
1160*4882a593Smuzhiyun 		cfb_copyarea(info, area);
1161*4882a593Smuzhiyun 		return;
1162*4882a593Smuzhiyun 	}
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	yincr = line_length;
1165*4882a593Smuzhiyun 	if (dy > sy) {
1166*4882a593Smuzhiyun 		dy += height - 1;
1167*4882a593Smuzhiyun 		sy += height - 1;
1168*4882a593Smuzhiyun 		yincr = -yincr;
1169*4882a593Smuzhiyun 	}
1170*4882a593Smuzhiyun 	backward = dy == sy && dx > sx && dx < sx + width;
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	/* Compute the offsets and alignments in the frame buffer.
1173*4882a593Smuzhiyun 	   More than anything else, these control how we do copies.  */
1174*4882a593Smuzhiyun 	depos = dy * line_length + dx;
1175*4882a593Smuzhiyun 	sepos = sy * line_length + sx;
1176*4882a593Smuzhiyun 	if (backward) {
1177*4882a593Smuzhiyun 		depos += width;
1178*4882a593Smuzhiyun 		sepos += width;
1179*4882a593Smuzhiyun 	}
1180*4882a593Smuzhiyun 
1181*4882a593Smuzhiyun 	/* Next copy full words at a time.  */
1182*4882a593Smuzhiyun 	n32 = width / 32;
1183*4882a593Smuzhiyun 	last_step = width % 32;
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	/* Finally copy the unaligned head of the span.  */
1186*4882a593Smuzhiyun 	mask_last = (1ul << last_step) - 1;
1187*4882a593Smuzhiyun 
1188*4882a593Smuzhiyun 	if (!backward) {
1189*4882a593Smuzhiyun 		step = 32;
1190*4882a593Smuzhiyun 		last_step = 32;
1191*4882a593Smuzhiyun 	} else {
1192*4882a593Smuzhiyun 		step = -32;
1193*4882a593Smuzhiyun 		last_step = -last_step;
1194*4882a593Smuzhiyun 		sepos -= 32;
1195*4882a593Smuzhiyun 		depos -= 32;
1196*4882a593Smuzhiyun 	}
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun 	tga_regs = par->tga_regs_base;
1199*4882a593Smuzhiyun 	tga_fb = par->tga_fb_base;
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	/* Set up the MODE and PIXELSHIFT registers.  */
1202*4882a593Smuzhiyun 	__raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_COPY, tga_regs+TGA_MODE_REG);
1203*4882a593Smuzhiyun 	__raw_writel(0, tga_regs+TGA_PIXELSHIFT_REG);
1204*4882a593Smuzhiyun 	wmb();
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 	for (i = 0; i < height; ++i) {
1207*4882a593Smuzhiyun 		unsigned long j;
1208*4882a593Smuzhiyun 		void __iomem *sfb;
1209*4882a593Smuzhiyun 		void __iomem *dfb;
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun 		sfb = tga_fb + sepos;
1212*4882a593Smuzhiyun 		dfb = tga_fb + depos;
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 		for (j = 0; j < n32; j++) {
1215*4882a593Smuzhiyun 			if (j < 2 && j + 1 < n32 && !backward &&
1216*4882a593Smuzhiyun 			    !(((unsigned long)sfb | (unsigned long)dfb) & 63)) {
1217*4882a593Smuzhiyun 				do {
1218*4882a593Smuzhiyun 					__raw_writel(sfb - tga_fb, tga_regs+TGA_COPY64_SRC);
1219*4882a593Smuzhiyun 					wmb();
1220*4882a593Smuzhiyun 					__raw_writel(dfb - tga_fb, tga_regs+TGA_COPY64_DST);
1221*4882a593Smuzhiyun 					wmb();
1222*4882a593Smuzhiyun 					sfb += 64;
1223*4882a593Smuzhiyun 					dfb += 64;
1224*4882a593Smuzhiyun 					j += 2;
1225*4882a593Smuzhiyun 				} while (j + 1 < n32);
1226*4882a593Smuzhiyun 				j--;
1227*4882a593Smuzhiyun 				continue;
1228*4882a593Smuzhiyun 			}
1229*4882a593Smuzhiyun 			__raw_writel(0xffffffff, sfb);
1230*4882a593Smuzhiyun 			wmb();
1231*4882a593Smuzhiyun 			__raw_writel(0xffffffff, dfb);
1232*4882a593Smuzhiyun 			wmb();
1233*4882a593Smuzhiyun 			sfb += step;
1234*4882a593Smuzhiyun 			dfb += step;
1235*4882a593Smuzhiyun 		}
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 		if (mask_last) {
1238*4882a593Smuzhiyun 			sfb += last_step - step;
1239*4882a593Smuzhiyun 			dfb += last_step - step;
1240*4882a593Smuzhiyun 			__raw_writel(mask_last, sfb);
1241*4882a593Smuzhiyun 			wmb();
1242*4882a593Smuzhiyun 			__raw_writel(mask_last, dfb);
1243*4882a593Smuzhiyun 			wmb();
1244*4882a593Smuzhiyun 		}
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 		sepos += yincr;
1247*4882a593Smuzhiyun 		depos += yincr;
1248*4882a593Smuzhiyun 	}
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 	/* Reset the MODE register to normal.  */
1251*4882a593Smuzhiyun 	__raw_writel(TGA_MODE_SBM_8BPP|TGA_MODE_SIMPLE, tga_regs+TGA_MODE_REG);
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun static void
1255*4882a593Smuzhiyun tgafb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
1256*4882a593Smuzhiyun {
1257*4882a593Smuzhiyun 	unsigned long dx, dy, width, height, sx, sy, vxres, vyres;
1258*4882a593Smuzhiyun 	unsigned long line_length, bpp;
1259*4882a593Smuzhiyun 
1260*4882a593Smuzhiyun 	dx = area->dx;
1261*4882a593Smuzhiyun 	dy = area->dy;
1262*4882a593Smuzhiyun 	width = area->width;
1263*4882a593Smuzhiyun 	height = area->height;
1264*4882a593Smuzhiyun 	sx = area->sx;
1265*4882a593Smuzhiyun 	sy = area->sy;
1266*4882a593Smuzhiyun 	vxres = info->var.xres_virtual;
1267*4882a593Smuzhiyun 	vyres = info->var.yres_virtual;
1268*4882a593Smuzhiyun 	line_length = info->fix.line_length;
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	/* The top left corners must be in the virtual screen.  */
1271*4882a593Smuzhiyun 	if (dx > vxres || sx > vxres || dy > vyres || sy > vyres)
1272*4882a593Smuzhiyun 		return;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	/* Clip the destination.  */
1275*4882a593Smuzhiyun 	if (dx + width > vxres)
1276*4882a593Smuzhiyun 		width = vxres - dx;
1277*4882a593Smuzhiyun 	if (dy + height > vyres)
1278*4882a593Smuzhiyun 		height = vyres - dy;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	/* The source must be completely inside the virtual screen.  */
1281*4882a593Smuzhiyun 	if (sx + width > vxres || sy + height > vyres)
1282*4882a593Smuzhiyun 		return;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	bpp = info->var.bits_per_pixel;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	/* Detect copies of the entire line.  */
1287*4882a593Smuzhiyun 	if (!(line_length & 63) && width * (bpp >> 3) == line_length) {
1288*4882a593Smuzhiyun 		if (bpp == 8)
1289*4882a593Smuzhiyun 			copyarea_line_8bpp(info, dy, sy, height, width);
1290*4882a593Smuzhiyun 		else
1291*4882a593Smuzhiyun 			copyarea_line_32bpp(info, dy, sy, height, width);
1292*4882a593Smuzhiyun 	}
1293*4882a593Smuzhiyun 
1294*4882a593Smuzhiyun 	/* ??? The documentation is unclear to me exactly how the pixelshift
1295*4882a593Smuzhiyun 	   register works in 32bpp mode.  Since I don't have hardware to test,
1296*4882a593Smuzhiyun 	   give up for now and fall back on the generic routines.  */
1297*4882a593Smuzhiyun 	else if (bpp == 32)
1298*4882a593Smuzhiyun 		cfb_copyarea(info, area);
1299*4882a593Smuzhiyun 
1300*4882a593Smuzhiyun 	else
1301*4882a593Smuzhiyun 		copyarea_8bpp(info, dx, dy, sx, sy, height,
1302*4882a593Smuzhiyun 			      width, line_length, area);
1303*4882a593Smuzhiyun }
1304*4882a593Smuzhiyun 
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun /*
1307*4882a593Smuzhiyun  *  Initialisation
1308*4882a593Smuzhiyun  */
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun static void
1311*4882a593Smuzhiyun tgafb_init_fix(struct fb_info *info)
1312*4882a593Smuzhiyun {
1313*4882a593Smuzhiyun 	struct tga_par *par = (struct tga_par *)info->par;
1314*4882a593Smuzhiyun 	int tga_bus_pci = dev_is_pci(par->dev);
1315*4882a593Smuzhiyun 	int tga_bus_tc = TGA_BUS_TC(par->dev);
1316*4882a593Smuzhiyun 	u8 tga_type = par->tga_type;
1317*4882a593Smuzhiyun 	const char *tga_type_name = NULL;
1318*4882a593Smuzhiyun 	unsigned memory_size;
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	switch (tga_type) {
1321*4882a593Smuzhiyun 	case TGA_TYPE_8PLANE:
1322*4882a593Smuzhiyun 		if (tga_bus_pci)
1323*4882a593Smuzhiyun 			tga_type_name = "Digital ZLXp-E1";
1324*4882a593Smuzhiyun 		if (tga_bus_tc)
1325*4882a593Smuzhiyun 			tga_type_name = "Digital ZLX-E1";
1326*4882a593Smuzhiyun 		memory_size = 2097152;
1327*4882a593Smuzhiyun 		break;
1328*4882a593Smuzhiyun 	case TGA_TYPE_24PLANE:
1329*4882a593Smuzhiyun 		if (tga_bus_pci)
1330*4882a593Smuzhiyun 			tga_type_name = "Digital ZLXp-E2";
1331*4882a593Smuzhiyun 		if (tga_bus_tc)
1332*4882a593Smuzhiyun 			tga_type_name = "Digital ZLX-E2";
1333*4882a593Smuzhiyun 		memory_size = 8388608;
1334*4882a593Smuzhiyun 		break;
1335*4882a593Smuzhiyun 	case TGA_TYPE_24PLUSZ:
1336*4882a593Smuzhiyun 		if (tga_bus_pci)
1337*4882a593Smuzhiyun 			tga_type_name = "Digital ZLXp-E3";
1338*4882a593Smuzhiyun 		if (tga_bus_tc)
1339*4882a593Smuzhiyun 			tga_type_name = "Digital ZLX-E3";
1340*4882a593Smuzhiyun 		memory_size = 16777216;
1341*4882a593Smuzhiyun 		break;
1342*4882a593Smuzhiyun 	}
1343*4882a593Smuzhiyun 	if (!tga_type_name) {
1344*4882a593Smuzhiyun 		tga_type_name = "Unknown";
1345*4882a593Smuzhiyun 		memory_size = 16777216;
1346*4882a593Smuzhiyun 	}
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	strlcpy(info->fix.id, tga_type_name, sizeof(info->fix.id));
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	info->fix.type = FB_TYPE_PACKED_PIXELS;
1351*4882a593Smuzhiyun 	info->fix.type_aux = 0;
1352*4882a593Smuzhiyun 	info->fix.visual = (tga_type == TGA_TYPE_8PLANE
1353*4882a593Smuzhiyun 			    ? FB_VISUAL_PSEUDOCOLOR
1354*4882a593Smuzhiyun 			    : FB_VISUAL_DIRECTCOLOR);
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	info->fix.smem_start = (size_t) par->tga_fb_base;
1357*4882a593Smuzhiyun 	info->fix.smem_len = memory_size;
1358*4882a593Smuzhiyun 	info->fix.mmio_start = (size_t) par->tga_regs_base;
1359*4882a593Smuzhiyun 	info->fix.mmio_len = 512;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	info->fix.xpanstep = 0;
1362*4882a593Smuzhiyun 	info->fix.ypanstep = 0;
1363*4882a593Smuzhiyun 	info->fix.ywrapstep = 0;
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 	info->fix.accel = FB_ACCEL_DEC_TGA;
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	/*
1368*4882a593Smuzhiyun 	 * These are needed by fb_set_logo_truepalette(), so we
1369*4882a593Smuzhiyun 	 * set them here for 24-plane cards.
1370*4882a593Smuzhiyun 	 */
1371*4882a593Smuzhiyun 	if (tga_type != TGA_TYPE_8PLANE) {
1372*4882a593Smuzhiyun 		info->var.red.length = 8;
1373*4882a593Smuzhiyun 		info->var.green.length = 8;
1374*4882a593Smuzhiyun 		info->var.blue.length = 8;
1375*4882a593Smuzhiyun 		info->var.red.offset = 16;
1376*4882a593Smuzhiyun 		info->var.green.offset = 8;
1377*4882a593Smuzhiyun 		info->var.blue.offset = 0;
1378*4882a593Smuzhiyun 	}
1379*4882a593Smuzhiyun }
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun static int tgafb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
1382*4882a593Smuzhiyun {
1383*4882a593Smuzhiyun 	/* We just use this to catch switches out of graphics mode. */
1384*4882a593Smuzhiyun 	tgafb_set_par(info); /* A bit of overkill for BASE_ADDR reset. */
1385*4882a593Smuzhiyun 	return 0;
1386*4882a593Smuzhiyun }
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun static int tgafb_register(struct device *dev)
1389*4882a593Smuzhiyun {
1390*4882a593Smuzhiyun 	static const struct fb_videomode modedb_tc = {
1391*4882a593Smuzhiyun 		/* 1280x1024 @ 72 Hz, 76.8 kHz hsync */
1392*4882a593Smuzhiyun 		"1280x1024@72", 0, 1280, 1024, 7645, 224, 28, 33, 3, 160, 3,
1393*4882a593Smuzhiyun 		FB_SYNC_ON_GREEN, FB_VMODE_NONINTERLACED
1394*4882a593Smuzhiyun 	};
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	static unsigned int const fb_offset_presets[4] = {
1397*4882a593Smuzhiyun 		TGA_8PLANE_FB_OFFSET,
1398*4882a593Smuzhiyun 		TGA_24PLANE_FB_OFFSET,
1399*4882a593Smuzhiyun 		0xffffffff,
1400*4882a593Smuzhiyun 		TGA_24PLUSZ_FB_OFFSET
1401*4882a593Smuzhiyun 	};
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	const struct fb_videomode *modedb_tga = NULL;
1404*4882a593Smuzhiyun 	resource_size_t bar0_start = 0, bar0_len = 0;
1405*4882a593Smuzhiyun 	const char *mode_option_tga = NULL;
1406*4882a593Smuzhiyun 	int tga_bus_pci = dev_is_pci(dev);
1407*4882a593Smuzhiyun 	int tga_bus_tc = TGA_BUS_TC(dev);
1408*4882a593Smuzhiyun 	unsigned int modedbsize_tga = 0;
1409*4882a593Smuzhiyun 	void __iomem *mem_base;
1410*4882a593Smuzhiyun 	struct fb_info *info;
1411*4882a593Smuzhiyun 	struct tga_par *par;
1412*4882a593Smuzhiyun 	u8 tga_type;
1413*4882a593Smuzhiyun 	int ret = 0;
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun 	/* Enable device in PCI config.  */
1416*4882a593Smuzhiyun 	if (tga_bus_pci && pci_enable_device(to_pci_dev(dev))) {
1417*4882a593Smuzhiyun 		printk(KERN_ERR "tgafb: Cannot enable PCI device\n");
1418*4882a593Smuzhiyun 		return -ENODEV;
1419*4882a593Smuzhiyun 	}
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 	/* Allocate the fb and par structures.  */
1422*4882a593Smuzhiyun 	info = framebuffer_alloc(sizeof(struct tga_par), dev);
1423*4882a593Smuzhiyun 	if (!info)
1424*4882a593Smuzhiyun 		return -ENOMEM;
1425*4882a593Smuzhiyun 
1426*4882a593Smuzhiyun 	par = info->par;
1427*4882a593Smuzhiyun 	dev_set_drvdata(dev, info);
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 	/* Request the mem regions.  */
1430*4882a593Smuzhiyun 	ret = -ENODEV;
1431*4882a593Smuzhiyun 	if (tga_bus_pci) {
1432*4882a593Smuzhiyun 		bar0_start = pci_resource_start(to_pci_dev(dev), 0);
1433*4882a593Smuzhiyun 		bar0_len = pci_resource_len(to_pci_dev(dev), 0);
1434*4882a593Smuzhiyun 	}
1435*4882a593Smuzhiyun 	if (tga_bus_tc) {
1436*4882a593Smuzhiyun 		bar0_start = to_tc_dev(dev)->resource.start;
1437*4882a593Smuzhiyun 		bar0_len = to_tc_dev(dev)->resource.end - bar0_start + 1;
1438*4882a593Smuzhiyun 	}
1439*4882a593Smuzhiyun 	if (!request_mem_region (bar0_start, bar0_len, "tgafb")) {
1440*4882a593Smuzhiyun 		printk(KERN_ERR "tgafb: cannot reserve FB region\n");
1441*4882a593Smuzhiyun 		goto err0;
1442*4882a593Smuzhiyun 	}
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	/* Map the framebuffer.  */
1445*4882a593Smuzhiyun 	mem_base = ioremap(bar0_start, bar0_len);
1446*4882a593Smuzhiyun 	if (!mem_base) {
1447*4882a593Smuzhiyun 		printk(KERN_ERR "tgafb: Cannot map MMIO\n");
1448*4882a593Smuzhiyun 		goto err1;
1449*4882a593Smuzhiyun 	}
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun 	/* Grab info about the card.  */
1452*4882a593Smuzhiyun 	tga_type = (readl(mem_base) >> 12) & 0x0f;
1453*4882a593Smuzhiyun 	par->dev = dev;
1454*4882a593Smuzhiyun 	par->tga_mem_base = mem_base;
1455*4882a593Smuzhiyun 	par->tga_fb_base = mem_base + fb_offset_presets[tga_type];
1456*4882a593Smuzhiyun 	par->tga_regs_base = mem_base + TGA_REGS_OFFSET;
1457*4882a593Smuzhiyun 	par->tga_type = tga_type;
1458*4882a593Smuzhiyun 	if (tga_bus_pci)
1459*4882a593Smuzhiyun 		par->tga_chip_rev = (to_pci_dev(dev))->revision;
1460*4882a593Smuzhiyun 	if (tga_bus_tc)
1461*4882a593Smuzhiyun 		par->tga_chip_rev = TGA_READ_REG(par, TGA_START_REG) & 0xff;
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	/* Setup framebuffer.  */
1464*4882a593Smuzhiyun 	info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
1465*4882a593Smuzhiyun 		      FBINFO_HWACCEL_IMAGEBLIT | FBINFO_HWACCEL_FILLRECT;
1466*4882a593Smuzhiyun 	info->fbops = &tgafb_ops;
1467*4882a593Smuzhiyun 	info->screen_base = par->tga_fb_base;
1468*4882a593Smuzhiyun 	info->pseudo_palette = par->palette;
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 	/* This should give a reasonable default video mode.  */
1471*4882a593Smuzhiyun 	if (tga_bus_pci) {
1472*4882a593Smuzhiyun 		mode_option_tga = mode_option_pci;
1473*4882a593Smuzhiyun 	}
1474*4882a593Smuzhiyun 	if (tga_bus_tc) {
1475*4882a593Smuzhiyun 		mode_option_tga = mode_option_tc;
1476*4882a593Smuzhiyun 		modedb_tga = &modedb_tc;
1477*4882a593Smuzhiyun 		modedbsize_tga = 1;
1478*4882a593Smuzhiyun 	}
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	tgafb_init_fix(info);
1481*4882a593Smuzhiyun 
1482*4882a593Smuzhiyun 	ret = fb_find_mode(&info->var, info,
1483*4882a593Smuzhiyun 			   mode_option ? mode_option : mode_option_tga,
1484*4882a593Smuzhiyun 			   modedb_tga, modedbsize_tga, NULL,
1485*4882a593Smuzhiyun 			   tga_type == TGA_TYPE_8PLANE ? 8 : 32);
1486*4882a593Smuzhiyun 	if (ret == 0 || ret == 4) {
1487*4882a593Smuzhiyun 		printk(KERN_ERR "tgafb: Could not find valid video mode\n");
1488*4882a593Smuzhiyun 		ret = -EINVAL;
1489*4882a593Smuzhiyun 		goto err1;
1490*4882a593Smuzhiyun 	}
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun 	if (fb_alloc_cmap(&info->cmap, 256, 0)) {
1493*4882a593Smuzhiyun 		printk(KERN_ERR "tgafb: Could not allocate color map\n");
1494*4882a593Smuzhiyun 		ret = -ENOMEM;
1495*4882a593Smuzhiyun 		goto err1;
1496*4882a593Smuzhiyun 	}
1497*4882a593Smuzhiyun 
1498*4882a593Smuzhiyun 	tgafb_set_par(info);
1499*4882a593Smuzhiyun 
1500*4882a593Smuzhiyun 	if (register_framebuffer(info) < 0) {
1501*4882a593Smuzhiyun 		printk(KERN_ERR "tgafb: Could not register framebuffer\n");
1502*4882a593Smuzhiyun 		ret = -EINVAL;
1503*4882a593Smuzhiyun 		goto err2;
1504*4882a593Smuzhiyun 	}
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	if (tga_bus_pci) {
1507*4882a593Smuzhiyun 		pr_info("tgafb: DC21030 [TGA] detected, rev=0x%02x\n",
1508*4882a593Smuzhiyun 			par->tga_chip_rev);
1509*4882a593Smuzhiyun 		pr_info("tgafb: at PCI bus %d, device %d, function %d\n",
1510*4882a593Smuzhiyun 			to_pci_dev(dev)->bus->number,
1511*4882a593Smuzhiyun 			PCI_SLOT(to_pci_dev(dev)->devfn),
1512*4882a593Smuzhiyun 			PCI_FUNC(to_pci_dev(dev)->devfn));
1513*4882a593Smuzhiyun 	}
1514*4882a593Smuzhiyun 	if (tga_bus_tc)
1515*4882a593Smuzhiyun 		pr_info("tgafb: SFB+ detected, rev=0x%02x\n",
1516*4882a593Smuzhiyun 			par->tga_chip_rev);
1517*4882a593Smuzhiyun 	fb_info(info, "%s frame buffer device at 0x%lx\n",
1518*4882a593Smuzhiyun 		info->fix.id, (long)bar0_start);
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 	return 0;
1521*4882a593Smuzhiyun 
1522*4882a593Smuzhiyun  err2:
1523*4882a593Smuzhiyun 	fb_dealloc_cmap(&info->cmap);
1524*4882a593Smuzhiyun  err1:
1525*4882a593Smuzhiyun 	if (mem_base)
1526*4882a593Smuzhiyun 		iounmap(mem_base);
1527*4882a593Smuzhiyun 	release_mem_region(bar0_start, bar0_len);
1528*4882a593Smuzhiyun  err0:
1529*4882a593Smuzhiyun 	framebuffer_release(info);
1530*4882a593Smuzhiyun 	return ret;
1531*4882a593Smuzhiyun }
1532*4882a593Smuzhiyun 
1533*4882a593Smuzhiyun static void tgafb_unregister(struct device *dev)
1534*4882a593Smuzhiyun {
1535*4882a593Smuzhiyun 	resource_size_t bar0_start = 0, bar0_len = 0;
1536*4882a593Smuzhiyun 	int tga_bus_pci = dev_is_pci(dev);
1537*4882a593Smuzhiyun 	int tga_bus_tc = TGA_BUS_TC(dev);
1538*4882a593Smuzhiyun 	struct fb_info *info = NULL;
1539*4882a593Smuzhiyun 	struct tga_par *par;
1540*4882a593Smuzhiyun 
1541*4882a593Smuzhiyun 	info = dev_get_drvdata(dev);
1542*4882a593Smuzhiyun 	if (!info)
1543*4882a593Smuzhiyun 		return;
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	par = info->par;
1546*4882a593Smuzhiyun 	unregister_framebuffer(info);
1547*4882a593Smuzhiyun 	fb_dealloc_cmap(&info->cmap);
1548*4882a593Smuzhiyun 	iounmap(par->tga_mem_base);
1549*4882a593Smuzhiyun 	if (tga_bus_pci) {
1550*4882a593Smuzhiyun 		bar0_start = pci_resource_start(to_pci_dev(dev), 0);
1551*4882a593Smuzhiyun 		bar0_len = pci_resource_len(to_pci_dev(dev), 0);
1552*4882a593Smuzhiyun 	}
1553*4882a593Smuzhiyun 	if (tga_bus_tc) {
1554*4882a593Smuzhiyun 		bar0_start = to_tc_dev(dev)->resource.start;
1555*4882a593Smuzhiyun 		bar0_len = to_tc_dev(dev)->resource.end - bar0_start + 1;
1556*4882a593Smuzhiyun 	}
1557*4882a593Smuzhiyun 	release_mem_region(bar0_start, bar0_len);
1558*4882a593Smuzhiyun 	framebuffer_release(info);
1559*4882a593Smuzhiyun }
1560*4882a593Smuzhiyun 
1561*4882a593Smuzhiyun static void tgafb_exit(void)
1562*4882a593Smuzhiyun {
1563*4882a593Smuzhiyun 	tc_unregister_driver(&tgafb_tc_driver);
1564*4882a593Smuzhiyun 	pci_unregister_driver(&tgafb_pci_driver);
1565*4882a593Smuzhiyun }
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun #ifndef MODULE
1568*4882a593Smuzhiyun static int tgafb_setup(char *arg)
1569*4882a593Smuzhiyun {
1570*4882a593Smuzhiyun 	char *this_opt;
1571*4882a593Smuzhiyun 
1572*4882a593Smuzhiyun 	if (arg && *arg) {
1573*4882a593Smuzhiyun 		while ((this_opt = strsep(&arg, ","))) {
1574*4882a593Smuzhiyun 			if (!*this_opt)
1575*4882a593Smuzhiyun 				continue;
1576*4882a593Smuzhiyun 			if (!strncmp(this_opt, "mode:", 5))
1577*4882a593Smuzhiyun 				mode_option = this_opt+5;
1578*4882a593Smuzhiyun 			else
1579*4882a593Smuzhiyun 				printk(KERN_ERR
1580*4882a593Smuzhiyun 				       "tgafb: unknown parameter %s\n",
1581*4882a593Smuzhiyun 				       this_opt);
1582*4882a593Smuzhiyun 		}
1583*4882a593Smuzhiyun 	}
1584*4882a593Smuzhiyun 
1585*4882a593Smuzhiyun 	return 0;
1586*4882a593Smuzhiyun }
1587*4882a593Smuzhiyun #endif /* !MODULE */
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun static int tgafb_init(void)
1590*4882a593Smuzhiyun {
1591*4882a593Smuzhiyun 	int status;
1592*4882a593Smuzhiyun #ifndef MODULE
1593*4882a593Smuzhiyun 	char *option = NULL;
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun 	if (fb_get_options("tgafb", &option))
1596*4882a593Smuzhiyun 		return -ENODEV;
1597*4882a593Smuzhiyun 	tgafb_setup(option);
1598*4882a593Smuzhiyun #endif
1599*4882a593Smuzhiyun 	status = pci_register_driver(&tgafb_pci_driver);
1600*4882a593Smuzhiyun 	if (!status)
1601*4882a593Smuzhiyun 		status = tc_register_driver(&tgafb_tc_driver);
1602*4882a593Smuzhiyun 	return status;
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun 
1605*4882a593Smuzhiyun /*
1606*4882a593Smuzhiyun  *  Modularisation
1607*4882a593Smuzhiyun  */
1608*4882a593Smuzhiyun 
1609*4882a593Smuzhiyun module_init(tgafb_init);
1610*4882a593Smuzhiyun module_exit(tgafb_exit);
1611*4882a593Smuzhiyun 
1612*4882a593Smuzhiyun MODULE_DESCRIPTION("Framebuffer driver for TGA/SFB+ chipset");
1613*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1614