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