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