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