pci_vga.c revision 1.15 1 /* $NetBSD: pci_vga.c,v 1.15 2019/05/04 08:20:05 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.15 2019/05/04 08:20:05 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 uint8_t conscolors[3][3] = { /* background, foreground, hilite */
59 {0x0, 0x0, 0x0}, {0x30, 0x30, 0x30}, { 0x3f, 0x3f, 0x3f}
60 };
61
62 static bus_space_tag_t vga_iot, vga_memt;
63 static int tags_valid = 0;
64
65 #define VGA_REG_SIZE (8*1024)
66 #define VGA_FB_SIZE (32*1024)
67
68 /*
69 * Go look for a VGA card on the PCI-bus. This search is a
70 * stripped down version of the PCI-probe. It only looks on
71 * bus0 for VGA cards. The first card found is used.
72 */
73 int
74 check_for_vga(bus_space_tag_t iot, bus_space_tag_t memt)
75 {
76 pci_chipset_tag_t pc = NULL; /* XXX */
77 bus_space_handle_t ioh_regs, memh_fb;
78 pcitag_t tag;
79 int device, found, id, maxndevs, i, j;
80 int got_ioh, got_memh, rv;
81 int class;
82 volatile uint8_t *regs;
83 uint8_t *fb;
84 const char *nbd = "NetBSD/Atari";
85
86 found = 0;
87 tag = 0;
88 id = 0;
89 rv = 0;
90 got_ioh = 0;
91 got_memh = 0;
92 maxndevs = pci_bus_maxdevs(pc, 0);
93
94 /*
95 * Map 8Kb of registers and 32Kb frame buffer.
96 * XXX: The way the registers are mapped here is plain wrong.
97 * We should try to pin-point the region down to 3[bcd]0 (see
98 * .../dev/ic/vga.c).
99 */
100 if (bus_space_map(iot, 0, VGA_REG_SIZE, 0, &ioh_regs))
101 return 0;
102 got_ioh = 1;
103
104 if (bus_space_map(memt, 0xa0000, VGA_FB_SIZE, 0, &memh_fb))
105 goto bad;
106 got_memh = 1;
107 regs = bus_space_vaddr(iot, ioh_regs);
108 fb = bus_space_vaddr(memt, memh_fb);
109
110 for (device = 0; !found && (device < maxndevs); device++) {
111
112 tag = pci_make_tag(pc, 0, device, 0);
113 id = pci_conf_read(pc, tag, PCI_ID_REG);
114 if (id == 0 || id == 0xffffffff)
115 continue;
116
117 /*
118 * Check if we have some display device here...
119 */
120 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
121 i = 0;
122 if (PCI_CLASS(class) == PCI_CLASS_PREHISTORIC &&
123 PCI_SUBCLASS(class) == PCI_SUBCLASS_PREHISTORIC_VGA)
124 i = 1;
125 if (PCI_CLASS(class) == PCI_CLASS_DISPLAY &&
126 PCI_SUBCLASS(class) == PCI_SUBCLASS_DISPLAY_VGA)
127 i = 1;
128 if (i == 0)
129 continue;
130
131 #if _MILANHW_
132 /* Don't need to be more specific */
133 milan_vga_init(pc, tag, id, regs, fb);
134 found = 1;
135 #else
136 switch (id = PCI_PRODUCT(id)) {
137
138 /*
139 * XXX Make the inclusion of the cases dependend
140 * on config options!
141 */
142 case PCI_PRODUCT_TSENG_ET6000:
143 case PCI_PRODUCT_TSENG_ET4000_W32P_A:
144 case PCI_PRODUCT_TSENG_ET4000_W32P_B:
145 case PCI_PRODUCT_TSENG_ET4000_W32P_C:
146 case PCI_PRODUCT_TSENG_ET4000_W32P_D:
147 tseng_init(pc, tag, id, regs, fb);
148 found = 1;
149 break;
150 case PCI_PRODUCT_ATI_RAGE_PRO_PCI_P:
151 ati_vga_init(pc, tag, id, regs, fb);
152 found = 1;
153 break;
154 default:
155 break;
156 }
157 #endif /* _MILANHW_ */
158 }
159 if (!found)
160 goto bad;
161
162 /*
163 * Assume the device is in CGA mode. Wscons expects this too...
164 */
165 bus_space_unmap(memt, memh_fb, VGA_FB_SIZE);
166 if (bus_space_map(memt, 0xb8000, VGA_FB_SIZE, 0, &memh_fb)) {
167 got_memh = 0;
168 goto bad;
169 }
170 fb = bus_space_vaddr(memt, memh_fb);
171
172 /*
173 * Generic parts of the initialization...
174 */
175
176 /* B&W colors */
177 vgaw(regs, VDAC_ADDRESS_W, 0);
178 for (i = 0; i < 256; i++) {
179 j = (i & 1) ? ((i > 7) ? 2 : 1) : 0;
180 vgaw(regs, VDAC_DATA, conscolors[j][0]);
181 vgaw(regs, VDAC_DATA, conscolors[j][1]);
182 vgaw(regs, VDAC_DATA, conscolors[j][2]);
183 }
184
185 loadfont(regs, fb);
186
187 /*
188 * Clear the screen and print a message. The latter
189 * is of diagnostic/debug use only.
190 */
191 for (i = 50 * 80; i >= 0; i -= 2) {
192 fb[i] = 0x20; fb[i+1] = 0x07;
193 }
194 for (i = 56; *nbd; i += 2)
195 fb[i] = *nbd++;
196
197 rv = 1;
198 vga_iot = iot;
199 vga_memt = memt;
200 rv = tags_valid = 1;
201
202 bad:
203 if (got_memh)
204 bus_space_unmap(memt, memh_fb, VGA_FB_SIZE);
205 if (got_ioh)
206 bus_space_unmap(iot, ioh_regs, VGA_REG_SIZE);
207 return rv;
208 }
209
210 #if NVGA_PCI > 0
211 void vgacnprobe(struct consdev *);
212 void vgacninit(struct consdev *);
213
214 void
215 vgacnprobe(struct consdev *cp)
216 {
217
218 if (tags_valid)
219 cp->cn_pri = CN_NORMAL;
220 }
221
222 void
223 vgacninit(struct consdev *cp)
224 {
225
226 if (tags_valid) {
227 /* XXX: Are those arguments correct? Leo */
228 vga_cnattach(vga_iot, vga_memt, 8, 0);
229 }
230 }
231 #endif /* NVGA_PCI */
232
233 /*
234 * Generic VGA. Load the configured kernel font into the videomemory and
235 * place the card into textmode.
236 */
237 static void
238 loadfont(volatile uint8_t *ba, uint8_t *fb)
239 /* ba: Register area KVA */
240 /* fb: Frame buffer KVA */
241 {
242 font_info *fd;
243 uint8_t *c, *f, tmp;
244 uint16_t z, y;
245
246 #if defined(KFONT_8X8)
247 fd = &font_info_8x8;
248 #else
249 fd = &font_info_8x16;
250 #endif
251
252 WAttr(ba, 0x20 | ACT_ID_ATTR_MODE_CNTL, 0x0a);
253 WSeq(ba, SEQ_ID_MAP_MASK, 0x04);
254 WSeq(ba, SEQ_ID_MEMORY_MODE, 0x06);
255 WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x02);
256 WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x00);
257 WGfx(ba, GCT_ID_MISC, 0x0c);
258
259 /*
260 * load text font into beginning of display memory. Each
261 * character cell is 32 bytes long (enough for 4 planes)
262 */
263 for (z = 0, c = fb; z < 256 * 32; z++)
264 *c++ = 0;
265
266 c = (uint8_t *)(fb) + (32 * fd->font_lo);
267 f = fd->font_p;
268 z = fd->font_lo;
269 for (; z <= fd->font_hi; z++, c += (32 - fd->height))
270 for (y = 0; y < fd->height; y++) {
271 *c++ = *f++;
272 }
273
274 /*
275 * Odd/Even addressing
276 */
277 WSeq(ba, SEQ_ID_MAP_MASK, 0x03);
278 WSeq(ba, SEQ_ID_MEMORY_MODE, 0x03);
279 WGfx(ba, GCT_ID_READ_MAP_SELECT, 0x00);
280 WGfx(ba, GCT_ID_GRAPHICS_MODE, 0x10);
281 WGfx(ba, GCT_ID_MISC, 0x0e);
282
283 /*
284 * Font height + underline location
285 */
286 tmp = RCrt(ba, CRT_ID_MAX_ROW_ADDRESS) & 0xe0;
287 WCrt(ba, CRT_ID_MAX_ROW_ADDRESS, tmp | (fd->height - 1));
288 tmp = RCrt(ba, CRT_ID_UNDERLINE_LOC) & 0xe0;
289 WCrt(ba, CRT_ID_UNDERLINE_LOC, tmp | (fd->height - 1));
290
291 /*
292 * Cursor setup
293 */
294 WCrt(ba, CRT_ID_CURSOR_START , 0x00);
295 WCrt(ba, CRT_ID_CURSOR_END , fd->height - 1);
296 WCrt(ba, CRT_ID_CURSOR_LOC_HIGH, 0x00);
297 WCrt(ba, CRT_ID_CURSOR_LOC_LOW , 0x00);
298
299 /*
300 * Enter text mode
301 */
302 WCrt(ba, CRT_ID_MODE_CONTROL , 0xa3);
303 WAttr(ba, ACT_ID_ATTR_MODE_CNTL | 0x20, 0x0a);
304 }
305