xref: /OK3568_Linux_fs/kernel/drivers/video/fbdev/vga16fb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * linux/drivers/video/vga16.c -- VGA 16-color framebuffer driver
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright 1999 Ben Pfaff <pfaffben@debian.org> and Petr Vandrovec <VANDROVE@vc.cvut.cz>
5*4882a593Smuzhiyun  * Based on VGA info at http://www.goodnet.com/~tinara/FreeVGA/home.htm
6*4882a593Smuzhiyun  * Based on VESA framebuffer (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * This file is subject to the terms and conditions of the GNU General
9*4882a593Smuzhiyun  * Public License.  See the file COPYING in the main directory of this
10*4882a593Smuzhiyun  * archive for more details.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/kernel.h>
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <linux/string.h>
17*4882a593Smuzhiyun #include <linux/mm.h>
18*4882a593Smuzhiyun #include <linux/delay.h>
19*4882a593Smuzhiyun #include <linux/fb.h>
20*4882a593Smuzhiyun #include <linux/ioport.h>
21*4882a593Smuzhiyun #include <linux/init.h>
22*4882a593Smuzhiyun #include <linux/platform_device.h>
23*4882a593Smuzhiyun #include <linux/screen_info.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #include <asm/io.h>
26*4882a593Smuzhiyun #include <video/vga.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define VGA_FB_PHYS 0xA0000
29*4882a593Smuzhiyun #define VGA_FB_PHYS_LEN 65536
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #define MODE_SKIP4	1
32*4882a593Smuzhiyun #define MODE_8BPP	2
33*4882a593Smuzhiyun #define MODE_CFB	4
34*4882a593Smuzhiyun #define MODE_TEXT	8
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun  * card parameters
40*4882a593Smuzhiyun  */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun struct vga16fb_par {
43*4882a593Smuzhiyun 	/* structure holding original VGA register settings when the
44*4882a593Smuzhiyun            screen is blanked */
45*4882a593Smuzhiyun 	struct {
46*4882a593Smuzhiyun 		unsigned char	SeqCtrlIndex;	  /* Sequencer Index reg.   */
47*4882a593Smuzhiyun 		unsigned char	CrtCtrlIndex;	  /* CRT-Contr. Index reg.  */
48*4882a593Smuzhiyun 		unsigned char	CrtMiscIO;	  /* Miscellaneous register */
49*4882a593Smuzhiyun 		unsigned char	HorizontalTotal;  /* CRT-Controller:00h */
50*4882a593Smuzhiyun 		unsigned char	HorizDisplayEnd;  /* CRT-Controller:01h */
51*4882a593Smuzhiyun 		unsigned char	StartHorizRetrace;/* CRT-Controller:04h */
52*4882a593Smuzhiyun 		unsigned char	EndHorizRetrace;  /* CRT-Controller:05h */
53*4882a593Smuzhiyun 		unsigned char	Overflow;	  /* CRT-Controller:07h */
54*4882a593Smuzhiyun 		unsigned char	StartVertRetrace; /* CRT-Controller:10h */
55*4882a593Smuzhiyun 		unsigned char	EndVertRetrace;	  /* CRT-Controller:11h */
56*4882a593Smuzhiyun 		unsigned char	ModeControl;	  /* CRT-Controller:17h */
57*4882a593Smuzhiyun 		unsigned char	ClockingMode;	  /* Seq-Controller:01h */
58*4882a593Smuzhiyun 	} vga_state;
59*4882a593Smuzhiyun 	struct vgastate state;
60*4882a593Smuzhiyun 	unsigned int ref_count;
61*4882a593Smuzhiyun 	int palette_blanked, vesa_blanked, mode, isVGA;
62*4882a593Smuzhiyun 	u8 misc, pel_msk, vss, clkdiv;
63*4882a593Smuzhiyun 	u8 crtc[VGA_CRT_C];
64*4882a593Smuzhiyun };
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun /* --------------------------------------------------------------------- */
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun static struct fb_var_screeninfo vga16fb_defined = {
69*4882a593Smuzhiyun 	.xres		= 640,
70*4882a593Smuzhiyun 	.yres		= 480,
71*4882a593Smuzhiyun 	.xres_virtual	= 640,
72*4882a593Smuzhiyun 	.yres_virtual	= 480,
73*4882a593Smuzhiyun 	.bits_per_pixel	= 4,
74*4882a593Smuzhiyun 	.activate	= FB_ACTIVATE_TEST,
75*4882a593Smuzhiyun 	.height		= -1,
76*4882a593Smuzhiyun 	.width		= -1,
77*4882a593Smuzhiyun 	.pixclock	= 39721,
78*4882a593Smuzhiyun 	.left_margin	= 48,
79*4882a593Smuzhiyun 	.right_margin	= 16,
80*4882a593Smuzhiyun 	.upper_margin	= 33,
81*4882a593Smuzhiyun 	.lower_margin	= 10,
82*4882a593Smuzhiyun 	.hsync_len 	= 96,
83*4882a593Smuzhiyun 	.vsync_len	= 2,
84*4882a593Smuzhiyun 	.vmode		= FB_VMODE_NONINTERLACED,
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /* name should not depend on EGA/VGA */
88*4882a593Smuzhiyun static const struct fb_fix_screeninfo vga16fb_fix = {
89*4882a593Smuzhiyun 	.id		= "VGA16 VGA",
90*4882a593Smuzhiyun 	.smem_start	= VGA_FB_PHYS,
91*4882a593Smuzhiyun 	.smem_len	= VGA_FB_PHYS_LEN,
92*4882a593Smuzhiyun 	.type		= FB_TYPE_VGA_PLANES,
93*4882a593Smuzhiyun 	.type_aux	= FB_AUX_VGA_PLANES_VGA4,
94*4882a593Smuzhiyun 	.visual		= FB_VISUAL_PSEUDOCOLOR,
95*4882a593Smuzhiyun 	.xpanstep	= 8,
96*4882a593Smuzhiyun 	.ypanstep	= 1,
97*4882a593Smuzhiyun 	.line_length	= 640 / 8,
98*4882a593Smuzhiyun 	.accel		= FB_ACCEL_NONE
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun /* The VGA's weird architecture often requires that we read a byte and
102*4882a593Smuzhiyun    write a byte to the same location.  It doesn't matter *what* byte
103*4882a593Smuzhiyun    we write, however.  This is because all the action goes on behind
104*4882a593Smuzhiyun    the scenes in the VGA's 32-bit latch register, and reading and writing
105*4882a593Smuzhiyun    video memory just invokes latch behavior.
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun    To avoid race conditions (is this necessary?), reading and writing
108*4882a593Smuzhiyun    the memory byte should be done with a single instruction.  One
109*4882a593Smuzhiyun    suitable instruction is the x86 bitwise OR.  The following
110*4882a593Smuzhiyun    read-modify-write routine should optimize to one such bitwise
111*4882a593Smuzhiyun    OR. */
rmw(volatile char __iomem * p)112*4882a593Smuzhiyun static inline void rmw(volatile char __iomem *p)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	readb(p);
115*4882a593Smuzhiyun 	writeb(1, p);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /* Set the Graphics Mode Register, and return its previous value.
119*4882a593Smuzhiyun    Bits 0-1 are write mode, bit 3 is read mode. */
setmode(int mode)120*4882a593Smuzhiyun static inline int setmode(int mode)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	int oldmode;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	oldmode = vga_io_rgfx(VGA_GFX_MODE);
125*4882a593Smuzhiyun 	vga_io_w(VGA_GFX_D, mode);
126*4882a593Smuzhiyun 	return oldmode;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun /* Select the Bit Mask Register and return its value. */
selectmask(void)130*4882a593Smuzhiyun static inline int selectmask(void)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun 	return vga_io_rgfx(VGA_GFX_BIT_MASK);
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /* Set the value of the Bit Mask Register.  It must already have been
136*4882a593Smuzhiyun    selected with selectmask(). */
setmask(int mask)137*4882a593Smuzhiyun static inline void setmask(int mask)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	vga_io_w(VGA_GFX_D, mask);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /* Set the Data Rotate Register and return its old value.
143*4882a593Smuzhiyun    Bits 0-2 are rotate count, bits 3-4 are logical operation
144*4882a593Smuzhiyun    (0=NOP, 1=AND, 2=OR, 3=XOR). */
setop(int op)145*4882a593Smuzhiyun static inline int setop(int op)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	int oldop;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	oldop = vga_io_rgfx(VGA_GFX_DATA_ROTATE);
150*4882a593Smuzhiyun 	vga_io_w(VGA_GFX_D, op);
151*4882a593Smuzhiyun 	return oldop;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun /* Set the Enable Set/Reset Register and return its old value.
155*4882a593Smuzhiyun    The code here always uses value 0xf for this register. */
setsr(int sr)156*4882a593Smuzhiyun static inline int setsr(int sr)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun 	int oldsr;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	oldsr = vga_io_rgfx(VGA_GFX_SR_ENABLE);
161*4882a593Smuzhiyun 	vga_io_w(VGA_GFX_D, sr);
162*4882a593Smuzhiyun 	return oldsr;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /* Set the Set/Reset Register and return its old value. */
setcolor(int color)166*4882a593Smuzhiyun static inline int setcolor(int color)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	int oldcolor;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	oldcolor = vga_io_rgfx(VGA_GFX_SR_VALUE);
171*4882a593Smuzhiyun 	vga_io_w(VGA_GFX_D, color);
172*4882a593Smuzhiyun 	return oldcolor;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun /* Return the value in the Graphics Address Register. */
getindex(void)176*4882a593Smuzhiyun static inline int getindex(void)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	return vga_io_r(VGA_GFX_I);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /* Set the value in the Graphics Address Register. */
setindex(int index)182*4882a593Smuzhiyun static inline void setindex(int index)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	vga_io_w(VGA_GFX_I, index);
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /* Check if the video mode is supported by the driver */
check_mode_supported(void)188*4882a593Smuzhiyun static inline int check_mode_supported(void)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	/* non-x86 architectures treat orig_video_isVGA as a boolean flag */
191*4882a593Smuzhiyun #if defined(CONFIG_X86)
192*4882a593Smuzhiyun 	/* only EGA and VGA in 16 color graphic mode are supported */
193*4882a593Smuzhiyun 	if (screen_info.orig_video_isVGA != VIDEO_TYPE_EGAC &&
194*4882a593Smuzhiyun 	    screen_info.orig_video_isVGA != VIDEO_TYPE_VGAC)
195*4882a593Smuzhiyun 		return -ENODEV;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	if (screen_info.orig_video_mode != 0x0D &&	/* 320x200/4 (EGA) */
198*4882a593Smuzhiyun 	    screen_info.orig_video_mode != 0x0E &&	/* 640x200/4 (EGA) */
199*4882a593Smuzhiyun 	    screen_info.orig_video_mode != 0x10 &&	/* 640x350/4 (EGA) */
200*4882a593Smuzhiyun 	    screen_info.orig_video_mode != 0x12)	/* 640x480/4 (VGA) */
201*4882a593Smuzhiyun 		return -ENODEV;
202*4882a593Smuzhiyun #endif
203*4882a593Smuzhiyun 	return 0;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun 
vga16fb_pan_var(struct fb_info * info,struct fb_var_screeninfo * var)206*4882a593Smuzhiyun static void vga16fb_pan_var(struct fb_info *info,
207*4882a593Smuzhiyun 			    struct fb_var_screeninfo *var)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
210*4882a593Smuzhiyun 	u32 xoffset, pos;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	xoffset = var->xoffset;
213*4882a593Smuzhiyun 	if (info->var.bits_per_pixel == 8) {
214*4882a593Smuzhiyun 		pos = (info->var.xres_virtual * var->yoffset + xoffset) >> 2;
215*4882a593Smuzhiyun 	} else if (par->mode & MODE_TEXT) {
216*4882a593Smuzhiyun 		int fh = 16; // FIXME !!! font height. Fugde for now.
217*4882a593Smuzhiyun 		pos = (info->var.xres_virtual * (var->yoffset / fh) + xoffset) >> 3;
218*4882a593Smuzhiyun 	} else {
219*4882a593Smuzhiyun 		if (info->var.nonstd)
220*4882a593Smuzhiyun 			xoffset--;
221*4882a593Smuzhiyun 		pos = (info->var.xres_virtual * var->yoffset + xoffset) >> 3;
222*4882a593Smuzhiyun 	}
223*4882a593Smuzhiyun 	vga_io_wcrt(VGA_CRTC_START_HI, pos >> 8);
224*4882a593Smuzhiyun 	vga_io_wcrt(VGA_CRTC_START_LO, pos & 0xFF);
225*4882a593Smuzhiyun 	/* if we support CFB4, then we must! support xoffset with pixel
226*4882a593Smuzhiyun 	 * granularity if someone supports xoffset in bit resolution */
227*4882a593Smuzhiyun 	vga_io_r(VGA_IS1_RC);		/* reset flip-flop */
228*4882a593Smuzhiyun 	vga_io_w(VGA_ATT_IW, VGA_ATC_PEL);
229*4882a593Smuzhiyun 	if (info->var.bits_per_pixel == 8)
230*4882a593Smuzhiyun 		vga_io_w(VGA_ATT_IW, (xoffset & 3) << 1);
231*4882a593Smuzhiyun 	else
232*4882a593Smuzhiyun 		vga_io_w(VGA_ATT_IW, xoffset & 7);
233*4882a593Smuzhiyun 	vga_io_r(VGA_IS1_RC);
234*4882a593Smuzhiyun 	vga_io_w(VGA_ATT_IW, 0x20);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun 
vga16fb_update_fix(struct fb_info * info)237*4882a593Smuzhiyun static void vga16fb_update_fix(struct fb_info *info)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	if (info->var.bits_per_pixel == 4) {
240*4882a593Smuzhiyun 		if (info->var.nonstd) {
241*4882a593Smuzhiyun 			info->fix.type = FB_TYPE_PACKED_PIXELS;
242*4882a593Smuzhiyun 			info->fix.line_length = info->var.xres_virtual / 2;
243*4882a593Smuzhiyun 		} else {
244*4882a593Smuzhiyun 			info->fix.type = FB_TYPE_VGA_PLANES;
245*4882a593Smuzhiyun 			info->fix.type_aux = FB_AUX_VGA_PLANES_VGA4;
246*4882a593Smuzhiyun 			info->fix.line_length = info->var.xres_virtual / 8;
247*4882a593Smuzhiyun 		}
248*4882a593Smuzhiyun 	} else if (info->var.bits_per_pixel == 0) {
249*4882a593Smuzhiyun 		info->fix.type = FB_TYPE_TEXT;
250*4882a593Smuzhiyun 		info->fix.type_aux = FB_AUX_TEXT_CGA;
251*4882a593Smuzhiyun 		info->fix.line_length = info->var.xres_virtual / 4;
252*4882a593Smuzhiyun 	} else {	/* 8bpp */
253*4882a593Smuzhiyun 		if (info->var.nonstd) {
254*4882a593Smuzhiyun 			info->fix.type = FB_TYPE_VGA_PLANES;
255*4882a593Smuzhiyun 			info->fix.type_aux = FB_AUX_VGA_PLANES_CFB8;
256*4882a593Smuzhiyun 			info->fix.line_length = info->var.xres_virtual / 4;
257*4882a593Smuzhiyun 		} else {
258*4882a593Smuzhiyun 			info->fix.type = FB_TYPE_PACKED_PIXELS;
259*4882a593Smuzhiyun 			info->fix.line_length = info->var.xres_virtual;
260*4882a593Smuzhiyun 		}
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
vga16fb_clock_chip(struct vga16fb_par * par,unsigned int * pixclock,const struct fb_info * info,int mul,int div)264*4882a593Smuzhiyun static void vga16fb_clock_chip(struct vga16fb_par *par,
265*4882a593Smuzhiyun 			       unsigned int *pixclock,
266*4882a593Smuzhiyun 			       const struct fb_info *info,
267*4882a593Smuzhiyun 			       int mul, int div)
268*4882a593Smuzhiyun {
269*4882a593Smuzhiyun 	static const struct {
270*4882a593Smuzhiyun 		u32 pixclock;
271*4882a593Smuzhiyun 		u8  misc;
272*4882a593Smuzhiyun 		u8  seq_clock_mode;
273*4882a593Smuzhiyun 	} *ptr, *best, vgaclocks[] = {
274*4882a593Smuzhiyun 		{ 79442 /* 12.587 */, 0x00, 0x08},
275*4882a593Smuzhiyun 		{ 70616 /* 14.161 */, 0x04, 0x08},
276*4882a593Smuzhiyun 		{ 39721 /* 25.175 */, 0x00, 0x00},
277*4882a593Smuzhiyun 		{ 35308 /* 28.322 */, 0x04, 0x00},
278*4882a593Smuzhiyun 		{     0 /* bad */,    0x00, 0x00}};
279*4882a593Smuzhiyun 	int err;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	*pixclock = (*pixclock * mul) / div;
282*4882a593Smuzhiyun 	best = vgaclocks;
283*4882a593Smuzhiyun 	err = *pixclock - best->pixclock;
284*4882a593Smuzhiyun 	if (err < 0) err = -err;
285*4882a593Smuzhiyun 	for (ptr = vgaclocks + 1; ptr->pixclock; ptr++) {
286*4882a593Smuzhiyun 		int tmp;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 		tmp = *pixclock - ptr->pixclock;
289*4882a593Smuzhiyun 		if (tmp < 0) tmp = -tmp;
290*4882a593Smuzhiyun 		if (tmp < err) {
291*4882a593Smuzhiyun 			err = tmp;
292*4882a593Smuzhiyun 			best = ptr;
293*4882a593Smuzhiyun 		}
294*4882a593Smuzhiyun 	}
295*4882a593Smuzhiyun 	par->misc |= best->misc;
296*4882a593Smuzhiyun 	par->clkdiv = best->seq_clock_mode;
297*4882a593Smuzhiyun 	*pixclock = (best->pixclock * div) / mul;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun #define FAIL(X) return -EINVAL
301*4882a593Smuzhiyun 
vga16fb_open(struct fb_info * info,int user)302*4882a593Smuzhiyun static int vga16fb_open(struct fb_info *info, int user)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	if (!par->ref_count) {
307*4882a593Smuzhiyun 		memset(&par->state, 0, sizeof(struct vgastate));
308*4882a593Smuzhiyun 		par->state.flags = VGA_SAVE_FONTS | VGA_SAVE_MODE |
309*4882a593Smuzhiyun 			VGA_SAVE_CMAP;
310*4882a593Smuzhiyun 		save_vga(&par->state);
311*4882a593Smuzhiyun 	}
312*4882a593Smuzhiyun 	par->ref_count++;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	return 0;
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun 
vga16fb_release(struct fb_info * info,int user)317*4882a593Smuzhiyun static int vga16fb_release(struct fb_info *info, int user)
318*4882a593Smuzhiyun {
319*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	if (!par->ref_count)
322*4882a593Smuzhiyun 		return -EINVAL;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (par->ref_count == 1)
325*4882a593Smuzhiyun 		restore_vga(&par->state);
326*4882a593Smuzhiyun 	par->ref_count--;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	return 0;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
vga16fb_check_var(struct fb_var_screeninfo * var,struct fb_info * info)331*4882a593Smuzhiyun static int vga16fb_check_var(struct fb_var_screeninfo *var,
332*4882a593Smuzhiyun 			     struct fb_info *info)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
335*4882a593Smuzhiyun 	u32 xres, right, hslen, left, xtotal;
336*4882a593Smuzhiyun 	u32 yres, lower, vslen, upper, ytotal;
337*4882a593Smuzhiyun 	u32 vxres, xoffset, vyres, yoffset;
338*4882a593Smuzhiyun 	u32 pos;
339*4882a593Smuzhiyun 	u8 r7, rMode;
340*4882a593Smuzhiyun 	int shift;
341*4882a593Smuzhiyun 	int mode;
342*4882a593Smuzhiyun 	u32 maxmem;
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 	par->pel_msk = 0xFF;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	if (var->bits_per_pixel == 4) {
347*4882a593Smuzhiyun 		if (var->nonstd) {
348*4882a593Smuzhiyun 			if (!par->isVGA)
349*4882a593Smuzhiyun 				return -EINVAL;
350*4882a593Smuzhiyun 			shift = 3;
351*4882a593Smuzhiyun 			mode = MODE_SKIP4 | MODE_CFB;
352*4882a593Smuzhiyun 			maxmem = 16384;
353*4882a593Smuzhiyun 			par->pel_msk = 0x0F;
354*4882a593Smuzhiyun 		} else {
355*4882a593Smuzhiyun 			shift = 3;
356*4882a593Smuzhiyun 			mode = 0;
357*4882a593Smuzhiyun 			maxmem = 65536;
358*4882a593Smuzhiyun 		}
359*4882a593Smuzhiyun 	} else if (var->bits_per_pixel == 8) {
360*4882a593Smuzhiyun 		if (!par->isVGA)
361*4882a593Smuzhiyun 			return -EINVAL;	/* no support on EGA */
362*4882a593Smuzhiyun 		shift = 2;
363*4882a593Smuzhiyun 		if (var->nonstd) {
364*4882a593Smuzhiyun 			mode = MODE_8BPP | MODE_CFB;
365*4882a593Smuzhiyun 			maxmem = 65536;
366*4882a593Smuzhiyun 		} else {
367*4882a593Smuzhiyun 			mode = MODE_SKIP4 | MODE_8BPP | MODE_CFB;
368*4882a593Smuzhiyun 			maxmem = 16384;
369*4882a593Smuzhiyun 		}
370*4882a593Smuzhiyun 	} else
371*4882a593Smuzhiyun 		return -EINVAL;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	xres = (var->xres + 7) & ~7;
374*4882a593Smuzhiyun 	vxres = (var->xres_virtual + 0xF) & ~0xF;
375*4882a593Smuzhiyun 	xoffset = (var->xoffset + 7) & ~7;
376*4882a593Smuzhiyun 	left = (var->left_margin + 7) & ~7;
377*4882a593Smuzhiyun 	right = (var->right_margin + 7) & ~7;
378*4882a593Smuzhiyun 	hslen = (var->hsync_len + 7) & ~7;
379*4882a593Smuzhiyun 
380*4882a593Smuzhiyun 	if (vxres < xres)
381*4882a593Smuzhiyun 		vxres = xres;
382*4882a593Smuzhiyun 	if (xres + xoffset > vxres)
383*4882a593Smuzhiyun 		xoffset = vxres - xres;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	var->xres = xres;
386*4882a593Smuzhiyun 	var->right_margin = right;
387*4882a593Smuzhiyun 	var->hsync_len = hslen;
388*4882a593Smuzhiyun 	var->left_margin = left;
389*4882a593Smuzhiyun 	var->xres_virtual = vxres;
390*4882a593Smuzhiyun 	var->xoffset = xoffset;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	xres >>= shift;
393*4882a593Smuzhiyun 	right >>= shift;
394*4882a593Smuzhiyun 	hslen >>= shift;
395*4882a593Smuzhiyun 	left >>= shift;
396*4882a593Smuzhiyun 	vxres >>= shift;
397*4882a593Smuzhiyun 	xtotal = xres + right + hslen + left;
398*4882a593Smuzhiyun 	if (xtotal >= 256)
399*4882a593Smuzhiyun 		FAIL("xtotal too big");
400*4882a593Smuzhiyun 	if (hslen > 32)
401*4882a593Smuzhiyun 		FAIL("hslen too big");
402*4882a593Smuzhiyun 	if (right + hslen + left > 64)
403*4882a593Smuzhiyun 		FAIL("hblank too big");
404*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_H_TOTAL] = xtotal - 5;
405*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_H_BLANK_START] = xres - 1;
406*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_H_DISP] = xres - 1;
407*4882a593Smuzhiyun 	pos = xres + right;
408*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_H_SYNC_START] = pos;
409*4882a593Smuzhiyun 	pos += hslen;
410*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_H_SYNC_END] = pos & 0x1F;
411*4882a593Smuzhiyun 	pos += left - 2; /* blank_end + 2 <= total + 5 */
412*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_H_BLANK_END] = (pos & 0x1F) | 0x80;
413*4882a593Smuzhiyun 	if (pos & 0x20)
414*4882a593Smuzhiyun 		par->crtc[VGA_CRTC_H_SYNC_END] |= 0x80;
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	yres = var->yres;
417*4882a593Smuzhiyun 	lower = var->lower_margin;
418*4882a593Smuzhiyun 	vslen = var->vsync_len;
419*4882a593Smuzhiyun 	upper = var->upper_margin;
420*4882a593Smuzhiyun 	vyres = var->yres_virtual;
421*4882a593Smuzhiyun 	yoffset = var->yoffset;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	if (yres > vyres)
424*4882a593Smuzhiyun 		vyres = yres;
425*4882a593Smuzhiyun 	if (vxres * vyres > maxmem) {
426*4882a593Smuzhiyun 		vyres = maxmem / vxres;
427*4882a593Smuzhiyun 		if (vyres < yres)
428*4882a593Smuzhiyun 			return -ENOMEM;
429*4882a593Smuzhiyun 	}
430*4882a593Smuzhiyun 	if (yoffset + yres > vyres)
431*4882a593Smuzhiyun 		yoffset = vyres - yres;
432*4882a593Smuzhiyun 	var->yres = yres;
433*4882a593Smuzhiyun 	var->lower_margin = lower;
434*4882a593Smuzhiyun 	var->vsync_len = vslen;
435*4882a593Smuzhiyun 	var->upper_margin = upper;
436*4882a593Smuzhiyun 	var->yres_virtual = vyres;
437*4882a593Smuzhiyun 	var->yoffset = yoffset;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	if (var->vmode & FB_VMODE_DOUBLE) {
440*4882a593Smuzhiyun 		yres <<= 1;
441*4882a593Smuzhiyun 		lower <<= 1;
442*4882a593Smuzhiyun 		vslen <<= 1;
443*4882a593Smuzhiyun 		upper <<= 1;
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 	ytotal = yres + lower + vslen + upper;
446*4882a593Smuzhiyun 	if (ytotal > 1024) {
447*4882a593Smuzhiyun 		ytotal >>= 1;
448*4882a593Smuzhiyun 		yres >>= 1;
449*4882a593Smuzhiyun 		lower >>= 1;
450*4882a593Smuzhiyun 		vslen >>= 1;
451*4882a593Smuzhiyun 		upper >>= 1;
452*4882a593Smuzhiyun 		rMode = 0x04;
453*4882a593Smuzhiyun 	} else
454*4882a593Smuzhiyun 		rMode = 0x00;
455*4882a593Smuzhiyun 	if (ytotal > 1024)
456*4882a593Smuzhiyun 		FAIL("ytotal too big");
457*4882a593Smuzhiyun 	if (vslen > 16)
458*4882a593Smuzhiyun 		FAIL("vslen too big");
459*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_V_TOTAL] = ytotal - 2;
460*4882a593Smuzhiyun 	r7 = 0x10;	/* disable linecompare */
461*4882a593Smuzhiyun 	if (ytotal & 0x100) r7 |= 0x01;
462*4882a593Smuzhiyun 	if (ytotal & 0x200) r7 |= 0x20;
463*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_PRESET_ROW] = 0;
464*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_MAX_SCAN] = 0x40;	/* 1 scanline, no linecmp */
465*4882a593Smuzhiyun 	if (var->vmode & FB_VMODE_DOUBLE)
466*4882a593Smuzhiyun 		par->crtc[VGA_CRTC_MAX_SCAN] |= 0x80;
467*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_CURSOR_START] = 0x20;
468*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_CURSOR_END]   = 0x00;
469*4882a593Smuzhiyun 	if ((mode & (MODE_CFB | MODE_8BPP)) == MODE_CFB)
470*4882a593Smuzhiyun 		xoffset--;
471*4882a593Smuzhiyun 	pos = yoffset * vxres + (xoffset >> shift);
472*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_START_HI]     = pos >> 8;
473*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_START_LO]     = pos & 0xFF;
474*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_CURSOR_HI]    = 0x00;
475*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_CURSOR_LO]    = 0x00;
476*4882a593Smuzhiyun 	pos = yres - 1;
477*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_V_DISP_END] = pos & 0xFF;
478*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_V_BLANK_START] = pos & 0xFF;
479*4882a593Smuzhiyun 	if (pos & 0x100)
480*4882a593Smuzhiyun 		r7 |= 0x0A;	/* 0x02 -> DISP_END, 0x08 -> BLANK_START */
481*4882a593Smuzhiyun 	if (pos & 0x200) {
482*4882a593Smuzhiyun 		r7 |= 0x40;	/* 0x40 -> DISP_END */
483*4882a593Smuzhiyun 		par->crtc[VGA_CRTC_MAX_SCAN] |= 0x20; /* BLANK_START */
484*4882a593Smuzhiyun 	}
485*4882a593Smuzhiyun 	pos += lower;
486*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_V_SYNC_START] = pos & 0xFF;
487*4882a593Smuzhiyun 	if (pos & 0x100)
488*4882a593Smuzhiyun 		r7 |= 0x04;
489*4882a593Smuzhiyun 	if (pos & 0x200)
490*4882a593Smuzhiyun 		r7 |= 0x80;
491*4882a593Smuzhiyun 	pos += vslen;
492*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_V_SYNC_END] = (pos & 0x0F) & ~0x10; /* disabled IRQ */
493*4882a593Smuzhiyun 	pos += upper - 1; /* blank_end + 1 <= ytotal + 2 */
494*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_V_BLANK_END] = pos & 0xFF; /* 0x7F for original VGA,
495*4882a593Smuzhiyun                      but some SVGA chips requires all 8 bits to set */
496*4882a593Smuzhiyun 	if (vxres >= 512)
497*4882a593Smuzhiyun 		FAIL("vxres too long");
498*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_OFFSET] = vxres >> 1;
499*4882a593Smuzhiyun 	if (mode & MODE_SKIP4)
500*4882a593Smuzhiyun 		par->crtc[VGA_CRTC_UNDERLINE] = 0x5F;	/* 256, cfb8 */
501*4882a593Smuzhiyun 	else
502*4882a593Smuzhiyun 		par->crtc[VGA_CRTC_UNDERLINE] = 0x1F;	/* 16, vgap */
503*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_MODE] = rMode | ((mode & MODE_TEXT) ? 0xA3 : 0xE3);
504*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_LINE_COMPARE] = 0xFF;
505*4882a593Smuzhiyun 	par->crtc[VGA_CRTC_OVERFLOW] = r7;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	par->vss = 0x00;	/* 3DA */
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	par->misc = 0xE3;	/* enable CPU, ports 0x3Dx, positive sync */
510*4882a593Smuzhiyun 	if (var->sync & FB_SYNC_HOR_HIGH_ACT)
511*4882a593Smuzhiyun 		par->misc &= ~0x40;
512*4882a593Smuzhiyun 	if (var->sync & FB_SYNC_VERT_HIGH_ACT)
513*4882a593Smuzhiyun 		par->misc &= ~0x80;
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	par->mode = mode;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	if (mode & MODE_8BPP)
518*4882a593Smuzhiyun 		/* pixel clock == vga clock / 2 */
519*4882a593Smuzhiyun 		vga16fb_clock_chip(par, &var->pixclock, info, 1, 2);
520*4882a593Smuzhiyun 	else
521*4882a593Smuzhiyun 		/* pixel clock == vga clock */
522*4882a593Smuzhiyun 		vga16fb_clock_chip(par, &var->pixclock, info, 1, 1);
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	var->red.offset = var->green.offset = var->blue.offset =
525*4882a593Smuzhiyun 	var->transp.offset = 0;
526*4882a593Smuzhiyun 	var->red.length = var->green.length = var->blue.length =
527*4882a593Smuzhiyun 		(par->isVGA) ? 6 : 2;
528*4882a593Smuzhiyun 	var->transp.length = 0;
529*4882a593Smuzhiyun 	var->activate = FB_ACTIVATE_NOW;
530*4882a593Smuzhiyun 	var->height = -1;
531*4882a593Smuzhiyun 	var->width = -1;
532*4882a593Smuzhiyun 	var->accel_flags = 0;
533*4882a593Smuzhiyun 	return 0;
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun #undef FAIL
536*4882a593Smuzhiyun 
vga16fb_set_par(struct fb_info * info)537*4882a593Smuzhiyun static int vga16fb_set_par(struct fb_info *info)
538*4882a593Smuzhiyun {
539*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
540*4882a593Smuzhiyun 	u8 gdc[VGA_GFX_C];
541*4882a593Smuzhiyun 	u8 seq[VGA_SEQ_C];
542*4882a593Smuzhiyun 	u8 atc[VGA_ATT_C];
543*4882a593Smuzhiyun 	int fh, i;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	seq[VGA_SEQ_CLOCK_MODE] = 0x01 | par->clkdiv;
546*4882a593Smuzhiyun 	if (par->mode & MODE_TEXT)
547*4882a593Smuzhiyun 		seq[VGA_SEQ_PLANE_WRITE] = 0x03;
548*4882a593Smuzhiyun 	else
549*4882a593Smuzhiyun 		seq[VGA_SEQ_PLANE_WRITE] = 0x0F;
550*4882a593Smuzhiyun 	seq[VGA_SEQ_CHARACTER_MAP] = 0x00;
551*4882a593Smuzhiyun 	if (par->mode & MODE_TEXT)
552*4882a593Smuzhiyun 		seq[VGA_SEQ_MEMORY_MODE] = 0x03;
553*4882a593Smuzhiyun 	else if (par->mode & MODE_SKIP4)
554*4882a593Smuzhiyun 		seq[VGA_SEQ_MEMORY_MODE] = 0x0E;
555*4882a593Smuzhiyun 	else
556*4882a593Smuzhiyun 		seq[VGA_SEQ_MEMORY_MODE] = 0x06;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun 	gdc[VGA_GFX_SR_VALUE] = 0x00;
559*4882a593Smuzhiyun 	gdc[VGA_GFX_SR_ENABLE] = 0x00;
560*4882a593Smuzhiyun 	gdc[VGA_GFX_COMPARE_VALUE] = 0x00;
561*4882a593Smuzhiyun 	gdc[VGA_GFX_DATA_ROTATE] = 0x00;
562*4882a593Smuzhiyun 	gdc[VGA_GFX_PLANE_READ] = 0;
563*4882a593Smuzhiyun 	if (par->mode & MODE_TEXT) {
564*4882a593Smuzhiyun 		gdc[VGA_GFX_MODE] = 0x10;
565*4882a593Smuzhiyun 		gdc[VGA_GFX_MISC] = 0x06;
566*4882a593Smuzhiyun 	} else {
567*4882a593Smuzhiyun 		if (par->mode & MODE_CFB)
568*4882a593Smuzhiyun 			gdc[VGA_GFX_MODE] = 0x40;
569*4882a593Smuzhiyun 		else
570*4882a593Smuzhiyun 			gdc[VGA_GFX_MODE] = 0x00;
571*4882a593Smuzhiyun 		gdc[VGA_GFX_MISC] = 0x05;
572*4882a593Smuzhiyun 	}
573*4882a593Smuzhiyun 	gdc[VGA_GFX_COMPARE_MASK] = 0x0F;
574*4882a593Smuzhiyun 	gdc[VGA_GFX_BIT_MASK] = 0xFF;
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	for (i = 0x00; i < 0x10; i++)
577*4882a593Smuzhiyun 		atc[i] = i;
578*4882a593Smuzhiyun 	if (par->mode & MODE_TEXT)
579*4882a593Smuzhiyun 		atc[VGA_ATC_MODE] = 0x04;
580*4882a593Smuzhiyun 	else if (par->mode & MODE_8BPP)
581*4882a593Smuzhiyun 		atc[VGA_ATC_MODE] = 0x41;
582*4882a593Smuzhiyun 	else
583*4882a593Smuzhiyun 		atc[VGA_ATC_MODE] = 0x81;
584*4882a593Smuzhiyun 	atc[VGA_ATC_OVERSCAN] = 0x00;	/* 0 for EGA, 0xFF for VGA */
585*4882a593Smuzhiyun 	atc[VGA_ATC_PLANE_ENABLE] = 0x0F;
586*4882a593Smuzhiyun 	if (par->mode & MODE_8BPP)
587*4882a593Smuzhiyun 		atc[VGA_ATC_PEL] = (info->var.xoffset & 3) << 1;
588*4882a593Smuzhiyun 	else
589*4882a593Smuzhiyun 		atc[VGA_ATC_PEL] = info->var.xoffset & 7;
590*4882a593Smuzhiyun 	atc[VGA_ATC_COLOR_PAGE] = 0x00;
591*4882a593Smuzhiyun 
592*4882a593Smuzhiyun 	if (par->mode & MODE_TEXT) {
593*4882a593Smuzhiyun 		fh = 16; // FIXME !!! Fudge font height.
594*4882a593Smuzhiyun 		par->crtc[VGA_CRTC_MAX_SCAN] = (par->crtc[VGA_CRTC_MAX_SCAN]
595*4882a593Smuzhiyun 					       & ~0x1F) | (fh - 1);
596*4882a593Smuzhiyun 	}
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 	vga_io_w(VGA_MIS_W, vga_io_r(VGA_MIS_R) | 0x01);
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	/* Enable graphics register modification */
601*4882a593Smuzhiyun 	if (!par->isVGA) {
602*4882a593Smuzhiyun 		vga_io_w(EGA_GFX_E0, 0x00);
603*4882a593Smuzhiyun 		vga_io_w(EGA_GFX_E1, 0x01);
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	/* update misc output register */
607*4882a593Smuzhiyun 	vga_io_w(VGA_MIS_W, par->misc);
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	/* synchronous reset on */
610*4882a593Smuzhiyun 	vga_io_wseq(0x00, 0x01);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	if (par->isVGA)
613*4882a593Smuzhiyun 		vga_io_w(VGA_PEL_MSK, par->pel_msk);
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	/* write sequencer registers */
616*4882a593Smuzhiyun 	vga_io_wseq(VGA_SEQ_CLOCK_MODE, seq[VGA_SEQ_CLOCK_MODE] | 0x20);
617*4882a593Smuzhiyun 	for (i = 2; i < VGA_SEQ_C; i++) {
618*4882a593Smuzhiyun 		vga_io_wseq(i, seq[i]);
619*4882a593Smuzhiyun 	}
620*4882a593Smuzhiyun 
621*4882a593Smuzhiyun 	/* synchronous reset off */
622*4882a593Smuzhiyun 	vga_io_wseq(0x00, 0x03);
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	/* deprotect CRT registers 0-7 */
625*4882a593Smuzhiyun 	vga_io_wcrt(VGA_CRTC_V_SYNC_END, par->crtc[VGA_CRTC_V_SYNC_END]);
626*4882a593Smuzhiyun 
627*4882a593Smuzhiyun 	/* write CRT registers */
628*4882a593Smuzhiyun 	for (i = 0; i < VGA_CRTC_REGS; i++) {
629*4882a593Smuzhiyun 		vga_io_wcrt(i, par->crtc[i]);
630*4882a593Smuzhiyun 	}
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	/* write graphics controller registers */
633*4882a593Smuzhiyun 	for (i = 0; i < VGA_GFX_C; i++) {
634*4882a593Smuzhiyun 		vga_io_wgfx(i, gdc[i]);
635*4882a593Smuzhiyun 	}
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	/* write attribute controller registers */
638*4882a593Smuzhiyun 	for (i = 0; i < VGA_ATT_C; i++) {
639*4882a593Smuzhiyun 		vga_io_r(VGA_IS1_RC);		/* reset flip-flop */
640*4882a593Smuzhiyun 		vga_io_wattr(i, atc[i]);
641*4882a593Smuzhiyun 	}
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun 	/* Wait for screen to stabilize. */
644*4882a593Smuzhiyun 	mdelay(50);
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 	vga_io_wseq(VGA_SEQ_CLOCK_MODE, seq[VGA_SEQ_CLOCK_MODE]);
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	vga_io_r(VGA_IS1_RC);
649*4882a593Smuzhiyun 	vga_io_w(VGA_ATT_IW, 0x20);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	vga16fb_update_fix(info);
652*4882a593Smuzhiyun 	return 0;
653*4882a593Smuzhiyun }
654*4882a593Smuzhiyun 
ega16_setpalette(int regno,unsigned red,unsigned green,unsigned blue)655*4882a593Smuzhiyun static void ega16_setpalette(int regno, unsigned red, unsigned green, unsigned blue)
656*4882a593Smuzhiyun {
657*4882a593Smuzhiyun 	static const unsigned char map[] = { 000, 001, 010, 011 };
658*4882a593Smuzhiyun 	int val;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	if (regno >= 16)
661*4882a593Smuzhiyun 		return;
662*4882a593Smuzhiyun 	val = map[red>>14] | ((map[green>>14]) << 1) | ((map[blue>>14]) << 2);
663*4882a593Smuzhiyun 	vga_io_r(VGA_IS1_RC);   /* ! 0x3BA */
664*4882a593Smuzhiyun 	vga_io_wattr(regno, val);
665*4882a593Smuzhiyun 	vga_io_r(VGA_IS1_RC);   /* some clones need it */
666*4882a593Smuzhiyun 	vga_io_w(VGA_ATT_IW, 0x20); /* unblank screen */
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun 
vga16_setpalette(int regno,unsigned red,unsigned green,unsigned blue)669*4882a593Smuzhiyun static void vga16_setpalette(int regno, unsigned red, unsigned green, unsigned blue)
670*4882a593Smuzhiyun {
671*4882a593Smuzhiyun 	outb(regno,       VGA_PEL_IW);
672*4882a593Smuzhiyun 	outb(red   >> 10, VGA_PEL_D);
673*4882a593Smuzhiyun 	outb(green >> 10, VGA_PEL_D);
674*4882a593Smuzhiyun 	outb(blue  >> 10, VGA_PEL_D);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun 
vga16fb_setcolreg(unsigned regno,unsigned red,unsigned green,unsigned blue,unsigned transp,struct fb_info * info)677*4882a593Smuzhiyun static int vga16fb_setcolreg(unsigned regno, unsigned red, unsigned green,
678*4882a593Smuzhiyun 			     unsigned blue, unsigned transp,
679*4882a593Smuzhiyun 			     struct fb_info *info)
680*4882a593Smuzhiyun {
681*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
682*4882a593Smuzhiyun 	int gray;
683*4882a593Smuzhiyun 
684*4882a593Smuzhiyun 	/*
685*4882a593Smuzhiyun 	 *  Set a single color register. The values supplied are
686*4882a593Smuzhiyun 	 *  already rounded down to the hardware's capabilities
687*4882a593Smuzhiyun 	 *  (according to the entries in the `var' structure). Return
688*4882a593Smuzhiyun 	 *  != 0 for invalid regno.
689*4882a593Smuzhiyun 	 */
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	if (regno >= 256)
692*4882a593Smuzhiyun 		return 1;
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	gray = info->var.grayscale;
695*4882a593Smuzhiyun 
696*4882a593Smuzhiyun 	if (gray) {
697*4882a593Smuzhiyun 		/* gray = 0.30*R + 0.59*G + 0.11*B */
698*4882a593Smuzhiyun 		red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
699*4882a593Smuzhiyun 	}
700*4882a593Smuzhiyun 	if (par->isVGA)
701*4882a593Smuzhiyun 		vga16_setpalette(regno,red,green,blue);
702*4882a593Smuzhiyun 	else
703*4882a593Smuzhiyun 		ega16_setpalette(regno,red,green,blue);
704*4882a593Smuzhiyun 	return 0;
705*4882a593Smuzhiyun }
706*4882a593Smuzhiyun 
vga16fb_pan_display(struct fb_var_screeninfo * var,struct fb_info * info)707*4882a593Smuzhiyun static int vga16fb_pan_display(struct fb_var_screeninfo *var,
708*4882a593Smuzhiyun 			       struct fb_info *info)
709*4882a593Smuzhiyun {
710*4882a593Smuzhiyun 	vga16fb_pan_var(info, var);
711*4882a593Smuzhiyun 	return 0;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun 
714*4882a593Smuzhiyun /* The following VESA blanking code is taken from vgacon.c.  The VGA
715*4882a593Smuzhiyun    blanking code was originally by Huang shi chao, and modified by
716*4882a593Smuzhiyun    Christoph Rimek (chrimek@toppoint.de) and todd j. derr
717*4882a593Smuzhiyun    (tjd@barefoot.org) for Linux. */
718*4882a593Smuzhiyun 
vga_vesa_blank(struct vga16fb_par * par,int mode)719*4882a593Smuzhiyun static void vga_vesa_blank(struct vga16fb_par *par, int mode)
720*4882a593Smuzhiyun {
721*4882a593Smuzhiyun 	unsigned char SeqCtrlIndex = vga_io_r(VGA_SEQ_I);
722*4882a593Smuzhiyun 	unsigned char CrtCtrlIndex = vga_io_r(VGA_CRT_IC);
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	/* save original values of VGA controller registers */
725*4882a593Smuzhiyun 	if(!par->vesa_blanked) {
726*4882a593Smuzhiyun 		par->vga_state.CrtMiscIO = vga_io_r(VGA_MIS_R);
727*4882a593Smuzhiyun 		//sti();
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 		par->vga_state.HorizontalTotal = vga_io_rcrt(0x00);	/* HorizontalTotal */
730*4882a593Smuzhiyun 		par->vga_state.HorizDisplayEnd = vga_io_rcrt(0x01);	/* HorizDisplayEnd */
731*4882a593Smuzhiyun 		par->vga_state.StartHorizRetrace = vga_io_rcrt(0x04);	/* StartHorizRetrace */
732*4882a593Smuzhiyun 		par->vga_state.EndHorizRetrace = vga_io_rcrt(0x05);	/* EndHorizRetrace */
733*4882a593Smuzhiyun 		par->vga_state.Overflow = vga_io_rcrt(0x07);		/* Overflow */
734*4882a593Smuzhiyun 		par->vga_state.StartVertRetrace = vga_io_rcrt(0x10);	/* StartVertRetrace */
735*4882a593Smuzhiyun 		par->vga_state.EndVertRetrace = vga_io_rcrt(0x11);	/* EndVertRetrace */
736*4882a593Smuzhiyun 		par->vga_state.ModeControl = vga_io_rcrt(0x17);	/* ModeControl */
737*4882a593Smuzhiyun 		par->vga_state.ClockingMode = vga_io_rseq(0x01);	/* ClockingMode */
738*4882a593Smuzhiyun 	}
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	/* assure that video is enabled */
741*4882a593Smuzhiyun 	/* "0x20" is VIDEO_ENABLE_bit in register 01 of sequencer */
742*4882a593Smuzhiyun 	vga_io_wseq(0x01, par->vga_state.ClockingMode | 0x20);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	/* test for vertical retrace in process.... */
745*4882a593Smuzhiyun 	if ((par->vga_state.CrtMiscIO & 0x80) == 0x80)
746*4882a593Smuzhiyun 		vga_io_w(VGA_MIS_W, par->vga_state.CrtMiscIO & 0xef);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	/*
749*4882a593Smuzhiyun 	 * Set <End of vertical retrace> to minimum (0) and
750*4882a593Smuzhiyun 	 * <Start of vertical Retrace> to maximum (incl. overflow)
751*4882a593Smuzhiyun 	 * Result: turn off vertical sync (VSync) pulse.
752*4882a593Smuzhiyun 	 */
753*4882a593Smuzhiyun 	if (mode & FB_BLANK_VSYNC_SUSPEND) {
754*4882a593Smuzhiyun 		vga_io_wcrt(VGA_CRTC_V_SYNC_START, 0xff);
755*4882a593Smuzhiyun 		vga_io_wcrt(VGA_CRTC_V_SYNC_END, 0x40);
756*4882a593Smuzhiyun 		/* bits 9,10 of vert. retrace */
757*4882a593Smuzhiyun 		vga_io_wcrt(VGA_CRTC_OVERFLOW, par->vga_state.Overflow | 0x84);
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	if (mode & FB_BLANK_HSYNC_SUSPEND) {
761*4882a593Smuzhiyun 		/*
762*4882a593Smuzhiyun 		 * Set <End of horizontal retrace> to minimum (0) and
763*4882a593Smuzhiyun 		 *  <Start of horizontal Retrace> to maximum
764*4882a593Smuzhiyun 		 * Result: turn off horizontal sync (HSync) pulse.
765*4882a593Smuzhiyun 		 */
766*4882a593Smuzhiyun 		vga_io_wcrt(VGA_CRTC_H_SYNC_START, 0xff);
767*4882a593Smuzhiyun 		vga_io_wcrt(VGA_CRTC_H_SYNC_END, 0x00);
768*4882a593Smuzhiyun 	}
769*4882a593Smuzhiyun 
770*4882a593Smuzhiyun 	/* restore both index registers */
771*4882a593Smuzhiyun 	outb_p(SeqCtrlIndex, VGA_SEQ_I);
772*4882a593Smuzhiyun 	outb_p(CrtCtrlIndex, VGA_CRT_IC);
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun 
vga_vesa_unblank(struct vga16fb_par * par)775*4882a593Smuzhiyun static void vga_vesa_unblank(struct vga16fb_par *par)
776*4882a593Smuzhiyun {
777*4882a593Smuzhiyun 	unsigned char SeqCtrlIndex = vga_io_r(VGA_SEQ_I);
778*4882a593Smuzhiyun 	unsigned char CrtCtrlIndex = vga_io_r(VGA_CRT_IC);
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	/* restore original values of VGA controller registers */
781*4882a593Smuzhiyun 	vga_io_w(VGA_MIS_W, par->vga_state.CrtMiscIO);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	/* HorizontalTotal */
784*4882a593Smuzhiyun 	vga_io_wcrt(0x00, par->vga_state.HorizontalTotal);
785*4882a593Smuzhiyun 	/* HorizDisplayEnd */
786*4882a593Smuzhiyun 	vga_io_wcrt(0x01, par->vga_state.HorizDisplayEnd);
787*4882a593Smuzhiyun 	/* StartHorizRetrace */
788*4882a593Smuzhiyun 	vga_io_wcrt(0x04, par->vga_state.StartHorizRetrace);
789*4882a593Smuzhiyun 	/* EndHorizRetrace */
790*4882a593Smuzhiyun 	vga_io_wcrt(0x05, par->vga_state.EndHorizRetrace);
791*4882a593Smuzhiyun 	/* Overflow */
792*4882a593Smuzhiyun 	vga_io_wcrt(0x07, par->vga_state.Overflow);
793*4882a593Smuzhiyun 	/* StartVertRetrace */
794*4882a593Smuzhiyun 	vga_io_wcrt(0x10, par->vga_state.StartVertRetrace);
795*4882a593Smuzhiyun 	/* EndVertRetrace */
796*4882a593Smuzhiyun 	vga_io_wcrt(0x11, par->vga_state.EndVertRetrace);
797*4882a593Smuzhiyun 	/* ModeControl */
798*4882a593Smuzhiyun 	vga_io_wcrt(0x17, par->vga_state.ModeControl);
799*4882a593Smuzhiyun 	/* ClockingMode */
800*4882a593Smuzhiyun 	vga_io_wseq(0x01, par->vga_state.ClockingMode);
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	/* restore index/control registers */
803*4882a593Smuzhiyun 	vga_io_w(VGA_SEQ_I, SeqCtrlIndex);
804*4882a593Smuzhiyun 	vga_io_w(VGA_CRT_IC, CrtCtrlIndex);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun 
vga_pal_blank(void)807*4882a593Smuzhiyun static void vga_pal_blank(void)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun 	int i;
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	for (i=0; i<16; i++) {
812*4882a593Smuzhiyun 		outb_p(i, VGA_PEL_IW);
813*4882a593Smuzhiyun 		outb_p(0, VGA_PEL_D);
814*4882a593Smuzhiyun 		outb_p(0, VGA_PEL_D);
815*4882a593Smuzhiyun 		outb_p(0, VGA_PEL_D);
816*4882a593Smuzhiyun 	}
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
819*4882a593Smuzhiyun /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
vga16fb_blank(int blank,struct fb_info * info)820*4882a593Smuzhiyun static int vga16fb_blank(int blank, struct fb_info *info)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 	switch (blank) {
825*4882a593Smuzhiyun 	case FB_BLANK_UNBLANK:				/* Unblank */
826*4882a593Smuzhiyun 		if (par->vesa_blanked) {
827*4882a593Smuzhiyun 			vga_vesa_unblank(par);
828*4882a593Smuzhiyun 			par->vesa_blanked = 0;
829*4882a593Smuzhiyun 		}
830*4882a593Smuzhiyun 		if (par->palette_blanked) {
831*4882a593Smuzhiyun 			par->palette_blanked = 0;
832*4882a593Smuzhiyun 		}
833*4882a593Smuzhiyun 		break;
834*4882a593Smuzhiyun 	case FB_BLANK_NORMAL:				/* blank */
835*4882a593Smuzhiyun 		vga_pal_blank();
836*4882a593Smuzhiyun 		par->palette_blanked = 1;
837*4882a593Smuzhiyun 		break;
838*4882a593Smuzhiyun 	default:			/* VESA blanking */
839*4882a593Smuzhiyun 		vga_vesa_blank(par, blank);
840*4882a593Smuzhiyun 		par->vesa_blanked = 1;
841*4882a593Smuzhiyun 		break;
842*4882a593Smuzhiyun 	}
843*4882a593Smuzhiyun 	return 0;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun 
vga_8planes_fillrect(struct fb_info * info,const struct fb_fillrect * rect)846*4882a593Smuzhiyun static void vga_8planes_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
847*4882a593Smuzhiyun {
848*4882a593Smuzhiyun 	u32 dx = rect->dx, width = rect->width;
849*4882a593Smuzhiyun         char oldindex = getindex();
850*4882a593Smuzhiyun         char oldmode = setmode(0x40);
851*4882a593Smuzhiyun         char oldmask = selectmask();
852*4882a593Smuzhiyun         int line_ofs, height;
853*4882a593Smuzhiyun         char oldop, oldsr;
854*4882a593Smuzhiyun         char __iomem *where;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun         dx /= 4;
857*4882a593Smuzhiyun         where = info->screen_base + dx + rect->dy * info->fix.line_length;
858*4882a593Smuzhiyun 
859*4882a593Smuzhiyun         if (rect->rop == ROP_COPY) {
860*4882a593Smuzhiyun                 oldop = setop(0);
861*4882a593Smuzhiyun                 oldsr = setsr(0);
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun                 width /= 4;
864*4882a593Smuzhiyun                 line_ofs = info->fix.line_length - width;
865*4882a593Smuzhiyun                 setmask(0xff);
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun                 height = rect->height;
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun                 while (height--) {
870*4882a593Smuzhiyun                         int x;
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun                         /* we can do memset... */
873*4882a593Smuzhiyun                         for (x = width; x > 0; --x) {
874*4882a593Smuzhiyun                                 writeb(rect->color, where);
875*4882a593Smuzhiyun                                 where++;
876*4882a593Smuzhiyun                         }
877*4882a593Smuzhiyun                         where += line_ofs;
878*4882a593Smuzhiyun                 }
879*4882a593Smuzhiyun         } else {
880*4882a593Smuzhiyun                 char oldcolor = setcolor(0xf);
881*4882a593Smuzhiyun                 int y;
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun                 oldop = setop(0x18);
884*4882a593Smuzhiyun                 oldsr = setsr(0xf);
885*4882a593Smuzhiyun                 setmask(0x0F);
886*4882a593Smuzhiyun                 for (y = 0; y < rect->height; y++) {
887*4882a593Smuzhiyun                         rmw(where);
888*4882a593Smuzhiyun                         rmw(where+1);
889*4882a593Smuzhiyun                         where += info->fix.line_length;
890*4882a593Smuzhiyun                 }
891*4882a593Smuzhiyun                 setcolor(oldcolor);
892*4882a593Smuzhiyun         }
893*4882a593Smuzhiyun         setmask(oldmask);
894*4882a593Smuzhiyun         setsr(oldsr);
895*4882a593Smuzhiyun         setop(oldop);
896*4882a593Smuzhiyun         setmode(oldmode);
897*4882a593Smuzhiyun         setindex(oldindex);
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
vga16fb_fillrect(struct fb_info * info,const struct fb_fillrect * rect)900*4882a593Smuzhiyun static void vga16fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun 	int x, x2, y2, vxres, vyres, width, height, line_ofs;
903*4882a593Smuzhiyun 	char __iomem *dst;
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun 	vxres = info->var.xres_virtual;
906*4882a593Smuzhiyun 	vyres = info->var.yres_virtual;
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	if (!rect->width || !rect->height || rect->dx > vxres || rect->dy > vyres)
909*4882a593Smuzhiyun 		return;
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 	/* We could use hardware clipping but on many cards you get around
912*4882a593Smuzhiyun 	 * hardware clipping by writing to framebuffer directly. */
913*4882a593Smuzhiyun 
914*4882a593Smuzhiyun 	x2 = rect->dx + rect->width;
915*4882a593Smuzhiyun 	y2 = rect->dy + rect->height;
916*4882a593Smuzhiyun 	x2 = x2 < vxres ? x2 : vxres;
917*4882a593Smuzhiyun 	y2 = y2 < vyres ? y2 : vyres;
918*4882a593Smuzhiyun 	width = x2 - rect->dx;
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	switch (info->fix.type) {
921*4882a593Smuzhiyun 	case FB_TYPE_VGA_PLANES:
922*4882a593Smuzhiyun 		if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 			height = y2 - rect->dy;
925*4882a593Smuzhiyun 			width = rect->width/8;
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 			line_ofs = info->fix.line_length - width;
928*4882a593Smuzhiyun 			dst = info->screen_base + (rect->dx/8) + rect->dy * info->fix.line_length;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 			switch (rect->rop) {
931*4882a593Smuzhiyun 			case ROP_COPY:
932*4882a593Smuzhiyun 				setmode(0);
933*4882a593Smuzhiyun 				setop(0);
934*4882a593Smuzhiyun 				setsr(0xf);
935*4882a593Smuzhiyun 				setcolor(rect->color);
936*4882a593Smuzhiyun 				selectmask();
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 				setmask(0xff);
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 				while (height--) {
941*4882a593Smuzhiyun 					for (x = 0; x < width; x++) {
942*4882a593Smuzhiyun 						writeb(0, dst);
943*4882a593Smuzhiyun 						dst++;
944*4882a593Smuzhiyun 					}
945*4882a593Smuzhiyun 					dst += line_ofs;
946*4882a593Smuzhiyun 				}
947*4882a593Smuzhiyun 				break;
948*4882a593Smuzhiyun 			case ROP_XOR:
949*4882a593Smuzhiyun 				setmode(0);
950*4882a593Smuzhiyun 				setop(0x18);
951*4882a593Smuzhiyun 				setsr(0xf);
952*4882a593Smuzhiyun 				setcolor(0xf);
953*4882a593Smuzhiyun 				selectmask();
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 				setmask(0xff);
956*4882a593Smuzhiyun 				while (height--) {
957*4882a593Smuzhiyun 					for (x = 0; x < width; x++) {
958*4882a593Smuzhiyun 						rmw(dst);
959*4882a593Smuzhiyun 						dst++;
960*4882a593Smuzhiyun 					}
961*4882a593Smuzhiyun 					dst += line_ofs;
962*4882a593Smuzhiyun 				}
963*4882a593Smuzhiyun 				break;
964*4882a593Smuzhiyun 			}
965*4882a593Smuzhiyun 		} else
966*4882a593Smuzhiyun 			vga_8planes_fillrect(info, rect);
967*4882a593Smuzhiyun 		break;
968*4882a593Smuzhiyun 	case FB_TYPE_PACKED_PIXELS:
969*4882a593Smuzhiyun 	default:
970*4882a593Smuzhiyun 		cfb_fillrect(info, rect);
971*4882a593Smuzhiyun 		break;
972*4882a593Smuzhiyun 	}
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun 
vga_8planes_copyarea(struct fb_info * info,const struct fb_copyarea * area)975*4882a593Smuzhiyun static void vga_8planes_copyarea(struct fb_info *info, const struct fb_copyarea *area)
976*4882a593Smuzhiyun {
977*4882a593Smuzhiyun         char oldindex = getindex();
978*4882a593Smuzhiyun         char oldmode = setmode(0x41);
979*4882a593Smuzhiyun         char oldop = setop(0);
980*4882a593Smuzhiyun         char oldsr = setsr(0xf);
981*4882a593Smuzhiyun         int height, line_ofs, x;
982*4882a593Smuzhiyun 	u32 sx, dx, width;
983*4882a593Smuzhiyun 	char __iomem *dest;
984*4882a593Smuzhiyun 	char __iomem *src;
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun         height = area->height;
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun         sx = area->sx / 4;
989*4882a593Smuzhiyun         dx = area->dx / 4;
990*4882a593Smuzhiyun         width = area->width / 4;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun         if (area->dy < area->sy || (area->dy == area->sy && dx < sx)) {
993*4882a593Smuzhiyun                 line_ofs = info->fix.line_length - width;
994*4882a593Smuzhiyun                 dest = info->screen_base + dx + area->dy * info->fix.line_length;
995*4882a593Smuzhiyun                 src = info->screen_base + sx + area->sy * info->fix.line_length;
996*4882a593Smuzhiyun                 while (height--) {
997*4882a593Smuzhiyun                         for (x = 0; x < width; x++) {
998*4882a593Smuzhiyun                                 readb(src);
999*4882a593Smuzhiyun                                 writeb(0, dest);
1000*4882a593Smuzhiyun                                 src++;
1001*4882a593Smuzhiyun                                 dest++;
1002*4882a593Smuzhiyun                         }
1003*4882a593Smuzhiyun                         src += line_ofs;
1004*4882a593Smuzhiyun                         dest += line_ofs;
1005*4882a593Smuzhiyun                 }
1006*4882a593Smuzhiyun         } else {
1007*4882a593Smuzhiyun                 line_ofs = info->fix.line_length - width;
1008*4882a593Smuzhiyun                 dest = info->screen_base + dx + width +
1009*4882a593Smuzhiyun 			(area->dy + height - 1) * info->fix.line_length;
1010*4882a593Smuzhiyun                 src = info->screen_base + sx + width +
1011*4882a593Smuzhiyun 			(area->sy + height - 1) * info->fix.line_length;
1012*4882a593Smuzhiyun                 while (height--) {
1013*4882a593Smuzhiyun                         for (x = 0; x < width; x++) {
1014*4882a593Smuzhiyun                                 --src;
1015*4882a593Smuzhiyun                                 --dest;
1016*4882a593Smuzhiyun                                 readb(src);
1017*4882a593Smuzhiyun                                 writeb(0, dest);
1018*4882a593Smuzhiyun                         }
1019*4882a593Smuzhiyun                         src -= line_ofs;
1020*4882a593Smuzhiyun                         dest -= line_ofs;
1021*4882a593Smuzhiyun                 }
1022*4882a593Smuzhiyun         }
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun         setsr(oldsr);
1025*4882a593Smuzhiyun         setop(oldop);
1026*4882a593Smuzhiyun         setmode(oldmode);
1027*4882a593Smuzhiyun         setindex(oldindex);
1028*4882a593Smuzhiyun }
1029*4882a593Smuzhiyun 
vga16fb_copyarea(struct fb_info * info,const struct fb_copyarea * area)1030*4882a593Smuzhiyun static void vga16fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
1031*4882a593Smuzhiyun {
1032*4882a593Smuzhiyun 	u32 dx = area->dx, dy = area->dy, sx = area->sx, sy = area->sy;
1033*4882a593Smuzhiyun 	int x, x2, y2, old_dx, old_dy, vxres, vyres;
1034*4882a593Smuzhiyun 	int height, width, line_ofs;
1035*4882a593Smuzhiyun 	char __iomem *dst = NULL;
1036*4882a593Smuzhiyun 	char __iomem *src = NULL;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	vxres = info->var.xres_virtual;
1039*4882a593Smuzhiyun 	vyres = info->var.yres_virtual;
1040*4882a593Smuzhiyun 
1041*4882a593Smuzhiyun 	if (area->dx > vxres || area->sx > vxres || area->dy > vyres ||
1042*4882a593Smuzhiyun 	    area->sy > vyres)
1043*4882a593Smuzhiyun 		return;
1044*4882a593Smuzhiyun 
1045*4882a593Smuzhiyun 	/* clip the destination */
1046*4882a593Smuzhiyun 	old_dx = area->dx;
1047*4882a593Smuzhiyun 	old_dy = area->dy;
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	/*
1050*4882a593Smuzhiyun 	 * We could use hardware clipping but on many cards you get around
1051*4882a593Smuzhiyun 	 * hardware clipping by writing to framebuffer directly.
1052*4882a593Smuzhiyun 	 */
1053*4882a593Smuzhiyun 	x2 = area->dx + area->width;
1054*4882a593Smuzhiyun 	y2 = area->dy + area->height;
1055*4882a593Smuzhiyun 	dx = area->dx > 0 ? area->dx : 0;
1056*4882a593Smuzhiyun 	dy = area->dy > 0 ? area->dy : 0;
1057*4882a593Smuzhiyun 	x2 = x2 < vxres ? x2 : vxres;
1058*4882a593Smuzhiyun 	y2 = y2 < vyres ? y2 : vyres;
1059*4882a593Smuzhiyun 	width = x2 - dx;
1060*4882a593Smuzhiyun 	height = y2 - dy;
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	if (sx + dx < old_dx || sy + dy < old_dy)
1063*4882a593Smuzhiyun 		return;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	/* update sx1,sy1 */
1066*4882a593Smuzhiyun 	sx += (dx - old_dx);
1067*4882a593Smuzhiyun 	sy += (dy - old_dy);
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	/* the source must be completely inside the virtual screen */
1070*4882a593Smuzhiyun 	if (sx + width > vxres || sy + height > vyres)
1071*4882a593Smuzhiyun 		return;
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	switch (info->fix.type) {
1074*4882a593Smuzhiyun 	case FB_TYPE_VGA_PLANES:
1075*4882a593Smuzhiyun 		if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
1076*4882a593Smuzhiyun 			width = width/8;
1077*4882a593Smuzhiyun 			line_ofs = info->fix.line_length - width;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 			setmode(1);
1080*4882a593Smuzhiyun 			setop(0);
1081*4882a593Smuzhiyun 			setsr(0xf);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 			if (dy < sy || (dy == sy && dx < sx)) {
1084*4882a593Smuzhiyun 				dst = info->screen_base + (dx/8) + dy * info->fix.line_length;
1085*4882a593Smuzhiyun 				src = info->screen_base + (sx/8) + sy * info->fix.line_length;
1086*4882a593Smuzhiyun 				while (height--) {
1087*4882a593Smuzhiyun 					for (x = 0; x < width; x++) {
1088*4882a593Smuzhiyun 						readb(src);
1089*4882a593Smuzhiyun 						writeb(0, dst);
1090*4882a593Smuzhiyun 						dst++;
1091*4882a593Smuzhiyun 						src++;
1092*4882a593Smuzhiyun 					}
1093*4882a593Smuzhiyun 					src += line_ofs;
1094*4882a593Smuzhiyun 					dst += line_ofs;
1095*4882a593Smuzhiyun 				}
1096*4882a593Smuzhiyun 			} else {
1097*4882a593Smuzhiyun 				dst = info->screen_base + (dx/8) + width +
1098*4882a593Smuzhiyun 					(dy + height - 1) * info->fix.line_length;
1099*4882a593Smuzhiyun 				src = info->screen_base + (sx/8) + width +
1100*4882a593Smuzhiyun 					(sy + height  - 1) * info->fix.line_length;
1101*4882a593Smuzhiyun 				while (height--) {
1102*4882a593Smuzhiyun 					for (x = 0; x < width; x++) {
1103*4882a593Smuzhiyun 						dst--;
1104*4882a593Smuzhiyun 						src--;
1105*4882a593Smuzhiyun 						readb(src);
1106*4882a593Smuzhiyun 						writeb(0, dst);
1107*4882a593Smuzhiyun 					}
1108*4882a593Smuzhiyun 					src -= line_ofs;
1109*4882a593Smuzhiyun 					dst -= line_ofs;
1110*4882a593Smuzhiyun 				}
1111*4882a593Smuzhiyun 			}
1112*4882a593Smuzhiyun 		} else
1113*4882a593Smuzhiyun 			vga_8planes_copyarea(info, area);
1114*4882a593Smuzhiyun 		break;
1115*4882a593Smuzhiyun 	case FB_TYPE_PACKED_PIXELS:
1116*4882a593Smuzhiyun 	default:
1117*4882a593Smuzhiyun 		cfb_copyarea(info, area);
1118*4882a593Smuzhiyun 		break;
1119*4882a593Smuzhiyun 	}
1120*4882a593Smuzhiyun }
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun #define TRANS_MASK_LOW  {0x0,0x8,0x4,0xC,0x2,0xA,0x6,0xE,0x1,0x9,0x5,0xD,0x3,0xB,0x7,0xF}
1123*4882a593Smuzhiyun #define TRANS_MASK_HIGH {0x000, 0x800, 0x400, 0xC00, 0x200, 0xA00, 0x600, 0xE00, \
1124*4882a593Smuzhiyun 			 0x100, 0x900, 0x500, 0xD00, 0x300, 0xB00, 0x700, 0xF00}
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun #if defined(__LITTLE_ENDIAN)
1127*4882a593Smuzhiyun static const u16 transl_l[] = TRANS_MASK_LOW;
1128*4882a593Smuzhiyun static const u16 transl_h[] = TRANS_MASK_HIGH;
1129*4882a593Smuzhiyun #elif defined(__BIG_ENDIAN)
1130*4882a593Smuzhiyun static const u16 transl_l[] = TRANS_MASK_HIGH;
1131*4882a593Smuzhiyun static const u16 transl_h[] = TRANS_MASK_LOW;
1132*4882a593Smuzhiyun #else
1133*4882a593Smuzhiyun #error "Only __BIG_ENDIAN and __LITTLE_ENDIAN are supported in vga-planes"
1134*4882a593Smuzhiyun #endif
1135*4882a593Smuzhiyun 
vga_8planes_imageblit(struct fb_info * info,const struct fb_image * image)1136*4882a593Smuzhiyun static void vga_8planes_imageblit(struct fb_info *info, const struct fb_image *image)
1137*4882a593Smuzhiyun {
1138*4882a593Smuzhiyun         char oldindex = getindex();
1139*4882a593Smuzhiyun         char oldmode = setmode(0x40);
1140*4882a593Smuzhiyun         char oldop = setop(0);
1141*4882a593Smuzhiyun         char oldsr = setsr(0);
1142*4882a593Smuzhiyun         char oldmask = selectmask();
1143*4882a593Smuzhiyun 	const unsigned char *cdat = image->data;
1144*4882a593Smuzhiyun 	u32 dx = image->dx;
1145*4882a593Smuzhiyun         char __iomem *where;
1146*4882a593Smuzhiyun         int y;
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun         dx /= 4;
1149*4882a593Smuzhiyun         where = info->screen_base + dx + image->dy * info->fix.line_length;
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun         setmask(0xff);
1152*4882a593Smuzhiyun         writeb(image->bg_color, where);
1153*4882a593Smuzhiyun         readb(where);
1154*4882a593Smuzhiyun         selectmask();
1155*4882a593Smuzhiyun         setmask(image->fg_color ^ image->bg_color);
1156*4882a593Smuzhiyun         setmode(0x42);
1157*4882a593Smuzhiyun         setop(0x18);
1158*4882a593Smuzhiyun         for (y = 0; y < image->height; y++, where += info->fix.line_length)
1159*4882a593Smuzhiyun                 writew(transl_h[cdat[y]&0xF] | transl_l[cdat[y] >> 4], where);
1160*4882a593Smuzhiyun         setmask(oldmask);
1161*4882a593Smuzhiyun         setsr(oldsr);
1162*4882a593Smuzhiyun         setop(oldop);
1163*4882a593Smuzhiyun         setmode(oldmode);
1164*4882a593Smuzhiyun         setindex(oldindex);
1165*4882a593Smuzhiyun }
1166*4882a593Smuzhiyun 
vga_imageblit_expand(struct fb_info * info,const struct fb_image * image)1167*4882a593Smuzhiyun static void vga_imageblit_expand(struct fb_info *info, const struct fb_image *image)
1168*4882a593Smuzhiyun {
1169*4882a593Smuzhiyun 	char __iomem *where = info->screen_base + (image->dx/8) +
1170*4882a593Smuzhiyun 		image->dy * info->fix.line_length;
1171*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
1172*4882a593Smuzhiyun 	char *cdat = (char *) image->data;
1173*4882a593Smuzhiyun 	char __iomem *dst;
1174*4882a593Smuzhiyun 	int x, y;
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	switch (info->fix.type) {
1177*4882a593Smuzhiyun 	case FB_TYPE_VGA_PLANES:
1178*4882a593Smuzhiyun 		if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
1179*4882a593Smuzhiyun 			if (par->isVGA) {
1180*4882a593Smuzhiyun 				setmode(2);
1181*4882a593Smuzhiyun 				setop(0);
1182*4882a593Smuzhiyun 				setsr(0xf);
1183*4882a593Smuzhiyun 				setcolor(image->fg_color);
1184*4882a593Smuzhiyun 				selectmask();
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 				setmask(0xff);
1187*4882a593Smuzhiyun 				writeb(image->bg_color, where);
1188*4882a593Smuzhiyun 				rmb();
1189*4882a593Smuzhiyun 				readb(where); /* fill latches */
1190*4882a593Smuzhiyun 				setmode(3);
1191*4882a593Smuzhiyun 				wmb();
1192*4882a593Smuzhiyun 				for (y = 0; y < image->height; y++) {
1193*4882a593Smuzhiyun 					dst = where;
1194*4882a593Smuzhiyun 					for (x = image->width/8; x--;)
1195*4882a593Smuzhiyun 						writeb(*cdat++, dst++);
1196*4882a593Smuzhiyun 					where += info->fix.line_length;
1197*4882a593Smuzhiyun 				}
1198*4882a593Smuzhiyun 				wmb();
1199*4882a593Smuzhiyun 			} else {
1200*4882a593Smuzhiyun 				setmode(0);
1201*4882a593Smuzhiyun 				setop(0);
1202*4882a593Smuzhiyun 				setsr(0xf);
1203*4882a593Smuzhiyun 				setcolor(image->bg_color);
1204*4882a593Smuzhiyun 				selectmask();
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 				setmask(0xff);
1207*4882a593Smuzhiyun 				for (y = 0; y < image->height; y++) {
1208*4882a593Smuzhiyun 					dst = where;
1209*4882a593Smuzhiyun 					for (x=image->width/8; x--;){
1210*4882a593Smuzhiyun 						rmw(dst);
1211*4882a593Smuzhiyun 						setcolor(image->fg_color);
1212*4882a593Smuzhiyun 						selectmask();
1213*4882a593Smuzhiyun 						if (*cdat) {
1214*4882a593Smuzhiyun 							setmask(*cdat++);
1215*4882a593Smuzhiyun 							rmw(dst++);
1216*4882a593Smuzhiyun 						}
1217*4882a593Smuzhiyun 					}
1218*4882a593Smuzhiyun 					where += info->fix.line_length;
1219*4882a593Smuzhiyun 				}
1220*4882a593Smuzhiyun 			}
1221*4882a593Smuzhiyun 		} else
1222*4882a593Smuzhiyun 			vga_8planes_imageblit(info, image);
1223*4882a593Smuzhiyun 		break;
1224*4882a593Smuzhiyun 	case FB_TYPE_PACKED_PIXELS:
1225*4882a593Smuzhiyun 	default:
1226*4882a593Smuzhiyun 		cfb_imageblit(info, image);
1227*4882a593Smuzhiyun 		break;
1228*4882a593Smuzhiyun 	}
1229*4882a593Smuzhiyun }
1230*4882a593Smuzhiyun 
vga_imageblit_color(struct fb_info * info,const struct fb_image * image)1231*4882a593Smuzhiyun static void vga_imageblit_color(struct fb_info *info, const struct fb_image *image)
1232*4882a593Smuzhiyun {
1233*4882a593Smuzhiyun 	/*
1234*4882a593Smuzhiyun 	 * Draw logo
1235*4882a593Smuzhiyun 	 */
1236*4882a593Smuzhiyun 	struct vga16fb_par *par = info->par;
1237*4882a593Smuzhiyun 	char __iomem *where =
1238*4882a593Smuzhiyun 		info->screen_base + image->dy * info->fix.line_length +
1239*4882a593Smuzhiyun 		image->dx/8;
1240*4882a593Smuzhiyun 	const char *cdat = image->data;
1241*4882a593Smuzhiyun 	char __iomem *dst;
1242*4882a593Smuzhiyun 	int x, y;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 	switch (info->fix.type) {
1245*4882a593Smuzhiyun 	case FB_TYPE_VGA_PLANES:
1246*4882a593Smuzhiyun 		if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4 &&
1247*4882a593Smuzhiyun 		    par->isVGA) {
1248*4882a593Smuzhiyun 			setsr(0xf);
1249*4882a593Smuzhiyun 			setop(0);
1250*4882a593Smuzhiyun 			setmode(0);
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 			for (y = 0; y < image->height; y++) {
1253*4882a593Smuzhiyun 				for (x = 0; x < image->width; x++) {
1254*4882a593Smuzhiyun 					dst = where + x/8;
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 					setcolor(*cdat);
1257*4882a593Smuzhiyun 					selectmask();
1258*4882a593Smuzhiyun 					setmask(1 << (7 - (x % 8)));
1259*4882a593Smuzhiyun 					fb_readb(dst);
1260*4882a593Smuzhiyun 					fb_writeb(0, dst);
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 					cdat++;
1263*4882a593Smuzhiyun 				}
1264*4882a593Smuzhiyun 				where += info->fix.line_length;
1265*4882a593Smuzhiyun 			}
1266*4882a593Smuzhiyun 		}
1267*4882a593Smuzhiyun 		break;
1268*4882a593Smuzhiyun 	case FB_TYPE_PACKED_PIXELS:
1269*4882a593Smuzhiyun 		cfb_imageblit(info, image);
1270*4882a593Smuzhiyun 		break;
1271*4882a593Smuzhiyun 	default:
1272*4882a593Smuzhiyun 		break;
1273*4882a593Smuzhiyun 	}
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun 
vga16fb_imageblit(struct fb_info * info,const struct fb_image * image)1276*4882a593Smuzhiyun static void vga16fb_imageblit(struct fb_info *info, const struct fb_image *image)
1277*4882a593Smuzhiyun {
1278*4882a593Smuzhiyun 	if (image->depth == 1)
1279*4882a593Smuzhiyun 		vga_imageblit_expand(info, image);
1280*4882a593Smuzhiyun 	else
1281*4882a593Smuzhiyun 		vga_imageblit_color(info, image);
1282*4882a593Smuzhiyun }
1283*4882a593Smuzhiyun 
vga16fb_destroy(struct fb_info * info)1284*4882a593Smuzhiyun static void vga16fb_destroy(struct fb_info *info)
1285*4882a593Smuzhiyun {
1286*4882a593Smuzhiyun 	iounmap(info->screen_base);
1287*4882a593Smuzhiyun 	fb_dealloc_cmap(&info->cmap);
1288*4882a593Smuzhiyun 	/* XXX unshare VGA regions */
1289*4882a593Smuzhiyun 	framebuffer_release(info);
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun 
1292*4882a593Smuzhiyun static const struct fb_ops vga16fb_ops = {
1293*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
1294*4882a593Smuzhiyun 	.fb_open        = vga16fb_open,
1295*4882a593Smuzhiyun 	.fb_release     = vga16fb_release,
1296*4882a593Smuzhiyun 	.fb_destroy	= vga16fb_destroy,
1297*4882a593Smuzhiyun 	.fb_check_var	= vga16fb_check_var,
1298*4882a593Smuzhiyun 	.fb_set_par	= vga16fb_set_par,
1299*4882a593Smuzhiyun 	.fb_setcolreg 	= vga16fb_setcolreg,
1300*4882a593Smuzhiyun 	.fb_pan_display = vga16fb_pan_display,
1301*4882a593Smuzhiyun 	.fb_blank 	= vga16fb_blank,
1302*4882a593Smuzhiyun 	.fb_fillrect	= vga16fb_fillrect,
1303*4882a593Smuzhiyun 	.fb_copyarea	= vga16fb_copyarea,
1304*4882a593Smuzhiyun 	.fb_imageblit	= vga16fb_imageblit,
1305*4882a593Smuzhiyun };
1306*4882a593Smuzhiyun 
1307*4882a593Smuzhiyun #ifndef MODULE
vga16fb_setup(char * options)1308*4882a593Smuzhiyun static int __init vga16fb_setup(char *options)
1309*4882a593Smuzhiyun {
1310*4882a593Smuzhiyun 	char *this_opt;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	if (!options || !*options)
1313*4882a593Smuzhiyun 		return 0;
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	while ((this_opt = strsep(&options, ",")) != NULL) {
1316*4882a593Smuzhiyun 		if (!*this_opt) continue;
1317*4882a593Smuzhiyun 	}
1318*4882a593Smuzhiyun 	return 0;
1319*4882a593Smuzhiyun }
1320*4882a593Smuzhiyun #endif
1321*4882a593Smuzhiyun 
vga16fb_probe(struct platform_device * dev)1322*4882a593Smuzhiyun static int vga16fb_probe(struct platform_device *dev)
1323*4882a593Smuzhiyun {
1324*4882a593Smuzhiyun 	struct fb_info *info;
1325*4882a593Smuzhiyun 	struct vga16fb_par *par;
1326*4882a593Smuzhiyun 	int i;
1327*4882a593Smuzhiyun 	int ret = 0;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	printk(KERN_DEBUG "vga16fb: initializing\n");
1330*4882a593Smuzhiyun 	info = framebuffer_alloc(sizeof(struct vga16fb_par), &dev->dev);
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	if (!info) {
1333*4882a593Smuzhiyun 		ret = -ENOMEM;
1334*4882a593Smuzhiyun 		goto err_fb_alloc;
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun 	info->apertures = alloc_apertures(1);
1337*4882a593Smuzhiyun 	if (!info->apertures) {
1338*4882a593Smuzhiyun 		ret = -ENOMEM;
1339*4882a593Smuzhiyun 		goto err_ioremap;
1340*4882a593Smuzhiyun 	}
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	/* XXX share VGA_FB_PHYS and I/O region with vgacon and others */
1343*4882a593Smuzhiyun 	info->screen_base = (void __iomem *)VGA_MAP_MEM(VGA_FB_PHYS, 0);
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	if (!info->screen_base) {
1346*4882a593Smuzhiyun 		printk(KERN_ERR "vga16fb: unable to map device\n");
1347*4882a593Smuzhiyun 		ret = -ENOMEM;
1348*4882a593Smuzhiyun 		goto err_ioremap;
1349*4882a593Smuzhiyun 	}
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	printk(KERN_INFO "vga16fb: mapped to 0x%p\n", info->screen_base);
1352*4882a593Smuzhiyun 	par = info->par;
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 	par->isVGA = screen_info.orig_video_isVGA;
1355*4882a593Smuzhiyun 	par->palette_blanked = 0;
1356*4882a593Smuzhiyun 	par->vesa_blanked = 0;
1357*4882a593Smuzhiyun 
1358*4882a593Smuzhiyun 	i = par->isVGA? 6 : 2;
1359*4882a593Smuzhiyun 
1360*4882a593Smuzhiyun 	vga16fb_defined.red.length   = i;
1361*4882a593Smuzhiyun 	vga16fb_defined.green.length = i;
1362*4882a593Smuzhiyun 	vga16fb_defined.blue.length  = i;
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 	/* name should not depend on EGA/VGA */
1365*4882a593Smuzhiyun 	info->fbops = &vga16fb_ops;
1366*4882a593Smuzhiyun 	info->var = vga16fb_defined;
1367*4882a593Smuzhiyun 	info->fix = vga16fb_fix;
1368*4882a593Smuzhiyun 	/* supports rectangles with widths of multiples of 8 */
1369*4882a593Smuzhiyun 	info->pixmap.blit_x = 1 << 7 | 1 << 15 | 1 << 23 | 1 << 31;
1370*4882a593Smuzhiyun 	info->flags = FBINFO_FLAG_DEFAULT | FBINFO_MISC_FIRMWARE |
1371*4882a593Smuzhiyun 		FBINFO_HWACCEL_YPAN;
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 	i = (info->var.bits_per_pixel == 8) ? 256 : 16;
1374*4882a593Smuzhiyun 	ret = fb_alloc_cmap(&info->cmap, i, 0);
1375*4882a593Smuzhiyun 	if (ret) {
1376*4882a593Smuzhiyun 		printk(KERN_ERR "vga16fb: unable to allocate colormap\n");
1377*4882a593Smuzhiyun 		ret = -ENOMEM;
1378*4882a593Smuzhiyun 		goto err_alloc_cmap;
1379*4882a593Smuzhiyun 	}
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	if (vga16fb_check_var(&info->var, info)) {
1382*4882a593Smuzhiyun 		printk(KERN_ERR "vga16fb: unable to validate variable\n");
1383*4882a593Smuzhiyun 		ret = -EINVAL;
1384*4882a593Smuzhiyun 		goto err_check_var;
1385*4882a593Smuzhiyun 	}
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun 	vga16fb_update_fix(info);
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun 	info->apertures->ranges[0].base = VGA_FB_PHYS;
1390*4882a593Smuzhiyun 	info->apertures->ranges[0].size = VGA_FB_PHYS_LEN;
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	if (register_framebuffer(info) < 0) {
1393*4882a593Smuzhiyun 		printk(KERN_ERR "vga16fb: unable to register framebuffer\n");
1394*4882a593Smuzhiyun 		ret = -EINVAL;
1395*4882a593Smuzhiyun 		goto err_check_var;
1396*4882a593Smuzhiyun 	}
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	fb_info(info, "%s frame buffer device\n", info->fix.id);
1399*4882a593Smuzhiyun 	platform_set_drvdata(dev, info);
1400*4882a593Smuzhiyun 
1401*4882a593Smuzhiyun 	return 0;
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun  err_check_var:
1404*4882a593Smuzhiyun 	fb_dealloc_cmap(&info->cmap);
1405*4882a593Smuzhiyun  err_alloc_cmap:
1406*4882a593Smuzhiyun 	iounmap(info->screen_base);
1407*4882a593Smuzhiyun  err_ioremap:
1408*4882a593Smuzhiyun 	framebuffer_release(info);
1409*4882a593Smuzhiyun  err_fb_alloc:
1410*4882a593Smuzhiyun 	return ret;
1411*4882a593Smuzhiyun }
1412*4882a593Smuzhiyun 
vga16fb_remove(struct platform_device * dev)1413*4882a593Smuzhiyun static int vga16fb_remove(struct platform_device *dev)
1414*4882a593Smuzhiyun {
1415*4882a593Smuzhiyun 	struct fb_info *info = platform_get_drvdata(dev);
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun 	if (info)
1418*4882a593Smuzhiyun 		unregister_framebuffer(info);
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 	return 0;
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun static struct platform_driver vga16fb_driver = {
1424*4882a593Smuzhiyun 	.probe = vga16fb_probe,
1425*4882a593Smuzhiyun 	.remove = vga16fb_remove,
1426*4882a593Smuzhiyun 	.driver = {
1427*4882a593Smuzhiyun 		.name = "vga16fb",
1428*4882a593Smuzhiyun 	},
1429*4882a593Smuzhiyun };
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun static struct platform_device *vga16fb_device;
1432*4882a593Smuzhiyun 
vga16fb_init(void)1433*4882a593Smuzhiyun static int __init vga16fb_init(void)
1434*4882a593Smuzhiyun {
1435*4882a593Smuzhiyun 	int ret;
1436*4882a593Smuzhiyun #ifndef MODULE
1437*4882a593Smuzhiyun 	char *option = NULL;
1438*4882a593Smuzhiyun 
1439*4882a593Smuzhiyun 	if (fb_get_options("vga16fb", &option))
1440*4882a593Smuzhiyun 		return -ENODEV;
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun 	vga16fb_setup(option);
1443*4882a593Smuzhiyun #endif
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	ret = check_mode_supported();
1446*4882a593Smuzhiyun 	if (ret)
1447*4882a593Smuzhiyun 		return ret;
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun 	ret = platform_driver_register(&vga16fb_driver);
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun 	if (!ret) {
1452*4882a593Smuzhiyun 		vga16fb_device = platform_device_alloc("vga16fb", 0);
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 		if (vga16fb_device)
1455*4882a593Smuzhiyun 			ret = platform_device_add(vga16fb_device);
1456*4882a593Smuzhiyun 		else
1457*4882a593Smuzhiyun 			ret = -ENOMEM;
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 		if (ret) {
1460*4882a593Smuzhiyun 			platform_device_put(vga16fb_device);
1461*4882a593Smuzhiyun 			platform_driver_unregister(&vga16fb_driver);
1462*4882a593Smuzhiyun 		}
1463*4882a593Smuzhiyun 	}
1464*4882a593Smuzhiyun 
1465*4882a593Smuzhiyun 	return ret;
1466*4882a593Smuzhiyun }
1467*4882a593Smuzhiyun 
vga16fb_exit(void)1468*4882a593Smuzhiyun static void __exit vga16fb_exit(void)
1469*4882a593Smuzhiyun {
1470*4882a593Smuzhiyun 	platform_device_unregister(vga16fb_device);
1471*4882a593Smuzhiyun 	platform_driver_unregister(&vga16fb_driver);
1472*4882a593Smuzhiyun }
1473*4882a593Smuzhiyun 
1474*4882a593Smuzhiyun MODULE_DESCRIPTION("Legacy VGA framebuffer device driver");
1475*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1476*4882a593Smuzhiyun module_init(vga16fb_init);
1477*4882a593Smuzhiyun module_exit(vga16fb_exit);
1478*4882a593Smuzhiyun 
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun /*
1481*4882a593Smuzhiyun  * Overrides for Emacs so that we follow Linus's tabbing style.
1482*4882a593Smuzhiyun  * ---------------------------------------------------------------------------
1483*4882a593Smuzhiyun  * Local variables:
1484*4882a593Smuzhiyun  * c-basic-offset: 8
1485*4882a593Smuzhiyun  * End:
1486*4882a593Smuzhiyun  */
1487*4882a593Smuzhiyun 
1488