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