mdacon.c 14 KB

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