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