10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*1106Smrj * Common Development and Distribution License (the "License").
6*1106Smrj * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*1106Smrj
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * Support routines for VGA drivers
310Sstevel@tonic-gate */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #include <sys/debug.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #include <sys/time.h>
370Sstevel@tonic-gate #include <sys/buf.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/conf.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <sys/vgareg.h>
430Sstevel@tonic-gate #include <sys/vgasubr.h>
440Sstevel@tonic-gate #include <sys/cmn_err.h>
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include <sys/kmem.h>
470Sstevel@tonic-gate #include <sys/conf.h>
480Sstevel@tonic-gate #include <sys/ddi.h>
490Sstevel@tonic-gate #include <sys/devops.h>
500Sstevel@tonic-gate #include <sys/sunddi.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate #include <sys/modctl.h>
530Sstevel@tonic-gate
540Sstevel@tonic-gate #define GET_HORIZ_END(c) vga_get_crtc(c, VGA_CRTC_H_D_END)
550Sstevel@tonic-gate #define GET_VERT_END(c) (vga_get_crtc(c, VGA_CRTC_VDE) \
560Sstevel@tonic-gate + (((vga_get_crtc(c, VGA_CRTC_OVFL_REG) >> \
570Sstevel@tonic-gate VGA_CRTC_OVFL_REG_VDE8) & 1) << 8) \
580Sstevel@tonic-gate + (((vga_get_crtc(c, VGA_CRTC_OVFL_REG) >> \
590Sstevel@tonic-gate VGA_CRTC_OVFL_REG_VDE9) & 1) << 9))
600Sstevel@tonic-gate
610Sstevel@tonic-gate #define GET_VERT_X2(c) \
620Sstevel@tonic-gate (vga_get_crtc(c, VGA_CRTC_CRT_MD) & VGA_CRTC_CRT_MD_VT_X2)
630Sstevel@tonic-gate
640Sstevel@tonic-gate unsigned char VGA_CRTC_TEXT[NUM_CRTC_REG] = {
650Sstevel@tonic-gate 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
660Sstevel@tonic-gate 0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x05, 0x00,
670Sstevel@tonic-gate 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
680Sstevel@tonic-gate 0xff };
690Sstevel@tonic-gate unsigned char VGA_SEQ_TEXT[NUM_SEQ_REG] = {
700Sstevel@tonic-gate 0x03, 0x00, 0x03, 0x00, 0x02 };
710Sstevel@tonic-gate unsigned char VGA_GRC_TEXT[NUM_GRC_REG] = {
720Sstevel@tonic-gate 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0e, 0x00, 0xff };
730Sstevel@tonic-gate unsigned char VGA_ATR_TEXT[NUM_ATR_REG] = {
740Sstevel@tonic-gate 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
750Sstevel@tonic-gate 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
760Sstevel@tonic-gate 0x0c, 0x00, 0x0f, 0x08, 0x00 };
770Sstevel@tonic-gate
780Sstevel@tonic-gate void
vga_get_hardware_settings(struct vgaregmap * reg,int * width,int * height)790Sstevel@tonic-gate vga_get_hardware_settings(struct vgaregmap *reg, int *width, int *height)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate *width = (GET_HORIZ_END(reg)+1)*8;
820Sstevel@tonic-gate *height = GET_VERT_END(reg)+1;
830Sstevel@tonic-gate if (GET_VERT_X2(reg)) *height *= 2;
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
86*1106Smrj #define PUTB(reg, off, v) ddi_put8(reg->handle, reg->addr + (off), v)
87*1106Smrj #define GETB(reg, off) ddi_get8(reg->handle, reg->addr + (off))
880Sstevel@tonic-gate
890Sstevel@tonic-gate int
vga_get_reg(struct vgaregmap * reg,int indexreg)900Sstevel@tonic-gate vga_get_reg(struct vgaregmap *reg, int indexreg)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate return (GETB(reg, indexreg));
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate void
vga_set_reg(struct vgaregmap * reg,int indexreg,int v)960Sstevel@tonic-gate vga_set_reg(struct vgaregmap *reg, int indexreg, int v)
970Sstevel@tonic-gate {
980Sstevel@tonic-gate PUTB(reg, indexreg, v);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate int
vga_get_crtc(struct vgaregmap * reg,int i)1020Sstevel@tonic-gate vga_get_crtc(struct vgaregmap *reg, int i)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate return (vga_get_indexed(reg, VGA_CRTC_ADR, VGA_CRTC_DATA, i));
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate void
vga_set_crtc(struct vgaregmap * reg,int i,int v)1080Sstevel@tonic-gate vga_set_crtc(struct vgaregmap *reg, int i, int v)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate vga_set_indexed(reg, VGA_CRTC_ADR, VGA_CRTC_DATA, i, v);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate
1130Sstevel@tonic-gate int
vga_get_seq(struct vgaregmap * reg,int i)1140Sstevel@tonic-gate vga_get_seq(struct vgaregmap *reg, int i)
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate return (vga_get_indexed(reg, VGA_SEQ_ADR, VGA_SEQ_DATA, i));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate void
vga_set_seq(struct vgaregmap * reg,int i,int v)1200Sstevel@tonic-gate vga_set_seq(struct vgaregmap *reg, int i, int v)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate vga_set_indexed(reg, VGA_SEQ_ADR, VGA_SEQ_DATA, i, v);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate int
vga_get_grc(struct vgaregmap * reg,int i)1260Sstevel@tonic-gate vga_get_grc(struct vgaregmap *reg, int i)
1270Sstevel@tonic-gate {
1280Sstevel@tonic-gate return (vga_get_indexed(reg, VGA_GRC_ADR, VGA_GRC_DATA, i));
1290Sstevel@tonic-gate }
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate void
vga_set_grc(struct vgaregmap * reg,int i,int v)1320Sstevel@tonic-gate vga_set_grc(struct vgaregmap *reg, int i, int v)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate vga_set_indexed(reg, VGA_GRC_ADR, VGA_GRC_DATA, i, v);
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate int
vga_get_atr(struct vgaregmap * reg,int i)1380Sstevel@tonic-gate vga_get_atr(struct vgaregmap *reg, int i)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate int ret;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate (void) GETB(reg, CGA_STAT);
1430Sstevel@tonic-gate PUTB(reg, VGA_ATR_AD, i);
1440Sstevel@tonic-gate ret = GETB(reg, VGA_ATR_DATA);
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate (void) GETB(reg, CGA_STAT);
1470Sstevel@tonic-gate PUTB(reg, VGA_ATR_AD, VGA_ATR_ENB_PLT);
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate return (ret);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate void
vga_set_atr(struct vgaregmap * reg,int i,int v)1530Sstevel@tonic-gate vga_set_atr(struct vgaregmap *reg, int i, int v)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate (void) GETB(reg, CGA_STAT);
1560Sstevel@tonic-gate PUTB(reg, VGA_ATR_AD, i);
1570Sstevel@tonic-gate PUTB(reg, VGA_ATR_AD, v);
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate (void) GETB(reg, CGA_STAT);
1600Sstevel@tonic-gate PUTB(reg, VGA_ATR_AD, VGA_ATR_ENB_PLT);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate void
vga_set_indexed(struct vgaregmap * reg,int indexreg,int datareg,unsigned char index,unsigned char val)1640Sstevel@tonic-gate vga_set_indexed(
1650Sstevel@tonic-gate struct vgaregmap *reg,
1660Sstevel@tonic-gate int indexreg,
1670Sstevel@tonic-gate int datareg,
1680Sstevel@tonic-gate unsigned char index,
1690Sstevel@tonic-gate unsigned char val)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate PUTB(reg, indexreg, index);
1720Sstevel@tonic-gate PUTB(reg, datareg, val);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate int
vga_get_indexed(struct vgaregmap * reg,int indexreg,int datareg,unsigned char index)1760Sstevel@tonic-gate vga_get_indexed(
1770Sstevel@tonic-gate struct vgaregmap *reg,
1780Sstevel@tonic-gate int indexreg,
1790Sstevel@tonic-gate int datareg,
1800Sstevel@tonic-gate unsigned char index)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate PUTB(reg, indexreg, index);
1830Sstevel@tonic-gate return (GETB(reg, datareg));
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate * VGA DAC access functions
1880Sstevel@tonic-gate * Note: These assume a VGA-style 6-bit DAC. Some DACs are 8 bits
1890Sstevel@tonic-gate * wide. These functions are not appropriate for those DACs.
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate void
vga_put_cmap(struct vgaregmap * reg,int index,unsigned char r,unsigned char g,unsigned char b)1920Sstevel@tonic-gate vga_put_cmap(
1930Sstevel@tonic-gate struct vgaregmap *reg,
1940Sstevel@tonic-gate int index,
1950Sstevel@tonic-gate unsigned char r,
1960Sstevel@tonic-gate unsigned char g,
1970Sstevel@tonic-gate unsigned char b)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate PUTB(reg, VGA_DAC_WR_AD, index);
2010Sstevel@tonic-gate PUTB(reg, VGA_DAC_DATA, r >> 2);
2020Sstevel@tonic-gate PUTB(reg, VGA_DAC_DATA, g >> 2);
2030Sstevel@tonic-gate PUTB(reg, VGA_DAC_DATA, b >> 2);
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate void
vga_get_cmap(struct vgaregmap * reg,int index,unsigned char * r,unsigned char * g,unsigned char * b)2070Sstevel@tonic-gate vga_get_cmap(
2080Sstevel@tonic-gate struct vgaregmap *reg,
2090Sstevel@tonic-gate int index,
2100Sstevel@tonic-gate unsigned char *r,
2110Sstevel@tonic-gate unsigned char *g,
2120Sstevel@tonic-gate unsigned char *b)
2130Sstevel@tonic-gate {
2140Sstevel@tonic-gate PUTB(reg, VGA_DAC_RD_AD, index);
2150Sstevel@tonic-gate *r = GETB(reg, VGA_DAC_DATA) << 2;
2160Sstevel@tonic-gate *g = GETB(reg, VGA_DAC_DATA) << 2;
2170Sstevel@tonic-gate *b = GETB(reg, VGA_DAC_DATA) << 2;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate #ifdef DEBUG
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate void
vga_dump_regs(struct vgaregmap * reg,int maxseq,int maxcrtc,int maxatr,int maxgrc)2230Sstevel@tonic-gate vga_dump_regs(struct vgaregmap *reg,
2240Sstevel@tonic-gate int maxseq, int maxcrtc, int maxatr, int maxgrc)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate int i, j;
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate printf("Sequencer regs:\n");
2290Sstevel@tonic-gate for (i = 0; i < maxseq; i += 0x10) {
2300Sstevel@tonic-gate printf("%2x: ", i);
2310Sstevel@tonic-gate for (j = 0; j < 0x08; j++) {
2320Sstevel@tonic-gate printf("%2x ", vga_get_seq(reg, i+j));
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate printf("- ");
2350Sstevel@tonic-gate for (; j < 0x10; j++) {
2360Sstevel@tonic-gate printf("%2x ", vga_get_seq(reg, i+j));
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate printf("\n");
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate printf("\nCRT Controller regs:\n");
2410Sstevel@tonic-gate for (i = 0; i < maxcrtc; i += 0x10) {
2420Sstevel@tonic-gate printf("%2x: ", i);
2430Sstevel@tonic-gate for (j = 0; j < 0x08; j++) {
2440Sstevel@tonic-gate printf("%2x ", vga_get_crtc(reg, i+j));
2450Sstevel@tonic-gate }
2460Sstevel@tonic-gate printf("- ");
2470Sstevel@tonic-gate for (; j < 0x10; j++) {
2480Sstevel@tonic-gate printf("%2x ", vga_get_crtc(reg, i+j));
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate printf("\n");
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate printf("\nAttribute Controller regs:\n");
2530Sstevel@tonic-gate for (i = 0; i < maxatr; i += 0x10) {
2540Sstevel@tonic-gate printf("%2x: ", i);
2550Sstevel@tonic-gate for (j = 0; j < 0x08; j++) {
2560Sstevel@tonic-gate printf("%2x ", vga_get_atr(reg, i+j));
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate printf("- ");
2590Sstevel@tonic-gate for (; j < 0x10; j++) {
2600Sstevel@tonic-gate printf("%2x ", vga_get_atr(reg, i+j));
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate printf("\n");
2630Sstevel@tonic-gate }
2640Sstevel@tonic-gate printf("\nGraphics Controller regs:\n");
2650Sstevel@tonic-gate for (i = 0; i < maxgrc; i += 0x10) {
2660Sstevel@tonic-gate printf("%2x: ", i);
2670Sstevel@tonic-gate for (j = 0; j < 0x08; j++) {
2680Sstevel@tonic-gate printf("%2x ", vga_get_grc(reg, i+j));
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate printf("- ");
2710Sstevel@tonic-gate for (; j < 0x10; j++) {
2720Sstevel@tonic-gate printf("%2x ", vga_get_grc(reg, i+j));
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate printf("\n");
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate #endif /* DEBUG */
2780Sstevel@tonic-gate
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate * VGA 80X25 text mode standard palette
2810Sstevel@tonic-gate */
2820Sstevel@tonic-gate unsigned char VGA_TEXT_PALETTES[64][3] = {
2830Sstevel@tonic-gate { 0x00, 0x00, 0x00 },
2840Sstevel@tonic-gate { 0x00, 0x00, 0x2A },
2850Sstevel@tonic-gate { 0x00, 0x2A, 0x00 },
2860Sstevel@tonic-gate { 0x00, 0x2A, 0x2A },
2870Sstevel@tonic-gate { 0x2A, 0x00, 0x00 },
2880Sstevel@tonic-gate { 0x2A, 0x00, 0x2A },
2890Sstevel@tonic-gate { 0x2A, 0x2A, 0x00 },
2900Sstevel@tonic-gate { 0x2A, 0x2A, 0x2A },
2910Sstevel@tonic-gate { 0x00, 0x00, 0x15 },
2920Sstevel@tonic-gate { 0x00, 0x00, 0x3F },
2930Sstevel@tonic-gate { 0x00, 0x2A, 0x15 },
2940Sstevel@tonic-gate { 0x00, 0x2A, 0x3F },
2950Sstevel@tonic-gate { 0x2A, 0x00, 0x15 },
2960Sstevel@tonic-gate { 0x2A, 0x00, 0x3F },
2970Sstevel@tonic-gate { 0x2A, 0x2A, 0x15 },
2980Sstevel@tonic-gate { 0x2A, 0x2A, 0x3F },
2990Sstevel@tonic-gate { 0x00, 0x15, 0x00 },
3000Sstevel@tonic-gate { 0x00, 0x15, 0x2A },
3010Sstevel@tonic-gate { 0x00, 0x3F, 0x00 },
3020Sstevel@tonic-gate { 0x00, 0x3F, 0x2A },
3030Sstevel@tonic-gate { 0x2A, 0x15, 0x00 },
3040Sstevel@tonic-gate { 0x2A, 0x15, 0x2A },
3050Sstevel@tonic-gate { 0x2A, 0x3F, 0x00 },
3060Sstevel@tonic-gate { 0x2A, 0x3F, 0x2A },
3070Sstevel@tonic-gate { 0x00, 0x15, 0x15 },
3080Sstevel@tonic-gate { 0x00, 0x15, 0x3F },
3090Sstevel@tonic-gate { 0x00, 0x3F, 0x15 },
3100Sstevel@tonic-gate { 0x00, 0x3F, 0x3F },
3110Sstevel@tonic-gate { 0x2A, 0x15, 0x15 },
3120Sstevel@tonic-gate { 0x2A, 0x15, 0x3F },
3130Sstevel@tonic-gate { 0x2A, 0x3F, 0x15 },
3140Sstevel@tonic-gate { 0x2A, 0x3F, 0x3F },
3150Sstevel@tonic-gate { 0x15, 0x00, 0x00 },
3160Sstevel@tonic-gate { 0x15, 0x00, 0x2A },
3170Sstevel@tonic-gate { 0x15, 0x2A, 0x00 },
3180Sstevel@tonic-gate { 0x15, 0x2A, 0x2A },
3190Sstevel@tonic-gate { 0x3F, 0x00, 0x00 },
3200Sstevel@tonic-gate { 0x3F, 0x00, 0x2A },
3210Sstevel@tonic-gate { 0x3F, 0x2A, 0x00 },
3220Sstevel@tonic-gate { 0x3F, 0x2A, 0x2A },
3230Sstevel@tonic-gate { 0x15, 0x00, 0x15 },
3240Sstevel@tonic-gate { 0x15, 0x00, 0x3F },
3250Sstevel@tonic-gate { 0x15, 0x2A, 0x15 },
3260Sstevel@tonic-gate { 0x15, 0x2A, 0x3F },
3270Sstevel@tonic-gate { 0x3F, 0x00, 0x15 },
3280Sstevel@tonic-gate { 0x3F, 0x00, 0x3F },
3290Sstevel@tonic-gate { 0x3F, 0x2A, 0x15 },
3300Sstevel@tonic-gate { 0x3F, 0x2A, 0x3F },
3310Sstevel@tonic-gate { 0x15, 0x15, 0x00 },
3320Sstevel@tonic-gate { 0x15, 0x15, 0x2A },
3330Sstevel@tonic-gate { 0x15, 0x3F, 0x00 },
3340Sstevel@tonic-gate { 0x15, 0x3F, 0x2A },
3350Sstevel@tonic-gate { 0x3F, 0x15, 0x00 },
3360Sstevel@tonic-gate { 0x3F, 0x15, 0x2A },
3370Sstevel@tonic-gate { 0x3F, 0x3F, 0x00 },
3380Sstevel@tonic-gate { 0x3F, 0x3F, 0x2A },
3390Sstevel@tonic-gate { 0x15, 0x15, 0x15 },
3400Sstevel@tonic-gate { 0x15, 0x15, 0x3F },
3410Sstevel@tonic-gate { 0x15, 0x3F, 0x15 },
3420Sstevel@tonic-gate { 0x15, 0x3F, 0x3F },
3430Sstevel@tonic-gate { 0x3F, 0x15, 0x15 },
3440Sstevel@tonic-gate { 0x3F, 0x15, 0x3F },
3450Sstevel@tonic-gate { 0x3F, 0x3F, 0x15 },
3460Sstevel@tonic-gate { 0x3F, 0x3F, 0x3F }
3470Sstevel@tonic-gate };
348