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