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