Home | History | Annotate | Line # | Download | only in apbus
xafb.c revision 1.12.4.1
      1 /*	$NetBSD: xafb.c,v 1.12.4.1 2006/09/09 02:41:48 rpaulo Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 /* "xa" frame buffer driver.  Currently supports 1280x1024x8 only. */
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: xafb.c,v 1.12.4.1 2006/09/09 02:41:48 rpaulo Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/buf.h>
     36 #include <sys/device.h>
     37 #include <sys/ioctl.h>
     38 #include <sys/malloc.h>
     39 #include <sys/systm.h>
     40 
     41 #include <uvm/uvm_extern.h>
     42 
     43 #include <machine/adrsmap.h>
     44 #include <machine/apcall.h>
     45 
     46 #include <machine/wsconsio.h>
     47 #include <dev/wscons/wsdisplayvar.h>
     48 #include <dev/rasops/rasops.h>
     49 
     50 #include <newsmips/apbus/apbusvar.h>
     51 
     52 struct xafb_reg {
     53 	volatile u_int r0;
     54 	volatile u_int index;
     55 	volatile u_int r2;
     56 	volatile u_int zero;
     57 	volatile u_int r4;
     58 	volatile u_int r5;
     59 	volatile u_int r6;
     60 	volatile u_int rgb;
     61 };
     62 
     63 struct xafb_devconfig {
     64 	u_char *dc_fbbase;		/* VRAM base address */
     65 	paddr_t dc_fbpaddr;		/* VRAM physical address */
     66 	struct xafb_reg *dc_reg;	/* register address */
     67 	struct rasops_info dc_ri;
     68 };
     69 
     70 struct xafb_softc {
     71 	struct device sc_dev;
     72 	struct xafb_devconfig *sc_dc;
     73 	int sc_nscreens;
     74 	u_char sc_cmap_red[256];
     75 	u_char sc_cmap_green[256];
     76 	u_char sc_cmap_blue[256];
     77 };
     78 
     79 int xafb_match(struct device *, struct cfdata *, void *);
     80 void xafb_attach(struct device *, struct device *, void *);
     81 
     82 int xafb_common_init(struct xafb_devconfig *);
     83 int xafb_is_console(void);
     84 
     85 int xafb_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
     86 paddr_t xafb_mmap(void *, void *, off_t, int);
     87 int xafb_alloc_screen(void *, const struct wsscreen_descr *, void **, int *,
     88     int *, long *);
     89 void xafb_free_screen(void *, void *);
     90 int xafb_show_screen(void *, void *, int, void (*) (void *, int, int), void *);
     91 
     92 int xafb_cnattach(void);
     93 int xafb_getcmap(struct xafb_softc *, struct wsdisplay_cmap *);
     94 int xafb_putcmap(struct xafb_softc *, struct wsdisplay_cmap *);
     95 
     96 static inline void xafb_setcolor(struct xafb_devconfig *, int, int, int, int);
     97 
     98 CFATTACH_DECL(xafb, sizeof(struct xafb_softc),
     99     xafb_match, xafb_attach, NULL, NULL);
    100 
    101 struct xafb_devconfig xafb_console_dc;
    102 
    103 struct wsdisplay_accessops xafb_accessops = {
    104 	xafb_ioctl,
    105 	xafb_mmap,
    106 	xafb_alloc_screen,
    107 	xafb_free_screen,
    108 	xafb_show_screen,
    109 	NULL	/* load_font */
    110 };
    111 
    112 struct wsscreen_descr xafb_stdscreen = {
    113 	"std",
    114 	0, 0,
    115 	0,
    116 	0, 0,
    117 	WSSCREEN_REVERSE
    118 };
    119 
    120 const struct wsscreen_descr *xafb_scrlist[] = {
    121 	&xafb_stdscreen
    122 };
    123 
    124 struct wsscreen_list xafb_screenlist = {
    125 	sizeof(xafb_scrlist) / sizeof(xafb_scrlist[0]), xafb_scrlist
    126 };
    127 
    128 int
    129 xafb_match(struct device *parent, struct cfdata *match, void *aux)
    130 {
    131 	struct apbus_attach_args *apa = aux;
    132 
    133 	if (strcmp(apa->apa_name, "xa") != 0)
    134 		return 0;
    135 
    136 	return 1;
    137 }
    138 
    139 void
    140 xafb_attach(struct device *parent, struct device *self, void *aux)
    141 {
    142 	struct xafb_softc *sc = (void *)self;
    143 	struct apbus_attach_args *apa = aux;
    144 	struct wsemuldisplaydev_attach_args wsa;
    145 	struct xafb_devconfig *dc;
    146 	struct rasops_info *ri;
    147 	int console, i;
    148 
    149 	console = xafb_is_console();
    150 
    151 	if (console) {
    152 		dc = &xafb_console_dc;
    153 		ri = &dc->dc_ri;
    154 		sc->sc_nscreens = 1;
    155 	} else {
    156 		dc = malloc(sizeof(struct xafb_devconfig), M_DEVBUF,
    157 		    M_WAITOK|M_ZERO);
    158 		dc->dc_fbpaddr = (paddr_t)0x10000000;
    159 		dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
    160 		dc->dc_reg = (void *)(apa->apa_hwbase + 0x3000);
    161 		if (xafb_common_init(dc) != 0) {
    162 			printf(": couldn't initialize device\n");
    163 			return;
    164 		}
    165 
    166 		ri = &dc->dc_ri;
    167 
    168 		/* clear screen */
    169 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, 0);
    170 	}
    171 	sc->sc_dc = dc;
    172 
    173 	for (i = 0; i < 256; i++) {
    174 		sc->sc_cmap_red[i] = i;
    175 		sc->sc_cmap_green[i] = i;
    176 		sc->sc_cmap_blue[i] = i;
    177 	}
    178 
    179 	printf(": %d x %d, %dbpp\n", ri->ri_width, ri->ri_height, ri->ri_depth);
    180 
    181 	wsa.console = console;
    182 	wsa.scrdata = &xafb_screenlist;
    183 	wsa.accessops = &xafb_accessops;
    184 	wsa.accesscookie = sc;
    185 
    186 	config_found(self, &wsa, wsemuldisplaydevprint);
    187 }
    188 
    189 void
    190 xafb_setcolor(struct xafb_devconfig *dc, int index, int r, int g, int b)
    191 {
    192 	volatile struct xafb_reg *reg = dc->dc_reg;
    193 
    194 	reg->index = index;
    195 	reg->zero = 0;
    196 	reg->rgb = r;
    197 	reg->rgb = g;
    198 	reg->rgb = b;
    199 }
    200 
    201 int
    202 xafb_common_init(struct xafb_devconfig *dc)
    203 {
    204 	struct rasops_info *ri = &dc->dc_ri;
    205 	int i;
    206 
    207 	for (i = 0; i < 256; i++)
    208 		xafb_setcolor(dc, i, i, i, i);
    209 
    210 	/* initialize rasops */
    211 	ri->ri_width = 1280;
    212 	ri->ri_height = 1024;
    213 	ri->ri_depth = 8;
    214 	ri->ri_stride = 2048;
    215 	ri->ri_bits = (void *)dc->dc_fbbase;
    216 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
    217 
    218 	rasops_init(ri, 44, 100);
    219 
    220 	/* mono */
    221 	ri->ri_devcmap[0] = 0;				/* bg */
    222 	ri->ri_devcmap[1] = 0xffffffff;			/* fg */
    223 
    224 	xafb_stdscreen.nrows = ri->ri_rows;
    225 	xafb_stdscreen.ncols = ri->ri_cols;
    226 	xafb_stdscreen.textops = &ri->ri_ops;
    227 	xafb_stdscreen.capabilities = ri->ri_caps;
    228 
    229 	return 0;
    230 }
    231 
    232 int
    233 xafb_is_console(void)
    234 {
    235 	volatile u_int *dipsw = (void *)NEWS5000_DIP_SWITCH;
    236 
    237 	if (*dipsw & 1)					/* XXX right? */
    238 		return 1;
    239 
    240 	return 0;
    241 }
    242 
    243 int
    244 xafb_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag, struct lwp *l)
    245 {
    246 	struct xafb_softc *sc = v;
    247 	struct xafb_devconfig *dc = sc->sc_dc;
    248 	struct newsmips_wsdisplay_fbinfo *nwdf = (void *)data;
    249 	struct wsdisplay_fbinfo *wdf = (void *)data;
    250 
    251 	switch (cmd) {
    252 	case WSDISPLAYIO_GTYPE:
    253 		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
    254 		return 0;
    255 
    256 	case NEWSMIPS_WSDISPLAYIO_GINFO:
    257 		nwdf->stride = dc->dc_ri.ri_stride;
    258 		/* FALLTHROUGH */
    259 	case WSDISPLAYIO_GINFO:
    260 		wdf->height = dc->dc_ri.ri_height;
    261 		wdf->width = dc->dc_ri.ri_width;
    262 		wdf->depth = dc->dc_ri.ri_depth;
    263 		wdf->cmsize = 256;
    264 		return 0;
    265 
    266 	case WSDISPLAYIO_GETCMAP:
    267 		return xafb_getcmap(sc, (struct wsdisplay_cmap *)data);
    268 
    269 	case WSDISPLAYIO_PUTCMAP:
    270 		return xafb_putcmap(sc, (struct wsdisplay_cmap *)data);
    271 
    272 	/* case WSDISPLAYIO_SVIDEO: */
    273 	}
    274 	return EPASSTHROUGH;
    275 }
    276 
    277 paddr_t
    278 xafb_mmap(void *v, void *vs, off_t offset, int prot)
    279 {
    280 	struct xafb_softc *sc = v;
    281 	struct xafb_devconfig *dc = sc->sc_dc;
    282 	struct rasops_info *ri = &dc->dc_ri;
    283 
    284 	if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
    285 		return -1;
    286 
    287 	return mips_btop(dc->dc_fbpaddr + offset);
    288 }
    289 
    290 int
    291 xafb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc,
    292     void **cookiep, int *ccolp, int *crowp, long *attrp)
    293 {
    294 	struct xafb_softc *sc = v;
    295 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
    296 	long defattr;
    297 
    298 	if (sc->sc_nscreens > 0)
    299 		return ENOMEM;
    300 
    301 	*cookiep = ri;
    302 	*ccolp = *crowp = 0;
    303 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    304 	*attrp = defattr;
    305 	sc->sc_nscreens++;
    306 
    307 	return 0;
    308 }
    309 
    310 void
    311 xafb_free_screen(void *v, void *cookie)
    312 {
    313 	struct xafb_softc *sc = v;
    314 
    315 	if (sc->sc_dc == &xafb_console_dc)
    316 		panic("xafb_free_screen: console");
    317 
    318 	sc->sc_nscreens--;
    319 }
    320 
    321 int
    322 xafb_show_screen(void *v, void *cookie, int waitok,
    323     void (*cb)(void *, int, int), void *cbarg)
    324 {
    325 
    326 	return 0;
    327 }
    328 
    329 int
    330 xafb_cnattach(void)
    331 {
    332 	struct xafb_devconfig *dc = &xafb_console_dc;
    333 	struct rasops_info *ri = &dc->dc_ri;
    334 	long defattr;
    335 	int crow = 0;
    336 
    337 	if (!xafb_is_console())
    338 		return -1;
    339 
    340 	dc->dc_fbpaddr = (paddr_t)0x10000000;
    341 	dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
    342 	dc->dc_reg = (void *)0xb4903000;			/* XXX */
    343 	xafb_common_init(dc);
    344 
    345 	crow = 0;			/* XXX current cursor pos */
    346 
    347 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    348 	wsdisplay_cnattach(&xafb_stdscreen, &dc->dc_ri, 0, crow, defattr);
    349 
    350 	return 0;
    351 }
    352 
    353 int
    354 xafb_getcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
    355 {
    356 	u_int index = cm->index;
    357 	u_int count = cm->count;
    358 	int error;
    359 
    360 	if (index >= 256 || count > 256 || index + count > 256)
    361 		return EINVAL;
    362 
    363 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    364 	if (error)
    365 		return error;
    366 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    367 	if (error)
    368 		return error;
    369 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    370 	if (error)
    371 		return error;
    372 
    373 	return 0;
    374 }
    375 
    376 int
    377 xafb_putcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
    378 {
    379 	u_int index = cm->index;
    380 	u_int count = cm->count;
    381 	int i, error;
    382 	u_char rbuf[256], gbuf[256], bbuf[256];
    383 	u_char *r, *g, *b;
    384 
    385 	if (cm->index >= 256 || cm->count > 256 ||
    386 	    (cm->index + cm->count) > 256)
    387 		return EINVAL;
    388 	error = copyin(cm->red, &rbuf[index], count);
    389 	if (error)
    390 		return error;
    391 	error = copyin(cm->green, &gbuf[index], count);
    392 	if (error)
    393 		return error;
    394 	error = copyin(cm->blue, &bbuf[index], count);
    395 	if (error)
    396 		return error;
    397 
    398 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    399 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    400 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    401 
    402 	r = &sc->sc_cmap_red[index];
    403 	g = &sc->sc_cmap_green[index];
    404 	b = &sc->sc_cmap_blue[index];
    405 	for (i = 0; i < count; i++)
    406 		xafb_setcolor(sc->sc_dc, index++, *r++, *g++, *b++);
    407 	return 0;
    408 }
    409