Home | History | Annotate | Line # | Download | only in pci
voodoofb.c revision 1.5
      1 /*	$NetBSD: voodoofb.c,v 1.5 2006/05/04 02:18:11 simonb Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * A console driver for 3Dfx Voodoo3 graphics boards
     32  * Thanks to Andreas Drewke (andreas_dr (at) gmx.de) for his Voodoo3 driver for BeOS
     33  * which I used as reference / documentation
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 __KERNEL_RCSID(0, "$NetBSD: voodoofb.c,v 1.5 2006/05/04 02:18:11 simonb Exp $");
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/device.h>
     43 #include <sys/malloc.h>
     44 #include <sys/callout.h>
     45 
     46 #include <uvm/uvm_extern.h>
     47 
     48 #if defined(macppc) || defined (sparc64) || defined(ofppc)
     49 #define HAVE_OPENFIRMWARE
     50 #endif
     51 
     52 #ifdef HAVE_OPENFIRMWARE
     53 #include <dev/ofw/openfirm.h>
     54 #include <dev/ofw/ofw_pci.h>
     55 #endif
     56 
     57 #include <dev/videomode/videomode.h>
     58 
     59 #include <dev/pci/pcivar.h>
     60 #include <dev/pci/pcireg.h>
     61 #include <dev/pci/pcidevs.h>
     62 #include <dev/pci/pciio.h>
     63 #include <dev/pci/voodoofbreg.h>
     64 
     65 #include <dev/wscons/wsdisplayvar.h>
     66 #include <dev/wscons/wsconsio.h>
     67 #include <dev/wsfont/wsfont.h>
     68 #include <dev/rasops/rasops.h>
     69 #include <dev/wscons/wsdisplay_vconsvar.h>
     70 
     71 #include "opt_wsemul.h"
     72 
     73 struct voodoofb_softc {
     74 	struct device sc_dev;
     75 	pci_chipset_tag_t sc_pc;
     76 	pcitag_t sc_pcitag;
     77 
     78 	bus_space_tag_t sc_memt;
     79 	bus_space_tag_t sc_iot;
     80 	bus_space_handle_t sc_memh;
     81 
     82 	bus_space_tag_t sc_regt;
     83 	bus_space_tag_t sc_fbt;
     84 	bus_space_tag_t sc_ioregt;
     85 	bus_space_handle_t sc_regh;
     86 	bus_space_handle_t sc_fbh;
     87 	bus_space_handle_t sc_ioregh;
     88 	bus_addr_t sc_regs, sc_fb, sc_ioreg;
     89 	bus_size_t sc_regsize, sc_fbsize, sc_ioregsize;
     90 
     91 	void *sc_ih;
     92 
     93 	size_t memsize;
     94 	int memtype;
     95 
     96 	int bits_per_pixel;
     97 	int width, height, linebytes;
     98 
     99 	int sc_mode;
    100 	uint32_t sc_bg;
    101 
    102 	u_char sc_cmap_red[256];
    103 	u_char sc_cmap_green[256];
    104 	u_char sc_cmap_blue[256];
    105 	int sc_dacw;
    106 
    107 	struct vcons_data vd;
    108 };
    109 
    110 struct voodoo_regs {
    111 	uint8_t vr_crtc[31];
    112 	uint8_t vr_graph[9];
    113 	uint8_t vr_attr[21];
    114 	uint8_t vr_seq[5];
    115 };
    116 
    117 static struct vcons_screen voodoofb_console_screen;
    118 
    119 extern const u_char rasops_cmap[768];
    120 
    121 static int	voodoofb_match(struct device *, struct cfdata *, void *);
    122 static void	voodoofb_attach(struct device *, struct device *, void *);
    123 
    124 CFATTACH_DECL(voodoofb, sizeof(struct voodoofb_softc), voodoofb_match,
    125     voodoofb_attach, NULL, NULL);
    126 
    127 static int	voodoofb_is_console(struct pci_attach_args *);
    128 static void 	voodoofb_init(struct voodoofb_softc *);
    129 
    130 static void	voodoofb_cursor(void *, int, int, int);
    131 static void	voodoofb_putchar(void *, int, int, u_int, long);
    132 static void	voodoofb_copycols(void *, int, int, int, int);
    133 static void	voodoofb_erasecols(void *, int, int, int, long);
    134 static void	voodoofb_copyrows(void *, int, int, int);
    135 static void	voodoofb_eraserows(void *, int, int, long);
    136 
    137 #if 0
    138 static int	voodoofb_allocattr(void *, int, int, int, long *);
    139 static void	voodoofb_scroll(void *, void *, int);
    140 static int	voodoofb_load_font(void *, void *, struct wsdisplay_font *);
    141 #endif
    142 
    143 static int	voodoofb_putcmap(struct voodoofb_softc *,
    144 			    struct wsdisplay_cmap *);
    145 static int 	voodoofb_getcmap(struct voodoofb_softc *,
    146 			    struct wsdisplay_cmap *);
    147 static int 	voodoofb_putpalreg(struct voodoofb_softc *, uint8_t, uint8_t,
    148 			    uint8_t, uint8_t);
    149 static void	voodoofb_bitblt(struct voodoofb_softc *, int, int, int, int,
    150 			    int, int);
    151 static void	voodoofb_rectfill(struct voodoofb_softc *, int, int, int, int,
    152 			    int);
    153 static void	voodoofb_rectinvert(struct voodoofb_softc *, int, int, int,
    154 			    int);
    155 static void	voodoofb_setup_mono(struct voodoofb_softc *, int, int, int,
    156 			    int, uint32_t, uint32_t);
    157 static void	voodoofb_feed_line(struct voodoofb_softc *, int, uint8_t *);
    158 
    159 #ifdef VOODOOFB_DEBUG
    160 static void	voodoofb_showpal(struct voodoofb_softc *);
    161 #endif
    162 
    163 static void	voodoofb_wait_idle(struct voodoofb_softc *);
    164 
    165 static int	voodoofb_intr(void *);
    166 
    167 static void	voodoofb_set_videomode(struct voodoofb_softc *,
    168 			    const struct videomode *);
    169 
    170 struct wsscreen_descr voodoofb_defaultscreen = {
    171 	"default",
    172 	0, 0,
    173 	NULL,
    174 	8, 16,
    175 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    176 };
    177 
    178 const struct wsscreen_descr *_voodoofb_scrlist[] = {
    179 	&voodoofb_defaultscreen,
    180 	/* XXX other formats, graphics screen? */
    181 };
    182 
    183 struct wsscreen_list voodoofb_screenlist = {
    184 	sizeof(_voodoofb_scrlist) / sizeof(struct wsscreen_descr *), _voodoofb_scrlist
    185 };
    186 
    187 static int	voodoofb_ioctl(void *, void *, u_long, caddr_t, int,
    188 		    struct lwp *);
    189 static paddr_t	voodoofb_mmap(void *, void *, off_t, int);
    190 
    191 static void	voodoofb_clearscreen(struct voodoofb_softc *);
    192 static void	voodoofb_init_screen(void *, struct vcons_screen *, int,
    193 			    long *);
    194 
    195 
    196 struct wsdisplay_accessops voodoofb_accessops = {
    197 	voodoofb_ioctl,
    198 	voodoofb_mmap,
    199 	NULL,	/* load_font */
    200 	NULL,	/* polls */
    201 	NULL,	/* scroll */
    202 };
    203 
    204 /*
    205  * Inline functions for getting access to register aperture.
    206  */
    207 static inline void
    208 voodoo3_write32(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
    209 {
    210 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, val);
    211 }
    212 
    213 static inline uint32_t
    214 voodoo3_read32(struct voodoofb_softc *sc, uint32_t reg)
    215 {
    216 	return bus_space_read_4(sc->sc_regt, sc->sc_regh, reg);
    217 }
    218 
    219 static inline void
    220 voodoo3_write_crtc(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    221 {
    222 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_INDEX - 0x300, reg);
    223 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_DATA - 0x300, val);
    224 }
    225 
    226 static inline void
    227 voodoo3_write_seq(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    228 {
    229 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_INDEX - 0x300, reg);
    230 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_DATA - 0x300, val);
    231 }
    232 
    233 static inline void
    234 voodoo3_write_gra(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    235 {
    236 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_INDEX - 0x300, reg);
    237 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_DATA - 0x300, val);
    238 }
    239 
    240 static inline void
    241 voodoo3_write_attr(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    242 {
    243 	volatile uint8_t junk;
    244 	uint8_t index;
    245 
    246 	junk = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, IS1_R - 0x300);
    247 	index = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300);
    248 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, reg);
    249 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, val);
    250 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, index);
    251 }
    252 
    253 static inline void
    254 vga_outb(struct voodoofb_softc *sc, uint32_t reg,  uint8_t val)
    255 {
    256 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, reg - 0x300, val);
    257 }
    258 
    259 /* wait until there's room for len bytes in the FIFO */
    260 static inline void
    261 voodoo3_make_room(struct voodoofb_softc *sc, int len)
    262 {
    263 	while ((voodoo3_read32(sc, STATUS) & 0x1f) < len);
    264 }
    265 
    266 static void
    267 voodoofb_wait_idle(struct voodoofb_softc *sc)
    268 {
    269 	int i = 0;
    270 
    271 	voodoo3_make_room(sc, 1);
    272 	voodoo3_write32(sc, COMMAND_3D, COMMAND_3D_NOP);
    273 
    274 	while (1) {
    275 		i = (voodoo3_read32(sc, STATUS) & STATUS_BUSY) ? 0 : i + 1;
    276 		if(i == 3) break;
    277 	}
    278 }
    279 
    280 static int
    281 voodoofb_match(struct device *parent, struct cfdata *match, void *aux)
    282 {
    283 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    284 
    285 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    286 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    287 		return 0;
    288 	if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
    289 	    (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_VOODOO3))
    290 		return 100;
    291 	return 0;
    292 }
    293 
    294 static void
    295 voodoofb_attach(struct device *parent, struct device *self, void *aux)
    296 {
    297 	struct voodoofb_softc *sc = (void *)self;
    298 	struct pci_attach_args *pa = aux;
    299 	char devinfo[256];
    300 	struct wsemuldisplaydev_attach_args aa;
    301 	struct rasops_info *ri;
    302 	pci_intr_handle_t ih;
    303 	ulong defattr;
    304 	const char *intrstr;
    305 	int console, width, height, node, i, j;
    306 #ifdef HAVE_OPENFIRMWARE
    307 	int linebytes, depth;
    308 #endif
    309 	uint32_t bg, fg, ul;
    310 
    311 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    312 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    313 	sc->sc_pc = pa->pa_pc;
    314 	sc->sc_pcitag = pa->pa_tag;
    315 	sc->sc_dacw = -1;
    316 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    317 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    318 
    319 	sc->sc_memt = pa->pa_memt;
    320 	sc->sc_iot = pa->pa_iot;
    321 
    322 	/* the framebuffer */
    323 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM,
    324 	    BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE |
    325 	    BUS_SPACE_MAP_LINEAR,
    326 	    &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    327 		printf("%s: failed to map the frame buffer.\n",
    328 		    sc->sc_dev.dv_xname);
    329 	}
    330 
    331 	/* memory-mapped registers */
    332 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    333 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
    334 		printf("%s: failed to map memory-mapped registers.\n",
    335 		    sc->sc_dev.dv_xname);
    336 	}
    337 
    338 	/* IO-mapped registers */
    339 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
    340 	    &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
    341 	    &sc->sc_ioregsize)) {
    342 		printf("%s: failed to map IO-mapped registers.\n",
    343 		    sc->sc_dev.dv_xname);
    344 	}
    345 	voodoofb_init(sc);
    346 
    347 	/* we should read these from the chip instead of depending on OF */
    348 	width = height = -1;
    349 
    350 #ifdef HAVE_OPENFIRMWARE
    351 	if (OF_getprop(node, "width", &width, 4) != 4)
    352 		OF_interpret("screen-width", 1, &width);
    353 	if (OF_getprop(node, "height", &height, 4) != 4)
    354 		OF_interpret("screen-height", 1, &height);
    355 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    356 		linebytes = width;			/* XXX */
    357 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    358 		depth = 8;				/* XXX */
    359 
    360 	if (width == -1 || height == -1)
    361 		return;
    362 
    363 	sc->width = width;
    364 	sc->height = height;
    365 	sc->bits_per_pixel = depth;
    366 	sc->linebytes = linebytes;
    367 	printf("%s: initial resolution %dx%d, %d bit\n", sc->sc_dev.dv_xname,
    368 	    sc->width, sc->height, sc->bits_per_pixel);
    369 #endif
    370 
    371 	/* XXX this should at least be configurable via kernel config */
    372 	voodoofb_set_videomode(sc, &videomode_list[16]);
    373 
    374 	vcons_init(&sc->vd, sc, &voodoofb_defaultscreen, &voodoofb_accessops);
    375 	sc->vd.init_screen = voodoofb_init_screen;
    376 
    377 	console = voodoofb_is_console(pa);
    378 
    379 	ri = &voodoofb_console_screen.scr_ri;
    380 	if (console) {
    381 		vcons_init_screen(&sc->vd, &voodoofb_console_screen, 1,
    382 		    &defattr);
    383 		voodoofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    384 
    385 		voodoofb_defaultscreen.textops = &ri->ri_ops;
    386 		voodoofb_defaultscreen.capabilities = ri->ri_caps;
    387 		voodoofb_defaultscreen.nrows = ri->ri_rows;
    388 		voodoofb_defaultscreen.ncols = ri->ri_cols;
    389 		wsdisplay_cnattach(&voodoofb_defaultscreen, ri, 0, 0, defattr);
    390 	} else {
    391 		/*
    392 		 * since we're not the console we can postpone the rest
    393 		 * until someone actually allocates a screen for us
    394 		 */
    395 		voodoofb_set_videomode(sc, &videomode_list[0]);
    396 	}
    397 
    398 	printf("%s: %d MB aperture at 0x%08x, %d MB registers at 0x%08x\n",
    399 	    sc->sc_dev.dv_xname, (u_int)(sc->sc_fbsize >> 20),
    400 	    (u_int)sc->sc_fb, (u_int)(sc->sc_regsize >> 20),
    401 	    (u_int)sc->sc_regs);
    402 #ifdef VOODOOFB_DEBUG
    403 	printf("fb: %08lx\n", (ulong)ri->ri_bits);
    404 #endif
    405 
    406 	j = 0;
    407 	for (i = 0; i < 256; i++) {
    408 		voodoofb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    409 		    rasops_cmap[j + 2]);
    410 		j += 3;
    411 	}
    412 
    413 	/* Interrupt. We don't use it for anything yet */
    414 	if (pci_intr_map(pa, &ih)) {
    415 		printf("%s: failed to map interrupt\n", sc->sc_dev.dv_xname);
    416 		return;
    417 	}
    418 
    419 	intrstr = pci_intr_string(sc->sc_pc, ih);
    420 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET, voodoofb_intr,
    421 	    sc);
    422 	if (sc->sc_ih == NULL) {
    423 		printf("%s: failed to establish interrupt",
    424 		    sc->sc_dev.dv_xname);
    425 		if (intrstr != NULL)
    426 			printf(" at %s", intrstr);
    427 		printf("\n");
    428 		return;
    429 	}
    430 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    431 
    432 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    433 	sc->sc_bg = ri->ri_devcmap[bg];
    434 	voodoofb_clearscreen(sc);
    435 
    436 	aa.console = console;
    437 	aa.scrdata = &voodoofb_screenlist;
    438 	aa.accessops = &voodoofb_accessops;
    439 	aa.accesscookie = &sc->vd;
    440 
    441 	config_found(self, &aa, wsemuldisplaydevprint);
    442 }
    443 
    444 static int
    445 voodoofb_putpalreg(struct voodoofb_softc *sc, uint8_t index, uint8_t r,
    446     uint8_t g, uint8_t b)
    447 {
    448 	uint32_t color;
    449 
    450 	sc->sc_cmap_red[index] = r;
    451 	sc->sc_cmap_green[index] = g;
    452 	sc->sc_cmap_blue[index] = b;
    453 
    454 	color = (r << 16) | (g << 8) | b;
    455 	voodoo3_make_room(sc, 2);
    456 	voodoo3_write32(sc, DACADDR, index);
    457 	voodoo3_write32(sc, DACDATA, color);
    458 
    459 	return 0;
    460 }
    461 
    462 static int
    463 voodoofb_putcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
    464 {
    465 	u_char *r, *g, *b;
    466 	u_int index = cm->index;
    467 	u_int count = cm->count;
    468 	int i, error;
    469 	u_char rbuf[256], gbuf[256], bbuf[256];
    470 
    471 #ifdef VOODOOFB_DEBUG
    472 	printf("putcmap: %d %d\n",index, count);
    473 #endif
    474 	if (cm->index >= 256 || cm->count > 256 ||
    475 	    (cm->index + cm->count) > 256)
    476 		return EINVAL;
    477 	error = copyin(cm->red, &rbuf[index], count);
    478 	if (error)
    479 		return error;
    480 	error = copyin(cm->green, &gbuf[index], count);
    481 	if (error)
    482 		return error;
    483 	error = copyin(cm->blue, &bbuf[index], count);
    484 	if (error)
    485 		return error;
    486 
    487 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    488 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    489 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    490 
    491 	r = &sc->sc_cmap_red[index];
    492 	g = &sc->sc_cmap_green[index];
    493 	b = &sc->sc_cmap_blue[index];
    494 
    495 	for (i = 0; i < count; i++) {
    496 		voodoofb_putpalreg(sc, index, *r, *g, *b);
    497 		index++;
    498 		r++, g++, b++;
    499 	}
    500 	return 0;
    501 }
    502 
    503 static int
    504 voodoofb_getcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
    505 {
    506 	u_int index = cm->index;
    507 	u_int count = cm->count;
    508 	int error;
    509 
    510 	if (index >= 255 || count > 256 || index + count > 256)
    511 		return EINVAL;
    512 
    513 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    514 	if (error)
    515 		return error;
    516 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    517 	if (error)
    518 		return error;
    519 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    520 	if (error)
    521 		return error;
    522 
    523 	return 0;
    524 }
    525 
    526 static int
    527 voodoofb_is_console(struct pci_attach_args *pa)
    528 {
    529 
    530 #ifdef HAVE_OPENFIRMWARE
    531 	/* check if we're the /chosen console device */
    532 	int chosen, stdout, node, us;
    533 
    534 	us=pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    535 	chosen = OF_finddevice("/chosen");
    536 	OF_getprop(chosen, "stdout", &stdout, 4);
    537 	node = OF_instance_to_package(stdout);
    538 	return(us == node);
    539 #else
    540 	/* XXX how do we know we're console on i386? */
    541 	return 1;
    542 #endif
    543 }
    544 
    545 static void
    546 voodoofb_clearscreen(struct voodoofb_softc *sc)
    547 {
    548 	voodoofb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
    549 }
    550 
    551 /*
    552  * wsdisplay_emulops
    553  */
    554 
    555 static void
    556 voodoofb_cursor(void *cookie, int on, int row, int col)
    557 {
    558 	struct rasops_info *ri = cookie;
    559 	struct vcons_screen *scr = ri->ri_hw;
    560 	struct voodoofb_softc *sc = scr->scr_cookie;
    561 	int x, y, wi, he;
    562 
    563 	wi = ri->ri_font->fontwidth;
    564 	he = ri->ri_font->fontheight;
    565 
    566 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    567 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    568 		y = ri->ri_crow * he + ri->ri_yorigin;
    569 		if (ri->ri_flg & RI_CURSOR) {
    570 			voodoofb_rectinvert(sc, x, y, wi, he);
    571 			ri->ri_flg &= ~RI_CURSOR;
    572 		}
    573 		ri->ri_crow = row;
    574 		ri->ri_ccol = col;
    575 		if (on)
    576 		{
    577 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    578 			y = ri->ri_crow * he + ri->ri_yorigin;
    579 			voodoofb_rectinvert(sc, x, y, wi, he);
    580 			ri->ri_flg |= RI_CURSOR;
    581 		}
    582 	} else {
    583 		ri->ri_flg &= ~RI_CURSOR;
    584 		ri->ri_crow = row;
    585 		ri->ri_ccol = col;
    586 	}
    587 }
    588 
    589 #if 0
    590 int
    591 voodoofb_mapchar(void *cookie, int uni, u_int *index)
    592 {
    593 	return 0;
    594 }
    595 #endif
    596 
    597 static void
    598 voodoofb_putchar(void *cookie, int row, int col, u_int c, long attr)
    599 {
    600 	struct rasops_info *ri = cookie;
    601 	struct vcons_screen *scr = ri->ri_hw;
    602 	struct voodoofb_softc *sc = scr->scr_cookie;
    603 
    604 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    605 		uint8_t *data;
    606 		int fg, bg, uc, i;
    607 		int x, y, wi, he;
    608 
    609 		wi = ri->ri_font->fontwidth;
    610 		he = ri->ri_font->fontheight;
    611 
    612 		if (!CHAR_IN_FONT(c, ri->ri_font))
    613 			return;
    614 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    615 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    616 		x = ri->ri_xorigin + col * wi;
    617 		y = ri->ri_yorigin + row * he;
    618 		if (c == 0x20) {
    619 			voodoofb_rectfill(sc, x, y, wi, he, bg);
    620 		} else {
    621 			uc = c-ri->ri_font->firstchar;
    622 			data = (uint8_t *)ri->ri_font->data + uc *
    623 			    ri->ri_fontscale;
    624 				voodoofb_setup_mono(sc, x, y, wi, he, fg, bg);
    625 			for (i = 0; i < he; i++) {
    626 				voodoofb_feed_line(sc,
    627 				    ri->ri_font->stride, data);
    628 				data += ri->ri_font->stride;
    629 			}
    630 		}
    631 	}
    632 }
    633 
    634 static void
    635 voodoofb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    636 {
    637 	struct rasops_info *ri = cookie;
    638 	struct vcons_screen *scr = ri->ri_hw;
    639 	struct voodoofb_softc *sc = scr->scr_cookie;
    640 	int32_t xs, xd, y, width, height;
    641 
    642 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    643 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    644 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    645 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    646 		width = ri->ri_font->fontwidth * ncols;
    647 		height = ri->ri_font->fontheight;
    648 		voodoofb_bitblt(sc, xs, y, xd, y, width, height);
    649 	}
    650 }
    651 
    652 static void
    653 voodoofb_erasecols(void *cookie, int row, int startcol, int ncols,
    654     long fillattr)
    655 {
    656 	struct rasops_info *ri = cookie;
    657 	struct vcons_screen *scr = ri->ri_hw;
    658 	struct voodoofb_softc *sc = scr->scr_cookie;
    659 	int32_t x, y, width, height, fg, bg, ul;
    660 
    661 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    662 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    663 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    664 		width = ri->ri_font->fontwidth * ncols;
    665 		height = ri->ri_font->fontheight;
    666 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    667 
    668 		voodoofb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    669 	}
    670 }
    671 
    672 static void
    673 voodoofb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    674 {
    675 	struct rasops_info *ri = cookie;
    676 	struct vcons_screen *scr = ri->ri_hw;
    677 	struct voodoofb_softc *sc = scr->scr_cookie;
    678 	int32_t x, ys, yd, width, height;
    679 
    680 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    681 		x = ri->ri_xorigin;
    682 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    683 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    684 		width = ri->ri_emuwidth;
    685 		height = ri->ri_font->fontheight * nrows;
    686 		voodoofb_bitblt(sc, x, ys, x, yd, width, height);
    687 	}
    688 }
    689 
    690 static void
    691 voodoofb_eraserows(void *cookie, int row, int nrows, long fillattr)
    692 {
    693 	struct rasops_info *ri = cookie;
    694 	struct vcons_screen *scr = ri->ri_hw;
    695 	struct voodoofb_softc *sc = scr->scr_cookie;
    696 	int32_t x, y, width, height, fg, bg, ul;
    697 
    698 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    699 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    700 		if ((row == 0) && (nrows == ri->ri_rows)) {
    701 			/* clear the whole screen */
    702 			voodoofb_rectfill(sc, 0, 0, ri->ri_width,
    703 			    ri->ri_height, ri->ri_devcmap[bg]);
    704 		} else {
    705 			x = ri->ri_xorigin;
    706 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    707 			width = ri->ri_emuwidth;
    708 			height = ri->ri_font->fontheight * nrows;
    709 			voodoofb_rectfill(sc, x, y, width, height,
    710 			    ri->ri_devcmap[bg]);
    711 		}
    712 	}
    713 }
    714 
    715 static void
    716 voodoofb_bitblt(struct voodoofb_softc *sc, int xs, int ys, int xd, int yd, int width, int height)
    717 {
    718 	uint32_t fmt, blitcmd;
    719 
    720 	fmt = sc->linebytes | ((sc->bits_per_pixel +
    721 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
    722 	blitcmd = COMMAND_2D_S2S_BITBLT | (ROP_COPY << 24);
    723 
    724 	if (xs <= xd) {
    725 	        blitcmd |= BIT(14);
    726 		xs += (width - 1);
    727 		xd += (width - 1);
    728 	}
    729 	if (ys <= yd) {
    730 		blitcmd |= BIT(15);
    731 		ys += (height - 1);
    732 		yd += (height - 1);
    733 	}
    734 	voodoo3_make_room(sc, 6);
    735 
    736 	voodoo3_write32(sc, SRCFORMAT, fmt);
    737 	voodoo3_write32(sc, DSTFORMAT, fmt);
    738 	voodoo3_write32(sc, DSTSIZE,   width | (height << 16));
    739 	voodoo3_write32(sc, DSTXY,     xd | (yd << 16));
    740 	voodoo3_write32(sc, SRCXY, xs | (ys << 16));
    741 	voodoo3_write32(sc, COMMAND_2D, blitcmd | SST_2D_GO);
    742 }
    743 
    744 static void
    745 voodoofb_rectfill(struct voodoofb_softc *sc, int x, int y, int width,
    746     int height, int colour)
    747 {
    748 	uint32_t fmt, col;
    749 
    750 	col = (colour << 24) | (colour << 16) | (colour << 8) | colour;
    751 	fmt = sc->linebytes | ((sc->bits_per_pixel +
    752 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
    753 
    754 	voodoo3_make_room(sc, 6);
    755 	voodoo3_write32(sc, DSTFORMAT, fmt);
    756 	voodoo3_write32(sc, COLORFORE, colour);
    757 	voodoo3_write32(sc, COLORBACK, colour);
    758 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT | (ROP_COPY << 24));
    759 	voodoo3_write32(sc, DSTSIZE,    width | (height << 16));
    760 	voodoo3_write32(sc, LAUNCH_2D,  x | (y << 16));
    761 }
    762 
    763 static void
    764 voodoofb_rectinvert(struct voodoofb_softc *sc, int x, int y, int width,
    765     int height)
    766 {
    767 	uint32_t fmt;
    768 
    769 	fmt = sc->linebytes | ((sc->bits_per_pixel +
    770 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
    771 
    772 	voodoo3_make_room(sc, 6);
    773 	voodoo3_write32(sc, DSTFORMAT,	fmt);
    774 	voodoo3_write32(sc, COMMAND_2D,	COMMAND_2D_FILLRECT |
    775 	    (ROP_INVERT << 24));
    776 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
    777 	voodoo3_write32(sc, DSTXY,	x | (y << 16));
    778 	voodoo3_write32(sc, LAUNCH_2D,	x | (y << 16));
    779 }
    780 
    781 static void
    782 voodoofb_setup_mono(struct voodoofb_softc *sc, int xd, int yd, int width, int height, uint32_t fg,
    783 					uint32_t bg)
    784 {
    785 	uint32_t dfmt, sfmt = sc->linebytes;
    786 
    787 	dfmt = sc->linebytes | ((sc->bits_per_pixel +
    788 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
    789 
    790 	voodoo3_make_room(sc, 9);
    791 	voodoo3_write32(sc, SRCFORMAT,	sfmt);
    792 	voodoo3_write32(sc, DSTFORMAT,	dfmt);
    793 	voodoo3_write32(sc, COLORFORE,	fg);
    794 	voodoo3_write32(sc, COLORBACK,	bg);
    795 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
    796 	voodoo3_write32(sc, DSTXY,	xd | (yd << 16));
    797 	voodoo3_write32(sc, SRCXY,	0);
    798 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
    799 	    (ROP_COPY << 24) | SST_2D_GO);
    800 
    801 	/* now feed the data into the chip */
    802 }
    803 
    804 static void
    805 voodoofb_feed_line(struct voodoofb_softc *sc, int count, uint8_t *data)
    806 {
    807 	int i;
    808 	uint32_t latch = 0, bork;
    809 	int shift = 0;
    810 
    811 	voodoo3_make_room(sc, count);
    812 	for (i = 0; i < count; i++) {
    813 		bork = data[i];
    814 		latch |= (bork << shift);
    815 		if (shift == 24) {
    816 			voodoo3_write32(sc, LAUNCH_2D, latch);
    817 			latch = 0;
    818 			shift = 0;
    819 		} else
    820 			shift += 8;
    821 		}
    822 	if (shift != 24)
    823 		voodoo3_write32(sc, LAUNCH_2D, latch);
    824 }
    825 
    826 #ifdef VOODOOFB_DEBUG
    827 static void
    828 voodoofb_showpal(struct voodoofb_softc *sc)
    829 {
    830 	int i, x = 0;
    831 
    832 	for (i = 0; i < 16; i++) {
    833 		voodoofb_rectfill(sc, x, 0, 64, 64, i);
    834 		x += 64;
    835 	}
    836 }
    837 #endif
    838 
    839 #if 0
    840 static int
    841 voodoofb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    842 {
    843 
    844 	return 0;
    845 }
    846 #endif
    847 
    848 /*
    849  * wsdisplay_accessops
    850  */
    851 
    852 static int
    853 voodoofb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
    854 	struct lwp *l)
    855 {
    856 	struct vcons_data *vd = v;
    857 	struct voodoofb_softc *sc = vd->cookie;
    858 	struct wsdisplay_fbinfo *wdf;
    859 	struct vcons_screen *ms = vd->active;
    860 
    861 	switch (cmd) {
    862 		case WSDISPLAYIO_GTYPE:
    863 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    864 			return 0;
    865 
    866 		case WSDISPLAYIO_GINFO:
    867 			wdf = (void *)data;
    868 			wdf->height = ms->scr_ri.ri_height;
    869 			wdf->width = ms->scr_ri.ri_width;
    870 			wdf->depth = ms->scr_ri.ri_depth;
    871 			wdf->cmsize = 256;
    872 			return 0;
    873 
    874 		case WSDISPLAYIO_GETCMAP:
    875 			return voodoofb_getcmap(sc,
    876 			    (struct wsdisplay_cmap *)data);
    877 
    878 		case WSDISPLAYIO_PUTCMAP:
    879 			return voodoofb_putcmap(sc,
    880 			    (struct wsdisplay_cmap *)data);
    881 
    882 		/* PCI config read/write passthrough. */
    883 		case PCI_IOC_CFGREAD:
    884 		case PCI_IOC_CFGWRITE:
    885 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    886 			    cmd, data, flag, l));
    887 
    888 		case WSDISPLAYIO_SMODE:
    889 			{
    890 				int new_mode = *(int*)data;
    891 				if (new_mode != sc->sc_mode)
    892 				{
    893 					sc->sc_mode = new_mode;
    894 					if(new_mode == WSDISPLAYIO_MODE_EMUL)
    895 					{
    896 						int i;
    897 
    898 						/* restore the palette */
    899 						for (i = 0; i < 256; i++) {
    900 							voodoofb_putpalreg(sc,
    901 							   i,
    902 							   sc->sc_cmap_red[i],
    903 							   sc->sc_cmap_green[i],
    904 							   sc->sc_cmap_blue[i]);
    905 						}
    906 						vcons_redraw_screen(ms);
    907 					}
    908 				}
    909 			}
    910 			return 0;
    911 	}
    912 	return EPASSTHROUGH;
    913 }
    914 
    915 static paddr_t
    916 voodoofb_mmap(void *v, void *vs, off_t offset, int prot)
    917 {
    918 	struct vcons_data *vd = v;
    919 	struct voodoofb_softc *sc = vd->cookie;
    920 	paddr_t pa;
    921 
    922 	/* 'regular' framebuffer mmap()ing */
    923 	if (offset < sc->sc_fbsize) {
    924 		pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
    925 		    BUS_SPACE_MAP_LINEAR);
    926 		return pa;
    927 	}
    928 
    929 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    930 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    931 		    BUS_SPACE_MAP_LINEAR);
    932 		return pa;
    933 	}
    934 
    935 	if ((offset >= sc->sc_regs) && (offset < (sc->sc_regs +
    936 	    sc->sc_regsize))) {
    937 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    938 		    BUS_SPACE_MAP_LINEAR);
    939 		return pa;
    940 	}
    941 
    942 	/* allow mapping of IO space */
    943 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    944 		pa = bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
    945 		    BUS_SPACE_MAP_LINEAR);
    946 		return pa;
    947 	}
    948 #ifdef OFB_ALLOW_OTHERS
    949 	if (offset >= 0x80000000) {
    950 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    951 		    BUS_SPACE_MAP_LINEAR);
    952 		return pa;
    953 	}
    954 #endif
    955 	return -1;
    956 }
    957 
    958 static void
    959 voodoofb_init_screen(void *cookie, struct vcons_screen *scr,
    960     int existing, long *defattr)
    961 {
    962 	struct voodoofb_softc *sc = cookie;
    963 	struct rasops_info *ri = &scr->scr_ri;
    964 
    965 	ri->ri_depth = sc->bits_per_pixel;
    966 	ri->ri_width = sc->width;
    967 	ri->ri_height = sc->height;
    968 	ri->ri_stride = sc->width;
    969 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    970 
    971 	ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
    972 
    973 #ifdef VOODOOFB_DEBUG
    974 	printf("addr: %08lx\n", (ulong)ri->ri_bits);
    975 #endif
    976 	if (existing) {
    977 		ri->ri_flg |= RI_CLEAR;
    978 	}
    979 
    980 	rasops_init(ri, sc->height/8, sc->width/8);
    981 	ri->ri_caps = WSSCREEN_WSCOLORS;
    982 
    983 	rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
    984 		    sc->width / ri->ri_font->fontwidth);
    985 
    986 	ri->ri_hw = scr;
    987 	ri->ri_ops.copyrows = voodoofb_copyrows;
    988 	ri->ri_ops.copycols = voodoofb_copycols;
    989 	ri->ri_ops.eraserows = voodoofb_eraserows;
    990 	ri->ri_ops.erasecols = voodoofb_erasecols;
    991 	ri->ri_ops.cursor = voodoofb_cursor;
    992 	ri->ri_ops.putchar = voodoofb_putchar;
    993 }
    994 
    995 #if 0
    996 int
    997 voodoofb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    998 {
    999 
   1000 	return 0;
   1001 }
   1002 #endif
   1003 
   1004 static int
   1005 voodoofb_intr(void *arg)
   1006 {
   1007 	struct voodoofb_softc *sc = arg;
   1008 
   1009 	voodoo3_write32(sc, V3_STATUS, 0);	/* clear interrupts */
   1010 	return 1;
   1011 }
   1012 
   1013 /* video mode stuff */
   1014 
   1015 #define REFFREQ 14318	/* .18 */
   1016 
   1017 #define ABS(a) ((a < 0) ? -a : a)
   1018 
   1019 static int
   1020 voodoofb_calc_pll(int freq, int *f_out, int isBanshee)
   1021 {
   1022 	int m, n, k, best_m, best_n, best_k, f_cur, best_error;
   1023 	int minm, maxm;
   1024 
   1025 	best_error = freq;
   1026 	best_n = best_m = best_k = 0;
   1027 
   1028 	if (isBanshee) {
   1029 		minm = 24;
   1030 		maxm = 24;
   1031 	} else {
   1032 		minm = 1;
   1033 		maxm = 57;
   1034 		/* This used to be 64, alas it seems the last 8 (funny that ?)
   1035 		 * values cause jittering at lower resolutions. I've not done
   1036 		 * any calculations to what the adjustment affects clock ranges,
   1037 		 * but I can still run at 1600x1200@75Hz */
   1038 	}
   1039 	for (n = 1; n < 256; n++) {
   1040 		f_cur = REFFREQ * (n + 2);
   1041 		if (f_cur < freq) {
   1042 			f_cur = f_cur / 3;
   1043 			if (freq - f_cur < best_error) {
   1044 				best_error = freq - f_cur;
   1045 				best_n = n;
   1046 				best_m = 1;
   1047 				best_k = 0;
   1048 				continue;
   1049       			}
   1050 		}
   1051 		for (m = minm; m < maxm; m++) {
   1052 			for (k = 0; k < 4; k++) {
   1053 				f_cur = REFFREQ * (n + 2) / (m + 2) / (1 << k);
   1054 				if (ABS(f_cur - freq) < best_error) {
   1055 					best_error = ABS(f_cur - freq);
   1056 					best_n = n;
   1057 					best_m = m;
   1058 					best_k = k;
   1059 				}
   1060 			}
   1061 		}
   1062 	}
   1063 	n = best_n;
   1064 	m = best_m;
   1065 	k = best_k;
   1066 	*f_out = REFFREQ * (n + 2) / (m + 2) / (1 << k);
   1067 	return ( n << 8) | (m << 2) | k;
   1068 }
   1069 
   1070 static void
   1071 voodoofb_setup_monitor(struct voodoofb_softc *sc, const struct videomode *vm)
   1072 {
   1073 	struct voodoo_regs mod;
   1074 	struct voodoo_regs *mode;
   1075 	uint32_t horizontal_display_end, horizontal_sync_start,
   1076 		horizontal_sync_end, horizontal_total,
   1077 		horizontal_blanking_start, horizontal_blanking_end;
   1078 
   1079 	uint32_t vertical_display_enable_end, vertical_sync_start,
   1080 		vertical_sync_end, vertical_total, vertical_blanking_start,
   1081 		vertical_blanking_end;
   1082 
   1083 	uint32_t wd; // CRTC offset
   1084 
   1085 	int i;
   1086 
   1087 	uint8_t misc;
   1088 
   1089 	memset(&mod, 0, sizeof(mode));
   1090 
   1091 	mode = &mod;
   1092 
   1093 	wd = (vm->hdisplay >> 3) - 1;
   1094 	horizontal_display_end	= (vm->hdisplay >> 3) - 1;
   1095 	horizontal_sync_start	= (vm->hsync_start >> 3) - 1;
   1096 	horizontal_sync_end	= (vm->hsync_end >> 3) - 1;
   1097 	horizontal_total  	= (vm->htotal   >> 3) - 1;
   1098 	horizontal_blanking_start = horizontal_display_end;
   1099 	horizontal_blanking_end = horizontal_total;
   1100 
   1101 	vertical_display_enable_end  = vm->vdisplay - 1;
   1102 	vertical_sync_start  	= vm->vsync_start;	// - 1;
   1103 	vertical_sync_end	= vm->vsync_end;	// - 1;
   1104 	vertical_total		= vm->vtotal - 2;
   1105 	vertical_blanking_start	= vertical_display_enable_end;
   1106 	vertical_blanking_end	= vertical_total;
   1107 
   1108 	misc = 0x0f |
   1109 	    (vm->hdisplay < 400 ? 0xa0 :
   1110 		vm->hdisplay < 480 ? 0x60 :
   1111 		vm->hdisplay < 768 ? 0xe0 : 0x20);
   1112 
   1113 	mode->vr_seq[0] = 3;
   1114 	mode->vr_seq[1] = 1;
   1115 	mode->vr_seq[2] = 8;
   1116 	mode->vr_seq[3] = 0;
   1117 	mode->vr_seq[4] = 6;
   1118 
   1119 	/* crtc regs start */
   1120 	mode->vr_crtc[0] = horizontal_total - 4;
   1121 	mode->vr_crtc[1] = horizontal_display_end;
   1122 	mode->vr_crtc[2] = horizontal_blanking_start;
   1123 	mode->vr_crtc[3] = 0x80 | (horizontal_blanking_end & 0x1f);
   1124 	mode->vr_crtc[4] = horizontal_sync_start;
   1125 
   1126 	mode->vr_crtc[5] = ((horizontal_blanking_end & 0x20) << 2) |
   1127 	    (horizontal_sync_end & 0x1f);
   1128 	mode->vr_crtc[6] = vertical_total;
   1129 	mode->vr_crtc[7] = ((vertical_sync_start & 0x200) >> 2) |
   1130 	    ((vertical_display_enable_end & 0x200) >> 3) |
   1131 	    ((vertical_total & 0x200) >> 4) |
   1132 	    0x10 |
   1133 	    ((vertical_blanking_start & 0x100) >> 5) |
   1134 	    ((vertical_sync_start  & 0x100) >> 6) |
   1135 	    ((vertical_display_enable_end  & 0x100) >> 7) |
   1136 	    ((vertical_total  & 0x100) >> 8);
   1137 
   1138 	mode->vr_crtc[8] = 0;
   1139 	mode->vr_crtc[9] = 0x40 |
   1140 	    ((vertical_blanking_start & 0x200) >> 4);
   1141 
   1142 	mode->vr_crtc[10] = 0;
   1143 	mode->vr_crtc[11] = 0;
   1144 	mode->vr_crtc[12] = 0;
   1145 	mode->vr_crtc[13] = 0;
   1146 	mode->vr_crtc[14] = 0;
   1147 	mode->vr_crtc[15] = 0;
   1148 
   1149 	mode->vr_crtc[16] = vertical_sync_start;
   1150 	mode->vr_crtc[17] = (vertical_sync_end & 0x0f) | 0x20;
   1151 	mode->vr_crtc[18] = vertical_display_enable_end;
   1152 	mode->vr_crtc[19] = wd; // CRTC offset
   1153 	mode->vr_crtc[20] = 0;
   1154 	mode->vr_crtc[21] = vertical_blanking_start;
   1155 	mode->vr_crtc[22] = vertical_blanking_end + 1;
   1156 	mode->vr_crtc[23] = 128;
   1157 	mode->vr_crtc[24] = 255;
   1158 
   1159 	/* attr regs start */
   1160 	mode->vr_attr[0] = 0;
   1161 	mode->vr_attr[1] = 0;
   1162 	mode->vr_attr[2] = 0;
   1163 	mode->vr_attr[3] = 0;
   1164 	mode->vr_attr[4] = 0;
   1165 	mode->vr_attr[5] = 0;
   1166 	mode->vr_attr[6] = 0;
   1167 	mode->vr_attr[7] = 0;
   1168 	mode->vr_attr[8] = 0;
   1169 	mode->vr_attr[9] = 0;
   1170 	mode->vr_attr[10] = 0;
   1171 	mode->vr_attr[11] = 0;
   1172 	mode->vr_attr[12] = 0;
   1173 	mode->vr_attr[13] = 0;
   1174 	mode->vr_attr[14] = 0;
   1175 	mode->vr_attr[15] = 0;
   1176 	mode->vr_attr[16] = 1;
   1177 	mode->vr_attr[17] = 0;
   1178 	mode->vr_attr[18] = 15;
   1179 	mode->vr_attr[19] = 0;
   1180 	/* attr regs end */
   1181 
   1182 	/* graph regs start */
   1183 	mode->vr_graph[0] = 159;
   1184 	mode->vr_graph[1] = 127;
   1185 	mode->vr_graph[2] = 127;
   1186 	mode->vr_graph[3] = 131;
   1187 	mode->vr_graph[4] = 130;
   1188 	mode->vr_graph[5] = 142;
   1189 	mode->vr_graph[6] = 30;
   1190 	mode->vr_graph[7] = 245;
   1191 	mode->vr_graph[8] = 0;
   1192 
   1193 	vga_outb(sc, MISC_W, misc | 0x01);
   1194 
   1195 	for(i = 0; i < 5; i++)
   1196 		voodoo3_write_seq(sc, i, mode->vr_seq[i]);
   1197 	for (i = 0; i < 0x19; i ++)
   1198         	voodoo3_write_crtc(sc, i, mode->vr_crtc[i]);
   1199 	for (i = 0; i < 0x14; i ++)
   1200         	voodoo3_write_attr(sc, i, mode->vr_attr[i]);
   1201 	for (i = 0; i < 0x09; i ++)
   1202         	voodoo3_write_gra(sc, i, mode->vr_graph[i]);
   1203 }
   1204 
   1205 static void
   1206 voodoofb_set_videomode(struct voodoofb_softc *sc,
   1207     const struct videomode *vm)
   1208 {
   1209 	uint32_t miscinit0 = 0;
   1210 	int vidpll, fout;
   1211 	uint32_t vp, vidproc = VIDPROCDEFAULT;
   1212 	uint32_t bpp = 1;	/* for now */
   1213 	uint32_t bytes_per_row = vm->hdisplay * bpp;
   1214 
   1215 	sc->bits_per_pixel = bpp << 3;
   1216 	sc->width = vm->hdisplay;
   1217 	sc->height = vm->vdisplay;
   1218 	sc->linebytes = bytes_per_row;
   1219 
   1220 	voodoofb_setup_monitor(sc, vm);
   1221 	vp = voodoo3_read32(sc, VIDPROCCFG);
   1222 
   1223 	vidproc &= ~(0x1c0000); /* clear bits 18 to 20, bpp in vidproccfg */
   1224 	/* enable bits 18 to 20 to the required bpp */
   1225 	vidproc |= ((bpp - 1) << VIDCFG_PIXFMT_SHIFT);
   1226 
   1227 	vidpll = voodoofb_calc_pll(vm->dot_clock, &fout, 0);
   1228 
   1229 #ifdef VOODOOFB_DEBUG
   1230 	printf("old vidproc: %08x\n", vp);
   1231 	printf("pll: %08x %d\n", vidpll, fout);
   1232 #endif
   1233 	/* bit 10 of vidproccfg, is enabled or disabled as needed */
   1234 	switch (bpp) {
   1235 		case 1:
   1236 			/*
   1237 			 * bit 10 off for palettized modes only, off means
   1238 			 * palette is used
   1239 			 */
   1240 			vidproc &= ~(1 << 10);
   1241 			break;
   1242 #if 0
   1243 		case 2:
   1244 			#if __POWERPC__
   1245 				miscinit0 = 0xc0000000;
   1246 			#endif
   1247 			/* bypass palette for 16bit modes */
   1248 			vidproc |= (1 << 10);
   1249 			break;
   1250 		case 4:
   1251 			#if __POWERPC__
   1252 				miscinit0 = 0x40000000;
   1253 			#endif
   1254 			vidproc |= (1 << 10); /* Same for 32bit modes */
   1255 			break;
   1256 #endif
   1257 		default:
   1258 			printf("We support only 8 bit for now\n");
   1259 			return;
   1260 	}
   1261 
   1262 	voodoofb_wait_idle(sc);
   1263 
   1264 	voodoo3_write32(sc, MISCINIT1, voodoo3_read32(sc, MISCINIT1) | 0x01);
   1265 
   1266 	voodoo3_make_room(sc, 4);
   1267 	voodoo3_write32(sc, VGAINIT0, 4928);
   1268 	voodoo3_write32(sc, DACMODE, 0);
   1269 	voodoo3_write32(sc, VIDDESKSTRIDE, bytes_per_row);
   1270 	voodoo3_write32(sc, PLLCTRL0, vidpll);
   1271 
   1272 	voodoo3_make_room(sc, 5);
   1273 	voodoo3_write32(sc, VIDSCREENSIZE, sc->width | (sc->height << 12));
   1274 	voodoo3_write32(sc, VIDDESKSTART,  1024);
   1275 
   1276 	vidproc &= ~VIDCFG_HWCURSOR_ENABLE;
   1277 	voodoo3_write32(sc, VIDPROCCFG, vidproc);
   1278 
   1279 	voodoo3_write32(sc, VGAINIT1, 0);
   1280 	voodoo3_write32(sc, MISCINIT0, miscinit0);
   1281 #ifdef VOODOOFB_DEBUG
   1282 	printf("vidproc: %08x\n", vidproc);
   1283 #endif
   1284 	voodoo3_make_room(sc, 8);
   1285 	voodoo3_write32(sc, SRCBASE, 0);
   1286 	voodoo3_write32(sc, DSTBASE, 0);
   1287 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
   1288   	voodoo3_write32(sc, CLIP0MIN,        0);
   1289   	voodoo3_write32(sc, CLIP0MAX,        0x0fff0fff);
   1290   	voodoo3_write32(sc, CLIP1MIN,        0);
   1291   	voodoo3_write32(sc, CLIP1MAX,        0x0fff0fff);
   1292 	voodoo3_write32(sc, SRCXY, 0);
   1293 	voodoofb_wait_idle(sc);
   1294 	printf("%s: switched to %dx%d, %d bit\n", sc->sc_dev.dv_xname,
   1295 	    sc->width, sc->height, sc->bits_per_pixel);
   1296 }
   1297 
   1298 static void
   1299 voodoofb_init(struct voodoofb_softc *sc)
   1300 {
   1301 	/* XXX */
   1302 	uint32_t vgainit0 = 0;
   1303 	uint32_t vidcfg = 0;
   1304 
   1305 #ifdef VOODOOFB_DEBUG
   1306 	printf("initializing engine...");
   1307 #endif
   1308 	vgainit0 = voodoo3_read32(sc, VGAINIT0);
   1309 #ifdef VOODOOFB_DEBUG
   1310 	printf("vga: %08x", vgainit0);
   1311 #endif
   1312 	vgainit0 |=
   1313 	    VGAINIT0_8BIT_DAC     |
   1314 	    VGAINIT0_EXT_ENABLE   |
   1315 	    VGAINIT0_WAKEUP_3C3   |
   1316 	    VGAINIT0_ALT_READBACK |
   1317 	    VGAINIT0_EXTSHIFTOUT;
   1318 
   1319 	vidcfg = voodoo3_read32(sc, VIDPROCCFG);
   1320 #ifdef VOODOOFB_DEBUG
   1321 	printf(" vidcfg: %08x\n", vidcfg);
   1322 #endif
   1323 	vidcfg |=
   1324 	    VIDCFG_VIDPROC_ENABLE |
   1325 	    VIDCFG_DESK_ENABLE;
   1326 	vidcfg &= ~VIDCFG_HWCURSOR_ENABLE;
   1327 
   1328 	voodoo3_make_room(sc, 2);
   1329 
   1330 	voodoo3_write32(sc, VGAINIT0, vgainit0);
   1331 	voodoo3_write32(sc, VIDPROCCFG, vidcfg);
   1332 
   1333 	voodoo3_make_room(sc, 8);
   1334 	voodoo3_write32(sc, SRCBASE, 0);
   1335 	voodoo3_write32(sc, DSTBASE, 0);
   1336 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
   1337   	voodoo3_write32(sc, CLIP0MIN,        0);
   1338   	voodoo3_write32(sc, CLIP0MAX,        0x1fff1fff);
   1339   	voodoo3_write32(sc, CLIP1MIN,        0);
   1340   	voodoo3_write32(sc, CLIP1MAX,        0x1fff1fff);
   1341 	voodoo3_write32(sc, SRCXY, 0);
   1342 
   1343 	voodoofb_wait_idle(sc);
   1344 }
   1345