video-vga.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* -*- linux-c -*- ------------------------------------------------------- *
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright 2007 rPath, Inc. - All Rights Reserved
  6. * Copyright 2009 Intel Corporation; author H. Peter Anvin
  7. *
  8. * ----------------------------------------------------------------------- */
  9. /*
  10. * Common all-VGA modes
  11. */
  12. #include "boot.h"
  13. #include "video.h"
  14. static struct mode_info vga_modes[] = {
  15. { VIDEO_80x25, 80, 25, 0 },
  16. { VIDEO_8POINT, 80, 50, 0 },
  17. { VIDEO_80x43, 80, 43, 0 },
  18. { VIDEO_80x28, 80, 28, 0 },
  19. { VIDEO_80x30, 80, 30, 0 },
  20. { VIDEO_80x34, 80, 34, 0 },
  21. { VIDEO_80x60, 80, 60, 0 },
  22. };
  23. static struct mode_info ega_modes[] = {
  24. { VIDEO_80x25, 80, 25, 0 },
  25. { VIDEO_8POINT, 80, 43, 0 },
  26. };
  27. static struct mode_info cga_modes[] = {
  28. { VIDEO_80x25, 80, 25, 0 },
  29. };
  30. static __videocard video_vga;
  31. /* Set basic 80x25 mode */
  32. static u8 vga_set_basic_mode(void)
  33. {
  34. struct biosregs ireg, oreg;
  35. u8 mode;
  36. initregs(&ireg);
  37. /* Query current mode */
  38. ireg.ax = 0x0f00;
  39. intcall(0x10, &ireg, &oreg);
  40. mode = oreg.al;
  41. if (mode != 3 && mode != 7)
  42. mode = 3;
  43. /* Set the mode */
  44. ireg.ax = mode; /* AH=0: set mode */
  45. intcall(0x10, &ireg, NULL);
  46. do_restore = 1;
  47. return mode;
  48. }
  49. static void vga_set_8font(void)
  50. {
  51. /* Set 8x8 font - 80x43 on EGA, 80x50 on VGA */
  52. struct biosregs ireg;
  53. initregs(&ireg);
  54. /* Set 8x8 font */
  55. ireg.ax = 0x1112;
  56. /* ireg.bl = 0; */
  57. intcall(0x10, &ireg, NULL);
  58. /* Use alternate print screen */
  59. ireg.ax = 0x1200;
  60. ireg.bl = 0x20;
  61. intcall(0x10, &ireg, NULL);
  62. /* Turn off cursor emulation */
  63. ireg.ax = 0x1201;
  64. ireg.bl = 0x34;
  65. intcall(0x10, &ireg, NULL);
  66. /* Cursor is scan lines 6-7 */
  67. ireg.ax = 0x0100;
  68. ireg.cx = 0x0607;
  69. intcall(0x10, &ireg, NULL);
  70. }
  71. static void vga_set_14font(void)
  72. {
  73. /* Set 9x14 font - 80x28 on VGA */
  74. struct biosregs ireg;
  75. initregs(&ireg);
  76. /* Set 9x14 font */
  77. ireg.ax = 0x1111;
  78. /* ireg.bl = 0; */
  79. intcall(0x10, &ireg, NULL);
  80. /* Turn off cursor emulation */
  81. ireg.ax = 0x1201;
  82. ireg.bl = 0x34;
  83. intcall(0x10, &ireg, NULL);
  84. /* Cursor is scan lines 11-12 */
  85. ireg.ax = 0x0100;
  86. ireg.cx = 0x0b0c;
  87. intcall(0x10, &ireg, NULL);
  88. }
  89. static void vga_set_80x43(void)
  90. {
  91. /* Set 80x43 mode on VGA (not EGA) */
  92. struct biosregs ireg;
  93. initregs(&ireg);
  94. /* Set 350 scans */
  95. ireg.ax = 0x1201;
  96. ireg.bl = 0x30;
  97. intcall(0x10, &ireg, NULL);
  98. /* Reset video mode */
  99. ireg.ax = 0x0003;
  100. intcall(0x10, &ireg, NULL);
  101. vga_set_8font();
  102. }
  103. /* I/O address of the VGA CRTC */
  104. u16 vga_crtc(void)
  105. {
  106. return (inb(0x3cc) & 1) ? 0x3d4 : 0x3b4;
  107. }
  108. static void vga_set_480_scanlines(void)
  109. {
  110. u16 crtc; /* CRTC base address */
  111. u8 csel; /* CRTC miscellaneous output register */
  112. crtc = vga_crtc();
  113. out_idx(0x0c, crtc, 0x11); /* Vertical sync end, unlock CR0-7 */
  114. out_idx(0x0b, crtc, 0x06); /* Vertical total */
  115. out_idx(0x3e, crtc, 0x07); /* Vertical overflow */
  116. out_idx(0xea, crtc, 0x10); /* Vertical sync start */
  117. out_idx(0xdf, crtc, 0x12); /* Vertical display end */
  118. out_idx(0xe7, crtc, 0x15); /* Vertical blank start */
  119. out_idx(0x04, crtc, 0x16); /* Vertical blank end */
  120. csel = inb(0x3cc);
  121. csel &= 0x0d;
  122. csel |= 0xe2;
  123. outb(csel, 0x3c2);
  124. }
  125. static void vga_set_vertical_end(int lines)
  126. {
  127. u16 crtc; /* CRTC base address */
  128. u8 ovfw; /* CRTC overflow register */
  129. int end = lines-1;
  130. crtc = vga_crtc();
  131. ovfw = 0x3c | ((end >> (8-1)) & 0x02) | ((end >> (9-6)) & 0x40);
  132. out_idx(ovfw, crtc, 0x07); /* Vertical overflow */
  133. out_idx(end, crtc, 0x12); /* Vertical display end */
  134. }
  135. static void vga_set_80x30(void)
  136. {
  137. vga_set_480_scanlines();
  138. vga_set_vertical_end(30*16);
  139. }
  140. static void vga_set_80x34(void)
  141. {
  142. vga_set_480_scanlines();
  143. vga_set_14font();
  144. vga_set_vertical_end(34*14);
  145. }
  146. static void vga_set_80x60(void)
  147. {
  148. vga_set_480_scanlines();
  149. vga_set_8font();
  150. vga_set_vertical_end(60*8);
  151. }
  152. static int vga_set_mode(struct mode_info *mode)
  153. {
  154. /* Set the basic mode */
  155. vga_set_basic_mode();
  156. /* Override a possibly broken BIOS */
  157. force_x = mode->x;
  158. force_y = mode->y;
  159. switch (mode->mode) {
  160. case VIDEO_80x25:
  161. break;
  162. case VIDEO_8POINT:
  163. vga_set_8font();
  164. break;
  165. case VIDEO_80x43:
  166. vga_set_80x43();
  167. break;
  168. case VIDEO_80x28:
  169. vga_set_14font();
  170. break;
  171. case VIDEO_80x30:
  172. vga_set_80x30();
  173. break;
  174. case VIDEO_80x34:
  175. vga_set_80x34();
  176. break;
  177. case VIDEO_80x60:
  178. vga_set_80x60();
  179. break;
  180. }
  181. return 0;
  182. }
  183. /*
  184. * Note: this probe includes basic information required by all
  185. * systems. It should be executed first, by making sure
  186. * video-vga.c is listed first in the Makefile.
  187. */
  188. static int vga_probe(void)
  189. {
  190. static const char *card_name[] = {
  191. "CGA/MDA/HGC", "EGA", "VGA"
  192. };
  193. static struct mode_info *mode_lists[] = {
  194. cga_modes,
  195. ega_modes,
  196. vga_modes,
  197. };
  198. static int mode_count[] = {
  199. ARRAY_SIZE(cga_modes),
  200. ARRAY_SIZE(ega_modes),
  201. ARRAY_SIZE(vga_modes),
  202. };
  203. struct biosregs ireg, oreg;
  204. initregs(&ireg);
  205. ireg.ax = 0x1200;
  206. ireg.bl = 0x10; /* Check EGA/VGA */
  207. intcall(0x10, &ireg, &oreg);
  208. #ifndef _WAKEUP
  209. boot_params.screen_info.orig_video_ega_bx = oreg.bx;
  210. #endif
  211. /* If we have MDA/CGA/HGC then BL will be unchanged at 0x10 */
  212. if (oreg.bl != 0x10) {
  213. /* EGA/VGA */
  214. ireg.ax = 0x1a00;
  215. intcall(0x10, &ireg, &oreg);
  216. if (oreg.al == 0x1a) {
  217. adapter = ADAPTER_VGA;
  218. #ifndef _WAKEUP
  219. boot_params.screen_info.orig_video_isVGA = 1;
  220. #endif
  221. } else {
  222. adapter = ADAPTER_EGA;
  223. }
  224. } else {
  225. adapter = ADAPTER_CGA;
  226. }
  227. video_vga.modes = mode_lists[adapter];
  228. video_vga.card_name = card_name[adapter];
  229. return mode_count[adapter];
  230. }
  231. static __videocard video_vga = {
  232. .card_name = "VGA",
  233. .probe = vga_probe,
  234. .set_mode = vga_set_mode,
  235. };