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