summaryrefslogtreecommitdiff
path: root/usr/src/common/vga/vgasubr.c
blob: ccd5e56ddc7585543e67f92ae6cab39cb0ea2274 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2018 Toomas Soome <tsoome@me.com>
 */

/*
 * Support routines for VGA drivers
 */

#if defined(_KERNEL)
#include <sys/debug.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/buf.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/conf.h>

#include <sys/cmn_err.h>

#include <sys/kmem.h>
#include <sys/conf.h>
#include <sys/ddi.h>
#include <sys/devops.h>
#include <sys/sunddi.h>

#include <sys/modctl.h>

#define	PUTB(reg, off, v)	ddi_put8(reg->handle, reg->addr + (off), v)
#define	GETB(reg, off)		ddi_get8(reg->handle, reg->addr + (off))

#elif defined(_STANDALONE)

#include <stand.h>
#include <machine/cpufunc.h>

#define	PUTB(reg, off, v)	outb(reg + (off), v)
#define	GETB(reg, off)		inb(reg + (off))
#endif

#include <sys/vgareg.h>
#include <sys/vgasubr.h>

#define	GET_HORIZ_END(c)	vga_get_crtc(c, VGA_CRTC_H_D_END)
#define	GET_VERT_END(c)	(vga_get_crtc(c, VGA_CRTC_VDE) \
	+ (((vga_get_crtc(c, VGA_CRTC_OVFL_REG) >> \
	    VGA_CRTC_OVFL_REG_VDE8) & 1) << 8) \
	+ (((vga_get_crtc(c, VGA_CRTC_OVFL_REG) >> \
	    VGA_CRTC_OVFL_REG_VDE9) & 1) << 9))

#define	GET_VERT_X2(c)	\
	(vga_get_crtc(c, VGA_CRTC_CRT_MD) & VGA_CRTC_CRT_MD_VT_X2)

unsigned char VGA_CRTC_TEXT[NUM_CRTC_REG] = {
	0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
	0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x05, 0x00,
	0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
	0xff };
unsigned char VGA_SEQ_TEXT[NUM_SEQ_REG] = {
	0x03, 0x00, 0x03, 0x00, 0x02 };
unsigned char VGA_GRC_TEXT[NUM_GRC_REG] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0e, 0x00, 0xff };
unsigned char VGA_ATR_TEXT[NUM_ATR_REG] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
	0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
	0x0c, 0x00, 0x0f, 0x08, 0x00 };

void
vga_get_hardware_settings(vgaregmap_t reg, int *width, int *height)
{
	*width = (GET_HORIZ_END(reg)+1)*8;
	*height = GET_VERT_END(reg)+1;
	if (GET_VERT_X2(reg)) *height *= 2;
}

int
vga_get_reg(vgaregmap_t reg, int indexreg)
{
	return (GETB(reg, indexreg));
}

void
vga_set_reg(vgaregmap_t reg, int indexreg, int v)
{
	PUTB(reg, indexreg, v);
}

int
vga_get_crtc(vgaregmap_t reg, int i)
{
	return (vga_get_indexed(reg, VGA_CRTC_ADR, VGA_CRTC_DATA, i));
}

void
vga_set_crtc(vgaregmap_t reg, int i, int v)
{
	vga_set_indexed(reg, VGA_CRTC_ADR, VGA_CRTC_DATA, i, v);
}

int
vga_get_seq(vgaregmap_t reg, int i)
{
	return (vga_get_indexed(reg, VGA_SEQ_ADR, VGA_SEQ_DATA, i));
}

void
vga_set_seq(vgaregmap_t reg, int i, int v)
{
	vga_set_indexed(reg, VGA_SEQ_ADR, VGA_SEQ_DATA, i, v);
}

int
vga_get_grc(vgaregmap_t reg, int i)
{
	return (vga_get_indexed(reg, VGA_GRC_ADR, VGA_GRC_DATA, i));
}

void
vga_set_grc(vgaregmap_t reg, int i, int v)
{
	vga_set_indexed(reg, VGA_GRC_ADR, VGA_GRC_DATA, i, v);
}

int
vga_get_atr(vgaregmap_t reg, int i)
{
	int ret;

	(void) GETB(reg, CGA_STAT);
	PUTB(reg, VGA_ATR_AD, i);
	ret = GETB(reg, VGA_ATR_DATA);

	(void) GETB(reg, CGA_STAT);
	PUTB(reg, VGA_ATR_AD, VGA_ATR_ENB_PLT);

	return (ret);
}

void
vga_set_atr(vgaregmap_t reg, int i, int v)
{
	(void) GETB(reg, CGA_STAT);
	PUTB(reg, VGA_ATR_AD, i);
	PUTB(reg, VGA_ATR_AD, v);

	(void) GETB(reg, CGA_STAT);
	PUTB(reg, VGA_ATR_AD, VGA_ATR_ENB_PLT);
}

void
vga_set_indexed(
	vgaregmap_t reg,
	int indexreg,
	int datareg,
	unsigned char index,
	unsigned char val)
{
	PUTB(reg, indexreg, index);
	PUTB(reg, datareg, val);
}

int
vga_get_indexed(
	vgaregmap_t reg,
	int indexreg,
	int datareg,
	unsigned char index)
{
	PUTB(reg, indexreg, index);
	return (GETB(reg, datareg));
}

/*
 * VGA DAC access functions
 * Note:  These assume a VGA-style 6-bit DAC.  Some DACs are 8 bits
 * wide.  These functions are not appropriate for those DACs.
 */
void
vga_put_cmap(
	vgaregmap_t reg,
	int index,
	unsigned char r,
	unsigned char g,
	unsigned char b)
{

	PUTB(reg, VGA_DAC_WR_AD, index);
	PUTB(reg, VGA_DAC_DATA, r >> 2);
	PUTB(reg, VGA_DAC_DATA, g >> 2);
	PUTB(reg, VGA_DAC_DATA, b >> 2);
}

void
vga_get_cmap(
	vgaregmap_t reg,
	int index,
	unsigned char *r,
	unsigned char *g,
	unsigned char *b)
{
	PUTB(reg, VGA_DAC_RD_AD, index);
	*r = GETB(reg, VGA_DAC_DATA) << 2;
	*g = GETB(reg, VGA_DAC_DATA) << 2;
	*b = GETB(reg, VGA_DAC_DATA) << 2;
}

#ifdef	DEBUG

void
vga_dump_regs(vgaregmap_t reg, int maxseq, int maxcrtc, int maxatr, int maxgrc)
{
	int i, j;

	printf("Sequencer regs:\n");
	for (i = 0; i < maxseq; i += 0x10) {
		printf("%2x:  ", i);
		for (j = 0; j < 0x08; j++) {
			printf("%2x ", vga_get_seq(reg, i+j));
		}
		printf("- ");
		for (; j < 0x10; j++) {
			printf("%2x ", vga_get_seq(reg, i+j));
		}
		printf("\n");
	}
	printf("\nCRT Controller regs:\n");
	for (i = 0; i < maxcrtc; i += 0x10) {
		printf("%2x:  ", i);
		for (j = 0; j < 0x08; j++) {
			printf("%2x ", vga_get_crtc(reg, i+j));
		}
		printf("- ");
		for (; j < 0x10; j++) {
			printf("%2x ", vga_get_crtc(reg, i+j));
		}
		printf("\n");
	}
	printf("\nAttribute Controller regs:\n");
	for (i = 0; i < maxatr; i += 0x10) {
		printf("%2x:  ", i);
		for (j = 0; j < 0x08; j++) {
			printf("%2x ", vga_get_atr(reg, i+j));
		}
		printf("- ");
		for (; j < 0x10; j++) {
			printf("%2x ", vga_get_atr(reg, i+j));
		}
		printf("\n");
	}
	printf("\nGraphics Controller regs:\n");
	for (i = 0; i < maxgrc; i += 0x10) {
		printf("%2x:  ", i);
		for (j = 0; j < 0x08; j++) {
			printf("%2x ", vga_get_grc(reg, i+j));
		}
		printf("- ");
		for (; j < 0x10; j++) {
			printf("%2x ", vga_get_grc(reg, i+j));
		}
		printf("\n");
	}
}
#endif	/* DEBUG */

/*
 * VGA 80X25 text mode standard palette
 */
unsigned char VGA_TEXT_PALETTES[64][3] = {
	{ 0x00, 0x00, 0x00 },
	{ 0x00, 0x00, 0x2A },
	{ 0x00, 0x2A, 0x00 },
	{ 0x00, 0x2A, 0x2A },
	{ 0x2A, 0x00, 0x00 },
	{ 0x2A, 0x00, 0x2A },
	{ 0x2A, 0x2A, 0x00 },
	{ 0x2A, 0x2A, 0x2A },
	{ 0x00, 0x00, 0x15 },
	{ 0x00, 0x00, 0x3F },
	{ 0x00, 0x2A, 0x15 },
	{ 0x00, 0x2A, 0x3F },
	{ 0x2A, 0x00, 0x15 },
	{ 0x2A, 0x00, 0x3F },
	{ 0x2A, 0x2A, 0x15 },
	{ 0x2A, 0x2A, 0x3F },
	{ 0x00, 0x15, 0x00 },
	{ 0x00, 0x15,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