xref: /netbsd-src/sys/dev/pci/voodoofb.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: voodoofb.c,v 1.12 2007/12/01 17:00:41 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 2005, 2006 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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * A console driver for 3Dfx Voodoo3 graphics boards
32  * Thanks to Andreas Drewke (andreas_dr@gmx.de) for his Voodoo3 driver for BeOS
33  * which I used as reference / documentation
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: voodoofb.c,v 1.12 2007/12/01 17:00:41 ad Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
44 #include <sys/callout.h>
45 
46 #include <uvm/uvm_extern.h>
47 
48 #if defined(macppc) || defined (sparc64) || defined(ofppc)
49 #define HAVE_OPENFIRMWARE
50 #endif
51 
52 /* XXX should be configurable */
53 #define VOODOOFB_VIDEOMODE 15
54 
55 #ifdef HAVE_OPENFIRMWARE
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/ofw_pci.h>
58 #endif
59 
60 #include <dev/videomode/videomode.h>
61 
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/pcidevs.h>
65 #include <dev/pci/pciio.h>
66 #include <dev/pci/voodoofbreg.h>
67 
68 #include <dev/wscons/wsdisplayvar.h>
69 #include <dev/wscons/wsconsio.h>
70 #include <dev/wsfont/wsfont.h>
71 #include <dev/rasops/rasops.h>
72 #include <dev/wscons/wsdisplay_vconsvar.h>
73 
74 #include "opt_wsemul.h"
75 
76 struct voodoofb_softc {
77 	struct device sc_dev;
78 	pci_chipset_tag_t sc_pc;
79 	pcitag_t sc_pcitag;
80 	struct pci_attach_args sc_pa;
81 
82 	bus_space_tag_t sc_memt;
83 	bus_space_tag_t sc_iot;
84 	bus_space_handle_t sc_memh;
85 
86 	bus_space_tag_t sc_regt;
87 	bus_space_tag_t sc_fbt;
88 	bus_space_tag_t sc_ioregt;
89 	bus_space_handle_t sc_regh;
90 	bus_space_handle_t sc_fbh;
91 	bus_space_handle_t sc_ioregh;
92 	bus_addr_t sc_regs, sc_fb, sc_ioreg;
93 	bus_size_t sc_regsize, sc_fbsize, sc_ioregsize;
94 
95 	void *sc_ih;
96 
97 	size_t memsize;
98 	int memtype;
99 
100 	int bits_per_pixel;
101 	int width, height, linebytes;
102 
103 	int sc_mode;
104 	uint32_t sc_bg;
105 
106 	u_char sc_cmap_red[256];
107 	u_char sc_cmap_green[256];
108 	u_char sc_cmap_blue[256];
109 	int sc_dacw;
110 
111 	struct vcons_data vd;
112 };
113 
114 struct voodoo_regs {
115 	uint8_t vr_crtc[31];
116 	uint8_t vr_graph[9];
117 	uint8_t vr_attr[21];
118 	uint8_t vr_seq[5];
119 };
120 
121 static struct vcons_screen voodoofb_console_screen;
122 
123 extern const u_char rasops_cmap[768];
124 
125 static int	voodoofb_match(struct device *, struct cfdata *, void *);
126 static void	voodoofb_attach(struct device *, struct device *, void *);
127 
128 static int	voodoofb_drm_print(void *, const char *);
129 static int	voodoofb_drm_unmap(struct voodoofb_softc *);
130 static int	voodoofb_drm_map(struct voodoofb_softc *);
131 
132 CFATTACH_DECL(voodoofb, sizeof(struct voodoofb_softc), voodoofb_match,
133     voodoofb_attach, NULL, NULL);
134 
135 static int	voodoofb_is_console(struct pci_attach_args *);
136 static void 	voodoofb_init(struct voodoofb_softc *);
137 
138 static void	voodoofb_cursor(void *, int, int, int);
139 static void	voodoofb_putchar(void *, int, int, u_int, long);
140 static void	voodoofb_copycols(void *, int, int, int, int);
141 static void	voodoofb_erasecols(void *, int, int, int, long);
142 static void	voodoofb_copyrows(void *, int, int, int);
143 static void	voodoofb_eraserows(void *, int, int, long);
144 
145 #if 0
146 static int	voodoofb_allocattr(void *, int, int, int, long *);
147 static void	voodoofb_scroll(void *, void *, int);
148 static int	voodoofb_load_font(void *, void *, struct wsdisplay_font *);
149 #endif
150 
151 static int	voodoofb_putcmap(struct voodoofb_softc *,
152 			    struct wsdisplay_cmap *);
153 static int 	voodoofb_getcmap(struct voodoofb_softc *,
154 			    struct wsdisplay_cmap *);
155 static int 	voodoofb_putpalreg(struct voodoofb_softc *, uint8_t, uint8_t,
156 			    uint8_t, uint8_t);
157 static void	voodoofb_bitblt(struct voodoofb_softc *, int, int, int, int,
158 			    int, int);
159 static void	voodoofb_rectfill(struct voodoofb_softc *, int, int, int, int,
160 			    int);
161 static void	voodoofb_rectinvert(struct voodoofb_softc *, int, int, int,
162 			    int);
163 static void	voodoofb_setup_mono(struct voodoofb_softc *, int, int, int,
164 			    int, uint32_t, uint32_t);
165 static void	voodoofb_feed_line(struct voodoofb_softc *, int, uint8_t *);
166 
167 #ifdef VOODOOFB_DEBUG
168 static void	voodoofb_showpal(struct voodoofb_softc *);
169 #endif
170 
171 static void	voodoofb_wait_idle(struct voodoofb_softc *);
172 
173 #ifdef VOODOOFB_ENABLE_INTR
174 static int	voodoofb_intr(void *);
175 #endif
176 
177 static void	voodoofb_set_videomode(struct voodoofb_softc *,
178 			    const struct videomode *);
179 
180 struct wsscreen_descr voodoofb_defaultscreen = {
181 	"default",
182 	0, 0,
183 	NULL,
184 	8, 16,
185 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
186 	NULL,
187 };
188 
189 const struct wsscreen_descr *_voodoofb_scrlist[] = {
190 	&voodoofb_defaultscreen,
191 	/* XXX other formats, graphics screen? */
192 };
193 
194 struct wsscreen_list voodoofb_screenlist = {
195 	sizeof(_voodoofb_scrlist) / sizeof(struct wsscreen_descr *), _voodoofb_scrlist
196 };
197 
198 static int	voodoofb_ioctl(void *, void *, u_long, void *, int,
199 		    struct lwp *);
200 static paddr_t	voodoofb_mmap(void *, void *, off_t, int);
201 
202 static void	voodoofb_clearscreen(struct voodoofb_softc *);
203 static void	voodoofb_init_screen(void *, struct vcons_screen *, int,
204 			    long *);
205 
206 
207 struct wsdisplay_accessops voodoofb_accessops = {
208 	voodoofb_ioctl,
209 	voodoofb_mmap,
210 	NULL,
211 	NULL,
212 	NULL,
213 	NULL,	/* load_font */
214 	NULL,	/* polls */
215 	NULL,	/* scroll */
216 };
217 
218 /*
219  * Inline functions for getting access to register aperture.
220  */
221 static inline void
222 voodoo3_write32(struct voodoofb_softc *sc, uint32_t reg, uint32_t val)
223 {
224 	bus_space_write_4(sc->sc_regt, sc->sc_regh, reg, val);
225 }
226 
227 static inline uint32_t
228 voodoo3_read32(struct voodoofb_softc *sc, uint32_t reg)
229 {
230 	return bus_space_read_4(sc->sc_regt, sc->sc_regh, reg);
231 }
232 
233 static inline void
234 voodoo3_write_crtc(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
235 {
236 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_INDEX - 0x300, reg);
237 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, CRTC_DATA - 0x300, val);
238 }
239 
240 static inline void
241 voodoo3_write_seq(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
242 {
243 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_INDEX - 0x300, reg);
244 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, SEQ_DATA - 0x300, val);
245 }
246 
247 static inline void
248 voodoo3_write_gra(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
249 {
250 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_INDEX - 0x300, reg);
251 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, GRA_DATA - 0x300, val);
252 }
253 
254 static inline void
255 voodoo3_write_attr(struct voodoofb_softc *sc, uint8_t reg, uint8_t val)
256 {
257 	volatile uint8_t junk;
258 	uint8_t index;
259 
260 	junk = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, IS1_R - 0x300);
261 	index = bus_space_read_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300);
262 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, reg);
263 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, val);
264 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, ATT_IW - 0x300, index);
265 }
266 
267 static inline void
268 vga_outb(struct voodoofb_softc *sc, uint32_t reg,  uint8_t val)
269 {
270 	bus_space_write_1(sc->sc_ioregt, sc->sc_ioregh, reg - 0x300, val);
271 }
272 
273 /* wait until there's room for len bytes in the FIFO */
274 static inline void
275 voodoo3_make_room(struct voodoofb_softc *sc, int len)
276 {
277 	while ((voodoo3_read32(sc, STATUS) & 0x1f) < len);
278 }
279 
280 static void
281 voodoofb_wait_idle(struct voodoofb_softc *sc)
282 {
283 	int i = 0;
284 
285 	voodoo3_make_room(sc, 1);
286 	voodoo3_write32(sc, COMMAND_3D, COMMAND_3D_NOP);
287 
288 	while (1) {
289 		i = (voodoo3_read32(sc, STATUS) & STATUS_BUSY) ? 0 : i + 1;
290 		if(i == 3) break;
291 	}
292 }
293 
294 static int
295 voodoofb_match(struct device *parent, struct cfdata *match, void *aux)
296 {
297 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
298 
299 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
300 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
301 		return 0;
302 	if ((PCI_VENDOR(pa->pa_id)==PCI_VENDOR_3DFX) &&
303 	    (PCI_PRODUCT(pa->pa_id)>=PCI_PRODUCT_3DFX_VOODOO3))
304 		return 100;
305 	return 0;
306 }
307 
308 static void
309 voodoofb_attach(struct device *parent, struct device *self, void *aux)
310 {
311 	struct voodoofb_softc *sc = (void *)self;
312 	struct pci_attach_args *pa = aux;
313 	char devinfo[256];
314 	struct wsemuldisplaydev_attach_args aa;
315 	struct rasops_info *ri;
316 #ifdef VOODOOFB_ENABLE_INTR
317 	pci_intr_handle_t ih;
318 	const char *intrstr;
319 #endif
320 	ulong defattr;
321 	int console, width, height, i, j;
322 #ifdef HAVE_OPENFIRMWARE
323 	int linebytes, depth, node;
324 #endif
325 	uint32_t bg, fg, ul;
326 
327 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
328 #ifdef HAVE_OPENFIRMWARE
329 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
330 #endif
331 	sc->sc_pc = pa->pa_pc;
332 	sc->sc_pcitag = pa->pa_tag;
333 	sc->sc_dacw = -1;
334 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
335 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
336 
337 	sc->sc_memt = pa->pa_memt;
338 	sc->sc_iot = pa->pa_iot;
339 	sc->sc_pa = *pa;
340 
341 	/* the framebuffer */
342 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM,
343 	    BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE |
344 	    BUS_SPACE_MAP_LINEAR,
345 	    &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
346 		printf("%s: failed to map the frame buffer.\n",
347 		    sc->sc_dev.dv_xname);
348 	}
349 
350 	/* memory-mapped registers */
351 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
352 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
353 		printf("%s: failed to map memory-mapped registers.\n",
354 		    sc->sc_dev.dv_xname);
355 	}
356 
357 	/* IO-mapped registers */
358 	if (pci_mapreg_map(pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
359 	    &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
360 	    &sc->sc_ioregsize)) {
361 		printf("%s: failed to map IO-mapped registers.\n",
362 		    sc->sc_dev.dv_xname);
363 	}
364 	voodoofb_init(sc);
365 
366 	/* we should read these from the chip instead of depending on OF */
367 	width = height = -1;
368 
369 #ifdef HAVE_OPENFIRMWARE
370 	if (OF_getprop(node, "width", &width, 4) != 4)
371 		OF_interpret("screen-width", 1, 1, &width);
372 	if (OF_getprop(node, "height", &height, 4) != 4)
373 		OF_interpret("screen-height", 1, 1, &height);
374 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
375 		linebytes = width;			/* XXX */
376 	if (OF_getprop(node, "depth", &depth, 4) != 4)
377 		depth = 8;				/* XXX */
378 
379 	if (width == -1 || height == -1)
380 		return;
381 
382 	sc->width = width;
383 	sc->height = height;
384 	sc->bits_per_pixel = depth;
385 	sc->linebytes = linebytes;
386 	printf("%s: initial resolution %dx%d, %d bit\n", sc->sc_dev.dv_xname,
387 	    sc->width, sc->height, sc->bits_per_pixel);
388 #endif
389 
390 	/* XXX this should at least be configurable via kernel config */
391 	voodoofb_set_videomode(sc, &videomode_list[VOODOOFB_VIDEOMODE]);
392 
393 	vcons_init(&sc->vd, sc, &voodoofb_defaultscreen, &voodoofb_accessops);
394 	sc->vd.init_screen = voodoofb_init_screen;
395 
396 	console = voodoofb_is_console(pa);
397 
398 	ri = &voodoofb_console_screen.scr_ri;
399 	if (console) {
400 		vcons_init_screen(&sc->vd, &voodoofb_console_screen, 1,
401 		    &defattr);
402 		voodoofb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
403 
404 		voodoofb_defaultscreen.textops = &ri->ri_ops;
405 		voodoofb_defaultscreen.capabilities = ri->ri_caps;
406 		voodoofb_defaultscreen.nrows = ri->ri_rows;
407 		voodoofb_defaultscreen.ncols = ri->ri_cols;
408 		wsdisplay_cnattach(&voodoofb_defaultscreen, ri, 0, 0, defattr);
409 	} else {
410 		/*
411 		 * since we're not the console we can postpone the rest
412 		 * until someone actually allocates a screen for us
413 		 */
414 		voodoofb_set_videomode(sc, &videomode_list[0]);
415 	}
416 
417 	printf("%s: %d MB aperture at 0x%08x, %d MB registers at 0x%08x\n",
418 	    sc->sc_dev.dv_xname, (u_int)(sc->sc_fbsize >> 20),
419 	    (u_int)sc->sc_fb, (u_int)(sc->sc_regsize >> 20),
420 	    (u_int)sc->sc_regs);
421 #ifdef VOODOOFB_DEBUG
422 	printf("fb: %08lx\n", (ulong)ri->ri_bits);
423 #endif
424 
425 	j = 0;
426 	for (i = 0; i < 256; i++) {
427 		voodoofb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
428 		    rasops_cmap[j + 2]);
429 		j += 3;
430 	}
431 
432 #ifdef VOODOOFB_ENABLE_INTR
433 	/* Interrupt. We don't use it for anything yet */
434 	if (pci_intr_map(pa, &ih)) {
435 		printf("%s: failed to map interrupt\n", sc->sc_dev.dv_xname);
436 		return;
437 	}
438 
439 	intrstr = pci_intr_string(sc->sc_pc, ih);
440 	sc->sc_ih = pci_intr_establish(sc->sc_pc, ih, IPL_NET, voodoofb_intr,
441 	    sc);
442 	if (sc->sc_ih == NULL) {
443 		printf("%s: failed to establish interrupt",
444 		    sc->sc_dev.dv_xname);
445 		if (intrstr != NULL)
446 			printf(" at %s", intrstr);
447 		printf("\n");
448 		return;
449 	}
450 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
451 #endif
452 
453 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
454 	sc->sc_bg = ri->ri_devcmap[bg];
455 	voodoofb_clearscreen(sc);
456 
457 	aa.console = console;
458 	aa.scrdata = &voodoofb_screenlist;
459 	aa.accessops = &voodoofb_accessops;
460 	aa.accesscookie = &sc->vd;
461 
462 	config_found(self, &aa, wsemuldisplaydevprint);
463 	config_found_ia(self, "drm", aux, voodoofb_drm_print);
464 }
465 
466 static int
467 voodoofb_drm_print(void *opaque, const char *pnp)
468 {
469 	if (pnp)
470 		aprint_normal("direct rendering for %s", pnp);
471 
472 	return UNSUPP;
473 }
474 
475 static int
476 voodoofb_drm_unmap(struct voodoofb_softc *sc)
477 {
478 	printf("%s: releasing bus resources\n", sc->sc_dev.dv_xname);
479 
480 	bus_space_unmap(sc->sc_ioregt, sc->sc_ioregh, sc->sc_ioregsize);
481 	bus_space_unmap(sc->sc_regt, sc->sc_regh, sc->sc_regsize);
482 	bus_space_unmap(sc->sc_fbt, sc->sc_fbh, sc->sc_fbsize);
483 
484 	return 0;
485 }
486 
487 static int
488 voodoofb_drm_map(struct voodoofb_softc *sc)
489 {
490 	if (pci_mapreg_map(&sc->sc_pa, 0x14, PCI_MAPREG_TYPE_MEM,
491 	    BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE |
492 	    BUS_SPACE_MAP_LINEAR,
493 	    &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
494 		printf("%s: failed to map the frame buffer.\n",
495 		    sc->sc_dev.dv_xname);
496 	}
497 
498 	/* memory-mapped registers */
499 	if (pci_mapreg_map(&sc->sc_pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
500 	    &sc->sc_regt, &sc->sc_regh, &sc->sc_regs, &sc->sc_regsize)) {
501 		printf("%s: failed to map memory-mapped registers.\n",
502 		    sc->sc_dev.dv_xname);
503 	}
504 
505 	/* IO-mapped registers */
506 	if (pci_mapreg_map(&sc->sc_pa, 0x18, PCI_MAPREG_TYPE_IO, 0,
507 	    &sc->sc_ioregt, &sc->sc_ioregh, &sc->sc_ioreg,
508 	    &sc->sc_ioregsize)) {
509 		printf("%s: failed to map IO-mapped registers.\n",
510 		    sc->sc_dev.dv_xname);
511 	}
512 
513 	voodoofb_init(sc);
514 	/* XXX this should at least be configurable via kernel config */
515 	voodoofb_set_videomode(sc, &videomode_list[VOODOOFB_VIDEOMODE]);
516 
517 	return 0;
518 }
519 
520 static int
521 voodoofb_putpalreg(struct voodoofb_softc *sc, uint8_t index, uint8_t r,
522     uint8_t g, uint8_t b)
523 {
524 	uint32_t color;
525 
526 	sc->sc_cmap_red[index] = r;
527 	sc->sc_cmap_green[index] = g;
528 	sc->sc_cmap_blue[index] = b;
529 
530 	color = (r << 16) | (g << 8) | b;
531 	voodoo3_make_room(sc, 2);
532 	voodoo3_write32(sc, DACADDR, index);
533 	voodoo3_write32(sc, DACDATA, color);
534 
535 	return 0;
536 }
537 
538 static int
539 voodoofb_putcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
540 {
541 	u_char *r, *g, *b;
542 	u_int index = cm->index;
543 	u_int count = cm->count;
544 	int i, error;
545 	u_char rbuf[256], gbuf[256], bbuf[256];
546 
547 #ifdef VOODOOFB_DEBUG
548 	printf("putcmap: %d %d\n",index, count);
549 #endif
550 	if (cm->index >= 256 || cm->count > 256 ||
551 	    (cm->index + cm->count) > 256)
552 		return EINVAL;
553 	error = copyin(cm->red, &rbuf[index], count);
554 	if (error)
555 		return error;
556 	error = copyin(cm->green, &gbuf[index], count);
557 	if (error)
558 		return error;
559 	error = copyin(cm->blue, &bbuf[index], count);
560 	if (error)
561 		return error;
562 
563 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
564 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
565 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
566 
567 	r = &sc->sc_cmap_red[index];
568 	g = &sc->sc_cmap_green[index];
569 	b = &sc->sc_cmap_blue[index];
570 
571 	for (i = 0; i < count; i++) {
572 		voodoofb_putpalreg(sc, index, *r, *g, *b);
573 		index++;
574 		r++, g++, b++;
575 	}
576 	return 0;
577 }
578 
579 static int
580 voodoofb_getcmap(struct voodoofb_softc *sc, struct wsdisplay_cmap *cm)
581 {
582 	u_int index = cm->index;
583 	u_int count = cm->count;
584 	int error;
585 
586 	if (index >= 255 || count > 256 || index + count > 256)
587 		return EINVAL;
588 
589 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
590 	if (error)
591 		return error;
592 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
593 	if (error)
594 		return error;
595 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
596 	if (error)
597 		return error;
598 
599 	return 0;
600 }
601 
602 static int
603 voodoofb_is_console(struct pci_attach_args *pa)
604 {
605 
606 #ifdef HAVE_OPENFIRMWARE
607 	/* check if we're the /chosen console device */
608 	int chosen, stdout, node, us;
609 
610 	us=pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
611 	chosen = OF_finddevice("/chosen");
612 	OF_getprop(chosen, "stdout", &stdout, 4);
613 	node = OF_instance_to_package(stdout);
614 	return(us == node);
615 #else
616 	/* XXX how do we know we're console on i386? */
617 	return 1;
618 #endif
619 }
620 
621 static void
622 voodoofb_clearscreen(struct voodoofb_softc *sc)
623 {
624 	voodoofb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
625 }
626 
627 /*
628  * wsdisplay_emulops
629  */
630 
631 static void
632 voodoofb_cursor(void *cookie, int on, int row, int col)
633 {
634 	struct rasops_info *ri = cookie;
635 	struct vcons_screen *scr = ri->ri_hw;
636 	struct voodoofb_softc *sc = scr->scr_cookie;
637 	int x, y, wi, he;
638 
639 	wi = ri->ri_font->fontwidth;
640 	he = ri->ri_font->fontheight;
641 
642 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
643 		x = ri->ri_ccol * wi + ri->ri_xorigin;
644 		y = ri->ri_crow * he + ri->ri_yorigin;
645 		if (ri->ri_flg & RI_CURSOR) {
646 			voodoofb_rectinvert(sc, x, y, wi, he);
647 			ri->ri_flg &= ~RI_CURSOR;
648 		}
649 		ri->ri_crow = row;
650 		ri->ri_ccol = col;
651 		if (on)
652 		{
653 			x = ri->ri_ccol * wi + ri->ri_xorigin;
654 			y = ri->ri_crow * he + ri->ri_yorigin;
655 			voodoofb_rectinvert(sc, x, y, wi, he);
656 			ri->ri_flg |= RI_CURSOR;
657 		}
658 	} else {
659 		ri->ri_flg &= ~RI_CURSOR;
660 		ri->ri_crow = row;
661 		ri->ri_ccol = col;
662 	}
663 }
664 
665 #if 0
666 int
667 voodoofb_mapchar(void *cookie, int uni, u_int *index)
668 {
669 	return 0;
670 }
671 #endif
672 
673 static void
674 voodoofb_putchar(void *cookie, int row, int col, u_int c, long attr)
675 {
676 	struct rasops_info *ri = cookie;
677 	struct vcons_screen *scr = ri->ri_hw;
678 	struct voodoofb_softc *sc = scr->scr_cookie;
679 
680 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
681 		uint8_t *data;
682 		int fg, bg, uc, i;
683 		int x, y, wi, he;
684 
685 		wi = ri->ri_font->fontwidth;
686 		he = ri->ri_font->fontheight;
687 
688 		if (!CHAR_IN_FONT(c, ri->ri_font))
689 			return;
690 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
691 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
692 		x = ri->ri_xorigin + col * wi;
693 		y = ri->ri_yorigin + row * he;
694 		if (c == 0x20) {
695 			voodoofb_rectfill(sc, x, y, wi, he, bg);
696 		} else {
697 			uc = c-ri->ri_font->firstchar;
698 			data = (uint8_t *)ri->ri_font->data + uc *
699 			    ri->ri_fontscale;
700 				voodoofb_setup_mono(sc, x, y, wi, he, fg, bg);
701 			for (i = 0; i < he; i++) {
702 				voodoofb_feed_line(sc,
703 				    ri->ri_font->stride, data);
704 				data += ri->ri_font->stride;
705 			}
706 		}
707 	}
708 }
709 
710 static void
711 voodoofb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
712 {
713 	struct rasops_info *ri = cookie;
714 	struct vcons_screen *scr = ri->ri_hw;
715 	struct voodoofb_softc *sc = scr->scr_cookie;
716 	int32_t xs, xd, y, width, height;
717 
718 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
719 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
720 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
721 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
722 		width = ri->ri_font->fontwidth * ncols;
723 		height = ri->ri_font->fontheight;
724 		voodoofb_bitblt(sc, xs, y, xd, y, width, height);
725 	}
726 }
727 
728 static void
729 voodoofb_erasecols(void *cookie, int row, int startcol, int ncols,
730     long fillattr)
731 {
732 	struct rasops_info *ri = cookie;
733 	struct vcons_screen *scr = ri->ri_hw;
734 	struct voodoofb_softc *sc = scr->scr_cookie;
735 	int32_t x, y, width, height, fg, bg, ul;
736 
737 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
738 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
739 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
740 		width = ri->ri_font->fontwidth * ncols;
741 		height = ri->ri_font->fontheight;
742 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
743 
744 		voodoofb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
745 	}
746 }
747 
748 static void
749 voodoofb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
750 {
751 	struct rasops_info *ri = cookie;
752 	struct vcons_screen *scr = ri->ri_hw;
753 	struct voodoofb_softc *sc = scr->scr_cookie;
754 	int32_t x, ys, yd, width, height;
755 
756 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
757 		x = ri->ri_xorigin;
758 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
759 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
760 		width = ri->ri_emuwidth;
761 		height = ri->ri_font->fontheight * nrows;
762 		voodoofb_bitblt(sc, x, ys, x, yd, width, height);
763 	}
764 }
765 
766 static void
767 voodoofb_eraserows(void *cookie, int row, int nrows, long fillattr)
768 {
769 	struct rasops_info *ri = cookie;
770 	struct vcons_screen *scr = ri->ri_hw;
771 	struct voodoofb_softc *sc = scr->scr_cookie;
772 	int32_t x, y, width, height, fg, bg, ul;
773 
774 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
775 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
776 		if ((row == 0) && (nrows == ri->ri_rows)) {
777 			/* clear the whole screen */
778 			voodoofb_rectfill(sc, 0, 0, ri->ri_width,
779 			    ri->ri_height, ri->ri_devcmap[bg]);
780 		} else {
781 			x = ri->ri_xorigin;
782 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
783 			width = ri->ri_emuwidth;
784 			height = ri->ri_font->fontheight * nrows;
785 			voodoofb_rectfill(sc, x, y, width, height,
786 			    ri->ri_devcmap[bg]);
787 		}
788 	}
789 }
790 
791 static void
792 voodoofb_bitblt(struct voodoofb_softc *sc, int xs, int ys, int xd, int yd, int width, int height)
793 {
794 	uint32_t fmt, blitcmd;
795 
796 	fmt = sc->linebytes | ((sc->bits_per_pixel +
797 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
798 	blitcmd = COMMAND_2D_S2S_BITBLT | (ROP_COPY << 24);
799 
800 	if (xs <= xd) {
801 	        blitcmd |= BIT(14);
802 		xs += (width - 1);
803 		xd += (width - 1);
804 	}
805 	if (ys <= yd) {
806 		blitcmd |= BIT(15);
807 		ys += (height - 1);
808 		yd += (height - 1);
809 	}
810 	voodoo3_make_room(sc, 6);
811 
812 	voodoo3_write32(sc, SRCFORMAT, fmt);
813 	voodoo3_write32(sc, DSTFORMAT, fmt);
814 	voodoo3_write32(sc, DSTSIZE,   width | (height << 16));
815 	voodoo3_write32(sc, DSTXY,     xd | (yd << 16));
816 	voodoo3_write32(sc, SRCXY, xs | (ys << 16));
817 	voodoo3_write32(sc, COMMAND_2D, blitcmd | SST_2D_GO);
818 }
819 
820 static void
821 voodoofb_rectfill(struct voodoofb_softc *sc, int x, int y, int width,
822     int height, int colour)
823 {
824 	uint32_t fmt, col;
825 
826 	col = (colour << 24) | (colour << 16) | (colour << 8) | colour;
827 	fmt = sc->linebytes | ((sc->bits_per_pixel +
828 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
829 
830 	voodoo3_make_room(sc, 6);
831 	voodoo3_write32(sc, DSTFORMAT, fmt);
832 	voodoo3_write32(sc, COLORFORE, colour);
833 	voodoo3_write32(sc, COLORBACK, colour);
834 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_FILLRECT | (ROP_COPY << 24));
835 	voodoo3_write32(sc, DSTSIZE,    width | (height << 16));
836 	voodoo3_write32(sc, LAUNCH_2D,  x | (y << 16));
837 }
838 
839 static void
840 voodoofb_rectinvert(struct voodoofb_softc *sc, int x, int y, int width,
841     int height)
842 {
843 	uint32_t fmt;
844 
845 	fmt = sc->linebytes | ((sc->bits_per_pixel +
846 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
847 
848 	voodoo3_make_room(sc, 6);
849 	voodoo3_write32(sc, DSTFORMAT,	fmt);
850 	voodoo3_write32(sc, COMMAND_2D,	COMMAND_2D_FILLRECT |
851 	    (ROP_INVERT << 24));
852 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
853 	voodoo3_write32(sc, DSTXY,	x | (y << 16));
854 	voodoo3_write32(sc, LAUNCH_2D,	x | (y << 16));
855 }
856 
857 static void
858 voodoofb_setup_mono(struct voodoofb_softc *sc, int xd, int yd, int width, int height, uint32_t fg,
859 					uint32_t bg)
860 {
861 	uint32_t dfmt, sfmt = sc->linebytes;
862 
863 	dfmt = sc->linebytes | ((sc->bits_per_pixel +
864 	    ((sc->bits_per_pixel == 8) ? 0 : 8)) << 13);
865 
866 	voodoo3_make_room(sc, 9);
867 	voodoo3_write32(sc, SRCFORMAT,	sfmt);
868 	voodoo3_write32(sc, DSTFORMAT,	dfmt);
869 	voodoo3_write32(sc, COLORFORE,	fg);
870 	voodoo3_write32(sc, COLORBACK,	bg);
871 	voodoo3_write32(sc, DSTSIZE,	width | (height << 16));
872 	voodoo3_write32(sc, DSTXY,	xd | (yd << 16));
873 	voodoo3_write32(sc, SRCXY,	0);
874 	voodoo3_write32(sc, COMMAND_2D, COMMAND_2D_H2S_BITBLT |
875 	    (ROP_COPY << 24) | SST_2D_GO);
876 
877 	/* now feed the data into the chip */
878 }
879 
880 static void
881 voodoofb_feed_line(struct voodoofb_softc *sc, int count, uint8_t *data)
882 {
883 	int i;
884 	uint32_t latch = 0, bork;
885 	int shift = 0;
886 
887 	voodoo3_make_room(sc, count);
888 	for (i = 0; i < count; i++) {
889 		bork = data[i];
890 		latch |= (bork << shift);
891 		if (shift == 24) {
892 			voodoo3_write32(sc, LAUNCH_2D, latch);
893 			latch = 0;
894 			shift = 0;
895 		} else
896 			shift += 8;
897 		}
898 	if (shift != 24)
899 		voodoo3_write32(sc, LAUNCH_2D, latch);
900 }
901 
902 #ifdef VOODOOFB_DEBUG
903 static void
904 voodoofb_showpal(struct voodoofb_softc *sc)
905 {
906 	int i, x = 0;
907 
908 	for (i = 0; i < 16; i++) {
909 		voodoofb_rectfill(sc, x, 0, 64, 64, i);
910 		x += 64;
911 	}
912 }
913 #endif
914 
915 #if 0
916 static int
917 voodoofb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
918 {
919 
920 	return 0;
921 }
922 #endif
923 
924 /*
925  * wsdisplay_accessops
926  */
927 
928 static int
929 voodoofb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
930 	struct lwp *l)
931 {
932 	struct vcons_data *vd = v;
933 	struct voodoofb_softc *sc = vd->cookie;
934 	struct wsdisplay_fbinfo *wdf;
935 	struct vcons_screen *ms = vd->active;
936 
937 	switch (cmd) {
938 		case WSDISPLAYIO_GTYPE:
939 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
940 			return 0;
941 
942 		case WSDISPLAYIO_GINFO:
943 			wdf = (void *)data;
944 			wdf->height = ms->scr_ri.ri_height;
945 			wdf->width = ms->scr_ri.ri_width;
946 			wdf->depth = ms->scr_ri.ri_depth;
947 			wdf->cmsize = 256;
948 			return 0;
949 
950 		case WSDISPLAYIO_GETCMAP:
951 			return voodoofb_getcmap(sc,
952 			    (struct wsdisplay_cmap *)data);
953 
954 		case WSDISPLAYIO_PUTCMAP:
955 			return voodoofb_putcmap(sc,
956 			    (struct wsdisplay_cmap *)data);
957 
958 		/* PCI config read/write passthrough. */
959 		case PCI_IOC_CFGREAD:
960 		case PCI_IOC_CFGWRITE:
961 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
962 			    cmd, data, flag, l));
963 
964 		case WSDISPLAYIO_SMODE:
965 			{
966 				int new_mode = *(int*)data;
967 				if (new_mode != sc->sc_mode)
968 				{
969 					sc->sc_mode = new_mode;
970 					if(new_mode == WSDISPLAYIO_MODE_EMUL)
971 					{
972 						voodoofb_drm_map(sc);
973 						int i;
974 
975 						/* restore the palette */
976 						for (i = 0; i < 256; i++) {
977 							voodoofb_putpalreg(sc,
978 							   i,
979 							   sc->sc_cmap_red[i],
980 							   sc->sc_cmap_green[i],
981 							   sc->sc_cmap_blue[i]);
982 						}
983 						vcons_redraw_screen(ms);
984 					} else
985 						voodoofb_drm_unmap(sc);
986 				}
987 			}
988 			return 0;
989 	}
990 	return EPASSTHROUGH;
991 }
992 
993 static paddr_t
994 voodoofb_mmap(void *v, void *vs, off_t offset, int prot)
995 {
996 	struct vcons_data *vd = v;
997 	struct voodoofb_softc *sc = vd->cookie;
998 	paddr_t pa;
999 
1000 	/* 'regular' framebuffer mmap()ing */
1001 	if (offset < sc->sc_fbsize) {
1002 		pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
1003 		    BUS_SPACE_MAP_LINEAR);
1004 		return pa;
1005 	}
1006 
1007 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
1008 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1009 		    BUS_SPACE_MAP_LINEAR);
1010 		return pa;
1011 	}
1012 
1013 	if ((offset >= sc->sc_regs) && (offset < (sc->sc_regs +
1014 	    sc->sc_regsize))) {
1015 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1016 		    BUS_SPACE_MAP_LINEAR);
1017 		return pa;
1018 	}
1019 
1020 	/* allow mapping of IO space */
1021 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
1022 		pa = bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
1023 		    BUS_SPACE_MAP_LINEAR);
1024 		return pa;
1025 	}
1026 #ifdef OFB_ALLOW_OTHERS
1027 	if (offset >= 0x80000000) {
1028 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1029 		    BUS_SPACE_MAP_LINEAR);
1030 		return pa;
1031 	}
1032 #endif
1033 	return -1;
1034 }
1035 
1036 static void
1037 voodoofb_init_screen(void *cookie, struct vcons_screen *scr,
1038     int existing, long *defattr)
1039 {
1040 	struct voodoofb_softc *sc = cookie;
1041 	struct rasops_info *ri = &scr->scr_ri;
1042 
1043 	ri->ri_depth = sc->bits_per_pixel;
1044 	ri->ri_width = sc->width;
1045 	ri->ri_height = sc->height;
1046 	ri->ri_stride = sc->width;
1047 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
1048 
1049 	ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
1050 
1051 #ifdef VOODOOFB_DEBUG
1052 	printf("addr: %08lx\n", (ulong)ri->ri_bits);
1053 #endif
1054 	if (existing) {
1055 		ri->ri_flg |= RI_CLEAR;
1056 	}
1057 
1058 	rasops_init(ri, sc->height/8, sc->width/8);
1059 	ri->ri_caps = WSSCREEN_WSCOLORS;
1060 
1061 	rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
1062 		    sc->width / ri->ri_font->fontwidth);
1063 
1064 	ri->ri_hw = scr;
1065 	ri->ri_ops.copyrows = voodoofb_copyrows;
1066 	ri->ri_ops.copycols = voodoofb_copycols;
1067 	ri->ri_ops.eraserows = voodoofb_eraserows;
1068 	ri->ri_ops.erasecols = voodoofb_erasecols;
1069 	ri->ri_ops.cursor = voodoofb_cursor;
1070 	ri->ri_ops.putchar = voodoofb_putchar;
1071 }
1072 
1073 #if 0
1074 int
1075 voodoofb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
1076 {
1077 
1078 	return 0;
1079 }
1080 #endif
1081 
1082 #ifdef VOODOOFB_ENABLE_INTR
1083 static int
1084 voodoofb_intr(void *arg)
1085 {
1086 	struct voodoofb_softc *sc = arg;
1087 
1088 	voodoo3_write32(sc, V3_STATUS, 0);	/* clear interrupts */
1089 	return 1;
1090 }
1091 #endif
1092 
1093 /* video mode stuff */
1094 
1095 #define REFFREQ 14318	/* .18 */
1096 
1097 #define ABS(a) ((a < 0) ? -a : a)
1098 
1099 static int
1100 voodoofb_calc_pll(int freq, int *f_out, int isBanshee)
1101 {
1102 	int m, n, k, best_m, best_n, best_k, f_cur, best_error;
1103 	int minm, maxm;
1104 
1105 	best_error = freq;
1106 	best_n = best_m = best_k = 0;
1107 
1108 	if (isBanshee) {
1109 		minm = 24;
1110 		maxm = 24;
1111 	} else {
1112 		minm = 1;
1113 		maxm = 57;
1114 		/* This used to be 64, alas it seems the last 8 (funny that ?)
1115 		 * values cause jittering at lower resolutions. I've not done
1116 		 * any calculations to what the adjustment affects clock ranges,
1117 		 * but I can still run at 1600x1200@75Hz */
1118 	}
1119 	for (n = 1; n < 256; n++) {
1120 		f_cur = REFFREQ * (n + 2);
1121 		if (f_cur < freq) {
1122 			f_cur = f_cur / 3;
1123 			if (freq - f_cur < best_error) {
1124 				best_error = freq - f_cur;
1125 				best_n = n;
1126 				best_m = 1;
1127 				best_k = 0;
1128 				continue;
1129       			}
1130 		}
1131 		for (m = minm; m < maxm; m++) {
1132 			for (k = 0; k < 4; k++) {
1133 				f_cur = REFFREQ * (n + 2) / (m + 2) / (1 << k);
1134 				if (ABS(f_cur - freq) < best_error) {
1135 					best_error = ABS(f_cur - freq);
1136 					best_n = n;
1137 					best_m = m;
1138 					best_k = k;
1139 				}
1140 			}
1141 		}
1142 	}
1143 	n = best_n;
1144 	m = best_m;
1145 	k = best_k;
1146 	*f_out = REFFREQ * (n + 2) / (m + 2) / (1 << k);
1147 	return ( n << 8) | (m << 2) | k;
1148 }
1149 
1150 static void
1151 voodoofb_setup_monitor(struct voodoofb_softc *sc, const struct videomode *vm)
1152 {
1153 	struct voodoo_regs mod;
1154 	struct voodoo_regs *mode;
1155 	uint32_t horizontal_display_end, horizontal_sync_start,
1156 		horizontal_sync_end, horizontal_total,
1157 		horizontal_blanking_start, horizontal_blanking_end;
1158 
1159 	uint32_t vertical_display_enable_end, vertical_sync_start,
1160 		vertical_sync_end, vertical_total, vertical_blanking_start,
1161 		vertical_blanking_end;
1162 
1163 	uint32_t wd; // CRTC offset
1164 
1165 	int i;
1166 
1167 	uint8_t misc;
1168 
1169 	memset(&mod, 0, sizeof(mode));
1170 
1171 	mode = &mod;
1172 
1173 	wd = (vm->hdisplay >> 3) - 1;
1174 	horizontal_display_end	= (vm->hdisplay >> 3) - 1;
1175 	horizontal_sync_start	= (vm->hsync_start >> 3) - 1;
1176 	horizontal_sync_end	= (vm->hsync_end >> 3) - 1;
1177 	horizontal_total  	= (vm->htotal   >> 3) - 1;
1178 	horizontal_blanking_start = horizontal_display_end;
1179 	horizontal_blanking_end = horizontal_total;
1180 
1181 	vertical_display_enable_end  = vm->vdisplay - 1;
1182 	vertical_sync_start  	= vm->vsync_start;	// - 1;
1183 	vertical_sync_end	= vm->vsync_end;	// - 1;
1184 	vertical_total		= vm->vtotal - 2;
1185 	vertical_blanking_start	= vertical_display_enable_end;
1186 	vertical_blanking_end	= vertical_total;
1187 
1188 	misc = 0x0f |
1189 	    (vm->hdisplay < 400 ? 0xa0 :
1190 		vm->hdisplay < 480 ? 0x60 :
1191 		vm->hdisplay < 768 ? 0xe0 : 0x20);
1192 
1193 	mode->vr_seq[0] = 3;
1194 	mode->vr_seq[1] = 1;
1195 	mode->vr_seq[2] = 8;
1196 	mode->vr_seq[3] = 0;
1197 	mode->vr_seq[4] = 6;
1198 
1199 	/* crtc regs start */
1200 	mode->vr_crtc[0] = horizontal_total - 4;
1201 	mode->vr_crtc[1] = horizontal_display_end;
1202 	mode->vr_crtc[2] = horizontal_blanking_start;
1203 	mode->vr_crtc[3] = 0x80 | (horizontal_blanking_end & 0x1f);
1204 	mode->vr_crtc[4] = horizontal_sync_start;
1205 
1206 	mode->vr_crtc[5] = ((horizontal_blanking_end & 0x20) << 2) |
1207 	    (horizontal_sync_end & 0x1f);
1208 	mode->vr_crtc[6] = vertical_total;
1209 	mode->vr_crtc[7] = ((vertical_sync_start & 0x200) >> 2) |
1210 	    ((vertical_display_enable_end & 0x200) >> 3) |
1211 	    ((vertical_total & 0x200) >> 4) |
1212 	    0x10 |
1213 	    ((vertical_blanking_start & 0x100) >> 5) |
1214 	    ((vertical_sync_start  & 0x100) >> 6) |
1215 	    ((vertical_display_enable_end  & 0x100) >> 7) |
1216 	    ((vertical_total  & 0x100) >> 8);
1217 
1218 	mode->vr_crtc[8] = 0;
1219 	mode->vr_crtc[9] = 0x40 |
1220 	    ((vertical_blanking_start & 0x200) >> 4);
1221 
1222 	mode->vr_crtc[10] = 0;
1223 	mode->vr_crtc[11] = 0;
1224 	mode->vr_crtc[12] = 0;
1225 	mode->vr_crtc[13] = 0;
1226 	mode->vr_crtc[14] = 0;
1227 	mode->vr_crtc[15] = 0;
1228 
1229 	mode->vr_crtc[16] = vertical_sync_start;
1230 	mode->vr_crtc[17] = (vertical_sync_end & 0x0f) | 0x20;
1231 	mode->vr_crtc[18] = vertical_display_enable_end;
1232 	mode->vr_crtc[19] = wd; // CRTC offset
1233 	mode->vr_crtc[20] = 0;
1234 	mode->vr_crtc[21] = vertical_blanking_start;
1235 	mode->vr_crtc[22] = vertical_blanking_end + 1;
1236 	mode->vr_crtc[23] = 128;
1237 	mode->vr_crtc[24] = 255;
1238 
1239 	/* attr regs start */
1240 	mode->vr_attr[0] = 0;
1241 	mode->vr_attr[1] = 0;
1242 	mode->vr_attr[2] = 0;
1243 	mode->vr_attr[3] = 0;
1244 	mode->vr_attr[4] = 0;
1245 	mode->vr_attr[5] = 0;
1246 	mode->vr_attr[6] = 0;
1247 	mode->vr_attr[7] = 0;
1248 	mode->vr_attr[8] = 0;
1249 	mode->vr_attr[9] = 0;
1250 	mode->vr_attr[10] = 0;
1251 	mode->vr_attr[11] = 0;
1252 	mode->vr_attr[12] = 0;
1253 	mode->vr_attr[13] = 0;
1254 	mode->vr_attr[14] = 0;
1255 	mode->vr_attr[15] = 0;
1256 	mode->vr_attr[16] = 1;
1257 	mode->vr_attr[17] = 0;
1258 	mode->vr_attr[18] = 15;
1259 	mode->vr_attr[19] = 0;
1260 	/* attr regs end */
1261 
1262 	/* graph regs start */
1263 	mode->vr_graph[0] = 159;
1264 	mode->vr_graph[1] = 127;
1265 	mode->vr_graph[2] = 127;
1266 	mode->vr_graph[3] = 131;
1267 	mode->vr_graph[4] = 130;
1268 	mode->vr_graph[5] = 142;
1269 	mode->vr_graph[6] = 30;
1270 	mode->vr_graph[7] = 245;
1271 	mode->vr_graph[8] = 0;
1272 
1273 	vga_outb(sc, MISC_W, misc | 0x01);
1274 
1275 	for(i = 0; i < 5; i++)
1276 		voodoo3_write_seq(sc, i, mode->vr_seq[i]);
1277 	for (i = 0; i < 0x19; i ++)
1278         	voodoo3_write_crtc(sc, i, mode->vr_crtc[i]);
1279 	for (i = 0; i < 0x14; i ++)
1280         	voodoo3_write_attr(sc, i, mode->vr_attr[i]);
1281 	for (i = 0; i < 0x09; i ++)
1282         	voodoo3_write_gra(sc, i, mode->vr_graph[i]);
1283 }
1284 
1285 static void
1286 voodoofb_set_videomode(struct voodoofb_softc *sc,
1287     const struct videomode *vm)
1288 {
1289 	uint32_t miscinit0 = 0;
1290 	int vidpll, fout;
1291 	uint32_t vp, vidproc = VIDPROCDEFAULT;
1292 	uint32_t bpp = 1;	/* for now */
1293 	uint32_t bytes_per_row = vm->hdisplay * bpp;
1294 
1295 	sc->bits_per_pixel = bpp << 3;
1296 	sc->width = vm->hdisplay;
1297 	sc->height = vm->vdisplay;
1298 	sc->linebytes = bytes_per_row;
1299 
1300 	voodoofb_setup_monitor(sc, vm);
1301 	vp = voodoo3_read32(sc, VIDPROCCFG);
1302 
1303 	vidproc &= ~(0x1c0000); /* clear bits 18 to 20, bpp in vidproccfg */
1304 	/* enable bits 18 to 20 to the required bpp */
1305 	vidproc |= ((bpp - 1) << VIDCFG_PIXFMT_SHIFT);
1306 
1307 	vidpll = voodoofb_calc_pll(vm->dot_clock, &fout, 0);
1308 
1309 #ifdef VOODOOFB_DEBUG
1310 	printf("old vidproc: %08x\n", vp);
1311 	printf("pll: %08x %d\n", vidpll, fout);
1312 #endif
1313 	/* bit 10 of vidproccfg, is enabled or disabled as needed */
1314 	switch (bpp) {
1315 		case 1:
1316 			/*
1317 			 * bit 10 off for palettized modes only, off means
1318 			 * palette is used
1319 			 */
1320 			vidproc &= ~(1 << 10);
1321 			break;
1322 #if 0
1323 		case 2:
1324 			#if __POWERPC__
1325 				miscinit0 = 0xc0000000;
1326 			#endif
1327 			/* bypass palette for 16bit modes */
1328 			vidproc |= (1 << 10);
1329 			break;
1330 		case 4:
1331 			#if __POWERPC__
1332 				miscinit0 = 0x40000000;
1333 			#endif
1334 			vidproc |= (1 << 10); /* Same for 32bit modes */
1335 			break;
1336 #endif
1337 		default:
1338 			printf("We support only 8 bit for now\n");
1339 			return;
1340 	}
1341 
1342 	voodoofb_wait_idle(sc);
1343 
1344 	voodoo3_write32(sc, MISCINIT1, voodoo3_read32(sc, MISCINIT1) | 0x01);
1345 
1346 	voodoo3_make_room(sc, 4);
1347 	voodoo3_write32(sc, VGAINIT0, 4928);
1348 	voodoo3_write32(sc, DACMODE, 0);
1349 	voodoo3_write32(sc, VIDDESKSTRIDE, bytes_per_row);
1350 	voodoo3_write32(sc, PLLCTRL0, vidpll);
1351 
1352 	voodoo3_make_room(sc, 5);
1353 	voodoo3_write32(sc, VIDSCREENSIZE, sc->width | (sc->height << 12));
1354 	voodoo3_write32(sc, VIDDESKSTART,  1024);
1355 
1356 	vidproc &= ~VIDCFG_HWCURSOR_ENABLE;
1357 	voodoo3_write32(sc, VIDPROCCFG, vidproc);
1358 
1359 	voodoo3_write32(sc, VGAINIT1, 0);
1360 	voodoo3_write32(sc, MISCINIT0, miscinit0);
1361 #ifdef VOODOOFB_DEBUG
1362 	printf("vidproc: %08x\n", vidproc);
1363 #endif
1364 	voodoo3_make_room(sc, 8);
1365 	voodoo3_write32(sc, SRCBASE, 0);
1366 	voodoo3_write32(sc, DSTBASE, 0);
1367 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
1368   	voodoo3_write32(sc, CLIP0MIN,        0);
1369   	voodoo3_write32(sc, CLIP0MAX,        0x0fff0fff);
1370   	voodoo3_write32(sc, CLIP1MIN,        0);
1371   	voodoo3_write32(sc, CLIP1MAX,        0x0fff0fff);
1372 	voodoo3_write32(sc, SRCXY, 0);
1373 	voodoofb_wait_idle(sc);
1374 	printf("%s: switched to %dx%d, %d bit\n", sc->sc_dev.dv_xname,
1375 	    sc->width, sc->height, sc->bits_per_pixel);
1376 }
1377 
1378 static void
1379 voodoofb_init(struct voodoofb_softc *sc)
1380 {
1381 	/* XXX */
1382 	uint32_t vgainit0 = 0;
1383 	uint32_t vidcfg = 0;
1384 
1385 #ifdef VOODOOFB_DEBUG
1386 	printf("initializing engine...");
1387 #endif
1388 	vgainit0 = voodoo3_read32(sc, VGAINIT0);
1389 #ifdef VOODOOFB_DEBUG
1390 	printf("vga: %08x", vgainit0);
1391 #endif
1392 	vgainit0 |=
1393 	    VGAINIT0_8BIT_DAC     |
1394 	    VGAINIT0_EXT_ENABLE   |
1395 	    VGAINIT0_WAKEUP_3C3   |
1396 	    VGAINIT0_ALT_READBACK |
1397 	    VGAINIT0_EXTSHIFTOUT;
1398 
1399 	vidcfg = voodoo3_read32(sc, VIDPROCCFG);
1400 #ifdef VOODOOFB_DEBUG
1401 	printf(" vidcfg: %08x\n", vidcfg);
1402 #endif
1403 	vidcfg |=
1404 	    VIDCFG_VIDPROC_ENABLE |
1405 	    VIDCFG_DESK_ENABLE;
1406 	vidcfg &= ~VIDCFG_HWCURSOR_ENABLE;
1407 
1408 	voodoo3_make_room(sc, 2);
1409 
1410 	voodoo3_write32(sc, VGAINIT0, vgainit0);
1411 	voodoo3_write32(sc, VIDPROCCFG, vidcfg);
1412 
1413 	voodoo3_make_room(sc, 8);
1414 	voodoo3_write32(sc, SRCBASE, 0);
1415 	voodoo3_write32(sc, DSTBASE, 0);
1416 	voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
1417   	voodoo3_write32(sc, CLIP0MIN,        0);
1418   	voodoo3_write32(sc, CLIP0MAX,        0x1fff1fff);
1419   	voodoo3_write32(sc, CLIP1MIN,        0);
1420   	voodoo3_write32(sc, CLIP1MAX,        0x1fff1fff);
1421 	voodoo3_write32(sc, SRCXY, 0);
1422 
1423 	voodoofb_wait_idle(sc);
1424 }
1425