summaryrefslogtreecommitdiff
path: root/src/drivers/emulation/qemu/cirrus.c
blob: ede0232a9ab091fd785daf8d16ea7ca51105e7ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2013 Vladimir Serbinenko
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdint.h>
#include <delay.h>
#include <edid.h>
#include <stdlib.h>
#include <string.h>

#include <boot/coreboot_tables.h>
#include <console/console.h>
#include <device/device.h>
#include <device/pci.h>
#include <device/pci_ids.h>
#include <device/pci_ops.h>

#include <pc80/vga.h>
#include <pc80/vga_io.h>

static int width  = CONFIG_DRIVERS_EMULATION_QEMU_BOCHS_XRES;
static int height = CONFIG_DRIVERS_EMULATION_QEMU_BOCHS_YRES;
static u32 addr   = 0;

enum
  {
    VGA_CR_HTOTAL = 0x00,
    VGA_CR_HORIZ_END = 0x01,
    VGA_CR_HBLANK_START = 0x02,
    VGA_CR_HBLANK_END = 0x03,
    VGA_CR_HORIZ_SYNC_PULSE_START = 0x04,
    VGA_CR_HORIZ_SYNC_PULSE_END = 0x05,
    VGA_CR_VERT_TOTAL = 0x06,
    VGA_CR_OVERFLOW = 0x07,
    VGA_CR_BYTE_PANNING = 0x08,
    VGA_CR_CELL_HEIGHT = 0x09,
    VGA_CR_CURSOR_START = 0x0a,
    VGA_CR_CURSOR_END = 0x0b,
    VGA_CR_START_ADDR_HIGH_REGISTER = 0x0c,
    VGA_CR_START_ADDR_LOW_REGISTER = 0x0d,
    VGA_CR_CURSOR_ADDR_HIGH = 0x0e,
    VGA_CR_CURSOR_ADDR_LOW = 0x0f,
    VGA_CR_VSYNC_START = 0x10,
    VGA_CR_VSYNC_END = 0x11,
    VGA_CR_VDISPLAY_END = 0x12,
    VGA_CR_PITCH = 0x13,
    VGA_CR_UNDERLINE_LOCATION = 0x14,
    VGA_CR_VERTICAL_BLANK_START = 0x15,
    VGA_CR_VERTICAL_BLANK_END = 0x16,
    VGA_CR_MODE = 0x17,
    VGA_CR_LINE_COMPARE = 0x18,
  };

#define VGA_IO_MISC_COLOR 0x01

#define VGA_CR_WIDTH_DIVISOR 8

#define VGA_CR_OVERFLOW_VERT_DISPLAY_ENABLE_END1_SHIFT 7
#define VGA_CR_OVERFLOW_VERT_DISPLAY_ENABLE_END1_MASK 0x02
#define VGA_CR_OVERFLOW_VERT_DISPLAY_ENABLE_END2_SHIFT 3
#define VGA_CR_OVERFLOW_VERT_DISPLAY_ENABLE_END2_MASK 0x40

#define VGA_CR_OVERFLOW_VERT_TOTAL1_SHIFT 8
#define VGA_CR_OVERFLOW_VERT_TOTAL1_MASK 0x01
#define VGA_CR_OVERFLOW_VERT_TOTAL2_SHIFT 4
#define VGA_CR_OVERFLOW_VERT_TOTAL2_MASK 0x20

#define VGA_CR_OVERFLOW_VSYNC_START1_SHIFT 6
#define VGA_CR_OVERFLOW_VSYNC_START1_MASK 0x04
#define VGA_CR_OVERFLOW_VSYNC_START2_SHIFT 2
#define VGA_CR_OVERFLOW_VSYNC_START2_MASK 0x80

#define VGA_CR_OVERFLOW_HEIGHT1_SHIFT 7
#define VGA_CR_OVERFLOW_HEIGHT1_MASK 0x02
#define VGA_CR_OVERFLOW_HEIGHT2_SHIFT 3
#define VGA_CR_OVERFLOW_HEIGHT2_MASK 0xc0
#define VGA_CR_OVERFLOW_LINE_COMPARE_SHIFT 4
#define VGA_CR_OVERFLOW_LINE_COMPARE_MASK 0x10

#define VGA_CR_CELL_HEIGHT_LINE_COMPARE_MASK 0x40
#define VGA_CR_CELL_HEIGHT_LINE_COMPARE_SHIFT 3
#define VGA_CR_CELL_HEIGHT_VERTICAL_BLANK_MASK 0x20
#define VGA_CR_CELL_HEIGHT_VERTICAL_BLANK_SHIFT 4
#define VGA_CR_CELL_HEIGHT_DOUBLE_SCAN 0x80
enum
  {
    VGA_CR_CURSOR_START_DISABLE = (1 << 5)
  };

#define VGA_CR_PITCH_DIVISOR 8

enum
  {
    VGA_CR_MODE_NO_CGA = 0x01,
    VGA_CR_MODE_NO_HERCULES = 0x02,
    VGA_CR_MODE_ADDRESS_WRAP = 0x20,
    VGA_CR_MODE_BYTE_MODE = 0x40,
    VGA_CR_MODE_TIMING_ENABLE = 0x80
  };

enum
  {
    VGA_SR_RESET = 0,
    VGA_SR_CLOCKING_MODE = 1,
    VGA_SR_MAP_MASK_REGISTER = 2,
    VGA_SR_CHAR_MAP_SELECT = 3,
    VGA_SR_MEMORY_MODE = 4,
  };

enum
  {
    VGA_SR_RESET_ASYNC = 1,
    VGA_SR_RESET_SYNC = 2
  };

enum
  {
    VGA_SR_CLOCKING_MODE_8_DOT_CLOCK = 1
  };

enum
  {
    VGA_SR_MEMORY_MODE_NORMAL = 0,
    VGA_SR_MEMORY_MODE_EXTERNAL_VIDEO_MEMORY = 2,
    VGA_SR_MEMORY_MODE_SEQUENTIAL_ADDRESSING = 4,
    VGA_SR_MEMORY_MODE_CHAIN4 = 8,
  };

enum
  {
    VGA_GR_SET_RESET_PLANE = 0,
    VGA_GR_SET_RESET_PLANE_ENABLE = 1,
    VGA_GR_COLOR_COMPARE = 2,
    VGA_GR_READ_MAP_REGISTER = 4,
    VGA_GR_MODE = 5,
    VGA_GR_GR6 = 6,
    VGA_GR_COLOR_COMPARE_DISABLE = 7,
    VGA_GR_BITMASK = 8,
    VGA_GR_MAX
  };

enum
  {
    VGA_TEXT_TEXT_PLANE = 0,
    VGA_TEXT_ATTR_PLANE = 1,
    VGA_TEXT_FONT_PLANE = 2
  };

enum
  {
    VGA_GR_GR6_GRAPHICS_MODE = 1,
    VGA_GR_GR6_MMAP_A0 = (1 << 2),
    VGA_GR_GR6_MMAP_CGA = (3 << 2)
  };

enum
  {
    VGA_GR_MODE_READ_MODE1 = 0x08,
    VGA_GR_MODE_ODD_EVEN = 0x10,
    VGA_GR_MODE_ODD_EVEN_SHIFT = 0x20,
    VGA_GR_MODE_256_COLOR = 0x40
  };

#define CIRRUS_CR_EXTENDED_DISPLAY 0x1b
#define CIRRUS_CR_EXTENDED_OVERLAY 0x1d

#define CIRRUS_CR_EXTENDED_DISPLAY_PITCH_MASK 0x10
#define CIRRUS_CR_EXTENDED_DISPLAY_PITCH_SHIFT 4
#define CIRRUS_CR_EXTENDED_DISPLAY_START_MASK1 0x1
#define CIRRUS_CR_EXTENDED_DISPLAY_START_SHIFT1 16
#define CIRRUS_CR_EXTENDED_DISPLAY_START_MASK2 0xc
#define CIRRUS_CR_EXTENDED_DISPLAY_START_SHIFT2 15

#define CIRRUS_CR_EXTENDED_OVERLAY_DISPLAY_START_MASK 0x80
#define CIRRUS_CR_EXTENDED_OVERLAY_DISPLAY_START_SHIFT 12
#define CIRRUS_SR_EXTENDED_MODE 7
#define CIRRUS_SR_EXTENDED_MODE_LFB_ENABLE 0xf0
#define CIRRUS_SR_EXTENDED_MODE_ENABLE_EXT 0x01
#define CIRRUS_SR_EXTENDED_MODE_32BPP      0x08
#define CIRRUS_HIDDEN_DAC_888COLOR 0xc5

static void
write_hidden_dac (uint8_t data)
{
	inb (0x3c8);
	inb (0x3c6);
	inb (0x3c6);
	inb (0x3c6);
	inb (0x3c6);
	outb (data, 0x3c6);
}

static void cirrus_init_linear_fb(struct device *dev)
{
	uint8_t cr_ext, cr_overlay;
	unsigned pitch = (width * 4) / VGA_CR_PITCH_DIVISOR;
	uint8_t sr_ext = 0, hidden_dac = 0;
	unsigned vdisplay_end = height - 2;
	unsigned line_compare = 0x3ff;
	uint8_t overflow, cell_height_reg;
	unsigned horizontal_end = width / VGA_CR_WIDTH_DIVISOR;
	unsigned horizontal_total = horizontal_end + 40;
	unsigned horizontal_blank_start = horizontal_end;
	unsigned horizontal_sync_pulse_start = horizontal_end + 3;
	unsigned horizontal_sync_pulse_end = 0;

	unsigned horizontal_blank_end = 0;
	unsigned vertical_blank_start = height + 1;
	unsigned vertical_blank_end = 0;
	unsigned vertical_sync_start = height + 3;
	unsigned vertical_sync_end = 0;
	unsigned vertical_total = height + 40;

	/* find lfb pci bar */
	addr = pci_read_config32(dev, PCI_BASE_ADDRESS_0);
	addr &= ~PCI_BASE_ADDRESS_MEM_ATTR_MASK;
	printk(BIOS_DEBUG, "QEMU VGA: cirrus framebuffer @ %x (pci bar 0)\n",
	       addr);

	vga_misc_write (VGA_IO_MISC_COLOR);

	vga_sr_write (VGA_SR_MEMORY_MODE,
		      VGA_SR_MEMORY_MODE_NORMAL);

	vga_sr_write (VGA_SR_MAP_MASK_REGISTER,
		      (1 << VGA_TEXT_TEXT_PLANE)
		      | (1 << VGA_TEXT_ATTR_PLANE));

	vga_sr_write (VGA_SR_CLOCKING_MODE,
		      VGA_SR_CLOCKING_MODE_8_DOT_CLOCK);

	vga_palette_disable();

	/* Disable CR0-7 write protection.  */
	vga_cr_write (VGA_CR_VSYNC_END, 0);

	overflow = ((vertical_total >> VGA_CR_OVERFLOW_VERT_TOTAL1_SHIFT)
		    & VGA_CR_OVERFLOW_VERT_TOTAL1_MASK)
		| ((vertical_total >> VGA_CR_OVERFLOW_VERT_TOTAL2_SHIFT)
		   & VGA_CR_OVERFLOW_VERT_TOTAL2_MASK)
		| ((vertical_sync_start >> VGA_CR_OVERFLOW_VSYNC_START2_SHIFT)
		   & VGA_CR_OVERFLOW_VSYNC_START2_MASK)
		| ((vertical_sync_start >> VGA_CR_OVERFLOW_VSYNC_START1_SHIFT)
		   & VGA_CR_OVERFLOW_VSYNC_START1_MASK)
		| ((vdisplay_end >> VGA_CR_OVERFLOW_VERT_DISPLAY_ENABLE_END1_SHIFT)
		   & VGA_CR_OVERFLOW_VERT_DISPLAY_ENABLE_END1_MASK)
		| ((vdisplay_end >> VGA_CR_OVERFLOW_VERT_DISPLAY_ENABLE_END2_SHIFT)
		   & VGA_CR_OVERFLOW_VERT_DISPLAY_ENABLE_END2_MASK)
		| ((vertical_sync_start >> VGA_CR_OVERFLOW_VSYNC_START1_SHIFT)
		   & VGA_CR_OVERFLOW_VSYNC_START1_MASK)
		| ((line_compare >> VGA_CR_OVERFLOW_LINE_COMPARE_SHIFT)
		   & VGA_CR_OVERFLOW_LINE_COMPARE_MASK);

	cell_height_reg = ((vertical_blank_start
			    >> VGA_CR_CELL_HEIGHT_VERTICAL_BLANK_SHIFT)
			   & VGA_CR_CELL_HEIGHT_VERTICAL_BLANK_MASK)
		| ((line_compare >> VGA_CR_CELL_HEIGHT_LINE_COMPARE_SHIFT)
		   & VGA_CR_CELL_HEIGHT_LINE_COMPARE_MASK);

	vga_cr_write (VGA_CR_HTOTAL, horizontal_total - 1);
	vga_cr_write (VGA_CR_HORIZ_END, horizontal_end - 1);
	vga_cr_write (VGA_CR_HBLANK_START, horizontal_blank_start - 1);
	vga_cr_write (VGA_CR_HBLANK_END, horizontal_blank_end);
	vga_cr_write (VGA_CR_HORIZ_SYNC_PULSE_START,
		      horizontal_sync_pulse_start);
	vga_cr_write (VGA_CR_HORIZ_SYNC_PULSE_END,
		      horizontal_sync_pulse_end);
	vga_cr_write (VGA_CR_VERT_TOTAL, vertical_total & 0xff);
	vga_cr_write (VGA_CR_OVERFLOW, overflow);
	vga_cr_write (VGA_CR_CELL_HEIGHT, cell_height_reg);
	vga_cr_write (VGA_CR_VSYNC_START, vertical_sync_start & 0xff);
	vga_cr_write (VGA_CR_VSYNC_END, vertical_sync_end & 0x0f);
	vga_cr_write (VGA_CR_VDISPLAY_END, vdisplay_end & 0xff);
	vga_cr_write (VGA_CR_PITCH, pitch & 0xff);
	vga_cr_write (VGA_CR_VERTICAL_BLANK_START, vertical_blank_start & 0xff);
	vga_cr_write (VGA_CR_VERTICAL_BLANK_END, vertical_blank_end & 0xff);
	vga_cr_write (VGA_CR_LINE_COMPARE, line_compare & 0xff);

	vga_gr_write (VGA_GR_MODE, VGA_GR_MODE_256_COLOR | VGA_GR_MODE_READ_MODE1);
	vga_gr_write (VGA_GR_GR6, VGA_GR_GR6_GRAPHICS_MODE);

	vga_sr_write (VGA_SR_MEMORY_MODE, VGA_SR_MEMORY_MODE_NORMAL);

	vga_cr_write (CIRRUS_CR_EXTENDED_DISPLAY,
		      (pitch >> CIRRUS_CR_EXTENDED_DISPLAY_PITCH_SHIFT)
		      & CIRRUS_CR_EXTENDED_DISPLAY_PITCH_MASK);

	vga_cr_write (VGA_CR_MODE, VGA_CR_MODE_TIMING_ENABLE
		      | VGA_CR_MODE_BYTE_MODE
		      | VGA_CR_MODE_NO_HERCULES | VGA_CR_MODE_NO_CGA);

	vga_cr_write (VGA_CR_START_ADDR_LOW_REGISTER, 0);
	vga_cr_write (VGA_CR_START_ADDR_HIGH_REGISTER, 0);

	cr_ext = vga_cr_read (CIRRUS_CR_EXTENDED_DISPLAY);
	cr_ext &= ~(CIRRUS_CR_EXTENDED_DISPLAY_START_MASK1
		    | CIRRUS_CR_EXTENDED_DISPLAY_START_MASK2);
	vga_cr_write (CIRRUS_CR_EXTENDED_DISPLAY, cr_ext);

	cr_overlay = vga_cr_read (CIRRUS_CR_EXTENDED_OVERLAY);
	cr_overlay &= ~(CIRRUS_CR_EXTENDED_OVERLAY_DISPLAY_START_MASK);
	vga_cr_write (CIRRUS_CR_EXTENDED_OVERLAY, cr_overlay);

	sr_ext = CIRRUS_SR_EXTENDED_MODE_LFB_ENABLE
		| CIRRUS_SR_EXTENDED_MODE_ENABLE_EXT
		| CIRRUS_SR_EXTENDED_MODE_32BPP;
	hidden_dac = CIRRUS_HIDDEN_DAC_888COLOR;
	vga_sr_write (CIRRUS_SR_EXTENDED_MODE, sr_ext);
	write_hidden_dac (hidden_dac);


	struct edid edid;
	edid.mode.ha = width;
	edid.mode.va = height;
	edid.panel_bits_per_color = 8;
	edid.panel_bits_per_pixel = 24;
	edid_set_framebuffer_bits_per_pixel(&edid, 32, 0);
	set_vbe_mode_info_valid(&edid, addr);
}

static void cirrus_init_text_mode(struct device *dev)
{
	vga_misc_write(0x1);
	vga_textmode_init();
}

static void cirrus_init(struct device *dev)
{
	if (IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER))
		cirrus_init_linear_fb(dev);
	else if (IS_ENABLED(CONFIG_VGA_TEXT_FRAMEBUFFER))
		cirrus_init_text_mode(dev);
}

static struct device_operations qemu_cirrus_graph_ops = {
	.read_resources	  = pci_dev_read_resources,
	.set_resources	  = pci_dev_set_resources,
	.enable_resources = pci_dev_enable_resources,
	.init		  = cirrus_init,
	.scan_bus	  = 0,
};

static const struct pci_driver qemu_cirrus_driver __pci_driver = {
	.ops	= &qemu_cirrus_graph_ops,
	.vendor = 0x1013,
	.device = 0x00b8,
};