Home | History | Annotate | Line # | Download | only in sbus
mgx.c revision 1.1
      1 /*	$NetBSD: mgx.c,v 1.1 2014/12/16 21:01:34 macallan Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 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 SSB 4096V-MGX graphics card */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: mgx.c,v 1.1 2014/12/16 21:01:34 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 #include <sys/kmem.h>
     41 
     42 #include <sys/bus.h>
     43 #include <machine/autoconf.h>
     44 
     45 #include <dev/sbus/sbusvar.h>
     46 #include <dev/sun/fbio.h>
     47 #include <dev/sun/fbvar.h>
     48 
     49 #include <dev/wscons/wsdisplayvar.h>
     50 #include <dev/wscons/wsconsio.h>
     51 #include <dev/wsfont/wsfont.h>
     52 #include <dev/rasops/rasops.h>
     53 
     54 #include <dev/wscons/wsdisplay_vconsvar.h>
     55 
     56 #include <dev/ic/vgareg.h>
     57 #include <dev/sbus/mgxreg.h>
     58 
     59 #include "opt_wsemul.h"
     60 
     61 
     62 struct mgx_softc {
     63 	device_t	sc_dev;
     64 	bus_space_tag_t sc_tag;
     65 	bus_space_handle_t sc_blith;
     66 	bus_space_handle_t sc_vgah;
     67 	bus_addr_t	sc_paddr;
     68 	void		*sc_fbaddr;
     69 	int		sc_width;
     70 	int		sc_height;
     71 	int		sc_stride;
     72 	int		sc_fbsize;
     73 	int		sc_mode;
     74 	u_char		sc_cmap_red[256];
     75 	u_char		sc_cmap_green[256];
     76 	u_char		sc_cmap_blue[256];
     77 	struct vcons_screen sc_console_screen;
     78 	struct wsscreen_descr sc_defaultscreen_descr;
     79 	const struct wsscreen_descr *sc_screens[1];
     80 	struct wsscreen_list sc_screenlist;
     81 	struct vcons_data vd;
     82 };
     83 
     84 static int	mgx_match(device_t, cfdata_t, void *);
     85 static void	mgx_attach(device_t, device_t, void *);
     86 static int	mgx_ioctl(void *, void *, u_long, void *, int,
     87 				 struct lwp*);
     88 static paddr_t	mgx_mmap(void *, void *, off_t, int);
     89 static void	mgx_init_screen(void *, struct vcons_screen *, int,
     90 				 long *);
     91 static void	mgx_write_dac(struct mgx_softc *, int, int, int, int);
     92 static void	mgx_setup(struct mgx_softc *, int);
     93 static void	mgx_init_palette(struct mgx_softc *);
     94 
     95 CFATTACH_DECL_NEW(mgx, sizeof(struct mgx_softc),
     96     mgx_match, mgx_attach, NULL, NULL);
     97 
     98 struct wsdisplay_accessops mgx_accessops = {
     99 	mgx_ioctl,
    100 	mgx_mmap,
    101 	NULL,	/* vcons_alloc_screen */
    102 	NULL,	/* vcons_free_screen */
    103 	NULL,	/* vcons_show_screen */
    104 	NULL,	/* load_font */
    105 	NULL,	/* polls */
    106 	NULL,	/* scroll */
    107 };
    108 
    109 static int
    110 mgx_match(device_t parent, cfdata_t cf, void *aux)
    111 {
    112 	struct sbus_attach_args *sa = aux;
    113 
    114 	if (strcmp("SMSI,mgx", sa->sa_name) == 0)
    115 		return 100;
    116 	return 0;
    117 }
    118 
    119 /*
    120  * Attach a display.  We need to notice if it is the console, too.
    121  */
    122 static void
    123 mgx_attach(device_t parent, device_t self, void *args)
    124 {
    125 	struct mgx_softc *sc = device_private(self);
    126 	struct sbus_attach_args *sa = args;
    127 	struct wsemuldisplaydev_attach_args aa;
    128 	struct rasops_info *ri;
    129 	unsigned long defattr;
    130 	bus_space_handle_t bh;
    131 	int node = sa->sa_node;
    132 	int isconsole;
    133 
    134 	aprint_normal("\n");
    135 	sc->sc_dev = self;
    136 	sc->sc_tag = sa->sa_bustag;
    137 
    138 	sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot,
    139 	    sa->sa_reg[8].oa_base);
    140 
    141 	/* read geometry information from the device tree */
    142 	sc->sc_width = prom_getpropint(sa->sa_node, "width", 1152);
    143 	sc->sc_height = prom_getpropint(sa->sa_node, "height", 900);
    144 	sc->sc_stride = prom_getpropint(sa->sa_node, "linebytes", 900);
    145 	sc->sc_fbsize = sc->sc_height * sc->sc_stride;
    146 	sc->sc_fbaddr = NULL; //(void *)(unsigned long)prom_getpropint(sa->sa_node, "address", 0);
    147 	if (sc->sc_fbaddr == NULL) {
    148 		if (sbus_bus_map(sa->sa_bustag,
    149 			 sa->sa_slot,
    150 			 sa->sa_reg[8].oa_base,
    151 			 sc->sc_fbsize,
    152 			 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
    153 			aprint_error_dev(self, "cannot map framebuffer\n");
    154 			return;
    155 		}
    156 		sc->sc_fbaddr = bus_space_vaddr(sa->sa_bustag, bh);
    157 	}
    158 
    159 	aprint_normal_dev(self, "%d x %d\n", sc->sc_width, sc->sc_height);
    160 
    161 	if (sbus_bus_map(sa->sa_bustag,
    162 			 sa->sa_slot,
    163 			 sa->sa_reg[4].oa_base, 0x1000, 0,
    164 			 &sc->sc_vgah) != 0) {
    165 		aprint_error("%s: couldn't map VGA registers\n",
    166 		    device_xname(sc->sc_dev));
    167 		return;
    168 	}
    169 
    170 	if (sbus_bus_map(sa->sa_bustag,
    171 			 sa->sa_slot,
    172 			 sa->sa_reg[5].oa_base, 0x1000, 0,
    173 			 &sc->sc_blith) != 0) {
    174 		aprint_error("%s: couldn't map blitter registers\n",
    175 		    device_xname(sc->sc_dev));
    176 		return;
    177 	}
    178 
    179 	mgx_setup(sc, 8);
    180 
    181 	sc->sc_defaultscreen_descr = (struct wsscreen_descr) {
    182 		"default",
    183 		0, 0,
    184 		NULL,
    185 		8, 16,
    186 		WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
    187 		NULL
    188 	};
    189 	sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
    190 	sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
    191 
    192 	isconsole = fb_is_console(node);
    193 
    194 	sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
    195 	wsfont_init();
    196 
    197 	vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr, &mgx_accessops);
    198 	sc->vd.init_screen = mgx_init_screen;
    199 
    200 	vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1, &defattr);
    201 	sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
    202 
    203 	ri = &sc->sc_console_screen.scr_ri;
    204 
    205 	sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
    206 	sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
    207 	sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
    208 	sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
    209 
    210 	mgx_init_palette(sc);
    211 
    212 	if(isconsole) {
    213 		wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0, defattr);
    214 		vcons_replay_msgbuf(&sc->sc_console_screen);
    215 	}
    216 
    217 	aa.console = isconsole;
    218 	aa.scrdata = &sc->sc_screenlist;
    219 	aa.accessops = &mgx_accessops;
    220 	aa.accesscookie = &sc->vd;
    221 
    222 	config_found(self, &aa, wsemuldisplaydevprint);
    223 }
    224 
    225 static void
    226 mgx_write_vga(struct mgx_softc *sc, uint32_t reg, uint8_t val)
    227 {
    228 	bus_space_write_1(sc->sc_tag, sc->sc_vgah, reg ^ 3, val);
    229 }
    230 
    231 static void
    232 mgx_write_dac(struct mgx_softc *sc, int idx, int r, int g, int b)
    233 {
    234 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_ADDRW, idx);
    235 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, r);
    236 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, g);
    237 	mgx_write_vga(sc, VGA_BASE + VGA_DAC_PALETTE, b);
    238 }
    239 
    240 static void
    241 mgx_init_palette(struct mgx_softc *sc)
    242 {
    243 	struct rasops_info *ri = &sc->sc_console_screen.scr_ri;
    244 	int i, j = 0;
    245 	uint8_t cmap[768];
    246 
    247 	rasops_get_cmap(ri, cmap, sizeof(cmap));
    248 	for (i = 0; i < 256; i++) {
    249 		sc->sc_cmap_red[i] = cmap[j];
    250 		sc->sc_cmap_green[i] = cmap[j + 1];
    251 		sc->sc_cmap_blue[i] = cmap[j + 2];
    252 		mgx_write_dac(sc, i, cmap[j], cmap[j + 1], cmap[j + 2]);
    253 		j += 3;
    254 	}
    255 }
    256 
    257 static void
    258 mgx_setup(struct mgx_softc *sc, int depth)
    259 {
    260 }
    261 
    262 static void
    263 mgx_init_screen(void *cookie, struct vcons_screen *scr,
    264     int existing, long *defattr)
    265 {
    266 	struct mgx_softc *sc = cookie;
    267 	struct rasops_info *ri = &scr->scr_ri;
    268 
    269 	ri->ri_depth = 8;
    270 	ri->ri_width = sc->sc_width;
    271 	ri->ri_height = sc->sc_height;
    272 	ri->ri_stride = sc->sc_stride;
    273 	ri->ri_flg = RI_CENTER;
    274 
    275 #if _LP64
    276 	/*
    277 	 * XXX
    278 	 * Assuming all 64bit SPARCs are fast enough to render anti-aliased
    279 	 * text on the fly. Matters only as long as we don't have acceleration
    280 	 * and glyphcache.
    281 	 */
    282 	if (ri->ri_depth == 8)
    283 		ri->ri_flg |= RI_8BIT_IS_RGB | RI_ENABLE_ALPHA;
    284 #endif
    285 
    286 	ri->ri_bits = sc->sc_fbaddr;
    287 	scr->scr_flags |= VCONS_DONT_READ;
    288 
    289 	rasops_init(ri, 0, 0);
    290 
    291 	ri->ri_caps = WSSCREEN_REVERSE | WSSCREEN_WSCOLORS;
    292 
    293 	rasops_reconfig(ri, ri->ri_height / ri->ri_font->fontheight,
    294 		    ri->ri_width / ri->ri_font->fontwidth);
    295 }
    296 
    297 static int
    298 mgx_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
    299     struct lwp *l)
    300 {
    301 	struct vcons_data *vd = v;
    302 	struct mgx_softc *sc = vd->cookie;
    303 	struct wsdisplay_fbinfo *wdf;
    304 	struct vcons_screen *ms = vd->active;
    305 
    306 	switch (cmd) {
    307 		case WSDISPLAYIO_GTYPE:
    308 			*(u_int *)data = WSDISPLAY_TYPE_MGX;
    309 			return 0;
    310 
    311 		case WSDISPLAYIO_GINFO:
    312 			wdf = (void *)data;
    313 			wdf->height = sc->sc_height;
    314 			wdf->width = sc->sc_width;
    315 			wdf->depth = 8;
    316 			wdf->cmsize = 256;
    317 			return 0;
    318 
    319 		case FBIOGVIDEO:
    320 		case WSDISPLAYIO_GVIDEO:
    321 			*(int *)data = 1;
    322 			return 0;
    323 
    324 		case WSDISPLAYIO_SVIDEO:
    325 		case FBIOSVIDEO:
    326 			return 0;
    327 
    328 		case WSDISPLAYIO_LINEBYTES:
    329 			{
    330 				int *ret = (int *)data;
    331 				*ret = sc->sc_stride;
    332 			}
    333 			return 0;
    334 
    335 		case WSDISPLAYIO_SMODE:
    336 			{
    337 				int new_mode = *(int*)data;
    338 				if (new_mode != sc->sc_mode)
    339 				{
    340 					sc->sc_mode = new_mode;
    341 					if (new_mode == WSDISPLAYIO_MODE_EMUL)
    342 					{
    343 						mgx_setup(sc, 8);
    344 						vcons_redraw_screen(ms);
    345 					} else {
    346 						mgx_setup(sc, 32);
    347 					}
    348 				}
    349 			}
    350 	}
    351 
    352 	return EPASSTHROUGH;
    353 }
    354 
    355 static paddr_t
    356 mgx_mmap(void *v, void *vs, off_t offset, int prot)
    357 {
    358 	struct vcons_data *vd = v;
    359 	struct mgx_softc *sc = vd->cookie;
    360 
    361 	/* regular fb mapping at 0 */
    362 	if ((offset >= 0) && (offset < 0x400000)) {
    363 		return bus_space_mmap(sc->sc_tag, sc->sc_paddr,
    364 		    offset, prot, BUS_SPACE_MAP_LINEAR);
    365 	}
    366 
    367 	return -1;
    368 }
    369