xref: /linux/drivers/video/console/mdacon.c (revision 4f1bcaf094ccc512c23e10104c05a6f8e5b7a9e4)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/video/mdacon.c -- Low level MDA based console driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	(c) 1998 Andrew Apted <ajapted@netspace.net.au>
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *      including portions (c) 1995-1998 Patrick Caulfield.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *      slight improvements (c) 2000 Edward Betts <edward@debian.org>
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This file is based on the VGA console driver (vgacon.c):
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *	Created 28 Sep 1997 by Geert Uytterhoeven
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *	Rewritten by Martin Mares <mj@ucw.cz>, July 1998
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *  and on the old console.c, vga.c and vesa_blank.c drivers:
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *	Copyright (C) 1991, 1992  Linus Torvalds
191da177e4SLinus Torvalds  *			    1995  Jay Estabrook
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *  This file is subject to the terms and conditions of the GNU General Public
221da177e4SLinus Torvalds  *  License.  See the file COPYING in the main directory of this archive for
231da177e4SLinus Torvalds  *  more details.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *  Changelog:
261da177e4SLinus Torvalds  *  Paul G. (03/2001) Fix mdacon= boot prompt to use __setup().
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <linux/types.h>
301da177e4SLinus Torvalds #include <linux/sched.h>
311da177e4SLinus Torvalds #include <linux/fs.h>
321da177e4SLinus Torvalds #include <linux/kernel.h>
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/tty.h>
351da177e4SLinus Torvalds #include <linux/console.h>
361da177e4SLinus Torvalds #include <linux/string.h>
371da177e4SLinus Torvalds #include <linux/kd.h>
381da177e4SLinus Torvalds #include <linux/slab.h>
391da177e4SLinus Torvalds #include <linux/vt_kern.h>
401da177e4SLinus Torvalds #include <linux/vt_buffer.h>
411da177e4SLinus Torvalds #include <linux/selection.h>
421da177e4SLinus Torvalds #include <linux/spinlock.h>
431da177e4SLinus Torvalds #include <linux/ioport.h>
441da177e4SLinus Torvalds #include <linux/delay.h>
451da177e4SLinus Torvalds #include <linux/init.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include <asm/io.h>
481da177e4SLinus Torvalds #include <asm/vga.h>
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds static DEFINE_SPINLOCK(mda_lock);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /* description of the hardware layout */
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds static unsigned long	mda_vram_base;		/* Base of video memory */
551da177e4SLinus Torvalds static unsigned long	mda_vram_len;		/* Size of video memory */
561da177e4SLinus Torvalds static unsigned int	mda_num_columns;	/* Number of text columns */
571da177e4SLinus Torvalds static unsigned int	mda_num_lines;		/* Number of text lines */
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds static unsigned int	mda_index_port;		/* Register select port */
601da177e4SLinus Torvalds static unsigned int	mda_value_port;		/* Register value port */
611da177e4SLinus Torvalds static unsigned int	mda_mode_port;		/* Mode control port */
621da177e4SLinus Torvalds static unsigned int	mda_status_port;	/* Status and Config port */
631da177e4SLinus Torvalds static unsigned int	mda_gfx_port;		/* Graphics control port */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /* current hardware state */
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds static int	mda_cursor_loc=-1;
681da177e4SLinus Torvalds static int	mda_cursor_size_from=-1;
691da177e4SLinus Torvalds static int	mda_cursor_size_to=-1;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds static enum { TYPE_MDA, TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } mda_type;
721da177e4SLinus Torvalds static char *mda_type_name;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /* console information */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static int	mda_first_vc = 1;
771da177e4SLinus Torvalds static int	mda_last_vc  = 16;
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static struct vc_data	*mda_display_fg = NULL;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds module_param(mda_first_vc, int, 0);
821da177e4SLinus Torvalds module_param(mda_last_vc, int, 0);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* MDA register values
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds #define MDA_CURSOR_BLINKING	0x00
881da177e4SLinus Torvalds #define MDA_CURSOR_OFF		0x20
891da177e4SLinus Torvalds #define MDA_CURSOR_SLOWBLINK	0x60
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds #define MDA_MODE_GRAPHICS	0x02
921da177e4SLinus Torvalds #define MDA_MODE_VIDEO_EN	0x08
931da177e4SLinus Torvalds #define MDA_MODE_BLINK_EN	0x20
941da177e4SLinus Torvalds #define MDA_MODE_GFX_PAGE1	0x80
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds #define MDA_STATUS_HSYNC	0x01
971da177e4SLinus Torvalds #define MDA_STATUS_VSYNC	0x80
981da177e4SLinus Torvalds #define MDA_STATUS_VIDEO	0x08
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define MDA_CONFIG_COL132	0x08
1011da177e4SLinus Torvalds #define MDA_GFX_MODE_EN		0x01
1021da177e4SLinus Torvalds #define MDA_GFX_PAGE_EN		0x02
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * MDA could easily be classified as "pre-dinosaur hardware".
1071da177e4SLinus Torvalds  */
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds static void write_mda_b(unsigned int val, unsigned char reg)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds 	unsigned long flags;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	spin_lock_irqsave(&mda_lock, flags);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	outb_p(reg, mda_index_port);
1161da177e4SLinus Torvalds 	outb_p(val, mda_value_port);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	spin_unlock_irqrestore(&mda_lock, flags);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds static void write_mda_w(unsigned int val, unsigned char reg)
1221da177e4SLinus Torvalds {
1231da177e4SLinus Torvalds 	unsigned long flags;
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	spin_lock_irqsave(&mda_lock, flags);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	outb_p(reg,   mda_index_port); outb_p(val >> 8,   mda_value_port);
1281da177e4SLinus Torvalds 	outb_p(reg+1, mda_index_port); outb_p(val & 0xff, mda_value_port);
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	spin_unlock_irqrestore(&mda_lock, flags);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds #ifdef TEST_MDA_B
1341da177e4SLinus Torvalds static int test_mda_b(unsigned char val, unsigned char reg)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	unsigned long flags;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	spin_lock_irqsave(&mda_lock, flags);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	outb_p(reg, mda_index_port);
1411da177e4SLinus Torvalds 	outb  (val, mda_value_port);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	udelay(20); val = (inb_p(mda_value_port) == val);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&mda_lock, flags);
1461da177e4SLinus Torvalds 	return val;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds #endif
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static inline void mda_set_cursor(unsigned int location)
1511da177e4SLinus Torvalds {
1521da177e4SLinus Torvalds 	if (mda_cursor_loc == location)
1531da177e4SLinus Torvalds 		return;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	write_mda_w(location >> 1, 0x0e);
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	mda_cursor_loc = location;
1581da177e4SLinus Torvalds }
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds static inline void mda_set_cursor_size(int from, int to)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	if (mda_cursor_size_from==from && mda_cursor_size_to==to)
1631da177e4SLinus Torvalds 		return;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	if (from > to) {
1661da177e4SLinus Torvalds 		write_mda_b(MDA_CURSOR_OFF, 0x0a);	/* disable cursor */
1671da177e4SLinus Torvalds 	} else {
1681da177e4SLinus Torvalds 		write_mda_b(from, 0x0a);	/* cursor start */
1691da177e4SLinus Torvalds 		write_mda_b(to,   0x0b);	/* cursor end */
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	mda_cursor_size_from = from;
1731da177e4SLinus Torvalds 	mda_cursor_size_to   = to;
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds #ifndef MODULE
1781da177e4SLinus Torvalds static int __init mdacon_setup(char *str)
1791da177e4SLinus Torvalds {
1801da177e4SLinus Torvalds 	/* command line format: mdacon=<first>,<last> */
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds 	int ints[3];
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	str = get_options(str, ARRAY_SIZE(ints), ints);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	if (ints[0] < 2)
1871da177e4SLinus Torvalds 		return 0;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	if (ints[1] < 1 || ints[1] > MAX_NR_CONSOLES ||
1901da177e4SLinus Torvalds 	    ints[2] < 1 || ints[2] > MAX_NR_CONSOLES)
1911da177e4SLinus Torvalds 		return 0;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	mda_first_vc = ints[1];
1941da177e4SLinus Torvalds 	mda_last_vc  = ints[2];
1951da177e4SLinus Torvalds 	return 1;
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds __setup("mdacon=", mdacon_setup);
1991da177e4SLinus Torvalds #endif
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds static int __init mda_detect(void)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	int count=0;
2041da177e4SLinus Torvalds 	u16 *p, p_save;
2051da177e4SLinus Torvalds 	u16 *q, q_save;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* do a memory check */
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	p = (u16 *) mda_vram_base;
2101da177e4SLinus Torvalds 	q = (u16 *) (mda_vram_base + 0x01000);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	p_save = scr_readw(p); q_save = scr_readw(q);
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	scr_writew(0xAA55, p); if (scr_readw(p) == 0xAA55) count++;
2151da177e4SLinus Torvalds 	scr_writew(0x55AA, p); if (scr_readw(p) == 0x55AA) count++;
2161da177e4SLinus Torvalds 	scr_writew(p_save, p);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	if (count != 2) {
2191da177e4SLinus Torvalds 		return 0;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/* check if we have 4K or 8K */
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	scr_writew(0xA55A, q); scr_writew(0x0000, p);
2251da177e4SLinus Torvalds 	if (scr_readw(q) == 0xA55A) count++;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	scr_writew(0x5AA5, q); scr_writew(0x0000, p);
2281da177e4SLinus Torvalds 	if (scr_readw(q) == 0x5AA5) count++;
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	scr_writew(p_save, p); scr_writew(q_save, q);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	if (count == 4) {
2331da177e4SLinus Torvalds 		mda_vram_len = 0x02000;
2341da177e4SLinus Torvalds 	}
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	/* Ok, there is definitely a card registering at the correct
2371da177e4SLinus Torvalds 	 * memory location, so now we do an I/O port test.
2381da177e4SLinus Torvalds 	 */
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds #ifdef TEST_MDA_B
2411da177e4SLinus Torvalds 	/* Edward: These two mess `tests' mess up my cursor on bootup */
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/* cursor low register */
2441da177e4SLinus Torvalds 	if (! test_mda_b(0x66, 0x0f)) {
2451da177e4SLinus Torvalds 		return 0;
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	/* cursor low register */
2491da177e4SLinus Torvalds 	if (! test_mda_b(0x99, 0x0f)) {
2501da177e4SLinus Torvalds 		return 0;
2511da177e4SLinus Torvalds 	}
2521da177e4SLinus Torvalds #endif
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	/* See if the card is a Hercules, by checking whether the vsync
2551da177e4SLinus Torvalds 	 * bit of the status register is changing.  This test lasts for
2561da177e4SLinus Torvalds 	 * approximately 1/10th of a second.
2571da177e4SLinus Torvalds 	 */
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	p_save = q_save = inb_p(mda_status_port) & MDA_STATUS_VSYNC;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	for (count=0; count < 50000 && p_save == q_save; count++) {
2621da177e4SLinus Torvalds 		q_save = inb(mda_status_port) & MDA_STATUS_VSYNC;
2631da177e4SLinus Torvalds 		udelay(2);
2641da177e4SLinus Torvalds 	}
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	if (p_save != q_save) {
2671da177e4SLinus Torvalds 		switch (inb_p(mda_status_port) & 0x70) {
2681da177e4SLinus Torvalds 			case 0x10:
2691da177e4SLinus Torvalds 				mda_type = TYPE_HERCPLUS;
2701da177e4SLinus Torvalds 				mda_type_name = "HerculesPlus";
2711da177e4SLinus Torvalds 				break;
2721da177e4SLinus Torvalds 			case 0x50:
2731da177e4SLinus Torvalds 				mda_type = TYPE_HERCCOLOR;
2741da177e4SLinus Torvalds 				mda_type_name = "HerculesColor";
2751da177e4SLinus Torvalds 				break;
2761da177e4SLinus Torvalds 			default:
2771da177e4SLinus Torvalds 				mda_type = TYPE_HERC;
2781da177e4SLinus Torvalds 				mda_type_name = "Hercules";
2791da177e4SLinus Torvalds 				break;
2801da177e4SLinus Torvalds 		}
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	return 1;
2841da177e4SLinus Torvalds }
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds static void __init mda_initialize(void)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	write_mda_b(97, 0x00);		/* horizontal total */
2891da177e4SLinus Torvalds 	write_mda_b(80, 0x01);		/* horizontal displayed */
2901da177e4SLinus Torvalds 	write_mda_b(82, 0x02);		/* horizontal sync pos */
2911da177e4SLinus Torvalds 	write_mda_b(15, 0x03);		/* horizontal sync width */
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	write_mda_b(25, 0x04);		/* vertical total */
2941da177e4SLinus Torvalds 	write_mda_b(6,  0x05);		/* vertical total adjust */
2951da177e4SLinus Torvalds 	write_mda_b(25, 0x06);		/* vertical displayed */
2961da177e4SLinus Torvalds 	write_mda_b(25, 0x07);		/* vertical sync pos */
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	write_mda_b(2,  0x08);		/* interlace mode */
2991da177e4SLinus Torvalds 	write_mda_b(13, 0x09);		/* maximum scanline */
3001da177e4SLinus Torvalds 	write_mda_b(12, 0x0a);		/* cursor start */
3011da177e4SLinus Torvalds 	write_mda_b(13, 0x0b);		/* cursor end */
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	write_mda_w(0x0000, 0x0c);	/* start address */
3041da177e4SLinus Torvalds 	write_mda_w(0x0000, 0x0e);	/* cursor location */
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN, mda_mode_port);
3071da177e4SLinus Torvalds 	outb_p(0x00, mda_status_port);
3081da177e4SLinus Torvalds 	outb_p(0x00, mda_gfx_port);
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds static const char __init *mdacon_startup(void)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	mda_num_columns = 80;
3141da177e4SLinus Torvalds 	mda_num_lines   = 25;
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	mda_vram_len  = 0x01000;
317*4f1bcaf0SBjorn Helgaas 	mda_vram_base = VGA_MAP_MEM(0xb0000, mda_vram_len);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	mda_index_port  = 0x3b4;
3201da177e4SLinus Torvalds 	mda_value_port  = 0x3b5;
3211da177e4SLinus Torvalds 	mda_mode_port   = 0x3b8;
3221da177e4SLinus Torvalds 	mda_status_port = 0x3ba;
3231da177e4SLinus Torvalds 	mda_gfx_port    = 0x3bf;
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	mda_type = TYPE_MDA;
3261da177e4SLinus Torvalds 	mda_type_name = "MDA";
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	if (! mda_detect()) {
3291da177e4SLinus Torvalds 		printk("mdacon: MDA card not detected.\n");
3301da177e4SLinus Torvalds 		return NULL;
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	if (mda_type != TYPE_MDA) {
3341da177e4SLinus Torvalds 		mda_initialize();
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	/* cursor looks ugly during boot-up, so turn it off */
3381da177e4SLinus Torvalds 	mda_set_cursor(mda_vram_len - 1);
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	printk("mdacon: %s with %ldK of memory detected.\n",
3411da177e4SLinus Torvalds 		mda_type_name, mda_vram_len/1024);
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	return "MDA-2";
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds static void mdacon_init(struct vc_data *c, int init)
3471da177e4SLinus Torvalds {
3481da177e4SLinus Torvalds 	c->vc_complement_mask = 0x0800;	 /* reverse video */
3491da177e4SLinus Torvalds 	c->vc_display_fg = &mda_display_fg;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	if (init) {
3521da177e4SLinus Torvalds 		c->vc_cols = mda_num_columns;
3531da177e4SLinus Torvalds 		c->vc_rows = mda_num_lines;
3541da177e4SLinus Torvalds 	} else
3551da177e4SLinus Torvalds 		vc_resize(c, mda_num_columns, mda_num_lines);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	/* make the first MDA console visible */
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	if (mda_display_fg == NULL)
3601da177e4SLinus Torvalds 		mda_display_fg = c;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds static void mdacon_deinit(struct vc_data *c)
3641da177e4SLinus Torvalds {
3651da177e4SLinus Torvalds 	/* con_set_default_unimap(c->vc_num); */
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	if (mda_display_fg == c)
3681da177e4SLinus Torvalds 		mda_display_fg = NULL;
3691da177e4SLinus Torvalds }
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds static inline u16 mda_convert_attr(u16 ch)
3721da177e4SLinus Torvalds {
3731da177e4SLinus Torvalds 	u16 attr = 0x0700;
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 	/* Underline and reverse-video are mutually exclusive on MDA.
3761da177e4SLinus Torvalds 	 * Since reverse-video is used for cursors and selected areas,
3771da177e4SLinus Torvalds 	 * it takes precedence.
3781da177e4SLinus Torvalds 	 */
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	if (ch & 0x0800)	attr = 0x7000;	/* reverse */
3811da177e4SLinus Torvalds 	else if (ch & 0x0400)	attr = 0x0100;	/* underline */
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	return ((ch & 0x0200) << 2) | 		/* intensity */
3841da177e4SLinus Torvalds 		(ch & 0x8000) |			/* blink */
3851da177e4SLinus Torvalds 		(ch & 0x00ff) | attr;
3861da177e4SLinus Torvalds }
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds static u8 mdacon_build_attr(struct vc_data *c, u8 color, u8 intensity,
3891da177e4SLinus Torvalds 			    u8 blink, u8 underline, u8 reverse)
3901da177e4SLinus Torvalds {
3911da177e4SLinus Torvalds 	/* The attribute is just a bit vector:
3921da177e4SLinus Torvalds 	 *
3931da177e4SLinus Torvalds 	 *	Bit 0..1 : intensity (0..2)
3941da177e4SLinus Torvalds 	 *	Bit 2    : underline
3951da177e4SLinus Torvalds 	 *	Bit 3    : reverse
3961da177e4SLinus Torvalds 	 *	Bit 7    : blink
3971da177e4SLinus Torvalds 	 */
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	return (intensity & 3) |
4001da177e4SLinus Torvalds 		((underline & 1) << 2) |
4011da177e4SLinus Torvalds 		((reverse   & 1) << 3) |
4021da177e4SLinus Torvalds 		((blink     & 1) << 7);
4031da177e4SLinus Torvalds }
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds static void mdacon_invert_region(struct vc_data *c, u16 *p, int count)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	for (; count > 0; count--) {
4081da177e4SLinus Torvalds 		scr_writew(scr_readw(p) ^ 0x0800, p);
4091da177e4SLinus Torvalds 		p++;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds #define MDA_ADDR(x,y)  ((u16 *) mda_vram_base + (y)*mda_num_columns + (x))
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static void mdacon_putc(struct vc_data *c, int ch, int y, int x)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	scr_writew(mda_convert_attr(ch), MDA_ADDR(x, y));
4181da177e4SLinus Torvalds }
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds static void mdacon_putcs(struct vc_data *c, const unsigned short *s,
4211da177e4SLinus Torvalds 		         int count, int y, int x)
4221da177e4SLinus Torvalds {
4231da177e4SLinus Torvalds 	u16 *dest = MDA_ADDR(x, y);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	for (; count > 0; count--) {
4261da177e4SLinus Torvalds 		scr_writew(mda_convert_attr(scr_readw(s++)), dest++);
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds static void mdacon_clear(struct vc_data *c, int y, int x,
4311da177e4SLinus Torvalds 			  int height, int width)
4321da177e4SLinus Torvalds {
4331da177e4SLinus Torvalds 	u16 *dest = MDA_ADDR(x, y);
4341da177e4SLinus Torvalds 	u16 eattr = mda_convert_attr(c->vc_video_erase_char);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	if (width <= 0 || height <= 0)
4371da177e4SLinus Torvalds 		return;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	if (x==0 && width==mda_num_columns) {
4401da177e4SLinus Torvalds 		scr_memsetw(dest, eattr, height*width*2);
4411da177e4SLinus Torvalds 	} else {
4421da177e4SLinus Torvalds 		for (; height > 0; height--, dest+=mda_num_columns)
4431da177e4SLinus Torvalds 			scr_memsetw(dest, eattr, width*2);
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds static void mdacon_bmove(struct vc_data *c, int sy, int sx,
4481da177e4SLinus Torvalds 			 int dy, int dx, int height, int width)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	u16 *src, *dest;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	if (width <= 0 || height <= 0)
4531da177e4SLinus Torvalds 		return;
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	if (sx==0 && dx==0 && width==mda_num_columns) {
4561da177e4SLinus Torvalds 		scr_memmovew(MDA_ADDR(0,dy), MDA_ADDR(0,sy), height*width*2);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	} else if (dy < sy || (dy == sy && dx < sx)) {
4591da177e4SLinus Torvalds 		src  = MDA_ADDR(sx, sy);
4601da177e4SLinus Torvalds 		dest = MDA_ADDR(dx, dy);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 		for (; height > 0; height--) {
4631da177e4SLinus Torvalds 			scr_memmovew(dest, src, width*2);
4641da177e4SLinus Torvalds 			src  += mda_num_columns;
4651da177e4SLinus Torvalds 			dest += mda_num_columns;
4661da177e4SLinus Torvalds 		}
4671da177e4SLinus Torvalds 	} else {
4681da177e4SLinus Torvalds 		src  = MDA_ADDR(sx, sy+height-1);
4691da177e4SLinus Torvalds 		dest = MDA_ADDR(dx, dy+height-1);
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 		for (; height > 0; height--) {
4721da177e4SLinus Torvalds 			scr_memmovew(dest, src, width*2);
4731da177e4SLinus Torvalds 			src  -= mda_num_columns;
4741da177e4SLinus Torvalds 			dest -= mda_num_columns;
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds static int mdacon_switch(struct vc_data *c)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	return 1;	/* redrawing needed */
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds static int mdacon_set_palette(struct vc_data *c, unsigned char *table)
4851da177e4SLinus Torvalds {
4861da177e4SLinus Torvalds 	return -EINVAL;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds static int mdacon_blank(struct vc_data *c, int blank, int mode_switch)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	if (mda_type == TYPE_MDA) {
4921da177e4SLinus Torvalds 		if (blank)
4931da177e4SLinus Torvalds 			scr_memsetw((void *)mda_vram_base,
4941da177e4SLinus Torvalds 				mda_convert_attr(c->vc_video_erase_char),
4951da177e4SLinus Torvalds 				c->vc_screenbuf_size);
4961da177e4SLinus Torvalds 		/* Tell console.c that it has to restore the screen itself */
4971da177e4SLinus Torvalds 		return 1;
4981da177e4SLinus Torvalds 	} else {
4991da177e4SLinus Torvalds 		if (blank)
5001da177e4SLinus Torvalds 			outb_p(0x00, mda_mode_port);	/* disable video */
5011da177e4SLinus Torvalds 		else
5021da177e4SLinus Torvalds 			outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN,
5031da177e4SLinus Torvalds 				mda_mode_port);
5041da177e4SLinus Torvalds 		return 0;
5051da177e4SLinus Torvalds 	}
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds static int mdacon_scrolldelta(struct vc_data *c, int lines)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds 	return 0;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static void mdacon_cursor(struct vc_data *c, int mode)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	if (mode == CM_ERASE) {
5161da177e4SLinus Torvalds 		mda_set_cursor(mda_vram_len - 1);
5171da177e4SLinus Torvalds 		return;
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	mda_set_cursor(c->vc_y*mda_num_columns*2 + c->vc_x*2);
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	switch (c->vc_cursor_type & 0x0f) {
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 		case CUR_LOWER_THIRD:	mda_set_cursor_size(10, 13); break;
5251da177e4SLinus Torvalds 		case CUR_LOWER_HALF:	mda_set_cursor_size(7,  13); break;
5261da177e4SLinus Torvalds 		case CUR_TWO_THIRDS:	mda_set_cursor_size(4,  13); break;
5271da177e4SLinus Torvalds 		case CUR_BLOCK:		mda_set_cursor_size(1,  13); break;
5281da177e4SLinus Torvalds 		case CUR_NONE:		mda_set_cursor_size(14, 13); break;
5291da177e4SLinus Torvalds 		default:		mda_set_cursor_size(12, 13); break;
5301da177e4SLinus Torvalds 	}
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static int mdacon_scroll(struct vc_data *c, int t, int b, int dir, int lines)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds 	u16 eattr = mda_convert_attr(c->vc_video_erase_char);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	if (!lines)
5381da177e4SLinus Torvalds 		return 0;
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds 	if (lines > c->vc_rows)   /* maximum realistic size */
5411da177e4SLinus Torvalds 		lines = c->vc_rows;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	switch (dir) {
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	case SM_UP:
5461da177e4SLinus Torvalds 		scr_memmovew(MDA_ADDR(0,t), MDA_ADDR(0,t+lines),
5471da177e4SLinus Torvalds 				(b-t-lines)*mda_num_columns*2);
5481da177e4SLinus Torvalds 		scr_memsetw(MDA_ADDR(0,b-lines), eattr,
5491da177e4SLinus Torvalds 				lines*mda_num_columns*2);
5501da177e4SLinus Torvalds 		break;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	case SM_DOWN:
5531da177e4SLinus Torvalds 		scr_memmovew(MDA_ADDR(0,t+lines), MDA_ADDR(0,t),
5541da177e4SLinus Torvalds 				(b-t-lines)*mda_num_columns*2);
5551da177e4SLinus Torvalds 		scr_memsetw(MDA_ADDR(0,t), eattr, lines*mda_num_columns*2);
5561da177e4SLinus Torvalds 		break;
5571da177e4SLinus Torvalds 	}
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	return 0;
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds /*
5641da177e4SLinus Torvalds  *  The console `switch' structure for the MDA based console
5651da177e4SLinus Torvalds  */
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds static const struct consw mda_con = {
5681da177e4SLinus Torvalds 	.owner =		THIS_MODULE,
5691da177e4SLinus Torvalds 	.con_startup =		mdacon_startup,
5701da177e4SLinus Torvalds 	.con_init =		mdacon_init,
5711da177e4SLinus Torvalds 	.con_deinit =		mdacon_deinit,
5721da177e4SLinus Torvalds 	.con_clear =		mdacon_clear,
5731da177e4SLinus Torvalds 	.con_putc =		mdacon_putc,
5741da177e4SLinus Torvalds 	.con_putcs =		mdacon_putcs,
5751da177e4SLinus Torvalds 	.con_cursor =		mdacon_cursor,
5761da177e4SLinus Torvalds 	.con_scroll =		mdacon_scroll,
5771da177e4SLinus Torvalds 	.con_bmove =		mdacon_bmove,
5781da177e4SLinus Torvalds 	.con_switch =		mdacon_switch,
5791da177e4SLinus Torvalds 	.con_blank =		mdacon_blank,
5801da177e4SLinus Torvalds 	.con_set_palette =	mdacon_set_palette,
5811da177e4SLinus Torvalds 	.con_scrolldelta =	mdacon_scrolldelta,
5821da177e4SLinus Torvalds 	.con_build_attr =	mdacon_build_attr,
5831da177e4SLinus Torvalds 	.con_invert_region =	mdacon_invert_region,
5841da177e4SLinus Torvalds };
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds int __init mda_console_init(void)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	if (mda_first_vc > mda_last_vc)
5891da177e4SLinus Torvalds 		return 1;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	return take_over_console(&mda_con, mda_first_vc-1, mda_last_vc-1, 0);
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds static void __exit mda_console_exit(void)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	give_up_console(&mda_con);
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds module_init(mda_console_init);
6001da177e4SLinus Torvalds module_exit(mda_console_exit);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6031da177e4SLinus Torvalds 
604