Home | History | Annotate | Line # | Download | only in pci
chipsfb.c revision 1.4
      1 /*	$NetBSD: chipsfb.c,v 1.4 2006/10/16 22:36:30 macallan Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 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  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * A console driver for Chips & Technologies 65550 graphics controllers
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: chipsfb.c,v 1.4 2006/10/16 22:36:30 macallan Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 #include <sys/callout.h>
     43 #include <sys/lwp.h>
     44 #include <sys/kauth.h>
     45 
     46 #include <uvm/uvm_extern.h>
     47 
     48 #if defined(macppc) || defined (sparc64) || defined(ofppc)
     49 #define HAVE_OPENFIRMWARE
     50 #endif
     51 
     52 #ifdef HAVE_OPENFIRMWARE
     53 #include <dev/ofw/openfirm.h>
     54 #include <dev/ofw/ofw_pci.h>
     55 #endif
     56 
     57 #include <dev/videomode/videomode.h>
     58 
     59 #include <dev/pci/pcivar.h>
     60 #include <dev/pci/pcireg.h>
     61 #include <dev/pci/pcidevs.h>
     62 #include <dev/pci/pciio.h>
     63 #include <dev/pci/chipsfbreg.h>
     64 
     65 #include <dev/wscons/wsdisplayvar.h>
     66 #include <dev/wscons/wsconsio.h>
     67 #include <dev/wsfont/wsfont.h>
     68 #include <dev/rasops/rasops.h>
     69 #include <dev/wscons/wsdisplay_vconsvar.h>
     70 
     71 #include <dev/i2c/i2cvar.h>
     72 #include <dev/i2c/i2c_bitbang.h>
     73 #include <dev/i2c/edidvar.h>
     74 
     75 #include "opt_wsemul.h"
     76 
     77 struct chipsfb_softc {
     78 	struct device sc_dev;
     79 	pci_chipset_tag_t sc_pc;
     80 	pcitag_t sc_pcitag;
     81 
     82 	bus_space_tag_t sc_memt;
     83 	bus_space_tag_t sc_iot;
     84 	bus_space_handle_t sc_memh;
     85 
     86 	bus_space_tag_t sc_fbt;
     87 	bus_space_tag_t sc_ioregt;
     88 	bus_space_handle_t sc_fbh;
     89 	bus_space_handle_t sc_ioregh;
     90 	bus_addr_t sc_fb, sc_ioreg;
     91 	bus_size_t sc_fbsize, sc_ioregsize;
     92 
     93 	void *sc_ih;
     94 
     95 	size_t memsize;
     96 
     97 	int bits_per_pixel;
     98 	int width, height, linebytes;
     99 
    100 	int sc_mode;
    101 	uint32_t sc_bg;
    102 
    103 	u_char sc_cmap_red[256];
    104 	u_char sc_cmap_green[256];
    105 	u_char sc_cmap_blue[256];
    106 	int sc_dacw;
    107 
    108 	/* I2C stuff */
    109 	struct i2c_controller sc_i2c;
    110 	uint8_t sc_edid[1024];
    111 	int sc_edidbytes;	/* number of bytes read from the monitor */
    112 
    113 	struct vcons_data vd;
    114 };
    115 
    116 static struct vcons_screen chipsfb_console_screen;
    117 
    118 extern const u_char rasops_cmap[768];
    119 
    120 static int	chipsfb_match(struct device *, struct cfdata *, void *);
    121 static void	chipsfb_attach(struct device *, struct device *, void *);
    122 
    123 CFATTACH_DECL(chipsfb, sizeof(struct chipsfb_softc), chipsfb_match,
    124     chipsfb_attach, NULL, NULL);
    125 
    126 static int	chipsfb_is_console(struct pci_attach_args *);
    127 static void 	chipsfb_init(struct chipsfb_softc *);
    128 
    129 static void	chipsfb_cursor(void *, int, int, int);
    130 static void	chipsfb_copycols(void *, int, int, int, int);
    131 static void	chipsfb_erasecols(void *, int, int, int, long);
    132 static void	chipsfb_copyrows(void *, int, int, int);
    133 static void	chipsfb_eraserows(void *, int, int, long);
    134 
    135 #if 0
    136 static int	chipsfb_allocattr(void *, int, int, int, long *);
    137 static void	chipsfb_scroll(void *, void *, int);
    138 static int	chipsfb_load_font(void *, void *, struct wsdisplay_font *);
    139 #endif
    140 
    141 static int	chipsfb_putcmap(struct chipsfb_softc *,
    142 			    struct wsdisplay_cmap *);
    143 static int 	chipsfb_getcmap(struct chipsfb_softc *,
    144 			    struct wsdisplay_cmap *);
    145 static int 	chipsfb_putpalreg(struct chipsfb_softc *, uint8_t, uint8_t,
    146 			    uint8_t, uint8_t);
    147 
    148 static void	chipsfb_bitblt(struct chipsfb_softc *, int, int, int, int,
    149 			    int, int, uint8_t);
    150 static void	chipsfb_rectfill(struct chipsfb_softc *, int, int, int, int,
    151 			    int);
    152 static void	chipsfb_putchar(void *, int, int, u_int, long);
    153 static void	chipsfb_setup_mono(struct chipsfb_softc *, int, int, int,
    154 			    int, uint32_t, uint32_t);
    155 static void	chipsfb_feed(struct chipsfb_softc *, int, uint8_t *);
    156 
    157 #ifdef chipsfb_DEBUG
    158 static void	chipsfb_showpal(struct chipsfb_softc *);
    159 #endif
    160 static void	chipsfb_restore_palette(struct chipsfb_softc *);
    161 
    162 static void	chipsfb_wait_idle(struct chipsfb_softc *);
    163 
    164 static uint32_t chipsfb_probe_vram(struct chipsfb_softc *);
    165 
    166 struct wsscreen_descr chipsfb_defaultscreen = {
    167 	"default",
    168 	0, 0,
    169 	NULL,
    170 	8, 16,
    171 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    172 };
    173 
    174 const struct wsscreen_descr *_chipsfb_scrlist[] = {
    175 	&chipsfb_defaultscreen,
    176 	/* XXX other formats, graphics screen? */
    177 };
    178 
    179 struct wsscreen_list chipsfb_screenlist = {
    180 	sizeof(_chipsfb_scrlist) / sizeof(struct wsscreen_descr *), _chipsfb_scrlist
    181 };
    182 
    183 static int	chipsfb_ioctl(void *, void *, u_long, caddr_t, int,
    184 		    struct lwp *);
    185 static paddr_t	chipsfb_mmap(void *, void *, off_t, int);
    186 static void	chipsfb_clearscreen(struct chipsfb_softc *);
    187 static void	chipsfb_init_screen(void *, struct vcons_screen *, int,
    188 			    long *);
    189 
    190 
    191 struct wsdisplay_accessops chipsfb_accessops = {
    192 	chipsfb_ioctl,
    193 	chipsfb_mmap,
    194 	NULL,	/* load_font */
    195 	NULL,	/* polls */
    196 	NULL,	/* scroll */
    197 };
    198 
    199 /*
    200  * Inline functions for getting access to register aperture.
    201  */
    202 static inline void
    203 chipsfb_write32(struct chipsfb_softc *sc, uint32_t reg, uint32_t val)
    204 {
    205 	bus_space_write_4(sc->sc_fbt, sc->sc_fbh, reg, val);
    206 }
    207 
    208 static inline uint32_t
    209 chipsfb_read32(struct chipsfb_softc *sc, uint32_t reg)
    210 {
    211 	return bus_space_read_4(sc->sc_fbt, sc->sc_fbh, reg);
    212 }
    213 
    214 static inline void
    215 chipsfb_write_vga(struct chipsfb_softc *sc, uint32_t reg,  uint8_t val)
    216 {
    217 	bus_space_write_1(sc->sc_iot, sc->sc_ioregh, reg, val);
    218 }
    219 
    220 static inline uint8_t
    221 chipsfb_read_vga(struct chipsfb_softc *sc, uint32_t reg)
    222 {
    223 	return bus_space_read_1(sc->sc_iot, sc->sc_ioregh, reg);
    224 }
    225 
    226 static inline uint8_t
    227 chipsfb_read_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index)
    228 {
    229 
    230 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    231 	return chipsfb_read_vga(sc, reg | 0x0001);
    232 }
    233 
    234 static inline void
    235 chipsfb_write_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index,
    236     uint8_t val)
    237 {
    238 
    239 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    240 	chipsfb_write_vga(sc, reg | 0x0001, val);
    241 }
    242 
    243 static void
    244 chipsfb_wait_idle(struct chipsfb_softc *sc)
    245 {
    246 
    247 #ifdef CHIPSFB_DEBUG
    248 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0);
    249 #endif
    250 
    251 	/* spin until the blitter is idle */
    252 	while ((chipsfb_read32(sc, CT_BLT_CONTROL) & BLT_IS_BUSY) != 0) {
    253 	}
    254 
    255 #ifdef CHIPSFB_DEBUG
    256 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0xffffffff);
    257 #endif
    258 }
    259 
    260 static int
    261 chipsfb_match(struct device *parent, struct cfdata *match, void *aux)
    262 {
    263 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    264 
    265 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    266 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    267 		return 0;
    268 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) &&
    269 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65550))
    270 		return 100;
    271 	return 0;
    272 }
    273 
    274 static void
    275 chipsfb_attach(struct device *parent, struct device *self, void *aux)
    276 {
    277 	struct chipsfb_softc *sc = (void *)self;
    278 	struct pci_attach_args *pa = aux;
    279 	char devinfo[256];
    280 	struct wsemuldisplaydev_attach_args aa;
    281 	struct rasops_info *ri;
    282 	pcireg_t screg;
    283 	ulong defattr;
    284 	int console, width, height, node, i, j;
    285 #ifdef HAVE_OPENFIRMWARE
    286 	int linebytes, depth;
    287 #endif
    288 	uint32_t bg, fg, ul;
    289 
    290 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    291 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    292 	sc->sc_pc = pa->pa_pc;
    293 	sc->sc_pcitag = pa->pa_tag;
    294 	sc->sc_dacw = -1;
    295 
    296 	screg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    297 	screg |= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    298 	pci_conf_write(sc->sc_pc, sc->sc_pcitag,PCI_COMMAND_STATUS_REG,screg);
    299 
    300 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    301 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    302 
    303 	sc->sc_memt = pa->pa_memt;
    304 	sc->sc_iot = pa->pa_iot;
    305 
    306 	/* the framebuffer */
    307 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
    308 	    BUS_SPACE_MAP_LINEAR,
    309 	    &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    310 		printf("%s: failed to map the frame buffer.\n",
    311 		    sc->sc_dev.dv_xname);
    312 	}
    313 
    314 	/* IO-mapped registers */
    315 	if (bus_space_map(sc->sc_iot, 0x0, PAGE_SIZE, 0, &sc->sc_ioregh) != 0) {
    316 		printf("%s: failed to map IO-mapped registers.\n",
    317 		    sc->sc_dev.dv_xname);
    318 	}
    319 
    320 	sc->memsize = chipsfb_probe_vram(sc);
    321 	chipsfb_init(sc);
    322 
    323 	/* we should read these from the chip instead of depending on OF */
    324 	width = height = -1;
    325 
    326 	/* detect panel size */
    327 	width = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HSIZE_LSB);
    328 	width |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HORZ_OVERFLOW_1)
    329 	    & 0x0f) << 8;
    330 	width = (width + 1) * 8;
    331 	height = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VSIZE_LSB);
    332 	height |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VERT_OVERFLOW_1)
    333 	    & 0x0f) << 8;
    334 	height++;
    335 	printf("Panel size: %d x %d\n", width, height);
    336 
    337 #ifdef HAVE_OPENFIRMWARE
    338 	if (OF_getprop(node, "width", &width, 4) != 4)
    339 		OF_interpret("screen-width", 1, &width);
    340 	if (OF_getprop(node, "height", &height, 4) != 4)
    341 		OF_interpret("screen-height", 1, &height);
    342 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    343 		linebytes = width;			/* XXX */
    344 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    345 		depth = 8;				/* XXX */
    346 
    347 	if (width == -1 || height == -1)
    348 		return;
    349 
    350 	sc->width = width;
    351 	sc->height = height;
    352 	sc->bits_per_pixel = depth;
    353 	sc->linebytes = linebytes;
    354 	printf("%s: initial resolution %dx%d, %d bit\n", sc->sc_dev.dv_xname,
    355 	    sc->width, sc->height, sc->bits_per_pixel);
    356 #endif
    357 
    358 #ifdef notyet
    359 	/* XXX this should at least be configurable via kernel config */
    360 	chipsfb_set_videomode(sc, &videomode_list[16]);
    361 #endif
    362 
    363 	vcons_init(&sc->vd, sc, &chipsfb_defaultscreen, &chipsfb_accessops);
    364 	sc->vd.init_screen = chipsfb_init_screen;
    365 
    366 	console = chipsfb_is_console(pa);
    367 
    368 	ri = &chipsfb_console_screen.scr_ri;
    369 	if (console) {
    370 		vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
    371 		    &defattr);
    372 		chipsfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    373 
    374 		chipsfb_defaultscreen.textops = &ri->ri_ops;
    375 		chipsfb_defaultscreen.capabilities = ri->ri_caps;
    376 		chipsfb_defaultscreen.nrows = ri->ri_rows;
    377 		chipsfb_defaultscreen.ncols = ri->ri_cols;
    378 		wsdisplay_cnattach(&chipsfb_defaultscreen, ri, 0, 0, defattr);
    379 	} else {
    380 		/*
    381 		 * since we're not the console we can postpone the rest
    382 		 * until someone actually allocates a screen for us
    383 		 */
    384 #ifdef notyet
    385 		chipsfb_set_videomode(sc, &videomode_list[0]);
    386 #endif
    387 	}
    388 
    389 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    390 	sc->sc_bg = ri->ri_devcmap[bg];
    391 	chipsfb_clearscreen(sc);
    392 
    393 	printf("%s: %d MB aperture, %d MB VRAM at 0x%08x\n",
    394 	    sc->sc_dev.dv_xname, (u_int)(sc->sc_fbsize >> 20),
    395 	    sc->memsize >> 20, (u_int)sc->sc_fb);
    396 #ifdef chipsfb_DEBUG
    397 	printf("fb: %08lx\n", (ulong)ri->ri_bits);
    398 #endif
    399 
    400 	j = 0;
    401 	for (i = 0; i < 256; i++) {
    402 		chipsfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    403 		    rasops_cmap[j + 2]);
    404 		j += 3;
    405 	}
    406 
    407 	aa.console = console;
    408 	aa.scrdata = &chipsfb_screenlist;
    409 	aa.accessops = &chipsfb_accessops;
    410 	aa.accesscookie = &sc->vd;
    411 
    412 	config_found(self, &aa, wsemuldisplaydevprint);
    413 }
    414 
    415 static int
    416 chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r,
    417     uint8_t g, uint8_t b)
    418 {
    419 
    420 	sc->sc_cmap_red[index] = r;
    421 	sc->sc_cmap_green[index] = g;
    422 	sc->sc_cmap_blue[index] = b;
    423 
    424 	chipsfb_write_vga(sc, CT_DACMASK, 0xff);
    425 	chipsfb_write_vga(sc, CT_WRITEINDEX, index);
    426 	chipsfb_write_vga(sc, CT_DACDATA, r);
    427 	chipsfb_write_vga(sc, CT_DACDATA, g);
    428 	chipsfb_write_vga(sc, CT_DACDATA, b);
    429 
    430 	return 0;
    431 }
    432 
    433 static int
    434 chipsfb_putcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    435 {
    436 	u_char *r, *g, *b;
    437 	u_int index = cm->index;
    438 	u_int count = cm->count;
    439 	int i, error;
    440 	u_char rbuf[256], gbuf[256], bbuf[256];
    441 
    442 #ifdef chipsfb_DEBUG
    443 	printf("putcmap: %d %d\n",index, count);
    444 #endif
    445 	if (cm->index >= 256 || cm->count > 256 ||
    446 	    (cm->index + cm->count) > 256)
    447 		return EINVAL;
    448 	error = copyin(cm->red, &rbuf[index], count);
    449 	if (error)
    450 		return error;
    451 	error = copyin(cm->green, &gbuf[index], count);
    452 	if (error)
    453 		return error;
    454 	error = copyin(cm->blue, &bbuf[index], count);
    455 	if (error)
    456 		return error;
    457 
    458 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    459 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    460 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    461 
    462 	r = &sc->sc_cmap_red[index];
    463 	g = &sc->sc_cmap_green[index];
    464 	b = &sc->sc_cmap_blue[index];
    465 
    466 	for (i = 0; i < count; i++) {
    467 		chipsfb_putpalreg(sc, index, *r, *g, *b);
    468 		index++;
    469 		r++, g++, b++;
    470 	}
    471 	return 0;
    472 }
    473 
    474 static int
    475 chipsfb_getcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    476 {
    477 	u_int index = cm->index;
    478 	u_int count = cm->count;
    479 	int error;
    480 
    481 	if (index >= 255 || count > 256 || index + count > 256)
    482 		return EINVAL;
    483 
    484 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    485 	if (error)
    486 		return error;
    487 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    488 	if (error)
    489 		return error;
    490 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    491 	if (error)
    492 		return error;
    493 
    494 	return 0;
    495 }
    496 
    497 static int
    498 chipsfb_is_console(struct pci_attach_args *pa)
    499 {
    500 
    501 #ifdef HAVE_OPENFIRMWARE
    502 	/* check if we're the /chosen console device */
    503 	int chosen, stdout, node, us;
    504 
    505 	us = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    506 	chosen = OF_finddevice("/chosen");
    507 	OF_getprop(chosen, "stdout", &stdout, 4);
    508 	node = OF_instance_to_package(stdout);
    509 	return(us == node);
    510 #else
    511 	/* XXX how do we know we're console on i386? */
    512 	return 1;
    513 #endif
    514 }
    515 
    516 static void
    517 chipsfb_clearscreen(struct chipsfb_softc *sc)
    518 {
    519 	chipsfb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
    520 }
    521 
    522 /*
    523  * wsdisplay_emulops
    524  */
    525 
    526 static void
    527 chipsfb_cursor(void *cookie, int on, int row, int col)
    528 {
    529 	struct rasops_info *ri = cookie;
    530 	struct vcons_screen *scr = ri->ri_hw;
    531 	struct chipsfb_softc *sc = scr->scr_cookie;
    532 	int x, y, wi, he;
    533 
    534 	wi = ri->ri_font->fontwidth;
    535 	he = ri->ri_font->fontheight;
    536 
    537 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    538 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    539 		y = ri->ri_crow * he + ri->ri_yorigin;
    540 		if (ri->ri_flg & RI_CURSOR) {
    541 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    542 			ri->ri_flg &= ~RI_CURSOR;
    543 		}
    544 		ri->ri_crow = row;
    545 		ri->ri_ccol = col;
    546 		if (on) {
    547 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    548 			y = ri->ri_crow * he + ri->ri_yorigin;
    549 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    550 			ri->ri_flg |= RI_CURSOR;
    551 		}
    552 	} else {
    553 		ri->ri_flg &= ~RI_CURSOR;
    554 		ri->ri_crow = row;
    555 		ri->ri_ccol = col;
    556 	}
    557 }
    558 
    559 #if 0
    560 int
    561 chipsfb_mapchar(void *cookie, int uni, u_int *index)
    562 {
    563 	return 0;
    564 }
    565 #endif
    566 
    567 static void
    568 chipsfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    569 {
    570 	struct rasops_info *ri = cookie;
    571 	struct vcons_screen *scr = ri->ri_hw;
    572 	struct chipsfb_softc *sc = scr->scr_cookie;
    573 	int32_t xs, xd, y, width, height;
    574 
    575 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    576 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    577 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    578 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    579 		width = ri->ri_font->fontwidth * ncols;
    580 		height = ri->ri_font->fontheight;
    581 		chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
    582 	}
    583 }
    584 
    585 static void
    586 chipsfb_erasecols(void *cookie, int row, int startcol, int ncols,
    587     long fillattr)
    588 {
    589 	struct rasops_info *ri = cookie;
    590 	struct vcons_screen *scr = ri->ri_hw;
    591 	struct chipsfb_softc *sc = scr->scr_cookie;
    592 	int32_t x, y, width, height, fg, bg, ul;
    593 
    594 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    595 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    596 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    597 		width = ri->ri_font->fontwidth * ncols;
    598 		height = ri->ri_font->fontheight;
    599 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    600 
    601 		chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    602 	}
    603 }
    604 
    605 static void
    606 chipsfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    607 {
    608 	struct rasops_info *ri = cookie;
    609 	struct vcons_screen *scr = ri->ri_hw;
    610 	struct chipsfb_softc *sc = scr->scr_cookie;
    611 	int32_t x, ys, yd, width, height;
    612 
    613 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    614 		x = ri->ri_xorigin;
    615 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    616 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    617 		width = ri->ri_emuwidth;
    618 		height = ri->ri_font->fontheight * nrows;
    619 		chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY);
    620 	}
    621 }
    622 
    623 static void
    624 chipsfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    625 {
    626 	struct rasops_info *ri = cookie;
    627 	struct vcons_screen *scr = ri->ri_hw;
    628 	struct chipsfb_softc *sc = scr->scr_cookie;
    629 	int32_t x, y, width, height, fg, bg, ul;
    630 
    631 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    632 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    633 		if ((row == 0) && (nrows == ri->ri_rows)) {
    634 			/* clear the whole screen */
    635 			chipsfb_rectfill(sc, 0, 0, ri->ri_width,
    636 			    ri->ri_height, ri->ri_devcmap[bg]);
    637 		} else {
    638 			x = ri->ri_xorigin;
    639 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    640 			width = ri->ri_emuwidth;
    641 			height = ri->ri_font->fontheight * nrows;
    642 			chipsfb_rectfill(sc, x, y, width, height,
    643 			    ri->ri_devcmap[bg]);
    644 		}
    645 	}
    646 }
    647 
    648 static void
    649 chipsfb_bitblt(struct chipsfb_softc *sc, int xs, int ys, int xd, int yd,
    650     int width, int height, uint8_t rop)
    651 {
    652 	uint32_t src, dst, cmd = rop, stride, size;
    653 
    654 	cmd |= BLT_PAT_IS_SOLID;
    655 
    656 	/* we assume 8 bit for now */
    657 	src = xs + ys * sc->linebytes;
    658 	dst = xd + yd * sc->linebytes;
    659 
    660 	if (xs < xd) {
    661 		/* right-to-left operation */
    662 		cmd |= BLT_START_RIGHT;
    663 		src += sc->linebytes - 1;
    664 		dst += sc->linebytes - 1;
    665 	}
    666 
    667 	if (ys < yd) {
    668 		/* bottom-to-top operation */
    669 		cmd |= BLT_START_BOTTOM;
    670 		src += (height - 1) * sc->linebytes;
    671 		dst += (height - 1) * sc->linebytes;
    672 	}
    673 
    674 	stride = (sc->linebytes << 16) | sc->linebytes;
    675 	size = (height << 16) | width;
    676 
    677 	chipsfb_wait_idle(sc);
    678 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    679 	chipsfb_write32(sc, CT_BLT_SRCADDR, src);
    680 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    681 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    682 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    683 #ifdef CHIPSFB_WAIT
    684 	chipsfb_wait_idle(sc);
    685 #endif
    686 }
    687 
    688 static void
    689 chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width,
    690     int height, int colour)
    691 {
    692 	uint32_t dst, cmd, stride, size;
    693 
    694 	cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT;
    695 
    696 	/* we assume 8 bit for now */
    697 	dst = x + y * sc->linebytes;
    698 
    699 	stride = (sc->linebytes << 16) | sc->linebytes;
    700 	size = (height << 16) | width;
    701 
    702 	chipsfb_wait_idle(sc);
    703 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    704 	chipsfb_write32(sc, CT_BLT_SRCADDR, dst);
    705 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    706 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    707 	chipsfb_write32(sc, CT_BLT_BG, colour);
    708 	chipsfb_write32(sc, CT_BLT_FG, colour);
    709 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    710 #ifdef CHIPSFB_WAIT
    711 	chipsfb_wait_idle(sc);
    712 #endif
    713 }
    714 
    715 static void
    716 chipsfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    717 {
    718 	struct rasops_info *ri = cookie;
    719 	struct vcons_screen *scr = ri->ri_hw;
    720 	struct chipsfb_softc *sc = scr->scr_cookie;
    721 
    722 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    723 		uint8_t *data;
    724 		int fg, bg, uc;
    725 		int x, y, wi, he;
    726 
    727 		wi = ri->ri_font->fontwidth;
    728 		he = ri->ri_font->fontheight;
    729 
    730 		if (!CHAR_IN_FONT(c, ri->ri_font))
    731 			return;
    732 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    733 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    734 		x = ri->ri_xorigin + col * wi;
    735 		y = ri->ri_yorigin + row * he;
    736 		if (c == 0x20) {
    737 			chipsfb_rectfill(sc, x, y, wi, he, bg);
    738 		} else {
    739 			uc = c-ri->ri_font->firstchar;
    740 			data = (uint8_t *)ri->ri_font->data + uc *
    741 			    ri->ri_fontscale;
    742 			chipsfb_setup_mono(sc, x, y, wi, he, fg, bg);
    743 			chipsfb_feed(sc, ri->ri_font->stride * he, data);
    744 		}
    745 	}
    746 }
    747 
    748 static void
    749 chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width,
    750     int height, uint32_t fg, uint32_t bg)
    751 {
    752 	uint32_t dst, cmd, stride, size;
    753 
    754 	cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY;
    755 
    756 	/* we assume 8 bit for now */
    757 	dst = xd + yd * sc->linebytes;
    758 
    759 	stride = (sc->linebytes << 16);
    760 	size = (height << 16) | width;
    761 
    762 	chipsfb_wait_idle(sc);
    763 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    764 	chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE);
    765 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    766 	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
    767 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    768 	chipsfb_write32(sc, CT_BLT_BG, bg);
    769 	chipsfb_write32(sc, CT_BLT_FG, fg);
    770 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    771 
    772 }
    773 
    774 static void
    775 chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data)
    776 {
    777 	int i;
    778 	uint32_t latch = 0, bork;
    779 	int shift = 0;
    780 
    781 	for (i = 0; i < count; i++) {
    782 		bork = data[i];
    783 		latch |= (bork << shift);
    784 		if (shift == 24) {
    785 			chipsfb_write32(sc, CT_OFF_DATA, latch);
    786 			latch = 0;
    787 			shift = 0;
    788 		} else
    789 			shift += 8;
    790 	}
    791 
    792 	if (shift != 0) {
    793 		chipsfb_write32(sc, CT_OFF_DATA, latch);
    794 	}
    795 
    796 	/* apparently the chip wants 64bit-aligned data or it won't go idle */
    797 	if ((count + 3) & 0x04) {
    798 		chipsfb_write32(sc, CT_OFF_DATA, 0);
    799 	}
    800 #ifdef CHIPSFB_WAIT
    801 	chipsfb_wait_idle(sc);
    802 #endif
    803 }
    804 
    805 #ifdef CHIPSFB_DEBUG
    806 static void
    807 chipsfb_showpal(struct chipsfb_softc *sc)
    808 {
    809 	int i, x = 0;
    810 
    811 	for (i = 0; i < 16; i++) {
    812 		chipsfb_rectfill(sc, x, 0, 64, 64, i);
    813 		x += 64;
    814 	}
    815 }
    816 #endif
    817 
    818 #if 0
    819 static int
    820 chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    821 {
    822 
    823 	return 0;
    824 }
    825 #endif
    826 
    827 static void
    828 chipsfb_restore_palette(struct chipsfb_softc *sc)
    829 {
    830 	int i;
    831 
    832 	for (i = 0; i < 256; i++) {
    833 		chipsfb_putpalreg(sc,
    834 		   i,
    835 		   sc->sc_cmap_red[i],
    836 		   sc->sc_cmap_green[i],
    837 		   sc->sc_cmap_blue[i]);
    838 	}
    839 }
    840 
    841 /*
    842  * wsdisplay_accessops
    843  */
    844 
    845 static int
    846 chipsfb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
    847 	struct lwp *l)
    848 {
    849 	struct vcons_data *vd = v;
    850 	struct chipsfb_softc *sc = vd->cookie;
    851 	struct wsdisplay_fbinfo *wdf;
    852 	struct vcons_screen *ms = vd->active;
    853 
    854 	switch (cmd) {
    855 		case WSDISPLAYIO_GTYPE:
    856 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    857 			return 0;
    858 
    859 		case WSDISPLAYIO_GINFO:
    860 			wdf = (void *)data;
    861 			wdf->height = ms->scr_ri.ri_height;
    862 			wdf->width = ms->scr_ri.ri_width;
    863 			wdf->depth = ms->scr_ri.ri_depth;
    864 			wdf->cmsize = 256;
    865 			return 0;
    866 
    867 		case WSDISPLAYIO_GETCMAP:
    868 			return chipsfb_getcmap(sc,
    869 			    (struct wsdisplay_cmap *)data);
    870 
    871 		case WSDISPLAYIO_PUTCMAP:
    872 			return chipsfb_putcmap(sc,
    873 			    (struct wsdisplay_cmap *)data);
    874 
    875 		/* PCI config read/write passthrough. */
    876 		case PCI_IOC_CFGREAD:
    877 		case PCI_IOC_CFGWRITE:
    878 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    879 			    cmd, data, flag, l));
    880 
    881 		case WSDISPLAYIO_SMODE:
    882 			{
    883 				int new_mode = *(int*)data;
    884 				if (new_mode != sc->sc_mode) {
    885 					sc->sc_mode = new_mode;
    886 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    887 						chipsfb_restore_palette(sc);
    888 						vcons_redraw_screen(ms);
    889 					}
    890 				}
    891 			}
    892 			return 0;
    893 	}
    894 	return EPASSTHROUGH;
    895 }
    896 
    897 static paddr_t
    898 chipsfb_mmap(void *v, void *vs, off_t offset, int prot)
    899 {
    900 	struct vcons_data *vd = v;
    901 	struct chipsfb_softc *sc = vd->cookie;
    902 	struct lwp *me;
    903 	paddr_t pa;
    904 
    905 	/* 'regular' framebuffer mmap()ing */
    906 	if (offset < sc->memsize) {
    907 		pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
    908 		    BUS_SPACE_MAP_LINEAR);
    909 		return pa;
    910 	}
    911 
    912 	/*
    913 	 * restrict all other mappings to processes with superuser privileges
    914 	 * or the kernel itself
    915 	 */
    916 	me = curlwp;
    917 	if (me != NULL) {
    918 		if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
    919 		    NULL) != 0) {
    920 			printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
    921 			return -1;
    922 		}
    923 	}
    924 
    925 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    926 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    927 		    BUS_SPACE_MAP_LINEAR);
    928 		return pa;
    929 	}
    930 
    931 #ifdef macppc
    932 	/* allow mapping of IO space */
    933 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    934 		pa = bus_space_mmap(sc->sc_iot, offset - 0xf2000000, 0, prot,
    935 		    BUS_SPACE_MAP_LINEAR);
    936 		return pa;
    937 	}
    938 #endif
    939 
    940 #ifdef OFB_ALLOW_OTHERS
    941 	if (offset >= 0x80000000) {
    942 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    943 		    BUS_SPACE_MAP_LINEAR);
    944 		return pa;
    945 	}
    946 #endif
    947 	return -1;
    948 }
    949 
    950 static void
    951 chipsfb_init_screen(void *cookie, struct vcons_screen *scr,
    952     int existing, long *defattr)
    953 {
    954 	struct chipsfb_softc *sc = cookie;
    955 	struct rasops_info *ri = &scr->scr_ri;
    956 
    957 	ri->ri_depth = sc->bits_per_pixel;
    958 	ri->ri_width = sc->width;
    959 	ri->ri_height = sc->height;
    960 	ri->ri_stride = sc->width;
    961 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    962 
    963 	ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
    964 
    965 #ifdef CHIPSFB_DEBUG
    966 	printf("addr: %08lx\n", (ulong)ri->ri_bits);
    967 #endif
    968 	if (existing) {
    969 		ri->ri_flg |= RI_CLEAR;
    970 	}
    971 
    972 	rasops_init(ri, sc->height/8, sc->width/8);
    973 	ri->ri_caps = WSSCREEN_WSCOLORS;
    974 
    975 	rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
    976 		    sc->width / ri->ri_font->fontwidth);
    977 
    978 	ri->ri_hw = scr;
    979 	ri->ri_ops.copyrows = chipsfb_copyrows;
    980 	ri->ri_ops.copycols = chipsfb_copycols;
    981 	ri->ri_ops.eraserows = chipsfb_eraserows;
    982 	ri->ri_ops.erasecols = chipsfb_erasecols;
    983 	ri->ri_ops.cursor = chipsfb_cursor;
    984 	ri->ri_ops.putchar = chipsfb_putchar;
    985 }
    986 
    987 #if 0
    988 int
    989 chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    990 {
    991 
    992 	return 0;
    993 }
    994 #endif
    995 
    996 static void
    997 chipsfb_init(struct chipsfb_softc *sc)
    998 {
    999 
   1000 	chipsfb_wait_idle(sc);
   1001 
   1002 	chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_IO_CONTROL,
   1003 	    ENABLE_CRTC_EXT | ENABLE_ATTR_EXT);
   1004 	chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_ADDR_MAPPING,
   1005 	    ENABLE_LINEAR);
   1006 
   1007 	/* setup the blitter */
   1008 }
   1009 
   1010 static uint32_t
   1011 chipsfb_probe_vram(struct chipsfb_softc *sc)
   1012 {
   1013 	uint32_t ofs = 0x00080000;	/* 512kB */
   1014 
   1015 	/*
   1016 	 * advance in 0.5MB steps, see if we can read back what we wrote and
   1017 	 * if what we wrote to 0 is left untouched. Max. fb size is 4MB so
   1018 	 * we voluntarily stop there.
   1019 	 */
   1020 	chipsfb_write32(sc, 0, 0xf0f0f0f0);
   1021 	chipsfb_write32(sc, ofs, 0x0f0f0f0f);
   1022 	while ((chipsfb_read32(sc, 0) == 0xf0f0f0f0) &&
   1023 	    (chipsfb_read32(sc, ofs) == 0x0f0f0f0f) &&
   1024 	    (ofs < 0x00400000)) {
   1025 
   1026 		ofs += 0x00080000;
   1027 		chipsfb_write32(sc, ofs, 0x0f0f0f0f);
   1028 	}
   1029 
   1030 	return ofs;
   1031 }
   1032