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 * slight improvements (c) 2000 Edward Betts <edward@debian.org>
10 * This file is based on the VGA console driver (vgacon.c):
12 * Created 28 Sep 1997 by Geert Uytterhoeven
14 * Rewritten by Martin Mares <mj@ucw.cz>, July 1998
16 * and on the old console.c, vga.c and vesa_blank.c drivers:
18 * Copyright (C) 1991, 1992 Linus Torvalds
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
26 * Paul G. (03/2001) Fix mdacon= boot prompt to use __setup().
29 #include <linux/types.h>
30 #include <linux/sched.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>
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>
50 static DEFINE_SPINLOCK(mda_lock);
52 /* description of the hardware layout */
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 */
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 */
65 /* current hardware state */
67 static int mda_cursor_loc=-1;
68 static int mda_cursor_size_from=-1;
69 static int mda_cursor_size_to=-1;
71 static enum { TYPE_MDA, TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } mda_type;
72 static char *mda_type_name;
74 /* console information */
76 static int mda_first_vc = 1;
77 static int mda_last_vc = 16;
79 static struct vc_data *mda_display_fg = NULL;
81 module_param(mda_first_vc, int, 0);
82 module_param(mda_last_vc, int, 0);
84 /* MDA register values
87 #define MDA_CURSOR_BLINKING 0x00
88 #define MDA_CURSOR_OFF 0x20
89 #define MDA_CURSOR_SLOWBLINK 0x60
91 #define MDA_MODE_GRAPHICS 0x02
92 #define MDA_MODE_VIDEO_EN 0x08
93 #define MDA_MODE_BLINK_EN 0x20
94 #define MDA_MODE_GFX_PAGE1 0x80
96 #define MDA_STATUS_HSYNC 0x01
97 #define MDA_STATUS_VSYNC 0x80
98 #define MDA_STATUS_VIDEO 0x08
100 #define MDA_CONFIG_COL132 0x08
101 #define MDA_GFX_MODE_EN 0x01
102 #define MDA_GFX_PAGE_EN 0x02
106 * MDA could easily be classified as "pre-dinosaur hardware".
109 static void write_mda_b(unsigned int val, unsigned char reg)
113 spin_lock_irqsave(&mda_lock, flags);
115 outb_p(reg, mda_index_port);
116 outb_p(val, mda_value_port);
118 spin_unlock_irqrestore(&mda_lock, flags);
121 static void write_mda_w(unsigned int val, unsigned char reg)
125 spin_lock_irqsave(&mda_lock, flags);
127 outb_p(reg, mda_index_port); outb_p(val >> 8, mda_value_port);
128 outb_p(reg+1, mda_index_port); outb_p(val & 0xff, mda_value_port);
130 spin_unlock_irqrestore(&mda_lock, flags);
134 static int test_mda_b(unsigned char val, unsigned char reg)
138 spin_lock_irqsave(&mda_lock, flags);
140 outb_p(reg, mda_index_port);
141 outb (val, mda_value_port);
143 udelay(20); val = (inb_p(mda_value_port) == val);
145 spin_unlock_irqrestore(&mda_lock, flags);
150 static inline void mda_set_cursor(unsigned int location)
152 if (mda_cursor_loc == location)
155 write_mda_w(location >> 1, 0x0e);
157 mda_cursor_loc = location;
160 static inline void mda_set_cursor_size(int from, int to)
162 if (mda_cursor_size_from==from && mda_cursor_size_to==to)
166 write_mda_b(MDA_CURSOR_OFF, 0x0a); /* disable cursor */
168 write_mda_b(from, 0x0a); /* cursor start */
169 write_mda_b(to, 0x0b); /* cursor end */
172 mda_cursor_size_from = from;
173 mda_cursor_size_to = to;
178 static int __init mdacon_setup(char *str)
180 /* command line format: mdacon=<first>,<last> */
184 str = get_options(str, ARRAY_SIZE(ints), ints);
189 if (ints[1] < 1 || ints[1] > MAX_NR_CONSOLES ||
190 ints[2] < 1 || ints[2] > MAX_NR_CONSOLES)
193 mda_first_vc = ints[1];
194 mda_last_vc = ints[2];
198 __setup("mdacon=", mdacon_setup);
201 static int __init mda_detect(void)
207 /* do a memory check */
209 p = (u16 *) mda_vram_base;
210 q = (u16 *) (mda_vram_base + 0x01000);
212 p_save = scr_readw(p); q_save = scr_readw(q);
214 scr_writew(0xAA55, p); if (scr_readw(p) == 0xAA55) count++;
215 scr_writew(0x55AA, p); if (scr_readw(p) == 0x55AA) count++;
216 scr_writew(p_save, p);
222 /* check if we have 4K or 8K */
224 scr_writew(0xA55A, q); scr_writew(0x0000, p);
225 if (scr_readw(q) == 0xA55A) count++;
227 scr_writew(0x5AA5, q); scr_writew(0x0000, p);
228 if (scr_readw(q) == 0x5AA5) count++;
230 scr_writew(p_save, p); scr_writew(q_save, q);
233 mda_vram_len = 0x02000;
236 /* Ok, there is definitely a card registering at the correct
237 * memory location, so now we do an I/O port test.
241 /* Edward: These two mess `tests' mess up my cursor on bootup */
243 /* cursor low register */
244 if (! test_mda_b(0x66, 0x0f)) {
248 /* cursor low register */
249 if (! test_mda_b(0x99, 0x0f)) {
254 /* See if the card is a Hercules, by checking whether the vsync
255 * bit of the status register is changing. This test lasts for
256 * approximately 1/10th of a second.
259 p_save = q_save = inb_p(mda_status_port) & MDA_STATUS_VSYNC;
261 for (count=0; count < 50000 && p_save == q_save; count++) {
262 q_save = inb(mda_status_port) & MDA_STATUS_VSYNC;
266 if (p_save != q_save) {
267 switch (inb_p(mda_status_port) & 0x70) {
269 mda_type = TYPE_HERCPLUS;
270 mda_type_name = "HerculesPlus";
273 mda_type = TYPE_HERCCOLOR;
274 mda_type_name = "HerculesColor";
277 mda_type = TYPE_HERC;
278 mda_type_name = "Hercules";
286 static void __init mda_initialize(void)
288 write_mda_b(97, 0x00); /* horizontal total */
289 write_mda_b(80, 0x01); /* horizontal displayed */
290 write_mda_b(82, 0x02); /* horizontal sync pos */
291 write_mda_b(15, 0x03); /* horizontal sync width */
293 write_mda_b(25, 0x04); /* vertical total */
294 write_mda_b(6, 0x05); /* vertical total adjust */
295 write_mda_b(25, 0x06); /* vertical displayed */
296 write_mda_b(25, 0x07); /* vertical sync pos */
298 write_mda_b(2, 0x08); /* interlace mode */
299 write_mda_b(13, 0x09); /* maximum scanline */
300 write_mda_b(12, 0x0a); /* cursor start */
301 write_mda_b(13, 0x0b); /* cursor end */
303 write_mda_w(0x0000, 0x0c); /* start address */
304 write_mda_w(0x0000, 0x0e); /* cursor location */
306 outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN, mda_mode_port);
307 outb_p(0x00, mda_status_port);
308 outb_p(0x00, mda_gfx_port);
311 static const char *mdacon_startup(void)
313 mda_num_columns = 80;
316 mda_vram_len = 0x01000;
317 mda_vram_base = VGA_MAP_MEM(0xb0000, mda_vram_len);
319 mda_index_port = 0x3b4;
320 mda_value_port = 0x3b5;
321 mda_mode_port = 0x3b8;
322 mda_status_port = 0x3ba;
323 mda_gfx_port = 0x3bf;
326 mda_type_name = "MDA";
328 if (! mda_detect()) {
329 printk("mdacon: MDA card not detected.\n");
333 if (mda_type != TYPE_MDA) {
337 /* cursor looks ugly during boot-up, so turn it off */
338 mda_set_cursor(mda_vram_len - 1);
340 printk("mdacon: %s with %ldK of memory detected.\n",
341 mda_type_name, mda_vram_len/1024);
346 static void mdacon_init(struct vc_data *c, int init)
348 c->vc_complement_mask = 0x0800; /* reverse video */
349 c->vc_display_fg = &mda_display_fg;
352 c->vc_cols = mda_num_columns;
353 c->vc_rows = mda_num_lines;
355 vc_resize(c, mda_num_columns, mda_num_lines);
357 /* make the first MDA console visible */
359 if (mda_display_fg == NULL)
363 static void mdacon_deinit(struct vc_data *c)
365 /* con_set_default_unimap(c->vc_num); */
367 if (mda_display_fg == c)
368 mda_display_fg = NULL;
371 static inline u16 mda_convert_attr(u16 ch)
375 /* Underline and reverse-video are mutually exclusive on MDA.
376 * Since reverse-video is used for cursors and selected areas,
377 * it takes precedence.
380 if (ch & 0x0800) attr = 0x7000; /* reverse */
381 else if (ch & 0x0400) attr = 0x0100; /* underline */
383 return ((ch & 0x0200) << 2) | /* intensity */
384 (ch & 0x8000) | /* blink */
385 (ch & 0x00ff) | attr;
388 static u8 mdacon_build_attr(struct vc_data *c, u8 color, u8 intensity,
389 u8 blink, u8 underline, u8 reverse)
391 /* The attribute is just a bit vector:
393 * Bit 0..1 : intensity (0..2)
399 return (intensity & 3) |
400 ((underline & 1) << 2) |
401 ((reverse & 1) << 3) |
405 static void mdacon_invert_region(struct vc_data *c, u16 *p, int count)
407 for (; count > 0; count--) {
408 scr_writew(scr_readw(p) ^ 0x0800, p);
413 #define MDA_ADDR(x,y) ((u16 *) mda_vram_base + (y)*mda_num_columns + (x))
415 static void mdacon_putc(struct vc_data *c, int ch, int y, int x)
417 scr_writew(mda_convert_attr(ch), MDA_ADDR(x, y));
420 static void mdacon_putcs(struct vc_data *c, const unsigned short *s,
421 int count, int y, int x)
423 u16 *dest = MDA_ADDR(x, y);
425 for (; count > 0; count--) {
426 scr_writew(mda_convert_attr(scr_readw(s++)), dest++);
430 static void mdacon_clear(struct vc_data *c, int y, int x,
431 int height, int width)
433 u16 *dest = MDA_ADDR(x, y);
434 u16 eattr = mda_convert_attr(c->vc_video_erase_char);
436 if (width <= 0 || height <= 0)
439 if (x==0 && width==mda_num_columns) {
440 scr_memsetw(dest, eattr, height*width*2);
442 for (; height > 0; height--, dest+=mda_num_columns)
443 scr_memsetw(dest, eattr, width*2);
447 static void mdacon_bmove(struct vc_data *c, int sy, int sx,
448 int dy, int dx, int height, int width)
452 if (width <= 0 || height <= 0)
455 if (sx==0 && dx==0 && width==mda_num_columns) {
456 scr_memmovew(MDA_ADDR(0,dy), MDA_ADDR(0,sy), height*width*2);
458 } else if (dy < sy || (dy == sy && dx < sx)) {
459 src = MDA_ADDR(sx, sy);
460 dest = MDA_ADDR(dx, dy);
462 for (; height > 0; height--) {
463 scr_memmovew(dest, src, width*2);
464 src += mda_num_columns;
465 dest += mda_num_columns;
468 src = MDA_ADDR(sx, sy+height-1);
469 dest = MDA_ADDR(dx, dy+height-1);
471 for (; height > 0; height--) {
472 scr_memmovew(dest, src, width*2);
473 src -= mda_num_columns;
474 dest -= mda_num_columns;
479 static int mdacon_switch(struct vc_data *c)
481 return 1; /* redrawing needed */
484 static int mdacon_set_palette(struct vc_data *c, unsigned char *table)
489 static int mdacon_blank(struct vc_data *c, int blank, int mode_switch)
491 if (mda_type == TYPE_MDA) {
493 scr_memsetw((void *)mda_vram_base,
494 mda_convert_attr(c->vc_video_erase_char),
495 c->vc_screenbuf_size);
496 /* Tell console.c that it has to restore the screen itself */
500 outb_p(0x00, mda_mode_port); /* disable video */
502 outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN,
508 static int mdacon_scrolldelta(struct vc_data *c, int lines)
513 static void mdacon_cursor(struct vc_data *c, int mode)
515 if (mode == CM_ERASE) {
516 mda_set_cursor(mda_vram_len - 1);
520 mda_set_cursor(c->vc_y*mda_num_columns*2 + c->vc_x*2);
522 switch (c->vc_cursor_type & 0x0f) {
524 case CUR_LOWER_THIRD: mda_set_cursor_size(10, 13); break;
525 case CUR_LOWER_HALF: mda_set_cursor_size(7, 13); break;
526 case CUR_TWO_THIRDS: mda_set_cursor_size(4, 13); break;
527 case CUR_BLOCK: mda_set_cursor_size(1, 13); break;
528 case CUR_NONE: mda_set_cursor_size(14, 13); break;
529 default: mda_set_cursor_size(12, 13); break;
533 static int mdacon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
535 u16 eattr = mda_convert_attr(c->vc_video_erase_char);
540 if (lines > c->vc_rows) /* maximum realistic size */
546 scr_memmovew(MDA_ADDR(0,t), MDA_ADDR(0,t+lines),
547 (b-t-lines)*mda_num_columns*2);
548 scr_memsetw(MDA_ADDR(0,b-lines), eattr,
549 lines*mda_num_columns*2);
553 scr_memmovew(MDA_ADDR(0,t+lines), MDA_ADDR(0,t),
554 (b-t-lines)*mda_num_columns*2);
555 scr_memsetw(MDA_ADDR(0,t), eattr, lines*mda_num_columns*2);
564 * The console `switch' structure for the MDA based console
567 static const struct consw mda_con = {
568 .owner = THIS_MODULE,
569 .con_startup = mdacon_startup,
570 .con_init = mdacon_init,
571 .con_deinit = mdacon_deinit,
572 .con_clear = mdacon_clear,
573 .con_putc = mdacon_putc,
574 .con_putcs = mdacon_putcs,
575 .con_cursor = mdacon_cursor,
576 .con_scroll = mdacon_scroll,
577 .con_bmove = mdacon_bmove,
578 .con_switch = mdacon_switch,
579 .con_blank = mdacon_blank,
580 .con_set_palette = mdacon_set_palette,
581 .con_scrolldelta = mdacon_scrolldelta,
582 .con_build_attr = mdacon_build_attr,
583 .con_invert_region = mdacon_invert_region,
586 int __init mda_console_init(void)
588 if (mda_first_vc > mda_last_vc)
591 return take_over_console(&mda_con, mda_first_vc-1, mda_last_vc-1, 0);
594 static void __exit mda_console_exit(void)
596 give_up_console(&mda_con);
599 module_init(mda_console_init);
600 module_exit(mda_console_exit);
602 MODULE_LICENSE("GPL");