machfb.c revision 1.90 1 /* $NetBSD: machfb.c,v 1.90 2013/11/06 14:52:25 macallan Exp $ */
2
3 /*
4 * Copyright (c) 2002 Bang Jun-Young
5 * Copyright (c) 2005, 2006, 2007 Michael Lorenz
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /*
32 * Some code is derived from ATI Rage Pro and Derivatives Programmer's Guide.
33 */
34
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0,
37 "$NetBSD: machfb.c,v 1.90 2013/11/06 14:52:25 macallan Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
44 #include <sys/callout.h>
45 #include <sys/lwp.h>
46 #include <sys/kauth.h>
47
48 #include <dev/videomode/videomode.h>
49 #include <dev/videomode/edidvar.h>
50
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcidevs.h>
54 #include <dev/pci/pciio.h>
55 #include <dev/pci/machfbreg.h>
56
57 #ifdef __sparc__
58 #include <dev/sun/fbio.h>
59 #include <dev/sun/fbvar.h>
60 #include <sys/conf.h>
61 #else
62 #include <dev/wscons/wsdisplayvar.h>
63 #endif
64
65 #include <dev/wscons/wsconsio.h>
66 #include <dev/wsfont/wsfont.h>
67 #include <dev/rasops/rasops.h>
68 #include <dev/pci/wsdisplay_pci.h>
69
70 #include <dev/wscons/wsdisplay_vconsvar.h>
71 #include <dev/wscons/wsdisplay_glyphcachevar.h>
72
73 #include "opt_wsemul.h"
74 #include "opt_machfb.h"
75
76 #define MACH64_REG_SIZE 0x800
77 #define MACH64_REG_OFF 0x7ff800
78
79 #define NBARS 3 /* number of Mach64 PCI BARs */
80
81 struct vga_bar {
82 bus_addr_t vb_base;
83 bus_size_t vb_size;
84 pcireg_t vb_type;
85 int vb_flags;
86 };
87
88 struct mach64_softc {
89 device_t sc_dev;
90 #ifdef __sparc__
91 struct fbdevice sc_fb;
92 #endif
93 pci_chipset_tag_t sc_pc;
94 pcitag_t sc_pcitag;
95
96 struct vga_bar sc_bars[NBARS];
97 struct vga_bar sc_rom;
98
99 #define sc_aperbase sc_bars[0].vb_base
100 #define sc_apersize sc_bars[0].vb_size
101
102 #define sc_iobase sc_bars[1].vb_base
103 #define sc_iosize sc_bars[1].vb_size
104
105 #define sc_regbase sc_bars[2].vb_base
106 #define sc_regsize sc_bars[2].vb_size
107
108 bus_space_tag_t sc_regt;
109 bus_space_tag_t sc_memt;
110 bus_space_tag_t sc_iot;
111 bus_space_handle_t sc_regh;
112 bus_space_handle_t sc_memh;
113 #if 0
114 void *sc_aperture; /* mapped aperture vaddr */
115 void *sc_registers; /* mapped registers vaddr */
116 #endif
117 uint32_t sc_nbus, sc_ndev, sc_nfunc;
118 size_t memsize;
119 int memtype;
120
121 int sc_mode;
122 int sc_bg;
123 int sc_locked;
124
125 int has_dsp;
126 int bits_per_pixel;
127 int max_x;
128 int max_y;
129 int virt_x;
130 int virt_y;
131 int color_depth;
132
133 int mem_freq;
134 int ramdac_freq;
135 int ref_freq;
136
137 int ref_div;
138 int log2_vclk_post_div;
139 int vclk_post_div;
140 int vclk_fb_div;
141 int mclk_post_div;
142 int mclk_fb_div;
143 int sc_clock; /* which clock to use */
144
145 struct videomode *sc_my_mode;
146 int sc_edid_size;
147 uint8_t sc_edid_data[1024];
148
149 u_char sc_cmap_red[256];
150 u_char sc_cmap_green[256];
151 u_char sc_cmap_blue[256];
152 int sc_dacw, sc_blanked, sc_console;
153 struct vcons_data vd;
154 struct wsdisplay_accessops sc_accessops;
155 glyphcache sc_gc;
156 };
157
158 struct mach64_crtcregs {
159 uint32_t h_total_disp;
160 uint32_t h_sync_strt_wid;
161 uint32_t v_total_disp;
162 uint32_t v_sync_strt_wid;
163 uint32_t gen_cntl;
164 uint32_t clock_cntl;
165 uint32_t color_depth;
166 uint32_t dot_clock;
167 };
168
169 static struct {
170 uint16_t chip_id;
171 uint32_t ramdac_freq;
172 } const mach64_info[] = {
173 { PCI_PRODUCT_ATI_MACH64_GX, 135000 },
174 { PCI_PRODUCT_ATI_MACH64_CX, 135000 },
175 { PCI_PRODUCT_ATI_MACH64_CT, 135000 },
176 { PCI_PRODUCT_ATI_RAGE_PRO_AGP, 230000 },
177 { PCI_PRODUCT_ATI_RAGE_PRO_AGP1X, 230000 },
178 { PCI_PRODUCT_ATI_RAGE_PRO_PCI_B, 230000 },
179 { PCI_PRODUCT_ATI_RAGE_XL_AGP, 230000 },
180 { PCI_PRODUCT_ATI_RAGE_PRO_PCI_P, 230000 },
181 { PCI_PRODUCT_ATI_RAGE_PRO_PCI_L, 230000 },
182 { PCI_PRODUCT_ATI_RAGE_XL_PCI, 230000 },
183 { PCI_PRODUCT_ATI_RAGE_XL_PCI66, 230000 },
184 { PCI_PRODUCT_ATI_RAGE_II, 135000 },
185 { PCI_PRODUCT_ATI_RAGE_IIP, 200000 },
186 { PCI_PRODUCT_ATI_RAGE_IIC_PCI, 230000 },
187 { PCI_PRODUCT_ATI_RAGE_IIC_AGP_B, 230000 },
188 { PCI_PRODUCT_ATI_RAGE_IIC_AGP_P, 230000 },
189 #if 0
190 { PCI_PRODUCT_ATI_RAGE_MOB_M3_PCI, 230000 },
191 { PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP, 230000 },
192 { PCI_PRODUCT_ATI_RAGE_MOBILITY, 230000 },
193 #endif
194 { PCI_PRODUCT_ATI_RAGE_L_MOB_M1_PCI, 230000 },
195 { PCI_PRODUCT_ATI_RAGE_LT_PRO_AGP, 230000 },
196 { PCI_PRODUCT_ATI_RAGE_LT_PRO, 230000 },
197 { PCI_PRODUCT_ATI_RAGE_LT, 230000 },
198 { PCI_PRODUCT_ATI_RAGE_LT_PRO_PCI, 230000 },
199 { PCI_PRODUCT_ATI_MACH64_VT, 170000 },
200 { PCI_PRODUCT_ATI_MACH64_VTB, 200000 },
201 { PCI_PRODUCT_ATI_MACH64_VT4, 230000 }
202 };
203
204 static int mach64_chip_id, mach64_chip_rev;
205 static struct videomode default_mode = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
206
207 static const char *mach64_gx_memtype_names[] = {
208 "DRAM", "VRAM", "VRAM", "DRAM",
209 "DRAM", "VRAM", "VRAM", "(unknown type)"
210 };
211
212 static const char *mach64_memtype_names[] = {
213 "(N/A)", "DRAM", "EDO DRAM", "EDO DRAM", "SDRAM", "SGRAM", "WRAM",
214 "(unknown type)"
215 };
216
217 static struct videomode mach64_modes[] = {
218 /* 640x400 @ 70 Hz, 31.5 kHz */
219 { 25175, 640, 664, 760, 800, 400, 409, 411, 450, 0, NULL, },
220 /* 640x480 @ 72 Hz, 36.5 kHz */
221 { 25175, 640, 664, 760, 800, 480, 491, 493, 525, 0, NULL, },
222 /* 800x600 @ 72 Hz, 48.0 kHz */
223 { 50000, 800, 856, 976, 1040, 600, 637, 643, 666,
224 VID_PHSYNC | VID_PVSYNC, NULL, },
225 /* 1024x768 @ 70 Hz, 56.5 kHz */
226 { 75000, 1024, 1048, 1184, 1328, 768, 771, 777, 806,
227 VID_NHSYNC | VID_NVSYNC, NULL, },
228 /* 1152x864 @ 70 Hz, 62.4 kHz */
229 { 92000, 1152, 1208, 1368, 1474, 864, 865, 875, 895, 0, NULL, },
230 /* 1280x1024 @ 70 Hz, 74.59 kHz */
231 { 126500, 1280, 1312, 1472, 1696, 1024, 1032, 1040, 1068,
232 VID_NHSYNC | VID_NVSYNC, NULL, }
233 };
234
235 extern const u_char rasops_cmap[768];
236
237 static int mach64_match(device_t, cfdata_t, void *);
238 static void mach64_attach(device_t, device_t, void *);
239
240 CFATTACH_DECL_NEW(machfb, sizeof(struct mach64_softc), mach64_match, mach64_attach,
241 NULL, NULL);
242
243 static void mach64_init(struct mach64_softc *);
244 static int mach64_get_memsize(struct mach64_softc *);
245 static int mach64_get_max_ramdac(struct mach64_softc *);
246
247 #if defined(__sparc__) || defined(__powerpc__)
248 static void mach64_get_mode(struct mach64_softc *, struct videomode *);
249 #endif
250
251 static int mach64_calc_crtcregs(struct mach64_softc *,
252 struct mach64_crtcregs *,
253 struct videomode *);
254 static void mach64_set_crtcregs(struct mach64_softc *,
255 struct mach64_crtcregs *);
256
257 static int mach64_modeswitch(struct mach64_softc *, struct videomode *);
258 static void mach64_set_dsp(struct mach64_softc *);
259 static void mach64_set_pll(struct mach64_softc *, int);
260 static void mach64_reset_engine(struct mach64_softc *);
261 static void mach64_init_engine(struct mach64_softc *);
262 #if 0
263 static void mach64_adjust_frame(struct mach64_softc *, int, int);
264 #endif
265 static void mach64_init_lut(struct mach64_softc *);
266
267 static void mach64_init_screen(void *, struct vcons_screen *, int, long *);
268 static int mach64_set_screentype(struct mach64_softc *,
269 const struct wsscreen_descr *);
270 static int mach64_is_console(struct mach64_softc *);
271
272 static void mach64_cursor(void *, int, int, int);
273 #if 0
274 static int mach64_mapchar(void *, int, u_int *);
275 #endif
276 static void mach64_putchar_mono(void *, int, int, u_int, long);
277 static void mach64_putchar_aa8(void *, int, int, u_int, long);
278 static void mach64_copycols(void *, int, int, int, int);
279 static void mach64_erasecols(void *, int, int, int, long);
280 static void mach64_copyrows(void *, int, int, int);
281 static void mach64_eraserows(void *, int, int, long);
282 static void mach64_clearscreen(struct mach64_softc *);
283
284 static int mach64_putcmap(struct mach64_softc *, struct wsdisplay_cmap *);
285 static int mach64_getcmap(struct mach64_softc *, struct wsdisplay_cmap *);
286 static int mach64_putpalreg(struct mach64_softc *, uint8_t, uint8_t,
287 uint8_t, uint8_t);
288 static void mach64_bitblt(void *, int, int, int, int, int, int, int);
289 static void mach64_rectfill(struct mach64_softc *, int, int, int, int, int);
290 static void mach64_setup_mono(struct mach64_softc *, int, int, int, int,
291 uint32_t, uint32_t);
292 static void mach64_feed_bytes(struct mach64_softc *, int, uint8_t *);
293 #if 0
294 static void mach64_showpal(struct mach64_softc *);
295 #endif
296
297 static void machfb_blank(struct mach64_softc *, int);
298 static int machfb_drm_print(void *, const char *);
299
300 static struct wsscreen_descr mach64_defaultscreen = {
301 "default",
302 80, 30,
303 NULL,
304 8, 16,
305 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
306 &default_mode
307 }, mach64_80x25_screen = {
308 "80x25", 80, 25,
309 NULL,
310 8, 16,
311 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
312 &mach64_modes[0]
313 }, mach64_80x30_screen = {
314 "80x30", 80, 30,
315 NULL,
316 8, 16,
317 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
318 &mach64_modes[1]
319 }, mach64_80x40_screen = {
320 "80x40", 80, 40,
321 NULL,
322 8, 10,
323 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
324 &mach64_modes[0]
325 }, mach64_80x50_screen = {
326 "80x50", 80, 50,
327 NULL,
328 8, 8,
329 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
330 &mach64_modes[0]
331 }, mach64_100x37_screen = {
332 "100x37", 100, 37,
333 NULL,
334 8, 16,
335 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
336 &mach64_modes[2]
337 }, mach64_128x48_screen = {
338 "128x48", 128, 48,
339 NULL,
340 8, 16,
341 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
342 &mach64_modes[3]
343 }, mach64_144x54_screen = {
344 "144x54", 144, 54,
345 NULL,
346 8, 16,
347 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
348 &mach64_modes[4]
349 }, mach64_160x64_screen = {
350 "160x54", 160, 64,
351 NULL,
352 8, 16,
353 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
354 &mach64_modes[5]
355 };
356
357 static const struct wsscreen_descr *_mach64_scrlist[] = {
358 &mach64_defaultscreen,
359 &mach64_80x25_screen,
360 &mach64_80x30_screen,
361 &mach64_80x40_screen,
362 &mach64_80x50_screen,
363 &mach64_100x37_screen,
364 &mach64_128x48_screen,
365 &mach64_144x54_screen,
366 &mach64_160x64_screen
367 };
368
369 static struct wsscreen_list mach64_screenlist = {
370 __arraycount(_mach64_scrlist),
371 _mach64_scrlist
372 };
373
374 static int mach64_ioctl(void *, void *, u_long, void *, int,
375 struct lwp *);
376 static paddr_t mach64_mmap(void *, void *, off_t, int);
377
378 #if 0
379 static int mach64_load_font(void *, void *, struct wsdisplay_font *);
380 #endif
381
382
383 static struct vcons_screen mach64_console_screen;
384
385 /* framebuffer device, SPARC-only so far */
386 #ifdef __sparc__
387
388 static void machfb_unblank(device_t);
389 static void machfb_fbattach(struct mach64_softc *);
390
391 extern struct cfdriver machfb_cd;
392
393 dev_type_open(machfb_fbopen);
394 dev_type_close(machfb_fbclose);
395 dev_type_ioctl(machfb_fbioctl);
396 dev_type_mmap(machfb_fbmmap);
397
398 /* frame buffer generic driver */
399 static struct fbdriver machfb_fbdriver = {
400 machfb_unblank, machfb_fbopen, machfb_fbclose, machfb_fbioctl, nopoll,
401 machfb_fbmmap, nokqfilter
402 };
403
404 #endif /* __sparc__ */
405
406 /*
407 * Inline functions for getting access to register aperture.
408 */
409
410 static inline uint32_t
411 regr(struct mach64_softc *sc, uint32_t index)
412 {
413 return bus_space_read_4(sc->sc_regt, sc->sc_regh, index + 0x400);
414 }
415
416 static inline uint8_t
417 regrb(struct mach64_softc *sc, uint32_t index)
418 {
419 return bus_space_read_1(sc->sc_regt, sc->sc_regh, index + 0x400);
420 }
421
422 static inline void
423 regw(struct mach64_softc *sc, uint32_t index, uint32_t data)
424 {
425 bus_space_write_4(sc->sc_regt, sc->sc_regh, index + 0x400, data);
426 bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 4,
427 BUS_SPACE_BARRIER_WRITE);
428 }
429
430 static inline void
431 regws(struct mach64_softc *sc, uint32_t index, uint32_t data)
432 {
433 bus_space_write_stream_4(sc->sc_regt, sc->sc_regh, index + 0x400, data);
434 bus_space_barrier(sc->sc_regt, sc->sc_regh, index + 0x400, 4,
435 BUS_SPACE_BARRIER_WRITE);
436 }
437
438 static inline void
439 regwb(struct mach64_softc *sc, uint32_t index, uint8_t data)
440 {
441 bus_space_write_1(sc->sc_regt, sc->sc_regh, index + 0x400, data);
442 bus_space_barrier(sc->sc_regt, sc->sc_regh, index + 0x400, 1,
443 BUS_SPACE_BARRIER_WRITE);
444 }
445
446 static inline void
447 regwb_pll(struct mach64_softc *sc, uint32_t index, uint8_t data)
448 {
449 uint32_t reg;
450
451 reg = regr(sc, CLOCK_CNTL);
452 reg |= PLL_WR_EN;
453 regw(sc, CLOCK_CNTL, reg);
454 reg &= ~(PLL_ADDR | PLL_DATA);
455 reg |= (index & 0x3f) << PLL_ADDR_SHIFT;
456 reg |= data << PLL_DATA_SHIFT;
457 reg |= CLOCK_STROBE;
458 regw(sc, CLOCK_CNTL, reg);
459 reg &= ~PLL_WR_EN;
460 regw(sc, CLOCK_CNTL, reg);
461 }
462
463 static inline uint8_t
464 regrb_pll(struct mach64_softc *sc, uint32_t index)
465 {
466
467 regwb(sc, CLOCK_CNTL + 1, index << 2);
468 return regrb(sc, CLOCK_CNTL + 2);
469 }
470
471 static inline void
472 wait_for_fifo(struct mach64_softc *sc, uint8_t v)
473 {
474 while ((regr(sc, FIFO_STAT) & 0xffff) > (0x8000 >> v))
475 continue;
476 }
477
478 static inline void
479 wait_for_idle(struct mach64_softc *sc)
480 {
481 wait_for_fifo(sc, 16);
482 while ((regr(sc, GUI_STAT) & 1) != 0)
483 continue;
484 }
485
486 static int
487 mach64_match(device_t parent, cfdata_t match, void *aux)
488 {
489 struct pci_attach_args *pa = (struct pci_attach_args *)aux;
490 int i;
491
492 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
493 PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
494 return 0;
495
496 for (i = 0; i < __arraycount(mach64_info); i++)
497 if (PCI_PRODUCT(pa->pa_id) == mach64_info[i].chip_id) {
498 mach64_chip_id = PCI_PRODUCT(pa->pa_id);
499 mach64_chip_rev = PCI_REVISION(pa->pa_class);
500 return 100;
501 }
502
503 return 0;
504 }
505
506 static void
507 mach64_attach(device_t parent, device_t self, void *aux)
508 {
509 struct mach64_softc *sc = device_private(self);
510 struct pci_attach_args *pa = aux;
511 struct rasops_info *ri;
512 prop_data_t edid_data;
513 #if defined(__sparc__) || defined(__powerpc__)
514 const struct videomode *mode = NULL;
515 #endif
516 int bar, id, expected_id;
517 int is_gx;
518 const char **memtype_names;
519 struct wsemuldisplaydev_attach_args aa;
520 long defattr;
521 int setmode, width, height;
522 pcireg_t screg;
523 uint32_t reg;
524 const pcireg_t enables = PCI_COMMAND_MEM_ENABLE;
525 int use_mmio = FALSE;
526
527 sc->sc_dev = self;
528 sc->sc_pc = pa->pa_pc;
529 sc->sc_pcitag = pa->pa_tag;
530 sc->sc_dacw = -1;
531 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
532 sc->sc_nbus = pa->pa_bus;
533 sc->sc_ndev = pa->pa_device;
534 sc->sc_nfunc = pa->pa_function;
535 sc->sc_locked = 0;
536 sc->sc_iot = pa->pa_iot;
537 sc->sc_accessops.ioctl = mach64_ioctl;
538 sc->sc_accessops.mmap = mach64_mmap;
539
540 pci_aprint_devinfo(pa, "Graphics processor");
541 #ifdef MACHFB_DEBUG
542 printf(prop_dictionary_externalize(device_properties(self)));
543 #endif
544
545 /* enable memory access */
546 screg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
547 if ((screg & enables) != enables) {
548 screg |= enables;
549 pci_conf_write(sc->sc_pc, sc->sc_pcitag,
550 PCI_COMMAND_STATUS_REG, screg);
551 }
552 for (bar = 0; bar < NBARS; bar++) {
553 reg = PCI_MAPREG_START + (bar * 4);
554 sc->sc_bars[bar].vb_type = pci_mapreg_type(sc->sc_pc,
555 sc->sc_pcitag, reg);
556 (void)pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, reg,
557 sc->sc_bars[bar].vb_type, &sc->sc_bars[bar].vb_base,
558 &sc->sc_bars[bar].vb_size, &sc->sc_bars[bar].vb_flags);
559 }
560 aprint_debug_dev(sc->sc_dev, "aperture size %08x\n",
561 (uint32_t)sc->sc_apersize);
562
563 sc->sc_rom.vb_type = PCI_MAPREG_TYPE_ROM;
564 pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, PCI_MAPREG_ROM,
565 sc->sc_rom.vb_type, &sc->sc_rom.vb_base,
566 &sc->sc_rom.vb_size, &sc->sc_rom.vb_flags);
567 sc->sc_memt = pa->pa_memt;
568
569 /* use MMIO register aperture if available */
570 if ((sc->sc_regbase != 0) && (sc->sc_regbase != 0xffffffff)) {
571 if (pci_mapreg_map(pa, MACH64_BAR_MMIO, PCI_MAPREG_TYPE_MEM, 0,
572 &sc->sc_regt, &sc->sc_regh, &sc->sc_regbase,
573 &sc->sc_regsize) == 0) {
574
575 /*
576 * the MMIO aperture maps both 1KB register blocks, but
577 * all register offsets are relative to the 2nd one so
578 * for now fix this up in MACH64_REG_OFF and the access
579 * functions
580 */
581 aprint_normal_dev(sc->sc_dev, "using MMIO aperture\n");
582 use_mmio = TRUE;
583 }
584 }
585 if (!use_mmio) {
586 if (bus_space_map(sc->sc_memt, sc->sc_aperbase, sc->sc_apersize,
587 BUS_SPACE_MAP_LINEAR, &sc->sc_memh)) {
588 panic("%s: failed to map aperture",
589 device_xname(sc->sc_dev));
590 }
591
592 sc->sc_regt = sc->sc_memt;
593 bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
594 MACH64_REG_SIZE, &sc->sc_regh);
595 }
596
597 mach64_init(sc);
598
599 aprint_normal_dev(sc->sc_dev,
600 "%d MB aperture at 0x%08x, %d KB registers at 0x%08x\n",
601 (u_int)(sc->sc_apersize / (1024 * 1024)),
602 (u_int)sc->sc_aperbase, (u_int)(sc->sc_regsize / 1024),
603 (u_int)sc->sc_regbase);
604
605 printf("%s: %d KB ROM at 0x%08x\n", device_xname(sc->sc_dev),
606 (int)sc->sc_rom.vb_size >> 10, (uint32_t)sc->sc_rom.vb_base);
607
608 prop_dictionary_get_uint32(device_properties(self), "width", &width);
609 prop_dictionary_get_uint32(device_properties(self), "height", &height);
610
611 if ((edid_data = prop_dictionary_get(device_properties(self), "EDID"))
612 != NULL) {
613 struct edid_info ei;
614
615 sc->sc_edid_size = min(1024, prop_data_size(edid_data));
616 memset(sc->sc_edid_data, 0, sizeof(sc->sc_edid_data));
617 memcpy(sc->sc_edid_data, prop_data_data_nocopy(edid_data),
618 sc->sc_edid_size);
619
620 edid_parse(sc->sc_edid_data, &ei);
621
622 #ifdef MACHFB_DEBUG
623 edid_print(&ei);
624 #endif
625 }
626
627 is_gx = 0;
628 switch(mach64_chip_id) {
629 case PCI_PRODUCT_ATI_MACH64_GX:
630 case PCI_PRODUCT_ATI_MACH64_CX:
631 is_gx = 1;
632 case PCI_PRODUCT_ATI_MACH64_CT:
633 sc->has_dsp = 0;
634 break;
635 case PCI_PRODUCT_ATI_MACH64_VT:
636 case PCI_PRODUCT_ATI_RAGE_II:
637 if((mach64_chip_rev & 0x07) == 0) {
638 sc->has_dsp = 0;
639 break;
640 }
641 /* Otherwise fall through. */
642 default:
643 sc->has_dsp = 1;
644 }
645
646 memtype_names = is_gx ? mach64_gx_memtype_names : mach64_memtype_names;
647
648 sc->memsize = mach64_get_memsize(sc);
649
650 if(is_gx)
651 sc->memtype = (regr(sc, CONFIG_STAT0) >> 3) & 0x07;
652 else
653 sc->memtype = regr(sc, CONFIG_STAT0) & 0x07;
654
655 /*
656 * XXX is there any way to calculate reference frequency from
657 * known values?
658 */
659 if ((mach64_chip_id == PCI_PRODUCT_ATI_RAGE_XL_PCI) ||
660 ((mach64_chip_id >= PCI_PRODUCT_ATI_RAGE_LT_PRO_PCI) &&
661 (mach64_chip_id <= PCI_PRODUCT_ATI_RAGE_LT_PRO))) {
662 aprint_normal_dev(sc->sc_dev, "ref_freq=29.498MHz\n");
663 sc->ref_freq = 29498;
664 } else
665 sc->ref_freq = 14318;
666
667 reg = regr(sc, CLOCK_CNTL);
668 aprint_debug("CLOCK_CNTL: %08x\n", reg);
669 sc->sc_clock = reg & 3;
670 aprint_debug("using clock %d\n", sc->sc_clock);
671
672 sc->ref_div = regrb_pll(sc, PLL_REF_DIV);
673 aprint_debug("ref_div: %d\n", sc->ref_div);
674 sc->mclk_fb_div = regrb_pll(sc, MCLK_FB_DIV);
675 aprint_debug("mclk_fb_div: %d\n", sc->mclk_fb_div);
676 sc->mem_freq = (2 * sc->ref_freq * sc->mclk_fb_div) /
677 (sc->ref_div * 2);
678 sc->mclk_post_div = (sc->mclk_fb_div * 2 * sc->ref_freq) /
679 (sc->mem_freq * sc->ref_div);
680 sc->ramdac_freq = mach64_get_max_ramdac(sc);
681 aprint_normal_dev(sc->sc_dev,
682 "%ld KB %s %d.%d MHz, maximum RAMDAC clock %d MHz\n",
683 (u_long)sc->memsize,
684 memtype_names[sc->memtype],
685 sc->mem_freq / 1000, sc->mem_freq % 1000,
686 sc->ramdac_freq / 1000);
687
688 id = regr(sc, CONFIG_CHIP_ID) & 0xffff;
689 switch(mach64_chip_id) {
690 case PCI_PRODUCT_ATI_MACH64_GX:
691 expected_id = 0x00d7;
692 break;
693 case PCI_PRODUCT_ATI_MACH64_CX:
694 expected_id = 0x0057;
695 break;
696 default:
697 /* Most chip IDs match their PCI product ID. */
698 expected_id = mach64_chip_id;
699 }
700
701 if (id != expected_id) {
702 aprint_error_dev(sc->sc_dev,
703 "chip ID mismatch, 0x%x != 0x%x\n", id, expected_id);
704 return;
705 }
706
707 sc->sc_console = mach64_is_console(sc);
708 aprint_debug("gen_cntl: %08x\n", regr(sc, CRTC_GEN_CNTL));
709 #if defined(__sparc__) || defined(__powerpc__)
710 if (sc->sc_console) {
711 if (mode != NULL) {
712 memcpy(&default_mode, mode, sizeof(struct videomode));
713 setmode = 1;
714 } else {
715 mach64_get_mode(sc, &default_mode);
716 setmode = 0;
717 }
718 sc->sc_my_mode = &default_mode;
719 } else {
720 /* fill in default_mode if it's empty */
721 mach64_get_mode(sc, &default_mode);
722 if (default_mode.dot_clock == 0) {
723 memcpy(&default_mode, &mach64_modes[4],
724 sizeof(default_mode));
725 }
726 sc->sc_my_mode = &default_mode;
727 setmode = 1;
728 }
729 #else
730 if (default_mode.dot_clock == 0) {
731 memcpy(&default_mode, &mach64_modes[0],
732 sizeof(default_mode));
733 }
734 sc->sc_my_mode = &mach64_modes[0];
735 setmode = 1;
736 #endif
737
738 sc->bits_per_pixel = 8;
739 sc->virt_x = sc->sc_my_mode->hdisplay;
740 sc->virt_y = sc->sc_my_mode->vdisplay;
741 sc->max_x = sc->virt_x - 1;
742 sc->max_y = (sc->memsize * 1024) /
743 (sc->virt_x * (sc->bits_per_pixel / 8)) - 1;
744
745 sc->color_depth = CRTC_PIX_WIDTH_8BPP;
746
747 mach64_init_engine(sc);
748
749 if (setmode)
750 mach64_modeswitch(sc, sc->sc_my_mode);
751
752 aprint_normal_dev(sc->sc_dev,
753 "initial resolution %dx%d at %d bpp\n",
754 sc->sc_my_mode->hdisplay, sc->sc_my_mode->vdisplay,
755 sc->bits_per_pixel);
756
757 #ifdef __sparc__
758 machfb_fbattach(sc);
759 #endif
760
761 wsfont_init();
762
763 vcons_init(&sc->vd, sc, &mach64_defaultscreen, &sc->sc_accessops);
764 sc->vd.init_screen = mach64_init_screen;
765
766 sc->sc_gc.gc_bitblt = mach64_bitblt;
767 sc->sc_gc.gc_blitcookie = sc;
768 sc->sc_gc.gc_rop = MIX_SRC;
769
770 ri = &mach64_console_screen.scr_ri;
771 if (sc->sc_console) {
772
773 vcons_init_screen(&sc->vd, &mach64_console_screen, 1,
774 &defattr);
775 mach64_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
776
777 mach64_defaultscreen.textops = &ri->ri_ops;
778 mach64_defaultscreen.capabilities = ri->ri_caps;
779 mach64_defaultscreen.nrows = ri->ri_rows;
780 mach64_defaultscreen.ncols = ri->ri_cols;
781 glyphcache_init(&sc->sc_gc, sc->sc_my_mode->vdisplay + 5,
782 ((sc->memsize * 1024) / sc->sc_my_mode->hdisplay) -
783 sc->sc_my_mode->vdisplay - 5,
784 sc->sc_my_mode->hdisplay,
785 ri->ri_font->fontwidth,
786 ri->ri_font->fontheight,
787 defattr);
788 wsdisplay_cnattach(&mach64_defaultscreen, ri, 0, 0, defattr);
789 } else {
790 /*
791 * since we're not the console we can postpone the rest
792 * until someone actually allocates a screen for us
793 */
794 mach64_modeswitch(sc, sc->sc_my_mode);
795 if (mach64_console_screen.scr_ri.ri_rows == 0) {
796 /* do some minimal setup to avoid weirdnesses later */
797 vcons_init_screen(&sc->vd, &mach64_console_screen, 1,
798 &defattr);
799 } else
800 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
801
802 glyphcache_init(&sc->sc_gc, sc->sc_my_mode->vdisplay + 5,
803 ((sc->memsize * 1024) / sc->sc_my_mode->hdisplay) -
804 sc->sc_my_mode->vdisplay - 5,
805 sc->sc_my_mode->hdisplay,
806 ri->ri_font->fontwidth,
807 ri->ri_font->fontheight,
808 defattr);
809 }
810
811 sc->sc_bg = mach64_console_screen.scr_ri.ri_devcmap[WS_DEFAULT_BG];
812 mach64_clearscreen(sc);
813 mach64_init_lut(sc);
814
815 if (sc->sc_console)
816 vcons_replay_msgbuf(&mach64_console_screen);
817
818 machfb_blank(sc, 0); /* unblank the screen */
819
820 aa.console = sc->sc_console;
821 aa.scrdata = &mach64_screenlist;
822 aa.accessops = &sc->sc_accessops;
823 aa.accesscookie = &sc->vd;
824
825 config_found(self, &aa, wsemuldisplaydevprint);
826 #if 0
827 /* XXX
828 * turns out some firmware doesn't turn these back on when needed
829 * so we need to turn them off only when mapping vram in
830 * WSDISPLAYIO_MODE_DUMB would overlap ( unlikely but far from
831 * impossible )
832 */
833 if (use_mmio) {
834 /*
835 * Now that we took over, turn off the aperture registers if we
836 * don't use them. Can't do this earlier since on some hardware
837 * we use firmware calls as early console output which may in
838 * turn try to access these registers.
839 */
840 reg = regr(sc, BUS_CNTL);
841 aprint_debug_dev(sc->sc_dev, "BUS_CNTL: %08x\n", reg);
842 reg |= BUS_APER_REG_DIS;
843 regw(sc, BUS_CNTL, reg);
844 }
845 #endif
846 config_found_ia(self, "drm", aux, machfb_drm_print);
847 }
848
849 static int
850 machfb_drm_print(void *aux, const char *pnp)
851 {
852 if (pnp)
853 aprint_normal("direct rendering for %s", pnp);
854 return (UNSUPP);
855 }
856
857 static void
858 mach64_init_screen(void *cookie, struct vcons_screen *scr, int existing,
859 long *defattr)
860 {
861 struct mach64_softc *sc = cookie;
862 struct rasops_info *ri = &scr->scr_ri;
863
864 /* XXX for now */
865 #define setmode 0
866
867 ri->ri_depth = sc->bits_per_pixel;
868 ri->ri_width = sc->sc_my_mode->hdisplay;
869 ri->ri_height = sc->sc_my_mode->vdisplay;
870 ri->ri_stride = ri->ri_width;
871 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
872 if (ri->ri_depth == 8)
873 ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
874
875 #ifdef VCONS_DRAW_INTR
876 scr->scr_flags |= VCONS_DONT_READ;
877 #endif
878
879 if (existing) {
880 if (setmode && mach64_set_screentype(sc, scr->scr_type)) {
881 panic("%s: failed to switch video mode",
882 device_xname(sc->sc_dev));
883 }
884 }
885
886 rasops_init(ri, 0, 0);
887 ri->ri_caps = WSSCREEN_WSCOLORS;
888 rasops_reconfig(ri, sc->sc_my_mode->vdisplay / ri->ri_font->fontheight,
889 sc->sc_my_mode->hdisplay / ri->ri_font->fontwidth);
890
891 /* enable acceleration */
892 ri->ri_hw = scr;
893 ri->ri_ops.copyrows = mach64_copyrows;
894 ri->ri_ops.copycols = mach64_copycols;
895 ri->ri_ops.eraserows = mach64_eraserows;
896 ri->ri_ops.erasecols = mach64_erasecols;
897 ri->ri_ops.cursor = mach64_cursor;
898 if (FONT_IS_ALPHA(ri->ri_font)) {
899 ri->ri_ops.putchar = mach64_putchar_aa8;
900 } else
901 ri->ri_ops.putchar = mach64_putchar_mono;
902 }
903
904 static void
905 mach64_init(struct mach64_softc *sc)
906 {
907 sc->sc_blanked = 0;
908 }
909
910 static int
911 mach64_get_memsize(struct mach64_softc *sc)
912 {
913 int tmp, memsize;
914 int mem_tab[] = {
915 512, 1024, 2048, 4096, 6144, 8192, 12288, 16384
916 };
917 tmp = regr(sc, MEM_CNTL);
918 #ifdef DIAGNOSTIC
919 aprint_debug_dev(sc->sc_dev, "memctl %08x\n", tmp);
920 #endif
921 if (sc->has_dsp) {
922 tmp &= 0x0000000f;
923 if (tmp < 8)
924 memsize = (tmp + 1) * 512;
925 else if (tmp < 12)
926 memsize = (tmp - 3) * 1024;
927 else
928 memsize = (tmp - 7) * 2048;
929 } else {
930 memsize = mem_tab[tmp & 0x07];
931 }
932
933 return memsize;
934 }
935
936 static int
937 mach64_get_max_ramdac(struct mach64_softc *sc)
938 {
939 int i;
940
941 if ((mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
942 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II) &&
943 (mach64_chip_rev & 0x07))
944 return 170000;
945
946 for (i = 0; i < __arraycount(mach64_info); i++)
947 if (mach64_chip_id == mach64_info[i].chip_id)
948 return mach64_info[i].ramdac_freq;
949
950 if (sc->bits_per_pixel == 8)
951 return 135000;
952 else
953 return 80000;
954 }
955
956 #if defined(__sparc__) || defined(__powerpc__)
957 static void
958 mach64_get_mode(struct mach64_softc *sc, struct videomode *mode)
959 {
960 struct mach64_crtcregs crtc;
961
962 crtc.h_total_disp = regr(sc, CRTC_H_TOTAL_DISP);
963 crtc.h_sync_strt_wid = regr(sc, CRTC_H_SYNC_STRT_WID);
964 crtc.v_total_disp = regr(sc, CRTC_V_TOTAL_DISP);
965 crtc.v_sync_strt_wid = regr(sc, CRTC_V_SYNC_STRT_WID);
966
967 mode->htotal = ((crtc.h_total_disp & 0xffff) + 1) << 3;
968 mode->hdisplay = ((crtc.h_total_disp >> 16) + 1) << 3;
969 mode->hsync_start = ((crtc.h_sync_strt_wid & 0xffff) + 1) << 3;
970 mode->hsync_end = ((crtc.h_sync_strt_wid >> 16) << 3) +
971 mode->hsync_start;
972 mode->vtotal = (crtc.v_total_disp & 0xffff) + 1;
973 mode->vdisplay = (crtc.v_total_disp >> 16) + 1;
974 mode->vsync_start = (crtc.v_sync_strt_wid & 0xffff) + 1;
975 mode->vsync_end = (crtc.v_sync_strt_wid >> 16) + mode->vsync_start;
976
977 #ifdef MACHFB_DEBUG
978 printf("mach64_get_mode: %d %d %d %d %d %d %d %d\n",
979 mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
980 mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal);
981 #endif
982 }
983 #endif
984
985 static int
986 mach64_calc_crtcregs(struct mach64_softc *sc, struct mach64_crtcregs *crtc,
987 struct videomode *mode)
988 {
989
990 if (mode->dot_clock > sc->ramdac_freq)
991 /* Clock too high. */
992 return 1;
993
994 crtc->h_total_disp = (((mode->hdisplay >> 3) - 1) << 16) |
995 ((mode->htotal >> 3) - 1);
996 crtc->h_sync_strt_wid =
997 (((mode->hsync_end - mode->hsync_start) >> 3) << 16) |
998 ((mode->hsync_start >> 3) - 1);
999
1000 crtc->v_total_disp = ((mode->vdisplay - 1) << 16) |
1001 (mode->vtotal - 1);
1002 crtc->v_sync_strt_wid =
1003 ((mode->vsync_end - mode->vsync_start) << 16) |
1004 (mode->vsync_start - 1);
1005
1006 if (mode->flags & VID_NVSYNC)
1007 crtc->v_sync_strt_wid |= CRTC_VSYNC_NEG;
1008
1009 switch (sc->bits_per_pixel) {
1010 case 8:
1011 crtc->color_depth = CRTC_PIX_WIDTH_8BPP;
1012 break;
1013 case 16:
1014 crtc->color_depth = CRTC_PIX_WIDTH_16BPP;
1015 break;
1016 case 32:
1017 crtc->color_depth = CRTC_PIX_WIDTH_32BPP;
1018 break;
1019 }
1020
1021 crtc->gen_cntl = 0;
1022 if (mode->flags & VID_INTERLACE)
1023 crtc->gen_cntl |= CRTC_INTERLACE_EN;
1024
1025 if (mode->flags & VID_CSYNC)
1026 crtc->gen_cntl |= CRTC_CSYNC_EN;
1027
1028 crtc->dot_clock = mode->dot_clock;
1029
1030 return 0;
1031 }
1032
1033 static void
1034 mach64_set_crtcregs(struct mach64_softc *sc, struct mach64_crtcregs *crtc)
1035 {
1036
1037 mach64_set_pll(sc, crtc->dot_clock);
1038
1039 if (sc->has_dsp)
1040 mach64_set_dsp(sc);
1041
1042 regw(sc, CRTC_H_TOTAL_DISP, crtc->h_total_disp);
1043 regw(sc, CRTC_H_SYNC_STRT_WID, crtc->h_sync_strt_wid);
1044 regw(sc, CRTC_V_TOTAL_DISP, crtc->v_total_disp);
1045 regw(sc, CRTC_V_SYNC_STRT_WID, crtc->v_sync_strt_wid);
1046
1047 regw(sc, CRTC_VLINE_CRNT_VLINE, 0);
1048
1049 regw(sc, CRTC_OFF_PITCH, (sc->virt_x >> 3) << 22);
1050
1051 regw(sc, CRTC_GEN_CNTL, crtc->gen_cntl | crtc->color_depth |
1052 /* XXX this unconditionally enables composite sync on SPARC */
1053 #ifdef __sparc__
1054 CRTC_CSYNC_EN |
1055 #endif
1056 CRTC_EXT_DISP_EN | CRTC_EXT_EN);
1057 }
1058
1059 static int
1060 mach64_modeswitch(struct mach64_softc *sc, struct videomode *mode)
1061 {
1062 struct mach64_crtcregs crtc;
1063
1064 memset(&crtc, 0, sizeof crtc); /* XXX gcc */
1065
1066 if (mach64_calc_crtcregs(sc, &crtc, mode))
1067 return 1;
1068 aprint_debug("crtc dot clock: %d\n", crtc.dot_clock);
1069 if (crtc.dot_clock == 0) {
1070 aprint_error("%s: preposterous dot clock (%d)\n",
1071 device_xname(sc->sc_dev), crtc.dot_clock);
1072 return 1;
1073 }
1074 mach64_set_crtcregs(sc, &crtc);
1075 return 0;
1076 }
1077
1078 static void
1079 mach64_reset_engine(struct mach64_softc *sc)
1080 {
1081
1082 /* Reset engine.*/
1083 regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) & ~GUI_ENGINE_ENABLE);
1084
1085 /* Enable engine. */
1086 regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) | GUI_ENGINE_ENABLE);
1087
1088 /* Ensure engine is not locked up by clearing any FIFO or
1089 host errors. */
1090 regw(sc, BUS_CNTL, regr(sc, BUS_CNTL) | BUS_HOST_ERR_ACK |
1091 BUS_FIFO_ERR_ACK);
1092 }
1093
1094 static void
1095 mach64_init_engine(struct mach64_softc *sc)
1096 {
1097 uint32_t pitch_value;
1098
1099 pitch_value = sc->virt_x;
1100
1101 if (sc->bits_per_pixel == 24)
1102 pitch_value *= 3;
1103
1104 mach64_reset_engine(sc);
1105
1106 wait_for_fifo(sc, 14);
1107
1108 regw(sc, CONTEXT_MASK, 0xffffffff);
1109
1110 regw(sc, DST_OFF_PITCH, (pitch_value / 8) << 22);
1111
1112 /* make sure the visible area starts where we're going to draw */
1113 regw(sc, CRTC_OFF_PITCH, (sc->virt_x >> 3) << 22);
1114
1115 regw(sc, DST_Y_X, 0);
1116 regw(sc, DST_HEIGHT, 0);
1117 regw(sc, DST_BRES_ERR, 0);
1118 regw(sc, DST_BRES_INC, 0);
1119 regw(sc, DST_BRES_DEC, 0);
1120
1121 regw(sc, DST_CNTL, DST_LAST_PEL | DST_X_LEFT_TO_RIGHT |
1122 DST_Y_TOP_TO_BOTTOM);
1123
1124 regw(sc, SRC_OFF_PITCH, (pitch_value / 8) << 22);
1125
1126 regw(sc, SRC_Y_X, 0);
1127 regw(sc, SRC_HEIGHT1_WIDTH1, 1);
1128 regw(sc, SRC_Y_X_START, 0);
1129 regw(sc, SRC_HEIGHT2_WIDTH2, 1);
1130
1131 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1132
1133 wait_for_fifo(sc, 13);
1134 regw(sc, HOST_CNTL, 0);
1135
1136 regw(sc, PAT_REG0, 0);
1137 regw(sc, PAT_REG1, 0);
1138 regw(sc, PAT_CNTL, 0);
1139
1140 regw(sc, SC_LEFT, 0);
1141 regw(sc, SC_TOP, 0);
1142 regw(sc, SC_BOTTOM, 0x3fff);
1143 regw(sc, SC_RIGHT, pitch_value - 1);
1144
1145 regw(sc, DP_BKGD_CLR, WS_DEFAULT_BG);
1146 regw(sc, DP_FRGD_CLR, WS_DEFAULT_FG);
1147 regw(sc, DP_WRITE_MASK, 0xffffffff);
1148 regw(sc, DP_MIX, (MIX_SRC << 16) | MIX_DST);
1149
1150 regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
1151
1152 wait_for_fifo(sc, 3);
1153 regw(sc, CLR_CMP_CLR, 0);
1154 regw(sc, CLR_CMP_MASK, 0xffffffff);
1155 regw(sc, CLR_CMP_CNTL, 0);
1156
1157 wait_for_fifo(sc, 3);
1158 switch (sc->bits_per_pixel) {
1159 case 8:
1160 regw(sc, DP_PIX_WIDTH, HOST_1BPP | SRC_8BPP | DST_8BPP);
1161 regw(sc, DP_CHAIN_MASK, DP_CHAIN_8BPP);
1162 /* We want 8 bit per channel */
1163 regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1164 break;
1165 case 32:
1166 regw(sc, DP_PIX_WIDTH, HOST_1BPP | SRC_32BPP | DST_32BPP);
1167 regw(sc, DP_CHAIN_MASK, DP_CHAIN_32BPP);
1168 regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
1169 break;
1170 }
1171 regw(sc, DP_WRITE_MASK, 0xff);
1172
1173 wait_for_fifo(sc, 5);
1174 regw(sc, CRTC_INT_CNTL, regr(sc, CRTC_INT_CNTL) & ~0x20);
1175 regw(sc, GUI_TRAJ_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
1176
1177 wait_for_idle(sc);
1178 }
1179
1180 #if 0
1181 static void
1182 mach64_adjust_frame(struct mach64_softc *sc, int x, int y)
1183 {
1184 int offset;
1185
1186 offset = ((x + y * sc->virt_x) * (sc->bits_per_pixel >> 3)) >> 3;
1187
1188 regw(sc, CRTC_OFF_PITCH, (regr(sc, CRTC_OFF_PITCH) & 0xfff00000) |
1189 offset);
1190 }
1191 #endif
1192
1193 static void
1194 mach64_set_dsp(struct mach64_softc *sc)
1195 {
1196 uint32_t fifo_depth, page_size, dsp_precision, dsp_loop_latency;
1197 uint32_t dsp_off, dsp_on, dsp_xclks_per_qw;
1198 uint32_t xclks_per_qw, y;
1199 uint32_t fifo_off, fifo_on;
1200
1201 aprint_normal_dev(sc->sc_dev, "initializing the DSP\n");
1202
1203 if (mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
1204 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II ||
1205 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIP ||
1206 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_PCI ||
1207 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_AGP_B ||
1208 mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_AGP_P) {
1209 dsp_loop_latency = 0;
1210 fifo_depth = 24;
1211 } else {
1212 dsp_loop_latency = 2;
1213 fifo_depth = 32;
1214 }
1215
1216 dsp_precision = 0;
1217 xclks_per_qw = (sc->mclk_fb_div * sc->vclk_post_div * 64 << 11) /
1218 (sc->vclk_fb_div * sc->mclk_post_div * sc->bits_per_pixel);
1219 y = (xclks_per_qw * fifo_depth) >> 11;
1220 while (y) {
1221 y >>= 1;
1222 dsp_precision++;
1223 }
1224 dsp_precision -= 5;
1225 fifo_off = ((xclks_per_qw * (fifo_depth - 1)) >> 5) + (3 << 6);
1226
1227 switch (sc->memtype) {
1228 case DRAM:
1229 case EDO_DRAM:
1230 case PSEUDO_EDO:
1231 if (sc->memsize > 1024) {
1232 page_size = 9;
1233 dsp_loop_latency += 6;
1234 } else {
1235 page_size = 10;
1236 if (sc->memtype == DRAM)
1237 dsp_loop_latency += 8;
1238 else
1239 dsp_loop_latency += 7;
1240 }
1241 break;
1242 case SDRAM:
1243 case SGRAM:
1244 if (sc->memsize > 1024) {
1245 page_size = 8;
1246 dsp_loop_latency += 8;
1247 } else {
1248 page_size = 10;
1249 dsp_loop_latency += 9;
1250 }
1251 break;
1252 default:
1253 page_size = 10;
1254 dsp_loop_latency += 9;
1255 break;
1256 }
1257
1258 if (xclks_per_qw >= (page_size << 11))
1259 fifo_on = ((2 * page_size + 1) << 6) + (xclks_per_qw >> 5);
1260 else
1261 fifo_on = (3 * page_size + 2) << 6;
1262
1263 dsp_xclks_per_qw = xclks_per_qw >> dsp_precision;
1264 dsp_on = fifo_on >> dsp_precision;
1265 dsp_off = fifo_off >> dsp_precision;
1266
1267 #ifdef MACHFB_DEBUG
1268 printf("dsp_xclks_per_qw = %d, dsp_on = %d, dsp_off = %d,\n"
1269 "dsp_precision = %d, dsp_loop_latency = %d,\n"
1270 "mclk_fb_div = %d, vclk_fb_div = %d,\n"
1271 "mclk_post_div = %d, vclk_post_div = %d\n",
1272 dsp_xclks_per_qw, dsp_on, dsp_off, dsp_precision, dsp_loop_latency,
1273 sc->mclk_fb_div, sc->vclk_fb_div,
1274 sc->mclk_post_div, sc->vclk_post_div);
1275 #endif
1276
1277 regw(sc, DSP_ON_OFF, ((dsp_on << 16) & DSP_ON) | (dsp_off & DSP_OFF));
1278 regw(sc, DSP_CONFIG, ((dsp_precision << 20) & DSP_PRECISION) |
1279 ((dsp_loop_latency << 16) & DSP_LOOP_LATENCY) |
1280 (dsp_xclks_per_qw & DSP_XCLKS_PER_QW));
1281 }
1282
1283 static void
1284 mach64_set_pll(struct mach64_softc *sc, int clock)
1285 {
1286 uint32_t q, clockreg;
1287 int clockshift = sc->sc_clock << 1;
1288 uint8_t reg, vclk_ctl;
1289
1290 q = (clock * sc->ref_div * 100) / (2 * sc->ref_freq);
1291 #ifdef MACHFB_DEBUG
1292 printf("q = %d\n", q);
1293 #endif
1294 if (q > 25500) {
1295 aprint_error_dev(sc->sc_dev, "Warning: q > 25500\n");
1296 q = 25500;
1297 sc->vclk_post_div = 1;
1298 sc->log2_vclk_post_div = 0;
1299 } else if (q > 12750) {
1300 sc->vclk_post_div = 1;
1301 sc->log2_vclk_post_div = 0;
1302 } else if (q > 6350) {
1303 sc->vclk_post_div = 2;
1304 sc->log2_vclk_post_div = 1;
1305 } else if (q > 3150) {
1306 sc->vclk_post_div = 4;
1307 sc->log2_vclk_post_div = 2;
1308 } else if (q >= 1600) {
1309 sc->vclk_post_div = 8;
1310 sc->log2_vclk_post_div = 3;
1311 } else {
1312 aprint_error_dev(sc->sc_dev, "Warning: q < 1600\n");
1313 sc->vclk_post_div = 8;
1314 sc->log2_vclk_post_div = 3;
1315 }
1316 sc->vclk_fb_div = q * sc->vclk_post_div / 100;
1317 aprint_debug("post_div: %d log2_post_div: %d mclk_div: %d\n",
1318 sc->vclk_post_div, sc->log2_vclk_post_div, sc->mclk_fb_div);
1319
1320 vclk_ctl = regrb_pll(sc, PLL_VCLK_CNTL);
1321 aprint_debug("vclk_ctl: %02x\n", vclk_ctl);
1322 vclk_ctl |= PLL_VCLK_RESET;
1323 regwb_pll(sc, PLL_VCLK_CNTL, vclk_ctl);
1324
1325 regwb_pll(sc, MCLK_FB_DIV, sc->mclk_fb_div);
1326 reg = regrb_pll(sc, VCLK_POST_DIV);
1327 reg &= ~(3 << clockshift);
1328 reg |= (sc->log2_vclk_post_div << clockshift);
1329 regwb_pll(sc, VCLK_POST_DIV, reg);
1330 regwb_pll(sc, VCLK0_FB_DIV + sc->sc_clock, sc->vclk_fb_div);
1331
1332 vclk_ctl &= ~PLL_VCLK_RESET;
1333 regwb_pll(sc, PLL_VCLK_CNTL, vclk_ctl);
1334
1335 clockreg = regr(sc, CLOCK_CNTL);
1336 clockreg &= ~CLOCK_SEL;
1337 clockreg |= sc->sc_clock | CLOCK_STROBE;
1338 regw(sc, CLOCK_CNTL, clockreg);
1339 }
1340
1341 static void
1342 mach64_init_lut(struct mach64_softc *sc)
1343 {
1344 uint8_t cmap[768];
1345 int i, idx;
1346
1347 rasops_get_cmap(&mach64_console_screen.scr_ri, cmap, sizeof(cmap));
1348 idx = 0;
1349 for (i = 0; i < 256; i++) {
1350 mach64_putpalreg(sc, i, cmap[idx], cmap[idx + 1],
1351 cmap[idx + 2]);
1352 idx += 3;
1353 }
1354 }
1355
1356 static int
1357 mach64_putpalreg(struct mach64_softc *sc, uint8_t index, uint8_t r, uint8_t g,
1358 uint8_t b)
1359 {
1360 sc->sc_cmap_red[index] = r;
1361 sc->sc_cmap_green[index] = g;
1362 sc->sc_cmap_blue[index] = b;
1363 /*
1364 * writing the dac index takes a while, in theory we can poll some
1365 * register to see when it's ready - but we better avoid writing it
1366 * unnecessarily
1367 */
1368 if (index != sc->sc_dacw) {
1369 regwb(sc, DAC_MASK, 0xff);
1370 regwb(sc, DAC_WINDEX, index);
1371 }
1372 sc->sc_dacw = index + 1;
1373 regwb(sc, DAC_DATA, r);
1374 regwb(sc, DAC_DATA, g);
1375 regwb(sc, DAC_DATA, b);
1376 return 0;
1377 }
1378
1379 static int
1380 mach64_putcmap(struct mach64_softc *sc, struct wsdisplay_cmap *cm)
1381 {
1382 uint index = cm->index;
1383 uint count = cm->count;
1384 int i, error;
1385 uint8_t rbuf[256], gbuf[256], bbuf[256];
1386 uint8_t *r, *g, *b;
1387
1388 if (cm->index >= 256 || cm->count > 256 ||
1389 (cm->index + cm->count) > 256)
1390 return EINVAL;
1391 error = copyin(cm->red, &rbuf[index], count);
1392 if (error)
1393 return error;
1394 error = copyin(cm->green, &gbuf[index], count);
1395 if (error)
1396 return error;
1397 error = copyin(cm->blue, &bbuf[index], count);
1398 if (error)
1399 return error;
1400
1401 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
1402 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
1403 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
1404
1405 r = &sc->sc_cmap_red[index];
1406 g = &sc->sc_cmap_green[index];
1407 b = &sc->sc_cmap_blue[index];
1408
1409 for (i = 0; i < count; i++) {
1410 mach64_putpalreg(sc, index, *r, *g, *b);
1411 index++;
1412 r++, g++, b++;
1413 }
1414 return 0;
1415 }
1416
1417 static int
1418 mach64_getcmap(struct mach64_softc *sc, struct wsdisplay_cmap *cm)
1419 {
1420 u_int index = cm->index;
1421 u_int count = cm->count;
1422 int error;
1423
1424 if (index >= 255 || count > 256 || index + count > 256)
1425 return EINVAL;
1426
1427 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
1428 if (error)
1429 return error;
1430 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
1431 if (error)
1432 return error;
1433 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
1434 if (error)
1435 return error;
1436
1437 return 0;
1438 }
1439
1440 static int
1441 mach64_set_screentype(struct mach64_softc *sc, const struct wsscreen_descr *des)
1442 {
1443 struct mach64_crtcregs regs;
1444
1445 if (mach64_calc_crtcregs(sc, ®s,
1446 (struct videomode *)des->modecookie))
1447 return 1;
1448
1449 mach64_set_crtcregs(sc, ®s);
1450 return 0;
1451 }
1452
1453 static int
1454 mach64_is_console(struct mach64_softc *sc)
1455 {
1456 bool console = 0;
1457
1458 prop_dictionary_get_bool(device_properties(sc->sc_dev),
1459 "is_console", &console);
1460 return console;
1461 }
1462
1463 /*
1464 * wsdisplay_emulops
1465 */
1466
1467 static void
1468 mach64_cursor(void *cookie, int on, int row, int col)
1469 {
1470 struct rasops_info *ri = cookie;
1471 struct vcons_screen *scr = ri->ri_hw;
1472 struct mach64_softc *sc = scr->scr_cookie;
1473 int x, y, wi, he;
1474
1475 wi = ri->ri_font->fontwidth;
1476 he = ri->ri_font->fontheight;
1477
1478 if ((!sc->sc_locked) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1479 x = ri->ri_ccol * wi + ri->ri_xorigin;
1480 y = ri->ri_crow * he + ri->ri_yorigin;
1481 if (ri->ri_flg & RI_CURSOR) {
1482 mach64_bitblt(sc, x, y, x, y, wi, he, MIX_NOT_SRC);
1483 ri->ri_flg &= ~RI_CURSOR;
1484 }
1485 ri->ri_crow = row;
1486 ri->ri_ccol = col;
1487 if (on) {
1488 x = ri->ri_ccol * wi + ri->ri_xorigin;
1489 y = ri->ri_crow * he + ri->ri_yorigin;
1490 mach64_bitblt(sc, x, y, x, y, wi, he, MIX_NOT_SRC);
1491 ri->ri_flg |= RI_CURSOR;
1492 }
1493 } else {
1494 scr->scr_ri.ri_crow = row;
1495 scr->scr_ri.ri_ccol = col;
1496 scr->scr_ri.ri_flg &= ~RI_CURSOR;
1497 }
1498 }
1499
1500 #if 0
1501 static int
1502 mach64_mapchar(void *cookie, int uni, u_int *index)
1503 {
1504 return 0;
1505 }
1506 #endif
1507
1508 static void
1509 mach64_putchar_mono(void *cookie, int row, int col, u_int c, long attr)
1510 {
1511 struct rasops_info *ri = cookie;
1512 struct wsdisplay_font *font = PICK_FONT(ri, c);
1513 struct vcons_screen *scr = ri->ri_hw;
1514 struct mach64_softc *sc = scr->scr_cookie;
1515
1516 if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
1517 int fg, bg, uc;
1518 uint8_t *data;
1519 int x, y, wi, he;
1520 wi = font->fontwidth;
1521 he = font->fontheight;
1522
1523 if (!CHAR_IN_FONT(c, font))
1524 return;
1525 bg = ri->ri_devcmap[(attr >> 16) & 0x0f];
1526 fg = ri->ri_devcmap[(attr >> 24) & 0x0f];
1527 x = ri->ri_xorigin + col * wi;
1528 y = ri->ri_yorigin + row * he;
1529 if (c == 0x20) {
1530 mach64_rectfill(sc, x, y, wi, he, bg);
1531 } else {
1532 uc = c - font->firstchar;
1533 data = (uint8_t *)font->data + uc *
1534 ri->ri_fontscale;
1535
1536 mach64_setup_mono(sc, x, y, wi, he, fg, bg);
1537 mach64_feed_bytes(sc, ri->ri_fontscale, data);
1538 }
1539 }
1540 }
1541
1542 static void
1543 mach64_putchar_aa8(void *cookie, int row, int col, u_int c, long attr)
1544 {
1545 struct rasops_info *ri = cookie;
1546 struct wsdisplay_font *font = PICK_FONT(ri, c);
1547 struct vcons_screen *scr = ri->ri_hw;
1548 struct mach64_softc *sc = scr->scr_cookie;
1549 uint32_t bg, latch = 0, bg8, fg8, pixel;
1550 int i, x, y, wi, he, r, g, b, aval;
1551 int r1, g1, b1, r0, g0, b0, fgo, bgo;
1552 uint8_t *data8;
1553 int rv = 0, cnt = 0;
1554
1555 if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
1556 return;
1557
1558 if (!CHAR_IN_FONT(c, font))
1559 return;
1560
1561 wi = font->fontwidth;
1562 he = font->fontheight;
1563 bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0x0f];
1564 x = ri->ri_xorigin + col * wi;
1565 y = ri->ri_yorigin + row * he;
1566
1567 if (c == 0x20) {
1568 mach64_rectfill(sc, x, y, wi, he, bg);
1569 return;
1570 }
1571
1572 rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
1573 if (rv == GC_OK)
1574 return;
1575
1576 data8 = WSFONT_GLYPH(c, font);
1577
1578 wait_for_fifo(sc, 11);
1579 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
1580 regw(sc, DP_SRC, MONO_SRC_ONE | BKGD_SRC_HOST | FRGD_SRC_HOST);
1581 regw(sc, DP_MIX, ((MIX_SRC & 0xffff) << 16) | MIX_SRC);
1582 regw(sc, CLR_CMP_CNTL ,0); /* no transparency */
1583 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1584 regw(sc, DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT);
1585 regw(sc, HOST_CNTL, HOST_BYTE_ALIGN);
1586 regw(sc, SRC_Y_X, 0);
1587 regw(sc, SRC_WIDTH1, wi);
1588 regw(sc, DST_Y_X, (x << 16) | y);
1589 regw(sc, DST_HEIGHT_WIDTH, (wi << 16) | he);
1590
1591 /*
1592 * we need the RGB colours here, so get offsets into rasops_cmap
1593 */
1594 fgo = ((attr >> 24) & 0xf) * 3;
1595 bgo = ((attr >> 16) & 0xf) * 3;
1596
1597 r0 = rasops_cmap[bgo];
1598 r1 = rasops_cmap[fgo];
1599 g0 = rasops_cmap[bgo + 1];
1600 g1 = rasops_cmap[fgo + 1];
1601 b0 = rasops_cmap[bgo + 2];
1602 b1 = rasops_cmap[fgo + 2];
1603 #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
1604 bg8 = R3G3B2(r0, g0, b0);
1605 fg8 = R3G3B2(r1, g1, b1);
1606
1607 wait_for_fifo(sc, 10);
1608
1609 for (i = 0; i < ri->ri_fontscale; i++) {
1610 aval = *data8;
1611 if (aval == 0) {
1612 pixel = bg8;
1613 } else if (aval == 255) {
1614 pixel = fg8;
1615 } else {
1616 r = aval * r1 + (255 - aval) * r0;
1617 g = aval * g1 + (255 - aval) * g0;
1618 b = aval * b1 + (255 - aval) * b0;
1619 pixel = ((r & 0xe000) >> 8) |
1620 ((g & 0xe000) >> 11) |
1621 ((b & 0xc000) >> 14);
1622 }
1623 latch = (latch << 8) | pixel;
1624 /* write in 32bit chunks */
1625 if ((i & 3) == 3) {
1626 regws(sc, HOST_DATA0, latch);
1627 /*
1628 * not strictly necessary, old data should be shifted
1629 * out
1630 */
1631 latch = 0;
1632 cnt++;
1633 if (cnt > 8) {
1634 wait_for_fifo(sc, 10);
1635 cnt = 0;
1636 }
1637 }
1638 data8++;
1639 }
1640 /* if we have pixels left in latch write them out */
1641 if ((i & 3) != 0) {
1642 latch = latch << ((4 - (i & 3)) << 3);
1643 regws(sc, HOST_DATA0, latch);
1644 }
1645
1646 if (rv == GC_ADD) {
1647 glyphcache_add(&sc->sc_gc, c, x, y);
1648 }
1649 }
1650
1651 static void
1652 mach64_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
1653 {
1654 struct rasops_info *ri = cookie;
1655 struct vcons_screen *scr = ri->ri_hw;
1656 struct mach64_softc *sc = scr->scr_cookie;
1657 int32_t xs, xd, y, width, height;
1658
1659 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1660 xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
1661 xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
1662 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1663 width = ri->ri_font->fontwidth * ncols;
1664 height = ri->ri_font->fontheight;
1665 mach64_bitblt(sc, xs, y, xd, y, width, height, MIX_SRC);
1666 }
1667 }
1668
1669 static void
1670 mach64_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
1671 {
1672 struct rasops_info *ri = cookie;
1673 struct vcons_screen *scr = ri->ri_hw;
1674 struct mach64_softc *sc = scr->scr_cookie;
1675 int32_t x, y, width, height, fg, bg, ul;
1676
1677 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1678 x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
1679 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1680 width = ri->ri_font->fontwidth * ncols;
1681 height = ri->ri_font->fontheight;
1682 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1683
1684 mach64_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1685 }
1686 }
1687
1688 static void
1689 mach64_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
1690 {
1691 struct rasops_info *ri = cookie;
1692 struct vcons_screen *scr = ri->ri_hw;
1693 struct mach64_softc *sc = scr->scr_cookie;
1694 int32_t x, ys, yd, width, height;
1695
1696 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1697 x = ri->ri_xorigin;
1698 ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
1699 yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
1700 width = ri->ri_emuwidth;
1701 height = ri->ri_font->fontheight*nrows;
1702 mach64_bitblt(sc, x, ys, x, yd, width, height, MIX_SRC);
1703 }
1704 }
1705
1706 static void
1707 mach64_eraserows(void *cookie, int row, int nrows, long fillattr)
1708 {
1709 struct rasops_info *ri = cookie;
1710 struct vcons_screen *scr = ri->ri_hw;
1711 struct mach64_softc *sc = scr->scr_cookie;
1712 int32_t x, y, width, height, fg, bg, ul;
1713
1714 if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
1715 if ((row == 0) && (nrows == ri->ri_rows)) {
1716 /* clear full screen */
1717 x = 0;
1718 y = 0;
1719 width = sc->virt_x;
1720 height = sc->virt_y;
1721 } else {
1722 x = ri->ri_xorigin;
1723 y = ri->ri_yorigin + ri->ri_font->fontheight * row;
1724 width = ri->ri_emuwidth;
1725 height = ri->ri_font->fontheight * nrows;
1726 }
1727 rasops_unpack_attr(fillattr, &fg, &bg, &ul);
1728
1729 mach64_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
1730 }
1731 }
1732
1733 static void
1734 mach64_bitblt(void *cookie, int xs, int ys, int xd, int yd, int width, int height, int rop)
1735 {
1736 struct mach64_softc *sc = cookie;
1737 uint32_t dest_ctl = 0;
1738
1739 wait_for_fifo(sc, 10);
1740 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
1741 regw(sc, DP_SRC, FRGD_SRC_BLIT);
1742 regw(sc, DP_MIX, (rop & 0xffff) << 16);
1743 regw(sc, CLR_CMP_CNTL, 0); /* no transparency */
1744 if (yd < ys) {
1745 dest_ctl = DST_Y_TOP_TO_BOTTOM;
1746 } else {
1747 ys += height - 1;
1748 yd += height - 1;
1749 dest_ctl = DST_Y_BOTTOM_TO_TOP;
1750 }
1751 if (xd < xs) {
1752 dest_ctl |= DST_X_LEFT_TO_RIGHT;
1753 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1754 } else {
1755 dest_ctl |= DST_X_RIGHT_TO_LEFT;
1756 xs += width - 1;
1757 xd += width - 1;
1758 regw(sc, SRC_CNTL, SRC_LINE_X_RIGHT_TO_LEFT);
1759 }
1760 regw(sc, DST_CNTL, dest_ctl);
1761
1762 regw(sc, SRC_Y_X, (xs << 16) | ys);
1763 regw(sc, SRC_WIDTH1, width);
1764 regw(sc, DST_Y_X, (xd << 16) | yd);
1765 regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
1766 }
1767
1768 static void
1769 mach64_setup_mono(struct mach64_softc *sc, int xd, int yd, int width,
1770 int height, uint32_t fg, uint32_t bg)
1771 {
1772 wait_for_idle(sc);
1773 regw(sc, DP_WRITE_MASK, 0xff); /* XXX only good for 8 bit */
1774 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_1BPP | HOST_1BPP);
1775 regw(sc, DP_SRC, MONO_SRC_HOST | BKGD_SRC_BKGD_CLR | FRGD_SRC_FRGD_CLR);
1776 regw(sc, DP_MIX, ((MIX_SRC & 0xffff) << 16) | MIX_SRC);
1777 regw(sc, CLR_CMP_CNTL ,0); /* no transparency */
1778 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1779 regw(sc, DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT);
1780 regw(sc, HOST_CNTL, HOST_BYTE_ALIGN);
1781 regw(sc, DP_BKGD_CLR, bg);
1782 regw(sc, DP_FRGD_CLR, fg);
1783 regw(sc, SRC_Y_X, 0);
1784 regw(sc, SRC_WIDTH1, width);
1785 regw(sc, DST_Y_X, (xd << 16) | yd);
1786 regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
1787 /* now feed the data into the chip */
1788 }
1789
1790 static void
1791 mach64_feed_bytes(struct mach64_softc *sc, int count, uint8_t *data)
1792 {
1793 int i;
1794 uint32_t latch = 0, bork;
1795 int shift = 0;
1796 int reg = 0;
1797
1798 for (i = 0; i < count; i++) {
1799 bork = data[i];
1800 latch |= (bork << shift);
1801 if (shift == 24) {
1802 regw(sc, HOST_DATA0 + reg, latch);
1803 latch = 0;
1804 shift = 0;
1805 reg = (reg + 4) & 0x3c;
1806 } else
1807 shift += 8;
1808 }
1809 if (shift != 0) /* 24 */
1810 regw(sc, HOST_DATA0 + reg, latch);
1811 }
1812
1813
1814 static void
1815 mach64_rectfill(struct mach64_softc *sc, int x, int y, int width, int height,
1816 int colour)
1817 {
1818 wait_for_fifo(sc, 11);
1819 regw(sc, DP_FRGD_CLR, colour);
1820 regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
1821 regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
1822 regw(sc, DP_MIX, MIX_SRC << 16);
1823 regw(sc, CLR_CMP_CNTL, 0); /* no transparency */
1824 regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
1825 regw(sc, DST_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
1826
1827 regw(sc, SRC_Y_X, (x << 16) | y);
1828 regw(sc, SRC_WIDTH1, width);
1829 regw(sc, DST_Y_X, (x << 16) | y);
1830 regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
1831 }
1832
1833 static void
1834 mach64_clearscreen(struct mach64_softc *sc)
1835 {
1836 mach64_rectfill(sc, 0, 0, sc->virt_x, sc->virt_y, sc->sc_bg);
1837 }
1838
1839
1840 #if 0
1841 static void
1842 mach64_showpal(struct mach64_softc *sc)
1843 {
1844 int i, x = 0;
1845
1846 for (i = 0; i < 16; i++) {
1847 mach64_rectfill(sc, x, 0, 64, 64, i);
1848 x += 64;
1849 }
1850 }
1851 #endif
1852
1853 /*
1854 * wsdisplay_accessops
1855 */
1856
1857 static int
1858 mach64_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
1859 struct lwp *l)
1860 {
1861 struct vcons_data *vd = v;
1862 struct mach64_softc *sc = vd->cookie;
1863 struct wsdisplay_fbinfo *wdf;
1864 struct vcons_screen *ms = vd->active;
1865
1866 switch (cmd) {
1867 case WSDISPLAYIO_GTYPE:
1868 *(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
1869 return 0;
1870
1871 case WSDISPLAYIO_LINEBYTES:
1872 *(u_int *)data = sc->virt_x * sc->bits_per_pixel / 8;
1873 return 0;
1874
1875 case WSDISPLAYIO_GINFO:
1876 wdf = (void *)data;
1877 wdf->height = sc->virt_y;
1878 wdf->width = sc->virt_x;
1879 wdf->depth = sc->bits_per_pixel;
1880 wdf->cmsize = 256;
1881 return 0;
1882
1883 case WSDISPLAYIO_GETCMAP:
1884 return mach64_getcmap(sc,
1885 (struct wsdisplay_cmap *)data);
1886
1887 case WSDISPLAYIO_PUTCMAP:
1888 return mach64_putcmap(sc,
1889 (struct wsdisplay_cmap *)data);
1890
1891 /* PCI config read/write passthrough. */
1892 case PCI_IOC_CFGREAD:
1893 case PCI_IOC_CFGWRITE:
1894 return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
1895 cmd, data, flag, l);
1896
1897 case WSDISPLAYIO_GET_BUSID:
1898 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
1899 sc->sc_pcitag, data);
1900
1901 case WSDISPLAYIO_SMODE: {
1902 int new_mode = *(int*)data;
1903 if (new_mode != sc->sc_mode) {
1904 sc->sc_mode = new_mode;
1905 if ((new_mode == WSDISPLAYIO_MODE_EMUL)
1906 && (ms != NULL))
1907 {
1908 /* restore initial video mode */
1909 mach64_init(sc);
1910 mach64_init_engine(sc);
1911 mach64_init_lut(sc);
1912 mach64_modeswitch(sc, sc->sc_my_mode);
1913 mach64_clearscreen(sc);
1914 glyphcache_wipe(&sc->sc_gc);
1915 vcons_redraw_screen(ms);
1916 }
1917 }
1918 }
1919 return 0;
1920 case WSDISPLAYIO_GET_EDID: {
1921 struct wsdisplayio_edid_info *d = data;
1922 return wsdisplayio_get_edid(sc->sc_dev, d);
1923 }
1924
1925 case WSDISPLAYIO_GET_FBINFO: {
1926 struct wsdisplayio_fbinfo *fbi = data;
1927 return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
1928 }
1929 }
1930 return EPASSTHROUGH;
1931 }
1932
1933 static paddr_t
1934 mach64_mmap(void *v, void *vs, off_t offset, int prot)
1935 {
1936 struct vcons_data *vd = v;
1937 struct mach64_softc *sc = vd->cookie;
1938 paddr_t pa;
1939
1940 if (sc->sc_mode == WSDISPLAYIO_MODE_DUMBFB) {
1941 /*
1942 *'regular' framebuffer mmap()ing
1943 */
1944 if (offset < (sc->memsize * 1024)) {
1945 pa = bus_space_mmap(sc->sc_memt, sc->sc_aperbase,
1946 offset, prot, BUS_SPACE_MAP_LINEAR);
1947 return pa;
1948 }
1949 } else if (sc->sc_mode == WSDISPLAYIO_MODE_MAPPED) {
1950 /*
1951 * restrict all other mappings to processes with superuser
1952 * privileges
1953 */
1954 if (kauth_authorize_machdep(kauth_cred_get(),
1955 KAUTH_MACHDEP_UNMANAGEDMEM,
1956 NULL, NULL, NULL, NULL) != 0) {
1957 return -1;
1958 }
1959 if ((offset >= sc->sc_aperbase) &&
1960 (offset < (sc->sc_aperbase + sc->sc_apersize))) {
1961 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1962 BUS_SPACE_MAP_LINEAR);
1963 return pa;
1964 }
1965
1966 if ((offset >= sc->sc_regbase) &&
1967 (offset < (sc->sc_regbase + sc->sc_regsize))) {
1968 pa = bus_space_mmap(sc->sc_regt, offset, 0, prot,
1969 BUS_SPACE_MAP_LINEAR);
1970 return pa;
1971 }
1972
1973 if ((offset >= sc->sc_rom.vb_base) &&
1974 (offset < (sc->sc_rom.vb_base + sc->sc_rom.vb_size))) {
1975 pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
1976 BUS_SPACE_MAP_LINEAR);
1977 return pa;
1978 }
1979
1980 #ifdef PCI_MAGIC_IO_RANGE
1981 if ((offset >= PCI_MAGIC_IO_RANGE) &&
1982 (offset <= PCI_MAGIC_IO_RANGE + 0x10000)) {
1983 return bus_space_mmap(sc->sc_iot,
1984 offset - PCI_MAGIC_IO_RANGE, 0, prot, 0);
1985 }
1986 #endif
1987 }
1988 return -1;
1989 }
1990
1991 #if 0
1992 static int
1993 mach64_load_font(void *v, void *cookie, struct wsdisplay_font *data)
1994 {
1995
1996 return 0;
1997 }
1998 #endif
1999
2000 void
2001 machfb_blank(struct mach64_softc *sc, int blank)
2002 {
2003 uint32_t reg;
2004
2005 #define MACH64_BLANK (CRTC_DISPLAY_DIS | CRTC_HSYNC_DIS | CRTC_VSYNC_DIS)
2006
2007 switch (blank)
2008 {
2009 case 0:
2010 reg = regr(sc, CRTC_GEN_CNTL);
2011 regw(sc, CRTC_GEN_CNTL, reg & ~(MACH64_BLANK));
2012 sc->sc_blanked = 0;
2013 break;
2014 case 1:
2015 reg = regr(sc, CRTC_GEN_CNTL);
2016 regw(sc, CRTC_GEN_CNTL, reg | (MACH64_BLANK));
2017 sc->sc_blanked = 1;
2018 break;
2019 default:
2020 break;
2021 }
2022 }
2023
2024 /* framebuffer device support */
2025 #ifdef __sparc__
2026
2027 static void
2028 machfb_unblank(device_t dev)
2029 {
2030 struct mach64_softc *sc = device_private(dev);
2031
2032 machfb_blank(sc, 0);
2033 }
2034
2035 static void
2036 machfb_fbattach(struct mach64_softc *sc)
2037 {
2038 struct fbdevice *fb = &sc->sc_fb;
2039
2040 fb->fb_device = sc->sc_dev;
2041 fb->fb_driver = &machfb_fbdriver;
2042
2043 fb->fb_type.fb_cmsize = 256;
2044 fb->fb_type.fb_size = sc->memsize;
2045
2046 fb->fb_type.fb_type = FBTYPE_GENERIC_PCI;
2047 fb->fb_flags = device_cfdata(sc->sc_dev)->cf_flags & FB_USERMASK;
2048 fb->fb_type.fb_depth = sc->bits_per_pixel;
2049 fb->fb_type.fb_width = sc->virt_x;
2050 fb->fb_type.fb_height = sc->virt_y;
2051
2052 fb_attach(fb, sc->sc_console);
2053 }
2054
2055 int
2056 machfb_fbopen(dev_t dev, int flags, int mode, struct lwp *l)
2057 {
2058 struct mach64_softc *sc;
2059
2060 sc = device_lookup_private(&machfb_cd, minor(dev));
2061 if (sc == NULL)
2062 return ENXIO;
2063 sc->sc_locked = 1;
2064
2065 #ifdef MACHFB_DEBUG
2066 printf("machfb_fbopen(%d)\n", minor(dev));
2067 #endif
2068 return 0;
2069 }
2070
2071 int
2072 machfb_fbclose(dev_t dev, int flags, int mode, struct lwp *l)
2073 {
2074 struct mach64_softc *sc = device_lookup_private(&machfb_cd, minor(dev));
2075
2076 #ifdef MACHFB_DEBUG
2077 printf("machfb_fbclose()\n");
2078 #endif
2079 mach64_init_engine(sc);
2080 mach64_init_lut(sc);
2081 sc->sc_locked = 0;
2082 return 0;
2083 }
2084
2085 int
2086 machfb_fbioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
2087 {
2088 struct mach64_softc *sc = device_lookup_private(&machfb_cd, minor(dev));
2089
2090 #ifdef MACHFB_DEBUG
2091 printf("machfb_fbioctl(%d, %lx)\n", minor(dev), cmd);
2092 #endif
2093 switch (cmd) {
2094 case FBIOGTYPE:
2095 *(struct fbtype *)data = sc->sc_fb.fb_type;
2096 break;
2097
2098 case FBIOGATTR:
2099 #define fba ((struct fbgattr *)data)
2100 fba->real_type = sc->sc_fb.fb_type.fb_type;
2101 fba->owner = 0; /* XXX ??? */
2102 fba->fbtype = sc->sc_fb.fb_type;
2103 fba->sattr.flags = 0;
2104 fba->sattr.emu_type = sc->sc_fb.fb_type.fb_type;
2105 fba->sattr.dev_specific[0] = sc->sc_nbus;
2106 fba->sattr.dev_specific[1] = sc->sc_ndev;
2107 fba->sattr.dev_specific[2] = sc->sc_nfunc;
2108 fba->sattr.dev_specific[3] = -1;
2109 fba->emu_types[0] = sc->sc_fb.fb_type.fb_type;
2110 fba->emu_types[1] = -1;
2111 #undef fba
2112 break;
2113
2114 #if 0
2115 case FBIOGETCMAP:
2116 #define p ((struct fbcmap *)data)
2117 return bt_getcmap(p, &sc->sc_cmap, 256, 1);
2118
2119 case FBIOPUTCMAP:
2120 /* copy to software map */
2121 error = bt_putcmap(p, &sc->sc_cmap, 256, 1);
2122 if (error)
2123 return error;
2124 /* now blast them into the chip */
2125 /* XXX should use retrace interrupt */
2126 cg6_loadcmap(sc, p->index, p->count);
2127 #undef p
2128 break;
2129 #endif
2130 case FBIOGVIDEO:
2131 *(int *)data = sc->sc_blanked;
2132 break;
2133
2134 case FBIOSVIDEO:
2135 machfb_blank(sc, *(int *)data);
2136 break;
2137
2138 #if 0
2139 case FBIOGCURSOR:
2140 break;
2141
2142 case FBIOSCURSOR:
2143 break;
2144
2145 case FBIOGCURPOS:
2146 *(struct fbcurpos *)data = sc->sc_cursor.cc_pos;
2147 break;
2148
2149 case FBIOSCURPOS:
2150 sc->sc_cursor.cc_pos = *(struct fbcurpos *)data;
2151 break;
2152
2153 case FBIOGCURMAX:
2154 /* max cursor size is 32x32 */
2155 ((struct fbcurpos *)data)->x = 32;
2156 ((struct fbcurpos *)data)->y = 32;
2157 break;
2158 #endif
2159 case PCI_IOC_CFGREAD:
2160 case PCI_IOC_CFGWRITE: {
2161 int ret;
2162 ret = pci_devioctl(sc->sc_pc, sc->sc_pcitag,
2163 cmd, data, flags, l);
2164
2165 #ifdef MACHFB_DEBUG
2166 printf("pci_devioctl: %d\n", ret);
2167 #endif
2168 return ret;
2169 }
2170
2171 case WSDISPLAYIO_GET_BUSID:
2172 return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
2173 sc->sc_pcitag, data);
2174
2175 default:
2176 return ENOTTY;
2177 }
2178 #ifdef MACHFB_DEBUG
2179 printf("machfb_fbioctl done\n");
2180 #endif
2181 return 0;
2182 }
2183
2184 paddr_t
2185 machfb_fbmmap(dev_t dev, off_t off, int prot)
2186 {
2187 struct mach64_softc *sc = device_lookup_private(&machfb_cd, minor(dev));
2188
2189 if (sc != NULL)
2190 return mach64_mmap(&sc->vd, NULL, off, prot);
2191
2192 return 0;
2193 }
2194
2195 #endif /* __sparc__ */
2196