Home | History | Annotate | Line # | Download | only in pci
machfb.c revision 1.14
      1 /*	$NetBSD: machfb.c,v 1.14 2003/06/29 22:30:25 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Bang Jun-Young
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * Some code is derived from ATI Rage Pro and Derivatives Programmer's Guide.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/callout.h>
     42 
     43 #ifdef __sparc__
     44 #include <machine/openfirm.h>
     45 #endif
     46 
     47 #include <dev/ic/videomode.h>
     48 
     49 #include <dev/pci/pcivar.h>
     50 #include <dev/pci/pcireg.h>
     51 #include <dev/pci/pcidevs.h>
     52 #include <dev/pci/pciio.h>
     53 #include <dev/pci/machfbreg.h>
     54 
     55 #include <dev/wscons/wsdisplayvar.h>
     56 #include <dev/wscons/wsconsio.h>
     57 #include <dev/wsfont/wsfont.h>
     58 #include <dev/rasops/rasops.h>
     59 
     60 #define MACH64_REG_SIZE		1024
     61 #define MACH64_REG_OFF		0x7ffc00
     62 
     63 #define	NBARS		3	/* number of Mach64 PCI BARs */
     64 
     65 struct vga_bar {
     66 	bus_addr_t vb_base;
     67 	bus_size_t vb_size;
     68 	pcireg_t vb_type;
     69 	int vb_flags;
     70 };
     71 
     72 struct mach64_softc {
     73 	struct device sc_dev;
     74 	pci_chipset_tag_t sc_pc;
     75 	pcitag_t sc_pcitag;
     76 
     77 	struct vga_bar sc_bars[NBARS];
     78 	struct vga_bar sc_rom;
     79 
     80 #define sc_aperbase 	sc_bars[0].vb_base
     81 #define sc_apersize	sc_bars[0].vb_size
     82 
     83 #define sc_iobase	sc_bars[1].vb_base
     84 #define sc_iosize	sc_bars[1].vb_size
     85 
     86 #define sc_regbase	sc_bars[2].vb_base
     87 #define sc_regsize	sc_bars[2].vb_size
     88 
     89 	bus_space_tag_t sc_regt;
     90 	bus_space_tag_t sc_memt;
     91 	bus_space_handle_t sc_regh;
     92 	bus_space_handle_t sc_memh;
     93 
     94 	size_t memsize;
     95 	int memtype;
     96 
     97 	int has_dsp;
     98 	int bits_per_pixel;
     99 	int max_x, max_y;
    100 	int virt_x, virt_y;
    101 	int color_depth;
    102 
    103 	int mem_freq;
    104 	int ramdac_freq;
    105 	int ref_freq;
    106 
    107 	int ref_div;
    108 	int log2_vclk_post_div;
    109 	int vclk_post_div;
    110 	int vclk_fb_div;
    111 	int mclk_post_div;
    112 	int mclk_fb_div;
    113 
    114 	struct mach64screen *wanted;
    115 	struct mach64screen *active;
    116 	void (*switchcb)(void *, int, int);
    117 	void *switchcbarg;
    118 	struct callout switch_callout;
    119 	LIST_HEAD(, mach64screen) screens;
    120 	const struct wsscreen_descr *currenttype;
    121 };
    122 
    123 struct mach64screen {
    124 	struct rasops_info ri;
    125 	LIST_ENTRY(mach64screen) next;
    126 	struct mach64_softc *sc;
    127 	const struct wsscreen_descr *type;
    128 	int active;
    129 	u_int16_t *mem;
    130 	int dispoffset;
    131 	int mindispoffset;
    132 	int maxdispoffset;
    133 
    134 	int cursoron;
    135 	int cursorcol;
    136 	int cursorrow;
    137 	u_int16_t cursortmp;
    138 };
    139 
    140 struct mach64_crtcregs {
    141 	u_int32_t h_total_disp;
    142 	u_int32_t h_sync_strt_wid;
    143 	u_int32_t v_total_disp;
    144 	u_int32_t v_sync_strt_wid;
    145 	u_int32_t gen_cntl;
    146 	u_int32_t clock_cntl;
    147 	u_int32_t color_depth;
    148 	u_int32_t dot_clock;
    149 };
    150 
    151 struct {
    152 	u_int16_t chip_id;
    153 	u_int32_t ramdac_freq;
    154 } mach64_info[] = {
    155 	{ PCI_PRODUCT_ATI_MACH64_CT, 135000 },
    156 	{ PCI_PRODUCT_ATI_RAGE_PRO_AGP, 230000 },
    157 	{ PCI_PRODUCT_ATI_RAGE_PRO_AGP1X, 230000 },
    158 	{ PCI_PRODUCT_ATI_RAGE_PRO_PCI_B, 230000 },
    159 	{ PCI_PRODUCT_ATI_RAGE_XL_AGP, 230000 },
    160 	{ PCI_PRODUCT_ATI_RAGE_PRO_PCI_P, 230000 },
    161 	{ PCI_PRODUCT_ATI_RAGE_PRO_PCI_L, 230000 },
    162 	{ PCI_PRODUCT_ATI_RAGE_XL_PCI, 230000 },
    163 	{ PCI_PRODUCT_ATI_RAGE_II, 135000 },
    164 	{ PCI_PRODUCT_ATI_RAGE_IIP, 200000 },
    165 	{ PCI_PRODUCT_ATI_RAGE_IIC_PCI, 230000 },
    166 	{ PCI_PRODUCT_ATI_RAGE_IIC_AGP_B, 230000 },
    167 	{ PCI_PRODUCT_ATI_RAGE_IIC_AGP_P, 230000 },
    168 	{ PCI_PRODUCT_ATI_RAGE_LT_PRO_AGP, 230000 },
    169 	{ PCI_PRODUCT_ATI_RAGE_MOB_M3_PCI, 230000 },
    170 	{ PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP, 230000 },
    171 	{ PCI_PRODUCT_ATI_RAGE_LT, 230000 },
    172 	{ PCI_PRODUCT_ATI_RAGE_LT_PRO_PCI, 230000 },
    173 	{ PCI_PRODUCT_ATI_RAGE_MOBILITY, 230000 },
    174 	{ PCI_PRODUCT_ATI_RAGE_LT_PRO, 230000 },
    175 	{ PCI_PRODUCT_ATI_MACH64_VT, 170000 },
    176 	{ PCI_PRODUCT_ATI_MACH64_VTB, 200000 },
    177 	{ PCI_PRODUCT_ATI_MACH64_VT4, 230000 }
    178 };
    179 
    180 static int mach64_chip_id, mach64_chip_rev;
    181 static struct videomode default_mode;
    182 static struct mach64screen mach64_console_screen;
    183 
    184 static char *mach64_memtype_names[] = {
    185 	"(N/A)", "DRAM", "EDO DRAM", "EDO DRAM", "SDRAM", "SGRAM", "WRAM",
    186 	"(unknown type)"
    187 };
    188 
    189 struct videomode mach64_modes[] = {
    190 	/* 640x400 @ 70 Hz, 31.5 kHz */
    191 	{ 25175, 640, 664, 760, 800, 400, 409, 411, 450, 0 },
    192 	/* 640x480 @ 72 Hz, 36.5 kHz */
    193 	{ 25175, 640, 664, 760, 800, 480, 491, 493, 525, 0 },
    194 	/* 800x600 @ 72 Hz, 48.0 kHz */
    195 	{ 50000, 800, 856, 976, 1040, 600, 637, 643, 666,
    196 	  VID_PHSYNC | VID_PVSYNC },
    197 	/* 1024x768 @ 70 Hz, 56.5 kHz */
    198 	{ 75000, 1024, 1048, 1184, 1328, 768, 771, 777, 806,
    199 	  VID_NHSYNC | VID_NVSYNC },
    200 	/* 1152x864 @ 70 Hz, 62.4 kHz */
    201 	{ 92000, 1152, 1208, 1368, 1474, 864, 865, 875, 895, 0 },
    202 	/* 1280x1024 @ 70 Hz, 74.59 kHz */
    203 	{ 126500, 1280, 1312, 1472, 1696, 1024, 1032, 1040, 1068,
    204 	  VID_NHSYNC | VID_NVSYNC }
    205 };
    206 
    207 /* FIXME values are wrong! */
    208 const u_char mach64_cmap[16 * 3] = {
    209 	0x00, 0x00, 0x00, /* black */
    210 	0x7f, 0x00, 0x00, /* red */
    211 	0x00, 0x7f, 0x00, /* green */
    212 	0x7f, 0x7f, 0x00, /* brown */
    213 	0x00, 0x00, 0x7f, /* blue */
    214 	0x7f, 0x00, 0x7f, /* magenta */
    215 	0x00, 0x7f, 0x7f, /* cyan */
    216 	0xff, 0xff, 0xff, /* white */
    217 
    218 	0x7f, 0x7f, 0x7f, /* black */
    219 	0xff, 0x00, 0x00, /* red */
    220 	0x00, 0xff, 0x00, /* green */
    221 	0xff, 0xff, 0x00, /* brown */
    222 	0x00, 0x00, 0xff, /* blue */
    223 	0xff, 0x00, 0xff, /* magenta */
    224 	0x00, 0xff, 0xff, /* cyan */
    225 	0xff, 0xff, 0xff, /* white */
    226 };
    227 
    228 int	mach64_match(struct device *, struct cfdata *, void *);
    229 void	mach64_attach(struct device *, struct device *, void *);
    230 
    231 CFATTACH_DECL(machfb, sizeof(struct mach64_softc), mach64_match, mach64_attach,
    232     NULL, NULL);
    233 
    234 void	mach64_init(struct mach64_softc *);
    235 int	mach64_get_memsize(struct mach64_softc *);
    236 int	mach64_get_max_ramdac(struct mach64_softc *);
    237 void	mach64_get_mode(struct mach64_softc *, struct videomode *);
    238 int	mach64_calc_crtcregs(struct mach64_softc *, struct mach64_crtcregs *,
    239 	    struct videomode *);
    240 void	mach64_set_crtcregs(struct mach64_softc *, struct mach64_crtcregs *);
    241 int	mach64_modeswitch(struct mach64_softc *, struct videomode *);
    242 void	mach64_set_dsp(struct mach64_softc *);
    243 void	mach64_set_pll(struct mach64_softc *, int);
    244 void	mach64_reset_engine(struct mach64_softc *);
    245 void	mach64_init_engine(struct mach64_softc *);
    246 void	mach64_adjust_frame(struct mach64_softc *, int, int);
    247 void	mach64_init_lut(struct mach64_softc *);
    248 void	mach64_switch_screen(struct mach64_softc *);
    249 void	mach64_init_screen(struct mach64_softc *, struct mach64screen *,
    250 	    const struct wsscreen_descr *, int, long *, int);
    251 void	mach64_restore_screen(struct mach64screen *,
    252 	    const struct wsscreen_descr *, u_int16_t *);
    253 int 	mach64_set_screentype(struct mach64_softc *,
    254 	    const struct wsscreen_descr *);
    255 int	mach64_is_console(struct pci_attach_args *);
    256 
    257 void	mach64_cursor(void *, int, int, int);
    258 int	mach64_mapchar(void *, int, u_int *);
    259 void	mach64_putchar(void *, int, int, u_int, long);
    260 void	mach64_copycols(void *, int, int, int, int);
    261 void	mach64_erasecols(void *, int, int, int, long);
    262 void	mach64_copyrows(void *, int, int, int);
    263 void	mach64_eraserows(void *, int, int, long);
    264 int	mach64_allocattr(void *, int, int, int, long *);
    265 
    266 #if 0
    267 const struct wsdisplay_emulops mach64_emulops = {
    268 	mach64_cursor,
    269 	mach64_mapchar,
    270 	mach64_putchar,
    271 	mach64_copycols,
    272 	mach64_erasecols,
    273 	mach64_copyrows,
    274 	mach64_eraserows,
    275 	mach64_allocattr,
    276 };
    277 #endif
    278 
    279 struct wsscreen_descr mach64_defaultscreen = {
    280 	"default",
    281 	0, 0,
    282 	&mach64_console_screen.ri.ri_ops,
    283 	8, 16,
    284 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    285 	&default_mode
    286 }, mach64_80x25_screen = {
    287 	"80x25", 80, 25,
    288 	&mach64_console_screen.ri.ri_ops,
    289 	8, 16,
    290 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    291 	&mach64_modes[0]
    292 }, mach64_80x30_screen = {
    293 	"80x30", 80, 30,
    294 	&mach64_console_screen.ri.ri_ops,
    295 	8, 16,
    296 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    297 	&mach64_modes[1]
    298 }, mach64_80x40_screen = {
    299 	"80x40", 80, 40,
    300 	&mach64_console_screen.ri.ri_ops,
    301 	8, 10,
    302 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    303 	&mach64_modes[0]
    304 }, mach64_80x50_screen = {
    305 	"80x50", 80, 50,
    306 	&mach64_console_screen.ri.ri_ops,
    307 	8, 8,
    308 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    309 	&mach64_modes[0]
    310 }, mach64_100x37_screen = {
    311 	"100x37", 100, 37,
    312 	&mach64_console_screen.ri.ri_ops,
    313 	8, 16,
    314 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    315 	&mach64_modes[2]
    316 }, mach64_128x48_screen = {
    317 	"128x48", 128, 48,
    318 	&mach64_console_screen.ri.ri_ops,
    319 	8, 16,
    320 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    321 	&mach64_modes[3]
    322 }, mach64_144x54_screen = {
    323 	"144x54", 144, 54,
    324 	&mach64_console_screen.ri.ri_ops,
    325 	8, 16,
    326 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    327 	&mach64_modes[4]
    328 }, mach64_160x64_screen = {
    329 	"160x54", 160, 64,
    330 	&mach64_console_screen.ri.ri_ops,
    331 	8, 16,
    332 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    333 	&mach64_modes[5]
    334 };
    335 
    336 const struct wsscreen_descr *_mach64_scrlist[] = {
    337 	&mach64_defaultscreen,
    338 	&mach64_80x25_screen,
    339 	&mach64_80x30_screen,
    340 	&mach64_80x40_screen,
    341 	&mach64_80x50_screen,
    342 	&mach64_100x37_screen,
    343 	&mach64_128x48_screen,
    344 	&mach64_144x54_screen,
    345 	&mach64_160x64_screen
    346 };
    347 
    348 struct wsscreen_list mach64_screenlist = {
    349 	sizeof(_mach64_scrlist) / sizeof(struct wsscreen_descr *),
    350 	_mach64_scrlist
    351 };
    352 
    353 int	mach64_ioctl(void *, u_long, caddr_t, int, struct proc *);
    354 paddr_t	mach64_mmap(void *, off_t, int);
    355 int	mach64_alloc_screen(void *, const struct wsscreen_descr *, void **,
    356 	    int *, int *, long *);
    357 void	mach64_free_screen(void *, void *);
    358 int	mach64_show_screen(void *, void *, int, void (*)(void *, int, int),
    359 	    void *);
    360 int	mach64_load_font(void *, void *, struct wsdisplay_font *);
    361 
    362 struct wsdisplay_accessops mach64_accessops = {
    363 	mach64_ioctl,
    364 	mach64_mmap,
    365 	mach64_alloc_screen,
    366 	mach64_free_screen,
    367 	mach64_show_screen,
    368 	NULL
    369 };
    370 
    371 /*
    372  * Inline functions for getting access to register aperture.
    373  */
    374 static inline u_int32_t regr(struct mach64_softc *, u_int32_t);
    375 static inline u_int8_t regrb(struct mach64_softc *, u_int32_t);
    376 static inline void regw(struct mach64_softc *, u_int32_t, u_int32_t);
    377 static inline void regwb(struct mach64_softc *, u_int32_t, u_int8_t);
    378 static inline void regwb_pll(struct mach64_softc *, u_int32_t, u_int8_t);
    379 
    380 static inline u_int32_t
    381 regr(struct mach64_softc *sc, u_int32_t index)
    382 {
    383 
    384 	return bus_space_read_4(sc->sc_regt, sc->sc_regh, index);
    385 }
    386 
    387 static inline u_int8_t
    388 regrb(struct mach64_softc *sc, u_int32_t index)
    389 {
    390 
    391 	return bus_space_read_1(sc->sc_regt, sc->sc_regh, index);
    392 }
    393 
    394 static inline void
    395 regw(struct mach64_softc *sc, u_int32_t index, u_int32_t data)
    396 {
    397 
    398 	bus_space_write_4(sc->sc_regt, sc->sc_regh, index, data);
    399 }
    400 
    401 static inline void
    402 regwb(struct mach64_softc *sc, u_int32_t index, u_int8_t data)
    403 {
    404 
    405 	bus_space_write_1(sc->sc_regt, sc->sc_regh, index, data);
    406 }
    407 
    408 static inline void
    409 regwb_pll(struct mach64_softc *sc, u_int32_t index, u_int8_t data)
    410 {
    411 
    412 	regwb(sc, CLOCK_CNTL + 1, (index << 2) | PLL_WR_EN);
    413 	regwb(sc, CLOCK_CNTL + 2, data);
    414 	regwb(sc, CLOCK_CNTL + 1, (index << 2) & ~PLL_WR_EN);
    415 }
    416 
    417 static inline void
    418 wait_for_fifo(struct mach64_softc *sc, u_int8_t v)
    419 {
    420 
    421 	while ((regr(sc, FIFO_STAT) & 0xffff) > (0x8000 >> v))
    422 		;
    423 }
    424 
    425 static inline void
    426 wait_for_idle(struct mach64_softc *sc)
    427 {
    428 
    429 	wait_for_fifo(sc, 16);
    430 	while ((regr(sc, GUI_STAT) & 1) != 0)
    431 		;
    432 }
    433 
    434 int
    435 mach64_match(struct device *parent, struct cfdata *match, void *aux)
    436 {
    437 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    438 	int i;
    439 
    440 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    441 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    442 		return 0;
    443 
    444 	for (i = 0; i < sizeof(mach64_info) / sizeof(mach64_info[0]); i++)
    445 		if (PCI_PRODUCT(pa->pa_id) == mach64_info[i].chip_id) {
    446 			mach64_chip_id = PCI_PRODUCT(pa->pa_id);
    447 			mach64_chip_rev = PCI_REVISION(pa->pa_class);
    448 			return 1;
    449 		}
    450 
    451 	return 0;
    452 }
    453 
    454 void
    455 mach64_attach(struct device *parent, struct device *self, void *aux)
    456 {
    457 	struct mach64_softc *sc = (void *)self;
    458 	struct pci_attach_args *pa = aux;
    459 	char devinfo[256];
    460 	int bar, reg, id;
    461 	struct wsemuldisplaydev_attach_args aa;
    462 	long defattr;
    463 	int setmode, console;
    464 
    465 	sc->sc_pc = pa->pa_pc;
    466 	sc->sc_pcitag = pa->pa_tag;
    467 
    468 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    469 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    470 
    471 	for (bar = 0; bar < NBARS; bar++) {
    472 		reg = PCI_MAPREG_START + (bar * 4);
    473 		sc->sc_bars[bar].vb_type = pci_mapreg_type(sc->sc_pc,
    474 		    sc->sc_pcitag, reg);
    475 		(void)pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, reg,
    476 		    sc->sc_bars[bar].vb_type, &sc->sc_bars[bar].vb_base,
    477 		    &sc->sc_bars[bar].vb_size, &sc->sc_bars[bar].vb_flags);
    478 	}
    479 	sc->sc_memt = pa->pa_memt;
    480 
    481 	mach64_init(sc);
    482 
    483 	printf("%s: %d MB aperture at 0x%08x, %d KB registers at 0x%08x\n",
    484 	    sc->sc_dev.dv_xname, (u_int)(sc->sc_apersize / (1024 * 1024)),
    485 	    (u_int)sc->sc_aperbase, (u_int)(sc->sc_regsize / 1024),
    486 	    (u_int)sc->sc_regbase);
    487 
    488 	if (mach64_chip_id == PCI_PRODUCT_ATI_MACH64_CT ||
    489 	    ((mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
    490 	      mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II) &&
    491 	      (mach64_chip_rev & 0x07) == 0))
    492 		sc->has_dsp = 0;
    493 	else
    494 		sc->has_dsp = 1;
    495 
    496 	sc->memsize = mach64_get_memsize(sc);
    497 	if (sc->memsize == 8192)
    498 		/* The last page is used as register aperture. */
    499 		sc->memsize -= 4;
    500 	sc->memtype = regr(sc, CONFIG_STAT0) & 0x07;
    501 
    502 	/* XXX is there any way to calculate reference frequency from
    503 	   known values? */
    504 	if (mach64_chip_id == PCI_PRODUCT_ATI_RAGE_XL_PCI)
    505 		sc->ref_freq = 29498;
    506 	else
    507 		sc->ref_freq = 14318;
    508 
    509 	regwb(sc, CLOCK_CNTL + 1, PLL_REF_DIV << 2);
    510 	sc->ref_div = regrb(sc, CLOCK_CNTL + 2);
    511 	regwb(sc, CLOCK_CNTL + 1, MCLK_FB_DIV << 2);
    512 	sc->mclk_fb_div = regrb(sc, CLOCK_CNTL + 2);
    513 	sc->mem_freq = (2 * sc->ref_freq * sc->mclk_fb_div) /
    514 	    (sc->ref_div * 2);
    515 	sc->mclk_post_div = (sc->mclk_fb_div * 2 * sc->ref_freq) /
    516 	    (sc->mem_freq * sc->ref_div);
    517 	sc->ramdac_freq = mach64_get_max_ramdac(sc);
    518 	printf("%s: %ld KB %s %d.%d MHz, maximum RAMDAC clock %d MHz\n",
    519 	    sc->sc_dev.dv_xname, (u_long)sc->memsize,
    520 	    mach64_memtype_names[sc->memtype],
    521 	    sc->mem_freq / 1000, sc->mem_freq % 1000,
    522 	    sc->ramdac_freq / 1000);
    523 
    524 	id = regr(sc, CONFIG_CHIP_ID) & 0xffff;
    525 	if (id != mach64_chip_id) {
    526 		printf("%s: chip ID mismatch, 0x%x != 0x%x\n",
    527 		    sc->sc_dev.dv_xname, id, mach64_chip_id);
    528 		return;
    529 	}
    530 
    531 	console = mach64_is_console(pa);
    532 
    533 #ifdef __sparc__
    534 	if (console) {
    535 		mach64_get_mode(sc, &default_mode);
    536 		setmode = 0;
    537 	} else {
    538 		memcpy(&default_mode, &mach64_modes[4], sizeof(struct videomode));
    539 		setmode = 1;
    540 	}
    541 #else
    542 	memcpy(&default_mode, &mach64_modes[0], sizeof(struct videomode));
    543 	setmode = 1;
    544 #endif
    545 
    546 	sc->bits_per_pixel = 8;
    547 	sc->virt_x = default_mode.hdisplay;
    548 	sc->virt_y = default_mode.vdisplay;
    549 	sc->max_x = sc->virt_x - 1;
    550 	sc->max_y = (sc->memsize * 1024) /
    551 	    (sc->virt_x * (sc->bits_per_pixel / 8)) - 1;
    552 
    553 	sc->color_depth = CRTC_PIX_WIDTH_8BPP;
    554 
    555 	mach64_init_engine(sc);
    556 #if 0
    557 	mach64_adjust_frame(0, 0);
    558 	if (sc->bits_per_pixel == 8)
    559 		mach64_init_lut(sc);
    560 #endif
    561 
    562 	printf("%s: initial resolution %dx%d at %d bpp\n", sc->sc_dev.dv_xname,
    563 	    default_mode.hdisplay, default_mode.vdisplay,
    564 	    sc->bits_per_pixel);
    565 
    566 	mach64_console_screen.ri.ri_hw = sc;
    567 	mach64_console_screen.ri.ri_depth = sc->bits_per_pixel;
    568 	mach64_console_screen.ri.ri_bits = (void*)(u_long)sc->sc_aperbase;
    569 	mach64_console_screen.ri.ri_width = default_mode.hdisplay;
    570 	mach64_console_screen.ri.ri_height = default_mode.vdisplay;
    571 	mach64_console_screen.ri.ri_stride = mach64_console_screen.ri.ri_width;
    572 	mach64_console_screen.ri.ri_flg = RI_CLEAR;
    573 
    574 	rasops_init(&mach64_console_screen.ri, mach64_console_screen.ri.ri_height / 16,
    575 	    mach64_console_screen.ri.ri_width / 8);
    576 
    577 	mach64_defaultscreen.nrows = mach64_console_screen.ri.ri_rows;
    578 	mach64_defaultscreen.ncols = mach64_console_screen.ri.ri_cols;
    579 
    580 	mach64_console_screen.ri.ri_ops.allocattr(&mach64_console_screen.ri, 0, 0, 0,
    581 	    &defattr);
    582 
    583 	/* Initialize fonts */
    584 	wsfont_init();
    585 
    586 	if (console) {
    587 		mach64_init_screen(sc, &mach64_console_screen,
    588 		    &mach64_defaultscreen, 1, &defattr, setmode);
    589 		wsdisplay_cnattach(&mach64_defaultscreen, &mach64_console_screen.ri,
    590 		    0, 0, defattr);
    591 	}
    592 
    593 	aa.console = console;
    594 	aa.scrdata = &mach64_screenlist;
    595 	aa.accessops = &mach64_accessops;
    596 	aa.accesscookie = sc;
    597 
    598 	config_found(self, &aa, wsemuldisplaydevprint);
    599 }
    600 
    601 void
    602 mach64_init_screen(struct mach64_softc *sc, struct mach64screen *scr,
    603     const struct wsscreen_descr *type, int existing, long *attrp, int setmode)
    604 {
    605 
    606 	scr->sc = sc;
    607 	scr->type = type;
    608 	scr->mindispoffset = 0;
    609 	scr->maxdispoffset = sc->memsize * 1024;
    610 	scr->dispoffset = 0;
    611 	scr->cursorcol = 0;
    612 	scr->cursorrow = 0;
    613 
    614 	scr->mem = (u_int16_t *)malloc(type->nrows * type->ncols * 2,
    615 	    M_DEVBUF, M_WAITOK);
    616 	if (existing) {
    617 		scr->active = 1;
    618 
    619 		if (setmode && mach64_set_screentype(sc, type)) {
    620 			panic("%s: failed to switch video mode",
    621 			    sc->sc_dev.dv_xname);
    622 		}
    623 	} else {
    624 		scr->active = 0;
    625 	}
    626 
    627 	LIST_INSERT_HEAD(&sc->screens, scr, next);
    628 }
    629 
    630 void
    631 mach64_init(struct mach64_softc *sc)
    632 {
    633 	u_int32_t *p32, saved_value;
    634 	u_int8_t *p;
    635 	int need_swap;
    636 
    637 	if (bus_space_map(sc->sc_memt, sc->sc_aperbase, sc->sc_apersize,
    638 		BUS_SPACE_MAP_LINEAR, &sc->sc_memh)) {
    639 		panic("%s: failed to map aperture", sc->sc_dev.dv_xname);
    640 	}
    641 	sc->sc_aperbase = (vaddr_t)bus_space_vaddr(sc->sc_memt, sc->sc_memh);
    642 
    643 	sc->sc_regt = sc->sc_memt;
    644 	bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
    645 	    sc->sc_regsize, &sc->sc_regh);
    646 	sc->sc_regbase = sc->sc_aperbase + 0x7ffc00;
    647 
    648 	/*
    649 	 * Test wether the aperture is byte swapped or not
    650 	 */
    651 	p32 = (u_int32_t*)(u_long)sc->sc_aperbase;
    652 	saved_value = *p32;
    653 	p = (u_int8_t*)(u_long)sc->sc_aperbase;
    654 	*p32 = 0x12345678;
    655 	if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
    656 		need_swap = 0;
    657 	else
    658 		need_swap = 1;
    659 	if (need_swap) {
    660 		sc->sc_aperbase += 0x800000;
    661 		sc->sc_apersize -= 0x800000;
    662 	}
    663 	*p32 = saved_value;
    664 
    665 	LIST_INIT(&sc->screens);
    666 	sc->active = NULL;
    667 	sc->currenttype = &mach64_defaultscreen;
    668 	callout_init(&sc->switch_callout);
    669 }
    670 
    671 int
    672 mach64_get_memsize(struct mach64_softc *sc)
    673 {
    674 	int tmp, memsize;
    675 	int mem_tab[] = {
    676 		512, 1024, 2048, 4096, 6144, 8192, 12288, 16384
    677 	};
    678 
    679 	tmp = regr(sc, MEM_CNTL);
    680 	if (sc->has_dsp) {
    681 		tmp &= 0x0000000f;
    682 		if (tmp < 8)
    683 			memsize = (tmp + 1) * 512;
    684 		else if (tmp < 12)
    685 			memsize = (tmp - 3) * 1024;
    686 		else
    687 			memsize = (tmp - 7) * 2048;
    688 	} else {
    689 		memsize = mem_tab[tmp & 0x07];
    690 	}
    691 
    692 	return memsize;
    693 }
    694 
    695 int
    696 mach64_get_max_ramdac(struct mach64_softc *sc)
    697 {
    698 	int i;
    699 
    700 	if ((mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
    701 	     mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II) &&
    702 	     (mach64_chip_rev & 0x07))
    703 		return 170000;
    704 
    705 	for (i = 0; i < sizeof(mach64_info) / sizeof(mach64_info[0]); i++)
    706 		if (mach64_chip_id == mach64_info[i].chip_id)
    707 			return mach64_info[i].ramdac_freq;
    708 
    709 	if (sc->bits_per_pixel == 8)
    710 		return 135000;
    711 	else
    712 		return 80000;
    713 }
    714 
    715 void
    716 mach64_get_mode(struct mach64_softc *sc, struct videomode *mode)
    717 {
    718 	struct mach64_crtcregs crtc;
    719 
    720 	crtc.h_total_disp = regr(sc, CRTC_H_TOTAL_DISP);
    721 	crtc.h_sync_strt_wid = regr(sc, CRTC_H_SYNC_STRT_WID);
    722 	crtc.v_total_disp = regr(sc, CRTC_V_TOTAL_DISP);
    723 	crtc.v_sync_strt_wid = regr(sc, CRTC_V_SYNC_STRT_WID);
    724 
    725 	mode->htotal = ((crtc.h_total_disp & 0xffff) + 1) << 3;
    726 	mode->hdisplay = ((crtc.h_total_disp >> 16) + 1) << 3;
    727 	mode->hsync_start = ((crtc.h_sync_strt_wid & 0xffff) + 1) << 3;
    728 	mode->hsync_end = ((crtc.h_sync_strt_wid >> 16) << 3) +
    729 	    mode->hsync_start;
    730 	mode->vtotal = (crtc.v_total_disp & 0xffff) + 1;
    731 	mode->vdisplay = (crtc.v_total_disp >> 16) + 1;
    732 	mode->vsync_start = (crtc.v_sync_strt_wid & 0xffff) + 1;
    733 	mode->vsync_end = (crtc.v_sync_strt_wid >> 16) + mode->vsync_start;
    734 
    735 #ifdef MACH64_DEBUG
    736 	printf("mach64_get_mode: %d %d %d %d %d %d %d %d\n",
    737 	    mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
    738 	    mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal);
    739 #endif
    740 }
    741 
    742 int
    743 mach64_calc_crtcregs(struct mach64_softc *sc, struct mach64_crtcregs *crtc,
    744     struct videomode *mode)
    745 {
    746 
    747 	if (mode->dot_clock > sc->ramdac_freq)
    748 		/* Clock too high. */
    749 		return 1;
    750 
    751 	crtc->h_total_disp = (((mode->hdisplay >> 3) - 1) << 16) |
    752 	    ((mode->htotal >> 3) - 1);
    753 	crtc->h_sync_strt_wid =
    754 	    (((mode->hsync_end - mode->hsync_start) >> 3) << 16) |
    755 	    ((mode->hsync_start >> 3) - 1);
    756 
    757 	crtc->v_total_disp = ((mode->vdisplay - 1) << 16) |
    758 	    (mode->vtotal - 1);
    759 	crtc->v_sync_strt_wid =
    760 	    ((mode->vsync_end - mode->vsync_start) << 16) |
    761 	    (mode->vsync_start - 1);
    762 
    763 	if (mode->flags & VID_NVSYNC)
    764 		crtc->v_sync_strt_wid |= CRTC_VSYNC_NEG;
    765 
    766 	switch (sc->bits_per_pixel) {
    767 	case 8:
    768 		crtc->color_depth = CRTC_PIX_WIDTH_8BPP;
    769 		break;
    770 	case 16:
    771 		crtc->color_depth = CRTC_PIX_WIDTH_16BPP;
    772 		break;
    773 	case 32:
    774 		crtc->color_depth = CRTC_PIX_WIDTH_32BPP;
    775 		break;
    776 	}
    777 
    778 	crtc->gen_cntl = 0;
    779 	if (mode->flags & VID_INTERLACE)
    780 		crtc->gen_cntl |= CRTC_INTERLACE_EN;
    781 	if (mode->flags & VID_CSYNC)
    782 		crtc->gen_cntl |= CRTC_CSYNC_EN;
    783 
    784 	crtc->dot_clock = mode->dot_clock;
    785 
    786 	return 0;
    787 }
    788 
    789 void
    790 mach64_set_crtcregs(struct mach64_softc *sc, struct mach64_crtcregs *crtc)
    791 {
    792 
    793 	mach64_set_pll(sc, crtc->dot_clock);
    794 
    795 	if (sc->has_dsp)
    796 		mach64_set_dsp(sc);
    797 
    798 	regw(sc, CRTC_H_TOTAL_DISP, crtc->h_total_disp);
    799 	regw(sc, CRTC_H_SYNC_STRT_WID, crtc->h_sync_strt_wid);
    800 	regw(sc, CRTC_V_TOTAL_DISP, crtc->v_total_disp);
    801 	regw(sc, CRTC_V_SYNC_STRT_WID, crtc->v_sync_strt_wid);
    802 
    803 	regw(sc, CRTC_VLINE_CRNT_VLINE, 0);
    804 
    805 	regw(sc, CRTC_OFF_PITCH, (sc->virt_x >> 3) << 22);
    806 
    807 	regw(sc, CRTC_GEN_CNTL, crtc->gen_cntl | crtc->color_depth |
    808 	    CRTC_EXT_DISP_EN | CRTC_EXT_EN);
    809 }
    810 
    811 int
    812 mach64_modeswitch(struct mach64_softc *sc, struct videomode *mode)
    813 {
    814 	struct mach64_crtcregs crtc;
    815 
    816 	if (mach64_calc_crtcregs(sc, &crtc, mode))
    817 		return 1;
    818 
    819 	mach64_set_crtcregs(sc, &crtc);
    820 	return 0;
    821 }
    822 
    823 void
    824 mach64_reset_engine(struct mach64_softc *sc)
    825 {
    826 
    827 	/* Reset engine.*/
    828 	regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) & ~GUI_ENGINE_ENABLE);
    829 
    830 	/* Enable engine. */
    831 	regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) | GUI_ENGINE_ENABLE);
    832 
    833 	/* Ensure engine is not locked up by clearing any FIFO or
    834 	   host errors. */
    835 	regw(sc, BUS_CNTL, regr(sc, BUS_CNTL) | BUS_HOST_ERR_ACK |
    836 	    BUS_FIFO_ERR_ACK);
    837 }
    838 
    839 void
    840 mach64_init_engine(struct mach64_softc *sc)
    841 {
    842 	u_int32_t pitch_value;
    843 
    844 	pitch_value = sc->virt_x;
    845 
    846 	if (sc->bits_per_pixel == 24)
    847 		pitch_value *= 3;
    848 
    849 	mach64_reset_engine(sc);
    850 
    851 	wait_for_fifo(sc, 14);
    852 
    853 	regw(sc, CONTEXT_MASK, 0xffffffff);
    854 
    855 	regw(sc, DST_OFF_PITCH, (pitch_value / 8) << 22);
    856 
    857 	regw(sc, DST_Y_X, 0);
    858 	regw(sc, DST_HEIGHT, 0);
    859 	regw(sc, DST_BRES_ERR, 0);
    860 	regw(sc, DST_BRES_INC, 0);
    861 	regw(sc, DST_BRES_DEC, 0);
    862 
    863 	regw(sc, DST_CNTL, DST_LAST_PEL | DST_X_LEFT_TO_RIGHT |
    864 	    DST_Y_TOP_TO_BOTTOM);
    865 
    866 	regw(sc, SRC_OFF_PITCH, (pitch_value / 8) << 22);
    867 
    868 	regw(sc, SRC_Y_X, 0);
    869 	regw(sc, SRC_HEIGHT1_WIDTH1, 1);
    870 	regw(sc, SRC_Y_X_START, 0);
    871 	regw(sc, SRC_HEIGHT2_WIDTH2, 1);
    872 
    873 	regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
    874 
    875 	wait_for_fifo(sc, 13);
    876 	regw(sc, HOST_CNTL, 0);
    877 
    878 	regw(sc, PAT_REG0, 0);
    879 	regw(sc, PAT_REG1, 0);
    880 	regw(sc, PAT_CNTL, 0);
    881 
    882 	regw(sc, SC_LEFT, 0);
    883 	regw(sc, SC_TOP, 0);
    884 	regw(sc, SC_BOTTOM, default_mode.vdisplay - 1);
    885 	regw(sc, SC_RIGHT, pitch_value - 1);
    886 
    887 	regw(sc, DP_BKGD_CLR, 0);
    888 	regw(sc, DP_FRGD_CLR, 0xffffffff);
    889 	regw(sc, DP_WRITE_MASK, 0xffffffff);
    890 	regw(sc, DP_MIX, (MIX_SRC << 16) | MIX_DST);
    891 
    892 	regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
    893 
    894 	wait_for_fifo(sc, 3);
    895 	regw(sc, CLR_CMP_CLR, 0);
    896 	regw(sc, CLR_CMP_MASK, 0xffffffff);
    897 	regw(sc, CLR_CMP_CNTL, 0);
    898 
    899 	wait_for_fifo(sc, 2);
    900 	switch (sc->bits_per_pixel) {
    901 	case 8:
    902 		regw(sc, DP_PIX_WIDTH, HOST_8BPP | SRC_8BPP | DST_8BPP);
    903 		regw(sc, DP_CHAIN_MASK, DP_CHAIN_8BPP);
    904 		regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) & ~DAC_8BIT_EN);
    905 		break;
    906 #if 0
    907 	case 32:
    908 		regw(sc, DP_PIX_WIDTH, HOST_32BPP | SRC_32BPP | DST_32BPP);
    909 		regw(sc, DP_CHAIN_MASK, DP_CHAIN_32BPP);
    910 		regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
    911 		break;
    912 #endif
    913 	}
    914 
    915 	wait_for_fifo(sc, 5);
    916 	regw(sc, CRTC_INT_CNTL, regr(sc, CRTC_INT_CNTL) & ~0x20);
    917 	regw(sc, GUI_TRAJ_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
    918 
    919 	wait_for_idle(sc);
    920 }
    921 
    922 void
    923 mach64_adjust_frame(struct mach64_softc *sc, int x, int y)
    924 {
    925 	int offset;
    926 
    927 	offset = ((x + y * sc->virt_x) * (sc->bits_per_pixel >> 3)) >> 3;
    928 
    929 	regw(sc, CRTC_OFF_PITCH, (regr(sc, CRTC_OFF_PITCH) & 0xfff00000) |
    930 	     offset);
    931 }
    932 
    933 void
    934 mach64_set_dsp(struct mach64_softc *sc)
    935 {
    936 	u_int32_t fifo_depth, page_size, dsp_precision, dsp_loop_latency;
    937 	u_int32_t dsp_off, dsp_on, dsp_xclks_per_qw;
    938 	u_int32_t xclks_per_qw, y;
    939 	u_int32_t fifo_off, fifo_on;
    940 
    941 	if (mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
    942 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II ||
    943 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIP ||
    944 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_PCI ||
    945 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_AGP_B ||
    946 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_AGP_P) {
    947 		dsp_loop_latency = 0;
    948 		fifo_depth = 24;
    949 	} else {
    950 		dsp_loop_latency = 2;
    951 		fifo_depth = 32;
    952 	}
    953 
    954 	dsp_precision = 0;
    955 	xclks_per_qw = (sc->mclk_fb_div * sc->vclk_post_div * 64 << 11) /
    956 	    (sc->vclk_fb_div * sc->mclk_post_div * sc->bits_per_pixel);
    957 	y = (xclks_per_qw * fifo_depth) >> 11;
    958 	while (y) {
    959 		y >>= 1;
    960 		dsp_precision++;
    961 	}
    962 	dsp_precision -= 5;
    963 	fifo_off = ((xclks_per_qw * (fifo_depth - 1)) >> 5) + (3 << 6);
    964 
    965 	switch (sc->memtype) {
    966 	case DRAM:
    967 	case EDO_DRAM:
    968 	case PSEUDO_EDO:
    969 		if (sc->memsize > 1024) {
    970 			page_size = 9;
    971 			dsp_loop_latency += 6;
    972 		} else {
    973 			page_size = 10;
    974 			if (sc->memtype == DRAM)
    975 				dsp_loop_latency += 8;
    976 			else
    977 				dsp_loop_latency += 7;
    978 		}
    979 		break;
    980 	case SDRAM:
    981 	case SGRAM:
    982 		if (sc->memsize > 1024) {
    983 			page_size = 8;
    984 			dsp_loop_latency += 8;
    985 		} else {
    986 			page_size = 10;
    987 			dsp_loop_latency += 9;
    988 		}
    989 		break;
    990 	default:
    991 		page_size = 10;
    992 		dsp_loop_latency += 9;
    993 		break;
    994 	}
    995 
    996 	if (xclks_per_qw >= (page_size << 11))
    997 		fifo_on = ((2 * page_size + 1) << 6) + (xclks_per_qw >> 5);
    998 	else
    999 		fifo_on = (3 * page_size + 2) << 6;
   1000 
   1001 	dsp_xclks_per_qw = xclks_per_qw >> dsp_precision;
   1002 	dsp_on = fifo_on >> dsp_precision;
   1003 	dsp_off = fifo_off >> dsp_precision;
   1004 
   1005 #ifdef MACH64_DEBUG
   1006 	printf("dsp_xclks_per_qw = %d, dsp_on = %d, dsp_off = %d,\n"
   1007 	    "dsp_precision = %d, dsp_loop_latency = %d,\n"
   1008 	    "mclk_fb_div = %d, vclk_fb_div = %d,\n"
   1009 	    "mclk_post_div = %d, vclk_post_div = %d\n",
   1010 	    dsp_xclks_per_qw, dsp_on, dsp_off, dsp_precision, dsp_loop_latency,
   1011 	    sc->mclk_fb_div, sc->vclk_fb_div,
   1012 	    sc->mclk_post_div, sc->vclk_post_div);
   1013 #endif
   1014 
   1015 	regw(sc, DSP_ON_OFF, ((dsp_on << 16) & DSP_ON) | (dsp_off & DSP_OFF));
   1016 	regw(sc, DSP_CONFIG, ((dsp_precision << 20) & DSP_PRECISION) |
   1017 	    ((dsp_loop_latency << 16) & DSP_LOOP_LATENCY) |
   1018 	    (dsp_xclks_per_qw & DSP_XCLKS_PER_QW));
   1019 }
   1020 
   1021 void
   1022 mach64_set_pll(struct mach64_softc *sc, int clock)
   1023 {
   1024 	int q;
   1025 
   1026 	q = (clock * sc->ref_div * 100) / (2 * sc->ref_freq);
   1027 #ifdef MACH64_DEBUG
   1028 	printf("q = %d\n", q);
   1029 #endif
   1030 	if (q > 25500) {
   1031 		printf("Warning: q > 25500\n");
   1032 		q = 25500;
   1033 		sc->vclk_post_div = 1;
   1034 		sc->log2_vclk_post_div = 0;
   1035 	} else if (q > 12750) {
   1036 		sc->vclk_post_div = 1;
   1037 		sc->log2_vclk_post_div = 0;
   1038 	} else if (q > 6350) {
   1039 		sc->vclk_post_div = 2;
   1040 		sc->log2_vclk_post_div = 1;
   1041 	} else if (q > 3150) {
   1042 		sc->vclk_post_div = 4;
   1043 		sc->log2_vclk_post_div = 2;
   1044 	} else if (q >= 1600) {
   1045 		sc->vclk_post_div = 8;
   1046 		sc->log2_vclk_post_div = 3;
   1047 	} else {
   1048 		printf("Warning: q < 1600\n");
   1049 		sc->vclk_post_div = 8;
   1050 		sc->log2_vclk_post_div = 3;
   1051 	}
   1052 	sc->vclk_fb_div = q * sc->vclk_post_div / 100;
   1053 
   1054 	regwb_pll(sc, MCLK_FB_DIV, sc->mclk_fb_div);
   1055 	regwb_pll(sc, VCLK_POST_DIV, sc->log2_vclk_post_div);
   1056 	regwb_pll(sc, VCLK0_FB_DIV, sc->vclk_fb_div);
   1057 }
   1058 
   1059 void
   1060 mach64_init_lut(struct mach64_softc *sc)
   1061 {
   1062 	int i;
   1063 
   1064 	regwb(sc, DAC_REGS, 0);
   1065 
   1066 	for (i = 0; i < 16; i++) {
   1067 		regwb(sc, DAC_REGS + 1, mach64_cmap[i * 3]);
   1068 		regwb(sc, DAC_REGS + 1, mach64_cmap[i * 3 + 1]);
   1069 		regwb(sc, DAC_REGS + 1, mach64_cmap[i + 3 + 2]);
   1070 	}
   1071 }
   1072 
   1073 void
   1074 mach64_switch_screen(struct mach64_softc *sc)
   1075 {
   1076 	struct mach64screen *scr, *oldscr;
   1077 	const struct wsscreen_descr *type;
   1078 
   1079 	scr = sc->wanted;
   1080 	if (!scr) {
   1081 		printf("mach64_switch_screen: disappeared\n");
   1082 		(*sc->switchcb)(sc->switchcbarg, EIO, 0);
   1083 		return;
   1084 	}
   1085 	type = scr->type;
   1086 	oldscr = sc->active; /* can be NULL! */
   1087 #ifdef DIAGNOSTIC
   1088 	if (oldscr) {
   1089 		if (!oldscr->active)
   1090 			panic("mach64_switch_screen: not active");
   1091 		if (oldscr->type != sc->currenttype)
   1092 			panic("mach64_switch_screen: bad type");
   1093 	}
   1094 #endif
   1095 	if (scr == oldscr)
   1096 		return;
   1097 
   1098 #ifdef DIAGNOSTIC
   1099 	if (scr->active)
   1100 		panic("mach64_switch_screen: active");
   1101 #endif
   1102 
   1103 	if (oldscr)
   1104 		oldscr->active = 0;
   1105 
   1106 	if (sc->currenttype != type) {
   1107 		mach64_set_screentype(sc, type);
   1108 		sc->currenttype = type;
   1109 	}
   1110 
   1111 	scr->dispoffset = scr->mindispoffset;
   1112 
   1113 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
   1114 
   1115 	}
   1116 
   1117 	/* Clear the entire screen. */
   1118 
   1119 	scr->active = 1;
   1120 	mach64_restore_screen(scr, type, scr->mem);
   1121 
   1122 	sc->active = scr;
   1123 
   1124 	mach64_cursor(scr, scr->cursoron, scr->cursorrow, scr->cursorcol);
   1125 
   1126 	sc->wanted = 0;
   1127 	if (sc->switchcb)
   1128 		(*sc->switchcb)(sc->switchcbarg, 0, 0);
   1129 }
   1130 
   1131 void
   1132 mach64_restore_screen(struct mach64screen *scr,
   1133     const struct wsscreen_descr *type, u_int16_t *mem)
   1134 {
   1135 
   1136 }
   1137 
   1138 int
   1139 mach64_set_screentype(struct mach64_softc *sc, const struct wsscreen_descr *des)
   1140 {
   1141 	struct mach64_crtcregs regs;
   1142 
   1143 	if (mach64_calc_crtcregs(sc, &regs,
   1144 	    (struct videomode *)des->modecookie))
   1145 		return 1;
   1146 
   1147 	mach64_set_crtcregs(sc, &regs);
   1148 	return 0;
   1149 }
   1150 
   1151 int
   1152 mach64_is_console(struct pci_attach_args *pa)
   1153 {
   1154 #ifdef __sparc__
   1155 	int node;
   1156 
   1157 	node = PCITAG_NODE(pa->pa_tag);
   1158 	if (node == -1)
   1159 		return 0;
   1160 
   1161 	return (node == OF_instance_to_package(OF_stdout()));
   1162 #else
   1163 	return 1;
   1164 #endif
   1165 }
   1166 
   1167 /*
   1168  * wsdisplay_emulops
   1169  */
   1170 
   1171 void
   1172 mach64_cursor(void *cookie, int on, int row, int col)
   1173 {
   1174 
   1175 }
   1176 
   1177 #if 0
   1178 int
   1179 mach64_mapchar(void *cookie, int uni, u_int *index)
   1180 {
   1181 
   1182 	return 0;
   1183 }
   1184 
   1185 void
   1186 mach64_putchar(void *cookie, int row, int col, u_int c, long attr)
   1187 {
   1188 
   1189 }
   1190 
   1191 void
   1192 mach64_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1193 {
   1194 
   1195 }
   1196 
   1197 void
   1198 mach64_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1199 {
   1200 
   1201 }
   1202 
   1203 void
   1204 mach64_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1205 {
   1206 
   1207 }
   1208 
   1209 int
   1210 mach64_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
   1211 {
   1212 
   1213 	return 0;
   1214 }
   1215 #endif
   1216 
   1217 void
   1218 mach64_eraserows(void *cookie, int row, int nrows, long fillattr)
   1219 {
   1220 
   1221 }
   1222 
   1223 /*
   1224  * wsdisplay_accessops
   1225  */
   1226 
   1227 int
   1228 mach64_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
   1229 {
   1230 
   1231 	return ENOTTY;
   1232 }
   1233 
   1234 paddr_t
   1235 mach64_mmap(void *v, off_t offset, int prot)
   1236 {
   1237 
   1238 	return -1;
   1239 }
   1240 
   1241 int
   1242 mach64_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
   1243     int *curxp, int *curyp, long *defattrp)
   1244 {
   1245 	struct mach64_softc *sc = v;
   1246 	struct mach64screen *scr;
   1247 
   1248 	scr = malloc(sizeof(struct mach64screen), M_DEVBUF, M_WAITOK|M_ZERO);
   1249 	mach64_init_screen(sc, scr, type, 0, defattrp, sc->active == NULL);
   1250 	rasops_init(&scr->ri, mach64_console_screen.ri.ri_height / 16,
   1251 	    mach64_console_screen.ri.ri_width / 8);
   1252 
   1253 	scr->mem = malloc(type->ncols * type->nrows * 2, M_DEVBUF,
   1254 	     M_WAITOK);
   1255 	mach64_eraserows(sc, 0, type->nrows, *defattrp);
   1256 	if (sc->active == NULL) {
   1257 		scr->active = 1;
   1258 		sc->active = scr;
   1259 		sc->currenttype = type;
   1260 	}
   1261 
   1262 	*cookiep = scr;
   1263 	*curxp = scr->cursorcol;
   1264 	*curyp = scr->cursorrow;
   1265 
   1266 	return 0;
   1267 }
   1268 
   1269 void
   1270 mach64_free_screen(void *v, void *cookie)
   1271 {
   1272 	struct mach64_softc *sc = v;
   1273 	struct mach64screen *scr = cookie;
   1274 
   1275 	LIST_REMOVE(scr, next);
   1276 	if (scr != &mach64_console_screen)
   1277 		free(scr, M_DEVBUF);
   1278 	else
   1279 		panic("mach64_free_screen: console");
   1280 
   1281 	if (sc->active == scr)
   1282 		sc->active = 0;
   1283 }
   1284 
   1285 int
   1286 mach64_show_screen(void *v, void *cookie, int waitok,
   1287     void (*cb)(void *, int, int), void *cbarg)
   1288 {
   1289 	struct mach64_softc *sc = v;
   1290 	struct mach64screen *scr, *oldscr;
   1291 
   1292 	scr = cookie;
   1293 	oldscr = sc->active;
   1294 	if (scr == oldscr)
   1295 		return 0;
   1296 
   1297 	sc->wanted = scr;
   1298 	sc->switchcb = cb;
   1299 	sc->switchcbarg = cbarg;
   1300 	if (cb) {
   1301 		callout_reset(&sc->switch_callout, 0,
   1302 		    (void(*)(void *))mach64_switch_screen, sc);
   1303 		return EAGAIN;
   1304 	}
   1305 
   1306 	mach64_switch_screen(sc);
   1307 
   1308 	return 0;
   1309 }
   1310 
   1311 #if 0
   1312 int
   1313 mach64_load_font(void *v, void *cookie, struct wsdisplay_font *data)
   1314 {
   1315 
   1316 	return 0;
   1317 }
   1318 #endif
   1319