video-vesa.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /* -*- linux-c -*- ------------------------------------------------------- *
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. * Copyright 2007 rPath, Inc. - All Rights Reserved
  5. *
  6. * This file is part of the Linux kernel, and is made available under
  7. * the terms of the GNU General Public License version 2.
  8. *
  9. * ----------------------------------------------------------------------- */
  10. /*
  11. * arch/i386/boot/video-vesa.c
  12. *
  13. * VESA text modes
  14. */
  15. #include "boot.h"
  16. #include "video.h"
  17. #include "vesa.h"
  18. /* VESA information */
  19. static struct vesa_general_info vginfo;
  20. static struct vesa_mode_info vminfo;
  21. __videocard video_vesa;
  22. static void vesa_store_mode_params_graphics(void);
  23. static int vesa_probe(void)
  24. {
  25. #if defined(CONFIG_VIDEO_VESA) || defined(CONFIG_FIRMWARE_EDID)
  26. u16 ax;
  27. u16 mode;
  28. addr_t mode_ptr;
  29. struct mode_info *mi;
  30. int nmodes = 0;
  31. video_vesa.modes = GET_HEAP(struct mode_info, 0);
  32. vginfo.signature = VBE2_MAGIC;
  33. /* Optimistically assume a VESA BIOS is register-clean... */
  34. ax = 0x4f00;
  35. asm("int $0x10" : "+a" (ax), "=m" (vginfo) : "D" (&vginfo));
  36. if (ax != 0x004f ||
  37. vginfo.signature != VESA_MAGIC ||
  38. vginfo.version < 0x0102)
  39. return 0; /* Not present */
  40. #endif /* CONFIG_VIDEO_VESA || CONFIG_FIRMWARE_EDID */
  41. #ifdef CONFIG_VIDEO_VESA
  42. set_fs(vginfo.video_mode_ptr.seg);
  43. mode_ptr = vginfo.video_mode_ptr.off;
  44. while ((mode = rdfs16(mode_ptr)) != 0xffff) {
  45. mode_ptr += 2;
  46. if (heap_free() < sizeof(struct mode_info))
  47. break; /* Heap full, can't save mode info */
  48. if (mode & ~0x1ff)
  49. continue;
  50. memset(&vminfo, 0, sizeof vminfo); /* Just in case... */
  51. ax = 0x4f01;
  52. asm("int $0x10"
  53. : "+a" (ax), "=m" (vminfo)
  54. : "c" (mode), "D" (&vminfo));
  55. if (ax != 0x004f)
  56. continue;
  57. if ((vminfo.mode_attr & 0x15) == 0x05) {
  58. /* Text Mode, TTY BIOS supported,
  59. supported by hardware */
  60. mi = GET_HEAP(struct mode_info, 1);
  61. mi->mode = mode + VIDEO_FIRST_VESA;
  62. mi->x = vminfo.h_res;
  63. mi->y = vminfo.v_res;
  64. nmodes++;
  65. } else if ((vminfo.mode_attr & 0x99) == 0x99) {
  66. #ifdef CONFIG_FB
  67. /* Graphics mode, color, linear frame buffer
  68. supported -- register the mode but hide from
  69. the menu. Only do this if framebuffer is
  70. configured, however, otherwise the user will
  71. be left without a screen. */
  72. mi = GET_HEAP(struct mode_info, 1);
  73. mi->mode = mode + VIDEO_FIRST_VESA;
  74. mi->x = mi->y = 0;
  75. nmodes++;
  76. #endif
  77. }
  78. }
  79. return nmodes;
  80. #else
  81. return 0;
  82. #endif /* CONFIG_VIDEO_VESA */
  83. }
  84. static int vesa_set_mode(struct mode_info *mode)
  85. {
  86. u16 ax;
  87. int is_graphic;
  88. u16 vesa_mode = mode->mode - VIDEO_FIRST_VESA;
  89. memset(&vminfo, 0, sizeof vminfo); /* Just in case... */
  90. ax = 0x4f01;
  91. asm("int $0x10"
  92. : "+a" (ax), "=m" (vminfo)
  93. : "c" (vesa_mode), "D" (&vminfo));
  94. if (ax != 0x004f)
  95. return -1;
  96. if ((vminfo.mode_attr & 0x15) == 0x05) {
  97. /* It's a supported text mode */
  98. is_graphic = 0;
  99. } else if ((vminfo.mode_attr & 0x99) == 0x99) {
  100. /* It's a graphics mode with linear frame buffer */
  101. is_graphic = 1;
  102. vesa_mode |= 0x4000; /* Request linear frame buffer */
  103. } else {
  104. return -1; /* Invalid mode */
  105. }
  106. ax = 0x4f02;
  107. asm volatile("int $0x10"
  108. : "+a" (ax)
  109. : "b" (vesa_mode), "D" (0));
  110. if (ax != 0x004f)
  111. return -1;
  112. graphic_mode = is_graphic;
  113. if (!is_graphic) {
  114. /* Text mode */
  115. force_x = mode->x;
  116. force_y = mode->y;
  117. do_restore = 1;
  118. } else {
  119. /* Graphics mode */
  120. vesa_store_mode_params_graphics();
  121. }
  122. return 0;
  123. }
  124. /* Switch DAC to 8-bit mode */
  125. static void vesa_dac_set_8bits(void)
  126. {
  127. u8 dac_size = 6;
  128. /* If possible, switch the DAC to 8-bit mode */
  129. if (vginfo.capabilities & 1) {
  130. u16 ax, bx;
  131. ax = 0x4f08;
  132. bx = 0x0800;
  133. asm volatile(INT10
  134. : "+a" (ax), "+b" (bx)
  135. : : "ecx", "edx", "esi", "edi");
  136. if (ax == 0x004f)
  137. dac_size = bx >> 8;
  138. }
  139. /* Set the color sizes to the DAC size, and offsets to 0 */
  140. boot_params.screen_info.red_size = dac_size;
  141. boot_params.screen_info.green_size = dac_size;
  142. boot_params.screen_info.blue_size = dac_size;
  143. boot_params.screen_info.rsvd_size = dac_size;
  144. boot_params.screen_info.red_pos = 0;
  145. boot_params.screen_info.green_pos = 0;
  146. boot_params.screen_info.blue_pos = 0;
  147. boot_params.screen_info.rsvd_pos = 0;
  148. }
  149. /* Save the VESA protected mode info */
  150. static void vesa_store_pm_info(void)
  151. {
  152. u16 ax, bx, di, es;
  153. ax = 0x4f0a;
  154. bx = di = 0;
  155. asm("pushw %%es; "INT10"; movw %%es,%0; popw %%es"
  156. : "=d" (es), "+a" (ax), "+b" (bx), "+D" (di)
  157. : : "ecx", "esi");
  158. if (ax != 0x004f)
  159. return;
  160. boot_params.screen_info.vesapm_seg = es;
  161. boot_params.screen_info.vesapm_off = di;
  162. }
  163. /*
  164. * Save video mode parameters for graphics mode
  165. */
  166. static void vesa_store_mode_params_graphics(void)
  167. {
  168. /* Tell the kernel we're in VESA graphics mode */
  169. boot_params.screen_info.orig_video_isVGA = 0x23;
  170. /* Mode parameters */
  171. boot_params.screen_info.vesa_attributes = vminfo.mode_attr;
  172. boot_params.screen_info.lfb_linelength = vminfo.logical_scan;
  173. boot_params.screen_info.lfb_width = vminfo.h_res;
  174. boot_params.screen_info.lfb_height = vminfo.v_res;
  175. boot_params.screen_info.lfb_depth = vminfo.bpp;
  176. boot_params.screen_info.pages = vminfo.image_planes;
  177. boot_params.screen_info.lfb_base = vminfo.lfb_ptr;
  178. memcpy(&boot_params.screen_info.red_size,
  179. &vminfo.rmask, 8);
  180. /* General parameters */
  181. boot_params.screen_info.lfb_size = vginfo.total_memory;
  182. if (vminfo.bpp <= 8)
  183. vesa_dac_set_8bits();
  184. vesa_store_pm_info();
  185. }
  186. /*
  187. * Save EDID information for the kernel; this is invoked, separately,
  188. * after mode-setting.
  189. */
  190. void vesa_store_edid(void)
  191. {
  192. #ifdef CONFIG_FIRMWARE_EDID
  193. u16 ax, bx, cx, dx, di;
  194. /* Apparently used as a nonsense token... */
  195. memset(&boot_params.edid_info, 0x13, sizeof boot_params.edid_info);
  196. if (vginfo.version < 0x0200)
  197. return; /* EDID requires VBE 2.0+ */
  198. ax = 0x4f15; /* VBE DDC */
  199. bx = 0x0000; /* Report DDC capabilities */
  200. cx = 0; /* Controller 0 */
  201. di = 0; /* ES:DI must be 0 by spec */
  202. /* Note: The VBE DDC spec is different from the main VESA spec;
  203. we genuinely have to assume all registers are destroyed here. */
  204. asm("pushw %%es; movw %2,%%es; "INT10"; popw %%es"
  205. : "+a" (ax), "+b" (bx)
  206. : "c" (cx), "D" (di)
  207. : "esi");
  208. if (ax != 0x004f)
  209. return; /* No EDID */
  210. /* BH = time in seconds to transfer EDD information */
  211. /* BL = DDC level supported */
  212. ax = 0x4f15; /* VBE DDC */
  213. bx = 0x0001; /* Read EDID */
  214. cx = 0; /* Controller 0 */
  215. dx = 0; /* EDID block number */
  216. di =(size_t) &boot_params.edid_info; /* (ES:)Pointer to block */
  217. asm(INT10
  218. : "+a" (ax), "+b" (bx), "+d" (dx)
  219. : "c" (cx), "D" (di)
  220. : "esi");
  221. #endif /* CONFIG_FIRMWARE_EDID */
  222. }
  223. __videocard video_vesa =
  224. {
  225. .card_name = "VESA",
  226. .probe = vesa_probe,
  227. .set_mode = vesa_set_mode,
  228. .xmode_first = VIDEO_FIRST_VESA,
  229. .xmode_n = 0x200,
  230. };