Home | History | Annotate | Line # | Download | only in pci
unichromefb.c revision 1.4.10.6
      1  1.4.10.6  yamt /* $NetBSD: unichromefb.c,v 1.4.10.6 2008/03/17 09:15:12 yamt Exp $ */
      2  1.4.10.2  yamt 
      3  1.4.10.2  yamt /*-
      4  1.4.10.2  yamt  * Copyright (c) 2006 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.4.10.2  yamt  * All rights reserved.
      6  1.4.10.2  yamt  *
      7  1.4.10.2  yamt  * Redistribution and use in source and binary forms, with or without
      8  1.4.10.2  yamt  * modification, are permitted provided that the following conditions
      9  1.4.10.2  yamt  * are met:
     10  1.4.10.2  yamt  * 1. Redistributions of source code must retain the above copyright
     11  1.4.10.2  yamt  *    notice, this list of conditions and the following disclaimer.
     12  1.4.10.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.4.10.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     14  1.4.10.2  yamt  *    documentation and/or other materials provided with the distribution.
     15  1.4.10.2  yamt  * 3. All advertising materials mentioning features or use of this software
     16  1.4.10.2  yamt  *    must display the following acknowledgement:
     17  1.4.10.2  yamt  *        This product includes software developed by Jared D. McNeill.
     18  1.4.10.2  yamt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19  1.4.10.2  yamt  *    contributors may be used to endorse or promote products derived
     20  1.4.10.2  yamt  *    from this software without specific prior written permission.
     21  1.4.10.2  yamt  *
     22  1.4.10.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.4.10.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.4.10.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.4.10.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.4.10.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.4.10.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.4.10.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.4.10.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.4.10.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.4.10.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.4.10.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     33  1.4.10.2  yamt  */
     34  1.4.10.2  yamt 
     35  1.4.10.2  yamt /*
     36  1.4.10.2  yamt  * Copyright 1998-2006 VIA Technologies, Inc. All Rights Reserved.
     37  1.4.10.2  yamt  * Copyright 2001-2006 S3 Graphics, Inc. All Rights Reserved.
     38  1.4.10.2  yamt  *
     39  1.4.10.2  yamt  * Permission is hereby granted, free of charge, to any person obtaining a
     40  1.4.10.2  yamt  * copy of this software and associated documentation files (the "Software"),
     41  1.4.10.2  yamt  * to deal in the Software without restriction, including without limitation
     42  1.4.10.2  yamt  * the rights to use, copy, modify, merge, publish, distribute, sub license,
     43  1.4.10.2  yamt  * and/or sell copies of the Software, and to permit persons to whom the
     44  1.4.10.2  yamt  * Software is furnished to do so, subject to the following conditions:
     45  1.4.10.2  yamt  *
     46  1.4.10.2  yamt  * The above copyright notice and this permission notice (including the
     47  1.4.10.2  yamt  * next paragraph) shall be included in all copies or substantial portions
     48  1.4.10.2  yamt  * of the Software.
     49  1.4.10.2  yamt  *
     50  1.4.10.2  yamt  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     51  1.4.10.2  yamt  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     52  1.4.10.2  yamt  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     53  1.4.10.2  yamt  * THE AUTHOR(S) OR COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     54  1.4.10.2  yamt  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     55  1.4.10.2  yamt  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     56  1.4.10.2  yamt  * DEALINGS IN THE SOFTWARE.
     57  1.4.10.2  yamt  */
     58  1.4.10.2  yamt 
     59  1.4.10.2  yamt #include <sys/cdefs.h>
     60  1.4.10.6  yamt __KERNEL_RCSID(0, "$NetBSD: unichromefb.c,v 1.4.10.6 2008/03/17 09:15:12 yamt Exp $");
     61  1.4.10.2  yamt 
     62  1.4.10.2  yamt #include <sys/param.h>
     63  1.4.10.2  yamt #include <sys/systm.h>
     64  1.4.10.2  yamt #include <sys/device.h>
     65  1.4.10.2  yamt #include <sys/malloc.h>
     66  1.4.10.2  yamt 
     67  1.4.10.5  yamt #include <sys/bus.h>
     68  1.4.10.2  yamt 
     69  1.4.10.2  yamt #include <dev/pci/pcivar.h>
     70  1.4.10.2  yamt #include <dev/pci/pcireg.h>
     71  1.4.10.2  yamt #include <dev/pci/pcidevs.h>
     72  1.4.10.6  yamt #include <dev/pci/pciio.h>
     73  1.4.10.2  yamt 
     74  1.4.10.2  yamt #include <dev/wscons/wsdisplayvar.h>
     75  1.4.10.2  yamt #include <dev/wscons/wsconsio.h>
     76  1.4.10.2  yamt #include <dev/wsfont/wsfont.h>
     77  1.4.10.2  yamt #include <dev/rasops/rasops.h>
     78  1.4.10.2  yamt #include <dev/wscons/wsdisplay_vconsvar.h>
     79  1.4.10.2  yamt 
     80  1.4.10.2  yamt #include <dev/pci/unichromereg.h>
     81  1.4.10.2  yamt #include <dev/pci/unichromemode.h>
     82  1.4.10.2  yamt #include <dev/pci/unichromehw.h>
     83  1.4.10.2  yamt #include <dev/pci/unichromeconfig.h>
     84  1.4.10.2  yamt #include <dev/pci/unichromeaccel.h>
     85  1.4.10.2  yamt 
     86  1.4.10.2  yamt #include "vga.h"
     87  1.4.10.2  yamt 
     88  1.4.10.2  yamt #if NVGA > 0
     89  1.4.10.2  yamt #include <dev/ic/mc6845reg.h>
     90  1.4.10.2  yamt #include <dev/ic/pcdisplayvar.h>
     91  1.4.10.2  yamt #include <dev/ic/vgareg.h>
     92  1.4.10.2  yamt #include <dev/ic/vgavar.h>
     93  1.4.10.2  yamt #endif
     94  1.4.10.2  yamt 
     95  1.4.10.2  yamt /* XXX */
     96  1.4.10.2  yamt #define UNICHROMEFB_DEPTH	16
     97  1.4.10.2  yamt #define UNICHROMEFB_MODE	VIA_RES_1280X1024
     98  1.4.10.2  yamt #define UNICHROMEFB_WIDTH	1280
     99  1.4.10.2  yamt #define UNICHROMEFB_HEIGHT	1024
    100  1.4.10.2  yamt 
    101  1.4.10.2  yamt struct unichromefb_softc {
    102  1.4.10.2  yamt 	struct device		sc_dev;
    103  1.4.10.2  yamt 	struct vcons_data	sc_vd;
    104  1.4.10.2  yamt 	void *			sc_fbbase;
    105  1.4.10.2  yamt 	unsigned int		sc_fbaddr;
    106  1.4.10.2  yamt 	unsigned int		sc_fbsize;
    107  1.4.10.4  yamt 	bus_addr_t		sc_mmiobase;
    108  1.4.10.4  yamt 	bus_size_t		sc_mmiosize;
    109  1.4.10.2  yamt 
    110  1.4.10.2  yamt 	bus_space_tag_t		sc_iot;
    111  1.4.10.2  yamt 	bus_space_handle_t	sc_ioh;
    112  1.4.10.2  yamt 
    113  1.4.10.2  yamt 	bus_space_tag_t		sc_memt;
    114  1.4.10.2  yamt 	bus_space_handle_t	sc_memh;
    115  1.4.10.6  yamt 	bus_space_tag_t		sc_apmemt;
    116  1.4.10.4  yamt 	bus_space_handle_t	sc_apmemh;
    117  1.4.10.4  yamt 
    118  1.4.10.4  yamt 	struct pci_attach_args	sc_pa;
    119  1.4.10.2  yamt 
    120  1.4.10.2  yamt 	int			sc_width;
    121  1.4.10.2  yamt 	int			sc_height;
    122  1.4.10.2  yamt 	int			sc_depth;
    123  1.4.10.2  yamt 	int			sc_stride;
    124  1.4.10.2  yamt 
    125  1.4.10.2  yamt 	int			sc_wsmode;
    126  1.4.10.2  yamt 
    127  1.4.10.2  yamt 	int			sc_accel;
    128  1.4.10.2  yamt };
    129  1.4.10.2  yamt 
    130  1.4.10.2  yamt static int unichromefb_match(struct device *, struct cfdata *, void *);
    131  1.4.10.2  yamt static void unichromefb_attach(struct device *, struct device *, void *);
    132  1.4.10.2  yamt 
    133  1.4.10.4  yamt static int unichromefb_drm_print(void *, const char *);
    134  1.4.10.4  yamt static int unichromefb_drm_unmap(struct unichromefb_softc *);
    135  1.4.10.4  yamt static int unichromefb_drm_map(struct unichromefb_softc *);
    136  1.4.10.4  yamt 
    137  1.4.10.2  yamt struct wsscreen_descr unichromefb_stdscreen = {
    138  1.4.10.2  yamt 	"fb",
    139  1.4.10.2  yamt 	0, 0,
    140  1.4.10.2  yamt 	NULL,
    141  1.4.10.2  yamt 	8, 16,
    142  1.4.10.3  yamt 	WSSCREEN_WSCOLORS, NULL,
    143  1.4.10.2  yamt };
    144  1.4.10.2  yamt 
    145  1.4.10.4  yamt static int	unichromefb_ioctl(void *, void *, u_long, void *, int,
    146  1.4.10.2  yamt 				  struct lwp *);
    147  1.4.10.2  yamt static paddr_t	unichromefb_mmap(void *, void *, off_t, int);
    148  1.4.10.2  yamt 
    149  1.4.10.2  yamt static void	unichromefb_init_screen(void *, struct vcons_screen *,
    150  1.4.10.2  yamt 					int, long *);
    151  1.4.10.2  yamt 
    152  1.4.10.2  yamt /* hardware access */
    153  1.4.10.2  yamt static uint8_t	uni_rd(struct unichromefb_softc *, int, uint8_t);
    154  1.4.10.2  yamt static void	uni_wr(struct unichromefb_softc *, int, uint8_t, uint8_t);
    155  1.4.10.2  yamt static void	uni_wr_mask(struct unichromefb_softc *, int, uint8_t,
    156  1.4.10.2  yamt 			    uint8_t, uint8_t);
    157  1.4.10.2  yamt static void	uni_wr_x(struct unichromefb_softc *, struct io_reg *, int);
    158  1.4.10.2  yamt static void	uni_wr_dac(struct unichromefb_softc *, uint8_t, uint8_t,
    159  1.4.10.2  yamt 			   uint8_t, uint8_t);
    160  1.4.10.2  yamt 
    161  1.4.10.2  yamt /* helpers */
    162  1.4.10.2  yamt static struct VideoModeTable *	uni_getmode(int);
    163  1.4.10.2  yamt static void	uni_setmode(struct unichromefb_softc *, int, int);
    164  1.4.10.2  yamt static void	uni_crt_lock(struct unichromefb_softc *);
    165  1.4.10.2  yamt static void	uni_crt_unlock(struct unichromefb_softc *);
    166  1.4.10.2  yamt static void	uni_crt_enable(struct unichromefb_softc *);
    167  1.4.10.2  yamt static void	uni_crt_disable(struct unichromefb_softc *);
    168  1.4.10.2  yamt static void	uni_screen_enable(struct unichromefb_softc *);
    169  1.4.10.2  yamt static void	uni_screen_disable(struct unichromefb_softc *);
    170  1.4.10.2  yamt static void	uni_set_start(struct unichromefb_softc *);
    171  1.4.10.2  yamt static void	uni_set_crtc(struct unichromefb_softc *,
    172  1.4.10.2  yamt 			     struct crt_mode_table *, int, int, int);
    173  1.4.10.2  yamt static void	uni_load_crtc(struct unichromefb_softc *, struct display_timing,
    174  1.4.10.2  yamt 			      int);
    175  1.4.10.2  yamt static void	uni_load_reg(struct unichromefb_softc *, int, int,
    176  1.4.10.2  yamt 			     struct io_register *, int);
    177  1.4.10.2  yamt static void	uni_fix_crtc(struct unichromefb_softc *);
    178  1.4.10.2  yamt static void	uni_load_offset(struct unichromefb_softc *, int, int, int);
    179  1.4.10.2  yamt static void	uni_load_fetchcnt(struct unichromefb_softc *, int, int, int);
    180  1.4.10.2  yamt static void	uni_load_fifo(struct unichromefb_softc *, int, int, int);
    181  1.4.10.2  yamt static void	uni_set_depth(struct unichromefb_softc *, int, int);
    182  1.4.10.2  yamt static uint32_t	uni_get_clkval(struct unichromefb_softc *, int);
    183  1.4.10.2  yamt static void	uni_set_vclk(struct unichromefb_softc *, uint32_t, int);
    184  1.4.10.2  yamt static void	uni_init_dac(struct unichromefb_softc *, int);
    185  1.4.10.2  yamt static void	uni_init_accel(struct unichromefb_softc *);
    186  1.4.10.2  yamt static void	uni_set_accel_depth(struct unichromefb_softc *);
    187  1.4.10.2  yamt 
    188  1.4.10.2  yamt /* graphics ops */
    189  1.4.10.2  yamt static void	uni_wait_idle(struct unichromefb_softc *);
    190  1.4.10.2  yamt static void	uni_fillrect(struct unichromefb_softc *,
    191  1.4.10.2  yamt 			     int, int, int, int, int);
    192  1.4.10.2  yamt static void	uni_rectinvert(struct unichromefb_softc *,
    193  1.4.10.2  yamt 			       int, int, int, int);
    194  1.4.10.2  yamt static void	uni_bitblit(struct unichromefb_softc *, int, int, int, int,
    195  1.4.10.2  yamt 			    int, int);
    196  1.4.10.2  yamt static void	uni_setup_mono(struct unichromefb_softc *, int, int, int,
    197  1.4.10.2  yamt 			       int, uint32_t, uint32_t);
    198  1.4.10.2  yamt #if notyet
    199  1.4.10.2  yamt static void	uni_cursor_show(struct unichromefb_softc *);
    200  1.4.10.2  yamt static void	uni_cursor_hide(struct unichromefb_softc *);
    201  1.4.10.2  yamt #endif
    202  1.4.10.2  yamt 
    203  1.4.10.2  yamt /* rasops glue */
    204  1.4.10.2  yamt static void	uni_copycols(void *, int, int, int, int);
    205  1.4.10.2  yamt static void	uni_copyrows(void *, int, int, int);
    206  1.4.10.2  yamt static void	uni_erasecols(void *, int, int, int, long);
    207  1.4.10.2  yamt static void	uni_eraserows(void *, int, int, long);
    208  1.4.10.2  yamt static void	uni_cursor(void *, int, int, int);
    209  1.4.10.2  yamt static void	uni_putchar(void *, int, int, u_int, long);
    210  1.4.10.2  yamt 
    211  1.4.10.2  yamt struct wsdisplay_accessops unichromefb_accessops = {
    212  1.4.10.2  yamt 	unichromefb_ioctl,
    213  1.4.10.2  yamt 	unichromefb_mmap,
    214  1.4.10.2  yamt 	NULL,
    215  1.4.10.2  yamt 	NULL,
    216  1.4.10.2  yamt 	NULL,
    217  1.4.10.2  yamt 	NULL,
    218  1.4.10.3  yamt 	NULL,
    219  1.4.10.3  yamt 	NULL,
    220  1.4.10.2  yamt };
    221  1.4.10.2  yamt 
    222  1.4.10.2  yamt static struct vcons_screen unichromefb_console_screen;
    223  1.4.10.2  yamt 
    224  1.4.10.2  yamt const struct wsscreen_descr *_unichromefb_scrlist[] = {
    225  1.4.10.2  yamt 	&unichromefb_stdscreen,
    226  1.4.10.2  yamt };
    227  1.4.10.2  yamt 
    228  1.4.10.2  yamt struct wsscreen_list unichromefb_screenlist = {
    229  1.4.10.2  yamt 	sizeof(_unichromefb_scrlist) / sizeof(struct wsscreen_descr *),
    230  1.4.10.2  yamt 	_unichromefb_scrlist
    231  1.4.10.2  yamt };
    232  1.4.10.2  yamt 
    233  1.4.10.2  yamt CFATTACH_DECL(unichromefb, sizeof(struct unichromefb_softc),
    234  1.4.10.2  yamt     unichromefb_match, unichromefb_attach, NULL, NULL);
    235  1.4.10.2  yamt 
    236  1.4.10.2  yamt static int
    237  1.4.10.2  yamt unichromefb_match(struct device *parent, struct cfdata *match, void *opaque)
    238  1.4.10.2  yamt {
    239  1.4.10.2  yamt 	struct pci_attach_args *pa;
    240  1.4.10.2  yamt 
    241  1.4.10.2  yamt 	pa = (struct pci_attach_args *)opaque;
    242  1.4.10.2  yamt 
    243  1.4.10.2  yamt 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    244  1.4.10.2  yamt 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    245  1.4.10.2  yamt 		return 0;
    246  1.4.10.2  yamt 
    247  1.4.10.2  yamt 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
    248  1.4.10.2  yamt 		return 0;
    249  1.4.10.2  yamt 
    250  1.4.10.2  yamt 	switch (PCI_PRODUCT(pa->pa_id)) {
    251  1.4.10.2  yamt 	case PCI_PRODUCT_VIATECH_VT3314_IG:
    252  1.4.10.2  yamt 		return 10;	/* beat vga(4) */
    253  1.4.10.2  yamt 	}
    254  1.4.10.2  yamt 
    255  1.4.10.2  yamt 	return 0;
    256  1.4.10.2  yamt }
    257  1.4.10.2  yamt 
    258  1.4.10.2  yamt static void
    259  1.4.10.2  yamt unichromefb_attach(struct device *parent, struct device *self, void *opaque)
    260  1.4.10.2  yamt {
    261  1.4.10.2  yamt 	struct unichromefb_softc *sc;
    262  1.4.10.2  yamt 	struct pci_attach_args *pa;
    263  1.4.10.2  yamt 	struct rasops_info *ri;
    264  1.4.10.2  yamt 	struct wsemuldisplaydev_attach_args aa;
    265  1.4.10.2  yamt 	uint8_t val;
    266  1.4.10.2  yamt 	long defattr;
    267  1.4.10.2  yamt 
    268  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)self;
    269  1.4.10.2  yamt 	pa = (struct pci_attach_args *)opaque;
    270  1.4.10.2  yamt 
    271  1.4.10.2  yamt 	sc->sc_width = UNICHROMEFB_WIDTH;
    272  1.4.10.2  yamt 	sc->sc_height = UNICHROMEFB_HEIGHT;
    273  1.4.10.2  yamt 	sc->sc_depth = UNICHROMEFB_DEPTH;
    274  1.4.10.2  yamt 	sc->sc_stride = sc->sc_width * (sc->sc_depth / 8);
    275  1.4.10.2  yamt 
    276  1.4.10.2  yamt 	sc->sc_wsmode = WSDISPLAYIO_MODE_EMUL;
    277  1.4.10.2  yamt 
    278  1.4.10.2  yamt 	sc->sc_iot = pa->pa_iot;
    279  1.4.10.4  yamt 	sc->sc_pa = *pa;
    280  1.4.10.2  yamt 
    281  1.4.10.2  yamt #if NVGA > 0
    282  1.4.10.2  yamt 	/* XXX vga_cnattach claims the I/O registers that we need;
    283  1.4.10.2  yamt 	 *     we need to nuke it here so we can take over.
    284  1.4.10.2  yamt 	 */
    285  1.4.10.2  yamt 	vga_cndetach();
    286  1.4.10.2  yamt #endif
    287  1.4.10.2  yamt 
    288  1.4.10.2  yamt 	if (bus_space_map(sc->sc_iot, VIA_REGBASE, 0x20, 0, &sc->sc_ioh)) {
    289  1.4.10.2  yamt 		aprint_error(": failed to map I/O registers\n");
    290  1.4.10.2  yamt 		return;
    291  1.4.10.2  yamt 	}
    292  1.4.10.2  yamt 
    293  1.4.10.4  yamt 	sc->sc_apmemt = pa->pa_memt;
    294  1.4.10.2  yamt 	val = uni_rd(sc, VIASR, SR30);
    295  1.4.10.2  yamt 	sc->sc_fbaddr = val << 24;
    296  1.4.10.2  yamt 	val = uni_rd(sc, VIASR, SR39);
    297  1.4.10.2  yamt 	sc->sc_fbsize = val * (4*1024*1024);
    298  1.4.10.2  yamt 	if (sc->sc_fbsize < 16*1024*1024 || sc->sc_fbsize > 64*1024*1024)
    299  1.4.10.2  yamt 		sc->sc_fbsize = 16*1024*1024;
    300  1.4.10.4  yamt 	if (bus_space_map(sc->sc_apmemt, sc->sc_fbaddr, sc->sc_fbsize,
    301  1.4.10.4  yamt 	    BUS_SPACE_MAP_LINEAR, &sc->sc_apmemh)) {
    302  1.4.10.2  yamt 		aprint_error(": failed to map aperture at 0x%08x/0x%x\n",
    303  1.4.10.2  yamt 		    sc->sc_fbaddr, sc->sc_fbsize);
    304  1.4.10.2  yamt 		return;
    305  1.4.10.2  yamt 	}
    306  1.4.10.4  yamt 	sc->sc_fbbase = (void *)bus_space_vaddr(sc->sc_apmemt, sc->sc_apmemh);
    307  1.4.10.2  yamt 
    308  1.4.10.2  yamt 	if (pci_mapreg_map(pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
    309  1.4.10.4  yamt 	    &sc->sc_memt, &sc->sc_memh, &sc->sc_mmiobase,
    310  1.4.10.4  yamt 	    &sc->sc_mmiosize)) {
    311  1.4.10.2  yamt 		sc->sc_accel = 0;
    312  1.4.10.2  yamt 		aprint_error(": failed to map MMIO registers\n");
    313  1.4.10.2  yamt 	} else {
    314  1.4.10.2  yamt 		sc->sc_accel = 1;
    315  1.4.10.2  yamt 	}
    316  1.4.10.2  yamt 
    317  1.4.10.2  yamt 	aprint_naive("\n");
    318  1.4.10.2  yamt 	aprint_normal(": VIA UniChrome frame buffer\n");
    319  1.4.10.2  yamt 
    320  1.4.10.2  yamt 	if (sc->sc_accel)
    321  1.4.10.2  yamt 		aprint_normal("%s: MMIO @0x%08x/0x%x\n",
    322  1.4.10.4  yamt 		    sc->sc_dev.dv_xname,
    323  1.4.10.4  yamt 		    (uint32_t)sc->sc_mmiobase,
    324  1.4.10.4  yamt 		    (uint32_t)sc->sc_mmiosize);
    325  1.4.10.2  yamt 
    326  1.4.10.2  yamt 	ri = &unichromefb_console_screen.scr_ri;
    327  1.4.10.2  yamt 	memset(ri, 0, sizeof(struct rasops_info));
    328  1.4.10.2  yamt 
    329  1.4.10.2  yamt 	vcons_init(&sc->sc_vd, sc, &unichromefb_stdscreen,
    330  1.4.10.2  yamt 	    &unichromefb_accessops);
    331  1.4.10.2  yamt 	sc->sc_vd.init_screen = unichromefb_init_screen;
    332  1.4.10.2  yamt 
    333  1.4.10.2  yamt 	uni_setmode(sc, UNICHROMEFB_MODE, sc->sc_depth);
    334  1.4.10.2  yamt 
    335  1.4.10.2  yamt 	uni_init_dac(sc, IGA1);
    336  1.4.10.2  yamt 	if (sc->sc_accel) {
    337  1.4.10.2  yamt 		uni_init_accel(sc);
    338  1.4.10.2  yamt 		uni_fillrect(sc, 0, 0, sc->sc_width, sc->sc_height, 0);
    339  1.4.10.2  yamt 	}
    340  1.4.10.2  yamt 
    341  1.4.10.2  yamt 	aprint_normal("%s: FB @0x%08x (%dx%dx%d)\n", sc->sc_dev.dv_xname,
    342  1.4.10.2  yamt 	       sc->sc_fbaddr, sc->sc_width, sc->sc_height, sc->sc_depth);
    343  1.4.10.2  yamt 
    344  1.4.10.2  yamt 	unichromefb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    345  1.4.10.2  yamt 	vcons_init_screen(&sc->sc_vd, &unichromefb_console_screen, 1, &defattr);
    346  1.4.10.2  yamt 
    347  1.4.10.2  yamt 	unichromefb_stdscreen.ncols = ri->ri_cols;
    348  1.4.10.2  yamt 	unichromefb_stdscreen.nrows = ri->ri_rows;
    349  1.4.10.2  yamt 	unichromefb_stdscreen.textops = &ri->ri_ops;
    350  1.4.10.2  yamt 	unichromefb_stdscreen.capabilities = ri->ri_caps;
    351  1.4.10.2  yamt 	unichromefb_stdscreen.modecookie = NULL;
    352  1.4.10.2  yamt 
    353  1.4.10.2  yamt 	wsdisplay_cnattach(&unichromefb_stdscreen, ri, 0, 0, defattr);
    354  1.4.10.2  yamt 
    355  1.4.10.2  yamt 	aa.console = 1; /* XXX */
    356  1.4.10.2  yamt 	aa.scrdata = &unichromefb_screenlist;
    357  1.4.10.2  yamt 	aa.accessops = &unichromefb_accessops;
    358  1.4.10.2  yamt 	aa.accesscookie = &sc->sc_vd;
    359  1.4.10.2  yamt 
    360  1.4.10.2  yamt 	config_found(self, &aa, wsemuldisplaydevprint);
    361  1.4.10.2  yamt 
    362  1.4.10.4  yamt 	config_found_ia(self, "drm", opaque, unichromefb_drm_print);
    363  1.4.10.4  yamt 
    364  1.4.10.2  yamt 	return;
    365  1.4.10.2  yamt }
    366  1.4.10.2  yamt 
    367  1.4.10.2  yamt static int
    368  1.4.10.4  yamt unichromefb_drm_print(void *opaque, const char *pnp)
    369  1.4.10.4  yamt {
    370  1.4.10.4  yamt 	if (pnp)
    371  1.4.10.4  yamt 		aprint_normal("direct rendering for %s", pnp);
    372  1.4.10.4  yamt 
    373  1.4.10.4  yamt 	return UNSUPP;
    374  1.4.10.4  yamt }
    375  1.4.10.4  yamt 
    376  1.4.10.4  yamt static int
    377  1.4.10.4  yamt unichromefb_drm_unmap(struct unichromefb_softc *sc)
    378  1.4.10.4  yamt {
    379  1.4.10.4  yamt 	printf("%s: releasing bus resources\n", sc->sc_dev.dv_xname);
    380  1.4.10.4  yamt 
    381  1.4.10.4  yamt 	bus_space_unmap(sc->sc_apmemt, sc->sc_apmemh, sc->sc_fbsize);
    382  1.4.10.4  yamt 	bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mmiosize);
    383  1.4.10.4  yamt 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, 0x20);
    384  1.4.10.4  yamt 
    385  1.4.10.4  yamt 	return 0;
    386  1.4.10.4  yamt }
    387  1.4.10.4  yamt 
    388  1.4.10.4  yamt static int
    389  1.4.10.4  yamt unichromefb_drm_map(struct unichromefb_softc *sc)
    390  1.4.10.4  yamt {
    391  1.4.10.4  yamt 	int rv;
    392  1.4.10.4  yamt 
    393  1.4.10.4  yamt 	rv = bus_space_map(sc->sc_iot, VIA_REGBASE, 0x20, 0,
    394  1.4.10.4  yamt 	    &sc->sc_ioh);
    395  1.4.10.4  yamt 	if (rv) {
    396  1.4.10.4  yamt 		printf("%s: failed to map I/O registers\n",
    397  1.4.10.4  yamt 		    sc->sc_dev.dv_xname);
    398  1.4.10.4  yamt 		return rv;
    399  1.4.10.4  yamt 	}
    400  1.4.10.4  yamt 	rv = bus_space_map(sc->sc_apmemt, sc->sc_fbaddr, sc->sc_fbsize,
    401  1.4.10.4  yamt 	    BUS_SPACE_MAP_LINEAR, &sc->sc_apmemh);
    402  1.4.10.4  yamt 	if (rv) {
    403  1.4.10.4  yamt 		printf("%s: failed to map aperture at 0x%08x/0x%x\n",
    404  1.4.10.4  yamt 		    sc->sc_dev.dv_xname, sc->sc_fbaddr, sc->sc_fbsize);
    405  1.4.10.4  yamt 		return rv;
    406  1.4.10.4  yamt 	}
    407  1.4.10.4  yamt 	sc->sc_fbbase = (void *)bus_space_vaddr(sc->sc_apmemt, sc->sc_apmemh);
    408  1.4.10.4  yamt 	rv = pci_mapreg_map(&sc->sc_pa, 0x14, PCI_MAPREG_TYPE_MEM, 0,
    409  1.4.10.4  yamt 	    &sc->sc_memt, &sc->sc_memh, &sc->sc_mmiobase,
    410  1.4.10.4  yamt 	    &sc->sc_mmiosize);
    411  1.4.10.4  yamt 	if (rv) {
    412  1.4.10.4  yamt 		printf("%s: failed to map MMIO registers\n",
    413  1.4.10.4  yamt 		    sc->sc_dev.dv_xname);
    414  1.4.10.4  yamt 		sc->sc_accel = 0;
    415  1.4.10.4  yamt 	}
    416  1.4.10.4  yamt 
    417  1.4.10.4  yamt 	uni_setmode(sc, UNICHROMEFB_MODE, sc->sc_depth);
    418  1.4.10.4  yamt 	uni_init_dac(sc, IGA1);
    419  1.4.10.4  yamt 	if (sc->sc_accel) {
    420  1.4.10.4  yamt 		uni_init_accel(sc);
    421  1.4.10.4  yamt 	}
    422  1.4.10.4  yamt 
    423  1.4.10.4  yamt 	printf("%s: re-acquired bus resources\n", sc->sc_dev.dv_xname);
    424  1.4.10.4  yamt 
    425  1.4.10.4  yamt 	return 0;
    426  1.4.10.4  yamt }
    427  1.4.10.4  yamt 
    428  1.4.10.4  yamt static int
    429  1.4.10.4  yamt unichromefb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    430  1.4.10.2  yamt 		  struct lwp *l)
    431  1.4.10.2  yamt {
    432  1.4.10.2  yamt 	struct vcons_data *vd;
    433  1.4.10.2  yamt 	struct unichromefb_softc *sc;
    434  1.4.10.2  yamt 	struct wsdisplay_fbinfo *fb;
    435  1.4.10.2  yamt 
    436  1.4.10.2  yamt 	vd = (struct vcons_data *)v;
    437  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)vd->cookie;
    438  1.4.10.2  yamt 
    439  1.4.10.2  yamt 	switch (cmd) {
    440  1.4.10.2  yamt 	case WSDISPLAYIO_GTYPE:
    441  1.4.10.2  yamt 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    442  1.4.10.2  yamt 		return 0;
    443  1.4.10.2  yamt 	case WSDISPLAYIO_GINFO:
    444  1.4.10.2  yamt 		if (vd->active != NULL) {
    445  1.4.10.2  yamt 			fb = (struct wsdisplay_fbinfo *)data;
    446  1.4.10.2  yamt 			fb->width = sc->sc_width;
    447  1.4.10.2  yamt 			fb->height = sc->sc_height;
    448  1.4.10.2  yamt 			fb->depth = sc->sc_depth;
    449  1.4.10.2  yamt 			fb->cmsize = 256;
    450  1.4.10.2  yamt 			return 0;
    451  1.4.10.2  yamt 		} else
    452  1.4.10.2  yamt 			return ENODEV;
    453  1.4.10.2  yamt 	case WSDISPLAYIO_GVIDEO:
    454  1.4.10.2  yamt 			return ENODEV;
    455  1.4.10.2  yamt 	case WSDISPLAYIO_SVIDEO:
    456  1.4.10.2  yamt 			return ENODEV;
    457  1.4.10.2  yamt 	case WSDISPLAYIO_GETCMAP:
    458  1.4.10.2  yamt 			return EINVAL;
    459  1.4.10.2  yamt 	case WSDISPLAYIO_PUTCMAP:
    460  1.4.10.2  yamt 			return EINVAL;
    461  1.4.10.2  yamt 	case WSDISPLAYIO_LINEBYTES:
    462  1.4.10.2  yamt 		*(u_int *)data = sc->sc_stride;
    463  1.4.10.2  yamt 		return 0;
    464  1.4.10.2  yamt 	case WSDISPLAYIO_SMODE:
    465  1.4.10.2  yamt 		{
    466  1.4.10.2  yamt 			int new_mode = *(int *)data;
    467  1.4.10.2  yamt 			if (new_mode != sc->sc_wsmode) {
    468  1.4.10.2  yamt 				sc->sc_wsmode = new_mode;
    469  1.4.10.4  yamt 				switch (new_mode) {
    470  1.4.10.4  yamt 				case WSDISPLAYIO_MODE_EMUL:
    471  1.4.10.4  yamt 					unichromefb_drm_map(sc);
    472  1.4.10.2  yamt 					vcons_redraw_screen(vd->active);
    473  1.4.10.4  yamt 					break;
    474  1.4.10.4  yamt 				default:
    475  1.4.10.4  yamt 					unichromefb_drm_unmap(sc);
    476  1.4.10.4  yamt 					break;
    477  1.4.10.4  yamt 				}
    478  1.4.10.2  yamt 			}
    479  1.4.10.2  yamt 		}
    480  1.4.10.2  yamt 		return 0;
    481  1.4.10.2  yamt 	case WSDISPLAYIO_SSPLASH:
    482  1.4.10.2  yamt 		return ENODEV;
    483  1.4.10.2  yamt 	case WSDISPLAYIO_SPROGRESS:
    484  1.4.10.2  yamt 		return ENODEV;
    485  1.4.10.6  yamt 
    486  1.4.10.6  yamt 	/* PCI config read/write passthrough. */
    487  1.4.10.6  yamt 	case PCI_IOC_CFGREAD:
    488  1.4.10.6  yamt 	case PCI_IOC_CFGWRITE:
    489  1.4.10.6  yamt 		return (pci_devioctl(sc->sc_pa.pa_pc, sc->sc_pa.pa_tag,
    490  1.4.10.6  yamt 		    cmd, data, flag, l));
    491  1.4.10.2  yamt 	}
    492  1.4.10.2  yamt 
    493  1.4.10.2  yamt 	return EPASSTHROUGH;
    494  1.4.10.2  yamt }
    495  1.4.10.2  yamt 
    496  1.4.10.2  yamt static paddr_t
    497  1.4.10.2  yamt unichromefb_mmap(void *v, void *vs, off_t offset, int prot)
    498  1.4.10.2  yamt {
    499  1.4.10.2  yamt 	return -1;
    500  1.4.10.2  yamt }
    501  1.4.10.2  yamt 
    502  1.4.10.2  yamt static void
    503  1.4.10.2  yamt unichromefb_init_screen(void *c, struct vcons_screen *scr, int existing,
    504  1.4.10.2  yamt 			long *defattr)
    505  1.4.10.2  yamt {
    506  1.4.10.2  yamt 	struct unichromefb_softc *sc;
    507  1.4.10.2  yamt 	struct rasops_info *ri;
    508  1.4.10.2  yamt 
    509  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)c;
    510  1.4.10.2  yamt 	ri = &scr->scr_ri;
    511  1.4.10.2  yamt 	ri->ri_flg = RI_CENTER;
    512  1.4.10.2  yamt 	ri->ri_depth = sc->sc_depth;
    513  1.4.10.2  yamt 	ri->ri_width = sc->sc_width;
    514  1.4.10.2  yamt 	ri->ri_height = sc->sc_height;
    515  1.4.10.2  yamt 	ri->ri_stride = sc->sc_stride;
    516  1.4.10.2  yamt 	ri->ri_bits = sc->sc_fbbase;
    517  1.4.10.2  yamt 	if (existing)
    518  1.4.10.2  yamt 		ri->ri_flg |= RI_CLEAR;
    519  1.4.10.2  yamt 
    520  1.4.10.2  yamt 	switch (ri->ri_depth) {
    521  1.4.10.2  yamt 	case 32:
    522  1.4.10.2  yamt 		ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = 8;
    523  1.4.10.2  yamt 		ri->ri_rpos = 16;
    524  1.4.10.2  yamt 		ri->ri_gpos = 8;
    525  1.4.10.2  yamt 		ri->ri_bpos = 0;
    526  1.4.10.2  yamt 		break;
    527  1.4.10.2  yamt 	case 16:
    528  1.4.10.2  yamt 		ri->ri_rnum = 5;
    529  1.4.10.2  yamt 		ri->ri_gnum = 6;
    530  1.4.10.2  yamt 		ri->ri_bnum = 5;
    531  1.4.10.2  yamt 		ri->ri_rpos = 11;
    532  1.4.10.2  yamt 		ri->ri_gpos = 5;
    533  1.4.10.2  yamt 		ri->ri_bpos = 0;
    534  1.4.10.2  yamt 		break;
    535  1.4.10.2  yamt 	}
    536  1.4.10.2  yamt 
    537  1.4.10.2  yamt 	rasops_init(ri, sc->sc_height / 16, sc->sc_width / 8);
    538  1.4.10.2  yamt 	ri->ri_caps = WSSCREEN_WSCOLORS;
    539  1.4.10.2  yamt 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    540  1.4.10.2  yamt 	    sc->sc_width / ri->ri_font->fontwidth);
    541  1.4.10.2  yamt 
    542  1.4.10.2  yamt 	ri->ri_hw = scr;
    543  1.4.10.2  yamt 	if (sc->sc_accel) {
    544  1.4.10.2  yamt 		ri->ri_ops.copyrows = uni_copyrows;
    545  1.4.10.2  yamt 		ri->ri_ops.copycols = uni_copycols;
    546  1.4.10.2  yamt 		ri->ri_ops.eraserows = uni_eraserows;
    547  1.4.10.2  yamt 		ri->ri_ops.erasecols = uni_erasecols;
    548  1.4.10.2  yamt 		ri->ri_ops.cursor = uni_cursor;
    549  1.4.10.2  yamt 		ri->ri_ops.putchar = uni_putchar;
    550  1.4.10.2  yamt 	}
    551  1.4.10.2  yamt 
    552  1.4.10.2  yamt 	return;
    553  1.4.10.2  yamt }
    554  1.4.10.2  yamt 
    555  1.4.10.2  yamt /*
    556  1.4.10.2  yamt  * hardware access
    557  1.4.10.2  yamt  */
    558  1.4.10.2  yamt static uint8_t
    559  1.4.10.2  yamt uni_rd(struct unichromefb_softc *sc, int off, uint8_t idx)
    560  1.4.10.2  yamt {
    561  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, idx);
    562  1.4.10.2  yamt 	return bus_space_read_1(sc->sc_iot, sc->sc_ioh, off + 1);
    563  1.4.10.2  yamt }
    564  1.4.10.2  yamt 
    565  1.4.10.2  yamt static void
    566  1.4.10.2  yamt uni_wr(struct unichromefb_softc *sc, int off, uint8_t idx, uint8_t val)
    567  1.4.10.2  yamt {
    568  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, idx);
    569  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off + 1, val);
    570  1.4.10.2  yamt }
    571  1.4.10.2  yamt 
    572  1.4.10.2  yamt static void
    573  1.4.10.2  yamt uni_wr_mask(struct unichromefb_softc *sc, int off, uint8_t idx,
    574  1.4.10.2  yamt     uint8_t val, uint8_t mask)
    575  1.4.10.2  yamt {
    576  1.4.10.2  yamt 	uint8_t tmp;
    577  1.4.10.2  yamt 
    578  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off, idx);
    579  1.4.10.2  yamt 	tmp = bus_space_read_1(sc->sc_iot, sc->sc_ioh, off + 1);
    580  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, off + 1,
    581  1.4.10.2  yamt 	    ((val & mask) | (tmp & ~mask)));
    582  1.4.10.2  yamt }
    583  1.4.10.2  yamt 
    584  1.4.10.2  yamt static void
    585  1.4.10.2  yamt uni_wr_dac(struct unichromefb_softc *sc, uint8_t idx,
    586  1.4.10.2  yamt     uint8_t r, uint8_t g, uint8_t b)
    587  1.4.10.2  yamt {
    588  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LUT_INDEX_WRITE, idx);
    589  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LUT_DATA, r);
    590  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LUT_DATA, g);
    591  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, LUT_DATA, b);
    592  1.4.10.2  yamt }
    593  1.4.10.2  yamt 
    594  1.4.10.2  yamt static void
    595  1.4.10.2  yamt uni_wr_x(struct unichromefb_softc *sc, struct io_reg *tbl, int num)
    596  1.4.10.2  yamt {
    597  1.4.10.2  yamt 	int i;
    598  1.4.10.2  yamt 	uint8_t tmp;
    599  1.4.10.2  yamt 
    600  1.4.10.2  yamt 	for (i = 0; i < num; i++) {
    601  1.4.10.2  yamt 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, tbl[i].port,
    602  1.4.10.2  yamt 		    tbl[i].index);
    603  1.4.10.6  yamt 		tmp = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    604  1.4.10.2  yamt 		    tbl[i].port + 1);
    605  1.4.10.2  yamt 		tmp = (tmp & (~tbl[i].mask)) | tbl[i].value;
    606  1.4.10.2  yamt 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, tbl[i].index + 1,
    607  1.4.10.2  yamt 		    tmp);
    608  1.4.10.2  yamt 	}
    609  1.4.10.2  yamt }
    610  1.4.10.2  yamt 
    611  1.4.10.2  yamt /*
    612  1.4.10.2  yamt  * helpers
    613  1.4.10.2  yamt  */
    614  1.4.10.2  yamt static struct VideoModeTable *
    615  1.4.10.2  yamt uni_getmode(int mode)
    616  1.4.10.2  yamt {
    617  1.4.10.2  yamt 	int i;
    618  1.4.10.2  yamt 
    619  1.4.10.2  yamt 	for (i = 0; i < NUM_TOTAL_MODETABLE; i++)
    620  1.4.10.2  yamt 		if (CLE266Modes[i].ModeIndex == mode)
    621  1.4.10.2  yamt 			return &CLE266Modes[i];
    622  1.4.10.2  yamt 
    623  1.4.10.2  yamt 	return NULL;
    624  1.4.10.2  yamt }
    625  1.4.10.2  yamt 
    626  1.4.10.2  yamt static void
    627  1.4.10.2  yamt uni_setmode(struct unichromefb_softc *sc, int idx, int bpp)
    628  1.4.10.2  yamt {
    629  1.4.10.2  yamt 	struct VideoModeTable *vtbl;
    630  1.4.10.2  yamt 	struct crt_mode_table *crt;
    631  1.4.10.2  yamt 	int i;
    632  1.4.10.2  yamt 
    633  1.4.10.2  yamt 	/* XXX */
    634  1.4.10.2  yamt 	vtbl = uni_getmode(idx);
    635  1.4.10.2  yamt 	if (vtbl == NULL)
    636  1.4.10.2  yamt 		panic("%s: unsupported mode: %d\n", sc->sc_dev.dv_xname, idx);
    637  1.4.10.2  yamt 
    638  1.4.10.2  yamt 	crt = vtbl->crtc;
    639  1.4.10.2  yamt 
    640  1.4.10.2  yamt 	uni_screen_disable(sc);
    641  1.4.10.2  yamt 
    642  1.4.10.2  yamt 	(void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIAStatus);
    643  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAAR, 0);
    644  1.4.10.2  yamt 
    645  1.4.10.2  yamt 	/* XXX assume CN900 for now */
    646  1.4.10.2  yamt 	uni_wr_x(sc, CN900_ModeXregs, NUM_TOTAL_CN900_ModeXregs);
    647  1.4.10.2  yamt 
    648  1.4.10.2  yamt 	uni_crt_disable(sc);
    649  1.4.10.2  yamt 
    650  1.4.10.2  yamt 	/* Fill VPIT params */
    651  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc, VPIT.Misc);
    652  1.4.10.2  yamt 
    653  1.4.10.2  yamt 	/* Write sequencer */
    654  1.4.10.2  yamt 	for (i = 1; i <= StdSR; i++) {
    655  1.4.10.2  yamt 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIASR, i);
    656  1.4.10.2  yamt 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIASR + 1,
    657  1.4.10.2  yamt 		    VPIT.SR[i - 1]);
    658  1.4.10.2  yamt 	}
    659  1.4.10.2  yamt 
    660  1.4.10.2  yamt 	uni_set_start(sc);
    661  1.4.10.2  yamt 
    662  1.4.10.2  yamt 	uni_set_crtc(sc, crt, idx, bpp / 8, IGA1);
    663  1.4.10.2  yamt 
    664  1.4.10.2  yamt 	for (i = 0; i < StdGR; i++) {
    665  1.4.10.2  yamt 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAGR, i);
    666  1.4.10.2  yamt 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAGR + 1,
    667  1.4.10.2  yamt 		    VPIT.GR[i]);
    668  1.4.10.2  yamt 	}
    669  1.4.10.2  yamt 
    670  1.4.10.2  yamt 	for (i = 0; i < StdAR; i++) {
    671  1.4.10.2  yamt 		(void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIAStatus);
    672  1.4.10.2  yamt 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAAR, i);
    673  1.4.10.2  yamt 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAAR,
    674  1.4.10.2  yamt 		    VPIT.AR[i]);
    675  1.4.10.2  yamt 	}
    676  1.4.10.2  yamt 
    677  1.4.10.2  yamt 	(void)bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIAStatus);
    678  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAAR, 0x20);
    679  1.4.10.2  yamt 
    680  1.4.10.2  yamt 	uni_set_crtc(sc, crt, idx, bpp / 8, IGA1);
    681  1.4.10.2  yamt 	/* set crt output path */
    682  1.4.10.2  yamt 	uni_wr_mask(sc, VIASR, SR16, 0x00, BIT6);
    683  1.4.10.2  yamt 
    684  1.4.10.2  yamt 	uni_crt_enable(sc);
    685  1.4.10.2  yamt 	uni_screen_enable(sc);
    686  1.4.10.2  yamt 
    687  1.4.10.2  yamt 	return;
    688  1.4.10.2  yamt }
    689  1.4.10.2  yamt 
    690  1.4.10.2  yamt static void
    691  1.4.10.2  yamt uni_crt_lock(struct unichromefb_softc *sc)
    692  1.4.10.2  yamt {
    693  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR11, BIT7, BIT7);
    694  1.4.10.2  yamt }
    695  1.4.10.2  yamt 
    696  1.4.10.2  yamt static void
    697  1.4.10.2  yamt uni_crt_unlock(struct unichromefb_softc *sc)
    698  1.4.10.2  yamt {
    699  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR11, 0, BIT7);
    700  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR47, 0, BIT0);
    701  1.4.10.2  yamt }
    702  1.4.10.2  yamt 
    703  1.4.10.2  yamt static void
    704  1.4.10.2  yamt uni_crt_enable(struct unichromefb_softc *sc)
    705  1.4.10.2  yamt {
    706  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR36, 0, BIT5+BIT4);
    707  1.4.10.2  yamt }
    708  1.4.10.2  yamt 
    709  1.4.10.2  yamt static void
    710  1.4.10.2  yamt uni_crt_disable(struct unichromefb_softc *sc)
    711  1.4.10.2  yamt {
    712  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR36, BIT5+BIT4, BIT5+BIT4);
    713  1.4.10.2  yamt }
    714  1.4.10.2  yamt 
    715  1.4.10.2  yamt static void
    716  1.4.10.2  yamt uni_screen_enable(struct unichromefb_softc *sc)
    717  1.4.10.2  yamt {
    718  1.4.10.2  yamt 	uni_wr_mask(sc, VIASR, SR01, 0, BIT5);
    719  1.4.10.2  yamt }
    720  1.4.10.2  yamt 
    721  1.4.10.2  yamt static void
    722  1.4.10.2  yamt uni_screen_disable(struct unichromefb_softc *sc)
    723  1.4.10.2  yamt {
    724  1.4.10.2  yamt 	uni_wr_mask(sc, VIASR, SR01, 0x20, BIT5);
    725  1.4.10.2  yamt }
    726  1.4.10.2  yamt 
    727  1.4.10.2  yamt static void
    728  1.4.10.2  yamt uni_set_start(struct unichromefb_softc *sc)
    729  1.4.10.2  yamt {
    730  1.4.10.2  yamt 	uni_crt_unlock(sc);
    731  1.4.10.2  yamt 
    732  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR0C, 0x00);
    733  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR0D, 0x00);
    734  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR34, 0x00);
    735  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR48, 0x00, BIT0 + BIT1);
    736  1.4.10.2  yamt 
    737  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR62, 0x00);
    738  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR63, 0x00);
    739  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR64, 0x00);
    740  1.4.10.2  yamt 	uni_wr(sc, VIACR, CRA3, 0x00);
    741  1.4.10.2  yamt 
    742  1.4.10.2  yamt 	uni_crt_lock(sc);
    743  1.4.10.2  yamt }
    744  1.4.10.2  yamt 
    745  1.4.10.2  yamt static void
    746  1.4.10.2  yamt uni_set_crtc(struct unichromefb_softc *sc, struct crt_mode_table *ctbl,
    747  1.4.10.2  yamt     int mode, int bpp_byte, int iga)
    748  1.4.10.2  yamt {
    749  1.4.10.2  yamt 	struct VideoModeTable *vtbl;
    750  1.4.10.2  yamt 	struct display_timing crtreg;
    751  1.4.10.2  yamt 	int i;
    752  1.4.10.2  yamt 	int index;
    753  1.4.10.2  yamt 	int haddr, vaddr;
    754  1.4.10.2  yamt 	uint8_t val;
    755  1.4.10.2  yamt 	uint32_t pll_d_n;
    756  1.4.10.2  yamt 
    757  1.4.10.2  yamt 	index = 0;
    758  1.4.10.2  yamt 
    759  1.4.10.2  yamt 	vtbl = uni_getmode(mode);
    760  1.4.10.2  yamt 	for (i = 0; i < vtbl->mode_array; i++) {
    761  1.4.10.2  yamt 		index = i;
    762  1.4.10.2  yamt 		if (ctbl[i].refresh_rate == 60)
    763  1.4.10.2  yamt 			break;
    764  1.4.10.2  yamt 	}
    765  1.4.10.2  yamt 
    766  1.4.10.2  yamt 	crtreg = ctbl[index].crtc;
    767  1.4.10.2  yamt 
    768  1.4.10.2  yamt 	haddr = crtreg.hor_addr;
    769  1.4.10.2  yamt 	vaddr = crtreg.ver_addr;
    770  1.4.10.2  yamt 
    771  1.4.10.2  yamt 	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIARMisc);
    772  1.4.10.2  yamt 	if (ctbl[index].h_sync_polarity == NEGATIVE) {
    773  1.4.10.2  yamt 		if (ctbl[index].v_sync_polarity == NEGATIVE)
    774  1.4.10.2  yamt 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc,
    775  1.4.10.2  yamt 			    (val & (~(BIT6+BIT7))) | (BIT6+BIT7));
    776  1.4.10.2  yamt 		else
    777  1.4.10.2  yamt 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc,
    778  1.4.10.2  yamt 			    (val & (~(BIT6+BIT7))) | (BIT6));
    779  1.4.10.2  yamt 	} else {
    780  1.4.10.2  yamt 		if (ctbl[index].v_sync_polarity == NEGATIVE)
    781  1.4.10.2  yamt 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc,
    782  1.4.10.2  yamt 			    (val & (~(BIT6+BIT7))) | (BIT7));
    783  1.4.10.2  yamt 		else
    784  1.4.10.2  yamt 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc,
    785  1.4.10.2  yamt 			    (val & (~(BIT6+BIT7))));
    786  1.4.10.2  yamt 	}
    787  1.4.10.2  yamt 
    788  1.4.10.2  yamt 	if (iga == IGA1) {
    789  1.4.10.2  yamt 		uni_crt_unlock(sc);
    790  1.4.10.2  yamt 		uni_wr(sc, VIACR, CR09, 0x00);
    791  1.4.10.2  yamt 		uni_wr_mask(sc, VIACR, CR11, 0x00, BIT4+BIT5+BIT6);
    792  1.4.10.2  yamt 		uni_wr_mask(sc, VIACR, CR17, 0x00, BIT7);
    793  1.4.10.2  yamt 	}
    794  1.4.10.2  yamt 
    795  1.4.10.2  yamt 	uni_load_crtc(sc, crtreg, iga);
    796  1.4.10.2  yamt 	uni_fix_crtc(sc);
    797  1.4.10.2  yamt 	uni_crt_lock(sc);
    798  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR17, 0x80, BIT7);
    799  1.4.10.2  yamt 
    800  1.4.10.2  yamt 	uni_load_offset(sc, haddr, bpp_byte, iga);
    801  1.4.10.2  yamt 	uni_load_fetchcnt(sc, haddr, bpp_byte, iga);
    802  1.4.10.2  yamt 	uni_load_fifo(sc, iga, haddr, vaddr);
    803  1.4.10.2  yamt 
    804  1.4.10.2  yamt 	uni_set_depth(sc, bpp_byte, iga);
    805  1.4.10.2  yamt 	pll_d_n = uni_get_clkval(sc, ctbl[index].clk);
    806  1.4.10.2  yamt 	uni_set_vclk(sc, pll_d_n, iga);
    807  1.4.10.2  yamt }
    808  1.4.10.2  yamt 
    809  1.4.10.2  yamt static void
    810  1.4.10.2  yamt uni_load_crtc(struct unichromefb_softc *sc,
    811  1.4.10.2  yamt     struct display_timing device_timing, int iga)
    812  1.4.10.2  yamt {
    813  1.4.10.2  yamt 	int regnum, val;
    814  1.4.10.2  yamt 	struct io_register *reg;
    815  1.4.10.2  yamt 	int i;
    816  1.4.10.2  yamt 
    817  1.4.10.2  yamt 	regnum = val = 0;
    818  1.4.10.2  yamt 	reg = NULL;
    819  1.4.10.2  yamt 
    820  1.4.10.2  yamt 	uni_crt_unlock(sc);
    821  1.4.10.2  yamt 
    822  1.4.10.2  yamt 	for (i = 0; i < 12; i++) {
    823  1.4.10.2  yamt 		switch (iga) {
    824  1.4.10.2  yamt 		case IGA1:
    825  1.4.10.2  yamt 			switch (i) {
    826  1.4.10.2  yamt 			case H_TOTAL_INDEX:
    827  1.4.10.2  yamt 				val = IGA1_HOR_TOTAL_FORMULA(
    828  1.4.10.2  yamt 				    device_timing.hor_total);
    829  1.4.10.2  yamt 				regnum = iga1_crtc_reg.hor_total.reg_num;
    830  1.4.10.2  yamt 				reg = iga1_crtc_reg.hor_total.reg;
    831  1.4.10.2  yamt 				break;
    832  1.4.10.2  yamt 			case H_ADDR_INDEX:
    833  1.4.10.2  yamt 				val = IGA1_HOR_ADDR_FORMULA(
    834  1.4.10.2  yamt 				    device_timing.hor_addr);
    835  1.4.10.2  yamt 				regnum = iga1_crtc_reg.hor_addr.reg_num;
    836  1.4.10.2  yamt 				reg = iga1_crtc_reg.hor_addr.reg;
    837  1.4.10.2  yamt 				break;
    838  1.4.10.2  yamt 			case H_BLANK_START_INDEX:
    839  1.4.10.2  yamt 				val = IGA1_HOR_BLANK_START_FORMULA(
    840  1.4.10.2  yamt 				    device_timing.hor_blank_start);
    841  1.4.10.2  yamt 				regnum = iga1_crtc_reg.hor_blank_start.reg_num;
    842  1.4.10.2  yamt 				reg = iga1_crtc_reg.hor_blank_start.reg;
    843  1.4.10.2  yamt 				break;
    844  1.4.10.2  yamt 			case H_BLANK_END_INDEX:
    845  1.4.10.2  yamt 				val = IGA1_HOR_BLANK_END_FORMULA(
    846  1.4.10.2  yamt 				    device_timing.hor_blank_start,
    847  1.4.10.2  yamt 				    device_timing.hor_blank_end);
    848  1.4.10.2  yamt 				regnum = iga1_crtc_reg.hor_blank_end.reg_num;
    849  1.4.10.2  yamt 				reg = iga1_crtc_reg.hor_blank_end.reg;
    850  1.4.10.2  yamt 				break;
    851  1.4.10.2  yamt 			case H_SYNC_START_INDEX:
    852  1.4.10.2  yamt 				val = IGA1_HOR_SYNC_START_FORMULA(
    853  1.4.10.2  yamt 				    device_timing.hor_sync_start);
    854  1.4.10.2  yamt 				regnum = iga1_crtc_reg.hor_sync_start.reg_num;
    855  1.4.10.2  yamt 				reg = iga1_crtc_reg.hor_sync_start.reg;
    856  1.4.10.2  yamt 				break;
    857  1.4.10.2  yamt 			case H_SYNC_END_INDEX:
    858  1.4.10.2  yamt 				val = IGA1_HOR_SYNC_END_FORMULA(
    859  1.4.10.2  yamt 				    device_timing.hor_sync_start,
    860  1.4.10.2  yamt 				    device_timing.hor_sync_end);
    861  1.4.10.2  yamt 				regnum = iga1_crtc_reg.hor_sync_end.reg_num;
    862  1.4.10.2  yamt 				reg = iga1_crtc_reg.hor_sync_end.reg;
    863  1.4.10.2  yamt 				break;
    864  1.4.10.2  yamt 			case V_TOTAL_INDEX:
    865  1.4.10.2  yamt 				val = IGA1_VER_TOTAL_FORMULA(
    866  1.4.10.2  yamt 				    device_timing.ver_total);
    867  1.4.10.2  yamt 				regnum = iga1_crtc_reg.ver_total.reg_num;
    868  1.4.10.2  yamt 				reg = iga1_crtc_reg.ver_total.reg;
    869  1.4.10.2  yamt 				break;
    870  1.4.10.2  yamt 			case V_ADDR_INDEX:
    871  1.4.10.2  yamt 				val = IGA1_VER_ADDR_FORMULA(
    872  1.4.10.2  yamt 				    device_timing.ver_addr);
    873  1.4.10.2  yamt 				regnum = iga1_crtc_reg.ver_addr.reg_num;
    874  1.4.10.2  yamt 				reg = iga1_crtc_reg.ver_addr.reg;
    875  1.4.10.2  yamt 				break;
    876  1.4.10.2  yamt 			case V_BLANK_START_INDEX:
    877  1.4.10.2  yamt 				val = IGA1_VER_BLANK_START_FORMULA(
    878  1.4.10.2  yamt 				    device_timing.ver_blank_start);
    879  1.4.10.2  yamt 				regnum = iga1_crtc_reg.ver_blank_start.reg_num;
    880  1.4.10.2  yamt 				reg = iga1_crtc_reg.ver_blank_start.reg;
    881  1.4.10.2  yamt 				break;
    882  1.4.10.2  yamt 			case V_BLANK_END_INDEX:
    883  1.4.10.2  yamt 				val = IGA1_VER_BLANK_END_FORMULA(
    884  1.4.10.2  yamt 				    device_timing.ver_blank_start,
    885  1.4.10.2  yamt 				    device_timing.ver_blank_end);
    886  1.4.10.2  yamt 				regnum = iga1_crtc_reg.ver_blank_end.reg_num;
    887  1.4.10.2  yamt 				reg = iga1_crtc_reg.ver_blank_end.reg;
    888  1.4.10.2  yamt 				break;
    889  1.4.10.2  yamt 			case V_SYNC_START_INDEX:
    890  1.4.10.2  yamt 				val = IGA1_VER_SYNC_START_FORMULA(
    891  1.4.10.2  yamt 				    device_timing.ver_sync_start);
    892  1.4.10.2  yamt 				regnum = iga1_crtc_reg.ver_sync_start.reg_num;
    893  1.4.10.2  yamt 				reg = iga1_crtc_reg.ver_sync_start.reg;
    894  1.4.10.2  yamt 				break;
    895  1.4.10.2  yamt 			case V_SYNC_END_INDEX:
    896  1.4.10.2  yamt 				val = IGA1_VER_SYNC_END_FORMULA(
    897  1.4.10.2  yamt 				    device_timing.ver_sync_start,
    898  1.4.10.2  yamt 				    device_timing.ver_sync_end);
    899  1.4.10.2  yamt 				regnum = iga1_crtc_reg.ver_sync_end.reg_num;
    900  1.4.10.2  yamt 				reg = iga1_crtc_reg.ver_sync_end.reg;
    901  1.4.10.2  yamt 				break;
    902  1.4.10.2  yamt 			default:
    903  1.4.10.2  yamt 				printf("%s: unknown index %d while setting up CRTC\n",
    904  1.4.10.2  yamt 				    sc->sc_dev.dv_xname, i);
    905  1.4.10.2  yamt 				break;
    906  1.4.10.2  yamt 			}
    907  1.4.10.2  yamt 			break;
    908  1.4.10.2  yamt 		case IGA2:
    909  1.4.10.2  yamt 			printf("%s: %s: IGA2 not supported\n",
    910  1.4.10.2  yamt 			    sc->sc_dev.dv_xname, __func__);
    911  1.4.10.2  yamt 			break;
    912  1.4.10.2  yamt 		}
    913  1.4.10.2  yamt 
    914  1.4.10.2  yamt 		uni_load_reg(sc, val, regnum, reg, VIACR);
    915  1.4.10.2  yamt 	}
    916  1.4.10.2  yamt 
    917  1.4.10.2  yamt 	uni_crt_lock(sc);
    918  1.4.10.2  yamt }
    919  1.4.10.2  yamt 
    920  1.4.10.2  yamt static void
    921  1.4.10.2  yamt uni_load_reg(struct unichromefb_softc *sc, int timing, int regnum,
    922  1.4.10.2  yamt     struct io_register *reg, int type)
    923  1.4.10.2  yamt {
    924  1.4.10.2  yamt 	int regmask, bitnum, data;
    925  1.4.10.2  yamt 	int i, j;
    926  1.4.10.2  yamt 	int shift_next_reg;
    927  1.4.10.2  yamt 	int startidx, endidx, cridx;
    928  1.4.10.2  yamt 	uint16_t getbit;
    929  1.4.10.2  yamt 
    930  1.4.10.2  yamt 	bitnum = 0;
    931  1.4.10.2  yamt 
    932  1.4.10.2  yamt 	for (i = 0; i < regnum; i++) {
    933  1.4.10.2  yamt 		regmask = data = 0;
    934  1.4.10.2  yamt 		startidx = reg[i].start_bit;
    935  1.4.10.2  yamt 		endidx = reg[i].end_bit;
    936  1.4.10.2  yamt 		cridx = reg[i].io_addr;
    937  1.4.10.2  yamt 
    938  1.4.10.2  yamt 		shift_next_reg = bitnum;
    939  1.4.10.2  yamt 
    940  1.4.10.2  yamt 		for (j = startidx; j <= endidx; j++) {
    941  1.4.10.2  yamt 			regmask = regmask | (BIT0 << j);
    942  1.4.10.2  yamt 			getbit = (timing & (BIT0 << bitnum));
    943  1.4.10.2  yamt 			data = data | ((getbit >> shift_next_reg) << startidx);
    944  1.4.10.2  yamt 			++bitnum;
    945  1.4.10.2  yamt 		}
    946  1.4.10.2  yamt 
    947  1.4.10.2  yamt 		if (type == VIACR)
    948  1.4.10.2  yamt 			uni_wr_mask(sc, VIACR, cridx, data, regmask);
    949  1.4.10.2  yamt 		else
    950  1.4.10.2  yamt 			uni_wr_mask(sc, VIASR, cridx, data, regmask);
    951  1.4.10.2  yamt 	}
    952  1.4.10.2  yamt 
    953  1.4.10.2  yamt 	return;
    954  1.4.10.2  yamt }
    955  1.4.10.2  yamt 
    956  1.4.10.2  yamt static void
    957  1.4.10.2  yamt uni_fix_crtc(struct unichromefb_softc *sc)
    958  1.4.10.2  yamt {
    959  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR03, 0x80, BIT7);
    960  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR18, 0xff);
    961  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR07, 0x10, BIT4);
    962  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR09, 0x40, BIT6);
    963  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR35, 0x10, BIT4);
    964  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR33, 0x06, BIT0+BIT1+BIT2);
    965  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR17, 0xe3);
    966  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR08, 0x00);
    967  1.4.10.2  yamt 	uni_wr(sc, VIACR, CR14, 0x00);
    968  1.4.10.2  yamt 
    969  1.4.10.2  yamt 	return;
    970  1.4.10.2  yamt }
    971  1.4.10.2  yamt 
    972  1.4.10.2  yamt static void
    973  1.4.10.2  yamt uni_load_offset(struct unichromefb_softc *sc, int haddr, int bpp, int iga)
    974  1.4.10.2  yamt {
    975  1.4.10.2  yamt 
    976  1.4.10.2  yamt 	switch (iga) {
    977  1.4.10.2  yamt 	case IGA1:
    978  1.4.10.2  yamt 		uni_load_reg(sc,
    979  1.4.10.2  yamt 		    IGA1_OFFSET_FORMULA(haddr, bpp),
    980  1.4.10.2  yamt 		    offset_reg.iga1_offset_reg.reg_num,
    981  1.4.10.2  yamt 		    offset_reg.iga1_offset_reg.reg,
    982  1.4.10.2  yamt 		    VIACR);
    983  1.4.10.2  yamt 		break;
    984  1.4.10.2  yamt 	default:
    985  1.4.10.2  yamt 		printf("%s: %s: only IGA1 is supported\n", sc->sc_dev.dv_xname,
    986  1.4.10.2  yamt 		    __func__);
    987  1.4.10.2  yamt 		break;
    988  1.4.10.2  yamt 	}
    989  1.4.10.2  yamt 
    990  1.4.10.2  yamt 	return;
    991  1.4.10.2  yamt }
    992  1.4.10.2  yamt 
    993  1.4.10.2  yamt static void
    994  1.4.10.2  yamt uni_load_fetchcnt(struct unichromefb_softc *sc, int haddr, int bpp, int iga)
    995  1.4.10.2  yamt {
    996  1.4.10.2  yamt 
    997  1.4.10.2  yamt 	switch (iga) {
    998  1.4.10.2  yamt 	case IGA1:
    999  1.4.10.2  yamt 		uni_load_reg(sc,
   1000  1.4.10.2  yamt 		    IGA1_FETCH_COUNT_FORMULA(haddr, bpp),
   1001  1.4.10.2  yamt 		    fetch_count_reg.iga1_fetch_count_reg.reg_num,
   1002  1.4.10.2  yamt 		    fetch_count_reg.iga1_fetch_count_reg.reg,
   1003  1.4.10.2  yamt 		    VIASR);
   1004  1.4.10.2  yamt 		break;
   1005  1.4.10.2  yamt 	default:
   1006  1.4.10.2  yamt 		printf("%s: %s: only IGA1 is supported\n", sc->sc_dev.dv_xname,
   1007  1.4.10.2  yamt 		    __func__);
   1008  1.4.10.2  yamt 		break;
   1009  1.4.10.2  yamt 	}
   1010  1.4.10.2  yamt 
   1011  1.4.10.2  yamt 	return;
   1012  1.4.10.2  yamt }
   1013  1.4.10.2  yamt 
   1014  1.4.10.2  yamt static void
   1015  1.4.10.2  yamt uni_load_fifo(struct unichromefb_softc *sc, int iga, int horact, int veract)
   1016  1.4.10.2  yamt {
   1017  1.4.10.2  yamt 	int val, regnum;
   1018  1.4.10.2  yamt 	struct io_register *reg;
   1019  1.4.10.2  yamt 	int iga1_fifo_max_depth, iga1_fifo_threshold;
   1020  1.4.10.2  yamt 	int iga1_fifo_high_threshold, iga1_display_queue_expire_num;
   1021  1.4.10.2  yamt 
   1022  1.4.10.2  yamt 	reg = NULL;
   1023  1.4.10.2  yamt 	iga1_fifo_max_depth = iga1_fifo_threshold = 0;
   1024  1.4.10.2  yamt 	iga1_fifo_high_threshold = iga1_display_queue_expire_num = 0;
   1025  1.4.10.2  yamt 
   1026  1.4.10.2  yamt 	switch (iga) {
   1027  1.4.10.2  yamt 	case IGA1:
   1028  1.4.10.2  yamt 		/* XXX if (type == CN900) { */
   1029  1.4.10.2  yamt 		iga1_fifo_max_depth = CN900_IGA1_FIFO_MAX_DEPTH;
   1030  1.4.10.2  yamt 		iga1_fifo_threshold = CN900_IGA1_FIFO_THRESHOLD;
   1031  1.4.10.2  yamt 		iga1_fifo_high_threshold = CN900_IGA1_FIFO_HIGH_THRESHOLD;
   1032  1.4.10.2  yamt 		if (horact > 1280 && veract > 1024)
   1033  1.4.10.2  yamt 			iga1_display_queue_expire_num = 16;
   1034  1.4.10.2  yamt 		else
   1035  1.4.10.2  yamt 			iga1_display_queue_expire_num =
   1036  1.4.10.2  yamt 			    CN900_IGA1_DISPLAY_QUEUE_EXPIRE_NUM;
   1037  1.4.10.2  yamt 		/* XXX } */
   1038  1.4.10.2  yamt 
   1039  1.4.10.2  yamt 		/* set display FIFO depth select */
   1040  1.4.10.2  yamt 		val = IGA1_FIFO_DEPTH_SELECT_FORMULA(iga1_fifo_max_depth);
   1041  1.4.10.2  yamt 		regnum =
   1042  1.4.10.2  yamt 		    display_fifo_depth_reg.iga1_fifo_depth_select_reg.reg_num;
   1043  1.4.10.2  yamt 		reg = display_fifo_depth_reg.iga1_fifo_depth_select_reg.reg;
   1044  1.4.10.2  yamt 		uni_load_reg(sc, val, regnum, reg, VIASR);
   1045  1.4.10.2  yamt 
   1046  1.4.10.2  yamt 		/* set display FIFO threshold select */
   1047  1.4.10.2  yamt 		val = IGA1_FIFO_THRESHOLD_FORMULA(iga1_fifo_threshold);
   1048  1.4.10.2  yamt 		regnum = fifo_threshold_select_reg.iga1_fifo_threshold_select_reg.reg_num;
   1049  1.4.10.2  yamt 		reg = fifo_threshold_select_reg.iga1_fifo_threshold_select_reg.reg;
   1050  1.4.10.2  yamt 		uni_load_reg(sc, val, regnum, reg, VIASR);
   1051  1.4.10.2  yamt 
   1052  1.4.10.2  yamt 		/* set display FIFO high threshold select */
   1053  1.4.10.2  yamt 		val = IGA1_FIFO_HIGH_THRESHOLD_FORMULA(iga1_fifo_high_threshold);
   1054  1.4.10.2  yamt 		regnum = fifo_high_threshold_select_reg.iga1_fifo_high_threshold_select_reg.reg_num;
   1055  1.4.10.2  yamt 		reg = fifo_high_threshold_select_reg.iga1_fifo_high_threshold_select_reg.reg;
   1056  1.4.10.2  yamt 		uni_load_reg(sc, val, regnum, reg, VIASR);
   1057  1.4.10.2  yamt 
   1058  1.4.10.2  yamt 		/* set display queue expire num */
   1059  1.4.10.2  yamt 		val = IGA1_DISPLAY_QUEUE_EXPIRE_NUM_FORMULA(iga1_display_queue_expire_num);
   1060  1.4.10.2  yamt 		regnum = display_queue_expire_num_reg.iga1_display_queue_expire_num_reg.reg_num;
   1061  1.4.10.2  yamt 		reg = display_queue_expire_num_reg.iga1_display_queue_expire_num_reg.reg;
   1062  1.4.10.2  yamt 		uni_load_reg(sc, val, regnum, reg, VIASR);
   1063  1.4.10.2  yamt 
   1064  1.4.10.2  yamt 		break;
   1065  1.4.10.2  yamt 	default:
   1066  1.4.10.2  yamt 		printf("%s: %s: only IGA1 is supported\n", sc->sc_dev.dv_xname,
   1067  1.4.10.2  yamt 		    __func__);
   1068  1.4.10.2  yamt 		break;
   1069  1.4.10.2  yamt 	}
   1070  1.4.10.2  yamt 
   1071  1.4.10.2  yamt 	return;
   1072  1.4.10.2  yamt }
   1073  1.4.10.2  yamt 
   1074  1.4.10.2  yamt static void
   1075  1.4.10.2  yamt uni_set_depth(struct unichromefb_softc *sc, int bpp, int iga)
   1076  1.4.10.2  yamt {
   1077  1.4.10.2  yamt 	switch (iga) {
   1078  1.4.10.2  yamt 	case IGA1:
   1079  1.4.10.2  yamt 		switch (bpp) {
   1080  1.4.10.2  yamt 		case MODE_32BPP:
   1081  1.4.10.2  yamt 			uni_wr_mask(sc, VIASR, SR15, 0xae, 0xfe);
   1082  1.4.10.2  yamt 			break;
   1083  1.4.10.2  yamt 		case MODE_16BPP:
   1084  1.4.10.2  yamt 			uni_wr_mask(sc, VIASR, SR15, 0xb6, 0xfe);
   1085  1.4.10.2  yamt 			break;
   1086  1.4.10.2  yamt 		case MODE_8BPP:
   1087  1.4.10.2  yamt 			uni_wr_mask(sc, VIASR, SR15, 0x22, 0xfe);
   1088  1.4.10.2  yamt 			break;
   1089  1.4.10.2  yamt 		default:
   1090  1.4.10.2  yamt 			printf("%s: %s: mode (%d) unsupported\n",
   1091  1.4.10.2  yamt 			    sc->sc_dev.dv_xname, __func__, bpp);
   1092  1.4.10.2  yamt 		}
   1093  1.4.10.2  yamt 		break;
   1094  1.4.10.2  yamt 	default:
   1095  1.4.10.2  yamt 		printf("%s: %s: only IGA1 is supported\n", sc->sc_dev.dv_xname,
   1096  1.4.10.2  yamt 		    __func__);
   1097  1.4.10.2  yamt 		break;
   1098  1.4.10.2  yamt 	}
   1099  1.4.10.2  yamt }
   1100  1.4.10.2  yamt 
   1101  1.4.10.2  yamt static uint32_t
   1102  1.4.10.2  yamt uni_get_clkval(struct unichromefb_softc *sc, int clk)
   1103  1.4.10.2  yamt {
   1104  1.4.10.2  yamt 	int i;
   1105  1.4.10.2  yamt 
   1106  1.4.10.2  yamt 	for (i = 0; i < NUM_TOTAL_PLL_TABLE; i++) {
   1107  1.4.10.2  yamt 		if (clk == pll_value[i].clk) {
   1108  1.4.10.2  yamt 			/* XXX only CN900 supported for now */
   1109  1.4.10.2  yamt 			return pll_value[i].k800_pll;
   1110  1.4.10.2  yamt 		}
   1111  1.4.10.2  yamt 	}
   1112  1.4.10.2  yamt 
   1113  1.4.10.2  yamt 	aprint_error("%s: can't find matching PLL value\n",
   1114  1.4.10.2  yamt 	    sc->sc_dev.dv_xname);
   1115  1.4.10.2  yamt 
   1116  1.4.10.2  yamt 	return 0;
   1117  1.4.10.2  yamt }
   1118  1.4.10.2  yamt 
   1119  1.4.10.2  yamt static void
   1120  1.4.10.2  yamt uni_set_vclk(struct unichromefb_softc *sc, uint32_t clk, int iga)
   1121  1.4.10.2  yamt {
   1122  1.4.10.2  yamt 	uint8_t val;
   1123  1.4.10.2  yamt 
   1124  1.4.10.2  yamt 	/* hardware reset on */
   1125  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR17, 0x00, BIT7);
   1126  1.4.10.2  yamt 
   1127  1.4.10.2  yamt 	switch (iga) {
   1128  1.4.10.2  yamt 	case IGA1:
   1129  1.4.10.2  yamt 		/* XXX only CN900 is supported */
   1130  1.4.10.2  yamt 		uni_wr(sc, VIASR, SR44, clk / 0x10000);
   1131  1.4.10.2  yamt 		uni_wr(sc, VIASR, SR45, (clk & 0xffff) / 0x100);
   1132  1.4.10.2  yamt 		uni_wr(sc, VIASR, SR46, clk % 0x100);
   1133  1.4.10.2  yamt 		break;
   1134  1.4.10.2  yamt 	default:
   1135  1.4.10.2  yamt 		printf("%s: %s: only IGA1 is supported\n", sc->sc_dev.dv_xname,
   1136  1.4.10.2  yamt 		    __func__);
   1137  1.4.10.2  yamt 		break;
   1138  1.4.10.2  yamt 	}
   1139  1.4.10.2  yamt 
   1140  1.4.10.2  yamt 	/* hardware reset off */
   1141  1.4.10.2  yamt 	uni_wr_mask(sc, VIACR, CR17, 0x80, BIT7);
   1142  1.4.10.2  yamt 
   1143  1.4.10.2  yamt 	/* reset pll */
   1144  1.4.10.2  yamt 	switch (iga) {
   1145  1.4.10.2  yamt 	case IGA1:
   1146  1.4.10.2  yamt 		uni_wr_mask(sc, VIASR, SR40, 0x02, BIT1);
   1147  1.4.10.2  yamt 		uni_wr_mask(sc, VIASR, SR40, 0x00, BIT1);
   1148  1.4.10.2  yamt 		break;
   1149  1.4.10.2  yamt 	}
   1150  1.4.10.2  yamt 
   1151  1.4.10.2  yamt 	/* good to go */
   1152  1.4.10.2  yamt 	val = bus_space_read_1(sc->sc_iot, sc->sc_ioh, VIARMisc);
   1153  1.4.10.2  yamt 	val |= (BIT2+BIT3);
   1154  1.4.10.2  yamt 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, VIAWMisc, val);
   1155  1.4.10.2  yamt 
   1156  1.4.10.2  yamt 	return;
   1157  1.4.10.2  yamt }
   1158  1.4.10.2  yamt 
   1159  1.4.10.2  yamt static void
   1160  1.4.10.2  yamt uni_init_dac(struct unichromefb_softc *sc, int iga)
   1161  1.4.10.2  yamt {
   1162  1.4.10.2  yamt 	int i;
   1163  1.4.10.2  yamt 
   1164  1.4.10.2  yamt 	/* XXX only IGA1 for now */
   1165  1.4.10.2  yamt 	uni_wr_mask(sc, VIASR, SR1A, 0x00, BIT0);
   1166  1.4.10.2  yamt 	uni_wr_mask(sc, VIASR, SR18, 0x00, BIT7+BIT6);
   1167  1.4.10.2  yamt 	for (i = 0; i < 256; i++)
   1168  1.4.10.2  yamt 		uni_wr_dac(sc, i,
   1169  1.4.10.2  yamt 		    palLUT_table[i].red, palLUT_table[i].green, palLUT_table[i].blue);
   1170  1.4.10.2  yamt 
   1171  1.4.10.2  yamt 	uni_wr_mask(sc, VIASR, SR18, 0xc0, BIT7+BIT6);
   1172  1.4.10.2  yamt 
   1173  1.4.10.2  yamt 	return;
   1174  1.4.10.2  yamt }
   1175  1.4.10.2  yamt 
   1176  1.4.10.2  yamt static void
   1177  1.4.10.2  yamt uni_init_accel(struct unichromefb_softc *sc)
   1178  1.4.10.2  yamt {
   1179  1.4.10.2  yamt 
   1180  1.4.10.2  yamt 	/* init 2D engine regs to reset 2D engine */
   1181  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_GEMODE, 0);
   1182  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCPOS, 0);
   1183  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTPOS, 0);
   1184  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DIMENSION, 0);
   1185  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_PATADDR, 0);
   1186  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_FGCOLOR, 0);
   1187  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_BGCOLOR, 0);
   1188  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_CLIPTL, 0);
   1189  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_CLIPBR, 0);
   1190  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_OFFSET, 0);
   1191  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_KEYCONTROL, 0);
   1192  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCBASE, 0);
   1193  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTBASE, 0);
   1194  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_PITCH, 0);
   1195  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_MONOPAT1, 0);
   1196  1.4.10.2  yamt 
   1197  1.4.10.2  yamt 	/* init AGP and VQ registers */
   1198  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSET, 0x00100000);
   1199  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x00000000);
   1200  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x00333004);
   1201  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x60000000);
   1202  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x61000000);
   1203  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x62000000);
   1204  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x63000000);
   1205  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x64000000);
   1206  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x7d000000);
   1207  1.4.10.2  yamt 
   1208  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSET, 0xfe020000);
   1209  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x00000000);
   1210  1.4.10.2  yamt 
   1211  1.4.10.2  yamt 	/* disable VQ */
   1212  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSET, 0x00fe0000);
   1213  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x00000004);
   1214  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x40008c0f);
   1215  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x44000000);
   1216  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x45080c04);
   1217  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_TRANSPACE, 0x46800408);
   1218  1.4.10.2  yamt 
   1219  1.4.10.2  yamt 	uni_set_accel_depth(sc);
   1220  1.4.10.2  yamt 
   1221  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCBASE, 0);
   1222  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTBASE, 0);
   1223  1.4.10.2  yamt 
   1224  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
   1225  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
   1226  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
   1227  1.4.10.2  yamt 
   1228  1.4.10.2  yamt 	return;
   1229  1.4.10.2  yamt }
   1230  1.4.10.2  yamt 
   1231  1.4.10.2  yamt static void
   1232  1.4.10.2  yamt uni_set_accel_depth(struct unichromefb_softc *sc)
   1233  1.4.10.2  yamt {
   1234  1.4.10.2  yamt 	uint32_t gemode;
   1235  1.4.10.2  yamt 
   1236  1.4.10.2  yamt 	gemode = MMIO_IN32(0x04) & 0xfffffcff;
   1237  1.4.10.2  yamt 
   1238  1.4.10.2  yamt 	switch (sc->sc_depth) {
   1239  1.4.10.2  yamt 	case 32:
   1240  1.4.10.2  yamt 		gemode |= VIA_GEM_32bpp;
   1241  1.4.10.2  yamt 		break;
   1242  1.4.10.2  yamt 	case 16:
   1243  1.4.10.2  yamt 		gemode |= VIA_GEM_16bpp;
   1244  1.4.10.2  yamt 		break;
   1245  1.4.10.2  yamt 	default:
   1246  1.4.10.2  yamt 		gemode |= VIA_GEM_8bpp;
   1247  1.4.10.2  yamt 		break;
   1248  1.4.10.2  yamt 	}
   1249  1.4.10.2  yamt 
   1250  1.4.10.2  yamt 	/* set colour depth and pitch */
   1251  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_GEMODE, gemode);
   1252  1.4.10.2  yamt 
   1253  1.4.10.2  yamt 	return;
   1254  1.4.10.2  yamt }
   1255  1.4.10.2  yamt 
   1256  1.4.10.2  yamt static void
   1257  1.4.10.2  yamt uni_wait_idle(struct unichromefb_softc *sc)
   1258  1.4.10.2  yamt {
   1259  1.4.10.2  yamt 	int loop = 0;
   1260  1.4.10.2  yamt 
   1261  1.4.10.2  yamt 	while (!(MMIO_IN32(VIA_REG_STATUS) & VIA_VR_QUEUE_BUSY) &&
   1262  1.4.10.2  yamt 	    (loop++ < MAXLOOP))
   1263  1.4.10.2  yamt 		;
   1264  1.4.10.2  yamt 
   1265  1.4.10.2  yamt 	while ((MMIO_IN32(VIA_REG_STATUS) &
   1266  1.4.10.2  yamt 	    (VIA_CMD_RGTR_BUSY | VIA_2D_ENG_BUSY | VIA_3D_ENG_BUSY)) &&
   1267  1.4.10.2  yamt 	    (loop++ < MAXLOOP))
   1268  1.4.10.2  yamt 		;
   1269  1.4.10.2  yamt 
   1270  1.4.10.2  yamt 	if (loop >= MAXLOOP)
   1271  1.4.10.2  yamt 		aprint_error("%s: engine stall\n", sc->sc_dev.dv_xname);
   1272  1.4.10.2  yamt 
   1273  1.4.10.2  yamt 	return;
   1274  1.4.10.2  yamt }
   1275  1.4.10.2  yamt 
   1276  1.4.10.2  yamt static void
   1277  1.4.10.2  yamt uni_fillrect(struct unichromefb_softc *sc, int x, int y, int width,
   1278  1.4.10.2  yamt     int height, int colour)
   1279  1.4.10.2  yamt {
   1280  1.4.10.2  yamt 
   1281  1.4.10.2  yamt 	uni_wait_idle(sc);
   1282  1.4.10.2  yamt 
   1283  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCPOS, 0);
   1284  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCBASE, 0);
   1285  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTBASE, 0);
   1286  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
   1287  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
   1288  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
   1289  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTPOS, ((y << 16) | x));
   1290  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DIMENSION,
   1291  1.4.10.2  yamt 	    (((height - 1) << 16) | (width - 1)));
   1292  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_FGCOLOR, colour);
   1293  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_GECMD, (0x01 | 0x2000 | 0xf0 << 24));
   1294  1.4.10.2  yamt 
   1295  1.4.10.2  yamt 	return;
   1296  1.4.10.2  yamt }
   1297  1.4.10.2  yamt 
   1298  1.4.10.2  yamt static void
   1299  1.4.10.2  yamt uni_rectinvert(struct unichromefb_softc *sc, int x, int y, int width,
   1300  1.4.10.2  yamt     int height)
   1301  1.4.10.2  yamt {
   1302  1.4.10.2  yamt 
   1303  1.4.10.2  yamt 	uni_wait_idle(sc);
   1304  1.4.10.2  yamt 
   1305  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCPOS, 0);
   1306  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCBASE, 0);
   1307  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTBASE, 0);
   1308  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
   1309  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
   1310  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
   1311  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTPOS, ((y << 16) | x));
   1312  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DIMENSION,
   1313  1.4.10.2  yamt 	    (((height - 1) << 16) | (width - 1)));
   1314  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_GECMD, (0x01 | 0x2000 | 0x55 << 24));
   1315  1.4.10.2  yamt 
   1316  1.4.10.2  yamt 	return;
   1317  1.4.10.2  yamt }
   1318  1.4.10.2  yamt 
   1319  1.4.10.2  yamt static void
   1320  1.4.10.2  yamt uni_bitblit(struct unichromefb_softc *sc, int xs, int ys, int xd, int yd, int width, int height)
   1321  1.4.10.2  yamt {
   1322  1.4.10.2  yamt 	uint32_t dir;
   1323  1.4.10.2  yamt 
   1324  1.4.10.2  yamt 	dir = 0;
   1325  1.4.10.2  yamt 
   1326  1.4.10.2  yamt 	if (ys < yd) {
   1327  1.4.10.2  yamt 		yd += height - 1;
   1328  1.4.10.2  yamt 		ys += height - 1;
   1329  1.4.10.2  yamt 		dir |= 0x4000;
   1330  1.4.10.2  yamt 	}
   1331  1.4.10.2  yamt 
   1332  1.4.10.2  yamt 	if (xs < xd) {
   1333  1.4.10.2  yamt 		xd += width - 1;
   1334  1.4.10.2  yamt 		xs += width - 1;
   1335  1.4.10.2  yamt 		dir |= 0x8000;
   1336  1.4.10.2  yamt 	}
   1337  1.4.10.2  yamt 
   1338  1.4.10.2  yamt 	uni_wait_idle(sc);
   1339  1.4.10.2  yamt 
   1340  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCBASE, 0);
   1341  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTBASE, 0);
   1342  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
   1343  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
   1344  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
   1345  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCPOS, ys << 16 | xs);
   1346  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTPOS, yd << 16 | xd);
   1347  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DIMENSION, ((height - 1) << 16) | (width - 1));
   1348  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_GECMD, (0x01 | dir | (0xcc << 24)));
   1349  1.4.10.2  yamt 
   1350  1.4.10.2  yamt 	return;
   1351  1.4.10.2  yamt }
   1352  1.4.10.2  yamt 
   1353  1.4.10.2  yamt static void
   1354  1.4.10.2  yamt uni_setup_mono(struct unichromefb_softc *sc, int xd, int yd, int width, int height,
   1355  1.4.10.2  yamt     uint32_t fg, uint32_t bg)
   1356  1.4.10.2  yamt {
   1357  1.4.10.2  yamt 
   1358  1.4.10.2  yamt 	uni_wait_idle(sc);
   1359  1.4.10.2  yamt 
   1360  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCBASE, 0);
   1361  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTBASE, 0);
   1362  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_PITCH, VIA_PITCH_ENABLE |
   1363  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) |
   1364  1.4.10.2  yamt 	    (((sc->sc_width * sc->sc_depth >> 3) >> 3) << 16)));
   1365  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_SRCPOS, 0);
   1366  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DSTPOS, (yd << 16) | xd);
   1367  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_DIMENSION, ((height - 1) << 16) | (width - 1));
   1368  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_FGCOLOR, fg);
   1369  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_BGCOLOR, bg);
   1370  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_GECMD, 0xcc020142);
   1371  1.4.10.2  yamt 
   1372  1.4.10.2  yamt 	return;
   1373  1.4.10.2  yamt }
   1374  1.4.10.2  yamt 
   1375  1.4.10.2  yamt #if notyet
   1376  1.4.10.2  yamt static void
   1377  1.4.10.2  yamt uni_cursor_show(struct unichromefb_softc *sc)
   1378  1.4.10.2  yamt {
   1379  1.4.10.2  yamt 	uint32_t val;
   1380  1.4.10.2  yamt 
   1381  1.4.10.2  yamt 	val = MMIO_IN32(VIA_REG_CURSOR_MODE);
   1382  1.4.10.2  yamt 	val |= 1;
   1383  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_CURSOR_MODE, val);
   1384  1.4.10.2  yamt 
   1385  1.4.10.2  yamt 	return;
   1386  1.4.10.2  yamt }
   1387  1.4.10.2  yamt 
   1388  1.4.10.2  yamt static void
   1389  1.4.10.2  yamt uni_cursor_hide(struct unichromefb_softc *sc)
   1390  1.4.10.2  yamt {
   1391  1.4.10.2  yamt 	uint32_t val;
   1392  1.4.10.2  yamt 
   1393  1.4.10.2  yamt 	val = MMIO_IN32(VIA_REG_CURSOR_MODE);
   1394  1.4.10.2  yamt 	val &= 0xfffffffe;
   1395  1.4.10.2  yamt 	MMIO_OUT32(VIA_REG_CURSOR_MODE, val);
   1396  1.4.10.2  yamt 
   1397  1.4.10.2  yamt 	return;
   1398  1.4.10.2  yamt }
   1399  1.4.10.2  yamt #endif
   1400  1.4.10.2  yamt 
   1401  1.4.10.2  yamt /*
   1402  1.4.10.2  yamt  * rasops glue
   1403  1.4.10.2  yamt  */
   1404  1.4.10.2  yamt static void
   1405  1.4.10.2  yamt uni_copycols(void *opaque, int row, int srccol, int dstcol, int ncols)
   1406  1.4.10.2  yamt {
   1407  1.4.10.2  yamt 	struct rasops_info *ri;
   1408  1.4.10.2  yamt 	struct vcons_screen *scr;
   1409  1.4.10.2  yamt 	struct unichromefb_softc *sc;
   1410  1.4.10.2  yamt 	int xs, xd, y, width, height;
   1411  1.4.10.2  yamt 
   1412  1.4.10.2  yamt 	ri = (struct rasops_info *)opaque;
   1413  1.4.10.2  yamt 	scr = (struct vcons_screen *)ri->ri_hw;
   1414  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)scr->scr_cookie;
   1415  1.4.10.2  yamt 
   1416  1.4.10.2  yamt 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
   1417  1.4.10.2  yamt 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1418  1.4.10.2  yamt 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1419  1.4.10.2  yamt 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1420  1.4.10.2  yamt 		width = ri->ri_font->fontwidth * ncols;
   1421  1.4.10.2  yamt 		height = ri->ri_font->fontheight;
   1422  1.4.10.2  yamt 		uni_bitblit(sc, xs, y, xd, y, width, height);
   1423  1.4.10.2  yamt 	}
   1424  1.4.10.2  yamt 
   1425  1.4.10.2  yamt 	return;
   1426  1.4.10.2  yamt }
   1427  1.4.10.2  yamt 
   1428  1.4.10.2  yamt static void
   1429  1.4.10.2  yamt uni_copyrows(void *opaque, int srcrow, int dstrow, int nrows)
   1430  1.4.10.2  yamt {
   1431  1.4.10.2  yamt 	struct rasops_info *ri;
   1432  1.4.10.2  yamt 	struct vcons_screen *scr;
   1433  1.4.10.2  yamt 	struct unichromefb_softc *sc;
   1434  1.4.10.2  yamt 	int x, ys, yd, width, height;
   1435  1.4.10.2  yamt 
   1436  1.4.10.2  yamt 	ri = (struct rasops_info *)opaque;
   1437  1.4.10.2  yamt 	scr = (struct vcons_screen *)ri->ri_hw;
   1438  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)scr->scr_cookie;
   1439  1.4.10.2  yamt 
   1440  1.4.10.2  yamt 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
   1441  1.4.10.2  yamt 		x = ri->ri_xorigin;
   1442  1.4.10.2  yamt 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1443  1.4.10.2  yamt 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1444  1.4.10.2  yamt 		width = ri->ri_emuwidth;
   1445  1.4.10.2  yamt 		height = ri->ri_font->fontheight * nrows;
   1446  1.4.10.2  yamt 		uni_bitblit(sc, x, ys, x, yd, width, height);
   1447  1.4.10.2  yamt 	}
   1448  1.4.10.2  yamt 
   1449  1.4.10.2  yamt 	return;
   1450  1.4.10.2  yamt }
   1451  1.4.10.2  yamt 
   1452  1.4.10.2  yamt static void
   1453  1.4.10.2  yamt uni_erasecols(void *opaque, int row, int startcol, int ncols, long fillattr)
   1454  1.4.10.2  yamt {
   1455  1.4.10.2  yamt 	struct rasops_info *ri;
   1456  1.4.10.2  yamt 	struct vcons_screen *scr;
   1457  1.4.10.2  yamt 	struct unichromefb_softc *sc;
   1458  1.4.10.2  yamt 	int x, y, width, height, fg, bg, ul;
   1459  1.4.10.2  yamt 
   1460  1.4.10.2  yamt 	ri = (struct rasops_info *)opaque;
   1461  1.4.10.2  yamt 	scr = (struct vcons_screen *)ri->ri_hw;
   1462  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)scr->scr_cookie;
   1463  1.4.10.2  yamt 
   1464  1.4.10.2  yamt 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
   1465  1.4.10.2  yamt 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1466  1.4.10.2  yamt 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1467  1.4.10.2  yamt 		width = ri->ri_font->fontwidth * ncols;
   1468  1.4.10.2  yamt 		height = ri->ri_font->fontheight;
   1469  1.4.10.2  yamt 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1470  1.4.10.2  yamt 		uni_fillrect(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1471  1.4.10.2  yamt 	}
   1472  1.4.10.2  yamt 
   1473  1.4.10.2  yamt 	return;
   1474  1.4.10.2  yamt }
   1475  1.4.10.2  yamt 
   1476  1.4.10.2  yamt static void
   1477  1.4.10.2  yamt uni_eraserows(void *opaque, int row, int nrows, long fillattr)
   1478  1.4.10.2  yamt {
   1479  1.4.10.2  yamt 	struct rasops_info *ri;
   1480  1.4.10.2  yamt 	struct vcons_screen *scr;
   1481  1.4.10.2  yamt 	struct unichromefb_softc *sc;
   1482  1.4.10.2  yamt 	int x, y, width, height, fg, bg, ul;
   1483  1.4.10.2  yamt 
   1484  1.4.10.2  yamt 	ri = (struct rasops_info *)opaque;
   1485  1.4.10.2  yamt 	scr = (struct vcons_screen *)ri->ri_hw;
   1486  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)scr->scr_cookie;
   1487  1.4.10.2  yamt 
   1488  1.4.10.2  yamt 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
   1489  1.4.10.2  yamt 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1490  1.4.10.2  yamt 		if ((row == 0) && (nrows == ri->ri_rows)) {
   1491  1.4.10.2  yamt 			/* clear the whole screen */
   1492  1.4.10.2  yamt 			uni_fillrect(sc, 0, 0, ri->ri_width,
   1493  1.4.10.2  yamt 			    ri->ri_height, ri->ri_devcmap[bg]);
   1494  1.4.10.2  yamt 		} else {
   1495  1.4.10.2  yamt 			x = ri->ri_xorigin;
   1496  1.4.10.2  yamt 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1497  1.4.10.2  yamt 			width = ri->ri_emuwidth;
   1498  1.4.10.2  yamt 			height = ri->ri_font->fontheight * nrows;
   1499  1.4.10.2  yamt 			uni_fillrect(sc, x, y, width, height,
   1500  1.4.10.2  yamt 			    ri->ri_devcmap[bg]);
   1501  1.4.10.2  yamt 		}
   1502  1.4.10.2  yamt 	}
   1503  1.4.10.2  yamt 
   1504  1.4.10.2  yamt 	return;
   1505  1.4.10.2  yamt }
   1506  1.4.10.2  yamt 
   1507  1.4.10.2  yamt static void
   1508  1.4.10.2  yamt uni_cursor(void *opaque, int on, int row, int col)
   1509  1.4.10.2  yamt {
   1510  1.4.10.2  yamt 	struct rasops_info *ri;
   1511  1.4.10.2  yamt 	struct vcons_screen *scr;
   1512  1.4.10.2  yamt 	struct unichromefb_softc *sc;
   1513  1.4.10.2  yamt 	int x, y, wi, he;
   1514  1.4.10.2  yamt 
   1515  1.4.10.2  yamt 	ri = (struct rasops_info *)opaque;
   1516  1.4.10.2  yamt 	scr = (struct vcons_screen *)ri->ri_hw;
   1517  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)scr->scr_cookie;
   1518  1.4.10.2  yamt 
   1519  1.4.10.2  yamt 	uni_wait_idle(sc);
   1520  1.4.10.2  yamt 
   1521  1.4.10.2  yamt 	wi = ri->ri_font->fontwidth;
   1522  1.4.10.2  yamt 	he = ri->ri_font->fontheight;
   1523  1.4.10.2  yamt 
   1524  1.4.10.2  yamt 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
   1525  1.4.10.2  yamt 		x = ri->ri_ccol * wi + ri->ri_xorigin;
   1526  1.4.10.2  yamt 		y = ri->ri_crow * he + ri->ri_yorigin;
   1527  1.4.10.2  yamt 		if (ri->ri_flg & RI_CURSOR) {
   1528  1.4.10.2  yamt 			uni_rectinvert(sc, x, y, wi, he);
   1529  1.4.10.2  yamt 			ri->ri_flg &= ~RI_CURSOR;
   1530  1.4.10.2  yamt 		}
   1531  1.4.10.2  yamt 		ri->ri_crow = row;
   1532  1.4.10.2  yamt 		ri->ri_ccol = col;
   1533  1.4.10.2  yamt 		if (on) {
   1534  1.4.10.2  yamt 			x = ri->ri_ccol * wi + ri->ri_xorigin;
   1535  1.4.10.2  yamt 			y = ri->ri_crow * he + ri->ri_yorigin;
   1536  1.4.10.2  yamt 			uni_rectinvert(sc, x, y, wi, he);
   1537  1.4.10.2  yamt 			ri->ri_flg |= RI_CURSOR;
   1538  1.4.10.2  yamt 		}
   1539  1.4.10.2  yamt 	} else {
   1540  1.4.10.2  yamt 		ri->ri_flg &= ~RI_CURSOR;
   1541  1.4.10.2  yamt 		ri->ri_crow = row;
   1542  1.4.10.2  yamt 		ri->ri_ccol = col;
   1543  1.4.10.2  yamt 	}
   1544  1.4.10.2  yamt 
   1545  1.4.10.2  yamt 	return;
   1546  1.4.10.2  yamt }
   1547  1.4.10.2  yamt 
   1548  1.4.10.2  yamt static void
   1549  1.4.10.2  yamt uni_putchar(void *opaque, int row, int col, u_int c, long attr)
   1550  1.4.10.2  yamt {
   1551  1.4.10.2  yamt 	struct rasops_info *ri;
   1552  1.4.10.2  yamt 	struct vcons_screen *scr;
   1553  1.4.10.2  yamt 	struct unichromefb_softc *sc;
   1554  1.4.10.2  yamt 
   1555  1.4.10.2  yamt 	ri = (struct rasops_info *)opaque;
   1556  1.4.10.2  yamt 	scr = (struct vcons_screen *)ri->ri_hw;
   1557  1.4.10.2  yamt 	sc = (struct unichromefb_softc *)scr->scr_cookie;
   1558  1.4.10.2  yamt 
   1559  1.4.10.2  yamt 	if (sc->sc_wsmode == WSDISPLAYIO_MODE_EMUL) {
   1560  1.4.10.2  yamt 		uint32_t *data;
   1561  1.4.10.2  yamt 		int fg, bg, ul, uc, i;
   1562  1.4.10.2  yamt 		int x, y, wi, he;
   1563  1.4.10.2  yamt 
   1564  1.4.10.2  yamt 		wi = ri->ri_font->fontwidth;
   1565  1.4.10.2  yamt 		he = ri->ri_font->fontheight;
   1566  1.4.10.2  yamt 
   1567  1.4.10.2  yamt 		if (!CHAR_IN_FONT(c, ri->ri_font))
   1568  1.4.10.2  yamt 			return;
   1569  1.4.10.2  yamt 
   1570  1.4.10.2  yamt 		rasops_unpack_attr(attr, &fg, &bg, &ul);
   1571  1.4.10.2  yamt 		x = ri->ri_xorigin + col * wi;
   1572  1.4.10.2  yamt 		y = ri->ri_yorigin + row * he;
   1573  1.4.10.2  yamt 		if (c == 0x20)
   1574  1.4.10.2  yamt 			uni_fillrect(sc, x, y, wi, he, ri->ri_devcmap[bg]);
   1575  1.4.10.2  yamt 		else {
   1576  1.4.10.2  yamt 			uc = c - ri->ri_font->firstchar;
   1577  1.4.10.2  yamt 			data = (uint32_t *)((uint8_t *)ri->ri_font->data +
   1578  1.4.10.2  yamt 			    uc * ri->ri_fontscale);
   1579  1.4.10.2  yamt 			uni_setup_mono(sc, x, y, wi, he,
   1580  1.4.10.2  yamt 			    ri->ri_devcmap[fg], ri->ri_devcmap[bg]);
   1581  1.4.10.2  yamt 			for (i = 0; i < (wi * he) / 4; i++) {
   1582  1.4.10.2  yamt 				MMIO_OUT32(VIA_MMIO_BLTBASE, *data);
   1583  1.4.10.2  yamt 				data++;
   1584  1.4.10.2  yamt 			}
   1585  1.4.10.2  yamt 		}
   1586  1.4.10.2  yamt 	}
   1587  1.4.10.2  yamt 
   1588  1.4.10.2  yamt 	return;
   1589  1.4.10.2  yamt }
   1590