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