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