blob: da8b790dbec76d1f41e7d1c161bfe7d49d6b2960 [file] [log] [blame]
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04001// QEMU Cirrus CLGD 54xx VGABIOS Extension.
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4// Copyright (c) 2004 Makoto Suzuki (suzu)
5//
6// This file may be distributed under the terms of the GNU LGPLv3 license.
7
Kevin O'Connor1f2c3072009-05-06 23:35:59 -04008#include "biosvar.h" // GET_GLOBAL
Kevin O'Connor3c065362011-12-27 21:34:33 -05009#include "bregs.h" // struct bregs
Kevin O'Connor5d369d82013-09-02 20:48:46 -040010#include "hw/pci.h" // pci_config_readl
11#include "hw/pci_regs.h" // PCI_BASE_ADDRESS_0
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040012#include "output.h" // dprintf
13#include "stdvga.h" // VGAREG_SEQU_ADDRESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040014#include "string.h" // memset16_far
Kevin O'Connor2f2ec112016-08-05 11:14:58 -040015#include "vgabios.h" // SET_VGA
16#include "vgautil.h" // VBE_total_memory
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040017
Kevin O'Connore48a5372011-12-20 23:56:14 -050018
19/****************************************************************
Kevin O'Connora96d4902012-02-01 21:10:44 -050020 * Cirrus mode tables
Kevin O'Connore48a5372011-12-20 23:56:14 -050021 ****************************************************************/
22
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040023/* VGA */
24static u16 cseq_vga[] VAR16 = {0x0007,0xffff};
25static u16 cgraph_vga[] VAR16 = {0x0009,0x000a,0x000b,0xffff};
26static u16 ccrtc_vga[] VAR16 = {0x001a,0x001b,0x001d,0xffff};
27
28/* extensions */
29static u16 cgraph_svgacolor[] VAR16 = {
30 0x0000,0x0001,0x0002,0x0003,0x0004,0x4005,0x0506,0x0f07,0xff08,
31 0x0009,0x000a,0x000b,
32 0xffff
33};
34/* 640x480x8 */
35static u16 cseq_640x480x8[] VAR16 = {
36 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
37 0x580b,0x580c,0x580d,0x580e,
38 0x0412,0x0013,0x2017,
39 0x331b,0x331c,0x331d,0x331e,
40 0xffff
41};
42static u16 ccrtc_640x480x8[] VAR16 = {
43 0x2c11,
44 0x5f00,0x4f01,0x4f02,0x8003,0x5204,0x1e05,0x0b06,0x3e07,
45 0x4009,0x000c,0x000d,
46 0xea10,0xdf12,0x5013,0x4014,0xdf15,0x0b16,0xc317,0xff18,
47 0x001a,0x221b,0x001d,
48 0xffff
49};
50/* 640x480x16 */
51static u16 cseq_640x480x16[] VAR16 = {
52 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1707,
53 0x580b,0x580c,0x580d,0x580e,
54 0x0412,0x0013,0x2017,
55 0x331b,0x331c,0x331d,0x331e,
56 0xffff
57};
58static u16 ccrtc_640x480x16[] VAR16 = {
59 0x2c11,
60 0x5f00,0x4f01,0x4f02,0x8003,0x5204,0x1e05,0x0b06,0x3e07,
61 0x4009,0x000c,0x000d,
62 0xea10,0xdf12,0xa013,0x4014,0xdf15,0x0b16,0xc317,0xff18,
63 0x001a,0x221b,0x001d,
64 0xffff
65};
66/* 640x480x24 */
67static u16 cseq_640x480x24[] VAR16 = {
68 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1507,
69 0x580b,0x580c,0x580d,0x580e,
70 0x0412,0x0013,0x2017,
71 0x331b,0x331c,0x331d,0x331e,
72 0xffff
73};
74static u16 ccrtc_640x480x24[] VAR16 = {
75 0x2c11,
76 0x5f00,0x4f01,0x4f02,0x8003,0x5204,0x1e05,0x0b06,0x3e07,
77 0x4009,0x000c,0x000d,
Kevin O'Connore19a68f2012-01-14 14:52:01 -050078 0xea10,0xdf12,0xf013,0x4014,0xdf15,0x0b16,0xc317,0xff18,
79 0x001a,0x221b,0x001d,
Kevin O'Connor1f2c3072009-05-06 23:35:59 -040080 0xffff
81};
82/* 800x600x8 */
83static u16 cseq_800x600x8[] VAR16 = {
84 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
85 0x230b,0x230c,0x230d,0x230e,
86 0x0412,0x0013,0x2017,
87 0x141b,0x141c,0x141d,0x141e,
88 0xffff
89};
90static u16 ccrtc_800x600x8[] VAR16 = {
91 0x2311,0x7d00,0x6301,0x6302,0x8003,0x6b04,0x1a05,0x9806,0xf007,
92 0x6009,0x000c,0x000d,
93 0x7d10,0x5712,0x6413,0x4014,0x5715,0x9816,0xc317,0xff18,
94 0x001a,0x221b,0x001d,
95 0xffff
96};
97/* 800x600x16 */
98static u16 cseq_800x600x16[] VAR16 = {
99 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1707,
100 0x230b,0x230c,0x230d,0x230e,
101 0x0412,0x0013,0x2017,
102 0x141b,0x141c,0x141d,0x141e,
103 0xffff
104};
105static u16 ccrtc_800x600x16[] VAR16 = {
106 0x2311,0x7d00,0x6301,0x6302,0x8003,0x6b04,0x1a05,0x9806,0xf007,
107 0x6009,0x000c,0x000d,
108 0x7d10,0x5712,0xc813,0x4014,0x5715,0x9816,0xc317,0xff18,
109 0x001a,0x221b,0x001d,
110 0xffff
111};
112/* 800x600x24 */
113static u16 cseq_800x600x24[] VAR16 = {
114 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1507,
115 0x230b,0x230c,0x230d,0x230e,
116 0x0412,0x0013,0x2017,
117 0x141b,0x141c,0x141d,0x141e,
118 0xffff
119};
120static u16 ccrtc_800x600x24[] VAR16 = {
121 0x2311,0x7d00,0x6301,0x6302,0x8003,0x6b04,0x1a05,0x9806,0xf007,
122 0x6009,0x000c,0x000d,
123 0x7d10,0x5712,0x2c13,0x4014,0x5715,0x9816,0xc317,0xff18,
124 0x001a,0x321b,0x001d,
125 0xffff
126};
127/* 1024x768x8 */
128static u16 cseq_1024x768x8[] VAR16 = {
129 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
130 0x760b,0x760c,0x760d,0x760e,
131 0x0412,0x0013,0x2017,
132 0x341b,0x341c,0x341d,0x341e,
133 0xffff
134};
135static u16 ccrtc_1024x768x8[] VAR16 = {
136 0x2911,0xa300,0x7f01,0x7f02,0x8603,0x8304,0x9405,0x2406,0xf507,
137 0x6009,0x000c,0x000d,
138 0x0310,0xff12,0x8013,0x4014,0xff15,0x2416,0xc317,0xff18,
139 0x001a,0x221b,0x001d,
140 0xffff
141};
142/* 1024x768x16 */
143static u16 cseq_1024x768x16[] VAR16 = {
144 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1707,
145 0x760b,0x760c,0x760d,0x760e,
146 0x0412,0x0013,0x2017,
147 0x341b,0x341c,0x341d,0x341e,
148 0xffff
149};
150static u16 ccrtc_1024x768x16[] VAR16 = {
151 0x2911,0xa300,0x7f01,0x7f02,0x8603,0x8304,0x9405,0x2406,0xf507,
152 0x6009,0x000c,0x000d,
153 0x0310,0xff12,0x0013,0x4014,0xff15,0x2416,0xc317,0xff18,
154 0x001a,0x321b,0x001d,
155 0xffff
156};
157/* 1024x768x24 */
158static u16 cseq_1024x768x24[] VAR16 = {
159 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1507,
160 0x760b,0x760c,0x760d,0x760e,
161 0x0412,0x0013,0x2017,
162 0x341b,0x341c,0x341d,0x341e,
163 0xffff
164};
165static u16 ccrtc_1024x768x24[] VAR16 = {
166 0x2911,0xa300,0x7f01,0x7f02,0x8603,0x8304,0x9405,0x2406,0xf507,
167 0x6009,0x000c,0x000d,
168 0x0310,0xff12,0x8013,0x4014,0xff15,0x2416,0xc317,0xff18,
169 0x001a,0x321b,0x001d,
170 0xffff
171};
172/* 1280x1024x8 */
173static u16 cseq_1280x1024x8[] VAR16 = {
174 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
175 0x760b,0x760c,0x760d,0x760e,
176 0x0412,0x0013,0x2017,
177 0x341b,0x341c,0x341d,0x341e,
178 0xffff
179};
180static u16 ccrtc_1280x1024x8[] VAR16 = {
181 0x2911,0xc300,0x9f01,0x9f02,0x8603,0x8304,0x9405,0x2406,0xf707,
182 0x6009,0x000c,0x000d,
183 0x0310,0xff12,0xa013,0x4014,0xff15,0x2416,0xc317,0xff18,
184 0x001a,0x221b,0x001d,
185 0xffff
186};
187/* 1280x1024x16 */
188static u16 cseq_1280x1024x16[] VAR16 = {
189 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1707,
190 0x760b,0x760c,0x760d,0x760e,
191 0x0412,0x0013,0x2017,
192 0x341b,0x341c,0x341d,0x341e,
193 0xffff
194};
195static u16 ccrtc_1280x1024x16[] VAR16 = {
196 0x2911,0xc300,0x9f01,0x9f02,0x8603,0x8304,0x9405,0x2406,0xf707,
197 0x6009,0x000c,0x000d,
198 0x0310,0xff12,0x4013,0x4014,0xff15,0x2416,0xc317,0xff18,
199 0x001a,0x321b,0x001d,
200 0xffff
201};
202
203/* 1600x1200x8 */
204static u16 cseq_1600x1200x8[] VAR16 = {
205 0x0300,0x2101,0x0f02,0x0003,0x0e04,0x1107,
206 0x760b,0x760c,0x760d,0x760e,
207 0x0412,0x0013,0x2017,
208 0x341b,0x341c,0x341d,0x341e,
209 0xffff
210};
211static u16 ccrtc_1600x1200x8[] VAR16 = {
212 0x2911,0xc300,0x9f01,0x9f02,0x8603,0x8304,0x9405,0x2406,0xf707,
213 0x6009,0x000c,0x000d,
Kevin O'Connore19a68f2012-01-14 14:52:01 -0500214 0x0310,0xff12,0xc813,0x4014,0xff15,0x2416,0xc317,0xff18,
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400215 0x001a,0x221b,0x001d,
216 0xffff
217};
218
Kevin O'Connor643290f2012-01-13 22:08:52 -0500219struct cirrus_mode_s {
Kevin O'Connora96d4902012-02-01 21:10:44 -0500220 u16 mode, vesamode;
Kevin O'Connor643290f2012-01-13 22:08:52 -0500221 struct vgamode_s info;
222
223 u16 hidden_dac; /* 0x3c6 */
224 u16 *seq; /* 0x3c4 */
225 u16 *graph; /* 0x3ce */
226 u16 *crtc; /* 0x3d4 */
227};
228
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400229static struct cirrus_mode_s cirrus_modes[] VAR16 = {
Kevin O'Connora96d4902012-02-01 21:10:44 -0500230 {0x5f,0x101,{MM_PACKED,640,480,8,8,16,SEG_GRAPH},0x00,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500231 cseq_640x480x8,cgraph_svgacolor,ccrtc_640x480x8},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500232 {0x64,0x111,{MM_DIRECT,640,480,16,8,16,SEG_GRAPH},0xe1,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500233 cseq_640x480x16,cgraph_svgacolor,ccrtc_640x480x16},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500234 {0x66,0x110,{MM_DIRECT,640,480,15,8,16,SEG_GRAPH},0xf0,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500235 cseq_640x480x16,cgraph_svgacolor,ccrtc_640x480x16},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500236 {0x71,0x112,{MM_DIRECT,640,480,24,8,16,SEG_GRAPH},0xe5,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500237 cseq_640x480x24,cgraph_svgacolor,ccrtc_640x480x24},
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400238
Kevin O'Connora96d4902012-02-01 21:10:44 -0500239 {0x5c,0x103,{MM_PACKED,800,600,8,8,16,SEG_GRAPH},0x00,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500240 cseq_800x600x8,cgraph_svgacolor,ccrtc_800x600x8},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500241 {0x65,0x114,{MM_DIRECT,800,600,16,8,16,SEG_GRAPH},0xe1,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500242 cseq_800x600x16,cgraph_svgacolor,ccrtc_800x600x16},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500243 {0x67,0x113,{MM_DIRECT,800,600,15,8,16,SEG_GRAPH},0xf0,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500244 cseq_800x600x16,cgraph_svgacolor,ccrtc_800x600x16},
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400245
Kevin O'Connora96d4902012-02-01 21:10:44 -0500246 {0x60,0x105,{MM_PACKED,1024,768,8,8,16,SEG_GRAPH},0x00,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500247 cseq_1024x768x8,cgraph_svgacolor,ccrtc_1024x768x8},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500248 {0x74,0x117,{MM_DIRECT,1024,768,16,8,16,SEG_GRAPH},0xe1,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500249 cseq_1024x768x16,cgraph_svgacolor,ccrtc_1024x768x16},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500250 {0x68,0x116,{MM_DIRECT,1024,768,15,8,16,SEG_GRAPH},0xf0,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500251 cseq_1024x768x16,cgraph_svgacolor,ccrtc_1024x768x16},
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400252
Kevin O'Connora96d4902012-02-01 21:10:44 -0500253 {0x78,0x115,{MM_DIRECT,800,600,24,8,16,SEG_GRAPH},0xe5,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500254 cseq_800x600x24,cgraph_svgacolor,ccrtc_800x600x24},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500255 {0x79,0x118,{MM_DIRECT,1024,768,24,8,16,SEG_GRAPH},0xe5,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500256 cseq_1024x768x24,cgraph_svgacolor,ccrtc_1024x768x24},
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400257
Kevin O'Connora96d4902012-02-01 21:10:44 -0500258 {0x6d,0x107,{MM_PACKED,1280,1024,8,8,16,SEG_GRAPH},0x00,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500259 cseq_1280x1024x8,cgraph_svgacolor,ccrtc_1280x1024x8},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500260 {0x69,0x119,{MM_DIRECT,1280,1024,15,8,16,SEG_GRAPH},0xf0,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500261 cseq_1280x1024x16,cgraph_svgacolor,ccrtc_1280x1024x16},
Kevin O'Connora96d4902012-02-01 21:10:44 -0500262 {0x75,0x11a,{MM_DIRECT,1280,1024,16,8,16,SEG_GRAPH},0xe1,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500263 cseq_1280x1024x16,cgraph_svgacolor,ccrtc_1280x1024x16},
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400264
Kevin O'Connora96d4902012-02-01 21:10:44 -0500265 {0x7b,0xffff,{MM_PACKED,1600,1200,8,8,16,SEG_GRAPH},0x00,
Kevin O'Connor643290f2012-01-13 22:08:52 -0500266 cseq_1600x1200x8,cgraph_svgacolor,ccrtc_1600x1200x8},
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400267};
268
Kevin O'Connor2c8ba892012-01-02 10:51:26 -0500269static struct cirrus_mode_s mode_switchback VAR16 =
Kevin O'Connora96d4902012-02-01 21:10:44 -0500270 {0xfe,0xffff,{0xff},0,cseq_vga,cgraph_vga,ccrtc_vga};
Kevin O'Connore48a5372011-12-20 23:56:14 -0500271
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500272int
273is_cirrus_mode(struct vgamode_s *vmode_g)
274{
275 return (vmode_g >= &cirrus_modes[0].info
276 && vmode_g <= &cirrus_modes[ARRAY_SIZE(cirrus_modes)-1].info);
277}
278
Kevin O'Connora96d4902012-02-01 21:10:44 -0500279struct vgamode_s *
280clext_find_mode(int mode)
281{
282 struct cirrus_mode_s *table_g = cirrus_modes;
283 while (table_g < &cirrus_modes[ARRAY_SIZE(cirrus_modes)]) {
284 if (GET_GLOBAL(table_g->mode) == mode
285 || GET_GLOBAL(table_g->vesamode) == mode)
286 return &table_g->info;
287 table_g++;
288 }
289 return stdvga_find_mode(mode);
290}
291
Kevin O'Connor59f75d42012-01-27 20:52:29 -0500292void
293clext_list_modes(u16 seg, u16 *dest, u16 *last)
294{
295 int i;
Kevin O'Connora96d4902012-02-01 21:10:44 -0500296 for (i=0; i<ARRAY_SIZE(cirrus_modes) && dest<last; i++) {
297 u16 mode = GET_GLOBAL(cirrus_modes[i].vesamode);
298 if (mode == 0xffff)
299 continue;
300 SET_FARVAR(seg, *dest, mode);
Kevin O'Connor59f75d42012-01-27 20:52:29 -0500301 dest++;
302 }
303 stdvga_list_modes(seg, dest, last);
304}
305
Kevin O'Connor643290f2012-01-13 22:08:52 -0500306
Kevin O'Connora96d4902012-02-01 21:10:44 -0500307/****************************************************************
308 * helper functions
309 ****************************************************************/
Kevin O'Connorc4a0b972012-01-09 20:21:31 -0500310
Kevin O'Connor9961f992012-01-21 11:53:44 -0500311int
312clext_get_window(struct vgamode_s *vmode_g, int window)
313{
314 return stdvga_grdc_read(window + 9);
315}
316
317int
318clext_set_window(struct vgamode_s *vmode_g, int window, int val)
319{
320 if (val >= 0x100)
321 return -1;
322 stdvga_grdc_write(window + 9, val);
323 return 0;
324}
325
Kevin O'Connor3876b532012-01-24 00:07:44 -0500326int
327clext_get_linelength(struct vgamode_s *vmode_g)
328{
329 u16 crtc_addr = stdvga_get_crtc();
330 u8 reg13 = stdvga_crtc_read(crtc_addr, 0x13);
331 u8 reg1b = stdvga_crtc_read(crtc_addr, 0x1b);
Kevin O'Connor68f56aa2013-09-10 10:41:33 -0400332 return (((reg1b & 0x10) << 4) + reg13) * 8 / stdvga_vram_ratio(vmode_g);
Kevin O'Connor3876b532012-01-24 00:07:44 -0500333}
334
335int
336clext_set_linelength(struct vgamode_s *vmode_g, int val)
337{
338 u16 crtc_addr = stdvga_get_crtc();
Kevin O'Connor68f56aa2013-09-10 10:41:33 -0400339 val = DIV_ROUND_UP(val * stdvga_vram_ratio(vmode_g), 8);
340 stdvga_crtc_write(crtc_addr, 0x13, val);
341 stdvga_crtc_mask(crtc_addr, 0x1b, 0x10, (val & 0x100) >> 4);
Kevin O'Connor3876b532012-01-24 00:07:44 -0500342 return 0;
343}
344
Kevin O'Connord61fc532012-01-27 20:37:45 -0500345int
346clext_get_displaystart(struct vgamode_s *vmode_g)
347{
348 u16 crtc_addr = stdvga_get_crtc();
349 u8 b2 = stdvga_crtc_read(crtc_addr, 0x0c);
350 u8 b1 = stdvga_crtc_read(crtc_addr, 0x0d);
351 u8 b3 = stdvga_crtc_read(crtc_addr, 0x1b);
352 u8 b4 = stdvga_crtc_read(crtc_addr, 0x1d);
353 int val = (b1 | (b2<<8) | ((b3 & 0x01) << 16) | ((b3 & 0x0c) << 15)
354 | ((b4 & 0x80) << 12));
Kevin O'Connor68f56aa2013-09-10 10:41:33 -0400355 return val * 4 / stdvga_vram_ratio(vmode_g);
Kevin O'Connord61fc532012-01-27 20:37:45 -0500356}
357
358int
359clext_set_displaystart(struct vgamode_s *vmode_g, int val)
360{
361 u16 crtc_addr = stdvga_get_crtc();
Kevin O'Connor68f56aa2013-09-10 10:41:33 -0400362 val = val * stdvga_vram_ratio(vmode_g) / 4;
Kevin O'Connord61fc532012-01-27 20:37:45 -0500363 stdvga_crtc_write(crtc_addr, 0x0d, val);
364 stdvga_crtc_write(crtc_addr, 0x0c, val >> 8);
365 stdvga_crtc_mask(crtc_addr, 0x1d, 0x80, (val & 0x0800) >> 4);
366 stdvga_crtc_mask(crtc_addr, 0x1b, 0x0d
367 , ((val & 0x0100) >> 8) | ((val & 0x0600) >> 7));
368 return 0;
369}
370
Kevin O'Connor2469f892012-02-04 12:40:02 -0500371int
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500372clext_save_restore(int cmd, u16 seg, void *data)
Kevin O'Connor2469f892012-02-04 12:40:02 -0500373{
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500374 if (cmd & SR_REGISTERS)
Kevin O'Connor2469f892012-02-04 12:40:02 -0500375 return -1;
Kevin O'Connor20dc4192014-02-05 20:52:25 -0500376 return stdvga_save_restore(cmd, seg, data);
Kevin O'Connor2469f892012-02-04 12:40:02 -0500377}
378
Kevin O'Connor987029a2012-02-01 21:16:34 -0500379
380/****************************************************************
381 * Mode setting
382 ****************************************************************/
383
384static void
385cirrus_switch_mode_setregs(u16 *data, u16 port)
386{
387 for (;;) {
388 u16 val = GET_GLOBAL(*data);
389 if (val == 0xffff)
390 return;
391 outw(val, port);
392 data++;
393 }
394}
395
396static void
397cirrus_switch_mode(struct cirrus_mode_s *table)
398{
399 // Unlock cirrus special
400 stdvga_sequ_write(0x06, 0x12);
401 cirrus_switch_mode_setregs(GET_GLOBAL(table->seq), VGAREG_SEQU_ADDRESS);
402 cirrus_switch_mode_setregs(GET_GLOBAL(table->graph), VGAREG_GRDC_ADDRESS);
403 cirrus_switch_mode_setregs(GET_GLOBAL(table->crtc), stdvga_get_crtc());
404
405 stdvga_pelmask_write(0x00);
406 stdvga_pelmask_read();
407 stdvga_pelmask_read();
408 stdvga_pelmask_read();
409 stdvga_pelmask_read();
410 stdvga_pelmask_write(GET_GLOBAL(table->hidden_dac));
411 stdvga_pelmask_write(0xff);
412
413 u8 memmodel = GET_GLOBAL(table->info.memmodel);
414 u8 on = 0;
415 if (memmodel == MM_PLANAR)
416 on = 0x41;
417 else if (memmodel != MM_TEXT)
418 on = 0x01;
419 stdvga_attr_mask(0x10, 0x01, on);
Laszlo Ersek2eeafbf2013-02-14 05:43:32 +0100420 stdvga_attrindex_write(0x20);
Kevin O'Connor987029a2012-02-01 21:16:34 -0500421}
422
Kevin O'Connore48a5372011-12-20 23:56:14 -0500423static void
424cirrus_enable_16k_granularity(void)
425{
Kevin O'Connoradd3bec2012-01-14 22:18:02 -0500426 stdvga_grdc_mask(0x0b, 0x00, 0x20);
Kevin O'Connore48a5372011-12-20 23:56:14 -0500427}
428
429static void
Kevin O'Connorb7b92932013-03-09 13:04:47 -0500430cirrus_clear_vram(u16 fill)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500431{
432 cirrus_enable_16k_granularity();
Kevin O'Connorb7b92932013-03-09 13:04:47 -0500433 int count = GET_GLOBAL(VBE_total_memory) / (16 * 1024);
434 int i;
Kevin O'Connore48a5372011-12-20 23:56:14 -0500435 for (i=0; i<count; i++) {
Kevin O'Connoradd3bec2012-01-14 22:18:02 -0500436 stdvga_grdc_write(0x09, i);
Kevin O'Connorb7b92932013-03-09 13:04:47 -0500437 memset16_far(SEG_GRAPH, 0, fill, 16 * 1024);
Kevin O'Connore48a5372011-12-20 23:56:14 -0500438 }
Kevin O'Connoradd3bec2012-01-14 22:18:02 -0500439 stdvga_grdc_write(0x09, 0x00);
Kevin O'Connore48a5372011-12-20 23:56:14 -0500440}
441
442int
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500443clext_set_mode(struct vgamode_s *vmode_g, int flags)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400444{
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500445 if (!is_cirrus_mode(vmode_g)) {
446 cirrus_switch_mode(&mode_switchback);
447 dprintf(1, "cirrus mode switch regular\n");
448 return stdvga_set_mode(vmode_g, flags);
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400449 }
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500450 struct cirrus_mode_s *table_g = container_of(
451 vmode_g, struct cirrus_mode_s, info);
452 cirrus_switch_mode(table_g);
Kevin O'Connor5b6936e2013-11-29 18:43:35 -0500453 if (GET_GLOBAL(vmode_g->memmodel) == MM_PACKED && !(flags & MF_NOPALETTE))
454 stdvga_set_packed_palette();
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500455 if (!(flags & MF_LINEARFB))
456 cirrus_enable_16k_granularity();
457 if (!(flags & MF_NOCLEARMEM))
Kevin O'Connorb7b92932013-03-09 13:04:47 -0500458 // fill with 0xff to keep win 2K happy
459 cirrus_clear_vram(flags & MF_LEGACY ? 0xffff : 0x0000);
Kevin O'Connore6bc4c12012-01-21 11:26:37 -0500460 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400461}
462
Kevin O'Connore48a5372011-12-20 23:56:14 -0500463
464/****************************************************************
465 * extbios
466 ****************************************************************/
467
468static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500469clext_101280(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500470{
Kevin O'Connoradd3bec2012-01-14 22:18:02 -0500471 u8 v = stdvga_crtc_read(stdvga_get_crtc(), 0x27);
Kevin O'Connore48a5372011-12-20 23:56:14 -0500472 if (v == 0xa0)
473 // 5430
474 regs->ax = 0x0032;
475 else if (v == 0xb8)
476 // 5446
477 regs->ax = 0x0039;
478 else
479 regs->ax = 0x00ff;
480 regs->bx = 0x00;
481 return;
482}
483
484static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500485clext_101281(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500486{
487 // XXX
488 regs->ax = 0x0100;
489}
490
491static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500492clext_101282(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500493{
Kevin O'Connoradd3bec2012-01-14 22:18:02 -0500494 regs->al = stdvga_crtc_read(stdvga_get_crtc(), 0x27) & 0x03;
Kevin O'Connore48a5372011-12-20 23:56:14 -0500495 regs->ah = 0xAF;
496}
497
498static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500499clext_101285(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500500{
Kevin O'Connor5e1694c2012-01-21 10:43:30 -0500501 regs->al = GET_GLOBAL(VBE_total_memory) / (64*1024);
Kevin O'Connore48a5372011-12-20 23:56:14 -0500502}
503
504static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500505clext_10129a(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500506{
507 regs->ax = 0x4060;
508 regs->cx = 0x1132;
509}
510
511extern void a0h_callback(void);
512ASM16(
513 // fatal: not implemented yet
514 "a0h_callback:"
515 "cli\n"
516 "hlt\n"
Kevin O'Connor900ded02012-03-05 17:10:13 -0500517 "lretw");
Kevin O'Connore48a5372011-12-20 23:56:14 -0500518
519static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500520clext_1012a0(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500521{
Kevin O'Connora96d4902012-02-01 21:10:44 -0500522 struct vgamode_s *table_g = clext_find_mode(regs->al & 0x7f);
Kevin O'Connore48a5372011-12-20 23:56:14 -0500523 regs->ah = (table_g ? 1 : 0);
Kevin O'Connor4c85a262012-02-12 11:50:52 -0500524 regs->bx = (u32)a0h_callback;
525 regs->ds = regs->si = regs->es = regs->di = 0xffff;
Kevin O'Connore48a5372011-12-20 23:56:14 -0500526}
527
528static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500529clext_1012a1(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500530{
531 regs->bx = 0x0e00; // IBM 8512/8513, color
532}
533
534static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500535clext_1012a2(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500536{
537 regs->al = 0x07; // HSync 31.5 - 64.0 kHz
538}
539
540static void
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500541clext_1012ae(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500542{
543 regs->al = 0x01; // High Refresh 75Hz
544}
545
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500546static void
547clext_1012XX(struct bregs *regs)
Kevin O'Connore48a5372011-12-20 23:56:14 -0500548{
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500549 debug_stub(regs);
550}
551
552void
553clext_1012(struct bregs *regs)
554{
Kevin O'Connore48a5372011-12-20 23:56:14 -0500555 switch (regs->bl) {
Kevin O'Connore91ec7c2012-01-14 16:30:49 -0500556 case 0x80: clext_101280(regs); break;
557 case 0x81: clext_101281(regs); break;
558 case 0x82: clext_101282(regs); break;
559 case 0x85: clext_101285(regs); break;
560 case 0x9a: clext_10129a(regs); break;
561 case 0xa0: clext_1012a0(regs); break;
562 case 0xa1: clext_1012a1(regs); break;
563 case 0xa2: clext_1012a2(regs); break;
564 case 0xae: clext_1012ae(regs); break;
565 default: clext_1012XX(regs); break;
Kevin O'Connore48a5372011-12-20 23:56:14 -0500566 }
567}
568
569
570/****************************************************************
Kevin O'Connore48a5372011-12-20 23:56:14 -0500571 * init
572 ****************************************************************/
573
Kevin O'Connor987029a2012-02-01 21:16:34 -0500574static int
575cirrus_check(void)
576{
577 stdvga_sequ_write(0x06, 0x92);
578 return stdvga_sequ_read(0x06) == 0x12;
579}
580
581static u8
582cirrus_get_memsize(void)
583{
584 // get DRAM band width
585 u8 v = stdvga_sequ_read(0x0f);
586 u8 x = (v >> 3) & 0x03;
587 if (x == 0x03 && v & 0x80)
588 // 4MB
589 return 0x40;
590 return 0x04 << x;
591}
592
Kevin O'Connor161d2012011-12-31 19:42:21 -0500593int
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500594clext_setup(void)
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400595{
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500596 int ret = stdvga_setup();
Kevin O'Connor161d2012011-12-31 19:42:21 -0500597 if (ret)
598 return ret;
599
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400600 dprintf(1, "cirrus init\n");
601 if (! cirrus_check())
Kevin O'Connor161d2012011-12-31 19:42:21 -0500602 return -1;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400603 dprintf(1, "cirrus init 2\n");
604
Kevin O'Connorcfd7ef92012-02-02 22:52:17 -0500605 // memory setup
606 stdvga_sequ_write(0x0a, stdvga_sequ_read(0x0f) & 0x18);
607 // set vga mode
608 stdvga_sequ_write(0x07, 0x00);
609 // reset bitblt
610 stdvga_grdc_write(0x31, 0x04);
611 stdvga_grdc_write(0x31, 0x00);
612
613 if (GET_GLOBAL(HaveRunInit))
614 return 0;
615
Kevin O'Connor97cc3542012-01-14 16:59:21 -0500616 u32 lfb_addr = 0;
Kevin O'Connor8cf8f8e2012-01-16 19:05:27 -0500617 int bdf = GET_GLOBAL(VgaBDF);
618 if (CONFIG_VGA_PCI && bdf >= 0)
619 lfb_addr = (pci_config_readl(bdf, PCI_BASE_ADDRESS_0)
Kevin O'Connor97cc3542012-01-14 16:59:21 -0500620 & PCI_BASE_ADDRESS_MEM_MASK);
621 SET_VGA(VBE_framebuffer, lfb_addr);
Kevin O'Connor643290f2012-01-13 22:08:52 -0500622 u16 totalmem = cirrus_get_memsize();
623 SET_VGA(VBE_total_memory, totalmem * 64 * 1024);
624 SET_VGA(VBE_win_granularity, 16);
625
Kevin O'Connor161d2012011-12-31 19:42:21 -0500626 return 0;
Kevin O'Connor1f2c3072009-05-06 23:35:59 -0400627}