mdacon.c 14 KB

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