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