Home | History | Annotate | Line # | Download | only in pci
chipsfb.c revision 1.3
      1 /*	$NetBSD: chipsfb.c,v 1.3 2006/09/27 06:39:38 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.3 2006/09/27 06:39:38 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 struct wsscreen_descr chipsfb_defaultscreen = {
    165 	"default",
    166 	0, 0,
    167 	NULL,
    168 	8, 16,
    169 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    170 };
    171 
    172 const struct wsscreen_descr *_chipsfb_scrlist[] = {
    173 	&chipsfb_defaultscreen,
    174 	/* XXX other formats, graphics screen? */
    175 };
    176 
    177 struct wsscreen_list chipsfb_screenlist = {
    178 	sizeof(_chipsfb_scrlist) / sizeof(struct wsscreen_descr *), _chipsfb_scrlist
    179 };
    180 
    181 static int	chipsfb_ioctl(void *, void *, u_long, caddr_t, int,
    182 		    struct lwp *);
    183 static paddr_t	chipsfb_mmap(void *, void *, off_t, int);
    184 static void	chipsfb_clearscreen(struct chipsfb_softc *);
    185 static void	chipsfb_init_screen(void *, struct vcons_screen *, int,
    186 			    long *);
    187 
    188 
    189 struct wsdisplay_accessops chipsfb_accessops = {
    190 	chipsfb_ioctl,
    191 	chipsfb_mmap,
    192 	NULL,	/* load_font */
    193 	NULL,	/* polls */
    194 	NULL,	/* scroll */
    195 };
    196 
    197 /*
    198  * Inline functions for getting access to register aperture.
    199  */
    200 static inline void
    201 chipsfb_write32(struct chipsfb_softc *sc, uint32_t reg, uint32_t val)
    202 {
    203 	bus_space_write_4(sc->sc_fbt, sc->sc_fbh, reg, val);
    204 }
    205 
    206 static inline uint32_t
    207 chipsfb_read32(struct chipsfb_softc *sc, uint32_t reg)
    208 {
    209 	return bus_space_read_4(sc->sc_fbt, sc->sc_fbh, reg);
    210 }
    211 
    212 static inline void
    213 chipsfb_write_vga(struct chipsfb_softc *sc, uint32_t reg,  uint8_t val)
    214 {
    215 	bus_space_write_1(sc->sc_iot, sc->sc_ioregh, reg, val);
    216 }
    217 
    218 static inline uint8_t
    219 chipsfb_read_vga(struct chipsfb_softc *sc, uint32_t reg)
    220 {
    221 	return bus_space_read_1(sc->sc_iot, sc->sc_ioregh, reg);
    222 }
    223 
    224 static inline uint8_t
    225 chipsfb_read_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index)
    226 {
    227 
    228 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    229 	return chipsfb_read_vga(sc, reg | 0x0001);
    230 }
    231 
    232 static inline void
    233 chipsfb_write_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index,
    234     uint8_t val)
    235 {
    236 
    237 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    238 	chipsfb_write_vga(sc, reg | 0x0001, val);
    239 }
    240 
    241 static void
    242 chipsfb_wait_idle(struct chipsfb_softc *sc)
    243 {
    244 
    245 #ifdef CHIPSFB_DEBUG
    246 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0);
    247 #endif
    248 
    249 	/* spin until the blitter is idle */
    250 	while ((chipsfb_read32(sc, CT_BLT_CONTROL) & BLT_IS_BUSY) != 0) {
    251 	}
    252 
    253 #ifdef CHIPSFB_DEBUG
    254 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0xffffffff);
    255 #endif
    256 }
    257 
    258 static int
    259 chipsfb_match(struct device *parent, struct cfdata *match, void *aux)
    260 {
    261 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    262 
    263 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
    264 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    265 		return 0;
    266 	if ((PCI_VENDOR(pa->pa_id) == PCI_VENDOR_CHIPS) &&
    267 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CHIPS_65550))
    268 		return 100;
    269 	return 0;
    270 }
    271 
    272 static void
    273 chipsfb_attach(struct device *parent, struct device *self, void *aux)
    274 {
    275 	struct chipsfb_softc *sc = (void *)self;
    276 	struct pci_attach_args *pa = aux;
    277 	char devinfo[256];
    278 	struct wsemuldisplaydev_attach_args aa;
    279 	struct rasops_info *ri;
    280 	pcireg_t screg;
    281 	ulong defattr;
    282 	int console, width, height, node, i, j;
    283 #ifdef HAVE_OPENFIRMWARE
    284 	int linebytes, depth;
    285 #endif
    286 	uint32_t bg, fg, ul;
    287 
    288 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    289 	node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    290 	sc->sc_pc = pa->pa_pc;
    291 	sc->sc_pcitag = pa->pa_tag;
    292 	sc->sc_dacw = -1;
    293 
    294 	screg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
    295 	screg |= PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
    296 	pci_conf_write(sc->sc_pc, sc->sc_pcitag,PCI_COMMAND_STATUS_REG,screg);
    297 
    298 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    299 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(pa->pa_class));
    300 
    301 	sc->sc_memt = pa->pa_memt;
    302 	sc->sc_iot = pa->pa_iot;
    303 
    304 	/* the framebuffer */
    305 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_MEM,
    306 	    BUS_SPACE_MAP_LINEAR,
    307 	    &sc->sc_fbt, &sc->sc_fbh, &sc->sc_fb, &sc->sc_fbsize)) {
    308 		printf("%s: failed to map the frame buffer.\n",
    309 		    sc->sc_dev.dv_xname);
    310 	}
    311 
    312 	/* IO-mapped registers */
    313 	if (bus_space_map(sc->sc_iot, 0x0, PAGE_SIZE, 0, &sc->sc_ioregh) != 0) {
    314 		printf("%s: failed to map IO-mapped registers.\n",
    315 		    sc->sc_dev.dv_xname);
    316 	}
    317 
    318 	chipsfb_init(sc);
    319 
    320 	/* we should read these from the chip instead of depending on OF */
    321 	width = height = -1;
    322 
    323 	/* detect panel size */
    324 	width = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HSIZE_LSB);
    325 	width |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HORZ_OVERFLOW_1)
    326 	    & 0x0f) << 8;
    327 	width = (width + 1) * 8;
    328 	height = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VSIZE_LSB);
    329 	height |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VERT_OVERFLOW_1)
    330 	    & 0x0f) << 8;
    331 	height++;
    332 	printf("Panel size: %d x %d\n", width, height);
    333 
    334 #ifdef HAVE_OPENFIRMWARE
    335 	if (OF_getprop(node, "width", &width, 4) != 4)
    336 		OF_interpret("screen-width", 1, &width);
    337 	if (OF_getprop(node, "height", &height, 4) != 4)
    338 		OF_interpret("screen-height", 1, &height);
    339 	if (OF_getprop(node, "linebytes", &linebytes, 4) != 4)
    340 		linebytes = width;			/* XXX */
    341 	if (OF_getprop(node, "depth", &depth, 4) != 4)
    342 		depth = 8;				/* XXX */
    343 
    344 	if (width == -1 || height == -1)
    345 		return;
    346 
    347 	sc->width = width;
    348 	sc->height = height;
    349 	sc->bits_per_pixel = depth;
    350 	sc->linebytes = linebytes;
    351 	printf("%s: initial resolution %dx%d, %d bit\n", sc->sc_dev.dv_xname,
    352 	    sc->width, sc->height, sc->bits_per_pixel);
    353 #endif
    354 
    355 #ifdef notyet
    356 	/* XXX this should at least be configurable via kernel config */
    357 	chipsfb_set_videomode(sc, &videomode_list[16]);
    358 #endif
    359 
    360 	vcons_init(&sc->vd, sc, &chipsfb_defaultscreen, &chipsfb_accessops);
    361 	sc->vd.init_screen = chipsfb_init_screen;
    362 
    363 	console = chipsfb_is_console(pa);
    364 
    365 	ri = &chipsfb_console_screen.scr_ri;
    366 	if (console) {
    367 		vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
    368 		    &defattr);
    369 		chipsfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    370 
    371 		chipsfb_defaultscreen.textops = &ri->ri_ops;
    372 		chipsfb_defaultscreen.capabilities = ri->ri_caps;
    373 		chipsfb_defaultscreen.nrows = ri->ri_rows;
    374 		chipsfb_defaultscreen.ncols = ri->ri_cols;
    375 		wsdisplay_cnattach(&chipsfb_defaultscreen, ri, 0, 0, defattr);
    376 	} else {
    377 		/*
    378 		 * since we're not the console we can postpone the rest
    379 		 * until someone actually allocates a screen for us
    380 		 */
    381 #ifdef notyet
    382 		chipsfb_set_videomode(sc, &videomode_list[0]);
    383 #endif
    384 	}
    385 
    386 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    387 	sc->sc_bg = ri->ri_devcmap[bg];
    388 	chipsfb_clearscreen(sc);
    389 
    390 	printf("%s: %d MB aperture at 0x%08x\n",
    391 	    sc->sc_dev.dv_xname, (u_int)(sc->sc_fbsize >> 20),
    392 	    (u_int)sc->sc_fb);
    393 #ifdef chipsfb_DEBUG
    394 	printf("fb: %08lx\n", (ulong)ri->ri_bits);
    395 #endif
    396 
    397 	j = 0;
    398 	for (i = 0; i < 256; i++) {
    399 		chipsfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
    400 		    rasops_cmap[j + 2]);
    401 		j += 3;
    402 	}
    403 
    404 	aa.console = console;
    405 	aa.scrdata = &chipsfb_screenlist;
    406 	aa.accessops = &chipsfb_accessops;
    407 	aa.accesscookie = &sc->vd;
    408 
    409 	config_found(self, &aa, wsemuldisplaydevprint);
    410 }
    411 
    412 static int
    413 chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r,
    414     uint8_t g, uint8_t b)
    415 {
    416 
    417 	sc->sc_cmap_red[index] = r;
    418 	sc->sc_cmap_green[index] = g;
    419 	sc->sc_cmap_blue[index] = b;
    420 
    421 	chipsfb_write_vga(sc, CT_DACMASK, 0xff);
    422 	chipsfb_write_vga(sc, CT_WRITEINDEX, index);
    423 	chipsfb_write_vga(sc, CT_DACDATA, r);
    424 	chipsfb_write_vga(sc, CT_DACDATA, g);
    425 	chipsfb_write_vga(sc, CT_DACDATA, b);
    426 
    427 	return 0;
    428 }
    429 
    430 static int
    431 chipsfb_putcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    432 {
    433 	u_char *r, *g, *b;
    434 	u_int index = cm->index;
    435 	u_int count = cm->count;
    436 	int i, error;
    437 	u_char rbuf[256], gbuf[256], bbuf[256];
    438 
    439 #ifdef chipsfb_DEBUG
    440 	printf("putcmap: %d %d\n",index, count);
    441 #endif
    442 	if (cm->index >= 256 || cm->count > 256 ||
    443 	    (cm->index + cm->count) > 256)
    444 		return EINVAL;
    445 	error = copyin(cm->red, &rbuf[index], count);
    446 	if (error)
    447 		return error;
    448 	error = copyin(cm->green, &gbuf[index], count);
    449 	if (error)
    450 		return error;
    451 	error = copyin(cm->blue, &bbuf[index], count);
    452 	if (error)
    453 		return error;
    454 
    455 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    456 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    457 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    458 
    459 	r = &sc->sc_cmap_red[index];
    460 	g = &sc->sc_cmap_green[index];
    461 	b = &sc->sc_cmap_blue[index];
    462 
    463 	for (i = 0; i < count; i++) {
    464 		chipsfb_putpalreg(sc, index, *r, *g, *b);
    465 		index++;
    466 		r++, g++, b++;
    467 	}
    468 	return 0;
    469 }
    470 
    471 static int
    472 chipsfb_getcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    473 {
    474 	u_int index = cm->index;
    475 	u_int count = cm->count;
    476 	int error;
    477 
    478 	if (index >= 255 || count > 256 || index + count > 256)
    479 		return EINVAL;
    480 
    481 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    482 	if (error)
    483 		return error;
    484 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    485 	if (error)
    486 		return error;
    487 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    488 	if (error)
    489 		return error;
    490 
    491 	return 0;
    492 }
    493 
    494 static int
    495 chipsfb_is_console(struct pci_attach_args *pa)
    496 {
    497 
    498 #ifdef HAVE_OPENFIRMWARE
    499 	/* check if we're the /chosen console device */
    500 	int chosen, stdout, node, us;
    501 
    502 	us = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag);
    503 	chosen = OF_finddevice("/chosen");
    504 	OF_getprop(chosen, "stdout", &stdout, 4);
    505 	node = OF_instance_to_package(stdout);
    506 	return(us == node);
    507 #else
    508 	/* XXX how do we know we're console on i386? */
    509 	return 1;
    510 #endif
    511 }
    512 
    513 static void
    514 chipsfb_clearscreen(struct chipsfb_softc *sc)
    515 {
    516 	chipsfb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
    517 }
    518 
    519 /*
    520  * wsdisplay_emulops
    521  */
    522 
    523 static void
    524 chipsfb_cursor(void *cookie, int on, int row, int col)
    525 {
    526 	struct rasops_info *ri = cookie;
    527 	struct vcons_screen *scr = ri->ri_hw;
    528 	struct chipsfb_softc *sc = scr->scr_cookie;
    529 	int x, y, wi, he;
    530 
    531 	wi = ri->ri_font->fontwidth;
    532 	he = ri->ri_font->fontheight;
    533 
    534 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    535 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    536 		y = ri->ri_crow * he + ri->ri_yorigin;
    537 		if (ri->ri_flg & RI_CURSOR) {
    538 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    539 			ri->ri_flg &= ~RI_CURSOR;
    540 		}
    541 		ri->ri_crow = row;
    542 		ri->ri_ccol = col;
    543 		if (on) {
    544 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    545 			y = ri->ri_crow * he + ri->ri_yorigin;
    546 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    547 			ri->ri_flg |= RI_CURSOR;
    548 		}
    549 	} else {
    550 		ri->ri_flg &= ~RI_CURSOR;
    551 		ri->ri_crow = row;
    552 		ri->ri_ccol = col;
    553 	}
    554 }
    555 
    556 #if 0
    557 int
    558 chipsfb_mapchar(void *cookie, int uni, u_int *index)
    559 {
    560 	return 0;
    561 }
    562 #endif
    563 
    564 static void
    565 chipsfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    566 {
    567 	struct rasops_info *ri = cookie;
    568 	struct vcons_screen *scr = ri->ri_hw;
    569 	struct chipsfb_softc *sc = scr->scr_cookie;
    570 	int32_t xs, xd, y, width, height;
    571 
    572 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    573 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    574 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    575 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    576 		width = ri->ri_font->fontwidth * ncols;
    577 		height = ri->ri_font->fontheight;
    578 		chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
    579 	}
    580 }
    581 
    582 static void
    583 chipsfb_erasecols(void *cookie, int row, int startcol, int ncols,
    584     long fillattr)
    585 {
    586 	struct rasops_info *ri = cookie;
    587 	struct vcons_screen *scr = ri->ri_hw;
    588 	struct chipsfb_softc *sc = scr->scr_cookie;
    589 	int32_t x, y, width, height, fg, bg, ul;
    590 
    591 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    592 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    593 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    594 		width = ri->ri_font->fontwidth * ncols;
    595 		height = ri->ri_font->fontheight;
    596 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    597 
    598 		chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    599 	}
    600 }
    601 
    602 static void
    603 chipsfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    604 {
    605 	struct rasops_info *ri = cookie;
    606 	struct vcons_screen *scr = ri->ri_hw;
    607 	struct chipsfb_softc *sc = scr->scr_cookie;
    608 	int32_t x, ys, yd, width, height;
    609 
    610 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    611 		x = ri->ri_xorigin;
    612 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    613 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    614 		width = ri->ri_emuwidth;
    615 		height = ri->ri_font->fontheight * nrows;
    616 		chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY);
    617 	}
    618 }
    619 
    620 static void
    621 chipsfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    622 {
    623 	struct rasops_info *ri = cookie;
    624 	struct vcons_screen *scr = ri->ri_hw;
    625 	struct chipsfb_softc *sc = scr->scr_cookie;
    626 	int32_t x, y, width, height, fg, bg, ul;
    627 
    628 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    629 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    630 		if ((row == 0) && (nrows == ri->ri_rows)) {
    631 			/* clear the whole screen */
    632 			chipsfb_rectfill(sc, 0, 0, ri->ri_width,
    633 			    ri->ri_height, ri->ri_devcmap[bg]);
    634 		} else {
    635 			x = ri->ri_xorigin;
    636 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    637 			width = ri->ri_emuwidth;
    638 			height = ri->ri_font->fontheight * nrows;
    639 			chipsfb_rectfill(sc, x, y, width, height,
    640 			    ri->ri_devcmap[bg]);
    641 		}
    642 	}
    643 }
    644 
    645 static void
    646 chipsfb_bitblt(struct chipsfb_softc *sc, int xs, int ys, int xd, int yd,
    647     int width, int height, uint8_t rop)
    648 {
    649 	uint32_t src, dst, cmd = rop, stride, size;
    650 
    651 	cmd |= BLT_PAT_IS_SOLID;
    652 
    653 	/* we assume 8 bit for now */
    654 	src = xs + ys * sc->linebytes;
    655 	dst = xd + yd * sc->linebytes;
    656 
    657 	if (xs < xd) {
    658 		/* right-to-left operation */
    659 		cmd |= BLT_START_RIGHT;
    660 		src += sc->linebytes - 1;
    661 		dst += sc->linebytes - 1;
    662 	}
    663 
    664 	if (ys < yd) {
    665 		/* bottom-to-top operation */
    666 		cmd |= BLT_START_BOTTOM;
    667 		src += (height - 1) * sc->linebytes;
    668 		dst += (height - 1) * sc->linebytes;
    669 	}
    670 
    671 	stride = (sc->linebytes << 16) | sc->linebytes;
    672 	size = (height << 16) | width;
    673 
    674 	chipsfb_wait_idle(sc);
    675 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    676 	chipsfb_write32(sc, CT_BLT_SRCADDR, src);
    677 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    678 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    679 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    680 #ifdef CHIPSFB_WAIT
    681 	chipsfb_wait_idle(sc);
    682 #endif
    683 }
    684 
    685 static void
    686 chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width,
    687     int height, int colour)
    688 {
    689 	uint32_t dst, cmd, stride, size;
    690 
    691 	cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT;
    692 
    693 	/* we assume 8 bit for now */
    694 	dst = x + y * sc->linebytes;
    695 
    696 	stride = (sc->linebytes << 16) | sc->linebytes;
    697 	size = (height << 16) | width;
    698 
    699 	chipsfb_wait_idle(sc);
    700 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    701 	chipsfb_write32(sc, CT_BLT_SRCADDR, dst);
    702 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    703 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    704 	chipsfb_write32(sc, CT_BLT_BG, colour);
    705 	chipsfb_write32(sc, CT_BLT_FG, colour);
    706 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    707 #ifdef CHIPSFB_WAIT
    708 	chipsfb_wait_idle(sc);
    709 #endif
    710 }
    711 
    712 static void
    713 chipsfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    714 {
    715 	struct rasops_info *ri = cookie;
    716 	struct vcons_screen *scr = ri->ri_hw;
    717 	struct chipsfb_softc *sc = scr->scr_cookie;
    718 
    719 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    720 		uint8_t *data;
    721 		int fg, bg, uc;
    722 		int x, y, wi, he;
    723 
    724 		wi = ri->ri_font->fontwidth;
    725 		he = ri->ri_font->fontheight;
    726 
    727 		if (!CHAR_IN_FONT(c, ri->ri_font))
    728 			return;
    729 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    730 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    731 		x = ri->ri_xorigin + col * wi;
    732 		y = ri->ri_yorigin + row * he;
    733 		if (c == 0x20) {
    734 			chipsfb_rectfill(sc, x, y, wi, he, bg);
    735 		} else {
    736 			uc = c-ri->ri_font->firstchar;
    737 			data = (uint8_t *)ri->ri_font->data + uc *
    738 			    ri->ri_fontscale;
    739 			chipsfb_setup_mono(sc, x, y, wi, he, fg, bg);
    740 			chipsfb_feed(sc, ri->ri_font->stride * he, data);
    741 		}
    742 	}
    743 }
    744 
    745 static void
    746 chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width,
    747     int height, uint32_t fg, uint32_t bg)
    748 {
    749 	uint32_t dst, cmd, stride, size;
    750 
    751 	cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY;
    752 
    753 	/* we assume 8 bit for now */
    754 	dst = xd + yd * sc->linebytes;
    755 
    756 	stride = (sc->linebytes << 16);
    757 	size = (height << 16) | width;
    758 
    759 	chipsfb_wait_idle(sc);
    760 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    761 	chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE);
    762 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    763 	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
    764 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    765 	chipsfb_write32(sc, CT_BLT_BG, bg);
    766 	chipsfb_write32(sc, CT_BLT_FG, fg);
    767 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    768 
    769 }
    770 
    771 static void
    772 chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data)
    773 {
    774 	int i;
    775 	uint32_t latch = 0, bork;
    776 	int shift = 0;
    777 
    778 	for (i = 0; i < count; i++) {
    779 		bork = data[i];
    780 		latch |= (bork << shift);
    781 		if (shift == 24) {
    782 			chipsfb_write32(sc, CT_OFF_DATA, latch);
    783 			latch = 0;
    784 			shift = 0;
    785 		} else
    786 			shift += 8;
    787 	}
    788 
    789 	if (shift != 0) {
    790 		chipsfb_write32(sc, CT_OFF_DATA, latch);
    791 	}
    792 
    793 	/* apparently the chip wants 64bit-aligned data or it won't go idle */
    794 	if ((count + 3) & 0x04) {
    795 		chipsfb_write32(sc, CT_OFF_DATA, 0);
    796 	}
    797 #ifdef CHIPSFB_WAIT
    798 	chipsfb_wait_idle(sc);
    799 #endif
    800 }
    801 
    802 #ifdef CHIPSFB_DEBUG
    803 static void
    804 chipsfb_showpal(struct chipsfb_softc *sc)
    805 {
    806 	int i, x = 0;
    807 
    808 	for (i = 0; i < 16; i++) {
    809 		chipsfb_rectfill(sc, x, 0, 64, 64, i);
    810 		x += 64;
    811 	}
    812 }
    813 #endif
    814 
    815 #if 0
    816 static int
    817 chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    818 {
    819 
    820 	return 0;
    821 }
    822 #endif
    823 
    824 static void
    825 chipsfb_restore_palette(struct chipsfb_softc *sc)
    826 {
    827 	int i;
    828 
    829 	for (i = 0; i < 256; i++) {
    830 		chipsfb_putpalreg(sc,
    831 		   i,
    832 		   sc->sc_cmap_red[i],
    833 		   sc->sc_cmap_green[i],
    834 		   sc->sc_cmap_blue[i]);
    835 	}
    836 }
    837 
    838 /*
    839  * wsdisplay_accessops
    840  */
    841 
    842 static int
    843 chipsfb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
    844 	struct lwp *l)
    845 {
    846 	struct vcons_data *vd = v;
    847 	struct chipsfb_softc *sc = vd->cookie;
    848 	struct wsdisplay_fbinfo *wdf;
    849 	struct vcons_screen *ms = vd->active;
    850 
    851 	switch (cmd) {
    852 		case WSDISPLAYIO_GTYPE:
    853 			*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    854 			return 0;
    855 
    856 		case WSDISPLAYIO_GINFO:
    857 			wdf = (void *)data;
    858 			wdf->height = ms->scr_ri.ri_height;
    859 			wdf->width = ms->scr_ri.ri_width;
    860 			wdf->depth = ms->scr_ri.ri_depth;
    861 			wdf->cmsize = 256;
    862 			return 0;
    863 
    864 		case WSDISPLAYIO_GETCMAP:
    865 			return chipsfb_getcmap(sc,
    866 			    (struct wsdisplay_cmap *)data);
    867 
    868 		case WSDISPLAYIO_PUTCMAP:
    869 			return chipsfb_putcmap(sc,
    870 			    (struct wsdisplay_cmap *)data);
    871 
    872 		/* PCI config read/write passthrough. */
    873 		case PCI_IOC_CFGREAD:
    874 		case PCI_IOC_CFGWRITE:
    875 			return (pci_devioctl(sc->sc_pc, sc->sc_pcitag,
    876 			    cmd, data, flag, l));
    877 
    878 		case WSDISPLAYIO_SMODE:
    879 			{
    880 				int new_mode = *(int*)data;
    881 				if (new_mode != sc->sc_mode) {
    882 					sc->sc_mode = new_mode;
    883 					if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    884 						chipsfb_restore_palette(sc);
    885 						vcons_redraw_screen(ms);
    886 					}
    887 				}
    888 			}
    889 			return 0;
    890 	}
    891 	return EPASSTHROUGH;
    892 }
    893 
    894 static paddr_t
    895 chipsfb_mmap(void *v, void *vs, off_t offset, int prot)
    896 {
    897 	struct vcons_data *vd = v;
    898 	struct chipsfb_softc *sc = vd->cookie;
    899 	struct lwp *me;
    900 	paddr_t pa;
    901 
    902 	/* 'regular' framebuffer mmap()ing */
    903 	if (offset < sc->sc_fbsize) {
    904 		pa = bus_space_mmap(sc->sc_fbt, offset, 0, prot,
    905 		    BUS_SPACE_MAP_LINEAR);
    906 		return pa;
    907 	}
    908 
    909 	/*
    910 	 * restrict all other mappings to processes with superuser privileges
    911 	 * or the kernel itself
    912 	 */
    913 	me = curlwp;
    914 	if (me != NULL) {
    915 		if (kauth_authorize_generic(me->l_cred, KAUTH_GENERIC_ISSUSER,
    916 		    NULL) != 0) {
    917 			printf("%s: mmap() rejected.\n", sc->sc_dev.dv_xname);
    918 			return -1;
    919 		}
    920 	}
    921 
    922 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    923 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    924 		    BUS_SPACE_MAP_LINEAR);
    925 		return pa;
    926 	}
    927 
    928 #ifdef macppc
    929 	/* allow mapping of IO space */
    930 	if ((offset >= 0xf2000000) && (offset < 0xf2800000)) {
    931 		pa = bus_space_mmap(sc->sc_iot, offset - 0xf2000000, 0, prot,
    932 		    BUS_SPACE_MAP_LINEAR);
    933 		return pa;
    934 	}
    935 #endif
    936 
    937 #ifdef OFB_ALLOW_OTHERS
    938 	if (offset >= 0x80000000) {
    939 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    940 		    BUS_SPACE_MAP_LINEAR);
    941 		return pa;
    942 	}
    943 #endif
    944 	return -1;
    945 }
    946 
    947 static void
    948 chipsfb_init_screen(void *cookie, struct vcons_screen *scr,
    949     int existing, long *defattr)
    950 {
    951 	struct chipsfb_softc *sc = cookie;
    952 	struct rasops_info *ri = &scr->scr_ri;
    953 
    954 	ri->ri_depth = sc->bits_per_pixel;
    955 	ri->ri_width = sc->width;
    956 	ri->ri_height = sc->height;
    957 	ri->ri_stride = sc->width;
    958 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
    959 
    960 	ri->ri_bits = bus_space_vaddr(sc->sc_fbt, sc->sc_fbh);
    961 
    962 #ifdef CHIPSFB_DEBUG
    963 	printf("addr: %08lx\n", (ulong)ri->ri_bits);
    964 #endif
    965 	if (existing) {
    966 		ri->ri_flg |= RI_CLEAR;
    967 	}
    968 
    969 	rasops_init(ri, sc->height/8, sc->width/8);
    970 	ri->ri_caps = WSSCREEN_WSCOLORS;
    971 
    972 	rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
    973 		    sc->width / ri->ri_font->fontwidth);
    974 
    975 	ri->ri_hw = scr;
    976 	ri->ri_ops.copyrows = chipsfb_copyrows;
    977 	ri->ri_ops.copycols = chipsfb_copycols;
    978 	ri->ri_ops.eraserows = chipsfb_eraserows;
    979 	ri->ri_ops.erasecols = chipsfb_erasecols;
    980 	ri->ri_ops.cursor = chipsfb_cursor;
    981 	ri->ri_ops.putchar = chipsfb_putchar;
    982 }
    983 
    984 #if 0
    985 int
    986 chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    987 {
    988 
    989 	return 0;
    990 }
    991 #endif
    992 
    993 static void
    994 chipsfb_init(struct chipsfb_softc *sc)
    995 {
    996 
    997 	chipsfb_wait_idle(sc);
    998 
    999 	chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_IO_CONTROL,
   1000 	    ENABLE_CRTC_EXT | ENABLE_ATTR_EXT);
   1001 	chipsfb_write_indexed(sc, CT_CONF_INDEX, XR_ADDR_MAPPING,
   1002 	    ENABLE_LINEAR);
   1003 
   1004 	/* setup the blitter */
   1005 }
   1006