Home | History | Annotate | Line # | Download | only in pci
pm2fb.c revision 1.16
      1 /*	$NetBSD: pm2fb.c,v 1.16 2012/09/05 23:19:13 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2009 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * A console driver for Permedia 2 graphics controllers
     30  * tested on sparc64 only so far
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: pm2fb.c,v 1.16 2012/09/05 23:19:13 macallan Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #include <sys/lwp.h>
     42 #include <sys/kauth.h>
     43 #include <sys/atomic.h>
     44 
     45 #include <dev/videomode/videomode.h>
     46 
     47 #include <dev/pci/pcivar.h>
     48 #include <dev/pci/pcireg.h>
     49 #include <dev/pci/pcidevs.h>
     50 #include <dev/pci/pciio.h>
     51 #include <dev/pci/pm2reg.h>
     52 
     53 #include <dev/wscons/wsdisplayvar.h>
     54 #include <dev/wscons/wsconsio.h>
     55 #include <dev/wsfont/wsfont.h>
     56 #include <dev/rasops/rasops.h>
     57 #include <dev/wscons/wsdisplay_vconsvar.h>
     58 #include <dev/pci/wsdisplay_pci.h>
     59 
     60 #include <dev/i2c/i2cvar.h>
     61 #include <dev/i2c/i2c_bitbang.h>
     62 #include <dev/i2c/ddcvar.h>
     63 #include <dev/videomode/videomode.h>
     64 #include <dev/videomode/edidvar.h>
     65 #include <dev/videomode/edidreg.h>
     66 
     67 #include "opt_pm2fb.h"
     68 
     69 #ifdef PM2FB_DEBUG
     70 #define DPRINTF aprint_error
     71 #else
     72 #define DPRINTF while (0) printf
     73 #endif
     74 
     75 struct pm2fb_softc {
     76 	device_t sc_dev;
     77 
     78 	pci_chipset_tag_t sc_pc;
     79 	pcitag_t sc_pcitag;
     80 
     81 	bus_space_tag_t sc_memt;
     82 	bus_space_tag_t sc_iot;
     83 
     84 	bus_space_handle_t sc_regh;
     85 	bus_addr_t sc_fb, sc_reg;
     86 	bus_size_t sc_fbsize, sc_regsize;
     87 
     88 	int sc_width, sc_height, sc_depth, sc_stride;
     89 	int sc_locked;
     90 	struct vcons_screen sc_console_screen;
     91 	struct wsscreen_descr sc_defaultscreen_descr;
     92 	const struct wsscreen_descr *sc_screens[1];
     93 	struct wsscreen_list sc_screenlist;
     94 	struct vcons_data vd;
     95 	int sc_mode;
     96 	u_char sc_cmap_red[256];
     97 	u_char sc_cmap_green[256];
     98 	u_char sc_cmap_blue[256];
     99 	/* engine stuff */
    100 	uint32_t sc_pprod;
    101 	int sc_is_pm2;
    102 	/* i2c stuff */
    103 	struct i2c_controller sc_i2c;
    104 	uint8_t sc_edid_data[128];
    105 	struct edid_info sc_ei;
    106 	struct videomode *sc_videomode;
    107 };
    108 
    109 static int	pm2fb_match(device_t, cfdata_t, void *);
    110 static void	pm2fb_attach(device_t, device_t, void *);
    111 
    112 CFATTACH_DECL_NEW(pm2fb, sizeof(struct pm2fb_softc),
    113     pm2fb_match, pm2fb_attach, NULL, NULL);
    114 
    115 extern const u_char rasops_cmap[768];
    116 
    117 static int	pm2fb_ioctl(void *, void *, u_long, void *, int,
    118 			     struct lwp *);
    119 static paddr_t	pm2fb_mmap(void *, void *, off_t, int);
    120 static void	pm2fb_init_screen(void *, struct vcons_screen *, int, long *);
    121 
    122 static int	pm2fb_putcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    123 static int 	pm2fb_getcmap(struct pm2fb_softc *, struct wsdisplay_cmap *);
    124 static void	pm2fb_restore_palette(struct pm2fb_softc *);
    125 static int 	pm2fb_putpalreg(struct pm2fb_softc *, uint8_t, uint8_t,
    126 			    uint8_t, uint8_t);
    127 
    128 static void	pm2fb_init(struct pm2fb_softc *);
    129 static void	pm2fb_flush_engine(struct pm2fb_softc *);
    130 static void	pm2fb_rectfill(struct pm2fb_softc *, int, int, int, int,
    131 			    uint32_t);
    132 static void	pm2fb_bitblt(struct pm2fb_softc *, int, int, int, int, int,
    133 			    int, int);
    134 
    135 static void	pm2fb_cursor(void *, int, int, int);
    136 static void	pm2fb_putchar(void *, int, int, u_int, long);
    137 static void	pm2fb_copycols(void *, int, int, int, int);
    138 static void	pm2fb_erasecols(void *, int, int, int, long);
    139 static void	pm2fb_copyrows(void *, int, int, int);
    140 static void	pm2fb_eraserows(void *, int, int, long);
    141 
    142 struct wsdisplay_accessops pm2fb_accessops = {
    143 	pm2fb_ioctl,
    144 	pm2fb_mmap,
    145 	NULL,	/* alloc_screen */
    146 	NULL,	/* free_screen */
    147 	NULL,	/* show_screen */
    148 	NULL, 	/* load_font */
    149 	NULL,	/* pollc */
    150 	NULL	/* scroll */
    151 };
    152 
    153 /* I2C glue */
    154 static int pm2fb_i2c_acquire_bus(void *, int);
    155 static void pm2fb_i2c_release_bus(void *, int);
    156 static int pm2fb_i2c_send_start(void *, int);
    157 static int pm2fb_i2c_send_stop(void *, int);
    158 static int pm2fb_i2c_initiate_xfer(void *, i2c_addr_t, int);
    159 static int pm2fb_i2c_read_byte(void *, uint8_t *, int);
    160 static int pm2fb_i2c_write_byte(void *, uint8_t, int);
    161 
    162 /* I2C bitbang glue */
    163 static void pm2fb_i2cbb_set_bits(void *, uint32_t);
    164 static void pm2fb_i2cbb_set_dir(void *, uint32_t);
    165 static uint32_t pm2fb_i2cbb_read(void *);
    166 
    167 static void pm2_setup_i2c(struct pm2fb_softc *);
    168 
    169 static const struct i2c_bitbang_ops pm2fb_i2cbb_ops = {
    170 	pm2fb_i2cbb_set_bits,
    171 	pm2fb_i2cbb_set_dir,
    172 	pm2fb_i2cbb_read,
    173 	{
    174 		PM2_DD_SDA_IN,
    175 		PM2_DD_SCL_IN,
    176 		0,
    177 		0
    178 	}
    179 };
    180 
    181 #if 0
    182 /* mode setting stuff */
    183 static int pm2fb_set_pll(struct pm2fb_softc *, int);
    184 static uint8_t pm2fb_read_dac(struct pm2fb_softc *, int);
    185 static void pm2fb_write_dac(struct pm2fb_softc *, int, uint8_t);
    186 #endif
    187 
    188 static inline void
    189 pm2fb_wait(struct pm2fb_softc *sc, int slots)
    190 {
    191 	uint32_t reg;
    192 
    193 	do {
    194 		reg = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    195 			PM2_INPUT_FIFO_SPACE);
    196 	} while (reg <= slots);
    197 }
    198 
    199 static void
    200 pm2fb_flush_engine(struct pm2fb_softc *sc)
    201 {
    202 
    203 	pm2fb_wait(sc, 2);
    204 
    205 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_FILTER_MODE,
    206 	    PM2FLT_PASS_SYNC);
    207 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SYNC, 0);
    208 	do {
    209 		while (bus_space_read_4(sc->sc_memt, sc->sc_regh,
    210 			PM2_OUTPUT_FIFO_WORDS) == 0);
    211 	} while (bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_OUTPUT_FIFO) !=
    212 	    0x188);
    213 }
    214 
    215 static int
    216 pm2fb_match(device_t parent, cfdata_t match, void *aux)
    217 {
    218 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    219 
    220 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY)
    221 		return 0;
    222 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_3DLABS)
    223 		return 0;
    224 
    225 	/* only cards tested on so far - likely need a list */
    226 	if ((PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2) ||
    227 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3DLABS_PERMEDIA2V))
    228 		return 100;
    229 	return (0);
    230 }
    231 
    232 static void
    233 pm2fb_attach(device_t parent, device_t self, void *aux)
    234 {
    235 	struct pm2fb_softc	*sc = device_private(self);
    236 	struct pci_attach_args	*pa = aux;
    237 	struct rasops_info	*ri;
    238 	struct wsemuldisplaydev_attach_args aa;
    239 	prop_dictionary_t	dict;
    240 	unsigned long		defattr;
    241 	bool			is_console;
    242 	int 			i, j;
    243 	uint32_t		flags;
    244 	uint8_t			cmap[768];
    245 
    246 	sc->sc_pc = pa->pa_pc;
    247 	sc->sc_pcitag = pa->pa_tag;
    248 	sc->sc_memt = pa->pa_memt;
    249 	sc->sc_iot = pa->pa_iot;
    250 	sc->sc_dev = self;
    251 	sc->sc_is_pm2 = (PCI_PRODUCT(pa->pa_id) ==
    252 	    PCI_PRODUCT_3DLABS_PERMEDIA2);
    253 	pci_aprint_devinfo(pa, NULL);
    254 
    255 	/* fill in parameters from properties */
    256 	dict = device_properties(self);
    257 	if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
    258 		aprint_error("%s: no width property\n", device_xname(self));
    259 		return;
    260 	}
    261 	if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
    262 		aprint_error("%s: no height property\n", device_xname(self));
    263 		return;
    264 	}
    265 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
    266 		aprint_error("%s: no depth property\n", device_xname(self));
    267 		return;
    268 	}
    269 	/*
    270 	 * don't look at the linebytes property - The Raptor firmware lies
    271 	 * about it. Get it from width * depth >> 3 instead.
    272 	 */
    273 
    274 #if 0
    275 	sc->sc_depth = 8;
    276 #endif
    277 	sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
    278 
    279 	prop_dictionary_get_bool(dict, "is_console", &is_console);
    280 
    281 	pci_mapreg_info(pa->pa_pc, pa->pa_tag, 0x14, PCI_MAPREG_TYPE_MEM,
    282 	    &sc->sc_fb, &sc->sc_fbsize, &flags);
    283 
    284 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM, 0,
    285 	    &sc->sc_memt, &sc->sc_regh, &sc->sc_reg, &sc->sc_regsize)) {
    286 		aprint_error("%s: failed to map registers.\n",
    287 		    device_xname(sc->sc_dev));
    288 	}
    289 
    290 	/*
    291 	 * XXX yeah, casting the fb address to uint32_t is formally wrong
    292 	 * but as far as I know there are no PM2 with 64bit BARs
    293 	 */
    294 	aprint_normal("%s: %d MB aperture at 0x%08x\n", device_xname(self),
    295 	    (int)(sc->sc_fbsize >> 20), (uint32_t)sc->sc_fb);
    296 
    297 	sc->sc_defaultscreen_descr = (struct wsscreen_descr){
    298 		"default",
    299 		0, 0,
    300 		NULL,
    301 		8, 16,
    302 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    303 		NULL
    304 	};
    305 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    306 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    307 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    308 	sc->sc_locked = 0;
    309 
    310 	pm2_setup_i2c(sc);
    311 
    312 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
    313 	    &pm2fb_accessops);
    314 	sc->vd.init_screen = pm2fb_init_screen;
    315 
    316 #if 0
    317 	pm2fb_write_dac(sc, PM2V_DAC_PIXEL_SIZE, PM2V_PS_8BIT);
    318 	pm2fb_write_dac(sc, PM2V_DAC_COLOR_FORMAT, PM2V_DAC_PALETTE);
    319 #endif
    320 
    321 	/* init engine here */
    322 	pm2fb_init(sc);
    323 
    324 	ri = &sc->sc_console_screen.scr_ri;
    325 
    326 	if (is_console) {
    327 		vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    328 		    &defattr);
    329 		sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    330 
    331 		pm2fb_rectfill(sc, 0, 0, sc->sc_width, sc->sc_height,
    332 		    ri->ri_devcmap[(defattr >> 16) & 0xff]);
    333 		sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    334 		sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    335 		sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    336 		sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    337 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
    338 		    defattr);
    339 		vcons_replay_msgbuf(&sc->sc_console_screen);
    340 	} else {
    341 		if (sc->sc_console_screen.scr_ri.ri_rows == 0) {
    342 			/* do some minimal setup to avoid weirdnesses later */
    343 			vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
    344 			   &defattr);
    345 		}
    346 	}
    347 
    348 	j = 0;
    349 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    350 	for (i = 0; i < 256; i++) {
    351 		sc->sc_cmap_red[i] = cmap[j];
    352 		sc->sc_cmap_green[i] = cmap[j + 1];
    353 		sc->sc_cmap_blue[i] = cmap[j + 2];
    354 		pm2fb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    355 		j += 3;
    356 	}
    357 
    358 	aa.console = is_console;
    359 	aa.scrdata = &sc->sc_screenlist;
    360 	aa.accessops = &pm2fb_accessops;
    361 	aa.accesscookie = &sc->vd;
    362 
    363 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    364 }
    365 
    366 static int
    367 pm2fb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    368 	struct lwp *l)
    369 {
    370 	struct vcons_data *vd = v;
    371 	struct pm2fb_softc *sc = vd->cookie;
    372 	struct wsdisplay_fbinfo *wdf;
    373 	struct vcons_screen *ms = vd->active;
    374 
    375 	switch (cmd) {
    376 	case WSDISPLAYIO_GTYPE:
    377 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    378 		return 0;
    379 
    380 	/* PCI config read/write passthrough. */
    381 	case PCI_IOC_CFGREAD:
    382 	case PCI_IOC_CFGWRITE:
    383 		return pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    384 		    cmd, data, flag, l);
    385 
    386 	case WSDISPLAYIO_GET_BUSID:
    387 		return wsdisplayio_busid_pci(sc->sc_dev, sc->sc_pc,
    388 		    sc->sc_pcitag, data);
    389 
    390 	case WSDISPLAYIO_GINFO:
    391 		if (ms == NULL)
    392 			return ENODEV;
    393 		wdf = (void *)data;
    394 		wdf->height = ms->scr_ri.ri_height;
    395 		wdf->width = ms->scr_ri.ri_width;
    396 		wdf->depth = ms->scr_ri.ri_depth;
    397 		wdf->cmsize = 256;
    398 		return 0;
    399 
    400 	case WSDISPLAYIO_GETCMAP:
    401 		return pm2fb_getcmap(sc,
    402 		    (struct wsdisplay_cmap *)data);
    403 
    404 	case WSDISPLAYIO_PUTCMAP:
    405 		return pm2fb_putcmap(sc,
    406 		    (struct wsdisplay_cmap *)data);
    407 
    408 	case WSDISPLAYIO_LINEBYTES:
    409 		*(u_int *)data = sc->sc_stride;
    410 		return 0;
    411 
    412 	case WSDISPLAYIO_SMODE: {
    413 		int new_mode = *(int*)data;
    414 		if (new_mode != sc->sc_mode) {
    415 			sc->sc_mode = new_mode;
    416 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    417 				pm2fb_restore_palette(sc);
    418 				vcons_redraw_screen(ms);
    419 			} else
    420 				pm2fb_flush_engine(sc);
    421 		}
    422 		}
    423 		return 0;
    424 	case WSDISPLAYIO_GET_EDID: {
    425 		struct wsdisplayio_edid_info *d = data;
    426 		d->data_size = 128;
    427 		if (d->buffer_size < 128)
    428 			return EAGAIN;
    429 		return copyout(sc->sc_edid_data, d->edid_data, 128);
    430 	}
    431 	}
    432 	return EPASSTHROUGH;
    433 }
    434 
    435 static paddr_t
    436 pm2fb_mmap(void *v, void *vs, off_t offset, int prot)
    437 {
    438 	struct vcons_data *vd = v;
    439 	struct pm2fb_softc *sc = vd->cookie;
    440 	paddr_t pa;
    441 
    442 	/* 'regular' framebuffer mmap()ing */
    443 	if (offset < sc->sc_fbsize) {
    444 		pa = bus_space_mmap(sc->sc_memt, sc->sc_fb + offset, 0, prot,
    445 		    BUS_SPACE_MAP_LINEAR);
    446 		return pa;
    447 	}
    448 
    449 	/*
    450 	 * restrict all other mappings to processes with superuser privileges
    451 	 * or the kernel itself
    452 	 */
    453 	if (kauth_authorize_machdep(kauth_cred_get(),
    454 	    KAUTH_MACHDEP_UNMANAGEDMEM,
    455 	    NULL, NULL, NULL, NULL) != 0) {
    456 		aprint_normal("%s: mmap() rejected.\n",
    457 		    device_xname(sc->sc_dev));
    458 		return -1;
    459 	}
    460 
    461 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    462 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    463 		    BUS_SPACE_MAP_LINEAR);
    464 		return pa;
    465 	}
    466 
    467 	if ((offset >= sc->sc_reg) &&
    468 	    (offset < (sc->sc_reg + sc->sc_regsize))) {
    469 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    470 		    BUS_SPACE_MAP_LINEAR);
    471 		return pa;
    472 	}
    473 
    474 #ifdef PCI_MAGIC_IO_RANGE
    475 	/* allow mapping of IO space */
    476 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    477 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    478 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    479 		    0, prot, BUS_SPACE_MAP_LINEAR);
    480 		return pa;
    481 	}
    482 #endif
    483 
    484 	return -1;
    485 }
    486 
    487 static void
    488 pm2fb_init_screen(void *cookie, struct vcons_screen *scr,
    489     int existing, long *defattr)
    490 {
    491 	struct pm2fb_softc *sc = cookie;
    492 	struct rasops_info *ri = &scr->scr_ri;
    493 
    494 	ri->ri_depth = sc->sc_depth;
    495 	ri->ri_width = sc->sc_width;
    496 	ri->ri_height = sc->sc_height;
    497 	ri->ri_stride = sc->sc_stride;
    498 	ri->ri_flg = RI_CENTER;
    499 	if (sc->sc_depth == 8)
    500 		ri->ri_flg |= RI_8BIT_IS_RGB /*| RI_ENABLE_ALPHA*/;
    501 
    502 	rasops_init(ri, 0, 0);
    503 	ri->ri_caps = WSSCREEN_WSCOLORS;
    504 
    505 	rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
    506 		    sc->sc_width / ri->ri_font->fontwidth);
    507 
    508 	ri->ri_hw = scr;
    509 	ri->ri_ops.copyrows = pm2fb_copyrows;
    510 	ri->ri_ops.copycols = pm2fb_copycols;
    511 	ri->ri_ops.cursor = pm2fb_cursor;
    512 	ri->ri_ops.eraserows = pm2fb_eraserows;
    513 	ri->ri_ops.erasecols = pm2fb_erasecols;
    514 	ri->ri_ops.putchar = pm2fb_putchar;
    515 }
    516 
    517 static int
    518 pm2fb_putcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    519 {
    520 	u_char *r, *g, *b;
    521 	u_int index = cm->index;
    522 	u_int count = cm->count;
    523 	int i, error;
    524 	u_char rbuf[256], gbuf[256], bbuf[256];
    525 
    526 #ifdef PM2FB_DEBUG
    527 	aprint_debug("putcmap: %d %d\n",index, count);
    528 #endif
    529 	if (cm->index >= 256 || cm->count > 256 ||
    530 	    (cm->index + cm->count) > 256)
    531 		return EINVAL;
    532 	error = copyin(cm->red, &rbuf[index], count);
    533 	if (error)
    534 		return error;
    535 	error = copyin(cm->green, &gbuf[index], count);
    536 	if (error)
    537 		return error;
    538 	error = copyin(cm->blue, &bbuf[index], count);
    539 	if (error)
    540 		return error;
    541 
    542 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    543 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    544 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    545 
    546 	r = &sc->sc_cmap_red[index];
    547 	g = &sc->sc_cmap_green[index];
    548 	b = &sc->sc_cmap_blue[index];
    549 
    550 	for (i = 0; i < count; i++) {
    551 		pm2fb_putpalreg(sc, index, *r, *g, *b);
    552 		index++;
    553 		r++, g++, b++;
    554 	}
    555 	return 0;
    556 }
    557 
    558 static int
    559 pm2fb_getcmap(struct pm2fb_softc *sc, struct wsdisplay_cmap *cm)
    560 {
    561 	u_int index = cm->index;
    562 	u_int count = cm->count;
    563 	int error;
    564 
    565 	if (index >= 255 || count > 256 || index + count > 256)
    566 		return EINVAL;
    567 
    568 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    569 	if (error)
    570 		return error;
    571 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    572 	if (error)
    573 		return error;
    574 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    575 	if (error)
    576 		return error;
    577 
    578 	return 0;
    579 }
    580 
    581 static void
    582 pm2fb_restore_palette(struct pm2fb_softc *sc)
    583 {
    584 	int i;
    585 
    586 	for (i = 0; i < (1 << sc->sc_depth); i++) {
    587 		pm2fb_putpalreg(sc, i, sc->sc_cmap_red[i],
    588 		    sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
    589 	}
    590 }
    591 
    592 static int
    593 pm2fb_putpalreg(struct pm2fb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
    594     uint8_t b)
    595 {
    596 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_PAL_WRITE_IDX, idx);
    597 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, r);
    598 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, g);
    599 	bus_space_write_1(sc->sc_memt, sc->sc_regh, PM2_DAC_DATA, b);
    600 	return 0;
    601 }
    602 
    603 #if 0
    604 static uint8_t
    605 pm2fb_read_dac(struct pm2fb_softc *sc, int reg)
    606 {
    607 	if (sc->sc_is_pm2) {
    608 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    609 		    PM2_DAC_PAL_WRITE_IDX, reg);
    610 		return bus_space_read_1(sc->sc_memt, sc->sc_regh,
    611 		    PM2_DAC_INDEX_DATA);
    612 	} else {
    613 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    614 		    PM2V_DAC_INDEX_LOW, reg & 0xff);
    615 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    616 		    PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
    617 		return bus_space_read_1(sc->sc_memt, sc->sc_regh,
    618 		    PM2V_DAC_INDEX_DATA);
    619 	}
    620 }
    621 
    622 static void
    623 pm2fb_write_dac(struct pm2fb_softc *sc, int reg, uint8_t data)
    624 {
    625 	if (sc->sc_is_pm2) {
    626 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    627 		    PM2_DAC_PAL_WRITE_IDX, reg);
    628 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    629 		    PM2_DAC_INDEX_DATA, data);
    630 	} else {
    631 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    632 		    PM2V_DAC_INDEX_LOW, reg & 0xff);
    633 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    634 		    PM2V_DAC_INDEX_HIGH, (reg >> 8) & 0xff);
    635 		bus_space_write_1(sc->sc_memt, sc->sc_regh,
    636 		    PM2V_DAC_INDEX_DATA, data);
    637 	}
    638 }
    639 #endif
    640 
    641 static void
    642 pm2fb_init(struct pm2fb_softc *sc)
    643 {
    644 	pm2fb_flush_engine(sc);
    645 
    646 	pm2fb_wait(sc, 8);
    647 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SCREEN_BASE, 0);
    648 #if 0
    649 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_BYPASS_MASK,
    650 		0xffffffff);
    651 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_WRITE_MASK,
    652 		0xffffffff);
    653 #endif
    654 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_HW_WRITEMASK,
    655 		0xffffffff);
    656 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_SW_WRITEMASK,
    657 		0xffffffff);
    658 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WRITE_MODE,
    659 		PM2WM_WRITE_EN);
    660 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCREENSIZE,
    661 	    (sc->sc_height << 16) | sc->sc_width);
    662 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MODE,
    663 	    PM2SC_SCREEN_EN);
    664 	pm2fb_wait(sc, 8);
    665 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DITHER_MODE, 0);
    666 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ALPHA_MODE, 0);
    667 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    668 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_COLOUR_MODE, 0);
    669 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_ADDRESS_MODE, 0);
    670 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_READ_MODE, 0);
    671 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEX_LUT_MODE, 0);
    672 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_YUV_MODE, 0);
    673 	pm2fb_wait(sc, 8);
    674 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH_MODE, 0);
    675 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DEPTH, 0);
    676 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STENCIL_MODE, 0);
    677 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STIPPLE_MODE, 0);
    678 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_ROP_MODE, 0);
    679 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_WINDOW_ORIGIN, 0);
    680 	sc->sc_pprod = bus_space_read_4(sc->sc_memt, sc->sc_regh,
    681 	    PM2_FB_READMODE) &
    682 	    (PM2FB_PP0_MASK | PM2FB_PP1_MASK | PM2FB_PP2_MASK);
    683 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_FB_READMODE,
    684 	    sc->sc_pprod);
    685 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_TEXMAP_FORMAT,
    686 	    sc->sc_pprod);
    687 	pm2fb_wait(sc, 9);
    688 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DY, 1 << 16);
    689 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DXDOM, 0);
    690 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXDOM, 0);
    691 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTXSUB, 0);
    692 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_STARTY, 0);
    693 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_COUNT, 0);
    694 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MINYX, 0);
    695 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SCISSOR_MAXYX,
    696 	    0x0fff0fff);
    697 	switch(sc->sc_depth) {
    698 		case 8:
    699 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    700 			    PM2_RE_PIXEL_SIZE, PM2PS_8BIT);
    701 			break;
    702 		case 16:
    703 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    704 			    PM2_RE_PIXEL_SIZE, PM2PS_16BIT);
    705 			break;
    706 		case 32:
    707 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    708 			    PM2_RE_PIXEL_SIZE, PM2PS_32BIT);
    709 			break;
    710 	}
    711 	pm2fb_flush_engine(sc);
    712 	DPRINTF("pixel size: %08x\n", bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_RE_PIXEL_SIZE));
    713 }
    714 
    715 static void
    716 pm2fb_rectfill(struct pm2fb_softc *sc, int x, int y, int wi, int he,
    717      uint32_t colour)
    718 {
    719 
    720 	pm2fb_wait(sc, 7);
    721 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    722 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    723 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_CONFIG,
    724 	    PM2RECFG_WRITE_EN);
    725 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_BLOCK_COLOUR,
    726 	    colour);
    727 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    728 	    (y << 16) | x);
    729 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    730 	    (he << 16) | wi);
    731 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    732 	    PM2RE_RECTANGLE | PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    733 }
    734 
    735 static void
    736 pm2fb_bitblt(struct pm2fb_softc *sc, int xs, int ys, int xd, int yd,
    737     int wi, int he, int rop)
    738 {
    739 	uint32_t dir = 0;
    740 	int rxs, rxd, rwi;
    741 
    742 	if (yd <= ys) {
    743 		dir |= PM2RE_INC_Y;
    744 	}
    745 	if (xd <= xs) {
    746 		dir |= PM2RE_INC_X;
    747 	}
    748 	pm2fb_wait(sc, 8);
    749 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_DDA_MODE, 0);
    750 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_MODE, 0);
    751 	if (sc->sc_depth == 8) {
    752 		int adjust;
    753 		/*
    754 		 * use packed mode for some extra speed
    755 		 * this copies 32bit quantities even in 8 bit mode, so we need
    756 		 * to adjust for cases where the lower two bits in source and
    757 		 * destination X don't align, and/or where the width isn't a
    758 		 * multiple of 4
    759 		 */
    760 		if (rop == 3) {
    761 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    762 			    PM2_RE_CONFIG,
    763 			    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
    764 			    PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
    765 		} else {
    766 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    767 			    PM2_RE_CONFIG,
    768 			    PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
    769 			    PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
    770 			    PM2RECFG_ROP_EN | (rop << 6));
    771 		}
    772 		rxs = xs >> 2;
    773 		rxd = xd >> 2;
    774 		rwi = wi >> 2;
    775 		/* adjust for non-aligned x */
    776 		adjust = (xs & 3) - (xd & 3);
    777 		bus_space_write_4(sc->sc_memt, sc->sc_regh,
    778 		    PM2_RE_PACKEDDATA_LIMIT,
    779 		    (xd << 16) | (xd + wi) | (adjust << 29));
    780 	} else {
    781 		/* we're in 16 or 32bit mode */
    782 		if (rop == 3) {
    783 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    784 			    PM2_RE_CONFIG,
    785 			    PM2RECFG_READ_SRC | PM2RECFG_WRITE_EN |
    786 			    PM2RECFG_ROP_EN | PM2RECFG_PACKED | (rop << 6));
    787 		} else {
    788 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    789 			    PM2_RE_CONFIG,
    790 			    PM2RECFG_READ_SRC | PM2RECFG_READ_DST |
    791 			    PM2RECFG_WRITE_EN | PM2RECFG_PACKED |
    792 			    PM2RECFG_ROP_EN | (rop << 6));
    793 		}
    794 		rxs = xs;
    795 		rxd = xd;
    796 		rwi = wi;
    797 	}
    798 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_START,
    799 	    (yd << 16) | rxd);
    800 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RECT_SIZE,
    801 	    (he << 16) | rwi);
    802 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_SOURCE_DELTA,
    803 	    (((ys - yd) & 0xfff) << 16) | ((rxs - rxd) & 0xfff));
    804 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_RE_RENDER,
    805 	    PM2RE_RECTANGLE | dir);
    806 }
    807 
    808 static void
    809 pm2fb_cursor(void *cookie, int on, int row, int col)
    810 {
    811 	struct rasops_info *ri = cookie;
    812 	struct vcons_screen *scr = ri->ri_hw;
    813 	struct pm2fb_softc *sc = scr->scr_cookie;
    814 	int x, y, wi, he;
    815 
    816 	wi = ri->ri_font->fontwidth;
    817 	he = ri->ri_font->fontheight;
    818 
    819 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    820 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    821 		y = ri->ri_crow * he + ri->ri_yorigin;
    822 		if (ri->ri_flg & RI_CURSOR) {
    823 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    824 			ri->ri_flg &= ~RI_CURSOR;
    825 		}
    826 		ri->ri_crow = row;
    827 		ri->ri_ccol = col;
    828 		if (on) {
    829 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    830 			y = ri->ri_crow * he + ri->ri_yorigin;
    831 			pm2fb_bitblt(sc, x, y, x, y, wi, he, 12);
    832 			ri->ri_flg |= RI_CURSOR;
    833 		}
    834 	} else {
    835 		scr->scr_ri.ri_crow = row;
    836 		scr->scr_ri.ri_ccol = col;
    837 		scr->scr_ri.ri_flg &= ~RI_CURSOR;
    838 	}
    839 
    840 }
    841 
    842 static void
    843 pm2fb_putchar(void *cookie, int row, int col, u_int c, long attr)
    844 {
    845 	struct rasops_info *ri = cookie;
    846 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    847 	struct vcons_screen *scr = ri->ri_hw;
    848 	struct pm2fb_softc *sc = scr->scr_cookie;
    849 	uint32_t mode;
    850 
    851 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    852 		void *data;
    853 		uint32_t fg, bg;
    854 		int uc, i;
    855 		int x, y, wi, he;
    856 
    857 		wi = font->fontwidth;
    858 		he = font->fontheight;
    859 
    860 		if (!CHAR_IN_FONT(c, font))
    861 			return;
    862 		bg = ri->ri_devcmap[(attr >> 16) & 0xf];
    863 		fg = ri->ri_devcmap[(attr >> 24) & 0xf];
    864 		x = ri->ri_xorigin + col * wi;
    865 		y = ri->ri_yorigin + row * he;
    866 		if (c == 0x20) {
    867 			pm2fb_rectfill(sc, x, y, wi, he, bg);
    868 		} else {
    869 			uc = c - font->firstchar;
    870 			data = (uint8_t *)font->data + uc * ri->ri_fontscale;
    871 
    872 			mode = PM2RM_MASK_MIRROR;
    873 			switch (ri->ri_font->stride) {
    874 				case 1:
    875 					mode |= 3 << 7;
    876 					break;
    877 				case 2:
    878 					mode |= 2 << 7;
    879 					break;
    880 			}
    881 
    882 			pm2fb_wait(sc, 8);
    883 
    884 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    885 			    PM2_RE_MODE, mode);
    886 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    887 			    PM2_RE_CONFIG, PM2RECFG_WRITE_EN);
    888 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    889 			    PM2_RE_BLOCK_COLOUR, bg);
    890 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    891 			    PM2_RE_RECT_START, (y << 16) | x);
    892 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    893 			    PM2_RE_RECT_SIZE, (he << 16) | wi);
    894 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    895 			    PM2_RE_RENDER,
    896 			    PM2RE_RECTANGLE |
    897 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    898 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    899 			    PM2_RE_BLOCK_COLOUR, fg);
    900 			bus_space_write_4(sc->sc_memt, sc->sc_regh,
    901 			    PM2_RE_RENDER,
    902 			    PM2RE_RECTANGLE | PM2RE_SYNC_ON_MASK |
    903 			    PM2RE_INC_X | PM2RE_INC_Y | PM2RE_FASTFILL);
    904 
    905 			pm2fb_wait(sc, he);
    906 			switch (ri->ri_font->stride) {
    907 			case 1: {
    908 				uint8_t *data8 = data;
    909 				uint32_t reg;
    910 				for (i = 0; i < he; i++) {
    911 					reg = *data8;
    912 					bus_space_write_4(sc->sc_memt,
    913 					    sc->sc_regh,
    914 					    PM2_RE_BITMASK, reg);
    915 					data8++;
    916 				}
    917 				break;
    918 				}
    919 			case 2: {
    920 				uint16_t *data16 = data;
    921 				uint32_t reg;
    922 				for (i = 0; i < he; i++) {
    923 					reg = *data16;
    924 					bus_space_write_4(sc->sc_memt,
    925 					    sc->sc_regh,
    926 					    PM2_RE_BITMASK, reg);
    927 					data16++;
    928 				}
    929 				break;
    930 			}
    931 			}
    932 		}
    933 	}
    934 }
    935 
    936 static void
    937 pm2fb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    938 {
    939 	struct rasops_info *ri = cookie;
    940 	struct vcons_screen *scr = ri->ri_hw;
    941 	struct pm2fb_softc *sc = scr->scr_cookie;
    942 	int32_t xs, xd, y, width, height;
    943 
    944 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    945 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    946 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    947 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    948 		width = ri->ri_font->fontwidth * ncols;
    949 		height = ri->ri_font->fontheight;
    950 		pm2fb_bitblt(sc, xs, y, xd, y, width, height, 3);
    951 	}
    952 }
    953 
    954 static void
    955 pm2fb_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
    956 {
    957 	struct rasops_info *ri = cookie;
    958 	struct vcons_screen *scr = ri->ri_hw;
    959 	struct pm2fb_softc *sc = scr->scr_cookie;
    960 	int32_t x, y, width, height, fg, bg, ul;
    961 
    962 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    963 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    964 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    965 		width = ri->ri_font->fontwidth * ncols;
    966 		height = ri->ri_font->fontheight;
    967 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    968 
    969 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    970 	}
    971 }
    972 
    973 static void
    974 pm2fb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    975 {
    976 	struct rasops_info *ri = cookie;
    977 	struct vcons_screen *scr = ri->ri_hw;
    978 	struct pm2fb_softc *sc = scr->scr_cookie;
    979 	int32_t x, ys, yd, width, height;
    980 
    981 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
    982 		x = ri->ri_xorigin;
    983 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    984 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    985 		width = ri->ri_emuwidth;
    986 		height = ri->ri_font->fontheight*nrows;
    987 		pm2fb_bitblt(sc, x, ys, x, yd, width, height, 3);
    988 	}
    989 }
    990 
    991 static void
    992 pm2fb_eraserows(void *cookie, int row, int nrows, long fillattr)
    993 {
    994 	struct rasops_info *ri = cookie;
    995 	struct vcons_screen *scr = ri->ri_hw;
    996 	struct pm2fb_softc *sc = scr->scr_cookie;
    997 	int32_t x, y, width, height, fg, bg, ul;
    998 
    999 	if ((sc->sc_locked == 0) && (sc->sc_mode == WSDISPLAYIO_MODE_EMUL)) {
   1000 		x = ri->ri_xorigin;
   1001 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
   1002 		width = ri->ri_emuwidth;
   1003 		height = ri->ri_font->fontheight * nrows;
   1004 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
   1005 
   1006 		pm2fb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
   1007 	}
   1008 }
   1009 
   1010 #define MODE_IS_VALID(m) (((m)->hdisplay < 2048) && ((m)->dot_clock < 230000))
   1011 static void
   1012 pm2_setup_i2c(struct pm2fb_softc *sc)
   1013 {
   1014 	int i;
   1015 #ifdef PM2FB_DEBUG
   1016 	int j;
   1017 #endif
   1018 
   1019 	/* Fill in the i2c tag */
   1020 	sc->sc_i2c.ic_cookie = sc;
   1021 	sc->sc_i2c.ic_acquire_bus = pm2fb_i2c_acquire_bus;
   1022 	sc->sc_i2c.ic_release_bus = pm2fb_i2c_release_bus;
   1023 	sc->sc_i2c.ic_send_start = pm2fb_i2c_send_start;
   1024 	sc->sc_i2c.ic_send_stop = pm2fb_i2c_send_stop;
   1025 	sc->sc_i2c.ic_initiate_xfer = pm2fb_i2c_initiate_xfer;
   1026 	sc->sc_i2c.ic_read_byte = pm2fb_i2c_read_byte;
   1027 	sc->sc_i2c.ic_write_byte = pm2fb_i2c_write_byte;
   1028 	sc->sc_i2c.ic_exec = NULL;
   1029 
   1030 	DPRINTF("data: %08x\n", bus_space_read_4(sc->sc_memt, sc->sc_regh,
   1031 		PM2_DISPLAY_DATA));
   1032 
   1033 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, 0);
   1034 
   1035 	/* zero out the EDID buffer */
   1036 	memset(sc->sc_edid_data, 0, 128);
   1037 
   1038 	/* Some monitors don't respond first time */
   1039 	i = 0;
   1040 	while (sc->sc_edid_data[1] == 0 && i < 10) {
   1041 		ddc_read_edid(&sc->sc_i2c, sc->sc_edid_data, 128);
   1042 		printf("\n");
   1043 		i++;
   1044 	}
   1045 #ifdef PM2FB_DEBUG
   1046 	printf("i = %d\n", i);
   1047 	for (i = 0; i < 128; i += 16) {
   1048 		printf("%02x:", i);
   1049 		for (j = 0; j < 16; j++)
   1050 			printf(" %02x", sc->sc_edid_data[i + j]);
   1051 		printf("\n");
   1052 	}
   1053 #endif
   1054 #if 0
   1055 	if (edid_parse(&sc->sc_edid_data[0], &sc->sc_ei) != -1) {
   1056 #ifdef PM2FB_DEBUG
   1057 		edid_print(&sc->sc_ei);
   1058 #endif
   1059 
   1060 		/*
   1061 		 * Now pick a mode.
   1062 		 */
   1063 		if ((sc->sc_ei.edid_preferred_mode != NULL)) {
   1064 			struct videomode *m = sc->sc_ei.edid_preferred_mode;
   1065 			if (MODE_IS_VALID(m)) {
   1066 				sc->sc_videomode = m;
   1067 			} else {
   1068 				aprint_error_dev(sc->sc_dev,
   1069 				    "unable to use preferred mode\n");
   1070 			}
   1071 		}
   1072 		/*
   1073 		 * if we can't use the preferred mode go look for the
   1074 		 * best one we can support
   1075 		 */
   1076 		if (sc->sc_videomode == NULL) {
   1077 			int n;
   1078 			struct videomode *m = sc->sc_ei.edid_modes;
   1079 
   1080 			sort_modes(sc->sc_ei.edid_modes,
   1081 			 	    &sc->sc_ei.edid_preferred_mode,
   1082 				    sc->sc_ei.edid_nmodes);
   1083 			while ((sc->sc_videomode == NULL) &&
   1084 			       (n < sc->sc_ei.edid_nmodes)) {
   1085 				if (MODE_IS_VALID(&m[n])) {
   1086 					sc->sc_videomode = &m[n];
   1087 				}
   1088 				n++;
   1089 			}
   1090 		}
   1091 	}
   1092 	if (sc->sc_videomode != NULL) {
   1093 		pm2fb_set_pll(sc, sc->sc_videomode->dot_clock);
   1094 		if (sc->sc_is_pm2) {
   1095 			printf("DAC: %02x\n",
   1096 			    pm2fb_read_dac(sc, PM2_DAC_COLOR_MODE));
   1097 		} else
   1098 			printf("DAC: %02x\n",
   1099 			    pm2fb_read_dac(sc, PM2V_DAC_COLOR_FORMAT));
   1100 		if (0) pm2fb_write_dac(sc, PM2_DAC_COLOR_MODE, CM_PALETTE);
   1101 	}
   1102 #endif
   1103 }
   1104 
   1105 /* I2C bitbanging */
   1106 static void pm2fb_i2cbb_set_bits(void *cookie, uint32_t bits)
   1107 {
   1108 	struct pm2fb_softc *sc = cookie;
   1109 	uint32_t out;
   1110 
   1111 	out = bits << 2;	/* bitmasks match the IN bits */
   1112 
   1113 	bus_space_write_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA, out);
   1114 	delay(100);
   1115 }
   1116 
   1117 static void pm2fb_i2cbb_set_dir(void *cookie, uint32_t dir)
   1118 {
   1119 	/* Nothing to do */
   1120 }
   1121 
   1122 static uint32_t pm2fb_i2cbb_read(void *cookie)
   1123 {
   1124 	struct pm2fb_softc *sc = cookie;
   1125 	uint32_t bits;
   1126 
   1127 	bits = bus_space_read_4(sc->sc_memt, sc->sc_regh, PM2_DISPLAY_DATA);
   1128 	return bits;
   1129 }
   1130 
   1131 /* higher level I2C stuff */
   1132 static int
   1133 pm2fb_i2c_acquire_bus(void *cookie, int flags)
   1134 {
   1135 	/* private bus */
   1136 	return (0);
   1137 }
   1138 
   1139 static void
   1140 pm2fb_i2c_release_bus(void *cookie, int flags)
   1141 {
   1142 	/* private bus */
   1143 }
   1144 
   1145 static int
   1146 pm2fb_i2c_send_start(void *cookie, int flags)
   1147 {
   1148 	return (i2c_bitbang_send_start(cookie, flags, &pm2fb_i2cbb_ops));
   1149 }
   1150 
   1151 static int
   1152 pm2fb_i2c_send_stop(void *cookie, int flags)
   1153 {
   1154 
   1155 	return (i2c_bitbang_send_stop(cookie, flags, &pm2fb_i2cbb_ops));
   1156 }
   1157 
   1158 static int
   1159 pm2fb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
   1160 {
   1161 
   1162 	return (i2c_bitbang_initiate_xfer(cookie, addr, flags,
   1163 	    &pm2fb_i2cbb_ops));
   1164 }
   1165 
   1166 static int
   1167 pm2fb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
   1168 {
   1169 	return (i2c_bitbang_read_byte(cookie, valp, flags, &pm2fb_i2cbb_ops));
   1170 }
   1171 
   1172 static int
   1173 pm2fb_i2c_write_byte(void *cookie, uint8_t val, int flags)
   1174 {
   1175 	return (i2c_bitbang_write_byte(cookie, val, flags, &pm2fb_i2cbb_ops));
   1176 }
   1177 
   1178 #if 0
   1179 
   1180 #define RefClk 14318	/* all frequencies are in kHz */
   1181 static int
   1182 pm2fb_set_pll(struct pm2fb_softc *sc, int freq)
   1183 {
   1184 	int m, n, p, diff, out_freq, bm, bn, bp, bdiff = 1000000, bfreq;
   1185 	int fi;
   1186 
   1187 	/*
   1188 	 * this should work on both PM2 and PM2V
   1189 	 */
   1190 	for (m = 0; m < 256; m++) {
   1191 		for (n = 2; n < 14; n++) {
   1192 			fi = RefClk * m / n;
   1193 			if ((fi < 110000) || (fi > 250000))
   1194 				continue;
   1195 			for (p = 0; p < 4; p++) {
   1196 				out_freq = fi >> p;
   1197 				diff = abs(out_freq - freq);
   1198 				if (diff < bdiff) {
   1199 					bdiff = diff;
   1200 					bfreq = out_freq;
   1201 					bm = m;
   1202 					bn = n;
   1203 					bp = p;
   1204 				}
   1205 			}
   1206 		}
   1207 	}
   1208 	printf("best: %d kHz ( %d off ), %d %d %d\n", bfreq, bdiff, bm, bn, bp);
   1209 	return 0;
   1210 }
   1211 #endif
   1212 
   1213