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