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