xref: /openbsd/sys/dev/ic/vga_subr.c (revision 9d02a238)
1 /* $OpenBSD: vga_subr.c,v 1.5 2015/07/18 00:48:05 miod Exp $ */
2 /* $NetBSD: vga_subr.c,v 1.6 2000/01/25 02:44:03 ad Exp $ */
3 
4 /*
5  * Copyright (c) 1998
6  *	Matthias Drochner.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/device.h>
33 #include <sys/queue.h>
34 #include <machine/bus.h>
35 
36 #include <dev/ic/mc6845reg.h>
37 #include <dev/ic/pcdisplayvar.h>
38 #include <dev/ic/vgareg.h>
39 
40 #include <dev/wscons/wsdisplayvar.h>
41 #include <dev/ic/vgavar.h>
42 
43 static void fontram(struct vga_handle *);
44 static void textram(struct vga_handle *);
45 
46 static void
fontram(struct vga_handle * vh)47 fontram(struct vga_handle *vh)
48 {
49 	/* program sequencer to access character generator */
50 
51 	vga_ts_write(vh, syncreset, 0x01);	/* synchronous reset */
52 	vga_ts_write(vh, wrplmask, 0x04);	/* write to map 2 */
53 	vga_ts_write(vh, memmode, 0x07);	/* sequential addressing */
54 	vga_ts_write(vh, syncreset, 0x03);	/* clear synchronous reset */
55 
56 	/* program graphics controller to access character generator */
57 
58 	vga_gdc_write(vh, rdplanesel, 0x02);	/* select map 2 for cpu reads */
59 	vga_gdc_write(vh, mode, 0x00);	/* disable odd-even addressing */
60 	vga_gdc_write(vh, misc, 0x04);	/* map starts at 0xA000 */
61 }
62 
63 static void
textram(struct vga_handle * vh)64 textram(struct vga_handle *vh)
65 {
66 	/* program sequencer to access video ram */
67 
68 	vga_ts_write(vh, syncreset, 0x01);	/* synchronous reset */
69 	vga_ts_write(vh, wrplmask, 0x03);	/* write to map 0 & 1 */
70 	vga_ts_write(vh, memmode, 0x03);	/* odd-even addressing */
71 	vga_ts_write(vh, syncreset, 0x03);	/* clear synchronous reset */
72 
73 	/* program graphics controller for text mode */
74 
75 	vga_gdc_write(vh, rdplanesel, 0x00);	/* select map 0 for cpu reads */
76 	vga_gdc_write(vh, mode, 0x10);		/* enable odd-even addressing */
77 	/* map starts at 0xb800 or 0xb000 (mono) */
78 	vga_gdc_write(vh, misc, (vh->vh_mono ? 0x0a : 0x0e));
79 }
80 
81 void
vga_loadchars(struct vga_handle * vh,int fontset,int first,int num,int lpc,char * data)82 vga_loadchars(struct vga_handle *vh, int fontset, int first, int num, int lpc,
83     char *data)
84 {
85 	int offset, i, j, s;
86 
87 	/* fontset number swizzle done in vga_setfontset() */
88 	offset = (fontset << 13) | (first << 5);
89 
90 	s = splhigh();
91 	fontram(vh);
92 
93 	for (i = 0; i < num; i++)
94 		for (j = 0; j < lpc; j++)
95 			bus_space_write_1(vh->vh_memt, vh->vh_allmemh,
96 					  offset + (i << 5) + j,
97 					  data[i * lpc + j]);
98 
99 	textram(vh);
100 	splx(s);
101 }
102 
103 void
vga_setfontset(struct vga_handle * vh,int fontset1,int fontset2)104 vga_setfontset(struct vga_handle *vh, int fontset1, int fontset2)
105 {
106 	u_int8_t cmap;
107 	static const u_int8_t cmaptaba[] = {
108 		0x00, 0x10, 0x01, 0x11,
109 		0x02, 0x12, 0x03, 0x13
110 	};
111 	static const u_int8_t cmaptabb[] = {
112 		0x00, 0x20, 0x04, 0x24,
113 		0x08, 0x28, 0x0c, 0x2c
114 	};
115 
116 	/* extended font if fontset1 != fontset2 */
117 	cmap = cmaptaba[fontset1] | cmaptabb[fontset2];
118 
119 	vga_ts_write(vh, fontsel, cmap);
120 }
121 
122 void
vga_setscreentype(struct vga_handle * vh,const struct wsscreen_descr * type)123 vga_setscreentype(struct vga_handle *vh, const struct wsscreen_descr *type)
124 {
125 	vga_6845_write(vh, maxrow, type->fontheight - 1);
126 
127 	/* lo byte */
128 	vga_6845_write(vh, vde, type->fontheight * type->nrows - 1);
129 
130 #ifndef PCDISPLAY_SOFTCURSOR
131 	/* set cursor to last 2 lines */
132 	vga_6845_write(vh, curstart, type->fontheight - 2);
133 	vga_6845_write(vh, curend, type->fontheight - 1);
134 #endif
135 	/*
136 	 * disable colour plane 3 if needed for font selection
137 	 */
138 	if (type->capabilities & WSSCREEN_HILIT) {
139 		/*
140 		 * these are the screens which don't support
141 		 * 512-character fonts
142 		 */
143 		vga_attr_write(vh, colplen, 0x0f);
144 	} else
145 		vga_attr_write(vh, colplen, 0x07);
146 }
147