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