Kevin O'Connor | 3471fdb | 2012-01-14 19:02:43 -0500 | [diff] [blame] | 1 | // Standard VGA driver code |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 2 | // |
| 3 | // Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2001-2008 the LGPL VGABios developers Team |
| 5 | // |
| 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
| 7 | |
Kevin O'Connor | c990f27 | 2011-12-31 16:00:54 -0500 | [diff] [blame] | 8 | #include "biosvar.h" // GET_GLOBAL |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 9 | #include "farptr.h" // SET_FARVAR |
| 10 | #include "stdvga.h" // stdvga_setup |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 11 | #include "string.h" // memset_far |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 12 | #include "vgabios.h" // struct vgamode_s |
Kevin O'Connor | c682ffe | 2016-08-05 11:48:20 -0400 | [diff] [blame] | 13 | #include "vgautil.h" // stdvga_attr_write |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 14 | #include "x86.h" // outb |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 15 | |
| 16 | |
| 17 | /**************************************************************** |
| 18 | * Attribute control |
| 19 | ****************************************************************/ |
| 20 | |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 21 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 22 | stdvga_set_border_color(u8 color) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 23 | { |
Kevin O'Connor | 8bc059e | 2009-05-17 21:19:36 -0400 | [diff] [blame] | 24 | u8 v1 = color & 0x0f; |
| 25 | if (v1 & 0x08) |
| 26 | v1 += 0x08; |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 27 | stdvga_attr_write(0x00, v1); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 28 | |
| 29 | int i; |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 30 | for (i = 1; i < 4; i++) |
| 31 | stdvga_attr_mask(i, 0x10, color & 0x10); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 35 | stdvga_set_overscan_border_color(u8 color) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 36 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 37 | stdvga_attr_write(0x11, color); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Kevin O'Connor | 8bc059e | 2009-05-17 21:19:36 -0400 | [diff] [blame] | 40 | u8 |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 41 | stdvga_get_overscan_border_color(void) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 42 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 43 | return stdvga_attr_read(0x11); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 47 | stdvga_set_palette(u8 palid) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 48 | { |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 49 | int i; |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 50 | for (i = 1; i < 4; i++) |
| 51 | stdvga_attr_mask(i, 0x01, palid & 0x01); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 55 | stdvga_set_all_palette_reg(u16 seg, u8 *data_far) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 56 | { |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 57 | int i; |
| 58 | for (i = 0; i < 0x10; i++) { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 59 | stdvga_attr_write(i, GET_FARVAR(seg, *data_far)); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 60 | data_far++; |
| 61 | } |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 62 | stdvga_attr_write(0x11, GET_FARVAR(seg, *data_far)); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 66 | stdvga_get_all_palette_reg(u16 seg, u8 *data_far) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 67 | { |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 68 | int i; |
| 69 | for (i = 0; i < 0x10; i++) { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 70 | SET_FARVAR(seg, *data_far, stdvga_attr_read(i)); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 71 | data_far++; |
| 72 | } |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 73 | SET_FARVAR(seg, *data_far, stdvga_attr_read(0x11)); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 77 | stdvga_toggle_intensity(u8 flag) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 78 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 79 | stdvga_attr_mask(0x10, 0x08, (flag & 0x01) << 3); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 83 | stdvga_select_video_dac_color_page(u8 flag, u8 data) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 84 | { |
Kevin O'Connor | 8bc059e | 2009-05-17 21:19:36 -0400 | [diff] [blame] | 85 | if (!(flag & 0x01)) { |
| 86 | // select paging mode |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 87 | stdvga_attr_mask(0x10, 0x80, data << 7); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 88 | return; |
| 89 | } |
Kevin O'Connor | 8bc059e | 2009-05-17 21:19:36 -0400 | [diff] [blame] | 90 | // select page |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 91 | u8 val = stdvga_attr_read(0x10); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 92 | if (!(val & 0x80)) |
Kevin O'Connor | 8bc059e | 2009-05-17 21:19:36 -0400 | [diff] [blame] | 93 | data <<= 2; |
| 94 | data &= 0x0f; |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 95 | stdvga_attr_write(0x14, data); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 99 | stdvga_read_video_dac_state(u8 *pmode, u8 *curpage) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 100 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 101 | u8 val1 = stdvga_attr_read(0x10) >> 7; |
| 102 | u8 val2 = stdvga_attr_read(0x14) & 0x0f; |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 103 | if (!(val1 & 0x01)) |
| 104 | val2 >>= 2; |
Kevin O'Connor | 8bc059e | 2009-05-17 21:19:36 -0400 | [diff] [blame] | 105 | *pmode = val1; |
| 106 | *curpage = val2; |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
| 110 | /**************************************************************** |
| 111 | * DAC control |
| 112 | ****************************************************************/ |
| 113 | |
| 114 | void |
Kevin O'Connor | 821d6b4 | 2011-12-31 18:19:22 -0500 | [diff] [blame] | 115 | stdvga_perform_gray_scale_summing(u16 start, u16 count) |
| 116 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 117 | stdvga_attrindex_write(0x00); |
Kevin O'Connor | 821d6b4 | 2011-12-31 18:19:22 -0500 | [diff] [blame] | 118 | int i; |
| 119 | for (i = start; i < start+count; i++) { |
| 120 | u8 rgb[3]; |
Kevin O'Connor | 3471fdb | 2012-01-14 19:02:43 -0500 | [diff] [blame] | 121 | stdvga_dac_read(GET_SEG(SS), rgb, i, 1); |
Kevin O'Connor | 821d6b4 | 2011-12-31 18:19:22 -0500 | [diff] [blame] | 122 | |
| 123 | // intensity = ( 0.3 * Red ) + ( 0.59 * Green ) + ( 0.11 * Blue ) |
| 124 | u16 intensity = ((77 * rgb[0] + 151 * rgb[1] + 28 * rgb[2]) + 0x80) >> 8; |
| 125 | if (intensity > 0x3f) |
| 126 | intensity = 0x3f; |
Kevin O'Connor | 9cba2b3 | 2013-03-09 13:00:40 -0500 | [diff] [blame] | 127 | rgb[0] = rgb[1] = rgb[2] = intensity; |
Kevin O'Connor | 821d6b4 | 2011-12-31 18:19:22 -0500 | [diff] [blame] | 128 | |
Kevin O'Connor | 3471fdb | 2012-01-14 19:02:43 -0500 | [diff] [blame] | 129 | stdvga_dac_write(GET_SEG(SS), rgb, i, 1); |
Kevin O'Connor | 821d6b4 | 2011-12-31 18:19:22 -0500 | [diff] [blame] | 130 | } |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 131 | stdvga_attrindex_write(0x20); |
Kevin O'Connor | 821d6b4 | 2011-12-31 18:19:22 -0500 | [diff] [blame] | 132 | } |
| 133 | |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 134 | |
| 135 | /**************************************************************** |
| 136 | * Memory control |
| 137 | ****************************************************************/ |
| 138 | |
| 139 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 140 | stdvga_set_text_block_specifier(u8 spec) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 141 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 142 | stdvga_sequ_write(0x03, spec); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Kevin O'Connor | 160d34a | 2012-01-16 18:48:26 -0500 | [diff] [blame] | 145 | // Enable reads and writes to the given "plane" when in planar4 mode. |
| 146 | void |
| 147 | stdvga_planar4_plane(int plane) |
| 148 | { |
| 149 | if (plane < 0) { |
| 150 | // Return to default mode (read plane0, write all planes) |
| 151 | stdvga_sequ_write(0x02, 0x0f); |
| 152 | stdvga_grdc_write(0x04, 0); |
| 153 | } else { |
| 154 | stdvga_sequ_write(0x02, 1<<plane); |
| 155 | stdvga_grdc_write(0x04, plane); |
| 156 | } |
| 157 | } |
| 158 | |
Kevin O'Connor | 2bec7d6 | 2011-12-31 04:31:16 -0500 | [diff] [blame] | 159 | |
| 160 | /**************************************************************** |
| 161 | * Font loading |
| 162 | ****************************************************************/ |
| 163 | |
| 164 | static void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 165 | get_font_access(void) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 166 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 167 | stdvga_sequ_write(0x00, 0x01); |
| 168 | stdvga_sequ_write(0x02, 0x04); |
| 169 | stdvga_sequ_write(0x04, 0x07); |
| 170 | stdvga_sequ_write(0x00, 0x03); |
| 171 | stdvga_grdc_write(0x04, 0x02); |
| 172 | stdvga_grdc_write(0x05, 0x00); |
| 173 | stdvga_grdc_write(0x06, 0x04); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Kevin O'Connor | 2bec7d6 | 2011-12-31 04:31:16 -0500 | [diff] [blame] | 176 | static void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 177 | release_font_access(void) |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 178 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 179 | stdvga_sequ_write(0x00, 0x01); |
| 180 | stdvga_sequ_write(0x02, 0x03); |
| 181 | stdvga_sequ_write(0x04, 0x03); |
| 182 | stdvga_sequ_write(0x00, 0x03); |
| 183 | u16 v = (stdvga_misc_read() & 0x01) ? 0x0e : 0x0a; |
| 184 | stdvga_grdc_write(0x06, v); |
| 185 | stdvga_grdc_write(0x04, 0x00); |
| 186 | stdvga_grdc_write(0x05, 0x10); |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Kevin O'Connor | 2bec7d6 | 2011-12-31 04:31:16 -0500 | [diff] [blame] | 189 | void |
| 190 | stdvga_load_font(u16 seg, void *src_far, u16 count |
| 191 | , u16 start, u8 destflags, u8 fontsize) |
| 192 | { |
| 193 | get_font_access(); |
| 194 | u16 blockaddr = ((destflags & 0x03) << 14) + ((destflags & 0x04) << 11); |
| 195 | void *dest_far = (void*)(blockaddr + start*32); |
| 196 | u16 i; |
| 197 | for (i = 0; i < count; i++) |
| 198 | memcpy_far(SEG_GRAPH, dest_far + i*32 |
| 199 | , seg, src_far + i*fontsize, fontsize); |
| 200 | release_font_access(); |
| 201 | } |
| 202 | |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 203 | |
| 204 | /**************************************************************** |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 205 | * CRTC registers |
| 206 | ****************************************************************/ |
| 207 | |
Kevin O'Connor | c990f27 | 2011-12-31 16:00:54 -0500 | [diff] [blame] | 208 | u16 |
| 209 | stdvga_get_crtc(void) |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 210 | { |
Kevin O'Connor | 3471fdb | 2012-01-14 19:02:43 -0500 | [diff] [blame] | 211 | if (stdvga_misc_read() & 1) |
Kevin O'Connor | c990f27 | 2011-12-31 16:00:54 -0500 | [diff] [blame] | 212 | return VGAREG_VGA_CRTC_ADDRESS; |
| 213 | return VGAREG_MDA_CRTC_ADDRESS; |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 216 | // Ratio between system visible framebuffer ram and the actual videoram used. |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 217 | int |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 218 | stdvga_vram_ratio(struct vgamode_s *vmode_g) |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 219 | { |
| 220 | switch (GET_GLOBAL(vmode_g->memmodel)) { |
| 221 | case MM_TEXT: |
| 222 | return 2; |
| 223 | case MM_CGA: |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 224 | return 4 / GET_GLOBAL(vmode_g->depth); |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 225 | case MM_PLANAR: |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 226 | return 4; |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 227 | default: |
| 228 | return 1; |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 232 | void |
Kevin O'Connor | c9aecfc | 2014-10-22 20:57:37 -0400 | [diff] [blame] | 233 | stdvga_set_cursor_shape(u16 cursor_type) |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 234 | { |
Kevin O'Connor | c990f27 | 2011-12-31 16:00:54 -0500 | [diff] [blame] | 235 | u16 crtc_addr = stdvga_get_crtc(); |
Kevin O'Connor | c9aecfc | 2014-10-22 20:57:37 -0400 | [diff] [blame] | 236 | stdvga_crtc_write(crtc_addr, 0x0a, cursor_type >> 8); |
| 237 | stdvga_crtc_write(crtc_addr, 0x0b, cursor_type); |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void |
Kevin O'Connor | 1692007 | 2012-01-27 22:59:46 -0500 | [diff] [blame] | 241 | stdvga_set_cursor_pos(int address) |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 242 | { |
Kevin O'Connor | c990f27 | 2011-12-31 16:00:54 -0500 | [diff] [blame] | 243 | u16 crtc_addr = stdvga_get_crtc(); |
Kevin O'Connor | 1692007 | 2012-01-27 22:59:46 -0500 | [diff] [blame] | 244 | address /= 2; // Assume we're in text mode. |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 245 | stdvga_crtc_write(crtc_addr, 0x0e, address >> 8); |
| 246 | stdvga_crtc_write(crtc_addr, 0x0f, address); |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 250 | stdvga_set_scan_lines(u8 lines) |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 251 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 252 | stdvga_crtc_mask(stdvga_get_crtc(), 0x09, 0x1f, lines - 1); |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Get vertical display end |
| 256 | u16 |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 257 | stdvga_get_vde(void) |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 258 | { |
Kevin O'Connor | c990f27 | 2011-12-31 16:00:54 -0500 | [diff] [blame] | 259 | u16 crtc_addr = stdvga_get_crtc(); |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 260 | u16 vde = stdvga_crtc_read(crtc_addr, 0x12); |
| 261 | u8 ovl = stdvga_crtc_read(crtc_addr, 0x07); |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 262 | vde += (((ovl & 0x02) << 7) + ((ovl & 0x40) << 3) + 1); |
| 263 | return vde; |
| 264 | } |
| 265 | |
Kevin O'Connor | 9961f99 | 2012-01-21 11:53:44 -0500 | [diff] [blame] | 266 | int |
| 267 | stdvga_get_window(struct vgamode_s *vmode_g, int window) |
| 268 | { |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | int |
| 273 | stdvga_set_window(struct vgamode_s *vmode_g, int window, int val) |
| 274 | { |
| 275 | return -1; |
| 276 | } |
| 277 | |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 278 | int |
| 279 | stdvga_get_linelength(struct vgamode_s *vmode_g) |
| 280 | { |
| 281 | u8 val = stdvga_crtc_read(stdvga_get_crtc(), 0x13); |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 282 | return val * 8 / stdvga_vram_ratio(vmode_g); |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | int |
| 286 | stdvga_set_linelength(struct vgamode_s *vmode_g, int val) |
| 287 | { |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 288 | val = DIV_ROUND_UP(val * stdvga_vram_ratio(vmode_g), 8); |
| 289 | stdvga_crtc_write(stdvga_get_crtc(), 0x13, val); |
Kevin O'Connor | 3876b53 | 2012-01-24 00:07:44 -0500 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
Kevin O'Connor | d61fc53 | 2012-01-27 20:37:45 -0500 | [diff] [blame] | 293 | int |
| 294 | stdvga_get_displaystart(struct vgamode_s *vmode_g) |
| 295 | { |
| 296 | u16 crtc_addr = stdvga_get_crtc(); |
| 297 | int addr = (stdvga_crtc_read(crtc_addr, 0x0c) << 8 |
| 298 | | stdvga_crtc_read(crtc_addr, 0x0d)); |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 299 | return addr * 4 / stdvga_vram_ratio(vmode_g); |
Kevin O'Connor | d61fc53 | 2012-01-27 20:37:45 -0500 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | int |
| 303 | stdvga_set_displaystart(struct vgamode_s *vmode_g, int val) |
| 304 | { |
| 305 | u16 crtc_addr = stdvga_get_crtc(); |
Kevin O'Connor | 68f56aa | 2013-09-10 10:41:33 -0400 | [diff] [blame] | 306 | val = val * stdvga_vram_ratio(vmode_g) / 4; |
Kevin O'Connor | d61fc53 | 2012-01-27 20:37:45 -0500 | [diff] [blame] | 307 | stdvga_crtc_write(crtc_addr, 0x0c, val >> 8); |
| 308 | stdvga_crtc_write(crtc_addr, 0x0d, val); |
| 309 | return 0; |
| 310 | } |
| 311 | |
Kevin O'Connor | e737b17 | 2012-02-04 11:08:39 -0500 | [diff] [blame] | 312 | int |
| 313 | stdvga_get_dacformat(struct vgamode_s *vmode_g) |
| 314 | { |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | int |
| 319 | stdvga_set_dacformat(struct vgamode_s *vmode_g, int val) |
| 320 | { |
| 321 | return -1; |
| 322 | } |
| 323 | |
Patrick Rudolph | 4902b8a | 2017-05-29 19:25:12 +0200 | [diff] [blame] | 324 | int |
| 325 | stdvga_get_linesize(struct vgamode_s *vmode_g) |
| 326 | { |
| 327 | return DIV_ROUND_UP(vmode_g->width * vga_bpp(vmode_g), 8); |
| 328 | } |
Kevin O'Connor | a0ecb05 | 2009-05-18 23:34:00 -0400 | [diff] [blame] | 329 | |
| 330 | /**************************************************************** |
Kevin O'Connor | f98bbf0 | 2012-01-27 23:09:02 -0500 | [diff] [blame] | 331 | * Save/Restore state |
Kevin O'Connor | c0c7df6 | 2009-05-17 18:11:33 -0400 | [diff] [blame] | 332 | ****************************************************************/ |
| 333 | |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 334 | struct saveVideoHardware { |
| 335 | u8 sequ_index; |
| 336 | u8 crtc_index; |
| 337 | u8 grdc_index; |
| 338 | u8 actl_index; |
| 339 | u8 feature; |
| 340 | u8 sequ_regs[4]; |
| 341 | u8 sequ0; |
| 342 | u8 crtc_regs[25]; |
| 343 | u8 actl_regs[20]; |
| 344 | u8 grdc_regs[9]; |
| 345 | u16 crtc_addr; |
| 346 | u8 plane_latch[4]; |
Kevin O'Connor | f5ec1e0 | 2014-02-05 18:49:44 -0500 | [diff] [blame] | 347 | } PACKED; |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 348 | |
| 349 | static void |
| 350 | stdvga_save_hw_state(u16 seg, struct saveVideoHardware *info) |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 351 | { |
Kevin O'Connor | c990f27 | 2011-12-31 16:00:54 -0500 | [diff] [blame] | 352 | u16 crtc_addr = stdvga_get_crtc(); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 353 | SET_FARVAR(seg, info->sequ_index, inb(VGAREG_SEQU_ADDRESS)); |
| 354 | SET_FARVAR(seg, info->crtc_index, inb(crtc_addr)); |
| 355 | SET_FARVAR(seg, info->grdc_index, inb(VGAREG_GRDC_ADDRESS)); |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 356 | SET_FARVAR(seg, info->actl_index, stdvga_attrindex_read()); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 357 | SET_FARVAR(seg, info->feature, inb(VGAREG_READ_FEATURE_CTL)); |
| 358 | |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 359 | int i; |
| 360 | for (i=0; i<4; i++) |
| 361 | SET_FARVAR(seg, info->sequ_regs[i], stdvga_sequ_read(i+1)); |
| 362 | SET_FARVAR(seg, info->sequ0, stdvga_sequ_read(0)); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 363 | |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 364 | for (i=0; i<25; i++) |
| 365 | SET_FARVAR(seg, info->crtc_regs[i], stdvga_crtc_read(crtc_addr, i)); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 366 | |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 367 | for (i=0; i<20; i++) |
| 368 | SET_FARVAR(seg, info->actl_regs[i], stdvga_attr_read(i)); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 369 | |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 370 | for (i=0; i<9; i++) |
| 371 | SET_FARVAR(seg, info->grdc_regs[i], stdvga_grdc_read(i)); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 372 | |
| 373 | SET_FARVAR(seg, info->crtc_addr, crtc_addr); |
| 374 | |
| 375 | /* XXX: read plane latches */ |
| 376 | for (i=0; i<4; i++) |
| 377 | SET_FARVAR(seg, info->plane_latch[i], 0); |
| 378 | } |
| 379 | |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 380 | static void |
| 381 | stdvga_restore_hw_state(u16 seg, struct saveVideoHardware *info) |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 382 | { |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 383 | int i; |
| 384 | for (i=0; i<4; i++) |
| 385 | stdvga_sequ_write(i+1, GET_FARVAR(seg, info->sequ_regs[i])); |
| 386 | stdvga_sequ_write(0x00, GET_FARVAR(seg, info->sequ0)); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 387 | |
| 388 | // Disable CRTC write protection |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 389 | u16 crtc_addr = GET_FARVAR(seg, info->crtc_addr); |
| 390 | stdvga_crtc_write(crtc_addr, 0x11, 0x00); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 391 | // Set CRTC regs |
| 392 | for (i=0; i<25; i++) |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 393 | if (i != 0x11) |
| 394 | stdvga_crtc_write(crtc_addr, i, GET_FARVAR(seg, info->crtc_regs[i])); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 395 | // select crtc base address |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 396 | stdvga_misc_mask(0x01, crtc_addr == VGAREG_VGA_CRTC_ADDRESS ? 0x01 : 0x00); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 397 | |
| 398 | // enable write protection if needed |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 399 | stdvga_crtc_write(crtc_addr, 0x11, GET_FARVAR(seg, info->crtc_regs[0x11])); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 400 | |
| 401 | // Set Attribute Ctl |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 402 | for (i=0; i<20; i++) |
| 403 | stdvga_attr_write(i, GET_FARVAR(seg, info->actl_regs[i])); |
| 404 | stdvga_attrindex_write(GET_FARVAR(seg, info->actl_index)); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 405 | |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 406 | for (i=0; i<9; i++) |
| 407 | stdvga_grdc_write(i, GET_FARVAR(seg, info->grdc_regs[i])); |
Kevin O'Connor | ca66864 | 2009-05-21 23:06:08 -0400 | [diff] [blame] | 408 | |
| 409 | outb(GET_FARVAR(seg, info->sequ_index), VGAREG_SEQU_ADDRESS); |
| 410 | outb(GET_FARVAR(seg, info->crtc_index), crtc_addr); |
| 411 | outb(GET_FARVAR(seg, info->grdc_index), VGAREG_GRDC_ADDRESS); |
| 412 | outb(GET_FARVAR(seg, info->feature), crtc_addr - 0x4 + 0xa); |
| 413 | } |
Kevin O'Connor | 124b6f7 | 2009-05-25 00:44:29 -0400 | [diff] [blame] | 414 | |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 415 | struct saveDACcolors { |
| 416 | u8 rwmode; |
| 417 | u8 peladdr; |
| 418 | u8 pelmask; |
| 419 | u8 dac[768]; |
| 420 | u8 color_select; |
Kevin O'Connor | f5ec1e0 | 2014-02-05 18:49:44 -0500 | [diff] [blame] | 421 | } PACKED; |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 422 | |
| 423 | static void |
| 424 | stdvga_save_dac_state(u16 seg, struct saveDACcolors *info) |
| 425 | { |
| 426 | /* XXX: check this */ |
| 427 | SET_FARVAR(seg, info->rwmode, inb(VGAREG_DAC_STATE)); |
| 428 | SET_FARVAR(seg, info->peladdr, inb(VGAREG_DAC_WRITE_ADDRESS)); |
| 429 | SET_FARVAR(seg, info->pelmask, stdvga_pelmask_read()); |
| 430 | stdvga_dac_read(seg, info->dac, 0, 256); |
| 431 | SET_FARVAR(seg, info->color_select, 0); |
| 432 | } |
| 433 | |
| 434 | static void |
| 435 | stdvga_restore_dac_state(u16 seg, struct saveDACcolors *info) |
| 436 | { |
| 437 | stdvga_pelmask_write(GET_FARVAR(seg, info->pelmask)); |
| 438 | stdvga_dac_write(seg, info->dac, 0, 256); |
| 439 | outb(GET_FARVAR(seg, info->peladdr), VGAREG_DAC_WRITE_ADDRESS); |
| 440 | } |
| 441 | |
| 442 | int |
Kevin O'Connor | 20dc419 | 2014-02-05 20:52:25 -0500 | [diff] [blame] | 443 | stdvga_save_restore(int cmd, u16 seg, void *data) |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 444 | { |
Kevin O'Connor | 20dc419 | 2014-02-05 20:52:25 -0500 | [diff] [blame] | 445 | void *pos = data; |
| 446 | if (cmd & SR_HARDWARE) { |
| 447 | if (cmd & SR_SAVE) |
| 448 | stdvga_save_hw_state(seg, pos); |
| 449 | if (cmd & SR_RESTORE) |
| 450 | stdvga_restore_hw_state(seg, pos); |
| 451 | pos += sizeof(struct saveVideoHardware); |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 452 | } |
Kevin O'Connor | 20dc419 | 2014-02-05 20:52:25 -0500 | [diff] [blame] | 453 | pos += bda_save_restore(cmd, seg, pos); |
| 454 | if (cmd & SR_DAC) { |
| 455 | if (cmd & SR_SAVE) |
| 456 | stdvga_save_dac_state(seg, pos); |
| 457 | if (cmd & SR_RESTORE) |
| 458 | stdvga_restore_dac_state(seg, pos); |
| 459 | pos += sizeof(struct saveDACcolors); |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 460 | } |
Kevin O'Connor | 20dc419 | 2014-02-05 20:52:25 -0500 | [diff] [blame] | 461 | return pos - data; |
Kevin O'Connor | 9f857fc | 2012-02-04 11:59:02 -0500 | [diff] [blame] | 462 | } |
| 463 | |
Kevin O'Connor | 124b6f7 | 2009-05-25 00:44:29 -0400 | [diff] [blame] | 464 | |
| 465 | /**************************************************************** |
| 466 | * Misc |
| 467 | ****************************************************************/ |
| 468 | |
| 469 | void |
Kevin O'Connor | 88ca741 | 2011-12-31 04:24:20 -0500 | [diff] [blame] | 470 | stdvga_enable_video_addressing(u8 disable) |
Kevin O'Connor | 124b6f7 | 2009-05-25 00:44:29 -0400 | [diff] [blame] | 471 | { |
| 472 | u8 v = (disable & 1) ? 0x00 : 0x02; |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 473 | stdvga_misc_mask(0x02, v); |
Kevin O'Connor | 124b6f7 | 2009-05-25 00:44:29 -0400 | [diff] [blame] | 474 | } |
| 475 | |
Kevin O'Connor | 161d201 | 2011-12-31 19:42:21 -0500 | [diff] [blame] | 476 | int |
Kevin O'Connor | d83c87b | 2013-01-21 01:14:12 -0500 | [diff] [blame] | 477 | stdvga_setup(void) |
Kevin O'Connor | 124b6f7 | 2009-05-25 00:44:29 -0400 | [diff] [blame] | 478 | { |
| 479 | // switch to color mode and enable CPU access 480 lines |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 480 | stdvga_misc_write(0xc3); |
Kevin O'Connor | 124b6f7 | 2009-05-25 00:44:29 -0400 | [diff] [blame] | 481 | // more than 64k 3C4/04 |
Kevin O'Connor | 86d2e00 | 2012-01-14 22:17:07 -0500 | [diff] [blame] | 482 | stdvga_sequ_write(0x04, 0x02); |
Kevin O'Connor | 161d201 | 2011-12-31 19:42:21 -0500 | [diff] [blame] | 483 | |
| 484 | return 0; |
Kevin O'Connor | 124b6f7 | 2009-05-25 00:44:29 -0400 | [diff] [blame] | 485 | } |