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