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