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