From 9c6e6c3f454fdc79f870e7051a29b96806962f28 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Mon, 5 Sep 2016 03:32:31 +0200
Subject: [PATCH 3/7] kernel/vga: Improve readability of sequencer register
 handling.

---
 sys/dev/video/fb/bmp/splash_bmp.c |  2 +-
 sys/dev/video/fb/vga.c            | 58 +++++++++++--------------------
 sys/dev/video/fb/vgareg.h         | 39 +++++++++++++++++++++
 3 files changed, 61 insertions(+), 38 deletions(-)

diff --git a/sys/dev/video/fb/bmp/splash_bmp.c b/sys/dev/video/fb/bmp/splash_bmp.c
index afe9fd6021..112232bd03 100644
--- a/sys/dev/video/fb/bmp/splash_bmp.c
+++ b/sys/dev/video/fb/bmp/splash_bmp.c
@@ -603,7 +603,7 @@ bmp_Draw(video_adapter_t *adp)
 	outw(GDCIDX, 0x0f01);			/* set/reset enable */
 
 	if (bmp_info.sdepth == 1)
-	    outw(TSIDX, 0x0102);		/* unmask plane #0 */
+	    vga_sequencer_set(MAP_MASK, 0x01);	/* unmask plane #0 */
     }
 #endif
 
diff --git a/sys/dev/video/fb/vga.c b/sys/dev/video/fb/vga.c
index 9d76495a33..bd86b5b852 100644
--- a/sys/dev/video/fb/vga.c
+++ b/sys/dev/video/fb/vga.c
@@ -765,8 +765,7 @@ set_display_start(video_adapter_t *adp, int x, int y)
 		roff = 0;
 		poff = x%ppb;
 	} else {
-		outb(TSIDX, 1);
-		if (inb(TSREG) & 1)
+		if (vga_sequencer_get(CLOCKING_MODE) & 1)
 			ppb = 9;
 		else
 			ppb = 8;
@@ -1131,8 +1130,8 @@ set_font_mode(video_adapter_t *adp, u_char *buf)
 	crit_enter();
 
 	/* save register values */
-	outb(TSIDX, 0x02); buf[0] = inb(TSREG);
-	outb(TSIDX, 0x04); buf[1] = inb(TSREG);
+	buf[0] = vga_sequencer_get(MAP_MASK);
+	buf[1] = vga_sequencer_get(MEMORY_MODE);
 	outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
 	outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
 	outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
@@ -1144,8 +1143,8 @@ set_font_mode(video_adapter_t *adp, u_char *buf)
 	outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
 	vga_reset_flipflop();
 	outb(ATC, 0x20);			/* enable palette */
-	outw(TSIDX, 0x0402);
-	outw(TSIDX, 0x0704);
+	vga_sequencer_set(MAP_MASK, 0x04);
+	vga_sequencer_set(MEMORY_MODE, 0x07);
 	outw(GDCIDX, 0x0204);
 	outw(GDCIDX, 0x0005);
 	outw(GDCIDX, 0x0406);			/* addr = a0000, 64kb */
@@ -1163,8 +1162,8 @@ set_normal_mode(video_adapter_t *adp, u_char *buf)
 	outb(ATC, 0x10); outb(ATC, buf[5]);
 	vga_reset_flipflop();
 	outb(ATC, 0x20);			/* enable palette */
-	outw(TSIDX, 0x0002 | (buf[0] << 8));
-	outw(TSIDX, 0x0004 | (buf[1] << 8));
+	vga_sequencer_set(MAP_MASK, buf[0]);
+	vga_sequencer_set(MEMORY_MODE, buf[1]);
 	outw(GDCIDX, 0x0004 | (buf[2] << 8));
 	outw(GDCIDX, 0x0005 | (buf[3] << 8));
 	outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
@@ -1299,7 +1298,7 @@ vga_show_font(video_adapter_t *adp, int page)
 		return EINVAL;
 
 	crit_enter();
-	outb(TSIDX, 0x03); outb(TSREG, cg[page]);
+	vga_sequencer_set(CHARACTER_MAP_SELECT, cg[page]);
 	crit_exit();
 
 	return 0;
@@ -1435,13 +1434,11 @@ vga_save_state(video_adapter_t *adp, void *p, size_t size)
 
 	crit_enter();
 
-	outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
-	for (i = 0, j = 5; i < 4; i++) {
-		outb(TSIDX, i + 1);
-		buf[j++]  =  inb(TSREG);
-	}
-	buf[9]  =  inb(MISC + 10);		/* dot-clock */
-	outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
+	vga_sequencer_set(RESET, 0x01);		/* stop sequencer */
+	for (i = CLOCKING_MODE, j = 5; i <= MEMORY_MODE; i++)
+		buf[j++] = vga_sequencer_get(i);
+	buf[9] = inb(MISC + 10);		/* dot-clock */
+	vga_sequencer_set(RESET, 0x03);		/* start sequencer */
 
 	for (i = HORIZONTAL_TOTAL, j = 10; i <= LINE_COMPARE; i++) /* crtc */
 		buf[j++] = vga_crtc_get(i);
@@ -1494,7 +1491,7 @@ static int
 vga_load_state(video_adapter_t *adp, void *p)
 {
 	u_char *buf;
-	int i;
+	int i, j;
 
 	prologue(adp, V_ADP_STATELOAD, ENODEV);
 	if (((adp_state_t *)p)->sig != V_STATE_SIG)
@@ -1508,13 +1505,11 @@ vga_load_state(video_adapter_t *adp, void *p)
 
 	crit_enter();
 
-	outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
-	for (i = 0; i < 4; ++i) {		/* program sequencer */
-		outb(TSIDX, i + 1);
-		outb(TSREG, buf[i + 5]);
-	}
+	vga_sequencer_set(RESET, 0x01);		/* stop sequencer */
+	for (i = CLOCKING_MODE, j = 5; i <= MEMORY_MODE; ++i) /* program sequencer */
+		vga_sequencer_set(i, buf[j++]);
 	outb(MISC, buf[9]);			/* set dot-clock */
-	outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
+	vga_sequencer_set(RESET, 0x03);		/* start sequencer */
 	vga_crtc_and(CURSOR_END, 0x7f);
 	for (i = HORIZONTAL_TOTAL; i <= LINE_COMPARE; ++i) /* program crtc */
 		vga_crtc_set(i, buf[i + 10]);
@@ -1650,29 +1645,18 @@ vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
 static int
 vga_blank_display(video_adapter_t *adp, int mode)
 {
-	u_char val;
-
 	crit_enter();
 	switch (mode) {
 	case V_DISPLAY_SUSPEND:
 	case V_DISPLAY_STAND_BY:
-		outb(TSIDX, 0x01);
-		val = inb(TSREG);
-		outb(TSIDX, 0x01);
-		outb(TSREG, val | 0x20);
+		vga_sequencer_or(CLOCKING_MODE, 0x20);
 		vga_crtc_and(CRT_MODE_CONTROL, 0x7f);
 		break;
 	case V_DISPLAY_BLANK:
-		outb(TSIDX, 0x01);
-		val = inb(TSREG);
-		outb(TSIDX, 0x01);
-		outb(TSREG, val | 0x20);
+		vga_sequencer_or(CLOCKING_MODE, 0x20);
 		break;
 	case V_DISPLAY_ON:
-		outb(TSIDX, 0x01);
-		val = inb(TSREG);
-		outb(TSIDX, 0x01);
-		outb(TSREG, val & 0xDF);
+		vga_sequencer_and(CLOCKING_MODE, 0xdf);
 		vga_crtc_or(CRT_MODE_CONTROL, 0x80);
 		break;
 	}
diff --git a/sys/dev/video/fb/vgareg.h b/sys/dev/video/fb/vgareg.h
index 978983608a..6e47a49dce 100644
--- a/sys/dev/video/fb/vgareg.h
+++ b/sys/dev/video/fb/vgareg.h
@@ -77,6 +77,14 @@ enum vga_crtc_reg {
 	LINE_COMPARE		= 0x18
 };
 
+enum vga_sequencer_reg {
+	RESET			= 0x00,
+	CLOCKING_MODE		= 0x01,
+	MAP_MASK		= 0x02,
+	CHARACTER_MAP_SELECT	= 0x03,
+	MEMORY_MODE		= 0x04
+};
+
 static __inline void
 vga_reset_flipflop(void)
 {
@@ -115,6 +123,37 @@ vga_crtc_or(enum vga_crtc_reg reg, uint8_t val)
 	vga_crtc_set(reg, oldval | val);
 }
 
+static __inline uint8_t
+vga_sequencer_get(enum vga_sequencer_reg reg)
+{
+	outb(TSIDX, reg);
+	return inb(TSREG);
+}
+
+static __inline void
+vga_sequencer_set(enum vga_sequencer_reg reg, uint8_t val)
+{
+	outw(TSIDX, reg | (val << 8));
+}
+
+static __inline void
+vga_sequencer_and(enum vga_sequencer_reg reg, uint8_t val)
+{
+	uint8_t oldval;
+
+	oldval = vga_sequencer_get(reg);
+	vga_sequencer_set(reg, oldval & val);
+}
+
+static __inline void
+vga_sequencer_or(enum vga_sequencer_reg reg, uint8_t val)
+{
+	uint8_t oldval;
+
+	oldval = vga_sequencer_get(reg);
+	vga_sequencer_set(reg, oldval | val);
+}
+
 #define VGA_DRIVER_NAME		"vga"
 #define VGA_UNIT(dev)		minor(dev)
 #define VGA_MKMINOR(unit)	(unit & 0xff)
-- 
2.23.0

