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