voodoofb.c revision 1.3 1 /* $NetBSD: voodoofb.c,v 1.3 2006/04/12 23:11:56 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 * 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.3 2006/04/12 23:11:56 macallan Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
44 #include <sys/callout.h>
45
46 #include <uvm/uvm_extern.h>
47
48 #if defined(macppc) || defined (sparc64) || defined(ofppc)
49 #define HAVE_OPENFIRMWARE
50 #endif
51
52 #ifdef HAVE_OPENFIRMWARE
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_pci.h>
55 #endif
56
57 #include <dev/videomode/videomode.h>
58
59 #include <dev/pci/pcivar.h>
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcidevs.h>
62 #include <dev/pci/pciio.h>
63 #include <dev/pci/voodoofbreg.h>
64
65 #include <dev/wscons/wsdisplayvar.h>
66 #include <dev/wscons/wsconsio.h>
67 #include <dev/wsfont/wsfont.h>
68 #include <dev/rasops/rasops.h>
69 #include <dev/wscons/wsdisplay_vconsvar.h>
70
71 #include "opt_wsemul.h"
72
73 struct voodoofb_softc {
74 struct device sc_dev;
75 pci_chipset_tag_t sc_pc;
76 pcitag_t sc_pcitag;
77
78 bus_space_tag_t sc_memt;
79 bus_space_tag_t sc_iot;
80 bus_space_handle_t sc_memh;
81
82 bus_space_tag_t sc_regt;
83 bus_space_tag_t sc_fbt;
84 bus_space_tag_t sc_ioregt;
85 bus_space_handle_t sc_regh;
86 bus_space_handle_t sc_fbh;
87 bus_space_handle_t sc_ioregh;
88 bus_addr_t sc_regs, sc_fb, sc_ioreg;
89 bus_size_t sc_regsize, sc_fbsize, sc_ioregsize;
90
91 void *sc_ih;
92
93 size_t memsize;
94 int memtype;
95
96 int bits_per_pixel;
97 int width, height, linebytes;
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(struct device *, struct cfdata *, void *);
122 static void voodoofb_attach(struct device *, struct device *, void *);
123
124 CFATTACH_DECL(voodoofb, sizeof(struct voodoofb_softc), voodoofb_match,
125 voodoofb_attach, NULL, NULL);
126
127 static int voodoofb_is_console(struct pci_attach_args *);
128 static void voodoofb_init(struct voodoofb_softc *);
129
130 static void voodoofb_cursor(void *, int, int, int);
131 static void voodoofb_putchar(void *, int, int, u_int, long);
132 static void voodoofb_copycols(void *, int, int, int, int);
133 static void voodoofb_erasecols(void *, int, int, int, long);
134 static void voodoofb_copyrows(void *, int, int, int);
135 static void voodoofb_eraserows(void *, int, int, long);
136
137 #if 0
138 static int voodoofb_allocattr(void *, int, int, int, long *);
139 static void voodoofb_scroll(void *, void *, int);
140 static int voodoofb_load_font(void *, void *, struct wsdisplay_font *);
141 #endif
142
143 static int voodoofb_putcmap(struct voodoofb_softc *,
144 struct wsdisplay_cmap *);
145 static int voodoofb_getcmap(struct voodoofb_softc *,
146 struct wsdisplay_cmap *);
147 static int voodoofb_putpalreg(struct voodoofb_softc *, uint8_t, uint8_t,
148 uint8_t, uint8_t);
149 static void voodoofb_bitblt(struct voodoofb_softc *, int, int, int, int,
150 int, int);
151 static void voodoofb_rectfill(struct voodoofb_softc *, int, int, int, int,
152 int);
153 static void voodoofb_rectinvert(struct voodoofb_softc *, int, int, int,
154 int);
155 static void voodoofb_setup_mono(struct voodoofb_softc *, int, int, int,
156 int, uint32_t, uint32_t);
157 static void voodoofb_feed_line(struct voodoofb_softc *, int, uint8_t *);
158
159 #ifdef VOODOOFB_DEBUG
160 static void voodoofb_showpal(struct voodoofb_softc *);
161 #endif
162
163 static void voodoofb_wait_idle(struct voodoofb_softc *);
164
165 static int voodoofb_intr(void *);
166
167 static void voodoofb_set_videomode(struct voodoofb_softc *,
168 const struct videomode *);
169
170 struct wsscreen_descr voodoofb_defaultscreen = {
171 "default",
172 0, 0,
173 NULL,
174 8, 16,
175 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
176 };
177
178 const struct wsscreen_descr *_voodoofb_scrlist[] = {
179 &voodoofb_defaultscreen,
180 /* XXX other formats, graphics screen? */
181 };
182
183 struct wsscreen_list voodoofb_screenlist = {
184 sizeof(_voodoofb_scrlist) / sizeof(struct wsscreen_descr *), _voodoofb_scrlist
185 };
186
187 static int voodoofb_ioctl(void *, void *, u_long, caddr_t, int,
188 struct lwp *);
189 static paddr_t voodoofb_mmap(void *, void *, off_t, int);
190
191 static void voodoofb_clearscreen(struct voodoofb_softc *);
192 static void voodoofb_init_screen(void *, struct vcons_screen *, int,
193 long *);
194
195
196 struct wsdisplay_accessops voodoofb_accessops = {
197 voodoofb_ioctl,
198 voodoofb_mmap,
199 NULL, /* load_font */
200 NULL, /* polls */
201 NULL, /* getwschar */
202 NULL, /* putwschar */
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 }
913 return EPASSTHROUGH;
914 }
915
916 static paddr_t
917 voodoofb_mmap(void *v, void *vs, off_t offset, int prot)
918 {
919 struct vcons_data *vd = v;
920 struct voodoofb_softc *sc = vd->cookie;
921 paddr_t pa;
922
923 /* 'regular' framebuffer mmap()ing */
924 if (offset < sc->sc_fbsize) {
925 pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
926 BUS_SPACE_MAP_LINEAR);
927 return pa;
928 }
929
930 if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
931 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
932 BUS_SPACE_MAP_LINEAR);
933 return pa;
934 }
935
936 if ((offset >= sc->sc_regs) && (offset < (sc->sc_regs +
937 sc->sc_regsize))) {
938 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
939 BUS_SPACE_MAP_LINEAR);
940 return pa;
941 }
942
943 /* allow mapping of IO space */
944 if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
945 pa = bus_space_mmap(sc->sc_iot, offset-0xf2000000, 0, prot,
946 BUS_SPACE_MAP_LINEAR);
947 return pa;
948 }
949 #ifdef OFB_ALLOW_OTHERS
950 if (offset >= 0x80000000) {
951 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
952 BUS_SPACE_MAP_LINEAR);
953 return pa;
954 }
955 #endif
956 return -1;
957 }
958
959 static void
960 voodoofb_init_screen(void *cookie, struct vcons_screen *scr,
961 int existing, long *defattr)
962 {
963 struct voodoofb_softc *sc = cookie;
964 struct rasops_info *ri = &scr->scr_ri;
965
966 ri->ri_depth = sc->bits_per_pixel;
967 ri->ri_width = sc->width;
968 ri->ri_height = sc->height;
969 ri->ri_stride = sc->width;
970 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
971
972 ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
973
974 #ifdef VOODOOFB_DEBUG
975 printf("addr: %08lx\n", (ulong)ri->ri_bits);
976 #endif
977 if (existing) {
978 ri->ri_flg |= RI_CLEAR;
979 }
980
981 rasops_init(ri, sc->height/8, sc->width/8);
982 ri->ri_caps = WSSCREEN_WSCOLORS;
983
984 rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
985 sc->width / ri->ri_font->fontwidth);
986
987 ri->ri_hw = scr;
988 ri->ri_ops.copyrows = voodoofb_copyrows;
989 ri->ri_ops.copycols = voodoofb_copycols;
990 ri->ri_ops.eraserows = voodoofb_eraserows;
991 ri->ri_ops.erasecols = voodoofb_erasecols;
992 ri->ri_ops.cursor = voodoofb_cursor;
993 ri->ri_ops.putchar = voodoofb_putchar;
994 }
995
996 #if 0
997 int
998 voodoofb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
999 {
1000
1001 return 0;
1002 }
1003 #endif
1004
1005 static int
1006 voodoofb_intr(void *arg)
1007 {
1008 struct voodoofb_softc *sc = arg;
1009
1010 voodoo3_write32(sc, V3_STATUS, 0); /* clear interrupts */
1011 return 1;
1012 }
1013
1014 /* video mode stuff */
1015
1016 #define REFFREQ 14318 /* .18 */
1017
1018 #define ABS(a) ((a < 0) ? -a : a)
1019
1020 static int
1021 voodoofb_calc_pll(int freq, int *f_out, int isBanshee)
1022 {
1023 int m, n, k, best_m, best_n, best_k, f_cur, best_error;
1024 int minm, maxm;
1025
1026 best_error = freq;
1027 best_n = best_m = best_k = 0;
1028
1029 if (isBanshee) {
1030 minm = 24;
1031 maxm = 24;
1032 } else {
1033 minm = 1;
1034 maxm = 57;
1035 /* This used to be 64, alas it seems the last 8 (funny that ?)
1036 * values cause jittering at lower resolutions. I've not done
1037 * any calculations to what the adjustment affects clock ranges,
1038 * but I can still run at 1600x1200@75Hz */
1039 }
1040 for (n = 1; n < 256; n++) {
1041 f_cur = REFFREQ * (n + 2);
1042 if (f_cur < freq) {
1043 f_cur = f_cur / 3;
1044 if (freq - f_cur < best_error) {
1045 best_error = freq - f_cur;
1046 best_n = n;
1047 best_m = 1;
1048 best_k = 0;
1049 continue;
1050 }
1051 }
1052 for (m = minm; m < maxm; m++) {
1053 for (k = 0; k < 4; k++) {
1054 f_cur = REFFREQ * (n + 2) / (m + 2) / (1 << k);
1055 if (ABS(f_cur - freq) < best_error) {
1056 best_error = ABS(f_cur - freq);
1057 best_n = n;
1058 best_m = m;
1059 best_k = k;
1060 }
1061 }
1062 }
1063 }
1064 n = best_n;
1065 m = best_m;
1066 k = best_k;
1067 *f_out = REFFREQ * (n + 2) / (m + 2) / (1 << k);
1068 return ( n << 8) | (m << 2) | k;
1069 }
1070
1071 static void
1072 voodoofb_setup_monitor(struct voodoofb_softc *sc, const struct videomode *vm)
1073 {
1074 struct voodoo_regs mod;
1075 struct voodoo_regs *mode;
1076 uint32_t horizontal_display_end, horizontal_sync_start,
1077 horizontal_sync_end, horizontal_total,
1078 horizontal_blanking_start, horizontal_blanking_end;
1079
1080 uint32_t vertical_display_enable_end, vertical_sync_start,
1081 vertical_sync_end, vertical_total, vertical_blanking_start,
1082 vertical_blanking_end;
1083
1084 uint32_t wd; // CRTC offset
1085
1086 int i;
1087
1088 uint8_t misc;
1089
1090 memset(&mod, 0, sizeof(mode));
1091
1092 mode = &mod;
1093
1094 wd = (vm->hdisplay >> 3) - 1;
1095 horizontal_display_end = (vm->hdisplay >> 3) - 1;
1096 horizontal_sync_start = (vm->hsync_start >> 3) - 1;
1097 horizontal_sync_end = (vm->hsync_end >> 3) - 1;
1098 horizontal_total = (vm->htotal >> 3) - 1;
1099 horizontal_blanking_start = horizontal_display_end;
1100 horizontal_blanking_end = horizontal_total;
1101
1102 vertical_display_enable_end = vm->vdisplay - 1;
1103 vertical_sync_start = vm->vsync_start; // - 1;
1104 vertical_sync_end = vm->vsync_end; // - 1;
1105 vertical_total = vm->vtotal - 2;
1106 vertical_blanking_start = vertical_display_enable_end;
1107 vertical_blanking_end = vertical_total;
1108
1109 misc = 0x0f |
1110 (vm->hdisplay < 400 ? 0xa0 :
1111 vm->hdisplay < 480 ? 0x60 :
1112 vm->hdisplay < 768 ? 0xe0 : 0x20);
1113
1114 mode->vr_seq[0] = 3;
1115 mode->vr_seq[1] = 1;
1116 mode->vr_seq[2] = 8;
1117 mode->vr_seq[3] = 0;
1118 mode->vr_seq[4] = 6;
1119
1120 /* crtc regs start */
1121 mode->vr_crtc[0] = horizontal_total - 4;
1122 mode->vr_crtc[1] = horizontal_display_end;
1123 mode->vr_crtc[2] = horizontal_blanking_start;
1124 mode->vr_crtc[3] = 0x80 | (horizontal_blanking_end & 0x1f);
1125 mode->vr_crtc[4] = horizontal_sync_start;
1126
1127 mode->vr_crtc[5] = ((horizontal_blanking_end & 0x20) << 2) |
1128 (horizontal_sync_end & 0x1f);
1129 mode->vr_crtc[6] = vertical_total;
1130 mode->vr_crtc[7] = ((vertical_sync_start & 0x200) >> 2) |
1131 ((vertical_display_enable_end & 0x200) >> 3) |
1132 ((vertical_total & 0x200) >> 4) |
1133 0x10 |
1134 ((vertical_blanking_start & 0x100) >> 5) |
1135 ((vertical_sync_start & 0x100) >> 6) |
1136 ((vertical_display_enable_end & 0x100) >> 7) |
1137 ((vertical_total & 0x100) >> 8);
1138
1139 mode->vr_crtc[8] = 0;
1140 mode->vr_crtc[9] = 0x40 |
1141 ((vertical_blanking_start & 0x200) >> 4);
1142
1143 mode->vr_crtc[10] = 0;
1144 mode->vr_crtc[11] = 0;
1145 mode->vr_crtc[12] = 0;
1146 mode->vr_crtc[13] = 0;
1147 mode->vr_crtc[14] = 0;
1148 mode->vr_crtc[15] = 0;
1149
1150 mode->vr_crtc[16] = vertical_sync_start;
1151 mode->vr_crtc[17] = (vertical_sync_end & 0x0f) | 0x20;
1152 mode->vr_crtc[18] = vertical_display_enable_end;
1153 mode->vr_crtc[19] = wd; // CRTC offset
1154 mode->vr_crtc[20] = 0;
1155 mode->vr_crtc[21] = vertical_blanking_start;
1156 mode->vr_crtc[22] = vertical_blanking_end + 1;
1157 mode->vr_crtc[23] = 128;
1158 mode->vr_crtc[24] = 255;
1159
1160 /* attr regs start */
1161 mode->vr_attr[0] = 0;
1162 mode->vr_attr[1] = 0;
1163 mode->vr_attr[2] = 0;
1164 mode->vr_attr[3] = 0;
1165 mode->vr_attr[4] = 0;
1166 mode->vr_attr[5] = 0;
1167 mode->vr_attr[6] = 0;
1168 mode->vr_attr[7] = 0;
1169 mode->vr_attr[8] = 0;
1170 mode->vr_attr[9] = 0;
1171 mode->vr_attr[10] = 0;
1172 mode->vr_attr[11] = 0;
1173 mode->vr_attr[12] = 0;
1174 mode->vr_attr[13] = 0;
1175 mode->vr_attr[14] = 0;
1176 mode->vr_attr[15] = 0;
1177 mode->vr_attr[16] = 1;
1178 mode->vr_attr[17] = 0;
1179 mode->vr_attr[18] = 15;
1180 mode->vr_attr[19] = 0;
1181 /* attr regs end */
1182
1183 /* graph regs start */
1184 mode->vr_graph[0] = 159;
1185 mode->vr_graph[1] = 127;
1186 mode->vr_graph[2] = 127;
1187 mode->vr_graph[3] = 131;
1188 mode->vr_graph[4] = 130;
1189 mode->vr_graph[5] = 142;
1190 mode->vr_graph[6] = 30;
1191 mode->vr_graph[7] = 245;
1192 mode->vr_graph[8] = 0;
1193
1194 vga_outb(sc, MISC_W, misc | 0x01);
1195
1196 for(i = 0; i < 5; i++)
1197 voodoo3_write_seq(sc, i, mode->vr_seq[i]);
1198 for (i = 0; i < 0x19; i ++)
1199 voodoo3_write_crtc(sc, i, mode->vr_crtc[i]);
1200 for (i = 0; i < 0x14; i ++)
1201 voodoo3_write_attr(sc, i, mode->vr_attr[i]);
1202 for (i = 0; i < 0x09; i ++)
1203 voodoo3_write_gra(sc, i, mode->vr_graph[i]);
1204 }
1205
1206 static void
1207 voodoofb_set_videomode(struct voodoofb_softc *sc,
1208 const struct videomode *vm)
1209 {
1210 uint32_t miscinit0 = 0;
1211 int vidpll, fout;
1212 uint32_t vp, vidproc = VIDPROCDEFAULT;
1213 uint32_t bpp = 1; /* for now */
1214 uint32_t bytes_per_row = vm->hdisplay * bpp;
1215
1216 sc->bits_per_pixel = bpp << 3;
1217 sc->width = vm->hdisplay;
1218 sc->height = vm->vdisplay;
1219 sc->linebytes = bytes_per_row;
1220
1221 voodoofb_setup_monitor(sc, vm);
1222 vp = voodoo3_read32(sc, VIDPROCCFG);
1223
1224 vidproc &= ~(0x1c0000); /* clear bits 18 to 20, bpp in vidproccfg */
1225 /* enable bits 18 to 20 to the required bpp */
1226 vidproc |= ((bpp - 1) << VIDCFG_PIXFMT_SHIFT);
1227
1228 vidpll = voodoofb_calc_pll(vm->dot_clock, &fout, 0);
1229
1230 #ifdef VOODOOFB_DEBUG
1231 printf("old vidproc: %08x\n", vp);
1232 printf("pll: %08x %d\n", vidpll, fout);
1233 #endif
1234 /* bit 10 of vidproccfg, is enabled or disabled as needed */
1235 switch (bpp) {
1236 case 1:
1237 /*
1238 * bit 10 off for palettized modes only, off means
1239 * palette is used
1240 */
1241 vidproc &= ~(1 << 10);
1242 break;
1243 #if 0
1244 case 2:
1245 #if __POWERPC__
1246 miscinit0 = 0xc0000000;
1247 #endif
1248 /* bypass palette for 16bit modes */
1249 vidproc |= (1 << 10);
1250 break;
1251 case 4:
1252 #if __POWERPC__
1253 miscinit0 = 0x40000000;
1254 #endif
1255 vidproc |= (1 << 10); /* Same for 32bit modes */
1256 break;
1257 #endif
1258 default:
1259 printf("We support only 8 bit for now\n");
1260 return;
1261 }
1262
1263 voodoofb_wait_idle(sc);
1264
1265 voodoo3_write32(sc, MISCINIT1, voodoo3_read32(sc, MISCINIT1) | 0x01);
1266
1267 voodoo3_make_room(sc, 4);
1268 voodoo3_write32(sc, VGAINIT0, 4928);
1269 voodoo3_write32(sc, DACMODE, 0);
1270 voodoo3_write32(sc, VIDDESKSTRIDE, bytes_per_row);
1271 voodoo3_write32(sc, PLLCTRL0, vidpll);
1272
1273 voodoo3_make_room(sc, 5);
1274 voodoo3_write32(sc, VIDSCREENSIZE, sc->width | (sc->height << 12));
1275 voodoo3_write32(sc, VIDDESKSTART, 1024);
1276
1277 vidproc &= ~VIDCFG_HWCURSOR_ENABLE;
1278 voodoo3_write32(sc, VIDPROCCFG, vidproc);
1279
1280 voodoo3_write32(sc, VGAINIT1, 0);
1281 voodoo3_write32(sc, MISCINIT0, miscinit0);
1282 #ifdef VOODOOFB_DEBUG
1283 printf("vidproc: %08x\n", vidproc);
1284 #endif
1285 voodoo3_make_room(sc, 8);
1286 voodoo3_write32(sc, SRCBASE, 0);
1287 voodoo3_write32(sc, DSTBASE, 0);
1288 voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
1289 voodoo3_write32(sc, CLIP0MIN, 0);
1290 voodoo3_write32(sc, CLIP0MAX, 0x0fff0fff);
1291 voodoo3_write32(sc, CLIP1MIN, 0);
1292 voodoo3_write32(sc, CLIP1MAX, 0x0fff0fff);
1293 voodoo3_write32(sc, SRCXY, 0);
1294 voodoofb_wait_idle(sc);
1295 printf("%s: switched to %dx%d, %d bit\n", sc->sc_dev.dv_xname,
1296 sc->width, sc->height, sc->bits_per_pixel);
1297 }
1298
1299 static void
1300 voodoofb_init(struct voodoofb_softc *sc)
1301 {
1302 /* XXX */
1303 uint32_t vgainit0 = 0;
1304 uint32_t vidcfg = 0;
1305
1306 #ifdef VOODOOFB_DEBUG
1307 printf("initializing engine...");
1308 #endif
1309 vgainit0 = voodoo3_read32(sc, VGAINIT0);
1310 #ifdef VOODOOFB_DEBUG
1311 printf("vga: %08x", vgainit0);
1312 #endif
1313 vgainit0 |=
1314 VGAINIT0_8BIT_DAC |
1315 VGAINIT0_EXT_ENABLE |
1316 VGAINIT0_WAKEUP_3C3 |
1317 VGAINIT0_ALT_READBACK |
1318 VGAINIT0_EXTSHIFTOUT;
1319
1320 vidcfg = voodoo3_read32(sc, VIDPROCCFG);
1321 #ifdef VOODOOFB_DEBUG
1322 printf(" vidcfg: %08x\n", vidcfg);
1323 #endif
1324 vidcfg |=
1325 VIDCFG_VIDPROC_ENABLE |
1326 VIDCFG_DESK_ENABLE;
1327 vidcfg &= ~VIDCFG_HWCURSOR_ENABLE;
1328
1329 voodoo3_make_room(sc, 2);
1330
1331 voodoo3_write32(sc, VGAINIT0, vgainit0);
1332 voodoo3_write32(sc, VIDPROCCFG, vidcfg);
1333
1334 voodoo3_make_room(sc, 8);
1335 voodoo3_write32(sc, SRCBASE, 0);
1336 voodoo3_write32(sc, DSTBASE, 0);
1337 voodoo3_write32(sc, COMMANDEXTRA_2D, 0);
1338 voodoo3_write32(sc, CLIP0MIN, 0);
1339 voodoo3_write32(sc, CLIP0MAX, 0x1fff1fff);
1340 voodoo3_write32(sc, CLIP1MIN, 0);
1341 voodoo3_write32(sc, CLIP1MAX, 0x1fff1fff);
1342 voodoo3_write32(sc, SRCXY, 0);
1343
1344 voodoofb_wait_idle(sc);
1345 }
1346