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