Home | History | Annotate | Line # | Download | only in dev
fb_elb.c revision 1.1
      1  1.1  hannken /*	$NetBSD: fb_elb.c,v 1.1 2003/03/11 10:57:57 hannken Exp $	*/
      2  1.1  hannken 
      3  1.1  hannken /*-
      4  1.1  hannken  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  1.1  hannken  * All rights reserved.
      6  1.1  hannken  *
      7  1.1  hannken  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  hannken  * by Juergen Hannken-Illjes.
      9  1.1  hannken  *
     10  1.1  hannken  * Redistribution and use in source and binary forms, with or without
     11  1.1  hannken  * modification, are permitted provided that the following conditions
     12  1.1  hannken  * are met:
     13  1.1  hannken  * 1. Redistributions of source code must retain the above copyright
     14  1.1  hannken  *    notice, this list of conditions and the following disclaimer.
     15  1.1  hannken  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  hannken  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  hannken  *    documentation and/or other materials provided with the distribution.
     18  1.1  hannken  * 3. All advertising materials mentioning features or use of this software
     19  1.1  hannken  *    must display the following acknowledgement:
     20  1.1  hannken  *      This product includes software developed by the NetBSD
     21  1.1  hannken  *      Foundation, Inc. and its contributors.
     22  1.1  hannken  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  hannken  *    contributors may be used to endorse or promote products derived
     24  1.1  hannken  *    from this software without specific prior written permission.
     25  1.1  hannken  *
     26  1.1  hannken  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  hannken  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  hannken  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  hannken  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  hannken  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  hannken  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  hannken  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  hannken  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  hannken  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  hannken  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  hannken  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  hannken  */
     38  1.1  hannken 
     39  1.1  hannken #include <sys/param.h>
     40  1.1  hannken #include <sys/conf.h>
     41  1.1  hannken #include <sys/device.h>
     42  1.1  hannken #include <sys/ioctl.h>
     43  1.1  hannken #include <sys/malloc.h>
     44  1.1  hannken #include <sys/systm.h>
     45  1.1  hannken 
     46  1.1  hannken #include <dev/wscons/wsconsio.h>
     47  1.1  hannken #include <dev/wscons/wsdisplayvar.h>
     48  1.1  hannken #include <dev/rasops/rasops.h>
     49  1.1  hannken 
     50  1.1  hannken #include <machine/explora.h>
     51  1.1  hannken #include <machine/bus.h>
     52  1.1  hannken 
     53  1.1  hannken #include <evbppc/explora/dev/elbvar.h>
     54  1.1  hannken 
     55  1.1  hannken #define FB_NPORTS		65536
     56  1.1  hannken 
     57  1.1  hannken struct fb_dev {
     58  1.1  hannken 	void *fb_vram;
     59  1.1  hannken 	bus_space_tag_t fb_iot;
     60  1.1  hannken 	bus_space_handle_t fb_ioh;
     61  1.1  hannken 	struct rasops_info fb_ri;
     62  1.1  hannken };
     63  1.1  hannken 
     64  1.1  hannken struct fb_elb_softc {
     65  1.1  hannken 	struct device sc_dev;
     66  1.1  hannken 	struct fb_dev *sc_fb;
     67  1.1  hannken 	int sc_nscreens;
     68  1.1  hannken };
     69  1.1  hannken 
     70  1.1  hannken static int	fb_elb_probe(struct device *, struct cfdata *, void *);
     71  1.1  hannken static void	fb_elb_attach(struct device *, struct device *, void *);
     72  1.1  hannken void		fb_cnattach(bus_space_tag_t, bus_addr_t, void *);
     73  1.1  hannken static void	fb_init(struct fb_dev *, int);
     74  1.1  hannken static int	fb_ioctl(void *, u_long, caddr_t, int, struct proc *);
     75  1.1  hannken static paddr_t	fb_mmap(void *, off_t, int);
     76  1.1  hannken static int	fb_alloc_screen(void *, const struct wsscreen_descr *, void **,
     77  1.1  hannken                     int *, int *, long *);
     78  1.1  hannken static void	fb_free_screen(void *, void *);
     79  1.1  hannken static int	fb_show_screen(void *, void *, int, void (*)(void *, int, int),
     80  1.1  hannken                     void *);
     81  1.1  hannken 
     82  1.1  hannken static void	s3_init(struct fb_dev *, int *, int *);
     83  1.1  hannken 
     84  1.1  hannken static struct fb_dev console_dev;
     85  1.1  hannken 
     86  1.1  hannken static struct wsdisplay_accessops accessops = {
     87  1.1  hannken 	fb_ioctl,
     88  1.1  hannken 	fb_mmap,
     89  1.1  hannken 	fb_alloc_screen,
     90  1.1  hannken 	fb_free_screen,
     91  1.1  hannken 	fb_show_screen,
     92  1.1  hannken 	NULL
     93  1.1  hannken };
     94  1.1  hannken 
     95  1.1  hannken static struct wsscreen_descr stdscreen = {
     96  1.1  hannken 	"std",
     97  1.1  hannken 	0, 0,
     98  1.1  hannken 	0,
     99  1.1  hannken 	0, 0,
    100  1.1  hannken 	0
    101  1.1  hannken };
    102  1.1  hannken 
    103  1.1  hannken static const struct wsscreen_descr *scrlist[] = {
    104  1.1  hannken 	&stdscreen
    105  1.1  hannken };
    106  1.1  hannken 
    107  1.1  hannken static struct wsscreen_list screenlist = {
    108  1.1  hannken 	sizeof(scrlist)/sizeof(scrlist[0]), scrlist
    109  1.1  hannken };
    110  1.1  hannken 
    111  1.1  hannken CFATTACH_DECL(fb_elb, sizeof(struct fb_elb_softc),
    112  1.1  hannken     fb_elb_probe, fb_elb_attach, NULL, NULL);
    113  1.1  hannken 
    114  1.1  hannken static int
    115  1.1  hannken fb_elb_probe(struct device *parent, struct cfdata *cf, void *aux)
    116  1.1  hannken {
    117  1.1  hannken 	struct elb_attach_args *oaa = aux;
    118  1.1  hannken 
    119  1.1  hannken 	if (strcmp(oaa->elb_name, cf->cf_name) != 0)
    120  1.1  hannken 		return 0;
    121  1.1  hannken 
    122  1.1  hannken 	return (1);
    123  1.1  hannken }
    124  1.1  hannken 
    125  1.1  hannken static void
    126  1.1  hannken fb_elb_attach(struct device *parent, struct device *self, void *aux)
    127  1.1  hannken {
    128  1.1  hannken 	struct fb_elb_softc *sc = (void *)self;
    129  1.1  hannken 	struct elb_attach_args *eaa = aux;
    130  1.1  hannken 	struct wsemuldisplaydev_attach_args waa;
    131  1.1  hannken 	struct rasops_info *ri;
    132  1.1  hannken 	bus_space_handle_t ioh;
    133  1.1  hannken 	int is_console;
    134  1.1  hannken 
    135  1.1  hannken 	is_console = ((void *)eaa->elb_base == console_dev.fb_vram);
    136  1.1  hannken 
    137  1.1  hannken 	if (is_console) {
    138  1.1  hannken 		sc->sc_fb = &console_dev;
    139  1.1  hannken 	} else {
    140  1.1  hannken 		sc->sc_fb = malloc(sizeof(struct fb_dev), M_DEVBUF, M_WAITOK);
    141  1.1  hannken 		memset(sc->sc_fb, 0, sizeof(struct fb_dev));
    142  1.1  hannken 	}
    143  1.1  hannken 
    144  1.1  hannken 	sc->sc_fb->fb_iot = eaa->elb_bt;
    145  1.1  hannken 	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base, SIZE_FB,
    146  1.1  hannken 	    BUS_SPACE_MAP_LINEAR, &ioh);
    147  1.1  hannken 	sc->sc_fb->fb_vram = bus_space_vaddr(sc->sc_fb->fb_iot, ioh);
    148  1.1  hannken 	bus_space_map(sc->sc_fb->fb_iot, eaa->elb_base2, FB_NPORTS,
    149  1.1  hannken 	    0, &sc->sc_fb->fb_ioh);
    150  1.1  hannken 
    151  1.1  hannken 	fb_init(sc->sc_fb, !is_console);
    152  1.1  hannken 
    153  1.1  hannken 	ri = &sc->sc_fb->fb_ri;
    154  1.1  hannken 
    155  1.1  hannken 	printf(": %d x %d\n", ri->ri_rows, ri->ri_cols);
    156  1.1  hannken 
    157  1.1  hannken 	waa.console = is_console;
    158  1.1  hannken 	waa.scrdata = &screenlist;
    159  1.1  hannken 	waa.accessops = &accessops;
    160  1.1  hannken 	waa.accesscookie = sc;
    161  1.1  hannken 
    162  1.1  hannken 	config_found_sm(self, &waa, wsemuldisplaydevprint, NULL);
    163  1.1  hannken }
    164  1.1  hannken 
    165  1.1  hannken static void
    166  1.1  hannken fb_init(struct fb_dev *fb, int full)
    167  1.1  hannken {
    168  1.1  hannken 	struct rasops_info *ri = &fb->fb_ri;
    169  1.1  hannken 
    170  1.1  hannken 	if (full) {
    171  1.1  hannken 		s3_init(fb, &ri->ri_width, &ri->ri_height);
    172  1.1  hannken 		ri->ri_depth = 8;
    173  1.1  hannken 		ri->ri_stride = ri->ri_width;
    174  1.1  hannken 		ri->ri_bits = fb->fb_vram;
    175  1.1  hannken 		ri->ri_flg = RI_CENTER;
    176  1.1  hannken 
    177  1.1  hannken 		rasops_init(ri, 500, 500);
    178  1.1  hannken 	} else {
    179  1.1  hannken 		ri->ri_origbits = fb->fb_vram;	/*XXX*/
    180  1.1  hannken 		rasops_reconfig(ri, 500, 500);
    181  1.1  hannken 	}
    182  1.1  hannken 
    183  1.1  hannken 	stdscreen.nrows = ri->ri_rows;
    184  1.1  hannken 	stdscreen.ncols = ri->ri_cols;
    185  1.1  hannken 	stdscreen.textops = &ri->ri_ops;
    186  1.1  hannken 	stdscreen.capabilities = ri->ri_caps;
    187  1.1  hannken }
    188  1.1  hannken 
    189  1.1  hannken static int
    190  1.1  hannken fb_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
    191  1.1  hannken {
    192  1.1  hannken 	struct fb_elb_softc *sc = v;
    193  1.1  hannken 	struct rasops_info *ri = &sc->sc_fb->fb_ri;
    194  1.1  hannken 	struct wsdisplay_fbinfo *wdf;
    195  1.1  hannken 
    196  1.1  hannken 	switch (cmd) {
    197  1.1  hannken 	case WSDISPLAYIO_GTYPE:
    198  1.1  hannken 		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
    199  1.1  hannken 		return(0);
    200  1.1  hannken 
    201  1.1  hannken 	case WSDISPLAYIO_GINFO:
    202  1.1  hannken 		wdf = (void *)data;
    203  1.1  hannken 		wdf->height = ri->ri_height;
    204  1.1  hannken 		wdf->width = ri->ri_width;
    205  1.1  hannken 		wdf->depth = ri->ri_depth;
    206  1.1  hannken 		wdf->cmsize = 16; /*XXX*/
    207  1.1  hannken 		return(0);
    208  1.1  hannken 
    209  1.1  hannken 	case WSDISPLAYIO_SVIDEO:
    210  1.1  hannken 	case WSDISPLAYIO_GETCMAP:
    211  1.1  hannken 	case WSDISPLAYIO_PUTCMAP:
    212  1.1  hannken 		break;
    213  1.1  hannken 	}
    214  1.1  hannken 
    215  1.1  hannken 	return(EPASSTHROUGH);
    216  1.1  hannken }
    217  1.1  hannken 
    218  1.1  hannken static paddr_t
    219  1.1  hannken fb_mmap(void *v, off_t offset, int prot)
    220  1.1  hannken {
    221  1.1  hannken 	return -1;
    222  1.1  hannken }
    223  1.1  hannken 
    224  1.1  hannken static int
    225  1.1  hannken fb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc, void **cookiep,
    226  1.1  hannken     int *ccolp, int *crowp, long *attrp)
    227  1.1  hannken {
    228  1.1  hannken 	struct fb_elb_softc *sc = v;
    229  1.1  hannken 	struct rasops_info *ri = &sc->sc_fb->fb_ri;
    230  1.1  hannken 
    231  1.1  hannken 	if (sc->sc_nscreens > 0)
    232  1.1  hannken 		return ENOMEM;
    233  1.1  hannken 
    234  1.1  hannken 	*cookiep = ri;
    235  1.1  hannken 	*ccolp = *crowp = 0;
    236  1.1  hannken 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, attrp);
    237  1.1  hannken 	sc->sc_nscreens++;
    238  1.1  hannken 
    239  1.1  hannken 	return(0);
    240  1.1  hannken }
    241  1.1  hannken 
    242  1.1  hannken static void
    243  1.1  hannken fb_free_screen(void *v, void *cookie)
    244  1.1  hannken {
    245  1.1  hannken 	struct fb_elb_softc *sc = v;
    246  1.1  hannken 
    247  1.1  hannken 	if (sc->sc_fb == &console_dev)
    248  1.1  hannken 		panic("fb_free_screen: freeing console");
    249  1.1  hannken 
    250  1.1  hannken 	sc->sc_nscreens--;
    251  1.1  hannken }
    252  1.1  hannken 
    253  1.1  hannken static int
    254  1.1  hannken fb_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int),
    255  1.1  hannken     void *cbarg)
    256  1.1  hannken {
    257  1.1  hannken 	return(0);
    258  1.1  hannken }
    259  1.1  hannken 
    260  1.1  hannken void
    261  1.1  hannken fb_cnattach(bus_space_tag_t iot, bus_addr_t iobase, void *vram)
    262  1.1  hannken {
    263  1.1  hannken 	struct rasops_info *ri = &console_dev.fb_ri;
    264  1.1  hannken 	long defattr;
    265  1.1  hannken 
    266  1.1  hannken 	console_dev.fb_iot = iot;
    267  1.1  hannken 	console_dev.fb_ioh = iobase;
    268  1.1  hannken 	console_dev.fb_vram = vram;
    269  1.1  hannken 
    270  1.1  hannken 	fb_init(&console_dev, 1);
    271  1.1  hannken 
    272  1.1  hannken 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    273  1.1  hannken 
    274  1.1  hannken 	wsdisplay_cnattach(&stdscreen, ri, 0, 0, defattr);
    275  1.1  hannken }
    276  1.1  hannken 
    277  1.1  hannken /*
    278  1.1  hannken  * S3 support routines
    279  1.1  hannken  */
    280  1.1  hannken 
    281  1.1  hannken #define S3_CRTC_INDEX		0x83d4
    282  1.1  hannken #define S3_CRTC_DATA		0x83d5
    283  1.1  hannken 
    284  1.1  hannken #define S3_DAC_RD_INDEX		0x83c7
    285  1.1  hannken #define S3_DAC_WR_INDEX		0x83c8
    286  1.1  hannken #define S3_DAC_DATA		0x83c9
    287  1.1  hannken 
    288  1.1  hannken #define CMAP_SIZE		256
    289  1.1  hannken 
    290  1.1  hannken static u_int8_t default_cmap[] = {
    291  1.1  hannken 	/* black */		  0,   0,   0,
    292  1.1  hannken 	/* blue */		  0,   0, 192,
    293  1.1  hannken 	/* green */		  0, 192,   0,
    294  1.1  hannken 	/* cyan */		  0, 192, 192,
    295  1.1  hannken 	/* red */		192,   0,   0,
    296  1.1  hannken 	/* magenta */		192,   0, 192,
    297  1.1  hannken 	/* brown */		192, 192,   0,
    298  1.1  hannken 	/* lightgrey */		212, 208, 200,
    299  1.1  hannken 	/* darkgrey */		200, 192, 188,
    300  1.1  hannken 	/* lightblue */		  0,   0, 255,
    301  1.1  hannken 	/* lightgreen */	  0, 255,   0,
    302  1.1  hannken 	/* lightcyan */		  0, 255, 255,
    303  1.1  hannken 	/* lightred */		255,   0,   0,
    304  1.1  hannken 	/* lightmagenta */	255,   0, 255,
    305  1.1  hannken 	/* yellow */		255, 255,   0,
    306  1.1  hannken 	/* white */		255, 255, 255,
    307  1.1  hannken };
    308  1.1  hannken 
    309  1.1  hannken static void
    310  1.1  hannken s3_init(struct fb_dev *fb, int *width, int *height)
    311  1.1  hannken {
    312  1.1  hannken 	int i, j, w, h;
    313  1.1  hannken 	bus_space_tag_t iot = fb->fb_iot;
    314  1.1  hannken 	bus_space_handle_t ioh = fb->fb_ioh;
    315  1.1  hannken 
    316  1.1  hannken 	/* Initialize colormap */
    317  1.1  hannken 
    318  1.1  hannken 	bus_space_write_1(iot, ioh, S3_DAC_WR_INDEX, 0);
    319  1.1  hannken 
    320  1.1  hannken 	for (i = j = 0; i < CMAP_SIZE*3; i++) {
    321  1.1  hannken 		bus_space_write_1(iot, ioh, S3_DAC_DATA, default_cmap[j] >> 2);
    322  1.1  hannken 		j = (j+1) % sizeof(default_cmap)/sizeof(default_cmap[0]);
    323  1.1  hannken 	}
    324  1.1  hannken 
    325  1.1  hannken 	/* Retrieve frame buffer geometry */
    326  1.1  hannken 
    327  1.1  hannken 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 1);
    328  1.1  hannken 	w = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
    329  1.1  hannken 
    330  1.1  hannken 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 18);
    331  1.1  hannken 	h = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
    332  1.1  hannken 
    333  1.1  hannken 	bus_space_write_1(iot, ioh, S3_CRTC_INDEX, 7);
    334  1.1  hannken 	i = bus_space_read_1(iot, ioh, S3_CRTC_DATA);
    335  1.1  hannken 
    336  1.1  hannken 	h += (i << 7) & 0x100;
    337  1.1  hannken 	h += (i << 3) & 0x200;
    338  1.1  hannken 
    339  1.1  hannken 	*width = (w+1) << 3;
    340  1.1  hannken 	*height = h+1;
    341  1.1  hannken }
    342