Home | History | Annotate | Line # | Download | only in pci
machfb.c revision 1.23.2.5
      1 /*	$NetBSD: machfb.c,v 1.23.2.5 2005/06/08 11:48:50 tron 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 __KERNEL_RCSID(0,
     36 	"$NetBSD: machfb.c,v 1.23.2.5 2005/06/08 11:48:50 tron Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/malloc.h>
     43 #include <sys/callout.h>
     44 
     45 #ifdef __sparc__
     46 #include <machine/promlib.h>
     47 #endif
     48 
     49 #ifdef __powerpc__
     50 #include <dev/ofw/openfirm.h>
     51 #include <dev/ofw/ofw_pci.h>
     52 #endif
     53 
     54 #include <dev/ic/videomode.h>
     55 
     56 #include <dev/pci/pcivar.h>
     57 #include <dev/pci/pcireg.h>
     58 #include <dev/pci/pcidevs.h>
     59 #include <dev/pci/pciio.h>
     60 #include <dev/pci/machfbreg.h>
     61 
     62 #include <dev/wscons/wsdisplayvar.h>
     63 #include <dev/wscons/wsconsio.h>
     64 #include <dev/wsfont/wsfont.h>
     65 #include <dev/rasops/rasops.h>
     66 
     67 #define MACH64_REG_SIZE		1024
     68 #define MACH64_REG_OFF		0x7ffc00
     69 
     70 #define	NBARS		3	/* number of Mach64 PCI BARs */
     71 
     72 struct vga_bar {
     73 	bus_addr_t vb_base;
     74 	pcireg_t vb_busaddr;
     75 	bus_size_t vb_size;
     76 	pcireg_t vb_type;
     77 	int vb_flags;
     78 };
     79 
     80 struct mach64_softc {
     81 	struct device sc_dev;
     82 	pci_chipset_tag_t sc_pc;
     83 	pcitag_t sc_pcitag;
     84 
     85 	struct vga_bar sc_bars[NBARS];
     86 	struct vga_bar sc_rom;
     87 
     88 #define sc_aperbase 	sc_bars[0].vb_base
     89 #define sc_apersize	sc_bars[0].vb_size
     90 #define sc_aperphys 	sc_bars[0].vb_busaddr
     91 
     92 #define sc_iobase	sc_bars[1].vb_base
     93 #define sc_iosize	sc_bars[1].vb_size
     94 
     95 #define sc_regbase	sc_bars[2].vb_base
     96 #define sc_regsize	sc_bars[2].vb_size
     97 #define sc_regphys	sc_bars[2].vb_busaddr
     98 
     99 	bus_space_tag_t sc_regt;
    100 	bus_space_tag_t sc_memt;
    101 	bus_space_handle_t sc_regh;
    102 	bus_space_handle_t sc_memh;
    103 
    104 	size_t memsize;
    105 	int memtype;
    106 
    107 	int sc_mode;
    108 	int sc_bg;
    109 
    110 	int has_dsp;
    111 	int bits_per_pixel;
    112 	int max_x;
    113 	int max_y;
    114 	int virt_x;
    115 	int virt_y;
    116 	int color_depth;
    117 
    118 	int mem_freq;
    119 	int ramdac_freq;
    120 	int ref_freq;
    121 
    122 	int ref_div;
    123 	int log2_vclk_post_div;
    124 	int vclk_post_div;
    125 	int vclk_fb_div;
    126 	int mclk_post_div;
    127 	int mclk_fb_div;
    128 
    129 	struct mach64screen *wanted;
    130 	struct mach64screen *active;
    131 	void (*switchcb)(void *, int, int);
    132 	void *switchcbarg;
    133 	struct callout switch_callout;
    134 	LIST_HEAD(, mach64screen) screens;
    135 	const struct wsscreen_descr *currenttype;
    136 	u_char sc_cmap_red[256];
    137 	u_char sc_cmap_green[256];
    138 	u_char sc_cmap_blue[256];
    139 	int sc_dacw;
    140 };
    141 
    142 struct mach64screen {
    143 	struct rasops_info ri;
    144 	LIST_ENTRY(mach64screen) next;
    145 	struct mach64_softc *sc;
    146 	const struct wsscreen_descr *type;
    147 	int active;
    148 	u_int *chars;
    149 	long *attrs;
    150 	int dispoffset;
    151 	int mindispoffset;
    152 	int maxdispoffset;
    153 
    154 	int cursoron;
    155 	int cursorcol;
    156 	int cursorrow;
    157 	int cursordrawn;
    158 };
    159 
    160 struct mach64_crtcregs {
    161 	u_int32_t h_total_disp;
    162 	u_int32_t h_sync_strt_wid;
    163 	u_int32_t v_total_disp;
    164 	u_int32_t v_sync_strt_wid;
    165 	u_int32_t gen_cntl;
    166 	u_int32_t clock_cntl;
    167 	u_int32_t color_depth;
    168 	u_int32_t dot_clock;
    169 };
    170 
    171 struct {
    172 	u_int16_t chip_id;
    173 	u_int32_t ramdac_freq;
    174 } mach64_info[] = {
    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_II, 135000 },
    184 	{ PCI_PRODUCT_ATI_RAGE_IIP, 200000 },
    185 	{ PCI_PRODUCT_ATI_RAGE_IIC_PCI, 230000 },
    186 	{ PCI_PRODUCT_ATI_RAGE_IIC_AGP_B, 230000 },
    187 	{ PCI_PRODUCT_ATI_RAGE_IIC_AGP_P, 230000 },
    188 	{ PCI_PRODUCT_ATI_RAGE_LT_PRO_AGP, 230000 },
    189 	{ PCI_PRODUCT_ATI_RAGE_MOB_M3_PCI, 230000 },
    190 	{ PCI_PRODUCT_ATI_RAGE_MOB_M3_AGP, 230000 },
    191 	{ PCI_PRODUCT_ATI_RAGE_LT, 230000 },
    192 	{ PCI_PRODUCT_ATI_RAGE_LT_PRO_PCI, 230000 },
    193 	{ PCI_PRODUCT_ATI_RAGE_MOBILITY, 230000 },
    194 	{ PCI_PRODUCT_ATI_RAGE_LT_PRO, 230000 },
    195 	{ PCI_PRODUCT_ATI_MACH64_VT, 170000 },
    196 	{ PCI_PRODUCT_ATI_MACH64_VTB, 200000 },
    197 	{ PCI_PRODUCT_ATI_MACH64_VT4, 230000 }
    198 };
    199 
    200 static int mach64_chip_id, mach64_chip_rev;
    201 static struct videomode default_mode;
    202 static struct mach64screen mach64_console_screen;
    203 
    204 static char *mach64_memtype_names[] = {
    205 	"(N/A)", "DRAM", "EDO DRAM", "EDO DRAM", "SDRAM", "SGRAM", "WRAM",
    206 	"(unknown type)"
    207 };
    208 
    209 struct videomode mach64_modes[] = {
    210 	/* 640x400 @ 70 Hz, 31.5 kHz */
    211 	{ 25175, 640, 664, 760, 800, 400, 409, 411, 450, 0 },
    212 	/* 640x480 @ 72 Hz, 36.5 kHz */
    213 	{ 25175, 640, 664, 760, 800, 480, 491, 493, 525, 0 },
    214 	/* 800x600 @ 72 Hz, 48.0 kHz */
    215 	{ 50000, 800, 856, 976, 1040, 600, 637, 643, 666,
    216 	  VID_PHSYNC | VID_PVSYNC },
    217 	/* 1024x768 @ 70 Hz, 56.5 kHz */
    218 	{ 75000, 1024, 1048, 1184, 1328, 768, 771, 777, 806,
    219 	  VID_NHSYNC | VID_NVSYNC },
    220 	/* 1152x864 @ 70 Hz, 62.4 kHz */
    221 	{ 92000, 1152, 1208, 1368, 1474, 864, 865, 875, 895, 0 },
    222 	/* 1280x1024 @ 70 Hz, 74.59 kHz */
    223 	{ 126500, 1280, 1312, 1472, 1696, 1024, 1032, 1040, 1068,
    224 	  VID_NHSYNC | VID_NVSYNC }
    225 };
    226 
    227 extern const u_char rasops_cmap[768];
    228 
    229 int	mach64_match(struct device *, struct cfdata *, void *);
    230 void	mach64_attach(struct device *, struct device *, void *);
    231 
    232 CFATTACH_DECL(machfb, sizeof(struct mach64_softc), mach64_match, mach64_attach,
    233     NULL, NULL);
    234 
    235 void	mach64_init(struct mach64_softc *);
    236 int	mach64_get_memsize(struct mach64_softc *);
    237 int	mach64_get_max_ramdac(struct mach64_softc *);
    238 void	mach64_get_mode(struct mach64_softc *, struct videomode *);
    239 int	mach64_calc_crtcregs(struct mach64_softc *, struct mach64_crtcregs *,
    240     struct videomode *);
    241 void	mach64_set_crtcregs(struct mach64_softc *, struct mach64_crtcregs *);
    242 int	mach64_modeswitch(struct mach64_softc *, struct videomode *);
    243 void	mach64_set_dsp(struct mach64_softc *);
    244 void	mach64_set_pll(struct mach64_softc *, int);
    245 void	mach64_reset_engine(struct mach64_softc *);
    246 void	mach64_init_engine(struct mach64_softc *);
    247 void	mach64_adjust_frame(struct mach64_softc *, int, int);
    248 void	mach64_init_lut(struct mach64_softc *);
    249 void	mach64_switch_screen(struct mach64_softc *);
    250 void	mach64_init_screen(struct mach64_softc *, struct mach64screen *,
    251     const struct wsscreen_descr *, int, long *, int);
    252 void	mach64_restore_screen(struct mach64screen *,
    253     const struct wsscreen_descr *, u_int *);
    254 int 	mach64_set_screentype(struct mach64_softc *,
    255     const struct wsscreen_descr *);
    256 int	mach64_is_console(struct pci_attach_args *);
    257 
    258 void	mach64_cursor(void *, int, int, int);
    259 int	mach64_mapchar(void *, int, u_int *);
    260 void	mach64_putchar(void *, int, int, u_int, long);
    261 void	mach64_copycols(void *, int, int, int, int);
    262 void	mach64_erasecols(void *, int, int, int, long);
    263 void	mach64_copyrows(void *, int, int, int);
    264 void	mach64_eraserows(void *, int, int, long);
    265 int	mach64_allocattr(void *, int, int, int, long *);
    266 void 	mach64_clearscreen(struct mach64_softc *);
    267 
    268 void	mach64_scroll(void *, void *, int);
    269 
    270 int	mach64_putcmap(struct mach64_softc *, struct wsdisplay_cmap *);
    271 int	mach64_getcmap(struct mach64_softc *, struct wsdisplay_cmap *);
    272 int	mach64_putpalreg(struct mach64_softc *, uint8_t, uint8_t, uint8_t,
    273 			 uint8_t);
    274 void	mach64_bitblt(struct mach64_softc *, int, int, int, int, int, int, int,
    275 		      int) ;
    276 void	mach64_rectfill(struct mach64_softc *, int, int, int, int, int);
    277 void	mach64_setup_mono(struct mach64_softc *, int, int, int, int, uint32_t,
    278 			  uint32_t);
    279 void	mach64_feed_bytes(struct mach64_softc *, int, uint8_t *);
    280 void	mach64_showpal(struct mach64_softc *);
    281 int	mach64_getwschar(void *, struct wsdisplay_char *);
    282 int	mach64_putwschar(void *, struct wsdisplay_char *);
    283 
    284 void	set_address(struct rasops_info *, bus_addr_t);
    285 
    286 #if 0
    287 const struct wsdisplay_emulops mach64_emulops = {
    288 	mach64_cursor,
    289 	mach64_mapchar,
    290 	mach64_putchar,
    291 	mach64_copycols,
    292 	mach64_erasecols,
    293 	mach64_copyrows,
    294 	mach64_eraserows,
    295 	mach64_allocattr,
    296 };
    297 #endif
    298 
    299 struct wsscreen_descr mach64_defaultscreen = {
    300 	"default",
    301 	0, 0,
    302 	&mach64_console_screen.ri.ri_ops,
    303 	8, 16,
    304 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    305 	&default_mode
    306 }, mach64_80x25_screen = {
    307 	"80x25", 80, 25,
    308 	&mach64_console_screen.ri.ri_ops,
    309 	8, 16,
    310 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    311 	&mach64_modes[0]
    312 }, mach64_80x30_screen = {
    313 	"80x30", 80, 30,
    314 	&mach64_console_screen.ri.ri_ops,
    315 	8, 16,
    316 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    317 	&mach64_modes[1]
    318 }, mach64_80x40_screen = {
    319 	"80x40", 80, 40,
    320 	&mach64_console_screen.ri.ri_ops,
    321 	8, 10,
    322 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    323 	&mach64_modes[0]
    324 }, mach64_80x50_screen = {
    325 	"80x50", 80, 50,
    326 	&mach64_console_screen.ri.ri_ops,
    327 	8, 8,
    328 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    329 	&mach64_modes[0]
    330 }, mach64_100x37_screen = {
    331 	"100x37", 100, 37,
    332 	&mach64_console_screen.ri.ri_ops,
    333 	8, 16,
    334 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    335 	&mach64_modes[2]
    336 }, mach64_128x48_screen = {
    337 	"128x48", 128, 48,
    338 	&mach64_console_screen.ri.ri_ops,
    339 	8, 16,
    340 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    341 	&mach64_modes[3]
    342 }, mach64_144x54_screen = {
    343 	"144x54", 144, 54,
    344 	&mach64_console_screen.ri.ri_ops,
    345 	8, 16,
    346 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    347 	&mach64_modes[4]
    348 }, mach64_160x64_screen = {
    349 	"160x54", 160, 64,
    350 	&mach64_console_screen.ri.ri_ops,
    351 	8, 16,
    352 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    353 	&mach64_modes[5]
    354 };
    355 
    356 const struct wsscreen_descr *_mach64_scrlist[] = {
    357 	&mach64_defaultscreen,
    358 	&mach64_80x25_screen,
    359 	&mach64_80x30_screen,
    360 	&mach64_80x40_screen,
    361 	&mach64_80x50_screen,
    362 	&mach64_100x37_screen,
    363 	&mach64_128x48_screen,
    364 	&mach64_144x54_screen,
    365 	&mach64_160x64_screen
    366 };
    367 
    368 struct wsscreen_list mach64_screenlist = {
    369 	sizeof(_mach64_scrlist) / sizeof(struct wsscreen_descr *),
    370 	_mach64_scrlist
    371 };
    372 
    373 int	mach64_ioctl(void *, u_long, caddr_t, int, struct proc *);
    374 paddr_t	mach64_mmap(void *, off_t, int);
    375 int	mach64_alloc_screen(void *, const struct wsscreen_descr *, void **,
    376     int *, int *, long *);
    377 void	mach64_free_screen(void *, void *);
    378 int	mach64_show_screen(void *, void *, int, void (*)(void *, int, int),
    379     void *);
    380 int	mach64_load_font(void *, void *, struct wsdisplay_font *);
    381 
    382 struct wsdisplay_accessops mach64_accessops = {
    383 	mach64_ioctl,
    384 	mach64_mmap,
    385 	mach64_alloc_screen,
    386 	mach64_free_screen,
    387 	mach64_show_screen,
    388 	NULL,			/* load_font */
    389 	NULL,			/* polls */
    390 	mach64_getwschar,	/* getwschar */
    391 	mach64_putwschar,	/* putwschar */
    392 	NULL,			/* scroll */
    393 	NULL,			/* getborder */
    394 	NULL			/* setborder */
    395 };
    396 
    397 /*
    398  * Inline functions for getting access to register aperture.
    399  */
    400 static inline u_int32_t regr(struct mach64_softc *, u_int32_t);
    401 static inline u_int8_t regrb(struct mach64_softc *, u_int32_t);
    402 static inline void regw(struct mach64_softc *, u_int32_t, u_int32_t);
    403 static inline void regwb(struct mach64_softc *, u_int32_t, u_int8_t);
    404 static inline void regwb_pll(struct mach64_softc *, u_int32_t, u_int8_t);
    405 
    406 static inline u_int32_t
    407 regr(struct mach64_softc *sc, u_int32_t index)
    408 {
    409 	return bus_space_read_4(sc->sc_regt, sc->sc_regh, index);
    410 }
    411 
    412 static inline u_int8_t
    413 regrb(struct mach64_softc *sc, u_int32_t index)
    414 {
    415 	return bus_space_read_1(sc->sc_regt, sc->sc_regh, index);
    416 }
    417 
    418 static inline void
    419 regw(struct mach64_softc *sc, u_int32_t index, u_int32_t data)
    420 {
    421 	bus_space_write_4(sc->sc_regt, sc->sc_regh, index, data);
    422 	bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 4,
    423 	    BUS_SPACE_BARRIER_WRITE);
    424 }
    425 
    426 static inline void
    427 regwb(struct mach64_softc *sc, u_int32_t index, u_int8_t data)
    428 {
    429 	bus_space_write_1(sc->sc_regt, sc->sc_regh, index, data);
    430 	bus_space_barrier(sc->sc_regt, sc->sc_regh, index, 1,
    431 	    BUS_SPACE_BARRIER_WRITE);
    432 }
    433 
    434 static inline void
    435 regwb_pll(struct mach64_softc *sc, u_int32_t index, u_int8_t data)
    436 {
    437 	regwb(sc, CLOCK_CNTL + 1, (index << 2) | PLL_WR_EN);
    438 	regwb(sc, CLOCK_CNTL + 2, data);
    439 	regwb(sc, CLOCK_CNTL + 1, (index << 2) & ~PLL_WR_EN);
    440 }
    441 
    442 static inline void
    443 wait_for_fifo(struct mach64_softc *sc, u_int8_t v)
    444 {
    445 	while ((regr(sc, FIFO_STAT) & 0xffff) > (0x8000 >> v))
    446 		continue;
    447 }
    448 
    449 static inline void
    450 wait_for_idle(struct mach64_softc *sc)
    451 {
    452 	wait_for_fifo(sc, 16);
    453 	while ((regr(sc, GUI_STAT) & 1) != 0)
    454 		continue;
    455 }
    456 
    457 int
    458 mach64_match(struct device *parent, struct cfdata *match, void *aux)
    459 {
    460 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    461 	int i;
    462 
    463 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    464 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    465 		return 0;
    466 
    467 	for (i = 0; i < sizeof(mach64_info) / sizeof(mach64_info[0]); i++)
    468 		if (PCI_PRODUCT(pa->pa_id) == mach64_info[i].chip_id) {
    469 			mach64_chip_id = PCI_PRODUCT(pa->pa_id);
    470 			mach64_chip_rev = PCI_REVISION(pa->pa_class);
    471 			return 100;
    472 		}
    473 
    474 	return 0;
    475 }
    476 
    477 void
    478 mach64_attach(struct device *parent, struct device *self, void *aux)
    479 {
    480 	struct mach64_softc *sc = (void *)self;
    481 	struct pci_attach_args *pa = aux;
    482 	char devinfo[256];
    483 	int bar, reg, id;
    484 	struct wsemuldisplaydev_attach_args aa;
    485 	long defattr;
    486 	int setmode, console;
    487 	pcireg_t screg;
    488 
    489 	sc->sc_pc = pa->pa_pc;
    490 	sc->sc_pcitag = pa->pa_tag;
    491 	sc->sc_dacw = -1;
    492 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    493 
    494 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    495 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    496 
    497 	/* enable memory and IO access */
    498 	screg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    499 	screg |= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    500 	pci_conf_write(sc->sc_pc, sc->sc_pcitag,PCI_COMMAND_STATUS_REG,screg);
    501 
    502 	for (bar = 0; bar < NBARS; bar++) {
    503 		reg = PCI_MAPREG_START + (bar * 4);
    504 		sc->sc_bars[bar].vb_type = pci_mapreg_type(sc->sc_pc,
    505 		    sc->sc_pcitag, reg);
    506 		(void)pci_mapreg_info(sc->sc_pc, sc->sc_pcitag, reg,
    507 		    sc->sc_bars[bar].vb_type, &sc->sc_bars[bar].vb_base,
    508 		    &sc->sc_bars[bar].vb_size, &sc->sc_bars[bar].vb_flags);
    509 		sc->sc_bars[bar].vb_busaddr = pci_conf_read(sc->sc_pc,
    510 		    sc->sc_pcitag, reg)&0xfffffff0;
    511 	}
    512 	sc->sc_memt = pa->pa_memt;
    513 
    514 	mach64_init(sc);
    515 
    516 	printf("%s: %d MB aperture at 0x%08x, %d KB registers at 0x%08x\n",
    517 	    sc->sc_dev.dv_xname, (u_int)(sc->sc_apersize / (1024 * 1024)),
    518 	    (u_int)sc->sc_aperphys, (u_int)(sc->sc_regsize / 1024),
    519 	    (u_int)sc->sc_regphys);
    520 
    521 	if (mach64_chip_id == PCI_PRODUCT_ATI_MACH64_CT ||
    522 	    ((mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
    523 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II) &&
    524 	    (mach64_chip_rev & 0x07) == 0))
    525 		sc->has_dsp = 0;
    526 	else
    527 		sc->has_dsp = 1;
    528 
    529 	sc->memsize = mach64_get_memsize(sc);
    530 	if (sc->memsize == 8192)
    531 		/* The last page is used as register aperture. */
    532 		sc->memsize -= 4;
    533 	sc->memtype = regr(sc, CONFIG_STAT0) & 0x07;
    534 
    535 	/* XXX is there any way to calculate reference frequency from
    536 	   known values? */
    537 	if ((mach64_chip_id == PCI_PRODUCT_ATI_RAGE_XL_PCI) ||
    538 	    ((mach64_chip_id >= PCI_PRODUCT_ATI_RAGE_LT_PRO_PCI) &&
    539 	    (mach64_chip_id <= PCI_PRODUCT_ATI_RAGE_LT_PRO))) {
    540 		printf("ref_freq=29.498MHz\n");
    541 		sc->ref_freq = 29498;
    542 	} else
    543 		sc->ref_freq = 14318;
    544 
    545 	regwb(sc, CLOCK_CNTL + 1, PLL_REF_DIV << 2);
    546 	sc->ref_div = regrb(sc, CLOCK_CNTL + 2);
    547 	regwb(sc, CLOCK_CNTL + 1, MCLK_FB_DIV << 2);
    548 	sc->mclk_fb_div = regrb(sc, CLOCK_CNTL + 2);
    549 	sc->mem_freq = (2 * sc->ref_freq * sc->mclk_fb_div) /
    550 	    (sc->ref_div * 2);
    551 	sc->mclk_post_div = (sc->mclk_fb_div * 2 * sc->ref_freq) /
    552 	    (sc->mem_freq * sc->ref_div);
    553 	sc->ramdac_freq = mach64_get_max_ramdac(sc);
    554 	printf("%s: %ld KB %s %d.%d MHz, maximum RAMDAC clock %d MHz\n",
    555 	    sc->sc_dev.dv_xname, (u_long)sc->memsize,
    556 	    mach64_memtype_names[sc->memtype],
    557 	    sc->mem_freq / 1000, sc->mem_freq % 1000,
    558 	    sc->ramdac_freq / 1000);
    559 
    560 	id = regr(sc, CONFIG_CHIP_ID) & 0xffff;
    561 	if (id != mach64_chip_id) {
    562 		printf("%s: chip ID mismatch, 0x%x != 0x%x\n",
    563 		    sc->sc_dev.dv_xname, id, mach64_chip_id);
    564 		return;
    565 	}
    566 
    567 	console = mach64_is_console(pa);
    568 
    569 #if defined(__sparc__) || defined(__powerpc__)
    570 	if (console) {
    571 		mach64_get_mode(sc, &default_mode);
    572 		setmode = 0;
    573 	} else {
    574 		memcpy(&default_mode, &mach64_modes[4], sizeof(default_mode));
    575 		setmode = 1;
    576 	}
    577 #else
    578 	memcpy(&default_mode, &mach64_modes[0], sizeof(default_mode));
    579 	setmode = 1;
    580 #endif
    581 
    582 	sc->bits_per_pixel = 8;
    583 	sc->virt_x = default_mode.hdisplay;
    584 	sc->virt_y = default_mode.vdisplay;
    585 	sc->max_x = sc->virt_x - 1;
    586 	sc->max_y = (sc->memsize * 1024) /
    587 	    (sc->virt_x * (sc->bits_per_pixel / 8)) - 1;
    588 
    589 	sc->color_depth = CRTC_PIX_WIDTH_8BPP;
    590 
    591 	mach64_init_engine(sc);
    592 #if 0
    593 	mach64_adjust_frame(0, 0);
    594 	if (sc->bits_per_pixel == 8)
    595 		mach64_init_lut(sc);
    596 #endif
    597 
    598 	printf("%s: initial resolution %dx%d at %d bpp\n", sc->sc_dev.dv_xname,
    599 	    default_mode.hdisplay, default_mode.vdisplay,
    600 	    sc->bits_per_pixel);
    601 
    602 	mach64_console_screen.ri.ri_hw = &mach64_console_screen;
    603 	mach64_console_screen.ri.ri_depth = sc->bits_per_pixel;
    604 	mach64_console_screen.ri.ri_width = default_mode.hdisplay;
    605 	mach64_console_screen.ri.ri_height = default_mode.vdisplay;
    606 	mach64_console_screen.ri.ri_stride = mach64_console_screen.ri.ri_width;
    607 
    608 	mach64_console_screen.ri.ri_bits=(void *)(uintptr_t)sc->sc_aperbase;
    609 
    610 	mach64_console_screen.ri.ri_flg = RI_CENTER;
    611 	mach64_console_screen.active = 1;
    612 	sc->active = &mach64_console_screen;
    613 
    614 	/* XXX width/height are nonsense, we only want to pick a font */
    615 	rasops_init(&mach64_console_screen.ri,
    616 	    mach64_console_screen.ri.ri_height / 16,
    617 	    mach64_console_screen.ri.ri_width / 8);
    618 
    619 	/* now get the real values */
    620 	rasops_reconfig(&mach64_console_screen.ri,
    621 	    mach64_console_screen.ri.ri_height /
    622 	    mach64_console_screen.ri.ri_font->fontheight,
    623 	    mach64_console_screen.ri.ri_width /
    624 	    mach64_console_screen.ri.ri_font->fontwidth);
    625 
    626 	set_address(&mach64_console_screen.ri, sc->sc_aperbase);
    627 
    628 	/* enable acceleration */
    629 	mach64_console_screen.ri.ri_ops.copyrows = mach64_copyrows;
    630 	mach64_console_screen.ri.ri_ops.eraserows = mach64_eraserows;
    631 	mach64_console_screen.ri.ri_ops.copycols = mach64_copycols;
    632 	mach64_console_screen.ri.ri_ops.erasecols = mach64_erasecols;
    633 	mach64_console_screen.ri.ri_ops.putchar = mach64_putchar;
    634 	mach64_console_screen.ri.ri_ops.cursor = mach64_cursor;
    635 
    636 	mach64_defaultscreen.nrows = mach64_console_screen.ri.ri_rows;
    637 	mach64_defaultscreen.ncols = mach64_console_screen.ri.ri_cols;
    638 
    639 	mach64_allocattr(&mach64_console_screen.ri, WS_DEFAULT_FG,
    640 	    WS_DEFAULT_BG, 0, &defattr);
    641 
    642 	sc->sc_bg = WS_DEFAULT_BG;
    643 
    644 	/* really necessary? */
    645 	mach64_defaultscreen.capabilities = mach64_console_screen.ri.ri_caps;
    646 	mach64_defaultscreen.textops = &mach64_console_screen.ri.ri_ops;
    647 
    648 	/* Initialize fonts */
    649 	/* XXX shouldn't that happen /before/ we call rasops_init()? */
    650 	wsfont_init();
    651 
    652 	mach64_init_screen(sc, &mach64_console_screen,
    653 	    &mach64_defaultscreen, 1, &defattr, setmode);
    654 	if (console) {
    655 		wsdisplay_cnattach(&mach64_defaultscreen,
    656 		    &mach64_console_screen.ri, 0, 0, defattr);
    657 	}
    658 
    659 	mach64_init_lut(sc);
    660 	mach64_clearscreen(sc);
    661 
    662 	aa.console = console;
    663 	aa.scrdata = &mach64_screenlist;
    664 	aa.accessops = &mach64_accessops;
    665 	aa.accesscookie = sc;
    666 
    667 	config_found(self, &aa, wsemuldisplaydevprint);
    668 }
    669 
    670 void
    671 mach64_init_screen(struct mach64_softc *sc, struct mach64screen *scr,
    672     const struct wsscreen_descr *type, int existing, long *attrp, int setmode)
    673 {
    674 	struct rasops_info *ri=&scr->ri;
    675 	int cnt;
    676 
    677 	scr->sc = sc;
    678 	scr->type = type;
    679 	scr->mindispoffset = 0;
    680 	scr->maxdispoffset = sc->memsize * 1024;
    681 	scr->dispoffset = 0;
    682 	scr->cursorcol = 0;
    683 	scr->cursorrow = 0;
    684 
    685 	cnt = type->nrows * type->ncols;
    686 	scr->attrs = malloc(cnt * (sizeof(long) + sizeof(u_int)),
    687 	    M_DEVBUF, M_WAITOK);
    688 	scr->chars = (u_int *)&scr->attrs[cnt];
    689 
    690 	/*
    691 	 * we allocate both chars and attributes in one chunk, attributes first
    692 	 * because they have the (potentially) bigger alignment
    693 	 */
    694 	ri->ri_depth = sc->bits_per_pixel;
    695 	ri->ri_width = default_mode.hdisplay;
    696 	ri->ri_height = default_mode.vdisplay;
    697 	ri->ri_stride = ri->ri_width;
    698 	ri->ri_flg = RI_CENTER;
    699 
    700 	if (existing) {
    701 		scr->active = 1;
    702 		ri->ri_flg |= RI_CLEAR;
    703 		if (setmode && mach64_set_screentype(sc, type)) {
    704 			panic("%s: failed to switch video mode",
    705 			    sc->sc_dev.dv_xname);
    706 		}
    707 	} else {
    708 		scr->active = 0;
    709 	}
    710 
    711 	LIST_INSERT_HEAD(&sc->screens, scr, next);
    712 }
    713 
    714 void
    715 mach64_init(struct mach64_softc *sc)
    716 {
    717 	u_int32_t *p32, saved_value;
    718 	u_int8_t *p;
    719 	int need_swap;
    720 
    721 	if (bus_space_map(sc->sc_memt, sc->sc_aperbase, sc->sc_apersize,
    722 		BUS_SPACE_MAP_LINEAR, &sc->sc_memh)) {
    723 		panic("%s: failed to map aperture", sc->sc_dev.dv_xname);
    724 	}
    725 	sc->sc_aperbase = (vaddr_t)bus_space_vaddr(sc->sc_memt, sc->sc_memh);
    726 
    727 	sc->sc_regt = sc->sc_memt;
    728 	bus_space_subregion(sc->sc_regt, sc->sc_memh, MACH64_REG_OFF,
    729 	    sc->sc_regsize, &sc->sc_regh);
    730 	sc->sc_regbase = sc->sc_aperbase + 0x7ffc00;
    731 
    732 	/*
    733 	 * Test wether the aperture is byte swapped or not
    734 	 */
    735 	p32 = (u_int32_t*)(u_long)sc->sc_aperbase;
    736 	saved_value = *p32;
    737 	p = (u_int8_t*)(u_long)sc->sc_aperbase;
    738 	*p32 = 0x12345678;
    739 	if (p[0] == 0x12 && p[1] == 0x34 && p[2] == 0x56 && p[3] == 0x78)
    740 		need_swap = 0;
    741 	else
    742 		need_swap = 1;
    743 	if (need_swap) {
    744 		sc->sc_aperbase += 0x800000;
    745 		sc->sc_apersize -= 0x800000;
    746 	}
    747 	*p32 = saved_value;
    748 
    749 	LIST_INIT(&sc->screens);
    750 	sc->active = NULL;
    751 	sc->currenttype = &mach64_defaultscreen;
    752 	callout_init(&sc->switch_callout);
    753 }
    754 
    755 int
    756 mach64_get_memsize(struct mach64_softc *sc)
    757 {
    758 	int tmp, memsize;
    759 	int mem_tab[] = {
    760 		512, 1024, 2048, 4096, 6144, 8192, 12288, 16384
    761 	};
    762 
    763 	tmp = regr(sc, MEM_CNTL);
    764 	printf("memctl: %08x\n",tmp);
    765 	if (sc->has_dsp) {
    766 		tmp &= 0x0000000f;
    767 		if (tmp < 8)
    768 			memsize = (tmp + 1) * 512;
    769 		else if (tmp < 12)
    770 			memsize = (tmp - 3) * 1024;
    771 		else
    772 			memsize = (tmp - 7) * 2048;
    773 	} else {
    774 		memsize = mem_tab[tmp & 0x07];
    775 	}
    776 
    777 	return memsize;
    778 }
    779 
    780 int
    781 mach64_get_max_ramdac(struct mach64_softc *sc)
    782 {
    783 	int i;
    784 
    785 	if ((mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
    786 	     mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II) &&
    787 	     (mach64_chip_rev & 0x07))
    788 		return 170000;
    789 
    790 	for (i = 0; i < sizeof(mach64_info) / sizeof(mach64_info[0]); i++)
    791 		if (mach64_chip_id == mach64_info[i].chip_id)
    792 			return mach64_info[i].ramdac_freq;
    793 
    794 	if (sc->bits_per_pixel == 8)
    795 		return 135000;
    796 	else
    797 		return 80000;
    798 }
    799 
    800 void
    801 mach64_get_mode(struct mach64_softc *sc, struct videomode *mode)
    802 {
    803 	struct mach64_crtcregs crtc;
    804 
    805 	crtc.h_total_disp = regr(sc, CRTC_H_TOTAL_DISP);
    806 	crtc.h_sync_strt_wid = regr(sc, CRTC_H_SYNC_STRT_WID);
    807 	crtc.v_total_disp = regr(sc, CRTC_V_TOTAL_DISP);
    808 	crtc.v_sync_strt_wid = regr(sc, CRTC_V_SYNC_STRT_WID);
    809 
    810 	mode->htotal = ((crtc.h_total_disp & 0xffff) + 1) << 3;
    811 	mode->hdisplay = ((crtc.h_total_disp >> 16) + 1) << 3;
    812 	mode->hsync_start = ((crtc.h_sync_strt_wid & 0xffff) + 1) << 3;
    813 	mode->hsync_end = ((crtc.h_sync_strt_wid >> 16) << 3) +
    814 	    mode->hsync_start;
    815 	mode->vtotal = (crtc.v_total_disp & 0xffff) + 1;
    816 	mode->vdisplay = (crtc.v_total_disp >> 16) + 1;
    817 	mode->vsync_start = (crtc.v_sync_strt_wid & 0xffff) + 1;
    818 	mode->vsync_end = (crtc.v_sync_strt_wid >> 16) + mode->vsync_start;
    819 
    820 #ifdef DEBUG_MACHFB
    821 	printf("mach64_get_mode: %d %d %d %d %d %d %d %d\n",
    822 	    mode->hdisplay, mode->hsync_start, mode->hsync_end, mode->htotal,
    823 	    mode->vdisplay, mode->vsync_start, mode->vsync_end, mode->vtotal);
    824 #endif
    825 }
    826 
    827 int
    828 mach64_calc_crtcregs(struct mach64_softc *sc, struct mach64_crtcregs *crtc,
    829     struct videomode *mode)
    830 {
    831 
    832 	if (mode->dot_clock > sc->ramdac_freq)
    833 		/* Clock too high. */
    834 		return 1;
    835 
    836 	crtc->h_total_disp = (((mode->hdisplay >> 3) - 1) << 16) |
    837 	    ((mode->htotal >> 3) - 1);
    838 	crtc->h_sync_strt_wid =
    839 	    (((mode->hsync_end - mode->hsync_start) >> 3) << 16) |
    840 	    ((mode->hsync_start >> 3) - 1);
    841 
    842 	crtc->v_total_disp = ((mode->vdisplay - 1) << 16) |
    843 	    (mode->vtotal - 1);
    844 	crtc->v_sync_strt_wid =
    845 	    ((mode->vsync_end - mode->vsync_start) << 16) |
    846 	    (mode->vsync_start - 1);
    847 
    848 	if (mode->flags & VID_NVSYNC)
    849 		crtc->v_sync_strt_wid |= CRTC_VSYNC_NEG;
    850 
    851 	switch (sc->bits_per_pixel) {
    852 	case 8:
    853 		crtc->color_depth = CRTC_PIX_WIDTH_8BPP;
    854 		break;
    855 	case 16:
    856 		crtc->color_depth = CRTC_PIX_WIDTH_16BPP;
    857 		break;
    858 	case 32:
    859 		crtc->color_depth = CRTC_PIX_WIDTH_32BPP;
    860 		break;
    861 	}
    862 
    863 	crtc->gen_cntl = 0;
    864 	if (mode->flags & VID_INTERLACE)
    865 		crtc->gen_cntl |= CRTC_INTERLACE_EN;
    866 	if (mode->flags & VID_CSYNC)
    867 		crtc->gen_cntl |= CRTC_CSYNC_EN;
    868 
    869 	crtc->dot_clock = mode->dot_clock;
    870 
    871 	return 0;
    872 }
    873 
    874 void
    875 mach64_set_crtcregs(struct mach64_softc *sc, struct mach64_crtcregs *crtc)
    876 {
    877 
    878 	mach64_set_pll(sc, crtc->dot_clock);
    879 
    880 	if (sc->has_dsp)
    881 		mach64_set_dsp(sc);
    882 
    883 	regw(sc, CRTC_H_TOTAL_DISP, crtc->h_total_disp);
    884 	regw(sc, CRTC_H_SYNC_STRT_WID, crtc->h_sync_strt_wid);
    885 	regw(sc, CRTC_V_TOTAL_DISP, crtc->v_total_disp);
    886 	regw(sc, CRTC_V_SYNC_STRT_WID, crtc->v_sync_strt_wid);
    887 
    888 	regw(sc, CRTC_VLINE_CRNT_VLINE, 0);
    889 
    890 	regw(sc, CRTC_OFF_PITCH, (sc->virt_x >> 3) << 22);
    891 
    892 	regw(sc, CRTC_GEN_CNTL, crtc->gen_cntl | crtc->color_depth |
    893 	    CRTC_EXT_DISP_EN | CRTC_EXT_EN);
    894 }
    895 
    896 int
    897 mach64_modeswitch(struct mach64_softc *sc, struct videomode *mode)
    898 {
    899 	struct mach64_crtcregs crtc;
    900 
    901 	if (mach64_calc_crtcregs(sc, &crtc, mode))
    902 		return 1;
    903 
    904 	mach64_set_crtcregs(sc, &crtc);
    905 	return 0;
    906 }
    907 
    908 void
    909 mach64_reset_engine(struct mach64_softc *sc)
    910 {
    911 
    912 	/* Reset engine.*/
    913 	regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) & ~GUI_ENGINE_ENABLE);
    914 
    915 	/* Enable engine. */
    916 	regw(sc, GEN_TEST_CNTL, regr(sc, GEN_TEST_CNTL) | GUI_ENGINE_ENABLE);
    917 
    918 	/* Ensure engine is not locked up by clearing any FIFO or
    919 	   host errors. */
    920 	regw(sc, BUS_CNTL, regr(sc, BUS_CNTL) | BUS_HOST_ERR_ACK |
    921 	    BUS_FIFO_ERR_ACK);
    922 }
    923 
    924 void
    925 mach64_init_engine(struct mach64_softc *sc)
    926 {
    927 	u_int32_t pitch_value;
    928 
    929 	pitch_value = sc->virt_x;
    930 
    931 	if (sc->bits_per_pixel == 24)
    932 		pitch_value *= 3;
    933 
    934 	mach64_reset_engine(sc);
    935 
    936 	wait_for_fifo(sc, 14);
    937 
    938 	regw(sc, CONTEXT_MASK, 0xffffffff);
    939 
    940 	regw(sc, DST_OFF_PITCH, (pitch_value / 8) << 22);
    941 
    942 	regw(sc, DST_Y_X, 0);
    943 	regw(sc, DST_HEIGHT, 0);
    944 	regw(sc, DST_BRES_ERR, 0);
    945 	regw(sc, DST_BRES_INC, 0);
    946 	regw(sc, DST_BRES_DEC, 0);
    947 
    948 	regw(sc, DST_CNTL, DST_LAST_PEL | DST_X_LEFT_TO_RIGHT |
    949 	    DST_Y_TOP_TO_BOTTOM);
    950 
    951 	regw(sc, SRC_OFF_PITCH, (pitch_value / 8) << 22);
    952 
    953 	regw(sc, SRC_Y_X, 0);
    954 	regw(sc, SRC_HEIGHT1_WIDTH1, 1);
    955 	regw(sc, SRC_Y_X_START, 0);
    956 	regw(sc, SRC_HEIGHT2_WIDTH2, 1);
    957 
    958 	regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
    959 
    960 	wait_for_fifo(sc, 13);
    961 	regw(sc, HOST_CNTL, 0);
    962 
    963 	regw(sc, PAT_REG0, 0);
    964 	regw(sc, PAT_REG1, 0);
    965 	regw(sc, PAT_CNTL, 0);
    966 
    967 	regw(sc, SC_LEFT, 0);
    968 	regw(sc, SC_TOP, 0);
    969 	regw(sc, SC_BOTTOM, default_mode.vdisplay - 1);
    970 	regw(sc, SC_RIGHT, pitch_value - 1);
    971 
    972 	regw(sc, DP_BKGD_CLR, 0);
    973 	regw(sc, DP_FRGD_CLR, 0xffffffff);
    974 	regw(sc, DP_WRITE_MASK, 0xffffffff);
    975 	regw(sc, DP_MIX, (MIX_SRC << 16) | MIX_DST);
    976 
    977 	regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
    978 
    979 	wait_for_fifo(sc, 3);
    980 	regw(sc, CLR_CMP_CLR, 0);
    981 	regw(sc, CLR_CMP_MASK, 0xffffffff);
    982 	regw(sc, CLR_CMP_CNTL, 0);
    983 
    984 	wait_for_fifo(sc, 2);
    985 	switch (sc->bits_per_pixel) {
    986 	case 8:
    987 		regw(sc, DP_PIX_WIDTH, HOST_8BPP | SRC_8BPP | DST_8BPP);
    988 		regw(sc, DP_CHAIN_MASK, DP_CHAIN_8BPP);
    989 		/* We want 8 bit per channel */
    990 		regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
    991 		break;
    992 #if 0
    993 	case 32:
    994 		regw(sc, DP_PIX_WIDTH, HOST_32BPP | SRC_32BPP | DST_32BPP);
    995 		regw(sc, DP_CHAIN_MASK, DP_CHAIN_32BPP);
    996 		regw(sc, DAC_CNTL, regr(sc, DAC_CNTL) | DAC_8BIT_EN);
    997 		break;
    998 #endif
    999 	}
   1000 
   1001 	wait_for_fifo(sc, 5);
   1002 	regw(sc, CRTC_INT_CNTL, regr(sc, CRTC_INT_CNTL) & ~0x20);
   1003 	regw(sc, GUI_TRAJ_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
   1004 
   1005 	wait_for_idle(sc);
   1006 }
   1007 
   1008 void
   1009 mach64_adjust_frame(struct mach64_softc *sc, int x, int y)
   1010 {
   1011 	int offset;
   1012 
   1013 	offset = ((x + y * sc->virt_x) * (sc->bits_per_pixel >> 3)) >> 3;
   1014 
   1015 	regw(sc, CRTC_OFF_PITCH, (regr(sc, CRTC_OFF_PITCH) & 0xfff00000) |
   1016 	     offset);
   1017 }
   1018 
   1019 void
   1020 mach64_set_dsp(struct mach64_softc *sc)
   1021 {
   1022 	u_int32_t fifo_depth, page_size, dsp_precision, dsp_loop_latency;
   1023 	u_int32_t dsp_off, dsp_on, dsp_xclks_per_qw;
   1024 	u_int32_t xclks_per_qw, y;
   1025 	u_int32_t fifo_off, fifo_on;
   1026 
   1027 	printf("initializing the DSP\n");
   1028 	if (mach64_chip_id == PCI_PRODUCT_ATI_MACH64_VT ||
   1029 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_II ||
   1030 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIP ||
   1031 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_PCI ||
   1032 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_AGP_B ||
   1033 	    mach64_chip_id == PCI_PRODUCT_ATI_RAGE_IIC_AGP_P) {
   1034 		dsp_loop_latency = 0;
   1035 		fifo_depth = 24;
   1036 	} else {
   1037 		dsp_loop_latency = 2;
   1038 		fifo_depth = 32;
   1039 	}
   1040 
   1041 	dsp_precision = 0;
   1042 	xclks_per_qw = (sc->mclk_fb_div * sc->vclk_post_div * 64 << 11) /
   1043 	    (sc->vclk_fb_div * sc->mclk_post_div * sc->bits_per_pixel);
   1044 	y = (xclks_per_qw * fifo_depth) >> 11;
   1045 	while (y) {
   1046 		y >>= 1;
   1047 		dsp_precision++;
   1048 	}
   1049 	dsp_precision -= 5;
   1050 	fifo_off = ((xclks_per_qw * (fifo_depth - 1)) >> 5) + (3 << 6);
   1051 
   1052 	switch (sc->memtype) {
   1053 	case DRAM:
   1054 	case EDO_DRAM:
   1055 	case PSEUDO_EDO:
   1056 		if (sc->memsize > 1024) {
   1057 			page_size = 9;
   1058 			dsp_loop_latency += 6;
   1059 		} else {
   1060 			page_size = 10;
   1061 			if (sc->memtype == DRAM)
   1062 				dsp_loop_latency += 8;
   1063 			else
   1064 				dsp_loop_latency += 7;
   1065 		}
   1066 		break;
   1067 	case SDRAM:
   1068 	case SGRAM:
   1069 		if (sc->memsize > 1024) {
   1070 			page_size = 8;
   1071 			dsp_loop_latency += 8;
   1072 		} else {
   1073 			page_size = 10;
   1074 			dsp_loop_latency += 9;
   1075 		}
   1076 		break;
   1077 	default:
   1078 		page_size = 10;
   1079 		dsp_loop_latency += 9;
   1080 		break;
   1081 	}
   1082 
   1083 	if (xclks_per_qw >= (page_size << 11))
   1084 		fifo_on = ((2 * page_size + 1) << 6) + (xclks_per_qw >> 5);
   1085 	else
   1086 		fifo_on = (3 * page_size + 2) << 6;
   1087 
   1088 	dsp_xclks_per_qw = xclks_per_qw >> dsp_precision;
   1089 	dsp_on = fifo_on >> dsp_precision;
   1090 	dsp_off = fifo_off >> dsp_precision;
   1091 
   1092 #ifdef DEBUG_MACHFB
   1093 	printf("dsp_xclks_per_qw = %d, dsp_on = %d, dsp_off = %d,\n"
   1094 	    "dsp_precision = %d, dsp_loop_latency = %d,\n"
   1095 	    "mclk_fb_div = %d, vclk_fb_div = %d,\n"
   1096 	    "mclk_post_div = %d, vclk_post_div = %d\n",
   1097 	    dsp_xclks_per_qw, dsp_on, dsp_off, dsp_precision, dsp_loop_latency,
   1098 	    sc->mclk_fb_div, sc->vclk_fb_div,
   1099 	    sc->mclk_post_div, sc->vclk_post_div);
   1100 #endif
   1101 
   1102 	regw(sc, DSP_ON_OFF, ((dsp_on << 16) & DSP_ON) | (dsp_off & DSP_OFF));
   1103 	regw(sc, DSP_CONFIG, ((dsp_precision << 20) & DSP_PRECISION) |
   1104 	    ((dsp_loop_latency << 16) & DSP_LOOP_LATENCY) |
   1105 	    (dsp_xclks_per_qw & DSP_XCLKS_PER_QW));
   1106 }
   1107 
   1108 void
   1109 mach64_set_pll(struct mach64_softc *sc, int clock)
   1110 {
   1111 	int q;
   1112 
   1113 	q = (clock * sc->ref_div * 100) / (2 * sc->ref_freq);
   1114 #ifdef DEBUG_MACHFB
   1115 	printf("q = %d\n", q);
   1116 #endif
   1117 	if (q > 25500) {
   1118 		printf("Warning: q > 25500\n");
   1119 		q = 25500;
   1120 		sc->vclk_post_div = 1;
   1121 		sc->log2_vclk_post_div = 0;
   1122 	} else if (q > 12750) {
   1123 		sc->vclk_post_div = 1;
   1124 		sc->log2_vclk_post_div = 0;
   1125 	} else if (q > 6350) {
   1126 		sc->vclk_post_div = 2;
   1127 		sc->log2_vclk_post_div = 1;
   1128 	} else if (q > 3150) {
   1129 		sc->vclk_post_div = 4;
   1130 		sc->log2_vclk_post_div = 2;
   1131 	} else if (q >= 1600) {
   1132 		sc->vclk_post_div = 8;
   1133 		sc->log2_vclk_post_div = 3;
   1134 	} else {
   1135 		printf("Warning: q < 1600\n");
   1136 		sc->vclk_post_div = 8;
   1137 		sc->log2_vclk_post_div = 3;
   1138 	}
   1139 	sc->vclk_fb_div = q * sc->vclk_post_div / 100;
   1140 
   1141 	regwb_pll(sc, MCLK_FB_DIV, sc->mclk_fb_div);
   1142 	regwb_pll(sc, VCLK_POST_DIV, sc->log2_vclk_post_div);
   1143 	regwb_pll(sc, VCLK0_FB_DIV, sc->vclk_fb_div);
   1144 }
   1145 
   1146 void
   1147 mach64_init_lut(struct mach64_softc *sc)
   1148 {
   1149 	int i, idx;
   1150 
   1151 	idx = 0;
   1152 	for (i = 0; i < 256; i++) {
   1153 		mach64_putpalreg(sc, i, rasops_cmap[idx], rasops_cmap[idx + 1],
   1154 		    rasops_cmap[idx + 2]);
   1155 		idx += 3;
   1156 	}
   1157 }
   1158 
   1159 int
   1160 mach64_putpalreg(struct mach64_softc *sc, uint8_t index, uint8_t r, uint8_t g,
   1161     uint8_t b)
   1162 {
   1163 	sc->sc_cmap_red[index] = r;
   1164 	sc->sc_cmap_green[index] = g;
   1165 	sc->sc_cmap_blue[index] = b;
   1166 	/*
   1167 	 * writing the dac index takes a while, in theory we can poll some
   1168 	 * register to see when it's ready - but we better avoid writing it
   1169 	 * unnecessarily
   1170 	 */
   1171 	if (index != sc->sc_dacw) {
   1172 		regwb(sc, DAC_MASK, 0xff);
   1173 		regwb(sc, DAC_WINDEX, index);
   1174 	}
   1175 	sc->sc_dacw = index + 1;
   1176 	regwb(sc, DAC_DATA, r);
   1177 	regwb(sc, DAC_DATA, g);
   1178 	regwb(sc, DAC_DATA, b);
   1179 	return 0;
   1180 }
   1181 
   1182 int
   1183 mach64_putcmap(struct mach64_softc *sc, struct wsdisplay_cmap *cm)
   1184 {
   1185 	u_int index = cm->index;
   1186 	u_int count = cm->count;
   1187 	int i, error;
   1188 	u_char rbuf[256], gbuf[256], bbuf[256];
   1189 	u_char *r, *g, *b;
   1190 
   1191 	printf("putcmap: %d %d\n",index, count);
   1192 	if (cm->index >= 256 || cm->count > 256 ||
   1193 	    (cm->index + cm->count) > 256)
   1194 		return EINVAL;
   1195 	error = copyin(cm->red, &rbuf[index], count);
   1196 	if (error)
   1197 		return error;
   1198 	error = copyin(cm->green, &gbuf[index], count);
   1199 	if (error)
   1200 		return error;
   1201 	error = copyin(cm->blue, &bbuf[index], count);
   1202 	if (error)
   1203 		return error;
   1204 
   1205 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
   1206 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
   1207 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
   1208 
   1209 	r = &sc->sc_cmap_red[index];
   1210 	g = &sc->sc_cmap_green[index];
   1211 	b = &sc->sc_cmap_blue[index];
   1212 
   1213 	for (i = 0; i < count; i++) {
   1214 		mach64_putpalreg(sc, index, *r, *g, *b);
   1215 		index++;
   1216 		r++, g++, b++;
   1217 	}
   1218 	return 0;
   1219 }
   1220 
   1221 int
   1222 mach64_getcmap(struct mach64_softc *sc, struct wsdisplay_cmap *cm)
   1223 {
   1224 	u_int index = cm->index;
   1225 	u_int count = cm->count;
   1226 	int error;
   1227 
   1228 	if (index >= 255 || count > 256 || index + count > 256)
   1229 		return EINVAL;
   1230 
   1231 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
   1232 	if (error)
   1233 		return error;
   1234 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
   1235 	if (error)
   1236 		return error;
   1237 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
   1238 	if (error)
   1239 		return error;
   1240 
   1241 	return 0;
   1242 }
   1243 
   1244 int
   1245 mach64_set_screentype(struct mach64_softc *sc, const struct wsscreen_descr *des)
   1246 {
   1247 	struct mach64_crtcregs regs;
   1248 
   1249 	if (mach64_calc_crtcregs(sc, &regs,
   1250 	    (struct videomode *)des->modecookie))
   1251 		return 1;
   1252 
   1253 	mach64_set_crtcregs(sc, &regs);
   1254 	return 0;
   1255 }
   1256 
   1257 int
   1258 mach64_is_console(struct pci_attach_args *pa)
   1259 {
   1260 #ifdef __sparc__
   1261 	int node;
   1262 
   1263 	node = PCITAG_NODE(pa->pa_tag);
   1264 	if (node == -1)
   1265 		return 0;
   1266 
   1267 	return (node == prom_instance_to_package(prom_stdout()));
   1268 #elif defined(__powerpc__)
   1269 	/* check if we're the /chosen console device */
   1270 	int chosen, stdout, node, us;
   1271 
   1272 	us = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
   1273 	chosen = OF_finddevice("/chosen");
   1274 	OF_getprop(chosen, "stdout", &stdout, 4);
   1275 	node = OF_instance_to_package(stdout);
   1276 	return (us == node);
   1277 #else
   1278 	return 1;
   1279 #endif
   1280 }
   1281 
   1282 /*
   1283  * wsdisplay_emulops
   1284  */
   1285 
   1286 void
   1287 mach64_cursor(void *cookie, int on, int row, int col)
   1288 {
   1289 	struct rasops_info *ri = cookie;
   1290 	struct mach64screen *scr = ri->ri_hw;
   1291 	struct mach64_softc *sc = scr->sc;
   1292 	int x, y, wi,he;
   1293 
   1294 	wi = ri->ri_font->fontwidth;
   1295 	he = ri->ri_font->fontheight;
   1296 
   1297 	if (scr->active) {
   1298 		x = scr->cursorcol * wi + ri->ri_xorigin;
   1299 		y = scr->cursorrow * he + ri->ri_yorigin;
   1300 		if (scr->cursordrawn) {
   1301 			mach64_bitblt(sc, x, y, x, y, wi, he, MIX_NOT_SRC,
   1302 			    0xff);
   1303 			scr->cursordrawn=0;
   1304 		}
   1305 		scr->cursorrow = row;
   1306 		scr->cursorcol = col;
   1307 		if ((scr->cursoron = on) != 0)
   1308 		{
   1309 			x = scr->cursorcol * wi + ri->ri_xorigin;
   1310 			y = scr->cursorrow * he + ri->ri_yorigin;
   1311 			mach64_bitblt(sc, x, y, x, y, wi, he, MIX_NOT_SRC,
   1312 			    0xff);
   1313 			scr->cursordrawn = 1;
   1314 		}
   1315 	} else {
   1316 		scr->cursoron = on;
   1317 		scr->cursorrow = row;
   1318 		scr->cursorcol = col;
   1319 		scr->cursordrawn = 0;
   1320 	}
   1321 }
   1322 
   1323 #if 0
   1324 int
   1325 mach64_mapchar(void *cookie, int uni, u_int *index)
   1326 {
   1327 	return 0;
   1328 }
   1329 #endif
   1330 
   1331 void
   1332 mach64_putchar(void *cookie, int row, int col, u_int c, long attr)
   1333 {
   1334 	struct rasops_info *ri = cookie;
   1335 	struct mach64screen *scr = ri->ri_hw;
   1336 	struct mach64_softc *sc = scr->sc;
   1337 	int offset = ri->ri_cols * row + col;
   1338 
   1339 	scr->attrs[offset] = attr;
   1340 	scr->chars[offset] = c;
   1341 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1342 		int fg, bg, uc;
   1343 		uint8_t *data;
   1344 		int x, y, wi, he;
   1345 		wi = ri->ri_font->fontwidth;
   1346 		he = ri->ri_font->fontheight;
   1347 
   1348 #ifdef notdef
   1349 		scr->putchar(cookie,row,col,c,attr);
   1350 #endif
   1351 		if (!CHAR_IN_FONT(c, ri->ri_font))
   1352 			return;
   1353 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
   1354 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
   1355 		x = ri->ri_xorigin + col * wi;
   1356 		y = ri->ri_yorigin + row * he;
   1357 		if (c == 0x20) {
   1358 			mach64_rectfill(sc, x, y, wi, he, bg);
   1359 		} else {
   1360 			uc = c-ri->ri_font->firstchar;
   1361 			data = (uint8_t *)ri->ri_font->data + uc *
   1362 			    ri->ri_fontscale;
   1363 
   1364 			mach64_setup_mono(sc, x, y, wi, he, fg, bg);
   1365 			mach64_feed_bytes(sc, ri->ri_fontscale, data);
   1366 		}
   1367 	}
   1368 }
   1369 
   1370 
   1371 void
   1372 mach64_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1373 {
   1374 	struct rasops_info *ri=cookie;
   1375 	struct mach64screen *scr=ri->ri_hw;
   1376 	struct mach64_softc *sc=scr->sc;
   1377 	int32_t xs,xd,y,width,height;
   1378 	int from, to;
   1379 
   1380 	from = srccol + row * ri->ri_cols;
   1381 	to = dstcol + row * ri->ri_cols;
   1382 
   1383 	memmove(&scr->attrs[to], &scr->attrs[from], ncols * sizeof(long));
   1384 	memmove(&scr->chars[to], &scr->chars[from], ncols * sizeof(u_int));
   1385 
   1386 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1387 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1388 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1389 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1390 		width = ri->ri_font->fontwidth * ncols;
   1391 		height = ri->ri_font->fontheight;
   1392 		mach64_bitblt(sc, xs, y, xd, y, width, height, MIX_SRC, 0xff);
   1393 	}
   1394 }
   1395 
   1396 void
   1397 mach64_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1398 {
   1399 	struct rasops_info *ri=cookie;
   1400 	struct mach64screen *scr=ri->ri_hw;
   1401 	struct mach64_softc *sc=scr->sc;
   1402 	int32_t x, y, width, height, fg, bg, ul;
   1403 	int start, end, i;
   1404 
   1405 	start = startcol + row * ri->ri_cols;
   1406 	end = start + ncols;
   1407 
   1408 	for (i = start; i < end; i++) {
   1409 		scr->attrs[i] = fillattr;
   1410 		scr->chars[i] = 0x20;
   1411 	}
   1412 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1413 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1414 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1415 		width = ri->ri_font->fontwidth * ncols;
   1416 		height = ri->ri_font->fontheight;
   1417 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1418 
   1419 		mach64_rectfill(sc, x, y, width, height, bg);
   1420 	}
   1421 }
   1422 
   1423 void
   1424 mach64_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1425 {
   1426 	struct rasops_info *ri=cookie;
   1427 	struct mach64screen *scr=ri->ri_hw;
   1428 	struct mach64_softc *sc=scr->sc;
   1429 	int32_t x, ys, yd, width, height;
   1430 	int from, to, len;
   1431 
   1432 	from = ri->ri_cols * srcrow;
   1433 	to = ri->ri_cols * dstrow;
   1434 	len = ri->ri_cols * nrows;
   1435 
   1436 	memmove(&scr->attrs[to], &scr->attrs[from], len*sizeof(long));
   1437 	memmove(&scr->chars[to], &scr->chars[from], len*sizeof(u_int));
   1438 
   1439 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1440 		x = ri->ri_xorigin;
   1441 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1442 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1443 		width = ri->ri_emuwidth;
   1444 		height = ri->ri_font->fontheight*nrows;
   1445 		mach64_bitblt(sc, x, ys, x, yd, width, height, MIX_SRC, 0xff);
   1446 	}
   1447 }
   1448 
   1449 void
   1450 mach64_eraserows(void *cookie, int row, int nrows, long fillattr)
   1451 {
   1452 	struct rasops_info *ri=cookie;
   1453 	struct mach64screen *scr=ri->ri_hw;
   1454 	struct mach64_softc *sc=scr->sc;
   1455 	int32_t x,y,width,height,fg,bg,ul;
   1456 	int start, end, i;
   1457 
   1458 	start = ri->ri_cols * row;
   1459 	end = ri->ri_cols * (row + nrows);
   1460 
   1461 	for (i=start;i<end;i++) {
   1462 		scr->attrs[i] = fillattr;
   1463 		scr->chars[i] = 0x20;
   1464 	}
   1465 
   1466 	if ((scr->active) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1467 		x = ri->ri_xorigin;
   1468 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1469 		width = ri->ri_emuwidth;
   1470 		height = ri->ri_font->fontheight * nrows;
   1471 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1472 
   1473 		mach64_rectfill(sc, x, y, width, height, bg);
   1474 	}
   1475 }
   1476 
   1477 void
   1478 mach64_bitblt(struct mach64_softc *sc, int xs, int ys, int xd, int yd, int width, int height, int rop, int mask)
   1479 {
   1480 	uint32_t dest_ctl = 0;
   1481 
   1482 	wait_for_idle(sc);
   1483 	regw(sc, DP_WRITE_MASK, mask);	/* XXX only good for 8 bit */
   1484 	regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
   1485 	regw(sc, DP_SRC, FRGD_SRC_BLIT);
   1486 	regw(sc, DP_MIX, (rop & 0xffff) << 16);
   1487 	regw(sc, CLR_CMP_CNTL, 0);	/* no transparency */
   1488 	if (yd < ys) {
   1489 		dest_ctl = DST_Y_TOP_TO_BOTTOM;
   1490 	} else {
   1491 		ys += height - 1;
   1492 		yd += height - 1;
   1493 		dest_ctl = DST_Y_BOTTOM_TO_TOP;
   1494 	}
   1495 	if (xd < xs) {
   1496 		dest_ctl |= DST_X_LEFT_TO_RIGHT;
   1497 		regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
   1498 	} else {
   1499 		dest_ctl |= DST_X_RIGHT_TO_LEFT;
   1500 		xs += width - 1;
   1501 		xd += width - 1;
   1502 		regw(sc, SRC_CNTL, SRC_LINE_X_RIGHT_TO_LEFT);
   1503 	}
   1504 	regw(sc, DST_CNTL, dest_ctl);
   1505 
   1506 	regw(sc, SRC_Y_X, (xs << 16) | ys);
   1507 	regw(sc, SRC_WIDTH1, width);
   1508 	regw(sc, DST_Y_X, (xd << 16) | yd);
   1509 	regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
   1510 }
   1511 
   1512 void
   1513 mach64_setup_mono(struct mach64_softc *sc, int xd, int yd, int width,
   1514      int height, uint32_t fg, uint32_t bg)
   1515 {
   1516 	wait_for_idle(sc);
   1517 	regw(sc, DP_WRITE_MASK, 0xff);	/* XXX only good for 8 bit */
   1518 	regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_1BPP | HOST_1BPP);
   1519 	regw(sc, DP_SRC, MONO_SRC_HOST | BKGD_SRC_BKGD_CLR | FRGD_SRC_FRGD_CLR);
   1520 	regw(sc, DP_MIX, ((MIX_SRC & 0xffff) << 16) | MIX_SRC);
   1521 	regw(sc, CLR_CMP_CNTL ,0);	/* no transparency */
   1522 	regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
   1523 	regw(sc, DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT);
   1524 	regw(sc, HOST_CNTL, HOST_BYTE_ALIGN);
   1525 	regw(sc, DP_BKGD_CLR, bg);
   1526 	regw(sc, DP_FRGD_CLR, fg);
   1527 	regw(sc, SRC_Y_X, 0);
   1528 	regw(sc, SRC_WIDTH1, width);
   1529 	regw(sc, DST_Y_X, (xd << 16) | yd);
   1530 	regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
   1531 	/* now feed the data into the chip */
   1532 }
   1533 
   1534 void
   1535 mach64_feed_bytes(struct mach64_softc *sc, int count, uint8_t *data)
   1536 {
   1537 	int i;
   1538 	uint32_t latch = 0, bork;
   1539 	int shift = 0;
   1540 	int reg = 0;
   1541 
   1542 	for (i=0;i<count;i++) {
   1543 		bork = data[i];
   1544 		latch |= (bork << shift);
   1545 		if (shift == 24) {
   1546 			regw(sc, HOST_DATA0 + reg, latch);
   1547 			latch = 0;
   1548 			shift = 0;
   1549 			reg = (reg + 4) & 0x3c;
   1550 		} else
   1551 			shift += 8;
   1552 	}
   1553 	if (shift != 0)	/* 24 */
   1554 		regw(sc, HOST_DATA0 + reg, latch);
   1555 }
   1556 
   1557 
   1558 void
   1559 mach64_rectfill(struct mach64_softc *sc, int x, int y, int width, int height,
   1560     int colour)
   1561 {
   1562 	wait_for_idle(sc);
   1563 	regw(sc, DP_WRITE_MASK, 0xff);
   1564 	regw(sc, DP_FRGD_CLR, colour);
   1565 	regw(sc, DP_PIX_WIDTH, DST_8BPP | SRC_8BPP | HOST_8BPP);
   1566 	regw(sc, DP_SRC, FRGD_SRC_FRGD_CLR);
   1567 	regw(sc, DP_MIX, MIX_SRC << 16);
   1568 	regw(sc, CLR_CMP_CNTL, 0);	/* no transparency */
   1569 	regw(sc, SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT);
   1570 	regw(sc, DST_CNTL, DST_X_LEFT_TO_RIGHT | DST_Y_TOP_TO_BOTTOM);
   1571 
   1572 	regw(sc, SRC_Y_X, (x << 16) | y);
   1573 	regw(sc, SRC_WIDTH1, width);
   1574 	regw(sc, DST_Y_X, (x << 16) | y);
   1575 	regw(sc, DST_HEIGHT_WIDTH, (width << 16) | height);
   1576 }
   1577 
   1578 void
   1579 mach64_clearscreen(struct mach64_softc *sc)
   1580 {
   1581 	mach64_rectfill(sc, 0, 0, sc->virt_x, sc->virt_y, sc->sc_bg);
   1582 }
   1583 
   1584 
   1585 void
   1586 mach64_showpal(struct mach64_softc *sc)
   1587 {
   1588 	int i, x = 0;
   1589 
   1590 	for (i = 0; i < 16; i++) {
   1591 		mach64_rectfill(sc, x, 0, 64, 64, i);
   1592 		x += 64;
   1593 	}
   1594 }
   1595 
   1596 int
   1597 mach64_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
   1598 {
   1599 	if ((fg == 0) && (bg == 0))
   1600 	{
   1601 		fg = WS_DEFAULT_FG;
   1602 		bg = WS_DEFAULT_BG;
   1603 	}
   1604 	*attrp = (fg & 0xf) << 24 | (bg & 0xf) << 16 | (flags & 0xff) << 8;
   1605 	return 0;
   1606 }
   1607 
   1608 /*
   1609  * wsdisplay_accessops
   1610  */
   1611 
   1612 int
   1613 mach64_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
   1614 {
   1615 	struct mach64_softc *sc = v;
   1616 	struct wsdisplay_fbinfo *wdf;
   1617 	struct mach64screen *ms=sc->active;
   1618 
   1619 	switch (cmd) {
   1620 		case WSDISPLAYIO_GTYPE:
   1621 			/* XXX is this the right type to return? */
   1622 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
   1623 			return 0;
   1624 
   1625 		case WSDISPLAYIO_GINFO:
   1626 			wdf = (void *)data;
   1627 			wdf->height = ms->ri.ri_height;
   1628 			wdf->width = ms->ri.ri_width;
   1629 			wdf->depth = ms->ri.ri_depth;
   1630 			wdf->cmsize = 256;
   1631 			return 0;
   1632 
   1633 		case WSDISPLAYIO_GETCMAP:
   1634 			return mach64_getcmap(sc,
   1635 			    (struct wsdisplay_cmap *)data);
   1636 
   1637 		case WSDISPLAYIO_PUTCMAP:
   1638 			return mach64_putcmap(sc,
   1639 			    (struct wsdisplay_cmap *)data);
   1640 
   1641 		/* PCI config read/write passthrough. */
   1642 		case PCI_IOC_CFGREAD:
   1643 		case PCI_IOC_CFGWRITE:
   1644 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
   1645 			    cmd, data, flag, p));
   1646 
   1647 		case WSDISPLAYIO_SMODE:
   1648 			{
   1649 				int new_mode = *(int*)data;
   1650 
   1651 				if (new_mode != sc->sc_mode)
   1652 				{
   1653 					sc->sc_mode = new_mode;
   1654 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
   1655 					{
   1656 						/*
   1657 						 * reset a few things the
   1658 						 * Xserver might have screwed up
   1659 						 */
   1660 						mach64_restore_screen(ms,
   1661 						    ms->type, ms->chars);
   1662 						mach64_cursor(ms, ms->cursoron,
   1663 						    ms->cursorrow,
   1664 						    ms->cursorcol);
   1665 					}
   1666 				}
   1667 			}
   1668 			return 0;
   1669 
   1670 		case WSDISPLAYIO_GETWSCHAR:
   1671 			return mach64_getwschar(sc,
   1672 			    (struct wsdisplay_char *)data);
   1673 
   1674 		case WSDISPLAYIO_PUTWSCHAR:
   1675 			return mach64_putwschar(sc,
   1676 			    (struct wsdisplay_char *)data);
   1677 	}
   1678 	return EPASSTHROUGH;
   1679 }
   1680 
   1681 paddr_t
   1682 mach64_mmap(void *v, off_t offset, int prot)
   1683 {
   1684 	struct mach64_softc *sc = v;
   1685 	paddr_t pa;
   1686 
   1687 	/* 'regular' framebuffer mmap()ing */
   1688 	if (offset<sc->sc_apersize) {
   1689 		pa = bus_space_mmap(sc->sc_memt, sc->sc_aperbase+offset, 0,
   1690 		    prot, BUS_SPACE_MAP_LINEAR);
   1691 		return pa;
   1692 	}
   1693 #if 0
   1694 	/* evil hack to allow mmap()ing other devices as well */
   1695 	if ((offset > 0x80000000) && (offset <= 0xffffffff)) {
   1696 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1697 		    BUS_SPACE_MAP_LINEAR);
   1698 		return pa;
   1699 	}
   1700 #endif
   1701 
   1702 	if ((offset >= sc->sc_aperphys) &&
   1703 	    (offset < (sc->sc_aperphys + sc->sc_apersize))) {
   1704 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1705 		    BUS_SPACE_MAP_LINEAR);
   1706 		return pa;
   1707 	}
   1708 
   1709 	if ((offset >= sc->sc_regphys) &&
   1710 	    (offset < (sc->sc_regphys + sc->sc_regsize))) {
   1711 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
   1712 		    BUS_SPACE_MAP_LINEAR);
   1713 		return pa;
   1714 	}
   1715 
   1716 	return -1;
   1717 }
   1718 
   1719 int
   1720 mach64_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
   1721     int *curxp, int *curyp, long *defattrp)
   1722 {
   1723 	struct mach64_softc *sc = v;
   1724 	struct mach64screen *scr;
   1725 	struct rasops_info *ri;
   1726 	int cnt = type->nrows * type->ncols;
   1727 
   1728 	scr = malloc(sizeof(struct mach64screen), M_DEVBUF, M_WAITOK | M_ZERO);
   1729 	mach64_init_screen(sc, scr, type, 0, defattrp, sc->active == NULL);
   1730 	ri = &scr->ri;
   1731 
   1732 	ri->ri_hw = scr;
   1733 #ifdef notdef
   1734 	ri->ri_bits = (void *)sc->sc_aperbase;
   1735 #endif
   1736 	rasops_init(ri, mach64_console_screen.ri.ri_height / 8,
   1737 	    mach64_console_screen.ri.ri_width / 8);
   1738 
   1739 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
   1740 	    ri->ri_width / ri->ri_font->fontwidth);
   1741 	set_address(ri, sc->sc_aperbase);
   1742 	mach64_allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattrp);
   1743 
   1744 	scr->ri.ri_ops.copyrows = mach64_copyrows;
   1745 	scr->ri.ri_ops.eraserows = mach64_eraserows;
   1746 	scr->ri.ri_ops.copycols = mach64_copycols;
   1747 	scr->ri.ri_ops.erasecols = mach64_erasecols;
   1748 	scr->ri.ri_ops.putchar = mach64_putchar;
   1749 	scr->ri.ri_ops.cursor = mach64_cursor;
   1750 
   1751 	scr->attrs = malloc(cnt * (sizeof(long) + sizeof(u_int)),
   1752 	    M_DEVBUF, M_WAITOK);
   1753 	scr->chars = (u_int *)&scr->attrs[cnt];
   1754 	mach64_eraserows(ri, 0, ri->ri_rows, *defattrp);
   1755 
   1756 	if (sc->active == NULL) {
   1757 		scr->active = 1;
   1758 		sc->active = scr;
   1759 		sc->currenttype = type;
   1760 	}
   1761 
   1762 	*cookiep = scr;
   1763 	*curxp = scr->cursorcol;
   1764 	*curyp = scr->cursorrow;
   1765 
   1766 	return 0;
   1767 }
   1768 
   1769 void
   1770 mach64_free_screen(void *v, void *cookie)
   1771 {
   1772 	struct mach64_softc *sc = v;
   1773 	struct mach64screen *scr = cookie;
   1774 
   1775 	LIST_REMOVE(scr, next);
   1776 	if (scr != &mach64_console_screen) {
   1777 		free(scr->attrs, M_DEVBUF);
   1778 		free(scr, M_DEVBUF);
   1779 	} else
   1780 		panic("mach64_free_screen: console");
   1781 
   1782 	if (sc->active == scr)
   1783 		sc->active = 0;
   1784 }
   1785 
   1786 int
   1787 mach64_show_screen(void *v, void *cookie, int waitok,
   1788     void (*cb)(void *, int, int), void *cbarg)
   1789 {
   1790 	struct mach64_softc *sc = v;
   1791 	struct mach64screen *scr, *oldscr;
   1792 
   1793 	scr = cookie;
   1794 	oldscr = sc->active;
   1795 	if (scr == oldscr)
   1796 		return 0;
   1797 
   1798 	sc->wanted = scr;
   1799 	sc->switchcb = cb;
   1800 	sc->switchcbarg = cbarg;
   1801 	if (cb) {
   1802 		callout_reset(&sc->switch_callout, 0,
   1803 		    (void(*)(void *))mach64_switch_screen, sc);
   1804 		return EAGAIN;
   1805 	}
   1806 
   1807 	mach64_switch_screen(sc);
   1808 
   1809 	return 0;
   1810 }
   1811 
   1812 void
   1813 mach64_switch_screen(struct mach64_softc *sc)
   1814 {
   1815 	struct mach64screen *scr, *oldscr;
   1816 	const struct wsscreen_descr *type;
   1817 
   1818 	scr = sc->wanted;
   1819 	if (!scr) {
   1820 		printf("mach64_switch_screen: disappeared\n");
   1821 		(*sc->switchcb)(sc->switchcbarg, EIO, 0);
   1822 		return;
   1823 	}
   1824 	type = scr->type;
   1825 	oldscr = sc->active; /* can be NULL! */
   1826 #ifdef DIAGNOSTIC
   1827 	if (oldscr) {
   1828 		if (!oldscr->active)
   1829 			panic("mach64_switch_screen: not active");
   1830 		if (oldscr->type != sc->currenttype)
   1831 			panic("mach64_switch_screen: bad type %p != %p",
   1832 			    oldscr->type, sc->currenttype);
   1833 	}
   1834 #endif
   1835 	if (scr == oldscr)
   1836 		return;
   1837 
   1838 #ifdef DIAGNOSTIC
   1839 /* XXX: this one bites us at reboot */
   1840 #ifdef notdef
   1841 	if (scr->active)
   1842 		panic("mach64_switch_screen: active");
   1843 #endif
   1844 #endif
   1845 
   1846 	if (oldscr)
   1847 		oldscr->active = 0;
   1848 
   1849 	if (sc->currenttype != type) {
   1850 		mach64_set_screentype(sc, type);
   1851 		sc->currenttype = type;
   1852 	}
   1853 
   1854 	scr->dispoffset = scr->mindispoffset;
   1855 
   1856 	if (!oldscr || (scr->dispoffset != oldscr->dispoffset)) {
   1857 
   1858 	}
   1859 
   1860 	/* Clear the entire screen. */
   1861 
   1862 	scr->active = 1;
   1863 	mach64_restore_screen(scr, type, scr->chars);
   1864 
   1865 	sc->active = scr;
   1866 
   1867 	scr->ri.ri_ops.cursor(scr, scr->cursoron, scr->cursorrow,
   1868 	    scr->cursorcol);
   1869 
   1870 	sc->wanted = 0;
   1871 	if (sc->switchcb)
   1872 		(*sc->switchcb)(sc->switchcbarg, 0, 0);
   1873 }
   1874 
   1875 void
   1876 mach64_restore_screen(struct mach64screen *scr,
   1877     const struct wsscreen_descr *type, u_int *mem)
   1878 {
   1879 	int i, j, offset = 0;
   1880 	u_int *charptr = scr->chars;
   1881 	long *attrptr = scr->attrs;
   1882 
   1883 	mach64_clearscreen(scr->sc);
   1884 	for (i = 0; i < scr->ri.ri_rows; i++) {
   1885 		for (j = 0; j < scr->ri.ri_cols; j++) {
   1886 			mach64_putchar(scr, i, j, charptr[offset],
   1887 			    attrptr[offset]);
   1888 			offset++;
   1889 		}
   1890 	}
   1891 	scr->cursordrawn = 0;
   1892 }
   1893 
   1894 /* set ri->ri_bits according to fb, ri_xorigin and ri_yorigin */
   1895 void
   1896 set_address(struct rasops_info *ri, bus_addr_t fb)
   1897 {
   1898 #ifdef notdef
   1899 	printf(" %d %d %d\n", ri->ri_xorigin, ri->ri_yorigin, ri->ri_stride);
   1900 #endif
   1901 	ri->ri_bits = (void *)((u_long)fb + ri->ri_stride * ri->ri_yorigin +
   1902 	    ri->ri_xorigin);
   1903 }
   1904 
   1905 int
   1906 mach64_getwschar(void *cookie, struct wsdisplay_char *wsc)
   1907 {
   1908 	struct mach64_softc *sc = cookie;
   1909 	struct mach64screen *scr = sc->active;
   1910 	int fg, bg, fl;
   1911 
   1912 	if (scr){
   1913 		if ((wsc->col >= 0) && (wsc->col < scr->ri.ri_cols) &&
   1914 		    (wsc->row >= 0) && (wsc->row < scr->ri.ri_rows)) {
   1915 			int pos = scr->ri.ri_cols * wsc->row + wsc->col;
   1916 
   1917 			wsc->letter = scr->chars[pos];
   1918 			rasops_unpack_attr(scr->attrs[pos], &fg, &bg, &fl);
   1919 			wsc->foreground = fg;
   1920 			wsc->background = bg;
   1921 			wsc->flags = fl;
   1922 			return 0;
   1923 		}
   1924 	}
   1925 	return EINVAL;
   1926 }
   1927 
   1928 int
   1929 mach64_putwschar(void *cookie, struct wsdisplay_char *wsc)
   1930 {
   1931 	struct mach64_softc *sc=cookie;
   1932 	struct mach64screen *scr=sc->active;
   1933 	long attr;
   1934 
   1935 	if (scr){
   1936 		if ((wsc->col >= 0) && (wsc->col < scr->ri.ri_cols) &&
   1937 		    (wsc->row >= 0) && (wsc->row < scr->ri.ri_rows)) {
   1938 			mach64_allocattr(&scr->ri, wsc->foreground,
   1939 			    wsc->background, wsc->flags,&attr);
   1940 			mach64_putchar(&scr->ri, wsc->row, wsc->col,
   1941 			    wsc->letter, attr);
   1942 			return 0;
   1943 		}
   1944 	}
   1945 	return EINVAL;
   1946 }
   1947 
   1948 #if 0
   1949 int
   1950 mach64_load_font(void *v, void *cookie, struct wsdisplay_font *data)
   1951 {
   1952 
   1953 	return 0;
   1954 }
   1955 #endif
   1956