Import 2.3.13
[davej-history.git] / drivers / video / mdacon.c
blob0c45722febab29dc287862a13111649e76696136
1 /*
2 * linux/drivers/video/mdacon.c -- Low level MDA based console driver
4 * (c) 1998 Andrew Apted <ajapted@netspace.net.au>
6 * including portions (c) 1995-1998 Patrick Caulfield.
8 * This file is based on the VGA console driver (vgacon.c):
9 *
10 * Created 28 Sep 1997 by Geert Uytterhoeven
12 * Rewritten by Martin Mares <mj@ucw.cz>, July 1998
14 * and on the old console.c, vga.c and vesa_blank.c drivers:
16 * Copyright (C) 1991, 1992 Linus Torvalds
17 * 1995 Jay Estabrook
19 * This file is subject to the terms and conditions of the GNU General Public
20 * License. See the file COPYING in the main directory of this archive for
21 * more details.
24 #include <linux/types.h>
25 #include <linux/sched.h>
26 #include <linux/fs.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/tty.h>
30 #include <linux/console.h>
31 #include <linux/console_struct.h>
32 #include <linux/string.h>
33 #include <linux/kd.h>
34 #include <linux/malloc.h>
35 #include <linux/vt_kern.h>
36 #include <linux/vt_buffer.h>
37 #include <linux/selection.h>
38 #include <linux/ioport.h>
39 #include <linux/delay.h>
40 #include <linux/init.h>
42 #include <asm/io.h>
43 #include <asm/vga.h>
46 /* description of the hardware layout */
48 static unsigned long mda_vram_base; /* Base of video memory */
49 static unsigned long mda_vram_len; /* Size of video memory */
50 static unsigned int mda_num_columns; /* Number of text columns */
51 static unsigned int mda_num_lines; /* Number of text lines */
53 static unsigned int mda_index_port; /* Register select port */
54 static unsigned int mda_value_port; /* Register value port */
55 static unsigned int mda_mode_port; /* Mode control port */
56 static unsigned int mda_status_port; /* Status and Config port */
57 static unsigned int mda_gfx_port; /* Graphics control port */
59 /* current hardware state */
61 static int mda_origin_loc=-1;
62 static int mda_cursor_loc=-1;
63 static int mda_cursor_size_from=-1;
64 static int mda_cursor_size_to=-1;
66 static enum { TYPE_MDA, TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } mda_type;
67 static char *mda_type_name;
69 /* console information */
71 static int mda_first_vc = 13;
72 static int mda_last_vc = 16;
74 static struct vc_data *mda_display_fg = NULL;
76 #ifdef MODULE_PARM
77 MODULE_PARM(mda_first_vc, "1-255i");
78 MODULE_PARM(mda_last_vc, "1-255i");
79 #endif
82 /* MDA register values
85 #define MDA_CURSOR_BLINKING 0x00
86 #define MDA_CURSOR_OFF 0x20
87 #define MDA_CURSOR_SLOWBLINK 0x60
89 #define MDA_MODE_GRAPHICS 0x02
90 #define MDA_MODE_VIDEO_EN 0x08
91 #define MDA_MODE_BLINK_EN 0x20
92 #define MDA_MODE_GFX_PAGE1 0x80
94 #define MDA_STATUS_HSYNC 0x01
95 #define MDA_STATUS_VSYNC 0x80
96 #define MDA_STATUS_VIDEO 0x08
98 #define MDA_CONFIG_COL132 0x08
99 #define MDA_GFX_MODE_EN 0x01
100 #define MDA_GFX_PAGE_EN 0x02
104 * MDA could easily be classified as "pre-dinosaur hardware".
107 static void write_mda_b(unsigned int val, unsigned char reg)
109 unsigned long flags;
111 save_flags(flags); cli();
113 outb_p(reg, mda_index_port);
114 outb_p(val, mda_value_port);
116 restore_flags(flags);
119 static void write_mda_w(unsigned int val, unsigned char reg)
121 unsigned long flags;
123 save_flags(flags); cli();
125 outb_p(reg, mda_index_port); outb_p(val >> 8, mda_value_port);
126 outb_p(reg+1, mda_index_port); outb_p(val & 0xff, mda_value_port);
128 restore_flags(flags);
131 static int test_mda_b(unsigned char val, unsigned char reg)
133 unsigned long flags;
135 save_flags(flags); cli();
137 outb_p(reg, mda_index_port);
138 outb (val, mda_value_port);
140 udelay(20); val = (inb_p(mda_value_port) == val);
142 restore_flags(flags);
144 return val;
147 static inline void mda_set_origin(unsigned int location)
149 if (mda_origin_loc == location)
150 return;
152 write_mda_w(location >> 1, 0x0c);
154 mda_origin_loc = location;
157 static inline void mda_set_cursor(unsigned int location)
159 if (mda_cursor_loc == location)
160 return;
162 write_mda_w(location >> 1, 0x0e);
164 mda_cursor_loc = location;
167 static inline void mda_set_cursor_size(int from, int to)
169 if (mda_cursor_size_from==from && mda_cursor_size_to==to)
170 return;
172 if (from > to) {
173 write_mda_b(MDA_CURSOR_OFF, 0x0a); /* disable cursor */
174 } else {
175 write_mda_b(from, 0x0a); /* cursor start */
176 write_mda_b(to, 0x0b); /* cursor end */
179 mda_cursor_size_from = from;
180 mda_cursor_size_to = to;
184 #ifndef MODULE
185 void __init mdacon_setup(char *str, int *ints)
187 /* command line format: mdacon=<first>,<last> */
189 if (ints[0] < 2)
190 return;
192 if (ints[1] < 1 || ints[1] > MAX_NR_CONSOLES ||
193 ints[2] < 1 || ints[2] > MAX_NR_CONSOLES)
194 return;
196 mda_first_vc = ints[1]-1;
197 mda_last_vc = ints[2]-1;
199 #endif
201 #ifdef MODULE
202 static int mda_detect(void)
203 #else
204 static int __init mda_detect(void)
205 #endif
207 int count=0;
208 u16 *p, p_save;
209 u16 *q, q_save;
211 /* do a memory check */
213 p = (u16 *) mda_vram_base;
214 q = (u16 *) (mda_vram_base + 0x01000);
216 p_save = scr_readw(p); q_save = scr_readw(q);
218 scr_writew(0xAA55, p); if (scr_readw(p) == 0xAA55) count++;
219 scr_writew(0x55AA, p); if (scr_readw(p) == 0x55AA) count++;
220 scr_writew(p_save, p);
222 if (count != 2) {
223 return 0;
226 /* check if we have 4K or 8K */
228 scr_writew(0xA55A, q); scr_writew(0x0000, p);
229 if (scr_readw(q) == 0xA55A) count++;
231 scr_writew(0x5AA5, q); scr_writew(0x0000, p);
232 if (scr_readw(q) == 0x5AA5) count++;
234 scr_writew(p_save, p); scr_writew(q_save, q);
236 if (count == 4) {
237 mda_vram_len = 0x02000;
240 /* Ok, there is definitely a card registering at the correct
241 * memory location, so now we do an I/O port test.
244 if (! test_mda_b(0x66, 0x0f)) { /* cursor low register */
245 return 0;
247 if (! test_mda_b(0x99, 0x0f)) { /* cursor low register */
248 return 0;
251 /* See if the card is a Hercules, by checking whether the vsync
252 * bit of the status register is changing. This test lasts for
253 * approximately 1/10th of a second.
256 p_save = q_save = inb_p(mda_status_port) & MDA_STATUS_VSYNC;
258 for (count=0; count < 50000 && p_save == q_save; count++) {
259 q_save = inb(mda_status_port) & MDA_STATUS_VSYNC;
260 udelay(2);
263 if (p_save != q_save) {
264 switch (inb_p(mda_status_port) & 0x70) {
265 case 0x10:
266 mda_type = TYPE_HERCPLUS;
267 mda_type_name = "HerculesPlus";
268 break;
269 case 0x50:
270 mda_type = TYPE_HERCCOLOR;
271 mda_type_name = "HerculesColor";
272 break;
273 default:
274 mda_type = TYPE_HERC;
275 mda_type_name = "Hercules";
276 break;
280 return 1;
283 #ifdef MODULE
284 static void mda_initialize(void)
285 #else
286 static void __init mda_initialize(void)
287 #endif
289 write_mda_b(97, 0x00); /* horizontal total */
290 write_mda_b(80, 0x01); /* horizontal displayed */
291 write_mda_b(82, 0x02); /* horizontal sync pos */
292 write_mda_b(15, 0x03); /* horizontal sync width */
294 write_mda_b(25, 0x04); /* vertical total */
295 write_mda_b(6, 0x05); /* vertical total adjust */
296 write_mda_b(25, 0x06); /* vertical displayed */
297 write_mda_b(25, 0x07); /* vertical sync pos */
299 write_mda_b(2, 0x08); /* interlace mode */
300 write_mda_b(13, 0x09); /* maximum scanline */
301 write_mda_b(12, 0x0a); /* cursor start */
302 write_mda_b(13, 0x0b); /* cursor end */
304 write_mda_w(0x0000, 0x0c); /* start address */
305 write_mda_w(0x0000, 0x0e); /* cursor location */
307 outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN, mda_mode_port);
308 outb_p(0x00, mda_status_port);
309 outb_p(0x00, mda_gfx_port);
312 #ifdef MODULE
313 static const char *mdacon_startup(void)
314 #else
315 static const char __init *mdacon_startup(void)
316 #endif
318 mda_num_columns = 80;
319 mda_num_lines = 25;
321 mda_vram_base = VGA_MAP_MEM(0xb0000);
322 mda_vram_len = 0x01000;
324 mda_index_port = 0x3b4;
325 mda_value_port = 0x3b5;
326 mda_mode_port = 0x3b8;
327 mda_status_port = 0x3ba;
328 mda_gfx_port = 0x3bf;
330 mda_type = TYPE_MDA;
331 mda_type_name = "MDA";
333 if (! mda_detect()) {
334 printk("mdacon: MDA card not detected.\n");
335 return NULL;
338 if (mda_type != TYPE_MDA) {
339 mda_initialize();
342 printk("mdacon: %s with %ldK of memory detected.\n",
343 mda_type_name, mda_vram_len/1024);
345 return "MDA-2";
348 static void mdacon_init(struct vc_data *c, int init)
350 c->vc_complement_mask = 0x0800; /* reverse video */
351 c->vc_display_fg = &mda_display_fg;
353 if (init) {
354 c->vc_cols = mda_num_columns;
355 c->vc_rows = mda_num_lines;
356 } else {
357 vc_resize_con(mda_num_lines, mda_num_columns, c->vc_num);
360 /* make the first MDA console visible */
362 if (mda_display_fg == NULL)
363 mda_display_fg = c;
365 MOD_INC_USE_COUNT;
368 static void mdacon_deinit(struct vc_data *c)
370 /* con_set_default_unimap(c->vc_num); */
372 if (mda_display_fg == c)
373 mda_display_fg = NULL;
375 MOD_DEC_USE_COUNT;
378 static inline u16 mda_convert_attr(u16 ch)
380 u16 attr = 0x0700;
382 /* Underline and reverse-video are mutually exclusive on MDA.
383 * Since reverse-video is used for cursors and selected areas,
384 * it takes precedence.
387 if (ch & 0x0800) attr = 0x7000; /* reverse */
388 else if (ch & 0x0400) attr = 0x0100; /* underline */
390 return ((ch & 0x0200) << 2) | /* intensity */
391 (ch & 0x8000) | /* blink */
392 (ch & 0x00ff) | attr;
395 static u8 mdacon_build_attr(struct vc_data *c, u8 color, u8 intensity,
396 u8 blink, u8 underline, u8 reverse)
398 /* The attribute is just a bit vector:
400 * Bit 0..1 : intensity (0..2)
401 * Bit 2 : underline
402 * Bit 3 : reverse
403 * Bit 7 : blink
406 return (intensity & 3) |
407 ((underline & 1) << 2) |
408 ((reverse & 1) << 3) |
409 ((blink & 1) << 7);
412 static void mdacon_invert_region(struct vc_data *c, u16 *p, int count)
414 for (; count > 0; count--) {
415 scr_writew(scr_readw(p) ^ 0x0800, p++);
419 #define MDA_ADDR(x,y) ((u16 *) mda_vram_base + (y)*mda_num_columns + (x))
421 static void mdacon_putc(struct vc_data *c, int ch, int y, int x)
423 scr_writew(mda_convert_attr(ch), MDA_ADDR(x, y));
426 static void mdacon_putcs(struct vc_data *c, const unsigned short *s,
427 int count, int y, int x)
429 u16 *dest = MDA_ADDR(x, y);
431 for (; count > 0; count--) {
432 scr_writew(mda_convert_attr(scr_readw(s++)), dest++);
436 static void mdacon_clear(struct vc_data *c, int y, int x,
437 int height, int width)
439 u16 *dest = MDA_ADDR(x, y);
440 u16 eattr = mda_convert_attr(c->vc_video_erase_char);
442 if (width <= 0 || height <= 0)
443 return;
445 if (x==0 && width==mda_num_columns) {
446 scr_memsetw(dest, eattr, height*width*2);
447 } else {
448 for (; height > 0; height--, dest+=mda_num_columns)
449 scr_memsetw(dest, eattr, width*2);
453 static void mdacon_bmove(struct vc_data *c, int sy, int sx,
454 int dy, int dx, int height, int width)
456 u16 *src, *dest;
458 if (width <= 0 || height <= 0)
459 return;
461 if (sx==0 && dx==0 && width==mda_num_columns) {
462 scr_memmovew(MDA_ADDR(0,dy), MDA_ADDR(0,sy), height*width*2);
464 } else if (dy < sy || (dy == sy && dx < sx)) {
465 src = MDA_ADDR(sx, sy);
466 dest = MDA_ADDR(dx, dy);
468 for (; height > 0; height--) {
469 scr_memmovew(dest, src, width*2);
470 src += mda_num_columns;
471 dest += mda_num_columns;
473 } else {
474 src = MDA_ADDR(sx, sy+height-1);
475 dest = MDA_ADDR(dx, dy+height-1);
477 for (; height > 0; height--) {
478 scr_memmovew(dest, src, width*2);
479 src -= mda_num_columns;
480 dest -= mda_num_columns;
485 static int mdacon_switch(struct vc_data *c)
487 return 1; /* redrawing needed */
490 static int mdacon_set_palette(struct vc_data *c, unsigned char *table)
492 return -EINVAL;
495 static int mdacon_blank(struct vc_data *c, int blank)
497 if (blank) {
498 outb_p(0x00, mda_mode_port); /* disable video */
499 } else {
500 outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN, mda_mode_port);
503 return 0;
506 static int mdacon_font_op(struct vc_data *c, struct console_font_op *op)
508 return -ENOSYS;
511 static int mdacon_scrolldelta(struct vc_data *c, int lines)
513 return 0;
516 static void mdacon_cursor(struct vc_data *c, int mode)
518 if (mode == CM_ERASE) {
519 mda_set_cursor(mda_vram_len - 1);
520 return;
523 mda_set_cursor(c->vc_y*mda_num_columns*2 + c->vc_x*2);
525 switch (c->vc_cursor_type & 0x0f) {
527 case CUR_LOWER_THIRD: mda_set_cursor_size(10, 13); break;
528 case CUR_LOWER_HALF: mda_set_cursor_size(7, 13); break;
529 case CUR_TWO_THIRDS: mda_set_cursor_size(4, 13); break;
530 case CUR_BLOCK: mda_set_cursor_size(1, 13); break;
531 case CUR_NONE: mda_set_cursor_size(14, 13); break;
532 default: mda_set_cursor_size(12, 13); break;
536 static int mdacon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
538 u16 eattr = mda_convert_attr(c->vc_video_erase_char);
540 if (!lines)
541 return 0;
543 if (lines > c->vc_rows) /* maximum realistic size */
544 lines = c->vc_rows;
546 switch (dir) {
548 case SM_UP:
549 scr_memmovew(MDA_ADDR(0,t), MDA_ADDR(0,t+lines),
550 (b-t-lines)*mda_num_columns*2);
551 scr_memsetw(MDA_ADDR(0,b-lines), eattr,
552 lines*mda_num_columns*2);
553 break;
555 case SM_DOWN:
556 scr_memmovew(MDA_ADDR(0,t+lines), MDA_ADDR(0,t),
557 (b-t-lines)*mda_num_columns*2);
558 scr_memsetw(MDA_ADDR(0,t), eattr, lines*mda_num_columns*2);
559 break;
562 return 0;
567 * The console `switch' structure for the MDA based console
570 struct consw mda_con = {
571 mdacon_startup, /* con_startup */
572 mdacon_init, /* con_init */
573 mdacon_deinit, /* con_deinit */
574 mdacon_clear, /* con_clear */
575 mdacon_putc, /* con_putc */
576 mdacon_putcs, /* con_putcs */
577 mdacon_cursor, /* con_cursor */
578 mdacon_scroll, /* con_scroll */
579 mdacon_bmove, /* con_bmove */
580 mdacon_switch, /* con_switch */
581 mdacon_blank, /* con_blank */
582 mdacon_font_op, /* con_font_op */
583 mdacon_set_palette, /* con_set_palette */
584 mdacon_scrolldelta, /* con_scrolldelta */
585 NULL, /* con_set_origin */
586 NULL, /* con_save_screen */
587 mdacon_build_attr, /* con_build_attr */
588 mdacon_invert_region, /* con_invert_region */
591 #ifdef MODULE
592 void mda_console_init(void)
593 #else
594 void __init mda_console_init(void)
595 #endif
597 if (mda_first_vc > mda_last_vc)
598 return;
600 take_over_console(&mda_con, mda_first_vc-1, mda_last_vc-1, 0);
603 #ifdef MODULE
605 int init_module(void)
607 mda_console_init();
609 return 0;
612 void cleanup_module(void)
614 give_up_console(&mda_con);
617 #endif