pci_vga.c revision 1.19 1 1.19 tsutsui /* $NetBSD: pci_vga.c,v 1.19 2022/08/15 04:37:46 tsutsui Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1999 Leo Weppelman. All rights reserved.
5 1.1 leo *
6 1.1 leo * Redistribution and use in source and binary forms, with or without
7 1.1 leo * modification, are permitted provided that the following conditions
8 1.1 leo * are met:
9 1.1 leo * 1. Redistributions of source code must retain the above copyright
10 1.1 leo * notice, this list of conditions and the following disclaimer.
11 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 leo * notice, this list of conditions and the following disclaimer in the
13 1.1 leo * documentation and/or other materials provided with the distribution.
14 1.1 leo *
15 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 leo */
26 1.9 lukem
27 1.9 lukem #include <sys/cdefs.h>
28 1.19 tsutsui __KERNEL_RCSID(0, "$NetBSD: pci_vga.c,v 1.19 2022/08/15 04:37:46 tsutsui Exp $");
29 1.9 lukem
30 1.1 leo #include <sys/param.h>
31 1.1 leo #include <sys/queue.h>
32 1.1 leo #include <sys/systm.h>
33 1.1 leo #include <dev/pci/pcireg.h>
34 1.1 leo #include <dev/pci/pcivar.h>
35 1.1 leo #include <dev/pci/pcidevs.h>
36 1.1 leo #include <atari/pci/pci_vga.h>
37 1.1 leo #include <atari/dev/grf_etreg.h>
38 1.1 leo #include <atari/include/iomap.h>
39 1.1 leo
40 1.1 leo #include <atari/dev/font.h>
41 1.1 leo
42 1.6 leo #include "vga_pci.h"
43 1.4 leo #if NVGA_PCI > 0
44 1.4 leo #include <dev/cons.h>
45 1.4 leo #include <dev/ic/mc6845reg.h>
46 1.4 leo #include <dev/ic/pcdisplayvar.h>
47 1.4 leo #include <dev/ic/vgareg.h>
48 1.4 leo #include <dev/ic/vgavar.h>
49 1.4 leo #endif
50 1.4 leo
51 1.15 tsutsui static void loadfont(volatile uint8_t *, uint8_t *fb);
52 1.1 leo
53 1.1 leo /* XXX: Shouldn't these be in font.h???? */
54 1.1 leo extern font_info font_info_8x8;
55 1.1 leo extern font_info font_info_8x16;
56 1.1 leo
57 1.1 leo /* Console colors */
58 1.19 tsutsui /* attribute controller registers */
59 1.19 tsutsui static const uint8_t vga_atc[] = {
60 1.19 tsutsui 0x00, /* 00: internal palette 0 */
61 1.19 tsutsui 0x01, /* 01: internal palette 1 */
62 1.19 tsutsui 0x02, /* 02: internal palette 2 */
63 1.19 tsutsui 0x03, /* 03: internal palette 3 */
64 1.19 tsutsui 0x04, /* 04: internal palette 4 */
65 1.19 tsutsui 0x05, /* 05: internal palette 5 */
66 1.19 tsutsui 0x14, /* 06: internal palette 6 */
67 1.19 tsutsui 0x07, /* 07: internal palette 7 */
68 1.19 tsutsui 0x38, /* 08: internal palette 8 */
69 1.19 tsutsui 0x39, /* 09: internal palette 9 */
70 1.19 tsutsui 0x3a, /* 0A: internal palette 10 */
71 1.19 tsutsui 0x3b, /* 0B: internal palette 11 */
72 1.19 tsutsui 0x3c, /* 0C: internal palette 12 */
73 1.19 tsutsui 0x3d, /* 0D: internal palette 13 */
74 1.19 tsutsui 0x3e, /* 0E: internal palette 14 */
75 1.19 tsutsui 0x3f, /* 0F: internal palette 15 */
76 1.19 tsutsui 0x0c, /* 10: attribute mode control */
77 1.19 tsutsui 0x00, /* 11: overscan color */
78 1.19 tsutsui 0x0f, /* 12: color plane enable */
79 1.19 tsutsui 0x08, /* 13: horizontal PEL panning */
80 1.19 tsutsui 0x00 /* 14: color select */
81 1.19 tsutsui };
82 1.19 tsutsui
83 1.19 tsutsui /* video DAC palette registers */
84 1.19 tsutsui /* XXX only set up 16 colors used by internal palette in ATC regsters */
85 1.19 tsutsui static const uint8_t vga_dacpal[] = {
86 1.19 tsutsui /* R G B */
87 1.19 tsutsui 0x00, 0x00, 0x00, /* BLACK */
88 1.19 tsutsui 0x00, 0x00, 0x2a, /* BLUE */
89 1.19 tsutsui 0x00, 0x2a, 0x00, /* GREEN */
90 1.19 tsutsui 0x00, 0x2a, 0x2a, /* CYAN */
91 1.19 tsutsui 0x2a, 0x00, 0x00, /* RED */
92 1.19 tsutsui 0x2a, 0x00, 0x2a, /* MAGENTA */
93 1.19 tsutsui 0x2a, 0x15, 0x00, /* BROWN */
94 1.19 tsutsui 0x2a, 0x2a, 0x2a, /* LIGHTGREY */
95 1.19 tsutsui 0x15, 0x15, 0x15, /* DARKGREY */
96 1.19 tsutsui 0x15, 0x15, 0x3f, /* LIGHTBLUE */
97 1.19 tsutsui 0x15, 0x3f, 0x15, /* LIGHTGREEN */
98 1.19 tsutsui 0x15, 0x3f, 0x3f, /* LIGHTCYAN */
99 1.19 tsutsui 0x3f, 0x15, 0x15, /* LIGHTRED */
100 1.19 tsutsui 0x3f, 0x15, 0x3f, /* LIGHTMAGENTA */
101 1.19 tsutsui 0x3f, 0x3f, 0x15, /* YELLOW */
102 1.19 tsutsui 0x3f, 0x3f, 0x3f /* WHITE */
103 1.1 leo };
104 1.1 leo
105 1.4 leo static bus_space_tag_t vga_iot, vga_memt;
106 1.4 leo static int tags_valid = 0;
107 1.3 leo
108 1.3 leo #define VGA_REG_SIZE (8*1024)
109 1.3 leo #define VGA_FB_SIZE (32*1024)
110 1.3 leo
111 1.1 leo /*
112 1.1 leo * Go look for a VGA card on the PCI-bus. This search is a
113 1.1 leo * stripped down version of the PCI-probe. It only looks on
114 1.1 leo * bus0 for VGA cards. The first card found is used.
115 1.1 leo */
116 1.1 leo int
117 1.13 dsl check_for_vga(bus_space_tag_t iot, bus_space_tag_t memt)
118 1.1 leo {
119 1.1 leo pci_chipset_tag_t pc = NULL; /* XXX */
120 1.3 leo bus_space_handle_t ioh_regs, memh_fb;
121 1.1 leo pcitag_t tag;
122 1.19 tsutsui int device, found, maxndevs, i;
123 1.15 tsutsui int got_ioh, got_memh, rv;
124 1.16 tsutsui uint32_t id, class;
125 1.15 tsutsui volatile uint8_t *regs;
126 1.15 tsutsui uint8_t *fb;
127 1.10 he const char *nbd = "NetBSD/Atari";
128 1.1 leo
129 1.1 leo found = 0;
130 1.1 leo tag = 0;
131 1.1 leo id = 0;
132 1.3 leo rv = 0;
133 1.3 leo got_ioh = 0;
134 1.3 leo got_memh = 0;
135 1.1 leo maxndevs = pci_bus_maxdevs(pc, 0);
136 1.1 leo
137 1.1 leo /*
138 1.3 leo * Map 8Kb of registers and 32Kb frame buffer.
139 1.3 leo * XXX: The way the registers are mapped here is plain wrong.
140 1.3 leo * We should try to pin-point the region down to 3[bcd]0 (see
141 1.3 leo * .../dev/ic/vga.c).
142 1.1 leo */
143 1.3 leo if (bus_space_map(iot, 0, VGA_REG_SIZE, 0, &ioh_regs))
144 1.3 leo return 0;
145 1.3 leo got_ioh = 1;
146 1.3 leo
147 1.3 leo if (bus_space_map(memt, 0xa0000, VGA_FB_SIZE, 0, &memh_fb))
148 1.3 leo goto bad;
149 1.3 leo got_memh = 1;
150 1.3 leo regs = bus_space_vaddr(iot, ioh_regs);
151 1.3 leo fb = bus_space_vaddr(memt, memh_fb);
152 1.1 leo
153 1.1 leo for (device = 0; !found && (device < maxndevs); device++) {
154 1.1 leo
155 1.1 leo tag = pci_make_tag(pc, 0, device, 0);
156 1.1 leo id = pci_conf_read(pc, tag, PCI_ID_REG);
157 1.1 leo if (id == 0 || id == 0xffffffff)
158 1.1 leo continue;
159 1.3 leo
160 1.3 leo /*
161 1.3 leo * Check if we have some display device here...
162 1.3 leo */
163 1.3 leo class = pci_conf_read(pc, tag, PCI_CLASS_REG);
164 1.3 leo i = 0;
165 1.3 leo if (PCI_CLASS(class) == PCI_CLASS_PREHISTORIC &&
166 1.3 leo PCI_SUBCLASS(class) == PCI_SUBCLASS_PREHISTORIC_VGA)
167 1.3 leo i = 1;
168 1.3 leo if (PCI_CLASS(class) == PCI_CLASS_DISPLAY &&
169 1.3 leo PCI_SUBCLASS(class) == PCI_SUBCLASS_DISPLAY_VGA)
170 1.3 leo i = 1;
171 1.3 leo if (i == 0)
172 1.3 leo continue;
173 1.3 leo
174 1.5 leo #if _MILANHW_
175 1.5 leo /* Don't need to be more specific */
176 1.5 leo milan_vga_init(pc, tag, id, regs, fb);
177 1.5 leo found = 1;
178 1.5 leo #else
179 1.1 leo switch (id = PCI_PRODUCT(id)) {
180 1.1 leo
181 1.1 leo /*
182 1.18 andvar * XXX Make the inclusion of the cases dependent
183 1.1 leo * on config options!
184 1.1 leo */
185 1.1 leo case PCI_PRODUCT_TSENG_ET6000:
186 1.1 leo case PCI_PRODUCT_TSENG_ET4000_W32P_A:
187 1.1 leo case PCI_PRODUCT_TSENG_ET4000_W32P_B:
188 1.1 leo case PCI_PRODUCT_TSENG_ET4000_W32P_C:
189 1.1 leo case PCI_PRODUCT_TSENG_ET4000_W32P_D:
190 1.1 leo tseng_init(pc, tag, id, regs, fb);
191 1.7 leo found = 1;
192 1.7 leo break;
193 1.8 leo case PCI_PRODUCT_ATI_RAGE_PRO_PCI_P:
194 1.7 leo ati_vga_init(pc, tag, id, regs, fb);
195 1.1 leo found = 1;
196 1.1 leo break;
197 1.1 leo default:
198 1.1 leo break;
199 1.1 leo }
200 1.5 leo #endif /* _MILANHW_ */
201 1.1 leo }
202 1.1 leo if (!found)
203 1.3 leo goto bad;
204 1.1 leo
205 1.1 leo /*
206 1.2 leo * Assume the device is in CGA mode. Wscons expects this too...
207 1.2 leo */
208 1.3 leo bus_space_unmap(memt, memh_fb, VGA_FB_SIZE);
209 1.3 leo if (bus_space_map(memt, 0xb8000, VGA_FB_SIZE, 0, &memh_fb)) {
210 1.3 leo got_memh = 0;
211 1.3 leo goto bad;
212 1.3 leo }
213 1.3 leo fb = bus_space_vaddr(memt, memh_fb);
214 1.2 leo
215 1.2 leo /*
216 1.1 leo * Generic parts of the initialization...
217 1.1 leo */
218 1.1 leo
219 1.19 tsutsui /* set ATC registers */
220 1.19 tsutsui for (i = 0; i < 21; i++)
221 1.19 tsutsui WAttr(regs, i, vga_atc[i]);
222 1.19 tsutsui
223 1.19 tsutsui /* set DAC palette */
224 1.19 tsutsui for (i = 0; i < 16; i++) {
225 1.19 tsutsui vgaw(regs, VDAC_ADDRESS_W, vga_atc[i]);
226 1.19 tsutsui vgaw(regs, VDAC_DATA, vga_dacpal[i * 3 + 0]);
227 1.19 tsutsui vgaw(regs, VDAC_DATA, vga_dacpal[i * 3 + 1]);
228 1.19 tsutsui vgaw(regs, VDAC_DATA, vga_dacpal[i * 3 + 2]);
229 1.1 leo }
230 1.1 leo
231 1.1 leo loadfont(regs, fb);
232 1.19 tsutsui #if NVGA_PCI > 0
233 1.19 tsutsui /* use explicit WSDISPLAY_FONTENC_IBM font that MI vga(4) assumes */
234 1.19 tsutsui vga_no_builtinfont = 1;
235 1.19 tsutsui #endif
236 1.1 leo
237 1.1 leo /*
238 1.1 leo * Clear the screen and print a message. The latter
239 1.1 leo * is of diagnostic/debug use only.
240 1.1 leo */
241 1.1 leo for (i = 50 * 80; i >= 0; i -= 2) {
242 1.1 leo fb[i] = 0x20; fb[i+1] = 0x07;
243 1.1 leo }
244 1.1 leo for (i = 56; *nbd; i += 2)
245 1.1 leo fb[i] = *nbd++;
246 1.1 leo
247 1.3 leo rv = 1;
248 1.3 leo vga_iot = iot;
249 1.3 leo vga_memt = memt;
250 1.3 leo rv = tags_valid = 1;
251 1.3 leo
252 1.15 tsutsui bad:
253 1.3 leo if (got_memh)
254 1.3 leo bus_space_unmap(memt, memh_fb, VGA_FB_SIZE);
255 1.3 leo if (got_ioh)
256 1.3 leo bus_space_unmap(iot, ioh_regs, VGA_REG_SIZE);
257 1.15 tsutsui return rv;
258 1.1 leo }
259 1.4 leo
260 1.4 leo #if NVGA_PCI > 0
261 1.4 leo void vgacnprobe(struct consdev *);
262 1.4 leo void vgacninit(struct consdev *);
263 1.4 leo
264 1.4 leo void
265 1.12 dsl vgacnprobe(struct consdev *cp)
266 1.4 leo {
267 1.15 tsutsui
268 1.4 leo if (tags_valid)
269 1.4 leo cp->cn_pri = CN_NORMAL;
270 1.4 leo }
271 1.4 leo
272 1.4 leo void
273 1.12 dsl vgacninit(struct consdev *cp)
274 1.4 leo {
275 1.15 tsutsui
276 1.4 leo if (tags_valid) {
277 1.4 leo /* XXX: Are those arguments correct? Leo */
278 1.4 leo vga_cnattach(vga_iot, vga_memt, 8, 0);
279 1.4 leo }
280 1.4 leo }
281 1.4 leo #endif /* NVGA_PCI */
282 1.1 leo
283 1.1 leo /*
284 1.1 leo * Generic VGA. Load the configured kernel font into the videomemory and
285 1.1 leo * place the card into textmode.
286 1.1 leo */
287 1.1 leo static void
288 1.15 tsutsui loadfont(volatile uint8_t *ba, uint8_t *fb)
289 1.13 dsl /* ba: Register area KVA */
290 1.15 tsutsui /* fb: Frame buffer KVA */
291 1.1 leo {
292 1.1 leo font_info *fd;
293 1.15 tsutsui uint8_t *c, *f, tmp;
294 1.15 tsutsui uint16_t z, y;
295 1.1 leo
296 1.1 leo #if defined(KFONT_8X8)
297 1.1 leo fd = &font_info_8x8;
298 1.1 leo #else
299 1.1 leo fd = &font_info_8x16;
300 1.1 leo #endif
301 1.1 leo
302 1.1 leo WAttr(ba, 0x20 | ACT_ID_ATTR_MODE_CNTL, 0x0a);
303 1.1 leo WSeq(ba, SEQ_ID_MAP_MASK, 0x04);
304 1.1 leo WSeq(ba, SEQ_ID_MEMORY_MODE, 0x06);
305 1.1 leo WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x02);
306 1.1 leo WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00);
307 1.2 leo WGfx(ba, GCT_ID_MISC, 0x0c);
308 1.1 leo
309 1.1 leo /*
310 1.1 leo * load text font into beginning of display memory. Each
311 1.1 leo * character cell is 32 bytes long (enough for 4 planes)
312 1.1 leo */
313 1.1 leo for (z = 0, c = fb; z < 256 * 32; z++)
314 1.1 leo *c++ = 0;
315 1.1 leo
316 1.15 tsutsui c = (uint8_t *)(fb) + (32 * fd->font_lo);
317 1.1 leo f = fd->font_p;
318 1.1 leo z = fd->font_lo;
319 1.1 leo for (; z <= fd->font_hi; z++, c += (32 - fd->height))
320 1.1 leo for (y = 0; y < fd->height; y++) {
321 1.1 leo *c++ = *f++;
322 1.1 leo }
323 1.1 leo
324 1.1 leo /*
325 1.1 leo * Odd/Even addressing
326 1.1 leo */
327 1.1 leo WSeq(ba, SEQ_ID_MAP_MASK, 0x03);
328 1.1 leo WSeq(ba, SEQ_ID_MEMORY_MODE, 0x03);
329 1.1 leo WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
330 1.1 leo WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x10);
331 1.2 leo WGfx(ba, GCT_ID_MISC, 0x0e);
332 1.1 leo
333 1.1 leo /*
334 1.1 leo * Font height + underline location
335 1.1 leo */
336 1.1 leo tmp = RCrt(ba, CRT_ID_MAX_ROW_ADDRESS) & 0xe0;
337 1.1 leo WCrt(ba, CRT_ID_MAX_ROW_ADDRESS, tmp | (fd->height - 1));
338 1.1 leo tmp = RCrt(ba, CRT_ID_UNDERLINE_LOC) & 0xe0;
339 1.1 leo WCrt(ba, CRT_ID_UNDERLINE_LOC, tmp | (fd->height - 1));
340 1.1 leo
341 1.1 leo /*
342 1.1 leo * Cursor setup
343 1.1 leo */
344 1.1 leo WCrt(ba, CRT_ID_CURSOR_START , 0x00);
345 1.1 leo WCrt(ba, CRT_ID_CURSOR_END , fd->height - 1);
346 1.1 leo WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
347 1.1 leo WCrt(ba, CRT_ID_CURSOR_LOC_LOW , 0x00);
348 1.1 leo
349 1.1 leo /*
350 1.1 leo * Enter text mode
351 1.1 leo */
352 1.1 leo WCrt(ba, CRT_ID_MODE_CONTROL , 0xa3);
353 1.1 leo WAttr(ba, ACT_ID_ATTR_MODE_CNTL | 0x20, 0x0a);
354 1.1 leo }
355