Home | History | Annotate | Line # | Download | only in apbus
xafb.c revision 1.15.4.1
      1 /*	$NetBSD: xafb.c,v 1.15.4.1 2010/08/11 22:52:28 yamt 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.15.4.1 2010/08/11 22:52:28 yamt 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 uint32_t r0;
     54 	volatile uint32_t index;
     55 	volatile uint32_t r2;
     56 	volatile uint32_t zero;
     57 	volatile uint32_t r4;
     58 	volatile uint32_t r5;
     59 	volatile uint32_t r6;
     60 	volatile uint32_t rgb;
     61 };
     62 
     63 struct xafb_devconfig {
     64 	uint8_t *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 	device_t sc_dev;
     72 	struct xafb_devconfig *sc_dc;
     73 	int sc_nscreens;
     74 	uint8_t sc_cmap_red[256];
     75 	uint8_t sc_cmap_green[256];
     76 	uint8_t sc_cmap_blue[256];
     77 };
     78 
     79 int xafb_match(device_t, cfdata_t, void *);
     80 void xafb_attach(device_t, device_t, 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, void *, 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_NEW(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 	__arraycount(xafb_scrlist), xafb_scrlist
    126 };
    127 
    128 int
    129 xafb_match(device_t parent, cfdata_t cf, 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(device_t parent, device_t self, void *aux)
    141 {
    142 	struct xafb_softc *sc = device_private(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 	sc->sc_dev = self;
    150 	console = xafb_is_console();
    151 
    152 	if (console) {
    153 		dc = &xafb_console_dc;
    154 		ri = &dc->dc_ri;
    155 		ri->ri_flg &= ~RI_NO_AUTO;
    156 		sc->sc_nscreens = 1;
    157 	} else {
    158 		dc = malloc(sizeof(struct xafb_devconfig), M_DEVBUF,
    159 		    M_WAITOK|M_ZERO);
    160 		dc->dc_fbpaddr = (paddr_t)0x10000000;
    161 		dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
    162 		dc->dc_reg = (void *)(apa->apa_hwbase + 0x3000);
    163 		if (xafb_common_init(dc) != 0) {
    164 			aprint_error(": couldn't initialize device\n");
    165 			return;
    166 		}
    167 
    168 		ri = &dc->dc_ri;
    169 
    170 		/* clear screen */
    171 		(*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, 0);
    172 	}
    173 	sc->sc_dc = dc;
    174 
    175 	for (i = 0; i < 256; i++) {
    176 		sc->sc_cmap_red[i] = i;
    177 		sc->sc_cmap_green[i] = i;
    178 		sc->sc_cmap_blue[i] = i;
    179 	}
    180 
    181 	aprint_normal(": %d x %d, %dbpp\n",
    182 	    ri->ri_width, ri->ri_height, ri->ri_depth);
    183 
    184 	wsa.console = console;
    185 	wsa.scrdata = &xafb_screenlist;
    186 	wsa.accessops = &xafb_accessops;
    187 	wsa.accesscookie = sc;
    188 
    189 	config_found(self, &wsa, wsemuldisplaydevprint);
    190 }
    191 
    192 void
    193 xafb_setcolor(struct xafb_devconfig *dc, int index, int r, int g, int b)
    194 {
    195 	volatile struct xafb_reg *reg = dc->dc_reg;
    196 
    197 	reg->index = index;
    198 	reg->zero = 0;
    199 	reg->rgb = r;
    200 	reg->rgb = g;
    201 	reg->rgb = b;
    202 }
    203 
    204 int
    205 xafb_common_init(struct xafb_devconfig *dc)
    206 {
    207 	struct rasops_info *ri = &dc->dc_ri;
    208 	int i;
    209 
    210 	for (i = 0; i < 256; i++)
    211 		xafb_setcolor(dc, i, i, i, i);
    212 
    213 	/* initialize rasops */
    214 	ri->ri_width = 1280;
    215 	ri->ri_height = 1024;
    216 	ri->ri_depth = 8;
    217 	ri->ri_stride = 2048;
    218 	ri->ri_bits = (void *)dc->dc_fbbase;
    219 	ri->ri_flg = RI_FORCEMONO | RI_FULLCLEAR;
    220 	if (dc == &xafb_console_dc)
    221 		ri->ri_flg |= RI_NO_AUTO;
    222 
    223 	rasops_init(ri, 44, 100);
    224 
    225 	/* mono */
    226 	ri->ri_devcmap[0] = 0;				/* bg */
    227 	ri->ri_devcmap[1] = 0xffffffff;			/* fg */
    228 
    229 	xafb_stdscreen.nrows = ri->ri_rows;
    230 	xafb_stdscreen.ncols = ri->ri_cols;
    231 	xafb_stdscreen.textops = &ri->ri_ops;
    232 	xafb_stdscreen.capabilities = ri->ri_caps;
    233 
    234 	return 0;
    235 }
    236 
    237 int
    238 xafb_is_console(void)
    239 {
    240 	volatile uint32_t *dipsw = (void *)NEWS5000_DIP_SWITCH;
    241 
    242 	if (*dipsw & 1)					/* XXX right? */
    243 		return 1;
    244 
    245 	return 0;
    246 }
    247 
    248 int
    249 xafb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, struct lwp *l)
    250 {
    251 	struct xafb_softc *sc = v;
    252 	struct xafb_devconfig *dc = sc->sc_dc;
    253 	struct newsmips_wsdisplay_fbinfo *nwdf = (void *)data;
    254 	struct wsdisplay_fbinfo *wdf = (void *)data;
    255 
    256 	switch (cmd) {
    257 	case WSDISPLAYIO_GTYPE:
    258 		*(int *)data = WSDISPLAY_TYPE_UNKNOWN;	/* XXX */
    259 		return 0;
    260 
    261 	case NEWSMIPS_WSDISPLAYIO_GINFO:
    262 		nwdf->stride = dc->dc_ri.ri_stride;
    263 		/* FALLTHROUGH */
    264 	case WSDISPLAYIO_GINFO:
    265 		wdf->height = dc->dc_ri.ri_height;
    266 		wdf->width = dc->dc_ri.ri_width;
    267 		wdf->depth = dc->dc_ri.ri_depth;
    268 		wdf->cmsize = 256;
    269 		return 0;
    270 
    271 	case WSDISPLAYIO_GETCMAP:
    272 		return xafb_getcmap(sc, (struct wsdisplay_cmap *)data);
    273 
    274 	case WSDISPLAYIO_PUTCMAP:
    275 		return xafb_putcmap(sc, (struct wsdisplay_cmap *)data);
    276 
    277 	/* case WSDISPLAYIO_SVIDEO: */
    278 	}
    279 	return EPASSTHROUGH;
    280 }
    281 
    282 paddr_t
    283 xafb_mmap(void *v, void *vs, off_t offset, int prot)
    284 {
    285 	struct xafb_softc *sc = v;
    286 	struct xafb_devconfig *dc = sc->sc_dc;
    287 	struct rasops_info *ri = &dc->dc_ri;
    288 
    289 	if (offset >= (ri->ri_stride * ri->ri_height) || offset < 0)
    290 		return -1;
    291 
    292 	return mips_btop(dc->dc_fbpaddr + offset);
    293 }
    294 
    295 int
    296 xafb_alloc_screen(void *v, const struct wsscreen_descr *scrdesc,
    297     void **cookiep, int *ccolp, int *crowp, long *attrp)
    298 {
    299 	struct xafb_softc *sc = v;
    300 	struct rasops_info *ri = &sc->sc_dc->dc_ri;
    301 	long defattr;
    302 
    303 	if (sc->sc_nscreens > 0)
    304 		return ENOMEM;
    305 
    306 	*cookiep = ri;
    307 	*ccolp = *crowp = 0;
    308 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    309 	*attrp = defattr;
    310 	sc->sc_nscreens++;
    311 
    312 	return 0;
    313 }
    314 
    315 void
    316 xafb_free_screen(void *v, void *cookie)
    317 {
    318 	struct xafb_softc *sc = v;
    319 
    320 	if (sc->sc_dc == &xafb_console_dc)
    321 		panic("xafb_free_screen: console");
    322 
    323 	sc->sc_nscreens--;
    324 }
    325 
    326 int
    327 xafb_show_screen(void *v, void *cookie, int waitok,
    328     void (*cb)(void *, int, int), void *cbarg)
    329 {
    330 
    331 	return 0;
    332 }
    333 
    334 int
    335 xafb_cnattach(void)
    336 {
    337 	struct xafb_devconfig *dc = &xafb_console_dc;
    338 	struct rasops_info *ri = &dc->dc_ri;
    339 	long defattr;
    340 	int crow = 0;
    341 
    342 	if (!xafb_is_console())
    343 		return -1;
    344 
    345 	dc->dc_fbpaddr = (paddr_t)0x10000000;
    346 	dc->dc_fbbase = (void *)MIPS_PHYS_TO_KSEG1(dc->dc_fbpaddr);
    347 	dc->dc_reg = (void *)0xb4903000;			/* XXX */
    348 	xafb_common_init(dc);
    349 
    350 	crow = 0;			/* XXX current cursor pos */
    351 
    352 	(*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
    353 	wsdisplay_cnattach(&xafb_stdscreen, &dc->dc_ri, 0, crow, defattr);
    354 
    355 	return 0;
    356 }
    357 
    358 int
    359 xafb_getcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
    360 {
    361 	u_int index = cm->index;
    362 	u_int count = cm->count;
    363 	int error;
    364 
    365 	if (index >= 256 || count > 256 || index + count > 256)
    366 		return EINVAL;
    367 
    368 	error = copyout(&sc->sc_cmap_red[index],   cm->red,   count);
    369 	if (error)
    370 		return error;
    371 	error = copyout(&sc->sc_cmap_green[index], cm->green, count);
    372 	if (error)
    373 		return error;
    374 	error = copyout(&sc->sc_cmap_blue[index],  cm->blue,  count);
    375 	if (error)
    376 		return error;
    377 
    378 	return 0;
    379 }
    380 
    381 int
    382 xafb_putcmap(struct xafb_softc *sc, struct wsdisplay_cmap *cm)
    383 {
    384 	u_int index = cm->index;
    385 	u_int count = cm->count;
    386 	int i, error;
    387 	u_char rbuf[256], gbuf[256], bbuf[256];
    388 	u_char *r, *g, *b;
    389 
    390 	if (cm->index >= 256 || cm->count > 256 ||
    391 	    (cm->index + cm->count) > 256)
    392 		return EINVAL;
    393 	error = copyin(cm->red, &rbuf[index], count);
    394 	if (error)
    395 		return error;
    396 	error = copyin(cm->green, &gbuf[index], count);
    397 	if (error)
    398 		return error;
    399 	error = copyin(cm->blue, &bbuf[index], count);
    400 	if (error)
    401 		return error;
    402 
    403 	memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
    404 	memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
    405 	memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
    406 
    407 	r = &sc->sc_cmap_red[index];
    408 	g = &sc->sc_cmap_green[index];
    409 	b = &sc->sc_cmap_blue[index];
    410 	for (i = 0; i < count; i++)
    411 		xafb_setcolor(sc->sc_dc, index++, *r++, *g++, *b++);
    412 	return 0;
    413 }
    414