xref: /netbsd/sys/arch/atari/pci/pci_vga.c (revision bf9ec67e)
1 /*	$NetBSD: pci_vga.c,v 1.8 2002/04/09 13:16:09 leo Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 Leo Weppelman.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Leo Weppelman.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/systm.h>
34 #include <dev/pci/pcireg.h>
35 #include <dev/pci/pcivar.h>
36 #include <dev/pci/pcidevs.h>
37 #include <atari/pci/pci_vga.h>
38 #include <atari/dev/grf_etreg.h>
39 #include <atari/include/iomap.h>
40 
41 #include <atari/dev/font.h>
42 
43 #include "vga_pci.h"
44 #if NVGA_PCI > 0
45 #include <dev/cons.h>
46 #include <dev/ic/mc6845reg.h>
47 #include <dev/ic/pcdisplayvar.h>
48 #include <dev/ic/vgareg.h>
49 #include <dev/ic/vgavar.h>
50 #endif
51 
52 static void loadfont(volatile u_char *, u_char *fb);
53 
54 /* XXX: Shouldn't these be in font.h???? */
55 extern font_info	font_info_8x8;
56 extern font_info	font_info_8x16;
57 
58 /* Console colors */
59 static u_char conscolors[3][3] = {	/* background, foreground, hilite */
60 	{0x0, 0x0, 0x0}, {0x30, 0x30, 0x30}, { 0x3f,  0x3f,  0x3f}
61 };
62 
63 static bus_space_tag_t	vga_iot, vga_memt;
64 static int		tags_valid = 0;
65 
66 #define		VGA_REG_SIZE	(8*1024)
67 #define		VGA_FB_SIZE	(32*1024)
68 
69 /*
70  * Go look for a VGA card on the PCI-bus. This search is a
71  * stripped down version of the PCI-probe. It only looks on
72  * bus0 for VGA cards. The first card found is used.
73  */
74 int
75 check_for_vga(iot, memt)
76 	bus_space_tag_t iot, memt;
77 {
78 	pci_chipset_tag_t	pc = NULL; /* XXX */
79 	bus_space_handle_t	ioh_regs, memh_fb;
80 	pcitag_t		tag;
81 	int			device, found, id, maxndevs, i, j;
82 	int			class, got_ioh, got_memh, rv;
83 	volatile u_char		*regs;
84 	u_char			*fb;
85 	char			*nbd = "NetBSD/Atari";
86 
87 	found    = 0;
88 	tag      = 0;
89 	id       = 0;
90 	rv	 = 0;
91 	got_ioh	 = 0;
92 	got_memh = 0;
93 	maxndevs = pci_bus_maxdevs(pc, 0);
94 
95 	/*
96 	 * Map 8Kb of registers and 32Kb frame buffer.
97 	 * XXX: The way the registers are mapped here is plain wrong.
98 	 *      We should try to pin-point the region down to 3[bcd]0 (see
99 	 *      .../dev/ic/vga.c).
100 	 */
101 	if (bus_space_map(iot, 0, VGA_REG_SIZE, 0, &ioh_regs))
102 		return 0;
103 	got_ioh = 1;
104 
105 	if (bus_space_map(memt, 0xa0000, VGA_FB_SIZE, 0, &memh_fb))
106 		goto bad;
107 	got_memh = 1;
108 	regs = bus_space_vaddr(iot, ioh_regs);
109 	fb   = bus_space_vaddr(memt, memh_fb);
110 
111 	for (device = 0; !found && (device < maxndevs); device++) {
112 
113 		tag = pci_make_tag(pc, 0, device, 0);
114 		id  = pci_conf_read(pc, tag, PCI_ID_REG);
115 		if (id == 0 || id == 0xffffffff)
116 			continue;
117 
118 		/*
119 		 * Check if we have some display device here...
120 		 */
121 		class = pci_conf_read(pc, tag, PCI_CLASS_REG);
122 		i = 0;
123 		if (PCI_CLASS(class) == PCI_CLASS_PREHISTORIC &&
124 		    PCI_SUBCLASS(class) == PCI_SUBCLASS_PREHISTORIC_VGA)
125 			i = 1;
126 		if (PCI_CLASS(class) == PCI_CLASS_DISPLAY &&
127 		    PCI_SUBCLASS(class) == PCI_SUBCLASS_DISPLAY_VGA)
128 			i = 1;
129 		if (i == 0)
130 			continue;
131 
132 #if _MILANHW_
133 		/* Don't need to be more specific */
134 		milan_vga_init(pc, tag, id, regs, fb);
135 		found = 1;
136 #else
137 		switch (id = PCI_PRODUCT(id)) {
138 
139 			/*
140 			 * XXX Make the inclusion of the cases dependend
141 			 *     on config options!
142 			 */
143 			case PCI_PRODUCT_TSENG_ET6000:
144 			case PCI_PRODUCT_TSENG_ET4000_W32P_A:
145 			case PCI_PRODUCT_TSENG_ET4000_W32P_B:
146 			case PCI_PRODUCT_TSENG_ET4000_W32P_C:
147 			case PCI_PRODUCT_TSENG_ET4000_W32P_D:
148 				tseng_init(pc, tag, id, regs, fb);
149 				found = 1;
150 				break;
151 			case PCI_PRODUCT_ATI_RAGE_PRO_PCI_P:
152 				ati_vga_init(pc, tag, id, regs, fb);
153 				found = 1;
154 				break;
155 			default:
156 				break;
157 		}
158 #endif /* _MILANHW_ */
159 	}
160 	if (!found)
161 		goto bad;
162 
163 	/*
164 	 * Assume the device is in CGA mode. Wscons expects this too...
165 	 */
166 	bus_space_unmap(memt, memh_fb, VGA_FB_SIZE);
167 	if (bus_space_map(memt, 0xb8000, VGA_FB_SIZE, 0, &memh_fb)) {
168 		got_memh = 0;
169 		goto bad;
170 	}
171 	fb = bus_space_vaddr(memt, memh_fb);
172 
173 	/*
174 	 * Generic parts of the initialization...
175 	 */
176 
177 	/* B&W colors */
178 	vgaw(regs, VDAC_ADDRESS_W, 0);
179 	for (i = 0; i < 256; i++) {
180 		j = (i & 1) ? ((i > 7) ? 2 : 1) : 0;
181 		vgaw(regs, VDAC_DATA, conscolors[j][0]);
182 		vgaw(regs, VDAC_DATA, conscolors[j][1]);
183 		vgaw(regs, VDAC_DATA, conscolors[j][2]);
184 	}
185 
186 	loadfont(regs, fb);
187 
188 	/*
189 	 * Clear the screen and print a message. The latter
190 	 * is of diagnostic/debug use only.
191 	 */
192 	for (i = 50 * 80; i >= 0; i -= 2) {
193 		fb[i] = 0x20; fb[i+1] = 0x07;
194 	}
195 	for (i = 56; *nbd; i += 2)
196 		fb[i] = *nbd++;
197 
198 	rv = 1;
199 	vga_iot  = iot;
200 	vga_memt = memt;
201 	rv = tags_valid = 1;
202 
203 bad:
204 	if (got_memh)
205 		bus_space_unmap(memt, memh_fb, VGA_FB_SIZE);
206 	if (got_ioh)
207 		bus_space_unmap(iot, ioh_regs, VGA_REG_SIZE);
208 	return (rv);
209 }
210 
211 #if NVGA_PCI > 0
212 void vgacnprobe(struct consdev *);
213 void vgacninit(struct consdev *);
214 
215 void
216 vgacnprobe(cp)
217 	struct consdev *cp;
218 {
219 	if (tags_valid)
220 		cp->cn_pri = CN_NORMAL;
221 }
222 
223 void
224 vgacninit(cp)
225 	struct consdev *cp;
226 {
227 	if (tags_valid) {
228 		/* XXX: Are those arguments correct? Leo */
229 		vga_cnattach(vga_iot, vga_memt, 8, 0);
230 	}
231 }
232 #endif /* NVGA_PCI */
233 
234 /*
235  * Generic VGA. Load the configured kernel font into the videomemory and
236  * place the card into textmode.
237  */
238 static void
239 loadfont(ba, fb)
240 	volatile u_char *ba;	/* Register area KVA */
241 	u_char		*fb;	/* Frame buffer	KVA  */
242 {
243 	font_info	*fd;
244 	u_char		*c, *f, tmp;
245 	u_short		z, y;
246 
247 #if defined(KFONT_8X8)
248 	fd = &font_info_8x8;
249 #else
250 	fd = &font_info_8x16;
251 #endif
252 
253 	WAttr(ba, 0x20 | ACT_ID_ATTR_MODE_CNTL, 0x0a);
254 	WSeq(ba, SEQ_ID_MAP_MASK,	 0x04);
255 	WSeq(ba, SEQ_ID_MEMORY_MODE,	 0x06);
256 	WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x02);
257 	WGfx(ba, GCT_ID_GRAPHICS_MODE,	 0x00);
258 	WGfx(ba, GCT_ID_MISC,		 0x0c);
259 
260 	/*
261 	 * load text font into beginning of display memory. Each
262 	 * character cell is 32 bytes long (enough for 4 planes)
263 	 */
264 	for (z = 0, c = fb; z < 256 * 32; z++)
265 		*c++ = 0;
266 
267 	c = (unsigned char *) (fb) + (32 * fd->font_lo);
268 	f = fd->font_p;
269 	z = fd->font_lo;
270 	for (; z <= fd->font_hi; z++, c += (32 - fd->height))
271 		for (y = 0; y < fd->height; y++) {
272 			*c++ = *f++;
273 		}
274 
275 	/*
276 	 * Odd/Even addressing
277 	 */
278 	WSeq(ba, SEQ_ID_MAP_MASK,	 0x03);
279 	WSeq(ba, SEQ_ID_MEMORY_MODE,	 0x03);
280 	WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
281 	WGfx(ba, GCT_ID_GRAPHICS_MODE,	 0x10);
282 	WGfx(ba, GCT_ID_MISC,		 0x0e);
283 
284 	/*
285 	 * Font height + underline location
286 	 */
287 	tmp = RCrt(ba, CRT_ID_MAX_ROW_ADDRESS) & 0xe0;
288 	WCrt(ba, CRT_ID_MAX_ROW_ADDRESS, tmp | (fd->height - 1));
289 	tmp = RCrt(ba, CRT_ID_UNDERLINE_LOC)   & 0xe0;
290 	WCrt(ba, CRT_ID_UNDERLINE_LOC,   tmp | (fd->height - 1));
291 
292 	/*
293 	 * Cursor setup
294 	 */
295 	WCrt(ba, CRT_ID_CURSOR_START   , 0x00);
296 	WCrt(ba, CRT_ID_CURSOR_END     , fd->height - 1);
297 	WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
298 	WCrt(ba, CRT_ID_CURSOR_LOC_LOW , 0x00);
299 
300 	/*
301 	 * Enter text mode
302 	 */
303 	WCrt(ba, CRT_ID_MODE_CONTROL   , 0xa3);
304 	WAttr(ba, ACT_ID_ATTR_MODE_CNTL | 0x20, 0x0a);
305 }
306