#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vga_subr.c,v 1.26 2023/05/06 21:34:40 andvar Exp $");
#include "opt_wsdisplay_border.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/queue.h>
#include <sys/bus.h>
#include <dev/ic/mc6845reg.h>
#include <dev/ic/pcdisplay.h>
#include <dev/ic/pcdisplayvar.h>
#include <dev/ic/vgareg.h>
#include <dev/ic/vgavar.h>
#include <dev/wscons/wsdisplayvar.h>
static void fontram(struct vga_handle *);
static void textram(struct vga_handle *);
static void
fontram(struct vga_handle *vh)
{
vga_ts_write(vh, syncreset, 0x01);
vga_ts_write(vh, wrplmask, 0x04);
vga_ts_write(vh, memmode, 0x07);
vga_ts_write(vh, syncreset, 0x03);
vga_gdc_write(vh, rdplanesel, 0x02);
vga_gdc_write(vh, mode, 0x00);
vga_gdc_write(vh, misc, 0x04);
}
static void
textram(struct vga_handle *vh)
{
vga_ts_write(vh, syncreset, 0x01);
vga_ts_write(vh, wrplmask, 0x03);
vga_ts_write(vh, memmode, 0x03);
vga_ts_write(vh, syncreset, 0x03);
vga_gdc_write(vh, rdplanesel, 0x00);
vga_gdc_write(vh, mode, 0x10);
vga_gdc_write(vh, misc, (vh->vh_mono ? 0x0a : 0x0e));
}
#ifndef VGA_RASTERCONSOLE
void
vga_loadchars(struct vga_handle *vh, int fontset, int first, int num, int lpc,
const char *data)
{
int offset, i, j, s;
offset = (fontset << 13) | (first << 5);
s = splhigh();
fontram(vh);
for (i = 0; i < num; i++)
for (j = 0; j < lpc; j++)
bus_space_write_1(vh->vh_memt, vh->vh_allmemh,
offset + (i << 5) + j, data[i * lpc + j]);
textram(vh);
splx(s);
}
void
vga_readoutchars(struct vga_handle *vh, int fontset, int first, int num,
int lpc, char *data)
{
int offset, i, j, s;
offset = (fontset << 13) | (first << 5);
s = splhigh();
fontram(vh);
for (i = 0; i < num; i++)
for (j = 0; j < lpc; j++)
data[i * lpc + j] = bus_space_read_1(vh->vh_memt,
vh->vh_allmemh, offset + (i << 5) + j);
textram(vh);
splx(s);
}
#ifdef VGA_CONSOLE_ATI_BROKEN_FONTSEL
void
vga_copyfont01(struct vga_handle *vh)
{
int s;
s = splhigh();
fontram(vh);
bus_space_copy_region_1(vh->vh_memt, vh->vh_allmemh, 0,
vh->vh_allmemh, 1 << 13, 1 << 13);
textram(vh);
splx(s);
}
#endif
void
vga_setfontset(struct vga_handle *vh, int fontset1, int fontset2)
{
uint8_t cmap;
static const uint8_t cmaptaba[] = {
0x00, 0x10, 0x01, 0x11,
0x02, 0x12, 0x03, 0x13
};
static const uint8_t cmaptabb[] = {
0x00, 0x20, 0x04, 0x24,
0x08, 0x28, 0x0c, 0x2c
};
cmap = cmaptaba[fontset1] | cmaptabb[fontset2];
vga_ts_write(vh, fontsel, cmap);
}
void
vga_setscreentype(struct vga_handle *vh, const struct wsscreen_descr *type)
{
vga_6845_write(vh, maxrow, type->fontheight - 1);
vga_6845_write(vh, vde, type->fontheight * type->nrows - 1);
#ifndef PCDISPLAY_SOFTCURSOR
vga_6845_write(vh, curstart, type->fontheight - 2);
vga_6845_write(vh, curend, type->fontheight - 1);
#endif
if (type->capabilities & WSSCREEN_HILIT) {
vga_attr_write(vh, colplen, 0x0f);
} else
vga_attr_write(vh, colplen, 0x07);
}
#else
void
vga_load_builtinfont(struct vga_handle *vh, uint8_t *font, int firstchar,
int numchars)
{
int i, s;
s = splhigh();
fontram(vh);
for (i = firstchar; i < firstchar + numchars; i++)
bus_space_read_region_1(vh->vh_memt, vh->vh_allmemh, i * 32,
font + i * 16, 16);
textram(vh);
splx(s);
}
#endif
void
vga_reset(struct vga_handle *vh, void (*md_initfunc)(struct vga_handle *))
{
uint8_t reg;
if (bus_space_map(vh->vh_iot, 0x3c0, 0x10, 0, &vh->vh_ioh_vga))
return;
reg = vga_raw_read(vh, VGA_MISC_DATAR);
vh->vh_mono = !(reg & 0x01);
if (bus_space_map(vh->vh_iot, vh->vh_mono ? 0x3b0 : 0x3d0, 0x10,
0, &vh->vh_ioh_6845))
goto out1;
if (bus_space_map(vh->vh_memt, 0xa0000, 0x20000, 0, &vh->vh_allmemh))
goto out2;
if (bus_space_subregion(vh->vh_memt, vh->vh_allmemh,
vh->vh_mono ? 0x10000 : 0x18000, 0x8000, &vh->vh_memh))
goto out3;
if ((vga_gdc_read(vh, misc) & 0x01) == 0)
goto out3;
vga_initregs(vh);
if (md_initfunc != NULL)
(*md_initfunc)(vh);
delay(10000);
bus_space_set_region_2(vh->vh_memt, vh->vh_memh, 0,
((BG_BLACK | FG_LIGHTGREY) << 8) | ' ', 80 * 25 );
out3:
bus_space_unmap(vh->vh_memt, vh->vh_allmemh, 0x20000);
out2:
bus_space_unmap(vh->vh_iot, vh->vh_ioh_6845, 0x10);
out1:
bus_space_unmap(vh->vh_iot, vh->vh_ioh_vga, 0x10);
}
#define VGA_MISCOUT 0x66
static const uint8_t vga_ts[] = {
0x03,
0x00,
0x03,
0x00,
0x02
};
static const uint8_t vga_crtc[] = {
0x5f,
0x4f,
0x50,
0x82,
0x55,
0x81,
0xbf,
0x1f,
0x00,
0x4f,
0x0d,
0x0e,
0x00,
0x00,
0x00,
0x00,
0x9c,
0x8e,
0x8f,
0x28,
0x00,
0x96,
0xb9,
0xa3,
0xff
};
static const uint8_t vga_gdc[] = {
0x00,
0x00,
0x00,
0x00,
0x00,
0x10,
0x0e,
0x00,
0xff
};
static const uint8_t vga_atc[] = {
0x00,
0x01,
0x02,
0x03,
0x04,
0x05,
0x14,
0x07,
0x38,
0x39,
0x3a,
0x3b,
0x3c,
0x3d,
0x3e,
0x3f,
0x0c,
WSDISPLAY_BORDER_COLOR,
0x0f,
0x08,
0x00
};
static const uint8_t vga_dacpal[] = {
0x00, 0x00, 0x00,
0x00, 0x00, 0x2a,
0x00, 0x2a, 0x00,
0x00, 0x2a, 0x2a,
0x2a, 0x00, 0x00,
0x2a, 0x00, 0x2a,
0x2a, 0x15, 0x00,
0x2a, 0x2a, 0x2a,
0x15, 0x15, 0x15,
0x15, 0x15, 0x3f,
0x15, 0x3f, 0x15,
0x15, 0x3f, 0x3f,
0x3f, 0x15, 0x15,
0x3f, 0x15, 0x3f,
0x3f, 0x3f, 0x15,
0x3f, 0x3f, 0x3f
};
void
vga_initregs(struct vga_handle *vh)
{
int i;
vga_ts_write(vh, mode, vga_ts[1] | VGA_TS_MODE_BLANK);
vga_ts_write(vh, syncreset, 0x01);
for (i = 2; i < VGA_TS_NREGS; i++)
_vga_ts_write(vh, i, vga_ts[i]);
vga_ts_write(vh, syncreset, 0x03);
vga_6845_write(vh, vsynce, vga_6845_read(vh, vsynce) & ~0x80);
for (i = 0; i < MC6845_NREGS; i++)
_vga_6845_write(vh, i, vga_crtc[i]);
for (i = 0; i < VGA_GDC_NREGS; i++)
_vga_gdc_write(vh, i, vga_gdc[i]);
for (i = 0; i < VGA_ATC_NREGS; i++)
_vga_attr_write(vh, i, vga_atc[i]);
if (!vh->vh_mono) {
for (i = 0; i < 16; i++) {
vga_raw_write(vh,
VGA_DAC_ADDRW, vga_atc[i]);
vga_raw_write(vh,
VGA_DAC_PALETTE, vga_dacpal[i * 3 + 0]);
vga_raw_write(vh,
VGA_DAC_PALETTE, vga_dacpal[i * 3 + 1]);
vga_raw_write(vh,
VGA_DAC_PALETTE, vga_dacpal[i * 3 + 2]);
}
}
vga_raw_write(vh,
VGA_MISC_DATAW, VGA_MISCOUT | (vh->vh_mono ? 0 : 0x01));
vga_ts_write(vh, mode, vga_ts[1] & ~VGA_TS_MODE_BLANK);
}