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