mdacon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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/sched.h>
  30. #include <linux/fs.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/tty.h>
  34. #include <linux/console.h>
  35. #include <linux/string.h>
  36. #include <linux/kd.h>
  37. #include <linux/slab.h>
  38. #include <linux/vt_kern.h>
  39. #include <linux/vt_buffer.h>
  40. #include <linux/selection.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/ioport.h>
  43. #include <linux/delay.h>
  44. #include <linux/init.h>
  45. #include <asm/io.h>
  46. #include <asm/vga.h>
  47. static DEFINE_SPINLOCK(mda_lock);
  48. /* description of the hardware layout */
  49. static unsigned long mda_vram_base; /* Base of video memory */
  50. static unsigned long mda_vram_len; /* Size of video memory */
  51. static unsigned int mda_num_columns; /* Number of text columns */
  52. static unsigned int mda_num_lines; /* Number of text lines */
  53. static unsigned int mda_index_port; /* Register select port */
  54. static unsigned int mda_value_port; /* Register value port */
  55. static unsigned int mda_mode_port; /* Mode control port */
  56. static unsigned int mda_status_port; /* Status and Config port */
  57. static unsigned int mda_gfx_port; /* Graphics control port */
  58. /* current hardware state */
  59. static int mda_cursor_loc=-1;
  60. static int mda_cursor_size_from=-1;
  61. static int mda_cursor_size_to=-1;
  62. static enum { TYPE_MDA, TYPE_HERC, TYPE_HERCPLUS, TYPE_HERCCOLOR } mda_type;
  63. static char *mda_type_name;
  64. /* console information */
  65. static int mda_first_vc = 1;
  66. static int mda_last_vc = 16;
  67. static struct vc_data *mda_display_fg = NULL;
  68. module_param(mda_first_vc, int, 0);
  69. module_param(mda_last_vc, int, 0);
  70. /* MDA register values
  71. */
  72. #define MDA_CURSOR_BLINKING 0x00
  73. #define MDA_CURSOR_OFF 0x20
  74. #define MDA_CURSOR_SLOWBLINK 0x60
  75. #define MDA_MODE_GRAPHICS 0x02
  76. #define MDA_MODE_VIDEO_EN 0x08
  77. #define MDA_MODE_BLINK_EN 0x20
  78. #define MDA_MODE_GFX_PAGE1 0x80
  79. #define MDA_STATUS_HSYNC 0x01
  80. #define MDA_STATUS_VSYNC 0x80
  81. #define MDA_STATUS_VIDEO 0x08
  82. #define MDA_CONFIG_COL132 0x08
  83. #define MDA_GFX_MODE_EN 0x01
  84. #define MDA_GFX_PAGE_EN 0x02
  85. /*
  86. * MDA could easily be classified as "pre-dinosaur hardware".
  87. */
  88. static void write_mda_b(unsigned int val, unsigned char reg)
  89. {
  90. unsigned long flags;
  91. spin_lock_irqsave(&mda_lock, flags);
  92. outb_p(reg, mda_index_port);
  93. outb_p(val, mda_value_port);
  94. spin_unlock_irqrestore(&mda_lock, flags);
  95. }
  96. static void write_mda_w(unsigned int val, unsigned char reg)
  97. {
  98. unsigned long flags;
  99. spin_lock_irqsave(&mda_lock, flags);
  100. outb_p(reg, mda_index_port); outb_p(val >> 8, mda_value_port);
  101. outb_p(reg+1, mda_index_port); outb_p(val & 0xff, mda_value_port);
  102. spin_unlock_irqrestore(&mda_lock, flags);
  103. }
  104. #ifdef TEST_MDA_B
  105. static int test_mda_b(unsigned char val, unsigned char reg)
  106. {
  107. unsigned long flags;
  108. spin_lock_irqsave(&mda_lock, flags);
  109. outb_p(reg, mda_index_port);
  110. outb (val, mda_value_port);
  111. udelay(20); val = (inb_p(mda_value_port) == val);
  112. spin_unlock_irqrestore(&mda_lock, flags);
  113. return val;
  114. }
  115. #endif
  116. static inline void mda_set_cursor(unsigned int location)
  117. {
  118. if (mda_cursor_loc == location)
  119. return;
  120. write_mda_w(location >> 1, 0x0e);
  121. mda_cursor_loc = location;
  122. }
  123. static inline void mda_set_cursor_size(int from, int to)
  124. {
  125. if (mda_cursor_size_from==from && mda_cursor_size_to==to)
  126. return;
  127. if (from > to) {
  128. write_mda_b(MDA_CURSOR_OFF, 0x0a); /* disable cursor */
  129. } else {
  130. write_mda_b(from, 0x0a); /* cursor start */
  131. write_mda_b(to, 0x0b); /* cursor end */
  132. }
  133. mda_cursor_size_from = from;
  134. mda_cursor_size_to = to;
  135. }
  136. #ifndef MODULE
  137. static int __init mdacon_setup(char *str)
  138. {
  139. /* command line format: mdacon=<first>,<last> */
  140. int ints[3];
  141. str = get_options(str, ARRAY_SIZE(ints), ints);
  142. if (ints[0] < 2)
  143. return 0;
  144. if (ints[1] < 1 || ints[1] > MAX_NR_CONSOLES ||
  145. ints[2] < 1 || ints[2] > MAX_NR_CONSOLES)
  146. return 0;
  147. mda_first_vc = ints[1];
  148. mda_last_vc = ints[2];
  149. return 1;
  150. }
  151. __setup("mdacon=", mdacon_setup);
  152. #endif
  153. static int __init mda_detect(void)
  154. {
  155. int count=0;
  156. u16 *p, p_save;
  157. u16 *q, q_save;
  158. /* do a memory check */
  159. p = (u16 *) mda_vram_base;
  160. q = (u16 *) (mda_vram_base + 0x01000);
  161. p_save = scr_readw(p); q_save = scr_readw(q);
  162. scr_writew(0xAA55, p); if (scr_readw(p) == 0xAA55) count++;
  163. scr_writew(0x55AA, p); if (scr_readw(p) == 0x55AA) count++;
  164. scr_writew(p_save, p);
  165. if (count != 2) {
  166. return 0;
  167. }
  168. /* check if we have 4K or 8K */
  169. scr_writew(0xA55A, q); scr_writew(0x0000, p);
  170. if (scr_readw(q) == 0xA55A) count++;
  171. scr_writew(0x5AA5, q); scr_writew(0x0000, p);
  172. if (scr_readw(q) == 0x5AA5) count++;
  173. scr_writew(p_save, p); scr_writew(q_save, q);
  174. if (count == 4) {
  175. mda_vram_len = 0x02000;
  176. }
  177. /* Ok, there is definitely a card registering at the correct
  178. * memory location, so now we do an I/O port test.
  179. */
  180. #ifdef TEST_MDA_B
  181. /* Edward: These two mess `tests' mess up my cursor on bootup */
  182. /* cursor low register */
  183. if (! test_mda_b(0x66, 0x0f)) {
  184. return 0;
  185. }
  186. /* cursor low register */
  187. if (! test_mda_b(0x99, 0x0f)) {
  188. return 0;
  189. }
  190. #endif
  191. /* See if the card is a Hercules, by checking whether the vsync
  192. * bit of the status register is changing. This test lasts for
  193. * approximately 1/10th of a second.
  194. */
  195. p_save = q_save = inb_p(mda_status_port) & MDA_STATUS_VSYNC;
  196. for (count=0; count < 50000 && p_save == q_save; count++) {
  197. q_save = inb(mda_status_port) & MDA_STATUS_VSYNC;
  198. udelay(2);
  199. }
  200. if (p_save != q_save) {
  201. switch (inb_p(mda_status_port) & 0x70) {
  202. case 0x10:
  203. mda_type = TYPE_HERCPLUS;
  204. mda_type_name = "HerculesPlus";
  205. break;
  206. case 0x50:
  207. mda_type = TYPE_HERCCOLOR;
  208. mda_type_name = "HerculesColor";
  209. break;
  210. default:
  211. mda_type = TYPE_HERC;
  212. mda_type_name = "Hercules";
  213. break;
  214. }
  215. }
  216. return 1;
  217. }
  218. static void __init mda_initialize(void)
  219. {
  220. write_mda_b(97, 0x00); /* horizontal total */
  221. write_mda_b(80, 0x01); /* horizontal displayed */
  222. write_mda_b(82, 0x02); /* horizontal sync pos */
  223. write_mda_b(15, 0x03); /* horizontal sync width */
  224. write_mda_b(25, 0x04); /* vertical total */
  225. write_mda_b(6, 0x05); /* vertical total adjust */
  226. write_mda_b(25, 0x06); /* vertical displayed */
  227. write_mda_b(25, 0x07); /* vertical sync pos */
  228. write_mda_b(2, 0x08); /* interlace mode */
  229. write_mda_b(13, 0x09); /* maximum scanline */
  230. write_mda_b(12, 0x0a); /* cursor start */
  231. write_mda_b(13, 0x0b); /* cursor end */
  232. write_mda_w(0x0000, 0x0c); /* start address */
  233. write_mda_w(0x0000, 0x0e); /* cursor location */
  234. outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN, mda_mode_port);
  235. outb_p(0x00, mda_status_port);
  236. outb_p(0x00, mda_gfx_port);
  237. }
  238. static const char __init *mdacon_startup(void)
  239. {
  240. mda_num_columns = 80;
  241. mda_num_lines = 25;
  242. mda_vram_base = VGA_MAP_MEM(0xb0000);
  243. mda_vram_len = 0x01000;
  244. mda_index_port = 0x3b4;
  245. mda_value_port = 0x3b5;
  246. mda_mode_port = 0x3b8;
  247. mda_status_port = 0x3ba;
  248. mda_gfx_port = 0x3bf;
  249. mda_type = TYPE_MDA;
  250. mda_type_name = "MDA";
  251. if (! mda_detect()) {
  252. printk("mdacon: MDA card not detected.\n");
  253. return NULL;
  254. }
  255. if (mda_type != TYPE_MDA) {
  256. mda_initialize();
  257. }
  258. /* cursor looks ugly during boot-up, so turn it off */
  259. mda_set_cursor(mda_vram_len - 1);
  260. printk("mdacon: %s with %ldK of memory detected.\n",
  261. mda_type_name, mda_vram_len/1024);
  262. return "MDA-2";
  263. }
  264. static void mdacon_init(struct vc_data *c, int init)
  265. {
  266. c->vc_complement_mask = 0x0800; /* reverse video */
  267. c->vc_display_fg = &mda_display_fg;
  268. if (init) {
  269. c->vc_cols = mda_num_columns;
  270. c->vc_rows = mda_num_lines;
  271. } else
  272. vc_resize(c, mda_num_columns, mda_num_lines);
  273. /* make the first MDA console visible */
  274. if (mda_display_fg == NULL)
  275. mda_display_fg = c;
  276. }
  277. static void mdacon_deinit(struct vc_data *c)
  278. {
  279. /* con_set_default_unimap(c->vc_num); */
  280. if (mda_display_fg == c)
  281. mda_display_fg = NULL;
  282. }
  283. static inline u16 mda_convert_attr(u16 ch)
  284. {
  285. u16 attr = 0x0700;
  286. /* Underline and reverse-video are mutually exclusive on MDA.
  287. * Since reverse-video is used for cursors and selected areas,
  288. * it takes precedence.
  289. */
  290. if (ch & 0x0800) attr = 0x7000; /* reverse */
  291. else if (ch & 0x0400) attr = 0x0100; /* underline */
  292. return ((ch & 0x0200) << 2) | /* intensity */
  293. (ch & 0x8000) | /* blink */
  294. (ch & 0x00ff) | attr;
  295. }
  296. static u8 mdacon_build_attr(struct vc_data *c, u8 color, u8 intensity,
  297. u8 blink, u8 underline, u8 reverse)
  298. {
  299. /* The attribute is just a bit vector:
  300. *
  301. * Bit 0..1 : intensity (0..2)
  302. * Bit 2 : underline
  303. * Bit 3 : reverse
  304. * Bit 7 : blink
  305. */
  306. return (intensity & 3) |
  307. ((underline & 1) << 2) |
  308. ((reverse & 1) << 3) |
  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");