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