mdacon.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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 u16 *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 = mda_vram_base;
  159. q = mda_vram_base + 0x01000 / 2;
  160. p_save = scr_readw(p);
  161. q_save = scr_readw(q);
  162. scr_writew(0xAA55, p);
  163. if (scr_readw(p) == 0xAA55)
  164. count++;
  165. scr_writew(0x55AA, p);
  166. if (scr_readw(p) == 0x55AA)
  167. count++;
  168. scr_writew(p_save, p);
  169. if (count != 2) {
  170. return 0;
  171. }
  172. /* check if we have 4K or 8K */
  173. scr_writew(0xA55A, q);
  174. scr_writew(0x0000, p);
  175. if (scr_readw(q) == 0xA55A)
  176. count++;
  177. scr_writew(0x5AA5, q);
  178. scr_writew(0x0000, p);
  179. if (scr_readw(q) == 0x5AA5)
  180. count++;
  181. scr_writew(p_save, p);
  182. scr_writew(q_save, q);
  183. if (count == 4) {
  184. mda_vram_len = 0x02000;
  185. }
  186. /* Ok, there is definitely a card registering at the correct
  187. * memory location, so now we do an I/O port test.
  188. */
  189. #ifdef TEST_MDA_B
  190. /* Edward: These two mess `tests' mess up my cursor on bootup */
  191. /* cursor low register */
  192. if (!test_mda_b(0x66, 0x0f))
  193. return 0;
  194. /* cursor low register */
  195. if (!test_mda_b(0x99, 0x0f))
  196. return 0;
  197. #endif
  198. /* See if the card is a Hercules, by checking whether the vsync
  199. * bit of the status register is changing. This test lasts for
  200. * approximately 1/10th of a second.
  201. */
  202. p_save = q_save = inb_p(mda_status_port) & MDA_STATUS_VSYNC;
  203. for (count = 0; count < 50000 && p_save == q_save; count++) {
  204. q_save = inb(mda_status_port) & MDA_STATUS_VSYNC;
  205. udelay(2);
  206. }
  207. if (p_save != q_save) {
  208. switch (inb_p(mda_status_port) & 0x70) {
  209. case 0x10:
  210. mda_type = TYPE_HERCPLUS;
  211. mda_type_name = "HerculesPlus";
  212. break;
  213. case 0x50:
  214. mda_type = TYPE_HERCCOLOR;
  215. mda_type_name = "HerculesColor";
  216. break;
  217. default:
  218. mda_type = TYPE_HERC;
  219. mda_type_name = "Hercules";
  220. break;
  221. }
  222. }
  223. return 1;
  224. }
  225. static void mda_initialize(void)
  226. {
  227. write_mda_b(97, 0x00); /* horizontal total */
  228. write_mda_b(80, 0x01); /* horizontal displayed */
  229. write_mda_b(82, 0x02); /* horizontal sync pos */
  230. write_mda_b(15, 0x03); /* horizontal sync width */
  231. write_mda_b(25, 0x04); /* vertical total */
  232. write_mda_b(6, 0x05); /* vertical total adjust */
  233. write_mda_b(25, 0x06); /* vertical displayed */
  234. write_mda_b(25, 0x07); /* vertical sync pos */
  235. write_mda_b(2, 0x08); /* interlace mode */
  236. write_mda_b(13, 0x09); /* maximum scanline */
  237. write_mda_b(12, 0x0a); /* cursor start */
  238. write_mda_b(13, 0x0b); /* cursor end */
  239. write_mda_w(0x0000, 0x0c); /* start address */
  240. write_mda_w(0x0000, 0x0e); /* cursor location */
  241. outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN, mda_mode_port);
  242. outb_p(0x00, mda_status_port);
  243. outb_p(0x00, mda_gfx_port);
  244. }
  245. static const char *mdacon_startup(void)
  246. {
  247. mda_num_columns = 80;
  248. mda_num_lines = 25;
  249. mda_vram_len = 0x01000;
  250. mda_vram_base = (u16 *)VGA_MAP_MEM(0xb0000, mda_vram_len);
  251. mda_index_port = 0x3b4;
  252. mda_value_port = 0x3b5;
  253. mda_mode_port = 0x3b8;
  254. mda_status_port = 0x3ba;
  255. mda_gfx_port = 0x3bf;
  256. mda_type = TYPE_MDA;
  257. mda_type_name = "MDA";
  258. if (! mda_detect()) {
  259. printk("mdacon: MDA card not detected.\n");
  260. return NULL;
  261. }
  262. if (mda_type != TYPE_MDA) {
  263. mda_initialize();
  264. }
  265. /* cursor looks ugly during boot-up, so turn it off */
  266. mda_set_cursor(mda_vram_len - 1);
  267. printk("mdacon: %s with %ldK of memory detected.\n",
  268. mda_type_name, mda_vram_len/1024);
  269. return "MDA-2";
  270. }
  271. static void mdacon_init(struct vc_data *c, int init)
  272. {
  273. c->vc_complement_mask = 0x0800; /* reverse video */
  274. c->vc_display_fg = &mda_display_fg;
  275. if (init) {
  276. c->vc_cols = mda_num_columns;
  277. c->vc_rows = mda_num_lines;
  278. } else
  279. vc_resize(c, mda_num_columns, mda_num_lines);
  280. /* make the first MDA console visible */
  281. if (mda_display_fg == NULL)
  282. mda_display_fg = c;
  283. }
  284. static void mdacon_deinit(struct vc_data *c)
  285. {
  286. /* con_set_default_unimap(c->vc_num); */
  287. if (mda_display_fg == c)
  288. mda_display_fg = NULL;
  289. }
  290. static inline u16 mda_convert_attr(u16 ch)
  291. {
  292. u16 attr = 0x0700;
  293. /* Underline and reverse-video are mutually exclusive on MDA.
  294. * Since reverse-video is used for cursors and selected areas,
  295. * it takes precedence.
  296. */
  297. if (ch & 0x0800) attr = 0x7000; /* reverse */
  298. else if (ch & 0x0400) attr = 0x0100; /* underline */
  299. return ((ch & 0x0200) << 2) | /* intensity */
  300. (ch & 0x8000) | /* blink */
  301. (ch & 0x00ff) | attr;
  302. }
  303. static u8 mdacon_build_attr(struct vc_data *c, u8 color, u8 intensity,
  304. u8 blink, u8 underline, u8 reverse, u8 italic)
  305. {
  306. /* The attribute is just a bit vector:
  307. *
  308. * Bit 0..1 : intensity (0..2)
  309. * Bit 2 : underline
  310. * Bit 3 : reverse
  311. * Bit 7 : blink
  312. */
  313. return (intensity & 3) |
  314. ((underline & 1) << 2) |
  315. ((reverse & 1) << 3) |
  316. (!!italic << 4) |
  317. ((blink & 1) << 7);
  318. }
  319. static void mdacon_invert_region(struct vc_data *c, u16 *p, int count)
  320. {
  321. for (; count > 0; count--) {
  322. scr_writew(scr_readw(p) ^ 0x0800, p);
  323. p++;
  324. }
  325. }
  326. static inline u16 *mda_addr(unsigned int x, unsigned int y)
  327. {
  328. return mda_vram_base + y * mda_num_columns + x;
  329. }
  330. static void mdacon_putc(struct vc_data *c, int ch, int y, int x)
  331. {
  332. scr_writew(mda_convert_attr(ch), mda_addr(x, y));
  333. }
  334. static void mdacon_putcs(struct vc_data *c, const unsigned short *s,
  335. int count, int y, int x)
  336. {
  337. u16 *dest = mda_addr(x, y);
  338. for (; count > 0; count--) {
  339. scr_writew(mda_convert_attr(scr_readw(s++)), dest++);
  340. }
  341. }
  342. static void mdacon_clear(struct vc_data *c, int y, int x,
  343. int height, int width)
  344. {
  345. u16 *dest = mda_addr(x, y);
  346. u16 eattr = mda_convert_attr(c->vc_video_erase_char);
  347. if (width <= 0 || height <= 0)
  348. return;
  349. if (x==0 && width==mda_num_columns) {
  350. scr_memsetw(dest, eattr, height*width*2);
  351. } else {
  352. for (; height > 0; height--, dest+=mda_num_columns)
  353. scr_memsetw(dest, eattr, width*2);
  354. }
  355. }
  356. static int mdacon_switch(struct vc_data *c)
  357. {
  358. return 1; /* redrawing needed */
  359. }
  360. static int mdacon_blank(struct vc_data *c, int blank, int mode_switch)
  361. {
  362. if (mda_type == TYPE_MDA) {
  363. if (blank)
  364. scr_memsetw(mda_vram_base,
  365. mda_convert_attr(c->vc_video_erase_char),
  366. c->vc_screenbuf_size);
  367. /* Tell console.c that it has to restore the screen itself */
  368. return 1;
  369. } else {
  370. if (blank)
  371. outb_p(0x00, mda_mode_port); /* disable video */
  372. else
  373. outb_p(MDA_MODE_VIDEO_EN | MDA_MODE_BLINK_EN,
  374. mda_mode_port);
  375. return 0;
  376. }
  377. }
  378. static void mdacon_cursor(struct vc_data *c, int mode)
  379. {
  380. if (mode == CM_ERASE) {
  381. mda_set_cursor(mda_vram_len - 1);
  382. return;
  383. }
  384. mda_set_cursor(c->vc_y*mda_num_columns*2 + c->vc_x*2);
  385. switch (c->vc_cursor_type & 0x0f) {
  386. case CUR_LOWER_THIRD: mda_set_cursor_size(10, 13); break;
  387. case CUR_LOWER_HALF: mda_set_cursor_size(7, 13); break;
  388. case CUR_TWO_THIRDS: mda_set_cursor_size(4, 13); break;
  389. case CUR_BLOCK: mda_set_cursor_size(1, 13); break;
  390. case CUR_NONE: mda_set_cursor_size(14, 13); break;
  391. default: mda_set_cursor_size(12, 13); break;
  392. }
  393. }
  394. static bool mdacon_scroll(struct vc_data *c, unsigned int t, unsigned int b,
  395. enum con_scroll dir, unsigned int lines)
  396. {
  397. u16 eattr = mda_convert_attr(c->vc_video_erase_char);
  398. if (!lines)
  399. return false;
  400. if (lines > c->vc_rows) /* maximum realistic size */
  401. lines = c->vc_rows;
  402. switch (dir) {
  403. case SM_UP:
  404. scr_memmovew(mda_addr(0, t), mda_addr(0, t + lines),
  405. (b-t-lines)*mda_num_columns*2);
  406. scr_memsetw(mda_addr(0, b - lines), eattr,
  407. lines*mda_num_columns*2);
  408. break;
  409. case SM_DOWN:
  410. scr_memmovew(mda_addr(0, t + lines), mda_addr(0, t),
  411. (b-t-lines)*mda_num_columns*2);
  412. scr_memsetw(mda_addr(0, t), eattr, lines*mda_num_columns*2);
  413. break;
  414. }
  415. return false;
  416. }
  417. /*
  418. * The console `switch' structure for the MDA based console
  419. */
  420. static const struct consw mda_con = {
  421. .owner = THIS_MODULE,
  422. .con_startup = mdacon_startup,
  423. .con_init = mdacon_init,
  424. .con_deinit = mdacon_deinit,
  425. .con_clear = mdacon_clear,
  426. .con_putc = mdacon_putc,
  427. .con_putcs = mdacon_putcs,
  428. .con_cursor = mdacon_cursor,
  429. .con_scroll = mdacon_scroll,
  430. .con_switch = mdacon_switch,
  431. .con_blank = mdacon_blank,
  432. .con_build_attr = mdacon_build_attr,
  433. .con_invert_region = mdacon_invert_region,
  434. };
  435. int __init mda_console_init(void)
  436. {
  437. int err;
  438. if (mda_first_vc > mda_last_vc)
  439. return 1;
  440. console_lock();
  441. err = do_take_over_console(&mda_con, mda_first_vc-1, mda_last_vc-1, 0);
  442. console_unlock();
  443. return err;
  444. }
  445. static void __exit mda_console_exit(void)
  446. {
  447. give_up_console(&mda_con);
  448. }
  449. module_init(mda_console_init);
  450. module_exit(mda_console_exit);
  451. MODULE_LICENSE("GPL");