vga.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /* VGA: VESA LFB (from boot) or legacy mode 13h / mode 03h. */
  2. #include "vga.h"
  3. #define VGA_FB ((volatile unsigned char *)0xA0000)
  4. /* Cached boot framebuffer info (set once when vga_fb_* first used). */
  5. static unsigned int vga_fb_w, vga_fb_h, vga_fb_pitch_val, vga_fb_bpp_val;
  6. static volatile unsigned char *vga_fb_lfb;
  7. static int vga_fb_cached;
  8. static volatile struct vga_boot_fb_info *vga_get_boot_fb(void)
  9. {
  10. volatile struct vga_boot_fb_info *p = VGA_BOOT_FB;
  11. __asm__ volatile ("" : "+r"(p));
  12. return p;
  13. }
  14. static void vga_cache_boot_fb(void)
  15. {
  16. volatile struct vga_boot_fb_info *info;
  17. if (vga_fb_cached)
  18. return;
  19. vga_fb_cached = 1;
  20. info = vga_get_boot_fb();
  21. if (!info->valid) {
  22. vga_fb_w = vga_fb_h = vga_fb_pitch_val = vga_fb_bpp_val = 0;
  23. vga_fb_lfb = 0;
  24. return;
  25. }
  26. vga_fb_w = info->width;
  27. vga_fb_h = info->height;
  28. vga_fb_pitch_val = info->pitch;
  29. vga_fb_bpp_val = info->bpp;
  30. /* Some BIOSes (e.g. 1024x768) report pitch in pixels or too small; enforce minimum. */
  31. {
  32. unsigned int min_pitch = vga_fb_w;
  33. if (vga_fb_bpp_val == 16)
  34. min_pitch *= 2;
  35. else if (vga_fb_bpp_val == 24)
  36. min_pitch *= 3;
  37. else if (vga_fb_bpp_val == 32)
  38. min_pitch *= 4;
  39. if (vga_fb_pitch_val < min_pitch)
  40. vga_fb_pitch_val = min_pitch;
  41. }
  42. vga_fb_lfb = (volatile unsigned char *)(unsigned long)info->lfb_base;
  43. }
  44. static inline void outb(unsigned short port, unsigned char v)
  45. {
  46. __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
  47. }
  48. static inline unsigned char inb(unsigned short port)
  49. {
  50. unsigned char v;
  51. __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
  52. return v;
  53. }
  54. /* ---- low-level register helpers ---- */
  55. static void write_seq(unsigned char idx, unsigned char val)
  56. {
  57. outb(0x3C4, idx);
  58. outb(0x3C5, val);
  59. }
  60. static void write_crtc(unsigned char idx, unsigned char val)
  61. {
  62. outb(0x3D4, idx);
  63. outb(0x3D5, val);
  64. }
  65. static void write_gc(unsigned char idx, unsigned char val)
  66. {
  67. outb(0x3CE, idx);
  68. outb(0x3CF, val);
  69. }
  70. static void crtc_unlock(void)
  71. {
  72. outb(0x3D4, 0x11);
  73. outb(0x3D5, inb(0x3D5) & 0x7F);
  74. }
  75. /* ---- register tables ---- */
  76. /* Mode 13h: 320x200, 256 colors, chain-4, framebuffer at 0xA0000 */
  77. static const unsigned char misc_13 = 0x63;
  78. static const unsigned char seq_13[] = { 0x03, 0x01, 0x0F, 0x00, 0x0E };
  79. static const unsigned char crtc_13[] = {
  80. 0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F,
  81. 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82. 0x9C, 0x0E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3, 0xFF
  83. };
  84. static const unsigned char gc_13[] = {
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F, 0xFF
  86. };
  87. static const unsigned char ac_13[] = {
  88. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  89. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  90. 0x41, 0x00, 0x0F, 0x00, 0x00
  91. };
  92. /* Mode 03h: 80x25, 16-color text, framebuffer at 0xB8000 */
  93. static const unsigned char misc_03 = 0x67;
  94. static const unsigned char seq_03[] = { 0x03, 0x00, 0x03, 0x00, 0x02 };
  95. static const unsigned char crtc_03[] = {
  96. 0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F,
  97. 0x00, 0x4F, 0x20, 0x0E, 0x00, 0x00, 0x00, 0x00,
  98. 0x9C, 0x0E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3, 0xFF
  99. };
  100. static const unsigned char gc_03[] = {
  101. 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x00, 0xFF
  102. };
  103. static const unsigned char ac_03[] = {
  104. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
  105. 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
  106. 0x0C, 0x00, 0x0F, 0x08, 0x00
  107. };
  108. /* ---- font buffer: 256 chars x 32 bytes/char ---- */
  109. #define FONT_SIZE 8192
  110. static unsigned char font_buf[FONT_SIZE];
  111. /* ---- set all VGA registers for a mode ---- */
  112. static void set_regs(unsigned char misc,
  113. const unsigned char *seq, const unsigned char *crtc,
  114. const unsigned char *gc, const unsigned char *ac)
  115. {
  116. unsigned int i;
  117. /* Misc Output */
  118. outb(0x3C2, misc);
  119. /* Sequencer: sync reset, write 1-4, release */
  120. write_seq(0, 0x01);
  121. for (i = 1; i < 5; i++)
  122. write_seq((unsigned char)i, seq[i]);
  123. write_seq(0, seq[0]);
  124. /* CRTC: unlock then write all 25 */
  125. crtc_unlock();
  126. for (i = 0; i < 25; i++)
  127. write_crtc((unsigned char)i, crtc[i]);
  128. /* Graphics Controller */
  129. for (i = 0; i < 9; i++)
  130. write_gc((unsigned char)i, gc[i]);
  131. /* Attribute Controller */
  132. (void)inb(0x3DA); /* reset flip-flop */
  133. for (i = 0; i < 21; i++) {
  134. outb(0x3C0, (unsigned char)i);
  135. outb(0x3C0, ac[i]);
  136. }
  137. /* Re-enable display (PAS bit) */
  138. (void)inb(0x3DA);
  139. outb(0x3C0, 0x20);
  140. }
  141. /* ---- font save / restore via plane 2 ---- */
  142. static void font_plane_access(void)
  143. {
  144. write_seq(0, 0x01); /* sync reset */
  145. write_seq(2, 0x04); /* map mask: plane 2 only */
  146. write_seq(4, 0x06); /* sequential access, extended memory */
  147. write_seq(0, 0x03); /* release reset */
  148. write_gc(4, 0x02); /* read map select: plane 2 */
  149. write_gc(5, 0x00); /* mode: read/write mode 0 */
  150. write_gc(6, 0x00); /* misc: A0000, 128K window */
  151. }
  152. static void text_plane_access(void)
  153. {
  154. write_seq(0, 0x01); /* sync reset */
  155. write_seq(2, 0x03); /* map mask: planes 0 + 1 */
  156. write_seq(4, 0x02); /* odd/even, extended memory */
  157. write_seq(0, 0x03); /* release reset */
  158. write_gc(4, 0x00); /* read map select: plane 0 */
  159. write_gc(5, 0x10); /* odd/even mode */
  160. write_gc(6, 0x0E); /* B8000, 32K window */
  161. }
  162. static void save_font(void)
  163. {
  164. unsigned int i;
  165. font_plane_access();
  166. for (i = 0; i < FONT_SIZE; i++)
  167. font_buf[i] = VGA_FB[i];
  168. }
  169. static void restore_font(void)
  170. {
  171. unsigned int i;
  172. font_plane_access();
  173. for (i = 0; i < FONT_SIZE; i++)
  174. VGA_FB[i] = font_buf[i];
  175. text_plane_access();
  176. }
  177. /* ---- public API ---- */
  178. void vga_set_mode_13(void)
  179. {
  180. save_font();
  181. set_regs(misc_13, seq_13, crtc_13, gc_13, ac_13);
  182. }
  183. void vga_set_mode_text(void)
  184. {
  185. set_regs(misc_03, seq_03, crtc_03, gc_03, ac_03);
  186. restore_font();
  187. }
  188. void vga_put_pixel(int x, int y, unsigned char color)
  189. {
  190. if (x >= 0 && x < VGA_GFX_WIDTH && y >= 0 && y < VGA_GFX_HEIGHT)
  191. VGA_FB[y * VGA_GFX_WIDTH + x] = color;
  192. }
  193. void vga_clear(unsigned char color)
  194. {
  195. unsigned int i;
  196. unsigned int n = VGA_GFX_WIDTH * VGA_GFX_HEIGHT;
  197. for (i = 0; i < n; i++)
  198. VGA_FB[i] = color;
  199. }
  200. /* ---- LFB API ---- */
  201. unsigned int vga_fb_width(void)
  202. {
  203. vga_cache_boot_fb();
  204. return vga_fb_w;
  205. }
  206. unsigned int vga_fb_height(void)
  207. {
  208. vga_cache_boot_fb();
  209. return vga_fb_h;
  210. }
  211. unsigned int vga_fb_pitch(void)
  212. {
  213. vga_cache_boot_fb();
  214. return vga_fb_pitch_val;
  215. }
  216. unsigned int vga_fb_bpp(void)
  217. {
  218. vga_cache_boot_fb();
  219. return vga_fb_bpp_val;
  220. }
  221. void *vga_fb_ptr(void)
  222. {
  223. vga_cache_boot_fb();
  224. return (void *)vga_fb_lfb;
  225. }
  226. int vga_has_vesa(void)
  227. {
  228. vga_cache_boot_fb();
  229. return (vga_fb_w != 0 && vga_fb_lfb != 0);
  230. }
  231. /* 16bpp: RGB 5:6:5. 32bpp: BGRX byte order (standard VBE / QEMU). */
  232. static void vga_lfb_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  233. {
  234. unsigned int pitch = vga_fb_pitch_val;
  235. unsigned int bpp = vga_fb_bpp_val;
  236. unsigned char *p;
  237. if (x >= vga_fb_w || y >= vga_fb_h || !vga_fb_lfb)
  238. return;
  239. p = (unsigned char *)vga_fb_lfb + (unsigned long)y * pitch;
  240. if (bpp == 16) {
  241. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  242. unsigned int off = x * 2;
  243. p[off] = (unsigned char)(rgb16 & 0xFF);
  244. p[off + 1] = (unsigned char)(rgb16 >> 8);
  245. } else if (bpp == 24) {
  246. unsigned int off = x * 3;
  247. p[off] = (unsigned char)b;
  248. p[off + 1] = (unsigned char)g;
  249. p[off + 2] = (unsigned char)r;
  250. } else if (bpp == 32) {
  251. unsigned int off = x * 4;
  252. p[off] = (unsigned char)b;
  253. p[off + 1] = (unsigned char)g;
  254. p[off + 2] = (unsigned char)r;
  255. p[off + 3] = 0;
  256. }
  257. }
  258. void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
  259. {
  260. vga_cache_boot_fb();
  261. if (!vga_fb_lfb)
  262. return;
  263. vga_lfb_put_pixel(x, y, r, g, b);
  264. }
  265. void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
  266. {
  267. unsigned int pitch = vga_fb_pitch_val;
  268. unsigned int bpp = vga_fb_bpp_val;
  269. vga_cache_boot_fb();
  270. if (!vga_fb_lfb)
  271. return;
  272. if (bpp == 16) {
  273. unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  274. unsigned short val = (unsigned short)((rgb16 & 0xFF) | ((rgb16 >> 8) << 8));
  275. volatile unsigned short *fb = (volatile unsigned short *)vga_fb_lfb;
  276. unsigned int n = (pitch / 2) * vga_fb_h;
  277. unsigned int i;
  278. for (i = 0; i < n; i++)
  279. fb[i] = val;
  280. } else if (bpp == 24) {
  281. volatile unsigned char *p = vga_fb_lfb;
  282. unsigned int row, x;
  283. for (row = 0; row < vga_fb_h; row++) {
  284. unsigned int row_off = row * pitch;
  285. for (x = 0; x < vga_fb_w; x++) {
  286. unsigned int off = row_off + x * 3;
  287. p[off] = (unsigned char)b;
  288. p[off + 1] = (unsigned char)g;
  289. p[off + 2] = (unsigned char)r;
  290. }
  291. }
  292. } else if (bpp == 32) {
  293. volatile unsigned int *fb = (volatile unsigned int *)vga_fb_lfb;
  294. unsigned int pixel = ((unsigned int)r << 16) | ((unsigned int)g << 8) | b;
  295. unsigned int stride = pitch / 4;
  296. unsigned int row, x;
  297. for (row = 0; row < vga_fb_h; row++)
  298. for (x = 0; x < vga_fb_w; x++)
  299. fb[row * stride + x] = pixel;
  300. }
  301. }