Home | History | Annotate | Line # | Download | only in ic
ct65550.c revision 1.6
      1 /*	$NetBSD: ct65550.c,v 1.6 2012/08/22 21:17:58 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.6 2012/08/22 21:17:58 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 		}
    263 	}
    264 
    265 	rasops_unpack_attr(defattr, &fg, &bg, &ul);
    266 	sc->sc_bg = ri->ri_devcmap[bg];
    267 	chipsfb_clearscreen(sc);
    268 
    269 	if (console)
    270 		vcons_replay_msgbuf(&chipsfb_console_screen);
    271 
    272 	aprint_normal_dev(sc->sc_dev, "%d MB aperture, %d MB VRAM at 0x%08x\n",
    273 	    (u_int)(sc->sc_fbsize >> 20),
    274 	    (int)sc->memsize >> 20, (u_int)sc->sc_fb);
    275 #ifdef CHIPSFB_DEBUG
    276 	aprint_debug("fb: %08lx\n", (ulong)ri->ri_bits);
    277 #endif
    278 
    279 	j = 0;
    280 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    281 	for (i = 0; i < 256; i++) {
    282 		chipsfb_putpalreg(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    283 		j += 3;
    284 	}
    285 
    286 	aa.console = console;
    287 	aa.scrdata = &chipsfb_screenlist;
    288 	aa.accessops = &chipsfb_accessops;
    289 	aa.accesscookie = &sc->vd;
    290 
    291 	config_found(sc->sc_dev, &aa, wsemuldisplaydevprint);
    292 }
    293 
    294 static int
    295 chipsfb_putpalreg(struct chipsfb_softc *sc, uint8_t index, uint8_t r,
    296     uint8_t g, uint8_t b)
    297 {
    298 
    299 	sc->sc_cmap_red[index] = r;
    300 	sc->sc_cmap_green[index] = g;
    301 	sc->sc_cmap_blue[index] = b;
    302 
    303 	chipsfb_write_vga(sc, CT_DACMASK, 0xff);
    304 	chipsfb_write_vga(sc, CT_WRITEINDEX, index);
    305 	chipsfb_write_vga(sc, CT_DACDATA, r);
    306 	chipsfb_write_vga(sc, CT_DACDATA, g);
    307 	chipsfb_write_vga(sc, CT_DACDATA, b);
    308 
    309 	return 0;
    310 }
    311 
    312 static int
    313 chipsfb_putcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    314 {
    315 	u_char *r, *g, *b;
    316 	u_int index = cm->index;
    317 	u_int count = cm->count;
    318 	int i, error;
    319 	u_char rbuf[256], gbuf[256], bbuf[256];
    320 
    321 #ifdef CHIPSFB_DEBUG
    322 	aprint_debug("putcmap: %d %d\n",index, count);
    323 #endif
    324 	if (cm->index >= 256 || cm->count > 256 ||
    325 	    (cm->index + cm->count) > 256)
    326 		return EINVAL;
    327 	error = copyin(cm->red, &rbuf[index], count);
    328 	if (error)
    329 		return error;
    330 	error = copyin(cm->green, &gbuf[index], count);
    331 	if (error)
    332 		return error;
    333 	error = copyin(cm->blue, &bbuf[index], count);
    334 	if (error)
    335 		return error;
    336 
    337 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    338 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    339 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    340 
    341 	r = &sc->sc_cmap_red[index];
    342 	g = &sc->sc_cmap_green[index];
    343 	b = &sc->sc_cmap_blue[index];
    344 
    345 	for (i = 0; i < count; i++) {
    346 		chipsfb_putpalreg(sc, index, *r, *g, *b);
    347 		index++;
    348 		r++, g++, b++;
    349 	}
    350 	return 0;
    351 }
    352 
    353 static int
    354 chipsfb_getcmap(struct chipsfb_softc *sc, struct wsdisplay_cmap *cm)
    355 {
    356 	u_int index = cm->index;
    357 	u_int count = cm->count;
    358 	int error;
    359 
    360 	if (index >= 255 || count > 256 || index + count > 256)
    361 		return EINVAL;
    362 
    363 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    364 	if (error)
    365 		return error;
    366 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    367 	if (error)
    368 		return error;
    369 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    370 	if (error)
    371 		return error;
    372 
    373 	return 0;
    374 }
    375 
    376 static void
    377 chipsfb_clearscreen(struct chipsfb_softc *sc)
    378 {
    379 	chipsfb_rectfill(sc, 0, 0, sc->width, sc->height, sc->sc_bg);
    380 }
    381 
    382 /*
    383  * wsdisplay_emulops
    384  */
    385 
    386 static void
    387 chipsfb_cursor(void *cookie, int on, int row, int col)
    388 {
    389 	struct rasops_info *ri = cookie;
    390 	struct vcons_screen *scr = ri->ri_hw;
    391 	struct chipsfb_softc *sc = scr->scr_cookie;
    392 	int x, y, wi, he;
    393 
    394 	wi = ri->ri_font->fontwidth;
    395 	he = ri->ri_font->fontheight;
    396 
    397 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    398 		x = ri->ri_ccol * wi + ri->ri_xorigin;
    399 		y = ri->ri_crow * he + ri->ri_yorigin;
    400 		if (ri->ri_flg & RI_CURSOR) {
    401 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    402 			ri->ri_flg &= ~RI_CURSOR;
    403 		}
    404 		ri->ri_crow = row;
    405 		ri->ri_ccol = col;
    406 		if (on) {
    407 			x = ri->ri_ccol * wi + ri->ri_xorigin;
    408 			y = ri->ri_crow * he + ri->ri_yorigin;
    409 			chipsfb_bitblt(sc, x, y, x, y, wi, he, ROP_NOT_DST);
    410 			ri->ri_flg |= RI_CURSOR;
    411 		}
    412 	} else {
    413 		ri->ri_flg &= ~RI_CURSOR;
    414 		ri->ri_crow = row;
    415 		ri->ri_ccol = col;
    416 	}
    417 }
    418 
    419 #if 0
    420 int
    421 chipsfb_mapchar(void *cookie, int uni, u_int *index)
    422 {
    423 	return 0;
    424 }
    425 #endif
    426 
    427 static void
    428 chipsfb_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
    429 {
    430 	struct rasops_info *ri = cookie;
    431 	struct vcons_screen *scr = ri->ri_hw;
    432 	struct chipsfb_softc *sc = scr->scr_cookie;
    433 	int32_t xs, xd, y, width, height;
    434 
    435 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    436 		xs = ri->ri_xorigin + ri->ri_font->fontwidth * srccol;
    437 		xd = ri->ri_xorigin + ri->ri_font->fontwidth * dstcol;
    438 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    439 		width = ri->ri_font->fontwidth * ncols;
    440 		height = ri->ri_font->fontheight;
    441 		chipsfb_bitblt(sc, xs, y, xd, y, width, height, ROP_COPY);
    442 	}
    443 }
    444 
    445 static void
    446 chipsfb_erasecols(void *cookie, int row, int startcol, int ncols,
    447     long fillattr)
    448 {
    449 	struct rasops_info *ri = cookie;
    450 	struct vcons_screen *scr = ri->ri_hw;
    451 	struct chipsfb_softc *sc = scr->scr_cookie;
    452 	int32_t x, y, width, height, fg, bg, ul;
    453 
    454 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    455 		x = ri->ri_xorigin + ri->ri_font->fontwidth * startcol;
    456 		y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    457 		width = ri->ri_font->fontwidth * ncols;
    458 		height = ri->ri_font->fontheight;
    459 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    460 
    461 		chipsfb_rectfill(sc, x, y, width, height, ri->ri_devcmap[bg]);
    462 	}
    463 }
    464 
    465 static void
    466 chipsfb_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
    467 {
    468 	struct rasops_info *ri = cookie;
    469 	struct vcons_screen *scr = ri->ri_hw;
    470 	struct chipsfb_softc *sc = scr->scr_cookie;
    471 	int32_t x, ys, yd, width, height;
    472 
    473 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    474 		x = ri->ri_xorigin;
    475 		ys = ri->ri_yorigin + ri->ri_font->fontheight * srcrow;
    476 		yd = ri->ri_yorigin + ri->ri_font->fontheight * dstrow;
    477 		width = ri->ri_emuwidth;
    478 		height = ri->ri_font->fontheight * nrows;
    479 		chipsfb_bitblt(sc, x, ys, x, yd, width, height, ROP_COPY);
    480 	}
    481 }
    482 
    483 static void
    484 chipsfb_eraserows(void *cookie, int row, int nrows, long fillattr)
    485 {
    486 	struct rasops_info *ri = cookie;
    487 	struct vcons_screen *scr = ri->ri_hw;
    488 	struct chipsfb_softc *sc = scr->scr_cookie;
    489 	int32_t x, y, width, height, fg, bg, ul;
    490 
    491 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    492 		rasops_unpack_attr(fillattr, &fg, &bg, &ul);
    493 		if ((row == 0) && (nrows == ri->ri_rows)) {
    494 			/* clear the whole screen */
    495 			chipsfb_rectfill(sc, 0, 0, ri->ri_width,
    496 			    ri->ri_height, ri->ri_devcmap[bg]);
    497 		} else {
    498 			x = ri->ri_xorigin;
    499 			y = ri->ri_yorigin + ri->ri_font->fontheight * row;
    500 			width = ri->ri_emuwidth;
    501 			height = ri->ri_font->fontheight * nrows;
    502 			chipsfb_rectfill(sc, x, y, width, height,
    503 			    ri->ri_devcmap[bg]);
    504 		}
    505 	}
    506 }
    507 
    508 static void
    509 chipsfb_bitblt(struct chipsfb_softc *sc, int xs, int ys, int xd, int yd,
    510     int width, int height, uint8_t rop)
    511 {
    512 	uint32_t src, dst, cmd = rop, stride, size;
    513 
    514 	cmd |= BLT_PAT_IS_SOLID;
    515 
    516 	/* we assume 8 bit for now */
    517 	src = xs + ys * sc->linebytes;
    518 	dst = xd + yd * sc->linebytes;
    519 
    520 	if (xs < xd) {
    521 		/* right-to-left operation */
    522 		cmd |= BLT_START_RIGHT;
    523 		src += width - 1;
    524 		dst += width - 1;
    525 	}
    526 
    527 	if (ys < yd) {
    528 		/* bottom-to-top operation */
    529 		cmd |= BLT_START_BOTTOM;
    530 		src += (height - 1) * sc->linebytes;
    531 		dst += (height - 1) * sc->linebytes;
    532 	}
    533 
    534 	stride = (sc->linebytes << 16) | sc->linebytes;
    535 	size = (height << 16) | width;
    536 
    537 	chipsfb_wait_idle(sc);
    538 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    539 	chipsfb_write32(sc, CT_BLT_SRCADDR, src);
    540 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    541 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    542 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    543 #ifdef CHIPSFB_WAIT
    544 	chipsfb_wait_idle(sc);
    545 #endif
    546 }
    547 
    548 static void
    549 chipsfb_rectfill(struct chipsfb_softc *sc, int x, int y, int width,
    550     int height, int colour)
    551 {
    552 	uint32_t dst, cmd, stride, size;
    553 
    554 	cmd = BLT_PAT_IS_SOLID | BLT_PAT_IS_MONO | ROP_PAT;
    555 
    556 	/* we assume 8 bit for now */
    557 	dst = x + y * sc->linebytes;
    558 
    559 	stride = (sc->linebytes << 16) | sc->linebytes;
    560 	size = (height << 16) | width;
    561 
    562 	chipsfb_wait_idle(sc);
    563 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    564 	chipsfb_write32(sc, CT_BLT_SRCADDR, dst);
    565 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    566 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    567 	chipsfb_write32(sc, CT_BLT_BG, colour);
    568 	chipsfb_write32(sc, CT_BLT_FG, colour);
    569 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    570 #ifdef CHIPSFB_WAIT
    571 	chipsfb_wait_idle(sc);
    572 #endif
    573 }
    574 
    575 static void
    576 chipsfb_putchar(void *cookie, int row, int col, u_int c, long attr)
    577 {
    578 	struct rasops_info *ri = cookie;
    579 	struct wsdisplay_font *font = PICK_FONT(ri, c);
    580 	struct vcons_screen *scr = ri->ri_hw;
    581 	struct chipsfb_softc *sc = scr->scr_cookie;
    582 
    583 	if (__predict_false((unsigned int)row > ri->ri_rows ||
    584 	    (unsigned int)col > ri->ri_cols))
    585 		return;
    586 
    587 	if (sc->sc_mode == WSDISPLAYIO_MODE_EMUL) {
    588 		uint8_t *data;
    589 		int fg, bg, uc;
    590 		int x, y, wi, he;
    591 
    592 		wi = font->fontwidth;
    593 		he = font->fontheight;
    594 
    595 		if (!CHAR_IN_FONT(c, font))
    596 			return;
    597 		bg = (u_char)ri->ri_devcmap[(attr >> 16) & 0xf];
    598 		fg = (u_char)ri->ri_devcmap[(attr >> 24) & 0xf];
    599 		x = ri->ri_xorigin + col * wi;
    600 		y = ri->ri_yorigin + row * he;
    601 		if (c == 0x20) {
    602 			chipsfb_rectfill(sc, x, y, wi, he, bg);
    603 		} else {
    604 			uc = c - font->firstchar;
    605 			data = (uint8_t *)font->data + uc *
    606 			    ri->ri_fontscale;
    607 			chipsfb_setup_mono(sc, x, y, wi, he, fg, bg);
    608 			chipsfb_feed(sc, font->stride * he, data);
    609 		}
    610 	}
    611 }
    612 
    613 static void
    614 chipsfb_setup_mono(struct chipsfb_softc *sc, int xd, int yd, int width,
    615     int height, uint32_t fg, uint32_t bg)
    616 {
    617 	uint32_t dst, cmd, stride, size;
    618 
    619 	cmd = BLT_PAT_IS_SOLID | BLT_SRC_IS_CPU | BLT_SRC_IS_MONO | ROP_COPY;
    620 
    621 	/* we assume 8 bit for now */
    622 	dst = xd + yd * sc->linebytes;
    623 
    624 	stride = (sc->linebytes << 16);
    625 	size = (height << 16) | width;
    626 
    627 	chipsfb_wait_idle(sc);
    628 	chipsfb_write32(sc, CT_BLT_STRIDE, stride);
    629 	chipsfb_write32(sc, CT_BLT_EXPCTL, MONO_SRC_ALIGN_BYTE);
    630 	chipsfb_write32(sc, CT_BLT_DSTADDR, dst);
    631 	chipsfb_write32(sc, CT_BLT_SRCADDR, 0);
    632 	chipsfb_write32(sc, CT_BLT_CONTROL, cmd);
    633 	chipsfb_write32(sc, CT_BLT_BG, bg);
    634 	chipsfb_write32(sc, CT_BLT_FG, fg);
    635 	chipsfb_write32(sc, CT_BLT_SIZE, size);
    636 }
    637 
    638 static void
    639 chipsfb_feed(struct chipsfb_softc *sc, int count, uint8_t *data)
    640 {
    641 	int i;
    642 	uint32_t latch = 0, bork;
    643 	int shift = 0;
    644 
    645 	for (i = 0; i < count; i++) {
    646 		bork = data[i];
    647 		latch |= (bork << shift);
    648 		if (shift == 24) {
    649 			chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
    650 			latch = 0;
    651 			shift = 0;
    652 		} else
    653 			shift += 8;
    654 	}
    655 
    656 	if (shift != 0) {
    657 		chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, latch);
    658 	}
    659 
    660 	/* apparently the chip wants 64bit-aligned data or it won't go idle */
    661 	if ((count + 3) & 0x04) {
    662 		chipsfb_write32(sc, CT_OFF_DATA - CT_OFF_BITBLT, 0);
    663 	}
    664 #ifdef CHIPSFB_WAIT
    665 	chipsfb_wait_idle(sc);
    666 #endif
    667 }
    668 
    669 #if 0
    670 static void
    671 chipsfb_showpal(struct chipsfb_softc *sc)
    672 {
    673 	int i, x = 0;
    674 
    675 	for (i = 0; i < 16; i++) {
    676 		chipsfb_rectfill(sc, x, 0, 64, 64, i);
    677 		x += 64;
    678 	}
    679 }
    680 #endif
    681 
    682 #if 0
    683 static int
    684 chipsfb_allocattr(void *cookie, int fg, int bg, int flags, long *attrp)
    685 {
    686 
    687 	return 0;
    688 }
    689 #endif
    690 
    691 static void
    692 chipsfb_restore_palette(struct chipsfb_softc *sc)
    693 {
    694 	int i;
    695 
    696 	for (i = 0; i < 256; i++) {
    697 		chipsfb_putpalreg(sc,
    698 		   i,
    699 		   sc->sc_cmap_red[i],
    700 		   sc->sc_cmap_green[i],
    701 		   sc->sc_cmap_blue[i]);
    702 	}
    703 }
    704 
    705 /*
    706  * wsdisplay_accessops
    707  */
    708 
    709 static int
    710 chipsfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    711 	struct lwp *l)
    712 {
    713 	struct vcons_data *vd = v;
    714 	struct chipsfb_softc *sc = vd->cookie;
    715 	struct wsdisplay_fbinfo *wdf;
    716 	struct vcons_screen *ms = vd->active;
    717 
    718 	switch (cmd) {
    719 	case WSDISPLAYIO_GTYPE:
    720 		*(u_int *)data = WSDISPLAY_TYPE_PCIMISC;
    721 		return 0;
    722 
    723 	case WSDISPLAYIO_GINFO:
    724 		wdf = (void *)data;
    725 		wdf->height = ms->scr_ri.ri_height;
    726 		wdf->width = ms->scr_ri.ri_width;
    727 		wdf->depth = ms->scr_ri.ri_depth;
    728 		wdf->cmsize = 256;
    729 		return 0;
    730 
    731 	case WSDISPLAYIO_GETCMAP:
    732 		return chipsfb_getcmap(sc,
    733 		    (struct wsdisplay_cmap *)data);
    734 
    735 	case WSDISPLAYIO_PUTCMAP:
    736 		return chipsfb_putcmap(sc,
    737 		    (struct wsdisplay_cmap *)data);
    738 
    739 
    740 	case WSDISPLAYIO_SMODE: {
    741 		int new_mode = *(int*)data;
    742 		if (new_mode != sc->sc_mode) {
    743 			sc->sc_mode = new_mode;
    744 			if(new_mode == WSDISPLAYIO_MODE_EMUL) {
    745 				chipsfb_restore_palette(sc);
    746 				vcons_redraw_screen(ms);
    747 			}
    748 		}
    749 		}
    750 		return 0;
    751 	default:
    752 		if (sc->sc_ioctl != NULL)
    753 			return sc->sc_ioctl(v, vs, cmd, data, flag, l);
    754 	}
    755 	return EPASSTHROUGH;
    756 }
    757 
    758 static paddr_t
    759 chipsfb_mmap(void *v, void *vs, off_t offset, int prot)
    760 {
    761 	struct vcons_data *vd = v;
    762 	struct chipsfb_softc *sc = vd->cookie;
    763 	paddr_t pa;
    764 
    765 	if (sc->sc_mmap != NULL)
    766 		return sc->sc_mmap(v, vs, offset, prot);
    767 
    768 	/* 'regular' framebuffer mmap()ing */
    769 	if (offset < sc->memsize) {
    770 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    771 		    BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE);
    772 		return pa;
    773 	}
    774 
    775 	/*
    776 	 * restrict all other mappings to processes with superuser privileges
    777 	 * or the kernel itself
    778 	 */
    779 	if (kauth_authorize_machdep(kauth_cred_get(), KAUTH_MACHDEP_UNMANAGEDMEM,
    780 	    NULL, NULL, NULL, NULL) != 0) {
    781 		aprint_normal_dev(sc->sc_dev, "mmap() rejected.\n");
    782 		return -1;
    783 	}
    784 
    785 	if ((offset >= sc->sc_fb) && (offset < (sc->sc_fb + sc->sc_fbsize))) {
    786 		pa = bus_space_mmap(sc->sc_memt, offset, 0, prot,
    787 		    BUS_SPACE_MAP_LINEAR);
    788 		return pa;
    789 	}
    790 
    791 #ifdef PCI_MAGIC_IO_RANGE
    792 	/* allow mapping of IO space */
    793 	if ((offset >= PCI_MAGIC_IO_RANGE) &&
    794 	    (offset < PCI_MAGIC_IO_RANGE + 0x10000)) {
    795 		pa = bus_space_mmap(sc->sc_iot, offset - PCI_MAGIC_IO_RANGE,
    796 		    0, prot, BUS_SPACE_MAP_LINEAR);
    797 		return pa;
    798 	}
    799 #endif
    800 
    801 	return -1;
    802 }
    803 
    804 static void
    805 chipsfb_init_screen(void *cookie, struct vcons_screen *scr,
    806     int existing, long *defattr)
    807 {
    808 	struct chipsfb_softc *sc = cookie;
    809 	struct rasops_info *ri = &scr->scr_ri;
    810 
    811 	ri->ri_depth = sc->bits_per_pixel;
    812 	ri->ri_width = sc->width;
    813 	ri->ri_height = sc->height;
    814 	ri->ri_stride = sc->width;
    815 	ri->ri_flg = RI_CENTER | RI_FULLCLEAR | RI_8BIT_IS_RGB;
    816 
    817 	ri->ri_bits = bus_space_vaddr(sc->sc_memt, sc->sc_fbh);
    818 
    819 #ifdef CHIPSFB_DEBUG
    820 	aprint_debug("addr: %08lx\n", (ulong)ri->ri_bits);
    821 #endif
    822 	if (existing) {
    823 		ri->ri_flg |= RI_CLEAR;
    824 	}
    825 
    826 	rasops_init(ri, 0, 0);
    827 	ri->ri_caps = WSSCREEN_WSCOLORS;
    828 
    829 	rasops_reconfig(ri, sc->height / ri->ri_font->fontheight,
    830 		    sc->width / ri->ri_font->fontwidth);
    831 
    832 	ri->ri_hw = scr;
    833 	ri->ri_ops.copyrows = chipsfb_copyrows;
    834 	ri->ri_ops.copycols = chipsfb_copycols;
    835 	ri->ri_ops.eraserows = chipsfb_eraserows;
    836 	ri->ri_ops.erasecols = chipsfb_erasecols;
    837 	ri->ri_ops.cursor = chipsfb_cursor;
    838 	ri->ri_ops.putchar = chipsfb_putchar;
    839 }
    840 
    841 #if 0
    842 int
    843 chipsfb_load_font(void *v, void *cookie, struct wsdisplay_font *data)
    844 {
    845 
    846 	return 0;
    847 }
    848 #endif
    849 
    850 static void
    851 chipsfb_init(struct chipsfb_softc *sc)
    852 {
    853 
    854 	chipsfb_wait_idle(sc);
    855 
    856 	/* setup the blitter */
    857 }
    858 
    859 uint32_t
    860 chipsfb_probe_vram(struct chipsfb_softc *sc)
    861 {
    862 	uint32_t ofs = 0x00080000;	/* 512kB */
    863 
    864 	/*
    865 	 * advance in 0.5MB steps, see if we can read back what we wrote and
    866 	 * if what we wrote to 0 is left untouched. Max. fb size is 4MB so
    867 	 * we voluntarily stop there.
    868 	 */
    869 	bus_space_write_4(sc->sc_memt, sc->sc_fbh, 0, 0xf0f0f0f0);
    870 	bus_space_write_4(sc->sc_memt, sc->sc_fbh, ofs, 0x0f0f0f0f);
    871 	while ((bus_space_read_4(sc->sc_memt, sc->sc_fbh, 0) == 0xf0f0f0f0) &&
    872 	    (bus_space_read_4(sc->sc_memt, sc->sc_fbh, ofs) == 0x0f0f0f0f) &&
    873 	    (ofs < 0x00400000)) {
    874 
    875 		ofs += 0x00080000;
    876 		bus_space_write_4(sc->sc_memt, sc->sc_fbh, ofs, 0x0f0f0f0f);
    877 	}
    878 
    879 	return ofs;
    880 }
    881