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