2 * linux/drivers/video/vga16.c -- VGA 16-color framebuffer driver
5 * Based on VGA info at http://www.goodnet.com/~tinara/FreeVGA/home.htm
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file COPYING in the main directory of this
10 * archive for more details.
13 #include <linux/aperture.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/string.h>
19 #include <linux/delay.h>
21 #include <linux/ioport.h>
22 #include <linux/init.h>
23 #include <linux/platform_device.h>
24 #include <linux/screen_info.h>
27 #include <video/vga.h>
34 /* --------------------------------------------------------------------- */
41 /* structure holding original VGA register settings when the
44 unsigned char SeqCtrlIndex; /* Sequencer Index reg. */
45 unsigned char CrtCtrlIndex; /* CRT-Contr. Index reg. */
46 unsigned char CrtMiscIO; /* Miscellaneous register */
47 unsigned char HorizontalTotal; /* CRT-Controller:00h */
48 unsigned char HorizDisplayEnd; /* CRT-Controller:01h */
49 unsigned char StartHorizRetrace;/* CRT-Controller:04h */
50 unsigned char EndHorizRetrace; /* CRT-Controller:05h */
51 unsigned char Overflow; /* CRT-Controller:07h */
52 unsigned char StartVertRetrace; /* CRT-Controller:10h */
53 unsigned char EndVertRetrace; /* CRT-Controller:11h */
54 unsigned char ModeControl; /* CRT-Controller:17h */
55 unsigned char ClockingMode; /* Seq-Controller:01h */
57 struct vgastate state;
58 unsigned int ref_count;
59 int palette_blanked, vesa_blanked, mode, isVGA;
60 u8 misc, pel_msk, vss, clkdiv;
64 /* --------------------------------------------------------------------- */
66 static struct fb_var_screeninfo vga16fb_defined = {
72 .activate = FB_ACTIVATE_TEST,
82 .vmode = FB_VMODE_NONINTERLACED,
85 /* name should not depend on EGA/VGA */
86 static const struct fb_fix_screeninfo vga16fb_fix = {
88 .smem_start = VGA_FB_PHYS_BASE,
89 .smem_len = VGA_FB_PHYS_SIZE,
90 .type = FB_TYPE_VGA_PLANES,
91 .type_aux = FB_AUX_VGA_PLANES_VGA4,
92 .visual = FB_VISUAL_PSEUDOCOLOR,
95 .line_length = 640 / 8,
96 .accel = FB_ACCEL_NONE
99 /* The VGA's weird architecture often requires that we read a byte and
100 write a byte to the same location. It doesn't matter *what* byte
101 we write, however. This is because all the action goes on behind
102 the scenes in the VGA's 32-bit latch register, and reading and writing
103 video memory just invokes latch behavior.
105 To avoid race conditions (is this necessary?), reading and writing
106 the memory byte should be done with a single instruction. One
107 suitable instruction is the x86 bitwise OR. The following
108 read-modify-write routine should optimize to one such bitwise
110 static inline void rmw(volatile char __iomem *p)
116 /* Set the Graphics Mode Register, and return its previous value.
117 Bits 0-1 are write mode, bit 3 is read mode. */
118 static inline int setmode(int mode)
122 oldmode = vga_io_rgfx(VGA_GFX_MODE);
123 vga_io_w(VGA_GFX_D, mode);
127 /* Select the Bit Mask Register and return its value. */
128 static inline int selectmask(void)
130 return vga_io_rgfx(VGA_GFX_BIT_MASK);
133 /* Set the value of the Bit Mask Register. It must already have been
134 selected with selectmask(). */
135 static inline void setmask(int mask)
137 vga_io_w(VGA_GFX_D, mask);
140 /* Set the Data Rotate Register and return its old value.
141 Bits 0-2 are rotate count, bits 3-4 are logical operation
142 (0=NOP, 1=AND, 2=OR, 3=XOR). */
143 static inline int setop(int op)
147 oldop = vga_io_rgfx(VGA_GFX_DATA_ROTATE);
148 vga_io_w(VGA_GFX_D, op);
152 /* Set the Enable Set/Reset Register and return its old value.
153 The code here always uses value 0xf for this register. */
154 static inline int setsr(int sr)
158 oldsr = vga_io_rgfx(VGA_GFX_SR_ENABLE);
159 vga_io_w(VGA_GFX_D, sr);
163 /* Set the Set/Reset Register and return its old value. */
164 static inline int setcolor(int color)
168 oldcolor = vga_io_rgfx(VGA_GFX_SR_VALUE);
169 vga_io_w(VGA_GFX_D, color);
173 /* Return the value in the Graphics Address Register. */
174 static inline int getindex(void)
176 return vga_io_r(VGA_GFX_I);
179 /* Set the value in the Graphics Address Register. */
180 static inline void setindex(int index)
182 vga_io_w(VGA_GFX_I, index);
185 /* Check if the video mode is supported by the driver */
186 static inline int check_mode_supported(const struct screen_info *si)
188 /* non-x86 architectures treat orig_video_isVGA as a boolean flag */
189 #if defined(CONFIG_X86)
190 /* only EGA and VGA in 16 color graphic mode are supported */
191 if (si->orig_video_isVGA != VIDEO_TYPE_EGAC &&
192 si->orig_video_isVGA != VIDEO_TYPE_VGAC)
195 if (si->orig_video_mode != 0x0D && /* 320x200/4 (EGA) */
196 si->orig_video_mode != 0x0E && /* 640x200/4 (EGA) */
197 si->orig_video_mode != 0x10 && /* 640x350/4 (EGA) */
198 si->orig_video_mode != 0x12) /* 640x480/4 (VGA) */
204 static void vga16fb_pan_var(struct fb_info *info,
205 struct fb_var_screeninfo *var)
207 struct vga16fb_par *par = info->par;
210 xoffset = var->xoffset;
211 if (info->var.bits_per_pixel == 8) {
212 pos = (info->var.xres_virtual * var->yoffset + xoffset) >> 2;
213 } else if (par->mode & MODE_TEXT) {
214 int fh = 16; // FIXME !!! font height. Fugde for now.
215 pos = (info->var.xres_virtual * (var->yoffset / fh) + xoffset) >> 3;
217 if (info->var.nonstd)
219 pos = (info->var.xres_virtual * var->yoffset + xoffset) >> 3;
221 vga_io_wcrt(VGA_CRTC_START_HI, pos >> 8);
222 vga_io_wcrt(VGA_CRTC_START_LO, pos & 0xFF);
223 /* if we support CFB4, then we must! support xoffset with pixel
224 * granularity if someone supports xoffset in bit resolution */
225 vga_io_r(VGA_IS1_RC); /* reset flip-flop */
226 vga_io_w(VGA_ATT_IW, VGA_ATC_PEL);
227 if (info->var.bits_per_pixel == 8)
228 vga_io_w(VGA_ATT_IW, (xoffset & 3) << 1);
230 vga_io_w(VGA_ATT_IW, xoffset & 7);
231 vga_io_r(VGA_IS1_RC);
232 vga_io_w(VGA_ATT_IW, 0x20);
235 static void vga16fb_update_fix(struct fb_info *info)
237 if (info->var.bits_per_pixel == 4) {
238 if (info->var.nonstd) {
239 info->fix.type = FB_TYPE_PACKED_PIXELS;
240 info->fix.line_length = info->var.xres_virtual / 2;
242 info->fix.type = FB_TYPE_VGA_PLANES;
243 info->fix.type_aux = FB_AUX_VGA_PLANES_VGA4;
244 info->fix.line_length = info->var.xres_virtual / 8;
246 } else if (info->var.bits_per_pixel == 0) {
247 info->fix.type = FB_TYPE_TEXT;
248 info->fix.type_aux = FB_AUX_TEXT_CGA;
249 info->fix.line_length = info->var.xres_virtual / 4;
251 if (info->var.nonstd) {
252 info->fix.type = FB_TYPE_VGA_PLANES;
253 info->fix.type_aux = FB_AUX_VGA_PLANES_CFB8;
254 info->fix.line_length = info->var.xres_virtual / 4;
256 info->fix.type = FB_TYPE_PACKED_PIXELS;
257 info->fix.line_length = info->var.xres_virtual;
262 static void vga16fb_clock_chip(struct vga16fb_par *par,
263 unsigned int *pixclock,
264 const struct fb_info *info,
267 static const struct {
271 } *ptr, *best, vgaclocks[] = {
272 { 79442 /* 12.587 */, 0x00, 0x08},
273 { 70616 /* 14.161 */, 0x04, 0x08},
274 { 39721 /* 25.175 */, 0x00, 0x00},
275 { 35308 /* 28.322 */, 0x04, 0x00},
276 { 0 /* bad */, 0x00, 0x00}};
279 *pixclock = (*pixclock * mul) / div;
281 err = *pixclock - best->pixclock;
282 if (err < 0) err = -err;
283 for (ptr = vgaclocks + 1; ptr->pixclock; ptr++) {
286 tmp = *pixclock - ptr->pixclock;
287 if (tmp < 0) tmp = -tmp;
293 par->misc |= best->misc;
294 par->clkdiv = best->seq_clock_mode;
295 *pixclock = (best->pixclock * div) / mul;
298 #define FAIL(X) return -EINVAL
300 static int vga16fb_open(struct fb_info *info, int user)
302 struct vga16fb_par *par = info->par;
304 if (!par->ref_count) {
305 memset(&par->state, 0, sizeof(struct vgastate));
306 par->state.flags = VGA_SAVE_FONTS | VGA_SAVE_MODE |
308 save_vga(&par->state);
315 static int vga16fb_release(struct fb_info *info, int user)
317 struct vga16fb_par *par = info->par;
322 if (par->ref_count == 1)
323 restore_vga(&par->state);
329 static int vga16fb_check_var(struct fb_var_screeninfo *var,
330 struct fb_info *info)
332 struct vga16fb_par *par = info->par;
333 u32 xres, right, hslen, left, xtotal;
334 u32 yres, lower, vslen, upper, ytotal;
335 u32 vxres, xoffset, vyres, yoffset;
344 if (var->bits_per_pixel == 4) {
349 mode = MODE_SKIP4 | MODE_CFB;
357 } else if (var->bits_per_pixel == 8) {
359 return -EINVAL; /* no support on EGA */
362 mode = MODE_8BPP | MODE_CFB;
365 mode = MODE_SKIP4 | MODE_8BPP | MODE_CFB;
371 xres = (var->xres + 7) & ~7;
372 vxres = (var->xres_virtual + 0xF) & ~0xF;
373 xoffset = (var->xoffset + 7) & ~7;
374 left = (var->left_margin + 7) & ~7;
375 right = (var->right_margin + 7) & ~7;
376 hslen = (var->hsync_len + 7) & ~7;
380 if (xres + xoffset > vxres)
381 xoffset = vxres - xres;
384 var->right_margin = right;
385 var->hsync_len = hslen;
386 var->left_margin = left;
387 var->xres_virtual = vxres;
388 var->xoffset = xoffset;
395 xtotal = xres + right + hslen + left;
397 FAIL("xtotal too big");
399 FAIL("hslen too big");
400 if (right + hslen + left > 64)
401 FAIL("hblank too big");
402 par->crtc[VGA_CRTC_H_TOTAL] = xtotal - 5;
403 par->crtc[VGA_CRTC_H_BLANK_START] = xres - 1;
404 par->crtc[VGA_CRTC_H_DISP] = xres - 1;
406 par->crtc[VGA_CRTC_H_SYNC_START] = pos;
408 par->crtc[VGA_CRTC_H_SYNC_END] = pos & 0x1F;
409 pos += left - 2; /* blank_end + 2 <= total + 5 */
410 par->crtc[VGA_CRTC_H_BLANK_END] = (pos & 0x1F) | 0x80;
412 par->crtc[VGA_CRTC_H_SYNC_END] |= 0x80;
415 lower = var->lower_margin;
416 vslen = var->vsync_len;
417 upper = var->upper_margin;
418 vyres = var->yres_virtual;
419 yoffset = var->yoffset;
423 if (vxres * vyres > maxmem) {
424 vyres = maxmem / vxres;
428 if (yoffset + yres > vyres)
429 yoffset = vyres - yres;
431 var->lower_margin = lower;
432 var->vsync_len = vslen;
433 var->upper_margin = upper;
434 var->yres_virtual = vyres;
435 var->yoffset = yoffset;
437 if (var->vmode & FB_VMODE_DOUBLE) {
443 ytotal = yres + lower + vslen + upper;
454 FAIL("ytotal too big");
456 FAIL("vslen too big");
457 par->crtc[VGA_CRTC_V_TOTAL] = ytotal - 2;
458 r7 = 0x10; /* disable linecompare */
459 if (ytotal & 0x100) r7 |= 0x01;
460 if (ytotal & 0x200) r7 |= 0x20;
461 par->crtc[VGA_CRTC_PRESET_ROW] = 0;
462 par->crtc[VGA_CRTC_MAX_SCAN] = 0x40; /* 1 scanline, no linecmp */
463 if (var->vmode & FB_VMODE_DOUBLE)
464 par->crtc[VGA_CRTC_MAX_SCAN] |= 0x80;
465 par->crtc[VGA_CRTC_CURSOR_START] = 0x20;
466 par->crtc[VGA_CRTC_CURSOR_END] = 0x00;
467 if ((mode & (MODE_CFB | MODE_8BPP)) == MODE_CFB)
469 pos = yoffset * vxres + (xoffset >> shift);
470 par->crtc[VGA_CRTC_START_HI] = pos >> 8;
471 par->crtc[VGA_CRTC_START_LO] = pos & 0xFF;
472 par->crtc[VGA_CRTC_CURSOR_HI] = 0x00;
473 par->crtc[VGA_CRTC_CURSOR_LO] = 0x00;
475 par->crtc[VGA_CRTC_V_DISP_END] = pos & 0xFF;
476 par->crtc[VGA_CRTC_V_BLANK_START] = pos & 0xFF;
478 r7 |= 0x0A; /* 0x02 -> DISP_END, 0x08 -> BLANK_START */
480 r7 |= 0x40; /* 0x40 -> DISP_END */
481 par->crtc[VGA_CRTC_MAX_SCAN] |= 0x20; /* BLANK_START */
484 par->crtc[VGA_CRTC_V_SYNC_START] = pos & 0xFF;
490 par->crtc[VGA_CRTC_V_SYNC_END] = (pos & 0x0F) & ~0x10; /* disabled IRQ */
491 pos += upper - 1; /* blank_end + 1 <= ytotal + 2 */
492 par->crtc[VGA_CRTC_V_BLANK_END] = pos & 0xFF; /* 0x7F for original VGA,
493 but some SVGA chips requires all 8 bits to set */
495 FAIL("vxres too long");
496 par->crtc[VGA_CRTC_OFFSET] = vxres >> 1;
497 if (mode & MODE_SKIP4)
498 par->crtc[VGA_CRTC_UNDERLINE] = 0x5F; /* 256, cfb8 */
500 par->crtc[VGA_CRTC_UNDERLINE] = 0x1F; /* 16, vgap */
501 par->crtc[VGA_CRTC_MODE] = rMode | ((mode & MODE_TEXT) ? 0xA3 : 0xE3);
502 par->crtc[VGA_CRTC_LINE_COMPARE] = 0xFF;
503 par->crtc[VGA_CRTC_OVERFLOW] = r7;
505 par->vss = 0x00; /* 3DA */
507 par->misc = 0xE3; /* enable CPU, ports 0x3Dx, positive sync */
508 if (var->sync & FB_SYNC_HOR_HIGH_ACT)
510 if (var->sync & FB_SYNC_VERT_HIGH_ACT)
515 if (mode & MODE_8BPP)
516 /* pixel clock == vga clock / 2 */
517 vga16fb_clock_chip(par, &var->pixclock, info, 1, 2);
519 /* pixel clock == vga clock */
520 vga16fb_clock_chip(par, &var->pixclock, info, 1, 1);
522 var->red.offset = var->green.offset = var->blue.offset =
523 var->transp.offset = 0;
524 var->red.length = var->green.length = var->blue.length =
525 (par->isVGA) ? 6 : 2;
526 var->transp.length = 0;
527 var->activate = FB_ACTIVATE_NOW;
530 var->accel_flags = 0;
535 static int vga16fb_set_par(struct fb_info *info)
537 struct vga16fb_par *par = info->par;
543 seq[VGA_SEQ_CLOCK_MODE] = 0x01 | par->clkdiv;
544 if (par->mode & MODE_TEXT)
545 seq[VGA_SEQ_PLANE_WRITE] = 0x03;
547 seq[VGA_SEQ_PLANE_WRITE] = 0x0F;
548 seq[VGA_SEQ_CHARACTER_MAP] = 0x00;
549 if (par->mode & MODE_TEXT)
550 seq[VGA_SEQ_MEMORY_MODE] = 0x03;
551 else if (par->mode & MODE_SKIP4)
552 seq[VGA_SEQ_MEMORY_MODE] = 0x0E;
554 seq[VGA_SEQ_MEMORY_MODE] = 0x06;
556 gdc[VGA_GFX_SR_VALUE] = 0x00;
557 gdc[VGA_GFX_SR_ENABLE] = 0x00;
558 gdc[VGA_GFX_COMPARE_VALUE] = 0x00;
559 gdc[VGA_GFX_DATA_ROTATE] = 0x00;
560 gdc[VGA_GFX_PLANE_READ] = 0;
561 if (par->mode & MODE_TEXT) {
562 gdc[VGA_GFX_MODE] = 0x10;
563 gdc[VGA_GFX_MISC] = 0x06;
565 if (par->mode & MODE_CFB)
566 gdc[VGA_GFX_MODE] = 0x40;
568 gdc[VGA_GFX_MODE] = 0x00;
569 gdc[VGA_GFX_MISC] = 0x05;
571 gdc[VGA_GFX_COMPARE_MASK] = 0x0F;
572 gdc[VGA_GFX_BIT_MASK] = 0xFF;
574 for (i = 0x00; i < 0x10; i++)
576 if (par->mode & MODE_TEXT)
577 atc[VGA_ATC_MODE] = 0x04;
578 else if (par->mode & MODE_8BPP)
579 atc[VGA_ATC_MODE] = 0x41;
581 atc[VGA_ATC_MODE] = 0x81;
582 atc[VGA_ATC_OVERSCAN] = 0x00; /* 0 for EGA, 0xFF for VGA */
583 atc[VGA_ATC_PLANE_ENABLE] = 0x0F;
584 if (par->mode & MODE_8BPP)
585 atc[VGA_ATC_PEL] = (info->var.xoffset & 3) << 1;
587 atc[VGA_ATC_PEL] = info->var.xoffset & 7;
588 atc[VGA_ATC_COLOR_PAGE] = 0x00;
590 if (par->mode & MODE_TEXT) {
591 fh = 16; // FIXME !!! Fudge font height.
592 par->crtc[VGA_CRTC_MAX_SCAN] = (par->crtc[VGA_CRTC_MAX_SCAN]
596 vga_io_w(VGA_MIS_W, vga_io_r(VGA_MIS_R) | 0x01);
598 /* Enable graphics register modification */
600 vga_io_w(EGA_GFX_E0, 0x00);
601 vga_io_w(EGA_GFX_E1, 0x01);
604 /* update misc output register */
605 vga_io_w(VGA_MIS_W, par->misc);
607 /* synchronous reset on */
608 vga_io_wseq(0x00, 0x01);
611 vga_io_w(VGA_PEL_MSK, par->pel_msk);
613 /* write sequencer registers */
614 vga_io_wseq(VGA_SEQ_CLOCK_MODE, seq[VGA_SEQ_CLOCK_MODE] | 0x20);
615 for (i = 2; i < VGA_SEQ_C; i++) {
616 vga_io_wseq(i, seq[i]);
619 /* synchronous reset off */
620 vga_io_wseq(0x00, 0x03);
622 /* deprotect CRT registers 0-7 */
623 vga_io_wcrt(VGA_CRTC_V_SYNC_END, par->crtc[VGA_CRTC_V_SYNC_END]);
625 /* write CRT registers */
626 for (i = 0; i < VGA_CRTC_REGS; i++) {
627 vga_io_wcrt(i, par->crtc[i]);
630 /* write graphics controller registers */
631 for (i = 0; i < VGA_GFX_C; i++) {
632 vga_io_wgfx(i, gdc[i]);
635 /* write attribute controller registers */
636 for (i = 0; i < VGA_ATT_C; i++) {
637 vga_io_r(VGA_IS1_RC); /* reset flip-flop */
638 vga_io_wattr(i, atc[i]);
641 /* Wait for screen to stabilize. */
644 vga_io_wseq(VGA_SEQ_CLOCK_MODE, seq[VGA_SEQ_CLOCK_MODE]);
646 vga_io_r(VGA_IS1_RC);
647 vga_io_w(VGA_ATT_IW, 0x20);
649 vga16fb_update_fix(info);
653 static void ega16_setpalette(int regno, unsigned red, unsigned green, unsigned blue)
655 static const unsigned char map[] = { 000, 001, 010, 011 };
660 val = map[red>>14] | ((map[green>>14]) << 1) | ((map[blue>>14]) << 2);
661 vga_io_r(VGA_IS1_RC); /* ! 0x3BA */
662 vga_io_wattr(regno, val);
663 vga_io_r(VGA_IS1_RC); /* some clones need it */
664 vga_io_w(VGA_ATT_IW, 0x20); /* unblank screen */
667 static void vga16_setpalette(int regno, unsigned red, unsigned green, unsigned blue)
669 outb(regno, VGA_PEL_IW);
670 outb(red >> 10, VGA_PEL_D);
671 outb(green >> 10, VGA_PEL_D);
672 outb(blue >> 10, VGA_PEL_D);
675 static int vga16fb_setcolreg(unsigned regno, unsigned red, unsigned green,
676 unsigned blue, unsigned transp,
677 struct fb_info *info)
679 struct vga16fb_par *par = info->par;
683 * Set a single color register. The values supplied are
684 * already rounded down to the hardware's capabilities
685 * (according to the entries in the `var' structure). Return
686 * != 0 for invalid regno.
692 gray = info->var.grayscale;
695 /* gray = 0.30*R + 0.59*G + 0.11*B */
696 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
699 vga16_setpalette(regno,red,green,blue);
701 ega16_setpalette(regno,red,green,blue);
705 static int vga16fb_pan_display(struct fb_var_screeninfo *var,
706 struct fb_info *info)
708 vga16fb_pan_var(info, var);
712 /* The following VESA blanking code is taken from vgacon.c. The VGA
713 blanking code was originally by Huang shi chao, and modified by
717 static void vga_vesa_blank(struct vga16fb_par *par, int mode)
719 unsigned char SeqCtrlIndex = vga_io_r(VGA_SEQ_I);
720 unsigned char CrtCtrlIndex = vga_io_r(VGA_CRT_IC);
722 /* save original values of VGA controller registers */
723 if(!par->vesa_blanked) {
724 par->vga_state.CrtMiscIO = vga_io_r(VGA_MIS_R);
727 par->vga_state.HorizontalTotal = vga_io_rcrt(0x00); /* HorizontalTotal */
728 par->vga_state.HorizDisplayEnd = vga_io_rcrt(0x01); /* HorizDisplayEnd */
729 par->vga_state.StartHorizRetrace = vga_io_rcrt(0x04); /* StartHorizRetrace */
730 par->vga_state.EndHorizRetrace = vga_io_rcrt(0x05); /* EndHorizRetrace */
731 par->vga_state.Overflow = vga_io_rcrt(0x07); /* Overflow */
732 par->vga_state.StartVertRetrace = vga_io_rcrt(0x10); /* StartVertRetrace */
733 par->vga_state.EndVertRetrace = vga_io_rcrt(0x11); /* EndVertRetrace */
734 par->vga_state.ModeControl = vga_io_rcrt(0x17); /* ModeControl */
735 par->vga_state.ClockingMode = vga_io_rseq(0x01); /* ClockingMode */
738 /* assure that video is enabled */
739 /* "0x20" is VIDEO_ENABLE_bit in register 01 of sequencer */
740 vga_io_wseq(0x01, par->vga_state.ClockingMode | 0x20);
742 /* test for vertical retrace in process.... */
743 if ((par->vga_state.CrtMiscIO & 0x80) == 0x80)
744 vga_io_w(VGA_MIS_W, par->vga_state.CrtMiscIO & 0xef);
747 * Set <End of vertical retrace> to minimum (0) and
748 * <Start of vertical Retrace> to maximum (incl. overflow)
749 * Result: turn off vertical sync (VSync) pulse.
751 if (mode & FB_BLANK_VSYNC_SUSPEND) {
752 vga_io_wcrt(VGA_CRTC_V_SYNC_START, 0xff);
753 vga_io_wcrt(VGA_CRTC_V_SYNC_END, 0x40);
754 /* bits 9,10 of vert. retrace */
755 vga_io_wcrt(VGA_CRTC_OVERFLOW, par->vga_state.Overflow | 0x84);
758 if (mode & FB_BLANK_HSYNC_SUSPEND) {
760 * Set <End of horizontal retrace> to minimum (0) and
761 * <Start of horizontal Retrace> to maximum
762 * Result: turn off horizontal sync (HSync) pulse.
764 vga_io_wcrt(VGA_CRTC_H_SYNC_START, 0xff);
765 vga_io_wcrt(VGA_CRTC_H_SYNC_END, 0x00);
768 /* restore both index registers */
769 outb_p(SeqCtrlIndex, VGA_SEQ_I);
770 outb_p(CrtCtrlIndex, VGA_CRT_IC);
773 static void vga_vesa_unblank(struct vga16fb_par *par)
775 unsigned char SeqCtrlIndex = vga_io_r(VGA_SEQ_I);
776 unsigned char CrtCtrlIndex = vga_io_r(VGA_CRT_IC);
778 /* restore original values of VGA controller registers */
779 vga_io_w(VGA_MIS_W, par->vga_state.CrtMiscIO);
781 /* HorizontalTotal */
782 vga_io_wcrt(0x00, par->vga_state.HorizontalTotal);
783 /* HorizDisplayEnd */
784 vga_io_wcrt(0x01, par->vga_state.HorizDisplayEnd);
785 /* StartHorizRetrace */
786 vga_io_wcrt(0x04, par->vga_state.StartHorizRetrace);
787 /* EndHorizRetrace */
788 vga_io_wcrt(0x05, par->vga_state.EndHorizRetrace);
790 vga_io_wcrt(0x07, par->vga_state.Overflow);
791 /* StartVertRetrace */
792 vga_io_wcrt(0x10, par->vga_state.StartVertRetrace);
794 vga_io_wcrt(0x11, par->vga_state.EndVertRetrace);
796 vga_io_wcrt(0x17, par->vga_state.ModeControl);
798 vga_io_wseq(0x01, par->vga_state.ClockingMode);
800 /* restore index/control registers */
801 vga_io_w(VGA_SEQ_I, SeqCtrlIndex);
802 vga_io_w(VGA_CRT_IC, CrtCtrlIndex);
805 static void vga_pal_blank(void)
809 for (i=0; i<16; i++) {
810 outb_p(i, VGA_PEL_IW);
811 outb_p(0, VGA_PEL_D);
812 outb_p(0, VGA_PEL_D);
813 outb_p(0, VGA_PEL_D);
817 /* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
818 static int vga16fb_blank(int blank, struct fb_info *info)
820 struct vga16fb_par *par = info->par;
823 case FB_BLANK_UNBLANK: /* Unblank */
824 if (par->vesa_blanked) {
825 vga_vesa_unblank(par);
826 par->vesa_blanked = 0;
828 if (par->palette_blanked) {
829 par->palette_blanked = 0;
832 case FB_BLANK_NORMAL: /* blank */
834 par->palette_blanked = 1;
836 default: /* VESA blanking */
837 vga_vesa_blank(par, blank);
838 par->vesa_blanked = 1;
844 static void vga_8planes_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
846 u32 dx = rect->dx, width = rect->width;
847 char oldindex = getindex();
848 char oldmode = setmode(0x40);
849 char oldmask = selectmask();
850 int line_ofs, height;
855 where = info->screen_base + dx + rect->dy * info->fix.line_length;
857 if (rect->rop == ROP_COPY) {
862 line_ofs = info->fix.line_length - width;
865 height = rect->height;
870 /* we can do memset... */
871 for (x = width; x > 0; --x) {
872 writeb(rect->color, where);
878 char oldcolor = setcolor(0xf);
884 for (y = 0; y < rect->height; y++) {
887 where += info->fix.line_length;
898 static void vga16fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
900 int x, x2, y2, vxres, vyres, width, height, line_ofs;
903 vxres = info->var.xres_virtual;
904 vyres = info->var.yres_virtual;
906 if (!rect->width || !rect->height || rect->dx > vxres || rect->dy > vyres)
909 /* We could use hardware clipping but on many cards you get around
910 * hardware clipping by writing to framebuffer directly. */
912 x2 = rect->dx + rect->width;
913 y2 = rect->dy + rect->height;
914 x2 = x2 < vxres ? x2 : vxres;
915 y2 = y2 < vyres ? y2 : vyres;
916 width = x2 - rect->dx;
918 switch (info->fix.type) {
919 case FB_TYPE_VGA_PLANES:
920 if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
922 height = y2 - rect->dy;
923 width = rect->width/8;
925 line_ofs = info->fix.line_length - width;
926 dst = info->screen_base + (rect->dx/8) + rect->dy * info->fix.line_length;
933 setcolor(rect->color);
939 for (x = 0; x < width; x++) {
955 for (x = 0; x < width; x++) {
964 vga_8planes_fillrect(info, rect);
966 case FB_TYPE_PACKED_PIXELS:
968 cfb_fillrect(info, rect);
973 static void vga_8planes_copyarea(struct fb_info *info, const struct fb_copyarea *area)
975 char oldindex = getindex();
976 char oldmode = setmode(0x41);
977 char oldop = setop(0);
978 char oldsr = setsr(0xf);
979 int height, line_ofs, x;
984 height = area->height;
988 width = area->width / 4;
990 if (area->dy < area->sy || (area->dy == area->sy && dx < sx)) {
991 line_ofs = info->fix.line_length - width;
992 dest = info->screen_base + dx + area->dy * info->fix.line_length;
993 src = info->screen_base + sx + area->sy * info->fix.line_length;
995 for (x = 0; x < width; x++) {
1005 line_ofs = info->fix.line_length - width;
1006 dest = info->screen_base + dx + width +
1007 (area->dy + height - 1) * info->fix.line_length;
1008 src = info->screen_base + sx + width +
1009 (area->sy + height - 1) * info->fix.line_length;
1011 for (x = 0; x < width; x++) {
1028 static void vga16fb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
1030 u32 dx = area->dx, dy = area->dy, sx = area->sx, sy = area->sy;
1031 int x, x2, y2, old_dx, old_dy, vxres, vyres;
1032 int height, width, line_ofs;
1033 char __iomem *dst = NULL;
1034 char __iomem *src = NULL;
1036 vxres = info->var.xres_virtual;
1037 vyres = info->var.yres_virtual;
1039 if (area->dx > vxres || area->sx > vxres || area->dy > vyres ||
1043 /* clip the destination */
1048 * We could use hardware clipping but on many cards you get around
1049 * hardware clipping by writing to framebuffer directly.
1051 x2 = area->dx + area->width;
1052 y2 = area->dy + area->height;
1053 dx = area->dx > 0 ? area->dx : 0;
1054 dy = area->dy > 0 ? area->dy : 0;
1055 x2 = x2 < vxres ? x2 : vxres;
1056 y2 = y2 < vyres ? y2 : vyres;
1060 if (sx + dx < old_dx || sy + dy < old_dy)
1063 /* update sx1,sy1 */
1064 sx += (dx - old_dx);
1065 sy += (dy - old_dy);
1067 /* the source must be completely inside the virtual screen */
1068 if (sx + width > vxres || sy + height > vyres)
1071 switch (info->fix.type) {
1072 case FB_TYPE_VGA_PLANES:
1073 if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
1075 line_ofs = info->fix.line_length - width;
1081 if (dy < sy || (dy == sy && dx < sx)) {
1082 dst = info->screen_base + (dx/8) + dy * info->fix.line_length;
1083 src = info->screen_base + (sx/8) + sy * info->fix.line_length;
1085 for (x = 0; x < width; x++) {
1095 dst = info->screen_base + (dx/8) + width +
1096 (dy + height - 1) * info->fix.line_length;
1097 src = info->screen_base + (sx/8) + width +
1098 (sy + height - 1) * info->fix.line_length;
1100 for (x = 0; x < width; x++) {
1111 vga_8planes_copyarea(info, area);
1113 case FB_TYPE_PACKED_PIXELS:
1115 cfb_copyarea(info, area);
1120 #define TRANS_MASK_LOW {0x0,0x8,0x4,0xC,0x2,0xA,0x6,0xE,0x1,0x9,0x5,0xD,0x3,0xB,0x7,0xF}
1121 #define TRANS_MASK_HIGH {0x000, 0x800, 0x400, 0xC00, 0x200, 0xA00, 0x600, 0xE00, \
1122 0x100, 0x900, 0x500, 0xD00, 0x300, 0xB00, 0x700, 0xF00}
1124 #if defined(__LITTLE_ENDIAN)
1125 static const u16 transl_l[] = TRANS_MASK_LOW;
1126 static const u16 transl_h[] = TRANS_MASK_HIGH;
1127 #elif defined(__BIG_ENDIAN)
1128 static const u16 transl_l[] = TRANS_MASK_HIGH;
1129 static const u16 transl_h[] = TRANS_MASK_LOW;
1131 #error "Only __BIG_ENDIAN and __LITTLE_ENDIAN are supported in vga-planes"
1134 static void vga_8planes_imageblit(struct fb_info *info, const struct fb_image *image)
1136 char oldindex = getindex();
1137 char oldmode = setmode(0x40);
1138 char oldop = setop(0);
1139 char oldsr = setsr(0);
1140 char oldmask = selectmask();
1141 const unsigned char *cdat = image->data;
1143 char __iomem *where;
1147 where = info->screen_base + dx + image->dy * info->fix.line_length;
1150 writeb(image->bg_color, where);
1153 setmask(image->fg_color ^ image->bg_color);
1156 for (y = 0; y < image->height; y++, where += info->fix.line_length)
1157 writew(transl_h[cdat[y]&0xF] | transl_l[cdat[y] >> 4], where);
1165 static void vga_imageblit_expand(struct fb_info *info, const struct fb_image *image)
1167 char __iomem *where = info->screen_base + (image->dx/8) +
1168 image->dy * info->fix.line_length;
1169 struct vga16fb_par *par = info->par;
1170 char *cdat = (char *) image->data;
1174 switch (info->fix.type) {
1175 case FB_TYPE_VGA_PLANES:
1176 if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4) {
1181 setcolor(image->fg_color);
1185 writeb(image->bg_color, where);
1187 readb(where); /* fill latches */
1190 for (y = 0; y < image->height; y++) {
1192 for (x = image->width/8; x--;)
1193 writeb(*cdat++, dst++);
1194 where += info->fix.line_length;
1201 setcolor(image->bg_color);
1205 for (y = 0; y < image->height; y++) {
1207 for (x=image->width/8; x--;){
1209 setcolor(image->fg_color);
1216 where += info->fix.line_length;
1220 vga_8planes_imageblit(info, image);
1222 case FB_TYPE_PACKED_PIXELS:
1224 cfb_imageblit(info, image);
1229 static void vga_imageblit_color(struct fb_info *info, const struct fb_image *image)
1234 struct vga16fb_par *par = info->par;
1235 char __iomem *where =
1236 info->screen_base + image->dy * info->fix.line_length +
1238 const char *cdat = image->data;
1242 switch (info->fix.type) {
1243 case FB_TYPE_VGA_PLANES:
1244 if (info->fix.type_aux == FB_AUX_VGA_PLANES_VGA4 &&
1250 for (y = 0; y < image->height; y++) {
1251 for (x = 0; x < image->width; x++) {
1256 setmask(1 << (7 - (x % 8)));
1262 where += info->fix.line_length;
1266 case FB_TYPE_PACKED_PIXELS:
1267 cfb_imageblit(info, image);
1274 static void vga16fb_imageblit(struct fb_info *info, const struct fb_image *image)
1276 if (image->depth == 1)
1277 vga_imageblit_expand(info, image);
1279 vga_imageblit_color(info, image);
1282 static void vga16fb_destroy(struct fb_info *info)
1284 iounmap(info->screen_base);
1285 fb_dealloc_cmap(&info->cmap);
1286 /* XXX unshare VGA regions */
1287 framebuffer_release(info);
1290 static const struct fb_ops vga16fb_ops = {
1291 .owner = THIS_MODULE,
1292 .fb_open = vga16fb_open,
1293 .fb_release = vga16fb_release,
1294 .fb_destroy = vga16fb_destroy,
1295 .fb_check_var = vga16fb_check_var,
1296 .fb_set_par = vga16fb_set_par,
1297 .fb_setcolreg = vga16fb_setcolreg,
1298 .fb_pan_display = vga16fb_pan_display,
1299 .fb_blank = vga16fb_blank,
1300 .fb_fillrect = vga16fb_fillrect,
1301 .fb_copyarea = vga16fb_copyarea,
1302 .fb_imageblit = vga16fb_imageblit,
1305 static int vga16fb_probe(struct platform_device *dev)
1307 struct screen_info *si;
1308 struct fb_info *info;
1309 struct vga16fb_par *par;
1313 si = dev_get_platdata(&dev->dev);
1317 ret = check_mode_supported(si);
1321 printk(KERN_DEBUG "vga16fb: initializing\n");
1322 info = framebuffer_alloc(sizeof(struct vga16fb_par), &dev->dev);
1329 /* XXX share VGA_FB_PHYS_BASE and I/O region with vgacon and others */
1330 info->screen_base = (void __iomem *)VGA_MAP_MEM(VGA_FB_PHYS_BASE, 0);
1332 if (!info->screen_base) {
1333 printk(KERN_ERR "vga16fb: unable to map device\n");
1338 printk(KERN_INFO "vga16fb: mapped to 0x%p\n", info->screen_base);
1341 #if defined(CONFIG_X86)
1342 par->isVGA = si->orig_video_isVGA == VIDEO_TYPE_VGAC;
1344 /* non-x86 architectures treat orig_video_isVGA as a boolean flag */
1345 par->isVGA = si->orig_video_isVGA;
1347 par->palette_blanked = 0;
1348 par->vesa_blanked = 0;
1350 i = par->isVGA? 6 : 2;
1352 vga16fb_defined.red.length = i;
1353 vga16fb_defined.green.length = i;
1354 vga16fb_defined.blue.length = i;
1356 /* name should not depend on EGA/VGA */
1357 info->fbops = &vga16fb_ops;
1358 info->var = vga16fb_defined;
1359 info->fix = vga16fb_fix;
1360 /* supports rectangles with widths of multiples of 8 */
1361 info->pixmap.blit_x = 1 << 7 | 1 << 15 | 1 << 23 | 1 << 31;
1362 info->flags = FBINFO_FLAG_DEFAULT | FBINFO_HWACCEL_YPAN;
1364 i = (info->var.bits_per_pixel == 8) ? 256 : 16;
1365 ret = fb_alloc_cmap(&info->cmap, i, 0);
1367 printk(KERN_ERR "vga16fb: unable to allocate colormap\n");
1369 goto err_alloc_cmap;
1372 if (vga16fb_check_var(&info->var, info)) {
1373 printk(KERN_ERR "vga16fb: unable to validate variable\n");
1378 vga16fb_update_fix(info);
1380 ret = devm_aperture_acquire_for_platform_device(dev, VGA_FB_PHYS_BASE, VGA_FB_PHYS_SIZE);
1383 if (register_framebuffer(info) < 0) {
1384 printk(KERN_ERR "vga16fb: unable to register framebuffer\n");
1389 fb_info(info, "%s frame buffer device\n", info->fix.id);
1390 platform_set_drvdata(dev, info);
1395 fb_dealloc_cmap(&info->cmap);
1397 iounmap(info->screen_base);
1399 framebuffer_release(info);
1404 static void vga16fb_remove(struct platform_device *dev)
1406 struct fb_info *info = platform_get_drvdata(dev);
1409 unregister_framebuffer(info);
1412 static const struct platform_device_id vga16fb_driver_id_table[] = {
1413 {"ega-framebuffer", 0},
1414 {"vga-framebuffer", 0},
1417 MODULE_DEVICE_TABLE(platform, vga16fb_driver_id_table);
1419 static struct platform_driver vga16fb_driver = {
1420 .probe = vga16fb_probe,
1421 .remove_new = vga16fb_remove,
1425 .id_table = vga16fb_driver_id_table,
1428 module_platform_driver(vga16fb_driver);
1430 MODULE_DESCRIPTION("Legacy VGA framebuffer device driver");
1431 MODULE_LICENSE("GPL");