Home | History | Annotate | Line # | Download | only in pci
pm2fb.c revision 1.7.4.2
      1  1.7.4.2      yamt /*	$NetBSD: pm2fb.c,v 1.7.4.2 2012/10/30 17:21:51 yamt Exp $	*/
      2      1.1  macallan 
      3      1.1  macallan /*
      4  1.7.4.2      yamt  * Copyright (c) 2009, 2012 Michael Lorenz
      5      1.1  macallan  * All rights reserved.
      6      1.1  macallan  *
      7      1.1  macallan  * Redistribution and use in source and binary forms, with or without
      8      1.1  macallan  * modification, are permitted provided that the following conditions
      9      1.1  macallan  * are met:
     10      1.1  macallan  * 1. Redistributions of source code must retain the above copyright
     11      1.1  macallan  *    notice, this list of conditions and the following disclaimer.
     12      1.1  macallan  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  macallan  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  macallan  *    documentation and/or other materials provided with the distribution.
     15      1.1  macallan  *
     16      1.1  macallan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1  macallan  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1  macallan  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1  macallan  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1  macallan  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21      1.1  macallan  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22      1.1  macallan  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23      1.1  macallan  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24      1.1  macallan  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25      1.1  macallan  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26      1.1  macallan  */
     27      1.1  macallan 
     28      1.1  macallan /*
     29      1.1  macallan  * A console driver for Permedia 2 graphics controllers
     30      1.1  macallan  * tested on sparc64 only so far
     31      1.1  macallan  */
     32      1.1  macallan 
     33      1.1  macallan #include <sys/cdefs.h>
     34  1.7.4.2      yamt __KERNEL_RCSID(0, "$NetBSD: pm2fb.c,v 1.7.4.2 2012/10/30 17:21:51 yamt Exp $");
     35      1.1  macallan 
     36      1.1  macallan #include <sys/param.h>
     37      1.1  macallan #include <sys/systm.h>
     38      1.1  macallan #include <sys/kernel.h>
     39      1.1  macallan #include <sys/device.h>
     40      1.1  macallan #include <sys/malloc.h>
     41      1.1  macallan #include <sys/lwp.h>
     42      1.1  macallan #include <sys/kauth.h>
     43  1.7.4.2      yamt #include <sys/atomic.h>
     44      1.1  macallan 
     45      1.1  macallan #include <dev/videomode/videomode.h>
     46      1.1  macallan 
     47      1.1  macallan #include <dev/pci/pcivar.h>
     48      1.1  macallan #include <dev/pci/pcireg.h>
     49      1.1  macallan #include <dev/pci/pcidevs.h>
     50      1.1  macallan #include <dev/pci/pciio.h>
     51      1.1  macallan #include <dev/pci/pm2reg.h>
     52      1.1  macallan 
     53      1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     54      1.1  macallan #include <dev/wscons/wsconsio.h>
     55      1.1  macallan #include <dev/wsfont/wsfont.h>
     56      1.1  macallan #include <dev/rasops/rasops.h>
     57      1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     58  1.7.4.2      yamt #include <dev/wscons/wsdisplay_glyphcachevar.h>
     59      1.7    cegger #include <dev/pci/wsdisplay_pci.h>
     60      1.1  macallan 
     61      1.1  macallan #include <dev/i2c/i2cvar.h>
     62  1.7.4.1      yamt #include <dev/i2c/i2c_bitbang.h>
     63  1.7.4.1      yamt #include <dev/i2c/ddcvar.h>
     64  1.7.4.1      yamt #include <dev/videomode/videomode.h>
     65  1.7.4.1      yamt #include <dev/videomode/edidvar.h>
     66  1.7.4.1      yamt #include <dev/videomode/edidreg.h>
     67  1.7.4.1      yamt 
     68  1.7.4.1      yamt #include "opt_pm2fb.h"
     69  1.7.4.1      yamt 
     70  1.7.4.1      yamt #ifdef PM2FB_DEBUG
     71  1.7.4.1      yamt #define DPRINTF aprint_error
     72  1.7.4.1      yamt #else
     73  1.7.4.1      yamt #define DPRINTF while (0) printf
     74  1.7.4.1      yamt #endif
     75      1.1  macallan 
     76      1.1  macallan struct pm2fb_softc {
     77      1.1  macallan 	device_t sc_dev;
     78      1.1  macallan 
     79      1.1  macallan 	pci_chipset_tag_t sc_pc;
     80      1.1  macallan 	pcitag_t sc_pcitag;
     81      1.1  macallan 
     82      1.1  macallan 	bus_space_tag_t sc_memt;
     83      1.1  macallan 	bus_space_tag_t sc_iot;
     84      1.1  macallan 
     85      1.1  macallan 	bus_space_handle_t sc_regh;
     86      1.1  macallan 	bus_addr_t sc_fb, sc_reg;
     87      1.1  macallan 	bus_size_t sc_fbsize, sc_regsize;
     88      1.1  macallan 
     89      1.1  macallan 	int sc_width, sc_height, sc_depth, sc_stride;
     90      1.1  macallan 	int sc_locked;
     91      1.1  macallan 	struct vcons_screen sc_console_screen;
     92      1.1  macallan 	struct wsscreen_descr sc_defaultscreen_descr;
     93      1.1  macallan 	const struct wsscreen_descr *sc_screens[1];
     94      1.1  macallan 	struct wsscreen_list sc_screenlist;
     95      1.1  macallan 	struct vcons_data vd;
     96      1.1  macallan 	int sc_mode;
     97      1.1  macallan 	u_char sc_cmap_red[256];
     98      1.1  macallan 	u_char sc_cmap_green[256];
     99      1.1  macallan 	u_char sc_cmap_blue[256];
    100      1.1  macallan 	/* engine stuff */
    101      1.3  macallan 	uint32_t sc_pprod;
    102  1.7.4.2      yamt 	int sc_is_pm2;
    103  1.7.4.1      yamt 	/* i2c stuff */
    104  1.7.4.1      yamt 	struct i2c_controller sc_i2c;
    105  1.7.4.1      yamt 	uint8_t sc_edid_data[128];
    106  1.7.4.2      yamt 	struct edid_info sc_ei;
    107  1.7.4.2      yamt 	const struct videomode *sc_videomode;
    108  1.7.4.2      yamt 	glyphcache sc_gc;
    109      1.1  macallan };
    110      1.1  macallan 
    111      1.1  macallan static int	pm2fb_match(device_t, cfdata_t, void *);
    112      1.1  macallan static void	pm2fb_attach(device_t, device_t, void *);
    113      1.1  macallan 
    114      1.1  macallan CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
    115      1.1  macallan     pm2fb_match, pm2fb_attach, NULL, NULL);
    116      1.1  macallan 
    117      1.1  macallan extern const u_char rasops_cmap[768];
    118      1.1  macallan 
    119      1.1  macallan static int	pm2fb_ioctl(void *, void *, u_long, void *, int,
    120      1.1  macallan 			     struct lwp *);
    121      1.1  macallan static paddr_t	pm2fb_mmap(void *, void *, off_t, int);
    122      1.1  macallan static void	pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
    123      1.1  macallan 
    124      1.1  macallan static int	pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    125      1.1  macallan static int 	pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    126      1.1  macallan static void	pm2fb_restore_palette(struct pm2fb_softc *);
    127      1.1  macallan static int 	pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
    128      1.1  macallan 			    uint8_t, uint8_t);
    129      1.1  macallan 
    130      1.1  macallan static void	pm2fb_init(struct pm2fb_softc *);
    131  1.7.4.2      yamt static inline void pm2fb_wait(struct pm2fb_softc *, int);
    132      1.1  macallan static void	pm2fb_flush_engine(struct pm2fb_softc *);
    133      1.1  macallan static void	pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
    134      1.1  macallan 			    uint32_t);
    135  1.7.4.2      yamt static void	pm2fb_rectfill_a(void *, int, int, int, int, long);
    136  1.7.4.2      yamt static void	pm2fb_bitblt(void *, int, int, int, int, int, int, int);
    137      1.1  macallan 
    138      1.1  macallan static void	pm2fb_cursor(void *, int, int, int);
    139      1.1  macallan static void	pm2fb_putchar(void *, int, int, u_int, long);
    140  1.7.4.2      yamt static void	pm2fb_putchar_aa(void *, int, int, u_int, long);
    141      1.1  macallan static void	pm2fb_copycols(void *, int, int, int, int);
    142      1.1  macallan static void	pm2fb_erasecols(void *, int, int, int, long);
    143      1.1  macallan static void	pm2fb_copyrows(void *, int, int, int);
    144      1.1  macallan static void	pm2fb_eraserows(void *, int, int, long);
    145      1.1  macallan 
    146      1.1  macallan struct wsdisplay_accessops pm2fb_accessops = {
    147      1.1  macallan 	pm2fb_ioctl,
    148      1.1  macallan 	pm2fb_mmap,
    149      1.1  macallan 	NULL,	/* alloc_screen */
    150      1.1  macallan 	NULL,	/* free_screen */
    151      1.1  macallan 	NULL,	/* show_screen */
    152      1.1  macallan 	NULL, 	/* load_font */
    153      1.1  macallan 	NULL,	/* pollc */
    154      1.1  macallan 	NULL	/* scroll */
    155      1.1  macallan };
    156      1.1  macallan 
    157  1.7.4.1      yamt /* I2C glue */
    158  1.7.4.1      yamt static int pm2fb_i2c_acquire_bus(void *, int);
    159  1.7.4.1      yamt static void pm2fb_i2c_release_bus(void *, int);
    160  1.7.4.1      yamt static int pm2fb_i2c_send_start(void *, int);
    161  1.7.4.1      yamt static int pm2fb_i2c_send_stop(void *, int);
    162  1.7.4.1      yamt static int pm2fb_i2c_initiate_xfer(void *, i2c_addr_t, int);
    163  1.7.4.1      yamt static int pm2fb_i2c_read_byte(void *, uint8_t *, int);
    164  1.7.4.1      yamt static int pm2fb_i2c_write_byte(void *, uint8_t, int);
    165  1.7.4.1      yamt 
    166  1.7.4.1      yamt /* I2C bitbang glue */
    167  1.7.4.1      yamt static void pm2fb_i2cbb_set_bits(void *, uint32_t);
    168  1.7.4.1      yamt static void pm2fb_i2cbb_set_dir(void *, uint32_t);
    169  1.7.4.1      yamt static uint32_t pm2fb_i2cbb_read(void *);
    170  1.7.4.1      yamt 
    171  1.7.4.1      yamt static void pm2_setup_i2c(struct pm2fb_softc *);
    172  1.7.4.1      yamt 
    173  1.7.4.1      yamt static const struct i2c_bitbang_ops pm2fb_i2cbb_ops = {
    174  1.7.4.1      yamt 	pm2fb_i2cbb_set_bits,
    175  1.7.4.1      yamt 	pm2fb_i2cbb_set_dir,
    176  1.7.4.1      yamt 	pm2fb_i2cbb_read,
    177  1.7.4.1      yamt 	{
    178  1.7.4.1      yamt 		PM2_DD_SDA_IN,
    179  1.7.4.1      yamt 		PM2_DD_SCL_IN,
    180  1.7.4.1      yamt 		0,
    181  1.7.4.1      yamt 		0
    182  1.7.4.1      yamt 	}
    183  1.7.4.1      yamt };
    184  1.7.4.1      yamt 
    185  1.7.4.2      yamt /* mode setting stuff */
    186  1.7.4.2      yamt static int pm2fb_set_pll(struct pm2fb_softc *, int);
    187  1.7.4.2      yamt static uint8_t pm2fb_read_dac(struct pm2fb_softc *, int);
    188  1.7.4.2      yamt static void pm2fb_write_dac(struct pm2fb_softc *, int, uint8_t);
    189  1.7.4.2      yamt static void pm2fb_set_mode(struct pm2fb_softc *, const struct videomode *);
    190  1.7.4.2      yamt 
    191  1.7.4.2      yamt /* this table is from xf86-video-glint */
    192  1.7.4.2      yamt #define PARTPROD(a,b,c) (((a)<<6) | ((b)<<3) | (c))
    193  1.7.4.2      yamt int partprodPermedia[] = {
    194  1.7.4.2      yamt 	-1,
    195  1.7.4.2      yamt 	PARTPROD(0,0,1), PARTPROD(0,1,1), PARTPROD(1,1,1), PARTPROD(1,1,2),
    196  1.7.4.2      yamt 	PARTPROD(1,2,2), PARTPROD(2,2,2), PARTPROD(1,2,3), PARTPROD(2,2,3),
    197  1.7.4.2      yamt 	PARTPROD(1,3,3), PARTPROD(2,3,3), PARTPROD(1,2,4), PARTPROD(3,3,3),
    198  1.7.4.2      yamt 	PARTPROD(1,3,4), PARTPROD(2,3,4),              -1, PARTPROD(3,3,4),
    199  1.7.4.2      yamt 	PARTPROD(1,4,4), PARTPROD(2,4,4),              -1, PARTPROD(3,4,4),
    200  1.7.4.2      yamt 	             -1, PARTPROD(2,3,5),              -1, PARTPROD(4,4,4),
    201  1.7.4.2      yamt 	PARTPROD(1,4,5), PARTPROD(2,4,5), PARTPROD(3,4,5),              -1,
    202  1.7.4.2      yamt 	             -1,              -1,              -1, PARTPROD(4,4,5),
    203  1.7.4.2      yamt 	PARTPROD(1,5,5), PARTPROD(2,5,5),              -1, PARTPROD(3,5,5),
    204  1.7.4.2      yamt 	             -1,              -1,              -1, PARTPROD(4,5,5),
    205  1.7.4.2      yamt 	             -1,              -1,              -1, PARTPROD(3,4,6),
    206  1.7.4.2      yamt 	             -1,              -1,              -1, PARTPROD(5,5,5),
    207  1.7.4.2      yamt 	PARTPROD(1,5,6), PARTPROD(2,5,6),              -1, PARTPROD(3,5,6),
    208  1.7.4.2      yamt 	             -1,              -1,              -1, PARTPROD(4,5,6),
    209  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    210  1.7.4.2      yamt 	             -1,              -1,              -1, PARTPROD(5,5,6),
    211  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    212  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    213  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    214  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    215  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    216  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    217  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    218  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    219  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    220  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    221  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    222  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    223  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    224  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    225  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    226  1.7.4.2      yamt 	             -1,              -1,              -1,              -1,
    227  1.7.4.2      yamt 		     0};
    228  1.7.4.2      yamt 
    229      1.1  macallan static inline void
    230      1.1  macallan pm2fb_wait(struct pm2fb_softc *sc, int slots)
    231      1.1  macallan {
    232      1.1  macallan 	uint32_t reg;
    233      1.1  macallan 
    234      1.1  macallan 	do {
    235      1.1  macallan 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    236      1.1  macallan 			PM2_INPUT_FIFO_SPACE);
    237      1.1  macallan 	} while (reg <= slots);
    238      1.1  macallan }
    239      1.1  macallan 
    240      1.1  macallan static void
    241      1.1  macallan pm2fb_flush_engine(struct pm2fb_softc *sc)
    242      1.1  macallan {
    243      1.1  macallan 
    244      1.1  macallan 	pm2fb_wait(sc, 2);
    245      1.1  macallan 
    246      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
    247      1.1  macallan 	    PM2FLT_PASS_SYNC);
    248      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
    249      1.1  macallan 	do {
    250      1.1  macallan 		while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    251      1.1  macallan 			PM2_OUTPUT_FIFO_WORDS) == 0);
    252      1.1  macallan 	} while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
    253      1.1  macallan 	    0x188);
    254      1.1  macallan }
    255      1.1  macallan 
    256      1.1  macallan static int
    257      1.1  macallan pm2fb_match(device_t parent, cfdata_t match, void *aux)
    258      1.1  macallan {
    259      1.1  macallan 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    260      1.1  macallan 
    261      1.1  macallan 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    262      1.1  macallan 		return 0;
    263      1.1  macallan 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3DLABS)
    264      1.1  macallan 		return 0;
    265      1.1  macallan 
    266  1.7.4.2      yamt 	/*
    267  1.7.4.2      yamt 	 * only card tested on so far is a TechSource Raptor GFX 8P /
    268  1.7.4.2      yamt 	 * Sun PGX32, which happens to be a Permedia 2v
    269  1.7.4.2      yamt 	 */
    270  1.7.4.2      yamt 	if (/*(PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2) ||*/
    271      1.1  macallan 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2V))
    272      1.1  macallan 		return 100;
    273      1.1  macallan 	return (0);
    274      1.1  macallan }
    275      1.1  macallan 
    276      1.1  macallan static void
    277      1.1  macallan pm2fb_attach(device_t parent, device_t self, void *aux)
    278      1.1  macallan {
    279      1.1  macallan 	struct pm2fb_softc	*sc = device_private(self);
    280      1.1  macallan 	struct pci_attach_args	*pa = aux;
    281      1.1  macallan 	struct rasops_info	*ri;
    282      1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    283      1.1  macallan 	prop_dictionary_t	dict;
    284      1.1  macallan 	unsigned long		defattr;
    285      1.1  macallan 	bool			is_console;
    286  1.7.4.2      yamt 	int 			i, j;
    287  1.7.4.2      yamt 	uint32_t		flags;
    288  1.7.4.2      yamt 	uint8_t			cmap[768];
    289      1.1  macallan 
    290      1.1  macallan 	sc->sc_pc = pa->pa_pc;
    291      1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    292      1.1  macallan 	sc->sc_memt = pa->pa_memt;
    293      1.1  macallan 	sc->sc_iot = pa->pa_iot;
    294      1.1  macallan 	sc->sc_dev = self;
    295  1.7.4.2      yamt 	sc->sc_is_pm2 = (PCI_PRODUCT(pa->pa_id) ==
    296  1.7.4.2      yamt 	    PCI_PRODUCT_3DLABS_PERMEDIA2);
    297  1.7.4.1      yamt 	pci_aprint_devinfo(pa, NULL);
    298      1.1  macallan 
    299  1.7.4.2      yamt 	/*
    300  1.7.4.2      yamt 	 * fill in parameters from properties
    301  1.7.4.2      yamt 	 * if we can't get a usable mode via DDC2 we'll use this to pick one,
    302  1.7.4.2      yamt 	 * which is why we fill them in with some conservative values that
    303  1.7.4.2      yamt 	 * hopefully work as a last resort
    304  1.7.4.2      yamt 	 */
    305      1.1  macallan 	dict = device_properties(self);
    306      1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    307      1.1  macallan 		aprint_error("%s: no width property\n", device_xname(self));
    308  1.7.4.2      yamt 		sc->sc_width = 1024;
    309      1.1  macallan 	}
    310      1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    311      1.1  macallan 		aprint_error("%s: no height property\n", device_xname(self));
    312  1.7.4.2      yamt 		sc->sc_height = 768;
    313      1.1  macallan 	}
    314      1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    315      1.1  macallan 		aprint_error("%s: no depth property\n", device_xname(self));
    316  1.7.4.2      yamt 		sc->sc_depth = 8;
    317      1.1  macallan 	}
    318  1.7.4.2      yamt 
    319      1.1  macallan 	/*
    320      1.1  macallan 	 * don't look at the linebytes property - The Raptor firmware lies
    321      1.1  macallan 	 * about it. Get it from width * depth >> 3 instead.
    322      1.1  macallan 	 */
    323  1.7.4.2      yamt 
    324      1.1  macallan 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    325      1.1  macallan 
    326      1.1  macallan 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    327      1.1  macallan 
    328      1.3  macallan 	pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
    329      1.3  macallan 	    &sc->sc_fb, &sc->sc_fbsize, &flags);
    330      1.1  macallan 
    331      1.1  macallan 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    332      1.1  macallan 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    333      1.1  macallan 		aprint_error("%s: failed to map registers.\n",
    334      1.1  macallan 		    device_xname(sc->sc_dev));
    335      1.1  macallan 	}
    336      1.1  macallan 
    337      1.1  macallan 	/*
    338      1.1  macallan 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    339      1.1  macallan 	 * but as far as I know there are no PM2 with 64bit BARs
    340      1.1  macallan 	 */
    341      1.1  macallan 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    342      1.1  macallan 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    343      1.1  macallan 
    344      1.1  macallan 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    345      1.1  macallan 		"default",
    346      1.1  macallan 		0, 0,
    347      1.1  macallan 		NULL,
    348      1.1  macallan 		8, 16,
    349      1.1  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    350      1.1  macallan 		NULL
    351      1.1  macallan 	};
    352      1.1  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    353      1.1  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    354      1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    355      1.1  macallan 	sc->sc_locked = 0;
    356      1.1  macallan 
    357  1.7.4.1      yamt 	pm2_setup_i2c(sc);
    358  1.7.4.1      yamt 
    359      1.1  macallan 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    360      1.1  macallan 	    &pm2fb_accessops);
    361      1.1  macallan 	sc->vd.init_screen = pm2fb_init_screen;
    362  1.7.4.2      yamt 
    363      1.1  macallan 	/* init engine here */
    364      1.1  macallan 	pm2fb_init(sc);
    365      1.1  macallan 
    366      1.1  macallan 	ri = &sc->sc_console_screen.scr_ri;
    367      1.1  macallan 
    368  1.7.4.2      yamt 	sc->sc_gc.gc_bitblt = pm2fb_bitblt;
    369  1.7.4.2      yamt 	sc->sc_gc.gc_rectfill = pm2fb_rectfill_a;
    370  1.7.4.2      yamt 	sc->sc_gc.gc_blitcookie = sc;
    371  1.7.4.2      yamt 	sc->sc_gc.gc_rop = 3;
    372  1.7.4.2      yamt 
    373  1.7.4.2      yamt #ifdef PM2FB_DEBUG
    374  1.7.4.2      yamt 	/*
    375  1.7.4.2      yamt 	 * leave some room at the bottom of the screen for various blitter
    376  1.7.4.2      yamt 	 * tests and in order to make the glyph cache visible
    377  1.7.4.2      yamt 	 */
    378  1.7.4.2      yamt 	sc->sc_height -= 200;
    379  1.7.4.2      yamt #endif
    380      1.1  macallan 
    381      1.1  macallan 	if (is_console) {
    382      1.1  macallan 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    383      1.1  macallan 		    &defattr);
    384      1.1  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    385      1.1  macallan 
    386      1.1  macallan 		pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    387      1.1  macallan 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    388      1.1  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    389      1.1  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    390      1.1  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    391      1.1  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    392  1.7.4.2      yamt 
    393  1.7.4.2      yamt 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    394  1.7.4.2      yamt 			min(2047, (sc->sc_fbsize / sc->sc_stride))
    395  1.7.4.2      yamt 			 - sc->sc_height - 5,
    396  1.7.4.2      yamt 			sc->sc_width,
    397  1.7.4.2      yamt 			ri->ri_font->fontwidth,
    398  1.7.4.2      yamt 			ri->ri_font->fontheight,
    399  1.7.4.2      yamt 			defattr);
    400      1.1  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    401      1.1  macallan 		    defattr);
    402      1.1  macallan 		vcons_replay_msgbuf(&sc->sc_console_screen);
    403      1.1  macallan 	} else {
    404  1.7.4.2      yamt 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    405  1.7.4.2      yamt 			/* do some minimal setup to avoid weirdnesses later */
    406  1.7.4.2      yamt 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    407  1.7.4.2      yamt 			   &defattr);
    408  1.7.4.2      yamt 		}
    409  1.7.4.2      yamt 		glyphcache_init(&sc->sc_gc, sc->sc_height + 5,
    410  1.7.4.2      yamt 			   min(2047, (sc->sc_fbsize / sc->sc_stride))
    411  1.7.4.2      yamt 			    - sc->sc_height - 5,
    412  1.7.4.2      yamt 			   sc->sc_width,
    413  1.7.4.2      yamt 			   ri->ri_font->fontwidth,
    414  1.7.4.2      yamt 			   ri->ri_font->fontheight,
    415  1.7.4.2      yamt 			   defattr);
    416  1.7.4.2      yamt 	}
    417  1.7.4.2      yamt 
    418  1.7.4.2      yamt 	j = 0;
    419  1.7.4.2      yamt 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    420  1.7.4.2      yamt 	for (i = 0; i < 256; i++) {
    421  1.7.4.2      yamt 		sc->sc_cmap_red[i] = cmap[j];
    422  1.7.4.2      yamt 		sc->sc_cmap_green[i] = cmap[j + 1];
    423  1.7.4.2      yamt 		sc->sc_cmap_blue[i] = cmap[j + 2];
    424  1.7.4.2      yamt 		pm2fb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    425  1.7.4.2      yamt 		j += 3;
    426      1.1  macallan 	}
    427      1.1  macallan 
    428      1.1  macallan 	aa.console = is_console;
    429      1.1  macallan 	aa.scrdata = &sc->sc_screenlist;
    430      1.1  macallan 	aa.accessops = &pm2fb_accessops;
    431      1.1  macallan 	aa.accesscookie = &sc->vd;
    432      1.1  macallan 
    433  1.7.4.2      yamt 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    434  1.7.4.2      yamt 
    435  1.7.4.2      yamt #ifdef PM2FB_DEBUG
    436  1.7.4.2      yamt 	/*
    437  1.7.4.2      yamt 	 * draw a pattern to check if pm2fb_bitblt() gets the alignment stuff
    438  1.7.4.2      yamt 	 * right
    439  1.7.4.2      yamt 	 */
    440  1.7.4.2      yamt 	pm2fb_rectfill(sc, 0, sc->sc_height, sc->sc_width, 200, 0xffffffff);
    441  1.7.4.2      yamt 	pm2fb_rectfill(sc, 0, sc->sc_height, 300, 10, 0);
    442  1.7.4.2      yamt 	pm2fb_rectfill(sc, 10, sc->sc_height, 200, 10, 0xe0e0e0e0);
    443  1.7.4.2      yamt 	for (i = 1; i < 20; i++) {
    444  1.7.4.2      yamt 		pm2fb_bitblt(sc, 0, sc->sc_height,
    445  1.7.4.2      yamt 			i, sc->sc_height + 10 * i,
    446  1.7.4.2      yamt 			300, 10, 3);
    447  1.7.4.2      yamt 		pm2fb_bitblt(sc, i, sc->sc_height,
    448  1.7.4.2      yamt 			400, sc->sc_height + 10 * i,
    449  1.7.4.2      yamt 			300, 10, 3);
    450  1.7.4.2      yamt 	}
    451  1.7.4.2      yamt #endif
    452      1.1  macallan }
    453      1.1  macallan 
    454      1.1  macallan static int
    455      1.1  macallan pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    456      1.1  macallan 	struct lwp *l)
    457      1.1  macallan {
    458      1.1  macallan 	struct vcons_data *vd = v;
    459      1.1  macallan 	struct pm2fb_softc *sc = vd->cookie;
    460      1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    461      1.1  macallan 	struct vcons_screen *ms = vd->active;
    462      1.1  macallan 
    463      1.1  macallan 	switch (cmd) {
    464      1.6    cegger 	case WSDISPLAYIO_GTYPE:
    465      1.6    cegger 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    466      1.6    cegger 		return 0;
    467      1.6    cegger 
    468      1.6    cegger 	/* PCI config read/write passthrough. */
    469      1.6    cegger 	case PCI_IOC_CFGREAD:
    470      1.6    cegger 	case PCI_IOC_CFGWRITE:
    471      1.6    cegger 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    472      1.6    cegger 		    cmd, data, flag, l);
    473      1.6    cegger 
    474      1.7    cegger 	case WSDISPLAYIO_GET_BUSID:
    475      1.7    cegger 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    476      1.7    cegger 		    sc->sc_pcitag, data);
    477      1.7    cegger 
    478      1.6    cegger 	case WSDISPLAYIO_GINFO:
    479      1.6    cegger 		if (ms == NULL)
    480      1.6    cegger 			return ENODEV;
    481      1.6    cegger 		wdf = (void *)data;
    482      1.6    cegger 		wdf->height = ms->scr_ri.ri_height;
    483      1.6    cegger 		wdf->width = ms->scr_ri.ri_width;
    484      1.6    cegger 		wdf->depth = ms->scr_ri.ri_depth;
    485      1.6    cegger 		wdf->cmsize = 256;
    486      1.6    cegger 		return 0;
    487      1.6    cegger 
    488      1.6    cegger 	case WSDISPLAYIO_GETCMAP:
    489      1.6    cegger 		return pm2fb_getcmap(sc,
    490      1.6    cegger 		    (struct wsdisplay_cmap *)data);
    491      1.6    cegger 
    492      1.6    cegger 	case WSDISPLAYIO_PUTCMAP:
    493      1.6    cegger 		return pm2fb_putcmap(sc,
    494      1.6    cegger 		    (struct wsdisplay_cmap *)data);
    495      1.6    cegger 
    496      1.6    cegger 	case WSDISPLAYIO_LINEBYTES:
    497      1.6    cegger 		*(u_int *)data = sc->sc_stride;
    498      1.6    cegger 		return 0;
    499      1.1  macallan 
    500      1.6    cegger 	case WSDISPLAYIO_SMODE: {
    501      1.6    cegger 		int new_mode = *(int*)data;
    502      1.6    cegger 		if (new_mode != sc->sc_mode) {
    503      1.6    cegger 			sc->sc_mode = new_mode;
    504      1.6    cegger 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    505  1.7.4.2      yamt 				pm2fb_init(sc);
    506      1.6    cegger 				pm2fb_restore_palette(sc);
    507  1.7.4.2      yamt 				glyphcache_wipe(&sc->sc_gc);
    508      1.6    cegger 				vcons_redraw_screen(ms);
    509      1.6    cegger 			} else
    510      1.6    cegger 				pm2fb_flush_engine(sc);
    511      1.6    cegger 		}
    512      1.6    cegger 		}
    513      1.6    cegger 		return 0;
    514  1.7.4.1      yamt 	case WSDISPLAYIO_GET_EDID: {
    515  1.7.4.1      yamt 		struct wsdisplayio_edid_info *d = data;
    516  1.7.4.1      yamt 		d->data_size = 128;
    517  1.7.4.1      yamt 		if (d->buffer_size < 128)
    518  1.7.4.1      yamt 			return EAGAIN;
    519  1.7.4.1      yamt 		return copyout(sc->sc_edid_data, d->edid_data, 128);
    520  1.7.4.1      yamt 	}
    521      1.1  macallan 	}
    522      1.1  macallan 	return EPASSTHROUGH;
    523      1.1  macallan }
    524      1.1  macallan 
    525      1.1  macallan static paddr_t
    526      1.1  macallan pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
    527      1.1  macallan {
    528      1.1  macallan 	struct vcons_data *vd = v;
    529      1.1  macallan 	struct pm2fb_softc *sc = vd->cookie;
    530      1.1  macallan 	paddr_t pa;
    531      1.1  macallan 
    532      1.1  macallan 	/* 'regular' framebuffer mmap()ing */
    533      1.1  macallan 	if (offset < sc->sc_fbsize) {
    534      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    535      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    536      1.1  macallan 		return pa;
    537      1.1  macallan 	}
    538      1.1  macallan 
    539      1.1  macallan 	/*
    540      1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    541      1.1  macallan 	 * or the kernel itself
    542      1.1  macallan 	 */
    543  1.7.4.2      yamt 	if (kauth_authorize_machdep(kauth_cred_get(),
    544  1.7.4.2      yamt 	    KAUTH_MACHDEP_UNMANAGEDMEM,
    545  1.7.4.1      yamt 	    NULL, NULL, NULL, NULL) != 0) {
    546      1.1  macallan 		aprint_normal("%s: mmap() rejected.\n",
    547      1.1  macallan 		    device_xname(sc->sc_dev));
    548      1.1  macallan 		return -1;
    549      1.1  macallan 	}
    550      1.1  macallan 
    551      1.1  macallan 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    552      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    553      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    554      1.1  macallan 		return pa;
    555      1.1  macallan 	}
    556      1.1  macallan 
    557      1.1  macallan 	if ((offset >= sc->sc_reg) &&
    558      1.1  macallan 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    559      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    560      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    561      1.1  macallan 		return pa;
    562      1.1  macallan 	}
    563      1.1  macallan 
    564      1.1  macallan #ifdef PCI_MAGIC_IO_RANGE
    565      1.1  macallan 	/* allow mapping of IO space */
    566      1.1  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    567      1.1  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    568      1.1  macallan 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    569      1.1  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    570      1.1  macallan 		return pa;
    571      1.1  macallan 	}
    572      1.1  macallan #endif
    573      1.1  macallan 
    574      1.1  macallan 	return -1;
    575      1.1  macallan }
    576      1.1  macallan 
    577      1.1  macallan static void
    578      1.1  macallan pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
    579      1.1  macallan     int existing, long *defattr)
    580      1.1  macallan {
    581      1.1  macallan 	struct pm2fb_softc *sc = cookie;
    582      1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    583      1.1  macallan 
    584      1.1  macallan 	ri->ri_depth = sc->sc_depth;
    585      1.1  macallan 	ri->ri_width = sc->sc_width;
    586      1.1  macallan 	ri->ri_height = sc->sc_height;
    587      1.1  macallan 	ri->ri_stride = sc->sc_stride;
    588      1.3  macallan 	ri->ri_flg = RI_CENTER;
    589  1.7.4.2      yamt 	if (sc->sc_depth == 8)
    590  1.7.4.2      yamt 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    591      1.1  macallan 
    592  1.7.4.1      yamt 	rasops_init(ri, 0, 0);
    593  1.7.4.2      yamt 	ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_UNDERLINE;
    594      1.1  macallan 
    595      1.1  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    596      1.1  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    597      1.1  macallan 
    598      1.1  macallan 	ri->ri_hw = scr;
    599      1.1  macallan 	ri->ri_ops.copyrows = pm2fb_copyrows;
    600      1.1  macallan 	ri->ri_ops.copycols = pm2fb_copycols;
    601      1.1  macallan 	ri->ri_ops.cursor = pm2fb_cursor;
    602      1.1  macallan 	ri->ri_ops.eraserows = pm2fb_eraserows;
    603      1.1  macallan 	ri->ri_ops.erasecols = pm2fb_erasecols;
    604  1.7.4.2      yamt 	if (FONT_IS_ALPHA(ri->ri_font)) {
    605  1.7.4.2      yamt 		ri->ri_ops.putchar = pm2fb_putchar_aa;
    606  1.7.4.2      yamt 	} else
    607  1.7.4.2      yamt 		ri->ri_ops.putchar = pm2fb_putchar;
    608      1.1  macallan }
    609      1.1  macallan 
    610      1.1  macallan static int
    611      1.1  macallan pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    612      1.1  macallan {
    613      1.1  macallan 	u_char *r, *g, *b;
    614      1.1  macallan 	u_int index = cm->index;
    615      1.1  macallan 	u_int count = cm->count;
    616      1.1  macallan 	int i, error;
    617      1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    618      1.1  macallan 
    619      1.1  macallan #ifdef PM2FB_DEBUG
    620      1.1  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    621      1.1  macallan #endif
    622      1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    623      1.1  macallan 	    (cm->index + cm->count) > 256)
    624      1.1  macallan 		return EINVAL;
    625      1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    626      1.1  macallan 	if (error)
    627      1.1  macallan 		return error;
    628      1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    629      1.1  macallan 	if (error)
    630      1.1  macallan 		return error;
    631      1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    632      1.1  macallan 	if (error)
    633      1.1  macallan 		return error;
    634      1.1  macallan 
    635      1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    636      1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    637      1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    638      1.1  macallan 
    639      1.1  macallan 	r = &sc->sc_cmap_red[index];
    640      1.1  macallan 	g = &sc->sc_cmap_green[index];
    641      1.1  macallan 	b = &sc->sc_cmap_blue[index];
    642      1.1  macallan 
    643      1.1  macallan 	for (i = 0; i < count; i++) {
    644      1.1  macallan 		pm2fb_putpalreg(sc, index, *r, *g, *b);
    645      1.1  macallan 		index++;
    646      1.1  macallan 		r++, g++, b++;
    647      1.1  macallan 	}
    648      1.1  macallan 	return 0;
    649      1.1  macallan }
    650      1.1  macallan 
    651      1.1  macallan static int
    652      1.1  macallan pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    653      1.1  macallan {
    654      1.1  macallan 	u_int index = cm->index;
    655      1.1  macallan 	u_int count = cm->count;
    656      1.1  macallan 	int error;
    657      1.1  macallan 
    658      1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    659      1.1  macallan 		return EINVAL;
    660      1.1  macallan 
    661      1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    662      1.1  macallan 	if (error)
    663      1.1  macallan 		return error;
    664      1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    665      1.1  macallan 	if (error)
    666      1.1  macallan 		return error;
    667      1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    668      1.1  macallan 	if (error)
    669      1.1  macallan 		return error;
    670      1.1  macallan 
    671      1.1  macallan 	return 0;
    672      1.1  macallan }
    673      1.1  macallan 
    674      1.1  macallan static void
    675      1.1  macallan pm2fb_restore_palette(struct pm2fb_softc *sc)
    676      1.1  macallan {
    677      1.1  macallan 	int i;
    678      1.1  macallan 
    679      1.1  macallan 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    680      1.1  macallan 		pm2fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    681      1.1  macallan 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    682      1.1  macallan 	}
    683      1.1  macallan }
    684      1.1  macallan 
    685      1.1  macallan static int
    686      1.1  macallan pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    687      1.1  macallan     uint8_t b)
    688      1.1  macallan {
    689      1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
    690      1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
    691      1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
    692      1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
    693      1.1  macallan 	return 0;
    694      1.1  macallan }
    695      1.1  macallan 
    696  1.7.4.2      yamt static uint8_t
    697  1.7.4.2      yamt pm2fb_read_dac(struct pm2fb_softc *sc, int reg)
    698  1.7.4.2      yamt {
    699  1.7.4.2      yamt 	if (sc->sc_is_pm2) {
    700  1.7.4.2      yamt 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    701  1.7.4.2      yamt 		    PM2_DAC_PAL_WRITE_IDX, reg);
    702  1.7.4.2      yamt 		return bus_space_read_1(sc->sc_memt, sc->sc_regh,
    703  1.7.4.2      yamt 		    PM2_DAC_INDEX_DATA);
    704  1.7.4.2      yamt 	} else {
    705  1.7.4.2      yamt 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    706  1.7.4.2      yamt 		    PM2V_DAC_INDEX_LOW, reg & 0xff);
    707  1.7.4.2      yamt 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    708  1.7.4.2      yamt 		    PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
    709  1.7.4.2      yamt 		return bus_space_read_1(sc->sc_memt, sc->sc_regh,
    710  1.7.4.2      yamt 		    PM2V_DAC_INDEX_DATA);
    711  1.7.4.2      yamt 	}
    712  1.7.4.2      yamt }
    713  1.7.4.2      yamt 
    714  1.7.4.2      yamt static void
    715  1.7.4.2      yamt pm2fb_write_dac(struct pm2fb_softc *sc, int reg, uint8_t data)
    716  1.7.4.2      yamt {
    717  1.7.4.2      yamt 	pm2fb_wait(sc, 3);
    718  1.7.4.2      yamt 	if (sc->sc_is_pm2) {
    719  1.7.4.2      yamt 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    720  1.7.4.2      yamt 		    PM2_DAC_PAL_WRITE_IDX, reg);
    721  1.7.4.2      yamt 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    722  1.7.4.2      yamt 		    PM2_DAC_INDEX_DATA, data);
    723  1.7.4.2      yamt 	} else {
    724  1.7.4.2      yamt 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    725  1.7.4.2      yamt 		    PM2V_DAC_INDEX_LOW, reg & 0xff);
    726  1.7.4.2      yamt 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    727  1.7.4.2      yamt 		    PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
    728  1.7.4.2      yamt 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    729  1.7.4.2      yamt 		    PM2V_DAC_INDEX_DATA, data);
    730  1.7.4.2      yamt 	}
    731  1.7.4.2      yamt }
    732  1.7.4.2      yamt 
    733      1.1  macallan static void
    734      1.1  macallan pm2fb_init(struct pm2fb_softc *sc)
    735      1.1  macallan {
    736      1.1  macallan 	pm2fb_flush_engine(sc);
    737      1.1  macallan 
    738      1.1  macallan 	pm2fb_wait(sc, 8);
    739      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
    740      1.1  macallan #if 0
    741      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
    742      1.1  macallan 		0xffffffff);
    743      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
    744      1.1  macallan 		0xffffffff);
    745      1.3  macallan #endif
    746      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
    747      1.3  macallan 		0xffffffff);
    748      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
    749      1.3  macallan 		0xffffffff);
    750      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
    751      1.3  macallan 		PM2WM_WRITE_EN);
    752      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
    753      1.3  macallan 	    (sc->sc_height << 16) | sc->sc_width);
    754      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
    755      1.3  macallan 	    PM2SC_SCREEN_EN);
    756      1.3  macallan 	pm2fb_wait(sc, 8);
    757      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
    758      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
    759      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    760      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
    761      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
    762      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
    763      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
    764      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
    765      1.3  macallan 	pm2fb_wait(sc, 8);
    766      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
    767      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
    768      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
    769      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
    770      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
    771      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
    772  1.7.4.2      yamt #if 0
    773      1.3  macallan 	sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    774      1.3  macallan 	    PM2_FB_READMODE) &
    775      1.3  macallan 	    (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
    776  1.7.4.2      yamt #endif
    777  1.7.4.2      yamt 	sc->sc_pprod = partprodPermedia[sc->sc_stride >> 5];
    778  1.7.4.2      yamt 
    779      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
    780      1.3  macallan 	    sc->sc_pprod);
    781      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
    782      1.3  macallan 	    sc->sc_pprod);
    783  1.7.4.2      yamt 	pm2fb_wait(sc, 9);
    784      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
    785      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
    786      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
    787      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
    788      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
    789      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
    790      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
    791      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
    792      1.3  macallan 	    0x0fff0fff);
    793  1.7.4.2      yamt 	/*
    794  1.7.4.2      yamt 	 * another scissor we need to disable in order to blit into off-screen
    795  1.7.4.2      yamt 	 * memory
    796  1.7.4.2      yamt 	 */
    797  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
    798  1.7.4.2      yamt 	    0x0fff0fff);
    799  1.7.4.2      yamt 
    800  1.7.4.2      yamt 	switch(sc->sc_depth) {
    801  1.7.4.2      yamt 		case 8:
    802  1.7.4.2      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    803  1.7.4.2      yamt 			    PM2_RE_PIXEL_SIZE, PM2PS_8BIT);
    804  1.7.4.2      yamt 			break;
    805  1.7.4.2      yamt 		case 16:
    806  1.7.4.2      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    807  1.7.4.2      yamt 			    PM2_RE_PIXEL_SIZE, PM2PS_16BIT);
    808  1.7.4.2      yamt 			break;
    809  1.7.4.2      yamt 		case 32:
    810  1.7.4.2      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    811  1.7.4.2      yamt 			    PM2_RE_PIXEL_SIZE, PM2PS_32BIT);
    812  1.7.4.2      yamt 			break;
    813  1.7.4.2      yamt 	}
    814      1.1  macallan 	pm2fb_flush_engine(sc);
    815  1.7.4.2      yamt 	DPRINTF("pixel size: %08x\n",
    816  1.7.4.2      yamt 	    bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_RE_PIXEL_SIZE));
    817      1.1  macallan }
    818      1.1  macallan 
    819      1.1  macallan static void
    820      1.1  macallan pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
    821      1.1  macallan      uint32_t colour)
    822      1.1  macallan {
    823      1.1  macallan 
    824      1.3  macallan 	pm2fb_wait(sc, 7);
    825      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    826      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    827      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    828      1.1  macallan 	    PM2RECFG_WRITE_EN);
    829      1.2  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
    830      1.1  macallan 	    colour);
    831      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    832      1.1  macallan 	    (y << 16) | x);
    833      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    834      1.1  macallan 	    (he << 16) | wi);
    835      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    836      1.2  macallan 	    PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    837      1.1  macallan }
    838      1.1  macallan 
    839      1.1  macallan static void
    840  1.7.4.2      yamt pm2fb_rectfill_a(void *cookie, int x, int y, int wi, int he, long attr)
    841  1.7.4.2      yamt {
    842  1.7.4.2      yamt 	struct pm2fb_softc *sc = cookie;
    843  1.7.4.2      yamt 
    844  1.7.4.2      yamt 	pm2fb_rectfill(sc, x, y, wi, he,
    845  1.7.4.2      yamt 	    sc->vd.active->scr_ri.ri_devcmap[(attr >> 24 & 0xf)]);
    846  1.7.4.2      yamt }
    847  1.7.4.2      yamt 
    848  1.7.4.2      yamt static void
    849  1.7.4.2      yamt pm2fb_bitblt(void *cookie, int xs, int ys, int xd, int yd,
    850      1.1  macallan     int wi, int he, int rop)
    851      1.1  macallan {
    852  1.7.4.2      yamt 	struct pm2fb_softc *sc = cookie;
    853      1.1  macallan 	uint32_t dir = 0;
    854  1.7.4.2      yamt 	int rxs, rxd, rwi, rxdelta;
    855      1.1  macallan 
    856      1.1  macallan 	if (yd <= ys) {
    857      1.1  macallan 		dir |= PM2RE_INC_Y;
    858      1.1  macallan 	}
    859      1.1  macallan 	if (xd <= xs) {
    860      1.1  macallan 		dir |= PM2RE_INC_X;
    861      1.1  macallan 	}
    862  1.7.4.2      yamt 	pm2fb_wait(sc, 8);
    863      1.2  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    864      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    865  1.7.4.2      yamt 	if (sc->sc_depth == 8) {
    866  1.7.4.2      yamt 		int adjust;
    867  1.7.4.2      yamt 		/*
    868  1.7.4.2      yamt 		 * use packed mode for some extra speed
    869  1.7.4.2      yamt 		 * this copies 32bit quantities even in 8 bit mode, so we need
    870  1.7.4.2      yamt 		 * to adjust for cases where the lower two bits in source and
    871  1.7.4.2      yamt 		 * destination X don't align, and/or where the width isn't a
    872  1.7.4.2      yamt 		 * multiple of 4
    873  1.7.4.2      yamt 		 */
    874  1.7.4.2      yamt 		if (rop == 3) {
    875  1.7.4.2      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    876  1.7.4.2      yamt 			    PM2_RE_CONFIG,
    877  1.7.4.2      yamt 			    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
    878  1.7.4.2      yamt 			    PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
    879  1.7.4.2      yamt 		} else {
    880  1.7.4.2      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    881  1.7.4.2      yamt 			    PM2_RE_CONFIG,
    882  1.7.4.2      yamt 			    PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
    883  1.7.4.2      yamt 			    PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
    884  1.7.4.2      yamt 			    PM2RECFG_ROP_EN | (rop << 6));
    885  1.7.4.2      yamt 		}
    886  1.7.4.2      yamt 		rxs = xs >> 2;
    887  1.7.4.2      yamt 		rxd = xd >> 2;
    888  1.7.4.2      yamt 		rwi = (wi + 7) >> 2;
    889  1.7.4.2      yamt 		rxdelta = (xs & 0xffc) - (xd & 0xffc);
    890  1.7.4.2      yamt 		/* adjust for non-aligned x */
    891  1.7.4.2      yamt 		adjust = ((xd & 3) - (xs & 3));
    892  1.7.4.2      yamt 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
    893  1.7.4.2      yamt 		    PM2_RE_PACKEDDATA_LIMIT,
    894  1.7.4.2      yamt 		    (xd << 16) | (xd + wi) | (adjust << 29));
    895  1.7.4.2      yamt 
    896      1.3  macallan 	} else {
    897  1.7.4.2      yamt 		/* we're in 16 or 32bit mode */
    898  1.7.4.2      yamt 		if (rop == 3) {
    899  1.7.4.2      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    900  1.7.4.2      yamt 			    PM2_RE_CONFIG,
    901  1.7.4.2      yamt 			    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
    902  1.7.4.2      yamt 			    PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
    903  1.7.4.2      yamt 		} else {
    904  1.7.4.2      yamt 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    905  1.7.4.2      yamt 			    PM2_RE_CONFIG,
    906  1.7.4.2      yamt 			    PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
    907  1.7.4.2      yamt 			    PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
    908  1.7.4.2      yamt 			    PM2RECFG_ROP_EN | (rop << 6));
    909  1.7.4.2      yamt 		}
    910  1.7.4.2      yamt 		rxs = xs;
    911  1.7.4.2      yamt 		rxd = xd;
    912  1.7.4.2      yamt 		rwi = wi;
    913  1.7.4.2      yamt 		rxdelta = xs - xd;
    914  1.7.4.2      yamt 	}
    915      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    916  1.7.4.2      yamt 	    (yd << 16) | rxd);
    917      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    918  1.7.4.2      yamt 	    (he << 16) | rwi);
    919      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
    920  1.7.4.2      yamt 	    (((ys - yd) & 0xfff) << 16) | (rxdelta & 0xfff));
    921      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    922      1.1  macallan 	    PM2RE_RECTANGLE | dir);
    923      1.1  macallan }
    924      1.1  macallan 
    925      1.1  macallan static void
    926      1.1  macallan pm2fb_cursor(void *cookie, int on, int row, int col)
    927      1.1  macallan {
    928      1.1  macallan 	struct rasops_info *ri = cookie;
    929      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    930      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
    931      1.1  macallan 	int x, y, wi, he;
    932      1.1  macallan 
    933      1.1  macallan 	wi = ri->ri_font->fontwidth;
    934      1.1  macallan 	he = ri->ri_font->fontheight;
    935      1.1  macallan 
    936      1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    937      1.1  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    938      1.1  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
    939      1.1  macallan 		if (ri->ri_flg & RI_CURSOR) {
    940      1.1  macallan 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    941      1.1  macallan 			ri->ri_flg &= ~RI_CURSOR;
    942      1.1  macallan 		}
    943      1.1  macallan 		ri->ri_crow = row;
    944      1.1  macallan 		ri->ri_ccol = col;
    945      1.1  macallan 		if (on) {
    946      1.1  macallan 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    947      1.1  macallan 			y = ri->ri_crow * he + ri->ri_yorigin;
    948      1.1  macallan 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    949      1.1  macallan 			ri->ri_flg |= RI_CURSOR;
    950      1.1  macallan 		}
    951      1.1  macallan 	} else {
    952      1.1  macallan 		scr->scr_ri.ri_crow = row;
    953      1.1  macallan 		scr->scr_ri.ri_ccol = col;
    954      1.1  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    955      1.1  macallan 	}
    956      1.1  macallan 
    957      1.1  macallan }
    958      1.1  macallan 
    959      1.1  macallan static void
    960      1.1  macallan pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    961      1.1  macallan {
    962      1.3  macallan 	struct rasops_info *ri = cookie;
    963      1.4  macallan 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    964      1.3  macallan 	struct vcons_screen *scr = ri->ri_hw;
    965      1.3  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
    966      1.3  macallan 	uint32_t mode;
    967      1.3  macallan 
    968      1.3  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    969      1.3  macallan 		void *data;
    970      1.3  macallan 		uint32_t fg, bg;
    971      1.3  macallan 		int uc, i;
    972      1.3  macallan 		int x, y, wi, he;
    973      1.3  macallan 
    974      1.4  macallan 		wi = font->fontwidth;
    975      1.4  macallan 		he = font->fontheight;
    976      1.3  macallan 
    977      1.4  macallan 		if (!CHAR_IN_FONT(c, font))
    978      1.3  macallan 			return;
    979      1.3  macallan 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    980      1.3  macallan 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    981      1.3  macallan 		x = ri->ri_xorigin + col * wi;
    982      1.3  macallan 		y = ri->ri_yorigin + row * he;
    983      1.3  macallan 		if (c == 0x20) {
    984      1.3  macallan 			pm2fb_rectfill(sc, x, y, wi, he, bg);
    985      1.3  macallan 		} else {
    986      1.4  macallan 			uc = c - font->firstchar;
    987      1.4  macallan 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
    988      1.3  macallan 
    989      1.3  macallan 			mode = PM2RM_MASK_MIRROR;
    990      1.3  macallan 			switch (ri->ri_font->stride) {
    991      1.3  macallan 				case 1:
    992      1.3  macallan 					mode |= 3 << 7;
    993      1.3  macallan 					break;
    994      1.3  macallan 				case 2:
    995      1.3  macallan 					mode |= 2 << 7;
    996      1.3  macallan 					break;
    997      1.3  macallan 			}
    998      1.3  macallan 
    999      1.3  macallan 			pm2fb_wait(sc, 8);
   1000      1.3  macallan 
   1001      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1002      1.3  macallan 			    PM2_RE_MODE, mode);
   1003      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1004      1.3  macallan 			    PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
   1005      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1006      1.3  macallan 			    PM2_RE_BLOCK_COLOUR, bg);
   1007      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1008      1.3  macallan 			    PM2_RE_RECT_START, (y << 16) | x);
   1009      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1010      1.3  macallan 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
   1011      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1012      1.3  macallan 			    PM2_RE_RENDER,
   1013      1.3  macallan 			    PM2RE_RECTANGLE |
   1014      1.3  macallan 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
   1015      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1016      1.3  macallan 			    PM2_RE_BLOCK_COLOUR, fg);
   1017      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1018      1.3  macallan 			    PM2_RE_RENDER,
   1019      1.3  macallan 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
   1020      1.3  macallan 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
   1021      1.3  macallan 
   1022      1.3  macallan 			pm2fb_wait(sc, he);
   1023      1.3  macallan 			switch (ri->ri_font->stride) {
   1024      1.3  macallan 			case 1: {
   1025      1.3  macallan 				uint8_t *data8 = data;
   1026      1.3  macallan 				uint32_t reg;
   1027      1.3  macallan 				for (i = 0; i < he; i++) {
   1028      1.3  macallan 					reg = *data8;
   1029      1.3  macallan 					bus_space_write_4(sc->sc_memt,
   1030      1.3  macallan 					    sc->sc_regh,
   1031      1.3  macallan 					    PM2_RE_BITMASK, reg);
   1032      1.3  macallan 					data8++;
   1033      1.3  macallan 				}
   1034      1.3  macallan 				break;
   1035      1.3  macallan 				}
   1036      1.3  macallan 			case 2: {
   1037      1.3  macallan 				uint16_t *data16 = data;
   1038      1.3  macallan 				uint32_t reg;
   1039      1.3  macallan 				for (i = 0; i < he; i++) {
   1040      1.3  macallan 					reg = *data16;
   1041      1.3  macallan 					bus_space_write_4(sc->sc_memt,
   1042      1.3  macallan 					    sc->sc_regh,
   1043      1.3  macallan 					    PM2_RE_BITMASK, reg);
   1044      1.3  macallan 					data16++;
   1045      1.3  macallan 				}
   1046      1.3  macallan 				break;
   1047      1.3  macallan 			}
   1048      1.3  macallan 			}
   1049      1.3  macallan 		}
   1050  1.7.4.2      yamt 		if (attr & 1)
   1051  1.7.4.2      yamt 			pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
   1052      1.3  macallan 	}
   1053      1.1  macallan }
   1054      1.1  macallan 
   1055      1.1  macallan static void
   1056  1.7.4.2      yamt pm2fb_putchar_aa(void *cookie, int row, int col, u_int c, long attr)
   1057  1.7.4.2      yamt {
   1058  1.7.4.2      yamt 	struct rasops_info *ri = cookie;
   1059  1.7.4.2      yamt 	struct wsdisplay_font *font = PICK_FONT(ri, c);
   1060  1.7.4.2      yamt 	struct vcons_screen *scr = ri->ri_hw;
   1061  1.7.4.2      yamt 	struct pm2fb_softc *sc = scr->scr_cookie;
   1062  1.7.4.2      yamt 	uint32_t bg, fg, /*latch = 0,*/ bg8, fg8, pixel;
   1063  1.7.4.2      yamt 	int i, x, y, wi, he, r, g, b, aval;
   1064  1.7.4.2      yamt 	int r1, g1, b1, r0, g0, b0, fgo, bgo;
   1065  1.7.4.2      yamt 	uint8_t *data8;
   1066  1.7.4.2      yamt 	int rv = GC_NOPE, cnt = 0;
   1067  1.7.4.2      yamt 
   1068  1.7.4.2      yamt 	if (sc->sc_mode != WSDISPLAYIO_MODE_EMUL)
   1069  1.7.4.2      yamt 		return;
   1070  1.7.4.2      yamt 
   1071  1.7.4.2      yamt 	if (!CHAR_IN_FONT(c, font))
   1072  1.7.4.2      yamt 		return;
   1073  1.7.4.2      yamt 
   1074  1.7.4.2      yamt 	wi = font->fontwidth;
   1075  1.7.4.2      yamt 	he = font->fontheight;
   1076  1.7.4.2      yamt 
   1077  1.7.4.2      yamt 	bg = ri->ri_devcmap[(attr >> 16) & 0xf];
   1078  1.7.4.2      yamt 	fg = ri->ri_devcmap[(attr >> 24) & 0xf];
   1079  1.7.4.2      yamt 	x = ri->ri_xorigin + col * wi;
   1080  1.7.4.2      yamt 	y = ri->ri_yorigin + row * he;
   1081  1.7.4.2      yamt 	if (c == 0x20) {
   1082  1.7.4.2      yamt 		pm2fb_rectfill(sc, x, y, wi, he, bg);
   1083  1.7.4.2      yamt 		if (attr & 1)
   1084  1.7.4.2      yamt 			pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
   1085  1.7.4.2      yamt 		return;
   1086  1.7.4.2      yamt 	}
   1087  1.7.4.2      yamt 
   1088  1.7.4.2      yamt 	rv = glyphcache_try(&sc->sc_gc, c, x, y, attr);
   1089  1.7.4.2      yamt 	if (rv == GC_OK)
   1090  1.7.4.2      yamt 		return;
   1091  1.7.4.2      yamt 
   1092  1.7.4.2      yamt 	data8 = WSFONT_GLYPH(c, font);
   1093  1.7.4.2      yamt 
   1094  1.7.4.2      yamt 	pm2fb_wait(sc, 5);
   1095  1.7.4.2      yamt #if 0
   1096  1.7.4.2      yamt 	/*
   1097  1.7.4.2      yamt 	 * TODO:
   1098  1.7.4.2      yamt 	 * - use packed mode here as well, instead of writing each pixel separately
   1099  1.7.4.2      yamt 	 * - see if we can trick the chip into doing the alpha blending for us
   1100  1.7.4.2      yamt 	 */
   1101  1.7.4.2      yamt 	x = x >> 2;
   1102  1.7.4.2      yamt 	wi = (wi + 3) >> 2;
   1103  1.7.4.2      yamt #endif
   1104  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
   1105  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
   1106  1.7.4.2      yamt 			    PM2RECFG_WRITE_EN /*| PM2RECFG_PACKED*/);
   1107  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1108  1.7.4.2      yamt 			    PM2_RE_RECT_START, (y << 16) | x);
   1109  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1110  1.7.4.2      yamt 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
   1111  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1112  1.7.4.2      yamt 			    PM2_RE_RENDER,
   1113  1.7.4.2      yamt 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_HOST |
   1114  1.7.4.2      yamt 			    PM2RE_INC_X | PM2RE_INC_Y);
   1115  1.7.4.2      yamt 	/*
   1116  1.7.4.2      yamt 	 * we need the RGB colours here, so get offsets into rasops_cmap
   1117  1.7.4.2      yamt 	 */
   1118  1.7.4.2      yamt 	fgo = ((attr >> 24) & 0xf) * 3;
   1119  1.7.4.2      yamt 	bgo = ((attr >> 16) & 0xf) * 3;
   1120  1.7.4.2      yamt 
   1121  1.7.4.2      yamt 	r0 = rasops_cmap[bgo];
   1122  1.7.4.2      yamt 	r1 = rasops_cmap[fgo];
   1123  1.7.4.2      yamt 	g0 = rasops_cmap[bgo + 1];
   1124  1.7.4.2      yamt 	g1 = rasops_cmap[fgo + 1];
   1125  1.7.4.2      yamt 	b0 = rasops_cmap[bgo + 2];
   1126  1.7.4.2      yamt 	b1 = rasops_cmap[fgo + 2];
   1127  1.7.4.2      yamt #define R3G3B2(r, g, b) ((r & 0xe0) | ((g >> 3) & 0x1c) | (b >> 6))
   1128  1.7.4.2      yamt 	bg8 = R3G3B2(r0, g0, b0);
   1129  1.7.4.2      yamt 	fg8 = R3G3B2(r1, g1, b1);
   1130  1.7.4.2      yamt 
   1131  1.7.4.2      yamt 	pm2fb_wait(sc, 200);
   1132  1.7.4.2      yamt 
   1133  1.7.4.2      yamt 	for (i = 0; i < ri->ri_fontscale; i++) {
   1134  1.7.4.2      yamt 		aval = *data8;
   1135  1.7.4.2      yamt 		if (aval == 0) {
   1136  1.7.4.2      yamt 			pixel = bg8;
   1137  1.7.4.2      yamt 		} else if (aval == 255) {
   1138  1.7.4.2      yamt 			pixel = fg8;
   1139  1.7.4.2      yamt 		} else {
   1140  1.7.4.2      yamt 			r = aval * r1 + (255 - aval) * r0;
   1141  1.7.4.2      yamt 			g = aval * g1 + (255 - aval) * g0;
   1142  1.7.4.2      yamt 			b = aval * b1 + (255 - aval) * b0;
   1143  1.7.4.2      yamt 			pixel = ((r & 0xe000) >> 8) |
   1144  1.7.4.2      yamt 				((g & 0xe000) >> 11) |
   1145  1.7.4.2      yamt 				((b & 0xc000) >> 14);
   1146  1.7.4.2      yamt 		}
   1147  1.7.4.2      yamt #if 0
   1148  1.7.4.2      yamt 		latch = (latch << 8) | pixel;
   1149  1.7.4.2      yamt 		/* write in 32bit chunks */
   1150  1.7.4.2      yamt 		if ((i & 3) == 3) {
   1151  1.7.4.2      yamt 			bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1152  1.7.4.2      yamt 			    PM2_RE_DATA, latch);
   1153  1.7.4.2      yamt 			/*
   1154  1.7.4.2      yamt 			 * not strictly necessary, old data should be shifted
   1155  1.7.4.2      yamt 			 * out
   1156  1.7.4.2      yamt 			 */
   1157  1.7.4.2      yamt 			latch = 0;
   1158  1.7.4.2      yamt 			cnt++;
   1159  1.7.4.2      yamt 			if (cnt > 190) {
   1160  1.7.4.2      yamt 				pm2fb_wait(sc, 200);
   1161  1.7.4.2      yamt 				cnt = 0;
   1162  1.7.4.2      yamt 			}
   1163  1.7.4.2      yamt 		}
   1164  1.7.4.2      yamt #else
   1165  1.7.4.2      yamt 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
   1166  1.7.4.2      yamt 			    PM2_RE_COLOUR, pixel);
   1167  1.7.4.2      yamt 
   1168  1.7.4.2      yamt 		if (cnt > 190) {
   1169  1.7.4.2      yamt 			pm2fb_wait(sc, 200);
   1170  1.7.4.2      yamt 			cnt = 0;
   1171  1.7.4.2      yamt 		}
   1172  1.7.4.2      yamt #endif
   1173  1.7.4.2      yamt 		data8++;
   1174  1.7.4.2      yamt 	}
   1175  1.7.4.2      yamt #if 0
   1176  1.7.4.2      yamt 	/* if we have pixels left in latch write them out */
   1177  1.7.4.2      yamt 	if ((i & 3) != 0) {
   1178  1.7.4.2      yamt 		latch = latch << ((4 - (i & 3)) << 3);
   1179  1.7.4.2      yamt 		bus_space_write_stream_4(sc->sc_memt, sc->sc_regh,
   1180  1.7.4.2      yamt 				    PM2_RE_DATA, latch);
   1181  1.7.4.2      yamt 	}
   1182  1.7.4.2      yamt #endif
   1183  1.7.4.2      yamt 	/*
   1184  1.7.4.2      yamt 	 * XXX
   1185  1.7.4.2      yamt 	 * occasionally characters end up in the cache only partially drawn
   1186  1.7.4.2      yamt 	 * apparently the blitter might end up grabbing them before they're
   1187  1.7.4.2      yamt 	 * completely flushed out into video memory
   1188  1.7.4.2      yamt 	 * so we let the pipeline drain a little bit before continuing
   1189  1.7.4.2      yamt 	 */
   1190  1.7.4.2      yamt 	pm2fb_wait(sc, 20);
   1191  1.7.4.2      yamt 
   1192  1.7.4.2      yamt 	if (rv == GC_ADD) {
   1193  1.7.4.2      yamt 		glyphcache_add(&sc->sc_gc, c, x, y);
   1194  1.7.4.2      yamt 	} else if (attr & 1)
   1195  1.7.4.2      yamt 		pm2fb_rectfill(sc, x, y + he - 2, wi, 1, fg);
   1196  1.7.4.2      yamt }
   1197  1.7.4.2      yamt 
   1198  1.7.4.2      yamt static void
   1199      1.1  macallan pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
   1200      1.1  macallan {
   1201      1.1  macallan 	struct rasops_info *ri = cookie;
   1202      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1203      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
   1204      1.1  macallan 	int32_t xs, xd, y, width, height;
   1205      1.1  macallan 
   1206      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1207      1.1  macallan 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
   1208      1.1  macallan 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
   1209      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1210      1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
   1211      1.1  macallan 		height = ri->ri_font->fontheight;
   1212      1.1  macallan 		pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
   1213      1.1  macallan 	}
   1214      1.1  macallan }
   1215      1.1  macallan 
   1216      1.1  macallan static void
   1217      1.1  macallan pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
   1218      1.1  macallan {
   1219      1.1  macallan 	struct rasops_info *ri = cookie;
   1220      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1221      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
   1222      1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
   1223      1.1  macallan 
   1224      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1225      1.1  macallan 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
   1226      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1227      1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
   1228      1.1  macallan 		height = ri->ri_font->fontheight;
   1229      1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1230      1.1  macallan 
   1231      1.1  macallan 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1232      1.1  macallan 	}
   1233      1.1  macallan }
   1234      1.1  macallan 
   1235      1.1  macallan static void
   1236      1.1  macallan pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
   1237      1.1  macallan {
   1238      1.1  macallan 	struct rasops_info *ri = cookie;
   1239      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1240      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
   1241      1.1  macallan 	int32_t x, ys, yd, width, height;
   1242      1.1  macallan 
   1243      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1244      1.1  macallan 		x = ri->ri_xorigin;
   1245      1.1  macallan 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
   1246      1.1  macallan 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
   1247      1.1  macallan 		width = ri->ri_emuwidth;
   1248      1.1  macallan 		height = ri->ri_font->fontheight*nrows;
   1249      1.1  macallan 		pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
   1250      1.1  macallan 	}
   1251      1.1  macallan }
   1252      1.1  macallan 
   1253      1.1  macallan static void
   1254      1.1  macallan pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
   1255      1.1  macallan {
   1256      1.1  macallan 	struct rasops_info *ri = cookie;
   1257      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
   1258      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
   1259      1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
   1260      1.1  macallan 
   1261      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1262      1.1  macallan 		x = ri->ri_xorigin;
   1263      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1264      1.1  macallan 		width = ri->ri_emuwidth;
   1265      1.1  macallan 		height = ri->ri_font->fontheight * nrows;
   1266      1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1267      1.1  macallan 
   1268      1.1  macallan 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1269      1.1  macallan 	}
   1270      1.1  macallan }
   1271      1.1  macallan 
   1272  1.7.4.2      yamt /*
   1273  1.7.4.2      yamt  * Permedia2 can't blit outside of 2048x2048, so reject anything higher
   1274  1.7.4.2      yamt  * max. dot clock is probably too high
   1275  1.7.4.2      yamt  */
   1276  1.7.4.2      yamt 
   1277  1.7.4.2      yamt #define MODE_IS_VALID(m) (((m)->hdisplay < 2048) && \
   1278  1.7.4.2      yamt 	((m)->dot_clock < 230000))
   1279  1.7.4.2      yamt 
   1280  1.7.4.1      yamt static void
   1281  1.7.4.1      yamt pm2_setup_i2c(struct pm2fb_softc *sc)
   1282  1.7.4.1      yamt {
   1283  1.7.4.2      yamt 	int i;
   1284  1.7.4.1      yamt #ifdef PM2FB_DEBUG
   1285  1.7.4.2      yamt 	int j;
   1286  1.7.4.1      yamt #endif
   1287  1.7.4.1      yamt 
   1288  1.7.4.1      yamt 	/* Fill in the i2c tag */
   1289  1.7.4.1      yamt 	sc->sc_i2c.ic_cookie = sc;
   1290  1.7.4.1      yamt 	sc->sc_i2c.ic_acquire_bus = pm2fb_i2c_acquire_bus;
   1291  1.7.4.1      yamt 	sc->sc_i2c.ic_release_bus = pm2fb_i2c_release_bus;
   1292  1.7.4.1      yamt 	sc->sc_i2c.ic_send_start = pm2fb_i2c_send_start;
   1293  1.7.4.1      yamt 	sc->sc_i2c.ic_send_stop = pm2fb_i2c_send_stop;
   1294  1.7.4.1      yamt 	sc->sc_i2c.ic_initiate_xfer = pm2fb_i2c_initiate_xfer;
   1295  1.7.4.1      yamt 	sc->sc_i2c.ic_read_byte = pm2fb_i2c_read_byte;
   1296  1.7.4.1      yamt 	sc->sc_i2c.ic_write_byte = pm2fb_i2c_write_byte;
   1297  1.7.4.1      yamt 	sc->sc_i2c.ic_exec = NULL;
   1298  1.7.4.1      yamt 
   1299  1.7.4.1      yamt 	DPRINTF("data: %08x\n", bus_space_read_4(sc->sc_memt, sc->sc_regh,
   1300  1.7.4.1      yamt 		PM2_DISPLAY_DATA));
   1301  1.7.4.1      yamt 
   1302  1.7.4.1      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, 0);
   1303  1.7.4.1      yamt 
   1304  1.7.4.1      yamt 	/* zero out the EDID buffer */
   1305  1.7.4.1      yamt 	memset(sc->sc_edid_data, 0, 128);
   1306  1.7.4.1      yamt 
   1307  1.7.4.1      yamt 	/* Some monitors don't respond first time */
   1308  1.7.4.1      yamt 	i = 0;
   1309  1.7.4.2      yamt 	while (sc->sc_edid_data[1] == 0 && i < 10) {
   1310  1.7.4.1      yamt 		ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
   1311  1.7.4.2      yamt 		i++;
   1312  1.7.4.2      yamt 	}
   1313  1.7.4.1      yamt #ifdef PM2FB_DEBUG
   1314  1.7.4.2      yamt 	printf("i = %d\n", i);
   1315  1.7.4.2      yamt 	for (i = 0; i < 128; i += 16) {
   1316  1.7.4.2      yamt 		printf("%02x:", i);
   1317  1.7.4.2      yamt 		for (j = 0; j < 16; j++)
   1318  1.7.4.2      yamt 			printf(" %02x", sc->sc_edid_data[i + j]);
   1319  1.7.4.2      yamt 		printf("\n");
   1320  1.7.4.1      yamt 	}
   1321  1.7.4.1      yamt #endif
   1322  1.7.4.2      yamt 
   1323  1.7.4.2      yamt 	if (edid_parse(&sc->sc_edid_data[0], &sc->sc_ei) != -1) {
   1324  1.7.4.2      yamt #ifdef PM2FB_DEBUG
   1325  1.7.4.2      yamt 		edid_print(&sc->sc_ei);
   1326  1.7.4.2      yamt #endif
   1327  1.7.4.2      yamt 
   1328  1.7.4.2      yamt 		/*
   1329  1.7.4.2      yamt 		 * Now pick a mode.
   1330  1.7.4.2      yamt 		 */
   1331  1.7.4.2      yamt 		if ((sc->sc_ei.edid_preferred_mode != NULL)) {
   1332  1.7.4.2      yamt 			struct videomode *m = sc->sc_ei.edid_preferred_mode;
   1333  1.7.4.2      yamt 			if (MODE_IS_VALID(m)) {
   1334  1.7.4.2      yamt 				sc->sc_videomode = m;
   1335  1.7.4.2      yamt 			} else {
   1336  1.7.4.2      yamt 				aprint_error_dev(sc->sc_dev,
   1337  1.7.4.2      yamt 				    "unable to use preferred mode\n");
   1338  1.7.4.2      yamt 			}
   1339  1.7.4.2      yamt 		}
   1340  1.7.4.2      yamt 		/*
   1341  1.7.4.2      yamt 		 * if we can't use the preferred mode go look for the
   1342  1.7.4.2      yamt 		 * best one we can support
   1343  1.7.4.2      yamt 		 */
   1344  1.7.4.2      yamt 		if (sc->sc_videomode == NULL) {
   1345  1.7.4.2      yamt 			int n;
   1346  1.7.4.2      yamt 			struct videomode *m = sc->sc_ei.edid_modes;
   1347  1.7.4.2      yamt 
   1348  1.7.4.2      yamt 			sort_modes(sc->sc_ei.edid_modes,
   1349  1.7.4.2      yamt 			 	    &sc->sc_ei.edid_preferred_mode,
   1350  1.7.4.2      yamt 				    sc->sc_ei.edid_nmodes);
   1351  1.7.4.2      yamt 			while ((sc->sc_videomode == NULL) &&
   1352  1.7.4.2      yamt 			       (n < sc->sc_ei.edid_nmodes)) {
   1353  1.7.4.2      yamt 				if (MODE_IS_VALID(&m[n])) {
   1354  1.7.4.2      yamt 					sc->sc_videomode = &m[n];
   1355  1.7.4.2      yamt 				}
   1356  1.7.4.2      yamt 				n++;
   1357  1.7.4.2      yamt 			}
   1358  1.7.4.2      yamt 		}
   1359  1.7.4.2      yamt 	}
   1360  1.7.4.2      yamt 	if (sc->sc_videomode == NULL) {
   1361  1.7.4.2      yamt 		/* no EDID data? */
   1362  1.7.4.2      yamt 		sc->sc_videomode = pick_mode_by_ref(sc->sc_width,
   1363  1.7.4.2      yamt 		    sc->sc_height, 60);
   1364  1.7.4.2      yamt 	}
   1365  1.7.4.2      yamt 	if (sc->sc_videomode != NULL) {
   1366  1.7.4.2      yamt 		pm2fb_set_mode(sc, sc->sc_videomode);
   1367  1.7.4.2      yamt 	}
   1368  1.7.4.1      yamt }
   1369  1.7.4.1      yamt 
   1370  1.7.4.1      yamt /* I2C bitbanging */
   1371  1.7.4.1      yamt static void pm2fb_i2cbb_set_bits(void *cookie, uint32_t bits)
   1372  1.7.4.1      yamt {
   1373  1.7.4.1      yamt 	struct pm2fb_softc *sc = cookie;
   1374  1.7.4.1      yamt 	uint32_t out;
   1375  1.7.4.1      yamt 
   1376  1.7.4.1      yamt 	out = bits << 2;	/* bitmasks match the IN bits */
   1377  1.7.4.1      yamt 
   1378  1.7.4.1      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, out);
   1379  1.7.4.2      yamt 	delay(100);
   1380  1.7.4.1      yamt }
   1381  1.7.4.1      yamt 
   1382  1.7.4.1      yamt static void pm2fb_i2cbb_set_dir(void *cookie, uint32_t dir)
   1383  1.7.4.1      yamt {
   1384  1.7.4.1      yamt 	/* Nothing to do */
   1385  1.7.4.1      yamt }
   1386  1.7.4.1      yamt 
   1387  1.7.4.1      yamt static uint32_t pm2fb_i2cbb_read(void *cookie)
   1388  1.7.4.1      yamt {
   1389  1.7.4.1      yamt 	struct pm2fb_softc *sc = cookie;
   1390  1.7.4.1      yamt 	uint32_t bits;
   1391  1.7.4.1      yamt 
   1392  1.7.4.1      yamt 	bits = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA);
   1393  1.7.4.1      yamt 	return bits;
   1394  1.7.4.1      yamt }
   1395  1.7.4.1      yamt 
   1396  1.7.4.1      yamt /* higher level I2C stuff */
   1397  1.7.4.1      yamt static int
   1398  1.7.4.1      yamt pm2fb_i2c_acquire_bus(void *cookie, int flags)
   1399  1.7.4.1      yamt {
   1400  1.7.4.1      yamt 	/* private bus */
   1401  1.7.4.1      yamt 	return (0);
   1402  1.7.4.1      yamt }
   1403  1.7.4.1      yamt 
   1404  1.7.4.1      yamt static void
   1405  1.7.4.1      yamt pm2fb_i2c_release_bus(void *cookie, int flags)
   1406  1.7.4.1      yamt {
   1407  1.7.4.1      yamt 	/* private bus */
   1408  1.7.4.1      yamt }
   1409  1.7.4.1      yamt 
   1410  1.7.4.1      yamt static int
   1411  1.7.4.1      yamt pm2fb_i2c_send_start(void *cookie, int flags)
   1412  1.7.4.1      yamt {
   1413  1.7.4.1      yamt 	return (i2c_bitbang_send_start(cookie, flags, &pm2fb_i2cbb_ops));
   1414  1.7.4.1      yamt }
   1415  1.7.4.1      yamt 
   1416  1.7.4.1      yamt static int
   1417  1.7.4.1      yamt pm2fb_i2c_send_stop(void *cookie, int flags)
   1418  1.7.4.1      yamt {
   1419  1.7.4.1      yamt 
   1420  1.7.4.1      yamt 	return (i2c_bitbang_send_stop(cookie, flags, &pm2fb_i2cbb_ops));
   1421  1.7.4.1      yamt }
   1422  1.7.4.1      yamt 
   1423  1.7.4.1      yamt static int
   1424  1.7.4.1      yamt pm2fb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
   1425  1.7.4.1      yamt {
   1426  1.7.4.1      yamt 
   1427  1.7.4.1      yamt 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
   1428  1.7.4.1      yamt 	    &pm2fb_i2cbb_ops));
   1429  1.7.4.1      yamt }
   1430  1.7.4.1      yamt 
   1431  1.7.4.1      yamt static int
   1432  1.7.4.1      yamt pm2fb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
   1433  1.7.4.1      yamt {
   1434  1.7.4.1      yamt 	return (i2c_bitbang_read_byte(cookie, valp, flags, &pm2fb_i2cbb_ops));
   1435  1.7.4.1      yamt }
   1436  1.7.4.1      yamt 
   1437  1.7.4.1      yamt static int
   1438  1.7.4.1      yamt pm2fb_i2c_write_byte(void *cookie, uint8_t val, int flags)
   1439  1.7.4.1      yamt {
   1440  1.7.4.1      yamt 	return (i2c_bitbang_write_byte(cookie, val, flags, &pm2fb_i2cbb_ops));
   1441  1.7.4.1      yamt }
   1442  1.7.4.2      yamt 
   1443  1.7.4.2      yamt #define RefClk 14318	/* all frequencies are in kHz */
   1444  1.7.4.2      yamt static int
   1445  1.7.4.2      yamt pm2fb_set_pll(struct pm2fb_softc *sc, int freq)
   1446  1.7.4.2      yamt {
   1447  1.7.4.2      yamt 	int m, n, p, diff, out_freq, bm, bn, bp, bdiff = 1000000, bfreq;
   1448  1.7.4.2      yamt 	int fi;
   1449  1.7.4.2      yamt 	uint8_t temp;
   1450  1.7.4.2      yamt 
   1451  1.7.4.2      yamt 	/*
   1452  1.7.4.2      yamt 	 * this should work on PM2V, PM2 needs something slightly different
   1453  1.7.4.2      yamt 	 */
   1454  1.7.4.2      yamt 	for (m = 1; m < 128; m++) {
   1455  1.7.4.2      yamt 		for (n = 2 * m + 1; n < 256; n++) {
   1456  1.7.4.2      yamt 			fi = RefClk * n / m;
   1457  1.7.4.2      yamt 			for (p = 0; p < 2; p++) {
   1458  1.7.4.2      yamt 				out_freq = fi >> (p + 1);
   1459  1.7.4.2      yamt 				diff = abs(out_freq - freq);
   1460  1.7.4.2      yamt 				if (diff < bdiff) {
   1461  1.7.4.2      yamt 					bdiff = diff;
   1462  1.7.4.2      yamt 					bfreq = out_freq;
   1463  1.7.4.2      yamt 					bm = m;
   1464  1.7.4.2      yamt 					bn = n;
   1465  1.7.4.2      yamt 					bp = p;
   1466  1.7.4.2      yamt 				}
   1467  1.7.4.2      yamt 			}
   1468  1.7.4.2      yamt 		}
   1469  1.7.4.2      yamt 	}
   1470  1.7.4.2      yamt #if 0
   1471  1.7.4.2      yamt 	/*
   1472  1.7.4.2      yamt 	 * XXX
   1473  1.7.4.2      yamt 	 * output between switching modes and attaching a wsdisplay will
   1474  1.7.4.2      yamt 	 * go through firmware calls on sparc64 and potentially mess up
   1475  1.7.4.2      yamt 	 * our drawing engine state
   1476  1.7.4.2      yamt 	 */
   1477  1.7.4.2      yamt 	DPRINTF("best: %d kHz ( %d off ), %d %d %d\n", bfreq, bdiff, bm, bn, bp);
   1478  1.7.4.2      yamt #endif
   1479  1.7.4.2      yamt 	temp = pm2fb_read_dac(sc, PM2V_DAC_CLOCK_CONTROL) & 0xfc;
   1480  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_CONTROL, 0);
   1481  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_M, bm);
   1482  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_N, bn);
   1483  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_A_P, bp);
   1484  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_CLOCK_CONTROL, temp | 3);
   1485  1.7.4.2      yamt 	return 0;
   1486  1.7.4.2      yamt }
   1487  1.7.4.2      yamt 
   1488  1.7.4.2      yamt /*
   1489  1.7.4.2      yamt  * most of the following was adapted from the xf86-video-glint driver's
   1490  1.7.4.2      yamt  * pm2v_dac.c
   1491  1.7.4.2      yamt  */
   1492  1.7.4.2      yamt static void
   1493  1.7.4.2      yamt pm2fb_set_mode(struct pm2fb_softc *sc, const struct videomode *mode)
   1494  1.7.4.2      yamt {
   1495  1.7.4.2      yamt 	int t1, t2, t3, t4, stride;
   1496  1.7.4.2      yamt 	uint32_t vclk;
   1497  1.7.4.2      yamt 	uint8_t sync = 0;
   1498  1.7.4.2      yamt 
   1499  1.7.4.2      yamt 	t1 = mode->hsync_start - mode->hdisplay;
   1500  1.7.4.2      yamt 	t2 = mode->vsync_start - mode->vdisplay;
   1501  1.7.4.2      yamt 	t3 = mode->hsync_end - mode->hsync_start;
   1502  1.7.4.2      yamt 	t4 = mode->vsync_end - mode->vsync_start;
   1503  1.7.4.2      yamt 
   1504  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HTOTAL,
   1505  1.7.4.2      yamt 	    ((mode->htotal) >> 3) - 1);
   1506  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_END,
   1507  1.7.4.2      yamt 	    (t1 + t3) >> 3);
   1508  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HSYNC_START,
   1509  1.7.4.2      yamt 	    (t1 >> 3) - 1);
   1510  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HBLANK_END,
   1511  1.7.4.2      yamt 	    (mode->htotal - mode->hdisplay) >> 3);
   1512  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HGATE_END,
   1513  1.7.4.2      yamt 	    (mode->htotal - mode->hdisplay) >> 3);
   1514  1.7.4.2      yamt 
   1515  1.7.4.2      yamt 	/* first round up to the next multiple of 32 */
   1516  1.7.4.2      yamt 	stride = (mode->hdisplay + 31) & ~31;
   1517  1.7.4.2      yamt 	/* then find the next bigger one that we have partial products for */
   1518  1.7.4.2      yamt 	while ((partprodPermedia[stride >> 5] == -1) && (stride < 2048)) {
   1519  1.7.4.2      yamt 		stride += 32;
   1520  1.7.4.2      yamt 	}
   1521  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_STRIDE,
   1522  1.7.4.2      yamt 	    stride >> 3);
   1523  1.7.4.2      yamt 
   1524  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VTOTAL,
   1525  1.7.4.2      yamt 	    mode->vtotal - 1);
   1526  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_END,
   1527  1.7.4.2      yamt 	    t2 + t4);
   1528  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VSYNC_START,
   1529  1.7.4.2      yamt 	    t2);
   1530  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VBLANK_END,
   1531  1.7.4.2      yamt 	    mode->vtotal - mode->vdisplay);
   1532  1.7.4.2      yamt 
   1533  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VIDEO_CONTROL,
   1534  1.7.4.2      yamt 	    PM2_VC_VIDEO_ENABLE | PM2_VC_RAMDAC_64BIT |
   1535  1.7.4.2      yamt 	    PM2_VC_HSYNC_ACT_HIGH | PM2_VC_VSYNC_ACT_HIGH);
   1536  1.7.4.2      yamt 
   1537  1.7.4.2      yamt 	vclk = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL);
   1538  1.7.4.2      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_VCLKCTL,
   1539  1.7.4.2      yamt 	    vclk & 0xfffffffc);
   1540  1.7.4.2      yamt 
   1541  1.7.4.2      yamt 	pm2fb_set_pll(sc, mode->dot_clock / 2);
   1542  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_MISC_CONTROL, PM2V_DAC_8BIT);
   1543  1.7.4.2      yamt 
   1544  1.7.4.2      yamt 	if (mode->flags & VID_PHSYNC)
   1545  1.7.4.2      yamt 		sync |= PM2V_DAC_HSYNC_INV;
   1546  1.7.4.2      yamt 	if (mode->flags & VID_PVSYNC)
   1547  1.7.4.2      yamt 		sync |= PM2V_DAC_VSYNC_INV;
   1548  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_SYNC_CONTROL, sync);
   1549  1.7.4.2      yamt 
   1550  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_COLOR_FORMAT, PM2V_DAC_PALETTE);
   1551  1.7.4.2      yamt 	pm2fb_write_dac(sc, PM2V_DAC_PIXEL_SIZE, PM2V_PS_8BIT);
   1552  1.7.4.2      yamt 	sc->sc_width = mode->hdisplay;
   1553  1.7.4.2      yamt 	sc->sc_height = mode->vdisplay;
   1554  1.7.4.2      yamt 	sc->sc_depth = 8;
   1555  1.7.4.2      yamt 	sc->sc_stride = stride;
   1556  1.7.4.2      yamt 	aprint_normal_dev(sc->sc_dev, "using %d x %d in 8 bit, stride %d\n",
   1557  1.7.4.2      yamt 	    sc->sc_width, sc->sc_height, stride);
   1558  1.7.4.2      yamt }
   1559