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