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