Home | History | Annotate | Line # | Download | only in dev
macfb.c revision 1.8
      1 /* $NetBSD: macfb.c,v 1.8 2002/09/27 20:33:14 thorpej 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 #include "grf.h"
     34 
     35 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/device.h>
     41 #include <sys/malloc.h>
     42 
     43 #include <machine/cpu.h>
     44 #include <machine/bus.h>
     45 
     46 #include <machine/grfioctl.h>
     47 #include <mac68k/nubus/nubus.h>
     48 #include <mac68k/dev/grfvar.h>
     49 #include <mac68k/dev/macfbvar.h>
     50 #include <dev/wscons/wsconsio.h>
     51 
     52 #include <dev/rcons/raster.h>
     53 #include <dev/wscons/wscons_raster.h>
     54 #include <dev/wscons/wsdisplayvar.h>
     55 
     56 int macfb_match __P((struct device *, struct cfdata *, void *));
     57 void macfb_attach __P((struct device *, struct device *, void *));
     58 
     59 const struct cfattach macfb_ca = {
     60 	sizeof(struct macfb_softc),
     61 	macfb_match,
     62 	macfb_attach,
     63 };
     64 
     65 const struct wsdisplay_emulops macfb_emulops = {
     66 	rcons_cursor,
     67 	rcons_mapchar,
     68 	rcons_putchar,
     69 	rcons_copycols,
     70 	rcons_erasecols,
     71 	rcons_copyrows,
     72 	rcons_eraserows,
     73 	rcons_allocattr
     74 };
     75 
     76 struct wsscreen_descr macfb_stdscreen = {
     77 	"std",
     78 	0, 0, /* will be filled in -- XXX shouldn't, it's global */
     79 	&macfb_emulops,
     80 	0, 0,
     81 	WSSCREEN_REVERSE
     82 };
     83 
     84 const struct wsscreen_descr *_macfb_scrlist[] = {
     85 	&macfb_stdscreen,
     86 };
     87 
     88 const struct wsscreen_list macfb_screenlist = {
     89 	sizeof(_macfb_scrlist) / sizeof(struct wsscreen_descr *),
     90 	_macfb_scrlist
     91 };
     92 
     93 static int	macfb_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
     94 static paddr_t	macfb_mmap __P((void *, off_t, int));
     95 static int	macfb_alloc_screen __P((void *, const struct wsscreen_descr *,
     96 		    void **, int *, int *, long *));
     97 static void	macfb_free_screen __P((void *, void *));
     98 static int	macfb_show_screen __P((void *, void *, int,
     99 		    void (*)(void *, int, int), void *));
    100 
    101 const struct wsdisplay_accessops macfb_accessops = {
    102 	macfb_ioctl,
    103 	macfb_mmap,
    104 	macfb_alloc_screen,
    105 	macfb_free_screen,
    106 	macfb_show_screen,
    107 	0 /* load_font */
    108 };
    109 
    110 void macfb_init __P((struct macfb_devconfig *));
    111 
    112 paddr_t macfb_consaddr;
    113 static int macfb_is_console __P((paddr_t addr));
    114 #ifdef WSDISPLAY_COMPAT_ITEFONT
    115 static void	init_itefont __P((void));
    116 #endif /* WSDISPLAY_COMPAT_ITEFONT */
    117 
    118 static struct macfb_devconfig macfb_console_dc;
    119 
    120 /* From Booter via locore */
    121 extern long		videoaddr;
    122 extern long		videorowbytes;
    123 extern long		videobitdepth;
    124 extern u_long		videosize;
    125 extern u_int32_t	mac68k_vidlog;
    126 extern u_int32_t	mac68k_vidphys;
    127 extern u_int32_t	mac68k_vidlen;
    128 
    129 static int
    130 macfb_is_console(paddr_t addr)
    131 {
    132 	if (addr != macfb_consaddr &&
    133 	    (addr >= 0xf9000000 && addr <= 0xfeffffff)) {
    134 		/*
    135 		 * This is in the NuBus standard slot space range, so we
    136 		 * may well have to look at 0xFssxxxxx, too.  Mask off the
    137 		 * slot number and duplicate it in bits 20-23, per IM:V
    138 		 * pp 459, 463, and IM:VI ch 30 p 17.
    139 		 * Note:  this is an ugly hack and I wish I knew what
    140 		 * to do about it.  -- sr
    141 		 */
    142 		addr = (paddr_t)(((u_long)addr & 0xff0fffff) |
    143 		    (((u_long)addr & 0x0f000000) >> 4));
    144 	}
    145 	return ((mac68k_machine.serial_console & 0x03) == 0
    146 	    && (addr == macfb_consaddr));
    147 }
    148 
    149 void
    150 macfb_clear(dc)
    151 	struct macfb_devconfig *dc;
    152 {
    153 	int i, rows;
    154 
    155 	/* clear the display */
    156 	rows = dc->dc_ht;
    157 	for (i = 0; rows-- > 0; i += dc->dc_rowbytes)
    158 		memset((u_char *)dc->dc_vaddr + dc->dc_offset + i,
    159 		    0, dc->dc_rowbytes);
    160 }
    161 
    162 void
    163 macfb_init(dc)
    164 	struct macfb_devconfig *dc;
    165 {
    166 	struct raster *rap;
    167 	struct rcons *rcp;
    168 
    169 	macfb_clear(dc);
    170 
    171 #ifdef WSDISPLAY_COMPAT_ITEFONT
    172 	init_itefont();
    173 #endif /* WSDISPLAY_COMPAT_ITEFONT */
    174 
    175 	rap = &dc->dc_raster;
    176 	rap->width = dc->dc_wid;
    177 	rap->height = dc->dc_ht;
    178 	rap->depth = dc->dc_depth;
    179 	rap->linelongs = dc->dc_rowbytes / sizeof(u_int32_t);
    180 	rap->pixels = (u_int32_t *)(dc->dc_vaddr + dc->dc_offset);
    181 
    182 	/* initialize the raster console blitter */
    183 	rcp = &dc->dc_rcons;
    184 	rcp->rc_sp = rap;
    185 	rcp->rc_crow = rcp->rc_ccol = -1;
    186 	rcp->rc_crowp = &rcp->rc_crow;
    187 	rcp->rc_ccolp = &rcp->rc_ccol;
    188 	rcons_init(rcp, 128, 192);
    189 
    190 	macfb_stdscreen.nrows = dc->dc_rcons.rc_maxrow;
    191 	macfb_stdscreen.ncols = dc->dc_rcons.rc_maxcol;
    192 }
    193 
    194 int
    195 macfb_match(parent, match, aux)
    196 	struct device *parent;
    197 	struct cfdata *match;
    198 	void *aux;
    199 {
    200 	return (1);
    201 }
    202 
    203 void
    204 macfb_attach(parent, self, aux)
    205 	struct device *parent;
    206 	struct device *self;
    207 	void *aux;
    208 {
    209 	struct grfbus_attach_args *ga = aux;
    210 	struct grfmode *gm = ga->ga_grfmode;
    211 	struct macfb_softc *sc;
    212 	struct wsemuldisplaydev_attach_args waa;
    213 	int isconsole;
    214 
    215 	sc = (struct macfb_softc *)self;
    216 
    217 	printf("\n");
    218 
    219 	isconsole = macfb_is_console(ga->ga_phys + ga->ga_grfmode->fboff);
    220 
    221 	if (isconsole) {
    222 		sc->sc_dc = &macfb_console_dc;
    223 		sc->nscreens = 1;
    224 	} else {
    225 		sc->sc_dc = malloc(sizeof(struct macfb_devconfig), M_DEVBUF, M_WAITOK);
    226 		sc->sc_dc->dc_vaddr = (vaddr_t)gm->fbbase;
    227 		sc->sc_dc->dc_paddr = ga->ga_phys;
    228 		sc->sc_dc->dc_size = gm->fbsize;
    229 
    230 		sc->sc_dc->dc_wid = gm->width;
    231 		sc->sc_dc->dc_ht = gm->height;
    232 		sc->sc_dc->dc_depth = gm->psize;
    233 		sc->sc_dc->dc_rowbytes = gm->rowbytes;
    234 
    235 		sc->sc_dc->dc_offset = gm->fboff;
    236 
    237 		macfb_clear(sc->sc_dc);
    238 
    239 		sc->nscreens = 1;
    240 	}
    241 
    242 	/* initialize the raster */
    243 	waa.console = isconsole;
    244 	waa.scrdata = &macfb_screenlist;
    245 	waa.accessops = &macfb_accessops;
    246 	waa.accesscookie = sc;
    247 
    248 	config_found(self, &waa, wsemuldisplaydevprint);
    249 
    250 #if NGRF > 0
    251 	grf_attach(sc, self->dv_unit);
    252 #endif
    253 }
    254 
    255 
    256 int
    257 macfb_ioctl(v, cmd, data, flag, p)
    258 	void *v;
    259 	u_long cmd;
    260 	caddr_t data;
    261 	int flag;
    262 	struct proc *p;
    263 {
    264 	struct macfb_softc *sc = v;
    265 	struct macfb_devconfig *dc = sc->sc_dc;
    266 	struct wsdisplay_fbinfo *wdf;
    267 
    268 	switch (cmd) {
    269 	case WSDISPLAYIO_GTYPE:
    270 		*(int *)data = dc->dc_type;
    271 		return 0;
    272 
    273 	case WSDISPLAYIO_GINFO:
    274 		wdf = (struct wsdisplay_fbinfo *)data;
    275 		wdf->height = dc->dc_raster.height;
    276 		wdf->width = dc->dc_raster.width;
    277 		wdf->depth = dc->dc_raster.depth;
    278 		wdf->cmsize = 256;
    279 		return 0;
    280 
    281 	case WSDISPLAYIO_GCURMAX:
    282 	case WSDISPLAYIO_GCURPOS:
    283 	case WSDISPLAYIO_GCURSOR:
    284 	case WSDISPLAYIO_GETCMAP:
    285 	case WSDISPLAYIO_GVIDEO:
    286 	case WSDISPLAYIO_PUTCMAP:
    287 	case WSDISPLAYIO_SCURPOS:
    288 	case WSDISPLAYIO_SCURSOR:
    289 	case WSDISPLAYIO_SVIDEO:
    290 		/* NONE of these operations are supported. */
    291 		return EPASSTHROUGH;
    292 	}
    293 
    294 	return EPASSTHROUGH;
    295 }
    296 
    297 static paddr_t
    298 macfb_mmap(v, offset, prot)
    299 	void *v;
    300 	off_t offset;
    301 	int prot;
    302 {
    303 	struct macfb_softc *sc = v;
    304 	struct macfb_devconfig *dc = sc->sc_dc;
    305 	paddr_t addr;
    306 
    307 	if (offset >= 0 &&
    308 	    offset < m68k_round_page(dc->dc_rowbytes * dc->dc_ht))
    309 		addr = m68k_btop(dc->dc_paddr + dc->dc_offset + offset);
    310 	else
    311 		addr = (-1);	/* XXX bogus */
    312 
    313 	return addr;
    314 }
    315 
    316 int
    317 macfb_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
    318 	void *v;
    319 	const struct wsscreen_descr *type;
    320 	void **cookiep;
    321 	int *curxp, *curyp;
    322 	long *defattrp;
    323 {
    324 	struct macfb_softc *sc = v;
    325 	long defattr;
    326 
    327 	if (sc->nscreens > 0)
    328 		return (ENOMEM);
    329 
    330 	*cookiep = &sc->sc_dc->dc_rcons; /* one and only for now */
    331 	*curxp = 0;
    332 	*curyp = 0;
    333 	rcons_allocattr(&sc->sc_dc->dc_rcons, 0, 0, 0, &defattr);
    334 	*defattrp = defattr;
    335 	sc->nscreens++;
    336 	return (0);
    337 }
    338 
    339 void
    340 macfb_free_screen(v, cookie)
    341 	void *v;
    342 	void *cookie;
    343 {
    344 	struct macfb_softc *sc = v;
    345 
    346 	if (sc->sc_dc == &macfb_console_dc)
    347 		panic("cfb_free_screen: console");
    348 
    349 	sc->nscreens--;
    350 }
    351 
    352 int
    353 macfb_show_screen(v, cookie, waitok, cb, cbarg)
    354 	void *v;
    355 	void *cookie;
    356 	int waitok;
    357 	void (*cb) __P((void *, int, int));
    358 	void *cbarg;
    359 {
    360 	return 0;
    361 }
    362 
    363 int
    364 macfb_cnattach(addr)
    365 	paddr_t addr;
    366 {
    367 	struct macfb_devconfig *dc = &macfb_console_dc;
    368 	long defattr;
    369 
    370 	dc->dc_vaddr = m68k_trunc_page(videoaddr);
    371 	dc->dc_paddr = m68k_trunc_page(mac68k_vidphys);
    372 
    373 	dc->dc_wid = videosize & 0xffff;
    374 	dc->dc_ht = (videosize >> 16) & 0xffff;
    375 	dc->dc_depth = videobitdepth;
    376 	dc->dc_rowbytes = videorowbytes;
    377 
    378 	dc->dc_size = (mac68k_vidlen > 0) ?
    379 	    mac68k_vidlen : dc->dc_ht * dc->dc_rowbytes;
    380 	dc->dc_offset = m68k_page_offset(mac68k_vidphys);
    381 
    382 	/* set up the display */
    383 	macfb_init(&macfb_console_dc);
    384 
    385 	rcons_allocattr(&dc->dc_rcons, 0, 0, 0, &defattr);
    386 
    387 	wsdisplay_cnattach(&macfb_stdscreen, &dc->dc_rcons,
    388 			0, 0, defattr);
    389 
    390 	macfb_consaddr = addr;
    391 	dc->isconsole = 1;
    392 	return (0);
    393 }
    394 
    395 #ifdef WSDISPLAY_COMPAT_ITEFONT
    396 #include <mac68k/dev/6x10.h>
    397 
    398 void
    399 init_itefont()
    400 {
    401 	static int itefont_initted = 0;
    402 	int i, j;
    403 
    404 	extern struct raster_font gallant19;		/* XXX */
    405 
    406 	if (itefont_initted)
    407 		return;
    408 	itefont_initted = 1;
    409 
    410 	/* XXX but we cannot use malloc here... */
    411 	gallant19.width = 6;
    412 	gallant19.height = 10;
    413 	gallant19.ascent = 0;
    414 
    415 	for (i = 32; i < 128; i++) {
    416 		u_int *p;
    417 
    418 		if (gallant19.chars[i].r == NULL)
    419 			continue;
    420 
    421 		gallant19.chars[i].r->width = 6;
    422 		gallant19.chars[i].r->height = 10;
    423 		p = gallant19.chars[i].r->pixels;
    424 
    425 		for (j = 0; j < 10; j++)
    426 			*p++ = Font6x10[i * 10 + j] << 26;
    427 	}
    428 }
    429 #endif /* WSDISPLAY_COMPAT_ITEFONT */
    430