vga.c 8.4 KB

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