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