Home | History | Annotate | Line # | Download | only in dev
lunafb.c revision 1.15.44.1
      1 /* $NetBSD: lunafb.c,v 1.15.44.1 2008/05/16 02:22:43 yamt Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33 
     34 __KERNEL_RCSID(0, "$NetBSD: lunafb.c,v 1.15.44.1 2008/05/16 02:22:43 yamt Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/conf.h>
     39 #include <sys/device.h>
     40 #include <sys/ioctl.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mman.h>
     43 #include <sys/proc.h>
     44 #include <sys/tty.h>
     45 #include <sys/errno.h>
     46 #include <sys/buf.h>
     47 
     48 #include <uvm/uvm_extern.h>
     49 
     50 #include <dev/rcons/raster.h>
     51 #include <dev/wscons/wsconsio.h>
     52 #include <dev/wscons/wscons_raster.h>
     53 #include <dev/wscons/wsdisplayvar.h>
     54 
     55 #include <machine/cpu.h>
     56 #include <machine/autoconf.h>
     57 
     58 struct bt454 {
     59 	u_int8_t bt_addr;		/* map address register */
     60 	u_int8_t bt_cmap;		/* colormap data register */
     61 };
     62 
     63 struct bt458 {
     64 	u_int8_t bt_addr;		/* map address register */
     65 		unsigned :24;
     66 	u_int8_t bt_cmap;		/* colormap data register */
     67 		unsigned :24;
     68 	u_int8_t bt_ctrl;		/* control register */
     69 		unsigned :24;
     70 	u_int8_t bt_omap;		/* overlay (cursor) map register */
     71 		unsigned :24;
     72 };
     73 
     74 #define	OMFB_RFCNT	0xB1000000	/* video h-origin/v-origin */
     75 #define	OMFB_PLANEMASK	0xB1040000	/* planemask register */
     76 #define	OMFB_FB_WADDR	0xB1080008	/* common plane */
     77 #define	OMFB_FB_RADDR	0xB10C0008	/* plane #0 */
     78 #define	OMFB_ROPFUNC	0xB12C0000	/* ROP function code */
     79 #define	OMFB_RAMDAC	0xC1100000	/* Bt454/Bt458 RAMDAC */
     80 #define	OMFB_SIZE	(0xB1300000 - 0xB1080000 + PAGE_SIZE)
     81 
     82 struct om_hwdevconfig {
     83 	int	dc_wid;			/* width of frame buffer */
     84 	int	dc_ht;			/* height of frame buffer */
     85 	int	dc_depth;		/* depth, bits per pixel */
     86 	int	dc_rowbytes;		/* bytes in a FB scan line */
     87 	int	dc_cmsize;		/* colormap size */
     88 	vaddr_t	dc_videobase;		/* base of flat frame buffer */
     89 	struct raster	dc_raster;	/* raster description */
     90 	struct rcons	dc_rcons;	/* raster blitter control info */
     91 };
     92 
     93 struct hwcmap {
     94 #define CMAP_SIZE 256
     95 	u_int8_t r[CMAP_SIZE];
     96 	u_int8_t g[CMAP_SIZE];
     97 	u_int8_t b[CMAP_SIZE];
     98 };
     99 
    100 struct omfb_softc {
    101 	struct device sc_dev;		/* base device */
    102 	struct om_hwdevconfig *sc_dc;	/* device configuration */
    103 	struct hwcmap sc_cmap;		/* software copy of colormap */
    104 	int nscreens;
    105 };
    106 
    107 static int  omgetcmap __P((struct omfb_softc *, struct wsdisplay_cmap *));
    108 static int  omsetcmap __P((struct omfb_softc *, struct wsdisplay_cmap *));
    109 
    110 static struct om_hwdevconfig omfb_console_dc;
    111 static void omfb_getdevconfig __P((paddr_t, struct om_hwdevconfig *));
    112 
    113 extern struct wsdisplay_emulops omfb_emulops;
    114 
    115 static struct wsscreen_descr omfb_stdscreen = {
    116 	"std", 0, 0,
    117 	&omfb_emulops,
    118 	0, 0,
    119 	0
    120 };
    121 
    122 static const struct wsscreen_descr *_omfb_scrlist[] = {
    123 	&omfb_stdscreen,
    124 };
    125 
    126 static const struct wsscreen_list omfb_screenlist = {
    127 	sizeof(_omfb_scrlist) / sizeof(struct wsscreen_descr *), _omfb_scrlist
    128 };
    129 
    130 static int   omfbioctl __P((void *, void *, u_long, void *, int,
    131 		            struct lwp *));
    132 static paddr_t omfbmmap __P((void *, void *, off_t, int));
    133 static int   omfb_alloc_screen __P((void *, const struct wsscreen_descr *,
    134 				      void **, int *, int *, long *));
    135 static void  omfb_free_screen __P((void *, void *));
    136 static int   omfb_show_screen __P((void *, void *, int,
    137 				void (*) (void *, int, int), void *));
    138 
    139 static const struct wsdisplay_accessops omfb_accessops = {
    140 	omfbioctl,
    141 	omfbmmap,
    142 	omfb_alloc_screen,
    143 	omfb_free_screen,
    144 	omfb_show_screen,
    145 	0 /* load_font */
    146 };
    147 
    148 static int  omfbmatch __P((struct device *, struct cfdata *, void *));
    149 static void omfbattach __P((struct device *, struct device *, void *));
    150 
    151 CFATTACH_DECL(fb, sizeof(struct omfb_softc),
    152     omfbmatch, omfbattach, NULL, NULL);
    153 extern struct cfdriver fb_cd;
    154 
    155 extern int hwplanemask;	/* hardware planemask; retrieved at boot */
    156 
    157 static int omfb_console;
    158 int  omfb_cnattach __P((void));
    159 
    160 static int
    161 omfbmatch(parent, cf, aux)
    162 	struct device *parent;
    163 	struct cfdata *cf;
    164 	void *aux;
    165 {
    166 	struct mainbus_attach_args *ma = aux;
    167 
    168 	if (strcmp(ma->ma_name, fb_cd.cd_name))
    169 		return (0);
    170 #if 0	/* XXX badaddr() bombs if no framebuffer is installed */
    171 	if (badaddr((void *)ma->ma_addr, 4))
    172 		return (0);
    173 #else
    174 	if (hwplanemask == 0)
    175 		return (0);
    176 #endif
    177 	return (1);
    178 }
    179 
    180 static void
    181 omfbattach(parent, self, args)
    182 	struct device *parent, *self;
    183 	void *args;
    184 {
    185 	struct omfb_softc *sc = (struct omfb_softc *)self;
    186 	struct wsemuldisplaydev_attach_args waa;
    187 
    188 	if (omfb_console) {
    189 		sc->sc_dc = &omfb_console_dc;
    190 		sc->nscreens = 1;
    191 	}
    192 	else {
    193 		sc->sc_dc = (struct om_hwdevconfig *)
    194 		    malloc(sizeof(struct om_hwdevconfig), M_DEVBUF, M_WAITOK);
    195 		omfb_getdevconfig(OMFB_FB_WADDR, sc->sc_dc);
    196 	}
    197 	printf(": %d x %d, %dbpp\n", sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
    198 	    sc->sc_dc->dc_depth);
    199 
    200 #if 0	/* WHITE on BLACK */
    201 	cm = &sc->sc_cmap;
    202 	memset(cm, 255, sizeof(struct hwcmap));
    203 	cm->r[0] = cm->g[0] = cm->b[0] = 0;
    204 #endif
    205 	waa.console = omfb_console;
    206 	waa.scrdata = &omfb_screenlist;
    207 	waa.accessops = &omfb_accessops;
    208 	waa.accesscookie = sc;
    209 
    210 	config_found(self, &waa, wsemuldisplaydevprint);
    211 }
    212 
    213 /* EXPORT */ int
    214 omfb_cnattach()
    215 {
    216 	struct om_hwdevconfig *dc = &omfb_console_dc;
    217 	long defattr;
    218 
    219 	omfb_getdevconfig(OMFB_FB_WADDR, dc);
    220 	(*omfb_emulops.allocattr)(&dc->dc_rcons, 0, 0, 0, &defattr);
    221 	wsdisplay_cnattach(&omfb_stdscreen, &dc->dc_rcons, 0, 0, defattr);
    222 	omfb_console = 1;
    223 	return (0);
    224 }
    225 
    226 static int
    227 omfbioctl(v, vs, cmd, data, flag, l)
    228 	void *v;
    229 	void *vs;
    230 	u_long cmd;
    231 	void *data;
    232 	int flag;
    233 	struct lwp *l;
    234 {
    235 	struct omfb_softc *sc = v;
    236 	struct om_hwdevconfig *dc = sc->sc_dc;
    237 
    238 	switch (cmd) {
    239 	case WSDISPLAYIO_GTYPE:
    240 		*(u_int *)data = 0x19990927;
    241 		return (0);
    242 
    243 	case WSDISPLAYIO_GINFO:
    244 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    245 		wsd_fbip->height = dc->dc_ht;
    246 		wsd_fbip->width = dc->dc_wid;
    247 		wsd_fbip->depth = dc->dc_depth;
    248 		wsd_fbip->cmsize = dc->dc_cmsize;
    249 #undef fbt
    250 		return (0);
    251 
    252 	case WSDISPLAYIO_GETCMAP:
    253 		return omgetcmap(sc, (struct wsdisplay_cmap *)data);
    254 
    255 	case WSDISPLAYIO_PUTCMAP:
    256 		return omsetcmap(sc, (struct wsdisplay_cmap *)data);
    257 
    258 	case WSDISPLAYIO_SVIDEO:
    259 	case WSDISPLAYIO_GVIDEO:
    260 	case WSDISPLAYIO_GCURPOS:
    261 	case WSDISPLAYIO_SCURPOS:
    262 	case WSDISPLAYIO_GCURMAX:
    263 	case WSDISPLAYIO_GCURSOR:
    264 	case WSDISPLAYIO_SCURSOR:
    265 		break;
    266 	}
    267 	return (EPASSTHROUGH);
    268 }
    269 
    270 /*
    271  * Return the address that would map the given device at the given
    272  * offset, allowing for the given protection, or return -1 for error.
    273  */
    274 static paddr_t
    275 omfbmmap(v, vs, offset, prot)
    276 	void *v;
    277 	void *vs;
    278 	off_t offset;
    279 	int prot;
    280 {
    281 	struct omfb_softc *sc = v;
    282 
    283 	if (offset >= OMFB_SIZE || offset < 0)
    284 		return (-1);
    285 	return m68k_btop(m68k_trunc_page(sc->sc_dc->dc_videobase) + offset);
    286 }
    287 
    288 static int
    289 omgetcmap(sc, p)
    290 	struct omfb_softc *sc;
    291 	struct wsdisplay_cmap *p;
    292 {
    293 	u_int index = p->index, count = p->count;
    294 	int cmsize, error;
    295 
    296 	cmsize = sc->sc_dc->dc_cmsize;
    297 	if (index >= cmsize || count > cmsize - index)
    298 		return (EINVAL);
    299 
    300 	error = copyout(&sc->sc_cmap.r[index], p->red, count);
    301 	if (error)
    302 		return error;
    303 	error = copyout(&sc->sc_cmap.g[index], p->green, count);
    304 	if (error)
    305 		return error;
    306 	error = copyout(&sc->sc_cmap.b[index], p->blue, count);
    307 	return error;
    308 }
    309 
    310 static int
    311 omsetcmap(sc, p)
    312 	struct omfb_softc *sc;
    313 	struct wsdisplay_cmap *p;
    314 {
    315 	struct hwcmap cmap;
    316 	u_int index = p->index, count = p->count;
    317 	int cmsize, i, error;
    318 
    319 	cmsize = sc->sc_dc->dc_cmsize;
    320 	if (index >= cmsize || (index + count) > cmsize)
    321 		return (EINVAL);
    322 
    323 	error = copyin(p->red, &cmap.r[index], count);
    324 	if (error)
    325 		return error;
    326 	error = copyin(p->green, &cmap.g[index], count);
    327 	if (error)
    328 		return error;
    329 	error = copyin(p->blue, &cmap.b[index], count);
    330 	if (error)
    331 		return error;
    332 
    333 	memcpy(&sc->sc_cmap.r[index], &cmap.r[index], count);
    334 	memcpy(&sc->sc_cmap.g[index], &cmap.g[index], count);
    335 	memcpy(&sc->sc_cmap.b[index], &cmap.b[index], count);
    336 	if (hwplanemask == 0x0f) {
    337 		struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC;
    338 		odac->bt_addr = index;
    339 		for (i = index; i < count; i++) {
    340 			odac->bt_cmap = sc->sc_cmap.r[i];
    341 			odac->bt_cmap = sc->sc_cmap.g[i];
    342 			odac->bt_cmap = sc->sc_cmap.b[i];
    343 		}
    344 	}
    345 	else if (hwplanemask == 0xff) {
    346 		struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC;
    347 		ndac->bt_addr = index;
    348 		for (i = index; i < count; i++) {
    349 			ndac->bt_cmap = sc->sc_cmap.r[i];
    350 			ndac->bt_cmap = sc->sc_cmap.g[i];
    351 			ndac->bt_cmap = sc->sc_cmap.b[i];
    352 		}
    353 	}
    354 	return (0);
    355 }
    356 
    357 static void
    358 omfb_getdevconfig(paddr, dc)
    359 	paddr_t paddr;
    360 	struct om_hwdevconfig *dc;
    361 {
    362 	int bpp, i;
    363 	struct raster *rap;
    364 	struct rcons *rcp;
    365 	union {
    366 		struct { short h, v; } p;
    367 		u_int32_t u;
    368 	} rfcnt;
    369 
    370 	switch (hwplanemask) {
    371 	case 0xff:
    372 		bpp = 8;	/* XXX check monochrome bit in DIPSW */
    373 		break;
    374 	default:
    375 	case 0x0f:
    376 		bpp = 4;	/* XXX check monochrome bit in DIPSW */
    377 		break;
    378 	case 1:
    379 		bpp = 1;
    380 		break;
    381 	}
    382 	dc->dc_wid = 1280;
    383 	dc->dc_ht = 1024;
    384 	dc->dc_depth = bpp;
    385 	dc->dc_rowbytes = 2048 / 8;
    386 	dc->dc_cmsize = (bpp == 1) ? 0 : 1 << bpp;
    387 	dc->dc_videobase = paddr;
    388 
    389 #if 0 /* WHITE on BLACK XXX experiment resulted in WHITE on SKYBLUE... */
    390 	if (hwplanemask == 0x0f) {
    391 		/* XXX Need Bt454 initialization */
    392 		struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC;
    393 		odac->bt_addr = 0;
    394 		odac->bt_cmap = 0;
    395 		odac->bt_cmap = 0;
    396 		odac->bt_cmap = 0;
    397 		for (i = 1; i < 16; i++) {
    398 			odac->bt_cmap = 255;
    399 			odac->bt_cmap = 255;
    400 			odac->bt_cmap = 255;
    401 		}
    402 	}
    403 	else if (hwplanemask == 0xff) {
    404 		struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC;
    405 
    406 		ndac->bt_addr = 0x04;
    407 		ndac->bt_ctrl = 0xff; /* all planes will be read */
    408 		ndac->bt_ctrl = 0x00; /* all planes have non-blink */
    409 		ndac->bt_ctrl = 0x43; /* pallete enabled, ovly plane */
    410 		ndac->bt_ctrl = 0x00; /* no test mode */
    411 		ndac->bt_addr = 0;
    412 		ndac->bt_cmap = 0;
    413 		ndac->bt_cmap = 0;
    414 		ndac->bt_cmap = 0;
    415 		for (i = 1; i < 256; i++) {
    416 			ndac->bt_cmap = 255;
    417 			ndac->bt_cmap = 255;
    418 			ndac->bt_cmap = 255;
    419 		}
    420 	}
    421 #endif
    422 
    423 	/* adjust h/v orgin on screen */
    424 	rfcnt.p.h = 7;
    425 	rfcnt.p.v = -27;
    426 	*(u_int32_t *)OMFB_RFCNT = rfcnt.u; /* single write of 0x007ffe6 */
    427 
    428 	/* clear the screen */
    429 	*(u_int32_t *)OMFB_PLANEMASK = 0xff;
    430 	((u_int32_t *)OMFB_ROPFUNC)[5] = ~0;	/* ROP copy */
    431 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes/sizeof(u_int32_t); i++)
    432 		*((u_int32_t *)dc->dc_videobase + i) = 0;
    433 	*(u_int32_t *)OMFB_PLANEMASK = 0x01;
    434 
    435 	/* initialize the raster */
    436 	rap = &dc->dc_raster;
    437 	rap->width = dc->dc_wid;
    438 	rap->height = dc->dc_ht;
    439 	rap->depth = dc->dc_depth;
    440 	rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
    441 	rap->pixels = (u_int32_t *)dc->dc_videobase;
    442 
    443 	/* initialize the raster console blitter */
    444 	rcp = &dc->dc_rcons;
    445 	rcp->rc_sp = rap;
    446 	rcp->rc_crow = rcp->rc_ccol = -1;
    447 	rcp->rc_crowp = &rcp->rc_crow;
    448 	rcp->rc_ccolp = &rcp->rc_ccol;
    449 	rcons_init(rcp, 34, 80);
    450 
    451 	omfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
    452 	omfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
    453 }
    454 
    455 static int
    456 omfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    457 	void *v;
    458 	const struct wsscreen_descr *type;
    459 	void **cookiep;
    460 	int *curxp, *curyp;
    461 	long *attrp;
    462 {
    463 	struct omfb_softc *sc = v;
    464 	long defattr;
    465 
    466 	if (sc->nscreens > 0)
    467 		return (ENOMEM);
    468 
    469 	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
    470 	*curxp = 0;
    471 	*curyp = 0;
    472 	(*omfb_emulops.allocattr)(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
    473 	*attrp = defattr;
    474 	sc->nscreens++;
    475 	return (0);
    476 }
    477 
    478 static void
    479 omfb_free_screen(v, cookie)
    480 	void *v;
    481 	void *cookie;
    482 {
    483 	struct omfb_softc *sc = v;
    484 
    485 	if (sc->sc_dc == &omfb_console_dc)
    486 		panic("omfb_free_screen: console");
    487 
    488 	sc->nscreens--;
    489 }
    490 
    491 static int
    492 omfb_show_screen(v, cookie, waitok, cb, cbarg)
    493 	void *v;
    494 	void *cookie;
    495 	int waitok;
    496 	void (*cb) __P((void *, int, int));
    497 	void *cbarg;
    498 {
    499 	return 0;
    500 }
    501