Home | History | Annotate | Line # | Download | only in dev
macfb.c revision 1.1.2.4
      1 /* $NetBSD: macfb.c,v 1.1.2.4 1999/11/02 06:49:58 scottr Exp $ */
      2 /*
      3  * Copyright (c) 1998 Matt DeBergalis
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed by Matt DeBergalis
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include "opt_wsdisplay_compat.h"
     33 
     34 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/malloc.h>
     41 #ifdef WSDISPLAY_COMPAT_GRF
     42 #include <sys/device.h>
     43 #include <sys/mman.h>
     44 #include <sys/proc.h>
     45 #include <sys/resourcevar.h>
     46 #include <sys/vnode.h>
     47 #endif /* WSDISPLAY_COMPAT_GRF */
     48 
     49 #include <machine/cpu.h>
     50 #include <machine/bus.h>
     51 
     52 #ifdef WSDISPLAY_COMPAT_GRF
     53 #include <miscfs/specfs/specdev.h>
     54 
     55 #include <vm/vm.h>
     56 #include <uvm/uvm_extern.h>
     57 #include <uvm/uvm_map.h>
     58 #endif /* WSDISPLAY_COMPAT_GRF */
     59 
     60 #include <machine/grfioctl.h>
     61 #include <mac68k/nubus/nubus.h>
     62 #include <mac68k/dev/grfvar.h>
     63 #include <mac68k/dev/macfbvar.h>
     64 #include <dev/wscons/wsconsio.h>
     65 
     66 #ifdef WSDISPLAY_COMPAT_ITE
     67 #include <machine/iteioctl.h>
     68 #endif /* WSDISPLAY_COMPAT_ITE */
     69 
     70 #include <dev/rcons/raster.h>
     71 #include <dev/wscons/wscons_raster.h>
     72 #include <dev/wscons/wsdisplayvar.h>
     73 
     74 int macfb_match __P((struct device *, struct cfdata *, void *));
     75 void macfb_attach __P((struct device *, struct device *, void *));
     76 
     77 struct cfattach macfb_ca = {
     78 	sizeof(struct macfb_softc),
     79 	macfb_match,
     80 	macfb_attach,
     81 };
     82 
     83 const struct wsdisplay_emulops macfb_emulops = {
     84 	rcons_cursor,
     85 	rcons_mapchar,
     86 	rcons_putchar,
     87 	rcons_copycols,
     88 	rcons_erasecols,
     89 	rcons_copyrows,
     90 	rcons_eraserows,
     91 	rcons_alloc_attr
     92 };
     93 
     94 struct wsscreen_descr macfb_stdscreen = {
     95 	"std",
     96 	0, 0, /* will be filled in -- XXX shouldn't, it's global */
     97 	&macfb_emulops,
     98 	0, 0,
     99 	WSSCREEN_REVERSE
    100 };
    101 
    102 const struct wsscreen_descr *_macfb_scrlist[] = {
    103 	&macfb_stdscreen,
    104 };
    105 
    106 const struct wsscreen_list macfb_screenlist = {
    107 	sizeof(_macfb_scrlist) / sizeof(struct wsscreen_descr *),
    108 	_macfb_scrlist
    109 };
    110 
    111 static int	macfb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
    112 static int	macfb_mmap __P((void *, off_t, int));
    113 static int	macfb_alloc_screen __P((void *, const struct wsscreen_descr *,
    114 		void **, int *, int *, long *));
    115 static void	macfb_free_screen __P((void *, void *));
    116 static void	macfb_show_screen __P((void *, void *));
    117 
    118 const struct wsdisplay_accessops macfb_accessops = {
    119 	macfb_ioctl,
    120 	macfb_mmap,
    121 	macfb_alloc_screen,
    122 	macfb_free_screen,
    123 	macfb_show_screen,
    124 	0 /* load_font */
    125 };
    126 
    127 void macfb_init __P((struct macfb_devconfig *));
    128 
    129 paddr_t macfb_consaddr;
    130 static int macfb_is_console __P((paddr_t addr));
    131 #ifdef WSDISPLAY_COMPAT_ITEFONT
    132 static void	init_itefont __P((void));
    133 #endif /* WSDISPLAY_COMPAT_ITEFONT */
    134 #ifdef WSDISPLAY_COMPAT_GRF
    135 int	macfb_grfmap __P((void *, caddr_t *, struct proc *));
    136 int	macfb_grfunmap __P((void *, caddr_t, struct proc *));
    137 #endif /* WSDISPLAY_COMPAT_GRF */
    138 
    139 static struct macfb_devconfig macfb_console_dc;
    140 
    141 /* From Booter via locore */
    142 extern long		videoaddr;
    143 extern long		videorowbytes;
    144 extern long		videobitdepth;
    145 extern u_long		videosize;
    146 extern u_int32_t	mac68k_vidlog;
    147 extern u_int32_t	mac68k_vidphys;
    148 extern u_int32_t	mac68k_vidlen;
    149 
    150 static int
    151 macfb_is_console(paddr_t addr)
    152 {
    153 	return ((mac68k_machine.serial_console & 0x03) == 0
    154 	    && (addr == macfb_consaddr));
    155 }
    156 
    157 void
    158 macfb_init(dc)
    159 	struct macfb_devconfig *dc;
    160 {
    161 	struct raster *rap;
    162 	struct rcons *rcp;
    163 	int i;
    164 
    165 	/* clear the display */
    166 	for (i = 0; i < (dc->dc_ht * dc->dc_rowbytes); i += dc->dc_rowbytes)
    167 		bzero((u_char *)dc->dc_vaddr + dc->dc_offset + i,
    168 		    dc->dc_rowbytes);
    169 
    170 #ifdef WSDISPLAY_COMPAT_ITEFONT
    171 	init_itefont();
    172 #endif /* WSDISPLAY_COMPAT_ITEFONT */
    173 
    174 	rap = &dc->dc_raster;
    175 	rap->width = dc->dc_wid;
    176 	rap->height = dc->dc_ht;
    177 	rap->depth = dc->dc_depth;
    178 	rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
    179 	rap->pixels = (u_int32_t *)dc->dc_vaddr + dc->dc_offset;
    180 
    181 	/* initialize the raster console blitter */
    182 	rcp = &dc->dc_rcons;
    183 	rcp->rc_sp = rap;
    184 	rcp->rc_crow = rcp->rc_ccol = -1;
    185 	rcp->rc_crowp = &rcp->rc_crow;
    186 	rcp->rc_ccolp = &rcp->rc_ccol;
    187 	rcons_init(rcp, 128, 128);
    188 
    189 	macfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
    190 	macfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
    191 }
    192 
    193 int
    194 macfb_match(parent, match, aux)
    195 	struct device *parent;
    196 	struct cfdata *match;
    197 	void *aux;
    198 {
    199 	return (1);
    200 }
    201 
    202 void
    203 macfb_attach(parent, self, aux)
    204 	struct device *parent;
    205 	struct device *self;
    206 	void *aux;
    207 {
    208 	struct grfbus_attach_args *ga = aux;
    209 	struct grfmode *gm = ga->ga_grfmode;
    210 	struct macfb_softc *sc;
    211 	struct wsemuldisplaydev_attach_args waa;
    212 	int isconsole;
    213 
    214 	sc = (struct macfb_softc *)self;
    215 
    216 	printf("\n");
    217 
    218 	isconsole = macfb_is_console(ga->ga_phys);
    219 
    220 	if (isconsole) {
    221 		sc->sc_dc = &macfb_console_dc;
    222 		sc->nscreens = 1;
    223 	} else {
    224 		sc->sc_dc = malloc(sizeof(struct macfb_devconfig), M_DEVBUF, M_WAITOK);
    225 		sc->sc_dc->dc_vaddr = (vaddr_t)gm->fbbase;
    226 		sc->sc_dc->dc_paddr = ga->ga_phys;
    227 		sc->sc_dc->dc_size = gm->fbsize;
    228 
    229 		sc->sc_dc->dc_wid = gm->width;
    230 		sc->sc_dc->dc_ht = gm->height;
    231 		sc->sc_dc->dc_depth = gm->psize;
    232 		sc->sc_dc->dc_rowbytes = gm->rowbytes;
    233 
    234 		sc->sc_dc->dc_offset = gm->fboff;
    235 
    236 		macfb_init(sc->sc_dc);
    237 
    238 		sc->nscreens = 1;
    239 	}
    240 
    241 	/* initialize the raster */
    242 	waa.console = isconsole;
    243 	waa.scrdata = &macfb_screenlist;
    244 	waa.accessops = &macfb_accessops;
    245 	waa.accesscookie = sc;
    246 
    247 	config_found(self, &waa, wsemuldisplaydevprint);
    248 }
    249 
    250 
    251 int
    252 macfb_ioctl(v, cmd, data, flag, p)
    253 	void *v;
    254 	u_long cmd;
    255 	caddr_t data;
    256 	int flag;
    257 	struct proc *p;
    258 {
    259 	struct macfb_softc *sc = v;
    260 	struct macfb_devconfig *dc = sc->sc_dc;
    261 	struct wsdisplay_fbinfo *wdf;
    262 #ifdef WSDISPLAY_COMPAT_GRF
    263 	struct grfinfo *gd;
    264 	struct grfmode *gm;
    265 #endif /* WSDISPLAY_COMPAT_GRF */
    266 
    267 	switch (cmd) {
    268 	case WSDISPLAYIO_GTYPE:
    269 		*(int *)data = dc->dc_type;
    270 		return 0;
    271 
    272 	case WSDISPLAYIO_GINFO:
    273 		wdf = (struct wsdisplay_fbinfo *)data;
    274 		wdf->height = dc->dc_raster.height;
    275 		wdf->width = dc->dc_raster.width;
    276 		wdf->depth = dc->dc_raster.depth;
    277 		wdf->cmsize = 256;
    278 		return 0;
    279 
    280 	case WSDISPLAYIO_GCURMAX:
    281 	case WSDISPLAYIO_GCURPOS:
    282 	case WSDISPLAYIO_GCURSOR:
    283 	case WSDISPLAYIO_GETCMAP:
    284 	case WSDISPLAYIO_GVIDEO:
    285 	case WSDISPLAYIO_PUTCMAP:
    286 	case WSDISPLAYIO_SCURPOS:
    287 	case WSDISPLAYIO_SCURSOR:
    288 	case WSDISPLAYIO_SVIDEO:
    289 		/* NONE of these operations are supported. */
    290 		return ENOTTY;
    291 
    292 #ifdef WSDISPLAY_COMPAT_ITE
    293 	case ITEIOC_GETBELL:
    294 	case ITEIOC_SETBELL:
    295 	case ITEIOC_RINGBELL:
    296 		/* NONE of these operations are supported. */
    297 		return ENOTTY;
    298 
    299 #endif /* WSDISPLAY_COMPAT_ITE */
    300 #ifdef WSDISPLAY_COMPAT_GRF
    301 	case GRFIOCGINFO:
    302 		gd = (struct grfinfo *)data;
    303 		bzero(gd, sizeof(struct grfinfo));
    304 		gd->gd_fbaddr     = (caddr_t)dc->dc_paddr;
    305 		gd->gd_fbsize     = dc->dc_size;
    306 		gd->gd_colors     = (short)(1 << dc->dc_depth);
    307 		gd->gd_planes     = (short)dc->dc_depth;
    308 		gd->gd_fbwidth    = dc->dc_wid;
    309 		gd->gd_fbheight   = dc->dc_ht;
    310 		gd->gd_fbrowbytes = dc->dc_rowbytes;
    311 		gd->gd_dwidth     = dc->dc_raster.width;
    312 		gd->gd_dheight    = dc->dc_raster.height;
    313 		return 0;
    314 
    315 	case GRFIOCON:
    316 	case GRFIOCOFF:
    317 		/* Nothing to do */
    318 		return 0;
    319 
    320 	case GRFIOCMAP:
    321 		return macfb_grfmap(v, (caddr_t *)data, p);
    322 
    323 	case GRFIOCUNMAP:
    324 		return macfb_grfunmap(v, *(caddr_t *)data, p);
    325 
    326 	case GRFIOCGMODE:
    327 		gm = (struct grfmode *)data;
    328 		bzero(gm, sizeof(struct grfmode));
    329 		gm->fbbase   = (char *)dc->dc_vaddr;
    330 		gm->fbsize   = dc->dc_size;
    331 		gm->fboff    = dc->dc_offset;
    332 		gm->rowbytes = dc->dc_rowbytes;
    333 		gm->width    = dc->dc_wid;
    334 		gm->height   = dc->dc_ht;
    335 		gm->psize    = dc->dc_depth;
    336 		return 0;
    337 
    338 	case GRFIOCLISTMODES:
    339 	case GRFIOCGETMODE:
    340 	case GRFIOCSETMODE:
    341 		/* NONE of these operations are (or ever were) supported. */
    342 		return ENOTTY;
    343 #endif /* WSDISPLAY_COMPAT_GRF */
    344 	}
    345 
    346 	return -1;
    347 }
    348 
    349 static int
    350 macfb_mmap(v, offset, prot)
    351 	void *v;
    352 	off_t offset;
    353 	int prot;
    354 {
    355 	struct macfb_softc *sc = v;
    356 	struct macfb_devconfig *dc = sc->sc_dc;
    357 	u_long addr;
    358 
    359 	if (offset >= 0 &&
    360 	    offset < m68k_round_page(dc->dc_rowbytes * dc->dc_ht))
    361 		addr = m68k_btop(dc->dc_paddr + dc->dc_offset + offset);
    362 	else
    363 		addr = (-1);	/* XXX bogus */
    364 
    365 	return (int)addr;
    366 }
    367 
    368 int
    369 macfb_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
    370 	void *v;
    371 	const struct wsscreen_descr *type;
    372 	void **cookiep;
    373 	int *curxp, *curyp;
    374 	long *defattrp;
    375 {
    376 	struct macfb_softc *sc = v;
    377 	long defattr;
    378 
    379 	if (sc->nscreens > 0)
    380 		return (ENOMEM);
    381 
    382 	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
    383 	*curxp = 0;
    384 	*curyp = 0;
    385 	rcons_alloc_attr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
    386 	*defattrp = defattr;
    387 	sc->nscreens++;
    388 	return (0);
    389 }
    390 
    391 void
    392 macfb_free_screen(v, cookie)
    393 	void *v;
    394 	void *cookie;
    395 {
    396 	struct macfb_softc *sc = v;
    397 
    398 	if (sc->sc_dc == &macfb_console_dc)
    399 		panic("cfb_free_screen: console");
    400 
    401 	sc->nscreens--;
    402 }
    403 
    404 void
    405 macfb_show_screen(v, cookie)
    406 	void *v;
    407 	void *cookie;
    408 {
    409 }
    410 
    411 int
    412 macfb_cnattach(addr)
    413 	paddr_t addr;
    414 {
    415 	struct macfb_devconfig *dc = &macfb_console_dc;
    416 	long defattr;
    417 
    418 	dc->dc_vaddr = videoaddr;
    419 	dc->dc_paddr = mac68k_vidphys;
    420 
    421 	dc->dc_wid = videosize & 0xffff;
    422 	dc->dc_ht = (videosize >> 16) & 0xffff;
    423 	dc->dc_depth = videobitdepth;
    424 	dc->dc_rowbytes = videorowbytes;
    425 
    426 	dc->dc_size = (mac68k_vidlen > 0) ?
    427 	    mac68k_vidlen : dc->dc_ht * dc->dc_rowbytes;
    428 	dc->dc_offset = 0;
    429 
    430 	/* set up the display */
    431 	macfb_init(&macfb_console_dc);
    432 
    433 	rcons_alloc_attr(&dc->dc_rcons, 0, 0, 0, &defattr);
    434 
    435 	wsdisplay_cnattach(&macfb_stdscreen, &dc->dc_rcons,
    436 			0, 0, defattr);
    437 
    438 	macfb_consaddr = addr;
    439 	dc->isconsole = 1;
    440 	return (0);
    441 }
    442 
    443 #ifdef WSDISPLAY_COMPAT_ITEFONT
    444 #include <mac68k/dev/6x10.h>
    445 
    446 void
    447 init_itefont()
    448 {
    449 	static int itefont_initted = 0;
    450 	int i, j;
    451 
    452 	extern struct raster_font gallant19;		/* XXX */
    453 
    454 	if (itefont_initted)
    455 		return;
    456 	itefont_initted = 1;
    457 
    458 	/* XXX but we cannot use malloc here... */
    459 	gallant19.width = 6;
    460 	gallant19.height = 10;
    461 	gallant19.ascent = 0;
    462 
    463 	for (i = 32; i < 128; i++) {
    464 		u_int *p;
    465 
    466 		if (gallant19.chars[i].r == NULL)
    467 			continue;
    468 
    469 		gallant19.chars[i].r->width = 6;
    470 		gallant19.chars[i].r->height = 10;
    471 		p = gallant19.chars[i].r->pixels;
    472 
    473 		for (j = 0; j < 10; j++)
    474 			*p++ = Font6x10[i * 10 + j] << 26;
    475 	}
    476 }
    477 #endif /* WSDISPLAY_COMPAT_ITEFONT */
    478 
    479 #ifdef WSDISPLAY_COMPAT_GRF
    480 int
    481 macfb_grfmap(v, addrp, p)
    482 	void *v;
    483 	caddr_t *addrp;
    484 	struct proc *p;
    485 {
    486 	struct macfb_softc *sc = v;
    487 	struct specinfo si;
    488 	struct vnode vn;
    489 	u_long len;
    490 	int error, flags;
    491 
    492 	*addrp = (caddr_t)sc->sc_dc->dc_paddr;
    493 	len = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
    494 	flags = MAP_SHARED | MAP_FIXED;
    495 
    496 	vn.v_type = VCHR;				/* XXX */
    497 	vn.v_specinfo = &si;				/* XXX */
    498 	vn.v_rdev = makedev(44,sc->sc_dev.dv_unit);	/* XXX */
    499 
    500 	error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
    501 	    (vsize_t)len, VM_PROT_ALL, VM_PROT_ALL,
    502 	    flags, (caddr_t)&vn, 0, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
    503 
    504 	/* Offset into page: */
    505 	*addrp += sc->sc_dc->dc_offset;
    506 
    507 	return (error);
    508 }
    509 
    510 int
    511 macfb_grfunmap(v, addr, p)
    512 	void *v;
    513 	caddr_t addr;
    514 	struct proc *p;
    515 {
    516 	struct macfb_softc *sc = v;
    517 	vm_size_t size;
    518 	int     rv;
    519 
    520 	addr -= sc->sc_dc->dc_offset;
    521 
    522 	if (addr <= 0)
    523 		return (-1);
    524 
    525 	size = m68k_round_page(sc->sc_dc->dc_offset + sc->sc_dc->dc_size);
    526 
    527 	rv = uvm_unmap(&p->p_vmspace->vm_map, (vaddr_t)addr,
    528 	    (vaddr_t)addr + size);
    529 
    530 	return (rv == KERN_SUCCESS ? 0 : EINVAL);
    531 }
    532 #endif /* WSDISPLAY_COMPAT_GRF */
    533