Home | History | Annotate | Line # | Download | only in pci
voodoofb.c revision 1.44
      1 /*	$NetBSD: voodoofb.c,v 1.44 2013/07/02 00:18:24 joerg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005, 2006, 2012 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * A console driver for 3Dfx Voodoo3 graphics boards
     30  * Thanks to Andreas Drewke (andreas_dr (at) gmx.de) for his Voodoo3 driver for BeOS
     31  * which I used as reference / documentation
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: voodoofb.c,v 1.44 2013/07/02 00:18:24 joerg Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 #include <sys/callout.h>
     43 #include <sys/kauth.h>
     44 
     45 #include <dev/pci/pcivar.h>
     46 #include <dev/pci/pcireg.h>
     47 #include <dev/pci/pcidevs.h>
     48 #include <dev/pci/pciio.h>
     49 #include <dev/pci/voodoofbreg.h>
     50 
     51 #include <dev/wscons/wsdisplayvar.h>
     52 #include <dev/wscons/wsconsio.h>
     53 #include <dev/wsfont/wsfont.h>
     54 #include <dev/rasops/rasops.h>
     55 #include <dev/wscons/wsdisplay_vconsvar.h>
     56 #include <dev/pci/wsdisplay_pci.h>
     57 
     58 #include <dev/i2c/i2cvar.h>
     59 #include <dev/i2c/i2c_bitbang.h>
     60 #include <dev/i2c/ddcvar.h>
     61 #include <dev/videomode/videomode.h>
     62 #include <dev/videomode/edidvar.h>
     63 #include <dev/videomode/edidreg.h>
     64 
     65 #include "opt_wsemul.h"
     66 
     67 struct voodoofb_softc {
     68 	device_t sc_dev;
     69 	pci_chipset_tag_t sc_pc;
     70 	pcitag_t sc_pcitag;
     71 	struct pci_attach_args sc_pa;
     72 
     73 	bus_space_tag_t sc_memt;
     74 	bus_space_tag_t sc_iot;
     75 	bus_space_handle_t sc_memh;
     76 
     77 	bus_space_tag_t sc_regt;
     78 	bus_space_tag_t sc_ioregt;
     79 	bus_space_handle_t sc_regh;
     80 	bus_space_handle_t sc_ioregh;
     81 	bus_addr_t sc_regs, sc_fb, sc_ioreg;
     82 	bus_size_t sc_regsize, sc_fbsize, sc_ioregsize;
     83 
     84 	void *sc_ih;
     85 
     86 #define MAX_CLOCK_VB	270000	/* Voodoo Banshee */
     87 #define MAX_CLOCK_V3	300000	/* Voodoo3 */
     88 #define MAX_CLOCK_V45	350000	/* Voodoo4/5 (not yet) */
     89 	uint32_t sc_max_clock;
     90 
     91 	size_t sc_memsize;
     92 	int sc_memtype;
     93 
     94 	int sc_bits_per_pixel;
     95 	int sc_width, sc_height, sc_linebytes;
     96 	const struct videomode *sc_videomode;
     97 
     98 	/* glyph cache */
     99 	long sc_defattr, sc_kernattr;
    100 	uint32_t sc_glyphs_defattr[256];
    101 	uint32_t sc_glyphs_kernattr[256];
    102 	int sc_numglyphs, sc_usedglyphs;
    103 	uint32_t sc_cache;	/* offset where cache starts */
    104 	uint32_t sc_fmt;	/* *fmt register */
    105 	void *sc_font;
    106 
    107 	/* i2c stuff */
    108 	struct i2c_controller sc_i2c;
    109 	uint8_t sc_edid_data[128];
    110 	struct edid_info sc_edid_info;
    111 	uint32_t sc_i2creg;
    112 
    113 	int sc_mode;
    114 	uint32_t sc_bg;
    115 
    116 	u_char sc_cmap_red[256];
    117 	u_char sc_cmap_green[256];
    118 	u_char sc_cmap_blue[256];
    119 	int sc_dacw;
    120 
    121 	struct vcons_data vd;
    122 };
    123 
    124 struct voodoo_regs {
    125 	uint8_t vr_crtc[31];
    126 	uint8_t vr_graph[9];
    127 	uint8_t vr_attr[21];
    128 	uint8_t vr_seq[5];
    129 };
    130 
    131 static struct vcons_screen voodoofb_console_screen;
    132 
    133 static int	voodoofb_match(device_t, cfdata_t, void *);
    134 static void	voodoofb_attach(device_t, device_t, void *);
    135 
    136 static int	voodoofb_drm_print(void *, const char *);
    137 static int	voodoofb_drm_unmap(struct voodoofb_softc *);
    138 static int	voodoofb_drm_map(struct voodoofb_softc *);
    139 
    140 CFATTACH_DECL_NEW(voodoofb, sizeof(struct voodoofb_softc), voodoofb_match,
    141     voodoofb_attach, NULL, NULL);
    142 
    143 static bool	voodoofb_is_console(struct voodoofb_softc *);
    144 static void 	voodoofb_init(struct voodoofb_softc *);
    145 
    146 static void	voodoofb_cursor(void *, int, int, int);
    147 static void	voodoofb_putchar(void *, int, int, u_int, long);
    148 static void	voodoofb_putchar_aa(void *, int, int, u_int, long);
    149 static void	voodoofb_copycols(void *, int, int, int, int);
    150 static void	voodoofb_erasecols(void *, int, int, int, long);
    151 static void	voodoofb_copyrows(void *, int, int, int);
    152 static void	voodoofb_eraserows(void *, int, int, long);
    153 
    154 #if 0
    155 static int	voodoofb_allocattr(void *, int, int, int, long *);
    156 static void	voodoofb_scroll(void *, void *, int);
    157 static int	voodoofb_load_font(void *, void *, struct wsdisplay_font *);
    158 #endif
    159 
    160 static int	voodoofb_putcmap(struct voodoofb_softc *,
    161 			    struct wsdisplay_cmap *);
    162 static int 	voodoofb_getcmap(struct voodoofb_softc *,
    163 			    struct wsdisplay_cmap *);
    164 static int 	voodoofb_putpalreg(struct voodoofb_softc *, uint8_t, uint8_t,
    165 			    uint8_t, uint8_t);
    166 static void	voodoofb_bitblt(struct voodoofb_softc *, int, int, int, int,
    167 			    int, int);
    168 static void	voodoofb_rectfill(struct voodoofb_softc *, int, int, int, int,
    169 			    int);
    170 static void	voodoofb_rectinvert(struct voodoofb_softc *, int, int, int,
    171 			    int);
    172 static void	voodoofb_setup_mono(struct voodoofb_softc *, int, int, int,
    173 			    int, uint32_t, uint32_t);
    174 static void	voodoofb_feed_line(struct voodoofb_softc *, int, uint8_t *);
    175 
    176 static void	voodoofb_wait_idle(struct voodoofb_softc *);
    177 static void	voodoofb_init_glyphcache(struct voodoofb_softc *,
    178 			    struct rasops_info *);
    179 
    180 #ifdef VOODOOFB_ENABLE_INTR
    181 static int	voodoofb_intr(void *);
    182 #endif
    183 
    184 static void	voodoofb_set_videomode(struct voodoofb_softc *,
    185 			    const struct videomode *);
    186 
    187 struct wsscreen_descr voodoofb_defaultscreen = {
    188 	"default",
    189 	0, 0,
    190 	NULL,
    191 	8, 16,
    192 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    193 	NULL,
    194 };
    195 
    196 const struct wsscreen_descr *_voodoofb_scrlist[] = {
    197 	&voodoofb_defaultscreen,
    198 	/* XXX other formats, graphics screen? */
    199 };
    200 
    201 struct wsscreen_list voodoofb_screenlist = {
    202 	sizeof(_voodoofb_scrlist) / sizeof(struct wsscreen_descr *), _voodoofb_scrlist
    203 };
    204 
    205 static int	voodoofb_ioctl(void *, void *, u_long, void *, int,
    206 		    struct lwp *);
    207 static paddr_t	voodoofb_mmap(void *, void *, off_t, int);
    208 
    209 static void	voodoofb_clearscreen(struct voodoofb_softc *);
    210 static void	voodoofb_init_screen(void *, struct vcons_screen *, int,
    211 			    long *);
    212 
    213 
    214 struct wsdisplay_accessops voodoofb_accessops = {
    215 	voodoofb_ioctl,
    216 	voodoofb_mmap,
    217 	NULL,
    218 	NULL,
    219 	NULL,
    220 	NULL,	/* load_font */
    221 	NULL,	/* polls */
    222 	NULL,	/* scroll */
    223 };
    224 
    225 /* I2C glue */
    226 static int voodoofb_i2c_acquire_bus(void *, int);
    227 static void voodoofb_i2c_release_bus(void *, int);
    228 static int voodoofb_i2c_send_start(void *, int);
    229 static int voodoofb_i2c_send_stop(void *, int);
    230 static int voodoofb_i2c_initiate_xfer(void *, i2c_addr_t, int);
    231 static int voodoofb_i2c_read_byte(void *, uint8_t *, int);
    232 static int voodoofb_i2c_write_byte(void *, uint8_t, int);
    233 
    234 /* I2C bitbang glue */
    235 static void voodoofb_i2cbb_set_bits(void *, uint32_t);
    236 static void voodoofb_i2cbb_set_dir(void *, uint32_t);
    237 static uint32_t voodoofb_i2cbb_read(void *);
    238 
    239 static void voodoofb_setup_i2c(struct voodoofb_softc *);
    240 
    241 static const struct i2c_bitbang_ops voodoofb_i2cbb_ops = {
    242 	voodoofb_i2cbb_set_bits,
    243 	voodoofb_i2cbb_set_dir,
    244 	voodoofb_i2cbb_read,
    245 	{
    246 		VSP_SDA0_IN,
    247 		VSP_SCL0_IN,
    248 		0,
    249 		0
    250 	}
    251 };
    252 
    253 extern const u_char rasops_cmap[768];
    254 
    255 /*
    256  * Inline functions for getting access to register aperture.
    257  */
    258 static inline void
    259 voodoo3_write32(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
    260 {
    261 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, val);
    262 }
    263 
    264 static inline void
    265 voodoo3_write32s(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
    266 {
    267 	bus_space_write_stream_4(sc->sc_regt, sc->sc_regh, reg, val);
    268 }
    269 static inline uint32_t
    270 voodoo3_read32(struct voodoofb_softc *sc, uint32_t reg)
    271 {
    272 	return bus_space_read_4(sc->sc_regt, sc->sc_regh, reg);
    273 }
    274 
    275 static inline void
    276 voodoo3_write_crtc(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    277 {
    278 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_INDEX - 0x300, reg);
    279 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_DATA - 0x300, val);
    280 }
    281 
    282 static inline void
    283 voodoo3_write_seq(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    284 {
    285 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_INDEX - 0x300, reg);
    286 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_DATA - 0x300, val);
    287 }
    288 
    289 static inline void
    290 voodoo3_write_gra(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    291 {
    292 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_INDEX - 0x300, reg);
    293 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_DATA - 0x300, val);
    294 }
    295 
    296 static inline void
    297 voodoo3_write_attr(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
    298 {
    299 	volatile uint8_t junk;
    300 	uint8_t index;
    301 
    302 	junk = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, IS1_R - 0x300);
    303 	index = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300);
    304 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, reg);
    305 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, val);
    306 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, index);
    307 }
    308 
    309 static inline void
    310 vga_outb(struct voodoofb_softc *sc, uint32_t reg,  uint8_t val)
    311 {
    312 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, reg - 0x300, val);
    313 }
    314 
    315 /* wait until there's room for len bytes in the FIFO */
    316 static inline void
    317 voodoo3_make_room(struct voodoofb_softc *sc, int len)
    318 {
    319 	while ((voodoo3_read32(sc, STATUS) & 0x1f) < len);
    320 }
    321 
    322 static void
    323 voodoofb_wait_idle(struct voodoofb_softc *sc)
    324 {
    325 	int i = 0;
    326 
    327 	voodoo3_make_room(sc, 1);
    328 	voodoo3_write32(sc, COMMAND_3D, COMMAND_3D_NOP);
    329 
    330 	while (1) {
    331 		i = (voodoo3_read32(sc, STATUS) & STATUS_BUSY) ? 0 : i + 1;
    332 		if(i == 3) break;
    333 	}
    334 }
    335 
    336 static int
    337 voodoofb_match(device_t parent, cfdata_t match, void *aux)
    338 {
    339 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    340 
    341 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    342 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    343 		return 0;
    344 	if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
    345 	    (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_VOODOO3))
    346 		return 100;
    347 
    348 	if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
    349 	    (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_BANSHEE))
    350 		return 100;
    351 	return 0;
    352 }
    353 
    354 static void
    355 voodoofb_attach(device_t parent, device_t self, void *aux)
    356 {
    357 	struct voodoofb_softc *sc = device_private(self);
    358 	struct pci_attach_args *pa = aux;
    359 	struct wsemuldisplaydev_attach_args aa;
    360 	struct rasops_info *ri;
    361 #ifdef VOODOOFB_ENABLE_INTR
    362 	pci_intr_handle_t ih;
    363 	const char *intrstr;
    364 #endif
    365 	ulong defattr;
    366 	int console, width, height, i, j;
    367 	prop_dictionary_t dict;
    368 	int linebytes, depth, flags;
    369 	uint32_t bg, fg, ul;
    370 
    371 	sc->sc_dev = self;
    372 
    373 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    374 	sc->sc_pc = pa->pa_pc;
    375 	sc->sc_pcitag = pa->pa_tag;
    376 	sc->sc_dacw = -1;
    377 	pci_aprint_devinfo(pa, NULL);
    378 
    379 	sc->sc_memt = pa->pa_memt;
    380 	sc->sc_iot = pa->pa_iot;
    381 	sc->sc_pa = *pa;
    382 
    383 	if (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_BANSHEE)
    384 		sc->sc_max_clock = MAX_CLOCK_VB;
    385 	else
    386 		sc->sc_max_clock = MAX_CLOCK_V3;
    387 
    388 	/* the framebuffer */
    389 	if (pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, 0x14, PCI_MAPREG_TYPE_MEM,
    390 	    &sc->sc_fb, &sc->sc_fbsize, &flags)) {
    391 		aprint_error_dev(self, "failed to map the frame buffer.\n");
    392 	}
    393 	sc->sc_memsize = sc->sc_fbsize >> 1;	/* VRAM aperture is 2x VRAM */
    394 
    395 	/* memory-mapped registers */
    396 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    397 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
    398 		aprint_error_dev(self, "failed to map memory-mapped registers.\n");
    399 	}
    400 
    401 	/* IO-mapped registers */
    402 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
    403 	    &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
    404 	    &sc->sc_ioregsize)) {
    405 		aprint_error_dev(self, "failed to map IO-mapped registers.\n");
    406 	}
    407 	voodoofb_init(sc);
    408 
    409 	/* we should read these from the chip instead of depending on OF */
    410 	width = height = -1;
    411 
    412 	dict = device_properties(self);
    413 	if (!prop_dictionary_get_uint32(dict, "width", &width)) {
    414 		aprint_error_dev(self, "no width property\n");
    415 		return;
    416 	}
    417 	if (!prop_dictionary_get_uint32(dict, "height", &height)) {
    418 		aprint_error_dev(self, "no height property\n");
    419 		return;
    420 	}
    421 	if (!prop_dictionary_get_uint32(dict, "depth", &depth)) {
    422 		aprint_error_dev(self, "no depth property\n");
    423 		return;
    424 	}
    425 	linebytes = width;			/* XXX */
    426 
    427 	if (width == -1 || height == -1)
    428 		return;
    429 
    430 	sc->sc_width = width;
    431 	sc->sc_height = height;
    432 	sc->sc_bits_per_pixel = depth;
    433 	sc->sc_linebytes = linebytes;
    434 	printf("%s: initial resolution %dx%d, %d bit\n", device_xname(self),
    435 	    sc->sc_width, sc->sc_height, sc->sc_bits_per_pixel);
    436 
    437 	sc->sc_videomode = NULL;
    438 	voodoofb_setup_i2c(sc);
    439 
    440 	/* XXX this should at least be configurable via kernel config */
    441 	if (sc->sc_videomode == NULL) {
    442 		sc->sc_videomode = pick_mode_by_ref(width, height, 60);
    443 	}
    444 
    445 	voodoofb_set_videomode(sc, sc->sc_videomode);
    446 
    447 	sc->sc_font = NULL;
    448 
    449 	vcons_init(&sc->vd, sc, &voodoofb_defaultscreen, &voodoofb_accessops);
    450 	sc->vd.init_screen = voodoofb_init_screen;
    451 
    452 	console = voodoofb_is_console(sc);
    453 
    454 	ri = &voodoofb_console_screen.scr_ri;
    455 	if (console) {
    456 		vcons_init_screen(&sc->vd, &voodoofb_console_screen, 1,
    457 		    &defattr);
    458 		voodoofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    459 
    460 		voodoofb_defaultscreen.textops = &ri->ri_ops;
    461 		voodoofb_defaultscreen.capabilities = ri->ri_caps;
    462 		voodoofb_defaultscreen.nrows = ri->ri_rows;
    463 		voodoofb_defaultscreen.ncols = ri->ri_cols;
    464 		wsdisplay_cnattach(&voodoofb_defaultscreen, ri, 0, 0, defattr);
    465 	} else {
    466 		/*
    467 		 * since we're not the console we can postpone the rest
    468 		 * until someone actually allocates a screen for us
    469 		 */
    470 		voodoofb_set_videomode(sc, sc->sc_videomode);
    471 	}
    472 
    473 	printf("%s: %d MB aperture at 0x%08x, %d MB registers at 0x%08x\n",
    474 	    device_xname(self), (u_int)(sc->sc_fbsize >> 20),
    475 	    (u_int)sc->sc_fb, (u_int)(sc->sc_regsize >> 20),
    476 	    (u_int)sc->sc_regs);
    477 #ifdef VOODOOFB_DEBUG
    478 	printf("fb: %08lx\n", (ulong)ri->ri_bits);
    479 #endif
    480 
    481 	j = 0;
    482 	if (sc->sc_bits_per_pixel == 8) {
    483 		uint8_t tmp;
    484 		for (i = 0; i < 256; i++) {
    485 			tmp = i & 0xe0;
    486 			/*
    487 			 * replicate bits so 0xe0 maps to a red value of 0xff
    488 			 * in order to make white look actually white
    489 			 */
    490 			tmp |= (tmp >> 3) | (tmp >> 6);
    491 			sc->sc_cmap_red[i] = tmp;
    492 
    493 			tmp = (i & 0x1c) << 3;
    494 			tmp |= (tmp >> 3) | (tmp >> 6);
    495 			sc->sc_cmap_green[i] = tmp;
    496 
    497 			tmp = (i & 0x03) << 6;
    498 			tmp |= tmp >> 2;
    499 			tmp |= tmp >> 4;
    500 			sc->sc_cmap_blue[i] = tmp;
    501 
    502 			voodoofb_putpalreg(sc, i, sc->sc_cmap_red[i],
    503 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    504 		}
    505 	} else {
    506 		/* linear ramp */
    507 		for (i = 0; i < 256; i++) {
    508 			sc->sc_cmap_red[i] = i;
    509 			sc->sc_cmap_green[i] = i;
    510 			sc->sc_cmap_blue[i] = i;
    511 
    512 			voodoofb_putpalreg(sc, i, sc->sc_cmap_red[i],
    513 			    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    514 		}
    515 	}
    516 
    517 #ifdef VOODOOFB_ENABLE_INTR
    518 	/* Interrupt. We don't use it for anything yet */
    519 	if (pci_intr_map(pa, &ih)) {
    520 		aprint_error_dev(self, "failed to map interrupt\n");
    521 		return;
    522 	}
    523 
    524 	intrstr = pci_intr_string(sc->sc_pc, ih);
    525 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET, voodoofb_intr,
    526 	    sc);
    527 	if (sc->sc_ih == NULL) {
    528 		aprint_error_dev(self, "failed to establish interrupt");
    529 		if (intrstr != NULL)
    530 			aprint_error(" at %s", intrstr);
    531 		aprint_error("\n");
    532 		return;
    533 	}
    534 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    535 #endif
    536 
    537 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    538 	sc->sc_bg = ri->ri_devcmap[bg];
    539 	voodoofb_clearscreen(sc);
    540 
    541 	if (console)
    542 		vcons_replay_msgbuf(&voodoofb_console_screen);
    543 	aa.console = console;
    544 	aa.scrdata = &voodoofb_screenlist;
    545 	aa.accessops = &voodoofb_accessops;
    546 	aa.accesscookie = &sc->vd;
    547 
    548 	config_found(self, &aa, wsemuldisplaydevprint);
    549 	config_found_ia(self, "drm", aux, voodoofb_drm_print);
    550 }
    551 
    552 static int
    553 voodoofb_drm_print(void *opaque, const char *pnp)
    554 {
    555 	if (pnp)
    556 		aprint_normal("drm at %s", pnp);
    557 
    558 	return UNCONF;
    559 }
    560 
    561 static int
    562 voodoofb_drm_unmap(struct voodoofb_softc *sc)
    563 {
    564 	printf("%s: releasing bus resources\n", device_xname(sc->sc_dev));
    565 
    566 	bus_space_unmap(sc->sc_ioregt, sc->sc_ioregh, sc->sc_ioregsize);
    567 	bus_space_unmap(sc->sc_regt, sc->sc_regh, sc->sc_regsize);
    568 
    569 	return 0;
    570 }
    571 
    572 static int
    573 voodoofb_drm_map(struct voodoofb_softc *sc)
    574 {
    575 
    576 	/* memory-mapped registers */
    577 	if (pci_mapreg_map(&sc->sc_pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    578 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
    579 		aprint_error_dev(sc->sc_dev, "failed to map memory-mapped registers.\n");
    580 	}
    581 
    582 	/* IO-mapped registers */
    583 	if (pci_mapreg_map(&sc->sc_pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
    584 	    &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
    585 	    &sc->sc_ioregsize)) {
    586 		aprint_error_dev(sc->sc_dev, "failed to map IO-mapped registers.\n");
    587 	}
    588 
    589 	voodoofb_init(sc);
    590 	/* XXX this should at least be configurable via kernel config */
    591 	voodoofb_set_videomode(sc, sc->sc_videomode);
    592 
    593 	return 0;
    594 }
    595 
    596 static int
    597 voodoofb_putpalreg(struct voodoofb_softc *sc, uint8_t index, uint8_t r,
    598     uint8_t g, uint8_t b)
    599 {
    600 	uint32_t color;
    601 
    602 	sc->sc_cmap_red[index] = r;
    603 	sc->sc_cmap_green[index] = g;
    604 	sc->sc_cmap_blue[index] = b;
    605 
    606 	color = (r << 16) | (g << 8) | b;
    607 	voodoo3_make_room(sc, 2);
    608 	voodoo3_write32(sc, DACADDR, index);
    609 	voodoo3_write32(sc, DACDATA, color);
    610 
    611 	return 0;
    612 }
    613 
    614 static int
    615 voodoofb_putcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
    616 {
    617 	u_char *r, *g, *b;
    618 	u_int index = cm->index;
    619 	u_int count = cm->count;
    620 	int i, error;
    621 	u_char rbuf[256], gbuf[256], bbuf[256];
    622 
    623 #ifdef VOODOOFB_DEBUG
    624 	printf("putcmap: %d %d\n",index, count);
    625 #endif
    626 	if (cm->index >= 256 || cm->count > 256 ||
    627 	    (cm->index + cm->count) > 256)
    628 		return EINVAL;
    629 	error = copyin(cm->red, &rbuf[index], count);
    630 	if (error)
    631 		return error;
    632 	error = copyin(cm->green, &gbuf[index], count);
    633 	if (error)
    634 		return error;
    635 	error = copyin(cm->blue, &bbuf[index], count);
    636 	if (error)
    637 		return error;
    638 
    639 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    640 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    641 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    642 
    643 	r = &sc->sc_cmap_red[index];
    644 	g = &sc->sc_cmap_green[index];
    645 	b = &sc->sc_cmap_blue[index];
    646 
    647 	for (i = 0; i < count; i++) {
    648 		voodoofb_putpalreg(sc, index, *r, *g, *b);
    649 		index++;
    650 		r++, g++, b++;
    651 	}
    652 	return 0;
    653 }
    654 
    655 static int
    656 voodoofb_getcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
    657 {
    658 	u_int index = cm->index;
    659 	u_int count = cm->count;
    660 	int error;
    661 
    662 	if (index >= 255 || count > 256 || index + count > 256)
    663 		return EINVAL;
    664 
    665 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    666 	if (error)
    667 		return error;
    668 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    669 	if (error)
    670 		return error;
    671 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    672 	if (error)
    673 		return error;
    674 
    675 	return 0;
    676 }
    677 
    678 static bool
    679 voodoofb_is_console(struct voodoofb_softc *sc)
    680 {
    681 	prop_dictionary_t dict;
    682 	bool console;
    683 
    684 	dict = device_properties(sc->sc_dev);
    685 	prop_dictionary_get_bool(dict, "is_console", &console);
    686 	return console;
    687 }
    688 
    689 static void
    690 voodoofb_clearscreen(struct voodoofb_softc *sc)
    691 {
    692 	voodoofb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height, sc->sc_bg);
    693 }
    694 
    695 /*
    696  * wsdisplay_emulops
    697  */
    698 
    699 static void
    700 voodoofb_cursor(void *cookie, int on, int row, int col)
    701 {
    702 	struct rasops_info *ri = cookie;
    703 	struct vcons_screen *scr = ri->ri_hw;
    704 	struct voodoofb_softc *sc = scr->scr_cookie;
    705 	int x, y, wi, he;
    706 
    707 	wi = ri->ri_font->fontwidth;
    708 	he = ri->ri_font->fontheight;
    709 
    710 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    711 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    712 		y = ri->ri_crow * he + ri->ri_yorigin;
    713 		if (ri->ri_flg & RI_CURSOR) {
    714 			voodoofb_rectinvert(sc, x, y, wi, he);
    715 			ri->ri_flg &= ~RI_CURSOR;
    716 		}
    717 		ri->ri_crow = row;
    718 		ri->ri_ccol = col;
    719 		if (on)
    720 		{
    721 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    722 			y = ri->ri_crow * he + ri->ri_yorigin;
    723 			voodoofb_rectinvert(sc, x, y, wi, he);
    724 			ri->ri_flg |= RI_CURSOR;
    725 		}
    726 	} else {
    727 		ri->ri_flg &= ~RI_CURSOR;
    728 		ri->ri_crow = row;
    729 		ri->ri_ccol = col;
    730 	}
    731 }
    732 
    733 #if 0
    734 int
    735 voodoofb_mapchar(void *cookie, int uni, u_int *index)
    736 {
    737 	return 0;
    738 }
    739 #endif
    740 
    741 static void
    742 voodoofb_putchar(void *cookie, int row, int col, u_int c, long attr)
    743 {
    744 	struct rasops_info *ri = cookie;
    745 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    746 	struct vcons_screen *scr = ri->ri_hw;
    747 	struct voodoofb_softc *sc = scr->scr_cookie;
    748 
    749 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    750 		uint8_t *data;
    751 		int fg, bg, uc, i;
    752 		int x, y, wi, he;
    753 
    754 		wi = font->fontwidth;
    755 		he = font->fontheight;
    756 
    757 		if (!CHAR_IN_FONT(c, font))
    758 			return;
    759 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    760 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    761 		x = ri->ri_xorigin + col * wi;
    762 		y = ri->ri_yorigin + row * he;
    763 		if (c == 0x20) {
    764 			voodoofb_rectfill(sc, x, y, wi, he, bg);
    765 		} else {
    766 			uc = c - font->firstchar;
    767 			data = (uint8_t *)font->data + uc *
    768 			    ri->ri_fontscale;
    769 				voodoofb_setup_mono(sc, x, y, wi, he, fg, bg);
    770 			for (i = 0; i < he; i++) {
    771 				voodoofb_feed_line(sc, font->stride, data);
    772 				data += font->stride;
    773 			}
    774 		}
    775 	}
    776 }
    777 
    778 static void
    779 voodoofb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
    780 {
    781 	struct rasops_info *ri = cookie;
    782 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    783 	struct vcons_screen *scr = ri->ri_hw;
    784 	struct voodoofb_softc *sc = scr->scr_cookie;
    785 	uint8_t *data;
    786 	uint32_t bg, latch = 0, bg8, fg8, pixel, save_offset = 0;
    787 	int i, x, y, wi, he, r, g, b, aval;
    788 	int r1, g1, b1, r0, g0, b0, fgo, bgo, j;
    789 
    790 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
    791 		return;
    792 
    793 	if (!CHAR_IN_FONT(c, font))
    794 		return;
    795 
    796 	wi = font->fontwidth;
    797 	he = font->fontheight;
    798 
    799 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    800 	x = ri->ri_xorigin + col * wi;
    801 	y = ri->ri_yorigin + row * he;
    802 	if (c == 0x20) {
    803 		voodoofb_rectfill(sc, x, y, wi, he, bg);
    804 		return;
    805 	}
    806 
    807 	/* first, see if it's in the cache */
    808 	if ((attr == sc->sc_defattr) && (c < 256)) {
    809 		uint32_t offset = sc->sc_glyphs_defattr[c];
    810 		if (offset != 0) {
    811 			voodoo3_make_room(sc, 8);
    812 			voodoo3_write32(sc, SRCBASE, offset);
    813 			voodoo3_write32(sc, DSTBASE, 0);
    814 			voodoo3_write32(sc, SRCFORMAT,	sc->sc_fmt);
    815 			voodoo3_write32(sc, DSTFORMAT,	sc->sc_linebytes | FMT_8BIT);
    816 			voodoo3_write32(sc, DSTSIZE,	wi | (he << 16));
    817 			voodoo3_write32(sc, DSTXY,	x | (y << 16));
    818 			voodoo3_write32(sc, SRCXY,	0);
    819 			voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_S2S_BITBLT |
    820 			    (ROP_COPY << 24) | SST_2D_GO);
    821 			return;
    822 		} else {
    823 			int slot = sc->sc_usedglyphs;
    824 			sc->sc_usedglyphs++;
    825 			save_offset = sc->sc_cache + (slot * ri->ri_fontscale);
    826 			sc->sc_glyphs_defattr[c] = save_offset;
    827 		}
    828 	}
    829 	data = WSFONT_GLYPH(c, font);
    830 
    831 	voodoo3_make_room(sc, 7);
    832 	voodoo3_write32(sc, DSTBASE, 0);
    833 	voodoo3_write32(sc, SRCFORMAT,	FMT_8BIT | FMT_PAD_BYTE);
    834 	voodoo3_write32(sc, DSTFORMAT,	sc->sc_linebytes | FMT_8BIT);
    835 	voodoo3_write32(sc, DSTSIZE,	wi | (he << 16));
    836 	voodoo3_write32(sc, DSTXY,	x | (y << 16));
    837 	voodoo3_write32(sc, SRCXY,	0);
    838 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
    839 	    (ROP_COPY << 24) | SST_2D_GO);
    840 
    841 	/*
    842 	 * we need the RGB colours here, so get offsets into rasops_cmap
    843 	 */
    844 	fgo = ((attr >> 24) & 0xf) * 3;
    845 	bgo = ((attr >> 16) & 0xf) * 3;
    846 
    847 	r0 = rasops_cmap[bgo];
    848 	r1 = rasops_cmap[fgo];
    849 	g0 = rasops_cmap[bgo + 1];
    850 	g1 = rasops_cmap[fgo + 1];
    851 	b0 = rasops_cmap[bgo + 2];
    852 	b1 = rasops_cmap[fgo + 2];
    853 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
    854 	bg8 = R3G3B2(r0, g0, b0);
    855 	fg8 = R3G3B2(r1, g1, b1);
    856 	voodoo3_make_room(sc, 15);
    857 	j = 15;
    858 	for (i = 0; i < ri->ri_fontscale; i++) {
    859 		aval = *data;
    860 		if (aval == 0) {
    861 			pixel = bg8;
    862 		} else if (aval == 255) {
    863 			pixel = fg8;
    864 		} else {
    865 			r = aval * r1 + (255 - aval) * r0;
    866 			g = aval * g1 + (255 - aval) * g0;
    867 			b = aval * b1 + (255 - aval) * b0;
    868 			pixel = ((r & 0xe000) >> 8) |
    869 				((g & 0xe000) >> 11) |
    870 				((b & 0xc000) >> 14);
    871 		}
    872 		latch = (latch << 8) | pixel;
    873 		/* write in 32bit chunks */
    874 		if ((i & 3) == 3) {
    875 			voodoo3_write32s(sc, LAUNCH_2D, latch);
    876 			/*
    877 			 * not strictly necessary, old data should be shifted
    878 			 * out
    879 			 */
    880 			latch = 0;
    881 			/*
    882 			 * check for pipeline space every now and then
    883 			 * I don't think we can feed a Voodoo3 faster than it
    884 			 * can process simple pixel data but I have been wrong
    885 			 * about that before
    886 			 */
    887 			j--;
    888 			if (j == 0) {
    889 				voodoo3_make_room(sc, 15);
    890 				j = 15;
    891 			}
    892 		}
    893 		data++;
    894 	}
    895 	/* if we have pixels left in latch write them out */
    896 	if ((i & 3) != 0) {
    897 		latch = latch << ((4 - (i & 3)) << 3);
    898 		voodoo3_write32s(sc, LAUNCH_2D, latch);
    899 	}
    900 	if (save_offset != 0) {
    901 		/* save the glyph we just drew */
    902 		voodoo3_make_room(sc, 8);
    903 		voodoo3_write32(sc, SRCBASE, 0);
    904 		voodoo3_write32(sc, DSTBASE, save_offset);
    905 		voodoo3_write32(sc, SRCFORMAT,	sc->sc_linebytes | FMT_8BIT);
    906 		voodoo3_write32(sc, DSTFORMAT,	sc->sc_fmt);
    907 		voodoo3_write32(sc, DSTSIZE,	wi | (he << 16));
    908 		voodoo3_write32(sc, DSTXY,	0);
    909 		voodoo3_write32(sc, SRCXY,	x | (y << 16));
    910 		voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_S2S_BITBLT |
    911 			    (ROP_COPY << 24) | SST_2D_GO);
    912 	}
    913 }
    914 
    915 static void
    916 voodoofb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    917 {
    918 	struct rasops_info *ri = cookie;
    919 	struct vcons_screen *scr = ri->ri_hw;
    920 	struct voodoofb_softc *sc = scr->scr_cookie;
    921 	int32_t xs, xd, y, width, height;
    922 
    923 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    924 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    925 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    926 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    927 		width = ri->ri_font->fontwidth * ncols;
    928 		height = ri->ri_font->fontheight;
    929 		voodoofb_bitblt(sc, xs, y, xd, y, width, height);
    930 	}
    931 }
    932 
    933 static void
    934 voodoofb_erasecols(void *cookie, int row, int startcol, int ncols,
    935     long fillattr)
    936 {
    937 	struct rasops_info *ri = cookie;
    938 	struct vcons_screen *scr = ri->ri_hw;
    939 	struct voodoofb_softc *sc = scr->scr_cookie;
    940 	int32_t x, y, width, height, fg, bg, ul;
    941 
    942 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    943 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    944 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    945 		width = ri->ri_font->fontwidth * ncols;
    946 		height = ri->ri_font->fontheight;
    947 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    948 
    949 		voodoofb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    950 	}
    951 }
    952 
    953 static void
    954 voodoofb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    955 {
    956 	struct rasops_info *ri = cookie;
    957 	struct vcons_screen *scr = ri->ri_hw;
    958 	struct voodoofb_softc *sc = scr->scr_cookie;
    959 	int32_t x, ys, yd, width, height;
    960 
    961 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    962 		x = ri->ri_xorigin;
    963 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    964 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    965 		width = ri->ri_emuwidth;
    966 		height = ri->ri_font->fontheight * nrows;
    967 		voodoofb_bitblt(sc, x, ys, x, yd, width, height);
    968 	}
    969 }
    970 
    971 static void
    972 voodoofb_eraserows(void *cookie, int row, int nrows, long fillattr)
    973 {
    974 	struct rasops_info *ri = cookie;
    975 	struct vcons_screen *scr = ri->ri_hw;
    976 	struct voodoofb_softc *sc = scr->scr_cookie;
    977 	int32_t x, y, width, height, fg, bg, ul;
    978 
    979 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    980 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    981 		if ((row == 0) && (nrows == ri->ri_rows)) {
    982 			/* clear the whole screen */
    983 			voodoofb_rectfill(sc, 0, 0, ri->ri_width,
    984 			    ri->ri_height, ri->ri_devcmap[bg]);
    985 		} else {
    986 			x = ri->ri_xorigin;
    987 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    988 			width = ri->ri_emuwidth;
    989 			height = ri->ri_font->fontheight * nrows;
    990 			voodoofb_rectfill(sc, x, y, width, height,
    991 			    ri->ri_devcmap[bg]);
    992 		}
    993 	}
    994 }
    995 
    996 static void
    997 voodoofb_bitblt(struct voodoofb_softc *sc, int xs, int ys, int xd, int yd, int width, int height)
    998 {
    999 	uint32_t fmt, blitcmd;
   1000 
   1001 	fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
   1002 	    ((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
   1003 	blitcmd = COMMAND_2D_S2S_BITBLT | (ROP_COPY << 24);
   1004 
   1005 	if (xs <= xd) {
   1006 	        blitcmd |= BIT(14);
   1007 		xs += (width - 1);
   1008 		xd += (width - 1);
   1009 	}
   1010 	if (ys <= yd) {
   1011 		blitcmd |= BIT(15);
   1012 		ys += (height - 1);
   1013 		yd += (height - 1);
   1014 	}
   1015 	voodoo3_make_room(sc, 8);
   1016 
   1017 	voodoo3_write32(sc, SRCBASE, 0);
   1018 	voodoo3_write32(sc, DSTBASE, 0);
   1019 	voodoo3_write32(sc, SRCFORMAT, fmt);
   1020 	voodoo3_write32(sc, DSTFORMAT, fmt);
   1021 	voodoo3_write32(sc, DSTSIZE,   width | (height << 16));
   1022 	voodoo3_write32(sc, DSTXY,     xd | (yd << 16));
   1023 	voodoo3_write32(sc, SRCXY, xs | (ys << 16));
   1024 	voodoo3_write32(sc, COMMAND_2D, blitcmd | SST_2D_GO);
   1025 }
   1026 
   1027 static void
   1028 voodoofb_rectfill(struct voodoofb_softc *sc, int x, int y, int width,
   1029     int height, int colour)
   1030 {
   1031 	uint32_t fmt, col;
   1032 
   1033 	col = (colour << 24) | (colour << 16) | (colour << 8) | colour;
   1034 	fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
   1035 	    ((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
   1036 
   1037 	voodoo3_make_room(sc, 7);
   1038 	voodoo3_write32(sc, DSTFORMAT, fmt);
   1039 	voodoo3_write32(sc, DSTBASE, 0);
   1040 	voodoo3_write32(sc, COLORFORE, colour);
   1041 	voodoo3_write32(sc, COLORBACK, colour);
   1042 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT | (ROP_COPY << 24));
   1043 	voodoo3_write32(sc, DSTSIZE,    width | (height << 16));
   1044 	voodoo3_write32(sc, LAUNCH_2D,  x | (y << 16));
   1045 }
   1046 
   1047 static void
   1048 voodoofb_rectinvert(struct voodoofb_softc *sc, int x, int y, int width,
   1049     int height)
   1050 {
   1051 	uint32_t fmt;
   1052 
   1053 	fmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
   1054 	    ((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
   1055 
   1056 	voodoo3_make_room(sc, 8);
   1057 	voodoo3_write32(sc, SRCBASE, 0);
   1058 	voodoo3_write32(sc, DSTBASE, 0);
   1059 	voodoo3_write32(sc, DSTFORMAT,	fmt);
   1060 	voodoo3_write32(sc, COMMAND_2D,	COMMAND_2D_FILLRECT |
   1061 	    (ROP_INVERT << 24));
   1062 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
   1063 	voodoo3_write32(sc, DSTXY,	x | (y << 16));
   1064 	voodoo3_write32(sc, LAUNCH_2D,	x | (y << 16));
   1065 }
   1066 
   1067 static void
   1068 voodoofb_setup_mono(struct voodoofb_softc *sc, int xd, int yd, int width, int height, uint32_t fg,
   1069 					uint32_t bg)
   1070 {
   1071 	uint32_t dfmt, sfmt = sc->sc_linebytes;
   1072 
   1073 	dfmt = sc->sc_linebytes | ((sc->sc_bits_per_pixel +
   1074 	    ((sc->sc_bits_per_pixel == 8) ? 0 : 8)) << 13);
   1075 
   1076 	voodoo3_make_room(sc, 10);
   1077 	voodoo3_write32(sc, SRCFORMAT,	sfmt);
   1078 	voodoo3_write32(sc, DSTFORMAT,	dfmt);
   1079 	voodoo3_write32(sc, DSTBASE, 0);
   1080 	voodoo3_write32(sc, COLORFORE,	fg);
   1081 	voodoo3_write32(sc, COLORBACK,	bg);
   1082 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
   1083 	voodoo3_write32(sc, DSTXY,	xd | (yd << 16));
   1084 	voodoo3_write32(sc, SRCXY,	0);
   1085 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
   1086 	    (ROP_COPY << 24) | SST_2D_GO);
   1087 
   1088 	/* now feed the data into the chip */
   1089 }
   1090 
   1091 static void
   1092 voodoofb_feed_line(struct voodoofb_softc *sc, int count, uint8_t *data)
   1093 {
   1094 	int i;
   1095 	uint32_t latch = 0, bork;
   1096 	int shift = 0;
   1097 
   1098 	voodoo3_make_room(sc, count);
   1099 	for (i = 0; i < count; i++) {
   1100 		bork = data[i];
   1101 		latch |= (bork << shift);
   1102 		if (shift == 24) {
   1103 			voodoo3_write32(sc, LAUNCH_2D, latch);
   1104 			latch = 0;
   1105 			shift = 0;
   1106 		} else
   1107 			shift += 8;
   1108 		}
   1109 	if (shift != 24)
   1110 		voodoo3_write32(sc, LAUNCH_2D, latch);
   1111 }
   1112 
   1113 #if 0
   1114 static int
   1115 voodoofb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
   1116 {
   1117 
   1118 	return 0;
   1119 }
   1120 #endif
   1121 
   1122 /*
   1123  * wsdisplay_accessops
   1124  */
   1125 
   1126 static int
   1127 voodoofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
   1128 	struct lwp *l)
   1129 {
   1130 	struct vcons_data *vd = v;
   1131 	struct voodoofb_softc *sc = vd->cookie;
   1132 	struct wsdisplay_fbinfo *wdf;
   1133 	struct vcons_screen *ms = vd->active;
   1134 
   1135 	switch (cmd) {
   1136 	case WSDISPLAYIO_GTYPE:
   1137 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
   1138 		return 0;
   1139 
   1140 	case WSDISPLAYIO_GINFO:
   1141 		wdf = (void *)data;
   1142 		wdf->height = ms->scr_ri.ri_height;
   1143 		wdf->width = ms->scr_ri.ri_width;
   1144 		wdf->depth = ms->scr_ri.ri_depth;
   1145 		wdf->cmsize = 256;
   1146 		return 0;
   1147 
   1148 	case WSDISPLAYIO_GETCMAP:
   1149 		return voodoofb_getcmap(sc,
   1150 		    (struct wsdisplay_cmap *)data);
   1151 
   1152 	case WSDISPLAYIO_PUTCMAP:
   1153 		return voodoofb_putcmap(sc,
   1154 		    (struct wsdisplay_cmap *)data);
   1155 
   1156 	/* PCI config read/write passthrough. */
   1157 	case PCI_IOC_CFGREAD:
   1158 	case PCI_IOC_CFGWRITE:
   1159 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
   1160 		    cmd, data, flag, l);
   1161 
   1162 	case WSDISPLAYIO_GET_BUSID:
   1163 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
   1164 		    sc->sc_pcitag, data);
   1165 
   1166 	case WSDISPLAYIO_SMODE: {
   1167 		int new_mode = *(int*)data;
   1168 		if (new_mode != sc->sc_mode) {
   1169 			sc->sc_mode = new_mode;
   1170 			if (new_mode == WSDISPLAYIO_MODE_EMUL) {
   1171 				voodoofb_drm_map(sc);
   1172 				int i;
   1173 
   1174 				/* restore the palette */
   1175 				for (i = 0; i < 256; i++) {
   1176 					voodoofb_putpalreg(sc,
   1177 					   i,
   1178 					   sc->sc_cmap_red[i],
   1179 					   sc->sc_cmap_green[i],
   1180 					   sc->sc_cmap_blue[i]);
   1181 				}
   1182 
   1183 				/* zap the glyph cache */
   1184 				for (i = 0; i < 256; i++) {
   1185 					sc->sc_glyphs_defattr[i] = 0;
   1186 					sc->sc_glyphs_kernattr[i] = 0;
   1187 				}
   1188 				sc->sc_usedglyphs = 0;
   1189 
   1190 				voodoofb_clearscreen(sc);
   1191 				vcons_redraw_screen(ms);
   1192 			} else {
   1193 				voodoofb_drm_unmap(sc);
   1194 			}
   1195 		}
   1196 		}
   1197 		return 0;
   1198 	}
   1199 	return EPASSTHROUGH;
   1200 }
   1201 
   1202 static paddr_t
   1203 voodoofb_mmap(void *v, void *vs, off_t offset, int prot)
   1204 {
   1205 	struct vcons_data *vd = v;
   1206 	struct voodoofb_softc *sc = vd->cookie;
   1207 	paddr_t pa;
   1208 
   1209 	/* 'regular' framebuffer mmap()ing */
   1210 	if (offset < sc->sc_fbsize) {
   1211 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1212 		    BUS_SPACE_MAP_LINEAR);
   1213 		return pa;
   1214 	}
   1215 
   1216 	/*
   1217 	 * restrict all other mappings to processes with superuser privileges
   1218 	 * or the kernel itself
   1219 	 */
   1220 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
   1221 	    NULL, NULL, NULL, NULL) != 0) {
   1222 		aprint_error_dev(sc->sc_dev, "mmap() rejected.\n");
   1223 		return -1;
   1224 	}
   1225 
   1226 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
   1227 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1228 		    BUS_SPACE_MAP_LINEAR);
   1229 		return pa;
   1230 	}
   1231 
   1232 	if ((offset >= sc->sc_regs) && (offset < (sc->sc_regs +
   1233 	    sc->sc_regsize))) {
   1234 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1235 		    BUS_SPACE_MAP_LINEAR);
   1236 		return pa;
   1237 	}
   1238 
   1239 #ifdef PCI_MAGIC_IO_RANGE
   1240 	/* allow mapping of IO space */
   1241 	if ((offset >= PCI_MAGIC_IO_RANGE) &&\
   1242 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
   1243 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
   1244 		    0, prot, BUS_SPACE_MAP_LINEAR);
   1245 		return pa;
   1246 	}
   1247 #endif
   1248 
   1249 #ifdef OFB_ALLOW_OTHERS
   1250 	if (offset >= 0x80000000) {
   1251 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1252 		    BUS_SPACE_MAP_LINEAR);
   1253 		return pa;
   1254 	}
   1255 #endif
   1256 	return -1;
   1257 }
   1258 
   1259 static void
   1260 voodoofb_init_screen(void *cookie, struct vcons_screen *scr,
   1261     int existing, long *defattr)
   1262 {
   1263 	struct voodoofb_softc *sc = cookie;
   1264 	struct rasops_info *ri = &scr->scr_ri;
   1265 
   1266 	ri->ri_depth = sc->sc_bits_per_pixel;
   1267 	ri->ri_width = sc->sc_width;
   1268 	ri->ri_height = sc->sc_height;
   1269 	ri->ri_stride = sc->sc_linebytes;
   1270 	ri->ri_flg = RI_CENTER | RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
   1271 
   1272 	rasops_init(ri, 0, 0);
   1273 	ri->ri_caps = WSSCREEN_WSCOLORS;
   1274 
   1275 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
   1276 		    sc->sc_width / ri->ri_font->fontwidth);
   1277 
   1278 	ri->ri_hw = scr;
   1279 	ri->ri_ops.copyrows = voodoofb_copyrows;
   1280 	ri->ri_ops.copycols = voodoofb_copycols;
   1281 	ri->ri_ops.eraserows = voodoofb_eraserows;
   1282 	ri->ri_ops.erasecols = voodoofb_erasecols;
   1283 	ri->ri_ops.cursor = voodoofb_cursor;
   1284 	if (FONT_IS_ALPHA(ri->ri_font)) {
   1285 		ri->ri_ops.putchar = voodoofb_putchar_aa;
   1286 		ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG,
   1287 		     0, &sc->sc_defattr);
   1288 		sc->sc_kernattr = (WS_KERNEL_FG << 24) | (WS_KERNEL_BG << 16);
   1289 		voodoofb_init_glyphcache(sc, ri);
   1290 	} else {
   1291 		ri->ri_ops.putchar = voodoofb_putchar;
   1292 	}
   1293 }
   1294 
   1295 #if 0
   1296 int
   1297 voodoofb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
   1298 {
   1299 
   1300 	return 0;
   1301 }
   1302 #endif
   1303 
   1304 #ifdef VOODOOFB_ENABLE_INTR
   1305 static int
   1306 voodoofb_intr(void *arg)
   1307 {
   1308 	struct voodoofb_softc *sc = arg;
   1309 
   1310 	voodoo3_write32(sc, V3_STATUS, 0);	/* clear interrupts */
   1311 	return 1;
   1312 }
   1313 #endif
   1314 
   1315 /* video mode stuff */
   1316 
   1317 #define REFFREQ 14318	/* .18 */
   1318 
   1319 #define ABS(a) ((a < 0) ? -a : a)
   1320 
   1321 static int
   1322 voodoofb_calc_pll(int freq, int *f_out, int isBanshee)
   1323 {
   1324 	int m, n, k, best_m, best_n, best_k, f_cur, best_error;
   1325 	int minm, maxm;
   1326 
   1327 	best_error = freq;
   1328 	best_n = best_m = best_k = 0;
   1329 
   1330 	if (isBanshee) {
   1331 		minm = 24;
   1332 		maxm = 24;
   1333 	} else {
   1334 		minm = 1;
   1335 		maxm = 57;
   1336 		/* This used to be 64, alas it seems the last 8 (funny that ?)
   1337 		 * values cause jittering at lower resolutions. I've not done
   1338 		 * any calculations to what the adjustment affects clock ranges,
   1339 		 * but I can still run at 1600x1200@75Hz */
   1340 	}
   1341 	for (n = 1; n < 256; n++) {
   1342 		f_cur = REFFREQ * (n + 2);
   1343 		if (f_cur < freq) {
   1344 			f_cur = f_cur / 3;
   1345 			if (freq - f_cur < best_error) {
   1346 				best_error = freq - f_cur;
   1347 				best_n = n;
   1348 				best_m = 1;
   1349 				best_k = 0;
   1350 				continue;
   1351       			}
   1352 		}
   1353 		for (m = minm; m < maxm; m++) {
   1354 			for (k = 0; k < 4; k++) {
   1355 				f_cur = REFFREQ * (n + 2) / (m + 2) / (1 << k);
   1356 				if (ABS(f_cur - freq) < best_error) {
   1357 					best_error = ABS(f_cur - freq);
   1358 					best_n = n;
   1359 					best_m = m;
   1360 					best_k = k;
   1361 				}
   1362 			}
   1363 		}
   1364 	}
   1365 	n = best_n;
   1366 	m = best_m;
   1367 	k = best_k;
   1368 	*f_out = REFFREQ * (n + 2) / (m + 2) / (1 << k);
   1369 	return ( n << 8) | (m << 2) | k;
   1370 }
   1371 
   1372 static void
   1373 voodoofb_setup_monitor(struct voodoofb_softc *sc, const struct videomode *vm)
   1374 {
   1375 	struct voodoo_regs mod;
   1376 	struct voodoo_regs *mode;
   1377 	uint32_t horizontal_display_end, horizontal_sync_start,
   1378 		horizontal_sync_end, horizontal_total,
   1379 		horizontal_blanking_start, horizontal_blanking_end;
   1380 
   1381 	uint32_t vertical_display_enable_end, vertical_sync_start,
   1382 		vertical_sync_end, vertical_total, vertical_blanking_start,
   1383 		vertical_blanking_end;
   1384 
   1385 	uint32_t wd; // CRTC offset
   1386 
   1387 	int i;
   1388 
   1389 	uint8_t misc;
   1390 
   1391 	memset(&mod, 0, sizeof(mod));
   1392 
   1393 	mode = &mod;
   1394 
   1395 	wd = (vm->hdisplay >> 3) - 1;
   1396 	horizontal_display_end	= (vm->hdisplay >> 3) - 1;
   1397 	horizontal_sync_start	= (vm->hsync_start >> 3) - 1;
   1398 	horizontal_sync_end	= (vm->hsync_end >> 3) - 1;
   1399 	horizontal_total  	= (vm->htotal   >> 3) - 1;
   1400 	horizontal_blanking_start = horizontal_display_end;
   1401 	horizontal_blanking_end = horizontal_total;
   1402 
   1403 	vertical_display_enable_end  = vm->vdisplay - 1;
   1404 	vertical_sync_start  	= vm->vsync_start;	// - 1;
   1405 	vertical_sync_end	= vm->vsync_end;	// - 1;
   1406 	vertical_total		= vm->vtotal - 2;
   1407 	vertical_blanking_start	= vertical_display_enable_end;
   1408 	vertical_blanking_end	= vertical_total;
   1409 
   1410 #if 0
   1411 	misc = 0x0f |
   1412 	    (vm->hdisplay < 400 ? 0xa0 :
   1413 		vm->hdisplay < 480 ? 0x60 :
   1414 		vm->hdisplay < 768 ? 0xe0 : 0x20);
   1415 #else
   1416 	misc = 0x2f;
   1417 	if (vm->flags & VID_NHSYNC)
   1418 		misc |= HSYNC_NEG;
   1419 	if (vm->flags & VID_NVSYNC)
   1420 		misc |= VSYNC_NEG;
   1421 #ifdef VOODOOFB_DEBUG
   1422 	printf("misc: %02x\n", misc);
   1423 #endif
   1424 #endif
   1425 	mode->vr_seq[0] = 3;
   1426 	mode->vr_seq[1] = 1;
   1427 	mode->vr_seq[2] = 8;
   1428 	mode->vr_seq[3] = 0;
   1429 	mode->vr_seq[4] = 6;
   1430 
   1431 	/* crtc regs start */
   1432 	mode->vr_crtc[0] = horizontal_total - 4;
   1433 	mode->vr_crtc[1] = horizontal_display_end;
   1434 	mode->vr_crtc[2] = horizontal_blanking_start;
   1435 	mode->vr_crtc[3] = 0x80 | (horizontal_blanking_end & 0x1f);
   1436 	mode->vr_crtc[4] = horizontal_sync_start;
   1437 
   1438 	mode->vr_crtc[5] = ((horizontal_blanking_end & 0x20) << 2) |
   1439 	    (horizontal_sync_end & 0x1f);
   1440 	mode->vr_crtc[6] = vertical_total;
   1441 	mode->vr_crtc[7] = ((vertical_sync_start & 0x200) >> 2) |
   1442 	    ((vertical_display_enable_end & 0x200) >> 3) |
   1443 	    ((vertical_total & 0x200) >> 4) |
   1444 	    0x10 |
   1445 	    ((vertical_blanking_start & 0x100) >> 5) |
   1446 	    ((vertical_sync_start  & 0x100) >> 6) |
   1447 	    ((vertical_display_enable_end  & 0x100) >> 7) |
   1448 	    ((vertical_total  & 0x100) >> 8);
   1449 
   1450 	mode->vr_crtc[8] = 0;
   1451 	mode->vr_crtc[9] = 0x40 |
   1452 	    ((vertical_blanking_start & 0x200) >> 4);
   1453 
   1454 	mode->vr_crtc[10] = 0;
   1455 	mode->vr_crtc[11] = 0;
   1456 	mode->vr_crtc[12] = 0;
   1457 	mode->vr_crtc[13] = 0;
   1458 	mode->vr_crtc[14] = 0;
   1459 	mode->vr_crtc[15] = 0;
   1460 
   1461 	mode->vr_crtc[16] = vertical_sync_start;
   1462 	mode->vr_crtc[17] = (vertical_sync_end & 0x0f) | 0x20;
   1463 	mode->vr_crtc[18] = vertical_display_enable_end;
   1464 	mode->vr_crtc[19] = wd; // CRTC offset
   1465 	mode->vr_crtc[20] = 0;
   1466 	mode->vr_crtc[21] = vertical_blanking_start;
   1467 	mode->vr_crtc[22] = vertical_blanking_end + 1;
   1468 	mode->vr_crtc[23] = 128;
   1469 	mode->vr_crtc[24] = 255;
   1470 
   1471 	/* overflow registers */
   1472 	mode->vr_crtc[CRTC_HDISP_EXT] =
   1473 	    (horizontal_total&0x100) >> 8 |
   1474 	    (horizontal_display_end & 0x100) >> 6 |
   1475 	    (horizontal_blanking_start & 0x100) >> 4 |
   1476 	    (horizontal_blanking_end & 0x40) >> 1 |
   1477 	    (horizontal_sync_start & 0x100) >> 2 |
   1478 	    (horizontal_sync_end & 0x20) << 2;
   1479 
   1480 	mode->vr_crtc[CRTC_VDISP_EXT] =
   1481 	    (vertical_total & 0x400) >> 10 |
   1482 	    (vertical_display_enable_end & 0x400) >> 8 | /* the manual is contradictory here */
   1483 	    (vertical_blanking_start & 0x400) >> 6 |
   1484 	    (vertical_blanking_end & 0x400) >> 4;
   1485 
   1486 	/* attr regs start */
   1487 	mode->vr_attr[0] = 0;
   1488 	mode->vr_attr[1] = 0;
   1489 	mode->vr_attr[2] = 0;
   1490 	mode->vr_attr[3] = 0;
   1491 	mode->vr_attr[4] = 0;
   1492 	mode->vr_attr[5] = 0;
   1493 	mode->vr_attr[6] = 0;
   1494 	mode->vr_attr[7] = 0;
   1495 	mode->vr_attr[8] = 0;
   1496 	mode->vr_attr[9] = 0;
   1497 	mode->vr_attr[10] = 0;
   1498 	mode->vr_attr[11] = 0;
   1499 	mode->vr_attr[12] = 0;
   1500 	mode->vr_attr[13] = 0;
   1501 	mode->vr_attr[14] = 0;
   1502 	mode->vr_attr[15] = 0;
   1503 	mode->vr_attr[16] = 1;
   1504 	mode->vr_attr[17] = 0;
   1505 	mode->vr_attr[18] = 15;
   1506 	mode->vr_attr[19] = 0;
   1507 	/* attr regs end */
   1508 
   1509 	/* graph regs start */
   1510 	mode->vr_graph[0] = 159;
   1511 	mode->vr_graph[1] = 127;
   1512 	mode->vr_graph[2] = 127;
   1513 	mode->vr_graph[3] = 131;
   1514 	mode->vr_graph[4] = 130;
   1515 	mode->vr_graph[5] = 142;
   1516 	mode->vr_graph[6] = 30;
   1517 	mode->vr_graph[7] = 245;
   1518 	mode->vr_graph[8] = 0;
   1519 
   1520 	vga_outb(sc, MISC_W, misc | 0x01);
   1521 
   1522 	for(i = 0; i < 5; i++)
   1523 		voodoo3_write_seq(sc, i, mode->vr_seq[i]);
   1524 	for (i = 0; i < CRTC_PCI_READBACK; i ++)
   1525         	voodoo3_write_crtc(sc, i, mode->vr_crtc[i]);
   1526 	for (i = 0; i < 0x14; i ++)
   1527         	voodoo3_write_attr(sc, i, mode->vr_attr[i]);
   1528 	for (i = 0; i < 0x09; i ++)
   1529         	voodoo3_write_gra(sc, i, mode->vr_graph[i]);
   1530 }
   1531 
   1532 static void
   1533 voodoofb_set_videomode(struct voodoofb_softc *sc,
   1534     const struct videomode *vm)
   1535 {
   1536 	uint32_t miscinit0 = 0;
   1537 	int vidpll, fout;
   1538 	uint32_t vp, vidproc = VIDPROCDEFAULT;
   1539 	uint32_t bpp = 1;	/* for now */
   1540 	uint32_t bytes_per_row = vm->hdisplay * bpp;
   1541 
   1542 	sc->sc_bits_per_pixel = bpp << 3;
   1543 	sc->sc_width = vm->hdisplay;
   1544 	sc->sc_height = vm->vdisplay;
   1545 	sc->sc_linebytes = bytes_per_row;
   1546 
   1547 	voodoofb_setup_monitor(sc, vm);
   1548 	vp = voodoo3_read32(sc, VIDPROCCFG);
   1549 
   1550 	vidproc &= ~(0x1c0000); /* clear bits 18 to 20, bpp in vidproccfg */
   1551 	/* enable bits 18 to 20 to the required bpp */
   1552 	vidproc |= ((bpp - 1) << VIDCFG_PIXFMT_SHIFT);
   1553 
   1554 	vidpll = voodoofb_calc_pll(vm->dot_clock, &fout, 0);
   1555 
   1556 #ifdef VOODOOFB_DEBUG
   1557 	printf("old vidproc: %08x\n", vp);
   1558 	printf("pll: %08x %d\n", vidpll, fout);
   1559 #endif
   1560 	/* bit 10 of vidproccfg, is enabled or disabled as needed */
   1561 	switch (bpp) {
   1562 		case 1:
   1563 			/*
   1564 			 * bit 10 off for palettized modes only, off means
   1565 			 * palette is used
   1566 			 */
   1567 			vidproc &= ~(1 << 10);
   1568 			break;
   1569 #if 0
   1570 		case 2:
   1571 			#if __POWERPC__
   1572 				miscinit0 = 0xc0000000;
   1573 			#endif
   1574 			/* bypass palette for 16bit modes */
   1575 			vidproc |= (1 << 10);
   1576 			break;
   1577 		case 4:
   1578 			#if __POWERPC__
   1579 				miscinit0 = 0x40000000;
   1580 			#endif
   1581 			vidproc |= (1 << 10); /* Same for 32bit modes */
   1582 			break;
   1583 #endif
   1584 		default:
   1585 			printf("We support only 8 bit for now\n");
   1586 			return;
   1587 	}
   1588 
   1589 	voodoofb_wait_idle(sc);
   1590 
   1591 	voodoo3_write32(sc, MISCINIT1, voodoo3_read32(sc, MISCINIT1) | 0x01);
   1592 
   1593 	voodoo3_make_room(sc, 4);
   1594 	voodoo3_write32(sc, VGAINIT0, 4928);
   1595 	voodoo3_write32(sc, DACMODE, 0);
   1596 	voodoo3_write32(sc, VIDDESKSTRIDE, bytes_per_row);
   1597 	voodoo3_write32(sc, PLLCTRL0, vidpll);
   1598 
   1599 	voodoo3_make_room(sc, 5);
   1600 	voodoo3_write32(sc, VIDSCREENSIZE, sc->sc_width |
   1601 	    (sc->sc_height << 12));
   1602 	voodoo3_write32(sc, VIDDESKSTART,  0);
   1603 
   1604 	vidproc &= ~VIDCFG_HWCURSOR_ENABLE;
   1605 	voodoo3_write32(sc, VIDPROCCFG, vidproc);
   1606 
   1607 	voodoo3_write32(sc, VGAINIT1, 0);
   1608 	voodoo3_write32(sc, MISCINIT0, miscinit0);
   1609 #ifdef VOODOOFB_DEBUG
   1610 	printf("vidproc: %08x\n", vidproc);
   1611 #endif
   1612 	voodoo3_make_room(sc, 8);
   1613 	voodoo3_write32(sc, SRCBASE, 0);
   1614 	voodoo3_write32(sc, DSTBASE, 0);
   1615 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
   1616   	voodoo3_write32(sc, CLIP0MIN,        0);
   1617   	voodoo3_write32(sc, CLIP0MAX,        0x0fff0fff);
   1618   	voodoo3_write32(sc, CLIP1MIN,        0);
   1619   	voodoo3_write32(sc, CLIP1MAX,        0x0fff0fff);
   1620 	voodoo3_write32(sc, SRCXY, 0);
   1621 	voodoofb_wait_idle(sc);
   1622 	printf("%s: switched to %dx%d, %d bit\n", device_xname(sc->sc_dev),
   1623 	    sc->sc_width, sc->sc_height, sc->sc_bits_per_pixel);
   1624 	sc->sc_numglyphs = 0;	/* invalidate the glyph cache */
   1625 }
   1626 
   1627 static void
   1628 voodoofb_init(struct voodoofb_softc *sc)
   1629 {
   1630 	/* XXX */
   1631 	uint32_t vgainit0 = 0;
   1632 	uint32_t vidcfg = 0;
   1633 
   1634 #ifdef VOODOOFB_DEBUG
   1635 	printf("initializing engine...");
   1636 #endif
   1637 	vgainit0 = voodoo3_read32(sc, VGAINIT0);
   1638 #ifdef VOODOOFB_DEBUG
   1639 	printf("vga: %08x", vgainit0);
   1640 #endif
   1641 	vgainit0 |=
   1642 	    VGAINIT0_8BIT_DAC     |
   1643 	    VGAINIT0_EXT_ENABLE   |
   1644 	    VGAINIT0_WAKEUP_3C3   |
   1645 	    VGAINIT0_ALT_READBACK |
   1646 	    VGAINIT0_EXTSHIFTOUT;
   1647 
   1648 	vidcfg = voodoo3_read32(sc, VIDPROCCFG);
   1649 #ifdef VOODOOFB_DEBUG
   1650 	printf(" vidcfg: %08x\n", vidcfg);
   1651 #endif
   1652 	vidcfg |=
   1653 	    VIDCFG_VIDPROC_ENABLE |
   1654 	    VIDCFG_DESK_ENABLE;
   1655 	vidcfg &= ~VIDCFG_HWCURSOR_ENABLE;
   1656 
   1657 	voodoo3_make_room(sc, 2);
   1658 
   1659 	voodoo3_write32(sc, VGAINIT0, vgainit0);
   1660 	voodoo3_write32(sc, VIDPROCCFG, vidcfg);
   1661 
   1662 	voodoo3_make_room(sc, 8);
   1663 	voodoo3_write32(sc, SRCBASE, 0);
   1664 	voodoo3_write32(sc, DSTBASE, 0);
   1665 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
   1666   	voodoo3_write32(sc, CLIP0MIN,        0);
   1667   	voodoo3_write32(sc, CLIP0MAX,        0x1fff1fff);
   1668   	voodoo3_write32(sc, CLIP1MIN,        0);
   1669   	voodoo3_write32(sc, CLIP1MAX,        0x1fff1fff);
   1670 	voodoo3_write32(sc, SRCXY, 0);
   1671 
   1672 	voodoofb_wait_idle(sc);
   1673 }
   1674 
   1675 #define MAX_HRES  1700		/*
   1676 				 * XXX in theory we can go higher but I
   1677 				 * couldn't get anything above 1680 x 1200
   1678 				 * to work, so until I find out why, it's
   1679 				 * disabled so people won't end up with a
   1680 				 * blank screen
   1681 				 */
   1682 #define MODE_IS_VALID(m, mclk) (((m)->dot_clock <= (mclk)) && \
   1683 					    ((m)->hdisplay < MAX_HRES))
   1684 static void
   1685 voodoofb_setup_i2c(struct voodoofb_softc *sc)
   1686 {
   1687 	int i;
   1688 
   1689 	/* Fill in the i2c tag */
   1690 	sc->sc_i2c.ic_cookie = sc;
   1691 	sc->sc_i2c.ic_acquire_bus = voodoofb_i2c_acquire_bus;
   1692 	sc->sc_i2c.ic_release_bus = voodoofb_i2c_release_bus;
   1693 	sc->sc_i2c.ic_send_start = voodoofb_i2c_send_start;
   1694 	sc->sc_i2c.ic_send_stop = voodoofb_i2c_send_stop;
   1695 	sc->sc_i2c.ic_initiate_xfer = voodoofb_i2c_initiate_xfer;
   1696 	sc->sc_i2c.ic_read_byte = voodoofb_i2c_read_byte;
   1697 	sc->sc_i2c.ic_write_byte = voodoofb_i2c_write_byte;
   1698 	sc->sc_i2c.ic_exec = NULL;
   1699 
   1700 	sc->sc_i2creg = voodoo3_read32(sc, VIDSERPARPORT);
   1701 #ifdef VOODOOFB_DEBUG
   1702 	printf("data: %08x\n", sc->sc_i2creg);
   1703 #endif
   1704 	sc->sc_i2creg |= VSP_ENABLE_IIC0;
   1705 	sc->sc_i2creg &= ~(VSP_SDA0_OUT | VSP_SCL0_OUT);
   1706 	voodoo3_write32(sc, VIDSERPARPORT, sc->sc_i2creg);
   1707 
   1708 	/* zero out the EDID buffer */
   1709 	memset(sc->sc_edid_data, 0, 128);
   1710 
   1711 	/* Some monitors don't respond first time */
   1712 	i = 0;
   1713 	while (sc->sc_edid_data[1] == 0 && i++ < 3)
   1714 		ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
   1715 	if (i < 3) {
   1716 		if (edid_parse(sc->sc_edid_data, &sc->sc_edid_info) != -1) {
   1717 #ifdef VOODOOFB_DEBUG
   1718 			edid_print(&sc->sc_edid_info);
   1719 #endif
   1720 			/*
   1721 			 * Now pick a mode.
   1722 			 * How do we know our max. pixel clock?
   1723 			 * All Voodoo3 should support at least 250MHz.
   1724 			 * All Voodoo3 I've seen so far have at least 8MB
   1725 			 * which we're not going to exhaust either in 8bit.
   1726 			 */
   1727 			if ((sc->sc_edid_info.edid_preferred_mode != NULL)) {
   1728 				struct videomode *m =
   1729 				    sc->sc_edid_info.edid_preferred_mode;
   1730 				if (MODE_IS_VALID(m, sc->sc_max_clock)) {
   1731 					sc->sc_videomode = m;
   1732 				} else {
   1733 					aprint_error_dev(sc->sc_dev,
   1734 					    "unable to use preferred mode\n");
   1735 				}
   1736 			}
   1737 			/*
   1738 			 * if we can't use the preferred mode go look for the
   1739 			 * best one we can support
   1740 			 */
   1741 			if (sc->sc_videomode == NULL) {
   1742 				int n = 0;
   1743 				struct videomode *m =
   1744 				     sc->sc_edid_info.edid_modes;
   1745 
   1746 				sort_modes(sc->sc_edid_info.edid_modes,
   1747 			   	    &sc->sc_edid_info.edid_preferred_mode,
   1748 				    sc->sc_edid_info.edid_nmodes);
   1749 				while ((sc->sc_videomode == NULL) &&
   1750 				       (n < sc->sc_edid_info.edid_nmodes)) {
   1751 					if (MODE_IS_VALID(&m[n],
   1752 					    sc->sc_max_clock)) {
   1753 						sc->sc_videomode = &m[n];
   1754 					}
   1755 					n++;
   1756 				}
   1757 			}
   1758 		}
   1759 	}
   1760 }
   1761 
   1762 /* I2C bitbanging */
   1763 static void voodoofb_i2cbb_set_bits(void *cookie, uint32_t bits)
   1764 {
   1765 	struct voodoofb_softc *sc = cookie;
   1766 	uint32_t out;
   1767 
   1768 	out = bits >> 2;	/* bitmasks match the IN bits */
   1769 
   1770 	voodoo3_write32(sc, VIDSERPARPORT, sc->sc_i2creg | out);
   1771 }
   1772 
   1773 static void voodoofb_i2cbb_set_dir(void *cookie, uint32_t dir)
   1774 {
   1775 	/* Nothing to do */
   1776 }
   1777 
   1778 static uint32_t voodoofb_i2cbb_read(void *cookie)
   1779 {
   1780 	struct voodoofb_softc *sc = cookie;
   1781 	uint32_t bits;
   1782 
   1783 	bits = voodoo3_read32(sc, VIDSERPARPORT);
   1784 
   1785 	return bits;
   1786 }
   1787 
   1788 /* higher level I2C stuff */
   1789 static int
   1790 voodoofb_i2c_acquire_bus(void *cookie, int flags)
   1791 {
   1792 	/* private bus */
   1793 	return (0);
   1794 }
   1795 
   1796 static void
   1797 voodoofb_i2c_release_bus(void *cookie, int flags)
   1798 {
   1799 	/* private bus */
   1800 }
   1801 
   1802 static int
   1803 voodoofb_i2c_send_start(void *cookie, int flags)
   1804 {
   1805 	return (i2c_bitbang_send_start(cookie, flags, &voodoofb_i2cbb_ops));
   1806 }
   1807 
   1808 static int
   1809 voodoofb_i2c_send_stop(void *cookie, int flags)
   1810 {
   1811 
   1812 	return (i2c_bitbang_send_stop(cookie, flags, &voodoofb_i2cbb_ops));
   1813 }
   1814 
   1815 static int
   1816 voodoofb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
   1817 {
   1818 
   1819 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
   1820 	    &voodoofb_i2cbb_ops));
   1821 }
   1822 
   1823 static int
   1824 voodoofb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
   1825 {
   1826 	return (i2c_bitbang_read_byte(cookie, valp, flags, &voodoofb_i2cbb_ops));
   1827 }
   1828 
   1829 static int
   1830 voodoofb_i2c_write_byte(void *cookie, uint8_t val, int flags)
   1831 {
   1832 	return (i2c_bitbang_write_byte(cookie, val, flags, &voodoofb_i2cbb_ops));
   1833 }
   1834 
   1835 /* glyph cache */
   1836 static void
   1837 voodoofb_init_glyphcache(struct voodoofb_softc *sc, struct rasops_info *ri)
   1838 {
   1839 	int bufsize, i;
   1840 
   1841 	if (sc->sc_numglyphs != 0) {
   1842 		/* re-initialize */
   1843 		if (ri->ri_font == sc->sc_font) {
   1844 			/* nothing to do, keep using the same cache */
   1845 			return;
   1846 		}
   1847 	}
   1848 	sc->sc_cache = (ri->ri_width * ri->ri_stride + 3) & 0xfffffffc;
   1849 	bufsize = sc->sc_memsize - sc->sc_cache;
   1850 	sc->sc_numglyphs = bufsize / ri->ri_fontscale;
   1851 	sc->sc_usedglyphs = 0;
   1852 	for (i = 0; i < 256; i++) {
   1853 		sc->sc_glyphs_kernattr[i] = 0;
   1854 		sc->sc_glyphs_defattr[i] = 0;
   1855 	}
   1856 	sc->sc_fmt = ri->ri_font->fontwidth | FMT_8BIT;
   1857 }
   1858