Home | History | Annotate | Line # | Download | only in pci
pm2fb.c revision 1.7.4.1
      1  1.7.4.1      yamt /*	$NetBSD: pm2fb.c,v 1.7.4.1 2012/04/17 00:07:57 yamt Exp $	*/
      2      1.1  macallan 
      3      1.1  macallan /*
      4      1.1  macallan  * Copyright (c) 2009 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.1      yamt __KERNEL_RCSID(0, "$NetBSD: pm2fb.c,v 1.7.4.1 2012/04/17 00:07:57 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.1  macallan 
     44      1.1  macallan #include <dev/videomode/videomode.h>
     45      1.1  macallan 
     46      1.1  macallan #include <dev/pci/pcivar.h>
     47      1.1  macallan #include <dev/pci/pcireg.h>
     48      1.1  macallan #include <dev/pci/pcidevs.h>
     49      1.1  macallan #include <dev/pci/pciio.h>
     50      1.1  macallan #include <dev/pci/pm2reg.h>
     51      1.1  macallan 
     52      1.1  macallan #include <dev/wscons/wsdisplayvar.h>
     53      1.1  macallan #include <dev/wscons/wsconsio.h>
     54      1.1  macallan #include <dev/wsfont/wsfont.h>
     55      1.1  macallan #include <dev/rasops/rasops.h>
     56      1.1  macallan #include <dev/wscons/wsdisplay_vconsvar.h>
     57      1.7    cegger #include <dev/pci/wsdisplay_pci.h>
     58      1.1  macallan 
     59      1.1  macallan #include <dev/i2c/i2cvar.h>
     60  1.7.4.1      yamt #include <dev/i2c/i2c_bitbang.h>
     61  1.7.4.1      yamt #include <dev/i2c/ddcvar.h>
     62  1.7.4.1      yamt #include <dev/videomode/videomode.h>
     63  1.7.4.1      yamt #include <dev/videomode/edidvar.h>
     64  1.7.4.1      yamt #include <dev/videomode/edidreg.h>
     65  1.7.4.1      yamt 
     66  1.7.4.1      yamt #include "opt_pm2fb.h"
     67  1.7.4.1      yamt 
     68  1.7.4.1      yamt #ifdef PM2FB_DEBUG
     69  1.7.4.1      yamt #define DPRINTF aprint_error
     70  1.7.4.1      yamt #else
     71  1.7.4.1      yamt #define DPRINTF while (0) printf
     72  1.7.4.1      yamt #endif
     73      1.1  macallan 
     74      1.1  macallan struct pm2fb_softc {
     75      1.1  macallan 	device_t sc_dev;
     76      1.1  macallan 
     77      1.1  macallan 	pci_chipset_tag_t sc_pc;
     78      1.1  macallan 	pcitag_t sc_pcitag;
     79      1.1  macallan 
     80      1.1  macallan 	bus_space_tag_t sc_memt;
     81      1.1  macallan 	bus_space_tag_t sc_iot;
     82      1.1  macallan 
     83      1.1  macallan 	bus_space_handle_t sc_regh;
     84      1.1  macallan 	bus_addr_t sc_fb, sc_reg;
     85      1.1  macallan 	bus_size_t sc_fbsize, sc_regsize;
     86      1.1  macallan 
     87      1.1  macallan 	int sc_width, sc_height, sc_depth, sc_stride;
     88      1.1  macallan 	int sc_locked;
     89      1.1  macallan 	struct vcons_screen sc_console_screen;
     90      1.1  macallan 	struct wsscreen_descr sc_defaultscreen_descr;
     91      1.1  macallan 	const struct wsscreen_descr *sc_screens[1];
     92      1.1  macallan 	struct wsscreen_list sc_screenlist;
     93      1.1  macallan 	struct vcons_data vd;
     94      1.1  macallan 	int sc_mode;
     95      1.1  macallan 	u_char sc_cmap_red[256];
     96      1.1  macallan 	u_char sc_cmap_green[256];
     97      1.1  macallan 	u_char sc_cmap_blue[256];
     98      1.1  macallan 	/* engine stuff */
     99      1.3  macallan 	uint32_t sc_pprod;
    100  1.7.4.1      yamt 	/* i2c stuff */
    101  1.7.4.1      yamt 	struct i2c_controller sc_i2c;
    102  1.7.4.1      yamt 	uint8_t sc_edid_data[128];
    103      1.1  macallan };
    104      1.1  macallan 
    105      1.1  macallan static int	pm2fb_match(device_t, cfdata_t, void *);
    106      1.1  macallan static void	pm2fb_attach(device_t, device_t, void *);
    107      1.1  macallan 
    108      1.1  macallan CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
    109      1.1  macallan     pm2fb_match, pm2fb_attach, NULL, NULL);
    110      1.1  macallan 
    111      1.1  macallan extern const u_char rasops_cmap[768];
    112      1.1  macallan 
    113      1.1  macallan static int	pm2fb_ioctl(void *, void *, u_long, void *, int,
    114      1.1  macallan 			     struct lwp *);
    115      1.1  macallan static paddr_t	pm2fb_mmap(void *, void *, off_t, int);
    116      1.1  macallan static void	pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
    117      1.1  macallan 
    118      1.1  macallan static int	pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    119      1.1  macallan static int 	pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    120      1.1  macallan static void	pm2fb_restore_palette(struct pm2fb_softc *);
    121      1.1  macallan static int 	pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
    122      1.1  macallan 			    uint8_t, uint8_t);
    123      1.1  macallan 
    124      1.1  macallan static void	pm2fb_init(struct pm2fb_softc *);
    125      1.1  macallan static void	pm2fb_flush_engine(struct pm2fb_softc *);
    126      1.1  macallan static void	pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
    127      1.1  macallan 			    uint32_t);
    128      1.1  macallan static void	pm2fb_bitblt(struct pm2fb_softc *, int, int, int, int, int,
    129      1.1  macallan 			    int, int);
    130      1.1  macallan 
    131      1.1  macallan static void	pm2fb_cursor(void *, int, int, int);
    132      1.1  macallan static void	pm2fb_putchar(void *, int, int, u_int, long);
    133      1.1  macallan static void	pm2fb_copycols(void *, int, int, int, int);
    134      1.1  macallan static void	pm2fb_erasecols(void *, int, int, int, long);
    135      1.1  macallan static void	pm2fb_copyrows(void *, int, int, int);
    136      1.1  macallan static void	pm2fb_eraserows(void *, int, int, long);
    137      1.1  macallan 
    138      1.1  macallan struct wsdisplay_accessops pm2fb_accessops = {
    139      1.1  macallan 	pm2fb_ioctl,
    140      1.1  macallan 	pm2fb_mmap,
    141      1.1  macallan 	NULL,	/* alloc_screen */
    142      1.1  macallan 	NULL,	/* free_screen */
    143      1.1  macallan 	NULL,	/* show_screen */
    144      1.1  macallan 	NULL, 	/* load_font */
    145      1.1  macallan 	NULL,	/* pollc */
    146      1.1  macallan 	NULL	/* scroll */
    147      1.1  macallan };
    148      1.1  macallan 
    149  1.7.4.1      yamt /* I2C glue */
    150  1.7.4.1      yamt static int pm2fb_i2c_acquire_bus(void *, int);
    151  1.7.4.1      yamt static void pm2fb_i2c_release_bus(void *, int);
    152  1.7.4.1      yamt static int pm2fb_i2c_send_start(void *, int);
    153  1.7.4.1      yamt static int pm2fb_i2c_send_stop(void *, int);
    154  1.7.4.1      yamt static int pm2fb_i2c_initiate_xfer(void *, i2c_addr_t, int);
    155  1.7.4.1      yamt static int pm2fb_i2c_read_byte(void *, uint8_t *, int);
    156  1.7.4.1      yamt static int pm2fb_i2c_write_byte(void *, uint8_t, int);
    157  1.7.4.1      yamt 
    158  1.7.4.1      yamt /* I2C bitbang glue */
    159  1.7.4.1      yamt static void pm2fb_i2cbb_set_bits(void *, uint32_t);
    160  1.7.4.1      yamt static void pm2fb_i2cbb_set_dir(void *, uint32_t);
    161  1.7.4.1      yamt static uint32_t pm2fb_i2cbb_read(void *);
    162  1.7.4.1      yamt 
    163  1.7.4.1      yamt static void pm2_setup_i2c(struct pm2fb_softc *);
    164  1.7.4.1      yamt 
    165  1.7.4.1      yamt static const struct i2c_bitbang_ops pm2fb_i2cbb_ops = {
    166  1.7.4.1      yamt 	pm2fb_i2cbb_set_bits,
    167  1.7.4.1      yamt 	pm2fb_i2cbb_set_dir,
    168  1.7.4.1      yamt 	pm2fb_i2cbb_read,
    169  1.7.4.1      yamt 	{
    170  1.7.4.1      yamt 		PM2_DD_SDA_IN,
    171  1.7.4.1      yamt 		PM2_DD_SCL_IN,
    172  1.7.4.1      yamt 		0,
    173  1.7.4.1      yamt 		0
    174  1.7.4.1      yamt 	}
    175  1.7.4.1      yamt };
    176  1.7.4.1      yamt 
    177      1.1  macallan static inline void
    178      1.1  macallan pm2fb_wait(struct pm2fb_softc *sc, int slots)
    179      1.1  macallan {
    180      1.1  macallan 	uint32_t reg;
    181      1.1  macallan 
    182      1.1  macallan 	do {
    183      1.1  macallan 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    184      1.1  macallan 			PM2_INPUT_FIFO_SPACE);
    185      1.1  macallan 	} while (reg <= slots);
    186      1.1  macallan }
    187      1.1  macallan 
    188      1.1  macallan static void
    189      1.1  macallan pm2fb_flush_engine(struct pm2fb_softc *sc)
    190      1.1  macallan {
    191      1.1  macallan 
    192      1.1  macallan 	pm2fb_wait(sc, 2);
    193      1.1  macallan 
    194      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
    195      1.1  macallan 	    PM2FLT_PASS_SYNC);
    196      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
    197      1.1  macallan 	do {
    198      1.1  macallan 		while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    199      1.1  macallan 			PM2_OUTPUT_FIFO_WORDS) == 0);
    200      1.1  macallan 	} while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
    201      1.1  macallan 	    0x188);
    202      1.1  macallan }
    203      1.1  macallan 
    204      1.1  macallan static int
    205      1.1  macallan pm2fb_match(device_t parent, cfdata_t match, void *aux)
    206      1.1  macallan {
    207      1.1  macallan 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    208      1.1  macallan 
    209      1.1  macallan 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    210      1.1  macallan 		return 0;
    211      1.1  macallan 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3DLABS)
    212      1.1  macallan 		return 0;
    213      1.1  macallan 
    214      1.1  macallan 	/* only cards tested on so far - likely need a list */
    215      1.1  macallan 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2) ||
    216      1.1  macallan 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2V))
    217      1.1  macallan 		return 100;
    218      1.1  macallan 	return (0);
    219      1.1  macallan }
    220      1.1  macallan 
    221      1.1  macallan static void
    222      1.1  macallan pm2fb_attach(device_t parent, device_t self, void *aux)
    223      1.1  macallan {
    224      1.1  macallan 	struct pm2fb_softc	*sc = device_private(self);
    225      1.1  macallan 	struct pci_attach_args	*pa = aux;
    226      1.1  macallan 	struct rasops_info	*ri;
    227      1.1  macallan 	struct wsemuldisplaydev_attach_args aa;
    228      1.1  macallan 	prop_dictionary_t	dict;
    229      1.1  macallan 	unsigned long		defattr;
    230      1.1  macallan 	bool			is_console;
    231      1.1  macallan 	int i, j;
    232      1.3  macallan 	uint32_t flags;
    233      1.1  macallan 
    234      1.1  macallan 	sc->sc_pc = pa->pa_pc;
    235      1.1  macallan 	sc->sc_pcitag = pa->pa_tag;
    236      1.1  macallan 	sc->sc_memt = pa->pa_memt;
    237      1.1  macallan 	sc->sc_iot = pa->pa_iot;
    238      1.1  macallan 	sc->sc_dev = self;
    239      1.1  macallan 
    240  1.7.4.1      yamt 	pci_aprint_devinfo(pa, NULL);
    241      1.1  macallan 
    242      1.1  macallan 	/* fill in parameters from properties */
    243      1.1  macallan 	dict = device_properties(self);
    244      1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    245      1.1  macallan 		aprint_error("%s: no width property\n", device_xname(self));
    246      1.1  macallan 		return;
    247      1.1  macallan 	}
    248      1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    249      1.1  macallan 		aprint_error("%s: no height property\n", device_xname(self));
    250      1.1  macallan 		return;
    251      1.1  macallan 	}
    252      1.1  macallan 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    253      1.1  macallan 		aprint_error("%s: no depth property\n", device_xname(self));
    254      1.1  macallan 		return;
    255      1.1  macallan 	}
    256      1.1  macallan 	/*
    257      1.1  macallan 	 * don't look at the linebytes property - The Raptor firmware lies
    258      1.1  macallan 	 * about it. Get it from width * depth >> 3 instead.
    259      1.1  macallan 	 */
    260      1.1  macallan 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    261      1.1  macallan 
    262      1.1  macallan 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    263      1.1  macallan 
    264      1.3  macallan 	pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
    265      1.3  macallan 	    &sc->sc_fb, &sc->sc_fbsize, &flags);
    266      1.1  macallan 
    267      1.1  macallan 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    268      1.1  macallan 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    269      1.1  macallan 		aprint_error("%s: failed to map registers.\n",
    270      1.1  macallan 		    device_xname(sc->sc_dev));
    271      1.1  macallan 	}
    272      1.1  macallan 
    273      1.1  macallan 	/*
    274      1.1  macallan 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    275      1.1  macallan 	 * but as far as I know there are no PM2 with 64bit BARs
    276      1.1  macallan 	 */
    277      1.1  macallan 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    278      1.1  macallan 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    279      1.1  macallan 
    280      1.1  macallan 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    281      1.1  macallan 		"default",
    282      1.1  macallan 		0, 0,
    283      1.1  macallan 		NULL,
    284      1.1  macallan 		8, 16,
    285      1.1  macallan 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    286      1.1  macallan 		NULL
    287      1.1  macallan 	};
    288      1.1  macallan 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    289      1.1  macallan 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    290      1.1  macallan 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    291      1.1  macallan 	sc->sc_locked = 0;
    292      1.1  macallan 
    293  1.7.4.1      yamt 	pm2_setup_i2c(sc);
    294  1.7.4.1      yamt 
    295      1.1  macallan 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    296      1.1  macallan 	    &pm2fb_accessops);
    297      1.1  macallan 	sc->vd.init_screen = pm2fb_init_screen;
    298      1.1  macallan 
    299      1.1  macallan 	/* init engine here */
    300      1.1  macallan 	pm2fb_init(sc);
    301      1.1  macallan 
    302      1.1  macallan 	ri = &sc->sc_console_screen.scr_ri;
    303      1.1  macallan 
    304      1.1  macallan 	j = 0;
    305  1.7.4.1      yamt 	for (i = 0; i < 256; i++) {
    306      1.1  macallan 		sc->sc_cmap_red[i] = rasops_cmap[j];
    307      1.1  macallan 		sc->sc_cmap_green[i] = rasops_cmap[j + 1];
    308      1.1  macallan 		sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
    309      1.1  macallan 		pm2fb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    310      1.1  macallan 		    rasops_cmap[j + 2]);
    311      1.1  macallan 		j += 3;
    312      1.1  macallan 	}
    313      1.1  macallan 
    314      1.1  macallan 	if (is_console) {
    315      1.1  macallan 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    316      1.1  macallan 		    &defattr);
    317      1.1  macallan 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    318      1.1  macallan 
    319      1.1  macallan 		pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    320      1.1  macallan 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    321      1.1  macallan 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    322      1.1  macallan 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    323      1.1  macallan 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    324      1.1  macallan 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    325      1.1  macallan 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    326      1.1  macallan 		    defattr);
    327      1.1  macallan 		vcons_replay_msgbuf(&sc->sc_console_screen);
    328      1.1  macallan 	} else {
    329      1.1  macallan 		/*
    330      1.1  macallan 		 * since we're not the console we can postpone the rest
    331      1.1  macallan 		 * until someone actually allocates a screen for us
    332      1.1  macallan 		 */
    333      1.1  macallan 		(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    334      1.1  macallan 	}
    335      1.1  macallan 
    336      1.1  macallan 	aa.console = is_console;
    337      1.1  macallan 	aa.scrdata = &sc->sc_screenlist;
    338      1.1  macallan 	aa.accessops = &pm2fb_accessops;
    339      1.1  macallan 	aa.accesscookie = &sc->vd;
    340      1.1  macallan 
    341      1.3  macallan 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    342      1.1  macallan }
    343      1.1  macallan 
    344      1.1  macallan static int
    345      1.1  macallan pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    346      1.1  macallan 	struct lwp *l)
    347      1.1  macallan {
    348      1.1  macallan 	struct vcons_data *vd = v;
    349      1.1  macallan 	struct pm2fb_softc *sc = vd->cookie;
    350      1.1  macallan 	struct wsdisplay_fbinfo *wdf;
    351      1.1  macallan 	struct vcons_screen *ms = vd->active;
    352      1.1  macallan 
    353      1.1  macallan 	switch (cmd) {
    354      1.6    cegger 	case WSDISPLAYIO_GTYPE:
    355      1.6    cegger 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    356      1.6    cegger 		return 0;
    357      1.6    cegger 
    358      1.6    cegger 	/* PCI config read/write passthrough. */
    359      1.6    cegger 	case PCI_IOC_CFGREAD:
    360      1.6    cegger 	case PCI_IOC_CFGWRITE:
    361      1.6    cegger 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    362      1.6    cegger 		    cmd, data, flag, l);
    363      1.6    cegger 
    364      1.7    cegger 	case WSDISPLAYIO_GET_BUSID:
    365      1.7    cegger 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    366      1.7    cegger 		    sc->sc_pcitag, data);
    367      1.7    cegger 
    368      1.6    cegger 	case WSDISPLAYIO_GINFO:
    369      1.6    cegger 		if (ms == NULL)
    370      1.6    cegger 			return ENODEV;
    371      1.6    cegger 		wdf = (void *)data;
    372      1.6    cegger 		wdf->height = ms->scr_ri.ri_height;
    373      1.6    cegger 		wdf->width = ms->scr_ri.ri_width;
    374      1.6    cegger 		wdf->depth = ms->scr_ri.ri_depth;
    375      1.6    cegger 		wdf->cmsize = 256;
    376      1.6    cegger 		return 0;
    377      1.6    cegger 
    378      1.6    cegger 	case WSDISPLAYIO_GETCMAP:
    379      1.6    cegger 		return pm2fb_getcmap(sc,
    380      1.6    cegger 		    (struct wsdisplay_cmap *)data);
    381      1.6    cegger 
    382      1.6    cegger 	case WSDISPLAYIO_PUTCMAP:
    383      1.6    cegger 		return pm2fb_putcmap(sc,
    384      1.6    cegger 		    (struct wsdisplay_cmap *)data);
    385      1.6    cegger 
    386      1.6    cegger 	case WSDISPLAYIO_LINEBYTES:
    387      1.6    cegger 		*(u_int *)data = sc->sc_stride;
    388      1.6    cegger 		return 0;
    389      1.1  macallan 
    390      1.6    cegger 	case WSDISPLAYIO_SMODE: {
    391      1.6    cegger 		int new_mode = *(int*)data;
    392      1.6    cegger 		if (new_mode != sc->sc_mode) {
    393      1.6    cegger 			sc->sc_mode = new_mode;
    394      1.6    cegger 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    395      1.6    cegger 				pm2fb_restore_palette(sc);
    396      1.6    cegger 				vcons_redraw_screen(ms);
    397      1.6    cegger 			} else
    398      1.6    cegger 				pm2fb_flush_engine(sc);
    399      1.6    cegger 		}
    400      1.6    cegger 		}
    401      1.6    cegger 		return 0;
    402  1.7.4.1      yamt 	case WSDISPLAYIO_GET_EDID: {
    403  1.7.4.1      yamt 		struct wsdisplayio_edid_info *d = data;
    404  1.7.4.1      yamt 		d->data_size = 128;
    405  1.7.4.1      yamt 		if (d->buffer_size < 128)
    406  1.7.4.1      yamt 			return EAGAIN;
    407  1.7.4.1      yamt 		return copyout(sc->sc_edid_data, d->edid_data, 128);
    408  1.7.4.1      yamt 	}
    409      1.1  macallan 	}
    410      1.1  macallan 	return EPASSTHROUGH;
    411      1.1  macallan }
    412      1.1  macallan 
    413      1.1  macallan static paddr_t
    414      1.1  macallan pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
    415      1.1  macallan {
    416      1.1  macallan 	struct vcons_data *vd = v;
    417      1.1  macallan 	struct pm2fb_softc *sc = vd->cookie;
    418      1.1  macallan 	paddr_t pa;
    419      1.1  macallan 
    420      1.1  macallan 	/* 'regular' framebuffer mmap()ing */
    421      1.1  macallan 	if (offset < sc->sc_fbsize) {
    422      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    423      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    424      1.1  macallan 		return pa;
    425      1.1  macallan 	}
    426      1.1  macallan 
    427      1.1  macallan 	/*
    428      1.1  macallan 	 * restrict all other mappings to processes with superuser privileges
    429      1.1  macallan 	 * or the kernel itself
    430      1.1  macallan 	 */
    431  1.7.4.1      yamt 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
    432  1.7.4.1      yamt 	    NULL, NULL, NULL, NULL) != 0) {
    433      1.1  macallan 		aprint_normal("%s: mmap() rejected.\n",
    434      1.1  macallan 		    device_xname(sc->sc_dev));
    435      1.1  macallan 		return -1;
    436      1.1  macallan 	}
    437      1.1  macallan 
    438      1.1  macallan 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    439      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    440      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    441      1.1  macallan 		return pa;
    442      1.1  macallan 	}
    443      1.1  macallan 
    444      1.1  macallan 	if ((offset >= sc->sc_reg) &&
    445      1.1  macallan 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    446      1.1  macallan 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    447      1.1  macallan 		    BUS_SPACE_MAP_LINEAR);
    448      1.1  macallan 		return pa;
    449      1.1  macallan 	}
    450      1.1  macallan 
    451      1.1  macallan #ifdef PCI_MAGIC_IO_RANGE
    452      1.1  macallan 	/* allow mapping of IO space */
    453      1.1  macallan 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    454      1.1  macallan 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    455      1.1  macallan 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    456      1.1  macallan 		    0, prot, BUS_SPACE_MAP_LINEAR);
    457      1.1  macallan 		return pa;
    458      1.1  macallan 	}
    459      1.1  macallan #endif
    460      1.1  macallan 
    461      1.1  macallan 	return -1;
    462      1.1  macallan }
    463      1.1  macallan 
    464      1.1  macallan static void
    465      1.1  macallan pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
    466      1.1  macallan     int existing, long *defattr)
    467      1.1  macallan {
    468      1.1  macallan 	struct pm2fb_softc *sc = cookie;
    469      1.1  macallan 	struct rasops_info *ri = &scr->scr_ri;
    470      1.1  macallan 
    471      1.1  macallan 	ri->ri_depth = sc->sc_depth;
    472      1.1  macallan 	ri->ri_width = sc->sc_width;
    473      1.1  macallan 	ri->ri_height = sc->sc_height;
    474      1.1  macallan 	ri->ri_stride = sc->sc_stride;
    475      1.3  macallan 	ri->ri_flg = RI_CENTER;
    476      1.1  macallan 
    477  1.7.4.1      yamt 	rasops_init(ri, 0, 0);
    478      1.1  macallan 	ri->ri_caps = WSSCREEN_WSCOLORS;
    479      1.1  macallan 
    480      1.1  macallan 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    481      1.1  macallan 		    sc->sc_width / ri->ri_font->fontwidth);
    482      1.1  macallan 
    483      1.1  macallan 	ri->ri_hw = scr;
    484      1.1  macallan 	ri->ri_ops.copyrows = pm2fb_copyrows;
    485      1.1  macallan 	ri->ri_ops.copycols = pm2fb_copycols;
    486      1.1  macallan 	ri->ri_ops.cursor = pm2fb_cursor;
    487      1.1  macallan 	ri->ri_ops.eraserows = pm2fb_eraserows;
    488      1.1  macallan 	ri->ri_ops.erasecols = pm2fb_erasecols;
    489      1.1  macallan 	ri->ri_ops.putchar = pm2fb_putchar;
    490      1.1  macallan }
    491      1.1  macallan 
    492      1.1  macallan static int
    493      1.1  macallan pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    494      1.1  macallan {
    495      1.1  macallan 	u_char *r, *g, *b;
    496      1.1  macallan 	u_int index = cm->index;
    497      1.1  macallan 	u_int count = cm->count;
    498      1.1  macallan 	int i, error;
    499      1.1  macallan 	u_char rbuf[256], gbuf[256], bbuf[256];
    500      1.1  macallan 
    501      1.1  macallan #ifdef PM2FB_DEBUG
    502      1.1  macallan 	aprint_debug("putcmap: %d %d\n",index, count);
    503      1.1  macallan #endif
    504      1.1  macallan 	if (cm->index >= 256 || cm->count > 256 ||
    505      1.1  macallan 	    (cm->index + cm->count) > 256)
    506      1.1  macallan 		return EINVAL;
    507      1.1  macallan 	error = copyin(cm->red, &rbuf[index], count);
    508      1.1  macallan 	if (error)
    509      1.1  macallan 		return error;
    510      1.1  macallan 	error = copyin(cm->green, &gbuf[index], count);
    511      1.1  macallan 	if (error)
    512      1.1  macallan 		return error;
    513      1.1  macallan 	error = copyin(cm->blue, &bbuf[index], count);
    514      1.1  macallan 	if (error)
    515      1.1  macallan 		return error;
    516      1.1  macallan 
    517      1.1  macallan 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    518      1.1  macallan 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    519      1.1  macallan 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    520      1.1  macallan 
    521      1.1  macallan 	r = &sc->sc_cmap_red[index];
    522      1.1  macallan 	g = &sc->sc_cmap_green[index];
    523      1.1  macallan 	b = &sc->sc_cmap_blue[index];
    524      1.1  macallan 
    525      1.1  macallan 	for (i = 0; i < count; i++) {
    526      1.1  macallan 		pm2fb_putpalreg(sc, index, *r, *g, *b);
    527      1.1  macallan 		index++;
    528      1.1  macallan 		r++, g++, b++;
    529      1.1  macallan 	}
    530      1.1  macallan 	return 0;
    531      1.1  macallan }
    532      1.1  macallan 
    533      1.1  macallan static int
    534      1.1  macallan pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    535      1.1  macallan {
    536      1.1  macallan 	u_int index = cm->index;
    537      1.1  macallan 	u_int count = cm->count;
    538      1.1  macallan 	int error;
    539      1.1  macallan 
    540      1.1  macallan 	if (index >= 255 || count > 256 || index + count > 256)
    541      1.1  macallan 		return EINVAL;
    542      1.1  macallan 
    543      1.1  macallan 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    544      1.1  macallan 	if (error)
    545      1.1  macallan 		return error;
    546      1.1  macallan 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    547      1.1  macallan 	if (error)
    548      1.1  macallan 		return error;
    549      1.1  macallan 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    550      1.1  macallan 	if (error)
    551      1.1  macallan 		return error;
    552      1.1  macallan 
    553      1.1  macallan 	return 0;
    554      1.1  macallan }
    555      1.1  macallan 
    556      1.1  macallan static void
    557      1.1  macallan pm2fb_restore_palette(struct pm2fb_softc *sc)
    558      1.1  macallan {
    559      1.1  macallan 	int i;
    560      1.1  macallan 
    561      1.1  macallan 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    562      1.1  macallan 		pm2fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    563      1.1  macallan 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    564      1.1  macallan 	}
    565      1.1  macallan }
    566      1.1  macallan 
    567      1.1  macallan static int
    568      1.1  macallan pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    569      1.1  macallan     uint8_t b)
    570      1.1  macallan {
    571      1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
    572      1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
    573      1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
    574      1.1  macallan 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
    575      1.1  macallan 	return 0;
    576      1.1  macallan }
    577      1.1  macallan 
    578      1.1  macallan static void
    579      1.1  macallan pm2fb_init(struct pm2fb_softc *sc)
    580      1.1  macallan {
    581      1.1  macallan 	pm2fb_flush_engine(sc);
    582      1.1  macallan 
    583      1.1  macallan 	pm2fb_wait(sc, 8);
    584      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
    585      1.1  macallan #if 0
    586      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
    587      1.1  macallan 		0xffffffff);
    588      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
    589      1.1  macallan 		0xffffffff);
    590      1.3  macallan #endif
    591      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
    592      1.3  macallan 		0xffffffff);
    593      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
    594      1.3  macallan 		0xffffffff);
    595      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
    596      1.3  macallan 		PM2WM_WRITE_EN);
    597      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
    598      1.3  macallan 	    (sc->sc_height << 16) | sc->sc_width);
    599      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
    600      1.3  macallan 	    PM2SC_SCREEN_EN);
    601      1.3  macallan 	pm2fb_wait(sc, 8);
    602      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
    603      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
    604      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    605      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
    606      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
    607      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
    608      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
    609      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
    610      1.3  macallan 	pm2fb_wait(sc, 8);
    611      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
    612      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
    613      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
    614      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
    615      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
    616      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
    617      1.3  macallan 	sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    618      1.3  macallan 	    PM2_FB_READMODE) &
    619      1.3  macallan 	    (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
    620      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
    621      1.3  macallan 	    sc->sc_pprod);
    622      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
    623      1.3  macallan 	    sc->sc_pprod);
    624      1.3  macallan 	pm2fb_wait(sc, 8);
    625      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
    626      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
    627      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
    628      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
    629      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
    630      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
    631      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
    632      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
    633      1.3  macallan 	    0x0fff0fff);
    634      1.1  macallan 	pm2fb_flush_engine(sc);
    635      1.1  macallan }
    636      1.1  macallan 
    637      1.1  macallan static void
    638      1.1  macallan pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
    639      1.1  macallan      uint32_t colour)
    640      1.1  macallan {
    641      1.1  macallan 
    642      1.3  macallan 	pm2fb_wait(sc, 7);
    643      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    644      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    645      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    646      1.1  macallan 	    PM2RECFG_WRITE_EN);
    647      1.2  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
    648      1.1  macallan 	    colour);
    649      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    650      1.1  macallan 	    (y << 16) | x);
    651      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    652      1.1  macallan 	    (he << 16) | wi);
    653      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    654      1.2  macallan 	    PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    655      1.1  macallan }
    656      1.1  macallan 
    657      1.1  macallan static void
    658      1.1  macallan pm2fb_bitblt(struct pm2fb_softc *sc, int xs, int ys, int xd, int yd,
    659      1.1  macallan     int wi, int he, int rop)
    660      1.1  macallan {
    661      1.1  macallan 	uint32_t dir = 0;
    662      1.1  macallan 
    663      1.1  macallan 	if (yd <= ys) {
    664      1.1  macallan 		dir |= PM2RE_INC_Y;
    665      1.1  macallan 	}
    666      1.1  macallan 	if (xd <= xs) {
    667      1.1  macallan 		dir |= PM2RE_INC_X;
    668      1.1  macallan 	}
    669      1.3  macallan 	pm2fb_wait(sc, 7);
    670      1.2  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    671      1.3  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    672      1.3  macallan 	if (rop == 3) {
    673      1.3  macallan 		bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    674      1.3  macallan 		    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN | PM2RECFG_ROP_EN |
    675      1.3  macallan 		    PM2RECFG_PACKED | (rop << 6));
    676      1.3  macallan 	} else {
    677      1.3  macallan 		bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    678      1.3  macallan 		    PM2RECFG_READ_SRC | PM2RECFG_READ_DST | PM2RECFG_WRITE_EN |
    679      1.3  macallan 		    PM2RECFG_PACKED | PM2RECFG_ROP_EN | (rop << 6));
    680      1.3  macallan 	}
    681      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    682      1.1  macallan 	    (yd << 16) | xd);
    683      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    684      1.1  macallan 	    (he << 16) | wi);
    685      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
    686      1.1  macallan 	    (((ys - yd) & 0xfff) << 16) | ((xs - xd) & 0xfff));
    687      1.1  macallan 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    688      1.1  macallan 	    PM2RE_RECTANGLE | dir);
    689      1.1  macallan }
    690      1.1  macallan 
    691      1.1  macallan static void
    692      1.1  macallan pm2fb_cursor(void *cookie, int on, int row, int col)
    693      1.1  macallan {
    694      1.1  macallan 	struct rasops_info *ri = cookie;
    695      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    696      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
    697      1.1  macallan 	int x, y, wi, he;
    698      1.1  macallan 
    699      1.1  macallan 	wi = ri->ri_font->fontwidth;
    700      1.1  macallan 	he = ri->ri_font->fontheight;
    701      1.1  macallan 
    702      1.1  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    703      1.1  macallan 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    704      1.1  macallan 		y = ri->ri_crow * he + ri->ri_yorigin;
    705      1.1  macallan 		if (ri->ri_flg & RI_CURSOR) {
    706      1.1  macallan 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    707      1.1  macallan 			ri->ri_flg &= ~RI_CURSOR;
    708      1.1  macallan 		}
    709      1.1  macallan 		ri->ri_crow = row;
    710      1.1  macallan 		ri->ri_ccol = col;
    711      1.1  macallan 		if (on) {
    712      1.1  macallan 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    713      1.1  macallan 			y = ri->ri_crow * he + ri->ri_yorigin;
    714      1.1  macallan 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    715      1.1  macallan 			ri->ri_flg |= RI_CURSOR;
    716      1.1  macallan 		}
    717      1.1  macallan 	} else {
    718      1.1  macallan 		scr->scr_ri.ri_crow = row;
    719      1.1  macallan 		scr->scr_ri.ri_ccol = col;
    720      1.1  macallan 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    721      1.1  macallan 	}
    722      1.1  macallan 
    723      1.1  macallan }
    724      1.1  macallan 
    725      1.1  macallan static void
    726      1.1  macallan pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    727      1.1  macallan {
    728      1.3  macallan 	struct rasops_info *ri = cookie;
    729      1.4  macallan 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    730      1.3  macallan 	struct vcons_screen *scr = ri->ri_hw;
    731      1.3  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
    732      1.3  macallan 	uint32_t mode;
    733      1.3  macallan 
    734      1.3  macallan 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    735      1.3  macallan 		void *data;
    736      1.3  macallan 		uint32_t fg, bg;
    737      1.3  macallan 		int uc, i;
    738      1.3  macallan 		int x, y, wi, he;
    739      1.3  macallan 
    740      1.4  macallan 		wi = font->fontwidth;
    741      1.4  macallan 		he = font->fontheight;
    742      1.3  macallan 
    743      1.4  macallan 		if (!CHAR_IN_FONT(c, font))
    744      1.3  macallan 			return;
    745      1.3  macallan 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    746      1.3  macallan 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    747      1.3  macallan 		x = ri->ri_xorigin + col * wi;
    748      1.3  macallan 		y = ri->ri_yorigin + row * he;
    749      1.3  macallan 		if (c == 0x20) {
    750      1.3  macallan 			pm2fb_rectfill(sc, x, y, wi, he, bg);
    751      1.3  macallan 		} else {
    752      1.4  macallan 			uc = c - font->firstchar;
    753      1.4  macallan 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
    754      1.3  macallan 
    755      1.3  macallan 			mode = PM2RM_MASK_MIRROR;
    756      1.3  macallan 			switch (ri->ri_font->stride) {
    757      1.3  macallan 				case 1:
    758      1.3  macallan 					mode |= 3 << 7;
    759      1.3  macallan 					break;
    760      1.3  macallan 				case 2:
    761      1.3  macallan 					mode |= 2 << 7;
    762      1.3  macallan 					break;
    763      1.3  macallan 			}
    764      1.3  macallan 
    765      1.3  macallan 			pm2fb_wait(sc, 8);
    766      1.3  macallan 
    767      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    768      1.3  macallan 			    PM2_RE_MODE, mode);
    769      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    770      1.3  macallan 			    PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
    771      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    772      1.3  macallan 			    PM2_RE_BLOCK_COLOUR, bg);
    773      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    774      1.3  macallan 			    PM2_RE_RECT_START, (y << 16) | x);
    775      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    776      1.3  macallan 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
    777      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    778      1.3  macallan 			    PM2_RE_RENDER,
    779      1.3  macallan 			    PM2RE_RECTANGLE |
    780      1.3  macallan 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    781      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    782      1.3  macallan 			    PM2_RE_BLOCK_COLOUR, fg);
    783      1.3  macallan 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    784      1.3  macallan 			    PM2_RE_RENDER,
    785      1.3  macallan 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
    786      1.3  macallan 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    787      1.3  macallan 
    788      1.3  macallan 			pm2fb_wait(sc, he);
    789      1.3  macallan 			switch (ri->ri_font->stride) {
    790      1.3  macallan 			case 1: {
    791      1.3  macallan 				uint8_t *data8 = data;
    792      1.3  macallan 				uint32_t reg;
    793      1.3  macallan 				for (i = 0; i < he; i++) {
    794      1.3  macallan 					reg = *data8;
    795      1.3  macallan 					bus_space_write_4(sc->sc_memt,
    796      1.3  macallan 					    sc->sc_regh,
    797      1.3  macallan 					    PM2_RE_BITMASK, reg);
    798      1.3  macallan 					data8++;
    799      1.3  macallan 				}
    800      1.3  macallan 				break;
    801      1.3  macallan 				}
    802      1.3  macallan 			case 2: {
    803      1.3  macallan 				uint16_t *data16 = data;
    804      1.3  macallan 				uint32_t reg;
    805      1.3  macallan 				for (i = 0; i < he; i++) {
    806      1.3  macallan 					reg = *data16;
    807      1.3  macallan 					bus_space_write_4(sc->sc_memt,
    808      1.3  macallan 					    sc->sc_regh,
    809      1.3  macallan 					    PM2_RE_BITMASK, reg);
    810      1.3  macallan 					data16++;
    811      1.3  macallan 				}
    812      1.3  macallan 				break;
    813      1.3  macallan 			}
    814      1.3  macallan 			}
    815      1.3  macallan 		}
    816      1.3  macallan 	}
    817      1.1  macallan }
    818      1.1  macallan 
    819      1.1  macallan static void
    820      1.1  macallan pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    821      1.1  macallan {
    822      1.1  macallan 	struct rasops_info *ri = cookie;
    823      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    824      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
    825      1.1  macallan 	int32_t xs, xd, y, width, height;
    826      1.1  macallan 
    827      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    828      1.1  macallan 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    829      1.1  macallan 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    830      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    831      1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
    832      1.1  macallan 		height = ri->ri_font->fontheight;
    833      1.1  macallan 		pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
    834      1.1  macallan 	}
    835      1.1  macallan }
    836      1.1  macallan 
    837      1.1  macallan static void
    838      1.1  macallan pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    839      1.1  macallan {
    840      1.1  macallan 	struct rasops_info *ri = cookie;
    841      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    842      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
    843      1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
    844      1.1  macallan 
    845      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    846      1.1  macallan 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    847      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    848      1.1  macallan 		width = ri->ri_font->fontwidth * ncols;
    849      1.1  macallan 		height = ri->ri_font->fontheight;
    850      1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    851      1.1  macallan 
    852      1.1  macallan 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    853      1.1  macallan 	}
    854      1.1  macallan }
    855      1.1  macallan 
    856      1.1  macallan static void
    857      1.1  macallan pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    858      1.1  macallan {
    859      1.1  macallan 	struct rasops_info *ri = cookie;
    860      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    861      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
    862      1.1  macallan 	int32_t x, ys, yd, width, height;
    863      1.1  macallan 
    864      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    865      1.1  macallan 		x = ri->ri_xorigin;
    866      1.1  macallan 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    867      1.1  macallan 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    868      1.1  macallan 		width = ri->ri_emuwidth;
    869      1.1  macallan 		height = ri->ri_font->fontheight*nrows;
    870      1.1  macallan 		pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
    871      1.1  macallan 	}
    872      1.1  macallan }
    873      1.1  macallan 
    874      1.1  macallan static void
    875      1.1  macallan pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
    876      1.1  macallan {
    877      1.1  macallan 	struct rasops_info *ri = cookie;
    878      1.1  macallan 	struct vcons_screen *scr = ri->ri_hw;
    879      1.1  macallan 	struct pm2fb_softc *sc = scr->scr_cookie;
    880      1.1  macallan 	int32_t x, y, width, height, fg, bg, ul;
    881      1.1  macallan 
    882      1.1  macallan 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    883      1.1  macallan 		x = ri->ri_xorigin;
    884      1.1  macallan 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    885      1.1  macallan 		width = ri->ri_emuwidth;
    886      1.1  macallan 		height = ri->ri_font->fontheight * nrows;
    887      1.1  macallan 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    888      1.1  macallan 
    889      1.1  macallan 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    890      1.1  macallan 	}
    891      1.1  macallan }
    892      1.1  macallan 
    893  1.7.4.1      yamt static void
    894  1.7.4.1      yamt pm2_setup_i2c(struct pm2fb_softc *sc)
    895  1.7.4.1      yamt {
    896  1.7.4.1      yamt #ifdef PM2FB_DEBUG
    897  1.7.4.1      yamt 	struct edid_info ei;
    898  1.7.4.1      yamt #endif
    899  1.7.4.1      yamt 	int i;
    900  1.7.4.1      yamt 
    901  1.7.4.1      yamt 	/* Fill in the i2c tag */
    902  1.7.4.1      yamt 	sc->sc_i2c.ic_cookie = sc;
    903  1.7.4.1      yamt 	sc->sc_i2c.ic_acquire_bus = pm2fb_i2c_acquire_bus;
    904  1.7.4.1      yamt 	sc->sc_i2c.ic_release_bus = pm2fb_i2c_release_bus;
    905  1.7.4.1      yamt 	sc->sc_i2c.ic_send_start = pm2fb_i2c_send_start;
    906  1.7.4.1      yamt 	sc->sc_i2c.ic_send_stop = pm2fb_i2c_send_stop;
    907  1.7.4.1      yamt 	sc->sc_i2c.ic_initiate_xfer = pm2fb_i2c_initiate_xfer;
    908  1.7.4.1      yamt 	sc->sc_i2c.ic_read_byte = pm2fb_i2c_read_byte;
    909  1.7.4.1      yamt 	sc->sc_i2c.ic_write_byte = pm2fb_i2c_write_byte;
    910  1.7.4.1      yamt 	sc->sc_i2c.ic_exec = NULL;
    911  1.7.4.1      yamt 
    912  1.7.4.1      yamt 	DPRINTF("data: %08x\n", bus_space_read_4(sc->sc_memt, sc->sc_regh,
    913  1.7.4.1      yamt 		PM2_DISPLAY_DATA));
    914  1.7.4.1      yamt 
    915  1.7.4.1      yamt 	/* make sure we're in i2c mode */
    916  1.7.4.1      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, 0);
    917  1.7.4.1      yamt 
    918  1.7.4.1      yamt 	/* zero out the EDID buffer */
    919  1.7.4.1      yamt 	memset(sc->sc_edid_data, 0, 128);
    920  1.7.4.1      yamt 
    921  1.7.4.1      yamt 	/* Some monitors don't respond first time */
    922  1.7.4.1      yamt 	i = 0;
    923  1.7.4.1      yamt 	while (sc->sc_edid_data[1] == 0 && i++ < 3)
    924  1.7.4.1      yamt 		ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
    925  1.7.4.1      yamt #ifdef PM2FB_DEBUG
    926  1.7.4.1      yamt 	if (edid_parse(&sc->sc_edid_data[0], &ei) != -1) {
    927  1.7.4.1      yamt 		edid_print(&ei);
    928  1.7.4.1      yamt 	}
    929  1.7.4.1      yamt #endif
    930  1.7.4.1      yamt }
    931  1.7.4.1      yamt 
    932  1.7.4.1      yamt /* I2C bitbanging */
    933  1.7.4.1      yamt static void pm2fb_i2cbb_set_bits(void *cookie, uint32_t bits)
    934  1.7.4.1      yamt {
    935  1.7.4.1      yamt 	struct pm2fb_softc *sc = cookie;
    936  1.7.4.1      yamt 	uint32_t out;
    937  1.7.4.1      yamt 
    938  1.7.4.1      yamt 	out = bits << 2;	/* bitmasks match the IN bits */
    939  1.7.4.1      yamt 
    940  1.7.4.1      yamt 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, out);
    941  1.7.4.1      yamt }
    942  1.7.4.1      yamt 
    943  1.7.4.1      yamt static void pm2fb_i2cbb_set_dir(void *cookie, uint32_t dir)
    944  1.7.4.1      yamt {
    945  1.7.4.1      yamt 	/* Nothing to do */
    946  1.7.4.1      yamt }
    947  1.7.4.1      yamt 
    948  1.7.4.1      yamt static uint32_t pm2fb_i2cbb_read(void *cookie)
    949  1.7.4.1      yamt {
    950  1.7.4.1      yamt 	struct pm2fb_softc *sc = cookie;
    951  1.7.4.1      yamt 	uint32_t bits;
    952  1.7.4.1      yamt 
    953  1.7.4.1      yamt 	bits = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA);
    954  1.7.4.1      yamt 
    955  1.7.4.1      yamt 	return bits;
    956  1.7.4.1      yamt }
    957  1.7.4.1      yamt 
    958  1.7.4.1      yamt /* higher level I2C stuff */
    959  1.7.4.1      yamt static int
    960  1.7.4.1      yamt pm2fb_i2c_acquire_bus(void *cookie, int flags)
    961  1.7.4.1      yamt {
    962  1.7.4.1      yamt 	/* private bus */
    963  1.7.4.1      yamt 	return (0);
    964  1.7.4.1      yamt }
    965  1.7.4.1      yamt 
    966  1.7.4.1      yamt static void
    967  1.7.4.1      yamt pm2fb_i2c_release_bus(void *cookie, int flags)
    968  1.7.4.1      yamt {
    969  1.7.4.1      yamt 	/* private bus */
    970  1.7.4.1      yamt }
    971  1.7.4.1      yamt 
    972  1.7.4.1      yamt static int
    973  1.7.4.1      yamt pm2fb_i2c_send_start(void *cookie, int flags)
    974  1.7.4.1      yamt {
    975  1.7.4.1      yamt 	return (i2c_bitbang_send_start(cookie, flags, &pm2fb_i2cbb_ops));
    976  1.7.4.1      yamt }
    977  1.7.4.1      yamt 
    978  1.7.4.1      yamt static int
    979  1.7.4.1      yamt pm2fb_i2c_send_stop(void *cookie, int flags)
    980  1.7.4.1      yamt {
    981  1.7.4.1      yamt 
    982  1.7.4.1      yamt 	return (i2c_bitbang_send_stop(cookie, flags, &pm2fb_i2cbb_ops));
    983  1.7.4.1      yamt }
    984  1.7.4.1      yamt 
    985  1.7.4.1      yamt static int
    986  1.7.4.1      yamt pm2fb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    987  1.7.4.1      yamt {
    988  1.7.4.1      yamt 
    989  1.7.4.1      yamt 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
    990  1.7.4.1      yamt 	    &pm2fb_i2cbb_ops));
    991  1.7.4.1      yamt }
    992  1.7.4.1      yamt 
    993  1.7.4.1      yamt static int
    994  1.7.4.1      yamt pm2fb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
    995  1.7.4.1      yamt {
    996  1.7.4.1      yamt 	return (i2c_bitbang_read_byte(cookie, valp, flags, &pm2fb_i2cbb_ops));
    997  1.7.4.1      yamt }
    998  1.7.4.1      yamt 
    999  1.7.4.1      yamt static int
   1000  1.7.4.1      yamt pm2fb_i2c_write_byte(void *cookie, uint8_t val, int flags)
   1001  1.7.4.1      yamt {
   1002  1.7.4.1      yamt 	return (i2c_bitbang_write_byte(cookie, val, flags, &pm2fb_i2cbb_ops));
   1003  1.7.4.1      yamt }
   1004