Home | History | Annotate | Line # | Download | only in sbus
cgtwelve.c revision 1.1
      1 /*	$NetBSD: cgtwelve.c,v 1.1 2010/03/24 00:33:06 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 Michael Lorenz
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /* a console driver for the Sun CG12 / Matrox SG3 graphics board */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: cgtwelve.c,v 1.1 2010/03/24 00:33:06 macallan Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/systm.h>
     36 #include <sys/buf.h>
     37 #include <sys/device.h>
     38 #include <sys/ioctl.h>
     39 #include <sys/conf.h>
     40 
     41 #include <sys/bus.h>
     42 #include <machine/autoconf.h>
     43 
     44 #include <dev/sbus/sbusvar.h>
     45 #include <dev/sun/fbio.h>
     46 #include <dev/sun/fbvar.h>
     47 
     48 #include <dev/wscons/wsdisplayvar.h>
     49 #include <dev/wscons/wsconsio.h>
     50 #include <dev/wsfont/wsfont.h>
     51 #include <dev/rasops/rasops.h>
     52 
     53 #include <dev/wscons/wsdisplay_vconsvar.h>
     54 
     55 #include <dev/sbus/cgtwelvereg.h>
     56 
     57 #include "opt_wsemul.h"
     58 #include "opt_cgtwelve.h"
     59 
     60 
     61 struct cgtwelve_softc {
     62 	device_t	sc_dev;
     63 	bus_space_tag_t sc_tag;
     64 	void		*sc_fbaddr;
     65 	int		sc_width;
     66 	int		sc_height;
     67 	int		sc_stride;
     68 	int		sc_fbsize;
     69 	int		sc_mode;
     70 	struct vcons_data vd;
     71 };
     72 
     73 static int	cgtwelve_match(device_t, cfdata_t, void *);
     74 static void	cgtwelve_attach(device_t, device_t, void *);
     75 static int	cgtwelve_ioctl(void *, void *, u_long, void *, int,
     76 				 struct lwp*);
     77 static paddr_t	cgtwelve_mmap(void *, void *, off_t, int);
     78 static void	cgtwelve_init_screen(void *, struct vcons_screen *, int,
     79 				 long *);
     80 
     81 CFATTACH_DECL_NEW(cgtwelve, sizeof(struct cgtwelve_softc),
     82     cgtwelve_match, cgtwelve_attach, NULL, NULL);
     83 
     84 struct wsscreen_descr cgtwelve_defscreendesc = {
     85 	"default",
     86 	0, 0,
     87 	NULL,
     88 	8, 16,
     89 	0,
     90 };
     91 
     92 static struct vcons_screen cgtwelve_console_screen;
     93 
     94 const struct wsscreen_descr *_cgtwelve_scrlist[] = {
     95 	&cgtwelve_defscreendesc,
     96 	/* XXX other formats, graphics screen? */
     97 };
     98 
     99 struct wsscreen_list cgtwelve_screenlist = {
    100 	sizeof(_cgtwelve_scrlist) / sizeof(struct wsscreen_descr *),
    101 	_cgtwelve_scrlist
    102 };
    103 
    104 struct wsdisplay_accessops cgtwelve_accessops = {
    105 	cgtwelve_ioctl,
    106 	cgtwelve_mmap,
    107 	NULL,	/* vcons_alloc_screen */
    108 	NULL,	/* vcons_free_screen */
    109 	NULL,	/* vcons_show_screen */
    110 	NULL,	/* load_font */
    111 	NULL,	/* polls */
    112 	NULL,	/* scroll */
    113 };
    114 
    115 static int
    116 cgtwelve_match(device_t parent, cfdata_t cf, void *aux)
    117 {
    118 	struct sbus_attach_args *sa = aux;
    119 
    120 	if (strcmp("cgtwelve", sa->sa_name) == 0)
    121 		return 100;
    122 	return 0;
    123 }
    124 
    125 /*
    126  * Attach a display.  We need to notice if it is the console, too.
    127  */
    128 static void
    129 cgtwelve_attach(device_t parent, device_t self, void *args)
    130 {
    131 	struct cgtwelve_softc *sc = device_private(self);
    132 	struct sbus_attach_args *sa = args;
    133 	struct wsemuldisplaydev_attach_args aa;
    134 	struct rasops_info *ri;
    135 	unsigned long defattr;
    136 	bus_space_handle_t bh;
    137 	int node = sa->sa_node;
    138 	int isconsole;
    139 
    140 	aprint_normal("\n");
    141 	sc->sc_dev = self;
    142 	sc->sc_tag = sa->sa_bustag;
    143 
    144 	/* read geometry information from the device tree */
    145 	sc->sc_width = prom_getpropint(sa->sa_node, "width", 1152);
    146 	sc->sc_height = prom_getpropint(sa->sa_node, "height", 900);
    147 	sc->sc_stride = (sc->sc_width + 7) >> 3;
    148 
    149 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
    150 	sc->sc_fbaddr = (void *)prom_getpropint(sa->sa_node, "address", 0);
    151 	if (sc->sc_fbaddr == NULL) {
    152 		if (sbus_bus_map(sa->sa_bustag,
    153 			 sa->sa_slot,
    154 			 sa->sa_offset + CG12_FB_MONO,
    155 			 sc->sc_fbsize,
    156 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    157 			aprint_error_dev(self, "cannot map framebuffer\n");
    158 			return;
    159 		}
    160 		sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh);
    161 	}
    162 
    163 	aprint_normal_dev(self, "%d x %d\n", sc->sc_width, sc->sc_height);
    164 
    165 	isconsole = fb_is_console(node);
    166 
    167 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    168 	wsfont_init();
    169 
    170 	vcons_init(&sc->vd, sc, &cgtwelve_defscreendesc, &cgtwelve_accessops);
    171 	sc->vd.init_screen = cgtwelve_init_screen;
    172 
    173 	vcons_init_screen(&sc->vd, &cgtwelve_console_screen, 1, &defattr);
    174 	cgtwelve_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    175 
    176 	memset(sc->sc_fbaddr, 0, sc->sc_fbsize);
    177 
    178 	ri = &cgtwelve_console_screen.scr_ri;
    179 
    180 	cgtwelve_defscreendesc.nrows = ri->ri_rows;
    181 	cgtwelve_defscreendesc.ncols = ri->ri_cols;
    182 	cgtwelve_defscreendesc.textops = &ri->ri_ops;
    183 	cgtwelve_defscreendesc.capabilities = ri->ri_caps;
    184 
    185 	if(isconsole) {
    186 		wsdisplay_cnattach(&cgtwelve_defscreendesc, ri, 0, 0, defattr);
    187 		vcons_replay_msgbuf(&cgtwelve_console_screen);
    188 	}
    189 
    190 	aa.console = isconsole;
    191 	aa.scrdata = &cgtwelve_screenlist;
    192 	aa.accessops = &cgtwelve_accessops;
    193 	aa.accesscookie = &sc->vd;
    194 
    195 	config_found(self, &aa, wsemuldisplaydevprint);
    196 
    197 }
    198 
    199 
    200 static void
    201 cgtwelve_init_screen(void *cookie, struct vcons_screen *scr,
    202     int existing, long *defattr)
    203 {
    204 	struct cgtwelve_softc *sc = cookie;
    205 	struct rasops_info *ri = &scr->scr_ri;
    206 
    207 	ri->ri_depth = 1;
    208 	ri->ri_width = sc->sc_width;
    209 	ri->ri_height = sc->sc_height;
    210 	ri->ri_stride = sc->sc_stride;
    211 	ri->ri_flg = RI_CENTER;
    212 
    213 	ri->ri_bits = sc->sc_fbaddr;
    214 
    215 	rasops_init(ri, ri->ri_height/8, ri->ri_width/8);
    216 	ri->ri_caps = WSSCREEN_REVERSE;
    217 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    218 		    ri->ri_width / ri->ri_font->fontwidth);
    219 }
    220 
    221 static int
    222 cgtwelve_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    223     struct lwp *l)
    224 {
    225 
    226 	switch (cmd) {
    227 		case WSDISPLAYIO_GTYPE:
    228 			*(u_int *)data = WSDISPLAY_TYPE_SUNCG12;
    229 			return 0;
    230 	}
    231 
    232 	return EPASSTHROUGH;
    233 }
    234 
    235 static paddr_t
    236 cgtwelve_mmap(void *v, void *vs, off_t offset, int prot)
    237 {
    238 	struct cgtwelve_softc *sc = v;
    239 
    240 	/* regular fb mapping at 0 */
    241 	if ((offset >= 0) && (offset < sc->sc_fbsize)) {
    242 #if 0
    243 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
    244 		    CG12_FB_MONO + offset, prot,
    245 		    BUS_SPACE_MAP_LINEAR);
    246 #endif
    247 	}
    248 
    249 	return -1;
    250 }
    251