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