patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / video / console / mdacon.c
1 /*
2  *  linux/drivers/video/mdacon.c -- Low level MDA based console driver
3  *
4  *      (c) 1998 Andrew Apted <ajapted@netspace.net.au>
5  *
6  *      including portions (c) 1995-1998 Patrick Caulfield.
7  *
8  *      slight improvements (c) 2000 Edward Betts <edward@debian.org>
9  *
10  *  This file is based on the VGA console driver (vgacon.c):
11  *      
12  *      Created 28 Sep 1997 by Geert Uytterhoeven
13  *
14  *      Rewritten by Martin Mares <mj@ucw.cz>, July 1998
15  *
16  *  and on the old console.c, vga.c and vesa_blank.c drivers:
17  *
18  *      Copyright (C) 1991, 1992  Linus Torvalds
19  *                          1995  Jay Estabrook
20  *
21  *  This file is subject to the terms and conditions of the GNU General Public
22  *  License.  See the file COPYING in the main directory of this archive for
23  *  more details.
24  *
25  *  Changelog:
26  *  Paul G. (03/2001) Fix mdacon= boot prompt to use __setup().
27  */
28
29 #include <linux/types.h>
30 #include <linux/sched.h>
31 #include <linux/fs.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/tty.h>
35 #include <linux/console.h>
36 #include <linux/string.h>
37 #include <linux/kd.h>
38 #include <linux/slab.h>
39 #include <linux/vt_kern.h>
40 #include <linux/vt_buffer.h>
41 #include <linux/selection.h>
42 #include <linux/spinlock.h>
43 #include <linux/ioport.h>
44 #include <linux/delay.h>
45 #include <linux/init.h>
46
47 #include <asm/io.h>
48 #include <asm/vga.h>
49
50 static spinlock_t mda_lock = SPIN_LOCK_UNLOCKED;
51
52 /* description of the hardware layout */
53
54 static unsigned long    mda_vram_base;          /* Base of video memory */
55 static unsigned long    mda_vram_len;           /* Size of video memory */
56 static unsigned int     mda_num_columns;        /* Number of text columns */
57 static unsigned int     mda_num_lines;          /* Number of text lines */
58
59 static unsigned int     mda_index_port;         /* Register select port */
60 static unsigned int     mda_value_port;         /* Register value port */
61 static unsigned int     mda_mode_port;          /* Mode control port */
62 static unsigned int     mda_status_port;        /* Status and Config port */
63 static unsigned int     mda_gfx_port;           /* Graphics control port */
64
65 /* current hardware state */
66
67 static int      mda_origin_loc=-1;
68 static int      mda_cursor_loc=-1;
69 static int      mda_cursor_size_from=-1;
70 static int      mda_cursor_size_to=-1;
71
72 static enum { TYPE_MDA, TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } mda_type;
73 static char *mda_type_name;
74
75 /* console information */
76
77 static int      mda_first_vc = 1;
78 static int      mda_last_vc  = 16;
79
80 static struct vc_data   *mda_display_fg = NULL;
81
82 MODULE_PARM(mda_first_vc, "1-255i");
83 MODULE_PARM(mda_last_vc,  "1-255i");
84
85 /* MDA register values
86  */
87
88 #define MDA_CURSOR_BLINKING     0x00
89 #define MDA_CURSOR_OFF          0x20
90 #define MDA_CURSOR_SLOWBLINK    0x60
91
92 #define MDA_MODE_GRAPHICS       0x02
93 #define MDA_MODE_VIDEO_EN       0x08
94 #define MDA_MODE_BLINK_EN       0x20
95 #define MDA_MODE_GFX_PAGE1      0x80
96
97 #define MDA_STATUS_HSYNC        0x01
98 #define MDA_STATUS_VSYNC        0x80
99 #define MDA_STATUS_VIDEO        0x08
100
101 #define MDA_CONFIG_COL132       0x08
102 #define MDA_GFX_MODE_EN         0x01
103 #define MDA_GFX_PAGE_EN         0x02
104
105
106 /*
107  * MDA could easily be classified as "pre-dinosaur hardware".
108  */
109
110 static void write_mda_b(unsigned int val, unsigned char reg)
111 {
112         unsigned long flags;
113
114         spin_lock_irqsave(&mda_lock, flags);    
115
116         outb_p(reg, mda_index_port); 
117         outb_p(val, mda_value_port);
118
119         spin_unlock_irqrestore(&mda_lock, flags);
120 }
121
122 static void write_mda_w(unsigned int val, unsigned char reg)
123 {
124         unsigned long flags;
125
126         spin_lock_irqsave(&mda_lock, flags);
127
128         outb_p(reg,   mda_index_port); outb_p(val >> 8,   mda_value_port);
129         outb_p(reg+1, mda_index_port); outb_p(val & 0xff, mda_value_port);
130
131         spin_unlock_irqrestore(&mda_lock, flags);
132 }
133
134 #ifdef TEST_MDA_B
135 static int test_mda_b(unsigned char val, unsigned char reg)
136 {
137         unsigned long flags;
138
139         spin_lock_irqsave(&mda_lock, flags);
140
141         outb_p(reg, mda_index_port); 
142         outb  (val, mda_value_port);
143
144         udelay(20); val = (inb_p(mda_value_port) == val);
145
146         spin_unlock_irqrestore(&mda_lock, flags);
147         return val;
148 }
149 #endif
150
151 static inline void mda_set_origin(unsigned int location)
152 {
153         if (mda_origin_loc == location)
154                 return;
155
156         write_mda_w(location >> 1, 0x0c);
157
158         mda_origin_loc = location;
159 }
160
161 static inline void mda_set_cursor(unsigned int location) 
162 {
163         if (mda_cursor_loc == location)
164                 return;
165
166         write_mda_w(location >> 1, 0x0e);
167
168         mda_cursor_loc = location;
169 }
170
171 static inline void mda_set_cursor_size(int from, int to)
172 {
173         if (mda_cursor_size_from==from && mda_cursor_size_to==to)
174                 return;
175         
176         if (from > to) {
177                 write_mda_b(MDA_CURSOR_OFF, 0x0a);      /* disable cursor */
178         } else {
179                 write_mda_b(from, 0x0a);        /* cursor start */
180                 write_mda_b(to,   0x0b);        /* cursor end */
181         }
182
183         mda_cursor_size_from = from;
184         mda_cursor_size_to   = to;
185 }
186
187
188 #ifndef MODULE
189 static int __init mdacon_setup(char *str)
190 {
191         /* command line format: mdacon=<first>,<last> */
192
193         int ints[3];
194
195         str = get_options(str, ARRAY_SIZE(ints), ints);
196
197         if (ints[0] < 2)
198                 return 0;
199
200         if (ints[1] < 1 || ints[1] > MAX_NR_CONSOLES || 
201             ints[2] < 1 || ints[2] > MAX_NR_CONSOLES)
202                 return 0;
203
204         mda_first_vc = ints[1];
205         mda_last_vc  = ints[2];
206         return 1;
207 }
208
209 __setup("mdacon=", mdacon_setup);
210 #endif
211
212 static int __init mda_detect(void)
213 {
214         int count=0;
215         u16 *p, p_save;
216         u16 *q, q_save;
217
218         /* do a memory check */
219
220         p = (u16 *) mda_vram_base;
221         q = (u16 *) (mda_vram_base + 0x01000);
222
223         p_save = scr_readw(p); q_save = scr_readw(q);
224
225         scr_writew(0xAA55, p); if (scr_readw(p) == 0xAA55) count++;
226         scr_writew(0x55AA, p); if (scr_readw(p) == 0x55AA) count++;
227         scr_writew(p_save, p);
228
229         if (count != 2) {
230                 return 0;
231         }
232
233         /* check if we have 4K or 8K */
234
235         scr_writew(0xA55A, q); scr_writew(0x0000, p);
236         if (scr_readw(q) == 0xA55A) count++;
237         
238         scr_writew(0x5AA5, q); scr_writew(0x0000, p);
239         if (scr_readw(q) == 0x5AA5) count++;
240
241         scr_writew(p_save, p); scr_writew(q_save, q);
242         
243         if (count == 4) {
244                 mda_vram_len = 0x02000;
245         }
246         
247         /* Ok, there is definitely a card registering at the correct
248          * memory location, so now we do an I/O port test.
249          */
250
251 #ifdef TEST_MDA_B
252         /* Edward: These two mess `tests' mess up my cursor on bootup */
253
254         /* cursor low register */
255         if (! test_mda_b(0x66, 0x0f)) {
256                 return 0;
257         }
258
259         /* cursor low register */
260         if (! test_mda_b(0x99, 0x0f)) {
261                 return 0;
262         }
263 #endif
264
265         /* See if the card is a Hercules, by checking whether the vsync
266          * bit of the status register is changing.  This test lasts for
267          * approximately 1/10th of a second.
268          */
269         
270         p_save = q_save = inb_p(mda_status_port) & MDA_STATUS_VSYNC;
271
272         for (count=0; count < 50000 && p_save == q_save; count++) {
273                 q_save = inb(mda_status_port) & MDA_STATUS_VSYNC;
274                 udelay(2);
275         }
276
277         if (p_save != q_save) {
278                 switch (inb_p(mda_status_port) & 0x70) {
279                         case 0x10:
280                                 mda_type = TYPE_HERCPLUS;
281                                 mda_type_name = "HerculesPlus";
282                                 break;
283                         case 0x50:
284                                 mda_type = TYPE_HERCCOLOR;
285                                 mda_type_name = "HerculesColor";
286                                 break;
287                         default:
288                                 mda_type = TYPE_HERC;
289                                 mda_type_name = "Hercules";
290                                 break;
291                 }
292         }
293
294         return 1;
295 }
296
297 static void __init mda_initialize(void)
298 {
299         write_mda_b(97, 0x00);          /* horizontal total */
300         write_mda_b(80, 0x01);          /* horizontal displayed */
301         write_mda_b(82, 0x02);          /* horizontal sync pos */
302         write_mda_b(15, 0x03);          /* horizontal sync width */
303
304         write_mda_b(25, 0x04);          /* vertical total */
305         write_mda_b(6,  0x05);          /* vertical total adjust */
306         write_mda_b(25, 0x06);          /* vertical displayed */
307         write_mda_b(25, 0x07);          /* vertical sync pos */
308
309         write_mda_b(2,  0x08);          /* interlace mode */
310         write_mda_b(13, 0x09);          /* maximum scanline */
311         write_mda_b(12, 0x0a);          /* cursor start */
312         write_mda_b(13, 0x0b);          /* cursor end */
313
314         write_mda_w(0x0000, 0x0c);      /* start address */
315         write_mda_w(0x0000, 0x0e);      /* cursor location */
316
317         outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN, mda_mode_port);
318         outb_p(0x00, mda_status_port);
319         outb_p(0x00, mda_gfx_port);
320 }
321
322 static const char __init *mdacon_startup(void)
323 {
324         mda_num_columns = 80;
325         mda_num_lines   = 25;
326
327         mda_vram_base = VGA_MAP_MEM(0xb0000);
328         mda_vram_len  = 0x01000;
329
330         mda_index_port  = 0x3b4;
331         mda_value_port  = 0x3b5;
332         mda_mode_port   = 0x3b8;
333         mda_status_port = 0x3ba;
334         mda_gfx_port    = 0x3bf;
335
336         mda_type = TYPE_MDA;
337         mda_type_name = "MDA";
338
339         if (! mda_detect()) {
340                 printk("mdacon: MDA card not detected.\n");
341                 return NULL;
342         }
343
344         if (mda_type != TYPE_MDA) {
345                 mda_initialize();
346         }
347
348         /* cursor looks ugly during boot-up, so turn it off */
349         mda_set_cursor(mda_vram_len - 1);
350
351         printk("mdacon: %s with %ldK of memory detected.\n",
352                 mda_type_name, mda_vram_len/1024);
353
354         return "MDA-2";
355 }
356
357 static void mdacon_init(struct vc_data *c, int init)
358 {
359         c->vc_complement_mask = 0x0800;  /* reverse video */
360         c->vc_display_fg = &mda_display_fg;
361
362         if (init) {
363                 c->vc_cols = mda_num_columns;
364                 c->vc_rows = mda_num_lines;
365         } else {
366                 vc_resize(c->vc_num, mda_num_columns, mda_num_lines);
367         }
368         
369         /* make the first MDA console visible */
370
371         if (mda_display_fg == NULL)
372                 mda_display_fg = c;
373 }
374
375 static void mdacon_deinit(struct vc_data *c)
376 {
377         /* con_set_default_unimap(c->vc_num); */
378
379         if (mda_display_fg == c)
380                 mda_display_fg = NULL;
381 }
382
383 static inline u16 mda_convert_attr(u16 ch)
384 {
385         u16 attr = 0x0700;
386
387         /* Underline and reverse-video are mutually exclusive on MDA.
388          * Since reverse-video is used for cursors and selected areas,
389          * it takes precedence. 
390          */
391
392         if (ch & 0x0800)        attr = 0x7000;  /* reverse */
393         else if (ch & 0x0400)   attr = 0x0100;  /* underline */
394
395         return ((ch & 0x0200) << 2) |           /* intensity */ 
396                 (ch & 0x8000) |                 /* blink */ 
397                 (ch & 0x00ff) | attr;
398 }
399
400 static u8 mdacon_build_attr(struct vc_data *c, u8 color, u8 intensity, 
401                             u8 blink, u8 underline, u8 reverse)
402 {
403         /* The attribute is just a bit vector:
404          *
405          *      Bit 0..1 : intensity (0..2)
406          *      Bit 2    : underline
407          *      Bit 3    : reverse
408          *      Bit 7    : blink
409          */
410
411         return (intensity & 3) |
412                 ((underline & 1) << 2) |
413                 ((reverse   & 1) << 3) |
414                 ((blink     & 1) << 7);
415 }
416
417 static void mdacon_invert_region(struct vc_data *c, u16 *p, int count)
418 {
419         for (; count > 0; count--) {
420                 scr_writew(scr_readw(p) ^ 0x0800, p);
421                 p++;
422         }
423 }
424
425 #define MDA_ADDR(x,y)  ((u16 *) mda_vram_base + (y)*mda_num_columns + (x))
426
427 static void mdacon_putc(struct vc_data *c, int ch, int y, int x)
428 {
429         scr_writew(mda_convert_attr(ch), MDA_ADDR(x, y));
430 }
431
432 static void mdacon_putcs(struct vc_data *c, const unsigned short *s,
433                          int count, int y, int x)
434 {
435         u16 *dest = MDA_ADDR(x, y);
436
437         for (; count > 0; count--) {
438                 scr_writew(mda_convert_attr(scr_readw(s++)), dest++);
439         }
440 }
441
442 static void mdacon_clear(struct vc_data *c, int y, int x, 
443                           int height, int width)
444 {
445         u16 *dest = MDA_ADDR(x, y);
446         u16 eattr = mda_convert_attr(c->vc_video_erase_char);
447
448         if (width <= 0 || height <= 0)
449                 return;
450
451         if (x==0 && width==mda_num_columns) {
452                 scr_memsetw(dest, eattr, height*width*2);
453         } else {
454                 for (; height > 0; height--, dest+=mda_num_columns)
455                         scr_memsetw(dest, eattr, width*2);
456         }
457 }
458                         
459 static void mdacon_bmove(struct vc_data *c, int sy, int sx, 
460                          int dy, int dx, int height, int width)
461 {
462         u16 *src, *dest;
463
464         if (width <= 0 || height <= 0)
465                 return;
466                 
467         if (sx==0 && dx==0 && width==mda_num_columns) {
468                 scr_memmovew(MDA_ADDR(0,dy), MDA_ADDR(0,sy), height*width*2);
469
470         } else if (dy < sy || (dy == sy && dx < sx)) {
471                 src  = MDA_ADDR(sx, sy);
472                 dest = MDA_ADDR(dx, dy);
473
474                 for (; height > 0; height--) {
475                         scr_memmovew(dest, src, width*2);
476                         src  += mda_num_columns;
477                         dest += mda_num_columns;
478                 }
479         } else {
480                 src  = MDA_ADDR(sx, sy+height-1);
481                 dest = MDA_ADDR(dx, dy+height-1);
482
483                 for (; height > 0; height--) {
484                         scr_memmovew(dest, src, width*2);
485                         src  -= mda_num_columns;
486                         dest -= mda_num_columns;
487                 }
488         }
489 }
490
491 static int mdacon_switch(struct vc_data *c)
492 {
493         return 1;       /* redrawing needed */
494 }
495
496 static int mdacon_set_palette(struct vc_data *c, unsigned char *table)
497 {
498         return -EINVAL;
499 }
500
501 static int mdacon_blank(struct vc_data *c, int blank, int mode_switch)
502 {
503         if (mda_type == TYPE_MDA) {
504                 if (blank) 
505                         scr_memsetw((void *)mda_vram_base, 
506                                 mda_convert_attr(c->vc_video_erase_char),
507                                 c->vc_screenbuf_size);
508                 /* Tell console.c that it has to restore the screen itself */
509                 return 1;
510         } else {
511                 if (blank)
512                         outb_p(0x00, mda_mode_port);    /* disable video */
513                 else
514                         outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN, 
515                                 mda_mode_port);
516                 return 0;
517         }
518 }
519
520 static int mdacon_font_op(struct vc_data *c, struct console_font_op *op)
521 {
522         return -ENOSYS;
523 }
524
525 static int mdacon_scrolldelta(struct vc_data *c, int lines)
526 {
527         return 0;
528 }
529
530 static void mdacon_cursor(struct vc_data *c, int mode)
531 {
532         if (mode == CM_ERASE) {
533                 mda_set_cursor(mda_vram_len - 1);
534                 return;
535         }
536
537         mda_set_cursor(c->vc_y*mda_num_columns*2 + c->vc_x*2);
538
539         switch (c->vc_cursor_type & 0x0f) {
540
541                 case CUR_LOWER_THIRD:   mda_set_cursor_size(10, 13); break;
542                 case CUR_LOWER_HALF:    mda_set_cursor_size(7,  13); break;
543                 case CUR_TWO_THIRDS:    mda_set_cursor_size(4,  13); break;
544                 case CUR_BLOCK:         mda_set_cursor_size(1,  13); break;
545                 case CUR_NONE:          mda_set_cursor_size(14, 13); break;
546                 default:                mda_set_cursor_size(12, 13); break;
547         }
548 }
549
550 static int mdacon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
551 {
552         u16 eattr = mda_convert_attr(c->vc_video_erase_char);
553
554         if (!lines)
555                 return 0;
556
557         if (lines > c->vc_rows)   /* maximum realistic size */
558                 lines = c->vc_rows;
559
560         switch (dir) {
561
562         case SM_UP:
563                 scr_memmovew(MDA_ADDR(0,t), MDA_ADDR(0,t+lines),
564                                 (b-t-lines)*mda_num_columns*2);
565                 scr_memsetw(MDA_ADDR(0,b-lines), eattr,
566                                 lines*mda_num_columns*2);
567                 break;
568
569         case SM_DOWN:
570                 scr_memmovew(MDA_ADDR(0,t+lines), MDA_ADDR(0,t),
571                                 (b-t-lines)*mda_num_columns*2);
572                 scr_memsetw(MDA_ADDR(0,t), eattr, lines*mda_num_columns*2);
573                 break;
574         }
575
576         return 0;
577 }
578
579
580 /*
581  *  The console `switch' structure for the MDA based console
582  */
583
584 const struct consw mda_con = {
585         .owner =                THIS_MODULE,
586         .con_startup =          mdacon_startup,
587         .con_init =             mdacon_init,
588         .con_deinit =           mdacon_deinit,
589         .con_clear =            mdacon_clear,
590         .con_putc =             mdacon_putc,
591         .con_putcs =            mdacon_putcs,
592         .con_cursor =           mdacon_cursor,
593         .con_scroll =           mdacon_scroll,
594         .con_bmove =            mdacon_bmove,
595         .con_switch =           mdacon_switch,
596         .con_blank =            mdacon_blank,
597         .con_font_op =          mdacon_font_op,
598         .con_set_palette =      mdacon_set_palette,
599         .con_scrolldelta =      mdacon_scrolldelta,
600         .con_build_attr =       mdacon_build_attr,
601         .con_invert_region =    mdacon_invert_region,
602 };
603
604 int __init mda_console_init(void)
605 {
606         if (mda_first_vc > mda_last_vc)
607                 return 1;
608
609         return take_over_console(&mda_con, mda_first_vc-1, mda_last_vc-1, 0);
610 }
611
612 void __exit mda_console_exit(void)
613 {
614         give_up_console(&mda_con);
615 }
616
617 module_init(mda_console_init);
618 module_exit(mda_console_exit);
619
620 MODULE_LICENSE("GPL");
621