Home | History | Annotate | Line # | Download | only in ic
ct65550.c revision 1.8
      1 /*	$NetBSD: ct65550.c,v 1.8 2013/10/09 17:20:54 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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * A console driver for Chips & Technologies 65550 graphics controllers
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: ct65550.c,v 1.8 2013/10/09 17:20:54 macallan Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/device.h>
     39 #include <sys/kauth.h>
     40 #include <sys/bus.h>
     41 #include <dev/videomode/videomode.h>
     42 
     43 #include <dev/ic/ct65550reg.h>
     44 #include <dev/ic/ct65550var.h>
     45 
     46 #include "opt_wsemul.h"
     47 #include "opt_chipsfb.h"
     48 
     49 static struct vcons_screen chipsfb_console_screen;
     50 
     51 extern const u_char rasops_cmap[768];
     52 
     53 static void 	chipsfb_init(struct chipsfb_softc *);
     54 
     55 static void	chipsfb_cursor(void *, int, int, int);
     56 static void	chipsfb_copycols(void *, int, int, int, int);
     57 static void	chipsfb_erasecols(void *, int, int, int, long);
     58 static void	chipsfb_copyrows(void *, int, int, int);
     59 static void	chipsfb_eraserows(void *, int, int, long);
     60 
     61 #if 0
     62 static int	chipsfb_allocattr(void *, int, int, int, long *);
     63 static void	chipsfb_scroll(void *, void *, int);
     64 static int	chipsfb_load_font(void *, void *, struct wsdisplay_font *);
     65 #endif
     66 
     67 static int	chipsfb_putcmap(struct chipsfb_softc *,
     68 			    struct wsdisplay_cmap *);
     69 static int 	chipsfb_getcmap(struct chipsfb_softc *,
     70 			    struct wsdisplay_cmap *);
     71 static int 	chipsfb_putpalreg(struct chipsfb_softc *, uint8_t, uint8_t,
     72 			    uint8_t, uint8_t);
     73 
     74 static void	chipsfb_bitblt(struct chipsfb_softc *, int, int, int, int,
     75 			    int, int, uint8_t);
     76 static void	chipsfb_rectfill(struct chipsfb_softc *, int, int, int, int,
     77 			    int);
     78 static void	chipsfb_putchar(void *, int, int, u_int, long);
     79 static void	chipsfb_setup_mono(struct chipsfb_softc *, int, int, int,
     80 			    int, uint32_t, uint32_t);
     81 static void	chipsfb_feed(struct chipsfb_softc *, int, uint8_t *);
     82 
     83 #if 0
     84 static void	chipsfb_showpal(struct chipsfb_softc *);
     85 #endif
     86 static void	chipsfb_restore_palette(struct chipsfb_softc *);
     87 
     88 static void	chipsfb_wait_idle(struct chipsfb_softc *);
     89 
     90 struct wsscreen_descr chipsfb_defaultscreen = {
     91 	"default",	/* name */
     92 	0, 0,		/* ncols, nrows */
     93 	NULL,		/* textops */
     94 	8, 16,		/* fontwidth, fontheight */
     95 	WSSCREEN_WSCOLORS | WSSCREEN_HILIT, /* capabilities */
     96 	NULL,		/* modecookie */
     97 };
     98 
     99 const struct wsscreen_descr *_chipsfb_scrlist[] = {
    100 	&chipsfb_defaultscreen,
    101 	/* XXX other formats, graphics screen? */
    102 };
    103 
    104 struct wsscreen_list chipsfb_screenlist = {
    105 	sizeof(_chipsfb_scrlist) / sizeof(struct wsscreen_descr *), _chipsfb_scrlist
    106 };
    107 
    108 static int	chipsfb_ioctl(void *, void *, u_long, void *, int,
    109 		    struct lwp *);
    110 static paddr_t	chipsfb_mmap(void *, void *, off_t, int);
    111 static void	chipsfb_clearscreen(struct chipsfb_softc *);
    112 static void	chipsfb_init_screen(void *, struct vcons_screen *, int,
    113 			    long *);
    114 
    115 
    116 struct wsdisplay_accessops chipsfb_accessops = {
    117 	chipsfb_ioctl,
    118 	chipsfb_mmap,
    119 	NULL,	/* vcons_alloc_screen */
    120 	NULL,	/* vcons_free_screen */
    121 	NULL,	/* vcons_show_screen */
    122 	NULL,	/* load_font */
    123 	NULL,	/* polls */
    124 	NULL,	/* scroll */
    125 };
    126 
    127 /*
    128  * Inline functions for getting access to register aperture.
    129  */
    130 static inline void
    131 chipsfb_write32(struct chipsfb_softc *sc, uint32_t reg, uint32_t val)
    132 {
    133 	bus_space_write_4(sc->sc_memt, sc->sc_mmregh, reg, val);
    134 }
    135 
    136 static inline uint32_t
    137 chipsfb_read32(struct chipsfb_softc *sc, uint32_t reg)
    138 {
    139 	return bus_space_read_4(sc->sc_memt, sc->sc_mmregh, reg);
    140 }
    141 
    142 static inline void
    143 chipsfb_write_vga(struct chipsfb_softc *sc, uint32_t reg,  uint8_t val)
    144 {
    145 	bus_space_write_1(sc->sc_iot, sc->sc_ioregh, reg, val);
    146 }
    147 
    148 static inline uint8_t
    149 chipsfb_read_vga(struct chipsfb_softc *sc, uint32_t reg)
    150 {
    151 	return bus_space_read_1(sc->sc_iot, sc->sc_ioregh, reg);
    152 }
    153 
    154 static inline uint8_t
    155 chipsfb_read_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index)
    156 {
    157 
    158 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    159 	return chipsfb_read_vga(sc, reg | 0x0001);
    160 }
    161 
    162 static inline void
    163 chipsfb_write_indexed(struct chipsfb_softc *sc, uint32_t reg, uint8_t index,
    164     uint8_t val)
    165 {
    166 
    167 	chipsfb_write_vga(sc, reg & 0xfffe, index);
    168 	chipsfb_write_vga(sc, reg | 0x0001, val);
    169 }
    170 
    171 static void
    172 chipsfb_wait_idle(struct chipsfb_softc *sc)
    173 {
    174 
    175 #ifdef CHIPSFB_DEBUG
    176 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0);
    177 #endif
    178 
    179 	/* spin until the blitter is idle */
    180 	while ((chipsfb_read32(sc, CT_BLT_CONTROL) & BLT_IS_BUSY) != 0) {
    181 	}
    182 
    183 #ifdef CHIPSFB_DEBUG
    184 	chipsfb_write32(sc, CT_OFF_FB + (800 * 598) - 4, 0xffffffff);
    185 #endif
    186 }
    187 
    188 void
    189 chipsfb_do_attach(struct chipsfb_softc *sc)
    190 {
    191 	struct wsemuldisplaydev_attach_args aa;
    192 	struct rasops_info *ri;
    193 	prop_dictionary_t dict;
    194 	ulong defattr;
    195 	bool console = false;
    196 	int width, height, i, j;
    197 	uint32_t bg, fg, ul;
    198 	uint8_t cmap[768];
    199 
    200 	dict = device_properties(sc->sc_dev);
    201 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    202 	sc->sc_dacw = -1;
    203 
    204 #ifdef CHIPSFB_DEBUG
    205 	printf(prop_dictionary_externalize(dict));
    206 #endif
    207 	chipsfb_init(sc);
    208 
    209 	width = height = -1;
    210 
    211 	/* detect panel size */
    212 	width = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HSIZE_LSB);
    213 	width |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_HORZ_OVERFLOW_1)
    214 	    & 0x0f) << 8;
    215 	width = (width + 1) * 8;
    216 	height = chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VSIZE_LSB);
    217 	height |= (chipsfb_read_indexed(sc, CT_FP_INDEX, FP_VERT_OVERFLOW_1)
    218 	    & 0x0f) << 8;
    219 	height++;
    220 	if ((width < 640) || ( width > 1280) || (height < 480) ||
    221 	    (height > 1024)) {
    222 		/* no sane values in the panel registers */
    223 		width = height = -1;
    224 	} else
    225 		aprint_verbose("Panel size: %d x %d\n", width, height);
    226 
    227 	if (!prop_dictionary_get_uint32(dict, "width", &sc->width))
    228 		sc->width = width;
    229 	if (!prop_dictionary_get_uint32(dict, "height", &sc->height))
    230 		sc->height = height;
    231 	if (!prop_dictionary_get_uint32(dict, "depth", &sc->bits_per_pixel))
    232 		sc->bits_per_pixel = 8;
    233 	if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->linebytes))
    234 		sc->linebytes = (sc->width * sc->bits_per_pixel) >> 3;
    235 
    236 	prop_dictionary_get_bool(dict, "is_console", &console);
    237 
    238 #ifdef notyet
    239 	/* XXX this should at least be configurable via kernel config */
    240 	chipsfb_set_videomode(sc, &videomode_list[16]);
    241 #endif
    242 
    243 	vcons_init(&sc->vd, sc, &chipsfb_defaultscreen, &chipsfb_accessops);
    244 	sc->vd.init_screen = chipsfb_init_screen;
    245 
    246 	ri = &chipsfb_console_screen.scr_ri;
    247 	if (console) {
    248 		vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
    249 		    &defattr);
    250 		chipsfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    251 
    252 		chipsfb_defaultscreen.textops = &ri->ri_ops;
    253 		chipsfb_defaultscreen.capabilities = ri->ri_caps;
    254 		chipsfb_defaultscreen.nrows = ri->ri_rows;
    255 		chipsfb_defaultscreen.ncols = ri->ri_cols;
    256 		wsdisplay_cnattach(&chipsfb_defaultscreen, ri, 0, 0, defattr);
    257 	} else {
    258 		if (chipsfb_console_screen.scr_ri.ri_rows == 0) {
    259 			/* do some minimal setup to avoid weirdnesses later */
    260 			vcons_init_screen(&sc->vd, &chipsfb_console_screen, 1,
    261 			    &defattr);
    262 		} else
    263 			(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    264 
    265 	}
    266 
    267 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    268 	sc->sc_bg = ri->ri_devcmap[bg];
    269 	chipsfb_clearscreen(sc);
    270 
    271 	if (console)
    272 		vcons_replay_msgbuf(&chipsfb_console_screen);
    273 
    274 	aprint_normal_dev(sc->sc_dev, "%d MB aperture, %d MB VRAM at 0x%08x\n",
    275 	    (u_int)(sc->sc_fbsize >> 20),
    276 	    (int)sc->memsize >> 20, (u_int)sc->sc_fb);
    277 #ifdef CHIPSFB_DEBUG
    278 	aprint_debug("fb: %08lx\n", (ulong)ri->ri_bits);
    279 #endif
    280 
    281 	j = 0;
    282 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    283 	for (i = 0; i < 256; i++) {
    284 		chipsfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    285 		j += 3;
    286 	}
    287 
    288 	aa.console = console;
    289 	aa.scrdata = &chipsfb_screenlist;
    290 	aa.accessops = &chipsfb_accessops;
    291 	aa.accesscookie = &sc->vd;
    292 
    293 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    294 }
    295 
    296 static int
    297 chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r,
    298     uint8_t g, uint8_t b)
    299 {
    300 
    301 	sc->sc_cmap_red[index] = r;
    302 	sc->sc_cmap_green[index] = g;
    303 	sc->sc_cmap_blue[index] = b;
    304 
    305 	chipsfb_write_vga(sc, CT_DACMASK, 0xff);
    306 	chipsfb_write_vga(sc, CT_WRITEINDEX, index);
    307 	chipsfb_write_vga(sc, CT_DACDATA, r);
    308 	chipsfb_write_vga(sc, CT_DACDATA, g);
    309 	chipsfb_write_vga(sc, CT_DACDATA, b);
    310 
    311 	return 0;
    312 }
    313 
    314 static int
    315 chipsfb_putcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    316 {
    317 	u_char *r, *g, *b;
    318 	u_int index = cm->index;
    319 	u_int count = cm->count;
    320 	int i, error;
    321 	u_char rbuf[256], gbuf[256], bbuf[256];
    322 
    323 #ifdef CHIPSFB_DEBUG
    324 	aprint_debug("putcmap: %d %d\n",index, count);
    325 #endif
    326 	if (cm->index >= 256 || cm->count > 256 ||
    327 	    (cm->index + cm->count) > 256)
    328 		return EINVAL;
    329 	error = copyin(cm->red, &rbuf[index], count);
    330 	if (error)
    331 		return error;
    332 	error = copyin(cm->green, &gbuf[index], count);
    333 	if (error)
    334 		return error;
    335 	error = copyin(cm->blue, &bbuf[index], count);
    336 	if (error)
    337 		return error;
    338 
    339 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    340 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    341 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    342 
    343 	r = &sc->sc_cmap_red[index];
    344 	g = &sc->sc_cmap_green[index];
    345 	b = &sc->sc_cmap_blue[index];
    346 
    347 	for (i = 0; i < count; i++) {
    348 		chipsfb_putpalreg(sc, index, *r, *g, *b);
    349 		index++;
    350 		r++, g++, b++;
    351 	}
    352 	return 0;
    353 }
    354 
    355 static int
    356 chipsfb_getcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    357 {
    358 	u_int index = cm->index;
    359 	u_int count = cm->count;
    360 	int error;
    361 
    362 	if (index >= 255 || count > 256 || index + count > 256)
    363 		return EINVAL;
    364 
    365 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    366 	if (error)
    367 		return error;
    368 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    369 	if (error)
    370 		return error;
    371 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    372 	if (error)
    373 		return error;
    374 
    375 	return 0;
    376 }
    377 
    378 static void
    379 chipsfb_clearscreen(struct chipsfb_softc *sc)
    380 {
    381 	chipsfb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
    382 }
    383 
    384 /*
    385  * wsdisplay_emulops
    386  */
    387 
    388 static void
    389 chipsfb_cursor(void *cookie, int on, int row, int col)
    390 {
    391 	struct rasops_info *ri = cookie;
    392 	struct vcons_screen *scr = ri->ri_hw;
    393 	struct chipsfb_softc *sc = scr->scr_cookie;
    394 	int x, y, wi, he;
    395 
    396 	wi = ri->ri_font->fontwidth;
    397 	he = ri->ri_font->fontheight;
    398 
    399 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    400 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    401 		y = ri->ri_crow * he + ri->ri_yorigin;
    402 		if (ri->ri_flg & RI_CURSOR) {
    403 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    404 			ri->ri_flg &= ~RI_CURSOR;
    405 		}
    406 		ri->ri_crow = row;
    407 		ri->ri_ccol = col;
    408 		if (on) {
    409 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    410 			y = ri->ri_crow * he + ri->ri_yorigin;
    411 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    412 			ri->ri_flg |= RI_CURSOR;
    413 		}
    414 	} else {
    415 		ri->ri_flg &= ~RI_CURSOR;
    416 		ri->ri_crow = row;
    417 		ri->ri_ccol = col;
    418 	}
    419 }
    420 
    421 #if 0
    422 int
    423 chipsfb_mapchar(void *cookie, int uni, u_int *index)
    424 {
    425 	return 0;
    426 }
    427 #endif
    428 
    429 static void
    430 chipsfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    431 {
    432 	struct rasops_info *ri = cookie;
    433 	struct vcons_screen *scr = ri->ri_hw;
    434 	struct chipsfb_softc *sc = scr->scr_cookie;
    435 	int32_t xs, xd, y, width, height;
    436 
    437 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    438 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    439 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    440 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    441 		width = ri->ri_font->fontwidth * ncols;
    442 		height = ri->ri_font->fontheight;
    443 		chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
    444 	}
    445 }
    446 
    447 static void
    448 chipsfb_erasecols(void *cookie, int row, int startcol, int ncols,
    449     long fillattr)
    450 {
    451 	struct rasops_info *ri = cookie;
    452 	struct vcons_screen *scr = ri->ri_hw;
    453 	struct chipsfb_softc *sc = scr->scr_cookie;
    454 	int32_t x, y, width, height, fg, bg, ul;
    455 
    456 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    457 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    458 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    459 		width = ri->ri_font->fontwidth * ncols;
    460 		height = ri->ri_font->fontheight;
    461 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    462 
    463 		chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    464 	}
    465 }
    466 
    467 static void
    468 chipsfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    469 {
    470 	struct rasops_info *ri = cookie;
    471 	struct vcons_screen *scr = ri->ri_hw;
    472 	struct chipsfb_softc *sc = scr->scr_cookie;
    473 	int32_t x, ys, yd, width, height;
    474 
    475 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    476 		x = ri->ri_xorigin;
    477 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    478 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    479 		width = ri->ri_emuwidth;
    480 		height = ri->ri_font->fontheight * nrows;
    481 		chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY);
    482 	}
    483 }
    484 
    485 static void
    486 chipsfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    487 {
    488 	struct rasops_info *ri = cookie;
    489 	struct vcons_screen *scr = ri->ri_hw;
    490 	struct chipsfb_softc *sc = scr->scr_cookie;
    491 	int32_t x, y, width, height, fg, bg, ul;
    492 
    493 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    494 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    495 		if ((row == 0) && (nrows == ri->ri_rows)) {
    496 			/* clear the whole screen */
    497 			chipsfb_rectfill(sc, 0, 0, ri->ri_width,
    498 			    ri->ri_height, ri->ri_devcmap[bg]);
    499 		} else {
    500 			x = ri->ri_xorigin;
    501 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    502 			width = ri->ri_emuwidth;
    503 			height = ri->ri_font->fontheight * nrows;
    504 			chipsfb_rectfill(sc, x, y, width, height,
    505 			    ri->ri_devcmap[bg]);
    506 		}
    507 	}
    508 }
    509 
    510 static void
    511 chipsfb_bitblt(struct chipsfb_softc *sc, int xs, int ys, int xd, int yd,
    512     int width, int height, uint8_t rop)
    513 {
    514 	uint32_t src, dst, cmd = rop, stride, size;
    515 
    516 	cmd |= BLT_PAT_IS_SOLID;
    517 
    518 	/* we assume 8 bit for now */
    519 	src = xs + ys * sc->linebytes;
    520 	dst = xd + yd * sc->linebytes;
    521 
    522 	if (xs < xd) {
    523 		/* right-to-left operation */
    524 		cmd |= BLT_START_RIGHT;
    525 		src += width - 1;
    526 		dst += width - 1;
    527 	}
    528 
    529 	if (ys < yd) {
    530 		/* bottom-to-top operation */
    531 		cmd |= BLT_START_BOTTOM;
    532 		src += (height - 1) * sc->linebytes;
    533 		dst += (height - 1) * sc->linebytes;
    534 	}
    535 
    536 	stride = (sc->linebytes << 16) | sc->linebytes;
    537 	size = (height << 16) | width;
    538 
    539 	chipsfb_wait_idle(sc);
    540 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    541 	chipsfb_write32(sc, CT_BLT_SRCADDR, src);
    542 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    543 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    544 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    545 #ifdef CHIPSFB_WAIT
    546 	chipsfb_wait_idle(sc);
    547 #endif
    548 }
    549 
    550 static void
    551 chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width,
    552     int height, int colour)
    553 {
    554 	uint32_t dst, cmd, stride, size;
    555 
    556 	cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT;
    557 
    558 	/* we assume 8 bit for now */
    559 	dst = x + y * sc->linebytes;
    560 
    561 	stride = (sc->linebytes << 16) | sc->linebytes;
    562 	size = (height << 16) | width;
    563 
    564 	chipsfb_wait_idle(sc);
    565 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    566 	chipsfb_write32(sc, CT_BLT_SRCADDR, dst);
    567 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    568 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    569 	chipsfb_write32(sc, CT_BLT_BG, colour);
    570 	chipsfb_write32(sc, CT_BLT_FG, colour);
    571 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    572 #ifdef CHIPSFB_WAIT
    573 	chipsfb_wait_idle(sc);
    574 #endif
    575 }
    576 
    577 static void
    578 chipsfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    579 {
    580 	struct rasops_info *ri = cookie;
    581 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    582 	struct vcons_screen *scr = ri->ri_hw;
    583 	struct chipsfb_softc *sc = scr->scr_cookie;
    584 
    585 	if (__predict_false((unsigned int)row > ri->ri_rows ||
    586 	    (unsigned int)col > ri->ri_cols))
    587 		return;
    588 
    589 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    590 		uint8_t *data;
    591 		int fg, bg, uc;
    592 		int x, y, wi, he;
    593 
    594 		wi = font->fontwidth;
    595 		he = font->fontheight;
    596 
    597 		if (!CHAR_IN_FONT(c, font))
    598 			return;
    599 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    600 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    601 		x = ri->ri_xorigin + col * wi;
    602 		y = ri->ri_yorigin + row * he;
    603 		if (c == 0x20) {
    604 			chipsfb_rectfill(sc, x, y, wi, he, bg);
    605 		} else {
    606 			uc = c - font->firstchar;
    607 			data = (uint8_t *)font->data + uc *
    608 			    ri->ri_fontscale;
    609 			chipsfb_setup_mono(sc, x, y, wi, he, fg, bg);
    610 			chipsfb_feed(sc, font->stride * he, data);
    611 		}
    612 	}
    613 }
    614 
    615 static void
    616 chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width,
    617     int height, uint32_t fg, uint32_t bg)
    618 {
    619 	uint32_t dst, cmd, stride, size;
    620 
    621 	cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY;
    622 
    623 	/* we assume 8 bit for now */
    624 	dst = xd + yd * sc->linebytes;
    625 
    626 	stride = (sc->linebytes << 16);
    627 	size = (height << 16) | width;
    628 
    629 	chipsfb_wait_idle(sc);
    630 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    631 	chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE);
    632 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    633 	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
    634 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    635 	chipsfb_write32(sc, CT_BLT_BG, bg);
    636 	chipsfb_write32(sc, CT_BLT_FG, fg);
    637 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    638 }
    639 
    640 static void
    641 chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data)
    642 {
    643 	int i;
    644 	uint32_t latch = 0, bork;
    645 	int shift = 0;
    646 
    647 	for (i = 0; i < count; i++) {
    648 		bork = data[i];
    649 		latch |= (bork << shift);
    650 		if (shift == 24) {
    651 			chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
    652 			latch = 0;
    653 			shift = 0;
    654 		} else
    655 			shift += 8;
    656 	}
    657 
    658 	if (shift != 0) {
    659 		chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
    660 	}
    661 
    662 	/* apparently the chip wants 64bit-aligned data or it won't go idle */
    663 	if ((count + 3) & 0x04) {
    664 		chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, 0);
    665 	}
    666 #ifdef CHIPSFB_WAIT
    667 	chipsfb_wait_idle(sc);
    668 #endif
    669 }
    670 
    671 #if 0
    672 static void
    673 chipsfb_showpal(struct chipsfb_softc *sc)
    674 {
    675 	int i, x = 0;
    676 
    677 	for (i = 0; i < 16; i++) {
    678 		chipsfb_rectfill(sc, x, 0, 64, 64, i);
    679 		x += 64;
    680 	}
    681 }
    682 #endif
    683 
    684 #if 0
    685 static int
    686 chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    687 {
    688 
    689 	return 0;
    690 }
    691 #endif
    692 
    693 static void
    694 chipsfb_restore_palette(struct chipsfb_softc *sc)
    695 {
    696 	int i;
    697 
    698 	for (i = 0; i < 256; i++) {
    699 		chipsfb_putpalreg(sc,
    700 		   i,
    701 		   sc->sc_cmap_red[i],
    702 		   sc->sc_cmap_green[i],
    703 		   sc->sc_cmap_blue[i]);
    704 	}
    705 }
    706 
    707 /*
    708  * wsdisplay_accessops
    709  */
    710 
    711 static int
    712 chipsfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    713 	struct lwp *l)
    714 {
    715 	struct vcons_data *vd = v;
    716 	struct chipsfb_softc *sc = vd->cookie;
    717 	struct wsdisplay_fbinfo *wdf;
    718 	struct vcons_screen *ms = vd->active;
    719 
    720 	switch (cmd) {
    721 	case WSDISPLAYIO_GTYPE:
    722 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    723 		return 0;
    724 
    725 	case WSDISPLAYIO_GINFO:
    726 		wdf = (void *)data;
    727 		wdf->height = ms->scr_ri.ri_height;
    728 		wdf->width = ms->scr_ri.ri_width;
    729 		wdf->depth = ms->scr_ri.ri_depth;
    730 		wdf->cmsize = 256;
    731 		return 0;
    732 
    733 	case WSDISPLAYIO_GETCMAP:
    734 		return chipsfb_getcmap(sc,
    735 		    (struct wsdisplay_cmap *)data);
    736 
    737 	case WSDISPLAYIO_PUTCMAP:
    738 		return chipsfb_putcmap(sc,
    739 		    (struct wsdisplay_cmap *)data);
    740 
    741 
    742 	case WSDISPLAYIO_SMODE: {
    743 		int new_mode = *(int*)data;
    744 		if (new_mode != sc->sc_mode) {
    745 			sc->sc_mode = new_mode;
    746 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    747 				chipsfb_restore_palette(sc);
    748 				vcons_redraw_screen(ms);
    749 			}
    750 		}
    751 		}
    752 		return 0;
    753 
    754 	case WSDISPLAYIO_GET_FBINFO: {
    755 		struct wsdisplayio_fbinfo *fbi = data;
    756 		return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
    757 	}
    758 
    759 	default:
    760 		if (sc->sc_ioctl != NULL)
    761 			return sc->sc_ioctl(v, vs, cmd, data, flag, l);
    762 	}
    763 	return EPASSTHROUGH;
    764 }
    765 
    766 static paddr_t
    767 chipsfb_mmap(void *v, void *vs, off_t offset, int prot)
    768 {
    769 	struct vcons_data *vd = v;
    770 	struct chipsfb_softc *sc = vd->cookie;
    771 	paddr_t pa;
    772 
    773 	if (sc->sc_mmap != NULL)
    774 		return sc->sc_mmap(v, vs, offset, prot);
    775 
    776 	/* 'regular' framebuffer mmap()ing */
    777 	if (offset < sc->memsize) {
    778 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    779 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    780 		return pa;
    781 	}
    782 
    783 	/*
    784 	 * restrict all other mappings to processes with superuser privileges
    785 	 * or the kernel itself
    786 	 */
    787 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
    788 	    NULL, NULL, NULL, NULL) != 0) {
    789 		aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n");
    790 		return -1;
    791 	}
    792 
    793 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    794 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    795 		    BUS_SPACE_MAP_LINEAR);
    796 		return pa;
    797 	}
    798 
    799 #ifdef PCI_MAGIC_IO_RANGE
    800 	/* allow mapping of IO space */
    801 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    802 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    803 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    804 		    0, prot, BUS_SPACE_MAP_LINEAR);
    805 		return pa;
    806 	}
    807 #endif
    808 
    809 	return -1;
    810 }
    811 
    812 static void
    813 chipsfb_init_screen(void *cookie, struct vcons_screen *scr,
    814     int existing, long *defattr)
    815 {
    816 	struct chipsfb_softc *sc = cookie;
    817 	struct rasops_info *ri = &scr->scr_ri;
    818 
    819 	ri->ri_depth = sc->bits_per_pixel;
    820 	ri->ri_width = sc->width;
    821 	ri->ri_height = sc->height;
    822 	ri->ri_stride = sc->width;
    823 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_8BIT_IS_RGB;
    824 
    825 	ri->ri_bits = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    826 
    827 #ifdef CHIPSFB_DEBUG
    828 	aprint_debug("addr: %08lx\n", (ulong)ri->ri_bits);
    829 #endif
    830 	if (existing) {
    831 		ri->ri_flg |= RI_CLEAR;
    832 	}
    833 
    834 	rasops_init(ri, 0, 0);
    835 	ri->ri_caps = WSSCREEN_WSCOLORS;
    836 
    837 	rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
    838 		    sc->width / ri->ri_font->fontwidth);
    839 
    840 	ri->ri_hw = scr;
    841 	ri->ri_ops.copyrows = chipsfb_copyrows;
    842 	ri->ri_ops.copycols = chipsfb_copycols;
    843 	ri->ri_ops.eraserows = chipsfb_eraserows;
    844 	ri->ri_ops.erasecols = chipsfb_erasecols;
    845 	ri->ri_ops.cursor = chipsfb_cursor;
    846 	ri->ri_ops.putchar = chipsfb_putchar;
    847 }
    848 
    849 #if 0
    850 int
    851 chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    852 {
    853 
    854 	return 0;
    855 }
    856 #endif
    857 
    858 static void
    859 chipsfb_init(struct chipsfb_softc *sc)
    860 {
    861 
    862 	chipsfb_wait_idle(sc);
    863 
    864 	/* setup the blitter */
    865 }
    866 
    867 uint32_t
    868 chipsfb_probe_vram(struct chipsfb_softc *sc)
    869 {
    870 	uint32_t ofs = 0x00080000;	/* 512kB */
    871 
    872 	/*
    873 	 * advance in 0.5MB steps, see if we can read back what we wrote and
    874 	 * if what we wrote to 0 is left untouched. Max. fb size is 4MB so
    875 	 * we voluntarily stop there.
    876 	 */
    877 	bus_space_write_4(sc->sc_memt, sc->sc_fbh, 0, 0xf0f0f0f0);
    878 	bus_space_write_4(sc->sc_memt, sc->sc_fbh, ofs, 0x0f0f0f0f);
    879 	while ((bus_space_read_4(sc->sc_memt, sc->sc_fbh, 0) == 0xf0f0f0f0) &&
    880 	    (bus_space_read_4(sc->sc_memt, sc->sc_fbh, ofs) == 0x0f0f0f0f) &&
    881 	    (ofs < 0x00400000)) {
    882 
    883 		ofs += 0x00080000;
    884 		bus_space_write_4(sc->sc_memt, sc->sc_fbh, ofs, 0x0f0f0f0f);
    885 	}
    886 
    887 	return ofs;
    888 }
    889