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