intelfb: add preliminary i2c support
[safe/jmp/linux-2.6] / drivers / video / hgafb.c
1 /*
2  * linux/drivers/video/hgafb.c -- Hercules graphics adaptor frame buffer device
3  * 
4  *      Created 25 Nov 1999 by Ferenc Bakonyi (fero@drama.obuda.kando.hu)
5  *      Based on skeletonfb.c by Geert Uytterhoeven and
6  *               mdacon.c by Andrew Apted
7  *
8  * History:
9  *
10  * - Revision 0.1.8 (23 Oct 2002): Ported to new framebuffer api.
11  * 
12  * - Revision 0.1.7 (23 Jan 2001): fix crash resulting from MDA only cards 
13  *                                 being detected as Hercules.   (Paul G.)
14  * - Revision 0.1.6 (17 Aug 2000): new style structs
15  *                                 documentation
16  * - Revision 0.1.5 (13 Mar 2000): spinlocks instead of saveflags();cli();etc
17  *                                 minor fixes
18  * - Revision 0.1.4 (24 Jan 2000): fixed a bug in hga_card_detect() for 
19  *                                  HGA-only systems
20  * - Revision 0.1.3 (22 Jan 2000): modified for the new fb_info structure
21  *                                 screen is cleared after rmmod
22  *                                 virtual resolutions
23  *                                 module parameter 'nologo={0|1}'
24  *                                 the most important: boot logo :)
25  * - Revision 0.1.0  (6 Dec 1999): faster scrolling and minor fixes
26  * - First release  (25 Nov 1999)
27  *
28  * This file is subject to the terms and conditions of the GNU General Public
29  * License.  See the file COPYING in the main directory of this archive
30  * for more details.
31  */
32
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/errno.h>
36 #include <linux/spinlock.h>
37 #include <linux/string.h>
38 #include <linux/mm.h>
39 #include <linux/tty.h>
40 #include <linux/slab.h>
41 #include <linux/delay.h>
42 #include <linux/fb.h>
43 #include <linux/init.h>
44 #include <linux/ioport.h>
45 #include <linux/platform_device.h>
46 #include <asm/io.h>
47 #include <asm/vga.h>
48
49 #if 0
50 #define DPRINTK(args...) printk(KERN_DEBUG __FILE__": " ##args)
51 #else
52 #define DPRINTK(args...)
53 #endif
54
55 #if 0
56 #define CHKINFO(ret) if (info != &fb_info) { printk(KERN_DEBUG __FILE__": This should never happen, line:%d \n", __LINE__); return ret; }
57 #else
58 #define CHKINFO(ret)
59 #endif
60
61 /* Description of the hardware layout */
62
63 static void __iomem *hga_vram;                  /* Base of video memory */
64 static unsigned long hga_vram_len;              /* Size of video memory */
65
66 #define HGA_ROWADDR(row) ((row%4)*8192 + (row>>2)*90)
67 #define HGA_TXT                 0
68 #define HGA_GFX                 1
69
70 static inline u8 __iomem * rowaddr(struct fb_info *info, u_int row)
71 {
72         return info->screen_base + HGA_ROWADDR(row);
73 }
74
75 static int hga_mode = -1;                       /* 0 = txt, 1 = gfx mode */
76
77 static enum { TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } hga_type;
78 static char *hga_type_name;
79
80 #define HGA_INDEX_PORT          0x3b4           /* Register select port */
81 #define HGA_VALUE_PORT          0x3b5           /* Register value port */
82 #define HGA_MODE_PORT           0x3b8           /* Mode control port */
83 #define HGA_STATUS_PORT         0x3ba           /* Status and Config port */
84 #define HGA_GFX_PORT            0x3bf           /* Graphics control port */
85
86 /* HGA register values */
87
88 #define HGA_CURSOR_BLINKING     0x00
89 #define HGA_CURSOR_OFF          0x20
90 #define HGA_CURSOR_SLOWBLINK    0x60
91
92 #define HGA_MODE_GRAPHICS       0x02
93 #define HGA_MODE_VIDEO_EN       0x08
94 #define HGA_MODE_BLINK_EN       0x20
95 #define HGA_MODE_GFX_PAGE1      0x80
96
97 #define HGA_STATUS_HSYNC        0x01
98 #define HGA_STATUS_VSYNC        0x80
99 #define HGA_STATUS_VIDEO        0x08
100
101 #define HGA_CONFIG_COL132       0x08
102 #define HGA_GFX_MODE_EN         0x01
103 #define HGA_GFX_PAGE_EN         0x02
104
105 /* Global locks */
106
107 static DEFINE_SPINLOCK(hga_reg_lock);
108
109 /* Framebuffer driver structures */
110
111 static struct fb_var_screeninfo __initdata hga_default_var = {
112         .xres           = 720,
113         .yres           = 348,
114         .xres_virtual   = 720,
115         .yres_virtual   = 348,
116         .bits_per_pixel = 1,
117         .red            = {0, 1, 0},
118         .green          = {0, 1, 0},
119         .blue           = {0, 1, 0},
120         .transp         = {0, 0, 0},
121         .height         = -1,
122         .width          = -1,
123 };
124
125 static struct fb_fix_screeninfo __initdata hga_fix = {
126         .id             = "HGA",
127         .type           = FB_TYPE_PACKED_PIXELS,        /* (not sure) */
128         .visual         = FB_VISUAL_MONO10,
129         .xpanstep       = 8,
130         .ypanstep       = 8,
131         .line_length    = 90,
132         .accel          = FB_ACCEL_NONE
133 };
134
135 /* Don't assume that tty1 will be the initial current console. */
136 static int release_io_port = 0;
137 static int release_io_ports = 0;
138 static int nologo = 0;
139
140 /* -------------------------------------------------------------------------
141  *
142  * Low level hardware functions
143  *
144  * ------------------------------------------------------------------------- */
145
146 static void write_hga_b(unsigned int val, unsigned char reg)
147 {
148         outb_p(reg, HGA_INDEX_PORT); 
149         outb_p(val, HGA_VALUE_PORT);
150 }
151
152 static void write_hga_w(unsigned int val, unsigned char reg)
153 {
154         outb_p(reg,   HGA_INDEX_PORT); outb_p(val >> 8,   HGA_VALUE_PORT);
155         outb_p(reg+1, HGA_INDEX_PORT); outb_p(val & 0xff, HGA_VALUE_PORT);
156 }
157
158 static int test_hga_b(unsigned char val, unsigned char reg)
159 {
160         outb_p(reg, HGA_INDEX_PORT); 
161         outb  (val, HGA_VALUE_PORT);
162         udelay(20); val = (inb_p(HGA_VALUE_PORT) == val);
163         return val;
164 }
165
166 static void hga_clear_screen(void)
167 {
168         unsigned char fillchar = 0xbf; /* magic */
169         unsigned long flags;
170
171         spin_lock_irqsave(&hga_reg_lock, flags);
172         if (hga_mode == HGA_TXT)
173                 fillchar = ' ';
174         else if (hga_mode == HGA_GFX)
175                 fillchar = 0x00;
176         spin_unlock_irqrestore(&hga_reg_lock, flags);
177         if (fillchar != 0xbf)
178                 memset_io(hga_vram, fillchar, hga_vram_len);
179 }
180
181 static void hga_txt_mode(void)
182 {
183         unsigned long flags;
184
185         spin_lock_irqsave(&hga_reg_lock, flags);
186         outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_BLINK_EN, HGA_MODE_PORT);
187         outb_p(0x00, HGA_GFX_PORT);
188         outb_p(0x00, HGA_STATUS_PORT);
189
190         write_hga_b(0x61, 0x00);        /* horizontal total */
191         write_hga_b(0x50, 0x01);        /* horizontal displayed */
192         write_hga_b(0x52, 0x02);        /* horizontal sync pos */
193         write_hga_b(0x0f, 0x03);        /* horizontal sync width */
194
195         write_hga_b(0x19, 0x04);        /* vertical total */
196         write_hga_b(0x06, 0x05);        /* vertical total adjust */
197         write_hga_b(0x19, 0x06);        /* vertical displayed */
198         write_hga_b(0x19, 0x07);        /* vertical sync pos */
199
200         write_hga_b(0x02, 0x08);        /* interlace mode */
201         write_hga_b(0x0d, 0x09);        /* maximum scanline */
202         write_hga_b(0x0c, 0x0a);        /* cursor start */
203         write_hga_b(0x0d, 0x0b);        /* cursor end */
204
205         write_hga_w(0x0000, 0x0c);      /* start address */
206         write_hga_w(0x0000, 0x0e);      /* cursor location */
207
208         hga_mode = HGA_TXT;
209         spin_unlock_irqrestore(&hga_reg_lock, flags);
210 }
211
212 static void hga_gfx_mode(void)
213 {
214         unsigned long flags;
215
216         spin_lock_irqsave(&hga_reg_lock, flags);
217         outb_p(0x00, HGA_STATUS_PORT);
218         outb_p(HGA_GFX_MODE_EN, HGA_GFX_PORT);
219         outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT);
220
221         write_hga_b(0x35, 0x00);        /* horizontal total */
222         write_hga_b(0x2d, 0x01);        /* horizontal displayed */
223         write_hga_b(0x2e, 0x02);        /* horizontal sync pos */
224         write_hga_b(0x07, 0x03);        /* horizontal sync width */
225
226         write_hga_b(0x5b, 0x04);        /* vertical total */
227         write_hga_b(0x02, 0x05);        /* vertical total adjust */
228         write_hga_b(0x57, 0x06);        /* vertical displayed */
229         write_hga_b(0x57, 0x07);        /* vertical sync pos */
230
231         write_hga_b(0x02, 0x08);        /* interlace mode */
232         write_hga_b(0x03, 0x09);        /* maximum scanline */
233         write_hga_b(0x00, 0x0a);        /* cursor start */
234         write_hga_b(0x00, 0x0b);        /* cursor end */
235
236         write_hga_w(0x0000, 0x0c);      /* start address */
237         write_hga_w(0x0000, 0x0e);      /* cursor location */
238
239         hga_mode = HGA_GFX;
240         spin_unlock_irqrestore(&hga_reg_lock, flags);
241 }
242
243 static void hga_show_logo(struct fb_info *info)
244 {
245 /*
246         void __iomem *dest = hga_vram;
247         char *logo = linux_logo_bw;
248         int x, y;
249         
250         for (y = 134; y < 134 + 80 ; y++) * this needs some cleanup *
251                 for (x = 0; x < 10 ; x++)
252                         writeb(~*(logo++),(dest + HGA_ROWADDR(y) + x + 40));
253 */
254 }
255
256 static void hga_pan(unsigned int xoffset, unsigned int yoffset)
257 {
258         unsigned int base;
259         unsigned long flags;
260         
261         base = (yoffset / 8) * 90 + xoffset;
262         spin_lock_irqsave(&hga_reg_lock, flags);
263         write_hga_w(base, 0x0c);        /* start address */
264         spin_unlock_irqrestore(&hga_reg_lock, flags);
265         DPRINTK("hga_pan: base:%d\n", base);
266 }
267
268 static void hga_blank(int blank_mode)
269 {
270         unsigned long flags;
271
272         spin_lock_irqsave(&hga_reg_lock, flags);
273         if (blank_mode) {
274                 outb_p(0x00, HGA_MODE_PORT);    /* disable video */
275         } else {
276                 outb_p(HGA_MODE_VIDEO_EN | HGA_MODE_GRAPHICS, HGA_MODE_PORT);
277         }
278         spin_unlock_irqrestore(&hga_reg_lock, flags);
279 }
280
281 static int __init hga_card_detect(void)
282 {
283         int count=0;
284         void __iomem *p, *q;
285         unsigned short p_save, q_save;
286
287         hga_vram_len  = 0x08000;
288
289         hga_vram = ioremap(0xb0000, hga_vram_len);
290
291         if (request_region(0x3b0, 12, "hgafb"))
292                 release_io_ports = 1;
293         if (request_region(0x3bf, 1, "hgafb"))
294                 release_io_port = 1;
295
296         /* do a memory check */
297
298         p = hga_vram;
299         q = hga_vram + 0x01000;
300
301         p_save = readw(p); q_save = readw(q);
302
303         writew(0xaa55, p); if (readw(p) == 0xaa55) count++;
304         writew(0x55aa, p); if (readw(p) == 0x55aa) count++;
305         writew(p_save, p);
306
307         if (count != 2) {
308                 return 0;
309         }
310
311         /* Ok, there is definitely a card registering at the correct
312          * memory location, so now we do an I/O port test.
313          */
314         
315         if (!test_hga_b(0x66, 0x0f)) {      /* cursor low register */
316                 return 0;
317         }
318         if (!test_hga_b(0x99, 0x0f)) {     /* cursor low register */
319                 return 0;
320         }
321
322         /* See if the card is a Hercules, by checking whether the vsync
323          * bit of the status register is changing.  This test lasts for
324          * approximately 1/10th of a second.
325          */
326         
327         p_save = q_save = inb_p(HGA_STATUS_PORT) & HGA_STATUS_VSYNC;
328
329         for (count=0; count < 50000 && p_save == q_save; count++) {
330                 q_save = inb(HGA_STATUS_PORT) & HGA_STATUS_VSYNC;
331                 udelay(2);
332         }
333
334         if (p_save == q_save) 
335                 return 0;
336
337         switch (inb_p(HGA_STATUS_PORT) & 0x70) {
338                 case 0x10:
339                         hga_type = TYPE_HERCPLUS;
340                         hga_type_name = "HerculesPlus";
341                         break;
342                 case 0x50:
343                         hga_type = TYPE_HERCCOLOR;
344                         hga_type_name = "HerculesColor";
345                         break;
346                 default:
347                         hga_type = TYPE_HERC;
348                         hga_type_name = "Hercules";
349                         break;
350         }
351         return 1;
352 }
353
354 /**
355  *      hgafb_open - open the framebuffer device
356  *      @info:pointer to fb_info object containing info for current hga board
357  *      @int:open by console system or userland.
358  */
359
360 static int hgafb_open(struct fb_info *info, int init)
361 {
362         hga_gfx_mode();
363         hga_clear_screen();
364         if (!nologo) hga_show_logo(info);
365         return 0;
366 }
367
368 /**
369  *      hgafb_open - open the framebuffer device
370  *      @info:pointer to fb_info object containing info for current hga board
371  *      @int:open by console system or userland.
372  */
373
374 static int hgafb_release(struct fb_info *info, int init)
375 {
376         hga_txt_mode();
377         hga_clear_screen();
378         return 0;
379 }
380
381 /**
382  *      hgafb_setcolreg - set color registers
383  *      @regno:register index to set
384  *      @red:red value, unused
385  *      @green:green value, unused
386  *      @blue:blue value, unused
387  *      @transp:transparency value, unused
388  *      @info:unused
389  *
390  *      This callback function is used to set the color registers of a HGA
391  *      board. Since we have only two fixed colors only @regno is checked.
392  *      A zero is returned on success and 1 for failure.
393  */
394
395 static int hgafb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
396                            u_int transp, struct fb_info *info)
397 {
398         if (regno > 1)
399                 return 1;
400         return 0;
401 }
402
403 /**
404  *      hga_pan_display - pan or wrap the display
405  *      @var:contains new xoffset, yoffset and vmode values
406  *      @info:pointer to fb_info object containing info for current hga board
407  *
408  *      This function looks only at xoffset, yoffset and the %FB_VMODE_YWRAP
409  *      flag in @var. If input parameters are correct it calls hga_pan() to 
410  *      program the hardware. @info->var is updated to the new values.
411  *      A zero is returned on success and %-EINVAL for failure.
412  */
413
414 static int hgafb_pan_display(struct fb_var_screeninfo *var,
415                              struct fb_info *info)
416 {
417         if (var->vmode & FB_VMODE_YWRAP) {
418                 if (var->yoffset < 0 || 
419                     var->yoffset >= info->var.yres_virtual ||
420                     var->xoffset)
421                         return -EINVAL;
422         } else {
423                 if (var->xoffset + var->xres > info->var.xres_virtual
424                  || var->yoffset + var->yres > info->var.yres_virtual
425                  || var->yoffset % 8)
426                         return -EINVAL;
427         }
428
429         hga_pan(var->xoffset, var->yoffset);
430         return 0;
431 }
432
433 /**
434  *      hgafb_blank - (un)blank the screen
435  *      @blank_mode:blanking method to use
436  *      @info:unused
437  *      
438  *      Blank the screen if blank_mode != 0, else unblank. 
439  *      Implements VESA suspend and powerdown modes on hardware that supports 
440  *      disabling hsync/vsync:
441  *              @blank_mode == 2 means suspend vsync,
442  *              @blank_mode == 3 means suspend hsync,
443  *              @blank_mode == 4 means powerdown.
444  */
445
446 static int hgafb_blank(int blank_mode, struct fb_info *info)
447 {
448         hga_blank(blank_mode);
449         return 0;
450 }
451
452 /*
453  * Accel functions
454  */
455 #ifdef CONFIG_FB_HGA_ACCEL
456 static void hgafb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
457 {
458         u_int rows, y;
459         u8 __iomem *dest;
460
461         y = rect->dy;
462
463         for (rows = rect->height; rows--; y++) {
464                 dest = rowaddr(info, y) + (rect->dx >> 3);
465                 switch (rect->rop) {
466                 case ROP_COPY:
467                         //fb_memset(dest, rect->color, (rect->width >> 3));
468                         break;
469                 case ROP_XOR:
470                         fb_writeb(~(fb_readb(dest)), dest);
471                         break;
472                 }
473         }
474 }
475
476 static void hgafb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
477 {
478         u_int rows, y1, y2;
479         u8 __iomem *src;
480         u8 __iomem *dest;
481
482         if (area->dy <= area->sy) {
483                 y1 = area->sy;
484                 y2 = area->dy;
485
486                 for (rows = area->height; rows--; ) {
487                         src = rowaddr(info, y1) + (area->sx >> 3);
488                         dest = rowaddr(info, y2) + (area->dx >> 3);
489                         //fb_memmove(dest, src, (area->width >> 3));
490                         y1++;
491                         y2++;
492                 }
493         } else {
494                 y1 = area->sy + area->height - 1;
495                 y2 = area->dy + area->height - 1;
496
497                 for (rows = area->height; rows--;) {
498                         src = rowaddr(info, y1) + (area->sx >> 3);
499                         dest = rowaddr(info, y2) + (area->dx >> 3);
500                         //fb_memmove(dest, src, (area->width >> 3));
501                         y1--;
502                         y2--;
503                 }
504         }
505 }
506
507 static void hgafb_imageblit(struct fb_info *info, const struct fb_image *image)
508 {
509         u8 __iomem *dest;
510         u8 *cdat = (u8 *) image->data;
511         u_int rows, y = image->dy;
512         u8 d;
513
514         for (rows = image->height; rows--; y++) {
515                 d = *cdat++;
516                 dest = rowaddr(info, y) + (image->dx >> 3);
517                 fb_writeb(d, dest);
518         }
519 }
520 #else /* !CONFIG_FB_HGA_ACCEL */
521 #define hgafb_fillrect cfb_fillrect
522 #define hgafb_copyarea cfb_copyarea
523 #define hgafb_imageblit cfb_imageblit
524 #endif /* CONFIG_FB_HGA_ACCEL */
525
526
527 static struct fb_ops hgafb_ops = {
528         .owner          = THIS_MODULE,
529         .fb_open        = hgafb_open,
530         .fb_release     = hgafb_release,
531         .fb_setcolreg   = hgafb_setcolreg,
532         .fb_pan_display = hgafb_pan_display,
533         .fb_blank       = hgafb_blank,
534         .fb_fillrect    = hgafb_fillrect,
535         .fb_copyarea    = hgafb_copyarea,
536         .fb_imageblit   = hgafb_imageblit,
537 };
538                 
539 /* ------------------------------------------------------------------------- *
540  *
541  * Functions in fb_info
542  * 
543  * ------------------------------------------------------------------------- */
544
545 /* ------------------------------------------------------------------------- */
546     
547         /*
548          *  Initialization
549          */
550
551 static int __init hgafb_probe(struct device *device)
552 {
553         struct fb_info *info;
554
555         if (! hga_card_detect()) {
556                 printk(KERN_INFO "hgafb: HGA card not detected.\n");
557                 if (hga_vram)
558                         iounmap(hga_vram);
559                 return -EINVAL;
560         }
561
562         printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
563                 hga_type_name, hga_vram_len/1024);
564
565         info = framebuffer_alloc(0, NULL);
566         if (!info) {
567                 iounmap(hga_vram);
568                 return -ENOMEM;
569         }
570
571         hga_fix.smem_start = (unsigned long)hga_vram;
572         hga_fix.smem_len = hga_vram_len;
573
574         info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_YPAN;
575         info->var = hga_default_var;
576         info->fix = hga_fix;
577         info->monspecs.hfmin = 0;
578         info->monspecs.hfmax = 0;
579         info->monspecs.vfmin = 10000;
580         info->monspecs.vfmax = 10000;
581         info->monspecs.dpms = 0;
582         info->fbops = &hgafb_ops;
583         info->screen_base = hga_vram;
584
585         if (register_framebuffer(info) < 0) {
586                 framebuffer_release(info);
587                 iounmap(hga_vram);
588                 return -EINVAL;
589         }
590
591         printk(KERN_INFO "fb%d: %s frame buffer device\n",
592                info->node, info->fix.id);
593         dev_set_drvdata(device, info);
594         return 0;
595 }
596
597 static int hgafb_remove(struct device *device)
598 {
599         struct fb_info *info = dev_get_drvdata(device);
600
601         hga_txt_mode();
602         hga_clear_screen();
603
604         if (info) {
605                 unregister_framebuffer(info);
606                 framebuffer_release(info);
607         }
608
609         iounmap(hga_vram);
610
611         if (release_io_ports)
612                 release_region(0x3b0, 12);
613
614         if (release_io_port)
615                 release_region(0x3bf, 1);
616
617         return 0;
618 }
619
620 static struct device_driver hgafb_driver = {
621         .name = "hgafb",
622         .bus  = &platform_bus_type,
623         .probe = hgafb_probe,
624         .remove = hgafb_remove,
625 };
626
627 static struct platform_device hgafb_device = {
628         .name = "hgafb",
629 };
630
631 static int __init hgafb_init(void)
632 {
633         int ret;
634
635         if (fb_get_options("hgafb", NULL))
636                 return -ENODEV;
637
638         ret = driver_register(&hgafb_driver);
639
640         if (!ret) {
641                 ret = platform_device_register(&hgafb_device);
642                 if (ret)
643                         driver_unregister(&hgafb_driver);
644         }
645
646         return ret;
647 }
648
649 static void __exit hgafb_exit(void)
650 {
651         platform_device_unregister(&hgafb_device);
652         driver_unregister(&hgafb_driver);
653 }
654
655 /* -------------------------------------------------------------------------
656  *
657  *  Modularization
658  *
659  * ------------------------------------------------------------------------- */
660
661 MODULE_AUTHOR("Ferenc Bakonyi (fero@drama.obuda.kando.hu)");
662 MODULE_DESCRIPTION("FBDev driver for Hercules Graphics Adaptor");
663 MODULE_LICENSE("GPL");
664
665 module_param(nologo, bool, 0);
666 MODULE_PARM_DESC(nologo, "Disables startup logo if != 0 (default=0)");
667 module_init(hgafb_init);
668 module_exit(hgafb_exit);