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