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