Home | History | Annotate | Line # | Download | only in dev
lunafb.c revision 1.12
      1 /* $NetBSD: lunafb.c,v 1.12 2003/11/13 03:09:28 chs 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.12 2003/11/13 03:09:28 chs 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 
     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 + PAGE_SIZE)
     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 paddr_t 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 CFATTACH_DECL(fb, sizeof(struct omfb_softc),
    158     omfbmatch, omfbattach, NULL, NULL);
    159 extern struct cfdriver fb_cd;
    160 
    161 extern int hwplanemask;	/* hardware planemask; retrieved at boot */
    162 
    163 static int omfb_console;
    164 int  omfb_cnattach __P((void));
    165 
    166 static int
    167 omfbmatch(parent, cf, aux)
    168 	struct device *parent;
    169 	struct cfdata *cf;
    170 	void *aux;
    171 {
    172 	struct mainbus_attach_args *ma = aux;
    173 
    174 	if (strcmp(ma->ma_name, fb_cd.cd_name))
    175 		return (0);
    176 #if 0	/* XXX badaddr() bombs if no framebuffer is installed */
    177 	if (badaddr((caddr_t)ma->ma_addr, 4))
    178 		return (0);
    179 #else
    180 	if (hwplanemask == 0)
    181 		return (0);
    182 #endif
    183 	return (1);
    184 }
    185 
    186 static void
    187 omfbattach(parent, self, args)
    188 	struct device *parent, *self;
    189 	void *args;
    190 {
    191 	struct omfb_softc *sc = (struct omfb_softc *)self;
    192 	struct wsemuldisplaydev_attach_args waa;
    193 
    194 	if (omfb_console) {
    195 		sc->sc_dc = &omfb_console_dc;
    196 		sc->nscreens = 1;
    197 	}
    198 	else {
    199 		sc->sc_dc = (struct om_hwdevconfig *)
    200 		    malloc(sizeof(struct om_hwdevconfig), M_DEVBUF, M_WAITOK);
    201 		omfb_getdevconfig(OMFB_FB_WADDR, sc->sc_dc);
    202 	}
    203 	printf(": %d x %d, %dbpp\n", sc->sc_dc->dc_wid, sc->sc_dc->dc_ht,
    204 	    sc->sc_dc->dc_depth);
    205 
    206 #if 0	/* WHITE on BLACK */
    207 	cm = &sc->sc_cmap;
    208 	memset(cm, 255, sizeof(struct hwcmap));
    209 	cm->r[0] = cm->g[0] = cm->b[0] = 0;
    210 #endif
    211 	waa.console = omfb_console;
    212 	waa.scrdata = &omfb_screenlist;
    213 	waa.accessops = &omfb_accessops;
    214 	waa.accesscookie = sc;
    215 
    216 	config_found(self, &waa, wsemuldisplaydevprint);
    217 }
    218 
    219 /* EXPORT */ int
    220 omfb_cnattach()
    221 {
    222 	struct om_hwdevconfig *dc = &omfb_console_dc;
    223 	long defattr;
    224 
    225 	omfb_getdevconfig(OMFB_FB_WADDR, dc);
    226 	(*omfb_emulops.allocattr)(&dc->dc_rcons, 0, 0, 0, &defattr);
    227 	wsdisplay_cnattach(&omfb_stdscreen, &dc->dc_rcons, 0, 0, defattr);
    228 	omfb_console = 1;
    229 	return (0);
    230 }
    231 
    232 static int
    233 omfbioctl(v, cmd, data, flag, p)
    234 	void *v;
    235 	u_long cmd;
    236 	caddr_t data;
    237 	int flag;
    238 	struct proc *p;
    239 {
    240 	struct omfb_softc *sc = v;
    241 	struct om_hwdevconfig *dc = sc->sc_dc;
    242 
    243 	switch (cmd) {
    244 	case WSDISPLAYIO_GTYPE:
    245 		*(u_int *)data = 0x19990927;
    246 		return (0);
    247 
    248 	case WSDISPLAYIO_GINFO:
    249 #define	wsd_fbip ((struct wsdisplay_fbinfo *)data)
    250 		wsd_fbip->height = dc->dc_ht;
    251 		wsd_fbip->width = dc->dc_wid;
    252 		wsd_fbip->depth = dc->dc_depth;
    253 		wsd_fbip->cmsize = dc->dc_cmsize;
    254 #undef fbt
    255 		return (0);
    256 
    257 	case WSDISPLAYIO_GETCMAP:
    258 		return omgetcmap(sc, (struct wsdisplay_cmap *)data);
    259 
    260 	case WSDISPLAYIO_PUTCMAP:
    261 		return omsetcmap(sc, (struct wsdisplay_cmap *)data);
    262 
    263 	case WSDISPLAYIO_SVIDEO:
    264 	case WSDISPLAYIO_GVIDEO:
    265 	case WSDISPLAYIO_GCURPOS:
    266 	case WSDISPLAYIO_SCURPOS:
    267 	case WSDISPLAYIO_GCURMAX:
    268 	case WSDISPLAYIO_GCURSOR:
    269 	case WSDISPLAYIO_SCURSOR:
    270 		break;
    271 	}
    272 	return (EPASSTHROUGH);
    273 }
    274 
    275 /*
    276  * Return the address that would map the given device at the given
    277  * offset, allowing for the given protection, or return -1 for error.
    278  */
    279 static paddr_t
    280 omfbmmap(v, offset, prot)
    281 	void *v;
    282 	off_t offset;
    283 	int prot;
    284 {
    285 	struct omfb_softc *sc = v;
    286 
    287 	if (offset >= OMFB_SIZE || offset < 0)
    288 		return (-1);
    289 	return m68k_btop(m68k_trunc_page(sc->sc_dc->dc_videobase) + offset);
    290 }
    291 
    292 static int
    293 omgetcmap(sc, p)
    294 	struct omfb_softc *sc;
    295 	struct wsdisplay_cmap *p;
    296 {
    297 	u_int index = p->index, count = p->count;
    298 	int cmsize, error;
    299 
    300 	cmsize = sc->sc_dc->dc_cmsize;
    301 	if (index >= cmsize || count > cmsize - index)
    302 		return (EINVAL);
    303 
    304 	error = copyout(&sc->sc_cmap.r[index], p->red, count);
    305 	if (error)
    306 		return error;
    307 	error = copyout(&sc->sc_cmap.g[index], p->green, count);
    308 	if (error)
    309 		return error;
    310 	error = copyout(&sc->sc_cmap.b[index], p->blue, count);
    311 	return error;
    312 }
    313 
    314 static int
    315 omsetcmap(sc, p)
    316 	struct omfb_softc *sc;
    317 	struct wsdisplay_cmap *p;
    318 {
    319 	struct hwcmap cmap;
    320 	u_int index = p->index, count = p->count;
    321 	int cmsize, i, error;
    322 
    323 	cmsize = sc->sc_dc->dc_cmsize;
    324 	if (index >= cmsize || (index + count) > cmsize)
    325 		return (EINVAL);
    326 
    327 	error = copyin(p->red, &cmap.r[index], count);
    328 	if (error)
    329 		return error;
    330 	error = copyin(p->green, &cmap.g[index], count);
    331 	if (error)
    332 		return error;
    333 	error = copyin(p->blue, &cmap.b[index], count);
    334 	if (error)
    335 		return error;
    336 
    337 	memcpy(&sc->sc_cmap.r[index], &cmap.r[index], count);
    338 	memcpy(&sc->sc_cmap.g[index], &cmap.g[index], count);
    339 	memcpy(&sc->sc_cmap.b[index], &cmap.b[index], count);
    340 	if (hwplanemask == 0x0f) {
    341 		struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC;
    342 		odac->bt_addr = index;
    343 		for (i = index; i < count; i++) {
    344 			odac->bt_cmap = sc->sc_cmap.r[i];
    345 			odac->bt_cmap = sc->sc_cmap.g[i];
    346 			odac->bt_cmap = sc->sc_cmap.b[i];
    347 		}
    348 	}
    349 	else if (hwplanemask == 0xff) {
    350 		struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC;
    351 		ndac->bt_addr = index;
    352 		for (i = index; i < count; i++) {
    353 			ndac->bt_cmap = sc->sc_cmap.r[i];
    354 			ndac->bt_cmap = sc->sc_cmap.g[i];
    355 			ndac->bt_cmap = sc->sc_cmap.b[i];
    356 		}
    357 	}
    358 	return (0);
    359 }
    360 
    361 static void
    362 omfb_getdevconfig(paddr, dc)
    363 	paddr_t paddr;
    364 	struct om_hwdevconfig *dc;
    365 {
    366 	int bpp, i;
    367 	struct raster *rap;
    368 	struct rcons *rcp;
    369 	union {
    370 		struct { short h, v; } p;
    371 		u_int32_t u;
    372 	} rfcnt;
    373 
    374 	switch (hwplanemask) {
    375 	case 0xff:
    376 		bpp = 8;	/* XXX check monochrome bit in DIPSW */
    377 		break;
    378 	default:
    379 	case 0x0f:
    380 		bpp = 4;	/* XXX check monochrome bit in DIPSW */
    381 		break;
    382 	case 1:
    383 		bpp = 1;
    384 		break;
    385 	}
    386 	dc->dc_wid = 1280;
    387 	dc->dc_ht = 1024;
    388 	dc->dc_depth = bpp;
    389 	dc->dc_rowbytes = 2048 / 8;
    390 	dc->dc_cmsize = (bpp == 1) ? 0 : 1 << bpp;
    391 	dc->dc_videobase = paddr;
    392 
    393 #if 0 /* WHITE on BLACK XXX experiment resulted in WHITE on SKYBLUE... */
    394 	if (hwplanemask == 0x0f) {
    395 		/* XXX Need Bt454 initialization */
    396 		struct bt454 *odac = (struct bt454 *)OMFB_RAMDAC;
    397 		odac->bt_addr = 0;
    398 		odac->bt_cmap = 0;
    399 		odac->bt_cmap = 0;
    400 		odac->bt_cmap = 0;
    401 		for (i = 1; i < 16; i++) {
    402 			odac->bt_cmap = 255;
    403 			odac->bt_cmap = 255;
    404 			odac->bt_cmap = 255;
    405 		}
    406 	}
    407 	else if (hwplanemask == 0xff) {
    408 		struct bt458 *ndac = (struct bt458 *)OMFB_RAMDAC;
    409 
    410 		ndac->bt_addr = 0x04;
    411 		ndac->bt_ctrl = 0xff; /* all planes will be read */
    412 		ndac->bt_ctrl = 0x00; /* all planes have non-blink */
    413 		ndac->bt_ctrl = 0x43; /* pallete enabled, ovly plane */
    414 		ndac->bt_ctrl = 0x00; /* no test mode */
    415 		ndac->bt_addr = 0;
    416 		ndac->bt_cmap = 0;
    417 		ndac->bt_cmap = 0;
    418 		ndac->bt_cmap = 0;
    419 		for (i = 1; i < 256; i++) {
    420 			ndac->bt_cmap = 255;
    421 			ndac->bt_cmap = 255;
    422 			ndac->bt_cmap = 255;
    423 		}
    424 	}
    425 #endif
    426 
    427 	/* adjust h/v orgin on screen */
    428 	rfcnt.p.h = 7;
    429 	rfcnt.p.v = -27;
    430 	*(u_int32_t *)OMFB_RFCNT = rfcnt.u; /* single write of 0x007ffe6 */
    431 
    432 	/* clear the screen */
    433 	*(u_int32_t *)OMFB_PLANEMASK = 0xff;
    434 	((u_int32_t *)OMFB_ROPFUNC)[5] = ~0;	/* ROP copy */
    435 	for (i = 0; i < dc->dc_ht * dc->dc_rowbytes/sizeof(u_int32_t); i++)
    436 		*((u_int32_t *)dc->dc_videobase + i) = 0;
    437 	*(u_int32_t *)OMFB_PLANEMASK = 0x01;
    438 
    439 	/* initialize the raster */
    440 	rap = &dc->dc_raster;
    441 	rap->width = dc->dc_wid;
    442 	rap->height = dc->dc_ht;
    443 	rap->depth = dc->dc_depth;
    444 	rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
    445 	rap->pixels = (u_int32_t *)dc->dc_videobase;
    446 
    447 	/* initialize the raster console blitter */
    448 	rcp = &dc->dc_rcons;
    449 	rcp->rc_sp = rap;
    450 	rcp->rc_crow = rcp->rc_ccol = -1;
    451 	rcp->rc_crowp = &rcp->rc_crow;
    452 	rcp->rc_ccolp = &rcp->rc_ccol;
    453 	rcons_init(rcp, 34, 80);
    454 
    455 	omfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
    456 	omfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
    457 }
    458 
    459 static int
    460 omfb_alloc_screen(v, type, cookiep, curxp, curyp, attrp)
    461 	void *v;
    462 	const struct wsscreen_descr *type;
    463 	void **cookiep;
    464 	int *curxp, *curyp;
    465 	long *attrp;
    466 {
    467 	struct omfb_softc *sc = v;
    468 	long defattr;
    469 
    470 	if (sc->nscreens > 0)
    471 		return (ENOMEM);
    472 
    473 	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
    474 	*curxp = 0;
    475 	*curyp = 0;
    476 	(*omfb_emulops.allocattr)(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
    477 	*attrp = defattr;
    478 	sc->nscreens++;
    479 	return (0);
    480 }
    481 
    482 static void
    483 omfb_free_screen(v, cookie)
    484 	void *v;
    485 	void *cookie;
    486 {
    487 	struct omfb_softc *sc = v;
    488 
    489 	if (sc->sc_dc == &omfb_console_dc)
    490 		panic("omfb_free_screen: console");
    491 
    492 	sc->nscreens--;
    493 }
    494 
    495 static int
    496 omfb_show_screen(v, cookie, waitok, cb, cbarg)
    497 	void *v;
    498 	void *cookie;
    499 	int waitok;
    500 	void (*cb) __P((void *, int, int));
    501 	void *cbarg;
    502 {
    503 	return 0;
    504 }
    505