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