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