xref: /netbsd/sys/dev/ic/vga_subr.c (revision c4a72b64)
1 /* $NetBSD: vga_subr.c,v 1.11 2002/10/15 18:14:42 junyoung Exp $ */
2 
3 /*
4  * Copyright (c) 1998
5  *	Matthias Drochner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the NetBSD Project
18  *	by Matthias Drochner.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: vga_subr.c,v 1.11 2002/10/15 18:14:42 junyoung Exp $");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41 #include <sys/queue.h>
42 #include <machine/bus.h>
43 
44 #include <dev/ic/mc6845reg.h>
45 #include <dev/ic/pcdisplayvar.h>
46 #include <dev/ic/vgareg.h>
47 #include <dev/ic/vgavar.h>
48 
49 #include <dev/wscons/wsdisplayvar.h>
50 
51 static void fontram(struct vga_handle *);
52 static void textram(struct vga_handle *);
53 
54 static void
55 fontram(struct vga_handle *vh)
56 {
57 	/* program sequencer to access character generator */
58 
59 	vga_ts_write(vh, syncreset, 0x01);	/* synchronous reset */
60 	vga_ts_write(vh, wrplmask, 0x04);	/* write to map 2 */
61 	vga_ts_write(vh, memmode, 0x07);	/* sequential addressing */
62 	vga_ts_write(vh, syncreset, 0x03);	/* clear synchronous reset */
63 
64 	/* program graphics controller to access character generator */
65 
66 	vga_gdc_write(vh, rdplanesel, 0x02);	/* select map 2 for cpu reads */
67 	vga_gdc_write(vh, mode, 0x00);	/* disable odd-even addressing */
68 	vga_gdc_write(vh, misc, 0x04);	/* map starts at 0xA000 */
69 }
70 
71 static void
72 textram(struct vga_handle *vh)
73 {
74 	/* program sequencer to access video ram */
75 
76 	vga_ts_write(vh, syncreset, 0x01);	/* synchronous reset */
77 	vga_ts_write(vh, wrplmask, 0x03);	/* write to map 0 & 1 */
78 	vga_ts_write(vh, memmode, 0x03);	/* odd-even addressing */
79 	vga_ts_write(vh, syncreset, 0x03);	/* clear synchronous reset */
80 
81 	/* program graphics controller for text mode */
82 
83 	vga_gdc_write(vh, rdplanesel, 0x00);	/* select map 0 for cpu reads */
84 	vga_gdc_write(vh, mode, 0x10);		/* enable odd-even addressing */
85 	/* map starts at 0xb800 or 0xb000 (mono) */
86 	vga_gdc_write(vh, misc, (vh->vh_mono ? 0x0a : 0x0e));
87 }
88 
89 #ifndef VGA_RASTERCONSOLE
90 void
91 vga_loadchars(struct vga_handle *vh, int fontset, int first, int num, int lpc,
92 	      char *data)
93 {
94 	int offset, i, j, s;
95 
96 	/* fontset number swizzle done in vga_setfontset() */
97 	offset = (fontset << 13) | (first << 5);
98 
99 	s = splhigh();
100 	fontram(vh);
101 
102 	for (i = 0; i < num; i++)
103 		for (j = 0; j < lpc; j++)
104 			bus_space_write_1(vh->vh_memt, vh->vh_allmemh,
105 					  offset + (i << 5) + j,
106 					  data[i * lpc + j]);
107 
108 	textram(vh);
109 	splx(s);
110 }
111 
112 void
113 vga_readoutchars(struct vga_handle *vh, int fontset, int first, int num, int lpc,
114 		 char *data)
115 {
116 	int offset, i, j, s;
117 
118 	/* fontset number swizzle done in vga_setfontset() */
119 	offset = (fontset << 13) | (first << 5);
120 
121 	s = splhigh();
122 	fontram(vh);
123 
124 	for (i = 0; i < num; i++)
125 		for (j = 0; j < lpc; j++)
126 			data[i * lpc + j] =
127 				bus_space_read_1(vh->vh_memt, vh->vh_allmemh,
128 						 offset + (i << 5) + j);
129 
130 	textram(vh);
131 	splx(s);
132 }
133 
134 #ifdef VGA_CONSOLE_ATI_BROKEN_FONTSEL
135 void
136 vga_copyfont01(struct vga_handle *vh)
137 {
138 	int s;
139 
140 	s = splhigh();
141 	fontram(vh);
142 
143 	bus_space_copy_region_1(vh->vh_memt,
144 				vh->vh_allmemh, 0,
145 				vh->vh_allmemh, 1 << 13,
146 				1 << 13);
147 
148 	textram(vh);
149 	splx(s);
150 }
151 #endif
152 
153 void
154 vga_setfontset(struct vga_handle *vh, int fontset1, int fontset2)
155 {
156 	u_int8_t cmap;
157 	static u_int8_t cmaptaba[] = {
158 		0x00, 0x10, 0x01, 0x11,
159 		0x02, 0x12, 0x03, 0x13
160 	};
161 	static u_int8_t cmaptabb[] = {
162 		0x00, 0x20, 0x04, 0x24,
163 		0x08, 0x28, 0x0c, 0x2c
164 	};
165 
166 	/* extended font if fontset1 != fontset2 */
167 	cmap = cmaptaba[fontset1] | cmaptabb[fontset2];
168 
169 	vga_ts_write(vh, fontsel, cmap);
170 }
171 
172 void
173 vga_setscreentype(struct vga_handle *vh, const struct wsscreen_descr *type)
174 {
175 	vga_6845_write(vh, maxrow, type->fontheight - 1);
176 
177 	/* lo byte */
178 	vga_6845_write(vh, vde, type->fontheight * type->nrows - 1);
179 
180 #ifndef PCDISPLAY_SOFTCURSOR
181 	/* set cursor to last 2 lines */
182 	vga_6845_write(vh, curstart, type->fontheight - 2);
183 	vga_6845_write(vh, curend, type->fontheight - 1);
184 #endif
185 	/*
186 	 * disable colour plane 3 if needed for font selection
187 	 */
188 	if (type->capabilities & WSSCREEN_HILIT) {
189 		/*
190 		 * these are the screens which don't support
191 		 * 512-character fonts
192 		 */
193 		vga_attr_write(vh, colplen, 0x0f);
194 	} else
195 		vga_attr_write(vh, colplen, 0x07);
196 }
197 
198 #else /* !VGA_RASTERCONSOLE */
199 void
200 vga_load_builtinfont(struct vga_handle *vh, u_int8_t *font, int firstchar,
201 	int numchars)
202 {
203 	int i, s;
204 
205 	s = splhigh();
206 	fontram(vh);
207 
208 	for (i = firstchar; i < firstchar + numchars; i++)
209 		bus_space_read_region_1(vh->vh_memt, vh->vh_allmemh, i * 32,
210 					font + i * 16, 16);
211 
212 	textram(vh);
213 	splx(s);
214 }
215 #endif /* !VGA_RASTERCONSOLE */
216