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