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