Home | History | Annotate | Line # | Download | only in dev
fb_elb.c revision 1.3
      1 /*	$NetBSD: fb_elb.c,v 1.3 2003/07/15 01:37:37 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Juergen Hannken-Illjes.
      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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *      This product includes software developed by the NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: fb_elb.c,v 1.3 2003/07/15 01:37:37 lukem Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/conf.h>
     44 #include <sys/device.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/malloc.h>
     47 #include <sys/systm.h>
     48 
     49 #include <dev/wscons/wsconsio.h>
     50 #include <dev/wscons/wsdisplayvar.h>
     51 #include <dev/rasops/rasops.h>
     52 
     53 #include <machine/explora.h>
     54 #include <machine/bus.h>
     55 
     56 #include <evbppc/explora/dev/elbvar.h>
     57 
     58 #define FB_NPORTS		65536
     59 
     60 struct fb_dev {
     61 	void *fb_vram;
     62 	bus_space_tag_t fb_iot;
     63 	bus_space_handle_t fb_ioh;
     64 	struct rasops_info fb_ri;
     65 };
     66 
     67 struct fb_elb_softc {
     68 	struct device sc_dev;
     69 	struct fb_dev *sc_fb;
     70 	int sc_nscreens;
     71 };
     72 
     73 static int	fb_elb_probe(struct device *, struct cfdata *, void *);
     74 static void	fb_elb_attach(struct device *, struct device *, void *);
     75 void		fb_cnattach(bus_space_tag_t, bus_addr_t, void *);
     76 static void	fb_init(struct fb_dev *, int);
     77 static int	fb_ioctl(void *, u_long, caddr_t, int, struct proc *);
     78 static paddr_t	fb_mmap(void *, off_t, int);
     79 static int	fb_alloc_screen(void *, const struct wsscreen_descr *, void **,
     80                     int *, int *, long *);
     81 static void	fb_free_screen(void *, void *);
     82 static int	fb_show_screen(void *, void *, int, void (*)(void *, int, int),
     83                     void *);
     84 
     85 static void	fb_eraserows(void *, int, int, long);
     86 static void	fb_erasecols(void *, int, int, int, long);
     87 static void	fb_copyrows(void *, int, int, int);
     88 static void	fb_copycols(void *, int, int, int, int);
     89 
     90 static void	s3_init(struct fb_dev *, int *, int *);
     91 static void	s3_copy(struct fb_dev *, int, int, int, int, int, int, int);
     92 static void	s3_fill(struct fb_dev *, int, int, int, int, int, int);
     93 
     94 static struct fb_dev console_dev;
     95 
     96 static struct wsdisplay_accessops accessops = {
     97 	fb_ioctl,
     98 	fb_mmap,
     99 	fb_alloc_screen,
    100 	fb_free_screen,
    101 	fb_show_screen,
    102 	NULL
    103 };
    104 
    105 static struct wsscreen_descr stdscreen = {
    106 	"std",
    107 	0, 0,
    108 	0,
    109 	0, 0,
    110 	0
    111 };
    112 
    113 static const struct wsscreen_descr *scrlist[] = {
    114 	&stdscreen
    115 };
    116 
    117 static struct wsscreen_list screenlist = {
    118 	sizeof(scrlist)/sizeof(scrlist[0]), scrlist
    119 };
    120 
    121 CFATTACH_DECL(fb_elb, sizeof(struct fb_elb_softc),
    122     fb_elb_probe, fb_elb_attach, NULL, NULL);
    123 
    124 static int
    125 fb_elb_probe(struct device *parent, struct cfdata *cf, void *aux)
    126 {
    127 	struct elb_attach_args *oaa = aux;
    128 
    129 	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
    130 		return 0;
    131 
    132 	return (1);
    133 }
    134 
    135 static void
    136 fb_elb_attach(struct device *parent, struct device *self, void *aux)
    137 {
    138 	struct fb_elb_softc *sc = (void *)self;
    139 	struct elb_attach_args *eaa = aux;
    140 	struct wsemuldisplaydev_attach_args waa;
    141 	struct rasops_info *ri;
    142 	bus_space_handle_t ioh;
    143 	int is_console;
    144 
    145 	is_console = ((void *)eaa->elb_base == console_dev.fb_vram);
    146 
    147 	if (is_console) {
    148 		sc->sc_fb = &console_dev;
    149 	} else {
    150 		sc->sc_fb = malloc(sizeof(struct fb_dev), M_DEVBUF, M_WAITOK);
    151 		memset(sc->sc_fb, 0, sizeof(struct fb_dev));
    152 	}
    153 
    154 	sc->sc_fb->fb_iot = eaa->elb_bt;
    155 	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base, SIZE_FB,
    156 	    BUS_SPACE_MAP_LINEAR, &ioh);
    157 	sc->sc_fb->fb_vram = bus_space_vaddr(sc->sc_fb->fb_iot, ioh);
    158 	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base2, FB_NPORTS,
    159 	    0, &sc->sc_fb->fb_ioh);
    160 
    161 	fb_init(sc->sc_fb, !is_console);
    162 
    163 	ri = &sc->sc_fb->fb_ri;
    164 
    165 	printf(": %d x %d\n", ri->ri_rows, ri->ri_cols);
    166 
    167 	waa.console = is_console;
    168 	waa.scrdata = &screenlist;
    169 	waa.accessops = &accessops;
    170 	waa.accesscookie = sc;
    171 
    172 	config_found_sm(self, &waa, wsemuldisplaydevprint, NULL);
    173 }
    174 
    175 static void
    176 fb_init(struct fb_dev *fb, int full)
    177 {
    178 	struct rasops_info *ri = &fb->fb_ri;
    179 
    180 	if (full) {
    181 		s3_init(fb, &ri->ri_width, &ri->ri_height);
    182 		ri->ri_depth = 8;
    183 		ri->ri_stride = ri->ri_width;
    184 		ri->ri_bits = fb->fb_vram;
    185 		ri->ri_flg = RI_CENTER;
    186 
    187 		rasops_init(ri, 500, 500);
    188 	} else {
    189 		ri->ri_origbits = fb->fb_vram;	/*XXX*/
    190 		rasops_reconfig(ri, 500, 500);
    191 	}
    192 
    193 	/* Replace the copy/erase ops. */
    194 	ri->ri_hw = fb;
    195 	ri->ri_ops.eraserows = fb_eraserows;
    196 	ri->ri_ops.erasecols = fb_erasecols;
    197 	ri->ri_ops.copyrows = fb_copyrows;
    198 	ri->ri_ops.copycols = fb_copycols;
    199 
    200 	stdscreen.nrows = ri->ri_rows;
    201 	stdscreen.ncols = ri->ri_cols;
    202 	stdscreen.textops = &ri->ri_ops;
    203 	stdscreen.capabilities = ri->ri_caps;
    204 }
    205 
    206 static int
    207 fb_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    208 {
    209 	struct fb_elb_softc *sc = v;
    210 	struct rasops_info *ri = &sc->sc_fb->fb_ri;
    211 	struct wsdisplay_fbinfo *wdf;
    212 
    213 	switch (cmd) {
    214 	case WSDISPLAYIO_GTYPE:
    215 		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
    216 		return(0);
    217 
    218 	case WSDISPLAYIO_GINFO:
    219 		wdf = (void *)data;
    220 		wdf->height = ri->ri_height;
    221 		wdf->width = ri->ri_width;
    222 		wdf->depth = ri->ri_depth;
    223 		wdf->cmsize = 16; /*XXX*/
    224 		return(0);
    225 
    226 	case WSDISPLAYIO_SVIDEO:
    227 	case WSDISPLAYIO_GETCMAP:
    228 	case WSDISPLAYIO_PUTCMAP:
    229 		break;
    230 	}
    231 
    232 	return(EPASSTHROUGH);
    233 }
    234 
    235 static paddr_t
    236 fb_mmap(void *v, off_t offset, int prot)
    237 {
    238 	return -1;
    239 }
    240 
    241 static int
    242 fb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc, void **cookiep,
    243     int *ccolp, int *crowp, long *attrp)
    244 {
    245 	struct fb_elb_softc *sc = v;
    246 	struct rasops_info *ri = &sc->sc_fb->fb_ri;
    247 
    248 	if (sc->sc_nscreens > 0)
    249 		return ENOMEM;
    250 
    251 	*cookiep = ri;
    252 	*ccolp = *crowp = 0;
    253 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, attrp);
    254 	sc->sc_nscreens++;
    255 
    256 	return(0);
    257 }
    258 
    259 static void
    260 fb_free_screen(void *v, void *cookie)
    261 {
    262 	struct fb_elb_softc *sc = v;
    263 
    264 	if (sc->sc_fb == &console_dev)
    265 		panic("fb_free_screen: freeing console");
    266 
    267 	sc->sc_nscreens--;
    268 }
    269 
    270 static int
    271 fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int),
    272     void *cbarg)
    273 {
    274 	return(0);
    275 }
    276 
    277 void
    278 fb_cnattach(bus_space_tag_t iot, bus_addr_t iobase, void *vram)
    279 {
    280 	struct rasops_info *ri = &console_dev.fb_ri;
    281 	long defattr;
    282 
    283 	console_dev.fb_iot = iot;
    284 	console_dev.fb_ioh = iobase;
    285 	console_dev.fb_vram = vram;
    286 
    287 	fb_init(&console_dev, 1);
    288 
    289 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    290 
    291 	wsdisplay_cnattach(&stdscreen, ri, 0, 0, defattr);
    292 }
    293 
    294 static void
    295 fb_eraserows(void *v, int row, int nrows, long attr)
    296 {
    297 	struct rasops_info *ri = v;
    298 	struct fb_dev *fb = ri->ri_hw;
    299 
    300 	row *= ri->ri_font->fontheight;
    301 	nrows *= ri->ri_font->fontheight;
    302 
    303 	s3_fill(fb, 0, row, ri->ri_stride, nrows, (attr >> 16)&0x0f, 0x0f);
    304 }
    305 
    306 static void
    307 fb_erasecols(void *v, int row, int startcol, int ncols, long attr)
    308 {
    309 	struct rasops_info *ri = v;
    310 	struct fb_dev *fb = ri->ri_hw;
    311 
    312 	row *= ri->ri_font->fontheight;
    313 	startcol *= ri->ri_font->fontwidth;
    314 	ncols *= ri->ri_font->fontwidth;
    315 
    316 	s3_fill(fb, startcol, row, ncols, ri->ri_font->fontheight,
    317 	    (attr >> 16)&0x0f, 0x0f);
    318 }
    319 
    320 static void
    321 fb_copyrows(void *v, int srcrow, int dstrow, int nrows)
    322 {
    323 	struct rasops_info *ri = v;
    324 	struct fb_dev *fb = ri->ri_hw;
    325 
    326 	srcrow *= ri->ri_font->fontheight;
    327 	dstrow *= ri->ri_font->fontheight;
    328 	nrows *= ri->ri_font->fontheight;
    329 
    330 	s3_copy(fb, 0, srcrow, 0, dstrow, ri->ri_stride, nrows, 0x0f);
    331 }
    332 
    333 static void
    334 fb_copycols(void *v, int row, int srccol, int dstcol, int ncols)
    335 {
    336 	struct rasops_info *ri = v;
    337 	struct fb_dev *fb = ri->ri_hw;
    338 
    339 	row *= ri->ri_font->fontheight;
    340 	srccol *= ri->ri_font->fontwidth;
    341 	dstcol *= ri->ri_font->fontwidth;
    342 	ncols *= ri->ri_font->fontwidth;
    343 
    344 	s3_copy(fb, srccol, row, dstcol, row,
    345 	    ncols, ri->ri_font->fontheight, 0x0f);
    346 }
    347 
    348 /*
    349  * S3 support routines
    350  */
    351 
    352 #define S3_CRTC_INDEX		0x83d4
    353 #define S3_CRTC_DATA		0x83d5
    354 
    355 #define S3_DAC_RD_INDEX		0x83c7
    356 #define S3_DAC_WR_INDEX		0x83c8
    357 #define S3_DAC_DATA		0x83c9
    358 
    359 #define S3_CUR_Y		0x82e8
    360 #define S3_CUR_X		0x86e8
    361 #define S3_DESTY_AXSTP		0x8ae8
    362 #define S3_DESTX_DIASTP		0x8ee8
    363 #define S3_MAJ_AXIS_PCNT	0x96e8
    364 #define S3_GP_STAT		0x9ae8
    365 #define S3_CMD			0x9ae8
    366 #define S3_BKGD_COLOR		0xa2e8
    367 #define S3_FRGD_COLOR		0xa6e8
    368 #define S3_WRT_MASK		0xaae8
    369 #define S3_RD_MASK		0xaee8
    370 #define S3_BKGD_MIX		0xb6e8
    371 #define S3_FRGD_MIX		0xbae8
    372 #define S3_MULTIFUNC_CNTL	0xbee8
    373 
    374 #define S3_GP_STAT_FIFO_1	0x0080
    375 #define S3_GP_STAT_BSY		0x0200
    376 
    377 #define S3_CMD_BITBLT		0xc001
    378 #define S3_CMD_RECT		0x4001
    379 #define S3_INC_Y		0x0080
    380 #define S3_INC_X		0x0020
    381 #define S3_DRAW			0x0010
    382 #define S3_MULTI		0x0002
    383 
    384 #define S3_CSRC_BKGDCOL		0x0000
    385 #define S3_CSRC_FRGDCOL		0x0020
    386 #define S3_CSRC_DISPMEM		0x0060
    387 #define S3_MIX_NEW		0x0007
    388 
    389 #define CMAP_SIZE		256
    390 
    391 static u_int8_t default_cmap[] = {
    392 	/* black */		  0,   0,   0,
    393 	/* blue */		  0,   0, 192,
    394 	/* green */		  0, 192,   0,
    395 	/* cyan */		  0, 192, 192,
    396 	/* red */		192,   0,   0,
    397 	/* magenta */		192,   0, 192,
    398 	/* brown */		192, 192,   0,
    399 	/* lightgrey */		212, 208, 200,
    400 	/* darkgrey */		200, 192, 188,
    401 	/* lightblue */		  0,   0, 255,
    402 	/* lightgreen */	  0, 255,   0,
    403 	/* lightcyan */		  0, 255, 255,
    404 	/* lightred */		255,   0,   0,
    405 	/* lightmagenta */	255,   0, 255,
    406 	/* yellow */		255, 255,   0,
    407 	/* white */		255, 255, 255,
    408 };
    409 
    410 static void
    411 s3_init(struct fb_dev *fb, int *width, int *height)
    412 {
    413 	int i, j, w, h;
    414 	bus_space_tag_t iot = fb->fb_iot;
    415 	bus_space_handle_t ioh = fb->fb_ioh;
    416 
    417 	/* Initialize colormap */
    418 
    419 	bus_space_write_1(iot, ioh, S3_DAC_WR_INDEX, 0);
    420 
    421 	for (i = j = 0; i < CMAP_SIZE*3; i++) {
    422 		bus_space_write_1(iot, ioh, S3_DAC_DATA, default_cmap[j] >> 2);
    423 		j = (j+1) % sizeof(default_cmap)/sizeof(default_cmap[0]);
    424 	}
    425 
    426 	/* Retrieve frame buffer geometry */
    427 
    428 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 1);
    429 	w = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
    430 
    431 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 18);
    432 	h = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
    433 
    434 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 7);
    435 	i = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
    436 
    437 	h += (i << 7) & 0x100;
    438 	h += (i << 3) & 0x200;
    439 
    440 	*width = (w+1) << 3;
    441 	*height = h+1;
    442 }
    443 
    444 static void
    445 s3_copy(struct fb_dev *fb, int src_x, int src_y, int dest_x, int dest_y,
    446     int width, int height, int mask)
    447 {
    448 	bus_space_tag_t iot = fb->fb_iot;
    449 	bus_space_handle_t ioh = fb->fb_ioh;
    450 	u_int16_t cmd = S3_CMD_BITBLT | S3_DRAW;
    451 
    452 	if (src_x > dest_x)
    453 		cmd |= S3_INC_X;
    454 	else {
    455 		src_x += width-1;
    456 		dest_x += width-1;
    457 	}
    458 
    459 	if (src_y > dest_y)
    460 		cmd |= S3_INC_Y;
    461 	else {
    462 		src_y += height-1;
    463 		dest_y += height-1;
    464 	}
    465 
    466 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
    467 		;
    468 
    469 	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_DISPMEM | S3_MIX_NEW);
    470 	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
    471 	bus_space_write_2(iot, ioh, S3_CUR_X, src_x);
    472 	bus_space_write_2(iot, ioh, S3_CUR_Y, src_y);
    473 	bus_space_write_2(iot, ioh, S3_DESTX_DIASTP, dest_x);
    474 	bus_space_write_2(iot, ioh, S3_DESTY_AXSTP, dest_y);
    475 	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
    476 	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
    477 	bus_space_write_2(iot, ioh, S3_CMD, cmd);
    478 
    479 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
    480 		;
    481 }
    482 
    483 static void
    484 s3_fill(struct fb_dev *fb, int x, int y, int width, int height,
    485     int color, int mask)
    486 {
    487 	bus_space_tag_t iot = fb->fb_iot;
    488 	bus_space_handle_t ioh = fb->fb_ioh;
    489 	u_int16_t cmd = S3_CMD_RECT | S3_INC_X | S3_INC_Y | S3_DRAW;
    490 
    491 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_FIFO_1)
    492 		;
    493 
    494 	bus_space_write_2(iot, ioh, S3_FRGD_MIX, S3_CSRC_FRGDCOL | S3_MIX_NEW);
    495 	bus_space_write_2(iot, ioh, S3_FRGD_COLOR, color);
    496 	bus_space_write_2(iot, ioh, S3_WRT_MASK, mask);
    497 	bus_space_write_2(iot, ioh, S3_CUR_X, x);
    498 	bus_space_write_2(iot, ioh, S3_CUR_Y, y);
    499 	bus_space_write_2(iot, ioh, S3_MULTIFUNC_CNTL, height-1);
    500 	bus_space_write_2(iot, ioh, S3_MAJ_AXIS_PCNT, width-1);
    501 	bus_space_write_2(iot, ioh, S3_CMD, cmd);
    502 
    503 	while (bus_space_read_2(iot, ioh, S3_GP_STAT) & S3_GP_STAT_BSY)
    504 		;
    505 }
    506