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