1 /* $NetBSD: pci_vga.c,v 1.21 2023/05/06 21:34:39 andvar 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.21 2023/05/06 21:34:39 andvar 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 /* attribute controller registers */
59 static const uint8_t vga_atc[] = {
60 0x00, /* 00: internal palette 0 */
61 0x01, /* 01: internal palette 1 */
62 0x02, /* 02: internal palette 2 */
63 0x03, /* 03: internal palette 3 */
64 0x04, /* 04: internal palette 4 */
65 0x05, /* 05: internal palette 5 */
66 0x14, /* 06: internal palette 6 */
67 0x07, /* 07: internal palette 7 */
68 0x38, /* 08: internal palette 8 */
69 0x39, /* 09: internal palette 9 */
70 0x3a, /* 0A: internal palette 10 */
71 0x3b, /* 0B: internal palette 11 */
72 0x3c, /* 0C: internal palette 12 */
73 0x3d, /* 0D: internal palette 13 */
74 0x3e, /* 0E: internal palette 14 */
75 0x3f, /* 0F: internal palette 15 */
76 0x0c, /* 10: attribute mode control */
77 0x00, /* 11: overscan color */
78 0x0f, /* 12: color plane enable */
79 0x08, /* 13: horizontal PEL panning */
80 0x00 /* 14: color select */
81 };
82
83 /* video DAC palette registers */
84 /* XXX only set up 16 colors used by internal palette in ATC registers */
85 static const uint8_t vga_dacpal[] = {
86 /* R G B */
87 0x00, 0x00, 0x00, /* BLACK */
88 0x00, 0x00, 0x2a, /* BLUE */
89 0x00, 0x2a, 0x00, /* GREEN */
90 0x00, 0x2a, 0x2a, /* CYAN */
91 0x2a, 0x00, 0x00, /* RED */
92 0x2a, 0x00, 0x2a, /* MAGENTA */
93 0x2a, 0x15, 0x00, /* BROWN */
94 0x2a, 0x2a, 0x2a, /* LIGHTGREY */
95 0x15, 0x15, 0x15, /* DARKGREY */
96 0x15, 0x15, 0x3f, /* LIGHTBLUE */
97 0x15, 0x3f, 0x15, /* LIGHTGREEN */
98 0x15, 0x3f, 0x3f, /* LIGHTCYAN */
99 0x3f, 0x15, 0x15, /* LIGHTRED */
100 0x3f, 0x15, 0x3f, /* LIGHTMAGENTA */
101 0x3f, 0x3f, 0x15, /* YELLOW */
102 0x3f, 0x3f, 0x3f /* WHITE */
103 };
104
105 static bus_space_tag_t vga_iot, vga_memt;
106 static int tags_valid = 0;
107
108 #define VGA_REG_SIZE (8*1024)
109 #define VGA_FB_SIZE (32*1024)
110
111 /*
112 * Go look for a VGA card on the PCI-bus. This search is a
113 * stripped down version of the PCI-probe. It only looks on
114 * bus0 for VGA cards. The first card found is used.
115 */
116 int
check_for_vga(bus_space_tag_t iot,bus_space_tag_t memt)117 check_for_vga(bus_space_tag_t iot, bus_space_tag_t memt)
118 {
119 pci_chipset_tag_t pc = NULL; /* XXX */
120 bus_space_handle_t ioh_regs, memh_fb;
121 pcitag_t tag;
122 int device, found, maxndevs, i;
123 int got_ioh, got_memh, rv;
124 uint32_t id, class;
125 volatile uint8_t *regs;
126 uint8_t *fb;
127 const char *nbd = "NetBSD/Atari";
128
129 found = 0;
130 tag = 0;
131 id = 0;
132 rv = 0;
133 got_ioh = 0;
134 got_memh = 0;
135 maxndevs = pci_bus_maxdevs(pc, 0);
136
137 /*
138 * Map 8Kb of registers and 32Kb frame buffer.
139 * XXX: The way the registers are mapped here is plain wrong.
140 * We should try to pin-point the region down to 3[bcd]0 (see
141 * .../dev/ic/vga.c).
142 */
143 if (bus_space_map(iot, 0, VGA_REG_SIZE, 0, &ioh_regs))
144 return 0;
145 got_ioh = 1;
146
147 if (bus_space_map(memt, 0xa0000, VGA_FB_SIZE, 0, &memh_fb))
148 goto bad;
149 got_memh = 1;
150 regs = bus_space_vaddr(iot, ioh_regs);
151 fb = bus_space_vaddr(memt, memh_fb);
152
153 for (device = 0; !found && (device < maxndevs); device++) {
154
155 tag = pci_make_tag(pc, 0, device, 0);
156 id = pci_conf_read(pc, tag, PCI_ID_REG);
157 if (id == 0 || id == 0xffffffff)
158 continue;
159
160 /*
161 * Check if we have some display device here...
162 */
163 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
164 i = 0;
165 if (PCI_CLASS(class) == PCI_CLASS_PREHISTORIC &&
166 PCI_SUBCLASS(class) == PCI_SUBCLASS_PREHISTORIC_VGA)
167 i = 1;
168 if (PCI_CLASS(class) == PCI_CLASS_DISPLAY &&
169 PCI_SUBCLASS(class) == PCI_SUBCLASS_DISPLAY_VGA)
170 i = 1;
171 if (i == 0)
172 continue;
173
174 #if _MILANHW_
175 /* Don't need to be more specific */
176 milan_vga_init(pc, tag, id, regs, fb);
177 found = 1;
178 #else
179 switch (id = PCI_PRODUCT(id)) {
180
181 /*
182 * XXX Make the inclusion of the cases dependent
183 * on config options!
184 */
185 case PCI_PRODUCT_TSENG_ET6000:
186 case PCI_PRODUCT_TSENG_ET4000_W32P_A:
187 case PCI_PRODUCT_TSENG_ET4000_W32P_B:
188 case PCI_PRODUCT_TSENG_ET4000_W32P_C:
189 case PCI_PRODUCT_TSENG_ET4000_W32P_D:
190 tseng_init(pc, tag, id, regs, fb);
191 found = 1;
192 break;
193 case PCI_PRODUCT_ATI_RAGE_PRO_PCI_P:
194 ati_vga_init(pc, tag, id, regs, fb);
195 found = 1;
196 break;
197 default:
198 break;
199 }
200 #endif /* _MILANHW_ */
201 }
202 if (!found)
203 goto bad;
204
205 /*
206 * Assume the device is in CGA mode. Wscons expects this too...
207 */
208 bus_space_unmap(memt, memh_fb, VGA_FB_SIZE);
209 if (bus_space_map(memt, 0xb8000, VGA_FB_SIZE, 0, &memh_fb)) {
210 got_memh = 0;
211 goto bad;
212 }
213 fb = bus_space_vaddr(memt, memh_fb);
214
215 /*
216 * Generic parts of the initialization...
217 */
218
219 /* set ATC registers */
220 for (i = 0; i < 21; i++)
221 WAttr(regs, i, vga_atc[i]);
222
223 /* set DAC palette */
224 for (i = 0; i < 16; i++) {
225 vgaw(regs, VDAC_ADDRESS_W, vga_atc[i]);
226 vgaw(regs, VDAC_DATA, vga_dacpal[i * 3 + 0]);
227 vgaw(regs, VDAC_DATA, vga_dacpal[i * 3 + 1]);
228 vgaw(regs, VDAC_DATA, vga_dacpal[i * 3 + 2]);
229 }
230
231 loadfont(regs, fb);
232 #if NVGA_PCI > 0
233 /* use explicit WSDISPLAY_FONTENC_IBM font that MI vga(4) assumes */
234 vga_no_builtinfont = 1;
235 #endif
236
237 /*
238 * Clear the screen and print a message. The latter
239 * is of diagnostic/debug use only.
240 */
241 for (i = 50 * 80; i >= 0; i -= 2) {
242 fb[i] = 0x20; fb[i+1] = 0x07;
243 }
244 for (i = 56; *nbd; i += 2)
245 fb[i] = *nbd++;
246
247 rv = 1;
248 vga_iot = iot;
249 vga_memt = memt;
250 rv = tags_valid = 1;
251
252 bad:
253 if (got_memh)
254 bus_space_unmap(memt, memh_fb, VGA_FB_SIZE);
255 if (got_ioh)
256 bus_space_unmap(iot, ioh_regs, VGA_REG_SIZE);
257 return rv;
258 }
259
260 #if NVGA_PCI > 0
261 void vgacnprobe(struct consdev *);
262 void vgacninit(struct consdev *);
263
264 void
vgacnprobe(struct consdev * cp)265 vgacnprobe(struct consdev *cp)
266 {
267
268 if (tags_valid)
269 cp->cn_pri = CN_NORMAL;
270 }
271
272 void
vgacninit(struct consdev * cp)273 vgacninit(struct consdev *cp)
274 {
275
276 if (tags_valid) {
277 /* XXX: Are those arguments correct? Leo */
278 vga_cnattach(vga_iot, vga_memt, 8, 0);
279 }
280 }
281 #endif /* NVGA_PCI */
282
283 /*
284 * Generic VGA. Load the configured kernel font into the videomemory and
285 * place the card into textmode.
286 */
287 static void
loadfont(volatile uint8_t * ba,uint8_t * fb)288 loadfont(volatile uint8_t *ba, uint8_t *fb)
289 /* ba: Register area KVA */
290 /* fb: Frame buffer KVA */
291 {
292 font_info *fd;
293 uint8_t *c, *f, tmp;
294 uint16_t z, y;
295
296 #if defined(KFONT_8X8)
297 fd = &font_info_8x8;
298 #else
299 fd = &font_info_8x16;
300 #endif
301
302 WAttr(ba, 0x20 | ACT_ID_ATTR_MODE_CNTL, 0x0a);
303 WSeq(ba, SEQ_ID_MAP_MASK, 0x04);
304 WSeq(ba, SEQ_ID_MEMORY_MODE, 0x06);
305 WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x02);
306 WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00);
307 WGfx(ba, GCT_ID_MISC, 0x0c);
308
309 /*
310 * load text font into beginning of display memory. Each
311 * character cell is 32 bytes long (enough for 4 planes)
312 */
313 for (z = 0, c = fb; z < 256 * 32; z++)
314 *c++ = 0;
315
316 c = (uint8_t *)(fb) + (32 * fd->font_lo);
317 f = fd->font_p;
318 z = fd->font_lo;
319 for (; z <= fd->font_hi; z++, c += (32 - fd->height))
320 for (y = 0; y < fd->height; y++) {
321 *c++ = *f++;
322 }
323
324 /*
325 * Odd/Even addressing
326 */
327 WSeq(ba, SEQ_ID_MAP_MASK, 0x03);
328 WSeq(ba, SEQ_ID_MEMORY_MODE, 0x03);
329 WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
330 WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x10);
331 WGfx(ba, GCT_ID_MISC, 0x0e);
332
333 /*
334 * Font height + underline location
335 */
336 tmp = RCrt(ba, CRT_ID_MAX_ROW_ADDRESS) & 0xe0;
337 WCrt(ba, CRT_ID_MAX_ROW_ADDRESS, tmp | (fd->height - 1));
338 tmp = RCrt(ba, CRT_ID_UNDERLINE_LOC) & 0xe0;
339 WCrt(ba, CRT_ID_UNDERLINE_LOC, tmp | (fd->height - 1));
340
341 /*
342 * Cursor setup
343 */
344 WCrt(ba, CRT_ID_CURSOR_START , 0x00);
345 WCrt(ba, CRT_ID_CURSOR_END , fd->height - 1);
346 WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
347 WCrt(ba, CRT_ID_CURSOR_LOC_LOW , 0x00);
348
349 /*
350 * Enter text mode
351 */
352 WCrt(ba, CRT_ID_MODE_CONTROL , 0xa3);
353 WAttr(ba, ACT_ID_ATTR_MODE_CNTL | 0x20, 0x0a);
354 }
355